天天看點

linux進階21——GDB(七):禁用和删除斷點1. 檢視已建好斷點2. 删除斷點3 禁用斷點

一段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 示例

[[email protected] 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) 
           

繼續閱讀