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