Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB: Run until specific breakpoint

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?

like image 569
sligocki Avatar asked Aug 22 '12 21:08

sligocki


People also ask

How do you run until breakpoint?

Just press c. It will continue execution until the next breakpoint.

How do I break a specific line in GDB?

Setting breakpoints A breakpoint is like a stop sign in your code -- whenever gdb gets to a breakpoint it halts execution of your program and allows you to examine it. To set breakpoints, type "break [filename]:[linenumber]". For example, if you wanted to set a breakpoint at line 55 of main.

What is one command that will run the program until the new breakpoint that we just created?

Controlling Executioncontinue (shorthand: c ) will resume execution until the next breakpoint is hit or the program exits.


1 Answers

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
like image 179
alex vasi Avatar answered Oct 08 '22 21:10

alex vasi