views:

1562

answers:

6
+9  Q: 

GDB breakpoints

Hi, I have a list of breakpoints which I want to add each time I debug a particular program. Is there a way I can put all these breakpoint information in a file and use it at the starting of each debug session? In other words can I provide a script file with breakpoint information to GDB before I give the 'run' command.

Thanks in Advance, Sachin

+8  A: 

You can put all of the commands you want into a .gdbinit file that lives in the same directory as the executable you are debugging.

Something like:

b somefile.c:128
b otherfile.c:33

Should work just fine.

Edit: Yes, the -x command line argument will allow you to execute arbitrary files at GDB startup, but maintaining a .gdbinit file for each project means that the file is executed automatically (without the need to specify a filename). Also, you can easily add the project-specific .gdbinit file to your source control, which means that all of your team members can utilize the same debugging facilities.

Sean Bright
+13  A: 

From man gdb(1):

  -x file
           Execute GDB commands from file file.

You could then put your breakpoints in a file:

break [file:]function
break [file:]function
...
Mark Renouf
+3  A: 

Besides using an external file, you can also just keep gdb open: If the binary under gdb changes, it will reload the binary and libraries without losing your breakpoints the next time you run.

Mitch Haile
+1  A: 

Or how about:

gdb --command=commands.gdb ./a.out

where commands.gdb is a textfile with your breakpoints.

/Johan

Update: --command is probably the same as -x

Johan
A: 

The documentation of GDB claims that a command "save breakpoints" and "source" can be used: http://sourceware.org/gdb/current/onlinedocs/gdb/Save-Breakpoints.html#Save-Breakpoints. However this doesn't work on my gdb (7.1-ubuntu).

Lukasz Czerwinski
A: 

The save breakpoints command is new as of gdb 7.2. After you've saved the breakpoints to a file you can read them into a later gdb session using the source 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.
aculich