corner stamp here.

Tips & Tricks: Debugging with Android NDK stack traces

Stuck in the hell of crashing applications? Don’t know how to find the tiny allocation or deallocation mistake hidden in a code stack of thousands of lines? Here is your way to heaven.

08-22 23:27:40.730: INFO/DEBUG(65): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
08-22 23:27:40.730: INFO/DEBUG(65): Build fingerprint: 'htc_wwe/htc_bravo/bravo/bravo:2.2/FRF91/218634:user/release-keys'
08-22 23:27:40.730: INFO/DEBUG(65): pid: 2474, tid: 2485  >>> com.test <<<
08-22 23:27:40.730: INFO/DEBUG(65): signal 11 (SIGSEGV), fault addr 00000001
08-22 23:27:40.730: INFO/DEBUG(65):  r0 00000001  r1 00000000  r2 afd438e4  r3 00000001
08-22 23:27:40.730: INFO/DEBUG(65):  r4 4825395c  r5 00001000  r6 00001000  r7 00000001
08-22 23:27:40.730: INFO/DEBUG(65):  r8 48253ad8  r9 432faf40  10 802a3448  fp 432faf40
...
08-22 23:27:40.730: INFO/DEBUG(65):  d30 0000000000000000  d31 0000000000000000
08-22 23:27:40.730: INFO/DEBUG(65):  scr 80000012
08-22 23:27:40.790: INFO/DEBUG(65):          #00  pc 00018656  /data/data/com.test/lib/libmylib.so
08-22 23:27:40.790: INFO/DEBUG(65):          #01  pc 000186d2  /data/data/com.test/lib/libmylib.so
08-22 23:27:40.790: INFO/DEBUG(65):          #02  pc 00018708  /data/data/com.test/lib/libmylib.so
...

Well if you understand that, then it means that either you’re an overly talented developer or you’d rather look for an ophthalmologist :). But you’re just a normal coder, than at best you know that this is a stack trace. An Android native stack trace to be more precise, generated after an application crash.

Hopefully, some tools are available to “decrypt” that mysterious data: arm-eabi-addr2line. This tools is in the “${ANDROID_NDK}/build/prebuilt/linux-x86/arm-eabi-[version]/bin/” directory which contains some utility tools for ARM processors. And combine with the original library that generated it, you can find the incriminated methods and file lines. And that’s priceless when you can’t debug your code!

First ensure your native code is compiled in Debug mode to access code information (“APP_OPTIM := debug” in your application.mk). Then call the executable with your .so compiled library, for example:

${Android-NDK}/build/prebuilt/linux-x86/arm-eabi-[version]/bin/arm-eabi-addr2line -C -f -e libmylib.so

Then just type the address, the one you can find after the “pc” directive, for example:

00018656

taken from line “#00  pc 00018656  /system/lib/libstlport.so“.

That’s all!

  1. matt

    awesome. for ndk-5 the exe is in a different place…

    android-ndk-r5\toolchains\arm-eabi-4.4.0\prebuilt\windows\bin

  2. www-admin

    Thanks for your information!
    I guess NDK is not really mature yet (see discussion about STLport)…

Leave a Reply