[Lldb-commits] [lldb] support attaching by name for platform android (PR #160931)
via lldb-commits
lldb-commits at lists.llvm.org
Fri Sep 26 12:12:44 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Chad Smith (cs01)
<details>
<summary>Changes</summary>
## Bug
Trying to attach to an android process by name fails:
```
(lldb) process attach -n com.android.bluetooth
error: attach failed: could not find a process named com.android.bluetooth
```
## Root Cause
PlatformAndroid does not implement the `FindProcesses` method.
As described in `include/lldb/Target/Platform.h`:
```
// The base class Platform will take care of the host platform. Subclasses
// will need to fill in the remote case.
virtual uint32_t FindProcesses(const ProcessInstanceInfoMatch &match_info,
ProcessInstanceInfoList &proc_infos);
```
## Fix
Implement `FindProcesses` method. Use adb to get the pid of the process name on the device with the adb client connection.
## Reproduce
With an android device connected, run the following
Install and start lldb-server on device
```
adb push lldb-server /data/local/tmp/
adb shell chmod +x /data/local/tmp/lldb-server
adb shell /data/local/tmp/lldb-server platform --listen 127.0.0.1:9500 --server
```
Start lldb, and run
```
platform select remote-android
platform connect connect://127.0.0.1:9500
log enable lldb platform
```
Connect to the process by name:
```
process attach -n named com.android.bluetooth
```
## Test Plan
Before:
```
(lldb) process attach -n com.android.bluetooth
error: attach failed: could not find a process named com.android.bluetooth
```
After:
```
(lldb) process attach -n com.android.bluetooth
lldb AdbClient::ResolveDeviceID Resolved device ID: 127.0.0.1
lldb AdbClient::AdbClient(device_id='127.0.0.1') - Creating AdbClient with device ID
lldb Connecting to ADB server at connect://127.0.0.1:5037
lldb Connected to Android device "127.0.0.1"
lldb Forwarding remote TCP port 38315 to local TCP port 35243
lldb AdbClient::~AdbClient() - Destroying AdbClient for device: 127.0.0.1
lldb gdbserver connect URL: connect://127.0.0.1:35243
lldb AdbClient::AdbClient(device_id='127.0.0.1') - Creating AdbClient with device ID
lldb Connecting to ADB server at connect://127.0.0.1:5037
lldb Selecting device: 127.0.0.1
lldb PlatformAndroid::FindProcesses found process 'com.android.bluetooth' with PID 2223
lldb AdbClient::~AdbClient() - Destroying AdbClient for device: 127.0.0.1
llvm-worker-48 PlatformRemoteGDBServer::GetModuleSpec - got module info for (/apex/com.android.art/lib64/libc++.so:aarch64-unknown-linux-android) : file = '/apex/com.android.art/lib64/libc++.so', arch = aarch64-unknown-linux-android, uuid = 552995D0-A86D-055F-1F03-C13783A2A16C, object size = 754128
llvm-worker-9 PlatformRemoteGDBServer::GetModuleSpec - got module info for (/apex/com.android.art/lib64/liblzma.so:aarch64-unknown-linux-android) : file = '/apex/com.android.art/lib64/liblzma.so', arch = aarch64-unknown-linux-android, uuid = E51CAC98-666F-6C3F-F605-1498079542E0, object size = 179944
Process 2223 stopped
```
---
Full diff: https://github.com/llvm/llvm-project/pull/160931.diff
2 Files Affected:
- (modified) lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp (+79)
- (modified) lldb/source/Plugins/Platform/Android/PlatformAndroid.h (+3)
``````````diff
diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp b/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
index 600cc0a04cd22..bdef98c2d760f 100644
--- a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
+++ b/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
@@ -477,6 +477,85 @@ std::string PlatformAndroid::GetRunAs() {
}
return run_as.str();
}
+uint32_t
+PlatformAndroid::FindProcesses(const ProcessInstanceInfoMatch &match_info,
+ ProcessInstanceInfoList &proc_infos) {
+ // Use the parent implementation for host platform
+ if (IsHost())
+ return PlatformLinux::FindProcesses(match_info, proc_infos);
+
+ // For remote Android platform, implement process name lookup using adb
+ proc_infos.clear();
+
+ // Check if we're looking for a process by name
+ const ProcessInstanceInfo &match_process_info = match_info.GetProcessInfo();
+ if (!match_process_info.GetExecutableFile() ||
+ match_info.GetNameMatchType() == NameMatch::Ignore) {
+ // Fall back to the parent implementation if not searching by name
+ return PlatformLinux::FindProcesses(match_info, proc_infos);
+ }
+
+ std::string process_name = match_process_info.GetExecutableFile().GetPath();
+ if (process_name.empty())
+ return 0;
+
+ // Use adb to find the process by name
+ Status error;
+ AdbClientUP adb(GetAdbClient(error));
+ if (error.Fail()) {
+ Log *log = GetLog(LLDBLog::Platform);
+ LLDB_LOGF(log, "PlatformAndroid::%s failed to get ADB client: %s",
+ __FUNCTION__, error.AsCString());
+ return 0;
+ }
+
+ // Use 'pidof' command to get the PID for the process name
+ std::string pidof_output;
+ std::string command = "pidof " + process_name;
+ error = adb->Shell(command.c_str(), seconds(5), &pidof_output);
+
+ if (error.Fail()) {
+ Log *log = GetLog(LLDBLog::Platform);
+ LLDB_LOGF(log, "PlatformAndroid::%s 'pidof %s' failed: %s", __FUNCTION__,
+ process_name.c_str(), error.AsCString());
+ return 0;
+ }
+
+ // Parse the PID from pidof output
+ pidof_output = llvm::StringRef(pidof_output).trim().str();
+ if (pidof_output.empty()) {
+ // No process found with that name
+ return 0;
+ }
+
+ // Parse the output as a single PID
+ lldb::pid_t pid;
+ if (!llvm::to_integer(pidof_output, pid)) {
+ Log *log = GetLog(LLDBLog::Platform);
+ LLDB_LOGF(log, "PlatformAndroid::%s failed to parse PID from output: '%s'",
+ __FUNCTION__, pidof_output.c_str());
+ return 0;
+ }
+
+ // Create ProcessInstanceInfo for the found process
+ ProcessInstanceInfo process_info;
+ process_info.SetProcessID(pid);
+ process_info.GetExecutableFile().SetFile(process_name,
+ FileSpec::Style::posix);
+
+ // Check if this process matches the criteria
+ if (match_info.Matches(process_info)) {
+ proc_infos.push_back(process_info);
+
+ Log *log = GetLog(LLDBLog::Platform);
+ LLDB_LOGF(log, "PlatformAndroid::%s found process '%s' with PID %llu",
+ __FUNCTION__, process_name.c_str(), (unsigned long long)pid);
+ return 1;
+ }
+
+ return 0;
+}
+
std::unique_ptr<AdbSyncService> PlatformAndroid::GetSyncService(Status &error) {
auto sync_service = std::make_unique<AdbSyncService>(m_device_id);
error = sync_service->SetupSyncConnection();
diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroid.h b/lldb/source/Plugins/Platform/Android/PlatformAndroid.h
index 3384525362ecf..701d12922a383 100644
--- a/lldb/source/Plugins/Platform/Android/PlatformAndroid.h
+++ b/lldb/source/Plugins/Platform/Android/PlatformAndroid.h
@@ -59,6 +59,9 @@ class PlatformAndroid : public platform_linux::PlatformLinux {
uint32_t GetDefaultMemoryCacheLineSize() override;
+ uint32_t FindProcesses(const ProcessInstanceInfoMatch &match_info,
+ ProcessInstanceInfoList &proc_infos) override;
+
protected:
const char *GetCacheHostname() override;
``````````
</details>
https://github.com/llvm/llvm-project/pull/160931
More information about the lldb-commits
mailing list