gdbgdbserver

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


The normal way I know to remote debug is to start gdbserver on a target and then remotely connect from gdb (using target remote).

But, is it possible that GDB be made to wait on a port until gdbserver comes up on that port?


Solution

  • You can do this with a bit of python code. gdb.execute("command ...") will raise a python exception if the command gets an error. So we can have python run the target remote host:port command repeatedly, for as long as it gets a timeout error (which should resemble host:port: Connection timed out.).

    Put the following into a file and use gdb's source command to read it in. It will define a new subcommand 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