本文主要是介绍《软件调试的艺术》笔记--停下来环顾程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.断点列表
创建的每个断点(包括断电、监视点和捕获点)都标识为从1开始的唯一整数标识符。这个标识符用来执行该断点上的各种
操作。调试器还包含一种列出所有断点及其属性的方法。
调试下面的代码:(代码1)
#include <stdio.h>void display(int i)
{i = i + 1;printf("i = %d\n",i);
}int main(void)
{int i = 1;display(i);return 0;
}
设置断点--显示断点列表--删除断点--显示删除后断点列表:
(gdb) break main
Breakpoint 1 at 0x80483d4: file a.c, line 11.
(gdb) break display
Breakpoint 2 at 0x80483aa: file a.c, line 5.
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x080483d4 in main at a.c:11
2 breakpoint keep y 0x080483aa in display at a.c:5
(gdb) delete 1
(gdb) info breakpoints
Num Type Disp Enb Address What
2 breakpoint keep y 0x080483aa in display at a.c:5
(gdb)
info breakpoints命令的简写为i b
2.设置断点
Breakpoint 1 at 0x80483d4: file a.c, line 11.
Breakpoint 3 at 0x80483d4: file a.c, line 11.
Note: breakpoint 3 also set at pc 0x80483d4.
Breakpoint 4 at 0x80483d4: file a.c, line 11.
Breakpoint 6 at 0x80483d4: file a.c, line 11.
2.断点的持久性
Breakpoint 1 at 0x80483d4: file a.c, line 11.
11 int i = 1;
12 display(i);
(gdb) s
display (i=1) at a.c:5
5 i = i + 2;
3.删除断点
Num Type Disp Enb Address What
4 breakpoint keep y 0x080483d4 in main at a.c:11
5 breakpoint keep y 0x080483aa in display at a.c:5
(gdb) delete 4
(gdb) info breakpoints
Num Type Disp Enb Address What
5 breakpoint keep y 0x080483aa in display at a.c:5
(gdb)
Num Type Disp Enb Address What
5 breakpoint keep y 0x080483aa in display at a.c:5
6 breakpoint keep y 0x080483d4 in main at a.c:11
(gdb) delete
Delete all breakpoints? (y or n) y
(gdb) info breakpoints
No breakpoints or watchpoints.
(gdb)
Num Type Disp Enb Address What
17 breakpoint dis y 0x080483d4 in main at a.c:11
19 breakpoint keep y 0x080483aa in display at a.c:5
(gdb) r
Starting program: /root/a.out
Breakpoint 17, main () at a.c:11
11 int i = 1;
(gdb) clear
Deleted breakpoint 17
(gdb) info breakpoints
Num Type Disp Enb Address What
19 breakpoint keep y 0x080483aa in display at a.c:5
Num Type Disp Enb Address What
14 breakpoint keep y 0x080483d4 in main at a.c:11
15 breakpoint keep y 0x080483d4 in main at a.c:11
16 breakpoint keep y 0x080483d4 in main at a.c:11
(gdb) clear main
Deleted breakpoints 14 15 16
(gdb) info breakpoints
No breakpoints or watchpoints.
(gdb)
4.禁用断点
Num Type Disp Enb Address What
17 breakpoint keep y 0x080483d4 in main at a.c:11
18 breakpoint keep y 0x080483aa in display at a.c:5
(gdb) disable 17
(gdb) info breakpoints
Num Type Disp Enb Address What
17 breakpoint keep n 0x080483d4 in main at a.c:11
18 breakpoint keep y 0x080483aa in display at a.c:5
(gdb) enable 17
(gdb) info breakpoints
Num Type Disp Enb Address What
17 breakpoint keep y 0x080483d4 in main at a.c:11
18 breakpoint keep y 0x080483aa in display at a.c:5
(gdb)
#include <stdio.h>int main(void)
{int m = 1;int i;for (i=0; i<10; i++) {printf("%d",i);}return 0;
}
(gdb) b 8 Breakpoint 2 at 0x80483c5: file b.c, line 8.
(gdb) info breakpoints
Num Type Disp Enb Address What
2 breakpoint keep y 0x080483c5 in main at b.c:8
(gdb) disable 2
(gdb) enable once 2
(gdb) info breakpoints
Num Type Disp Enb Address What
2 breakpoint dis y 0x080483c5 in main at b.c:8
(gdb) r
Starting program: /root/a.out
Breakpoint 2, main () at b.c:8
8 printf("%d\n",i);
(gdb) c
Continuing.
0123456789
Program exited normally.
5.断点属性
Num Type Disp Enb Address What
1 breakpoint keep y 0x080483d4 in main at a.c:11
2 breakpoint keep y 0x080483aa in display at a.c:5
6.恢复执行
6.1.finish
Breakpoint 1 at 0x4004ff: file gdb.c, line 5.
(gdb) r
Starting program: /home/yanwenjie/ctest/a.out
Breakpoint 1, display (i=1) at gdb.c:5
5 i = i + 1;
(gdb) finish
Run till exit from #0 display (i=1) at gdb.c:5
i = 2
main () at gdb.c:13
13 return 0;
(gdb)
6.2.until
Breakpoint 1, main () at gdb.c:5
5 int m = 1;
(gdb) n
7 for (i=0; i<10; i++) {
(gdb)
8 printf("%d",i);
(gdb)
7 for (i=0; i<10; i++) {
(gdb) until
10 return 0;
(gdb) n
11 }
7.条件断点
8.断点命令列表
Num Type Disp Enb Address What
1 breakpoint keep y 0x000000000040050c in main at gdb.c:8
(gdb) commands 1
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
>printf "i is %d\n",i
>end
(gdb) r
Breakpoint 1, main () at gdb.c:8
8 printf("%d",i);
i is 0
(gdb) c
Continuing.
Breakpoint 1, main () at gdb.c:8
8 printf("%d",i);
i is 1
9.监视点
#include <stdio.h>int main(void)
{int m = 1;int i;for (i=0; i<10; i++) {if (i%2 ==0) {m++;}}return 0;
}
调试结果如下:
Breakpoint 1 at 0x4004b8: file gdb.c, line 5.
(gdb) r
Breakpoint 1, main () at gdb.c:5
5 int m = 1;
(gdb) watch m
Hardware watchpoint 2: m
(gdb) p m
$1 = 0
(gdb) c
Continuing.
Hardware watchpoint 2: m
Old value = 0
New value = 2
main () at gdb.c:7
7 for (i=0; i<10; i++) {
(gdb) p m
$2 = 2
这篇关于《软件调试的艺术》笔记--停下来环顾程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!