C++ 让 GDB 保存断点列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/501486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting GDB to save a list of breakpoints
提问by casualcoder
OK, info breaklists the breakpoints, but not in a format that would work well with reusing them using the --command as in this question. Does GDB have a method for dumping them into a file acceptable for input again? Sometimes in a debugging session, it is necessary to restart GDB after building up a set of breakpoints for testing.
好的,info break列出了断点,但格式不适合使用 --command 重用它们,如本问题所示。GDB 是否有将它们转储到可接受再次输入的文件中的方法?有时在调试会话中,需要在建立一组断点后重新启动 GDB 进行测试。
The .gdbinit file has the same problem as --command. The info breakcommand does not list commands, but rather a table for human consumption.
.gdbinit 文件与 --command 存在相同的问题。该信息休息命令不会列出命令,而是供人食用的表。
To elaborate, here is a sample from info break:
详细说明,这里是info break的示例:
(gdb) info break Num Type Disp Enb Address What 1 breakpoint keep y 0x08048517 <foo::bar(void)+7>
回答by aculich
As of GDB 7.2 (2011-08-23) you can now use the save breakpointscommand.
从 GDB 7.2 (2011-08-23) 开始,您现在可以使用save breakpoints命令。
save breakpoints <filename>
Save all current breakpoint definitions to a file suitable for use
in a later debugging session. To read the saved breakpoint
definitions, use the `source' command.
Use source <filename>
to restore the saved breakpoints from the file.
用于source <filename>
从文件中恢复保存的断点。
回答by Johannes Schaub - litb
This answer is outdated. GDB now supports saving directly. See this answer.
这个答案已经过时了。GDB 现在支持直接保存。看到这个答案。
You can use logging:
您可以使用日志记录:
(gdb) b main
Breakpoint 1 at 0x8049329
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x08049329 <main+16>
(gdb) set logging file breaks.txt
(gdb) set logging on
Copying output to breaks.txt.
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x08049329 <main+16>
(gdb) q
The file breaks.txt now contains:
文件breaks.txt 现在包含:
Num Type Disp Enb Address What
1 breakpoint keep y 0x08049329 <main+16>
Writing an AWK script that transforms that into a format useful for the .gdbinit
or a --command
file is easy. Or you may even make the script emit separate --eval-command
's to the GDB command line...
编写一个 AWK 脚本将其转换为对.gdbinit
或--command
文件有用的格式很容易。或者您甚至可以让脚本--eval-command
向 GDB 命令行发出单独的's ...
Adding this small macro to .gdbinitwill help you do it:
将这个小宏添加到.gdbinit将帮助您做到这一点:
# Call with dump_breaks file.txt
define dump_breaks
set logging file $arg0
set logging redirect on
set logging on
info breakpoints
set logging off
set logging redirect off
end
回答by Paul Beckingham
Put your GDB commands and breakpoints in a .gdbinit filejust as you might type them at the gdb>
prompt, and GDB will automatically load and run them on startup. This is a per-directory file, so you can have different files for different projects.
将您的 GDB 命令和断点放在.gdbinit 文件中,就像您在gdb>
提示符下键入它们一样,GDB 将在启动时自动加载并运行它们。这是每个目录的文件,因此您可以为不同的项目使用不同的文件。
回答by Dan Berindei
An extension to anon's extensionto Johannes' answer:
.gdbinit:
define bsave
shell rm -f brestore.txt
set logging file brestore.txt
set logging on
info break
set logging off
# Reformat on-the-fly to a valid GDB command file
shell perl -n -e 'print "break \n" if /^\d+.+?(\S+)$/g' brestore.txt > brestore.gdb
end
document bsave
store actual breakpoints
end
define brestore
source brestore.gdb
end
document brestore
restore breakpoints saved by bsave
end
With brestore
you can then restore the breakpoints saved with bsave
.
有了brestore
那么你就可以恢复保存的断点bsave
。
回答by Dan Berindei
Extension to the answer from Johannes: you could automatically reformat the output of info break
into a valid GDB command file:
Johannes 回答的扩展:您可以自动将 的输出重新格式化info break
为有效的 GDB 命令文件:
.gdbinit:
define bsave
shell rm -f brestore.txt
set logging file brestore.txt
set logging on
info break
set logging off
# Reformat on-the-fly to a valid gdb command file
shell perl -n -e 'print "break \n" if /^\d+.+?(\S+)$/g' brestore.txt > brestore.gdb
end
document bsave
store actual breakpoints
end
Afterwards you have a valid commandfile in brestore.gdb
.
之后,您在brestore.gdb
.
This worked for me when the application is compiled with -g
.
当应用程序使用-g
.
I also successfully tested it with GDB v6.8 on Ubuntu 9.10(Karmic Koala).
我还在Ubuntu 9.10(Karmic Koala)上使用 GDB v6.8 成功测试了它。
回答by badeip
Put the following in ~/.gdbinitto define bsaveand brestoreas GDB commands to save- and restore breakpoints.
将以下内容放入~/.gdbinit以将bsave和brestore定义为 GDB 命令以保存和恢复断点。
define bsave
save breakpoints ~/.breakpoints
end
define brestore
source ~/.breakpoints
end
回答by Abel
回答by Magnux
warning: Current output protocol does not support redirection
警告:当前输出协议不支持重定向
I also get this error/warning in GDB when trying to enable logging in TUImode. However, the logging seems to work when in "non-TUI" mode. So I leave TUI mode whenever I want to log something. (Toggle back and forth into TUI mode with Ctrl+ X, Ctrl+ A).
尝试在TUI模式下启用日志记录时,我也在 GDB 中收到此错误/警告。但是,日志记录似乎在“非 TUI”模式下工作。所以每当我想记录一些东西时,我都会离开 TUI 模式。(使用Ctrl+X、Ctrl+A来回切换到 TUI 模式)。
Here's how I work:
这是我的工作方式:
- start GDB (in normal mode)
- enable logging:
set logging on
- now it should not complain. - toggle back/forth to TUI mode and do GDB stuff
- whenever I want to log something (like a huge backtrace dump) - toggle to normal mode
- 启动 GDB(在正常模式下)
- 启用日志记录:
set logging on
- 现在它不应该抱怨。 - 来回切换到 TUI 模式并执行 GDB 操作
- 每当我想记录一些东西(比如一个巨大的回溯转储) - 切换到正常模式
回答by Mark A
I found the following addition to a previous answer useful to save/load the breakpoints to a specific file.
我发现对先前答案的以下补充有助于将断点保存/加载到特定文件。
- Save breakpoints: bsave {filename}
- Load breakpoints: bload {filename}
- 保存断点:bsave {filename}
- 加载断点:bload {filename}
As in the previous answer, add the following code to the file ~/.gdbinit
与上一个答案一样,将以下代码添加到文件 ~/.gdbinit
# Save breakpoints to a file
define bsave
if $argc != 1
help bsave
else
save breakpoints $arg0
end
end
document bsave
Saves all current defined breakpoints to the defined file in the PWD
Usage: bsave <filename>
end
# Loads breakpoints from a file
define bload
if $argc != 1
help bload
else
source $arg0
end
end
document bload
Loads all breakpoints from the defined file in the PWD
Usage: bload <filename>
end
回答by noisy
Any other ideas? I have got
还有其他想法吗?我有
warning: Current output protocol does not support redirection
after
后
set logging on
EDIT:
编辑:
I know that question is "how to save a list of breakpoints", however I just discovered, that with GDB we can simply set "saved in file" breakpoints by
我知道这个问题是“如何保存断点列表”,但是我刚刚发现,使用 GDB 我们可以简单地设置“保存在文件中”断点
gdb> source breakpoints.txt
where breakpoints.txt
is a file like this:
breakpoints.txt
像这样的文件在哪里:
break main.cpp:25
break engine.cpp:465
break wheel.cpp:57