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