e8a105d35d2ac33f77e523e6360f3a2fcc332c00
[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 "FunctionDumper.h"
19 #include "TypeDumper.h"
20 #include "VariableDumper.h"
21
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Config/config.h"
25 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
26 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
27 #include "llvm/DebugInfo/PDB/IPDBSession.h"
28 #include "llvm/DebugInfo/PDB/PDB.h"
29 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
30 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
31 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
32 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
33 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/ConvertUTF.h"
36 #include "llvm/Support/FileSystem.h"
37 #include "llvm/Support/Format.h"
38 #include "llvm/Support/ManagedStatic.h"
39 #include "llvm/Support/PrettyStackTrace.h"
40 #include "llvm/Support/Process.h"
41 #include "llvm/Support/raw_ostream.h"
42 #include "llvm/Support/Signals.h"
43
44 #if defined(HAVE_DIA_SDK)
45 #include <Windows.h>
46 #endif
47
48 using namespace llvm;
49
50 namespace opts {
51
52 enum class PDB_DumpType { ByType, ByObjFile, Both };
53
54 cl::list<std::string> InputFilenames(cl::Positional,
55                                      cl::desc("<input PDB files>"),
56                                      cl::OneOrMore);
57
58 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"));
59 cl::opt<bool> Symbols("symbols",
60                       cl::desc("Display symbols for each compiland"));
61 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"));
62 cl::opt<bool> Types("types", cl::desc("Display types"));
63 cl::opt<bool> ClassDefs("class-definitions",
64                         cl::desc("Display full class definitions"));
65 }
66
67 static void dumpInput(StringRef Path) {
68   std::unique_ptr<IPDBSession> Session(
69       llvm::createPDBReader(PDB_ReaderType::DIA, Path));
70   if (!Session) {
71     outs() << "Unable to create PDB reader.  Check that a valid implementation";
72     outs() << " is available for your platform.";
73     return;
74   }
75
76   auto GlobalScope(Session->getGlobalScope());
77   std::string FileName(GlobalScope->getSymbolsFileName());
78
79   outs() << "Summary for " << FileName;
80   uint64_t FileSize = 0;
81   if (!llvm::sys::fs::file_size(FileName, FileSize))
82     outs() << newline(2) << "Size: " << FileSize << " bytes";
83   else
84     outs() << newline(2) << "Size: (Unable to obtain file size)";
85
86   outs() << newline(2) << "Guid: " << GlobalScope->getGuid();
87   outs() << newline(2) << "Age: " << GlobalScope->getAge();
88   outs() << newline(2) << "Attributes: ";
89   if (GlobalScope->hasCTypes())
90     outs() << "HasCTypes ";
91   if (GlobalScope->hasPrivateSymbols())
92     outs() << "HasPrivateSymbols ";
93
94   if (opts::Compilands) {
95     outs() << "\n---COMPILANDS---";
96     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
97     CompilandDumper Dumper;
98     while (auto Compiland = Compilands->getNext())
99       Dumper.start(*Compiland, outs(), 2, false);
100   }
101
102   if (opts::Types) {
103     outs() << "\n---TYPES---";
104     TypeDumper Dumper(false, opts::ClassDefs);
105     Dumper.start(*GlobalScope, outs(), 2);
106   }
107
108   if (opts::Symbols) {
109     outs() << "\n---SYMBOLS---";
110     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
111     CompilandDumper Dumper;
112     while (auto Compiland = Compilands->getNext())
113       Dumper.start(*Compiland, outs(), 2, true);
114   }
115
116   if (opts::Globals) {
117     outs() << "\n---GLOBALS---";
118     {
119       FunctionDumper Dumper;
120       auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
121       while (auto Function = Functions->getNext())
122         Dumper.start(*Function, FunctionDumper::PointerType::None, outs(), 2);
123     }
124     {
125       auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
126       VariableDumper Dumper;
127       while (auto Var = Vars->getNext())
128         Dumper.start(*Var, outs(), 2);
129     }
130     {
131       auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
132       CompilandDumper Dumper;
133       while (auto Thunk = Thunks->getNext())
134         Dumper.dump(*Thunk, outs(), 2);
135     }
136   }
137   outs().flush();
138 }
139
140 int main(int argc_, const char *argv_[]) {
141   // Print a stack trace if we signal out.
142   sys::PrintStackTraceOnErrorSignal();
143   PrettyStackTraceProgram X(argc_, argv_);
144
145   SmallVector<const char *, 256> argv;
146   llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
147   std::error_code EC = llvm::sys::Process::GetArgumentVector(
148       argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
149   if (EC) {
150     llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
151     return 1;
152   }
153
154   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
155
156   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
157
158 #if defined(HAVE_DIA_SDK)
159   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
160 #endif
161
162   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
163                 dumpInput);
164
165 #if defined(HAVE_DIA_SDK)
166   CoUninitialize();
167 #endif
168
169   return 0;
170 }