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 "LinePrinter.h"
20 #include "TypeDumper.h"
21 #include "VariableDumper.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/StringExtras.h"
25 #include "llvm/Config/config.h"
26 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
27 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
28 #include "llvm/DebugInfo/PDB/IPDBSession.h"
29 #include "llvm/DebugInfo/PDB/PDB.h"
30 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
31 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
32 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
33 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
34 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/ConvertUTF.h"
37 #include "llvm/Support/FileSystem.h"
38 #include "llvm/Support/Format.h"
39 #include "llvm/Support/ManagedStatic.h"
40 #include "llvm/Support/PrettyStackTrace.h"
41 #include "llvm/Support/Process.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include "llvm/Support/Signals.h"
45 #if defined(HAVE_DIA_SDK)
53 enum class PDB_DumpType { ByType, ByObjFile, Both };
55 cl::list<std::string> InputFilenames(cl::Positional,
56 cl::desc("<input PDB files>"),
59 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"));
60 cl::opt<bool> Symbols("symbols",
61 cl::desc("Display symbols for each compiland"));
62 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"));
63 cl::opt<bool> Types("types", cl::desc("Display types"));
64 cl::opt<bool> ClassDefs("class-definitions",
65 cl::desc("Display full class definitions"));
68 static void dumpInput(StringRef Path) {
69 std::unique_ptr<IPDBSession> Session(
70 llvm::createPDBReader(PDB_ReaderType::DIA, Path));
72 outs() << "Unable to create PDB reader. Check that a valid implementation";
73 outs() << " is available for your platform.";
77 LinePrinter Printer(2, outs());
79 auto GlobalScope(Session->getGlobalScope());
80 std::string FileName(GlobalScope->getSymbolsFileName());
82 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
83 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
85 uint64_t FileSize = 0;
88 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
89 if (!llvm::sys::fs::file_size(FileName, FileSize)) {
90 Printer << ": " << FileSize << " bytes";
92 Printer << ": (Unable to obtain file size)";
96 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
97 Printer << ": " << GlobalScope->getGuid();
100 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
101 Printer << ": " << GlobalScope->getAge();
104 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
106 if (GlobalScope->hasCTypes())
107 outs() << "HasCTypes ";
108 if (GlobalScope->hasPrivateSymbols())
109 outs() << "HasPrivateSymbols ";
112 if (opts::Compilands) {
114 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
115 << "---COMPILANDS---";
117 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
118 CompilandDumper Dumper(Printer);
119 while (auto Compiland = Compilands->getNext())
120 Dumper.start(*Compiland, outs(), 2, false);
126 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
128 TypeDumper Dumper(Printer, false, opts::ClassDefs);
129 Dumper.start(*GlobalScope, outs(), 2);
135 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
137 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
138 CompilandDumper Dumper(Printer);
139 while (auto Compiland = Compilands->getNext())
140 Dumper.start(*Compiland, outs(), 2, true);
146 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
149 FunctionDumper Dumper(Printer);
150 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
151 while (auto Function = Functions->getNext()) {
153 Dumper.start(*Function, FunctionDumper::PointerType::None, outs(), 2);
157 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
158 VariableDumper Dumper(Printer);
159 while (auto Var = Vars->getNext())
160 Dumper.start(*Var, outs(), 2);
163 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
164 CompilandDumper Dumper(Printer);
165 while (auto Thunk = Thunks->getNext())
166 Dumper.dump(*Thunk, outs(), 2);
173 int main(int argc_, const char *argv_[]) {
174 // Print a stack trace if we signal out.
175 sys::PrintStackTraceOnErrorSignal();
176 PrettyStackTraceProgram X(argc_, argv_);
178 SmallVector<const char *, 256> argv;
179 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
180 std::error_code EC = llvm::sys::Process::GetArgumentVector(
181 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
183 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
187 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
189 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
191 #if defined(HAVE_DIA_SDK)
192 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
195 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
198 #if defined(HAVE_DIA_SDK)