-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[lldb] support attaching by name for platform android #160931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-lldb Author: Chad Smith (cs01) ChangesBugTrying to attach to an android process by name fails:
Root CausePlatformAndroid does not implement the As described in
FixImplement ReproduceWith an android device connected, run the following Install and start lldb-server on device
Start lldb, and run
Connect to the process by name:
Test PlanBefore:
After:
Full diff: https://github.com/llvm/llvm-project/pull/160931.diff 2 Files Affected:
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;
|
There was a problem hiding this 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.
✅ With the latest revision this PR passed the C/C++ code formatter. |
41225dd
to
0ddd9a5
Compare
If there is > 1 process with the same name, this occurs:
|
6db4cd1
to
7a02e47
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fancy
7a02e47
to
e292993
Compare
Addressed all comments from Walter. #thanks @walter-erquinigo for review and feedback on my pull request :) |
@JDevlieghere This has been reviewed by @walter-erquinigo and is ready to merge unless you have other feedback |
There was a problem hiding this 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.
## 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 ```
## 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 ```
## 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 ```
Bug
Trying to attach to an android process by name fails:
Root Cause
PlatformAndroid does not implement the
FindProcesses
method.As described in
include/lldb/Target/Platform.h
: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 usingadb shell pidof <name>
.Reproduce
With an android device connected, run the following
Install and start lldb-server on device
Start lldb, and run
Connect to the process by name:
Test Plan
Before:
After: