某些時候由於模擬環境的限制,調試必須要在目標板上進行。由於嵌入式系統資源比較有限,一般不能在目標板上直接構建GDB的調試環境,這時我們通常採用gdb+gdbserver的遠程調試方法:gdbserver在目標板中運行,而gdb則在主機上運行。
構建gdb+gdbserver調試環境的在於,要將gdb和gdbserver都編譯成適用於目標板的版本。比如我們用x86的主機和ARM目標板,平時在主機上直接調試的時候都使用用於x86調試的gdb,但這個gdb不能用於遠程調試中,需要針對ARM平臺進行配置之後重新編譯才行;而gdbserver要運行在目標板上,則需要用arm-linux-gcc編譯才行。
我們可以從http://ftp.gnu.org/gnu/gdb/或其他站點下載GDB的源代碼來進行編譯。得到源代碼包gdb-6.6.tar.gz之後,將target配置成arm-linux,然後進行編譯:
$tar xzvf gdb-6.6.tar.gz $cd gdb-6.6 $./configure --target=arm-linux $make $make install |
注意這時我們編譯的是用於主機上的gdb程序,因此仍然用x86版本的gcc編譯,而不是用arm-gcc。而接下來我們要編譯的gdbserver程序則是運行在目標板上的,需要用arm-gcc來編譯了,用CC=<your_arm-linux-gcc_path>來指定arm-linux-gcc編譯器:
$cd gdb/gdbserver/ $./configure --target=arm-linux --host=arm-linux $make CC=/opt/toolchain/bin/arm-linux-gcc $make install |
得到gdb和gdbserver之後,將gdbserver下載到目標板上就可以進行遠程調試了。我們還是以前面用過的overflow程序爲例來說明,注意overflow程序也需要重新用arm-linux-gcc編譯得到ARM版本的overflow程序,並下載到目標板上。
完成這些之後可以用file命令來檢查所準備gdb和gdbserver及overflow程序的格式是否正確:
$file arm-linux-gdb arm-linux-gdb:ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV),for GNU/Linux 2.4.17, dynamically linked (uses shared libs), notstripped $file overflow overflow:ELF 32-bit LSB executable, ARM, version 1 (SYSV), forGNU/Linux 2.4.17, dynamically linked (uses shared libs), notstripped $file gdbserver gdbserver:ELF 32-bit LSB executable, ARM, version 1 (SYSV), forGNU/Linux 2.4.17, dynamically linked (uses shared libs), notstripped |
注意確保在目標板上運行的gdbserver及overflow程序被編譯成ARMELF格式,而gdb由於是運行在主機上,還是x86格式的。
gdb和gdbserver之間可以通過TCP(格式爲host:port)、UDP(格式爲udp:host:port)或者串口(比如/dev/ttyb)來通信,我們以TCP方式爲例來說明。
假設目標板的IP爲192.168.2.1,主機爲192.168.2.100,使用端口5678來調試,首先在目標板上運行gdbserver:
#gdbserver 192.168.16.1:5678 ./overflow Process./overflow created; pid = 618 Listeningon port 5678 |
然後在主機上運行gdb,並運行gdb命令“targetremote 192.168.2.1:5678”:
$arm-linux-gdb ./overflow GNUgdb 6.6 Copyright(C) 2006 Free Software Foundation, Inc. GDBis free software, covered by the GNU General Public License, andyou are welcometo change it and/or distribute copies of it under certainconditions. Type"show copying" to see the conditions. Thereis absolutely no warranty for GDB. Type "show warranty"for details. ThisGDB was configured as "--host=i686-pc-linux-gnu--target=arm-linux"... (gdb)target remote 192.168.2.1:5678 Remotedebugging using 192.168.2.1:5678 0x28556080in ??() (gdb) |
接下來你就可以象前面所介紹的那樣使用gdb命令了,比如設置斷點及查看變量單步執行等。