ce79c7933f29eed7237067f0758b4a2e94abdae7
[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::Detailed);
69       outs() << "\n";
70     }
71   }
72   outs().flush();
73 }
74
75 int main(int argc_, const char *argv_[]) {
76   // Print a stack trace if we signal out.
77   sys::PrintStackTraceOnErrorSignal();
78   PrettyStackTraceProgram X(argc_, argv_);
79
80   SmallVector<const char *, 256> argv;
81   llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
82   std::error_code EC = llvm::sys::Process::GetArgumentVector(
83       argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
84   if (EC) {
85     llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
86     return 1;
87   }
88
89   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
90
91   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
92
93 #if defined(HAVE_DIA_SDK)
94   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
95 #endif
96
97   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
98                 dumpInput);
99
100 #if defined(HAVE_DIA_SDK)
101   CoUninitialize();
102 #endif
103
104   return 0;
105 }