如何在linux系统下快速上手调试器gdb

如何在linux系统下快速上手调试器gdb_第1张图片

1.什么是调试器?

调试器是一种帮助开发人员查看和修复程序错误的工具

2.gdb的作用和特点?

gdb的作用

帮助开发人员寻找错误,并且提供了一系列的功能和工具进行调试

gdb的特点

  1. 源代码级调试
  2. 多平台支持
  3. 强大的调试功能
  4. 拓展性强

3.gdb的基本用法

gdb的基本用法
打开一个文件

-rw-rw-r-- 1 shuaijie shuaijie   67 Sep  9 07:56 Makefile
-rwxrwxr-x 1 shuaijie shuaijie 9880 Sep  9 10:27 mycode
-rw-rw-r-- 1 shuaijie shuaijie  490 Sep  9 10:25 mycode.c
[shuaijie@hecs-20819 test_gdb]$ 

用gdb打开文件,进行调试工作

[shuaijie@hecs-20819 test_gdb]$ gdb mycode
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/shuaijie/test_gdb/test_gdb/mycode...done.

list/l 行号:显示binFile源代码,接着上次的位置往下列,每次列10行。

 (gdb) list 0
1	#include<stdio.h>
2	void Printf3()
3	{
4	    printf("hello Printf3\n");
5	}
6	void Printf2()
7	{
8	    printf("hello Printf2\n");
9	    Printf3();
10	}

list/l 函数名:列出某个函数的源代码。

