CentOS上GDB的安装使用

一,安装GDB

[root@vmhostd ~]# rpm -q gdb

package gdb is not installed
[root@vmhostd ~]# yum install gdb
.....

Installed:
  gdb.i686 0:7.2-75.el6                                                                                                                  

Complete!
[root@vmhostd ~]# rpm -q gdb
gdb-7.2-75.el6.i686

[root@vmhostd ~]# 


二,编写编译C程序

[root@vmhostd ~]# vi test.c
[root@vmhostd ~]# cat test.c
#include<stdio.h>


int add(int a, int b)
{
printf("a=%d,b=%d\n",a,b);
return a+b;
}


int main()
{
int a=1;
int b=2;
int res = add(a,b);
printf("res:%d\n",res);
}
[root@vmhostd ~]# 

[root@vmhostd ~]# gcc test.c -o test -g


三,调试程序

[root@vmhostd ~]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-75.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
...
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) file test //加载程序文件
Reading symbols from /root/test...done.
(gdb) b add //设置断点
Breakpoint 1 at 0x80483ca: file test.c, line 5.
(gdb) b main
Breakpoint 2 at 0x80483f9: file test.c, line 11.
(gdb) r //运行程序
Starting program: /root/test 


Breakpoint 2, main () at test.c:11
11 int a=1;
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686
(gdb) p a //查看变量
$1 = 134513424
(gdb) s
12 int b=2;
(gdb) p a
$2 = 1
(gdb) c //继续运行直至下一断点
Continuing.


Breakpoint 1, add (a=1, b=2) at test.c:5
5 printf("a=%d,b=%d\n",a,b);
(gdb) p a
$3 = 1
(gdb) s //单步执行
a=1,b=2
6 return a+b;
(gdb) s
7 }
(gdb) s
main () at test.c:14
14 printf("res:%d\n",res);
(gdb) p res
$4 = 3
(gdb) c
Continuing.
res:3


Program exited normally.
(gdb) 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值