Fix ADB client disconnect issues.

http://reviews.llvm.org/D22029

llvm-svn: 274638
This commit is contained in:
Oleksiy Vyalov 2016-07-06 17:02:42 +00:00
parent dafc5d75ea
commit d6a143fcbf
4 changed files with 106 additions and 25 deletions

View File

@ -225,10 +225,10 @@ AdbClient::DeletePortForwarding (const uint16_t local_port)
}
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, size_t size)
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 FileSpec &remote_file, const FileSpec &l
}
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 FileSpec &local_file, const FileSpec &re
}
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 FileSpec &remote_file, uint32_t &mode, uint3
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

View File

@ -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);
const std::unique_ptr<Connection> m_conn;
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);
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);

View File

@ -204,17 +204,12 @@ PlatformAndroid::ConnectRemote(Args& args)
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& source,
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& source,
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 (const lldb::ModuleSP& module_sp,
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 (const lldb::ModuleSP& module_sp,
// 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 (const lldb::ModuleSP& module_sp,
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::GetLibdlFunctionDeclarations() const
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;
}

View File

@ -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;