要调试生成的可执行程序,必须在生成的时候加入-g选项,生成可以调试的代码
例如:gcc -o test a.c b.c -g
这样gcc就会在链接的时候加入一些用于调试的符号
在生成可以调试的可执行程序后,使用gdb命令进入调试模式
1 [email protected]:/home/leo/test/project/classes# gdb test 2 GNU gdb (Ubuntu 7.10-1ubuntu2) 7.10 3 Copyright (C) 2015 Free Software Foundation, Inc. 4 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 5 This is free software: you are free to change and redistribute it. 6 There is NO WARRANTY, to the extent permitted by law. Type "show copying" 7 and "show warranty" for details. 8 This GDB was configured as "x86_64-linux-gnu". 9 Type "show configuration" for configuration details. 10 For bug reporting instructions, please see: 11 <http://www.gnu.org/software/gdb/bugs/>. 12 Find the GDB manual and other documentation resources online at: 13 <http://www.gnu.org/software/gdb/documentation/>. 14 For help, type "help". 15 Type "apropos word" to search for commands related to "word"... 16 Reading symbols from test...done. 17 (gdb)
此时,程序并没有开始运行,在运行程序之前,可以做一些准备工作
1. 设置运行参数
(gdb) set args 5 设置第一个参数为5
(gdb) show args
Argument list to give program being debugged when it is started is "5".
2. 设置断点,break命令,简写b
在指定行数设置断点
(gdb) b 8 在第8行设置断点 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb)
为指定函数设置断点
(gdb) b add_int Breakpoint 3 at 0x400660: file math/add_int.cpp, line 5.
在指定行数或函数设置条件变量
(gdb) b add_int if num1 == 1 Breakpoint 4 at 0x400660: file math/add_int.cpp, line 5.
查看断点
(gdb) info breakpoints Num Type Disp Enb Address What 4 breakpoint keep y 0x0000000000400660 in add_int(int, int) at math/add_int.cpp:5 stop only if num1 == 1 breakpoint already hit 1 time
禁用断点 disable b 4,禁用4号断点
启动断点 enable b 4,启用4号断点
删除断点 clear 51 删除51行的断点
3. 查看源文件,list命令,简写l
(gdb) list 1 #include "stdlib.h" 2 #include "stdio.h" 3 4 int add_int(int, int); 5 float add_float(float, float); 6 7 int main(int argc, char *argv[]) 8 { 9 int value = (*argv[1]) - ‘0‘; 10 printf("Leo is here %d . \n", value); (gdb) list 11 12 int num1 = add_int(1, 2); 13 float num2 = add_float(2.3, 4.5); 14 printf("result is %d %f.", num1, num2); 15 16 getchar(); 17 18 return 0; 19 } (gdb) list Line number 20 out of range; main.cpp has 19 lines. (gdb)
也可以指定查看的行数list 5,,10,查看从5到10行的源代码
做好准备工作之后,就可以真正运行程序了
4. 运行程序,run命令,简写r
(gdb) b 8 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb) run 5 可以直接在run后面可以跟运行参数,也可以用set设置运行参数 Starting program: /home/leo/test/project/classes/test 5 Breakpoint 1, main (argc=2, argv=0x7fffffffe018) at main.cpp:8 8 { (gdb)
5. 查看变量,display命令。在使用断点命令后,在运行到断点处,就可以使用此命令查看变量得值了
(gdb) b 8 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb) run 5 Starting program: /home/leo/test/project/classes/test 5 Breakpoint 1, main (argc=2, argv=0x7fffffffe018) at main.cpp:8 8 { (gdb) display argc 查看运行参数个数 1: argc = 2 (gdb) display argv[1] 查看第一个运行参数的值 2: argv[1] = 0x7fffffffe38b "5" (gdb)
6. 继续执行,continue命令,简写c
(gdb) b 8 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb) run 5 Starting program: /home/leo/test/project/classes/test 5 Breakpoint 1, main (argc=2, argv=0x7fffffffe018) at main.cpp:8 8 { (gdb) c Continuing. Leo is here 5 . result is 3 6.800000.
[Inferior 1 (process 4009) exited normally]
7. 修改变量的值,set命令
(gdb) b 8 Breakpoint 1 at 0x4004f0: file main.cpp, line 8. (gdb) run 7 Starting program: /home/leo/test/project/classes/test 7 Breakpoint 1, main (argc=2, argv=0x7fffffffe018) at main.cpp:8 8 { (gdb) display argv[1] 1: argv[1] = 0x7fffffffe38b "7" (gdb) set argv[1] = "8" 将第一个运行参数的值从字符串“7”改为“8” (gdb) c Continuing. Leo is here 8 . result is 3 6.800000.
[Inferior 1 (process 4009) exited normally]
8. 退出调试,q命令
(gdb) q A debugging session is active. Inferior 1 [process 3997] will be killed. Quit anyway? (y or n) y
9. 查看变量类型,whatis命令
(gdb) whatis num1 type = int (gdb)
10. 查看结构详细定义,ptype命令
(gdb) ptype u type = struct User { int id char name[20] }
10. 进入函数体(step,简写s)和单步调试命令(next,简写n)
(gdb) s 6 }2: num2 = 2 1: num1 = 1 (gdb) n main (argc=<optimized out>, argv=<optimized out>) at main.cpp:13 13 float num2 = add_float(2.3, 4.5); (gdb)
11. 查看调用堆栈(backtrace,简写bt)
(gdb) bt #0 add_int ([email protected]=1, [email protected]=2) at math/add_int.cpp:6 #1 0x000000000040051b in main (argc=<optimized out>, argv=<optimized out>) at main.cpp:12 (gdb)
12. 获取当前断点下的运行情况(info)
13. 多线程调试
info thread: 获取当前进程中所有线程信息;
thread thread_id: 进入指定的线程进行调试;
14. 打印指定函数的汇编代码
disassamble sum
15. 帮助信息
help