| 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 | |
| 9 | #include "llvm/DebugInfo/DWARF/DWARFExpressionPrinter.h" |
| 10 | #include "DWARFExpressionPrinterImpl.h" |
| 11 | #include "llvm/ADT/SmallString.h" |
| 12 | #include "llvm/ADT/StringExtras.h" |
| 13 | #include "llvm/DebugInfo/DIContext.h" |
| 14 | #include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h" |
| 15 | #include "llvm/Support/Endian.h" |
| 16 | #include "llvm/Support/FormatVariadic.h" |
| 17 | #include <cassert> |
| 18 | #include <cstdint> |
| 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace dwarf; |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | typedef DWARFExpression::Operation Op; |
| 26 | |
| 27 | /// Some backends (e.g. NVPTX) encode virtual register names as the DWARF |
| 28 | /// register number: the ASCII bytes of the name are concatenated into a |
| 29 | /// uint64_t (see NVPTXRegisterInfo::encodeRegisterForDwarf). When the object |
| 30 | /// file is not the target backend, MCRegisterInfo cannot map these numbers, so |
| 31 | /// recover the string for dumping. |
| 32 | /// Returns true if the register name was decoded successfully, false otherwise. |
| 33 | static bool decodeVirtualRegisterName(uint64_t DwarfRegNum, |
| 34 | SmallString<8> &Out) { |
| 35 | if (DwarfRegNum == 0) |
| 36 | return false; |
| 37 | |
| 38 | uint64_t DwarfRegNumBE = |
| 39 | support::endian::byte_swap<uint64_t>(value: DwarfRegNum, endian: endianness::big); |
| 40 | const char *Data = reinterpret_cast<const char *>(&DwarfRegNumBE); |
| 41 | const char *Begin = std::find_if(first: Data, last: Data + sizeof(DwarfRegNumBE), |
| 42 | pred: [](char c) { return c != '\0'; }); |
| 43 | SmallString<8> Tmp(Begin, Data + sizeof(DwarfRegNumBE)); |
| 44 | |
| 45 | if (Tmp.size() < 2) |
| 46 | return false; |
| 47 | |
| 48 | if (!llvm::isAlnum(C: Tmp[0]) && Tmp[0] != '%') |
| 49 | return false; |
| 50 | |
| 51 | for (size_t I = 1; I < Tmp.size(); ++I) |
| 52 | if (!llvm::isAlnum(C: Tmp[I])) |
| 53 | return false; |
| 54 | |
| 55 | Out = Tmp; |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | /// Resolves a DWARF register number to a display name: first via \p |
| 60 | /// GetNameForDWARFReg (MC register names), otherwise try decoding |
| 61 | /// ASCII-encoded virtual register names (NVPTX-specific). |
| 62 | /// Returns empty if neither applies. |
| 63 | static std::string resolveRegName( |
| 64 | uint64_t DwarfRegNum, bool IsEH, |
| 65 | const std::function<StringRef(uint64_t, bool)> &GetNameForDWARFReg) { |
| 66 | if (GetNameForDWARFReg) { |
| 67 | StringRef R = GetNameForDWARFReg(DwarfRegNum, IsEH); |
| 68 | if (!R.empty()) |
| 69 | return R.str(); |
| 70 | } |
| 71 | SmallString<8> Decoded; |
| 72 | if (decodeVirtualRegisterName(DwarfRegNum, Out&: Decoded)) |
| 73 | return Decoded.str().str(); |
| 74 | return "" ; |
| 75 | } |
| 76 | |
| 77 | static bool printOp(const DWARFExpression::Operation *Op, raw_ostream &OS, |
| 78 | DIDumpOptions DumpOpts, const DWARFExpression *Expr, |
| 79 | detail::BaseTypeRefPrinter PrintBaseTypeRef) { |
| 80 | if (Op->isError()) { |
| 81 | if (!DumpOpts.PrintRegisterOnly) |
| 82 | OS << "<decoding error>" ; |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | std::optional<unsigned> SubOpcode = Op->getSubCode(); |
| 87 | |
| 88 | // In "register-only" mode, still show simple constant-valued locations. |
| 89 | // This lets clients print annotations like "i = 0" when the location is |
| 90 | // a constant (e.g. DW_OP_constu/consts ... DW_OP_stack_value). |
| 91 | // We continue to suppress all other non-register ops in this mode. |
| 92 | if (DumpOpts.PrintRegisterOnly) { |
| 93 | // First, try pretty-printing registers (existing behavior below also does |
| 94 | // this, but we need to short-circuit here to avoid printing opcode names). |
| 95 | if ((Op->getCode() >= DW_OP_breg0 && Op->getCode() <= DW_OP_breg31) || |
| 96 | (Op->getCode() >= DW_OP_reg0 && Op->getCode() <= DW_OP_reg31) || |
| 97 | Op->getCode() == DW_OP_bregx || Op->getCode() == DW_OP_regx || |
| 98 | Op->getCode() == DW_OP_regval_type || |
| 99 | SubOpcode == DW_OP_LLVM_call_frame_entry_reg || |
| 100 | SubOpcode == DW_OP_LLVM_aspace_bregx) { |
| 101 | if (detail::prettyPrintRegisterOp(PrintBaseTypeRef, OS, DumpOpts, |
| 102 | Opcode: Op->getCode(), Operands: Op->getRawOperands())) |
| 103 | return true; |
| 104 | // If we couldn't pretty-print, fall through and suppress. |
| 105 | } |
| 106 | |
| 107 | // Show constants (decimal), suppress everything else. |
| 108 | if (Op->getCode() == DW_OP_constu) { |
| 109 | OS << (uint64_t)Op->getRawOperand(Idx: 0); |
| 110 | return true; |
| 111 | } |
| 112 | if (Op->getCode() == DW_OP_consts) { |
| 113 | OS << (int64_t)Op->getRawOperand(Idx: 0); |
| 114 | return true; |
| 115 | } |
| 116 | if (Op->getCode() >= DW_OP_lit0 && Op->getCode() <= DW_OP_lit31) { |
| 117 | OS << (unsigned)(Op->getCode() - DW_OP_lit0); |
| 118 | return true; |
| 119 | } |
| 120 | if (Op->getCode() == DW_OP_stack_value) |
| 121 | return true; // metadata; don't print a token |
| 122 | |
| 123 | return true; // suppress other opcodes silently in register-only mode |
| 124 | } |
| 125 | |
| 126 | if (!DumpOpts.PrintRegisterOnly) { |
| 127 | StringRef Name = OperationEncodingString(Encoding: Op->getCode()); |
| 128 | assert(!Name.empty() && "DW_OP has no name!" ); |
| 129 | OS << Name; |
| 130 | |
| 131 | if (SubOpcode) { |
| 132 | StringRef SubName = SubOperationEncodingString(OpEncoding: Op->getCode(), SubOpEncoding: *SubOpcode); |
| 133 | assert(!SubName.empty() && "DW_OP SubOp has no name!" ); |
| 134 | OS << ' ' << SubName; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if ((Op->getCode() >= DW_OP_breg0 && Op->getCode() <= DW_OP_breg31) || |
| 139 | (Op->getCode() >= DW_OP_reg0 && Op->getCode() <= DW_OP_reg31) || |
| 140 | Op->getCode() == DW_OP_bregx || Op->getCode() == DW_OP_regx || |
| 141 | Op->getCode() == DW_OP_regval_type || |
| 142 | SubOpcode == DW_OP_LLVM_call_frame_entry_reg || |
| 143 | SubOpcode == DW_OP_LLVM_aspace_bregx) |
| 144 | if (detail::prettyPrintRegisterOp(PrintBaseTypeRef, OS, DumpOpts, |
| 145 | Opcode: Op->getCode(), Operands: Op->getRawOperands())) |
| 146 | return true; |
| 147 | |
| 148 | if (!DumpOpts.PrintRegisterOnly) { |
| 149 | for (unsigned Operand = 0; Operand < Op->getDescription().Op.size(); |
| 150 | ++Operand) { |
| 151 | unsigned Size = Op->getDescription().Op[Operand]; |
| 152 | unsigned Signed = Size & DWARFExpression::Operation::SignBit; |
| 153 | |
| 154 | if (Size == DWARFExpression::Operation::SizeSubOpLEB) { |
| 155 | assert(Operand == 0 && "DW_OP SubOp must be the first operand" ); |
| 156 | assert(SubOpcode && "DW_OP SubOp description is inconsistent" ); |
| 157 | } else if (Size == DWARFExpression::Operation::BaseTypeRef && |
| 158 | PrintBaseTypeRef) { |
| 159 | // For DW_OP_convert the operand may be 0 to indicate that conversion to |
| 160 | // the generic type should be done. The same holds for |
| 161 | // DW_OP_reinterpret, which is currently not supported. |
| 162 | if (Op->getCode() == DW_OP_convert && Op->getRawOperand(Idx: Operand) == 0) |
| 163 | OS << " 0x0" ; |
| 164 | else |
| 165 | PrintBaseTypeRef(OS, DumpOpts, Op->getRawOperands(), Operand); |
| 166 | } else if (Size == DWARFExpression::Operation::WasmLocationArg) { |
| 167 | assert(Operand == 1); |
| 168 | switch (Op->getRawOperand(Idx: 0)) { |
| 169 | case 0: |
| 170 | case 1: |
| 171 | case 2: |
| 172 | case 3: // global as uint32 |
| 173 | case 4: |
| 174 | OS << formatv(Fmt: " {0:x}" , Vals: Op->getRawOperand(Idx: Operand)); |
| 175 | break; |
| 176 | default: |
| 177 | assert(false); |
| 178 | } |
| 179 | } else if (Size == DWARFExpression::Operation::SizeBlock) { |
| 180 | uint64_t Offset = Op->getRawOperand(Idx: Operand); |
| 181 | for (unsigned i = 0; i < Op->getRawOperand(Idx: Operand - 1); ++i) |
| 182 | OS << formatv(Fmt: " {0:x2}" , |
| 183 | Vals: static_cast<uint8_t>(Expr->getData()[Offset++])); |
| 184 | } else { |
| 185 | if (Signed) |
| 186 | OS << formatv(Fmt: " {0:+d}" , Vals: (int64_t)Op->getRawOperand(Idx: Operand)); |
| 187 | else if (Op->getCode() != DW_OP_entry_value && |
| 188 | Op->getCode() != DW_OP_GNU_entry_value) |
| 189 | OS << formatv(Fmt: " {0:x}" , Vals: Op->getRawOperand(Idx: Operand)); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | void detail::printDwarfExpression(const DWARFExpression *E, raw_ostream &OS, |
| 197 | DIDumpOptions DumpOpts, |
| 198 | BaseTypeRefPrinter PrintBaseTypeRef, |
| 199 | bool IsEH) { |
| 200 | uint32_t EntryValExprSize = 0; |
| 201 | uint64_t EntryValStartOffset = 0; |
| 202 | if (E->getData().empty()) |
| 203 | OS << "<empty>" ; |
| 204 | |
| 205 | for (auto &Op : *E) { |
| 206 | DumpOpts.IsEH = IsEH; |
| 207 | if (!printOp(Op: &Op, OS, DumpOpts, Expr: E, PrintBaseTypeRef) && |
| 208 | !DumpOpts.PrintRegisterOnly) { |
| 209 | uint64_t FailOffset = Op.getEndOffset(); |
| 210 | while (FailOffset < E->getData().size()) |
| 211 | OS << formatv(Fmt: " {0:x-2}" , |
| 212 | Vals: static_cast<uint8_t>(E->getData()[FailOffset++])); |
| 213 | return; |
| 214 | } |
| 215 | if (!DumpOpts.PrintRegisterOnly) { |
| 216 | if (Op.getCode() == DW_OP_entry_value || |
| 217 | Op.getCode() == DW_OP_GNU_entry_value) { |
| 218 | OS << "(" ; |
| 219 | EntryValExprSize = Op.getRawOperand(Idx: 0); |
| 220 | EntryValStartOffset = Op.getEndOffset(); |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | if (EntryValExprSize) { |
| 225 | EntryValExprSize -= Op.getEndOffset() - EntryValStartOffset; |
| 226 | if (EntryValExprSize == 0) |
| 227 | OS << ")" ; |
| 228 | } |
| 229 | |
| 230 | if (Op.getEndOffset() < E->getData().size()) |
| 231 | OS << ", " ; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /// A user-facing string representation of a DWARF expression. This might be an |
| 237 | /// Address expression, in which case it will be implicitly dereferenced, or a |
| 238 | /// Value expression. |
| 239 | struct PrintedExpr { |
| 240 | enum ExprKind { |
| 241 | Address, |
| 242 | Value, |
| 243 | }; |
| 244 | ExprKind Kind; |
| 245 | SmallString<16> String; |
| 246 | |
| 247 | PrintedExpr(ExprKind K = Address) : Kind(K) {} |
| 248 | }; |
| 249 | |
| 250 | static bool printCompactDWARFExpr( |
| 251 | raw_ostream &OS, DWARFExpression::iterator I, |
| 252 | const DWARFExpression::iterator E, |
| 253 | std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg = |
| 254 | nullptr) { |
| 255 | SmallVector<PrintedExpr, 4> Stack; |
| 256 | |
| 257 | auto UnknownOpcode = [](raw_ostream &OS, uint8_t Opcode, |
| 258 | std::optional<unsigned> SubOpcode) -> bool { |
| 259 | // If we hit an unknown operand, we don't know its effect on the stack, |
| 260 | // so bail out on the whole expression. |
| 261 | OS << "<unknown op " << dwarf::OperationEncodingString(Encoding: Opcode) << " (" |
| 262 | << (int)Opcode; |
| 263 | if (SubOpcode) |
| 264 | OS << ") subop " << dwarf::SubOperationEncodingString(OpEncoding: Opcode, SubOpEncoding: *SubOpcode) |
| 265 | << " (" << *SubOpcode; |
| 266 | OS << ")>" ; |
| 267 | return false; |
| 268 | }; |
| 269 | |
| 270 | // Keep the diagnostic in the compact printer so every register form reports |
| 271 | // failure only after resolveRegName has tried to get a target name and decode |
| 272 | // an ASCII-packed name. |
| 273 | auto UnknownRegister = [](raw_ostream &OS, uint64_t DwarfRegNum) -> bool { |
| 274 | OS << "<unknown register " << DwarfRegNum << ">" ; |
| 275 | return false; |
| 276 | }; |
| 277 | |
| 278 | while (I != E) { |
| 279 | const DWARFExpression::Operation &Op = *I; |
| 280 | uint8_t Opcode = Op.getCode(); |
| 281 | switch (Opcode) { |
| 282 | case dwarf::DW_OP_regx: { |
| 283 | // DW_OP_regx: A register, with the register num given as an operand. |
| 284 | // Printed as the plain register name. |
| 285 | const uint64_t DwarfRegNum = Op.getRawOperand(Idx: 0); |
| 286 | std::string RegName = |
| 287 | resolveRegName(DwarfRegNum, IsEH: false, GetNameForDWARFReg); |
| 288 | if (RegName.empty()) |
| 289 | return UnknownRegister(OS, DwarfRegNum); |
| 290 | raw_svector_ostream S(Stack.emplace_back(Args: PrintedExpr::Value).String); |
| 291 | S << RegName; |
| 292 | break; |
| 293 | } |
| 294 | case dwarf::DW_OP_bregx: { |
| 295 | const uint64_t DwarfRegNum = Op.getRawOperand(Idx: 0); |
| 296 | const uint64_t Offset = Op.getRawOperand(Idx: 1); |
| 297 | std::string RegName = |
| 298 | resolveRegName(DwarfRegNum, IsEH: false, GetNameForDWARFReg); |
| 299 | if (RegName.empty()) |
| 300 | return UnknownRegister(OS, DwarfRegNum); |
| 301 | raw_svector_ostream S(Stack.emplace_back().String); |
| 302 | S << RegName; |
| 303 | if (Offset) |
| 304 | S << formatv(Fmt: "{0:+d}" , Vals: Offset); |
| 305 | break; |
| 306 | } |
| 307 | case dwarf::DW_OP_entry_value: |
| 308 | case dwarf::DW_OP_GNU_entry_value: { |
| 309 | // DW_OP_entry_value contains a sub-expression which must be rendered |
| 310 | // separately. |
| 311 | uint64_t SubExprLength = Op.getRawOperand(Idx: 0); |
| 312 | DWARFExpression::iterator SubExprEnd = I.skipBytes(Add: SubExprLength); |
| 313 | ++I; |
| 314 | |
| 315 | SmallString<16> SubExpr; |
| 316 | raw_svector_ostream SubExprOS(SubExpr); |
| 317 | // Keep the subexpression separate so we can copy its diagnostic on |
| 318 | // failure without leaving a partial entry(...) in the output. |
| 319 | if (!printCompactDWARFExpr(OS&: SubExprOS, I, E: SubExprEnd, |
| 320 | GetNameForDWARFReg)) { |
| 321 | OS << SubExprOS.str(); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | raw_svector_ostream S(Stack.emplace_back().String); |
| 326 | S << "entry(" << SubExprOS.str() << ")" ; |
| 327 | I = SubExprEnd; |
| 328 | continue; |
| 329 | } |
| 330 | case dwarf::DW_OP_stack_value: { |
| 331 | // The top stack entry should be treated as the actual value of tne |
| 332 | // variable, rather than the address of the variable in memory. |
| 333 | assert(!Stack.empty()); |
| 334 | Stack.back().Kind = PrintedExpr::Value; |
| 335 | break; |
| 336 | } |
| 337 | case dwarf::DW_OP_nop: { |
| 338 | break; |
| 339 | } |
| 340 | case dwarf::DW_OP_LLVM_user: { |
| 341 | std::optional<unsigned> SubOpcode = Op.getSubCode(); |
| 342 | if (SubOpcode == dwarf::DW_OP_LLVM_nop) |
| 343 | break; |
| 344 | return UnknownOpcode(OS, Opcode, SubOpcode); |
| 345 | } |
| 346 | default: |
| 347 | if (Opcode >= dwarf::DW_OP_reg0 && Opcode <= dwarf::DW_OP_reg31) { |
| 348 | // DW_OP_reg<N>: A register, with the register num implied by the |
| 349 | // opcode. Printed as the plain register name. |
| 350 | uint64_t DwarfRegNum = Opcode - dwarf::DW_OP_reg0; |
| 351 | std::string RegName = |
| 352 | resolveRegName(DwarfRegNum, IsEH: false, GetNameForDWARFReg); |
| 353 | if (RegName.empty()) |
| 354 | return UnknownRegister(OS, DwarfRegNum); |
| 355 | raw_svector_ostream S(Stack.emplace_back(Args: PrintedExpr::Value).String); |
| 356 | S << RegName; |
| 357 | } else if (Opcode >= dwarf::DW_OP_breg0 && |
| 358 | Opcode <= dwarf::DW_OP_breg31) { |
| 359 | int DwarfRegNum = Opcode - dwarf::DW_OP_breg0; |
| 360 | int64_t Offset = Op.getRawOperand(Idx: 0); |
| 361 | std::string RegName = |
| 362 | resolveRegName(DwarfRegNum, IsEH: false, GetNameForDWARFReg); |
| 363 | if (RegName.empty()) |
| 364 | return UnknownRegister(OS, DwarfRegNum); |
| 365 | raw_svector_ostream S(Stack.emplace_back().String); |
| 366 | S << RegName; |
| 367 | if (Offset) |
| 368 | S << formatv(Fmt: "{0:+d}" , Vals&: Offset); |
| 369 | } else { |
| 370 | return UnknownOpcode(OS, Opcode, std::nullopt); |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | ++I; |
| 375 | } |
| 376 | |
| 377 | if (Stack.size() != 1) { |
| 378 | OS << "<stack of size " << Stack.size() << ", expected 1>" ; |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | if (Stack.front().Kind == PrintedExpr::Address) |
| 383 | OS << "[" << Stack.front().String << "]" ; |
| 384 | else |
| 385 | OS << Stack.front().String; |
| 386 | |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | void printDwarfExpression(const DWARFExpression *E, raw_ostream &OS, |
| 391 | DIDumpOptions DumpOpts, bool IsEH) { |
| 392 | detail::printDwarfExpression(E, OS, DumpOpts, PrintBaseTypeRef: nullptr, IsEH); |
| 393 | } |
| 394 | |
| 395 | bool printDwarfExpressionCompact( |
| 396 | const DWARFExpression *E, raw_ostream &OS, |
| 397 | std::function<StringRef(uint64_t RegNum, bool IsEH)> GetNameForDWARFReg) { |
| 398 | return printCompactDWARFExpr(OS, I: E->begin(), E: E->end(), GetNameForDWARFReg); |
| 399 | } |
| 400 | |
| 401 | bool detail::prettyPrintRegisterOp(BaseTypeRefPrinter PrintBaseTypeRef, |
| 402 | raw_ostream &OS, DIDumpOptions DumpOpts, |
| 403 | uint8_t Opcode, |
| 404 | ArrayRef<uint64_t> Operands) { |
| 405 | uint64_t DwarfRegNum; |
| 406 | unsigned OpNum = 0; |
| 407 | |
| 408 | std::optional<unsigned> SubOpcode; |
| 409 | if (Opcode == DW_OP_LLVM_user) |
| 410 | SubOpcode = Operands[OpNum++]; |
| 411 | |
| 412 | const bool RegNumFromOperand = |
| 413 | Opcode == DW_OP_bregx || Opcode == DW_OP_regx || |
| 414 | Opcode == DW_OP_regval_type || SubOpcode == DW_OP_LLVM_aspace_bregx || |
| 415 | SubOpcode == DW_OP_LLVM_call_frame_entry_reg; |
| 416 | |
| 417 | if (RegNumFromOperand) |
| 418 | DwarfRegNum = Operands[OpNum++]; |
| 419 | else if (Opcode >= DW_OP_breg0 && Opcode < DW_OP_bregx) |
| 420 | DwarfRegNum = Opcode - DW_OP_breg0; |
| 421 | else |
| 422 | DwarfRegNum = Opcode - DW_OP_reg0; |
| 423 | |
| 424 | std::string RegName = |
| 425 | resolveRegName(DwarfRegNum, IsEH: DumpOpts.IsEH, GetNameForDWARFReg: DumpOpts.GetNameForDWARFReg); |
| 426 | |
| 427 | if (!RegName.empty()) { |
| 428 | if ((Opcode >= DW_OP_breg0 && Opcode <= DW_OP_breg31) || |
| 429 | Opcode == DW_OP_bregx || SubOpcode == DW_OP_LLVM_aspace_bregx) |
| 430 | OS << ' ' << RegName << formatv(Fmt: "{0:+d}" , Vals: int64_t(Operands[OpNum])); |
| 431 | else |
| 432 | OS << ' ' << RegName; |
| 433 | |
| 434 | if (Opcode == DW_OP_regval_type) { |
| 435 | if (PrintBaseTypeRef) |
| 436 | PrintBaseTypeRef(OS, DumpOpts, Operands, 1); |
| 437 | else |
| 438 | OS << formatv(Fmt: " <base_type ref: {0:x}>" , Vals: Operands[1]); |
| 439 | } |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | } // namespace llvm |
| 447 | |