GNU工具集中的调试器是GDB (GNU Debugger),该程序是一个交互式工具,工作在字符模式。除gdb外,linux下比较有名的调试器还有xxgdb,ddd, kgdb,ups。
GDB主要帮忙你完成下面四个方面的功能
一般来说GDB主要调试的是C/C++的程序。要调试C/C++的程序,首先在编译时,我们必须要把调试信息加到可执行文件中。使用编译器 (cc/gcc/g++) 的-g 参数可以做到这一点。如:
gcc -g hello.c -o hello
g++ -g hello.cpp -o hello
如果没有-g,你将看不见程序的函数名、变量名,所代替的全是运行时的内存地址.
用list命令来打印程序的源代码。默认打印10行.
一般是打印当前行的上5行和下5行,如果显示函数是是上2行下8行,默认
围,使用下面命令可以设置一次显示源程序的行数
root@sony-HP-Notebook:/usr/local/cpp_demo/gdb# gdb a.out
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...
(gdb) b main
Breakpoint 1 at 0x11a9: file test.c, line 9.
(gdb) list
1 #include <stdio.h>
2 void fun(void)
3 {
4 int i = 0;
5 for (i = 0; i < 10; i++)
6 printf("fun==> i = %d\n", i);
7 }
8 int main(int argc, char **argv)
9 {
10 int i = 0;
(gdb) l
11 // 将传入参数全部输出
12 for (i = 0; i < argc; i++)
13 {
14 printf("argv[%d]: %s\n",i, argv[i]);
15 }
16
17 fun();
18 printf("hello world\n");
19 return 0;
20 }
(gdb) l
Line number 21 out of range; test.c has 20 lines.
(gdb)
break 设置断点,可以简写为b
C++中可以使用class::function或function(type,type)
格式来指定函数名
如果有名称空间,可以使用namespace::class::funtion或者function(type,type)
格式来指定函数名
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000000011a9 in main at test.c:9
一般来说,为断点设置一个条件,我们使用if关键词,后面跟其断点条件。设置一个条件断点:
b test.c:14 if i == 5
用法示例
root@sony-HP-Notebook:/usr/local/cpp_demo/gdb# gdb a.out
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...
(gdb) b test.c:14 if i==5
Breakpoint 1 at 0x11cc: file test.c, line 14.
(gdb) set args 1 2 3 4 5 6 7
(gdb) start
Temporary breakpoint 2 at 0x11a9: file test.c, line 9.
Starting program: /usr/local/cpp_demo/gdb/a.out 1 2 3 4 5 6 7
Temporary breakpoint 2, main (argc=21845, argv=0x0) at test.c:9
9 {
(gdb) continue
Continuing.
argv[0]: /usr/local/cpp_demo/gdb/a.out
argv[1]: 1
argv[2]: 2
argv[3]: 3
argv[4]: 4
Breakpoint 1, main (argc=8, argv=0x7fffffffe6d8) at test.c:14
14 printf("argv[%d]: %s\n",i, argv[i]);
(gdb) p i
$1 = 5
(gdb)
d 10
# 批量删除
d 10-12
如果什么都不指定,表示disable所有的停止点
如果什么都不指定,表示enable所有的停止点
查看运行时数据
print 打印变量、字符串、表达式等的值,可简写为p
在条件断点示例中有使用
p count 打打印count的信
你可以设置一些自动显示的变量,当程序停住时,或是在你单步跟踪时,这些变量会自动显示。相关的GDB命令是display。
type = double
$4=13
你可以使用set var命令来告诉GDB,width不是你GDB的参数,而是程序的变量名,
如:set var width=47 // 将变量var值设置为47
在你改变程序变量取值时,最好都使用set var格式的GDB命令
使用gdb调试的时候,gdb只能跟踪一个进程。可以在fork函数调用之前,通过指令设置gdb调试工具跟踪父进程或者是跟踪子进程。默认跟踪父进程。
注意,一定要在fork函数调用之前设置才有效