preface

Recently, I learned the localization skills of Native Crash in the Course of Android Development Masters. I wrote an article to record the localization process of Native Crash according to the principle that good memory is better than bad writing. In the future, I will continue to record some knowledge and skills in the learning process of performance optimization. I hope I can communicate with you more and make progress together.

To begin, provide the Android project address used in this article: github.com/AndroidAdva…

An overview of the

The brekpad module was integrated in this article’s project to generate crash files for subsequent analysis, and the integrated Breakpad module is shown in figure 1The general steps of positioning are as follows

  1. Click crash button from the crash, the collapse of the information, if awarded the Sdcard permissions will be stored in a priority/Sdcard/crashDump, on the other hand will into the directory/data/data/com dodola. Breakpad/files/crashDump.
  2. adb pull /sdcard/crashDump .
  3. useminidump_stackwalk crashDump/***.dmp >crashLog.txtGenerate crash text message file
  4. Use the AARCH64-linux-Android-addr2line in the NDK package to capture the exact location of the crash

Software environment

The software environment for this article is as follows

  • MacOS 11.2
  • Android Studio 4.1.1
  • NDK-16b
  • Redmi K30Pro Android 11.0
  • breakpad

The above listed software should be in addition to the NDK, everyone should have ready-made, in fact, NDK leaders may also be installed, but this project needs this version of NDK tool to compile, so I downloaded this version of NDK, click the hyperlink above can download the corresponding macOS NDK compressed package. If it’s Windows or Linux you can go to the official website to find it. Windows is not recommended because the breakPad tool may need to be compiled by itself, which is not very friendly and can be implemented by installing a Linux VIRTUAL machine.

Compile and install the project

Download the project

git clone https://github.com/AndroidAdvanceWithGeektime/Chapter01
Copy the code

After downloading the project, open the project through Android Studio, and compile failed

This is because the NDK tool of the corresponding version is not specified. The solution is as follows: Add the corresponding ndK-16b path to the local.properties directory of the project root directory, for example, my path is as follows

SDK. Dir = / Users/ray/Library/Android SDK # set the NDK project path. The NDK dir = / Users/ray/Library/Android/SDK/Android - the NDK - r16bCopy the code

Once you’ve set it up, recompile the project, run the deployment on the phone, allow file permissions and then hit the Crash button and the application will crash. Next, we analyze the operation of locating Nativa Crash.

Compile and generate analysis tools

To get the minidump_StackWalk tool, download the breakPad source code and compile it. To obtain the source code for BreakPad, you need to install depot_tools. The document for the breadpad source is here. Here is a brief introduction to depot_tools,breakpad installation, and how to generate the minidump_stackWalk tool.

  1. Install depot_tools
Git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git # suggested Settings to ~ /. Zprofile inside to avoid repetition and set the export PATH=/path/to/depot_tools:$PATHCopy the code
  1. Install breakpad
mkdir breakpad && cd breakpad
fetch breakpad
Copy the code

It’s not enough for us to download breakpad directly, we also need some third-party files, if there is no third_party folder in the SRC directory

mkdir third_party && cd third_party
Copy the code

Then download the third party dependencies

git clone https://chromium.googlesource.com/linux-syscall-support 
Copy the code

Once the third-party dependencies have been downloaded, you can compile the object file in breakpad’s SRC directory

./configure && make
make install
Copy the code
  1. Get the target file

The path of the target file is SRC /processor/minidump_stackwalk

Generate crash text message file

Using the minidump_stackWalk program we compiled above, we can convert breakpad DMP files into TXT files for easy viewing. The details are as follows (my Breakpad installation path is in the same directory as the project)

./breakpad/src/src/processor/minidump_stackwalk Chapter01/crashDump/***.dmp > crashLog.txt
Copy the code

Check the crashLog. TXT

X0 = 0xB400007a972dd3C0 x1 = 0 Thread 0 (crashed) // the Thread used for crash 0 libcrash-lib.so + 0x5e0 0x0000007fef78ecd4 x2 = 0xb400007a00430000 x3 = 0x0000007fef78da28 x4 = 0x00000079fab18700 x5 = 0xa2685857527afd55 x6 = 0x0000007fef78d9b0 x7 = 0x0000007a06a721fc x8 = 0x0000000000000001 x9 = 0x0000000000000000 x10 = 0x0000000000430000 x11 = 0x0000007a80000000 x12 = 0x000000007086f460 x13 = 0xb7ac8963bffc2d7d x14 = 0x0000000000000006 x15 = 0xffffffffffffffff  x16 = 0x00000079f44a8fe8 x17 = 0x00000079f44985cc x18 = 0x0000007a97e06000 x19 = 0xb400007a972f1c00 x20 = 0x0000000000000000 x21 = 0xb400007a972f1c00 x22 = 0x0000007a977e8000 x23 = 0xb400007a972f1cb8 x24 = 0x0000007a0681e268 x25 = 0x0000007a977e8000 x26 = 0x0000000000000037 x27 = 0x0000000000000001 x28 = 0x0000007fef78ece0 fp = 0x0000007fef78ecb0 lr = 0x00000079f4498604 sp = 0x0000007fef78ec90 pc = 0x00000079f44985e0 Found by: given as instruction pointer in contextCopy the code

Note that ibcrash-lib.so, 0x5e0 are very important and will be used soon.

Locate the crashed code

Finally, at the most critical moment, we can capture the specific location of crash through the above information. The detailed operations are as follows

ray@RayYudeMacBook-Pro Chapter01 % $ANDROID_HOME/android - the NDK - r16b/toolchains/aarch64 - Linux - android 4.9 / prebuilt/Darwin - x86_64 / bin/aarch64 - Linux - android - add r2line -f -C -e sample/build/intermediates/transforms/mergeJniLibs/debug/0/lib/arm64-v8a/libcrash-lib.so 0x5e0 Crash() /Users/ray/AndroidPerformance/Chapter01/sample/src/main/cpp/crash.cpp:10Copy the code

In line 10 of crash. CPP, we find out what caused the crash. crash.cpp

conclusion

This article briefly introduces how to analyze and locate native crash through breakpad tool, as well as some common pit points, hoping to help you.

Refer to the link

Android development master class

Github.com/AndroidAdva…

Breakpad document

Depot_tools website