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