on-demand callstack on android Jelly Bean

by groleo

Up to Jelly Bean, there were no means to obtain the callstack in a programmatic.
You could use libunwind or try to crash your process so you could trigger debuggerd, but that was it.

Now Jelly Bean offers the android::CallStack class. You can find it in frameworks/native/libs/utils/CallStack.cpp.

It will log the callstack using logcat:

D/CallStack( 8043): (null)#00  pc 00000731  /system/bin/test-callstack
D/CallStack( 8043): (null)#01  pc 000005db  /system/bin/test-callstack
D/CallStack( 8043): (null)#02  pc 00019929  /system/lib/libc.so
D/CallStack( 8043): (null)#03  pc 0000060d  /system/bin/test-callstack

As example, the first line is translated into file:line using

addr2line -e ${ANDROID_PRODUCT_OUT}/symbols/system/bin/test-callstack 00000731

The code snippet looks like this:

// android::CallStack::dump() uses ALOGD, so DEBUG needs to be defined
#define DEBUG
#include <CallStack.h>
...
    android::CallStack a;
    a.update();
    a.dump();
...
Advertisements