1 //===- llvm-pdbdump.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #include "llvm-pdbdump.h"
17 #include "CompilandDumper.h"
18 #include "FunctionDumper.h"
19 #include "TypeDumper.h"
20 #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"
44 #if defined(HAVE_DIA_SDK)
52 enum class PDB_DumpType { ByType, ByObjFile, Both };
54 cl::list<std::string> InputFilenames(cl::Positional,
55 cl::desc("<input PDB files>"),
58 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"));
59 cl::opt<bool> Symbols("symbols",
60 cl::desc("Display symbols for each compiland"));
61 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"));
62 cl::opt<bool> Types("types", cl::desc("Display types"));
63 cl::opt<bool> ClassDefs("class-definitions",
64 cl::desc("Display full class definitions"));
67 static void dumpInput(StringRef Path) {
68 std::unique_ptr<IPDBSession> Session(
69 llvm::createPDBReader(PDB_ReaderType::DIA, Path));
71 outs() << "Unable to create PDB reader. Check that a valid implementation";
72 outs() << " is available for your platform.";
76 auto GlobalScope(Session->getGlobalScope());
77 std::string FileName(GlobalScope->getSymbolsFileName());
79 outs() << "Summary for " << FileName;
80 uint64_t FileSize = 0;
81 if (!llvm::sys::fs::file_size(FileName, FileSize))
82 outs() << newline(2) << "Size: " << FileSize << " bytes";
84 outs() << newline(2) << "Size: (Unable to obtain file size)";
86 outs() << newline(2) << "Guid: " << GlobalScope->getGuid();
87 outs() << newline(2) << "Age: " << GlobalScope->getAge();
88 outs() << newline(2) << "Attributes: ";
89 if (GlobalScope->hasCTypes())
90 outs() << "HasCTypes ";
91 if (GlobalScope->hasPrivateSymbols())
92 outs() << "HasPrivateSymbols ";
94 if (opts::Compilands) {
95 outs() << "\n---COMPILANDS---";
96 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
97 CompilandDumper Dumper;
98 while (auto Compiland = Compilands->getNext())
99 Dumper.start(*Compiland, outs(), 2, false);
103 outs() << "\n---TYPES---";
104 TypeDumper Dumper(false, opts::ClassDefs);
105 Dumper.start(*GlobalScope, outs(), 2);
109 outs() << "\n---SYMBOLS---";
110 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
111 CompilandDumper Dumper;
112 while (auto Compiland = Compilands->getNext())
113 Dumper.start(*Compiland, outs(), 2, true);
117 outs() << "\n---GLOBALS---";
119 FunctionDumper Dumper;
120 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
121 while (auto Function = Functions->getNext())
122 Dumper.start(*Function, FunctionDumper::PointerType::None, outs(), 2);
125 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
126 VariableDumper Dumper;
127 while (auto Var = Vars->getNext())
128 Dumper.start(*Var, outs(), 2);
131 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
132 CompilandDumper Dumper;
133 while (auto Thunk = Thunks->getNext())
134 Dumper.dump(*Thunk, outs(), 2);
140 int main(int argc_, const char *argv_[]) {
141 // Print a stack trace if we signal out.
142 sys::PrintStackTraceOnErrorSignal();
143 PrettyStackTraceProgram X(argc_, argv_);
145 SmallVector<const char *, 256> argv;
146 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
147 std::error_code EC = llvm::sys::Process::GetArgumentVector(
148 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
150 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
154 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
156 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
158 #if defined(HAVE_DIA_SDK)
159 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
162 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
165 #if defined(HAVE_DIA_SDK)