|Debugging with GDB|
You may want to specify executable and core dump file names. The usual way to do this is at start-up time, using the arguments to gdb's start-up commands (see Getting In and Out of gdb).
Occasionally it is necessary to change to a different file during a
gdb session. Or you may run gdb and forget to
specify a file you want to use. Or you are debugging a remote target
gdbserver (see file). In these situations the gdb commands to specify
new files are useful.
runcommand. If you do not specify a directory and the file is not found in the gdb working directory, gdb uses the environment variable
PATHas a list of directories to search, just as the shell does when looking for a program to run. You can change the value of this variable, for both gdb and your program, using the
You can load unlinked object .o files into gdb using
file command. You will not be able to “run” an object
file, but you can disassemble functions and inspect variables. Also,
if the underlying BFD functionality supports it, you could use
gdb -write to patch object files using this technique. Note
that gdb can neither interpret nor modify relocations in this
case, so branches and some initialized variables will appear to go to
the wrong place. But this feature is still handy from time to time.
filewith no argument makes gdb discard any information it has on both executable file and the symbol table.
exec-file[ filename ]
PATHif necessary to locate your program. Omitting filename means to discard information on the executable file.
symbol-file[ filename ]
PATHis searched when necessary. Use the
filecommand to get both symbol table and program to run from the same file.
symbol-file with no argument clears out gdb information on your
program's symbol table.
symbol-file command causes gdb to forget the contents of
some breakpoints and auto-display expressions. This is because they may
contain pointers to the internal data recording symbols and data types,
which are part of the old symbol table data being discarded inside
symbol-file does not repeat if you press <RET> again after
executing it once.
When gdb is configured for a particular environment, it understands debugging information in whatever format is the standard generated for that environment; you may use either a gnu compiler, or other compilers that adhere to the local conventions. Best results are usually obtained from gnu compilers; for example, using gcc you can generate debugging information for optimized code.
For most kinds of object files, with the exception of old SVR3 systems
using COFF, the
symbol-file command does not normally read the
symbol table in full right away. Instead, it scans the symbol table
quickly to find which source files and which symbols are present. The
details are read later, one source file at a time, as they are needed.
The purpose of this two-stage reading strategy is to make gdb
start up faster. For the most part, it is invisible except for
occasional pauses while the symbol table details for a particular source
file are being read. (The
set verbose command can turn these
pauses into messages if desired. See Optional Warnings and Messages.)
We have not implemented the two-stage strategy for COFF yet. When the
symbol table is stored in COFF format,
symbol-file reads the
symbol table data in full right away. Note that “stabs-in-COFF”
still does the two-stage strategy, since the debug info is actually
in stabs format.
core-file with no argument specifies that no core file is
to be used.
Note that the core file is ignored when your program is actually running
under gdb. So, if you have been running your program and you
wish to debug a core file instead, you must kill the subprocess in which
the program is running. To do this, use the
(see Killing the Child Process).
add-symbol-filefilename address [
add-symbol-filecommand 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. The address should give 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 ‘-s section 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 is kept in addition to the old.
Changes can be reverted using the command
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:
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
.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
add-symbol-file does not repeat if you press <RET> after using it.
add-symbol-filecommand. The file to remove can be identified by its filename or by an address that lies within the boundaries of this symbol file in memory. Example:
(gdb) add-symbol-file /home/user/gdb/mylib.so 0x7ffff7ff9480 add symbol table from file "/home/user/gdb/mylib.so" at .text_addr = 0x7ffff7ff9480 (y or n) y Reading symbols from /home/user/gdb/mylib.so...done. (gdb) remove-symbol-file -a 0x7ffff7ff9480 Remove symbol table from file "/home/user/gdb/mylib.so"? (y or n) y (gdb)
remove-symbol-file does not repeat if you press <RET> after using it.
syscall DSOinto each process's address space; this DSO provides kernel-specific code for some system calls. The argument can be any expression whose evaluation yields the address of the file's shared object file header. For this command to work, you must have used
exec-filecommands in advance.
add-shared-symbol-files command can currently be used only
in the Cygwin build of gdb on MS-Windows OS, where it is an
alias for the
dll-symbols command (see Cygwin Native).
gdb automatically looks for shared libraries, however if
gdb does not find yours, you can invoke
add-shared-symbol-files. It takes one argument: the shared
library's file name.
assf is a shorthand alias for
sectioncommand changes the base address of the named section of the exec file to addr. This can be used if the exec file does not contain section addresses, (such as in the
a.outformat), or when the addresses specified in the file itself are wrong. Each section must be changed separately. The
info filescommand, described below, lists all the sections and their addresses.
info targetare synonymous; both print the current target (see Specifying a Debugging Target), including the names of the executable and core dump files currently in use by gdb, and the files from which symbols were loaded. The command
help targetlists all possible targets rather than current ones.
maint info sections
maint info sections. In addition to the section information displayed by
info files, this command displays the flags and file offset of each section in the executable and core dump files. In addition,
maint info sectionsprovides the following command options (which may be arbitrarily combined):
set trust-readonly-sections on
The default is off.
set trust-readonly-sections off
All file-specifying commands allow both absolute and relative file names as arguments. gdb always converts the file name to an absolute file name and remembers it that way.
On MS-Windows gdb must be linked with the Expat library to support shared libraries. See Expat.
gdb automatically loads symbol definitions from shared libraries
when you use the
run command, or when you examine a core file.
(Before you issue the
run command, gdb does not understand
references to a function in a shared library, however—unless you are
debugging a core file).
On HP-UX, if the program loads a library explicitly, gdb
automatically loads the symbols at the time of the
There are times, however, when you may wish to not automatically load symbol definitions from shared libraries, such as when they are particularly large or there are many of them.
on, symbols from all shared object libraries will be loaded automatically when the inferior begins execution, you attach to an independently started inferior, or when the dynamic linker informs gdb that a new library has been loaded. If mode is
off, symbols must be loaded manually, using the
sharedlibrarycommand. The default value is
If your program uses lots of shared libraries with debug info that takes large amounts of memory, you can decrease the gdb memory footprint by preventing it from automatically loading the symbols from shared libraries. To that end, type set auto-solib-add off before running the inferior, then load each library whose debug symbols you do need with sharedlibrary regexp, where regexp is a regular expression that matches the libraries whose symbols you want to be loaded.
run. If regex is omitted all shared libraries required by your program are loaded.
Sometimes you may wish that gdb stops and gives you control
when any of shared library events happen. The best way to do this is
catch load and
catch unload (see Set Catchpoints).
gdb also supports the the
command for this. This command exists for historical reasons. It is
less useful than setting a catchpoint, because it does not allow for
conditions or commands as a catchpoint does.
Shared libraries are also supported in many cross or remote debugging configurations. gdb needs to have access to the target's libraries; this can be accomplished either by providing copies of the libraries on the host system, or by asking gdb to automatically retrieve the libraries from the target. If copies of the target libraries are provided, they need to be the same as the target libraries, although the copies on the target can be stripped as long as the copies on the host are not.
For remote debugging, you need to tell gdb where the target libraries are, so that it can load the correct copies—otherwise, it may try to load the host's libraries. gdb has two variables to specify the search directories for target libraries.
set sysrootto find shared libraries, they need to be laid out in the same way that they are on the target, with e.g. a /lib and /usr/lib hierarchy under path.
If path starts with the sequence remote:, gdb will
retrieve the target libraries from the remote system. This is only
supported when using a remote target that supports the
command (see Sending files to a remote system).
The part of path following the initial remote:
(if present) is used as system root prefix on the remote file system.
For targets with an MS-DOS based filesystem, such as MS-Windows and SymbianOS, gdb tries prefixing a few variants of the target absolute file name with path. But first, on Unix hosts, gdb converts all backslash directory separators into forward slashes, because the backslash is not a directory separator on Unix:
c:\foo\bar.dll ⇒ c:/foo/bar.dll
Then, gdb attempts prefixing the target file name with path, and looks for the resulting file name in the host file system:
c:/foo/bar.dll ⇒ /path/to/sysroot/c:/foo/bar.dll
If that does not find the shared library, gdb tries removing the ‘:’ character from the drive spec, both for convenience, and, for the case of the host file system not supporting file names with colons:
c:/foo/bar.dll ⇒ /path/to/sysroot/c/foo/bar.dll
This makes it possible to have a system root that mirrors a target with more than one drive. E.g., you may want to setup your local copies of the target system shared libraries like so (note ‘c’ vs ‘z’):
/path/to/sysroot/c/sys/bin/foo.dll /path/to/sysroot/c/sys/bin/bar.dll /path/to/sysroot/z/sys/bin/bar.dll
and point the system root at /path/to/sysroot, so that gdb can find the correct copies of both c:\sys\bin\foo.dll, and z:\sys\bin\bar.dll.
If that still does not find the shared library, gdb tries removing the whole drive spec from the target file name:
c:/foo/bar.dll ⇒ /path/to/sysroot/foo/bar.dll
This last lookup makes it possible to not care about the drive name, if you don't want or need to.
set solib-absolute-prefix command is an alias for
You can set the default system root by using the configure-time ‘--with-sysroot’ option. If the system root is inside gdb's configured binary prefix (set with ‘--prefix’ or ‘--exec-prefix’), then the default system root will be updated automatically if the installed gdb is moved to a new location.
Shared library file names as reported by the target system may not
make sense as is on the system gdb is running on. For
example, when remote debugging a target that has MS-DOS based file
system semantics, from a Unix host, the target may be reporting to
gdb a list of loaded shared libraries with file names such as
c:\Windows\kernel32.dll. On Unix hosts, there's no concept of
drive letters, so the ‘c:\’ prefix is not normally understood as
indicating an absolute file name, and neither is the backslash
normally considered a directory separator character. In that case,
the native file system would interpret this whole absolute file name
as a relative file name with no directory components. This would make
it impossible to point gdb at a copy of the remote target's
shared libraries on the host using
set sysroot, and impractical
set solib-search-path. Setting
dos-based tells gdb
to interpret such file names similarly to how the target would, and to
map them to file names valid on gdb's native file system
semantics. The value of kind can be
"auto", in addition
to one of the supported file system kinds. In that case, gdb
tries to determine the appropriate file system variant based on the
current target's operating system (see Configuring the Current ABI). The supported file system settings are:
When processing file names provided by the user, gdb
frequently needs to compare them to the file names recorded in the
program's debug info. Normally, gdb compares just the
base names of the files as strings, which is reasonably fast
even for very large programs. (The base name of a file is the last
portion of its name, after stripping all the leading directories.)
This shortcut in comparison is based upon the assumption that files
cannot have more than one base name. This is usually true, but
references to files that use symlinks or similar filesystem
facilities violate that assumption. If your program records files
using such facilities, or if you provide file names to gdb
using symlinks etc., you can set
true to instruct gdb to completely canonicalize each
pair of file names it needs to compare. This will make file-name
comparisons accurate, but at a price of a significant slowdown.
 If you want to specify a local system root using a directory that happens to be named remote:, you need to use some equivalent variant of the name like ./remote:.