linux环境下调试代码

//创建文件test.c
[admin@bogon code]$ touch test.c
[admin@bogon code]$ ls
test.c
//编辑test.c
[admin@bogon code]$ vim test.c

test.c文件

  1 #include<stdio.h>
  2 
  3 int Sum(int x, int y)
  4 {
  5     return x + y;
  6 }
  7 
  8 int main()
  9 {   
 10     int a = 3;
 11     int b = 4;
 12     int c = Sum(a, b);
 13     printf("Sum = %d\n",c);
 14     return 0;
 15 }

//编译文件 
// -o选项  输入到哪个文件
//-g选项  必须加,表示为debug版本
[admin@bogon code]$ gcc -o test -g test.c
[admin@bogon code]$ ls
test  test.c
[admin@bogon code]$ gdb
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-92.el6)
Copyright (C) 2010 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 "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
//要调试的文件的文字,用关键字file
(gdb) file test
Reading symbols from /home/admin/code/test...done.
//显示代码,默认显示10行
(gdb) l
1	#include<stdio.h>
2	
3	int Sum(int x, int y)
4	{
5		return x + y;
6	}
7	
8	int main()
9	{
10		int a = 3;
(gdb) l
11		int b = 4;
12		int c = Sum(a, b);
13		printf("Sum = %d\n",c);
14		return 0;
15	}
//打断点,b后面跟行号
gdb) b 12
Breakpoint 1 at 0x80483eb: file test.c, line 12.
(gdb) b 5
Breakpoint 2 at 0x80483c7: file test.c, line 5.
//显示断点信息
(gdb) info b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x080483eb in main at test.c:12
2       breakpoint     keep y   0x080483c7 in Sum at test.c:5
//运行代码
(gdb) r
Starting program: /home/admin/code/test 

Breakpoint 1, main () at test.c:12
//提示到达第一个断点处
12		int c = Sum(a, b);
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.149.el6.i686
//进入函数体
(gdb) s

Breakpoint 2, Sum (x=3, y=4) at test.c:5
5		return x + y;
//单步执行,逐过程,类似VS 的F10
(gdb) n
6	}
//逐语句,类似于VS的F11
(gdb) s
main () at test.c:13
13		printf("Sum = %d\n",c);
//p类似于VS的监视
(gdb) p c
$1 = 7
(gdb) p &c
$2 = (int *) 0xbffff3ac
//显示特定监视
(gdb) display c
1: c = 7
(gdb) n
Sum = 7
14		return 0;
1: c = 7
//移除特定监视
(gdb) undisplay 1
//结束调试
gdb) quit
[admin@bogon code]$ 
(gdb) info b
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x080483eb in main at test.c:12
2       breakpoint     keep y   0x080483c7 in Sum at test.c:5
//删除断点
(gdb) d 1
(gdb) info b
Num     Type           Disp Enb Address    What
2       breakpoint     keep y   0x080483c7 in Sum at test.c:5

//调到某一行
gdb) until 14
Sum = 7
main () at test.c:14
14		return 0;
(gdb) 

总结:
gdb:调试程序
file:调试哪个程序
b:设置断点
info:显示断点
d:删除断点
r:运行程序
n:逐过程执行代码
s:逐语句执行代码
bt:打印出函数调用栈(重要)在程序出错时使用
p:查看监视信息
display:显示监视信息
undisplay:不显示监视信息
finsish:执行到当前函数返回
c:从当前位置开始连续而非单步执行程序
until:执行程序调到某一行
qiut:结束调试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值