GDB 调试手册

本文详细介绍了如何使用GDB调试器进行程序调试,包括编译时加入调试符号、运行程序、查看程序执行过程、使用‘调用栈’、使用breakpoint和watchpoints等高级特性,以及如何使用GDB进行内存检查和寄存器查看。

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

gdb 调试手册

1,使用gdb 调试器

1.1,编译时加入调试符号

gcc -g program.c -o programname

1.2, 用gdb调试器运行程序

gdb programname
(gdb) run arg1 arg2 ...  //带参运行

1.3,在gdb调试器中重新运行

(gdb) kill //杀死运行中的程序
Kill the program being debugged? (y or n) y
(gdb) run ...

1.4, 退出调试器

(gdb)quit

1.5,  获得调试器帮助命令

(gdb) help //help  可跟 类名,具体的命令名

2,查看程序执行过程

2.1,停止执行

组合键Ctr+C停止执行。

2.2,继续执行

(gdb) continue

2.3,  查看当前停止的位置

(gdb) list

2.4, 怎么一步一步或一行一行的执行  (nexti,stepi表示指令级别)

(gdb) next   // 执行下一行, 会跳过函数
(gdb) step //执行下一步(下一条指令),会进入函数内部执行

2.5, 怎么检查变量

(gdb) print x
$1 = 900
(gdb) print s
$3 = 0x8048470 "Hello World!\n"
(gdb)

2.6,修改变量

(gdb) set x=3
(gdb) p x
$4=3

2.7,调用链入到你程序中的函数(包含自已定义的函数,和标准库中的函数)

(gdb) call abort() //调用abort()函数产生core文件

2.8,从函数中返回

(gdb) finish

3,使用 “调用栈”

3.1, 获取调用栈

(gdb) backtrace
#0  func2 (x=30) at test.c:5
#1  0x80483e6 in func1 (a=30) at test.c:10
#2  0x8048414 in main (argc=1, argv=0xbffffaf4) at test.c:19
#3  0x40037f5c in __libc_start_main () from /lib/libc.so.6
(gdb) 

3.2, 改变栈帧

(gdb) frame 2 //指定帧号
#2  0x8048414 in main (argc=1, argv=0xbffffaf4) at test.c:19
19        x = func1(x);
(gdb) 

3.3,检查栈 帧

(gdb) info frame //显示当前 栈帧的信息
Stack level 2, frame at 0xbffffa8c:
eip = 0x8048414 in main (test.c:19); saved eip 0x40037f5c
called by frame at 0xbffffac8, caller of frame at 0xbffffa5c
source language c.
Arglist at 0xbffffa8c, args: argc=1, argv=0xbffffaf4
Locals at 0xbffffa8c, Previous frame's sp is 0x0
Saved registers:
 ebp at 0xbffffa8c, eip at 0xbffffa90


(gdb) info locals //显示当前栈帧中局部变量
x = 30
s = 0x8048484 "Hello World!\n"


(gdb) info args //显示当前栈帧的入口参数
argc = 1
argv = (char **) 0xbffffaf4

4,使用 breakpoint

4.1,设置断点

(gdb) break 19 //指定行设置断点
Breakpoint 1 at 0x80483f8: file test.c, line 19
(gdb) break test.c:19 //多文件时,指定源文件的指定行设置 断点
Breakpoint 2 at 0x80483f8: file test.c, line 19  
(gdb) break func1 //指定C函数处设置断点
Breakpoint 3 at 0x80483ca: file test.c, line 10
(gdb) break TestClass::testFunc(int) //指定C++函数处设置 断点
Breakpoint 1 at 0x80485b2: file cpptest.cpp, line 16.

4.2,设置临时断点

tbreak

4.3, 获取所有断点

(gdb) info breakpoints
Num Type           Disp Enb Address    What
2   breakpoint     keep y   0x080483c3 in func2 at test.c:5
3   breakpoint     keep y   0x080483da in func1 at test.c:10

4.4,  关闭断点

(gdb) disable 2 //关闭指定的断点
(gdb) info breakpoints
Num Type           Disp Enb Address    What
2   breakpoint     keep n   0x080483c3 in func2 at test.c:5
3   breakpoint     keep y   0x080483da in func1 at test.c:10

4.5,跳过断点

(gdb) ignore 2 5
Will ignore next 5 crossings of breakpoint 2.

5,使用 watchpoints

类似于breakpoint,但是只指对变量查看。


6, gdb高级特性

6.1,检查内存

使用x命令,语法:x/格式  地址
char *s = "Hello World\n", some uses of the x command could be:
(gdb) x/s s //作为字符串查看
0x8048434 <_IO_stdin_used+4>:    "Hello World\n"
(gdb) x/c s //作为字符查看
0x8048434 <_IO_stdin_used+4>:   72 'H'
(gdb) x/4c s //查看前4个字符
0x8048434 <_IO_stdin_used+4>:   72 'H'  101 'e' 108 'l' 108 'l'
(gdb) x/t s //检查前32位
0x8048434 <_IO_stdin_used+4>:   01101100011011000110010101001000
(gdb) x/3x s //检查前24字节,显示为16进制
0x8048434 <_IO_stdin_used+4>:   0x6c6c6548      0x6f57206f      0x0a646c72

6.2,查看寄存器

(gdb) info registers

6.3,反汇编

(gdb)disassemble main

6.4,调试core文件

prompt > gdb myprogram
...
(gdb) core core
...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值