一个调试示例
——————
源程序:tst.c
1 #include <stdio.h>
2
3 int func(int n)
4 {
5int sum=0,i;
6for(i=0; i<n; i)
7{
8sum=i;
9}
10return sum;
11 }
12
13
14 main()
15 {
16int i;
17long result = 0;
18for(i=1; i<=100; i)
19{
20result = i;
21}
22
23printf("result[1-100] = %d /n", result );
24printf("result[1-250] = %d /n", func(250) );
25 }
编译生成执行文件:(Linux下)
hchen/test> cc -g tst.c -o tst
使用GDB调试:
hchen/test> gdb tst<----------启动GDB
GNU gdb 5.1.1
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.Type "show warranty" for details.
This GDB was configured as "i386-suse-linux"...
(gdb) l<-------------------- l命令相当于list,从第一行开始例出原码。
1#include <stdio.h>
2
3int func(int n)
4{
5int sum=0,i;
6for(i=0; i<n; i)
7{
8sum=i;
9}
10return sum;
(gdb)<--------------------直接回车表示,重复上一次命令
11}
12
13
14main()
15{
16int i;
17long result = 0;
18for(i=1; i<=100; i)
19{
20result = i;
(gdb) break 16<--------------------设置断点,在源程序第16行处。
Breakpoint 1 at 0x8048496: file tst.c, line 16.
(gdb) break func<--------------------设置断点,在函数func()入口处。
Breakpoint 2 at 0x8048456: file tst.c, line 5.
(gdb) info break<--------------------查看断点信息。
Num TypeDisp Enb AddressWhat
1breakpointkeep y0x08048496 in main at tst.c:16
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-40593-1.html