自己创建一个异常
Native Exception,简称NE,是发生于C/C++ code里面最常见的一种异常,
我们在写代码的时候一些常见的操作都会导致NE,比如空指针赋值,数组越界访问等,现在我在代码里面人为的添加一个简单的exception:
test.c:
1 #include <stdio.h>
2
3 void func4()
4 {
5 char *p = NULL;
6 *p = 0x5;//异常产生的地方
7 }
8
9 void func3()
10 {
11 int var4 = 4;
12 }
13
14
15 void func2()
16 {
17 int var3 = 3;
18 func3();
19 func4();
20 }
21
22 void func1()
23 {
24 int var1,var2;
25 var1 = 2;
26 }
27
28 void main()
29 {
30 int var0 = 1;
31 func1();
32 func2();
33 return;
34 }
Android.mk
1 LOCAL_PATH := $(call my-dir)
2 include $(CLEAR_VARS)
3
4 LOCAL_CFLAGS += -g3 -O0
5
6 LOCAL_SRC_FILES := test.c
7
8 LOCAL_MODULE := test
9
10 LOCAL_MULTILIB := 32
11
12 include $(BUILD_EXECUTABLE)
我把它放入pls/vendor/mediatek/proprietary/external/libtest/目录下面,我们对它进行编译并push到手机里面:
mmm vendor/mediatek/proprietary/external/libtest/
out/target/product/xxx/system/bin/test
adb push out/target/product/xxx/system/bin/test system/bin/
gdb-server调试程序
启动gdbserver:
$ adb shell ./system/bin/gdbserver :1234 system/bin/test
Process system/bin/test created; pid = 4130
Listening on port 1234
$ adb forward tcp:1234 tcp:1234
gdb 调试这个bin文件:
$ ./prebuilts/gcc/linux-x86/arm/cit-arm-linux-androideabi-4.8/bin/arm-linux-androideabi-gdb out/target/product/xxx/symbols/system/bin/test
Reading symbols from out/target/product/xxx/symbols/system/bin/test...done.
(gdb) set solib-search-path out/target/product/xxx/symbols/system/lib/
(gdb) set solib-absolute-prefix out/target/product/xxx/symbols/
(gdb) target remote:1234
Remote debugging using :1234
Reading symbols from out/target/product/xxx/symbols/system/bin/linker...done.
Loaded symbols for out/target/product/xxx/symbols/system/bin/linker
__dl__start () at bionic/linker/arch/arm/begin.S:32
32 mov r0, sp
(gdb) list
现在test程序加载 成功了:
(gdb) b main //设置断点
Breakpoint 1 at 0xaaaaa772: file vendor/mediatek/proprietary/external/libtest/test.c, line 30.
(gdb) n //单步执行
33 bl __linker_init
当运行到func函数里面就出现异常:
(gdb) n
Program received signal SIGSEGV, Segmentation fault.
0xaaaaa740 in func4 () at vendor/mediatek/proprietary/external/libtest/test.c:6
6 *p = 0x5;
可以很清楚的知道,我们在调用libtest这里面出现了问题,AEE是MTK平台自己的一套处理异常的工具,代码是封装好的,当应用app发生了异常,它回收集异常信息到压缩在DB文件里面,我们需要用GAT工具才能打开这个文件,通过在main_log里面,我们可以搜索到如下信息:
01-02 04:16:18.768 4180 4180 I AEE_AED : Build fingerprint: 'xxx:7.0/NRD90M/v6H5E-2:eng/test-keys'
01-02 04:16:18.768 4180 4180 I AEE_AED : Revision: '0'
01-02 04:16:18.768 4180 4180 I AEE_AED : ABI: 'arm'
01-02 04:16:18.768 4180 4180 I AEE_AED : pid: 4142, tid: 4142, name: test >>> system/bin/test <<<
01-02 04:16:18.769 4180 4180 I AEE_AED : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0
01-02 04: