一个调试示例源程序: 代码:1 #include 2 int func(int n) 4{5 int sum=0,i; 6 for(i=0; i7{8 sum+=i; 9} 10 return sum; 11 } 12 13 14 main() 15 { 16 int i; 17 long result = 0; 18 for(i=1; i <=100; i++) 19{ 20 result += i; 21} 22 23 printf("result[1-100] = %d ", result ); 24 printf("result[1-250] = %d ", func(250) ); 25 } 编译生成执行文件:( Linux 下) hchen/test> cc -g -o tst 使用 GDB 调试: 代码: hchen/test> gdb tst <---------- 启动 GDB GNU gdb Copyright 2002 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are e 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 23 int func(int n) 4{5 int sum=0,i; 6 for(i=0; i7{8 sum+=i; 9} 10 return sum; (gdb) <-------------------- 直接回车表示,重复上一次命令 11 } 12 13 14 main() 15 { 16 int i; 17 long result = 0; 18 for(i=1; i <=100; i++) 19 { 20 result += i; (gdb) break 16 <-------------------- 设置断点,在源程序第 16 行处。 Breakpoint 1 at 0x8048496: file , line 16. (gdb) break func <-------------------- 设置断点,在函数 func() 入口处。 Breakpoint 2 at 0x8048456: file , line 5. (gdb) info break <-------------------- 查看断点信息。 Num Type Disp Enb Address What 1 breakpoint keep y 0x08048496 in main at :16 2 breakpoint keep y 0x08048456 in func at :5 (gdb) r <--------------------- 运行程序, run 命令简写 Starting program: /home/hchen/test/tst Breakpoint 1, main () at :17 <---------- 在断点处停住。 17 long result = 0; (gdb) n <--------------------- 单条语句执行, next 命令简写。 18 for(i=1; i <=100; i++) (gdb) n 20 result += i; (gdb) n 18 for(i=1; i <=100; i++) (gdb) n 20 result += i; (gdb) c <--------------------- 继续运行程序, continue 命令简写。 Continuing. result[1-100] = 5050 <---------- 程序输出。 Breakpoint 2, func (n=250) at :5 5 int sum=0,i; (gdb) n6 for(i=1; i <=n; i++) (gdb) pi <--------------
gdb调试工具使用方法 来自淘豆网m.daumloan.com转载请标明出处.