gdb调试信息与调试原理

    Linux 下 C++ 程序开发,即使使用 makefile、cmake 等编译工具,其最终都是调用 gcc 这一编译工具组的。这里说的工具组,是因为编译 C 程序和 C++ 程序使用的编译工具还是有一点差别的,一般编译 C 程序使用 gcc,编译 C++ 程序使用 g++。(下面为了叙述方便,除非特别说明,否则不做具体区分是 gcc 还是 g++,统一使用 gcc 这一名词代指。)

本课程中我使用的操作系统是 CentOS 7.0,为了演示方便直接使用的 root 账号演示。如果读者的机器上没有安装 gcc 和 gdb,可以使用 yum 命令安装一下。

# 安装 gcc
yum install gcc
# 安装 g++
yum install gcc-c++
# 安装 gdb
yum install gdb

复制

一般要调试某个程序,为了能清晰地看到调试的每一行代码、调用的堆栈信息、变量名和函数名等信息,需要调试程序含有调试符号信息。使用 gcc 编译程序时,如果加上 -g 选项即可在编译后的程序中保留调试符号信息。举个例子,以下命令将生成一个带调试信息的程序 hello_server。

gcc -g -o hello_server hello_server.c

复制

那么如何判断 hello_server 是否带有调试信息呢?我们使用 gdb 来调试一下这个程序,gdb 会显示正确读取到该程序的调试信息,在打开的 Linux Shell 终端输入 gdb hello_server 查看显示结果即可:

[root@localhost testclient]# gdb hello_server
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7_4.1
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 /root/testclient/hello_server...done.
(gdb)

复制

gdb 加载成功以后,会显示如下信息:

Reading symbols from /root/testclient/hello_server...done.

复制

即读取符号文件完毕,说明该程序含有调试信息。我们不加 -g 选项再试试:

[root@localhost testclient]# gcc -o hello_server2 hello_server.c
[root@localhost testclient]# gdb hello_server2
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7_4.1
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 /root/testclient/hello_server2...(no debugging symbols found)...done.
(gdb)

复制

细心的读者应该看出差别了,不加 -g 选项用 gdb 调试生成 hello_server 2 程序时,读取调试符号信息时会收到如下提示:

Reading symbols from /root/testclient/hello_server2...(no debugging symbols found)...done.

复制

顺便提一下,除了不加 -g 选项,也可以使用 Linux 的 strip 命令移除掉某个程序中的调试信息,我们这里对 hello_server 使用 strip 命令试试:

[root@localhost testclient]# strip hello_server
##使用 strip 命令之前
-rwxr-xr-x. 1 root root 12416 Sep 8 09:45 hello_server
##使用 strip 命令之后
-rwxr-xr-x. 1 root root 6312 Sep 8 09:55 hello_server

复制

可以发现,对 hello_server 使用 strip 命令之后,这个程序明显变小了(由 12416 个字节减少为 6312 个字节)。我们通常会在程序测试没问题以后,将其发布到生产环境或者正式环境中,因此生成不带调试符号信息的程序,以减小程序体积或提高程序执行效率。

我们再用 gdb 验证一下这个程序的调试信息确实被移除了:

[root@localhost testclient]# gdb hello_server
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7_4.1
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 /root/testclient/hello_server...(no debugging symbols found)...done.
(gdb)

复制

另外补充两点说明:

  • 本课程里虽然以 gcc 为例,但 -g 选项实际上同样也适用于使用 makefile 、cmake 等工具编译生成的 Linux 程序。
  • 在实际生成调试程序时,一般不仅要加上 -g 选项,也建议关闭编译器的程序优化选项。编译器的程序优化选项一般有五个级别,从 O0 ~ O4 ( 注意第一个 O0 ,是字母 O 加上数字 0 ), O0 表示不优化,从 O1 ~ O4 优化级别越来越高,O4 最高。这样做的目的是为了调试的时候,符号文件显示的调试变量等能与源代码完全对应起来。举个例子,假设有以下代码:
  int func()
  {
      int a = 1;
      int b = a + 1;
      int c = a + b;
      return a + b + c;
  }

  int main()
  {
      int a = func();
      printf("%d\n", a);
  }

复制

此代码中,由于在 main() 函数中调用了 func() 函数,而 func() 函数值可以在编译期间直接算出来,如果开启了优化选项,在实际调试的时候,这个函数中的局部变量 a 、 b 、c ,被直接的值取而代之(即编译器计算得出的值, a 直接使用 1,b 直接使用 2,c 直接使用 3,不会产生通过 a 计算 b,通过 a、b 计算 c 的指令),甚至连函数 func() 也可能被优化掉。如果出现这种情况,调试显示的代码和实际代码可能就会有差异了,这会给排查问题带来困难。当然,上面说的优化现象是否一定会出现,不同版本的编译器可能会有不同的行为。总之一句话,建议生成调试文件时关闭编译器优化选项

小结

本节课主要介绍了 gdb 等工具的安装以及调试符号对于调试的重要性,同时也要注意编译器优化选项对调试正确性的影响

猜你喜欢

转载自blog.csdn.net/weixin_38293850/article/details/107974741