0

I have a file ~/.gdb_bps containing GDB breakpoints. I generated this file with save breakpoints .gdb_bps. I'm trying to source this file when GDB starts by adding this line to ~/.gdbinit:

source .gdb_bps

When I start GDB I get the error:

No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]

However sourcing during the GDB session works as expected:

(gdb) source .gdb_bps
Breakpoint 1 at 0x401227: file test/varargs2_test.c, line 22.
Breakpoint 2 at 0x4012a3: file test/varargs2_test.c, line 27.
Breakpoint 3 at 0x40117b: file test/varargs2_test.c, line 9.
Breakpoint 4 at 0x401256: file test/varargs2_test.c, line 25.

My question is why is source .gdb_bps giving an error when using it in ~/.gdbinit?

builder-7000
  • 7,131
  • 3
  • 19
  • 43

2 Answers2

1

~/.gdbinit is run before the object file in the command line is processed.

Although it requires a bit of setup, saving your breakpoints as a gdb script that can be auto-loaded might be the cleanest way to do this.

First, create a directory to hold the scripts, and add the appropriate lines to ~/.gdbinit.

~/devel$ mkdir ~/gdbscripts
~/devel$ cat ~/.gdbinit
add-auto-load-safe-path /home/mp/gdbscripts
add-auto-load-scripts-directory /home/mp/gdbscripts
set debug auto-load on

We'll remove the last line after things are working.

Now let's see where gdb looks for its script files.

~/devel$ gdb -q sigw
Reading symbols from sigw...done.
auto-load: Attempted file "/home/mp/devel/sigw-gdb.gdb" does not exist.
auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load:/home/mp/gdbscripts".
auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load:/home/mp/gdbscripts".
auto-load: Attempted file "/usr/lib/debug/home/mp/devel/sigw-gdb.gdb" does not exist.
auto-load: Attempted file "/usr/share/gdb/auto-load/home/mp/devel/sigw-gdb.gdb" does not exist.
auto-load: Attempted file "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb" does not exist.
auto-load: Attempted file "/home/mp/devel/sigw-gdb.py" does not exist.
auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load:/home/mp/gdbscripts".
auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load:/home/mp/gdbscripts".
auto-load: Attempted file "/usr/lib/debug/home/mp/devel/sigw-gdb.py" does not exist.
auto-load: Attempted file "/usr/share/gdb/auto-load/home/mp/devel/sigw-gdb.py" does not exist.
auto-load: Attempted file "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.py" does not exist.

There are a couple good candidates there, some of which are only writable by root and some of which you'd need to add to your safe-path. Let's use ~/gdbscripts/$PWD/sigw-gdb.gdb.

(gdb) shell mkdir -p ~/gdbscripts/$PWD
(gdb) b main
Breakpoint 1 at 0x84f: file sigw.c, line 15.
(gdb) save breakpoints ~/gdbscripts/$PWD/sigw-gdb.gdb
Unable to open file '/home/mp/gdbscripts/$PWD/sigw-gdb.gdb' for saving (No such file or directory)

It looks like the save command expands ~ but doesn't expand environment variables. We'll have to use the full path.

(gdb) save breakpoints ~/gdbscripts/home/mp/devel/sigw-gdb.gdb
Saved to file '/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb'.

Test it out.

(gdb) quit
~/devel$ gdb -q sigw
Reading symbols from sigw...done.
...
auto-load: Matching file "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb" to pattern "/home/mp/gdbscripts"
auto-load: Matched - file "/home/mp/gdbscripts" to pattern "/home/mp/gdbscripts".
auto-load: File "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb" matches directory "/home/mp/gdbscripts".
Breakpoint 1 at 0x84f: file sigw.c, line 15.
...
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x000000000000084f in main at sigw.c:15
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40
0

One could load the breakpoints after .gdbinit has been processed. This can be accomplished with the -x option:

$ gdb -x .gdb_bps program
Reading symbols from test/varargs2_test...
Breakpoint 1 at 0x40117b: file test/varargs2_test.c, line 9.
Breakpoint 2 at 0x4011b2: file test/varargs2_test.c, line 12.
(gdb) 

To avoid typing the option define the bash function:

gdb() (
    [ -f .gdb_bps ] && xopt="-x .gdb_bps" || xopt=
    command gdb -q $@ $xopt
)

and use it like gdb program

builder-7000
  • 7,131
  • 3
  • 19
  • 43
  • In what sequence will each of these be loaded: `.gdbinit`, sources given by `-x`, and the executable (and its symbols) to debug? This might explain *why* the OP's try didn't work. – the busybee Dec 13 '19 at 08:00
  • .gdbinit is loaded before the file specified by `-x`. The GDB initialization process is described here: https://sourceware.org/gdb/onlinedocs/gdb/Startup.html – builder-7000 Dec 13 '19 at 16:00