Chad Smith a19c9a8ba1
support attaching by name for platform android (#160931)
## 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
```
2025-10-09 11:28:13 -07:00

106 lines
3.2 KiB
C++

//===-- PlatformAndroid.h ---------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_PLATFORMANDROID_H
#define LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_PLATFORMANDROID_H
#include <memory>
#include <string>
#include "Plugins/Platform/Linux/PlatformLinux.h"
#include "AdbClient.h"
namespace lldb_private {
namespace platform_android {
class PlatformAndroid : public platform_linux::PlatformLinux {
public:
PlatformAndroid(bool is_host);
static void Initialize();
static void Terminate();
// lldb_private::PluginInterface functions
static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
static void DebuggerInitialize(lldb_private::Debugger &debugger);
static llvm::StringRef GetPluginNameStatic(bool is_host) {
return is_host ? Platform::GetHostPlatformName() : "remote-android";
}
static llvm::StringRef GetPluginDescriptionStatic(bool is_host);
llvm::StringRef GetPluginName() override {
return GetPluginNameStatic(IsHost());
}
// lldb_private::Platform functions
Status ConnectRemote(Args &args) override;
Status GetFile(const FileSpec &source, const FileSpec &destination) override;
Status PutFile(const FileSpec &source, const FileSpec &destination,
uint32_t uid = UINT32_MAX, uint32_t gid = UINT32_MAX) override;
uint32_t GetSdkVersion();
bool GetRemoteOSVersion() override;
Status DisconnectRemote() override;
uint32_t GetDefaultMemoryCacheLineSize() override;
uint32_t FindProcesses(const ProcessInstanceInfoMatch &match_info,
ProcessInstanceInfoList &proc_infos) override;
protected:
const char *GetCacheHostname() override;
Status DownloadModuleSlice(const FileSpec &src_file_spec,
const uint64_t src_offset, const uint64_t src_size,
const FileSpec &dst_file_spec) override;
Status DownloadSymbolFile(const lldb::ModuleSP &module_sp,
const FileSpec &dst_file_spec) override;
llvm::StringRef
GetLibdlFunctionDeclarations(lldb_private::Process *process) override;
typedef std::unique_ptr<AdbClient> AdbClientUP;
virtual AdbClientUP GetAdbClient(Status &error);
std::string GetRunAs();
public:
virtual llvm::StringRef GetPropertyPackageName();
protected:
virtual std::unique_ptr<AdbSyncService> GetSyncService(Status &error);
private:
std::string m_device_id;
uint32_t m_sdk_version;
// Helper functions for process information gathering
void PopulateProcessStatusInfo(lldb::pid_t pid,
ProcessInstanceInfo &process_info);
void PopulateProcessCommandLine(lldb::pid_t pid,
ProcessInstanceInfo &process_info);
void PopulateProcessArchitecture(lldb::pid_t pid,
ProcessInstanceInfo &process_info);
};
} // namespace platform_android
} // namespace lldb_private
#endif // LLDB_SOURCE_PLUGINS_PLATFORM_ANDROID_PLATFORMANDROID_H