LLVM 24.0.0git
LVCodeViewVisitor.h
Go to the documentation of this file.
1//===-- LVCodeViewVisitor.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 LVCodeViewVisitor class, which is used to describe a
10// debug information (CodeView) visitor.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_DEBUGINFO_LOGICALVIEW_READERS_CODEVIEWVISITOR_H
15#define LLVM_DEBUGINFO_LOGICALVIEW_READERS_CODEVIEWVISITOR_H
16
17#include "llvm/ADT/iterator.h"
24#include "llvm/Object/Binary.h"
26#include "llvm/Support/Error.h"
27#include <stack>
28#include <utility>
29
30namespace llvm {
31namespace logicalview {
32
33using namespace llvm::codeview;
34
37struct LVShared;
38
41 LVLogicalVisitor *LogicalVisitor;
44 uint32_t StreamIdx;
45 LVShared *Shared = nullptr;
46
47 // Current type index during the types traversal.
48 TypeIndex CurrentTypeIndex = TypeIndex::None();
49
50 void printTypeIndex(StringRef FieldName, TypeIndex TI,
51 uint32_t StreamIdx) const;
52
53public:
56 uint32_t StreamIdx, LVShared *Shared)
57 : TypeVisitorCallbacks(), W(W), LogicalVisitor(LogicalVisitor),
58 Types(Types), Ids(Ids), StreamIdx(StreamIdx), Shared(Shared) {}
59
60 Error visitTypeBegin(CVType &Record) override;
61 Error visitTypeBegin(CVType &Record, TypeIndex TI) override;
62 Error visitMemberBegin(CVMemberRecord &Record) override;
63 Error visitMemberEnd(CVMemberRecord &Record) override;
64 Error visitUnknownMember(CVMemberRecord &Record) override;
65
74 Error visitUnknownType(CVType &Record) override;
75};
76
78 LVCodeViewReader *Reader;
79 const llvm::object::coff_section *CoffSection;
80 StringRef SectionContents;
81
82public:
84 const llvm::object::SectionRef &Section,
86 StringRef SectionContents)
87 : Reader(Reader), SectionContents(SectionContents) {
88 CoffSection = Obj->getCOFFSection(Section);
89 }
90
93 if (Error Err = Reader.readLongestContiguousChunk(Data)) {
94 llvm::consumeError(std::move(Err));
95 return 0;
96 }
97 return Data.data() - SectionContents.bytes_begin();
98 }
99
100 void printRelocatedField(StringRef Label, uint32_t RelocOffset,
101 uint32_t Offset, StringRef *RelocSym = nullptr);
102
103 void getLinkageName(uint32_t RelocOffset, uint32_t Offset,
104 StringRef *RelocSym = nullptr);
105
106 StringRef getFileNameForFileOffset(uint32_t FileOffset) override;
107 DebugStringTableSubsectionRef getStringTable() override;
108};
109
110class LVElement;
111class LVScope;
112class LVSymbol;
113class LVType;
114
115// Visitor for CodeView symbol streams found in COFF object files and PDB files.
117 LVCodeViewReader *Reader;
118 ScopedPrinter &W;
119 LVLogicalVisitor *LogicalVisitor;
122 LVSymbolVisitorDelegate *ObjDelegate;
123 LVShared *Shared;
124
125 // Current object name collected from S_OBJNAME.
126 StringRef CurrentObjectName;
127 // Last symbol processed by S_LOCAL.
128 LVSymbol *LocalSymbol = nullptr;
129
130 bool InFunctionScope = false;
131 bool IsCompileUnit = false;
132
133 // Register for the locals and parameters symbols in the current frame.
134 RegisterId LocalFrameRegister = RegisterId::NONE;
135 RegisterId ParamFrameRegister = RegisterId::NONE;
136
137 void printLocalVariableAddrRange(const LocalVariableAddrRange &Range,
138 uint32_t RelocationOffset);
139 void printLocalVariableAddrGap(ArrayRef<LocalVariableAddrGap> Gaps);
140 void printTypeIndex(StringRef FieldName, TypeIndex TI) const;
141
142 // Return true if this symbol is a Compile Unit.
143 bool symbolIsCompileUnit(SymbolKind Kind) {
144 switch (Kind) {
145 case SymbolKind::S_COMPILE2:
146 case SymbolKind::S_COMPILE3:
147 return true;
148 default:
149 return false;
150 }
151 }
152
153 // Determine symbol kind (local or parameter).
154 void determineSymbolKind(LVSymbol *Symbol, RegisterId Register) {
155 if (Register == LocalFrameRegister) {
156 Symbol->setIsVariable();
157 return;
158 }
159 if (Register == ParamFrameRegister) {
160 Symbol->setIsParameter();
161 return;
162 }
163 // Assume is a variable.
164 Symbol->setIsVariable();
165 }
166
167 void setLocalVariableType(LVSymbol *Symbol, TypeIndex TI);
168
169public:
171 LVLogicalVisitor *LogicalVisitor,
174 LVSymbolVisitorDelegate *ObjDelegate, LVShared *Shared)
175 : Reader(Reader), W(W), LogicalVisitor(LogicalVisitor), Types(Types),
176 Ids(Ids), ObjDelegate(ObjDelegate), Shared(Shared) {}
177
178 Error visitSymbolBegin(CVSymbol &Record) override;
179 Error visitSymbolBegin(CVSymbol &Record, uint32_t Offset) override;
180 Error visitSymbolEnd(CVSymbol &Record) override;
181 Error visitUnknownSymbol(CVSymbol &Record) override;
182
185 Error visitKnownRecord(CVSymbol &Record, BuildInfoSym &BuildInfo) override;
186 Error visitKnownRecord(CVSymbol &Record, Compile2Sym &Compile2) override;
187 Error visitKnownRecord(CVSymbol &Record, Compile3Sym &Compile3) override;
192 &DefRangeFramePointerRelFullScope) override;
195 DefRangeFramePointerRelSym &DefRangeFramePointerRel) override;
197 DefRangeRegisterRelSym &DefRangeRegisterRel) override;
200 DefRangeRegisterRelIndirSym &DefRangeRegisterRelIndir) override;
202 DefRangeRegisterSym &DefRangeRegister) override;
205 DefRangeSubfieldRegisterSym &DefRangeSubfieldRegister) override;
207 DefRangeSubfieldSym &DefRangeSubfield) override;
208 Error visitKnownRecord(CVSymbol &Record, DefRangeSym &DefRange) override;
209 Error visitKnownRecord(CVSymbol &Record, FrameProcSym &FrameProc) override;
212 Error visitKnownRecord(CVSymbol &Record, ObjNameSym &ObjName) override;
213 Error visitKnownRecord(CVSymbol &Record, ProcSym &Proc) override;
216 Error visitKnownRecord(CVSymbol &Record, ScopeEndSym &ScopeEnd) override;
221 Error visitKnownRecord(CVSymbol &Record, CallerSym &Caller) override;
222};
223
224// Visitor for CodeView types and symbols to populate elements.
225class LVLogicalVisitor final {
226 LVCodeViewReader *Reader;
227 ScopedPrinter &W;
228
229 // Encapsulates access to the input file and any dependent type server,
230 // including any precompiled header object.
232 std::shared_ptr<llvm::pdb::InputFile> TypeServer = nullptr;
233 std::shared_ptr<LazyRandomTypeCollection> PrecompHeader = nullptr;
234
235 std::shared_ptr<LVShared> Shared;
236
237 // Object files have only one type stream that contains both types and ids.
238 // Precompiled header objects don't contain an IPI stream. Use the TPI.
239 LazyRandomTypeCollection &types() {
240 return TypeServer ? TypeServer->types()
241 : (PrecompHeader ? *PrecompHeader : Input.types());
242 }
244 return TypeServer ? TypeServer->ids()
245 : (PrecompHeader ? *PrecompHeader : Input.ids());
246 }
247
248 using LVScopeStack = std::stack<LVScope *>;
249 LVScopeStack ScopeStack;
250 LVScope *ReaderParent = nullptr;
251 LVScope *ReaderScope = nullptr;
252 bool InCompileUnitScope = false;
253
254 // Allow processing of argument list.
255 bool ProcessArgumentList = false;
256 StringRef OverloadedMethodName;
257 std::string CompileUnitName;
258
259 // Inlined functions source information.
260 using LVInlineeEntry = std::pair<uint32_t, StringRef>;
261 using LVInlineeInfo = std::map<TypeIndex, LVInlineeEntry>;
262 LVInlineeInfo InlineeInfo;
263
264 Error visitFieldListMemberStream(TypeIndex TI, LVElement *Element,
266
267 LVType *createBaseType(TypeIndex TI, StringRef TypeName);
268 LVType *createPointerType(TypeIndex TI, StringRef TypeName);
269 LVSymbol *createParameter(TypeIndex TI, StringRef Name, LVScope *Parent);
270 LVSymbol *createParameter(LVElement *Element, StringRef Name,
271 LVScope *Parent);
272 void createDataMember(CVMemberRecord &Record, LVScope *Parent, StringRef Name,
274 void createParents(StringRef ScopedName, LVElement *Element);
275
276public:
278 llvm::pdb::InputFile &Input);
279
280 // Current elements during the processing of a RecordType or RecordSymbol.
281 // They are shared with the SymbolVisitor.
285 LVType *CurrentType = nullptr;
286
287 // Input source in the case of type server or precompiled header.
288 void setInput(std::shared_ptr<llvm::pdb::InputFile> TypeServer) {
289 this->TypeServer = TypeServer;
290 }
291 void setInput(std::shared_ptr<LazyRandomTypeCollection> PrecompHeader) {
292 this->PrecompHeader = PrecompHeader;
293 }
294
296 InlineeInfo.emplace(std::piecewise_construct, std::forward_as_tuple(TI),
297 std::forward_as_tuple(LineNumber, Filename));
298 }
299
300 LLVM_ABI void printTypeIndex(StringRef FieldName, TypeIndex TI,
301 uint32_t StreamIdx);
302
306
307 // Break down the annotation byte code and calculate code and line offsets.
308 LLVM_ABI Error inlineSiteAnnotation(LVScope *AbstractFunction,
309 LVScope *InlinedFunction,
311
312 void pushScope(LVScope *Scope) {
313 ScopeStack.push(ReaderParent);
314 ReaderParent = ReaderScope;
315 ReaderScope = Scope;
316 }
317 void popScope() {
318 ReaderScope = ReaderParent;
319 ReaderParent = ScopeStack.top();
320 ScopeStack.pop();
321 }
322 void closeScope() {
323 if (InCompileUnitScope) {
324 InCompileUnitScope = false;
325 popScope();
326 }
327 }
328 void setRoot(LVScope *Root) { ReaderScope = Root; }
329
330 LLVM_ABI void addElement(LVScope *Scope, bool IsCompileUnit);
331 LLVM_ABI void addElement(LVSymbol *Symbol);
333
334 std::string getCompileUnitName() { return CompileUnitName; }
335 void setCompileUnitName(std::string Name) {
336 CompileUnitName = std::move(Name);
337 }
338
340 LVScope *Parent = nullptr);
341 LVShared *getShared() { return Shared.get(); }
342
343 LVScope *getReaderScope() const { return ReaderScope; }
344
346 uint32_t StreamIdx);
349 LVElement *Element, uint32_t StreamIdx);
351
352 void startProcessArgumentList() { ProcessArgumentList = true; }
353 void stopProcessArgumentList() { ProcessArgumentList = false; }
354
355 LLVM_ABI void processFiles();
356 LLVM_ABI void processLines();
358
359 LLVM_ABI void printRecords(raw_ostream &OS) const;
360
363 TypeIndex TI, LVElement *Element);
365 LVElement *Element);
367 TypeIndex TI, LVElement *Element);
369 TypeIndex TI, LVElement *Element);
371 TypeIndex TI, LVElement *Element);
373 TypeIndex TI, LVElement *Element);
375 TypeIndex TI, LVElement *Element);
377 TypeIndex TI, LVElement *Element);
379 LVElement *Element);
381 TypeIndex TI, LVElement *Element);
383 TypeIndex TI, LVElement *Element);
385 TypeIndex TI, LVElement *Element);
387 MethodOverloadListRecord &Overloads,
388 TypeIndex TI, LVElement *Element);
390 TypeIndex TI, LVElement *Element);
392 TypeIndex TI, LVElement *Element);
394 TypeIndex TI, LVElement *Element);
396 TypeIndex TI, LVElement *Element);
398 TypeIndex TI, LVElement *Element);
400 TypeIndex TI, LVElement *Element);
402 TypeIndex TI, LVElement *Element);
404 TypeIndex TI, LVElement *Element);
406 UdtSourceLineRecord &SourceLine, TypeIndex TI,
407 LVElement *Element);
409 UdtModSourceLineRecord &ModSourceLine,
410 TypeIndex TI, LVElement *Element);
412 TypeIndex TI, LVElement *Element);
414 TypeIndex TI, LVElement *Element);
415
418 TypeIndex TI, LVElement *Element);
421 LVElement *Element);
423 EnumeratorRecord &Enum, TypeIndex TI,
424 LVElement *Element);
427 LVElement *Element);
430 LVElement *Element);
432 OneMethodRecord &Method, TypeIndex TI,
433 LVElement *Element);
436 LVElement *Element);
439 LVElement *Element);
441 TypeIndex TI, LVElement *Element);
444 LVElement *Element);
445
446 template <typename T>
448 TypeVisitorCallbacks &Callbacks, TypeIndex TI,
449 LVElement *Element) {
450 TypeRecordKind RK = static_cast<TypeRecordKind>(Record.Kind);
451 T KnownRecord(RK);
452 if (Error Err = Callbacks.visitKnownMember(Record, KnownRecord))
453 return Err;
454 if (Error Err = visitKnownMember(Record, KnownRecord, TI, Element))
455 return Err;
456 return Error::success();
457 }
458
459 template <typename T>
461 TypeRecordKind RK = static_cast<TypeRecordKind>(Record.kind());
462 T KnownRecord(RK);
464 const_cast<CVType &>(Record), KnownRecord))
465 return Err;
466 if (Error Err = visitKnownRecord(Record, KnownRecord, TI, Element))
467 return Err;
468 return Error::success();
469 }
470
472 TypeVisitorCallbacks &Callbacks,
473 TypeIndex TI, LVElement *Element);
475 LVElement *Element);
476};
477
478} // namespace logicalview
479} // namespace llvm
480
481#endif // LLVM_DEBUGINFO_LOGICALVIEW_READERS_CODEVIEWVISITOR_H
static std::string getLinkageName(GlobalValue::LinkageTypes LT)
static Error visitKnownRecord(CVSymbol &Record, SymbolVisitorCallbacks &Callbacks)
#define LLVM_ABI
Definition Compiler.h:211
DXIL Resource Access
#define T
OptimizedStructLayoutField Field
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Provides read only access to a subclass of BinaryStream.
This is an important base class in LLVM.
Definition Constant.h:44
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
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
Represents a read-only view of a CodeView string table.
S_DEFRANGE_REGISTER_REL_INDIR.
Provides amortized O(1) random access to a CodeView type stream.
LF_INDEX - Used to chain two large LF_FIELDLIST or LF_METHODLIST records together.
Definition TypeRecord.h:926
For method overload sets. LF_METHOD.
Definition TypeRecord.h:776
static Error deserializeAs(CVType &CVT, T &Record)
A 32-bit type reference.
Definition TypeIndex.h:97
static TypeIndex None()
Definition TypeIndex.h:149
void addInlineeInfo(TypeIndex TI, uint32_t LineNumber, StringRef Filename)
LLVM_ABI Error visitKnownRecord(CVType &Record, ArgListRecord &Args, TypeIndex TI, LVElement *Element)
LLVM_ABI void printRecords(raw_ostream &OS) const
LLVM_ABI void printTypeEnd(CVType &Record)
LLVM_ABI Error visitMemberRecord(CVMemberRecord &Record, TypeVisitorCallbacks &Callbacks, TypeIndex TI, LVElement *Element)
LLVM_ABI Error visitKnownMember(CVMemberRecord &Record, BaseClassRecord &Base, TypeIndex TI, LVElement *Element)
LLVM_ABI void printMemberEnd(CVMemberRecord &Record)
void setCompileUnitName(std::string Name)
LLVM_ABI Error inlineSiteAnnotation(LVScope *AbstractFunction, LVScope *InlinedFunction, InlineSiteSym &InlineSite)
LLVM_ABI LVLogicalVisitor(LVCodeViewReader *Reader, ScopedPrinter &W, llvm::pdb::InputFile &Input)
LLVM_ABI void printTypeIndex(StringRef FieldName, TypeIndex TI, uint32_t StreamIdx)
LLVM_ABI Error visitUnknownMember(CVMemberRecord &Record, TypeIndex TI)
void setInput(std::shared_ptr< LazyRandomTypeCollection > PrecompHeader)
Error visitKnownMember(CVMemberRecord &Record, TypeVisitorCallbacks &Callbacks, TypeIndex TI, LVElement *Element)
LLVM_ABI Error visitUnknownType(CVType &Record, TypeIndex TI)
void setInput(std::shared_ptr< llvm::pdb::InputFile > TypeServer)
LLVM_ABI void addElement(LVScope *Scope, bool IsCompileUnit)
LLVM_ABI void printTypeBegin(CVType &Record, TypeIndex TI, LVElement *Element, uint32_t StreamIdx)
LLVM_ABI LVElement * getElement(uint32_t StreamIdx, TypeIndex TI, LVScope *Parent=nullptr)
LLVM_ABI void printMemberBegin(CVMemberRecord &Record, TypeIndex TI, LVElement *Element, uint32_t StreamIdx)
LLVM_ABI Error finishVisitation(CVType &Record, TypeIndex TI, LVElement *Element)
Error visitKnownRecord(CVType &Record, TypeIndex TI, LVElement *Element)
LLVM_ABI LVElement * createElement(TypeLeafKind Kind)
LVSymbolVisitorDelegate(LVCodeViewReader *Reader, const llvm::object::SectionRef &Section, const llvm::object::COFFObjectFile *Obj, StringRef SectionContents)
uint32_t getRecordOffset(BinaryStreamReader Reader) override
LVSymbolVisitor(LVCodeViewReader *Reader, ScopedPrinter &W, LVLogicalVisitor *LogicalVisitor, LazyRandomTypeCollection &Types, LazyRandomTypeCollection &Ids, LVSymbolVisitorDelegate *ObjDelegate, LVShared *Shared)
LVTypeVisitor(ScopedPrinter &W, LVLogicalVisitor *LogicalVisitor, LazyRandomTypeCollection &Types, LazyRandomTypeCollection &Ids, uint32_t StreamIdx, LVShared *Shared)
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
CVRecord< TypeLeafKind > CVType
Definition CVRecord.h:64
CVRecord< SymbolKind > CVSymbol
Definition CVRecord.h:65
MemberAccess
Source-level access specifier. (CV_access_e)
Definition CodeView.h:246
TypeLeafKind
Duplicate copy of the above enum, but using the official CV names.
Definition CodeView.h:34
TypeRecordKind
Distinguishes individual records in .debug$T or .debug$P section or PDB type stream.
Definition CodeView.h:27
SymbolKind
Duplicate copy of the above enum, but using the official CV names.
Definition CodeView.h:48
This is an optimization pass for GlobalISel generic memory operations.
std::tuple< uint64_t, uint32_t > InlineSite
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106