LINUX应用调试2:GDB调试应用程序

二、应用调试2:使用GDB来调试应用程序
编译:
编译gdb 运行于PC

tar xjf gdb-7.4.tar.bz2
cd gdb-7.4/
./configure --target=arm-linux
make
vi Makefile
     /prefix
          prefix = /usr/local
               mkdir tmp
               make install prefix=$PWD/tmp

把arm-linux-gdb复制到/bin目录

sudo cp arm-linux-gdb /bin
sudo chmod +x /bin/arm-linux-gdb

编译gdbserver 运行于ARM板
cd gdb/gdbserver/ ./configure --host=arm-linux host:表示应用程序编译出来在哪里执行
然后make
出现错误:

linux-arm-low.c:642: error: `PTRACE_GETSIGINFO' undeclared (first use in this function)
linux-arm-low.c:642: error: (Each undeclared identifier is reported only once
linux-arm-low.c:642: error: for each function it appears in.)

缺少头文件
解决方法:

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/work/tools/gcc-3.4.5-glibc-2.3.6/bin
cd /work/tools/gcc-3.4.5-glibc-2.3.6
grep "PTRACE_GETSIGINFO" * -nR
arm-linux/sys-include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO       0x4202
arm-linux/include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO   0x4202
distributed/arm-linux/sys-include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO   0x4202
distributed/arm-linux/include/linux/ptrace.h:27:#define PTRACE_GETSIGINFO       0x4202

 cd -
/work/debug/gdb-7.4/gdb/gdbserverbook@book-desktop:/work/debug/gdb-7.4/gdb/gdbserver$ vi linux-arm-low.c   

添加#include <linux/ptrace.h>

cp gdbserver /work/nfs_root/first_fs/bin

编译要调试的应用,编译时加上-g选项

调试:

//192.168.1.104是ARM上的地址

1. 在ARM板上
gdbserver 192.168.1.104:2345 ./test_debug
出现如下:

/ # gdbserver 192.168.1.104:2345 ./test_debug
Process ./test_debug created; pid = 774
Listening on port 2345

2. 在PC上

/bin/arm-linux-gdb ./test_debug

输入:target remote 192.168.1.104:2345
然后: 使用gdb命令来控制程序

另一种方法:
让程序在开发板上直接运行,当它发生错误时,令它产生core dump文件
然后使用gdb根据core dump文件找到发生错误的地方
在ARM板上:

  1. ulimit -c unlimited
  2. 执行应用程序 : 程序出错时会在当前目录下生成名为core的文件
    然后在28th_app_debug目录下cp /work/nfs_root/first_fs/core .
/ # ulimit -c unlimited
/ # ./test_debug
a = 0x12
Segmentation fault (core dumped)

在PC上:
3. /bin/arm-linux-gdb ./test_debug ./core

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章