37

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 of the breakpoint information in a file and use it at the start of each debug session? In other words can I provide a script file with breakpoint information to GDB before I give the 'run' command?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Sachin Khot
  • 1,412
  • 2
  • 14
  • 18

6 Answers6

32

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
  • 30,697
  • 19
  • 94
  • 123
30

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.

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 use the same debugging facilities.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
13

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mitch Haile
  • 11,716
  • 2
  • 28
  • 25
10

The save breakpoints command is new as of GDB 7.2 (2011-08-23). After you've saved the breakpoints to a file you can read them into a later GDB session using the source command and then the next time you run GDB you can use the -x <filename> option.

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aculich
  • 14,545
  • 9
  • 64
  • 71
3

Or use:

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

where commands.gdb is a text file with your breakpoints.

--command is probably the same as -x

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Johan
  • 20,067
  • 28
  • 92
  • 110
1

The documentation of GDB claims that the commands "save breakpoints" and "source" can be used.

However, this doesn't work on my GDB (Ubuntu 7.10 (Gutsy Gibbon)).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lukasz Czerwinski
  • 13,499
  • 10
  • 55
  • 65
  • 2
    I had the same version and it wasn't working. I found out that this feature is added with gdb version 7.2. If you compile the newer version it works. – Can Bal Dec 22 '11 at 21:37