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