我经常使用gdbserver来远程调试我的程序。这次我遇到了一个独特的要求,我对gdb的了解不足。我想远程调试进程,但无法直接访问。让我们说有3个位置A,B和C.我想要调试的过程在C中。我有在A中调试的工具。但是C不能通过A访问。它可以通过B访问,可通过A访问。所以我不能在C上运行gdbserver并通过A连接。有没有办法可以利用AB和BC之间的连接来调试在C到A中运行的进程
此外,我没有保存ssh密钥。我只能使用密码登录,并且不能选择保存密钥。
答案 0 :(得分:1)
我认为这里一种有效的方法是使用SSH隧道,如下所示:
┌───────┐ ┌───────┐ ┌───────┐
│ HOSTA │ ←→ │ HOSTB | ←→ │ HOSTC │
└───────┘ └───────┘ └───────┘
# A terminal on HOSTA where you have access to HOSTB:22
# HOSTB has access to HOSTC:22, but HOSTA does not.
ssh BName@HOSTB \
-f `# Run in background ` \
-N `# Do not execute a remote command ` \
-L60022:HOSTC:22 `# HOSTC in the context of HOSTB `
# Enter your password for BName@HOSTB
# The above maps HOSTA:60022 through HOSTB to HOSTC:22
# Now HOSTA has SSH access to HOSTC.
# Next, open tunnels to other HOSTC ports we need for gdb
ssh CName@localhost `# localhost seems strange here, but is correct ` \
-p 60022 `# This is the port we forwarded above ` \
-f `# Run in background ` \
-N `# Do not execute a remote command ` \
-L62345:localhost:2345 `# localhost in the context of HOSTC `
# Enter your password for CName@HOSTC
现在,您可以通过SSH访问HOSTC,也可以使用可用于GDB的免费端口。接下来,在HOSTC上启动gdbserver
# A terminal on HOSTA where you have access to HOSTC via localhost:60022
ssh -p 60022 CName@localhost
# Enter your password for CName@HOSTC
# You are now at a terminal on HOSTC
gdbserver :2345 myAppToDebug # Note that 2345 is the port we opened above
gdbserver现在正在HOSTC上的端口2345上运行。因此,现在您可以将所选的调试工具连接到HOSTA:62345
,它应该可以工作。