LLVM 22.0.0git
Symbolize.h
Go to the documentation of this file.
1//===- Symbolize.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// Header for LLVM symbolization library.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
14#define LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/ilist_node.h"
21#include "llvm/Object/Binary.h"
22#include "llvm/Object/BuildID.h"
24#include "llvm/Support/Error.h"
25#include <algorithm>
26#include <cstdint>
27#include <map>
28#include <memory>
29#include <optional>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace llvm {
35namespace object {
37class MachOObjectFile;
38class ObjectFile;
39struct SectionedAddress;
40} // namespace object
41
42namespace symbolize {
43
45
46using namespace object;
47
50
51class CachedBinary;
52
54public:
55 struct Options {
56 FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName;
57 FileLineInfoKind PathStyle = FileLineInfoKind::AbsoluteFilePath;
58 bool SkipLineZero = false;
59 bool UseSymbolTable = true;
60 bool Demangle = true;
61 bool RelativeAddresses = false;
62 bool UntagAddresses = false;
63 bool UseDIA = false;
64 bool DisableGsym = false;
65 std::string DefaultArch;
66 std::vector<std::string> DsymHints;
67 std::string FallbackDebugPath;
68 std::string DWPName;
69 std::vector<std::string> DebugFileDirectory;
70 std::vector<std::string> GsymFileDirectory;
71 size_t MaxCacheSize =
72 sizeof(size_t) == 4
73 ? 512 * 1024 * 1024 /* 512 MiB */
74 : static_cast<size_t>(4ULL * 1024 * 1024 * 1024) /* 4 GiB */;
75 };
76
78 LLVM_ABI LLVMSymbolizer(const Options &Opts);
79
81
82 // Overloads accepting ObjectFile does not support COFF currently
84 symbolizeCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
89 object::SectionedAddress ModuleOffset);
92 object::SectionedAddress ModuleOffset);
95 object::SectionedAddress ModuleOffset);
98 object::SectionedAddress ModuleOffset);
99
101 symbolizeData(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
106 object::SectionedAddress ModuleOffset);
108 symbolizeFrame(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
113 object::SectionedAddress ModuleOffset);
114
116 findSymbol(const ObjectFile &Obj, StringRef Symbol, uint64_t Offset);
121
122 LLVM_ABI void flush();
123
124 // Evict entries from the binary cache until it is under the maximum size
125 // given in the options. Calling this invalidates references in the DI...
126 // objects returned by the methods above.
127 LLVM_ABI void pruneCache();
128
129 LLVM_ABI static std::string
130 DemangleName(StringRef Name, const SymbolizableModule *DbiModuleDescriptor);
131
132 void setBuildIDFetcher(std::unique_ptr<BuildIDFetcher> Fetcher) {
133 BIDFetcher = std::move(Fetcher);
134 }
135
136 /// Returns a SymbolizableModule or an error if loading debug info failed.
137 /// Only one attempt is made to load a module, and errors during loading are
138 /// only reported once. Subsequent calls to get module info for a module that
139 /// failed to load will return nullptr.
142
143private:
144 // Bundles together object file with code/data and object file with
145 // corresponding debug info. These objects can be the same.
146 using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
147
148 template <typename T>
150 symbolizeCodeCommon(const T &ModuleSpecifier,
151 object::SectionedAddress ModuleOffset);
152 template <typename T>
154 symbolizeInlinedCodeCommon(const T &ModuleSpecifier,
155 object::SectionedAddress ModuleOffset);
156 template <typename T>
157 Expected<DIGlobal> symbolizeDataCommon(const T &ModuleSpecifier,
158 object::SectionedAddress ModuleOffset);
159 template <typename T>
161 symbolizeFrameCommon(const T &ModuleSpecifier,
162 object::SectionedAddress ModuleOffset);
163 template <typename T>
165 findSymbolCommon(const T &ModuleSpecifier, StringRef Symbol, uint64_t Offset);
166
168
169 /// Returns a SymbolizableModule or an error if loading debug info failed.
170 /// Unlike the above, errors are reported each time, since they are more
171 /// likely to be transient.
174
176 createModuleInfo(const ObjectFile *Obj, std::unique_ptr<DIContext> Context,
178
179 ObjectFile *lookUpDsymFile(const std::string &Path,
180 const MachOObjectFile *ExeObj,
181 const std::string &ArchName);
182 ObjectFile *lookUpDebuglinkObject(const std::string &Path,
183 const ObjectFile *Obj,
184 const std::string &ArchName);
185 ObjectFile *lookUpBuildIDObject(const std::string &Path,
186 const ELFObjectFileBase *Obj,
187 const std::string &ArchName);
188 std::string lookUpGsymFile(const std::string &Path);
189
190 bool findDebugBinary(const std::string &OrigPath,
191 const std::string &DebuglinkName, uint32_t CRCHash,
192 std::string &Result);
193
194 bool getOrFindDebugBinary(const ArrayRef<uint8_t> BuildID,
195 std::string &Result);
196
197 /// Returns pair of pointers to object and debug object.
198 Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
199 const std::string &ArchName);
200
201 /// Return a pointer to the object file with the specified name, for a
202 /// specified architecture (e.g. if path refers to a Mach-O universal
203 /// binary, only one object file from it will be returned).
204 Expected<ObjectFile *> getOrCreateObject(const std::string &InputPath,
205 const std::string &DefaultArchName);
206
207 /// Return a pointer to the object file with the specified name, for a
208 /// specified architecture that is present inside an archive file.
209 Expected<ObjectFile *> getOrCreateObjectFromArchive(StringRef ArchivePath,
210 StringRef MemberName,
211 StringRef ArchName,
212 StringRef FullPath);
213
214 /// Update the LRU cache order when a binary is accessed.
215 void recordAccess(CachedBinary &Bin);
216
217 std::map<std::string, std::unique_ptr<SymbolizableModule>, std::less<>>
218 Modules;
219 StringMap<std::string> BuildIDPaths;
220
221 /// Contains cached results of getOrCreateObjectPair().
222 std::map<std::pair<std::string, std::string>, ObjectPair>
223 ObjectPairForPathArch;
224
225 /// Contains parsed binary for each path, or parsing error.
226 std::map<std::string, CachedBinary, std::less<>> BinaryForPath;
227
228 /// Store the archive path for the object file.
230
231 /// A list of cached binaries in LRU order.
232 simple_ilist<CachedBinary> LRUBinaries;
233 /// Sum of the sizes of the cached binaries.
234 size_t CacheSize = 0;
235
236 struct ContainerCacheKey {
237 std::string Path;
238 std::string MemberName;
239 std::string ArchName;
240
241 // Required for map comparison.
242 bool operator<(const ContainerCacheKey &Other) const {
243 return std::tie(Path, MemberName, ArchName) <
244 std::tie(Other.Path, Other.MemberName, Other.ArchName);
245 }
246 };
247
248 /// Parsed object file for each path/member/architecture triple.
249 /// Used to cache objects extracted from containers (e.g., Mach-O
250 /// universal binaries, archives).
251 std::map<ContainerCacheKey, std::unique_ptr<ObjectFile>> ObjectFileCache;
252
253 Expected<object::Binary *>
254 loadOrGetBinary(const std::string &ArchivePathKey,
255 std::optional<StringRef> FullPathKey = std::nullopt);
256
257 Expected<ObjectFile *> findOrCacheObject(
258 const ContainerCacheKey &Key,
259 llvm::function_ref<Expected<std::unique_ptr<ObjectFile>>()> Loader,
260 const std::string &PathForBinaryCache);
261
262 Options Opts;
263
264 std::unique_ptr<BuildIDFetcher> BIDFetcher;
265};
266
267// A binary intrusively linked into a LRU cache list. If the binary is empty,
268// then the entry marks that an error occurred, and it is not part of the LRU
269// list.
270class CachedBinary : public ilist_node<CachedBinary> {
271public:
272 CachedBinary() = default;
274
277
278 // Add an action to be performed when the binary is evicted, before all
279 // previously registered evictors.
280 LLVM_ABI void pushEvictor(std::function<void()> Evictor);
281
282 // Run all registered evictors in the reverse of the order in which they were
283 // added.
284 void evict() {
285 if (Evictor)
286 Evictor();
287 }
288
289 size_t size() { return Bin.getBinary()->getData().size(); }
290
291private:
293 std::function<void()> Evictor;
294};
295
296} // end namespace symbolize
297} // end namespace llvm
298
299#endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
This file defines the StringMap class.
This file declares a library for handling Build IDs and using them to find debug info.
static cl::opt< unsigned > CacheSize("cdsort-cache-size", cl::ReallyHidden, cl::desc("The size of a line in the cache"))
#define LLVM_ABI
Definition Compiler.h:213
This file defines the DenseMap class.
static LVOptions Options
Definition LVOptions.cpp:25
#define T
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Tagged union holding either a T or a Error.
Definition Error.h:485
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
An efficient, type-erasing, non-owning reference to a callable.
This class is the base class for all object file types.
Definition ObjectFile.h:231
A simple intrusive list implementation.
CachedBinary(OwningBinary< Binary > Bin)
Definition Symbolize.h:273
OwningBinary< Binary > * operator->()
Definition Symbolize.h:276
LLVM_ABI void pushEvictor(std::function< void()> Evictor)
OwningBinary< Binary > & operator*()
Definition Symbolize.h:275
static LLVM_ABI std::string DemangleName(StringRef Name, const SymbolizableModule *DbiModuleDescriptor)
LLVM_ABI Expected< std::vector< DILineInfo > > findSymbol(const ObjectFile &Obj, StringRef Symbol, uint64_t Offset)
LLVM_ABI Expected< DIInliningInfo > symbolizeInlinedCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
LLVM_ABI Expected< DILineInfo > symbolizeCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
Definition Symbolize.cpp:86
LLVM_ABI Expected< DIGlobal > symbolizeData(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
LLVM_ABI Expected< std::vector< DILocal > > symbolizeFrame(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
LLVM_ABI Expected< SymbolizableModule * > getOrCreateModuleInfo(StringRef ModuleName)
Returns a SymbolizableModule or an error if loading debug info failed.
void setBuildIDFetcher(std::unique_ptr< BuildIDFetcher > Fetcher)
Definition Symbolize.h:132
This file defines the ilist_node class template, which is a convenient base class for creating classe...
bool operator<(const ELFSymbolRef &A, const ELFSymbolRef &B)
SmallVector< uint8_t, 10 > BuildID
A build ID in binary form.
Definition BuildID.h:26
DILineInfoSpecifier::FileLineInfoKind FileLineInfoKind
Definition Symbolize.h:49
DILineInfoSpecifier::FunctionNameKind FunctionNameKind
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
@ Other
Any other memory.
Definition ModRef.h:68
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1879
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
std::vector< std::string > GsymFileDirectory
Definition Symbolize.h:70
std::vector< std::string > DebugFileDirectory
Definition Symbolize.h:69
std::vector< std::string > DsymHints
Definition Symbolize.h:66