blob: 94a0b2d5e780af3abdc617134ffa5ceaf44002ff [file] [log] [blame]
Zachary Turnercfb13562017-06-09 20:46:17 +00001//===- PrettyCompilandDumper.cpp - llvm-pdbutil compiland dumper -*- C++ *-===//
Zachary Turner395adf92015-02-22 22:03:38 +00002//
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
Zachary Turnereb6ab042017-01-11 00:35:43 +000010#include "PrettyCompilandDumper.h"
11
Zachary Turner756b8232015-02-27 09:15:59 +000012#include "LinePrinter.h"
Zachary Turnereb6ab042017-01-11 00:35:43 +000013#include "PrettyFunctionDumper.h"
Zachary Turnercfb13562017-06-09 20:46:17 +000014#include "llvm-pdbutil.h"
Zachary Turner395adf92015-02-22 22:03:38 +000015
16#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera20f3bc2016-03-08 21:42:24 +000017#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
Zachary Turner395adf92015-02-22 22:03:38 +000018#include "llvm/DebugInfo/PDB/IPDBSession.h"
Zachary Turnera20f3bc2016-03-08 21:42:24 +000019#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
Zachary Turner395adf92015-02-22 22:03:38 +000020#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"
Aaron Smith7034ff82018-10-11 21:37:18 +000031#include "llvm/DebugInfo/PDB/PDBSymbolUsingNamespace.h"
Zachary Turner395adf92015-02-22 22:03:38 +000032#include "llvm/Support/Format.h"
33#include "llvm/Support/Path.h"
34#include "llvm/Support/raw_ostream.h"
35
Zachary Turner395adf92015-02-22 22:03:38 +000036#include <utility>
Zachary Turner395adf92015-02-22 22:03:38 +000037
38using namespace llvm;
Zachary Turnerc95df942016-05-04 20:32:13 +000039using namespace llvm::pdb;
Zachary Turner395adf92015-02-22 22:03:38 +000040
Zachary Turner756b8232015-02-27 09:15:59 +000041CompilandDumper::CompilandDumper(LinePrinter &P)
Zachary Turnerb6fca0b2015-02-27 09:53:55 +000042 : PDBSymDumper(true), Printer(P) {}
Zachary Turner395adf92015-02-22 22:03:38 +000043
Zachary Turner53e4b562015-03-01 06:51:29 +000044void CompilandDumper::dump(const PDBSymbolCompilandDetails &Symbol) {}
Zachary Turner395adf92015-02-22 22:03:38 +000045
Zachary Turner53e4b562015-03-01 06:51:29 +000046void CompilandDumper::dump(const PDBSymbolCompilandEnv &Symbol) {}
Zachary Turner395adf92015-02-22 22:03:38 +000047
Zachary Turnera20f3bc2016-03-08 21:42:24 +000048void CompilandDumper::start(const PDBSymbolCompiland &Symbol,
49 CompilandDumpFlags opts) {
Zachary Turner395adf92015-02-22 22:03:38 +000050 std::string FullName = Symbol.getName();
Zachary Turner91491392015-03-01 06:49:49 +000051 if (Printer.IsCompilandExcluded(FullName))
52 return;
53
Zachary Turner756b8232015-02-27 09:15:59 +000054 Printer.NewLine();
55 WithColor(Printer, PDB_ColorItem::Path).get() << FullName;
Zachary Turner395adf92015-02-22 22:03:38 +000056
Zachary Turnera20f3bc2016-03-08 21:42:24 +000057 if (opts & Flags::Lines) {
58 const IPDBSession &Session = Symbol.getSession();
Aaron Smith71e93df2018-03-07 02:23:08 +000059 if (auto Files = Session.getSourceFilesForCompiland(Symbol)) {
Zachary Turnera20f3bc2016-03-08 21:42:24 +000060 Printer.Indent();
Aaron Smith71e93df2018-03-07 02:23:08 +000061 while (auto File = Files->getNext()) {
Zachary Turnera20f3bc2016-03-08 21:42:24 +000062 Printer.NewLine();
Aaron Smith71e93df2018-03-07 02:23:08 +000063 WithColor(Printer, PDB_ColorItem::Path).get() << File->getFileName();
Aaron Smithd9438b82018-04-10 14:47:12 +000064 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 }
Zachary Turnera20f3bc2016-03-08 21:42:24 +000070
Aaron Smith71e93df2018-03-07 02:23:08 +000071 auto Lines = Session.findLineNumbers(Symbol, *File);
72 if (!Lines)
73 continue;
Zachary Turnera20f3bc2016-03-08 21:42:24 +000074
Aaron Smith71e93df2018-03-07 02:23:08 +000075 Printer.Indent();
76 while (auto Line = Lines->getNext()) {
77 Printer.NewLine();
78 uint32_t LineStart = Line->getLineNumber();
79 uint32_t LineEnd = Line->getLineNumberEnd();
Adrian McCarthy03c1f4b2016-08-17 23:01:03 +000080
Aaron Smith71e93df2018-03-07 02:23:08 +000081 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()
Zachary Turnera20f3bc2016-03-08 21:42:24 +0000103 << "[" << format_hex(AddrStart, 10) << " - "
104 << format_hex(AddrEnd, 10) << "]";
Aaron Smith71e93df2018-03-07 02:23:08 +0000105 Printer << " (" << Line->getLength() << " bytes)";
106 } else {
107 uint64_t AddrStart = Line->getVirtualAddress();
108 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turnera20f3bc2016-03-08 21:42:24 +0000109 << "[" << format_hex(AddrStart, 10) << "] ";
Aaron Smith71e93df2018-03-07 02:23:08 +0000110 Printer << "(0 bytes)";
111 }
Zachary Turnera20f3bc2016-03-08 21:42:24 +0000112 }
Aaron Smith71e93df2018-03-07 02:23:08 +0000113 Printer.Unindent();
Zachary Turnera20f3bc2016-03-08 21:42:24 +0000114 }
115 Printer.Unindent();
116 }
Zachary Turnera20f3bc2016-03-08 21:42:24 +0000117 }
118
119 if (opts & Flags::Children) {
Aaron Smith71e93df2018-03-07 02:23:08 +0000120 if (auto ChildrenEnum = Symbol.findAllChildren()) {
121 Printer.Indent();
122 while (auto Child = ChildrenEnum->getNext())
123 Child->dump(*this);
124 Printer.Unindent();
125 }
Zachary Turnera20f3bc2016-03-08 21:42:24 +0000126 }
Zachary Turner395adf92015-02-22 22:03:38 +0000127}
128
Zachary Turner53e4b562015-03-01 06:51:29 +0000129void CompilandDumper::dump(const PDBSymbolData &Symbol) {
Zachary Turner4afda8f2017-05-14 01:13:40 +0000130 if (!shouldDumpSymLevel(opts::pretty::SymLevel::Data))
131 return;
Zachary Turner91491392015-03-01 06:49:49 +0000132 if (Printer.IsSymbolExcluded(Symbol.getName()))
133 return;
134
Zachary Turner756b8232015-02-27 09:15:59 +0000135 Printer.NewLine();
136
Zachary Turner395adf92015-02-22 22:03:38 +0000137 switch (auto LocType = Symbol.getLocationType()) {
138 case PDB_LocType::Static:
Zachary Turner756b8232015-02-27 09:15:59 +0000139 Printer << "data: ";
140 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turner7c69a582015-05-01 20:24:26 +0000141 << "[" << format_hex(Symbol.getVirtualAddress(), 10) << "]";
Zachary Turner4afda8f2017-05-14 01:13:40 +0000142
143 WithColor(Printer, PDB_ColorItem::Comment).get()
144 << " [sizeof = " << getTypeLength(Symbol) << "]";
145
Zachary Turner395adf92015-02-22 22:03:38 +0000146 break;
147 case PDB_LocType::Constant:
Zachary Turner756b8232015-02-27 09:15:59 +0000148 Printer << "constant: ";
149 WithColor(Printer, PDB_ColorItem::LiteralValue).get()
150 << "[" << Symbol.getValue() << "]";
Zachary Turner4afda8f2017-05-14 01:13:40 +0000151 WithColor(Printer, PDB_ColorItem::Comment).get()
152 << " [sizeof = " << getTypeLength(Symbol) << "]";
Zachary Turner395adf92015-02-22 22:03:38 +0000153 break;
154 default:
Zachary Turner756b8232015-02-27 09:15:59 +0000155 Printer << "data(unexpected type=" << LocType << ")";
Zachary Turner395adf92015-02-22 22:03:38 +0000156 }
157
Zachary Turner756b8232015-02-27 09:15:59 +0000158 Printer << " ";
159 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner395adf92015-02-22 22:03:38 +0000160}
161
Zachary Turner53e4b562015-03-01 06:51:29 +0000162void CompilandDumper::dump(const PDBSymbolFunc &Symbol) {
Zachary Turner4afda8f2017-05-14 01:13:40 +0000163 if (!shouldDumpSymLevel(opts::pretty::SymLevel::Functions))
164 return;
Zachary Turner92d755e2015-02-23 05:58:34 +0000165 if (Symbol.getLength() == 0)
166 return;
Zachary Turner91491392015-03-01 06:49:49 +0000167 if (Printer.IsSymbolExcluded(Symbol.getName()))
168 return;
Zachary Turner395adf92015-02-22 22:03:38 +0000169
Zachary Turner756b8232015-02-27 09:15:59 +0000170 Printer.NewLine();
171 FunctionDumper Dumper(Printer);
Zachary Turner53e4b562015-03-01 06:51:29 +0000172 Dumper.start(Symbol, FunctionDumper::PointerType::None);
Zachary Turner395adf92015-02-22 22:03:38 +0000173}
174
Zachary Turner53e4b562015-03-01 06:51:29 +0000175void CompilandDumper::dump(const PDBSymbolLabel &Symbol) {
Zachary Turner91491392015-03-01 06:49:49 +0000176 if (Printer.IsSymbolExcluded(Symbol.getName()))
177 return;
178
Zachary Turner756b8232015-02-27 09:15:59 +0000179 Printer.NewLine();
180 Printer << "label ";
181 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turner7c69a582015-05-01 20:24:26 +0000182 << "[" << format_hex(Symbol.getVirtualAddress(), 10) << "] ";
Zachary Turner756b8232015-02-27 09:15:59 +0000183 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
Zachary Turner395adf92015-02-22 22:03:38 +0000184}
185
Zachary Turner53e4b562015-03-01 06:51:29 +0000186void CompilandDumper::dump(const PDBSymbolThunk &Symbol) {
Zachary Turner4afda8f2017-05-14 01:13:40 +0000187 if (!shouldDumpSymLevel(opts::pretty::SymLevel::Thunks))
188 return;
Zachary Turner91491392015-03-01 06:49:49 +0000189 if (Printer.IsSymbolExcluded(Symbol.getName()))
190 return;
191
Zachary Turner756b8232015-02-27 09:15:59 +0000192 Printer.NewLine();
193 Printer << "thunk ";
Zachary Turner2d4f33a2016-05-24 22:58:46 +0000194 codeview::ThunkOrdinal Ordinal = Symbol.getThunkOrdinal();
Zachary Turner7c69a582015-05-01 20:24:26 +0000195 uint64_t VA = Symbol.getVirtualAddress();
Zachary Turner2d4f33a2016-05-24 22:58:46 +0000196 if (Ordinal == codeview::ThunkOrdinal::TrampIncremental) {
Zachary Turner7c69a582015-05-01 20:24:26 +0000197 uint64_t Target = Symbol.getTargetVirtualAddress();
198 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(VA, 10);
Zachary Turner756b8232015-02-27 09:15:59 +0000199 Printer << " -> ";
200 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(Target, 10);
Zachary Turner395adf92015-02-22 22:03:38 +0000201 } else {
Zachary Turner756b8232015-02-27 09:15:59 +0000202 WithColor(Printer, PDB_ColorItem::Address).get()
Zachary Turner7c69a582015-05-01 20:24:26 +0000203 << "[" << format_hex(VA, 10) << " - "
204 << format_hex(VA + Symbol.getLength(), 10) << "]";
Zachary Turner395adf92015-02-22 22:03:38 +0000205 }
Zachary Turner0c7c98a2015-03-02 04:39:56 +0000206 Printer << " (";
207 WithColor(Printer, PDB_ColorItem::Register).get() << Ordinal;
208 Printer << ") ";
Zachary Turner395adf92015-02-22 22:03:38 +0000209 std::string Name = Symbol.getName();
210 if (!Name.empty())
Zachary Turner756b8232015-02-27 09:15:59 +0000211 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
Zachary Turner395adf92015-02-22 22:03:38 +0000212}
213
Zachary Turner53e4b562015-03-01 06:51:29 +0000214void CompilandDumper::dump(const PDBSymbolTypeTypedef &Symbol) {}
Zachary Turner395adf92015-02-22 22:03:38 +0000215
Zachary Turner53e4b562015-03-01 06:51:29 +0000216void CompilandDumper::dump(const PDBSymbolUnknown &Symbol) {
Zachary Turner756b8232015-02-27 09:15:59 +0000217 Printer.NewLine();
218 Printer << "unknown (" << Symbol.getSymTag() << ")";
Zachary Turner395adf92015-02-22 22:03:38 +0000219}
Aaron Smith7034ff82018-10-11 21:37:18 +0000220
221void 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}