blob: e8a105d35d2ac33f77e523e6360f3a2fcc332c00 [file] [log] [blame]
Zachary Turnerfcb14ad2015-01-27 20:46:21 +00001//===- 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
Zachary Turner9a818ad2015-02-22 22:03:38 +000016#include "llvm-pdbdump.h"
17#include "CompilandDumper.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000018#include "FunctionDumper.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000019#include "TypeDumper.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000020#include "VariableDumper.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000021
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000022#include "llvm/ADT/ArrayRef.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000023#include "llvm/ADT/StringExtras.h"
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000024#include "llvm/Config/config.h"
Zachary Turnera5549172015-02-10 22:43:25 +000025#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000026#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000027#include "llvm/DebugInfo/PDB/IPDBSession.h"
28#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000029#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000030#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000031#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000032#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
33#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/ConvertUTF.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000036#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000037#include "llvm/Support/Format.h"
38#include "llvm/Support/ManagedStatic.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000039#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000040#include "llvm/Support/Process.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000041#include "llvm/Support/raw_ostream.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000042#include "llvm/Support/Signals.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000043
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000044#if defined(HAVE_DIA_SDK)
Zachary Turnera5549172015-02-10 22:43:25 +000045#include <Windows.h>
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000046#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000047
48using namespace llvm;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000049
50namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000051
52enum class PDB_DumpType { ByType, ByObjFile, Both };
53
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000054cl::list<std::string> InputFilenames(cl::Positional,
55 cl::desc("<input PDB files>"),
56 cl::OneOrMore);
57
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000058cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"));
59cl::opt<bool> Symbols("symbols",
60 cl::desc("Display symbols for each compiland"));
61cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"));
62cl::opt<bool> Types("types", cl::desc("Display types"));
63cl::opt<bool> ClassDefs("class-definitions",
64 cl::desc("Display full class definitions"));
Zachary Turner49693b42015-01-28 01:22:33 +000065}
66
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000067static void dumpInput(StringRef Path) {
Zachary Turnera5549172015-02-10 22:43:25 +000068 std::unique_ptr<IPDBSession> Session(
69 llvm::createPDBReader(PDB_ReaderType::DIA, Path));
70 if (!Session) {
71 outs() << "Unable to create PDB reader. Check that a valid implementation";
72 outs() << " is available for your platform.";
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000073 return;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000074 }
Zachary Turner7058dfc2015-01-27 22:40:14 +000075
Zachary Turnera5549172015-02-10 22:43:25 +000076 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +000077 std::string FileName(GlobalScope->getSymbolsFileName());
78
79 outs() << "Summary for " << FileName;
80 uint64_t FileSize = 0;
81 if (!llvm::sys::fs::file_size(FileName, FileSize))
82 outs() << newline(2) << "Size: " << FileSize << " bytes";
83 else
84 outs() << newline(2) << "Size: (Unable to obtain file size)";
85
86 outs() << newline(2) << "Guid: " << GlobalScope->getGuid();
87 outs() << newline(2) << "Age: " << GlobalScope->getAge();
88 outs() << newline(2) << "Attributes: ";
89 if (GlobalScope->hasCTypes())
90 outs() << "HasCTypes ";
91 if (GlobalScope->hasPrivateSymbols())
92 outs() << "HasPrivateSymbols ";
93
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000094 if (opts::Compilands) {
95 outs() << "\n---COMPILANDS---";
Zachary Turnerc074de02015-02-12 21:09:24 +000096 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner9a818ad2015-02-22 22:03:38 +000097 CompilandDumper Dumper;
98 while (auto Compiland = Compilands->getNext())
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000099 Dumper.start(*Compiland, outs(), 2, false);
100 }
101
102 if (opts::Types) {
103 outs() << "\n---TYPES---";
104 TypeDumper Dumper(false, opts::ClassDefs);
105 Dumper.start(*GlobalScope, outs(), 2);
106 }
107
108 if (opts::Symbols) {
109 outs() << "\n---SYMBOLS---";
110 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
111 CompilandDumper Dumper;
112 while (auto Compiland = Compilands->getNext())
113 Dumper.start(*Compiland, outs(), 2, true);
114 }
115
116 if (opts::Globals) {
117 outs() << "\n---GLOBALS---";
118 {
119 FunctionDumper Dumper;
120 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
121 while (auto Function = Functions->getNext())
122 Dumper.start(*Function, FunctionDumper::PointerType::None, outs(), 2);
123 }
124 {
125 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
126 VariableDumper Dumper;
127 while (auto Var = Vars->getNext())
128 Dumper.start(*Var, outs(), 2);
129 }
130 {
131 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
132 CompilandDumper Dumper;
133 while (auto Thunk = Thunks->getNext())
134 Dumper.dump(*Thunk, outs(), 2);
135 }
Zachary Turner7058dfc2015-01-27 22:40:14 +0000136 }
Zachary Turnera5549172015-02-10 22:43:25 +0000137 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000138}
139
140int main(int argc_, const char *argv_[]) {
141 // Print a stack trace if we signal out.
142 sys::PrintStackTraceOnErrorSignal();
143 PrettyStackTraceProgram X(argc_, argv_);
144
145 SmallVector<const char *, 256> argv;
146 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
147 std::error_code EC = llvm::sys::Process::GetArgumentVector(
148 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
149 if (EC) {
150 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
151 return 1;
152 }
153
154 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
155
156 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
157
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000158#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000159 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000160#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000161
162 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
163 dumpInput);
164
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000165#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000166 CoUninitialize();
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000167#endif
168
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000169 return 0;
170}