[llvm-pdbdump] Simplify options and output.
[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/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Config/config.h"
19 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
20 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
21 #include "llvm/DebugInfo/PDB/IPDBSession.h"
22 #include "llvm/DebugInfo/PDB/PDB.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
24 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/ConvertUTF.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/PrettyStackTrace.h"
30 #include "llvm/Support/Process.h"
31 #include "llvm/Support/Signals.h"
32 #include "llvm/Support/raw_ostream.h"
33
34 #if defined(HAVE_DIA_SDK)
35 #include <Windows.h>
36 #endif
37
38 using namespace llvm;
39
40 namespace opts {
41
42 enum class PDB_DumpType { ByType, ByObjFile, Both };
43
44 cl::list<std::string> InputFilenames(cl::Positional,
45                                      cl::desc("<input PDB files>"),
46                                      cl::OneOrMore);
47
48 cl::opt<bool> DumpCompilands("compilands", cl::desc("Display compilands"));
49 cl::opt<bool> DumpSymbols("symbols",
50                           cl::desc("Display symbols (implies --compilands"));
51 cl::opt<bool> DumpTypes("types", cl::desc("Display types"));
52 }
53
54 static void dumpInput(StringRef Path) {
55   std::unique_ptr<IPDBSession> Session(
56       llvm::createPDBReader(PDB_ReaderType::DIA, Path));
57   if (!Session) {
58     outs() << "Unable to create PDB reader.  Check that a valid implementation";
59     outs() << " is available for your platform.";
60     return;
61   }
62
63   auto GlobalScope(Session->getGlobalScope());
64   PDB_DumpFlags Flags = PDB_DF_None;
65   if (opts::DumpTypes)
66     Flags |= PDB_DF_Children | PDB_DF_Enums | PDB_DF_Funcsigs |
67              PDB_DF_Typedefs | PDB_DF_VTables;
68   GlobalScope->dump(outs(), 0, PDB_DumpLevel::Normal, Flags);
69   outs() << "\n";
70
71   if (opts::DumpSymbols || opts::DumpCompilands) {
72     outs() << "Dumping compilands\n";
73     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
74     Flags = PDB_DF_None;
75     if (opts::DumpSymbols)
76       Flags |= PDB_DF_Children | PDB_DF_Data | PDB_DF_Functions |
77                PDB_DF_Thunks | PDB_DF_Labels;
78     while (auto Compiland = Compilands->getNext()) {
79       Compiland->dump(outs(), 2, PDB_DumpLevel::Detailed, Flags);
80       outs() << "\n";
81     }
82   }
83   outs().flush();
84 }
85
86 int main(int argc_, const char *argv_[]) {
87   // Print a stack trace if we signal out.
88   sys::PrintStackTraceOnErrorSignal();
89   PrettyStackTraceProgram X(argc_, argv_);
90
91   SmallVector<const char *, 256> argv;
92   llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
93   std::error_code EC = llvm::sys::Process::GetArgumentVector(
94       argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
95   if (EC) {
96     llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
97     return 1;
98   }
99
100   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
101
102   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
103
104 #if defined(HAVE_DIA_SDK)
105   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
106 #endif
107
108   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
109                 dumpInput);
110
111 #if defined(HAVE_DIA_SDK)
112   CoUninitialize();
113 #endif
114
115   return 0;
116 }