C++ GDB调试工具

2019/09/18 C++

GDB(GNU Project Debugger)可以帮助用户看到在程序运行过程中,程序内部发生了什么,或者在程序crash的时候,程序在做什么。是调试C++的不二工具,在IDE中的调试功能实际也是应用了GDB工具。

GDB调试示例

在编译时应当加上-g的命令,保留调试信息。

然后用gdb test进行GDB环境,执行目标文件。下面是一个GDB调试的示例,可以显示代码,设置断点,查看局部变量等。

$ g++ test.cpp -o test -g
$ gdb test
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 test...done.
(gdb) l
1	#include <iostream>
2	
3	int sum(const int& a, const int& b) {
4	    int tmp_sum = a + b;
5	    return tmp_sum;
6	} 
7	 
8	int main() {
9	    int a = 1, b = 2;
10	    a = b;
(gdb) l
11	    int c = sum(a, b);
12	    std::cout << "sum is " << c << std::endl;
13	    return 0;
14	}
(gdb) l
Line number 15 out of range; test.cpp has 14 lines.
(gdb) break 10
Breakpoint 1 at 0x40094d: file test.cpp, line 10.
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040094d in main() at test.cpp:10
(gdb) dele[K[K[K[Kr
Starting program: /home/SENSETIME/zhushengfa/ws/c_test/test 

Breakpoint 1, main () at test.cpp:10
10	    a = b;
(gdb) p a
$1 = 1
(gdb) p b
$2 = 2
(gdb) s[Kn
11	    int c = sum(a, b);
(gdb) p a
$3 = 2
(gdb) p b
$4 = 2
(gdb) s
sum (a=@0x7fffffffd82c: 2, b=@0x7fffffffd830: 2) at test.cpp:4
4	    int tmp_sum = a + b;
(gdb) p su[K[Ktmp_sum
$5 = 0
(gdb) p a
$6 = (const int &) @0x7fffffffd82c: 2
(gdb) s
5	    return tmp_sum;
(gdb) p tmp_sum
$7 = 4
(gdb) p &tmp_sum
$8 = (int *) 0x7fffffffd80c
(gdb) p &a
$9 = (const int *) 0x7fffffffd82c
(gdb) s
6	} 
(gdb) s
main () at test.cpp:12
12	    std::cout << "sum is " << c << std::endl;
(gdb) p &a
$10 = (int *) 0x7fffffffd82c
(gdb) n
sum is 4
13	    return 0;
(gdb) n
14	}
(gdb) n
__libc_start_main (main=0x400928 <main()>, argc=1, argv=0x7fffffffd928, init=<optimized out>, 
    fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffd918) at ../csu/libc-start.c:325
325	../csu/libc-start.c: No such file or directory.
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040094d in main() at test.cpp:10
	breakpoint already hit 1 time
(gdb) delete break 1
(gdb) info break
No breakpoints or watchpoints.
(gdb) q
A debugging session is active.

	Inferior 1 [process 31780] will be killed.

Quit anyway? (y or n) y

GDB调试命令

列出常用的GDB调试命令如下表,便于参考。

command function
r(run) 运行程序
l(list) 列出源代码
s(step) 单步执行,进入调用函数内部
n(next) 单行执行
break n 第n行设置断点
info break 查看断点信息
delete break 删除断点
p variable 打印variable的值
continue 继续运行
q 退出GDB模式

Search

    Table of Contents