Remote debugging using gdbserver over ssh

Mulvihic picture Mulvihic · Nov 21, 2014 · Viewed 13.7k times · Source

I want to debug a process running on a remote box from my host box (I built the code on the host machine). Both have linux type operating systems.

I seems I can only communicate to the remote box from the host box via ssh (I tested using telnet).

I have followed the following steps to set this up:

On the Remote box:

  1. Stop the firewall service:

    service firewall_service stop

  2. Attach the process to gdbserver

    --attach :remote_port process_id

On the Host box:

  1. Set up port forwarding via ssh

    sudo ssh remote_username@remote_ip -L host_port:localhost:remote_port -f sleep 60m

  2. Set up gdb to attach to a remote process:

    gdb file.debug

    (gdb) target remote remote_ip:remote_port

When I try to start the debugging on the host by running 'target remote remote_ip:remote_port' on the host box I get a 'Connection timedout' error.

Can you guys see anything I am doing wrong, anything to check or an alternative way to debug remotely over ssh I would be grateful. Thanks

Answer

Employed Russian picture Employed Russian · Nov 22, 2014

This command:

sudo ssh remote_username@remote_ip -L host_port:localhost:remote_port ...

forwards local host_port to remote_port on remote_ip's localhost. This is useful only if you could not just connect to remote_ip:remote_port directly (for example, if that port is blocked by firewall).

This command:

(gdb) target remote remote_ip:remote_port

asks GDB to connect to remote_port on remote_ip. But you said that you can only reach remote_ip via ssh, so it's not surprising that GDB times out.

What you want:

ssh remote_username@remote_ip -L host_port:localhost:remote_port ...
(gdb) target remote :host_port

In other words, you connect to local host_port, and ssh forwards that local connection to remote_ip:remote_port, where gdbserver is listening for it.