通过ssh使用gdbserver进行远程调试

8
我想从主机上调试远程计算机上运行的进程(我在主机上构建了代码)。两台计算机都是Linux操作系统。
看起来我只能通过ssh(我使用telnet进行了测试)从主机上与远程计算机进行通信。
我已经按照以下步骤设置了这个过程:
在远程计算机上:
1.停止防火墙服务: service firewall_service stop 2.将进程附加到gdbserver --attach:remote_port process_id
在主机上:
3.通过ssh设置端口转发 sudo ssh remote_username@remote_ip -L host_port:localhost:remote_port -f sleep 60m
4.设置gdb以附加到远程进程: gdb file.debug (gdb) target remote remote_ip:remote_port
当我在主机上运行'target remote remote_ip:remote_port'尝试启动调试时,我得到一个“连接超时”的错误。
如果您能够发现我做错了什么,需要检查或替代通过ssh进行远程调试的方法,我将不胜感激。谢谢。
1个回答

8
这条命令:
sudo ssh remote_username@remote_ip -L host_port:localhost:remote_port ...

将本地主机端口 host_port 转发到远程IP的localhost上的远程端口 remote_port。仅在无法直接连接到远程IP:remote_port(例如,如果该端口被防火墙阻止)时才有用。

此命令:

(gdb) target remote remote_ip:remote_port

请求 GDB 连接到远程 IP 的远程端口。但是你说你只能通过 ssh 访问远程 IP,所以 GDB 超时也就不足为奇了。

您想要的是:

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

换句话说,您连接到本地 host_port,然后ssh将该本地连接转发到远程ip:remote_port,gdbserver在那里倾听它。

谢谢-我会尝试一下,但看起来还有另一个防火墙位于两个盒子之间,这可能是我无法连接的原因。 - Mulvihic
3
第一段错误,“localhost”是在ssh服务器上进行评估的,与对“localhost:remote_port”进行本地连接完全不同。选项“-L host_port:localhost:remote_port”非常有用,可以访问SSH服务器上除SSH端口本身之外的端口,绕过可能阻止它们的任何防火墙。 - Ben Voigt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接