C++ – GDB: Run until specific breakpoint

breakpointsccommand linegdb

In GDB debugging C++ code: I have 15 breakpoints strategically set, but I don't want any of them to activate until I've hit breakpoint #2. Is there any run-until-breakpoint-n command in GDB?

I find myself doing one of two things instead:

  1. Delete all other breakpoints so that #2 is all that exists, run, re-add all breakpoints; or

  2. Run and repeatedly continue past all breaks until I see the first break at #2.

I want something like run-until 2 that will ignore all other breakpoints except #2, but not delete them. Does this exist? Does anyone else have a better way to deal with this?

Best Answer

As of version 7.0 GDB supports python scripting. I wrote a simple script that will temporary disable all enabled breakpoints except the one with specified number and execute GDB run command.

Add the following code to the .gdbinit file:

python
import gdb

class RunUntilCommand(gdb.Command):
    """Run until breakpoint and temporary disable other ones"""

    def __init__ (self):
        super(RunUntilCommand, self).__init__ ("run-until",
                                               gdb.COMMAND_BREAKPOINTS)

    def invoke(self, bp_num, from_tty):
        try:
            bp_num = int(bp_num)
        except (TypeError, ValueError):
            print "Enter breakpoint number as argument."
            return

        all_breakpoints = gdb.breakpoints() or []
        breakpoints = [b for b in all_breakpoints
                       if b.is_valid() and b.enabled and b.number != bp_num and
                       b.visible == gdb.BP_BREAKPOINT]

        for b in breakpoints:
            b.enabled = False

        gdb.execute("run")

        for b in breakpoints:
            b.enabled = True

RunUntilCommand()
end