how to print native c code's call stack

7,778 views
Skip to first unread message

shawn wang

unread,
Sep 12, 2012, 10:07:15 AM9/12/12
to android-ndk
hi,all,how to print native c code's call stack.
in C++ code,we can use

#include <utils/CallStack.h>
...
CallStack stack;
stack.update();
stack.dump("");

but this could not be used in c code.

Vladimir Kunschikov

unread,
Nov 22, 2013, 2:20:55 AM11/22/13
to andro...@googlegroups.com
 In plain C:
#include <corkscrew/backtrace.h>
void print_backtrace()
{  
    backtrace_frame_t frames[256];
    ssize_t count = unwind_backtrace(frames, 2, sizeof(frames));
    backtrace_symbol_t symbols[count];

    get_backtrace_symbols(frames, count, symbols);
    for(size_t i = 0; i < count; i++){
        char line[1024] = {0};
        format_backtrace_line(i, frames + i, symbols +i, line, 1024);
        log(LOG_INFO, " #%d %s\n", i,  line); /* use your logging here */
    }
    free_backtrace_symbols(symbols, count);
}
 You should link your app with libcorkscrew.
I have removed the call to __cxx_demangle() in system/core/libcorkscrew/demangle.c to simplify linking. It works anyway, you can use the c++filt tool for the demangling.

David Turner

unread,
Nov 22, 2013, 5:04:41 AM11/22/13
to andro...@googlegroups.com
FYI, an alternative would be libbacktrace: https://github.com/mirrors/gcc/tree/master/libbacktrace
It relies on <unwind.h>, which is implemented in libgcc.a (i.e. it's already linked into your binaries by default).



--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.

To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/groups/opt_out.

Message has been deleted

Vladimir Kunschikov

unread,
Nov 22, 2013, 7:29:59 AM11/22/13
to andro...@googlegroups.com
  At firstt I thought that libcorkscrew is a wrapper around this library, but it isn't.  Can you give a sample of this library usage?
Reply all
Reply to author
Forward
0 new messages