2015-02-27 10:15:59 +01:00
|
|
|
//===- LinePrinter.cpp ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-02-27 10:15:59 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "LinePrinter.h"
|
|
|
|
|
2017-06-09 22:46:17 +02:00
|
|
|
#include "llvm-pdbutil.h"
|
2015-03-02 05:39:56 +01:00
|
|
|
|
2015-12-01 08:49:23 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-06-23 20:52:13 +02:00
|
|
|
#include "llvm/DebugInfo/MSF/MSFCommon.h"
|
|
|
|
#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
|
2017-04-13 23:11:00 +02:00
|
|
|
#include "llvm/DebugInfo/PDB/UDTLayout.h"
|
2017-06-23 20:52:13 +02:00
|
|
|
#include "llvm/Support/BinaryStreamReader.h"
|
2017-06-16 00:24:24 +02:00
|
|
|
#include "llvm/Support/Format.h"
|
2017-06-23 20:52:13 +02:00
|
|
|
#include "llvm/Support/FormatAdapters.h"
|
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2015-03-01 07:49:49 +01:00
|
|
|
#include "llvm/Support/Regex.h"
|
|
|
|
|
2015-02-27 10:15:59 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-05-04 22:32:13 +02:00
|
|
|
using namespace llvm;
|
2017-06-23 20:52:13 +02:00
|
|
|
using namespace llvm::msf;
|
2016-05-04 22:32:13 +02:00
|
|
|
using namespace llvm::pdb;
|
|
|
|
|
2015-09-29 21:49:06 +02:00
|
|
|
namespace {
|
|
|
|
bool IsItemExcluded(llvm::StringRef Item,
|
|
|
|
std::list<llvm::Regex> &IncludeFilters,
|
|
|
|
std::list<llvm::Regex> &ExcludeFilters) {
|
|
|
|
if (Item.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto match_pred = [Item](llvm::Regex &R) { return R.match(Item); };
|
|
|
|
|
|
|
|
// Include takes priority over exclude. If the user specified include
|
|
|
|
// filters, and none of them include this item, them item is gone.
|
2015-12-01 08:49:23 +01:00
|
|
|
if (!IncludeFilters.empty() && !any_of(IncludeFilters, match_pred))
|
2015-09-29 21:49:06 +02:00
|
|
|
return true;
|
|
|
|
|
2015-12-01 08:49:23 +01:00
|
|
|
if (any_of(ExcludeFilters, match_pred))
|
2015-09-29 21:49:06 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 10:15:59 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-03-23 16:28:15 +01:00
|
|
|
LinePrinter::LinePrinter(int Indent, bool UseColor, llvm::raw_ostream &Stream)
|
|
|
|
: OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor) {
|
2016-06-30 19:42:48 +02:00
|
|
|
SetFilters(ExcludeTypeFilters, opts::pretty::ExcludeTypes.begin(),
|
|
|
|
opts::pretty::ExcludeTypes.end());
|
|
|
|
SetFilters(ExcludeSymbolFilters, opts::pretty::ExcludeSymbols.begin(),
|
|
|
|
opts::pretty::ExcludeSymbols.end());
|
|
|
|
SetFilters(ExcludeCompilandFilters, opts::pretty::ExcludeCompilands.begin(),
|
|
|
|
opts::pretty::ExcludeCompilands.end());
|
|
|
|
|
|
|
|
SetFilters(IncludeTypeFilters, opts::pretty::IncludeTypes.begin(),
|
|
|
|
opts::pretty::IncludeTypes.end());
|
|
|
|
SetFilters(IncludeSymbolFilters, opts::pretty::IncludeSymbols.begin(),
|
|
|
|
opts::pretty::IncludeSymbols.end());
|
|
|
|
SetFilters(IncludeCompilandFilters, opts::pretty::IncludeCompilands.begin(),
|
|
|
|
opts::pretty::IncludeCompilands.end());
|
2015-03-02 05:39:56 +01:00
|
|
|
}
|
2015-02-27 10:15:59 +01:00
|
|
|
|
2017-06-16 00:24:24 +02:00
|
|
|
void LinePrinter::Indent(uint32_t Amount) {
|
|
|
|
if (Amount == 0)
|
|
|
|
Amount = IndentSpaces;
|
|
|
|
CurrentIndent += Amount;
|
|
|
|
}
|
2015-02-27 10:15:59 +01:00
|
|
|
|
2017-06-16 00:24:24 +02:00
|
|
|
void LinePrinter::Unindent(uint32_t Amount) {
|
|
|
|
if (Amount == 0)
|
|
|
|
Amount = IndentSpaces;
|
|
|
|
CurrentIndent = std::max<int>(0, CurrentIndent - Amount);
|
2015-02-27 10:15:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinePrinter::NewLine() {
|
|
|
|
OS << "\n";
|
|
|
|
OS.indent(CurrentIndent);
|
|
|
|
}
|
|
|
|
|
2017-06-16 00:24:24 +02:00
|
|
|
void LinePrinter::print(const Twine &T) { OS << T; }
|
|
|
|
|
|
|
|
void LinePrinter::printLine(const Twine &T) {
|
|
|
|
NewLine();
|
|
|
|
OS << T;
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:11:00 +02:00
|
|
|
bool LinePrinter::IsClassExcluded(const ClassLayout &Class) {
|
2017-04-24 19:47:24 +02:00
|
|
|
if (IsTypeExcluded(Class.getName(), Class.getSize()))
|
2017-04-13 23:11:00 +02:00
|
|
|
return true;
|
|
|
|
if (Class.deepPaddingSize() < opts::pretty::PaddingThreshold)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-16 00:24:24 +02:00
|
|
|
void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
|
|
|
|
uint32_t StartOffset) {
|
|
|
|
NewLine();
|
|
|
|
OS << Label << " (";
|
|
|
|
if (!Data.empty()) {
|
|
|
|
OS << "\n";
|
|
|
|
OS << format_bytes_with_ascii(Data, StartOffset, 32, 4,
|
|
|
|
CurrentIndent + IndentSpaces, true);
|
|
|
|
NewLine();
|
|
|
|
}
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
2017-06-22 22:58:11 +02:00
|
|
|
void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
|
|
|
|
uint64_t Base, uint32_t StartOffset) {
|
|
|
|
NewLine();
|
|
|
|
OS << Label << " (";
|
|
|
|
if (!Data.empty()) {
|
|
|
|
OS << "\n";
|
|
|
|
Base += StartOffset;
|
|
|
|
OS << format_bytes_with_ascii(Data, Base, 32, 4,
|
|
|
|
CurrentIndent + IndentSpaces, true);
|
|
|
|
NewLine();
|
|
|
|
}
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
2017-06-23 20:52:13 +02:00
|
|
|
namespace {
|
|
|
|
struct Run {
|
|
|
|
Run() = default;
|
|
|
|
explicit Run(uint32_t Block) : Block(Block) {}
|
|
|
|
uint32_t Block = 0;
|
|
|
|
uint32_t ByteLen = 0;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static std::vector<Run> computeBlockRuns(uint32_t BlockSize,
|
|
|
|
const msf::MSFStreamLayout &Layout) {
|
|
|
|
std::vector<Run> Runs;
|
|
|
|
if (Layout.Length == 0)
|
|
|
|
return Runs;
|
|
|
|
|
|
|
|
ArrayRef<support::ulittle32_t> Blocks = Layout.Blocks;
|
|
|
|
assert(!Blocks.empty());
|
|
|
|
uint32_t StreamBytesRemaining = Layout.Length;
|
2017-06-23 23:11:54 +02:00
|
|
|
uint32_t CurrentBlock = Blocks[0];
|
|
|
|
Runs.emplace_back(CurrentBlock);
|
2017-06-23 20:52:13 +02:00
|
|
|
while (!Blocks.empty()) {
|
|
|
|
Run *CurrentRun = &Runs.back();
|
|
|
|
uint32_t NextBlock = Blocks.front();
|
2017-06-23 23:11:54 +02:00
|
|
|
if (NextBlock < CurrentBlock || (NextBlock - CurrentBlock > 1)) {
|
2017-06-23 20:52:13 +02:00
|
|
|
Runs.emplace_back(NextBlock);
|
|
|
|
CurrentRun = &Runs.back();
|
|
|
|
}
|
|
|
|
uint32_t Used = std::min(BlockSize, StreamBytesRemaining);
|
|
|
|
CurrentRun->ByteLen += Used;
|
|
|
|
StreamBytesRemaining -= Used;
|
2017-06-23 23:11:54 +02:00
|
|
|
CurrentBlock = NextBlock;
|
2017-06-23 20:52:13 +02:00
|
|
|
Blocks = Blocks.drop_front();
|
|
|
|
}
|
|
|
|
return Runs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::pair<Run, uint32_t> findRun(uint32_t Offset, ArrayRef<Run> Runs) {
|
|
|
|
for (const auto &R : Runs) {
|
|
|
|
if (Offset < R.ByteLen)
|
|
|
|
return std::make_pair(R, Offset);
|
|
|
|
Offset -= R.ByteLen;
|
|
|
|
}
|
|
|
|
llvm_unreachable("Invalid offset!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
|
|
|
|
uint32_t StreamIdx,
|
|
|
|
StringRef StreamPurpose, uint32_t Offset,
|
|
|
|
uint32_t Size) {
|
|
|
|
if (StreamIdx >= File.getNumStreams()) {
|
|
|
|
formatLine("Stream {0}: Not present", StreamIdx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Size + Offset > File.getStreamByteSize(StreamIdx)) {
|
|
|
|
formatLine(
|
|
|
|
"Stream {0}: Invalid offset and size, range out of stream bounds",
|
|
|
|
StreamIdx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-12 20:24:38 +02:00
|
|
|
auto S = File.createIndexedStream(StreamIdx);
|
2017-06-23 20:52:13 +02:00
|
|
|
if (!S) {
|
|
|
|
NewLine();
|
|
|
|
formatLine("Stream {0}: Not present", StreamIdx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t End =
|
|
|
|
(Size == 0) ? S->getLength() : std::min(Offset + Size, S->getLength());
|
|
|
|
Size = End - Offset;
|
|
|
|
|
|
|
|
formatLine("Stream {0}: {1} (dumping {2:N} / {3:N} bytes)", StreamIdx,
|
|
|
|
StreamPurpose, Size, S->getLength());
|
|
|
|
AutoIndent Indent(*this);
|
|
|
|
BinaryStreamRef Slice(*S);
|
2017-06-23 22:18:38 +02:00
|
|
|
BinarySubstreamRef Substream;
|
|
|
|
Substream.Offset = Offset;
|
|
|
|
Substream.StreamData = Slice.drop_front(Offset).keep_front(Size);
|
|
|
|
|
2017-06-23 20:52:13 +02:00
|
|
|
auto Layout = File.getStreamLayout(StreamIdx);
|
2017-06-23 22:18:38 +02:00
|
|
|
formatMsfStreamData(Label, File, Layout, Substream);
|
2017-06-23 20:52:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
|
|
|
|
const msf::MSFStreamLayout &Stream,
|
|
|
|
BinarySubstreamRef Substream) {
|
|
|
|
BinaryStreamReader Reader(Substream.StreamData);
|
|
|
|
|
|
|
|
auto Runs = computeBlockRuns(File.getBlockSize(), Stream);
|
|
|
|
|
|
|
|
NewLine();
|
|
|
|
OS << Label << " (";
|
|
|
|
while (Reader.bytesRemaining() > 0) {
|
|
|
|
OS << "\n";
|
|
|
|
|
|
|
|
Run FoundRun;
|
|
|
|
uint32_t RunOffset;
|
2017-06-23 22:18:38 +02:00
|
|
|
std::tie(FoundRun, RunOffset) = findRun(Substream.Offset, Runs);
|
2017-06-23 20:52:13 +02:00
|
|
|
assert(FoundRun.ByteLen >= RunOffset);
|
|
|
|
uint32_t Len = FoundRun.ByteLen - RunOffset;
|
|
|
|
Len = std::min(Len, Reader.bytesRemaining());
|
|
|
|
uint64_t Base = FoundRun.Block * File.getBlockSize() + RunOffset;
|
|
|
|
ArrayRef<uint8_t> Data;
|
|
|
|
consumeError(Reader.readBytes(Data, Len));
|
|
|
|
OS << format_bytes_with_ascii(Data, Base, 32, 4,
|
|
|
|
CurrentIndent + IndentSpaces, true);
|
|
|
|
if (Reader.bytesRemaining() > 0) {
|
|
|
|
NewLine();
|
|
|
|
OS << formatv(" {0}",
|
|
|
|
fmt_align("<discontinuity>", AlignStyle::Center, 114, '-'));
|
|
|
|
}
|
2017-06-23 22:18:38 +02:00
|
|
|
Substream.Offset += Len;
|
2017-06-23 20:52:13 +02:00
|
|
|
}
|
|
|
|
NewLine();
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:25:52 +02:00
|
|
|
void LinePrinter::formatMsfStreamBlocks(
|
|
|
|
PDBFile &File, const msf::MSFStreamLayout &StreamLayout) {
|
|
|
|
auto Blocks = makeArrayRef(StreamLayout.Blocks);
|
|
|
|
uint32_t L = StreamLayout.Length;
|
|
|
|
|
|
|
|
while (L > 0) {
|
|
|
|
NewLine();
|
|
|
|
assert(!Blocks.empty());
|
|
|
|
OS << formatv("Block {0} (\n", uint32_t(Blocks.front()));
|
|
|
|
uint32_t UsedBytes = std::min(L, File.getBlockSize());
|
|
|
|
ArrayRef<uint8_t> BlockData =
|
|
|
|
cantFail(File.getBlockData(Blocks.front(), File.getBlockSize()));
|
|
|
|
uint64_t BaseOffset = Blocks.front();
|
|
|
|
BaseOffset *= File.getBlockSize();
|
|
|
|
OS << format_bytes_with_ascii(BlockData, BaseOffset, 32, 4,
|
|
|
|
CurrentIndent + IndentSpaces, true);
|
|
|
|
NewLine();
|
|
|
|
OS << ")";
|
|
|
|
NewLine();
|
|
|
|
L -= UsedBytes;
|
|
|
|
Blocks = Blocks.drop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-13 23:11:00 +02:00
|
|
|
bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName, uint32_t Size) {
|
|
|
|
if (IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters))
|
|
|
|
return true;
|
|
|
|
if (Size < opts::pretty::SizeThreshold)
|
|
|
|
return true;
|
|
|
|
return false;
|
2015-03-01 07:49:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
|
2015-09-29 21:49:06 +02:00
|
|
|
return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
|
2015-03-01 07:49:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
|
2015-09-29 21:49:06 +02:00
|
|
|
return IsItemExcluded(CompilandName, IncludeCompilandFilters,
|
|
|
|
ExcludeCompilandFilters);
|
2015-03-01 07:49:49 +01:00
|
|
|
}
|
|
|
|
|
2017-03-29 19:11:27 +02:00
|
|
|
WithColor::WithColor(LinePrinter &P, PDB_ColorItem C)
|
|
|
|
: OS(P.OS), UseColor(P.hasColor()) {
|
|
|
|
if (UseColor)
|
2017-03-23 16:28:15 +01:00
|
|
|
applyColor(C);
|
2015-02-27 10:15:59 +01:00
|
|
|
}
|
|
|
|
|
2017-03-29 19:11:27 +02:00
|
|
|
WithColor::~WithColor() {
|
|
|
|
if (UseColor)
|
|
|
|
OS.resetColor();
|
|
|
|
}
|
2015-02-27 10:15:59 +01:00
|
|
|
|
2015-11-03 02:04:44 +01:00
|
|
|
void WithColor::applyColor(PDB_ColorItem C) {
|
2015-02-27 10:15:59 +01:00
|
|
|
switch (C) {
|
2015-11-03 02:04:44 +01:00
|
|
|
case PDB_ColorItem::None:
|
|
|
|
OS.resetColor();
|
|
|
|
return;
|
2017-04-10 21:33:29 +02:00
|
|
|
case PDB_ColorItem::Comment:
|
|
|
|
OS.changeColor(raw_ostream::GREEN, false);
|
|
|
|
return;
|
2015-02-27 10:15:59 +01:00
|
|
|
case PDB_ColorItem::Address:
|
2015-11-03 02:04:44 +01:00
|
|
|
OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
|
2015-02-27 10:15:59 +01:00
|
|
|
return;
|
|
|
|
case PDB_ColorItem::Keyword:
|
2015-11-03 02:04:44 +01:00
|
|
|
OS.changeColor(raw_ostream::MAGENTA, true);
|
2015-02-27 10:15:59 +01:00
|
|
|
return;
|
2015-03-02 05:39:56 +01:00
|
|
|
case PDB_ColorItem::Register:
|
2015-02-27 10:15:59 +01:00
|
|
|
case PDB_ColorItem::Offset:
|
2015-11-03 02:04:44 +01:00
|
|
|
OS.changeColor(raw_ostream::YELLOW, false);
|
2015-02-27 10:15:59 +01:00
|
|
|
return;
|
|
|
|
case PDB_ColorItem::Type:
|
2015-11-03 02:04:44 +01:00
|
|
|
OS.changeColor(raw_ostream::CYAN, true);
|
2015-02-27 10:15:59 +01:00
|
|
|
return;
|
|
|
|
case PDB_ColorItem::Identifier:
|
2015-11-03 02:04:44 +01:00
|
|
|
OS.changeColor(raw_ostream::CYAN, false);
|
2015-02-27 10:15:59 +01:00
|
|
|
return;
|
|
|
|
case PDB_ColorItem::Path:
|
2015-11-03 02:04:44 +01:00
|
|
|
OS.changeColor(raw_ostream::CYAN, false);
|
2015-02-27 10:15:59 +01:00
|
|
|
return;
|
2017-04-10 21:33:29 +02:00
|
|
|
case PDB_ColorItem::Padding:
|
2015-02-27 10:15:59 +01:00
|
|
|
case PDB_ColorItem::SectionHeader:
|
2015-11-03 02:04:44 +01:00
|
|
|
OS.changeColor(raw_ostream::RED, true);
|
2015-02-27 10:15:59 +01:00
|
|
|
return;
|
|
|
|
case PDB_ColorItem::LiteralValue:
|
2015-11-03 02:04:44 +01:00
|
|
|
OS.changeColor(raw_ostream::GREEN, true);
|
2015-02-27 10:15:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|