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