llvm-pdbdump: Fix gcc/clang build
[oota-llvm.git] / tools / llvm-pdbdump / llvm-pdbdump.cpp
1 //===- llvm-pdbdump.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
12 // platforms.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm-pdbdump.h"
17 #include "CompilandDumper.h"
18 #include "TypeDumper.h"
19
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"
38
39 #if defined(HAVE_DIA_SDK)
40 #include <Windows.h>
41 #endif
42
43 using namespace llvm;
44
45 namespace opts {
46
47 enum class PDB_DumpType { ByType, ByObjFile, Both };
48
49 cl::list<std::string> InputFilenames(cl::Positional,
50                                      cl::desc("<input PDB files>"),
51                                      cl::OneOrMore);
52
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 }
58
59 static void dumpInput(StringRef Path) {
60   std::unique_ptr<IPDBSession> Session(
61       llvm::createPDBReader(PDB_ReaderType::DIA, Path));
62   if (!Session) {
63     outs() << "Unable to create PDB reader.  Check that a valid implementation";
64     outs() << " is available for your platform.";
65     return;
66   }
67
68   auto GlobalScope(Session->getGlobalScope());
69   std::string FileName(GlobalScope->getSymbolsFileName());
70
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";
75   else
76     outs() << newline(2) << "Size: (Unable to obtain file size)";
77
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 ";
85
86   if (opts::DumpTypes) {
87     outs() << "\nDumping types";
88     TypeDumper Dumper;
89     Dumper.start(*GlobalScope, outs(), 2);
90   }
91
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);
98   }
99   outs().flush();
100 }
101
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_);
106
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);
111   if (EC) {
112     llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
113     return 1;
114   }
115
116   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
117
118   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
119
120 #if defined(HAVE_DIA_SDK)
121   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
122 #endif
123
124   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
125                 dumpInput);
126
127 #if defined(HAVE_DIA_SDK)
128   CoUninitialize();
129 #endif
130
131   return 0;
132 }