LinePrinter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #include "LinePrinter.h"
  9. #include "llvm-pdbutil.h"
  10. #include "llvm/ADT/STLExtras.h"
  11. #include "llvm/DebugInfo/MSF/MSFCommon.h"
  12. #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
  13. #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
  14. #include "llvm/DebugInfo/PDB/UDTLayout.h"
  15. #include "llvm/Support/BinaryStreamReader.h"
  16. #include "llvm/Support/Format.h"
  17. #include "llvm/Support/FormatAdapters.h"
  18. #include "llvm/Support/FormatVariadic.h"
  19. #include "llvm/Support/Regex.h"
  20. #include <algorithm>
  21. using namespace llvm;
  22. using namespace llvm::msf;
  23. using namespace llvm::pdb;
  24. namespace {
  25. bool IsItemExcluded(llvm::StringRef Item,
  26. std::list<llvm::Regex> &IncludeFilters,
  27. std::list<llvm::Regex> &ExcludeFilters) {
  28. if (Item.empty())
  29. return false;
  30. auto match_pred = [Item](llvm::Regex &R) { return R.match(Item); };
  31. // Include takes priority over exclude. If the user specified include
  32. // filters, and none of them include this item, them item is gone.
  33. if (!IncludeFilters.empty() && !any_of(IncludeFilters, match_pred))
  34. return true;
  35. if (any_of(ExcludeFilters, match_pred))
  36. return true;
  37. return false;
  38. }
  39. }
  40. using namespace llvm;
  41. LinePrinter::LinePrinter(int Indent, bool UseColor, llvm::raw_ostream &Stream)
  42. : OS(Stream), IndentSpaces(Indent), CurrentIndent(0), UseColor(UseColor) {
  43. SetFilters(ExcludeTypeFilters, opts::pretty::ExcludeTypes.begin(),
  44. opts::pretty::ExcludeTypes.end());
  45. SetFilters(ExcludeSymbolFilters, opts::pretty::ExcludeSymbols.begin(),
  46. opts::pretty::ExcludeSymbols.end());
  47. SetFilters(ExcludeCompilandFilters, opts::pretty::ExcludeCompilands.begin(),
  48. opts::pretty::ExcludeCompilands.end());
  49. SetFilters(IncludeTypeFilters, opts::pretty::IncludeTypes.begin(),
  50. opts::pretty::IncludeTypes.end());
  51. SetFilters(IncludeSymbolFilters, opts::pretty::IncludeSymbols.begin(),
  52. opts::pretty::IncludeSymbols.end());
  53. SetFilters(IncludeCompilandFilters, opts::pretty::IncludeCompilands.begin(),
  54. opts::pretty::IncludeCompilands.end());
  55. }
  56. void LinePrinter::Indent(uint32_t Amount) {
  57. if (Amount == 0)
  58. Amount = IndentSpaces;
  59. CurrentIndent += Amount;
  60. }
  61. void LinePrinter::Unindent(uint32_t Amount) {
  62. if (Amount == 0)
  63. Amount = IndentSpaces;
  64. CurrentIndent = std::max<int>(0, CurrentIndent - Amount);
  65. }
  66. void LinePrinter::NewLine() {
  67. OS << "\n";
  68. OS.indent(CurrentIndent);
  69. }
  70. void LinePrinter::print(const Twine &T) { OS << T; }
  71. void LinePrinter::printLine(const Twine &T) {
  72. NewLine();
  73. OS << T;
  74. }
  75. bool LinePrinter::IsClassExcluded(const ClassLayout &Class) {
  76. if (IsTypeExcluded(Class.getName(), Class.getSize()))
  77. return true;
  78. if (Class.deepPaddingSize() < opts::pretty::PaddingThreshold)
  79. return true;
  80. return false;
  81. }
  82. void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
  83. uint64_t StartOffset) {
  84. NewLine();
  85. OS << Label << " (";
  86. if (!Data.empty()) {
  87. OS << "\n";
  88. OS << format_bytes_with_ascii(Data, StartOffset, 32, 4,
  89. CurrentIndent + IndentSpaces, true);
  90. NewLine();
  91. }
  92. OS << ")";
  93. }
  94. void LinePrinter::formatBinary(StringRef Label, ArrayRef<uint8_t> Data,
  95. uint64_t Base, uint64_t StartOffset) {
  96. NewLine();
  97. OS << Label << " (";
  98. if (!Data.empty()) {
  99. OS << "\n";
  100. Base += StartOffset;
  101. OS << format_bytes_with_ascii(Data, Base, 32, 4,
  102. CurrentIndent + IndentSpaces, true);
  103. NewLine();
  104. }
  105. OS << ")";
  106. }
  107. namespace {
  108. struct Run {
  109. Run() = default;
  110. explicit Run(uint32_t Block) : Block(Block) {}
  111. uint32_t Block = 0;
  112. uint64_t ByteLen = 0;
  113. };
  114. } // namespace
  115. static std::vector<Run> computeBlockRuns(uint32_t BlockSize,
  116. const msf::MSFStreamLayout &Layout) {
  117. std::vector<Run> Runs;
  118. if (Layout.Length == 0)
  119. return Runs;
  120. ArrayRef<support::ulittle32_t> Blocks = Layout.Blocks;
  121. assert(!Blocks.empty());
  122. uint64_t StreamBytesRemaining = Layout.Length;
  123. uint32_t CurrentBlock = Blocks[0];
  124. Runs.emplace_back(CurrentBlock);
  125. while (!Blocks.empty()) {
  126. Run *CurrentRun = &Runs.back();
  127. uint32_t NextBlock = Blocks.front();
  128. if (NextBlock < CurrentBlock || (NextBlock - CurrentBlock > 1)) {
  129. Runs.emplace_back(NextBlock);
  130. CurrentRun = &Runs.back();
  131. }
  132. uint64_t Used =
  133. std::min(static_cast<uint64_t>(BlockSize), StreamBytesRemaining);
  134. CurrentRun->ByteLen += Used;
  135. StreamBytesRemaining -= Used;
  136. CurrentBlock = NextBlock;
  137. Blocks = Blocks.drop_front();
  138. }
  139. return Runs;
  140. }
  141. static std::pair<Run, uint64_t> findRun(uint64_t Offset, ArrayRef<Run> Runs) {
  142. for (const auto &R : Runs) {
  143. if (Offset < R.ByteLen)
  144. return std::make_pair(R, Offset);
  145. Offset -= R.ByteLen;
  146. }
  147. llvm_unreachable("Invalid offset!");
  148. }
  149. void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
  150. uint32_t StreamIdx,
  151. StringRef StreamPurpose, uint64_t Offset,
  152. uint64_t Size) {
  153. if (StreamIdx >= File.getNumStreams()) {
  154. formatLine("Stream {0}: Not present", StreamIdx);
  155. return;
  156. }
  157. if (Size + Offset > File.getStreamByteSize(StreamIdx)) {
  158. formatLine(
  159. "Stream {0}: Invalid offset and size, range out of stream bounds",
  160. StreamIdx);
  161. return;
  162. }
  163. auto S = File.createIndexedStream(StreamIdx);
  164. if (!S) {
  165. NewLine();
  166. formatLine("Stream {0}: Not present", StreamIdx);
  167. return;
  168. }
  169. uint64_t End =
  170. (Size == 0) ? S->getLength() : std::min(Offset + Size, S->getLength());
  171. Size = End - Offset;
  172. formatLine("Stream {0}: {1} (dumping {2:N} / {3:N} bytes)", StreamIdx,
  173. StreamPurpose, Size, S->getLength());
  174. AutoIndent Indent(*this);
  175. BinaryStreamRef Slice(*S);
  176. BinarySubstreamRef Substream;
  177. Substream.Offset = Offset;
  178. Substream.StreamData = Slice.drop_front(Offset).keep_front(Size);
  179. auto Layout = File.getStreamLayout(StreamIdx);
  180. formatMsfStreamData(Label, File, Layout, Substream);
  181. }
  182. void LinePrinter::formatMsfStreamData(StringRef Label, PDBFile &File,
  183. const msf::MSFStreamLayout &Stream,
  184. BinarySubstreamRef Substream) {
  185. BinaryStreamReader Reader(Substream.StreamData);
  186. auto Runs = computeBlockRuns(File.getBlockSize(), Stream);
  187. NewLine();
  188. OS << Label << " (";
  189. while (Reader.bytesRemaining() > 0) {
  190. OS << "\n";
  191. Run FoundRun;
  192. uint64_t RunOffset;
  193. std::tie(FoundRun, RunOffset) = findRun(Substream.Offset, Runs);
  194. assert(FoundRun.ByteLen >= RunOffset);
  195. uint64_t Len = FoundRun.ByteLen - RunOffset;
  196. Len = std::min(Len, Reader.bytesRemaining());
  197. uint64_t Base = FoundRun.Block * File.getBlockSize() + RunOffset;
  198. ArrayRef<uint8_t> Data;
  199. consumeError(Reader.readBytes(Data, Len));
  200. OS << format_bytes_with_ascii(Data, Base, 32, 4,
  201. CurrentIndent + IndentSpaces, true);
  202. if (Reader.bytesRemaining() > 0) {
  203. NewLine();
  204. OS << formatv(" {0}",
  205. fmt_align("<discontinuity>", AlignStyle::Center, 114, '-'));
  206. }
  207. Substream.Offset += Len;
  208. }
  209. NewLine();
  210. OS << ")";
  211. }
  212. void LinePrinter::formatMsfStreamBlocks(
  213. PDBFile &File, const msf::MSFStreamLayout &StreamLayout) {
  214. auto Blocks = makeArrayRef(StreamLayout.Blocks);
  215. uint64_t L = StreamLayout.Length;
  216. while (L > 0) {
  217. NewLine();
  218. assert(!Blocks.empty());
  219. OS << formatv("Block {0} (\n", uint32_t(Blocks.front()));
  220. uint64_t UsedBytes =
  221. std::min(L, static_cast<uint64_t>(File.getBlockSize()));
  222. ArrayRef<uint8_t> BlockData =
  223. cantFail(File.getBlockData(Blocks.front(), File.getBlockSize()));
  224. uint64_t BaseOffset = Blocks.front();
  225. BaseOffset *= File.getBlockSize();
  226. OS << format_bytes_with_ascii(BlockData, BaseOffset, 32, 4,
  227. CurrentIndent + IndentSpaces, true);
  228. NewLine();
  229. OS << ")";
  230. NewLine();
  231. L -= UsedBytes;
  232. Blocks = Blocks.drop_front();
  233. }
  234. }
  235. bool LinePrinter::IsTypeExcluded(llvm::StringRef TypeName, uint64_t Size) {
  236. if (IsItemExcluded(TypeName, IncludeTypeFilters, ExcludeTypeFilters))
  237. return true;
  238. if (Size < opts::pretty::SizeThreshold)
  239. return true;
  240. return false;
  241. }
  242. bool LinePrinter::IsSymbolExcluded(llvm::StringRef SymbolName) {
  243. return IsItemExcluded(SymbolName, IncludeSymbolFilters, ExcludeSymbolFilters);
  244. }
  245. bool LinePrinter::IsCompilandExcluded(llvm::StringRef CompilandName) {
  246. return IsItemExcluded(CompilandName, IncludeCompilandFilters,
  247. ExcludeCompilandFilters);
  248. }
  249. WithColor::WithColor(LinePrinter &P, PDB_ColorItem C)
  250. : OS(P.OS), UseColor(P.hasColor()) {
  251. if (UseColor)
  252. applyColor(C);
  253. }
  254. WithColor::~WithColor() {
  255. if (UseColor)
  256. OS.resetColor();
  257. }
  258. void WithColor::applyColor(PDB_ColorItem C) {
  259. switch (C) {
  260. case PDB_ColorItem::None:
  261. OS.resetColor();
  262. return;
  263. case PDB_ColorItem::Comment:
  264. OS.changeColor(raw_ostream::GREEN, false);
  265. return;
  266. case PDB_ColorItem::Address:
  267. OS.changeColor(raw_ostream::YELLOW, /*bold=*/true);
  268. return;
  269. case PDB_ColorItem::Keyword:
  270. OS.changeColor(raw_ostream::MAGENTA, true);
  271. return;
  272. case PDB_ColorItem::Register:
  273. case PDB_ColorItem::Offset:
  274. OS.changeColor(raw_ostream::YELLOW, false);
  275. return;
  276. case PDB_ColorItem::Type:
  277. OS.changeColor(raw_ostream::CYAN, true);
  278. return;
  279. case PDB_ColorItem::Identifier:
  280. OS.changeColor(raw_ostream::CYAN, false);
  281. return;
  282. case PDB_ColorItem::Path:
  283. OS.changeColor(raw_ostream::CYAN, false);
  284. return;
  285. case PDB_ColorItem::Padding:
  286. case PDB_ColorItem::SectionHeader:
  287. OS.changeColor(raw_ostream::RED, true);
  288. return;
  289. case PDB_ColorItem::LiteralValue:
  290. OS.changeColor(raw_ostream::GREEN, true);
  291. return;
  292. }
  293. }