简单示例:
gdb server
b server.c 10 //在server.c的第10行断点
n
p
bt //堆栈查看
p cfg->type //查看结构体某个字段的值
得到的函数列表:
info functions
启动 GDB 调试工具
$ gdb --quiet
info 命令列出程序信息
(gdb) info proc
list 命令
(gdb) list main
disassemble 命令
(gdb) disass main
========================================
刚刚学了下gdb调试器的简单使用,感觉还不错,趁热打铁,把过程讲述下,自己也增强下,呵呵,废话少说,Begin!!!
[root@localhost hello]# vim test.c //新建一个test.c的源文件
在test.c中键入如下代码,很简单的程序:
/*test.c*/
#include
int sum(int m);
int main(void)
{
int i,m =0;
sum(50);
for(i=50; i!=0; i--)m+=i;
printf("The sum of 1~50 is %d \n",m);
}
int sum(int m)
{
int i,n =0;
// sum(50);
for(i=m; i!=0; i--)n+=i;
printf("The sum of 1~m is %d \n",n);
}
完了后保存它,返回终端
[root@localhost hello]# gcc -g test.c -o test //记得一定要加 -g,这样编译出的可执行代码才包含调试的信息,否则gdb是无法载入的
[root@localhost hello]# gdb test //test为带有调试信息的目标文件
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 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-redhat-linux-gnu"...
(gdb) //以上的信息就是gdb的启动画面,指定了gdb的版本号等信息
//然后键入 l 查看加载的文件,l表示list,b表示breakpoint,p表示print
(gdb) l //一次加载10行总[个人总结],
1 /*test.c*/
2 #include
3 int sum(int m);
4
5 int main(void)
6 {
7 int i,m =0;
8 sum(50);
9 for(i=50; i!=0; i--)m+=i;
10 printf("The sum of 1~50 is %d \n",m);
(gdb)
(gdb) l //继续通过参数 l 加载,加载接下来的10行
11 }
12
13 int sum(int m)
14 {
15 int i,n =0;
16 // sum(50);
17 for(i=m; i!=0; i--)n+=i;
18 printf("The sum of 1~m is %d \n",n);
19 }
(gdb) l //加载完毕,再加载显然是不会在加了哈
Line number 20 out of range; test.c has 19 lines.
//首先来看怎么设断点
(gdb) b 8 //b表示设断点,后面表示行号,就是加载时显示的行号
Breakpoint 1 at 0x804833f: file test.c, line 8.
(gdb) b 9
Breakpoint 2 at 0x804834c: file test.c, line 9.
(gdb) info b //我设了两个断点,通过info 来查看断点信息
Num Type Disp Enb Address What
1 breakpoint keep y 0x0804833f in main at test.c:8
2 breakpoint keep y 0x0804834c in main at test.c:9
(gdb)
(gdb) r //r表示run,运行至断点1,如果没设断点,则运行至结尾
Starting program: /root/hello/test
Breakpoint 1, main () at test.c:8
8 sum(50);
(gdb) c //c表示continue,继续运行
Continuing.
The sum of 1~m is 1275
Breakpoint 2, main () at test.c:9
9 for(i=50; i!=0; i--)m+=i;
(gdb) //呵呵,上面几个操作看明白了吧
//怎么来看变量值呢
(gdb) l //先用l来看看源码
4
5 int main(void)
6 {
7 int i,m =0;
8 sum(50);
9 for(i=50; i!=0; i--)m+=i;
10 printf("The sum of 1~50 is %d \n",m);
11 }
12
13 int sum(int m)
(gdb) p m //p表示print,打印m的值
$1 = 0
(gdb) p i //打印i的值,i为什么这么大,应该不用我介绍了吧
$2 = 1073828704
//变量知道怎么看了,那么单步运行呢
(gdb) n //n表示单步运行,这表示一次运行一行,所以它不会进入调用的函数
10 printf("The sum of 1~50 is %d \n",m);
(gdb) n //n的运行机制通过这俩个n应该要看出个门道来喽,我用颜色强调了下,明白了没
The sum of 1~50 is 1275
11 }
//那么如果碰到调用函数怎么进入呢,不急,有办法
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/hello/test
Breakpoint 1, main () at test.c:8
8 sum(50); //前面设的断点,表示下一步将运行这断点所在的行
(gdb) s //用s来进入函数,也就说s也是单步运行的意思,但是它碰到函数时会进入函数运行; 而n不会,直接跳过
sum (m=50) at test.c:15 //仔细看看,是不是进入了sum函数哈
15 int i,n =0;
(gdb) s
17 for(i=m; i!=0; i--)n+=i;
(gdb) s
18 printf("The sum of 1~m is %d \n",n);
(gdb)
//这样在windows下的编译器的调试方法在gdb中都有相应的命令对应,并且更加灵活,哦忘了说怎么取消断点拉,呵呵简单
(gdb) info b //首先查看断点信息
Num Type Disp Enb Address What
1 breakpoint keep y 0x0804833f in main at test.c:8
breakpoint already hit 1 time
2 breakpoint keep y 0x0804834c in main at test.c:9
(gdb) delet 1 //用delet取消断点,后面的数字应该明白吧
//还有其它命令可以通过输入help来查看,或者help command[需要查看的命令]来查看
//退出gdb,返回终端是通过q来实现的
(gdb) q
The program is running. Exit anyway? (y or n) y
[root@localhost hello]#
还有其它很多命令,我就不一一介绍拉。
gdb server
b server.c 10 //在server.c的第10行断点
n
p
bt //堆栈查看
p cfg->type //查看结构体某个字段的值
得到的函数列表:
info functions
启动 GDB 调试工具
$ gdb --quiet
info 命令列出程序信息
(gdb) info proc
list 命令
(gdb) list main
disassemble 命令
(gdb) disass main
========================================
刚刚学了下gdb调试器的简单使用,感觉还不错,趁热打铁,把过程讲述下,自己也增强下,呵呵,废话少说,Begin!!!
[root@localhost hello]# vim test.c //新建一个test.c的源文件
在test.c中键入如下代码,很简单的程序:
/*test.c*/
#include
int sum(int m);
int main(void)
{
int i,m =0;
sum(50);
for(i=50; i!=0; i--)m+=i;
printf("The sum of 1~50 is %d \n",m);
}
int sum(int m)
{
int i,n =0;
// sum(50);
for(i=m; i!=0; i--)n+=i;
printf("The sum of 1~m is %d \n",n);
}
完了后保存它,返回终端
[root@localhost hello]# gcc -g test.c -o test //记得一定要加 -g,这样编译出的可执行代码才包含调试的信息,否则gdb是无法载入的
[root@localhost hello]# gdb test //test为带有调试信息的目标文件
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 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-redhat-linux-gnu"...
(gdb) //以上的信息就是gdb的启动画面,指定了gdb的版本号等信息
//然后键入 l 查看加载的文件,l表示list,b表示breakpoint,p表示print
(gdb) l //一次加载10行总[个人总结],
1 /*test.c*/
2 #include
3 int sum(int m);
4
5 int main(void)
6 {
7 int i,m =0;
8 sum(50);
9 for(i=50; i!=0; i--)m+=i;
10 printf("The sum of 1~50 is %d \n",m);
(gdb)
(gdb) l //继续通过参数 l 加载,加载接下来的10行
11 }
12
13 int sum(int m)
14 {
15 int i,n =0;
16 // sum(50);
17 for(i=m; i!=0; i--)n+=i;
18 printf("The sum of 1~m is %d \n",n);
19 }
(gdb) l //加载完毕,再加载显然是不会在加了哈
Line number 20 out of range; test.c has 19 lines.
//首先来看怎么设断点
(gdb) b 8 //b表示设断点,后面表示行号,就是加载时显示的行号
Breakpoint 1 at 0x804833f: file test.c, line 8.
(gdb) b 9
Breakpoint 2 at 0x804834c: file test.c, line 9.
(gdb) info b //我设了两个断点,通过info 来查看断点信息
Num Type Disp Enb Address What
1 breakpoint keep y 0x0804833f in main at test.c:8
2 breakpoint keep y 0x0804834c in main at test.c:9
(gdb)
(gdb) r //r表示run,运行至断点1,如果没设断点,则运行至结尾
Starting program: /root/hello/test
Breakpoint 1, main () at test.c:8
8 sum(50);
(gdb) c //c表示continue,继续运行
Continuing.
The sum of 1~m is 1275
Breakpoint 2, main () at test.c:9
9 for(i=50; i!=0; i--)m+=i;
(gdb) //呵呵,上面几个操作看明白了吧
//怎么来看变量值呢
(gdb) l //先用l来看看源码
4
5 int main(void)
6 {
7 int i,m =0;
8 sum(50);
9 for(i=50; i!=0; i--)m+=i;
10 printf("The sum of 1~50 is %d \n",m);
11 }
12
13 int sum(int m)
(gdb) p m //p表示print,打印m的值
$1 = 0
(gdb) p i //打印i的值,i为什么这么大,应该不用我介绍了吧
$2 = 1073828704
//变量知道怎么看了,那么单步运行呢
(gdb) n //n表示单步运行,这表示一次运行一行,所以它不会进入调用的函数
10 printf("The sum of 1~50 is %d \n",m);
(gdb) n //n的运行机制通过这俩个n应该要看出个门道来喽,我用颜色强调了下,明白了没
The sum of 1~50 is 1275
11 }
//那么如果碰到调用函数怎么进入呢,不急,有办法
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/hello/test
Breakpoint 1, main () at test.c:8
8 sum(50); //前面设的断点,表示下一步将运行这断点所在的行
(gdb) s //用s来进入函数,也就说s也是单步运行的意思,但是它碰到函数时会进入函数运行; 而n不会,直接跳过
sum (m=50) at test.c:15 //仔细看看,是不是进入了sum函数哈
15 int i,n =0;
(gdb) s
17 for(i=m; i!=0; i--)n+=i;
(gdb) s
18 printf("The sum of 1~m is %d \n",n);
(gdb)
//这样在windows下的编译器的调试方法在gdb中都有相应的命令对应,并且更加灵活,哦忘了说怎么取消断点拉,呵呵简单
(gdb) info b //首先查看断点信息
Num Type Disp Enb Address What
1 breakpoint keep y 0x0804833f in main at test.c:8
breakpoint already hit 1 time
2 breakpoint keep y 0x0804834c in main at test.c:9
(gdb) delet 1 //用delet取消断点,后面的数字应该明白吧
//还有其它命令可以通过输入help来查看,或者help command[需要查看的命令]来查看
//退出gdb,返回终端是通过q来实现的
(gdb) q
The program is running. Exit anyway? (y or n) y
[root@localhost hello]#
还有其它很多命令,我就不一一介绍拉。
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/757/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
最后编辑: jackxiang 编辑于2013-7-1 19:59
评论列表