8.3. 运行 GNU Debugger

要在您要调试的程序上运行 GNU Debugger

$ scl enable devtoolset-11 'gdb file_name'

这将以交互模式启动 gdb 调试器并显示默认提示符 (gdb)。要退出调试会话并返回到 shell 提示符,请随时运行以下命令:

(gdb) quit

请注意,您可以使用 scl 程序执行任何命令,从而导致使用 Red Hat Developer Toolset 二进制文件运行它,而不是 Red Hat Enterprise Linux 系统等同的 Red Hat Enterprise Linux 系统。这可让您使用 Red Hat Developer Toolset gdb 作为默认值运行 shell 会话:

$ scl enable devtoolset-11 'bash'
注意

在任意时间点验证您使用的 gdb 版本:

$ which gdb

Red Hat Developer Toolset 的 gdb 可执行文件路径以 /opt 开头。另外,您可以使用以下命令确认与 Red Hat Developer Toolset gdb 的版本号匹配:

$ gdb -v

例 8.3. 在 fibonacci Binary 文件中运行 gdb 工具程序

这个示例假设您已成功编译了 fibonacci 二进制文件,如 例 8.1 “使用调试信息编译 C 程序” 所示。

使用 gdb 启动调试 fibonacci

$ scl enable devtoolset-11 'gdb fibonacci'
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-2.el7
Copyright (C) 2017 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from fibonacci...done.
(gdb)