Getting GDB to save a list of breakpoints
Solution 1
As of GDB 7.2 (2011-08-23) you can now use the save breakpoints command.
save breakpoints <filename>
Save all current breakpoint definitions to a file suitable for use
in a later debugging session. To read the saved breakpoint
definitions, use the `source' command.
Use source <filename>
to restore the saved breakpoints from the file.
Solution 2
This answer is outdated. GDB now supports saving directly. See this answer.
You can use logging:
(gdb) b main
Breakpoint 1 at 0x8049329
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x08049329 <main+16>
(gdb) set logging file breaks.txt
(gdb) set logging on
Copying output to breaks.txt.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x08049329 <main+16>
(gdb) q
The file breaks.txt now contains:
Num Type Disp Enb Address What
1 breakpoint keep y 0x08049329 <main+16>
Writing an AWK script that transforms that into a format useful for the .gdbinit
or a --command
file is easy. Or you may even make the script emit separate --eval-command
's to the GDB command line...
Adding this small macro to .gdbinit will help you do it:
# Call with dump_breaks file.txt
define dump_breaks
set logging file $arg0
set logging redirect on
set logging on
info breakpoints
set logging off
set logging redirect off
end
Solution 3
Put your GDB commands and breakpoints in a .gdbinit file just as you might type them at the gdb>
prompt, and GDB will automatically load and run them on startup. This is a per-directory file, so you can have different files for different projects.
Solution 4
An extension to anon's extension to Johannes' answer:
.gdbinit:
define bsave
shell rm -f brestore.txt
set logging file brestore.txt
set logging on
info break
set logging off
# Reformat on-the-fly to a valid GDB command file
shell perl -n -e 'print "break $1\n" if /^\d+.+?(\S+)$/g' brestore.txt > brestore.gdb
end
document bsave
store actual breakpoints
end
define brestore
source brestore.gdb
end
document brestore
restore breakpoints saved by bsave
end
With brestore
you can then restore the breakpoints saved with bsave
.
Solution 5
Put the following in ~/.gdbinit to define bsave and brestore as GDB commands to save- and restore breakpoints.
define bsave
save breakpoints ~/.breakpoints
end
define brestore
source ~/.breakpoints
end
casualcoder
Updated on July 08, 2022Comments
-
casualcoder almost 2 years
OK, info break lists the breakpoints, but not in a format that would work well with reusing them using the --command as in this question. Does GDB have a method for dumping them into a file acceptable for input again? Sometimes in a debugging session, it is necessary to restart GDB after building up a set of breakpoints for testing.
The .gdbinit file has the same problem as --command. The info break command does not list commands, but rather a table for human consumption.
To elaborate, here is a sample from info break:
(gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x08048517 <foo::bar(void)+7>
-
casualcoder over 15 yearsOne could just as easily use cut-and-paste, but the scripting method seems to be the way to go.
-
Johannes Schaub - litb over 15 yearsi don't think cut-and-paste is easier than just writing a script once, then using it every time again :) after all, that was the very reason you asked this question in the first place, i think :)
-
casualcoder over 15 yearsUm, I meant use cut-and-paste instead of the logging method. Scripting is it so far for sure.
-
deft_code over 14 yearswow! gdb fail! I use it everyday and love many of its features. But lack is just plain dumb.
-
casualcoder almost 14 yearsThis actually fails to work, I get "warning: save-tracepoints: no tracepoints to save.' This despite breakpoints being set. Using gdb 6.8.
-
Magnux almost 14 yearsOh, and if you like using "screen" (like I do) it will get a bit messy, since it uses the same hotkeys.
-
aculich over 13 yearsThis answer is now over 2 years old so it may be obsolete if you are using a newer version of gdb. As of gdb 7.2 you can now use the
save breakpoints
command. -
George Godik over 12 yearsHere's a better regex : perl -ne "print \"break \$1 \n\" if /at\s(.*:\d+)/" brestore.txt
-
bjackfly over 10 yearswhat about if they are from a shared lib load? It answers N by default it seems...
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]
-
aculich over 10 years@bjackfly use
set breakpoint pending on
as described in how to answer Y in gdb script and gdb: how to set breakpoints on future shared libraries with a --command flag -
mMontu almost 10 yearsThis answer is still useful to those of us who need to work with legacy systems.
-
Lekensteyn over 9 yearsNote that when you have a breakpoint condition which cannot be resolved at startup (
break g_log if log_level==G_LOG_LEVEL_CRITICAL
), then at least gdb 7.8.1 will stop parsing further commands. If you have additional commands which should be executed for that breakpoint, put thecommands
line before thecondition
line. -
aculich over 9 years@Andry I rolled back your edit to my original blockquote because the text is a verbatim quote from the documentation... I would otherwise agree with your edit if it were my own words instead.
-
Andry over 9 years@aculich: I see. I would recommend to use the quoting style rather than the code style in any case.
-
truthadjustr about 7 yearsThis works for me. GDB needs the presence of a global .gdbinit in your $HOME/.gdbinit with content 'add-auto-load-safe-path /home/johnny/src/.gdbinit' and thus the src/ folder has also a separate .gdbinit
-
truthadjustr about 7 yearsThe .gdbinit does not work with 'cgdb' so I am fine with the explicit 'save breakpoints /tmp/pointbreak.dbg' and it's opposite 'source /tmp/pointbreak.dbg'
-
Peter Mortensen over 4 yearsThis has already been covered in the accepted answer.