1 //===-- DWARFExpression.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Expression/DWARFExpression.h"
18 #include "lldb/Core/RegisterValue.h"
19 #include "lldb/Core/Scalar.h"
20 #include "lldb/Core/Value.h"
21 #include "lldb/Core/dwarf.h"
22 #include "lldb/Utility/DataEncoder.h"
23 #include "lldb/Utility/Log.h"
24 #include "lldb/Utility/StreamString.h"
25 #include "lldb/Utility/VMRange.h"
27 #include "lldb/Host/Host.h"
28 #include "lldb/Utility/Endian.h"
30 #include "lldb/Symbol/Function.h"
32 #include "lldb/Target/ABI.h"
33 #include "lldb/Target/ExecutionContext.h"
34 #include "lldb/Target/Process.h"
35 #include "lldb/Target/RegisterContext.h"
36 #include "lldb/Target/StackFrame.h"
37 #include "lldb/Target/StackID.h"
38 #include "lldb/Target/Thread.h"
40 #include "Plugins/SymbolFile/DWARF/DWARFCompileUnit.h"
43 using namespace lldb_private;
46 ReadAddressFromDebugAddrSection(const DWARFCompileUnit *dwarf_cu,
48 uint32_t index_size = dwarf_cu->GetAddressByteSize();
49 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
50 lldb::offset_t offset = addr_base + index * index_size;
51 return dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
55 //----------------------------------------------------------------------
56 // DWARFExpression constructor
57 //----------------------------------------------------------------------
58 DWARFExpression::DWARFExpression(DWARFCompileUnit *dwarf_cu)
59 : m_module_wp(), m_data(), m_dwarf_cu(dwarf_cu),
60 m_reg_kind(eRegisterKindDWARF), m_loclist_slide(LLDB_INVALID_ADDRESS) {}
62 DWARFExpression::DWARFExpression(const DWARFExpression &rhs)
63 : m_module_wp(rhs.m_module_wp), m_data(rhs.m_data),
64 m_dwarf_cu(rhs.m_dwarf_cu), m_reg_kind(rhs.m_reg_kind),
65 m_loclist_slide(rhs.m_loclist_slide) {}
67 DWARFExpression::DWARFExpression(lldb::ModuleSP module_sp,
68 const DataExtractor &data,
69 DWARFCompileUnit *dwarf_cu,
70 lldb::offset_t data_offset,
71 lldb::offset_t data_length)
72 : m_module_wp(), m_data(data, data_offset, data_length),
73 m_dwarf_cu(dwarf_cu), m_reg_kind(eRegisterKindDWARF),
74 m_loclist_slide(LLDB_INVALID_ADDRESS) {
76 m_module_wp = module_sp;
79 //----------------------------------------------------------------------
81 //----------------------------------------------------------------------
82 DWARFExpression::~DWARFExpression() {}
84 bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
86 void DWARFExpression::SetOpcodeData(const DataExtractor &data) {
90 void DWARFExpression::CopyOpcodeData(lldb::ModuleSP module_sp,
91 const DataExtractor &data,
92 lldb::offset_t data_offset,
93 lldb::offset_t data_length) {
94 const uint8_t *bytes = data.PeekData(data_offset, data_length);
96 m_module_wp = module_sp;
97 m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length)));
98 m_data.SetByteOrder(data.GetByteOrder());
99 m_data.SetAddressByteSize(data.GetAddressByteSize());
103 void DWARFExpression::CopyOpcodeData(const void *data,
104 lldb::offset_t data_length,
105 ByteOrder byte_order,
106 uint8_t addr_byte_size) {
107 if (data && data_length) {
108 m_data.SetData(DataBufferSP(new DataBufferHeap(data, data_length)));
109 m_data.SetByteOrder(byte_order);
110 m_data.SetAddressByteSize(addr_byte_size);
114 void DWARFExpression::CopyOpcodeData(uint64_t const_value,
115 lldb::offset_t const_value_byte_size,
116 uint8_t addr_byte_size) {
117 if (const_value_byte_size) {
119 DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
120 m_data.SetByteOrder(endian::InlHostByteOrder());
121 m_data.SetAddressByteSize(addr_byte_size);
125 void DWARFExpression::SetOpcodeData(lldb::ModuleSP module_sp,
126 const DataExtractor &data,
127 lldb::offset_t data_offset,
128 lldb::offset_t data_length) {
129 m_module_wp = module_sp;
130 m_data.SetData(data, data_offset, data_length);
133 void DWARFExpression::DumpLocation(Stream *s, lldb::offset_t offset,
134 lldb::offset_t length,
135 lldb::DescriptionLevel level,
137 if (!m_data.ValidOffsetForDataOfSize(offset, length))
139 const lldb::offset_t start_offset = offset;
140 const lldb::offset_t end_offset = offset + length;
141 while (m_data.ValidOffset(offset) && offset < end_offset) {
142 const lldb::offset_t op_offset = offset;
143 const uint8_t op = m_data.GetU8(&offset);
149 case lldb::eDescriptionLevelBrief:
150 if (offset > start_offset)
154 case lldb::eDescriptionLevelFull:
155 case lldb::eDescriptionLevelVerbose:
156 if (offset > start_offset)
159 if (level == lldb::eDescriptionLevelFull)
161 // Fall through for verbose and print offset and DW_OP prefix..
162 s->Printf("0x%8.8" PRIx64 ": %s", op_offset,
163 op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
169 *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") ";
170 break; // 0x03 1 address
175 s->Printf("DW_OP_const1u(0x%2.2x) ", m_data.GetU8(&offset));
176 break; // 0x08 1 1-byte constant
178 s->Printf("DW_OP_const1s(0x%2.2x) ", m_data.GetU8(&offset));
179 break; // 0x09 1 1-byte constant
181 s->Printf("DW_OP_const2u(0x%4.4x) ", m_data.GetU16(&offset));
182 break; // 0x0a 1 2-byte constant
184 s->Printf("DW_OP_const2s(0x%4.4x) ", m_data.GetU16(&offset));
185 break; // 0x0b 1 2-byte constant
187 s->Printf("DW_OP_const4u(0x%8.8x) ", m_data.GetU32(&offset));
188 break; // 0x0c 1 4-byte constant
190 s->Printf("DW_OP_const4s(0x%8.8x) ", m_data.GetU32(&offset));
191 break; // 0x0d 1 4-byte constant
193 s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset));
194 break; // 0x0e 1 8-byte constant
196 s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset));
197 break; // 0x0f 1 8-byte constant
199 s->Printf("DW_OP_constu(0x%" PRIx64 ") ", m_data.GetULEB128(&offset));
200 break; // 0x10 1 ULEB128 constant
202 s->Printf("DW_OP_consts(0x%" PRId64 ") ", m_data.GetSLEB128(&offset));
203 break; // 0x11 1 SLEB128 constant
205 s->PutCString("DW_OP_dup");
208 s->PutCString("DW_OP_drop");
211 s->PutCString("DW_OP_over");
214 s->Printf("DW_OP_pick(0x%2.2x) ", m_data.GetU8(&offset));
215 break; // 0x15 1 1-byte stack index
217 s->PutCString("DW_OP_swap");
220 s->PutCString("DW_OP_rot");
223 s->PutCString("DW_OP_xderef");
226 s->PutCString("DW_OP_abs");
229 s->PutCString("DW_OP_and");
232 s->PutCString("DW_OP_div");
235 s->PutCString("DW_OP_minus");
238 s->PutCString("DW_OP_mod");
241 s->PutCString("DW_OP_mul");
244 s->PutCString("DW_OP_neg");
247 s->PutCString("DW_OP_not");
250 s->PutCString("DW_OP_or");
253 s->PutCString("DW_OP_plus");
255 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
256 s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ") ",
257 m_data.GetULEB128(&offset));
261 s->PutCString("DW_OP_shl");
264 s->PutCString("DW_OP_shr");
267 s->PutCString("DW_OP_shra");
270 s->PutCString("DW_OP_xor");
273 s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset));
274 break; // 0x2f 1 signed 2-byte constant
276 s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset));
277 break; // 0x28 1 signed 2-byte constant
279 s->PutCString("DW_OP_eq");
282 s->PutCString("DW_OP_ge");
285 s->PutCString("DW_OP_gt");
288 s->PutCString("DW_OP_le");
291 s->PutCString("DW_OP_lt");
294 s->PutCString("DW_OP_ne");
297 case DW_OP_lit0: // 0x30
298 case DW_OP_lit1: // 0x31
299 case DW_OP_lit2: // 0x32
300 case DW_OP_lit3: // 0x33
301 case DW_OP_lit4: // 0x34
302 case DW_OP_lit5: // 0x35
303 case DW_OP_lit6: // 0x36
304 case DW_OP_lit7: // 0x37
305 case DW_OP_lit8: // 0x38
306 case DW_OP_lit9: // 0x39
307 case DW_OP_lit10: // 0x3A
308 case DW_OP_lit11: // 0x3B
309 case DW_OP_lit12: // 0x3C
310 case DW_OP_lit13: // 0x3D
311 case DW_OP_lit14: // 0x3E
312 case DW_OP_lit15: // 0x3F
313 case DW_OP_lit16: // 0x40
314 case DW_OP_lit17: // 0x41
315 case DW_OP_lit18: // 0x42
316 case DW_OP_lit19: // 0x43
317 case DW_OP_lit20: // 0x44
318 case DW_OP_lit21: // 0x45
319 case DW_OP_lit22: // 0x46
320 case DW_OP_lit23: // 0x47
321 case DW_OP_lit24: // 0x48
322 case DW_OP_lit25: // 0x49
323 case DW_OP_lit26: // 0x4A
324 case DW_OP_lit27: // 0x4B
325 case DW_OP_lit28: // 0x4C
326 case DW_OP_lit29: // 0x4D
327 case DW_OP_lit30: // 0x4E
329 s->Printf("DW_OP_lit%i", op - DW_OP_lit0);
332 case DW_OP_reg0: // 0x50
333 case DW_OP_reg1: // 0x51
334 case DW_OP_reg2: // 0x52
335 case DW_OP_reg3: // 0x53
336 case DW_OP_reg4: // 0x54
337 case DW_OP_reg5: // 0x55
338 case DW_OP_reg6: // 0x56
339 case DW_OP_reg7: // 0x57
340 case DW_OP_reg8: // 0x58
341 case DW_OP_reg9: // 0x59
342 case DW_OP_reg10: // 0x5A
343 case DW_OP_reg11: // 0x5B
344 case DW_OP_reg12: // 0x5C
345 case DW_OP_reg13: // 0x5D
346 case DW_OP_reg14: // 0x5E
347 case DW_OP_reg15: // 0x5F
348 case DW_OP_reg16: // 0x60
349 case DW_OP_reg17: // 0x61
350 case DW_OP_reg18: // 0x62
351 case DW_OP_reg19: // 0x63
352 case DW_OP_reg20: // 0x64
353 case DW_OP_reg21: // 0x65
354 case DW_OP_reg22: // 0x66
355 case DW_OP_reg23: // 0x67
356 case DW_OP_reg24: // 0x68
357 case DW_OP_reg25: // 0x69
358 case DW_OP_reg26: // 0x6A
359 case DW_OP_reg27: // 0x6B
360 case DW_OP_reg28: // 0x6C
361 case DW_OP_reg29: // 0x6D
362 case DW_OP_reg30: // 0x6E
363 case DW_OP_reg31: // 0x6F
365 uint32_t reg_num = op - DW_OP_reg0;
367 RegisterInfo reg_info;
368 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
370 s->PutCString(reg_info.name);
372 } else if (reg_info.alt_name) {
373 s->PutCString(reg_info.alt_name);
378 s->Printf("DW_OP_reg%u", reg_num);
414 uint32_t reg_num = op - DW_OP_breg0;
415 int64_t reg_offset = m_data.GetSLEB128(&offset);
417 RegisterInfo reg_info;
418 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
420 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
422 } else if (reg_info.alt_name) {
423 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
428 s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset);
431 case DW_OP_regx: // 0x90 1 ULEB128 register
433 uint32_t reg_num = m_data.GetULEB128(&offset);
435 RegisterInfo reg_info;
436 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
438 s->PutCString(reg_info.name);
440 } else if (reg_info.alt_name) {
441 s->PutCString(reg_info.alt_name);
446 s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num);
449 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
450 s->Printf("DW_OP_fbreg(%" PRIi64 ")", m_data.GetSLEB128(&offset));
452 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
454 uint32_t reg_num = m_data.GetULEB128(&offset);
455 int64_t reg_offset = m_data.GetSLEB128(&offset);
457 RegisterInfo reg_info;
458 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
460 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
462 } else if (reg_info.alt_name) {
463 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
468 s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num,
471 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
472 s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
474 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
475 s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
477 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
478 s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
481 s->PutCString("DW_OP_nop");
483 case DW_OP_push_object_address:
484 s->PutCString("DW_OP_push_object_address");
485 break; // 0x97 DWARF3
486 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
487 s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
489 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
490 s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
492 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
493 s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset));
495 // case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break;
497 // case DW_OP_bit_piece: // 0x9d DWARF3 2
498 // s->Printf("DW_OP_bit_piece(0x%x, 0x%x)",
499 // m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
501 // case DW_OP_lo_user: s->PutCString("DW_OP_lo_user"); break;
503 // case DW_OP_hi_user: s->PutCString("DW_OP_hi_user"); break;
505 // case DW_OP_APPLE_extern:
506 // s->Printf("DW_OP_APPLE_extern(%" PRIu64 ")",
507 // m_data.GetULEB128(&offset));
509 // case DW_OP_APPLE_array_ref:
510 // s->PutCString("DW_OP_APPLE_array_ref");
512 case DW_OP_form_tls_address:
513 s->PutCString("DW_OP_form_tls_address"); // 0x9b
515 case DW_OP_GNU_addr_index: // 0xfb
516 s->Printf("DW_OP_GNU_addr_index(0x%" PRIx64 ")",
517 m_data.GetULEB128(&offset));
519 case DW_OP_GNU_const_index: // 0xfc
520 s->Printf("DW_OP_GNU_const_index(0x%" PRIx64 ")",
521 m_data.GetULEB128(&offset));
523 case DW_OP_GNU_push_tls_address:
524 s->PutCString("DW_OP_GNU_push_tls_address"); // 0xe0
526 case DW_OP_APPLE_uninit:
527 s->PutCString("DW_OP_APPLE_uninit"); // 0xF0
529 // case DW_OP_APPLE_assign: // 0xF1 - pops value off and
530 // assigns it to second item on stack (2nd item must have
531 // assignable context)
532 // s->PutCString("DW_OP_APPLE_assign");
534 // case DW_OP_APPLE_address_of: // 0xF2 - gets the address of
535 // the top stack item (top item must be a variable, or have
536 // value_type that is an address already)
537 // s->PutCString("DW_OP_APPLE_address_of");
539 // case DW_OP_APPLE_value_of: // 0xF3 - pops the value off the
540 // stack and pushes the value of that object (top item must be a
541 // variable, or expression local)
542 // s->PutCString("DW_OP_APPLE_value_of");
544 // case DW_OP_APPLE_deref_type: // 0xF4 - gets the address of
545 // the top stack item (top item must be a variable, or a clang
547 // s->PutCString("DW_OP_APPLE_deref_type");
549 // case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression
551 // s->Printf("DW_OP_APPLE_expr_local(%" PRIu64 ")",
552 // m_data.GetULEB128(&offset));
554 // case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size,
555 // followed by constant float data
557 // uint8_t float_length = m_data.GetU8(&offset);
558 // s->Printf("DW_OP_APPLE_constf(<%u> ", float_length);
559 // m_data.Dump(s, offset, eFormatHex, float_length, 1,
560 // UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
562 // // Consume the float data
563 // m_data.GetData(&offset, float_length);
566 // case DW_OP_APPLE_scalar_cast:
567 // s->Printf("DW_OP_APPLE_scalar_cast(%s)",
568 // Scalar::GetValueTypeAsCString
569 // ((Scalar::Type)m_data.GetU8(&offset)));
571 // case DW_OP_APPLE_clang_cast:
573 // clang::Type *clang_type = (clang::Type
574 // *)m_data.GetMaxU64(&offset, sizeof(void*));
575 // s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type);
578 // case DW_OP_APPLE_clear:
579 // s->PutCString("DW_OP_APPLE_clear");
581 // case DW_OP_APPLE_error: // 0xFF - Stops expression
582 // evaluation and returns an error (no args)
583 // s->PutCString("DW_OP_APPLE_error");
589 void DWARFExpression::SetLocationListSlide(addr_t slide) {
590 m_loclist_slide = slide;
593 int DWARFExpression::GetRegisterKind() { return m_reg_kind; }
595 void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
596 m_reg_kind = reg_kind;
599 bool DWARFExpression::IsLocationList() const {
600 return m_loclist_slide != LLDB_INVALID_ADDRESS;
603 void DWARFExpression::GetDescription(Stream *s, lldb::DescriptionLevel level,
604 addr_t location_list_base_addr,
606 if (IsLocationList()) {
607 // We have a location list
608 lldb::offset_t offset = 0;
610 addr_t curr_base_addr = location_list_base_addr;
611 while (m_data.ValidOffset(offset)) {
612 addr_t begin_addr_offset = LLDB_INVALID_ADDRESS;
613 addr_t end_addr_offset = LLDB_INVALID_ADDRESS;
614 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
615 begin_addr_offset, end_addr_offset))
618 if (begin_addr_offset == 0 && end_addr_offset == 0)
621 if (begin_addr_offset < end_addr_offset) {
624 VMRange addr_range(curr_base_addr + begin_addr_offset,
625 curr_base_addr + end_addr_offset);
626 addr_range.Dump(s, 0, 8);
628 lldb::offset_t location_length = m_data.GetU16(&offset);
629 DumpLocation(s, offset, location_length, level, abi);
631 offset += location_length;
633 if ((m_data.GetAddressByteSize() == 4 &&
634 (begin_addr_offset == UINT32_MAX)) ||
635 (m_data.GetAddressByteSize() == 8 &&
636 (begin_addr_offset == UINT64_MAX))) {
637 curr_base_addr = end_addr_offset + location_list_base_addr;
638 // We have a new base address
641 *s << "base_addr = " << end_addr_offset;
648 // We have a normal location that contains DW_OP location opcodes
649 DumpLocation(s, 0, m_data.GetByteSize(), level, abi);
653 static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
654 lldb::RegisterKind reg_kind,
655 uint32_t reg_num, Status *error_ptr,
657 if (reg_ctx == NULL) {
659 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
661 uint32_t native_reg =
662 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
663 if (native_reg == LLDB_INVALID_REGNUM) {
665 error_ptr->SetErrorStringWithFormat("Unable to convert register "
666 "kind=%u reg_num=%u to a native "
667 "register number.\n",
670 const RegisterInfo *reg_info =
671 reg_ctx->GetRegisterInfoAtIndex(native_reg);
672 RegisterValue reg_value;
673 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
674 if (reg_value.GetScalarValue(value.GetScalar())) {
675 value.SetValueType(Value::eValueTypeScalar);
676 value.SetContext(Value::eContextTypeRegisterInfo,
677 const_cast<RegisterInfo *>(reg_info));
682 // If we get this error, then we need to implement a value
683 // buffer in the dwarf expression evaluation function...
685 error_ptr->SetErrorStringWithFormat(
686 "register %s can't be converted to a scalar value",
691 error_ptr->SetErrorStringWithFormat("register %s is not available",
700 // DWARFExpression::LocationListContainsLoadAddress (Process* process, const
701 // Address &addr) const
703 // return LocationListContainsLoadAddress(process,
704 // addr.GetLoadAddress(process));
708 // DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t
711 // if (load_addr == LLDB_INVALID_ADDRESS)
714 // if (IsLocationList())
716 // lldb::offset_t offset = 0;
718 // addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
720 // if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
723 // while (m_data.ValidOffset(offset))
725 // // We need to figure out what the value is for the location.
726 // addr_t lo_pc = m_data.GetAddress(&offset);
727 // addr_t hi_pc = m_data.GetAddress(&offset);
728 // if (lo_pc == 0 && hi_pc == 0)
732 // lo_pc += loc_list_base_addr;
733 // hi_pc += loc_list_base_addr;
735 // if (lo_pc <= load_addr && load_addr < hi_pc)
738 // offset += m_data.GetU16(&offset);
745 static offset_t GetOpcodeDataSize(const DataExtractor &data,
746 const lldb::offset_t data_offset,
748 lldb::offset_t offset = data_offset;
751 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
752 return data.GetAddressByteSize();
754 // Opcodes with no arguments
755 case DW_OP_deref: // 0x06
756 case DW_OP_dup: // 0x12
757 case DW_OP_drop: // 0x13
758 case DW_OP_over: // 0x14
759 case DW_OP_swap: // 0x16
760 case DW_OP_rot: // 0x17
761 case DW_OP_xderef: // 0x18
762 case DW_OP_abs: // 0x19
763 case DW_OP_and: // 0x1a
764 case DW_OP_div: // 0x1b
765 case DW_OP_minus: // 0x1c
766 case DW_OP_mod: // 0x1d
767 case DW_OP_mul: // 0x1e
768 case DW_OP_neg: // 0x1f
769 case DW_OP_not: // 0x20
770 case DW_OP_or: // 0x21
771 case DW_OP_plus: // 0x22
772 case DW_OP_shl: // 0x24
773 case DW_OP_shr: // 0x25
774 case DW_OP_shra: // 0x26
775 case DW_OP_xor: // 0x27
776 case DW_OP_eq: // 0x29
777 case DW_OP_ge: // 0x2a
778 case DW_OP_gt: // 0x2b
779 case DW_OP_le: // 0x2c
780 case DW_OP_lt: // 0x2d
781 case DW_OP_ne: // 0x2e
782 case DW_OP_lit0: // 0x30
783 case DW_OP_lit1: // 0x31
784 case DW_OP_lit2: // 0x32
785 case DW_OP_lit3: // 0x33
786 case DW_OP_lit4: // 0x34
787 case DW_OP_lit5: // 0x35
788 case DW_OP_lit6: // 0x36
789 case DW_OP_lit7: // 0x37
790 case DW_OP_lit8: // 0x38
791 case DW_OP_lit9: // 0x39
792 case DW_OP_lit10: // 0x3A
793 case DW_OP_lit11: // 0x3B
794 case DW_OP_lit12: // 0x3C
795 case DW_OP_lit13: // 0x3D
796 case DW_OP_lit14: // 0x3E
797 case DW_OP_lit15: // 0x3F
798 case DW_OP_lit16: // 0x40
799 case DW_OP_lit17: // 0x41
800 case DW_OP_lit18: // 0x42
801 case DW_OP_lit19: // 0x43
802 case DW_OP_lit20: // 0x44
803 case DW_OP_lit21: // 0x45
804 case DW_OP_lit22: // 0x46
805 case DW_OP_lit23: // 0x47
806 case DW_OP_lit24: // 0x48
807 case DW_OP_lit25: // 0x49
808 case DW_OP_lit26: // 0x4A
809 case DW_OP_lit27: // 0x4B
810 case DW_OP_lit28: // 0x4C
811 case DW_OP_lit29: // 0x4D
812 case DW_OP_lit30: // 0x4E
813 case DW_OP_lit31: // 0x4f
814 case DW_OP_reg0: // 0x50
815 case DW_OP_reg1: // 0x51
816 case DW_OP_reg2: // 0x52
817 case DW_OP_reg3: // 0x53
818 case DW_OP_reg4: // 0x54
819 case DW_OP_reg5: // 0x55
820 case DW_OP_reg6: // 0x56
821 case DW_OP_reg7: // 0x57
822 case DW_OP_reg8: // 0x58
823 case DW_OP_reg9: // 0x59
824 case DW_OP_reg10: // 0x5A
825 case DW_OP_reg11: // 0x5B
826 case DW_OP_reg12: // 0x5C
827 case DW_OP_reg13: // 0x5D
828 case DW_OP_reg14: // 0x5E
829 case DW_OP_reg15: // 0x5F
830 case DW_OP_reg16: // 0x60
831 case DW_OP_reg17: // 0x61
832 case DW_OP_reg18: // 0x62
833 case DW_OP_reg19: // 0x63
834 case DW_OP_reg20: // 0x64
835 case DW_OP_reg21: // 0x65
836 case DW_OP_reg22: // 0x66
837 case DW_OP_reg23: // 0x67
838 case DW_OP_reg24: // 0x68
839 case DW_OP_reg25: // 0x69
840 case DW_OP_reg26: // 0x6A
841 case DW_OP_reg27: // 0x6B
842 case DW_OP_reg28: // 0x6C
843 case DW_OP_reg29: // 0x6D
844 case DW_OP_reg30: // 0x6E
845 case DW_OP_reg31: // 0x6F
846 case DW_OP_nop: // 0x96
847 case DW_OP_push_object_address: // 0x97 DWARF3
848 case DW_OP_form_tls_address: // 0x9b DWARF3
849 case DW_OP_call_frame_cfa: // 0x9c DWARF3
850 case DW_OP_stack_value: // 0x9f DWARF4
851 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
854 // Opcodes with a single 1 byte arguments
855 case DW_OP_const1u: // 0x08 1 1-byte constant
856 case DW_OP_const1s: // 0x09 1 1-byte constant
857 case DW_OP_pick: // 0x15 1 1-byte stack index
858 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
859 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
862 // Opcodes with a single 2 byte arguments
863 case DW_OP_const2u: // 0x0a 1 2-byte constant
864 case DW_OP_const2s: // 0x0b 1 2-byte constant
865 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
866 case DW_OP_bra: // 0x28 1 signed 2-byte constant
867 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
870 // Opcodes with a single 4 byte arguments
871 case DW_OP_const4u: // 0x0c 1 4-byte constant
872 case DW_OP_const4s: // 0x0d 1 4-byte constant
873 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
876 // Opcodes with a single 8 byte arguments
877 case DW_OP_const8u: // 0x0e 1 8-byte constant
878 case DW_OP_const8s: // 0x0f 1 8-byte constant
881 // All opcodes that have a single ULEB (signed or unsigned) argument
882 case DW_OP_constu: // 0x10 1 ULEB128 constant
883 case DW_OP_consts: // 0x11 1 SLEB128 constant
884 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
885 case DW_OP_breg0: // 0x70 1 ULEB128 register
886 case DW_OP_breg1: // 0x71 1 ULEB128 register
887 case DW_OP_breg2: // 0x72 1 ULEB128 register
888 case DW_OP_breg3: // 0x73 1 ULEB128 register
889 case DW_OP_breg4: // 0x74 1 ULEB128 register
890 case DW_OP_breg5: // 0x75 1 ULEB128 register
891 case DW_OP_breg6: // 0x76 1 ULEB128 register
892 case DW_OP_breg7: // 0x77 1 ULEB128 register
893 case DW_OP_breg8: // 0x78 1 ULEB128 register
894 case DW_OP_breg9: // 0x79 1 ULEB128 register
895 case DW_OP_breg10: // 0x7a 1 ULEB128 register
896 case DW_OP_breg11: // 0x7b 1 ULEB128 register
897 case DW_OP_breg12: // 0x7c 1 ULEB128 register
898 case DW_OP_breg13: // 0x7d 1 ULEB128 register
899 case DW_OP_breg14: // 0x7e 1 ULEB128 register
900 case DW_OP_breg15: // 0x7f 1 ULEB128 register
901 case DW_OP_breg16: // 0x80 1 ULEB128 register
902 case DW_OP_breg17: // 0x81 1 ULEB128 register
903 case DW_OP_breg18: // 0x82 1 ULEB128 register
904 case DW_OP_breg19: // 0x83 1 ULEB128 register
905 case DW_OP_breg20: // 0x84 1 ULEB128 register
906 case DW_OP_breg21: // 0x85 1 ULEB128 register
907 case DW_OP_breg22: // 0x86 1 ULEB128 register
908 case DW_OP_breg23: // 0x87 1 ULEB128 register
909 case DW_OP_breg24: // 0x88 1 ULEB128 register
910 case DW_OP_breg25: // 0x89 1 ULEB128 register
911 case DW_OP_breg26: // 0x8a 1 ULEB128 register
912 case DW_OP_breg27: // 0x8b 1 ULEB128 register
913 case DW_OP_breg28: // 0x8c 1 ULEB128 register
914 case DW_OP_breg29: // 0x8d 1 ULEB128 register
915 case DW_OP_breg30: // 0x8e 1 ULEB128 register
916 case DW_OP_breg31: // 0x8f 1 ULEB128 register
917 case DW_OP_regx: // 0x90 1 ULEB128 register
918 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
919 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
920 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
921 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
922 data.Skip_LEB128(&offset);
923 return offset - data_offset;
925 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
926 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
927 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
928 data.Skip_LEB128(&offset);
929 data.Skip_LEB128(&offset);
930 return offset - data_offset;
932 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
935 uint64_t block_len = data.Skip_LEB128(&offset);
937 return offset - data_offset;
943 return LLDB_INVALID_OFFSET;
946 lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(uint32_t op_addr_idx,
949 if (IsLocationList())
950 return LLDB_INVALID_ADDRESS;
951 lldb::offset_t offset = 0;
952 uint32_t curr_op_addr_idx = 0;
953 while (m_data.ValidOffset(offset)) {
954 const uint8_t op = m_data.GetU8(&offset);
956 if (op == DW_OP_addr) {
957 const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
958 if (curr_op_addr_idx == op_addr_idx)
962 } else if (op == DW_OP_GNU_addr_index) {
963 uint64_t index = m_data.GetULEB128(&offset);
964 if (curr_op_addr_idx == op_addr_idx) {
970 return ReadAddressFromDebugAddrSection(m_dwarf_cu, index);
974 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
975 if (op_arg_size == LLDB_INVALID_OFFSET) {
979 offset += op_arg_size;
982 return LLDB_INVALID_ADDRESS;
985 bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) {
986 if (IsLocationList())
988 lldb::offset_t offset = 0;
989 while (m_data.ValidOffset(offset)) {
990 const uint8_t op = m_data.GetU8(&offset);
992 if (op == DW_OP_addr) {
993 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
994 // We have to make a copy of the data as we don't know if this
995 // data is from a read only memory mapped buffer, so we duplicate
996 // all of the data first, then modify it, and if all goes well,
997 // we then replace the data for this expression
999 // So first we copy the data into a heap buffer
1000 std::unique_ptr<DataBufferHeap> head_data_ap(
1001 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1003 // Make en encoder so we can write the address into the buffer using
1004 // the correct byte order (endianness)
1005 DataEncoder encoder(head_data_ap->GetBytes(), head_data_ap->GetByteSize(),
1006 m_data.GetByteOrder(), addr_byte_size);
1008 // Replace the address in the new buffer
1009 if (encoder.PutMaxU64(offset, addr_byte_size, file_addr) == UINT32_MAX)
1012 // All went well, so now we can reset the data using a shared
1013 // pointer to the heap data so "m_data" will now correctly
1014 // manage the heap data.
1015 m_data.SetData(DataBufferSP(head_data_ap.release()));
1018 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1019 if (op_arg_size == LLDB_INVALID_OFFSET)
1021 offset += op_arg_size;
1027 bool DWARFExpression::ContainsThreadLocalStorage() const {
1028 // We are assuming for now that any thread local variable will not
1029 // have a location list. This has been true for all thread local
1030 // variables we have seen so far produced by any compiler.
1031 if (IsLocationList())
1033 lldb::offset_t offset = 0;
1034 while (m_data.ValidOffset(offset)) {
1035 const uint8_t op = m_data.GetU8(&offset);
1037 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
1039 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1040 if (op_arg_size == LLDB_INVALID_OFFSET)
1043 offset += op_arg_size;
1047 bool DWARFExpression::LinkThreadLocalStorage(
1048 lldb::ModuleSP new_module_sp,
1049 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
1050 &link_address_callback) {
1051 // We are assuming for now that any thread local variable will not
1052 // have a location list. This has been true for all thread local
1053 // variables we have seen so far produced by any compiler.
1054 if (IsLocationList())
1057 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1058 // We have to make a copy of the data as we don't know if this
1059 // data is from a read only memory mapped buffer, so we duplicate
1060 // all of the data first, then modify it, and if all goes well,
1061 // we then replace the data for this expression
1063 // So first we copy the data into a heap buffer
1064 std::shared_ptr<DataBufferHeap> heap_data_sp(
1065 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1067 // Make en encoder so we can write the address into the buffer using
1068 // the correct byte order (endianness)
1069 DataEncoder encoder(heap_data_sp->GetBytes(), heap_data_sp->GetByteSize(),
1070 m_data.GetByteOrder(), addr_byte_size);
1072 lldb::offset_t offset = 0;
1073 lldb::offset_t const_offset = 0;
1074 lldb::addr_t const_value = 0;
1075 size_t const_byte_size = 0;
1076 while (m_data.ValidOffset(offset)) {
1077 const uint8_t op = m_data.GetU8(&offset);
1079 bool decoded_data = false;
1082 // Remember the const offset in case we later have a
1083 // DW_OP_form_tls_address
1084 // or DW_OP_GNU_push_tls_address
1085 const_offset = offset;
1086 const_value = m_data.GetU32(&offset);
1087 decoded_data = true;
1088 const_byte_size = 4;
1092 // Remember the const offset in case we later have a
1093 // DW_OP_form_tls_address
1094 // or DW_OP_GNU_push_tls_address
1095 const_offset = offset;
1096 const_value = m_data.GetU64(&offset);
1097 decoded_data = true;
1098 const_byte_size = 8;
1101 case DW_OP_form_tls_address:
1102 case DW_OP_GNU_push_tls_address:
1103 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
1105 // a file address on the stack. We assume that DW_OP_const4u or
1107 // is used for these values, and we check that the last opcode we got
1109 // either of these was DW_OP_const4u or DW_OP_const8u. If so, then we can
1111 // the value accodingly. For Darwin, the value in the DW_OP_const4u or
1112 // DW_OP_const8u is the file address of a structure that contains a
1114 // pointer, the pthread key and the offset into the data pointed to by the
1115 // pthread key. So we must link this address and also set the module of
1117 // expression to the new_module_sp so we can resolve the file address
1119 if (const_byte_size > 0) {
1120 lldb::addr_t linked_file_addr = link_address_callback(const_value);
1121 if (linked_file_addr == LLDB_INVALID_ADDRESS)
1123 // Replace the address in the new buffer
1124 if (encoder.PutMaxU64(const_offset, const_byte_size,
1125 linked_file_addr) == UINT32_MAX)
1133 const_byte_size = 0;
1137 if (!decoded_data) {
1138 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1139 if (op_arg_size == LLDB_INVALID_OFFSET)
1142 offset += op_arg_size;
1146 // If we linked the TLS address correctly, update the module so that when the
1148 // is evaluated it can resolve the file address to a load address and read the
1150 m_module_wp = new_module_sp;
1151 m_data.SetData(heap_data_sp);
1155 bool DWARFExpression::LocationListContainsAddress(
1156 lldb::addr_t loclist_base_addr, lldb::addr_t addr) const {
1157 if (addr == LLDB_INVALID_ADDRESS)
1160 if (IsLocationList()) {
1161 lldb::offset_t offset = 0;
1163 if (loclist_base_addr == LLDB_INVALID_ADDRESS)
1166 while (m_data.ValidOffset(offset)) {
1167 // We need to figure out what the value is for the location.
1168 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1169 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1170 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1174 if (lo_pc == 0 && hi_pc == 0)
1177 lo_pc += loclist_base_addr - m_loclist_slide;
1178 hi_pc += loclist_base_addr - m_loclist_slide;
1180 if (lo_pc <= addr && addr < hi_pc)
1183 offset += m_data.GetU16(&offset);
1189 bool DWARFExpression::GetLocation(addr_t base_addr, addr_t pc,
1190 lldb::offset_t &offset,
1191 lldb::offset_t &length) {
1193 if (!IsLocationList()) {
1194 length = m_data.GetByteSize();
1198 if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS) {
1199 addr_t curr_base_addr = base_addr;
1201 while (m_data.ValidOffset(offset)) {
1202 // We need to figure out what the value is for the location.
1203 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1204 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1205 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1209 if (lo_pc == 0 && hi_pc == 0)
1212 lo_pc += curr_base_addr - m_loclist_slide;
1213 hi_pc += curr_base_addr - m_loclist_slide;
1215 length = m_data.GetU16(&offset);
1217 if (length > 0 && lo_pc <= pc && pc < hi_pc)
1223 offset = LLDB_INVALID_OFFSET;
1228 bool DWARFExpression::DumpLocationForAddress(Stream *s,
1229 lldb::DescriptionLevel level,
1230 addr_t base_addr, addr_t address,
1232 lldb::offset_t offset = 0;
1233 lldb::offset_t length = 0;
1235 if (GetLocation(base_addr, address, offset, length)) {
1237 DumpLocation(s, offset, length, level, abi);
1244 bool DWARFExpression::Evaluate(ExecutionContextScope *exe_scope,
1245 lldb::addr_t loclist_base_load_addr,
1246 const Value *initial_value_ptr,
1247 const Value *object_address_ptr, Value &result,
1248 Status *error_ptr) const {
1249 ExecutionContext exe_ctx(exe_scope);
1250 return Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, initial_value_ptr,
1251 object_address_ptr, result, error_ptr);
1254 bool DWARFExpression::Evaluate(ExecutionContext *exe_ctx,
1255 RegisterContext *reg_ctx,
1256 lldb::addr_t loclist_base_load_addr,
1257 const Value *initial_value_ptr,
1258 const Value *object_address_ptr, Value &result,
1259 Status *error_ptr) const {
1260 ModuleSP module_sp = m_module_wp.lock();
1262 if (IsLocationList()) {
1263 lldb::offset_t offset = 0;
1265 StackFrame *frame = NULL;
1267 pc = reg_ctx->GetPC();
1269 frame = exe_ctx->GetFramePtr();
1272 RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
1275 pc = reg_ctx_sp->GetPC();
1278 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS) {
1279 if (pc == LLDB_INVALID_ADDRESS) {
1281 error_ptr->SetErrorString("Invalid PC in frame.");
1285 addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
1287 while (m_data.ValidOffset(offset)) {
1288 // We need to figure out what the value is for the location.
1289 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1290 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1291 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
1295 if (lo_pc == 0 && hi_pc == 0)
1298 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
1299 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
1301 uint16_t length = m_data.GetU16(&offset);
1303 if (length > 0 && lo_pc <= pc && pc < hi_pc) {
1304 return DWARFExpression::Evaluate(
1305 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, offset, length,
1306 m_reg_kind, initial_value_ptr, object_address_ptr, result,
1313 error_ptr->SetErrorString("variable not available");
1317 // Not a location list, just a single expression.
1318 return DWARFExpression::Evaluate(
1319 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, 0, m_data.GetByteSize(),
1320 m_reg_kind, initial_value_ptr, object_address_ptr, result, error_ptr);
1323 bool DWARFExpression::Evaluate(
1324 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
1325 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
1326 DWARFCompileUnit *dwarf_cu, const lldb::offset_t opcodes_offset,
1327 const lldb::offset_t opcodes_length, const lldb::RegisterKind reg_kind,
1328 const Value *initial_value_ptr, const Value *object_address_ptr,
1329 Value &result, Status *error_ptr) {
1331 if (opcodes_length == 0) {
1333 error_ptr->SetErrorString(
1334 "no location, value may have been optimized out");
1337 std::vector<Value> stack;
1339 Process *process = NULL;
1340 StackFrame *frame = NULL;
1343 process = exe_ctx->GetProcessPtr();
1344 frame = exe_ctx->GetFramePtr();
1346 if (reg_ctx == NULL && frame)
1347 reg_ctx = frame->GetRegisterContext().get();
1349 if (initial_value_ptr)
1350 stack.push_back(*initial_value_ptr);
1352 lldb::offset_t offset = opcodes_offset;
1353 const lldb::offset_t end_offset = opcodes_offset + opcodes_length;
1357 /// Insertion point for evaluating multi-piece expression.
\13
1358 uint64_t op_piece_offset = 0;
1359 Value pieces; // Used for DW_OP_piece
1361 // Make sure all of the data is available in opcodes.
1362 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length)) {
1364 error_ptr->SetErrorString(
1365 "invalid offset and/or length for opcodes buffer.");
1368 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1370 while (opcodes.ValidOffset(offset) && offset < end_offset) {
1371 const lldb::offset_t op_offset = offset;
1372 const uint8_t op = opcodes.GetU8(&offset);
1374 if (log && log->GetVerbose()) {
1375 size_t count = stack.size();
1376 log->Printf("Stack before operation has %" PRIu64 " values:",
1378 for (size_t i = 0; i < count; ++i) {
1379 StreamString new_value;
1380 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1381 stack[i].Dump(&new_value);
1382 log->Printf(" %s", new_value.GetData());
1384 log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
1387 //----------------------------------------------------------------------
1388 // The DW_OP_addr operation has a single operand that encodes a machine
1389 // address and whose size is the size of an address on the target machine.
1390 //----------------------------------------------------------------------
1392 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1393 stack.back().SetValueType(Value::eValueTypeFileAddress);
1396 //----------------------------------------------------------------------
1397 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1398 // shared libraries that have a location like:
1399 // DW_OP_addr(0x1000)
1400 // If this address resides in a shared library, then this virtual
1401 // address won't make sense when it is evaluated in the context of a
1402 // running process where shared libraries have been slid. To account for
1403 // this, this new address type where we can store the section pointer
1404 // and a 4 byte offset.
1405 //----------------------------------------------------------------------
1406 // case DW_OP_addr_sect_offset4:
1408 // result_type = eResultTypeFileAddress;
1409 // lldb::Section *sect = (lldb::Section
1410 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
1411 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1413 // Address so_addr (sect, sect_offset);
1414 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
1415 // if (load_addr != LLDB_INVALID_ADDRESS)
1417 // // We successfully resolve a file address to a load
1419 // stack.push_back(load_addr);
1426 // error_ptr->SetErrorStringWithFormat ("Section %s in
1427 // %s is not currently loaded.\n",
1428 // sect->GetName().AsCString(),
1429 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1435 //----------------------------------------------------------------------
1436 // OPCODE: DW_OP_deref
1438 // DESCRIPTION: Pops the top stack entry and treats it as an address.
1439 // The value retrieved from that address is pushed. The size of the
1440 // data retrieved from the dereferenced address is the size of an
1441 // address on the target machine.
1442 //----------------------------------------------------------------------
1444 if (stack.empty()) {
1446 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1449 Value::ValueType value_type = stack.back().GetValueType();
1450 switch (value_type) {
1451 case Value::eValueTypeHostAddress: {
1452 void *src = (void *)stack.back().GetScalar().ULongLong();
1454 ::memcpy(&ptr, src, sizeof(void *));
1455 stack.back().GetScalar() = ptr;
1456 stack.back().ClearContext();
1458 case Value::eValueTypeLoadAddress:
1461 lldb::addr_t pointer_addr =
1462 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1464 lldb::addr_t pointer_value =
1465 process->ReadPointerFromMemory(pointer_addr, error);
1466 if (pointer_value != LLDB_INVALID_ADDRESS) {
1467 stack.back().GetScalar() = pointer_value;
1468 stack.back().ClearContext();
1471 error_ptr->SetErrorStringWithFormat(
1472 "Failed to dereference pointer from 0x%" PRIx64
1473 " for DW_OP_deref: %s\n",
1474 pointer_addr, error.AsCString());
1479 error_ptr->SetErrorStringWithFormat(
1480 "NULL process for DW_OP_deref.\n");
1485 error_ptr->SetErrorStringWithFormat(
1486 "NULL execution context for DW_OP_deref.\n");
1497 //----------------------------------------------------------------------
1498 // OPCODE: DW_OP_deref_size
1500 // 1 - uint8_t that specifies the size of the data to dereference.
1501 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1502 // stack entry and treats it as an address. The value retrieved from that
1503 // address is pushed. In the DW_OP_deref_size operation, however, the
1504 // size in bytes of the data retrieved from the dereferenced address is
1505 // specified by the single operand. This operand is a 1-byte unsigned
1506 // integral constant whose value may not be larger than the size of an
1507 // address on the target machine. The data retrieved is zero extended
1508 // to the size of an address on the target machine before being pushed
1509 // on the expression stack.
1510 //----------------------------------------------------------------------
1511 case DW_OP_deref_size: {
1512 if (stack.empty()) {
1514 error_ptr->SetErrorString(
1515 "Expression stack empty for DW_OP_deref_size.");
1518 uint8_t size = opcodes.GetU8(&offset);
1519 Value::ValueType value_type = stack.back().GetValueType();
1520 switch (value_type) {
1521 case Value::eValueTypeHostAddress: {
1522 void *src = (void *)stack.back().GetScalar().ULongLong();
1524 ::memcpy(&ptr, src, sizeof(void *));
1525 // I can't decide whether the size operand should apply to the bytes in
1527 // lldb-host endianness or the target endianness.. I doubt this'll ever
1529 // but I'll opt for assuming big endian regardless.
1538 ptr = ptr & 0xffffff;
1541 ptr = ptr & 0xffffffff;
1543 // the casts are added to work around the case where intptr_t is a 32
1545 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1548 ptr = (intptr_t)ptr & 0xffffffffffULL;
1551 ptr = (intptr_t)ptr & 0xffffffffffffULL;
1554 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1559 stack.back().GetScalar() = ptr;
1560 stack.back().ClearContext();
1562 case Value::eValueTypeLoadAddress:
1565 lldb::addr_t pointer_addr =
1566 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1567 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1569 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1571 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
1572 process->GetByteOrder(), size);
1573 lldb::offset_t addr_data_offset = 0;
1576 stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
1579 stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
1582 stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
1585 stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
1588 stack.back().GetScalar() =
1589 addr_data.GetPointer(&addr_data_offset);
1591 stack.back().ClearContext();
1594 error_ptr->SetErrorStringWithFormat(
1595 "Failed to dereference pointer from 0x%" PRIx64
1596 " for DW_OP_deref: %s\n",
1597 pointer_addr, error.AsCString());
1602 error_ptr->SetErrorStringWithFormat(
1603 "NULL process for DW_OP_deref.\n");
1608 error_ptr->SetErrorStringWithFormat(
1609 "NULL execution context for DW_OP_deref.\n");
1620 //----------------------------------------------------------------------
1621 // OPCODE: DW_OP_xderef_size
1623 // 1 - uint8_t that specifies the size of the data to dereference.
1624 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1625 // the top of the stack is treated as an address. The second stack
1626 // entry is treated as an "address space identifier" for those
1627 // architectures that support multiple address spaces. The top two
1628 // stack elements are popped, a data item is retrieved through an
1629 // implementation-defined address calculation and pushed as the new
1630 // stack top. In the DW_OP_xderef_size operation, however, the size in
1631 // bytes of the data retrieved from the dereferenced address is
1632 // specified by the single operand. This operand is a 1-byte unsigned
1633 // integral constant whose value may not be larger than the size of an
1634 // address on the target machine. The data retrieved is zero extended
1635 // to the size of an address on the target machine before being pushed
1636 // on the expression stack.
1637 //----------------------------------------------------------------------
1638 case DW_OP_xderef_size:
1640 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1642 //----------------------------------------------------------------------
1643 // OPCODE: DW_OP_xderef
1645 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1646 // the top of the stack is treated as an address. The second stack entry
1647 // is treated as an "address space identifier" for those architectures
1648 // that support multiple address spaces. The top two stack elements are
1649 // popped, a data item is retrieved through an implementation-defined
1650 // address calculation and pushed as the new stack top. The size of the
1651 // data retrieved from the dereferenced address is the size of an address
1652 // on the target machine.
1653 //----------------------------------------------------------------------
1656 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1659 //----------------------------------------------------------------------
1660 // All DW_OP_constXXX opcodes have a single operand as noted below:
1663 // --------------- ----------------------------------------------------
1664 // DW_OP_const1u 1-byte unsigned integer constant
1665 // DW_OP_const1s 1-byte signed integer constant
1666 // DW_OP_const2u 2-byte unsigned integer constant
1667 // DW_OP_const2s 2-byte signed integer constant
1668 // DW_OP_const4u 4-byte unsigned integer constant
1669 // DW_OP_const4s 4-byte signed integer constant
1670 // DW_OP_const8u 8-byte unsigned integer constant
1671 // DW_OP_const8s 8-byte signed integer constant
1672 // DW_OP_constu unsigned LEB128 integer constant
1673 // DW_OP_consts signed LEB128 integer constant
1674 //----------------------------------------------------------------------
1676 stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
1679 stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
1682 stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
1685 stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
1688 stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
1691 stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
1694 stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
1697 stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
1700 stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1703 stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1706 //----------------------------------------------------------------------
1707 // OPCODE: DW_OP_dup
1709 // DESCRIPTION: duplicates the value at the top of the stack
1710 //----------------------------------------------------------------------
1712 if (stack.empty()) {
1714 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1717 stack.push_back(stack.back());
1720 //----------------------------------------------------------------------
1721 // OPCODE: DW_OP_drop
1723 // DESCRIPTION: pops the value at the top of the stack
1724 //----------------------------------------------------------------------
1726 if (stack.empty()) {
1728 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1734 //----------------------------------------------------------------------
1735 // OPCODE: DW_OP_over
1737 // DESCRIPTION: Duplicates the entry currently second in the stack at
1738 // the top of the stack.
1739 //----------------------------------------------------------------------
1741 if (stack.size() < 2) {
1743 error_ptr->SetErrorString(
1744 "Expression stack needs at least 2 items for DW_OP_over.");
1747 stack.push_back(stack[stack.size() - 2]);
1750 //----------------------------------------------------------------------
1751 // OPCODE: DW_OP_pick
1752 // OPERANDS: uint8_t index into the current stack
1753 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1754 // inclusive) is pushed on the stack
1755 //----------------------------------------------------------------------
1757 uint8_t pick_idx = opcodes.GetU8(&offset);
1758 if (pick_idx < stack.size())
1759 stack.push_back(stack[pick_idx]);
1762 error_ptr->SetErrorStringWithFormat(
1763 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1768 //----------------------------------------------------------------------
1769 // OPCODE: DW_OP_swap
1771 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1772 // of the stack becomes the second stack entry, and the second entry
1773 // becomes the top of the stack
1774 //----------------------------------------------------------------------
1776 if (stack.size() < 2) {
1778 error_ptr->SetErrorString(
1779 "Expression stack needs at least 2 items for DW_OP_swap.");
1783 stack.back() = stack[stack.size() - 2];
1784 stack[stack.size() - 2] = tmp;
1788 //----------------------------------------------------------------------
1789 // OPCODE: DW_OP_rot
1791 // DESCRIPTION: Rotates the first three stack entries. The entry at
1792 // the top of the stack becomes the third stack entry, the second
1793 // entry becomes the top of the stack, and the third entry becomes
1794 // the second entry.
1795 //----------------------------------------------------------------------
1797 if (stack.size() < 3) {
1799 error_ptr->SetErrorString(
1800 "Expression stack needs at least 3 items for DW_OP_rot.");
1803 size_t last_idx = stack.size() - 1;
1804 Value old_top = stack[last_idx];
1805 stack[last_idx] = stack[last_idx - 1];
1806 stack[last_idx - 1] = stack[last_idx - 2];
1807 stack[last_idx - 2] = old_top;
1811 //----------------------------------------------------------------------
1812 // OPCODE: DW_OP_abs
1814 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1815 // value and pushes its absolute value. If the absolute value can not be
1816 // represented, the result is undefined.
1817 //----------------------------------------------------------------------
1819 if (stack.empty()) {
1821 error_ptr->SetErrorString(
1822 "Expression stack needs at least 1 item for DW_OP_abs.");
1824 } else if (stack.back().ResolveValue(exe_ctx).AbsoluteValue() == false) {
1826 error_ptr->SetErrorString(
1827 "Failed to take the absolute value of the first stack item.");
1832 //----------------------------------------------------------------------
1833 // OPCODE: DW_OP_and
1835 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1836 // operation on the two, and pushes the result.
1837 //----------------------------------------------------------------------
1839 if (stack.size() < 2) {
1841 error_ptr->SetErrorString(
1842 "Expression stack needs at least 2 items for DW_OP_and.");
1847 stack.back().ResolveValue(exe_ctx) =
1848 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1852 //----------------------------------------------------------------------
1853 // OPCODE: DW_OP_div
1855 // DESCRIPTION: pops the top two stack values, divides the former second
1856 // entry by the former top of the stack using signed division, and
1857 // pushes the result.
1858 //----------------------------------------------------------------------
1860 if (stack.size() < 2) {
1862 error_ptr->SetErrorString(
1863 "Expression stack needs at least 2 items for DW_OP_div.");
1867 if (tmp.ResolveValue(exe_ctx).IsZero()) {
1869 error_ptr->SetErrorString("Divide by zero.");
1874 stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1875 if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1877 error_ptr->SetErrorString("Divide failed.");
1884 //----------------------------------------------------------------------
1885 // OPCODE: DW_OP_minus
1887 // DESCRIPTION: pops the top two stack values, subtracts the former top
1888 // of the stack from the former second entry, and pushes the result.
1889 //----------------------------------------------------------------------
1891 if (stack.size() < 2) {
1893 error_ptr->SetErrorString(
1894 "Expression stack needs at least 2 items for DW_OP_minus.");
1899 stack.back().ResolveValue(exe_ctx) =
1900 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1904 //----------------------------------------------------------------------
1905 // OPCODE: DW_OP_mod
1907 // DESCRIPTION: pops the top two stack values and pushes the result of
1908 // the calculation: former second stack entry modulo the former top of
1910 //----------------------------------------------------------------------
1912 if (stack.size() < 2) {
1914 error_ptr->SetErrorString(
1915 "Expression stack needs at least 2 items for DW_OP_mod.");
1920 stack.back().ResolveValue(exe_ctx) =
1921 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1925 //----------------------------------------------------------------------
1926 // OPCODE: DW_OP_mul
1928 // DESCRIPTION: pops the top two stack entries, multiplies them
1929 // together, and pushes the result.
1930 //----------------------------------------------------------------------
1932 if (stack.size() < 2) {
1934 error_ptr->SetErrorString(
1935 "Expression stack needs at least 2 items for DW_OP_mul.");
1940 stack.back().ResolveValue(exe_ctx) =
1941 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1945 //----------------------------------------------------------------------
1946 // OPCODE: DW_OP_neg
1948 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1949 //----------------------------------------------------------------------
1951 if (stack.empty()) {
1953 error_ptr->SetErrorString(
1954 "Expression stack needs at least 1 item for DW_OP_neg.");
1957 if (stack.back().ResolveValue(exe_ctx).UnaryNegate() == false) {
1959 error_ptr->SetErrorString("Unary negate failed.");
1965 //----------------------------------------------------------------------
1966 // OPCODE: DW_OP_not
1968 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1970 //----------------------------------------------------------------------
1972 if (stack.empty()) {
1974 error_ptr->SetErrorString(
1975 "Expression stack needs at least 1 item for DW_OP_not.");
1978 if (stack.back().ResolveValue(exe_ctx).OnesComplement() == false) {
1980 error_ptr->SetErrorString("Logical NOT failed.");
1986 //----------------------------------------------------------------------
1989 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1990 // operation on the two, and pushes the result.
1991 //----------------------------------------------------------------------
1993 if (stack.size() < 2) {
1995 error_ptr->SetErrorString(
1996 "Expression stack needs at least 2 items for DW_OP_or.");
2001 stack.back().ResolveValue(exe_ctx) =
2002 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
2006 //----------------------------------------------------------------------
2007 // OPCODE: DW_OP_plus
2009 // DESCRIPTION: pops the top two stack entries, adds them together, and
2010 // pushes the result.
2011 //----------------------------------------------------------------------
2013 if (stack.size() < 2) {
2015 error_ptr->SetErrorString(
2016 "Expression stack needs at least 2 items for DW_OP_plus.");
2021 stack.back().GetScalar() += tmp.GetScalar();
2025 //----------------------------------------------------------------------
2026 // OPCODE: DW_OP_plus_uconst
2028 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
2029 // constant operand and pushes the result.
2030 //----------------------------------------------------------------------
2031 case DW_OP_plus_uconst:
2032 if (stack.empty()) {
2034 error_ptr->SetErrorString(
2035 "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
2038 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
2039 // Implicit conversion from a UINT to a Scalar...
2040 stack.back().GetScalar() += uconst_value;
2041 if (!stack.back().GetScalar().IsValid()) {
2043 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
2049 //----------------------------------------------------------------------
2050 // OPCODE: DW_OP_shl
2052 // DESCRIPTION: pops the top two stack entries, shifts the former
2053 // second entry left by the number of bits specified by the former top
2054 // of the stack, and pushes the result.
2055 //----------------------------------------------------------------------
2057 if (stack.size() < 2) {
2059 error_ptr->SetErrorString(
2060 "Expression stack needs at least 2 items for DW_OP_shl.");
2065 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
2069 //----------------------------------------------------------------------
2070 // OPCODE: DW_OP_shr
2072 // DESCRIPTION: pops the top two stack entries, shifts the former second
2073 // entry right logically (filling with zero bits) by the number of bits
2074 // specified by the former top of the stack, and pushes the result.
2075 //----------------------------------------------------------------------
2077 if (stack.size() < 2) {
2079 error_ptr->SetErrorString(
2080 "Expression stack needs at least 2 items for DW_OP_shr.");
2085 if (stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
2086 tmp.ResolveValue(exe_ctx)) == false) {
2088 error_ptr->SetErrorString("DW_OP_shr failed.");
2094 //----------------------------------------------------------------------
2095 // OPCODE: DW_OP_shra
2097 // DESCRIPTION: pops the top two stack entries, shifts the former second
2098 // entry right arithmetically (divide the magnitude by 2, keep the same
2099 // sign for the result) by the number of bits specified by the former
2100 // top of the stack, and pushes the result.
2101 //----------------------------------------------------------------------
2103 if (stack.size() < 2) {
2105 error_ptr->SetErrorString(
2106 "Expression stack needs at least 2 items for DW_OP_shra.");
2111 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
2115 //----------------------------------------------------------------------
2116 // OPCODE: DW_OP_xor
2118 // DESCRIPTION: pops the top two stack entries, performs the bitwise
2119 // exclusive-or operation on the two, and pushes the result.
2120 //----------------------------------------------------------------------
2122 if (stack.size() < 2) {
2124 error_ptr->SetErrorString(
2125 "Expression stack needs at least 2 items for DW_OP_xor.");
2130 stack.back().ResolveValue(exe_ctx) =
2131 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
2135 //----------------------------------------------------------------------
2136 // OPCODE: DW_OP_skip
2137 // OPERANDS: int16_t
2138 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
2139 // signed integer constant. The 2-byte constant is the number of bytes
2140 // of the DWARF expression to skip forward or backward from the current
2141 // operation, beginning after the 2-byte constant.
2142 //----------------------------------------------------------------------
2144 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
2145 lldb::offset_t new_offset = offset + skip_offset;
2146 if (new_offset >= opcodes_offset && new_offset < end_offset)
2147 offset = new_offset;
2150 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
2155 //----------------------------------------------------------------------
2156 // OPCODE: DW_OP_bra
2157 // OPERANDS: int16_t
2158 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
2159 // signed integer constant. This operation pops the top of stack. If
2160 // the value popped is not the constant 0, the 2-byte constant operand
2161 // is the number of bytes of the DWARF expression to skip forward or
2162 // backward from the current operation, beginning after the 2-byte
2164 //----------------------------------------------------------------------
2166 if (stack.empty()) {
2168 error_ptr->SetErrorString(
2169 "Expression stack needs at least 1 item for DW_OP_bra.");
2174 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
2176 if (tmp.ResolveValue(exe_ctx) != zero) {
2177 lldb::offset_t new_offset = offset + bra_offset;
2178 if (new_offset >= opcodes_offset && new_offset < end_offset)
2179 offset = new_offset;
2182 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
2189 //----------------------------------------------------------------------
2192 // DESCRIPTION: pops the top two stack values, compares using the
2193 // equals (==) operator.
2194 // STACK RESULT: push the constant value 1 onto the stack if the result
2195 // of the operation is true or the constant value 0 if the result of the
2196 // operation is false.
2197 //----------------------------------------------------------------------
2199 if (stack.size() < 2) {
2201 error_ptr->SetErrorString(
2202 "Expression stack needs at least 2 items for DW_OP_eq.");
2207 stack.back().ResolveValue(exe_ctx) =
2208 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
2212 //----------------------------------------------------------------------
2215 // DESCRIPTION: pops the top two stack values, compares using the
2216 // greater than or equal to (>=) operator.
2217 // STACK RESULT: push the constant value 1 onto the stack if the result
2218 // of the operation is true or the constant value 0 if the result of the
2219 // operation is false.
2220 //----------------------------------------------------------------------
2222 if (stack.size() < 2) {
2224 error_ptr->SetErrorString(
2225 "Expression stack needs at least 2 items for DW_OP_ge.");
2230 stack.back().ResolveValue(exe_ctx) =
2231 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
2235 //----------------------------------------------------------------------
2238 // DESCRIPTION: pops the top two stack values, compares using the
2239 // greater than (>) operator.
2240 // STACK RESULT: push the constant value 1 onto the stack if the result
2241 // of the operation is true or the constant value 0 if the result of the
2242 // operation is false.
2243 //----------------------------------------------------------------------
2245 if (stack.size() < 2) {
2247 error_ptr->SetErrorString(
2248 "Expression stack needs at least 2 items for DW_OP_gt.");
2253 stack.back().ResolveValue(exe_ctx) =
2254 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
2258 //----------------------------------------------------------------------
2261 // DESCRIPTION: pops the top two stack values, compares using the
2262 // less than or equal to (<=) operator.
2263 // STACK RESULT: push the constant value 1 onto the stack if the result
2264 // of the operation is true or the constant value 0 if the result of the
2265 // operation is false.
2266 //----------------------------------------------------------------------
2268 if (stack.size() < 2) {
2270 error_ptr->SetErrorString(
2271 "Expression stack needs at least 2 items for DW_OP_le.");
2276 stack.back().ResolveValue(exe_ctx) =
2277 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
2281 //----------------------------------------------------------------------
2284 // DESCRIPTION: pops the top two stack values, compares using the
2285 // less than (<) operator.
2286 // STACK RESULT: push the constant value 1 onto the stack if the result
2287 // of the operation is true or the constant value 0 if the result of the
2288 // operation is false.
2289 //----------------------------------------------------------------------
2291 if (stack.size() < 2) {
2293 error_ptr->SetErrorString(
2294 "Expression stack needs at least 2 items for DW_OP_lt.");
2299 stack.back().ResolveValue(exe_ctx) =
2300 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
2304 //----------------------------------------------------------------------
2307 // DESCRIPTION: pops the top two stack values, compares using the
2308 // not equal (!=) operator.
2309 // STACK RESULT: push the constant value 1 onto the stack if the result
2310 // of the operation is true or the constant value 0 if the result of the
2311 // operation is false.
2312 //----------------------------------------------------------------------
2314 if (stack.size() < 2) {
2316 error_ptr->SetErrorString(
2317 "Expression stack needs at least 2 items for DW_OP_ne.");
2322 stack.back().ResolveValue(exe_ctx) =
2323 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
2327 //----------------------------------------------------------------------
2328 // OPCODE: DW_OP_litn
2330 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
2331 // STACK RESULT: push the unsigned literal constant value onto the top
2333 //----------------------------------------------------------------------
2366 stack.push_back(Scalar(op - DW_OP_lit0));
2369 //----------------------------------------------------------------------
2370 // OPCODE: DW_OP_regN
2372 // DESCRIPTION: Push the value in register n on the top of the stack.
2373 //----------------------------------------------------------------------
2406 reg_num = op - DW_OP_reg0;
2408 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2409 stack.push_back(tmp);
2413 //----------------------------------------------------------------------
2414 // OPCODE: DW_OP_regx
2416 // ULEB128 literal operand that encodes the register.
2417 // DESCRIPTION: Push the value in register on the top of the stack.
2418 //----------------------------------------------------------------------
2420 reg_num = opcodes.GetULEB128(&offset);
2421 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2422 stack.push_back(tmp);
2427 //----------------------------------------------------------------------
2428 // OPCODE: DW_OP_bregN
2430 // SLEB128 offset from register N
2431 // DESCRIPTION: Value is in memory at the address specified by register
2432 // N plus an offset.
2433 //----------------------------------------------------------------------
2465 case DW_OP_breg31: {
2466 reg_num = op - DW_OP_breg0;
2468 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2470 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2471 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2473 stack.push_back(tmp);
2474 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2478 //----------------------------------------------------------------------
2479 // OPCODE: DW_OP_bregx
2481 // ULEB128 literal operand that encodes the register.
2482 // SLEB128 offset from register N
2483 // DESCRIPTION: Value is in memory at the address specified by register
2484 // N plus an offset.
2485 //----------------------------------------------------------------------
2487 reg_num = opcodes.GetULEB128(&offset);
2489 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2491 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2492 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2494 stack.push_back(tmp);
2495 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2504 if (frame->GetFrameBaseValue(value, error_ptr)) {
2505 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2506 value += fbreg_offset;
2507 stack.push_back(value);
2508 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2513 error_ptr->SetErrorString(
2514 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2519 error_ptr->SetErrorStringWithFormat(
2520 "NULL execution context for DW_OP_fbreg.\n");
2526 //----------------------------------------------------------------------
2527 // OPCODE: DW_OP_nop
2529 // DESCRIPTION: A place holder. It has no effect on the location stack
2530 // or any of its values.
2531 //----------------------------------------------------------------------
2535 //----------------------------------------------------------------------
2536 // OPCODE: DW_OP_piece
2538 // ULEB128: byte size of the piece
2539 // DESCRIPTION: The operand describes the size in bytes of the piece of
2540 // the object referenced by the DWARF expression whose result is at the
2541 // top of the stack. If the piece is located in a register, but does not
2542 // occupy the entire register, the placement of the piece within that
2543 // register is defined by the ABI.
2545 // Many compilers store a single variable in sets of registers, or store
2546 // a variable partially in memory and partially in registers.
2547 // DW_OP_piece provides a way of describing how large a part of a
2548 // variable a particular DWARF expression refers to.
2549 //----------------------------------------------------------------------
2551 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2553 if (piece_byte_size > 0) {
2556 if (stack.empty()) {
2557 // In a multi-piece expression, this means that the current piece is
2559 // Fill with zeros for now by resizing the data and appending it
2560 curr_piece.ResizeData(piece_byte_size);
2561 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2562 pieces.AppendDataToHostBuffer(curr_piece);
2565 // Extract the current piece into "curr_piece"
2566 Value curr_piece_source_value(stack.back());
2569 const Value::ValueType curr_piece_source_value_type =
2570 curr_piece_source_value.GetValueType();
2571 switch (curr_piece_source_value_type) {
2572 case Value::eValueTypeLoadAddress:
2574 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2575 lldb::addr_t load_addr =
2576 curr_piece_source_value.GetScalar().ULongLong(
2577 LLDB_INVALID_ADDRESS);
2578 if (process->ReadMemory(
2579 load_addr, curr_piece.GetBuffer().GetBytes(),
2580 piece_byte_size, error) != piece_byte_size) {
2582 error_ptr->SetErrorStringWithFormat(
2583 "failed to read memory DW_OP_piece(%" PRIu64
2584 ") from 0x%" PRIx64,
2585 piece_byte_size, load_addr);
2590 error_ptr->SetErrorStringWithFormat(
2591 "failed to resize the piece memory buffer for "
2592 "DW_OP_piece(%" PRIu64 ")",
2599 case Value::eValueTypeFileAddress:
2600 case Value::eValueTypeHostAddress:
2602 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2603 LLDB_INVALID_ADDRESS);
2604 error_ptr->SetErrorStringWithFormat(
2605 "failed to read memory DW_OP_piece(%" PRIu64
2606 ") from %s address 0x%" PRIx64,
2607 piece_byte_size, curr_piece_source_value.GetValueType() ==
2608 Value::eValueTypeFileAddress
2615 case Value::eValueTypeScalar: {
2616 uint32_t bit_size = piece_byte_size * 8;
2617 uint32_t bit_offset = 0;
2618 if (!curr_piece_source_value.GetScalar().ExtractBitfield(
2619 bit_size, bit_offset)) {
2621 error_ptr->SetErrorStringWithFormat(
2622 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2623 " byte scalar value.",
2625 (uint64_t)curr_piece_source_value.GetScalar()
2629 curr_piece = curr_piece_source_value;
2632 case Value::eValueTypeVector: {
2633 if (curr_piece_source_value.GetVector().length >= piece_byte_size)
2634 curr_piece_source_value.GetVector().length = piece_byte_size;
2637 error_ptr->SetErrorStringWithFormat(
2638 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2639 " byte vector value.",
2641 (uint64_t)curr_piece_source_value.GetVector().length);
2647 // Check if this is the first piece?
2648 if (op_piece_offset == 0) {
2649 // This is the first piece, we should push it back onto the stack so
2651 // pieces will be able to access this piece and add to it
2652 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2654 error_ptr->SetErrorString("failed to append piece data");
2658 // If this is the second or later piece there should be a value on
2660 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2662 error_ptr->SetErrorStringWithFormat(
2663 "DW_OP_piece for offset %" PRIu64
2664 " but top of stack is of size %" PRIu64,
2665 op_piece_offset, pieces.GetBuffer().GetByteSize());
2669 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2671 error_ptr->SetErrorString("failed to append piece data");
2675 op_piece_offset += piece_byte_size;
2680 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2681 if (stack.size() < 1) {
2683 error_ptr->SetErrorString(
2684 "Expression stack needs at least 1 item for DW_OP_bit_piece.");
2687 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2688 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2689 switch (stack.back().GetValueType()) {
2690 case Value::eValueTypeScalar: {
2691 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2692 piece_bit_offset)) {
2694 error_ptr->SetErrorStringWithFormat(
2695 "unable to extract %" PRIu64 " bit value with %" PRIu64
2696 " bit offset from a %" PRIu64 " bit scalar value.",
2697 piece_bit_size, piece_bit_offset,
2698 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2703 case Value::eValueTypeFileAddress:
2704 case Value::eValueTypeLoadAddress:
2705 case Value::eValueTypeHostAddress:
2707 error_ptr->SetErrorStringWithFormat(
2708 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2709 ", bit_offset = %" PRIu64 ") from an addresss value.",
2710 piece_bit_size, piece_bit_offset);
2714 case Value::eValueTypeVector:
2716 error_ptr->SetErrorStringWithFormat(
2717 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2718 ", bit_offset = %" PRIu64 ") from a vector value.",
2719 piece_bit_size, piece_bit_offset);
2726 //----------------------------------------------------------------------
2727 // OPCODE: DW_OP_push_object_address
2729 // DESCRIPTION: Pushes the address of the object currently being
2730 // evaluated as part of evaluation of a user presented expression.
2731 // This object may correspond to an independent variable described by
2732 // its own DIE or it may be a component of an array, structure, or class
2733 // whose address has been dynamically determined by an earlier step
2734 // during user expression evaluation.
2735 //----------------------------------------------------------------------
2736 case DW_OP_push_object_address:
2737 if (object_address_ptr)
2738 stack.push_back(*object_address_ptr);
2741 error_ptr->SetErrorString("DW_OP_push_object_address used without "
2742 "specifying an object address");
2747 //----------------------------------------------------------------------
2748 // OPCODE: DW_OP_call2
2750 // uint16_t compile unit relative offset of a DIE
2751 // DESCRIPTION: Performs subroutine calls during evaluation
2752 // of a DWARF expression. The operand is the 2-byte unsigned offset
2753 // of a debugging information entry in the current compilation unit.
2755 // Operand interpretation is exactly like that for DW_FORM_ref2.
2757 // This operation transfers control of DWARF expression evaluation
2758 // to the DW_AT_location attribute of the referenced DIE. If there is
2759 // no such attribute, then there is no effect. Execution of the DWARF
2760 // expression of a DW_AT_location attribute may add to and/or remove from
2761 // values on the stack. Execution returns to the point following the call
2762 // when the end of the attribute is reached. Values on the stack at the
2763 // time of the call may be used as parameters by the called expression
2764 // and values left on the stack by the called expression may be used as
2765 // return values by prior agreement between the calling and called
2767 //----------------------------------------------------------------------
2770 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2772 //----------------------------------------------------------------------
2773 // OPCODE: DW_OP_call4
2775 // uint32_t compile unit relative offset of a DIE
2776 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2777 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset
2778 // of a debugging information entry in the current compilation unit.
2780 // Operand interpretation DW_OP_call4 is exactly like that for
2783 // This operation transfers control of DWARF expression evaluation
2784 // to the DW_AT_location attribute of the referenced DIE. If there is
2785 // no such attribute, then there is no effect. Execution of the DWARF
2786 // expression of a DW_AT_location attribute may add to and/or remove from
2787 // values on the stack. Execution returns to the point following the call
2788 // when the end of the attribute is reached. Values on the stack at the
2789 // time of the call may be used as parameters by the called expression
2790 // and values left on the stack by the called expression may be used as
2791 // return values by prior agreement between the calling and called
2793 //----------------------------------------------------------------------
2796 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2799 //----------------------------------------------------------------------
2800 // OPCODE: DW_OP_stack_value
2802 // DESCRIPTION: Specifies that the object does not exist in memory but
2803 // rather is a constant value. The value from the top of the stack is
2804 // the value to be used. This is the actual object value and not the
2806 //----------------------------------------------------------------------
2807 case DW_OP_stack_value:
2808 stack.back().SetValueType(Value::eValueTypeScalar);
2811 //----------------------------------------------------------------------
2812 // OPCODE: DW_OP_call_frame_cfa
2814 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2815 // the canonical frame address consistent with the call frame information
2816 // located in .debug_frame (or in the FDEs of the eh_frame section).
2817 //----------------------------------------------------------------------
2818 case DW_OP_call_frame_cfa:
2820 // Note that we don't have to parse FDEs because this DWARF expression
2821 // is commonly evaluated with a valid stack frame.
2822 StackID id = frame->GetStackID();
2823 addr_t cfa = id.GetCallFrameAddress();
2824 if (cfa != LLDB_INVALID_ADDRESS) {
2825 stack.push_back(Scalar(cfa));
2826 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2827 } else if (error_ptr)
2828 error_ptr->SetErrorString("Stack frame does not include a canonical "
2829 "frame address for DW_OP_call_frame_cfa "
2833 error_ptr->SetErrorString("Invalid stack frame in context for "
2834 "DW_OP_call_frame_cfa opcode.");
2839 //----------------------------------------------------------------------
2840 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2841 // opcode, DW_OP_GNU_push_tls_address)
2843 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2844 // an address in the current thread's thread-local storage block,
2845 // and pushes it on the stack.
2846 //----------------------------------------------------------------------
2847 case DW_OP_form_tls_address:
2848 case DW_OP_GNU_push_tls_address: {
2849 if (stack.size() < 1) {
2851 if (op == DW_OP_form_tls_address)
2852 error_ptr->SetErrorString(
2853 "DW_OP_form_tls_address needs an argument.");
2855 error_ptr->SetErrorString(
2856 "DW_OP_GNU_push_tls_address needs an argument.");
2861 if (!exe_ctx || !module_sp) {
2863 error_ptr->SetErrorString("No context to evaluate TLS within.");
2867 Thread *thread = exe_ctx->GetThreadPtr();
2870 error_ptr->SetErrorString("No thread to evaluate TLS within.");
2874 // Lookup the TLS block address for this thread and module.
2875 const addr_t tls_file_addr =
2876 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2877 const addr_t tls_load_addr =
2878 thread->GetThreadLocalData(module_sp, tls_file_addr);
2880 if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2882 error_ptr->SetErrorString(
2883 "No TLS data currently exists for this thread.");
2887 stack.back().GetScalar() = tls_load_addr;
2888 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2891 //----------------------------------------------------------------------
2892 // OPCODE: DW_OP_GNU_addr_index
2894 // ULEB128: index to the .debug_addr section
2895 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2896 // section with the base address specified by the DW_AT_addr_base
2897 // attribute and the 0 based index is the ULEB128 encoded index.
2898 //----------------------------------------------------------------------
2899 case DW_OP_GNU_addr_index: {
2902 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2903 "compile unit being specified");
2906 uint64_t index = opcodes.GetULEB128(&offset);
2907 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2908 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2909 lldb::offset_t offset = addr_base + index * index_size;
2911 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
2912 &offset, index_size);
2913 stack.push_back(Scalar(value));
2914 stack.back().SetValueType(Value::eValueTypeFileAddress);
2917 //----------------------------------------------------------------------
2918 // OPCODE: DW_OP_GNU_const_index
2920 // ULEB128: index to the .debug_addr section
2921 // DESCRIPTION: Pushes an constant with the size of a machine address to
2922 // the stack from the .debug_addr section with the base address specified
2923 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2925 //----------------------------------------------------------------------
2926 case DW_OP_GNU_const_index: {
2929 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2930 "compile unit being specified");
2933 uint64_t index = opcodes.GetULEB128(&offset);
2934 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2935 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2936 lldb::offset_t offset = addr_base + index * index_size;
2937 const DWARFDataExtractor &debug_addr =
2938 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data();
2939 switch (index_size) {
2941 stack.push_back(Scalar(debug_addr.GetU32(&offset)));
2944 stack.push_back(Scalar(debug_addr.GetU64(&offset)));
2947 assert(false && "Unhandled index size");
2954 log->Printf("Unhandled opcode %s in DWARFExpression.",
2955 DW_OP_value_to_name(op));
2960 if (stack.empty()) {
2961 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2962 // or DW_OP_bit_piece opcodes
2963 if (pieces.GetBuffer().GetByteSize()) {
2967 error_ptr->SetErrorString("Stack empty after evaluation.");
2971 if (log && log->GetVerbose()) {
2972 size_t count = stack.size();
2973 log->Printf("Stack after operation has %" PRIu64 " values:",
2975 for (size_t i = 0; i < count; ++i) {
2976 StreamString new_value;
2977 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2978 stack[i].Dump(&new_value);
2979 log->Printf(" %s", new_value.GetData());
2982 result = stack.back();
2984 return true; // Return true on success
2987 size_t DWARFExpression::LocationListSize(const DWARFCompileUnit *dwarf_cu,
2988 const DataExtractor &debug_loc_data,
2989 lldb::offset_t offset) {
2990 const lldb::offset_t debug_loc_offset = offset;
2991 while (debug_loc_data.ValidOffset(offset)) {
2992 lldb::addr_t start_addr = LLDB_INVALID_ADDRESS;
2993 lldb::addr_t end_addr = LLDB_INVALID_ADDRESS;
2994 if (!AddressRangeForLocationListEntry(dwarf_cu, debug_loc_data, &offset,
2995 start_addr, end_addr))
2998 if (start_addr == 0 && end_addr == 0)
3001 uint16_t loc_length = debug_loc_data.GetU16(&offset);
3002 offset += loc_length;
3005 if (offset > debug_loc_offset)
3006 return offset - debug_loc_offset;
3010 bool DWARFExpression::AddressRangeForLocationListEntry(
3011 const DWARFCompileUnit *dwarf_cu, const DataExtractor &debug_loc_data,
3012 lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc) {
3013 if (!debug_loc_data.ValidOffset(*offset_ptr))
3016 switch (dwarf_cu->GetSymbolFileDWARF()->GetLocationListFormat()) {
3017 case NonLocationList:
3019 case RegularLocationList:
3020 low_pc = debug_loc_data.GetAddress(offset_ptr);
3021 high_pc = debug_loc_data.GetAddress(offset_ptr);
3023 case SplitDwarfLocationList:
3024 switch (debug_loc_data.GetU8(offset_ptr)) {
3025 case DW_LLE_end_of_list:
3027 case DW_LLE_startx_endx: {
3028 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3029 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3030 index = debug_loc_data.GetULEB128(offset_ptr);
3031 high_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3034 case DW_LLE_startx_length: {
3035 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3036 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3037 uint32_t length = debug_loc_data.GetU32(offset_ptr);
3038 high_pc = low_pc + length;
3042 // Not supported entry type
3046 assert(false && "Not supported location list type");
3050 static bool print_dwarf_exp_op(Stream &s, const DataExtractor &data,
3051 lldb::offset_t *offset_ptr, int address_size,
3052 int dwarf_ref_size) {
3053 uint8_t opcode = data.GetU8(offset_ptr);
3054 DRC_class opcode_class;
3060 opcode_class = DW_OP_value_to_class(opcode) & (~DRC_DWARFv3);
3062 s.Printf("%s ", DW_OP_value_to_name(opcode));
3064 /* Does this take zero parameters? If so we can shortcut this function. */
3065 if (opcode_class == DRC_ZEROOPERANDS)
3068 if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_bregx) {
3069 uint = data.GetULEB128(offset_ptr);
3070 sint = data.GetSLEB128(offset_ptr);
3071 s.Printf("%" PRIu64 " %" PRIi64, uint, sint);
3074 if (opcode_class != DRC_ONEOPERAND) {
3075 s.Printf("UNKNOWN OP %u", opcode);
3081 size = address_size;
3151 case DW_OP_deref_size:
3152 case DW_OP_xderef_size:
3165 case DW_OP_call_ref:
3166 size = dwarf_ref_size;
3169 case DW_OP_plus_uconst:
3171 case DW_OP_GNU_addr_index:
3172 case DW_OP_GNU_const_index:
3176 s.Printf("UNKNOWN ONE-OPERAND OPCODE, #%u", opcode);
3182 sint = (int8_t)data.GetU8(offset_ptr);
3183 s.Printf("%+" PRIi64, sint);
3186 sint = (int16_t)data.GetU16(offset_ptr);
3187 s.Printf("%+" PRIi64, sint);
3190 sint = (int32_t)data.GetU32(offset_ptr);
3191 s.Printf("%+" PRIi64, sint);
3194 sint = (int64_t)data.GetU64(offset_ptr);
3195 s.Printf("%+" PRIi64, sint);
3198 sint = data.GetSLEB128(offset_ptr);
3199 s.Printf("%+" PRIi64, sint);
3202 uint = data.GetU8(offset_ptr);
3203 s.Printf("0x%2.2" PRIx64, uint);
3206 uint = data.GetU16(offset_ptr);
3207 s.Printf("0x%4.4" PRIx64, uint);
3210 uint = data.GetU32(offset_ptr);
3211 s.Printf("0x%8.8" PRIx64, uint);
3214 uint = data.GetU64(offset_ptr);
3215 s.Printf("0x%16.16" PRIx64, uint);
3218 uint = data.GetULEB128(offset_ptr);
3219 s.Printf("0x%" PRIx64, uint);
3226 bool DWARFExpression::PrintDWARFExpression(Stream &s, const DataExtractor &data,
3227 int address_size, int dwarf_ref_size,
3228 bool location_expression) {
3230 lldb::offset_t offset = 0;
3231 while (data.ValidOffset(offset)) {
3232 if (location_expression && op_count > 0)
3236 if (!print_dwarf_exp_op(s, data, &offset, address_size, dwarf_ref_size))
3244 void DWARFExpression::PrintDWARFLocationList(
3245 Stream &s, const DWARFCompileUnit *cu, const DataExtractor &debug_loc_data,
3246 lldb::offset_t offset) {
3247 uint64_t start_addr, end_addr;
3248 uint32_t addr_size = DWARFCompileUnit::GetAddressByteSize(cu);
3249 s.SetAddressByteSize(DWARFCompileUnit::GetAddressByteSize(cu));
3250 dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
3251 while (debug_loc_data.ValidOffset(offset)) {
3252 start_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3253 end_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3255 if (start_addr == 0 && end_addr == 0)
3258 s.PutCString("\n ");
3261 s.AddressRange(start_addr + base_addr, end_addr + base_addr,
3262 cu->GetAddressByteSize(), NULL, ": ");
3263 uint32_t loc_length = debug_loc_data.GetU16(&offset);
3265 DataExtractor locationData(debug_loc_data, offset, loc_length);
3266 PrintDWARFExpression(s, locationData, addr_size, 4, false);
3267 offset += loc_length;
3271 bool DWARFExpression::GetOpAndEndOffsets(StackFrame &frame,
3272 lldb::offset_t &op_offset,
3273 lldb::offset_t &end_offset) {
3274 SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction);
3279 addr_t loclist_base_file_addr =
3280 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
3281 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS) {
3285 addr_t pc_file_addr = frame.GetFrameCodeAddress().GetFileAddress();
3286 lldb::offset_t opcodes_offset, opcodes_length;
3287 if (!GetLocation(loclist_base_file_addr, pc_file_addr, opcodes_offset,
3292 if (opcodes_length == 0) {
3296 op_offset = opcodes_offset;
3297 end_offset = opcodes_offset + opcodes_length;
3301 bool DWARFExpression::MatchesOperand(StackFrame &frame,
3302 const Instruction::Operand &operand) {
3303 using namespace OperandMatchers;
3305 lldb::offset_t op_offset;
3306 lldb::offset_t end_offset;
3307 if (!GetOpAndEndOffsets(frame, op_offset, end_offset)) {
3311 if (!m_data.ValidOffset(op_offset) || op_offset >= end_offset) {
3315 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
3320 DataExtractor opcodes = m_data;
3321 uint8_t opcode = opcodes.GetU8(&op_offset);
3323 if (opcode == DW_OP_fbreg) {
3324 int64_t offset = opcodes.GetSLEB128(&op_offset);
3326 DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr);
3331 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
3332 return fb_expr->MatchesOperand(frame, child);
3336 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3337 recurse)(operand)) {
3341 return MatchUnaryOp(
3342 MatchOpType(Instruction::Operand::Type::Dereference),
3343 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3344 MatchImmOp(offset), recurse))(operand);
3347 bool dereference = false;
3348 const RegisterInfo *reg = nullptr;
3351 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
3352 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
3353 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
3354 offset = opcodes.GetSLEB128(&op_offset);
3355 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
3356 } else if (opcode == DW_OP_regx) {
3357 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3358 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3359 } else if (opcode == DW_OP_bregx) {
3360 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3361 offset = opcodes.GetSLEB128(&op_offset);
3362 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3373 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3374 MatchRegOp(*reg))(operand)) {
3378 return MatchUnaryOp(
3379 MatchOpType(Instruction::Operand::Type::Dereference),
3380 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3382 MatchImmOp(offset)))(operand);
3384 return MatchRegOp(*reg)(operand);