第 3 章 Debugging Applications
You can use common techniques to debug applications in multiple situations.
3.1. Enabling Debugging with Debugging Information 复制链接链接已复制到粘贴板!
To debug applications and libraries, you must obtain debugging information.
3.1.1. Debugging information 复制链接链接已复制到粘贴板!
Debugging information links binary code to source code so you can inspect variables and execution flow. The GNU Compiler Collection (GCC) generates this data in the Debug With Arbitrary Record Format (DWARF) within ELF files, and the GNU Debugger (GDB) uses it to analyze program behavior.
Red Hat Enterprise Linux uses the ELF format for executable binaries, shared libraries, or debuginfo files. Within these ELF files, the DWARF format is used to hold the debug information.
To display DWARF information stored within an ELF file, run the readelf -w file command.
STABS (Symbol Table format) is an older, less capable debugging format, occasionally used with UNIX. For debugging on RHEL, use the DWARF format instead. GCC and GDB support STABS on a best-effort basis only. Tools such as Valgrind and elfutils do not support STABS.
To debug C and C++ applications effectively, generate debugging information during compilation. Use GCC’s -g option to create this data. Debuggers use this data to map executable code to source lines for inspecting variables and logic.
Prerequisites
-
You have the
gccpackage installed.
Procedure
Compile and link your code with the
-goption to generate debugging information:$ gcc ... -g ...Optional: Set the optimization level to
-Og:$ gcc ... -g -Og ...Compiler optimizations can make executable code hard to relate to the source code. The
-Ogoption optimizes the code without interfering with debugging. However, be aware that changing optimization levels can alter the program’s behavior.Optional: Use
-gfor moderate debugging information, or-g3to include macro definitions:$ gcc ... -g3 ...
Verification
Test the code by using the
-fcompare-debugGCC option:$ gcc -fcompare-debug ...This option tests code compiled with and without debug information. If the resulting binaries are identical, the executable code is not affected by debugging options. By using the
-fcompare-debugoption significantly increases compilation time.
3.1.3. Debuginfo and debugsource packages 复制链接链接已复制到粘贴板!
The debuginfo and debugsource packages contain debugging information and source code for programs and libraries. To debug Red Hat Enterprise Linux applications, install these packages from additional repositories.
- Debugging information package types
-
Debuginfo packages: Thedebuginfopackages provide debugging information needed to provide human-readable names for binary code features. These packages contain.debugfiles, which contain DWARF debugging information. These files are installed to the/usr/lib/debugdirectory.
Debugsource packages: The debugsource packages contain the source files used for compiling the binary code. With both debuginfo and debugsource packages installed, debuggers such as GDB or LLDB can relate the execution of binary code to the source code. The source code files are installed to the /usr/src/debug directory.
To obtain the necessary debuginfo packages for troubleshooting installed applications or libraries, use the GNU Debugger (GDB). It automatically detects missing symbols and identifies the specific packages needed. Follow GDB’s recommendations to install them for full debugging capabilities.
Prerequisites
- The application or library you want to debug must be installed on the system.
-
GDB and the
debuginfo-installtool must be installed on the system. -
Repositories providing
debuginfoanddebugsourcepackages must be configured and enabled on the system. For details, see Enabling debug and source repositories.
Procedure
Start GDB attached to the application or library you want to debug. GDB automatically recognizes missing debugging information and suggests a command to run.
$ gdb -q /bin/lsReading symbols from /bin/ls...Reading symbols from .gnu_debugdata for /usr/bin/ls...(no debugging symbols found)...done. (no debugging symbols found)...done. Missing separate debuginfos, use: dnf debuginfo-install coreutils-9.5-6.el10.x86_64 (gdb)Exit GDB: type q and confirm with Enter.
(gdb) qRun the command suggested by GDB to install the required
debuginfopackages:# dnf debuginfo-install coreutils-9.5-6.el10.x86_64The
dnfpackage management tool provides a summary of the changes, asks for confirmation and once you confirm, downloads and installs all the necessary files.In case GDB is not able to suggest the
debuginfopackage, follow the procedure described in
To manually determine which debuginfo packages you need to install, locate the executable file and find the package that installs it.
Prefer Use GDB to determine the packages for installation when that is an option.
Use this manual procedure only if GDB cannot suggest which package to install.
Prerequisites
- The application or library must be installed on the system.
- The application or library was installed from a package.
-
The
debuginfo-installtool must be available on the system. -
Channels providing the
debuginfopackages must be configured and enabled on the system. -
If you use the
locatecommand in this procedure, themlocatepackage must be installed and its database must be current. Asroot, rundnf install mlocateif needed, then runupdatedb.
Procedure
Find the executable file of the application or library.
Use the
whichcommand to find the application file.$ which less/usr/bin/lessUse the
locatecommand to find the library file.$ locate libz | grep so/usr/lib64/libz.so.1 /usr/lib64/libz.so.1.2.11If your debugging started from error messages, choose the path where the library file name includes the same numbers as the messages. If you are unsure, continue with the path where the file name has no extra numbers.
Search for a name and version of the package that provided the file:
$ rpm -qf /usr/lib64/libz.so.1.3.1.zlib-ngzlib-ng-compat-2.2.3-1.el10.x86_64The output provides details for the installed package in the name:epoch-version.release.architecture format.
重要If this step does not produce any results, it is not possible to determine which package provided the binary file. There are several possible cases:
- The file is installed from a package which is not known to package management tools in their current configuration.
-
The file is installed from a locally downloaded and manually installed package. Determining a suitable
debuginfopackage automatically is impossible in that case. - Your package management tools are misconfigured.
-
The file is not installed from any package. In such a case, no corresponding
debuginfopackage exists.
Because further steps depend on this one, you must resolve this situation or stop this procedure. Describing the exact troubleshooting steps is beyond the scope of this procedure.
Install the
debuginfopackages using thednf debuginfo-installutility. In the command, use the package name and other details you determined during the previous step:# dnf debuginfo-install zlib-ng-compat-2.2.3-1.el10.x86_64