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