GDB:运行到特定的断点

在GDB调试C ++代码:我有15个断点战略设置,但我不希望他们中的任何人激活,直到我打断点#2。 GDB中是否有run-until-breakpoint-n命令?

我发现自己做了以下两件事之一:

  • 删除所有其他断点,以使#2完全存在,运行,重新添加所有断点; 要么

  • 运行并反复continue过去,直到我在#2看到第一个休息。

  • 我想要类似run-until 2 ,它会忽略除#2之外的所有其他断点,但不会删除它们。 这是否存在? 其他人是否有更好的方法来处理这个问题?


    从版本7.0开始,GDB支持python脚本。 我写了一个简单的脚本,它将暂时禁用除指定编号以外的所有已启用断点并执行GDB run命令。

    将下面的代码添加到.gdbinit文件中:

    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
    

    您可以enabledisable断点,并且这些命令将接受一个范围。 在程序执行过程中的关键点,使用这些命令和范围。

    我认为当你提到断点2时,你指的是断点的gdb编号。 这是一个简单的gdb会话示例:

    (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) 
    

    现在只有断点2被启用。 运行该程序,并在#2执行中断时,使用范围重新启用所有您想要的断点:

    (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
    

    您还可以混合使用断点编号和范围:

    (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
    

    比删除所有其他断点稍微痛苦一点就是disable它们。 这样,您不必重新输入与断点关联的所有内容以将其恢复,只需通过数字重新enable

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

    上一篇: GDB: Run until specific breakpoint

    下一篇: Getting gdb to save a list of breakpoints?