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;
71 llvm::createPDBReader(PDB_ReaderType::DIA, Path, Session);
73 case PDB_ErrorCode::Success:
75 case PDB_ErrorCode::NoPdbImpl:
76 outs() << "Reading PDBs is not supported on this platform.\n";
78 case PDB_ErrorCode::InvalidPath:
79 outs() << "Unable to load PDB at '" << Path
80 << "'. Check that the file exists and is readable.\n";
82 case PDB_ErrorCode::InvalidFileFormat:
83 outs() << "Unable to load PDB at '" << Path
84 << "'. The file has an unrecognized format.\n";
87 outs() << "Unable to load PDB at '" << Path
88 << "'. An unknown error occured.\n";
92 LinePrinter Printer(2, outs());
94 auto GlobalScope(Session->getGlobalScope());
95 std::string FileName(GlobalScope->getSymbolsFileName());
97 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
98 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
100 uint64_t FileSize = 0;
103 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
104 if (!llvm::sys::fs::file_size(FileName, FileSize)) {
105 Printer << ": " << FileSize << " bytes";
107 Printer << ": (Unable to obtain file size)";
111 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
112 Printer << ": " << GlobalScope->getGuid();
115 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
116 Printer << ": " << GlobalScope->getAge();
119 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
121 if (GlobalScope->hasCTypes())
122 outs() << "HasCTypes ";
123 if (GlobalScope->hasPrivateSymbols())
124 outs() << "HasPrivateSymbols ";
127 if (opts::Compilands) {
129 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
130 << "---COMPILANDS---";
132 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
133 CompilandDumper Dumper(Printer);
134 while (auto Compiland = Compilands->getNext())
135 Dumper.start(*Compiland, outs(), 2, false);
141 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
143 TypeDumper Dumper(Printer, false, opts::ClassDefs);
144 Dumper.start(*GlobalScope, outs(), 2);
150 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
152 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
153 CompilandDumper Dumper(Printer);
154 while (auto Compiland = Compilands->getNext())
155 Dumper.start(*Compiland, outs(), 2, true);
161 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
164 FunctionDumper Dumper(Printer);
165 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
166 while (auto Function = Functions->getNext()) {
168 Dumper.start(*Function, FunctionDumper::PointerType::None, outs(), 2);
172 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
173 VariableDumper Dumper(Printer);
174 while (auto Var = Vars->getNext())
175 Dumper.start(*Var, outs(), 2);
178 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
179 CompilandDumper Dumper(Printer);
180 while (auto Thunk = Thunks->getNext())
181 Dumper.dump(*Thunk, outs(), 2);
188 int main(int argc_, const char *argv_[]) {
189 // Print a stack trace if we signal out.
190 sys::PrintStackTraceOnErrorSignal();
191 PrettyStackTraceProgram X(argc_, argv_);
193 SmallVector<const char *, 256> argv;
194 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
195 std::error_code EC = llvm::sys::Process::GetArgumentVector(
196 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
198 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
202 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
204 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
206 #if defined(HAVE_DIA_SDK)
207 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
210 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
213 #if defined(HAVE_DIA_SDK)