blob: e1f0370b55ae7b7d10c974fb86ca9c55497c6db2 [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 Turner2d11c202015-02-27 09:15:59 +000019#include "LinePrinter.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000020#include "TypeDumper.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000021#include "VariableDumper.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000022
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000023#include "llvm/ADT/ArrayRef.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000024#include "llvm/ADT/StringExtras.h"
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000025#include "llvm/Config/config.h"
Zachary Turnera5549172015-02-10 22:43:25 +000026#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000027#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000028#include "llvm/DebugInfo/PDB/IPDBSession.h"
29#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000030#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000031#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000032#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000033#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
34#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000035#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/ConvertUTF.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000037#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000038#include "llvm/Support/Format.h"
39#include "llvm/Support/ManagedStatic.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000040#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000041#include "llvm/Support/Process.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000042#include "llvm/Support/raw_ostream.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000043#include "llvm/Support/Signals.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000044
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000045#if defined(HAVE_DIA_SDK)
Zachary Turnera5549172015-02-10 22:43:25 +000046#include <Windows.h>
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000047#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000048
49using namespace llvm;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000050
51namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000052
53enum class PDB_DumpType { ByType, ByObjFile, Both };
54
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000055cl::list<std::string> InputFilenames(cl::Positional,
56 cl::desc("<input PDB files>"),
57 cl::OneOrMore);
58
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000059cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"));
60cl::opt<bool> Symbols("symbols",
61 cl::desc("Display symbols for each compiland"));
62cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"));
63cl::opt<bool> Types("types", cl::desc("Display types"));
64cl::opt<bool> ClassDefs("class-definitions",
65 cl::desc("Display full class definitions"));
Zachary Turnerf5abda22015-03-01 06:49:49 +000066
67cl::list<std::string>
68 ExcludeTypes("exclude-types",
69 cl::desc("Exclude types by regular expression"),
70 cl::ZeroOrMore);
71cl::list<std::string>
72 ExcludeSymbols("exclude-symbols",
73 cl::desc("Exclude symbols by regular expression"),
74 cl::ZeroOrMore);
75cl::list<std::string>
76 ExcludeCompilands("exclude-compilands",
77 cl::desc("Exclude compilands by regular expression"),
78 cl::ZeroOrMore);
Zachary Turner49693b42015-01-28 01:22:33 +000079}
80
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000081static void dumpInput(StringRef Path) {
Zachary Turnerccf04152015-02-28 20:23:18 +000082 std::unique_ptr<IPDBSession> Session;
83 PDB_ErrorCode Error =
84 llvm::createPDBReader(PDB_ReaderType::DIA, Path, Session);
85 switch (Error) {
86 case PDB_ErrorCode::Success:
87 break;
88 case PDB_ErrorCode::NoPdbImpl:
89 outs() << "Reading PDBs is not supported on this platform.\n";
90 return;
91 case PDB_ErrorCode::InvalidPath:
92 outs() << "Unable to load PDB at '" << Path
93 << "'. Check that the file exists and is readable.\n";
94 return;
95 case PDB_ErrorCode::InvalidFileFormat:
96 outs() << "Unable to load PDB at '" << Path
97 << "'. The file has an unrecognized format.\n";
98 return;
99 default:
100 outs() << "Unable to load PDB at '" << Path
101 << "'. An unknown error occured.\n";
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000102 return;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000103 }
Zachary Turner7058dfc2015-01-27 22:40:14 +0000104
Zachary Turner2d11c202015-02-27 09:15:59 +0000105 LinePrinter Printer(2, outs());
Zachary Turnerf5abda22015-03-01 06:49:49 +0000106 Printer.SetTypeFilters(opts::ExcludeTypes.begin(), opts::ExcludeTypes.end());
107 Printer.SetSymbolFilters(opts::ExcludeSymbols.begin(),
108 opts::ExcludeSymbols.end());
109 Printer.SetCompilandFilters(opts::ExcludeCompilands.begin(),
110 opts::ExcludeCompilands.end());
Zachary Turner2d11c202015-02-27 09:15:59 +0000111
Zachary Turnera5549172015-02-10 22:43:25 +0000112 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000113 std::string FileName(GlobalScope->getSymbolsFileName());
114
Zachary Turner2d11c202015-02-27 09:15:59 +0000115 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
116 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
117 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000118 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000119
Zachary Turner2d11c202015-02-27 09:15:59 +0000120 Printer.NewLine();
121 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
122 if (!llvm::sys::fs::file_size(FileName, FileSize)) {
123 Printer << ": " << FileSize << " bytes";
124 } else {
125 Printer << ": (Unable to obtain file size)";
126 }
127
128 Printer.NewLine();
129 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
130 Printer << ": " << GlobalScope->getGuid();
131
132 Printer.NewLine();
133 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
134 Printer << ": " << GlobalScope->getAge();
135
136 Printer.NewLine();
137 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
138 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000139 if (GlobalScope->hasCTypes())
140 outs() << "HasCTypes ";
141 if (GlobalScope->hasPrivateSymbols())
142 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000143 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000144
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000145 if (opts::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000146 Printer.NewLine();
147 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
148 << "---COMPILANDS---";
149 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000150 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000151 CompilandDumper Dumper(Printer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000152 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000153 Dumper.start(*Compiland, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000154 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000155 }
156
157 if (opts::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000158 Printer.NewLine();
159 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
160 Printer.Indent();
Zachary Turnerf5abda22015-03-01 06:49:49 +0000161 TypeDumper Dumper(Printer, opts::ClassDefs);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000162 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000163 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000164 }
165
166 if (opts::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000167 Printer.NewLine();
168 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
169 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000170 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000171 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000172 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000173 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000174 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000175 }
176
177 if (opts::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000178 Printer.NewLine();
179 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
180 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000181 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000182 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000183 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000184 while (auto Function = Functions->getNext()) {
185 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000186 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000187 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000188 }
189 {
190 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000191 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000192 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000193 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000194 }
195 {
196 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000197 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000198 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000199 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000200 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000201 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000202 }
Zachary Turnera5549172015-02-10 22:43:25 +0000203 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000204}
205
206int main(int argc_, const char *argv_[]) {
207 // Print a stack trace if we signal out.
208 sys::PrintStackTraceOnErrorSignal();
209 PrettyStackTraceProgram X(argc_, argv_);
210
211 SmallVector<const char *, 256> argv;
212 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
213 std::error_code EC = llvm::sys::Process::GetArgumentVector(
214 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
215 if (EC) {
216 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
217 return 1;
218 }
219
220 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
221
222 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
223
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000224#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000225 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000226#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000227
228 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
229 dumpInput);
230
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000231#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000232 CoUninitialize();
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000233#endif
234
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000235 return 0;
236}