【跟我一起学gdb】(10)自动化gdb脚本之 list 命令

版权声明:本文为博主原创文章,未经博主允许不得转载,转载请注明文章来源,联系方式:[email protected] https://blog.csdn.net/u012421852/article/details/86661695

main.c

tom@ubuntu:~/dvp$ cat -n main.c
     1	const char* acWordsList[] = {"hello", "world", "good", "BeiJing"};
     2	
     3	static int s = 0;
     4	
     5	typedef struct
     6	{
     7		int iW;
     8		int iH;
     9		int iX;
    10		int iY;
    11	}BOX_ST;
    12	
    13	BOX_ST stBox;
    14	
    15	int sum(const int a, const int b)
    16	{
    17		int c = a + b;
    18		
    19		return c;
    20	}
    21	
    22	int main()
    23	{
    24		for (int i = 0; i < 18; ++i)
    25		{
    26			stBox.iW = i;
    27			stBox.iH = i;
    28			stBox.iX = i;
    29			stBox.iY = i;
    30			
    31			s = sum(i,i+2);
    32		}
    33		
    34		return 0;
    35	}
tom@ubuntu:~/dvp$ 

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.

By default, when a single location is given, display ten lines.
This can be changed using "set listsize", and the current value
can be shown using "show listsize".
(gdb)

show listsize 显示当前list命令显示的行数

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

set listsize <N> 设置当前list命令显示的行数

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

list <lineN> 显示给定行号lineN附近的代码

(gdb) list 10
5	typedef struct
6	{
7		int iW;
8		int iH;
9		int iX;
10		int iY;
11	}BOX_ST;
12	
13	BOX_ST stBox;
14	
(gdb) 

list <file:lineN> 显示文件file中行号lineN附近的代码

(gdb) list main.c:10
5	typedef struct
6	{
7		int iW;
8		int iH;
9		int iX;
10		int iY;
11	}BOX_ST;
12	
13	BOX_ST stBox;
14	
(gdb) 

list <function> 显示函数function附近的代码

(gdb) list main
18		
19		return c;
20	}
21	
22	int main()
23	{
24		for (int i = 0; i < 18; ++i)
25		{
26			stBox.iW = i;
27			stBox.iH = i;
(gdb) list sum
11	}BOX_ST;
12	
13	BOX_ST stBox;
14	
15	int sum(const int a, const int b)
16	{
17		int c = a + b;
18		
19		return c;
20	}
(gdb) 

list <file:function> 显示文件file中函数function附近的代码

(gdb) list main.c:main
18		
19		return c;
20	}
21	
22	int main()
23	{
24		for (int i = 0; i < 18; ++i)
25		{
26			stBox.iW = i;
27			stBox.iH = i;
(gdb) list main.c:sum
11	}BOX_ST;
12	
13	BOX_ST stBox;
14	
15	int sum(const int a, const int b)
16	{
17		int c = a + b;
18		
19		return c;
20	}
(gdb) 

list <*address> 显示地址为address的代码的附近的代码

(gdb) disassemble sum
Dump of assembler code for function sum:
   0x00000000004004d6 <+0>:	push   %rbp
   0x00000000004004d7 <+1>:	mov    %rsp,%rbp
   0x00000000004004da <+4>:	mov    %edi,-0x14(%rbp)
   0x00000000004004dd <+7>:	mov    %esi,-0x18(%rbp)
   0x00000000004004e0 <+10>:	mov    -0x14(%rbp),%edx
   0x00000000004004e3 <+13>:	mov    -0x18(%rbp),%eax
   0x00000000004004e6 <+16>:	add    %edx,%eax
   0x00000000004004e8 <+18>:	mov    %eax,-0x4(%rbp)
   0x00000000004004eb <+21>:	mov    -0x4(%rbp),%eax
   0x00000000004004ee <+24>:	pop    %rbp
   0x00000000004004ef <+25>:	retq   
End of assembler dump.
(gdb) list *0x4004d6
0x4004d6 is in sum (main.c:16).
11	}BOX_ST;
12	
13	BOX_ST stBox;
14	
15	int sum(const int a, const int b)
16	{
17		int c = a + b;
18		
19		return c;
20	}
(gdb) 

list <object>

功能:显示对象object附近的代码

(gdb) list stBox
8	{
9		int iW;
10		int iH;
11		int iX;
12		int iY;
13	}BOX_ST;
14	
15	BOX_ST stBox;
16	
17	int sum(const int a, const int b)
(gdb)

list <struct-type>

功能:显示自定义数据结构类型的代码

(gdb) list BOX_ST
8	{
9		int iW;
10		int iH;
11		int iX;
12		int iY;
13	}BOX_ST;
14	
15	BOX_ST stBox;
16	
17	int sum(const int a, const int b)
(gdb) 

list <global var>

功能:显示指定全局变量声明行附近的代码

(gdb) list s
1	#include <stdio.h>
2	
3	const char* acWordsList[] = {"hello", "world", "good", "BeiJing"};
4	
5	static int s = 0;
6	
7	typedef struct
8	{
9		int iW;
10		int iH;
(gdb) 

list 显示当前行后面的代码块

(gdb) list sum
13	}BOX_ST;
14	
15	BOX_ST stBox;
16	
17	int sum(const int a, const int b)
18	{
19		int c = a + b;
20		
21		return c;
22	}
(gdb) list
23	
24	int main()
25	{
26		for (int i = 0; i < 18; ++i)
27		{
28			stBox.iW = i;
29			stBox.iH = i;
30			stBox.iX = i;
31			stBox.iY = i;
32			
(gdb) list
33			s = sum(i,i+2);
34			
35			printf("%d + %d = %d\n", i, i + 2, s);
36		}
37		
38		return 0;
39	}
(gdb) 

list -

功能:显示当前行前面的代码块

(gdb) list
33			s = sum(i,i+2);
34			
35			printf("%d + %d = %d\n", i, i + 2, s);
36		}
37		
38		return 0;
39	}
(gdb) list -
23	
24	int main()
25	{
26		for (int i = 0; i < 18; ++i)
27		{
28			stBox.iW = i;
29			stBox.iH = i;
30			stBox.iX = i;
31			stBox.iY = i;
32			
(gdb) list -
13	}BOX_ST;
14	
15	BOX_ST stBox;
16	
17	int sum(const int a, const int b)
18	{
19		int c = a + b;
20		
21		return c;
22	}
(gdb) 

忘记怎么使用list命令时,有一妙计便可解决此困扰,那就是(gdb)help list

《完》

猜你喜欢

转载自blog.csdn.net/u012421852/article/details/86661695
今日推荐