天天看点

Using NDK-STACK to Debug Cocos2dx Android Project

‘ndk-stack’ is a simple tool that allows you to analyze stack traces as they appear in the output of ‘adb logcat’ and replace any address inside a shared library with the corresponding:values.

In the output of ‘adb logcat’, you may see something like the followings, and we must translate those:

I/DEBUG   (   ): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG   (   ): Build fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'
I/DEBUG   (   ): pid: , tid:   %gt;%gt;%gt; /data/local/ndk-tests/crasher
I/DEBUG   (   ): signal  (SIGSEGV), fault addr d9f00d8
I/DEBUG   (   ):  r0 af88  r1 a008  r2 baadf00d  r3 d9f00d8
I/DEBUG   (   ):  r4   r5 a008  r6 af88  r7 c44
I/DEBUG   (   ):  r8   r9      fp 
I/DEBUG   (   ):  ip c  sp be956cc8  lr   pc e  cpsr 
I/DEBUG   (   ):          #00  pc e  /data/local/ndk-tests/crasher
I/DEBUG   (   ):          #01  pc fe  /data/local/ndk-tests/crasher
I/DEBUG   (   ):          #02  pc f6  /data/local/ndk-tests/crasher
I/DEBUG   (   ):          #03  pc ac  /system/lib/libc.so
I/DEBUG   (   ):          #04  pc ea  /data/local/ndk-tests/crasher
I/DEBUG   (   ):          #05  pc   /data/local/ndk-tests/crasher
I/DEBUG   (   ):          #06  pc d362  /system/lib/libc.so
I/DEBUG   (   ):
           

How to use this tool

If you succeed in building Cocos2d-x android project, it will generate shared libraries in $PROJECT_PATH/obj/local/abi, where ‘abi’ stands for your device’s ABI (i.e.’armeabi’ by default);

You can feed the logcat text either as direct input to the program, e.g.:

adb logcat | $NDK/ndk-stack -sym $PROJECT_PATH/obj/local/armeabi
           

Or you can use the -dump option to specify the logcat as an input file, e.g.:

adb logcat > /tmp/foo.txt
$NDK/ndk-stack -sym $PROJECT_PATH/obj/local/armeabi -dump foo.txt
           

After that, you will get the more readable output:

********** Crash dump: **********
Build fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'
pid: , tid:   /data/local/ndk-tests/crasher 
signal  (SIGSEGV), fault addr d9f00d8
Stack frame #00  pc e  /data/local/ndk-tests/crasher : Routine zoo in /tmp/foo/crasher/jni/zoo.c:
Stack frame #01  pc fe  /data/local/ndk-tests/crasher : Routine bar in /tmp/foo/crasher/jni/bar.c:
Stack frame #02  pc f6  /data/local/ndk-tests/crasher : Routine my_comparison in /tmp/foo/crasher/jni/foo.c:
Stack frame #03  pc ac  /system/lib/libc.so
Stack frame #04  pc ea  /data/local/ndk-tests/crasher : Routine foo in /tmp/foo/crasher/jni/foo.c:
Stack frame #05  pc   /data/local/ndk-tests/crasher : Routine main in /tmp/foo/crasher/jni/main.c:
Stack frame #06  pc d362  /system/lib/libc.so
           

IMPORTANT

The tool looks for the initial line containing starts in the logcat output, i.e.something this looks like:

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
           

When copy/pasting traces, don’t forget this line from the traces, or ndk-stack won’t work correctly.

Now both the versions(linux/windows/mac) of ndk-stack are available at Android Developer .

References

$NDK_ROOT/docs/NDK-STACK.html