native 的崩溃相比于 java 很难看懂,对于排查问题很不利。不过我们可以通过 NDK 中的工具 ndk-stack 来还原堆栈信息。
先制造一个异常:
extern "C"
JNIEXPORT void JNICALL
Java_com_teletian_sample_myndk_MainActivity_testCrash(JNIEnv *env, jobject thiz) {
int* i = NULL;
i[0] = 0;
}
运行之后出现崩溃,信息如下:
--------- beginning of crash
06-18 11:45:55.554 13648 13648 F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 13648 (an.sample.myndk), pid 13648 (an.sample.myndk)
06-18 11:45:55.577 13683 13683 I crash_dump32: obtaining output fd from tombstoned, type: kDebuggerdTombstone
06-18 11:45:55.577 1820 1820 I /system/bin/tombstoned: received crash request for pid 13648
06-18 11:45:55.579 13683 13683 I crash_dump32: performing dump of process 13648 (target tid = 13648)
06-18 11:45:55.584 13683 13683 F DEBUG : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
06-18 11:45:55.584 13683 13683 F DEBUG : Build fingerprint: 'google/sdk_gphone_x86/generic_x86:10/QSR1.210802.001/7603624:userdebug/dev-keys'
06-18 11:45:55.584 13683 13683 F DEBUG : Revision: '0'
06-18 11:45:55.584 13683 13683 F DEBUG : ABI: 'x86'
06-18 11:45:55.584 13683 13683 F DEBUG : Timestamp: 2022-06-18 11:45:55+0800
06-18 11:45:55.584 13683 13683 F DEBUG : pid: 13648, tid: 13648, name: an.sample.myndk >>> com.teletian.sample.myndk <<<
06-18 11:45:55.584 13683 13683 F DEBUG : uid: 10146
06-18 11:45:55.584 13683 13683 F DEBUG : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
06-18 11:45:55.584 13683 13683 F DEBUG : Cause: null pointer dereference
06-18 11:45:55.584 13683 13683 F DEBUG : eax 00000000 ebx 0000000c ecx ecb8dfc0 edx ff9f45e0
06-18 11:45:55.584 13683 13683 F DEBUG : edi ff9f47cc esi ece565bb
06-18 11:45:55.584 13683 13683 F DEBUG : ebp ff9f45d8 esp ff9f45d0 eip c1a1d47b
06-18 11:45:55.742 13683 13683 F DEBUG :
06-18 11:45:55.742 13683 13683 F DEBUG : backtrace:
06-18 11:45:55.742 13683 13683 F DEBUG : #00 pc 0000047b /data/app/com.teletian.sample.myndk-XGWmLhI8hu8vJdkaA7VHEQ==/lib/x86/libmyndk.so (Java_com_teletian_sample_myndk_MainActivity_testCrash+27) (BuildId: 24293db346fab188b0e1c453f33aa6777f9f0be6)
06-18 11:45:55.742 13683 13683 F DEBUG : #01 pc 00144f67 /apex/com.android.runtime/lib/libart.so (art_quick_generic_jni_trampoline+71) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.742 13683 13683 F DEBUG : #02 pc 0013e7d2 /apex/com.android.runtime/lib/libart.so (art_quick_invoke_stub+338) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.742 13683 13683 F DEBUG : #03 pc 00149a69 /apex/com.android.runtime/lib/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+281) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.742 13683 13683 F DEBUG : #04 pc 00332542 /apex/com.android.runtime/lib/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+386) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.742 13683 13683 F DEBUG : #05 pc 0032c1dc /apex/com.android.runtime/lib/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+988) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.742 13683 13683 F DEBUG : #06 pc 0068191d /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+989) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.742 13683 13683 F DEBUG : #07 pc 00138821 /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.742 13683 13683 F DEBUG : #08 pc 0000035c [anon:dalvik-classes3.dex extracted in memory from /data/app/com.teletian.sample.myndk-XGWmLhI8hu8vJdkaA7VHEQ==/base.apk!classes3.dex] (com.teletian.sample.myndk.MainActivity.onCreate+40)
06-18 11:45:55.743 13683 13683 F DEBUG : #09 pc 00681b8c /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.743 13683 13683 F DEBUG : #10 pc 00138821 /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.743 13683 13683 F DEBUG : #11 pc 0019176e /system/framework/framework.jar (android.app.Activity.performCreate+30)
06-18 11:45:55.743 13683 13683 F DEBUG : #12 pc 00681b8c /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.743 13683 13683 F DEBUG : #13 pc 00138821 /apex/com.android.runtime/lib/libart.so (mterp_op_invoke_virtual+33) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.743 13683 13683 F DEBUG : #14 pc 00191736 /system/framework/framework.jar (android.app.Activity.performCreate+2)
06-18 11:45:55.743 13683 13683 F DEBUG : #15 pc 00681b8c /apex/com.android.runtime/lib/libart.so (MterpInvokeVirtual+1612) (BuildId: 26b0fc8885e87fc8f11e1081b83a9b8e)
06-18 11:45:55.743 13683 13683 F DEBUG : #16 pc 00138821 /apex/com.android.run