歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 搭建交叉調試環境Arm-Linux-Gdb與gdbserver

搭建交叉調試環境Arm-Linux-Gdb與gdbserver

日期:2017/2/28 16:21:20   编辑:Linux教程
操作系統:Ubuntu9.04 開發板:博創2410s 交叉編譯工具:arm-linux-gcc-4.1.1 gdb+gdbserver 是調試目標板的常用方法.
網絡環境如下:HOST 192.168.1.123 Target: 192.168.1.21
NFS共享目錄: mount -t nfs -o intr,nolock,rsize=1024,wsize=1024 192.168.1.123:/home/www.linuxidc.com/arm2410s /tmp
天藍色:命令 紫色:命令運行結果 紅色:需要注意的
1、准備工作
建立安裝目錄,並修改目錄權限
www.linuxidc.com@ubuntu:~$ sudo mkdir -p /opt/crosstool/arm-gdb
www.linuxidc.com@ubuntu:~$ sudo chown -R www.linuxidc.com /opt/crosstool/arm-gdb

下載gdb-6.5.tar.bz2(或者更新版的gdb)
www.linuxidc.com@ubuntu:~$ cd downloads

2、編譯arm-linux-gdb gdb-6.5使用了autoconf/automake。因此,通過設置configure腳本的--target,--host,--prefix參數就可以方便的移植到別的平台。 --target指定了需要調試的目標機環境,一般設置為交叉編譯器的前綴,比如--target=arm-linux, --target=mips-linux,--target=armv5-linux-uclibc, --target的缺省值為i386-linux, 也就是i386PC機 --host指定編譯後的文件的運行環境,取值可以是i386-linux或者交叉編譯器的前綴,缺省為i386-linux --prefix指定要安裝的目錄
www.linuxidc.com@ubuntu:~/downloads$ tar jxvf gdb-6.5.tar.bz2
www.linuxidc.com@ubuntu:~/downloads$ cd gdb-6.5
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5$ ./configure --target=arm-linux --prefix=/opt/crosstool/arm-gdb
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5$ make
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5$ sudo make install
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5$ ls /opt/crosstool/arm-gdb/bin
arm-linux-gdb arm-linux-gdbtui arm-linux-run
安裝後,在/opt/crosstool/arm-gdb/bin可以看到arm-linux-gdb、arm-linux-gdbtui、arm-linux-run等
配置環境變量
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5$ vi ~/.bashrc
在文件最後加上
if [ -d /opt/crosstool/arm-gdb/bin ]; then
PATH=/opt/crosstool/arm-gdb/bin:$PATH
fi
使剛配置的環境變量生效
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5$ source ~/.bashrc

3、編譯gdbserver
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5$ cd ../gdb-6.5/gdb/gdbserver
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5/gdb/gdbserver$ ./configure --target=arm-linux --host=arm-linux
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5/gdb/gdbserver$ make CC=/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/bin/arm-linux-gcc #這裡是交叉編譯器的路徑
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5/gdb/gdbserver$ ls

可以看到剛剛生成的gdbserver,將其拷貝到nfs共享目錄下,因為gdbserver最終是在目標機上運行。
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5/gdb/gdbserver$ cp gdbserver ~/arm2410s

4、拷貝libthread庫(這一步不做的話,運行gdbserver會出錯!)
www.linuxidc.com@ubuntu:~/downloads/gdb-6.5/gdb/gdbserver$ cd /opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib
www.linuxidc.com@ubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib$ ls -l libthread_db*
-rwxr-xr-x 1 www.linuxidc.com www.linuxidc.com 28092 2009-12-29 18:51 libthread_db-1.0.so
lrwxrwxrwx 1 www.linuxidc.com www.linuxidc.com 17 2009-12-29 18:51 libthread_db.so -> libthread_db.so.1
lrwxrwxrwx 1 www.linuxidc.com www.linuxidc.com 19 2009-12-29 18:51 libthread_db.so.1 -> libthread_db-1.0.so

