linux之gdb调试

1、编译要加上-g选项

gcc -g -o hello hello.c

2、常用命令

l 查看源码 , b 加断点, r 开始运行调试, n 下一步, s下一步但是会进入子函数. p 输出数据.

info args查看当前函数参数值;info locals 看当前函数栈上值信息;info registers 表示查看寄存器值

3、设置条件断点:

b _add if g_val==10

4、删除断点

d + 断点索引1,2,3..

5、多线程调试

info threads 查看所有运行的线程信息;

thread 3表示切换到第三个线程;

6、gdb在ARM开发板上调试

到http://www.gnu.org/software/gdb/download/下载gdb包

(1)在本机PC安装arm-linux-gdb客户端

#tar jxvf gdb-7.2-tar-bz2
#cd gdb-7.2
#./configure --target=arm-linux --prefix=/usr/local/arm-gdb –v(--target配置gdb的目标平台,--prefix配置安装路径)
#make
#make install

这里使用的arm-linux-gcc版本一定要与编译开发板内核的gcc版本一致
这样arm-linux-gdb的客户端就安装到了--prefix所配置安装路径里。

(2)在ARM板上安装gdbserver

#cd gdb-7.2/gdb/gdbserver
#./configure --target=arm-linux --host=arm-linux(--target=arm-linux表示目标平台,--host表示主机端运行的是arm-linux-gdb,不需要配置—prefix,因为gdbserver不在主机端安装运行)
#make CC=/usr/local/arm-linux-gcc-3.4.1/bin/arm-linux-gcc
把生成的 gdbserver 拷贝进目标板,一般在/usr/bin

这里使用的arm-linux-gcc版本一定要与编译开发板内核的gcc版本一致

ARM板上的gdb版本和linux下的gdb版本要一致:arm-linux-gdb -v查看版本号

(3)编译程序

arm-linux-gcc -g hello.c -o hello

在目标板运行   #gdbserver 10.88.33.14:777 hello (#gdbserver 客户端IP地址:端口 调试的程序名)
在客户机运行   #arm-linux-gdb hello  (我都是在同一个目录里进行的,即mount到目标板的那个目录)

(gdb) target remote 10.88.33.1:777 (target remote 目标板IP地址:端口)     与开发板握手

(4)调试

设置好断点之后输入c(continue)继续运行,因为可执行程序hello已经在开发板上运行,不同于在linux上调试的代码输入r

7、安卓系统使用gdb调试:安卓对于C/C++代码的调试

(1)在system/bin目录下查看gdbserver是否存在,如果不存在按上面的方法copy到/system/bin目录下;

(2)查看mediaserver进程号:ps -df |  grep mediaserver    得到1668

(3)启动gdbserver:

gdbserver remote:1235 --attach 1668      //1235是监控的中断号;1668是调试程序的进程ID

(4) 客户端启动gdb: 在android源码中./prebuilts/gcc/linux-x86/arm/arm-eabi-4.8/bin/arm-eabi-gdb

GNU gdb (GDB) 7.6
Copyright (C) 2013 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=x86_64-linux-gnu --target=arm-linux-android".
For bug reporting instructions, please see:
<http://source.android.com/source/report-bugs.html>.
(gdb)

target remote 192.168.31.127:2019       握手

(5)指定调试代码

file ~/**xxx**/out/target/product/aries/symbols/system/bin/mediaserver

注意调试代码在Android.mk中要添加LOCAL_CFLAGS += -g,然后编译image烧入开发板

8、gdb调试u-boot和linux内核

https://www.cnblogs.com/shenlian/archive/2011/06/15/2081392.html

 9、trace跟踪

使用arm-linux-gcc编译trace工具并安装,将trace安装到开发板

trace -o log.txt  ./hello      //跟踪./hello使用了哪些系统调用

trace -o log.txt rmmod first_drv    //当卸载驱动出错时,也可以使用trace跟踪rmmod first_drv命令

发生系统调用时,都会发生异常中断swi,进入中断处理,当./hello是子进程时说明。/hello程序有被跟踪,发信号给主进程trace.trace 根据swi,  #num中的num知道是sys_open还是sys_read或其他系统调用

10、分析core dump文件定位程序出错的地方

ulimit -c    查看core dump文件的大小

ulimit -c unlimited      //不限制core dump文件的大小

./hello     产生core dump文件

将产生的名为core的文件copy到PC虚拟机中的Linux系统中来

arm-linux-gdb  ./hello  ./core

这种方法可以快速找到程序错误的地方,也不用在ARM板上运行gdbserver,但不能打断点

原文地址:https://www.cnblogs.com/zhu-g5may/p/11111520.html