PrettyCompilandDumper.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //===- PrettyCompilandDumper.cpp - llvm-pdbutil compiland dumper -*- 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 "PrettyCompilandDumper.h"
  9. #include "LinePrinter.h"
  10. #include "PrettyFunctionDumper.h"
  11. #include "llvm-pdbutil.h"
  12. #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
  13. #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
  14. #include "llvm/DebugInfo/PDB/IPDBSession.h"
  15. #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
  16. #include "llvm/DebugInfo/PDB/PDBExtras.h"
  17. #include "llvm/DebugInfo/PDB/PDBSymbol.h"
  18. #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
  19. #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
  20. #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
  21. #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
  22. #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
  23. #include "llvm/DebugInfo/PDB/PDBSymbolLabel.h"
  24. #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
  25. #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
  26. #include "llvm/DebugInfo/PDB/PDBSymbolUnknown.h"
  27. #include "llvm/DebugInfo/PDB/PDBSymbolUsingNamespace.h"
  28. #include "llvm/Support/Format.h"
  29. #include "llvm/Support/Path.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include <utility>
  32. using namespace llvm;
  33. using namespace llvm::pdb;
  34. CompilandDumper::CompilandDumper(LinePrinter &P)
  35. : PDBSymDumper(true), Printer(P) {}
  36. void CompilandDumper::dump(const PDBSymbolCompilandDetails &Symbol) {}
  37. void CompilandDumper::dump(const PDBSymbolCompilandEnv &Symbol) {}
  38. void CompilandDumper::start(const PDBSymbolCompiland &Symbol,
  39. CompilandDumpFlags opts) {
  40. std::string FullName = Symbol.getName();
  41. if (Printer.IsCompilandExcluded(FullName))
  42. return;
  43. Printer.NewLine();
  44. WithColor(Printer, PDB_ColorItem::Path).get() << FullName;
  45. if (opts & Flags::Lines) {
  46. const IPDBSession &Session = Symbol.getSession();
  47. if (auto Files = Session.getSourceFilesForCompiland(Symbol)) {
  48. Printer.Indent();
  49. while (auto File = Files->getNext()) {
  50. Printer.NewLine();
  51. WithColor(Printer, PDB_ColorItem::Path).get() << File->getFileName();
  52. if (File->getChecksumType() != PDB_Checksum::None) {
  53. auto ChecksumType = File->getChecksumType();
  54. auto ChecksumHexString = toHex(File->getChecksum());
  55. WithColor(Printer, PDB_ColorItem::Comment).get()
  56. << " (" << ChecksumType << ": " << ChecksumHexString << ")";
  57. }
  58. auto Lines = Session.findLineNumbers(Symbol, *File);
  59. if (!Lines)
  60. continue;
  61. Printer.Indent();
  62. while (auto Line = Lines->getNext()) {
  63. Printer.NewLine();
  64. uint32_t LineStart = Line->getLineNumber();
  65. uint32_t LineEnd = Line->getLineNumberEnd();
  66. Printer << "Line ";
  67. PDB_ColorItem StatementColor = Line->isStatement()
  68. ? PDB_ColorItem::Keyword
  69. : PDB_ColorItem::LiteralValue;
  70. WithColor(Printer, StatementColor).get() << LineStart;
  71. if (LineStart != LineEnd)
  72. WithColor(Printer, StatementColor).get() << " - " << LineEnd;
  73. uint32_t ColumnStart = Line->getColumnNumber();
  74. uint32_t ColumnEnd = Line->getColumnNumberEnd();
  75. if (ColumnStart != 0 || ColumnEnd != 0) {
  76. Printer << ", Column: ";
  77. WithColor(Printer, StatementColor).get() << ColumnStart;
  78. if (ColumnEnd != ColumnStart)
  79. WithColor(Printer, StatementColor).get() << " - " << ColumnEnd;
  80. }
  81. Printer << ", Address: ";
  82. if (Line->getLength() > 0) {
  83. uint64_t AddrStart = Line->getVirtualAddress();
  84. uint64_t AddrEnd = AddrStart + Line->getLength() - 1;
  85. WithColor(Printer, PDB_ColorItem::Address).get()
  86. << "[" << format_hex(AddrStart, 10) << " - "
  87. << format_hex(AddrEnd, 10) << "]";
  88. Printer << " (" << Line->getLength() << " bytes)";
  89. } else {
  90. uint64_t AddrStart = Line->getVirtualAddress();
  91. WithColor(Printer, PDB_ColorItem::Address).get()
  92. << "[" << format_hex(AddrStart, 10) << "] ";
  93. Printer << "(0 bytes)";
  94. }
  95. }
  96. Printer.Unindent();
  97. }
  98. Printer.Unindent();
  99. }
  100. }
  101. if (opts & Flags::Children) {
  102. if (auto ChildrenEnum = Symbol.findAllChildren()) {
  103. Printer.Indent();
  104. while (auto Child = ChildrenEnum->getNext())
  105. Child->dump(*this);
  106. Printer.Unindent();
  107. }
  108. }
  109. }
  110. void CompilandDumper::dump(const PDBSymbolData &Symbol) {
  111. if (!shouldDumpSymLevel(opts::pretty::SymLevel::Data))
  112. return;
  113. if (Printer.IsSymbolExcluded(Symbol.getName()))
  114. return;
  115. Printer.NewLine();
  116. switch (auto LocType = Symbol.getLocationType()) {
  117. case PDB_LocType::Static:
  118. Printer << "data: ";
  119. WithColor(Printer, PDB_ColorItem::Address).get()
  120. << "[" << format_hex(Symbol.getVirtualAddress(), 10) << "]";
  121. WithColor(Printer, PDB_ColorItem::Comment).get()
  122. << " [sizeof = " << getTypeLength(Symbol) << "]";
  123. break;
  124. case PDB_LocType::Constant:
  125. Printer << "constant: ";
  126. WithColor(Printer, PDB_ColorItem::LiteralValue).get()
  127. << "[" << Symbol.getValue() << "]";
  128. WithColor(Printer, PDB_ColorItem::Comment).get()
  129. << " [sizeof = " << getTypeLength(Symbol) << "]";
  130. break;
  131. default:
  132. Printer << "data(unexpected type=" << LocType << ")";
  133. }
  134. Printer << " ";
  135. WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
  136. }
  137. void CompilandDumper::dump(const PDBSymbolFunc &Symbol) {
  138. if (!shouldDumpSymLevel(opts::pretty::SymLevel::Functions))
  139. return;
  140. if (Symbol.getLength() == 0)
  141. return;
  142. if (Printer.IsSymbolExcluded(Symbol.getName()))
  143. return;
  144. Printer.NewLine();
  145. FunctionDumper Dumper(Printer);
  146. Dumper.start(Symbol, FunctionDumper::PointerType::None);
  147. }
  148. void CompilandDumper::dump(const PDBSymbolLabel &Symbol) {
  149. if (Printer.IsSymbolExcluded(Symbol.getName()))
  150. return;
  151. Printer.NewLine();
  152. Printer << "label ";
  153. WithColor(Printer, PDB_ColorItem::Address).get()
  154. << "[" << format_hex(Symbol.getVirtualAddress(), 10) << "] ";
  155. WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
  156. }
  157. void CompilandDumper::dump(const PDBSymbolThunk &Symbol) {
  158. if (!shouldDumpSymLevel(opts::pretty::SymLevel::Thunks))
  159. return;
  160. if (Printer.IsSymbolExcluded(Symbol.getName()))
  161. return;
  162. Printer.NewLine();
  163. Printer << "thunk ";
  164. codeview::ThunkOrdinal Ordinal = Symbol.getThunkOrdinal();
  165. uint64_t VA = Symbol.getVirtualAddress();
  166. if (Ordinal == codeview::ThunkOrdinal::TrampIncremental) {
  167. uint64_t Target = Symbol.getTargetVirtualAddress();
  168. WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(VA, 10);
  169. Printer << " -> ";
  170. WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(Target, 10);
  171. } else {
  172. WithColor(Printer, PDB_ColorItem::Address).get()
  173. << "[" << format_hex(VA, 10) << " - "
  174. << format_hex(VA + Symbol.getLength(), 10) << "]";
  175. }
  176. Printer << " (";
  177. WithColor(Printer, PDB_ColorItem::Register).get() << Ordinal;
  178. Printer << ") ";
  179. std::string Name = Symbol.getName();
  180. if (!Name.empty())
  181. WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
  182. }
  183. void CompilandDumper::dump(const PDBSymbolTypeTypedef &Symbol) {}
  184. void CompilandDumper::dump(const PDBSymbolUnknown &Symbol) {
  185. Printer.NewLine();
  186. Printer << "unknown (" << Symbol.getSymTag() << ")";
  187. }
  188. void CompilandDumper::dump(const PDBSymbolUsingNamespace &Symbol) {
  189. if (Printer.IsSymbolExcluded(Symbol.getName()))
  190. return;
  191. Printer.NewLine();
  192. Printer << "using namespace ";
  193. std::string Name = Symbol.getName();
  194. WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
  195. }