[llvm-pdbdump] Provide a mechanism to dump the raw contents of a PDB
[oota-llvm.git] / tools / llvm-pdbdump / llvm-pdbdump.cpp
1 //===- 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
16 #include "llvm-pdbdump.h"
17 #include "CompilandDumper.h"
18 #include "ExternalSymbolDumper.h"
19 #include "FunctionDumper.h"
20 #include "LinePrinter.h"
21 #include "TypeDumper.h"
22 #include "VariableDumper.h"
23
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/BitVector.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/Config/config.h"
29 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
30 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
31 #include "llvm/DebugInfo/PDB/IPDBSession.h"
32 #include "llvm/DebugInfo/PDB/PDB.h"
33 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
34 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
35 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
36 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
37 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/ConvertUTF.h"
40 #include "llvm/Support/FileSystem.h"
41 #include "llvm/Support/Format.h"
42 #include "llvm/Support/ManagedStatic.h"
43 #include "llvm/Support/MemoryBuffer.h"
44 #include "llvm/Support/PrettyStackTrace.h"
45 #include "llvm/Support/Process.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/Signals.h"
48
49 #if defined(HAVE_DIA_SDK)
50 #include <Windows.h>
51 #endif
52
53 using namespace llvm;
54
55 namespace opts {
56
57 enum class PDB_DumpType { ByType, ByObjFile, Both };
58
59 cl::list<std::string> InputFilenames(cl::Positional,
60                                      cl::desc("<input PDB files>"),
61                                      cl::OneOrMore);
62
63 cl::OptionCategory TypeCategory("Symbol Type Options");
64 cl::OptionCategory FilterCategory("Filtering Options");
65 cl::OptionCategory OtherOptions("Other Options");
66
67 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
68                          cl::cat(TypeCategory));
69 cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
70                       cl::cat(TypeCategory));
71 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
72                       cl::cat(TypeCategory));
73 cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
74                         cl::cat(TypeCategory));
75 cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory));
76 cl::opt<bool>
77     All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
78         cl::cat(TypeCategory));
79
80 cl::opt<uint64_t> LoadAddress(
81     "load-address",
82     cl::desc("Assume the module is loaded at the specified address"),
83     cl::cat(OtherOptions));
84
85 cl::opt<bool> DumpHeaders("dump-headers", cl::desc("dump PDB headers"),
86                           cl::cat(OtherOptions));
87 cl::opt<bool> DumpStreamSizes("dump-stream-sizes",
88                               cl::desc("dump PDB stream sizes"),
89                               cl::cat(OtherOptions));
90 cl::opt<bool> DumpStreamBlocks("dump-stream-blocks",
91                                cl::desc("dump PDB stream blocks"),
92                                cl::cat(OtherOptions));
93 cl::opt<std::string> DumpStreamData("dump-stream", cl::desc("dump stream data"),
94                                     cl::cat(OtherOptions));
95
96 cl::list<std::string>
97     ExcludeTypes("exclude-types",
98                  cl::desc("Exclude types by regular expression"),
99                  cl::ZeroOrMore, cl::cat(FilterCategory));
100 cl::list<std::string>
101     ExcludeSymbols("exclude-symbols",
102                    cl::desc("Exclude symbols by regular expression"),
103                    cl::ZeroOrMore, cl::cat(FilterCategory));
104 cl::list<std::string>
105     ExcludeCompilands("exclude-compilands",
106                       cl::desc("Exclude compilands by regular expression"),
107                       cl::ZeroOrMore, cl::cat(FilterCategory));
108
109 cl::list<std::string> IncludeTypes(
110     "include-types",
111     cl::desc("Include only types which match a regular expression"),
112     cl::ZeroOrMore, cl::cat(FilterCategory));
113 cl::list<std::string> IncludeSymbols(
114     "include-symbols",
115     cl::desc("Include only symbols which match a regular expression"),
116     cl::ZeroOrMore, cl::cat(FilterCategory));
117 cl::list<std::string> IncludeCompilands(
118     "include-compilands",
119     cl::desc("Include only compilands those which match a regular expression"),
120     cl::ZeroOrMore, cl::cat(FilterCategory));
121
122 cl::opt<bool> ExcludeCompilerGenerated(
123     "no-compiler-generated",
124     cl::desc("Don't show compiler generated types and symbols"),
125     cl::cat(FilterCategory));
126 cl::opt<bool>
127     ExcludeSystemLibraries("no-system-libs",
128                            cl::desc("Don't show symbols from system libraries"),
129                            cl::cat(FilterCategory));
130 cl::opt<bool> NoClassDefs("no-class-definitions",
131                           cl::desc("Don't display full class definitions"),
132                           cl::cat(FilterCategory));
133 cl::opt<bool> NoEnumDefs("no-enum-definitions",
134                          cl::desc("Don't display full enum definitions"),
135                          cl::cat(FilterCategory));
136 }
137
138
139 static void reportError(StringRef Input, StringRef Message) {
140   if (Input == "-")
141     Input = "<stdin>";
142   errs() << Input << ": " << Message << "\n";
143   errs().flush();
144   exit(1);
145 }
146
147 static void reportError(StringRef Input, std::error_code EC) {
148   reportError(Input, EC.message());
149 }
150
151 static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
152                                    const uint64_t Size) {
153   if (Addr + Size < Addr || Addr + Size < Size ||
154       Addr + Size > uintptr_t(M.getBufferEnd()) ||
155       Addr < uintptr_t(M.getBufferStart())) {
156     return std::make_error_code(std::errc::bad_address);
157   }
158   return std::error_code();
159 }
160
161 template <typename T>
162 static std::error_code checkOffset(MemoryBufferRef M, ArrayRef<T> AR) {
163   return checkOffset(M, uintptr_t(AR.data()), (uint64_t)AR.size() * sizeof(T));
164 }
165
166 static std::error_code checkOffset(MemoryBufferRef M, StringRef SR) {
167   return checkOffset(M, uintptr_t(SR.data()), SR.size());
168 }
169
170 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
171 // Returns unexpected_eof if error.
172 template <typename T>
173 static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
174                                  const void *Ptr,
175                                  const uint64_t Size = sizeof(T)) {
176   uintptr_t Addr = uintptr_t(Ptr);
177   if (std::error_code EC = checkOffset(M, Addr, Size))
178     return EC;
179   Obj = reinterpret_cast<const T *>(Addr);
180   return std::error_code();
181 }
182
183 static uint64_t bytesToBlocks(uint64_t NumBytes, uint64_t BlockSize) {
184   return RoundUpToAlignment(NumBytes, BlockSize) / BlockSize;
185 }
186
187 static uint64_t blockToOffset(uint64_t BlockNumber, uint64_t BlockSize) {
188   return BlockNumber * BlockSize;
189 }
190
191 static void dumpStructure(MemoryBufferRef M) {
192   const PDB::SuperBlock *SB;
193   if (auto EC = getObject(SB, M, M.getBufferStart()))
194     reportError(M.getBufferIdentifier(), EC);
195
196   if (opts::DumpHeaders) {
197     outs() << "BlockSize: " << SB->BlockSize << '\n';
198     outs() << "Unknown0: " << SB->Unknown0 << '\n';
199     outs() << "NumBlocks: " << SB->NumBlocks << '\n';
200     outs() << "NumDirectoryBytes: " << SB->NumDirectoryBytes << '\n';
201     outs() << "Unknown1: " << SB->Unknown1 << '\n';
202     outs() << "BlockMapAddr: " << SB->BlockMapAddr << '\n';
203   }
204
205   // We don't support blocksizes which aren't a multiple of four bytes.
206   if (SB->BlockSize % sizeof(support::ulittle32_t) != 0)
207     reportError(M.getBufferIdentifier(),
208                 std::make_error_code(std::errc::illegal_byte_sequence));
209
210   // We don't support directories whose sizes aren't a multiple of four bytes.
211   if (SB->NumDirectoryBytes % sizeof(support::ulittle32_t) != 0)
212     reportError(M.getBufferIdentifier(),
213                 std::make_error_code(std::errc::illegal_byte_sequence));
214
215   // The number of blocks which comprise the directory is a simple function of
216   // the number of bytes it contains.
217   uint64_t NumDirectoryBlocks =
218       bytesToBlocks(SB->NumDirectoryBytes, SB->BlockSize);
219   if (opts::DumpHeaders)
220     outs() << "NumDirectoryBlocks: " << NumDirectoryBlocks << '\n';
221
222   // The block map, as we understand it, is a block which consists of a list of
223   // block numbers.
224   // It is unclear what would happen if the number of blocks couldn't fit on a
225   // single block.
226   if (NumDirectoryBlocks > SB->BlockSize / sizeof(support::ulittle32_t))
227     reportError(M.getBufferIdentifier(),
228                 std::make_error_code(std::errc::illegal_byte_sequence));
229
230
231   uint64_t BlockMapOffset = (uint64_t)SB->BlockMapAddr * SB->BlockSize;
232   if (opts::DumpHeaders)
233     outs() << "BlockMapOffset: " << BlockMapOffset << '\n';
234
235   // The directory is not contiguous.  Instead, the block map contains a
236   // contiguous list of block numbers whose contents, when concatenated in
237   // order, make up the directory.
238   auto DirectoryBlocks =
239       makeArrayRef(reinterpret_cast<const support::ulittle32_t *>(
240                        M.getBufferStart() + BlockMapOffset),
241                    NumDirectoryBlocks);
242   if (auto EC = checkOffset(M, DirectoryBlocks))
243     reportError(M.getBufferIdentifier(), EC);
244
245   if (opts::DumpHeaders) {
246     outs() << "DirectoryBlocks: [";
247     for (const support::ulittle32_t &DirectoryBlockAddr : DirectoryBlocks) {
248       if (&DirectoryBlockAddr != &DirectoryBlocks.front())
249         outs() << ", ";
250       outs() << DirectoryBlockAddr;
251     }
252     outs() << "]\n";
253   }
254
255   bool SeenNumStreams = false;
256   uint32_t NumStreams = 0;
257   std::vector<uint32_t> StreamSizes;
258   DenseMap<uint32_t, std::vector<uint32_t>> StreamMap;
259   uint32_t StreamIdx = 0;
260   uint64_t DirectoryBytesRead = 0;
261   // The structure of the directory is as follows:
262   //    struct PDBDirectory {
263   //      uint32_t NumStreams;
264   //      uint32_t StreamSizes[NumStreams];
265   //      uint32_t StreamMap[NumStreams][];
266   //    };
267   //
268   //  Empty streams don't consume entries in the StreamMap.
269   for (uint32_t DirectoryBlockAddr : DirectoryBlocks) {
270     uint64_t DirectoryBlockOffset =
271         blockToOffset(DirectoryBlockAddr, SB->BlockSize);
272     auto DirectoryBlock =
273         makeArrayRef(reinterpret_cast<const support::ulittle32_t *>(
274                          M.getBufferStart() + DirectoryBlockOffset),
275                      SB->BlockSize / sizeof(support::ulittle32_t));
276     if (auto EC = checkOffset(M, DirectoryBlock))
277       reportError(M.getBufferIdentifier(), EC);
278
279     // We read data out of the directory four bytes at a time.  Depending on
280     // where we are in the directory, the contents may be: the number of streams
281     // in the directory, a stream's size, or a block in the stream map.
282     for (uint32_t Data : DirectoryBlock) {
283       // Don't read beyond the end of the directory.
284       if (DirectoryBytesRead == SB->NumDirectoryBytes)
285         break;
286
287       DirectoryBytesRead += sizeof(Data);
288
289       // This data must be the number of streams if we haven't seen it yet.
290       if (!SeenNumStreams) {
291         NumStreams = Data;
292         SeenNumStreams = true;
293         continue;
294       }
295       // This data must be a stream size if we have not seen them all yet.
296       if (StreamSizes.size() < NumStreams) {
297         // It seems like some streams have their set to -1 when their contents
298         // are not present.  Treat them like empty streams for now.
299         if (Data == UINT32_MAX)
300           StreamSizes.push_back(0);
301         else
302           StreamSizes.push_back(Data);
303         continue;
304       }
305
306       // This data must be a stream block number if we have seen all of the
307       // stream sizes.
308       std::vector<uint32_t> *StreamBlocks = nullptr;
309       // Figure out which stream this block number belongs to.
310       while (StreamIdx < NumStreams) {
311         uint64_t NumExpectedStreamBlocks =
312             bytesToBlocks(StreamSizes[StreamIdx], SB->BlockSize);
313         StreamBlocks = &StreamMap[StreamIdx];
314         if (NumExpectedStreamBlocks > StreamBlocks->size())
315           break;
316         ++StreamIdx;
317       }
318       // It seems this block doesn't belong to any stream?  The stream is either
319       // corrupt or something more mysterious is going on.
320       if (StreamIdx == NumStreams)
321         reportError(M.getBufferIdentifier(),
322                     std::make_error_code(std::errc::illegal_byte_sequence));
323
324       StreamBlocks->push_back(Data);
325     }
326   }
327
328   // We should have read exactly SB->NumDirectoryBytes bytes.
329   assert(DirectoryBytesRead == SB->NumDirectoryBytes);
330
331   if (opts::DumpHeaders)
332     outs() << "NumStreams: " << NumStreams << '\n';
333   if (opts::DumpStreamSizes)
334     for (uint32_t StreamIdx = 0; StreamIdx < NumStreams; ++StreamIdx)
335       outs() << "StreamSizes[" << StreamIdx << "]: " << StreamSizes[StreamIdx]
336              << '\n';
337
338   if (opts::DumpStreamBlocks) {
339     for (uint32_t StreamIdx = 0; StreamIdx < NumStreams; ++StreamIdx) {
340       outs() << "StreamBlocks[" << StreamIdx << "]: [";
341       std::vector<uint32_t> &StreamBlocks = StreamMap[StreamIdx];
342       for (uint32_t &StreamBlock : StreamBlocks) {
343         if (&StreamBlock != &StreamBlocks.front())
344           outs() << ", ";
345         outs() << StreamBlock;
346       }
347       outs() << "]\n";
348     }
349   }
350
351   StringRef DumpStreamStr = opts::DumpStreamData;
352   uint32_t DumpStreamNum;
353   if (!DumpStreamStr.getAsInteger(/*Radix=*/0U, DumpStreamNum) &&
354       DumpStreamNum < NumStreams) {
355     uint32_t StreamBytesRead = 0;
356     uint32_t StreamSize = StreamSizes[DumpStreamNum];
357     std::vector<uint32_t> &StreamBlocks = StreamMap[DumpStreamNum];
358     for (uint32_t &StreamBlockAddr : StreamBlocks) {
359       uint64_t StreamBlockOffset = blockToOffset(StreamBlockAddr, SB->BlockSize);
360       uint32_t BytesLeftToReadInStream = StreamSize - StreamBytesRead;
361       if (BytesLeftToReadInStream == 0)
362         break;
363
364       uint32_t BytesToReadInBlock =
365           std::min(BytesLeftToReadInStream, static_cast<uint32_t>(SB->BlockSize));
366       auto StreamBlockData =
367           StringRef(M.getBufferStart() + StreamBlockOffset, BytesToReadInBlock);
368       if (auto EC = checkOffset(M, StreamBlockData))
369         reportError(M.getBufferIdentifier(), EC);
370
371       outs() << StreamBlockData;
372       StreamBytesRead += StreamBlockData.size();
373     }
374   }
375 }
376
377 static void dumpInput(StringRef Path) {
378   if (opts::DumpHeaders || !opts::DumpStreamData.empty()) {
379     ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
380         MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
381                                      /*RequiresNullTerminator=*/false);
382
383     if (std::error_code EC = ErrorOrBuffer.getError())
384       reportError(Path, EC);
385
386     std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
387
388     dumpStructure(Buffer->getMemBufferRef());
389
390     outs().flush();
391     return;
392   }
393
394   std::unique_ptr<IPDBSession> Session;
395   PDB_ErrorCode Error = loadDataForPDB(PDB_ReaderType::DIA, Path, Session);
396   switch (Error) {
397   case PDB_ErrorCode::Success:
398     break;
399   case PDB_ErrorCode::NoPdbImpl:
400     outs() << "Reading PDBs is not supported on this platform.\n";
401     return;
402   case PDB_ErrorCode::InvalidPath:
403     outs() << "Unable to load PDB at '" << Path
404            << "'.  Check that the file exists and is readable.\n";
405     return;
406   case PDB_ErrorCode::InvalidFileFormat:
407     outs() << "Unable to load PDB at '" << Path
408            << "'.  The file has an unrecognized format.\n";
409     return;
410   default:
411     outs() << "Unable to load PDB at '" << Path
412            << "'.  An unknown error occured.\n";
413     return;
414   }
415   if (opts::LoadAddress)
416     Session->setLoadAddress(opts::LoadAddress);
417
418   LinePrinter Printer(2, outs());
419
420   auto GlobalScope(Session->getGlobalScope());
421   std::string FileName(GlobalScope->getSymbolsFileName());
422
423   WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
424   WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
425   Printer.Indent();
426   uint64_t FileSize = 0;
427
428   Printer.NewLine();
429   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
430   if (!sys::fs::file_size(FileName, FileSize)) {
431     Printer << ": " << FileSize << " bytes";
432   } else {
433     Printer << ": (Unable to obtain file size)";
434   }
435
436   Printer.NewLine();
437   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
438   Printer << ": " << GlobalScope->getGuid();
439
440   Printer.NewLine();
441   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
442   Printer << ": " << GlobalScope->getAge();
443
444   Printer.NewLine();
445   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
446   Printer << ": ";
447   if (GlobalScope->hasCTypes())
448     outs() << "HasCTypes ";
449   if (GlobalScope->hasPrivateSymbols())
450     outs() << "HasPrivateSymbols ";
451   Printer.Unindent();
452
453   if (opts::Compilands) {
454     Printer.NewLine();
455     WithColor(Printer, PDB_ColorItem::SectionHeader).get()
456         << "---COMPILANDS---";
457     Printer.Indent();
458     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
459     CompilandDumper Dumper(Printer);
460     while (auto Compiland = Compilands->getNext())
461       Dumper.start(*Compiland, false);
462     Printer.Unindent();
463   }
464
465   if (opts::Types) {
466     Printer.NewLine();
467     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
468     Printer.Indent();
469     TypeDumper Dumper(Printer);
470     Dumper.start(*GlobalScope);
471     Printer.Unindent();
472   }
473
474   if (opts::Symbols) {
475     Printer.NewLine();
476     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
477     Printer.Indent();
478     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
479     CompilandDumper Dumper(Printer);
480     while (auto Compiland = Compilands->getNext())
481       Dumper.start(*Compiland, true);
482     Printer.Unindent();
483   }
484
485   if (opts::Globals) {
486     Printer.NewLine();
487     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
488     Printer.Indent();
489     {
490       FunctionDumper Dumper(Printer);
491       auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
492       while (auto Function = Functions->getNext()) {
493         Printer.NewLine();
494         Dumper.start(*Function, FunctionDumper::PointerType::None);
495       }
496     }
497     {
498       auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
499       VariableDumper Dumper(Printer);
500       while (auto Var = Vars->getNext())
501         Dumper.start(*Var);
502     }
503     {
504       auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
505       CompilandDumper Dumper(Printer);
506       while (auto Thunk = Thunks->getNext())
507         Dumper.dump(*Thunk);
508     }
509     Printer.Unindent();
510   }
511   if (opts::Externals) {
512     Printer.NewLine();
513     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
514     Printer.Indent();
515     ExternalSymbolDumper Dumper(Printer);
516     Dumper.start(*GlobalScope);
517   }
518   outs().flush();
519 }
520
521 int main(int argc_, const char *argv_[]) {
522   // Print a stack trace if we signal out.
523   sys::PrintStackTraceOnErrorSignal();
524   PrettyStackTraceProgram X(argc_, argv_);
525
526   SmallVector<const char *, 256> argv;
527   SpecificBumpPtrAllocator<char> ArgAllocator;
528   std::error_code EC = sys::Process::GetArgumentVector(
529       argv, makeArrayRef(argv_, argc_), ArgAllocator);
530   if (EC) {
531     errs() << "error: couldn't get arguments: " << EC.message() << '\n';
532     return 1;
533   }
534
535   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
536
537   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
538   if (opts::All) {
539     opts::Compilands = true;
540     opts::Symbols = true;
541     opts::Globals = true;
542     opts::Types = true;
543     opts::Externals = true;
544   }
545   if (opts::ExcludeCompilerGenerated) {
546     opts::ExcludeTypes.push_back("__vc_attributes");
547     opts::ExcludeCompilands.push_back("* Linker *");
548   }
549   if (opts::ExcludeSystemLibraries) {
550     opts::ExcludeCompilands.push_back(
551         "f:\\binaries\\Intermediate\\vctools\\crt_bld");
552   }
553
554 #if defined(HAVE_DIA_SDK)
555   CoInitializeEx(nullptr, COINIT_MULTITHREADED);
556 #endif
557
558   std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
559                 dumpInput);
560
561 #if defined(HAVE_DIA_SDK)
562   CoUninitialize();
563 #endif
564
565   return 0;
566 }