summaryrefslogtreecommitdiffstats
path: root/llvm/tools/llvm-pdbdump/llvm-pdbdump.cpp
blob: 89f885b574ec16dda58c9bec1da5aeaa0cb9f0e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//===- llvm-pdbdump.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Dumps debug information present in PDB files.  This utility makes use of
// the Microsoft Windows SDK, so will not compile or run on non-Windows
// platforms.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"

#include "llvm-pdbdump.h"
#include "COMExtras.h"
#include "DIAExtras.h"
#include "DIASymbol.h"

using namespace llvm;
using namespace llvm::sys::windows;

namespace opts {
cl::list<std::string> InputFilenames(cl::Positional,
                                     cl::desc("<input PDB files>"),
                                     cl::OneOrMore);

cl::opt<bool> Streams("streams", cl::desc("Display data stream information"));
cl::alias StreamsShort("s", cl::desc("Alias for --streams"),
                       cl::aliasopt(Streams));

cl::opt<bool> StreamData("stream-data",
                         cl::desc("Dumps stream record data as bytes"));
cl::alias StreamDataShort("S", cl::desc("Alias for --stream-data"),
                          cl::aliasopt(StreamData));

cl::opt<bool> Tables("tables",
                     cl::desc("Display summary information for all of the "
                              "debug tables in the input file"));
cl::alias TablesShort("t", cl::desc("Alias for --tables"),
                      cl::aliasopt(Tables));

cl::opt<bool> SourceFiles("source-files",
                          cl::desc("Display a list of the source files "
                                   "contained in the PDB"));
cl::alias SourceFilesShort("f", cl::desc("Alias for --source-files"),
                           cl::aliasopt(SourceFiles));

cl::opt<bool> Compilands("compilands",
                         cl::desc("Display a list of compilands (e.g. object "
                                  "files) and their source file composition"));
cl::alias CompilandsShort("c", cl::desc("Alias for --compilands"),
                          cl::aliasopt(Compilands));
}

template <typename TableType>
static HRESULT GetDiaTable(IDiaSession *Session, TableType **Table) {
  CComPtr<IDiaEnumTables> EnumTables = nullptr;
  HRESULT Error = S_OK;
  if (FAILED(Error = Session->getEnumTables(&EnumTables)))
    return Error;

  for (auto CurTable : make_com_enumerator(EnumTables)) {
    TableType *ResultTable = nullptr;
    if (FAILED(CurTable->QueryInterface(
            __uuidof(TableType), reinterpret_cast<void **>(&ResultTable))))
      continue;

    *Table = ResultTable;
    return S_OK;
  }
  return E_FAIL;
}

