Error PlatformAndroid::ConnectRemote(Args& args) { m_device_id.clear(); if (IsHost()) { return Error ("can't connect to the host platform '%s', always connected", GetPluginName().GetCString()); } if (!m_remote_platform_sp) m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer()); int port; std::string scheme, host, path; const char *url = args.GetArgumentAtIndex(0); if (!url) return Error("URL is null."); if (!UriParser::Parse(url, scheme, host, port, path)) return Error("Invalid URL: %s", url); if (scheme == "adb") m_device_id = host; auto error = PlatformLinux::ConnectRemote(args); if (error.Success()) { AdbClient adb; error = AdbClient::CreateByDeviceID(m_device_id, adb); if (error.Fail()) return error; m_device_id = adb.GetDeviceID(); } return error; }
static Status ForwardPortWithAdb( const uint16_t local_port, const uint16_t remote_port, llvm::StringRef remote_socket_name, const llvm::Optional<AdbClient::UnixSocketNamespace> &socket_namespace, std::string &device_id) { Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM)); AdbClient adb; auto error = AdbClient::CreateByDeviceID(device_id, adb); if (error.Fail()) return error; device_id = adb.GetDeviceID(); if (log) log->Printf("Connected to Android device \"%s\"", device_id.c_str()); if (remote_port != 0) { if (log) log->Printf("Forwarding remote TCP port %d to local TCP port %d", remote_port, local_port); return adb.SetPortForwarding(local_port, remote_port); } if (log) log->Printf("Forwarding remote socket \"%s\" to local TCP port %d", remote_socket_name.str().c_str(), local_port); if (!socket_namespace) return Status("Invalid socket namespace"); return adb.SetPortForwarding(local_port, remote_socket_name, *socket_namespace); }
Error AdbClient::CreateByDeviceID(const std::string &device_id, AdbClient &adb) { DeviceIDList connect_devices; auto error = adb.GetDevices(connect_devices); if (error.Fail()) return error; if (device_id.empty()) { if (connect_devices.size() != 1) return Error("Expected a single connected device, got instead %" PRIu64, static_cast<uint64_t>(connect_devices.size())); adb.SetDeviceID(connect_devices.front()); } else { auto find_it = std::find(connect_devices.begin(), connect_devices.end(), device_id); if (find_it == connect_devices.end()) return Error("Device \"%s\" not found", device_id.c_str()); adb.SetDeviceID(*find_it); } return error; }
static Error ForwardPortWithAdb (const uint16_t local_port, const uint16_t remote_port, const char* remote_socket_name, std::string& device_id) { Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); AdbClient adb; auto error = AdbClient::CreateByDeviceID(device_id, adb); if (error.Fail ()) return error; device_id = adb.GetDeviceID(); if (log) log->Printf("Connected to Android device \"%s\"", device_id.c_str ()); if (remote_port != 0) { if (log) log->Printf("Forwarding remote TCP port %d to local TCP port %d", remote_port, local_port); return adb.SetPortForwarding(local_port, remote_port); } if (log) log->Printf("Forwarding remote socket \"%s\" to local TCP port %d", remote_socket_name, local_port); return adb.SetPortForwarding(local_port, remote_socket_name); }
Error AdbClient::CreateByDeviceID(const std::string &device_id, AdbClient &adb) { DeviceIDList connect_devices; auto error = adb.GetDevices(connect_devices); if (error.Fail()) return error; std::string android_serial; if (!device_id.empty()) android_serial = device_id; else if (const char *env_serial = std::getenv("ANDROID_SERIAL")) android_serial = env_serial; if (android_serial.empty()) { if (connect_devices.size() != 1) return Error("Expected a single connected device, got instead %zu - try " "setting 'ANDROID_SERIAL'", connect_devices.size()); adb.SetDeviceID(connect_devices.front()); } else { auto find_it = std::find(connect_devices.begin(), connect_devices.end(), android_serial); if (find_it == connect_devices.end()) return Error("Device \"%s\" not found", android_serial.c_str()); adb.SetDeviceID(*find_it); } return error; }
Error PlatformAndroid::GetFile (const FileSpec& source, const FileSpec& destination) { if (IsHost() || !m_remote_platform_sp) return PlatformLinux::GetFile(source, destination); FileSpec source_spec (source.GetPath (false), false, FileSpec::ePathSyntaxPosix); if (source_spec.IsRelative()) source_spec = GetRemoteWorkingDirectory ().CopyByAppendingPathComponent (source_spec.GetCString (false)); AdbClient adb (m_device_id); return adb.PullFile (source_spec, destination); }
static Error ForwardPortWithAdb (uint16_t port, std::string& device_id) { Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM)); AdbClient adb; auto error = AdbClient::CreateByDeviceID(device_id, adb); if (error.Fail ()) return error; device_id = adb.GetDeviceID(); if (log) log->Printf("Connected to Android device \"%s\"", device_id.c_str ()); return adb.SetPortForwarding(port); }
Error PlatformAndroid::PutFile (const FileSpec& source, const FileSpec& destination, uint32_t uid, uint32_t gid) { if (IsHost() || !m_remote_platform_sp) return PlatformLinux::PutFile (source, destination, uid, gid); FileSpec destination_spec (destination.GetPath (false), false, FileSpec::ePathSyntaxPosix); if (destination_spec.IsRelative()) destination_spec = GetRemoteWorkingDirectory ().CopyByAppendingPathComponent (destination_spec.GetCString (false)); AdbClient adb (m_device_id); // TODO: Set correct uid and gid on remote file. return adb.PushFile(source, destination_spec); }
static Error DeleteForwardPortWithAdb (uint16_t port, const std::string& device_id) { AdbClient adb (device_id); return adb.DeletePortForwarding (port); }