GDB远程调试:让GDB等待gdbserver启动

GDB remote debugging: make GDB wait for gdbserver to be started

我知道远程调试的正常方法是在目标上启动 gdbserver,然后从 gdb 远程连接(使用目标远程)。

但是,是否可以让 GDB 在某个端口上等待,直到 gdbserver 在该端口上出现?

没有内置任何东西,但我认为有几种方法可以让它发挥作用。

一个是根据目标 运行 gdbserver 的要求,比如从 inetd 或等同物。

另一个是 运行 目标上的代理,并让代理接受连接,但在 gdbserver 准备好之前什么都不做。不过,我不确定这是否会 运行 导致 gdb 方面的超时问题。

您可以使用一些 python 代码来完成此操作。如果命令出错,gdb.execute("command ...") 将引发 python 异常。所以我们可以重复使用 python 运行 target remote host:port 命令,只要它收到超时错误(应该类似于 host:port: Connection timed out.)。

把下面的内容放到一个文件中,然后用gdb的source命令读入。它会定义一个新的子命令target waitremote host:port .

define target waitremote
python connectwithwait("$arg0")
end

document target waitremote
Use a remote gdbserver, waiting until it's connected.
end

python
def connectwithwait(hostandport):
  while 1:
    try:
      gdb.execute("target remote " + hostandport)
      return True
    except gdb.error, e:
      if "Connection timed out" in str(e):
        print "timed out, retrying"
        continue
      else:
        print "Cannot connect: " + str(e)
        return e
end