將libthread_db-1.0.so拷貝到nfs共享目錄下,我的是~/arm2410s,~/arm2410s/lib是我的外部庫存放位置(我的跟文件系統是cramfs,無法直接拷貝到目標板的/lib)
www.linuxidc.com@ubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib$ mkdir ~/arm2410s/lib
www.linuxidc.com@ubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib$ cp libthread_db-1.0.so ~/arm2410s/lib
www.linuxidc.com@ubuntu:/opt/crosstool/gcc-4.1.1-glibc-2.3.2/arm-linux/arm-linux/lib$ cd ~/arm2410s/lib
www.linuxidc.com@ubuntu:~/arm2410s/lib$ ln -s libthread_db-1.0.so libthread_db.so.1
www.linuxidc.com@ubuntu:~/arm2410s/lib$ ln -s libthread_db-1.0.so libthread_db.so

然後在你在開發板上運行gdbserver前,先設置一下庫文件搜索路徑LD_LIBRARAY_PATH
minicom下
[email protected]:/# mount -t nfs -o intr,nolock,rsize=1024,wsize=1024 192.168.1.123:/home/www.linuxidc.com/arm2410s /tmp
[email protected]:/# export LD_LIBRARAY_PATH=${LD_LIBRARAY_PATH}:/tmp/lib

若根文件系統非只讀文件系統,那你大可以直接將libthread_db-1.0.so拷貝到開發板的/lib或/usr/lib,然後建立相應的符合鏈接

4、測試
寫一個程序, 用arm-linux-gcc -g 編譯, 放到nfs共享目錄/home/www.linuxidc.com/arm2410s目錄下,我的程序名為gprs
minicom下
[email protected]:/# cd /tmp
[email protected]:/tmp# ./gdbserver 192.168.1.123:6666 gprs

Process gprs created; pid = 825
Listening on port 6666

其中,gpbserver使用方法: gpbserver 主機ip地址:通信端口 要調試的程序 【程序的命令行參數】 程序的命令行參數是可選的
啟動一個終端,運行如下命令
www.linuxidc.com@ubuntu:~$ cd arm2410s/
www.linuxidc.com@ubuntu:~/arm2410s$ arm-linux-gdb gprs

GNU gdb 6.5
Copyright (C) 2006 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=arm-linux"...

(gdb) target remote 192.168.1.21:6666 #連接目標:target remote 開發板ip地址:通信端口
Remote debugging using 192.168.1.21:6666
0x40001330 in ?? ()

(gdb)

此時,開發板終端可以看到應答信息Remote debugging from host 192.168.1.123
[email protected]:/tmp# gdbserver 192.168.1.123:6666 gprs
Process gprs created; pid = 825
Listening on port 6666
Remote debugging from host 192.168.1.123


現在就可以通過l查看程序源碼, 然後b設置斷點了;運行則用命令c。
5、常用的GDB命令:

load:裝入一個程序
symbol-file:裝入符號庫文件,可以是用-g參數編譯的可執行文件。
f(ile):指定一個可執行文件進行調試,gdb將讀取些文件的調試訊息,如f a.exe
l(ist):列程序出源文件
r(un) :裝載完要調試的可執行文件後,可以用run命令運行可執行文件
b(reak):設置斷點(break point),如b 25,則在源程序的第25行設置一個斷點,當程序執行到第25行時,就會產生中斷;也可以使用b funcname,funcname為函數的名稱,


當程序運行到斷點停下來時,
c(ontinue):c命令可以另中斷的程序繼續執行,直到下一個中斷點或程序結束
p(rint):輸入某個變量的值,如程序定義了一個int a的就是,p a就會輸出aa的當前值
n(ext):程序執行到斷點時中斷執行,可以用n指令進行單步執行
s(tep):程序執行到斷點時中斷執行,可以用s指令進行單步執行進某一函數
q(uit):退出GDB


使用簡化命令,如l,和完整命令list,效果是一樣的。
6、需要注意的問題 1)、簡化命令f不一定能用,gdb會提示
(gdb) f gprs
No symbol table is loaded. Use the "file" command.
可用完整命令file代替 (gdb) file gprs
Reading symbols from /home/www.linuxidc.com/arm2410s/gprs...done.

2)、交錯調試時我們不使用r(un)來執行程序。因為執行完target remote命令後,目標板程序已經在運行,所有應該用continue命令而不是run命令。如果你選擇了重新運行程序,gdb會提示不知道怎麼運行程序,因為開發板那邊gpbserver已經“Killing inferior”退出了.
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y

Starting program: /home/www.linuxidc.com/arm2410s/gprs
Don't know how to run. Try "help target".
Copyright © Linux教程網 All Rights Reserved