Rewrite llvm-pdbdump in terms of LLVMDebugInfoPDB.
[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/DebugInfo/PDB/PDB.h"
19 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
20 #include "llvm/DebugInfo/PDB/IPDBSession.h"
21 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
22 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
23 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/ConvertUTF.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/ManagedStatic.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Support/Process.h"
30 #include "llvm/Support/PrettyStackTrace.h"
31 #include "llvm/Support/Signals.h"
32
33 #include <Windows.h>
34
35 using namespace llvm;
36
37 namespace opts {
38 cl::list<std::string> InputFilenames(cl::Positional,
39                                      cl::desc("<input PDB files>"),
40                                      cl::OneOrMore);
41
42 cl::opt<bool> Compilands("compilands",
43                          cl::desc("Display a list of compilands (e.g. object "
44                                   "files) and symbols for each one."));
45 cl::alias CompilandsShort("c", cl::desc("Alias for --compilands"),
46                           cl::aliasopt(Compilands));
47 }
48
49 static void dumpInput(StringRef Path) {
50   std::unique_ptr<IPDBSession> Session(
51       llvm::createPDBReader(PDB_ReaderType::DIA, Path));
52   if (!Session) {
53     outs() << "Unable to create PDB reader.  Check that a valid implementation";
54     outs() << " is available for your platform.";
55     return;
56   }
57
58   auto GlobalScope(Session->getGlobalScope());
59   GlobalScope->dump(outs(), 0, PDB_DumpLevel::Normal);
60   outs().flush();
61
62   if (opts::Compilands) {
63     auto Compilands = GlobalScope->findChildren(PDB_SymType::Compiland);
64     if (Compilands) {
65       while (auto Compiland = Compilands->getNext()) {
66         Compiland->dump(outs(), 0, PDB_DumpLevel::Normal);
67       }
68     }
69   }
70   outs().flush();
71 }
72
73 int main(int argc_, const char *argv_[]) {
74   // Print a stack trace if we signal out.
75   sys::PrintStackTraceOnErrorSignal();
76   PrettyStackTraceProgram X(argc_, argv_);
77
78   SmallVector<const char *, 256> argv;
79   llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
80   std::error_code EC = llvm::sys::Process::GetArgumentVector(
81       argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
82   if (EC) {
83     llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
84     return 1;
85   }
86
87   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
88
89   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
90
91   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
92
93   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
94                 dumpInput);
95
96   CoUninitialize();
97   return 0;
98 }