]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/llvm-pdbutil/PrettyCompilandDumper.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / llvm-pdbutil / PrettyCompilandDumper.cpp
1 //===- PrettyCompilandDumper.cpp - llvm-pdbutil compiland dumper -*- C++ *-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "PrettyCompilandDumper.h"
11
12 #include "LinePrinter.h"
13 #include "PrettyFunctionDumper.h"
14 #include "llvm-pdbutil.h"
15
16 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
17 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
18 #include "llvm/DebugInfo/PDB/IPDBSession.h"
19 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
20 #include "llvm/DebugInfo/PDB/PDBExtras.h"
21 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
25 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
26 #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
27 #include "llvm/DebugInfo/PDB/PDBSymbolLabel.h"
28 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
29 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
30 #include "llvm/DebugInfo/PDB/PDBSymbolUnknown.h"
31 #include "llvm/DebugInfo/PDB/PDBSymbolUsingNamespace.h"
32 #include "llvm/Support/Format.h"
33 #include "llvm/Support/Path.h"
34 #include "llvm/Support/raw_ostream.h"
35
36 #include <utility>
37
38 using namespace llvm;
39 using namespace llvm::pdb;
40
41 CompilandDumper::CompilandDumper(LinePrinter &P)
42     : PDBSymDumper(true), Printer(P) {}
43
44 void CompilandDumper::dump(const PDBSymbolCompilandDetails &Symbol) {}
45
46 void CompilandDumper::dump(const PDBSymbolCompilandEnv &Symbol) {}
47
48 void CompilandDumper::start(const PDBSymbolCompiland &Symbol,
49                             CompilandDumpFlags opts) {
50   std::string FullName = Symbol.getName();
51   if (Printer.IsCompilandExcluded(FullName))
52     return;
53
54   Printer.NewLine();
55   WithColor(Printer, PDB_ColorItem::Path).get() << FullName;
56
57   if (opts & Flags::Lines) {
58     const IPDBSession &Session = Symbol.getSession();
59     if (auto Files = Session.getSourceFilesForCompiland(Symbol)) {
60       Printer.Indent();
61       while (auto File = Files->getNext()) {
62         Printer.NewLine();
63         WithColor(Printer, PDB_ColorItem::Path).get() << File->getFileName();
64         if (File->getChecksumType() != PDB_Checksum::None) {
65           auto ChecksumType = File->getChecksumType();
66           auto ChecksumHexString = toHex(File->getChecksum());
67           WithColor(Printer, PDB_ColorItem::Comment).get()
68               << " (" << ChecksumType << ": " << ChecksumHexString << ")";
69         }
70
71         auto Lines = Session.findLineNumbers(Symbol, *File);
72         if (!Lines)
73           continue;
74
75         Printer.Indent();
76         while (auto Line = Lines->getNext()) {
77           Printer.NewLine();
78           uint32_t LineStart = Line->getLineNumber();
79           uint32_t LineEnd = Line->getLineNumberEnd();
80
81           Printer << "Line ";
82           PDB_ColorItem StatementColor = Line->isStatement()
83             ? PDB_ColorItem::Keyword
84             : PDB_ColorItem::LiteralValue;
85           WithColor(Printer, StatementColor).get() << LineStart;
86           if (LineStart != LineEnd)
87             WithColor(Printer, StatementColor).get() << " - " << LineEnd;
88
89           uint32_t ColumnStart = Line->getColumnNumber();
90           uint32_t ColumnEnd = Line->getColumnNumberEnd();
91           if (ColumnStart != 0 || ColumnEnd != 0) {
92             Printer << ", Column: ";
93             WithColor(Printer, StatementColor).get() << ColumnStart;
94             if (ColumnEnd != ColumnStart)
95               WithColor(Printer, StatementColor).get() << " - " << ColumnEnd;
96           }
97
98           Printer << ", Address: ";
99           if (Line->getLength() > 0) {
100             uint64_t AddrStart = Line->getVirtualAddress();
101             uint64_t AddrEnd = AddrStart + Line->getLength() - 1;
102             WithColor(Printer, PDB_ColorItem::Address).get()
103               << "[" << format_hex(AddrStart, 10) << " - "
104               << format_hex(AddrEnd, 10) << "]";
105             Printer << " (" << Line->getLength() << " bytes)";
106           } else {
107             uint64_t AddrStart = Line->getVirtualAddress();
108             WithColor(Printer, PDB_ColorItem::Address).get()
109               << "[" << format_hex(AddrStart, 10) << "] ";
110             Printer << "(0 bytes)";
111           }
112         }
113         Printer.Unindent();
114       }
115       Printer.Unindent();
116     }
117   }
118
119   if (opts & Flags::Children) {
120     if (auto ChildrenEnum = Symbol.findAllChildren()) {
121       Printer.Indent();
122       while (auto Child = ChildrenEnum->getNext())
123         Child->dump(*this);
124       Printer.Unindent();
125     }
126   }
127 }
128
129 void CompilandDumper::dump(const PDBSymbolData &Symbol) {
130   if (!shouldDumpSymLevel(opts::pretty::SymLevel::Data))
131     return;
132   if (Printer.IsSymbolExcluded(Symbol.getName()))
133     return;
134
135   Printer.NewLine();
136
137   switch (auto LocType = Symbol.getLocationType()) {
138   case PDB_LocType::Static:
139     Printer << "data: ";
140     WithColor(Printer, PDB_ColorItem::Address).get()
141         << "[" << format_hex(Symbol.getVirtualAddress(), 10) << "]";
142
143     WithColor(Printer, PDB_ColorItem::Comment).get()
144         << " [sizeof = " << getTypeLength(Symbol) << "]";
145
146     break;
147   case PDB_LocType::Constant:
148     Printer << "constant: ";
149     WithColor(Printer, PDB_ColorItem::LiteralValue).get()
150         << "[" << Symbol.getValue() << "]";
151     WithColor(Printer, PDB_ColorItem::Comment).get()
152         << " [sizeof = " << getTypeLength(Symbol) << "]";
153     break;
154   default:
155     Printer << "data(unexpected type=" << LocType << ")";
156   }
157
158   Printer << " ";
159   WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
160 }
161
162 void CompilandDumper::dump(const PDBSymbolFunc &Symbol) {
163   if (!shouldDumpSymLevel(opts::pretty::SymLevel::Functions))
164     return;
165   if (Symbol.getLength() == 0)
166     return;
167   if (Printer.IsSymbolExcluded(Symbol.getName()))
168     return;
169
170   Printer.NewLine();
171   FunctionDumper Dumper(Printer);
172   Dumper.start(Symbol, FunctionDumper::PointerType::None);
173 }
174
175 void CompilandDumper::dump(const PDBSymbolLabel &Symbol) {
176   if (Printer.IsSymbolExcluded(Symbol.getName()))
177     return;
178
179   Printer.NewLine();
180   Printer << "label ";
181   WithColor(Printer, PDB_ColorItem::Address).get()
182       << "[" << format_hex(Symbol.getVirtualAddress(), 10) << "] ";
183   WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
184 }
185
186 void CompilandDumper::dump(const PDBSymbolThunk &Symbol) {
187   if (!shouldDumpSymLevel(opts::pretty::SymLevel::Thunks))
188     return;
189   if (Printer.IsSymbolExcluded(Symbol.getName()))
190     return;
191
192   Printer.NewLine();
193   Printer << "thunk ";
194   codeview::ThunkOrdinal Ordinal = Symbol.getThunkOrdinal();
195   uint64_t VA = Symbol.getVirtualAddress();
196   if (Ordinal == codeview::ThunkOrdinal::TrampIncremental) {
197     uint64_t Target = Symbol.getTargetVirtualAddress();
198     WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(VA, 10);
199     Printer << " -> ";
200     WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(Target, 10);
201   } else {
202     WithColor(Printer, PDB_ColorItem::Address).get()
203         << "[" << format_hex(VA, 10) << " - "
204         << format_hex(VA + Symbol.getLength(), 10) << "]";
205   }
206   Printer << " (";
207   WithColor(Printer, PDB_ColorItem::Register).get() << Ordinal;
208   Printer << ") ";
209   std::string Name = Symbol.getName();
210   if (!Name.empty())
211     WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
212 }
213
214 void CompilandDumper::dump(const PDBSymbolTypeTypedef &Symbol) {}
215
216 void CompilandDumper::dump(const PDBSymbolUnknown &Symbol) {
217   Printer.NewLine();
218   Printer << "unknown (" << Symbol.getSymTag() << ")";
219 }
220
221 void CompilandDumper::dump(const PDBSymbolUsingNamespace &Symbol) {
222   if (Printer.IsSymbolExcluded(Symbol.getName()))
223     return;
224
225   Printer.NewLine();
226   Printer << "using namespace ";
227   std::string Name = Symbol.getName();
228   WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
229 }