(gdb) l main
23	    }
24	    Printf1();
25	    return res;
26	}
27	int main()
28	{
29	    printf("debug begin\n");
30	    int top=100;
31	    int sum=addToTop(top);
32	    printf("sum:%d\n",sum);

r或run:运行程序

(gdb) r
Starting program: /home/shuaijie/test_gdb/test_gdb/mycode 
debug begin
hello Printf1
hello Printf2
hello Printf3
sum:5050
debgin end
[Inferior 1 (process 25637) exited normally]
Missing separate debuginfos, use: debuginfo-install glibc-2.17-326.el7_9.x86_64

break(b) 行号:在某一行设置断点

(gdb) b 30
Breakpoint 4 at 0x40060f: file mycode.c, line 30.
(gdb) b 31
Breakpoint 5 at 0x400616: file mycode.c, line 31.
(gdb) b 32
Breakpoint 6 at 0x400623: file mycode.c, line 32.

info break :查看断点信息

(gdb) info break
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x000000000040060f in main at mycode.c:30
5       breakpoint     keep y   0x0000000000400616 in main at mycode.c:31
6       breakpoint     keep y   0x0000000000400623 in main at mycode.c:32

s或step:进入函数调用
先用r运行程序,再用s进入函数调用

(gdb) r
Starting program: /home/shuaijie/test_gdb/test_gdb/mycode 
debug begin

Breakpoint 4, main () at mycode.c:30
30	    int top=100;
(gdb) s

Breakpoint 5, main () at mycode.c:31
31	    int sum=addToTop(top);
(gdb) 
addToTop (top=100) at mycode.c:18
18	    int res=0;
(gdb) 
19	    int i=1;
(gdb) 
20	    for(;i<=top;i++)
(gdb) 
22	        res+=i;

n 或 next:每次执行一条语句

(gdb) n
20	    for(;i<=top;i++)
(gdb) n
22	        res+=i;
(gdb) n
20	    for(;i<=top;i++)
(gdb) n
22	        res+=i;

finish:执行到当前函数返回,然后挺下来等待命令

(gdb) finish
Run till exit from #0  addToTop (top=100) at mycode.c:22
hello Printf1
hello Printf2
hello Printf3
0x0000000000400620 in main () at mycode.c:31
31	    int sum=addToTop(top);
Value returned is $1 = 5050

print§:打印表达式的值,通过表达式可以修改变量的值或者调用函数

(gdb) print
$2 = 5050

p 变量:打印变量值。

(gdb) p
$3 = 5050

set var:修改变量的值

(gdb) set top =30
(gdb) p top
$6 = 30

continue(或c):从当前位置开始连续而非单步执行程序

(gdb) r
Starting program: /home/shuaijie/test_gdb/test_gdb/mycode 
debug begin

Breakpoint 4, main () at mycode.c:30
30	    int top=100;
(gdb) c
Continuing.

Breakpoint 5, main () at mycode.c:31
31	    int sum=addToTop(top);
(gdb) 
Continuing.
hello Printf1
hello Printf2
hello Printf3

Breakpoint 6, main () at mycode.c:32
32	    printf("sum:%d\n",sum);
(gdb) 
Continuing.
sum:5050
debgin end
[Inferior 1 (process 25650) exited normally]

delete breakpoints:删除所有断点
先执行info break 查看当前的断点信息,再执行delete breakpoints删除所有的断点

(gdb) info break**加粗样式**
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x000000000040060f in main at mycode.c:30
	breakpoint already hit 1 time
5       breakpoint     keep y   0x0000000000400616 in main at mycode.c:31
	breakpoint already hit 1 time
6       breakpoint     keep y   0x0000000000400623 in main at mycode.c:32
	breakpoint already hit 1 time
(gdb) delete breakpoints
Delete all breakpoints? (y or n) y
(gdb) info break
No breakpoints or watchpoints.

delete breakpoints n:删除序号为n的断点
先用b在30,31,32这三行设置三个断点;再用info break查看当前的断点信息;然后用delete breakpoints 7把序号为7的断点删除;最后只剩下了8,9断点

(gdb) b 30
Breakpoint 7 at 0x40060f: file mycode.c, line 30.
(gdb) b 31
Breakpoint 8 at 0x400616: file mycode.c, line 31.
(gdb) b 32
Breakpoint 9 at 0x400623: file mycode.c, line 32.
(gdb) info break
Num     Type           Disp Enb Address            What
7       breakpoint     keep y   0x000000000040060f in main at mycode.c:30
8       breakpoint     keep y   0x0000000000400616 in main at mycode.c:31
9       breakpoint     keep y   0x0000000000400623 in main at mycode.c:32
(gdb) delete breakpoints 7
(gdb) info break
Num     Type           Disp Enb Address            What
8       breakpoint     keep y   0x0000000000400616 in main at mycode.c:31
9       breakpoint     keep y   0x0000000000400623 in main at mycode.c:32

info(或i) breakpoints:参看当前设置了哪些断点

(gdb) info breakpoints
Num     Type           Disp Enb Address            What
8       breakpoint     keep y   0x0000000000400616 in main at mycode.c:31
9       breakpoint     keep y   0x0000000000400623 in main at mycode.c:32

until X行号:跳至X行
先用r执行程序,然后执行unitl 4跳转到第4行

(gdb) r
Starting program: /home/shuaijie/test_gdb/test_gdb/mycode 
debug begin

Breakpoint 8, main () at mycode.c:31
31	    int sum=addToTop(top);
(gdb) until 4
hello Printf1
hello Printf2
hello Printf3

Breakpoint 9, main () at mycode.c:32
32	    printf("sum:%d\n",sum);

disable breakpoints:禁用断点
先用r执行程序,再执行disable breakpoints,用info break查看断点信息,可以看到当前状态为关闭状态

(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) dis^H^CEOF [assumed Y]
Starting program: /home/shuaijie/test_gdb/test_gdb/mycode 
Quit
(gdb) disable breakpoints
(gdb) info break
Num     Type           Disp Enb Address            What
8       breakpoint     keep n   0x0000000000400616 in main at mycode.c:31
9       breakpoint     keep n   0x0000000000400623 in main at mycode.c:32

breaktrace(或bt):查看各级函数调用及参数

(gdb) bt
#0  main () at mycode.c:32

enable breakpoints:启用断点

(gdb) info break
Num     Type           Disp Enb Address            What
8       breakpoint     keep y   0x0000000000400616 in main at mycode.c:31
9       breakpoint     keep y   0x0000000000400623 in main at mycode.c:32
10      breakpoint     keep y   0x0000000000400623 in main at mycode.c:32
	breakpoint already hit 1 time

quit或者ctrl+d:退出gdb

(gdb) quit
[shuaijie@hecs-20819 test_gdb]$ 

你可能感兴趣的:(linux,php,windows)