通过Python解析tombstone

方法:

下载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

参考文章:

1、跟踪Android callback 调用堆栈

这边文章详细讲解比较详细,且讲到了寄存器、内存地址的计算方式,推荐。

2、Android Native/Tombstone Crash Log 详细分析

3、Android C/JAVA 输出CallStack

4、Android NDK Tombstone/Crash 分析

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值