gdb

本文深入探讨了GDB的高级调试技巧,包括多线程调试、宏调试、源文件定位、条件断点、命令行参数设置、变量操作、x命令使用及command命令自动化调试。通过实例展示了如何在GDB环境下高效调试程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 检查ulimit -c是否能生成core dump?

2. ulimit -c 8000或unlimit等来设置core文件大小限制;

3. 启动coredump,N种方式:

    gdb exefile corefile

    gdb -c corefile exefile

    gdb -core=corefile exefile

    gdb -c corefile或gdb -core=corefile,启动后再执行file exefile指定符号表。

   gdb启动,然后file xxx, core-file corefile。


如果需要带参数,则gdb exefile,然后执行run时候,带参数。如run “abc”。


http://sourceware.org/gdb/current/onlinedocs/gdb.html

-symbols  file -s  file
Read symbol table from file  file
-exec  file -e  file
Use file  file as the executable file to execute when appropriate, and for examining pure data in conjunction with a core dump. 
-se  file
Read symbol table from file  file and use it as the executable file. 
-core  file -c  file
Use file  file as a core dump to examine. 
-pid  number -p number
Connect to process ID number, as with the attach command.  ----------- 可以指定gdb调试一个进程!!!使用ptrace来实现的!!!
-command  file -x  file
Execute commands from file  file. The contents of this file is evaluated exactly as the  source command would. See  Command files
-eval-command  command -ex  command
Execute a single  gdb command.

This option may be used multiple times to call multiple commands. It may also be interleaved with ‘-command’ as required.

          gdb -ex 'target sim' -ex 'load' \
             -x setbreakpoints -ex 'run' a.out

-init-command  file -ix  file
Execute commands from file  file before loading the inferior (but after loading gdbinit files). See  Startup
-init-eval-command  command -iex  command
Execute a single  gdb command before loading the inferior (but after loading gdbinit files). See  Startup
-directory  directory -d  directory
Add  directory to the path to search for source and script files. 
-r -readnow
Read each symbol file's entire symbol table immediately, rather than the default, which is to read it incrementally as it is needed. This makes startup slower, but makes future operations faster.



http://coolshell.cn/articles/3643.html

一、多线程调试

http://blog.youkuaiyun.com/an_zhenwei/article/details/11810727


二、调试宏

这个问题超多。在GDB下,我们无法print宏定义,因为宏是预编译的。但是我们还是有办法来调试宏,这个需要GCC的配合。

在GCC编译程序的时候,加上-ggdb3参数,这样,你就可以调试宏了。

另外,你可以使用下述的GDB的宏调试命令 来查看相关的宏。

  • info macro xxx – 你可以查看这个宏在哪些文件里被引用了,以及宏定义是什么样的。
  • macro – 你可以查看宏展开的样子。

三、源文件

这个问题问的也是很多的,太多的朋友都说找不到源文件。在这里我想提醒大家做下面的检查:

  1. 编译程序员是否加上了-g参数以包含debug信息。
  2. 路径是否设置正确了。使用GDB的directory命令来设置源文件的目录。

下面给一个调试/bin/ls的示例(ubuntu下)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ apt-get source coreutils
$ sudo apt-get install coreutils-dbgsym
$ gdb /bin/ls
GNU gdb (GDB) 7.1-ubuntu
(gdb) list main
1192    ls .c: No such file or directory.
in ls .c
(gdb) directory ~ /src/coreutils-7 .4 /src/
Source directories searched: /home/hchen/src/coreutils-7 .4:$cdir:$cwd
(gdb) list main
1192        }
1193    }
1194
1195    int
1196    main (int argc, char **argv)
1197    {
1198      int i;
1199      struct pending *thispend;
1200      int n_files;
1201
四、条件断点

条件断点是语法是:break  [where] if [condition],这种断点真是非常管用。尤其是在一个循环或递归中,或是要监视某个变量。注意,这个设置是在GDB中的,只不过每经过那个断点时GDB会帮你检查一下条件是否满足。

五、命令行参数

有时候,我们需要调试的程序需要有命令行参数,很多朋友都不知道怎么设置调试的程序的命令行参数。其实,有两种方法:

  1. gdb命令行的 –args 参数
  2. gdb环境中 set args命令。
六、gdb的变量

有时候,在调试程序时,我们不单单只是查看运行时的变量,我们还可以直接设置程序中的变量,以模拟一些很难在测试中出现的情况,比较一些出错,或是switch的分支语句。使用set命令可以修改程序中的变量。

另外,你知道gdb中也可以有变量吗?就像shell一样,gdb中的变量以$开头,比如你想打印一个数组中的个个元素,你可以这样:

1
2
3
4
5
(gdb) set $i = 0
 
(gdb) p a[$i++]
 
...   #然后就一路回车下去了

当然,这里只是给一个示例,表示程序的变量和gdb的变量是可以交互的。

七、x命令

也许,你很喜欢用p命令。所以,当你不知道变量名的时候,你可能会手足无措,因为p命令总是需要一个变量名的。x命令是用来查看内存的,在gdb中 “help x” 你可以查看其帮助。

  • x/x 以十六进制输出
  • x/d 以十进制输出
  • x/c 以单字符输出
  • x/i  反汇编 – 通常,我们会使用 x/10i $ip-20 来查看当前的汇编($ip是指令寄存器)
  • x/s 以字符串输出
八、command命令

有一些朋友问我如何自动化调试。这里向大家介绍command命令,简单的理解一下,其就是把一组gdb的命令打包,有点像字处理软件的“宏”。下面是一个示例:

1
2
3
4
5
6
7
8
9
10
(gdb) break func
Breakpoint 1 at 0x3475678: file test .c, line 12.
(gdb) command 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end" .
>print arg1
>print arg2
>print arg3
>end
(gdb)

当我们的断点到达时,自动执行command中的三个命令,把func的三个参数值打出来。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值