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.
27};
28
30 OS << FI.Range << ": " << "Name=" << HEX32(FI.Name) << '\n';
31 if (FI.OptLineTable)
32 OS << FI.OptLineTable << '\n';
33 if (FI.Inline)
34 OS << FI.Inline << '\n';
35 return OS;
36}
37
39 uint64_t BaseAddr) {
40 FunctionInfo FI;
41 uint64_t Offset = 0;
42 if (!Data.isValidOffsetForDataOfSize(Offset, 4))
43 return createStringError(std::errc::io_error,
44 "0x%8.8" PRIx64 ": missing FunctionInfo Size", Offset);
45 FI.Range = {BaseAddr, BaseAddr + Data.getU32(&Offset)};
46 if (!Data.isValidOffsetForDataOfSize(Offset, 4))
47 return createStringError(std::errc::io_error,
48 "0x%8.8" PRIx64 ": missing FunctionInfo Name", Offset);
49 FI.Name = Data.getU32(&Offset);
50 if (FI.Name == 0)
51 return createStringError(std::errc::io_error,
52 "0x%8.8" PRIx64 ": invalid FunctionInfo Name value 0x%8.8x",
53 Offset - 4, FI.Name);
54 bool Done = false;
55 while (!Done) {
56 if (!Data.isValidOffsetForDataOfSize(Offset, 4))
57 return createStringError(std::errc::io_error,
58 "0x%8.8" PRIx64 ": missing FunctionInfo InfoType value", Offset);
59 const uint32_t IT = Data.getU32(&Offset);
60 if (!Data.isValidOffsetForDataOfSize(Offset, 4))
61 return createStringError(std::errc::io_error,
62 "0x%8.8" PRIx64 ": missing FunctionInfo InfoType length", Offset);
63 const uint32_t InfoLength = Data.getU32(&Offset);
64 if (!Data.isValidOffsetForDataOfSize(Offset, InfoLength))
65 return createStringError(std::errc::io_error,
66 "0x%8.8" PRIx64 ": missing FunctionInfo data for InfoType %u",
67 Offset, IT);
68 DataExtractor InfoData(Data.getData().substr(Offset, InfoLength),
69 Data.isLittleEndian(),
70 Data.getAddressSize());
71 switch (IT) {
72 case InfoType::EndOfList:
73 Done = true;
74 break;
75
76 case InfoType::LineTableInfo:
77 if (Expected<LineTable> LT = LineTable::decode(InfoData, BaseAddr))
78 FI.OptLineTable = std::move(LT.get());
79 else
80 return LT.takeError();
81 break;
82
83 case InfoType::InlineInfo:
84 if (Expected<InlineInfo> II = InlineInfo::decode(InfoData, BaseAddr))
85 FI.Inline = std::move(II.get());
86 else
87 return II.takeError();
88 break;
89
90 case InfoType::MergedFunctionsInfo:
92 MergedFunctionsInfo::decode(InfoData, BaseAddr))
93 FI.MergedFunctions = std::move(MI.get());
94 else
95 return MI.takeError();
96 break;
97
98 default:
99 return createStringError(std::errc::io_error,
100 "0x%8.8" PRIx64 ": unsupported InfoType %u",
101 Offset-8, IT);
102 }
103 Offset += InfoLength;
104 }
105 return std::move(FI);
106}
107
110 if (!isValid())
111 return 0;
114 llvm::Expected<uint64_t> Result = encode(FW);
115 if (!Result) {
117 consumeError(Result.takeError());
118 return 0;
119 }
120 return EncodingCache.size();
121}
122
124 bool NoPadding) const {
125 if (!isValid())
126 return createStringError(std::errc::invalid_argument,
127 "attempted to encode invalid FunctionInfo object");
128 // Align FunctionInfo data to a 4 byte alignment, if padding is allowed
129 if (NoPadding == false)
130 Out.alignTo(4);
131 const uint64_t FuncInfoOffset = Out.tell();
132 // Check if we have already encoded this function info into EncodingCache.
133 // This will be non empty when creating segmented GSYM files as we need to
134 // precompute exactly how big FunctionInfo objects encode into so we can
135 // accurately make segments of a specific size.
136 if (!EncodingCache.empty() &&
138 // We already encoded this object, just write out the bytes.
141 return FuncInfoOffset;
142 }
143 // Write the size in bytes of this function as a uint32_t. This can be zero
144 // if we just have a symbol from a symbol table and that symbol has no size.
145 Out.writeU32(size());
146 // Write the name of this function as a uint32_t string table offset.
147 Out.writeU32(Name);
148
149 if (OptLineTable) {
150 Out.writeU32(InfoType::LineTableInfo);
151 // Write a uint32_t length as zero for now, we will fix this up after
152 // writing the LineTable out with the number of bytes that were written.
153 Out.writeU32(0);
154 const auto StartOffset = Out.tell();
155 llvm::Error err = OptLineTable->encode(Out, Range.start());
156 if (err)
157 return std::move(err);
158 const auto Length = Out.tell() - StartOffset;
159 if (Length > UINT32_MAX)
160 return createStringError(std::errc::invalid_argument,
161 "LineTable length is greater than UINT32_MAX");
162 // Fixup the size of the LineTable data with the correct size.
163 Out.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
164 }
165
166 // Write out the inline function info if we have any and if it is valid.
167 if (Inline) {
168 Out.writeU32(InfoType::InlineInfo);
169 // Write a uint32_t length as zero for now, we will fix this up after
170 // writing the LineTable out with the number of bytes that were written.
171 Out.writeU32(0);
172 const auto StartOffset = Out.tell();
173 llvm::Error err = Inline->encode(Out, Range.start());
174 if (err)
175 return std::move(err);
176 const auto Length = Out.tell() - StartOffset;
177 if (Length > UINT32_MAX)
178 return createStringError(std::errc::invalid_argument,
179 "InlineInfo length is greater than UINT32_MAX");
180 // Fixup the size of the InlineInfo data with the correct size.
181 Out.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
182 }
183
184 // Write out the merged functions info if we have any and if it is valid.
185 if (MergedFunctions) {
186 Out.writeU32(InfoType::MergedFunctionsInfo);
187 // Write a uint32_t length as zero for now, we will fix this up after
188 // writing the LineTable out with the number of bytes that were written.
189 Out.writeU32(0);
190 const auto StartOffset = Out.tell();
191 llvm::Error err = MergedFunctions->encode(Out);
192 if (err)
193 return std::move(err);
194 const auto Length = Out.tell() - StartOffset;
195 if (Length > UINT32_MAX)
196 return createStringError(
197 std::errc::invalid_argument,
198 "MergedFunctionsInfo length is greater than UINT32_MAX");
199 // Fixup the size of the MergedFunctionsInfo data with the correct size.
200 Out.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
201 }
202
203 // Terminate the data chunks with and end of list with zero size
204 Out.writeU32(InfoType::EndOfList);
205 Out.writeU32(0);
206 return FuncInfoOffset;
207}
208
210 const GsymReader &GR,
211 uint64_t FuncAddr,
212 uint64_t Addr) {
213 LookupResult LR;
214 LR.LookupAddr = Addr;
215 uint64_t Offset = 0;
216 LR.FuncRange = {FuncAddr, FuncAddr + Data.getU32(&Offset)};
217 uint32_t NameOffset = Data.getU32(&Offset);
218 // The "lookup" functions doesn't report errors as accurately as the "decode"
219 // function as it is meant to be fast. For more accurage errors we could call
220 // "decode".
221 if (!Data.isValidOffset(Offset))
222 return createStringError(std::errc::io_error,
223 "FunctionInfo data is truncated");
224 // This function will be called with the result of a binary search of the
225 // address table, we must still make sure the address does not fall into a
226 // gap between functions are after the last function.
227 if (LR.FuncRange.size() > 0 && !LR.FuncRange.contains(Addr))
228 return createStringError(std::errc::io_error,
229 "address 0x%" PRIx64 " is not in GSYM", Addr);
230
231 if (NameOffset == 0)
232 return createStringError(std::errc::io_error,
233 "0x%8.8" PRIx64 ": invalid FunctionInfo Name value 0x00000000",
234 Offset - 4);
235 LR.FuncName = GR.getString(NameOffset);
236 bool Done = false;
237 std::optional<LineEntry> LineEntry;
238 std::optional<DataExtractor> InlineInfoData;
239 while (!Done) {
240 if (!Data.isValidOffsetForDataOfSize(Offset, 8))
241 return createStringError(std::errc::io_error,
242 "FunctionInfo data is truncated");
243 const uint32_t IT = Data.getU32(&Offset);
244 const uint32_t InfoLength = Data.getU32(&Offset);
245 const StringRef InfoBytes = Data.getData().substr(Offset, InfoLength);
246 if (InfoLength != InfoBytes.size())
247 return createStringError(std::errc::io_error,
248 "FunctionInfo data is truncated");
249 DataExtractor InfoData(InfoBytes, Data.isLittleEndian(),
250 Data.getAddressSize());
251 switch (IT) {
252 case InfoType::EndOfList:
253 Done = true;
254 break;
255
256 case InfoType::LineTableInfo:
257 if (auto ExpectedLE = LineTable::lookup(InfoData, FuncAddr, Addr))
258 LineEntry = ExpectedLE.get();
259 else
260 return ExpectedLE.takeError();
261 break;
262
263 case InfoType::InlineInfo:
264 // We will parse the inline info after our line table, but only if
265 // we have a line entry.
266 InlineInfoData = InfoData;
267 break;
268
269 default:
270 break;
271 }
272 Offset += InfoLength;
273 }
274
275 if (!LineEntry) {
276 // We don't have a valid line entry for our address, fill in our source
277 // location as best we can and return.
278 SourceLocation SrcLoc;
279 SrcLoc.Name = LR.FuncName;
280 SrcLoc.Offset = Addr - FuncAddr;
281 LR.Locations.push_back(SrcLoc);
282 return LR;
283 }
284
285 std::optional<FileEntry> LineEntryFile = GR.getFile(LineEntry->File);
286 if (!LineEntryFile)
287 return createStringError(std::errc::invalid_argument,
288 "failed to extract file[%" PRIu32 "]",
289 LineEntry->File);
290
291 SourceLocation SrcLoc;
292 SrcLoc.Name = LR.FuncName;
293 SrcLoc.Offset = Addr - FuncAddr;
294 SrcLoc.Dir = GR.getString(LineEntryFile->Dir);
295 SrcLoc.Base = GR.getString(LineEntryFile->Base);
296 SrcLoc.Line = LineEntry->Line;
297 LR.Locations.push_back(SrcLoc);
298 // If we don't have inline information, we are done.
299 if (!InlineInfoData)
300 return LR;
301 // We have inline information. Try to augment the lookup result with this
302 // data.
303 llvm::Error Err = InlineInfo::lookup(GR, *InlineInfoData, FuncAddr, Addr,
304 LR.Locations);
305 if (Err)
306 return std::move(Err);
307 return LR;
308}
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:94
size_t size() const
Definition: SmallVector.h:91
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
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 FunctionInfo &R)
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:1286
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
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
bool isValid() const
Query if a FunctionInfo object is valid.
Definition: FunctionInfo.h:121
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
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:160
static llvm::Expected< InlineInfo > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an InlineInfo object from a binary data stream.
Definition: InlineInfo.cpp:223
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