LLVM 24.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
16#include <cassert>
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 ListSeparator LS;
119 for (uint32_t Reg : RL.getRegisters()) {
120 auto Loc = *RL.getRegisterLocation(Reg);
121 OS << LS;
122 printRegister(OS, DumpOpts, Reg);
123 OS << '=';
124 printUnwindLocation(Loc, OS, DumpOpts);
125 }
126}
127
129 const RegisterLocations &RL) {
130 auto DumpOpts = DIDumpOptions();
131 printRegisterLocations(RL, OS, DumpOpts);
132 return OS;
133}
134
135/// Print an UnwindRow to the stream.
136///
137/// \param Row the UnwindRow to print.
138///
139/// \param OS the stream to use for output.
140///
141/// \param MRI register information that helps emit register names insteead
142/// of raw register numbers.
143///
144/// \param IsEH true if the DWARF Call Frame Information is from .eh_frame
145/// instead of from .debug_frame. This is needed for register number
146/// conversion because some register numbers differ between the two sections
147/// for certain architectures like x86.
148///
149/// \param IndentLevel specify the indent level as an integer. The UnwindRow
150/// will be output to the stream preceded by 2 * IndentLevel number of spaces.
151static void printUnwindRow(const UnwindRow &Row, raw_ostream &OS,
152 DIDumpOptions DumpOpts, unsigned IndentLevel) {
153 OS.indent(2 * IndentLevel);
154 if (Row.hasAddress())
155 OS << formatv("{0:x}: ", Row.getAddress());
156 OS << "CFA=";
157 printUnwindLocation(Row.getCFAValue(), OS, DumpOpts);
158 if (Row.getRegisterLocations().hasLocations()) {
159 OS << ": ";
160 printRegisterLocations(Row.getRegisterLocations(), OS, DumpOpts);
161 }
162 OS << "\n";
163}
164
166 auto DumpOpts = DIDumpOptions();
167 printUnwindRow(Row, OS, DumpOpts, 0);
168 return OS;
169}
170
172 DIDumpOptions DumpOpts,
173 unsigned IndentLevel) {
174 for (const UnwindRow &Row : Rows)
175 printUnwindRow(Row, OS, DumpOpts, IndentLevel);
176}
177
179 auto DumpOpts = DIDumpOptions();
180 printUnwindTable(Rows, OS, DumpOpts, 0);
181 return OS;
182}
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
This file contains some functions that are useful when dealing with strings.
A helper class to return the specified delimiter string after the first invocation of operator String...
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/.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
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