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 "CommandObjectTarget.h" 11 12 // C Includes 13 #include <errno.h> 14 15 // C++ Includes 16 // Other libraries and framework includes 17 // Project includes 18 #include "lldb/Interpreter/Args.h" 19 #include "lldb/Core/Debugger.h" 20 #include "lldb/Core/InputReader.h" 21 #include "lldb/Core/Section.h" 22 #include "lldb/Core/State.h" 23 #include "lldb/Core/Timer.h" 24 #include "lldb/Core/ValueObjectVariable.h" 25 #include "lldb/Interpreter/CommandInterpreter.h" 26 #include "lldb/Interpreter/CommandReturnObject.h" 27 #include "lldb/Interpreter/Options.h" 28 #include "lldb/Interpreter/OptionGroupArchitecture.h" 29 #include "lldb/Interpreter/OptionGroupBoolean.h" 30 #include "lldb/Interpreter/OptionGroupFile.h" 31 #include "lldb/Interpreter/OptionGroupFormat.h" 32 #include "lldb/Interpreter/OptionGroupVariable.h" 33 #include "lldb/Interpreter/OptionGroupPlatform.h" 34 #include "lldb/Interpreter/OptionGroupUInt64.h" 35 #include "lldb/Interpreter/OptionGroupUUID.h" 36 #include "lldb/Interpreter/OptionGroupValueObjectDisplay.h" 37 #include "lldb/Symbol/LineTable.h" 38 #include "lldb/Symbol/ObjectFile.h" 39 #include "lldb/Symbol/SymbolFile.h" 40 #include "lldb/Symbol/SymbolVendor.h" 41 #include "lldb/Symbol/VariableList.h" 42 #include "lldb/Target/Process.h" 43 #include "lldb/Target/StackFrame.h" 44 #include "lldb/Target/Thread.h" 45 #include "lldb/Target/ThreadSpec.h" 46 47 using namespace lldb; 48 using namespace lldb_private; 49 50 51 52 static void 53 DumpTargetInfo (uint32_t target_idx, Target *target, const char *prefix_cstr, bool show_stopped_process_status, Stream &strm) 54 { 55 const ArchSpec &target_arch = target->GetArchitecture(); 56 57 Module *exe_module = target->GetExecutableModulePointer(); 58 char exe_path[PATH_MAX]; 59 bool exe_valid = false; 60 if (exe_module) 61 exe_valid = exe_module->GetFileSpec().GetPath (exe_path, sizeof(exe_path)); 62 63 if (!exe_valid) 64 ::strcpy (exe_path, "<none>"); 65 66 strm.Printf ("%starget #%u: %s", prefix_cstr ? prefix_cstr : "", target_idx, exe_path); 67 68 uint32_t properties = 0; 69 if (target_arch.IsValid()) 70 { 71 strm.Printf ("%sarch=%s", properties++ > 0 ? ", " : " ( ", target_arch.GetTriple().str().c_str()); 72 properties++; 73 } 74 PlatformSP platform_sp (target->GetPlatform()); 75 if (platform_sp) 76 strm.Printf ("%splatform=%s", properties++ > 0 ? ", " : " ( ", platform_sp->GetName()); 77 78 ProcessSP process_sp (target->GetProcessSP()); 79 bool show_process_status = false; 80 if (process_sp) 81 { 82 lldb::pid_t pid = process_sp->GetID(); 83 StateType state = process_sp->GetState(); 84 if (show_stopped_process_status) 85 show_process_status = StateIsStoppedState(state, true); 86 const char *state_cstr = StateAsCString (state); 87 if (pid != LLDB_INVALID_PROCESS_ID) 88 strm.Printf ("%spid=%llu", properties++ > 0 ? ", " : " ( ", pid); 89 strm.Printf ("%sstate=%s", properties++ > 0 ? ", " : " ( ", state_cstr); 90 } 91 if (properties > 0) 92 strm.PutCString (" )\n"); 93 else 94 strm.EOL(); 95 if (show_process_status) 96 { 97 const bool only_threads_with_stop_reason = true; 98 const uint32_t start_frame = 0; 99 const uint32_t num_frames = 1; 100 const uint32_t num_frames_with_source = 1; 101 process_sp->GetStatus (strm); 102 process_sp->GetThreadStatus (strm, 103 only_threads_with_stop_reason, 104 start_frame, 105 num_frames, 106 num_frames_with_source); 107 108 } 109 } 110 111 static uint32_t 112 DumpTargetList (TargetList &target_list, bool show_stopped_process_status, Stream &strm) 113 { 114 const uint32_t num_targets = target_list.GetNumTargets(); 115 if (num_targets) 116 { 117 TargetSP selected_target_sp (target_list.GetSelectedTarget()); 118 strm.PutCString ("Current targets:\n"); 119 for (uint32_t i=0; i<num_targets; ++i) 120 { 121 TargetSP target_sp (target_list.GetTargetAtIndex (i)); 122 if (target_sp) 123 { 124 bool is_selected = target_sp.get() == selected_target_sp.get(); 125 DumpTargetInfo (i, 126 target_sp.get(), 127 is_selected ? "* " : " ", 128 show_stopped_process_status, 129 strm); 130 } 131 } 132 } 133 return num_targets; 134 } 135 #pragma mark CommandObjectTargetCreate 136 137 //------------------------------------------------------------------------- 138 // "target create" 139 //------------------------------------------------------------------------- 140 141 class CommandObjectTargetCreate : public CommandObject 142 { 143 public: 144 CommandObjectTargetCreate(CommandInterpreter &interpreter) : 145 CommandObject (interpreter, 146 "target create", 147 "Create a target using the argument as the main executable.", 148 NULL), 149 m_option_group (interpreter), 150 m_arch_option (), 151 m_platform_options(true), // Do include the "--platform" option in the platform settings by passing true 152 m_core_file (LLDB_OPT_SET_1, false, "core-file", 'c', 0, eArgTypePath, "Fullpath to a core file to use for this target.") 153 { 154 CommandArgumentEntry arg; 155 CommandArgumentData file_arg; 156 157 // Define the first (and only) variant of this arg. 158 file_arg.arg_type = eArgTypeFilename; 159 file_arg.arg_repetition = eArgRepeatPlain; 160 161 // There is only one variant this argument could be; put it into the argument entry. 162 arg.push_back (file_arg); 163 164 // Push the data for the first argument into the m_arguments vector. 165 m_arguments.push_back (arg); 166 167 m_option_group.Append (&m_arch_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 168 m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 169 m_option_group.Append (&m_core_file, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 170 m_option_group.Finalize(); 171 } 172 173 ~CommandObjectTargetCreate () 174 { 175 } 176 177 Options * 178 GetOptions () 179 { 180 return &m_option_group; 181 } 182 183 bool 184 Execute (Args& command, CommandReturnObject &result) 185 { 186 const int argc = command.GetArgumentCount(); 187 FileSpec core_file (m_core_file.GetOptionValue().GetCurrentValue()); 188 189 if (argc == 1 || core_file) 190 { 191 const char *file_path = command.GetArgumentAtIndex(0); 192 Timer scoped_timer(__PRETTY_FUNCTION__, "(lldb) target create '%s'", file_path); 193 FileSpec file_spec; 194 195 if (file_path) 196 file_spec.SetFile (file_path, true); 197 198 TargetSP target_sp; 199 Debugger &debugger = m_interpreter.GetDebugger(); 200 const char *arch_cstr = m_arch_option.GetArchitectureName(); 201 const bool get_dependent_files = true; 202 Error error (debugger.GetTargetList().CreateTarget (debugger, 203 file_spec, 204 arch_cstr, 205 get_dependent_files, 206 &m_platform_options, 207 target_sp)); 208 209 if (target_sp) 210 { 211 debugger.GetTargetList().SetSelectedTarget(target_sp.get()); 212 if (core_file) 213 { 214 ProcessSP process_sp (target_sp->CreateProcess (m_interpreter.GetDebugger().GetListener(), NULL, &core_file)); 215 char core_path[PATH_MAX]; 216 core_file.GetPath(core_path, sizeof(core_path)); 217 218 if (process_sp) 219 { 220 // Seems wierd that we Launch a core file, but that is 221 // what we do! 222 error = process_sp->LoadCore(); 223 224 if (error.Fail()) 225 { 226 result.AppendError(error.AsCString("can't find plug-in for core file")); 227 result.SetStatus (eReturnStatusFailed); 228 return false; 229 } 230 else 231 { 232 result.AppendMessageWithFormat ("Core file '%s' (%s) was loaded.\n", core_path, target_sp->GetArchitecture().GetArchitectureName()); 233 result.SetStatus (eReturnStatusSuccessFinishNoResult); 234 } 235 } 236 else 237 { 238 result.AppendErrorWithFormat ("Unable to find process plug-in for core file '%s'\n", core_path); 239 result.SetStatus (eReturnStatusFailed); 240 } 241 } 242 else 243 { 244 result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, target_sp->GetArchitecture().GetArchitectureName()); 245 result.SetStatus (eReturnStatusSuccessFinishNoResult); 246 } 247 } 248 else 249 { 250 result.AppendError(error.AsCString()); 251 result.SetStatus (eReturnStatusFailed); 252 } 253 } 254 else 255 { 256 result.AppendErrorWithFormat("'%s' takes exactly one executable path argument, or use the --core-file option.\n", m_cmd_name.c_str()); 257 result.SetStatus (eReturnStatusFailed); 258 } 259 return result.Succeeded(); 260 261 } 262 263 int 264 HandleArgumentCompletion (Args &input, 265 int &cursor_index, 266 int &cursor_char_position, 267 OptionElementVector &opt_element_vector, 268 int match_start_point, 269 int max_return_elements, 270 bool &word_complete, 271 StringList &matches) 272 { 273 std::string completion_str (input.GetArgumentAtIndex(cursor_index)); 274 completion_str.erase (cursor_char_position); 275 276 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, 277 CommandCompletions::eDiskFileCompletion, 278 completion_str.c_str(), 279 match_start_point, 280 max_return_elements, 281 NULL, 282 word_complete, 283 matches); 284 return matches.GetSize(); 285 } 286 private: 287 OptionGroupOptions m_option_group; 288 OptionGroupArchitecture m_arch_option; 289 OptionGroupPlatform m_platform_options; 290 OptionGroupFile m_core_file; 291 292 }; 293 294 #pragma mark CommandObjectTargetList 295 296 //---------------------------------------------------------------------- 297 // "target list" 298 //---------------------------------------------------------------------- 299 300 class CommandObjectTargetList : public CommandObject 301 { 302 public: 303 CommandObjectTargetList (CommandInterpreter &interpreter) : 304 CommandObject (interpreter, 305 "target list", 306 "List all current targets in the current debug session.", 307 NULL, 308 0) 309 { 310 } 311 312 virtual 313 ~CommandObjectTargetList () 314 { 315 } 316 317 virtual bool 318 Execute (Args& args, CommandReturnObject &result) 319 { 320 if (args.GetArgumentCount() == 0) 321 { 322 Stream &strm = result.GetOutputStream(); 323 324 bool show_stopped_process_status = false; 325 if (DumpTargetList (m_interpreter.GetDebugger().GetTargetList(), show_stopped_process_status, strm) == 0) 326 { 327 strm.PutCString ("No targets.\n"); 328 } 329 result.SetStatus (eReturnStatusSuccessFinishResult); 330 } 331 else 332 { 333 result.AppendError ("the 'target list' command takes no arguments\n"); 334 result.SetStatus (eReturnStatusFailed); 335 } 336 return result.Succeeded(); 337 } 338 }; 339 340 341 #pragma mark CommandObjectTargetSelect 342 343 //---------------------------------------------------------------------- 344 // "target select" 345 //---------------------------------------------------------------------- 346 347 class CommandObjectTargetSelect : public CommandObject 348 { 349 public: 350 CommandObjectTargetSelect (CommandInterpreter &interpreter) : 351 CommandObject (interpreter, 352 "target select", 353 "Select a target as the current target by target index.", 354 NULL, 355 0) 356 { 357 } 358 359 virtual 360 ~CommandObjectTargetSelect () 361 { 362 } 363 364 virtual bool 365 Execute (Args& args, CommandReturnObject &result) 366 { 367 if (args.GetArgumentCount() == 1) 368 { 369 bool success = false; 370 const char *target_idx_arg = args.GetArgumentAtIndex(0); 371 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success); 372 if (success) 373 { 374 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList(); 375 const uint32_t num_targets = target_list.GetNumTargets(); 376 if (target_idx < num_targets) 377 { 378 TargetSP target_sp (target_list.GetTargetAtIndex (target_idx)); 379 if (target_sp) 380 { 381 Stream &strm = result.GetOutputStream(); 382 target_list.SetSelectedTarget (target_sp.get()); 383 bool show_stopped_process_status = false; 384 DumpTargetList (target_list, show_stopped_process_status, strm); 385 result.SetStatus (eReturnStatusSuccessFinishResult); 386 } 387 else 388 { 389 result.AppendErrorWithFormat ("target #%u is NULL in target list\n", target_idx); 390 result.SetStatus (eReturnStatusFailed); 391 } 392 } 393 else 394 { 395 result.AppendErrorWithFormat ("index %u is out of range, valid target indexes are 0 - %u\n", 396 target_idx, 397 num_targets - 1); 398 result.SetStatus (eReturnStatusFailed); 399 } 400 } 401 else 402 { 403 result.AppendErrorWithFormat("invalid index string value '%s'\n", target_idx_arg); 404 result.SetStatus (eReturnStatusFailed); 405 } 406 } 407 else 408 { 409 result.AppendError ("'target select' takes a single argument: a target index\n"); 410 result.SetStatus (eReturnStatusFailed); 411 } 412 return result.Succeeded(); 413 } 414 }; 415 416 #pragma mark CommandObjectTargetSelect 417 418 //---------------------------------------------------------------------- 419 // "target delete" 420 //---------------------------------------------------------------------- 421 422 class CommandObjectTargetDelete : public CommandObject 423 { 424 public: 425 CommandObjectTargetDelete (CommandInterpreter &interpreter) : 426 CommandObject (interpreter, 427 "target delete", 428 "Delete one or more targets by target index.", 429 NULL, 430 0), 431 m_option_group (interpreter), 432 m_cleanup_option (LLDB_OPT_SET_1, false, "clean", 'c', 0, eArgTypeNone, "Perform extra cleanup to minimize memory consumption after deleting the target.", false) 433 { 434 m_option_group.Append (&m_cleanup_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 435 m_option_group.Finalize(); 436 } 437 438 virtual 439 ~CommandObjectTargetDelete () 440 { 441 } 442 443 virtual bool 444 Execute (Args& args, CommandReturnObject &result) 445 { 446 const size_t argc = args.GetArgumentCount(); 447 std::vector<TargetSP> delete_target_list; 448 TargetList &target_list = m_interpreter.GetDebugger().GetTargetList(); 449 bool success = true; 450 TargetSP target_sp; 451 if (argc > 0) 452 { 453 const uint32_t num_targets = target_list.GetNumTargets(); 454 for (uint32_t arg_idx = 0; success && arg_idx < argc; ++arg_idx) 455 { 456 const char *target_idx_arg = args.GetArgumentAtIndex(arg_idx); 457 uint32_t target_idx = Args::StringToUInt32 (target_idx_arg, UINT32_MAX, 0, &success); 458 if (success) 459 { 460 if (target_idx < num_targets) 461 { 462 target_sp = target_list.GetTargetAtIndex (target_idx); 463 if (target_sp) 464 { 465 delete_target_list.push_back (target_sp); 466 continue; 467 } 468 } 469 result.AppendErrorWithFormat ("target index %u is out of range, valid target indexes are 0 - %u\n", 470 target_idx, 471 num_targets - 1); 472 result.SetStatus (eReturnStatusFailed); 473 success = false; 474 } 475 else 476 { 477 result.AppendErrorWithFormat("invalid target index '%s'\n", target_idx_arg); 478 result.SetStatus (eReturnStatusFailed); 479 success = false; 480 } 481 } 482 483 } 484 else 485 { 486 target_sp = target_list.GetSelectedTarget(); 487 if (target_sp) 488 { 489 delete_target_list.push_back (target_sp); 490 } 491 else 492 { 493 result.AppendErrorWithFormat("no target is currently selected\n"); 494 result.SetStatus (eReturnStatusFailed); 495 success = false; 496 } 497 } 498 if (success) 499 { 500 const size_t num_targets_to_delete = delete_target_list.size(); 501 for (size_t idx = 0; idx < num_targets_to_delete; ++idx) 502 { 503 target_sp = delete_target_list[idx]; 504 target_list.DeleteTarget(target_sp); 505 target_sp->Destroy(); 506 } 507 // If "--clean" was specified, prune any orphaned shared modules from 508 // the global shared module list 509 if (m_cleanup_option.GetOptionValue ()) 510 { 511 ModuleList::RemoveOrphanSharedModules(); 512 } 513 result.GetOutputStream().Printf("%u targets deleted.\n", (uint32_t)num_targets_to_delete); 514 result.SetStatus(eReturnStatusSuccessFinishResult); 515 } 516 517 return result.Succeeded(); 518 } 519 520 Options * 521 GetOptions () 522 { 523 return &m_option_group; 524 } 525 526 protected: 527 OptionGroupOptions m_option_group; 528 OptionGroupBoolean m_cleanup_option; 529 }; 530 531 532 #pragma mark CommandObjectTargetVariable 533 534 //---------------------------------------------------------------------- 535 // "target variable" 536 //---------------------------------------------------------------------- 537 538 class CommandObjectTargetVariable : public CommandObject 539 { 540 public: 541 CommandObjectTargetVariable (CommandInterpreter &interpreter) : 542 CommandObject (interpreter, 543 "target variable", 544 "Read global variable(s) prior to running your binary.", 545 NULL, 546 0), 547 m_option_group (interpreter), 548 m_option_variable (false), // Don't include frame options 549 m_option_format (eFormatDefault), 550 m_option_compile_units (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypePath, "A basename or fullpath to a file that contains global variables. This option can be specified multiple times."), 551 m_option_shared_libraries (LLDB_OPT_SET_1, false, "shlib",'s', 0, eArgTypePath, "A basename or fullpath to a shared library to use in the search for global variables. This option can be specified multiple times."), 552 m_varobj_options() 553 { 554 CommandArgumentEntry arg; 555 CommandArgumentData var_name_arg; 556 557 // Define the first (and only) variant of this arg. 558 var_name_arg.arg_type = eArgTypeVarName; 559 var_name_arg.arg_repetition = eArgRepeatPlus; 560 561 // There is only one variant this argument could be; put it into the argument entry. 562 arg.push_back (var_name_arg); 563 564 // Push the data for the first argument into the m_arguments vector. 565 m_arguments.push_back (arg); 566 567 m_option_group.Append (&m_varobj_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 568 m_option_group.Append (&m_option_variable, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 569 m_option_group.Append (&m_option_format, OptionGroupFormat::OPTION_GROUP_FORMAT | OptionGroupFormat::OPTION_GROUP_GDB_FMT, LLDB_OPT_SET_1); 570 m_option_group.Append (&m_option_compile_units, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 571 m_option_group.Append (&m_option_shared_libraries, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 572 m_option_group.Finalize(); 573 } 574 575 virtual 576 ~CommandObjectTargetVariable () 577 { 578 } 579 580 void 581 DumpValueObject (Stream &s, VariableSP &var_sp, ValueObjectSP &valobj_sp, const char *root_name) 582 { 583 ValueObject::DumpValueObjectOptions options; 584 585 options.SetPointerDepth(m_varobj_options.ptr_depth) 586 .SetMaximumDepth(m_varobj_options.max_depth) 587 .SetShowTypes(m_varobj_options.show_types) 588 .SetShowLocation(m_varobj_options.show_location) 589 .SetUseObjectiveC(m_varobj_options.use_objc) 590 .SetUseDynamicType(m_varobj_options.use_dynamic) 591 .SetUseSyntheticValue((lldb::SyntheticValueType)m_varobj_options.use_synth) 592 .SetFlatOutput(m_varobj_options.flat_output) 593 .SetOmitSummaryDepth(m_varobj_options.no_summary_depth) 594 .SetIgnoreCap(m_varobj_options.ignore_cap); 595 596 switch (var_sp->GetScope()) 597 { 598 case eValueTypeVariableGlobal: 599 if (m_option_variable.show_scope) 600 s.PutCString("GLOBAL: "); 601 break; 602 603 case eValueTypeVariableStatic: 604 if (m_option_variable.show_scope) 605 s.PutCString("STATIC: "); 606 break; 607 608 case eValueTypeVariableArgument: 609 if (m_option_variable.show_scope) 610 s.PutCString(" ARG: "); 611 break; 612 613 case eValueTypeVariableLocal: 614 if (m_option_variable.show_scope) 615 s.PutCString(" LOCAL: "); 616 break; 617 618 default: 619 break; 620 } 621 622 if (m_option_variable.show_decl) 623 { 624 bool show_fullpaths = false; 625 bool show_module = true; 626 if (var_sp->DumpDeclaration(&s, show_fullpaths, show_module)) 627 s.PutCString (": "); 628 } 629 630 const Format format = m_option_format.GetFormat(); 631 if (format != eFormatDefault) 632 valobj_sp->SetFormat (format); 633 634 ValueObject::DumpValueObject (s, 635 valobj_sp.get(), 636 root_name, 637 options, 638 format); 639 640 } 641 642 643 static uint32_t GetVariableCallback (void *baton, 644 const char *name, 645 VariableList &variable_list) 646 { 647 Target *target = static_cast<Target *>(baton); 648 if (target) 649 { 650 return target->GetImages().FindGlobalVariables (ConstString(name), 651 true, 652 UINT32_MAX, 653 variable_list); 654 } 655 return 0; 656 } 657 658 659 660 virtual bool 661 Execute (Args& args, CommandReturnObject &result) 662 { 663 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext()); 664 Target *target = exe_ctx.GetTargetPtr(); 665 if (target) 666 { 667 const size_t argc = args.GetArgumentCount(); 668 Stream &s = result.GetOutputStream(); 669 if (argc > 0) 670 { 671 672 for (size_t idx = 0; idx < argc; ++idx) 673 { 674 VariableList variable_list; 675 ValueObjectList valobj_list; 676 677 const char *arg = args.GetArgumentAtIndex(idx); 678 uint32_t matches = 0; 679 bool use_var_name = false; 680 if (m_option_variable.use_regex) 681 { 682 RegularExpression regex(arg); 683 if (!regex.IsValid ()) 684 { 685 result.GetErrorStream().Printf ("error: invalid regular expression: '%s'\n", arg); 686 result.SetStatus (eReturnStatusFailed); 687 return false; 688 } 689 use_var_name = true; 690 matches = target->GetImages().FindGlobalVariables (regex, 691 true, 692 UINT32_MAX, 693 variable_list); 694 } 695 else 696 { 697 Error error (Variable::GetValuesForVariableExpressionPath (arg, 698 exe_ctx.GetBestExecutionContextScope(), 699 GetVariableCallback, 700 target, 701 variable_list, 702 valobj_list)); 703 matches = variable_list.GetSize(); 704 } 705 706 if (matches == 0) 707 { 708 result.GetErrorStream().Printf ("error: can't find global variable '%s'\n", arg); 709 result.SetStatus (eReturnStatusFailed); 710 return false; 711 } 712 else 713 { 714 for (uint32_t global_idx=0; global_idx<matches; ++global_idx) 715 { 716 VariableSP var_sp (variable_list.GetVariableAtIndex(global_idx)); 717 if (var_sp) 718 { 719 ValueObjectSP valobj_sp (valobj_list.GetValueObjectAtIndex(global_idx)); 720 if (!valobj_sp) 721 valobj_sp = ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp); 722 723 if (valobj_sp) 724 DumpValueObject (s, var_sp, valobj_sp, use_var_name ? var_sp->GetName().GetCString() : arg); 725 } 726 } 727 } 728 } 729 } 730 else 731 { 732 bool success = false; 733 StackFrame *frame = exe_ctx.GetFramePtr(); 734 CompileUnit *comp_unit = NULL; 735 if (frame) 736 { 737 comp_unit = frame->GetSymbolContext (eSymbolContextCompUnit).comp_unit; 738 if (comp_unit) 739 { 740 const bool can_create = true; 741 VariableListSP comp_unit_varlist_sp (comp_unit->GetVariableList(can_create)); 742 if (comp_unit_varlist_sp) 743 { 744 size_t count = comp_unit_varlist_sp->GetSize(); 745 if (count > 0) 746 { 747 s.Printf ("Global variables for %s/%s:\n", 748 comp_unit->GetDirectory().GetCString(), 749 comp_unit->GetFilename().GetCString()); 750 751 success = true; 752 for (uint32_t i=0; i<count; ++i) 753 { 754 VariableSP var_sp (comp_unit_varlist_sp->GetVariableAtIndex(i)); 755 if (var_sp) 756 { 757 ValueObjectSP valobj_sp (ValueObjectVariable::Create (exe_ctx.GetBestExecutionContextScope(), var_sp)); 758 759 if (valobj_sp) 760 DumpValueObject (s, var_sp, valobj_sp, var_sp->GetName().GetCString()); 761 } 762 } 763 } 764 } 765 } 766 } 767 if (!success) 768 { 769 if (frame) 770 { 771 if (comp_unit) 772 result.AppendErrorWithFormat ("no global variables in current compile unit: %s/%s\n", 773 comp_unit->GetDirectory().GetCString(), 774 comp_unit->GetFilename().GetCString()); 775 else 776 result.AppendError ("no debug information for frame %u\n", frame->GetFrameIndex()); 777 } 778 else 779 result.AppendError ("'target variable' takes one or more global variable names as arguments\n"); 780 result.SetStatus (eReturnStatusFailed); 781 } 782 } 783 } 784 else 785 { 786 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 787 result.SetStatus (eReturnStatusFailed); 788 return false; 789 } 790 791 if (m_interpreter.TruncationWarningNecessary()) 792 { 793 result.GetOutputStream().Printf(m_interpreter.TruncationWarningText(), 794 m_cmd_name.c_str()); 795 m_interpreter.TruncationWarningGiven(); 796 } 797 798 return result.Succeeded(); 799 } 800 801 Options * 802 GetOptions () 803 { 804 return &m_option_group; 805 } 806 807 protected: 808 OptionGroupOptions m_option_group; 809 OptionGroupVariable m_option_variable; 810 OptionGroupFormat m_option_format; 811 OptionGroupFileList m_option_compile_units; 812 OptionGroupFileList m_option_shared_libraries; 813 OptionGroupValueObjectDisplay m_varobj_options; 814 815 }; 816 817 818 #pragma mark CommandObjectTargetModulesSearchPathsAdd 819 820 class CommandObjectTargetModulesSearchPathsAdd : public CommandObject 821 { 822 public: 823 824 CommandObjectTargetModulesSearchPathsAdd (CommandInterpreter &interpreter) : 825 CommandObject (interpreter, 826 "target modules search-paths add", 827 "Add new image search paths substitution pairs to the current target.", 828 NULL) 829 { 830 CommandArgumentEntry arg; 831 CommandArgumentData old_prefix_arg; 832 CommandArgumentData new_prefix_arg; 833 834 // Define the first variant of this arg pair. 835 old_prefix_arg.arg_type = eArgTypeOldPathPrefix; 836 old_prefix_arg.arg_repetition = eArgRepeatPairPlus; 837 838 // Define the first variant of this arg pair. 839 new_prefix_arg.arg_type = eArgTypeNewPathPrefix; 840 new_prefix_arg.arg_repetition = eArgRepeatPairPlus; 841 842 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they 843 // must always occur together, they are treated as two variants of one argument rather than two independent 844 // arguments. Push them both into the first argument position for m_arguments... 845 846 arg.push_back (old_prefix_arg); 847 arg.push_back (new_prefix_arg); 848 849 m_arguments.push_back (arg); 850 } 851 852 ~CommandObjectTargetModulesSearchPathsAdd () 853 { 854 } 855 856 bool 857 Execute (Args& command, 858 CommandReturnObject &result) 859 { 860 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 861 if (target) 862 { 863 uint32_t argc = command.GetArgumentCount(); 864 if (argc & 1) 865 { 866 result.AppendError ("add requires an even number of arguments\n"); 867 result.SetStatus (eReturnStatusFailed); 868 } 869 else 870 { 871 for (uint32_t i=0; i<argc; i+=2) 872 { 873 const char *from = command.GetArgumentAtIndex(i); 874 const char *to = command.GetArgumentAtIndex(i+1); 875 876 if (from[0] && to[0]) 877 { 878 bool last_pair = ((argc - i) == 2); 879 target->GetImageSearchPathList().Append (ConstString(from), 880 ConstString(to), 881 last_pair); // Notify if this is the last pair 882 result.SetStatus (eReturnStatusSuccessFinishNoResult); 883 } 884 else 885 { 886 if (from[0]) 887 result.AppendError ("<path-prefix> can't be empty\n"); 888 else 889 result.AppendError ("<new-path-prefix> can't be empty\n"); 890 result.SetStatus (eReturnStatusFailed); 891 } 892 } 893 } 894 } 895 else 896 { 897 result.AppendError ("invalid target\n"); 898 result.SetStatus (eReturnStatusFailed); 899 } 900 return result.Succeeded(); 901 } 902 }; 903 904 #pragma mark CommandObjectTargetModulesSearchPathsClear 905 906 class CommandObjectTargetModulesSearchPathsClear : public CommandObject 907 { 908 public: 909 910 CommandObjectTargetModulesSearchPathsClear (CommandInterpreter &interpreter) : 911 CommandObject (interpreter, 912 "target modules search-paths clear", 913 "Clear all current image search path substitution pairs from the current target.", 914 "target modules search-paths clear") 915 { 916 } 917 918 ~CommandObjectTargetModulesSearchPathsClear () 919 { 920 } 921 922 bool 923 Execute (Args& command, 924 CommandReturnObject &result) 925 { 926 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 927 if (target) 928 { 929 bool notify = true; 930 target->GetImageSearchPathList().Clear(notify); 931 result.SetStatus (eReturnStatusSuccessFinishNoResult); 932 } 933 else 934 { 935 result.AppendError ("invalid target\n"); 936 result.SetStatus (eReturnStatusFailed); 937 } 938 return result.Succeeded(); 939 } 940 }; 941 942 #pragma mark CommandObjectTargetModulesSearchPathsInsert 943 944 class CommandObjectTargetModulesSearchPathsInsert : public CommandObject 945 { 946 public: 947 948 CommandObjectTargetModulesSearchPathsInsert (CommandInterpreter &interpreter) : 949 CommandObject (interpreter, 950 "target modules search-paths insert", 951 "Insert a new image search path substitution pair into the current target at the specified index.", 952 NULL) 953 { 954 CommandArgumentEntry arg1; 955 CommandArgumentEntry arg2; 956 CommandArgumentData index_arg; 957 CommandArgumentData old_prefix_arg; 958 CommandArgumentData new_prefix_arg; 959 960 // Define the first and only variant of this arg. 961 index_arg.arg_type = eArgTypeIndex; 962 index_arg.arg_repetition = eArgRepeatPlain; 963 964 // Put the one and only variant into the first arg for m_arguments: 965 arg1.push_back (index_arg); 966 967 // Define the first variant of this arg pair. 968 old_prefix_arg.arg_type = eArgTypeOldPathPrefix; 969 old_prefix_arg.arg_repetition = eArgRepeatPairPlus; 970 971 // Define the first variant of this arg pair. 972 new_prefix_arg.arg_type = eArgTypeNewPathPrefix; 973 new_prefix_arg.arg_repetition = eArgRepeatPairPlus; 974 975 // There are two required arguments that must always occur together, i.e. an argument "pair". Because they 976 // must always occur together, they are treated as two variants of one argument rather than two independent 977 // arguments. Push them both into the same argument position for m_arguments... 978 979 arg2.push_back (old_prefix_arg); 980 arg2.push_back (new_prefix_arg); 981 982 // Add arguments to m_arguments. 983 m_arguments.push_back (arg1); 984 m_arguments.push_back (arg2); 985 } 986 987 ~CommandObjectTargetModulesSearchPathsInsert () 988 { 989 } 990 991 bool 992 Execute (Args& command, 993 CommandReturnObject &result) 994 { 995 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 996 if (target) 997 { 998 uint32_t argc = command.GetArgumentCount(); 999 // check for at least 3 arguments and an odd nubmer of parameters 1000 if (argc >= 3 && argc & 1) 1001 { 1002 bool success = false; 1003 1004 uint32_t insert_idx = Args::StringToUInt32(command.GetArgumentAtIndex(0), UINT32_MAX, 0, &success); 1005 1006 if (!success) 1007 { 1008 result.AppendErrorWithFormat("<index> parameter is not an integer: '%s'.\n", command.GetArgumentAtIndex(0)); 1009 result.SetStatus (eReturnStatusFailed); 1010 return result.Succeeded(); 1011 } 1012 1013 // shift off the index 1014 command.Shift(); 1015 argc = command.GetArgumentCount(); 1016 1017 for (uint32_t i=0; i<argc; i+=2, ++insert_idx) 1018 { 1019 const char *from = command.GetArgumentAtIndex(i); 1020 const char *to = command.GetArgumentAtIndex(i+1); 1021 1022 if (from[0] && to[0]) 1023 { 1024 bool last_pair = ((argc - i) == 2); 1025 target->GetImageSearchPathList().Insert (ConstString(from), 1026 ConstString(to), 1027 insert_idx, 1028 last_pair); 1029 result.SetStatus (eReturnStatusSuccessFinishNoResult); 1030 } 1031 else 1032 { 1033 if (from[0]) 1034 result.AppendError ("<path-prefix> can't be empty\n"); 1035 else 1036 result.AppendError ("<new-path-prefix> can't be empty\n"); 1037 result.SetStatus (eReturnStatusFailed); 1038 return false; 1039 } 1040 } 1041 } 1042 else 1043 { 1044 result.AppendError ("insert requires at least three arguments\n"); 1045 result.SetStatus (eReturnStatusFailed); 1046 return result.Succeeded(); 1047 } 1048 1049 } 1050 else 1051 { 1052 result.AppendError ("invalid target\n"); 1053 result.SetStatus (eReturnStatusFailed); 1054 } 1055 return result.Succeeded(); 1056 } 1057 }; 1058 1059 1060 #pragma mark CommandObjectTargetModulesSearchPathsList 1061 1062 1063 class CommandObjectTargetModulesSearchPathsList : public CommandObject 1064 { 1065 public: 1066 1067 CommandObjectTargetModulesSearchPathsList (CommandInterpreter &interpreter) : 1068 CommandObject (interpreter, 1069 "target modules search-paths list", 1070 "List all current image search path substitution pairs in the current target.", 1071 "target modules search-paths list") 1072 { 1073 } 1074 1075 ~CommandObjectTargetModulesSearchPathsList () 1076 { 1077 } 1078 1079 bool 1080 Execute (Args& command, 1081 CommandReturnObject &result) 1082 { 1083 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 1084 if (target) 1085 { 1086 if (command.GetArgumentCount() != 0) 1087 { 1088 result.AppendError ("list takes no arguments\n"); 1089 result.SetStatus (eReturnStatusFailed); 1090 return result.Succeeded(); 1091 } 1092 1093 target->GetImageSearchPathList().Dump(&result.GetOutputStream()); 1094 result.SetStatus (eReturnStatusSuccessFinishResult); 1095 } 1096 else 1097 { 1098 result.AppendError ("invalid target\n"); 1099 result.SetStatus (eReturnStatusFailed); 1100 } 1101 return result.Succeeded(); 1102 } 1103 }; 1104 1105 #pragma mark CommandObjectTargetModulesSearchPathsQuery 1106 1107 class CommandObjectTargetModulesSearchPathsQuery : public CommandObject 1108 { 1109 public: 1110 1111 CommandObjectTargetModulesSearchPathsQuery (CommandInterpreter &interpreter) : 1112 CommandObject (interpreter, 1113 "target modules search-paths query", 1114 "Transform a path using the first applicable image search path.", 1115 NULL) 1116 { 1117 CommandArgumentEntry arg; 1118 CommandArgumentData path_arg; 1119 1120 // Define the first (and only) variant of this arg. 1121 path_arg.arg_type = eArgTypePath; 1122 path_arg.arg_repetition = eArgRepeatPlain; 1123 1124 // There is only one variant this argument could be; put it into the argument entry. 1125 arg.push_back (path_arg); 1126 1127 // Push the data for the first argument into the m_arguments vector. 1128 m_arguments.push_back (arg); 1129 } 1130 1131 ~CommandObjectTargetModulesSearchPathsQuery () 1132 { 1133 } 1134 1135 bool 1136 Execute (Args& command, 1137 CommandReturnObject &result) 1138 { 1139 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 1140 if (target) 1141 { 1142 if (command.GetArgumentCount() != 1) 1143 { 1144 result.AppendError ("query requires one argument\n"); 1145 result.SetStatus (eReturnStatusFailed); 1146 return result.Succeeded(); 1147 } 1148 1149 ConstString orig(command.GetArgumentAtIndex(0)); 1150 ConstString transformed; 1151 if (target->GetImageSearchPathList().RemapPath(orig, transformed)) 1152 result.GetOutputStream().Printf("%s\n", transformed.GetCString()); 1153 else 1154 result.GetOutputStream().Printf("%s\n", orig.GetCString()); 1155 1156 result.SetStatus (eReturnStatusSuccessFinishResult); 1157 } 1158 else 1159 { 1160 result.AppendError ("invalid target\n"); 1161 result.SetStatus (eReturnStatusFailed); 1162 } 1163 return result.Succeeded(); 1164 } 1165 }; 1166 1167 //---------------------------------------------------------------------- 1168 // Static Helper functions 1169 //---------------------------------------------------------------------- 1170 static void 1171 DumpModuleArchitecture (Stream &strm, Module *module, bool full_triple, uint32_t width) 1172 { 1173 if (module) 1174 { 1175 const char *arch_cstr; 1176 if (full_triple) 1177 arch_cstr = module->GetArchitecture().GetTriple().str().c_str(); 1178 else 1179 arch_cstr = module->GetArchitecture().GetArchitectureName(); 1180 if (width) 1181 strm.Printf("%-*s", width, arch_cstr); 1182 else 1183 strm.PutCString(arch_cstr); 1184 } 1185 } 1186 1187 static void 1188 DumpModuleUUID (Stream &strm, Module *module) 1189 { 1190 if (module->GetUUID().IsValid()) 1191 module->GetUUID().Dump (&strm); 1192 else 1193 strm.PutCString(" "); 1194 } 1195 1196 static uint32_t 1197 DumpCompileUnitLineTable 1198 ( 1199 CommandInterpreter &interpreter, 1200 Stream &strm, 1201 Module *module, 1202 const FileSpec &file_spec, 1203 bool load_addresses 1204 ) 1205 { 1206 uint32_t num_matches = 0; 1207 if (module) 1208 { 1209 SymbolContextList sc_list; 1210 num_matches = module->ResolveSymbolContextsForFileSpec (file_spec, 1211 0, 1212 false, 1213 eSymbolContextCompUnit, 1214 sc_list); 1215 1216 for (uint32_t i=0; i<num_matches; ++i) 1217 { 1218 SymbolContext sc; 1219 if (sc_list.GetContextAtIndex(i, sc)) 1220 { 1221 if (i > 0) 1222 strm << "\n\n"; 1223 1224 strm << "Line table for " << *static_cast<FileSpec*> (sc.comp_unit) << " in `" 1225 << module->GetFileSpec().GetFilename() << "\n"; 1226 LineTable *line_table = sc.comp_unit->GetLineTable(); 1227 if (line_table) 1228 line_table->GetDescription (&strm, 1229 interpreter.GetExecutionContext().GetTargetPtr(), 1230 lldb::eDescriptionLevelBrief); 1231 else 1232 strm << "No line table"; 1233 } 1234 } 1235 } 1236 return num_matches; 1237 } 1238 1239 static void 1240 DumpFullpath (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width) 1241 { 1242 if (file_spec_ptr) 1243 { 1244 if (width > 0) 1245 { 1246 char fullpath[PATH_MAX]; 1247 if (file_spec_ptr->GetPath(fullpath, sizeof(fullpath))) 1248 { 1249 strm.Printf("%-*s", width, fullpath); 1250 return; 1251 } 1252 } 1253 else 1254 { 1255 file_spec_ptr->Dump(&strm); 1256 return; 1257 } 1258 } 1259 // Keep the width spacing correct if things go wrong... 1260 if (width > 0) 1261 strm.Printf("%-*s", width, ""); 1262 } 1263 1264 static void 1265 DumpDirectory (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width) 1266 { 1267 if (file_spec_ptr) 1268 { 1269 if (width > 0) 1270 strm.Printf("%-*s", width, file_spec_ptr->GetDirectory().AsCString("")); 1271 else 1272 file_spec_ptr->GetDirectory().Dump(&strm); 1273 return; 1274 } 1275 // Keep the width spacing correct if things go wrong... 1276 if (width > 0) 1277 strm.Printf("%-*s", width, ""); 1278 } 1279 1280 static void 1281 DumpBasename (Stream &strm, const FileSpec *file_spec_ptr, uint32_t width) 1282 { 1283 if (file_spec_ptr) 1284 { 1285 if (width > 0) 1286 strm.Printf("%-*s", width, file_spec_ptr->GetFilename().AsCString("")); 1287 else 1288 file_spec_ptr->GetFilename().Dump(&strm); 1289 return; 1290 } 1291 // Keep the width spacing correct if things go wrong... 1292 if (width > 0) 1293 strm.Printf("%-*s", width, ""); 1294 } 1295 1296 1297 static void 1298 DumpModuleSymtab (CommandInterpreter &interpreter, Stream &strm, Module *module, SortOrder sort_order) 1299 { 1300 if (module) 1301 { 1302 ObjectFile *objfile = module->GetObjectFile (); 1303 if (objfile) 1304 { 1305 Symtab *symtab = objfile->GetSymtab(); 1306 if (symtab) 1307 symtab->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), sort_order); 1308 } 1309 } 1310 } 1311 1312 static void 1313 DumpModuleSections (CommandInterpreter &interpreter, Stream &strm, Module *module) 1314 { 1315 if (module) 1316 { 1317 ObjectFile *objfile = module->GetObjectFile (); 1318 if (objfile) 1319 { 1320 SectionList *section_list = objfile->GetSectionList(); 1321 if (section_list) 1322 { 1323 strm.PutCString ("Sections for '"); 1324 strm << module->GetFileSpec(); 1325 if (module->GetObjectName()) 1326 strm << '(' << module->GetObjectName() << ')'; 1327 strm.Printf ("' (%s):\n", module->GetArchitecture().GetArchitectureName()); 1328 strm.IndentMore(); 1329 section_list->Dump(&strm, interpreter.GetExecutionContext().GetTargetPtr(), true, UINT32_MAX); 1330 strm.IndentLess(); 1331 } 1332 } 1333 } 1334 } 1335 1336 static bool 1337 DumpModuleSymbolVendor (Stream &strm, Module *module) 1338 { 1339 if (module) 1340 { 1341 SymbolVendor *symbol_vendor = module->GetSymbolVendor(true); 1342 if (symbol_vendor) 1343 { 1344 symbol_vendor->Dump(&strm); 1345 return true; 1346 } 1347 } 1348 return false; 1349 } 1350 1351 static bool 1352 LookupAddressInModule 1353 ( 1354 CommandInterpreter &interpreter, 1355 Stream &strm, 1356 Module *module, 1357 uint32_t resolve_mask, 1358 lldb::addr_t raw_addr, 1359 lldb::addr_t offset, 1360 bool verbose 1361 ) 1362 { 1363 if (module) 1364 { 1365 lldb::addr_t addr = raw_addr - offset; 1366 Address so_addr; 1367 SymbolContext sc; 1368 Target *target = interpreter.GetExecutionContext().GetTargetPtr(); 1369 if (target && !target->GetSectionLoadList().IsEmpty()) 1370 { 1371 if (!target->GetSectionLoadList().ResolveLoadAddress (addr, so_addr)) 1372 return false; 1373 else if (so_addr.GetModulePtr() != module) 1374 return false; 1375 } 1376 else 1377 { 1378 if (!module->ResolveFileAddress (addr, so_addr)) 1379 return false; 1380 } 1381 1382 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope(); 1383 strm.IndentMore(); 1384 strm.Indent (" Address: "); 1385 so_addr.Dump (&strm, exe_scope, Address::DumpStyleModuleWithFileAddress); 1386 strm.PutCString (" ("); 1387 so_addr.Dump (&strm, exe_scope, Address::DumpStyleSectionNameOffset); 1388 strm.PutCString (")\n"); 1389 strm.Indent (" Summary: "); 1390 const uint32_t save_indent = strm.GetIndentLevel (); 1391 strm.SetIndentLevel (save_indent + 13); 1392 so_addr.Dump (&strm, exe_scope, Address::DumpStyleResolvedDescription); 1393 strm.SetIndentLevel (save_indent); 1394 // Print out detailed address information when verbose is enabled 1395 if (verbose) 1396 { 1397 strm.EOL(); 1398 so_addr.Dump (&strm, exe_scope, Address::DumpStyleDetailedSymbolContext); 1399 } 1400 strm.IndentLess(); 1401 return true; 1402 } 1403 1404 return false; 1405 } 1406 1407 static uint32_t 1408 LookupSymbolInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex) 1409 { 1410 if (module) 1411 { 1412 SymbolContext sc; 1413 1414 ObjectFile *objfile = module->GetObjectFile (); 1415 if (objfile) 1416 { 1417 Symtab *symtab = objfile->GetSymtab(); 1418 if (symtab) 1419 { 1420 uint32_t i; 1421 std::vector<uint32_t> match_indexes; 1422 ConstString symbol_name (name); 1423 uint32_t num_matches = 0; 1424 if (name_is_regex) 1425 { 1426 RegularExpression name_regexp(name); 1427 num_matches = symtab->AppendSymbolIndexesMatchingRegExAndType (name_regexp, 1428 eSymbolTypeAny, 1429 match_indexes); 1430 } 1431 else 1432 { 1433 num_matches = symtab->AppendSymbolIndexesWithName (symbol_name, match_indexes); 1434 } 1435 1436 1437 if (num_matches > 0) 1438 { 1439 strm.Indent (); 1440 strm.Printf("%u symbols match %s'%s' in ", num_matches, 1441 name_is_regex ? "the regular expression " : "", name); 1442 DumpFullpath (strm, &module->GetFileSpec(), 0); 1443 strm.PutCString(":\n"); 1444 strm.IndentMore (); 1445 Symtab::DumpSymbolHeader (&strm); 1446 for (i=0; i < num_matches; ++i) 1447 { 1448 Symbol *symbol = symtab->SymbolAtIndex(match_indexes[i]); 1449 strm.Indent (); 1450 symbol->Dump (&strm, interpreter.GetExecutionContext().GetTargetPtr(), i); 1451 } 1452 strm.IndentLess (); 1453 return num_matches; 1454 } 1455 } 1456 } 1457 } 1458 return 0; 1459 } 1460 1461 1462 static void 1463 DumpSymbolContextList (CommandInterpreter &interpreter, Stream &strm, SymbolContextList &sc_list, bool prepend_addr, bool verbose) 1464 { 1465 strm.IndentMore (); 1466 uint32_t i; 1467 const uint32_t num_matches = sc_list.GetSize(); 1468 1469 for (i=0; i<num_matches; ++i) 1470 { 1471 SymbolContext sc; 1472 if (sc_list.GetContextAtIndex(i, sc)) 1473 { 1474 strm.Indent(); 1475 ExecutionContextScope *exe_scope = interpreter.GetExecutionContext().GetBestExecutionContextScope (); 1476 1477 if (prepend_addr) 1478 { 1479 if (sc.line_entry.range.GetBaseAddress().IsValid()) 1480 { 1481 sc.line_entry.range.GetBaseAddress().Dump (&strm, 1482 exe_scope, 1483 Address::DumpStyleLoadAddress, 1484 Address::DumpStyleModuleWithFileAddress); 1485 strm.PutCString(" in "); 1486 } 1487 } 1488 sc.DumpStopContext(&strm, 1489 exe_scope, 1490 sc.line_entry.range.GetBaseAddress(), 1491 true, 1492 true, 1493 false); 1494 strm.EOL(); 1495 if (verbose) 1496 { 1497 if (sc.line_entry.range.GetBaseAddress().IsValid()) 1498 { 1499 if (sc.line_entry.range.GetBaseAddress().Dump (&strm, 1500 exe_scope, 1501 Address::DumpStyleDetailedSymbolContext)) 1502 strm.PutCString("\n\n"); 1503 } 1504 else if (sc.function->GetAddressRange().GetBaseAddress().IsValid()) 1505 { 1506 if (sc.function->GetAddressRange().GetBaseAddress().Dump (&strm, 1507 exe_scope, 1508 Address::DumpStyleDetailedSymbolContext)) 1509 strm.PutCString("\n\n"); 1510 } 1511 } 1512 } 1513 } 1514 strm.IndentLess (); 1515 } 1516 1517 static uint32_t 1518 LookupFunctionInModule (CommandInterpreter &interpreter, Stream &strm, Module *module, const char *name, bool name_is_regex, bool verbose) 1519 { 1520 if (module && name && name[0]) 1521 { 1522 SymbolContextList sc_list; 1523 const bool include_symbols = false; 1524 const bool include_inlines = true; 1525 const bool append = true; 1526 uint32_t num_matches = 0; 1527 if (name_is_regex) 1528 { 1529 RegularExpression function_name_regex (name); 1530 num_matches = module->FindFunctions (function_name_regex, 1531 include_symbols, 1532 include_inlines, 1533 append, 1534 sc_list); 1535 } 1536 else 1537 { 1538 ConstString function_name (name); 1539 num_matches = module->FindFunctions (function_name, 1540 NULL, 1541 eFunctionNameTypeBase | eFunctionNameTypeFull | eFunctionNameTypeMethod | eFunctionNameTypeSelector, 1542 include_symbols, 1543 include_inlines, 1544 append, 1545 sc_list); 1546 } 1547 1548 if (num_matches) 1549 { 1550 strm.Indent (); 1551 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : ""); 1552 DumpFullpath (strm, &module->GetFileSpec(), 0); 1553 strm.PutCString(":\n"); 1554 DumpSymbolContextList (interpreter, strm, sc_list, true, verbose); 1555 } 1556 return num_matches; 1557 } 1558 return 0; 1559 } 1560 1561 static uint32_t 1562 LookupTypeInModule (CommandInterpreter &interpreter, 1563 Stream &strm, 1564 Module *module, 1565 const char *name_cstr, 1566 bool name_is_regex) 1567 { 1568 if (module && name_cstr && name_cstr[0]) 1569 { 1570 TypeList type_list; 1571 const uint32_t max_num_matches = 1; 1572 uint32_t num_matches = 0; 1573 SymbolContext sc; 1574 1575 ConstString name(name_cstr); 1576 num_matches = module->FindTypes(sc, name, NULL, true, max_num_matches, type_list); 1577 1578 if (num_matches) 1579 { 1580 strm.Indent (); 1581 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : ""); 1582 DumpFullpath (strm, &module->GetFileSpec(), 0); 1583 strm.PutCString(":\n"); 1584 const uint32_t num_types = type_list.GetSize(); 1585 for (uint32_t i=0; i<num_types; ++i) 1586 { 1587 TypeSP type_sp (type_list.GetTypeAtIndex(i)); 1588 if (type_sp) 1589 { 1590 // Resolve the clang type so that any forward references 1591 // to types that haven't yet been parsed will get parsed. 1592 type_sp->GetClangFullType (); 1593 type_sp->GetDescription (&strm, eDescriptionLevelFull, true); 1594 } 1595 strm.EOL(); 1596 } 1597 } 1598 return num_matches; 1599 } 1600 return 0; 1601 } 1602 1603 static uint32_t 1604 LookupFileAndLineInModule (CommandInterpreter &interpreter, 1605 Stream &strm, 1606 Module *module, 1607 const FileSpec &file_spec, 1608 uint32_t line, 1609 bool check_inlines, 1610 bool verbose) 1611 { 1612 if (module && file_spec) 1613 { 1614 SymbolContextList sc_list; 1615 const uint32_t num_matches = module->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines, 1616 eSymbolContextEverything, sc_list); 1617 if (num_matches > 0) 1618 { 1619 strm.Indent (); 1620 strm.Printf("%u match%s found in ", num_matches, num_matches > 1 ? "es" : ""); 1621 strm << file_spec; 1622 if (line > 0) 1623 strm.Printf (":%u", line); 1624 strm << " in "; 1625 DumpFullpath (strm, &module->GetFileSpec(), 0); 1626 strm.PutCString(":\n"); 1627 DumpSymbolContextList (interpreter, strm, sc_list, true, verbose); 1628 return num_matches; 1629 } 1630 } 1631 return 0; 1632 1633 } 1634 1635 1636 static size_t 1637 FindModulesByName (Target *target, 1638 const char *module_name, 1639 ModuleList &module_list, 1640 bool check_global_list) 1641 { 1642 // Dump specified images (by basename or fullpath) 1643 FileSpec module_file_spec(module_name, false); 1644 1645 const size_t initial_size = module_list.GetSize (); 1646 1647 size_t num_matches = 0; 1648 1649 if (target) 1650 { 1651 num_matches = target->GetImages().FindModules (&module_file_spec, 1652 NULL, 1653 NULL, 1654 NULL, 1655 module_list); 1656 1657 // Not found in our module list for our target, check the main 1658 // shared module list in case it is a extra file used somewhere 1659 // else 1660 if (num_matches == 0) 1661 num_matches = ModuleList::FindSharedModules (module_file_spec, 1662 target->GetArchitecture(), 1663 NULL, 1664 NULL, 1665 module_list); 1666 } 1667 else 1668 { 1669 num_matches = ModuleList::FindSharedModules (module_file_spec, 1670 ArchSpec(), 1671 NULL, 1672 NULL, 1673 module_list); 1674 } 1675 1676 if (check_global_list && num_matches == 0) 1677 { 1678 // Check the global list 1679 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex()); 1680 const uint32_t num_modules = Module::GetNumberAllocatedModules(); 1681 ModuleSP module_sp; 1682 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx) 1683 { 1684 Module *module = Module::GetAllocatedModuleAtIndex(image_idx); 1685 1686 if (module) 1687 { 1688 if (FileSpec::Equal(module->GetFileSpec(), module_file_spec, true)) 1689 { 1690 module_sp = module->shared_from_this(); 1691 module_list.AppendIfNeeded(module_sp); 1692 } 1693 } 1694 } 1695 } 1696 return module_list.GetSize () - initial_size; 1697 } 1698 1699 #pragma mark CommandObjectTargetModulesModuleAutoComplete 1700 1701 //---------------------------------------------------------------------- 1702 // A base command object class that can auto complete with module file 1703 // paths 1704 //---------------------------------------------------------------------- 1705 1706 class CommandObjectTargetModulesModuleAutoComplete : public CommandObject 1707 { 1708 public: 1709 1710 CommandObjectTargetModulesModuleAutoComplete (CommandInterpreter &interpreter, 1711 const char *name, 1712 const char *help, 1713 const char *syntax) : 1714 CommandObject (interpreter, name, help, syntax) 1715 { 1716 CommandArgumentEntry arg; 1717 CommandArgumentData file_arg; 1718 1719 // Define the first (and only) variant of this arg. 1720 file_arg.arg_type = eArgTypeFilename; 1721 file_arg.arg_repetition = eArgRepeatStar; 1722 1723 // There is only one variant this argument could be; put it into the argument entry. 1724 arg.push_back (file_arg); 1725 1726 // Push the data for the first argument into the m_arguments vector. 1727 m_arguments.push_back (arg); 1728 } 1729 1730 virtual 1731 ~CommandObjectTargetModulesModuleAutoComplete () 1732 { 1733 } 1734 1735 virtual int 1736 HandleArgumentCompletion (Args &input, 1737 int &cursor_index, 1738 int &cursor_char_position, 1739 OptionElementVector &opt_element_vector, 1740 int match_start_point, 1741 int max_return_elements, 1742 bool &word_complete, 1743 StringList &matches) 1744 { 1745 // Arguments are the standard module completer. 1746 std::string completion_str (input.GetArgumentAtIndex(cursor_index)); 1747 completion_str.erase (cursor_char_position); 1748 1749 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, 1750 CommandCompletions::eModuleCompletion, 1751 completion_str.c_str(), 1752 match_start_point, 1753 max_return_elements, 1754 NULL, 1755 word_complete, 1756 matches); 1757 return matches.GetSize(); 1758 } 1759 }; 1760 1761 #pragma mark CommandObjectTargetModulesSourceFileAutoComplete 1762 1763 //---------------------------------------------------------------------- 1764 // A base command object class that can auto complete with module source 1765 // file paths 1766 //---------------------------------------------------------------------- 1767 1768 class CommandObjectTargetModulesSourceFileAutoComplete : public CommandObject 1769 { 1770 public: 1771 1772 CommandObjectTargetModulesSourceFileAutoComplete (CommandInterpreter &interpreter, 1773 const char *name, 1774 const char *help, 1775 const char *syntax) : 1776 CommandObject (interpreter, name, help, syntax) 1777 { 1778 CommandArgumentEntry arg; 1779 CommandArgumentData source_file_arg; 1780 1781 // Define the first (and only) variant of this arg. 1782 source_file_arg.arg_type = eArgTypeSourceFile; 1783 source_file_arg.arg_repetition = eArgRepeatPlus; 1784 1785 // There is only one variant this argument could be; put it into the argument entry. 1786 arg.push_back (source_file_arg); 1787 1788 // Push the data for the first argument into the m_arguments vector. 1789 m_arguments.push_back (arg); 1790 } 1791 1792 virtual 1793 ~CommandObjectTargetModulesSourceFileAutoComplete () 1794 { 1795 } 1796 1797 virtual int 1798 HandleArgumentCompletion (Args &input, 1799 int &cursor_index, 1800 int &cursor_char_position, 1801 OptionElementVector &opt_element_vector, 1802 int match_start_point, 1803 int max_return_elements, 1804 bool &word_complete, 1805 StringList &matches) 1806 { 1807 // Arguments are the standard source file completer. 1808 std::string completion_str (input.GetArgumentAtIndex(cursor_index)); 1809 completion_str.erase (cursor_char_position); 1810 1811 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, 1812 CommandCompletions::eSourceFileCompletion, 1813 completion_str.c_str(), 1814 match_start_point, 1815 max_return_elements, 1816 NULL, 1817 word_complete, 1818 matches); 1819 return matches.GetSize(); 1820 } 1821 }; 1822 1823 1824 #pragma mark CommandObjectTargetModulesDumpSymtab 1825 1826 1827 class CommandObjectTargetModulesDumpSymtab : public CommandObjectTargetModulesModuleAutoComplete 1828 { 1829 public: 1830 CommandObjectTargetModulesDumpSymtab (CommandInterpreter &interpreter) : 1831 CommandObjectTargetModulesModuleAutoComplete (interpreter, 1832 "target modules dump symtab", 1833 "Dump the symbol table from one or more target modules.", 1834 NULL), 1835 m_options (interpreter) 1836 { 1837 } 1838 1839 virtual 1840 ~CommandObjectTargetModulesDumpSymtab () 1841 { 1842 } 1843 1844 virtual bool 1845 Execute (Args& command, 1846 CommandReturnObject &result) 1847 { 1848 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 1849 if (target == NULL) 1850 { 1851 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 1852 result.SetStatus (eReturnStatusFailed); 1853 return false; 1854 } 1855 else 1856 { 1857 uint32_t num_dumped = 0; 1858 1859 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 1860 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 1861 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 1862 1863 if (command.GetArgumentCount() == 0) 1864 { 1865 // Dump all sections for all modules images 1866 const uint32_t num_modules = target->GetImages().GetSize(); 1867 if (num_modules > 0) 1868 { 1869 result.GetOutputStream().Printf("Dumping symbol table for %u modules.\n", num_modules); 1870 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx) 1871 { 1872 if (num_dumped > 0) 1873 { 1874 result.GetOutputStream().EOL(); 1875 result.GetOutputStream().EOL(); 1876 } 1877 num_dumped++; 1878 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx), m_options.m_sort_order); 1879 } 1880 } 1881 else 1882 { 1883 result.AppendError ("the target has no associated executable images"); 1884 result.SetStatus (eReturnStatusFailed); 1885 return false; 1886 } 1887 } 1888 else 1889 { 1890 // Dump specified images (by basename or fullpath) 1891 const char *arg_cstr; 1892 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx) 1893 { 1894 ModuleList module_list; 1895 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true); 1896 if (num_matches > 0) 1897 { 1898 for (size_t i=0; i<num_matches; ++i) 1899 { 1900 Module *module = module_list.GetModulePointerAtIndex(i); 1901 if (module) 1902 { 1903 if (num_dumped > 0) 1904 { 1905 result.GetOutputStream().EOL(); 1906 result.GetOutputStream().EOL(); 1907 } 1908 num_dumped++; 1909 DumpModuleSymtab (m_interpreter, result.GetOutputStream(), module, m_options.m_sort_order); 1910 } 1911 } 1912 } 1913 else 1914 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr); 1915 } 1916 } 1917 1918 if (num_dumped > 0) 1919 result.SetStatus (eReturnStatusSuccessFinishResult); 1920 else 1921 { 1922 result.AppendError ("no matching executable images found"); 1923 result.SetStatus (eReturnStatusFailed); 1924 } 1925 } 1926 return result.Succeeded(); 1927 } 1928 1929 virtual Options * 1930 GetOptions () 1931 { 1932 return &m_options; 1933 } 1934 1935 class CommandOptions : public Options 1936 { 1937 public: 1938 1939 CommandOptions (CommandInterpreter &interpreter) : 1940 Options(interpreter), 1941 m_sort_order (eSortOrderNone) 1942 { 1943 } 1944 1945 virtual 1946 ~CommandOptions () 1947 { 1948 } 1949 1950 virtual Error 1951 SetOptionValue (uint32_t option_idx, const char *option_arg) 1952 { 1953 Error error; 1954 char short_option = (char) m_getopt_table[option_idx].val; 1955 1956 switch (short_option) 1957 { 1958 case 's': 1959 m_sort_order = (SortOrder) Args::StringToOptionEnum (option_arg, 1960 g_option_table[option_idx].enum_values, 1961 eSortOrderNone, 1962 error); 1963 break; 1964 1965 default: 1966 error.SetErrorStringWithFormat("invalid short option character '%c'", short_option); 1967 break; 1968 1969 } 1970 return error; 1971 } 1972 1973 void 1974 OptionParsingStarting () 1975 { 1976 m_sort_order = eSortOrderNone; 1977 } 1978 1979 const OptionDefinition* 1980 GetDefinitions () 1981 { 1982 return g_option_table; 1983 } 1984 1985 // Options table: Required for subclasses of Options. 1986 static OptionDefinition g_option_table[]; 1987 1988 SortOrder m_sort_order; 1989 }; 1990 1991 protected: 1992 1993 CommandOptions m_options; 1994 }; 1995 1996 static OptionEnumValueElement 1997 g_sort_option_enumeration[4] = 1998 { 1999 { eSortOrderNone, "none", "No sorting, use the original symbol table order."}, 2000 { eSortOrderByAddress, "address", "Sort output by symbol address."}, 2001 { eSortOrderByName, "name", "Sort output by symbol name."}, 2002 { 0, NULL, NULL } 2003 }; 2004 2005 2006 OptionDefinition 2007 CommandObjectTargetModulesDumpSymtab::CommandOptions::g_option_table[] = 2008 { 2009 { LLDB_OPT_SET_1, false, "sort", 's', required_argument, g_sort_option_enumeration, 0, eArgTypeSortOrder, "Supply a sort order when dumping the symbol table."}, 2010 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 2011 }; 2012 2013 #pragma mark CommandObjectTargetModulesDumpSections 2014 2015 //---------------------------------------------------------------------- 2016 // Image section dumping command 2017 //---------------------------------------------------------------------- 2018 2019 class CommandObjectTargetModulesDumpSections : public CommandObjectTargetModulesModuleAutoComplete 2020 { 2021 public: 2022 CommandObjectTargetModulesDumpSections (CommandInterpreter &interpreter) : 2023 CommandObjectTargetModulesModuleAutoComplete (interpreter, 2024 "target modules dump sections", 2025 "Dump the sections from one or more target modules.", 2026 //"target modules dump sections [<file1> ...]") 2027 NULL) 2028 { 2029 } 2030 2031 virtual 2032 ~CommandObjectTargetModulesDumpSections () 2033 { 2034 } 2035 2036 virtual bool 2037 Execute (Args& command, 2038 CommandReturnObject &result) 2039 { 2040 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 2041 if (target == NULL) 2042 { 2043 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 2044 result.SetStatus (eReturnStatusFailed); 2045 return false; 2046 } 2047 else 2048 { 2049 uint32_t num_dumped = 0; 2050 2051 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 2052 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 2053 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 2054 2055 if (command.GetArgumentCount() == 0) 2056 { 2057 // Dump all sections for all modules images 2058 const uint32_t num_modules = target->GetImages().GetSize(); 2059 if (num_modules > 0) 2060 { 2061 result.GetOutputStream().Printf("Dumping sections for %u modules.\n", num_modules); 2062 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx) 2063 { 2064 num_dumped++; 2065 DumpModuleSections (m_interpreter, result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx)); 2066 } 2067 } 2068 else 2069 { 2070 result.AppendError ("the target has no associated executable images"); 2071 result.SetStatus (eReturnStatusFailed); 2072 return false; 2073 } 2074 } 2075 else 2076 { 2077 // Dump specified images (by basename or fullpath) 2078 const char *arg_cstr; 2079 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx) 2080 { 2081 ModuleList module_list; 2082 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true); 2083 if (num_matches > 0) 2084 { 2085 for (size_t i=0; i<num_matches; ++i) 2086 { 2087 Module *module = module_list.GetModulePointerAtIndex(i); 2088 if (module) 2089 { 2090 num_dumped++; 2091 DumpModuleSections (m_interpreter, result.GetOutputStream(), module); 2092 } 2093 } 2094 } 2095 else 2096 { 2097 // Check the global list 2098 Mutex::Locker locker(Module::GetAllocationModuleCollectionMutex()); 2099 2100 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr); 2101 } 2102 } 2103 } 2104 2105 if (num_dumped > 0) 2106 result.SetStatus (eReturnStatusSuccessFinishResult); 2107 else 2108 { 2109 result.AppendError ("no matching executable images found"); 2110 result.SetStatus (eReturnStatusFailed); 2111 } 2112 } 2113 return result.Succeeded(); 2114 } 2115 }; 2116 2117 2118 #pragma mark CommandObjectTargetModulesDumpSymfile 2119 2120 //---------------------------------------------------------------------- 2121 // Image debug symbol dumping command 2122 //---------------------------------------------------------------------- 2123 2124 class CommandObjectTargetModulesDumpSymfile : public CommandObjectTargetModulesModuleAutoComplete 2125 { 2126 public: 2127 CommandObjectTargetModulesDumpSymfile (CommandInterpreter &interpreter) : 2128 CommandObjectTargetModulesModuleAutoComplete (interpreter, 2129 "target modules dump symfile", 2130 "Dump the debug symbol file for one or more target modules.", 2131 //"target modules dump symfile [<file1> ...]") 2132 NULL) 2133 { 2134 } 2135 2136 virtual 2137 ~CommandObjectTargetModulesDumpSymfile () 2138 { 2139 } 2140 2141 virtual bool 2142 Execute (Args& command, 2143 CommandReturnObject &result) 2144 { 2145 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 2146 if (target == NULL) 2147 { 2148 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 2149 result.SetStatus (eReturnStatusFailed); 2150 return false; 2151 } 2152 else 2153 { 2154 uint32_t num_dumped = 0; 2155 2156 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 2157 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 2158 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 2159 2160 if (command.GetArgumentCount() == 0) 2161 { 2162 // Dump all sections for all modules images 2163 const uint32_t num_modules = target->GetImages().GetSize(); 2164 if (num_modules > 0) 2165 { 2166 result.GetOutputStream().Printf("Dumping debug symbols for %u modules.\n", num_modules); 2167 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx) 2168 { 2169 if (DumpModuleSymbolVendor (result.GetOutputStream(), target->GetImages().GetModulePointerAtIndex(image_idx))) 2170 num_dumped++; 2171 } 2172 } 2173 else 2174 { 2175 result.AppendError ("the target has no associated executable images"); 2176 result.SetStatus (eReturnStatusFailed); 2177 return false; 2178 } 2179 } 2180 else 2181 { 2182 // Dump specified images (by basename or fullpath) 2183 const char *arg_cstr; 2184 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx) 2185 { 2186 ModuleList module_list; 2187 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, true); 2188 if (num_matches > 0) 2189 { 2190 for (size_t i=0; i<num_matches; ++i) 2191 { 2192 Module *module = module_list.GetModulePointerAtIndex(i); 2193 if (module) 2194 { 2195 if (DumpModuleSymbolVendor (result.GetOutputStream(), module)) 2196 num_dumped++; 2197 } 2198 } 2199 } 2200 else 2201 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr); 2202 } 2203 } 2204 2205 if (num_dumped > 0) 2206 result.SetStatus (eReturnStatusSuccessFinishResult); 2207 else 2208 { 2209 result.AppendError ("no matching executable images found"); 2210 result.SetStatus (eReturnStatusFailed); 2211 } 2212 } 2213 return result.Succeeded(); 2214 } 2215 }; 2216 2217 2218 #pragma mark CommandObjectTargetModulesDumpLineTable 2219 2220 //---------------------------------------------------------------------- 2221 // Image debug line table dumping command 2222 //---------------------------------------------------------------------- 2223 2224 class CommandObjectTargetModulesDumpLineTable : public CommandObjectTargetModulesSourceFileAutoComplete 2225 { 2226 public: 2227 CommandObjectTargetModulesDumpLineTable (CommandInterpreter &interpreter) : 2228 CommandObjectTargetModulesSourceFileAutoComplete (interpreter, 2229 "target modules dump line-table", 2230 "Dump the debug symbol file for one or more target modules.", 2231 NULL) 2232 { 2233 } 2234 2235 virtual 2236 ~CommandObjectTargetModulesDumpLineTable () 2237 { 2238 } 2239 2240 virtual bool 2241 Execute (Args& command, 2242 CommandReturnObject &result) 2243 { 2244 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 2245 if (target == NULL) 2246 { 2247 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 2248 result.SetStatus (eReturnStatusFailed); 2249 return false; 2250 } 2251 else 2252 { 2253 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); 2254 uint32_t total_num_dumped = 0; 2255 2256 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 2257 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 2258 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 2259 2260 if (command.GetArgumentCount() == 0) 2261 { 2262 result.AppendErrorWithFormat ("\nSyntax: %s\n", m_cmd_syntax.c_str()); 2263 result.SetStatus (eReturnStatusFailed); 2264 } 2265 else 2266 { 2267 // Dump specified images (by basename or fullpath) 2268 const char *arg_cstr; 2269 for (int arg_idx = 0; (arg_cstr = command.GetArgumentAtIndex(arg_idx)) != NULL; ++arg_idx) 2270 { 2271 FileSpec file_spec(arg_cstr, false); 2272 const uint32_t num_modules = target->GetImages().GetSize(); 2273 if (num_modules > 0) 2274 { 2275 uint32_t num_dumped = 0; 2276 for (uint32_t i = 0; i<num_modules; ++i) 2277 { 2278 if (DumpCompileUnitLineTable (m_interpreter, 2279 result.GetOutputStream(), 2280 target->GetImages().GetModulePointerAtIndex(i), 2281 file_spec, 2282 exe_ctx.GetProcessPtr() && exe_ctx.GetProcessRef().IsAlive())) 2283 num_dumped++; 2284 } 2285 if (num_dumped == 0) 2286 result.AppendWarningWithFormat ("No source filenames matched '%s'.\n", arg_cstr); 2287 else 2288 total_num_dumped += num_dumped; 2289 } 2290 } 2291 } 2292 2293 if (total_num_dumped > 0) 2294 result.SetStatus (eReturnStatusSuccessFinishResult); 2295 else 2296 { 2297 result.AppendError ("no source filenames matched any command arguments"); 2298 result.SetStatus (eReturnStatusFailed); 2299 } 2300 } 2301 return result.Succeeded(); 2302 } 2303 }; 2304 2305 2306 #pragma mark CommandObjectTargetModulesDump 2307 2308 //---------------------------------------------------------------------- 2309 // Dump multi-word command for target modules 2310 //---------------------------------------------------------------------- 2311 2312 class CommandObjectTargetModulesDump : public CommandObjectMultiword 2313 { 2314 public: 2315 2316 //------------------------------------------------------------------ 2317 // Constructors and Destructors 2318 //------------------------------------------------------------------ 2319 CommandObjectTargetModulesDump(CommandInterpreter &interpreter) : 2320 CommandObjectMultiword (interpreter, 2321 "target modules dump", 2322 "A set of commands for dumping information about one or more target modules.", 2323 "target modules dump [symtab|sections|symfile|line-table] [<file1> <file2> ...]") 2324 { 2325 LoadSubCommand ("symtab", CommandObjectSP (new CommandObjectTargetModulesDumpSymtab (interpreter))); 2326 LoadSubCommand ("sections", CommandObjectSP (new CommandObjectTargetModulesDumpSections (interpreter))); 2327 LoadSubCommand ("symfile", CommandObjectSP (new CommandObjectTargetModulesDumpSymfile (interpreter))); 2328 LoadSubCommand ("line-table", CommandObjectSP (new CommandObjectTargetModulesDumpLineTable (interpreter))); 2329 } 2330 2331 virtual 2332 ~CommandObjectTargetModulesDump() 2333 { 2334 } 2335 }; 2336 2337 class CommandObjectTargetModulesAdd : public CommandObject 2338 { 2339 public: 2340 CommandObjectTargetModulesAdd (CommandInterpreter &interpreter) : 2341 CommandObject (interpreter, 2342 "target modules add", 2343 "Add a new module to the current target's modules.", 2344 "target modules add [<module>]") 2345 { 2346 } 2347 2348 virtual 2349 ~CommandObjectTargetModulesAdd () 2350 { 2351 } 2352 2353 virtual bool 2354 Execute (Args& args, 2355 CommandReturnObject &result) 2356 { 2357 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 2358 if (target == NULL) 2359 { 2360 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 2361 result.SetStatus (eReturnStatusFailed); 2362 return false; 2363 } 2364 else 2365 { 2366 const size_t argc = args.GetArgumentCount(); 2367 if (argc == 0) 2368 { 2369 result.AppendError ("one or more executable image paths must be specified"); 2370 result.SetStatus (eReturnStatusFailed); 2371 return false; 2372 } 2373 else 2374 { 2375 for (size_t i=0; i<argc; ++i) 2376 { 2377 const char *path = args.GetArgumentAtIndex(i); 2378 if (path) 2379 { 2380 FileSpec file_spec(path, true); 2381 ArchSpec arch; 2382 if (file_spec.Exists()) 2383 { 2384 ModuleSP module_sp (target->GetSharedModule(file_spec, arch)); 2385 if (!module_sp) 2386 { 2387 result.AppendError ("one or more executable image paths must be specified"); 2388 result.SetStatus (eReturnStatusFailed); 2389 return false; 2390 } 2391 result.SetStatus (eReturnStatusSuccessFinishResult); 2392 } 2393 else 2394 { 2395 char resolved_path[PATH_MAX]; 2396 result.SetStatus (eReturnStatusFailed); 2397 if (file_spec.GetPath (resolved_path, sizeof(resolved_path))) 2398 { 2399 if (strcmp (resolved_path, path) != 0) 2400 { 2401 result.AppendErrorWithFormat ("invalid module path '%s' with resolved path '%s'\n", path, resolved_path); 2402 break; 2403 } 2404 } 2405 result.AppendErrorWithFormat ("invalid module path '%s'\n", path); 2406 break; 2407 } 2408 } 2409 } 2410 } 2411 } 2412 return result.Succeeded(); 2413 } 2414 2415 int 2416 HandleArgumentCompletion (Args &input, 2417 int &cursor_index, 2418 int &cursor_char_position, 2419 OptionElementVector &opt_element_vector, 2420 int match_start_point, 2421 int max_return_elements, 2422 bool &word_complete, 2423 StringList &matches) 2424 { 2425 std::string completion_str (input.GetArgumentAtIndex(cursor_index)); 2426 completion_str.erase (cursor_char_position); 2427 2428 CommandCompletions::InvokeCommonCompletionCallbacks (m_interpreter, 2429 CommandCompletions::eDiskFileCompletion, 2430 completion_str.c_str(), 2431 match_start_point, 2432 max_return_elements, 2433 NULL, 2434 word_complete, 2435 matches); 2436 return matches.GetSize(); 2437 } 2438 2439 }; 2440 2441 class CommandObjectTargetModulesLoad : public CommandObjectTargetModulesModuleAutoComplete 2442 { 2443 public: 2444 CommandObjectTargetModulesLoad (CommandInterpreter &interpreter) : 2445 CommandObjectTargetModulesModuleAutoComplete (interpreter, 2446 "target modules load", 2447 "Set the load addresses for one or more sections in a target module.", 2448 "target modules load [--file <module> --uuid <uuid>] <sect-name> <address> [<sect-name> <address> ....]"), 2449 m_option_group (interpreter), 2450 m_file_option (LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypePath, "Fullpath or basename for module to load."), 2451 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) 2452 { 2453 m_option_group.Append (&m_uuid_option_group, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 2454 m_option_group.Append (&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 2455 m_option_group.Append (&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1); 2456 m_option_group.Finalize(); 2457 } 2458 2459 virtual 2460 ~CommandObjectTargetModulesLoad () 2461 { 2462 } 2463 2464 virtual bool 2465 Execute (Args& args, 2466 CommandReturnObject &result) 2467 { 2468 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 2469 if (target == NULL) 2470 { 2471 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 2472 result.SetStatus (eReturnStatusFailed); 2473 return false; 2474 } 2475 else 2476 { 2477 const size_t argc = args.GetArgumentCount(); 2478 const FileSpec *file_ptr = NULL; 2479 const UUID *uuid_ptr = NULL; 2480 if (m_file_option.GetOptionValue().OptionWasSet()) 2481 file_ptr = &m_file_option.GetOptionValue().GetCurrentValue(); 2482 2483 if (m_uuid_option_group.GetOptionValue().OptionWasSet()) 2484 uuid_ptr = &m_uuid_option_group.GetOptionValue().GetCurrentValue(); 2485 2486 if (file_ptr || uuid_ptr) 2487 { 2488 2489 ModuleList matching_modules; 2490 const size_t num_matches = target->GetImages().FindModules (file_ptr, // File spec to match (can be NULL to match by UUID only) 2491 NULL, // Architecture 2492 uuid_ptr, // UUID to match (can be NULL to not match on UUID) 2493 NULL, // Object name 2494 matching_modules); 2495 2496 char path[PATH_MAX]; 2497 if (num_matches == 1) 2498 { 2499 Module *module = matching_modules.GetModulePointerAtIndex(0); 2500 if (module) 2501 { 2502 ObjectFile *objfile = module->GetObjectFile(); 2503 if (objfile) 2504 { 2505 SectionList *section_list = objfile->GetSectionList(); 2506 if (section_list) 2507 { 2508 if (argc == 0) 2509 { 2510 if (m_slide_option.GetOptionValue().OptionWasSet()) 2511 { 2512 Module *module = matching_modules.GetModulePointerAtIndex(0); 2513 if (module) 2514 { 2515 ObjectFile *objfile = module->GetObjectFile(); 2516 if (objfile) 2517 { 2518 SectionList *section_list = objfile->GetSectionList(); 2519 if (section_list) 2520 { 2521 const size_t num_sections = section_list->GetSize(); 2522 const addr_t slide = m_slide_option.GetOptionValue().GetCurrentValue(); 2523 for (size_t sect_idx = 0; sect_idx < num_sections; ++sect_idx) 2524 { 2525 SectionSP section_sp (section_list->GetSectionAtIndex(sect_idx)); 2526 if (section_sp) 2527 target->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), section_sp->GetFileAddress() + slide); 2528 } 2529 } 2530 } 2531 } 2532 } 2533 else 2534 { 2535 result.AppendError ("one or more section name + load address pair must be specified"); 2536 result.SetStatus (eReturnStatusFailed); 2537 return false; 2538 } 2539 } 2540 else 2541 { 2542 if (m_slide_option.GetOptionValue().OptionWasSet()) 2543 { 2544 result.AppendError ("The \"--slide <offset>\" option can't be used in conjunction with setting section load addresses.\n"); 2545 result.SetStatus (eReturnStatusFailed); 2546 return false; 2547 } 2548 2549 for (size_t i=0; i<argc; i += 2) 2550 { 2551 const char *sect_name = args.GetArgumentAtIndex(i); 2552 const char *load_addr_cstr = args.GetArgumentAtIndex(i+1); 2553 if (sect_name && load_addr_cstr) 2554 { 2555 ConstString const_sect_name(sect_name); 2556 bool success = false; 2557 addr_t load_addr = Args::StringToUInt64(load_addr_cstr, LLDB_INVALID_ADDRESS, 0, &success); 2558 if (success) 2559 { 2560 SectionSP section_sp (section_list->FindSectionByName(const_sect_name)); 2561 if (section_sp) 2562 { 2563 target->GetSectionLoadList().SetSectionLoadAddress (section_sp.get(), load_addr); 2564 result.AppendMessageWithFormat("section '%s' loaded at 0x%llx\n", sect_name, load_addr); 2565 } 2566 else 2567 { 2568 result.AppendErrorWithFormat ("no section found that matches the section name '%s'\n", sect_name); 2569 result.SetStatus (eReturnStatusFailed); 2570 break; 2571 } 2572 } 2573 else 2574 { 2575 result.AppendErrorWithFormat ("invalid load address string '%s'\n", load_addr_cstr); 2576 result.SetStatus (eReturnStatusFailed); 2577 break; 2578 } 2579 } 2580 else 2581 { 2582 if (sect_name) 2583 result.AppendError ("section names must be followed by a load address.\n"); 2584 else 2585 result.AppendError ("one or more section name + load address pair must be specified.\n"); 2586 result.SetStatus (eReturnStatusFailed); 2587 break; 2588 } 2589 } 2590 } 2591 } 2592 else 2593 { 2594 module->GetFileSpec().GetPath (path, sizeof(path)); 2595 result.AppendErrorWithFormat ("no sections in object file '%s'\n", path); 2596 result.SetStatus (eReturnStatusFailed); 2597 } 2598 } 2599 else 2600 { 2601 module->GetFileSpec().GetPath (path, sizeof(path)); 2602 result.AppendErrorWithFormat ("no object file for module '%s'\n", path); 2603 result.SetStatus (eReturnStatusFailed); 2604 } 2605 } 2606 else 2607 { 2608 module->GetFileSpec().GetPath (path, sizeof(path)); 2609 result.AppendErrorWithFormat ("invalid module '%s'.\n", path); 2610 result.SetStatus (eReturnStatusFailed); 2611 } 2612 } 2613 else 2614 { 2615 char uuid_cstr[64]; 2616 if (file_ptr) 2617 file_ptr->GetPath (path, sizeof(path)); 2618 else 2619 path[0] = '\0'; 2620 2621 if (uuid_ptr) 2622 uuid_ptr->GetAsCString(uuid_cstr, sizeof(uuid_cstr)); 2623 else 2624 uuid_cstr[0] = '\0'; 2625 if (num_matches > 1) 2626 { 2627 result.AppendErrorWithFormat ("multiple modules match%s%s%s%s:\n", 2628 path[0] ? " file=" : "", 2629 path, 2630 uuid_cstr[0] ? " uuid=" : "", 2631 uuid_cstr); 2632 for (size_t i=0; i<num_matches; ++i) 2633 { 2634 if (matching_modules.GetModulePointerAtIndex(i)->GetFileSpec().GetPath (path, sizeof(path))) 2635 result.AppendMessageWithFormat("%s\n", path); 2636 } 2637 } 2638 else 2639 { 2640 result.AppendErrorWithFormat ("no modules were found that match%s%s%s%s.\n", 2641 path[0] ? " file=" : "", 2642 path, 2643 uuid_cstr[0] ? " uuid=" : "", 2644 uuid_cstr); 2645 } 2646 result.SetStatus (eReturnStatusFailed); 2647 } 2648 } 2649 else 2650 { 2651 result.AppendError ("either the \"--file <module>\" or the \"--uuid <uuid>\" option must be specified.\n"); 2652 result.SetStatus (eReturnStatusFailed); 2653 return false; 2654 } 2655 } 2656 return result.Succeeded(); 2657 } 2658 2659 virtual Options * 2660 GetOptions () 2661 { 2662 return &m_option_group; 2663 } 2664 2665 protected: 2666 OptionGroupOptions m_option_group; 2667 OptionGroupUUID m_uuid_option_group; 2668 OptionGroupFile m_file_option; 2669 OptionGroupUInt64 m_slide_option; 2670 }; 2671 2672 //---------------------------------------------------------------------- 2673 // List images with associated information 2674 //---------------------------------------------------------------------- 2675 class CommandObjectTargetModulesList : public CommandObject 2676 { 2677 public: 2678 2679 class CommandOptions : public Options 2680 { 2681 public: 2682 2683 CommandOptions (CommandInterpreter &interpreter) : 2684 Options(interpreter), 2685 m_format_array(), 2686 m_use_global_module_list (false), 2687 m_module_addr (LLDB_INVALID_ADDRESS) 2688 { 2689 } 2690 2691 virtual 2692 ~CommandOptions () 2693 { 2694 } 2695 2696 virtual Error 2697 SetOptionValue (uint32_t option_idx, const char *option_arg) 2698 { 2699 char short_option = (char) m_getopt_table[option_idx].val; 2700 if (short_option == 'g') 2701 { 2702 m_use_global_module_list = true; 2703 } 2704 else if (short_option == 'a') 2705 { 2706 bool success; 2707 m_module_addr = Args::StringToAddress(option_arg, LLDB_INVALID_ADDRESS, &success); 2708 if (!success) 2709 { 2710 Error error; 2711 error.SetErrorStringWithFormat("invalid address: \"%s\"", option_arg); 2712 } 2713 } 2714 else 2715 { 2716 uint32_t width = 0; 2717 if (option_arg) 2718 width = strtoul (option_arg, NULL, 0); 2719 m_format_array.push_back(std::make_pair(short_option, width)); 2720 } 2721 Error error; 2722 return error; 2723 } 2724 2725 void 2726 OptionParsingStarting () 2727 { 2728 m_format_array.clear(); 2729 m_use_global_module_list = false; 2730 m_module_addr = LLDB_INVALID_ADDRESS; 2731 } 2732 2733 const OptionDefinition* 2734 GetDefinitions () 2735 { 2736 return g_option_table; 2737 } 2738 2739 // Options table: Required for subclasses of Options. 2740 2741 static OptionDefinition g_option_table[]; 2742 2743 // Instance variables to hold the values for command options. 2744 typedef std::vector< std::pair<char, uint32_t> > FormatWidthCollection; 2745 FormatWidthCollection m_format_array; 2746 bool m_use_global_module_list; 2747 lldb::addr_t m_module_addr; 2748 }; 2749 2750 CommandObjectTargetModulesList (CommandInterpreter &interpreter) : 2751 CommandObject (interpreter, 2752 "target modules list", 2753 "List current executable and dependent shared library images.", 2754 "target modules list [<cmd-options>]"), 2755 m_options (interpreter) 2756 { 2757 } 2758 2759 virtual 2760 ~CommandObjectTargetModulesList () 2761 { 2762 } 2763 2764 virtual 2765 Options * 2766 GetOptions () 2767 { 2768 return &m_options; 2769 } 2770 2771 virtual bool 2772 Execute (Args& command, 2773 CommandReturnObject &result) 2774 { 2775 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 2776 const bool use_global_module_list = m_options.m_use_global_module_list; 2777 if (target == NULL && use_global_module_list == false) 2778 { 2779 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 2780 result.SetStatus (eReturnStatusFailed); 2781 return false; 2782 } 2783 else 2784 { 2785 if (target) 2786 { 2787 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 2788 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 2789 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 2790 } 2791 // Dump all sections for all modules images 2792 uint32_t num_modules = 0; 2793 Mutex::Locker locker; 2794 2795 Stream &strm = result.GetOutputStream(); 2796 2797 if (m_options.m_module_addr != LLDB_INVALID_ADDRESS) 2798 { 2799 if (target) 2800 { 2801 Address module_address; 2802 if (module_address.SetLoadAddress(m_options.m_module_addr, target)) 2803 { 2804 Module *module = module_address.GetModulePtr(); 2805 if (module) 2806 { 2807 PrintModule (target, module, UINT32_MAX, 0, strm); 2808 result.SetStatus (eReturnStatusSuccessFinishResult); 2809 } 2810 else 2811 { 2812 result.AppendError ("Couldn't find module matching address: 0x%llx.", m_options.m_module_addr); 2813 result.SetStatus (eReturnStatusFailed); 2814 } 2815 } 2816 else 2817 { 2818 result.AppendError ("Couldn't find module containing address: 0x%llx.", m_options.m_module_addr); 2819 result.SetStatus (eReturnStatusFailed); 2820 } 2821 } 2822 else 2823 { 2824 result.AppendError ("Can only look up modules by address with a valid target."); 2825 result.SetStatus (eReturnStatusFailed); 2826 } 2827 return result.Succeeded(); 2828 } 2829 2830 if (use_global_module_list) 2831 { 2832 locker.Reset (Module::GetAllocationModuleCollectionMutex()->GetMutex()); 2833 num_modules = Module::GetNumberAllocatedModules(); 2834 } 2835 else 2836 num_modules = target->GetImages().GetSize(); 2837 2838 if (num_modules > 0) 2839 { 2840 for (uint32_t image_idx = 0; image_idx<num_modules; ++image_idx) 2841 { 2842 ModuleSP module_sp; 2843 Module *module; 2844 if (use_global_module_list) 2845 { 2846 module = Module::GetAllocatedModuleAtIndex(image_idx); 2847 module_sp = module->shared_from_this(); 2848 } 2849 else 2850 { 2851 module_sp = target->GetImages().GetModuleAtIndex(image_idx); 2852 module = module_sp.get(); 2853 } 2854 2855 int indent = strm.Printf("[%3u] ", image_idx); 2856 PrintModule (target, module, image_idx, indent, strm); 2857 2858 } 2859 result.SetStatus (eReturnStatusSuccessFinishResult); 2860 } 2861 else 2862 { 2863 if (use_global_module_list) 2864 result.AppendError ("the global module list is empty"); 2865 else 2866 result.AppendError ("the target has no associated executable images"); 2867 result.SetStatus (eReturnStatusFailed); 2868 return false; 2869 } 2870 } 2871 return result.Succeeded(); 2872 } 2873 protected: 2874 2875 void 2876 PrintModule (Target *target, Module *module, uint32_t idx, int indent, Stream &strm) 2877 { 2878 2879 bool dump_object_name = false; 2880 if (m_options.m_format_array.empty()) 2881 { 2882 m_options.m_format_array.push_back(std::make_pair('u', 0)); 2883 m_options.m_format_array.push_back(std::make_pair('h', 0)); 2884 m_options.m_format_array.push_back(std::make_pair('f', 0)); 2885 m_options.m_format_array.push_back(std::make_pair('S', 0)); 2886 } 2887 const size_t num_entries = m_options.m_format_array.size(); 2888 bool print_space = false; 2889 for (size_t i=0; i<num_entries; ++i) 2890 { 2891 if (print_space) 2892 strm.PutChar(' '); 2893 print_space = true; 2894 const char format_char = m_options.m_format_array[i].first; 2895 uint32_t width = m_options.m_format_array[i].second; 2896 switch (format_char) 2897 { 2898 case 'A': 2899 DumpModuleArchitecture (strm, module, false, width); 2900 break; 2901 2902 case 't': 2903 DumpModuleArchitecture (strm, module, true, width); 2904 break; 2905 2906 case 'f': 2907 DumpFullpath (strm, &module->GetFileSpec(), width); 2908 dump_object_name = true; 2909 break; 2910 2911 case 'd': 2912 DumpDirectory (strm, &module->GetFileSpec(), width); 2913 break; 2914 2915 case 'b': 2916 DumpBasename (strm, &module->GetFileSpec(), width); 2917 dump_object_name = true; 2918 break; 2919 2920 case 'h': 2921 case 'o': 2922 // Image header address 2923 { 2924 uint32_t addr_nibble_width = target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16; 2925 2926 ObjectFile *objfile = module->GetObjectFile (); 2927 if (objfile) 2928 { 2929 Address header_addr(objfile->GetHeaderAddress()); 2930 if (header_addr.IsValid()) 2931 { 2932 if (target && !target->GetSectionLoadList().IsEmpty()) 2933 { 2934 lldb::addr_t header_load_addr = header_addr.GetLoadAddress (target); 2935 if (header_load_addr == LLDB_INVALID_ADDRESS) 2936 { 2937 header_addr.Dump (&strm, target, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleFileAddress); 2938 } 2939 else 2940 { 2941 if (format_char == 'o') 2942 { 2943 // Show the offset of slide for the image 2944 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr - header_addr.GetFileAddress()); 2945 } 2946 else 2947 { 2948 // Show the load address of the image 2949 strm.Printf ("0x%*.*llx", addr_nibble_width, addr_nibble_width, header_load_addr); 2950 } 2951 } 2952 break; 2953 } 2954 // The address was valid, but the image isn't loaded, output the address in an appropriate format 2955 header_addr.Dump (&strm, target, Address::DumpStyleFileAddress); 2956 break; 2957 } 2958 } 2959 strm.Printf ("%*s", addr_nibble_width + 2, ""); 2960 } 2961 break; 2962 case 'r': 2963 { 2964 uint32_t ref_count = 0; 2965 ModuleSP module_sp (module->shared_from_this()); 2966 if (module_sp) 2967 { 2968 // Take one away to make sure we don't count our local "module_sp" 2969 ref_count = module_sp.use_count() - 1; 2970 } 2971 if (width) 2972 strm.Printf("{%*u}", width, ref_count); 2973 else 2974 strm.Printf("{%u}", ref_count); 2975 } 2976 break; 2977 2978 case 's': 2979 case 'S': 2980 { 2981 SymbolVendor *symbol_vendor = module->GetSymbolVendor(); 2982 if (symbol_vendor) 2983 { 2984 SymbolFile *symbol_file = symbol_vendor->GetSymbolFile(); 2985 if (symbol_file) 2986 { 2987 if (format_char == 'S') 2988 { 2989 FileSpec &symfile_spec = symbol_file->GetObjectFile()->GetFileSpec(); 2990 // Dump symbol file only if different from module file 2991 if (!symfile_spec || symfile_spec == module->GetFileSpec()) 2992 { 2993 print_space = false; 2994 break; 2995 } 2996 // Add a newline and indent past the index 2997 strm.Printf ("\n%*s", indent, ""); 2998 } 2999 DumpFullpath (strm, &symbol_file->GetObjectFile()->GetFileSpec(), width); 3000 dump_object_name = true; 3001 break; 3002 } 3003 } 3004 strm.Printf("%.*s", width, "<NONE>"); 3005 } 3006 break; 3007 3008 case 'm': 3009 module->GetModificationTime().Dump(&strm, width); 3010 break; 3011 3012 case 'p': 3013 strm.Printf("%p", module); 3014 break; 3015 3016 case 'u': 3017 DumpModuleUUID(strm, module); 3018 break; 3019 3020 default: 3021 break; 3022 } 3023 3024 } 3025 if (dump_object_name) 3026 { 3027 const char *object_name = module->GetObjectName().GetCString(); 3028 if (object_name) 3029 strm.Printf ("(%s)", object_name); 3030 } 3031 strm.EOL(); 3032 } 3033 3034 CommandOptions m_options; 3035 }; 3036 3037 OptionDefinition 3038 CommandObjectTargetModulesList::CommandOptions::g_option_table[] = 3039 { 3040 { LLDB_OPT_SET_1, false, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Display the image at this address."}, 3041 { LLDB_OPT_SET_1, false, "arch", 'A', optional_argument, NULL, 0, eArgTypeWidth, "Display the architecture when listing images."}, 3042 { LLDB_OPT_SET_1, false, "triple", 't', optional_argument, NULL, 0, eArgTypeWidth, "Display the triple when listing images."}, 3043 { 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."}, 3044 { 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)."}, 3045 { LLDB_OPT_SET_1, false, "uuid", 'u', no_argument, NULL, 0, eArgTypeNone, "Display the UUID when listing images."}, 3046 { LLDB_OPT_SET_1, false, "fullpath", 'f', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image object file."}, 3047 { LLDB_OPT_SET_1, false, "directory", 'd', optional_argument, NULL, 0, eArgTypeWidth, "Display the directory with optional width for the image object file."}, 3048 { LLDB_OPT_SET_1, false, "basename", 'b', optional_argument, NULL, 0, eArgTypeWidth, "Display the basename with optional width for the image object file."}, 3049 { LLDB_OPT_SET_1, false, "symfile", 's', optional_argument, NULL, 0, eArgTypeWidth, "Display the fullpath to the image symbol file with optional width."}, 3050 { 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."}, 3051 { LLDB_OPT_SET_1, false, "mod-time", 'm', optional_argument, NULL, 0, eArgTypeWidth, "Display the modification time with optional width of the module."}, 3052 { 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."}, 3053 { LLDB_OPT_SET_1, false, "pointer", 'p', optional_argument, NULL, 0, eArgTypeNone, "Display the module pointer."}, 3054 { 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."}, 3055 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 3056 }; 3057 3058 3059 3060 //---------------------------------------------------------------------- 3061 // Lookup information in images 3062 //---------------------------------------------------------------------- 3063 class CommandObjectTargetModulesLookup : public CommandObject 3064 { 3065 public: 3066 3067 enum 3068 { 3069 eLookupTypeInvalid = -1, 3070 eLookupTypeAddress = 0, 3071 eLookupTypeSymbol, 3072 eLookupTypeFileLine, // Line is optional 3073 eLookupTypeFunction, 3074 eLookupTypeType, 3075 kNumLookupTypes 3076 }; 3077 3078 class CommandOptions : public Options 3079 { 3080 public: 3081 3082 CommandOptions (CommandInterpreter &interpreter) : 3083 Options(interpreter) 3084 { 3085 OptionParsingStarting(); 3086 } 3087 3088 virtual 3089 ~CommandOptions () 3090 { 3091 } 3092 3093 virtual Error 3094 SetOptionValue (uint32_t option_idx, const char *option_arg) 3095 { 3096 Error error; 3097 3098 char short_option = (char) m_getopt_table[option_idx].val; 3099 3100 switch (short_option) 3101 { 3102 case 'a': 3103 m_type = eLookupTypeAddress; 3104 m_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS); 3105 if (m_addr == LLDB_INVALID_ADDRESS) 3106 error.SetErrorStringWithFormat ("invalid address string '%s'", option_arg); 3107 break; 3108 3109 case 'o': 3110 m_offset = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS); 3111 if (m_offset == LLDB_INVALID_ADDRESS) 3112 error.SetErrorStringWithFormat ("invalid offset string '%s'", option_arg); 3113 break; 3114 3115 case 's': 3116 m_str = option_arg; 3117 m_type = eLookupTypeSymbol; 3118 break; 3119 3120 case 'f': 3121 m_file.SetFile (option_arg, false); 3122 m_type = eLookupTypeFileLine; 3123 break; 3124 3125 case 'i': 3126 m_check_inlines = false; 3127 break; 3128 3129 case 'l': 3130 m_line_number = Args::StringToUInt32(option_arg, UINT32_MAX); 3131 if (m_line_number == UINT32_MAX) 3132 error.SetErrorStringWithFormat ("invalid line number string '%s'", option_arg); 3133 else if (m_line_number == 0) 3134 error.SetErrorString ("zero is an invalid line number"); 3135 m_type = eLookupTypeFileLine; 3136 break; 3137 3138 case 'n': 3139 m_str = option_arg; 3140 m_type = eLookupTypeFunction; 3141 break; 3142 3143 case 't': 3144 m_str = option_arg; 3145 m_type = eLookupTypeType; 3146 break; 3147 3148 case 'v': 3149 m_verbose = 1; 3150 break; 3151 3152 case 'r': 3153 m_use_regex = true; 3154 break; 3155 } 3156 3157 return error; 3158 } 3159 3160 void 3161 OptionParsingStarting () 3162 { 3163 m_type = eLookupTypeInvalid; 3164 m_str.clear(); 3165 m_file.Clear(); 3166 m_addr = LLDB_INVALID_ADDRESS; 3167 m_offset = 0; 3168 m_line_number = 0; 3169 m_use_regex = false; 3170 m_check_inlines = true; 3171 m_verbose = false; 3172 } 3173 3174 const OptionDefinition* 3175 GetDefinitions () 3176 { 3177 return g_option_table; 3178 } 3179 3180 // Options table: Required for subclasses of Options. 3181 3182 static OptionDefinition g_option_table[]; 3183 int m_type; // Should be a eLookupTypeXXX enum after parsing options 3184 std::string m_str; // Holds name lookup 3185 FileSpec m_file; // Files for file lookups 3186 lldb::addr_t m_addr; // Holds the address to lookup 3187 lldb::addr_t m_offset; // Subtract this offset from m_addr before doing lookups. 3188 uint32_t m_line_number; // Line number for file+line lookups 3189 bool m_use_regex; // Name lookups in m_str are regular expressions. 3190 bool m_check_inlines;// Check for inline entries when looking up by file/line. 3191 bool m_verbose; // Enable verbose lookup info 3192 3193 }; 3194 3195 CommandObjectTargetModulesLookup (CommandInterpreter &interpreter) : 3196 CommandObject (interpreter, 3197 "target modules lookup", 3198 "Look up information within executable and dependent shared library images.", 3199 NULL), 3200 m_options (interpreter) 3201 { 3202 CommandArgumentEntry arg; 3203 CommandArgumentData file_arg; 3204 3205 // Define the first (and only) variant of this arg. 3206 file_arg.arg_type = eArgTypeFilename; 3207 file_arg.arg_repetition = eArgRepeatStar; 3208 3209 // There is only one variant this argument could be; put it into the argument entry. 3210 arg.push_back (file_arg); 3211 3212 // Push the data for the first argument into the m_arguments vector. 3213 m_arguments.push_back (arg); 3214 } 3215 3216 virtual 3217 ~CommandObjectTargetModulesLookup () 3218 { 3219 } 3220 3221 virtual Options * 3222 GetOptions () 3223 { 3224 return &m_options; 3225 } 3226 3227 3228 bool 3229 LookupInModule (CommandInterpreter &interpreter, Module *module, CommandReturnObject &result, bool &syntax_error) 3230 { 3231 switch (m_options.m_type) 3232 { 3233 case eLookupTypeAddress: 3234 if (m_options.m_addr != LLDB_INVALID_ADDRESS) 3235 { 3236 if (LookupAddressInModule (m_interpreter, 3237 result.GetOutputStream(), 3238 module, 3239 eSymbolContextEverything, 3240 m_options.m_addr, 3241 m_options.m_offset, 3242 m_options.m_verbose)) 3243 { 3244 result.SetStatus(eReturnStatusSuccessFinishResult); 3245 return true; 3246 } 3247 } 3248 break; 3249 3250 case eLookupTypeSymbol: 3251 if (!m_options.m_str.empty()) 3252 { 3253 if (LookupSymbolInModule (m_interpreter, result.GetOutputStream(), module, m_options.m_str.c_str(), m_options.m_use_regex)) 3254 { 3255 result.SetStatus(eReturnStatusSuccessFinishResult); 3256 return true; 3257 } 3258 } 3259 break; 3260 3261 case eLookupTypeFileLine: 3262 if (m_options.m_file) 3263 { 3264 3265 if (LookupFileAndLineInModule (m_interpreter, 3266 result.GetOutputStream(), 3267 module, 3268 m_options.m_file, 3269 m_options.m_line_number, 3270 m_options.m_check_inlines, 3271 m_options.m_verbose)) 3272 { 3273 result.SetStatus(eReturnStatusSuccessFinishResult); 3274 return true; 3275 } 3276 } 3277 break; 3278 3279 case eLookupTypeFunction: 3280 if (!m_options.m_str.empty()) 3281 { 3282 if (LookupFunctionInModule (m_interpreter, 3283 result.GetOutputStream(), 3284 module, 3285 m_options.m_str.c_str(), 3286 m_options.m_use_regex, 3287 m_options.m_verbose)) 3288 { 3289 result.SetStatus(eReturnStatusSuccessFinishResult); 3290 return true; 3291 } 3292 } 3293 break; 3294 3295 case eLookupTypeType: 3296 if (!m_options.m_str.empty()) 3297 { 3298 if (LookupTypeInModule (m_interpreter, 3299 result.GetOutputStream(), 3300 module, 3301 m_options.m_str.c_str(), 3302 m_options.m_use_regex)) 3303 { 3304 result.SetStatus(eReturnStatusSuccessFinishResult); 3305 return true; 3306 } 3307 } 3308 break; 3309 3310 default: 3311 m_options.GenerateOptionUsage (result.GetErrorStream(), this); 3312 syntax_error = true; 3313 break; 3314 } 3315 3316 result.SetStatus (eReturnStatusFailed); 3317 return false; 3318 } 3319 3320 virtual bool 3321 Execute (Args& command, 3322 CommandReturnObject &result) 3323 { 3324 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 3325 if (target == NULL) 3326 { 3327 result.AppendError ("invalid target, create a debug target using the 'target create' command"); 3328 result.SetStatus (eReturnStatusFailed); 3329 return false; 3330 } 3331 else 3332 { 3333 bool syntax_error = false; 3334 uint32_t i; 3335 uint32_t num_successful_lookups = 0; 3336 uint32_t addr_byte_size = target->GetArchitecture().GetAddressByteSize(); 3337 result.GetOutputStream().SetAddressByteSize(addr_byte_size); 3338 result.GetErrorStream().SetAddressByteSize(addr_byte_size); 3339 // Dump all sections for all modules images 3340 3341 if (command.GetArgumentCount() == 0) 3342 { 3343 // Dump all sections for all modules images 3344 const uint32_t num_modules = target->GetImages().GetSize(); 3345 if (num_modules > 0) 3346 { 3347 for (i = 0; i<num_modules && syntax_error == false; ++i) 3348 { 3349 if (LookupInModule (m_interpreter, target->GetImages().GetModulePointerAtIndex(i), result, syntax_error)) 3350 { 3351 result.GetOutputStream().EOL(); 3352 num_successful_lookups++; 3353 } 3354 } 3355 } 3356 else 3357 { 3358 result.AppendError ("the target has no associated executable images"); 3359 result.SetStatus (eReturnStatusFailed); 3360 return false; 3361 } 3362 } 3363 else 3364 { 3365 // Dump specified images (by basename or fullpath) 3366 const char *arg_cstr; 3367 for (i = 0; (arg_cstr = command.GetArgumentAtIndex(i)) != NULL && syntax_error == false; ++i) 3368 { 3369 ModuleList module_list; 3370 const size_t num_matches = FindModulesByName (target, arg_cstr, module_list, false); 3371 if (num_matches > 0) 3372 { 3373 for (size_t i=0; i<num_matches; ++i) 3374 { 3375 Module *module = module_list.GetModulePointerAtIndex(i); 3376 if (module) 3377 { 3378 if (LookupInModule (m_interpreter, module, result, syntax_error)) 3379 { 3380 result.GetOutputStream().EOL(); 3381 num_successful_lookups++; 3382 } 3383 } 3384 } 3385 } 3386 else 3387 result.AppendWarningWithFormat("Unable to find an image that matches '%s'.\n", arg_cstr); 3388 } 3389 } 3390 3391 if (num_successful_lookups > 0) 3392 result.SetStatus (eReturnStatusSuccessFinishResult); 3393 else 3394 result.SetStatus (eReturnStatusFailed); 3395 } 3396 return result.Succeeded(); 3397 } 3398 protected: 3399 3400 CommandOptions m_options; 3401 }; 3402 3403 OptionDefinition 3404 CommandObjectTargetModulesLookup::CommandOptions::g_option_table[] = 3405 { 3406 { LLDB_OPT_SET_1, true, "address", 'a', required_argument, NULL, 0, eArgTypeAddress, "Lookup an address in one or more target modules."}, 3407 { 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."}, 3408 { LLDB_OPT_SET_2| LLDB_OPT_SET_4 3409 /* FIXME: re-enable this for types when the LookupTypeInModule actually uses the regex option: | LLDB_OPT_SET_5 */ , 3410 false, "regex", 'r', no_argument, NULL, 0, eArgTypeNone, "The <name> argument for name lookups are regular expressions."}, 3411 { 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."}, 3412 { 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."}, 3413 { 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)."}, 3414 { LLDB_OPT_SET_3, false, "no-inlines", 'i', no_argument, NULL, 0, eArgTypeNone, "Check inline line entries (must be used in conjunction with --file)."}, 3415 { LLDB_OPT_SET_4, true, "function", 'n', required_argument, NULL, 0, eArgTypeFunctionName, "Lookup a function by name in the debug symbols in one or more target modules."}, 3416 { LLDB_OPT_SET_5, true, "type", 't', required_argument, NULL, 0, eArgTypeName, "Lookup a type by name in the debug symbols in one or more target modules."}, 3417 { LLDB_OPT_SET_ALL, false, "verbose", 'v', no_argument, NULL, 0, eArgTypeNone, "Enable verbose lookup information."}, 3418 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 3419 }; 3420 3421 3422 #pragma mark CommandObjectMultiwordImageSearchPaths 3423 3424 //------------------------------------------------------------------------- 3425 // CommandObjectMultiwordImageSearchPaths 3426 //------------------------------------------------------------------------- 3427 3428 class CommandObjectTargetModulesImageSearchPaths : public CommandObjectMultiword 3429 { 3430 public: 3431 3432 CommandObjectTargetModulesImageSearchPaths (CommandInterpreter &interpreter) : 3433 CommandObjectMultiword (interpreter, 3434 "target modules search-paths", 3435 "A set of commands for operating on debugger target image search paths.", 3436 "target modules search-paths <subcommand> [<subcommand-options>]") 3437 { 3438 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesSearchPathsAdd (interpreter))); 3439 LoadSubCommand ("clear", CommandObjectSP (new CommandObjectTargetModulesSearchPathsClear (interpreter))); 3440 LoadSubCommand ("insert", CommandObjectSP (new CommandObjectTargetModulesSearchPathsInsert (interpreter))); 3441 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesSearchPathsList (interpreter))); 3442 LoadSubCommand ("query", CommandObjectSP (new CommandObjectTargetModulesSearchPathsQuery (interpreter))); 3443 } 3444 3445 ~CommandObjectTargetModulesImageSearchPaths() 3446 { 3447 } 3448 }; 3449 3450 3451 3452 #pragma mark CommandObjectTargetModules 3453 3454 //------------------------------------------------------------------------- 3455 // CommandObjectTargetModules 3456 //------------------------------------------------------------------------- 3457 3458 class CommandObjectTargetModules : public CommandObjectMultiword 3459 { 3460 public: 3461 //------------------------------------------------------------------ 3462 // Constructors and Destructors 3463 //------------------------------------------------------------------ 3464 CommandObjectTargetModules(CommandInterpreter &interpreter) : 3465 CommandObjectMultiword (interpreter, 3466 "target modules", 3467 "A set of commands for accessing information for one or more target modules.", 3468 "target modules <sub-command> ...") 3469 { 3470 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetModulesAdd (interpreter))); 3471 LoadSubCommand ("load", CommandObjectSP (new CommandObjectTargetModulesLoad (interpreter))); 3472 //LoadSubCommand ("unload", CommandObjectSP (new CommandObjectTargetModulesUnload (interpreter))); 3473 LoadSubCommand ("dump", CommandObjectSP (new CommandObjectTargetModulesDump (interpreter))); 3474 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetModulesList (interpreter))); 3475 LoadSubCommand ("lookup", CommandObjectSP (new CommandObjectTargetModulesLookup (interpreter))); 3476 LoadSubCommand ("search-paths", CommandObjectSP (new CommandObjectTargetModulesImageSearchPaths (interpreter))); 3477 3478 } 3479 virtual 3480 ~CommandObjectTargetModules() 3481 { 3482 } 3483 3484 private: 3485 //------------------------------------------------------------------ 3486 // For CommandObjectTargetModules only 3487 //------------------------------------------------------------------ 3488 DISALLOW_COPY_AND_ASSIGN (CommandObjectTargetModules); 3489 }; 3490 3491 3492 #pragma mark CommandObjectTargetStopHookAdd 3493 3494 //------------------------------------------------------------------------- 3495 // CommandObjectTargetStopHookAdd 3496 //------------------------------------------------------------------------- 3497 3498 class CommandObjectTargetStopHookAdd : public CommandObject 3499 { 3500 public: 3501 3502 class CommandOptions : public Options 3503 { 3504 public: 3505 CommandOptions (CommandInterpreter &interpreter) : 3506 Options(interpreter), 3507 m_line_start(0), 3508 m_line_end (UINT_MAX), 3509 m_func_name_type_mask (eFunctionNameTypeAuto), 3510 m_sym_ctx_specified (false), 3511 m_thread_specified (false), 3512 m_use_one_liner (false), 3513 m_one_liner() 3514 { 3515 } 3516 3517 ~CommandOptions () {} 3518 3519 const OptionDefinition* 3520 GetDefinitions () 3521 { 3522 return g_option_table; 3523 } 3524 3525 virtual Error 3526 SetOptionValue (uint32_t option_idx, const char *option_arg) 3527 { 3528 Error error; 3529 char short_option = (char) m_getopt_table[option_idx].val; 3530 bool success; 3531 3532 switch (short_option) 3533 { 3534 case 'c': 3535 m_class_name = option_arg; 3536 m_sym_ctx_specified = true; 3537 break; 3538 3539 case 'e': 3540 m_line_end = Args::StringToUInt32 (option_arg, UINT_MAX, 0, &success); 3541 if (!success) 3542 { 3543 error.SetErrorStringWithFormat ("invalid end line number: \"%s\"", option_arg); 3544 break; 3545 } 3546 m_sym_ctx_specified = true; 3547 break; 3548 3549 case 'l': 3550 m_line_start = Args::StringToUInt32 (option_arg, 0, 0, &success); 3551 if (!success) 3552 { 3553 error.SetErrorStringWithFormat ("invalid start line number: \"%s\"", option_arg); 3554 break; 3555 } 3556 m_sym_ctx_specified = true; 3557 break; 3558 3559 case 'n': 3560 m_function_name = option_arg; 3561 m_func_name_type_mask |= eFunctionNameTypeAuto; 3562 m_sym_ctx_specified = true; 3563 break; 3564 3565 case 'f': 3566 m_file_name = option_arg; 3567 m_sym_ctx_specified = true; 3568 break; 3569 case 's': 3570 m_module_name = option_arg; 3571 m_sym_ctx_specified = true; 3572 break; 3573 case 't' : 3574 { 3575 m_thread_id = Args::StringToUInt64(option_arg, LLDB_INVALID_THREAD_ID, 0); 3576 if (m_thread_id == LLDB_INVALID_THREAD_ID) 3577 error.SetErrorStringWithFormat ("invalid thread id string '%s'", option_arg); 3578 m_thread_specified = true; 3579 } 3580 break; 3581 case 'T': 3582 m_thread_name = option_arg; 3583 m_thread_specified = true; 3584 break; 3585 case 'q': 3586 m_queue_name = option_arg; 3587 m_thread_specified = true; 3588 break; 3589 case 'x': 3590 { 3591 m_thread_index = Args::StringToUInt32(option_arg, UINT32_MAX, 0); 3592 if (m_thread_id == UINT32_MAX) 3593 error.SetErrorStringWithFormat ("invalid thread index string '%s'", option_arg); 3594 m_thread_specified = true; 3595 } 3596 break; 3597 case 'o': 3598 m_use_one_liner = true; 3599 m_one_liner = option_arg; 3600 break; 3601 default: 3602 error.SetErrorStringWithFormat ("unrecognized option %c.", short_option); 3603 break; 3604 } 3605 return error; 3606 } 3607 3608 void 3609 OptionParsingStarting () 3610 { 3611 m_class_name.clear(); 3612 m_function_name.clear(); 3613 m_line_start = 0; 3614 m_line_end = UINT_MAX; 3615 m_file_name.clear(); 3616 m_module_name.clear(); 3617 m_func_name_type_mask = eFunctionNameTypeAuto; 3618 m_thread_id = LLDB_INVALID_THREAD_ID; 3619 m_thread_index = UINT32_MAX; 3620 m_thread_name.clear(); 3621 m_queue_name.clear(); 3622 3623 m_sym_ctx_specified = false; 3624 m_thread_specified = false; 3625 3626 m_use_one_liner = false; 3627 m_one_liner.clear(); 3628 } 3629 3630 3631 static OptionDefinition g_option_table[]; 3632 3633 std::string m_class_name; 3634 std::string m_function_name; 3635 uint32_t m_line_start; 3636 uint32_t m_line_end; 3637 std::string m_file_name; 3638 std::string m_module_name; 3639 uint32_t m_func_name_type_mask; // A pick from lldb::FunctionNameType. 3640 lldb::tid_t m_thread_id; 3641 uint32_t m_thread_index; 3642 std::string m_thread_name; 3643 std::string m_queue_name; 3644 bool m_sym_ctx_specified; 3645 bool m_thread_specified; 3646 // Instance variables to hold the values for one_liner options. 3647 bool m_use_one_liner; 3648 std::string m_one_liner; 3649 }; 3650 3651 Options * 3652 GetOptions () 3653 { 3654 return &m_options; 3655 } 3656 3657 CommandObjectTargetStopHookAdd (CommandInterpreter &interpreter) : 3658 CommandObject (interpreter, 3659 "target stop-hook add ", 3660 "Add a hook to be executed when the target stops.", 3661 "target stop-hook add"), 3662 m_options (interpreter) 3663 { 3664 } 3665 3666 ~CommandObjectTargetStopHookAdd () 3667 { 3668 } 3669 3670 static size_t 3671 ReadCommandsCallbackFunction (void *baton, 3672 InputReader &reader, 3673 lldb::InputReaderAction notification, 3674 const char *bytes, 3675 size_t bytes_len) 3676 { 3677 StreamSP out_stream = reader.GetDebugger().GetAsyncOutputStream(); 3678 Target::StopHook *new_stop_hook = ((Target::StopHook *) baton); 3679 static bool got_interrupted; 3680 bool batch_mode = reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode(); 3681 3682 switch (notification) 3683 { 3684 case eInputReaderActivate: 3685 if (!batch_mode) 3686 { 3687 out_stream->Printf ("%s\n", "Enter your stop hook command(s). Type 'DONE' to end."); 3688 if (reader.GetPrompt()) 3689 out_stream->Printf ("%s", reader.GetPrompt()); 3690 out_stream->Flush(); 3691 } 3692 got_interrupted = false; 3693 break; 3694 3695 case eInputReaderDeactivate: 3696 break; 3697 3698 case eInputReaderReactivate: 3699 if (reader.GetPrompt() && !batch_mode) 3700 { 3701 out_stream->Printf ("%s", reader.GetPrompt()); 3702 out_stream->Flush(); 3703 } 3704 got_interrupted = false; 3705 break; 3706 3707 case eInputReaderAsynchronousOutputWritten: 3708 break; 3709 3710 case eInputReaderGotToken: 3711 if (bytes && bytes_len && baton) 3712 { 3713 StringList *commands = new_stop_hook->GetCommandPointer(); 3714 if (commands) 3715 { 3716 commands->AppendString (bytes, bytes_len); 3717 } 3718 } 3719 if (!reader.IsDone() && reader.GetPrompt() && !batch_mode) 3720 { 3721 out_stream->Printf ("%s", reader.GetPrompt()); 3722 out_stream->Flush(); 3723 } 3724 break; 3725 3726 case eInputReaderInterrupt: 3727 { 3728 // Finish, and cancel the stop hook. 3729 new_stop_hook->GetTarget()->RemoveStopHookByID(new_stop_hook->GetID()); 3730 if (!batch_mode) 3731 { 3732 out_stream->Printf ("Stop hook cancelled.\n"); 3733 out_stream->Flush(); 3734 } 3735 3736 reader.SetIsDone (true); 3737 } 3738 got_interrupted = true; 3739 break; 3740 3741 case eInputReaderEndOfFile: 3742 reader.SetIsDone (true); 3743 break; 3744 3745 case eInputReaderDone: 3746 if (!got_interrupted && !batch_mode) 3747 { 3748 out_stream->Printf ("Stop hook #%llu added.\n", new_stop_hook->GetID()); 3749 out_stream->Flush(); 3750 } 3751 break; 3752 } 3753 3754 return bytes_len; 3755 } 3756 3757 bool 3758 Execute (Args& command, 3759 CommandReturnObject &result) 3760 { 3761 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 3762 if (target) 3763 { 3764 Target::StopHookSP new_hook_sp; 3765 target->AddStopHook (new_hook_sp); 3766 3767 // First step, make the specifier. 3768 std::auto_ptr<SymbolContextSpecifier> specifier_ap; 3769 if (m_options.m_sym_ctx_specified) 3770 { 3771 specifier_ap.reset(new SymbolContextSpecifier(m_interpreter.GetDebugger().GetSelectedTarget())); 3772 3773 if (!m_options.m_module_name.empty()) 3774 { 3775 specifier_ap->AddSpecification (m_options.m_module_name.c_str(), SymbolContextSpecifier::eModuleSpecified); 3776 } 3777 3778 if (!m_options.m_class_name.empty()) 3779 { 3780 specifier_ap->AddSpecification (m_options.m_class_name.c_str(), SymbolContextSpecifier::eClassOrNamespaceSpecified); 3781 } 3782 3783 if (!m_options.m_file_name.empty()) 3784 { 3785 specifier_ap->AddSpecification (m_options.m_file_name.c_str(), SymbolContextSpecifier::eFileSpecified); 3786 } 3787 3788 if (m_options.m_line_start != 0) 3789 { 3790 specifier_ap->AddLineSpecification (m_options.m_line_start, SymbolContextSpecifier::eLineStartSpecified); 3791 } 3792 3793 if (m_options.m_line_end != UINT_MAX) 3794 { 3795 specifier_ap->AddLineSpecification (m_options.m_line_end, SymbolContextSpecifier::eLineEndSpecified); 3796 } 3797 3798 if (!m_options.m_function_name.empty()) 3799 { 3800 specifier_ap->AddSpecification (m_options.m_function_name.c_str(), SymbolContextSpecifier::eFunctionSpecified); 3801 } 3802 } 3803 3804 if (specifier_ap.get()) 3805 new_hook_sp->SetSpecifier (specifier_ap.release()); 3806 3807 // Next see if any of the thread options have been entered: 3808 3809 if (m_options.m_thread_specified) 3810 { 3811 ThreadSpec *thread_spec = new ThreadSpec(); 3812 3813 if (m_options.m_thread_id != LLDB_INVALID_THREAD_ID) 3814 { 3815 thread_spec->SetTID (m_options.m_thread_id); 3816 } 3817 3818 if (m_options.m_thread_index != UINT32_MAX) 3819 thread_spec->SetIndex (m_options.m_thread_index); 3820 3821 if (!m_options.m_thread_name.empty()) 3822 thread_spec->SetName (m_options.m_thread_name.c_str()); 3823 3824 if (!m_options.m_queue_name.empty()) 3825 thread_spec->SetQueueName (m_options.m_queue_name.c_str()); 3826 3827 new_hook_sp->SetThreadSpecifier (thread_spec); 3828 3829 } 3830 if (m_options.m_use_one_liner) 3831 { 3832 // Use one-liner. 3833 new_hook_sp->GetCommandPointer()->AppendString (m_options.m_one_liner.c_str()); 3834 result.AppendMessageWithFormat("Stop hook #%llu added.\n", new_hook_sp->GetID()); 3835 } 3836 else 3837 { 3838 // Otherwise gather up the command list, we'll push an input reader and suck the data from that directly into 3839 // the new stop hook's command string. 3840 InputReaderSP reader_sp (new InputReader(m_interpreter.GetDebugger())); 3841 if (!reader_sp) 3842 { 3843 result.AppendError("out of memory\n"); 3844 result.SetStatus (eReturnStatusFailed); 3845 target->RemoveStopHookByID (new_hook_sp->GetID()); 3846 return false; 3847 } 3848 3849 Error err (reader_sp->Initialize (CommandObjectTargetStopHookAdd::ReadCommandsCallbackFunction, 3850 new_hook_sp.get(), // baton 3851 eInputReaderGranularityLine, // token size, to pass to callback function 3852 "DONE", // end token 3853 "> ", // prompt 3854 true)); // echo input 3855 if (!err.Success()) 3856 { 3857 result.AppendError (err.AsCString()); 3858 result.SetStatus (eReturnStatusFailed); 3859 target->RemoveStopHookByID (new_hook_sp->GetID()); 3860 return false; 3861 } 3862 m_interpreter.GetDebugger().PushInputReader (reader_sp); 3863 } 3864 result.SetStatus (eReturnStatusSuccessFinishNoResult); 3865 } 3866 else 3867 { 3868 result.AppendError ("invalid target\n"); 3869 result.SetStatus (eReturnStatusFailed); 3870 } 3871 3872 return result.Succeeded(); 3873 } 3874 private: 3875 CommandOptions m_options; 3876 }; 3877 3878 OptionDefinition 3879 CommandObjectTargetStopHookAdd::CommandOptions::g_option_table[] = 3880 { 3881 { LLDB_OPT_SET_ALL, false, "one-liner", 'o', required_argument, NULL, NULL, eArgTypeOneLiner, 3882 "Specify a one-line breakpoint command inline. Be sure to surround it with quotes." }, 3883 { LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName, 3884 "Set the module within which the stop-hook is to be run."}, 3885 { LLDB_OPT_SET_ALL, false, "thread-index", 'x', required_argument, NULL, NULL, eArgTypeThreadIndex, 3886 "The stop hook is run only for the thread whose index matches this argument."}, 3887 { LLDB_OPT_SET_ALL, false, "thread-id", 't', required_argument, NULL, NULL, eArgTypeThreadID, 3888 "The stop hook is run only for the thread whose TID matches this argument."}, 3889 { LLDB_OPT_SET_ALL, false, "thread-name", 'T', required_argument, NULL, NULL, eArgTypeThreadName, 3890 "The stop hook is run only for the thread whose thread name matches this argument."}, 3891 { LLDB_OPT_SET_ALL, false, "queue-name", 'q', required_argument, NULL, NULL, eArgTypeQueueName, 3892 "The stop hook is run only for threads in the queue whose name is given by this argument."}, 3893 { LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, 3894 "Specify the source file within which the stop-hook is to be run." }, 3895 { LLDB_OPT_SET_1, false, "start-line", 'l', required_argument, NULL, 0, eArgTypeLineNum, 3896 "Set the start of the line range for which the stop-hook is to be run."}, 3897 { LLDB_OPT_SET_1, false, "end-line", 'e', required_argument, NULL, 0, eArgTypeLineNum, 3898 "Set the end of the line range for which the stop-hook is to be run."}, 3899 { LLDB_OPT_SET_2, false, "classname", 'c', required_argument, NULL, NULL, eArgTypeClassName, 3900 "Specify the class within which the stop-hook is to be run." }, 3901 { LLDB_OPT_SET_3, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, 3902 "Set the function name within which the stop hook will be run." }, 3903 { 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL } 3904 }; 3905 3906 #pragma mark CommandObjectTargetStopHookDelete 3907 3908 //------------------------------------------------------------------------- 3909 // CommandObjectTargetStopHookDelete 3910 //------------------------------------------------------------------------- 3911 3912 class CommandObjectTargetStopHookDelete : public CommandObject 3913 { 3914 public: 3915 3916 CommandObjectTargetStopHookDelete (CommandInterpreter &interpreter) : 3917 CommandObject (interpreter, 3918 "target stop-hook delete", 3919 "Delete a stop-hook.", 3920 "target stop-hook delete [<idx>]") 3921 { 3922 } 3923 3924 ~CommandObjectTargetStopHookDelete () 3925 { 3926 } 3927 3928 bool 3929 Execute (Args& command, 3930 CommandReturnObject &result) 3931 { 3932 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 3933 if (target) 3934 { 3935 // FIXME: see if we can use the breakpoint id style parser? 3936 size_t num_args = command.GetArgumentCount(); 3937 if (num_args == 0) 3938 { 3939 if (!m_interpreter.Confirm ("Delete all stop hooks?", true)) 3940 { 3941 result.SetStatus (eReturnStatusFailed); 3942 return false; 3943 } 3944 else 3945 { 3946 target->RemoveAllStopHooks(); 3947 } 3948 } 3949 else 3950 { 3951 bool success; 3952 for (size_t i = 0; i < num_args; i++) 3953 { 3954 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success); 3955 if (!success) 3956 { 3957 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i)); 3958 result.SetStatus(eReturnStatusFailed); 3959 return false; 3960 } 3961 success = target->RemoveStopHookByID (user_id); 3962 if (!success) 3963 { 3964 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i)); 3965 result.SetStatus(eReturnStatusFailed); 3966 return false; 3967 } 3968 } 3969 } 3970 result.SetStatus (eReturnStatusSuccessFinishNoResult); 3971 } 3972 else 3973 { 3974 result.AppendError ("invalid target\n"); 3975 result.SetStatus (eReturnStatusFailed); 3976 } 3977 3978 return result.Succeeded(); 3979 } 3980 }; 3981 #pragma mark CommandObjectTargetStopHookEnableDisable 3982 3983 //------------------------------------------------------------------------- 3984 // CommandObjectTargetStopHookEnableDisable 3985 //------------------------------------------------------------------------- 3986 3987 class CommandObjectTargetStopHookEnableDisable : public CommandObject 3988 { 3989 public: 3990 3991 CommandObjectTargetStopHookEnableDisable (CommandInterpreter &interpreter, bool enable, const char *name, const char *help, const char *syntax) : 3992 CommandObject (interpreter, 3993 name, 3994 help, 3995 syntax), 3996 m_enable (enable) 3997 { 3998 } 3999 4000 ~CommandObjectTargetStopHookEnableDisable () 4001 { 4002 } 4003 4004 bool 4005 Execute (Args& command, 4006 CommandReturnObject &result) 4007 { 4008 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 4009 if (target) 4010 { 4011 // FIXME: see if we can use the breakpoint id style parser? 4012 size_t num_args = command.GetArgumentCount(); 4013 bool success; 4014 4015 if (num_args == 0) 4016 { 4017 target->SetAllStopHooksActiveState (m_enable); 4018 } 4019 else 4020 { 4021 for (size_t i = 0; i < num_args; i++) 4022 { 4023 lldb::user_id_t user_id = Args::StringToUInt32 (command.GetArgumentAtIndex(i), 0, 0, &success); 4024 if (!success) 4025 { 4026 result.AppendErrorWithFormat ("invalid stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i)); 4027 result.SetStatus(eReturnStatusFailed); 4028 return false; 4029 } 4030 success = target->SetStopHookActiveStateByID (user_id, m_enable); 4031 if (!success) 4032 { 4033 result.AppendErrorWithFormat ("unknown stop hook id: \"%s\".\n", command.GetArgumentAtIndex(i)); 4034 result.SetStatus(eReturnStatusFailed); 4035 return false; 4036 } 4037 } 4038 } 4039 result.SetStatus (eReturnStatusSuccessFinishNoResult); 4040 } 4041 else 4042 { 4043 result.AppendError ("invalid target\n"); 4044 result.SetStatus (eReturnStatusFailed); 4045 } 4046 return result.Succeeded(); 4047 } 4048 private: 4049 bool m_enable; 4050 }; 4051 4052 #pragma mark CommandObjectTargetStopHookList 4053 4054 //------------------------------------------------------------------------- 4055 // CommandObjectTargetStopHookList 4056 //------------------------------------------------------------------------- 4057 4058 class CommandObjectTargetStopHookList : public CommandObject 4059 { 4060 public: 4061 4062 CommandObjectTargetStopHookList (CommandInterpreter &interpreter) : 4063 CommandObject (interpreter, 4064 "target stop-hook list", 4065 "List all stop-hooks.", 4066 "target stop-hook list [<type>]") 4067 { 4068 } 4069 4070 ~CommandObjectTargetStopHookList () 4071 { 4072 } 4073 4074 bool 4075 Execute (Args& command, 4076 CommandReturnObject &result) 4077 { 4078 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); 4079 if (!target) 4080 { 4081 result.AppendError ("invalid target\n"); 4082 result.SetStatus (eReturnStatusFailed); 4083 return result.Succeeded(); 4084 } 4085 4086 size_t num_hooks = target->GetNumStopHooks (); 4087 if (num_hooks == 0) 4088 { 4089 result.GetOutputStream().PutCString ("No stop hooks.\n"); 4090 } 4091 else 4092 { 4093 for (size_t i = 0; i < num_hooks; i++) 4094 { 4095 Target::StopHookSP this_hook = target->GetStopHookAtIndex (i); 4096 if (i > 0) 4097 result.GetOutputStream().PutCString ("\n"); 4098 this_hook->GetDescription (&(result.GetOutputStream()), eDescriptionLevelFull); 4099 } 4100 } 4101 result.SetStatus (eReturnStatusSuccessFinishResult); 4102 return result.Succeeded(); 4103 } 4104 }; 4105 4106 #pragma mark CommandObjectMultiwordTargetStopHooks 4107 //------------------------------------------------------------------------- 4108 // CommandObjectMultiwordTargetStopHooks 4109 //------------------------------------------------------------------------- 4110 4111 class CommandObjectMultiwordTargetStopHooks : public CommandObjectMultiword 4112 { 4113 public: 4114 4115 CommandObjectMultiwordTargetStopHooks (CommandInterpreter &interpreter) : 4116 CommandObjectMultiword (interpreter, 4117 "target stop-hook", 4118 "A set of commands for operating on debugger target stop-hooks.", 4119 "target stop-hook <subcommand> [<subcommand-options>]") 4120 { 4121 LoadSubCommand ("add", CommandObjectSP (new CommandObjectTargetStopHookAdd (interpreter))); 4122 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetStopHookDelete (interpreter))); 4123 LoadSubCommand ("disable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter, 4124 false, 4125 "target stop-hook disable [<id>]", 4126 "Disable a stop-hook.", 4127 "target stop-hook disable"))); 4128 LoadSubCommand ("enable", CommandObjectSP (new CommandObjectTargetStopHookEnableDisable (interpreter, 4129 true, 4130 "target stop-hook enable [<id>]", 4131 "Enable a stop-hook.", 4132 "target stop-hook enable"))); 4133 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetStopHookList (interpreter))); 4134 } 4135 4136 ~CommandObjectMultiwordTargetStopHooks() 4137 { 4138 } 4139 }; 4140 4141 4142 4143 #pragma mark CommandObjectMultiwordTarget 4144 4145 //------------------------------------------------------------------------- 4146 // CommandObjectMultiwordTarget 4147 //------------------------------------------------------------------------- 4148 4149 CommandObjectMultiwordTarget::CommandObjectMultiwordTarget (CommandInterpreter &interpreter) : 4150 CommandObjectMultiword (interpreter, 4151 "target", 4152 "A set of commands for operating on debugger targets.", 4153 "target <subcommand> [<subcommand-options>]") 4154 { 4155 4156 LoadSubCommand ("create", CommandObjectSP (new CommandObjectTargetCreate (interpreter))); 4157 LoadSubCommand ("delete", CommandObjectSP (new CommandObjectTargetDelete (interpreter))); 4158 LoadSubCommand ("list", CommandObjectSP (new CommandObjectTargetList (interpreter))); 4159 LoadSubCommand ("select", CommandObjectSP (new CommandObjectTargetSelect (interpreter))); 4160 LoadSubCommand ("stop-hook", CommandObjectSP (new CommandObjectMultiwordTargetStopHooks (interpreter))); 4161 LoadSubCommand ("modules", CommandObjectSP (new CommandObjectTargetModules (interpreter))); 4162 LoadSubCommand ("variable", CommandObjectSP (new CommandObjectTargetVariable (interpreter))); 4163 } 4164 4165 CommandObjectMultiwordTarget::~CommandObjectMultiwordTarget () 4166 { 4167 } 4168 4169 4170