1 //===- CallSiteInfo.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 LLVM_DEBUGINFO_GSYM_CALLSITEINFO_H 10 #define LLVM_DEBUGINFO_GSYM_CALLSITEINFO_H 11 12 #include "llvm/ADT/BitmaskEnum.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/ADT/StringSet.h" 15 #include "llvm/Support/Compiler.h" 16 #include "llvm/Support/Error.h" 17 #include <vector> 18 19 namespace llvm { 20 class DataExtractor; 21 class raw_ostream; 22 23 namespace yaml { 24 struct FunctionsYAML; 25 } // namespace yaml 26 27 namespace gsym { 28 class FileWriter; 29 class GsymCreator; 30 struct FunctionInfo; 31 struct CallSiteInfo { 32 enum Flags : uint8_t { 33 None = 0, 34 // This flag specifies that the call site can only call a function within 35 // the same link unit as the call site. 36 InternalCall = 1 << 0, 37 // This flag specifies that the call site can only call a function outside 38 // the link unit that the call site is in. 39 ExternalCall = 1 << 1, 40 41 LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue*/ ExternalCall), 42 }; 43 44 /// The return offset of the call site - relative to the function start. 45 uint64_t ReturnOffset = 0; 46 47 /// Offsets into the string table for function names regex patterns. 48 std::vector<uint32_t> MatchRegex; 49 50 /// Bitwise OR of CallSiteInfo::Flags values 51 uint8_t Flags = CallSiteInfo::Flags::None; 52 53 /// Equality comparison operator for CallSiteInfo. 54 bool operator==(const CallSiteInfo &RHS) const { 55 return ReturnOffset == RHS.ReturnOffset && MatchRegex == RHS.MatchRegex && 56 Flags == RHS.Flags; 57 } 58 59 /// Inequality comparison operator for CallSiteInfo. 60 bool operator!=(const CallSiteInfo &RHS) const { return !(*this == RHS); } 61 62 /// Decode a CallSiteInfo object from a binary data stream. 63 /// 64 /// \param Data The binary stream to read the data from. 65 /// \param Offset The current offset within the data stream. 66 /// \returns A CallSiteInfo or an error describing the issue. 67 LLVM_ABI static llvm::Expected<CallSiteInfo> decode(DataExtractor &Data, 68 uint64_t &Offset); 69 70 /// Encode this CallSiteInfo object into a FileWriter stream. 71 /// 72 /// \param O The binary stream to write the data to. 73 /// \returns An error object that indicates success or failure. 74 LLVM_ABI llvm::Error encode(FileWriter &O) const; 75 }; 76 77 struct CallSiteInfoCollection { 78 std::vector<CallSiteInfo> CallSites; 79 80 /// Decode a CallSiteInfoCollection object from a binary data stream. 81 /// 82 /// \param Data The binary stream to read the data from. 83 /// \returns A CallSiteInfoCollection or an error describing the issue. 84 LLVM_ABI static llvm::Expected<CallSiteInfoCollection> 85 decode(DataExtractor &Data); 86 87 /// Encode this CallSiteInfoCollection object into a FileWriter stream. 88 /// 89 /// \param O The binary stream to write the data to. 90 /// \returns An error object that indicates success or failure. 91 LLVM_ABI llvm::Error encode(FileWriter &O) const; 92 }; 93 94 class CallSiteInfoLoader { 95 public: 96 /// Constructor that initializes the CallSiteInfoLoader with necessary data 97 /// structures. 98 /// 99 /// \param GCreator A reference to the GsymCreator. CallSiteInfoLoader(GsymCreator & GCreator,std::vector<FunctionInfo> & Funcs)100 CallSiteInfoLoader(GsymCreator &GCreator, std::vector<FunctionInfo> &Funcs) 101 : GCreator(GCreator), Funcs(Funcs) {} 102 103 /// This method reads the specified YAML file, parses its content, and updates 104 /// the `Funcs` vector with call site information based on the YAML data. 105 /// 106 /// \param Funcs A reference to a vector of FunctionInfo objects to be 107 /// populated. 108 /// \param YAMLFile A StringRef representing the path to the YAML 109 /// file to be loaded. 110 /// \returns An `llvm::Error` indicating success or describing any issues 111 /// encountered during the loading process. 112 LLVM_ABI llvm::Error loadYAML(StringRef YAMLFile); 113 114 private: 115 /// Builds a map from function names to FunctionInfo pointers based on the 116 /// provided `Funcs` vector. 117 /// 118 /// \param Funcs A reference to a vector of FunctionInfo objects. 119 /// \returns A StringMap mapping function names (StringRef) to their 120 /// corresponding FunctionInfo pointers. 121 StringMap<FunctionInfo *> buildFunctionMap(); 122 123 /// Processes the parsed YAML functions and updates the `FuncMap` accordingly. 124 /// 125 /// \param FuncYAMLs A constant reference to an llvm::yaml::FunctionsYAML 126 /// object containing parsed YAML data. 127 /// \param FuncMap A reference to a StringMap mapping function names to 128 /// FunctionInfo pointers. 129 /// \returns An `llvm::Error` indicating success or describing any issues 130 /// encountered during processing. 131 llvm::Error processYAMLFunctions(const llvm::yaml::FunctionsYAML &FuncYAMLs, 132 StringMap<FunctionInfo *> &FuncMap); 133 134 /// Reference to the parent Gsym Creator object. 135 GsymCreator &GCreator; 136 137 /// Reference to the vector of FunctionInfo objects to be populated. 138 std::vector<FunctionInfo> &Funcs; 139 }; 140 141 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, const CallSiteInfo &CSI); 142 LLVM_ABI raw_ostream &operator<<(raw_ostream &OS, 143 const CallSiteInfoCollection &CSIC); 144 145 } // namespace gsym 146 } // namespace llvm 147 148 #endif // LLVM_DEBUGINFO_GSYM_CALLSITEINFO_H 149