首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在退出前获得gdb完整的命令历史?

如何在退出前获得gdb完整的命令历史?
EN

Stack Overflow用户
提问于 2022-10-26 22:08:25
回答 2查看 53关注 0票数 0

手册中有很好的记录,即gdb在退出后(https://sourceware.org/gdb/onlinedocs/gdb/Command-History.html)写入命令历史记录。但是,我希望获得完整的命令历史记录,而不需要根据需要将其填充到一个scratch缓冲区中,以便在另一个gdb实例中使用gdb -x FILE编辑或方便地从新with内部重新运行。

有什么方法可以从运行中的gdb实例中获取信息?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-27 07:38:54

由于GDB 9可以使用pipe命令(即记录在这里 )和一些基本的shell命令来实现您想要的结果。

代码语言:javascript
运行
复制
$ 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的内容

代码语言:javascript
运行
复制
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文件)中,我们可以这样做:

代码语言:javascript
运行
复制
source show-all-commands.py

您可能需要在source行中添加到Python的路径,这样GDB才能找到它。

然后,就像以前一样,我们可以使用新命令:

代码语言:javascript
运行
复制
pipe show all-commands | sed -e 's/[[:space:]]\+[0-9]\+[[:space:]]\+//' | head -n -1 | tee /tmp/commands

如果您感觉非常敏锐,那么您当然可以使新的Python命令变得更聪明,也许它可以直接将命令写入输出文件,但我将其作为一个练习留给读者。

票数 2
EN

Stack Overflow用户

发布于 2022-10-27 03:43:24

有什么方法可以从运行中的gdb实例中获取信息?

您可以看到使用(gdb) show commands的历史。

可以使用以下方法将输出保存到文件中:

代码语言:javascript
运行
复制
(gdb) set logging file /tmp/gdb-history
(gdb) set logging on
(gdb) show commands
(gdb) set logging off

不幸的是,历史记录中有序列号,在使用gdb -x /tmp/gdb-history之前,您必须去掉序列号。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74214750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档