断点调试:
- #include <stdio.h>
- int main()
- {
- int sum = 0, i = 0;
- char input[5];
- while(1)
- {
- sum = 0;
- scanf("%s",input);
- for(i=0; input[i]!='\0'; ++i)
- sum = sum*10 + input[i] - '0';
- printf("input=%d\n", sum);
- }
- return 0;
- }
这个程序就是把字符串转换成数值,编译运行程序后:
- $ gcc atoi.c -g -o atoi
- $ ./atoi
- 123
- input=123
- 234
- input=123234
第一次结果正确,但是第二次并不正确,还是用gdb来调试。
下面是gdb调试的过程
- [tym@lxy gdb]$ gdb atoi
- GNU gdb Fedora (6.8-37.el5)
- Copyright (C) 2008 Free Software Foundation, Inc.
- License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
- This is free software: you are free to change and redistribute it.
- There is NO WARRANTY, to the extent permitted by law. Type "show copying"
- and "show warranty" for details.
- This GDB was configured as "i386-redhat-linux-gnu"...
- (gdb) l
- 1 #include <stdio.h>
- 2
- 3 int main()
- 4 {
- 5 int sum = 0, i = 0;
- 6 char input[5];
- 7
- 8 while(1)
- 9 {
- 10 scanf("%s",input);
- (gdb) l
- 11 for(i=0; input[i]!='\0'; ++i)
- 12 sum = sum*10 + input[i] - '0';
- 13 printf("input=%d\n", sum);
- 14 }
- 15 return 0;
- 16 }
- (gdb) b 10
- Breakpoint 1 at 0x80483d3: file atoi.c, line 10.
- (gdb) b 12
- Breakpoint 2 at 0x80483ef: file atoi.c, line 12.
- (gdb) run
- Starting program: /home/tym/source/gdb/atoi
- Breakpoint 1, main () at atoi.c:10
- 10 scanf("%s",input);
- (gdb) i locals
- sum = 0
- i = 0
- input = "S"
- (gdb) n
- 123
- 11 for(i=0; input[i]!='\0'; ++i)
- (gdb) n
- Breakpoint 2, main () at atoi.c:12
- 12 sum = sum*10 + input[i] - '0';
- (gdb) n
- 11 for(i=0; input[i]!='\0'; ++i)
- (gdb) n
- Breakpoint 2, main () at atoi.c:12
- 12 sum = sum*10 + input[i] - '0';
- (gdb) n
- 11 for(i=0; input[i]!='\0'; ++i)
- (gdb) i locals
- sum = 1
- i = 0
- input = "123\000"
- (gdb) n
- Breakpoint 2, main () at atoi.c:12
- 12 sum = sum*10 + input[i] - '0';
- (gdb) i locals
- sum = 1
- i = 1
- input = "123\000"
- (gdb) n
- 11 for(i=0; input[i]!='\0'; ++i)
- (gdb) n
- Breakpoint 2, main () at atoi.c:12
- 12 sum = sum*10 + input[i] - '0';
- (gdb) i locals
- sum = 12
- i = 2
- input = "123\000"
- (gdb) n
- 11 for(i=0; input[i]!='\0'; ++i)
- (gdb) n
- 13 printf("input=%d\n", sum);
- (gdb) locals
- Undefined command: "locals". Try "help".
- (gdb) i locals
- sum = 123
- i = 3
- input = "123\000"
- (gdb) n
- input=123
- 14 }
- (gdb) n
- Breakpoint 1, main () at atoi.c:10
- 10 scanf("%s",input);
- (gdb) n
- 234
- 11 for(i=0; input[i]!='\0'; ++i)
- (gdb) n
- Breakpoint 2, main () at atoi.c:12
- 12 sum = sum*10 + input[i] - '0';
- (gdb) i locals
- sum = 123
- i = 0
- input = "234\000"
在123行你看出来在第二次输入input时,你看到sum的值并不是空的,而是上次结果,所以程序的问题就暴露出来了。
下面介绍本章的命令及别的命令:
命令 |
描述 |
break(或b) 行号 |
在某一行设置断点 |
break 函数名 |
在某个函数开头设置断点 |
break ... if ... |
设置条件断点 |
continue(或c) |
从当前位置开始连续运行程序 |
delete breakpoints 断点号 |
删除断点 |
display 变量名 |
跟踪查看某个变量,每次停下来都显示它的值 |
disable breakpoints 断点号 |
禁用断点 |
enable 断点号 |
启用断点 |
info(或i) breakpoints |
查看当前设置了哪些断点 |
run(或r) |
从头开始连续运行程序 |
undisplay 跟踪显示号 |
取消跟踪显示 |
我们还应该记得,我们使用gdb进入gdb的命令提示符,如果想使用shell命令,我们应该在gdb的命令提示符下输入shell shell_command
阅读(899) | 评论(0) | 转发(1) |