LLVM 17.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
19#include "llvm/Support/Errc.h"
20#include "llvm/Support/Error.h"
23#include <map>
24
25namespace llvm {
26namespace logicalview {
27
29
31class LVObject;
32
33class LVSplitContext final {
34 std::unique_ptr<ToolOutputFile> OutputFile;
35 std::string Location;
36
37public:
38 LVSplitContext() = default;
39 LVSplitContext(const LVSplitContext &) = delete;
41 ~LVSplitContext() = default;
42
44 std::error_code open(std::string Name, std::string Extension,
46 void close() {
47 if (OutputFile) {
48 OutputFile->os().close();
49 OutputFile = nullptr;
50 }
51 }
52
53 std::string getLocation() const { return Location; }
54 raw_fd_ostream &os() { return OutputFile->os(); }
55};
56
57/// The logical reader owns of all the logical elements created during
58/// the debug information parsing. For its creation it uses a specific
59/// bump allocator for each type of logical element.
60class LVReader {
61 LVBinaryType BinaryType;
62
63 // Context used by '--output=split' command line option.
64 LVSplitContext SplitContext;
65
66 // Compile Units DIE Offset => Scope.
67 using LVCompileUnits = std::map<LVOffset, LVScopeCompileUnit *>;
68 LVCompileUnits CompileUnits;
69
70 // Added elements to be used during elements comparison.
71 LVLines Lines;
72 LVScopes Scopes;
73 LVSymbols Symbols;
74 LVTypes Types;
75
76 // Create split folder.
77 Error createSplitFolder();
78 bool OutputSplit = false;
79
80// Define a specific bump allocator for the given KIND.
81#define LV_OBJECT_ALLOCATOR(KIND) \
82 llvm::SpecificBumpPtrAllocator<LV##KIND> Allocated##KIND;
83
84 // Lines allocator.
86 LV_OBJECT_ALLOCATOR(LineDebug)
87 LV_OBJECT_ALLOCATOR(LineAssembler)
88
89 // Locations allocator.
91 LV_OBJECT_ALLOCATOR(LocationSymbol)
92
93 // Operations allocator.
95
96 // Scopes allocator.
98 LV_OBJECT_ALLOCATOR(ScopeAggregate)
99 LV_OBJECT_ALLOCATOR(ScopeAlias)
100 LV_OBJECT_ALLOCATOR(ScopeArray)
101 LV_OBJECT_ALLOCATOR(ScopeCompileUnit)
102 LV_OBJECT_ALLOCATOR(ScopeEnumeration)
103 LV_OBJECT_ALLOCATOR(ScopeFormalPack)
104 LV_OBJECT_ALLOCATOR(ScopeFunction)
105 LV_OBJECT_ALLOCATOR(ScopeFunctionInlined)
106 LV_OBJECT_ALLOCATOR(ScopeFunctionType)
107 LV_OBJECT_ALLOCATOR(ScopeNamespace)
108 LV_OBJECT_ALLOCATOR(ScopeRoot)
109 LV_OBJECT_ALLOCATOR(ScopeTemplatePack)
110
111 // Symbols allocator.
112 LV_OBJECT_ALLOCATOR(Symbol)
113
114 // Types allocator.
116 LV_OBJECT_ALLOCATOR(TypeDefinition)
117 LV_OBJECT_ALLOCATOR(TypeEnumerator)
118 LV_OBJECT_ALLOCATOR(TypeImport)
119 LV_OBJECT_ALLOCATOR(TypeParam)
120 LV_OBJECT_ALLOCATOR(TypeSubrange)
121
122#undef LV_OBJECT_ALLOCATOR
123
124protected:
125 LVScopeRoot *Root = nullptr;
126 std::string InputFilename;
127 std::string FileFormatName;
131
132 // Only for ELF format. The CodeView is handled in a different way.
134
135 // Record Compilation Unit entry.
137 CompileUnits.emplace(Offset, CompileUnit);
138 }
139
140 // Create the Scope Root.
141 virtual Error createScopes() {
142 Root = createScopeRoot();
144 if (options().getAttributeFormat())
146 return Error::success();
147 }
148
149 // Return a pathname composed by: parent_path(InputFilename)/filename(From).
150 // This is useful when a type server (PDB file associated with an object
151 // file or a precompiled header file) or a DWARF split object have been
152 // moved from their original location. That is the case when running
153 // regression tests, where object files are created in one location and
154 // executed in a different location.
156 // During the reader initialization, any backslashes in 'InputFilename'
157 // are converted to forward slashes.
158 SmallString<128> Path;
163 return std::string(Path);
164 }
165
166 virtual Error printScopes();
167 virtual Error printMatchedElements(bool UseMatchedElements);
168 virtual void sortScopes() {}
169
170public:
171 LVReader() = delete;
173 LVBinaryType BinaryType = LVBinaryType::NONE)
174 : BinaryType(BinaryType), OutputSplit(options().getOutputSplit()),
176 OS(W.getOStream()) {}
177 LVReader(const LVReader &) = delete;
178 LVReader &operator=(const LVReader &) = delete;
179 virtual ~LVReader() = default;
180
181// Creates a logical object of the given KIND. The signature for the created
182// functions looks like:
183// ...
184// LVScope *createScope()
185// LVScopeRoot *creatScopeRoot()
186// LVType *createType();
187// ...
188#define LV_CREATE_OBJECT(KIND) \
189 LV##KIND *create##KIND() { \
190 return new (Allocated##KIND.Allocate()) LV##KIND(); \
191 }
192
193 // Lines creation.
194 LV_CREATE_OBJECT(Line)
195 LV_CREATE_OBJECT(LineDebug)
196 LV_CREATE_OBJECT(LineAssembler)
197
198 // Locations creation.
200 LV_CREATE_OBJECT(LocationSymbol)
201
202 // Scopes creation.
203 LV_CREATE_OBJECT(Scope)
204 LV_CREATE_OBJECT(ScopeAggregate)
205 LV_CREATE_OBJECT(ScopeAlias)
206 LV_CREATE_OBJECT(ScopeArray)
207 LV_CREATE_OBJECT(ScopeCompileUnit)
208 LV_CREATE_OBJECT(ScopeEnumeration)
209 LV_CREATE_OBJECT(ScopeFormalPack)
210 LV_CREATE_OBJECT(ScopeFunction)
211 LV_CREATE_OBJECT(ScopeFunctionInlined)
212 LV_CREATE_OBJECT(ScopeFunctionType)
213 LV_CREATE_OBJECT(ScopeNamespace)
214 LV_CREATE_OBJECT(ScopeRoot)
215 LV_CREATE_OBJECT(ScopeTemplatePack)
216
217 // Symbols creation.
218 LV_CREATE_OBJECT(Symbol)
219
220 // Types creation.
221 LV_CREATE_OBJECT(Type)
222 LV_CREATE_OBJECT(TypeDefinition)
223 LV_CREATE_OBJECT(TypeEnumerator)
224 LV_CREATE_OBJECT(TypeImport)
225 LV_CREATE_OBJECT(TypeParam)
226 LV_CREATE_OBJECT(TypeSubrange)
227
228#undef LV_CREATE_OBJECT
229
230 // Operations creation.
232 LVUnsigned Operand2) {
233 return new (AllocatedOperation.Allocate())
234 LVOperation(OpCode, Operand1, Operand2);
235 }
236
237 StringRef getFilename(LVObject *Object, size_t Index) const;
239 void setFilename(std::string Name) { InputFilename = std::move(Name); }
241
243
244 bool isBinaryTypeNone() const { return BinaryType == LVBinaryType::NONE; }
245 bool isBinaryTypeELF() const { return BinaryType == LVBinaryType::ELF; }
246 bool isBinaryTypeCOFF() const { return BinaryType == LVBinaryType::COFF; }
247
249 void setCompileUnit(LVScope *Scope) {
250 assert(Scope && Scope->isCompileUnit() && "Scope is not a compile unit");
251 CompileUnit = static_cast<LVScopeCompileUnit *>(Scope);
252 }
254 CompileUnit->setCPUType(Type);
255 }
257 return CompileUnit->getCPUType();
258 }
259
260 // Access to the scopes root.
261 LVScopeRoot *getScopesRoot() const { return Root; }
262
263 Error doPrint();
264 Error doLoad();
265
266 virtual std::string getRegisterName(LVSmall Opcode, uint64_t Operands[2]) {
267 llvm_unreachable("Invalid instance reader.");
268 return {};
269 }
270
273 return getDotTextSectionIndex();
274 }
275
276 virtual bool isSystemEntry(LVElement *Element, StringRef Name = {}) const {
277 return false;
278 };
279
280 // Access to split context.
281 LVSplitContext &getSplitContext() { return SplitContext; }
282
283 // In the case of element comparison, register that added element.
285 if (!options().getCompareContext() && options().getCompareLines())
286 Lines.push_back(Line);
287 }
289 if (!options().getCompareContext() && options().getCompareScopes())
290 Scopes.push_back(Scope);
291 }
293 if (!options().getCompareContext() && options().getCompareSymbols())
294 Symbols.push_back(Symbol);
295 }
297 if (!options().getCompareContext() && options().getCompareTypes())
298 Types.push_back(Type);
299 }
300
301 const LVLines &getLines() const { return Lines; }
302 const LVScopes &getScopes() const { return Scopes; }
303 const LVSymbols &getSymbols() const { return Symbols; }
304 const LVTypes &getTypes() const { return Types; }
305
306 // Conditions to print an object.
307 bool doPrintLine(const LVLine *Line) const {
308 return patterns().printElement(Line);
309 }
311 return patterns().printObject(Location);
312 }
313 bool doPrintScope(const LVScope *Scope) const {
314 return patterns().printElement(Scope);
315 }
316 bool doPrintSymbol(const LVSymbol *Symbol) const {
317 return patterns().printElement(Symbol);
318 }
319 bool doPrintType(const LVType *Type) const {
320 return patterns().printElement(Type);
321 }
322
323 static LVReader &getInstance();
324 static void setInstance(LVReader *Reader);
325
326 void print(raw_ostream &OS) const;
327 virtual void printRecords(raw_ostream &OS) const {}
328
329#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
330 void dump() const { print(dbgs()); }
331#endif
332};
333
336 return getReader().getSplitContext();
337}
339 return getReader().getCompileUnit();
340}
341
342} // end namespace logicalview
343} // end namespace llvm
344
345#endif // LLVM_DEBUGINFO_LOGICALVIEW_CORE_LVREADER_H
BlockVerifier::State From
#define LV_CREATE_OBJECT(KIND)
Definition: LVReader.h:188
#define LV_OBJECT_ALLOCATOR(KIND)
Definition: LVReader.h:81
mir Rename Register Operands
PowerPC Reduce CR logical Operation
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Stores all information relating to a compile unit, be it in its original instance in the object file ...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
void setName(StringRef ElementName) override
Definition: LVElement.cpp:96
bool printObject(const LVLocation *Location) const
Definition: LVOptions.cpp:538
bool printElement(const LVLine *Line) const
Definition: LVOptions.cpp:533
The logical reader owns of all the logical elements created during the debug information parsing.
Definition: LVReader.h:60
virtual void sortScopes()
Definition: LVReader.h:168
StringRef getFileFormatName() const
Definition: LVReader.h:240
void addCompileUnitOffset(LVOffset Offset, LVScopeCompileUnit *CompileUnit)
Definition: LVReader.h:136
bool doPrintLine(const LVLine *Line) const
Definition: LVReader.h:307
void print(raw_ostream &OS) const
Definition: LVReader.cpp:304
const LVTypes & getTypes() const
Definition: LVReader.h:304
std::string FileFormatName
Definition: LVReader.h:127
void notifyAddedElement(LVType *Type)
Definition: LVReader.h:296
void notifyAddedElement(LVLine *Line)
Definition: LVReader.h:284
const LVSymbols & getSymbols() const
Definition: LVReader.h:303
void notifyAddedElement(LVSymbol *Symbol)
Definition: LVReader.h:292
raw_ostream & outputStream()
Definition: LVReader.h:242
codeview::CPUType getCompileUnitCPUType()
Definition: LVReader.h:256
std::string createAlternativePath(StringRef From)
Definition: LVReader.h:155
const LVLines & getLines() const
Definition: LVReader.h:301
LVReader(const LVReader &)=delete
bool doPrintSymbol(const LVSymbol *Symbol) const
Definition: LVReader.h:316
void setFilename(std::string Name)
Definition: LVReader.h:239
bool isBinaryTypeCOFF() const
Definition: LVReader.h:246
bool isBinaryTypeNone() const
Definition: LVReader.h:244
LVOperation * createOperation(LVSmall OpCode, LVUnsigned Operand1, LVUnsigned Operand2)
Definition: LVReader.h:231
LVSectionIndex getDotTextSectionIndex() const
Definition: LVReader.h:271
void setCompileUnitCPUType(codeview::CPUType Type)
Definition: LVReader.h:253
LVReader & operator=(const LVReader &)=delete
bool doPrintLocation(const LVLocation *Location) const
Definition: LVReader.h:310
virtual bool isSystemEntry(LVElement *Element, StringRef Name={}) const
Definition: LVReader.h:276
LVSplitContext & getSplitContext()
Definition: LVReader.h:281
bool isBinaryTypeELF() const
Definition: LVReader.h:245
virtual ~LVReader()=default
StringRef getFilename() const
Definition: LVReader.h:238
static LVReader & getInstance()
Definition: LVReader.cpp:154
const LVScopes & getScopes() const
Definition: LVReader.h:302
LVScopeCompileUnit * CompileUnit
Definition: LVReader.h:130
static void setInstance(LVReader *Reader)
Definition: LVReader.cpp:160
LVReader(StringRef InputFilename, StringRef FileFormatName, ScopedPrinter &W, LVBinaryType BinaryType=LVBinaryType::NONE)
Definition: LVReader.h:172
LVScopeCompileUnit * getCompileUnit() const
Definition: LVReader.h:248
virtual std::string getRegisterName(LVSmall Opcode, uint64_t Operands[2])
Definition: LVReader.h:266
LVScopeRoot * getScopesRoot() const
Definition: LVReader.h:261
virtual LVSectionIndex getSectionIndex(LVScope *Scope)
Definition: LVReader.h:272
LVSectionIndex DotTextSectionIndex
Definition: LVReader.h:133
void setCompileUnit(LVScope *Scope)
Definition: LVReader.h:249
bool doPrintType(const LVType *Type) const
Definition: LVReader.h:319
virtual Error printMatchedElements(bool UseMatchedElements)
Definition: LVReader.cpp:297
virtual Error printScopes()
Definition: LVReader.cpp:281
virtual Error createScopes()
Definition: LVReader.h:141
bool doPrintScope(const LVScope *Scope) const
Definition: LVReader.h:313
void notifyAddedElement(LVScope *Scope)
Definition: LVReader.h:288
virtual void printRecords(raw_ostream &OS) const
Definition: LVReader.h:327
void setFileFormatName(StringRef FileFormatName)
Definition: LVScope.h:794
std::string getLocation() const
Definition: LVReader.h:53
LVSplitContext(const LVSplitContext &)=delete
LVSplitContext & operator=(const LVSplitContext &)=delete
Error createSplitFolder(StringRef Where)
Definition: LVReader.cpp:113
std::error_code open(std::string Name, std::string Extension, raw_ostream &OS)
Definition: LVReader.cpp:132
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:454
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#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:75
LVReader & getReader()
Definition: LVReader.h:334
LVPatterns & patterns()
Definition: LVOptions.h:640
uint8_t LVSmall
Definition: LVObject.h:43
constexpr LVSectionIndex UndefinedSectionIndex
Definition: LVReader.h:28
LVScopeCompileUnit * getReaderCompileUnit()
Definition: LVReader.h:338
LVSplitContext & getReaderSplitContext()
Definition: LVReader.h:335
LVOptions & options()
Definition: LVOptions.h:445
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:577
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:467
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition: Path.cpp:568
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:456
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163