How to reload a recompiled binary in gdb without exiting and losing breakpoints?
Here is a script I use in gdb 8.3 (slightly adapted for this answer):
define make
shell make
python gdb.execute("file " + gdb.current_progspace().filename)
# clear cache
directory
end
You need to have gdb w/Python.
Note the directory
command which updates the source files cache.
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.
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.