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"));
59 static void dumpInput(StringRef Path) {
60 std::unique_ptr<IPDBSession> Session(
61 llvm::createPDBReader(PDB_ReaderType::DIA, Path));
63 outs() << "Unable to create PDB reader. Check that a valid implementation";
64 outs() << " is available for your platform.";
68 auto GlobalScope(Session->getGlobalScope());
69 std::string FileName(GlobalScope->getSymbolsFileName());
71 outs() << "Summary for " << FileName;
72 uint64_t FileSize = 0;
73 if (!llvm::sys::fs::file_size(FileName, FileSize))
74 outs() << newline(2) << "Size: " << FileSize << " bytes";
76 outs() << newline(2) << "Size: (Unable to obtain file size)";
78 outs() << newline(2) << "Guid: " << GlobalScope->getGuid();
79 outs() << newline(2) << "Age: " << GlobalScope->getAge();
80 outs() << newline(2) << "Attributes: ";
81 if (GlobalScope->hasCTypes())
82 outs() << "HasCTypes ";
83 if (GlobalScope->hasPrivateSymbols())
84 outs() << "HasPrivateSymbols ";
86 if (opts::DumpTypes) {
87 outs() << "\nDumping types";
89 Dumper.start(*GlobalScope, outs(), 2);
92 if (opts::DumpSymbols || opts::DumpCompilands) {
93 outs() << "\nDumping compilands";
94 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
95 CompilandDumper Dumper;
96 while (auto Compiland = Compilands->getNext())
97 Dumper.start(*Compiland, outs(), 2, opts::DumpSymbols);
102 int main(int argc_, const char *argv_[]) {
103 // Print a stack trace if we signal out.
104 sys::PrintStackTraceOnErrorSignal();
105 PrettyStackTraceProgram X(argc_, argv_);
107 SmallVector<const char *, 256> argv;
108 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
109 std::error_code EC = llvm::sys::Process::GetArgumentVector(
110 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
112 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
116 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
118 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
120 #if defined(HAVE_DIA_SDK)
121 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
124 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
127 #if defined(HAVE_DIA_SDK)