<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58114>58114</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            How to debug a truly remote android?
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mark2185
      </td>
    </tr>
</table>

<pre>
    I want to be able to run a process on a truly remote android (connected to a remote server), connect to it via `lldb` and debug C++ code.

My `android device` (let's call it `Ganymede`) is connected to a remote server (`Jupiter`). I can `ssh` to that server from my local PC (codename `Earth`) and create whichever tunnels I need.

I tried running `lldb-server platform --listen "*:54321"` on `Ganymede` as one of the applications on the device (through `adb shell run-as com.company.exerunner.Test ./lldb-server blah`), and I have succeeded in connecting to it from `Jupiter` via:
```
$ ( Jupiter ) > lldb
$ ( Jupiter lldb )> platform select remote-android
$ ( Jupiter lldb )> platform connect connect://localhost:54321
    Triple: aarch64-unknown-linux-android
OS Version: 33 (4.14.276-ge333cb8619d0-ab8811257)
  Hostname: localhost
 Connected: yes
WorkingDir: /data/user/0/com.company.exerunner.Test
    Kernel: #1 SMP PREEMPT Fri Jul 8 12:03:57 UTC 2022
```

But, when I try doing the same through an `ssh` tunnel from `Earth` to `Jupiter`, created with `ssh -L54321:localhost:54321 user@saturn`, I get `error: failed to connect port`. 

If I've understood correctly `lldb`'s logic behind the scenes, it creates a `gdb-stub` on `Jupiter` (based on [docs](https://lldb.llvm.org/use/remote.html)), on some random free port, and uses `adb` to forward that local port to the port on `Ganymede`. But it forwards from `Jupiter` to the `Ganymede`.

I can verify that through this (I also compiled a debug version of `lldb` and ran it under a debugger and I think that's what it does)
```
$> adb forward --list
0B231JEC202267 tcp:<random-port> tcp:54321
```
That makes sense why won't it work on when I'm `ssh-ing` to `Jupiter`, I need to forward the port `5037` as well, so that `lldb` can talk to the `adb` on the server. And I need to forward that random `gdb-stub` port somehow. 

Based on [the docs](https://lldb.llvm.org/man/lldb-server.html#gdb-server-connections), I can specify the `gdbserver-port` flag to set the `gdbserver` port, instead of it being random (if I understood that part correctly).

So, I run the server with
```
$ (Ganymede) > ./lldb-server platform --listen "*:54321" --gdbserver-port 12345
```

But how do I tell `platform connect` to use that `gdbserver` port instead of spawning a new one?
I have no idea, so I forwarded the port `12345` from `Earth` to `Jupiter`, and used `adb forward tcp:12345 tcp:12345` to forward it from `Jupiter` to `Ganymede`, and tried running `gdb-remote 12345` within `lldb` from `Jupiter` instead.
I get:
```
error: Connection shut down by remote side while waiting for reply to initial handshake packet
```

So, I can't even connect to `Ganymede` from `Saturn`, where it is physically plugged into, when setting the `gdbserver port` manually. Maybe I'm holding it wrong, I don't know.

I tried using `lldb-server gdbserver` then instead of `lldb-server platform`, straight on `Ganymede`.

```
$ (Ganymede) > lldb-server gdbserver :12345
$ (Jupiter) > adb forward tcp:12345 tcp:12345
$ (Jupiter) > lldb
$( Jupiter-lldb ) > gdb-remote 12345
error: failed to get reply to handshake packet within timeout of 6.0 seconds
```

Tried it as well with `*:12345` to allow connecting from _any_ host (but that's not in the docs so I may be wrong), but no change.

So, my question is - how do I connect to a `lldb-server` on a truly remote `android device`?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVWFtz4jYU_jXwosFjbG554CEhocu2mWaatH3ckW2B3diWK8lh-ff9jiSDISST7mS5yDo6t-9854hEZoflhu15bZiRLBGMJ6Wgj6qtGWeNkqnQmkn6YlRbHpgSlTTYV2dKFhkbRItU1rVIjchIjncbtFBvQg2im0G0Yn4LbSgMeys4G8zCsswSvNFRLBNJu2OrQXSHP2zPRDAI7wfhrXt9PJBApzMTb0UqSBTaS2EG0VyzlJclHY7lX3h9qERGO6CfFZp9ZiIdgp3f26YwMNjKBGyDA2s6TOucNEHO5Nx0MlslK1YdWCmhlz2tXBwyUfNKkNQDVyb3-sm_VAkOjfu8SHNBB5gWFpUaemohsj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