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 << format(" <base_type ref: 0x%" PRIx64 ">", 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 << format("0x%08" PRIx64 " -> ", Operands[Operand]);
40 OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
41 if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
42 OS << " \"" << *Name << "\"";
43 } else {
44 OS << format(" <invalid base_type ref: 0x%" PRIx64 ">", 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 << format(" 0x%" PRIx64, 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 << format(" 0x%02x",
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 << format(" 0x%" PRIx64, 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 << format(" %02x", static_cast<uint8_t>(E->getData()[FailOffset++]));
180 return;
181 }
182 if (!DumpOpts.PrintRegisterOnly) {
183 if (Op.getCode() == DW_OP_entry_value ||
184 Op.getCode() == DW_OP_GNU_entry_value) {
185 OS << "(";
186 EntryValExprSize = Op.getRawOperand(0);
187 EntryValStartOffset = Op.getEndOffset();
188 continue;
189 }
190
191 if (EntryValExprSize) {
192 EntryValExprSize -= Op.getEndOffset() - EntryValStartOffset;
193 if (EntryValExprSize == 0)
194 OS << ")";
195 }
196
197 if (Op.getEndOffset() < E->getData().size())
198 OS << ", ";
199 }
200 }
201}
202
203/// A user-facing string representation of a DWARF expression. This might be an
204/// Address expression, in which case it will be implicitly dereferenced, or a
205/// Value expression.
216
220 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg =
221 nullptr) {
223
224 auto UnknownOpcode = [](raw_ostream &OS, uint8_t Opcode,
225 std::optional<unsigned> SubOpcode) -> bool {
226 // If we hit an unknown operand, we don't know its effect on the stack,
227 // so bail out on the whole expression.
228 OS << "<unknown op " << dwarf::OperationEncodingString(Opcode) << " ("
229 << (int)Opcode;
230 if (SubOpcode)
231 OS << ") subop " << dwarf::SubOperationEncodingString(Opcode, *SubOpcode)
232 << " (" << *SubOpcode;
233 OS << ")>";
234 return false;
235 };
236
237 while (I != E) {
239 uint8_t Opcode = Op.getCode();
240 switch (Opcode) {
241 case dwarf::DW_OP_regx: {
242 // DW_OP_regx: A register, with the register num given as an operand.
243 // Printed as the plain register name.
244 uint64_t DwarfRegNum = Op.getRawOperand(0);
245 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
246 if (RegName.empty())
247 return false;
248 raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
249 S << RegName;
250 break;
251 }
252 case dwarf::DW_OP_bregx: {
253 int DwarfRegNum = Op.getRawOperand(0);
254 int64_t Offset = Op.getRawOperand(1);
255 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
256 if (RegName.empty())
257 return false;
258 raw_svector_ostream S(Stack.emplace_back().String);
259 S << RegName;
260 if (Offset)
261 S << formatv("{0:+d}", Offset);
262 break;
263 }
264 case dwarf::DW_OP_entry_value:
265 case dwarf::DW_OP_GNU_entry_value: {
266 // DW_OP_entry_value contains a sub-expression which must be rendered
267 // separately.
268 uint64_t SubExprLength = Op.getRawOperand(0);
269 DWARFExpression::iterator SubExprEnd = I.skipBytes(SubExprLength);
270 ++I;
271 raw_svector_ostream S(Stack.emplace_back().String);
272 S << "entry(";
273 printCompactDWARFExpr(S, I, SubExprEnd, GetNameForDWARFReg);
274 S << ")";
275 I = SubExprEnd;
276 continue;
277 }
278 case dwarf::DW_OP_stack_value: {
279 // The top stack entry should be treated as the actual value of tne
280 // variable, rather than the address of the variable in memory.
281 assert(!Stack.empty());
282 Stack.back().Kind = PrintedExpr::Value;
283 break;
284 }
285 case dwarf::DW_OP_nop: {
286 break;
287 }
288 case dwarf::DW_OP_LLVM_user: {
289 std::optional<unsigned> SubOpcode = Op.getSubCode();
290 if (SubOpcode == dwarf::DW_OP_LLVM_nop)
291 break;
292 return UnknownOpcode(OS, Opcode, SubOpcode);
293 }
294 default:
295 if (Opcode >= dwarf::DW_OP_reg0 && Opcode <= dwarf::DW_OP_reg31) {
296 // DW_OP_reg<N>: A register, with the register num implied by the
297 // opcode. Printed as the plain register name.
298 uint64_t DwarfRegNum = Opcode - dwarf::DW_OP_reg0;
299 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
300 if (RegName.empty())
301 return false;
302 raw_svector_ostream S(Stack.emplace_back(PrintedExpr::Value).String);
303 S << RegName;
304 } else if (Opcode >= dwarf::DW_OP_breg0 &&
305 Opcode <= dwarf::DW_OP_breg31) {
306 int DwarfRegNum = Opcode - dwarf::DW_OP_breg0;
307 int64_t Offset = Op.getRawOperand(0);
308 auto RegName = GetNameForDWARFReg(DwarfRegNum, false);
309 if (RegName.empty())
310 return false;
311 raw_svector_ostream S(Stack.emplace_back().String);
312 S << RegName;
313 if (Offset)
314 S << formatv("{0:+d}", Offset);
315 } else {
316 return UnknownOpcode(OS, Opcode, std::nullopt);
317 }
318 break;
319 }
320 ++I;
321 }
322
323 if (Stack.size() != 1) {
324 OS << "<stack of size " << Stack.size() << ", expected 1>";
325 return false;
326 }
327
328 if (Stack.front().Kind == PrintedExpr::Address)
329 OS << "[" << Stack.front().String << "]";
330 else
331 OS << Stack.front().String;
332
333 return true;
334}
335
337 const DWARFExpression *E, raw_ostream &OS,
338 std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) {
339 return printCompactDWARFExpr(OS, E->begin(), E->end(), GetNameForDWARFReg);
340}
341
343 DIDumpOptions DumpOpts, uint8_t Opcode,
344 ArrayRef<uint64_t> Operands) {
345 if (!DumpOpts.GetNameForDWARFReg)
346 return false;
347
348 uint64_t DwarfRegNum;
349 unsigned OpNum = 0;
350
351 std::optional<unsigned> SubOpcode;
352 if (Opcode == DW_OP_LLVM_user)
353 SubOpcode = Operands[OpNum++];
354
355 if (Opcode == DW_OP_bregx || Opcode == DW_OP_regx ||
356 Opcode == DW_OP_regval_type || SubOpcode == DW_OP_LLVM_aspace_bregx ||
357 SubOpcode == DW_OP_LLVM_call_frame_entry_reg)
358 DwarfRegNum = Operands[OpNum++];
359 else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx)
360 DwarfRegNum = Opcode - DW_OP_breg0;
361 else
362 DwarfRegNum = Opcode - DW_OP_reg0;
363
364 auto RegName = DumpOpts.GetNameForDWARFReg(DwarfRegNum, DumpOpts.IsEH);
365 if (!RegName.empty()) {
366 if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) ||
367 Opcode == DW_OP_bregx || SubOpcode == DW_OP_LLVM_aspace_bregx)
368 OS << ' ' << RegName << formatv("{0:+d}", int64_t(Operands[OpNum]));
369 else
370 OS << ' ' << RegName.data();
371
372 if (Opcode == DW_OP_regval_type)
373 prettyPrintBaseTypeRef(U, OS, DumpOpts, Operands, 1);
374 return true;
375 }
376
377 return false;
378}
379
380} // 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.
Definition Types.h:26
@ 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)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:129
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)