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