方法:
下载my_parse_stack.py 到android sdk目录,然后执行 ./my_parse_stack.py tombstone_xx 即可打印出调用堆栈。
想要让cpp crash的时候可以被android捕捉,保存到tombstone中,则需要native app 不对信号量做处理。
当然,如果一定要在代码中捕捉信号量,并打印对应的堆栈信息,方法如下:
1、代码
android native c++打印堆栈
android::CallStack stack;
stack.update(2); // void update(int32_t ignoreDepth=1, pid_t tid=BACKTRACE_CURRENT_THREAD);
android::String8 info = stack.toString(); // 获得堆栈内容
CLog::Log("CrashLog/device_exception", LOGCATCH, info.string()); // 输出到log中
2、需要在Android.mk中libutils.so库
对应的class CallStack的代码实现在system/core/libutils/CallStack.cpp中,头文件定义在system/core/include/utils/CallStack.h,如下为android 5.1 的头文件声明:
class CallStack声明
namespace android {
class Printer;
// Collect/print the call stack (function, file, line) traces for a single thread.
class CallStack {
public:
// Create an empty call stack. No-op.
CallStack();
// Create a callstack with the current thread's stack trace.
// Immediately dump it to logcat using the given logtag.
CallStack(const char* logtag, int32_t ignoreDepth=1);
~CallStack();
// Reset the stack frames (same as creating an empty call stack).
void clear() { mFrameLines.clear(); }
// Immediately collect the stack traces for the specified thread.
// The default is to dump the stack of the current call.
void update(int32_t ignoreDepth=1, pid_t tid=BACKTRACE_CURRENT_THREAD);
// Dump a stack trace to the log using the supplied logtag.
void log(const char* logtag,
android_LogPriority priority = ANDROID_LOG_DEBUG,
const char* prefix = 0) const;
// Dump a stack trace to the specified file descriptor.
void dump(int fd, int indent = 0, const char* prefix = 0) const;
// Return a string (possibly very long) containing the complete stack trace.
String8 toString(const char* prefix = 0) const;
// Dump a serialized representation of the stack trace to the specified printer.
void print(Printer& printer) const;
// Get the count of stack frames that are in this call stack.
size_t size() const { return mFrameLines.size(); }
private:
Vector<String8> mFrameLines;
};
}; // namespace android
参考文章:
这边文章详细讲解比较详细,且讲到了寄存器、内存地址的计算方式,推荐。