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