如何修改gdb中的断点行?

How to modify the line of a breakpoint in gdb?

我设置了一个断点并设置了它的条件和一些其他命令。现在我意识到我应该把它设置在前面几行。如何在不删除断点并丢失其设置的情况下更改断点行?

How can I change the line of the breakpoint without deleting it and losing its settings?

你不能。

你可以做的是使用 save breakpoints /tmp/bp.txt 命令保存所有断点的当前设置,编辑 /tmp/bp.txt 文件更新行信息(或其他任何东西),最后 delete 删除当前断点并 source /tmp/bp.txt 重新加载它们。

考虑到您可能会想在很多场合这样做,我建议在您的 .gdbinit 中添加以下内容:

define loadbp
  delete breakpoints
  source .gdbbp
end
document loadbp
  Set stored breakpoints after deleting any current breakpoints.

The breakpoints to load (set) are expected in ./.gdbbp which is the file created by savebp, but can
be edited manually.

Some breakpoints may not be set because the needed shared object hasn't been loaded yet.  gdb
doesn't prompt when such breakpoint commands are not set interactively.  Consequently, it may be
necessary to run this command again, once the shared object has been loaded.  To account for cases
in which shared objects are loaded automatically by the dynamic loader, this command also sets a
breakpoint in main.  This ensures that there is an early opportunity to call this command again to
set shared object breakpoints.
end

define savebp
  save breakpoints .gdbbp
end
document savebp
  Store current breakpoints in .gdbbp for retrieval using loadbp.
end

要使用这些命令,您需要获取 .gdbinit 或重新启动 gdb。然后,在 gdb 命令提示符处键入 savebp 并按 Enter,根据需要编辑 ./.gdbbp,然后键入 loadbp 并在 gdb 命令提示符处按 Enter。

请注意,如所写,这些命令相对于当前目录保存和加载 .gdbbp。通常,这是您开始 gdb 的目录,但您可以在 gdb 中更改它,因此请注意文件的保存位置。 (您可以在 gdb 命令提示符下 运行 pwd 命令来查看当前目录是什么。)