gdb 调试程序

要调试生成的可执行程序,必须在生成的时候加入-g选项,生成可以调试的代码

例如:gcc -o test a.c b.c -g

这样gcc就会在链接的时候加入一些用于调试的符号

在生成可以调试的可执行程序后,使用gdb命令进入调试模式

 root@ubuntu:/home/leo/test/project/classes# gdb test
GNU gdb (Ubuntu 7.10-1ubuntu2) 7.10
Copyright (C) Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 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)

此时,程序并没有开始运行,在运行程序之前,可以做一些准备工作

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 at 0x4004f0: file main.cpp, line .
(gdb)

为指定函数设置断点

(gdb) b add_int
Breakpoint at 0x400660: file math/add_int.cpp, line .

在指定行数或函数设置条件变量

(gdb) b add_int if num1 ==
Breakpoint at 0x400660: file math/add_int.cpp, line .

查看断点

(gdb) info breakpoints
Num Type Disp Enb Address What
breakpoint keep y 0x0000000000400660 in add_int(int, int)
at math/add_int.cpp:
stop only if num1 ==
breakpoint already hit time

禁用断点 disable b 4,禁用4号断点

启动断点 enable b 4,启用4号断点

删除断点 clear 51 删除51行的断点

3. 查看源文件,list命令,简写l

(gdb) list
#include "stdlib.h"
#include "stdio.h" int add_int(int, int);
float add_float(float, float); int main(int argc, char *argv[])
{
int value = (*argv[]) - '';
printf("Leo is here %d . \n", value);
(gdb) list int num1 = add_int(, );
float num2 = add_float(2.3, 4.5);
printf("result is %d %f.", num1, num2); getchar(); return ;
}
(gdb) list
Line number out of range; main.cpp has lines.
(gdb)

也可以指定查看的行数list 5,,10,查看从5到10行的源代码

做好准备工作之后,就可以真正运行程序了

4. 运行程序,run命令,简写r

(gdb) b
Breakpoint at 0x4004f0: file main.cpp, line .
(gdb) run 5 可以直接在run后面可以跟运行参数,也可以用set设置运行参数
Starting program: /home/leo/test/project/classes/test Breakpoint , main (argc=, argv=0x7fffffffe018) at main.cpp:
{
(gdb)

5. 查看变量,display命令。在使用断点命令后,在运行到断点处,就可以使用此命令查看变量得值了

(gdb) b
Breakpoint at 0x4004f0: file main.cpp, line .
(gdb) run
Starting program: /home/leo/test/project/classes/test Breakpoint , main (argc=, argv=0x7fffffffe018) at main.cpp:
{
(gdb) display argc 查看运行参数个数
: argc =
(gdb) display argv[] 查看第一个运行参数的值
: argv[] = 0x7fffffffe38b ""
(gdb)

6. 继续执行,continue命令,简写c

(gdb) b
Breakpoint at 0x4004f0: file main.cpp, line .
(gdb) run
Starting program: /home/leo/test/project/classes/test Breakpoint , main (argc=, argv=0x7fffffffe018) at main.cpp:
{
(gdb) c
Continuing.
Leo is here .
result is 6.800000.

[Inferior 1 (process 4009) exited normally]

7. 修改变量的值,set命令

(gdb) b
Breakpoint at 0x4004f0: file main.cpp, line .
(gdb) run
Starting program: /home/leo/test/project/classes/test Breakpoint , main (argc=, argv=0x7fffffffe018) at main.cpp:
{
(gdb) display argv[]
: argv[] = 0x7fffffffe38b ""
(gdb) set argv[] = "" 将第一个运行参数的值从字符串“7”改为“8”
(gdb) c
Continuing.
Leo is here .
result is 6.800000.

[Inferior 1 (process 4009) exited normally]

8. 退出调试,q命令

(gdb) q
A debugging session is active. Inferior [process ] 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[]
}

10. 进入函数体(step,简写s)和单步调试命令(next,简写n)

(gdb) s
}: num2 =
: num1 =
(gdb) n
main (argc=<optimized out>, argv=<optimized out>) at main.cpp:
float num2 = add_float(2.3, 4.5);
(gdb)

11. 查看调用堆栈(backtrace,简写bt)

(gdb) bt
# add_int (num1=num1@entry=, num2=num2@entry=) at math/add_int.cpp:
# 0x000000000040051b in main (argc=<optimized out>, argv=<optimized out>)
at main.cpp:
(gdb)

12. 获取当前断点下的运行情况(info)

13.  多线程调试

  info thread: 获取当前进程中所有线程信息;

  thread thread_id: 进入指定的线程进行调试;

14. 打印指定函数的汇编代码

  disassamble sum

15.  帮助信息

  help

上一篇:orcale设置自增列


下一篇:typedef的使用1——引入