這兩種方法都不是我發(fā)明了,,都是網(wǎng)上一些高手公共出來的調(diào)試方法,無奈找不到出處的地方了,,所以就在此總結(jié)一下,以方便android下的調(diào)試:
簡要說明:
android系統(tǒng)中調(diào)試Java非常容易,,一般遇到錯(cuò)誤都在logcat中打印出錯(cuò)時(shí)函數(shù)的調(diào)用關(guān)系,,
而C庫中出錯(cuò)時(shí)只看到一些二進(jìn)制信息,使用gdbserver調(diào)試環(huán)境搭建又比較復(fù)雜,。
方法一:
下在介紹一個(gè)簡單的調(diào)試庫的方法,,當(dāng)然需要有so庫的源代碼
舉例
a) 錯(cuò)誤信息如下,它表示了出錯(cuò)時(shí)的函數(shù)調(diào)用關(guān)系(下面調(diào)上面的)
I/DEBUG ( 634): #00 pc 000078e6 /system/lib/libmultiplayerservice.so
I/DEBUG ( 634): #01 pc 000087bc /system/lib/libmultiplayerservice.so
I/DEBUG ( 634): #02 pc 0000e94e /system/lib/libsensorservice.so
I/DEBUG ( 634): #03 pc 0000a790 /system/lib/libsensorservice.so
I/DEBUG ( 634): #04 pc 0000d4b2 /system/lib/libsensorservice.so
I/DEBUG ( 634): #05 pc 0000d852 /system/lib/libsensorservice.so
I/DEBUG ( 634): #06 pc 00015ece /system/lib/libutils.so
I/DEBUG ( 634): #07 pc 0000153a /system/lib/libsystem_server.so
I/DEBUG ( 634): #08 pc 00001756 /system/lib/libsystem_server.so
I/DEBUG ( 634): #09 pc 0000adb8 /system/lib/libandroid_servers.so
I/DEBUG ( 634): #10 pc 00011cb4 /system/lib/libdvm.so
b)進(jìn)入源碼中帶符號(hào)表的so庫所在目錄
$ cd out/target/product/generic/obj/SHARED_LIBRARIES/libmultiplayerservice_intermediates/LINKED
這個(gè)有個(gè)需要注意的地方:
對(duì)于可執(zhí)行程序及動(dòng)態(tài)庫,,一般在LINKED子目錄中是帶有符號(hào)的庫(沒有經(jīng)過符號(hào)剝離),,如果可執(zhí)行文件中沒有包括調(diào)試符號(hào),您將獲得??:0 作為響應(yīng),。
c)用addr2line命令找到地址對(duì)應(yīng)的程序位置,,動(dòng)態(tài)庫為libmultiplayerservice.so
arm-eabi-addr2line
000078e6 -e libmultiplayerservice.so
結(jié)果:,顯示出對(duì)應(yīng)的程序文件和行數(shù),,如果不是debug版本,,可能有一兩行偏差
frameworks/base/services/multiplayerservice/PlayerSocket.cpp 421 行
d)注意
arm-eabi_addr2line在prebuild/linux-x86/toolchain/arm-eabi-xxx/bin目錄下,
運(yùn)行build/envsetup.sh后即可直接使用它,,同目錄下的objdump, nm也是常用調(diào)試命令
方法二:
1,、首先需要一個(gè)重要的腳本文件:
#!/usr/bin/python
# stack symbol parser
import os
import string
import sys
#define android product name
ANDROID_PRODUCT_NAME = 'generic'
ANDROID_WORKSPACE = os.getcwd()+"/"
# addr2line tool path and symbol path
addr2line_tool = ANDROID_WORKSPACE + 'prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-addr2line'
symbol_dir = ANDROID_WORKSPACE + 'out/target/product/' + ANDROID_PRODUCT_NAME +'/symbols'
symbol_bin = symbol_dir + '/system/bin/'
symbol_lib = symbol_dir + '/system/lib/'
class ReadLog:
def __init__(self,filename):
self.logname = filename
def parse(self):
f = file(self.logname,'r')
lines = f.readlines()
if lines != []:
print 'read file ok'
else:
print 'read file failed'
result =[]
for line in lines:
if line.find('stack') != -1:
print 'stop search'
break
elif line.find('system') != -1:
#print 'find one item' + line
result.append(line)
return result
class ParseContent:
def __init__(self,addr,lib):
self.address = addr # pc address
self.exename = lib # executable or shared library
def addr2line(self):
cmd = addr2line_tool + " -C -f -s -e " + symbol_dir + self.exename + " " + self.address
#print cmd
stream = os.popen(cmd)
lines = stream.readlines();
list = map(string.strip,lines)
return list
inputarg = sys.argv
if len(inputarg) < 2:
print 'Please input panic log'
exit()
filename = inputarg[1]
readlog = ReadLog(filename)
inputlist = readlog.parse()
for item in inputlist:
itemsplit = item.split()
test = ParseContent(itemsplit[-2],itemsplit[-1])
list = test.addr2line()
print "%-30s%s" % (list[1],list[0])
將以上文件保存hy.panic.py
2、相關(guān)的死機(jī)堆棧信息保存 error.txt
例如:
I/DEBUG ( 634): #00 pc 000078e6 /system/lib/libmultiplayerservice.so
I/DEBUG ( 634): #01 pc 000087bc /system/lib/libmultiplayerservice.so
I/DEBUG ( 634): #02 pc 0000e94e /system/lib/libsensorservice.so
I/DEBUG ( 634): #03 pc 0000a790 /system/lib/libsensorservice.so
I/DEBUG ( 634): #04 pc 0000d4b2 /system/lib/libsensorservice.so
I/DEBUG ( 634): #05 pc 0000d852 /system/lib/libsensorservice.so
I/DEBUG ( 634): #06 pc 00015ece /system/lib/libutils.so
I/DEBUG ( 634): #07 pc 0000153a /system/lib/libsystem_server.so
I/DEBUG ( 634): #08 pc 00001756 /system/lib/libsystem_server.so
I/DEBUG ( 634): #09 pc 0000adb8 /system/lib/libandroid_servers.so
I/DEBUG ( 634): #10 pc 00011cb4 /system/lib/libdvm.so
3,、將以上兩個(gè)文件拷貝到android的編譯根路徑下面,,執(zhí)行
python hy.panic.py error.txt
方法2使用非常方便,,相比于加打印效率大大提高,。非常感謝提供腳本的同學(xué)。