Friday 8 March 2019

How to profile Haxe HXCPP with Visual Studio.


Note: this should work with ANY executable under windows that has included debug information compatible with VS.

How many days have toy wasted in Haxe trying to solve bugs in HXCPP because you haven't figured out profile or debug properly?

To be honest , after several months and a game almost fully ported, I still don't know how to use HXScout, or making some debugger work in FlashDevelop (or HaxeDevelop).

I have wasted really over several hundred hours not really knowing what was going on under the hood after compiling HXCPP. The only weapon I had for debugging Haxe (HXCPP) was trace();

So you can imagine my code is full of those. Most are left overs just in case it breaks again.

But now I have figured out that I can use VisualStudio debug utils, which includes a profiler. A super powerful one I must say.

How Can we use it? Easy, check these steps carefuly:


Compile some haxe HXCPP program for windows using -debug.
I use Lime and OpenFL so my command would be:
lime build window -debug
 I think you need to add some parameters to the compilation project.xml. In openFL/lime I used:

<cpp build-library="hxcpp" />
<haxelib name="hxcpp" />
     <section if="debug">
         
        <haxedef name="HXCPP_DEBUGGER"/>
        <haxedef name="HXCPP_STACK_TRACE"/>
        <haxedef name="HXCPP_STACK_LINE"/>
        <haxedef name="HXCPP_CHECK_POINTER"/>
    </section>

   
 
Not sure if everything in there is necessary I just added them all. I'm too lazy (busy) to recompile removing one by one till I have an optimum configuration .  If you do it for me, I will edit the post.

Then once it has been fully compiled, you will need to open the EXE file. And right after, after go to Visual Studio and attach debugger to the process, like this:

 Next, find your computer and the executable (process) for debugging:

Attach it now, you will start seeing Events / CPU / Memory usage:
Then, lets profile this thing: Enable [Heap Profiling].
Now it's time to take snapshots to be able to analyse the data:
Nice, you should get now something like this:
Take another snapshot to be able to compare if, in this case, memory is properly allocated.
Click the difference memory text . And you will get a panel full of information , here on the left side...
 Now double click in one of the elements to see more information about it.

Now we get all the instances of that object type, how much memory is taking each one of them, and how long they have been allocated in memory.

You can even double click to view the source code and add breakpoints if necessary. It's great.

Et violá , this is all you need to get you started in the crazy world of profiling.

This is how you profile using Visual Studio . It's super handy for HXCPP developers.

You are welcome ;)