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::ValueType::Scalar); 178 value.SetContext(Value::ContextType::RegisterInfo, 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 namespace { 908 /// The location description kinds described by the DWARF v5 909 /// specification. Composite locations are handled out-of-band and 910 /// thus aren't part of the enum. 911 enum LocationDescriptionKind { 912 Empty, 913 Memory, 914 Register, 915 Implicit 916 /* Composite*/ 917 }; 918 /// Adjust value's ValueType according to the kind of location description. 919 void UpdateValueTypeFromLocationDescription(Log *log, const DWARFUnit *dwarf_cu, 920 LocationDescriptionKind kind, 921 Value *value = nullptr) { 922 // Note that this function is conflating DWARF expressions with 923 // DWARF location descriptions. Perhaps it would be better to define 924 // a wrapper for DWARFExpresssion::Eval() that deals with DWARF 925 // location descriptions (which consist of one or more DWARF 926 // expressions). But doing this would mean we'd also need factor the 927 // handling of DW_OP_(bit_)piece out of this function. 928 if (dwarf_cu && dwarf_cu->GetVersion() >= 4) { 929 const char *log_msg = "DWARF location description kind: %s"; 930 switch (kind) { 931 case Empty: 932 LLDB_LOGF(log, log_msg, "Empty"); 933 break; 934 case Memory: 935 LLDB_LOGF(log, log_msg, "Memory"); 936 if (value->GetValueType() == Value::ValueType::Scalar) 937 value->SetValueType(Value::ValueType::LoadAddress); 938 break; 939 case Register: 940 LLDB_LOGF(log, log_msg, "Register"); 941 value->SetValueType(Value::ValueType::Scalar); 942 break; 943 case Implicit: 944 LLDB_LOGF(log, log_msg, "Implicit"); 945 if (value->GetValueType() == Value::ValueType::LoadAddress) 946 value->SetValueType(Value::ValueType::Scalar); 947 break; 948 } 949 } 950 } 951 } // namespace 952 953 bool DWARFExpression::Evaluate( 954 ExecutionContext *exe_ctx, RegisterContext *reg_ctx, 955 lldb::ModuleSP module_sp, const DataExtractor &opcodes, 956 const DWARFUnit *dwarf_cu, const lldb::RegisterKind reg_kind, 957 const Value *initial_value_ptr, const Value *object_address_ptr, 958 Value &result, Status *error_ptr) { 959 960 if (opcodes.GetByteSize() == 0) { 961 if (error_ptr) 962 error_ptr->SetErrorString( 963 "no location, value may have been optimized out"); 964 return false; 965 } 966 std::vector<Value> stack; 967 968 Process *process = nullptr; 969 StackFrame *frame = nullptr; 970 971 if (exe_ctx) { 972 process = exe_ctx->GetProcessPtr(); 973 frame = exe_ctx->GetFramePtr(); 974 } 975 if (reg_ctx == nullptr && frame) 976 reg_ctx = frame->GetRegisterContext().get(); 977 978 if (initial_value_ptr) 979 stack.push_back(*initial_value_ptr); 980 981 lldb::offset_t offset = 0; 982 Value tmp; 983 uint32_t reg_num; 984 985 /// Insertion point for evaluating multi-piece expression. 986 uint64_t op_piece_offset = 0; 987 Value pieces; // Used for DW_OP_piece 988 989 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 990 // A generic type is "an integral type that has the size of an address and an 991 // unspecified signedness". For now, just use the signedness of the operand. 992 // TODO: Implement a real typed stack, and store the genericness of the value 993 // there. 994 auto to_generic = [&](auto v) { 995 bool is_signed = std::is_signed<decltype(v)>::value; 996 return Scalar(llvm::APSInt( 997 llvm::APInt(8 * opcodes.GetAddressByteSize(), v, is_signed), 998 !is_signed)); 999 }; 1000 1001 // The default kind is a memory location. This is updated by any 1002 // operation that changes this, such as DW_OP_stack_value, and reset 1003 // by composition operations like DW_OP_piece. 1004 LocationDescriptionKind dwarf4_location_description_kind = Memory; 1005 1006 while (opcodes.ValidOffset(offset)) { 1007 const lldb::offset_t op_offset = offset; 1008 const uint8_t op = opcodes.GetU8(&offset); 1009 1010 if (log && log->GetVerbose()) { 1011 size_t count = stack.size(); 1012 LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:", 1013 (uint64_t)count); 1014 for (size_t i = 0; i < count; ++i) { 1015 StreamString new_value; 1016 new_value.Printf("[%" PRIu64 "]", (uint64_t)i); 1017 stack[i].Dump(&new_value); 1018 LLDB_LOGF(log, " %s", new_value.GetData()); 1019 } 1020 LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset, 1021 DW_OP_value_to_name(op)); 1022 } 1023 1024 switch (op) { 1025 // The DW_OP_addr operation has a single operand that encodes a machine 1026 // address and whose size is the size of an address on the target machine. 1027 case DW_OP_addr: 1028 stack.push_back(Scalar(opcodes.GetAddress(&offset))); 1029 stack.back().SetValueType(Value::ValueType::FileAddress); 1030 // Convert the file address to a load address, so subsequent 1031 // DWARF operators can operate on it. 1032 if (frame) 1033 stack.back().ConvertToLoadAddress(module_sp.get(), 1034 frame->CalculateTarget().get()); 1035 break; 1036 1037 // The DW_OP_addr_sect_offset4 is used for any location expressions in 1038 // shared libraries that have a location like: 1039 // DW_OP_addr(0x1000) 1040 // If this address resides in a shared library, then this virtual address 1041 // won't make sense when it is evaluated in the context of a running 1042 // process where shared libraries have been slid. To account for this, this 1043 // new address type where we can store the section pointer and a 4 byte 1044 // offset. 1045 // case DW_OP_addr_sect_offset4: 1046 // { 1047 // result_type = eResultTypeFileAddress; 1048 // lldb::Section *sect = (lldb::Section 1049 // *)opcodes.GetMaxU64(&offset, sizeof(void *)); 1050 // lldb::addr_t sect_offset = opcodes.GetU32(&offset); 1051 // 1052 // Address so_addr (sect, sect_offset); 1053 // lldb::addr_t load_addr = so_addr.GetLoadAddress(); 1054 // if (load_addr != LLDB_INVALID_ADDRESS) 1055 // { 1056 // // We successfully resolve a file address to a load 1057 // // address. 1058 // stack.push_back(load_addr); 1059 // break; 1060 // } 1061 // else 1062 // { 1063 // // We were able 1064 // if (error_ptr) 1065 // error_ptr->SetErrorStringWithFormat ("Section %s in 1066 // %s is not currently loaded.\n", 1067 // sect->GetName().AsCString(), 1068 // sect->GetModule()->GetFileSpec().GetFilename().AsCString()); 1069 // return false; 1070 // } 1071 // } 1072 // break; 1073 1074 // OPCODE: DW_OP_deref 1075 // OPERANDS: none 1076 // DESCRIPTION: Pops the top stack entry and treats it as an address. 1077 // The value retrieved from that address is pushed. The size of the data 1078 // retrieved from the dereferenced address is the size of an address on the 1079 // target machine. 1080 case DW_OP_deref: { 1081 if (stack.empty()) { 1082 if (error_ptr) 1083 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref."); 1084 return false; 1085 } 1086 Value::ValueType value_type = stack.back().GetValueType(); 1087 switch (value_type) { 1088 case Value::ValueType::HostAddress: { 1089 void *src = (void *)stack.back().GetScalar().ULongLong(); 1090 intptr_t ptr; 1091 ::memcpy(&ptr, src, sizeof(void *)); 1092 stack.back().GetScalar() = ptr; 1093 stack.back().ClearContext(); 1094 } break; 1095 case Value::ValueType::FileAddress: { 1096 auto file_addr = stack.back().GetScalar().ULongLong( 1097 LLDB_INVALID_ADDRESS); 1098 if (!module_sp) { 1099 if (error_ptr) 1100 error_ptr->SetErrorString( 1101 "need module to resolve file address for DW_OP_deref"); 1102 return false; 1103 } 1104 Address so_addr; 1105 if (!module_sp->ResolveFileAddress(file_addr, so_addr)) { 1106 if (error_ptr) 1107 error_ptr->SetErrorString( 1108 "failed to resolve file address in module"); 1109 return false; 1110 } 1111 addr_t load_Addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr()); 1112 if (load_Addr == LLDB_INVALID_ADDRESS) { 1113 if (error_ptr) 1114 error_ptr->SetErrorString("failed to resolve load address"); 1115 return false; 1116 } 1117 stack.back().GetScalar() = load_Addr; 1118 // Fall through to load address promotion code below. 1119 } LLVM_FALLTHROUGH; 1120 case Value::ValueType::Scalar: 1121 // Promote Scalar to LoadAddress and fall through. 1122 stack.back().SetValueType(Value::ValueType::LoadAddress); 1123 LLVM_FALLTHROUGH; 1124 case Value::ValueType::LoadAddress: 1125 if (exe_ctx) { 1126 if (process) { 1127 lldb::addr_t pointer_addr = 1128 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1129 Status error; 1130 lldb::addr_t pointer_value = 1131 process->ReadPointerFromMemory(pointer_addr, error); 1132 if (pointer_value != LLDB_INVALID_ADDRESS) { 1133 stack.back().GetScalar() = pointer_value; 1134 stack.back().ClearContext(); 1135 } else { 1136 if (error_ptr) 1137 error_ptr->SetErrorStringWithFormat( 1138 "Failed to dereference pointer from 0x%" PRIx64 1139 " for DW_OP_deref: %s\n", 1140 pointer_addr, error.AsCString()); 1141 return false; 1142 } 1143 } else { 1144 if (error_ptr) 1145 error_ptr->SetErrorString("NULL process for DW_OP_deref.\n"); 1146 return false; 1147 } 1148 } else { 1149 if (error_ptr) 1150 error_ptr->SetErrorString( 1151 "NULL execution context for DW_OP_deref.\n"); 1152 return false; 1153 } 1154 break; 1155 1156 case Value::ValueType::Invalid: 1157 if (error_ptr) 1158 error_ptr->SetErrorString("Invalid value type for DW_OP_deref.\n"); 1159 return false; 1160 } 1161 1162 } break; 1163 1164 // OPCODE: DW_OP_deref_size 1165 // OPERANDS: 1 1166 // 1 - uint8_t that specifies the size of the data to dereference. 1167 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top 1168 // stack entry and treats it as an address. The value retrieved from that 1169 // address is pushed. In the DW_OP_deref_size operation, however, the size 1170 // in bytes of the data retrieved from the dereferenced address is 1171 // specified by the single operand. This operand is a 1-byte unsigned 1172 // integral constant whose value may not be larger than the size of an 1173 // address on the target machine. The data retrieved is zero extended to 1174 // the size of an address on the target machine before being pushed on the 1175 // expression stack. 1176 case DW_OP_deref_size: { 1177 if (stack.empty()) { 1178 if (error_ptr) 1179 error_ptr->SetErrorString( 1180 "Expression stack empty for DW_OP_deref_size."); 1181 return false; 1182 } 1183 uint8_t size = opcodes.GetU8(&offset); 1184 Value::ValueType value_type = stack.back().GetValueType(); 1185 switch (value_type) { 1186 case Value::ValueType::HostAddress: { 1187 void *src = (void *)stack.back().GetScalar().ULongLong(); 1188 intptr_t ptr; 1189 ::memcpy(&ptr, src, sizeof(void *)); 1190 // I can't decide whether the size operand should apply to the bytes in 1191 // their 1192 // lldb-host endianness or the target endianness.. I doubt this'll ever 1193 // come up but I'll opt for assuming big endian regardless. 1194 switch (size) { 1195 case 1: 1196 ptr = ptr & 0xff; 1197 break; 1198 case 2: 1199 ptr = ptr & 0xffff; 1200 break; 1201 case 3: 1202 ptr = ptr & 0xffffff; 1203 break; 1204 case 4: 1205 ptr = ptr & 0xffffffff; 1206 break; 1207 // the casts are added to work around the case where intptr_t is a 32 1208 // bit quantity; 1209 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this 1210 // program. 1211 case 5: 1212 ptr = (intptr_t)ptr & 0xffffffffffULL; 1213 break; 1214 case 6: 1215 ptr = (intptr_t)ptr & 0xffffffffffffULL; 1216 break; 1217 case 7: 1218 ptr = (intptr_t)ptr & 0xffffffffffffffULL; 1219 break; 1220 default: 1221 break; 1222 } 1223 stack.back().GetScalar() = ptr; 1224 stack.back().ClearContext(); 1225 } break; 1226 case Value::ValueType::Scalar: 1227 case Value::ValueType::LoadAddress: 1228 if (exe_ctx) { 1229 if (process) { 1230 lldb::addr_t pointer_addr = 1231 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1232 uint8_t addr_bytes[sizeof(lldb::addr_t)]; 1233 Status error; 1234 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) == 1235 size) { 1236 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), 1237 process->GetByteOrder(), size); 1238 lldb::offset_t addr_data_offset = 0; 1239 switch (size) { 1240 case 1: 1241 stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset); 1242 break; 1243 case 2: 1244 stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset); 1245 break; 1246 case 4: 1247 stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset); 1248 break; 1249 case 8: 1250 stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset); 1251 break; 1252 default: 1253 stack.back().GetScalar() = 1254 addr_data.GetAddress(&addr_data_offset); 1255 } 1256 stack.back().ClearContext(); 1257 } else { 1258 if (error_ptr) 1259 error_ptr->SetErrorStringWithFormat( 1260 "Failed to dereference pointer from 0x%" PRIx64 1261 " for DW_OP_deref: %s\n", 1262 pointer_addr, error.AsCString()); 1263 return false; 1264 } 1265 } else { 1266 if (error_ptr) 1267 error_ptr->SetErrorString("NULL process for DW_OP_deref_size.\n"); 1268 return false; 1269 } 1270 } else { 1271 if (error_ptr) 1272 error_ptr->SetErrorString( 1273 "NULL execution context for DW_OP_deref_size.\n"); 1274 return false; 1275 } 1276 break; 1277 1278 case Value::ValueType::FileAddress: 1279 case Value::ValueType::Invalid: 1280 if (error_ptr) 1281 error_ptr->SetErrorString("Invalid value for DW_OP_deref_size.\n"); 1282 return false; 1283 } 1284 1285 } break; 1286 1287 // OPCODE: DW_OP_xderef_size 1288 // OPERANDS: 1 1289 // 1 - uint8_t that specifies the size of the data to dereference. 1290 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at 1291 // the top of the stack is treated as an address. The second stack entry is 1292 // treated as an "address space identifier" for those architectures that 1293 // support multiple address spaces. The top two stack elements are popped, 1294 // a data item is retrieved through an implementation-defined address 1295 // calculation and pushed as the new stack top. In the DW_OP_xderef_size 1296 // operation, however, the size in bytes of the data retrieved from the 1297 // dereferenced address is specified by the single operand. This operand is 1298 // a 1-byte unsigned integral constant whose value may not be larger than 1299 // the size of an address on the target machine. The data retrieved is zero 1300 // extended to the size of an address on the target machine before being 1301 // pushed on the expression stack. 1302 case DW_OP_xderef_size: 1303 if (error_ptr) 1304 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size."); 1305 return false; 1306 // OPCODE: DW_OP_xderef 1307 // OPERANDS: none 1308 // DESCRIPTION: Provides an extended dereference mechanism. The entry at 1309 // the top of the stack is treated as an address. The second stack entry is 1310 // treated as an "address space identifier" for those architectures that 1311 // support multiple address spaces. The top two stack elements are popped, 1312 // a data item is retrieved through an implementation-defined address 1313 // calculation and pushed as the new stack top. The size of the data 1314 // retrieved from the dereferenced address is the size of an address on the 1315 // target machine. 1316 case DW_OP_xderef: 1317 if (error_ptr) 1318 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef."); 1319 return false; 1320 1321 // All DW_OP_constXXX opcodes have a single operand as noted below: 1322 // 1323 // Opcode Operand 1 1324 // DW_OP_const1u 1-byte unsigned integer constant 1325 // DW_OP_const1s 1-byte signed integer constant 1326 // DW_OP_const2u 2-byte unsigned integer constant 1327 // DW_OP_const2s 2-byte signed integer constant 1328 // DW_OP_const4u 4-byte unsigned integer constant 1329 // DW_OP_const4s 4-byte signed integer constant 1330 // DW_OP_const8u 8-byte unsigned integer constant 1331 // DW_OP_const8s 8-byte signed integer constant 1332 // DW_OP_constu unsigned LEB128 integer constant 1333 // DW_OP_consts signed LEB128 integer constant 1334 case DW_OP_const1u: 1335 stack.push_back(to_generic(opcodes.GetU8(&offset))); 1336 break; 1337 case DW_OP_const1s: 1338 stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset))); 1339 break; 1340 case DW_OP_const2u: 1341 stack.push_back(to_generic(opcodes.GetU16(&offset))); 1342 break; 1343 case DW_OP_const2s: 1344 stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset))); 1345 break; 1346 case DW_OP_const4u: 1347 stack.push_back(to_generic(opcodes.GetU32(&offset))); 1348 break; 1349 case DW_OP_const4s: 1350 stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset))); 1351 break; 1352 case DW_OP_const8u: 1353 stack.push_back(to_generic(opcodes.GetU64(&offset))); 1354 break; 1355 case DW_OP_const8s: 1356 stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset))); 1357 break; 1358 // These should also use to_generic, but we can't do that due to a 1359 // producer-side bug in llvm. See llvm.org/pr48087. 1360 case DW_OP_constu: 1361 stack.push_back(Scalar(opcodes.GetULEB128(&offset))); 1362 break; 1363 case DW_OP_consts: 1364 stack.push_back(Scalar(opcodes.GetSLEB128(&offset))); 1365 break; 1366 1367 // OPCODE: DW_OP_dup 1368 // OPERANDS: none 1369 // DESCRIPTION: duplicates the value at the top of the stack 1370 case DW_OP_dup: 1371 if (stack.empty()) { 1372 if (error_ptr) 1373 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup."); 1374 return false; 1375 } else 1376 stack.push_back(stack.back()); 1377 break; 1378 1379 // OPCODE: DW_OP_drop 1380 // OPERANDS: none 1381 // DESCRIPTION: pops the value at the top of the stack 1382 case DW_OP_drop: 1383 if (stack.empty()) { 1384 if (error_ptr) 1385 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop."); 1386 return false; 1387 } else 1388 stack.pop_back(); 1389 break; 1390 1391 // OPCODE: DW_OP_over 1392 // OPERANDS: none 1393 // DESCRIPTION: Duplicates the entry currently second in the stack at 1394 // the top of the stack. 1395 case DW_OP_over: 1396 if (stack.size() < 2) { 1397 if (error_ptr) 1398 error_ptr->SetErrorString( 1399 "Expression stack needs at least 2 items for DW_OP_over."); 1400 return false; 1401 } else 1402 stack.push_back(stack[stack.size() - 2]); 1403 break; 1404 1405 // OPCODE: DW_OP_pick 1406 // OPERANDS: uint8_t index into the current stack 1407 // DESCRIPTION: The stack entry with the specified index (0 through 255, 1408 // inclusive) is pushed on the stack 1409 case DW_OP_pick: { 1410 uint8_t pick_idx = opcodes.GetU8(&offset); 1411 if (pick_idx < stack.size()) 1412 stack.push_back(stack[stack.size() - 1 - pick_idx]); 1413 else { 1414 if (error_ptr) 1415 error_ptr->SetErrorStringWithFormat( 1416 "Index %u out of range for DW_OP_pick.\n", pick_idx); 1417 return false; 1418 } 1419 } break; 1420 1421 // OPCODE: DW_OP_swap 1422 // OPERANDS: none 1423 // DESCRIPTION: swaps the top two stack entries. The entry at the top 1424 // of the stack becomes the second stack entry, and the second entry 1425 // becomes the top of the stack 1426 case DW_OP_swap: 1427 if (stack.size() < 2) { 1428 if (error_ptr) 1429 error_ptr->SetErrorString( 1430 "Expression stack needs at least 2 items for DW_OP_swap."); 1431 return false; 1432 } else { 1433 tmp = stack.back(); 1434 stack.back() = stack[stack.size() - 2]; 1435 stack[stack.size() - 2] = tmp; 1436 } 1437 break; 1438 1439 // OPCODE: DW_OP_rot 1440 // OPERANDS: none 1441 // DESCRIPTION: Rotates the first three stack entries. The entry at 1442 // the top of the stack becomes the third stack entry, the second entry 1443 // becomes the top of the stack, and the third entry becomes the second 1444 // entry. 1445 case DW_OP_rot: 1446 if (stack.size() < 3) { 1447 if (error_ptr) 1448 error_ptr->SetErrorString( 1449 "Expression stack needs at least 3 items for DW_OP_rot."); 1450 return false; 1451 } else { 1452 size_t last_idx = stack.size() - 1; 1453 Value old_top = stack[last_idx]; 1454 stack[last_idx] = stack[last_idx - 1]; 1455 stack[last_idx - 1] = stack[last_idx - 2]; 1456 stack[last_idx - 2] = old_top; 1457 } 1458 break; 1459 1460 // OPCODE: DW_OP_abs 1461 // OPERANDS: none 1462 // DESCRIPTION: pops the top stack entry, interprets it as a signed 1463 // value and pushes its absolute value. If the absolute value can not be 1464 // represented, the result is undefined. 1465 case DW_OP_abs: 1466 if (stack.empty()) { 1467 if (error_ptr) 1468 error_ptr->SetErrorString( 1469 "Expression stack needs at least 1 item for DW_OP_abs."); 1470 return false; 1471 } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) { 1472 if (error_ptr) 1473 error_ptr->SetErrorString( 1474 "Failed to take the absolute value of the first stack item."); 1475 return false; 1476 } 1477 break; 1478 1479 // OPCODE: DW_OP_and 1480 // OPERANDS: none 1481 // DESCRIPTION: pops the top two stack values, performs a bitwise and 1482 // operation on the two, and pushes the result. 1483 case DW_OP_and: 1484 if (stack.size() < 2) { 1485 if (error_ptr) 1486 error_ptr->SetErrorString( 1487 "Expression stack needs at least 2 items for DW_OP_and."); 1488 return false; 1489 } else { 1490 tmp = stack.back(); 1491 stack.pop_back(); 1492 stack.back().ResolveValue(exe_ctx) = 1493 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx); 1494 } 1495 break; 1496 1497 // OPCODE: DW_OP_div 1498 // OPERANDS: none 1499 // DESCRIPTION: pops the top two stack values, divides the former second 1500 // entry by the former top of the stack using signed division, and pushes 1501 // the result. 1502 case DW_OP_div: 1503 if (stack.size() < 2) { 1504 if (error_ptr) 1505 error_ptr->SetErrorString( 1506 "Expression stack needs at least 2 items for DW_OP_div."); 1507 return false; 1508 } else { 1509 tmp = stack.back(); 1510 if (tmp.ResolveValue(exe_ctx).IsZero()) { 1511 if (error_ptr) 1512 error_ptr->SetErrorString("Divide by zero."); 1513 return false; 1514 } else { 1515 stack.pop_back(); 1516 stack.back() = 1517 stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx); 1518 if (!stack.back().ResolveValue(exe_ctx).IsValid()) { 1519 if (error_ptr) 1520 error_ptr->SetErrorString("Divide failed."); 1521 return false; 1522 } 1523 } 1524 } 1525 break; 1526 1527 // OPCODE: DW_OP_minus 1528 // OPERANDS: none 1529 // DESCRIPTION: pops the top two stack values, subtracts the former top 1530 // of the stack from the former second entry, and pushes the result. 1531 case DW_OP_minus: 1532 if (stack.size() < 2) { 1533 if (error_ptr) 1534 error_ptr->SetErrorString( 1535 "Expression stack needs at least 2 items for DW_OP_minus."); 1536 return false; 1537 } else { 1538 tmp = stack.back(); 1539 stack.pop_back(); 1540 stack.back().ResolveValue(exe_ctx) = 1541 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx); 1542 } 1543 break; 1544 1545 // OPCODE: DW_OP_mod 1546 // OPERANDS: none 1547 // DESCRIPTION: pops the top two stack values and pushes the result of 1548 // the calculation: former second stack entry modulo the former top of the 1549 // stack. 1550 case DW_OP_mod: 1551 if (stack.size() < 2) { 1552 if (error_ptr) 1553 error_ptr->SetErrorString( 1554 "Expression stack needs at least 2 items for DW_OP_mod."); 1555 return false; 1556 } else { 1557 tmp = stack.back(); 1558 stack.pop_back(); 1559 stack.back().ResolveValue(exe_ctx) = 1560 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx); 1561 } 1562 break; 1563 1564 // OPCODE: DW_OP_mul 1565 // OPERANDS: none 1566 // DESCRIPTION: pops the top two stack entries, multiplies them 1567 // together, and pushes the result. 1568 case DW_OP_mul: 1569 if (stack.size() < 2) { 1570 if (error_ptr) 1571 error_ptr->SetErrorString( 1572 "Expression stack needs at least 2 items for DW_OP_mul."); 1573 return false; 1574 } else { 1575 tmp = stack.back(); 1576 stack.pop_back(); 1577 stack.back().ResolveValue(exe_ctx) = 1578 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx); 1579 } 1580 break; 1581 1582 // OPCODE: DW_OP_neg 1583 // OPERANDS: none 1584 // DESCRIPTION: pops the top stack entry, and pushes its negation. 1585 case DW_OP_neg: 1586 if (stack.empty()) { 1587 if (error_ptr) 1588 error_ptr->SetErrorString( 1589 "Expression stack needs at least 1 item for DW_OP_neg."); 1590 return false; 1591 } else { 1592 if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) { 1593 if (error_ptr) 1594 error_ptr->SetErrorString("Unary negate failed."); 1595 return false; 1596 } 1597 } 1598 break; 1599 1600 // OPCODE: DW_OP_not 1601 // OPERANDS: none 1602 // DESCRIPTION: pops the top stack entry, and pushes its bitwise 1603 // complement 1604 case DW_OP_not: 1605 if (stack.empty()) { 1606 if (error_ptr) 1607 error_ptr->SetErrorString( 1608 "Expression stack needs at least 1 item for DW_OP_not."); 1609 return false; 1610 } else { 1611 if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) { 1612 if (error_ptr) 1613 error_ptr->SetErrorString("Logical NOT failed."); 1614 return false; 1615 } 1616 } 1617 break; 1618 1619 // OPCODE: DW_OP_or 1620 // OPERANDS: none 1621 // DESCRIPTION: pops the top two stack entries, performs a bitwise or 1622 // operation on the two, and pushes the result. 1623 case DW_OP_or: 1624 if (stack.size() < 2) { 1625 if (error_ptr) 1626 error_ptr->SetErrorString( 1627 "Expression stack needs at least 2 items for DW_OP_or."); 1628 return false; 1629 } else { 1630 tmp = stack.back(); 1631 stack.pop_back(); 1632 stack.back().ResolveValue(exe_ctx) = 1633 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx); 1634 } 1635 break; 1636 1637 // OPCODE: DW_OP_plus 1638 // OPERANDS: none 1639 // DESCRIPTION: pops the top two stack entries, adds them together, and 1640 // pushes the result. 1641 case DW_OP_plus: 1642 if (stack.size() < 2) { 1643 if (error_ptr) 1644 error_ptr->SetErrorString( 1645 "Expression stack needs at least 2 items for DW_OP_plus."); 1646 return false; 1647 } else { 1648 tmp = stack.back(); 1649 stack.pop_back(); 1650 stack.back().GetScalar() += tmp.GetScalar(); 1651 } 1652 break; 1653 1654 // OPCODE: DW_OP_plus_uconst 1655 // OPERANDS: none 1656 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128 1657 // constant operand and pushes the result. 1658 case DW_OP_plus_uconst: 1659 if (stack.empty()) { 1660 if (error_ptr) 1661 error_ptr->SetErrorString( 1662 "Expression stack needs at least 1 item for DW_OP_plus_uconst."); 1663 return false; 1664 } else { 1665 const uint64_t uconst_value = opcodes.GetULEB128(&offset); 1666 // Implicit conversion from a UINT to a Scalar... 1667 stack.back().GetScalar() += uconst_value; 1668 if (!stack.back().GetScalar().IsValid()) { 1669 if (error_ptr) 1670 error_ptr->SetErrorString("DW_OP_plus_uconst failed."); 1671 return false; 1672 } 1673 } 1674 break; 1675 1676 // OPCODE: DW_OP_shl 1677 // OPERANDS: none 1678 // DESCRIPTION: pops the top two stack entries, shifts the former 1679 // second entry left by the number of bits specified by the former top of 1680 // the stack, and pushes the result. 1681 case DW_OP_shl: 1682 if (stack.size() < 2) { 1683 if (error_ptr) 1684 error_ptr->SetErrorString( 1685 "Expression stack needs at least 2 items for DW_OP_shl."); 1686 return false; 1687 } else { 1688 tmp = stack.back(); 1689 stack.pop_back(); 1690 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx); 1691 } 1692 break; 1693 1694 // OPCODE: DW_OP_shr 1695 // OPERANDS: none 1696 // DESCRIPTION: pops the top two stack entries, shifts the former second 1697 // entry right logically (filling with zero bits) by the number of bits 1698 // specified by the former top of the stack, and pushes the result. 1699 case DW_OP_shr: 1700 if (stack.size() < 2) { 1701 if (error_ptr) 1702 error_ptr->SetErrorString( 1703 "Expression stack needs at least 2 items for DW_OP_shr."); 1704 return false; 1705 } else { 1706 tmp = stack.back(); 1707 stack.pop_back(); 1708 if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical( 1709 tmp.ResolveValue(exe_ctx))) { 1710 if (error_ptr) 1711 error_ptr->SetErrorString("DW_OP_shr failed."); 1712 return false; 1713 } 1714 } 1715 break; 1716 1717 // OPCODE: DW_OP_shra 1718 // OPERANDS: none 1719 // DESCRIPTION: pops the top two stack entries, shifts the former second 1720 // entry right arithmetically (divide the magnitude by 2, keep the same 1721 // sign for the result) by the number of bits specified by the former top 1722 // of the stack, and pushes the result. 1723 case DW_OP_shra: 1724 if (stack.size() < 2) { 1725 if (error_ptr) 1726 error_ptr->SetErrorString( 1727 "Expression stack needs at least 2 items for DW_OP_shra."); 1728 return false; 1729 } else { 1730 tmp = stack.back(); 1731 stack.pop_back(); 1732 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx); 1733 } 1734 break; 1735 1736 // OPCODE: DW_OP_xor 1737 // OPERANDS: none 1738 // DESCRIPTION: pops the top two stack entries, performs the bitwise 1739 // exclusive-or operation on the two, and pushes the result. 1740 case DW_OP_xor: 1741 if (stack.size() < 2) { 1742 if (error_ptr) 1743 error_ptr->SetErrorString( 1744 "Expression stack needs at least 2 items for DW_OP_xor."); 1745 return false; 1746 } else { 1747 tmp = stack.back(); 1748 stack.pop_back(); 1749 stack.back().ResolveValue(exe_ctx) = 1750 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx); 1751 } 1752 break; 1753 1754 // OPCODE: DW_OP_skip 1755 // OPERANDS: int16_t 1756 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte 1757 // signed integer constant. The 2-byte constant is the number of bytes of 1758 // the DWARF expression to skip forward or backward from the current 1759 // operation, beginning after the 2-byte constant. 1760 case DW_OP_skip: { 1761 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset); 1762 lldb::offset_t new_offset = offset + skip_offset; 1763 if (opcodes.ValidOffset(new_offset)) 1764 offset = new_offset; 1765 else { 1766 if (error_ptr) 1767 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip."); 1768 return false; 1769 } 1770 } break; 1771 1772 // OPCODE: DW_OP_bra 1773 // OPERANDS: int16_t 1774 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte 1775 // signed integer constant. This operation pops the top of stack. If the 1776 // value popped is not the constant 0, the 2-byte constant operand is the 1777 // number of bytes of the DWARF expression to skip forward or backward from 1778 // the current operation, beginning after the 2-byte constant. 1779 case DW_OP_bra: 1780 if (stack.empty()) { 1781 if (error_ptr) 1782 error_ptr->SetErrorString( 1783 "Expression stack needs at least 1 item for DW_OP_bra."); 1784 return false; 1785 } else { 1786 tmp = stack.back(); 1787 stack.pop_back(); 1788 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset); 1789 Scalar zero(0); 1790 if (tmp.ResolveValue(exe_ctx) != zero) { 1791 lldb::offset_t new_offset = offset + bra_offset; 1792 if (opcodes.ValidOffset(new_offset)) 1793 offset = new_offset; 1794 else { 1795 if (error_ptr) 1796 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra."); 1797 return false; 1798 } 1799 } 1800 } 1801 break; 1802 1803 // OPCODE: DW_OP_eq 1804 // OPERANDS: none 1805 // DESCRIPTION: pops the top two stack values, compares using the 1806 // equals (==) operator. 1807 // STACK RESULT: push the constant value 1 onto the stack if the result 1808 // of the operation is true or the constant value 0 if the result of the 1809 // operation is false. 1810 case DW_OP_eq: 1811 if (stack.size() < 2) { 1812 if (error_ptr) 1813 error_ptr->SetErrorString( 1814 "Expression stack needs at least 2 items for DW_OP_eq."); 1815 return false; 1816 } else { 1817 tmp = stack.back(); 1818 stack.pop_back(); 1819 stack.back().ResolveValue(exe_ctx) = 1820 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx); 1821 } 1822 break; 1823 1824 // OPCODE: DW_OP_ge 1825 // OPERANDS: none 1826 // DESCRIPTION: pops the top two stack values, compares using the 1827 // greater than or equal to (>=) operator. 1828 // STACK RESULT: push the constant value 1 onto the stack if the result 1829 // of the operation is true or the constant value 0 if the result of the 1830 // operation is false. 1831 case DW_OP_ge: 1832 if (stack.size() < 2) { 1833 if (error_ptr) 1834 error_ptr->SetErrorString( 1835 "Expression stack needs at least 2 items for DW_OP_ge."); 1836 return false; 1837 } else { 1838 tmp = stack.back(); 1839 stack.pop_back(); 1840 stack.back().ResolveValue(exe_ctx) = 1841 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx); 1842 } 1843 break; 1844 1845 // OPCODE: DW_OP_gt 1846 // OPERANDS: none 1847 // DESCRIPTION: pops the top two stack values, compares using the 1848 // greater than (>) operator. 1849 // STACK RESULT: push the constant value 1 onto the stack if the result 1850 // of the operation is true or the constant value 0 if the result of the 1851 // operation is false. 1852 case DW_OP_gt: 1853 if (stack.size() < 2) { 1854 if (error_ptr) 1855 error_ptr->SetErrorString( 1856 "Expression stack needs at least 2 items for DW_OP_gt."); 1857 return false; 1858 } else { 1859 tmp = stack.back(); 1860 stack.pop_back(); 1861 stack.back().ResolveValue(exe_ctx) = 1862 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx); 1863 } 1864 break; 1865 1866 // OPCODE: DW_OP_le 1867 // OPERANDS: none 1868 // DESCRIPTION: pops the top two stack values, compares using the 1869 // less than or equal to (<=) operator. 1870 // STACK RESULT: push the constant value 1 onto the stack if the result 1871 // of the operation is true or the constant value 0 if the result of the 1872 // operation is false. 1873 case DW_OP_le: 1874 if (stack.size() < 2) { 1875 if (error_ptr) 1876 error_ptr->SetErrorString( 1877 "Expression stack needs at least 2 items for DW_OP_le."); 1878 return false; 1879 } else { 1880 tmp = stack.back(); 1881 stack.pop_back(); 1882 stack.back().ResolveValue(exe_ctx) = 1883 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx); 1884 } 1885 break; 1886 1887 // OPCODE: DW_OP_lt 1888 // OPERANDS: none 1889 // DESCRIPTION: pops the top two stack values, compares using the 1890 // less than (<) operator. 1891 // STACK RESULT: push the constant value 1 onto the stack if the result 1892 // of the operation is true or the constant value 0 if the result of the 1893 // operation is false. 1894 case DW_OP_lt: 1895 if (stack.size() < 2) { 1896 if (error_ptr) 1897 error_ptr->SetErrorString( 1898 "Expression stack needs at least 2 items for DW_OP_lt."); 1899 return false; 1900 } else { 1901 tmp = stack.back(); 1902 stack.pop_back(); 1903 stack.back().ResolveValue(exe_ctx) = 1904 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx); 1905 } 1906 break; 1907 1908 // OPCODE: DW_OP_ne 1909 // OPERANDS: none 1910 // DESCRIPTION: pops the top two stack values, compares using the 1911 // not equal (!=) operator. 1912 // STACK RESULT: push the constant value 1 onto the stack if the result 1913 // of the operation is true or the constant value 0 if the result of the 1914 // operation is false. 1915 case DW_OP_ne: 1916 if (stack.size() < 2) { 1917 if (error_ptr) 1918 error_ptr->SetErrorString( 1919 "Expression stack needs at least 2 items for DW_OP_ne."); 1920 return false; 1921 } else { 1922 tmp = stack.back(); 1923 stack.pop_back(); 1924 stack.back().ResolveValue(exe_ctx) = 1925 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx); 1926 } 1927 break; 1928 1929 // OPCODE: DW_OP_litn 1930 // OPERANDS: none 1931 // DESCRIPTION: encode the unsigned literal values from 0 through 31. 1932 // STACK RESULT: push the unsigned literal constant value onto the top 1933 // of the stack. 1934 case DW_OP_lit0: 1935 case DW_OP_lit1: 1936 case DW_OP_lit2: 1937 case DW_OP_lit3: 1938 case DW_OP_lit4: 1939 case DW_OP_lit5: 1940 case DW_OP_lit6: 1941 case DW_OP_lit7: 1942 case DW_OP_lit8: 1943 case DW_OP_lit9: 1944 case DW_OP_lit10: 1945 case DW_OP_lit11: 1946 case DW_OP_lit12: 1947 case DW_OP_lit13: 1948 case DW_OP_lit14: 1949 case DW_OP_lit15: 1950 case DW_OP_lit16: 1951 case DW_OP_lit17: 1952 case DW_OP_lit18: 1953 case DW_OP_lit19: 1954 case DW_OP_lit20: 1955 case DW_OP_lit21: 1956 case DW_OP_lit22: 1957 case DW_OP_lit23: 1958 case DW_OP_lit24: 1959 case DW_OP_lit25: 1960 case DW_OP_lit26: 1961 case DW_OP_lit27: 1962 case DW_OP_lit28: 1963 case DW_OP_lit29: 1964 case DW_OP_lit30: 1965 case DW_OP_lit31: 1966 stack.push_back(to_generic(op - DW_OP_lit0)); 1967 break; 1968 1969 // OPCODE: DW_OP_regN 1970 // OPERANDS: none 1971 // DESCRIPTION: Push the value in register n on the top of the stack. 1972 case DW_OP_reg0: 1973 case DW_OP_reg1: 1974 case DW_OP_reg2: 1975 case DW_OP_reg3: 1976 case DW_OP_reg4: 1977 case DW_OP_reg5: 1978 case DW_OP_reg6: 1979 case DW_OP_reg7: 1980 case DW_OP_reg8: 1981 case DW_OP_reg9: 1982 case DW_OP_reg10: 1983 case DW_OP_reg11: 1984 case DW_OP_reg12: 1985 case DW_OP_reg13: 1986 case DW_OP_reg14: 1987 case DW_OP_reg15: 1988 case DW_OP_reg16: 1989 case DW_OP_reg17: 1990 case DW_OP_reg18: 1991 case DW_OP_reg19: 1992 case DW_OP_reg20: 1993 case DW_OP_reg21: 1994 case DW_OP_reg22: 1995 case DW_OP_reg23: 1996 case DW_OP_reg24: 1997 case DW_OP_reg25: 1998 case DW_OP_reg26: 1999 case DW_OP_reg27: 2000 case DW_OP_reg28: 2001 case DW_OP_reg29: 2002 case DW_OP_reg30: 2003 case DW_OP_reg31: { 2004 dwarf4_location_description_kind = Register; 2005 reg_num = op - DW_OP_reg0; 2006 2007 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 2008 stack.push_back(tmp); 2009 else 2010 return false; 2011 } break; 2012 // OPCODE: DW_OP_regx 2013 // OPERANDS: 2014 // ULEB128 literal operand that encodes the register. 2015 // DESCRIPTION: Push the value in register on the top of the stack. 2016 case DW_OP_regx: { 2017 dwarf4_location_description_kind = Register; 2018 reg_num = opcodes.GetULEB128(&offset); 2019 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp)) 2020 stack.push_back(tmp); 2021 else 2022 return false; 2023 } break; 2024 2025 // OPCODE: DW_OP_bregN 2026 // OPERANDS: 2027 // SLEB128 offset from register N 2028 // DESCRIPTION: Value is in memory at the address specified by register 2029 // N plus an offset. 2030 case DW_OP_breg0: 2031 case DW_OP_breg1: 2032 case DW_OP_breg2: 2033 case DW_OP_breg3: 2034 case DW_OP_breg4: 2035 case DW_OP_breg5: 2036 case DW_OP_breg6: 2037 case DW_OP_breg7: 2038 case DW_OP_breg8: 2039 case DW_OP_breg9: 2040 case DW_OP_breg10: 2041 case DW_OP_breg11: 2042 case DW_OP_breg12: 2043 case DW_OP_breg13: 2044 case DW_OP_breg14: 2045 case DW_OP_breg15: 2046 case DW_OP_breg16: 2047 case DW_OP_breg17: 2048 case DW_OP_breg18: 2049 case DW_OP_breg19: 2050 case DW_OP_breg20: 2051 case DW_OP_breg21: 2052 case DW_OP_breg22: 2053 case DW_OP_breg23: 2054 case DW_OP_breg24: 2055 case DW_OP_breg25: 2056 case DW_OP_breg26: 2057 case DW_OP_breg27: 2058 case DW_OP_breg28: 2059 case DW_OP_breg29: 2060 case DW_OP_breg30: 2061 case DW_OP_breg31: { 2062 reg_num = op - DW_OP_breg0; 2063 2064 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, 2065 tmp)) { 2066 int64_t breg_offset = opcodes.GetSLEB128(&offset); 2067 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; 2068 tmp.ClearContext(); 2069 stack.push_back(tmp); 2070 stack.back().SetValueType(Value::ValueType::LoadAddress); 2071 } else 2072 return false; 2073 } break; 2074 // OPCODE: DW_OP_bregx 2075 // OPERANDS: 2 2076 // ULEB128 literal operand that encodes the register. 2077 // SLEB128 offset from register N 2078 // DESCRIPTION: Value is in memory at the address specified by register 2079 // N plus an offset. 2080 case DW_OP_bregx: { 2081 reg_num = opcodes.GetULEB128(&offset); 2082 2083 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, 2084 tmp)) { 2085 int64_t breg_offset = opcodes.GetSLEB128(&offset); 2086 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; 2087 tmp.ClearContext(); 2088 stack.push_back(tmp); 2089 stack.back().SetValueType(Value::ValueType::LoadAddress); 2090 } else 2091 return false; 2092 } break; 2093 2094 case DW_OP_fbreg: 2095 if (exe_ctx) { 2096 if (frame) { 2097 Scalar value; 2098 if (frame->GetFrameBaseValue(value, error_ptr)) { 2099 int64_t fbreg_offset = opcodes.GetSLEB128(&offset); 2100 value += fbreg_offset; 2101 stack.push_back(value); 2102 stack.back().SetValueType(Value::ValueType::LoadAddress); 2103 } else 2104 return false; 2105 } else { 2106 if (error_ptr) 2107 error_ptr->SetErrorString( 2108 "Invalid stack frame in context for DW_OP_fbreg opcode."); 2109 return false; 2110 } 2111 } else { 2112 if (error_ptr) 2113 error_ptr->SetErrorString( 2114 "NULL execution context for DW_OP_fbreg.\n"); 2115 return false; 2116 } 2117 2118 break; 2119 2120 // OPCODE: DW_OP_nop 2121 // OPERANDS: none 2122 // DESCRIPTION: A place holder. It has no effect on the location stack 2123 // or any of its values. 2124 case DW_OP_nop: 2125 break; 2126 2127 // OPCODE: DW_OP_piece 2128 // OPERANDS: 1 2129 // ULEB128: byte size of the piece 2130 // DESCRIPTION: The operand describes the size in bytes of the piece of 2131 // the object referenced by the DWARF expression whose result is at the top 2132 // of the stack. If the piece is located in a register, but does not occupy 2133 // the entire register, the placement of the piece within that register is 2134 // defined by the ABI. 2135 // 2136 // Many compilers store a single variable in sets of registers, or store a 2137 // variable partially in memory and partially in registers. DW_OP_piece 2138 // provides a way of describing how large a part of a variable a particular 2139 // DWARF expression refers to. 2140 case DW_OP_piece: { 2141 LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind; 2142 // Reset for the next piece. 2143 dwarf4_location_description_kind = Memory; 2144 2145 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset); 2146 2147 if (piece_byte_size > 0) { 2148 Value curr_piece; 2149 2150 if (stack.empty()) { 2151 UpdateValueTypeFromLocationDescription( 2152 log, dwarf_cu, LocationDescriptionKind::Empty); 2153 // In a multi-piece expression, this means that the current piece is 2154 // not available. Fill with zeros for now by resizing the data and 2155 // appending it 2156 curr_piece.ResizeData(piece_byte_size); 2157 // Note that "0" is not a correct value for the unknown bits. 2158 // It would be better to also return a mask of valid bits together 2159 // with the expression result, so the debugger can print missing 2160 // members as "<optimized out>" or something. 2161 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size); 2162 pieces.AppendDataToHostBuffer(curr_piece); 2163 } else { 2164 Status error; 2165 // Extract the current piece into "curr_piece" 2166 Value curr_piece_source_value(stack.back()); 2167 stack.pop_back(); 2168 UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc, 2169 &curr_piece_source_value); 2170 2171 const Value::ValueType curr_piece_source_value_type = 2172 curr_piece_source_value.GetValueType(); 2173 switch (curr_piece_source_value_type) { 2174 case Value::ValueType::Invalid: 2175 return false; 2176 case Value::ValueType::LoadAddress: 2177 if (process) { 2178 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) { 2179 lldb::addr_t load_addr = 2180 curr_piece_source_value.GetScalar().ULongLong( 2181 LLDB_INVALID_ADDRESS); 2182 if (process->ReadMemory( 2183 load_addr, curr_piece.GetBuffer().GetBytes(), 2184 piece_byte_size, error) != piece_byte_size) { 2185 if (error_ptr) 2186 error_ptr->SetErrorStringWithFormat( 2187 "failed to read memory DW_OP_piece(%" PRIu64 2188 ") from 0x%" PRIx64, 2189 piece_byte_size, load_addr); 2190 return false; 2191 } 2192 } else { 2193 if (error_ptr) 2194 error_ptr->SetErrorStringWithFormat( 2195 "failed to resize the piece memory buffer for " 2196 "DW_OP_piece(%" PRIu64 ")", 2197 piece_byte_size); 2198 return false; 2199 } 2200 } 2201 break; 2202 2203 case Value::ValueType::FileAddress: 2204 case Value::ValueType::HostAddress: 2205 if (error_ptr) { 2206 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong( 2207 LLDB_INVALID_ADDRESS); 2208 error_ptr->SetErrorStringWithFormat( 2209 "failed to read memory DW_OP_piece(%" PRIu64 2210 ") from %s address 0x%" PRIx64, 2211 piece_byte_size, curr_piece_source_value.GetValueType() == 2212 Value::ValueType::FileAddress 2213 ? "file" 2214 : "host", 2215 addr); 2216 } 2217 return false; 2218 2219 case Value::ValueType::Scalar: { 2220 uint32_t bit_size = piece_byte_size * 8; 2221 uint32_t bit_offset = 0; 2222 Scalar &scalar = curr_piece_source_value.GetScalar(); 2223 if (!scalar.ExtractBitfield( 2224 bit_size, bit_offset)) { 2225 if (error_ptr) 2226 error_ptr->SetErrorStringWithFormat( 2227 "unable to extract %" PRIu64 " bytes from a %" PRIu64 2228 " byte scalar value.", 2229 piece_byte_size, 2230 (uint64_t)curr_piece_source_value.GetScalar() 2231 .GetByteSize()); 2232 return false; 2233 } 2234 // Create curr_piece with bit_size. By default Scalar 2235 // grows to the nearest host integer type. 2236 llvm::APInt fail_value(1, 0, false); 2237 llvm::APInt ap_int = scalar.UInt128(fail_value); 2238 assert(ap_int.getBitWidth() >= bit_size); 2239 llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(), 2240 ap_int.getNumWords()}; 2241 curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf)); 2242 } break; 2243 } 2244 2245 // Check if this is the first piece? 2246 if (op_piece_offset == 0) { 2247 // This is the first piece, we should push it back onto the stack 2248 // so subsequent pieces will be able to access this piece and add 2249 // to it. 2250 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) { 2251 if (error_ptr) 2252 error_ptr->SetErrorString("failed to append piece data"); 2253 return false; 2254 } 2255 } else { 2256 // If this is the second or later piece there should be a value on 2257 // the stack. 2258 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) { 2259 if (error_ptr) 2260 error_ptr->SetErrorStringWithFormat( 2261 "DW_OP_piece for offset %" PRIu64 2262 " but top of stack is of size %" PRIu64, 2263 op_piece_offset, pieces.GetBuffer().GetByteSize()); 2264 return false; 2265 } 2266 2267 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) { 2268 if (error_ptr) 2269 error_ptr->SetErrorString("failed to append piece data"); 2270 return false; 2271 } 2272 } 2273 } 2274 op_piece_offset += piece_byte_size; 2275 } 2276 } break; 2277 2278 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3); 2279 if (stack.size() < 1) { 2280 UpdateValueTypeFromLocationDescription(log, dwarf_cu, 2281 LocationDescriptionKind::Empty); 2282 // Reset for the next piece. 2283 dwarf4_location_description_kind = Memory; 2284 if (error_ptr) 2285 error_ptr->SetErrorString( 2286 "Expression stack needs at least 1 item for DW_OP_bit_piece."); 2287 return false; 2288 } else { 2289 UpdateValueTypeFromLocationDescription( 2290 log, dwarf_cu, dwarf4_location_description_kind, &stack.back()); 2291 // Reset for the next piece. 2292 dwarf4_location_description_kind = Memory; 2293 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset); 2294 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset); 2295 switch (stack.back().GetValueType()) { 2296 case Value::ValueType::Invalid: 2297 return false; 2298 case Value::ValueType::Scalar: { 2299 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size, 2300 piece_bit_offset)) { 2301 if (error_ptr) 2302 error_ptr->SetErrorStringWithFormat( 2303 "unable to extract %" PRIu64 " bit value with %" PRIu64 2304 " bit offset from a %" PRIu64 " bit scalar value.", 2305 piece_bit_size, piece_bit_offset, 2306 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8)); 2307 return false; 2308 } 2309 } break; 2310 2311 case Value::ValueType::FileAddress: 2312 case Value::ValueType::LoadAddress: 2313 case Value::ValueType::HostAddress: 2314 if (error_ptr) { 2315 error_ptr->SetErrorStringWithFormat( 2316 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64 2317 ", bit_offset = %" PRIu64 ") from an address value.", 2318 piece_bit_size, piece_bit_offset); 2319 } 2320 return false; 2321 } 2322 } 2323 break; 2324 2325 // OPCODE: DW_OP_implicit_value 2326 // OPERANDS: 2 2327 // ULEB128 size of the value block in bytes 2328 // uint8_t* block bytes encoding value in target's memory 2329 // representation 2330 // DESCRIPTION: Value is immediately stored in block in the debug info with 2331 // the memory representation of the target. 2332 case DW_OP_implicit_value: { 2333 dwarf4_location_description_kind = Implicit; 2334 2335 const uint32_t len = opcodes.GetULEB128(&offset); 2336 const void *data = opcodes.GetData(&offset, len); 2337 2338 if (!data) { 2339 LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data"); 2340 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", 2341 DW_OP_value_to_name(op)); 2342 return false; 2343 } 2344 2345 Value result(data, len); 2346 stack.push_back(result); 2347 break; 2348 } 2349 2350 case DW_OP_implicit_pointer: { 2351 dwarf4_location_description_kind = Implicit; 2352 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", DW_OP_value_to_name(op)); 2353 return false; 2354 } 2355 2356 // OPCODE: DW_OP_push_object_address 2357 // OPERANDS: none 2358 // DESCRIPTION: Pushes the address of the object currently being 2359 // evaluated as part of evaluation of a user presented expression. This 2360 // object may correspond to an independent variable described by its own 2361 // DIE or it may be a component of an array, structure, or class whose 2362 // address has been dynamically determined by an earlier step during user 2363 // expression evaluation. 2364 case DW_OP_push_object_address: 2365 if (object_address_ptr) 2366 stack.push_back(*object_address_ptr); 2367 else { 2368 if (error_ptr) 2369 error_ptr->SetErrorString("DW_OP_push_object_address used without " 2370 "specifying an object address"); 2371 return false; 2372 } 2373 break; 2374 2375 // OPCODE: DW_OP_call2 2376 // OPERANDS: 2377 // uint16_t compile unit relative offset of a DIE 2378 // DESCRIPTION: Performs subroutine calls during evaluation 2379 // of a DWARF expression. The operand is the 2-byte unsigned offset of a 2380 // debugging information entry in the current compilation unit. 2381 // 2382 // Operand interpretation is exactly like that for DW_FORM_ref2. 2383 // 2384 // This operation transfers control of DWARF expression evaluation to the 2385 // DW_AT_location attribute of the referenced DIE. If there is no such 2386 // attribute, then there is no effect. Execution of the DWARF expression of 2387 // a DW_AT_location attribute may add to and/or remove from values on the 2388 // stack. Execution returns to the point following the call when the end of 2389 // the attribute is reached. Values on the stack at the time of the call 2390 // may be used as parameters by the called expression and values left on 2391 // the stack by the called expression may be used as return values by prior 2392 // agreement between the calling and called expressions. 2393 case DW_OP_call2: 2394 if (error_ptr) 2395 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2."); 2396 return false; 2397 // OPCODE: DW_OP_call4 2398 // OPERANDS: 1 2399 // uint32_t compile unit relative offset of a DIE 2400 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF 2401 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of 2402 // a debugging information entry in the current compilation unit. 2403 // 2404 // Operand interpretation DW_OP_call4 is exactly like that for 2405 // DW_FORM_ref4. 2406 // 2407 // This operation transfers control of DWARF expression evaluation to the 2408 // DW_AT_location attribute of the referenced DIE. If there is no such 2409 // attribute, then there is no effect. Execution of the DWARF expression of 2410 // a DW_AT_location attribute may add to and/or remove from values on the 2411 // stack. Execution returns to the point following the call when the end of 2412 // the attribute is reached. Values on the stack at the time of the call 2413 // may be used as parameters by the called expression and values left on 2414 // the stack by the called expression may be used as return values by prior 2415 // agreement between the calling and called expressions. 2416 case DW_OP_call4: 2417 if (error_ptr) 2418 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4."); 2419 return false; 2420 2421 // OPCODE: DW_OP_stack_value 2422 // OPERANDS: None 2423 // DESCRIPTION: Specifies that the object does not exist in memory but 2424 // rather is a constant value. The value from the top of the stack is the 2425 // value to be used. This is the actual object value and not the location. 2426 case DW_OP_stack_value: 2427 dwarf4_location_description_kind = Implicit; 2428 if (stack.empty()) { 2429 if (error_ptr) 2430 error_ptr->SetErrorString( 2431 "Expression stack needs at least 1 item for DW_OP_stack_value."); 2432 return false; 2433 } 2434 stack.back().SetValueType(Value::ValueType::Scalar); 2435 break; 2436 2437 // OPCODE: DW_OP_convert 2438 // OPERANDS: 1 2439 // A ULEB128 that is either a DIE offset of a 2440 // DW_TAG_base_type or 0 for the generic (pointer-sized) type. 2441 // 2442 // DESCRIPTION: Pop the top stack element, convert it to a 2443 // different type, and push the result. 2444 case DW_OP_convert: { 2445 if (stack.size() < 1) { 2446 if (error_ptr) 2447 error_ptr->SetErrorString( 2448 "Expression stack needs at least 1 item for DW_OP_convert."); 2449 return false; 2450 } 2451 const uint64_t die_offset = opcodes.GetULEB128(&offset); 2452 uint64_t bit_size; 2453 bool sign; 2454 if (die_offset == 0) { 2455 // The generic type has the size of an address on the target 2456 // machine and an unspecified signedness. Scalar has no 2457 // "unspecified signedness", so we use unsigned types. 2458 if (!module_sp) { 2459 if (error_ptr) 2460 error_ptr->SetErrorString("No module"); 2461 return false; 2462 } 2463 sign = false; 2464 bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8; 2465 if (!bit_size) { 2466 if (error_ptr) 2467 error_ptr->SetErrorString("unspecified architecture"); 2468 return false; 2469 } 2470 } else { 2471 // Retrieve the type DIE that the value is being converted to. 2472 // FIXME: the constness has annoying ripple effects. 2473 DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(die_offset); 2474 if (!die) { 2475 if (error_ptr) 2476 error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE"); 2477 return false; 2478 } 2479 uint64_t encoding = 2480 die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user); 2481 bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8; 2482 if (!bit_size) 2483 bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0); 2484 if (!bit_size) { 2485 if (error_ptr) 2486 error_ptr->SetErrorString("Unsupported type size in DW_OP_convert"); 2487 return false; 2488 } 2489 switch (encoding) { 2490 case DW_ATE_signed: 2491 case DW_ATE_signed_char: 2492 sign = true; 2493 break; 2494 case DW_ATE_unsigned: 2495 case DW_ATE_unsigned_char: 2496 sign = false; 2497 break; 2498 default: 2499 if (error_ptr) 2500 error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert"); 2501 return false; 2502 } 2503 } 2504 Scalar &top = stack.back().ResolveValue(exe_ctx); 2505 top.TruncOrExtendTo(bit_size, sign); 2506 break; 2507 } 2508 2509 // OPCODE: DW_OP_call_frame_cfa 2510 // OPERANDS: None 2511 // DESCRIPTION: Specifies a DWARF expression that pushes the value of 2512 // the canonical frame address consistent with the call frame information 2513 // located in .debug_frame (or in the FDEs of the eh_frame section). 2514 case DW_OP_call_frame_cfa: 2515 if (frame) { 2516 // Note that we don't have to parse FDEs because this DWARF expression 2517 // is commonly evaluated with a valid stack frame. 2518 StackID id = frame->GetStackID(); 2519 addr_t cfa = id.GetCallFrameAddress(); 2520 if (cfa != LLDB_INVALID_ADDRESS) { 2521 stack.push_back(Scalar(cfa)); 2522 stack.back().SetValueType(Value::ValueType::LoadAddress); 2523 } else if (error_ptr) 2524 error_ptr->SetErrorString("Stack frame does not include a canonical " 2525 "frame address for DW_OP_call_frame_cfa " 2526 "opcode."); 2527 } else { 2528 if (error_ptr) 2529 error_ptr->SetErrorString("Invalid stack frame in context for " 2530 "DW_OP_call_frame_cfa opcode."); 2531 return false; 2532 } 2533 break; 2534 2535 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension 2536 // opcode, DW_OP_GNU_push_tls_address) 2537 // OPERANDS: none 2538 // DESCRIPTION: Pops a TLS offset from the stack, converts it to 2539 // an address in the current thread's thread-local storage block, and 2540 // pushes it on the stack. 2541 case DW_OP_form_tls_address: 2542 case DW_OP_GNU_push_tls_address: { 2543 if (stack.size() < 1) { 2544 if (error_ptr) { 2545 if (op == DW_OP_form_tls_address) 2546 error_ptr->SetErrorString( 2547 "DW_OP_form_tls_address needs an argument."); 2548 else 2549 error_ptr->SetErrorString( 2550 "DW_OP_GNU_push_tls_address needs an argument."); 2551 } 2552 return false; 2553 } 2554 2555 if (!exe_ctx || !module_sp) { 2556 if (error_ptr) 2557 error_ptr->SetErrorString("No context to evaluate TLS within."); 2558 return false; 2559 } 2560 2561 Thread *thread = exe_ctx->GetThreadPtr(); 2562 if (!thread) { 2563 if (error_ptr) 2564 error_ptr->SetErrorString("No thread to evaluate TLS within."); 2565 return false; 2566 } 2567 2568 // Lookup the TLS block address for this thread and module. 2569 const addr_t tls_file_addr = 2570 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 2571 const addr_t tls_load_addr = 2572 thread->GetThreadLocalData(module_sp, tls_file_addr); 2573 2574 if (tls_load_addr == LLDB_INVALID_ADDRESS) { 2575 if (error_ptr) 2576 error_ptr->SetErrorString( 2577 "No TLS data currently exists for this thread."); 2578 return false; 2579 } 2580 2581 stack.back().GetScalar() = tls_load_addr; 2582 stack.back().SetValueType(Value::ValueType::LoadAddress); 2583 } break; 2584 2585 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.) 2586 // OPERANDS: 1 2587 // ULEB128: index to the .debug_addr section 2588 // DESCRIPTION: Pushes an address to the stack from the .debug_addr 2589 // section with the base address specified by the DW_AT_addr_base attribute 2590 // and the 0 based index is the ULEB128 encoded index. 2591 case DW_OP_addrx: 2592 case DW_OP_GNU_addr_index: { 2593 if (!dwarf_cu) { 2594 if (error_ptr) 2595 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a " 2596 "compile unit being specified"); 2597 return false; 2598 } 2599 uint64_t index = opcodes.GetULEB128(&offset); 2600 lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index); 2601 stack.push_back(Scalar(value)); 2602 stack.back().SetValueType(Value::ValueType::FileAddress); 2603 } break; 2604 2605 // OPCODE: DW_OP_GNU_const_index 2606 // OPERANDS: 1 2607 // ULEB128: index to the .debug_addr section 2608 // DESCRIPTION: Pushes an constant with the size of a machine address to 2609 // the stack from the .debug_addr section with the base address specified 2610 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128 2611 // encoded index. 2612 case DW_OP_GNU_const_index: { 2613 if (!dwarf_cu) { 2614 if (error_ptr) 2615 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a " 2616 "compile unit being specified"); 2617 return false; 2618 } 2619 uint64_t index = opcodes.GetULEB128(&offset); 2620 lldb::addr_t value = ReadAddressFromDebugAddrSection(dwarf_cu, index); 2621 stack.push_back(Scalar(value)); 2622 } break; 2623 2624 case DW_OP_GNU_entry_value: 2625 case DW_OP_entry_value: { 2626 if (!Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, opcodes, offset, 2627 error_ptr, log)) { 2628 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", 2629 DW_OP_value_to_name(op)); 2630 return false; 2631 } 2632 break; 2633 } 2634 2635 default: 2636 if (error_ptr) 2637 error_ptr->SetErrorStringWithFormatv( 2638 "Unhandled opcode {0} in DWARFExpression", LocationAtom(op)); 2639 return false; 2640 } 2641 } 2642 2643 if (stack.empty()) { 2644 // Nothing on the stack, check if we created a piece value from DW_OP_piece 2645 // or DW_OP_bit_piece opcodes 2646 if (pieces.GetBuffer().GetByteSize()) { 2647 result = pieces; 2648 return true; 2649 } 2650 if (error_ptr) 2651 error_ptr->SetErrorString("Stack empty after evaluation."); 2652 return false; 2653 } 2654 2655 UpdateValueTypeFromLocationDescription( 2656 log, dwarf_cu, dwarf4_location_description_kind, &stack.back()); 2657 2658 if (log && log->GetVerbose()) { 2659 size_t count = stack.size(); 2660 LLDB_LOGF(log, 2661 "Stack after operation has %" PRIu64 " values:", (uint64_t)count); 2662 for (size_t i = 0; i < count; ++i) { 2663 StreamString new_value; 2664 new_value.Printf("[%" PRIu64 "]", (uint64_t)i); 2665 stack[i].Dump(&new_value); 2666 LLDB_LOGF(log, " %s", new_value.GetData()); 2667 } 2668 } 2669 result = stack.back(); 2670 return true; // Return true on success 2671 } 2672 2673 static DataExtractor ToDataExtractor(const llvm::DWARFLocationExpression &loc, 2674 ByteOrder byte_order, uint32_t addr_size) { 2675 auto buffer_sp = 2676 std::make_shared<DataBufferHeap>(loc.Expr.data(), loc.Expr.size()); 2677 return DataExtractor(buffer_sp, byte_order, addr_size); 2678 } 2679 2680 llvm::Optional<DataExtractor> 2681 DWARFExpression::GetLocationExpression(addr_t load_function_start, 2682 addr_t addr) const { 2683 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS); 2684 2685 std::unique_ptr<llvm::DWARFLocationTable> loctable_up = 2686 m_dwarf_cu->GetLocationTable(m_data); 2687 llvm::Optional<DataExtractor> result; 2688 uint64_t offset = 0; 2689 auto lookup_addr = 2690 [&](uint32_t index) -> llvm::Optional<llvm::object::SectionedAddress> { 2691 addr_t address = ReadAddressFromDebugAddrSection(m_dwarf_cu, index); 2692 if (address == LLDB_INVALID_ADDRESS) 2693 return llvm::None; 2694 return llvm::object::SectionedAddress{address}; 2695 }; 2696 auto process_list = [&](llvm::Expected<llvm::DWARFLocationExpression> loc) { 2697 if (!loc) { 2698 LLDB_LOG_ERROR(log, loc.takeError(), "{0}"); 2699 return true; 2700 } 2701 if (loc->Range) { 2702 // This relocates low_pc and high_pc by adding the difference between the 2703 // function file address, and the actual address it is loaded in memory. 2704 addr_t slide = load_function_start - m_loclist_addresses->func_file_addr; 2705 loc->Range->LowPC += slide; 2706 loc->Range->HighPC += slide; 2707 2708 if (loc->Range->LowPC <= addr && addr < loc->Range->HighPC) 2709 result = ToDataExtractor(*loc, m_data.GetByteOrder(), 2710 m_data.GetAddressByteSize()); 2711 } 2712 return !result; 2713 }; 2714 llvm::Error E = loctable_up->visitAbsoluteLocationList( 2715 offset, llvm::object::SectionedAddress{m_loclist_addresses->cu_file_addr}, 2716 lookup_addr, process_list); 2717 if (E) 2718 LLDB_LOG_ERROR(log, std::move(E), "{0}"); 2719 return result; 2720 } 2721 2722 bool DWARFExpression::MatchesOperand(StackFrame &frame, 2723 const Instruction::Operand &operand) { 2724 using namespace OperandMatchers; 2725 2726 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext(); 2727 if (!reg_ctx_sp) { 2728 return false; 2729 } 2730 2731 DataExtractor opcodes; 2732 if (IsLocationList()) { 2733 SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction); 2734 if (!sc.function) 2735 return false; 2736 2737 addr_t load_function_start = 2738 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress(); 2739 if (load_function_start == LLDB_INVALID_ADDRESS) 2740 return false; 2741 2742 addr_t pc = frame.GetFrameCodeAddress().GetLoadAddress( 2743 frame.CalculateTarget().get()); 2744 2745 if (llvm::Optional<DataExtractor> expr = GetLocationExpression(load_function_start, pc)) 2746 opcodes = std::move(*expr); 2747 else 2748 return false; 2749 } else 2750 opcodes = m_data; 2751 2752 2753 lldb::offset_t op_offset = 0; 2754 uint8_t opcode = opcodes.GetU8(&op_offset); 2755 2756 if (opcode == DW_OP_fbreg) { 2757 int64_t offset = opcodes.GetSLEB128(&op_offset); 2758 2759 DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr); 2760 if (!fb_expr) { 2761 return false; 2762 } 2763 2764 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) { 2765 return fb_expr->MatchesOperand(frame, child); 2766 }; 2767 2768 if (!offset && 2769 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), 2770 recurse)(operand)) { 2771 return true; 2772 } 2773 2774 return MatchUnaryOp( 2775 MatchOpType(Instruction::Operand::Type::Dereference), 2776 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), 2777 MatchImmOp(offset), recurse))(operand); 2778 } 2779 2780 bool dereference = false; 2781 const RegisterInfo *reg = nullptr; 2782 int64_t offset = 0; 2783 2784 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) { 2785 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0); 2786 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) { 2787 offset = opcodes.GetSLEB128(&op_offset); 2788 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0); 2789 } else if (opcode == DW_OP_regx) { 2790 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset)); 2791 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num); 2792 } else if (opcode == DW_OP_bregx) { 2793 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset)); 2794 offset = opcodes.GetSLEB128(&op_offset); 2795 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num); 2796 } else { 2797 return false; 2798 } 2799 2800 if (!reg) { 2801 return false; 2802 } 2803 2804 if (dereference) { 2805 if (!offset && 2806 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), 2807 MatchRegOp(*reg))(operand)) { 2808 return true; 2809 } 2810 2811 return MatchUnaryOp( 2812 MatchOpType(Instruction::Operand::Type::Dereference), 2813 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), 2814 MatchRegOp(*reg), 2815 MatchImmOp(offset)))(operand); 2816 } else { 2817 return MatchRegOp(*reg)(operand); 2818 } 2819 } 2820 2821