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::OptionCategory TypeCategory("Symbol Type Options");
60 cl::OptionCategory FilterCategory("Filtering Options");
62 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
63 cl::cat(TypeCategory));
64 cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
65 cl::cat(TypeCategory));
66 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
67 cl::cat(TypeCategory));
68 cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory));
70 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
71 cl::cat(TypeCategory));
74 ExcludeTypes("exclude-types",
75 cl::desc("Exclude types by regular expression"),
76 cl::ZeroOrMore, cl::cat(FilterCategory));
78 ExcludeSymbols("exclude-symbols",
79 cl::desc("Exclude symbols by regular expression"),
80 cl::ZeroOrMore, cl::cat(FilterCategory));
82 ExcludeCompilands("exclude-compilands",
83 cl::desc("Exclude compilands by regular expression"),
84 cl::ZeroOrMore, cl::cat(FilterCategory));
85 cl::opt<bool> ExcludeCompilerGenerated(
86 "no-compiler-generated",
87 cl::desc("Don't show compiler generated types and symbols"),
88 cl::cat(FilterCategory));
90 ExcludeSystemLibraries("no-system-libs",
91 cl::desc("Don't show symbols from system libraries"),
92 cl::cat(FilterCategory));
93 cl::opt<bool> NoClassDefs("no-class-definitions",
94 cl::desc("Don't display full class definitions"),
95 cl::cat(FilterCategory));
96 cl::opt<bool> NoEnumDefs("no-enum-definitions",
97 cl::desc("Don't display full enum definitions"),
98 cl::cat(FilterCategory));
101 static void dumpInput(StringRef Path) {
102 std::unique_ptr<IPDBSession> Session;
103 PDB_ErrorCode Error =
104 llvm::createPDBReader(PDB_ReaderType::DIA, Path, Session);
106 case PDB_ErrorCode::Success:
108 case PDB_ErrorCode::NoPdbImpl:
109 outs() << "Reading PDBs is not supported on this platform.\n";
111 case PDB_ErrorCode::InvalidPath:
112 outs() << "Unable to load PDB at '" << Path
113 << "'. Check that the file exists and is readable.\n";
115 case PDB_ErrorCode::InvalidFileFormat:
116 outs() << "Unable to load PDB at '" << Path
117 << "'. The file has an unrecognized format.\n";
120 outs() << "Unable to load PDB at '" << Path
121 << "'. An unknown error occured.\n";
125 LinePrinter Printer(2, outs());
127 auto GlobalScope(Session->getGlobalScope());
128 std::string FileName(GlobalScope->getSymbolsFileName());
130 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
131 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
133 uint64_t FileSize = 0;
136 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
137 if (!llvm::sys::fs::file_size(FileName, FileSize)) {
138 Printer << ": " << FileSize << " bytes";
140 Printer << ": (Unable to obtain file size)";
144 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
145 Printer << ": " << GlobalScope->getGuid();
148 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
149 Printer << ": " << GlobalScope->getAge();
152 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
154 if (GlobalScope->hasCTypes())
155 outs() << "HasCTypes ";
156 if (GlobalScope->hasPrivateSymbols())
157 outs() << "HasPrivateSymbols ";
160 if (opts::Compilands) {
162 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
163 << "---COMPILANDS---";
165 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
166 CompilandDumper Dumper(Printer);
167 while (auto Compiland = Compilands->getNext())
168 Dumper.start(*Compiland, false);
174 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
176 TypeDumper Dumper(Printer);
177 Dumper.start(*GlobalScope);
183 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
185 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
186 CompilandDumper Dumper(Printer);
187 while (auto Compiland = Compilands->getNext())
188 Dumper.start(*Compiland, true);
194 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
197 FunctionDumper Dumper(Printer);
198 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
199 while (auto Function = Functions->getNext()) {
201 Dumper.start(*Function, FunctionDumper::PointerType::None);
205 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
206 VariableDumper Dumper(Printer);
207 while (auto Var = Vars->getNext())
211 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
212 CompilandDumper Dumper(Printer);
213 while (auto Thunk = Thunks->getNext())
221 int main(int argc_, const char *argv_[]) {
222 // Print a stack trace if we signal out.
223 sys::PrintStackTraceOnErrorSignal();
224 PrettyStackTraceProgram X(argc_, argv_);
226 SmallVector<const char *, 256> argv;
227 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
228 std::error_code EC = llvm::sys::Process::GetArgumentVector(
229 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
231 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
235 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
237 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
239 opts::Compilands = true;
240 opts::Symbols = true;
241 opts::Globals = true;
244 if (opts::ExcludeCompilerGenerated) {
245 opts::ExcludeTypes.push_back("__vc_attributes");
246 opts::ExcludeCompilands.push_back("* Linker *");
248 if (opts::ExcludeSystemLibraries) {
249 opts::ExcludeCompilands.push_back(
250 "f:\\binaries\\Intermediate\\vctools\\crt_bld");
253 #if defined(HAVE_DIA_SDK)
254 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
257 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
260 #if defined(HAVE_DIA_SDK)