android ndk 崩溃捕获(cocos2d-x 之android崩溃捕获)

ndk 崩溃捕获 ,我们可以采用

Google Breakpad这个开源库

breakpad是一个跨平台的c++崩溃处理系统。
包括:dmp生成模块、 上传模块、 服务器存储模块、解析dmp模块 等。

这里我只大概说一下dmp生成模块。

1、编译静态库
解压并拷贝breakpad源码目录到项目中,编译:

脚本如下:

LOCAL_PATH := $(call my-dir)

# Defube the client library module, as a simple static library that
# exports the right include path / linker flags to its users.

include $(CLEAR_VARS)

LOCAL_MODULE := breakpad_client_static
LOCAL_MODULE_FILENAME := breakpad_client

LOCAL_CPP_EXTENSION := .cc

# Breakpad uses inline ARM assembly that requires the library
# to be built in ARM mode. Otherwise, the build will fail with
# cryptic assembler messages like:
#   Compile++ thumb  : google_breakpad_client <= crash_generation_client.cc
#   /tmp/cc8aMSoD.s: Assembler messages:
#   /tmp/cc8aMSoD.s:132: Error: invalid immediate: 288 is out of range
#   /tmp/cc8aMSoD.s:244: Error: invalid immediate: 296 is out of range
LOCAL_ARM_MODE := arm

# List of client source files, directly taken from Makefile.am
LOCAL_SRC_FILES := \
    src/client/linux/crash_generation/crash_generation_client.cc \
    src/client/linux/handler/exception_handler.cc \
    src/client/linux/handler/minidump_descriptor.cc \
    src/client/linux/log/log.cc \
    src/client/linux/minidump_writer/linux_dumper.cc \
    src/client/linux/minidump_writer/linux_ptrace_dumper.cc \
    src/client/linux/minidump_writer/minidump_writer.cc \
    src/client/minidump_file_writer.cc \
    src/common/android/breakpad_getcontext.S \
    src/common/convert_UTF.c \
    src/common/md5.cc src/common/string_conversion.cc \
    src/common/linux/elfutils.cc \
    src/common/linux/file_id.cc \
    src/common/linux/guid_creator.cc \
    src/common/linux/linux_libc_support.cc \
    src/common/linux/memory_mapped_file.cc \
    src/common/linux/safe_readlink.cc

LOCAL_C_INCLUDES        := $(LOCAL_PATH)/src/common/android/include \
                           $(LOCAL_PATH)/src

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)
LOCAL_EXPORT_LDLIBS     := -llog

include $(BUILD_STATIC_LIBRARY)


2、这编译生成的breakpad_client.a文件就可以直接导入我们工程直接使用了

3、在我们工程项目中添加这个库的初始化代码

包含头文件

#include "client/linux/handler/exception_handler.h"
#include "client/linux/handler/minidump_descriptor.h"
#include "client/linux/log/log.h"

void InitCrash()
{
#ifdef ANDROID

    std::string s_strOutPath_Debug = FORMAT("%s/%s") << GetDocumentPath() << "Debug";
    DebugMsg("InitCrashReport: %s\n ", s_strOutPath_Debug.c_str());
   //设置生成dmp的路径
    static google_breakpad::MinidumpDescriptor descriptor(s_strOutPath_Debug);
    static google_breakpad::ExceptionHandler eh(descriptor, NULL, DumpCallback,
        NULL, true, -1);

#endif
}


//dmp回调

bool DumpCallback(const google_breakpad::MinidumpDescriptor& descriptor,
    void* context,
    bool succeeded)
{
        DebugMsg("Dump path: %s\n succeeded:%d", descriptor.path(), succeeded==true?1:0);

        if(!succeeded)
        {
            std::remove(descriptor.path());
        }
        return succeeded;
}

4、生成了dmp我们就要开始怎么去解析呢?

在解析这个4f46eb83-d5e8-91dc-3ba0eec4-20a1eabe.dmp 这样一个dump文件之前,我们要准备dump_syms、minidump_stackwalk这两个文件

这个两个文件怎么生成的,可以下载google_breakpad这个开源代码,到linux环境下进行编译既可以出来,后面我们在解析4f46eb83-d5e8-91dc-3ba0eec4-20a1eabe.dmp这个文件,我们还要保存我们的符号文件例如:android ndk 崩溃捕获(cocos2d-x 之android崩溃捕获)_第1张图片 这个里面libzerg.so就是我们要保存的符号文件,后面就是把这些文件准备到一个文件夹下如图:android ndk 崩溃捕获(cocos2d-x 之android崩溃捕获)_第2张图片


然后就是按下面的步骤:

adb push E:/android/android-sdk-windows/android-sdk-windows/platform-tools/10000 /data/local/tmp/10000
adb shell
cd /data/local/tmp/10000

chmod 777 libzerg.so
chmod 777 dump_syms
chmod 777 minidump_stackwalk

./dump_syms ./libzerg.so>libzerg.so.sym
busybox head -n1 libzerg.so.sym

注意:E21A4C5B4525F184923BC20F0D8557170这个是通过busybox head -n1 libzerg.so.sym这个命令生成

mkdir symbols
cd symbols
mkdir libzerg.so
cd libzerg.so
mkdir E21A4C5B4525F184923BC20F0D8557170
cd /data/local/tmp/10000
cat libzerg.so.sym> ./symbols/libzerg.so/E21A4C5B4525F184923BC20F0D8557170/libzerg.so.sym

cd /data/local/tmp/10000
mkdir res
./minidump_stackwalk 4f46eb83-d5e8-91dc-3ba0eec4-20a1eabe.dmp ./symbols

执行到这里就会生成:

android ndk 崩溃捕获(cocos2d-x 之android崩溃捕获)_第3张图片


这样整个崩溃解析就算完成了



你可能感兴趣的:(android ndk 崩溃捕获(cocos2d-x 之android崩溃捕获))