Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

非主线程native crash无法获取java堆栈 #8

Closed
lizhangqu opened this issue Jun 13, 2019 · 8 comments
Closed

非主线程native crash无法获取java堆栈 #8

lizhangqu opened this issue Jun 13, 2019 · 8 comments
Labels
bug Something isn't working

Comments

@lizhangqu
Copy link

No description provided.

@caikelun
Copy link
Collaborator

在 native 层通过 pthread_create() 创建的线程,没有 attach 到 jvm,就没有对应的 java stacktrace。

@lizhangqu
Copy link
Author

java层创建的线程中执行native代码发生crash也无法获取到java堆栈

@caikelun
Copy link
Collaborator

目前输出的tombstone格式中,java stacktrace 是在dump文件的尾部,是否需要再确认下?
另外你可以直接用附带的 sample app 试一下。

@lizhangqu
Copy link
Author

@caikelun

子线程制造native crash

   new Thread(new Runnable() {
            @Override
            public void run() {
                XCrash.testNativeCrash(false);
            }
        }).start();

解析crash日志输出到logcat


    public static void log(String tag, String message) {
        int logLevel = Log.ERROR;
        // Split by line, then ensure each line can fit into Log's maximum length.
        for (int i = 0, length = message.length(); i < length; i++) {
            int newline = message.indexOf('\n', i);
            newline = newline != -1 ? newline : length;
            do {
                int end = Math.min(newline, i + 4000);
                Log.println(logLevel, tag, message.substring(i, end));
                i = end;
            } while (i < newline);
        }
    }

    private void debug(String logPath, String emergency) {
        Log.d(TAG, "logPath: " + (logPath != null ? logPath : "(null)") + ", emergency: " + (emergency != null ? emergency : "(null)"));

        // Parse and save the crash info to a JSON file for debugging.
        FileWriter writer = null;
        try {
            File debug = new File(getApplicationContext().getFilesDir() + "/tombstones/debug.json");
            debug.createNewFile();
            writer = new FileWriter(debug, false);

            Map<String, String> parse = TombstoneParser.parse(logPath, emergency);
            String crashType = parse.get(TombstoneParser.keyCrashType);
            String startTime = parse.get(TombstoneParser.keyStartTime);
            String crashTime = parse.get(TombstoneParser.keyCrashTime);

            String pid = parse.get(TombstoneParser.keyProcessId);
            String tid = parse.get(TombstoneParser.keyThreadId);
            String pname = parse.get(TombstoneParser.keyProcessName);
            String tname = parse.get(TombstoneParser.keyThreadName);


            String signal = parse.get(TombstoneParser.keySignal);
            String code = parse.get(TombstoneParser.keyCode);
            String faultAddr = parse.get(TombstoneParser.keyFaultAddr);
            String nativeBacktrace = parse.get(TombstoneParser.keyBacktrace);
            String javaStacktrace = parse.get(TombstoneParser.keyJavaStacktrace);
            String otherThreads = parse.get(TombstoneParser.keyOtherThreads);
            String logcat = parse.get(TombstoneParser.keyLogcat);

            log(TAG, "Crash type: " + crashType);
            log(TAG, "Start time: " + startTime);
            log(TAG, "Crash time: " + crashTime);

            log(TAG, String.format("pid: %s, tid: %s, name: %s >>> %s <<<", pid, tid, tname, pname));
            log(TAG, String.format("signal: %s, code: %s, fault addr: %s", signal, code, faultAddr));
            log(TAG, "native backtrace:\n" + nativeBacktrace);
            log(TAG, "java stacktrace:\n" + javaStacktrace);
            log(TAG, "other threads:\n" + otherThreads);
            log(TAG, "logcat:\n" + logcat);
            writer.write(new JSONObject(parse).toString());
        } catch (Exception e) {
            Log.d(TAG, "debug failed", e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception ignored) {
                }
            }
        }
    }

logcat

