一段c语言程序:

#include <stdio.h>
int main(){
    int num = 0;
    scanf("%d", &num);
    printf("%d", num);
    return 0;
}

1. 查看已建好断点

1.1 语法

(gdb) info breakpoint [n]
(gdb) info break [n]

参数 n 作为可选参数,为某个断点的编号,表示查看指定断点而非全部断点。

1.2 示例

[root@localhost day6]# gdb test2 -q
Reading symbols from /home/gdb/day6/test2...done.
(gdb) l 0
1	#include <stdio.h>
2	int main(){
3	    int num = 0;
4	    scanf("%d", &num);
5	    printf("%d", num);
6	    return 0;
7	}
(gdb) 
Line number 8 out of range; test2.c has 7 lines.
(gdb) b 1
Breakpoint 1 at 0x40057a: file test2.c, line 1.
(gdb) r
Starting program: /home/gdb/day6/test2 

Breakpoint 1, main () at test2.c:3
3	    int num = 0;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
(gdb) watch num
Hardware watchpoint 2: num
(gdb) catch throw
Function "__cxa_throw" not defined.
Catchpoint 3 (throw)
(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040057a in main at test2.c:1
	breakpoint already hit 1 time
2       hw watchpoint  keep y                      num
3       breakpoint     keep y   <PENDING>          exception throw
(gdb) info breakpoints 1
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040057a in main at test2.c:1
	breakpoint already hit 1 time
(gdb) info watchpoints 
Num     Type           Disp Enb Address            What
2       hw watchpoint  keep y                      num
(gdb) info watchpoints 2
Num     Type           Disp Enb Address            What
2       hw watchpoint  keep y   

以上输出信息中各列的含义分别是:断点编号(Num)、断点类型(Type)、是临时断点还是永久断点(Disp)、目前是启用状态还是禁用状态(Enb)、断点的位置(Address)、断点当前的状态(作用的行号、已经命中的次数等,用 What 列表示)。

2. 删除断点

2.1 clear

2.1.1 功能

删除指定位置处的所有断点。

2.1.2 语法

(gdb) clear location

参数 location 通常为某一行代码的行号或者某个具体的函数名。当 location 参数为某个函数的函数名时,表示删除位于该函数入口处的所有断点。

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040057a in main at test2.c:1
	breakpoint already hit 1 time
2       hw watchpoint  keep y                      num
3       breakpoint     keep y   <PENDING>          exception throw
(gdb) clear 1
Deleted breakpoint 1    <---------删除位于第一行的断点
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
2       hw watchpoint  keep y                      num
3       breakpoint     keep y   <PENDING>          exception throw
(gdb) 

2.2 delete

2.2.1 功能

通常用来删除所有断点,也可以删除指定编号的各类型断点。

2.2.2 语法

delete [breakpoints] [num]

其中,breakpoints 参数可有可无,num 参数为指定断点的编号,其可以是 delete 删除某一个断点,而非全部。

2.2.3 示例

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
2       hw watchpoint  keep y                      num
3       breakpoint     keep y   <PENDING>          exception throw
4       breakpoint     keep y   0x000000000040057a in main at test2.c:1
(gdb) delete 2
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
3       breakpoint     keep y   <PENDING>          exception throw
4       breakpoint     keep y   0x000000000040057a in main at test2.c:1
(gdb) delete 
Delete all breakpoints? (y or n) y
(gdb) info breakpoints
No breakpoints or watchpoints.
(gdb) 

3 禁用断点

3.1 功能

使目标断点暂时失去作用,必要时可以再将其激活,恢复断点原有的功能。

3.2 语法

disable [breakpoints] [num...]

breakpoints 参数可有可无;num... 表示可以有多个参数,每个参数都为要禁用断点的编号。如果指定 num...,disable 命令会禁用指定编号的断点;反之若不设定 num...,则 disable 会禁用当前程序中所有的断点。

3.3 示例

(gdb) disable delete 5 
warning: bad breakpoint number at or near 'delete 5'
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
5       breakpoint     keep n   0x000000000040057a in main at test2.c:1
6       breakpoint     keep y   0x000000000040057a in main at test2.c:3
7       breakpoint     keep y   0x0000000000400581 in main at test2.c:4
(gdb) enable delete 5
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
5       breakpoint     del  y   0x000000000040057a in main at test2.c:1
6       breakpoint     keep y   0x000000000040057a in main at test2.c:3
7       breakpoint     keep y   0x0000000000400581 in main at test2.c:4
(gdb) 
Logo

更多推荐