How to get Callstack in Android Log File


When you are running into bugs or issues in you Android Apps, callstack information of your components would be very much helpful, defintely.

Here is a post I’v found in freescale community, auther by MingZhou, it instructs you have get the caller stack information for you Android App or components. The original url is: https://community.freescale.com/docs/DOC-93624

———————-

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

 

Leave a comment

Your email address will not be published. Required fields are marked *