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