52

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. How to load both the files into gdb.

# gdb --core core
# (gdb) 
# (gdb) symbol-file foo.symbol

How to load the second symbol file. Or is there any way to load all the files of directory in gdb

0

7 Answers 7

41

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.

5
  • 1
    I tried that but it is not loading the symbol files from said directory Commented Dec 4, 2013 at 18:33
  • Do you load bar.so dynamically? Does it contian a debug link?
    – alk
    Commented Dec 4, 2013 at 18:36
  • Yes bar.so is linking dynamically and does not contain the symbols. I have symbol file of bar.symbol Commented Dec 4, 2013 at 19:05
  • 10
    gdb cannot figure this out the address, how can I know the address? Commented Dec 5, 2013 at 8:47
  • 1
    In my case gdb searched not in the directory set to debug-file-directory itself, but in a subdirectory, which is the absolute path to the original file (bar.so).
    – Alex Che
    Commented Apr 23, 2019 at 18:15
31

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)
2
12

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]

8

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
# Note: Replace "readelf" with path to binary if it is not in your PATH.
READELF_BINARY = 'readelf'

class AddSymbolFileAuto (gdb.Command):
    """Load symbols from FILE, assuming FILE has been dynamically loaded (auto-address).
Usage: add-symbol-file-auto FILE [-readnow | -readnever]
The necessary starting address of the file's text is resolved by 'readelf'."""
    def __init__(self):
        super(AddSymbolFileAuto, self).__init__("add-symbol-file-auto", gdb.COMMAND_FILES)

    def invoke(self, solibpath, from_tty):
        from os import path
        self.dont_repeat()
        if os.path.exists(solibpath) == False:
            print ("{0}: No such file or directory." .format(solibpath))
            return
        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
        elfres = subprocess.check_output([READELF_BINARY, "-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?

    def complete(self, text, word):
        return gdb.COMPLETE_FILENAME

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
2
  • +1, good to have this here. I don't like the tempfile either. I remember looking at Python but I think there was some limitation using python with that specific gdb version.
    – Jaakko
    Commented Jun 20, 2020 at 16:58
  • I've found this script very useful and adjusted it a bit to match add-symbol-file, hoping @rob-w doesn't mind: added help, moved to gdb files (it will be shown next to add-symbol-file in GDB's help), added don't-repeat, added file exist check (for the original one please see the edit). Note: instead of added to .gdbinit this may also by included in anyfile and then source anyfile and/or can be moved to anyfile.py with source anyfile.py - in which case the first line python and last line end are removed. Commented Apr 8, 2021 at 14:52
2

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.

0

While debugging core files, I found that the address obtained using readelf (as mentioned in other answers here) doesn't load the symbols files for shared libraries properly when add-symbol-file command is used.

I instead needed to use the 'From' address shown in command info sharedlibrary/ info shared <libname>:

(gdb) info shared libcore
From        To          Syms Read   Shared Object Library
0x0f24caa0  0x0f41c280  Yes (*)     /lib/libcore.so

After using this address and loading the symbol file, I was able to get line numbers properly.

(gdb) add-symbol-file libcore.so.debug 0x0f24caa0
add symbol table from file "libcore.so.debug" at
    .text_addr = 0xf24caa0
(y or n) y
Reading symbols from libcore.so.debug...

I found this solution after reading this page: Creating and using debug symbol tables with CMake and GDB

-2
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.