5bc33d06e3814312e1500a92b442e393ac5ee350
[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 "LinePrinter.h"
20 #include "TypeDumper.h"
21 #include "VariableDumper.h"
22
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"
44
45 #if defined(HAVE_DIA_SDK)
46 #include <Windows.h>
47 #endif
48
49 using namespace llvm;
50
51 namespace opts {
52
53 enum class PDB_DumpType { ByType, ByObjFile, Both };
54
55 cl::list<std::string> InputFilenames(cl::Positional,
56                                      cl::desc("<input PDB files>"),
57                                      cl::OneOrMore);
58
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"));
66 }
67
68 static void dumpInput(StringRef Path) {
69   std::unique_ptr<IPDBSession> Session(
70       llvm::createPDBReader(PDB_ReaderType::DIA, Path));
71   if (!Session) {
72     outs() << "Unable to create PDB reader.  Check that a valid implementation";
73     outs() << " is available for your platform.";
74     return;
75   }
76
77   LinePrinter Printer(2, outs());
78
79   auto GlobalScope(Session->getGlobalScope());
80   std::string FileName(GlobalScope->getSymbolsFileName());
81
82   WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
83   WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
84   Printer.Indent();
85   uint64_t FileSize = 0;
86
87   Printer.NewLine();
88   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
89   if (!llvm::sys::fs::file_size(FileName, FileSize)) {
90     Printer << ": " << FileSize << " bytes";
91   } else {
92     Printer << ": (Unable to obtain file size)";
93   }
94
95   Printer.NewLine();
96   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
97   Printer << ": " << GlobalScope->getGuid();
98
99   Printer.NewLine();
100   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
101   Printer << ": " << GlobalScope->getAge();
102
103   Printer.NewLine();
104   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
105   Printer << ": ";
106   if (GlobalScope->hasCTypes())
107     outs() << "HasCTypes ";
108   if (GlobalScope->hasPrivateSymbols())
109     outs() << "HasPrivateSymbols ";
110   Printer.Unindent();
111
112   if (opts::Compilands) {
113     Printer.NewLine();
114     WithColor(Printer, PDB_ColorItem::SectionHeader).get()
115         << "---COMPILANDS---";
116     Printer.Indent();
117     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
118     CompilandDumper Dumper(Printer);
119     while (auto Compiland = Compilands->getNext())
120       Dumper.start(*Compiland, outs(), 2, false);
121     Printer.Unindent();
122   }
123
124   if (opts::Types) {
125     Printer.NewLine();
126     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
127     Printer.Indent();
128     TypeDumper Dumper(Printer, false, opts::ClassDefs);
129     Dumper.start(*GlobalScope, outs(), 2);
130     Printer.Unindent();
131   }
132
133   if (opts::Symbols) {
134     Printer.NewLine();
135     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
136     Printer.Indent();
137     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
138     CompilandDumper Dumper(Printer);
139     while (auto Compiland = Compilands->getNext())
140       Dumper.start(*Compiland, outs(), 2, true);
141     Printer.Unindent();
142   }
143
144   if (opts::Globals) {
145     Printer.NewLine();
146     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
147     Printer.Indent();
148     {
149       FunctionDumper Dumper(Printer);
150       auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
151       while (auto Function = Functions->getNext()) {
152         Printer.NewLine();
153         Dumper.start(*Function, FunctionDumper::PointerType::None, outs(), 2);
154       }
155     }
156     {
157       auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
158       VariableDumper Dumper(Printer);
159       while (auto Var = Vars->getNext())
160         Dumper.start(*Var, outs(), 2);
161     }
162     {
163       auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
164       CompilandDumper Dumper(Printer);
165       while (auto Thunk = Thunks->getNext())
166         Dumper.dump(*Thunk, outs(), 2);
167     }
168     Printer.Unindent();
169   }
170   outs().flush();
171 }
172
173 int main(int argc_, const char *argv_[]) {
174   // Print a stack trace if we signal out.
175   sys::PrintStackTraceOnErrorSignal();
176   PrettyStackTraceProgram X(argc_, argv_);
177
178   SmallVector<const char *, 256> argv;
179   llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
180   std::error_code EC = llvm::sys::Process::GetArgumentVector(
181       argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
182   if (EC) {
183     llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
184     return 1;
185   }
186
187   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
188
189   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
190
191 #if defined(HAVE_DIA_SDK)
192   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
193 #endif
194
195   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
196                 dumpInput);
197
198 #if defined(HAVE_DIA_SDK)
199   CoUninitialize();
200 #endif
201
202   return 0;
203 }