desktop:~$ cat debugme.c <-----------------------------------------------显示程序内容
#include
#define BIGNUM 50
void index_to_the_moon(int ary[]);
int main(int argc, char *argv[])
{
int i=0;
int intary[100]={0};
for(i=50;i<100;i++)
{
intary[i]=i*i;
}
index_to_the_moon(intary);
return 0;
}
void index_to_the_moon(int ary[])
{
int i;
for(i=0;i
ary[i]=i;
}
desktop:~$ gcc -g debugme.c -o debugme <-------------------------------编译
desktop:~$ gdb debugme <-----------------------------------------------------启动GDB
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <
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 "i486-linux-gnu"...
(gdb) l <------------------------------------------------------------------------------------------------------从第一行开始列出源程序
1 #include
2 #define BIGNUM 50
3
4 void index_to_the_moon(int ary[]);
5
6 int main(int argc, char *argv[])
7 {
8 int i=0;
9 int intary[100]={0};
10 for(i=50;i<100;i++)
(gdb) list 11,15
11 {
12 intary[i]=i*i;
13 }
14 index_to_the_moon(intary);
15
(gdb) l
16 return 0;
17 }
18 void index_to_the_moon(int ary[])
19 {
20 int i;
21 for(i=0;i
22 ary[i]=i;
23 }
(gdb) break 8 <---------------------------------------------------------------------------在第8行设置断点
Breakpoint 1 at 0x8048388: file debugme.c, line 8.
(gdb) break 12 if i==75 <-------------------------------------------------------------在第12行设置条件断点
Breakpoint 2 at 0x80483b7: file debugme.c, line 12.
(gdb) break index_to_the_moon <------------------------------------------------代码进入函数时停止执行
Breakpoint 3 at 0x80483f7: file debugme.c, line 21.
(gdb) info break <----------------------------------------------------------------------查看断点信息
Num Type Disp Enb Address What
1 breakpoint keep y 0x08048388 in main at debugme.c:8
2 breakpoint keep y 0x080483b7 in main at debugme.c:12
stop only if i==75
3 breakpoint keep y 0x080483f7 in index_to_the_moon at debugme.c:21
(gdb) r <---------------------------------------------------------------------------------------运行程序
Starting program: /home/wangzb/debugme
Breakpoint 1, main () at debugme.c:8
8 int i=0;
(gdb) continue
Continuing.
Breakpoint 2, main () at debugme.c:12
12 intary[i]=i*i;
(gdb) n
10 for(i=50;i<100;i++)
(gdb) print i <---------------------------------------------------------------------------------查看变量
$1 = 75
(gdb) p intary[i]
$2 = 5625
(gdb) search return
16 return 0;
(gdb) c <------------------------------------------------------------------------------------继续运行程序
Continuing.
Breakpoint 3, index_to_the_moon (ary=0xbfec1a60) at debugme.c:21
21 for(i=0;i
(gdb) n <-------------------------------------------------------------------------------------单条语句执行,next
22 ary[i]=i;
(gdb) n
21 for(i=0;i
(gdb) n
22 ary[i]=i;
(gdb) n
21 for(i=0;i
(gdb) whatis ary<-------------------------------------------------------------------------------查看变量类型
type = int *
(gdb) p ary@5<--------------------------------------------------------------------------打印始于ary的5个值
$1 = {0xbffd4370, 0x0, 0x190, 0x0, 0x0}
(gdb) p ary[1]@5<------------------------------------------------打印以第一个元素开始的存储在ary的5个值
$2 = {1, 0, 0, 0, 0}
(gdb) bt<-------------------------------------------------------------------------------------查看函数堆栈
#0 index_to_the_moon (ary=0xbf88bc30) at debugme.c:21
#1 0x080483e0 in main () at debugme.c:14
(gdb) reverse-search return<----------------------------------------------------------寻找return先前出现的位置
16 return 0;
(gdb) finish<--------------------------------------------------------------------------------------跳出函数
Run till exit from #0 index_to_the_moon (ary=0xbfec1a60) at debugme.c:21
main () at debugme.c:16
16 return 0;
(gdb) n
17 }
(gdb) c
Continuing.
Program exited normally.<--------------------------------------------------------------调试结束
(gdb) q<----------------------------------------------------------------------------------------退出GDB
desktop:~$
阅读(1499) | 评论(0) | 转发(0) |