1 //===-- Module.h ------------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_CORE_MODULE_H 10 #define LLDB_CORE_MODULE_H 11 12 #include "lldb/Core/Address.h" 13 #include "lldb/Core/ModuleList.h" 14 #include "lldb/Core/ModuleSpec.h" 15 #include "lldb/Symbol/ObjectFile.h" 16 #include "lldb/Symbol/SymbolContextScope.h" 17 #include "lldb/Symbol/TypeSystem.h" 18 #include "lldb/Target/PathMappingList.h" 19 #include "lldb/Target/Statistics.h" 20 #include "lldb/Utility/ArchSpec.h" 21 #include "lldb/Utility/ConstString.h" 22 #include "lldb/Utility/FileSpec.h" 23 #include "lldb/Utility/Status.h" 24 #include "lldb/Utility/UUID.h" 25 #include "lldb/Utility/XcodeSDK.h" 26 #include "lldb/lldb-defines.h" 27 #include "lldb/lldb-enumerations.h" 28 #include "lldb/lldb-forward.h" 29 #include "lldb/lldb-types.h" 30 31 #include "llvm/ADT/DenseSet.h" 32 #include "llvm/ADT/STLFunctionalExtras.h" 33 #include "llvm/ADT/StableHashing.h" 34 #include "llvm/ADT/StringRef.h" 35 #include "llvm/Support/Chrono.h" 36 37 #include <atomic> 38 #include <cstddef> 39 #include <cstdint> 40 #include <memory> 41 #include <mutex> 42 #include <optional> 43 #include <string> 44 #include <vector> 45 46 namespace lldb_private { 47 class CompilerDeclContext; 48 class Function; 49 class Log; 50 class ObjectFile; 51 class RegularExpression; 52 class SectionList; 53 class Stream; 54 class Symbol; 55 class SymbolContext; 56 class SymbolContextList; 57 class SymbolFile; 58 class Symtab; 59 class Target; 60 class TypeList; 61 class TypeMap; 62 class VariableList; 63 64 /// Options used by Module::FindFunctions. This cannot be a nested class 65 /// because it must be forward-declared in ModuleList.h. 66 struct ModuleFunctionSearchOptions { 67 /// Include the symbol table. 68 bool include_symbols = false; 69 /// Include inlined functions. 70 bool include_inlines = false; 71 }; 72 73 /// \class Module Module.h "lldb/Core/Module.h" 74 /// A class that describes an executable image and its associated 75 /// object and symbol files. 76 /// 77 /// The module is designed to be able to select a single slice of an 78 /// executable image as it would appear on disk and during program execution. 79 /// 80 /// Modules control when and if information is parsed according to which 81 /// accessors are called. For example the object file (ObjectFile) 82 /// representation will only be parsed if the object file is requested using 83 /// the Module::GetObjectFile() is called. The debug symbols will only be 84 /// parsed if the symbol file (SymbolFile) is requested using the 85 /// Module::GetSymbolFile() method. 86 /// 87 /// The module will parse more detailed information as more queries are made. 88 class Module : public std::enable_shared_from_this<Module>, 89 public SymbolContextScope { 90 public: 91 class LookupInfo; 92 // Static functions that can track the lifetime of module objects. This is 93 // handy because we might have Module objects that are in shared pointers 94 // that aren't in the global module list (from ModuleList). If this is the 95 // case we need to know about it. The modules in the global list maintained 96 // by these functions can be viewed using the "target modules list" command 97 // using the "--global" (-g for short). 98 static size_t GetNumberAllocatedModules(); 99 100 static Module *GetAllocatedModuleAtIndex(size_t idx); 101 102 static std::recursive_mutex &GetAllocationModuleCollectionMutex(); 103 104 /// Construct with file specification and architecture. 105 /// 106 /// Clients that wish to share modules with other targets should use 107 /// ModuleList::GetSharedModule(). 108 /// 109 /// \param[in] file_spec 110 /// The file specification for the on disk representation of 111 /// this executable image. 112 /// 113 /// \param[in] arch 114 /// The architecture to set as the current architecture in 115 /// this module. 116 /// 117 /// \param[in] object_name 118 /// The name of an object in a module used to extract a module 119 /// within a module (.a files and modules that contain multiple 120 /// architectures). 121 /// 122 /// \param[in] object_offset 123 /// The offset within an existing module used to extract a 124 /// module within a module (.a files and modules that contain 125 /// multiple architectures). 126 Module( 127 const FileSpec &file_spec, const ArchSpec &arch, 128 ConstString object_name = ConstString(), lldb::offset_t object_offset = 0, 129 const llvm::sys::TimePoint<> &object_mod_time = llvm::sys::TimePoint<>()); 130 131 Module(const ModuleSpec &module_spec); 132 133 template <typename ObjFilePlugin, typename... Args> CreateModuleFromObjectFile(Args &&...args)134 static lldb::ModuleSP CreateModuleFromObjectFile(Args &&...args) { 135 // Must create a module and place it into a shared pointer before we can 136 // create an object file since it has a std::weak_ptr back to the module, 137 // so we need to control the creation carefully in this static function 138 lldb::ModuleSP module_sp(new Module()); 139 module_sp->m_objfile_sp = 140 std::make_shared<ObjFilePlugin>(module_sp, std::forward<Args>(args)...); 141 module_sp->m_did_load_objfile.store(true, std::memory_order_relaxed); 142 143 // Once we get the object file, set module ArchSpec to the one we get from 144 // the object file. If the object file does not have an architecture, we 145 // consider the creation a failure. 146 ArchSpec arch = module_sp->m_objfile_sp->GetArchitecture(); 147 if (!arch) 148 return nullptr; 149 module_sp->m_arch = arch; 150 151 // Also copy the object file's FileSpec. 152 module_sp->m_file = module_sp->m_objfile_sp->GetFileSpec(); 153 return module_sp; 154 } 155 156 /// Destructor. 157 ~Module() override; 158 159 bool MatchesModuleSpec(const ModuleSpec &module_ref); 160 161 /// Set the load address for all sections in a module to be the file address 162 /// plus \a slide. 163 /// 164 /// Many times a module will be loaded in a target with a constant offset 165 /// applied to all top level sections. This function can set the load 166 /// address for all top level sections to be the section file address + 167 /// offset. 168 /// 169 /// \param[in] target 170 /// The target in which to apply the section load addresses. 171 /// 172 /// \param[in] value 173 /// if \a value_is_offset is true, then value is the offset to 174 /// apply to all file addresses for all top level sections in 175 /// the object file as each section load address is being set. 176 /// If \a value_is_offset is false, then "value" is the new 177 /// absolute base address for the image. 178 /// 179 /// \param[in] value_is_offset 180 /// If \b true, then \a value is an offset to apply to each 181 /// file address of each top level section. 182 /// If \b false, then \a value is the image base address that 183 /// will be used to rigidly slide all loadable sections. 184 /// 185 /// \param[out] changed 186 /// If any section load addresses were changed in \a target, 187 /// then \a changed will be set to \b true. Else \a changed 188 /// will be set to false. This allows this function to be 189 /// called multiple times on the same module for the same 190 /// target. If the module hasn't moved, then \a changed will 191 /// be false and no module updated notification will need to 192 /// be sent out. 193 /// 194 /// \return 195 /// /b True if any sections were successfully loaded in \a target, 196 /// /b false otherwise. 197 bool SetLoadAddress(Target &target, lldb::addr_t value, bool value_is_offset, 198 bool &changed); 199 200 /// \copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*) 201 /// 202 /// \see SymbolContextScope 203 void CalculateSymbolContext(SymbolContext *sc) override; 204 205 lldb::ModuleSP CalculateSymbolContextModule() override; 206 207 void 208 GetDescription(llvm::raw_ostream &s, 209 lldb::DescriptionLevel level = lldb::eDescriptionLevelFull); 210 211 /// Get the module path and object name. 212 /// 213 /// Modules can refer to object files. In this case the specification is 214 /// simple and would return the path to the file: 215 /// 216 /// "/usr/lib/foo.dylib" 217 /// 218 /// Modules can be .o files inside of a BSD archive (.a file). In this case, 219 /// the object specification will look like: 220 /// 221 /// "/usr/lib/foo.a(bar.o)" 222 /// 223 /// There are many places where logging wants to log this fully qualified 224 /// specification, so we centralize this functionality here. 225 /// 226 /// \return 227 /// The object path + object name if there is one. 228 std::string GetSpecificationDescription() const; 229 230 /// Dump a description of this object to a Stream. 231 /// 232 /// Dump a description of the contents of this object to the supplied stream 233 /// \a s. The dumped content will be only what has been loaded or parsed up 234 /// to this point at which this function is called, so this is a good way to 235 /// see what has been parsed in a module. 236 /// 237 /// \param[in] s 238 /// The stream to which to dump the object description. 239 void Dump(Stream *s); 240 241 /// \copydoc SymbolContextScope::DumpSymbolContext(Stream*) 242 /// 243 /// \see SymbolContextScope 244 void DumpSymbolContext(Stream *s) override; 245 246 /// Find a symbol in the object file's symbol table. 247 /// 248 /// \param[in] name 249 /// The name of the symbol that we are looking for. 250 /// 251 /// \param[in] symbol_type 252 /// If set to eSymbolTypeAny, find a symbol of any type that 253 /// has a name that matches \a name. If set to any other valid 254 /// SymbolType enumeration value, then search only for 255 /// symbols that match \a symbol_type. 256 /// 257 /// \return 258 /// Returns a valid symbol pointer if a symbol was found, 259 /// nullptr otherwise. 260 const Symbol *FindFirstSymbolWithNameAndType( 261 ConstString name, lldb::SymbolType symbol_type = lldb::eSymbolTypeAny); 262 263 void FindSymbolsWithNameAndType(ConstString name, 264 lldb::SymbolType symbol_type, 265 SymbolContextList &sc_list); 266 267 void FindSymbolsMatchingRegExAndType( 268 const RegularExpression ®ex, lldb::SymbolType symbol_type, 269 SymbolContextList &sc_list, 270 Mangled::NamePreference mangling_preference = Mangled::ePreferDemangled); 271 272 /// Find a function symbols in the object file's symbol table. 273 /// 274 /// \param[in] name 275 /// The name of the symbol that we are looking for. 276 /// 277 /// \param[in] name_type_mask 278 /// A mask that has one or more bitwise OR'ed values from the 279 /// lldb::FunctionNameType enumeration type that indicate what 280 /// kind of names we are looking for. 281 /// 282 /// \param[out] sc_list 283 /// A list to append any matching symbol contexts to. 284 void FindFunctionSymbols(ConstString name, uint32_t name_type_mask, 285 SymbolContextList &sc_list); 286 287 /// Find compile units by partial or full path. 288 /// 289 /// Finds all compile units that match \a path in all of the modules and 290 /// returns the results in \a sc_list. 291 /// 292 /// \param[in] path 293 /// The name of the function we are looking for. 294 /// 295 /// \param[out] sc_list 296 /// A symbol context list that gets filled in with all of the 297 /// matches. 298 void FindCompileUnits(const FileSpec &path, SymbolContextList &sc_list); 299 300 /// Find functions by lookup info. 301 /// 302 /// If the function is an inlined function, it will have a block, 303 /// representing the inlined function, and the function will be the 304 /// containing function. If it is not inlined, then the block will be NULL. 305 /// 306 /// \param[in] lookup_info 307 /// The lookup info of the function we are looking for. 308 /// 309 /// \param[out] sc_list 310 /// A symbol context list that gets filled in with all of the 311 /// matches. 312 void FindFunctions(const LookupInfo &lookup_info, 313 const CompilerDeclContext &parent_decl_ctx, 314 const ModuleFunctionSearchOptions &options, 315 SymbolContextList &sc_list); 316 317 /// Find functions by name. 318 /// 319 /// If the function is an inlined function, it will have a block, 320 /// representing the inlined function, and the function will be the 321 /// containing function. If it is not inlined, then the block will be NULL. 322 /// 323 /// \param[in] name 324 /// The name of the function we are looking for. 325 /// 326 /// \param[in] name_type_mask 327 /// A bit mask of bits that indicate what kind of names should 328 /// be used when doing the lookup. Bits include fully qualified 329 /// names, base names, C++ methods, or ObjC selectors. 330 /// See FunctionNameType for more details. 331 /// 332 /// \param[out] sc_list 333 /// A symbol context list that gets filled in with all of the 334 /// matches. 335 void FindFunctions(ConstString name, 336 const CompilerDeclContext &parent_decl_ctx, 337 lldb::FunctionNameType name_type_mask, 338 const ModuleFunctionSearchOptions &options, 339 SymbolContextList &sc_list); 340 341 /// Find functions by compiler context. 342 void FindFunctions(llvm::ArrayRef<CompilerContext> compiler_ctx, 343 lldb::FunctionNameType name_type_mask, 344 const ModuleFunctionSearchOptions &options, 345 SymbolContextList &sc_list); 346 347 /// Find functions by name. 348 /// 349 /// If the function is an inlined function, it will have a block, 350 /// representing the inlined function, and the function will be the 351 /// containing function. If it is not inlined, then the block will be NULL. 352 /// 353 /// \param[in] regex 354 /// A regular expression to use when matching the name. 355 /// 356 /// \param[out] sc_list 357 /// A symbol context list that gets filled in with all of the 358 /// matches. 359 void FindFunctions(const RegularExpression ®ex, 360 const ModuleFunctionSearchOptions &options, 361 SymbolContextList &sc_list); 362 363 /// Find addresses by file/line 364 /// 365 /// \param[in] target_sp 366 /// The target the addresses are desired for. 367 /// 368 /// \param[in] file 369 /// Source file to locate. 370 /// 371 /// \param[in] line 372 /// Source line to locate. 373 /// 374 /// \param[in] function 375 /// Optional filter function. Addresses within this function will be 376 /// added to the 'local' list. All others will be added to the 'extern' 377 /// list. 378 /// 379 /// \param[out] output_local 380 /// All matching addresses within 'function' 381 /// 382 /// \param[out] output_extern 383 /// All matching addresses not within 'function' 384 void FindAddressesForLine(const lldb::TargetSP target_sp, 385 const FileSpec &file, uint32_t line, 386 Function *function, 387 std::vector<Address> &output_local, 388 std::vector<Address> &output_extern); 389 390 /// Find global and static variables by name. 391 /// 392 /// \param[in] name 393 /// The name of the global or static variable we are looking 394 /// for. 395 /// 396 /// \param[in] parent_decl_ctx 397 /// If valid, a decl context that results must exist within 398 /// 399 /// \param[in] max_matches 400 /// Allow the number of matches to be limited to \a 401 /// max_matches. Specify UINT32_MAX to get all possible matches. 402 /// 403 /// \param[in] variable_list 404 /// A list of variables that gets the matches appended to. 405 /// 406 void FindGlobalVariables(ConstString name, 407 const CompilerDeclContext &parent_decl_ctx, 408 size_t max_matches, VariableList &variable_list); 409 410 /// Find global and static variables by regular expression. 411 /// 412 /// \param[in] regex 413 /// A regular expression to use when matching the name. 414 /// 415 /// \param[in] max_matches 416 /// Allow the number of matches to be limited to \a 417 /// max_matches. Specify UINT32_MAX to get all possible matches. 418 /// 419 /// \param[in] variable_list 420 /// A list of variables that gets the matches appended to. 421 /// 422 void FindGlobalVariables(const RegularExpression ®ex, size_t max_matches, 423 VariableList &variable_list); 424 425 /// Find types using a type-matching object that contains all search 426 /// parameters. 427 /// 428 /// \see lldb_private::TypeQuery 429 /// 430 /// \param[in] query 431 /// A type matching object that contains all of the details of the type 432 /// search. 433 /// 434 /// \param[in] results 435 /// Any matching types will be populated into the \a results object using 436 /// TypeMap::InsertUnique(...). 437 void FindTypes(const TypeQuery &query, TypeResults &results); 438 439 /// Get const accessor for the module architecture. 440 /// 441 /// \return 442 /// A const reference to the architecture object. 443 const ArchSpec &GetArchitecture() const; 444 445 /// Get const accessor for the module file specification. 446 /// 447 /// This function returns the file for the module on the host system that is 448 /// running LLDB. This can differ from the path on the platform since we 449 /// might be doing remote debugging. 450 /// 451 /// \return 452 /// A const reference to the file specification object. GetFileSpec()453 const FileSpec &GetFileSpec() const { return m_file; } 454 455 /// Get accessor for the module platform file specification. 456 /// 457 /// Platform file refers to the path of the module as it is known on the 458 /// remote system on which it is being debugged. For local debugging this is 459 /// always the same as Module::GetFileSpec(). But remote debugging might 460 /// mention a file "/usr/lib/liba.dylib" which might be locally downloaded 461 /// and cached. In this case the platform file could be something like: 462 /// "/tmp/lldb/platform-cache/remote.host.computer/usr/lib/liba.dylib" The 463 /// file could also be cached in a local developer kit directory. 464 /// 465 /// \return 466 /// A const reference to the file specification object. GetPlatformFileSpec()467 const FileSpec &GetPlatformFileSpec() const { 468 if (m_platform_file) 469 return m_platform_file; 470 return m_file; 471 } 472 SetPlatformFileSpec(const FileSpec & file)473 void SetPlatformFileSpec(const FileSpec &file) { m_platform_file = file; } 474 GetRemoteInstallFileSpec()475 const FileSpec &GetRemoteInstallFileSpec() const { 476 return m_remote_install_file; 477 } 478 SetRemoteInstallFileSpec(const FileSpec & file)479 void SetRemoteInstallFileSpec(const FileSpec &file) { 480 m_remote_install_file = file; 481 } 482 GetSymbolFileFileSpec()483 const FileSpec &GetSymbolFileFileSpec() const { return m_symfile_spec; } 484 485 void PreloadSymbols(); 486 487 void SetSymbolFileFileSpec(const FileSpec &file); 488 GetModificationTime()489 const llvm::sys::TimePoint<> &GetModificationTime() const { 490 return m_mod_time; 491 } 492 GetObjectModificationTime()493 const llvm::sys::TimePoint<> &GetObjectModificationTime() const { 494 return m_object_mod_time; 495 } 496 497 /// This callback will be called by SymbolFile implementations when 498 /// parsing a compile unit that contains SDK information. 499 /// \param sysroot will be added to the path remapping dictionary. 500 void RegisterXcodeSDK(llvm::StringRef sdk, llvm::StringRef sysroot); 501 502 /// Tells whether this module is capable of being the main executable for a 503 /// process. 504 /// 505 /// \return 506 /// \b true if it is, \b false otherwise. 507 bool IsExecutable(); 508 509 /// Tells whether this module has been loaded in the target passed in. This 510 /// call doesn't distinguish between whether the module is loaded by the 511 /// dynamic loader, or by a "target module add" type call. 512 /// 513 /// \param[in] target 514 /// The target to check whether this is loaded in. 515 /// 516 /// \return 517 /// \b true if it is, \b false otherwise. 518 bool IsLoadedInTarget(Target *target); 519 520 bool LoadScriptingResourceInTarget(Target *target, Status &error, 521 Stream &feedback_stream); 522 523 /// Get the number of compile units for this module. 524 /// 525 /// \return 526 /// The number of compile units that the symbol vendor plug-in 527 /// finds. 528 size_t GetNumCompileUnits(); 529 530 lldb::CompUnitSP GetCompileUnitAtIndex(size_t idx); 531 532 ConstString GetObjectName() const; 533 GetObjectOffset()534 uint64_t GetObjectOffset() const { return m_object_offset; } 535 536 /// Get the object file representation for the current architecture. 537 /// 538 /// If the object file has not been located or parsed yet, this function 539 /// will find the best ObjectFile plug-in that can parse Module::m_file. 540 /// 541 /// \return 542 /// If Module::m_file does not exist, or no plug-in was found 543 /// that can parse the file, or the object file doesn't contain 544 /// the current architecture in Module::m_arch, nullptr will be 545 /// returned, else a valid object file interface will be 546 /// returned. The returned pointer is owned by this object and 547 /// remains valid as long as the object is around. 548 virtual ObjectFile *GetObjectFile(); 549 550 /// Get the unified section list for the module. This is the section list 551 /// created by the module's object file and any debug info and symbol files 552 /// created by the symbol vendor. 553 /// 554 /// If the symbol vendor has not been loaded yet, this function will return 555 /// the section list for the object file. 556 /// 557 /// \return 558 /// Unified module section list. 559 virtual SectionList *GetSectionList(); 560 561 /// Notify the module that the file addresses for the Sections have been 562 /// updated. 563 /// 564 /// If the Section file addresses for a module are updated, this method 565 /// should be called. Any parts of the module, object file, or symbol file 566 /// that has cached those file addresses must invalidate or update its 567 /// cache. 568 virtual void SectionFileAddressesChanged(); 569 570 /// Returns a reference to the UnwindTable for this Module 571 /// 572 /// The UnwindTable contains FuncUnwinders objects for any function in this 573 /// Module. If a FuncUnwinders object hasn't been created yet (i.e. the 574 /// function has yet to be unwound in a stack walk), it will be created when 575 /// requested. Specifically, we do not create FuncUnwinders objects for 576 /// functions until they are needed. 577 /// 578 /// \return 579 /// Returns the unwind table for this module. If this object has no 580 /// associated object file, an empty UnwindTable is returned. 581 UnwindTable &GetUnwindTable(); 582 583 llvm::VersionTuple GetVersion(); 584 585 /// Load an object file from memory. 586 /// 587 /// If available, the size of the object file in memory may be passed to 588 /// avoid additional round trips to process memory. If the size is not 589 /// provided, a default value is used. This value should be large enough to 590 /// enable the ObjectFile plugins to read the header of the object file 591 /// without going back to the process. 592 /// 593 /// \return 594 /// The object file loaded from memory or nullptr, if the operation 595 /// failed (see the `error` for more information in that case). 596 ObjectFile *GetMemoryObjectFile(const lldb::ProcessSP &process_sp, 597 lldb::addr_t header_addr, Status &error, 598 size_t size_to_read = 512); 599 600 /// Get the module's symbol file 601 /// 602 /// If the symbol file has already been loaded, this function returns it. All 603 /// arguments are ignored. If the symbol file has not been located yet, and 604 /// the can_create argument is false, the function returns nullptr. If 605 /// can_create is true, this function will find the best SymbolFile plug-in 606 /// that can use the current object file. feedback_strm, if not null, is used 607 /// to report the details of the search process. 608 virtual SymbolFile *GetSymbolFile(bool can_create = true, 609 Stream *feedback_strm = nullptr); 610 611 /// Get the module's symbol table 612 /// 613 /// If the symbol table has already been loaded, this function returns it. 614 /// Otherwise, it will only be loaded when can_create is true. 615 Symtab *GetSymtab(bool can_create = true); 616 617 /// Get a reference to the UUID value contained in this object. 618 /// 619 /// If the executable image file doesn't not have a UUID value built into 620 /// the file format, an MD5 checksum of the entire file, or slice of the 621 /// file for the current architecture should be used. 622 /// 623 /// \return 624 /// A const pointer to the internal copy of the UUID value in 625 /// this module if this module has a valid UUID value, NULL 626 /// otherwise. 627 const lldb_private::UUID &GetUUID(); 628 629 /// A debugging function that will cause everything in a module to 630 /// be parsed. 631 /// 632 /// All compile units will be parsed, along with all globals and static 633 /// variables and all functions for those compile units. All types, scopes, 634 /// local variables, static variables, global variables, and line tables 635 /// will be parsed. This can be used prior to dumping a module to see a 636 /// complete list of the resulting debug information that gets parsed, or as 637 /// a debug function to ensure that the module can consume all of the debug 638 /// data the symbol vendor provides. 639 void ParseAllDebugSymbols(); 640 641 bool ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr); 642 643 /// Resolve the symbol context for the given address. 644 /// 645 /// Tries to resolve the matching symbol context based on a lookup from the 646 /// current symbol vendor. If the lazy lookup fails, an attempt is made to 647 /// parse the eh_frame section to handle stripped symbols. If this fails, 648 /// an attempt is made to resolve the symbol to the previous address to 649 /// handle the case of a function with a tail call. 650 /// 651 /// Use properties of the modified SymbolContext to inspect any resolved 652 /// target, module, compilation unit, symbol, function, function block or 653 /// line entry. Use the return value to determine which of these properties 654 /// have been modified. 655 /// 656 /// \param[in] so_addr 657 /// A load address to resolve. 658 /// 659 /// \param[in] resolve_scope 660 /// The scope that should be resolved (see SymbolContext::Scope). 661 /// A combination of flags from the enumeration SymbolContextItem 662 /// requesting a resolution depth. Note that the flags that are 663 /// actually resolved may be a superset of the requested flags. 664 /// For instance, eSymbolContextSymbol requires resolution of 665 /// eSymbolContextModule, and eSymbolContextFunction requires 666 /// eSymbolContextSymbol. 667 /// 668 /// \param[out] sc 669 /// The SymbolContext that is modified based on symbol resolution. 670 /// 671 /// \param[in] resolve_tail_call_address 672 /// Determines if so_addr should resolve to a symbol in the case 673 /// of a function whose last instruction is a call. In this case, 674 /// the PC can be one past the address range of the function. 675 /// 676 /// \return 677 /// The scope that has been resolved (see SymbolContext::Scope). 678 /// 679 /// \see SymbolContext::Scope 680 uint32_t ResolveSymbolContextForAddress( 681 const Address &so_addr, lldb::SymbolContextItem resolve_scope, 682 SymbolContext &sc, bool resolve_tail_call_address = false); 683 684 /// Resolve items in the symbol context for a given file and line. 685 /// 686 /// Tries to resolve \a file_path and \a line to a list of matching symbol 687 /// contexts. 688 /// 689 /// The line table entries contains addresses that can be used to further 690 /// resolve the values in each match: the function, block, symbol. Care 691 /// should be taken to minimize the amount of information that is requested 692 /// to only what is needed -- typically the module, compile unit, line table 693 /// and line table entry are sufficient. 694 /// 695 /// \param[in] file_path 696 /// A path to a source file to match. If \a file_path does not 697 /// specify a directory, then this query will match all files 698 /// whose base filename matches. If \a file_path does specify 699 /// a directory, the fullpath to the file must match. 700 /// 701 /// \param[in] line 702 /// The source line to match, or zero if just the compile unit 703 /// should be resolved. 704 /// 705 /// \param[in] check_inlines 706 /// Check for inline file and line number matches. This option 707 /// should be used sparingly as it will cause all line tables 708 /// for every compile unit to be parsed and searched for 709 /// matching inline file entries. 710 /// 711 /// \param[in] resolve_scope 712 /// The scope that should be resolved (see 713 /// SymbolContext::Scope). 714 /// 715 /// \param[out] sc_list 716 /// A symbol context list that gets matching symbols contexts 717 /// appended to. 718 /// 719 /// \return 720 /// The number of matches that were added to \a sc_list. 721 /// 722 /// \see SymbolContext::Scope 723 uint32_t ResolveSymbolContextForFilePath( 724 const char *file_path, uint32_t line, bool check_inlines, 725 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list); 726 727 /// Resolve items in the symbol context for a given file and line. 728 /// 729 /// Tries to resolve \a file_spec and \a line to a list of matching symbol 730 /// contexts. 731 /// 732 /// The line table entries contains addresses that can be used to further 733 /// resolve the values in each match: the function, block, symbol. Care 734 /// should be taken to minimize the amount of information that is requested 735 /// to only what is needed -- typically the module, compile unit, line table 736 /// and line table entry are sufficient. 737 /// 738 /// \param[in] file_spec 739 /// A file spec to a source file to match. If \a file_path does 740 /// not specify a directory, then this query will match all 741 /// files whose base filename matches. If \a file_path does 742 /// specify a directory, the fullpath to the file must match. 743 /// 744 /// \param[in] line 745 /// The source line to match, or zero if just the compile unit 746 /// should be resolved. 747 /// 748 /// \param[in] check_inlines 749 /// Check for inline file and line number matches. This option 750 /// should be used sparingly as it will cause all line tables 751 /// for every compile unit to be parsed and searched for 752 /// matching inline file entries. 753 /// 754 /// \param[in] resolve_scope 755 /// The scope that should be resolved (see 756 /// SymbolContext::Scope). 757 /// 758 /// \param[out] sc_list 759 /// A symbol context list that gets filled in with all of the 760 /// matches. 761 /// 762 /// \return 763 /// A integer that contains SymbolContext::Scope bits set for 764 /// each item that was successfully resolved. 765 /// 766 /// \see SymbolContext::Scope 767 uint32_t ResolveSymbolContextsForFileSpec( 768 const FileSpec &file_spec, uint32_t line, bool check_inlines, 769 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list); 770 771 void SetFileSpecAndObjectName(const FileSpec &file, ConstString object_name); 772 773 bool GetIsDynamicLinkEditor(); 774 775 llvm::Expected<lldb::TypeSystemSP> 776 GetTypeSystemForLanguage(lldb::LanguageType language); 777 778 /// Call \p callback for each \p TypeSystem in this \p Module. 779 /// Return true from callback to keep iterating, false to stop iterating. 780 void ForEachTypeSystem(llvm::function_ref<bool(lldb::TypeSystemSP)> callback); 781 782 // Special error functions that can do printf style formatting that will 783 // prepend the message with something appropriate for this module (like the 784 // architecture, path and object name (if any)). This centralizes code so 785 // that everyone doesn't need to format their error and log messages on their 786 // own and keeps the output a bit more consistent. 787 template <typename... Args> LogMessage(Log * log,const char * format,Args &&...args)788 void LogMessage(Log *log, const char *format, Args &&...args) { 789 LogMessage(log, llvm::formatv(format, std::forward<Args>(args)...)); 790 } 791 792 template <typename... Args> LogMessageVerboseBacktrace(Log * log,const char * format,Args &&...args)793 void LogMessageVerboseBacktrace(Log *log, const char *format, 794 Args &&...args) { 795 LogMessageVerboseBacktrace( 796 log, llvm::formatv(format, std::forward<Args>(args)...)); 797 } 798 799 template <typename... Args> ReportWarning(const char * format,Args &&...args)800 void ReportWarning(const char *format, Args &&...args) { 801 ReportWarning(llvm::formatv(format, std::forward<Args>(args)...)); 802 } 803 804 template <typename... Args> ReportError(const char * format,Args &&...args)805 void ReportError(const char *format, Args &&...args) { 806 ReportError(llvm::formatv(format, std::forward<Args>(args)...)); 807 } 808 809 // Only report an error once when the module is first detected to be modified 810 // so we don't spam the console with many messages. 811 template <typename... Args> ReportErrorIfModifyDetected(const char * format,Args &&...args)812 void ReportErrorIfModifyDetected(const char *format, Args &&...args) { 813 ReportErrorIfModifyDetected( 814 llvm::formatv(format, std::forward<Args>(args)...)); 815 } 816 817 void ReportWarningOptimization(std::optional<lldb::user_id_t> debugger_id); 818 819 void 820 ReportWarningUnsupportedLanguage(lldb::LanguageType language, 821 std::optional<lldb::user_id_t> debugger_id); 822 823 // Return true if the file backing this module has changed since the module 824 // was originally created since we saved the initial file modification time 825 // when the module first gets created. 826 bool FileHasChanged() const; 827 828 // SymbolFile and ObjectFile member objects should lock the 829 // module mutex to avoid deadlocks. GetMutex()830 std::recursive_mutex &GetMutex() const { return m_mutex; } 831 GetSourceMappingList()832 PathMappingList &GetSourceMappingList() { return m_source_mappings; } 833 GetSourceMappingList()834 const PathMappingList &GetSourceMappingList() const { 835 return m_source_mappings; 836 } 837 838 /// Finds a source file given a file spec using the module source path 839 /// remappings (if any). 840 /// 841 /// Tries to resolve \a orig_spec by checking the module source path 842 /// remappings. It makes sure the file exists, so this call can be expensive 843 /// if the remappings are on a network file system, so use this function 844 /// sparingly (not in a tight debug info parsing loop). 845 /// 846 /// \param[in] orig_spec 847 /// The original source file path to try and remap. 848 /// 849 /// \param[out] new_spec 850 /// The newly remapped filespec that is guaranteed to exist. 851 /// 852 /// \return 853 /// /b true if \a orig_spec was successfully located and 854 /// \a new_spec is filled in with an existing file spec, 855 /// \b false otherwise. 856 bool FindSourceFile(const FileSpec &orig_spec, FileSpec &new_spec) const; 857 858 /// Remaps a source file given \a path into \a new_path. 859 /// 860 /// Remaps \a path if any source remappings match. This function does NOT 861 /// stat the file system so it can be used in tight loops where debug info 862 /// is being parsed. 863 /// 864 /// \param[in] path 865 /// The original source file path to try and remap. 866 /// 867 /// \return 868 /// The newly remapped filespec that is may or may not exist if 869 /// \a path was successfully located. 870 std::optional<std::string> RemapSourceFile(llvm::StringRef path) const; 871 bool RemapSourceFile(const char *, std::string &) const = delete; 872 873 /// Update the ArchSpec to a more specific variant. 874 bool MergeArchitecture(const ArchSpec &arch_spec); 875 876 /// Accessor for the symbol table parse time metric. 877 /// 878 /// The value is returned as a reference to allow it to be updated by the 879 /// ElapsedTime RAII object. GetSymtabParseTime()880 StatsDuration &GetSymtabParseTime() { return m_symtab_parse_time; } 881 882 /// Accessor for the symbol table index time metric. 883 /// 884 /// The value is returned as a reference to allow it to be updated by the 885 /// ElapsedTime RAII object. GetSymtabIndexTime()886 StatsDuration &GetSymtabIndexTime() { return m_symtab_index_time; } 887 GetSymbolLocatorStatistics()888 StatisticsMap &GetSymbolLocatorStatistics() { 889 return m_symbol_locator_duration_map; 890 } 891 892 void ResetStatistics(); 893 894 /// \class LookupInfo Module.h "lldb/Core/Module.h" 895 /// A class that encapsulates name lookup information. 896 /// 897 /// Users can type a wide variety of partial names when setting breakpoints 898 /// by name or when looking for functions by name. The SymbolFile object is 899 /// only required to implement name lookup for function basenames and for 900 /// fully mangled names. This means if the user types in a partial name, we 901 /// must reduce this to a name lookup that will work with all SymbolFile 902 /// objects. So we might reduce a name lookup to look for a basename, and then 903 /// prune out any results that don't match. 904 /// 905 /// The "m_name" member variable represents the name as it was typed by the 906 /// user. "m_lookup_name" will be the name we actually search for through 907 /// the symbol or objects files. Lanaguage is included in case we need to 908 /// filter results by language at a later date. The "m_name_type_mask" 909 /// member variable tells us what kinds of names we are looking for and can 910 /// help us prune out unwanted results. 911 /// 912 /// Function lookups are done in Module.cpp, ModuleList.cpp and in 913 /// BreakpointResolverName.cpp and they all now use this class to do lookups 914 /// correctly. 915 class LookupInfo { 916 public: 917 LookupInfo() = default; 918 919 LookupInfo(ConstString name, lldb::FunctionNameType name_type_mask, 920 lldb::LanguageType language); 921 GetName()922 ConstString GetName() const { return m_name; } 923 SetName(ConstString name)924 void SetName(ConstString name) { m_name = name; } 925 GetLookupName()926 ConstString GetLookupName() const { return m_lookup_name; } 927 SetLookupName(ConstString name)928 void SetLookupName(ConstString name) { m_lookup_name = name; } 929 GetNameTypeMask()930 lldb::FunctionNameType GetNameTypeMask() const { return m_name_type_mask; } 931 SetNameTypeMask(lldb::FunctionNameType mask)932 void SetNameTypeMask(lldb::FunctionNameType mask) { 933 m_name_type_mask = mask; 934 } 935 GetLanguageType()936 lldb::LanguageType GetLanguageType() const { return m_language; } 937 938 bool NameMatchesLookupInfo( 939 ConstString function_name, 940 lldb::LanguageType language_type = lldb::eLanguageTypeUnknown) const; 941 942 void Prune(SymbolContextList &sc_list, size_t start_idx) const; 943 944 protected: 945 /// What the user originally typed 946 ConstString m_name; 947 948 /// The actual name will lookup when calling in the object or symbol file 949 ConstString m_lookup_name; 950 951 /// Limit matches to only be for this language 952 lldb::LanguageType m_language = lldb::eLanguageTypeUnknown; 953 954 /// One or more bits from lldb::FunctionNameType that indicate what kind of 955 /// names we are looking for 956 lldb::FunctionNameType m_name_type_mask = lldb::eFunctionNameTypeNone; 957 958 /// If \b true, then demangled names that match will need to contain 959 /// "m_name" in order to be considered a match 960 bool m_match_name_after_lookup = false; 961 }; 962 963 /// Get a unique hash for this module. 964 /// 965 /// The hash should be enough to identify the file on disk and the 966 /// architecture of the file. If the module represents an object inside of a 967 /// file, then the hash should include the object name and object offset to 968 /// ensure a unique hash. Some examples: 969 /// - just a regular object file (mach-o, elf, coff, etc) should create a hash 970 /// - a universal mach-o file that contains to multiple architectures, 971 /// each architecture slice should have a unique hash even though they come 972 /// from the same file 973 /// - a .o file inside of a BSD archive. Each .o file will have an object name 974 /// and object offset that should produce a unique hash. The object offset 975 /// is needed as BSD archive files can contain multiple .o files that have 976 /// the same name. 977 uint32_t Hash(); 978 979 /// Get a unique cache key for the current module. 980 /// 981 /// The cache key must be unique for a file on disk and not change if the file 982 /// is updated. This allows cache data to use this key as a prefix and as 983 /// files are modified in disk, we will overwrite the cache files. If one file 984 /// can contain multiple files, like a universal mach-o file or like a BSD 985 /// archive, the cache key must contain enough information to differentiate 986 /// these different files. 987 std::string GetCacheKey(); 988 989 /// Get the global index file cache. 990 /// 991 /// LLDB can cache data for a module between runs. This cache directory can be 992 /// used to stored data that previously was manually created each time you debug. 993 /// Examples include debug information indexes, symbol tables, symbol table 994 /// indexes, and more. 995 /// 996 /// \returns 997 /// If caching is enabled in the lldb settings, return a pointer to the data 998 /// file cache. If caching is not enabled, return NULL. 999 static DataFileCache *GetIndexCache(); 1000 protected: 1001 // Member Variables 1002 mutable std::recursive_mutex m_mutex; ///< A mutex to keep this object happy 1003 /// in multi-threaded environments. 1004 1005 /// The modification time for this module when it was created. 1006 llvm::sys::TimePoint<> m_mod_time; 1007 1008 ArchSpec m_arch; ///< The architecture for this module. 1009 UUID m_uuid; ///< Each module is assumed to have a unique identifier to help 1010 /// match it up to debug symbols. 1011 FileSpec m_file; ///< The file representation on disk for this module (if 1012 /// there is one). 1013 FileSpec m_platform_file; ///< The path to the module on the platform on which 1014 /// it is being debugged 1015 FileSpec m_remote_install_file; ///< If set when debugging on remote 1016 /// platforms, this module will be installed 1017 /// at this location 1018 FileSpec m_symfile_spec; ///< If this path is valid, then this is the file 1019 /// that _will_ be used as the symbol file for this 1020 /// module 1021 ConstString m_object_name; ///< The name an object within this module that is 1022 /// selected, or empty of the module is represented 1023 /// by \a m_file. 1024 uint64_t m_object_offset = 0; 1025 llvm::sys::TimePoint<> m_object_mod_time; 1026 1027 /// DataBuffer containing the module image, if it was provided at 1028 /// construction time. Otherwise the data will be retrieved by mapping 1029 /// one of the FileSpec members above. 1030 lldb::DataBufferSP m_data_sp; 1031 1032 lldb::ObjectFileSP m_objfile_sp; ///< A shared pointer to the object file 1033 /// parser for this module as it may or may 1034 /// not be shared with the SymbolFile 1035 UnwindTable m_unwind_table; ///< Table of FuncUnwinders 1036 /// objects created for this 1037 /// Module's functions 1038 lldb::SymbolVendorUP 1039 m_symfile_up; ///< A pointer to the symbol vendor for this module. 1040 std::vector<lldb::SymbolVendorUP> 1041 m_old_symfiles; ///< If anyone calls Module::SetSymbolFileFileSpec() and 1042 /// changes the symbol file, 1043 ///< we need to keep all old symbol files around in case anyone has type 1044 /// references to them 1045 TypeSystemMap m_type_system_map; ///< A map of any type systems associated 1046 /// with this module 1047 /// Module specific source remappings for when you have debug info for a 1048 /// module that doesn't match where the sources currently are. 1049 PathMappingList m_source_mappings = 1050 ModuleList::GetGlobalModuleListProperties().GetSymlinkMappings(); 1051 1052 lldb::SectionListUP m_sections_up; ///< Unified section list for module that 1053 /// is used by the ObjectFile and 1054 /// ObjectFile instances for the debug info 1055 1056 std::atomic<bool> m_did_load_objfile{false}; 1057 std::atomic<bool> m_did_load_symfile{false}; 1058 std::atomic<bool> m_did_set_uuid{false}; 1059 mutable bool m_file_has_changed : 1, 1060 m_first_file_changed_log : 1; /// See if the module was modified after it 1061 /// was initially opened. 1062 /// We store a symbol table parse time duration here because we might have 1063 /// an object file and a symbol file which both have symbol tables. The parse 1064 /// time for the symbol tables can be aggregated here. 1065 StatsDuration m_symtab_parse_time; 1066 /// We store a symbol named index time duration here because we might have 1067 /// an object file and a symbol file which both have symbol tables. The parse 1068 /// time for the symbol tables can be aggregated here. 1069 StatsDuration m_symtab_index_time; 1070 1071 StatisticsMap m_symbol_locator_duration_map; 1072 1073 /// A set of hashes of all warnings and errors, to avoid reporting them 1074 /// multiple times to the same Debugger. 1075 llvm::DenseMap<llvm::stable_hash, std::unique_ptr<std::once_flag>> 1076 m_shown_diagnostics; 1077 std::recursive_mutex m_diagnostic_mutex; 1078 1079 void SymbolIndicesToSymbolContextList(Symtab *symtab, 1080 std::vector<uint32_t> &symbol_indexes, 1081 SymbolContextList &sc_list); 1082 1083 bool SetArchitecture(const ArchSpec &new_arch); 1084 1085 void SetUUID(const lldb_private::UUID &uuid); 1086 1087 SectionList *GetUnifiedSectionList(); 1088 1089 friend class ModuleList; 1090 friend class ObjectFile; 1091 friend class SymbolFile; 1092 1093 private: 1094 Module(); // Only used internally by CreateJITModule () 1095 1096 Module(const Module &) = delete; 1097 const Module &operator=(const Module &) = delete; 1098 1099 void LogMessage(Log *log, const llvm::formatv_object_base &payload); 1100 void LogMessageVerboseBacktrace(Log *log, 1101 const llvm::formatv_object_base &payload); 1102 void ReportWarning(const llvm::formatv_object_base &payload); 1103 void ReportError(const llvm::formatv_object_base &payload); 1104 void ReportErrorIfModifyDetected(const llvm::formatv_object_base &payload); 1105 std::once_flag *GetDiagnosticOnceFlag(llvm::StringRef msg); 1106 }; 1107 1108 } // namespace lldb_private 1109 1110 #endif // LLDB_CORE_MODULE_H 1111