LLVM 20.0.0git
FunctionInfo.cpp
Go to the documentation of this file.
1//===- FunctionInfo.cpp ---------------------------------------------------===//
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
15#include <optional>
16
17using namespace llvm;
18using namespace gsym;
19
20/// FunctionInfo information type that is used to encode the optional data
21/// that is associated with a FunctionInfo object.
28};
29
31 OS << FI.Range << ": " << "Name=" << HEX32(FI.Name) << '\n';
32 if (FI.OptLineTable)
33 OS << FI.OptLineTable << '\n';
34 if (FI.Inline)
35 OS << FI.Inline << '\n';
36 if (FI.CallSites)
37 OS << *FI.CallSites << '\n';
38 return OS;
39}
40
42 uint64_t BaseAddr) {
43 FunctionInfo FI;
44 uint64_t Offset = 0;
45 if (!Data.isValidOffsetForDataOfSize(Offset, 4))
46 return createStringError(std::errc::io_error,
47 "0x%8.8" PRIx64 ": missing FunctionInfo Size", Offset);
48 FI.Range = {BaseAddr, BaseAddr + Data.getU32(&Offset)};
49 if (!Data.isValidOffsetForDataOfSize(Offset, 4))
50 return createStringError(std::errc::io_error,
51 "0x%8.8" PRIx64 ": missing FunctionInfo Name", Offset);
52 FI.Name = Data.getU32(&Offset);
53 if (FI.Name == 0)
54 return createStringError(std::errc::io_error,
55 "0x%8.8" PRIx64 ": invalid FunctionInfo Name value 0x%8.8x",
56 Offset - 4, FI.Name);
57 bool Done = false;
58 while (!Done) {
59 if (!Data.isValidOffsetForDataOfSize(Offset, 4))
60 return createStringError(std::errc::io_error,
61 "0x%8.8" PRIx64 ": missing FunctionInfo InfoType value", Offset);
62 const uint32_t IT = Data.getU32(&Offset);
63 if (!Data.isValidOffsetForDataOfSize(Offset, 4))
64 return createStringError(std::errc::io_error,
65 "0x%8.8" PRIx64 ": missing FunctionInfo InfoType length", Offset);
66 const uint32_t InfoLength = Data.getU32(&Offset);
67 if (!Data.isValidOffsetForDataOfSize(Offset, InfoLength))
68 return createStringError(std::errc::io_error,
69 "0x%8.8" PRIx64 ": missing FunctionInfo data for InfoType %u",
70 Offset, IT);
71 DataExtractor InfoData(Data.getData().substr(Offset, InfoLength),
72 Data.isLittleEndian(),
73 Data.getAddressSize());
74 switch (IT) {
75 case InfoType::EndOfList:
76 Done = true;
77 break;
78
79 case InfoType::LineTableInfo:
80 if (Expected<LineTable> LT = LineTable::decode(InfoData, BaseAddr))
81 FI.OptLineTable = std::move(LT.get());
82 else
83 return LT.takeError();
84 break;
85
86 case InfoType::InlineInfo:
87 if (Expected<InlineInfo> II = InlineInfo::decode(InfoData, BaseAddr))
88 FI.Inline = std::move(II.get());
89 else
90 return II.takeError();
91 break;
92
93 case InfoType::MergedFunctionsInfo:
95 MergedFunctionsInfo::decode(InfoData, BaseAddr))
96 FI.MergedFunctions = std::move(MI.get());
97 else
98 return MI.takeError();
99 break;
100
101 case InfoType::CallSiteInfo:
104 FI.CallSites = std::move(CI.get());
105 else
106 return CI.takeError();
107 break;
108
109 default:
110 return createStringError(std::errc::io_error,
111 "0x%8.8" PRIx64 ": unsupported InfoType %u",
112 Offset-8, IT);
113 }
114 Offset += InfoLength;
115 }
116 return std::move(FI);
117}
118
121 if (!isValid())
122 return 0;
125 llvm::Expected<uint64_t> Result = encode(FW);
126 if (!Result) {
128 consumeError(Result.takeError());
129 return 0;
130 }
131 return EncodingCache.size();
132}
133
135 bool NoPadding) const {
136 if (!isValid())
137 return createStringError(std::errc::invalid_argument,
138 "attempted to encode invalid FunctionInfo object");
139 // Align FunctionInfo data to a 4 byte alignment, if padding is allowed
140 if (NoPadding == false)
141 Out.alignTo(4);
142 const uint64_t FuncInfoOffset = Out.tell();
143 // Check if we have already encoded this function info into EncodingCache.
144 // This will be non empty when creating segmented GSYM files as we need to
145 // precompute exactly how big FunctionInfo objects encode into so we can
146 // accurately make segments of a specific size.
147 if (!EncodingCache.empty() &&
149 // We already encoded this object, just write out the bytes.
152 return FuncInfoOffset;
153 }
154 // Write the size in bytes of this function as a uint32_t. This can be zero
155 // if we just have a symbol from a symbol table and that symbol has no size.
156 Out.writeU32(size());
157 // Write the name of this function as a uint32_t string table offset.
158 Out.writeU32(Name);
159
160 if (OptLineTable) {
161 Out.writeU32(InfoType::LineTableInfo);
162 // Write a uint32_t length as zero for now, we will fix this up after
163 // writing the LineTable out with the number of bytes that were written.
164 Out.writeU32(0);
165 const auto StartOffset = Out.tell();
166 llvm::Error err = OptLineTable->encode(Out, Range.start());
167 if (err)
168 return std::move(err);
169 const auto Length = Out.tell() - StartOffset;
170 if (Length > UINT32_MAX)
171 return createStringError(std::errc::invalid_argument,
172 "LineTable length is greater than UINT32_MAX");
173 // Fixup the size of the LineTable data with the correct size.
174 Out.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
175 }
176
177 // Write out the inline function info if we have any and if it is valid.
178 if (Inline) {
179 Out.writeU32(InfoType::InlineInfo);
180 // Write a uint32_t length as zero for now, we will fix this up after
181 // writing the LineTable out with the number of bytes that were written.
182 Out.writeU32(0);
183 const auto StartOffset = Out.tell();
184 llvm::Error err = Inline->encode(Out, Range.start());
185 if (err)
186 return std::move(err);
187 const auto Length = Out.tell() - StartOffset;
188 if (Length > UINT32_MAX)
189 return createStringError(std::errc::invalid_argument,
190 "InlineInfo length is greater than UINT32_MAX");
191 // Fixup the size of the InlineInfo data with the correct size.
192 Out.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
193 }
194
195 // Write out the merged functions info if we have any and if it is valid.
196 if (MergedFunctions) {
197 Out.writeU32(InfoType::MergedFunctionsInfo);
198 // Write a uint32_t length as zero for now, we will fix this up after
199 // writing the LineTable out with the number of bytes that were written.
200 Out.writeU32(0);
201 const auto StartOffset = Out.tell();
202 llvm::Error err = MergedFunctions->encode(Out);
203 if (err)
204 return std::move(err);
205 const auto Length = Out.tell() - StartOffset;
206 if (Length > UINT32_MAX)
207 return createStringError(
208 std::errc::invalid_argument,
209 "MergedFunctionsInfo length is greater than UINT32_MAX");
210 // Fixup the size of the MergedFunctionsInfo data with the correct size.
211 Out.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
212 }
213
214 // Write out the call sites if we have any and if they are valid.
215 if (CallSites) {
216 Out.writeU32(InfoType::CallSiteInfo);
217 // Write a uint32_t length as zero for now, we will fix this up after
218 // writing the CallSites out with the number of bytes that were written.
219 Out.writeU32(0);
220 const auto StartOffset = Out.tell();
221 Error Err = CallSites->encode(Out);
222 if (Err)
223 return std::move(Err);
224 const auto Length = Out.tell() - StartOffset;
225 if (Length > UINT32_MAX)
226 return createStringError(std::errc::invalid_argument,
227 "CallSites length is greater than UINT32_MAX");
228 // Fixup the size of the CallSites data with the correct size.
229 Out.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
230 }
231
232 // Terminate the data chunks with an end of list with zero size.
233 Out.writeU32(InfoType::EndOfList);
234 Out.writeU32(0);
235 return FuncInfoOffset;
236}
237
239 const GsymReader &GR,
240 uint64_t FuncAddr,
241 uint64_t Addr) {
242 LookupResult LR;
243 LR.LookupAddr = Addr;
244 uint64_t Offset = 0;
245 LR.FuncRange = {FuncAddr, FuncAddr + Data.getU32(&Offset)};
246 uint32_t NameOffset = Data.getU32(&Offset);
247 // The "lookup" functions doesn't report errors as accurately as the "decode"
248 // function as it is meant to be fast. For more accurage errors we could call
249 // "decode".
250 if (!Data.isValidOffset(Offset))
251 return createStringError(std::errc::io_error,
252 "FunctionInfo data is truncated");
253 // This function will be called with the result of a binary search of the
254 // address table, we must still make sure the address does not fall into a
255 // gap between functions are after the last function.
256 if (LR.FuncRange.size() > 0 && !LR.FuncRange.contains(Addr))
257 return createStringError(std::errc::io_error,
258 "address 0x%" PRIx64 " is not in GSYM", Addr);
259
260 if (NameOffset == 0)
261 return createStringError(std::errc::io_error,
262 "0x%8.8" PRIx64 ": invalid FunctionInfo Name value 0x00000000",
263 Offset - 4);
264 LR.FuncName = GR.getString(NameOffset);
265 bool Done = false;
266 std::optional<LineEntry> LineEntry;
267 std::optional<DataExtractor> InlineInfoData;
268 while (!Done) {
269 if (!Data.isValidOffsetForDataOfSize(Offset, 8))
270 return createStringError(std::errc::io_error,
271 "FunctionInfo data is truncated");
272 const uint32_t IT = Data.getU32(&Offset);
273 const uint32_t InfoLength = Data.getU32(&Offset);
274 const StringRef InfoBytes = Data.getData().substr(Offset, InfoLength);
275 if (InfoLength != InfoBytes.size())
276 return createStringError(std::errc::io_error,
277 "FunctionInfo data is truncated");
278 DataExtractor InfoData(InfoBytes, Data.isLittleEndian(),
279 Data.getAddressSize());
280 switch (IT) {
281 case InfoType::EndOfList:
282 Done = true;
283 break;
284
285 case InfoType::LineTableInfo:
286 if (auto ExpectedLE = LineTable::lookup(InfoData, FuncAddr, Addr))
287 LineEntry = ExpectedLE.get();
288 else
289 return ExpectedLE.takeError();
290 break;
291
292 case InfoType::InlineInfo:
293 // We will parse the inline info after our line table, but only if
294 // we have a line entry.
295 InlineInfoData = InfoData;
296 break;
297
298 default:
299 break;
300 }
301 Offset += InfoLength;
302 }
303
304 if (!LineEntry) {
305 // We don't have a valid line entry for our address, fill in our source
306 // location as best we can and return.
307 SourceLocation SrcLoc;
308 SrcLoc.Name = LR.FuncName;
309 SrcLoc.Offset = Addr - FuncAddr;
310 LR.Locations.push_back(SrcLoc);
311 return LR;
312 }
313
314 std::optional<FileEntry> LineEntryFile = GR.getFile(LineEntry->File);
315 if (!LineEntryFile)
316 return createStringError(std::errc::invalid_argument,
317 "failed to extract file[%" PRIu32 "]",
318 LineEntry->File);
319
320 SourceLocation SrcLoc;
321 SrcLoc.Name = LR.FuncName;
322 SrcLoc.Offset = Addr - FuncAddr;
323 SrcLoc.Dir = GR.getString(LineEntryFile->Dir);
324 SrcLoc.Base = GR.getString(LineEntryFile->Base);
325 SrcLoc.Line = LineEntry->Line;
326 LR.Locations.push_back(SrcLoc);
327 // If we don't have inline information, we are done.
328 if (!InlineInfoData)
329 return LR;
330 // We have inline information. Try to augment the lookup result with this
331 // data.
332 llvm::Error Err = InlineInfo::lookup(GR, *InlineInfoData, FuncAddr, Addr,
333 LR.Locations);
334 if (Err)
335 return std::move(Err);
336 return LR;
337}
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
uint64_t Addr
#define HEX32(v)
Definition: ExtractRanges.h:19
InfoType
FunctionInfo information type that is used to encode the optional data that is associated with a Func...
@ EndOfList
@ LineTableInfo
IRTranslator LLVM IR MI
uint64_t IntrinsicInst * II
raw_pwrite_stream & OS
uint64_t start() const
Definition: AddressRanges.h:28
bool contains(uint64_t Addr) const
Definition: AddressRanges.h:32
uint64_t size() const
Definition: AddressRanges.h:30
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:481
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:286
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:150
A simplified binary data writer class that doesn't require targets, target definitions,...
Definition: FileWriter.h:29
uint64_t tell()
Return the current offset within the file.
Definition: FileWriter.cpp:66
void fixup32(uint32_t Value, uint64_t Offset)
Fixup a uint32_t value at the specified offset in the stream.
Definition: FileWriter.cpp:52
void alignTo(size_t Align)
Pad with zeroes at the current file position until the current file position matches the specified al...
Definition: FileWriter.cpp:70
void writeU32(uint32_t Value)
Write a single uint32_t value into the stream at the current file position.
Definition: FileWriter.cpp:42
void writeData(llvm::ArrayRef< uint8_t > Data)
Write an array of uint8_t values into the stream at the current file position.
Definition: FileWriter.cpp:58
llvm::endianness getByteOrder() const
Definition: FileWriter.h:117
GsymReader is used to read GSYM data from a file or buffer.
Definition: GsymReader.h:44
std::optional< FileEntry > getFile(uint32_t Index) const
Get the a file entry for the suppplied file index.
Definition: GsymReader.h:150
StringRef getString(uint32_t Offset) const
Get a string from the string table.
Definition: GsymReader.h:139
static llvm::Expected< LineTable > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an LineTable object from a binary data stream.
Definition: LineTable.cpp:251
static Expected< LineEntry > lookup(DataExtractor &Data, uint64_t BaseAddr, uint64_t Addr)
Lookup a single address within a line table's data.
Definition: LineTable.cpp:266
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
raw_ostream & operator<<(raw_ostream &OS, const CallSiteInfo &CSI)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
@ Length
Definition: DWP.cpp:480
@ Done
Definition: Threading.h:61
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1291
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
static llvm::Expected< CallSiteInfoCollection > decode(DataExtractor &Data)
Decode a CallSiteInfoCollection object from a binary data stream.
Function information in GSYM files encodes information for one contiguous address range.
Definition: FunctionInfo.h:92
std::optional< InlineInfo > Inline
Definition: FunctionInfo.h:96
std::optional< MergedFunctionsInfo > MergedFunctions
Definition: FunctionInfo.h:97
bool isValid() const
Query if a FunctionInfo object is valid.
Definition: FunctionInfo.h:125
std::optional< CallSiteInfoCollection > CallSites
Definition: FunctionInfo.h:98
uint64_t size() const
Definition: FunctionInfo.h:200
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:94
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:102
std::optional< LineTable > OptLineTable
Definition: FunctionInfo.h:95
Inline information stores the name of the inline function along with an array of address ranges.
Definition: InlineInfo.h:59
static llvm::Error lookup(const GsymReader &GR, DataExtractor &Data, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs)
Lookup a single address within the inline info data.
Definition: InlineInfo.cpp:159
static llvm::Expected< InlineInfo > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an InlineInfo object from a binary data stream.
Definition: InlineInfo.cpp:222
Line entries are used to encode the line tables in FunctionInfo objects.
Definition: LineEntry.h:22
uint32_t File
1 based index of file in FileTable
Definition: LineEntry.h:24
uint32_t Line
Source line number.
Definition: LineEntry.h:25
uint64_t LookupAddr
The address that this lookup pertains to.
Definition: LookupResult.h:39
AddressRange FuncRange
The concrete function address range.
Definition: LookupResult.h:40
StringRef FuncName
The concrete function name that contains LookupAddr.
Definition: LookupResult.h:41
SourceLocations Locations
The source locations that match this address.
Definition: LookupResult.h:51
static llvm::Expected< MergedFunctionsInfo > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an MergedFunctionsInfo object from a binary data stream.
StringRef Base
Line entry source file basename.
Definition: LookupResult.h:24
uint32_t Line
Source file line number.
Definition: LookupResult.h:25
uint32_t Offset
Byte size offset within the named function.
Definition: LookupResult.h:26
StringRef Dir
Line entry source file directory path.
Definition: LookupResult.h:23
StringRef Name
Function or symbol name.
Definition: LookupResult.h:22