The ability to debug Swift code running on Android is one of the priorities of the Android project board. Related efforts are tracked under: Get this lldb working for Android debugging · Issue #10831 · swiftlang/llvm-project · GitHub
As a contributor, I worked on two topics:
1. Making LLDB work on Android
When developing an Android app (APK), the debugging context is slightly different from other platforms. First, debugging is done remotely, with lldb-server running on the device, inside the sandboxed environment of the app. Second, there is a JVM, with many libraries loaded after launch, and code segments generated on the fly. The Swift code to debug is often located in one of these shared libraries. Finally, the Swift Core Libraries use Android-specific implementations for types such as String and Array. This is due to constraints related to pointer tagging on Android.
All these aspects expose issues in LLDB that do not appear on other platforms.
Here is a list of known issues:
- Deadlock when loading modules
- Solved by cherry-picking from branch
next - PR: [lldb][android] Cherry-pick fixes from branch "next" by gmondada · Pull Request #12049 · swiftlang/llvm-project · GitHub
- Waiting for review
- Solved by cherry-picking from branch
- Crash when loading modules from the target + Attach processes by name
- Solved by cherry-picking from branch
next - PR: [lldb][android] Cherry-pick fixes from branch "next" by gmondada · Pull Request #12042 · swiftlang/llvm-project · GitHub
- Merged
- Solved by cherry-picking from branch
- Do not expose internal breakpoints to DAP clients
- Management of Android specific pointer tagging in LLDB
- lldb-server ignoring last entry in zip files
- Assertion on UnsafeMutablePointer<JNIEnv?>
- Issue: [lldb] Assertion when printing an UnsafePointer<Int?> in lldb (branch stable/21.x) · Issue #12053 · swiftlang/llvm-project · GitHub
- The code looks correct, just the asset() condition seems wrong
- Correctly format LLDB warnings in the debug console (cosmetic issue)
All these fixes are available on my work branch: GitHub - gmondada/llvm-project at gab/swift-lldb-patches
2. Swift debugging in VS Code
Swift debugging is already well supported in VS Code, thanks to the lldb-dap extension. See Configuring VS Code for Swift Development | Swift.org
To provide a good user experience on Android, the extension needs to manage these steps:
- Launch the APK on the remote device and keep it suspended (displaying “Waiting for Debugger”) until the debugger is attached
- Start lldb-server on the device
- Establish the connection with lldb-server and attach to the app process
- Set all breakpoints
- Resume the application execution
- Clean up everything (ADB tunnels, unix-sockets, temp files...) when the debug session ends
I implemented the logic to automate these steps on the following branch: GitHub - gmondada/llvm-project at gab/lldb-dap-android
The current implementation works only with a version of lldb-dap that includes all the fixes mentioned above. It expects an Android device (or emulator) to be connected, and the ADB server running.
To enable Android debugging, your .vscode/launch.json file must look like:
{
"configurations": [
{
"type": "lldb-dap",
"request": "launch",
"name": "Hello Swift",
"androidComponent": "org.example.helloswift/.MainActivity"
}
]
}
The extension does not handle app compilation and installation on the device. However, this can be automated by using a preLaunchTask.