1 //===-- AdbClient.cpp -----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/FileUtilities.h"
16 #include "lldb/Host/ConnectionFileDescriptor.h"
17 #include "lldb/Host/FileSystem.h"
18 #include "lldb/Host/PosixApi.h"
19 #include "lldb/Utility/DataBuffer.h"
20 #include "lldb/Utility/DataBufferHeap.h"
21 #include "lldb/Utility/DataEncoder.h"
22 #include "lldb/Utility/DataExtractor.h"
23 #include "lldb/Utility/FileSpec.h"
24 #include "lldb/Utility/StreamString.h"
25 #include "lldb/Utility/Timeout.h"
34 // On Windows, transitive dependencies pull in <Windows.h>, which defines a
35 // macro that clashes with a method name.
41 using namespace lldb_private
;
42 using namespace lldb_private::platform_android
;
43 using namespace std::chrono
;
45 static const seconds
kReadTimeout(20);
46 static const char *kOKAY
= "OKAY";
47 static const char *kFAIL
= "FAIL";
48 static const char *kDATA
= "DATA";
49 static const char *kDONE
= "DONE";
51 static const char *kSEND
= "SEND";
52 static const char *kRECV
= "RECV";
53 static const char *kSTAT
= "STAT";
55 static const size_t kSyncPacketLen
= 8;
56 // Maximum size of a filesync DATA packet.
57 static const size_t kMaxPushData
= 2 * 1024;
58 // Default mode for pushed files.
59 static const uint32_t kDefaultMode
= 0100770; // S_IFREG | S_IRWXU | S_IRWXG
61 static const char *kSocketNamespaceAbstract
= "localabstract";
62 static const char *kSocketNamespaceFileSystem
= "localfilesystem";
64 static Status
ReadAllBytes(Connection
&conn
, void *buffer
, size_t size
) {
67 ConnectionStatus status
;
68 char *read_buffer
= static_cast<char *>(buffer
);
70 auto now
= steady_clock::now();
71 const auto deadline
= now
+ kReadTimeout
;
72 size_t total_read_bytes
= 0;
73 while (total_read_bytes
< size
&& now
< deadline
) {
75 conn
.Read(read_buffer
+ total_read_bytes
, size
- total_read_bytes
,
76 duration_cast
<microseconds
>(deadline
- now
), status
, &error
);
79 total_read_bytes
+= read_bytes
;
80 if (status
!= eConnectionStatusSuccess
)
82 now
= steady_clock::now();
84 if (total_read_bytes
< size
)
85 error
= Status::FromErrorStringWithFormat(
86 "Unable to read requested number of bytes. Connection status: %d.",
91 Status
AdbClient::CreateByDeviceID(const std::string
&device_id
,
94 std::string android_serial
;
95 if (!device_id
.empty())
96 android_serial
= device_id
;
97 else if (const char *env_serial
= std::getenv("ANDROID_SERIAL"))
98 android_serial
= env_serial
;
100 if (android_serial
.empty()) {
101 DeviceIDList connected_devices
;
102 error
= adb
.GetDevices(connected_devices
);
106 if (connected_devices
.size() != 1)
107 return Status::FromErrorStringWithFormat(
108 "Expected a single connected device, got instead %zu - try "
109 "setting 'ANDROID_SERIAL'",
110 connected_devices
.size());
111 adb
.SetDeviceID(connected_devices
.front());
113 adb
.SetDeviceID(android_serial
);
118 AdbClient::AdbClient() = default;
120 AdbClient::AdbClient(const std::string
&device_id
) : m_device_id(device_id
) {}
122 AdbClient::~AdbClient() = default;
124 void AdbClient::SetDeviceID(const std::string
&device_id
) {
125 m_device_id
= device_id
;
128 const std::string
&AdbClient::GetDeviceID() const { return m_device_id
; }
130 Status
AdbClient::Connect() {
132 m_conn
= std::make_unique
<ConnectionFileDescriptor
>();
133 std::string port
= "5037";
134 if (const char *env_port
= std::getenv("ANDROID_ADB_SERVER_PORT")) {
137 std::string uri
= "connect://127.0.0.1:" + port
;
138 m_conn
->Connect(uri
.c_str(), &error
);
143 Status
AdbClient::GetDevices(DeviceIDList
&device_list
) {
146 auto error
= SendMessage("host:devices");
150 error
= ReadResponseStatus();
154 std::vector
<char> in_buffer
;
155 error
= ReadMessage(in_buffer
);
157 llvm::StringRef
response(&in_buffer
[0], in_buffer
.size());
158 llvm::SmallVector
<llvm::StringRef
, 4> devices
;
159 response
.split(devices
, "\n", -1, false);
161 for (const auto &device
: devices
)
162 device_list
.push_back(std::string(device
.split('\t').first
));
164 // Force disconnect since ADB closes connection after host:devices response
170 Status
AdbClient::SetPortForwarding(const uint16_t local_port
,
171 const uint16_t remote_port
) {
173 snprintf(message
, sizeof(message
), "forward:tcp:%d;tcp:%d", local_port
,
176 Status error
= SendDeviceMessage(message
);
180 return ReadResponseStatus();
184 AdbClient::SetPortForwarding(const uint16_t local_port
,
185 llvm::StringRef remote_socket_name
,
186 const UnixSocketNamespace socket_namespace
) {
187 char message
[PATH_MAX
];
188 const char *sock_namespace_str
=
189 (socket_namespace
== UnixSocketNamespaceAbstract
)
190 ? kSocketNamespaceAbstract
191 : kSocketNamespaceFileSystem
;
192 snprintf(message
, sizeof(message
), "forward:tcp:%d;%s:%s", local_port
,
193 sock_namespace_str
, remote_socket_name
.str().c_str());
195 Status error
= SendDeviceMessage(message
);
199 return ReadResponseStatus();
202 Status
AdbClient::DeletePortForwarding(const uint16_t local_port
) {
204 snprintf(message
, sizeof(message
), "killforward:tcp:%d", local_port
);
206 Status error
= SendDeviceMessage(message
);
210 return ReadResponseStatus();
213 Status
AdbClient::SendMessage(const std::string
&packet
, const bool reconnect
) {
215 if (!m_conn
|| reconnect
) {
221 char length_buffer
[5];
222 snprintf(length_buffer
, sizeof(length_buffer
), "%04x",
223 static_cast<int>(packet
.size()));
225 ConnectionStatus status
;
227 m_conn
->Write(length_buffer
, 4, status
, &error
);
231 m_conn
->Write(packet
.c_str(), packet
.size(), status
, &error
);
235 Status
AdbClient::SendDeviceMessage(const std::string
&packet
) {
236 std::ostringstream msg
;
237 msg
<< "host-serial:" << m_device_id
<< ":" << packet
;
238 return SendMessage(msg
.str());
241 Status
AdbClient::ReadMessage(std::vector
<char> &message
) {
247 auto error
= ReadAllBytes(buffer
, 4);
251 unsigned int packet_len
= 0;
252 sscanf(buffer
, "%x", &packet_len
);
254 message
.resize(packet_len
, 0);
255 error
= ReadAllBytes(&message
[0], packet_len
);
262 Status
AdbClient::ReadMessageStream(std::vector
<char> &message
,
263 milliseconds timeout
) {
264 auto start
= steady_clock::now();
268 lldb::ConnectionStatus status
= lldb::eConnectionStatusSuccess
;
270 while (error
.Success() && status
== lldb::eConnectionStatusSuccess
) {
271 auto end
= steady_clock::now();
272 auto elapsed
= end
- start
;
273 if (elapsed
>= timeout
)
274 return Status::FromErrorString("Timed out");
276 size_t n
= m_conn
->Read(buffer
, sizeof(buffer
),
277 duration_cast
<microseconds
>(timeout
- elapsed
),
280 message
.insert(message
.end(), &buffer
[0], &buffer
[n
]);
285 Status
AdbClient::ReadResponseStatus() {
288 static const size_t packet_len
= 4;
289 response_id
[packet_len
] = 0;
291 auto error
= ReadAllBytes(response_id
, packet_len
);
295 if (strncmp(response_id
, kOKAY
, packet_len
) != 0)
296 return GetResponseError(response_id
);
301 Status
AdbClient::GetResponseError(const char *response_id
) {
302 if (strcmp(response_id
, kFAIL
) != 0)
303 return Status::FromErrorStringWithFormat(
304 "Got unexpected response id from adb: \"%s\"", response_id
);
306 std::vector
<char> error_message
;
307 auto error
= ReadMessage(error_message
);
308 if (!error
.Success())
310 return Status(std::string(&error_message
[0], error_message
.size()));
313 Status
AdbClient::SwitchDeviceTransport() {
314 std::ostringstream msg
;
315 msg
<< "host:transport:" << m_device_id
;
317 auto error
= SendMessage(msg
.str());
321 return ReadResponseStatus();
324 Status
AdbClient::StartSync() {
325 auto error
= SwitchDeviceTransport();
327 return Status::FromErrorStringWithFormat(
328 "Failed to switch to device transport: %s", error
.AsCString());
332 return Status::FromErrorStringWithFormat("Sync failed: %s",
338 Status
AdbClient::Sync() {
339 auto error
= SendMessage("sync:", false);
343 return ReadResponseStatus();
346 Status
AdbClient::ReadAllBytes(void *buffer
, size_t size
) {
347 return ::ReadAllBytes(*m_conn
, buffer
, size
);
350 Status
AdbClient::internalShell(const char *command
, milliseconds timeout
,
351 std::vector
<char> &output_buf
) {
354 auto error
= SwitchDeviceTransport();
356 return Status::FromErrorStringWithFormat(
357 "Failed to switch to device transport: %s", error
.AsCString());
359 StreamString adb_command
;
360 adb_command
.Printf("shell:%s", command
);
361 error
= SendMessage(std::string(adb_command
.GetString()), false);
365 error
= ReadResponseStatus();
369 error
= ReadMessageStream(output_buf
, timeout
);
373 // ADB doesn't propagate return code of shell execution - if
374 // output starts with /system/bin/sh: most likely command failed.
375 static const char *kShellPrefix
= "/system/bin/sh:";
376 if (output_buf
.size() > strlen(kShellPrefix
)) {
377 if (!memcmp(&output_buf
[0], kShellPrefix
, strlen(kShellPrefix
)))
378 return Status::FromErrorStringWithFormat(
379 "Shell command %s failed: %s", command
,
380 std::string(output_buf
.begin(), output_buf
.end()).c_str());
386 Status
AdbClient::Shell(const char *command
, milliseconds timeout
,
387 std::string
*output
) {
388 std::vector
<char> output_buffer
;
389 auto error
= internalShell(command
, timeout
, output_buffer
);
394 output
->assign(output_buffer
.begin(), output_buffer
.end());
398 Status
AdbClient::ShellToFile(const char *command
, milliseconds timeout
,
399 const FileSpec
&output_file_spec
) {
400 std::vector
<char> output_buffer
;
401 auto error
= internalShell(command
, timeout
, output_buffer
);
405 const auto output_filename
= output_file_spec
.GetPath();
407 llvm::raw_fd_ostream
dst(output_filename
, EC
, llvm::sys::fs::OF_None
);
409 return Status::FromErrorStringWithFormat("Unable to open local file %s",
410 output_filename
.c_str());
412 dst
.write(&output_buffer
[0], output_buffer
.size());
415 return Status::FromErrorStringWithFormat("Failed to write file %s",
416 output_filename
.c_str());
420 std::unique_ptr
<AdbClient::SyncService
>
421 AdbClient::GetSyncService(Status
&error
) {
422 std::unique_ptr
<SyncService
> sync_service
;
425 sync_service
.reset(new SyncService(std::move(m_conn
)));
430 Status
AdbClient::SyncService::internalPullFile(const FileSpec
&remote_file
,
431 const FileSpec
&local_file
) {
432 const auto local_file_path
= local_file
.GetPath();
433 llvm::FileRemover
local_file_remover(local_file_path
);
436 llvm::raw_fd_ostream
dst(local_file_path
, EC
, llvm::sys::fs::OF_None
);
438 return Status::FromErrorStringWithFormat("Unable to open local file %s",
439 local_file_path
.c_str());
441 const auto remote_file_path
= remote_file
.GetPath(false);
442 auto error
= SendSyncRequest(kRECV
, remote_file_path
.length(),
443 remote_file_path
.c_str());
447 std::vector
<char> chunk
;
450 error
= PullFileChunk(chunk
, eof
);
454 dst
.write(&chunk
[0], chunk
.size());
458 return Status::FromErrorStringWithFormat("Failed to write file %s",
459 local_file_path
.c_str());
461 local_file_remover
.releaseFile();
465 Status
AdbClient::SyncService::internalPushFile(const FileSpec
&local_file
,
466 const FileSpec
&remote_file
) {
467 const auto local_file_path(local_file
.GetPath());
468 std::ifstream
src(local_file_path
.c_str(), std::ios::in
| std::ios::binary
);
470 return Status::FromErrorStringWithFormat("Unable to open local file %s",
471 local_file_path
.c_str());
473 std::stringstream file_description
;
474 file_description
<< remote_file
.GetPath(false).c_str() << "," << kDefaultMode
;
475 std::string file_description_str
= file_description
.str();
476 auto error
= SendSyncRequest(kSEND
, file_description_str
.length(),
477 file_description_str
.c_str());
481 char chunk
[kMaxPushData
];
482 while (!src
.eof() && !src
.read(chunk
, kMaxPushData
).bad()) {
483 size_t chunk_size
= src
.gcount();
484 error
= SendSyncRequest(kDATA
, chunk_size
, chunk
);
486 return Status::FromErrorStringWithFormat("Failed to send file chunk: %s",
489 error
= SendSyncRequest(
490 kDONE
, llvm::sys::toTimeT(FileSystem::Instance().GetModificationTime(local_file
)),
495 std::string response_id
;
497 error
= ReadSyncHeader(response_id
, data_len
);
499 return Status::FromErrorStringWithFormat("Failed to read DONE response: %s",
501 if (response_id
== kFAIL
) {
502 std::string
error_message(data_len
, 0);
503 error
= ReadAllBytes(&error_message
[0], data_len
);
505 return Status::FromErrorStringWithFormat(
506 "Failed to read DONE error message: %s", error
.AsCString());
507 return Status::FromErrorStringWithFormat("Failed to push file: %s",
508 error_message
.c_str());
509 } else if (response_id
!= kOKAY
)
510 return Status::FromErrorStringWithFormat("Got unexpected DONE response: %s",
511 response_id
.c_str());
513 // If there was an error reading the source file, finish the adb file
514 // transfer first so that adb isn't expecting any more data.
516 return Status::FromErrorStringWithFormat("Failed read on %s",
517 local_file_path
.c_str());
521 Status
AdbClient::SyncService::internalStat(const FileSpec
&remote_file
,
522 uint32_t &mode
, uint32_t &size
,
524 const std::string
remote_file_path(remote_file
.GetPath(false));
525 auto error
= SendSyncRequest(kSTAT
, remote_file_path
.length(),
526 remote_file_path
.c_str());
528 return Status::FromErrorStringWithFormat("Failed to send request: %s",
531 static const size_t stat_len
= strlen(kSTAT
);
532 static const size_t response_len
= stat_len
+ (sizeof(uint32_t) * 3);
534 std::vector
<char> buffer(response_len
);
535 error
= ReadAllBytes(&buffer
[0], buffer
.size());
537 return Status::FromErrorStringWithFormat("Failed to read response: %s",
540 DataExtractor
extractor(&buffer
[0], buffer
.size(), eByteOrderLittle
,
544 const void *command
= extractor
.GetData(&offset
, stat_len
);
546 return Status::FromErrorStringWithFormat("Failed to get response command");
547 const char *command_str
= static_cast<const char *>(command
);
548 if (strncmp(command_str
, kSTAT
, stat_len
))
549 return Status::FromErrorStringWithFormat("Got invalid stat command: %s",
552 mode
= extractor
.GetU32(&offset
);
553 size
= extractor
.GetU32(&offset
);
554 mtime
= extractor
.GetU32(&offset
);
558 Status
AdbClient::SyncService::PullFile(const FileSpec
&remote_file
,
559 const FileSpec
&local_file
) {
560 return executeCommand([this, &remote_file
, &local_file
]() {
561 return internalPullFile(remote_file
, local_file
);
565 Status
AdbClient::SyncService::PushFile(const FileSpec
&local_file
,
566 const FileSpec
&remote_file
) {
567 return executeCommand([this, &local_file
, &remote_file
]() {
568 return internalPushFile(local_file
, remote_file
);
572 Status
AdbClient::SyncService::Stat(const FileSpec
&remote_file
, uint32_t &mode
,
573 uint32_t &size
, uint32_t &mtime
) {
574 return executeCommand([this, &remote_file
, &mode
, &size
, &mtime
]() {
575 return internalStat(remote_file
, mode
, size
, mtime
);
579 bool AdbClient::SyncService::IsConnected() const {
580 return m_conn
&& m_conn
->IsConnected();
583 AdbClient::SyncService::SyncService(std::unique_ptr
<Connection
> &&conn
)
584 : m_conn(std::move(conn
)) {}
587 AdbClient::SyncService::executeCommand(const std::function
<Status()> &cmd
) {
589 return Status::FromErrorString("SyncService is disconnected");
591 Status error
= cmd();
598 AdbClient::SyncService::~SyncService() = default;
600 Status
AdbClient::SyncService::SendSyncRequest(const char *request_id
,
601 const uint32_t data_len
,
603 DataEncoder
encoder(eByteOrderLittle
, sizeof(void *));
604 encoder
.AppendData(llvm::StringRef(request_id
));
605 encoder
.AppendU32(data_len
);
606 llvm::ArrayRef
<uint8_t> bytes
= encoder
.GetData();
608 ConnectionStatus status
;
609 m_conn
->Write(bytes
.data(), kSyncPacketLen
, status
, &error
);
614 m_conn
->Write(data
, data_len
, status
, &error
);
618 Status
AdbClient::SyncService::ReadSyncHeader(std::string
&response_id
,
619 uint32_t &data_len
) {
620 char buffer
[kSyncPacketLen
];
622 auto error
= ReadAllBytes(buffer
, kSyncPacketLen
);
623 if (error
.Success()) {
624 response_id
.assign(&buffer
[0], 4);
625 DataExtractor
extractor(&buffer
[4], 4, eByteOrderLittle
, sizeof(void *));
627 data_len
= extractor
.GetU32(&offset
);
633 Status
AdbClient::SyncService::PullFileChunk(std::vector
<char> &buffer
,
637 std::string response_id
;
639 auto error
= ReadSyncHeader(response_id
, data_len
);
643 if (response_id
== kDATA
) {
644 buffer
.resize(data_len
, 0);
645 error
= ReadAllBytes(&buffer
[0], data_len
);
648 } else if (response_id
== kDONE
) {
650 } else if (response_id
== kFAIL
) {
651 std::string
error_message(data_len
, 0);
652 error
= ReadAllBytes(&error_message
[0], data_len
);
654 return Status::FromErrorStringWithFormat(
655 "Failed to read pull error message: %s", error
.AsCString());
656 return Status::FromErrorStringWithFormat("Failed to pull file: %s",
657 error_message
.c_str());
659 return Status::FromErrorStringWithFormat(
660 "Pull failed with unknown response: %s", response_id
.c_str());
665 Status
AdbClient::SyncService::ReadAllBytes(void *buffer
, size_t size
) {
666 return ::ReadAllBytes(*m_conn
, buffer
, size
);