blob: 24b2b79c5c293108ca53b708065c5cceb8afb9b9 [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 Turnere5cb2692015-05-01 20:24:26 +000018#include "ExternalSymbolDumper.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000019#include "FunctionDumper.h"
Zachary Turner2d11c202015-02-27 09:15:59 +000020#include "LinePrinter.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000021#include "TypeDumper.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000022#include "VariableDumper.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000023
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000024#include "llvm/ADT/ArrayRef.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000025#include "llvm/ADT/StringExtras.h"
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000026#include "llvm/Config/config.h"
Zachary Turnera5549172015-02-10 22:43:25 +000027#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000028#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000029#include "llvm/DebugInfo/PDB/IPDBSession.h"
30#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000031#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000032#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000033#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000034#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
35#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/ConvertUTF.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000038#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000039#include "llvm/Support/Format.h"
40#include "llvm/Support/ManagedStatic.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000041#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000042#include "llvm/Support/Process.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000043#include "llvm/Support/raw_ostream.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000044#include "llvm/Support/Signals.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000045
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000046#if defined(HAVE_DIA_SDK)
Zachary Turnera5549172015-02-10 22:43:25 +000047#include <Windows.h>
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000048#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000049
50using namespace llvm;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000051
52namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000053
54enum class PDB_DumpType { ByType, ByObjFile, Both };
55
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000056cl::list<std::string> InputFilenames(cl::Positional,
57 cl::desc("<input PDB files>"),
58 cl::OneOrMore);
59
Zachary Turner7797c722015-03-02 04:39:56 +000060cl::OptionCategory TypeCategory("Symbol Type Options");
61cl::OptionCategory FilterCategory("Filtering Options");
Zachary Turnere5cb2692015-05-01 20:24:26 +000062cl::OptionCategory OtherOptions("Other Options");
Zachary Turner7797c722015-03-02 04:39:56 +000063
64cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
65 cl::cat(TypeCategory));
66cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
67 cl::cat(TypeCategory));
68cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
69 cl::cat(TypeCategory));
Zachary Turnere5cb2692015-05-01 20:24:26 +000070cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
71 cl::cat(TypeCategory));
Zachary Turner7797c722015-03-02 04:39:56 +000072cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory));
73cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +000074 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
75 cl::cat(TypeCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +000076
Zachary Turnere5cb2692015-05-01 20:24:26 +000077cl::opt<uint64_t> LoadAddress(
78 "load-address",
79 cl::desc("Assume the module is loaded at the specified address"),
80 cl::cat(OtherOptions));
81
Zachary Turnerf5abda22015-03-01 06:49:49 +000082cl::list<std::string>
83 ExcludeTypes("exclude-types",
84 cl::desc("Exclude types by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +000085 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +000086cl::list<std::string>
87 ExcludeSymbols("exclude-symbols",
88 cl::desc("Exclude symbols by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +000089 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +000090cl::list<std::string>
91 ExcludeCompilands("exclude-compilands",
92 cl::desc("Exclude compilands by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +000093 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turner4dddcc62015-09-29 19:49:06 +000094
95cl::list<std::string> IncludeTypes(
96 "include-types",
97 cl::desc("Include only types which match a regular expression"),
98 cl::ZeroOrMore, cl::cat(FilterCategory));
99cl::list<std::string> IncludeSymbols(
100 "include-symbols",
101 cl::desc("Include only symbols which match a regular expression"),
102 cl::ZeroOrMore, cl::cat(FilterCategory));
103cl::list<std::string> IncludeCompilands(
104 "include-compilands",
105 cl::desc("Include only compilands those which match a regular expression"),
106 cl::ZeroOrMore, cl::cat(FilterCategory));
107
Zachary Turner7797c722015-03-02 04:39:56 +0000108cl::opt<bool> ExcludeCompilerGenerated(
109 "no-compiler-generated",
110 cl::desc("Don't show compiler generated types and symbols"),
111 cl::cat(FilterCategory));
112cl::opt<bool>
113 ExcludeSystemLibraries("no-system-libs",
114 cl::desc("Don't show symbols from system libraries"),
115 cl::cat(FilterCategory));
Zachary Turner65323652015-03-04 06:09:53 +0000116cl::opt<bool> NoClassDefs("no-class-definitions",
117 cl::desc("Don't display full class definitions"),
118 cl::cat(FilterCategory));
119cl::opt<bool> NoEnumDefs("no-enum-definitions",
120 cl::desc("Don't display full enum definitions"),
121 cl::cat(FilterCategory));
Zachary Turner49693b42015-01-28 01:22:33 +0000122}
123
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000124static void dumpInput(StringRef Path) {
Zachary Turnerccf04152015-02-28 20:23:18 +0000125 std::unique_ptr<IPDBSession> Session;
126 PDB_ErrorCode Error =
Zachary Turner4b083542015-04-17 22:40:36 +0000127 llvm::loadDataForPDB(PDB_ReaderType::DIA, Path, Session);
Zachary Turnerccf04152015-02-28 20:23:18 +0000128 switch (Error) {
129 case PDB_ErrorCode::Success:
130 break;
131 case PDB_ErrorCode::NoPdbImpl:
132 outs() << "Reading PDBs is not supported on this platform.\n";
133 return;
134 case PDB_ErrorCode::InvalidPath:
135 outs() << "Unable to load PDB at '" << Path
136 << "'. Check that the file exists and is readable.\n";
137 return;
138 case PDB_ErrorCode::InvalidFileFormat:
139 outs() << "Unable to load PDB at '" << Path
140 << "'. The file has an unrecognized format.\n";
141 return;
142 default:
143 outs() << "Unable to load PDB at '" << Path
144 << "'. An unknown error occured.\n";
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000145 return;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000146 }
Zachary Turnere5cb2692015-05-01 20:24:26 +0000147 if (opts::LoadAddress)
148 Session->setLoadAddress(opts::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000149
Zachary Turner2d11c202015-02-27 09:15:59 +0000150 LinePrinter Printer(2, outs());
151
Zachary Turnera5549172015-02-10 22:43:25 +0000152 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000153 std::string FileName(GlobalScope->getSymbolsFileName());
154
Zachary Turner2d11c202015-02-27 09:15:59 +0000155 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
156 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
157 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000158 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000159
Zachary Turner2d11c202015-02-27 09:15:59 +0000160 Printer.NewLine();
161 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
162 if (!llvm::sys::fs::file_size(FileName, FileSize)) {
163 Printer << ": " << FileSize << " bytes";
164 } else {
165 Printer << ": (Unable to obtain file size)";
166 }
167
168 Printer.NewLine();
169 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
170 Printer << ": " << GlobalScope->getGuid();
171
172 Printer.NewLine();
173 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
174 Printer << ": " << GlobalScope->getAge();
175
176 Printer.NewLine();
177 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
178 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000179 if (GlobalScope->hasCTypes())
180 outs() << "HasCTypes ";
181 if (GlobalScope->hasPrivateSymbols())
182 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000183 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000184
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000185 if (opts::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000186 Printer.NewLine();
187 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
188 << "---COMPILANDS---";
189 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000190 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000191 CompilandDumper Dumper(Printer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000192 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000193 Dumper.start(*Compiland, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000194 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000195 }
196
197 if (opts::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000198 Printer.NewLine();
199 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
200 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000201 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000202 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000203 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000204 }
205
206 if (opts::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000207 Printer.NewLine();
208 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
209 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000210 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000211 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000212 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000213 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000214 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000215 }
216
217 if (opts::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000218 Printer.NewLine();
219 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
220 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000221 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000222 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000223 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000224 while (auto Function = Functions->getNext()) {
225 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000226 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000227 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000228 }
229 {
230 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000231 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000232 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000233 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000234 }
235 {
236 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000237 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000238 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000239 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000240 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000241 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000242 }
Zachary Turnere5cb2692015-05-01 20:24:26 +0000243 if (opts::Externals) {
244 Printer.NewLine();
245 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
246 Printer.Indent();
247 ExternalSymbolDumper Dumper(Printer);
248 Dumper.start(*GlobalScope);
249 }
Zachary Turnera5549172015-02-10 22:43:25 +0000250 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000251}
252
253int main(int argc_, const char *argv_[]) {
254 // Print a stack trace if we signal out.
255 sys::PrintStackTraceOnErrorSignal();
256 PrettyStackTraceProgram X(argc_, argv_);
257
258 SmallVector<const char *, 256> argv;
259 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
260 std::error_code EC = llvm::sys::Process::GetArgumentVector(
261 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
262 if (EC) {
263 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
264 return 1;
265 }
266
267 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
268
269 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turner7797c722015-03-02 04:39:56 +0000270 if (opts::All) {
271 opts::Compilands = true;
272 opts::Symbols = true;
273 opts::Globals = true;
274 opts::Types = true;
Zachary Turnere5cb2692015-05-01 20:24:26 +0000275 opts::Externals = true;
Zachary Turner7797c722015-03-02 04:39:56 +0000276 }
277 if (opts::ExcludeCompilerGenerated) {
278 opts::ExcludeTypes.push_back("__vc_attributes");
279 opts::ExcludeCompilands.push_back("* Linker *");
280 }
281 if (opts::ExcludeSystemLibraries) {
282 opts::ExcludeCompilands.push_back(
283 "f:\\binaries\\Intermediate\\vctools\\crt_bld");
284 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000285
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000286#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000287 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000288#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000289
290 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
291 dumpInput);
292
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000293#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000294 CoUninitialize();
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000295#endif
296
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000297 return 0;
298}