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/StringMap.h"
17#include "llvm/ADT/ilist_node.h"
20#include "llvm/Object/Binary.h"
21#include "llvm/Object/BuildID.h"
23#include "llvm/Support/Error.h"
24#include <algorithm>
25#include <cstdint>
26#include <map>
27#include <memory>
28#include <string>
29#include <utility>
30#include <vector>
31
32namespace llvm {
33namespace object {
34class ELFObjectFileBase;
35class MachOObjectFile;
36class ObjectFile;
37struct SectionedAddress;
38} // namespace object
39
40namespace symbolize {
41
42class SymbolizableModule;
43
44using namespace object;
45
48
49class CachedBinary;
50
52public:
53 struct Options {
54 FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName;
55 FileLineInfoKind PathStyle = FileLineInfoKind::AbsoluteFilePath;
56 bool SkipLineZero = false;
57 bool UseSymbolTable = true;
58 bool Demangle = true;
59 bool RelativeAddresses = false;
60 bool UntagAddresses = false;
61 bool UseDIA = false;
62 bool DisableGsym = false;
63 std::string DefaultArch;
64 std::vector<std::string> DsymHints;
65 std::string FallbackDebugPath;
66 std::string DWPName;
67 std::vector<std::string> DebugFileDirectory;
68 std::vector<std::string> GsymFileDirectory;
69 size_t MaxCacheSize =
70 sizeof(size_t) == 4
71 ? 512 * 1024 * 1024 /* 512 MiB */
72 : static_cast<size_t>(4ULL * 1024 * 1024 * 1024) /* 4 GiB */;
73 };
74
76 LLVM_ABI LLVMSymbolizer(const Options &Opts);
77
79
80 // Overloads accepting ObjectFile does not support COFF currently
82 symbolizeCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
87 object::SectionedAddress ModuleOffset);
90 object::SectionedAddress ModuleOffset);
93 object::SectionedAddress ModuleOffset);
96 object::SectionedAddress ModuleOffset);
97
99 symbolizeData(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
104 object::SectionedAddress ModuleOffset);
106 symbolizeFrame(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
111 object::SectionedAddress ModuleOffset);
112
114 findSymbol(const ObjectFile &Obj, StringRef Symbol, uint64_t Offset);
119
120 LLVM_ABI void flush();
121
122 // Evict entries from the binary cache until it is under the maximum size
123 // given in the options. Calling this invalidates references in the DI...
124 // objects returned by the methods above.
125 LLVM_ABI void pruneCache();
126
127 LLVM_ABI static std::string
128 DemangleName(StringRef Name, const SymbolizableModule *DbiModuleDescriptor);
129
130 void setBuildIDFetcher(std::unique_ptr<BuildIDFetcher> Fetcher) {
131 BIDFetcher = std::move(Fetcher);
132 }
133
134 /// Returns a SymbolizableModule or an error if loading debug info failed.
135 /// Only one attempt is made to load a module, and errors during loading are
136 /// only reported once. Subsequent calls to get module info for a module that
137 /// failed to load will return nullptr.
140
141private:
142 // Bundles together object file with code/data and object file with
143 // corresponding debug info. These objects can be the same.
144 using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
145
146 template <typename T>
148 symbolizeCodeCommon(const T &ModuleSpecifier,
149 object::SectionedAddress ModuleOffset);
150 template <typename T>
152 symbolizeInlinedCodeCommon(const T &ModuleSpecifier,
153 object::SectionedAddress ModuleOffset);
154 template <typename T>
155 Expected<DIGlobal> symbolizeDataCommon(const T &ModuleSpecifier,
156 object::SectionedAddress ModuleOffset);
157 template <typename T>
159 symbolizeFrameCommon(const T &ModuleSpecifier,
160 object::SectionedAddress ModuleOffset);
161 template <typename T>
163 findSymbolCommon(const T &ModuleSpecifier, StringRef Symbol, uint64_t Offset);
164
166
167 /// Returns a SymbolizableModule or an error if loading debug info failed.
168 /// Unlike the above, errors are reported each time, since they are more
169 /// likely to be transient.
172
174 createModuleInfo(const ObjectFile *Obj, std::unique_ptr<DIContext> Context,
176
177 ObjectFile *lookUpDsymFile(const std::string &Path,
178 const MachOObjectFile *ExeObj,
179 const std::string &ArchName);
180 ObjectFile *lookUpDebuglinkObject(const std::string &Path,
181 const ObjectFile *Obj,
182 const std::string &ArchName);
183 ObjectFile *lookUpBuildIDObject(const std::string &Path,
184 const ELFObjectFileBase *Obj,
185 const std::string &ArchName);
186 std::string lookUpGsymFile(const std::string &Path);
187
188 bool findDebugBinary(const std::string &OrigPath,
189 const std::string &DebuglinkName, uint32_t CRCHash,
190 std::string &Result);
191
192 bool getOrFindDebugBinary(const ArrayRef<uint8_t> BuildID,
193 std::string &Result);
194
195 /// Returns pair of pointers to object and debug object.
196 Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
197 const std::string &ArchName);
198
199 /// Return a pointer to object file at specified path, for a specified
200 /// architecture (e.g. if path refers to a Mach-O universal binary, only one
201 /// object file from it will be returned).
202 Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
203 const std::string &ArchName);
204
205 /// Update the LRU cache order when a binary is accessed.
206 void recordAccess(CachedBinary &Bin);
207
208 std::map<std::string, std::unique_ptr<SymbolizableModule>, std::less<>>
209 Modules;
210 StringMap<std::string> BuildIDPaths;
211
212 /// Contains cached results of getOrCreateObjectPair().
213 std::map<std::pair<std::string, std::string>, ObjectPair>
214 ObjectPairForPathArch;
215
216 /// Contains parsed binary for each path, or parsing error.
217 std::map<std::string, CachedBinary, std::less<>> BinaryForPath;
218
219 /// A list of cached binaries in LRU order.
220 simple_ilist<CachedBinary> LRUBinaries;
221 /// Sum of the sizes of the cached binaries.
222 size_t CacheSize = 0;
223
224 /// Parsed object file for path/architecture pair, where "path" refers
225 /// to Mach-O universal binary.
226 std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
227 ObjectForUBPathAndArch;
228
229 Options Opts;
230
231 std::unique_ptr<BuildIDFetcher> BIDFetcher;
232};
233
234// A binary intrusively linked into a LRU cache list. If the binary is empty,
235// then the entry marks that an error occurred, and it is not part of the LRU
236// list.
237class CachedBinary : public ilist_node<CachedBinary> {
238public:
239 CachedBinary() = default;
241
244
245 // Add an action to be performed when the binary is evicted, before all
246 // previously registered evictors.
247 LLVM_ABI void pushEvictor(std::function<void()> Evictor);
248
249 // Run all registered evictors in the reverse of the order in which they were
250 // added.
251 void evict() {
252 if (Evictor)
253 Evictor();
254 }
255
256 size_t size() { return Bin.getBinary()->getData().size(); }
257
258private:
260 std::function<void()> Evictor;
261};
262
263} // end namespace symbolize
264} // end namespace llvm
265
266#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.
#define LLVM_ABI
Definition: Compiler.h:213
DILineInfoSpecifier::FunctionNameKind FunctionNameKind
std::string Name
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Tagged union holding either a T or a Error.
Definition: Error.h:485
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
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
This class is the base class for all object file types.
Definition: ObjectFile.h:231
A simple intrusive list implementation.
Definition: simple_ilist.h:81
CachedBinary(OwningBinary< Binary > Bin)
Definition: Symbolize.h:240
OwningBinary< Binary > * operator->()
Definition: Symbolize.h:243
LLVM_ABI void pushEvictor(std::function< void()> Evictor)
Definition: Symbolize.cpp:847
OwningBinary< Binary > & operator*()
Definition: Symbolize.h:242
static LLVM_ABI std::string DemangleName(StringRef Name, const SymbolizableModule *DbiModuleDescriptor)
Definition: Symbolize.cpp:798
LLVM_ABI Expected< std::vector< DILineInfo > > findSymbol(const ObjectFile &Obj, StringRef Symbol, uint64_t Offset)
Definition: Symbolize.cpp:270
LLVM_ABI Expected< DIInliningInfo > symbolizeInlinedCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
Definition: Symbolize.cpp:136
LLVM_ABI Expected< DILineInfo > symbolizeCode(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
Definition: Symbolize.cpp:85
LLVM_ABI Expected< DIGlobal > symbolizeData(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
Definition: Symbolize.cpp:181
LLVM_ABI Expected< std::vector< DILocal > > symbolizeFrame(const ObjectFile &Obj, object::SectionedAddress ModuleOffset)
Definition: Symbolize.cpp:222
LLVM_ABI Expected< SymbolizableModule * > getOrCreateModuleInfo(StringRef ModuleName)
Returns a SymbolizableModule or an error if loading debug info failed.
Definition: Symbolize.cpp:636
void setBuildIDFetcher(std::unique_ptr< BuildIDFetcher > Fetcher)
Definition: Symbolize.h:130
This file defines the ilist_node class template, which is a convenient base class for creating classe...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:142
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:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
DINameKind FunctionNameKind
Definition: DIContext.h:157
std::vector< std::string > GsymFileDirectory
Definition: Symbolize.h:68
std::vector< std::string > DebugFileDirectory
Definition: Symbolize.h:67
std::vector< std::string > DsymHints
Definition: Symbolize.h:64