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 ExcludeTypes("exclude-types",
69 cl::desc("Exclude types by regular expression"),
72 ExcludeSymbols("exclude-symbols",
73 cl::desc("Exclude symbols by regular expression"),
76 ExcludeCompilands("exclude-compilands",
77 cl::desc("Exclude compilands by regular expression"),
81 static void dumpInput(StringRef Path) {
82 std::unique_ptr<IPDBSession> Session;
84 llvm::createPDBReader(PDB_ReaderType::DIA, Path, Session);
86 case PDB_ErrorCode::Success:
88 case PDB_ErrorCode::NoPdbImpl:
89 outs() << "Reading PDBs is not supported on this platform.\n";
91 case PDB_ErrorCode::InvalidPath:
92 outs() << "Unable to load PDB at '" << Path
93 << "'. Check that the file exists and is readable.\n";
95 case PDB_ErrorCode::InvalidFileFormat:
96 outs() << "Unable to load PDB at '" << Path
97 << "'. The file has an unrecognized format.\n";
100 outs() << "Unable to load PDB at '" << Path
101 << "'. An unknown error occured.\n";
105 LinePrinter Printer(2, outs());
106 Printer.SetTypeFilters(opts::ExcludeTypes.begin(), opts::ExcludeTypes.end());
107 Printer.SetSymbolFilters(opts::ExcludeSymbols.begin(),
108 opts::ExcludeSymbols.end());
109 Printer.SetCompilandFilters(opts::ExcludeCompilands.begin(),
110 opts::ExcludeCompilands.end());
112 auto GlobalScope(Session->getGlobalScope());
113 std::string FileName(GlobalScope->getSymbolsFileName());
115 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
116 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
118 uint64_t FileSize = 0;
121 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
122 if (!llvm::sys::fs::file_size(FileName, FileSize)) {
123 Printer << ": " << FileSize << " bytes";
125 Printer << ": (Unable to obtain file size)";
129 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
130 Printer << ": " << GlobalScope->getGuid();
133 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
134 Printer << ": " << GlobalScope->getAge();
137 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
139 if (GlobalScope->hasCTypes())
140 outs() << "HasCTypes ";
141 if (GlobalScope->hasPrivateSymbols())
142 outs() << "HasPrivateSymbols ";
145 if (opts::Compilands) {
147 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
148 << "---COMPILANDS---";
150 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
151 CompilandDumper Dumper(Printer);
152 while (auto Compiland = Compilands->getNext())
153 Dumper.start(*Compiland, outs(), 2, false);
159 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
161 TypeDumper Dumper(Printer, opts::ClassDefs);
162 Dumper.start(*GlobalScope, outs(), 2);
168 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
170 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
171 CompilandDumper Dumper(Printer);
172 while (auto Compiland = Compilands->getNext())
173 Dumper.start(*Compiland, outs(), 2, true);
179 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
182 FunctionDumper Dumper(Printer);
183 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
184 while (auto Function = Functions->getNext()) {
186 Dumper.start(*Function, FunctionDumper::PointerType::None, outs(), 2);
190 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
191 VariableDumper Dumper(Printer);
192 while (auto Var = Vars->getNext())
193 Dumper.start(*Var, outs(), 2);
196 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
197 CompilandDumper Dumper(Printer);
198 while (auto Thunk = Thunks->getNext())
199 Dumper.dump(*Thunk, outs(), 2);
206 int main(int argc_, const char *argv_[]) {
207 // Print a stack trace if we signal out.
208 sys::PrintStackTraceOnErrorSignal();
209 PrettyStackTraceProgram X(argc_, argv_);
211 SmallVector<const char *, 256> argv;
212 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
213 std::error_code EC = llvm::sys::Process::GetArgumentVector(
214 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
216 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
220 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
222 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
224 #if defined(HAVE_DIA_SDK)
225 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
228 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
231 #if defined(HAVE_DIA_SDK)