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