首先:这个问题已经被问了几次,有些答案很有用,但没有一个提供有效的解决方案。
我从尝试this answer的代码开始。令人惊讶的是,它做了它的事情,但是有一个很大的问题:我能想到的唯一调用这个代码的方法是SIGSEGV
处理程序,并且它有自己的堆栈 - 因此我无法得到我的实际堆栈崩溃的应用就像那样。
然后,我尝试合并this answer。它稍微好一点 - 它产生堆栈的第一项(发生崩溃的方法)。但那是 - 没有实际的回溯。因此,只要崩溃发生在第三方库(或标准库)中,此信息就毫无意义。
如何进一步改进代码并最终为我糟糕的崩溃应用程序获取堆栈跟踪?
P上。 S.在Android 4.0.3和Android 5.0上测试过,到目前为止行为是一样的。我想支持至少5.0和最近的版本,如4.3-4.4。
答案 0 :(得分:1)
您是否尝试过coffeecatch库?
它是一个JNI信号捕获器,允许将SIGSEGV(+)信号转换为具有混合jni / java回溯的java异常。它适用于API-19,但我还没有机会在API> 19上测试它。它提供了程序地址,可以传递给addr2line,以便获得对源的最终引用。
代码模板:
#include "coffeejni.h"
#include "coffeecatch.h"
void MyClass::foo(JNIEnv *env, int arg1, int arg2) {
....
int rc;
COFFEE_TRY_JNI(env, rc = crashInside(arg1, arg2));
....
}
追踪示例:
F/myapp (24535): "DESIGN ERROR": thread=t1
F/myapp (24535): java.lang.Error: signal 11 (Address not mapped to object) at address 0xdeadbaad [at libc.so:0x18282]
F/myapp (24535): at com.example.NativeSupport.nsc(Native Method)
F/myapp (24535): at com.example.NativeSupport.nsc_quiet(NativeSupport.java:328)
F/myapp (24535): at com.example.NativeSupport.loop(NativeSupport.java:287)
F/myapp (24535): at com.example.NativeSupport.access$2(NativeSupport.java:274)
F/myapp (24535): at com.example.NativeSupport$2.run(NativeSupport.java:124)
F/myapp (24535): at java.lang.Thread.run(Thread.java:856)
F/myapp (24535): Caused by: java.lang.Error: signal 11 (Address not mapped to object) at address 0xdeadbaad [at libc.so:0x18282]
F/myapp (24535): at system.lib.libc_so.0x18282(Native Method)
F/myapp (24535): at system.lib.libc_so.0xdc04(abort:0x4:0)
F/myapp (24535): at data.data.example.lib.libexample_so.0xf147(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x12d1b(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x1347b(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x13969(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x13ab3(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x17a9b(Native Method)
F/myapp (24535): at system.lib.libdvm_so.0x1f4b0(dvmPlatformInvoke:0x70:0)
F/myapp (24535): at system.lib.libdvm_so.0x4dfa5(dvmCallJNIMethod(unsigned int const*, JValue*, Method const*, Thread*):0x164:0)
F/myapp (24535): at system.lib.libdvm_so.0x28920(Native Method)
F/myapp (24535): at system.lib.libdvm_so.0x2d0b0(dvmInterpret(Thread*, Method const*, JValue*):0xb4:0)
F/myapp (24535): at system.lib.libdvm_so.0x5f599(dvmCallMethodV(Thread*, Method const*, Object*, bool, JValue*, std::__va_list):0x110:0)
F/myapp (24535): at system.lib.libdvm_so.0x5f5c3(dvmCallMethod(Thread*, Method const*, Object*, JValue*, ...):0x14:0)
F/myapp (24535): at system.lib.libdvm_so.0x549eb(Native Method)
F/myapp (24535): at system.lib.libc_so.0x12dd0(__thread_entry:0x30:0)
F/myapp (24535): at system.lib.libc_so.0x12534(pthread_create:0xac:0)
堆栈跟踪的Native(jni)部分是:
F/myapp (24535): at data.data.example.lib.libexample_so.0xf147(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x12d1b(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x1347b(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x13969(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x13ab3(Native Method)
F/myapp (24535): at data.data.example.lib.libexample_so.0x17a9b(Native Method)
最后得到一个人形可读的回溯:
cd android-ndk/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin
./arm-linux-androideabi-addr2line -e /home/joe/myproj/obj/local/armeabi-v7a/libexample.so 0xf147 0x12d1b 0x1347b 0x13969 0x13ab3 0x17a9b