LLVM 23.0.0git
LVReader.h
Go to the documentation of this file.
1//===-- LVReader.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// This file defines the LVReader class, which is used to describe a debug
10// information reader.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVREADER_H
15#define LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVREADER_H
16
20#include "llvm/Support/Errc.h"
21#include "llvm/Support/Error.h"
24#include <map>
25
26namespace llvm {
27namespace logicalview {
28
30
32class LVObject;
33
34class LVSplitContext final {
35 std::unique_ptr<ToolOutputFile> OutputFile;
36 std::string Location;
37
38public:
39 LVSplitContext() = default;
40 LVSplitContext(const LVSplitContext &) = delete;
42 ~LVSplitContext() = default;
43
45 LLVM_ABI std::error_code open(std::string Name, std::string Extension,
46 raw_ostream &OS);
47 void close() {
48 if (OutputFile) {
49 OutputFile->os().close();
50 OutputFile = nullptr;
51 }
52 }
53
54 std::string getLocation() const { return Location; }
55 raw_fd_ostream &os() { return OutputFile->os(); }
56};
57
58/// The logical reader owns of all the logical elements created during
59/// the debug information parsing. For its creation it uses a specific
60/// bump allocator for each type of logical element.
62 LVBinaryType BinaryType;
63
64 // Context used by '--output=split' command line option.
65 LVSplitContext SplitContext;
66
67 // Compile Units DIE Offset => Scope.
68 using LVCompileUnits = std::map<LVOffset, LVScopeCompileUnit *>;
69 LVCompileUnits CompileUnits;
70
71 // Added elements to be used during elements comparison.
72 LVLines Lines;
73 LVScopes Scopes;
74 LVSymbols Symbols;
75 LVTypes Types;
76
77 // Create split folder.
78 Error createSplitFolder();
79 bool OutputSplit = false;
80
81// Define a specific bump allocator for the given KIND.
82#define LV_OBJECT_ALLOCATOR(KIND) \
83 llvm::SpecificBumpPtrAllocator<LV##KIND> Allocated##KIND;
84
85 // Lines allocator.
87 LV_OBJECT_ALLOCATOR(LineDebug)
88 LV_OBJECT_ALLOCATOR(LineAssembler)
89
90 // Locations allocator.
91 LV_OBJECT_ALLOCATOR(Location)
92 LV_OBJECT_ALLOCATOR(LocationSymbol)
93
94 // Operations allocator.
96
97 // Scopes allocator.
99 LV_OBJECT_ALLOCATOR(ScopeAggregate)
100 LV_OBJECT_ALLOCATOR(ScopeAlias)
101 LV_OBJECT_ALLOCATOR(ScopeArray)
102 LV_OBJECT_ALLOCATOR(ScopeCompileUnit)
103 LV_OBJECT_ALLOCATOR(ScopeEnumeration)
104 LV_OBJECT_ALLOCATOR(ScopeFormalPack)
105 LV_OBJECT_ALLOCATOR(ScopeFunction)
106 LV_OBJECT_ALLOCATOR(ScopeFunctionInlined)
107 LV_OBJECT_ALLOCATOR(ScopeFunctionType)
108 LV_OBJECT_ALLOCATOR(ScopeModule)
109 LV_OBJECT_ALLOCATOR(ScopeNamespace)
110 LV_OBJECT_ALLOCATOR(ScopeRoot)
111 LV_OBJECT_ALLOCATOR(ScopeTemplatePack)
112
113 // Symbols allocator.
114 LV_OBJECT_ALLOCATOR(Symbol)
115
116 // Types allocator.
118 LV_OBJECT_ALLOCATOR(TypeDefinition)
119 LV_OBJECT_ALLOCATOR(TypeEnumerator)
120 LV_OBJECT_ALLOCATOR(TypeImport)
121 LV_OBJECT_ALLOCATOR(TypeParam)
122 LV_OBJECT_ALLOCATOR(TypeSubrange)
123
124#undef LV_OBJECT_ALLOCATOR
125
126 // Scopes with ranges for current compile unit. It is used to find a line
127 // giving its exact or closest address. To support comdat functions, all
128 // addresses for the same section are recorded in the same map.
129 using LVSectionRanges = std::map<LVSectionIndex, std::unique_ptr<LVRange>>;
130 LVSectionRanges SectionRanges;
131
132protected:
133 // Current elements during the processing of a DIE/MDNode.
137 LVType *CurrentType = nullptr;
138 LVLine *CurrentLine = nullptr;
140
141 // Address ranges collected for current DIE/MDNode/AST Node.
142 std::vector<LVAddressRange> CurrentRanges;
143
144 LVScopeRoot *Root = nullptr;
145 std::string InputFilename;
146 std::string FileFormatName;
150
151 // Only for ELF format. The CodeView is handled in a different way.
153
154 void addSectionRange(LVSectionIndex SectionIndex, LVScope *Scope);
155 void addSectionRange(LVSectionIndex SectionIndex, LVScope *Scope,
156 LVAddress LowerAddress, LVAddress UpperAddress);
158
159 // Record Compilation Unit entry.
163
164 LVElement *createElement(dwarf::Tag Tag);
165
166 // Create the Scope Root.
167 virtual Error createScopes() {
168 Root = createScopeRoot();
169 Root->setName(getFilename());
170 if (options().getAttributeFormat())
171 Root->setFileFormatName(FileFormatName);
172 return Error::success();
173 }
174
175 // Return a pathname composed by: parent_path(InputFilename)/filename(From).
176 // This is useful when a type server (PDB file associated with an object
177 // file or a precompiled header file) or a DWARF split object have been
178 // moved from their original location. That is the case when running
179 // regression tests, where object files are created in one location and
180 // executed in a different location.
182 // During the reader initialization, any backslashes in 'InputFilename'
183 // are converted to forward slashes.
184 SmallString<128> Path;
189 return std::string(Path);
190 }
191
192 virtual Error printScopes();
193 virtual Error printMatchedElements(bool UseMatchedElements);
194 virtual void sortScopes() {}
195
196 void printCollectedElements(LVScope *Root);
197 bool checkIntegrityScopesTree(LVScope *Root);
198
199public:
200 LVReader() = delete;
202 LVBinaryType BinaryType = LVBinaryType::NONE)
203 : BinaryType(BinaryType), OutputSplit(options().getOutputSplit()),
205 OS(W.getOStream()) {}
206 LVReader(const LVReader &) = delete;
207 LVReader &operator=(const LVReader &) = delete;
208 virtual ~LVReader() = default;
209
210// Creates a logical object of the given KIND. The signature for the created
211// functions looks like:
212// ...
213// LVScope *createScope()
214// LVScopeRoot *creatScopeRoot()
215// LVType *createType();
216// ...
217#define LV_CREATE_OBJECT(KIND) \
218 LV##KIND *create##KIND() { \
219 return new (Allocated##KIND.Allocate()) LV##KIND(); \
220 }
221
222 // Lines creation.
223 LV_CREATE_OBJECT(Line)
224 LV_CREATE_OBJECT(LineDebug)
225 LV_CREATE_OBJECT(LineAssembler)
226
227 // Locations creation.
228 LV_CREATE_OBJECT(Location)
229 LV_CREATE_OBJECT(LocationSymbol)
230
231 // Scopes creation.
232 LV_CREATE_OBJECT(Scope)
233 LV_CREATE_OBJECT(ScopeAggregate)
234 LV_CREATE_OBJECT(ScopeAlias)
235 LV_CREATE_OBJECT(ScopeArray)
236 LV_CREATE_OBJECT(ScopeCompileUnit)
237 LV_CREATE_OBJECT(ScopeEnumeration)
238 LV_CREATE_OBJECT(ScopeFormalPack)
239 LV_CREATE_OBJECT(ScopeFunction)
240 LV_CREATE_OBJECT(ScopeFunctionInlined)
241 LV_CREATE_OBJECT(ScopeFunctionType)
242 LV_CREATE_OBJECT(ScopeModule)
243 LV_CREATE_OBJECT(ScopeNamespace)
244 LV_CREATE_OBJECT(ScopeRoot)
245 LV_CREATE_OBJECT(ScopeTemplatePack)
246
247 // Symbols creation.
248 LV_CREATE_OBJECT(Symbol)
249
250 // Types creation.
251 LV_CREATE_OBJECT(Type)
252 LV_CREATE_OBJECT(TypeDefinition)
253 LV_CREATE_OBJECT(TypeEnumerator)
254 LV_CREATE_OBJECT(TypeImport)
255 LV_CREATE_OBJECT(TypeParam)
256 LV_CREATE_OBJECT(TypeSubrange)
257
258#undef LV_CREATE_OBJECT
259
260 // Operations creation.
262 return new (AllocatedOperation.Allocate()) LVOperation(OpCode, Operands);
263 }
264
265 StringRef getFilename(LVObject *Object, size_t Index) const;
267 void setFilename(std::string Name) { InputFilename = std::move(Name); }
269
271
272 bool isBinaryTypeNone() const { return BinaryType == LVBinaryType::NONE; }
273 bool isBinaryTypeELF() const { return BinaryType == LVBinaryType::ELF; }
274 bool isBinaryTypeCOFF() const { return BinaryType == LVBinaryType::COFF; }
275
277 void setCompileUnit(LVScope *Scope) {
278 assert(Scope && Scope->isCompileUnit() && "Scope is not a compile unit");
279 CompileUnit = static_cast<LVScopeCompileUnit *>(Scope);
280 }
285 return CompileUnit->getCPUType();
286 }
287
288 // Access to the scopes root.
289 LVScopeRoot *getScopesRoot() const { return Root; }
290
291 Error doPrint();
292 Error doLoad();
293
294 virtual std::string getRegisterName(LVSmall Opcode,
295 ArrayRef<uint64_t> Operands) {
296 llvm_unreachable("Invalid instance reader.");
297 return {};
298 }
299
302 return getDotTextSectionIndex();
303 }
304
305 virtual bool isSystemEntry(LVElement *Element, StringRef Name = {}) const {
306 return false;
307 };
308
309 // Access to split context.
310 LVSplitContext &getSplitContext() { return SplitContext; }
311
312 // In the case of element comparison, register that added element.
314 if (!options().getCompareContext() && options().getCompareLines())
315 Lines.push_back(Line);
316 }
318 if (!options().getCompareContext() && options().getCompareScopes())
319 Scopes.push_back(Scope);
320 }
322 if (!options().getCompareContext() && options().getCompareSymbols())
323 Symbols.push_back(Symbol);
324 }
326 if (!options().getCompareContext() && options().getCompareTypes())
327 Types.push_back(Type);
328 }
329
330 const LVLines &getLines() const { return Lines; }
331 const LVScopes &getScopes() const { return Scopes; }
332 const LVSymbols &getSymbols() const { return Symbols; }
333 const LVTypes &getTypes() const { return Types; }
334
335 // Conditions to print an object.
336 bool doPrintLine(const LVLine *Line) const {
337 return patterns().printElement(Line);
338 }
340 return patterns().printObject(Location);
341 }
342 bool doPrintScope(const LVScope *Scope) const {
343 return patterns().printElement(Scope);
344 }
345 bool doPrintSymbol(const LVSymbol *Symbol) const {
346 return patterns().printElement(Symbol);
347 }
348 bool doPrintType(const LVType *Type) const {
349 return patterns().printElement(Type);
350 }
351
352 static LVReader &getInstance();
353 static void setInstance(LVReader *Reader);
354
355 void print(raw_ostream &OS) const;
356 virtual void printRecords(raw_ostream &OS) const {}
357
358#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
359 void dump() const { print(dbgs()); }
360#endif
361};
362
370
371} // end namespace logicalview
372} // end namespace llvm
373
374#endif // LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVREADER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_ABI
Definition Compiler.h:215
static SmallString< 128 > getFilename(const DIScope *SP, vfs::FileSystem &VFS)
Extract a filename for a DIScope.
#define LV_CREATE_OBJECT(KIND)
Definition LVReader.h:217
#define LV_OBJECT_ALLOCATOR(KIND)
Definition LVReader.h:82
PowerPC Reduce CR logical Operation
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
The logical reader owns of all the logical elements created during the debug information parsing.
Definition LVReader.h:61
virtual void sortScopes()
Definition LVReader.h:194
StringRef getFileFormatName() const
Definition LVReader.h:268
LVRange * getSectionRanges(LVSectionIndex SectionIndex)
Definition LVReader.cpp:236
void addCompileUnitOffset(LVOffset Offset, LVScopeCompileUnit *CompileUnit)
Definition LVReader.h:160
bool doPrintLine(const LVLine *Line) const
Definition LVReader.h:336
std::vector< LVAddressRange > CurrentRanges
Definition LVReader.h:142
const LVTypes & getTypes() const
Definition LVReader.h:333
void notifyAddedElement(LVType *Type)
Definition LVReader.h:325
void notifyAddedElement(LVLine *Line)
Definition LVReader.h:313
virtual std::string getRegisterName(LVSmall Opcode, ArrayRef< uint64_t > Operands)
Definition LVReader.h:294
const LVSymbols & getSymbols() const
Definition LVReader.h:332
void notifyAddedElement(LVSymbol *Symbol)
Definition LVReader.h:321
raw_ostream & outputStream()
Definition LVReader.h:270
codeview::CPUType getCompileUnitCPUType()
Definition LVReader.h:284
std::string createAlternativePath(StringRef From)
Definition LVReader.h:181
const LVLines & getLines() const
Definition LVReader.h:330
LVReader(const LVReader &)=delete
bool doPrintSymbol(const LVSymbol *Symbol) const
Definition LVReader.h:345
void setFilename(std::string Name)
Definition LVReader.h:267
bool isBinaryTypeCOFF() const
Definition LVReader.h:274
bool isBinaryTypeNone() const
Definition LVReader.h:272
LVSectionIndex getDotTextSectionIndex() const
Definition LVReader.h:300
void setCompileUnitCPUType(codeview::CPUType Type)
Definition LVReader.h:281
LVReader & operator=(const LVReader &)=delete
bool doPrintLocation(const LVLocation *Location) const
Definition LVReader.h:339
virtual bool isSystemEntry(LVElement *Element, StringRef Name={}) const
Definition LVReader.h:305
LVSplitContext & getSplitContext()
Definition LVReader.h:310
bool isBinaryTypeELF() const
Definition LVReader.h:273
virtual ~LVReader()=default
StringRef getFilename() const
Definition LVReader.h:266
static LVReader & getInstance()
Definition LVReader.cpp:180
const LVScopes & getScopes() const
Definition LVReader.h:331
LVScopeCompileUnit * CompileUnit
Definition LVReader.h:149
LVReader(StringRef InputFilename, StringRef FileFormatName, ScopedPrinter &W, LVBinaryType BinaryType=LVBinaryType::NONE)
Definition LVReader.h:201
LVScopeCompileUnit * getCompileUnit() const
Definition LVReader.h:276
LVScopeRoot * getScopesRoot() const
Definition LVReader.h:289
virtual LVSectionIndex getSectionIndex(LVScope *Scope)
Definition LVReader.h:301
LVSectionIndex DotTextSectionIndex
Definition LVReader.h:152
void setCompileUnit(LVScope *Scope)
Definition LVReader.h:277
LVOperation * createOperation(LVSmall OpCode, ArrayRef< LVUnsigned > Operands)
Definition LVReader.h:261
bool doPrintType(const LVType *Type) const
Definition LVReader.h:348
void addSectionRange(LVSectionIndex SectionIndex, LVScope *Scope)
Definition LVReader.cpp:225
virtual Error createScopes()
Definition LVReader.h:167
bool doPrintScope(const LVScope *Scope) const
Definition LVReader.h:342
void notifyAddedElement(LVScope *Scope)
Definition LVReader.h:317
virtual void printRecords(raw_ostream &OS) const
Definition LVReader.h:356
std::string getLocation() const
Definition LVReader.h:54
LVSplitContext(const LVSplitContext &)=delete
LVSplitContext & operator=(const LVSplitContext &)=delete
LLVM_ABI Error createSplitFolder(StringRef Where)
Definition LVReader.cpp:139
LLVM_ABI std::error_code open(std::string Name, std::string Extension, raw_ostream &OS)
Definition LVReader.cpp:158
A raw_ostream that writes to a file descriptor.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
CPUType
These values correspond to the CV_CPU_TYPE_e enumeration, and are documented here: https://msdn....
Definition CodeView.h:76
LVReader & getReader()
Definition LVReader.h:363
uint64_t LVOffset
Definition LVObject.h:39
LVPatterns & patterns()
Definition LVOptions.h:645
constexpr LVSectionIndex UndefinedSectionIndex
Definition LVReader.h:29
LVScopeCompileUnit * getReaderCompileUnit()
Definition LVReader.h:367
SmallVector< LVScope *, 8 > LVScopes
Definition LVObject.h:80
SmallVector< LVSymbol *, 8 > LVSymbols
Definition LVObject.h:81
LVSplitContext & getReaderSplitContext()
Definition LVReader.h:364
uint8_t LVSmall
Definition LVObject.h:42
uint64_t LVSectionIndex
Definition LVObject.h:35
SmallVector< LVLine *, 8 > LVLines
Definition LVObject.h:77
uint64_t LVAddress
Definition LVObject.h:36
LVOptions & options()
Definition LVOptions.h:448
SmallVector< LVType *, 8 > LVTypes
Definition LVObject.h:82
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:478
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:594
LLVM_ABI std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition Path.cpp:585
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:467
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209