LLVM 23.0.0git
DWARFExpressionPrinter.cpp
Go to the documentation of this file.
1//===-- DWARFExpression.cpp -----------------------------------------------===//
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 <cstdint>
17
18using namespace llvm;
19using namespace dwarf;
20
21namespace llvm {
22
25
27 DIDumpOptions DumpOpts,
28 ArrayRef<uint64_t> Operands,
29 unsigned Operand) {
30 assert(Operand < Operands.size() && "operand out of bounds");
31 if (!U) {
32 OS << formatv(" <base_type ref: {0:x}>", Operands[Operand]);
33 return;
34 }
35 auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
36 if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {
37 OS << " (";
38 if (DumpOpts.Verbose)
39 OS << formatv("{0:x8} -> ", Operands[Operand]);
40 OS << formatv("{0:x8})", U->getOffset() + Operands[Operand]);
41 if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
42 OS << " \"" << *Name << "\"";
43 } else {
44 OS << formatv(" <invalid base_type ref: {0:x}>", Operands[Operand]);
45 }
46}
47
49 DIDumpOptions DumpOpts, const DWARFExpression *Expr,
50 DWARFUnit *U) {
51 if (Op->isError()) {
52 if (!DumpOpts.PrintRegisterOnly)
53 OS << "<decoding error>";
54 return false;
55 }
56
57 std::optional<unsigned> SubOpcode = Op->getSubCode();
58
59 // In "register-only" mode, still show simple constant-valued locations.
60 // This lets clients print annotations like "i = 0" when the location is
61 // a constant (e.g. DW_OP_constu/consts ... DW_OP_stack_value).
62 // We continue to suppress all other non-register ops in this mode.
63 if (DumpOpts.PrintRegisterOnly) {
64 // First, try pretty-printing registers (existing behavior below also does
65 // this, but we need to short-circuit here to avoid printing opcode names).
66 if ((Op->getCode() >= DW_OP_breg0 && Op->getCode() <= DW_OP_breg31) ||
67 (Op->getCode() >= DW_OP_reg0 && Op->getCode() <= DW_OP_reg31) ||
68 Op->getCode() == DW_OP_bregx || Op->getCode() == DW_OP_regx ||
69 Op->getCode() == DW_OP_regval_type ||
70 SubOpcode == DW_OP_LLVM_call_frame_entry_reg ||
71 SubOpcode == DW_OP_LLVM_aspace_bregx) {
72 if (prettyPrintRegisterOp(U, OS, DumpOpts, Op->getCode(),
73 Op->getRawOperands()))
74 return true;
75 // If we couldn't pretty-print, fall through and suppress.
76 }
77
78 // Show constants (decimal), suppress everything else.
79 if (Op->getCode() == DW_OP_constu) {
80 OS << (uint64_t)Op->getRawOperand(0);
81 return true;
82 }
83 if (Op->getCode() == DW_OP_consts) {
84 OS << (int64_t)Op->getRawOperand(0);
85 return true;
86 }
87 if (Op->getCode() >= DW_OP_lit0 && Op->getCode() <= DW_OP_lit31) {
88 OS << (unsigned)(Op->getCode() - DW_OP_lit0);
89 return true;
90 }
91 if (Op->getCode() == DW_OP_stack_value)
92 return true; // metadata; don't print a token
93
94 return true; // suppress other opcodes silently in register-only mode
95 }
96
97 if (!DumpOpts.PrintRegisterOnly) {
98 StringRef Name = OperationEncodingString(Op->getCode());
99 assert(!Name.empty() && "DW_OP has no name!");
100 OS << Name;
101
102 if (SubOpcode) {
103 StringRef SubName = SubOperationEncodingString(Op->getCode(), *SubOpcode);
104 assert(!SubName.empty() && "DW_OP SubOp has no name!");
105 OS << ' ' << SubName;
106 }
107 }
108
109 if ((Op->getCode() >= DW_OP_breg0 && Op->getCode() <= DW_OP_breg31) ||
110 (Op->getCode() >= DW_OP_reg0 && Op->getCode() <= DW_OP_reg31) ||
111 Op->getCode() == DW_OP_bregx || Op->getCode() == DW_OP_regx ||
112 Op->getCode() == DW_OP_regval_type ||
113 SubOpcode == DW_OP_LLVM_call_frame_entry_reg ||
114 SubOpcode == DW_OP_LLVM_aspace_bregx)
115 if (prettyPrintRegisterOp(U, OS, DumpOpts, Op->getCode(),
116 Op->getRawOperands()))
117 return true;
118
119 if (!DumpOpts.PrintRegisterOnly) {
120 for (unsigned Operand = 0; Operand < Op->getDescription().Op.size();
121 ++Operand) {
122 unsigned Size = Op->getDescription().Op[Operand];
124
126 assert(Operand == 0 && "DW_OP SubOp must be the first operand");
127 assert(SubOpcode && "DW_OP SubOp description is inconsistent");
129 // For DW_OP_convert the operand may be 0 to indicate that conversion to
130 // the generic type should be done. The same holds for
131 // DW_OP_reinterpret, which is currently not supported.
132 if (Op->getCode() == DW_OP_convert && Op->getRawOperand(Operand) == 0)
133 OS << " 0x0";
134 else
135 prettyPrintBaseTypeRef(U, OS, DumpOpts, Op->getRawOperands(),
136 Operand);
138 assert(Operand == 1);
139 switch (Op->getRawOperand(0)) {
140 case 0:
141 case 1:
142 case 2:
143 case 3: // global as uint32
144 case 4:
145 OS << formatv(" {0:x}", Op->getRawOperand(Operand));
146 break;
147 default:
148 assert(false);
149 }
151 uint64_t Offset = Op->getRawOperand(Operand);
152 for (unsigned i = 0; i < Op->getRawOperand(Operand - 1); ++i)
153 OS << formatv(" {0:x2}",
154 static_cast<uint8_t>(Expr->getData()[Offset++]));
155 } else {
156 if (Signed)
157 OS << formatv(" {0:+d}", (int64_t)Op->getRawOperand(Operand));
158 else if (Op->getCode() != DW_OP_entry_value &&
159 Op->getCode() != DW_OP_GNU_entry_value)
160 OS << formatv(" {0:x}", Op->getRawOperand(Operand));
161 }
162 }
163 }
164 return true;
165}
166
168 DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH) {
169 uint32_t EntryValExprSize = 0;
170 uint64_t EntryValStartOffset = 0;
171 if (E->getData().empty())
172 OS << "<empty>";
173
174 for (auto &Op : *E) {
175 DumpOpts.IsEH = IsEH;
176 if (!printOp(&Op, OS, DumpOpts, E, U) && !DumpOpts.PrintRegisterOnly) {
177 uint64_t FailOffset = Op.getEndOffset();
178 while (FailOffset < E->getData().size())
179 OS << formatv(" {0:x-2}",
180 static_cast<uint8_t>(E->getData()[FailOffset++]));
181 return;
182 }
183 if (!DumpOpts.PrintRegisterOnly) {
184 if (Op.getCode() == DW_OP_entry_value ||
185 Op.getCode() == DW_OP_GNU_entry_value) {
186 OS << "(";
187 EntryValExprSize = Op.getRawOperand(0);
188 EntryValStartOffset = Op.getEndOffset();
189 continue;
190 }
191
192 if (EntryValExprSize) {
193 EntryValExprSize -= Op.getEndOffset() - EntryValStartOffset;
194 if (EntryValExprSize == 0)
195 OS << ")";
196 }
197
198 if (Op.getEndOffset() < E->getData().size())
199 OS << ", ";
200 }
201 }
202}
203
204/// A user-facing string representation of a DWARF expression. This might be an
205/// Address expression, in which case it will be implicitly dereferenced, or a
206/// Value expression.
217
221 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg =
222 nullptr) {
224
225 auto UnknownOpcode = [](raw_ostream &OS, uint8_t Opcode,
226 std::optional<unsigned> SubOpcode) -> bool {
227 // If we hit an unknown operand, we don't know its effect on the stack,
228 // so bail out on the whole expression.
229 OS << "<unknown op " << dwarf::OperationEncodingString(Opcode) << " ("
230 << (int)Opcode;
231 if (SubOpcode)
232 OS << ") subop " << dwarf::SubOperationEncodingString(Opcode, *SubOpcode)
233 << " (" << *SubOpcode;
234 OS << ")>";
235 return false;
236 };
237
238 while (I != E) {
240 uint8_t Opcode = Op.getCode();
241 switch (Opcode) {
242 case dwarf::DW_OP_regx: {
243 // DW_OP_regx: A register, with the register num given as an operand.
244 // Printed as the plain register name.
245 uint64_t DwarfRegNum = Op.getRawOperand(0);
246 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
247 if (RegName.empty())
248 return false;
249 raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
250 S << RegName;
251 break;
252 }
253 case dwarf::DW_OP_bregx: {
254 int DwarfRegNum = Op.getRawOperand(0);
255 int64_t Offset = Op.getRawOperand(1);
256 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
257 if (RegName.empty())
258 return false;
259 raw_svector_ostream S(Stack.emplace_back().String);
260 S << RegName;
261 if (Offset)
262 S << formatv("{0:+d}", Offset);
263 break;
264 }
265 case dwarf::DW_OP_entry_value:
266 case dwarf::DW_OP_GNU_entry_value: {
267 // DW_OP_entry_value contains a sub-expression which must be rendered
268 // separately.
269 uint64_t SubExprLength = Op.getRawOperand(0);
270 DWARFExpression::iterator SubExprEnd = I.skipBytes(SubExprLength);
271 ++I;
272 raw_svector_ostream S(Stack.emplace_back().String);
273 S << "entry(";
274 printCompactDWARFExpr(S, I, SubExprEnd, GetNameForDWARFReg);
275 S << ")";
276 I = SubExprEnd;
277 continue;
278 }
279 case dwarf::DW_OP_stack_value: {
280 // The top stack entry should be treated as the actual value of tne
281 // variable, rather than the address of the variable in memory.
282 assert(!Stack.empty());
283 Stack.back().Kind = PrintedExpr::Value;
284 break;
285 }
286 case dwarf::DW_OP_nop: {
287 break;
288 }
289 case dwarf::DW_OP_LLVM_user: {
290 std::optional<unsigned> SubOpcode = Op.getSubCode();
291 if (SubOpcode == dwarf::DW_OP_LLVM_nop)
292 break;
293 return UnknownOpcode(OS, Opcode, SubOpcode);
294 }
295 default:
296 if (Opcode >= dwarf::DW_OP_reg0 && Opcode <= dwarf::DW_OP_reg31) {
297 // DW_OP_reg<N>: A register, with the register num implied by the
298 // opcode. Printed as the plain register name.
299 uint64_t DwarfRegNum = Opcode - dwarf::DW_OP_reg0;
300 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
301 if (RegName.empty())
302 return false;
303 raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
304 S << RegName;
305 } else if (Opcode >= dwarf::DW_OP_breg0 &&
306 Opcode <= dwarf::DW_OP_breg31) {
307 int DwarfRegNum = Opcode - dwarf::DW_OP_breg0;
308 int64_t Offset = Op.getRawOperand(0);
309 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
310 if (RegName.empty())
311 return false;
312 raw_svector_ostream S(Stack.emplace_back().String);
313 S << RegName;
314 if (Offset)
315 S << formatv("{0:+d}", Offset);
316 } else {
317 return UnknownOpcode(OS, Opcode, std::nullopt);
318 }
319 break;
320 }
321 ++I;
322 }
323
324 if (Stack.size() != 1) {
325 OS << "<stack of size " << Stack.size() << ", expected 1>";
326 return false;
327 }
328
329 if (Stack.front().Kind == PrintedExpr::Address)
330 OS << "[" << Stack.front().String << "]";
331 else
332 OS << Stack.front().String;
333
334 return true;
335}
336
338 const DWARFExpression *E, raw_ostream &OS,
339 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
340 return printCompactDWARFExpr(OS, E->begin(), E->end(), GetNameForDWARFReg);
341}
342
344 DIDumpOptions DumpOpts, uint8_t Opcode,
345 ArrayRef<uint64_t> Operands) {
346 if (!DumpOpts.GetNameForDWARFReg)
347 return false;
348
349 uint64_t DwarfRegNum;
350 unsigned OpNum = 0;
351
352 std::optional<unsigned> SubOpcode;
353 if (Opcode == DW_OP_LLVM_user)
354 SubOpcode = Operands[OpNum++];
355
356 if (Opcode == DW_OP_bregx || Opcode == DW_OP_regx ||
357 Opcode == DW_OP_regval_type || SubOpcode == DW_OP_LLVM_aspace_bregx ||
358 SubOpcode == DW_OP_LLVM_call_frame_entry_reg)
359 DwarfRegNum = Operands[OpNum++];
360 else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx)
361 DwarfRegNum = Opcode - DW_OP_breg0;
362 else
363 DwarfRegNum = Opcode - DW_OP_reg0;
364
365 auto RegName = DumpOpts.GetNameForDWARFReg(DwarfRegNum, DumpOpts.IsEH);
366 if (!RegName.empty()) {
367 if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
368 Opcode == DW_OP_bregx || SubOpcode == DW_OP_LLVM_aspace_bregx)
369 OS << ' ' << RegName << formatv("{0:+d}", int64_t(Operands[OpNum]));
370 else
371 OS << ' ' << RegName.data();
372
373 if (Opcode == DW_OP_regval_type)
374 prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, 1);
375 return true;
376 }
377
378 return false;
379}
380
381} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define RegName(no)
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the SmallString class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
This class represents an Operation in the Expression.
@ SizeSubOpLEB
The operand is a ULEB128 encoded SubOpcode.
@ SizeBlock
Preceding operand contains block size.
An iterator to go through the expression operations.
StringRef getData() const
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an SmallVector or SmallString.
LLVM_ABI StringRef SubOperationEncodingString(unsigned OpEncoding, unsigned SubOpEncoding)
Definition Dwarf.cpp:202
LLVM_ABI StringRef OperationEncodingString(unsigned Encoding)
Definition Dwarf.cpp:138
Calculates the starting offsets for various sections within the .debug_names section.
Definition Dwarf.h:35
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
static bool printOp(const DWARFExpression::Operation *Op, raw_ostream &OS, DIDumpOptions DumpOpts, const DWARFExpression *Expr, DWARFUnit *U)
LLVM_ABI void printDwarfExpression(const DWARFExpression *E, raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH=false)
Print a Dwarf expression/.
Op::Description Desc
static bool printCompactDWARFExpr(raw_ostream &OS, DWARFExpression::iterator I, const DWARFExpression::iterator E, std::function< StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg=nullptr)
static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS, DIDumpOptions DumpOpts, ArrayRef< uint64_t > Operands, unsigned Operand)
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
DWARFExpression::Operation Op
LLVM_ABI bool prettyPrintRegisterOp(DWARFUnit *U, raw_ostream &OS, DIDumpOptions DumpOpts, uint8_t Opcode, ArrayRef< uint64_t > Operands)
Pretty print a register opcode and operands.
LLVM_ABI bool printDwarfExpressionCompact(const DWARFExpression *E, raw_ostream &OS, std::function< StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg=nullptr)
Print the expression in a format intended to be compact and useful to a user, but not perfectly unamb...
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
Description of the encoding of one expression Op.
PrintedExpr(ExprKind K=Address)