LLVM 20.0.0git
InlineInfo.cpp
Go to the documentation of this file.
1//===- InlineInfo.cpp -------------------------------------------*- 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
14#include <inttypes.h>
15
16using namespace llvm;
17using namespace gsym;
18
19
21 if (!II.isValid())
22 return OS;
23 bool First = true;
24 for (auto Range : II.Ranges) {
25 if (First)
26 First = false;
27 else
28 OS << ' ';
29 OS << Range;
30 }
31 OS << " Name = " << HEX32(II.Name) << ", CallFile = " << II.CallFile
32 << ", CallLine = " << II.CallFile << '\n';
33 for (const auto &Child : II.Children)
34 OS << Child;
35 return OS;
36}
37
39 std::vector<const InlineInfo *> &InlineStack) {
40 if (II.Ranges.contains(Addr)) {
41 // If this is the top level that represents the concrete function,
42 // there will be no name and we shoud clear the inline stack. Otherwise
43 // we have found an inline call stack that we need to insert.
44 if (II.Name != 0)
45 InlineStack.insert(InlineStack.begin(), &II);
46 for (const auto &Child : II.Children) {
47 if (::getInlineStackHelper(Child, Addr, InlineStack))
48 break;
49 }
50 return !InlineStack.empty();
51 }
52 return false;
53}
54
55std::optional<InlineInfo::InlineArray>
57 InlineArray Result;
58 if (getInlineStackHelper(*this, Addr, Result))
59 return Result;
60 return std::nullopt;
61}
62
63/// Skip an InlineInfo object in the specified data at the specified offset.
64///
65/// Used during the InlineInfo::lookup() call to quickly skip child InlineInfo
66/// objects where the addres ranges isn't contained in the InlineInfo object
67/// or its children. This avoids allocations by not appending child InlineInfo
68/// objects to the InlineInfo::Children array.
69///
70/// \param Data The binary stream to read the data from.
71///
72/// \param Offset The byte offset within \a Data.
73///
74/// \param SkippedRanges If true, address ranges have already been skipped.
75
76static bool skip(DataExtractor &Data, uint64_t &Offset, bool SkippedRanges) {
77 if (!SkippedRanges) {
78 if (skipRanges(Data, Offset) == 0)
79 return false;
80 }
81 bool HasChildren = Data.getU8(&Offset) != 0;
82 Data.getU32(&Offset); // Skip Inline.Name.
83 Data.getULEB128(&Offset); // Skip Inline.CallFile.
84 Data.getULEB128(&Offset); // Skip Inline.CallLine.
85 if (HasChildren) {
86 while (skip(Data, Offset, false /* SkippedRanges */))
87 /* Do nothing */;
88 }
89 // We skipped a valid InlineInfo.
90 return true;
91}
92
93/// A Lookup helper functions.
94///
95/// Used during the InlineInfo::lookup() call to quickly only parse an
96/// InlineInfo object if the address falls within this object. This avoids
97/// allocations by not appending child InlineInfo objects to the
98/// InlineInfo::Children array and also skips any InlineInfo objects that do
99/// not contain the address we are looking up.
100///
101/// \param Data The binary stream to read the data from.
102///
103/// \param Offset The byte offset within \a Data.
104///
105/// \param BaseAddr The address that the relative address range offsets are
106/// relative to.
107
109 uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs,
110 llvm::Error &Err) {
111 InlineInfo Inline;
112 decodeRanges(Inline.Ranges, Data, BaseAddr, Offset);
113 if (Inline.Ranges.empty())
114 return true;
115 // Check if the address is contained within the inline information, and if
116 // not, quickly skip this InlineInfo object and all its children.
117 if (!Inline.Ranges.contains(Addr)) {
118 skip(Data, Offset, true /* SkippedRanges */);
119 return false;
120 }
121
122 // The address range is contained within this InlineInfo, add the source
123 // location for this InlineInfo and any children that contain the address.
124 bool HasChildren = Data.getU8(&Offset) != 0;
125 Inline.Name = Data.getU32(&Offset);
126 Inline.CallFile = (uint32_t)Data.getULEB128(&Offset);
127 Inline.CallLine = (uint32_t)Data.getULEB128(&Offset);
128 if (HasChildren) {
129 // Child address ranges are encoded relative to the first address in the
130 // parent InlineInfo object.
131 const auto ChildBaseAddr = Inline.Ranges[0].start();
132 bool Done = false;
133 while (!Done)
134 Done = lookup(GR, Data, Offset, ChildBaseAddr, Addr, SrcLocs, Err);
135 }
136
137 std::optional<FileEntry> CallFile = GR.getFile(Inline.CallFile);
138 if (!CallFile) {
139 Err = createStringError(std::errc::invalid_argument,
140 "failed to extract file[%" PRIu32 "]",
141 Inline.CallFile);
142 return false;
143 }
144
145 if (CallFile->Dir || CallFile->Base) {
146 SourceLocation SrcLoc;
147 SrcLoc.Name = SrcLocs.back().Name;
148 SrcLoc.Offset = SrcLocs.back().Offset;
149 SrcLoc.Dir = GR.getString(CallFile->Dir);
150 SrcLoc.Base = GR.getString(CallFile->Base);
151 SrcLoc.Line = Inline.CallLine;
152 SrcLocs.back().Name = GR.getString(Inline.Name);
153 SrcLocs.back().Offset = Addr - Inline.Ranges[0].start();
154 SrcLocs.push_back(SrcLoc);
155 }
156 return true;
157}
158
160 uint64_t BaseAddr, uint64_t Addr,
161 SourceLocations &SrcLocs) {
162 // Call our recursive helper function starting at offset zero.
163 uint64_t Offset = 0;
165 ::lookup(GR, Data, Offset, BaseAddr, Addr, SrcLocs, Err);
166 return Err;
167}
168
169/// Decode an InlineInfo in Data at the specified offset.
170///
171/// A local helper function to decode InlineInfo objects. This function is
172/// called recursively when parsing child InlineInfo objects.
173///
174/// \param Data The data extractor to decode from.
175/// \param Offset The offset within \a Data to decode from.
176/// \param BaseAddr The base address to use when decoding address ranges.
177/// \returns An InlineInfo or an error describing the issue that was
178/// encountered during decoding.
180 uint64_t BaseAddr) {
181 InlineInfo Inline;
182 if (!Data.isValidOffset(Offset))
183 return createStringError(std::errc::io_error,
184 "0x%8.8" PRIx64 ": missing InlineInfo address ranges data", Offset);
185 decodeRanges(Inline.Ranges, Data, BaseAddr, Offset);
186 if (Inline.Ranges.empty())
187 return Inline;
188 if (!Data.isValidOffsetForDataOfSize(Offset, 1))
189 return createStringError(std::errc::io_error,
190 "0x%8.8" PRIx64 ": missing InlineInfo uint8_t indicating children",
191 Offset);
192 bool HasChildren = Data.getU8(&Offset) != 0;
193 if (!Data.isValidOffsetForDataOfSize(Offset, 4))
194 return createStringError(std::errc::io_error,
195 "0x%8.8" PRIx64 ": missing InlineInfo uint32_t for name", Offset);
196 Inline.Name = Data.getU32(&Offset);
197 if (!Data.isValidOffset(Offset))
198 return createStringError(std::errc::io_error,
199 "0x%8.8" PRIx64 ": missing ULEB128 for InlineInfo call file", Offset);
200 Inline.CallFile = (uint32_t)Data.getULEB128(&Offset);
201 if (!Data.isValidOffset(Offset))
202 return createStringError(std::errc::io_error,
203 "0x%8.8" PRIx64 ": missing ULEB128 for InlineInfo call line", Offset);
204 Inline.CallLine = (uint32_t)Data.getULEB128(&Offset);
205 if (HasChildren) {
206 // Child address ranges are encoded relative to the first address in the
207 // parent InlineInfo object.
208 const auto ChildBaseAddr = Inline.Ranges[0].start();
209 while (true) {
210 llvm::Expected<InlineInfo> Child = decode(Data, Offset, ChildBaseAddr);
211 if (!Child)
212 return Child.takeError();
213 // InlineInfo with empty Ranges termintes a child sibling chain.
214 if (Child.get().Ranges.empty())
215 break;
216 Inline.Children.emplace_back(std::move(*Child));
217 }
218 }
219 return Inline;
220}
221
223 uint64_t BaseAddr) {
224 uint64_t Offset = 0;
225 return ::decode(Data, Offset, BaseAddr);
226}
227
229 // Users must verify the InlineInfo is valid prior to calling this funtion.
230 // We don't want to emit any InlineInfo objects if they are not valid since
231 // it will waste space in the GSYM file.
232 if (!isValid())
233 return createStringError(std::errc::invalid_argument,
234 "attempted to encode invalid InlineInfo object");
235 encodeRanges(Ranges, O, BaseAddr);
236 bool HasChildren = !Children.empty();
237 O.writeU8(HasChildren);
238 O.writeU32(Name);
239 O.writeULEB(CallFile);
240 O.writeULEB(CallLine);
241 if (HasChildren) {
242 // Child address ranges are encoded as relative to the first
243 // address in the Ranges for this object. This keeps the offsets
244 // small and allows for efficient encoding using ULEB offsets.
245 const uint64_t ChildBaseAddr = Ranges[0].start();
246 for (const auto &Child : Children) {
247 // Make sure all child address ranges are contained in the parent address
248 // ranges.
249 for (const auto &ChildRange: Child.Ranges) {
250 if (!Ranges.contains(ChildRange))
251 return createStringError(std::errc::invalid_argument,
252 "child range not contained in parent");
253 }
254 llvm::Error Err = Child.encode(O, ChildBaseAddr);
255 if (Err)
256 return Err;
257 }
258
259 // Terminate child sibling chain by emitting a zero. This zero will cause
260 // the decodeAll() function above to return false and stop the decoding
261 // of child InlineInfo objects that are siblings.
262 O.writeULEB(0);
263 }
264 return Error::success();
265}
266
268 uint64_t NumChildren = II.Children.size();
269 for (const auto &Child : II.Children)
270 NumChildren += GetTotalNumChildren(Child);
271 return NumChildren;
272}
273
274bool InlineInfo::operator<(const InlineInfo &RHS) const {
276}
uint64_t Addr
#define HEX32(v)
Definition: ExtractRanges.h:19
static bool lookup(const GsymReader &GR, DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
Definition: InlineInfo.cpp:108
static bool getInlineStackHelper(const InlineInfo &II, uint64_t Addr, std::vector< const InlineInfo * > &InlineStack)
Definition: InlineInfo.cpp:38
static bool skip(DataExtractor &Data, uint64_t &Offset, bool SkippedRanges)
Skip an InlineInfo object in the specified data at the specified offset.
Definition: InlineInfo.cpp:76
static uint64_t GetTotalNumChildren(const InlineInfo &II)
Definition: InlineInfo.cpp:267
static llvm::Expected< InlineInfo > decode(DataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr)
Decode an InlineInfo in Data at the specified offset.
Definition: InlineInfo.cpp:179
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
raw_pwrite_stream & OS
Value * RHS
bool contains(uint64_t Addr) const
Definition: AddressRanges.h:66
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
Error takeError()
Take ownership of the stored error.
Definition: Error.h:608
reference get()
Returns a reference to the stored T value.
Definition: Error.h:578
char back() const
back - Get the last character in the string.
Definition: StringRef.h:159
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
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
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
void encodeRanges(const AddressRanges &Ranges, FileWriter &O, uint64_t BaseAddr)
raw_ostream & operator<<(raw_ostream &OS, const CallSiteInfo &CSI)
void decodeRanges(AddressRanges &Ranges, DataExtractor &Data, uint64_t BaseAddr, uint64_t &Offset)
Address ranges are decoded and encoded to be relative to a base address.
uint64_t skipRanges(DataExtractor &Data, uint64_t &Offset)
Skip an address range object in the specified data a the specified offset.
std::vector< SourceLocation > SourceLocations
Definition: LookupResult.h:36
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
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
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Inline information stores the name of the inline function along with an array of address ranges.
Definition: InlineInfo.h:59
bool operator<(const InlineInfo &RHS) const
Compare InlineInfo objects.
Definition: InlineInfo.cpp:274
std::vector< InlineInfo > Children
Definition: InlineInfo.h:65
AddressRanges Ranges
Definition: InlineInfo.h:64
std::vector< const InlineInfo * > InlineArray
Definition: InlineInfo.h:76
std::optional< InlineArray > getInlineStack(uint64_t Addr) const
Lookup an address in the InlineInfo object.
Definition: InlineInfo.cpp:56
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
uint32_t CallFile
1 based file index in the file table.
Definition: InlineInfo.h:62
bool isValid() const
Definition: InlineInfo.h:74
llvm::Error encode(FileWriter &O, uint64_t BaseAddr) const
Encode this InlineInfo object into FileWriter stream.
Definition: InlineInfo.cpp:228
uint32_t CallLine
Source line number.
Definition: InlineInfo.h:63
static llvm::Expected< InlineInfo > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an InlineInfo object from a binary data stream.
Definition: InlineInfo.cpp:222
uint32_t Name
String table offset in the string table.
Definition: InlineInfo.h:61
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