diff --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h index 55e91f027b52..3bddc88bc36f 100644 --- a/lldb/include/lldb/Target/Platform.h +++ b/lldb/include/lldb/Target/Platform.h @@ -1137,6 +1137,9 @@ class ModuleCache; const uint64_t src_size, const FileSpec& dst_file_spec); + virtual const char * + GetCacheHostname (); + private: typedef std::function ModuleResolver; diff --git a/lldb/source/Plugins/Platform/Android/AdbClient.cpp b/lldb/source/Plugins/Platform/Android/AdbClient.cpp index eb094a174395..d3d5878380b2 100644 --- a/lldb/source/Plugins/Platform/Android/AdbClient.cpp +++ b/lldb/source/Plugins/Platform/Android/AdbClient.cpp @@ -8,7 +8,6 @@ //===----------------------------------------------------------------------===// // Other libraries and framework includes -#include "lldb/Host/ConnectionFileDescriptor.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/STLExtras.h" @@ -16,6 +15,7 @@ // Project includes #include "AdbClient.h" +#include #include using namespace lldb; @@ -29,6 +29,32 @@ const char * kFAIL = "FAIL"; } // namespace +Error +AdbClient::CreateByDeviceID (const char* device_id, AdbClient &adb) +{ + DeviceIDList connect_devices; + auto error = adb.GetDevices (connect_devices); + if (error.Fail ()) + return error; + + if (device_id) + { + 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); + + adb.SetDeviceID (*find_it); + } + else + { + if (connect_devices.size () != 1) + return Error ("Expected a single connected device, got instead %zu", connect_devices.size ()); + + adb.SetDeviceID (connect_devices.front ()); + } + return error; +} + AdbClient::AdbClient (const std::string &device_id) : m_device_id (device_id) { @@ -40,6 +66,12 @@ AdbClient::SetDeviceID (const std::string& device_id) m_device_id = device_id; } +const std::string& +AdbClient::GetDeviceID() const +{ + return m_device_id; +} + Error AdbClient::Connect () { diff --git a/lldb/source/Plugins/Platform/Android/AdbClient.h b/lldb/source/Plugins/Platform/Android/AdbClient.h index 382e30455da7..f372b244021c 100644 --- a/lldb/source/Plugins/Platform/Android/AdbClient.h +++ b/lldb/source/Plugins/Platform/Android/AdbClient.h @@ -21,6 +21,7 @@ // Project includes #include "lldb/Core/Error.h" +#include "lldb/Host/ConnectionFileDescriptor.h" namespace lldb_private { @@ -29,11 +30,14 @@ class AdbClient public: using DeviceIDList = std::list; + static Error + CreateByDeviceID (const char* device_id, AdbClient &adb); + AdbClient () = default; explicit AdbClient (const std::string &device_id); - void - SetDeviceID (const std::string& device_id); + const std::string& + GetDeviceID() const; Error GetDevices (DeviceIDList &device_list); @@ -48,6 +52,9 @@ private: Error Connect (); + void + SetDeviceID (const std::string& device_id); + Error SendMessage (const std::string &packet); diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp b/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp index 02d283b1a510..e339e03db6d0 100644 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp +++ b/lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp @@ -15,6 +15,7 @@ #include "lldb/Host/HostInfo.h" // Project includes +#include "AdbClient.h" #include "PlatformAndroid.h" #include "PlatformAndroidRemoteGDBServer.h" @@ -171,6 +172,8 @@ PlatformAndroid::GetPluginName() 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()); @@ -178,5 +181,24 @@ PlatformAndroid::ConnectRemote (Args& args) if (!m_remote_platform_sp) m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer()); - return PlatformLinux::ConnectRemote (args); + + auto error = PlatformLinux::ConnectRemote (args); + if (error.Success ()) + { + // Fetch the device list from ADB and if only 1 device found then use that device + // TODO: Handle the case when more device is available + AdbClient adb; + error = AdbClient::CreateByDeviceID (nullptr, adb); + if (error.Fail ()) + return error; + + m_device_id = adb.GetDeviceID (); + } + return error; +} + +const char * +PlatformAndroid::GetCacheHostname () +{ + return m_device_id.c_str (); } diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroid.h b/lldb/source/Plugins/Platform/Android/PlatformAndroid.h index eaba103c99be..08b3a0270723 100644 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroid.h +++ b/lldb/source/Plugins/Platform/Android/PlatformAndroid.h @@ -12,6 +12,9 @@ // C Includes // C++ Includes + +#include + // Other libraries and framework includes // Project includes #include "Plugins/Platform/Linux/PlatformLinux.h" @@ -60,7 +63,12 @@ namespace lldb_private { lldb_private::Error ConnectRemote (lldb_private::Args& args) override; + protected: + const char * + GetCacheHostname () override; + private: + std::string m_device_id; DISALLOW_COPY_AND_ASSIGN (PlatformAndroid); }; } // namespace lldb_private diff --git a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp index c32cdd4f1e51..b3c0ff4b12c7 100644 --- a/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.cpp @@ -10,8 +10,6 @@ // Other libraries and framework includes #include "lldb/Core/Error.h" #include "lldb/Core/Log.h" -#include "lldb/Host/ConnectionFileDescriptor.h" -#include "llvm/ADT/StringRef.h" // Project includes #include "AdbClient.h" @@ -31,20 +29,13 @@ ForwardPortWithAdb (uint16_t port, std::string& device_id) // Fetch the device list from ADB and if only 1 device found then use that device // TODO: Handle the case when more device is available AdbClient adb; - - AdbClient::DeviceIDList connect_devices; - auto error = adb.GetDevices (connect_devices); + auto error = AdbClient::CreateByDeviceID (nullptr, adb); if (error.Fail ()) return error; - if (connect_devices.size () != 1) - return Error ("Expected a single connected device, got instead %zu", connect_devices.size ()); - - device_id = connect_devices.front (); if (log) - log->Printf("Connected to Android device \"%s\"", device_id.c_str ()); + log->Printf("Connected to Android device \"%s\"", adb.GetDeviceID ().c_str ()); - adb.SetDeviceID (device_id); return adb.SetPortForwarding (port); } diff --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp index d5a61f0ecdb4..6517d108cc96 100644 --- a/lldb/source/Target/Platform.cpp +++ b/lldb/source/Target/Platform.cpp @@ -1838,7 +1838,7 @@ Platform::GetCachedSharedModule (const ModuleSpec &module_spec, // Check local cache for a module. auto error = m_module_cache->Get (GetModuleCacheRoot (), - GetHostname (), + GetCacheHostname (), module_spec, module_sp, did_create_ptr); @@ -1880,7 +1880,7 @@ Platform::GetCachedSharedModule (const ModuleSpec &module_spec, // Put downloaded file into local module cache. error = m_module_cache->Put (GetModuleCacheRoot (), - GetHostname (), + GetCacheHostname (), module_spec, tmp_download_file_spec); if (error.Fail ()) @@ -1893,7 +1893,7 @@ Platform::GetCachedSharedModule (const ModuleSpec &module_spec, } error = m_module_cache->Get (GetModuleCacheRoot (), - GetHostname (), + GetCacheHostname (), module_spec, module_sp, did_create_ptr); @@ -1958,3 +1958,9 @@ Platform::GetModuleCacheRoot () dir_spec.AppendPathComponent (GetName ().AsCString ()); return dir_spec; } + +const char * +Platform::GetCacheHostname () +{ + return GetHostname (); +}