Skip to content

Commit 591f204

Browse files
[android] update README for building and running on Android (#176)
## Purpose Cleanup and enhance the README file for build and running ds2 on Android. ## Overview * Clarify how to build with the Android NDK and for Android in general * Add instructions on running on Android, using adb, etc * Add instructions on running in platform mode * Overall cleanup ## Validation Inspected the rendered contents [here](https://github.com/andrurogerz/ds2/tree/cleanup-readme). --------- Co-authored-by: Saleem Abdulrasool <compnerd@compnerd.org>
1 parent f99d0e3 commit 591f204

File tree

1 file changed

+124
-27
lines changed

1 file changed

+124
-27
lines changed

README.md

Lines changed: 124 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ The build instructions include instructions assuming that the [ninja-build](http
2424
- flex
2525
- bison
2626

27+
**Android**
28+
- Android NDK r18b or newer (https://developer.android.com/ndk/downloads)
29+
2730
### Building with CMake + Ninja
2831

2932
> **Windows Only**
@@ -41,13 +44,21 @@ ninja -C out
4144
### Compiling for Android
4245

4346
For Android native debugging, it is possible to build ds2 with the Android NDK.
47+
An NDK version of at least r18b is required for C++17 support.
48+
49+
CMake will automatically find the installed Android NDK if either the
50+
`ANDROID_NDK_ROOT` or `ANDROID_NDK` environment variable is set. Alternatively,
51+
the NDK location can be provided to CMake directly with the `CMAKE_ANDROID_NDK`
52+
variable, e.g. `-DCMAKE_ANDROID_NDK=/home/user/Android/Sdk/ndk/26.1.10909125`.
4453

54+
To build for Android:
4555
```sh
46-
cmake -B out -D CMAKE_SYSTEM_NAME=Android -D CMAKE_ANDROID_ARCH_ABI=armeabi-v7a -G Ninja -S ds2
47-
ninja -C out
56+
cd ds2
57+
cmake -B out -S . -D CMAKE_SYSTEM_NAME=Android -D CMAKE_ANDROID_ARCH_ABI=arm64-v8a -D CMAKE_BUILD_TYPE=Release -G Ninja
58+
cmake --build out
4859
```
4960

50-
Note that this will build ds2 targeting the highest level API level that the
61+
By default, CMake will build ds2 targeting the highest level API level that the
5162
NDK supports. If you want to target another api level, e.g. 21, add the flag
5263
`-DCMAKE_SYSTEM_VERSION=21` to your CMake invocation.
5364

@@ -67,39 +78,125 @@ make
6778
This will generate a binary that you can copy to your device to start
6879
debugging.
6980

81+
## Running ds2
7082

83+
### Running on a remote host
7184

72-
## Running ds2
85+
Launch ds2 in platform mode (not supported on Windows):
86+
```sh
87+
$ ./ds2 platform --server --listen localhost:4242
88+
```
7389

74-
### Example
90+
Launch ds2 as a single-instance gdb server debugging a program:
91+
```sh
92+
$ ./ds2 gdbserver localhost:4242 /path/to/executable
93+
```
7594

76-
#### On the remote host
95+
In both cases, ds2 is ready to accept connections on port 4242 from lldb.
7796

78-
Launch ds2 with something like:
97+
### Running on an Android device
7998

80-
$ ./ds2 gdbserver localhost:4242 /path/to/TestSimpleOutput
99+
When debugging Android NDK programs or applications, an Android device or
100+
emulator must be must be connected to the host machine and accessible via `adb`:
101+
```sh
102+
$ adb devices
103+
List of devices attached
104+
emulator-5554 device
105+
```
81106

82-
ds2 is now ready to accept connections on port 4242 from lldb.
107+
To use ds2 on the Android target, copy the locally build ds2 binary to the
108+
`/data/local/tmp` directory on the Android target using `adb push` and launch it
109+
from there:
110+
```sh
111+
$ adb push build/ds2 /data/local/tmp
112+
$ adb shell /data/local/tmp/ds2 platform --server --listen *:4242
113+
```
83114

84-
#### On your local host
115+
> [!NOTE]
116+
> If built on a Windows host, the ds2 executable file must also be marked
117+
> executable before launching it:
118+
> ```sh
119+
> $ adb shell chmod +x /data/local/tmp/ds2
120+
> ```
121+
122+
The port that ds2 is listening on must be forwarded to the connected host
123+
using `adb forward`:
124+
```sh
125+
$ adb forward tcp:4242 tcp:4242
126+
```
85127
86-
$ lldb /path/to/TestSimpleOutput
87-
Current executable set to '/path/to/TestSimpleOutput' (x86_64).
88-
(lldb) gdb-remote localhost:4242
89-
Process 8336 stopped
90-
* thread #1: tid = 8336, 0x00007ffff7ddb2d0, name = 'TestSimpleOutput', stop reason = signal SIGTRAP
91-
frame #0: 0x00007ffff7ddb2d0
92-
-> 0x7ffff7ddb2d0: movq %rsp, %rdi
93-
0x7ffff7ddb2d3: callq 0x7ffff7ddea70
94-
0x7ffff7ddb2d8: movq %rax, %r12
95-
0x7ffff7ddb2db: movl 0x221b17(%rip), %eax
96-
(lldb) b main
97-
Breakpoint 1: where = TestSimpleOutput`main + 29 at TestSimpleOutput.cpp:6, address = 0x000000000040096d
98-
[... debug debug ...]
99-
(lldb) c
100-
Process 8336 resuming
101-
Process 8336 exited with status = 0 (0x00000000)
102-
(lldb)
128+
When debugging an Android application, the ds2 binary must be copied from
129+
`/data/local/tmp` into the target application's sandbox directory. This can be
130+
done using Android's `run-as` command:
131+
```sh
132+
$ adb shell run-as com.example.app cp /data/local/tmp/ds2 ./ds2
133+
$ adb shell run-as com.example.app ./ds2 platform --server --listen *:4242
134+
```
135+
136+
> [!NOTE]
137+
> When running in an Android application's sandbox, the target application must
138+
> have internet permissions or ds2 will fail to open a port on launch:
139+
> ```xml
140+
> <uses-permission android:name="android.permission.INTERNET" />
141+
> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
142+
> ```
143+
144+
### Run lldb client on the local host
145+
146+
#### Platform Mode
147+
If ds2 was launched in `platform` mode (not supported on Windows), lldb can
148+
connect to it using `platform` commands.
149+
150+
For a remote Linux host:
151+
```
152+
$ lldb
153+
(lldb) platform select remote-linux
154+
(lldb) platform connect connect://localhost:4242
155+
```
156+
157+
For a remote Android host:
158+
```
159+
$ lldb
160+
(lldb) platform select remote-android
161+
(lldb) platform connect connect://localhost:4242
162+
(lldb) platform settings -w /data/local/tmp
163+
```
164+
165+
> [!NOTE]
166+
> When running in an Android application's sandbox, the `platform settings -w`
167+
> command, which sets the working directory, is not necessary because the
168+
> it is already set to the root of the application's writable sandbox directory.
169+
170+
Once connected in platform mode, you can select the program to be run using the
171+
`file` command, run, and debug.
172+
```
173+
(lldb) file /path/to/executable
174+
(lldb) b main
175+
(lldb) run
176+
```
177+
178+
#### Gdb Server Mode
179+
If ds2 was launched in `gdbserver` mode, lldb can connect to it with the
180+
`gdb-remote` command:
181+
```
182+
$ lldb /path/to/executable
183+
Current executable set to '/path/to/executable' (x86_64).
184+
(lldb) gdb-remote localhost:4242
185+
Process 8336 stopped
186+
* thread #1: tid = 8336, 0x00007ffff7ddb2d0, name = 'executable', stop reason = signal SIGTRAP
187+
frame #0: 0x00007ffff7ddb2d0
188+
-> 0x7ffff7ddb2d0: movq %rsp, %rdi
189+
0x7ffff7ddb2d3: callq 0x7ffff7ddea70
190+
0x7ffff7ddb2d8: movq %rax, %r12
191+
0x7ffff7ddb2db: movl 0x221b17(%rip), %eax
192+
(lldb) b main
193+
Breakpoint 1: where = executable`main + 29 at test.cpp:6, address = 0x000000000040096d
194+
[... debug debug ...]
195+
(lldb) c
196+
Process 8336 resuming
197+
Process 8336 exited with status = 0 (0x00000000)
198+
(lldb)
199+
```
103200
104201
### Command-Line Options
105202

0 commit comments

Comments
 (0)