LLVM 22.0.0git
DWARFUnwindTablePrinter.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
13#include "llvm/Support/Format.h"
15#include <cassert>
16#include <cinttypes>
17#include <cstdint>
18
19using namespace llvm;
20using namespace dwarf;
21
22static void printRegister(raw_ostream &OS, DIDumpOptions DumpOpts,
23 unsigned RegNum) {
24 if (DumpOpts.GetNameForDWARFReg) {
25 auto RegName = DumpOpts.GetNameForDWARFReg(RegNum, DumpOpts.IsEH);
26 if (!RegName.empty()) {
27 OS << RegName;
28 return;
29 }
30 }
31 OS << "reg" << RegNum;
32}
33
34/// Print an unwind location expression as text and use the register information
35/// if some is provided.
36///
37/// \param R the unwind location to print.
38///
39/// \param OS the stream to use for output.
40///
41/// \param MRI register information that helps emit register names insteead
42/// of raw register numbers.
43///
44/// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
45/// instead of from .debug_frame. This is needed for register number
46/// conversion because some register numbers differ between the two sections
47/// for certain architectures like x86.
49 DIDumpOptions DumpOpts) {
50 if (UL.getDereference())
51 OS << '[';
52 switch (UL.getLocation()) {
54 OS << "unspecified";
55 break;
57 OS << "undefined";
58 break;
60 OS << "same";
61 break;
63 OS << "CFA";
64 if (UL.getOffset() == 0)
65 break;
66 if (UL.getOffset() > 0)
67 OS << "+";
68 OS << UL.getOffset();
69 break;
71 printRegister(OS, DumpOpts, UL.getRegister());
72 if (UL.getOffset() == 0 && !UL.hasAddressSpace())
73 break;
74 if (UL.getOffset() >= 0)
75 OS << "+";
76 OS << UL.getOffset();
77 if (UL.hasAddressSpace())
78 OS << " in addrspace" << UL.getAddressSpace();
79 break;
81 if (UL.getDWARFExpressionBytes()) {
82 auto Expr = *UL.getDWARFExpressionBytes();
83 printDwarfExpression(&Expr, OS, DumpOpts, nullptr);
84 }
85 break;
86 }
88 OS << UL.getOffset();
89 break;
90 }
91 if (UL.getDereference())
92 OS << ']';
93}
94
96 const UnwindLocation &UL) {
97 auto DumpOpts = DIDumpOptions();
98 printUnwindLocation(UL, OS, DumpOpts);
99 return OS;
100}
101
102/// Print all registers + locations that are currently defined in a register
103/// locations.
104///
105/// \param RL the register locations to print.
106///
107/// \param OS the stream to use for output.
108///
109/// \param MRI register information that helps emit register names insteead
110/// of raw register numbers.
111///
112/// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
113/// instead of from .debug_frame. This is needed for register number
114/// conversion because some register numbers differ between the two sections
115/// for certain architectures like x86.
117 DIDumpOptions DumpOpts) {
118 bool First = true;
119 for (uint32_t Reg : RL.getRegisters()) {
120 auto Loc = *RL.getRegisterLocation(Reg);
121 if (First)
122 First = false;
123 else
124 OS << ", ";
125 printRegister(OS, DumpOpts, Reg);
126 OS << '=';
127 printUnwindLocation(Loc, OS, DumpOpts);
128 }
129}
130
132 const RegisterLocations &RL) {
133 auto DumpOpts = DIDumpOptions();
134 printRegisterLocations(RL, OS, DumpOpts);
135 return OS;
136}
137
138/// Print an UnwindRow to the stream.
139///
140/// \param Row the UnwindRow to print.
141///
142/// \param OS the stream to use for output.
143///
144/// \param MRI register information that helps emit register names insteead
145/// of raw register numbers.
146///
147/// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
148/// instead of from .debug_frame. This is needed for register number
149/// conversion because some register numbers differ between the two sections
150/// for certain architectures like x86.
151///
152/// \param IndentLevel specify the indent level as an integer. The UnwindRow
153/// will be output to the stream preceded by 2 * IndentLevel number of spaces.
154static void printUnwindRow(const UnwindRow &Row, raw_ostream &OS,
155 DIDumpOptions DumpOpts, unsigned IndentLevel) {
156 OS.indent(2 * IndentLevel);
157 if (Row.hasAddress())
158 OS << format("0x%" PRIx64 ": ", Row.getAddress());
159 OS << "CFA=";
160 printUnwindLocation(Row.getCFAValue(), OS, DumpOpts);
161 if (Row.getRegisterLocations().hasLocations()) {
162 OS << ": ";
163 printRegisterLocations(Row.getRegisterLocations(), OS, DumpOpts);
164 }
165 OS << "\n";
166}
167
169 auto DumpOpts = DIDumpOptions();
170 printUnwindRow(Row, OS, DumpOpts, 0);
171 return OS;
172}
173
175 DIDumpOptions DumpOpts,
176 unsigned IndentLevel) {
177 for (const UnwindRow &Row : Rows)
178 printUnwindRow(Row, OS, DumpOpts, IndentLevel);
179}
180
182 auto DumpOpts = DIDumpOptions();
183 printUnwindTable(Rows, OS, DumpOpts, 0);
184 return OS;
185}
static void printRegisterLocations(const RegisterLocations &RL, raw_ostream &OS, DIDumpOptions DumpOpts)
Print all registers + locations that are currently defined in a register locations.
static void printRegister(raw_ostream &OS, DIDumpOptions DumpOpts, unsigned RegNum)
static void printUnwindRow(const UnwindRow &Row, raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel)
Print an UnwindRow to the stream.
static void printUnwindLocation(const UnwindLocation &UL, raw_ostream &OS, DIDumpOptions DumpOpts)
Print an unwind location expression as text and use the register information if some is provided.
#define RegName(no)
Register Reg
A class that can track all registers with locations in a UnwindRow object.
std::optional< UnwindLocation > getRegisterLocation(uint32_t RegNum) const
Return the location for the register in RegNum if there is a location.
SmallVector< uint32_t, 4 > getRegisters() const
A class that represents a location for the Call Frame Address (CFA) or a register.
@ Undefined
Register is not available and can't be recovered.
@ Constant
Value is a constant value contained in "Offset": reg = Offset.
@ DWARFExpr
Register or CFA value is in or at a value found by evaluating a DWARF expression: reg = eval(dwarf_ex...
@ Same
Register value is in the register, nothing needs to be done to unwind it: reg = reg.
@ CFAPlusOffset
Register is in or at the CFA plus an offset: reg = CFA + offset reg = defef(CFA + offset)
@ RegPlusOffset
Register or CFA is in or at a register plus offset, optionally in an address space: reg = reg + offse...
std::optional< DWARFExpression > getDWARFExpressionBytes() const
A class that represents a single row in the unwind table that is decoded by parsing the DWARF Call Fr...
A class that contains all UnwindRow objects for an FDE or a single unwind row for a CIE.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Calculates the starting offsets for various sections within the .debug_names section.
Definition Dwarf.h:35
LLVM_ABI void printUnwindTable(const UnwindTable &Rows, raw_ostream &OS, DIDumpOptions DumpOpts, unsigned IndentLevel=0)
Print a UnwindTable to the stream.
LLVM_ABI raw_ostream & operator<<(raw_ostream &OS, const UnwindLocation &R)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void printDwarfExpression(const DWARFExpression *E, raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH=false)
Print a Dwarf expression/.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:129
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
Container for dump options that control which debug information will be dumped.
Definition DIContext.h:196
std::function< llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)> GetNameForDWARFReg
Definition DIContext.h:217