[Lldb-commits] [lldb] [lldb][Android] Fix platform process list regression (PR #164333)

Chad Smith via lldb-commits lldb-commits at lists.llvm.org
Mon Oct 20 21:31:44 PDT 2025


================
@@ -652,75 +709,169 @@ PlatformAndroid::FindProcesses(const ProcessInstanceInfoMatch &match_info,
     return 0;
   }
 
-  // Use 'pidof' command to get PIDs for the process name.
-  // Quote the process name to handle special characters (spaces, etc.)
-  std::string pidof_output;
-  StreamString command;
-  command.Printf("pidof '%s'", process_name.c_str());
-  error = adb->Shell(command.GetData(), seconds(5), &pidof_output);
+  std::string ps_output;
+  error = adb->Shell("ps -A -o PID,ARGS", seconds(5), &ps_output);
 
   if (error.Fail()) {
     Log *log = GetLog(LLDBLog::Platform);
-    LLDB_LOG(log, "PlatformAndroid::{} 'pidof {}' failed: {}", __FUNCTION__,
-             process_name.c_str(), error.AsCString());
-    return 0;
-  }
-
-  // Parse PIDs from pidof output.
-  // Note: pidof can return multiple PIDs (space-separated) if multiple
-  // instances of the same executable are running.
-  pidof_output = llvm::StringRef(pidof_output).trim().str();
-  if (pidof_output.empty()) {
-    Log *log = GetLog(LLDBLog::Platform);
-    LLDB_LOGF(log, "PlatformAndroid::%s no process found with name '%s'",
-              __FUNCTION__, process_name.c_str());
+    LLDB_LOG(log, "PlatformAndroid::{} 'ps -A' failed: {}", __FUNCTION__,
+             error.AsCString());
     return 0;
   }
 
-  // Split the output by whitespace to handle multiple PIDs
-  llvm::SmallVector<llvm::StringRef, 8> pid_strings;
-  llvm::StringRef(pidof_output).split(pid_strings, ' ', -1, false);
-
   Log *log = GetLog(LLDBLog::Platform);
 
-  // Process each PID and gather information
+  llvm::SmallVector<llvm::StringRef, 256> lines;
+  llvm::StringRef(ps_output).split(lines, '\n', -1, false);
+
   uint32_t num_matches = 0;
-  for (llvm::StringRef pid_str : pid_strings) {
-    pid_str = pid_str.trim();
-    if (pid_str.empty())
+
+  for (llvm::StringRef line : lines) {
+    line = line.trim();
+    if (line.empty())
       continue;
 
+    if (line.starts_with("PID"))
+      continue;
+
+    // Parse PID (first whitespace-separated field)
+    auto space_pos = line.find(' ');
+    if (space_pos == llvm::StringRef::npos)
+      continue;
+
+    llvm::StringRef pid_str = line.substr(0, space_pos);
+    llvm::StringRef cmdline = line.substr(space_pos + 1).trim();
+
     lldb::pid_t pid;
     if (!llvm::to_integer(pid_str, pid)) {
       LLDB_LOGF(log, "PlatformAndroid::%s failed to parse PID from: '%s'",
                 __FUNCTION__, pid_str.str().c_str());
       continue;
     }
 
+    bool name_matches = true;
+    if (name_match_type != NameMatch::Ignore && !process_name.empty()) {
+      name_matches = false;
+      switch (name_match_type) {
+      case NameMatch::Equals:
+        name_matches = (cmdline == process_name);
+        break;
+      case NameMatch::StartsWith:
+        name_matches = cmdline.starts_with(process_name);
+        break;
+      case NameMatch::EndsWith:
+        name_matches = cmdline.ends_with(process_name);
+        break;
+      case NameMatch::Contains:
+        name_matches = cmdline.contains(process_name);
+        break;
+      case NameMatch::RegularExpression: {
+        llvm::Regex regex(process_name);
+        name_matches = regex.match(cmdline);
+        break;
+      }
+      default:
+        name_matches = true;
+        break;
+      }
+    }
+
+    if (!name_matches)
+      continue;
+
     ProcessInstanceInfo process_info;
     process_info.SetProcessID(pid);
-    process_info.GetExecutableFile().SetFile(process_name,
-                                             FileSpec::Style::posix);
 
-    // Populate additional process information
+    // Set the executable name from cmdline (first token, typically)
+    llvm::StringRef exe_name = cmdline;
+    auto first_space = cmdline.find(' ');
+    if (first_space != llvm::StringRef::npos)
+      exe_name = cmdline.substr(0, first_space);
+
+    process_info.GetExecutableFile().SetFile(exe_name, FileSpec::Style::posix);
+
----------------
cs01 wrote:

I noticed it was slow too. However it did not seem slower than before. But I like the idea of batching the calls. I can do that tomorrow. 

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


More information about the lldb-commits mailing list