IL2CPP Internals - Debugging tips for generated code
This is the third blog post in the
IL2CPP Internals
series. In this post, we will explore some tips which make debugging C++ code
generated by IL2CPP a little bit easier. We will see how to set breakpoints, view the content of strings and user defined
types and determine where exceptions occur.
As we get into this, consider that we are debugging generated C++ code created from .NET IL code.
So debugging it will likely not be the most pleasant experience. However, with a few of these tips,
it is possible to gain meaningful insight into how the code for a Unity project executes on
the actual target device (we’ll talk a little bit about debugging managed code at the end of the post).
Also, be prepared for the generated code in your project to differ from this code. With each new version of Unity,
we are looking for ways to make the generated code better, faster and smaller.
The setup
For this post, I’m using Unity 5.0.1p3 on OSX. I’ll use the same example project as in the
post about generated code ,
but this time I’ll build for the iOS target using the IL2CPP scripting backend. As I did in the previous post,
I’ll build with the “Development Player” option selected, so that il2cpp.exe will generate C++ code with type and method
names based on the names in the IL code.
After Unity is finished generating the Xcode project, I can open it in Xcode
(I have version 6.3.1, but any recent version should work), choose my target device (an iPad Mini 3, but any iOS device should work)
and build the project in Xcode.
Setting breakpoints
Before running the project, I’ll first set a breakpoint at the top of the Start method in the HelloWorld class.
As we saw in the previous post, the name of this method in the generated C++ code is HelloWorld_Start_m3.
We can use Cmd+Shift+O and start typing the name of this method to find in in Xcode, then set a breakpoint in it.
We can also choose Debug > Breakpoints > Create Symbolic Breakpoint in XCode, and set it to break at this method.
Now when I run the Xcode project, I immediately see it break at the start of the method.
We can set breakpoints on other methods in the generated code like this if we know the name of the method. We can also set
breakpoints in Xcode at a specific line in one of the generated code files. In fact, all of the generated files are part of
the Xcode project. You will find them in the Project Navigator in the Classes/Native directory.