[Lldb-commits] [lldb] r274638 - Fix ADB client disconnect issues.
Oleksiy Vyalov via lldb-commits
lldb-commits at lists.llvm.org
Wed Jul 6 10:02:42 PDT 2016
Author: ovyalov
Date: Wed Jul 6 12:02:42 2016
New Revision: 274638
URL: http://llvm.org/viewvc/llvm-project?rev=274638&view=rev
Log:
Fix ADB client disconnect issues.
http://reviews.llvm.org/D22029
Modified:
lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp
lldb/trunk/source/Plugins/Platform/Android/AdbClient.h
lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp
lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.h
Modified: lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp?rev=274638&r1=274637&r2=274638&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Android/AdbClient.cpp Wed Jul 6 12:02:42 2016
@@ -225,10 +225,10 @@ AdbClient::DeletePortForwarding (const u
}
Error
-AdbClient::SendMessage (const std::string &packet)
+AdbClient::SendMessage (const std::string &packet, const bool reconnect)
{
Error error;
- if (!m_conn || !m_conn->IsConnected())
+ if (!m_conn || reconnect)
{
error = Connect ();
if (error.Fail ())
@@ -364,7 +364,7 @@ AdbClient::StartSync ()
Error
AdbClient::Sync ()
{
- auto error = SendMessage ("sync:");
+ auto error = SendMessage ("sync:", false);
if (error.Fail ())
return error;
@@ -380,9 +380,13 @@ AdbClient::ReadAllBytes (void *buffer, s
Error
AdbClient::Shell (const char* command, uint32_t timeout_ms, std::string* output)
{
+ auto error = SwitchDeviceTransport ();
+ if (error.Fail ())
+ return Error ("Failed to switch to device transport: %s", error.AsCString ());
+
StreamString adb_command;
adb_command.Printf("shell:%s", command);
- auto error = SendMessage (adb_command.GetData());
+ error = SendMessage (adb_command.GetData(), false);
if (error.Fail ())
return error;
@@ -412,7 +416,7 @@ AdbClient::GetSyncService (Error &error)
}
Error
-AdbClient::SyncService::PullFile (const FileSpec &remote_file, const FileSpec &local_file)
+AdbClient::SyncService::internalPullFile (const FileSpec &remote_file, const FileSpec &local_file)
{
const auto local_file_path = local_file.GetPath ();
llvm::FileRemover local_file_remover (local_file_path.c_str ());
@@ -442,7 +446,7 @@ AdbClient::SyncService::PullFile (const
}
Error
-AdbClient::SyncService::PushFile (const FileSpec &local_file, const FileSpec &remote_file)
+AdbClient::SyncService::internalPushFile (const FileSpec &local_file, const FileSpec &remote_file)
{
const auto local_file_path (local_file.GetPath ());
std::ifstream src (local_file_path.c_str(), std::ios::in | std::ios::binary);
@@ -492,7 +496,7 @@ AdbClient::SyncService::PushFile (const
}
Error
-AdbClient::SyncService::Stat (const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime)
+AdbClient::SyncService::internalStat (const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime)
{
const std::string remote_file_path (remote_file.GetPath (false));
auto error = SendSyncRequest (kSTAT, remote_file_path.length (), remote_file_path.c_str ());
@@ -523,11 +527,54 @@ AdbClient::SyncService::Stat (const File
return Error ();
}
+Error
+AdbClient::SyncService::PullFile (const FileSpec &remote_file, const FileSpec &local_file)
+{
+ return executeCommand ([this, &remote_file, &local_file]() {
+ return internalPullFile (remote_file, local_file);
+ });
+}
+
+Error
+AdbClient::SyncService::PushFile (const FileSpec &local_file, const FileSpec &remote_file)
+{
+ return executeCommand ([this, &local_file, &remote_file]() {
+ return internalPushFile (local_file, remote_file);
+ });
+}
+
+Error
+AdbClient::SyncService::Stat (const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime)
+{
+ return executeCommand ([this, &remote_file, &mode, &size, &mtime]() {
+ return internalStat (remote_file, mode, size, mtime);
+ });
+}
+
+bool
+AdbClient::SyncService::IsConnected () const
+{
+ return m_conn && m_conn->IsConnected ();
+}
+
AdbClient::SyncService::SyncService(std::unique_ptr<Connection> &&conn):
m_conn(std::move(conn))
{
}
+Error
+AdbClient::SyncService::executeCommand (const std::function<Error()> &cmd)
+{
+ if (!m_conn)
+ return Error ("SyncService is disconnected");
+
+ const auto error = cmd ();
+ if (error.Fail ())
+ m_conn.reset ();
+
+ return error;
+}
+
AdbClient::SyncService::~SyncService () {}
Error
Modified: lldb/trunk/source/Plugins/Platform/Android/AdbClient.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/AdbClient.h?rev=274638&r1=274637&r2=274638&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/AdbClient.h (original)
+++ lldb/trunk/source/Plugins/Platform/Android/AdbClient.h Wed Jul 6 12:02:42 2016
@@ -14,6 +14,7 @@
// C++ Includes
+#include <functional>
#include <list>
#include <memory>
#include <string>
@@ -57,6 +58,9 @@ public:
Error
Stat (const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime);
+ bool
+ IsConnected () const;
+
private:
explicit SyncService (std::unique_ptr<Connection> &&conn);
@@ -72,8 +76,19 @@ public:
Error
ReadAllBytes (void *buffer, size_t size);
+ Error
+ internalPullFile (const FileSpec &remote_file, const FileSpec &local_file);
+
+ Error
+ internalPushFile (const FileSpec &local_file, const FileSpec &remote_file);
+
+ Error
+ internalStat (const FileSpec &remote_file, uint32_t &mode, uint32_t &size, uint32_t &mtime);
+
+ Error
+ executeCommand (const std::function<Error()> &cmd);
- const std::unique_ptr<Connection> m_conn;
+ std::unique_ptr<Connection> m_conn;
};
static Error
@@ -118,7 +133,7 @@ private:
SetDeviceID (const std::string &device_id);
Error
- SendMessage (const std::string &packet);
+ SendMessage (const std::string &packet, const bool reconnect = true);
Error
SendDeviceMessage (const std::string &packet);
Modified: lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp?rev=274638&r1=274637&r2=274638&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp Wed Jul 6 12:02:42 2016
@@ -204,17 +204,12 @@ PlatformAndroid::ConnectRemote(Args& arg
auto error = PlatformLinux::ConnectRemote(args);
if (error.Success())
{
- m_adb.reset(new AdbClient);
- error = AdbClient::CreateByDeviceID(m_device_id, *m_adb);
+ AdbClient adb;
+ error = AdbClient::CreateByDeviceID(m_device_id, adb);
if (error.Fail())
return error;
- m_device_id = m_adb->GetDeviceID();
- m_adb_sync_svc = m_adb->GetSyncService(error);
- if (error.Fail())
- return error;
-
- error = m_adb->SwitchDeviceTransport();
+ m_device_id = adb.GetDeviceID();
}
return error;
}
@@ -230,7 +225,12 @@ PlatformAndroid::GetFile (const FileSpec
if (source_spec.IsRelative())
source_spec = GetRemoteWorkingDirectory ().CopyByAppendingPathComponent (source_spec.GetCString (false));
- return m_adb_sync_svc->PullFile (source_spec, destination);
+ Error error;
+ auto sync_service = GetSyncService (error);
+ if (error.Fail ())
+ return error;
+
+ return sync_service->PullFile (source_spec, destination);
}
Error
@@ -247,7 +247,11 @@ PlatformAndroid::PutFile (const FileSpec
destination_spec = GetRemoteWorkingDirectory ().CopyByAppendingPathComponent (destination_spec.GetCString (false));
// TODO: Set correct uid and gid on remote file.
- return m_adb_sync_svc->PushFile(source, destination_spec);
+ Error error;
+ auto sync_service = GetSyncService (error);
+ if (error.Fail ())
+ return error;
+ return sync_service->PushFile(source, destination_spec);
}
const char *
@@ -296,7 +300,8 @@ PlatformAndroid::GetSdkVersion()
return m_sdk_version;
std::string version_string;
- Error error = m_adb->Shell("getprop ro.build.version.sdk", 5000 /* ms */, &version_string);
+ AdbClient adb(m_device_id);
+ Error error = adb.Shell("getprop ro.build.version.sdk", 5000 /* ms */, &version_string);
version_string = llvm::StringRef(version_string).trim().str();
if (error.Fail() || version_string.empty())
@@ -333,8 +338,9 @@ PlatformAndroid::DownloadSymbolFile (con
if (module_sp->GetSectionList()->FindSectionByName(ConstString(".symtab")) != nullptr)
return Error("Symtab already available in the module");
+ AdbClient adb(m_device_id);
std::string tmpdir;
- Error error = m_adb->Shell("mktemp --directory --tmpdir /data/local/tmp", 5000 /* ms */, &tmpdir);
+ Error error = adb.Shell("mktemp --directory --tmpdir /data/local/tmp", 5000 /* ms */, &tmpdir);
if (error.Fail() || tmpdir.empty())
return Error("Failed to generate temporary directory on the device (%s)", error.AsCString());
tmpdir = llvm::StringRef(tmpdir).trim().str();
@@ -342,10 +348,10 @@ PlatformAndroid::DownloadSymbolFile (con
// Create file remover for the temporary directory created on the device
std::unique_ptr<std::string, std::function<void(std::string*)>> tmpdir_remover(
&tmpdir,
- [this](std::string* s) {
+ [this, &adb](std::string* s) {
StreamString command;
command.Printf("rm -rf %s", s->c_str());
- Error error = m_adb->Shell(command.GetData(), 5000 /* ms */, nullptr);
+ Error error = adb.Shell(command.GetData(), 5000 /* ms */, nullptr);
Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
if (error.Fail())
@@ -361,7 +367,7 @@ PlatformAndroid::DownloadSymbolFile (con
command.Printf("oatdump --symbolize=%s --output=%s",
module_sp->GetPlatformFileSpec().GetCString(false),
symfile_platform_filespec.GetCString(false));
- error = m_adb->Shell(command.GetData(), 60000 /* ms */, nullptr);
+ error = adb.Shell(command.GetData(), 60000 /* ms */, nullptr);
if (error.Fail())
return Error("Oatdump failed: %s", error.AsCString());
@@ -388,3 +394,15 @@ PlatformAndroid::GetLibdlFunctionDeclara
extern "C" char* dlerror(void) asm("__dl_dlerror");
)";
}
+
+AdbClient::SyncService*
+PlatformAndroid::GetSyncService (Error &error)
+{
+ if (m_adb_sync_svc && m_adb_sync_svc->IsConnected ())
+ return m_adb_sync_svc.get ();
+
+ AdbClient adb (m_device_id);
+ m_adb_sync_svc = adb.GetSyncService (error);
+ return (error.Success ()) ? m_adb_sync_svc.get () : nullptr;
+}
+
Modified: lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.h?rev=274638&r1=274637&r2=274638&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.h (original)
+++ lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.h Wed Jul 6 12:02:42 2016
@@ -105,7 +105,8 @@ namespace platform_android {
GetLibdlFunctionDeclarations() const override;
private:
- std::unique_ptr<AdbClient> m_adb;
+ AdbClient::SyncService* GetSyncService (Error &error);
+
std::unique_ptr<AdbClient::SyncService> m_adb_sync_svc;
std::string m_device_id;
uint32_t m_sdk_version;
More information about the lldb-commits
mailing list