使用BreakPad捕获native异常

简介

breakpad是由谷歌提供的跨平台的异常捕获分析工具,目前支持Mac、Linux、Windows平台,主要包括三大部分:

  • client,以library的形式内置在你的应用中,当崩溃发生时写 minidump文件
  • symbol dumper, 读取由编译器生成的调试信息(debugging information),并生成 symbol file
  • processor, 读取 minidump文件 和 symbol file ,生成可读的c/c++ Stack trace.

编译

下载breakpad源码

git colne https://codeload.github.com/google/breakpad/zip/master

编译

./configure
make
make install

编译完成后会生成minidump_stackwalk执行程序,是用来转换dmp文件的

Android中使用breakpad

  1. 把breakpad目录下src下源码到Androidstudio工程的cpp中
  2. 配置cmake
cmake_minimum_required(VERSION 3.4.1)

set(BREAKPAD_ROOT ${CMAKE_CURRENT_SOURCE_DIR})

include_directories(${BREAKPAD_ROOT}/src ${BREAKPAD_ROOT}/src/common/android/include)


file(GLOB BREAKPAD_SOURCES_COMMON
        ${BREAKPAD_ROOT}/src/client/linux/crash_generation/crash_generation_client.cc
        ${BREAKPAD_ROOT}/src/client/linux/dump_writer_common/thread_info.cc
        ${BREAKPAD_ROOT}/src/client/linux/dump_writer_common/ucontext_reader.cc
        ${BREAKPAD_ROOT}/src/client/linux/handler/exception_handler.cc
        ${BREAKPAD_ROOT}/src/client/linux/handler/minidump_descriptor.cc
        ${BREAKPAD_ROOT}/src/client/linux/log/log.cc
        ${BREAKPAD_ROOT}/src/client/linux/microdump_writer/microdump_writer.cc
        ${BREAKPAD_ROOT}/src/client/linux/minidump_writer/linux_dumper.cc
        ${BREAKPAD_ROOT}/src/client/linux/minidump_writer/linux_ptrace_dumper.cc
        ${BREAKPAD_ROOT}/src/client/linux/minidump_writer/minidump_writer.cc
        ${BREAKPAD_ROOT}/src/client/minidump_file_writer.cc
        ${BREAKPAD_ROOT}/src/common/convert_UTF.c
        ${BREAKPAD_ROOT}/src/common/md5.cc
        ${BREAKPAD_ROOT}/src/common/string_conversion.cc
        ${BREAKPAD_ROOT}/src/common/linux/elfutils.cc
        ${BREAKPAD_ROOT}/src/common/linux/file_id.cc
        ${BREAKPAD_ROOT}/src/common/linux/guid_creator.cc
        ${BREAKPAD_ROOT}/src/common/linux/linux_libc_support.cc
        ${BREAKPAD_ROOT}/src/common/linux/memory_mapped_file.cc
        ${BREAKPAD_ROOT}/src/common/linux/safe_readlink.cc

        )

file(GLOB BREAKPAD_ASM_SOURCE ${BREAKPAD_ROOT}/src/common/android/breakpad_getcontext.S
        )

set_source_files_properties(${BREAKPAD_ASM_SOURCE} PROPERTIES LANGUAGE C)

add_library(breakpad STATIC ${BREAKPAD_SOURCES_COMMON} ${BREAKPAD_ASM_SOURCE})

target_link_libraries(breakpad log)

  1. 需要写个初始化方法
JNIEXPORT void JNICALL
Java_com_sample_breakpad_BreakpadInit_initBreakpadNative(JNIEnv *env, jclass type, jstring path_) {
    const char *path = env->GetStringUTFChars(path_, 0);

    google_breakpad::MinidumpDescriptor descriptor(path);
    static google_breakpad::ExceptionHandler eh(descriptor, NULL, DumpCallback, NULL, true, -1);

    env->ReleaseStringUTFChars(path_, path);
}
//crash时回调函数
bool DumpCallback(const google_breakpad::MinidumpDescriptor &descriptor,
                  void *context,
                  bool succeeded) {
    ALOGD("Dump path: %s\n", descriptor.path());
    return succeeded;
}
  1. 写测试程序
//人为写个空指针
void Crash() {
    int *test=NULL;
    *test=1;
}

当native crash时会生成1f1e4c88-6a3c-4827-ee3626b2-4f74ccee.dmp文件,直接打开

&F/system/lib64/libstagefright_omx.soLEpB@/system/framework/ims-common.jarLEpBD/system/framework/qcom.fmradio.jarLEpB&��┙tw���}�_@/system/lib64/libpowermanager.soLEpBF/system/framework/UxPerformance.jarLEpBD/system/framework/QPerformance.jarLEpBX/apex/com.android.runtime/javalib/okhttp.jarLEpB�
�����W��Eҽ�F/system/lib64/libmedia_jni_utils.soLEpBH/system/framework/com.nxp.nfc.nq.jarLEpB�4�Un�������i�=�8/system/lib64/libmediandk.soLEpBF/system/framework/telephony-ext.jarLEpBN/system/framework/android.test.base.jarLEpBB/system/framework/voip-common.jarv/dev/__properties__/u:object_r:exported_fingerprint_prop:s0h/dev/__properties__/u:object_r:persist_debug_prop:s0LEpB��B�6��R�3"k��;`/apex/com.android.runtime/lib64/libartpalette.soLEpB

上面的内容无法分析,需要使用上述编译的minidump_stackwalk转换

minidump_stackwalk 目录/1f1e4c88-6a3c-4827-ee3626b2-4f74ccee.dmp >crashlog.txt

打开crashlog.txt

Crash reason:  SIGSEGV /SEGV_MAPERR
Crash address: 0x0
Process uptime: not available

Thread 0 (crashed)
 0  libcrash-lib.so + 0x650
     x0 = 0x0000007267acfb80    x1 = 0x0000007ffef9ef14
     x2 = 0x0000000000000000    x3 = 0x0000007267a69c00
     x4 = 0x0000007ffefa0080    x5 = 0x000000725b4a8965

libcrash-lib.so崩溃的动态库,0x650崩溃地址,需要使用addr2line转换,这里需要注意的是根据你的设备使用动态库的类型选择对应的addr2line,我这里使用的是arm64,所以选择/ndk目录/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin/aarch64-linux-android-addr2line

 ndk目录/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/bin/aarch64-linux-android-addr2line -f -C -e libcrash-lib.so 0x650

打印如下

//出错的函数
Crash()
//出错的源码行数
/Users/lqc/Desktop/demo/Chapter01-master/sample/src/main/cpp/crash.cpp:10

你可能感兴趣的:(使用BreakPad捕获native异常)