aardvarkk aardvarkk - 1 month ago 8
Android Question

Step In with native code leads to garbage in Android Studio

I have native code I've built using Gradle in Android Studio. Most of the debugging seems to work fine in C++, but I just added a new class and when I try to step in to the function call it takes me to an absolutely bizarre location in a completely unrelated area of code.

For example, I have the following line of code with a breakpoint:

SemVer ver_cl = PlatformHelper::getAppVersion();


I run "Step In", and I end up in
gnu-libstdc++
implementation of hashtable.h on a seemingly-arbitrary line of code.

I'm running in a simulator. I've made sure to sync my project to the Gradle files, but I'm not sure what could be causing this kind of behaviour.

Answer

Implementation of PlatformHelper::getAppVersion(); can give insight to the problem. In this particular case it occurred that the method had no return value. For historical reasons, it is not an error in C++, but modern compilers usually trigger a warning in such situations and by adding -Werror compilation flag for GCC you can force it to treat warnings as errors. When non-void method does not return a value, the stack gets corrupted and control is returned to a random place. aardvarkk was "lucky" that the address occurred to be a valid one.

Another situation when similar behaviour can be seen is when when you compile your code with optimization (anything other then -O0). One of the strongest optimization techniques is inlining: function bodies are inserted directly in-to the place where they are called. This works especially good for templates. Downside of this process is that some functions are completely eliminated and they are not visible in stacktrace. So it is possible that when you step into PlatformHelper::getAppVersion(); you directly drop somewhere in-to it's internals where hashmap is used because all the code between invocation of getAppVersion and usage of the hashmap has been optimized away. But in this case your program will function correctly, only debugging will be cumbered a bit.