blob: 597683291ffd9a3c7383383031f528219dfac810 [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 Turner7797c722015-03-02 04:39:56 +000059cl::OptionCategory TypeCategory("Symbol Type Options");
60cl::OptionCategory FilterCategory("Filtering Options");
61
62cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
63 cl::cat(TypeCategory));
64cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
65 cl::cat(TypeCategory));
66cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
67 cl::cat(TypeCategory));
68cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory));
69cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +000070 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
71 cl::cat(TypeCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +000072
73cl::list<std::string>
74 ExcludeTypes("exclude-types",
75 cl::desc("Exclude types by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +000076 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +000077cl::list<std::string>
78 ExcludeSymbols("exclude-symbols",
79 cl::desc("Exclude symbols by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +000080 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +000081cl::list<std::string>
82 ExcludeCompilands("exclude-compilands",
83 cl::desc("Exclude compilands by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +000084 cl::ZeroOrMore, cl::cat(FilterCategory));
85cl::opt<bool> ExcludeCompilerGenerated(
86 "no-compiler-generated",
87 cl::desc("Don't show compiler generated types and symbols"),
88 cl::cat(FilterCategory));
89cl::opt<bool>
90 ExcludeSystemLibraries("no-system-libs",
91 cl::desc("Don't show symbols from system libraries"),
92 cl::cat(FilterCategory));
Zachary Turner65323652015-03-04 06:09:53 +000093cl::opt<bool> NoClassDefs("no-class-definitions",
94 cl::desc("Don't display full class definitions"),
95 cl::cat(FilterCategory));
96cl::opt<bool> NoEnumDefs("no-enum-definitions",
97 cl::desc("Don't display full enum definitions"),
98 cl::cat(FilterCategory));
Zachary Turner49693b42015-01-28 01:22:33 +000099}
100
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000101static void dumpInput(StringRef Path) {
Zachary Turnerccf04152015-02-28 20:23:18 +0000102 std::unique_ptr<IPDBSession> Session;
103 PDB_ErrorCode Error =
Zachary Turner4b083542015-04-17 22:40:36 +0000104 llvm::loadDataForPDB(PDB_ReaderType::DIA, Path, Session);
Zachary Turnerccf04152015-02-28 20:23:18 +0000105 switch (Error) {
106 case PDB_ErrorCode::Success:
107 break;
108 case PDB_ErrorCode::NoPdbImpl:
109 outs() << "Reading PDBs is not supported on this platform.\n";
110 return;
111 case PDB_ErrorCode::InvalidPath:
112 outs() << "Unable to load PDB at '" << Path
113 << "'. Check that the file exists and is readable.\n";
114 return;
115 case PDB_ErrorCode::InvalidFileFormat:
116 outs() << "Unable to load PDB at '" << Path
117 << "'. The file has an unrecognized format.\n";
118 return;
119 default:
120 outs() << "Unable to load PDB at '" << Path
121 << "'. An unknown error occured.\n";
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000122 return;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000123 }
Zachary Turner7058dfc2015-01-27 22:40:14 +0000124
Zachary Turner2d11c202015-02-27 09:15:59 +0000125 LinePrinter Printer(2, outs());
126
Zachary Turnera5549172015-02-10 22:43:25 +0000127 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000128 std::string FileName(GlobalScope->getSymbolsFileName());
129
Zachary Turner2d11c202015-02-27 09:15:59 +0000130 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
131 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
132 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000133 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000134
Zachary Turner2d11c202015-02-27 09:15:59 +0000135 Printer.NewLine();
136 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
137 if (!llvm::sys::fs::file_size(FileName, FileSize)) {
138 Printer << ": " << FileSize << " bytes";
139 } else {
140 Printer << ": (Unable to obtain file size)";
141 }
142
143 Printer.NewLine();
144 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
145 Printer << ": " << GlobalScope->getGuid();
146
147 Printer.NewLine();
148 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
149 Printer << ": " << GlobalScope->getAge();
150
151 Printer.NewLine();
152 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
153 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000154 if (GlobalScope->hasCTypes())
155 outs() << "HasCTypes ";
156 if (GlobalScope->hasPrivateSymbols())
157 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000158 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000159
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000160 if (opts::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000161 Printer.NewLine();
162 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
163 << "---COMPILANDS---";
164 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000165 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000166 CompilandDumper Dumper(Printer);
Zachary Turner9a818ad2015-02-22 22:03:38 +0000167 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000168 Dumper.start(*Compiland, false);
Zachary Turner2d11c202015-02-27 09:15:59 +0000169 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000170 }
171
172 if (opts::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000173 Printer.NewLine();
174 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
175 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000176 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000177 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000178 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000179 }
180
181 if (opts::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000182 Printer.NewLine();
183 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
184 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000185 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000186 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000187 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000188 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000189 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000190 }
191
192 if (opts::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000193 Printer.NewLine();
194 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
195 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000196 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000197 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000198 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000199 while (auto Function = Functions->getNext()) {
200 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000201 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000202 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000203 }
204 {
205 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000206 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000207 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000208 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000209 }
210 {
211 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000212 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000213 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000214 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000215 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000216 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000217 }
Zachary Turnera5549172015-02-10 22:43:25 +0000218 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000219}
220
221int main(int argc_, const char *argv_[]) {
222 // Print a stack trace if we signal out.
223 sys::PrintStackTraceOnErrorSignal();
224 PrettyStackTraceProgram X(argc_, argv_);
225
226 SmallVector<const char *, 256> argv;
227 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
228 std::error_code EC = llvm::sys::Process::GetArgumentVector(
229 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
230 if (EC) {
231 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
232 return 1;
233 }
234
235 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
236
237 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turner7797c722015-03-02 04:39:56 +0000238 if (opts::All) {
239 opts::Compilands = true;
240 opts::Symbols = true;
241 opts::Globals = true;
242 opts::Types = true;
Zachary Turner7797c722015-03-02 04:39:56 +0000243 }
244 if (opts::ExcludeCompilerGenerated) {
245 opts::ExcludeTypes.push_back("__vc_attributes");
246 opts::ExcludeCompilands.push_back("* Linker *");
247 }
248 if (opts::ExcludeSystemLibraries) {
249 opts::ExcludeCompilands.push_back(
250 "f:\\binaries\\Intermediate\\vctools\\crt_bld");
251 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000252
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000253#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000254 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000255#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000256
257 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
258 dumpInput);
259
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000260#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000261 CoUninitialize();
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000262#endif
263
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000264 return 0;
265}