PrettyCompilandDumper.cpp 7.7 KB

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