1 //===- llvm-pdbdump.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/ConvertUTF.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/ManagedStatic.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/Process.h"
24 #include "llvm/Support/PrettyStackTrace.h"
25 #include "llvm/Support/Signals.h"
27 #include "llvm-pdbdump.h"
28 #include "COMExtras.h"
29 #include "DIAExtras.h"
30 #include "DIASymbol.h"
33 using namespace llvm::sys::windows;
36 cl::list<std::string> InputFilenames(cl::Positional,
37 cl::desc("<input PDB files>"),
40 cl::opt<bool> Streams("streams", cl::desc("Display data stream information"));
41 cl::alias StreamsShort("x", cl::desc("Alias for --streams"),
42 cl::aliasopt(Streams));
44 cl::opt<bool> StreamData("stream-data",
45 cl::desc("Dumps stream record data as bytes"));
46 cl::alias StreamDataShort("X", cl::desc("Alias for --stream-data"),
47 cl::aliasopt(StreamData));
49 cl::opt<bool> Tables("tables",
50 cl::desc("Display summary information for all of the "
51 "debug tables in the input file"));
52 cl::alias TablesShort("t", cl::desc("Alias for --tables"),
53 cl::aliasopt(Tables));
55 cl::opt<bool> SourceFiles("source-files",
56 cl::desc("Display a list of the source files "
57 "contained in the PDB"));
58 cl::alias SourceFilesShort("f", cl::desc("Alias for --source-files"),
59 cl::aliasopt(SourceFiles));
61 cl::opt<bool> Compilands("compilands",
62 cl::desc("Display a list of compilands (e.g. object "
63 "files) and their source file composition"));
64 cl::alias CompilandsShort("c", cl::desc("Alias for --compilands"),
65 cl::aliasopt(Compilands));
67 cl::opt<bool> Symbols("symbols", cl::desc("Display symbols"));
68 cl::alias SymbolsShort("s", cl::desc("Alias for --symbols"),
69 cl::aliasopt(Symbols));
71 cl::opt<bool> SymbolDetails("symbol-details",
72 cl::desc("Display symbol details"));
73 cl::alias SymbolDetailsShort("S", cl::desc("Alias for --symbol-details"),
74 cl::aliasopt(SymbolDetails));
77 template <typename TableType>
78 static HRESULT getDIATable(IDiaSession *Session, TableType **Table) {
79 CComPtr<IDiaEnumTables> EnumTables = nullptr;
81 if (FAILED(Error = Session->getEnumTables(&EnumTables)))
84 for (auto CurTable : make_com_enumerator(EnumTables)) {
85 TableType *ResultTable = nullptr;
86 if (FAILED(CurTable->QueryInterface(
87 __uuidof(TableType), reinterpret_cast<void **>(&ResultTable))))
96 static void dumpBasicFileInfo(StringRef Path, IDiaSession *Session) {
97 CComPtr<IDiaSymbol> GlobalScope;
98 HRESULT hr = Session->get_globalScope(&GlobalScope);
99 DIASymbol GlobalScopeSymbol(GlobalScope);
101 GlobalScopeSymbol.getSymbolsFileName().dump("File", 0);
103 outs() << "File: " << Path << "\n";
104 HANDLE FileHandle = ::CreateFile(
105 Path.data(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
106 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
107 LARGE_INTEGER FileSize;
108 if (INVALID_HANDLE_VALUE != FileHandle) {
110 if (::GetFileSizeEx(FileHandle, &FileSize))
111 outs() << "Size: " << FileSize.QuadPart << " bytes\n";
113 outs() << "Size: (Unable to obtain file size)\n";
114 FILETIME ModifiedTime;
116 if (::GetFileTime(FileHandle, nullptr, nullptr, &ModifiedTime)) {
117 ULARGE_INTEGER TimeInteger;
118 TimeInteger.LowPart = ModifiedTime.dwLowDateTime;
119 TimeInteger.HighPart = ModifiedTime.dwHighDateTime;
120 llvm::sys::TimeValue Time;
121 Time.fromWin32Time(TimeInteger.QuadPart);
122 outs() << "Timestamp: " << Time.str() << "\n";
124 outs() << "Timestamp: (Unable to obtain time stamp)\n";
126 ::CloseHandle(FileHandle);
130 GlobalScopeSymbol.fullDump(2);
135 static void dumpDataStreams(IDiaSession *Session) {
136 CComPtr<IDiaEnumDebugStreams> DebugStreams = nullptr;
137 if (FAILED(Session->getEnumDebugStreams(&DebugStreams)))
141 if (FAILED(DebugStreams->get_Count(&Count)))
143 outs() << "Data Streams [count=" << Count << "]\n";
147 for (auto Stream : make_com_enumerator(DebugStreams)) {
149 if (FAILED(Stream->get_name(&Name16)))
151 if (BSTRToUTF8(Name16, Name8))
152 outs() << " " << Name8;
153 ::SysFreeString(Name16);
154 if (FAILED(Stream->get_Count(&Count))) {
159 outs() << " [" << Count << " records]\n";
160 if (opts::StreamData) {
162 for (auto StreamRecord : make_com_data_record_enumerator(Stream)) {
163 outs() << " Record " << RecordIndex << " [" << StreamRecord.size()
165 for (uint8_t byte : StreamRecord) {
166 outs() << " " << llvm::format_hex_no_prefix(byte, 2, true);
177 static void dumpDebugTables(IDiaSession *Session) {
178 CComPtr<IDiaEnumTables> EnumTables = nullptr;
179 if (SUCCEEDED(Session->getEnumTables(&EnumTables))) {
181 if (FAILED(EnumTables->get_Count(&Count)))
184 outs() << "Debug Tables [count=" << Count << "]\n";
187 for (auto Table : make_com_enumerator(EnumTables)) {
189 if (FAILED(Table->get_name(&Name16)))
191 if (BSTRToUTF8(Name16, Name8))
192 outs() << " " << Name8;
193 ::SysFreeString(Name16);
194 if (SUCCEEDED(Table->get_Count(&Count))) {
195 outs() << " [" << Count << " items]\n";
204 static void dumpSourceFiles(IDiaSession *Session) {
205 CComPtr<IDiaEnumSourceFiles> EnumSourceFileList;
206 if (FAILED(getDIATable(Session, &EnumSourceFileList)))
209 LONG SourceFileCount = 0;
210 EnumSourceFileList->get_Count(&SourceFileCount);
212 outs() << "Dumping source files [" << SourceFileCount << " files]\n";
213 for (auto SourceFile : make_com_enumerator(EnumSourceFileList)) {
214 CComBSTR SourceFileName;
215 if (S_OK != SourceFile->get_fileName(&SourceFileName))
218 std::string SourceFileName8;
219 BSTRToUTF8(SourceFileName, SourceFileName8);
220 outs() << SourceFileName8 << "\n";
226 static void dumpCompilands(IDiaSession *Session) {
227 CComPtr<IDiaEnumSourceFiles> EnumSourceFileList;
228 if (FAILED(getDIATable(Session, &EnumSourceFileList)))
231 LONG SourceFileCount = 0;
232 EnumSourceFileList->get_Count(&SourceFileCount);
234 CComPtr<IDiaSymbol> GlobalScope;
235 HRESULT hr = Session->get_globalScope(&GlobalScope);
236 DIASymbol GlobalScopeSymbol(GlobalScope);
240 CComPtr<IDiaEnumSymbols> EnumCompilands;
242 GlobalScope->findChildren(SymTagCompiland, nullptr, nsNone,
246 LONG CompilandCount = 0;
247 EnumCompilands->get_Count(&CompilandCount);
248 outs() << "Dumping compilands [" << CompilandCount
249 << " compilands containing " << SourceFileCount << " source files]\n";
251 for (auto Compiland : make_com_enumerator(EnumCompilands)) {
252 DIASymbol CompilandSymbol(Compiland);
254 outs() << CompilandSymbol.getName().value() << "\n";
256 CComPtr<IDiaEnumSourceFiles> EnumFiles;
257 if (S_OK != Session->findFile(Compiland, nullptr, nsNone, &EnumFiles))
260 for (auto SourceFile : make_com_enumerator(EnumFiles)) {
261 DWORD ChecksumType = 0;
262 DWORD ChecksumSize = 0;
263 std::vector<uint8_t> Checksum;
265 SourceFile->get_checksumType(&ChecksumType);
266 if (S_OK == SourceFile->get_checksum(0, &ChecksumSize, nullptr)) {
267 Checksum.resize(ChecksumSize);
269 SourceFile->get_checksum(ChecksumSize, &ChecksumSize,
271 outs() << "[" << ((ChecksumType == HashMD5) ? "MD5 " : "SHA-1")
273 for (auto byte : Checksum)
274 outs() << format_hex_no_prefix(byte, 2, true);
278 CComBSTR SourceFileName;
279 if (S_OK != SourceFile->get_fileName(&SourceFileName))
282 std::string SourceFileName8;
283 BSTRToUTF8(SourceFileName, SourceFileName8);
284 outs() << SourceFileName8 << "\n";
292 static void dumpSymbols(IDiaSession *Session) {
293 CComPtr<IDiaEnumSymbols> EnumSymbols;
294 if (FAILED(getDIATable(Session, &EnumSymbols)))
297 LONG SymbolCount = 0;
298 EnumSymbols->get_Count(&SymbolCount);
300 outs() << "Dumping symbols [" << SymbolCount << " symbols]\n";
301 int UnnamedSymbolCount = 0;
302 for (auto Symbol : make_com_enumerator(EnumSymbols)) {
303 DIASymbol SymbolSymbol(Symbol);
304 DIAResult<DIAString> SymbolName = SymbolSymbol.getName();
305 if (!SymbolName.hasValue() || SymbolName.value().empty()) {
306 ++UnnamedSymbolCount;
307 outs() << " (Unnamed symbol)\n";
309 outs() << " " << SymbolSymbol.getName().value() << "\n";
311 if (opts::SymbolDetails)
312 SymbolSymbol.fullDump(4);
314 outs() << "(Found " << UnnamedSymbolCount << " unnamed symbols)\n";
318 static void dumpInput(StringRef Path) {
319 SmallVector<UTF16, 128> Path16String;
320 llvm::convertUTF8ToUTF16String(Path, Path16String);
321 wchar_t *Path16 = reinterpret_cast<wchar_t *>(Path16String.data());
322 CComPtr<IDiaDataSource> source;
324 ::CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER,
325 __uuidof(IDiaDataSource), (void **)&source);
328 if (FAILED(source->loadDataFromPdb(Path16)))
330 CComPtr<IDiaSession> Session;
331 if (FAILED(source->openSession(&Session)))
334 dumpBasicFileInfo(Path, Session);
335 if (opts::Streams || opts::StreamData) {
336 dumpDataStreams(Session);
340 dumpDebugTables(Session);
343 if (opts::SourceFiles) {
344 dumpSourceFiles(Session);
347 if (opts::Compilands) {
348 dumpCompilands(Session);
351 if (opts::Symbols || opts::SymbolDetails) {
352 dumpSymbols(Session);
356 int main(int argc_, const char *argv_[]) {
357 // Print a stack trace if we signal out.
358 sys::PrintStackTraceOnErrorSignal();
359 PrettyStackTraceProgram X(argc_, argv_);
361 SmallVector<const char *, 256> argv;
362 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
363 std::error_code EC = llvm::sys::Process::GetArgumentVector(
364 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
366 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
370 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
372 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
374 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
376 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),