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/StringRef.h" 34 #include "llvm/Support/Chrono.h" 35 36 #include <atomic> 37 #include <cstddef> 38 #include <cstdint> 39 #include <memory> 40 #include <mutex> 41 #include <optional> 42 #include <string> 43 #include <vector> 44 45 namespace lldb_private { 46 class CompilerDeclContext; 47 class Function; 48 class Log; 49 class ObjectFile; 50 class RegularExpression; 51 class SectionList; 52 class Stream; 53 class Symbol; 54 class SymbolContext; 55 class SymbolContextList; 56 class SymbolFile; 57 class Symtab; 58 class Target; 59 class TypeList; 60 class TypeMap; 61 class VariableList; 62 63 /// Options used by Module::FindFunctions. This cannot be a nested class 64 /// because it must be forward-declared in ModuleList.h. 65 struct ModuleFunctionSearchOptions { 66 /// Include the symbol table. 67 bool include_symbols = false; 68 /// Include inlined functions. 69 bool include_inlines = false; 70 }; 71 72 /// \class Module Module.h "lldb/Core/Module.h" 73 /// A class that describes an executable image and its associated 74 /// object and symbol files. 75 /// 76 /// The module is designed to be able to select a single slice of an 77 /// executable image as it would appear on disk and during program execution. 78 /// 79 /// Modules control when and if information is parsed according to which 80 /// accessors are called. For example the object file (ObjectFile) 81 /// representation will only be parsed if the object file is requested using 82 /// the Module::GetObjectFile() is called. The debug symbols will only be 83 /// parsed if the symbol file (SymbolFile) is requested using the 84 /// Module::GetSymbolFile() method. 85 /// 86 /// The module will parse more detailed information as more queries are made. 87 class Module : public std::enable_shared_from_this<Module>, 88 public SymbolContextScope { 89 public: 90 class LookupInfo; 91 // Static functions that can track the lifetime of module objects. This is 92 // handy because we might have Module objects that are in shared pointers 93 // that aren't in the global module list (from ModuleList). If this is the 94 // case we need to know about it. The modules in the global list maintained 95 // by these functions can be viewed using the "target modules list" command 96 // using the "--global" (-g for short). 97 static size_t GetNumberAllocatedModules(); 98 99 static Module *GetAllocatedModuleAtIndex(size_t idx); 100 101 static std::recursive_mutex &GetAllocationModuleCollectionMutex(); 102 103 /// Construct with file specification and architecture. 104 /// 105 /// Clients that wish to share modules with other targets should use 106 /// ModuleList::GetSharedModule(). 107 /// 108 /// \param[in] file_spec 109 /// The file specification for the on disk representation of 110 /// this executable image. 111 /// 112 /// \param[in] arch 113 /// The architecture to set as the current architecture in 114 /// this module. 115 /// 116 /// \param[in] object_name 117 /// The name of an object in a module used to extract a module 118 /// within a module (.a files and modules that contain multiple 119 /// architectures). 120 /// 121 /// \param[in] object_offset 122 /// The offset within an existing module used to extract a 123 /// module within a module (.a files and modules that contain 124 /// multiple architectures). 125 Module( 126 const FileSpec &file_spec, const ArchSpec &arch, 127 const ConstString *object_name = nullptr, 128 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 name. 342 /// 343 /// If the function is an inlined function, it will have a block, 344 /// representing the inlined function, and the function will be the 345 /// containing function. If it is not inlined, then the block will be NULL. 346 /// 347 /// \param[in] regex 348 /// A regular expression to use when matching the name. 349 /// 350 /// \param[out] sc_list 351 /// A symbol context list that gets filled in with all of the 352 /// matches. 353 void FindFunctions(const RegularExpression ®ex, 354 const ModuleFunctionSearchOptions &options, 355 SymbolContextList &sc_list); 356 357 /// Find addresses by file/line 358 /// 359 /// \param[in] target_sp 360 /// The target the addresses are desired for. 361 /// 362 /// \param[in] file 363 /// Source file to locate. 364 /// 365 /// \param[in] line 366 /// Source line to locate. 367 /// 368 /// \param[in] function 369 /// Optional filter function. Addresses within this function will be 370 /// added to the 'local' list. All others will be added to the 'extern' 371 /// list. 372 /// 373 /// \param[out] output_local 374 /// All matching addresses within 'function' 375 /// 376 /// \param[out] output_extern 377 /// All matching addresses not within 'function' 378 void FindAddressesForLine(const lldb::TargetSP target_sp, 379 const FileSpec &file, uint32_t line, 380 Function *function, 381 std::vector<Address> &output_local, 382 std::vector<Address> &output_extern); 383 384 /// Find global and static variables by name. 385 /// 386 /// \param[in] name 387 /// The name of the global or static variable we are looking 388 /// for. 389 /// 390 /// \param[in] parent_decl_ctx 391 /// If valid, a decl context that results must exist within 392 /// 393 /// \param[in] max_matches 394 /// Allow the number of matches to be limited to \a 395 /// max_matches. Specify UINT32_MAX to get all possible matches. 396 /// 397 /// \param[in] variable_list 398 /// A list of variables that gets the matches appended to. 399 /// 400 void FindGlobalVariables(ConstString name, 401 const CompilerDeclContext &parent_decl_ctx, 402 size_t max_matches, VariableList &variable_list); 403 404 /// Find global and static variables by regular expression. 405 /// 406 /// \param[in] regex 407 /// A regular expression to use when matching the name. 408 /// 409 /// \param[in] max_matches 410 /// Allow the number of matches to be limited to \a 411 /// max_matches. Specify UINT32_MAX to get all possible matches. 412 /// 413 /// \param[in] variable_list 414 /// A list of variables that gets the matches appended to. 415 /// 416 void FindGlobalVariables(const RegularExpression ®ex, size_t max_matches, 417 VariableList &variable_list); 418 419 /// Find types by name. 420 /// 421 /// Type lookups in modules go through the SymbolFile. The SymbolFile needs to 422 /// be able to lookup types by basename and not the fully qualified typename. 423 /// This allows the type accelerator tables to stay small, even with heavily 424 /// templatized C++. The type search will then narrow down the search 425 /// results. If "exact_match" is true, then the type search will only match 426 /// exact type name matches. If "exact_match" is false, the type will match 427 /// as long as the base typename matches and as long as any immediate 428 /// containing namespaces/class scopes that are specified match. So to 429 /// search for a type "d" in "b::c", the name "b::c::d" can be specified and 430 /// it will match any class/namespace "b" which contains a class/namespace 431 /// "c" which contains type "d". We do this to allow users to not always 432 /// have to specify complete scoping on all expressions, but it also allows 433 /// for exact matching when required. 434 /// 435 /// \param[in] type_name 436 /// The name of the type we are looking for that is a fully 437 /// or partially qualified type name. 438 /// 439 /// \param[in] exact_match 440 /// If \b true, \a type_name is fully qualified and must match 441 /// exactly. If \b false, \a type_name is a partially qualified 442 /// name where the leading namespaces or classes can be 443 /// omitted to make finding types that a user may type 444 /// easier. 445 /// 446 /// \param[out] types 447 /// A type list gets populated with any matches. 448 /// 449 void 450 FindTypes(ConstString type_name, bool exact_match, size_t max_matches, 451 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 452 TypeList &types); 453 454 /// Find types by name. 455 /// 456 /// This behaves like the other FindTypes method but allows to 457 /// specify a DeclContext and a language for the type being searched 458 /// for. 459 /// 460 /// \param searched_symbol_files 461 /// Prevents one file from being visited multiple times. 462 void 463 FindTypes(llvm::ArrayRef<CompilerContext> pattern, LanguageSet languages, 464 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 465 TypeMap &types); 466 467 lldb::TypeSP FindFirstType(const SymbolContext &sc, ConstString type_name, 468 bool exact_match); 469 470 /// Find types by name that are in a namespace. This function is used by the 471 /// expression parser when searches need to happen in an exact namespace 472 /// scope. 473 /// 474 /// \param[in] type_name 475 /// The name of a type within a namespace that should not include 476 /// any qualifying namespaces (just a type basename). 477 /// 478 /// \param[out] type_list 479 /// A type list gets populated with any matches. 480 void FindTypesInNamespace(ConstString type_name, 481 const CompilerDeclContext &parent_decl_ctx, 482 size_t max_matches, TypeList &type_list); 483 484 /// Get const accessor for the module architecture. 485 /// 486 /// \return 487 /// A const reference to the architecture object. 488 const ArchSpec &GetArchitecture() const; 489 490 /// Get const accessor for the module file specification. 491 /// 492 /// This function returns the file for the module on the host system that is 493 /// running LLDB. This can differ from the path on the platform since we 494 /// might be doing remote debugging. 495 /// 496 /// \return 497 /// A const reference to the file specification object. GetFileSpec()498 const FileSpec &GetFileSpec() const { return m_file; } 499 500 /// Get accessor for the module platform file specification. 501 /// 502 /// Platform file refers to the path of the module as it is known on the 503 /// remote system on which it is being debugged. For local debugging this is 504 /// always the same as Module::GetFileSpec(). But remote debugging might 505 /// mention a file "/usr/lib/liba.dylib" which might be locally downloaded 506 /// and cached. In this case the platform file could be something like: 507 /// "/tmp/lldb/platform-cache/remote.host.computer/usr/lib/liba.dylib" The 508 /// file could also be cached in a local developer kit directory. 509 /// 510 /// \return 511 /// A const reference to the file specification object. GetPlatformFileSpec()512 const FileSpec &GetPlatformFileSpec() const { 513 if (m_platform_file) 514 return m_platform_file; 515 return m_file; 516 } 517 SetPlatformFileSpec(const FileSpec & file)518 void SetPlatformFileSpec(const FileSpec &file) { m_platform_file = file; } 519 GetRemoteInstallFileSpec()520 const FileSpec &GetRemoteInstallFileSpec() const { 521 return m_remote_install_file; 522 } 523 SetRemoteInstallFileSpec(const FileSpec & file)524 void SetRemoteInstallFileSpec(const FileSpec &file) { 525 m_remote_install_file = file; 526 } 527 GetSymbolFileFileSpec()528 const FileSpec &GetSymbolFileFileSpec() const { return m_symfile_spec; } 529 530 void PreloadSymbols(); 531 532 void SetSymbolFileFileSpec(const FileSpec &file); 533 GetModificationTime()534 const llvm::sys::TimePoint<> &GetModificationTime() const { 535 return m_mod_time; 536 } 537 GetObjectModificationTime()538 const llvm::sys::TimePoint<> &GetObjectModificationTime() const { 539 return m_object_mod_time; 540 } 541 542 /// This callback will be called by SymbolFile implementations when 543 /// parsing a compile unit that contains SDK information. 544 /// \param sysroot will be added to the path remapping dictionary. 545 void RegisterXcodeSDK(llvm::StringRef sdk, llvm::StringRef sysroot); 546 547 /// Tells whether this module is capable of being the main executable for a 548 /// process. 549 /// 550 /// \return 551 /// \b true if it is, \b false otherwise. 552 bool IsExecutable(); 553 554 /// Tells whether this module has been loaded in the target passed in. This 555 /// call doesn't distinguish between whether the module is loaded by the 556 /// dynamic loader, or by a "target module add" type call. 557 /// 558 /// \param[in] target 559 /// The target to check whether this is loaded in. 560 /// 561 /// \return 562 /// \b true if it is, \b false otherwise. 563 bool IsLoadedInTarget(Target *target); 564 565 bool LoadScriptingResourceInTarget(Target *target, Status &error, 566 Stream *feedback_stream = nullptr); 567 568 /// Get the number of compile units for this module. 569 /// 570 /// \return 571 /// The number of compile units that the symbol vendor plug-in 572 /// finds. 573 size_t GetNumCompileUnits(); 574 575 lldb::CompUnitSP GetCompileUnitAtIndex(size_t idx); 576 577 ConstString GetObjectName() const; 578 GetObjectOffset()579 uint64_t GetObjectOffset() const { return m_object_offset; } 580 581 /// Get the object file representation for the current architecture. 582 /// 583 /// If the object file has not been located or parsed yet, this function 584 /// will find the best ObjectFile plug-in that can parse Module::m_file. 585 /// 586 /// \return 587 /// If Module::m_file does not exist, or no plug-in was found 588 /// that can parse the file, or the object file doesn't contain 589 /// the current architecture in Module::m_arch, nullptr will be 590 /// returned, else a valid object file interface will be 591 /// returned. The returned pointer is owned by this object and 592 /// remains valid as long as the object is around. 593 virtual ObjectFile *GetObjectFile(); 594 595 /// Get the unified section list for the module. This is the section list 596 /// created by the module's object file and any debug info and symbol files 597 /// created by the symbol vendor. 598 /// 599 /// If the symbol vendor has not been loaded yet, this function will return 600 /// the section list for the object file. 601 /// 602 /// \return 603 /// Unified module section list. 604 virtual SectionList *GetSectionList(); 605 606 /// Notify the module that the file addresses for the Sections have been 607 /// updated. 608 /// 609 /// If the Section file addresses for a module are updated, this method 610 /// should be called. Any parts of the module, object file, or symbol file 611 /// that has cached those file addresses must invalidate or update its 612 /// cache. 613 virtual void SectionFileAddressesChanged(); 614 615 /// Returns a reference to the UnwindTable for this Module 616 /// 617 /// The UnwindTable contains FuncUnwinders objects for any function in this 618 /// Module. If a FuncUnwinders object hasn't been created yet (i.e. the 619 /// function has yet to be unwound in a stack walk), it will be created when 620 /// requested. Specifically, we do not create FuncUnwinders objects for 621 /// functions until they are needed. 622 /// 623 /// \return 624 /// Returns the unwind table for this module. If this object has no 625 /// associated object file, an empty UnwindTable is returned. 626 UnwindTable &GetUnwindTable(); 627 628 llvm::VersionTuple GetVersion(); 629 630 /// Load an object file from memory. 631 /// 632 /// If available, the size of the object file in memory may be passed to 633 /// avoid additional round trips to process memory. If the size is not 634 /// provided, a default value is used. This value should be large enough to 635 /// enable the ObjectFile plugins to read the header of the object file 636 /// without going back to the process. 637 /// 638 /// \return 639 /// The object file loaded from memory or nullptr, if the operation 640 /// failed (see the `error` for more information in that case). 641 ObjectFile *GetMemoryObjectFile(const lldb::ProcessSP &process_sp, 642 lldb::addr_t header_addr, Status &error, 643 size_t size_to_read = 512); 644 645 /// Get the module's symbol file 646 /// 647 /// If the symbol file has already been loaded, this function returns it. All 648 /// arguments are ignored. If the symbol file has not been located yet, and 649 /// the can_create argument is false, the function returns nullptr. If 650 /// can_create is true, this function will find the best SymbolFile plug-in 651 /// that can use the current object file. feedback_strm, if not null, is used 652 /// to report the details of the search process. 653 virtual SymbolFile *GetSymbolFile(bool can_create = true, 654 Stream *feedback_strm = nullptr); 655 656 Symtab *GetSymtab(); 657 658 /// Get a reference to the UUID value contained in this object. 659 /// 660 /// If the executable image file doesn't not have a UUID value built into 661 /// the file format, an MD5 checksum of the entire file, or slice of the 662 /// file for the current architecture should be used. 663 /// 664 /// \return 665 /// A const pointer to the internal copy of the UUID value in 666 /// this module if this module has a valid UUID value, NULL 667 /// otherwise. 668 const lldb_private::UUID &GetUUID(); 669 670 /// A debugging function that will cause everything in a module to 671 /// be parsed. 672 /// 673 /// All compile units will be parsed, along with all globals and static 674 /// variables and all functions for those compile units. All types, scopes, 675 /// local variables, static variables, global variables, and line tables 676 /// will be parsed. This can be used prior to dumping a module to see a 677 /// complete list of the resulting debug information that gets parsed, or as 678 /// a debug function to ensure that the module can consume all of the debug 679 /// data the symbol vendor provides. 680 void ParseAllDebugSymbols(); 681 682 bool ResolveFileAddress(lldb::addr_t vm_addr, Address &so_addr); 683 684 /// Resolve the symbol context for the given address. 685 /// 686 /// Tries to resolve the matching symbol context based on a lookup from the 687 /// current symbol vendor. If the lazy lookup fails, an attempt is made to 688 /// parse the eh_frame section to handle stripped symbols. If this fails, 689 /// an attempt is made to resolve the symbol to the previous address to 690 /// handle the case of a function with a tail call. 691 /// 692 /// Use properties of the modified SymbolContext to inspect any resolved 693 /// target, module, compilation unit, symbol, function, function block or 694 /// line entry. Use the return value to determine which of these properties 695 /// have been modified. 696 /// 697 /// \param[in] so_addr 698 /// A load address to resolve. 699 /// 700 /// \param[in] resolve_scope 701 /// The scope that should be resolved (see SymbolContext::Scope). 702 /// A combination of flags from the enumeration SymbolContextItem 703 /// requesting a resolution depth. Note that the flags that are 704 /// actually resolved may be a superset of the requested flags. 705 /// For instance, eSymbolContextSymbol requires resolution of 706 /// eSymbolContextModule, and eSymbolContextFunction requires 707 /// eSymbolContextSymbol. 708 /// 709 /// \param[out] sc 710 /// The SymbolContext that is modified based on symbol resolution. 711 /// 712 /// \param[in] resolve_tail_call_address 713 /// Determines if so_addr should resolve to a symbol in the case 714 /// of a function whose last instruction is a call. In this case, 715 /// the PC can be one past the address range of the function. 716 /// 717 /// \return 718 /// The scope that has been resolved (see SymbolContext::Scope). 719 /// 720 /// \see SymbolContext::Scope 721 uint32_t ResolveSymbolContextForAddress( 722 const Address &so_addr, lldb::SymbolContextItem resolve_scope, 723 SymbolContext &sc, bool resolve_tail_call_address = false); 724 725 /// Resolve items in the symbol context for a given file and line. 726 /// 727 /// Tries to resolve \a file_path and \a line to a list of matching symbol 728 /// contexts. 729 /// 730 /// The line table entries contains addresses that can be used to further 731 /// resolve the values in each match: the function, block, symbol. Care 732 /// should be taken to minimize the amount of information that is requested 733 /// to only what is needed -- typically the module, compile unit, line table 734 /// and line table entry are sufficient. 735 /// 736 /// \param[in] file_path 737 /// A path to a source file to match. If \a file_path does not 738 /// specify a directory, then this query will match all files 739 /// whose base filename matches. If \a file_path does specify 740 /// a directory, the fullpath to the file must match. 741 /// 742 /// \param[in] line 743 /// The source line to match, or zero if just the compile unit 744 /// should be resolved. 745 /// 746 /// \param[in] check_inlines 747 /// Check for inline file and line number matches. This option 748 /// should be used sparingly as it will cause all line tables 749 /// for every compile unit to be parsed and searched for 750 /// matching inline file entries. 751 /// 752 /// \param[in] resolve_scope 753 /// The scope that should be resolved (see 754 /// SymbolContext::Scope). 755 /// 756 /// \param[out] sc_list 757 /// A symbol context list that gets matching symbols contexts 758 /// appended to. 759 /// 760 /// \return 761 /// The number of matches that were added to \a sc_list. 762 /// 763 /// \see SymbolContext::Scope 764 uint32_t ResolveSymbolContextForFilePath( 765 const char *file_path, uint32_t line, bool check_inlines, 766 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list); 767 768 /// Resolve items in the symbol context for a given file and line. 769 /// 770 /// Tries to resolve \a file_spec and \a line to a list of matching symbol 771 /// contexts. 772 /// 773 /// The line table entries contains addresses that can be used to further 774 /// resolve the values in each match: the function, block, symbol. Care 775 /// should be taken to minimize the amount of information that is requested 776 /// to only what is needed -- typically the module, compile unit, line table 777 /// and line table entry are sufficient. 778 /// 779 /// \param[in] file_spec 780 /// A file spec to a source file to match. If \a file_path does 781 /// not specify a directory, then this query will match all 782 /// files whose base filename matches. If \a file_path does 783 /// specify a directory, the fullpath to the file must match. 784 /// 785 /// \param[in] line 786 /// The source line to match, or zero if just the compile unit 787 /// should be resolved. 788 /// 789 /// \param[in] check_inlines 790 /// Check for inline file and line number matches. This option 791 /// should be used sparingly as it will cause all line tables 792 /// for every compile unit to be parsed and searched for 793 /// matching inline file entries. 794 /// 795 /// \param[in] resolve_scope 796 /// The scope that should be resolved (see 797 /// SymbolContext::Scope). 798 /// 799 /// \param[out] sc_list 800 /// A symbol context list that gets filled in with all of the 801 /// matches. 802 /// 803 /// \return 804 /// A integer that contains SymbolContext::Scope bits set for 805 /// each item that was successfully resolved. 806 /// 807 /// \see SymbolContext::Scope 808 uint32_t ResolveSymbolContextsForFileSpec( 809 const FileSpec &file_spec, uint32_t line, bool check_inlines, 810 lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list); 811 812 void SetFileSpecAndObjectName(const FileSpec &file, ConstString object_name); 813 814 bool GetIsDynamicLinkEditor(); 815 816 llvm::Expected<lldb::TypeSystemSP> 817 GetTypeSystemForLanguage(lldb::LanguageType language); 818 819 /// Call \p callback for each \p TypeSystem in this \p Module. 820 /// Return true from callback to keep iterating, false to stop iterating. 821 void ForEachTypeSystem(llvm::function_ref<bool(lldb::TypeSystemSP)> callback); 822 823 // Special error functions that can do printf style formatting that will 824 // prepend the message with something appropriate for this module (like the 825 // architecture, path and object name (if any)). This centralizes code so 826 // that everyone doesn't need to format their error and log messages on their 827 // own and keeps the output a bit more consistent. 828 template <typename... Args> LogMessage(Log * log,const char * format,Args &&...args)829 void LogMessage(Log *log, const char *format, Args &&...args) { 830 LogMessage(log, llvm::formatv(format, std::forward<Args>(args)...)); 831 } 832 833 template <typename... Args> LogMessageVerboseBacktrace(Log * log,const char * format,Args &&...args)834 void LogMessageVerboseBacktrace(Log *log, const char *format, 835 Args &&...args) { 836 LogMessageVerboseBacktrace( 837 log, llvm::formatv(format, std::forward<Args>(args)...)); 838 } 839 840 template <typename... Args> ReportWarning(const char * format,Args &&...args)841 void ReportWarning(const char *format, Args &&...args) { 842 ReportWarning(llvm::formatv(format, std::forward<Args>(args)...)); 843 } 844 845 template <typename... Args> ReportError(const char * format,Args &&...args)846 void ReportError(const char *format, Args &&...args) { 847 ReportError(llvm::formatv(format, std::forward<Args>(args)...)); 848 } 849 850 // Only report an error once when the module is first detected to be modified 851 // so we don't spam the console with many messages. 852 template <typename... Args> ReportErrorIfModifyDetected(const char * format,Args &&...args)853 void ReportErrorIfModifyDetected(const char *format, Args &&...args) { 854 ReportErrorIfModifyDetected( 855 llvm::formatv(format, std::forward<Args>(args)...)); 856 } 857 858 void ReportWarningOptimization(std::optional<lldb::user_id_t> debugger_id); 859 860 void 861 ReportWarningUnsupportedLanguage(lldb::LanguageType language, 862 std::optional<lldb::user_id_t> debugger_id); 863 864 // Return true if the file backing this module has changed since the module 865 // was originally created since we saved the initial file modification time 866 // when the module first gets created. 867 bool FileHasChanged() const; 868 869 // SymbolFile and ObjectFile member objects should lock the 870 // module mutex to avoid deadlocks. GetMutex()871 std::recursive_mutex &GetMutex() const { return m_mutex; } 872 GetSourceMappingList()873 PathMappingList &GetSourceMappingList() { return m_source_mappings; } 874 GetSourceMappingList()875 const PathMappingList &GetSourceMappingList() const { 876 return m_source_mappings; 877 } 878 879 /// Finds a source file given a file spec using the module source path 880 /// remappings (if any). 881 /// 882 /// Tries to resolve \a orig_spec by checking the module source path 883 /// remappings. It makes sure the file exists, so this call can be expensive 884 /// if the remappings are on a network file system, so use this function 885 /// sparingly (not in a tight debug info parsing loop). 886 /// 887 /// \param[in] orig_spec 888 /// The original source file path to try and remap. 889 /// 890 /// \param[out] new_spec 891 /// The newly remapped filespec that is guaranteed to exist. 892 /// 893 /// \return 894 /// /b true if \a orig_spec was successfully located and 895 /// \a new_spec is filled in with an existing file spec, 896 /// \b false otherwise. 897 bool FindSourceFile(const FileSpec &orig_spec, FileSpec &new_spec) const; 898 899 /// Remaps a source file given \a path into \a new_path. 900 /// 901 /// Remaps \a path if any source remappings match. This function does NOT 902 /// stat the file system so it can be used in tight loops where debug info 903 /// is being parsed. 904 /// 905 /// \param[in] path 906 /// The original source file path to try and remap. 907 /// 908 /// \return 909 /// The newly remapped filespec that is may or may not exist if 910 /// \a path was successfully located. 911 std::optional<std::string> RemapSourceFile(llvm::StringRef path) const; 912 bool RemapSourceFile(const char *, std::string &) const = delete; 913 914 /// Update the ArchSpec to a more specific variant. 915 bool MergeArchitecture(const ArchSpec &arch_spec); 916 917 /// Accessor for the symbol table parse time metric. 918 /// 919 /// The value is returned as a reference to allow it to be updated by the 920 /// ElapsedTime RAII object. GetSymtabParseTime()921 StatsDuration &GetSymtabParseTime() { return m_symtab_parse_time; } 922 923 /// Accessor for the symbol table index time metric. 924 /// 925 /// The value is returned as a reference to allow it to be updated by the 926 /// ElapsedTime RAII object. GetSymtabIndexTime()927 StatsDuration &GetSymtabIndexTime() { return m_symtab_index_time; } 928 929 /// \class LookupInfo Module.h "lldb/Core/Module.h" 930 /// A class that encapsulates name lookup information. 931 /// 932 /// Users can type a wide variety of partial names when setting breakpoints 933 /// by name or when looking for functions by name. The SymbolFile object is 934 /// only required to implement name lookup for function basenames and for 935 /// fully mangled names. This means if the user types in a partial name, we 936 /// must reduce this to a name lookup that will work with all SymbolFile 937 /// objects. So we might reduce a name lookup to look for a basename, and then 938 /// prune out any results that don't match. 939 /// 940 /// The "m_name" member variable represents the name as it was typed by the 941 /// user. "m_lookup_name" will be the name we actually search for through 942 /// the symbol or objects files. Lanaguage is included in case we need to 943 /// filter results by language at a later date. The "m_name_type_mask" 944 /// member variable tells us what kinds of names we are looking for and can 945 /// help us prune out unwanted results. 946 /// 947 /// Function lookups are done in Module.cpp, ModuleList.cpp and in 948 /// BreakpointResolverName.cpp and they all now use this class to do lookups 949 /// correctly. 950 class LookupInfo { 951 public: 952 LookupInfo() = default; 953 954 LookupInfo(ConstString name, lldb::FunctionNameType name_type_mask, 955 lldb::LanguageType language); 956 GetName()957 ConstString GetName() const { return m_name; } 958 SetName(ConstString name)959 void SetName(ConstString name) { m_name = name; } 960 GetLookupName()961 ConstString GetLookupName() const { return m_lookup_name; } 962 SetLookupName(ConstString name)963 void SetLookupName(ConstString name) { m_lookup_name = name; } 964 GetNameTypeMask()965 lldb::FunctionNameType GetNameTypeMask() const { return m_name_type_mask; } 966 SetNameTypeMask(lldb::FunctionNameType mask)967 void SetNameTypeMask(lldb::FunctionNameType mask) { 968 m_name_type_mask = mask; 969 } 970 GetLanguageType()971 lldb::LanguageType GetLanguageType() const { return m_language; } 972 973 bool NameMatchesLookupInfo( 974 ConstString function_name, 975 lldb::LanguageType language_type = lldb::eLanguageTypeUnknown) const; 976 977 void Prune(SymbolContextList &sc_list, size_t start_idx) const; 978 979 protected: 980 /// What the user originally typed 981 ConstString m_name; 982 983 /// The actual name will lookup when calling in the object or symbol file 984 ConstString m_lookup_name; 985 986 /// Limit matches to only be for this language 987 lldb::LanguageType m_language = lldb::eLanguageTypeUnknown; 988 989 /// One or more bits from lldb::FunctionNameType that indicate what kind of 990 /// names we are looking for 991 lldb::FunctionNameType m_name_type_mask = lldb::eFunctionNameTypeNone; 992 993 ///< If \b true, then demangled names that match will need to contain 994 ///< "m_name" in order to be considered a match 995 bool m_match_name_after_lookup = false; 996 }; 997 998 /// Get a unique hash for this module. 999 /// 1000 /// The hash should be enough to identify the file on disk and the 1001 /// architecture of the file. If the module represents an object inside of a 1002 /// file, then the hash should include the object name and object offset to 1003 /// ensure a unique hash. Some examples: 1004 /// - just a regular object file (mach-o, elf, coff, etc) should create a hash 1005 /// - a universal mach-o file that contains to multiple architectures, 1006 /// each architecture slice should have a unique hash even though they come 1007 /// from the same file 1008 /// - a .o file inside of a BSD archive. Each .o file will have an object name 1009 /// and object offset that should produce a unique hash. The object offset 1010 /// is needed as BSD archive files can contain multiple .o files that have 1011 /// the same name. 1012 uint32_t Hash(); 1013 1014 /// Get a unique cache key for the current module. 1015 /// 1016 /// The cache key must be unique for a file on disk and not change if the file 1017 /// is updated. This allows cache data to use this key as a prefix and as 1018 /// files are modified in disk, we will overwrite the cache files. If one file 1019 /// can contain multiple files, like a universal mach-o file or like a BSD 1020 /// archive, the cache key must contain enough information to differentiate 1021 /// these different files. 1022 std::string GetCacheKey(); 1023 1024 /// Get the global index file cache. 1025 /// 1026 /// LLDB can cache data for a module between runs. This cache directory can be 1027 /// used to stored data that previously was manually created each time you debug. 1028 /// Examples include debug information indexes, symbol tables, symbol table 1029 /// indexes, and more. 1030 /// 1031 /// \returns 1032 /// If caching is enabled in the lldb settings, return a pointer to the data 1033 /// file cache. If caching is not enabled, return NULL. 1034 static DataFileCache *GetIndexCache(); 1035 protected: 1036 // Member Variables 1037 mutable std::recursive_mutex m_mutex; ///< A mutex to keep this object happy 1038 /// in multi-threaded environments. 1039 1040 /// The modification time for this module when it was created. 1041 llvm::sys::TimePoint<> m_mod_time; 1042 1043 ArchSpec m_arch; ///< The architecture for this module. 1044 UUID m_uuid; ///< Each module is assumed to have a unique identifier to help 1045 /// match it up to debug symbols. 1046 FileSpec m_file; ///< The file representation on disk for this module (if 1047 /// there is one). 1048 FileSpec m_platform_file; ///< The path to the module on the platform on which 1049 /// it is being debugged 1050 FileSpec m_remote_install_file; ///< If set when debugging on remote 1051 /// platforms, this module will be installed 1052 /// at this location 1053 FileSpec m_symfile_spec; ///< If this path is valid, then this is the file 1054 /// that _will_ be used as the symbol file for this 1055 /// module 1056 ConstString m_object_name; ///< The name an object within this module that is 1057 /// selected, or empty of the module is represented 1058 /// by \a m_file. 1059 uint64_t m_object_offset = 0; 1060 llvm::sys::TimePoint<> m_object_mod_time; 1061 1062 /// DataBuffer containing the module image, if it was provided at 1063 /// construction time. Otherwise the data will be retrieved by mapping 1064 /// one of the FileSpec members above. 1065 lldb::DataBufferSP m_data_sp; 1066 1067 lldb::ObjectFileSP m_objfile_sp; ///< A shared pointer to the object file 1068 /// parser for this module as it may or may 1069 /// not be shared with the SymbolFile 1070 std::optional<UnwindTable> m_unwind_table; ///< Table of FuncUnwinders 1071 /// objects created for this 1072 /// Module's functions 1073 lldb::SymbolVendorUP 1074 m_symfile_up; ///< A pointer to the symbol vendor for this module. 1075 std::vector<lldb::SymbolVendorUP> 1076 m_old_symfiles; ///< If anyone calls Module::SetSymbolFileFileSpec() and 1077 /// changes the symbol file, 1078 ///< we need to keep all old symbol files around in case anyone has type 1079 /// references to them 1080 TypeSystemMap m_type_system_map; ///< A map of any type systems associated 1081 /// with this module 1082 /// Module specific source remappings for when you have debug info for a 1083 /// module that doesn't match where the sources currently are. 1084 PathMappingList m_source_mappings = 1085 ModuleList::GetGlobalModuleListProperties().GetSymlinkMappings(); 1086 1087 lldb::SectionListUP m_sections_up; ///< Unified section list for module that 1088 /// is used by the ObjectFile and and 1089 /// ObjectFile instances for the debug info 1090 1091 std::atomic<bool> m_did_load_objfile{false}; 1092 std::atomic<bool> m_did_load_symfile{false}; 1093 std::atomic<bool> m_did_set_uuid{false}; 1094 mutable bool m_file_has_changed : 1, 1095 m_first_file_changed_log : 1; /// See if the module was modified after it 1096 /// was initially opened. 1097 /// We store a symbol table parse time duration here because we might have 1098 /// an object file and a symbol file which both have symbol tables. The parse 1099 /// time for the symbol tables can be aggregated here. 1100 StatsDuration m_symtab_parse_time; 1101 /// We store a symbol named index time duration here because we might have 1102 /// an object file and a symbol file which both have symbol tables. The parse 1103 /// time for the symbol tables can be aggregated here. 1104 StatsDuration m_symtab_index_time; 1105 1106 std::once_flag m_optimization_warning; 1107 std::once_flag m_language_warning; 1108 1109 /// Resolve a file or load virtual address. 1110 /// 1111 /// Tries to resolve \a vm_addr as a file address (if \a 1112 /// vm_addr_is_file_addr is true) or as a load address if \a 1113 /// vm_addr_is_file_addr is false) in the symbol vendor. \a resolve_scope 1114 /// indicates what clients wish to resolve and can be used to limit the 1115 /// scope of what is parsed. 1116 /// 1117 /// \param[in] vm_addr 1118 /// The load virtual address to resolve. 1119 /// 1120 /// \param[in] vm_addr_is_file_addr 1121 /// If \b true, \a vm_addr is a file address, else \a vm_addr 1122 /// if a load address. 1123 /// 1124 /// \param[in] resolve_scope 1125 /// The scope that should be resolved (see 1126 /// SymbolContext::Scope). 1127 /// 1128 /// \param[out] so_addr 1129 /// The section offset based address that got resolved if 1130 /// any bits are returned. 1131 /// 1132 /// \param[out] sc 1133 // The symbol context that has objects filled in. Each bit 1134 /// in the \a resolve_scope pertains to a member in the \a sc. 1135 /// 1136 /// \return 1137 /// A integer that contains SymbolContext::Scope bits set for 1138 /// each item that was successfully resolved. 1139 /// 1140 /// \see SymbolContext::Scope 1141 uint32_t ResolveSymbolContextForAddress(lldb::addr_t vm_addr, 1142 bool vm_addr_is_file_addr, 1143 lldb::SymbolContextItem resolve_scope, 1144 Address &so_addr, SymbolContext &sc); 1145 1146 void SymbolIndicesToSymbolContextList(Symtab *symtab, 1147 std::vector<uint32_t> &symbol_indexes, 1148 SymbolContextList &sc_list); 1149 1150 bool SetArchitecture(const ArchSpec &new_arch); 1151 1152 void SetUUID(const lldb_private::UUID &uuid); 1153 1154 SectionList *GetUnifiedSectionList(); 1155 1156 friend class ModuleList; 1157 friend class ObjectFile; 1158 friend class SymbolFile; 1159 1160 private: 1161 Module(); // Only used internally by CreateJITModule () 1162 1163 void FindTypes_Impl( 1164 ConstString name, const CompilerDeclContext &parent_decl_ctx, 1165 size_t max_matches, 1166 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, 1167 TypeMap &types); 1168 1169 Module(const Module &) = delete; 1170 const Module &operator=(const Module &) = delete; 1171 1172 void LogMessage(Log *log, const llvm::formatv_object_base &payload); 1173 void LogMessageVerboseBacktrace(Log *log, 1174 const llvm::formatv_object_base &payload); 1175 void ReportWarning(const llvm::formatv_object_base &payload); 1176 void ReportError(const llvm::formatv_object_base &payload); 1177 void ReportErrorIfModifyDetected(const llvm::formatv_object_base &payload); 1178 }; 1179 1180 } // namespace lldb_private 1181 1182 #endif // LLDB_CORE_MODULE_H 1183