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