1、编译选项
make menuconfig后,需要选择相关编译选项
勾选gdb和gdbserver,在Development目录下,将gdbserver选为M
2、sstrip选项置为none
sstrip选项会让生成的可执行文件没有debugging symbol,所以要取消,在Global build settings->Binary stripping method
3、Global build settings下开启Compile packages with debugging info选项
Compile packages with debugging info对所有安装包起作用,如果只想对自己的起作用,可以不勾选,而是自己的Makefile中加上-g编译选项
4、找到gdb和gdbserver安装包,上传到openwrt
实际上,gdb还有几个依赖包,以及自己的测试包(helloworld)需要上传
root@OpenWrt:/# ls *.ipk
gdb_8.0.1-1_i386_pentium4.ipk helloworld_1_i386_pentium4.ipk libreadline_7.0-1_i386_pentium4.ipk
gdbserver_8.0.1-1_i386_pentium4.ipk libncurses_6.1-1_i386_pentium4.ipk terminfo_6.1-1_i386_pentium4.ipk
5、安装上面的软件包
opkg install gdb_8.0.1-1_i386_pentium4.ipk
。。。
6、首次试用
1)设备端直接调试
ssh登录到设备端,执行gdb /bin/helloworld
如下为程序源码:
src/helloworld.c
#include <stdio.h>
void C(int *p)
{
*p = 0x12;
}
void B(int *p)
{
C(p);
}
void A(int *p)
{
B(p);
}
void A2(int *p)
{
C(p);
}
int main(int argc, char **argv)
{
int a;
int *p = NULL;
A2(&a); // A2 > C
printf("a = 0x%x\n", a);
A(p); // A > B > C
return 0;
}
值得注意的是:在 src/Makefile中,添加一行:
CFLAGS+=-g
2) 远程调试
a、在目标机上启动gdbserver
root@OpenWrt:~# gdbserver 127.0.0.1:3000 helloworld
Process cpp11-demo created; pid = 3335
Listening on port 3000
如上,命令格式为:gdbserver <local IP>:<port> <program> [args list]
<local IP>就写成127.0.0.1,<port>指定为3000,要调试的是debug-demo程序。
b、在开发机上启动gdb,并执行 target remote 192.168.1.2:3000 进行连接:
yp@ubuntu:/home/openwrt/openwrt$ ./staging_dir/toolchain-i386_pentium4_gcc-7.3.0_musl/bin/i486-openwrt-linux-gdb
(gdb) target remote 192.168.1.2:3000
Remote debugging using 192.168.1.2:3000
warning: while parsing target description (at line 10): Target description specified unknown architecture "mips"
warning: Could not load XML target description; ignoring
Reply contains invalid hex digit 59
好,再连接目标机:
(gdb) target remote 192.168.1.2:3000
Remote debugging using 192.168.1.2:3000
warning: Can not parse XML target description; XML support was disabled at compile time
0x77fe0f40 in ?? ()
(gdb)
3)core调试
在设备端执行:
ulimit -c unlimited
在/tmp/目录下生成core文件,可执行
gdb /bin/helloworld /tmp/helloworld.1528191802.3045.11.core
或者拷贝到主机端,执行
yp@ubuntu:/home/openwrt/openwrt$ ./staging_dir/toolchain-i386_pentium4_gcc-7.3.0_musl/bin/i486-openwrt-linux-gdb ./build_dir/target-i386_pentium4_musl/helloworld/ipkg-i386_pentium4/helloworld/bin/helloworld helloworld.1528191802.3045.11.core
GNU gdb (GDB) 7.4 Copyright (C) 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "--host=i686-pc-linux-gnu --target=arm-linux". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>... Reading symbols from /home/share/jz2440/test_debug...done. [New LWP 748] warning: `/lib/libc.so.6': Shared library architecture unknown is not compatible with target architecture arm. warning: `/lib/ld-linux.so.2': Shared library architecture unknown is not compatible with target architecture arm. Core was generated by `./test_debug'. Program terminated with signal 11, Segmentation fault. #0 0x000084ac in C (p=0x0) at test_debug.c:6 6 *p = 0x12;
#0 0x000084ac in C (p=0x0) at test_debug.c:6 #1 0x000084d0 in B (p=0x0) at test_debug.c:12 #2 0x000084f0 in A (p=0x0) at test_debug.c:17 #3 0x00008554 in main (argc=1, argv=0xbeb32eb4) at test_debug.c:34
7、调试命令