Android中使用addr2line来分析出错信息
系统:Ubuntu12.04
手机系统:Android5.0
在Android的开发过程中有会有很多的bug,利用工具可以很好的帮忙我们来分析问题,特别是一些系统报错的信息中会打印出堆栈,我们可以根据这个堆栈报错信息定位是哪个文件哪行代码出的错。下面就把我使用addr2line的过程记录下来
首先是在电脑上编译出一个eng版本,烧录到手机,在测试或调试的过程中出错了,查看出错信息如下:
01-23 11:45:38.782 D/AEE/AED (10995): coredump_socket_create: 10905 01-23 11:45:38.783 I/AEE/AED (10995): $** *** *** *** *** *** *** *** Exception *** *** *** *** *** *** *** **$ 01-23 11:45:38.783 I/AEE/AED (10995): Build Info: 'L0:ALPS.L0.MP6.V3_GIONEE6752.LWT.L:MT6752:S01,GiONEE/GN9006/GN9006:5.0/LRX21M/1421905945:eng/release-keys' 01-23 11:45:38.784 I/AEE/AED (10995): Flavor Info: 'None' 01-23 11:45:38.784 I/AEE/AED (10995): Exception Log Time:[Fri Jan 23 11:45:38 CST 2015] [65998.900000] 01-23 11:45:38.784 I/AEE/AED (10995): 01-23 11:45:38.785 I/AEE/AED (10995): Exception Class: Native (NE) 01-23 11:45:38.785 I/AEE/AED (10995): Exception Type: SIGSEGV 01-23 11:45:38.785 I/AEE/AED (10995): 01-23 11:45:38.785 I/AEE/AED (10995): Current Executing Process: 01-23 11:45:38.785 I/AEE/AED (10995): pid: 10905, tid: 10944 01-23 11:45:38.785 I/AEE/AED (10995): com.gionee.gallery 01-23 11:45:38.785 I/AEE/AED (10995): 01-23 11:45:38.786 I/AEE/AED (10995): Backtrace: 01-23 11:45:38.786 I/AEE/AED (10995): #00 pc 0000b23c /system/lib/libz.so (inflate+712) 01-23 11:45:38.786 I/AEE/AED (10995): #01 pc 000187e3 /system/lib/libpng.so (png_read_IDAT_data+210) 01-23 11:45:38.786 I/AEE/AED (10995): #02 pc 0000d965 /system/lib/libpng.so (png_read_row+288) 01-23 11:45:38.786 I/AEE/AED (10995): #03 pc 0000db7d /system/lib/libpng.so (png_read_rows+56) 01-23 11:45:38.786 I/AEE/AED (10995): #04 pc 001d9aec /system/lib/libskia.so (SkPNGImageDecoder::onDecodeSubset(SkBitmap*, SkIRect const&)+2412) 01-23 11:45:38.786 I/AEE/AED (10995): #05 pc 001cf1dc /system/lib/libskia.so (SkImageDecoder::decodeSubset(SkBitmap*, SkIRect const&, SkColorType, int, void*)+108) 01-23 11:45:38.786 I/AEE/AED (10995): #06 pc 00095107 /system/lib/libandroid_runtime.so 01-23 11:45:38.786 I/AEE/AED (10995): #07 pc 000178f9 /data/dalvik-cache/arm/system@[email protected] 01-23 11:45:38.786 I/AEE/AED (10995): 01-23 11:45:38.786 I/AEE/AED (10995): $** *** *** *** *** *** *** *** Exception *** *** *** *** *** *** *** **$ 01-23 11:45:38.786 D/AEE/AED (10995): aed_report_dumpstate: filepath /sdcard/mtklog/aee_exp/temp/db.LjXvBw, pid 10905, tid 10944, exp_class 1, db_opt 0如果想查找这行
#01 pc 000187e3 /system/lib/libpng.so (png_read_IDAT_data+210)
出错代码信息可以在电脑上(在你编译系统的android目录)查找:libpng.so
查找结果如下
dzt@dzt-All-Series:~/D/dzt/workarea/7501_mp_l/android_mtk_l6752_lwt_mp$ find -name libpng.so ./out/target/product/gionee6752_lwt_l/obj_arm/lib/libpng.so ./out/target/product/gionee6752_lwt_l/obj_arm/SHARED_LIBRARIES/libpng_intermediates/LINKED/libpng.so ./out/target/product/gionee6752_lwt_l/obj/lib/libpng.so ./out/target/product/gionee6752_lwt_l/obj/SHARED_LIBRARIES/libpng_intermediates/LINKED/libpng.so ./out/target/product/gionee6752_lwt_l/symbols/system/lib/libpng.so ./out/target/product/gionee6752_lwt_l/symbols/system/lib64/libpng.so ./out/target/product/gionee6752_lwt_l/system/lib/libpng.so ./out/target/product/gionee6752_lwt_l/system/lib64/libpng.so ./out/target/product/gionee_backup/symbols/system/lib/libpng.so ./out/target/product/gionee_backup/symbols/system/lib64/libpng.so dzt@dzt-All-Series:~/D/dzt/workarea/7501_mp_l/android_mtk_l6752_lwt_mp$有用的信息是这两行,带有symbols字样的
./out/target/product/gionee6752_lwt_l/symbols/system/lib/libpng.so
./out/target/product/gionee6752_lwt_l/symbols/system/lib64/libpng.so
这样就可以使用addr2line来查找出错的文件行数,有一点需要注意,你烧录到手机上的版本和你电脑上的版本要一致,这个数据才是准确的。
dzt@dzt-All-Series:~/D/dzt/workarea/7501_mp_l/android_mtk_l6752_lwt_mp$ addr2line -e ./out/target/product/gionee6752_lwt_l/symbols/system/lib/libpng.so 000187e3 /home/dzt/D/dzt/workarea/7501_mp_l/android_mtk_l6752_lwt_mp/external/libpng/pngrutil.c:3995 dzt@dzt-All-Series:~/D/dzt/workarea/7501_mp_l/android_mtk_l6752_lwt_mp$ addr2line -e ./out/target/product/gionee6752_lwt_l/symbols/system/lib64/libpng.so 000187e3 /home/dzt/D/dzt/workarea/7501_mp_l/android_mtk_l6752_lwt_mp/external/libpng/pngrtran.c:105 dzt@dzt-All-Series:~/D/dzt/workarea/7501_mp_l/android_mtk_l6752_lwt_mp$如果找到对应的行数就会输出这样的结果
dzt@dzt-All-Series:~/D/dzt/workarea/7501_mp_l/android_mtk_l6752_lwt_mp$ addr2line -e ./out/target/product/gionee6752_lwt_l/system/lib/libpng.so 000187e3 ??:0 dzt@dzt-All-Series:~/D/dzt/workarea/7501_mp_l/android_mtk_l6752_lwt_mp$
这样你就能通过打印的堆栈信息来查找出错的那行代码,更有利于我们定位问题。
这里介绍一种打印堆栈的方法
打开调用栈的方法,在认为会出错的地方自己抛出一个空指针的异常抓获再打印出堆栈
public void setSpeakerphoneOn(boolean on){
IAudioService service = getService();
try {
throw new NullPointerException("the debug exception for speaker");
service.setSpeakerphoneOn(on);
} catch (RemoteException e) {
Log.e(TAG, "Dead object in setSpeakerphoneOn", e);
} catch (NullPointerException e) {
e.printStackTrace();
}
}