[llvm-pdbdump] Better error handling.
[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   PDB_ErrorCode Error =
71       llvm::createPDBReader(PDB_ReaderType::DIA, Path, Session);
72   switch (Error) {
73   case PDB_ErrorCode::Success:
74     break;
75   case PDB_ErrorCode::NoPdbImpl:
76     outs() << "Reading PDBs is not supported on this platform.\n";
77     return;
78   case PDB_ErrorCode::InvalidPath:
79     outs() << "Unable to load PDB at '" << Path
80            << "'.  Check that the file exists and is readable.\n";
81     return;
82   case PDB_ErrorCode::InvalidFileFormat:
83     outs() << "Unable to load PDB at '" << Path
84            << "'.  The file has an unrecognized format.\n";
85     return;
86   default:
87     outs() << "Unable to load PDB at '" << Path
88            << "'.  An unknown error occured.\n";
89     return;
90   }
91
92   LinePrinter Printer(2, outs());
93
94   auto GlobalScope(Session->getGlobalScope());
95   std::string FileName(GlobalScope->getSymbolsFileName());
96
97   WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
98   WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
99   Printer.Indent();
100   uint64_t FileSize = 0;
101
102   Printer.NewLine();
103   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
104   if (!llvm::sys::fs::file_size(FileName, FileSize)) {
105     Printer << ": " << FileSize << " bytes";
106   } else {
107     Printer << ": (Unable to obtain file size)";
108   }
109
110   Printer.NewLine();
111   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
112   Printer << ": " << GlobalScope->getGuid();
113
114   Printer.NewLine();
115   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
116   Printer << ": " << GlobalScope->getAge();
117
118   Printer.NewLine();
119   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
120   Printer << ": ";
121   if (GlobalScope->hasCTypes())
122     outs() << "HasCTypes ";
123   if (GlobalScope->hasPrivateSymbols())
124     outs() << "HasPrivateSymbols ";
125   Printer.Unindent();
126
127   if (opts::Compilands) {
128     Printer.NewLine();
129     WithColor(Printer, PDB_ColorItem::SectionHeader).get()
130         << "---COMPILANDS---";
131     Printer.Indent();
132     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
133     CompilandDumper Dumper(Printer);
134     while (auto Compiland = Compilands->getNext())
135       Dumper.start(*Compiland, outs(), 2, false);
136     Printer.Unindent();
137   }
138
139   if (opts::Types) {
140     Printer.NewLine();
141     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
142     Printer.Indent();
143     TypeDumper Dumper(Printer, false, opts::ClassDefs);
144     Dumper.start(*GlobalScope, outs(), 2);
145     Printer.Unindent();
146   }
147
148   if (opts::Symbols) {
149     Printer.NewLine();
150     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
151     Printer.Indent();
152     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
153     CompilandDumper Dumper(Printer);
154     while (auto Compiland = Compilands->getNext())
155       Dumper.start(*Compiland, outs(), 2, true);
156     Printer.Unindent();
157   }
158
159   if (opts::Globals) {
160     Printer.NewLine();
161     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
162     Printer.Indent();
163     {
164       FunctionDumper Dumper(Printer);
165       auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
166       while (auto Function = Functions->getNext()) {
167         Printer.NewLine();
168         Dumper.start(*Function, FunctionDumper::PointerType::None, outs(), 2);
169       }
170     }
171     {
172       auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
173       VariableDumper Dumper(Printer);
174       while (auto Var = Vars->getNext())
175         Dumper.start(*Var, outs(), 2);
176     }
177     {
178       auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
179       CompilandDumper Dumper(Printer);
180       while (auto Thunk = Thunks->getNext())
181         Dumper.dump(*Thunk, outs(), 2);
182     }
183     Printer.Unindent();
184   }
185   outs().flush();
186 }
187
188 int main(int argc_, const char *argv_[]) {
189   // Print a stack trace if we signal out.
190   sys::PrintStackTraceOnErrorSignal();
191   PrettyStackTraceProgram X(argc_, argv_);
192
193   SmallVector<const char *, 256> argv;
194   llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
195   std::error_code EC = llvm::sys::Process::GetArgumentVector(
196       argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
197   if (EC) {
198     llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
199     return 1;
200   }
201
202   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
203
204   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
205
206 #if defined(HAVE_DIA_SDK)
207   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
208 #endif
209
210   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
211                 dumpInput);
212
213 #if defined(HAVE_DIA_SDK)
214   CoUninitialize();
215 #endif
216
217   return 0;
218 }