llvm-pdbdump: Add flags controlling the type of values to dump.
[oota-llvm.git] / lib / DebugInfo / PDB / PDBSymbolExe.cpp
1 //===- PDBSymbolExe.cpp - ---------------------------------------*- 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 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
11
12 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
13 #include "llvm/DebugInfo/PDB/PDBExtras.h"
14 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
15 #include "llvm/Support/ConvertUTF.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <utility>
19
20 #include <utility>
21
22 using namespace llvm;
23
24 #define SKIP_SYMBOL_IF_FLAG_UNSET(Tag, Flag)                                   \
25   case PDB_SymType::Tag:                                                       \
26     if ((Flags & Flag) == 0)                                                   \
27       continue;                                                                \
28     break;
29
30 PDBSymbolExe::PDBSymbolExe(const IPDBSession &PDBSession,
31                            std::unique_ptr<IPDBRawSymbol> Symbol)
32     : PDBSymbol(PDBSession, std::move(Symbol)) {}
33
34 void PDBSymbolExe::dump(raw_ostream &OS, int Indent,
35                         PDB_DumpLevel Level, PDB_DumpFlags Flags) const {
36   std::string FileName(getSymbolsFileName());
37
38   OS << stream_indent(Indent) << "Summary for " << FileName << "\n";
39
40   uint64_t FileSize = 0;
41   if (!llvm::sys::fs::file_size(FileName, FileSize))
42     OS << stream_indent(Indent + 2) << "Size: " << FileSize << " bytes\n";
43   else
44     OS << stream_indent(Indent + 2) << "Size: (Unable to obtain file size)\n";
45   PDB_UniqueId Guid = getGuid();
46   OS << stream_indent(Indent + 2) << "Guid: " << Guid << "\n";
47   OS << stream_indent(Indent + 2) << "Age: " << getAge() << "\n";
48   OS << stream_indent(Indent + 2) << "Attributes: ";
49   if (hasCTypes())
50     OS << "HasCTypes ";
51   if (hasPrivateSymbols())
52     OS << "HasPrivateSymbols ";
53   OS << "\n";
54
55   if (Flags & PDB_DF_Children) {
56     if (Flags & PDB_DF_Hidden) {
57       // For some reason, for each SymTag T, this dumps more items of type T
58       // than are dumped by calling dumpChildren(T).  In other words, there are
59       // "hidden" symbols.  For example, it causes functions to be dumped which
60       // have no address information, whereas specifically dumping only
61       // functions will not find those symbols.
62       //
63       // My suspicion is that in the underlying DIA call, when you call
64       // findChildren, passing a value of SymTagNone means all children
65       // recursively, whereas passing a concrete tag value means only immediate
66       // children of the global scope.  So perhaps we need to find these
67       // mysterious missing values by recursing through the hierarchy.
68       //
69       // On the other hand, there may just be some symbols that DIA tries to
70       // hide from you because it thinks you don't care about them.  However
71       // experimentation shows that even vtables, for example, can't be found
72       // without an exhaustive search.
73       auto ChildrenEnum = findAllChildren();
74       OS << stream_indent(Indent + 2) << ChildrenEnum->getChildCount()
75          << " symbols";
76
77       while (auto Child = ChildrenEnum->getNext()) {
78         switch (Child->getSymTag()) {
79           SKIP_SYMBOL_IF_FLAG_UNSET(Function, PDB_DF_Functions)
80           SKIP_SYMBOL_IF_FLAG_UNSET(Data, PDB_DF_Data)
81           SKIP_SYMBOL_IF_FLAG_UNSET(Label, PDB_DF_Labels)
82           SKIP_SYMBOL_IF_FLAG_UNSET(PublicSymbol, PDB_DF_PublicSyms)
83           SKIP_SYMBOL_IF_FLAG_UNSET(UDT, PDB_DF_Classes)
84           SKIP_SYMBOL_IF_FLAG_UNSET(Enum, PDB_DF_Enums)
85           SKIP_SYMBOL_IF_FLAG_UNSET(FunctionSig, PDB_DF_Funcsigs)
86           SKIP_SYMBOL_IF_FLAG_UNSET(VTable, PDB_DF_VTables)
87           SKIP_SYMBOL_IF_FLAG_UNSET(Thunk, PDB_DF_Thunks)
88           SKIP_SYMBOL_IF_FLAG_UNSET(Compiland, PDB_DF_ObjFiles)
89         default:
90           continue;
91         }
92         PDB_DumpLevel ChildLevel = (Level == PDB_DumpLevel::Detailed)
93                                        ? PDB_DumpLevel::Normal
94                                        : PDB_DumpLevel::Compact;
95         OS << "\n";
96         Child->dump(OS, Indent + 4, ChildLevel, PDB_DF_Children);
97       }
98     } else {
99       if (Flags & PDB_DF_ObjFiles)
100         dumpChildren(OS, "Compilands", PDB_SymType::Compiland, Indent + 4);
101       if (Flags & PDB_DF_Functions)
102         dumpChildren(OS, "Functions", PDB_SymType::Function, Indent + 4);
103       if (Flags & PDB_DF_Data)
104         dumpChildren(OS, "Data", PDB_SymType::Data, Indent + 4);
105       if (Flags & PDB_DF_Labels)
106         dumpChildren(OS, "Labels", PDB_SymType::Label, Indent + 4);
107       if (Flags & PDB_DF_PublicSyms)
108         dumpChildren(OS, "Public Symbols", PDB_SymType::PublicSymbol,
109                      Indent + 4);
110       if (Flags & PDB_DF_Classes)
111         dumpChildren(OS, "UDTs", PDB_SymType::UDT, Indent + 4);
112       if (Flags & PDB_DF_Enums)
113         dumpChildren(OS, "Enums", PDB_SymType::Enum, Indent + 4);
114       if (Flags & PDB_DF_Funcsigs)
115         dumpChildren(OS, "Function Signatures", PDB_SymType::FunctionSig,
116                      Indent + 4);
117       if (Flags & PDB_DF_Typedefs)
118         dumpChildren(OS, "Typedefs", PDB_SymType::Typedef, Indent + 4);
119       if (Flags & PDB_DF_VTables)
120         dumpChildren(OS, "VTables", PDB_SymType::VTable, Indent + 4);
121       if (Flags & PDB_DF_Thunks)
122         dumpChildren(OS, "Thunks", PDB_SymType::Thunk, Indent + 4);
123     }
124   }
125 }
126
127 void PDBSymbolExe::dumpChildren(raw_ostream &OS, StringRef Label,
128                                 PDB_SymType ChildType, int Indent) const {
129   auto ChildrenEnum = findAllChildren(ChildType);
130   OS << stream_indent(Indent) << Label << ": (" << ChildrenEnum->getChildCount()
131      << " items)\n";
132   while (auto Child = ChildrenEnum->getNext()) {
133     Child->dump(OS, Indent + 2, PDB_DumpLevel::Normal, PDB_DF_None);
134     OS << "\n";
135   }
136 }