Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote debugging using gdbserver over ssh

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

like image 208
Mulvihic Avatar asked Nov 21 '14 12:11

Mulvihic


People also ask

How do I start gdbserver on target?

To use the server, you log on to the target system, and run the 'gdbserver' program. You must tell it (a) how to communicate with GDB, (b) the name of your program, and (c) its arguments. The general syntax is: target> gdbserver COMM PROGRAM [ARGS ...]

What is GDB remote debugging?

What's remote debugging? It's where you run GDB on one machine and the program being debugged on another. To do this you need something to allow GDB to control the program being debugged, and that something is called the remote stub. GDB ships with a remote stub called gdbserver, but other remote stubs exist.

What port does gdbserver use?

The `host:2345' argument means that gdbserver is to expect a TCP connection from machine `host' to local TCP port 2345.


1 Answers

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.

like image 72
Employed Russian Avatar answered Oct 25 '22 16:10

Employed Russian