gdb调试

1、当程序执行出错后,会生成一个core文件

2、调试命令:gdb 可执行文件 core文件,比如我的hello文件生成的core dump文件core,就是执行

1 $ gdb hello core
2 Core was generated by `./hello'.
3 Program terminated with signal SIGSEGV, Segmentation fault.
4 #0  0x000000000040072d in main () at test.c:17
5 17              *p = 10;

可以看到显示了哪里出错

3、

 1 (gdb) where
 2 #0  0x000000000040072d in main () at test.c:17
 3 (gdb) list
 4 12      #include <fcntl.h>
 5 13      int main()
 6 14      {
 7 15              FILE *fp = NULL;
 8 16              int *p = NULL;
 9 17              *p = 10;
10 18              char *filepath = "/home/test/test.txt";
11 19              char buffer[1024] = {'\0'};
12 20              int len=0;
13 21              if( (0 == access(filepath, F_OK)) && (fp = fopen(filepath,"r")) )
14 (gdb) 

也可以执行where命令显示哪里出错了,list命令是列出出错地方的上下文。list m,n,m,n显示m至n行的代码,不带参数的list显示出错代码附近的10行。list也可以简写为字母l

 

4、

正常情况执行gdb,一开始打印很多的版本信息之类的,如果不想看到,可以在执行的时候加上-q参数

1 $ gdb -q hello

5、

break命令设置断点

1 - break linenum
2 - break funcname
3 - break filename:linenum
4 - break filename:funcname

比如,

 1 (gdb) break main
 2 Breakpoint 1 at 0x400701: file test.c, line 14.
 3 (gdb) run
 4 Starting program: /home/jimmy/Personal/Linux服务器编程/day1/mine/hello 
 5 
 6 Breakpoint 1, main () at test.c:14
 7 14      {
 8 (gdb) l 10,16
 9 10      #include <sys/types.h>
10 11      #include <sys/stat.h>
11 12      #include <fcntl.h>
12 13      int main()
13 14      {
14 15              FILE *fp = NULL;
15 16              char *filepath = "/home/jimmy/Personal/Linux服务器编程/day1/mine/test.txt";
16 (gdb) s
17 15              FILE *fp = NULL;
18 (gdb) s
19 16              char *filepath = "/home/jimmy/Personal/Linux服务器编程/day1/mine/test.txt";
20 (gdb) 

(1)在main函数设置一个断点:break main

(2)然后运行:run

(3)然后也可以一步步调试:s,这里说明一下,s是step的缩写,当遇到一个调用的函数的时候,step会进入到函数里面,还有一个命令是next,遇到调用的函数的时候直接输出函数的结果,不会进入到函数里面。

 

6、其他一些调试手段

 1 (gdb) print len
 2 $1 = 32767
 3 (gdb) s
 4 19              if( (0 == access(filepath, F_OK)) && (fp = fopen(filepath,"r")) )
 5 (gdb) print len
 6 $2 = 0
 7 (gdb) whatis len
 8 type = int
 9 (gdb) info break
10 Num     Type           Disp Enb Address            What
11 1       breakpoint     keep y   0x0000000000400701 in main at test.c:14
12         breakpoint already hit 1 time
13 (gdb) 

print打印变量的值,whatis显示变量的类型,info break显示有多少个断点

 

posted @ 2018-01-01 11:34  伊斯科明  阅读(114)  评论(0编辑  收藏  举报