LLVM 23.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::string PDBName;
70 std::vector<std::string> DebugFileDirectory;
71 std::vector<std::string> GsymFileDirectory;
72 size_t MaxCacheSize =
73 sizeof(size_t) == 4
74 ? 512 * 1024 * 1024 /* 512 MiB */
75 : static_cast<size_t>(4ULL * 1024 * 1024 * 1024) /* 4 GiB */;
76 };
77
79 LLVM_ABI LLVMSymbolizer(const Options &Opts);
80
82
83 // Overloads accepting ObjectFile does not support COFF currently
85 symbolizeCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
90 object::SectionedAddress ModuleOffset);
93 object::SectionedAddress ModuleOffset);
96 object::SectionedAddress ModuleOffset);
99 object::SectionedAddress ModuleOffset);
100
102 symbolizeData(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
107 object::SectionedAddress ModuleOffset);
109 symbolizeFrame(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
114 object::SectionedAddress ModuleOffset);
115
117 findSymbol(const ObjectFile &Obj, StringRef Symbol, uint64_t Offset);
122
123 LLVM_ABI void flush();
124
125 // Evict entries from the binary cache until it is under the maximum size
126 // given in the options. Calling this invalidates references in the DI...
127 // objects returned by the methods above.
128 LLVM_ABI void pruneCache();
129
130 LLVM_ABI static std::string
131 DemangleName(StringRef Name, const SymbolizableModule *DbiModuleDescriptor);
132
133 void setBuildIDFetcher(std::unique_ptr<BuildIDFetcher> Fetcher) {
134 BIDFetcher = std::move(Fetcher);
135 }
136
137 /// Returns a SymbolizableModule or an error if loading debug info failed.
138 /// Only one attempt is made to load a module, and errors during loading are
139 /// only reported once. Subsequent calls to get module info for a module that
140 /// failed to load will return nullptr.
143
144private:
145 // Bundles together object file with code/data and object file with
146 // corresponding debug info. These objects can be the same.
147 using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
148
149 template <typename T>
151 symbolizeCodeCommon(const T &ModuleSpecifier,
152 object::SectionedAddress ModuleOffset);
153 template <typename T>
155 symbolizeInlinedCodeCommon(const T &ModuleSpecifier,
156 object::SectionedAddress ModuleOffset);
157 template <typename T>
158 Expected<DIGlobal> symbolizeDataCommon(const T &ModuleSpecifier,
159 object::SectionedAddress ModuleOffset);
160 template <typename T>
162 symbolizeFrameCommon(const T &ModuleSpecifier,
163 object::SectionedAddress ModuleOffset);
164 template <typename T>
166 findSymbolCommon(const T &ModuleSpecifier, StringRef Symbol, uint64_t Offset);
167
169
170 /// Returns a SymbolizableModule or an error if loading debug info failed.
171 /// Unlike the above, errors are reported each time, since they are more
172 /// likely to be transient.
175
177 createModuleInfo(const ObjectFile *Obj, std::unique_ptr<DIContext> Context,
179
180 ObjectFile *lookUpDsymFile(const std::string &Path,
181 const MachOObjectFile *ExeObj,
182 const std::string &ArchName);
183 ObjectFile *lookUpDebuglinkObject(const std::string &Path,
184 const ObjectFile *Obj,
185 const std::string &ArchName);
186 ObjectFile *lookUpBuildIDObject(const std::string &Path,
187 const ELFObjectFileBase *Obj,
188 const std::string &ArchName);
189 std::string lookUpGsymFile(const std::string &Path);
190
191 bool findDebugBinary(const std::string &OrigPath,
192 const std::string &DebuglinkName, uint32_t CRCHash,
193 std::string &Result);
194
195 bool getOrFindDebugBinary(const ArrayRef<uint8_t> BuildID,
196 std::string &Result);
197
198 /// Returns pair of pointers to object and debug object.
199 Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
200 const std::string &ArchName);
201
202 /// Return a pointer to the object file with the specified name, for a
203 /// specified architecture (e.g. if path refers to a Mach-O universal
204 /// binary, only one object file from it will be returned).
205 Expected<ObjectFile *> getOrCreateObject(const std::string &InputPath,
206 const std::string &DefaultArchName);
207
208 /// Return a pointer to the object file with the specified name, for a
209 /// specified architecture that is present inside an archive file.
210 Expected<ObjectFile *> getOrCreateObjectFromArchive(StringRef ArchivePath,
211 StringRef MemberName,
212 StringRef ArchName,
213 StringRef FullPath);
214
215 /// Update the LRU cache order when a binary is accessed.
216 void recordAccess(CachedBinary &Bin);
217
218 std::map<std::string, std::unique_ptr<SymbolizableModule>, std::less<>>
219 Modules;
220 StringMap<std::string> BuildIDPaths;
221
222 /// Contains cached results of getOrCreateObjectPair().
223 std::map<std::pair<std::string, std::string>, ObjectPair>
224 ObjectPairForPathArch;
225
226 /// Contains parsed binary for each path, or parsing error.
227 std::map<std::string, CachedBinary, std::less<>> BinaryForPath;
228
229 /// Store the archive path for the object file.
231
232 /// A list of cached binaries in LRU order.
233 simple_ilist<CachedBinary> LRUBinaries;
234 /// Sum of the sizes of the cached binaries.
235 size_t CacheSize = 0;
236
237 struct ContainerCacheKey {
238 std::string Path;
239 std::string MemberName;
240 std::string ArchName;
241
242 // Required for map comparison.
243 bool operator<(const ContainerCacheKey &Other) const {
244 return std::tie(Path, MemberName, ArchName) <
245 std::tie(Other.Path, Other.MemberName, Other.ArchName);
246 }
247 };
248
249 /// Parsed object file for each path/member/architecture triple.
250 /// Used to cache objects extracted from containers (e.g., Mach-O
251 /// universal binaries, archives).
252 std::map<ContainerCacheKey, std::unique_ptr<ObjectFile>> ObjectFileCache;
253
254 Expected<object::Binary *>
255 loadOrGetBinary(const std::string &ArchivePathKey,
256 std::optional<StringRef> FullPathKey = std::nullopt);
257
258 Expected<ObjectFile *> findOrCacheObject(
259 const ContainerCacheKey &Key,
260 llvm::function_ref<Expected<std::unique_ptr<ObjectFile>>()> Loader,
261 const std::string &PathForBinaryCache);
262
263 Options Opts;
264
265 std::unique_ptr<BuildIDFetcher> BIDFetcher;
266};
267
268// A binary intrusively linked into a LRU cache list. If the binary is empty,
269// then the entry marks that an error occurred, and it is not part of the LRU
270// list.
271class CachedBinary : public ilist_node<CachedBinary> {
272public:
273 CachedBinary() = default;
275
278
279 // Add an action to be performed when the binary is evicted, before all
280 // previously registered evictors.
281 LLVM_ABI void pushEvictor(std::function<void()> Evictor);
282
283 // Run all registered evictors in the reverse of the order in which they were
284 // added.
285 void evict() {
286 if (Evictor)
287 Evictor();
288 }
289
290 size_t size() { return Bin.getBinary()->getData().size(); }
291
292private:
294 std::function<void()> Evictor;
295};
296
297} // end namespace symbolize
298} // end namespace llvm
299
300#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:215
This file defines the DenseMap class.
static LVOptions Options
Definition LVOptions.cpp:25
#define T
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:128
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:274
OwningBinary< Binary > * operator->()
Definition Symbolize.h:277
LLVM_ABI void pushEvictor(std::function< void()> Evictor)
OwningBinary< Binary > & operator*()
Definition Symbolize.h:276
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:133
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:558
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:1916
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860
std::vector< std::string > GsymFileDirectory
Definition Symbolize.h:71
std::vector< std::string > DebugFileDirectory
Definition Symbolize.h:70
std::vector< std::string > DsymHints
Definition Symbolize.h:66