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