GDB调试器实验---对gdb各项调试参数的详细说明

GDB调试器实验,实验还成功,现在做一些笔记如下,可能以后忘记,到时候回头喵喵:
 
[root@localhost gcc]# cd /home/gdb/
[root@localhost gdb]# ls
test  test.c
[root@localhost gdb]#  gdb test               //启动GDB进行调试
GNU gdb Red Hat Linux (6.5-25.el5rh)
Copyright (C) 2006 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"...Using host libthread_db library "/lib/libthread_db.so.1".
(gdb)  l                  //“l(list)”命令用于查看文件
1       #include  
2
3       int main()
4       {
5               int sum(int sum);
6               int i,result=0;
7               sum(100);
8               for(i=1;i<=100;i++)
9               {
10                      result+=i;
(gdb) l
11              }
12              printf("the sum in main function is %d\n",result);
13      }
14
15      int sum(int num)
16      {
17              int i,n=0;
18              for(i=1;i<=num;i++)
19              {
20                      n+=i;
(gdb) l
21              }
22              printf("the sum in sum function is %d\n",n);
23      }
(gdb)  b 7            //“b(breakpoint)”命令用于设置断点
Breakpoint 1 at 0x804839c: file test.c, line 7.
(gdb)  info b          //“info”命令用于查看断点
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x0804839c in main at test.c:7
(gdb)  r            //“r(run)”命令,用于运行代码,默认是从首行开始,也可以在r后加上数字,从程序中指定行开始运行
Starting program: /home/gdb/test
Breakpoint 1, main () at test.c:7
7               sum(100);
(gdb)  p result                   //“p(point)”命令,用于查看某个变量的值
$1 = 0
(gdb) p i                     //在第7行,由于i还没有被赋值,所以是一个随机数
$2 = 4083700
(gdb)  s                        //“s(setp)”命令,用于单步调试,如果有函数“s”会进入函数内部,“n(nxet)”命令不会进入函数内部
sum (num=100) at test.c:17
17              int i,n=0;
(gdb)  p num              //一进入函数内部,num由形参传过来的值为100
$3 = 100              //所以显示num值为100
(gdb)  n
18              for(i=1;i<=num;i++)
(gdb)  p num
$4 = 100          
(gdb)  finish             //当进入函数内部后,当发现函数体调试没问题,finish命令运行程序,直到当前函数结束
Run till exit from #0  sum (num=100) at test.c:18
the sum in sum function is 5050
main () at test.c:8
8               for(i=1;i<=100;i++)
Value returned is $5 = 32
(gdb)  b 12      //设置第二个断点
Breakpoint 2 at 0x80483c1: file test.c, line 12.
(gdb)  info b  
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x0804839c in main at test.c:7
        breakpoint already hit 1 time
2   breakpoint     keep y   0x080483c1 in main at test.c:12
(gdb)  r                   //*****run默认从第一行开始运行*****
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/gdb/test
Breakpoint 1, main () at test.c:7
7               sum(100);                                                               //停在第一个断点
(gdb)  c                             //c命令,用于恢复程序的运行。例如,我们在查看第一个断点的变量及堆栈情况后,c命令运行就停在第二个断点了
Continuing.
the sum in sum function is 5050
Breakpoint 2, main () at test.c:12   
12              printf("the sum in main function is %d\n",result);     
//停在第二个断点
(gdb)  c                                    //继续运行
Continuing.
the sum in main function is 5050
Program exited with code 041.
(gdb)  q  //退出GDB调试

你可能感兴趣的:(工具使用,linux基础知识)