llvm-pdbdump.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //===- llvm-pdbdump.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Dumps debug information present in PDB files. This utility makes use of
  11. // the Microsoft Windows SDK, so will not compile or run on non-Windows
  12. // platforms.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm-pdbdump.h"
  16. #include "CompilandDumper.h"
  17. #include "ExternalSymbolDumper.h"
  18. #include "FunctionDumper.h"
  19. #include "LinePrinter.h"
  20. #include "TypeDumper.h"
  21. #include "VariableDumper.h"
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/ADT/StringExtras.h"
  24. #include "llvm/Config/config.h"
  25. #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
  26. #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
  27. #include "llvm/DebugInfo/PDB/IPDBSession.h"
  28. #include "llvm/DebugInfo/PDB/PDB.h"
  29. #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
  30. #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
  31. #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
  32. #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
  33. #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Support/ConvertUTF.h"
  36. #include "llvm/Support/FileSystem.h"
  37. #include "llvm/Support/Format.h"
  38. #include "llvm/Support/ManagedStatic.h"
  39. #include "llvm/Support/PrettyStackTrace.h"
  40. #include "llvm/Support/Process.h"
  41. #include "llvm/Support/raw_ostream.h"
  42. #include "llvm/Support/Signals.h"
  43. #if defined(HAVE_DIA_SDK)
  44. #include <windows.h>
  45. #endif
  46. using namespace llvm;
  47. namespace opts {
  48. enum class PDB_DumpType { ByType, ByObjFile, Both };
  49. cl::list<std::string> InputFilenames(cl::Positional,
  50. cl::desc("<input PDB files>"),
  51. cl::OneOrMore);
  52. cl::OptionCategory TypeCategory("Symbol Type Options");
  53. cl::OptionCategory FilterCategory("Filtering Options");
  54. cl::OptionCategory OtherOptions("Other Options");
  55. cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
  56. cl::cat(TypeCategory));
  57. cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
  58. cl::cat(TypeCategory));
  59. cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
  60. cl::cat(TypeCategory));
  61. cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
  62. cl::cat(TypeCategory));
  63. cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory));
  64. cl::opt<bool>
  65. All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
  66. cl::cat(TypeCategory));
  67. cl::opt<uint64_t> LoadAddress(
  68. "load-address",
  69. cl::desc("Assume the module is loaded at the specified address"),
  70. cl::cat(OtherOptions));
  71. cl::list<std::string>
  72. ExcludeTypes("exclude-types",
  73. cl::desc("Exclude types by regular expression"),
  74. cl::ZeroOrMore, cl::cat(FilterCategory));
  75. cl::list<std::string>
  76. ExcludeSymbols("exclude-symbols",
  77. cl::desc("Exclude symbols by regular expression"),
  78. cl::ZeroOrMore, cl::cat(FilterCategory));
  79. cl::list<std::string>
  80. ExcludeCompilands("exclude-compilands",
  81. cl::desc("Exclude compilands by regular expression"),
  82. cl::ZeroOrMore, cl::cat(FilterCategory));
  83. cl::opt<bool> ExcludeCompilerGenerated(
  84. "no-compiler-generated",
  85. cl::desc("Don't show compiler generated types and symbols"),
  86. cl::cat(FilterCategory));
  87. cl::opt<bool>
  88. ExcludeSystemLibraries("no-system-libs",
  89. cl::desc("Don't show symbols from system libraries"),
  90. cl::cat(FilterCategory));
  91. cl::opt<bool> NoClassDefs("no-class-definitions",
  92. cl::desc("Don't display full class definitions"),
  93. cl::cat(FilterCategory));
  94. cl::opt<bool> NoEnumDefs("no-enum-definitions",
  95. cl::desc("Don't display full enum definitions"),
  96. cl::cat(FilterCategory));
  97. }
  98. static void dumpInput(StringRef Path) {
  99. std::unique_ptr<IPDBSession> Session;
  100. PDB_ErrorCode Error =
  101. llvm::loadDataForPDB(PDB_ReaderType::DIA, Path, Session);
  102. switch (Error) {
  103. case PDB_ErrorCode::Success:
  104. break;
  105. case PDB_ErrorCode::NoPdbImpl:
  106. outs() << "Reading PDBs is not supported on this platform.\n";
  107. return;
  108. case PDB_ErrorCode::InvalidPath:
  109. outs() << "Unable to load PDB at '" << Path
  110. << "'. Check that the file exists and is readable.\n";
  111. return;
  112. case PDB_ErrorCode::InvalidFileFormat:
  113. outs() << "Unable to load PDB at '" << Path
  114. << "'. The file has an unrecognized format.\n";
  115. return;
  116. default:
  117. outs() << "Unable to load PDB at '" << Path
  118. << "'. An unknown error occured.\n";
  119. return;
  120. }
  121. if (opts::LoadAddress)
  122. Session->setLoadAddress(opts::LoadAddress);
  123. LinePrinter Printer(2, outs());
  124. auto GlobalScope(Session->getGlobalScope());
  125. std::string FileName(GlobalScope->getSymbolsFileName());
  126. WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
  127. WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
  128. Printer.Indent();
  129. uint64_t FileSize = 0;
  130. Printer.NewLine();
  131. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
  132. if (!llvm::sys::fs::file_size(FileName, FileSize)) {
  133. Printer << ": " << FileSize << " bytes";
  134. } else {
  135. Printer << ": (Unable to obtain file size)";
  136. }
  137. Printer.NewLine();
  138. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
  139. Printer << ": " << GlobalScope->getGuid();
  140. Printer.NewLine();
  141. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
  142. Printer << ": " << GlobalScope->getAge();
  143. Printer.NewLine();
  144. WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
  145. Printer << ": ";
  146. if (GlobalScope->hasCTypes())
  147. outs() << "HasCTypes ";
  148. if (GlobalScope->hasPrivateSymbols())
  149. outs() << "HasPrivateSymbols ";
  150. Printer.Unindent();
  151. if (opts::Compilands) {
  152. Printer.NewLine();
  153. WithColor(Printer, PDB_ColorItem::SectionHeader).get()
  154. << "---COMPILANDS---";
  155. Printer.Indent();
  156. auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
  157. CompilandDumper Dumper(Printer);
  158. while (auto Compiland = Compilands->getNext())
  159. Dumper.start(*Compiland, false);
  160. Printer.Unindent();
  161. }
  162. if (opts::Types) {
  163. Printer.NewLine();
  164. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
  165. Printer.Indent();
  166. TypeDumper Dumper(Printer);
  167. Dumper.start(*GlobalScope);
  168. Printer.Unindent();
  169. }
  170. if (opts::Symbols) {
  171. Printer.NewLine();
  172. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
  173. Printer.Indent();
  174. auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
  175. CompilandDumper Dumper(Printer);
  176. while (auto Compiland = Compilands->getNext())
  177. Dumper.start(*Compiland, true);
  178. Printer.Unindent();
  179. }
  180. if (opts::Globals) {
  181. Printer.NewLine();
  182. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
  183. Printer.Indent();
  184. {
  185. FunctionDumper Dumper(Printer);
  186. auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
  187. while (auto Function = Functions->getNext()) {
  188. Printer.NewLine();
  189. Dumper.start(*Function, FunctionDumper::PointerType::None);
  190. }
  191. }
  192. {
  193. auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
  194. VariableDumper Dumper(Printer);
  195. while (auto Var = Vars->getNext())
  196. Dumper.start(*Var);
  197. }
  198. {
  199. auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
  200. CompilandDumper Dumper(Printer);
  201. while (auto Thunk = Thunks->getNext())
  202. Dumper.dump(*Thunk);
  203. }
  204. Printer.Unindent();
  205. }
  206. if (opts::Externals) {
  207. Printer.NewLine();
  208. WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
  209. Printer.Indent();
  210. ExternalSymbolDumper Dumper(Printer);
  211. Dumper.start(*GlobalScope);
  212. }
  213. outs().flush();
  214. }
  215. int main(int argc_, const char *argv_[]) {
  216. // Print a stack trace if we signal out.
  217. sys::PrintStackTraceOnErrorSignal();
  218. PrettyStackTraceProgram X(argc_, argv_);
  219. SmallVector<const char *, 256> argv;
  220. llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
  221. std::error_code EC = llvm::sys::Process::GetArgumentVector(
  222. argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
  223. if (EC) {
  224. llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
  225. return 1;
  226. }
  227. llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
  228. cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
  229. if (opts::All) {
  230. opts::Compilands = true;
  231. opts::Symbols = true;
  232. opts::Globals = true;
  233. opts::Types = true;
  234. opts::Externals = true;
  235. }
  236. if (opts::ExcludeCompilerGenerated) {
  237. opts::ExcludeTypes.push_back("__vc_attributes");
  238. opts::ExcludeCompilands.push_back("* Linker *");
  239. }
  240. if (opts::ExcludeSystemLibraries) {
  241. opts::ExcludeCompilands.push_back(
  242. "f:\\binaries\\Intermediate\\vctools\\crt_bld");
  243. }
  244. #if defined(HAVE_DIA_SDK)
  245. CoInitializeEx(nullptr, COINIT_MULTITHREADED);
  246. #endif
  247. std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
  248. dumpInput);
  249. #if defined(HAVE_DIA_SDK)
  250. CoUninitialize();
  251. #endif
  252. return 0;
  253. }