调试项目
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
调试原生代码崩溃
如果您想要了解原生代码崩溃转储或 Tombstone,不妨阅读调试 Android 平台原生代码一文。
如需了解更多的常见崩溃类型及其调查方法,请参阅诊断原生代码崩溃问题。
ndk-stack 工具可帮助您对崩溃问题进行符号化处理。您可以按照常规调试应用文档中的说明,在 Android Studio 中调试崩溃问题。如果您更喜欢使用命令行,可以使用 ndk-gdb 从 shell 附加 gdb
或 lldb
。
让应用能够直接访问 Tombstone 轨迹
从 Android 12(API 级别 31)开始,您可以通过 ApplicationExitInfo.getTraceInputStream()
方法以协议缓冲区的形式访问应用的原生代码崩溃 Tombstone。协议缓冲区使用此架构进行序列化。以前,只能通过 Android 调试桥 (adb) 来访问这些信息。
以下示例说明了如何在应用中实现此功能:
ActivityManager activityManager: ActivityManager = getSystemService(Context.ACTIVITY_SERVICE);
MutableList<ApplicationExitInfo> exitReasons = activityManager.getHistoricalProcessExitReasons(/* packageName = */ null, /* pid = */ 0, /* maxNum = */ 5);
for (ApplicationExitInfo aei: exitReasons) {
if (aei.getReason() == REASON_CRASH_NATIVE) {
// Get the tombstone input stream.
InputStream trace = aei.getTraceInputStream();
// The tombstone parser built with protoc uses the tombstone schema, then parses the trace.
Tombstone tombstone = Tombstone.parseFrom(trace);
}
}
调试原生内存问题
Address Sanitizer (HWASan/ASan)
HWAddress Sanitizer (HWASan) 和 Address Sanitizer (ASan) 与 Valgrind 类似,但在 Android 上明显速度更快且支持效果更好。
以下是在 Android 上调试内存错误的最佳方法。
Malloc 调试
有关 C 库内置的原生内存问题调试选项的完整说明,请参阅 Malloc 调试和使用 libc 回调跟踪原生内存。
Malloc 钩子
如果您想构建自己的工具,Android 的 libc 也支持拦截在程序执行期间发生的所有分配/释放调用。有关使用说明,请参阅 malloc_hooks 文档。
Malloc 统计信息
Android 支持 <malloc.h>
的 mallinfo(3) 和 malloc_info(3) 扩展。
Android 6.0 (Marshmallow) 及更高版本提供 malloc_info
功能,Bionic 的 malloc.h 头文件中记录了其 XML 架构。
性能分析
如需对原生代码进行 CPU 性能分析,请使用 Simpleperf。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Debug your project\n\nDebug native crashes\n--------------------\n\nIf you're struggling to understand a native crash dump or tombstone,\n[Debugging Native Android Platform Code](https://source.android.com/devices/tech/debug/index.html)\nis a good introduction.\n\nFor a fuller catalog of common types of crash and how to investigate them, see\n[Diagnosing Native Crashes](https://source.android.com/devices/tech/debug/native-crash).\n\nThe [ndk-stack](/ndk/guides/ndk-stack) tool can help symbolize your crashes.\nYou can debug crashes in Android Studio as described in the general\n[Debug your app](/studio/debug) documentation. If you prefer to use the\ncommand-line, [ndk-gdb](/ndk/guides/ndk-gdb) lets you attach either `gdb` or\n`lldb` from your shell.\n\n### Provide apps direct access to tombstone traces\n\nStarting in Android 12 (API level 31), you can access your app's native crash\ntombstone as a\n[protocol buffer](https://developers.google.com/protocol-buffers/) through the\n[`ApplicationExitInfo.getTraceInputStream()`](/reference/android/app/ApplicationExitInfo#getTraceInputStream())\nmethod. The protocol buffer is serialized using [this schema](https://android.googlesource.com/platform/system/core/+/refs/heads/main/debuggerd/proto/tombstone.proto).\nPreviously, the only way to get access to this information was through the\n[Android Debug Bridge](/studio/command-line/adb) (adb).\n\nHere's an example of how to implement this in your app: \n\n ActivityManager activityManager: ActivityManager = getSystemService(Context.ACTIVITY_SERVICE);\n MutableList\u003cApplicationExitInfo\u003e exitReasons = activityManager.getHistoricalProcessExitReasons(/* packageName = */ null, /* pid = */ 0, /* maxNum = */ 5);\n for (ApplicationExitInfo aei: exitReasons) {\n if (aei.getReason() == REASON_CRASH_NATIVE) {\n // Get the tombstone input stream.\n InputStream trace = aei.getTraceInputStream();\n // The tombstone parser built with protoc uses the tombstone schema, then parses the trace.\n Tombstone tombstone = Tombstone.parseFrom(trace);\n }\n }\n\nDebug native memory issues\n--------------------------\n\n### Address Sanitizer (HWASan/ASan)\n\n[HWAddress Sanitizer](/ndk/guides/hwasan) (HWASan) and\n[Address Sanitizer](/ndk/guides/asan) (ASan) are similar to Valgrind, but\nsignificantly faster and much better supported on Android.\n\nThese are your best option for debugging memory errors on Android.\n\n### Malloc debug\n\nSee\n[Malloc Debug](https://android.googlesource.com/platform/bionic/+/main/libc/malloc_debug/README.md)\nand\n[Native Memory Tracking using libc Callbacks](https://android.googlesource.com/platform/bionic/+/main/libc/malloc_debug/README_api.md)\nfor a thorough description of the C library's built-in options for debugging\nnative memory issues.\n\n### Malloc hooks\n\nIf you want to build your own tools, Android's libc also supports intercepting\nall allocation/free calls that happen during program execution. See the\n[malloc_hooks documentation](https://android.googlesource.com/platform/bionic/+/main/libc/malloc_hooks/README.md)\nfor usage instructions.\n\n### Malloc statistics\n\nAndroid supports the\n[mallinfo(3)](http://man7.org/linux/man-pages/man3/mallinfo.3.html)\nand\n[malloc_info(3)](http://man7.org/linux/man-pages/man3/malloc_info.3.html)\nextensions to `\u003cmalloc.h\u003e`.\n\nThe `malloc_info` functionality is available in Android 6.0 (Marshmallow) and\nhigher and its XML schema is documented in Bionic's\n[malloc.h](https://android.googlesource.com/platform/bionic/+/main/libc/include/malloc.h)\nheader.\n\nProfiling\n---------\n\nFor CPU profiling of native code, you can use [Simpleperf](/ndk/guides/simpleperf)."]]