1 //===-- Function.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_SYMBOL_FUNCTION_H 10 #define LLDB_SYMBOL_FUNCTION_H 11 12 #include "lldb/Core/AddressRange.h" 13 #include "lldb/Core/Declaration.h" 14 #include "lldb/Core/Mangled.h" 15 #include "lldb/Expression/DWARFExpressionList.h" 16 #include "lldb/Symbol/Block.h" 17 #include "lldb/Utility/UserID.h" 18 #include "lldb/lldb-forward.h" 19 #include "llvm/ADT/ArrayRef.h" 20 21 #include <mutex> 22 23 namespace lldb_private { 24 25 class ExecutionContext; 26 27 /// \class FunctionInfo Function.h "lldb/Symbol/Function.h" 28 /// A class that contains generic function information. 29 /// 30 /// This provides generic function information that gets reused between inline 31 /// functions and function types. 32 class FunctionInfo { 33 public: 34 /// Construct with the function method name and optional declaration 35 /// information. 36 /// 37 /// \param[in] name 38 /// A C string name for the method name for this function. This 39 /// value should not be the mangled named, but the simple method 40 /// name. 41 /// 42 /// \param[in] decl_ptr 43 /// Optional declaration information that describes where the 44 /// function was declared. This can be NULL. 45 FunctionInfo(const char *name, const Declaration *decl_ptr); 46 47 /// Construct with the function method name and optional declaration 48 /// information. 49 /// 50 /// \param[in] name 51 /// A name for the method name for this function. This value 52 /// should not be the mangled named, but the simple method name. 53 /// 54 /// \param[in] decl_ptr 55 /// Optional declaration information that describes where the 56 /// function was declared. This can be NULL. 57 FunctionInfo(ConstString name, const Declaration *decl_ptr); 58 59 /// Destructor. 60 /// 61 /// The destructor is virtual since classes inherit from this class. 62 virtual ~FunctionInfo(); 63 64 /// Compare two function information objects. 65 /// 66 /// First compares the method names, and if equal, then compares the 67 /// declaration information. 68 /// 69 /// \param[in] lhs 70 /// The Left Hand Side const FunctionInfo object reference. 71 /// 72 /// \param[in] rhs 73 /// The Right Hand Side const FunctionInfo object reference. 74 /// 75 /// \return 76 /// -1 if lhs < rhs 77 /// 0 if lhs == rhs 78 /// 1 if lhs > rhs 79 static int Compare(const FunctionInfo &lhs, const FunctionInfo &rhs); 80 81 /// Dump a description of this object to a Stream. 82 /// 83 /// Dump a description of the contents of this object to the supplied stream 84 /// \a s. 85 /// 86 /// \param[in] s 87 /// The stream to which to dump the object description. 88 void Dump(Stream *s, bool show_fullpaths) const; 89 90 /// Get accessor for the declaration information. 91 /// 92 /// \return 93 /// A reference to the declaration object. 94 Declaration &GetDeclaration(); 95 96 /// Get const accessor for the declaration information. 97 /// 98 /// \return 99 /// A const reference to the declaration object. 100 const Declaration &GetDeclaration() const; 101 102 /// Get accessor for the method name. 103 /// 104 /// \return 105 /// A const reference to the method name object. 106 ConstString GetName() const; 107 108 /// Get the memory cost of this object. 109 /// 110 /// \return 111 /// The number of bytes that this object occupies in memory. 112 /// The returned value does not include the bytes for any 113 /// shared string values. 114 virtual size_t MemorySize() const; 115 116 protected: 117 /// Function method name (not a mangled name). 118 ConstString m_name; 119 120 /// Information describing where this function information was defined. 121 Declaration m_declaration; 122 }; 123 124 /// \class InlineFunctionInfo Function.h "lldb/Symbol/Function.h" 125 /// A class that describes information for an inlined function. 126 class InlineFunctionInfo : public FunctionInfo { 127 public: 128 /// Construct with the function method name, mangled name, and optional 129 /// declaration information. 130 /// 131 /// \param[in] name 132 /// A C string name for the method name for this function. This 133 /// value should not be the mangled named, but the simple method 134 /// name. 135 /// 136 /// \param[in] mangled 137 /// A C string name for the mangled name for this function. This 138 /// value can be NULL if there is no mangled information. 139 /// 140 /// \param[in] decl_ptr 141 /// Optional declaration information that describes where the 142 /// function was declared. This can be NULL. 143 /// 144 /// \param[in] call_decl_ptr 145 /// Optional calling location declaration information that 146 /// describes from where this inlined function was called. 147 InlineFunctionInfo(const char *name, llvm::StringRef mangled, 148 const Declaration *decl_ptr, 149 const Declaration *call_decl_ptr); 150 151 /// Construct with the function method name, mangled name, and optional 152 /// declaration information. 153 /// 154 /// \param[in] name 155 /// A name for the method name for this function. This value 156 /// should not be the mangled named, but the simple method name. 157 /// 158 /// \param[in] mangled 159 /// A name for the mangled name for this function. This value 160 /// can be empty if there is no mangled information. 161 /// 162 /// \param[in] decl_ptr 163 /// Optional declaration information that describes where the 164 /// function was declared. This can be NULL. 165 /// 166 /// \param[in] call_decl_ptr 167 /// Optional calling location declaration information that 168 /// describes from where this inlined function was called. 169 InlineFunctionInfo(ConstString name, const Mangled &mangled, 170 const Declaration *decl_ptr, 171 const Declaration *call_decl_ptr); 172 173 /// Destructor. 174 ~InlineFunctionInfo() override; 175 176 /// Compare two inlined function information objects. 177 /// 178 /// First compares the FunctionInfo objects, and if equal, compares the 179 /// mangled names. 180 /// 181 /// \param[in] lhs 182 /// The Left Hand Side const InlineFunctionInfo object 183 /// reference. 184 /// 185 /// \param[in] rhs 186 /// The Right Hand Side const InlineFunctionInfo object 187 /// reference. 188 /// 189 /// \return 190 /// -1 if lhs < rhs 191 /// 0 if lhs == rhs 192 /// 1 if lhs > rhs 193 int Compare(const InlineFunctionInfo &lhs, const InlineFunctionInfo &rhs); 194 195 /// Dump a description of this object to a Stream. 196 /// 197 /// Dump a description of the contents of this object to the supplied stream 198 /// \a s. 199 /// 200 /// \param[in] s 201 /// The stream to which to dump the object description. 202 void Dump(Stream *s, bool show_fullpaths) const; 203 204 void DumpStopContext(Stream *s) const; 205 206 ConstString GetName() const; 207 208 ConstString GetDisplayName() const; 209 210 /// Get accessor for the call site declaration information. 211 /// 212 /// \return 213 /// A reference to the declaration object. 214 Declaration &GetCallSite(); 215 216 /// Get const accessor for the call site declaration information. 217 /// 218 /// \return 219 /// A const reference to the declaration object. 220 const Declaration &GetCallSite() const; 221 222 /// Get accessor for the mangled name object. 223 /// 224 /// \return 225 /// A reference to the mangled name object. 226 Mangled &GetMangled(); 227 228 /// Get const accessor for the mangled name object. 229 /// 230 /// \return 231 /// A const reference to the mangled name object. 232 const Mangled &GetMangled() const; 233 234 /// Get the memory cost of this object. 235 /// 236 /// \return 237 /// The number of bytes that this object occupies in memory. 238 /// The returned value does not include the bytes for any 239 /// shared string values. 240 size_t MemorySize() const override; 241 242 private: 243 /// Mangled inlined function name (can be empty if there is no mangled 244 /// information). 245 Mangled m_mangled; 246 247 Declaration m_call_decl; 248 }; 249 250 class Function; 251 252 /// \class CallSiteParameter Function.h "lldb/Symbol/Function.h" 253 /// 254 /// Represent the locations of a parameter at a call site, both in the caller 255 /// and in the callee. 256 struct CallSiteParameter { 257 DWARFExpressionList LocationInCallee; 258 DWARFExpressionList LocationInCaller; 259 }; 260 261 /// A vector of \c CallSiteParameter. 262 using CallSiteParameterArray = llvm::SmallVector<CallSiteParameter, 0>; 263 264 /// \class CallEdge Function.h "lldb/Symbol/Function.h" 265 /// 266 /// Represent a call made within a Function. This can be used to find a path 267 /// in the call graph between two functions, or to evaluate DW_OP_entry_value. 268 class CallEdge { 269 public: 270 enum class AddrType : uint8_t { Call, AfterCall }; 271 virtual ~CallEdge(); 272 273 /// Get the callee's definition. 274 /// 275 /// Note that this might lazily invoke the DWARF parser. A register context 276 /// from the caller's activation is needed to find indirect call targets. 277 virtual Function *GetCallee(ModuleList &images, 278 ExecutionContext &exe_ctx) = 0; 279 280 /// Get the load PC address of the instruction which executes after the call 281 /// returns. Returns LLDB_INVALID_ADDRESS iff this is a tail call. \p caller 282 /// is the Function containing this call, and \p target is the Target which 283 /// made the call. 284 lldb::addr_t GetReturnPCAddress(Function &caller, Target &target) const; 285 286 /// Return an address in the caller. This can either be the address of the 287 /// call instruction, or the address of the instruction after the call. GetCallerAddress(Function & caller,Target & target)288 std::pair<AddrType, lldb::addr_t> GetCallerAddress(Function &caller, 289 Target &target) const { 290 return {caller_address_type, 291 GetLoadAddress(caller_address, caller, target)}; 292 } 293 IsTailCall()294 bool IsTailCall() const { return is_tail_call; } 295 296 /// Get the call site parameters available at this call edge. GetCallSiteParameters()297 llvm::ArrayRef<CallSiteParameter> GetCallSiteParameters() const { 298 return parameters; 299 } 300 301 /// Non-tail-calls go first, sorted by the return address. They are followed 302 /// by tail calls, which have no specific order. GetSortKey()303 std::pair<bool, lldb::addr_t> GetSortKey() const { 304 return {is_tail_call, GetUnresolvedReturnPCAddress()}; 305 } 306 307 protected: 308 CallEdge(AddrType caller_address_type, lldb::addr_t caller_address, 309 bool is_tail_call, CallSiteParameterArray &¶meters); 310 311 /// Helper that finds the load address of \p unresolved_pc, a file address 312 /// which refers to an instruction within \p caller. 313 static lldb::addr_t GetLoadAddress(lldb::addr_t unresolved_pc, 314 Function &caller, Target &target); 315 316 /// Like \ref GetReturnPCAddress, but returns an unresolved file address. GetUnresolvedReturnPCAddress()317 lldb::addr_t GetUnresolvedReturnPCAddress() const { 318 return caller_address_type == AddrType::AfterCall && !is_tail_call 319 ? caller_address 320 : LLDB_INVALID_ADDRESS; 321 } 322 323 private: 324 lldb::addr_t caller_address; 325 AddrType caller_address_type; 326 bool is_tail_call; 327 328 CallSiteParameterArray parameters; 329 }; 330 331 /// A direct call site. Used to represent call sites where the address of the 332 /// callee is fixed (e.g. a function call in C in which the call target is not 333 /// a function pointer). 334 class DirectCallEdge : public CallEdge { 335 public: 336 /// Construct a call edge using a symbol name to identify the callee, and a 337 /// return PC within the calling function to identify a specific call site. 338 DirectCallEdge(const char *symbol_name, AddrType caller_address_type, 339 lldb::addr_t caller_address, bool is_tail_call, 340 CallSiteParameterArray &¶meters); 341 342 Function *GetCallee(ModuleList &images, ExecutionContext &exe_ctx) override; 343 344 private: 345 void ParseSymbolFileAndResolve(ModuleList &images); 346 347 // Used to describe a direct call. 348 // 349 // Either the callee's mangled name or its definition, discriminated by 350 // \ref resolved. 351 union { 352 const char *symbol_name; 353 Function *def; 354 } lazy_callee; 355 356 /// Whether or not an attempt was made to find the callee's definition. 357 bool resolved = false; 358 }; 359 360 /// An indirect call site. Used to represent call sites where the address of 361 /// the callee is not fixed, e.g. a call to a C++ virtual function (where the 362 /// address is loaded out of a vtable), or a call to a function pointer in C. 363 class IndirectCallEdge : public CallEdge { 364 public: 365 /// Construct a call edge using a DWARFExpression to identify the callee, and 366 /// a return PC within the calling function to identify a specific call site. 367 IndirectCallEdge(DWARFExpressionList call_target, 368 AddrType caller_address_type, lldb::addr_t caller_address, 369 bool is_tail_call, CallSiteParameterArray &¶meters); 370 371 Function *GetCallee(ModuleList &images, ExecutionContext &exe_ctx) override; 372 373 private: 374 // Used to describe an indirect call. 375 // 376 // Specifies the location of the callee address in the calling frame. 377 DWARFExpressionList call_target; 378 }; 379 380 /// \class Function Function.h "lldb/Symbol/Function.h" 381 /// A class that describes a function. 382 /// 383 /// Functions belong to CompileUnit objects (Function::m_comp_unit), have 384 /// unique user IDs (Function::UserID), know how to reconstruct their symbol 385 /// context (Function::SymbolContextScope), have a specific function type 386 /// (Function::m_type_uid), have a simple method name (FunctionInfo::m_name), 387 /// be declared at a specific location (FunctionInfo::m_declaration), possibly 388 /// have mangled names (Function::m_mangled), an optional return type 389 /// (Function::m_type), and contains lexical blocks (Function::m_blocks). 390 /// 391 /// The function information is split into a few pieces: 392 /// \li The concrete instance information 393 /// \li The abstract information 394 /// 395 /// The abstract information is found in the function type (Type) that 396 /// describes a function information, return type and parameter types. 397 /// 398 /// The concrete information is the address range information and specific 399 /// locations for an instance of this function. 400 class Function : public UserID, public SymbolContextScope { 401 public: 402 /// Construct with a compile unit, function UID, function type UID, optional 403 /// mangled name, function type, and a section offset based address range. 404 /// 405 /// \param[in] comp_unit 406 /// The compile unit to which this function belongs. 407 /// 408 /// \param[in] func_uid 409 /// The UID for this function. This value is provided by the 410 /// SymbolFile plug-in and can be any value that allows 411 /// the plug-in to quickly find and parse more detailed 412 /// information when and if more information is needed. 413 /// 414 /// \param[in] func_type_uid 415 /// The type UID for the function Type to allow for lazy type 416 /// parsing from the debug information. 417 /// 418 /// \param[in] mangled 419 /// The optional mangled name for this function. If empty, there 420 /// is no mangled information. 421 /// 422 /// \param[in] func_type 423 /// The optional function type. If NULL, the function type will 424 /// be parsed on demand when accessed using the 425 /// Function::GetType() function by asking the SymbolFile 426 /// plug-in to get the type for \a func_type_uid. 427 /// 428 /// \param[in] range 429 /// The section offset based address for this function. 430 Function(CompileUnit *comp_unit, lldb::user_id_t func_uid, 431 lldb::user_id_t func_type_uid, const Mangled &mangled, 432 Type *func_type, Address address, AddressRanges ranges); 433 434 /// Destructor. 435 ~Function() override; 436 437 /// \copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*) 438 /// 439 /// \see SymbolContextScope 440 void CalculateSymbolContext(SymbolContext *sc) override; 441 442 lldb::ModuleSP CalculateSymbolContextModule() override; 443 444 CompileUnit *CalculateSymbolContextCompileUnit() override; 445 446 Function *CalculateSymbolContextFunction() override; 447 GetAddressRanges()448 AddressRanges GetAddressRanges() { return m_block.GetRanges(); } 449 450 /// Return the address of the function (its entry point). This address is also 451 /// used as a base address for relocation of function-scope entities (blocks 452 /// and variables). GetAddress()453 const Address &GetAddress() const { return m_address; } 454 GetRangeContainingLoadAddress(lldb::addr_t load_addr,Target & target,AddressRange & range)455 bool GetRangeContainingLoadAddress(lldb::addr_t load_addr, Target &target, 456 AddressRange &range) { 457 return m_block.GetRangeContainingLoadAddress(load_addr, target, range); 458 } 459 460 lldb::LanguageType GetLanguage() const; 461 462 /// Find the file and line number of the source location of the start of the 463 /// function. This will use the declaration if present and fall back on the 464 /// line table if that fails. So there may NOT be a line table entry for 465 /// this source file/line combo. 466 /// 467 /// \param[out] source_file 468 /// The source file. 469 /// 470 /// \param[out] line_no 471 /// The line number. 472 void GetStartLineSourceInfo(lldb::SupportFileSP &source_file_sp, 473 uint32_t &line_no); 474 475 using SourceRange = Range<uint32_t, uint32_t>; 476 /// Find the file and line number range of the function. 477 llvm::Expected<std::pair<lldb::SupportFileSP, SourceRange>> GetSourceInfo(); 478 479 /// Get the outgoing call edges from this function, sorted by their return 480 /// PC addresses (in increasing order). 481 llvm::ArrayRef<std::unique_ptr<CallEdge>> GetCallEdges(); 482 483 /// Get the outgoing tail-calling edges from this function. If none exist, 484 /// return std::nullopt. 485 llvm::ArrayRef<std::unique_ptr<CallEdge>> GetTailCallingEdges(); 486 487 /// Get the outgoing call edge from this function which has the given return 488 /// address \p return_pc, or return nullptr. Note that this will not return a 489 /// tail-calling edge. 490 CallEdge *GetCallEdgeForReturnAddress(lldb::addr_t return_pc, Target &target); 491 492 /// Get accessor for the block list. 493 /// 494 /// \return 495 /// The block list object that describes all lexical blocks 496 /// in the function. 497 /// 498 /// \see BlockList 499 Block &GetBlock(bool can_create); 500 501 /// Get accessor for the compile unit that owns this function. 502 /// 503 /// \return 504 /// A compile unit object pointer. 505 CompileUnit *GetCompileUnit(); 506 507 /// Get const accessor for the compile unit that owns this function. 508 /// 509 /// \return 510 /// A const compile unit object pointer. 511 const CompileUnit *GetCompileUnit() const; 512 513 void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target); 514 515 /// Get accessor for the frame base location. 516 /// 517 /// \return 518 /// A location expression that describes the function frame 519 /// base. GetFrameBaseExpression()520 DWARFExpressionList &GetFrameBaseExpression() { return m_frame_base; } 521 522 /// Get const accessor for the frame base location. 523 /// 524 /// \return 525 /// A const compile unit object pointer. GetFrameBaseExpression()526 const DWARFExpressionList &GetFrameBaseExpression() const { return m_frame_base; } 527 528 ConstString GetName() const; 529 530 ConstString GetNameNoArguments() const; 531 532 ConstString GetDisplayName() const; 533 GetMangled()534 const Mangled &GetMangled() const { return m_mangled; } 535 536 /// Get the DeclContext for this function, if available. 537 /// 538 /// \return 539 /// The DeclContext, or NULL if none exists. 540 CompilerDeclContext GetDeclContext(); 541 542 /// Get the CompilerContext for this function, if available. 543 /// 544 /// \return 545 /// The CompilerContext, or an empty vector if none is available. 546 std::vector<CompilerContext> GetCompilerContext(); 547 548 /// Get accessor for the type that describes the function return value type, 549 /// and parameter types. 550 /// 551 /// \return 552 /// A type object pointer. 553 Type *GetType(); 554 555 /// Get const accessor for the type that describes the function return value 556 /// type, and parameter types. 557 /// 558 /// \return 559 /// A const type object pointer. 560 const Type *GetType() const; 561 562 CompilerType GetCompilerType(); 563 564 /// Get the size of the prologue instructions for this function. The 565 /// "prologue" instructions include any instructions given line number 0 566 /// immediately following the prologue end. 567 /// 568 /// \return 569 /// The size of the prologue. 570 uint32_t GetPrologueByteSize(); 571 572 /// Dump a description of this object to a Stream. 573 /// 574 /// Dump a description of the contents of this object to the supplied stream 575 /// \a s. 576 /// 577 /// \param[in] s 578 /// The stream to which to dump the object description. 579 /// 580 /// \param[in] show_context 581 /// If \b true, variables will dump their symbol context 582 /// information. 583 void Dump(Stream *s, bool show_context) const; 584 585 /// \copydoc SymbolContextScope::DumpSymbolContext(Stream*) 586 /// 587 /// \see SymbolContextScope 588 void DumpSymbolContext(Stream *s) override; 589 590 /// Get the memory cost of this object. 591 /// 592 /// \return 593 /// The number of bytes that this object occupies in memory. 594 /// The returned value does not include the bytes for any 595 /// shared string values. 596 size_t MemorySize() const; 597 598 /// Get whether compiler optimizations were enabled for this function 599 /// 600 /// The debug information may provide information about whether this 601 /// function was compiled with optimization or not. In this case, 602 /// "optimized" means that the debug experience may be difficult for the 603 /// user to understand. Variables may not be available when the developer 604 /// would expect them, stepping through the source lines in the function may 605 /// appear strange, etc. 606 /// 607 /// \return 608 /// Returns 'true' if this function was compiled with 609 /// optimization. 'false' indicates that either the optimization 610 /// is unknown, or this function was built without optimization. 611 bool GetIsOptimized(); 612 613 /// Get whether this function represents a 'top-level' function 614 /// 615 /// The concept of a top-level function is language-specific, mostly meant 616 /// to represent the notion of scripting-style code that has global 617 /// visibility of the variables/symbols/functions/... defined within the 618 /// containing file/module 619 /// 620 /// If stopped in a top-level function, LLDB will expose global variables 621 /// as-if locals in the 'frame variable' command 622 /// 623 /// \return 624 /// Returns 'true' if this function is a top-level function, 625 /// 'false' otherwise. 626 bool IsTopLevelFunction(); 627 628 lldb::DisassemblerSP GetInstructions(const ExecutionContext &exe_ctx, 629 const char *flavor, 630 bool force_live_memory = false); 631 632 bool GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor, 633 Stream &strm, bool force_live_memory = false); 634 635 protected: 636 enum { 637 /// Whether we already tried to calculate the prologue size. 638 flagsCalculatedPrologueSize = (1 << 0) 639 }; 640 641 /// The compile unit that owns this function. 642 CompileUnit *m_comp_unit; 643 644 /// The user ID of for the prototype Type for this function. 645 lldb::user_id_t m_type_uid; 646 647 /// The function prototype type for this function that includes the function 648 /// info (FunctionInfo), return type and parameters. 649 Type *m_type; 650 651 /// The mangled function name if any. If empty, there is no mangled 652 /// information. 653 Mangled m_mangled; 654 655 /// All lexical blocks contained in this function. 656 Block m_block; 657 658 /// The address (entry point) of the function. 659 Address m_address; 660 661 /// The frame base expression for variables that are relative to the frame 662 /// pointer. 663 DWARFExpressionList m_frame_base; 664 665 Flags m_flags; 666 667 /// Compute the prologue size once and cache it. 668 uint32_t m_prologue_byte_size; 669 670 /// Exclusive lock that controls read/write access to m_call_edges and 671 /// m_call_edges_resolved. 672 std::mutex m_call_edges_lock; 673 674 /// Whether call site info has been parsed. 675 bool m_call_edges_resolved = false; 676 677 /// Outgoing call edges. 678 std::vector<std::unique_ptr<CallEdge>> m_call_edges; 679 680 private: 681 Function(const Function &) = delete; 682 const Function &operator=(const Function &) = delete; 683 }; 684 685 } // namespace lldb_private 686 687 #endif // LLDB_SYMBOL_FUNCTION_H 688