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