44

I'd like to predefine some breakpoints in a gdb script and to invoke some special commands at these breakpoints and afterwards to automatically continue the program execution. So, ideally, I'd like to have a gdb script like the following:

b someFunction
...
if breakpoint from above reached do:
  print var1
  call someOtherFunction
  continue
done

Additionally an unfortunate fact is, that I can't rely on the python interface for using breakpoints, as the gdb version at the server I currently work at is too old!

Lord Bo
  • 3,152
  • 3
  • 15
  • 22
  • 1
    possible duplicate of [Do specific action when certain breakpoint hits in gdb](http://stackoverflow.com/questions/6517423/do-specific-action-when-certain-breakpoint-hits-in-gdb) – Ciro Santilli OurBigBook.com Jun 13 '15 at 21:43

1 Answers1

62

You should take a look at the command command, which enables you to add gdb commands as a breakpoint is hit. See the breakpoint command list section of the gdb manual.

For example:

break someFunction
commands
print var1
end

will, when the breakpoint on someFunction is hit, automatically print var1.

borrible
  • 17,120
  • 7
  • 53
  • 75
  • 12
    Thank You, that was the key! One little additional remark: If You have extensive output by using such a command and do not want it to be stopped everytime it hits the bottom of the terminal (because then gdb will ask "Type to continue, or q to quit"), just state "set pagination off" in gdb or your script. – Lord Bo Dec 18 '12 at 19:37
  • 4
    Note this doesn't work in non interactive mode (--batch or MI mode for example) until https://sourceware.org/bugzilla/show_bug.cgi?id=10079 is fixed – pixelbeat Oct 15 '15 at 00:20
  • 1
    If say I wand to execute same commands for multiple breakpoints, then how to do it? (without copy pasting;) – Vram Vardanian Apr 21 '17 at 08:00
  • 2
    Additional remark: begin and end your command list with `silent` and `cont`: `silent` skips the usual output GDB shows on breakpoint hit, `cont` skips breaking into the interactive prompt; continues after playing your command list. Some call this a _tracepoint_ i.e. just trace values of a variable without stopping execution. – legends2k Dec 14 '18 at 11:31
  • 2
    @VramVardanian `commands` takes a list of breakpoint numbers as an argument. Use `info b` to list the breakpoints. – remcycles Jun 24 '19 at 16:56