gdb中的多个命令由某种分隔符';'分隔?

Tre*_*ith 135 debugging gdb

我试图在gdb中一次执行两个命令:

finish; next
Run Code Online (Sandbox Code Playgroud)

我试过用';' 分开命令,但gdb不允许我同时做两个.

是否可以在gdb中执行多个命令,类似于以';'分隔的bash命令 分隔符?

Sea*_*ght 166

我不相信(但我可能错了).你可以这样做:

(gdb) define fn
> finish
> next
> end

然后输入:

(gdb) fn

您也可以将它放在您的~/.gdbinit文件中,以便始终可用.

  • 如果你忘记了如何定义一个函数,你可以使用`show user <function name>`来查看它的来源,例如`show user fn`. (3认同)
  • 调用 gdb 只是为了打印调用者的堆栈跟踪时的错误方法:`execlp("gdb", "gdb", "-batch", "-n", "-ex", "bt full", ...`和我无法关闭分页。 (2认同)

ale*_*cco 39

如果从命令行运行gdb,则可以使用-ex参数传递多个命令,如:

$ gdb ./prog -ex 'b srcfile.c:90' -ex 'b somefunc' -ex 'r -p arg1 -q arg2'
Run Code Online (Sandbox Code Playgroud)

这与显示和其他命令相结合使得运行gdb变得不那么麻烦.


Mic*_*der 9

GDB没有这样的命令分隔符.我简短地看了一下,万一它很容易添加一个,但不幸的是没有....


Gre*_*bet 5

您可以使用中的python集成来做到这一点gdb

如果s ; bt步进然后打印回溯,那会很好,但事实并非如此。

您可以通过调用Python解释器来完成同样的事情。

python import gdb ; print gdb.execute("s") ; print gdb.execute("bt")

可以将其包装成专用命令,这里称为“ cmds”,并由python定义支持。

这是一个.gdbinit扩展了具有运行多个命令功能的示例。

# multiple commands
python
from __future__ import print_function
import gdb


class Cmds(gdb.Command):
  """run multiple commands separated by ';'"""
  def __init__(self):
    gdb.Command.__init__(
      self,
      "cmds",
      gdb.COMMAND_DATA,
      gdb.COMPLETE_SYMBOL,
      True,
    )

  def invoke(self, arg, from_tty):
    for fragment in arg.split(';'):
      # from_tty is passed in from invoke.
      # These commands should be considered interactive if the command
      # that invoked them is interactive.
      # to_string is false. We just want to write the output of the commands, not capture it.
      gdb.execute(fragment, from_tty=from_tty, to_string=False)
      print()


Cmds()
end
Run Code Online (Sandbox Code Playgroud)

示例调用:

$ gdb
(gdb) cmds echo hi ; echo bye
hi
bye
Run Code Online (Sandbox Code Playgroud)