GDB调试带参数的程序(转载+整理+实践)

GDB调试带参数的程序(转载+整理+实践)


http://www.cnblogs.com/hankers/archive/2012/12/07/2806836.html

一.gdb常用命令:

命令 描述
backtrace(或bt) 查看各级函数调用及参数
finish 连续运行到当前函数返回为止,然后停下来等待命令
frame(或f) 帧编号 选择栈帧
info(或i) locals 查看当前栈帧局部变量的值
list(或l) 列出源代码,接着上次的位置往下列,每次列10行
list 行号 列出从第几行开始的源代码
list 函数名 列出某个函数的源代码
next(或n) 执行下一行语句
print(或p) 打印表达式的值,通过表达式可以修改变量的值或者调用函数
quit(或q) 退出gdb调试环境
set var 修改变量的值
start 开始执行程序,停在main函数第一行语句前面等待命令
step(或s) 执行下一行语句,如果有函数调用则进入到函数中


http://www.cnblogs.com/rosesmall/archive/2012/04/10/2440514.html

一般来说GDB主要调试的是C/C++的程序。要调试C/C++的程序,首先在编译时,我们必须要 
把调试信息加到可执行文件中。使用编译 器(cc/gcc/g++)的 -g 参数可以做到这一点。如: 

> cc -g hello.c -o hello 
> g++ -g hello.cpp -o hello 

如果没有-g,你将看不见程序的函数名、变量名,所代替的全是 运行时的内存地址。当你用 
-g把调试信息加入之后,并成功编译目标代码以后,让我们来看看如何用gdb来调试他。 


具体来说就是

例如一个程序名为prog 参数为  -l a -C abc
则,运行gcc/g++ -g  prog.c/cpp -o prog
就可以用gdb调试程序prog
#gdb prog
进入gdb调试界面
输入参数命令set args 后面加上程序所要用的参数,注意,不再带有程序名,直接加参数,如:
set args -l a -C abc
回车后输入
r
即可开始运行


http://blog.csdn.net/dadalan/article/details/3758025

一个调试示例 
—————— 
源程序:tst.c 
1 #include 

3 int func(int n) 
4 { 
5 int sum=0,i; 
6 for(i=0; i<7; i++) {

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 /n", result ); 
24 printf("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 
Copyright 2002 Free Software Foundation, Inc. 
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 

3 int func(int n) 
4 { 
5 int sum=0,i; 
6 for(i=0; i 7 { 
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 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 <---------- 在断点处停住。 
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 tst.c:5 
5 int sum=0,i; 
(gdb) n 
6 for(i=1; i<=n; i++) 
(gdb) p i <--------------------- 打印变量i的值,print命令简写。 
$1 = 134513808 
(gdb) n 
8 sum+=i; 
(gdb) n 
6 for(i=1; i<=n; i++)

(gdb) p sum 
$2 = 1 
(gdb) n 
8 sum+=i; 
(gdb) p i 
$3 = 2 
(gdb) n 
6 for(i=1; i<=n; i++) 
(gdb) p sum 
$4 = 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/libc.so.6 
(gdb) finish <--------------------- 退出函数。 
Run till exit from #0 func (n=250) at tst.c:5 
0x080484e4 in main () at tst.c:24 
24 printf("result[1-250] = %d /n", func(250) ); 
Value returned is $6 = 31375 
(gdb) c <--------------------- 继续运行。 
Continuing. 
result[1-250] = 31375 <----------程序输出。 
Program exited with code 027. <--------程序退出,调试结束。 
(gdb) q <--------------------- 退出gdb。 
hchen/test> 
好了,有了以上的感性认识,还是让我们来系统地认识一下gdb吧。 
使用GDB 
———— 
一般来说GDB主要调试的是C/C++的程序。要调试C/C++的程序,首先在编译时,我们必
须要把调试信息加到可执行文件中。使用编译器(cc/gcc/g++)的 -g 参数可以做到这一点。
如: 
> cc -g hello.c -o hello 
> g++ -g hello.cpp -o hello 
如果没有-g,你将看不见程序的函数名、变量名,所代替的全是运行时的内存地址。当你用
-g把调试信息加入之后,并成功编译目标代码以后,让我们来看看如何用gdb来调试他。 






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值