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> 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. 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. 467 const FileSpec &GetPlatformFileSpec() const { 468 if (m_platform_file) 469 return m_platform_file; 470 return m_file; 471 } 472 473 void SetPlatformFileSpec(const FileSpec &file) { m_platform_file = file; } 474 475 const FileSpec &GetRemoteInstallFileSpec() const { 476 return m_remote_install_file; 477 } 478 479 void SetRemoteInstallFileSpec(const FileSpec &file) { 480 m_remote_install_file = file; 481 } 482 483 const FileSpec &GetSymbolFileFileSpec() const { return m_symfile_spec; } 484 485 void PreloadSymbols(); 486 487 void SetSymbolFileFileSpec(const FileSpec &file); 488 489 const llvm::sys::TimePoint<> &GetModificationTime() const { 490 return m_mod_time; 491 } 492 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 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 Symtab *GetSymtab(); 612 613 /// Get a reference to the UUID value contained in this object. 614 /// 615 /// If the executable image file doesn't not have a UUID value built into 616 /// the file format, an MD5 checksum of the entire file, or slice of the 617 /// file for the current architecture should be used. 618 /// 619 /// \return 620 /// A const pointer to the internal copy of the UUID value in 621 /// this module if this module has a valid UUID value, NULL 622 /// otherwise. 623 const lldb_private::UUID &GetUUID(); 624 625 /// A debugging function that will cause everything in a module to 626 /// be parsed. 627 /// 628 /// All compile units will be parsed, along with all globals and static 629 /// variables and all functions for those compile units. All types, scopes, 630 /// local variables, static variables, global variables, and line tables 631 /// will be parsed. This can be used prior to dumping a module to see a 632 /// complete list of the resulting debug information that gets parsed, or as 633 /// a debug function to ensure that the module can consume all of the debug 634 /// data the symbol vendor provides. 635 void ParseAllDebugSymbols(); 636 637 bool ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr); 638 639 /// Resolve the symbol context for the given address. 640 /// 641 /// Tries to resolve the matching symbol context based on a lookup from the 642 /// current symbol vendor. If the lazy lookup fails, an attempt is made to 643 /// parse the eh_frame section to handle stripped symbols. If this fails, 644 /// an attempt is made to resolve the symbol to the previous address to 645 /// handle the case of a function with a tail call. 646 /// 647 /// Use properties of the modified SymbolContext to inspect any resolved 648 /// target, module, compilation unit, symbol, function, function block or 649 /// line entry. Use the return value to determine which of these properties 650 /// have been modified. 651 /// 652 /// \param[in] so_addr 653 /// A load address to resolve. 654 /// 655 /// \param[in] resolve_scope 656 /// The scope that should be resolved (see SymbolContext::Scope). 657 /// A combination of flags from the enumeration SymbolContextItem 658 /// requesting a resolution depth. Note that the flags that are 659 /// actually resolved may be a superset of the requested flags. 660 /// For instance, eSymbolContextSymbol requires resolution of 661 /// eSymbolContextModule, and eSymbolContextFunction requires 662 /// eSymbolContextSymbol. 663 /// 664 /// \param[out] sc 665 /// The SymbolContext that is modified based on symbol resolution. 666 /// 667 /// \param[in] resolve_tail_call_address 668 /// Determines if so_addr should resolve to a symbol in the case 669 /// of a function whose last instruction is a call. In this case, 670 /// the PC can be one past the address range of the function. 671 /// 672 /// \return 673 /// The scope that has been resolved (see SymbolContext::Scope). 674 /// 675 /// \see SymbolContext::Scope 676 uint32_t ResolveSymbolContextForAddress( 677 const Address &so_addr, lldb::SymbolContextItem resolve_scope, 678 SymbolContext &sc, bool resolve_tail_call_address = false); 679 680 /// Resolve items in the symbol context for a given file and line. 681 /// 682 /// Tries to resolve \a file_path and \a line to a list of matching symbol 683 /// contexts. 684 /// 685 /// The line table entries contains addresses that can be used to further 686 /// resolve the values in each match: the function, block, symbol. Care 687 /// should be taken to minimize the amount of information that is requested 688 /// to only what is needed -- typically the module, compile unit, line table 689 /// and line table entry are sufficient. 690 /// 691 /// \param[in] file_path 692 /// A path to a source file to match. If \a file_path does not 693 /// specify a directory, then this query will match all files 694 /// whose base filename matches. If \a file_path does specify 695 /// a directory, the fullpath to the file must match. 696 /// 697 /// \param[in] line 698 /// The source line to match, or zero if just the compile unit 699 /// should be resolved. 700 /// 701 /// \param[in] check_inlines 702 /// Check for inline file and line number matches. This option 703 /// should be used sparingly as it will cause all line tables 704 /// for every compile unit to be parsed and searched for 705 /// matching inline file entries. 706 /// 707 /// \param[in] resolve_scope 708 /// The scope that should be resolved (see 709 /// SymbolContext::Scope). 710 /// 711 /// \param[out] sc_list 712 /// A symbol context list that gets matching symbols contexts 713 /// appended to. 714 /// 715 /// \return 716 /// The number of matches that were added to \a sc_list. 717 /// 718 /// \see SymbolContext::Scope 719 uint32_t ResolveSymbolContextForFilePath( 720 const char *file_path, uint32_t line, bool check_inlines, 721 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list); 722 723 /// Resolve items in the symbol context for a given file and line. 724 /// 725 /// Tries to resolve \a file_spec and \a line to a list of matching symbol 726 /// contexts. 727 /// 728 /// The line table entries contains addresses that can be used to further 729 /// resolve the values in each match: the function, block, symbol. Care 730 /// should be taken to minimize the amount of information that is requested 731 /// to only what is needed -- typically the module, compile unit, line table 732 /// and line table entry are sufficient. 733 /// 734 /// \param[in] file_spec 735 /// A file spec to a source file to match. If \a file_path does 736 /// not specify a directory, then this query will match all 737 /// files whose base filename matches. If \a file_path does 738 /// specify a directory, the fullpath to the file must match. 739 /// 740 /// \param[in] line 741 /// The source line to match, or zero if just the compile unit 742 /// should be resolved. 743 /// 744 /// \param[in] check_inlines 745 /// Check for inline file and line number matches. This option 746 /// should be used sparingly as it will cause all line tables 747 /// for every compile unit to be parsed and searched for 748 /// matching inline file entries. 749 /// 750 /// \param[in] resolve_scope 751 /// The scope that should be resolved (see 752 /// SymbolContext::Scope). 753 /// 754 /// \param[out] sc_list 755 /// A symbol context list that gets filled in with all of the 756 /// matches. 757 /// 758 /// \return 759 /// A integer that contains SymbolContext::Scope bits set for 760 /// each item that was successfully resolved. 761 /// 762 /// \see SymbolContext::Scope 763 uint32_t ResolveSymbolContextsForFileSpec( 764 const FileSpec &file_spec, uint32_t line, bool check_inlines, 765 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list); 766 767 void SetFileSpecAndObjectName(const FileSpec &file, ConstString object_name); 768 769 bool GetIsDynamicLinkEditor(); 770 771 llvm::Expected<lldb::TypeSystemSP> 772 GetTypeSystemForLanguage(lldb::LanguageType language); 773 774 /// Call \p callback for each \p TypeSystem in this \p Module. 775 /// Return true from callback to keep iterating, false to stop iterating. 776 void ForEachTypeSystem(llvm::function_ref<bool(lldb::TypeSystemSP)> callback); 777 778 // Special error functions that can do printf style formatting that will 779 // prepend the message with something appropriate for this module (like the 780 // architecture, path and object name (if any)). This centralizes code so 781 // that everyone doesn't need to format their error and log messages on their 782 // own and keeps the output a bit more consistent. 783 template <typename... Args> 784 void LogMessage(Log *log, const char *format, Args &&...args) { 785 LogMessage(log, llvm::formatv(format, std::forward<Args>(args)...)); 786 } 787 788 template <typename... Args> 789 void LogMessageVerboseBacktrace(Log *log, const char *format, 790 Args &&...args) { 791 LogMessageVerboseBacktrace( 792 log, llvm::formatv(format, std::forward<Args>(args)...)); 793 } 794 795 template <typename... Args> 796 void ReportWarning(const char *format, Args &&...args) { 797 ReportWarning(llvm::formatv(format, std::forward<Args>(args)...)); 798 } 799 800 template <typename... Args> 801 void ReportError(const char *format, Args &&...args) { 802 ReportError(llvm::formatv(format, std::forward<Args>(args)...)); 803 } 804 805 // Only report an error once when the module is first detected to be modified 806 // so we don't spam the console with many messages. 807 template <typename... Args> 808 void ReportErrorIfModifyDetected(const char *format, Args &&...args) { 809 ReportErrorIfModifyDetected( 810 llvm::formatv(format, std::forward<Args>(args)...)); 811 } 812 813 void ReportWarningOptimization(std::optional<lldb::user_id_t> debugger_id); 814 815 void 816 ReportWarningUnsupportedLanguage(lldb::LanguageType language, 817 std::optional<lldb::user_id_t> debugger_id); 818 819 // Return true if the file backing this module has changed since the module 820 // was originally created since we saved the initial file modification time 821 // when the module first gets created. 822 bool FileHasChanged() const; 823 824 // SymbolFile and ObjectFile member objects should lock the 825 // module mutex to avoid deadlocks. 826 std::recursive_mutex &GetMutex() const { return m_mutex; } 827 828 PathMappingList &GetSourceMappingList() { return m_source_mappings; } 829 830 const PathMappingList &GetSourceMappingList() const { 831 return m_source_mappings; 832 } 833 834 /// Finds a source file given a file spec using the module source path 835 /// remappings (if any). 836 /// 837 /// Tries to resolve \a orig_spec by checking the module source path 838 /// remappings. It makes sure the file exists, so this call can be expensive 839 /// if the remappings are on a network file system, so use this function 840 /// sparingly (not in a tight debug info parsing loop). 841 /// 842 /// \param[in] orig_spec 843 /// The original source file path to try and remap. 844 /// 845 /// \param[out] new_spec 846 /// The newly remapped filespec that is guaranteed to exist. 847 /// 848 /// \return 849 /// /b true if \a orig_spec was successfully located and 850 /// \a new_spec is filled in with an existing file spec, 851 /// \b false otherwise. 852 bool FindSourceFile(const FileSpec &orig_spec, FileSpec &new_spec) const; 853 854 /// Remaps a source file given \a path into \a new_path. 855 /// 856 /// Remaps \a path if any source remappings match. This function does NOT 857 /// stat the file system so it can be used in tight loops where debug info 858 /// is being parsed. 859 /// 860 /// \param[in] path 861 /// The original source file path to try and remap. 862 /// 863 /// \return 864 /// The newly remapped filespec that is may or may not exist if 865 /// \a path was successfully located. 866 std::optional<std::string> RemapSourceFile(llvm::StringRef path) const; 867 bool RemapSourceFile(const char *, std::string &) const = delete; 868 869 /// Update the ArchSpec to a more specific variant. 870 bool MergeArchitecture(const ArchSpec &arch_spec); 871 872 /// Accessor for the symbol table parse time metric. 873 /// 874 /// The value is returned as a reference to allow it to be updated by the 875 /// ElapsedTime RAII object. 876 StatsDuration &GetSymtabParseTime() { return m_symtab_parse_time; } 877 878 /// Accessor for the symbol table index time metric. 879 /// 880 /// The value is returned as a reference to allow it to be updated by the 881 /// ElapsedTime RAII object. 882 StatsDuration &GetSymtabIndexTime() { return m_symtab_index_time; } 883 884 void ResetStatistics(); 885 886 /// \class LookupInfo Module.h "lldb/Core/Module.h" 887 /// A class that encapsulates name lookup information. 888 /// 889 /// Users can type a wide variety of partial names when setting breakpoints 890 /// by name or when looking for functions by name. The SymbolFile object is 891 /// only required to implement name lookup for function basenames and for 892 /// fully mangled names. This means if the user types in a partial name, we 893 /// must reduce this to a name lookup that will work with all SymbolFile 894 /// objects. So we might reduce a name lookup to look for a basename, and then 895 /// prune out any results that don't match. 896 /// 897 /// The "m_name" member variable represents the name as it was typed by the 898 /// user. "m_lookup_name" will be the name we actually search for through 899 /// the symbol or objects files. Lanaguage is included in case we need to 900 /// filter results by language at a later date. The "m_name_type_mask" 901 /// member variable tells us what kinds of names we are looking for and can 902 /// help us prune out unwanted results. 903 /// 904 /// Function lookups are done in Module.cpp, ModuleList.cpp and in 905 /// BreakpointResolverName.cpp and they all now use this class to do lookups 906 /// correctly. 907 class LookupInfo { 908 public: 909 LookupInfo() = default; 910 911 LookupInfo(ConstString name, lldb::FunctionNameType name_type_mask, 912 lldb::LanguageType language); 913 914 ConstString GetName() const { return m_name; } 915 916 void SetName(ConstString name) { m_name = name; } 917 918 ConstString GetLookupName() const { return m_lookup_name; } 919 920 void SetLookupName(ConstString name) { m_lookup_name = name; } 921 922 lldb::FunctionNameType GetNameTypeMask() const { return m_name_type_mask; } 923 924 void SetNameTypeMask(lldb::FunctionNameType mask) { 925 m_name_type_mask = mask; 926 } 927 928 lldb::LanguageType GetLanguageType() const { return m_language; } 929 930 bool NameMatchesLookupInfo( 931 ConstString function_name, 932 lldb::LanguageType language_type = lldb::eLanguageTypeUnknown) const; 933 934 void Prune(SymbolContextList &sc_list, size_t start_idx) const; 935 936 protected: 937 /// What the user originally typed 938 ConstString m_name; 939 940 /// The actual name will lookup when calling in the object or symbol file 941 ConstString m_lookup_name; 942 943 /// Limit matches to only be for this language 944 lldb::LanguageType m_language = lldb::eLanguageTypeUnknown; 945 946 /// One or more bits from lldb::FunctionNameType that indicate what kind of 947 /// names we are looking for 948 lldb::FunctionNameType m_name_type_mask = lldb::eFunctionNameTypeNone; 949 950 ///< If \b true, then demangled names that match will need to contain 951 ///< "m_name" in order to be considered a match 952 bool m_match_name_after_lookup = false; 953 }; 954 955 /// Get a unique hash for this module. 956 /// 957 /// The hash should be enough to identify the file on disk and the 958 /// architecture of the file. If the module represents an object inside of a 959 /// file, then the hash should include the object name and object offset to 960 /// ensure a unique hash. Some examples: 961 /// - just a regular object file (mach-o, elf, coff, etc) should create a hash 962 /// - a universal mach-o file that contains to multiple architectures, 963 /// each architecture slice should have a unique hash even though they come 964 /// from the same file 965 /// - a .o file inside of a BSD archive. Each .o file will have an object name 966 /// and object offset that should produce a unique hash. The object offset 967 /// is needed as BSD archive files can contain multiple .o files that have 968 /// the same name. 969 uint32_t Hash(); 970 971 /// Get a unique cache key for the current module. 972 /// 973 /// The cache key must be unique for a file on disk and not change if the file 974 /// is updated. This allows cache data to use this key as a prefix and as 975 /// files are modified in disk, we will overwrite the cache files. If one file 976 /// can contain multiple files, like a universal mach-o file or like a BSD 977 /// archive, the cache key must contain enough information to differentiate 978 /// these different files. 979 std::string GetCacheKey(); 980 981 /// Get the global index file cache. 982 /// 983 /// LLDB can cache data for a module between runs. This cache directory can be 984 /// used to stored data that previously was manually created each time you debug. 985 /// Examples include debug information indexes, symbol tables, symbol table 986 /// indexes, and more. 987 /// 988 /// \returns 989 /// If caching is enabled in the lldb settings, return a pointer to the data 990 /// file cache. If caching is not enabled, return NULL. 991 static DataFileCache *GetIndexCache(); 992 protected: 993 // Member Variables 994 mutable std::recursive_mutex m_mutex; ///< A mutex to keep this object happy 995 /// in multi-threaded environments. 996 997 /// The modification time for this module when it was created. 998 llvm::sys::TimePoint<> m_mod_time; 999 1000 ArchSpec m_arch; ///< The architecture for this module. 1001 UUID m_uuid; ///< Each module is assumed to have a unique identifier to help 1002 /// match it up to debug symbols. 1003 FileSpec m_file; ///< The file representation on disk for this module (if 1004 /// there is one). 1005 FileSpec m_platform_file; ///< The path to the module on the platform on which 1006 /// it is being debugged 1007 FileSpec m_remote_install_file; ///< If set when debugging on remote 1008 /// platforms, this module will be installed 1009 /// at this location 1010 FileSpec m_symfile_spec; ///< If this path is valid, then this is the file 1011 /// that _will_ be used as the symbol file for this 1012 /// module 1013 ConstString m_object_name; ///< The name an object within this module that is 1014 /// selected, or empty of the module is represented 1015 /// by \a m_file. 1016 uint64_t m_object_offset = 0; 1017 llvm::sys::TimePoint<> m_object_mod_time; 1018 1019 /// DataBuffer containing the module image, if it was provided at 1020 /// construction time. Otherwise the data will be retrieved by mapping 1021 /// one of the FileSpec members above. 1022 lldb::DataBufferSP m_data_sp; 1023 1024 lldb::ObjectFileSP m_objfile_sp; ///< A shared pointer to the object file 1025 /// parser for this module as it may or may 1026 /// not be shared with the SymbolFile 1027 UnwindTable m_unwind_table; ///< Table of FuncUnwinders 1028 /// objects created for this 1029 /// Module's functions 1030 lldb::SymbolVendorUP 1031 m_symfile_up; ///< A pointer to the symbol vendor for this module. 1032 std::vector<lldb::SymbolVendorUP> 1033 m_old_symfiles; ///< If anyone calls Module::SetSymbolFileFileSpec() and 1034 /// changes the symbol file, 1035 ///< we need to keep all old symbol files around in case anyone has type 1036 /// references to them 1037 TypeSystemMap m_type_system_map; ///< A map of any type systems associated 1038 /// with this module 1039 /// Module specific source remappings for when you have debug info for a 1040 /// module that doesn't match where the sources currently are. 1041 PathMappingList m_source_mappings = 1042 ModuleList::GetGlobalModuleListProperties().GetSymlinkMappings(); 1043 1044 lldb::SectionListUP m_sections_up; ///< Unified section list for module that 1045 /// is used by the ObjectFile and 1046 /// ObjectFile instances for the debug info 1047 1048 std::atomic<bool> m_did_load_objfile{false}; 1049 std::atomic<bool> m_did_load_symfile{false}; 1050 std::atomic<bool> m_did_set_uuid{false}; 1051 mutable bool m_file_has_changed : 1, 1052 m_first_file_changed_log : 1; /// See if the module was modified after it 1053 /// was initially opened. 1054 /// We store a symbol table parse time duration here because we might have 1055 /// an object file and a symbol file which both have symbol tables. The parse 1056 /// time for the symbol tables can be aggregated here. 1057 StatsDuration m_symtab_parse_time; 1058 /// We store a symbol named index time duration here because we might have 1059 /// an object file and a symbol file which both have symbol tables. The parse 1060 /// time for the symbol tables can be aggregated here. 1061 StatsDuration m_symtab_index_time; 1062 1063 /// A set of hashes of all warnings and errors, to avoid reporting them 1064 /// multiple times to the same Debugger. 1065 llvm::DenseMap<llvm::stable_hash, std::unique_ptr<std::once_flag>> 1066 m_shown_diagnostics; 1067 std::recursive_mutex m_diagnostic_mutex; 1068 1069 void SymbolIndicesToSymbolContextList(Symtab *symtab, 1070 std::vector<uint32_t> &symbol_indexes, 1071 SymbolContextList &sc_list); 1072 1073 bool SetArchitecture(const ArchSpec &new_arch); 1074 1075 void SetUUID(const lldb_private::UUID &uuid); 1076 1077 SectionList *GetUnifiedSectionList(); 1078 1079 friend class ModuleList; 1080 friend class ObjectFile; 1081 friend class SymbolFile; 1082 1083 private: 1084 Module(); // Only used internally by CreateJITModule () 1085 1086 Module(const Module &) = delete; 1087 const Module &operator=(const Module &) = delete; 1088 1089 void LogMessage(Log *log, const llvm::formatv_object_base &payload); 1090 void LogMessageVerboseBacktrace(Log *log, 1091 const llvm::formatv_object_base &payload); 1092 void ReportWarning(const llvm::formatv_object_base &payload); 1093 void ReportError(const llvm::formatv_object_base &payload); 1094 void ReportErrorIfModifyDetected(const llvm::formatv_object_base &payload); 1095 std::once_flag *GetDiagnosticOnceFlag(llvm::StringRef msg); 1096 }; 1097 1098 } // namespace lldb_private 1099 1100 #endif // LLDB_CORE_MODULE_H 1101