分享 gdb list 功能

原贴地址点击打开链接

先贴一下我用来示范的程序,我们主要通过这段代码来step by step地学习gdb的使用.如果你想花15分钟学习,这不是你应该用的;如果你只是想对gdb有个简单的了解,这也不是你想要的; 如果你想对gdb有一个基本而全面的了解,这就是你想要的.

1 #include <stdio.h>
2
3 int func( int n )
4 {
5     int sum = 0, i;
6
7    for( i  = 0; i < n; i++ )
8    {
9        sum += i;
10    }
11    
12    return sum;
13   }
14
15
16  void main()
17  {
18     int i; 
19      long result = 0;
20      for( i = 1; i <=100; i++ )
21      {
22           result += i;
23      }
24
25      printf( "result[1-100] = %d \n", result );
26      printf( "result[1-250] = %d \n", func( 250 ) );
27     }


然后执行如下命令: gcc -g test.c -o test ,  生成test的可执行文件.

开始执行   gdb test

-------------------------------------------------------------------------------------------

GNU gdb Red Hat Linux (6.3.0.0-1.96rh)
Copyright 2004 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 "x86_64-redhat-linux-gnu"...Using host libthread_db library "/lib64/tls/libthread_db.so.1".

(gdb) 
-----------------------------------------------------------------------------------------


 list   显示代码内容

(1)  help list  看一下系统的帮助

-----------------------------------------------------------------------------------------

(gdb) help list
List specified function or line.
With no argument, lists ten more lines after or around previous listing.
"list -" lists the ten lines before a previous ten-line listing.
One argument specifies a line, and ten lines are listed around that line.
Two arguments with comma between specify starting and ending lines to list.
Lines can be specified in these ways:
  LINENUM, to list around that line in current file,
  FILE:LINENUM, to list around that line in that file,
  FUNCTION, to list around beginning of that function,
  FILE:FUNCTION, to distinguish among like-named static functions.
  *ADDRESS, to list around the line containing that address.
With two args if one is empty it stands for ten lines away from the other arg.

----------------------------------------------------------------------------------------------------------

上面说明: 

  list  显示上次显示的行的周围10行或后10行的代码.

        list +  显示上次显示的行的后10行代码

  list -    显示上次显示的行的前10行代码  

  list后面带一个参数的话,是显示某一行附近的代码,带两个参数(中间要带一个逗号)的话,是显示两个数字之间的行的内容.

  参数的指定方式:  

                 一个数字 

                 文件名:数字 

                 函数名

                 文件名:函数名

                 *内存地址  显示存在该内存地址处的代码的内容   

(2)   list显示当前行的后10行或周围10行的代码

(gdb) list
10          }
11          
12          return sum;
13      }
14
15
16      void main()
17      {
18          int i; 
19          long result = 0;


可以看到显示的是void main()前后的10行的代码


(3)list-   显示当前行的前10行代码

(gdb) list -
1       #include <stdio.h>
2
3       int func( int n )
4       {
5           int sum = 0, i;
6
7           for( i  = 0; i < n; i++ )
8           {
9               sum += i;


可以看到显示的是上面显示的10行代码的前面10行的代码


(4)  list  数字

(gdb) list 10
5           int sum = 0, i;
6
7           for( i  = 0; i < n; i++ )
8           {
9               sum += i;
10          }
11          
12          return sum;
13      }
14

可以看到显示的是10行附近的代码


(5)   list  文件名:数字

(gdb) list  test.c:15
10          }
11          
12          return sum;
13      }
14
15
16      void main()
17      {
18          int i; 
19          long result = 0;

可以看到显示的是test.c文件中的15行代码


(6) list 函数名

(gdb) list func
1       #include <stdio.h>
2
3       int func( int n )
4       {
5           int sum = 0, i;
6
7           for( i  = 0; i < n; i++ )
8           {
9               sum += i;
10          }

可以看到显示的是func函数的开头部分


(7)  如果想接着看函数的后面部分,可以继续执行list

(gdb) list
11          
12          return sum;
13      }
14
15
16      void main()
17      {
18          int i; 
19          long result = 0;
20          for( i = 1; i <=100; i++ )

可以看到显示的是上次10行的后10行的结果


(8) 现在看一下list +的效果

(gdb) list +
21          {
22              result += i;
23          }
24
25          printf( "result[1-100] = %d \n", result );
26          printf( "result[1-250] = %d \n", func( 250 ) );
27      }


(9)   list  文件名:函数名

(gdb) list test.c:func
1       #include <stdio.h>
2
3       int func( int n )
4       {
5           int sum = 0, i;
6
7           for( i  = 0; i < n; i++ )
8           {
9               sum += i;
10          }


(10)    更改一次显示的行数

这个每次显示10行是可以调的,如果觉得10行太小,可以更改一下.

这个行数是通过listsize来调整的.


可以通过show listsize来看一次显示几次

(gdb) show listsize
Number of source lines gdb will list by default is 10.


可以通过set listsize <count>来设置每次显示的行数

(gdb) set listsize 20
(gdb) show listsize
Number of source lines gdb will list by default is 20.
(gdb) list func
1       #include <stdio.h>
2
3       int func( int n )
4       {
5           int sum = 0, i;
6
7           for( i  = 0; i < n; i++ )
8           {
9               sum += i;
10          }
11          
12          return sum;
13      }
14
15
16      void main()
17      {
18          int i; 
19          long result = 0;

20          for( i = 1; i <=100; i++ )

可以看到设置完后,通过show listsize和list func都可以看到我们的更改生效了.


(11)   list *内存地址

既然是通过内存地址来显示代码内容就需要知道程序先跑起来,另外也需要提前知道某一段代码所在的内存位置,这个可以通过info line来查看

比如我们要看第20行代码的内存位置, info line 20;   也可以某一个函数的内存位置, info line func;

(gdb) info line 20
Line 20 of "test.c" starts at address 0x4004eb <main+16> and ends at 0x4004f8 <main+29>.
(gdb) list *0x4004eb
0x4004eb is in main (test.c:20).
10          }
11          
12          return sum;
13      }
14
15
16      void main()
17      {
18          int i; 
19          long result = 0;
20          for( i = 1; i <=100; i++ )
21          {
22              result += i;
23          }
24
25          printf( "result[1-100] = %d \n", result );
26          printf( "result[1-250] = %d \n", func( 250 ) );
27      }

上面先通过info line显示了20行代码的内存位置是0x4004eb,  然后通过list *0x4004eb来查看该 段代码的内容,可以看到显示的是20行代码,原因是因为之前设置的行数是20


至此list命令的学习结束了.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值