blob: 6e4d95af944a5deb09a2a4f8e0cbe2971dfeecce [file] [log] [blame]
Zachary Turner2d11c202015-02-27 09:15:59 +00001//===- LinePrinter.cpp ------------------------------------------*- 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 "LinePrinter.h"
11
Zachary Turnerbd336e42017-06-09 20:46:17 +000012#include "llvm-pdbutil.h"
Zachary Turner7797c722015-03-02 04:39:56 +000013
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000014#include "llvm/ADT/STLExtras.h"
Zachary Turner0b36c3e2017-06-23 18:52:13 +000015#include "llvm/DebugInfo/MSF/MSFCommon.h"
16#include "llvm/DebugInfo/MSF/MSFStreamLayout.h"
17#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
18#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
Zachary Turner4dc4f012017-04-13 21:11:00 +000019#include "llvm/DebugInfo/PDB/UDTLayout.h"
Zachary Turner0b36c3e2017-06-23 18:52:13 +000020#include "llvm/Support/BinaryStreamReader.h"
Zachary Turner63055452017-06-15 22:24:24 +000021#include "llvm/Support/Format.h"
Zachary Turner0b36c3e2017-06-23 18:52:13 +000022#include "llvm/Support/FormatAdapters.h"
23#include "llvm/Support/FormatVariadic.h"
Zachary Turnerf5abda22015-03-01 06:49:49 +000024#include "llvm/Support/Regex.h"
25
Zachary Turner2d11c202015-02-27 09:15:59 +000026#include <algorithm>
27
Zachary Turnerec28fc32016-05-04 20:32:13 +000028using namespace llvm;
Zachary Turner0b36c3e2017-06-23 18:52:13 +000029using namespace llvm::msf;
Zachary Turnerec28fc32016-05-04 20:32:13 +000030using namespace llvm::pdb;
31
Zachary Turner4dddcc62015-09-29 19:49:06 +000032namespace {
Zachary Turner4dddcc62015-09-29 19:49:06 +000033bool IsItemExcluded(llvm::StringRef Item,
34 std::list<llvm::Regex> &IncludeFilters,
35 std::list<llvm::Regex> &ExcludeFilters) {
36 if (Item.empty())
37 return false;
38
39 auto match_pred = [Item](llvm::Regex &R) { return R.match(Item); };
40
41 // Include takes priority over exclude. If the user specified include
42 // filters, and none of them include this item, them item is gone.
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000043 if (!IncludeFilters.empty() && !any_of(IncludeFilters, match_pred))
Zachary Turner4dddcc62015-09-29 19:49:06 +000044 return true;
45
Sanjoy Dasff3b8b42015-12-01 07:49:23 +000046 if (any_of(ExcludeFilters, match_pred))
Zachary Turner4dddcc62015-09-29 19:49:06 +000047 return true;
48
49 return false;
50}
51}
52
Zachary Turner2d11c202015-02-27 09:15:59 +000053using namespace llvm;
54
Adrian McCarthy1aa207d2017-03-23 15:28:15 +000055LinePrinter::LinePrinter(int Indent, bool UseColor, llvm::raw_ostream &Stream)
56 : OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +000057 SetFilters(ExcludeTypeFilters, opts::pretty::ExcludeTypes.begin(),
58 opts::pretty::ExcludeTypes.end());
59 SetFilters(ExcludeSymbolFilters, opts::pretty::ExcludeSymbols.begin(),
60 opts::pretty::ExcludeSymbols.end());
61 SetFilters(ExcludeCompilandFilters, opts::pretty::ExcludeCompilands.begin(),
62 opts::pretty::ExcludeCompilands.end());
Zachary Turner4dddcc62015-09-29 19:49:06 +000063
Zachary Turnera30bd1a2016-06-30 17:42:48 +000064 SetFilters(IncludeTypeFilters, opts::pretty::IncludeTypes.begin(),
65 opts::pretty::IncludeTypes.end());
66 SetFilters(IncludeSymbolFilters, opts::pretty::IncludeSymbols.begin(),
67 opts::pretty::IncludeSymbols.end());
68 SetFilters(IncludeCompilandFilters, opts::pretty::IncludeCompilands.begin(),
69 opts::pretty::IncludeCompilands.end());
Zachary Turner7797c722015-03-02 04:39:56 +000070}
Zachary Turner2d11c202015-02-27 09:15:59 +000071
Zachary Turner63055452017-06-15 22:24:24 +000072void LinePrinter::Indent(uint32_t Amount) {
73 if (Amount == 0)
74 Amount = IndentSpaces;
75 CurrentIndent += Amount;
76}
Zachary Turner2d11c202015-02-27 09:15:59 +000077
Zachary Turner63055452017-06-15 22:24:24 +000078void LinePrinter::Unindent(uint32_t Amount) {
79 if (Amount == 0)
80 Amount = IndentSpaces;
81 CurrentIndent = std::max<int>(0, CurrentIndent - Amount);
Zachary Turner2d11c202015-02-27 09:15:59 +000082}
83
84void LinePrinter::NewLine() {
85 OS << "\n";
86 OS.indent(CurrentIndent);
87}
88
Zachary Turner63055452017-06-15 22:24:24 +000089void LinePrinter::print(const Twine &T) { OS << T; }
90
91void LinePrinter::printLine(const Twine &T) {
92 NewLine();
93 OS << T;
94}
95
Zachary Turner4dc4f012017-04-13 21:11:00 +000096bool LinePrinter::IsClassExcluded(const ClassLayout &Class) {
Zachary Turner16901642017-04-24 17:47:24 +000097 if (IsTypeExcluded(Class.getName(), Class.getSize()))
Zachary Turner4dc4f012017-04-13 21:11:00 +000098 return true;
99 if (Class.deepPaddingSize() < opts::pretty::PaddingThreshold)
100 return true;
101 return false;
102}
103
Zachary Turner63055452017-06-15 22:24:24 +0000104void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
105 uint32_t StartOffset) {
106 NewLine();
107 OS << Label << " (";
108 if (!Data.empty()) {
109 OS << "\n";
110 OS << format_bytes_with_ascii(Data, StartOffset, 32, 4,
111 CurrentIndent + IndentSpaces, true);
112 NewLine();
113 }
114 OS << ")";
115}
116
Zachary Turner99402032017-06-22 20:58:11 +0000117void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
118 uint64_t Base, uint32_t StartOffset) {
119 NewLine();
120 OS << Label << " (";
121 if (!Data.empty()) {
122 OS << "\n";
123 Base += StartOffset;
124 OS << format_bytes_with_ascii(Data, Base, 32, 4,
125 CurrentIndent + IndentSpaces, true);
126 NewLine();
127 }
128 OS << ")";
129}
130
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000131namespace {
132struct Run {
133 Run() = default;
134 explicit Run(uint32_t Block) : Block(Block) {}
135 uint32_t Block = 0;
136 uint32_t ByteLen = 0;
137};
138} // namespace
139
140static std::vector<Run> computeBlockRuns(uint32_t BlockSize,
141 const msf::MSFStreamLayout &Layout) {
142 std::vector<Run> Runs;
143 if (Layout.Length == 0)
144 return Runs;
145
146 ArrayRef<support::ulittle32_t> Blocks = Layout.Blocks;
147 assert(!Blocks.empty());
148 uint32_t StreamBytesRemaining = Layout.Length;
Zachary Turnerdd739682017-06-23 21:11:54 +0000149 uint32_t CurrentBlock = Blocks[0];
150 Runs.emplace_back(CurrentBlock);
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000151 while (!Blocks.empty()) {
152 Run *CurrentRun = &Runs.back();
153 uint32_t NextBlock = Blocks.front();
Zachary Turnerdd739682017-06-23 21:11:54 +0000154 if (NextBlock < CurrentBlock || (NextBlock - CurrentBlock > 1)) {
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000155 Runs.emplace_back(NextBlock);
156 CurrentRun = &Runs.back();
157 }
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000158 uint32_t Used = std::min(BlockSize, StreamBytesRemaining);
159 CurrentRun->ByteLen += Used;
160 StreamBytesRemaining -= Used;
Zachary Turnerdd739682017-06-23 21:11:54 +0000161 CurrentBlock = NextBlock;
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000162 Blocks = Blocks.drop_front();
163 }
164 return Runs;
165}
166
167static std::pair<Run, uint32_t> findRun(uint32_t Offset, ArrayRef<Run> Runs) {
168 for (const auto &R : Runs) {
169 if (Offset < R.ByteLen)
170 return std::make_pair(R, Offset);
171 Offset -= R.ByteLen;
172 }
173 llvm_unreachable("Invalid offset!");
174}
175
176void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
177 uint32_t StreamIdx,
178 StringRef StreamPurpose, uint32_t Offset,
179 uint32_t Size) {
180 if (StreamIdx >= File.getNumStreams()) {
181 formatLine("Stream {0}: Not present", StreamIdx);
182 return;
183 }
184 if (Size + Offset > File.getStreamByteSize(StreamIdx)) {
185 formatLine(
186 "Stream {0}: Invalid offset and size, range out of stream bounds",
187 StreamIdx);
188 return;
189 }
190
191 auto S = MappedBlockStream::createIndexedStream(
192 File.getMsfLayout(), File.getMsfBuffer(), StreamIdx, File.getAllocator());
193 if (!S) {
194 NewLine();
195 formatLine("Stream {0}: Not present", StreamIdx);
196 return;
197 }
198
199 uint32_t End =
200 (Size == 0) ? S->getLength() : std::min(Offset + Size, S->getLength());
201 Size = End - Offset;
202
203 formatLine("Stream {0}: {1} (dumping {2:N} / {3:N} bytes)", StreamIdx,
204 StreamPurpose, Size, S->getLength());
205 AutoIndent Indent(*this);
206 BinaryStreamRef Slice(*S);
Zachary Turner6c3e41b2017-06-23 20:18:38 +0000207 BinarySubstreamRef Substream;
208 Substream.Offset = Offset;
209 Substream.StreamData = Slice.drop_front(Offset).keep_front(Size);
210
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000211 auto Layout = File.getStreamLayout(StreamIdx);
Zachary Turner6c3e41b2017-06-23 20:18:38 +0000212 formatMsfStreamData(Label, File, Layout, Substream);
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000213}
214
215void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
216 const msf::MSFStreamLayout &Stream,
217 BinarySubstreamRef Substream) {
218 BinaryStreamReader Reader(Substream.StreamData);
219
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000220 auto Runs = computeBlockRuns(File.getBlockSize(), Stream);
221
222 NewLine();
223 OS << Label << " (";
224 while (Reader.bytesRemaining() > 0) {
225 OS << "\n";
226
227 Run FoundRun;
228 uint32_t RunOffset;
Zachary Turner6c3e41b2017-06-23 20:18:38 +0000229 std::tie(FoundRun, RunOffset) = findRun(Substream.Offset, Runs);
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000230 assert(FoundRun.ByteLen >= RunOffset);
231 uint32_t Len = FoundRun.ByteLen - RunOffset;
232 Len = std::min(Len, Reader.bytesRemaining());
233 uint64_t Base = FoundRun.Block * File.getBlockSize() + RunOffset;
234 ArrayRef<uint8_t> Data;
235 consumeError(Reader.readBytes(Data, Len));
236 OS << format_bytes_with_ascii(Data, Base, 32, 4,
237 CurrentIndent + IndentSpaces, true);
238 if (Reader.bytesRemaining() > 0) {
239 NewLine();
240 OS << formatv(" {0}",
241 fmt_align("<discontinuity>", AlignStyle::Center, 114, '-'));
242 }
Zachary Turner6c3e41b2017-06-23 20:18:38 +0000243 Substream.Offset += Len;
Zachary Turner0b36c3e2017-06-23 18:52:13 +0000244 }
245 NewLine();
246 OS << ")";
247}
248
Zachary Turner4dc4f012017-04-13 21:11:00 +0000249bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size) {
250 if (IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters))
251 return true;
252 if (Size < opts::pretty::SizeThreshold)
253 return true;
254 return false;
Zachary Turnerf5abda22015-03-01 06:49:49 +0000255}
256
257bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
Zachary Turner4dddcc62015-09-29 19:49:06 +0000258 return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
Zachary Turnerf5abda22015-03-01 06:49:49 +0000259}
260
261bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
Zachary Turner4dddcc62015-09-29 19:49:06 +0000262 return IsItemExcluded(CompilandName, IncludeCompilandFilters,
263 ExcludeCompilandFilters);
Zachary Turnerf5abda22015-03-01 06:49:49 +0000264}
265
Adrian McCarthy5fcfc2c2017-03-29 17:11:27 +0000266WithColor::WithColor(LinePrinter &P, PDB_ColorItem C)
267 : OS(P.OS), UseColor(P.hasColor()) {
268 if (UseColor)
Adrian McCarthy1aa207d2017-03-23 15:28:15 +0000269 applyColor(C);
Zachary Turner2d11c202015-02-27 09:15:59 +0000270}
271
Adrian McCarthy5fcfc2c2017-03-29 17:11:27 +0000272WithColor::~WithColor() {
273 if (UseColor)
274 OS.resetColor();
275}
Zachary Turner2d11c202015-02-27 09:15:59 +0000276
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000277void WithColor::applyColor(PDB_ColorItem C) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000278 switch (C) {
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000279 case PDB_ColorItem::None:
280 OS.resetColor();
281 return;
Zachary Turner0c990bbe2017-04-10 19:33:29 +0000282 case PDB_ColorItem::Comment:
283 OS.changeColor(raw_ostream::GREEN, false);
284 return;
Zachary Turner2d11c202015-02-27 09:15:59 +0000285 case PDB_ColorItem::Address:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000286 OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000287 return;
288 case PDB_ColorItem::Keyword:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000289 OS.changeColor(raw_ostream::MAGENTA, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000290 return;
Zachary Turner7797c722015-03-02 04:39:56 +0000291 case PDB_ColorItem::Register:
Zachary Turner2d11c202015-02-27 09:15:59 +0000292 case PDB_ColorItem::Offset:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000293 OS.changeColor(raw_ostream::YELLOW, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000294 return;
295 case PDB_ColorItem::Type:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000296 OS.changeColor(raw_ostream::CYAN, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000297 return;
298 case PDB_ColorItem::Identifier:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000299 OS.changeColor(raw_ostream::CYAN, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000300 return;
301 case PDB_ColorItem::Path:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000302 OS.changeColor(raw_ostream::CYAN, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000303 return;
Zachary Turner0c990bbe2017-04-10 19:33:29 +0000304 case PDB_ColorItem::Padding:
Zachary Turner2d11c202015-02-27 09:15:59 +0000305 case PDB_ColorItem::SectionHeader:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000306 OS.changeColor(raw_ostream::RED, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000307 return;
308 case PDB_ColorItem::LiteralValue:
Rui Ueyamafa05aac2015-11-03 01:04:44 +0000309 OS.changeColor(raw_ostream::GREEN, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000310 return;
311 }
312}