gdb & gdbserver 在arm上移植,及调试配置

1. 下载gdb
http://ftp.gnu.org/gnu/gdb/
http://ftp.gnu.org/gnu/gdb/gdb-8.2.tar.xz
解压到:
tar -Jxvf gdb-8.2.tar.xz
/home/semilog/working/gdb/gdb-8.2/

2. 编译

(1) 先编译在 pc 上运行的 gdb  (arm-linux-gnueabihf-gdb)
此时使用的编译器是 ubuntu 下面的 gcc 
./configure --target=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf-  --prefix=/home/semilog/working/gdb/pc_gdb
说明:
--target=arm-linux-gnueabihf  :目标调试的类型,我们是要用来调试 arm 平台下的linux下应用
--program-prefix=arm-linux-gnueabihf- : 在pc上运行的调试目标arm 的程序的缀最终生成
--prefix=/home/semilog/working/gdb/pc_gdb : 安装目录,  表示在pc 上运行的gdb

此时在 /home/semilog/working/gdb/gdb-8.2/下面config 和 编译。

make -j4
make install

(2) 再来编译在ARM板上运行的 gdbserver

source env_setup.sh

#!/bin/sh

TOOLCHAIN_PATH=/home/semilog/semilog/gcc-linaro-5.3-2016.02-x86_64_arm-linux-gnueabihf
export CROSS_COMPILE=$TOOLCHAIN_PATH/bin/arm-linux-gnueabihf-
export PATH=$TOOLCHAIN_PATH/bin:$PATH
export PROJECT=gdb
#export PREFIX=/home/semilog/semilog/bin
export PS1="\[\e[32;1m\][$PROJECT]\[\e[0m\]:\w> "

/home/semilog/working/gdb/gdb-8.2 目录下:
make distclean
./configure --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --prefix=/home/semilog/working/gdb/arm_gdb

make -j2
make install
cd /home/semilog/working/gdb/arm_gdb/bin/
arm-linux-gnueabihf-strip gdbserver
arm-linux-gnueabihf-strip gdb

PS:
如果只想编译gdbserver, 可以按如下操作:

cd  /home/semilog/working/gdb/gdb-8.2/gdb/gdbserver
./configure --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc

make -j2
arm-linux-gnueabihf-strip gdbserver


不需要 make install 了。  最后发现,这样编译出来的gdbserver 小很多,只有上面的3分之一  放在arm上运行也ok


3. 配置运行
gdb和gdbserver之间可以通过TCP(格式为: host:port)、UDP(格式为: udp: host:port)或者串口(比如/dev/ttyS2)来通信,我们以TCP方式为例来说明。
假设目标板的IP为192.168.1.1,主机为192.168.1.100,使用端口6000来调试,首先在目标板上运行gdbserver:
# gdbserver 192.168.1.1:6000 ./test_os
Process ./test_os created; pid = 1297
Listening on port 6000

然后在主机上运行gdb,并运行gdb命令“target remote 192.168.1.1:6000”:
$arm-linux-gnueabihf-gdb ./overflow
GNU gdb 8.2
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.1:6000
     Remote debugging using 192.168.1.1:6000
     0x28556080 in ??()
(gdb)

接下来你就可以象前面所介绍的那样使用gdb命令了,比如设置断点及查看变量单步执行等。


----------------------------------

4. 实际使用

出错如下:

(gdb) target remote 192.168.48.1:6000
Remote debugging using 192.168.48.1:6000
warning: Can not parse XML target description; XML support was disabled at compile time
Reading /lib/ld-linux-armhf.so.3 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /lib/ld-linux-armhf.so.3 from remote target...
Reading symbols from target:/lib/ld-linux-armhf.so.3...done.
Remote 'g' packet reply is too long (expected 168 bytes, got 328 bytes): 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020fdffbe00000000007afdb6100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

(1). 没有XML库,出错
(gdb)target remote 192.168.48.1:6000

warning: Can not parse XML target description; XML support was disabled at compile time

https://github.com/libexpat/libexpat

配置并编译expat
ubuntu要安装:docbook-to-man
sudo apt-get install docbook-to-man   
./buildconf.sh
  ./configure --without-docbook --prefix=/home/semilog/working/gdb/expat_out

   make && make install

这个编译过程中可能会出错,把doc编译安装的去掉。一切ok

再编译:

./configure   

./configure --target=arm-linux-gnueabihf --program-prefix=arm-linux-gnueabihf-  --prefix=/home/semilog/working/gdb/pc_gdb --with-expat --includedir=/home/semilog/working/gdb/expat_out/include --libdir=/home/semilog/working/gdb/expat_out/lib

make 
make install

这次再连接,果然就可以调试了。

(2)其中
Reading /lib/ld-linux-armhf.so.3 from remote target...

这里要设置
(gdb) set sysroot  /system/out/targetfs/
targetfs 对应于arm板上的根文件系统:


5. 总结

使用流程如下:
test_os: 为要调试的arm板上的程序
arm板的ip地址:192.168.1.1
/home/semilog/output/targetfs/ : 根文件系统路径
/home/semilog/test/test_os:  下载到arm板上运行的程序test_os的路径
-tui:  参数的话,可以看到这块源码,根据个人习惯使用

1. 连上arm板
gdbserver 192.168.1.1:6000 test_os


2. ubuntu的pc上
arm-linux-gnueabihf-gdb -tui /home/semilog/test/test_os     // 加上/home/semilog/test/test_os是为什么在pc 上看到源码,方便

进入GDB:

(gdb) set sysroot /home/semilog/output/targetfs/
(gdb) target remote 192.168.1.1:6000

然后就一样用了

经测试可以。

经测试可以使用如下命令指定符号表

(gdb) symbol-file /home/semilog/test/test_os

最后我发现,我用编译器自己带的arm-linux-gnueabihf-gdb 也可以连接上调试。看来版本并不一定要严格对应。

猜你喜欢

转载自blog.csdn.net/semilog/article/details/83622760