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 "TypeDumper.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/Config/config.h"
23 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
24 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
25 #include "llvm/DebugInfo/PDB/IPDBSession.h"
26 #include "llvm/DebugInfo/PDB/PDB.h"
27 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
28 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/ConvertUTF.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/Format.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/PrettyStackTrace.h"
35 #include "llvm/Support/Process.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/Signals.h"
39 #if defined(HAVE_DIA_SDK)
47 enum class PDB_DumpType { ByType, ByObjFile, Both };
49 cl::list<std::string> InputFilenames(cl::Positional,
50 cl::desc("<input PDB files>"),
53 cl::opt<bool> DumpCompilands("compilands", cl::desc("Display compilands"));
54 cl::opt<bool> DumpSymbols("symbols",
55 cl::desc("Display symbols (implies --compilands"));
56 cl::opt<bool> DumpTypes("types", cl::desc("Display types"));
57 cl::opt<bool> DumpClassDefs("class-definitions",
58 cl::desc("Display full class definitions"));
61 static void dumpInput(StringRef Path) {
62 std::unique_ptr<IPDBSession> Session(
63 llvm::createPDBReader(PDB_ReaderType::DIA, Path));
65 outs() << "Unable to create PDB reader. Check that a valid implementation";
66 outs() << " is available for your platform.";
70 auto GlobalScope(Session->getGlobalScope());
71 std::string FileName(GlobalScope->getSymbolsFileName());
73 outs() << "Summary for " << FileName;
74 uint64_t FileSize = 0;
75 if (!llvm::sys::fs::file_size(FileName, FileSize))
76 outs() << newline(2) << "Size: " << FileSize << " bytes";
78 outs() << newline(2) << "Size: (Unable to obtain file size)";
80 outs() << newline(2) << "Guid: " << GlobalScope->getGuid();
81 outs() << newline(2) << "Age: " << GlobalScope->getAge();
82 outs() << newline(2) << "Attributes: ";
83 if (GlobalScope->hasCTypes())
84 outs() << "HasCTypes ";
85 if (GlobalScope->hasPrivateSymbols())
86 outs() << "HasPrivateSymbols ";
88 if (opts::DumpTypes) {
89 outs() << "\nDumping types";
90 TypeDumper Dumper(false, opts::DumpClassDefs);
91 Dumper.start(*GlobalScope, outs(), 2);
94 if (opts::DumpSymbols || opts::DumpCompilands) {
95 outs() << "\nDumping compilands";
96 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
97 CompilandDumper Dumper;
98 while (auto Compiland = Compilands->getNext())
99 Dumper.start(*Compiland, outs(), 2, opts::DumpSymbols);
104 int main(int argc_, const char *argv_[]) {
105 // Print a stack trace if we signal out.
106 sys::PrintStackTraceOnErrorSignal();
107 PrettyStackTraceProgram X(argc_, argv_);
109 SmallVector<const char *, 256> argv;
110 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
111 std::error_code EC = llvm::sys::Process::GetArgumentVector(
112 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
114 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
118 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
120 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
122 #if defined(HAVE_DIA_SDK)
123 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
126 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
129 #if defined(HAVE_DIA_SDK)