Skip to content

Conversation

cs01
Copy link
Contributor

@cs01 cs01 commented Sep 26, 2025

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 the FindProcesses method in PlatformAndroid. Use adb to get the pid of the process name on the device with the adb client connection using adb shell pidof <name>.

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 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

@cs01 cs01 requested a review from JDevlieghere as a code owner September 26, 2025 19:12
@llvmbot llvmbot added the lldb label Sep 26, 2025
@llvmbot
Copy link
Member

llvmbot commented Sep 26, 2025

@llvm/pr-subscribers-lldb

Author: Chad Smith (cs01)

Changes

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 &amp;match_info,
                                 ProcessInstanceInfoList &amp;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 --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;
 

Copy link
Contributor

@royitaqi royitaqi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few clarification questions, mostly for my own learning.

Copy link

github-actions bot commented Oct 8, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@cs01 cs01 force-pushed the cs01/android-attach-by-name branch from 41225dd to 0ddd9a5 Compare October 8, 2025 22:49
@cs01
Copy link
Contributor Author

cs01 commented Oct 8, 2025

If there is > 1 process with the same name, this occurs:

(lldb) process attach -n sleep
error: attach failed: more than one process named sleep:
PID    PARENT USER       TRIPLE                         ARGUMENTS
====== ====== ========== ============================== ============================
5231   4741   root       armv7-unknown-linux-android    sleep 10000
5232   4741   root       armv7-unknown-linux-android    sleep 10000
5233   4741   root       armv7-unknown-linux-android    sleep 10000
5330   4741   root       armv7-unknown-linux-android    sleep 10006

@cs01 cs01 force-pushed the cs01/android-attach-by-name branch 2 times, most recently from 6db4cd1 to 7a02e47 Compare October 9, 2025 00:43
Copy link
Member

@walter-erquinigo walter-erquinigo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fancy

@cs01 cs01 force-pushed the cs01/android-attach-by-name branch from 7a02e47 to e292993 Compare October 9, 2025 16:26
@cs01
Copy link
Contributor Author

cs01 commented Oct 9, 2025

Addressed all comments from Walter.

#thanks @walter-erquinigo for review and feedback on my pull request :)

@cs01
Copy link
Contributor Author

cs01 commented Oct 9, 2025

@JDevlieghere This has been reviewed by @walter-erquinigo and is ready to merge unless you have other feedback

Copy link
Contributor

@royitaqi royitaqi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM~! Thanks for the updates.

@JDevlieghere JDevlieghere merged commit a19c9a8 into llvm:main Oct 9, 2025
9 checks passed
@cs01 cs01 deleted the cs01/android-attach-by-name branch October 9, 2025 19:00
svkeerthy pushed a commit that referenced this pull request Oct 9, 2025
## 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 the `FindProcesses` method in PlatformAndroid. Use adb to get
the pid of the process name on the device with the adb client connection
using `adb shell pidof <name>`.

## 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 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
```
@cs01 cs01 changed the title support attaching by name for platform android [lldb] support attaching by name for platform android Oct 12, 2025
DharuniRAcharya pushed a commit to DharuniRAcharya/llvm-project that referenced this pull request Oct 13, 2025
## 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 the `FindProcesses` method in PlatformAndroid. Use adb to get
the pid of the process name on the device with the adb client connection
using `adb shell pidof <name>`.

## 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 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
```
akadutta pushed a commit to akadutta/llvm-project that referenced this pull request Oct 14, 2025
## 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 the `FindProcesses` method in PlatformAndroid. Use adb to get
the pid of the process name on the device with the adb client connection
using `adb shell pidof <name>`.

## 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 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
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants