OpenWRT的debug功能使用

1、编译选项

make menuconfig后,需要选择相关编译选项

勾选gdb和gdbserver,在Development目录下,将gdbserver选为M

OpenWRT的debug功能使用_第1张图片

2、sstrip选项置为none

sstrip选项会让生成的可执行文件没有debugging symbol,所以要取消,在Global build settings->Binary stripping method

OpenWRT的debug功能使用_第2张图片

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 
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 : [args list]

就写成127.0.0.1,指定为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

好,再连接目标机:

(gdbtarget remote 192.168.1.2:3000
Remote debugging using 192.168.1.2:3000
warningCan not parse XML target descriptionXML 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 //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:
//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;
复制代码
  bt:可以显示调用关系
 
#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、调试命令

  (1)l:列出所有源代码
  (2)break main:在main处打断点
          break test_debug.c:11:在test_debug.c的11行打断点
  (3)c:运行到断点处
  (4)step:单步执行
  (5)next:单步执行,但是step会进入函数里面,但是next不会
  (6)print a:打印a这个变量的值
  (7)quit:退出,输入此命令则开发板上的gdbserver也退出
   (8)  详细的GDB调试命令
     (9) 把代码上传到openwrt,在源代码下执行gdb  /bin/helloworld,然后run,list可以看见代码


你可能感兴趣的:(linux,物联网)