GDB:运行直到特定断点

时间:2012-08-22 21:27:34

标签: c++ c command-line gdb breakpoints

在GDB中调试C ++代码:我策略性地设置了15个断点,但是在我遇到第2个断点之前我不希望它们中的任何一个被激活。 GDB中是否有任何run-until-breakpoint- n 命令?

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

  1. 删除所有其他断点,使#2全部存在,运行,重新添加所有断点;或

  2. 重复continue经过所有休息,直到我看到#2的第一个休息时间。

  3. 我希望run-until 2之类的东西会忽略除#2之外的所有其他断点,但不会删除它们。这存在吗?还有其他人有更好的方法来解决这个问题吗?

4 个答案:

答案 0 :(得分:11)

您可以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

答案 1 :(得分:11)

从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

答案 2 :(得分:3)

比删除所有其他断点稍微痛苦的是disable他们。这样您就不必重新输入与断点关联的所有内容以将其恢复,只需再次按enable编号。

答案 3 :(得分:2)

可以禁用gdb个断点而不是删除。然后,您需要做的就是在需要时重新启用它们。