Skip to content

Commit a443a6c

Browse files
committed
[lldb][Android] Fix platform process list regression after FindProcesses implementation
1 parent 10afda0 commit a443a6c

File tree

2 files changed

+92
-214
lines changed

2 files changed

+92
-214
lines changed

lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp

Lines changed: 91 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "lldb/Utility/UriParser.h"
1616
#include "lldb/ValueObject/ValueObject.h"
1717

18+
#include <unordered_map>
19+
1820
#include "AdbClient.h"
1921
#include "PlatformAndroid.h"
2022
#include "PlatformAndroidRemoteGDBServer.h"
@@ -479,246 +481,131 @@ std::string PlatformAndroid::GetRunAs() {
479481
return run_as.str();
480482
}
481483

482-
// Helper function to populate process status information from
483-
// /proc/[pid]/status
484-
void PlatformAndroid::PopulateProcessStatusInfo(
485-
lldb::pid_t pid, ProcessInstanceInfo &process_info) {
486-
// Read /proc/[pid]/status to get parent PID, UIDs, and GIDs
487-
Status error;
488-
AdbClientUP status_adb = GetAdbClient(error);
489-
if (error.Fail())
490-
return;
491-
492-
std::string status_output;
493-
StreamString status_cmd;
494-
status_cmd.Printf(
495-
"cat /proc/%llu/status 2>/dev/null | grep -E '^(PPid|Uid|Gid):'",
496-
static_cast<unsigned long long>(pid));
497-
Status status_error =
498-
status_adb->Shell(status_cmd.GetData(), seconds(5), &status_output);
484+
static bool NeedsCmdlineSupplement(const ProcessInstanceInfo &proc_info) {
485+
llvm::StringRef name =
486+
proc_info.GetExecutableFile().GetFilename().GetStringRef();
487+
return name.contains("app_process") || name.contains("zygote");
488+
}
499489

500-
if (status_error.Fail() || status_output.empty())
490+
// Fetch /proc/PID/cmdline for processes to get actual package names.
491+
// Android apps often show as "zygote" or "app_process" without this.
492+
static void SupplementWithCmdlineInfo(ProcessInstanceInfoList &proc_infos,
493+
AdbClient *adb, Log *log) {
494+
if (proc_infos.empty())
501495
return;
502496

503-
llvm::SmallVector<llvm::StringRef, 16> lines;
504-
llvm::StringRef(status_output).split(lines, '\n');
505-
506-
for (llvm::StringRef line : lines) {
507-
line = line.trim();
508-
if (line.starts_with("PPid:")) {
509-
llvm::StringRef ppid_str = line.substr(5).trim();
510-
lldb::pid_t ppid;
511-
if (llvm::to_integer(ppid_str, ppid))
512-
process_info.SetParentProcessID(ppid);
513-
} else if (line.starts_with("Uid:")) {
514-
llvm::SmallVector<llvm::StringRef, 4> uid_parts;
515-
line.substr(4).trim().split(uid_parts, '\t', -1, false);
516-
if (uid_parts.size() >= 2) {
517-
uint32_t uid, euid;
518-
if (llvm::to_integer(uid_parts[0].trim(), uid))
519-
process_info.SetUserID(uid);
520-
if (llvm::to_integer(uid_parts[1].trim(), euid))
521-
process_info.SetEffectiveUserID(euid);
522-
}
523-
} else if (line.starts_with("Gid:")) {
524-
llvm::SmallVector<llvm::StringRef, 4> gid_parts;
525-
line.substr(4).trim().split(gid_parts, '\t', -1, false);
526-
if (gid_parts.size() >= 2) {
527-
uint32_t gid, egid;
528-
if (llvm::to_integer(gid_parts[0].trim(), gid))
529-
process_info.SetGroupID(gid);
530-
if (llvm::to_integer(gid_parts[1].trim(), egid))
531-
process_info.SetEffectiveGroupID(egid);
532-
}
497+
std::unordered_map<lldb::pid_t, ProcessInstanceInfo *> pid_map;
498+
std::string pid_list;
499+
for (auto &proc_info : proc_infos) {
500+
if (NeedsCmdlineSupplement(proc_info)) {
501+
lldb::pid_t pid = proc_info.GetProcessID();
502+
pid_map[pid] = &proc_info;
503+
if (!pid_list.empty())
504+
pid_list += " ";
505+
pid_list += std::to_string(pid);
533506
}
534507
}
535-
}
536508

537-
// Helper function to populate command line arguments from /proc/[pid]/cmdline
538-
void PlatformAndroid::PopulateProcessCommandLine(
539-
lldb::pid_t pid, ProcessInstanceInfo &process_info) {
540-
// Read /proc/[pid]/cmdline to get command line arguments
541-
Status error;
542-
AdbClientUP cmdline_adb = GetAdbClient(error);
543-
if (error.Fail())
509+
if (pid_list.empty())
544510
return;
545511

512+
// Use xargs -P to parallelize cmdline fetching (up to 8 concurrent reads)
513+
StreamString cmd;
514+
cmd.Printf(
515+
"echo '%s' | xargs -n 1 -P 8 sh -c "
516+
"'echo \"$1:$(cat /proc/$1/cmdline 2>/dev/null | tr \"\\0\" \" \")\"' sh",
517+
pid_list.c_str());
518+
546519
std::string cmdline_output;
547-
StreamString cmdline_cmd;
548-
cmdline_cmd.Printf("cat /proc/%llu/cmdline 2>/dev/null | tr '\\000' ' '",
549-
static_cast<unsigned long long>(pid));
550-
Status cmdline_error =
551-
cmdline_adb->Shell(cmdline_cmd.GetData(), seconds(5), &cmdline_output);
520+
Status error = adb->Shell(cmd.GetData(), seconds(5), &cmdline_output);
552521

553-
if (cmdline_error.Fail() || cmdline_output.empty())
522+
if (error.Fail() || cmdline_output.empty())
554523
return;
555524

556-
cmdline_output = llvm::StringRef(cmdline_output).trim().str();
557-
if (cmdline_output.empty())
558-
return;
525+
llvm::SmallVector<llvm::StringRef, 256> lines;
526+
llvm::StringRef(cmdline_output).split(lines, '\n', -1, false);
559527

560-
llvm::SmallVector<llvm::StringRef, 16> args;
561-
llvm::StringRef(cmdline_output).split(args, ' ', -1, false);
562-
if (args.empty())
563-
return;
528+
for (llvm::StringRef line : lines) {
529+
line = line.trim();
530+
auto colon_pos = line.find(':');
531+
if (colon_pos == llvm::StringRef::npos)
532+
continue;
564533

565-
process_info.SetArg0(args[0]);
566-
Args process_args;
567-
for (size_t i = 1; i < args.size(); i++) {
568-
if (!args[i].empty())
569-
process_args.AppendArgument(args[i]);
570-
}
571-
process_info.SetArguments(process_args, false);
572-
}
534+
llvm::StringRef pid_str = line.substr(0, colon_pos);
535+
llvm::StringRef cmdline = line.substr(colon_pos + 1).trim();
573536

574-
// Helper function to populate architecture from /proc/[pid]/exe
575-
void PlatformAndroid::PopulateProcessArchitecture(
576-
lldb::pid_t pid, ProcessInstanceInfo &process_info) {
577-
// Read /proc/[pid]/exe to get executable path for architecture detection
578-
Status error;
579-
AdbClientUP exe_adb = GetAdbClient(error);
580-
if (error.Fail())
581-
return;
537+
lldb::pid_t pid;
538+
if (!llvm::to_integer(pid_str, pid) || cmdline.empty())
539+
continue;
582540

583-
std::string exe_output;
584-
StreamString exe_cmd;
585-
exe_cmd.Printf("readlink /proc/%llu/exe 2>/dev/null",
586-
static_cast<unsigned long long>(pid));
587-
Status exe_error = exe_adb->Shell(exe_cmd.GetData(), seconds(5), &exe_output);
541+
auto it = pid_map.find(pid);
542+
if (it == pid_map.end())
543+
continue;
588544

589-
if (exe_error.Fail() || exe_output.empty())
590-
return;
545+
ProcessInstanceInfo *proc_info = it->second;
546+
llvm::SmallVector<llvm::StringRef, 16> args;
547+
cmdline.split(args, ' ', -1, false);
591548

592-
exe_output = llvm::StringRef(exe_output).trim().str();
593-
594-
// Determine architecture from exe path
595-
ArchSpec arch;
596-
if (exe_output.find("64") != std::string::npos ||
597-
exe_output.find("arm64") != std::string::npos ||
598-
exe_output.find("aarch64") != std::string::npos) {
599-
arch.SetTriple("aarch64-unknown-linux-android");
600-
} else if (exe_output.find("x86_64") != std::string::npos) {
601-
arch.SetTriple("x86_64-unknown-linux-android");
602-
} else if (exe_output.find("x86") != std::string::npos ||
603-
exe_output.find("i686") != std::string::npos) {
604-
arch.SetTriple("i686-unknown-linux-android");
605-
} else {
606-
// Default to armv7 for 32-bit ARM (most common on Android)
607-
arch.SetTriple("armv7-unknown-linux-android");
608-
}
549+
if (!args.empty()) {
550+
proc_info->GetExecutableFile().SetFile(args[0], FileSpec::Style::posix);
609551

610-
if (arch.IsValid())
611-
process_info.SetArchitecture(arch);
552+
if (args.size() > 1) {
553+
Args process_args;
554+
for (size_t i = 1; i < args.size(); ++i) {
555+
if (!args[i].empty())
556+
process_args.AppendArgument(args[i]);
557+
}
558+
proc_info->SetArguments(process_args, false);
559+
}
560+
561+
LLDB_LOGF(log,
562+
"PlatformAndroid::%s supplemented PID %llu with cmdline: %s",
563+
__FUNCTION__, static_cast<unsigned long long>(pid),
564+
cmdline.str().c_str());
565+
}
566+
}
612567
}
613568

614569
uint32_t
615570
PlatformAndroid::FindProcesses(const ProcessInstanceInfoMatch &match_info,
616571
ProcessInstanceInfoList &proc_infos) {
617572
proc_infos.clear();
618573

619-
// When LLDB is running natively on an Android device (IsHost() == true),
620-
// use the parent class's standard Linux /proc enumeration. IsHost() is only
621-
// true when compiled for Android (#if defined(__ANDROID__)), so calling
622-
// PlatformLinux methods is safe (Android is Linux-based).
623574
if (IsHost())
624575
return PlatformLinux::FindProcesses(match_info, proc_infos);
625576

626-
// Remote Android platform: implement process name lookup using 'pidof' over
627-
// adb.
628-
629-
// LLDB stores the search name in GetExecutableFile() (even though it's
630-
// actually a process name like "com.android.chrome" rather than an
631-
// executable path). If no search name is provided, we can't use
632-
// 'pidof', so return early with no results.
633-
const ProcessInstanceInfo &match_process_info = match_info.GetProcessInfo();
634-
if (!match_process_info.GetExecutableFile() ||
635-
match_info.GetNameMatchType() == NameMatch::Ignore) {
636-
return 0;
637-
}
638-
639-
// Extract the process name to search for (typically an Android package name
640-
// like "com.example.app" or binary name like "app_process64")
641-
std::string process_name = match_process_info.GetExecutableFile().GetPath();
642-
if (process_name.empty())
643-
return 0;
644-
645-
// Use adb to find the process by name
646-
Status error;
647-
AdbClientUP adb(GetAdbClient(error));
648-
if (error.Fail()) {
649-
Log *log = GetLog(LLDBLog::Platform);
650-
LLDB_LOGF(log, "PlatformAndroid::%s failed to get ADB client: %s",
651-
__FUNCTION__, error.AsCString());
652-
return 0;
653-
}
654-
655-
// Use 'pidof' command to get PIDs for the process name.
656-
// Quote the process name to handle special characters (spaces, etc.)
657-
std::string pidof_output;
658-
StreamString command;
659-
command.Printf("pidof '%s'", process_name.c_str());
660-
error = adb->Shell(command.GetData(), seconds(5), &pidof_output);
661-
662-
if (error.Fail()) {
663-
Log *log = GetLog(LLDBLog::Platform);
664-
LLDB_LOG(log, "PlatformAndroid::{} 'pidof {}' failed: {}", __FUNCTION__,
665-
process_name.c_str(), error.AsCString());
666-
return 0;
667-
}
668-
669-
// Parse PIDs from pidof output.
670-
// Note: pidof can return multiple PIDs (space-separated) if multiple
671-
// instances of the same executable are running.
672-
pidof_output = llvm::StringRef(pidof_output).trim().str();
673-
if (pidof_output.empty()) {
674-
Log *log = GetLog(LLDBLog::Platform);
675-
LLDB_LOGF(log, "PlatformAndroid::%s no process found with name '%s'",
676-
__FUNCTION__, process_name.c_str());
577+
if (!m_remote_platform_sp)
677578
return 0;
678-
}
679-
680-
// Split the output by whitespace to handle multiple PIDs
681-
llvm::SmallVector<llvm::StringRef, 8> pid_strings;
682-
llvm::StringRef(pidof_output).split(pid_strings, ' ', -1, false);
683-
684-
Log *log = GetLog(LLDBLog::Platform);
685-
686-
// Process each PID and gather information
687-
uint32_t num_matches = 0;
688-
for (llvm::StringRef pid_str : pid_strings) {
689-
pid_str = pid_str.trim();
690-
if (pid_str.empty())
691-
continue;
692579

693-
lldb::pid_t pid;
694-
if (!llvm::to_integer(pid_str, pid)) {
695-
LLDB_LOGF(log, "PlatformAndroid::%s failed to parse PID from: '%s'",
696-
__FUNCTION__, pid_str.str().c_str());
697-
continue;
580+
// Android-specific process name handling:
581+
// Apps spawned from zygote initially appear as "app_process" or "zygote"
582+
// in the process list, but their actual package names (e.g.,
583+
// "com.example.app") are only available in /proc/PID/cmdline. To support
584+
// name-based matching, we must first fetch cmdline info for all processes,
585+
// then apply the original name filter.
586+
ProcessInstanceInfoMatch broad_match_info = match_info;
587+
broad_match_info.SetNameMatchType(NameMatch::Ignore);
588+
589+
ProcessInstanceInfoList all_procs;
590+
uint32_t count =
591+
m_remote_platform_sp->FindProcesses(broad_match_info, all_procs);
592+
593+
if (count > 0) {
594+
Status error;
595+
AdbClientUP adb(GetAdbClient(error));
596+
if (error.Success()) {
597+
Log *log = GetLog(LLDBLog::Platform);
598+
SupplementWithCmdlineInfo(all_procs, adb.get(), log);
698599
}
699600

700-
ProcessInstanceInfo process_info;
701-
process_info.SetProcessID(pid);
702-
process_info.GetExecutableFile().SetFile(process_name,
703-
FileSpec::Style::posix);
704-
705-
// Populate additional process information
706-
PopulateProcessStatusInfo(pid, process_info);
707-
PopulateProcessCommandLine(pid, process_info);
708-
PopulateProcessArchitecture(pid, process_info);
709-
710-
// Check if this process matches the criteria
711-
if (match_info.Matches(process_info)) {
712-
proc_infos.push_back(process_info);
713-
num_matches++;
714-
715-
LLDB_LOGF(log, "PlatformAndroid::%s found process '%s' with PID %llu",
716-
__FUNCTION__, process_name.c_str(),
717-
static_cast<unsigned long long>(pid));
601+
// Apply the original name matching against supplemented process info.
602+
for (auto &proc_info : all_procs) {
603+
if (match_info.Matches(proc_info))
604+
proc_infos.push_back(proc_info);
718605
}
719606
}
720607

721-
return num_matches;
608+
return proc_infos.size();
722609
}
723610

724611
std::unique_ptr<AdbSyncService> PlatformAndroid::GetSyncService(Status &error) {

lldb/source/Plugins/Platform/Android/PlatformAndroid.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PlatformAndroid : public platform_linux::PlatformLinux {
6060
uint32_t GetDefaultMemoryCacheLineSize() override;
6161

6262
uint32_t FindProcesses(const ProcessInstanceInfoMatch &match_info,
63-
ProcessInstanceInfoList &proc_infos) override;
63+
ProcessInstanceInfoList &process_infos) override;
6464

6565
protected:
6666
const char *GetCacheHostname() override;
@@ -86,17 +86,8 @@ class PlatformAndroid : public platform_linux::PlatformLinux {
8686
protected:
8787
virtual std::unique_ptr<AdbSyncService> GetSyncService(Status &error);
8888

89-
private:
9089
std::string m_device_id;
9190
uint32_t m_sdk_version;
92-
93-
// Helper functions for process information gathering
94-
void PopulateProcessStatusInfo(lldb::pid_t pid,
95-
ProcessInstanceInfo &process_info);
96-
void PopulateProcessCommandLine(lldb::pid_t pid,
97-
ProcessInstanceInfo &process_info);
98-
void PopulateProcessArchitecture(lldb::pid_t pid,
99-
ProcessInstanceInfo &process_info);
10091
};
10192

10293
} // namespace platform_android

0 commit comments

Comments
 (0)