ndk-stack
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
借助 ndk-stack
工具,您可以使用符号来表示来自 adb logcat
的堆栈轨迹或 /data/tombstones/
中的 Tombstone。该工具会将共享库内的任何地址替换为源代码中对应的 <source-file>:<line-number>
,从而简化调试流程。
例如,它可将下面的代码:
I/DEBUG ( 31): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
I/DEBUG ( 31): Build fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'
I/DEBUG ( 31): pid: 351, tid: 351 >>> /data/local/ndk-tests/crasher <<<
I/DEBUG ( 31): signal 11 (SIGSEGV), fault addr 0d9f00d8
I/DEBUG ( 31): r0 0000af88 r1 0000a008 r2 baadf00d r3 0d9f00d8
I/DEBUG ( 31): r4 00000004 r5 0000a008 r6 0000af88 r7 00013c44
I/DEBUG ( 31): r8 00000000 r9 00000000 10 00000000 fp 00000000
I/DEBUG ( 31): ip 0000959c sp be956cc8 lr 00008403 pc 0000841e cpsr 60000030
I/DEBUG ( 31): #00 pc 0000841e /data/local/ndk-tests/crasher
I/DEBUG ( 31): #01 pc 000083fe /data/local/ndk-tests/crasher
I/DEBUG ( 31): #02 pc 000083f6 /data/local/ndk-tests/crasher
I/DEBUG ( 31): #03 pc 000191ac /system/lib/libc.so
I/DEBUG ( 31): #04 pc 000083ea /data/local/ndk-tests/crasher
I/DEBUG ( 31): #05 pc 00008458 /data/local/ndk-tests/crasher
I/DEBUG ( 31): #06 pc 0000d362 /system/lib/libc.so
I/DEBUG ( 31):
转换为更具可读性的代码:
********** Crash dump: **********
Build fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'
pid: 351, tid: 351 >>> /data/local/ndk-tests/crasher <<<
signal 11 (SIGSEGV), fault addr 0d9f00d8
Stack frame #00 pc 0000841e /data/local/ndk-tests/crasher : Routine zoo in /tmp/foo/crasher/jni/zoo.c:13
Stack frame #01 pc 000083fe /data/local/ndk-tests/crasher : Routine bar in /tmp/foo/crasher/jni/bar.c:5
Stack frame #02 pc 000083f6 /data/local/ndk-tests/crasher : Routine my_comparison in /tmp/foo/crasher/jni/foo.c:9
Stack frame #03 pc 000191ac /system/lib/libc.so
Stack frame #04 pc 000083ea /data/local/ndk-tests/crasher : Routine foo in /tmp/foo/crasher/jni/foo.c:14
Stack frame #05 pc 00008458 /data/local/ndk-tests/crasher : Routine main in /tmp/foo/crasher/jni/main.c:19
Stack frame #06 pc 0000d362 /system/lib/libc.so
用法
若要使用 ndk-stack
,您首先需要一个包含未剥离版应用共享库的目录。如果您使用 ndk-build
,则可在 $PROJECT_PATH/obj/local/<abi>
中找到这些未剥离的共享库,其中 <abi>
是设备的 ABI。
对于 Android Gradle 插件 (AGP) build,未剥离的库将位于 <project-path>/build/intermediates/cxx/<build-type>/<hash>/obj/<abi>
中,其中 <project-path>
是包含您尝试符号化的模块的 AGP 项目的所在目录(默认为 app
),<build-type>
是 CMake 或 ndk-build build 类型的名称(例如 RelWithDebInfo
、Release
、Debug
等),<hash>
是任意值,<abi>
是设备的 ABI。
使用此工具的方式有两种。您可以将 logcat 文本作为直接输入馈送到程序。例如:
adb logcat | $NDK/ndk-stack -sym $PROJECT_PATH/obj/local/armeabi-v7a
您也可以使用 -dump
选项将 logcat 指定为输入文件。例如:
adb logcat > /tmp/foo.txt
$NDK/ndk-stack -sym $PROJECT_PATH/obj/local/armeabi-v7a -dump foo.txt
该工具会在开始解析 logcat 输出时查找第一行星号。例如:
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
注意:在复制/粘贴堆栈轨迹时,请别忘了这行代码,否则 ndk-stack
将无法正常运行。
更多信息
Google Play 利用 ndk-stack
在 Google Play 管理中心内使用符号来表示原生应用的堆栈轨迹。如需了解如何在生产环境中为您的应用启用此功能,请参阅如何在 Google Play 管理中心内为应用添加原生调试符号文件。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# ndk-stack\n\nThe `ndk-stack` tool allows you to symbolize stack traces from\n[`adb logcat`](/tools/help/logcat) or a\ntombstone in `/data/tombstones/`. It replaces any\naddress inside a shared library with the corresponding\n`\u003csource-file\u003e:\u003cline-number\u003e` from your source code,\nmaking debugging easier.\n\nFor example, it translates something like: \n\n```\nI/DEBUG ( 31): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\nI/DEBUG ( 31): Build fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'\nI/DEBUG ( 31): pid: 351, tid: 351 \u003e\u003e\u003e /data/local/ndk-tests/crasher \u003c\u003c\u003c\nI/DEBUG ( 31): signal 11 (SIGSEGV), fault addr 0d9f00d8\nI/DEBUG ( 31): r0 0000af88 r1 0000a008 r2 baadf00d r3 0d9f00d8\nI/DEBUG ( 31): r4 00000004 r5 0000a008 r6 0000af88 r7 00013c44\nI/DEBUG ( 31): r8 00000000 r9 00000000 10 00000000 fp 00000000\nI/DEBUG ( 31): ip 0000959c sp be956cc8 lr 00008403 pc 0000841e cpsr 60000030\nI/DEBUG ( 31): #00 pc 0000841e /data/local/ndk-tests/crasher\nI/DEBUG ( 31): #01 pc 000083fe /data/local/ndk-tests/crasher\nI/DEBUG ( 31): #02 pc 000083f6 /data/local/ndk-tests/crasher\nI/DEBUG ( 31): #03 pc 000191ac /system/lib/libc.so\nI/DEBUG ( 31): #04 pc 000083ea /data/local/ndk-tests/crasher\nI/DEBUG ( 31): #05 pc 00008458 /data/local/ndk-tests/crasher\nI/DEBUG ( 31): #06 pc 0000d362 /system/lib/libc.so\nI/DEBUG ( 31):\n```\n\ninto the more readable: \n\n```\n********** Crash dump: **********\nBuild fingerprint: 'generic/google_sdk/generic/:2.2/FRF91/43546:eng/test-keys'\npid: 351, tid: 351 \u003e\u003e\u003e /data/local/ndk-tests/crasher \u003c\u003c\u003c\nsignal 11 (SIGSEGV), fault addr 0d9f00d8\nStack frame #00 pc 0000841e /data/local/ndk-tests/crasher : Routine zoo in /tmp/foo/crasher/jni/zoo.c:13\nStack frame #01 pc 000083fe /data/local/ndk-tests/crasher : Routine bar in /tmp/foo/crasher/jni/bar.c:5\nStack frame #02 pc 000083f6 /data/local/ndk-tests/crasher : Routine my_comparison in /tmp/foo/crasher/jni/foo.c:9\nStack frame #03 pc 000191ac /system/lib/libc.so\nStack frame #04 pc 000083ea /data/local/ndk-tests/crasher : Routine foo in /tmp/foo/crasher/jni/foo.c:14\nStack frame #05 pc 00008458 /data/local/ndk-tests/crasher : Routine main in /tmp/foo/crasher/jni/main.c:19\nStack frame #06 pc 0000d362 /system/lib/libc.so\n```\n\nUsage\n-----\n\nTo use `ndk-stack`, you first need a directory containing unstripped versions of\nyour app's shared libraries. If you use `ndk-build`, these unstripped shared\nlibraries are found in `$PROJECT_PATH/obj/local/\u003cabi\u003e`, where `\u003cabi\u003e` is your\ndevice's ABI.\n\nFor an Android Gradle Plugin (AGP) build, the unstripped libraries will be in\n`\u003cproject-path\u003e/build/intermediates/cxx/\u003cbuild-type\u003e/\u003chash\u003e/obj/\u003cabi\u003e`, where\n`\u003cproject-path\u003e` is the directory of the AGP project that contains the module\nyou're trying to symbolize (by default this is `app`), `\u003cbuild-type\u003e` is the\nname of the CMake or ndk-build build type (such as `RelWithDebInfo`, `Release`,\n`Debug`, etc.), `\u003chash\u003e` is arbitrary, and `\u003cabi\u003e` is your device's ABI.\n\nThere are two ways to use the tool. You can feed the logcat text as direct input to the program.\nFor example: \n\n```\nadb logcat | $NDK/ndk-stack -sym $PROJECT_PATH/obj/local/armeabi-v7a\n```\n\nYou can also use the `-dump` option to specify the logcat as an input file. For example: \n\n```\nadb logcat \u003e /tmp/foo.txt\n$NDK/ndk-stack -sym $PROJECT_PATH/obj/local/armeabi-v7a -dump foo.txt\n```\n\nWhen it begins parsing the logcat output, the tool looks for an initial line of asterisks.\nFor example: \n\n```\n*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n```\n\n**Note:** When copy/pasting traces, don't forget this line, or\n`ndk-stack` won't work correctly.\n\nMore information\n----------------\n\nGoogle Play uses `ndk-stack` to symbolize stack traces for native\napps in the Google Play Console. For information on how to enable this for your\napp in a production environment, see how to\n[include a native debug symbols file](/studio/build/shrink-code#strip-native-libraries)\nfor your app in the Google Play Console."]]