gdb scripting: execute commands at selected breakpoint

30,060

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.

Share:
30,060
Lord Bo
Author by

Lord Bo

Updated on December 19, 2020

Comments

  • Lord Bo
    Lord Bo over 3 years

    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
    Lord Bo over 11 years
    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 <return> to continue, or q <return> to quit"), just state "set pagination off" in gdb or your script.
  • pixelbeat
    pixelbeat over 8 years
    Note this doesn't work in non interactive mode (--batch or MI mode for example) until sourceware.org/bugzilla/show_bug.cgi?id=10079 is fixed
  • Vram Vardanian
    Vram Vardanian about 7 years
    If say I wand to execute same commands for multiple breakpoints, then how to do it? (without copy pasting;)
  • legends2k
    legends2k over 5 years
    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.
  • remcycles
    remcycles about 5 years
    @VramVardanian commands takes a list of breakpoint numbers as an argument. Use info b to list the breakpoints.