Use Android device serial number instead of hostname as a target identifier within module cache.

http://reviews.llvm.org/D8597

llvm-svn: 233202
diff --git a/lldb/source/Plugins/Platform/Android/AdbClient.cpp b/lldb/source/Plugins/Platform/Android/AdbClient.cpp
index eb094a1..d3d5878 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 <algorithm>
 #include <sstream>
 
 using namespace lldb;
@@ -29,6 +29,32 @@
 
 }  // 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 @@
     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 382e304..f372b24 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 @@
 public:
     using DeviceIDList = std::list<std::string>;
 
+    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 @@
     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 02d283b..e339e03 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 @@
 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 @@
 
     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 eaba103..08b3a02 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 <string>
+
 // Other libraries and framework includes
 // Project includes
 #include "Plugins/Platform/Linux/PlatformLinux.h"
@@ -60,7 +63,12 @@
         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 c32cdd4..b3c0ff4 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 @@
     // 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);
 }