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