Skip to content

Commit 617de12

Browse files
committed
support attaching by name for platform android
1 parent e28a559 commit 617de12

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,85 @@ std::string PlatformAndroid::GetRunAs() {
477477
}
478478
return run_as.str();
479479
}
480+
uint32_t
481+
PlatformAndroid::FindProcesses(const ProcessInstanceInfoMatch &match_info,
482+
ProcessInstanceInfoList &proc_infos) {
483+
// Use the parent implementation for host platform
484+
if (IsHost())
485+
return PlatformLinux::FindProcesses(match_info, proc_infos);
486+
487+
// For remote Android platform, implement process name lookup using adb
488+
proc_infos.clear();
489+
490+
// Check if we're looking for a process by name
491+
const ProcessInstanceInfo &match_process_info = match_info.GetProcessInfo();
492+
if (!match_process_info.GetExecutableFile() ||
493+
match_info.GetNameMatchType() == NameMatch::Ignore) {
494+
// Fall back to the parent implementation if not searching by name
495+
return PlatformLinux::FindProcesses(match_info, proc_infos);
496+
}
497+
498+
std::string process_name = match_process_info.GetExecutableFile().GetPath();
499+
if (process_name.empty())
500+
return 0;
501+
502+
// Use adb to find the process by name
503+
Status error;
504+
AdbClientUP adb(GetAdbClient(error));
505+
if (error.Fail()) {
506+
Log *log = GetLog(LLDBLog::Platform);
507+
LLDB_LOGF(log, "PlatformAndroid::%s failed to get ADB client: %s",
508+
__FUNCTION__, error.AsCString());
509+
return 0;
510+
}
511+
512+
// Use 'pidof' command to get the PID for the process name
513+
std::string pidof_output;
514+
std::string command = "pidof " + process_name;
515+
error = adb->Shell(command.c_str(), seconds(5), &pidof_output);
516+
517+
if (error.Fail()) {
518+
Log *log = GetLog(LLDBLog::Platform);
519+
LLDB_LOGF(log, "PlatformAndroid::%s 'pidof %s' failed: %s", __FUNCTION__,
520+
process_name.c_str(), error.AsCString());
521+
return 0;
522+
}
523+
524+
// Parse the PID from pidof output
525+
pidof_output = llvm::StringRef(pidof_output).trim().str();
526+
if (pidof_output.empty()) {
527+
// No process found with that name
528+
return 0;
529+
}
530+
531+
// Parse the output as a single PID
532+
lldb::pid_t pid;
533+
if (!llvm::to_integer(pidof_output, pid)) {
534+
Log *log = GetLog(LLDBLog::Platform);
535+
LLDB_LOGF(log, "PlatformAndroid::%s failed to parse PID from output: '%s'",
536+
__FUNCTION__, pidof_output.c_str());
537+
return 0;
538+
}
539+
540+
// Create ProcessInstanceInfo for the found process
541+
ProcessInstanceInfo process_info;
542+
process_info.SetProcessID(pid);
543+
process_info.GetExecutableFile().SetFile(process_name,
544+
FileSpec::Style::posix);
545+
546+
// Check if this process matches the criteria
547+
if (match_info.Matches(process_info)) {
548+
proc_infos.push_back(process_info);
549+
550+
Log *log = GetLog(LLDBLog::Platform);
551+
LLDB_LOGF(log, "PlatformAndroid::%s found process '%s' with PID %llu",
552+
__FUNCTION__, process_name.c_str(), (unsigned long long)pid);
553+
return 1;
554+
}
555+
556+
return 0;
557+
}
558+
480559
std::unique_ptr<AdbSyncService> PlatformAndroid::GetSyncService(Status &error) {
481560
auto sync_service = std::make_unique<AdbSyncService>(m_device_id);
482561
error = sync_service->SetupSyncConnection();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class PlatformAndroid : public platform_linux::PlatformLinux {
5959

6060
uint32_t GetDefaultMemoryCacheLineSize() override;
6161

62+
uint32_t FindProcesses(const ProcessInstanceInfoMatch &match_info,
63+
ProcessInstanceInfoList &proc_infos) override;
64+
6265
protected:
6366
const char *GetCacheHostname() override;
6467

0 commit comments

Comments
 (0)