LLVM 20.0.0git
LinePrinter.cpp
Go to the documentation of this file.
1//===- LinePrinter.cpp ------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "llvm/ADT/STLExtras.h"
20#include "llvm/Object/COFF.h"
22#include "llvm/Support/Format.h"
25#include "llvm/Support/Regex.h"
26
27#include <algorithm>
28
29using namespace llvm;
30using namespace llvm::msf;
31using namespace llvm::pdb;
32
33namespace {
34bool IsItemExcluded(llvm::StringRef Item,
35 std::list<llvm::Regex> &IncludeFilters,
36 std::list<llvm::Regex> &ExcludeFilters) {
37 if (Item.empty())
38 return false;
39
40 auto match_pred = [Item](llvm::Regex &R) { return R.match(Item); };
41
42 // Include takes priority over exclude. If the user specified include
43 // filters, and none of them include this item, them item is gone.
44 if (!IncludeFilters.empty() && !any_of(IncludeFilters, match_pred))
45 return true;
46
47 if (any_of(ExcludeFilters, match_pred))
48 return true;
49
50 return false;
51}
52} // namespace
53
54using namespace llvm;
55
57 const FilterOptions &Filters)
58 : OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor),
59 Filters(Filters) {
60 SetFilters(ExcludeTypeFilters, Filters.ExcludeTypes.begin(),
61 Filters.ExcludeTypes.end());
62 SetFilters(ExcludeSymbolFilters, Filters.ExcludeSymbols.begin(),
63 Filters.ExcludeSymbols.end());
64 SetFilters(ExcludeCompilandFilters, Filters.ExcludeCompilands.begin(),
65 Filters.ExcludeCompilands.end());
66
67 SetFilters(IncludeTypeFilters, Filters.IncludeTypes.begin(),
68 Filters.IncludeTypes.end());
69 SetFilters(IncludeSymbolFilters, Filters.IncludeSymbols.begin(),
70 Filters.IncludeSymbols.end());
71 SetFilters(IncludeCompilandFilters, Filters.IncludeCompilands.begin(),
72 Filters.IncludeCompilands.end());
73}
74
76 if (Amount == 0)
77 Amount = IndentSpaces;
78 CurrentIndent += Amount;
79}
80
82 if (Amount == 0)
83 Amount = IndentSpaces;
84 CurrentIndent = std::max<int>(0, CurrentIndent - Amount);
85}
86
88 OS << "\n";
89 OS.indent(CurrentIndent);
90}
91
92void LinePrinter::print(const Twine &T) { OS << T; }
93
95 NewLine();
96 OS << T;
97}
98
100 if (IsTypeExcluded(Class.getName(), Class.getSize()))
101 return true;
102 if (Class.deepPaddingSize() < Filters.PaddingThreshold)
103 return true;
104 return false;
105}
106
108 uint64_t StartOffset) {
109 NewLine();
110 OS << Label << " (";
111 if (!Data.empty()) {
112 OS << "\n";
113 OS << format_bytes_with_ascii(Data, StartOffset, 32, 4,
114 CurrentIndent + IndentSpaces, true);
115 NewLine();
116 }
117 OS << ")";
118}
119
121 uint64_t Base, uint64_t StartOffset) {
122 NewLine();
123 OS << Label << " (";
124 if (!Data.empty()) {
125 OS << "\n";
126 Base += StartOffset;
127 OS << format_bytes_with_ascii(Data, Base, 32, 4,
128 CurrentIndent + IndentSpaces, true);
129 NewLine();
130 }
131 OS << ")";
132}
133
134namespace {
135struct Run {
136 Run() = default;
137 explicit Run(uint32_t Block) : Block(Block) {}
138 uint32_t Block = 0;
139 uint64_t ByteLen = 0;
140};
141} // namespace
142
143static std::vector<Run> computeBlockRuns(uint32_t BlockSize,
144 const msf::MSFStreamLayout &Layout) {
145 std::vector<Run> Runs;
146 if (Layout.Length == 0)
147 return Runs;
148
150 assert(!Blocks.empty());
151 uint64_t StreamBytesRemaining = Layout.Length;
152 uint32_t CurrentBlock = Blocks[0];
153 Runs.emplace_back(CurrentBlock);
154 while (!Blocks.empty()) {
155 Run *CurrentRun = &Runs.back();
156 uint32_t NextBlock = Blocks.front();
157 if (NextBlock < CurrentBlock || (NextBlock - CurrentBlock > 1)) {
158 Runs.emplace_back(NextBlock);
159 CurrentRun = &Runs.back();
160 }
161 uint64_t Used =
162 std::min(static_cast<uint64_t>(BlockSize), StreamBytesRemaining);
163 CurrentRun->ByteLen += Used;
164 StreamBytesRemaining -= Used;
165 CurrentBlock = NextBlock;
166 Blocks = Blocks.drop_front();
167 }
168 return Runs;
169}
170
171static std::pair<Run, uint64_t> findRun(uint64_t Offset, ArrayRef<Run> Runs) {
172 for (const auto &R : Runs) {
173 if (Offset < R.ByteLen)
174 return std::make_pair(R, Offset);
175 Offset -= R.ByteLen;
176 }
177 llvm_unreachable("Invalid offset!");
178}
179
181 uint32_t StreamIdx,
182 StringRef StreamPurpose, uint64_t Offset,
183 uint64_t Size) {
184 if (StreamIdx >= File.getNumStreams()) {
185 formatLine("Stream {0}: Not present", StreamIdx);
186 return;
187 }
188 if (Size + Offset > File.getStreamByteSize(StreamIdx)) {
190 "Stream {0}: Invalid offset and size, range out of stream bounds",
191 StreamIdx);
192 return;
193 }
194
195 auto S = File.createIndexedStream(StreamIdx);
196 if (!S) {
197 NewLine();
198 formatLine("Stream {0}: Not present", StreamIdx);
199 return;
200 }
201
202 uint64_t End =
203 (Size == 0) ? S->getLength() : std::min(Offset + Size, S->getLength());
204 Size = End - Offset;
205
206 formatLine("Stream {0}: {1} (dumping {2:N} / {3:N} bytes)", StreamIdx,
207 StreamPurpose, Size, S->getLength());
208 AutoIndent Indent(*this);
209 BinaryStreamRef Slice(*S);
210 BinarySubstreamRef Substream;
211 Substream.Offset = Offset;
212 Substream.StreamData = Slice.drop_front(Offset).keep_front(Size);
213
214 auto Layout = File.getStreamLayout(StreamIdx);
215 formatMsfStreamData(Label, File, Layout, Substream);
216}
217
219 const msf::MSFStreamLayout &Stream,
220 BinarySubstreamRef Substream) {
221 BinaryStreamReader Reader(Substream.StreamData);
222
223 auto Runs = computeBlockRuns(File.getBlockSize(), Stream);
224
225 NewLine();
226 OS << Label << " (";
227 while (Reader.bytesRemaining() > 0) {
228 OS << "\n";
229
230 Run FoundRun;
231 uint64_t RunOffset;
232 std::tie(FoundRun, RunOffset) = findRun(Substream.Offset, Runs);
233 assert(FoundRun.ByteLen >= RunOffset);
234 uint64_t Len = FoundRun.ByteLen - RunOffset;
235 Len = std::min(Len, Reader.bytesRemaining());
236 uint64_t Base = FoundRun.Block * File.getBlockSize() + RunOffset;
238 consumeError(Reader.readBytes(Data, Len));
239 OS << format_bytes_with_ascii(Data, Base, 32, 4,
240 CurrentIndent + IndentSpaces, true);
241 if (Reader.bytesRemaining() > 0) {
242 NewLine();
243 OS << formatv(" {0}",
244 fmt_align("<discontinuity>", AlignStyle::Center, 114, '-'));
245 }
246 Substream.Offset += Len;
247 }
248 NewLine();
249 OS << ")";
250}
251
253 PDBFile &File, const msf::MSFStreamLayout &StreamLayout) {
254 auto Blocks = ArrayRef(StreamLayout.Blocks);
255 uint64_t L = StreamLayout.Length;
256
257 while (L > 0) {
258 NewLine();
259 assert(!Blocks.empty());
260 OS << formatv("Block {0} (\n", uint32_t(Blocks.front()));
261 uint64_t UsedBytes =
262 std::min(L, static_cast<uint64_t>(File.getBlockSize()));
263 ArrayRef<uint8_t> BlockData =
264 cantFail(File.getBlockData(Blocks.front(), File.getBlockSize()));
265 uint64_t BaseOffset = Blocks.front();
266 BaseOffset *= File.getBlockSize();
267 OS << format_bytes_with_ascii(BlockData, BaseOffset, 32, 4,
268 CurrentIndent + IndentSpaces, true);
269 NewLine();
270 OS << ")";
271 NewLine();
272 L -= UsedBytes;
273 Blocks = Blocks.drop_front();
274 }
275}
276
278 if (IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters))
279 return true;
280 if (Size < Filters.SizeThreshold)
281 return true;
282 return false;
283}
284
286 return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
287}
288
290 return IsItemExcluded(CompilandName, IncludeCompilandFilters,
291 ExcludeCompilandFilters);
292}
293
295 : OS(P.OS), UseColor(P.hasColor()) {
296 if (UseColor)
297 applyColor(C);
298}
299
301 if (UseColor)
302 OS.resetColor();
303}
304
305void WithColor::applyColor(PDB_ColorItem C) {
306 switch (C) {
308 OS.resetColor();
309 return;
312 return;
314 OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
315 return;
318 return;
322 return;
325 return;
328 return;
331 return;
335 return;
338 return;
339 }
340}
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
DenseMap< Block *, BlockRelaxAux > Blocks
Definition: ELF_riscv.cpp:507
static std::pair< Run, uint64_t > findRun(uint64_t Offset, ArrayRef< Run > Runs)
static std::vector< Run > computeBlockRuns(uint32_t BlockSize, const msf::MSFStreamLayout &Layout)
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
static const int BlockSize
Definition: TarWriter.cpp:33
static ManagedStatic< cl::opt< cl::boolOrDefault >, CreateUseColor > UseColor
Definition: WithColor.cpp:33
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Provides read only access to a subclass of BinaryStream.
Error readBytes(ArrayRef< uint8_t > &Buffer, uint32_t Size)
Read Size bytes from the underlying stream at the current offset and and set Buffer to the resulting ...
uint64_t bytesRemaining() const
RefType drop_front(uint64_t N) const
Return a new BinaryStreamRef with the first N elements removed.
RefType keep_front(uint64_t N) const
Return a new BinaryStreamRef with only the first N elements remaining.
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Describes the layout of a stream in an MSF layout.
Definition: MSFCommon.h:77
std::vector< support::ulittle32_t > Blocks
Definition: MSFCommon.h:80
void print(const Twine &T)
Definition: LinePrinter.cpp:92
void printLine(const Twine &T)
Definition: LinePrinter.cpp:94
void Unindent(uint32_t Amount=0)
Definition: LinePrinter.cpp:81
void formatMsfStreamData(StringRef Label, PDBFile &File, uint32_t StreamIdx, StringRef StreamPurpose, uint64_t Offset, uint64_t Size)
bool IsSymbolExcluded(llvm::StringRef SymbolName)
LinePrinter(int Indent, bool UseColor, raw_ostream &Stream, const FilterOptions &Filters)
Definition: LinePrinter.cpp:56
void formatMsfStreamBlocks(PDBFile &File, const msf::MSFStreamLayout &Stream)
void formatLine(const char *Fmt, Ts &&...Items)
Definition: LinePrinter.h:63
void formatBinary(StringRef Label, ArrayRef< uint8_t > Data, uint64_t StartOffset)
bool IsTypeExcluded(llvm::StringRef TypeName, uint64_t Size)
bool IsClassExcluded(const ClassLayout &Class)
Definition: LinePrinter.cpp:99
bool IsCompilandExcluded(llvm::StringRef CompilandName)
void Indent(uint32_t Amount=0)
Definition: LinePrinter.cpp:75
WithColor(LinePrinter &P, PDB_ColorItem C)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
static constexpr Colors YELLOW
Definition: raw_ostream.h:117
static constexpr Colors CYAN
Definition: raw_ostream.h:120
virtual raw_ostream & changeColor(enum Colors Color, bool Bold=false, bool BG=false)
Changes the foreground color of text that will be output from this point forward.
virtual raw_ostream & resetColor()
Resets the colors to terminal defaults.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
static constexpr Colors MAGENTA
Definition: raw_ostream.h:119
static constexpr Colors GREEN
Definition: raw_ostream.h:116
static constexpr Colors RED
Definition: raw_ostream.h:115
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1729
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:756
FormattedBytes format_bytes_with_ascii(ArrayRef< uint8_t > Bytes, std::optional< uint64_t > FirstByteOffset=std::nullopt, uint32_t NumPerLine=16, uint8_t ByteGroupSize=4, uint32_t IndentLevel=0, bool Upper=false)
Definition: Format.h:250
support::detail::AlignAdapter< T > fmt_align(T &&Item, AlignStyle Where, size_t Amount, char Fill=' ')
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
std::list< std::string > IncludeCompilands
Definition: LinePrinter.h:30
std::list< std::string > IncludeTypes
Definition: LinePrinter.h:28
std::list< std::string > IncludeSymbols
Definition: LinePrinter.h:29
uint32_t SizeThreshold
Definition: LinePrinter.h:32
std::list< std::string > ExcludeTypes
Definition: LinePrinter.h:25
uint32_t PaddingThreshold
Definition: LinePrinter.h:31
std::list< std::string > ExcludeCompilands
Definition: LinePrinter.h:27
std::list< std::string > ExcludeSymbols
Definition: LinePrinter.h:26
BinaryStreamRef StreamData