LinePrinter.cpp 10 KB

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