Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting GDB to save a list of breakpoints

People also ask

How do I save breakpoints in GDB?

To save breakpoint definitions to a file use the save breakpoints command. This command saves all current breakpoint definitions together with their commands and ignore counts, into a file filename suitable for use in a later debugging session.

How do I get a list of breakpoints in GDB?

You can see these breakpoints with the GDB maintenance command `maint info breakpoints' . Using the same format as `info breakpoints' , display both the breakpoints you've set explicitly, and those GDB is using for internal purposes. Internal breakpoints are shown with negative breakpoint numbers.

Which command lists the breakpoints in GDB?

b +N - Puts a breakpoint N lines down from the current line. b fn - Puts a breakpoint at the beginning of function "fn" d N - Deletes breakpoint number N. info break - list breakpoints.

What does the LIST command do in GDB?

"list -" lists the ten lines before a previous ten-line listing. One argument specifies a line, and ten lines are listed around that line.


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.


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

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.


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.


Extension to the answer from Johannes: you could automatically reformat the output of info break into a valid GDB command file:

.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

Afterwards you have a valid commandfile in brestore.gdb.

This worked for me when the application is compiled with -g.

I also successfully tested it with GDB v6.8 on Ubuntu 9.10 (Karmic Koala).