1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
//===- BytesOutputStyle.cpp ----------------------------------- *- C++ --*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "BytesOutputStyle.h"
#include "StreamUtil.h"
#include "llvm-pdbutil.h"
#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
#include "llvm/DebugInfo/PDB/Native/RawError.h"
#include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/FormatAdapters.h"
#include "llvm/Support/FormatVariadic.h"
using namespace llvm;
using namespace llvm::msf;
using namespace llvm::pdb;
namespace {
struct StreamSpec {
uint32_t SI = 0;
uint32_t Begin = 0;
uint32_t Size = 0;
};
} // namespace
static Expected<StreamSpec> parseStreamSpec(StringRef Str) {
StreamSpec Result;
if (Str.consumeInteger(0, Result.SI))
return make_error<RawError>(raw_error_code::invalid_format,
"Invalid Stream Specification");
if (Str.consume_front(":")) {
if (Str.consumeInteger(0, Result.Begin))
return make_error<RawError>(raw_error_code::invalid_format,
"Invalid Stream Specification");
}
if (Str.consume_front("@")) {
if (Str.consumeInteger(0, Result.Size))
return make_error<RawError>(raw_error_code::invalid_format,
"Invalid Stream Specification");
}
if (!Str.empty())
return make_error<RawError>(raw_error_code::invalid_format,
"Invalid Stream Specification");
return Result;
}
static SmallVector<StreamSpec, 2> parseStreamSpecs(LinePrinter &P) {
SmallVector<StreamSpec, 2> Result;
for (auto &Str : opts::bytes::DumpStreamData) {
auto ESS = parseStreamSpec(Str);
if (!ESS) {
P.formatLine("Error parsing stream spec {0}: {1}", Str,
toString(ESS.takeError()));
continue;
}
Result.push_back(*ESS);
}
return Result;
}
static void printHeader(LinePrinter &P, const Twine &S) {
P.NewLine();
P.formatLine("{0,=60}", S);
P.formatLine("{0}", fmt_repeat('=', 60));
}
BytesOutputStyle::BytesOutputStyle(PDBFile &File)
: File(File), P(2, false, outs()) {}
Error BytesOutputStyle::dump() {
if (opts::bytes::DumpBlockRange.hasValue()) {
auto &R = *opts::bytes::DumpBlockRange;
uint32_t Max = R.Max.getValueOr(R.Min);
if (Max < R.Min)
return make_error<StringError>(
"Invalid block range specified. Max < Min",
inconvertibleErrorCode());
if (Max >= File.getBlockCount())
return make_error<StringError>(
"Invalid block range specified. Requested block out of bounds",
inconvertibleErrorCode());
dumpBlockRanges(R.Min, Max);
P.NewLine();
}
if (!opts::bytes::DumpStreamData.empty()) {
dumpStreamBytes();
P.NewLine();
}
return Error::success();
}
void BytesOutputStyle::dumpBlockRanges(uint32_t Min, uint32_t Max) {
printHeader(P, "MSF Blocks");
AutoIndent Indent(P);
for (uint32_t I = Min; I <= Max; ++I) {
uint64_t Base = I;
Base *= File.getBlockSize();
auto ExpectedData = File.getBlockData(I, File.getBlockSize());
if (!ExpectedData) {
P.formatLine("Could not get block {0}. Reason = {1}", I,
toString(ExpectedData.takeError()));
continue;
}
std::string Label = formatv("Block {0}", I).str();
P.formatBinary(Label, *ExpectedData, Base, 0);
}
}
void BytesOutputStyle::dumpStreamBytes() {
if (StreamPurposes.empty())
discoverStreamPurposes(File, StreamPurposes);
printHeader(P, "Stream Data");
ExitOnError Err("Unexpected error reading stream data");
auto Specs = parseStreamSpecs(P);
for (const auto &Spec : Specs) {
uint32_t End = 0;
AutoIndent Indent(P);
if (Spec.SI >= File.getNumStreams()) {
P.formatLine("Stream {0}: Not present", Spec.SI);
continue;
}
auto S = MappedBlockStream::createIndexedStream(
File.getMsfLayout(), File.getMsfBuffer(), Spec.SI, File.getAllocator());
if (!S) {
P.NewLine();
P.formatLine("Stream {0}: Not present", Spec.SI);
continue;
}
if (Spec.Size == 0)
End = S->getLength();
else
End = std::min(Spec.Begin + Spec.Size, S->getLength());
uint32_t Size = End - Spec.Begin;
P.formatLine("Stream {0} ({1:N} bytes): {2}", Spec.SI, S->getLength(),
StreamPurposes[Spec.SI]);
AutoIndent Indent2(P);
BinaryStreamReader R(*S);
ArrayRef<uint8_t> StreamData;
Err(R.readBytes(StreamData, S->getLength()));
StreamData = StreamData.slice(Spec.Begin, Size);
P.formatBinary("Data", StreamData, Spec.Begin);
}
}
|