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