How to Print Function Caller Stack in Android Log File

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

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

标签 (1)
标记 (2)
无评分
版本历史
最后更新:
‎10-13-2012 12:57 AM
更新人: