1283625Sdim//===- CompilandDumper.cpp - llvm-pdbdump compiland symbol dumper *- C++ *-===//
2283625Sdim//
3283625Sdim//                     The LLVM Compiler Infrastructure
4283625Sdim//
5283625Sdim// This file is distributed under the University of Illinois Open Source
6283625Sdim// License. See LICENSE.TXT for details.
7283625Sdim//
8283625Sdim//===----------------------------------------------------------------------===//
9283625Sdim
10283625Sdim#include "CompilandDumper.h"
11283625Sdim#include "LinePrinter.h"
12283625Sdim#include "llvm-pdbdump.h"
13283625Sdim
14283625Sdim#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
15283625Sdim#include "llvm/DebugInfo/PDB/IPDBSession.h"
16283625Sdim#include "llvm/DebugInfo/PDB/PDBExtras.h"
17283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbol.h"
18283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
19283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
20283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
21283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
22283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
23283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbolLabel.h"
24283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
25283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
26283625Sdim#include "llvm/DebugInfo/PDB/PDBSymbolUnknown.h"
27283625Sdim#include "llvm/Support/Format.h"
28283625Sdim#include "llvm/Support/Path.h"
29283625Sdim#include "llvm/Support/raw_ostream.h"
30283625Sdim
31283625Sdim#include "FunctionDumper.h"
32283625Sdim
33283625Sdim#include <utility>
34283625Sdim#include <vector>
35283625Sdim
36283625Sdimusing namespace llvm;
37283625Sdim
38283625SdimCompilandDumper::CompilandDumper(LinePrinter &P)
39283625Sdim    : PDBSymDumper(true), Printer(P) {}
40283625Sdim
41283625Sdimvoid CompilandDumper::dump(const PDBSymbolCompilandDetails &Symbol) {}
42283625Sdim
43283625Sdimvoid CompilandDumper::dump(const PDBSymbolCompilandEnv &Symbol) {}
44283625Sdim
45283625Sdimvoid CompilandDumper::start(const PDBSymbolCompiland &Symbol, bool Children) {
46283625Sdim  std::string FullName = Symbol.getName();
47283625Sdim  if (Printer.IsCompilandExcluded(FullName))
48283625Sdim    return;
49283625Sdim
50283625Sdim  Printer.NewLine();
51283625Sdim  WithColor(Printer, PDB_ColorItem::Path).get() << FullName;
52283625Sdim  if (!Children)
53283625Sdim    return;
54283625Sdim
55283625Sdim  auto ChildrenEnum = Symbol.findAllChildren();
56283625Sdim  Printer.Indent();
57283625Sdim  while (auto Child = ChildrenEnum->getNext())
58283625Sdim    Child->dump(*this);
59283625Sdim  Printer.Unindent();
60283625Sdim}
61283625Sdim
62283625Sdimvoid CompilandDumper::dump(const PDBSymbolData &Symbol) {
63283625Sdim  if (Printer.IsSymbolExcluded(Symbol.getName()))
64283625Sdim    return;
65283625Sdim
66283625Sdim  Printer.NewLine();
67283625Sdim
68283625Sdim  switch (auto LocType = Symbol.getLocationType()) {
69283625Sdim  case PDB_LocType::Static:
70283625Sdim    Printer << "data: ";
71283625Sdim    WithColor(Printer, PDB_ColorItem::Address).get()
72283625Sdim        << "[" << format_hex(Symbol.getVirtualAddress(), 10) << "]";
73283625Sdim    break;
74283625Sdim  case PDB_LocType::Constant:
75283625Sdim    Printer << "constant: ";
76283625Sdim    WithColor(Printer, PDB_ColorItem::LiteralValue).get()
77283625Sdim        << "[" << Symbol.getValue() << "]";
78283625Sdim    break;
79283625Sdim  default:
80283625Sdim    Printer << "data(unexpected type=" << LocType << ")";
81283625Sdim  }
82283625Sdim
83283625Sdim  Printer << " ";
84283625Sdim  WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
85283625Sdim}
86283625Sdim
87283625Sdimvoid CompilandDumper::dump(const PDBSymbolFunc &Symbol) {
88283625Sdim  if (Symbol.getLength() == 0)
89283625Sdim    return;
90283625Sdim  if (Printer.IsSymbolExcluded(Symbol.getName()))
91283625Sdim    return;
92283625Sdim
93283625Sdim  Printer.NewLine();
94283625Sdim  FunctionDumper Dumper(Printer);
95283625Sdim  Dumper.start(Symbol, FunctionDumper::PointerType::None);
96283625Sdim}
97283625Sdim
98283625Sdimvoid CompilandDumper::dump(const PDBSymbolLabel &Symbol) {
99283625Sdim  if (Printer.IsSymbolExcluded(Symbol.getName()))
100283625Sdim    return;
101283625Sdim
102283625Sdim  Printer.NewLine();
103283625Sdim  Printer << "label ";
104283625Sdim  WithColor(Printer, PDB_ColorItem::Address).get()
105283625Sdim      << "[" << format_hex(Symbol.getVirtualAddress(), 10) << "] ";
106283625Sdim  WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
107283625Sdim}
108283625Sdim
109283625Sdimvoid CompilandDumper::dump(const PDBSymbolThunk &Symbol) {
110283625Sdim  if (Printer.IsSymbolExcluded(Symbol.getName()))
111283625Sdim    return;
112283625Sdim
113283625Sdim  Printer.NewLine();
114283625Sdim  Printer << "thunk ";
115283625Sdim  PDB_ThunkOrdinal Ordinal = Symbol.getThunkOrdinal();
116283625Sdim  uint64_t VA = Symbol.getVirtualAddress();
117283625Sdim  if (Ordinal == PDB_ThunkOrdinal::TrampIncremental) {
118283625Sdim    uint64_t Target = Symbol.getTargetVirtualAddress();
119283625Sdim    WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(VA, 10);
120283625Sdim    Printer << " -> ";
121283625Sdim    WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(Target, 10);
122283625Sdim  } else {
123283625Sdim    WithColor(Printer, PDB_ColorItem::Address).get()
124283625Sdim        << "[" << format_hex(VA, 10) << " - "
125283625Sdim        << format_hex(VA + Symbol.getLength(), 10) << "]";
126283625Sdim  }
127283625Sdim  Printer << " (";
128283625Sdim  WithColor(Printer, PDB_ColorItem::Register).get() << Ordinal;
129283625Sdim  Printer << ") ";
130283625Sdim  std::string Name = Symbol.getName();
131283625Sdim  if (!Name.empty())
132283625Sdim    WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
133283625Sdim}
134283625Sdim
135283625Sdimvoid CompilandDumper::dump(const PDBSymbolTypeTypedef &Symbol) {}
136283625Sdim
137283625Sdimvoid CompilandDumper::dump(const PDBSymbolUnknown &Symbol) {
138283625Sdim  Printer.NewLine();
139283625Sdim  Printer << "unknown (" << Symbol.getSymTag() << ")";
140283625Sdim}
141