blob: 4a4c64b80cc18aa0cfe41ab9032cfee4782f14a6 [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));
94cl::opt<bool> ExcludeCompilerGenerated(
95 "no-compiler-generated",
96 cl::desc("Don't show compiler generated types and symbols"),
97 cl::cat(FilterCategory));
98cl::opt<bool>
99 ExcludeSystemLibraries("no-system-libs",
100 cl::desc("Don't show symbols from system libraries"),
101 cl::cat(FilterCategory));
Zachary Turner65323652015-03-04 06:09:53 +0000102cl::opt<bool> NoClassDefs("no-class-definitions",
103 cl::desc("Don't display full class definitions"),
104 cl::cat(FilterCategory));
105cl::opt<bool> NoEnumDefs("no-enum-definitions",
106 cl::desc("Don't display full enum definitions"),
107 cl::cat(FilterCategory));
Zachary Turner49693b42015-01-28 01:22:33 +0000108}
109
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000110static void dumpInput(StringRef Path) {
Zachary Turnerccf04152015-02-28 20:23:18 +0000111 std::unique_ptr<IPDBSession> Session;
112 PDB_ErrorCode Error =
Zachary Turner4b083542015-04-17 22:40:36 +0000113 llvm::loadDataForPDB(PDB_ReaderType::DIA, Path, Session);
Zachary Turnerccf04152015-02-28 20:23:18 +0000114 switch (Error) {
115 case PDB_ErrorCode::Success:
116 break;
117 case PDB_ErrorCode::NoPdbImpl:
118 outs() << "Reading PDBs is not supported on this platform.\n";
119 return;
120 case PDB_ErrorCode::InvalidPath:
121 outs() << "Unable to load PDB at '" << Path
122 << "'. Check that the file exists and is readable.\n";
123 return;
124 case PDB_ErrorCode::InvalidFileFormat:
125 outs() << "Unable to load PDB at '" << Path
126 << "'. The file has an unrecognized format.\n";
127 return;
128 default:
129 outs() << "Unable to load PDB at '" << Path
130 << "'. An unknown error occured.\n";
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000131 return;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000132 }
Zachary Turnere5cb2692015-05-01 20:24:26 +0000133 if (opts::LoadAddress)
134 Session->setLoadAddress(opts::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000135
Zachary Turner2d11c202015-02-27 09:15:59 +0000136 LinePrinter Printer(2, outs());
137
Zachary Turnera5549172015-02-10 22:43:25 +0000138 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000139 std::string FileName(GlobalScope->getSymbolsFileName());
140
Zachary Turner2d11c202015-02-27 09:15:59 +0000141 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
142 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
143 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000144 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000145
Zachary Turner2d11c202015-02-27 09:15:59 +0000146 Printer.NewLine();
147 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
148 if (!llvm::sys::fs::file_size(FileName, FileSize)) {
149 Printer << ": " << FileSize << " bytes";
150 } else {
151 Printer << ": (Unable to obtain file size)";
152 }
153
154 Printer.NewLine();
155 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
156 Printer << ": " << GlobalScope->getGuid();
157
158 Printer.NewLine();
159 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
160 Printer << ": " << GlobalScope->getAge();
161
162 Printer.NewLine();
163 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
164 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000165 if (GlobalScope->hasCTypes())
166 outs() << "HasCTypes ";
167 if (GlobalScope->hasPrivateSymbols())
168 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000169 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000170
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000171 if (opts::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000172 Printer.NewLine();
173 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
174 << "---COMPILANDS---";
175 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000176 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000177 CompilandDumper Dumper(Printer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000178 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000179 Dumper.start(*Compiland, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000180 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000181 }
182
183 if (opts::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000184 Printer.NewLine();
185 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
186 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000187 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000188 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000189 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000190 }
191
192 if (opts::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000193 Printer.NewLine();
194 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
195 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000196 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000197 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000198 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000199 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000200 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000201 }
202
203 if (opts::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000204 Printer.NewLine();
205 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
206 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000207 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000208 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000209 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000210 while (auto Function = Functions->getNext()) {
211 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000212 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000213 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000214 }
215 {
216 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000217 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000218 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000219 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000220 }
221 {
222 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000223 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000224 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000225 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000226 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000227 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000228 }
Zachary Turnere5cb2692015-05-01 20:24:26 +0000229 if (opts::Externals) {
230 Printer.NewLine();
231 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
232 Printer.Indent();
233 ExternalSymbolDumper Dumper(Printer);
234 Dumper.start(*GlobalScope);
235 }
Zachary Turnera5549172015-02-10 22:43:25 +0000236 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000237}
238
239int main(int argc_, const char *argv_[]) {
240 // Print a stack trace if we signal out.
241 sys::PrintStackTraceOnErrorSignal();
242 PrettyStackTraceProgram X(argc_, argv_);
243
244 SmallVector<const char *, 256> argv;
245 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
246 std::error_code EC = llvm::sys::Process::GetArgumentVector(
247 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
248 if (EC) {
249 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
250 return 1;
251 }
252
253 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
254
255 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turner7797c722015-03-02 04:39:56 +0000256 if (opts::All) {
257 opts::Compilands = true;
258 opts::Symbols = true;
259 opts::Globals = true;
260 opts::Types = true;
Zachary Turnere5cb2692015-05-01 20:24:26 +0000261 opts::Externals = true;
Zachary Turner7797c722015-03-02 04:39:56 +0000262 }
263 if (opts::ExcludeCompilerGenerated) {
264 opts::ExcludeTypes.push_back("__vc_attributes");
265 opts::ExcludeCompilands.push_back("* Linker *");
266 }
267 if (opts::ExcludeSystemLibraries) {
268 opts::ExcludeCompilands.push_back(
269 "f:\\binaries\\Intermediate\\vctools\\crt_bld");
270 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000271
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000272#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000273 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000274#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000275
276 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
277 dumpInput);
278
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000279#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000280 CoUninitialize();
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000281#endif
282
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000283 return 0;
284}