LLVM 20.0.0git
FunctionInfo.h
Go to the documentation of this file.
1//===- FunctionInfo.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_FUNCTIONINFO_H
10#define LLVM_DEBUGINFO_GSYM_FUNCTIONINFO_H
11
19#include <cstdint>
20
21namespace llvm {
22class raw_ostream;
23
24namespace gsym {
25
26class GsymReader;
27/// Function information in GSYM files encodes information for one contiguous
28/// address range. If a function has discontiguous address ranges, they will
29/// need to be encoded using multiple FunctionInfo objects.
30///
31/// ENCODING
32///
33/// The function information gets the function start address as an argument
34/// to the FunctionInfo::decode(...) function. This information is calculated
35/// from the GSYM header and an address offset from the GSYM address offsets
36/// table. The encoded FunctionInfo information must be aligned to a 4 byte
37/// boundary.
38///
39/// The encoded data for a FunctionInfo starts with fixed data that all
40/// function info objects have:
41///
42/// ENCODING NAME DESCRIPTION
43/// ========= =========== ====================================================
44/// uint32_t Size The size in bytes of this function.
45/// uint32_t Name The string table offset of the function name.
46///
47/// The optional data in a FunctionInfo object follows this fixed information
48/// and consists of a stream of tuples that consist of:
49///
50/// ENCODING NAME DESCRIPTION
51/// ========= =========== ====================================================
52/// uint32_t InfoType An "InfoType" enumeration that describes the type
53/// of optional data that is encoded.
54/// uint32_t InfoLength The size in bytes of the encoded data that
55/// immediately follows this length if this value is
56/// greater than zero.
57/// uint8_t[] InfoData Encoded bytes that represent the data for the
58/// "InfoType". These bytes are only present if
59/// "InfoLength" is greater than zero.
60///
61/// The "InfoType" is an enumeration:
62///
63/// enum InfoType {
64/// EndOfList = 0u,
65/// LineTableInfo = 1u,
66/// InlineInfo = 2u
67/// };
68///
69/// This stream of tuples is terminated by a "InfoType" whose value is
70/// InfoType::EndOfList and a zero for "InfoLength". This signifies the end of
71/// the optional information list. This format allows us to add new optional
72/// information data to a FunctionInfo object over time and allows older
73/// clients to still parse the format and skip over any data that they don't
74/// understand or want to parse.
75///
76/// So the function information encoding essientially looks like:
77///
78/// struct {
79/// uint32_t Size;
80/// uint32_t Name;
81/// struct {
82/// uint32_t InfoType;
83/// uint32_t InfoLength;
84/// uint8_t InfoData[InfoLength];
85/// }[N];
86/// }
87///
88/// Where "N" is the number of tuples.
91 uint32_t Name; ///< String table offset in the string table.
92 std::optional<LineTable> OptLineTable;
93 std::optional<InlineInfo> Inline;
94 std::optional<MergedFunctionsInfo> MergedFunctions;
95 /// If we encode a FunctionInfo during segmenting so we know its size, we can
96 /// cache that encoding here so we don't need to re-encode it when saving the
97 /// GSYM file.
99
101 : Range(Addr, Addr + Size), Name(N) {}
102
103 /// Query if a FunctionInfo has rich debug info.
104 ///
105 /// \returns A bool that indicates if this object has something else than
106 /// range and name. When converting information from a symbol table and from
107 /// debug info, we might end up with multiple FunctionInfo objects for the
108 /// same range and we need to be able to tell which one is the better object
109 /// to use.
110 bool hasRichInfo() const { return OptLineTable || Inline; }
111
112 /// Query if a FunctionInfo object is valid.
113 ///
114 /// Address and size can be zero and there can be no line entries for a
115 /// symbol so the only indication this entry is valid is if the name is
116 /// not zero. This can happen when extracting information from symbol
117 /// tables that do not encode symbol sizes. In that case only the
118 /// address and name will be filled in.
119 ///
120 /// \returns A boolean indicating if this FunctionInfo is valid.
121 bool isValid() const {
122 return Name != 0;
123 }
124
125 /// Decode an object from a binary data stream.
126 ///
127 /// \param Data The binary stream to read the data from. This object must
128 /// have the data for the object starting at offset zero. The data
129 /// can contain more data than needed.
130 ///
131 /// \param BaseAddr The FunctionInfo's start address and will be used as the
132 /// base address when decoding any contained information like the line table
133 /// and the inline info.
134 ///
135 /// \returns An FunctionInfo or an error describing the issue that was
136 /// encountered during decoding.
138 uint64_t BaseAddr);
139
140 /// Encode this object into FileWriter stream.
141 ///
142 /// \param O The binary stream to write the data to at the current file
143 /// position.
144 ///
145 /// \param NoPadding Directly write the FunctionInfo data, without any padding
146 /// By default, FunctionInfo will be 4-byte aligned by padding with
147 /// 0's at the start. This is OK since the function will return the offset of
148 /// actual data in the stream. However when writing FunctionInfo's as a
149 /// stream, the padding will break the decoding of the data - since the offset
150 /// where the FunctionInfo starts is not kept in this scenario.
151 ///
152 /// \returns An error object that indicates failure or the offset of the
153 /// function info that was successfully written into the stream.
154 llvm::Expected<uint64_t> encode(FileWriter &O, bool NoPadding = false) const;
155
156 /// Encode this function info into the internal byte cache and return the size
157 /// in bytes.
158 ///
159 /// When segmenting GSYM files we need to know how big each FunctionInfo will
160 /// encode into so we can generate segments of the right size. We don't want
161 /// to have to encode a FunctionInfo twice, so we can cache the encoded bytes
162 /// and re-use then when calling FunctionInfo::encode(...).
163 ///
164 /// \returns The size in bytes of the FunctionInfo if it were to be encoded
165 /// into a byte stream.
167
168 /// Lookup an address within a FunctionInfo object's data stream.
169 ///
170 /// Instead of decoding an entire FunctionInfo object when doing lookups,
171 /// we can decode only the information we need from the FunctionInfo's data
172 /// for the specific address. The lookup result information is returned as
173 /// a LookupResult.
174 ///
175 /// \param Data The binary stream to read the data from. This object must
176 /// have the data for the object starting at offset zero. The data
177 /// can contain more data than needed.
178 ///
179 /// \param GR The GSYM reader that contains the string and file table that
180 /// will be used to fill in information in the returned result.
181 ///
182 /// \param FuncAddr The function start address decoded from the GsymReader.
183 ///
184 /// \param Addr The address to lookup.
185 ///
186 /// \returns An LookupResult or an error describing the issue that was
187 /// encountered during decoding. An error should only be returned if the
188 /// address is not contained in the FunctionInfo or if the data is corrupted.
190 const GsymReader &GR,
191 uint64_t FuncAddr,
192 uint64_t Addr);
193
194 uint64_t startAddress() const { return Range.start(); }
195 uint64_t endAddress() const { return Range.end(); }
196 uint64_t size() const { return Range.size(); }
197
198 void clear() {
199 Range = {0, 0};
200 Name = 0;
201 OptLineTable = std::nullopt;
202 Inline = std::nullopt;
203 }
204};
205
206inline bool operator==(const FunctionInfo &LHS, const FunctionInfo &RHS) {
207 return LHS.Range == RHS.Range && LHS.Name == RHS.Name &&
208 LHS.OptLineTable == RHS.OptLineTable && LHS.Inline == RHS.Inline;
209}
210inline bool operator!=(const FunctionInfo &LHS, const FunctionInfo &RHS) {
211 return !(LHS == RHS);
212}
213/// This sorting will order things consistently by address range first, but
214/// then followed by increasing levels of debug info like inline information
215/// and line tables. We might end up with a FunctionInfo from debug info that
216/// will have the same range as one from the symbol table, but we want to
217/// quickly be able to sort and use the best version when creating the final
218/// GSYM file. This function compares the inline information as we have seen
219/// cases where LTO can generate a wide array of differing inline information,
220/// mostly due to messing up the address ranges for inlined functions, so the
221/// inline information with the most entries will appeear last. If the inline
222/// information match, either by both function infos not having any or both
223/// being exactly the same, we will then compare line tables. Comparing line
224/// tables allows the entry with the most line entries to appear last. This
225/// ensures we are able to save the FunctionInfo with the most debug info into
226/// the GSYM file.
227inline bool operator<(const FunctionInfo &LHS, const FunctionInfo &RHS) {
228 // First sort by address range
229 if (LHS.Range != RHS.Range)
230 return LHS.Range < RHS.Range;
231 if (LHS.Inline == RHS.Inline)
232 return LHS.OptLineTable < RHS.OptLineTable;
233 return LHS.Inline < RHS.Inline;
234}
235
236raw_ostream &operator<<(raw_ostream &OS, const FunctionInfo &R);
237
238} // namespace gsym
239} // namespace llvm
240
241#endif // LLVM_DEBUGINFO_GSYM_FUNCTIONINFO_H
uint64_t Addr
uint64_t Size
raw_pwrite_stream & OS
This file defines the SmallString class.
Value * RHS
Value * LHS
A class that represents an address range.
Definition: AddressRanges.h:22
uint64_t start() const
Definition: AddressRanges.h:28
uint64_t end() const
Definition: AddressRanges.h:29
uint64_t size() const
Definition: AddressRanges.h:30
Tagged union holding either a T or a Error.
Definition: Error.h:481
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
A simplified binary data writer class that doesn't require targets, target definitions,...
Definition: FileWriter.h:29
GsymReader is used to read GSYM data from a file or buffer.
Definition: GsymReader.h:44
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & operator<<(raw_ostream &OS, const FunctionInfo &R)
bool operator<(const FunctionInfo &LHS, const FunctionInfo &RHS)
This sorting will order things consistently by address range first, but then followed by increasing l...
Definition: FunctionInfo.h:227
bool operator!=(const FunctionInfo &LHS, const FunctionInfo &RHS)
Definition: FunctionInfo.h:210
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
#define N
Function information in GSYM files encodes information for one contiguous address range.
Definition: FunctionInfo.h:89
std::optional< InlineInfo > Inline
Definition: FunctionInfo.h:93
std::optional< MergedFunctionsInfo > MergedFunctions
Definition: FunctionInfo.h:94
uint64_t startAddress() const
Definition: FunctionInfo.h:194
uint64_t endAddress() const
Definition: FunctionInfo.h:195
bool isValid() const
Query if a FunctionInfo object is valid.
Definition: FunctionInfo.h:121
bool hasRichInfo() const
Query if a FunctionInfo has rich debug info.
Definition: FunctionInfo.h:110
uint64_t size() const
Definition: FunctionInfo.h:196
static llvm::Expected< LookupResult > lookup(DataExtractor &Data, const GsymReader &GR, uint64_t FuncAddr, uint64_t Addr)
Lookup an address within a FunctionInfo object's data stream.
uint64_t cacheEncoding()
Encode this function info into the internal byte cache and return the size in bytes.
uint32_t Name
String table offset in the string table.
Definition: FunctionInfo.h:91
llvm::Expected< uint64_t > encode(FileWriter &O, bool NoPadding=false) const
Encode this object into FileWriter stream.
SmallString< 32 > EncodingCache
If we encode a FunctionInfo during segmenting so we know its size, we can cache that encoding here so...
Definition: FunctionInfo.h:98
std::optional< LineTable > OptLineTable
Definition: FunctionInfo.h:92
FunctionInfo(uint64_t Addr=0, uint64_t Size=0, uint32_t N=0)
Definition: FunctionInfo.h:100
static llvm::Expected< FunctionInfo > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an object from a binary data stream.