1 //===-- AdbClient.cpp -------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // Other libraries and framework includes 11 #include "AdbClient.h" 12 13 #include "llvm/ADT/STLExtras.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/Support/FileUtilities.h" 17 18 #include "lldb/Core/DataBuffer.h" 19 #include "lldb/Core/DataBufferHeap.h" 20 #include "lldb/Core/DataEncoder.h" 21 #include "lldb/Core/DataExtractor.h" 22 #include "lldb/Core/StreamString.h" 23 #include "lldb/Host/ConnectionFileDescriptor.h" 24 #include "lldb/Host/FileSpec.h" 25 #include "lldb/Host/FileSystem.h" 26 #include "lldb/Host/PosixApi.h" 27 28 #include <limits.h> 29 30 #include <algorithm> 31 #include <cstdlib> 32 #include <fstream> 33 #include <sstream> 34 35 // On Windows, transitive dependencies pull in <Windows.h>, which defines a 36 // macro that clashes with a method name. 37 #ifdef SendMessage 38 #undef SendMessage 39 #endif 40 41 using namespace lldb; 42 using namespace lldb_private; 43 using namespace lldb_private::platform_android; 44 45 namespace { 46 47 const std::chrono::seconds kReadTimeout(8); 48 const char *kOKAY = "OKAY"; 49 const char *kFAIL = "FAIL"; 50 const char *kDATA = "DATA"; 51 const char *kDONE = "DONE"; 52 53 const char *kSEND = "SEND"; 54 const char *kRECV = "RECV"; 55 const char *kSTAT = "STAT"; 56 57 const size_t kSyncPacketLen = 8; 58 // Maximum size of a filesync DATA packet. 59 const size_t kMaxPushData = 2 * 1024; 60 // Default mode for pushed files. 61 const uint32_t kDefaultMode = 0100770; // S_IFREG | S_IRWXU | S_IRWXG 62 63 const char *kSocketNamespaceAbstract = "localabstract"; 64 const char *kSocketNamespaceFileSystem = "localfilesystem"; 65 66 Error ReadAllBytes(Connection &conn, void *buffer, size_t size) { 67 using namespace std::chrono; 68 69 Error error; 70 ConnectionStatus status; 71 char *read_buffer = static_cast<char *>(buffer); 72 73 auto now = steady_clock::now(); 74 const auto deadline = now + kReadTimeout; 75 size_t total_read_bytes = 0; 76 while (total_read_bytes < size && now < deadline) { 77 uint32_t timeout_usec = duration_cast<microseconds>(deadline - now).count(); 78 auto read_bytes = 79 conn.Read(read_buffer + total_read_bytes, size - total_read_bytes, 80 timeout_usec, status, &error); 81 if (error.Fail()) 82 return error; 83 total_read_bytes += read_bytes; 84 if (status != eConnectionStatusSuccess) 85 break; 86 now = steady_clock::now(); 87 } 88 if (total_read_bytes < size) 89 error = Error( 90 "Unable to read requested number of bytes. Connection status: %d.", 91 status); 92 return error; 93 } 94 95 } // namespace 96 97 Error AdbClient::CreateByDeviceID(const std::string &device_id, 98 AdbClient &adb) { 99 DeviceIDList connect_devices; 100 auto error = adb.GetDevices(connect_devices); 101 if (error.Fail()) 102 return error; 103 104 std::string android_serial; 105 if (!device_id.empty()) 106 android_serial = device_id; 107 else if (const char *env_serial = std::getenv("ANDROID_SERIAL")) 108 android_serial = env_serial; 109 110 if (android_serial.empty()) { 111 if (connect_devices.size() != 1) 112 return Error("Expected a single connected device, got instead %zu - try " 113 "setting 'ANDROID_SERIAL'", 114 connect_devices.size()); 115 adb.SetDeviceID(connect_devices.front()); 116 } else { 117 auto find_it = std::find(connect_devices.begin(), connect_devices.end(), 118 android_serial); 119 if (find_it == connect_devices.end()) 120 return Error("Device \"%s\" not found", android_serial.c_str()); 121 122 adb.SetDeviceID(*find_it); 123 } 124 return error; 125 } 126 127 AdbClient::AdbClient() {} 128 129 AdbClient::AdbClient(const std::string &device_id) : m_device_id(device_id) {} 130 131 AdbClient::~AdbClient() {} 132 133 void AdbClient::SetDeviceID(const std::string &device_id) { 134 m_device_id = device_id; 135 } 136 137 const std::string &AdbClient::GetDeviceID() const { return m_device_id; } 138 139 Error AdbClient::Connect() { 140 Error error; 141 m_conn.reset(new ConnectionFileDescriptor); 142 m_conn->Connect("connect://localhost:5037", &error); 143 144 return error; 145 } 146 147 Error AdbClient::GetDevices(DeviceIDList &device_list) { 148 device_list.clear(); 149 150 auto error = SendMessage("host:devices"); 151 if (error.Fail()) 152 return error; 153 154 error = ReadResponseStatus(); 155 if (error.Fail()) 156 return error; 157 158 std::vector<char> in_buffer; 159 error = ReadMessage(in_buffer); 160 161 llvm::StringRef response(&in_buffer[0], in_buffer.size()); 162 llvm::SmallVector<llvm::StringRef, 4> devices; 163 response.split(devices, "\n", -1, false); 164 165 for (const auto device : devices) 166 device_list.push_back(device.split('\t').first); 167 168 // Force disconnect since ADB closes connection after host:devices 169 // response is sent. 170 m_conn.reset(); 171 return error; 172 } 173 174 Error AdbClient::SetPortForwarding(const uint16_t local_port, 175 const uint16_t remote_port) { 176 char message[48]; 177 snprintf(message, sizeof(message), "forward:tcp:%d;tcp:%d", local_port, 178 remote_port); 179 180 const auto error = SendDeviceMessage(message); 181 if (error.Fail()) 182 return error; 183 184 return ReadResponseStatus(); 185 } 186 187 Error AdbClient::SetPortForwarding(const uint16_t local_port, 188 const char *remote_socket_name, 189 const UnixSocketNamespace socket_namespace) { 190 char message[PATH_MAX]; 191 const char *sock_namespace_str = 192 (socket_namespace == UnixSocketNamespaceAbstract) 193 ? kSocketNamespaceAbstract 194 : kSocketNamespaceFileSystem; 195 snprintf(message, sizeof(message), "forward:tcp:%d;%s:%s", local_port, 196 sock_namespace_str, remote_socket_name); 197 198 const auto error = SendDeviceMessage(message); 199 if (error.Fail()) 200 return error; 201 202 return ReadResponseStatus(); 203 } 204 205 Error AdbClient::DeletePortForwarding(const uint16_t local_port) { 206 char message[32]; 207 snprintf(message, sizeof(message), "killforward:tcp:%d", local_port); 208 209 const auto error = SendDeviceMessage(message); 210 if (error.Fail()) 211 return error; 212 213 return ReadResponseStatus(); 214 } 215 216 Error AdbClient::SendMessage(const std::string &packet, const bool reconnect) { 217 Error error; 218 if (!m_conn || reconnect) { 219 error = Connect(); 220 if (error.Fail()) 221 return error; 222 } 223 224 char length_buffer[5]; 225 snprintf(length_buffer, sizeof(length_buffer), "%04x", 226 static_cast<int>(packet.size())); 227 228 ConnectionStatus status; 229 230 m_conn->Write(length_buffer, 4, status, &error); 231 if (error.Fail()) 232 return error; 233 234 m_conn->Write(packet.c_str(), packet.size(), status, &error); 235 return error; 236 } 237 238 Error AdbClient::SendDeviceMessage(const std::string &packet) { 239 std::ostringstream msg; 240 msg << "host-serial:" << m_device_id << ":" << packet; 241 return SendMessage(msg.str()); 242 } 243 244 Error AdbClient::ReadMessage(std::vector<char> &message) { 245 message.clear(); 246 247 char buffer[5]; 248 buffer[4] = 0; 249 250 auto error = ReadAllBytes(buffer, 4); 251 if (error.Fail()) 252 return error; 253 254 unsigned int packet_len = 0; 255 sscanf(buffer, "%x", &packet_len); 256 257 message.resize(packet_len, 0); 258 error = ReadAllBytes(&message[0], packet_len); 259 if (error.Fail()) 260 message.clear(); 261 262 return error; 263 } 264 265 Error AdbClient::ReadMessageStream(std::vector<char> &message, 266 uint32_t timeout_ms) { 267 auto start = std::chrono::steady_clock::now(); 268 message.clear(); 269 270 Error error; 271 lldb::ConnectionStatus status = lldb::eConnectionStatusSuccess; 272 char buffer[1024]; 273 while (error.Success() && status == lldb::eConnectionStatusSuccess) { 274 auto end = std::chrono::steady_clock::now(); 275 uint32_t elapsed_time = 276 std::chrono::duration_cast<std::chrono::milliseconds>(end - start) 277 .count(); 278 if (elapsed_time >= timeout_ms) 279 return Error("Timed out"); 280 281 size_t n = m_conn->Read(buffer, sizeof(buffer), 282 1000 * (timeout_ms - elapsed_time), status, &error); 283 if (n > 0) 284 message.insert(message.end(), &buffer[0], &buffer[n]); 285 } 286 return error; 287 } 288 289 Error AdbClient::ReadResponseStatus() { 290 char response_id[5]; 291 292 static const size_t packet_len = 4; 293 response_id[packet_len] = 0; 294 295 auto error = ReadAllBytes(response_id, packet_len); 296 if (error.Fail()) 297 return error; 298 299 if (strncmp(response_id, kOKAY, packet_len) != 0) 300 return GetResponseError(response_id); 301 302 return error; 303 } 304 305 Error AdbClient::GetResponseError(const char *response_id) { 306 if (strcmp(response_id, kFAIL) != 0) 307 return Error("Got unexpected response id from adb: \"%s\"", response_id); 308 309 std::vector<char> error_message; 310 auto error = ReadMessage(error_message); 311 if (error.Success()) 312 error.SetErrorString( 313 std::string(&error_message[0], error_message.size()).c_str()); 314 315 return error; 316 } 317 318 Error AdbClient::SwitchDeviceTransport() { 319 std::ostringstream msg; 320 msg << "host:transport:" << m_device_id; 321 322 auto error = SendMessage(msg.str()); 323 if (error.Fail()) 324 return error; 325 326 return ReadResponseStatus(); 327 } 328 329 Error AdbClient::StartSync() { 330 auto error = SwitchDeviceTransport(); 331 if (error.Fail()) 332 return Error("Failed to switch to device transport: %s", error.AsCString()); 333 334 error = Sync(); 335 if (error.Fail()) 336 return Error("Sync failed: %s", error.AsCString()); 337 338 return error; 339 } 340 341 Error AdbClient::Sync() { 342 auto error = SendMessage("sync:", false); 343 if (error.Fail()) 344 return error; 345 346 return ReadResponseStatus(); 347 } 348 349 Error AdbClient::ReadAllBytes(void *buffer, size_t size) { 350 return ::ReadAllBytes(*m_conn, buffer, size); 351 } 352 353 Error AdbClient::internalShell(const char *command, uint32_t timeout_ms, 354 std::vector<char> &output_buf) { 355 output_buf.clear(); 356 357 auto error = SwitchDeviceTransport(); 358 if (error.Fail()) 359 return Error("Failed to switch to device transport: %s", error.AsCString()); 360 361 StreamString adb_command; 362 adb_command.Printf("shell:%s", command); 363 error = SendMessage(adb_command.GetData(), false); 364 if (error.Fail()) 365 return error; 366 367 error = ReadResponseStatus(); 368 if (error.Fail()) 369 return error; 370 371 error = ReadMessageStream(output_buf, timeout_ms); 372 if (error.Fail()) 373 return error; 374 375 // ADB doesn't propagate return code of shell execution - if 376 // output starts with /system/bin/sh: most likely command failed. 377 static const char *kShellPrefix = "/system/bin/sh:"; 378 if (output_buf.size() > strlen(kShellPrefix)) { 379 if (!memcmp(&output_buf[0], kShellPrefix, strlen(kShellPrefix))) 380 return Error("Shell command %s failed: %s", command, 381 std::string(output_buf.begin(), output_buf.end()).c_str()); 382 } 383 384 return Error(); 385 } 386 387 Error AdbClient::Shell(const char *command, uint32_t timeout_ms, 388 std::string *output) { 389 std::vector<char> output_buffer; 390 auto error = internalShell(command, timeout_ms, output_buffer); 391 if (error.Fail()) 392 return error; 393 394 if (output) 395 output->assign(output_buffer.begin(), output_buffer.end()); 396 return error; 397 } 398 399 Error AdbClient::ShellToFile(const char *command, uint32_t timeout_ms, 400 const FileSpec &output_file_spec) { 401 std::vector<char> output_buffer; 402 auto error = internalShell(command, timeout_ms, output_buffer); 403 if (error.Fail()) 404 return error; 405 406 const auto output_filename = output_file_spec.GetPath(); 407 std::ofstream dst(output_filename, std::ios::out | std::ios::binary); 408 if (!dst.is_open()) 409 return Error("Unable to open local file %s", output_filename.c_str()); 410 411 dst.write(&output_buffer[0], output_buffer.size()); 412 dst.close(); 413 if (!dst) 414 return Error("Failed to write file %s", output_filename.c_str()); 415 return Error(); 416 } 417 418 std::unique_ptr<AdbClient::SyncService> 419 AdbClient::GetSyncService(Error &error) { 420 std::unique_ptr<SyncService> sync_service; 421 error = StartSync(); 422 if (error.Success()) 423 sync_service.reset(new SyncService(std::move(m_conn))); 424 425 return sync_service; 426 } 427 428 Error AdbClient::SyncService::internalPullFile(const FileSpec &remote_file, 429 const FileSpec &local_file) { 430 const auto local_file_path = local_file.GetPath(); 431 llvm::FileRemover local_file_remover(local_file_path); 432 433 std::ofstream dst(local_file_path, std::ios::out | std::ios::binary); 434 if (!dst.is_open()) 435 return Error("Unable to open local file %s", local_file_path.c_str()); 436 437 const auto remote_file_path = remote_file.GetPath(false); 438 auto error = SendSyncRequest(kRECV, remote_file_path.length(), 439 remote_file_path.c_str()); 440 if (error.Fail()) 441 return error; 442 443 std::vector<char> chunk; 444 bool eof = false; 445 while (!eof) { 446 error = PullFileChunk(chunk, eof); 447 if (error.Fail()) 448 return error; 449 if (!eof) 450 dst.write(&chunk[0], chunk.size()); 451 } 452 453 local_file_remover.releaseFile(); 454 return error; 455 } 456 457 Error AdbClient::SyncService::internalPushFile(const FileSpec &local_file, 458 const FileSpec &remote_file) { 459 const auto local_file_path(local_file.GetPath()); 460 std::ifstream src(local_file_path.c_str(), std::ios::in | std::ios::binary); 461 if (!src.is_open()) 462 return Error("Unable to open local file %s", local_file_path.c_str()); 463 464 std::stringstream file_description; 465 file_description << remote_file.GetPath(false).c_str() << "," << kDefaultMode; 466 std::string file_description_str = file_description.str(); 467 auto error = SendSyncRequest(kSEND, file_description_str.length(), 468 file_description_str.c_str()); 469 if (error.Fail()) 470 return error; 471 472 char chunk[kMaxPushData]; 473 while (!src.eof() && !src.read(chunk, kMaxPushData).bad()) { 474 size_t chunk_size = src.gcount(); 475 error = SendSyncRequest(kDATA, chunk_size, chunk); 476 if (error.Fail()) 477 return Error("Failed to send file chunk: %s", error.AsCString()); 478 } 479 error = SendSyncRequest( 480 kDONE, std::chrono::duration_cast<std::chrono::seconds>( 481 FileSystem::GetModificationTime(local_file).time_since_epoch()) 482 .count(), 483 nullptr); 484 if (error.Fail()) 485 return error; 486 487 std::string response_id; 488 uint32_t data_len; 489 error = ReadSyncHeader(response_id, data_len); 490 if (error.Fail()) 491 return Error("Failed to read DONE response: %s", error.AsCString()); 492 if (response_id == kFAIL) { 493 std::string error_message(data_len, 0); 494 error = ReadAllBytes(&error_message[0], data_len); 495 if (error.Fail()) 496 return Error("Failed to read DONE error message: %s", error.AsCString()); 497 return Error("Failed to push file: %s", error_message.c_str()); 498 } else if (response_id != kOKAY) 499 return Error("Got unexpected DONE response: %s", response_id.c_str()); 500 501 // If there was an error reading the source file, finish the adb file 502 // transfer first so that adb isn't expecting any more data. 503 if (src.bad()) 504 return Error("Failed read on %s", local_file_path.c_str()); 505 return error; 506 } 507 508 Error AdbClient::SyncService::internalStat(const FileSpec &remote_file, 509 uint32_t &mode, uint32_t &size, 510 uint32_t &mtime) { 511 const std::string remote_file_path(remote_file.GetPath(false)); 512 auto error = SendSyncRequest(kSTAT, remote_file_path.length(), 513 remote_file_path.c_str()); 514 if (error.Fail()) 515 return Error("Failed to send request: %s", error.AsCString()); 516 517 static const size_t stat_len = strlen(kSTAT); 518 static const size_t response_len = stat_len + (sizeof(uint32_t) * 3); 519 520 std::vector<char> buffer(response_len); 521 error = ReadAllBytes(&buffer[0], buffer.size()); 522 if (error.Fail()) 523 return Error("Failed to read response: %s", error.AsCString()); 524 525 DataExtractor extractor(&buffer[0], buffer.size(), eByteOrderLittle, 526 sizeof(void *)); 527 offset_t offset = 0; 528 529 const void *command = extractor.GetData(&offset, stat_len); 530 if (!command) 531 return Error("Failed to get response command"); 532 const char *command_str = static_cast<const char *>(command); 533 if (strncmp(command_str, kSTAT, stat_len)) 534 return Error("Got invalid stat command: %s", command_str); 535 536 mode = extractor.GetU32(&offset); 537 size = extractor.GetU32(&offset); 538 mtime = extractor.GetU32(&offset); 539 return Error(); 540 } 541 542 Error AdbClient::SyncService::PullFile(const FileSpec &remote_file, 543 const FileSpec &local_file) { 544 return executeCommand([this, &remote_file, &local_file]() { 545 return internalPullFile(remote_file, local_file); 546 }); 547 } 548 549 Error AdbClient::SyncService::PushFile(const FileSpec &local_file, 550 const FileSpec &remote_file) { 551 return executeCommand([this, &local_file, &remote_file]() { 552 return internalPushFile(local_file, remote_file); 553 }); 554 } 555 556 Error AdbClient::SyncService::Stat(const FileSpec &remote_file, uint32_t &mode, 557 uint32_t &size, uint32_t &mtime) { 558 return executeCommand([this, &remote_file, &mode, &size, &mtime]() { 559 return internalStat(remote_file, mode, size, mtime); 560 }); 561 } 562 563 bool AdbClient::SyncService::IsConnected() const { 564 return m_conn && m_conn->IsConnected(); 565 } 566 567 AdbClient::SyncService::SyncService(std::unique_ptr<Connection> &&conn) 568 : m_conn(std::move(conn)) {} 569 570 Error AdbClient::SyncService::executeCommand( 571 const std::function<Error()> &cmd) { 572 if (!m_conn) 573 return Error("SyncService is disconnected"); 574 575 const auto error = cmd(); 576 if (error.Fail()) 577 m_conn.reset(); 578 579 return error; 580 } 581 582 AdbClient::SyncService::~SyncService() {} 583 584 Error AdbClient::SyncService::SendSyncRequest(const char *request_id, 585 const uint32_t data_len, 586 const void *data) { 587 const DataBufferSP data_sp(new DataBufferHeap(kSyncPacketLen, 0)); 588 DataEncoder encoder(data_sp, eByteOrderLittle, sizeof(void *)); 589 auto offset = encoder.PutData(0, request_id, strlen(request_id)); 590 encoder.PutU32(offset, data_len); 591 592 Error error; 593 ConnectionStatus status; 594 m_conn->Write(data_sp->GetBytes(), kSyncPacketLen, status, &error); 595 if (error.Fail()) 596 return error; 597 598 if (data) 599 m_conn->Write(data, data_len, status, &error); 600 return error; 601 } 602 603 Error AdbClient::SyncService::ReadSyncHeader(std::string &response_id, 604 uint32_t &data_len) { 605 char buffer[kSyncPacketLen]; 606 607 auto error = ReadAllBytes(buffer, kSyncPacketLen); 608 if (error.Success()) { 609 response_id.assign(&buffer[0], 4); 610 DataExtractor extractor(&buffer[4], 4, eByteOrderLittle, sizeof(void *)); 611 offset_t offset = 0; 612 data_len = extractor.GetU32(&offset); 613 } 614 615 return error; 616 } 617 618 Error AdbClient::SyncService::PullFileChunk(std::vector<char> &buffer, 619 bool &eof) { 620 buffer.clear(); 621 622 std::string response_id; 623 uint32_t data_len; 624 auto error = ReadSyncHeader(response_id, data_len); 625 if (error.Fail()) 626 return error; 627 628 if (response_id == kDATA) { 629 buffer.resize(data_len, 0); 630 error = ReadAllBytes(&buffer[0], data_len); 631 if (error.Fail()) 632 buffer.clear(); 633 } else if (response_id == kDONE) { 634 eof = true; 635 } else if (response_id == kFAIL) { 636 std::string error_message(data_len, 0); 637 error = ReadAllBytes(&error_message[0], data_len); 638 if (error.Fail()) 639 return Error("Failed to read pull error message: %s", error.AsCString()); 640 return Error("Failed to pull file: %s", error_message.c_str()); 641 } else 642 return Error("Pull failed with unknown response: %s", response_id.c_str()); 643 644 return Error(); 645 } 646 647 Error AdbClient::SyncService::ReadAllBytes(void *buffer, size_t size) { 648 return ::ReadAllBytes(*m_conn, buffer, size); 649 } 650