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:

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

  • 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?


    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
    

    You can enable and disable breakpoints, and these commands will accept a range. Use these commands, with a range, at strategic points during the execution of the program.

    I assume that when you mention breakpoint #2 you're referring to the gdb numbering of breakpoints. Here is a simple example gdb session:

    (gdb) info breakpoints
    Num Type           Disp Enb Address    What
    1   breakpoint     keep y   0x00001ddb in main at example.c:34
    2   breakpoint     keep y   0x00001e00 in main at example.c:39
    3   breakpoint     keep y   0x00001e15 in main at example.c:40
    (gdb) disable 1-3
    (gdb) enable 2
    (gdb) info breakpoints
    Num Type           Disp Enb Address    What
    1   breakpoint     keep n   0x00001ddb in main at example.c:34
    2   breakpoint     keep y   0x00001e00 in main at example.c:39
    3   breakpoint     keep n   0x00001e15 in main at example.c:40
    (gdb) 
    

    Now only breakpoint #2 is enabled. Run the program and when execution breaks at #2, re-enable all of your desired breakpoints with a range:

    (gdb) enable 1-3
    (gdb) info breakpoints
    Num Type           Disp Enb Address    What
    1   breakpoint     keep y   0x00001ddb in main at example.c:34
    2   breakpoint     keep y   0x00001e00 in main at example.c:39
    3   breakpoint     keep y   0x00001e15 in main at example.c:40
    

    You can also mix breakpoint numbers and ranges:

    (gdb) disable 1 4 6-7
    (gdb) info breakpoints
    Num Type           Disp Enb Address    What
    1   breakpoint     keep n   0x00001ddb in main at example.c:34
    2   breakpoint     keep y   0x00001e00 in main at example.c:39
    3   breakpoint     keep y   0x00001e15 in main at example.c:40
    4   breakpoint     keep n   0x00001e4f in main at example.c:43
    5   breakpoint     keep y   0x00001e4f in main at example.c:44
    6   breakpoint     keep n   0x00001e5e in main at example.c:45
    7   breakpoint     keep n   0x00001e5e in main at example.c:46
    

    Slightly less painful than deleting all the other breakpoints would be to disable them. That way you don't have to reenter all the stuff associated with the breakpoint to bring it back, just enable it again by number.

    链接地址: http://www.djcxy.com/p/24312.html

    上一篇: 如何在Python中生成列表的所有排列

    下一篇: GDB:运行到特定的断点