blob: 49a7c8147fc71f3ebaf5ae4311816495594cae11 [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"
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000018#include "llvm/Config/config.h"
Zachary Turnera5549172015-02-10 22:43:25 +000019#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000020#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000021#include "llvm/DebugInfo/PDB/IPDBSession.h"
22#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000023#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000024#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/ConvertUTF.h"
27#include "llvm/Support/Format.h"
28#include "llvm/Support/ManagedStatic.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000029#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000030#include "llvm/Support/Process.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000031#include "llvm/Support/Signals.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000032#include "llvm/Support/raw_ostream.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000033
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000034#if defined(HAVE_DIA_SDK)
Zachary Turnera5549172015-02-10 22:43:25 +000035#include <Windows.h>
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000036#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000037
38using namespace llvm;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000039
40namespace opts {
41cl::list<std::string> InputFilenames(cl::Positional,
42 cl::desc("<input PDB files>"),
43 cl::OneOrMore);
44
Zachary Turner4287b942015-01-28 00:33:00 +000045cl::opt<bool> Compilands("compilands",
46 cl::desc("Display a list of compilands (e.g. object "
Zachary Turnera5549172015-02-10 22:43:25 +000047 "files) and symbols for each one."));
Zachary Turner4287b942015-01-28 00:33:00 +000048cl::alias CompilandsShort("c", cl::desc("Alias for --compilands"),
49 cl::aliasopt(Compilands));
Zachary Turner49693b42015-01-28 01:22:33 +000050}
51
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000052static void dumpInput(StringRef Path) {
Zachary Turnera5549172015-02-10 22:43:25 +000053 std::unique_ptr<IPDBSession> Session(
54 llvm::createPDBReader(PDB_ReaderType::DIA, Path));
55 if (!Session) {
56 outs() << "Unable to create PDB reader. Check that a valid implementation";
57 outs() << " is available for your platform.";
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000058 return;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000059 }
Zachary Turner7058dfc2015-01-27 22:40:14 +000060
Zachary Turnera5549172015-02-10 22:43:25 +000061 auto GlobalScope(Session->getGlobalScope());
62 GlobalScope->dump(outs(), 0, PDB_DumpLevel::Normal);
63 outs().flush();
Zachary Turner4287b942015-01-28 00:33:00 +000064
65 if (opts::Compilands) {
Zachary Turnerc074de02015-02-12 21:09:24 +000066 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
67 while (auto Compiland = Compilands->getNext()) {
Zachary Turnera952c492015-02-13 07:40:03 +000068 Compiland->dump(outs(), 0, PDB_DumpLevel::Detailed);
69 outs() << "\n";
Zachary Turnera5549172015-02-10 22:43:25 +000070 }
Zachary Turner7058dfc2015-01-27 22:40:14 +000071 }
Zachary Turnera5549172015-02-10 22:43:25 +000072 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000073}
74
75int main(int argc_, const char *argv_[]) {
76 // Print a stack trace if we signal out.
77 sys::PrintStackTraceOnErrorSignal();
78 PrettyStackTraceProgram X(argc_, argv_);
79
80 SmallVector<const char *, 256> argv;
81 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
82 std::error_code EC = llvm::sys::Process::GetArgumentVector(
83 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
84 if (EC) {
85 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
86 return 1;
87 }
88
89 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
90
91 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
92
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000093#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000094 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000095#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000096
97 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
98 dumpInput);
99
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000100#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000101 CoUninitialize();
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000102#endif
103
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000104 return 0;
105}