Target.h revision b344843f75ef893762c93fd0a22d2d45712ce74d
1//===-- Target.h ------------------------------------------------*- 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#ifndef liblldb_Target_h_ 11#define liblldb_Target_h_ 12 13// C Includes 14// C++ Includes 15 16// Other libraries and framework includes 17// Project includes 18#include "lldb/lldb-public.h" 19#include "lldb/Breakpoint/BreakpointList.h" 20#include "lldb/Breakpoint/BreakpointLocationCollection.h" 21#include "lldb/Core/Broadcaster.h" 22#include "lldb/Core/Event.h" 23#include "lldb/Core/ModuleList.h" 24#include "lldb/Core/UserSettingsController.h" 25#include "lldb/Symbol/SymbolContext.h" 26#include "lldb/Target/ABI.h" 27#include "lldb/Target/ExecutionContextScope.h" 28#include "lldb/Target/PathMappingList.h" 29#include "lldb/Target/SectionLoadList.h" 30 31#include "lldb/API/SBTarget.h" 32 33namespace lldb_private { 34 35//---------------------------------------------------------------------- 36// TargetInstanceSettings 37//---------------------------------------------------------------------- 38class TargetInstanceSettings : public InstanceSettings 39{ 40public: 41 42 TargetInstanceSettings (UserSettingsController &owner, bool live_instance = true, const char *name = NULL); 43 44 TargetInstanceSettings (const TargetInstanceSettings &rhs); 45 46 virtual 47 ~TargetInstanceSettings (); 48 49 TargetInstanceSettings& 50 operator= (const TargetInstanceSettings &rhs); 51 52 void 53 UpdateInstanceSettingsVariable (const ConstString &var_name, 54 const char *index_value, 55 const char *value, 56 const ConstString &instance_name, 57 const SettingEntry &entry, 58 VarSetOperationType op, 59 Error &err, 60 bool pending); 61 62 bool 63 GetInstanceSettingsValue (const SettingEntry &entry, 64 const ConstString &var_name, 65 StringList &value, 66 Error *err); 67 68protected: 69 70 void 71 CopyInstanceSettings (const lldb::InstanceSettingsSP &new_settings, 72 bool pending); 73 74 const ConstString 75 CreateInstanceName (); 76 77 std::string m_expr_prefix_path; 78 std::string m_expr_prefix_contents; 79 80}; 81 82//---------------------------------------------------------------------- 83// Target 84//---------------------------------------------------------------------- 85class Target : 86 public Broadcaster, 87 public ExecutionContextScope, 88 public TargetInstanceSettings 89{ 90public: 91 friend class TargetList; 92 93 //------------------------------------------------------------------ 94 /// Broadcaster event bits definitions. 95 //------------------------------------------------------------------ 96 enum 97 { 98 eBroadcastBitBreakpointChanged = (1 << 0), 99 eBroadcastBitModulesLoaded = (1 << 1), 100 eBroadcastBitModulesUnloaded = (1 << 2) 101 }; 102 103 static void 104 SettingsInitialize (); 105 106 static void 107 SettingsTerminate (); 108 109 static lldb::UserSettingsControllerSP & 110 GetSettingsController (); 111 112 static ArchSpec 113 GetDefaultArchitecture (); 114 115 static void 116 SetDefaultArchitecture (const ArchSpec &arch); 117 118 void 119 UpdateInstanceName (); 120 121 lldb::ModuleSP 122 GetSharedModule (const FileSpec& file_spec, 123 const ArchSpec& arch, 124 const lldb_private::UUID *uuid = NULL, 125 const ConstString *object_name = NULL, 126 off_t object_offset = 0, 127 Error *error_ptr = NULL); 128private: 129 //------------------------------------------------------------------ 130 /// Construct with optional file and arch. 131 /// 132 /// This member is private. Clients must use 133 /// TargetList::CreateTarget(const FileSpec*, const ArchSpec*) 134 /// so all targets can be tracked from the central target list. 135 /// 136 /// @see TargetList::CreateTarget(const FileSpec*, const ArchSpec*) 137 //------------------------------------------------------------------ 138 Target(Debugger &debugger, const lldb::PlatformSP &platform_sp); 139 140public: 141 ~Target(); 142 143 Mutex & 144 GetAPIMutex () 145 { 146 return m_mutex; 147 } 148 149 void 150 DeleteCurrentProcess (); 151 152 //------------------------------------------------------------------ 153 /// Dump a description of this object to a Stream. 154 /// 155 /// Dump a description of the contents of this object to the 156 /// supplied stream \a s. The dumped content will be only what has 157 /// been loaded or parsed up to this point at which this function 158 /// is called, so this is a good way to see what has been parsed 159 /// in a target. 160 /// 161 /// @param[in] s 162 /// The stream to which to dump the object descripton. 163 //------------------------------------------------------------------ 164 void 165 Dump (Stream *s, lldb::DescriptionLevel description_level); 166 167 const lldb::ProcessSP & 168 CreateProcess (Listener &listener, const char *plugin_name = NULL); 169 170 const lldb::ProcessSP & 171 GetProcessSP () const; 172 173 lldb::TargetSP 174 GetSP(); 175 176 //------------------------------------------------------------------ 177 // This part handles the breakpoints. 178 //------------------------------------------------------------------ 179 180 BreakpointList & 181 GetBreakpointList(bool internal = false); 182 183 const BreakpointList & 184 GetBreakpointList(bool internal = false) const; 185 186 lldb::BreakpointSP 187 GetLastCreatedBreakpoint () 188 { 189 return m_last_created_breakpoint; 190 } 191 192 lldb::BreakpointSP 193 GetBreakpointByID (lldb::break_id_t break_id); 194 195 // Use this to create a file and line breakpoint to a given module or all module it is NULL 196 lldb::BreakpointSP 197 CreateBreakpoint (const FileSpec *containingModule, 198 const FileSpec &file, 199 uint32_t line_no, 200 bool check_inlines, 201 bool internal = false); 202 203 // Use this to create a breakpoint from a load address 204 lldb::BreakpointSP 205 CreateBreakpoint (lldb::addr_t load_addr, 206 bool internal = false); 207 208 // Use this to create Address breakpoints: 209 lldb::BreakpointSP 210 CreateBreakpoint (Address &addr, 211 bool internal = false); 212 213 // Use this to create a function breakpoint by regexp in containingModule, or all modules if it is NULL 214 lldb::BreakpointSP 215 CreateBreakpoint (FileSpec *containingModule, 216 RegularExpression &func_regexp, 217 bool internal = false); 218 219 // Use this to create a function breakpoint by name in containingModule, or all modules if it is NULL 220 lldb::BreakpointSP 221 CreateBreakpoint (FileSpec *containingModule, 222 const char *func_name, 223 uint32_t func_name_type_mask, 224 bool internal = false); 225 226 // Use this to create a general breakpoint: 227 lldb::BreakpointSP 228 CreateBreakpoint (lldb::SearchFilterSP &filter_sp, 229 lldb::BreakpointResolverSP &resolver_sp, 230 bool internal = false); 231 232 void 233 RemoveAllBreakpoints (bool internal_also = false); 234 235 void 236 DisableAllBreakpoints (bool internal_also = false); 237 238 void 239 EnableAllBreakpoints (bool internal_also = false); 240 241 bool 242 DisableBreakpointByID (lldb::break_id_t break_id); 243 244 bool 245 EnableBreakpointByID (lldb::break_id_t break_id); 246 247 bool 248 RemoveBreakpointByID (lldb::break_id_t break_id); 249 250 void 251 ModulesDidLoad (ModuleList &module_list); 252 253 void 254 ModulesDidUnload (ModuleList &module_list); 255 256protected: 257 void 258 ModuleAdded (lldb::ModuleSP &module_sp); 259 260 void 261 ModuleUpdated (lldb::ModuleSP &old_module_sp, lldb::ModuleSP &new_module_sp); 262 263public: 264 //------------------------------------------------------------------ 265 /// Gets the module for the main executable. 266 /// 267 /// Each process has a notion of a main executable that is the file 268 /// that will be executed or attached to. Executable files can have 269 /// dependent modules that are discovered from the object files, or 270 /// discovered at runtime as things are dynamically loaded. 271 /// 272 /// @return 273 /// The shared pointer to the executable module which can 274 /// contains a NULL Module object if no executable has been 275 /// set. 276 /// 277 /// @see DynamicLoader 278 /// @see ObjectFile::GetDependentModules (FileSpecList&) 279 /// @see Process::SetExecutableModule(lldb::ModuleSP&) 280 //------------------------------------------------------------------ 281 lldb::ModuleSP 282 GetExecutableModule (); 283 284 //------------------------------------------------------------------ 285 /// Set the main executable module. 286 /// 287 /// Each process has a notion of a main executable that is the file 288 /// that will be executed or attached to. Executable files can have 289 /// dependent modules that are discovered from the object files, or 290 /// discovered at runtime as things are dynamically loaded. 291 /// 292 /// Setting the executable causes any of the current dependant 293 /// image information to be cleared and replaced with the static 294 /// dependent image information found by calling 295 /// ObjectFile::GetDependentModules (FileSpecList&) on the main 296 /// executable and any modules on which it depends. Calling 297 /// Process::GetImages() will return the newly found images that 298 /// were obtained from all of the object files. 299 /// 300 /// @param[in] module_sp 301 /// A shared pointer reference to the module that will become 302 /// the main executable for this process. 303 /// 304 /// @param[in] get_dependent_files 305 /// If \b true then ask the object files to track down any 306 /// known dependent files. 307 /// 308 /// @see ObjectFile::GetDependentModules (FileSpecList&) 309 /// @see Process::GetImages() 310 //------------------------------------------------------------------ 311 void 312 SetExecutableModule (lldb::ModuleSP& module_sp, bool get_dependent_files); 313 314 //------------------------------------------------------------------ 315 /// Get accessor for the images for this process. 316 /// 317 /// Each process has a notion of a main executable that is the file 318 /// that will be executed or attached to. Executable files can have 319 /// dependent modules that are discovered from the object files, or 320 /// discovered at runtime as things are dynamically loaded. After 321 /// a main executable has been set, the images will contain a list 322 /// of all the files that the executable depends upon as far as the 323 /// object files know. These images will usually contain valid file 324 /// virtual addresses only. When the process is launched or attached 325 /// to, the DynamicLoader plug-in will discover where these images 326 /// were loaded in memory and will resolve the load virtual 327 /// addresses is each image, and also in images that are loaded by 328 /// code. 329 /// 330 /// @return 331 /// A list of Module objects in a module list. 332 //------------------------------------------------------------------ 333 ModuleList& 334 GetImages () 335 { 336 return m_images; 337 } 338 339 const ModuleList& 340 GetImages () const 341 { 342 return m_images; 343 } 344 345 ArchSpec & 346 GetArchitecture () 347 { 348 return m_arch_spec; 349 } 350 351 const ArchSpec & 352 GetArchitecture () const 353 { 354 return m_arch_spec; 355 } 356 357 //------------------------------------------------------------------ 358 /// Set the architecture for this target. 359 /// 360 /// If the current target has no Images read in, then this just sets the architecture, which will 361 /// be used to select the architecture of the ExecutableModule when that is set. 362 /// If the current target has an ExecutableModule, then calling SetArchitecture with a different 363 /// architecture from the currently selected one will reset the ExecutableModule to that slice 364 /// of the file backing the ExecutableModule. If the file backing the ExecutableModule does not 365 /// contain a fork of this architecture, then this code will return false, and the architecture 366 /// won't be changed. 367 /// If the input arch_spec is the same as the already set architecture, this is a no-op. 368 /// 369 /// @param[in] arch_spec 370 /// The new architecture. 371 /// 372 /// @return 373 /// \b true if the architecture was successfully set, \bfalse otherwise. 374 //------------------------------------------------------------------ 375 bool 376 SetArchitecture (const ArchSpec &arch_spec); 377 378 Debugger & 379 GetDebugger () 380 { 381 return m_debugger; 382 } 383 384 size_t 385 ReadMemoryFromFileCache (const Address& addr, 386 void *dst, 387 size_t dst_len, 388 Error &error); 389 390 // Reading memory through the target allows us to skip going to the process 391 // for reading memory if possible and it allows us to try and read from 392 // any constant sections in our object files on disk. If you always want 393 // live program memory, read straight from the process. If you possibly 394 // want to read from const sections in object files, read from the target. 395 // This version of ReadMemory will try and read memory from the process 396 // if the process is alive. The order is: 397 // 1 - if (prefer_file_cache == true) then read from object file cache 398 // 2 - if there is a valid process, try and read from its memory 399 // 3 - if (prefer_file_cache == false) then read from object file cache 400 size_t 401 ReadMemory (const Address& addr, 402 bool prefer_file_cache, 403 void *dst, 404 size_t dst_len, 405 Error &error); 406 407 SectionLoadList& 408 GetSectionLoadList() 409 { 410 return m_section_load_list; 411 } 412 413 const SectionLoadList& 414 GetSectionLoadList() const 415 { 416 return m_section_load_list; 417 } 418 419 420 static Target * 421 GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, 422 const SymbolContext *sc_ptr); 423 424 //------------------------------------------------------------------ 425 // lldb::ExecutionContextScope pure virtual functions 426 //------------------------------------------------------------------ 427 virtual Target * 428 CalculateTarget (); 429 430 virtual Process * 431 CalculateProcess (); 432 433 virtual Thread * 434 CalculateThread (); 435 436 virtual StackFrame * 437 CalculateStackFrame (); 438 439 virtual void 440 CalculateExecutionContext (ExecutionContext &exe_ctx); 441 442 PathMappingList & 443 GetImageSearchPathList (); 444 445 ClangASTContext * 446 GetScratchClangASTContext(); 447 448 const char * 449 GetExpressionPrefixContentsAsCString (); 450 451 // Since expressions results can persist beyond the lifetime of a process, 452 // and the const expression results are available after a process is gone, 453 // we provide a way for expressions to be evaluated from the Target itself. 454 // If an expression is going to be run, then it should have a frame filled 455 // in in th execution context. 456 ExecutionResults 457 EvaluateExpression (const char *expression, 458 StackFrame *frame, 459 bool unwind_on_error, 460 bool keep_in_memory, 461 lldb::ValueObjectSP &result_valobj_sp); 462 463 ClangPersistentVariables & 464 GetPersistentVariables() 465 { 466 return m_persistent_variables; 467 } 468 469 //------------------------------------------------------------------ 470 // Target Stop Hooks 471 //------------------------------------------------------------------ 472 class StopHook : public UserID 473 { 474 public: 475 ~StopHook (); 476 477 StopHook (const StopHook &rhs); 478 479 StringList * 480 GetCommandPointer () 481 { 482 return &m_commands; 483 } 484 485 const StringList & 486 GetCommands() 487 { 488 return m_commands; 489 } 490 491 lldb::TargetSP & 492 GetTarget() 493 { 494 return m_target_sp; 495 } 496 497 void 498 SetCommands (StringList &in_commands) 499 { 500 m_commands = in_commands; 501 } 502 503 // Set the specifier. The stop hook will own the specifier, and is responsible for deleting it when we're done. 504 void 505 SetSpecifier (SymbolContextSpecifier *specifier) 506 { 507 m_specifier_sp.reset (specifier); 508 } 509 510 SymbolContextSpecifier * 511 GetSpecifier () 512 { 513 return m_specifier_sp.get(); 514 } 515 516 // Set the Thread Specifier. The stop hook will own the thread specifier, and is responsible for deleting it when we're done. 517 void 518 SetThreadSpecifier (ThreadSpec *specifier); 519 520 ThreadSpec * 521 GetThreadSpecifier() 522 { 523 return m_thread_spec_ap.get(); 524 } 525 526 bool 527 IsActive() 528 { 529 return m_active; 530 } 531 532 void 533 SetIsActive (bool is_active) 534 { 535 m_active = is_active; 536 } 537 538 void 539 GetDescription (Stream *s, lldb::DescriptionLevel level) const; 540 541 private: 542 lldb::TargetSP m_target_sp; 543 StringList m_commands; 544 lldb::SymbolContextSpecifierSP m_specifier_sp; 545 std::auto_ptr<ThreadSpec> m_thread_spec_ap; 546 bool m_active; 547 548 // Use AddStopHook to make a new empty stop hook. The GetCommandPointer and fill it with commands, 549 // and SetSpecifier to set the specifier shared pointer (can be null, that will match anything.) 550 StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid); 551 friend class Target; 552 }; 553 typedef lldb::SharedPtr<StopHook>::Type StopHookSP; 554 555 // Add an empty stop hook to the Target's stop hook list, and returns a shared pointer to it in new_hook. 556 // Returns the id of the new hook. 557 lldb::user_id_t 558 AddStopHook (StopHookSP &new_hook); 559 560 void 561 RunStopHooks (); 562 563 size_t 564 GetStopHookSize(); 565 566// StopHookSP & 567// GetStopHookByIndex (size_t index); 568// 569 bool 570 RemoveStopHookByID (lldb::user_id_t uid); 571 572 void 573 RemoveAllStopHooks (); 574 575 StopHookSP 576 GetStopHookByID (lldb::user_id_t uid); 577 578 bool 579 SetStopHookActiveStateByID (lldb::user_id_t uid, bool active_state); 580 581 void 582 SetAllStopHooksActiveState (bool active_state); 583 584 size_t GetNumStopHooks () const 585 { 586 return m_stop_hooks.size(); 587 } 588 589 StopHookSP 590 GetStopHookAtIndex (size_t index) 591 { 592 if (index >= GetNumStopHooks()) 593 return StopHookSP(); 594 StopHookCollection::iterator pos = m_stop_hooks.begin(); 595 596 while (index > 0) 597 { 598 pos++; 599 index--; 600 } 601 return (*pos).second; 602 } 603 604 lldb::PlatformSP 605 GetPlatform () 606 { 607 return m_platform_sp; 608 } 609 610 //------------------------------------------------------------------ 611 // Target::SettingsController 612 //------------------------------------------------------------------ 613 class SettingsController : public UserSettingsController 614 { 615 public: 616 SettingsController (); 617 618 virtual 619 ~SettingsController (); 620 621 bool 622 SetGlobalVariable (const ConstString &var_name, 623 const char *index_value, 624 const char *value, 625 const SettingEntry &entry, 626 const VarSetOperationType op, 627 Error&err); 628 629 bool 630 GetGlobalVariable (const ConstString &var_name, 631 StringList &value, 632 Error &err); 633 634 static SettingEntry global_settings_table[]; 635 static SettingEntry instance_settings_table[]; 636 637 ArchSpec & 638 GetArchitecture () 639 { 640 return m_default_architecture; 641 } 642 protected: 643 644 lldb::InstanceSettingsSP 645 CreateInstanceSettings (const char *instance_name); 646 647 private: 648 649 // Class-wide settings. 650 ArchSpec m_default_architecture; 651 652 DISALLOW_COPY_AND_ASSIGN (SettingsController); 653 }; 654 655 656protected: 657 friend class lldb::SBTarget; 658 659 //------------------------------------------------------------------ 660 // Member variables. 661 //------------------------------------------------------------------ 662 Debugger & m_debugger; 663 lldb::PlatformSP m_platform_sp; ///< The platform for this target. 664 Mutex m_mutex; ///< An API mutex that is used by the lldb::SB* classes make the SB interface thread safe 665 ArchSpec m_arch_spec; 666 ModuleList m_images; ///< The list of images for this process (shared libraries and anything dynamically loaded). 667 SectionLoadList m_section_load_list; 668 BreakpointList m_breakpoint_list; 669 BreakpointList m_internal_breakpoint_list; 670 lldb::BreakpointSP m_last_created_breakpoint; 671 // We want to tightly control the process destruction process so 672 // we can correctly tear down everything that we need to, so the only 673 // class that knows about the process lifespan is this target class. 674 lldb::ProcessSP m_process_sp; 675 lldb::SearchFilterSP m_search_filter_sp; 676 PathMappingList m_image_search_paths; 677 std::auto_ptr<ClangASTContext> m_scratch_ast_context_ap; 678 ClangPersistentVariables m_persistent_variables; ///< These are the persistent variables associated with this process for the expression parser. 679 680 681 typedef std::map<lldb::user_id_t, StopHookSP> StopHookCollection; 682 StopHookCollection m_stop_hooks; 683 lldb::user_id_t m_stop_hook_next_id; 684 685 //------------------------------------------------------------------ 686 // Methods. 687 //------------------------------------------------------------------ 688 lldb::SearchFilterSP 689 GetSearchFilterForModule (const FileSpec *containingModule); 690 691 static void 692 ImageSearchPathsChanged (const PathMappingList &path_list, 693 void *baton); 694 695private: 696 DISALLOW_COPY_AND_ASSIGN (Target); 697}; 698 699} // namespace lldb_private 700 701#endif // liblldb_Target_h_ 702