简体   繁体   English

GDB:运行直到特定断点

[英]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. 在GDB中调试C ++代码:我策略性地设置了15个断点,但我不希望它们中的任何一个在我遇到断点#2之前激活。 Is there any run-until-breakpoint- n command in GDB? GDB中是否有任何run-until-breakpoint- n命令?

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; 删除所有其他断点,使#2全部存在,运行,重新添加所有断点; or 要么

  2. Run and repeatedly continue past all breaks until I see the first break at #2. 跑步并反复continue经过所有休息,直到我看到#2的第一个休息时间。

I want something like run-until 2 that will ignore all other breakpoints except #2, but not delete them. 我想要像run-until 2那样忽略除#2之外的所有其他断点,但不删除它们。 Does this exist? 这存在吗? Does anyone else have a better way to deal with this? 有没有其他人有更好的方法来解决这个问题?

You can enable and disable breakpoints, and these commands will accept a range . 您可以enabledisable断点,这些命令将接受范围 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. 我假设当你提到断点#2时,你指的是断点的gdb编号。 Here is a simple example gdb session: 这是一个简单的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) 

Now only breakpoint #2 is enabled. 现在只启用断点#2。 Run the program and when execution breaks at #2, re-enable all of your desired breakpoints with a range: 运行程序,当执行在#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

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

As of version 7.0 GDB supports python scripting. 从7.0版开始,GDB支持python脚本。 I wrote a simple script that will temporary disable all enabled breakpoints except the one with specified number and execute GDB run command. 我写了一个简单的脚本,它将临时禁用所有已启用的断点,除了具有指定编号的断点并执行GDB run命令。

Add the following code to the .gdbinit file: 将以下代码添加到.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

Slightly less painful than deleting all the other breakpoints would be to disable them. 比删除所有其他断点稍微痛苦的是disable它们。 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. 这样您就不必重新输入与断点关联的所有内容来将其恢复,只需按编号再次enable它。

With gdb breakpoints can be disabled instead of deleted. 使用gdb断点可以禁用而不是删除。 Then all you have to do is reenable them when needed. 然后,您需要做的就是在需要时重新启用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM