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