Issue 58114
Summary How to debug a truly remote android?
Labels new issue
Assignees
Reporter mark2185
    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`?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to