blob: 2c5109888bcc2bd5d7ab129f31a08e854b6a9cae [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 Turnerfcb14ad2015-01-27 20:46:21 +000016#include "llvm/ADT/ArrayRef.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000017#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"
26
Zachary Turner7058dfc2015-01-27 22:40:14 +000027#include "llvm-pdbdump.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000028#include "COMExtras.h"
Zachary Turner7058dfc2015-01-27 22:40:14 +000029#include "DIAExtras.h"
30#include "DIASymbol.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000031
32using namespace llvm;
33using namespace llvm::sys::windows;
34
35namespace opts {
36cl::list<std::string> InputFilenames(cl::Positional,
37 cl::desc("<input PDB files>"),
38 cl::OneOrMore);
39
40cl::opt<bool> Streams("streams", cl::desc("Display data stream information"));
Zachary Turner49693b42015-01-28 01:22:33 +000041cl::alias StreamsShort("x", cl::desc("Alias for --streams"),
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000042 cl::aliasopt(Streams));
43
44cl::opt<bool> StreamData("stream-data",
45 cl::desc("Dumps stream record data as bytes"));
Zachary Turner49693b42015-01-28 01:22:33 +000046cl::alias StreamDataShort("X", cl::desc("Alias for --stream-data"),
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000047 cl::aliasopt(StreamData));
Zachary Turner7058dfc2015-01-27 22:40:14 +000048
49cl::opt<bool> Tables("tables",
50 cl::desc("Display summary information for all of the "
51 "debug tables in the input file"));
52cl::alias TablesShort("t", cl::desc("Alias for --tables"),
53 cl::aliasopt(Tables));
Zachary Turner4287b942015-01-28 00:33:00 +000054
55cl::opt<bool> SourceFiles("source-files",
56 cl::desc("Display a list of the source files "
57 "contained in the PDB"));
58cl::alias SourceFilesShort("f", cl::desc("Alias for --source-files"),
59 cl::aliasopt(SourceFiles));
60
61cl::opt<bool> Compilands("compilands",
62 cl::desc("Display a list of compilands (e.g. object "
63 "files) and their source file composition"));
64cl::alias CompilandsShort("c", cl::desc("Alias for --compilands"),
65 cl::aliasopt(Compilands));
Zachary Turner49693b42015-01-28 01:22:33 +000066
67cl::opt<bool> Symbols("symbols", cl::desc("Display symbols"));
68cl::alias SymbolsShort("s", cl::desc("Alias for --symbols"),
69 cl::aliasopt(Symbols));
70
71cl::opt<bool> SymbolDetails("symbol-details",
72 cl::desc("Display symbol details"));
73cl::alias SymbolDetailsShort("S", cl::desc("Alias for --symbol-details"),
74 cl::aliasopt(SymbolDetails));
Zachary Turner4287b942015-01-28 00:33:00 +000075}
76
77template <typename TableType>
Zachary Turner49693b42015-01-28 01:22:33 +000078static HRESULT getDIATable(IDiaSession *Session, TableType **Table) {
Zachary Turner4287b942015-01-28 00:33:00 +000079 CComPtr<IDiaEnumTables> EnumTables = nullptr;
80 HRESULT Error = S_OK;
81 if (FAILED(Error = Session->getEnumTables(&EnumTables)))
82 return Error;
83
84 for (auto CurTable : make_com_enumerator(EnumTables)) {
85 TableType *ResultTable = nullptr;
86 if (FAILED(CurTable->QueryInterface(
87 __uuidof(TableType), reinterpret_cast<void **>(&ResultTable))))
88 continue;
89
90 *Table = ResultTable;
91 return S_OK;
92 }
93 return E_FAIL;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000094}
95
Zachary Turner7058dfc2015-01-27 22:40:14 +000096static void dumpBasicFileInfo(StringRef Path, IDiaSession *Session) {
97 CComPtr<IDiaSymbol> GlobalScope;
98 HRESULT hr = Session->get_globalScope(&GlobalScope);
99 DIASymbol GlobalScopeSymbol(GlobalScope);
100 if (S_OK == hr)
101 GlobalScopeSymbol.getSymbolsFileName().dump("File", 0);
102 else
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) {
109 outs().indent(2);
110 if (::GetFileSizeEx(FileHandle, &FileSize))
111 outs() << "Size: " << FileSize.QuadPart << " bytes\n";
112 else
113 outs() << "Size: (Unable to obtain file size)\n";
114 FILETIME ModifiedTime;
115 outs().indent(2);
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";
123 } else {
124 outs() << "Timestamp: (Unable to obtain time stamp)\n";
125 }
126 ::CloseHandle(FileHandle);
127 }
128
129 if (S_OK == hr)
130 GlobalScopeSymbol.fullDump(2);
131 outs() << "\n";
132 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000133}
134
Zachary Turner7058dfc2015-01-27 22:40:14 +0000135static void dumpDataStreams(IDiaSession *Session) {
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000136 CComPtr<IDiaEnumDebugStreams> DebugStreams = nullptr;
Zachary Turner7058dfc2015-01-27 22:40:14 +0000137 if (FAILED(Session->getEnumDebugStreams(&DebugStreams)))
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000138 return;
139
140 LONG Count = 0;
141 if (FAILED(DebugStreams->get_Count(&Count)))
142 return;
143 outs() << "Data Streams [count=" << Count << "]\n";
144
145 std::string Name8;
146
147 for (auto Stream : make_com_enumerator(DebugStreams)) {
148 BSTR Name16;
149 if (FAILED(Stream->get_name(&Name16)))
150 continue;
151 if (BSTRToUTF8(Name16, Name8))
152 outs() << " " << Name8;
153 ::SysFreeString(Name16);
154 if (FAILED(Stream->get_Count(&Count))) {
155 outs() << "\n";
156 continue;
157 }
158
159 outs() << " [" << Count << " records]\n";
160 if (opts::StreamData) {
161 int RecordIndex = 0;
162 for (auto StreamRecord : make_com_data_record_enumerator(Stream)) {
163 outs() << " Record " << RecordIndex << " [" << StreamRecord.size()
164 << " bytes]";
165 for (uint8_t byte : StreamRecord) {
166 outs() << " " << llvm::format_hex_no_prefix(byte, 2, true);
167 }
168 outs() << "\n";
169 ++RecordIndex;
170 }
171 }
172 }
Zachary Turner7058dfc2015-01-27 22:40:14 +0000173 outs() << "\n";
174 outs().flush();
175}
176
177static void dumpDebugTables(IDiaSession *Session) {
178 CComPtr<IDiaEnumTables> EnumTables = nullptr;
179 if (SUCCEEDED(Session->getEnumTables(&EnumTables))) {
180 LONG Count = 0;
181 if (FAILED(EnumTables->get_Count(&Count)))
182 return;
183
184 outs() << "Debug Tables [count=" << Count << "]\n";
185
186 std::string Name8;
187 for (auto Table : make_com_enumerator(EnumTables)) {
188 BSTR Name16;
189 if (FAILED(Table->get_name(&Name16)))
190 continue;
191 if (BSTRToUTF8(Name16, Name8))
192 outs() << " " << Name8;
193 ::SysFreeString(Name16);
194 if (SUCCEEDED(Table->get_Count(&Count))) {
195 outs() << " [" << Count << " items]\n";
196 } else
197 outs() << "\n";
198 }
199 }
200 outs() << "\n";
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000201 outs().flush();
202}
203
Zachary Turner4287b942015-01-28 00:33:00 +0000204static void dumpSourceFiles(IDiaSession *Session) {
205 CComPtr<IDiaEnumSourceFiles> EnumSourceFileList;
Zachary Turner49693b42015-01-28 01:22:33 +0000206 if (FAILED(getDIATable(Session, &EnumSourceFileList)))
Zachary Turner4287b942015-01-28 00:33:00 +0000207 return;
208
209 LONG SourceFileCount = 0;
210 EnumSourceFileList->get_Count(&SourceFileCount);
211
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))
216 continue;
217 outs().indent(2);
218 std::string SourceFileName8;
219 BSTRToUTF8(SourceFileName, SourceFileName8);
220 outs() << SourceFileName8 << "\n";
221 }
222 outs() << "\n";
223 outs().flush();
224}
225
226static void dumpCompilands(IDiaSession *Session) {
227 CComPtr<IDiaEnumSourceFiles> EnumSourceFileList;
Zachary Turner49693b42015-01-28 01:22:33 +0000228 if (FAILED(getDIATable(Session, &EnumSourceFileList)))
Zachary Turner4287b942015-01-28 00:33:00 +0000229 return;
230
231 LONG SourceFileCount = 0;
232 EnumSourceFileList->get_Count(&SourceFileCount);
233
234 CComPtr<IDiaSymbol> GlobalScope;
235 HRESULT hr = Session->get_globalScope(&GlobalScope);
236 DIASymbol GlobalScopeSymbol(GlobalScope);
237 if (S_OK != hr)
238 return;
239
240 CComPtr<IDiaEnumSymbols> EnumCompilands;
241 if (S_OK !=
242 GlobalScope->findChildren(SymTagCompiland, nullptr, nsNone,
243 &EnumCompilands))
244 return;
245
246 LONG CompilandCount = 0;
247 EnumCompilands->get_Count(&CompilandCount);
248 outs() << "Dumping compilands [" << CompilandCount
249 << " compilands containing " << SourceFileCount << " source files]\n";
250
251 for (auto Compiland : make_com_enumerator(EnumCompilands)) {
252 DIASymbol CompilandSymbol(Compiland);
253 outs().indent(2);
254 outs() << CompilandSymbol.getName().value() << "\n";
255
256 CComPtr<IDiaEnumSourceFiles> EnumFiles;
257 if (S_OK != Session->findFile(Compiland, nullptr, nsNone, &EnumFiles))
258 continue;
259
260 for (auto SourceFile : make_com_enumerator(EnumFiles)) {
261 DWORD ChecksumType = 0;
262 DWORD ChecksumSize = 0;
263 std::vector<uint8_t> Checksum;
264 outs().indent(4);
265 SourceFile->get_checksumType(&ChecksumType);
266 if (S_OK == SourceFile->get_checksum(0, &ChecksumSize, nullptr)) {
267 Checksum.resize(ChecksumSize);
268 if (S_OK ==
269 SourceFile->get_checksum(ChecksumSize, &ChecksumSize,
270 &Checksum[0])) {
271 outs() << "[" << ((ChecksumType == HashMD5) ? "MD5 " : "SHA-1")
272 << ": ";
273 for (auto byte : Checksum)
274 outs() << format_hex_no_prefix(byte, 2, true);
275 outs() << "] ";
276 }
277 }
278 CComBSTR SourceFileName;
279 if (S_OK != SourceFile->get_fileName(&SourceFileName))
280 continue;
281
282 std::string SourceFileName8;
283 BSTRToUTF8(SourceFileName, SourceFileName8);
284 outs() << SourceFileName8 << "\n";
285 }
286 }
287
288 outs() << "\n";
289 outs().flush();
290}
291
Zachary Turner49693b42015-01-28 01:22:33 +0000292static void dumpSymbols(IDiaSession *Session) {
293 CComPtr<IDiaEnumSymbols> EnumSymbols;
294 if (FAILED(getDIATable(Session, &EnumSymbols)))
295 return;
296
297 LONG SymbolCount = 0;
298 EnumSymbols->get_Count(&SymbolCount);
299
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";
308 } else {
309 outs() << " " << SymbolSymbol.getName().value() << "\n";
310 }
311 if (opts::SymbolDetails)
312 SymbolSymbol.fullDump(4);
313 }
314 outs() << "(Found " << UnnamedSymbolCount << " unnamed symbols)\n";
315 outs().flush();
316}
317
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000318static 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;
323 HRESULT hr =
324 ::CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER,
325 __uuidof(IDiaDataSource), (void **)&source);
326 if (FAILED(hr))
327 return;
328 if (FAILED(source->loadDataFromPdb(Path16)))
329 return;
Zachary Turner4287b942015-01-28 00:33:00 +0000330 CComPtr<IDiaSession> Session;
331 if (FAILED(source->openSession(&Session)))
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000332 return;
Zachary Turner7058dfc2015-01-27 22:40:14 +0000333
Zachary Turner4287b942015-01-28 00:33:00 +0000334 dumpBasicFileInfo(Path, Session);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000335 if (opts::Streams || opts::StreamData) {
Zachary Turner4287b942015-01-28 00:33:00 +0000336 dumpDataStreams(Session);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000337 }
Zachary Turner7058dfc2015-01-27 22:40:14 +0000338
339 if (opts::Tables) {
Zachary Turner4287b942015-01-28 00:33:00 +0000340 dumpDebugTables(Session);
341 }
342
343 if (opts::SourceFiles) {
344 dumpSourceFiles(Session);
345 }
346
347 if (opts::Compilands) {
348 dumpCompilands(Session);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000349 }
Zachary Turner49693b42015-01-28 01:22:33 +0000350
351 if (opts::Symbols || opts::SymbolDetails) {
352 dumpSymbols(Session);
353 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000354}
355
356int main(int argc_, const char *argv_[]) {
357 // Print a stack trace if we signal out.
358 sys::PrintStackTraceOnErrorSignal();
359 PrettyStackTraceProgram X(argc_, argv_);
360
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);
365 if (EC) {
366 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
367 return 1;
368 }
369
370 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
371
372 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
373
374 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
375
376 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
377 dumpInput);
378
379 CoUninitialize();
380 return 0;
381}