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