[DAGCombiner] Tidyup FMINNUM/FMAXNUM constant folding
[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 "ExternalSymbolDumper.h"
19 #include "FunctionDumper.h"
20 #include "LinePrinter.h"
21 #include "TypeDumper.h"
22 #include "VariableDumper.h"
23
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/StringExtras.h"
26 #include "llvm/Config/config.h"
27 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
28 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
29 #include "llvm/DebugInfo/PDB/IPDBSession.h"
30 #include "llvm/DebugInfo/PDB/PDB.h"
31 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
32 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
33 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
34 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
35 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/ConvertUTF.h"
38 #include "llvm/Support/FileSystem.h"
39 #include "llvm/Support/Format.h"
40 #include "llvm/Support/ManagedStatic.h"
41 #include "llvm/Support/PrettyStackTrace.h"
42 #include "llvm/Support/Process.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Support/Signals.h"
45
46 #if defined(HAVE_DIA_SDK)
47 #include <Windows.h>
48 #endif
49
50 using namespace llvm;
51
52 namespace opts {
53
54 enum class PDB_DumpType { ByType, ByObjFile, Both };
55
56 cl::list<std::string> InputFilenames(cl::Positional,
57                                      cl::desc("<input PDB files>"),
58                                      cl::OneOrMore);
59
60 cl::OptionCategory TypeCategory("Symbol Type Options");
61 cl::OptionCategory FilterCategory("Filtering Options");
62 cl::OptionCategory OtherOptions("Other Options");
63
64 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
65                          cl::cat(TypeCategory));
66 cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
67                       cl::cat(TypeCategory));
68 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
69                       cl::cat(TypeCategory));
70 cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
71                         cl::cat(TypeCategory));
72 cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory));
73 cl::opt<bool>
74     All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
75         cl::cat(TypeCategory));
76
77 cl::opt<uint64_t> LoadAddress(
78     "load-address",
79     cl::desc("Assume the module is loaded at the specified address"),
80     cl::cat(OtherOptions));
81
82 cl::list<std::string>
83     ExcludeTypes("exclude-types",
84                  cl::desc("Exclude types by regular expression"),
85                  cl::ZeroOrMore, cl::cat(FilterCategory));
86 cl::list<std::string>
87     ExcludeSymbols("exclude-symbols",
88                    cl::desc("Exclude symbols by regular expression"),
89                    cl::ZeroOrMore, cl::cat(FilterCategory));
90 cl::list<std::string>
91     ExcludeCompilands("exclude-compilands",
92                       cl::desc("Exclude compilands by regular expression"),
93                       cl::ZeroOrMore, cl::cat(FilterCategory));
94
95 cl::list<std::string> IncludeTypes(
96     "include-types",
97     cl::desc("Include only types which match a regular expression"),
98     cl::ZeroOrMore, cl::cat(FilterCategory));
99 cl::list<std::string> IncludeSymbols(
100     "include-symbols",
101     cl::desc("Include only symbols which match a regular expression"),
102     cl::ZeroOrMore, cl::cat(FilterCategory));
103 cl::list<std::string> IncludeCompilands(
104     "include-compilands",
105     cl::desc("Include only compilands those which match a regular expression"),
106     cl::ZeroOrMore, cl::cat(FilterCategory));
107
108 cl::opt<bool> ExcludeCompilerGenerated(
109     "no-compiler-generated",
110     cl::desc("Don't show compiler generated types and symbols"),
111     cl::cat(FilterCategory));
112 cl::opt<bool>
113     ExcludeSystemLibraries("no-system-libs",
114                            cl::desc("Don't show symbols from system libraries"),
115                            cl::cat(FilterCategory));
116 cl::opt<bool> NoClassDefs("no-class-definitions",
117                           cl::desc("Don't display full class definitions"),
118                           cl::cat(FilterCategory));
119 cl::opt<bool> NoEnumDefs("no-enum-definitions",
120                          cl::desc("Don't display full enum definitions"),
121                          cl::cat(FilterCategory));
122 }
123
124 static void dumpInput(StringRef Path) {
125   std::unique_ptr<IPDBSession> Session;
126   PDB_ErrorCode Error =
127       llvm::loadDataForPDB(PDB_ReaderType::DIA, Path, Session);
128   switch (Error) {
129   case PDB_ErrorCode::Success:
130     break;
131   case PDB_ErrorCode::NoPdbImpl:
132     outs() << "Reading PDBs is not supported on this platform.\n";
133     return;
134   case PDB_ErrorCode::InvalidPath:
135     outs() << "Unable to load PDB at '" << Path
136            << "'.  Check that the file exists and is readable.\n";
137     return;
138   case PDB_ErrorCode::InvalidFileFormat:
139     outs() << "Unable to load PDB at '" << Path
140            << "'.  The file has an unrecognized format.\n";
141     return;
142   default:
143     outs() << "Unable to load PDB at '" << Path
144            << "'.  An unknown error occured.\n";
145     return;
146   }
147   if (opts::LoadAddress)
148     Session->setLoadAddress(opts::LoadAddress);
149
150   LinePrinter Printer(2, outs());
151
152   auto GlobalScope(Session->getGlobalScope());
153   std::string FileName(GlobalScope->getSymbolsFileName());
154
155   WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
156   WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
157   Printer.Indent();
158   uint64_t FileSize = 0;
159
160   Printer.NewLine();
161   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
162   if (!llvm::sys::fs::file_size(FileName, FileSize)) {
163     Printer << ": " << FileSize << " bytes";
164   } else {
165     Printer << ": (Unable to obtain file size)";
166   }
167
168   Printer.NewLine();
169   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
170   Printer << ": " << GlobalScope->getGuid();
171
172   Printer.NewLine();
173   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
174   Printer << ": " << GlobalScope->getAge();
175
176   Printer.NewLine();
177   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
178   Printer << ": ";
179   if (GlobalScope->hasCTypes())
180     outs() << "HasCTypes ";
181   if (GlobalScope->hasPrivateSymbols())
182     outs() << "HasPrivateSymbols ";
183   Printer.Unindent();
184
185   if (opts::Compilands) {
186     Printer.NewLine();
187     WithColor(Printer, PDB_ColorItem::SectionHeader).get()
188         << "---COMPILANDS---";
189     Printer.Indent();
190     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
191     CompilandDumper Dumper(Printer);
192     while (auto Compiland = Compilands->getNext())
193       Dumper.start(*Compiland, false);
194     Printer.Unindent();
195   }
196
197   if (opts::Types) {
198     Printer.NewLine();
199     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
200     Printer.Indent();
201     TypeDumper Dumper(Printer);
202     Dumper.start(*GlobalScope);
203     Printer.Unindent();
204   }
205
206   if (opts::Symbols) {
207     Printer.NewLine();
208     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
209     Printer.Indent();
210     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
211     CompilandDumper Dumper(Printer);
212     while (auto Compiland = Compilands->getNext())
213       Dumper.start(*Compiland, true);
214     Printer.Unindent();
215   }
216
217   if (opts::Globals) {
218     Printer.NewLine();
219     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
220     Printer.Indent();
221     {
222       FunctionDumper Dumper(Printer);
223       auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
224       while (auto Function = Functions->getNext()) {
225         Printer.NewLine();
226         Dumper.start(*Function, FunctionDumper::PointerType::None);
227       }
228     }
229     {
230       auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
231       VariableDumper Dumper(Printer);
232       while (auto Var = Vars->getNext())
233         Dumper.start(*Var);
234     }
235     {
236       auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
237       CompilandDumper Dumper(Printer);
238       while (auto Thunk = Thunks->getNext())
239         Dumper.dump(*Thunk);
240     }
241     Printer.Unindent();
242   }
243   if (opts::Externals) {
244     Printer.NewLine();
245     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
246     Printer.Indent();
247     ExternalSymbolDumper Dumper(Printer);
248     Dumper.start(*GlobalScope);
249   }
250   outs().flush();
251 }
252
253 int main(int argc_, const char *argv_[]) {
254   // Print a stack trace if we signal out.
255   sys::PrintStackTraceOnErrorSignal();
256   PrettyStackTraceProgram X(argc_, argv_);
257
258   SmallVector<const char *, 256> argv;
259   llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
260   std::error_code EC = llvm::sys::Process::GetArgumentVector(
261       argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
262   if (EC) {
263     llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
264     return 1;
265   }
266
267   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
268
269   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
270   if (opts::All) {
271     opts::Compilands = true;
272     opts::Symbols = true;
273     opts::Globals = true;
274     opts::Types = true;
275     opts::Externals = true;
276   }
277   if (opts::ExcludeCompilerGenerated) {
278     opts::ExcludeTypes.push_back("__vc_attributes");
279     opts::ExcludeCompilands.push_back("* Linker *");
280   }
281   if (opts::ExcludeSystemLibraries) {
282     opts::ExcludeCompilands.push_back(
283         "f:\\binaries\\Intermediate\\vctools\\crt_bld");
284   }
285
286 #if defined(HAVE_DIA_SDK)
287   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
288 #endif
289
290   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
291                 dumpInput);
292
293 #if defined(HAVE_DIA_SDK)
294   CoUninitialize();
295 #endif
296
297   return 0;
298 }