06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: Crash type: native
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: Start time: 2019-06-13T16:54:04.623+0800
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: Crash time: 2019-06-13T16:54:08.020+0800
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: pid: 7644, tid: 7737, name: Thread-1892  >>> xcrash.sample <<<
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: signal: 11 (SIGSEGV), code: 1 (SEGV_MAPERR), fault addr: 0x0
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: native backtrace:
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #00 pc 00006efa  /data/app-lib/xcrash.sample-2/libxcrash.so (xc_test_call_4+13)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #01 pc 00006f23  /data/app-lib/xcrash.sample-2/libxcrash.so (xc_test_call_3+16)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #02 pc 00006f3f  /data/app-lib/xcrash.sample-2/libxcrash.so (xc_test_call_2+16)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #03 pc 00006f53  /data/app-lib/xcrash.sample-2/libxcrash.so (xc_test_call_1+8)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #04 pc 00006feb  /data/app-lib/xcrash.sample-2/libxcrash.so (xc_test_crash+86)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #05 pc 000203cc  /system/lib/libdvm.so (dvmPlatformInvoke+112)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #06 pc 00051097  /system/lib/libdvm.so (_Z16dvmCallJNIMethodPKjP6JValuePK6MethodP6Thread+398)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #07 pc 00029860  /system/lib/libdvm.so
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #08 pc 00030d34  /system/lib/libdvm.so (_Z11dvmMterpStdP6Thread+76)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #09 pc 0002e3cc  /system/lib/libdvm.so (_Z12dvmInterpretP6ThreadPK6MethodP6JValue+184)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #10 pc 000634fd  /system/lib/libdvm.so (_Z14dvmCallMethodVP6ThreadPK6MethodP6ObjectbP6JValueSt9__va_list+336)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #11 pc 00063521  /system/lib/libdvm.so (_Z13dvmCallMethodP6ThreadPK6MethodP6ObjectP6JValuez+20)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #12 pc 000581ff  /system/lib/libdvm.so
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #13 pc 0000d230  /system/lib/libc.so (__thread_entry+72)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: #14 pc 0000d3c8  /system/lib/libc.so (pthread_create+240)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: java stacktrace:
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: null
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: other threads:
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: pid: 7644, tid: 7649, name: Signal Catcher  >>> xcrash.sample <<<
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     r0  fffffffc  r1  00000000  r2  00000000  r3  00000008
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     r4  717bad08  r5  717bad48  r6  bea6b4f0  r7  000000b1
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     r8  7469d988  r9  416acc34  r10 416b32c4  r11 4168f5cc
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     ip  717bad44  sp  717bad08  lr  4006c4b1  pc  4007a2cc
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: backtrace:
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     #00 pc 000212cc  /system/lib/libc.so (__rt_sigtimedwait+12)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     #01 pc 000134ad  /system/lib/libc.so (sigwait+24)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     #02 pc 00054ec7  /system/lib/libdvm.so
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     #03 pc 000578e1  /system/lib/libdvm.so
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     #04 pc 0000d230  /system/lib/libc.so (__thread_entry+72)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     #05 pc 0000d3c8  /system/lib/libc.so (pthread_create+240)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample: stack:
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacc8  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717baccc  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacd0  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacd4  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacd8  717bad68  [stack:7649]
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacdc  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bace0  7469d988  [anon:libc_malloc]
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bace4  717c0fc4
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bace8  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacec  717bad68  [stack:7649]
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacf0  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacf4  41652507  /system/lib/libdvm.so (_Z14dvmCallMethodVP6ThreadPK6MethodP6ObjectbP6JValueSt9__va_list+346)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacf8  41690b29  /system/lib/libdvm.so
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bacfc  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad00  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad04  717baca8  [stack:7649]
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     #00  717bad08  00000204
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          ........  ........
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     #01  717bad08  00000204
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad0c  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad10  fffffe58
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad14  718e7420  [anon:libc_malloc]
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad18  416b2e18  /system/lib/libdvm.so
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad1c  41643ecb  /system/lib/libdvm.so
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:     #02  717bad20  42ace350  /dev/ashmem/dalvik-heap (deleted)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad24  42ace3a8  /dev/ashmem/dalvik-heap (deleted)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad28  00000001
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad2c  41ce4a30  /dev/ashmem/dalvik-zygote (deleted)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad30  7469dcf4  [anon:libc_malloc]
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad34  41643917  /system/lib/libdvm.so (_Z27dvmRemoveFromReferenceTableP14ReferenceTablePP6ObjectS2_+8)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad38  7469d988  [anon:libc_malloc]
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad3c  416478c7  /system/lib/libdvm.so (dvmReleaseTrackedAlloc+30)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad40  42ace3c0  /dev/ashmem/dalvik-heap (deleted)
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad44  00000204
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad48  00000000
06-13 16:54:08.775 7644-7771/xcrash.sample E/xcrash_sample:          717bad4c  7469d988  [anon:libc_malloc]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bad50  416b2e18  /system/lib/libdvm.so
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bad54  41645d85  /system/lib/libdvm.so (_Z22dvmAttachCurrentThreadPK16JavaVMAttachArgsb+388)
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bad58  41ce4a30  /dev/ashmem/dalvik-zygote (deleted)
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bad5c  42ace3c0  /dev/ashmem/dalvik-heap (deleted)
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          ........  ........
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:     #03  717bad98  718e7420  [anon:libc_malloc]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bad9c  00010002
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bada0  41ca6fc8  [anon:libc_malloc]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bada4  41ce4a30  /dev/ashmem/dalvik-zygote (deleted)
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bada8  717badd0  [stack:7649]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badac  41ca6738  [anon:libc_malloc]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badb0  41646895  /system/lib/libdvm.so
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badb4  40066234  /system/lib/libc.so (__thread_entry+76)
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:     #04  717badb8  718e7420  [anon:libc_malloc]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badbc  41ca6738  [anon:libc_malloc]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badc0  717badd0  [stack:7649]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badc4  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badc8  00000078
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badcc  400663cc  /system/lib/libc.so (pthread_create+244)
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:     #05  717badd0  717badd0  [stack:7649]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badd4  41ca6738  [anon:libc_malloc]
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badd8  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717baddc  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bade0  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bade4  30f12619
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bade8  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badec  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badf0  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badf4  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badf8  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717badfc  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bae00  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bae04  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bae08  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample:          717bae0c  00000000
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: total threads (exclude the crashed thread): 26
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: threads matched whitelist: 1
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: threads ignored by max count limit: 0
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: dumped threads: 1
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: logcat:
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: --------- tail end of log main (/system/bin/logcat -b main -d -v threadtime -t 240 *:D)
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: 06-13 16:54:07.915  7644  7644 W AwContents: nativeOnDraw failed; clearing to background color.
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: 06-13 16:54:07.995  7644  7738 D xcrash  : crashed APP's thread is running ...... 1
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: 06-13 16:54:08.005  7644  7644 W AwContents: nativeOnDraw failed; clearing to background color.
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: --------- tail end of log system (/system/bin/logcat -b system -d -v threadtime -t 60 *:W)
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: --------- tail end of log events (/system/bin/logcat -b events -d -v threadtime -t 60 *:I)
06-13 16:54:08.785 7644-7771/xcrash.sample E/xcrash_sample: 06-13 16:54:04.795  7644  7644 I am_on_resume_called: [0,xcrash.sample.MainActivity]

@caikelun
Copy link
Collaborator

感谢反馈!这里确实有问题,我马上修复。

@caikelun caikelun added the bug Something isn't working label Jun 14, 2019
@lizhangqu
Copy link
Author

@caikelun

应该是这里的判断有问题

 if((is_main_thread && 0 == strcmp(c_name, "main")) ||
           (!is_main_thread && strstr(c_name, tname)))

strstr入参调换下顺序

 if((is_main_thread && 0 == strcmp(c_name, "main")) ||
           (!is_main_thread && strstr(tname, c_name)))

@caikelun
Copy link
Collaborator

@lizhangqu 已经提交了 patch 1bd127a 修复了这个 bug。发了新版本 2.1.6

@lizhangqu
Copy link
Author

牛批

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants