C++ – How to reload a recompiled binary in gdb without exiting and losing breakpoints

cgdb

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 589.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 589.cpp, line 204.
(gdb) r
Starting program: /home/code/589

Breakpoint 1, main () at 589.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/589' 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 589
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 "589"? (y or n) y
Reading symbols from 589...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.

Best Answer

When I was using gdb 5, using just 'run' after recompilation was enough to reload the symbols. Now, with gdb 8.1, I need to type 'file executable' before 'run' in order to force gdb to reload the symbols after recompilation.

Related Topic