gdb 调试手册
(gdb) run arg1 arg2 ... //带参运行
Kill the program being debugged? (y or n) y
(gdb) run ...
(gdb) step //执行下一步(下一条指令),会进入函数内部执行
$1 = 900
(gdb) print s
$3 = 0x8048470 "Hello World!\n"
(gdb)
(gdb) p x
$4=3
#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)
#2 0x8048414 in main (argc=1, argv=0xbffffaf4) at test.c:19
19 x = func1(x);
(gdb)
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
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.
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
(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
Will ignore next 5 crossings of breakpoint 2.
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
...
(gdb) core core
...
1,使用gdb 调试器
1.1,编译时加入调试符号
gcc -g program.c -o programname1.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)quit1.5, 获得调试器帮助命令
(gdb) help //help 可跟 类名,具体的命令名2,查看程序执行过程
2.1,停止执行
组合键Ctr+C停止执行。2.2,继续执行
(gdb) continue2.3, 查看当前停止的位置
(gdb) list2.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) finish3,使用 “调用栈”
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,设置临时断点
tbreak4.3, 获取所有断点
(gdb) info breakpointsNum 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 5Will 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 registers6.3,反汇编
(gdb)disassemble main6.4,调试core文件
prompt > gdb myprogram...
(gdb) core core
...