手册中有很好的记录,即gdb在退出后(https://sourceware.org/gdb/onlinedocs/gdb/Command-History.html)写入命令历史记录。但是,我希望获得完整的命令历史记录,而不需要根据需要将其填充到一个scratch缓冲区中,以便在另一个gdb实例中使用gdb -x FILE
编辑或方便地从新with内部重新运行。
有什么方法可以从运行中的gdb实例中获取信息?
发布于 2022-10-27 07:38:54
由于GDB 9可以使用pipe
命令(即记录在这里 )和一些基本的shell命令来实现您想要的结果。
$ gdb -q
(gdb) p 1
$1 = 1
(gdb) p 2
$2 = 2
(gdb) pipe show commands | sed -e 's/[[:space:]]\+[0-9]\+[[:space:]]\+//' | head -n -1 | tee /tmp/commands
p 1
p 2
(gdb) q
$ cat /tmp/commands
p 1
p 2
GDB的pipe
命令安排将命令输出发送到shell命令。我通过sed
传递输出以去掉命令索引,然后通过head
删除最后一个命令,这将是当前正在运行的pipe
命令,最后,我使用tee
将输出发送到文件。
注释中指出,show commands
只显示最后10个命令。然而,show commands
也采用了一些参数,这些参数是记录在这里。
这样,我们就可以使用Python向GDB添加一个新命令。这是show-all-commands.py
的内容
import re
class ShowAllCommands(gdb.Command):
"""
show all-commands
Show GDB's complete command history. Unlike 'show commands' this
lists everything in GDB's command history.
"""
def __init__(self):
super().__init__("show all-commands", gdb.COMMAND_OBSCURE)
def invoke(self, arg, from_tty):
start = 1
last_command_number = 0
all_commands = []
get_more = True
while get_more:
found_new_line = False
output = gdb.execute(f"show commands {start}", False, True)
for line in output.splitlines():
g = re.search(r'^\s+(\d+)', line).group(1)
if not g:
break
if int(g) <= last_command_number:
continue
last_command_number = int(g)
all_commands.append(line)
found_new_line = True
if not found_new_line:
break
start = "+"
for line in all_commands:
print(line)
ShowAllCommands()
然后在GDB (或来自.gdbinit文件)中,我们可以这样做:
source show-all-commands.py
您可能需要在source
行中添加到Python的路径,这样GDB才能找到它。
然后,就像以前一样,我们可以使用新命令:
pipe show all-commands | sed -e 's/[[:space:]]\+[0-9]\+[[:space:]]\+//' | head -n -1 | tee /tmp/commands
如果您感觉非常敏锐,那么您当然可以使新的Python命令变得更聪明,也许它可以直接将命令写入输出文件,但我将其作为一个练习留给读者。
发布于 2022-10-27 03:43:24
有什么方法可以从运行中的gdb实例中获取信息?
您可以看到使用(gdb) show commands
的历史。
可以使用以下方法将输出保存到文件中:
(gdb) set logging file /tmp/gdb-history
(gdb) set logging on
(gdb) show commands
(gdb) set logging off
不幸的是,历史记录中有序列号,在使用gdb -x /tmp/gdb-history
之前,您必须去掉序列号。
https://stackoverflow.com/questions/74214750
复制相似问题