[Lldb-commits] [lldb] support attaching by name for platform android (PR #160931)

Chad Smith via lldb-commits lldb-commits at lists.llvm.org
Wed Oct 8 15:47:58 PDT 2025


================
@@ -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);
----------------
cs01 wrote:

> Can path contain special characters (e.g. space)? If so, maybe wrap with quotes?

Good call, I updated to wrap with quotes

> Also, what will happen if there are multiple processes running the same executable? (I did a quick internet search. It seems it's possible.)

Another good call. I updated to return the list of pids that match. Turns out lldb does not care and will just use the first one. This behavior should probably be changed to prompt the user to choose one, but that is out of scope for this diff.

https://github.com/llvm/llvm-project/pull/160931


More information about the lldb-commits mailing list