1 //===-- CommandObjectPlatform.cpp -------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include <mutex> 10 #include "CommandObjectPlatform.h" 11 #include "lldb/Core/Debugger.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/PluginManager.h" 14 #include "lldb/Host/OptionParser.h" 15 #include "lldb/Host/StringConvert.h" 16 #include "lldb/Interpreter/CommandInterpreter.h" 17 #include "lldb/Interpreter/CommandOptionValidators.h" 18 #include "lldb/Interpreter/CommandReturnObject.h" 19 #include "lldb/Interpreter/OptionGroupFile.h" 20 #include "lldb/Interpreter/OptionGroupPlatform.h" 21 #include "lldb/Target/ExecutionContext.h" 22 #include "lldb/Target/Platform.h" 23 #include "lldb/Target/Process.h" 24 #include "lldb/Utility/Args.h" 25 #include "lldb/Utility/DataExtractor.h" 26 27 #include "llvm/ADT/SmallString.h" 28 #include "llvm/Support/Threading.h" 29 30 using namespace lldb; 31 using namespace lldb_private; 32 33 static mode_t ParsePermissionString(const char *) = delete; 34 35 static mode_t ParsePermissionString(llvm::StringRef permissions) { 36 if (permissions.size() != 9) 37 return (mode_t)(-1); 38 bool user_r, user_w, user_x, group_r, group_w, group_x, world_r, world_w, 39 world_x; 40 41 user_r = (permissions[0] == 'r'); 42 user_w = (permissions[1] == 'w'); 43 user_x = (permissions[2] == 'x'); 44 45 group_r = (permissions[3] == 'r'); 46 group_w = (permissions[4] == 'w'); 47 group_x = (permissions[5] == 'x'); 48 49 world_r = (permissions[6] == 'r'); 50 world_w = (permissions[7] == 'w'); 51 world_x = (permissions[8] == 'x'); 52 53 mode_t user, group, world; 54 user = (user_r ? 4 : 0) | (user_w ? 2 : 0) | (user_x ? 1 : 0); 55 group = (group_r ? 4 : 0) | (group_w ? 2 : 0) | (group_x ? 1 : 0); 56 world = (world_r ? 4 : 0) | (world_w ? 2 : 0) | (world_x ? 1 : 0); 57 58 return user | group | world; 59 } 60 61 static constexpr OptionDefinition g_permissions_options[] = { 62 #define LLDB_OPTIONS_permissions 63 #include "CommandOptions.inc" 64 }; 65 66 class OptionPermissions : public OptionGroup { 67 public: 68 OptionPermissions() {} 69 70 ~OptionPermissions() override = default; 71 72 lldb_private::Status 73 SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 74 ExecutionContext *execution_context) override { 75 Status error; 76 char short_option = (char)GetDefinitions()[option_idx].short_option; 77 switch (short_option) { 78 case 'v': { 79 if (option_arg.getAsInteger(8, m_permissions)) { 80 m_permissions = 0777; 81 error.SetErrorStringWithFormat("invalid value for permissions: %s", 82 option_arg.str().c_str()); 83 } 84 85 } break; 86 case 's': { 87 mode_t perms = ParsePermissionString(option_arg); 88 if (perms == (mode_t)-1) 89 error.SetErrorStringWithFormat("invalid value for permissions: %s", 90 option_arg.str().c_str()); 91 else 92 m_permissions = perms; 93 } break; 94 case 'r': 95 m_permissions |= lldb::eFilePermissionsUserRead; 96 break; 97 case 'w': 98 m_permissions |= lldb::eFilePermissionsUserWrite; 99 break; 100 case 'x': 101 m_permissions |= lldb::eFilePermissionsUserExecute; 102 break; 103 case 'R': 104 m_permissions |= lldb::eFilePermissionsGroupRead; 105 break; 106 case 'W': 107 m_permissions |= lldb::eFilePermissionsGroupWrite; 108 break; 109 case 'X': 110 m_permissions |= lldb::eFilePermissionsGroupExecute; 111 break; 112 case 'd': 113 m_permissions |= lldb::eFilePermissionsWorldRead; 114 break; 115 case 't': 116 m_permissions |= lldb::eFilePermissionsWorldWrite; 117 break; 118 case 'e': 119 m_permissions |= lldb::eFilePermissionsWorldExecute; 120 break; 121 default: 122 error.SetErrorStringWithFormat("unrecognized option '%c'", short_option); 123 break; 124 } 125 126 return error; 127 } 128 129 void OptionParsingStarting(ExecutionContext *execution_context) override { 130 m_permissions = 0; 131 } 132 133 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 134 return llvm::makeArrayRef(g_permissions_options); 135 } 136 137 // Instance variables to hold the values for command options. 138 139 uint32_t m_permissions; 140 141 private: 142 DISALLOW_COPY_AND_ASSIGN(OptionPermissions); 143 }; 144 145 // "platform select <platform-name>" 146 class CommandObjectPlatformSelect : public CommandObjectParsed { 147 public: 148 CommandObjectPlatformSelect(CommandInterpreter &interpreter) 149 : CommandObjectParsed(interpreter, "platform select", 150 "Create a platform if needed and select it as the " 151 "current platform.", 152 "platform select <platform-name>", 0), 153 m_option_group(), 154 m_platform_options( 155 false) // Don't include the "--platform" option by passing false 156 { 157 m_option_group.Append(&m_platform_options, LLDB_OPT_SET_ALL, 1); 158 m_option_group.Finalize(); 159 } 160 161 ~CommandObjectPlatformSelect() override = default; 162 163 int HandleCompletion(CompletionRequest &request) override { 164 CommandCompletions::PlatformPluginNames(GetCommandInterpreter(), request, 165 nullptr); 166 return request.GetNumberOfMatches(); 167 } 168 169 Options *GetOptions() override { return &m_option_group; } 170 171 protected: 172 bool DoExecute(Args &args, CommandReturnObject &result) override { 173 if (args.GetArgumentCount() == 1) { 174 const char *platform_name = args.GetArgumentAtIndex(0); 175 if (platform_name && platform_name[0]) { 176 const bool select = true; 177 m_platform_options.SetPlatformName(platform_name); 178 Status error; 179 ArchSpec platform_arch; 180 PlatformSP platform_sp(m_platform_options.CreatePlatformWithOptions( 181 m_interpreter, ArchSpec(), select, error, platform_arch)); 182 if (platform_sp) { 183 GetDebugger().GetPlatformList().SetSelectedPlatform(platform_sp); 184 185 platform_sp->GetStatus(result.GetOutputStream()); 186 result.SetStatus(eReturnStatusSuccessFinishResult); 187 } else { 188 result.AppendError(error.AsCString()); 189 result.SetStatus(eReturnStatusFailed); 190 } 191 } else { 192 result.AppendError("invalid platform name"); 193 result.SetStatus(eReturnStatusFailed); 194 } 195 } else { 196 result.AppendError( 197 "platform create takes a platform name as an argument\n"); 198 result.SetStatus(eReturnStatusFailed); 199 } 200 return result.Succeeded(); 201 } 202 203 OptionGroupOptions m_option_group; 204 OptionGroupPlatform m_platform_options; 205 }; 206 207 // "platform list" 208 class CommandObjectPlatformList : public CommandObjectParsed { 209 public: 210 CommandObjectPlatformList(CommandInterpreter &interpreter) 211 : CommandObjectParsed(interpreter, "platform list", 212 "List all platforms that are available.", nullptr, 213 0) {} 214 215 ~CommandObjectPlatformList() override = default; 216 217 protected: 218 bool DoExecute(Args &args, CommandReturnObject &result) override { 219 Stream &ostrm = result.GetOutputStream(); 220 ostrm.Printf("Available platforms:\n"); 221 222 PlatformSP host_platform_sp(Platform::GetHostPlatform()); 223 ostrm.Printf("%s: %s\n", host_platform_sp->GetPluginName().GetCString(), 224 host_platform_sp->GetDescription()); 225 226 uint32_t idx; 227 for (idx = 0; true; ++idx) { 228 const char *plugin_name = 229 PluginManager::GetPlatformPluginNameAtIndex(idx); 230 if (plugin_name == nullptr) 231 break; 232 const char *plugin_desc = 233 PluginManager::GetPlatformPluginDescriptionAtIndex(idx); 234 if (plugin_desc == nullptr) 235 break; 236 ostrm.Printf("%s: %s\n", plugin_name, plugin_desc); 237 } 238 239 if (idx == 0) { 240 result.AppendError("no platforms are available\n"); 241 result.SetStatus(eReturnStatusFailed); 242 } else 243 result.SetStatus(eReturnStatusSuccessFinishResult); 244 return result.Succeeded(); 245 } 246 }; 247 248 // "platform status" 249 class CommandObjectPlatformStatus : public CommandObjectParsed { 250 public: 251 CommandObjectPlatformStatus(CommandInterpreter &interpreter) 252 : CommandObjectParsed(interpreter, "platform status", 253 "Display status for the current platform.", nullptr, 254 0) {} 255 256 ~CommandObjectPlatformStatus() override = default; 257 258 protected: 259 bool DoExecute(Args &args, CommandReturnObject &result) override { 260 Stream &ostrm = result.GetOutputStream(); 261 262 Target *target = GetDebugger().GetSelectedTarget().get(); 263 PlatformSP platform_sp; 264 if (target) { 265 platform_sp = target->GetPlatform(); 266 } 267 if (!platform_sp) { 268 platform_sp = GetDebugger().GetPlatformList().GetSelectedPlatform(); 269 } 270 if (platform_sp) { 271 platform_sp->GetStatus(ostrm); 272 result.SetStatus(eReturnStatusSuccessFinishResult); 273 } else { 274 result.AppendError("no platform is currently selected\n"); 275 result.SetStatus(eReturnStatusFailed); 276 } 277 return result.Succeeded(); 278 } 279 }; 280 281 // "platform connect <connect-url>" 282 class CommandObjectPlatformConnect : public CommandObjectParsed { 283 public: 284 CommandObjectPlatformConnect(CommandInterpreter &interpreter) 285 : CommandObjectParsed( 286 interpreter, "platform connect", 287 "Select the current platform by providing a connection URL.", 288 "platform connect <connect-url>", 0) {} 289 290 ~CommandObjectPlatformConnect() override = default; 291 292 protected: 293 bool DoExecute(Args &args, CommandReturnObject &result) override { 294 Stream &ostrm = result.GetOutputStream(); 295 296 PlatformSP platform_sp( 297 GetDebugger().GetPlatformList().GetSelectedPlatform()); 298 if (platform_sp) { 299 Status error(platform_sp->ConnectRemote(args)); 300 if (error.Success()) { 301 platform_sp->GetStatus(ostrm); 302 result.SetStatus(eReturnStatusSuccessFinishResult); 303 304 platform_sp->ConnectToWaitingProcesses(GetDebugger(), error); 305 if (error.Fail()) { 306 result.AppendError(error.AsCString()); 307 result.SetStatus(eReturnStatusFailed); 308 } 309 } else { 310 result.AppendErrorWithFormat("%s\n", error.AsCString()); 311 result.SetStatus(eReturnStatusFailed); 312 } 313 } else { 314 result.AppendError("no platform is currently selected\n"); 315 result.SetStatus(eReturnStatusFailed); 316 } 317 return result.Succeeded(); 318 } 319 320 Options *GetOptions() override { 321 PlatformSP platform_sp( 322 GetDebugger().GetPlatformList().GetSelectedPlatform()); 323 OptionGroupOptions *m_platform_options = nullptr; 324 if (platform_sp) { 325 m_platform_options = platform_sp->GetConnectionOptions(m_interpreter); 326 if (m_platform_options != nullptr && !m_platform_options->m_did_finalize) 327 m_platform_options->Finalize(); 328 } 329 return m_platform_options; 330 } 331 }; 332 333 // "platform disconnect" 334 class CommandObjectPlatformDisconnect : public CommandObjectParsed { 335 public: 336 CommandObjectPlatformDisconnect(CommandInterpreter &interpreter) 337 : CommandObjectParsed(interpreter, "platform disconnect", 338 "Disconnect from the current platform.", 339 "platform disconnect", 0) {} 340 341 ~CommandObjectPlatformDisconnect() override = default; 342 343 protected: 344 bool DoExecute(Args &args, CommandReturnObject &result) override { 345 PlatformSP platform_sp( 346 GetDebugger().GetPlatformList().GetSelectedPlatform()); 347 if (platform_sp) { 348 if (args.GetArgumentCount() == 0) { 349 Status error; 350 351 if (platform_sp->IsConnected()) { 352 // Cache the instance name if there is one since we are about to 353 // disconnect and the name might go with it. 354 const char *hostname_cstr = platform_sp->GetHostname(); 355 std::string hostname; 356 if (hostname_cstr) 357 hostname.assign(hostname_cstr); 358 359 error = platform_sp->DisconnectRemote(); 360 if (error.Success()) { 361 Stream &ostrm = result.GetOutputStream(); 362 if (hostname.empty()) 363 ostrm.Printf("Disconnected from \"%s\"\n", 364 platform_sp->GetPluginName().GetCString()); 365 else 366 ostrm.Printf("Disconnected from \"%s\"\n", hostname.c_str()); 367 result.SetStatus(eReturnStatusSuccessFinishResult); 368 } else { 369 result.AppendErrorWithFormat("%s", error.AsCString()); 370 result.SetStatus(eReturnStatusFailed); 371 } 372 } else { 373 // Not connected... 374 result.AppendErrorWithFormat( 375 "not connected to '%s'", 376 platform_sp->GetPluginName().GetCString()); 377 result.SetStatus(eReturnStatusFailed); 378 } 379 } else { 380 // Bad args 381 result.AppendError( 382 "\"platform disconnect\" doesn't take any arguments"); 383 result.SetStatus(eReturnStatusFailed); 384 } 385 } else { 386 result.AppendError("no platform is currently selected"); 387 result.SetStatus(eReturnStatusFailed); 388 } 389 return result.Succeeded(); 390 } 391 }; 392 393 // "platform settings" 394 class CommandObjectPlatformSettings : public CommandObjectParsed { 395 public: 396 CommandObjectPlatformSettings(CommandInterpreter &interpreter) 397 : CommandObjectParsed(interpreter, "platform settings", 398 "Set settings for the current target's platform, " 399 "or for a platform by name.", 400 "platform settings", 0), 401 m_options(), 402 m_option_working_dir(LLDB_OPT_SET_1, false, "working-dir", 'w', 0, 403 eArgTypePath, 404 "The working directory for the platform.") { 405 m_options.Append(&m_option_working_dir, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 406 } 407 408 ~CommandObjectPlatformSettings() override = default; 409 410 protected: 411 bool DoExecute(Args &args, CommandReturnObject &result) override { 412 PlatformSP platform_sp( 413 GetDebugger().GetPlatformList().GetSelectedPlatform()); 414 if (platform_sp) { 415 if (m_option_working_dir.GetOptionValue().OptionWasSet()) 416 platform_sp->SetWorkingDirectory( 417 m_option_working_dir.GetOptionValue().GetCurrentValue()); 418 } else { 419 result.AppendError("no platform is currently selected"); 420 result.SetStatus(eReturnStatusFailed); 421 } 422 return result.Succeeded(); 423 } 424 425 Options *GetOptions() override { 426 if (!m_options.DidFinalize()) 427 m_options.Finalize(); 428 return &m_options; 429 } 430 431 protected: 432 OptionGroupOptions m_options; 433 OptionGroupFile m_option_working_dir; 434 }; 435 436 // "platform mkdir" 437 class CommandObjectPlatformMkDir : public CommandObjectParsed { 438 public: 439 CommandObjectPlatformMkDir(CommandInterpreter &interpreter) 440 : CommandObjectParsed(interpreter, "platform mkdir", 441 "Make a new directory on the remote end.", nullptr, 442 0), 443 m_options() {} 444 445 ~CommandObjectPlatformMkDir() override = default; 446 447 bool DoExecute(Args &args, CommandReturnObject &result) override { 448 PlatformSP platform_sp( 449 GetDebugger().GetPlatformList().GetSelectedPlatform()); 450 if (platform_sp) { 451 std::string cmd_line; 452 args.GetCommandString(cmd_line); 453 uint32_t mode; 454 const OptionPermissions *options_permissions = 455 (const OptionPermissions *)m_options.GetGroupWithOption('r'); 456 if (options_permissions) 457 mode = options_permissions->m_permissions; 458 else 459 mode = lldb::eFilePermissionsUserRWX | lldb::eFilePermissionsGroupRWX | 460 lldb::eFilePermissionsWorldRX; 461 Status error = platform_sp->MakeDirectory(FileSpec(cmd_line), mode); 462 if (error.Success()) { 463 result.SetStatus(eReturnStatusSuccessFinishResult); 464 } else { 465 result.AppendError(error.AsCString()); 466 result.SetStatus(eReturnStatusFailed); 467 } 468 } else { 469 result.AppendError("no platform currently selected\n"); 470 result.SetStatus(eReturnStatusFailed); 471 } 472 return result.Succeeded(); 473 } 474 475 Options *GetOptions() override { 476 if (!m_options.DidFinalize()) { 477 m_options.Append(new OptionPermissions()); 478 m_options.Finalize(); 479 } 480 return &m_options; 481 } 482 483 OptionGroupOptions m_options; 484 }; 485 486 // "platform fopen" 487 class CommandObjectPlatformFOpen : public CommandObjectParsed { 488 public: 489 CommandObjectPlatformFOpen(CommandInterpreter &interpreter) 490 : CommandObjectParsed(interpreter, "platform file open", 491 "Open a file on the remote end.", nullptr, 0), 492 m_options() {} 493 494 ~CommandObjectPlatformFOpen() override = default; 495 496 bool DoExecute(Args &args, CommandReturnObject &result) override { 497 PlatformSP platform_sp( 498 GetDebugger().GetPlatformList().GetSelectedPlatform()); 499 if (platform_sp) { 500 Status error; 501 std::string cmd_line; 502 args.GetCommandString(cmd_line); 503 mode_t perms; 504 const OptionPermissions *options_permissions = 505 (const OptionPermissions *)m_options.GetGroupWithOption('r'); 506 if (options_permissions) 507 perms = options_permissions->m_permissions; 508 else 509 perms = lldb::eFilePermissionsUserRW | lldb::eFilePermissionsGroupRW | 510 lldb::eFilePermissionsWorldRead; 511 lldb::user_id_t fd = platform_sp->OpenFile( 512 FileSpec(cmd_line), 513 File::eOpenOptionRead | File::eOpenOptionWrite | 514 File::eOpenOptionAppend | File::eOpenOptionCanCreate, 515 perms, error); 516 if (error.Success()) { 517 result.AppendMessageWithFormat("File Descriptor = %" PRIu64 "\n", fd); 518 result.SetStatus(eReturnStatusSuccessFinishResult); 519 } else { 520 result.AppendError(error.AsCString()); 521 result.SetStatus(eReturnStatusFailed); 522 } 523 } else { 524 result.AppendError("no platform currently selected\n"); 525 result.SetStatus(eReturnStatusFailed); 526 } 527 return result.Succeeded(); 528 } 529 530 Options *GetOptions() override { 531 if (!m_options.DidFinalize()) { 532 m_options.Append(new OptionPermissions()); 533 m_options.Finalize(); 534 } 535 return &m_options; 536 } 537 538 OptionGroupOptions m_options; 539 }; 540 541 // "platform fclose" 542 class CommandObjectPlatformFClose : public CommandObjectParsed { 543 public: 544 CommandObjectPlatformFClose(CommandInterpreter &interpreter) 545 : CommandObjectParsed(interpreter, "platform file close", 546 "Close a file on the remote end.", nullptr, 0) {} 547 548 ~CommandObjectPlatformFClose() override = default; 549 550 bool DoExecute(Args &args, CommandReturnObject &result) override { 551 PlatformSP platform_sp( 552 GetDebugger().GetPlatformList().GetSelectedPlatform()); 553 if (platform_sp) { 554 std::string cmd_line; 555 args.GetCommandString(cmd_line); 556 const lldb::user_id_t fd = 557 StringConvert::ToUInt64(cmd_line.c_str(), UINT64_MAX); 558 Status error; 559 bool success = platform_sp->CloseFile(fd, error); 560 if (success) { 561 result.AppendMessageWithFormat("file %" PRIu64 " closed.\n", fd); 562 result.SetStatus(eReturnStatusSuccessFinishResult); 563 } else { 564 result.AppendError(error.AsCString()); 565 result.SetStatus(eReturnStatusFailed); 566 } 567 } else { 568 result.AppendError("no platform currently selected\n"); 569 result.SetStatus(eReturnStatusFailed); 570 } 571 return result.Succeeded(); 572 } 573 }; 574 575 // "platform fread" 576 577 static constexpr OptionDefinition g_platform_fread_options[] = { 578 #define LLDB_OPTIONS_platform_fread 579 #include "CommandOptions.inc" 580 }; 581 582 class CommandObjectPlatformFRead : public CommandObjectParsed { 583 public: 584 CommandObjectPlatformFRead(CommandInterpreter &interpreter) 585 : CommandObjectParsed(interpreter, "platform file read", 586 "Read data from a file on the remote end.", nullptr, 587 0), 588 m_options() {} 589 590 ~CommandObjectPlatformFRead() override = default; 591 592 bool DoExecute(Args &args, CommandReturnObject &result) override { 593 PlatformSP platform_sp( 594 GetDebugger().GetPlatformList().GetSelectedPlatform()); 595 if (platform_sp) { 596 std::string cmd_line; 597 args.GetCommandString(cmd_line); 598 const lldb::user_id_t fd = 599 StringConvert::ToUInt64(cmd_line.c_str(), UINT64_MAX); 600 std::string buffer(m_options.m_count, 0); 601 Status error; 602 uint32_t retcode = platform_sp->ReadFile( 603 fd, m_options.m_offset, &buffer[0], m_options.m_count, error); 604 result.AppendMessageWithFormat("Return = %d\n", retcode); 605 result.AppendMessageWithFormat("Data = \"%s\"\n", buffer.c_str()); 606 result.SetStatus(eReturnStatusSuccessFinishResult); 607 } else { 608 result.AppendError("no platform currently selected\n"); 609 result.SetStatus(eReturnStatusFailed); 610 } 611 return result.Succeeded(); 612 } 613 614 Options *GetOptions() override { return &m_options; } 615 616 protected: 617 class CommandOptions : public Options { 618 public: 619 CommandOptions() : Options() {} 620 621 ~CommandOptions() override = default; 622 623 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 624 ExecutionContext *execution_context) override { 625 Status error; 626 char short_option = (char)m_getopt_table[option_idx].val; 627 628 switch (short_option) { 629 case 'o': 630 if (option_arg.getAsInteger(0, m_offset)) 631 error.SetErrorStringWithFormat("invalid offset: '%s'", 632 option_arg.str().c_str()); 633 break; 634 case 'c': 635 if (option_arg.getAsInteger(0, m_count)) 636 error.SetErrorStringWithFormat("invalid offset: '%s'", 637 option_arg.str().c_str()); 638 break; 639 default: 640 error.SetErrorStringWithFormat("unrecognized option '%c'", 641 short_option); 642 break; 643 } 644 645 return error; 646 } 647 648 void OptionParsingStarting(ExecutionContext *execution_context) override { 649 m_offset = 0; 650 m_count = 1; 651 } 652 653 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 654 return llvm::makeArrayRef(g_platform_fread_options); 655 } 656 657 // Instance variables to hold the values for command options. 658 659 uint32_t m_offset; 660 uint32_t m_count; 661 }; 662 663 CommandOptions m_options; 664 }; 665 666 // "platform fwrite" 667 668 static constexpr OptionDefinition g_platform_fwrite_options[] = { 669 #define LLDB_OPTIONS_platform_fwrite 670 #include "CommandOptions.inc" 671 }; 672 673 class CommandObjectPlatformFWrite : public CommandObjectParsed { 674 public: 675 CommandObjectPlatformFWrite(CommandInterpreter &interpreter) 676 : CommandObjectParsed(interpreter, "platform file write", 677 "Write data to a file on the remote end.", nullptr, 678 0), 679 m_options() {} 680 681 ~CommandObjectPlatformFWrite() override = default; 682 683 bool DoExecute(Args &args, CommandReturnObject &result) override { 684 PlatformSP platform_sp( 685 GetDebugger().GetPlatformList().GetSelectedPlatform()); 686 if (platform_sp) { 687 std::string cmd_line; 688 args.GetCommandString(cmd_line); 689 Status error; 690 const lldb::user_id_t fd = 691 StringConvert::ToUInt64(cmd_line.c_str(), UINT64_MAX); 692 uint32_t retcode = 693 platform_sp->WriteFile(fd, m_options.m_offset, &m_options.m_data[0], 694 m_options.m_data.size(), error); 695 result.AppendMessageWithFormat("Return = %d\n", retcode); 696 result.SetStatus(eReturnStatusSuccessFinishResult); 697 } else { 698 result.AppendError("no platform currently selected\n"); 699 result.SetStatus(eReturnStatusFailed); 700 } 701 return result.Succeeded(); 702 } 703 704 Options *GetOptions() override { return &m_options; } 705 706 protected: 707 class CommandOptions : public Options { 708 public: 709 CommandOptions() : Options() {} 710 711 ~CommandOptions() override = default; 712 713 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 714 ExecutionContext *execution_context) override { 715 Status error; 716 char short_option = (char)m_getopt_table[option_idx].val; 717 718 switch (short_option) { 719 case 'o': 720 if (option_arg.getAsInteger(0, m_offset)) 721 error.SetErrorStringWithFormat("invalid offset: '%s'", 722 option_arg.str().c_str()); 723 break; 724 case 'd': 725 m_data.assign(option_arg); 726 break; 727 default: 728 error.SetErrorStringWithFormat("unrecognized option '%c'", 729 short_option); 730 break; 731 } 732 733 return error; 734 } 735 736 void OptionParsingStarting(ExecutionContext *execution_context) override { 737 m_offset = 0; 738 m_data.clear(); 739 } 740 741 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 742 return llvm::makeArrayRef(g_platform_fwrite_options); 743 } 744 745 // Instance variables to hold the values for command options. 746 747 uint32_t m_offset; 748 std::string m_data; 749 }; 750 751 CommandOptions m_options; 752 }; 753 754 class CommandObjectPlatformFile : public CommandObjectMultiword { 755 public: 756 // Constructors and Destructors 757 CommandObjectPlatformFile(CommandInterpreter &interpreter) 758 : CommandObjectMultiword( 759 interpreter, "platform file", 760 "Commands to access files on the current platform.", 761 "platform file [open|close|read|write] ...") { 762 LoadSubCommand( 763 "open", CommandObjectSP(new CommandObjectPlatformFOpen(interpreter))); 764 LoadSubCommand( 765 "close", CommandObjectSP(new CommandObjectPlatformFClose(interpreter))); 766 LoadSubCommand( 767 "read", CommandObjectSP(new CommandObjectPlatformFRead(interpreter))); 768 LoadSubCommand( 769 "write", CommandObjectSP(new CommandObjectPlatformFWrite(interpreter))); 770 } 771 772 ~CommandObjectPlatformFile() override = default; 773 774 private: 775 // For CommandObjectPlatform only 776 DISALLOW_COPY_AND_ASSIGN(CommandObjectPlatformFile); 777 }; 778 779 // "platform get-file remote-file-path host-file-path" 780 class CommandObjectPlatformGetFile : public CommandObjectParsed { 781 public: 782 CommandObjectPlatformGetFile(CommandInterpreter &interpreter) 783 : CommandObjectParsed( 784 interpreter, "platform get-file", 785 "Transfer a file from the remote end to the local host.", 786 "platform get-file <remote-file-spec> <local-file-spec>", 0) { 787 SetHelpLong( 788 R"(Examples: 789 790 (lldb) platform get-file /the/remote/file/path /the/local/file/path 791 792 Transfer a file from the remote end with file path /the/remote/file/path to the local host.)"); 793 794 CommandArgumentEntry arg1, arg2; 795 CommandArgumentData file_arg_remote, file_arg_host; 796 797 // Define the first (and only) variant of this arg. 798 file_arg_remote.arg_type = eArgTypeFilename; 799 file_arg_remote.arg_repetition = eArgRepeatPlain; 800 // There is only one variant this argument could be; put it into the 801 // argument entry. 802 arg1.push_back(file_arg_remote); 803 804 // Define the second (and only) variant of this arg. 805 file_arg_host.arg_type = eArgTypeFilename; 806 file_arg_host.arg_repetition = eArgRepeatPlain; 807 // There is only one variant this argument could be; put it into the 808 // argument entry. 809 arg2.push_back(file_arg_host); 810 811 // Push the data for the first and the second arguments into the 812 // m_arguments vector. 813 m_arguments.push_back(arg1); 814 m_arguments.push_back(arg2); 815 } 816 817 ~CommandObjectPlatformGetFile() override = default; 818 819 bool DoExecute(Args &args, CommandReturnObject &result) override { 820 // If the number of arguments is incorrect, issue an error message. 821 if (args.GetArgumentCount() != 2) { 822 result.GetErrorStream().Printf("error: required arguments missing; " 823 "specify both the source and destination " 824 "file paths\n"); 825 result.SetStatus(eReturnStatusFailed); 826 return false; 827 } 828 829 PlatformSP platform_sp( 830 GetDebugger().GetPlatformList().GetSelectedPlatform()); 831 if (platform_sp) { 832 const char *remote_file_path = args.GetArgumentAtIndex(0); 833 const char *local_file_path = args.GetArgumentAtIndex(1); 834 Status error = platform_sp->GetFile(FileSpec(remote_file_path), 835 FileSpec(local_file_path)); 836 if (error.Success()) { 837 result.AppendMessageWithFormat( 838 "successfully get-file from %s (remote) to %s (host)\n", 839 remote_file_path, local_file_path); 840 result.SetStatus(eReturnStatusSuccessFinishResult); 841 } else { 842 result.AppendMessageWithFormat("get-file failed: %s\n", 843 error.AsCString()); 844 result.SetStatus(eReturnStatusFailed); 845 } 846 } else { 847 result.AppendError("no platform currently selected\n"); 848 result.SetStatus(eReturnStatusFailed); 849 } 850 return result.Succeeded(); 851 } 852 }; 853 854 // "platform get-size remote-file-path" 855 class CommandObjectPlatformGetSize : public CommandObjectParsed { 856 public: 857 CommandObjectPlatformGetSize(CommandInterpreter &interpreter) 858 : CommandObjectParsed(interpreter, "platform get-size", 859 "Get the file size from the remote end.", 860 "platform get-size <remote-file-spec>", 0) { 861 SetHelpLong( 862 R"(Examples: 863 864 (lldb) platform get-size /the/remote/file/path 865 866 Get the file size from the remote end with path /the/remote/file/path.)"); 867 868 CommandArgumentEntry arg1; 869 CommandArgumentData file_arg_remote; 870 871 // Define the first (and only) variant of this arg. 872 file_arg_remote.arg_type = eArgTypeFilename; 873 file_arg_remote.arg_repetition = eArgRepeatPlain; 874 // There is only one variant this argument could be; put it into the 875 // argument entry. 876 arg1.push_back(file_arg_remote); 877 878 // Push the data for the first argument into the m_arguments vector. 879 m_arguments.push_back(arg1); 880 } 881 882 ~CommandObjectPlatformGetSize() override = default; 883 884 bool DoExecute(Args &args, CommandReturnObject &result) override { 885 // If the number of arguments is incorrect, issue an error message. 886 if (args.GetArgumentCount() != 1) { 887 result.GetErrorStream().Printf("error: required argument missing; " 888 "specify the source file path as the only " 889 "argument\n"); 890 result.SetStatus(eReturnStatusFailed); 891 return false; 892 } 893 894 PlatformSP platform_sp( 895 GetDebugger().GetPlatformList().GetSelectedPlatform()); 896 if (platform_sp) { 897 std::string remote_file_path(args.GetArgumentAtIndex(0)); 898 user_id_t size = platform_sp->GetFileSize(FileSpec(remote_file_path)); 899 if (size != UINT64_MAX) { 900 result.AppendMessageWithFormat("File size of %s (remote): %" PRIu64 901 "\n", 902 remote_file_path.c_str(), size); 903 result.SetStatus(eReturnStatusSuccessFinishResult); 904 } else { 905 result.AppendMessageWithFormat( 906 "Error getting file size of %s (remote)\n", 907 remote_file_path.c_str()); 908 result.SetStatus(eReturnStatusFailed); 909 } 910 } else { 911 result.AppendError("no platform currently selected\n"); 912 result.SetStatus(eReturnStatusFailed); 913 } 914 return result.Succeeded(); 915 } 916 }; 917 918 // "platform put-file" 919 class CommandObjectPlatformPutFile : public CommandObjectParsed { 920 public: 921 CommandObjectPlatformPutFile(CommandInterpreter &interpreter) 922 : CommandObjectParsed( 923 interpreter, "platform put-file", 924 "Transfer a file from this system to the remote end.", nullptr, 0) { 925 } 926 927 ~CommandObjectPlatformPutFile() override = default; 928 929 bool DoExecute(Args &args, CommandReturnObject &result) override { 930 const char *src = args.GetArgumentAtIndex(0); 931 const char *dst = args.GetArgumentAtIndex(1); 932 933 FileSpec src_fs(src); 934 FileSystem::Instance().Resolve(src_fs); 935 FileSpec dst_fs(dst ? dst : src_fs.GetFilename().GetCString()); 936 937 PlatformSP platform_sp( 938 GetDebugger().GetPlatformList().GetSelectedPlatform()); 939 if (platform_sp) { 940 Status error(platform_sp->PutFile(src_fs, dst_fs)); 941 if (error.Success()) { 942 result.SetStatus(eReturnStatusSuccessFinishNoResult); 943 } else { 944 result.AppendError(error.AsCString()); 945 result.SetStatus(eReturnStatusFailed); 946 } 947 } else { 948 result.AppendError("no platform currently selected\n"); 949 result.SetStatus(eReturnStatusFailed); 950 } 951 return result.Succeeded(); 952 } 953 }; 954 955 // "platform process launch" 956 class CommandObjectPlatformProcessLaunch : public CommandObjectParsed { 957 public: 958 CommandObjectPlatformProcessLaunch(CommandInterpreter &interpreter) 959 : CommandObjectParsed(interpreter, "platform process launch", 960 "Launch a new process on a remote platform.", 961 "platform process launch program", 962 eCommandRequiresTarget | eCommandTryTargetAPILock), 963 m_options() {} 964 965 ~CommandObjectPlatformProcessLaunch() override = default; 966 967 Options *GetOptions() override { return &m_options; } 968 969 protected: 970 bool DoExecute(Args &args, CommandReturnObject &result) override { 971 Target *target = GetDebugger().GetSelectedTarget().get(); 972 PlatformSP platform_sp; 973 if (target) { 974 platform_sp = target->GetPlatform(); 975 } 976 if (!platform_sp) { 977 platform_sp = GetDebugger().GetPlatformList().GetSelectedPlatform(); 978 } 979 980 if (platform_sp) { 981 Status error; 982 const size_t argc = args.GetArgumentCount(); 983 Target *target = m_exe_ctx.GetTargetPtr(); 984 Module *exe_module = target->GetExecutableModulePointer(); 985 if (exe_module) { 986 m_options.launch_info.GetExecutableFile() = exe_module->GetFileSpec(); 987 llvm::SmallString<128> exe_path; 988 m_options.launch_info.GetExecutableFile().GetPath(exe_path); 989 if (!exe_path.empty()) 990 m_options.launch_info.GetArguments().AppendArgument(exe_path); 991 m_options.launch_info.GetArchitecture() = exe_module->GetArchitecture(); 992 } 993 994 if (argc > 0) { 995 if (m_options.launch_info.GetExecutableFile()) { 996 // We already have an executable file, so we will use this and all 997 // arguments to this function are extra arguments 998 m_options.launch_info.GetArguments().AppendArguments(args); 999 } else { 1000 // We don't have any file yet, so the first argument is our 1001 // executable, and the rest are program arguments 1002 const bool first_arg_is_executable = true; 1003 m_options.launch_info.SetArguments(args, first_arg_is_executable); 1004 } 1005 } 1006 1007 if (m_options.launch_info.GetExecutableFile()) { 1008 Debugger &debugger = GetDebugger(); 1009 1010 if (argc == 0) 1011 target->GetRunArguments(m_options.launch_info.GetArguments()); 1012 1013 ProcessSP process_sp(platform_sp->DebugProcess( 1014 m_options.launch_info, debugger, target, error)); 1015 if (process_sp && process_sp->IsAlive()) { 1016 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1017 return true; 1018 } 1019 1020 if (error.Success()) 1021 result.AppendError("process launch failed"); 1022 else 1023 result.AppendError(error.AsCString()); 1024 result.SetStatus(eReturnStatusFailed); 1025 } else { 1026 result.AppendError("'platform process launch' uses the current target " 1027 "file and arguments, or the executable and its " 1028 "arguments can be specified in this command"); 1029 result.SetStatus(eReturnStatusFailed); 1030 return false; 1031 } 1032 } else { 1033 result.AppendError("no platform is selected\n"); 1034 } 1035 return result.Succeeded(); 1036 } 1037 1038 protected: 1039 ProcessLaunchCommandOptions m_options; 1040 }; 1041 1042 // "platform process list" 1043 1044 static PosixPlatformCommandOptionValidator posix_validator; 1045 static constexpr OptionDefinition g_platform_process_list_options[] = { 1046 #define LLDB_OPTIONS_platform_process_list 1047 #include "CommandOptions.inc" 1048 }; 1049 1050 class CommandObjectPlatformProcessList : public CommandObjectParsed { 1051 public: 1052 CommandObjectPlatformProcessList(CommandInterpreter &interpreter) 1053 : CommandObjectParsed(interpreter, "platform process list", 1054 "List processes on a remote platform by name, pid, " 1055 "or many other matching attributes.", 1056 "platform process list", 0), 1057 m_options() {} 1058 1059 ~CommandObjectPlatformProcessList() override = default; 1060 1061 Options *GetOptions() override { return &m_options; } 1062 1063 protected: 1064 bool DoExecute(Args &args, CommandReturnObject &result) override { 1065 Target *target = GetDebugger().GetSelectedTarget().get(); 1066 PlatformSP platform_sp; 1067 if (target) { 1068 platform_sp = target->GetPlatform(); 1069 } 1070 if (!platform_sp) { 1071 platform_sp = GetDebugger().GetPlatformList().GetSelectedPlatform(); 1072 } 1073 1074 if (platform_sp) { 1075 Status error; 1076 if (args.GetArgumentCount() == 0) { 1077 if (platform_sp) { 1078 Stream &ostrm = result.GetOutputStream(); 1079 1080 lldb::pid_t pid = 1081 m_options.match_info.GetProcessInfo().GetProcessID(); 1082 if (pid != LLDB_INVALID_PROCESS_ID) { 1083 ProcessInstanceInfo proc_info; 1084 if (platform_sp->GetProcessInfo(pid, proc_info)) { 1085 ProcessInstanceInfo::DumpTableHeader(ostrm, m_options.show_args, 1086 m_options.verbose); 1087 proc_info.DumpAsTableRow(ostrm, platform_sp->GetUserIDResolver(), 1088 m_options.show_args, m_options.verbose); 1089 result.SetStatus(eReturnStatusSuccessFinishResult); 1090 } else { 1091 result.AppendErrorWithFormat( 1092 "no process found with pid = %" PRIu64 "\n", pid); 1093 result.SetStatus(eReturnStatusFailed); 1094 } 1095 } else { 1096 ProcessInstanceInfoList proc_infos; 1097 const uint32_t matches = 1098 platform_sp->FindProcesses(m_options.match_info, proc_infos); 1099 const char *match_desc = nullptr; 1100 const char *match_name = 1101 m_options.match_info.GetProcessInfo().GetName(); 1102 if (match_name && match_name[0]) { 1103 switch (m_options.match_info.GetNameMatchType()) { 1104 case NameMatch::Ignore: 1105 break; 1106 case NameMatch::Equals: 1107 match_desc = "matched"; 1108 break; 1109 case NameMatch::Contains: 1110 match_desc = "contained"; 1111 break; 1112 case NameMatch::StartsWith: 1113 match_desc = "started with"; 1114 break; 1115 case NameMatch::EndsWith: 1116 match_desc = "ended with"; 1117 break; 1118 case NameMatch::RegularExpression: 1119 match_desc = "matched the regular expression"; 1120 break; 1121 } 1122 } 1123 1124 if (matches == 0) { 1125 if (match_desc) 1126 result.AppendErrorWithFormat( 1127 "no processes were found that %s \"%s\" on the \"%s\" " 1128 "platform\n", 1129 match_desc, match_name, 1130 platform_sp->GetPluginName().GetCString()); 1131 else 1132 result.AppendErrorWithFormat( 1133 "no processes were found on the \"%s\" platform\n", 1134 platform_sp->GetPluginName().GetCString()); 1135 result.SetStatus(eReturnStatusFailed); 1136 } else { 1137 result.AppendMessageWithFormat( 1138 "%u matching process%s found on \"%s\"", matches, 1139 matches > 1 ? "es were" : " was", 1140 platform_sp->GetName().GetCString()); 1141 if (match_desc) 1142 result.AppendMessageWithFormat(" whose name %s \"%s\"", 1143 match_desc, match_name); 1144 result.AppendMessageWithFormat("\n"); 1145 ProcessInstanceInfo::DumpTableHeader(ostrm, m_options.show_args, 1146 m_options.verbose); 1147 for (uint32_t i = 0; i < matches; ++i) { 1148 proc_infos.GetProcessInfoAtIndex(i).DumpAsTableRow( 1149 ostrm, platform_sp->GetUserIDResolver(), 1150 m_options.show_args, m_options.verbose); 1151 } 1152 } 1153 } 1154 } 1155 } else { 1156 result.AppendError("invalid args: process list takes only options\n"); 1157 result.SetStatus(eReturnStatusFailed); 1158 } 1159 } else { 1160 result.AppendError("no platform is selected\n"); 1161 result.SetStatus(eReturnStatusFailed); 1162 } 1163 return result.Succeeded(); 1164 } 1165 1166 class CommandOptions : public Options { 1167 public: 1168 CommandOptions() 1169 : Options(), match_info(), show_args(false), verbose(false) { 1170 } 1171 1172 ~CommandOptions() override = default; 1173 1174 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1175 ExecutionContext *execution_context) override { 1176 Status error; 1177 const int short_option = m_getopt_table[option_idx].val; 1178 bool success = false; 1179 1180 uint32_t id = LLDB_INVALID_PROCESS_ID; 1181 success = !option_arg.getAsInteger(0, id); 1182 switch (short_option) { 1183 case 'p': { 1184 match_info.GetProcessInfo().SetProcessID(id); 1185 if (!success) 1186 error.SetErrorStringWithFormat("invalid process ID string: '%s'", 1187 option_arg.str().c_str()); 1188 break; 1189 } 1190 case 'P': 1191 match_info.GetProcessInfo().SetParentProcessID(id); 1192 if (!success) 1193 error.SetErrorStringWithFormat( 1194 "invalid parent process ID string: '%s'", 1195 option_arg.str().c_str()); 1196 break; 1197 1198 case 'u': 1199 match_info.GetProcessInfo().SetUserID(success ? id : UINT32_MAX); 1200 if (!success) 1201 error.SetErrorStringWithFormat("invalid user ID string: '%s'", 1202 option_arg.str().c_str()); 1203 break; 1204 1205 case 'U': 1206 match_info.GetProcessInfo().SetEffectiveUserID(success ? id 1207 : UINT32_MAX); 1208 if (!success) 1209 error.SetErrorStringWithFormat( 1210 "invalid effective user ID string: '%s'", 1211 option_arg.str().c_str()); 1212 break; 1213 1214 case 'g': 1215 match_info.GetProcessInfo().SetGroupID(success ? id : UINT32_MAX); 1216 if (!success) 1217 error.SetErrorStringWithFormat("invalid group ID string: '%s'", 1218 option_arg.str().c_str()); 1219 break; 1220 1221 case 'G': 1222 match_info.GetProcessInfo().SetEffectiveGroupID(success ? id 1223 : UINT32_MAX); 1224 if (!success) 1225 error.SetErrorStringWithFormat( 1226 "invalid effective group ID string: '%s'", 1227 option_arg.str().c_str()); 1228 break; 1229 1230 case 'a': { 1231 TargetSP target_sp = 1232 execution_context ? execution_context->GetTargetSP() : TargetSP(); 1233 DebuggerSP debugger_sp = 1234 target_sp ? target_sp->GetDebugger().shared_from_this() 1235 : DebuggerSP(); 1236 PlatformSP platform_sp = 1237 debugger_sp ? debugger_sp->GetPlatformList().GetSelectedPlatform() 1238 : PlatformSP(); 1239 match_info.GetProcessInfo().GetArchitecture() = 1240 Platform::GetAugmentedArchSpec(platform_sp.get(), option_arg); 1241 } break; 1242 1243 case 'n': 1244 match_info.GetProcessInfo().GetExecutableFile().SetFile( 1245 option_arg, FileSpec::Style::native); 1246 match_info.SetNameMatchType(NameMatch::Equals); 1247 break; 1248 1249 case 'e': 1250 match_info.GetProcessInfo().GetExecutableFile().SetFile( 1251 option_arg, FileSpec::Style::native); 1252 match_info.SetNameMatchType(NameMatch::EndsWith); 1253 break; 1254 1255 case 's': 1256 match_info.GetProcessInfo().GetExecutableFile().SetFile( 1257 option_arg, FileSpec::Style::native); 1258 match_info.SetNameMatchType(NameMatch::StartsWith); 1259 break; 1260 1261 case 'c': 1262 match_info.GetProcessInfo().GetExecutableFile().SetFile( 1263 option_arg, FileSpec::Style::native); 1264 match_info.SetNameMatchType(NameMatch::Contains); 1265 break; 1266 1267 case 'r': 1268 match_info.GetProcessInfo().GetExecutableFile().SetFile( 1269 option_arg, FileSpec::Style::native); 1270 match_info.SetNameMatchType(NameMatch::RegularExpression); 1271 break; 1272 1273 case 'A': 1274 show_args = true; 1275 break; 1276 1277 case 'v': 1278 verbose = true; 1279 break; 1280 1281 default: 1282 error.SetErrorStringWithFormat("unrecognized option '%c'", 1283 short_option); 1284 break; 1285 } 1286 1287 return error; 1288 } 1289 1290 void OptionParsingStarting(ExecutionContext *execution_context) override { 1291 match_info.Clear(); 1292 show_args = false; 1293 verbose = false; 1294 } 1295 1296 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1297 return llvm::makeArrayRef(g_platform_process_list_options); 1298 } 1299 1300 // Instance variables to hold the values for command options. 1301 1302 ProcessInstanceInfoMatch match_info; 1303 bool show_args; 1304 bool verbose; 1305 }; 1306 1307 CommandOptions m_options; 1308 }; 1309 1310 // "platform process info" 1311 class CommandObjectPlatformProcessInfo : public CommandObjectParsed { 1312 public: 1313 CommandObjectPlatformProcessInfo(CommandInterpreter &interpreter) 1314 : CommandObjectParsed( 1315 interpreter, "platform process info", 1316 "Get detailed information for one or more process by process ID.", 1317 "platform process info <pid> [<pid> <pid> ...]", 0) { 1318 CommandArgumentEntry arg; 1319 CommandArgumentData pid_args; 1320 1321 // Define the first (and only) variant of this arg. 1322 pid_args.arg_type = eArgTypePid; 1323 pid_args.arg_repetition = eArgRepeatStar; 1324 1325 // There is only one variant this argument could be; put it into the 1326 // argument entry. 1327 arg.push_back(pid_args); 1328 1329 // Push the data for the first argument into the m_arguments vector. 1330 m_arguments.push_back(arg); 1331 } 1332 1333 ~CommandObjectPlatformProcessInfo() override = default; 1334 1335 protected: 1336 bool DoExecute(Args &args, CommandReturnObject &result) override { 1337 Target *target = GetDebugger().GetSelectedTarget().get(); 1338 PlatformSP platform_sp; 1339 if (target) { 1340 platform_sp = target->GetPlatform(); 1341 } 1342 if (!platform_sp) { 1343 platform_sp = GetDebugger().GetPlatformList().GetSelectedPlatform(); 1344 } 1345 1346 if (platform_sp) { 1347 const size_t argc = args.GetArgumentCount(); 1348 if (argc > 0) { 1349 Status error; 1350 1351 if (platform_sp->IsConnected()) { 1352 Stream &ostrm = result.GetOutputStream(); 1353 for (auto &entry : args.entries()) { 1354 lldb::pid_t pid; 1355 if (entry.ref.getAsInteger(0, pid)) { 1356 result.AppendErrorWithFormat("invalid process ID argument '%s'", 1357 entry.ref.str().c_str()); 1358 result.SetStatus(eReturnStatusFailed); 1359 break; 1360 } else { 1361 ProcessInstanceInfo proc_info; 1362 if (platform_sp->GetProcessInfo(pid, proc_info)) { 1363 ostrm.Printf("Process information for process %" PRIu64 ":\n", 1364 pid); 1365 proc_info.Dump(ostrm, platform_sp->GetUserIDResolver()); 1366 } else { 1367 ostrm.Printf("error: no process information is available for " 1368 "process %" PRIu64 "\n", 1369 pid); 1370 } 1371 ostrm.EOL(); 1372 } 1373 } 1374 } else { 1375 // Not connected... 1376 result.AppendErrorWithFormat( 1377 "not connected to '%s'", 1378 platform_sp->GetPluginName().GetCString()); 1379 result.SetStatus(eReturnStatusFailed); 1380 } 1381 } else { 1382 // No args 1383 result.AppendError("one or more process id(s) must be specified"); 1384 result.SetStatus(eReturnStatusFailed); 1385 } 1386 } else { 1387 result.AppendError("no platform is currently selected"); 1388 result.SetStatus(eReturnStatusFailed); 1389 } 1390 return result.Succeeded(); 1391 } 1392 }; 1393 1394 static constexpr OptionDefinition g_platform_process_attach_options[] = { 1395 #define LLDB_OPTIONS_platform_process_attach 1396 #include "CommandOptions.inc" 1397 }; 1398 1399 class CommandObjectPlatformProcessAttach : public CommandObjectParsed { 1400 public: 1401 class CommandOptions : public Options { 1402 public: 1403 CommandOptions() : Options() { 1404 // Keep default values of all options in one place: OptionParsingStarting 1405 // () 1406 OptionParsingStarting(nullptr); 1407 } 1408 1409 ~CommandOptions() override = default; 1410 1411 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1412 ExecutionContext *execution_context) override { 1413 Status error; 1414 char short_option = (char)m_getopt_table[option_idx].val; 1415 switch (short_option) { 1416 case 'p': { 1417 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 1418 if (option_arg.getAsInteger(0, pid)) { 1419 error.SetErrorStringWithFormat("invalid process ID '%s'", 1420 option_arg.str().c_str()); 1421 } else { 1422 attach_info.SetProcessID(pid); 1423 } 1424 } break; 1425 1426 case 'P': 1427 attach_info.SetProcessPluginName(option_arg); 1428 break; 1429 1430 case 'n': 1431 attach_info.GetExecutableFile().SetFile(option_arg, 1432 FileSpec::Style::native); 1433 break; 1434 1435 case 'w': 1436 attach_info.SetWaitForLaunch(true); 1437 break; 1438 1439 default: 1440 error.SetErrorStringWithFormat("invalid short option character '%c'", 1441 short_option); 1442 break; 1443 } 1444 return error; 1445 } 1446 1447 void OptionParsingStarting(ExecutionContext *execution_context) override { 1448 attach_info.Clear(); 1449 } 1450 1451 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1452 return llvm::makeArrayRef(g_platform_process_attach_options); 1453 } 1454 1455 bool HandleOptionArgumentCompletion( 1456 CompletionRequest &request, OptionElementVector &opt_element_vector, 1457 int opt_element_index, CommandInterpreter &interpreter) override { 1458 int opt_arg_pos = opt_element_vector[opt_element_index].opt_arg_pos; 1459 int opt_defs_index = opt_element_vector[opt_element_index].opt_defs_index; 1460 1461 // We are only completing the name option for now... 1462 1463 if (GetDefinitions()[opt_defs_index].short_option == 'n') { 1464 // Are we in the name? 1465 1466 // Look to see if there is a -P argument provided, and if so use that 1467 // plugin, otherwise use the default plugin. 1468 1469 const char *partial_name = nullptr; 1470 partial_name = request.GetParsedLine().GetArgumentAtIndex(opt_arg_pos); 1471 1472 PlatformSP platform_sp(interpreter.GetPlatform(true)); 1473 if (platform_sp) { 1474 ProcessInstanceInfoList process_infos; 1475 ProcessInstanceInfoMatch match_info; 1476 if (partial_name) { 1477 match_info.GetProcessInfo().GetExecutableFile().SetFile( 1478 partial_name, FileSpec::Style::native); 1479 match_info.SetNameMatchType(NameMatch::StartsWith); 1480 } 1481 platform_sp->FindProcesses(match_info, process_infos); 1482 const uint32_t num_matches = process_infos.GetSize(); 1483 if (num_matches > 0) { 1484 for (uint32_t i = 0; i < num_matches; ++i) { 1485 request.AddCompletion(llvm::StringRef( 1486 process_infos.GetProcessNameAtIndex(i), 1487 process_infos.GetProcessNameLengthAtIndex(i))); 1488 } 1489 } 1490 } 1491 } 1492 1493 return false; 1494 } 1495 1496 // Options table: Required for subclasses of Options. 1497 1498 static OptionDefinition g_option_table[]; 1499 1500 // Instance variables to hold the values for command options. 1501 1502 ProcessAttachInfo attach_info; 1503 }; 1504 1505 CommandObjectPlatformProcessAttach(CommandInterpreter &interpreter) 1506 : CommandObjectParsed(interpreter, "platform process attach", 1507 "Attach to a process.", 1508 "platform process attach <cmd-options>"), 1509 m_options() {} 1510 1511 ~CommandObjectPlatformProcessAttach() override = default; 1512 1513 bool DoExecute(Args &command, CommandReturnObject &result) override { 1514 PlatformSP platform_sp( 1515 GetDebugger().GetPlatformList().GetSelectedPlatform()); 1516 if (platform_sp) { 1517 Status err; 1518 ProcessSP remote_process_sp = platform_sp->Attach( 1519 m_options.attach_info, GetDebugger(), nullptr, err); 1520 if (err.Fail()) { 1521 result.AppendError(err.AsCString()); 1522 result.SetStatus(eReturnStatusFailed); 1523 } else if (!remote_process_sp) { 1524 result.AppendError("could not attach: unknown reason"); 1525 result.SetStatus(eReturnStatusFailed); 1526 } else 1527 result.SetStatus(eReturnStatusSuccessFinishResult); 1528 } else { 1529 result.AppendError("no platform is currently selected"); 1530 result.SetStatus(eReturnStatusFailed); 1531 } 1532 return result.Succeeded(); 1533 } 1534 1535 Options *GetOptions() override { return &m_options; } 1536 1537 protected: 1538 CommandOptions m_options; 1539 }; 1540 1541 class CommandObjectPlatformProcess : public CommandObjectMultiword { 1542 public: 1543 // Constructors and Destructors 1544 CommandObjectPlatformProcess(CommandInterpreter &interpreter) 1545 : CommandObjectMultiword(interpreter, "platform process", 1546 "Commands to query, launch and attach to " 1547 "processes on the current platform.", 1548 "platform process [attach|launch|list] ...") { 1549 LoadSubCommand( 1550 "attach", 1551 CommandObjectSP(new CommandObjectPlatformProcessAttach(interpreter))); 1552 LoadSubCommand( 1553 "launch", 1554 CommandObjectSP(new CommandObjectPlatformProcessLaunch(interpreter))); 1555 LoadSubCommand("info", CommandObjectSP(new CommandObjectPlatformProcessInfo( 1556 interpreter))); 1557 LoadSubCommand("list", CommandObjectSP(new CommandObjectPlatformProcessList( 1558 interpreter))); 1559 } 1560 1561 ~CommandObjectPlatformProcess() override = default; 1562 1563 private: 1564 // For CommandObjectPlatform only 1565 DISALLOW_COPY_AND_ASSIGN(CommandObjectPlatformProcess); 1566 }; 1567 1568 // "platform shell" 1569 static constexpr OptionDefinition g_platform_shell_options[] = { 1570 #define LLDB_OPTIONS_platform_shell 1571 #include "CommandOptions.inc" 1572 }; 1573 1574 class CommandObjectPlatformShell : public CommandObjectRaw { 1575 public: 1576 class CommandOptions : public Options { 1577 public: 1578 CommandOptions() : Options() {} 1579 1580 ~CommandOptions() override = default; 1581 1582 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 1583 return llvm::makeArrayRef(g_platform_shell_options); 1584 } 1585 1586 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1587 ExecutionContext *execution_context) override { 1588 Status error; 1589 1590 const char short_option = (char)GetDefinitions()[option_idx].short_option; 1591 1592 switch (short_option) { 1593 case 't': 1594 uint32_t timeout_sec; 1595 if (option_arg.getAsInteger(10, timeout_sec)) 1596 error.SetErrorStringWithFormat( 1597 "could not convert \"%s\" to a numeric value.", 1598 option_arg.str().c_str()); 1599 else 1600 timeout = std::chrono::seconds(timeout_sec); 1601 break; 1602 default: 1603 error.SetErrorStringWithFormat("invalid short option character '%c'", 1604 short_option); 1605 break; 1606 } 1607 1608 return error; 1609 } 1610 1611 void OptionParsingStarting(ExecutionContext *execution_context) override {} 1612 1613 Timeout<std::micro> timeout = std::chrono::seconds(10); 1614 }; 1615 1616 CommandObjectPlatformShell(CommandInterpreter &interpreter) 1617 : CommandObjectRaw(interpreter, "platform shell", 1618 "Run a shell command on the current platform.", 1619 "platform shell <shell-command>", 0), 1620 m_options() {} 1621 1622 ~CommandObjectPlatformShell() override = default; 1623 1624 Options *GetOptions() override { return &m_options; } 1625 1626 bool DoExecute(llvm::StringRef raw_command_line, 1627 CommandReturnObject &result) override { 1628 ExecutionContext exe_ctx = GetCommandInterpreter().GetExecutionContext(); 1629 m_options.NotifyOptionParsingStarting(&exe_ctx); 1630 1631 1632 // Print out an usage syntax on an empty command line. 1633 if (raw_command_line.empty()) { 1634 result.GetOutputStream().Printf("%s\n", this->GetSyntax().str().c_str()); 1635 return true; 1636 } 1637 1638 OptionsWithRaw args(raw_command_line); 1639 const char *expr = args.GetRawPart().c_str(); 1640 1641 if (args.HasArgs()) 1642 if (!ParseOptions(args.GetArgs(), result)) 1643 return false; 1644 1645 PlatformSP platform_sp( 1646 GetDebugger().GetPlatformList().GetSelectedPlatform()); 1647 Status error; 1648 if (platform_sp) { 1649 FileSpec working_dir{}; 1650 std::string output; 1651 int status = -1; 1652 int signo = -1; 1653 error = (platform_sp->RunShellCommand(expr, working_dir, &status, &signo, 1654 &output, m_options.timeout)); 1655 if (!output.empty()) 1656 result.GetOutputStream().PutCString(output); 1657 if (status > 0) { 1658 if (signo > 0) { 1659 const char *signo_cstr = Host::GetSignalAsCString(signo); 1660 if (signo_cstr) 1661 result.GetOutputStream().Printf( 1662 "error: command returned with status %i and signal %s\n", 1663 status, signo_cstr); 1664 else 1665 result.GetOutputStream().Printf( 1666 "error: command returned with status %i and signal %i\n", 1667 status, signo); 1668 } else 1669 result.GetOutputStream().Printf( 1670 "error: command returned with status %i\n", status); 1671 } 1672 } else { 1673 result.GetOutputStream().Printf( 1674 "error: cannot run remote shell commands without a platform\n"); 1675 error.SetErrorString( 1676 "error: cannot run remote shell commands without a platform"); 1677 } 1678 1679 if (error.Fail()) { 1680 result.AppendError(error.AsCString()); 1681 result.SetStatus(eReturnStatusFailed); 1682 } else { 1683 result.SetStatus(eReturnStatusSuccessFinishResult); 1684 } 1685 return true; 1686 } 1687 1688 CommandOptions m_options; 1689 }; 1690 1691 // "platform install" - install a target to a remote end 1692 class CommandObjectPlatformInstall : public CommandObjectParsed { 1693 public: 1694 CommandObjectPlatformInstall(CommandInterpreter &interpreter) 1695 : CommandObjectParsed( 1696 interpreter, "platform target-install", 1697 "Install a target (bundle or executable file) to the remote end.", 1698 "platform target-install <local-thing> <remote-sandbox>", 0) {} 1699 1700 ~CommandObjectPlatformInstall() override = default; 1701 1702 bool DoExecute(Args &args, CommandReturnObject &result) override { 1703 if (args.GetArgumentCount() != 2) { 1704 result.AppendError("platform target-install takes two arguments"); 1705 result.SetStatus(eReturnStatusFailed); 1706 return false; 1707 } 1708 // TODO: move the bulk of this code over to the platform itself 1709 FileSpec src(args.GetArgumentAtIndex(0)); 1710 FileSystem::Instance().Resolve(src); 1711 FileSpec dst(args.GetArgumentAtIndex(1)); 1712 if (!FileSystem::Instance().Exists(src)) { 1713 result.AppendError("source location does not exist or is not accessible"); 1714 result.SetStatus(eReturnStatusFailed); 1715 return false; 1716 } 1717 PlatformSP platform_sp( 1718 GetDebugger().GetPlatformList().GetSelectedPlatform()); 1719 if (!platform_sp) { 1720 result.AppendError("no platform currently selected"); 1721 result.SetStatus(eReturnStatusFailed); 1722 return false; 1723 } 1724 1725 Status error = platform_sp->Install(src, dst); 1726 if (error.Success()) { 1727 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1728 } else { 1729 result.AppendErrorWithFormat("install failed: %s", error.AsCString()); 1730 result.SetStatus(eReturnStatusFailed); 1731 } 1732 return result.Succeeded(); 1733 } 1734 }; 1735 1736 CommandObjectPlatform::CommandObjectPlatform(CommandInterpreter &interpreter) 1737 : CommandObjectMultiword( 1738 interpreter, "platform", "Commands to manage and create platforms.", 1739 "platform [connect|disconnect|info|list|status|select] ...") { 1740 LoadSubCommand("select", 1741 CommandObjectSP(new CommandObjectPlatformSelect(interpreter))); 1742 LoadSubCommand("list", 1743 CommandObjectSP(new CommandObjectPlatformList(interpreter))); 1744 LoadSubCommand("status", 1745 CommandObjectSP(new CommandObjectPlatformStatus(interpreter))); 1746 LoadSubCommand("connect", CommandObjectSP( 1747 new CommandObjectPlatformConnect(interpreter))); 1748 LoadSubCommand( 1749 "disconnect", 1750 CommandObjectSP(new CommandObjectPlatformDisconnect(interpreter))); 1751 LoadSubCommand("settings", CommandObjectSP(new CommandObjectPlatformSettings( 1752 interpreter))); 1753 LoadSubCommand("mkdir", 1754 CommandObjectSP(new CommandObjectPlatformMkDir(interpreter))); 1755 LoadSubCommand("file", 1756 CommandObjectSP(new CommandObjectPlatformFile(interpreter))); 1757 LoadSubCommand("get-file", CommandObjectSP(new CommandObjectPlatformGetFile( 1758 interpreter))); 1759 LoadSubCommand("get-size", CommandObjectSP(new CommandObjectPlatformGetSize( 1760 interpreter))); 1761 LoadSubCommand("put-file", CommandObjectSP(new CommandObjectPlatformPutFile( 1762 interpreter))); 1763 LoadSubCommand("process", CommandObjectSP( 1764 new CommandObjectPlatformProcess(interpreter))); 1765 LoadSubCommand("shell", 1766 CommandObjectSP(new CommandObjectPlatformShell(interpreter))); 1767 LoadSubCommand( 1768 "target-install", 1769 CommandObjectSP(new CommandObjectPlatformInstall(interpreter))); 1770 } 1771 1772 CommandObjectPlatform::~CommandObjectPlatform() = default; 1773