gdb使用技巧总结

简介

一般来说,GDB主要帮忙你完成下面四个方面的功能:

  1. 启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。
  2. 可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)
  3. 当程序被停住时,可以检查此时你的程序中所发生的事。
  4. 动态的改变你程序的执行环境。

技巧1 启动时不显示提示信息

[ych@localhost ~]$ gdb
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.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/>.

gdb在启动时会显示如上类似的提示信息。
如果不想显示这个信息,则可以使用-q选项把提示信息关掉:

[ych@localhost ~]$ gdb -q
(gdb) q

也可以在
~/.bashrc中,为gdb设置一个别名:

alias gdb="gdb -q"

技巧2 列出函数的名字

使用gdb调试时,使用“info functions”命令可以列出可执行文件的所有函数名称。以上面代码为例:

(gdb) info functions tips*
All functions matching regular expression "tips*":

Non-debugging symbols:
0x00000000004005dd  tips2_func1
0x00000000004005fc  tips2_func2
0x0000000000400619  tips2_func3
0x0000000000400636  tips2_func4

这个命令也支持正则表达式:“info functions tips*”

技巧3 gdb进入程序的方法

  1. 使用gdb运行可执行程序
[ych@localhost ~]$ gdb tips2
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.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/ych/tips2...(no debugging symbols found)...done.
(gdb) 
(gdb) 
  1. 使用gdb 的 -p参数接入正在运行的程序
    gdb -p procID
[ych@localhost ~]$ gdb -p 3390
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.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/>.
Attaching to process 3390

技巧4 gdb调试的所用到的命令

以下命令是进入gdb调试界面后所使用的调试命令

  1. 设置断点
    break – Set breakpoint at specified line or function
    可以带代码行号或者函数名
(gdb) break 28
Breakpoint 1 at 0x40065d: file tips2.c, line 28.
  1. 查看设置的断点状态
    info breakpoints – Status of specified breakpoints (all user-settable breakpoints if no argument)
(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000040065d in main at tips2.c:28
2       breakpoint     keep y   0x000000000040065d in main at tips2.c:29
3       breakpoint     keep y   0x000000000040065d in main at tips2.c:30
  1. 开始debug程序
    run – Start debugged program(简写 r)
(gdb) run
Starting program: /home/ych/tips2 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, main () at tips2.c:30
30			tips2_func1();
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.6.x86_64
  1. 下一步,同函数级别
    next – Step program(简写 n)
(gdb) run
Starting program: /home/ych/tips2 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Breakpoint 1, main () at tips2.c:30
30			tips2_func1();
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.6.x86_64
(gdb) n
tips2_func1(6)
31	        pthread_create(&t1, NULL, tips2_func2, "Thread 1");
(gdb) n
[New Thread 0x7ffff77f1700 (LWP 14310)]
32	        pthread_create(&t2, NULL, tips2_func3, "Thread 2");
(gdb) n
[New Thread 0x7ffff6ff0700 (LWP 14311)]
33			tips2_func4();
(gdb) n
tips2_func4(23)
36				sleep(1000);
  1. 下一步,走到函数内部
    step – Step program until it reaches a different source line(简写 s)
Breakpoint 3, main () at tips2.c:32
32	        pthread_create(&t2, NULL, tips2_func3, "Thread 2");
(gdb) n
[New Thread 0x7ffff6ff0700 (LWP 14383)]

Breakpoint 4, main () at tips2.c:33
33			tips2_func4();
(gdb) s
tips2_func4 () at tips2.c:23
23	    printf("%s(%d)\r\n", __FUNCTION__, __LINE__);
(gdb) 
  1. 打印变量值
    print – Print value of expression EXP (简写 p)
Breakpoint 2, main () at tips2.c:31
31	        pthread_create(&t1, NULL, tips2_func2, "Thread 1");
Value returned is $1 = (void *) 0x10
(gdb) p t1
$2 = 0
(gdb) n
[New Thread 0x7ffff77f1700 (LWP 25004)]

Breakpoint 3, main () at tips2.c:32
32	        pthread_create(&t2, NULL, tips2_func3, "Thread 2");
(gdb) p t1
$3 = 140737345689344
(gdb) p t2
$4 = 140737488348560
  1. 继续运行程序
    continue – Continue program being debugged (简写 c)
(gdb) c
Continuing.
[New Thread 0x7ffff6ff0700 (LWP 25086)]

Breakpoint 4, main () at tips2.c:33
33			tips2_func4();
  1. 退出函数
    finish – Execute until selected stack frame returns
Breakpoint 1, main () at tips2.c:30
30			tips2_func1();
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.6.x86_64
(gdb) s
tips2_func1 () at tips2.c:6
6	    printf("%s(%d)\r\n", __FUNCTION__, __LINE__);
(gdb) finish 
Run till exit from #0  tips2_func1 () at tips2.c:6
tips2_func1(6)
  1. 列出指定函数或者行号上下的源码
    list – List specified function or line
(gdb) list main
22	{
23	    printf("%s(%d)\r\n", __FUNCTION__, __LINE__);
24	}
25	
26	int main(void)
27	{
28	        pthread_t t1, t2;
29	
30			tips2_func1();
31	        pthread_create(&t1, NULL, tips2_func2, "Thread 1");
(gdb) 
32	        pthread_create(&t2, NULL, tips2_func3, "Thread 2");
33			tips2_func4();
34	
35	        while (1)
36				sleep(1000);
37	        return;
38	}

通过 set listsize 命令可以设置设置一次显示源代码的行数
通过 show listsize 命令可以查看当前listsize的设置

猜你喜欢

转载自blog.csdn.net/yaochuh/article/details/100581929