1 //===-- CommandObjectTarget.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 "CommandObjectTarget.h" 10 11 #include "lldb/Core/Debugger.h" 12 #include "lldb/Core/IOHandler.h" 13 #include "lldb/Core/Module.h" 14 #include "lldb/Core/ModuleSpec.h" 15 #include "lldb/Core/Section.h" 16 #include "lldb/Core/ValueObjectVariable.h" 17 #include "lldb/DataFormatters/ValueObjectPrinter.h" 18 #include "lldb/Host/OptionParser.h" 19 #include "lldb/Interpreter/CommandInterpreter.h" 20 #include "lldb/Interpreter/CommandReturnObject.h" 21 #include "lldb/Interpreter/OptionArgParser.h" 22 #include "lldb/Interpreter/OptionGroupArchitecture.h" 23 #include "lldb/Interpreter/OptionGroupBoolean.h" 24 #include "lldb/Interpreter/OptionGroupFile.h" 25 #include "lldb/Interpreter/OptionGroupFormat.h" 26 #include "lldb/Interpreter/OptionGroupPlatform.h" 27 #include "lldb/Interpreter/OptionGroupString.h" 28 #include "lldb/Interpreter/OptionGroupUInt64.h" 29 #include "lldb/Interpreter/OptionGroupUUID.h" 30 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h" 31 #include "lldb/Interpreter/OptionGroupVariable.h" 32 #include "lldb/Interpreter/Options.h" 33 #include "lldb/Symbol/CompileUnit.h" 34 #include "lldb/Symbol/FuncUnwinders.h" 35 #include "lldb/Symbol/LineTable.h" 36 #include "lldb/Symbol/LocateSymbolFile.h" 37 #include "lldb/Symbol/ObjectFile.h" 38 #include "lldb/Symbol/SymbolFile.h" 39 #include "lldb/Symbol/UnwindPlan.h" 40 #include "lldb/Symbol/VariableList.h" 41 #include "lldb/Target/ABI.h" 42 #include "lldb/Target/Process.h" 43 #include "lldb/Target/RegisterContext.h" 44 #include "lldb/Target/SectionLoadList.h" 45 #include "lldb/Target/StackFrame.h" 46 #include "lldb/Target/Thread.h" 47 #include "lldb/Target/ThreadSpec.h" 48 #include "lldb/Utility/Args.h" 49 #include "lldb/Utility/State.h" 50 #include "lldb/Utility/Timer.h" 51 52 #include "llvm/Support/FileSystem.h" 53 #include "llvm/Support/FormatAdapters.h" 54 55 56 using namespace lldb; 57 using namespace lldb_private; 58 59 static void DumpTargetInfo(uint32_t target_idx, Target *target, 60 const char *prefix_cstr, 61 bool show_stopped_process_status, Stream &strm) { 62 const ArchSpec &target_arch = target->GetArchitecture(); 63 64 Module *exe_module = target->GetExecutableModulePointer(); 65 char exe_path[PATH_MAX]; 66 bool exe_valid = false; 67 if (exe_module) 68 exe_valid = exe_module->GetFileSpec().GetPath(exe_path, sizeof(exe_path)); 69 70 if (!exe_valid) 71 ::strcpy(exe_path, "<none>"); 72 73 strm.Printf("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, 74 exe_path); 75 76 uint32_t properties = 0; 77 if (target_arch.IsValid()) { 78 strm.Printf("%sarch=", properties++ > 0 ? ", " : " ( "); 79 target_arch.DumpTriple(strm.AsRawOstream()); 80 properties++; 81 } 82 PlatformSP platform_sp(target->GetPlatform()); 83 if (platform_sp) 84 strm.Printf("%splatform=%s", properties++ > 0 ? ", " : " ( ", 85 platform_sp->GetName().GetCString()); 86 87 ProcessSP process_sp(target->GetProcessSP()); 88 bool show_process_status = false; 89 if (process_sp) { 90 lldb::pid_t pid = process_sp->GetID(); 91 StateType state = process_sp->GetState(); 92 if (show_stopped_process_status) 93 show_process_status = StateIsStoppedState(state, true); 94 const char *state_cstr = StateAsCString(state); 95 if (pid != LLDB_INVALID_PROCESS_ID) 96 strm.Printf("%spid=%" PRIu64, properties++ > 0 ? ", " : " ( ", pid); 97 strm.Printf("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr); 98 } 99 if (properties > 0) 100 strm.PutCString(" )\n"); 101 else 102 strm.EOL(); 103 if (show_process_status) { 104 const bool only_threads_with_stop_reason = true; 105 const uint32_t start_frame = 0; 106 const uint32_t num_frames = 1; 107 const uint32_t num_frames_with_source = 1; 108 const bool stop_format = false; 109 process_sp->GetStatus(strm); 110 process_sp->GetThreadStatus(strm, only_threads_with_stop_reason, 111 start_frame, num_frames, num_frames_with_source, 112 stop_format); 113 } 114 } 115 116 static uint32_t DumpTargetList(TargetList &target_list, 117 bool show_stopped_process_status, Stream &strm) { 118 const uint32_t num_targets = target_list.GetNumTargets(); 119 if (num_targets) { 120 TargetSP selected_target_sp(target_list.GetSelectedTarget()); 121 strm.PutCString("Current targets:\n"); 122 for (uint32_t i = 0; i < num_targets; ++i) { 123 TargetSP target_sp(target_list.GetTargetAtIndex(i)); 124 if (target_sp) { 125 bool is_selected = target_sp.get() == selected_target_sp.get(); 126 DumpTargetInfo(i, target_sp.get(), is_selected ? "* " : " ", 127 show_stopped_process_status, strm); 128 } 129 } 130 } 131 return num_targets; 132 } 133 134 // Note that the negation in the argument name causes a slightly confusing 135 // mapping of the enum values. 136 static constexpr OptionEnumValueElement g_dependents_enumaration[] = { 137 { 138 eLoadDependentsDefault, 139 "default", 140 "Only load dependents when the target is an executable.", 141 }, 142 { 143 eLoadDependentsNo, 144 "true", 145 "Don't load dependents, even if the target is an executable.", 146 }, 147 { 148 eLoadDependentsYes, 149 "false", 150 "Load dependents, even if the target is not an executable.", 151 }, 152 }; 153 154 #define LLDB_OPTIONS_target_dependents 155 #include "CommandOptions.inc" 156 157 class OptionGroupDependents : public OptionGroup { 158 public: 159 OptionGroupDependents() {} 160 161 ~OptionGroupDependents() override {} 162 163 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 164 return llvm::makeArrayRef(g_target_dependents_options); 165 } 166 167 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_value, 168 ExecutionContext *execution_context) override { 169 Status error; 170 171 // For compatibility no value means don't load dependents. 172 if (option_value.empty()) { 173 m_load_dependent_files = eLoadDependentsNo; 174 return error; 175 } 176 177 const char short_option = 178 g_target_dependents_options[option_idx].short_option; 179 if (short_option == 'd') { 180 LoadDependentFiles tmp_load_dependents; 181 tmp_load_dependents = (LoadDependentFiles)OptionArgParser::ToOptionEnum( 182 option_value, g_target_dependents_options[option_idx].enum_values, 0, 183 error); 184 if (error.Success()) 185 m_load_dependent_files = tmp_load_dependents; 186 } else { 187 error.SetErrorStringWithFormat("unrecognized short option '%c'", 188 short_option); 189 } 190 191 return error; 192 } 193 194 Status SetOptionValue(uint32_t, const char *, ExecutionContext *) = delete; 195 196 void OptionParsingStarting(ExecutionContext *execution_context) override { 197 m_load_dependent_files = eLoadDependentsDefault; 198 } 199 200 LoadDependentFiles m_load_dependent_files; 201 202 private: 203 OptionGroupDependents(const OptionGroupDependents &) = delete; 204 const OptionGroupDependents & 205 operator=(const OptionGroupDependents &) = delete; 206 }; 207 208 #pragma mark CommandObjectTargetCreate 209 210 class CommandObjectTargetCreate : public CommandObjectParsed { 211 public: 212 CommandObjectTargetCreate(CommandInterpreter &interpreter) 213 : CommandObjectParsed( 214 interpreter, "target create", 215 "Create a target using the argument as the main executable.", 216 nullptr), 217 m_option_group(), m_arch_option(), 218 m_platform_options(true), // Include the --platform option. 219 m_core_file(LLDB_OPT_SET_1, false, "core", 'c', 0, eArgTypeFilename, 220 "Fullpath to a core file to use for this target."), 221 m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', 0, 222 eArgTypeFilename, 223 "Fullpath to a stand alone debug " 224 "symbols file for when debug symbols " 225 "are not in the executable."), 226 m_remote_file( 227 LLDB_OPT_SET_1, false, "remote-file", 'r', 0, eArgTypeFilename, 228 "Fullpath to the file on the remote host if debugging remotely."), 229 m_add_dependents() { 230 CommandArgumentEntry arg; 231 CommandArgumentData file_arg; 232 233 // Define the first (and only) variant of this arg. 234 file_arg.arg_type = eArgTypeFilename; 235 file_arg.arg_repetition = eArgRepeatPlain; 236 237 // There is only one variant this argument could be; put it into the 238 // argument entry. 239 arg.push_back(file_arg); 240 241 // Push the data for the first argument into the m_arguments vector. 242 m_arguments.push_back(arg); 243 244 m_option_group.Append(&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 245 m_option_group.Append(&m_platform_options, LLDB_OPT_SET_ALL, 1); 246 m_option_group.Append(&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 247 m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 248 m_option_group.Append(&m_remote_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 249 m_option_group.Append(&m_add_dependents, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 250 m_option_group.Finalize(); 251 } 252 253 ~CommandObjectTargetCreate() override = default; 254 255 Options *GetOptions() override { return &m_option_group; } 256 257 void 258 HandleArgumentCompletion(CompletionRequest &request, 259 OptionElementVector &opt_element_vector) override { 260 CommandCompletions::InvokeCommonCompletionCallbacks( 261 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, 262 request, nullptr); 263 } 264 265 protected: 266 bool DoExecute(Args &command, CommandReturnObject &result) override { 267 const size_t argc = command.GetArgumentCount(); 268 FileSpec core_file(m_core_file.GetOptionValue().GetCurrentValue()); 269 FileSpec remote_file(m_remote_file.GetOptionValue().GetCurrentValue()); 270 271 if (core_file) { 272 auto file = FileSystem::Instance().Open( 273 core_file, lldb_private::File::eOpenOptionRead); 274 275 if (!file) { 276 result.AppendErrorWithFormatv("Cannot open '{0}': {1}.", 277 core_file.GetPath(), 278 llvm::toString(file.takeError())); 279 result.SetStatus(eReturnStatusFailed); 280 return false; 281 } 282 } 283 284 if (argc == 1 || core_file || remote_file) { 285 FileSpec symfile(m_symbol_file.GetOptionValue().GetCurrentValue()); 286 if (symfile) { 287 auto file = FileSystem::Instance().Open( 288 symfile, lldb_private::File::eOpenOptionRead); 289 290 if (!file) { 291 result.AppendErrorWithFormatv("Cannot open '{0}': {1}.", 292 symfile.GetPath(), 293 llvm::toString(file.takeError())); 294 result.SetStatus(eReturnStatusFailed); 295 return false; 296 } 297 } 298 299 const char *file_path = command.GetArgumentAtIndex(0); 300 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 301 Timer scoped_timer(func_cat, "(lldb) target create '%s'", file_path); 302 FileSpec file_spec; 303 304 if (file_path) { 305 file_spec.SetFile(file_path, FileSpec::Style::native); 306 FileSystem::Instance().Resolve(file_spec); 307 } 308 309 bool must_set_platform_path = false; 310 311 Debugger &debugger = GetDebugger(); 312 313 TargetSP target_sp; 314 llvm::StringRef arch_cstr = m_arch_option.GetArchitectureName(); 315 Status error(debugger.GetTargetList().CreateTarget( 316 debugger, file_path, arch_cstr, 317 m_add_dependents.m_load_dependent_files, &m_platform_options, 318 target_sp)); 319 320 if (target_sp) { 321 // Only get the platform after we create the target because we might 322 // have switched platforms depending on what the arguments were to 323 // CreateTarget() we can't rely on the selected platform. 324 325 PlatformSP platform_sp = target_sp->GetPlatform(); 326 327 if (remote_file) { 328 if (platform_sp) { 329 // I have a remote file.. two possible cases 330 if (file_spec && FileSystem::Instance().Exists(file_spec)) { 331 // if the remote file does not exist, push it there 332 if (!platform_sp->GetFileExists(remote_file)) { 333 Status err = platform_sp->PutFile(file_spec, remote_file); 334 if (err.Fail()) { 335 result.AppendError(err.AsCString()); 336 result.SetStatus(eReturnStatusFailed); 337 return false; 338 } 339 } 340 } else { 341 // there is no local file and we need one 342 // in order to make the remote ---> local transfer we need a 343 // platform 344 // TODO: if the user has passed in a --platform argument, use it 345 // to fetch the right platform 346 if (!platform_sp) { 347 result.AppendError( 348 "unable to perform remote debugging without a platform"); 349 result.SetStatus(eReturnStatusFailed); 350 return false; 351 } 352 if (file_path) { 353 // copy the remote file to the local file 354 Status err = platform_sp->GetFile(remote_file, file_spec); 355 if (err.Fail()) { 356 result.AppendError(err.AsCString()); 357 result.SetStatus(eReturnStatusFailed); 358 return false; 359 } 360 } else { 361 // make up a local file 362 result.AppendError("remote --> local transfer without local " 363 "path is not implemented yet"); 364 result.SetStatus(eReturnStatusFailed); 365 return false; 366 } 367 } 368 } else { 369 result.AppendError("no platform found for target"); 370 result.SetStatus(eReturnStatusFailed); 371 return false; 372 } 373 } 374 375 if (symfile || remote_file) { 376 ModuleSP module_sp(target_sp->GetExecutableModule()); 377 if (module_sp) { 378 if (symfile) 379 module_sp->SetSymbolFileFileSpec(symfile); 380 if (remote_file) { 381 std::string remote_path = remote_file.GetPath(); 382 target_sp->SetArg0(remote_path.c_str()); 383 module_sp->SetPlatformFileSpec(remote_file); 384 } 385 } 386 } 387 388 debugger.GetTargetList().SetSelectedTarget(target_sp.get()); 389 if (must_set_platform_path) { 390 ModuleSpec main_module_spec(file_spec); 391 ModuleSP module_sp = 392 target_sp->GetOrCreateModule(main_module_spec, true /* notify */); 393 if (module_sp) 394 module_sp->SetPlatformFileSpec(remote_file); 395 } 396 397 if (core_file) { 398 FileSpec core_file_dir; 399 core_file_dir.GetDirectory() = core_file.GetDirectory(); 400 target_sp->AppendExecutableSearchPaths(core_file_dir); 401 402 ProcessSP process_sp(target_sp->CreateProcess( 403 GetDebugger().GetListener(), llvm::StringRef(), &core_file)); 404 405 if (process_sp) { 406 // Seems weird that we Launch a core file, but that is what we 407 // do! 408 error = process_sp->LoadCore(); 409 410 if (error.Fail()) { 411 result.AppendError( 412 error.AsCString("can't find plug-in for core file")); 413 result.SetStatus(eReturnStatusFailed); 414 return false; 415 } else { 416 result.AppendMessageWithFormatv("Core file '{0}' ({1}) was loaded.\n", core_file.GetPath(), 417 target_sp->GetArchitecture().GetArchitectureName()); 418 result.SetStatus(eReturnStatusSuccessFinishNoResult); 419 } 420 } else { 421 result.AppendErrorWithFormatv( 422 "Unable to find process plug-in for core file '{0}'\n", 423 core_file.GetPath()); 424 result.SetStatus(eReturnStatusFailed); 425 } 426 } else { 427 result.AppendMessageWithFormat( 428 "Current executable set to '%s' (%s).\n", 429 file_spec.GetPath().c_str(), 430 target_sp->GetArchitecture().GetArchitectureName()); 431 result.SetStatus(eReturnStatusSuccessFinishNoResult); 432 } 433 } else { 434 result.AppendError(error.AsCString()); 435 result.SetStatus(eReturnStatusFailed); 436 } 437 } else { 438 result.AppendErrorWithFormat("'%s' takes exactly one executable path " 439 "argument, or use the --core option.\n", 440 m_cmd_name.c_str()); 441 result.SetStatus(eReturnStatusFailed); 442 } 443 return result.Succeeded(); 444 } 445 446 private: 447 OptionGroupOptions m_option_group; 448 OptionGroupArchitecture m_arch_option; 449 OptionGroupPlatform m_platform_options; 450 OptionGroupFile m_core_file; 451 OptionGroupFile m_symbol_file; 452 OptionGroupFile m_remote_file; 453 OptionGroupDependents m_add_dependents; 454 }; 455 456 #pragma mark CommandObjectTargetList 457 458 class CommandObjectTargetList : public CommandObjectParsed { 459 public: 460 CommandObjectTargetList(CommandInterpreter &interpreter) 461 : CommandObjectParsed( 462 interpreter, "target list", 463 "List all current targets in the current debug session.", nullptr) { 464 } 465 466 ~CommandObjectTargetList() override = default; 467 468 protected: 469 bool DoExecute(Args &args, CommandReturnObject &result) override { 470 if (args.GetArgumentCount() == 0) { 471 Stream &strm = result.GetOutputStream(); 472 473 bool show_stopped_process_status = false; 474 if (DumpTargetList(GetDebugger().GetTargetList(), 475 show_stopped_process_status, strm) == 0) { 476 strm.PutCString("No targets.\n"); 477 } 478 result.SetStatus(eReturnStatusSuccessFinishResult); 479 } else { 480 result.AppendError("the 'target list' command takes no arguments\n"); 481 result.SetStatus(eReturnStatusFailed); 482 } 483 return result.Succeeded(); 484 } 485 }; 486 487 #pragma mark CommandObjectTargetSelect 488 489 class CommandObjectTargetSelect : public CommandObjectParsed { 490 public: 491 CommandObjectTargetSelect(CommandInterpreter &interpreter) 492 : CommandObjectParsed( 493 interpreter, "target select", 494 "Select a target as the current target by target index.", nullptr) { 495 } 496 497 ~CommandObjectTargetSelect() override = default; 498 499 protected: 500 bool DoExecute(Args &args, CommandReturnObject &result) override { 501 if (args.GetArgumentCount() == 1) { 502 const char *target_idx_arg = args.GetArgumentAtIndex(0); 503 uint32_t target_idx; 504 if (llvm::to_integer(target_idx_arg, target_idx)) { 505 TargetList &target_list = GetDebugger().GetTargetList(); 506 const uint32_t num_targets = target_list.GetNumTargets(); 507 if (target_idx < num_targets) { 508 TargetSP target_sp(target_list.GetTargetAtIndex(target_idx)); 509 if (target_sp) { 510 Stream &strm = result.GetOutputStream(); 511 target_list.SetSelectedTarget(target_sp.get()); 512 bool show_stopped_process_status = false; 513 DumpTargetList(target_list, show_stopped_process_status, strm); 514 result.SetStatus(eReturnStatusSuccessFinishResult); 515 } else { 516 result.AppendErrorWithFormat("target #%u is NULL in target list\n", 517 target_idx); 518 result.SetStatus(eReturnStatusFailed); 519 } 520 } else { 521 if (num_targets > 0) { 522 result.AppendErrorWithFormat( 523 "index %u is out of range, valid target indexes are 0 - %u\n", 524 target_idx, num_targets - 1); 525 } else { 526 result.AppendErrorWithFormat( 527 "index %u is out of range since there are no active targets\n", 528 target_idx); 529 } 530 result.SetStatus(eReturnStatusFailed); 531 } 532 } else { 533 result.AppendErrorWithFormat("invalid index string value '%s'\n", 534 target_idx_arg); 535 result.SetStatus(eReturnStatusFailed); 536 } 537 } else { 538 result.AppendError( 539 "'target select' takes a single argument: a target index\n"); 540 result.SetStatus(eReturnStatusFailed); 541 } 542 return result.Succeeded(); 543 } 544 }; 545 546 #pragma mark CommandObjectTargetDelete 547 548 class CommandObjectTargetDelete : public CommandObjectParsed { 549 public: 550 CommandObjectTargetDelete(CommandInterpreter &interpreter) 551 : CommandObjectParsed(interpreter, "target delete", 552 "Delete one or more targets by target index.", 553 nullptr), 554 m_option_group(), m_all_option(LLDB_OPT_SET_1, false, "all", 'a', 555 "Delete all targets.", false, true), 556 m_cleanup_option( 557 LLDB_OPT_SET_1, false, "clean", 'c', 558 "Perform extra cleanup to minimize memory consumption after " 559 "deleting the target. " 560 "By default, LLDB will keep in memory any modules previously " 561 "loaded by the target as well " 562 "as all of its debug info. Specifying --clean will unload all of " 563 "these shared modules and " 564 "cause them to be reparsed again the next time the target is run", 565 false, true) { 566 m_option_group.Append(&m_all_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 567 m_option_group.Append(&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 568 m_option_group.Finalize(); 569 } 570 571 ~CommandObjectTargetDelete() override = default; 572 573 Options *GetOptions() override { return &m_option_group; } 574 575 protected: 576 bool DoExecute(Args &args, CommandReturnObject &result) override { 577 const size_t argc = args.GetArgumentCount(); 578 std::vector<TargetSP> delete_target_list; 579 TargetList &target_list = GetDebugger().GetTargetList(); 580 TargetSP target_sp; 581 582 if (m_all_option.GetOptionValue()) { 583 for (int i = 0; i < target_list.GetNumTargets(); ++i) 584 delete_target_list.push_back(target_list.GetTargetAtIndex(i)); 585 } else if (argc > 0) { 586 const uint32_t num_targets = target_list.GetNumTargets(); 587 // Bail out if don't have any targets. 588 if (num_targets == 0) { 589 result.AppendError("no targets to delete"); 590 result.SetStatus(eReturnStatusFailed); 591 return false; 592 } 593 594 for (auto &entry : args.entries()) { 595 uint32_t target_idx; 596 if (entry.ref().getAsInteger(0, target_idx)) { 597 result.AppendErrorWithFormat("invalid target index '%s'\n", 598 entry.c_str()); 599 result.SetStatus(eReturnStatusFailed); 600 return false; 601 } 602 if (target_idx < num_targets) { 603 target_sp = target_list.GetTargetAtIndex(target_idx); 604 if (target_sp) { 605 delete_target_list.push_back(target_sp); 606 continue; 607 } 608 } 609 if (num_targets > 1) 610 result.AppendErrorWithFormat("target index %u is out of range, valid " 611 "target indexes are 0 - %u\n", 612 target_idx, num_targets - 1); 613 else 614 result.AppendErrorWithFormat( 615 "target index %u is out of range, the only valid index is 0\n", 616 target_idx); 617 618 result.SetStatus(eReturnStatusFailed); 619 return false; 620 } 621 } else { 622 target_sp = target_list.GetSelectedTarget(); 623 if (!target_sp) { 624 result.AppendErrorWithFormat("no target is currently selected\n"); 625 result.SetStatus(eReturnStatusFailed); 626 return false; 627 } 628 delete_target_list.push_back(target_sp); 629 } 630 631 const size_t num_targets_to_delete = delete_target_list.size(); 632 for (size_t idx = 0; idx < num_targets_to_delete; ++idx) { 633 target_sp = delete_target_list[idx]; 634 target_list.DeleteTarget(target_sp); 635 target_sp->Destroy(); 636 } 637 // If "--clean" was specified, prune any orphaned shared modules from the 638 // global shared module list 639 if (m_cleanup_option.GetOptionValue()) { 640 const bool mandatory = true; 641 ModuleList::RemoveOrphanSharedModules(mandatory); 642 } 643 result.GetOutputStream().Printf("%u targets deleted.\n", 644 (uint32_t)num_targets_to_delete); 645 result.SetStatus(eReturnStatusSuccessFinishResult); 646 647 return true; 648 } 649 650 OptionGroupOptions m_option_group; 651 OptionGroupBoolean m_all_option; 652 OptionGroupBoolean m_cleanup_option; 653 }; 654 655 class CommandObjectTargetShowLaunchEnvironment : public CommandObjectParsed { 656 public: 657 CommandObjectTargetShowLaunchEnvironment(CommandInterpreter &interpreter) 658 : CommandObjectParsed( 659 interpreter, "target show-launch-environment", 660 "Shows the environment being passed to the process when launched, " 661 "taking info account 3 settings: target.env-vars, " 662 "target.inherit-env and target.unset-env-vars.", 663 nullptr, eCommandRequiresTarget) {} 664 665 ~CommandObjectTargetShowLaunchEnvironment() override = default; 666 667 protected: 668 bool DoExecute(Args &args, CommandReturnObject &result) override { 669 Target *target = m_exe_ctx.GetTargetPtr(); 670 Environment env = target->GetEnvironment(); 671 672 std::vector<Environment::value_type *> env_vector; 673 env_vector.reserve(env.size()); 674 for (auto &KV : env) 675 env_vector.push_back(&KV); 676 std::sort(env_vector.begin(), env_vector.end(), 677 [](Environment::value_type *a, Environment::value_type *b) { 678 return a->first() < b->first(); 679 }); 680 681 auto &strm = result.GetOutputStream(); 682 for (auto &KV : env_vector) 683 strm.Format("{0}={1}\n", KV->first(), KV->second); 684 685 result.SetStatus(eReturnStatusSuccessFinishResult); 686 return result.Succeeded(); 687 } 688 }; 689 690 #pragma mark CommandObjectTargetVariable 691 692 class CommandObjectTargetVariable : public CommandObjectParsed { 693 static const uint32_t SHORT_OPTION_FILE = 0x66696c65; // 'file' 694 static const uint32_t SHORT_OPTION_SHLB = 0x73686c62; // 'shlb' 695 696 public: 697 CommandObjectTargetVariable(CommandInterpreter &interpreter) 698 : CommandObjectParsed(interpreter, "target variable", 699 "Read global variables for the current target, " 700 "before or while running a process.", 701 nullptr, eCommandRequiresTarget), 702 m_option_group(), 703 m_option_variable(false), // Don't include frame options 704 m_option_format(eFormatDefault), 705 m_option_compile_units(LLDB_OPT_SET_1, false, "file", SHORT_OPTION_FILE, 706 0, eArgTypeFilename, 707 "A basename or fullpath to a file that contains " 708 "global variables. This option can be " 709 "specified multiple times."), 710 m_option_shared_libraries( 711 LLDB_OPT_SET_1, false, "shlib", SHORT_OPTION_SHLB, 0, 712 eArgTypeFilename, 713 "A basename or fullpath to a shared library to use in the search " 714 "for global " 715 "variables. This option can be specified multiple times."), 716 m_varobj_options() { 717 CommandArgumentEntry arg; 718 CommandArgumentData var_name_arg; 719 720 // Define the first (and only) variant of this arg. 721 var_name_arg.arg_type = eArgTypeVarName; 722 var_name_arg.arg_repetition = eArgRepeatPlus; 723 724 // There is only one variant this argument could be; put it into the 725 // argument entry. 726 arg.push_back(var_name_arg); 727 728 // Push the data for the first argument into the m_arguments vector. 729 m_arguments.push_back(arg); 730 731 m_option_group.Append(&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 732 m_option_group.Append(&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 733 m_option_group.Append(&m_option_format, 734 OptionGroupFormat::OPTION_GROUP_FORMAT | 735 OptionGroupFormat::OPTION_GROUP_GDB_FMT, 736 LLDB_OPT_SET_1); 737 m_option_group.Append(&m_option_compile_units, LLDB_OPT_SET_ALL, 738 LLDB_OPT_SET_1); 739 m_option_group.Append(&m_option_shared_libraries, LLDB_OPT_SET_ALL, 740 LLDB_OPT_SET_1); 741 m_option_group.Finalize(); 742 } 743 744 ~CommandObjectTargetVariable() override = default; 745 746 void DumpValueObject(Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, 747 const char *root_name) { 748 DumpValueObjectOptions options(m_varobj_options.GetAsDumpOptions()); 749 750 if (!valobj_sp->GetTargetSP()->GetDisplayRuntimeSupportValues() && 751 valobj_sp->IsRuntimeSupportValue()) 752 return; 753 754 switch (var_sp->GetScope()) { 755 case eValueTypeVariableGlobal: 756 if (m_option_variable.show_scope) 757 s.PutCString("GLOBAL: "); 758 break; 759 760 case eValueTypeVariableStatic: 761 if (m_option_variable.show_scope) 762 s.PutCString("STATIC: "); 763 break; 764 765 case eValueTypeVariableArgument: 766 if (m_option_variable.show_scope) 767 s.PutCString(" ARG: "); 768 break; 769 770 case eValueTypeVariableLocal: 771 if (m_option_variable.show_scope) 772 s.PutCString(" LOCAL: "); 773 break; 774 775 case eValueTypeVariableThreadLocal: 776 if (m_option_variable.show_scope) 777 s.PutCString("THREAD: "); 778 break; 779 780 default: 781 break; 782 } 783 784 if (m_option_variable.show_decl) { 785 bool show_fullpaths = false; 786 bool show_module = true; 787 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module)) 788 s.PutCString(": "); 789 } 790 791 const Format format = m_option_format.GetFormat(); 792 if (format != eFormatDefault) 793 options.SetFormat(format); 794 795 options.SetRootValueObjectName(root_name); 796 797 valobj_sp->Dump(s, options); 798 } 799 800 static size_t GetVariableCallback(void *baton, const char *name, 801 VariableList &variable_list) { 802 size_t old_size = variable_list.GetSize(); 803 Target *target = static_cast<Target *>(baton); 804 if (target) 805 target->GetImages().FindGlobalVariables(ConstString(name), UINT32_MAX, 806 variable_list); 807 return variable_list.GetSize() - old_size; 808 } 809 810 Options *GetOptions() override { return &m_option_group; } 811 812 protected: 813 void DumpGlobalVariableList(const ExecutionContext &exe_ctx, 814 const SymbolContext &sc, 815 const VariableList &variable_list, Stream &s) { 816 if (variable_list.Empty()) 817 return; 818 if (sc.module_sp) { 819 if (sc.comp_unit) { 820 s.Format("Global variables for {0} in {1}:\n", 821 sc.comp_unit->GetPrimaryFile(), sc.module_sp->GetFileSpec()); 822 } else { 823 s.Printf("Global variables for %s\n", 824 sc.module_sp->GetFileSpec().GetPath().c_str()); 825 } 826 } else if (sc.comp_unit) { 827 s.Format("Global variables for {0}\n", sc.comp_unit->GetPrimaryFile()); 828 } 829 830 for (VariableSP var_sp : variable_list) { 831 if (!var_sp) 832 continue; 833 ValueObjectSP valobj_sp(ValueObjectVariable::Create( 834 exe_ctx.GetBestExecutionContextScope(), var_sp)); 835 836 if (valobj_sp) 837 DumpValueObject(s, var_sp, valobj_sp, var_sp->GetName().GetCString()); 838 } 839 } 840 841 bool DoExecute(Args &args, CommandReturnObject &result) override { 842 Target *target = m_exe_ctx.GetTargetPtr(); 843 const size_t argc = args.GetArgumentCount(); 844 Stream &s = result.GetOutputStream(); 845 846 if (argc > 0) { 847 for (const Args::ArgEntry &arg : args) { 848 VariableList variable_list; 849 ValueObjectList valobj_list; 850 851 size_t matches = 0; 852 bool use_var_name = false; 853 if (m_option_variable.use_regex) { 854 RegularExpression regex( 855 llvm::StringRef::withNullAsEmpty(arg.c_str())); 856 if (!regex.IsValid()) { 857 result.GetErrorStream().Printf( 858 "error: invalid regular expression: '%s'\n", arg.c_str()); 859 result.SetStatus(eReturnStatusFailed); 860 return false; 861 } 862 use_var_name = true; 863 target->GetImages().FindGlobalVariables(regex, UINT32_MAX, 864 variable_list); 865 matches = variable_list.GetSize(); 866 } else { 867 Status error(Variable::GetValuesForVariableExpressionPath( 868 arg.c_str(), m_exe_ctx.GetBestExecutionContextScope(), 869 GetVariableCallback, target, variable_list, valobj_list)); 870 matches = variable_list.GetSize(); 871 } 872 873 if (matches == 0) { 874 result.GetErrorStream().Printf( 875 "error: can't find global variable '%s'\n", arg.c_str()); 876 result.SetStatus(eReturnStatusFailed); 877 return false; 878 } else { 879 for (uint32_t global_idx = 0; global_idx < matches; ++global_idx) { 880 VariableSP var_sp(variable_list.GetVariableAtIndex(global_idx)); 881 if (var_sp) { 882 ValueObjectSP valobj_sp( 883 valobj_list.GetValueObjectAtIndex(global_idx)); 884 if (!valobj_sp) 885 valobj_sp = ValueObjectVariable::Create( 886 m_exe_ctx.GetBestExecutionContextScope(), var_sp); 887 888 if (valobj_sp) 889 DumpValueObject(s, var_sp, valobj_sp, 890 use_var_name ? var_sp->GetName().GetCString() 891 : arg.c_str()); 892 } 893 } 894 } 895 } 896 } else { 897 const FileSpecList &compile_units = 898 m_option_compile_units.GetOptionValue().GetCurrentValue(); 899 const FileSpecList &shlibs = 900 m_option_shared_libraries.GetOptionValue().GetCurrentValue(); 901 SymbolContextList sc_list; 902 const size_t num_compile_units = compile_units.GetSize(); 903 const size_t num_shlibs = shlibs.GetSize(); 904 if (num_compile_units == 0 && num_shlibs == 0) { 905 bool success = false; 906 StackFrame *frame = m_exe_ctx.GetFramePtr(); 907 CompileUnit *comp_unit = nullptr; 908 if (frame) { 909 SymbolContext sc = frame->GetSymbolContext(eSymbolContextCompUnit); 910 if (sc.comp_unit) { 911 const bool can_create = true; 912 VariableListSP comp_unit_varlist_sp( 913 sc.comp_unit->GetVariableList(can_create)); 914 if (comp_unit_varlist_sp) { 915 size_t count = comp_unit_varlist_sp->GetSize(); 916 if (count > 0) { 917 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, s); 918 success = true; 919 } 920 } 921 } 922 } 923 if (!success) { 924 if (frame) { 925 if (comp_unit) 926 result.AppendErrorWithFormatv( 927 "no global variables in current compile unit: {0}\n", 928 comp_unit->GetPrimaryFile()); 929 else 930 result.AppendErrorWithFormat( 931 "no debug information for frame %u\n", 932 frame->GetFrameIndex()); 933 } else 934 result.AppendError("'target variable' takes one or more global " 935 "variable names as arguments\n"); 936 result.SetStatus(eReturnStatusFailed); 937 } 938 } else { 939 SymbolContextList sc_list; 940 // We have one or more compile unit or shlib 941 if (num_shlibs > 0) { 942 for (size_t shlib_idx = 0; shlib_idx < num_shlibs; ++shlib_idx) { 943 const FileSpec module_file(shlibs.GetFileSpecAtIndex(shlib_idx)); 944 ModuleSpec module_spec(module_file); 945 946 ModuleSP module_sp( 947 target->GetImages().FindFirstModule(module_spec)); 948 if (module_sp) { 949 if (num_compile_units > 0) { 950 for (size_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) 951 module_sp->FindCompileUnits( 952 compile_units.GetFileSpecAtIndex(cu_idx), sc_list); 953 } else { 954 SymbolContext sc; 955 sc.module_sp = module_sp; 956 sc_list.Append(sc); 957 } 958 } else { 959 // Didn't find matching shlib/module in target... 960 result.AppendErrorWithFormat( 961 "target doesn't contain the specified shared library: %s\n", 962 module_file.GetPath().c_str()); 963 } 964 } 965 } else { 966 // No shared libraries, we just want to find globals for the compile 967 // units files that were specified 968 for (size_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx) 969 target->GetImages().FindCompileUnits( 970 compile_units.GetFileSpecAtIndex(cu_idx), sc_list); 971 } 972 973 const uint32_t num_scs = sc_list.GetSize(); 974 if (num_scs > 0) { 975 SymbolContext sc; 976 for (uint32_t sc_idx = 0; sc_idx < num_scs; ++sc_idx) { 977 if (sc_list.GetContextAtIndex(sc_idx, sc)) { 978 if (sc.comp_unit) { 979 const bool can_create = true; 980 VariableListSP comp_unit_varlist_sp( 981 sc.comp_unit->GetVariableList(can_create)); 982 if (comp_unit_varlist_sp) 983 DumpGlobalVariableList(m_exe_ctx, sc, *comp_unit_varlist_sp, 984 s); 985 } else if (sc.module_sp) { 986 // Get all global variables for this module 987 lldb_private::RegularExpression all_globals_regex( 988 llvm::StringRef( 989 ".")); // Any global with at least one character 990 VariableList variable_list; 991 sc.module_sp->FindGlobalVariables(all_globals_regex, UINT32_MAX, 992 variable_list); 993 DumpGlobalVariableList(m_exe_ctx, sc, variable_list, s); 994 } 995 } 996 } 997 } 998 } 999 } 1000 1001 if (m_interpreter.TruncationWarningNecessary()) { 1002 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(), 1003 m_cmd_name.c_str()); 1004 m_interpreter.TruncationWarningGiven(); 1005 } 1006 1007 return result.Succeeded(); 1008 } 1009 1010 OptionGroupOptions m_option_group; 1011 OptionGroupVariable m_option_variable; 1012 OptionGroupFormat m_option_format; 1013 OptionGroupFileList m_option_compile_units; 1014 OptionGroupFileList m_option_shared_libraries; 1015 OptionGroupValueObjectDisplay m_varobj_options; 1016 }; 1017 1018 #pragma mark CommandObjectTargetModulesSearchPathsAdd 1019 1020 class CommandObjectTargetModulesSearchPathsAdd : public CommandObjectParsed { 1021 public: 1022 CommandObjectTargetModulesSearchPathsAdd(CommandInterpreter &interpreter) 1023 : CommandObjectParsed(interpreter, "target modules search-paths add", 1024 "Add new image search paths substitution pairs to " 1025 "the current target.", 1026 nullptr, eCommandRequiresTarget) { 1027 CommandArgumentEntry arg; 1028 CommandArgumentData old_prefix_arg; 1029 CommandArgumentData new_prefix_arg; 1030 1031 // Define the first variant of this arg pair. 1032 old_prefix_arg.arg_type = eArgTypeOldPathPrefix; 1033 old_prefix_arg.arg_repetition = eArgRepeatPairPlus; 1034 1035 // Define the first variant of this arg pair. 1036 new_prefix_arg.arg_type = eArgTypeNewPathPrefix; 1037 new_prefix_arg.arg_repetition = eArgRepeatPairPlus; 1038 1039 // There are two required arguments that must always occur together, i.e. 1040 // an argument "pair". Because they must always occur together, they are 1041 // treated as two variants of one argument rather than two independent 1042 // arguments. Push them both into the first argument position for 1043 // m_arguments... 1044 1045 arg.push_back(old_prefix_arg); 1046 arg.push_back(new_prefix_arg); 1047 1048 m_arguments.push_back(arg); 1049 } 1050 1051 ~CommandObjectTargetModulesSearchPathsAdd() override = default; 1052 1053 protected: 1054 bool DoExecute(Args &command, CommandReturnObject &result) override { 1055 Target *target = &GetSelectedTarget(); 1056 const size_t argc = command.GetArgumentCount(); 1057 if (argc & 1) { 1058 result.AppendError("add requires an even number of arguments\n"); 1059 result.SetStatus(eReturnStatusFailed); 1060 } else { 1061 for (size_t i = 0; i < argc; i += 2) { 1062 const char *from = command.GetArgumentAtIndex(i); 1063 const char *to = command.GetArgumentAtIndex(i + 1); 1064 1065 if (from[0] && to[0]) { 1066 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); 1067 if (log) { 1068 LLDB_LOGF(log, 1069 "target modules search path adding ImageSearchPath " 1070 "pair: '%s' -> '%s'", 1071 from, to); 1072 } 1073 bool last_pair = ((argc - i) == 2); 1074 target->GetImageSearchPathList().Append( 1075 ConstString(from), ConstString(to), 1076 last_pair); // Notify if this is the last pair 1077 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1078 } else { 1079 if (from[0]) 1080 result.AppendError("<path-prefix> can't be empty\n"); 1081 else 1082 result.AppendError("<new-path-prefix> can't be empty\n"); 1083 result.SetStatus(eReturnStatusFailed); 1084 } 1085 } 1086 } 1087 return result.Succeeded(); 1088 } 1089 }; 1090 1091 #pragma mark CommandObjectTargetModulesSearchPathsClear 1092 1093 class CommandObjectTargetModulesSearchPathsClear : public CommandObjectParsed { 1094 public: 1095 CommandObjectTargetModulesSearchPathsClear(CommandInterpreter &interpreter) 1096 : CommandObjectParsed(interpreter, "target modules search-paths clear", 1097 "Clear all current image search path substitution " 1098 "pairs from the current target.", 1099 "target modules search-paths clear", 1100 eCommandRequiresTarget) {} 1101 1102 ~CommandObjectTargetModulesSearchPathsClear() override = default; 1103 1104 protected: 1105 bool DoExecute(Args &command, CommandReturnObject &result) override { 1106 Target *target = &GetSelectedTarget(); 1107 bool notify = true; 1108 target->GetImageSearchPathList().Clear(notify); 1109 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1110 return result.Succeeded(); 1111 } 1112 }; 1113 1114 #pragma mark CommandObjectTargetModulesSearchPathsInsert 1115 1116 class CommandObjectTargetModulesSearchPathsInsert : public CommandObjectParsed { 1117 public: 1118 CommandObjectTargetModulesSearchPathsInsert(CommandInterpreter &interpreter) 1119 : CommandObjectParsed(interpreter, "target modules search-paths insert", 1120 "Insert a new image search path substitution pair " 1121 "into the current target at the specified index.", 1122 nullptr, eCommandRequiresTarget) { 1123 CommandArgumentEntry arg1; 1124 CommandArgumentEntry arg2; 1125 CommandArgumentData index_arg; 1126 CommandArgumentData old_prefix_arg; 1127 CommandArgumentData new_prefix_arg; 1128 1129 // Define the first and only variant of this arg. 1130 index_arg.arg_type = eArgTypeIndex; 1131 index_arg.arg_repetition = eArgRepeatPlain; 1132 1133 // Put the one and only variant into the first arg for m_arguments: 1134 arg1.push_back(index_arg); 1135 1136 // Define the first variant of this arg pair. 1137 old_prefix_arg.arg_type = eArgTypeOldPathPrefix; 1138 old_prefix_arg.arg_repetition = eArgRepeatPairPlus; 1139 1140 // Define the first variant of this arg pair. 1141 new_prefix_arg.arg_type = eArgTypeNewPathPrefix; 1142 new_prefix_arg.arg_repetition = eArgRepeatPairPlus; 1143 1144 // There are two required arguments that must always occur together, i.e. 1145 // an argument "pair". Because they must always occur together, they are 1146 // treated as two variants of one argument rather than two independent 1147 // arguments. Push them both into the same argument position for 1148 // m_arguments... 1149 1150 arg2.push_back(old_prefix_arg); 1151 arg2.push_back(new_prefix_arg); 1152 1153 // Add arguments to m_arguments. 1154 m_arguments.push_back(arg1); 1155 m_arguments.push_back(arg2); 1156 } 1157 1158 ~CommandObjectTargetModulesSearchPathsInsert() override = default; 1159 1160 void 1161 HandleArgumentCompletion(CompletionRequest &request, 1162 OptionElementVector &opt_element_vector) override { 1163 if (!m_exe_ctx.HasTargetScope() || request.GetCursorIndex() != 0) 1164 return; 1165 1166 Target *target = m_exe_ctx.GetTargetPtr(); 1167 const PathMappingList &list = target->GetImageSearchPathList(); 1168 const size_t num = list.GetSize(); 1169 ConstString old_path, new_path; 1170 for (size_t i = 0; i < num; ++i) { 1171 if (!list.GetPathsAtIndex(i, old_path, new_path)) 1172 break; 1173 StreamString strm; 1174 strm << old_path << " -> " << new_path; 1175 request.TryCompleteCurrentArg(std::to_string(i), strm.GetString()); 1176 } 1177 } 1178 1179 protected: 1180 bool DoExecute(Args &command, CommandReturnObject &result) override { 1181 Target *target = &GetSelectedTarget(); 1182 size_t argc = command.GetArgumentCount(); 1183 // check for at least 3 arguments and an odd number of parameters 1184 if (argc >= 3 && argc & 1) { 1185 uint32_t insert_idx; 1186 1187 if (!llvm::to_integer(command.GetArgumentAtIndex(0), insert_idx)) { 1188 result.AppendErrorWithFormat( 1189 "<index> parameter is not an integer: '%s'.\n", 1190 command.GetArgumentAtIndex(0)); 1191 result.SetStatus(eReturnStatusFailed); 1192 return result.Succeeded(); 1193 } 1194 1195 // shift off the index 1196 command.Shift(); 1197 argc = command.GetArgumentCount(); 1198 1199 for (uint32_t i = 0; i < argc; i += 2, ++insert_idx) { 1200 const char *from = command.GetArgumentAtIndex(i); 1201 const char *to = command.GetArgumentAtIndex(i + 1); 1202 1203 if (from[0] && to[0]) { 1204 bool last_pair = ((argc - i) == 2); 1205 target->GetImageSearchPathList().Insert( 1206 ConstString(from), ConstString(to), insert_idx, last_pair); 1207 result.SetStatus(eReturnStatusSuccessFinishNoResult); 1208 } else { 1209 if (from[0]) 1210 result.AppendError("<path-prefix> can't be empty\n"); 1211 else 1212 result.AppendError("<new-path-prefix> can't be empty\n"); 1213 result.SetStatus(eReturnStatusFailed); 1214 return false; 1215 } 1216 } 1217 } else { 1218 result.AppendError("insert requires at least three arguments\n"); 1219 result.SetStatus(eReturnStatusFailed); 1220 return result.Succeeded(); 1221 } 1222 return result.Succeeded(); 1223 } 1224 }; 1225 1226 #pragma mark CommandObjectTargetModulesSearchPathsList 1227 1228 class CommandObjectTargetModulesSearchPathsList : public CommandObjectParsed { 1229 public: 1230 CommandObjectTargetModulesSearchPathsList(CommandInterpreter &interpreter) 1231 : CommandObjectParsed(interpreter, "target modules search-paths list", 1232 "List all current image search path substitution " 1233 "pairs in the current target.", 1234 "target modules search-paths list", 1235 eCommandRequiresTarget) {} 1236 1237 ~CommandObjectTargetModulesSearchPathsList() override = default; 1238 1239 protected: 1240 bool DoExecute(Args &command, CommandReturnObject &result) override { 1241 Target *target = &GetSelectedTarget(); 1242 if (command.GetArgumentCount() != 0) { 1243 result.AppendError("list takes no arguments\n"); 1244 result.SetStatus(eReturnStatusFailed); 1245 return result.Succeeded(); 1246 } 1247 1248 target->GetImageSearchPathList().Dump(&result.GetOutputStream()); 1249 result.SetStatus(eReturnStatusSuccessFinishResult); 1250 return result.Succeeded(); 1251 } 1252 }; 1253 1254 #pragma mark CommandObjectTargetModulesSearchPathsQuery 1255 1256 class CommandObjectTargetModulesSearchPathsQuery : public CommandObjectParsed { 1257 public: 1258 CommandObjectTargetModulesSearchPathsQuery(CommandInterpreter &interpreter) 1259 : CommandObjectParsed( 1260 interpreter, "target modules search-paths query", 1261 "Transform a path using the first applicable image search path.", 1262 nullptr, eCommandRequiresTarget) { 1263 CommandArgumentEntry arg; 1264 CommandArgumentData path_arg; 1265 1266 // Define the first (and only) variant of this arg. 1267 path_arg.arg_type = eArgTypeDirectoryName; 1268 path_arg.arg_repetition = eArgRepeatPlain; 1269 1270 // There is only one variant this argument could be; put it into the 1271 // argument entry. 1272 arg.push_back(path_arg); 1273 1274 // Push the data for the first argument into the m_arguments vector. 1275 m_arguments.push_back(arg); 1276 } 1277 1278 ~CommandObjectTargetModulesSearchPathsQuery() override = default; 1279 1280 protected: 1281 bool DoExecute(Args &command, CommandReturnObject &result) override { 1282 Target *target = &GetSelectedTarget(); 1283 if (command.GetArgumentCount() != 1) { 1284 result.AppendError("query requires one argument\n"); 1285 result.SetStatus(eReturnStatusFailed); 1286 return result.Succeeded(); 1287 } 1288 1289 ConstString orig(command.GetArgumentAtIndex(0)); 1290 ConstString transformed; 1291 if (target->GetImageSearchPathList().RemapPath(orig, transformed)) 1292 result.GetOutputStream().Printf("%s\n", transformed.GetCString()); 1293 else 1294 result.GetOutputStream().Printf("%s\n", orig.GetCString()); 1295 1296 result.SetStatus(eReturnStatusSuccessFinishResult); 1297 return result.Succeeded(); 1298 } 1299 }; 1300 1301 // Static Helper functions 1302 static void DumpModuleArchitecture(Stream &strm, Module *module, 1303 bool full_triple, uint32_t width) { 1304 if (module) { 1305 StreamString arch_strm; 1306 1307 if (full_triple) 1308 module->GetArchitecture().DumpTriple(arch_strm.AsRawOstream()); 1309 else 1310 arch_strm.PutCString(module->GetArchitecture().GetArchitectureName()); 1311 std::string arch_str = std::string(arch_strm.GetString()); 1312 1313 if (width) 1314 strm.Printf("%-*s", width, arch_str.c_str()); 1315 else 1316 strm.PutCString(arch_str); 1317 } 1318 } 1319 1320 static void DumpModuleUUID(Stream &strm, Module *module) { 1321 if (module && module->GetUUID().IsValid()) 1322 module->GetUUID().Dump(&strm); 1323 else 1324 strm.PutCString(" "); 1325 } 1326 1327 static uint32_t DumpCompileUnitLineTable(CommandInterpreter &interpreter, 1328 Stream &strm, Module *module, 1329 const FileSpec &file_spec, 1330 lldb::DescriptionLevel desc_level) { 1331 uint32_t num_matches = 0; 1332 if (module) { 1333 SymbolContextList sc_list; 1334 num_matches = module->ResolveSymbolContextsForFileSpec( 1335 file_spec, 0, false, eSymbolContextCompUnit, sc_list); 1336 1337 for (uint32_t i = 0; i < num_matches; ++i) { 1338 SymbolContext sc; 1339 if (sc_list.GetContextAtIndex(i, sc)) { 1340 if (i > 0) 1341 strm << "\n\n"; 1342 1343 strm << "Line table for " << sc.comp_unit->GetPrimaryFile() << " in `" 1344 << module->GetFileSpec().GetFilename() << "\n"; 1345 LineTable *line_table = sc.comp_unit->GetLineTable(); 1346 if (line_table) 1347 line_table->GetDescription( 1348 &strm, interpreter.GetExecutionContext().GetTargetPtr(), 1349 desc_level); 1350 else 1351 strm << "No line table"; 1352 } 1353 } 1354 } 1355 return num_matches; 1356 } 1357 1358 static void DumpFullpath(Stream &strm, const FileSpec *file_spec_ptr, 1359 uint32_t width) { 1360 if (file_spec_ptr) { 1361 if (width > 0) { 1362 std::string fullpath = file_spec_ptr->GetPath(); 1363 strm.Printf("%-*s", width, fullpath.c_str()); 1364 return; 1365 } else { 1366 file_spec_ptr->Dump(strm.AsRawOstream()); 1367 return; 1368 } 1369 } 1370 // Keep the width spacing correct if things go wrong... 1371 if (width > 0) 1372 strm.Printf("%-*s", width, ""); 1373 } 1374 1375 static void DumpDirectory(Stream &strm, const FileSpec *file_spec_ptr, 1376 uint32_t width) { 1377 if (file_spec_ptr) { 1378 if (width > 0) 1379 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString("")); 1380 else 1381 file_spec_ptr->GetDirectory().Dump(&strm); 1382 return; 1383 } 1384 // Keep the width spacing correct if things go wrong... 1385 if (width > 0) 1386 strm.Printf("%-*s", width, ""); 1387 } 1388 1389 static void DumpBasename(Stream &strm, const FileSpec *file_spec_ptr, 1390 uint32_t width) { 1391 if (file_spec_ptr) { 1392 if (width > 0) 1393 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString("")); 1394 else 1395 file_spec_ptr->GetFilename().Dump(&strm); 1396 return; 1397 } 1398 // Keep the width spacing correct if things go wrong... 1399 if (width > 0) 1400 strm.Printf("%-*s", width, ""); 1401 } 1402 1403 static size_t DumpModuleObjfileHeaders(Stream &strm, ModuleList &module_list) { 1404 size_t num_dumped = 0; 1405 std::lock_guard<std::recursive_mutex> guard(module_list.GetMutex()); 1406 const size_t num_modules = module_list.GetSize(); 1407 if (num_modules > 0) { 1408 strm.Printf("Dumping headers for %" PRIu64 " module(s).\n", 1409 static_cast<uint64_t>(num_modules)); 1410 strm.IndentMore(); 1411 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { 1412 Module *module = module_list.GetModulePointerAtIndexUnlocked(image_idx); 1413 if (module) { 1414 if (num_dumped++ > 0) { 1415 strm.EOL(); 1416 strm.EOL(); 1417 } 1418 ObjectFile *objfile = module->GetObjectFile(); 1419 if (objfile) 1420 objfile->Dump(&strm); 1421 else { 1422 strm.Format("No object file for module: {0:F}\n", 1423 module->GetFileSpec()); 1424 } 1425 } 1426 } 1427 strm.IndentLess(); 1428 } 1429 return num_dumped; 1430 } 1431 1432 static void DumpModuleSymtab(CommandInterpreter &interpreter, Stream &strm, 1433 Module *module, SortOrder sort_order, 1434 Mangled::NamePreference name_preference) { 1435 if (!module) 1436 return; 1437 if (Symtab *symtab = module->GetSymtab()) 1438 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), 1439 sort_order, name_preference); 1440 } 1441 1442 static void DumpModuleSections(CommandInterpreter &interpreter, Stream &strm, 1443 Module *module) { 1444 if (module) { 1445 SectionList *section_list = module->GetSectionList(); 1446 if (section_list) { 1447 strm.Printf("Sections for '%s' (%s):\n", 1448 module->GetSpecificationDescription().c_str(), 1449 module->GetArchitecture().GetArchitectureName()); 1450 section_list->Dump(strm.AsRawOstream(), strm.GetIndentLevel() + 2, 1451 interpreter.GetExecutionContext().GetTargetPtr(), true, 1452 UINT32_MAX); 1453 } 1454 } 1455 } 1456 1457 static bool DumpModuleSymbolFile(Stream &strm, Module *module) { 1458 if (module) { 1459 if (SymbolFile *symbol_file = module->GetSymbolFile(true)) { 1460 symbol_file->Dump(strm); 1461 return true; 1462 } 1463 } 1464 return false; 1465 } 1466 1467 static void DumpAddress(ExecutionContextScope *exe_scope, 1468 const Address &so_addr, bool verbose, Stream &strm) { 1469 strm.IndentMore(); 1470 strm.Indent(" Address: "); 1471 so_addr.Dump(&strm, exe_scope, Address::DumpStyleModuleWithFileAddress); 1472 strm.PutCString(" ("); 1473 so_addr.Dump(&strm, exe_scope, Address::DumpStyleSectionNameOffset); 1474 strm.PutCString(")\n"); 1475 strm.Indent(" Summary: "); 1476 const uint32_t save_indent = strm.GetIndentLevel(); 1477 strm.SetIndentLevel(save_indent + 13); 1478 so_addr.Dump(&strm, exe_scope, Address::DumpStyleResolvedDescription); 1479 strm.SetIndentLevel(save_indent); 1480 // Print out detailed address information when verbose is enabled 1481 if (verbose) { 1482 strm.EOL(); 1483 so_addr.Dump(&strm, exe_scope, Address::DumpStyleDetailedSymbolContext); 1484 } 1485 strm.IndentLess(); 1486 } 1487 1488 static bool LookupAddressInModule(CommandInterpreter &interpreter, Stream &strm, 1489 Module *module, uint32_t resolve_mask, 1490 lldb::addr_t raw_addr, lldb::addr_t offset, 1491 bool verbose) { 1492 if (module) { 1493 lldb::addr_t addr = raw_addr - offset; 1494 Address so_addr; 1495 SymbolContext sc; 1496 Target *target = interpreter.GetExecutionContext().GetTargetPtr(); 1497 if (target && !target->GetSectionLoadList().IsEmpty()) { 1498 if (!target->GetSectionLoadList().ResolveLoadAddress(addr, so_addr)) 1499 return false; 1500 else if (so_addr.GetModule().get() != module) 1501 return false; 1502 } else { 1503 if (!module->ResolveFileAddress(addr, so_addr)) 1504 return false; 1505 } 1506 1507 ExecutionContextScope *exe_scope = 1508 interpreter.GetExecutionContext().GetBestExecutionContextScope(); 1509 DumpAddress(exe_scope, so_addr, verbose, strm); 1510 // strm.IndentMore(); 1511 // strm.Indent (" Address: "); 1512 // so_addr.Dump (&strm, exe_scope, 1513 // Address::DumpStyleModuleWithFileAddress); 1514 // strm.PutCString (" ("); 1515 // so_addr.Dump (&strm, exe_scope, 1516 // Address::DumpStyleSectionNameOffset); 1517 // strm.PutCString (")\n"); 1518 // strm.Indent (" Summary: "); 1519 // const uint32_t save_indent = strm.GetIndentLevel (); 1520 // strm.SetIndentLevel (save_indent + 13); 1521 // so_addr.Dump (&strm, exe_scope, 1522 // Address::DumpStyleResolvedDescription); 1523 // strm.SetIndentLevel (save_indent); 1524 // // Print out detailed address information when verbose is enabled 1525 // if (verbose) 1526 // { 1527 // strm.EOL(); 1528 // so_addr.Dump (&strm, exe_scope, 1529 // Address::DumpStyleDetailedSymbolContext); 1530 // } 1531 // strm.IndentLess(); 1532 return true; 1533 } 1534 1535 return false; 1536 } 1537 1538 static uint32_t LookupSymbolInModule(CommandInterpreter &interpreter, 1539 Stream &strm, Module *module, 1540 const char *name, bool name_is_regex, 1541 bool verbose) { 1542 if (!module) 1543 return 0; 1544 1545 Symtab *symtab = module->GetSymtab(); 1546 if (!symtab) 1547 return 0; 1548 1549 SymbolContext sc; 1550 std::vector<uint32_t> match_indexes; 1551 ConstString symbol_name(name); 1552 uint32_t num_matches = 0; 1553 if (name_is_regex) { 1554 RegularExpression name_regexp(symbol_name.GetStringRef()); 1555 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType( 1556 name_regexp, eSymbolTypeAny, match_indexes); 1557 } else { 1558 num_matches = 1559 symtab->AppendSymbolIndexesWithName(symbol_name, match_indexes); 1560 } 1561 1562 if (num_matches > 0) { 1563 strm.Indent(); 1564 strm.Printf("%u symbols match %s'%s' in ", num_matches, 1565 name_is_regex ? "the regular expression " : "", name); 1566 DumpFullpath(strm, &module->GetFileSpec(), 0); 1567 strm.PutCString(":\n"); 1568 strm.IndentMore(); 1569 for (uint32_t i = 0; i < num_matches; ++i) { 1570 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]); 1571 if (symbol && symbol->ValueIsAddress()) { 1572 DumpAddress( 1573 interpreter.GetExecutionContext().GetBestExecutionContextScope(), 1574 symbol->GetAddressRef(), verbose, strm); 1575 } 1576 } 1577 strm.IndentLess(); 1578 } 1579 return num_matches; 1580 } 1581 1582 static void DumpSymbolContextList(ExecutionContextScope *exe_scope, 1583 Stream &strm, SymbolContextList &sc_list, 1584 bool verbose) { 1585 strm.IndentMore(); 1586 1587 const uint32_t num_matches = sc_list.GetSize(); 1588 1589 for (uint32_t i = 0; i < num_matches; ++i) { 1590 SymbolContext sc; 1591 if (sc_list.GetContextAtIndex(i, sc)) { 1592 AddressRange range; 1593 1594 sc.GetAddressRange(eSymbolContextEverything, 0, true, range); 1595 1596 DumpAddress(exe_scope, range.GetBaseAddress(), verbose, strm); 1597 } 1598 } 1599 strm.IndentLess(); 1600 } 1601 1602 static size_t LookupFunctionInModule(CommandInterpreter &interpreter, 1603 Stream &strm, Module *module, 1604 const char *name, bool name_is_regex, 1605 bool include_inlines, bool include_symbols, 1606 bool verbose) { 1607 if (module && name && name[0]) { 1608 SymbolContextList sc_list; 1609 size_t num_matches = 0; 1610 if (name_is_regex) { 1611 RegularExpression function_name_regex((llvm::StringRef(name))); 1612 module->FindFunctions(function_name_regex, include_symbols, 1613 include_inlines, sc_list); 1614 } else { 1615 ConstString function_name(name); 1616 module->FindFunctions(function_name, CompilerDeclContext(), 1617 eFunctionNameTypeAuto, include_symbols, 1618 include_inlines, sc_list); 1619 } 1620 num_matches = sc_list.GetSize(); 1621 if (num_matches) { 1622 strm.Indent(); 1623 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, 1624 num_matches > 1 ? "es" : ""); 1625 DumpFullpath(strm, &module->GetFileSpec(), 0); 1626 strm.PutCString(":\n"); 1627 DumpSymbolContextList( 1628 interpreter.GetExecutionContext().GetBestExecutionContextScope(), 1629 strm, sc_list, verbose); 1630 } 1631 return num_matches; 1632 } 1633 return 0; 1634 } 1635 1636 static size_t LookupTypeInModule(Target *target, 1637 CommandInterpreter &interpreter, Stream &strm, 1638 Module *module, const char *name_cstr, 1639 bool name_is_regex) { 1640 TypeList type_list; 1641 if (module && name_cstr && name_cstr[0]) { 1642 const uint32_t max_num_matches = UINT32_MAX; 1643 size_t num_matches = 0; 1644 bool name_is_fully_qualified = false; 1645 1646 ConstString name(name_cstr); 1647 llvm::DenseSet<lldb_private::SymbolFile *> searched_symbol_files; 1648 module->FindTypes(name, name_is_fully_qualified, max_num_matches, 1649 searched_symbol_files, type_list); 1650 1651 if (type_list.Empty()) 1652 return 0; 1653 1654 strm.Indent(); 1655 strm.Printf("%" PRIu64 " match%s found in ", (uint64_t)num_matches, 1656 num_matches > 1 ? "es" : ""); 1657 DumpFullpath(strm, &module->GetFileSpec(), 0); 1658 strm.PutCString(":\n"); 1659 for (TypeSP type_sp : type_list.Types()) { 1660 if (!type_sp) 1661 continue; 1662 // Resolve the clang type so that any forward references to types 1663 // that haven't yet been parsed will get parsed. 1664 type_sp->GetFullCompilerType(); 1665 type_sp->GetDescription(&strm, eDescriptionLevelFull, true, target); 1666 // Print all typedef chains 1667 TypeSP typedef_type_sp(type_sp); 1668 TypeSP typedefed_type_sp(typedef_type_sp->GetTypedefType()); 1669 while (typedefed_type_sp) { 1670 strm.EOL(); 1671 strm.Printf(" typedef '%s': ", 1672 typedef_type_sp->GetName().GetCString()); 1673 typedefed_type_sp->GetFullCompilerType(); 1674 typedefed_type_sp->GetDescription(&strm, eDescriptionLevelFull, true, 1675 target); 1676 typedef_type_sp = typedefed_type_sp; 1677 typedefed_type_sp = typedef_type_sp->GetTypedefType(); 1678 } 1679 } 1680 strm.EOL(); 1681 } 1682 return type_list.GetSize(); 1683 } 1684 1685 static size_t LookupTypeHere(Target *target, CommandInterpreter &interpreter, 1686 Stream &strm, Module &module, 1687 const char *name_cstr, bool name_is_regex) { 1688 TypeList type_list; 1689 const uint32_t max_num_matches = UINT32_MAX; 1690 bool name_is_fully_qualified = false; 1691 1692 ConstString name(name_cstr); 1693 llvm::DenseSet<SymbolFile *> searched_symbol_files; 1694 module.FindTypes(name, name_is_fully_qualified, max_num_matches, 1695 searched_symbol_files, type_list); 1696 1697 if (type_list.Empty()) 1698 return 0; 1699 1700 strm.Indent(); 1701 strm.PutCString("Best match found in "); 1702 DumpFullpath(strm, &module.GetFileSpec(), 0); 1703 strm.PutCString(":\n"); 1704 1705 TypeSP type_sp(type_list.GetTypeAtIndex(0)); 1706 if (type_sp) { 1707 // Resolve the clang type so that any forward references to types that 1708 // haven't yet been parsed will get parsed. 1709 type_sp->GetFullCompilerType(); 1710 type_sp->GetDescription(&strm, eDescriptionLevelFull, true, target); 1711 // Print all typedef chains. 1712 TypeSP typedef_type_sp(type_sp); 1713 TypeSP typedefed_type_sp(typedef_type_sp->GetTypedefType()); 1714 while (typedefed_type_sp) { 1715 strm.EOL(); 1716 strm.Printf(" typedef '%s': ", 1717 typedef_type_sp->GetName().GetCString()); 1718 typedefed_type_sp->GetFullCompilerType(); 1719 typedefed_type_sp->GetDescription(&strm, eDescriptionLevelFull, true, 1720 target); 1721 typedef_type_sp = typedefed_type_sp; 1722 typedefed_type_sp = typedef_type_sp->GetTypedefType(); 1723 } 1724 } 1725 strm.EOL(); 1726 return type_list.GetSize(); 1727 } 1728 1729 static uint32_t LookupFileAndLineInModule(CommandInterpreter &interpreter, 1730 Stream &strm, Module *module, 1731 const FileSpec &file_spec, 1732 uint32_t line, bool check_inlines, 1733 bool verbose) { 1734 if (module && file_spec) { 1735 SymbolContextList sc_list; 1736 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec( 1737 file_spec, line, check_inlines, eSymbolContextEverything, sc_list); 1738 if (num_matches > 0) { 1739 strm.Indent(); 1740 strm.Printf("%u match%s found in ", num_matches, 1741 num_matches > 1 ? "es" : ""); 1742 strm << file_spec; 1743 if (line > 0) 1744 strm.Printf(":%u", line); 1745 strm << " in "; 1746 DumpFullpath(strm, &module->GetFileSpec(), 0); 1747 strm.PutCString(":\n"); 1748 DumpSymbolContextList( 1749 interpreter.GetExecutionContext().GetBestExecutionContextScope(), 1750 strm, sc_list, verbose); 1751 return num_matches; 1752 } 1753 } 1754 return 0; 1755 } 1756 1757 static size_t FindModulesByName(Target *target, const char *module_name, 1758 ModuleList &module_list, 1759 bool check_global_list) { 1760 FileSpec module_file_spec(module_name); 1761 ModuleSpec module_spec(module_file_spec); 1762 1763 const size_t initial_size = module_list.GetSize(); 1764 1765 if (check_global_list) { 1766 // Check the global list 1767 std::lock_guard<std::recursive_mutex> guard( 1768 Module::GetAllocationModuleCollectionMutex()); 1769 const size_t num_modules = Module::GetNumberAllocatedModules(); 1770 ModuleSP module_sp; 1771 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { 1772 Module *module = Module::GetAllocatedModuleAtIndex(image_idx); 1773 1774 if (module) { 1775 if (module->MatchesModuleSpec(module_spec)) { 1776 module_sp = module->shared_from_this(); 1777 module_list.AppendIfNeeded(module_sp); 1778 } 1779 } 1780 } 1781 } else { 1782 if (target) { 1783 target->GetImages().FindModules(module_spec, module_list); 1784 const size_t num_matches = module_list.GetSize(); 1785 1786 // Not found in our module list for our target, check the main shared 1787 // module list in case it is a extra file used somewhere else 1788 if (num_matches == 0) { 1789 module_spec.GetArchitecture() = target->GetArchitecture(); 1790 ModuleList::FindSharedModules(module_spec, module_list); 1791 } 1792 } else { 1793 ModuleList::FindSharedModules(module_spec, module_list); 1794 } 1795 } 1796 1797 return module_list.GetSize() - initial_size; 1798 } 1799 1800 #pragma mark CommandObjectTargetModulesModuleAutoComplete 1801 1802 // A base command object class that can auto complete with module file 1803 // paths 1804 1805 class CommandObjectTargetModulesModuleAutoComplete 1806 : public CommandObjectParsed { 1807 public: 1808 CommandObjectTargetModulesModuleAutoComplete(CommandInterpreter &interpreter, 1809 const char *name, 1810 const char *help, 1811 const char *syntax, 1812 uint32_t flags = 0) 1813 : CommandObjectParsed(interpreter, name, help, syntax, flags) { 1814 CommandArgumentEntry arg; 1815 CommandArgumentData file_arg; 1816 1817 // Define the first (and only) variant of this arg. 1818 file_arg.arg_type = eArgTypeFilename; 1819 file_arg.arg_repetition = eArgRepeatStar; 1820 1821 // There is only one variant this argument could be; put it into the 1822 // argument entry. 1823 arg.push_back(file_arg); 1824 1825 // Push the data for the first argument into the m_arguments vector. 1826 m_arguments.push_back(arg); 1827 } 1828 1829 ~CommandObjectTargetModulesModuleAutoComplete() override = default; 1830 1831 void 1832 HandleArgumentCompletion(CompletionRequest &request, 1833 OptionElementVector &opt_element_vector) override { 1834 CommandCompletions::InvokeCommonCompletionCallbacks( 1835 GetCommandInterpreter(), CommandCompletions::eModuleCompletion, request, 1836 nullptr); 1837 } 1838 }; 1839 1840 #pragma mark CommandObjectTargetModulesSourceFileAutoComplete 1841 1842 // A base command object class that can auto complete with module source 1843 // file paths 1844 1845 class CommandObjectTargetModulesSourceFileAutoComplete 1846 : public CommandObjectParsed { 1847 public: 1848 CommandObjectTargetModulesSourceFileAutoComplete( 1849 CommandInterpreter &interpreter, const char *name, const char *help, 1850 const char *syntax, uint32_t flags) 1851 : CommandObjectParsed(interpreter, name, help, syntax, flags) { 1852 CommandArgumentEntry arg; 1853 CommandArgumentData source_file_arg; 1854 1855 // Define the first (and only) variant of this arg. 1856 source_file_arg.arg_type = eArgTypeSourceFile; 1857 source_file_arg.arg_repetition = eArgRepeatPlus; 1858 1859 // There is only one variant this argument could be; put it into the 1860 // argument entry. 1861 arg.push_back(source_file_arg); 1862 1863 // Push the data for the first argument into the m_arguments vector. 1864 m_arguments.push_back(arg); 1865 } 1866 1867 ~CommandObjectTargetModulesSourceFileAutoComplete() override = default; 1868 1869 void 1870 HandleArgumentCompletion(CompletionRequest &request, 1871 OptionElementVector &opt_element_vector) override { 1872 CommandCompletions::InvokeCommonCompletionCallbacks( 1873 GetCommandInterpreter(), CommandCompletions::eSourceFileCompletion, 1874 request, nullptr); 1875 } 1876 }; 1877 1878 #pragma mark CommandObjectTargetModulesDumpObjfile 1879 1880 class CommandObjectTargetModulesDumpObjfile 1881 : public CommandObjectTargetModulesModuleAutoComplete { 1882 public: 1883 CommandObjectTargetModulesDumpObjfile(CommandInterpreter &interpreter) 1884 : CommandObjectTargetModulesModuleAutoComplete( 1885 interpreter, "target modules dump objfile", 1886 "Dump the object file headers from one or more target modules.", 1887 nullptr, eCommandRequiresTarget) {} 1888 1889 ~CommandObjectTargetModulesDumpObjfile() override = default; 1890 1891 protected: 1892 bool DoExecute(Args &command, CommandReturnObject &result) override { 1893 Target *target = &GetSelectedTarget(); 1894 1895 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 1896 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 1897 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 1898 1899 size_t num_dumped = 0; 1900 if (command.GetArgumentCount() == 0) { 1901 // Dump all headers for all modules images 1902 num_dumped = DumpModuleObjfileHeaders(result.GetOutputStream(), 1903 target->GetImages()); 1904 if (num_dumped == 0) { 1905 result.AppendError("the target has no associated executable images"); 1906 result.SetStatus(eReturnStatusFailed); 1907 } 1908 } else { 1909 // Find the modules that match the basename or full path. 1910 ModuleList module_list; 1911 const char *arg_cstr; 1912 for (int arg_idx = 0; 1913 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; 1914 ++arg_idx) { 1915 size_t num_matched = 1916 FindModulesByName(target, arg_cstr, module_list, true); 1917 if (num_matched == 0) { 1918 result.AppendWarningWithFormat( 1919 "Unable to find an image that matches '%s'.\n", arg_cstr); 1920 } 1921 } 1922 // Dump all the modules we found. 1923 num_dumped = 1924 DumpModuleObjfileHeaders(result.GetOutputStream(), module_list); 1925 } 1926 1927 if (num_dumped > 0) { 1928 result.SetStatus(eReturnStatusSuccessFinishResult); 1929 } else { 1930 result.AppendError("no matching executable images found"); 1931 result.SetStatus(eReturnStatusFailed); 1932 } 1933 return result.Succeeded(); 1934 } 1935 }; 1936 1937 #pragma mark CommandObjectTargetModulesDumpSymtab 1938 1939 static constexpr OptionEnumValueElement g_sort_option_enumeration[] = { 1940 { 1941 eSortOrderNone, 1942 "none", 1943 "No sorting, use the original symbol table order.", 1944 }, 1945 { 1946 eSortOrderByAddress, 1947 "address", 1948 "Sort output by symbol address.", 1949 }, 1950 { 1951 eSortOrderByName, 1952 "name", 1953 "Sort output by symbol name.", 1954 }, 1955 }; 1956 1957 #define LLDB_OPTIONS_target_modules_dump_symtab 1958 #include "CommandOptions.inc" 1959 1960 class CommandObjectTargetModulesDumpSymtab 1961 : public CommandObjectTargetModulesModuleAutoComplete { 1962 public: 1963 CommandObjectTargetModulesDumpSymtab(CommandInterpreter &interpreter) 1964 : CommandObjectTargetModulesModuleAutoComplete( 1965 interpreter, "target modules dump symtab", 1966 "Dump the symbol table from one or more target modules.", nullptr, 1967 eCommandRequiresTarget), 1968 m_options() {} 1969 1970 ~CommandObjectTargetModulesDumpSymtab() override = default; 1971 1972 Options *GetOptions() override { return &m_options; } 1973 1974 class CommandOptions : public Options { 1975 public: 1976 CommandOptions() : Options(), m_sort_order(eSortOrderNone) {} 1977 1978 ~CommandOptions() override = default; 1979 1980 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 1981 ExecutionContext *execution_context) override { 1982 Status error; 1983 const int short_option = m_getopt_table[option_idx].val; 1984 1985 switch (short_option) { 1986 case 'm': 1987 m_prefer_mangled.SetCurrentValue(true); 1988 m_prefer_mangled.SetOptionWasSet(); 1989 break; 1990 1991 case 's': 1992 m_sort_order = (SortOrder)OptionArgParser::ToOptionEnum( 1993 option_arg, GetDefinitions()[option_idx].enum_values, 1994 eSortOrderNone, error); 1995 break; 1996 1997 default: 1998 llvm_unreachable("Unimplemented option"); 1999 } 2000 return error; 2001 } 2002 2003 void OptionParsingStarting(ExecutionContext *execution_context) override { 2004 m_sort_order = eSortOrderNone; 2005 m_prefer_mangled.Clear(); 2006 } 2007 2008 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2009 return llvm::makeArrayRef(g_target_modules_dump_symtab_options); 2010 } 2011 2012 SortOrder m_sort_order; 2013 OptionValueBoolean m_prefer_mangled = {false, false}; 2014 }; 2015 2016 protected: 2017 bool DoExecute(Args &command, CommandReturnObject &result) override { 2018 Target *target = &GetSelectedTarget(); 2019 uint32_t num_dumped = 0; 2020 Mangled::NamePreference name_preference = 2021 (m_options.m_prefer_mangled ? Mangled::ePreferMangled 2022 : Mangled::ePreferDemangled); 2023 2024 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 2025 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 2026 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 2027 2028 if (command.GetArgumentCount() == 0) { 2029 // Dump all sections for all modules images 2030 std::lock_guard<std::recursive_mutex> guard( 2031 target->GetImages().GetMutex()); 2032 const size_t num_modules = target->GetImages().GetSize(); 2033 if (num_modules > 0) { 2034 result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 2035 " modules.\n", 2036 (uint64_t)num_modules); 2037 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { 2038 if (num_dumped > 0) { 2039 result.GetOutputStream().EOL(); 2040 result.GetOutputStream().EOL(); 2041 } 2042 if (m_interpreter.WasInterrupted()) 2043 break; 2044 num_dumped++; 2045 DumpModuleSymtab( 2046 m_interpreter, result.GetOutputStream(), 2047 target->GetImages().GetModulePointerAtIndexUnlocked(image_idx), 2048 m_options.m_sort_order, name_preference); 2049 } 2050 } else { 2051 result.AppendError("the target has no associated executable images"); 2052 result.SetStatus(eReturnStatusFailed); 2053 return false; 2054 } 2055 } else { 2056 // Dump specified images (by basename or fullpath) 2057 const char *arg_cstr; 2058 for (int arg_idx = 0; 2059 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; 2060 ++arg_idx) { 2061 ModuleList module_list; 2062 const size_t num_matches = 2063 FindModulesByName(target, arg_cstr, module_list, true); 2064 if (num_matches > 0) { 2065 for (size_t i = 0; i < num_matches; ++i) { 2066 Module *module = module_list.GetModulePointerAtIndex(i); 2067 if (module) { 2068 if (num_dumped > 0) { 2069 result.GetOutputStream().EOL(); 2070 result.GetOutputStream().EOL(); 2071 } 2072 if (m_interpreter.WasInterrupted()) 2073 break; 2074 num_dumped++; 2075 DumpModuleSymtab(m_interpreter, result.GetOutputStream(), module, 2076 m_options.m_sort_order, name_preference); 2077 } 2078 } 2079 } else 2080 result.AppendWarningWithFormat( 2081 "Unable to find an image that matches '%s'.\n", arg_cstr); 2082 } 2083 } 2084 2085 if (num_dumped > 0) 2086 result.SetStatus(eReturnStatusSuccessFinishResult); 2087 else { 2088 result.AppendError("no matching executable images found"); 2089 result.SetStatus(eReturnStatusFailed); 2090 } 2091 return result.Succeeded(); 2092 } 2093 2094 CommandOptions m_options; 2095 }; 2096 2097 #pragma mark CommandObjectTargetModulesDumpSections 2098 2099 // Image section dumping command 2100 2101 class CommandObjectTargetModulesDumpSections 2102 : public CommandObjectTargetModulesModuleAutoComplete { 2103 public: 2104 CommandObjectTargetModulesDumpSections(CommandInterpreter &interpreter) 2105 : CommandObjectTargetModulesModuleAutoComplete( 2106 interpreter, "target modules dump sections", 2107 "Dump the sections from one or more target modules.", 2108 //"target modules dump sections [<file1> ...]") 2109 nullptr, eCommandRequiresTarget) {} 2110 2111 ~CommandObjectTargetModulesDumpSections() override = default; 2112 2113 protected: 2114 bool DoExecute(Args &command, CommandReturnObject &result) override { 2115 Target *target = &GetSelectedTarget(); 2116 uint32_t num_dumped = 0; 2117 2118 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 2119 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 2120 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 2121 2122 if (command.GetArgumentCount() == 0) { 2123 // Dump all sections for all modules images 2124 const size_t num_modules = target->GetImages().GetSize(); 2125 if (num_modules > 0) { 2126 result.GetOutputStream().Printf("Dumping sections for %" PRIu64 2127 " modules.\n", 2128 (uint64_t)num_modules); 2129 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { 2130 if (m_interpreter.WasInterrupted()) 2131 break; 2132 num_dumped++; 2133 DumpModuleSections( 2134 m_interpreter, result.GetOutputStream(), 2135 target->GetImages().GetModulePointerAtIndex(image_idx)); 2136 } 2137 } else { 2138 result.AppendError("the target has no associated executable images"); 2139 result.SetStatus(eReturnStatusFailed); 2140 return false; 2141 } 2142 } else { 2143 // Dump specified images (by basename or fullpath) 2144 const char *arg_cstr; 2145 for (int arg_idx = 0; 2146 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; 2147 ++arg_idx) { 2148 ModuleList module_list; 2149 const size_t num_matches = 2150 FindModulesByName(target, arg_cstr, module_list, true); 2151 if (num_matches > 0) { 2152 for (size_t i = 0; i < num_matches; ++i) { 2153 if (m_interpreter.WasInterrupted()) 2154 break; 2155 Module *module = module_list.GetModulePointerAtIndex(i); 2156 if (module) { 2157 num_dumped++; 2158 DumpModuleSections(m_interpreter, result.GetOutputStream(), 2159 module); 2160 } 2161 } 2162 } else { 2163 // Check the global list 2164 std::lock_guard<std::recursive_mutex> guard( 2165 Module::GetAllocationModuleCollectionMutex()); 2166 2167 result.AppendWarningWithFormat( 2168 "Unable to find an image that matches '%s'.\n", arg_cstr); 2169 } 2170 } 2171 } 2172 2173 if (num_dumped > 0) 2174 result.SetStatus(eReturnStatusSuccessFinishResult); 2175 else { 2176 result.AppendError("no matching executable images found"); 2177 result.SetStatus(eReturnStatusFailed); 2178 } 2179 return result.Succeeded(); 2180 } 2181 }; 2182 2183 #pragma mark CommandObjectTargetModulesDumpClangAST 2184 2185 // Clang AST dumping command 2186 2187 class CommandObjectTargetModulesDumpClangAST 2188 : public CommandObjectTargetModulesModuleAutoComplete { 2189 public: 2190 CommandObjectTargetModulesDumpClangAST(CommandInterpreter &interpreter) 2191 : CommandObjectTargetModulesModuleAutoComplete( 2192 interpreter, "target modules dump ast", 2193 "Dump the clang ast for a given module's symbol file.", 2194 //"target modules dump ast [<file1> ...]") 2195 nullptr, eCommandRequiresTarget) {} 2196 2197 ~CommandObjectTargetModulesDumpClangAST() override = default; 2198 2199 protected: 2200 bool DoExecute(Args &command, CommandReturnObject &result) override { 2201 Target *target = &GetSelectedTarget(); 2202 2203 const size_t num_modules = target->GetImages().GetSize(); 2204 if (num_modules == 0) { 2205 result.AppendError("the target has no associated executable images"); 2206 result.SetStatus(eReturnStatusFailed); 2207 return false; 2208 } 2209 2210 if (command.GetArgumentCount() == 0) { 2211 // Dump all ASTs for all modules images 2212 result.GetOutputStream().Printf("Dumping clang ast for %" PRIu64 2213 " modules.\n", 2214 (uint64_t)num_modules); 2215 for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { 2216 if (m_interpreter.WasInterrupted()) 2217 break; 2218 Module *m = target->GetImages().GetModulePointerAtIndex(image_idx); 2219 if (SymbolFile *sf = m->GetSymbolFile()) 2220 sf->DumpClangAST(result.GetOutputStream()); 2221 } 2222 result.SetStatus(eReturnStatusSuccessFinishResult); 2223 return true; 2224 } 2225 2226 // Dump specified ASTs (by basename or fullpath) 2227 for (const Args::ArgEntry &arg : command.entries()) { 2228 ModuleList module_list; 2229 const size_t num_matches = 2230 FindModulesByName(target, arg.c_str(), module_list, true); 2231 if (num_matches == 0) { 2232 // Check the global list 2233 std::lock_guard<std::recursive_mutex> guard( 2234 Module::GetAllocationModuleCollectionMutex()); 2235 2236 result.AppendWarningWithFormat( 2237 "Unable to find an image that matches '%s'.\n", arg.c_str()); 2238 continue; 2239 } 2240 2241 for (size_t i = 0; i < num_matches; ++i) { 2242 if (m_interpreter.WasInterrupted()) 2243 break; 2244 Module *m = module_list.GetModulePointerAtIndex(i); 2245 if (SymbolFile *sf = m->GetSymbolFile()) 2246 sf->DumpClangAST(result.GetOutputStream()); 2247 } 2248 } 2249 result.SetStatus(eReturnStatusSuccessFinishResult); 2250 return true; 2251 } 2252 }; 2253 2254 #pragma mark CommandObjectTargetModulesDumpSymfile 2255 2256 // Image debug symbol dumping command 2257 2258 class CommandObjectTargetModulesDumpSymfile 2259 : public CommandObjectTargetModulesModuleAutoComplete { 2260 public: 2261 CommandObjectTargetModulesDumpSymfile(CommandInterpreter &interpreter) 2262 : CommandObjectTargetModulesModuleAutoComplete( 2263 interpreter, "target modules dump symfile", 2264 "Dump the debug symbol file for one or more target modules.", 2265 //"target modules dump symfile [<file1> ...]") 2266 nullptr, eCommandRequiresTarget) {} 2267 2268 ~CommandObjectTargetModulesDumpSymfile() override = default; 2269 2270 protected: 2271 bool DoExecute(Args &command, CommandReturnObject &result) override { 2272 Target *target = &GetSelectedTarget(); 2273 uint32_t num_dumped = 0; 2274 2275 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 2276 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 2277 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 2278 2279 if (command.GetArgumentCount() == 0) { 2280 // Dump all sections for all modules images 2281 const ModuleList &target_modules = target->GetImages(); 2282 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); 2283 const size_t num_modules = target_modules.GetSize(); 2284 if (num_modules > 0) { 2285 result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 2286 " modules.\n", 2287 (uint64_t)num_modules); 2288 for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) { 2289 if (m_interpreter.WasInterrupted()) 2290 break; 2291 if (DumpModuleSymbolFile( 2292 result.GetOutputStream(), 2293 target_modules.GetModulePointerAtIndexUnlocked(image_idx))) 2294 num_dumped++; 2295 } 2296 } else { 2297 result.AppendError("the target has no associated executable images"); 2298 result.SetStatus(eReturnStatusFailed); 2299 return false; 2300 } 2301 } else { 2302 // Dump specified images (by basename or fullpath) 2303 const char *arg_cstr; 2304 for (int arg_idx = 0; 2305 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; 2306 ++arg_idx) { 2307 ModuleList module_list; 2308 const size_t num_matches = 2309 FindModulesByName(target, arg_cstr, module_list, true); 2310 if (num_matches > 0) { 2311 for (size_t i = 0; i < num_matches; ++i) { 2312 if (m_interpreter.WasInterrupted()) 2313 break; 2314 Module *module = module_list.GetModulePointerAtIndex(i); 2315 if (module) { 2316 if (DumpModuleSymbolFile(result.GetOutputStream(), module)) 2317 num_dumped++; 2318 } 2319 } 2320 } else 2321 result.AppendWarningWithFormat( 2322 "Unable to find an image that matches '%s'.\n", arg_cstr); 2323 } 2324 } 2325 2326 if (num_dumped > 0) 2327 result.SetStatus(eReturnStatusSuccessFinishResult); 2328 else { 2329 result.AppendError("no matching executable images found"); 2330 result.SetStatus(eReturnStatusFailed); 2331 } 2332 return result.Succeeded(); 2333 } 2334 }; 2335 2336 #pragma mark CommandObjectTargetModulesDumpLineTable 2337 #define LLDB_OPTIONS_target_modules_dump 2338 #include "CommandOptions.inc" 2339 2340 // Image debug line table dumping command 2341 2342 class CommandObjectTargetModulesDumpLineTable 2343 : public CommandObjectTargetModulesSourceFileAutoComplete { 2344 public: 2345 CommandObjectTargetModulesDumpLineTable(CommandInterpreter &interpreter) 2346 : CommandObjectTargetModulesSourceFileAutoComplete( 2347 interpreter, "target modules dump line-table", 2348 "Dump the line table for one or more compilation units.", nullptr, 2349 eCommandRequiresTarget) {} 2350 2351 ~CommandObjectTargetModulesDumpLineTable() override = default; 2352 2353 Options *GetOptions() override { return &m_options; } 2354 2355 protected: 2356 bool DoExecute(Args &command, CommandReturnObject &result) override { 2357 Target *target = m_exe_ctx.GetTargetPtr(); 2358 uint32_t total_num_dumped = 0; 2359 2360 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 2361 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 2362 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 2363 2364 if (command.GetArgumentCount() == 0) { 2365 result.AppendError("file option must be specified."); 2366 result.SetStatus(eReturnStatusFailed); 2367 return result.Succeeded(); 2368 } else { 2369 // Dump specified images (by basename or fullpath) 2370 const char *arg_cstr; 2371 for (int arg_idx = 0; 2372 (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != nullptr; 2373 ++arg_idx) { 2374 FileSpec file_spec(arg_cstr); 2375 2376 const ModuleList &target_modules = target->GetImages(); 2377 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); 2378 const size_t num_modules = target_modules.GetSize(); 2379 if (num_modules > 0) { 2380 uint32_t num_dumped = 0; 2381 for (uint32_t i = 0; i < num_modules; ++i) { 2382 if (m_interpreter.WasInterrupted()) 2383 break; 2384 if (DumpCompileUnitLineTable( 2385 m_interpreter, result.GetOutputStream(), 2386 target_modules.GetModulePointerAtIndexUnlocked(i), 2387 file_spec, 2388 m_options.m_verbose ? eDescriptionLevelFull 2389 : eDescriptionLevelBrief)) 2390 num_dumped++; 2391 } 2392 if (num_dumped == 0) 2393 result.AppendWarningWithFormat( 2394 "No source filenames matched '%s'.\n", arg_cstr); 2395 else 2396 total_num_dumped += num_dumped; 2397 } 2398 } 2399 } 2400 2401 if (total_num_dumped > 0) 2402 result.SetStatus(eReturnStatusSuccessFinishResult); 2403 else { 2404 result.AppendError("no source filenames matched any command arguments"); 2405 result.SetStatus(eReturnStatusFailed); 2406 } 2407 return result.Succeeded(); 2408 } 2409 2410 class CommandOptions : public Options { 2411 public: 2412 CommandOptions() : Options() { OptionParsingStarting(nullptr); } 2413 2414 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2415 ExecutionContext *execution_context) override { 2416 assert(option_idx == 0 && "We only have one option."); 2417 m_verbose = true; 2418 2419 return Status(); 2420 } 2421 2422 void OptionParsingStarting(ExecutionContext *execution_context) override { 2423 m_verbose = false; 2424 } 2425 2426 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2427 return llvm::makeArrayRef(g_target_modules_dump_options); 2428 } 2429 2430 bool m_verbose; 2431 }; 2432 2433 CommandOptions m_options; 2434 }; 2435 2436 #pragma mark CommandObjectTargetModulesDump 2437 2438 // Dump multi-word command for target modules 2439 2440 class CommandObjectTargetModulesDump : public CommandObjectMultiword { 2441 public: 2442 // Constructors and Destructors 2443 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) 2444 : CommandObjectMultiword( 2445 interpreter, "target modules dump", 2446 "Commands for dumping information about one or " 2447 "more target modules.", 2448 "target modules dump " 2449 "[headers|symtab|sections|ast|symfile|line-table] " 2450 "[<file1> <file2> ...]") { 2451 LoadSubCommand("objfile", 2452 CommandObjectSP( 2453 new CommandObjectTargetModulesDumpObjfile(interpreter))); 2454 LoadSubCommand( 2455 "symtab", 2456 CommandObjectSP(new CommandObjectTargetModulesDumpSymtab(interpreter))); 2457 LoadSubCommand("sections", 2458 CommandObjectSP(new CommandObjectTargetModulesDumpSections( 2459 interpreter))); 2460 LoadSubCommand("symfile", 2461 CommandObjectSP( 2462 new CommandObjectTargetModulesDumpSymfile(interpreter))); 2463 LoadSubCommand( 2464 "ast", CommandObjectSP( 2465 new CommandObjectTargetModulesDumpClangAST(interpreter))); 2466 LoadSubCommand("line-table", 2467 CommandObjectSP(new CommandObjectTargetModulesDumpLineTable( 2468 interpreter))); 2469 } 2470 2471 ~CommandObjectTargetModulesDump() override = default; 2472 }; 2473 2474 class CommandObjectTargetModulesAdd : public CommandObjectParsed { 2475 public: 2476 CommandObjectTargetModulesAdd(CommandInterpreter &interpreter) 2477 : CommandObjectParsed(interpreter, "target modules add", 2478 "Add a new module to the current target's modules.", 2479 "target modules add [<module>]", 2480 eCommandRequiresTarget), 2481 m_option_group(), m_symbol_file(LLDB_OPT_SET_1, false, "symfile", 's', 2482 0, eArgTypeFilename, 2483 "Fullpath to a stand alone debug " 2484 "symbols file for when debug symbols " 2485 "are not in the executable.") { 2486 m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL, 2487 LLDB_OPT_SET_1); 2488 m_option_group.Append(&m_symbol_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 2489 m_option_group.Finalize(); 2490 } 2491 2492 ~CommandObjectTargetModulesAdd() override = default; 2493 2494 Options *GetOptions() override { return &m_option_group; } 2495 2496 void 2497 HandleArgumentCompletion(CompletionRequest &request, 2498 OptionElementVector &opt_element_vector) override { 2499 CommandCompletions::InvokeCommonCompletionCallbacks( 2500 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, 2501 request, nullptr); 2502 } 2503 2504 protected: 2505 OptionGroupOptions m_option_group; 2506 OptionGroupUUID m_uuid_option_group; 2507 OptionGroupFile m_symbol_file; 2508 2509 bool DoExecute(Args &args, CommandReturnObject &result) override { 2510 Target *target = &GetSelectedTarget(); 2511 bool flush = false; 2512 2513 const size_t argc = args.GetArgumentCount(); 2514 if (argc == 0) { 2515 if (m_uuid_option_group.GetOptionValue().OptionWasSet()) { 2516 // We are given a UUID only, go locate the file 2517 ModuleSpec module_spec; 2518 module_spec.GetUUID() = 2519 m_uuid_option_group.GetOptionValue().GetCurrentValue(); 2520 if (m_symbol_file.GetOptionValue().OptionWasSet()) 2521 module_spec.GetSymbolFileSpec() = 2522 m_symbol_file.GetOptionValue().GetCurrentValue(); 2523 if (Symbols::DownloadObjectAndSymbolFile(module_spec)) { 2524 ModuleSP module_sp( 2525 target->GetOrCreateModule(module_spec, true /* notify */)); 2526 if (module_sp) { 2527 result.SetStatus(eReturnStatusSuccessFinishResult); 2528 return true; 2529 } else { 2530 StreamString strm; 2531 module_spec.GetUUID().Dump(&strm); 2532 if (module_spec.GetFileSpec()) { 2533 if (module_spec.GetSymbolFileSpec()) { 2534 result.AppendErrorWithFormat( 2535 "Unable to create the executable or symbol file with " 2536 "UUID %s with path %s and symbol file %s", 2537 strm.GetData(), module_spec.GetFileSpec().GetPath().c_str(), 2538 module_spec.GetSymbolFileSpec().GetPath().c_str()); 2539 } else { 2540 result.AppendErrorWithFormat( 2541 "Unable to create the executable or symbol file with " 2542 "UUID %s with path %s", 2543 strm.GetData(), 2544 module_spec.GetFileSpec().GetPath().c_str()); 2545 } 2546 } else { 2547 result.AppendErrorWithFormat("Unable to create the executable " 2548 "or symbol file with UUID %s", 2549 strm.GetData()); 2550 } 2551 result.SetStatus(eReturnStatusFailed); 2552 return false; 2553 } 2554 } else { 2555 StreamString strm; 2556 module_spec.GetUUID().Dump(&strm); 2557 result.AppendErrorWithFormat( 2558 "Unable to locate the executable or symbol file with UUID %s", 2559 strm.GetData()); 2560 result.SetStatus(eReturnStatusFailed); 2561 return false; 2562 } 2563 } else { 2564 result.AppendError( 2565 "one or more executable image paths must be specified"); 2566 result.SetStatus(eReturnStatusFailed); 2567 return false; 2568 } 2569 } else { 2570 for (auto &entry : args.entries()) { 2571 if (entry.ref().empty()) 2572 continue; 2573 2574 FileSpec file_spec(entry.ref()); 2575 if (FileSystem::Instance().Exists(file_spec)) { 2576 ModuleSpec module_spec(file_spec); 2577 if (m_uuid_option_group.GetOptionValue().OptionWasSet()) 2578 module_spec.GetUUID() = 2579 m_uuid_option_group.GetOptionValue().GetCurrentValue(); 2580 if (m_symbol_file.GetOptionValue().OptionWasSet()) 2581 module_spec.GetSymbolFileSpec() = 2582 m_symbol_file.GetOptionValue().GetCurrentValue(); 2583 if (!module_spec.GetArchitecture().IsValid()) 2584 module_spec.GetArchitecture() = target->GetArchitecture(); 2585 Status error; 2586 ModuleSP module_sp(target->GetOrCreateModule( 2587 module_spec, true /* notify */, &error)); 2588 if (!module_sp) { 2589 const char *error_cstr = error.AsCString(); 2590 if (error_cstr) 2591 result.AppendError(error_cstr); 2592 else 2593 result.AppendErrorWithFormat("unsupported module: %s", 2594 entry.c_str()); 2595 result.SetStatus(eReturnStatusFailed); 2596 return false; 2597 } else { 2598 flush = true; 2599 } 2600 result.SetStatus(eReturnStatusSuccessFinishResult); 2601 } else { 2602 std::string resolved_path = file_spec.GetPath(); 2603 result.SetStatus(eReturnStatusFailed); 2604 if (resolved_path != entry.ref()) { 2605 result.AppendErrorWithFormat( 2606 "invalid module path '%s' with resolved path '%s'\n", 2607 entry.ref().str().c_str(), resolved_path.c_str()); 2608 break; 2609 } 2610 result.AppendErrorWithFormat("invalid module path '%s'\n", 2611 entry.c_str()); 2612 break; 2613 } 2614 } 2615 } 2616 2617 if (flush) { 2618 ProcessSP process = target->GetProcessSP(); 2619 if (process) 2620 process->Flush(); 2621 } 2622 2623 return result.Succeeded(); 2624 } 2625 }; 2626 2627 class CommandObjectTargetModulesLoad 2628 : public CommandObjectTargetModulesModuleAutoComplete { 2629 public: 2630 CommandObjectTargetModulesLoad(CommandInterpreter &interpreter) 2631 : CommandObjectTargetModulesModuleAutoComplete( 2632 interpreter, "target modules load", 2633 "Set the load addresses for one or more sections in a target " 2634 "module.", 2635 "target modules load [--file <module> --uuid <uuid>] <sect-name> " 2636 "<address> [<sect-name> <address> ....]", 2637 eCommandRequiresTarget), 2638 m_option_group(), 2639 m_file_option(LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeName, 2640 "Fullpath or basename for module to load.", ""), 2641 m_load_option(LLDB_OPT_SET_1, false, "load", 'l', 2642 "Write file contents to the memory.", false, true), 2643 m_pc_option(LLDB_OPT_SET_1, false, "set-pc-to-entry", 'p', 2644 "Set PC to the entry point." 2645 " Only applicable with '--load' option.", 2646 false, true), 2647 m_slide_option(LLDB_OPT_SET_1, false, "slide", 's', 0, eArgTypeOffset, 2648 "Set the load address for all sections to be the " 2649 "virtual address in the file plus the offset.", 2650 0) { 2651 m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL, 2652 LLDB_OPT_SET_1); 2653 m_option_group.Append(&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 2654 m_option_group.Append(&m_load_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 2655 m_option_group.Append(&m_pc_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 2656 m_option_group.Append(&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 2657 m_option_group.Finalize(); 2658 } 2659 2660 ~CommandObjectTargetModulesLoad() override = default; 2661 2662 Options *GetOptions() override { return &m_option_group; } 2663 2664 protected: 2665 bool DoExecute(Args &args, CommandReturnObject &result) override { 2666 Target *target = &GetSelectedTarget(); 2667 const bool load = m_load_option.GetOptionValue().GetCurrentValue(); 2668 const bool set_pc = m_pc_option.GetOptionValue().GetCurrentValue(); 2669 2670 const size_t argc = args.GetArgumentCount(); 2671 ModuleSpec module_spec; 2672 bool search_using_module_spec = false; 2673 2674 // Allow "load" option to work without --file or --uuid option. 2675 if (load) { 2676 if (!m_file_option.GetOptionValue().OptionWasSet() && 2677 !m_uuid_option_group.GetOptionValue().OptionWasSet()) { 2678 ModuleList &module_list = target->GetImages(); 2679 if (module_list.GetSize() == 1) { 2680 search_using_module_spec = true; 2681 module_spec.GetFileSpec() = 2682 module_list.GetModuleAtIndex(0)->GetFileSpec(); 2683 } 2684 } 2685 } 2686 2687 if (m_file_option.GetOptionValue().OptionWasSet()) { 2688 search_using_module_spec = true; 2689 const char *arg_cstr = m_file_option.GetOptionValue().GetCurrentValue(); 2690 const bool use_global_module_list = true; 2691 ModuleList module_list; 2692 const size_t num_matches = FindModulesByName( 2693 target, arg_cstr, module_list, use_global_module_list); 2694 if (num_matches == 1) { 2695 module_spec.GetFileSpec() = 2696 module_list.GetModuleAtIndex(0)->GetFileSpec(); 2697 } else if (num_matches > 1) { 2698 search_using_module_spec = false; 2699 result.AppendErrorWithFormat( 2700 "more than 1 module matched by name '%s'\n", arg_cstr); 2701 result.SetStatus(eReturnStatusFailed); 2702 } else { 2703 search_using_module_spec = false; 2704 result.AppendErrorWithFormat("no object file for module '%s'\n", 2705 arg_cstr); 2706 result.SetStatus(eReturnStatusFailed); 2707 } 2708 } 2709 2710 if (m_uuid_option_group.GetOptionValue().OptionWasSet()) { 2711 search_using_module_spec = true; 2712 module_spec.GetUUID() = 2713 m_uuid_option_group.GetOptionValue().GetCurrentValue(); 2714 } 2715 2716 if (search_using_module_spec) { 2717 ModuleList matching_modules; 2718 target->GetImages().FindModules(module_spec, matching_modules); 2719 const size_t num_matches = matching_modules.GetSize(); 2720 2721 char path[PATH_MAX]; 2722 if (num_matches == 1) { 2723 Module *module = matching_modules.GetModulePointerAtIndex(0); 2724 if (module) { 2725 ObjectFile *objfile = module->GetObjectFile(); 2726 if (objfile) { 2727 SectionList *section_list = module->GetSectionList(); 2728 if (section_list) { 2729 bool changed = false; 2730 if (argc == 0) { 2731 if (m_slide_option.GetOptionValue().OptionWasSet()) { 2732 const addr_t slide = 2733 m_slide_option.GetOptionValue().GetCurrentValue(); 2734 const bool slide_is_offset = true; 2735 module->SetLoadAddress(*target, slide, slide_is_offset, 2736 changed); 2737 } else { 2738 result.AppendError("one or more section name + load " 2739 "address pair must be specified"); 2740 result.SetStatus(eReturnStatusFailed); 2741 return false; 2742 } 2743 } else { 2744 if (m_slide_option.GetOptionValue().OptionWasSet()) { 2745 result.AppendError("The \"--slide <offset>\" option can't " 2746 "be used in conjunction with setting " 2747 "section load addresses.\n"); 2748 result.SetStatus(eReturnStatusFailed); 2749 return false; 2750 } 2751 2752 for (size_t i = 0; i < argc; i += 2) { 2753 const char *sect_name = args.GetArgumentAtIndex(i); 2754 const char *load_addr_cstr = args.GetArgumentAtIndex(i + 1); 2755 if (sect_name && load_addr_cstr) { 2756 ConstString const_sect_name(sect_name); 2757 addr_t load_addr; 2758 if (llvm::to_integer(load_addr_cstr, load_addr)) { 2759 SectionSP section_sp( 2760 section_list->FindSectionByName(const_sect_name)); 2761 if (section_sp) { 2762 if (section_sp->IsThreadSpecific()) { 2763 result.AppendErrorWithFormat( 2764 "thread specific sections are not yet " 2765 "supported (section '%s')\n", 2766 sect_name); 2767 result.SetStatus(eReturnStatusFailed); 2768 break; 2769 } else { 2770 if (target->GetSectionLoadList() 2771 .SetSectionLoadAddress(section_sp, load_addr)) 2772 changed = true; 2773 result.AppendMessageWithFormat( 2774 "section '%s' loaded at 0x%" PRIx64 "\n", 2775 sect_name, load_addr); 2776 } 2777 } else { 2778 result.AppendErrorWithFormat("no section found that " 2779 "matches the section " 2780 "name '%s'\n", 2781 sect_name); 2782 result.SetStatus(eReturnStatusFailed); 2783 break; 2784 } 2785 } else { 2786 result.AppendErrorWithFormat( 2787 "invalid load address string '%s'\n", load_addr_cstr); 2788 result.SetStatus(eReturnStatusFailed); 2789 break; 2790 } 2791 } else { 2792 if (sect_name) 2793 result.AppendError("section names must be followed by " 2794 "a load address.\n"); 2795 else 2796 result.AppendError("one or more section name + load " 2797 "address pair must be specified.\n"); 2798 result.SetStatus(eReturnStatusFailed); 2799 break; 2800 } 2801 } 2802 } 2803 2804 if (changed) { 2805 target->ModulesDidLoad(matching_modules); 2806 Process *process = m_exe_ctx.GetProcessPtr(); 2807 if (process) 2808 process->Flush(); 2809 } 2810 if (load) { 2811 ProcessSP process = target->CalculateProcess(); 2812 Address file_entry = objfile->GetEntryPointAddress(); 2813 if (!process) { 2814 result.AppendError("No process"); 2815 return false; 2816 } 2817 if (set_pc && !file_entry.IsValid()) { 2818 result.AppendError("No entry address in object file"); 2819 return false; 2820 } 2821 std::vector<ObjectFile::LoadableData> loadables( 2822 objfile->GetLoadableData(*target)); 2823 if (loadables.size() == 0) { 2824 result.AppendError("No loadable sections"); 2825 return false; 2826 } 2827 Status error = process->WriteObjectFile(std::move(loadables)); 2828 if (error.Fail()) { 2829 result.AppendError(error.AsCString()); 2830 return false; 2831 } 2832 if (set_pc) { 2833 ThreadList &thread_list = process->GetThreadList(); 2834 RegisterContextSP reg_context( 2835 thread_list.GetSelectedThread()->GetRegisterContext()); 2836 addr_t file_entry_addr = file_entry.GetLoadAddress(target); 2837 if (!reg_context->SetPC(file_entry_addr)) { 2838 result.AppendErrorWithFormat("failed to set PC value to " 2839 "0x%" PRIx64 "\n", 2840 file_entry_addr); 2841 result.SetStatus(eReturnStatusFailed); 2842 } 2843 } 2844 } 2845 } else { 2846 module->GetFileSpec().GetPath(path, sizeof(path)); 2847 result.AppendErrorWithFormat("no sections in object file '%s'\n", 2848 path); 2849 result.SetStatus(eReturnStatusFailed); 2850 } 2851 } else { 2852 module->GetFileSpec().GetPath(path, sizeof(path)); 2853 result.AppendErrorWithFormat("no object file for module '%s'\n", 2854 path); 2855 result.SetStatus(eReturnStatusFailed); 2856 } 2857 } else { 2858 FileSpec *module_spec_file = module_spec.GetFileSpecPtr(); 2859 if (module_spec_file) { 2860 module_spec_file->GetPath(path, sizeof(path)); 2861 result.AppendErrorWithFormat("invalid module '%s'.\n", path); 2862 } else 2863 result.AppendError("no module spec"); 2864 result.SetStatus(eReturnStatusFailed); 2865 } 2866 } else { 2867 std::string uuid_str; 2868 2869 if (module_spec.GetFileSpec()) 2870 module_spec.GetFileSpec().GetPath(path, sizeof(path)); 2871 else 2872 path[0] = '\0'; 2873 2874 if (module_spec.GetUUIDPtr()) 2875 uuid_str = module_spec.GetUUID().GetAsString(); 2876 if (num_matches > 1) { 2877 result.AppendErrorWithFormat( 2878 "multiple modules match%s%s%s%s:\n", path[0] ? " file=" : "", 2879 path, !uuid_str.empty() ? " uuid=" : "", uuid_str.c_str()); 2880 for (size_t i = 0; i < num_matches; ++i) { 2881 if (matching_modules.GetModulePointerAtIndex(i) 2882 ->GetFileSpec() 2883 .GetPath(path, sizeof(path))) 2884 result.AppendMessageWithFormat("%s\n", path); 2885 } 2886 } else { 2887 result.AppendErrorWithFormat( 2888 "no modules were found that match%s%s%s%s.\n", 2889 path[0] ? " file=" : "", path, !uuid_str.empty() ? " uuid=" : "", 2890 uuid_str.c_str()); 2891 } 2892 result.SetStatus(eReturnStatusFailed); 2893 } 2894 } else { 2895 result.AppendError("either the \"--file <module>\" or the \"--uuid " 2896 "<uuid>\" option must be specified.\n"); 2897 result.SetStatus(eReturnStatusFailed); 2898 return false; 2899 } 2900 return result.Succeeded(); 2901 } 2902 2903 OptionGroupOptions m_option_group; 2904 OptionGroupUUID m_uuid_option_group; 2905 OptionGroupString m_file_option; 2906 OptionGroupBoolean m_load_option; 2907 OptionGroupBoolean m_pc_option; 2908 OptionGroupUInt64 m_slide_option; 2909 }; 2910 2911 // List images with associated information 2912 #define LLDB_OPTIONS_target_modules_list 2913 #include "CommandOptions.inc" 2914 2915 class CommandObjectTargetModulesList : public CommandObjectParsed { 2916 public: 2917 class CommandOptions : public Options { 2918 public: 2919 CommandOptions() 2920 : Options(), m_format_array(), m_use_global_module_list(false), 2921 m_module_addr(LLDB_INVALID_ADDRESS) {} 2922 2923 ~CommandOptions() override = default; 2924 2925 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 2926 ExecutionContext *execution_context) override { 2927 Status error; 2928 2929 const int short_option = m_getopt_table[option_idx].val; 2930 if (short_option == 'g') { 2931 m_use_global_module_list = true; 2932 } else if (short_option == 'a') { 2933 m_module_addr = OptionArgParser::ToAddress( 2934 execution_context, option_arg, LLDB_INVALID_ADDRESS, &error); 2935 } else { 2936 unsigned long width = 0; 2937 option_arg.getAsInteger(0, width); 2938 m_format_array.push_back(std::make_pair(short_option, width)); 2939 } 2940 return error; 2941 } 2942 2943 void OptionParsingStarting(ExecutionContext *execution_context) override { 2944 m_format_array.clear(); 2945 m_use_global_module_list = false; 2946 m_module_addr = LLDB_INVALID_ADDRESS; 2947 } 2948 2949 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 2950 return llvm::makeArrayRef(g_target_modules_list_options); 2951 } 2952 2953 // Instance variables to hold the values for command options. 2954 typedef std::vector<std::pair<char, uint32_t>> FormatWidthCollection; 2955 FormatWidthCollection m_format_array; 2956 bool m_use_global_module_list; 2957 lldb::addr_t m_module_addr; 2958 }; 2959 2960 CommandObjectTargetModulesList(CommandInterpreter &interpreter) 2961 : CommandObjectParsed( 2962 interpreter, "target modules list", 2963 "List current executable and dependent shared library images.", 2964 "target modules list [<cmd-options>]"), 2965 m_options() {} 2966 2967 ~CommandObjectTargetModulesList() override = default; 2968 2969 Options *GetOptions() override { return &m_options; } 2970 2971 protected: 2972 bool DoExecute(Args &command, CommandReturnObject &result) override { 2973 Target *target = GetDebugger().GetSelectedTarget().get(); 2974 const bool use_global_module_list = m_options.m_use_global_module_list; 2975 // Define a local module list here to ensure it lives longer than any 2976 // "locker" object which might lock its contents below (through the 2977 // "module_list_ptr" variable). 2978 ModuleList module_list; 2979 if (target == nullptr && !use_global_module_list) { 2980 result.AppendError("invalid target, create a debug target using the " 2981 "'target create' command"); 2982 result.SetStatus(eReturnStatusFailed); 2983 return false; 2984 } else { 2985 if (target) { 2986 uint32_t addr_byte_size = 2987 target->GetArchitecture().GetAddressByteSize(); 2988 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 2989 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 2990 } 2991 // Dump all sections for all modules images 2992 Stream &strm = result.GetOutputStream(); 2993 2994 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS) { 2995 if (target) { 2996 Address module_address; 2997 if (module_address.SetLoadAddress(m_options.m_module_addr, target)) { 2998 ModuleSP module_sp(module_address.GetModule()); 2999 if (module_sp) { 3000 PrintModule(target, module_sp.get(), 0, strm); 3001 result.SetStatus(eReturnStatusSuccessFinishResult); 3002 } else { 3003 result.AppendErrorWithFormat( 3004 "Couldn't find module matching address: 0x%" PRIx64 ".", 3005 m_options.m_module_addr); 3006 result.SetStatus(eReturnStatusFailed); 3007 } 3008 } else { 3009 result.AppendErrorWithFormat( 3010 "Couldn't find module containing address: 0x%" PRIx64 ".", 3011 m_options.m_module_addr); 3012 result.SetStatus(eReturnStatusFailed); 3013 } 3014 } else { 3015 result.AppendError( 3016 "Can only look up modules by address with a valid target."); 3017 result.SetStatus(eReturnStatusFailed); 3018 } 3019 return result.Succeeded(); 3020 } 3021 3022 size_t num_modules = 0; 3023 3024 // This locker will be locked on the mutex in module_list_ptr if it is 3025 // non-nullptr. Otherwise it will lock the 3026 // AllocationModuleCollectionMutex when accessing the global module list 3027 // directly. 3028 std::unique_lock<std::recursive_mutex> guard( 3029 Module::GetAllocationModuleCollectionMutex(), std::defer_lock); 3030 3031 const ModuleList *module_list_ptr = nullptr; 3032 const size_t argc = command.GetArgumentCount(); 3033 if (argc == 0) { 3034 if (use_global_module_list) { 3035 guard.lock(); 3036 num_modules = Module::GetNumberAllocatedModules(); 3037 } else { 3038 module_list_ptr = &target->GetImages(); 3039 } 3040 } else { 3041 for (const Args::ArgEntry &arg : command) { 3042 // Dump specified images (by basename or fullpath) 3043 const size_t num_matches = FindModulesByName( 3044 target, arg.c_str(), module_list, use_global_module_list); 3045 if (num_matches == 0) { 3046 if (argc == 1) { 3047 result.AppendErrorWithFormat("no modules found that match '%s'", 3048 arg.c_str()); 3049 result.SetStatus(eReturnStatusFailed); 3050 return false; 3051 } 3052 } 3053 } 3054 3055 module_list_ptr = &module_list; 3056 } 3057 3058 std::unique_lock<std::recursive_mutex> lock; 3059 if (module_list_ptr != nullptr) { 3060 lock = 3061 std::unique_lock<std::recursive_mutex>(module_list_ptr->GetMutex()); 3062 3063 num_modules = module_list_ptr->GetSize(); 3064 } 3065 3066 if (num_modules > 0) { 3067 for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) { 3068 ModuleSP module_sp; 3069 Module *module; 3070 if (module_list_ptr) { 3071 module_sp = module_list_ptr->GetModuleAtIndexUnlocked(image_idx); 3072 module = module_sp.get(); 3073 } else { 3074 module = Module::GetAllocatedModuleAtIndex(image_idx); 3075 module_sp = module->shared_from_this(); 3076 } 3077 3078 const size_t indent = strm.Printf("[%3u] ", image_idx); 3079 PrintModule(target, module, indent, strm); 3080 } 3081 result.SetStatus(eReturnStatusSuccessFinishResult); 3082 } else { 3083 if (argc) { 3084 if (use_global_module_list) 3085 result.AppendError( 3086 "the global module list has no matching modules"); 3087 else 3088 result.AppendError("the target has no matching modules"); 3089 } else { 3090 if (use_global_module_list) 3091 result.AppendError("the global module list is empty"); 3092 else 3093 result.AppendError( 3094 "the target has no associated executable images"); 3095 } 3096 result.SetStatus(eReturnStatusFailed); 3097 return false; 3098 } 3099 } 3100 return result.Succeeded(); 3101 } 3102 3103 void PrintModule(Target *target, Module *module, int indent, Stream &strm) { 3104 if (module == nullptr) { 3105 strm.PutCString("Null module"); 3106 return; 3107 } 3108 3109 bool dump_object_name = false; 3110 if (m_options.m_format_array.empty()) { 3111 m_options.m_format_array.push_back(std::make_pair('u', 0)); 3112 m_options.m_format_array.push_back(std::make_pair('h', 0)); 3113 m_options.m_format_array.push_back(std::make_pair('f', 0)); 3114 m_options.m_format_array.push_back(std::make_pair('S', 0)); 3115 } 3116 const size_t num_entries = m_options.m_format_array.size(); 3117 bool print_space = false; 3118 for (size_t i = 0; i < num_entries; ++i) { 3119 if (print_space) 3120 strm.PutChar(' '); 3121 print_space = true; 3122 const char format_char = m_options.m_format_array[i].first; 3123 uint32_t width = m_options.m_format_array[i].second; 3124 switch (format_char) { 3125 case 'A': 3126 DumpModuleArchitecture(strm, module, false, width); 3127 break; 3128 3129 case 't': 3130 DumpModuleArchitecture(strm, module, true, width); 3131 break; 3132 3133 case 'f': 3134 DumpFullpath(strm, &module->GetFileSpec(), width); 3135 dump_object_name = true; 3136 break; 3137 3138 case 'd': 3139 DumpDirectory(strm, &module->GetFileSpec(), width); 3140 break; 3141 3142 case 'b': 3143 DumpBasename(strm, &module->GetFileSpec(), width); 3144 dump_object_name = true; 3145 break; 3146 3147 case 'h': 3148 case 'o': 3149 // Image header address 3150 { 3151 uint32_t addr_nibble_width = 3152 target ? (target->GetArchitecture().GetAddressByteSize() * 2) 3153 : 16; 3154 3155 ObjectFile *objfile = module->GetObjectFile(); 3156 if (objfile) { 3157 Address base_addr(objfile->GetBaseAddress()); 3158 if (base_addr.IsValid()) { 3159 if (target && !target->GetSectionLoadList().IsEmpty()) { 3160 lldb::addr_t load_addr = base_addr.GetLoadAddress(target); 3161 if (load_addr == LLDB_INVALID_ADDRESS) { 3162 base_addr.Dump(&strm, target, 3163 Address::DumpStyleModuleWithFileAddress, 3164 Address::DumpStyleFileAddress); 3165 } else { 3166 if (format_char == 'o') { 3167 // Show the offset of slide for the image 3168 strm.Printf("0x%*.*" PRIx64, addr_nibble_width, 3169 addr_nibble_width, 3170 load_addr - base_addr.GetFileAddress()); 3171 } else { 3172 // Show the load address of the image 3173 strm.Printf("0x%*.*" PRIx64, addr_nibble_width, 3174 addr_nibble_width, load_addr); 3175 } 3176 } 3177 break; 3178 } 3179 // The address was valid, but the image isn't loaded, output the 3180 // address in an appropriate format 3181 base_addr.Dump(&strm, target, Address::DumpStyleFileAddress); 3182 break; 3183 } 3184 } 3185 strm.Printf("%*s", addr_nibble_width + 2, ""); 3186 } 3187 break; 3188 3189 case 'r': { 3190 size_t ref_count = 0; 3191 ModuleSP module_sp(module->shared_from_this()); 3192 if (module_sp) { 3193 // Take one away to make sure we don't count our local "module_sp" 3194 ref_count = module_sp.use_count() - 1; 3195 } 3196 if (width) 3197 strm.Printf("{%*" PRIu64 "}", width, (uint64_t)ref_count); 3198 else 3199 strm.Printf("{%" PRIu64 "}", (uint64_t)ref_count); 3200 } break; 3201 3202 case 's': 3203 case 'S': { 3204 if (const SymbolFile *symbol_file = module->GetSymbolFile()) { 3205 const FileSpec symfile_spec = 3206 symbol_file->GetObjectFile()->GetFileSpec(); 3207 if (format_char == 'S') { 3208 // Dump symbol file only if different from module file 3209 if (!symfile_spec || symfile_spec == module->GetFileSpec()) { 3210 print_space = false; 3211 break; 3212 } 3213 // Add a newline and indent past the index 3214 strm.Printf("\n%*s", indent, ""); 3215 } 3216 DumpFullpath(strm, &symfile_spec, width); 3217 dump_object_name = true; 3218 break; 3219 } 3220 strm.Printf("%.*s", width, "<NONE>"); 3221 } break; 3222 3223 case 'm': 3224 strm.Format("{0:%c}", llvm::fmt_align(module->GetModificationTime(), 3225 llvm::AlignStyle::Left, width)); 3226 break; 3227 3228 case 'p': 3229 strm.Printf("%p", static_cast<void *>(module)); 3230 break; 3231 3232 case 'u': 3233 DumpModuleUUID(strm, module); 3234 break; 3235 3236 default: 3237 break; 3238 } 3239 } 3240 if (dump_object_name) { 3241 const char *object_name = module->GetObjectName().GetCString(); 3242 if (object_name) 3243 strm.Printf("(%s)", object_name); 3244 } 3245 strm.EOL(); 3246 } 3247 3248 CommandOptions m_options; 3249 }; 3250 3251 #pragma mark CommandObjectTargetModulesShowUnwind 3252 3253 // Lookup unwind information in images 3254 #define LLDB_OPTIONS_target_modules_show_unwind 3255 #include "CommandOptions.inc" 3256 3257 class CommandObjectTargetModulesShowUnwind : public CommandObjectParsed { 3258 public: 3259 enum { 3260 eLookupTypeInvalid = -1, 3261 eLookupTypeAddress = 0, 3262 eLookupTypeSymbol, 3263 eLookupTypeFunction, 3264 eLookupTypeFunctionOrSymbol, 3265 kNumLookupTypes 3266 }; 3267 3268 class CommandOptions : public Options { 3269 public: 3270 CommandOptions() 3271 : Options(), m_type(eLookupTypeInvalid), m_str(), 3272 m_addr(LLDB_INVALID_ADDRESS) {} 3273 3274 ~CommandOptions() override = default; 3275 3276 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 3277 ExecutionContext *execution_context) override { 3278 Status error; 3279 3280 const int short_option = m_getopt_table[option_idx].val; 3281 3282 switch (short_option) { 3283 case 'a': { 3284 m_str = std::string(option_arg); 3285 m_type = eLookupTypeAddress; 3286 m_addr = OptionArgParser::ToAddress(execution_context, option_arg, 3287 LLDB_INVALID_ADDRESS, &error); 3288 if (m_addr == LLDB_INVALID_ADDRESS) 3289 error.SetErrorStringWithFormat("invalid address string '%s'", 3290 option_arg.str().c_str()); 3291 break; 3292 } 3293 3294 case 'n': 3295 m_str = std::string(option_arg); 3296 m_type = eLookupTypeFunctionOrSymbol; 3297 break; 3298 3299 default: 3300 llvm_unreachable("Unimplemented option"); 3301 } 3302 3303 return error; 3304 } 3305 3306 void OptionParsingStarting(ExecutionContext *execution_context) override { 3307 m_type = eLookupTypeInvalid; 3308 m_str.clear(); 3309 m_addr = LLDB_INVALID_ADDRESS; 3310 } 3311 3312 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 3313 return llvm::makeArrayRef(g_target_modules_show_unwind_options); 3314 } 3315 3316 // Instance variables to hold the values for command options. 3317 3318 int m_type; // Should be a eLookupTypeXXX enum after parsing options 3319 std::string m_str; // Holds name lookup 3320 lldb::addr_t m_addr; // Holds the address to lookup 3321 }; 3322 3323 CommandObjectTargetModulesShowUnwind(CommandInterpreter &interpreter) 3324 : CommandObjectParsed( 3325 interpreter, "target modules show-unwind", 3326 "Show synthesized unwind instructions for a function.", nullptr, 3327 eCommandRequiresTarget | eCommandRequiresProcess | 3328 eCommandProcessMustBeLaunched | eCommandProcessMustBePaused), 3329 m_options() {} 3330 3331 ~CommandObjectTargetModulesShowUnwind() override = default; 3332 3333 Options *GetOptions() override { return &m_options; } 3334 3335 protected: 3336 bool DoExecute(Args &command, CommandReturnObject &result) override { 3337 Target *target = m_exe_ctx.GetTargetPtr(); 3338 Process *process = m_exe_ctx.GetProcessPtr(); 3339 ABI *abi = nullptr; 3340 if (process) 3341 abi = process->GetABI().get(); 3342 3343 if (process == nullptr) { 3344 result.AppendError( 3345 "You must have a process running to use this command."); 3346 result.SetStatus(eReturnStatusFailed); 3347 return false; 3348 } 3349 3350 ThreadList threads(process->GetThreadList()); 3351 if (threads.GetSize() == 0) { 3352 result.AppendError("The process must be paused to use this command."); 3353 result.SetStatus(eReturnStatusFailed); 3354 return false; 3355 } 3356 3357 ThreadSP thread(threads.GetThreadAtIndex(0)); 3358 if (!thread) { 3359 result.AppendError("The process must be paused to use this command."); 3360 result.SetStatus(eReturnStatusFailed); 3361 return false; 3362 } 3363 3364 SymbolContextList sc_list; 3365 3366 if (m_options.m_type == eLookupTypeFunctionOrSymbol) { 3367 ConstString function_name(m_options.m_str.c_str()); 3368 target->GetImages().FindFunctions(function_name, eFunctionNameTypeAuto, 3369 true, false, sc_list); 3370 } else if (m_options.m_type == eLookupTypeAddress && target) { 3371 Address addr; 3372 if (target->GetSectionLoadList().ResolveLoadAddress(m_options.m_addr, 3373 addr)) { 3374 SymbolContext sc; 3375 ModuleSP module_sp(addr.GetModule()); 3376 module_sp->ResolveSymbolContextForAddress(addr, 3377 eSymbolContextEverything, sc); 3378 if (sc.function || sc.symbol) { 3379 sc_list.Append(sc); 3380 } 3381 } 3382 } else { 3383 result.AppendError( 3384 "address-expression or function name option must be specified."); 3385 result.SetStatus(eReturnStatusFailed); 3386 return false; 3387 } 3388 3389 size_t num_matches = sc_list.GetSize(); 3390 if (num_matches == 0) { 3391 result.AppendErrorWithFormat("no unwind data found that matches '%s'.", 3392 m_options.m_str.c_str()); 3393 result.SetStatus(eReturnStatusFailed); 3394 return false; 3395 } 3396 3397 for (uint32_t idx = 0; idx < num_matches; idx++) { 3398 SymbolContext sc; 3399 sc_list.GetContextAtIndex(idx, sc); 3400 if (sc.symbol == nullptr && sc.function == nullptr) 3401 continue; 3402 if (!sc.module_sp || sc.module_sp->GetObjectFile() == nullptr) 3403 continue; 3404 AddressRange range; 3405 if (!sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, 0, 3406 false, range)) 3407 continue; 3408 if (!range.GetBaseAddress().IsValid()) 3409 continue; 3410 ConstString funcname(sc.GetFunctionName()); 3411 if (funcname.IsEmpty()) 3412 continue; 3413 addr_t start_addr = range.GetBaseAddress().GetLoadAddress(target); 3414 if (abi) 3415 start_addr = abi->FixCodeAddress(start_addr); 3416 3417 FuncUnwindersSP func_unwinders_sp( 3418 sc.module_sp->GetUnwindTable() 3419 .GetUncachedFuncUnwindersContainingAddress(start_addr, sc)); 3420 if (!func_unwinders_sp) 3421 continue; 3422 3423 result.GetOutputStream().Printf( 3424 "UNWIND PLANS for %s`%s (start addr 0x%" PRIx64 ")\n", 3425 sc.module_sp->GetPlatformFileSpec().GetFilename().AsCString(), 3426 funcname.AsCString(), start_addr); 3427 3428 Args args; 3429 target->GetUserSpecifiedTrapHandlerNames(args); 3430 size_t count = args.GetArgumentCount(); 3431 for (size_t i = 0; i < count; i++) { 3432 const char *trap_func_name = args.GetArgumentAtIndex(i); 3433 if (strcmp(funcname.GetCString(), trap_func_name) == 0) 3434 result.GetOutputStream().Printf( 3435 "This function is " 3436 "treated as a trap handler function via user setting.\n"); 3437 } 3438 PlatformSP platform_sp(target->GetPlatform()); 3439 if (platform_sp) { 3440 const std::vector<ConstString> trap_handler_names( 3441 platform_sp->GetTrapHandlerSymbolNames()); 3442 for (ConstString trap_name : trap_handler_names) { 3443 if (trap_name == funcname) { 3444 result.GetOutputStream().Printf( 3445 "This function's " 3446 "name is listed by the platform as a trap handler.\n"); 3447 } 3448 } 3449 } 3450 3451 result.GetOutputStream().Printf("\n"); 3452 3453 UnwindPlanSP non_callsite_unwind_plan = 3454 func_unwinders_sp->GetUnwindPlanAtNonCallSite(*target, *thread); 3455 if (non_callsite_unwind_plan) { 3456 result.GetOutputStream().Printf( 3457 "Asynchronous (not restricted to call-sites) UnwindPlan is '%s'\n", 3458 non_callsite_unwind_plan->GetSourceName().AsCString()); 3459 } 3460 UnwindPlanSP callsite_unwind_plan = 3461 func_unwinders_sp->GetUnwindPlanAtCallSite(*target, *thread); 3462 if (callsite_unwind_plan) { 3463 result.GetOutputStream().Printf( 3464 "Synchronous (restricted to call-sites) UnwindPlan is '%s'\n", 3465 callsite_unwind_plan->GetSourceName().AsCString()); 3466 } 3467 UnwindPlanSP fast_unwind_plan = 3468 func_unwinders_sp->GetUnwindPlanFastUnwind(*target, *thread); 3469 if (fast_unwind_plan) { 3470 result.GetOutputStream().Printf( 3471 "Fast UnwindPlan is '%s'\n", 3472 fast_unwind_plan->GetSourceName().AsCString()); 3473 } 3474 3475 result.GetOutputStream().Printf("\n"); 3476 3477 UnwindPlanSP assembly_sp = 3478 func_unwinders_sp->GetAssemblyUnwindPlan(*target, *thread); 3479 if (assembly_sp) { 3480 result.GetOutputStream().Printf( 3481 "Assembly language inspection UnwindPlan:\n"); 3482 assembly_sp->Dump(result.GetOutputStream(), thread.get(), 3483 LLDB_INVALID_ADDRESS); 3484 result.GetOutputStream().Printf("\n"); 3485 } 3486 3487 UnwindPlanSP of_unwind_sp = 3488 func_unwinders_sp->GetObjectFileUnwindPlan(*target); 3489 if (of_unwind_sp) { 3490 result.GetOutputStream().Printf("object file UnwindPlan:\n"); 3491 of_unwind_sp->Dump(result.GetOutputStream(), thread.get(), 3492 LLDB_INVALID_ADDRESS); 3493 result.GetOutputStream().Printf("\n"); 3494 } 3495 3496 UnwindPlanSP of_unwind_augmented_sp = 3497 func_unwinders_sp->GetObjectFileAugmentedUnwindPlan(*target, *thread); 3498 if (of_unwind_augmented_sp) { 3499 result.GetOutputStream().Printf("object file augmented UnwindPlan:\n"); 3500 of_unwind_augmented_sp->Dump(result.GetOutputStream(), thread.get(), 3501 LLDB_INVALID_ADDRESS); 3502 result.GetOutputStream().Printf("\n"); 3503 } 3504 3505 UnwindPlanSP ehframe_sp = 3506 func_unwinders_sp->GetEHFrameUnwindPlan(*target); 3507 if (ehframe_sp) { 3508 result.GetOutputStream().Printf("eh_frame UnwindPlan:\n"); 3509 ehframe_sp->Dump(result.GetOutputStream(), thread.get(), 3510 LLDB_INVALID_ADDRESS); 3511 result.GetOutputStream().Printf("\n"); 3512 } 3513 3514 UnwindPlanSP ehframe_augmented_sp = 3515 func_unwinders_sp->GetEHFrameAugmentedUnwindPlan(*target, *thread); 3516 if (ehframe_augmented_sp) { 3517 result.GetOutputStream().Printf("eh_frame augmented UnwindPlan:\n"); 3518 ehframe_augmented_sp->Dump(result.GetOutputStream(), thread.get(), 3519 LLDB_INVALID_ADDRESS); 3520 result.GetOutputStream().Printf("\n"); 3521 } 3522 3523 if (UnwindPlanSP plan_sp = 3524 func_unwinders_sp->GetDebugFrameUnwindPlan(*target)) { 3525 result.GetOutputStream().Printf("debug_frame UnwindPlan:\n"); 3526 plan_sp->Dump(result.GetOutputStream(), thread.get(), 3527 LLDB_INVALID_ADDRESS); 3528 result.GetOutputStream().Printf("\n"); 3529 } 3530 3531 if (UnwindPlanSP plan_sp = 3532 func_unwinders_sp->GetDebugFrameAugmentedUnwindPlan(*target, 3533 *thread)) { 3534 result.GetOutputStream().Printf("debug_frame augmented UnwindPlan:\n"); 3535 plan_sp->Dump(result.GetOutputStream(), thread.get(), 3536 LLDB_INVALID_ADDRESS); 3537 result.GetOutputStream().Printf("\n"); 3538 } 3539 3540 UnwindPlanSP arm_unwind_sp = 3541 func_unwinders_sp->GetArmUnwindUnwindPlan(*target); 3542 if (arm_unwind_sp) { 3543 result.GetOutputStream().Printf("ARM.exidx unwind UnwindPlan:\n"); 3544 arm_unwind_sp->Dump(result.GetOutputStream(), thread.get(), 3545 LLDB_INVALID_ADDRESS); 3546 result.GetOutputStream().Printf("\n"); 3547 } 3548 3549 if (UnwindPlanSP symfile_plan_sp = 3550 func_unwinders_sp->GetSymbolFileUnwindPlan(*thread)) { 3551 result.GetOutputStream().Printf("Symbol file UnwindPlan:\n"); 3552 symfile_plan_sp->Dump(result.GetOutputStream(), thread.get(), 3553 LLDB_INVALID_ADDRESS); 3554 result.GetOutputStream().Printf("\n"); 3555 } 3556 3557 UnwindPlanSP compact_unwind_sp = 3558 func_unwinders_sp->GetCompactUnwindUnwindPlan(*target); 3559 if (compact_unwind_sp) { 3560 result.GetOutputStream().Printf("Compact unwind UnwindPlan:\n"); 3561 compact_unwind_sp->Dump(result.GetOutputStream(), thread.get(), 3562 LLDB_INVALID_ADDRESS); 3563 result.GetOutputStream().Printf("\n"); 3564 } 3565 3566 if (fast_unwind_plan) { 3567 result.GetOutputStream().Printf("Fast UnwindPlan:\n"); 3568 fast_unwind_plan->Dump(result.GetOutputStream(), thread.get(), 3569 LLDB_INVALID_ADDRESS); 3570 result.GetOutputStream().Printf("\n"); 3571 } 3572 3573 ABISP abi_sp = process->GetABI(); 3574 if (abi_sp) { 3575 UnwindPlan arch_default(lldb::eRegisterKindGeneric); 3576 if (abi_sp->CreateDefaultUnwindPlan(arch_default)) { 3577 result.GetOutputStream().Printf("Arch default UnwindPlan:\n"); 3578 arch_default.Dump(result.GetOutputStream(), thread.get(), 3579 LLDB_INVALID_ADDRESS); 3580 result.GetOutputStream().Printf("\n"); 3581 } 3582 3583 UnwindPlan arch_entry(lldb::eRegisterKindGeneric); 3584 if (abi_sp->CreateFunctionEntryUnwindPlan(arch_entry)) { 3585 result.GetOutputStream().Printf( 3586 "Arch default at entry point UnwindPlan:\n"); 3587 arch_entry.Dump(result.GetOutputStream(), thread.get(), 3588 LLDB_INVALID_ADDRESS); 3589 result.GetOutputStream().Printf("\n"); 3590 } 3591 } 3592 3593 result.GetOutputStream().Printf("\n"); 3594 } 3595 return result.Succeeded(); 3596 } 3597 3598 CommandOptions m_options; 3599 }; 3600 3601 // Lookup information in images 3602 #define LLDB_OPTIONS_target_modules_lookup 3603 #include "CommandOptions.inc" 3604 3605 class CommandObjectTargetModulesLookup : public CommandObjectParsed { 3606 public: 3607 enum { 3608 eLookupTypeInvalid = -1, 3609 eLookupTypeAddress = 0, 3610 eLookupTypeSymbol, 3611 eLookupTypeFileLine, // Line is optional 3612 eLookupTypeFunction, 3613 eLookupTypeFunctionOrSymbol, 3614 eLookupTypeType, 3615 kNumLookupTypes 3616 }; 3617 3618 class CommandOptions : public Options { 3619 public: 3620 CommandOptions() : Options() { OptionParsingStarting(nullptr); } 3621 3622 ~CommandOptions() override = default; 3623 3624 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 3625 ExecutionContext *execution_context) override { 3626 Status error; 3627 3628 const int short_option = m_getopt_table[option_idx].val; 3629 3630 switch (short_option) { 3631 case 'a': { 3632 m_type = eLookupTypeAddress; 3633 m_addr = OptionArgParser::ToAddress(execution_context, option_arg, 3634 LLDB_INVALID_ADDRESS, &error); 3635 } break; 3636 3637 case 'o': 3638 if (option_arg.getAsInteger(0, m_offset)) 3639 error.SetErrorStringWithFormat("invalid offset string '%s'", 3640 option_arg.str().c_str()); 3641 break; 3642 3643 case 's': 3644 m_str = std::string(option_arg); 3645 m_type = eLookupTypeSymbol; 3646 break; 3647 3648 case 'f': 3649 m_file.SetFile(option_arg, FileSpec::Style::native); 3650 m_type = eLookupTypeFileLine; 3651 break; 3652 3653 case 'i': 3654 m_include_inlines = false; 3655 break; 3656 3657 case 'l': 3658 if (option_arg.getAsInteger(0, m_line_number)) 3659 error.SetErrorStringWithFormat("invalid line number string '%s'", 3660 option_arg.str().c_str()); 3661 else if (m_line_number == 0) 3662 error.SetErrorString("zero is an invalid line number"); 3663 m_type = eLookupTypeFileLine; 3664 break; 3665 3666 case 'F': 3667 m_str = std::string(option_arg); 3668 m_type = eLookupTypeFunction; 3669 break; 3670 3671 case 'n': 3672 m_str = std::string(option_arg); 3673 m_type = eLookupTypeFunctionOrSymbol; 3674 break; 3675 3676 case 't': 3677 m_str = std::string(option_arg); 3678 m_type = eLookupTypeType; 3679 break; 3680 3681 case 'v': 3682 m_verbose = true; 3683 break; 3684 3685 case 'A': 3686 m_print_all = true; 3687 break; 3688 3689 case 'r': 3690 m_use_regex = true; 3691 break; 3692 default: 3693 llvm_unreachable("Unimplemented option"); 3694 } 3695 3696 return error; 3697 } 3698 3699 void OptionParsingStarting(ExecutionContext *execution_context) override { 3700 m_type = eLookupTypeInvalid; 3701 m_str.clear(); 3702 m_file.Clear(); 3703 m_addr = LLDB_INVALID_ADDRESS; 3704 m_offset = 0; 3705 m_line_number = 0; 3706 m_use_regex = false; 3707 m_include_inlines = true; 3708 m_verbose = false; 3709 m_print_all = false; 3710 } 3711 3712 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 3713 return llvm::makeArrayRef(g_target_modules_lookup_options); 3714 } 3715 3716 int m_type; // Should be a eLookupTypeXXX enum after parsing options 3717 std::string m_str; // Holds name lookup 3718 FileSpec m_file; // Files for file lookups 3719 lldb::addr_t m_addr; // Holds the address to lookup 3720 lldb::addr_t 3721 m_offset; // Subtract this offset from m_addr before doing lookups. 3722 uint32_t m_line_number; // Line number for file+line lookups 3723 bool m_use_regex; // Name lookups in m_str are regular expressions. 3724 bool m_include_inlines; // Check for inline entries when looking up by 3725 // file/line. 3726 bool m_verbose; // Enable verbose lookup info 3727 bool m_print_all; // Print all matches, even in cases where there's a best 3728 // match. 3729 }; 3730 3731 CommandObjectTargetModulesLookup(CommandInterpreter &interpreter) 3732 : CommandObjectParsed(interpreter, "target modules lookup", 3733 "Look up information within executable and " 3734 "dependent shared library images.", 3735 nullptr, eCommandRequiresTarget), 3736 m_options() { 3737 CommandArgumentEntry arg; 3738 CommandArgumentData file_arg; 3739 3740 // Define the first (and only) variant of this arg. 3741 file_arg.arg_type = eArgTypeFilename; 3742 file_arg.arg_repetition = eArgRepeatStar; 3743 3744 // There is only one variant this argument could be; put it into the 3745 // argument entry. 3746 arg.push_back(file_arg); 3747 3748 // Push the data for the first argument into the m_arguments vector. 3749 m_arguments.push_back(arg); 3750 } 3751 3752 ~CommandObjectTargetModulesLookup() override = default; 3753 3754 Options *GetOptions() override { return &m_options; } 3755 3756 bool LookupHere(CommandInterpreter &interpreter, CommandReturnObject &result, 3757 bool &syntax_error) { 3758 switch (m_options.m_type) { 3759 case eLookupTypeAddress: 3760 case eLookupTypeFileLine: 3761 case eLookupTypeFunction: 3762 case eLookupTypeFunctionOrSymbol: 3763 case eLookupTypeSymbol: 3764 default: 3765 return false; 3766 case eLookupTypeType: 3767 break; 3768 } 3769 3770 StackFrameSP frame = m_exe_ctx.GetFrameSP(); 3771 3772 if (!frame) 3773 return false; 3774 3775 const SymbolContext &sym_ctx(frame->GetSymbolContext(eSymbolContextModule)); 3776 3777 if (!sym_ctx.module_sp) 3778 return false; 3779 3780 switch (m_options.m_type) { 3781 default: 3782 return false; 3783 case eLookupTypeType: 3784 if (!m_options.m_str.empty()) { 3785 if (LookupTypeHere(&GetSelectedTarget(), m_interpreter, 3786 result.GetOutputStream(), *sym_ctx.module_sp, 3787 m_options.m_str.c_str(), m_options.m_use_regex)) { 3788 result.SetStatus(eReturnStatusSuccessFinishResult); 3789 return true; 3790 } 3791 } 3792 break; 3793 } 3794 3795 return false; 3796 } 3797 3798 bool LookupInModule(CommandInterpreter &interpreter, Module *module, 3799 CommandReturnObject &result, bool &syntax_error) { 3800 switch (m_options.m_type) { 3801 case eLookupTypeAddress: 3802 if (m_options.m_addr != LLDB_INVALID_ADDRESS) { 3803 if (LookupAddressInModule( 3804 m_interpreter, result.GetOutputStream(), module, 3805 eSymbolContextEverything | 3806 (m_options.m_verbose 3807 ? static_cast<int>(eSymbolContextVariable) 3808 : 0), 3809 m_options.m_addr, m_options.m_offset, m_options.m_verbose)) { 3810 result.SetStatus(eReturnStatusSuccessFinishResult); 3811 return true; 3812 } 3813 } 3814 break; 3815 3816 case eLookupTypeSymbol: 3817 if (!m_options.m_str.empty()) { 3818 if (LookupSymbolInModule(m_interpreter, result.GetOutputStream(), 3819 module, m_options.m_str.c_str(), 3820 m_options.m_use_regex, m_options.m_verbose)) { 3821 result.SetStatus(eReturnStatusSuccessFinishResult); 3822 return true; 3823 } 3824 } 3825 break; 3826 3827 case eLookupTypeFileLine: 3828 if (m_options.m_file) { 3829 if (LookupFileAndLineInModule( 3830 m_interpreter, result.GetOutputStream(), module, 3831 m_options.m_file, m_options.m_line_number, 3832 m_options.m_include_inlines, m_options.m_verbose)) { 3833 result.SetStatus(eReturnStatusSuccessFinishResult); 3834 return true; 3835 } 3836 } 3837 break; 3838 3839 case eLookupTypeFunctionOrSymbol: 3840 case eLookupTypeFunction: 3841 if (!m_options.m_str.empty()) { 3842 if (LookupFunctionInModule( 3843 m_interpreter, result.GetOutputStream(), module, 3844 m_options.m_str.c_str(), m_options.m_use_regex, 3845 m_options.m_include_inlines, 3846 m_options.m_type == 3847 eLookupTypeFunctionOrSymbol, // include symbols 3848 m_options.m_verbose)) { 3849 result.SetStatus(eReturnStatusSuccessFinishResult); 3850 return true; 3851 } 3852 } 3853 break; 3854 3855 case eLookupTypeType: 3856 if (!m_options.m_str.empty()) { 3857 if (LookupTypeInModule( 3858 &GetSelectedTarget(), m_interpreter, result.GetOutputStream(), 3859 module, m_options.m_str.c_str(), m_options.m_use_regex)) { 3860 result.SetStatus(eReturnStatusSuccessFinishResult); 3861 return true; 3862 } 3863 } 3864 break; 3865 3866 default: 3867 m_options.GenerateOptionUsage( 3868 result.GetErrorStream(), this, 3869 GetCommandInterpreter().GetDebugger().GetTerminalWidth()); 3870 syntax_error = true; 3871 break; 3872 } 3873 3874 result.SetStatus(eReturnStatusFailed); 3875 return false; 3876 } 3877 3878 protected: 3879 bool DoExecute(Args &command, CommandReturnObject &result) override { 3880 Target *target = &GetSelectedTarget(); 3881 bool syntax_error = false; 3882 uint32_t i; 3883 uint32_t num_successful_lookups = 0; 3884 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 3885 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 3886 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 3887 // Dump all sections for all modules images 3888 3889 if (command.GetArgumentCount() == 0) { 3890 ModuleSP current_module; 3891 3892 // Where it is possible to look in the current symbol context first, 3893 // try that. If this search was successful and --all was not passed, 3894 // don't print anything else. 3895 if (LookupHere(m_interpreter, result, syntax_error)) { 3896 result.GetOutputStream().EOL(); 3897 num_successful_lookups++; 3898 if (!m_options.m_print_all) { 3899 result.SetStatus(eReturnStatusSuccessFinishResult); 3900 return result.Succeeded(); 3901 } 3902 } 3903 3904 // Dump all sections for all other modules 3905 3906 const ModuleList &target_modules = target->GetImages(); 3907 std::lock_guard<std::recursive_mutex> guard(target_modules.GetMutex()); 3908 const size_t num_modules = target_modules.GetSize(); 3909 if (num_modules > 0) { 3910 for (i = 0; i < num_modules && !syntax_error; ++i) { 3911 Module *module_pointer = 3912 target_modules.GetModulePointerAtIndexUnlocked(i); 3913 3914 if (module_pointer != current_module.get() && 3915 LookupInModule(m_interpreter, 3916 target_modules.GetModulePointerAtIndexUnlocked(i), 3917 result, syntax_error)) { 3918 result.GetOutputStream().EOL(); 3919 num_successful_lookups++; 3920 } 3921 } 3922 } else { 3923 result.AppendError("the target has no associated executable images"); 3924 result.SetStatus(eReturnStatusFailed); 3925 return false; 3926 } 3927 } else { 3928 // Dump specified images (by basename or fullpath) 3929 const char *arg_cstr; 3930 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != nullptr && 3931 !syntax_error; 3932 ++i) { 3933 ModuleList module_list; 3934 const size_t num_matches = 3935 FindModulesByName(target, arg_cstr, module_list, false); 3936 if (num_matches > 0) { 3937 for (size_t j = 0; j < num_matches; ++j) { 3938 Module *module = module_list.GetModulePointerAtIndex(j); 3939 if (module) { 3940 if (LookupInModule(m_interpreter, module, result, syntax_error)) { 3941 result.GetOutputStream().EOL(); 3942 num_successful_lookups++; 3943 } 3944 } 3945 } 3946 } else 3947 result.AppendWarningWithFormat( 3948 "Unable to find an image that matches '%s'.\n", arg_cstr); 3949 } 3950 } 3951 3952 if (num_successful_lookups > 0) 3953 result.SetStatus(eReturnStatusSuccessFinishResult); 3954 else 3955 result.SetStatus(eReturnStatusFailed); 3956 return result.Succeeded(); 3957 } 3958 3959 CommandOptions m_options; 3960 }; 3961 3962 #pragma mark CommandObjectMultiwordImageSearchPaths 3963 3964 // CommandObjectMultiwordImageSearchPaths 3965 3966 class CommandObjectTargetModulesImageSearchPaths 3967 : public CommandObjectMultiword { 3968 public: 3969 CommandObjectTargetModulesImageSearchPaths(CommandInterpreter &interpreter) 3970 : CommandObjectMultiword( 3971 interpreter, "target modules search-paths", 3972 "Commands for managing module search paths for a target.", 3973 "target modules search-paths <subcommand> [<subcommand-options>]") { 3974 LoadSubCommand( 3975 "add", CommandObjectSP( 3976 new CommandObjectTargetModulesSearchPathsAdd(interpreter))); 3977 LoadSubCommand( 3978 "clear", CommandObjectSP(new CommandObjectTargetModulesSearchPathsClear( 3979 interpreter))); 3980 LoadSubCommand( 3981 "insert", 3982 CommandObjectSP( 3983 new CommandObjectTargetModulesSearchPathsInsert(interpreter))); 3984 LoadSubCommand( 3985 "list", CommandObjectSP(new CommandObjectTargetModulesSearchPathsList( 3986 interpreter))); 3987 LoadSubCommand( 3988 "query", CommandObjectSP(new CommandObjectTargetModulesSearchPathsQuery( 3989 interpreter))); 3990 } 3991 3992 ~CommandObjectTargetModulesImageSearchPaths() override = default; 3993 }; 3994 3995 #pragma mark CommandObjectTargetModules 3996 3997 // CommandObjectTargetModules 3998 3999 class CommandObjectTargetModules : public CommandObjectMultiword { 4000 public: 4001 // Constructors and Destructors 4002 CommandObjectTargetModules(CommandInterpreter &interpreter) 4003 : CommandObjectMultiword(interpreter, "target modules", 4004 "Commands for accessing information for one or " 4005 "more target modules.", 4006 "target modules <sub-command> ...") { 4007 LoadSubCommand( 4008 "add", CommandObjectSP(new CommandObjectTargetModulesAdd(interpreter))); 4009 LoadSubCommand("load", CommandObjectSP(new CommandObjectTargetModulesLoad( 4010 interpreter))); 4011 LoadSubCommand("dump", CommandObjectSP(new CommandObjectTargetModulesDump( 4012 interpreter))); 4013 LoadSubCommand("list", CommandObjectSP(new CommandObjectTargetModulesList( 4014 interpreter))); 4015 LoadSubCommand( 4016 "lookup", 4017 CommandObjectSP(new CommandObjectTargetModulesLookup(interpreter))); 4018 LoadSubCommand( 4019 "search-paths", 4020 CommandObjectSP( 4021 new CommandObjectTargetModulesImageSearchPaths(interpreter))); 4022 LoadSubCommand( 4023 "show-unwind", 4024 CommandObjectSP(new CommandObjectTargetModulesShowUnwind(interpreter))); 4025 } 4026 4027 ~CommandObjectTargetModules() override = default; 4028 4029 private: 4030 // For CommandObjectTargetModules only 4031 CommandObjectTargetModules(const CommandObjectTargetModules &) = delete; 4032 const CommandObjectTargetModules & 4033 operator=(const CommandObjectTargetModules &) = delete; 4034 }; 4035 4036 class CommandObjectTargetSymbolsAdd : public CommandObjectParsed { 4037 public: 4038 CommandObjectTargetSymbolsAdd(CommandInterpreter &interpreter) 4039 : CommandObjectParsed( 4040 interpreter, "target symbols add", 4041 "Add a debug symbol file to one of the target's current modules by " 4042 "specifying a path to a debug symbols file or by using the options " 4043 "to specify a module.", 4044 "target symbols add <cmd-options> [<symfile>]", 4045 eCommandRequiresTarget), 4046 m_option_group(), 4047 m_file_option( 4048 LLDB_OPT_SET_1, false, "shlib", 's', 4049 CommandCompletions::eModuleCompletion, eArgTypeShlibName, 4050 "Locate the debug symbols for the shared library specified by " 4051 "name."), 4052 m_current_frame_option( 4053 LLDB_OPT_SET_2, false, "frame", 'F', 4054 "Locate the debug symbols for the currently selected frame.", 4055 false, true) 4056 4057 { 4058 m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL, 4059 LLDB_OPT_SET_1); 4060 m_option_group.Append(&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 4061 m_option_group.Append(&m_current_frame_option, LLDB_OPT_SET_2, 4062 LLDB_OPT_SET_2); 4063 m_option_group.Finalize(); 4064 } 4065 4066 ~CommandObjectTargetSymbolsAdd() override = default; 4067 4068 void 4069 HandleArgumentCompletion(CompletionRequest &request, 4070 OptionElementVector &opt_element_vector) override { 4071 CommandCompletions::InvokeCommonCompletionCallbacks( 4072 GetCommandInterpreter(), CommandCompletions::eDiskFileCompletion, 4073 request, nullptr); 4074 } 4075 4076 Options *GetOptions() override { return &m_option_group; } 4077 4078 protected: 4079 bool AddModuleSymbols(Target *target, ModuleSpec &module_spec, bool &flush, 4080 CommandReturnObject &result) { 4081 const FileSpec &symbol_fspec = module_spec.GetSymbolFileSpec(); 4082 if (!symbol_fspec) { 4083 result.AppendError( 4084 "one or more executable image paths must be specified"); 4085 result.SetStatus(eReturnStatusFailed); 4086 return false; 4087 } 4088 4089 char symfile_path[PATH_MAX]; 4090 symbol_fspec.GetPath(symfile_path, sizeof(symfile_path)); 4091 4092 if (!module_spec.GetUUID().IsValid()) { 4093 if (!module_spec.GetFileSpec() && !module_spec.GetPlatformFileSpec()) 4094 module_spec.GetFileSpec().GetFilename() = symbol_fspec.GetFilename(); 4095 } 4096 4097 // Now module_spec represents a symbol file for a module that might exist 4098 // in the current target. Let's find possible matches. 4099 ModuleList matching_modules; 4100 4101 // First extract all module specs from the symbol file 4102 lldb_private::ModuleSpecList symfile_module_specs; 4103 if (ObjectFile::GetModuleSpecifications(module_spec.GetSymbolFileSpec(), 4104 0, 0, symfile_module_specs)) { 4105 // Now extract the module spec that matches the target architecture 4106 ModuleSpec target_arch_module_spec; 4107 ModuleSpec symfile_module_spec; 4108 target_arch_module_spec.GetArchitecture() = target->GetArchitecture(); 4109 if (symfile_module_specs.FindMatchingModuleSpec(target_arch_module_spec, 4110 symfile_module_spec)) { 4111 if (symfile_module_spec.GetUUID().IsValid()) { 4112 // It has a UUID, look for this UUID in the target modules 4113 ModuleSpec symfile_uuid_module_spec; 4114 symfile_uuid_module_spec.GetUUID() = symfile_module_spec.GetUUID(); 4115 target->GetImages().FindModules(symfile_uuid_module_spec, 4116 matching_modules); 4117 } 4118 } 4119 4120 if (matching_modules.IsEmpty()) { 4121 // No matches yet. Iterate through the module specs to find a UUID 4122 // value that we can match up to an image in our target. 4123 const size_t num_symfile_module_specs = symfile_module_specs.GetSize(); 4124 for (size_t i = 0; 4125 i < num_symfile_module_specs && matching_modules.IsEmpty(); ++i) { 4126 if (symfile_module_specs.GetModuleSpecAtIndex( 4127 i, symfile_module_spec)) { 4128 if (symfile_module_spec.GetUUID().IsValid()) { 4129 // It has a UUID. Look for this UUID in the target modules. 4130 ModuleSpec symfile_uuid_module_spec; 4131 symfile_uuid_module_spec.GetUUID() = 4132 symfile_module_spec.GetUUID(); 4133 target->GetImages().FindModules(symfile_uuid_module_spec, 4134 matching_modules); 4135 } 4136 } 4137 } 4138 } 4139 } 4140 4141 // Just try to match up the file by basename if we have no matches at 4142 // this point. For example, module foo might have symbols in foo.debug. 4143 if (matching_modules.IsEmpty()) 4144 target->GetImages().FindModules(module_spec, matching_modules); 4145 4146 while (matching_modules.IsEmpty()) { 4147 ConstString filename_no_extension( 4148 module_spec.GetFileSpec().GetFileNameStrippingExtension()); 4149 // Empty string returned, let's bail 4150 if (!filename_no_extension) 4151 break; 4152 4153 // Check if there was no extension to strip and the basename is the same 4154 if (filename_no_extension == module_spec.GetFileSpec().GetFilename()) 4155 break; 4156 4157 // Replace basename with one fewer extension 4158 module_spec.GetFileSpec().GetFilename() = filename_no_extension; 4159 target->GetImages().FindModules(module_spec, matching_modules); 4160 } 4161 4162 if (matching_modules.GetSize() > 1) { 4163 result.AppendErrorWithFormat("multiple modules match symbol file '%s', " 4164 "use the --uuid option to resolve the " 4165 "ambiguity.\n", 4166 symfile_path); 4167 result.SetStatus(eReturnStatusFailed); 4168 return false; 4169 } 4170 4171 if (matching_modules.GetSize() == 1) { 4172 ModuleSP module_sp(matching_modules.GetModuleAtIndex(0)); 4173 4174 // The module has not yet created its symbol vendor, we can just give 4175 // the existing target module the symfile path to use for when it 4176 // decides to create it! 4177 module_sp->SetSymbolFileFileSpec(symbol_fspec); 4178 4179 SymbolFile *symbol_file = 4180 module_sp->GetSymbolFile(true, &result.GetErrorStream()); 4181 if (symbol_file) { 4182 ObjectFile *object_file = symbol_file->GetObjectFile(); 4183 if (object_file && object_file->GetFileSpec() == symbol_fspec) { 4184 // Provide feedback that the symfile has been successfully added. 4185 const FileSpec &module_fs = module_sp->GetFileSpec(); 4186 result.AppendMessageWithFormat( 4187 "symbol file '%s' has been added to '%s'\n", symfile_path, 4188 module_fs.GetPath().c_str()); 4189 4190 // Let clients know something changed in the module if it is 4191 // currently loaded 4192 ModuleList module_list; 4193 module_list.Append(module_sp); 4194 target->SymbolsDidLoad(module_list); 4195 4196 // Make sure we load any scripting resources that may be embedded 4197 // in the debug info files in case the platform supports that. 4198 Status error; 4199 StreamString feedback_stream; 4200 module_sp->LoadScriptingResourceInTarget(target, error, 4201 &feedback_stream); 4202 if (error.Fail() && error.AsCString()) 4203 result.AppendWarningWithFormat( 4204 "unable to load scripting data for module %s - error " 4205 "reported was %s", 4206 module_sp->GetFileSpec() 4207 .GetFileNameStrippingExtension() 4208 .GetCString(), 4209 error.AsCString()); 4210 else if (feedback_stream.GetSize()) 4211 result.AppendWarning(feedback_stream.GetData()); 4212 4213 flush = true; 4214 result.SetStatus(eReturnStatusSuccessFinishResult); 4215 return true; 4216 } 4217 } 4218 // Clear the symbol file spec if anything went wrong 4219 module_sp->SetSymbolFileFileSpec(FileSpec()); 4220 } 4221 4222 StreamString ss_symfile_uuid; 4223 if (module_spec.GetUUID().IsValid()) { 4224 ss_symfile_uuid << " ("; 4225 module_spec.GetUUID().Dump(&ss_symfile_uuid); 4226 ss_symfile_uuid << ')'; 4227 } 4228 result.AppendErrorWithFormat( 4229 "symbol file '%s'%s does not match any existing module%s\n", 4230 symfile_path, ss_symfile_uuid.GetData(), 4231 !llvm::sys::fs::is_regular_file(symbol_fspec.GetPath()) 4232 ? "\n please specify the full path to the symbol file" 4233 : ""); 4234 result.SetStatus(eReturnStatusFailed); 4235 return false; 4236 } 4237 4238 bool DoExecute(Args &args, CommandReturnObject &result) override { 4239 Target *target = m_exe_ctx.GetTargetPtr(); 4240 result.SetStatus(eReturnStatusFailed); 4241 bool flush = false; 4242 ModuleSpec module_spec; 4243 const bool uuid_option_set = 4244 m_uuid_option_group.GetOptionValue().OptionWasSet(); 4245 const bool file_option_set = m_file_option.GetOptionValue().OptionWasSet(); 4246 const bool frame_option_set = 4247 m_current_frame_option.GetOptionValue().OptionWasSet(); 4248 const size_t argc = args.GetArgumentCount(); 4249 4250 if (argc == 0) { 4251 if (uuid_option_set || file_option_set || frame_option_set) { 4252 bool success = false; 4253 bool error_set = false; 4254 if (frame_option_set) { 4255 Process *process = m_exe_ctx.GetProcessPtr(); 4256 if (process) { 4257 const StateType process_state = process->GetState(); 4258 if (StateIsStoppedState(process_state, true)) { 4259 StackFrame *frame = m_exe_ctx.GetFramePtr(); 4260 if (frame) { 4261 ModuleSP frame_module_sp( 4262 frame->GetSymbolContext(eSymbolContextModule).module_sp); 4263 if (frame_module_sp) { 4264 if (FileSystem::Instance().Exists( 4265 frame_module_sp->GetPlatformFileSpec())) { 4266 module_spec.GetArchitecture() = 4267 frame_module_sp->GetArchitecture(); 4268 module_spec.GetFileSpec() = 4269 frame_module_sp->GetPlatformFileSpec(); 4270 } 4271 module_spec.GetUUID() = frame_module_sp->GetUUID(); 4272 success = module_spec.GetUUID().IsValid() || 4273 module_spec.GetFileSpec(); 4274 } else { 4275 result.AppendError("frame has no module"); 4276 error_set = true; 4277 } 4278 } else { 4279 result.AppendError("invalid current frame"); 4280 error_set = true; 4281 } 4282 } else { 4283 result.AppendErrorWithFormat("process is not stopped: %s", 4284 StateAsCString(process_state)); 4285 error_set = true; 4286 } 4287 } else { 4288 result.AppendError( 4289 "a process must exist in order to use the --frame option"); 4290 error_set = true; 4291 } 4292 } else { 4293 if (uuid_option_set) { 4294 module_spec.GetUUID() = 4295 m_uuid_option_group.GetOptionValue().GetCurrentValue(); 4296 success |= module_spec.GetUUID().IsValid(); 4297 } else if (file_option_set) { 4298 module_spec.GetFileSpec() = 4299 m_file_option.GetOptionValue().GetCurrentValue(); 4300 ModuleSP module_sp( 4301 target->GetImages().FindFirstModule(module_spec)); 4302 if (module_sp) { 4303 module_spec.GetFileSpec() = module_sp->GetFileSpec(); 4304 module_spec.GetPlatformFileSpec() = 4305 module_sp->GetPlatformFileSpec(); 4306 module_spec.GetUUID() = module_sp->GetUUID(); 4307 module_spec.GetArchitecture() = module_sp->GetArchitecture(); 4308 } else { 4309 module_spec.GetArchitecture() = target->GetArchitecture(); 4310 } 4311 success |= module_spec.GetUUID().IsValid() || 4312 FileSystem::Instance().Exists(module_spec.GetFileSpec()); 4313 } 4314 } 4315 4316 if (success) { 4317 if (Symbols::DownloadObjectAndSymbolFile(module_spec)) { 4318 if (module_spec.GetSymbolFileSpec()) 4319 success = AddModuleSymbols(target, module_spec, flush, result); 4320 } 4321 } 4322 4323 if (!success && !error_set) { 4324 StreamString error_strm; 4325 if (uuid_option_set) { 4326 error_strm.PutCString("unable to find debug symbols for UUID "); 4327 module_spec.GetUUID().Dump(&error_strm); 4328 } else if (file_option_set) { 4329 error_strm.PutCString( 4330 "unable to find debug symbols for the executable file "); 4331 error_strm << module_spec.GetFileSpec(); 4332 } else if (frame_option_set) { 4333 error_strm.PutCString( 4334 "unable to find debug symbols for the current frame"); 4335 } 4336 result.AppendError(error_strm.GetString()); 4337 } 4338 } else { 4339 result.AppendError("one or more symbol file paths must be specified, " 4340 "or options must be specified"); 4341 } 4342 } else { 4343 if (uuid_option_set) { 4344 result.AppendError("specify either one or more paths to symbol files " 4345 "or use the --uuid option without arguments"); 4346 } else if (frame_option_set) { 4347 result.AppendError("specify either one or more paths to symbol files " 4348 "or use the --frame option without arguments"); 4349 } else if (file_option_set && argc > 1) { 4350 result.AppendError("specify at most one symbol file path when " 4351 "--shlib option is set"); 4352 } else { 4353 PlatformSP platform_sp(target->GetPlatform()); 4354 4355 for (auto &entry : args.entries()) { 4356 if (!entry.ref().empty()) { 4357 auto &symbol_file_spec = module_spec.GetSymbolFileSpec(); 4358 symbol_file_spec.SetFile(entry.ref(), FileSpec::Style::native); 4359 FileSystem::Instance().Resolve(symbol_file_spec); 4360 if (file_option_set) { 4361 module_spec.GetFileSpec() = 4362 m_file_option.GetOptionValue().GetCurrentValue(); 4363 } 4364 if (platform_sp) { 4365 FileSpec symfile_spec; 4366 if (platform_sp 4367 ->ResolveSymbolFile(*target, module_spec, symfile_spec) 4368 .Success()) 4369 module_spec.GetSymbolFileSpec() = symfile_spec; 4370 } 4371 4372 bool symfile_exists = 4373 FileSystem::Instance().Exists(module_spec.GetSymbolFileSpec()); 4374 4375 if (symfile_exists) { 4376 if (!AddModuleSymbols(target, module_spec, flush, result)) 4377 break; 4378 } else { 4379 std::string resolved_symfile_path = 4380 module_spec.GetSymbolFileSpec().GetPath(); 4381 if (resolved_symfile_path != entry.ref()) { 4382 result.AppendErrorWithFormat( 4383 "invalid module path '%s' with resolved path '%s'\n", 4384 entry.c_str(), resolved_symfile_path.c_str()); 4385 break; 4386 } 4387 result.AppendErrorWithFormat("invalid module path '%s'\n", 4388 entry.c_str()); 4389 break; 4390 } 4391 } 4392 } 4393 } 4394 } 4395 4396 if (flush) { 4397 Process *process = m_exe_ctx.GetProcessPtr(); 4398 if (process) 4399 process->Flush(); 4400 } 4401 return result.Succeeded(); 4402 } 4403 4404 OptionGroupOptions m_option_group; 4405 OptionGroupUUID m_uuid_option_group; 4406 OptionGroupFile m_file_option; 4407 OptionGroupBoolean m_current_frame_option; 4408 }; 4409 4410 #pragma mark CommandObjectTargetSymbols 4411 4412 // CommandObjectTargetSymbols 4413 4414 class CommandObjectTargetSymbols : public CommandObjectMultiword { 4415 public: 4416 // Constructors and Destructors 4417 CommandObjectTargetSymbols(CommandInterpreter &interpreter) 4418 : CommandObjectMultiword( 4419 interpreter, "target symbols", 4420 "Commands for adding and managing debug symbol files.", 4421 "target symbols <sub-command> ...") { 4422 LoadSubCommand( 4423 "add", CommandObjectSP(new CommandObjectTargetSymbolsAdd(interpreter))); 4424 } 4425 4426 ~CommandObjectTargetSymbols() override = default; 4427 4428 private: 4429 // For CommandObjectTargetModules only 4430 CommandObjectTargetSymbols(const CommandObjectTargetSymbols &) = delete; 4431 const CommandObjectTargetSymbols & 4432 operator=(const CommandObjectTargetSymbols &) = delete; 4433 }; 4434 4435 #pragma mark CommandObjectTargetStopHookAdd 4436 4437 // CommandObjectTargetStopHookAdd 4438 #define LLDB_OPTIONS_target_stop_hook_add 4439 #include "CommandOptions.inc" 4440 4441 class CommandObjectTargetStopHookAdd : public CommandObjectParsed, 4442 public IOHandlerDelegateMultiline { 4443 public: 4444 class CommandOptions : public Options { 4445 public: 4446 CommandOptions() 4447 : Options(), m_line_start(0), m_line_end(UINT_MAX), 4448 m_func_name_type_mask(eFunctionNameTypeAuto), 4449 m_sym_ctx_specified(false), m_thread_specified(false), 4450 m_use_one_liner(false), m_one_liner() {} 4451 4452 ~CommandOptions() override = default; 4453 4454 llvm::ArrayRef<OptionDefinition> GetDefinitions() override { 4455 return llvm::makeArrayRef(g_target_stop_hook_add_options); 4456 } 4457 4458 Status SetOptionValue(uint32_t option_idx, llvm::StringRef option_arg, 4459 ExecutionContext *execution_context) override { 4460 Status error; 4461 const int short_option = m_getopt_table[option_idx].val; 4462 4463 switch (short_option) { 4464 case 'c': 4465 m_class_name = std::string(option_arg); 4466 m_sym_ctx_specified = true; 4467 break; 4468 4469 case 'e': 4470 if (option_arg.getAsInteger(0, m_line_end)) { 4471 error.SetErrorStringWithFormat("invalid end line number: \"%s\"", 4472 option_arg.str().c_str()); 4473 break; 4474 } 4475 m_sym_ctx_specified = true; 4476 break; 4477 4478 case 'G': { 4479 bool value, success; 4480 value = OptionArgParser::ToBoolean(option_arg, false, &success); 4481 if (success) { 4482 m_auto_continue = value; 4483 } else 4484 error.SetErrorStringWithFormat( 4485 "invalid boolean value '%s' passed for -G option", 4486 option_arg.str().c_str()); 4487 } break; 4488 case 'l': 4489 if (option_arg.getAsInteger(0, m_line_start)) { 4490 error.SetErrorStringWithFormat("invalid start line number: \"%s\"", 4491 option_arg.str().c_str()); 4492 break; 4493 } 4494 m_sym_ctx_specified = true; 4495 break; 4496 4497 case 'i': 4498 m_no_inlines = true; 4499 break; 4500 4501 case 'n': 4502 m_function_name = std::string(option_arg); 4503 m_func_name_type_mask |= eFunctionNameTypeAuto; 4504 m_sym_ctx_specified = true; 4505 break; 4506 4507 case 'f': 4508 m_file_name = std::string(option_arg); 4509 m_sym_ctx_specified = true; 4510 break; 4511 4512 case 's': 4513 m_module_name = std::string(option_arg); 4514 m_sym_ctx_specified = true; 4515 break; 4516 4517 case 't': 4518 if (option_arg.getAsInteger(0, m_thread_id)) 4519 error.SetErrorStringWithFormat("invalid thread id string '%s'", 4520 option_arg.str().c_str()); 4521 m_thread_specified = true; 4522 break; 4523 4524 case 'T': 4525 m_thread_name = std::string(option_arg); 4526 m_thread_specified = true; 4527 break; 4528 4529 case 'q': 4530 m_queue_name = std::string(option_arg); 4531 m_thread_specified = true; 4532 break; 4533 4534 case 'x': 4535 if (option_arg.getAsInteger(0, m_thread_index)) 4536 error.SetErrorStringWithFormat("invalid thread index string '%s'", 4537 option_arg.str().c_str()); 4538 m_thread_specified = true; 4539 break; 4540 4541 case 'o': 4542 m_use_one_liner = true; 4543 m_one_liner.push_back(std::string(option_arg)); 4544 break; 4545 4546 default: 4547 llvm_unreachable("Unimplemented option"); 4548 } 4549 return error; 4550 } 4551 4552 void OptionParsingStarting(ExecutionContext *execution_context) override { 4553 m_class_name.clear(); 4554 m_function_name.clear(); 4555 m_line_start = 0; 4556 m_line_end = UINT_MAX; 4557 m_file_name.clear(); 4558 m_module_name.clear(); 4559 m_func_name_type_mask = eFunctionNameTypeAuto; 4560 m_thread_id = LLDB_INVALID_THREAD_ID; 4561 m_thread_index = UINT32_MAX; 4562 m_thread_name.clear(); 4563 m_queue_name.clear(); 4564 4565 m_no_inlines = false; 4566 m_sym_ctx_specified = false; 4567 m_thread_specified = false; 4568 4569 m_use_one_liner = false; 4570 m_one_liner.clear(); 4571 m_auto_continue = false; 4572 } 4573 4574 std::string m_class_name; 4575 std::string m_function_name; 4576 uint32_t m_line_start; 4577 uint32_t m_line_end; 4578 std::string m_file_name; 4579 std::string m_module_name; 4580 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType. 4581 lldb::tid_t m_thread_id; 4582 uint32_t m_thread_index; 4583 std::string m_thread_name; 4584 std::string m_queue_name; 4585 bool m_sym_ctx_specified; 4586 bool m_no_inlines; 4587 bool m_thread_specified; 4588 // Instance variables to hold the values for one_liner options. 4589 bool m_use_one_liner; 4590 std::vector<std::string> m_one_liner; 4591 bool m_auto_continue; 4592 }; 4593 4594 CommandObjectTargetStopHookAdd(CommandInterpreter &interpreter) 4595 : CommandObjectParsed(interpreter, "target stop-hook add", 4596 "Add a hook to be executed when the target stops.", 4597 "target stop-hook add"), 4598 IOHandlerDelegateMultiline("DONE", 4599 IOHandlerDelegate::Completion::LLDBCommand), 4600 m_options() {} 4601 4602 ~CommandObjectTargetStopHookAdd() override = default; 4603 4604 Options *GetOptions() override { return &m_options; } 4605 4606 protected: 4607 void IOHandlerActivated(IOHandler &io_handler, bool interactive) override { 4608 StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 4609 if (output_sp && interactive) { 4610 output_sp->PutCString( 4611 "Enter your stop hook command(s). Type 'DONE' to end.\n"); 4612 output_sp->Flush(); 4613 } 4614 } 4615 4616 void IOHandlerInputComplete(IOHandler &io_handler, 4617 std::string &line) override { 4618 if (m_stop_hook_sp) { 4619 if (line.empty()) { 4620 StreamFileSP error_sp(io_handler.GetErrorStreamFileSP()); 4621 if (error_sp) { 4622 error_sp->Printf("error: stop hook #%" PRIu64 4623 " aborted, no commands.\n", 4624 m_stop_hook_sp->GetID()); 4625 error_sp->Flush(); 4626 } 4627 Target *target = GetDebugger().GetSelectedTarget().get(); 4628 if (target) 4629 target->RemoveStopHookByID(m_stop_hook_sp->GetID()); 4630 } else { 4631 m_stop_hook_sp->GetCommandPointer()->SplitIntoLines(line); 4632 StreamFileSP output_sp(io_handler.GetOutputStreamFileSP()); 4633 if (output_sp) { 4634 output_sp->Printf("Stop hook #%" PRIu64 " added.\n", 4635 m_stop_hook_sp->GetID()); 4636 output_sp->Flush(); 4637 } 4638 } 4639 m_stop_hook_sp.reset(); 4640 } 4641 io_handler.SetIsDone(true); 4642 } 4643 4644 bool DoExecute(Args &command, CommandReturnObject &result) override { 4645 m_stop_hook_sp.reset(); 4646 4647 Target &target = GetSelectedOrDummyTarget(); 4648 Target::StopHookSP new_hook_sp = target.CreateStopHook(); 4649 4650 // First step, make the specifier. 4651 std::unique_ptr<SymbolContextSpecifier> specifier_up; 4652 if (m_options.m_sym_ctx_specified) { 4653 specifier_up = std::make_unique<SymbolContextSpecifier>( 4654 GetDebugger().GetSelectedTarget()); 4655 4656 if (!m_options.m_module_name.empty()) { 4657 specifier_up->AddSpecification( 4658 m_options.m_module_name.c_str(), 4659 SymbolContextSpecifier::eModuleSpecified); 4660 } 4661 4662 if (!m_options.m_class_name.empty()) { 4663 specifier_up->AddSpecification( 4664 m_options.m_class_name.c_str(), 4665 SymbolContextSpecifier::eClassOrNamespaceSpecified); 4666 } 4667 4668 if (!m_options.m_file_name.empty()) { 4669 specifier_up->AddSpecification(m_options.m_file_name.c_str(), 4670 SymbolContextSpecifier::eFileSpecified); 4671 } 4672 4673 if (m_options.m_line_start != 0) { 4674 specifier_up->AddLineSpecification( 4675 m_options.m_line_start, 4676 SymbolContextSpecifier::eLineStartSpecified); 4677 } 4678 4679 if (m_options.m_line_end != UINT_MAX) { 4680 specifier_up->AddLineSpecification( 4681 m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified); 4682 } 4683 4684 if (!m_options.m_function_name.empty()) { 4685 specifier_up->AddSpecification( 4686 m_options.m_function_name.c_str(), 4687 SymbolContextSpecifier::eFunctionSpecified); 4688 } 4689 } 4690 4691 if (specifier_up) 4692 new_hook_sp->SetSpecifier(specifier_up.release()); 4693 4694 // Next see if any of the thread options have been entered: 4695 4696 if (m_options.m_thread_specified) { 4697 ThreadSpec *thread_spec = new ThreadSpec(); 4698 4699 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID) { 4700 thread_spec->SetTID(m_options.m_thread_id); 4701 } 4702 4703 if (m_options.m_thread_index != UINT32_MAX) 4704 thread_spec->SetIndex(m_options.m_thread_index); 4705 4706 if (!m_options.m_thread_name.empty()) 4707 thread_spec->SetName(m_options.m_thread_name.c_str()); 4708 4709 if (!m_options.m_queue_name.empty()) 4710 thread_spec->SetQueueName(m_options.m_queue_name.c_str()); 4711 4712 new_hook_sp->SetThreadSpecifier(thread_spec); 4713 } 4714 4715 new_hook_sp->SetAutoContinue(m_options.m_auto_continue); 4716 if (m_options.m_use_one_liner) { 4717 // Use one-liners. 4718 for (auto cmd : m_options.m_one_liner) 4719 new_hook_sp->GetCommandPointer()->AppendString(cmd.c_str()); 4720 result.AppendMessageWithFormat("Stop hook #%" PRIu64 " added.\n", 4721 new_hook_sp->GetID()); 4722 } else { 4723 m_stop_hook_sp = new_hook_sp; 4724 m_interpreter.GetLLDBCommandsFromIOHandler("> ", // Prompt 4725 *this); // IOHandlerDelegate 4726 } 4727 result.SetStatus(eReturnStatusSuccessFinishNoResult); 4728 4729 return result.Succeeded(); 4730 } 4731 4732 private: 4733 CommandOptions m_options; 4734 Target::StopHookSP m_stop_hook_sp; 4735 }; 4736 4737 #pragma mark CommandObjectTargetStopHookDelete 4738 4739 // CommandObjectTargetStopHookDelete 4740 4741 class CommandObjectTargetStopHookDelete : public CommandObjectParsed { 4742 public: 4743 CommandObjectTargetStopHookDelete(CommandInterpreter &interpreter) 4744 : CommandObjectParsed(interpreter, "target stop-hook delete", 4745 "Delete a stop-hook.", 4746 "target stop-hook delete [<idx>]") {} 4747 4748 ~CommandObjectTargetStopHookDelete() override = default; 4749 4750 void 4751 HandleArgumentCompletion(CompletionRequest &request, 4752 OptionElementVector &opt_element_vector) override { 4753 CommandCompletions::InvokeCommonCompletionCallbacks( 4754 GetCommandInterpreter(), CommandCompletions::eStopHookIDCompletion, 4755 request, nullptr); 4756 } 4757 4758 protected: 4759 bool DoExecute(Args &command, CommandReturnObject &result) override { 4760 Target &target = GetSelectedOrDummyTarget(); 4761 // FIXME: see if we can use the breakpoint id style parser? 4762 size_t num_args = command.GetArgumentCount(); 4763 if (num_args == 0) { 4764 if (!m_interpreter.Confirm("Delete all stop hooks?", true)) { 4765 result.SetStatus(eReturnStatusFailed); 4766 return false; 4767 } else { 4768 target.RemoveAllStopHooks(); 4769 } 4770 } else { 4771 for (size_t i = 0; i < num_args; i++) { 4772 lldb::user_id_t user_id; 4773 if (!llvm::to_integer(command.GetArgumentAtIndex(i), user_id)) { 4774 result.AppendErrorWithFormat("invalid stop hook id: \"%s\".\n", 4775 command.GetArgumentAtIndex(i)); 4776 result.SetStatus(eReturnStatusFailed); 4777 return false; 4778 } 4779 if (!target.RemoveStopHookByID(user_id)) { 4780 result.AppendErrorWithFormat("unknown stop hook id: \"%s\".\n", 4781 command.GetArgumentAtIndex(i)); 4782 result.SetStatus(eReturnStatusFailed); 4783 return false; 4784 } 4785 } 4786 } 4787 result.SetStatus(eReturnStatusSuccessFinishNoResult); 4788 return result.Succeeded(); 4789 } 4790 }; 4791 4792 #pragma mark CommandObjectTargetStopHookEnableDisable 4793 4794 // CommandObjectTargetStopHookEnableDisable 4795 4796 class CommandObjectTargetStopHookEnableDisable : public CommandObjectParsed { 4797 public: 4798 CommandObjectTargetStopHookEnableDisable(CommandInterpreter &interpreter, 4799 bool enable, const char *name, 4800 const char *help, const char *syntax) 4801 : CommandObjectParsed(interpreter, name, help, syntax), m_enable(enable) { 4802 } 4803 4804 ~CommandObjectTargetStopHookEnableDisable() override = default; 4805 4806 void 4807 HandleArgumentCompletion(CompletionRequest &request, 4808 OptionElementVector &opt_element_vector) override { 4809 if (request.GetCursorIndex()) 4810 return; 4811 CommandCompletions::InvokeCommonCompletionCallbacks( 4812 GetCommandInterpreter(), CommandCompletions::eStopHookIDCompletion, 4813 request, nullptr); 4814 } 4815 4816 protected: 4817 bool DoExecute(Args &command, CommandReturnObject &result) override { 4818 Target &target = GetSelectedOrDummyTarget(); 4819 // FIXME: see if we can use the breakpoint id style parser? 4820 size_t num_args = command.GetArgumentCount(); 4821 bool success; 4822 4823 if (num_args == 0) { 4824 target.SetAllStopHooksActiveState(m_enable); 4825 } else { 4826 for (size_t i = 0; i < num_args; i++) { 4827 lldb::user_id_t user_id; 4828 if (!llvm::to_integer(command.GetArgumentAtIndex(i), user_id)) { 4829 result.AppendErrorWithFormat("invalid stop hook id: \"%s\".\n", 4830 command.GetArgumentAtIndex(i)); 4831 result.SetStatus(eReturnStatusFailed); 4832 return false; 4833 } 4834 success = target.SetStopHookActiveStateByID(user_id, m_enable); 4835 if (!success) { 4836 result.AppendErrorWithFormat("unknown stop hook id: \"%s\".\n", 4837 command.GetArgumentAtIndex(i)); 4838 result.SetStatus(eReturnStatusFailed); 4839 return false; 4840 } 4841 } 4842 } 4843 result.SetStatus(eReturnStatusSuccessFinishNoResult); 4844 return result.Succeeded(); 4845 } 4846 4847 private: 4848 bool m_enable; 4849 }; 4850 4851 #pragma mark CommandObjectTargetStopHookList 4852 4853 // CommandObjectTargetStopHookList 4854 4855 class CommandObjectTargetStopHookList : public CommandObjectParsed { 4856 public: 4857 CommandObjectTargetStopHookList(CommandInterpreter &interpreter) 4858 : CommandObjectParsed(interpreter, "target stop-hook list", 4859 "List all stop-hooks.", 4860 "target stop-hook list [<type>]") {} 4861 4862 ~CommandObjectTargetStopHookList() override = default; 4863 4864 protected: 4865 bool DoExecute(Args &command, CommandReturnObject &result) override { 4866 Target &target = GetSelectedOrDummyTarget(); 4867 4868 size_t num_hooks = target.GetNumStopHooks(); 4869 if (num_hooks == 0) { 4870 result.GetOutputStream().PutCString("No stop hooks.\n"); 4871 } else { 4872 for (size_t i = 0; i < num_hooks; i++) { 4873 Target::StopHookSP this_hook = target.GetStopHookAtIndex(i); 4874 if (i > 0) 4875 result.GetOutputStream().PutCString("\n"); 4876 this_hook->GetDescription(&(result.GetOutputStream()), 4877 eDescriptionLevelFull); 4878 } 4879 } 4880 result.SetStatus(eReturnStatusSuccessFinishResult); 4881 return result.Succeeded(); 4882 } 4883 }; 4884 4885 #pragma mark CommandObjectMultiwordTargetStopHooks 4886 4887 // CommandObjectMultiwordTargetStopHooks 4888 4889 class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword { 4890 public: 4891 CommandObjectMultiwordTargetStopHooks(CommandInterpreter &interpreter) 4892 : CommandObjectMultiword( 4893 interpreter, "target stop-hook", 4894 "Commands for operating on debugger target stop-hooks.", 4895 "target stop-hook <subcommand> [<subcommand-options>]") { 4896 LoadSubCommand("add", CommandObjectSP( 4897 new CommandObjectTargetStopHookAdd(interpreter))); 4898 LoadSubCommand( 4899 "delete", 4900 CommandObjectSP(new CommandObjectTargetStopHookDelete(interpreter))); 4901 LoadSubCommand("disable", 4902 CommandObjectSP(new CommandObjectTargetStopHookEnableDisable( 4903 interpreter, false, "target stop-hook disable [<id>]", 4904 "Disable a stop-hook.", "target stop-hook disable"))); 4905 LoadSubCommand("enable", 4906 CommandObjectSP(new CommandObjectTargetStopHookEnableDisable( 4907 interpreter, true, "target stop-hook enable [<id>]", 4908 "Enable a stop-hook.", "target stop-hook enable"))); 4909 LoadSubCommand("list", CommandObjectSP(new CommandObjectTargetStopHookList( 4910 interpreter))); 4911 } 4912 4913 ~CommandObjectMultiwordTargetStopHooks() override = default; 4914 }; 4915 4916 #pragma mark CommandObjectMultiwordTarget 4917 4918 // CommandObjectMultiwordTarget 4919 4920 CommandObjectMultiwordTarget::CommandObjectMultiwordTarget( 4921 CommandInterpreter &interpreter) 4922 : CommandObjectMultiword(interpreter, "target", 4923 "Commands for operating on debugger targets.", 4924 "target <subcommand> [<subcommand-options>]") { 4925 LoadSubCommand("create", 4926 CommandObjectSP(new CommandObjectTargetCreate(interpreter))); 4927 LoadSubCommand("delete", 4928 CommandObjectSP(new CommandObjectTargetDelete(interpreter))); 4929 LoadSubCommand("list", 4930 CommandObjectSP(new CommandObjectTargetList(interpreter))); 4931 LoadSubCommand("select", 4932 CommandObjectSP(new CommandObjectTargetSelect(interpreter))); 4933 LoadSubCommand("show-launch-environment", 4934 CommandObjectSP(new CommandObjectTargetShowLaunchEnvironment( 4935 interpreter))); 4936 LoadSubCommand( 4937 "stop-hook", 4938 CommandObjectSP(new CommandObjectMultiwordTargetStopHooks(interpreter))); 4939 LoadSubCommand("modules", 4940 CommandObjectSP(new CommandObjectTargetModules(interpreter))); 4941 LoadSubCommand("symbols", 4942 CommandObjectSP(new CommandObjectTargetSymbols(interpreter))); 4943 LoadSubCommand("variable", 4944 CommandObjectSP(new CommandObjectTargetVariable(interpreter))); 4945 } 4946 4947 CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget() = default; 4948