aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/DebugInfo/DWARF/DWARFUnwindTablePrinter.cpp
blob: a88f4a554bcf01ec7dfec40ea91949c33165a0c0 (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
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/DebugInfo/DWARF/DWARFUnwindTablePrinter.h"
#include "llvm/DebugInfo/DIContext.h"
#include "llvm/DebugInfo/DWARF/DWARFExpressionPrinter.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cinttypes>
#include <cstdint>
#include <optional>

using namespace llvm;
using namespace dwarf;

static void printRegister(raw_ostream &OS, DIDumpOptions DumpOpts,
                          unsigned RegNum) {
  if (DumpOpts.GetNameForDWARFReg) {
    auto RegName = DumpOpts.GetNameForDWARFReg(RegNum, DumpOpts.IsEH);
    if (!RegName.empty()) {
      OS << RegName;
      return;
    }
  }
  OS << "reg" << RegNum;
}

/// Print an unwind location expression as text and use the register information
/// if some is provided.
///
/// \param R the unwind location to print.
///
/// \param OS the stream to use for output.
///
/// \param MRI register information that helps emit register names insteead
/// of raw register numbers.
///
/// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
/// instead of from .debug_frame. This is needed for register number
/// conversion because some register numbers differ between the two sections
/// for certain architectures like x86.
static void printUnwindLocation(const UnwindLocation &UL, raw_ostream &OS,
                                DIDumpOptions DumpOpts) {
  if (UL.getDereference())
    OS << '[';
  switch (UL.getLocation()) {
  case UnwindLocation::Unspecified:
    OS << "unspecified";
    break;
  case UnwindLocation::Undefined:
    OS << "undefined";
    break;
  case UnwindLocation::Same:
    OS << "same";
    break;
  case UnwindLocation::CFAPlusOffset:
    OS << "CFA";
    if (UL.getOffset() == 0)
      break;
    if (UL.getOffset() > 0)
      OS << "+";
    OS << UL.getOffset();
    break;
  case UnwindLocation::RegPlusOffset:
    printRegister(OS, DumpOpts, UL.getRegister());
    if (UL.getOffset() == 0 && !UL.hasAddressSpace())
      break;
    if (UL.getOffset() >= 0)
      OS << "+";
    OS << UL.getOffset();
    if (UL.hasAddressSpace())
      OS << " in addrspace" << UL.getAddressSpace();
    break;
  case UnwindLocation::DWARFExpr: {
    if (UL.getDWARFExpressionBytes()) {
      auto Expr = *UL.getDWARFExpressionBytes();
      printDwarfExpression(&Expr, OS, DumpOpts, nullptr);
    }
    break;
  }
  case UnwindLocation::Constant:
    OS << UL.getOffset();
    break;
  }
  if (UL.getDereference())
    OS << ']';
}

raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS,
                                     const UnwindLocation &UL) {
  auto DumpOpts = DIDumpOptions();
  printUnwindLocation(UL, OS, DumpOpts);
  return OS;
}

/// Print all registers + locations that are currently defined in a register
/// locations.
///
/// \param RL the register locations to print.
///
/// \param OS the stream to use for output.
///
/// \param MRI register information that helps emit register names insteead
/// of raw register numbers.
///
/// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
/// instead of from .debug_frame. This is needed for register number
/// conversion because some register numbers differ between the two sections
/// for certain architectures like x86.
static void printRegisterLocations(const RegisterLocations &RL, raw_ostream &OS,
                                   DIDumpOptions DumpOpts) {
  bool First = true;
  for (uint32_t Reg : RL.getRegisters()) {
    auto Loc = *RL.getRegisterLocation(Reg);
    if (First)
      First = false;
    else
      OS << ", ";
    printRegister(OS, DumpOpts, Reg);
    OS << '=';
    printUnwindLocation(Loc, OS, DumpOpts);
  }
}

raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS,
                                     const RegisterLocations &RL) {
  auto DumpOpts = DIDumpOptions();
  printRegisterLocations(RL, OS, DumpOpts);
  return OS;
}

/// Print an UnwindRow to the stream.
///
/// \param Row the UnwindRow to print.
///
/// \param OS the stream to use for output.
///
/// \param MRI register information that helps emit register names insteead
/// of raw register numbers.
///
/// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
/// instead of from .debug_frame. This is needed for register number
/// conversion because some register numbers differ between the two sections
/// for certain architectures like x86.
///
/// \param IndentLevel specify the indent level as an integer. The UnwindRow
/// will be output to the stream preceded by 2 * IndentLevel number of spaces.
static void printUnwindRow(const UnwindRow &Row, raw_ostream &OS,
                           DIDumpOptions DumpOpts, unsigned IndentLevel) {
  OS.indent(2 * IndentLevel);
  if (Row.hasAddress())
    OS << format("0x%" PRIx64 ": ", Row.getAddress());
  OS << "CFA=";
  printUnwindLocation(Row.getCFAValue(), OS, DumpOpts);
  if (Row.getRegisterLocations().hasLocations()) {
    OS << ": ";
    printRegisterLocations(Row.getRegisterLocations(), OS, DumpOpts);
  }
  OS << "\n";
}

raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const UnwindRow &Row) {
  auto DumpOpts = DIDumpOptions();
  printUnwindRow(Row, OS, DumpOpts, 0);
  return OS;
}

void llvm::dwarf::printUnwindTable(const UnwindTable &Rows, raw_ostream &OS,
                                   DIDumpOptions DumpOpts,
                                   unsigned IndentLevel) {
  for (const UnwindRow &Row : Rows)
    printUnwindRow(Row, OS, DumpOpts, IndentLevel);
}

raw_ostream &llvm::dwarf::operator<<(raw_ostream &OS, const UnwindTable &Rows) {
  auto DumpOpts = DIDumpOptions();
  printUnwindTable(Rows, OS, DumpOpts, 0);
  return OS;
}