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