Search code examples
c++gdb

How to reload a recompiled binary in gdb without exiting and losing breakpoints?


According to this excellent guide one should be able to recompile a source file and simply use 'r' to have gdb begin debugging the new, changed binary.

This also seemed implied in the gdb manual by "If the modification time of your symbol file has changed since the last time GDB read its symbols, GDB discards its symbol table, and reads it again."

I am trying to debug a simple, single .cpp file on Ubuntu 16.10. After compiling via g++ -ggdb -std=c++11 foo.cpp, I can debug as usual.

GNU gdb (Ubuntu 7.11.90.20161005-0ubuntu2) 7.11.90.20161005-git
[...]
(gdb) break main
Breakpoint 1 at 0x2754: file foo.cpp, line 204.
(gdb) r
Starting program: /home/code/foo

Breakpoint 1, main () at foo.cpp:204
(gdb) n
(gdb) k
Kill the program being debugged? (y or n) y

Here, I make a minor change to source file and then recompile. When trying to run the file again:

(gdb) r
/home/code/foo' has changed; re-reading symbols.
Error in re-setting breakpoint 1: Cannot access memory at address 0x55555555674b
Starting program: /home/code/598
warning: Probes-based dynamic linker interface failed.
Reverting to original interface.

[Inferior 1 (process 20898) exited normally]

Is there a way to successfully reload the binary while keeping my breakpoints intact?

EDIT: This post had the answer I was looking for. You reload the executable with the file binaryname command.

(gdb) file foo
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
A program is being debugged already.
Load new symbol table from "foo"? (y or n) y
Reading symbols from foo...done.
Error in re-setting breakpoint 1: Cannot access memory at address 0x274b
Error in re-setting breakpoint 2: Cannot access memory at address 0x274b

We see the breakpoints are still there, just disabled:

(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep n   0x0000555555556754
        breakpoint already hit 1 time
2       breakpoint     keep n   0x000055555555677b 

And so we just enable them:

(gdb) enable
(gdb) i b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000555555556754 
        breakpoint already hit 1 time
2       breakpoint     keep y   0x000055555555677b
(gdb) 

This works, but I would love to hear if anyone has further advice or input on whether simply using run should indeed work.


Solution

  • The issue specifically with breakpoints and PIE seems to have been fixed in gdb 8.3.1 - see https://www.gnu.org/software/gdb/news/ and PR 25011.

    Since the issue is due to position-independent executables (PIE), relinking the program with -no-pie should also get around it.

    The issue that got me to this question was that automatic reloading of symbols seemed to have been broken in new gdb, but it seems that change was not in gdb but rather that Linux distributions started enabling PIE by default in gcc. Linking with -no-pie also fixed symbol reloading for me.