How to Print Function Caller Stack in Android Log File

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Print Function Caller Stack in Android Log File

How to Print Function Caller Stack in Android Log File

Print caller stack may help you to analyze the program and find out the caller stack more easily.

You can write your code like this:

Java:

     Exception e = new Exception();

     Log.e(TAG,"xxx",e);

C++ file:

      #include <utils/Callstack.h>

     android::CallStack stack;

     stack.update(1,30);

     stacn.dump("xxx");

Then you can see the function's caller stack in Android main log file.

C file:

#include <corkscrew/backtrace.h>

#define MAX_DEPTH 31
#define MAX_BACKTRACE_LINE_LENGTH 800

static backtrace_frame_t mStack[MAX_DEPTH];
static size_t mCount;

void csupdate(int32_t ignoreDepth, int32_t maxDepth) {
    if (maxDepth > MAX_DEPTH) {
        maxDepth = MAX_DEPTH;
    }
    ssize_t count = unwind_backtrace(mStack, ignoreDepth + 1, maxDepth);
    mCount = count > 0 ? count : 0;
}

void csdump(const char* prefix)\
{
size_t i = 0;
    backtrace_symbol_t symbols[MAX_DEPTH];

    get_backtrace_symbols(mStack, mCount, symbols);

    for (i = 0; i < mCount; i++) {
        char line[MAX_BACKTRACE_LINE_LENGTH];
        format_backtrace_line(i, &mStack[i], &symbols[i],
                line, MAX_BACKTRACE_LINE_LENGTH);
        ALOGE("%s%s", prefix, line);
    }

    free_backtrace_symbols(symbols, mCount);
}

void myFunc()

{

     csupdate(1, 30);

     csdump("myprefix");

}

In Android.mk, add libcorkscrew, as below

LOCAL_SHARED_LIBRARIES := libxxx libyyy libcorkscrew

Labels (1)
Tags (2)
No ratings
Version history
Last update:
‎10-13-2012 12:57 AM
Updated by: