OSDN Git Service

Update aosp/master LLVM for rebase to r230699.
[android-x86/external-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 "TypeDumper.h"
19
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/Config/config.h"
23 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
24 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
25 #include "llvm/DebugInfo/PDB/IPDBSession.h"
26 #include "llvm/DebugInfo/PDB/PDB.h"
27 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
28 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/ConvertUTF.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/Format.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/PrettyStackTrace.h"
35 #include "llvm/Support/Process.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/Signals.h"
38
39 #if defined(HAVE_DIA_SDK)
40 #include <Windows.h>
41 #endif
42
43 using namespace llvm;
44
45 namespace opts {
46
47 enum class PDB_DumpType { ByType, ByObjFile, Both };
48
49 cl::list<std::string> InputFilenames(cl::Positional,
50                                      cl::desc("<input PDB files>"),
51                                      cl::OneOrMore);
52
53 cl::opt<bool> DumpCompilands("compilands", cl::desc("Display compilands"));
54 cl::opt<bool> DumpSymbols("symbols",
55                           cl::desc("Display symbols (implies --compilands"));
56 cl::opt<bool> DumpTypes("types", cl::desc("Display types"));
57 cl::opt<bool> DumpClassDefs("class-definitions",
58                             cl::desc("Display full class definitions"));
59 }
60
61 static void dumpInput(StringRef Path) {
62   std::unique_ptr<IPDBSession> Session(
63       llvm::createPDBReader(PDB_ReaderType::DIA, Path));
64   if (!Session) {
65     outs() << "Unable to create PDB reader.  Check that a valid implementation";
66     outs() << " is available for your platform.";
67     return;
68   }
69
70   auto GlobalScope(Session->getGlobalScope());
71   std::string FileName(GlobalScope->getSymbolsFileName());
72
73   outs() << "Summary for " << FileName;
74   uint64_t FileSize = 0;
75   if (!llvm::sys::fs::file_size(FileName, FileSize))
76     outs() << newline(2) << "Size: " << FileSize << " bytes";
77   else
78     outs() << newline(2) << "Size: (Unable to obtain file size)";
79
80   outs() << newline(2) << "Guid: " << GlobalScope->getGuid();
81   outs() << newline(2) << "Age: " << GlobalScope->getAge();
82   outs() << newline(2) << "Attributes: ";
83   if (GlobalScope->hasCTypes())
84     outs() << "HasCTypes ";
85   if (GlobalScope->hasPrivateSymbols())
86     outs() << "HasPrivateSymbols ";
87
88   if (opts::DumpTypes) {
89     outs() << "\nDumping types";
90     TypeDumper Dumper(false, opts::DumpClassDefs);
91     Dumper.start(*GlobalScope, outs(), 2);
92   }
93
94   if (opts::DumpSymbols || opts::DumpCompilands) {
95     outs() << "\nDumping compilands";
96     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
97     CompilandDumper Dumper;
98     while (auto Compiland = Compilands->getNext())
99       Dumper.start(*Compiland, outs(), 2, opts::DumpSymbols);
100   }
101   outs().flush();
102 }
103
104 int main(int argc_, const char *argv_[]) {
105   // Print a stack trace if we signal out.
106   sys::PrintStackTraceOnErrorSignal();
107   PrettyStackTraceProgram X(argc_, argv_);
108
109   SmallVector<const char *, 256> argv;
110   llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
111   std::error_code EC = llvm::sys::Process::GetArgumentVector(
112       argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
113   if (EC) {
114     llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
115     return 1;
116   }
117
118   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
119
120   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
121
122 #if defined(HAVE_DIA_SDK)
123   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
124 #endif
125
126   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
127                 dumpInput);
128
129 #if defined(HAVE_DIA_SDK)
130   CoUninitialize();
131 #endif
132
133   return 0;
134 }