How to load multiple symbol files in gdb
. I have a executable foo.out and loading a module bar.so. I have created two symbol files foo.symbol and bar.symbol. Ho
To set the directory containing symbol file use
set debug-file-directory <directory>
and use
show debug-file-directory
to show what currently is set as directory containing symbol files.
Symbol files are read automagically from this directory if their name (without path) is provided by the binary in terms of a debug-link.
To add additional symbols you might use add-symbol-file
.
(as the gdb onlinedocs seem to be unavailable at the moment I quote this here)
add-symbol-file filename address
add-symbol-file filename address [ -readnow ] [ -mapped ]
add-symbol-file filename -ssection address ...
The add-symbol-file command reads additional symbol table information from the file filename. You would use this command when filename has been dynamically loaded (by some other means) into the program that is running. address should be the memory address at which the file has been loaded; gdb cannot figure this out for itself. You can additionally specify an arbitrary number of `-ssection address' pairs, to give an explicit section name and base address for that section. You can specify any address as an expression.
The symbol table of the file filename is added to the symbol table originally read with the symbol-file command. You can use the add-symbol-file command any number of times; the new symbol data thus read keeps adding to the old. To discard all old symbol data instead, use the symbol-file command without any arguments.
Although filename is typically a shared library file, an executable file, or some other object file which has been fully relocated for loading into a process, you can also load symbolic information from relocatable .o files, as long as:
- the file's symbolic information refers only to linker symbols defined in that file, not to symbols defined by other object files,
- every section the file's symbolic information refers to has actually been loaded into the inferior, as it appears in the file, and
- you can determine the address at which every section was loaded, and provide these to the add-symbol-file command.
Some embedded operating systems, like Sun Chorus and VxWorks, can load relocatable files into an already running program; such systems typically make the requirements above easy to meet. However, it's important to recognize that many native systems use complex link procedures (.linkonce section factoring and C++ constructor table assembly, for example) that make the requirements difficult to meet. In general, one cannot assume that using add-symbol-file to read a relocatable object file's symbolic information will have the same effect as linking the relocatable object file into the program in the normal way.
add-symbol-file does not repeat if you press after using it.
You can use the
-mapped' and
-readnow' options just as with the symbol-file command, to change how gdb manages the symbol table information for filename.
add-symbol-file-auto
from Jaakko's answer relies on writing to an external file. This can cause issues if the file cannot be read or written. Here is an implementation that offers the same functionality, using a GDB command defined in Python. Add this to your .gdbinit
:
python
class AddSymbolFileAuto (gdb.Command):
def __init__(self):
super(AddSymbolFileAuto, self).__init__("add-symbol-file-auto", gdb.COMMAND_USER)
def invoke(self, solibpath, from_tty):
offset = self.get_text_offset(solibpath)
gdb_cmd = "add-symbol-file %s %s" % (solibpath, offset)
gdb.execute(gdb_cmd, from_tty)
def get_text_offset(self, solibpath):
import subprocess
# Note: Replace "readelf" with path to binary if it is not in your PATH.
elfres = subprocess.check_output(["readelf", "-WS", solibpath])
for line in elfres.splitlines():
if "] .text " in line:
return "0x" + line.split()[4]
return "" # TODO: Raise error when offset is not found?
AddSymbolFileAuto()
end
Usage example:
add-symbol-file-auto foo.symbol
Note that merely loading the symbols with add-symbol-file can allow you to set breakpoints, but this does not automatically mean that you can do something useful with it, such as actually triggering the breakpoint. Use info sharedlibrary (or info shared
) to verify that the symbols are actually relevant to the debugging target (optionally with a pattern to show specific results instead of all). It should look like this:
(gdb) gdb-symbol-file-auto path/to/library.symbols
(gdb) info shared symbol-file
From To Syms Read Shared Object Library
0x0000abc0 0x0000def0 Yes path/to/library
The following is shown when the loaded symbols are not used:
From To Syms Read Shared Object Library
0x0000abc0 0x0000def0 Yes (*) path/to/library
(*): Shared library is missing debugging information.
The following is shown when GDB is unable to load the library at all (e.g. if GDB has a bug):
From To Syms Read Shared Object Library
0x0000abc0 0x0000def0 No path/to/library
Additional symbols can be loaded to the gdb
debug session with:
add-symbol-file filename address
Parameter address
is the address for .text
section. This address can be retrieved with:
readelf -WS path/to/file.elf | grep .text | awk '{ print "0x"$5 }'
This can be automated in gdb
by adding following entry to ~/.gdbinit
:
define add-symbol-file-auto
# Parse .text address to temp file
shell echo set \$text_address=$(readelf -WS $arg0 | grep .text | awk '{ print "0x"$5 }') >/tmp/temp_gdb_text_address.txt
# Source .text address
source /tmp/temp_gdb_text_address.txt
# Clean tempfile
shell rm -f /tmp/temp_gdb_text_address.txt
# Load symbol table
add-symbol-file $arg0 $text_address
end
After above function definition add-symbol-file-auto
can be used load additional symbols:
(gdb) add-symbol-file-auto path/to/bootloader.elf
add symbol table from file "path/to/bootloader.elf" at
.text_addr = 0x8010400
(gdb) add-symbol-file-auto path/to/application.elf
add symbol table from file "path/to/application.elf" at
.text_addr = 0x8000000
(gdb) break main
Breakpoint 1 at 0x8006cb0: main. (2 locations)
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y <MULTIPLE>
1.1 y 0x08006cb0 in main() at ./source/main.cpp:114
1.2 y 0x080106a6 in main() at ./main.cpp:10
(gdb)
Instead of attempting to load symbols manually into the correct location I have found it more convenient to merge the symbols back to the stripped executables with eu-unstrip
and then reproduce the crash with symbols already present.
This approach does not depend on symbol files matching the naming scheme required by the path resolution mechanisms (debug link and build id) that get used when you set debug-file-directory
.
In addition of the alk's answer and its comments, the address asked is the address of the .text
section.
You can find it by using the readelf
command
Here you have an example of a readelf
use for binary files The address where filename has been loaded is missing [GDB]
add-symbol-file filename address
add-symbol-file filename address [ -readnow ] [ -mapped ]
add-symbol-file filename -ssection address ...
see http://www.delorie.com/gnu/docs/gdb/gdb_125.html