使用gdbserver遠(yuǎn)程調(diào)試
1.默認(rèn)crosstool交叉編譯器沒有自帶gdbserver,需要自行編譯
到GNU官方FTP下載,目前最新版的是gdb-6.7.1
下載地址:http://ftp./gnu/gdb/
注:若已有g(shù)dbserver,可以直接跳到步驟4
2.編譯gdbserver
編譯GDB源碼時只需要編譯出gdbserver就可以了,交叉編譯器自帶arm-linux-gdb(client端)可以使用。
gdb-6.7.1
# cd gdb-6.7.1/gdb/gdbserver/
#./configure --host=arm-linux --prefix=/work/install/gdbserver
#make
#make install
這時會在/work/install/gdbserver目錄下生成bin/gdbserver,將其拷貝到nfs文件系統(tǒng)
#cd /work/install/gdbserver
#cp bin/gdbserver /work/nfs/rootfs/bin
3.庫問題
這里需要注意的是運(yùn)行g(shù)dbserver還需要libthread_db庫,若你自己做的文件系統(tǒng)內(nèi)沒有這個庫的話需要從交叉編譯器內(nèi)拷一個過去。
# gdbserver -h (target)
gdbserver: error while loading shared libraries: libthread_db.so.1: cannot open shared object file: No such file or directory
# cp -avf lib/libthread_db* /work/nfs/rootfs_bluetooth_omap/lib/
`/lib/libthread_db-1.0.so' -> `/work/nfs/rootfs/lib/libthread_db-1.0.so'
`/lib/libthread_db.so.1' -> `/work/nfs/rootfs/lib/libthread_db.so.1'
注:若不知道缺少什么庫可以根據(jù)運(yùn)行時錯誤提示拷貝或者用先用strace跟蹤一下:
#strace -f -F -o strace.log gdbserver -h
#vi strace.log
發(fā)現(xiàn)如下字段:
872
writev(2, [{"gdbserver", 9}, {": ", 2}, {"error while loading shared
libra"..., 36}, {": ", 2}, {"libthread_db.so.1", 17}, {": ", 2},
{"cannot open shared object file", 30}, {": ", 2}, {"No such file or
directory", 25}, {"\n", 1}], 10) = 126
872 exit_group(127) = ?
得知缺少libthread_db.so.1庫(紅色部分標(biāo)出)。
4.調(diào)試過程
1)Target端建立遠(yuǎn)程調(diào)試服務(wù)
# gdbserver 192.168.167.170:1234 obexftp (target)
Process obexftp created; pid = 858
Listening on port 1234
2)Host端GDB加載要調(diào)試的程序
這里要調(diào)試的程序得是交叉編譯過的,并且加了-g參數(shù)。不過大部分編譯程序默認(rèn)就是加了-g參數(shù)的,這點可以從編譯時的log看出。
# arm-linux-gdb obexftp
GNU gdb 6.6.50.20070301 (MontaVista 6.6.50-2.0.1.0702865 2007-03-26)
Copyright (C) 2007 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "--host=i686-pc-linux-gnu --target=armv5tl-montavista-linux-gnueabi"...
3)連接到目標(biāo)板調(diào)試服務(wù)
(gdb) target remote 192.168.167.15:1234
Remote debugging using 192.168.167.15:1234
Error while reading shared library symbols:
Dwarf Error: Can't read DWARF data from '/opt/montavista/pro/devkit/arm/v5t_le/target/usr/lib/debug/lib/ld-2.5.90.so.debug'
0x400007a0 in _start () from /opt/montavista/pro/devkit/arm/v5t_le/target/lib/ld-linux.so.3
注:上面兩行錯誤信息暫時不用管,原因還不清楚,但是暫時發(fā)現(xiàn)不影響使用。
連接成功后ARM板上的信息應(yīng)該是這樣的:
# ./gdbserver 192.168.0.29:1234 arm0702_8.out
Process arm0702_8.out created; pid = 228
Remote debugging from host 192.168.0.29 <----多出這一行
上面這行表示宿主機(jī)和開發(fā)板連接成功?,F(xiàn)在我們就可以在Host端像調(diào)試本地程序一樣調(diào)試ARM板上程序。不過,需要注意的是這里執(zhí)行程序要用“c”,不能用“r”。因為程序已經(jīng)在Target Board上面由gdbserver啟動了。
調(diào)試過程如下:
(gdb) b main
Breakpoint 1 at 0x9870: file obexftp.c, line 376.
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00009870 in main at obexftp.c:376
(gdb) c
Continuing.
Error while mapping shared library sections:
/work/install/bluetooth//lib/libobexftp.so.0: No such file or directory.
Error while mapping shared library sections:
/work/install/bluetooth//lib/libc.so.6: No such file or directory.
Breakpoint 1, main (argc=1, argv=0xbed0dec4) at obexftp.c:384
384 if (strstr(argv[0], "ls") != NULL) most_recent_cmd = 'l';
(gdb)
若產(chǎn)生這個錯誤主要是由于該調(diào)試的應(yīng)用程序使用到了額外的庫,而這個庫在gdb默認(rèn)的搜索路徑內(nèi)沒有
(相對與遠(yuǎn)程調(diào)試,gdb默認(rèn)搜索的路徑即為交叉編譯器的庫路徑,下面我會介紹到)
因此,這里我們需要修改一下gdb默認(rèn)的共享庫搜索路徑。
修改的辦法是設(shè)置GDB的環(huán)境變量:
(gdb) show solib-absolute-prefix
The current system root is "/opt/montavista/pro/devkit/arm/v5t_le/target".
上面這個路徑即GDB默認(rèn)的絕對搜索路徑,即交叉編譯器庫路徑
(gdb) show solib-search-path
The search path for loading non-absolute shared library symbol files is .
(gdb) set solib-search-path /work/install/bluetooth/lib
這個路徑為若在solib-absolute-prefix指定的路徑內(nèi)沒有搜索到庫,則再繼續(xù)嘗試從該路徑進(jìn)行搜索。
這點倒有點類似于系統(tǒng)默認(rèn)庫搜索路徑與LD_LIBRARY_PATH的關(guān)系。
詳細(xì)參考GDB手冊中相關(guān)部分:
http://wiki./index.php/GDB_Manual_15_1
設(shè)置好solib-search-path后再運(yùn)行程序:
(gdb) set solib-search-path /work/install/bluetooth/lib/
(gdb) c
Continuing.
Error while reading shared library symbols:
Dwarf Error: Can't read DWARF data from '/opt/montavista/pro/devkit/arm/v5t_le/target/usr/lib/debug/lib/ld-2.5.90.so.debug'
Breakpoint 1, main (argc=1, argv=0xbe896eb4) at obexftp.c:384
384 if (strstr(argv[0], "ls") != NULL) most_recent_cmd = 'l';
(gdb) l
379 char *output_file = NULL;
380 char *move_src = NULL;
381 /* char *inbox; */
382
383 /* preset mode of operation depending on our name */
384 if (strstr(argv[0], "ls") != NULL) most_recent_cmd = 'l';
385 if (strstr(argv[0], "get") != NULL) most_recent_cmd = 'g';
386 if (strstr(argv[0], "put") != NULL) most_recent_cmd = 'p';
387 if (strstr(argv[0], "rm") != NULL) most_recent_cmd = 'k';
388
(gdb)
運(yùn)行成功
注:使用GDB調(diào)試時查看代碼不是很方便。CLWEN使用VIM作為GDB前端界面,結(jié)合gdb的遠(yuǎn)程調(diào)試功能,動態(tài)的將程序當(dāng)前運(yùn)行的代碼顯示在VIM上,查看起來十分方便。其遠(yuǎn)程調(diào)試方法和GDB+GDB Server一樣,但是多了一個GUI界面(VIM)。
參考:《使用CLEWN遠(yuǎn)程調(diào)試》