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 "lldb/Expression/DWARFExpression.h" 10 11 #include <cinttypes> 12 13 #include <vector> 14 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/Value.h" 17 #include "lldb/Core/dwarf.h" 18 #include "lldb/Utility/DataEncoder.h" 19 #include "lldb/Utility/LLDBLog.h" 20 #include "lldb/Utility/Log.h" 21 #include "lldb/Utility/RegisterValue.h" 22 #include "lldb/Utility/Scalar.h" 23 #include "lldb/Utility/StreamString.h" 24 #include "lldb/Utility/VMRange.h" 25 26 #include "lldb/Host/Host.h" 27 #include "lldb/Utility/Endian.h" 28 29 #include "lldb/Symbol/Function.h" 30 31 #include "lldb/Target/ABI.h" 32 #include "lldb/Target/ExecutionContext.h" 33 #include "lldb/Target/Process.h" 34 #include "lldb/Target/RegisterContext.h" 35 #include "lldb/Target/StackFrame.h" 36 #include "lldb/Target/StackID.h" 37 #include "lldb/Target/Target.h" 38 #include "lldb/Target/Thread.h" 39 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" 40 #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 41 42 #include "Plugins/SymbolFile/DWARF/DWARFUnit.h" 43 44 using namespace lldb; 45 using namespace lldb_private; 46 using namespace lldb_private::dwarf; 47 48 // DWARFExpression constructor 49 DWARFExpression::DWARFExpression() : m_data() {} 50 51 DWARFExpression::DWARFExpression(const DataExtractor &data) : m_data(data) {} 52 53 // Destructor 54 DWARFExpression::~DWARFExpression() = default; 55 56 bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; } 57 58 void DWARFExpression::UpdateValue(uint64_t const_value, 59 lldb::offset_t const_value_byte_size, 60 uint8_t addr_byte_size) { 61 if (!const_value_byte_size) 62 return; 63 64 m_data.SetData( 65 DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size))); 66 m_data.SetByteOrder(endian::InlHostByteOrder()); 67 m_data.SetAddressByteSize(addr_byte_size); 68 } 69 70 void DWARFExpression::DumpLocation(Stream *s, lldb::DescriptionLevel level, 71 ABI *abi) const { 72 llvm::DWARFExpression(m_data.GetAsLLVM(), m_data.GetAddressByteSize()) 73 .print(s->AsRawOstream(), llvm::DIDumpOptions(), 74 abi ? &abi->GetMCRegisterInfo() : nullptr, nullptr); 75 } 76 77 RegisterKind DWARFExpression::GetRegisterKind() const { return m_reg_kind; } 78 79 void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) { 80 m_reg_kind = reg_kind; 81 } 82 83 84 static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx, 85 lldb::RegisterKind reg_kind, 86 uint32_t reg_num, Status *error_ptr, 87 Value &value) { 88 if (reg_ctx == nullptr) { 89 if (error_ptr) 90 error_ptr->SetErrorString("No register context in frame.\n"); 91 } else { 92 uint32_t native_reg = 93 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num); 94 if (native_reg == LLDB_INVALID_REGNUM) { 95 if (error_ptr) 96 error_ptr->SetErrorStringWithFormat("Unable to convert register " 97 "kind=%u reg_num=%u to a native " 98 "register number.\n", 99 reg_kind, reg_num); 100 } else { 101 const RegisterInfo *reg_info = 102 reg_ctx->GetRegisterInfoAtIndex(native_reg); 103 RegisterValue reg_value; 104 if (reg_ctx->ReadRegister(reg_info, reg_value)) { 105 if (reg_value.GetScalarValue(value.GetScalar())) { 106 value.SetValueType(Value::ValueType::Scalar); 107 value.SetContext(Value::ContextType::RegisterInfo, 108 const_cast<RegisterInfo *>(reg_info)); 109 if (error_ptr) 110 error_ptr->Clear(); 111 return true; 112 } else { 113 // If we get this error, then we need to implement a value buffer in 114 // the dwarf expression evaluation function... 115 if (error_ptr) 116 error_ptr->SetErrorStringWithFormat( 117 "register %s can't be converted to a scalar value", 118 reg_info->name); 119 } 120 } else { 121 if (error_ptr) 122 error_ptr->SetErrorStringWithFormat("register %s is not available", 123 reg_info->name); 124 } 125 } 126 } 127 return false; 128 } 129 130 /// Return the length in bytes of the set of operands for \p op. No guarantees 131 /// are made on the state of \p data after this call. 132 static offset_t GetOpcodeDataSize(const DataExtractor &data, 133 const lldb::offset_t data_offset, 134 const uint8_t op) { 135 lldb::offset_t offset = data_offset; 136 switch (op) { 137 case DW_OP_addr: 138 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3) 139 return data.GetAddressByteSize(); 140 141 // Opcodes with no arguments 142 case DW_OP_deref: // 0x06 143 case DW_OP_dup: // 0x12 144 case DW_OP_drop: // 0x13 145 case DW_OP_over: // 0x14 146 case DW_OP_swap: // 0x16 147 case DW_OP_rot: // 0x17 148 case DW_OP_xderef: // 0x18 149 case DW_OP_abs: // 0x19 150 case DW_OP_and: // 0x1a 151 case DW_OP_div: // 0x1b 152 case DW_OP_minus: // 0x1c 153 case DW_OP_mod: // 0x1d 154 case DW_OP_mul: // 0x1e 155 case DW_OP_neg: // 0x1f 156 case DW_OP_not: // 0x20 157 case DW_OP_or: // 0x21 158 case DW_OP_plus: // 0x22 159 case DW_OP_shl: // 0x24 160 case DW_OP_shr: // 0x25 161 case DW_OP_shra: // 0x26 162 case DW_OP_xor: // 0x27 163 case DW_OP_eq: // 0x29 164 case DW_OP_ge: // 0x2a 165 case DW_OP_gt: // 0x2b 166 case DW_OP_le: // 0x2c 167 case DW_OP_lt: // 0x2d 168 case DW_OP_ne: // 0x2e 169 case DW_OP_lit0: // 0x30 170 case DW_OP_lit1: // 0x31 171 case DW_OP_lit2: // 0x32 172 case DW_OP_lit3: // 0x33 173 case DW_OP_lit4: // 0x34 174 case DW_OP_lit5: // 0x35 175 case DW_OP_lit6: // 0x36 176 case DW_OP_lit7: // 0x37 177 case DW_OP_lit8: // 0x38 178 case DW_OP_lit9: // 0x39 179 case DW_OP_lit10: // 0x3A 180 case DW_OP_lit11: // 0x3B 181 case DW_OP_lit12: // 0x3C 182 case DW_OP_lit13: // 0x3D 183 case DW_OP_lit14: // 0x3E 184 case DW_OP_lit15: // 0x3F 185 case DW_OP_lit16: // 0x40 186 case DW_OP_lit17: // 0x41 187 case DW_OP_lit18: // 0x42 188 case DW_OP_lit19: // 0x43 189 case DW_OP_lit20: // 0x44 190 case DW_OP_lit21: // 0x45 191 case DW_OP_lit22: // 0x46 192 case DW_OP_lit23: // 0x47 193 case DW_OP_lit24: // 0x48 194 case DW_OP_lit25: // 0x49 195 case DW_OP_lit26: // 0x4A 196 case DW_OP_lit27: // 0x4B 197 case DW_OP_lit28: // 0x4C 198 case DW_OP_lit29: // 0x4D 199 case DW_OP_lit30: // 0x4E 200 case DW_OP_lit31: // 0x4f 201 case DW_OP_reg0: // 0x50 202 case DW_OP_reg1: // 0x51 203 case DW_OP_reg2: // 0x52 204 case DW_OP_reg3: // 0x53 205 case DW_OP_reg4: // 0x54 206 case DW_OP_reg5: // 0x55 207 case DW_OP_reg6: // 0x56 208 case DW_OP_reg7: // 0x57 209 case DW_OP_reg8: // 0x58 210 case DW_OP_reg9: // 0x59 211 case DW_OP_reg10: // 0x5A 212 case DW_OP_reg11: // 0x5B 213 case DW_OP_reg12: // 0x5C 214 case DW_OP_reg13: // 0x5D 215 case DW_OP_reg14: // 0x5E 216 case DW_OP_reg15: // 0x5F 217 case DW_OP_reg16: // 0x60 218 case DW_OP_reg17: // 0x61 219 case DW_OP_reg18: // 0x62 220 case DW_OP_reg19: // 0x63 221 case DW_OP_reg20: // 0x64 222 case DW_OP_reg21: // 0x65 223 case DW_OP_reg22: // 0x66 224 case DW_OP_reg23: // 0x67 225 case DW_OP_reg24: // 0x68 226 case DW_OP_reg25: // 0x69 227 case DW_OP_reg26: // 0x6A 228 case DW_OP_reg27: // 0x6B 229 case DW_OP_reg28: // 0x6C 230 case DW_OP_reg29: // 0x6D 231 case DW_OP_reg30: // 0x6E 232 case DW_OP_reg31: // 0x6F 233 case DW_OP_nop: // 0x96 234 case DW_OP_push_object_address: // 0x97 DWARF3 235 case DW_OP_form_tls_address: // 0x9b DWARF3 236 case DW_OP_call_frame_cfa: // 0x9c DWARF3 237 case DW_OP_stack_value: // 0x9f DWARF4 238 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension 239 return 0; 240 241 // Opcodes with a single 1 byte arguments 242 case DW_OP_const1u: // 0x08 1 1-byte constant 243 case DW_OP_const1s: // 0x09 1 1-byte constant 244 case DW_OP_pick: // 0x15 1 1-byte stack index 245 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved 246 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved 247 return 1; 248 249 // Opcodes with a single 2 byte arguments 250 case DW_OP_const2u: // 0x0a 1 2-byte constant 251 case DW_OP_const2s: // 0x0b 1 2-byte constant 252 case DW_OP_skip: // 0x2f 1 signed 2-byte constant 253 case DW_OP_bra: // 0x28 1 signed 2-byte constant 254 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3) 255 return 2; 256 257 // Opcodes with a single 4 byte arguments 258 case DW_OP_const4u: // 0x0c 1 4-byte constant 259 case DW_OP_const4s: // 0x0d 1 4-byte constant 260 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3) 261 return 4; 262 263 // Opcodes with a single 8 byte arguments 264 case DW_OP_const8u: // 0x0e 1 8-byte constant 265 case DW_OP_const8s: // 0x0f 1 8-byte constant 266 return 8; 267 268 // All opcodes that have a single ULEB (signed or unsigned) argument 269 case DW_OP_addrx: // 0xa1 1 ULEB128 index 270 case DW_OP_constu: // 0x10 1 ULEB128 constant 271 case DW_OP_consts: // 0x11 1 SLEB128 constant 272 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend 273 case DW_OP_breg0: // 0x70 1 ULEB128 register 274 case DW_OP_breg1: // 0x71 1 ULEB128 register 275 case DW_OP_breg2: // 0x72 1 ULEB128 register 276 case DW_OP_breg3: // 0x73 1 ULEB128 register 277 case DW_OP_breg4: // 0x74 1 ULEB128 register 278 case DW_OP_breg5: // 0x75 1 ULEB128 register 279 case DW_OP_breg6: // 0x76 1 ULEB128 register 280 case DW_OP_breg7: // 0x77 1 ULEB128 register 281 case DW_OP_breg8: // 0x78 1 ULEB128 register 282 case DW_OP_breg9: // 0x79 1 ULEB128 register 283 case DW_OP_breg10: // 0x7a 1 ULEB128 register 284 case DW_OP_breg11: // 0x7b 1 ULEB128 register 285 case DW_OP_breg12: // 0x7c 1 ULEB128 register 286 case DW_OP_breg13: // 0x7d 1 ULEB128 register 287 case DW_OP_breg14: // 0x7e 1 ULEB128 register 288 case DW_OP_breg15: // 0x7f 1 ULEB128 register 289 case DW_OP_breg16: // 0x80 1 ULEB128 register 290 case DW_OP_breg17: // 0x81 1 ULEB128 register 291 case DW_OP_breg18: // 0x82 1 ULEB128 register 292 case DW_OP_breg19: // 0x83 1 ULEB128 register 293 case DW_OP_breg20: // 0x84 1 ULEB128 register 294 case DW_OP_breg21: // 0x85 1 ULEB128 register 295 case DW_OP_breg22: // 0x86 1 ULEB128 register 296 case DW_OP_breg23: // 0x87 1 ULEB128 register 297 case DW_OP_breg24: // 0x88 1 ULEB128 register 298 case DW_OP_breg25: // 0x89 1 ULEB128 register 299 case DW_OP_breg26: // 0x8a 1 ULEB128 register 300 case DW_OP_breg27: // 0x8b 1 ULEB128 register 301 case DW_OP_breg28: // 0x8c 1 ULEB128 register 302 case DW_OP_breg29: // 0x8d 1 ULEB128 register 303 case DW_OP_breg30: // 0x8e 1 ULEB128 register 304 case DW_OP_breg31: // 0x8f 1 ULEB128 register 305 case DW_OP_regx: // 0x90 1 ULEB128 register 306 case DW_OP_fbreg: // 0x91 1 SLEB128 offset 307 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed 308 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index 309 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index 310 data.Skip_LEB128(&offset); 311 return offset - data_offset; 312 313 // All opcodes that have a 2 ULEB (signed or unsigned) arguments 314 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset 315 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3); 316 data.Skip_LEB128(&offset); 317 data.Skip_LEB128(&offset); 318 return offset - data_offset; 319 320 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size 321 // (DWARF4) 322 { 323 uint64_t block_len = data.Skip_LEB128(&offset); 324 offset += block_len; 325 return offset - data_offset; 326 } 327 328 case DW_OP_GNU_entry_value: 329 case DW_OP_entry_value: // 0xa3 ULEB128 size + variable-length block 330 { 331 uint64_t subexpr_len = data.GetULEB128(&offset); 332 return (offset - data_offset) + subexpr_len; 333 } 334 335 default: 336 break; 337 } 338 return LLDB_INVALID_OFFSET; 339 } 340 341 lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(const DWARFUnit *dwarf_cu, 342 uint32_t op_addr_idx, 343 bool &error) const { 344 error = false; 345 lldb::offset_t offset = 0; 346 uint32_t curr_op_addr_idx = 0; 347 while (m_data.ValidOffset(offset)) { 348 const uint8_t op = m_data.GetU8(&offset); 349 350 if (op == DW_OP_addr) { 351 const lldb::addr_t op_file_addr = m_data.GetAddress(&offset); 352 if (curr_op_addr_idx == op_addr_idx) 353 return op_file_addr; 354 ++curr_op_addr_idx; 355 } else if (op == DW_OP_GNU_addr_index || op == DW_OP_addrx) { 356 uint64_t index = m_data.GetULEB128(&offset); 357 if (curr_op_addr_idx == op_addr_idx) { 358 if (!dwarf_cu) { 359 error = true; 360 break; 361 } 362 363 return dwarf_cu->ReadAddressFromDebugAddrSection(index); 364 } 365 ++curr_op_addr_idx; 366 } else { 367 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op); 368 if (op_arg_size == LLDB_INVALID_OFFSET) { 369 error = true; 370 break; 371 } 372 offset += op_arg_size; 373 } 374 } 375 return LLDB_INVALID_ADDRESS; 376 } 377 378 bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) { 379 lldb::offset_t offset = 0; 380 while (m_data.ValidOffset(offset)) { 381 const uint8_t op = m_data.GetU8(&offset); 382 383 if (op == DW_OP_addr) { 384 const uint32_t addr_byte_size = m_data.GetAddressByteSize(); 385 // We have to make a copy of the data as we don't know if this data is 386 // from a read only memory mapped buffer, so we duplicate all of the data 387 // first, then modify it, and if all goes well, we then replace the data 388 // for this expression 389 390 // Make en encoder that contains a copy of the location expression data 391 // so we can write the address into the buffer using the correct byte 392 // order. 393 DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(), 394 m_data.GetByteOrder(), addr_byte_size); 395 396 // Replace the address in the new buffer 397 if (encoder.PutAddress(offset, file_addr) == UINT32_MAX) 398 return false; 399 400 // All went well, so now we can reset the data using a shared pointer to 401 // the heap data so "m_data" will now correctly manage the heap data. 402 m_data.SetData(encoder.GetDataBuffer()); 403 return true; 404 } else { 405 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op); 406 if (op_arg_size == LLDB_INVALID_OFFSET) 407 break; 408 offset += op_arg_size; 409 } 410 } 411 return false; 412 } 413 414 bool DWARFExpression::ContainsThreadLocalStorage() const { 415 lldb::offset_t offset = 0; 416 while (m_data.ValidOffset(offset)) { 417 const uint8_t op = m_data.GetU8(&offset); 418 419 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address) 420 return true; 421 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op); 422 if (op_arg_size == LLDB_INVALID_OFFSET) 423 return false; 424 offset += op_arg_size; 425 } 426 return false; 427 } 428 bool DWARFExpression::LinkThreadLocalStorage( 429 std::function<lldb::addr_t(lldb::addr_t file_addr)> const 430 &link_address_callback) { 431 const uint32_t addr_byte_size = m_data.GetAddressByteSize(); 432 // We have to make a copy of the data as we don't know if this data is from a 433 // read only memory mapped buffer, so we duplicate all of the data first, 434 // then modify it, and if all goes well, we then replace the data for this 435 // expression. 436 // Make en encoder that contains a copy of the location expression data so we 437 // can write the address into the buffer using the correct byte order. 438 DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(), 439 m_data.GetByteOrder(), addr_byte_size); 440 441 lldb::offset_t offset = 0; 442 lldb::offset_t const_offset = 0; 443 lldb::addr_t const_value = 0; 444 size_t const_byte_size = 0; 445 while (m_data.ValidOffset(offset)) { 446 const uint8_t op = m_data.GetU8(&offset); 447 448 bool decoded_data = false; 449 switch (op) { 450 case DW_OP_const4u: 451 // Remember the const offset in case we later have a 452 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address 453 const_offset = offset; 454 const_value = m_data.GetU32(&offset); 455 decoded_data = true; 456 const_byte_size = 4; 457 break; 458 459 case DW_OP_const8u: 460 // Remember the const offset in case we later have a 461 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address 462 const_offset = offset; 463 const_value = m_data.GetU64(&offset); 464 decoded_data = true; 465 const_byte_size = 8; 466 break; 467 468 case DW_OP_form_tls_address: 469 case DW_OP_GNU_push_tls_address: 470 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded 471 // by a file address on the stack. We assume that DW_OP_const4u or 472 // DW_OP_const8u is used for these values, and we check that the last 473 // opcode we got before either of these was DW_OP_const4u or 474 // DW_OP_const8u. If so, then we can link the value accodingly. For 475 // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file 476 // address of a structure that contains a function pointer, the pthread 477 // key and the offset into the data pointed to by the pthread key. So we 478 // must link this address and also set the module of this expression to 479 // the new_module_sp so we can resolve the file address correctly 480 if (const_byte_size > 0) { 481 lldb::addr_t linked_file_addr = link_address_callback(const_value); 482 if (linked_file_addr == LLDB_INVALID_ADDRESS) 483 return false; 484 // Replace the address in the new buffer 485 if (encoder.PutUnsigned(const_offset, const_byte_size, 486 linked_file_addr) == UINT32_MAX) 487 return false; 488 } 489 break; 490 491 default: 492 const_offset = 0; 493 const_value = 0; 494 const_byte_size = 0; 495 break; 496 } 497 498 if (!decoded_data) { 499 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op); 500 if (op_arg_size == LLDB_INVALID_OFFSET) 501 return false; 502 else 503 offset += op_arg_size; 504 } 505 } 506 507 m_data.SetData(encoder.GetDataBuffer()); 508 return true; 509 } 510 511 static bool Evaluate_DW_OP_entry_value(std::vector<Value> &stack, 512 ExecutionContext *exe_ctx, 513 RegisterContext *reg_ctx, 514 const DataExtractor &opcodes, 515 lldb::offset_t &opcode_offset, 516 Status *error_ptr, Log *log) { 517 // DW_OP_entry_value(sub-expr) describes the location a variable had upon 518 // function entry: this variable location is presumed to be optimized out at 519 // the current PC value. The caller of the function may have call site 520 // information that describes an alternate location for the variable (e.g. a 521 // constant literal, or a spilled stack value) in the parent frame. 522 // 523 // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative): 524 // 525 // void child(int &sink, int x) { 526 // ... 527 // /* "x" gets optimized out. */ 528 // 529 // /* The location of "x" here is: DW_OP_entry_value($reg2). */ 530 // ++sink; 531 // } 532 // 533 // void parent() { 534 // int sink; 535 // 536 // /* 537 // * The callsite information emitted here is: 538 // * 539 // * DW_TAG_call_site 540 // * DW_AT_return_pc ... (for "child(sink, 123);") 541 // * DW_TAG_call_site_parameter (for "sink") 542 // * DW_AT_location ($reg1) 543 // * DW_AT_call_value ($SP - 8) 544 // * DW_TAG_call_site_parameter (for "x") 545 // * DW_AT_location ($reg2) 546 // * DW_AT_call_value ($literal 123) 547 // * 548 // * DW_TAG_call_site 549 // * DW_AT_return_pc ... (for "child(sink, 456);") 550 // * ... 551 // */ 552 // child(sink, 123); 553 // child(sink, 456); 554 // } 555 // 556 // When the program stops at "++sink" within `child`, the debugger determines 557 // the call site by analyzing the return address. Once the call site is found, 558 // the debugger determines which parameter is referenced by DW_OP_entry_value 559 // and evaluates the corresponding location for that parameter in `parent`. 560 561 // 1. Find the function which pushed the current frame onto the stack. 562 if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) { 563 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no exe/reg context"); 564 return false; 565 } 566 567 StackFrame *current_frame = exe_ctx->GetFramePtr(); 568 Thread *thread = exe_ctx->GetThreadPtr(); 569 if (!current_frame || !thread) { 570 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current frame/thread"); 571 return false; 572 } 573 574 Target &target = exe_ctx->GetTargetRef(); 575 StackFrameSP parent_frame = nullptr; 576 addr_t return_pc = LLDB_INVALID_ADDRESS; 577 uint32_t current_frame_idx = current_frame->GetFrameIndex(); 578 uint32_t num_frames = thread->GetStackFrameCount(); 579 for (uint32_t parent_frame_idx = current_frame_idx + 1; 580 parent_frame_idx < num_frames; ++parent_frame_idx) { 581 parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx); 582 // Require a valid sequence of frames. 583 if (!parent_frame) 584 break; 585 586 // Record the first valid return address, even if this is an inlined frame, 587 // in order to look up the associated call edge in the first non-inlined 588 // parent frame. 589 if (return_pc == LLDB_INVALID_ADDRESS) { 590 return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target); 591 LLDB_LOG(log, 592 "Evaluate_DW_OP_entry_value: immediate ancestor with pc = {0:x}", 593 return_pc); 594 } 595 596 // If we've found an inlined frame, skip it (these have no call site 597 // parameters). 598 if (parent_frame->IsInlined()) 599 continue; 600 601 // We've found the first non-inlined parent frame. 602 break; 603 } 604 if (!parent_frame || !parent_frame->GetRegisterContext()) { 605 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent frame with reg ctx"); 606 return false; 607 } 608 609 Function *parent_func = 610 parent_frame->GetSymbolContext(eSymbolContextFunction).function; 611 if (!parent_func) { 612 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent function"); 613 return false; 614 } 615 616 // 2. Find the call edge in the parent function responsible for creating the 617 // current activation. 618 Function *current_func = 619 current_frame->GetSymbolContext(eSymbolContextFunction).function; 620 if (!current_func) { 621 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current function"); 622 return false; 623 } 624 625 CallEdge *call_edge = nullptr; 626 ModuleList &modlist = target.GetImages(); 627 ExecutionContext parent_exe_ctx = *exe_ctx; 628 parent_exe_ctx.SetFrameSP(parent_frame); 629 if (!parent_frame->IsArtificial()) { 630 // If the parent frame is not artificial, the current activation may be 631 // produced by an ambiguous tail call. In this case, refuse to proceed. 632 call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target); 633 if (!call_edge) { 634 LLDB_LOG(log, 635 "Evaluate_DW_OP_entry_value: no call edge for retn-pc = {0:x} " 636 "in parent frame {1}", 637 return_pc, parent_func->GetName()); 638 return false; 639 } 640 Function *callee_func = call_edge->GetCallee(modlist, parent_exe_ctx); 641 if (callee_func != current_func) { 642 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: ambiguous call sequence, " 643 "can't find real parent frame"); 644 return false; 645 } 646 } else { 647 // The StackFrameList solver machinery has deduced that an unambiguous tail 648 // call sequence that produced the current activation. The first edge in 649 // the parent that points to the current function must be valid. 650 for (auto &edge : parent_func->GetTailCallingEdges()) { 651 if (edge->GetCallee(modlist, parent_exe_ctx) == current_func) { 652 call_edge = edge.get(); 653 break; 654 } 655 } 656 } 657 if (!call_edge) { 658 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no unambiguous edge from parent " 659 "to current function"); 660 return false; 661 } 662 663 // 3. Attempt to locate the DW_OP_entry_value expression in the set of 664 // available call site parameters. If found, evaluate the corresponding 665 // parameter in the context of the parent frame. 666 const uint32_t subexpr_len = opcodes.GetULEB128(&opcode_offset); 667 const void *subexpr_data = opcodes.GetData(&opcode_offset, subexpr_len); 668 if (!subexpr_data) { 669 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: subexpr could not be read"); 670 return false; 671 } 672 673 const CallSiteParameter *matched_param = nullptr; 674 for (const CallSiteParameter ¶m : call_edge->GetCallSiteParameters()) { 675 DataExtractor param_subexpr_extractor; 676 if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor)) 677 continue; 678 lldb::offset_t param_subexpr_offset = 0; 679 const void *param_subexpr_data = 680 param_subexpr_extractor.GetData(¶m_subexpr_offset, subexpr_len); 681 if (!param_subexpr_data || 682 param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0) 683 continue; 684 685 // At this point, the DW_OP_entry_value sub-expression and the callee-side 686 // expression in the call site parameter are known to have the same length. 687 // Check whether they are equal. 688 // 689 // Note that an equality check is sufficient: the contents of the 690 // DW_OP_entry_value subexpression are only used to identify the right call 691 // site parameter in the parent, and do not require any special handling. 692 if (memcmp(subexpr_data, param_subexpr_data, subexpr_len) == 0) { 693 matched_param = ¶m; 694 break; 695 } 696 } 697 if (!matched_param) { 698 LLDB_LOG(log, 699 "Evaluate_DW_OP_entry_value: no matching call site param found"); 700 return false; 701 } 702 703 // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value 704 // subexpresion whenever llvm does. 705 Value result; 706 const DWARFExpressionList ¶m_expr = matched_param->LocationInCaller; 707 if (!param_expr.Evaluate(&parent_exe_ctx, 708 parent_frame->GetRegisterContext().get(), 709 /*initial_value_ptr=*/nullptr, 710 /*object_address_ptr=*/nullptr, result, error_ptr)) { 711 LLDB_LOG(log, 712 "Evaluate_DW_OP_entry_value: call site param evaluation failed"); 713 return false; 714 } 715 716 stack.push_back(result); 717 return true; 718 } 719 720 namespace { 721 /// The location description kinds described by the DWARF v5 722 /// specification. Composite locations are handled out-of-band and 723 /// thus aren't part of the enum. 724 enum LocationDescriptionKind { 725 Empty, 726 Memory, 727 Register, 728 Implicit 729 /* Composite*/ 730 }; 731 /// Adjust value's ValueType according to the kind of location description. 732 void UpdateValueTypeFromLocationDescription(Log *log, const DWARFUnit *dwarf_cu, 733 LocationDescriptionKind kind, 734 Value *value = nullptr) { 735 // Note that this function is conflating DWARF expressions with 736 // DWARF location descriptions. Perhaps it would be better to define 737 // a wrapper for DWARFExpresssion::Eval() that deals with DWARF 738 // location descriptions (which consist of one or more DWARF 739 // expressions). But doing this would mean we'd also need factor the 740 // handling of DW_OP_(bit_)piece out of this function. 741 if (dwarf_cu && dwarf_cu->GetVersion() >= 4) { 742 const char *log_msg = "DWARF location description kind: %s"; 743 switch (kind) { 744 case Empty: 745 LLDB_LOGF(log, log_msg, "Empty"); 746 break; 747 case Memory: 748 LLDB_LOGF(log, log_msg, "Memory"); 749 if (value->GetValueType() == Value::ValueType::Scalar) 750 value->SetValueType(Value::ValueType::LoadAddress); 751 break; 752 case Register: 753 LLDB_LOGF(log, log_msg, "Register"); 754 value->SetValueType(Value::ValueType::Scalar); 755 break; 756 case Implicit: 757 LLDB_LOGF(log, log_msg, "Implicit"); 758 if (value->GetValueType() == Value::ValueType::LoadAddress) 759 value->SetValueType(Value::ValueType::Scalar); 760 break; 761 } 762 } 763 } 764 } // namespace 765 766 /// Helper function to move common code used to resolve a file address and turn 767 /// into a load address. 768 /// 769 /// \param exe_ctx Pointer to the execution context 770 /// \param module_sp shared_ptr contains the module if we have one 771 /// \param error_ptr pointer to Status object if we have one 772 /// \param dw_op_type C-style string used to vary the error output 773 /// \param file_addr the file address we are trying to resolve and turn into a 774 /// load address 775 /// \param so_addr out parameter, will be set to load addresss or section offset 776 /// \param check_sectionoffset bool which determines if having a section offset 777 /// but not a load address is considerd a success 778 /// \returns llvm::Optional containing the load address if resolving and getting 779 /// the load address succeed or an empty Optinal otherwise. If 780 /// check_sectionoffset is true we consider LLDB_INVALID_ADDRESS a 781 /// success if so_addr.IsSectionOffset() is true. 782 static llvm::Optional<lldb::addr_t> 783 ResolveLoadAddress(ExecutionContext *exe_ctx, lldb::ModuleSP &module_sp, 784 Status *error_ptr, const char *dw_op_type, 785 lldb::addr_t file_addr, Address &so_addr, 786 bool check_sectionoffset = false) { 787 if (!module_sp) { 788 if (error_ptr) 789 error_ptr->SetErrorStringWithFormat( 790 "need module to resolve file address for %s", dw_op_type); 791 return {}; 792 } 793 794 if (!module_sp->ResolveFileAddress(file_addr, so_addr)) { 795 if (error_ptr) 796 error_ptr->SetErrorString("failed to resolve file address in module"); 797 return {}; 798 } 799 800 addr_t load_addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr()); 801 802 if (load_addr == LLDB_INVALID_ADDRESS && 803 (check_sectionoffset && !so_addr.IsSectionOffset())) { 804 if (error_ptr) 805 error_ptr->SetErrorString("failed to resolve load address"); 806 return {}; 807 } 808 809 return load_addr; 810 } 811 812 /// Helper function to move common code used to load sized data from a uint8_t 813 /// buffer. 814 /// 815 /// \param addr_bytes uint8_t buffer containg raw data 816 /// \param size_addr_bytes how large is the underlying raw data 817 /// \param byte_order what is the byter order of the underlyig data 818 /// \param size How much of the underlying data we want to use 819 /// \return The underlying data converted into a Scalar 820 static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes, 821 size_t size_addr_bytes, 822 ByteOrder byte_order, size_t size) { 823 DataExtractor addr_data(addr_bytes, size_addr_bytes, byte_order, size); 824 825 lldb::offset_t addr_data_offset = 0; 826 if (size <= 8) 827 return addr_data.GetMaxU64(&addr_data_offset, size); 828 else 829 return addr_data.GetAddress(&addr_data_offset); 830 } 831 832 bool DWARFExpression::Evaluate( 833 ExecutionContext *exe_ctx, RegisterContext *reg_ctx, 834 lldb::ModuleSP module_sp, const DataExtractor &opcodes, 835 const DWARFUnit *dwarf_cu, const lldb::RegisterKind reg_kind, 836 const Value *initial_value_ptr, const Value *object_address_ptr, 837 Value &result, Status *error_ptr) { 838 839 if (opcodes.GetByteSize() == 0) { 840 if (error_ptr) 841 error_ptr->SetErrorString( 842 "no location, value may have been optimized out"); 843 return false; 844 } 845 std::vector<Value> stack; 846 847 Process *process = nullptr; 848 StackFrame *frame = nullptr; 849 850 if (exe_ctx) { 851 process = exe_ctx->GetProcessPtr(); 852 frame = exe_ctx->GetFramePtr(); 853 } 854 if (reg_ctx == nullptr && frame) 855 reg_ctx = frame->GetRegisterContext().get(); 856 857 if (initial_value_ptr) 858 stack.push_back(*initial_value_ptr); 859 860 lldb::offset_t offset = 0; 861 Value tmp; 862 uint32_t reg_num; 863 864 /// Insertion point for evaluating multi-piece expression. 865 uint64_t op_piece_offset = 0; 866 Value pieces; // Used for DW_OP_piece 867 868 Log *log = GetLog(LLDBLog::Expressions); 869 // A generic type is "an integral type that has the size of an address and an 870 // unspecified signedness". For now, just use the signedness of the operand. 871 // TODO: Implement a real typed stack, and store the genericness of the value 872 // there. 873 auto to_generic = [&](auto v) { 874 bool is_signed = std::is_signed<decltype(v)>::value; 875 return Scalar(llvm::APSInt( 876 llvm::APInt(8 * opcodes.GetAddressByteSize(), v, is_signed), 877 !is_signed)); 878 }; 879 880 // The default kind is a memory location. This is updated by any 881 // operation that changes this, such as DW_OP_stack_value, and reset 882 // by composition operations like DW_OP_piece. 883 LocationDescriptionKind dwarf4_location_description_kind = Memory; 884 885 while (opcodes.ValidOffset(offset)) { 886 const lldb::offset_t op_offset = offset; 887 const uint8_t op = opcodes.GetU8(&offset); 888 889 if (log && log->GetVerbose()) { 890 size_t count = stack.size(); 891 LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:", 892 (uint64_t)count); 893 for (size_t i = 0; i < count; ++i) { 894 StreamString new_value; 895 new_value.Printf("[%" PRIu64 "]", (uint64_t)i); 896 stack[i].Dump(&new_value); 897 LLDB_LOGF(log, " %s", new_value.GetData()); 898 } 899 LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset, 900 DW_OP_value_to_name(op)); 901 } 902 903 switch (op) { 904 // The DW_OP_addr operation has a single operand that encodes a machine 905 // address and whose size is the size of an address on the target machine. 906 case DW_OP_addr: 907 stack.push_back(Scalar(opcodes.GetAddress(&offset))); 908 stack.back().SetValueType(Value::ValueType::FileAddress); 909 // Convert the file address to a load address, so subsequent 910 // DWARF operators can operate on it. 911 if (frame) 912 stack.back().ConvertToLoadAddress(module_sp.get(), 913 frame->CalculateTarget().get()); 914 break; 915 916 // The DW_OP_addr_sect_offset4 is used for any location expressions in 917 // shared libraries that have a location like: 918 // DW_OP_addr(0x1000) 919 // If this address resides in a shared library, then this virtual address 920 // won't make sense when it is evaluated in the context of a running 921 // process where shared libraries have been slid. To account for this, this 922 // new address type where we can store the section pointer and a 4 byte 923 // offset. 924 // case DW_OP_addr_sect_offset4: 925 // { 926 // result_type = eResultTypeFileAddress; 927 // lldb::Section *sect = (lldb::Section 928 // *)opcodes.GetMaxU64(&offset, sizeof(void *)); 929 // lldb::addr_t sect_offset = opcodes.GetU32(&offset); 930 // 931 // Address so_addr (sect, sect_offset); 932 // lldb::addr_t load_addr = so_addr.GetLoadAddress(); 933 // if (load_addr != LLDB_INVALID_ADDRESS) 934 // { 935 // // We successfully resolve a file address to a load 936 // // address. 937 // stack.push_back(load_addr); 938 // break; 939 // } 940 // else 941 // { 942 // // We were able 943 // if (error_ptr) 944 // error_ptr->SetErrorStringWithFormat ("Section %s in 945 // %s is not currently loaded.\n", 946 // sect->GetName().AsCString(), 947 // sect->GetModule()->GetFileSpec().GetFilename().AsCString()); 948 // return false; 949 // } 950 // } 951 // break; 952 953 // OPCODE: DW_OP_deref 954 // OPERANDS: none 955 // DESCRIPTION: Pops the top stack entry and treats it as an address. 956 // The value retrieved from that address is pushed. The size of the data 957 // retrieved from the dereferenced address is the size of an address on the 958 // target machine. 959 case DW_OP_deref: { 960 if (stack.empty()) { 961 if (error_ptr) 962 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref."); 963 return false; 964 } 965 Value::ValueType value_type = stack.back().GetValueType(); 966 switch (value_type) { 967 case Value::ValueType::HostAddress: { 968 void *src = (void *)stack.back().GetScalar().ULongLong(); 969 intptr_t ptr; 970 ::memcpy(&ptr, src, sizeof(void *)); 971 stack.back().GetScalar() = ptr; 972 stack.back().ClearContext(); 973 } break; 974 case Value::ValueType::FileAddress: { 975 auto file_addr = stack.back().GetScalar().ULongLong( 976 LLDB_INVALID_ADDRESS); 977 978 Address so_addr; 979 auto maybe_load_addr = ResolveLoadAddress( 980 exe_ctx, module_sp, error_ptr, "DW_OP_deref", file_addr, so_addr); 981 982 if (!maybe_load_addr) 983 return false; 984 985 stack.back().GetScalar() = *maybe_load_addr; 986 // Fall through to load address promotion code below. 987 } LLVM_FALLTHROUGH; 988 case Value::ValueType::Scalar: 989 // Promote Scalar to LoadAddress and fall through. 990 stack.back().SetValueType(Value::ValueType::LoadAddress); 991 LLVM_FALLTHROUGH; 992 case Value::ValueType::LoadAddress: 993 if (exe_ctx) { 994 if (process) { 995 lldb::addr_t pointer_addr = 996 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 997 Status error; 998 lldb::addr_t pointer_value = 999 process->ReadPointerFromMemory(pointer_addr, error); 1000 if (pointer_value != LLDB_INVALID_ADDRESS) { 1001 if (ABISP abi_sp = process->GetABI()) 1002 pointer_value = abi_sp->FixCodeAddress(pointer_value); 1003 stack.back().GetScalar() = pointer_value; 1004 stack.back().ClearContext(); 1005 } else { 1006 if (error_ptr) 1007 error_ptr->SetErrorStringWithFormat( 1008 "Failed to dereference pointer from 0x%" PRIx64 1009 " for DW_OP_deref: %s\n", 1010 pointer_addr, error.AsCString()); 1011 return false; 1012 } 1013 } else { 1014 if (error_ptr) 1015 error_ptr->SetErrorString("NULL process for DW_OP_deref.\n"); 1016 return false; 1017 } 1018 } else { 1019 if (error_ptr) 1020 error_ptr->SetErrorString( 1021 "NULL execution context for DW_OP_deref.\n"); 1022 return false; 1023 } 1024 break; 1025 1026 case Value::ValueType::Invalid: 1027 if (error_ptr) 1028 error_ptr->SetErrorString("Invalid value type for DW_OP_deref.\n"); 1029 return false; 1030 } 1031 1032 } break; 1033 1034 // OPCODE: DW_OP_deref_size 1035 // OPERANDS: 1 1036 // 1 - uint8_t that specifies the size of the data to dereference. 1037 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top 1038 // stack entry and treats it as an address. The value retrieved from that 1039 // address is pushed. In the DW_OP_deref_size operation, however, the size 1040 // in bytes of the data retrieved from the dereferenced address is 1041 // specified by the single operand. This operand is a 1-byte unsigned 1042 // integral constant whose value may not be larger than the size of an 1043 // address on the target machine. The data retrieved is zero extended to 1044 // the size of an address on the target machine before being pushed on the 1045 // expression stack. 1046 case DW_OP_deref_size: { 1047 if (stack.empty()) { 1048 if (error_ptr) 1049 error_ptr->SetErrorString( 1050 "Expression stack empty for DW_OP_deref_size."); 1051 return false; 1052 } 1053 uint8_t size = opcodes.GetU8(&offset); 1054 Value::ValueType value_type = stack.back().GetValueType(); 1055 switch (value_type) { 1056 case Value::ValueType::HostAddress: { 1057 void *src = (void *)stack.back().GetScalar().ULongLong(); 1058 intptr_t ptr; 1059 ::memcpy(&ptr, src, sizeof(void *)); 1060 // I can't decide whether the size operand should apply to the bytes in 1061 // their 1062 // lldb-host endianness or the target endianness.. I doubt this'll ever 1063 // come up but I'll opt for assuming big endian regardless. 1064 switch (size) { 1065 case 1: 1066 ptr = ptr & 0xff; 1067 break; 1068 case 2: 1069 ptr = ptr & 0xffff; 1070 break; 1071 case 3: 1072 ptr = ptr & 0xffffff; 1073 break; 1074 case 4: 1075 ptr = ptr & 0xffffffff; 1076 break; 1077 // the casts are added to work around the case where intptr_t is a 32 1078 // bit quantity; 1079 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this 1080 // program. 1081 case 5: 1082 ptr = (intptr_t)ptr & 0xffffffffffULL; 1083 break; 1084 case 6: 1085 ptr = (intptr_t)ptr & 0xffffffffffffULL; 1086 break; 1087 case 7: 1088 ptr = (intptr_t)ptr & 0xffffffffffffffULL; 1089 break; 1090 default: 1091 break; 1092 } 1093 stack.back().GetScalar() = ptr; 1094 stack.back().ClearContext(); 1095 } break; 1096 case Value::ValueType::FileAddress: { 1097 auto file_addr = 1098 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1099 Address so_addr; 1100 auto maybe_load_addr = 1101 ResolveLoadAddress(exe_ctx, module_sp, error_ptr, 1102 "DW_OP_deref_size", file_addr, so_addr, 1103 /*check_sectionoffset=*/true); 1104 1105 if (!maybe_load_addr) 1106 return false; 1107 1108 addr_t load_addr = *maybe_load_addr; 1109 1110 if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) { 1111 uint8_t addr_bytes[8]; 1112 Status error; 1113 1114 if (exe_ctx->GetTargetRef().ReadMemory( 1115 so_addr, &addr_bytes, size, error, 1116 /*force_live_memory=*/false) == size) { 1117 ObjectFile *objfile = module_sp->GetObjectFile(); 1118 1119 stack.back().GetScalar() = DerefSizeExtractDataHelper( 1120 addr_bytes, size, objfile->GetByteOrder(), size); 1121 stack.back().ClearContext(); 1122 break; 1123 } else { 1124 if (error_ptr) 1125 error_ptr->SetErrorStringWithFormat( 1126 "Failed to dereference pointer for for DW_OP_deref_size: " 1127 "%s\n", 1128 error.AsCString()); 1129 return false; 1130 } 1131 } 1132 stack.back().GetScalar() = load_addr; 1133 // Fall through to load address promotion code below. 1134 } 1135 1136 LLVM_FALLTHROUGH; 1137 case Value::ValueType::Scalar: 1138 case Value::ValueType::LoadAddress: 1139 if (exe_ctx) { 1140 if (process) { 1141 lldb::addr_t pointer_addr = 1142 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1143 uint8_t addr_bytes[sizeof(lldb::addr_t)]; 1144 Status error; 1145 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) == 1146 size) { 1147 1148 stack.back().GetScalar() = 1149 DerefSizeExtractDataHelper(addr_bytes, sizeof(addr_bytes), 1150 process->GetByteOrder(), size); 1151 stack.back().ClearContext(); 1152 } else { 1153 if (error_ptr) 1154 error_ptr->SetErrorStringWithFormat( 1155 "Failed to dereference pointer from 0x%" PRIx64 1156 " for DW_OP_deref: %s\n", 1157 pointer_addr, error.AsCString()); 1158 return false; 1159 } 1160 } else { 1161 if (error_ptr) 1162 error_ptr->SetErrorString("NULL process for DW_OP_deref_size.\n"); 1163 return false; 1164 } 1165 } else { 1166 if (error_ptr) 1167 error_ptr->SetErrorString( 1168 "NULL execution context for DW_OP_deref_size.\n"); 1169 return false; 1170 } 1171 break; 1172 1173 case Value::ValueType::Invalid: 1174 if (error_ptr) 1175 error_ptr->SetErrorString("Invalid value for DW_OP_deref_size.\n"); 1176 return false; 1177 } 1178 1179 } break; 1180 1181 // OPCODE: DW_OP_xderef_size 1182 // OPERANDS: 1 1183 // 1 - uint8_t that specifies the size of the data to dereference. 1184 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at 1185 // the top of the stack is treated as an address. The second stack entry is 1186 // treated as an "address space identifier" for those architectures that 1187 // support multiple address spaces. The top two stack elements are popped, 1188 // a data item is retrieved through an implementation-defined address 1189 // calculation and pushed as the new stack top. In the DW_OP_xderef_size 1190 // operation, however, the size in bytes of the data retrieved from the 1191 // dereferenced address is specified by the single operand. This operand is 1192 // a 1-byte unsigned integral constant whose value may not be larger than 1193 // the size of an address on the target machine. The data retrieved is zero 1194 // extended to the size of an address on the target machine before being 1195 // pushed on the expression stack. 1196 case DW_OP_xderef_size: 1197 if (error_ptr) 1198 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size."); 1199 return false; 1200 // OPCODE: DW_OP_xderef 1201 // OPERANDS: none 1202 // DESCRIPTION: Provides an extended dereference mechanism. The entry at 1203 // the top of the stack is treated as an address. The second stack entry is 1204 // treated as an "address space identifier" for those architectures that 1205 // support multiple address spaces. The top two stack elements are popped, 1206 // a data item is retrieved through an implementation-defined address 1207 // calculation and pushed as the new stack top. The size of the data 1208 // retrieved from the dereferenced address is the size of an address on the 1209 // target machine. 1210 case DW_OP_xderef: 1211 if (error_ptr) 1212 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef."); 1213 return false; 1214 1215 // All DW_OP_constXXX opcodes have a single operand as noted below: 1216 // 1217 // Opcode Operand 1 1218 // DW_OP_const1u 1-byte unsigned integer constant 1219 // DW_OP_const1s 1-byte signed integer constant 1220 // DW_OP_const2u 2-byte unsigned integer constant 1221 // DW_OP_const2s 2-byte signed integer constant 1222 // DW_OP_const4u 4-byte unsigned integer constant 1223 // DW_OP_const4s 4-byte signed integer constant 1224 // DW_OP_const8u 8-byte unsigned integer constant 1225 // DW_OP_const8s 8-byte signed integer constant 1226 // DW_OP_constu unsigned LEB128 integer constant 1227 // DW_OP_consts signed LEB128 integer constant 1228 case DW_OP_const1u: 1229 stack.push_back(to_generic(opcodes.GetU8(&offset))); 1230 break; 1231 case DW_OP_const1s: 1232 stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset))); 1233 break; 1234 case DW_OP_const2u: 1235 stack.push_back(to_generic(opcodes.GetU16(&offset))); 1236 break; 1237 case DW_OP_const2s: 1238 stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset))); 1239 break; 1240 case DW_OP_const4u: 1241 stack.push_back(to_generic(opcodes.GetU32(&offset))); 1242 break; 1243 case DW_OP_const4s: 1244 stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset))); 1245 break; 1246 case DW_OP_const8u: 1247 stack.push_back(to_generic(opcodes.GetU64(&offset))); 1248 break; 1249 case DW_OP_const8s: 1250 stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset))); 1251 break; 1252 // These should also use to_generic, but we can't do that due to a 1253 // producer-side bug in llvm. See llvm.org/pr48087. 1254 case DW_OP_constu: 1255 stack.push_back(Scalar(opcodes.GetULEB128(&offset))); 1256 break; 1257 case DW_OP_consts: 1258 stack.push_back(Scalar(opcodes.GetSLEB128(&offset))); 1259 break; 1260 1261 // OPCODE: DW_OP_dup 1262 // OPERANDS: none 1263 // DESCRIPTION: duplicates the value at the top of the stack 1264 case DW_OP_dup: 1265 if (stack.empty()) { 1266 if (error_ptr) 1267 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup."); 1268 return false; 1269 } else 1270 stack.push_back(stack.back()); 1271 break; 1272 1273 // OPCODE: DW_OP_drop 1274 // OPERANDS: none 1275 // DESCRIPTION: pops the value at the top of the stack 1276 case DW_OP_drop: 1277 if (stack.empty()) { 1278 if (error_ptr) 1279 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop."); 1280 return false; 1281 } else 1282 stack.pop_back(); 1283 break; 1284 1285 // OPCODE: DW_OP_over 1286 // OPERANDS: none 1287 // DESCRIPTION: Duplicates the entry currently second in the stack at 1288 // the top of the stack. 1289 case DW_OP_over: 1290 if (stack.size() < 2) { 1291 if (error_ptr) 1292 error_ptr->SetErrorString( 1293 "Expression stack needs at least 2 items for DW_OP_over."); 1294 return false; 1295 } else 1296 stack.push_back(stack[stack.size() - 2]); 1297 break; 1298 1299 // OPCODE: DW_OP_pick 1300 // OPERANDS: uint8_t index into the current stack 1301 // DESCRIPTION: The stack entry with the specified index (0 through 255, 1302 // inclusive) is pushed on the stack 1303 case DW_OP_pick: { 1304 uint8_t pick_idx = opcodes.GetU8(&offset); 1305 if (pick_idx < stack.size()) 1306 stack.push_back(stack[stack.size() - 1 - pick_idx]); 1307 else { 1308 if (error_ptr) 1309 error_ptr->SetErrorStringWithFormat( 1310 "Index %u out of range for DW_OP_pick.\n", pick_idx); 1311 return false; 1312 } 1313 } break; 1314 1315 // OPCODE: DW_OP_swap 1316 // OPERANDS: none 1317 // DESCRIPTION: swaps the top two stack entries. The entry at the top 1318 // of the stack becomes the second stack entry, and the second entry 1319 // becomes the top of the stack 1320 case DW_OP_swap: 1321 if (stack.size() < 2) { 1322 if (error_ptr) 1323 error_ptr->SetErrorString( 1324 "Expression stack needs at least 2 items for DW_OP_swap."); 1325 return false; 1326 } else { 1327 tmp = stack.back(); 1328 stack.back() = stack[stack.size() - 2]; 1329 stack[stack.size() - 2] = tmp; 1330 } 1331 break; 1332 1333 // OPCODE: DW_OP_rot 1334 // OPERANDS: none 1335 // DESCRIPTION: Rotates the first three stack entries. The entry at 1336 // the top of the stack becomes the third stack entry, the second entry 1337 // becomes the top of the stack, and the third entry becomes the second 1338 // entry. 1339 case DW_OP_rot: 1340 if (stack.size() < 3) { 1341 if (error_ptr) 1342 error_ptr->SetErrorString( 1343 "Expression stack needs at least 3 items for DW_OP_rot."); 1344 return false; 1345 } else { 1346 size_t last_idx = stack.size() - 1; 1347 Value old_top = stack[last_idx]; 1348 stack[last_idx] = stack[last_idx - 1]; 1349 stack[last_idx - 1] = stack[last_idx - 2]; 1350 stack[last_idx - 2] = old_top; 1351 } 1352 break; 1353 1354 // OPCODE: DW_OP_abs 1355 // OPERANDS: none 1356 // DESCRIPTION: pops the top stack entry, interprets it as a signed 1357 // value and pushes its absolute value. If the absolute value can not be 1358 // represented, the result is undefined. 1359 case DW_OP_abs: 1360 if (stack.empty()) { 1361 if (error_ptr) 1362 error_ptr->SetErrorString( 1363 "Expression stack needs at least 1 item for DW_OP_abs."); 1364 return false; 1365 } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) { 1366 if (error_ptr) 1367 error_ptr->SetErrorString( 1368 "Failed to take the absolute value of the first stack item."); 1369 return false; 1370 } 1371 break; 1372 1373 // OPCODE: DW_OP_and 1374 // OPERANDS: none 1375 // DESCRIPTION: pops the top two stack values, performs a bitwise and 1376 // operation on the two, and pushes the result. 1377 case DW_OP_and: 1378 if (stack.size() < 2) { 1379 if (error_ptr) 1380 error_ptr->SetErrorString( 1381 "Expression stack needs at least 2 items for DW_OP_and."); 1382 return false; 1383 } else { 1384 tmp = stack.back(); 1385 stack.pop_back(); 1386 stack.back().ResolveValue(exe_ctx) = 1387 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx); 1388 } 1389 break; 1390 1391 // OPCODE: DW_OP_div 1392 // OPERANDS: none 1393 // DESCRIPTION: pops the top two stack values, divides the former second 1394 // entry by the former top of the stack using signed division, and pushes 1395 // the result. 1396 case DW_OP_div: 1397 if (stack.size() < 2) { 1398 if (error_ptr) 1399 error_ptr->SetErrorString( 1400 "Expression stack needs at least 2 items for DW_OP_div."); 1401 return false; 1402 } else { 1403 tmp = stack.back(); 1404 if (tmp.ResolveValue(exe_ctx).IsZero()) { 1405 if (error_ptr) 1406 error_ptr->SetErrorString("Divide by zero."); 1407 return false; 1408 } else { 1409 stack.pop_back(); 1410 stack.back() = 1411 stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx); 1412 if (!stack.back().ResolveValue(exe_ctx).IsValid()) { 1413 if (error_ptr) 1414 error_ptr->SetErrorString("Divide failed."); 1415 return false; 1416 } 1417 } 1418 } 1419 break; 1420 1421 // OPCODE: DW_OP_minus 1422 // OPERANDS: none 1423 // DESCRIPTION: pops the top two stack values, subtracts the former top 1424 // of the stack from the former second entry, and pushes the result. 1425 case DW_OP_minus: 1426 if (stack.size() < 2) { 1427 if (error_ptr) 1428 error_ptr->SetErrorString( 1429 "Expression stack needs at least 2 items for DW_OP_minus."); 1430 return false; 1431 } else { 1432 tmp = stack.back(); 1433 stack.pop_back(); 1434 stack.back().ResolveValue(exe_ctx) = 1435 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx); 1436 } 1437 break; 1438 1439 // OPCODE: DW_OP_mod 1440 // OPERANDS: none 1441 // DESCRIPTION: pops the top two stack values and pushes the result of 1442 // the calculation: former second stack entry modulo the former top of the 1443 // stack. 1444 case DW_OP_mod: 1445 if (stack.size() < 2) { 1446 if (error_ptr) 1447 error_ptr->SetErrorString( 1448 "Expression stack needs at least 2 items for DW_OP_mod."); 1449 return false; 1450 } else { 1451 tmp = stack.back(); 1452 stack.pop_back(); 1453 stack.back().ResolveValue(exe_ctx) = 1454 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx); 1455 } 1456 break; 1457 1458 // OPCODE: DW_OP_mul 1459 // OPERANDS: none 1460 // DESCRIPTION: pops the top two stack entries, multiplies them 1461 // together, and pushes the result. 1462 case DW_OP_mul: 1463 if (stack.size() < 2) { 1464 if (error_ptr) 1465 error_ptr->SetErrorString( 1466 "Expression stack needs at least 2 items for DW_OP_mul."); 1467 return false; 1468 } else { 1469 tmp = stack.back(); 1470 stack.pop_back(); 1471 stack.back().ResolveValue(exe_ctx) = 1472 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx); 1473 } 1474 break; 1475 1476 // OPCODE: DW_OP_neg 1477 // OPERANDS: none 1478 // DESCRIPTION: pops the top stack entry, and pushes its negation. 1479 case DW_OP_neg: 1480 if (stack.empty()) { 1481 if (error_ptr) 1482 error_ptr->SetErrorString( 1483 "Expression stack needs at least 1 item for DW_OP_neg."); 1484 return false; 1485 } else { 1486 if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) { 1487 if (error_ptr) 1488 error_ptr->SetErrorString("Unary negate failed."); 1489 return false; 1490 } 1491 } 1492 break; 1493 1494 // OPCODE: DW_OP_not 1495 // OPERANDS: none 1496 // DESCRIPTION: pops the top stack entry, and pushes its bitwise 1497 // complement 1498 case DW_OP_not: 1499 if (stack.empty()) { 1500 if (error_ptr) 1501 error_ptr->SetErrorString( 1502 "Expression stack needs at least 1 item for DW_OP_not."); 1503 return false; 1504 } else { 1505 if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) { 1506 if (error_ptr) 1507 error_ptr->SetErrorString("Logical NOT failed."); 1508 return false; 1509 } 1510 } 1511 break; 1512 1513 // OPCODE: DW_OP_or 1514 // OPERANDS: none 1515 // DESCRIPTION: pops the top two stack entries, performs a bitwise or 1516 // operation on the two, and pushes the result. 1517 case DW_OP_or: 1518 if (stack.size() < 2) { 1519 if (error_ptr) 1520 error_ptr->SetErrorString( 1521 "Expression stack needs at least 2 items for DW_OP_or."); 1522 return false; 1523 } else { 1524 tmp = stack.back(); 1525 stack.pop_back(); 1526 stack.back().ResolveValue(exe_ctx) = 1527 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx); 1528 } 1529 break; 1530 1531 // OPCODE: DW_OP_plus 1532 // OPERANDS: none 1533 // DESCRIPTION: pops the top two stack entries, adds them together, and 1534 // pushes the result. 1535 case DW_OP_plus: 1536 if (stack.size() < 2) { 1537 if (error_ptr) 1538 error_ptr->SetErrorString( 1539 "Expression stack needs at least 2 items for DW_OP_plus."); 1540 return false; 1541 } else { 1542 tmp = stack.back(); 1543 stack.pop_back(); 1544 stack.back().GetScalar() += tmp.GetScalar(); 1545 } 1546 break; 1547 1548 // OPCODE: DW_OP_plus_uconst 1549 // OPERANDS: none 1550 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128 1551 // constant operand and pushes the result. 1552 case DW_OP_plus_uconst: 1553 if (stack.empty()) { 1554 if (error_ptr) 1555 error_ptr->SetErrorString( 1556 "Expression stack needs at least 1 item for DW_OP_plus_uconst."); 1557 return false; 1558 } else { 1559 const uint64_t uconst_value = opcodes.GetULEB128(&offset); 1560 // Implicit conversion from a UINT to a Scalar... 1561 stack.back().GetScalar() += uconst_value; 1562 if (!stack.back().GetScalar().IsValid()) { 1563 if (error_ptr) 1564 error_ptr->SetErrorString("DW_OP_plus_uconst failed."); 1565 return false; 1566 } 1567 } 1568 break; 1569 1570 // OPCODE: DW_OP_shl 1571 // OPERANDS: none 1572 // DESCRIPTION: pops the top two stack entries, shifts the former 1573 // second entry left by the number of bits specified by the former top of 1574 // the stack, and pushes the result. 1575 case DW_OP_shl: 1576 if (stack.size() < 2) { 1577 if (error_ptr) 1578 error_ptr->SetErrorString( 1579 "Expression stack needs at least 2 items for DW_OP_shl."); 1580 return false; 1581 } else { 1582 tmp = stack.back(); 1583 stack.pop_back(); 1584 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx); 1585 } 1586 break; 1587 1588 // OPCODE: DW_OP_shr 1589 // OPERANDS: none 1590 // DESCRIPTION: pops the top two stack entries, shifts the former second 1591 // entry right logically (filling with zero bits) by the number of bits 1592 // specified by the former top of the stack, and pushes the result. 1593 case DW_OP_shr: 1594 if (stack.size() < 2) { 1595 if (error_ptr) 1596 error_ptr->SetErrorString( 1597 "Expression stack needs at least 2 items for DW_OP_shr."); 1598 return false; 1599 } else { 1600 tmp = stack.back(); 1601 stack.pop_back(); 1602 if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical( 1603 tmp.ResolveValue(exe_ctx))) { 1604 if (error_ptr) 1605 error_ptr->SetErrorString("DW_OP_shr failed."); 1606 return false; 1607 } 1608 } 1609 break; 1610 1611 // OPCODE: DW_OP_shra 1612 // OPERANDS: none 1613 // DESCRIPTION: pops the top two stack entries, shifts the former second 1614 // entry right arithmetically (divide the magnitude by 2, keep the same 1615 // sign for the result) by the number of bits specified by the former top 1616 // of the stack, and pushes the result. 1617 case DW_OP_shra: 1618 if (stack.size() < 2) { 1619 if (error_ptr) 1620 error_ptr->SetErrorString( 1621 "Expression stack needs at least 2 items for DW_OP_shra."); 1622 return false; 1623 } else { 1624 tmp = stack.back(); 1625 stack.pop_back(); 1626 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx); 1627 } 1628 break; 1629 1630 // OPCODE: DW_OP_xor 1631 // OPERANDS: none 1632 // DESCRIPTION: pops the top two stack entries, performs the bitwise 1633 // exclusive-or operation on the two, and pushes the result. 1634 case DW_OP_xor: 1635 if (stack.size() < 2) { 1636 if (error_ptr) 1637 error_ptr->SetErrorString( 1638 "Expression stack needs at least 2 items for DW_OP_xor."); 1639 return false; 1640 } else { 1641 tmp = stack.back(); 1642 stack.pop_back(); 1643 stack.back().ResolveValue(exe_ctx) = 1644 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx); 1645 } 1646 break; 1647 1648 // OPCODE: DW_OP_skip 1649 // OPERANDS: int16_t 1650 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte 1651 // signed integer constant. The 2-byte constant is the number of bytes of 1652 // the DWARF expression to skip forward or backward from the current 1653 // operation, beginning after the 2-byte constant. 1654 case DW_OP_skip: { 1655 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset); 1656 lldb::offset_t new_offset = offset + skip_offset; 1657 if (opcodes.ValidOffset(new_offset)) 1658 offset = new_offset; 1659 else { 1660 if (error_ptr) 1661 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip."); 1662 return false; 1663 } 1664 } break; 1665 1666 // OPCODE: DW_OP_bra 1667 // OPERANDS: int16_t 1668 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte 1669 // signed integer constant. This operation pops the top of stack. If the 1670 // value popped is not the constant 0, the 2-byte constant operand is the 1671 // number of bytes of the DWARF expression to skip forward or backward from 1672 // the current operation, beginning after the 2-byte constant. 1673 case DW_OP_bra: 1674 if (stack.empty()) { 1675 if (error_ptr) 1676 error_ptr->SetErrorString( 1677 "Expression stack needs at least 1 item for DW_OP_bra."); 1678 return false; 1679 } else { 1680 tmp = stack.back(); 1681 stack.pop_back(); 1682 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset); 1683 Scalar zero(0); 1684 if (tmp.ResolveValue(exe_ctx) != zero) { 1685 lldb::offset_t new_offset = offset + bra_offset; 1686 if (opcodes.ValidOffset(new_offset)) 1687 offset = new_offset; 1688 else { 1689 if (error_ptr) 1690 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra."); 1691 return false; 1692 } 1693 } 1694 } 1695 break; 1696 1697 // OPCODE: DW_OP_eq 1698 // OPERANDS: none 1699 // DESCRIPTION: pops the top two stack values, compares using the 1700 // equals (==) operator. 1701 // STACK RESULT: push the constant value 1 onto the stack if the result 1702 // of the operation is true or the constant value 0 if the result of the 1703 // operation is false. 1704 case DW_OP_eq: 1705 if (stack.size() < 2) { 1706 if (error_ptr) 1707 error_ptr->SetErrorString( 1708 "Expression stack needs at least 2 items for DW_OP_eq."); 1709 return false; 1710 } else { 1711 tmp = stack.back(); 1712 stack.pop_back(); 1713 stack.back().ResolveValue(exe_ctx) = 1714 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx); 1715 } 1716 break; 1717 1718 // OPCODE: DW_OP_ge 1719 // OPERANDS: none 1720 // DESCRIPTION: pops the top two stack values, compares using the 1721 // greater than or equal to (>=) operator. 1722 // STACK RESULT: push the constant value 1 onto the stack if the result 1723 // of the operation is true or the constant value 0 if the result of the 1724 // operation is false. 1725 case DW_OP_ge: 1726 if (stack.size() < 2) { 1727 if (error_ptr) 1728 error_ptr->SetErrorString( 1729 "Expression stack needs at least 2 items for DW_OP_ge."); 1730 return false; 1731 } else { 1732 tmp = stack.back(); 1733 stack.pop_back(); 1734 stack.back().ResolveValue(exe_ctx) = 1735 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx); 1736 } 1737 break; 1738 1739 // OPCODE: DW_OP_gt 1740 // OPERANDS: none 1741 // DESCRIPTION: pops the top two stack values, compares using the 1742 // greater than (>) operator. 1743 // STACK RESULT: push the constant value 1 onto the stack if the result 1744 // of the operation is true or the constant value 0 if the result of the 1745 // operation is false. 1746 case DW_OP_gt: 1747 if (stack.size() < 2) { 1748 if (error_ptr) 1749 error_ptr->SetErrorString( 1750 "Expression stack needs at least 2 items for DW_OP_gt."); 1751 return false; 1752 } else { 1753 tmp = stack.back(); 1754 stack.pop_back(); 1755 stack.back().ResolveValue(exe_ctx) = 1756 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx); 1757 } 1758 break; 1759 1760 // OPCODE: DW_OP_le 1761 // OPERANDS: none 1762 // DESCRIPTION: pops the top two stack values, compares using the 1763 // less than or equal to (<=) operator. 1764 // STACK RESULT: push the constant value 1 onto the stack if the result 1765 // of the operation is true or the constant value 0 if the result of the 1766 // operation is false. 1767 case DW_OP_le: 1768 if (stack.size() < 2) { 1769 if (error_ptr) 1770 error_ptr->SetErrorString( 1771 "Expression stack needs at least 2 items for DW_OP_le."); 1772 return false; 1773 } else { 1774 tmp = stack.back(); 1775 stack.pop_back(); 1776 stack.back().ResolveValue(exe_ctx) = 1777 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx); 1778 } 1779 break; 1780 1781 // OPCODE: DW_OP_lt 1782 // OPERANDS: none 1783 // DESCRIPTION: pops the top two stack values, compares using the 1784 // less than (<) operator. 1785 // STACK RESULT: push the constant value 1 onto the stack if the result 1786 // of the operation is true or the constant value 0 if the result of the 1787 // operation is false. 1788 case DW_OP_lt: 1789 if (stack.size() < 2) { 1790 if (error_ptr) 1791 error_ptr->SetErrorString( 1792 "Expression stack needs at least 2 items for DW_OP_lt."); 1793 return false; 1794 } else { 1795 tmp = stack.back(); 1796 stack.pop_back(); 1797 stack.back().ResolveValue(exe_ctx) = 1798 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx); 1799 } 1800 break; 1801 1802 // OPCODE: DW_OP_ne 1803 // OPERANDS: none 1804 // DESCRIPTION: pops the top two stack values, compares using the 1805 // not equal (!=) operator. 1806 // STACK RESULT: push the constant value 1 onto the stack if the result 1807 // of the operation is true or the constant value 0 if the result of the 1808 // operation is false. 1809 case DW_OP_ne: 1810 if (stack.size() < 2) { 1811 if (error_ptr) 1812 error_ptr->SetErrorString( 1813 "Expression stack needs at least 2 items for DW_OP_ne."); 1814 return false; 1815 } else { 1816 tmp = stack.back(); 1817 stack.pop_back(); 1818 stack.back().ResolveValue(exe_ctx) = 1819 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx); 1820 } 1821 break; 1822 1823 // OPCODE: DW_OP_litn 1824 // OPERANDS: none 1825 // DESCRIPTION: encode the unsigned literal values from 0 through 31. 1826 // STACK RESULT: push the unsigned literal constant value onto the top 1827 // of the stack. 1828 case DW_OP_lit0: 1829 case DW_OP_lit1: 1830 case DW_OP_lit2: 1831 case DW_OP_lit3: 1832 case DW_OP_lit4: 1833 case DW_OP_lit5: 1834 case DW_OP_lit6: 1835 case DW_OP_lit7: 1836 case DW_OP_lit8: 1837 case DW_OP_lit9: 1838 case DW_OP_lit10: 1839 case DW_OP_lit11: 1840 case DW_OP_lit12: 1841 case DW_OP_lit13: 1842 case DW_OP_lit14: 1843 case DW_OP_lit15: 1844 case DW_OP_lit16: 1845 case DW_OP_lit17: 1846 case DW_OP_lit18: 1847 case DW_OP_lit19: 1848 case DW_OP_lit20: 1849 case DW_OP_lit21: 1850 case DW_OP_lit22: 1851 case DW_OP_lit23: 1852 case DW_OP_lit24: 1853 case DW_OP_lit25: 1854 case DW_OP_lit26: 1855 case DW_OP_lit27: 1856 case DW_OP_lit28: 1857 case DW_OP_lit29: 1858 case DW_OP_lit30: 1859 case DW_OP_lit31: 1860 stack.push_back(to_generic(op - DW_OP_lit0)); 1861 break; 1862 1863 // OPCODE: DW_OP_regN 1864 // OPERANDS: none 1865 // DESCRIPTION: Push the value in register n on the top of the stack. 1866 case DW_OP_reg0: 1867 case DW_OP_reg1: 1868 case DW_OP_reg2: 1869 case DW_OP_reg3: 1870 case DW_OP_reg4: 1871 case DW_OP_reg5: 1872 case DW_OP_reg6: 1873 case DW_OP_reg7: 1874 case DW_OP_reg8: 1875 case DW_OP_reg9: 1876 case DW_OP_reg10: 1877 case DW_OP_reg11: 1878 case DW_OP_reg12: 1879 case DW_OP_reg13: 1880 case DW_OP_reg14: 1881 case DW_OP_reg15: 1882 case DW_OP_reg16: 1883 case DW_OP_reg17: 1884 case DW_OP_reg18: 1885 case DW_OP_reg19: 1886 case DW_OP_reg20: 1887 case DW_OP_reg21: 1888 case DW_OP_reg22: 1889 case DW_OP_reg23: 1890 case DW_OP_reg24: 1891 case DW_OP_reg25: 1892 case DW_OP_reg26: 1893 case DW_OP_reg27: 1894 case DW_OP_reg28: 1895 case DW_OP_reg29: 1896 case DW_OP_reg30: 1897 case DW_OP_reg31: { 1898 dwarf4_location_description_kind = Register; 1899 reg_num = op - DW_OP_reg0; 1900 1901 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 1902 stack.push_back(tmp); 1903 else 1904 return false; 1905 } break; 1906 // OPCODE: DW_OP_regx 1907 // OPERANDS: 1908 // ULEB128 literal operand that encodes the register. 1909 // DESCRIPTION: Push the value in register on the top of the stack. 1910 case DW_OP_regx: { 1911 dwarf4_location_description_kind = Register; 1912 reg_num = opcodes.GetULEB128(&offset); 1913 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 1914 stack.push_back(tmp); 1915 else 1916 return false; 1917 } break; 1918 1919 // OPCODE: DW_OP_bregN 1920 // OPERANDS: 1921 // SLEB128 offset from register N 1922 // DESCRIPTION: Value is in memory at the address specified by register 1923 // N plus an offset. 1924 case DW_OP_breg0: 1925 case DW_OP_breg1: 1926 case DW_OP_breg2: 1927 case DW_OP_breg3: 1928 case DW_OP_breg4: 1929 case DW_OP_breg5: 1930 case DW_OP_breg6: 1931 case DW_OP_breg7: 1932 case DW_OP_breg8: 1933 case DW_OP_breg9: 1934 case DW_OP_breg10: 1935 case DW_OP_breg11: 1936 case DW_OP_breg12: 1937 case DW_OP_breg13: 1938 case DW_OP_breg14: 1939 case DW_OP_breg15: 1940 case DW_OP_breg16: 1941 case DW_OP_breg17: 1942 case DW_OP_breg18: 1943 case DW_OP_breg19: 1944 case DW_OP_breg20: 1945 case DW_OP_breg21: 1946 case DW_OP_breg22: 1947 case DW_OP_breg23: 1948 case DW_OP_breg24: 1949 case DW_OP_breg25: 1950 case DW_OP_breg26: 1951 case DW_OP_breg27: 1952 case DW_OP_breg28: 1953 case DW_OP_breg29: 1954 case DW_OP_breg30: 1955 case DW_OP_breg31: { 1956 reg_num = op - DW_OP_breg0; 1957 1958 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, 1959 tmp)) { 1960 int64_t breg_offset = opcodes.GetSLEB128(&offset); 1961 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; 1962 tmp.ClearContext(); 1963 stack.push_back(tmp); 1964 stack.back().SetValueType(Value::ValueType::LoadAddress); 1965 } else 1966 return false; 1967 } break; 1968 // OPCODE: DW_OP_bregx 1969 // OPERANDS: 2 1970 // ULEB128 literal operand that encodes the register. 1971 // SLEB128 offset from register N 1972 // DESCRIPTION: Value is in memory at the address specified by register 1973 // N plus an offset. 1974 case DW_OP_bregx: { 1975 reg_num = opcodes.GetULEB128(&offset); 1976 1977 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, 1978 tmp)) { 1979 int64_t breg_offset = opcodes.GetSLEB128(&offset); 1980 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; 1981 tmp.ClearContext(); 1982 stack.push_back(tmp); 1983 stack.back().SetValueType(Value::ValueType::LoadAddress); 1984 } else 1985 return false; 1986 } break; 1987 1988 case DW_OP_fbreg: 1989 if (exe_ctx) { 1990 if (frame) { 1991 Scalar value; 1992 if (frame->GetFrameBaseValue(value, error_ptr)) { 1993 int64_t fbreg_offset = opcodes.GetSLEB128(&offset); 1994 value += fbreg_offset; 1995 stack.push_back(value); 1996 stack.back().SetValueType(Value::ValueType::LoadAddress); 1997 } else 1998 return false; 1999 } else { 2000 if (error_ptr) 2001 error_ptr->SetErrorString( 2002 "Invalid stack frame in context for DW_OP_fbreg opcode."); 2003 return false; 2004 } 2005 } else { 2006 if (error_ptr) 2007 error_ptr->SetErrorString( 2008 "NULL execution context for DW_OP_fbreg.\n"); 2009 return false; 2010 } 2011 2012 break; 2013 2014 // OPCODE: DW_OP_nop 2015 // OPERANDS: none 2016 // DESCRIPTION: A place holder. It has no effect on the location stack 2017 // or any of its values. 2018 case DW_OP_nop: 2019 break; 2020 2021 // OPCODE: DW_OP_piece 2022 // OPERANDS: 1 2023 // ULEB128: byte size of the piece 2024 // DESCRIPTION: The operand describes the size in bytes of the piece of 2025 // the object referenced by the DWARF expression whose result is at the top 2026 // of the stack. If the piece is located in a register, but does not occupy 2027 // the entire register, the placement of the piece within that register is 2028 // defined by the ABI. 2029 // 2030 // Many compilers store a single variable in sets of registers, or store a 2031 // variable partially in memory and partially in registers. DW_OP_piece 2032 // provides a way of describing how large a part of a variable a particular 2033 // DWARF expression refers to. 2034 case DW_OP_piece: { 2035 LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind; 2036 // Reset for the next piece. 2037 dwarf4_location_description_kind = Memory; 2038 2039 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset); 2040 2041 if (piece_byte_size > 0) { 2042 Value curr_piece; 2043 2044 if (stack.empty()) { 2045 UpdateValueTypeFromLocationDescription( 2046 log, dwarf_cu, LocationDescriptionKind::Empty); 2047 // In a multi-piece expression, this means that the current piece is 2048 // not available. Fill with zeros for now by resizing the data and 2049 // appending it 2050 curr_piece.ResizeData(piece_byte_size); 2051 // Note that "0" is not a correct value for the unknown bits. 2052 // It would be better to also return a mask of valid bits together 2053 // with the expression result, so the debugger can print missing 2054 // members as "<optimized out>" or something. 2055 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size); 2056 pieces.AppendDataToHostBuffer(curr_piece); 2057 } else { 2058 Status error; 2059 // Extract the current piece into "curr_piece" 2060 Value curr_piece_source_value(stack.back()); 2061 stack.pop_back(); 2062 UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc, 2063 &curr_piece_source_value); 2064 2065 const Value::ValueType curr_piece_source_value_type = 2066 curr_piece_source_value.GetValueType(); 2067 switch (curr_piece_source_value_type) { 2068 case Value::ValueType::Invalid: 2069 return false; 2070 case Value::ValueType::LoadAddress: 2071 if (process) { 2072 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) { 2073 lldb::addr_t load_addr = 2074 curr_piece_source_value.GetScalar().ULongLong( 2075 LLDB_INVALID_ADDRESS); 2076 if (process->ReadMemory( 2077 load_addr, curr_piece.GetBuffer().GetBytes(), 2078 piece_byte_size, error) != piece_byte_size) { 2079 if (error_ptr) 2080 error_ptr->SetErrorStringWithFormat( 2081 "failed to read memory DW_OP_piece(%" PRIu64 2082 ") from 0x%" PRIx64, 2083 piece_byte_size, load_addr); 2084 return false; 2085 } 2086 } else { 2087 if (error_ptr) 2088 error_ptr->SetErrorStringWithFormat( 2089 "failed to resize the piece memory buffer for " 2090 "DW_OP_piece(%" PRIu64 ")", 2091 piece_byte_size); 2092 return false; 2093 } 2094 } 2095 break; 2096 2097 case Value::ValueType::FileAddress: 2098 case Value::ValueType::HostAddress: 2099 if (error_ptr) { 2100 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong( 2101 LLDB_INVALID_ADDRESS); 2102 error_ptr->SetErrorStringWithFormat( 2103 "failed to read memory DW_OP_piece(%" PRIu64 2104 ") from %s address 0x%" PRIx64, 2105 piece_byte_size, curr_piece_source_value.GetValueType() == 2106 Value::ValueType::FileAddress 2107 ? "file" 2108 : "host", 2109 addr); 2110 } 2111 return false; 2112 2113 case Value::ValueType::Scalar: { 2114 uint32_t bit_size = piece_byte_size * 8; 2115 uint32_t bit_offset = 0; 2116 Scalar &scalar = curr_piece_source_value.GetScalar(); 2117 if (!scalar.ExtractBitfield( 2118 bit_size, bit_offset)) { 2119 if (error_ptr) 2120 error_ptr->SetErrorStringWithFormat( 2121 "unable to extract %" PRIu64 " bytes from a %" PRIu64 2122 " byte scalar value.", 2123 piece_byte_size, 2124 (uint64_t)curr_piece_source_value.GetScalar() 2125 .GetByteSize()); 2126 return false; 2127 } 2128 // Create curr_piece with bit_size. By default Scalar 2129 // grows to the nearest host integer type. 2130 llvm::APInt fail_value(1, 0, false); 2131 llvm::APInt ap_int = scalar.UInt128(fail_value); 2132 assert(ap_int.getBitWidth() >= bit_size); 2133 llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(), 2134 ap_int.getNumWords()}; 2135 curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf)); 2136 } break; 2137 } 2138 2139 // Check if this is the first piece? 2140 if (op_piece_offset == 0) { 2141 // This is the first piece, we should push it back onto the stack 2142 // so subsequent pieces will be able to access this piece and add 2143 // to it. 2144 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) { 2145 if (error_ptr) 2146 error_ptr->SetErrorString("failed to append piece data"); 2147 return false; 2148 } 2149 } else { 2150 // If this is the second or later piece there should be a value on 2151 // the stack. 2152 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) { 2153 if (error_ptr) 2154 error_ptr->SetErrorStringWithFormat( 2155 "DW_OP_piece for offset %" PRIu64 2156 " but top of stack is of size %" PRIu64, 2157 op_piece_offset, pieces.GetBuffer().GetByteSize()); 2158 return false; 2159 } 2160 2161 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) { 2162 if (error_ptr) 2163 error_ptr->SetErrorString("failed to append piece data"); 2164 return false; 2165 } 2166 } 2167 } 2168 op_piece_offset += piece_byte_size; 2169 } 2170 } break; 2171 2172 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3); 2173 if (stack.size() < 1) { 2174 UpdateValueTypeFromLocationDescription(log, dwarf_cu, 2175 LocationDescriptionKind::Empty); 2176 // Reset for the next piece. 2177 dwarf4_location_description_kind = Memory; 2178 if (error_ptr) 2179 error_ptr->SetErrorString( 2180 "Expression stack needs at least 1 item for DW_OP_bit_piece."); 2181 return false; 2182 } else { 2183 UpdateValueTypeFromLocationDescription( 2184 log, dwarf_cu, dwarf4_location_description_kind, &stack.back()); 2185 // Reset for the next piece. 2186 dwarf4_location_description_kind = Memory; 2187 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset); 2188 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset); 2189 switch (stack.back().GetValueType()) { 2190 case Value::ValueType::Invalid: 2191 return false; 2192 case Value::ValueType::Scalar: { 2193 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size, 2194 piece_bit_offset)) { 2195 if (error_ptr) 2196 error_ptr->SetErrorStringWithFormat( 2197 "unable to extract %" PRIu64 " bit value with %" PRIu64 2198 " bit offset from a %" PRIu64 " bit scalar value.", 2199 piece_bit_size, piece_bit_offset, 2200 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8)); 2201 return false; 2202 } 2203 } break; 2204 2205 case Value::ValueType::FileAddress: 2206 case Value::ValueType::LoadAddress: 2207 case Value::ValueType::HostAddress: 2208 if (error_ptr) { 2209 error_ptr->SetErrorStringWithFormat( 2210 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64 2211 ", bit_offset = %" PRIu64 ") from an address value.", 2212 piece_bit_size, piece_bit_offset); 2213 } 2214 return false; 2215 } 2216 } 2217 break; 2218 2219 // OPCODE: DW_OP_implicit_value 2220 // OPERANDS: 2 2221 // ULEB128 size of the value block in bytes 2222 // uint8_t* block bytes encoding value in target's memory 2223 // representation 2224 // DESCRIPTION: Value is immediately stored in block in the debug info with 2225 // the memory representation of the target. 2226 case DW_OP_implicit_value: { 2227 dwarf4_location_description_kind = Implicit; 2228 2229 const uint32_t len = opcodes.GetULEB128(&offset); 2230 const void *data = opcodes.GetData(&offset, len); 2231 2232 if (!data) { 2233 LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data"); 2234 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", 2235 DW_OP_value_to_name(op)); 2236 return false; 2237 } 2238 2239 Value result(data, len); 2240 stack.push_back(result); 2241 break; 2242 } 2243 2244 case DW_OP_implicit_pointer: { 2245 dwarf4_location_description_kind = Implicit; 2246 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", DW_OP_value_to_name(op)); 2247 return false; 2248 } 2249 2250 // OPCODE: DW_OP_push_object_address 2251 // OPERANDS: none 2252 // DESCRIPTION: Pushes the address of the object currently being 2253 // evaluated as part of evaluation of a user presented expression. This 2254 // object may correspond to an independent variable described by its own 2255 // DIE or it may be a component of an array, structure, or class whose 2256 // address has been dynamically determined by an earlier step during user 2257 // expression evaluation. 2258 case DW_OP_push_object_address: 2259 if (object_address_ptr) 2260 stack.push_back(*object_address_ptr); 2261 else { 2262 if (error_ptr) 2263 error_ptr->SetErrorString("DW_OP_push_object_address used without " 2264 "specifying an object address"); 2265 return false; 2266 } 2267 break; 2268 2269 // OPCODE: DW_OP_call2 2270 // OPERANDS: 2271 // uint16_t compile unit relative offset of a DIE 2272 // DESCRIPTION: Performs subroutine calls during evaluation 2273 // of a DWARF expression. The operand is the 2-byte unsigned offset of a 2274 // debugging information entry in the current compilation unit. 2275 // 2276 // Operand interpretation is exactly like that for DW_FORM_ref2. 2277 // 2278 // This operation transfers control of DWARF expression evaluation to the 2279 // DW_AT_location attribute of the referenced DIE. If there is no such 2280 // attribute, then there is no effect. Execution of the DWARF expression of 2281 // a DW_AT_location attribute may add to and/or remove from values on the 2282 // stack. Execution returns to the point following the call when the end of 2283 // the attribute is reached. Values on the stack at the time of the call 2284 // may be used as parameters by the called expression and values left on 2285 // the stack by the called expression may be used as return values by prior 2286 // agreement between the calling and called expressions. 2287 case DW_OP_call2: 2288 if (error_ptr) 2289 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2."); 2290 return false; 2291 // OPCODE: DW_OP_call4 2292 // OPERANDS: 1 2293 // uint32_t compile unit relative offset of a DIE 2294 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF 2295 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of 2296 // a debugging information entry in the current compilation unit. 2297 // 2298 // Operand interpretation DW_OP_call4 is exactly like that for 2299 // DW_FORM_ref4. 2300 // 2301 // This operation transfers control of DWARF expression evaluation to the 2302 // DW_AT_location attribute of the referenced DIE. If there is no such 2303 // attribute, then there is no effect. Execution of the DWARF expression of 2304 // a DW_AT_location attribute may add to and/or remove from values on the 2305 // stack. Execution returns to the point following the call when the end of 2306 // the attribute is reached. Values on the stack at the time of the call 2307 // may be used as parameters by the called expression and values left on 2308 // the stack by the called expression may be used as return values by prior 2309 // agreement between the calling and called expressions. 2310 case DW_OP_call4: 2311 if (error_ptr) 2312 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4."); 2313 return false; 2314 2315 // OPCODE: DW_OP_stack_value 2316 // OPERANDS: None 2317 // DESCRIPTION: Specifies that the object does not exist in memory but 2318 // rather is a constant value. The value from the top of the stack is the 2319 // value to be used. This is the actual object value and not the location. 2320 case DW_OP_stack_value: 2321 dwarf4_location_description_kind = Implicit; 2322 if (stack.empty()) { 2323 if (error_ptr) 2324 error_ptr->SetErrorString( 2325 "Expression stack needs at least 1 item for DW_OP_stack_value."); 2326 return false; 2327 } 2328 stack.back().SetValueType(Value::ValueType::Scalar); 2329 break; 2330 2331 // OPCODE: DW_OP_convert 2332 // OPERANDS: 1 2333 // A ULEB128 that is either a DIE offset of a 2334 // DW_TAG_base_type or 0 for the generic (pointer-sized) type. 2335 // 2336 // DESCRIPTION: Pop the top stack element, convert it to a 2337 // different type, and push the result. 2338 case DW_OP_convert: { 2339 if (stack.size() < 1) { 2340 if (error_ptr) 2341 error_ptr->SetErrorString( 2342 "Expression stack needs at least 1 item for DW_OP_convert."); 2343 return false; 2344 } 2345 const uint64_t die_offset = opcodes.GetULEB128(&offset); 2346 uint64_t bit_size; 2347 bool sign; 2348 if (die_offset == 0) { 2349 // The generic type has the size of an address on the target 2350 // machine and an unspecified signedness. Scalar has no 2351 // "unspecified signedness", so we use unsigned types. 2352 if (!module_sp) { 2353 if (error_ptr) 2354 error_ptr->SetErrorString("No module"); 2355 return false; 2356 } 2357 sign = false; 2358 bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8; 2359 if (!bit_size) { 2360 if (error_ptr) 2361 error_ptr->SetErrorString("unspecified architecture"); 2362 return false; 2363 } 2364 } else { 2365 // Retrieve the type DIE that the value is being converted to. 2366 // FIXME: the constness has annoying ripple effects. 2367 DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(die_offset); 2368 if (!die) { 2369 if (error_ptr) 2370 error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE"); 2371 return false; 2372 } 2373 uint64_t encoding = 2374 die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user); 2375 bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8; 2376 if (!bit_size) 2377 bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0); 2378 if (!bit_size) { 2379 if (error_ptr) 2380 error_ptr->SetErrorString("Unsupported type size in DW_OP_convert"); 2381 return false; 2382 } 2383 switch (encoding) { 2384 case DW_ATE_signed: 2385 case DW_ATE_signed_char: 2386 sign = true; 2387 break; 2388 case DW_ATE_unsigned: 2389 case DW_ATE_unsigned_char: 2390 sign = false; 2391 break; 2392 default: 2393 if (error_ptr) 2394 error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert"); 2395 return false; 2396 } 2397 } 2398 Scalar &top = stack.back().ResolveValue(exe_ctx); 2399 top.TruncOrExtendTo(bit_size, sign); 2400 break; 2401 } 2402 2403 // OPCODE: DW_OP_call_frame_cfa 2404 // OPERANDS: None 2405 // DESCRIPTION: Specifies a DWARF expression that pushes the value of 2406 // the canonical frame address consistent with the call frame information 2407 // located in .debug_frame (or in the FDEs of the eh_frame section). 2408 case DW_OP_call_frame_cfa: 2409 if (frame) { 2410 // Note that we don't have to parse FDEs because this DWARF expression 2411 // is commonly evaluated with a valid stack frame. 2412 StackID id = frame->GetStackID(); 2413 addr_t cfa = id.GetCallFrameAddress(); 2414 if (cfa != LLDB_INVALID_ADDRESS) { 2415 stack.push_back(Scalar(cfa)); 2416 stack.back().SetValueType(Value::ValueType::LoadAddress); 2417 } else if (error_ptr) 2418 error_ptr->SetErrorString("Stack frame does not include a canonical " 2419 "frame address for DW_OP_call_frame_cfa " 2420 "opcode."); 2421 } else { 2422 if (error_ptr) 2423 error_ptr->SetErrorString("Invalid stack frame in context for " 2424 "DW_OP_call_frame_cfa opcode."); 2425 return false; 2426 } 2427 break; 2428 2429 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension 2430 // opcode, DW_OP_GNU_push_tls_address) 2431 // OPERANDS: none 2432 // DESCRIPTION: Pops a TLS offset from the stack, converts it to 2433 // an address in the current thread's thread-local storage block, and 2434 // pushes it on the stack. 2435 case DW_OP_form_tls_address: 2436 case DW_OP_GNU_push_tls_address: { 2437 if (stack.size() < 1) { 2438 if (error_ptr) { 2439 if (op == DW_OP_form_tls_address) 2440 error_ptr->SetErrorString( 2441 "DW_OP_form_tls_address needs an argument."); 2442 else 2443 error_ptr->SetErrorString( 2444 "DW_OP_GNU_push_tls_address needs an argument."); 2445 } 2446 return false; 2447 } 2448 2449 if (!exe_ctx || !module_sp) { 2450 if (error_ptr) 2451 error_ptr->SetErrorString("No context to evaluate TLS within."); 2452 return false; 2453 } 2454 2455 Thread *thread = exe_ctx->GetThreadPtr(); 2456 if (!thread) { 2457 if (error_ptr) 2458 error_ptr->SetErrorString("No thread to evaluate TLS within."); 2459 return false; 2460 } 2461 2462 // Lookup the TLS block address for this thread and module. 2463 const addr_t tls_file_addr = 2464 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 2465 const addr_t tls_load_addr = 2466 thread->GetThreadLocalData(module_sp, tls_file_addr); 2467 2468 if (tls_load_addr == LLDB_INVALID_ADDRESS) { 2469 if (error_ptr) 2470 error_ptr->SetErrorString( 2471 "No TLS data currently exists for this thread."); 2472 return false; 2473 } 2474 2475 stack.back().GetScalar() = tls_load_addr; 2476 stack.back().SetValueType(Value::ValueType::LoadAddress); 2477 } break; 2478 2479 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.) 2480 // OPERANDS: 1 2481 // ULEB128: index to the .debug_addr section 2482 // DESCRIPTION: Pushes an address to the stack from the .debug_addr 2483 // section with the base address specified by the DW_AT_addr_base attribute 2484 // and the 0 based index is the ULEB128 encoded index. 2485 case DW_OP_addrx: 2486 case DW_OP_GNU_addr_index: { 2487 if (!dwarf_cu) { 2488 if (error_ptr) 2489 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a " 2490 "compile unit being specified"); 2491 return false; 2492 } 2493 uint64_t index = opcodes.GetULEB128(&offset); 2494 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index); 2495 stack.push_back(Scalar(value)); 2496 stack.back().SetValueType(Value::ValueType::FileAddress); 2497 } break; 2498 2499 // OPCODE: DW_OP_GNU_const_index 2500 // OPERANDS: 1 2501 // ULEB128: index to the .debug_addr section 2502 // DESCRIPTION: Pushes an constant with the size of a machine address to 2503 // the stack from the .debug_addr section with the base address specified 2504 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128 2505 // encoded index. 2506 case DW_OP_GNU_const_index: { 2507 if (!dwarf_cu) { 2508 if (error_ptr) 2509 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a " 2510 "compile unit being specified"); 2511 return false; 2512 } 2513 uint64_t index = opcodes.GetULEB128(&offset); 2514 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index); 2515 stack.push_back(Scalar(value)); 2516 } break; 2517 2518 case DW_OP_GNU_entry_value: 2519 case DW_OP_entry_value: { 2520 if (!Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, opcodes, offset, 2521 error_ptr, log)) { 2522 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", 2523 DW_OP_value_to_name(op)); 2524 return false; 2525 } 2526 break; 2527 } 2528 2529 default: 2530 if (error_ptr) 2531 error_ptr->SetErrorStringWithFormatv( 2532 "Unhandled opcode {0} in DWARFExpression", LocationAtom(op)); 2533 return false; 2534 } 2535 } 2536 2537 if (stack.empty()) { 2538 // Nothing on the stack, check if we created a piece value from DW_OP_piece 2539 // or DW_OP_bit_piece opcodes 2540 if (pieces.GetBuffer().GetByteSize()) { 2541 result = pieces; 2542 return true; 2543 } 2544 if (error_ptr) 2545 error_ptr->SetErrorString("Stack empty after evaluation."); 2546 return false; 2547 } 2548 2549 UpdateValueTypeFromLocationDescription( 2550 log, dwarf_cu, dwarf4_location_description_kind, &stack.back()); 2551 2552 if (log && log->GetVerbose()) { 2553 size_t count = stack.size(); 2554 LLDB_LOGF(log, 2555 "Stack after operation has %" PRIu64 " values:", (uint64_t)count); 2556 for (size_t i = 0; i < count; ++i) { 2557 StreamString new_value; 2558 new_value.Printf("[%" PRIu64 "]", (uint64_t)i); 2559 stack[i].Dump(&new_value); 2560 LLDB_LOGF(log, " %s", new_value.GetData()); 2561 } 2562 } 2563 result = stack.back(); 2564 return true; // Return true on success 2565 } 2566 2567 bool DWARFExpression::ParseDWARFLocationList( 2568 const DWARFUnit *dwarf_cu, const DataExtractor &data, 2569 DWARFExpressionList *location_list) { 2570 location_list->Clear(); 2571 std::unique_ptr<llvm::DWARFLocationTable> loctable_up = 2572 dwarf_cu->GetLocationTable(data); 2573 Log *log = GetLog(LLDBLog::Expressions); 2574 auto lookup_addr = 2575 [&](uint32_t index) -> llvm::Optional<llvm::object::SectionedAddress> { 2576 addr_t address = dwarf_cu->ReadAddressFromDebugAddrSection(index); 2577 if (address == LLDB_INVALID_ADDRESS) 2578 return llvm::None; 2579 return llvm::object::SectionedAddress{address}; 2580 }; 2581 auto process_list = [&](llvm::Expected<llvm::DWARFLocationExpression> loc) { 2582 if (!loc) { 2583 LLDB_LOG_ERROR(log, loc.takeError(), "{0}"); 2584 return true; 2585 } 2586 auto buffer_sp = 2587 std::make_shared<DataBufferHeap>(loc->Expr.data(), loc->Expr.size()); 2588 DWARFExpression expr = DWARFExpression(DataExtractor( 2589 buffer_sp, data.GetByteOrder(), data.GetAddressByteSize())); 2590 location_list->AddExpression(loc->Range->LowPC, loc->Range->HighPC, expr); 2591 return true; 2592 }; 2593 llvm::Error error = loctable_up->visitAbsoluteLocationList( 2594 0, llvm::object::SectionedAddress{dwarf_cu->GetBaseAddress()}, 2595 lookup_addr, process_list); 2596 location_list->Sort(); 2597 if (error) { 2598 LLDB_LOG_ERROR(log, std::move(error), "{0}"); 2599 return false; 2600 } 2601 return true; 2602 } 2603 2604 bool DWARFExpression::MatchesOperand( 2605 StackFrame &frame, const Instruction::Operand &operand) const { 2606 using namespace OperandMatchers; 2607 2608 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext(); 2609 if (!reg_ctx_sp) { 2610 return false; 2611 } 2612 2613 DataExtractor opcodes(m_data); 2614 2615 lldb::offset_t op_offset = 0; 2616 uint8_t opcode = opcodes.GetU8(&op_offset); 2617 2618 if (opcode == DW_OP_fbreg) { 2619 int64_t offset = opcodes.GetSLEB128(&op_offset); 2620 2621 DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr); 2622 if (!fb_expr) { 2623 return false; 2624 } 2625 2626 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) { 2627 return fb_expr->MatchesOperand(frame, child); 2628 }; 2629 2630 if (!offset && 2631 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), 2632 recurse)(operand)) { 2633 return true; 2634 } 2635 2636 return MatchUnaryOp( 2637 MatchOpType(Instruction::Operand::Type::Dereference), 2638 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), 2639 MatchImmOp(offset), recurse))(operand); 2640 } 2641 2642 bool dereference = false; 2643 const RegisterInfo *reg = nullptr; 2644 int64_t offset = 0; 2645 2646 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) { 2647 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0); 2648 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) { 2649 offset = opcodes.GetSLEB128(&op_offset); 2650 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0); 2651 } else if (opcode == DW_OP_regx) { 2652 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset)); 2653 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num); 2654 } else if (opcode == DW_OP_bregx) { 2655 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset)); 2656 offset = opcodes.GetSLEB128(&op_offset); 2657 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num); 2658 } else { 2659 return false; 2660 } 2661 2662 if (!reg) { 2663 return false; 2664 } 2665 2666 if (dereference) { 2667 if (!offset && 2668 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), 2669 MatchRegOp(*reg))(operand)) { 2670 return true; 2671 } 2672 2673 return MatchUnaryOp( 2674 MatchOpType(Instruction::Operand::Type::Dereference), 2675 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), 2676 MatchRegOp(*reg), 2677 MatchImmOp(offset)))(operand); 2678 } else { 2679 return MatchRegOp(*reg)(operand); 2680 } 2681 } 2682