LLVM 22.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,
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.
74 LVSymbols Symbols;
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.
134 LVElement *CurrentElement = nullptr;
135 LVScope *CurrentScope = nullptr;
136 LVSymbol *CurrentSymbol = nullptr;
137 LVType *CurrentType = nullptr;
138 LVLine *CurrentLine = nullptr;
139 LVOffset CurrentOffset = 0;
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);
157 LVRange *getSectionRanges(LVSectionIndex SectionIndex);
158
159 // Record Compilation Unit entry.
161 CompileUnits.emplace(Offset, CompileUnit);
162 }
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;
185 sys::path::append(Path, sys::path::Style::posix,
186 sys::path::parent_path(InputFilename),
187 sys::path::filename(sys::path::convert_to_slash(
188 From, sys::path::Style::windows)));
189 return std::string(Path);
190 }
191
192 virtual Error printScopes();
193 virtual Error printMatchedElements(bool UseMatchedElements);
194 virtual void sortScopes() {}
195
196public:
197 LVReader() = delete;
199 LVBinaryType BinaryType = LVBinaryType::NONE)
200 : BinaryType(BinaryType), OutputSplit(options().getOutputSplit()),
201 InputFilename(InputFilename), FileFormatName(FileFormatName), W(W),
202 OS(W.getOStream()) {}
203 LVReader(const LVReader &) = delete;
204 LVReader &operator=(const LVReader &) = delete;
205 virtual ~LVReader() = default;
206
207// Creates a logical object of the given KIND. The signature for the created
208// functions looks like:
209// ...
210// LVScope *createScope()
211// LVScopeRoot *creatScopeRoot()
212// LVType *createType();
213// ...
214#define LV_CREATE_OBJECT(KIND) \
215 LV##KIND *create##KIND() { \
216 return new (Allocated##KIND.Allocate()) LV##KIND(); \
217 }
218
219 // Lines creation.
220 LV_CREATE_OBJECT(Line)
221 LV_CREATE_OBJECT(LineDebug)
222 LV_CREATE_OBJECT(LineAssembler)
223
224 // Locations creation.
225 LV_CREATE_OBJECT(Location)
226 LV_CREATE_OBJECT(LocationSymbol)
227
228 // Scopes creation.
229 LV_CREATE_OBJECT(Scope)
230 LV_CREATE_OBJECT(ScopeAggregate)
231 LV_CREATE_OBJECT(ScopeAlias)
232 LV_CREATE_OBJECT(ScopeArray)
233 LV_CREATE_OBJECT(ScopeCompileUnit)
234 LV_CREATE_OBJECT(ScopeEnumeration)
235 LV_CREATE_OBJECT(ScopeFormalPack)
236 LV_CREATE_OBJECT(ScopeFunction)
237 LV_CREATE_OBJECT(ScopeFunctionInlined)
238 LV_CREATE_OBJECT(ScopeFunctionType)
239 LV_CREATE_OBJECT(ScopeModule)
240 LV_CREATE_OBJECT(ScopeNamespace)
241 LV_CREATE_OBJECT(ScopeRoot)
242 LV_CREATE_OBJECT(ScopeTemplatePack)
243
244 // Symbols creation.
245 LV_CREATE_OBJECT(Symbol)
246
247 // Types creation.
249 LV_CREATE_OBJECT(TypeDefinition)
250 LV_CREATE_OBJECT(TypeEnumerator)
251 LV_CREATE_OBJECT(TypeImport)
252 LV_CREATE_OBJECT(TypeParam)
253 LV_CREATE_OBJECT(TypeSubrange)
254
255#undef LV_CREATE_OBJECT
256
257 // Operations creation.
259 return new (AllocatedOperation.Allocate()) LVOperation(OpCode, Operands);
260 }
261
262 StringRef getFilename(LVObject *Object, size_t Index) const;
264 void setFilename(std::string Name) { InputFilename = std::move(Name); }
265 StringRef getFileFormatName() const { return FileFormatName; }
266
268
269 bool isBinaryTypeNone() const { return BinaryType == LVBinaryType::NONE; }
270 bool isBinaryTypeELF() const { return BinaryType == LVBinaryType::ELF; }
271 bool isBinaryTypeCOFF() const { return BinaryType == LVBinaryType::COFF; }
272
274 void setCompileUnit(LVScope *Scope) {
275 assert(Scope && Scope->isCompileUnit() && "Scope is not a compile unit");
276 CompileUnit = static_cast<LVScopeCompileUnit *>(Scope);
277 }
279 CompileUnit->setCPUType(Type);
280 }
282 return CompileUnit->getCPUType();
283 }
284
285 // Access to the scopes root.
286 LVScopeRoot *getScopesRoot() const { return Root; }
287
288 Error doPrint();
289 Error doLoad();
290
291 virtual std::string getRegisterName(LVSmall Opcode,
293 llvm_unreachable("Invalid instance reader.");
294 return {};
295 }
296
297 LVSectionIndex getDotTextSectionIndex() const { return DotTextSectionIndex; }
299 return getDotTextSectionIndex();
300 }
301
302 virtual bool isSystemEntry(LVElement *Element, StringRef Name = {}) const {
303 return false;
304 };
305
306 // Access to split context.
307 LVSplitContext &getSplitContext() { return SplitContext; }
308
309 // In the case of element comparison, register that added element.
311 if (!options().getCompareContext() && options().getCompareLines())
312 Lines.push_back(Line);
313 }
315 if (!options().getCompareContext() && options().getCompareScopes())
316 Scopes.push_back(Scope);
317 }
319 if (!options().getCompareContext() && options().getCompareSymbols())
320 Symbols.push_back(Symbol);
321 }
323 if (!options().getCompareContext() && options().getCompareTypes())
324 Types.push_back(Type);
325 }
326
327 const LVLines &getLines() const { return Lines; }
328 const LVScopes &getScopes() const { return Scopes; }
329 const LVSymbols &getSymbols() const { return Symbols; }
330 const LVTypes &getTypes() const { return Types; }
331
332 // Conditions to print an object.
333 bool doPrintLine(const LVLine *Line) const {
334 return patterns().printElement(Line);
335 }
336 bool doPrintLocation(const LVLocation *Location) const {
337 return patterns().printObject(Location);
338 }
339 bool doPrintScope(const LVScope *Scope) const {
340 return patterns().printElement(Scope);
341 }
342 bool doPrintSymbol(const LVSymbol *Symbol) const {
343 return patterns().printElement(Symbol);
344 }
345 bool doPrintType(const LVType *Type) const {
346 return patterns().printElement(Type);
347 }
348
349 static LVReader &getInstance();
350 static void setInstance(LVReader *Reader);
351
352 void print(raw_ostream &OS) const;
353 virtual void printRecords(raw_ostream &OS) const {}
354
355#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
356 void dump() const { print(dbgs()); }
357#endif
358};
359
362 return getReader().getSplitContext();
363}
365 return getReader().getCompileUnit();
366}
367
368} // end namespace logicalview
369} // end namespace llvm
370
371#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)
BlockVerifier::State From
RelocType Type
Definition: COFFYAML.cpp:410
#define LLVM_ABI
Definition: Compiler.h:213
std::string Name
uint32_t Index
uint64_t Offset
Definition: ELF_riscv.cpp:478
static SmallString< 128 > getFilename(const DIScope *SP)
Extract a filename for a DIScope.
#define LV_CREATE_OBJECT(KIND)
Definition: LVReader.h:214
#define LV_OBJECT_ALLOCATOR(KIND)
Definition: LVReader.h:82
mir Rename Register Operands
static cl::opt< std::string > InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"))
PowerPC Reduce CR logical Operation
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
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:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Stores all information relating to a compile unit, be it in its original instance in the object file ...
void setName(StringRef ElementName) override
Definition: LVElement.cpp:95
LLVM_ABI bool printObject(const LVLocation *Location) const
Definition: LVOptions.cpp:539
LLVM_ABI bool printElement(const LVLine *Line) const
Definition: LVOptions.cpp:534
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:265
void addCompileUnitOffset(LVOffset Offset, LVScopeCompileUnit *CompileUnit)
Definition: LVReader.h:160
bool doPrintLine(const LVLine *Line) const
Definition: LVReader.h:333
std::vector< LVAddressRange > CurrentRanges
Definition: LVReader.h:142
const LVTypes & getTypes() const
Definition: LVReader.h:330
std::string FileFormatName
Definition: LVReader.h:146
void notifyAddedElement(LVType *Type)
Definition: LVReader.h:322
void notifyAddedElement(LVLine *Line)
Definition: LVReader.h:310
virtual std::string getRegisterName(LVSmall Opcode, ArrayRef< uint64_t > Operands)
Definition: LVReader.h:291
const LVSymbols & getSymbols() const
Definition: LVReader.h:329
void notifyAddedElement(LVSymbol *Symbol)
Definition: LVReader.h:318
raw_ostream & outputStream()
Definition: LVReader.h:267
codeview::CPUType getCompileUnitCPUType()
Definition: LVReader.h:281
std::string createAlternativePath(StringRef From)
Definition: LVReader.h:181
const LVLines & getLines() const
Definition: LVReader.h:327
LVReader(const LVReader &)=delete
bool doPrintSymbol(const LVSymbol *Symbol) const
Definition: LVReader.h:342
void setFilename(std::string Name)
Definition: LVReader.h:264
bool isBinaryTypeCOFF() const
Definition: LVReader.h:271
bool isBinaryTypeNone() const
Definition: LVReader.h:269
LVSectionIndex getDotTextSectionIndex() const
Definition: LVReader.h:297
void setCompileUnitCPUType(codeview::CPUType Type)
Definition: LVReader.h:278
LVReader & operator=(const LVReader &)=delete
bool doPrintLocation(const LVLocation *Location) const
Definition: LVReader.h:336
virtual bool isSystemEntry(LVElement *Element, StringRef Name={}) const
Definition: LVReader.h:302
LVSplitContext & getSplitContext()
Definition: LVReader.h:307
bool isBinaryTypeELF() const
Definition: LVReader.h:270
virtual ~LVReader()=default
StringRef getFilename() const
Definition: LVReader.h:263
static LVReader & getInstance()
Definition: LVReader.cpp:152
const LVScopes & getScopes() const
Definition: LVReader.h:328
LVReader(StringRef InputFilename, StringRef FileFormatName, ScopedPrinter &W, LVBinaryType BinaryType=LVBinaryType::NONE)
Definition: LVReader.h:198
LVScopeCompileUnit * getCompileUnit() const
Definition: LVReader.h:273
LVScopeRoot * getScopesRoot() const
Definition: LVReader.h:286
virtual LVSectionIndex getSectionIndex(LVScope *Scope)
Definition: LVReader.h:298
void setCompileUnit(LVScope *Scope)
Definition: LVReader.h:274
LVOperation * createOperation(LVSmall OpCode, ArrayRef< LVUnsigned > Operands)
Definition: LVReader.h:258
bool doPrintType(const LVType *Type) const
Definition: LVReader.h:345
virtual Error createScopes()
Definition: LVReader.h:167
bool doPrintScope(const LVScope *Scope) const
Definition: LVReader.h:339
void notifyAddedElement(LVScope *Scope)
Definition: LVReader.h:314
virtual void printRecords(raw_ostream &OS) const
Definition: LVReader.h:353
void setFileFormatName(StringRef FileFormatName)
Definition: LVScope.h:822
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:111
LLVM_ABI std::error_code open(std::string Name, std::string Extension, raw_ostream &OS)
Definition: LVReader.cpp:130
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:461
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:77
LVReader & getReader()
Definition: LVReader.h:360
LVPatterns & patterns()
Definition: LVOptions.h:645
constexpr LVSectionIndex UndefinedSectionIndex
Definition: LVReader.h:29
LVScopeCompileUnit * getReaderCompileUnit()
Definition: LVReader.h:364
LVSplitContext & getReaderSplitContext()
Definition: LVReader.h:361
LVOptions & options()
Definition: LVOptions.h:448
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207