OK, info break lists 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。
Edit: the .gdbinit file has the same problem as --command. The info break command does not list commands, but rather a table for human consumption.
编辑:.gdbinit文件的问题与—命令相同。info break命令不列出命令,而是一个供人使用的表。
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>
12 个解决方案
#1
170
As of gdb 7.2 you can now use the save breakpoints command.
从gdb 7.2开始,现在可以使用save断点命令。
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.
#2
24
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:
文件了。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或一个命令文件有用的格式。或者,您甚至可以让脚本发出单独的——gdb命令行中的eval命令……
Adding this small macro to .gdbinit will 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
#3
11
Put your gdb commands and breakpoints in a .gdbinit file just 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将自动加载并在启动时运行它们。这是一个目录文件,因此您可以为不同的项目拥有不同的文件。
#4
9
An extension to anon's extension to Johannes' answer:
对anon的答案的延伸:
.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 $1\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保存的断点。
#5
6
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 $1\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编译时,这对我来说是可行的。
EDIT: successfully tested with gdb v6.8 on Ubuntu Karmic.
编辑:在Ubuntu Karmic上成功测试gdb v6.8。
#6
3
Perhaps this:
也许是这样的:
http://sourceware.org/gdb/current/onlinedocs/gdb/Save-Breakpoints.html
http://sourceware.org/gdb/current/onlinedocs/gdb/Save-Breakpoints.html
#7
3
put the following in ~/.gdbinit to define bsave and brestore as gdb commands to save- and restore breakpoints.
把下面的放在~/中。gdbinit定义bsave和brestore作为gdb命令来保存和恢复断点。
define bsave
save breakpoints ~/.breakpoints
end
define brestore
source ~/.breakpoints
end
#8
1
warning: Current output protocol does not support redirection
警告:当前输出协议不支持重定向
I also get this error/warning in GDB when trying to enable logging in TUI mode, however the logging seems to work when in "non-TUI" mode. So I leave TUI mode whenever I want to log someting. (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)
- 启动GDB(正常模式)
- enable logging:
set logging on
- now it should not complain. - 启用日志记录:设置登录——现在它不应该抱怨。
- toggle back/forth to TUI mode and do GDB stuff
- 切换到TUI模式并做GDB之类的事情
- whenver I want to log something (like a huge backtrace dump) - toggle to normal mode
- 当我想要记录一些东西(比如一个巨大的回溯转储)-切换到正常模式
Hope this helps, /M:o)
希望这有助于/ M:o)
#9
1
I know this is an old thread but It came up in my google search to help me do this. I'm new to gdb and found the following addition to the answer above useful to save/load the breakpoints to a specific file.
我知道这是一个旧的线程,但它出现在我的谷歌搜索帮助我做这个。我是gdb的新手,在上面的答案中发现以下的添加对于将断点保存/加载到一个特定的文件非常有用。
- Save breakpoints: bsave {filename}
- 保存断点:bsave {文件名}
- Load breakpoints: bload {filename}
- 负载断点:bload {文件名}
As above 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
#10
0
The problem is that setting a breakpoint is context sensative. What if you have two static functions named foo? If you are already debugging one of the modules that defines foo, then gdb will assume you meant that one. But if you just dump "break foo" into a file and then read that file at start-up, it will not be clear which function foo you mean.
问题是设置断点是上下文敏感的。如果有两个名为foo的静态函数呢?如果您已经在调试一个定义foo的模块,那么gdb将假定您的意思是那个模块。但是,如果您只是将“break foo”转储到一个文件中,然后在启动时读取该文件,就不清楚您指的是哪个函数foo。
#11
0
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 discover, that with gdb we can simply set "saved in file" breakpoints by
我知道这个问题是“如何保存断点列表”,但是我刚刚发现,使用gdb,我们可以简单地设置“保存在文件中”的断点
gdb> source breakpoints.txt
where breakpoints.txt is file like this:
断点的地方。txt是这样的文件:
break main.cpp:25
break engine.cpp:465
break wheel.cpp:57
#12
0
The problem is that setting a breakpoint is context sensative. What if you have two static functions named foo? If you are already debugging one of the modules that defines foo, then gdb will assume you meant that one. But if you just dump "break foo" into a file and then read that file at start-up, it will not be clear which function foo you mean.
问题是设置断点是上下文敏感的。如果有两个名为foo的静态函数呢?如果您已经在调试一个定义foo的模块,那么gdb将假定您的意思是那个模块。但是,如果您只是将“break foo”转储到一个文件中,然后在启动时读取该文件,就不清楚您指的是哪个函数foo。
I don't have the mod points to reply, but what you do is to make your breakpoints explicit, by specifying the source file and line number. If foo() is specified in both foo.c:42, and in bar.c:1337
我没有要回复的mod点,但是您要做的是通过指定源文件和行号使断点显式。如果在两个foo中都指定了foo()。c:42,bar.c:1337
break foo.c:42
break bar.c:1337
Alternatively, specify an in-source breakpoint that only trigger if the program is running under gdb. See How to detect if the current process is being run by GDB?
或者,指定一个只有在程序在gdb下运行时才触发的源内断点。查看如何检测当前进程是否由GDB运行?
#1
170
As of gdb 7.2 you can now use the save breakpoints command.
从gdb 7.2开始,现在可以使用save断点命令。
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.
#2
24
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:
文件了。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或一个命令文件有用的格式。或者,您甚至可以让脚本发出单独的——gdb命令行中的eval命令……
Adding this small macro to .gdbinit will 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
#3
11
Put your gdb commands and breakpoints in a .gdbinit file just 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将自动加载并在启动时运行它们。这是一个目录文件,因此您可以为不同的项目拥有不同的文件。
#4
9
An extension to anon's extension to Johannes' answer:
对anon的答案的延伸:
.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 $1\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保存的断点。
#5
6
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 $1\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编译时,这对我来说是可行的。
EDIT: successfully tested with gdb v6.8 on Ubuntu Karmic.
编辑:在Ubuntu Karmic上成功测试gdb v6.8。
#6
3
Perhaps this:
也许是这样的:
http://sourceware.org/gdb/current/onlinedocs/gdb/Save-Breakpoints.html
http://sourceware.org/gdb/current/onlinedocs/gdb/Save-Breakpoints.html
#7
3
put the following in ~/.gdbinit to define bsave and brestore as gdb commands to save- and restore breakpoints.
把下面的放在~/中。gdbinit定义bsave和brestore作为gdb命令来保存和恢复断点。
define bsave
save breakpoints ~/.breakpoints
end
define brestore
source ~/.breakpoints
end
#8
1
warning: Current output protocol does not support redirection
警告:当前输出协议不支持重定向
I also get this error/warning in GDB when trying to enable logging in TUI mode, however the logging seems to work when in "non-TUI" mode. So I leave TUI mode whenever I want to log someting. (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)
- 启动GDB(正常模式)
- enable logging:
set logging on
- now it should not complain. - 启用日志记录:设置登录——现在它不应该抱怨。
- toggle back/forth to TUI mode and do GDB stuff
- 切换到TUI模式并做GDB之类的事情
- whenver I want to log something (like a huge backtrace dump) - toggle to normal mode
- 当我想要记录一些东西(比如一个巨大的回溯转储)-切换到正常模式
Hope this helps, /M:o)
希望这有助于/ M:o)
#9
1
I know this is an old thread but It came up in my google search to help me do this. I'm new to gdb and found the following addition to the answer above useful to save/load the breakpoints to a specific file.
我知道这是一个旧的线程,但它出现在我的谷歌搜索帮助我做这个。我是gdb的新手,在上面的答案中发现以下的添加对于将断点保存/加载到一个特定的文件非常有用。
- Save breakpoints: bsave {filename}
- 保存断点:bsave {文件名}
- Load breakpoints: bload {filename}
- 负载断点:bload {文件名}
As above 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
#10
0
The problem is that setting a breakpoint is context sensative. What if you have two static functions named foo? If you are already debugging one of the modules that defines foo, then gdb will assume you meant that one. But if you just dump "break foo" into a file and then read that file at start-up, it will not be clear which function foo you mean.
问题是设置断点是上下文敏感的。如果有两个名为foo的静态函数呢?如果您已经在调试一个定义foo的模块,那么gdb将假定您的意思是那个模块。但是,如果您只是将“break foo”转储到一个文件中,然后在启动时读取该文件,就不清楚您指的是哪个函数foo。
#11
0
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 discover, that with gdb we can simply set "saved in file" breakpoints by
我知道这个问题是“如何保存断点列表”,但是我刚刚发现,使用gdb,我们可以简单地设置“保存在文件中”的断点
gdb> source breakpoints.txt
where breakpoints.txt is file like this:
断点的地方。txt是这样的文件:
break main.cpp:25
break engine.cpp:465
break wheel.cpp:57
#12
0
The problem is that setting a breakpoint is context sensative. What if you have two static functions named foo? If you are already debugging one of the modules that defines foo, then gdb will assume you meant that one. But if you just dump "break foo" into a file and then read that file at start-up, it will not be clear which function foo you mean.
问题是设置断点是上下文敏感的。如果有两个名为foo的静态函数呢?如果您已经在调试一个定义foo的模块,那么gdb将假定您的意思是那个模块。但是,如果您只是将“break foo”转储到一个文件中,然后在启动时读取该文件,就不清楚您指的是哪个函数foo。
I don't have the mod points to reply, but what you do is to make your breakpoints explicit, by specifying the source file and line number. If foo() is specified in both foo.c:42, and in bar.c:1337
我没有要回复的mod点,但是您要做的是通过指定源文件和行号使断点显式。如果在两个foo中都指定了foo()。c:42,bar.c:1337
break foo.c:42
break bar.c:1337
Alternatively, specify an in-source breakpoint that only trigger if the program is running under gdb. See How to detect if the current process is being run by GDB?
或者,指定一个只有在程序在gdb下运行时才触发的源内断点。查看如何检测当前进程是否由GDB运行?