gdb+gdbserver远程调试技术

来源:互联网 发布:单片机编程题 编辑:程序博客网 时间:2024/04/30 10:25


首先声明,此文是在别人的基础上添加一些自己的体会。之前做嵌入式开发的时候,弄过一段时间gdb+gdbserver远程调试,最后无果而终,只好将就用printf。。。

首先感谢这篇刘品的文章,看着整洁清楚,http://www.cnblogs.com/huaping-audio/archive/2009/06/19/1506898.html     从中知道了,原来gdb和gdbserver版本要一样,系统自带的gdb就不行了,因为找不到源码,没法编译对应的嵌入式版本的gdbserver。

其次,这篇文章网上找不到了,在下面贴出来,一开始的时候,测试自己的程序,发现有库的问题,根据这篇文件解决了这个问题,但是还是一直是看不到源码,纠结的。。。最后无奈写了个简单的10行的main函数,测试居然可以查看源码和堆栈,自己的程序加了-g的,为什么无法查看源码。。。。。。隔了一天,偶然间注意到了arm-linux-strip。。。,难道是这个的原因?去掉Makefile中的这行,测试,果然如此,顿时各种无语。。记录下

使用gdbserver远程调试 1.默认crosstool交叉编译器没有自带gdbserver,需要自行编译到GNU官方FTP下载,目前最新版的是gdb-6.7.1下载地址:http://ftp.gnu.org/gnu/gdb/注:若已有gdbserver,可以直接跳到步骤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文件系统#cd /work/install/gdbserver#cp bin/gdbserver /work/nfs/rootfs/bin 3.库问题这里需要注意的是运行gdbserver还需要libthread_db库,若你自己做的文件系统内没有这个库的话需要从交叉编译器内拷一个过去。# 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'注:若不知道缺少什么库可以根据运行时错误提示拷贝或者用先用strace跟踪一下:#strace -f -F -o strace.log gdbserver -h#vi strace.log  发现如下字段: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) = 126872   exit_group(127)                   = ?得知缺少libthread_db.so.1库(红色部分标出)。 4.调试过程1)Target端建立远程调试服务# gdbserver 192.168.167.170:1234 obexftp (target)Process obexftp created; pid = 858Listening on port 1234 2)Host端GDB加载要调试的程序这里要调试的程序得是交叉编译过的,并且加了-g参数。不过大部分编译程序默认就是加了-g参数的,这点可以从编译时的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 arewelcome 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)连接到目标板调试服务(gdb) target remote 192.168.167.15:1234Remote debugging using 192.168.167.15:1234Error 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注:上面两行错误信息暂时不用管,原因还不清楚,但是暂时发现不影响使用。连接成功后ARM板上的信息应该是这样的: # ./gdbserver 192.168.0.29:1234 arm0702_8.out Process arm0702_8.out created; pid = 228 Remote debugging from host 192.168.0.29  <----多出这一行上面这行表示宿主机和开发板连接成功。现在我们就可以在Host端像调试本地程序一样调试ARM板上程序。不过,需要注意的是这里执行程序要用“c”,不能用“r”。因为程序已经在Target Board上面由gdbserver启动了。调试过程如下:(gdb) b mainBreakpoint 1 at 0x9870: file obexftp.c, line 376.(gdb) info bNum Type           Disp Enb Address    What1   breakpoint     keep y   0x00009870 in main at obexftp.c:376(gdb) cContinuing.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:384384             if (strstr(argv[0], "ls") != NULL)      most_recent_cmd = 'l';(gdb)若产生这个错误主要是由于该调试的应用程序使用到了额外的库,而这个库在gdb默认的搜索路径内没有(相对与远程调试,gdb默认搜索的路径即为交叉编译器的库路径,下面我会介绍到)因此,这里我们需要修改一下gdb默认的共享库搜索路径。修改的办法是设置GDB的环境变量:(gdb) show solib-absolute-prefixThe current system root is "/opt/montavista/pro/devkit/arm/v5t_le/target". 上面这个路径即GDB默认的绝对搜索路径,即交叉编译器库路径(gdb) show solib-search-pathThe search path for loading non-absolute shared library symbol files is .(gdb) set solib-search-path /work/install/bluetooth/lib这个路径为若在solib-absolute-prefix指定的路径内没有搜索到库,则再继续尝试从该路径进行搜索。这点倒有点类似于系统默认库搜索路径与LD_LIBRARY_PATH的关系。详细参考GDB手册中相关部分:http://wiki.chinaunix.net/index.php/GDB_Manual_15_1设置好solib-search-path后再运行程序:(gdb) set solib-search-path /work/install/bluetooth/lib/(gdb) cContinuing.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:384384             if (strstr(argv[0], "ls") != NULL)      most_recent_cmd = 'l';(gdb) l379             char *output_file = NULL;380             char *move_src = NULL;381             /* char *inbox; */382383             /* 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) 运行成功  注:使用GDB调试时查看代码不是很方便。CLWEN使用VIM作为GDB前端界面,结合gdb的远程调试功能,动态的将程序当前运行的代码显示在VIM上,查看起来十分方便。其远程调试方法和GDB+GDB Server一样,但是多了一个GUI界面(VIM)。参考:《使用CLEWN远程调试》http://blog.chinaunix.net/u2/60011/showart_1006565.html

0 0