嵌入式 GDB调试示例过程

一个调试示例
——————
源程序:tst.c
 #include <stdio.h>
 int func(int n)
 {
 int sum=0,i;
 for(i=0; i<=n; i++)
 {
 sum+=i;
 }
 return sum;
 }
main()
{
 int i;
 long result = 0;
 for(i=1; i<=100; i++)
 {
 result += i;
 }
 
 printf("result[1-100] = %d \n",result);
 printf("result[1-250] = %d \n",func(250));
 }
编译生成执行文件:(Linux下)
hchen/test> gcc -g tst.c -o tst
使用GDB调试:
hchen/test> gdb tst <---------- 启动GDB
GNU gdb 5.1.1
Copyright 2002 Free Software Foundation,Inc.
GDB is free software,covered by the GNU General Public License,andyou are
welcome to change it and/or distribute copies of it under certainconditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" fordetails.
This GDB was configured as "i386-suse-linux"...
(gdb) l <-------------------- l命令相当于list,从第一行开始列出原码。
 #include <stdio.h>
 int func(int n)
{
 int sum=0,i;
 for(i=0; i<=n; i++)
 {
 sum+=i;
 }
 return sum;
(gdb) <-------------------- 直接回车表示,重复上一次命令
 }
 main()
 {
 int i;
 long result = 0;
 for(i=1; i<=100; i++)
 {
 result += 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 Type Disp Enb Address What
1 breakpoint keep y 0x08048496 in main at tst.c:16
2 breakpoint keep y 0x08048456 in func at tst.c:5
(gdb) r <--------------------- 运行程序,run命令简写
Starting program: /home/hchen/test/tst
Breakpoint 1,main () at tst.c:17 <---------- 在断点处停住。
 long result = 0;
(gdb) n <--------------------- 单条语句执行,next命令简写。
 for(i=1; i<=100; i++)
(gdb) n
 result += i;
(gdb) n
 for(i=1; i<=100; i++)
(gdb) n
 result += i;
(gdb) c <--------------------- 继续运行程序,continue命令简写。
Continuing.
result[1-100] = 5050 <----------程序输出。
Breakpoint 2,func (n=250) at tst.c:5
 int sum=0,i;
(gdb) n
 for(i=1; i<=n; i++)
(gdb) p i <--------------------- 打印变量i的值,print命令简写。
= 134513808
(gdb) n
 sum+=i;
(gdb) n
 for(i=1; i<=n; i++)
(gdb) p sum
= 1
(gdb) n
 sum+=i;
(gdb) p i
= 2
(gdb) n
 for(i=1; i<=n; i++)
(gdb) p sum
= 3
(gdb) bt <--------------------- 查看函数堆栈。
#0 func (n=250) at tst.c:5
#1 0x080484e4 in main () at tst.c:24
#2 0x400409ed in __libc_start_main () from /lib
(gdb) finish <--------------------- 退出函数。
Run till exit from #0 func (n=250) at tst.c:5
0x080484e4 in main () at tst.c:24
 printf("result[1-250] = %d \n",func(250));
Value returned is = 31375
(gdb) c <--------------------- 继续运行。
Continuing.
result[1-250] = 31375 <----------程序输出。
Program exited with code 027. <--------程序退出,调试结束。
(gdb) q <--------------------- 退出gdb。
hchen/test>
…………
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值