static void dumpBasicFileInfo(StringRef Path, IDiaSession *Session) {
  CComPtr<IDiaSymbol> GlobalScope;
  HRESULT hr = Session->get_globalScope(&GlobalScope);
  DIASymbol GlobalScopeSymbol(GlobalScope);
  if (S_OK == hr)
    GlobalScopeSymbol.getSymbolsFileName().dump("File", 0);
  else
    outs() << "File: " << Path << "\n";
  HANDLE FileHandle = ::CreateFile(
      Path.data(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
  LARGE_INTEGER FileSize;
  if (INVALID_HANDLE_VALUE != FileHandle) {
    outs().indent(2);
    if (::GetFileSizeEx(FileHandle, &FileSize))
      outs() << "Size: " << FileSize.QuadPart << " bytes\n";
    else
      outs() << "Size: (Unable to obtain file size)\n";
    FILETIME ModifiedTime;
    outs().indent(2);
    if (::GetFileTime(FileHandle, nullptr, nullptr, &ModifiedTime)) {
      ULARGE_INTEGER TimeInteger;
      TimeInteger.LowPart = ModifiedTime.dwLowDateTime;
      TimeInteger.HighPart = ModifiedTime.dwHighDateTime;
      llvm::sys::TimeValue Time;
      Time.fromWin32Time(TimeInteger.QuadPart);
      outs() << "Timestamp: " << Time.str() << "\n";
    } else {
      outs() << "Timestamp: (Unable to obtain time stamp)\n";
    }
    ::CloseHandle(FileHandle);
  }

  if (S_OK == hr)
    GlobalScopeSymbol.fullDump(2);
  outs() << "\n";
  outs().flush();
}

static void dumpDataStreams(IDiaSession *Session) {
  CComPtr<IDiaEnumDebugStreams> DebugStreams = nullptr;
  if (FAILED(Session->getEnumDebugStreams(&DebugStreams)))
    return;

  LONG Count = 0;
  if (FAILED(DebugStreams->get_Count(&Count)))
    return;
  outs() << "Data Streams [count=" << Count << "]\n";

  std::string Name8;

  for (auto Stream : make_com_enumerator(DebugStreams)) {
    BSTR Name16;
    if (FAILED(Stream->get_name(&Name16)))
      continue;
    if (BSTRToUTF8(Name16, Name8))
      outs() << "  " << Name8;
    ::SysFreeString(Name16);
    if (FAILED(Stream->get_Count(&Count))) {
      outs() << "\n";
      continue;
    }

    outs() << " [" << Count << " records]\n";
    if (opts::StreamData) {
      int RecordIndex = 0;
      for (auto StreamRecord : make_com_data_record_enumerator(Stream)) {
        outs() << "    Record " << RecordIndex << " [" << StreamRecord.size()
               << " bytes]";
        for (uint8_t byte : StreamRecord) {
          outs() << " " << llvm::format_hex_no_prefix(byte, 2, true);
        }
        outs() << "\n";
        ++RecordIndex;
      }
    }
  }
  outs() << "\n";
  outs().flush();
}

static void dumpDebugTables(IDiaSession *Session) {
  CComPtr<IDiaEnumTables> EnumTables = nullptr;
  if (SUCCEEDED(Session->getEnumTables(&EnumTables))) {
    LONG Count = 0;
    if (FAILED(EnumTables->get_Count(&Count)))
      return;

    outs() << "Debug Tables [count=" << Count << "]\n";

    std::string Name8;
    for (auto Table : make_com_enumerator(EnumTables)) {
      BSTR Name16;
      if (FAILED(Table->get_name(&Name16)))
        continue;
      if (BSTRToUTF8(Name16, Name8))
        outs() << "  " << Name8;
      ::SysFreeString(Name16);
      if (SUCCEEDED(Table->get_Count(&Count))) {
        outs() << " [" << Count << " items]\n";
      } else
        outs() << "\n";
    }
  }
  outs() << "\n";
  outs().flush();
}

static void dumpSourceFiles(IDiaSession *Session) {
  CComPtr<IDiaEnumSourceFiles> EnumSourceFileList;
  if (FAILED(GetDiaTable(Session, &EnumSourceFileList)))
    return;

  LONG SourceFileCount = 0;
  EnumSourceFileList->get_Count(&SourceFileCount);

  outs() << "Dumping source files [" << SourceFileCount << " files]\n";
  for (auto SourceFile : make_com_enumerator(EnumSourceFileList)) {
    CComBSTR SourceFileName;
    if (S_OK != SourceFile->get_fileName(&SourceFileName))
      continue;
    outs().indent(2);
    std::string SourceFileName8;
    BSTRToUTF8(SourceFileName, SourceFileName8);
    outs() << SourceFileName8 << "\n";
  }
  outs() << "\n";
  outs().flush();
}

static void dumpCompilands(IDiaSession *Session) {
  CComPtr<IDiaEnumSourceFiles> EnumSourceFileList;
  if (FAILED(GetDiaTable(Session, &EnumSourceFileList)))
    return;

  LONG SourceFileCount = 0;
  EnumSourceFileList->get_Count(&SourceFileCount);

  CComPtr<IDiaSymbol> GlobalScope;
  HRESULT hr = Session->get_globalScope(&GlobalScope);
  DIASymbol GlobalScopeSymbol(GlobalScope);
  if (S_OK != hr)
    return;

  CComPtr<IDiaEnumSymbols> EnumCompilands;
  if (S_OK !=
      GlobalScope->findChildren(SymTagCompiland, nullptr, nsNone,
                                &EnumCompilands))
    return;

  LONG CompilandCount = 0;
  EnumCompilands->get_Count(&CompilandCount);
  outs() << "Dumping compilands [" << CompilandCount
         << " compilands containing " << SourceFileCount << " source files]\n";

  for (auto Compiland : make_com_enumerator(EnumCompilands)) {
    DIASymbol CompilandSymbol(Compiland);
    outs().indent(2);
    outs() << CompilandSymbol.getName().value() << "\n";

    CComPtr<IDiaEnumSourceFiles> EnumFiles;
    if (S_OK != Session->findFile(Compiland, nullptr, nsNone, &EnumFiles))
      continue;

    for (auto SourceFile : make_com_enumerator(EnumFiles)) {
      DWORD ChecksumType = 0;
      DWORD ChecksumSize = 0;
      std::vector<uint8_t> Checksum;
      outs().indent(4);
      SourceFile->get_checksumType(&ChecksumType);
      if (S_OK == SourceFile->get_checksum(0, &ChecksumSize, nullptr)) {
        Checksum.resize(ChecksumSize);
        if (S_OK ==
            SourceFile->get_checksum(ChecksumSize, &ChecksumSize,
                                     &Checksum[0])) {
          outs() << "[" << ((ChecksumType == HashMD5) ? "MD5  " : "SHA-1")
                 << ": ";
          for (auto byte : Checksum)
            outs() << format_hex_no_prefix(byte, 2, true);
          outs() << "] ";
        }
      }
      CComBSTR SourceFileName;
      if (S_OK != SourceFile->get_fileName(&SourceFileName))
        continue;

      std::string SourceFileName8;
      BSTRToUTF8(SourceFileName, SourceFileName8);
      outs() << SourceFileName8 << "\n";
    }
  }

  outs() << "\n";
  outs().flush();
}

static void dumpInput(StringRef Path) {
  SmallVector<UTF16, 128> Path16String;
  llvm::convertUTF8ToUTF16String(Path, Path16String);
  wchar_t *Path16 = reinterpret_cast<wchar_t *>(Path16String.data());
  CComPtr<IDiaDataSource> source;
  HRESULT hr =
      ::CoCreateInstance(CLSID_DiaSource, nullptr, CLSCTX_INPROC_SERVER,
                         __uuidof(IDiaDataSource), (void **)&source);
  if (FAILED(hr))
    return;
  if (FAILED(source->loadDataFromPdb(Path16)))
    return;
  CComPtr<IDiaSession> Session;
  if (FAILED(source->openSession(&Session)))
    return;

  dumpBasicFileInfo(Path, Session);
  if (opts::Streams || opts::StreamData) {
    dumpDataStreams(Session);
  }

  if (opts::Tables) {
    dumpDebugTables(Session);
  }

  if (opts::SourceFiles) {
    dumpSourceFiles(Session);
  }

  if (opts::Compilands) {
    dumpCompilands(Session);
  }
}

int main(int argc_, const char *argv_[]) {
  // Print a stack trace if we signal out.
  sys::PrintStackTraceOnErrorSignal();
  PrettyStackTraceProgram X(argc_, argv_);

  SmallVector<const char *, 256> argv;
  llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
  std::error_code EC = llvm::sys::Process::GetArgumentVector(
      argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
  if (EC) {
    llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
    return 1;
  }

  llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.

  cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");

  CoInitializeEx(nullptr, COINIT_MULTITHREADED);

  std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
                dumpInput);

  CoUninitialize();
  return 0;
}
OpenPOWER on IntegriCloud