赞
踩
本文主要介绍Linux操作系统中GDB(The GNU Project Debugger)工具的常见用法。
引用GDB官网中的介绍,内容如下:
GDB, the GNU Project debugger, allows you to see what is going on 'inside' another program while it executes -- or what another program was doing at the moment it crashed.
GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:
- Start your program, specifying anything that might affect its behavior.
- Make your program stop on specified conditions.
- Examine what has happened, when your program has stopped.
- Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.
Those programs might be executing on the same machine as GDB (native), on another machine (remote), or on a simulator. GDB can run on most popular UNIX and Microsoft Windows variants, as well as on Mac OS X.
本章通过调试示例程序的方式,介绍GDB工具的常见用法。
示例程序的源代码(gdb_test1.cpp)内容如下:
- #include <iostream>
-
- using namespace std;
-
- int Sum(int& x, int& y)
- {
- cout << "Func Sum begin" << endl;
- int z;
- z = x + y;
-
- cout << "Func Sum end" << endl;
- return z;
- }
-
- int main()
- {
- cout << "Func main begin" << endl;
-
- int a = 1;
- int b = 2;
- int c;
-
- c = Sum(a, b);
- cout << "c is: " << c << endl;
-
- cout << "Func main end" << endl;
-
- return 0;
- }
编译上述代码,命令如下:
g++ -o gdb_test1 gdb_test1.cpp -g
注意:在编译过程中添加“-g”选项,将源代码信息添加到可执行文件中,便于后面进行gdb调试。
1. 启动gdb,命令及过程信息如下:
[root@node1 /opt/liitdar/mydemos/simples/gdb_test]<span class="hljs-comment"># gdb</span>
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.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 <span class="hljs-string">"show copying"</span>
and <span class="hljs-string">"show warranty"</span> <span class="hljs-keyword">for</span> details.
This GDB was configured as <span class="hljs-string">"x86_64-redhat-linux-gnu"</span>.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb)
说明:
2. 加载被调试的可执行文件,命令及过程信息如下:
(gdb) file gdb_test1
Reading symbols from /opt/liitdar/mydemos/simples/gdb_test/gdb_test1...<span class="hljs-keyword">done</span>.
(gdb)
说明:
3. 设置断点(breakpoint),命令及过程信息如下:
(gdb) b main
Breakpoint 1 at 0x400923: file gdb_test1.cpp, line 17.
(gdb)
说明:
- (gdb) b main
- Breakpoint 1 at 0x400873: file gdb_test1.cpp, line 17.
- (gdb)
- Note: breakpoint 1 also set at pc 0x400873.
- Breakpoint 2 at 0x400873: file gdb_test1.cpp, line 17.
- (gdb)
- Note: breakpoints 1 and 2 also set at pc 0x400873.
- Breakpoint 3 at 0x400873: file gdb_test1.cpp, line 17.
- (gdb)
4. 删除断点,命令及过程信息如下:
(gdb) d 1
(gdb)
说明:
5. 运行被调试的可执行文件,命令及过程信息如下:
【未设置断点时】:
(gdb) r
Starting program: /opt/liitdar/mydemos/simples/gdb_test/gdb_test1
Func main begin
Func Sum begin
Func Sum end
c is: 3
Func main end
[Inferior 1 (process 7432) exited normally]
(gdb)
【设置断点时】:
(gdb) r
Starting program: /opt/liitdar/mydemos/simples/gdb_test/gdb_test1
Breakpoint 2, main () at gdb_test1.cpp:17
17 cout << <span class="hljs-string">"Func main begin"</span> << <span class="hljs-string">endl;
(gdb) </span>
说明:
6. 继续执行被调试程序,命令及过程信息如下:
(gdb) c
Continuing.
Func main begin
Func Sum begin
Func Sum end
c is: 3
Func main end
[Inferior 1 (process 7548) exited normally]
(gdb)
说明:
7. 执行一行调试程序源码(如果此行代码中有函数调用,则会进入该函数中,继续一次一行地执行该函数的代码),命令及过程信息如下:
(gdb) s
23 c = Sum(a, b);
(gdb)
Sum (x=@0x7fffffffe498: 1, y=@0x7fffffffe494: 2) at gdb_test1.cpp:7
7 cout << <span class="hljs-string">"Func Sum begin"</span> << <span class="hljs-string">endl;
(gdb) </span>
说明:
8. 执行一行调试程序源码(如有函数调用,会执行该函数的所有内容),命令及过程信息如下:
(gdb) n
23 c = Sum(a, b);
(gdb)
Func Sum begin
Func Sum end
24 cout << <span class="hljs-string">"c is: "</span> << <span class="hljs-string">c << endl;
(gdb)
</span>
说明:
9. 打印变量的值,命令及过程信息如下:
(gdb) n
Func main begin
19 int a = 1;
(gdb)
20 int b = 2;
(gdb)
23 c = Sum(a, b);
(gdb) p c
<span class="hljs-variable">$19</span> = 0
(gdb) n
Func Sum begin
Func Sum end
24 cout << <span class="hljs-string">"c is: "</span> << <span class="hljs-string">c << endl;
(gdb) p c</span>
<span class="hljs-variable">$20</span> = 3
(gdb)
说明:
在运行gdb时,如果遇到此问题,说明系统中缺少debug信息,此问题的错误信息后面通常都会给出问题解决方法,如下:
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64 libgcc-4.8.5-16.el7_4.2.x86_64 libstdc++-4.8.5-16.el7_4.2.x86_64
1. 根据提示的问题解决方法,运行相关的命令,操作如下:
[root@node1 /opt/liitdar/mydemos/simples/gdb_test]<span class="hljs-comment"># debuginfo-install glibc-2.17-196.el7_4.2.x86_64 libgcc-4.8.5-16.el7_4.2.x86_64 libstdc++-4.8.5-16.el7_4.2.x86_64</span>
Loaded plugins: fastestmirror
enabling epel-debuginfo
epel-debuginfo/x86_64/metalink | 7.0 kB 00:00:00
epel-debuginfo | 1.5 kB 00:00:00
epel-debuginfo/x86_64/primary | 503 kB 00:00:02
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirror01.idc.hinet.net
* epel-debuginfo: mirror01.idc.hinet.net
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
epel-debuginfo 2783/2783
Could not find debuginfo <span class="hljs-keyword">for</span> main pkg: glibc-2.17-196.el7_4.2.x86_64
Could not find debuginfo pkg <span class="hljs-keyword">for</span> dependency package nss-softokn-freebl-3.28.3-8.el7_4.x86_64
Could not find debuginfo <span class="hljs-keyword">for</span> main pkg: libgcc-4.8.5-16.el7_4.2.x86_64
Could not find debuginfo <span class="hljs-keyword">for</span> main pkg: libstdc++-4.8.5-16.el7_4.2.x86_64
No debuginfo packages available to install
[root@node1 /opt/liitdar/mydemos/simples/gdb_test]<span class="hljs-comment"># </span>
2. 运行“debuginfo-install”命令,安装debug信息时,提示找不到debuginfo包,这是因为yum源中没有配置debuginfo、或者未打开debuginfo源的开关。所以,首先需要保证yum源中的debuginfo配置可用。本文中使用的yum配置文件(CentOS-Base.repo)中的debuginfo相关信息如下:
<span class="hljs-comment">#Debug Info</span>
[debuginfo]
name=CentOS-<span class="hljs-variable">$releasever</span> - DebugInfo
baseurl=http://debuginfo.centos.org/<span class="hljs-variable">$releasever</span>/<span class="hljs-variable">$basearch</span>/
gpgcheck=0
enabled=1
说明:
3. 配置完yum源后,更新yum缓存;
4. 再次运行前面的gdb提示命令(内容如下),此时即可成功安装debuginfo信息了。
debuginfo-install glibc-2.17-196.el7_4.2.x86_64 libgcc-4.8.5-16.el7_4.2.x86_64 libstdc++-4.8.5-16.el7_4.2.x86_64
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。