[GDB] GDB的快速入门指南:安装、启动、断点、单步调试、恢
2018-09-10 本文已影响165人
AkuRinbu
目录
hello.c
gcc 编译
安装GDB
启动GDB
断点
设置断点
查看断点
清除断点
启用与禁用断点
单步调试
恢复执行
查看变量
hello.c
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
gcc 编译
- 在用
gcc
编译代码的时候,需要开启-g
选项,提供调试用的信息; -
gdb
就是根据这些信息,来进行调试的;
anno@anno-m:~/Desktop$ ls
hello.c
anno@anno-m:~/Desktop$ gcc -g hello.c -o hello
anno@anno-m:~/Desktop$ ls
hello hello.c
安装GDB
$ sudo apt-get update
$ sudo apt-get install gdb
$ gdb -version
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.3) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
启动GDB
anno@anno-m:~/Desktop$ gdb hello
(gdb) run
Starting program: /home/anno/Desktop/hello
Hello, world!
[Inferior 1 (process 2978) exited normally]
-
TUI模式,按组合键
Ctrl+X+A
切换
断点
设置断点
-
break 命令设置断点,简写
b
; - break main ,在main()函数的入口处设置断点;
- break 5,在源代码的第5行设置断点;
- break hello.c:5 ,指定源码文件的代码第5行设置断点;
anno@anno-m:~/Desktop$ gdb hello
(gdb) b main
Breakpoint 1 at 0x400531: file hello.c, line 5.
(gdb) r
Starting program: /home/anno/Desktop/hello
Breakpoint 1, main () at hello.c:5
5 printf("Hello, world!\n");
(gdb)
查看断点
-
info breakpoints,显示当前全部的断点,简写
i b
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400531 in main at hello.c:5
breakpoint already hit 1 time
清除断点
-
delete + 断点的数值标识符,
delete 1
,删除第1个断点;
(gdb) b main
Breakpoint 1 at 0x400531: file hello.c, line 5.
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400531 in main at hello.c:5
(gdb) delete 1
(gdb) i b
No breakpoints or watchpoints.
-
clear + 函数名 、 +行号、+文件名:行号 ,清除断点main()函数处的断点:
clear main
或者clear 5
(本质是main函数的第一条语句所在);
(gdb) b main
Breakpoint 1 at 0x400531: file hello.c, line 5.
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400531 in main at hello.c:5
(gdb) clear main
Deleted breakpoint 1
(gdb) i b
No breakpoints or watchpoints.
(gdb)
启用与禁用断点
-
disable + 断点的数值标识符,
disable 1
:禁用第1个断点; -
enable + 断点的数值标识符,
enable 1
:启用第1个断点; -
Enb字段,表明断点是
禁用(n)
还是启用(y)
的;
(gdb) b main
Breakpoint 1 at 0x400531: file hello.c, line 5.
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400531 in main at hello.c:5
(gdb) disable 1
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep n 0x0000000000400531 in main at hello.c:5
(gdb) enable 1
(gdb) i b
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400531 in main at hello.c:5
单步调试
- next,n,越过 函数调用(函数会在背地里自己悄悄运行完),单步执行;
- step,s,进入 函数体内部,单步执行;
(gdb) r
Starting program: /home/anno/Desktop/hello
Breakpoint 1, main () at hello.c:5
5 printf("Hello, world!\n");
(gdb) n
Hello, world!
7 return 0;
(gdb) n
8 }
(gdb) n
__libc_start_main (main=0x40052d <main>, argc=1, argv=0x7fffffffdf38,
init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>,
stack_end=0x7fffffffdf28) at libc-start.c:321
321 libc-start.c: No such file or directory.
(gdb) n
[Inferior 1 (process 3984) exited normally]
恢复执行
- continue,c,恢复执行,直到遇到下一个断点;
- continue命令执行期间,按下CTRL-C瞬间停止;
查看变量
[GDB]检查变量:print、disp、call
https://www.jianshu.com/p/79671588f6d8
- disp,使得每次有暂停,都会输出指定的变量的值;
- print,p,只显示一次变量的值;
- 要求变量名在当前的域是可见的,比如某个变量
i
是函数foo()
的局部变量
,那么只有是在进入到这个函数的里面时才可以使用print i 或者 disp i
,不然gdb
也不知道i
是谁;
更多功能
一、TUI模式,双开 汇编代码 窗口
- 1、Ctrl + X + A 进入TUI模式;
- 2、
(gdb) list
:显示10行C
源码; - 3、
(gdb) layout split
:同时显示C源码
以及汇编源码
; - 4、
(gdb) info registers
:显示使用到的寄存器信息;
- 5、
(gdb) set disassembly-flavor intel
:改变显示的汇编语法;
set disassembly-flavor intel
set disassembly-flavor att
- 6、再次输入
(gdb) layout split
,使语法改变生效;
显示Intel语法的汇编