47

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!

1

1 Answer 1

64

You should take a look at the commands command, which enables you to add a series of GDB commands (i.e. a list of commands) which will be executed when the breakpoint is hit. See the breakpoint command list section of the GDB manual.

For example:

break someFunction
commands
print var1
end

When the breakpoint on someFunction is hit, GDB will execute the command print var1.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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
If say I wand to execute same commands for multiple breakpoints, then how to do it? (without copy pasting;)
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.
@VramVardanian commands takes a list of breakpoint numbers as an argument. Use info b to list the breakpoints.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.