[Debugging Information in Separate Files]
gdb allows you to put a program's debugging information in a file separate from the executable itself, in a way that allows gdb to find and load the debugging information automatically. Since debugging information can be very large—sometimes larger than the executable code itself—some systems distribute debugging information for their executables in separate files, which users can install only when they need to debug a problem.
gdb supports two ways of specifying the separate debug info file:
Depending on the way the debug info file is specified, gdb uses two different methods of looking for the debug file:
So, for example, suppose you ask gdb to debug /usr/bin/ls, which has a debug link that specifies the file ls.debug, and a build ID whose value in hex is abcdef1234
. If the list of the global debug directories includes /usr/lib/debug, then gdb will look for the following debug information files, in the indicated order:
Global debugging info directories default to what is set by gdb configure option --with-separate-debug-dir. During gdb run you can also set the global debugging info directories, and view the list gdb is currently using.
set debug-file-directory
directories
Set the directories which gdb searches for separate debugging information files to directory. Multiple path components can be set concatenating them by a path separator.
show debug-file-directory
.gnu_debuglink
with the contents described above.
.note.gnu.build-id
, but that name is not mandatory. It contains unique identification for the built files—the ID remains the same across multiple builds of the same build tree. The default algorithm SHA1 produces 160 bits (40 hexadecimal characters) of the content for the build ID string. The same section with an identical value is present in the original built binary with symbols, in its stripped variant, and in the separate debugging information file.
The debugging information file itself should be an ordinary executable, containing a full set of linker symbols, sections, and debugging information. The sections of the debugging information file should have the same names, addresses, and sizes as the original file, but they need not contain any data—much like a .bss
section in an ordinary executable.
The gnu binary utilities (Binutils) package includes the ‘objcopy’ utility that can produce the separated executable / debugging information file pairs using the following commands:
objcopy --only-keep-debug foo foo.debug strip -g foo
These commands remove the debugging information from the executable file foo and place it in the file foo.debug. You can use the first, second or both methods to link the two files:
objcopy --add-gnu-debuglink=foo.debug foo
Ulrich Drepper's elfutils package, starting with version 0.53, contains a version of the strip
command such that the command strip foo -f foo.debug has the same functionality as the two objcopy
commands and the ln -s
command above, together.
ld --build-id
or the gcc counterpart gcc -Wl,--build-id
. Build ID support plus compatibility fixes for debug files separation are present in gnu binary utilities (Binutils) package since version 2.18.参考: https://sourceware.org/gdb/download/onlinedocs/gdb/Separate-Debug-Files.html#Separate-Debug-Files