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 "ExternalSymbolDumper.h"
19 #include "FunctionDumper.h"
20 #include "LinePrinter.h"
21 #include "TypeDumper.h"
22 #include "VariableDumper.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Config/config.h"
27 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
28 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
29 #include "llvm/DebugInfo/PDB/IPDBSession.h"
30 #include "llvm/DebugInfo/PDB/PDB.h"
31 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
32 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
33 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
34 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
35 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/ConvertUTF.h"
38 #include "llvm/Support/FileSystem.h"
39 #include "llvm/Support/Format.h"
40 #include "llvm/Support/ManagedStatic.h"
41 #include "llvm/Support/PrettyStackTrace.h"
42 #include "llvm/Support/Process.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Support/Signals.h"
46 #if defined(HAVE_DIA_SDK)
54 enum class PDB_DumpType { ByType, ByObjFile, Both };
56 cl::list<std::string> InputFilenames(cl::Positional,
57 cl::desc("<input PDB files>"),
60 cl::OptionCategory TypeCategory("Symbol Type Options");
61 cl::OptionCategory FilterCategory("Filtering Options");
62 cl::OptionCategory OtherOptions("Other Options");
64 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
65 cl::cat(TypeCategory));
66 cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
67 cl::cat(TypeCategory));
68 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
69 cl::cat(TypeCategory));
70 cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
71 cl::cat(TypeCategory));
72 cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory));
74 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
75 cl::cat(TypeCategory));
77 cl::opt<uint64_t> LoadAddress(
79 cl::desc("Assume the module is loaded at the specified address"),
80 cl::cat(OtherOptions));
83 ExcludeTypes("exclude-types",
84 cl::desc("Exclude types by regular expression"),
85 cl::ZeroOrMore, cl::cat(FilterCategory));
87 ExcludeSymbols("exclude-symbols",
88 cl::desc("Exclude symbols by regular expression"),
89 cl::ZeroOrMore, cl::cat(FilterCategory));
91 ExcludeCompilands("exclude-compilands",
92 cl::desc("Exclude compilands by regular expression"),
93 cl::ZeroOrMore, cl::cat(FilterCategory));
95 cl::list<std::string> IncludeTypes(
97 cl::desc("Include only types which match a regular expression"),
98 cl::ZeroOrMore, cl::cat(FilterCategory));
99 cl::list<std::string> IncludeSymbols(
101 cl::desc("Include only symbols which match a regular expression"),
102 cl::ZeroOrMore, cl::cat(FilterCategory));
103 cl::list<std::string> IncludeCompilands(
104 "include-compilands",
105 cl::desc("Include only compilands those which match a regular expression"),
106 cl::ZeroOrMore, cl::cat(FilterCategory));
108 cl::opt<bool> ExcludeCompilerGenerated(
109 "no-compiler-generated",
110 cl::desc("Don't show compiler generated types and symbols"),
111 cl::cat(FilterCategory));
113 ExcludeSystemLibraries("no-system-libs",
114 cl::desc("Don't show symbols from system libraries"),
115 cl::cat(FilterCategory));
116 cl::opt<bool> NoClassDefs("no-class-definitions",
117 cl::desc("Don't display full class definitions"),
118 cl::cat(FilterCategory));
119 cl::opt<bool> NoEnumDefs("no-enum-definitions",
120 cl::desc("Don't display full enum definitions"),
121 cl::cat(FilterCategory));
124 static void dumpInput(StringRef Path) {
125 std::unique_ptr<IPDBSession> Session;
126 PDB_ErrorCode Error =
127 llvm::loadDataForPDB(PDB_ReaderType::DIA, Path, Session);
129 case PDB_ErrorCode::Success:
131 case PDB_ErrorCode::NoPdbImpl:
132 outs() << "Reading PDBs is not supported on this platform.\n";
134 case PDB_ErrorCode::InvalidPath:
135 outs() << "Unable to load PDB at '" << Path
136 << "'. Check that the file exists and is readable.\n";
138 case PDB_ErrorCode::InvalidFileFormat:
139 outs() << "Unable to load PDB at '" << Path
140 << "'. The file has an unrecognized format.\n";
143 outs() << "Unable to load PDB at '" << Path
144 << "'. An unknown error occured.\n";
147 if (opts::LoadAddress)
148 Session->setLoadAddress(opts::LoadAddress);
150 LinePrinter Printer(2, outs());
152 auto GlobalScope(Session->getGlobalScope());
153 std::string FileName(GlobalScope->getSymbolsFileName());
155 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
156 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
158 uint64_t FileSize = 0;
161 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
162 if (!llvm::sys::fs::file_size(FileName, FileSize)) {
163 Printer << ": " << FileSize << " bytes";
165 Printer << ": (Unable to obtain file size)";
169 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
170 Printer << ": " << GlobalScope->getGuid();
173 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
174 Printer << ": " << GlobalScope->getAge();
177 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
179 if (GlobalScope->hasCTypes())
180 outs() << "HasCTypes ";
181 if (GlobalScope->hasPrivateSymbols())
182 outs() << "HasPrivateSymbols ";
185 if (opts::Compilands) {
187 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
188 << "---COMPILANDS---";
190 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
191 CompilandDumper Dumper(Printer);
192 while (auto Compiland = Compilands->getNext())
193 Dumper.start(*Compiland, false);
199 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
201 TypeDumper Dumper(Printer);
202 Dumper.start(*GlobalScope);
208 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
210 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
211 CompilandDumper Dumper(Printer);
212 while (auto Compiland = Compilands->getNext())
213 Dumper.start(*Compiland, true);
219 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
222 FunctionDumper Dumper(Printer);
223 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
224 while (auto Function = Functions->getNext()) {
226 Dumper.start(*Function, FunctionDumper::PointerType::None);
230 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
231 VariableDumper Dumper(Printer);
232 while (auto Var = Vars->getNext())
236 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
237 CompilandDumper Dumper(Printer);
238 while (auto Thunk = Thunks->getNext())
243 if (opts::Externals) {
245 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
247 ExternalSymbolDumper Dumper(Printer);
248 Dumper.start(*GlobalScope);
253 int main(int argc_, const char *argv_[]) {
254 // Print a stack trace if we signal out.
255 sys::PrintStackTraceOnErrorSignal();
256 PrettyStackTraceProgram X(argc_, argv_);
258 SmallVector<const char *, 256> argv;
259 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
260 std::error_code EC = llvm::sys::Process::GetArgumentVector(
261 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
263 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
267 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
269 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
271 opts::Compilands = true;
272 opts::Symbols = true;
273 opts::Globals = true;
275 opts::Externals = true;
277 if (opts::ExcludeCompilerGenerated) {
278 opts::ExcludeTypes.push_back("__vc_attributes");
279 opts::ExcludeCompilands.push_back("* Linker *");
281 if (opts::ExcludeSystemLibraries) {
282 opts::ExcludeCompilands.push_back(
283 "f:\\binaries\\Intermediate\\vctools\\crt_bld");
286 #if defined(HAVE_DIA_SDK)
287 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
290 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
293 #if defined(HAVE_DIA_SDK)