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"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Value.h"
18 #include "lldb/Core/dwarf.h"
19 #include "lldb/Utility/DataEncoder.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/RegisterValue.h"
22 #include "lldb/Utility/Scalar.h"
23 #include "lldb/Utility/StreamString.h"
24 #include "lldb/Utility/VMRange.h"
26 #include "lldb/Host/Host.h"
27 #include "lldb/Utility/Endian.h"
29 #include "lldb/Symbol/Function.h"
31 #include "lldb/Target/ABI.h"
32 #include "lldb/Target/ExecutionContext.h"
33 #include "lldb/Target/Process.h"
34 #include "lldb/Target/RegisterContext.h"
35 #include "lldb/Target/StackFrame.h"
36 #include "lldb/Target/StackID.h"
37 #include "lldb/Target/Thread.h"
39 #include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
42 using namespace lldb_private;
45 ReadAddressFromDebugAddrSection(const DWARFUnit *dwarf_cu,
47 uint32_t index_size = dwarf_cu->GetAddressByteSize();
48 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
49 lldb::offset_t offset = addr_base + index * index_size;
50 return dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
54 //----------------------------------------------------------------------
55 // DWARFExpression constructor
56 //----------------------------------------------------------------------
57 DWARFExpression::DWARFExpression(DWARFUnit *dwarf_cu)
58 : m_module_wp(), m_data(), m_dwarf_cu(dwarf_cu),
59 m_reg_kind(eRegisterKindDWARF), m_loclist_slide(LLDB_INVALID_ADDRESS) {}
61 DWARFExpression::DWARFExpression(const DWARFExpression &rhs)
62 : m_module_wp(rhs.m_module_wp), m_data(rhs.m_data),
63 m_dwarf_cu(rhs.m_dwarf_cu), m_reg_kind(rhs.m_reg_kind),
64 m_loclist_slide(rhs.m_loclist_slide) {}
66 DWARFExpression::DWARFExpression(lldb::ModuleSP module_sp,
67 const DataExtractor &data,
69 lldb::offset_t data_offset,
70 lldb::offset_t data_length)
71 : m_module_wp(), m_data(data, data_offset, data_length),
72 m_dwarf_cu(dwarf_cu), m_reg_kind(eRegisterKindDWARF),
73 m_loclist_slide(LLDB_INVALID_ADDRESS) {
75 m_module_wp = module_sp;
78 //----------------------------------------------------------------------
80 //----------------------------------------------------------------------
81 DWARFExpression::~DWARFExpression() {}
83 bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
85 void DWARFExpression::SetOpcodeData(const DataExtractor &data) {
89 void DWARFExpression::CopyOpcodeData(lldb::ModuleSP module_sp,
90 const DataExtractor &data,
91 lldb::offset_t data_offset,
92 lldb::offset_t data_length) {
93 const uint8_t *bytes = data.PeekData(data_offset, data_length);
95 m_module_wp = module_sp;
96 m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length)));
97 m_data.SetByteOrder(data.GetByteOrder());
98 m_data.SetAddressByteSize(data.GetAddressByteSize());
102 void DWARFExpression::CopyOpcodeData(const void *data,
103 lldb::offset_t data_length,
104 ByteOrder byte_order,
105 uint8_t addr_byte_size) {
106 if (data && data_length) {
107 m_data.SetData(DataBufferSP(new DataBufferHeap(data, data_length)));
108 m_data.SetByteOrder(byte_order);
109 m_data.SetAddressByteSize(addr_byte_size);
113 void DWARFExpression::CopyOpcodeData(uint64_t const_value,
114 lldb::offset_t const_value_byte_size,
115 uint8_t addr_byte_size) {
116 if (const_value_byte_size) {
118 DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
119 m_data.SetByteOrder(endian::InlHostByteOrder());
120 m_data.SetAddressByteSize(addr_byte_size);
124 void DWARFExpression::SetOpcodeData(lldb::ModuleSP module_sp,
125 const DataExtractor &data,
126 lldb::offset_t data_offset,
127 lldb::offset_t data_length) {
128 m_module_wp = module_sp;
129 m_data.SetData(data, data_offset, data_length);
132 void DWARFExpression::DumpLocation(Stream *s, lldb::offset_t offset,
133 lldb::offset_t length,
134 lldb::DescriptionLevel level,
136 if (!m_data.ValidOffsetForDataOfSize(offset, length))
138 const lldb::offset_t start_offset = offset;
139 const lldb::offset_t end_offset = offset + length;
140 while (m_data.ValidOffset(offset) && offset < end_offset) {
141 const lldb::offset_t op_offset = offset;
142 const uint8_t op = m_data.GetU8(&offset);
148 case lldb::eDescriptionLevelBrief:
149 if (op_offset > start_offset)
153 case lldb::eDescriptionLevelFull:
154 case lldb::eDescriptionLevelVerbose:
155 if (op_offset > start_offset)
158 if (level == lldb::eDescriptionLevelFull)
160 // Fall through for verbose and print offset and DW_OP prefix..
161 s->Printf("0x%8.8" PRIx64 ": %s", op_offset,
162 op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
168 *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") ";
169 break; // 0x03 1 address
174 s->Printf("DW_OP_const1u(0x%2.2x)", m_data.GetU8(&offset));
175 break; // 0x08 1 1-byte constant
177 s->Printf("DW_OP_const1s(0x%2.2x)", m_data.GetU8(&offset));
178 break; // 0x09 1 1-byte constant
180 s->Printf("DW_OP_const2u(0x%4.4x)", m_data.GetU16(&offset));
181 break; // 0x0a 1 2-byte constant
183 s->Printf("DW_OP_const2s(0x%4.4x)", m_data.GetU16(&offset));
184 break; // 0x0b 1 2-byte constant
186 s->Printf("DW_OP_const4u(0x%8.8x)", m_data.GetU32(&offset));
187 break; // 0x0c 1 4-byte constant
189 s->Printf("DW_OP_const4s(0x%8.8x)", m_data.GetU32(&offset));
190 break; // 0x0d 1 4-byte constant
192 s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ")", m_data.GetU64(&offset));
193 break; // 0x0e 1 8-byte constant
195 s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ")", m_data.GetU64(&offset));
196 break; // 0x0f 1 8-byte constant
198 s->Printf("DW_OP_constu(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
199 break; // 0x10 1 ULEB128 constant
201 s->Printf("DW_OP_consts(0x%" PRId64 ")", m_data.GetSLEB128(&offset));
202 break; // 0x11 1 SLEB128 constant
204 s->PutCString("DW_OP_dup");
207 s->PutCString("DW_OP_drop");
210 s->PutCString("DW_OP_over");
213 s->Printf("DW_OP_pick(0x%2.2x)", m_data.GetU8(&offset));
214 break; // 0x15 1 1-byte stack index
216 s->PutCString("DW_OP_swap");
219 s->PutCString("DW_OP_rot");
222 s->PutCString("DW_OP_xderef");
225 s->PutCString("DW_OP_abs");
228 s->PutCString("DW_OP_and");
231 s->PutCString("DW_OP_div");
234 s->PutCString("DW_OP_minus");
237 s->PutCString("DW_OP_mod");
240 s->PutCString("DW_OP_mul");
243 s->PutCString("DW_OP_neg");
246 s->PutCString("DW_OP_not");
249 s->PutCString("DW_OP_or");
252 s->PutCString("DW_OP_plus");
254 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
255 s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ")",
256 m_data.GetULEB128(&offset));
260 s->PutCString("DW_OP_shl");
263 s->PutCString("DW_OP_shr");
266 s->PutCString("DW_OP_shra");
269 s->PutCString("DW_OP_xor");
272 s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset));
273 break; // 0x2f 1 signed 2-byte constant
275 s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset));
276 break; // 0x28 1 signed 2-byte constant
278 s->PutCString("DW_OP_eq");
281 s->PutCString("DW_OP_ge");
284 s->PutCString("DW_OP_gt");
287 s->PutCString("DW_OP_le");
290 s->PutCString("DW_OP_lt");
293 s->PutCString("DW_OP_ne");
296 case DW_OP_lit0: // 0x30
297 case DW_OP_lit1: // 0x31
298 case DW_OP_lit2: // 0x32
299 case DW_OP_lit3: // 0x33
300 case DW_OP_lit4: // 0x34
301 case DW_OP_lit5: // 0x35
302 case DW_OP_lit6: // 0x36
303 case DW_OP_lit7: // 0x37
304 case DW_OP_lit8: // 0x38
305 case DW_OP_lit9: // 0x39
306 case DW_OP_lit10: // 0x3A
307 case DW_OP_lit11: // 0x3B
308 case DW_OP_lit12: // 0x3C
309 case DW_OP_lit13: // 0x3D
310 case DW_OP_lit14: // 0x3E
311 case DW_OP_lit15: // 0x3F
312 case DW_OP_lit16: // 0x40
313 case DW_OP_lit17: // 0x41
314 case DW_OP_lit18: // 0x42
315 case DW_OP_lit19: // 0x43
316 case DW_OP_lit20: // 0x44
317 case DW_OP_lit21: // 0x45
318 case DW_OP_lit22: // 0x46
319 case DW_OP_lit23: // 0x47
320 case DW_OP_lit24: // 0x48
321 case DW_OP_lit25: // 0x49
322 case DW_OP_lit26: // 0x4A
323 case DW_OP_lit27: // 0x4B
324 case DW_OP_lit28: // 0x4C
325 case DW_OP_lit29: // 0x4D
326 case DW_OP_lit30: // 0x4E
328 s->Printf("DW_OP_lit%i", op - DW_OP_lit0);
331 case DW_OP_reg0: // 0x50
332 case DW_OP_reg1: // 0x51
333 case DW_OP_reg2: // 0x52
334 case DW_OP_reg3: // 0x53
335 case DW_OP_reg4: // 0x54
336 case DW_OP_reg5: // 0x55
337 case DW_OP_reg6: // 0x56
338 case DW_OP_reg7: // 0x57
339 case DW_OP_reg8: // 0x58
340 case DW_OP_reg9: // 0x59
341 case DW_OP_reg10: // 0x5A
342 case DW_OP_reg11: // 0x5B
343 case DW_OP_reg12: // 0x5C
344 case DW_OP_reg13: // 0x5D
345 case DW_OP_reg14: // 0x5E
346 case DW_OP_reg15: // 0x5F
347 case DW_OP_reg16: // 0x60
348 case DW_OP_reg17: // 0x61
349 case DW_OP_reg18: // 0x62
350 case DW_OP_reg19: // 0x63
351 case DW_OP_reg20: // 0x64
352 case DW_OP_reg21: // 0x65
353 case DW_OP_reg22: // 0x66
354 case DW_OP_reg23: // 0x67
355 case DW_OP_reg24: // 0x68
356 case DW_OP_reg25: // 0x69
357 case DW_OP_reg26: // 0x6A
358 case DW_OP_reg27: // 0x6B
359 case DW_OP_reg28: // 0x6C
360 case DW_OP_reg29: // 0x6D
361 case DW_OP_reg30: // 0x6E
362 case DW_OP_reg31: // 0x6F
364 uint32_t reg_num = op - DW_OP_reg0;
366 RegisterInfo reg_info;
367 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
369 s->PutCString(reg_info.name);
371 } else if (reg_info.alt_name) {
372 s->PutCString(reg_info.alt_name);
377 s->Printf("DW_OP_reg%u", reg_num);
413 uint32_t reg_num = op - DW_OP_breg0;
414 int64_t reg_offset = m_data.GetSLEB128(&offset);
416 RegisterInfo reg_info;
417 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
419 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
421 } else if (reg_info.alt_name) {
422 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
427 s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset);
430 case DW_OP_regx: // 0x90 1 ULEB128 register
432 uint32_t reg_num = m_data.GetULEB128(&offset);
434 RegisterInfo reg_info;
435 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
437 s->PutCString(reg_info.name);
439 } else if (reg_info.alt_name) {
440 s->PutCString(reg_info.alt_name);
445 s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num);
448 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
449 s->Printf("DW_OP_fbreg(%" PRIi64 ")", m_data.GetSLEB128(&offset));
451 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
453 uint32_t reg_num = m_data.GetULEB128(&offset);
454 int64_t reg_offset = m_data.GetSLEB128(&offset);
456 RegisterInfo reg_info;
457 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
459 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
461 } else if (reg_info.alt_name) {
462 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
467 s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num,
470 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
471 s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
473 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
474 s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
476 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
477 s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
480 s->PutCString("DW_OP_nop");
482 case DW_OP_push_object_address:
483 s->PutCString("DW_OP_push_object_address");
484 break; // 0x97 DWARF3
485 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
486 s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
488 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
489 s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
491 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
492 s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset));
494 // case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break;
496 // case DW_OP_bit_piece: // 0x9d DWARF3 2
497 // s->Printf("DW_OP_bit_piece(0x%x, 0x%x)",
498 // m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
500 // case DW_OP_lo_user: s->PutCString("DW_OP_lo_user"); break;
502 // case DW_OP_hi_user: s->PutCString("DW_OP_hi_user"); break;
504 // case DW_OP_APPLE_extern:
505 // s->Printf("DW_OP_APPLE_extern(%" PRIu64 ")",
506 // m_data.GetULEB128(&offset));
508 // case DW_OP_APPLE_array_ref:
509 // s->PutCString("DW_OP_APPLE_array_ref");
511 case DW_OP_form_tls_address:
512 s->PutCString("DW_OP_form_tls_address"); // 0x9b
514 case DW_OP_GNU_addr_index: // 0xfb
515 s->Printf("DW_OP_GNU_addr_index(0x%" PRIx64 ")",
516 m_data.GetULEB128(&offset));
518 case DW_OP_GNU_const_index: // 0xfc
519 s->Printf("DW_OP_GNU_const_index(0x%" PRIx64 ")",
520 m_data.GetULEB128(&offset));
522 case DW_OP_GNU_push_tls_address:
523 s->PutCString("DW_OP_GNU_push_tls_address"); // 0xe0
525 case DW_OP_APPLE_uninit:
526 s->PutCString("DW_OP_APPLE_uninit"); // 0xF0
528 // case DW_OP_APPLE_assign: // 0xF1 - pops value off and
529 // assigns it to second item on stack (2nd item must have
530 // assignable context)
531 // s->PutCString("DW_OP_APPLE_assign");
533 // case DW_OP_APPLE_address_of: // 0xF2 - gets the address of
534 // the top stack item (top item must be a variable, or have
535 // value_type that is an address already)
536 // s->PutCString("DW_OP_APPLE_address_of");
538 // case DW_OP_APPLE_value_of: // 0xF3 - pops the value off the
539 // stack and pushes the value of that object (top item must be a
540 // variable, or expression local)
541 // s->PutCString("DW_OP_APPLE_value_of");
543 // case DW_OP_APPLE_deref_type: // 0xF4 - gets the address of
544 // the top stack item (top item must be a variable, or a clang
546 // s->PutCString("DW_OP_APPLE_deref_type");
548 // case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression
550 // s->Printf("DW_OP_APPLE_expr_local(%" PRIu64 ")",
551 // m_data.GetULEB128(&offset));
553 // case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size,
554 // followed by constant float data
556 // uint8_t float_length = m_data.GetU8(&offset);
557 // s->Printf("DW_OP_APPLE_constf(<%u> ", float_length);
558 // m_data.Dump(s, offset, eFormatHex, float_length, 1,
559 // UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
561 // // Consume the float data
562 // m_data.GetData(&offset, float_length);
565 // case DW_OP_APPLE_scalar_cast:
566 // s->Printf("DW_OP_APPLE_scalar_cast(%s)",
567 // Scalar::GetValueTypeAsCString
568 // ((Scalar::Type)m_data.GetU8(&offset)));
570 // case DW_OP_APPLE_clang_cast:
572 // clang::Type *clang_type = (clang::Type
573 // *)m_data.GetMaxU64(&offset, sizeof(void*));
574 // s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type);
577 // case DW_OP_APPLE_clear:
578 // s->PutCString("DW_OP_APPLE_clear");
580 // case DW_OP_APPLE_error: // 0xFF - Stops expression
581 // evaluation and returns an error (no args)
582 // s->PutCString("DW_OP_APPLE_error");
588 void DWARFExpression::SetLocationListSlide(addr_t slide) {
589 m_loclist_slide = slide;
592 int DWARFExpression::GetRegisterKind() { return m_reg_kind; }
594 void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
595 m_reg_kind = reg_kind;
598 bool DWARFExpression::IsLocationList() const {
599 return m_loclist_slide != LLDB_INVALID_ADDRESS;
602 void DWARFExpression::GetDescription(Stream *s, lldb::DescriptionLevel level,
603 addr_t location_list_base_addr,
605 if (IsLocationList()) {
606 // We have a location list
607 lldb::offset_t offset = 0;
609 addr_t curr_base_addr = location_list_base_addr;
610 while (m_data.ValidOffset(offset)) {
611 addr_t begin_addr_offset = LLDB_INVALID_ADDRESS;
612 addr_t end_addr_offset = LLDB_INVALID_ADDRESS;
613 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
614 begin_addr_offset, end_addr_offset))
617 if (begin_addr_offset == 0 && end_addr_offset == 0)
620 if (begin_addr_offset < end_addr_offset) {
623 VMRange addr_range(curr_base_addr + begin_addr_offset,
624 curr_base_addr + end_addr_offset);
625 addr_range.Dump(s, 0, 8);
627 lldb::offset_t location_length = m_data.GetU16(&offset);
628 DumpLocation(s, offset, location_length, level, abi);
630 offset += location_length;
632 if ((m_data.GetAddressByteSize() == 4 &&
633 (begin_addr_offset == UINT32_MAX)) ||
634 (m_data.GetAddressByteSize() == 8 &&
635 (begin_addr_offset == UINT64_MAX))) {
636 curr_base_addr = end_addr_offset + location_list_base_addr;
637 // We have a new base address
640 *s << "base_addr = " << end_addr_offset;
647 // We have a normal location that contains DW_OP location opcodes
648 DumpLocation(s, 0, m_data.GetByteSize(), level, abi);
652 static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
653 lldb::RegisterKind reg_kind,
654 uint32_t reg_num, Status *error_ptr,
656 if (reg_ctx == NULL) {
658 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
660 uint32_t native_reg =
661 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
662 if (native_reg == LLDB_INVALID_REGNUM) {
664 error_ptr->SetErrorStringWithFormat("Unable to convert register "
665 "kind=%u reg_num=%u to a native "
666 "register number.\n",
669 const RegisterInfo *reg_info =
670 reg_ctx->GetRegisterInfoAtIndex(native_reg);
671 RegisterValue reg_value;
672 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
673 if (reg_value.GetScalarValue(value.GetScalar())) {
674 value.SetValueType(Value::eValueTypeScalar);
675 value.SetContext(Value::eContextTypeRegisterInfo,
676 const_cast<RegisterInfo *>(reg_info));
681 // If we get this error, then we need to implement a value buffer in
682 // the dwarf expression evaluation function...
684 error_ptr->SetErrorStringWithFormat(
685 "register %s can't be converted to a scalar value",
690 error_ptr->SetErrorStringWithFormat("register %s is not available",
699 // DWARFExpression::LocationListContainsLoadAddress (Process* process, const
700 // Address &addr) const
702 // return LocationListContainsLoadAddress(process,
703 // addr.GetLoadAddress(process));
707 // DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t
710 // if (load_addr == LLDB_INVALID_ADDRESS)
713 // if (IsLocationList())
715 // lldb::offset_t offset = 0;
717 // addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
719 // if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
722 // while (m_data.ValidOffset(offset))
724 // // We need to figure out what the value is for the location.
725 // addr_t lo_pc = m_data.GetAddress(&offset);
726 // addr_t hi_pc = m_data.GetAddress(&offset);
727 // if (lo_pc == 0 && hi_pc == 0)
731 // lo_pc += loc_list_base_addr;
732 // hi_pc += loc_list_base_addr;
734 // if (lo_pc <= load_addr && load_addr < hi_pc)
737 // offset += m_data.GetU16(&offset);
744 static offset_t GetOpcodeDataSize(const DataExtractor &data,
745 const lldb::offset_t data_offset,
747 lldb::offset_t offset = data_offset;
750 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
751 return data.GetAddressByteSize();
753 // Opcodes with no arguments
754 case DW_OP_deref: // 0x06
755 case DW_OP_dup: // 0x12
756 case DW_OP_drop: // 0x13
757 case DW_OP_over: // 0x14
758 case DW_OP_swap: // 0x16
759 case DW_OP_rot: // 0x17
760 case DW_OP_xderef: // 0x18
761 case DW_OP_abs: // 0x19
762 case DW_OP_and: // 0x1a
763 case DW_OP_div: // 0x1b
764 case DW_OP_minus: // 0x1c
765 case DW_OP_mod: // 0x1d
766 case DW_OP_mul: // 0x1e
767 case DW_OP_neg: // 0x1f
768 case DW_OP_not: // 0x20
769 case DW_OP_or: // 0x21
770 case DW_OP_plus: // 0x22
771 case DW_OP_shl: // 0x24
772 case DW_OP_shr: // 0x25
773 case DW_OP_shra: // 0x26
774 case DW_OP_xor: // 0x27
775 case DW_OP_eq: // 0x29
776 case DW_OP_ge: // 0x2a
777 case DW_OP_gt: // 0x2b
778 case DW_OP_le: // 0x2c
779 case DW_OP_lt: // 0x2d
780 case DW_OP_ne: // 0x2e
781 case DW_OP_lit0: // 0x30
782 case DW_OP_lit1: // 0x31
783 case DW_OP_lit2: // 0x32
784 case DW_OP_lit3: // 0x33
785 case DW_OP_lit4: // 0x34
786 case DW_OP_lit5: // 0x35
787 case DW_OP_lit6: // 0x36
788 case DW_OP_lit7: // 0x37
789 case DW_OP_lit8: // 0x38
790 case DW_OP_lit9: // 0x39
791 case DW_OP_lit10: // 0x3A
792 case DW_OP_lit11: // 0x3B
793 case DW_OP_lit12: // 0x3C
794 case DW_OP_lit13: // 0x3D
795 case DW_OP_lit14: // 0x3E
796 case DW_OP_lit15: // 0x3F
797 case DW_OP_lit16: // 0x40
798 case DW_OP_lit17: // 0x41
799 case DW_OP_lit18: // 0x42
800 case DW_OP_lit19: // 0x43
801 case DW_OP_lit20: // 0x44
802 case DW_OP_lit21: // 0x45
803 case DW_OP_lit22: // 0x46
804 case DW_OP_lit23: // 0x47
805 case DW_OP_lit24: // 0x48
806 case DW_OP_lit25: // 0x49
807 case DW_OP_lit26: // 0x4A
808 case DW_OP_lit27: // 0x4B
809 case DW_OP_lit28: // 0x4C
810 case DW_OP_lit29: // 0x4D
811 case DW_OP_lit30: // 0x4E
812 case DW_OP_lit31: // 0x4f
813 case DW_OP_reg0: // 0x50
814 case DW_OP_reg1: // 0x51
815 case DW_OP_reg2: // 0x52
816 case DW_OP_reg3: // 0x53
817 case DW_OP_reg4: // 0x54
818 case DW_OP_reg5: // 0x55
819 case DW_OP_reg6: // 0x56
820 case DW_OP_reg7: // 0x57
821 case DW_OP_reg8: // 0x58
822 case DW_OP_reg9: // 0x59
823 case DW_OP_reg10: // 0x5A
824 case DW_OP_reg11: // 0x5B
825 case DW_OP_reg12: // 0x5C
826 case DW_OP_reg13: // 0x5D
827 case DW_OP_reg14: // 0x5E
828 case DW_OP_reg15: // 0x5F
829 case DW_OP_reg16: // 0x60
830 case DW_OP_reg17: // 0x61
831 case DW_OP_reg18: // 0x62
832 case DW_OP_reg19: // 0x63
833 case DW_OP_reg20: // 0x64
834 case DW_OP_reg21: // 0x65
835 case DW_OP_reg22: // 0x66
836 case DW_OP_reg23: // 0x67
837 case DW_OP_reg24: // 0x68
838 case DW_OP_reg25: // 0x69
839 case DW_OP_reg26: // 0x6A
840 case DW_OP_reg27: // 0x6B
841 case DW_OP_reg28: // 0x6C
842 case DW_OP_reg29: // 0x6D
843 case DW_OP_reg30: // 0x6E
844 case DW_OP_reg31: // 0x6F
845 case DW_OP_nop: // 0x96
846 case DW_OP_push_object_address: // 0x97 DWARF3
847 case DW_OP_form_tls_address: // 0x9b DWARF3
848 case DW_OP_call_frame_cfa: // 0x9c DWARF3
849 case DW_OP_stack_value: // 0x9f DWARF4
850 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
853 // Opcodes with a single 1 byte arguments
854 case DW_OP_const1u: // 0x08 1 1-byte constant
855 case DW_OP_const1s: // 0x09 1 1-byte constant
856 case DW_OP_pick: // 0x15 1 1-byte stack index
857 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
858 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
861 // Opcodes with a single 2 byte arguments
862 case DW_OP_const2u: // 0x0a 1 2-byte constant
863 case DW_OP_const2s: // 0x0b 1 2-byte constant
864 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
865 case DW_OP_bra: // 0x28 1 signed 2-byte constant
866 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
869 // Opcodes with a single 4 byte arguments
870 case DW_OP_const4u: // 0x0c 1 4-byte constant
871 case DW_OP_const4s: // 0x0d 1 4-byte constant
872 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
875 // Opcodes with a single 8 byte arguments
876 case DW_OP_const8u: // 0x0e 1 8-byte constant
877 case DW_OP_const8s: // 0x0f 1 8-byte constant
880 // All opcodes that have a single ULEB (signed or unsigned) argument
881 case DW_OP_constu: // 0x10 1 ULEB128 constant
882 case DW_OP_consts: // 0x11 1 SLEB128 constant
883 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
884 case DW_OP_breg0: // 0x70 1 ULEB128 register
885 case DW_OP_breg1: // 0x71 1 ULEB128 register
886 case DW_OP_breg2: // 0x72 1 ULEB128 register
887 case DW_OP_breg3: // 0x73 1 ULEB128 register
888 case DW_OP_breg4: // 0x74 1 ULEB128 register
889 case DW_OP_breg5: // 0x75 1 ULEB128 register
890 case DW_OP_breg6: // 0x76 1 ULEB128 register
891 case DW_OP_breg7: // 0x77 1 ULEB128 register
892 case DW_OP_breg8: // 0x78 1 ULEB128 register
893 case DW_OP_breg9: // 0x79 1 ULEB128 register
894 case DW_OP_breg10: // 0x7a 1 ULEB128 register
895 case DW_OP_breg11: // 0x7b 1 ULEB128 register
896 case DW_OP_breg12: // 0x7c 1 ULEB128 register
897 case DW_OP_breg13: // 0x7d 1 ULEB128 register
898 case DW_OP_breg14: // 0x7e 1 ULEB128 register
899 case DW_OP_breg15: // 0x7f 1 ULEB128 register
900 case DW_OP_breg16: // 0x80 1 ULEB128 register
901 case DW_OP_breg17: // 0x81 1 ULEB128 register
902 case DW_OP_breg18: // 0x82 1 ULEB128 register
903 case DW_OP_breg19: // 0x83 1 ULEB128 register
904 case DW_OP_breg20: // 0x84 1 ULEB128 register
905 case DW_OP_breg21: // 0x85 1 ULEB128 register
906 case DW_OP_breg22: // 0x86 1 ULEB128 register
907 case DW_OP_breg23: // 0x87 1 ULEB128 register
908 case DW_OP_breg24: // 0x88 1 ULEB128 register
909 case DW_OP_breg25: // 0x89 1 ULEB128 register
910 case DW_OP_breg26: // 0x8a 1 ULEB128 register
911 case DW_OP_breg27: // 0x8b 1 ULEB128 register
912 case DW_OP_breg28: // 0x8c 1 ULEB128 register
913 case DW_OP_breg29: // 0x8d 1 ULEB128 register
914 case DW_OP_breg30: // 0x8e 1 ULEB128 register
915 case DW_OP_breg31: // 0x8f 1 ULEB128 register
916 case DW_OP_regx: // 0x90 1 ULEB128 register
917 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
918 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
919 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
920 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
921 data.Skip_LEB128(&offset);
922 return offset - data_offset;
924 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
925 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
926 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
927 data.Skip_LEB128(&offset);
928 data.Skip_LEB128(&offset);
929 return offset - data_offset;
931 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
934 uint64_t block_len = data.Skip_LEB128(&offset);
936 return offset - data_offset;
942 return LLDB_INVALID_OFFSET;
945 lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(uint32_t op_addr_idx,
948 if (IsLocationList())
949 return LLDB_INVALID_ADDRESS;
950 lldb::offset_t offset = 0;
951 uint32_t curr_op_addr_idx = 0;
952 while (m_data.ValidOffset(offset)) {
953 const uint8_t op = m_data.GetU8(&offset);
955 if (op == DW_OP_addr) {
956 const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
957 if (curr_op_addr_idx == op_addr_idx)
961 } else if (op == DW_OP_GNU_addr_index) {
962 uint64_t index = m_data.GetULEB128(&offset);
963 if (curr_op_addr_idx == op_addr_idx) {
969 return ReadAddressFromDebugAddrSection(m_dwarf_cu, index);
973 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
974 if (op_arg_size == LLDB_INVALID_OFFSET) {
978 offset += op_arg_size;
981 return LLDB_INVALID_ADDRESS;
984 bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) {
985 if (IsLocationList())
987 lldb::offset_t offset = 0;
988 while (m_data.ValidOffset(offset)) {
989 const uint8_t op = m_data.GetU8(&offset);
991 if (op == DW_OP_addr) {
992 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
993 // We have to make a copy of the data as we don't know if this data is
994 // from a read only memory mapped buffer, so we duplicate all of the data
995 // first, then modify it, and if all goes well, we then replace the data
996 // for this expression
998 // So first we copy the data into a heap buffer
999 std::unique_ptr<DataBufferHeap> head_data_ap(
1000 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1002 // Make en encoder so we can write the address into the buffer using the
1003 // correct byte order (endianness)
1004 DataEncoder encoder(head_data_ap->GetBytes(), head_data_ap->GetByteSize(),
1005 m_data.GetByteOrder(), addr_byte_size);
1007 // Replace the address in the new buffer
1008 if (encoder.PutMaxU64(offset, addr_byte_size, file_addr) == UINT32_MAX)
1011 // All went well, so now we can reset the data using a shared pointer to
1012 // the heap data so "m_data" will now correctly manage the heap data.
1013 m_data.SetData(DataBufferSP(head_data_ap.release()));
1016 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1017 if (op_arg_size == LLDB_INVALID_OFFSET)
1019 offset += op_arg_size;
1025 bool DWARFExpression::ContainsThreadLocalStorage() const {
1026 // We are assuming for now that any thread local variable will not have a
1027 // location list. This has been true for all thread local variables we have
1028 // seen so far produced by any compiler.
1029 if (IsLocationList())
1031 lldb::offset_t offset = 0;
1032 while (m_data.ValidOffset(offset)) {
1033 const uint8_t op = m_data.GetU8(&offset);
1035 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
1037 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1038 if (op_arg_size == LLDB_INVALID_OFFSET)
1041 offset += op_arg_size;
1045 bool DWARFExpression::LinkThreadLocalStorage(
1046 lldb::ModuleSP new_module_sp,
1047 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
1048 &link_address_callback) {
1049 // We are assuming for now that any thread local variable will not have a
1050 // location list. This has been true for all thread local variables we have
1051 // seen so far produced by any compiler.
1052 if (IsLocationList())
1055 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1056 // We have to make a copy of the data as we don't know if this data is from a
1057 // read only memory mapped buffer, so we duplicate all of the data first,
1058 // then modify it, and if all goes well, we then replace the data for this
1061 // So first we copy the data into a heap buffer
1062 std::shared_ptr<DataBufferHeap> heap_data_sp(
1063 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1065 // Make en encoder so we can write the address into the buffer using the
1066 // correct byte order (endianness)
1067 DataEncoder encoder(heap_data_sp->GetBytes(), heap_data_sp->GetByteSize(),
1068 m_data.GetByteOrder(), addr_byte_size);
1070 lldb::offset_t offset = 0;
1071 lldb::offset_t const_offset = 0;
1072 lldb::addr_t const_value = 0;
1073 size_t const_byte_size = 0;
1074 while (m_data.ValidOffset(offset)) {
1075 const uint8_t op = m_data.GetU8(&offset);
1077 bool decoded_data = false;
1080 // Remember the const offset in case we later have a
1081 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
1082 const_offset = offset;
1083 const_value = m_data.GetU32(&offset);
1084 decoded_data = true;
1085 const_byte_size = 4;
1089 // Remember the const offset in case we later have a
1090 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
1091 const_offset = offset;
1092 const_value = m_data.GetU64(&offset);
1093 decoded_data = true;
1094 const_byte_size = 8;
1097 case DW_OP_form_tls_address:
1098 case DW_OP_GNU_push_tls_address:
1099 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
1100 // by a file address on the stack. We assume that DW_OP_const4u or
1101 // DW_OP_const8u is used for these values, and we check that the last
1102 // opcode we got before either of these was DW_OP_const4u or
1103 // DW_OP_const8u. If so, then we can link the value accodingly. For
1104 // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
1105 // address of a structure that contains a function pointer, the pthread
1106 // key and the offset into the data pointed to by the pthread key. So we
1107 // must link this address and also set the module of this expression to
1108 // the new_module_sp so we can resolve the file address correctly
1109 if (const_byte_size > 0) {
1110 lldb::addr_t linked_file_addr = link_address_callback(const_value);
1111 if (linked_file_addr == LLDB_INVALID_ADDRESS)
1113 // Replace the address in the new buffer
1114 if (encoder.PutMaxU64(const_offset, const_byte_size,
1115 linked_file_addr) == UINT32_MAX)
1123 const_byte_size = 0;
1127 if (!decoded_data) {
1128 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1129 if (op_arg_size == LLDB_INVALID_OFFSET)
1132 offset += op_arg_size;
1136 // If we linked the TLS address correctly, update the module so that when the
1137 // expression is evaluated it can resolve the file address to a load address
1140 m_module_wp = new_module_sp;
1141 m_data.SetData(heap_data_sp);
1145 bool DWARFExpression::LocationListContainsAddress(
1146 lldb::addr_t loclist_base_addr, lldb::addr_t addr) const {
1147 if (addr == LLDB_INVALID_ADDRESS)
1150 if (IsLocationList()) {
1151 lldb::offset_t offset = 0;
1153 if (loclist_base_addr == LLDB_INVALID_ADDRESS)
1156 while (m_data.ValidOffset(offset)) {
1157 // We need to figure out what the value is for the location.
1158 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1159 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1160 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1164 if (lo_pc == 0 && hi_pc == 0)
1167 lo_pc += loclist_base_addr - m_loclist_slide;
1168 hi_pc += loclist_base_addr - m_loclist_slide;
1170 if (lo_pc <= addr && addr < hi_pc)
1173 offset += m_data.GetU16(&offset);
1179 bool DWARFExpression::GetLocation(addr_t base_addr, addr_t pc,
1180 lldb::offset_t &offset,
1181 lldb::offset_t &length) {
1183 if (!IsLocationList()) {
1184 length = m_data.GetByteSize();
1188 if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS) {
1189 addr_t curr_base_addr = base_addr;
1191 while (m_data.ValidOffset(offset)) {
1192 // We need to figure out what the value is for the location.
1193 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1194 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1195 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1199 if (lo_pc == 0 && hi_pc == 0)
1202 lo_pc += curr_base_addr - m_loclist_slide;
1203 hi_pc += curr_base_addr - m_loclist_slide;
1205 length = m_data.GetU16(&offset);
1207 if (length > 0 && lo_pc <= pc && pc < hi_pc)
1213 offset = LLDB_INVALID_OFFSET;
1218 bool DWARFExpression::DumpLocationForAddress(Stream *s,
1219 lldb::DescriptionLevel level,
1220 addr_t base_addr, addr_t address,
1222 lldb::offset_t offset = 0;
1223 lldb::offset_t length = 0;
1225 if (GetLocation(base_addr, address, offset, length)) {
1227 DumpLocation(s, offset, length, level, abi);
1234 bool DWARFExpression::Evaluate(ExecutionContextScope *exe_scope,
1235 lldb::addr_t loclist_base_load_addr,
1236 const Value *initial_value_ptr,
1237 const Value *object_address_ptr, Value &result,
1238 Status *error_ptr) const {
1239 ExecutionContext exe_ctx(exe_scope);
1240 return Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, initial_value_ptr,
1241 object_address_ptr, result, error_ptr);
1244 bool DWARFExpression::Evaluate(ExecutionContext *exe_ctx,
1245 RegisterContext *reg_ctx,
1246 lldb::addr_t loclist_base_load_addr,
1247 const Value *initial_value_ptr,
1248 const Value *object_address_ptr, Value &result,
1249 Status *error_ptr) const {
1250 ModuleSP module_sp = m_module_wp.lock();
1252 if (IsLocationList()) {
1253 lldb::offset_t offset = 0;
1255 StackFrame *frame = NULL;
1257 pc = reg_ctx->GetPC();
1259 frame = exe_ctx->GetFramePtr();
1262 RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
1265 pc = reg_ctx_sp->GetPC();
1268 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS) {
1269 if (pc == LLDB_INVALID_ADDRESS) {
1271 error_ptr->SetErrorString("Invalid PC in frame.");
1275 addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
1277 while (m_data.ValidOffset(offset)) {
1278 // We need to figure out what the value is for the location.
1279 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1280 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1281 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
1285 if (lo_pc == 0 && hi_pc == 0)
1288 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
1289 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
1291 uint16_t length = m_data.GetU16(&offset);
1293 if (length > 0 && lo_pc <= pc && pc < hi_pc) {
1294 return DWARFExpression::Evaluate(
1295 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, offset, length,
1296 m_reg_kind, initial_value_ptr, object_address_ptr, result,
1303 error_ptr->SetErrorString("variable not available");
1307 // Not a location list, just a single expression.
1308 return DWARFExpression::Evaluate(
1309 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, 0, m_data.GetByteSize(),
1310 m_reg_kind, initial_value_ptr, object_address_ptr, result, error_ptr);
1313 bool DWARFExpression::Evaluate(
1314 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
1315 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
1316 DWARFUnit *dwarf_cu, const lldb::offset_t opcodes_offset,
1317 const lldb::offset_t opcodes_length, const lldb::RegisterKind reg_kind,
1318 const Value *initial_value_ptr, const Value *object_address_ptr,
1319 Value &result, Status *error_ptr) {
1321 if (opcodes_length == 0) {
1323 error_ptr->SetErrorString(
1324 "no location, value may have been optimized out");
1327 std::vector<Value> stack;
1329 Process *process = NULL;
1330 StackFrame *frame = NULL;
1333 process = exe_ctx->GetProcessPtr();
1334 frame = exe_ctx->GetFramePtr();
1336 if (reg_ctx == NULL && frame)
1337 reg_ctx = frame->GetRegisterContext().get();
1339 if (initial_value_ptr)
1340 stack.push_back(*initial_value_ptr);
1342 lldb::offset_t offset = opcodes_offset;
1343 const lldb::offset_t end_offset = opcodes_offset + opcodes_length;
1347 /// Insertion point for evaluating multi-piece expression.
\13
1348 uint64_t op_piece_offset = 0;
1349 Value pieces; // Used for DW_OP_piece
1351 // Make sure all of the data is available in opcodes.
1352 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length)) {
1354 error_ptr->SetErrorString(
1355 "invalid offset and/or length for opcodes buffer.");
1358 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1360 while (opcodes.ValidOffset(offset) && offset < end_offset) {
1361 const lldb::offset_t op_offset = offset;
1362 const uint8_t op = opcodes.GetU8(&offset);
1364 if (log && log->GetVerbose()) {
1365 size_t count = stack.size();
1366 log->Printf("Stack before operation has %" PRIu64 " values:",
1368 for (size_t i = 0; i < count; ++i) {
1369 StreamString new_value;
1370 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1371 stack[i].Dump(&new_value);
1372 log->Printf(" %s", new_value.GetData());
1374 log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
1378 //----------------------------------------------------------------------
1379 // The DW_OP_addr operation has a single operand that encodes a machine
1380 // address and whose size is the size of an address on the target machine.
1381 //----------------------------------------------------------------------
1383 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1384 stack.back().SetValueType(Value::eValueTypeFileAddress);
1385 // Convert the file address to a load address, so subsequent
1386 // DWARF operators can operate on it.
1388 stack.back().ConvertToLoadAddress(module_sp.get(),
1389 frame->CalculateTarget().get());
1392 //----------------------------------------------------------------------
1393 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1394 // shared libraries that have a location like:
1395 // DW_OP_addr(0x1000)
1396 // If this address resides in a shared library, then this virtual address
1397 // won't make sense when it is evaluated in the context of a running
1398 // process where shared libraries have been slid. To account for this, this
1399 // new address type where we can store the section pointer and a 4 byte
1401 //----------------------------------------------------------------------
1402 // case DW_OP_addr_sect_offset4:
1404 // result_type = eResultTypeFileAddress;
1405 // lldb::Section *sect = (lldb::Section
1406 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
1407 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1409 // Address so_addr (sect, sect_offset);
1410 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
1411 // if (load_addr != LLDB_INVALID_ADDRESS)
1413 // // We successfully resolve a file address to a load
1415 // stack.push_back(load_addr);
1422 // error_ptr->SetErrorStringWithFormat ("Section %s in
1423 // %s is not currently loaded.\n",
1424 // sect->GetName().AsCString(),
1425 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1431 //----------------------------------------------------------------------
1432 // OPCODE: DW_OP_deref
1434 // DESCRIPTION: Pops the top stack entry and treats it as an address.
1435 // The value retrieved from that address is pushed. The size of the data
1436 // retrieved from the dereferenced address is the size of an address on the
1438 //----------------------------------------------------------------------
1440 if (stack.empty()) {
1442 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1445 Value::ValueType value_type = stack.back().GetValueType();
1446 switch (value_type) {
1447 case Value::eValueTypeHostAddress: {
1448 void *src = (void *)stack.back().GetScalar().ULongLong();
1450 ::memcpy(&ptr, src, sizeof(void *));
1451 stack.back().GetScalar() = ptr;
1452 stack.back().ClearContext();
1454 case Value::eValueTypeFileAddress: {
1455 auto file_addr = stack.back().GetScalar().ULongLong(
1456 LLDB_INVALID_ADDRESS);
1459 error_ptr->SetErrorStringWithFormat(
1460 "need module to resolve file address for DW_OP_deref");
1464 if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
1466 error_ptr->SetErrorStringWithFormat(
1467 "failed to resolve file address in module");
1470 addr_t load_Addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
1471 if (load_Addr == LLDB_INVALID_ADDRESS) {
1473 error_ptr->SetErrorStringWithFormat(
1474 "failed to resolve load address");
1477 stack.back().GetScalar() = load_Addr;
1478 stack.back().SetValueType(Value::eValueTypeLoadAddress);
1479 // Fall through to load address code below...
1481 case Value::eValueTypeLoadAddress:
1484 lldb::addr_t pointer_addr =
1485 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1487 lldb::addr_t pointer_value =
1488 process->ReadPointerFromMemory(pointer_addr, error);
1489 if (pointer_value != LLDB_INVALID_ADDRESS) {
1490 stack.back().GetScalar() = pointer_value;
1491 stack.back().ClearContext();
1494 error_ptr->SetErrorStringWithFormat(
1495 "Failed to dereference pointer from 0x%" PRIx64
1496 " for DW_OP_deref: %s\n",
1497 pointer_addr, error.AsCString());
1502 error_ptr->SetErrorStringWithFormat(
1503 "NULL process for DW_OP_deref.\n");
1508 error_ptr->SetErrorStringWithFormat(
1509 "NULL execution context for DW_OP_deref.\n");
1520 //----------------------------------------------------------------------
1521 // OPCODE: DW_OP_deref_size
1523 // 1 - uint8_t that specifies the size of the data to dereference.
1524 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1525 // stack entry and treats it as an address. The value retrieved from that
1526 // address is pushed. In the DW_OP_deref_size operation, however, the size
1527 // in bytes of the data retrieved from the dereferenced address is
1528 // specified by the single operand. This operand is a 1-byte unsigned
1529 // integral constant whose value may not be larger than the size of an
1530 // address on the target machine. The data retrieved is zero extended to
1531 // the size of an address on the target machine before being pushed on the
1532 // expression stack.
1533 //----------------------------------------------------------------------
1534 case DW_OP_deref_size: {
1535 if (stack.empty()) {
1537 error_ptr->SetErrorString(
1538 "Expression stack empty for DW_OP_deref_size.");
1541 uint8_t size = opcodes.GetU8(&offset);
1542 Value::ValueType value_type = stack.back().GetValueType();
1543 switch (value_type) {
1544 case Value::eValueTypeHostAddress: {
1545 void *src = (void *)stack.back().GetScalar().ULongLong();
1547 ::memcpy(&ptr, src, sizeof(void *));
1548 // I can't decide whether the size operand should apply to the bytes in
1550 // lldb-host endianness or the target endianness.. I doubt this'll ever
1551 // come up but I'll opt for assuming big endian regardless.
1560 ptr = ptr & 0xffffff;
1563 ptr = ptr & 0xffffffff;
1565 // the casts are added to work around the case where intptr_t is a 32
1567 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1570 ptr = (intptr_t)ptr & 0xffffffffffULL;
1573 ptr = (intptr_t)ptr & 0xffffffffffffULL;
1576 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1581 stack.back().GetScalar() = ptr;
1582 stack.back().ClearContext();
1584 case Value::eValueTypeLoadAddress:
1587 lldb::addr_t pointer_addr =
1588 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1589 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1591 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1593 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
1594 process->GetByteOrder(), size);
1595 lldb::offset_t addr_data_offset = 0;
1598 stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
1601 stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
1604 stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
1607 stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
1610 stack.back().GetScalar() =
1611 addr_data.GetPointer(&addr_data_offset);
1613 stack.back().ClearContext();
1616 error_ptr->SetErrorStringWithFormat(
1617 "Failed to dereference pointer from 0x%" PRIx64
1618 " for DW_OP_deref: %s\n",
1619 pointer_addr, error.AsCString());
1624 error_ptr->SetErrorStringWithFormat(
1625 "NULL process for DW_OP_deref.\n");
1630 error_ptr->SetErrorStringWithFormat(
1631 "NULL execution context for DW_OP_deref.\n");
1642 //----------------------------------------------------------------------
1643 // OPCODE: DW_OP_xderef_size
1645 // 1 - uint8_t that specifies the size of the data to dereference.
1646 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1647 // the top of the stack is treated as an address. The second stack entry is
1648 // treated as an "address space identifier" for those architectures that
1649 // support multiple address spaces. The top two stack elements are popped,
1650 // a data item is retrieved through an implementation-defined address
1651 // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1652 // operation, however, the size in bytes of the data retrieved from the
1653 // dereferenced address is specified by the single operand. This operand is
1654 // a 1-byte unsigned integral constant whose value may not be larger than
1655 // the size of an address on the target machine. The data retrieved is zero
1656 // extended to the size of an address on the target machine before being
1657 // pushed on the expression stack.
1658 //----------------------------------------------------------------------
1659 case DW_OP_xderef_size:
1661 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1663 //----------------------------------------------------------------------
1664 // OPCODE: DW_OP_xderef
1666 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1667 // the top of the stack is treated as an address. The second stack entry is
1668 // treated as an "address space identifier" for those architectures that
1669 // support multiple address spaces. The top two stack elements are popped,
1670 // a data item is retrieved through an implementation-defined address
1671 // calculation and pushed as the new stack top. The size of the data
1672 // retrieved from the dereferenced address is the size of an address on the
1674 //----------------------------------------------------------------------
1677 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1680 //----------------------------------------------------------------------
1681 // All DW_OP_constXXX opcodes have a single operand as noted below:
1684 // --------------- ----------------------------------------------------
1685 // DW_OP_const1u 1-byte unsigned integer constant DW_OP_const1s
1686 // 1-byte signed integer constant DW_OP_const2u 2-byte unsigned integer
1687 // constant DW_OP_const2s 2-byte signed integer constant DW_OP_const4u
1688 // 4-byte unsigned integer constant DW_OP_const4s 4-byte signed integer
1689 // constant DW_OP_const8u 8-byte unsigned integer constant DW_OP_const8s
1690 // 8-byte signed integer constant DW_OP_constu unsigned LEB128 integer
1691 // constant DW_OP_consts signed LEB128 integer constant
1692 //----------------------------------------------------------------------
1694 stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
1697 stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
1700 stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
1703 stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
1706 stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
1709 stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
1712 stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
1715 stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
1718 stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1721 stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1724 //----------------------------------------------------------------------
1725 // OPCODE: DW_OP_dup
1727 // DESCRIPTION: duplicates the value at the top of the stack
1728 //----------------------------------------------------------------------
1730 if (stack.empty()) {
1732 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1735 stack.push_back(stack.back());
1738 //----------------------------------------------------------------------
1739 // OPCODE: DW_OP_drop
1741 // DESCRIPTION: pops the value at the top of the stack
1742 //----------------------------------------------------------------------
1744 if (stack.empty()) {
1746 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1752 //----------------------------------------------------------------------
1753 // OPCODE: DW_OP_over
1755 // DESCRIPTION: Duplicates the entry currently second in the stack at
1756 // the top of the stack.
1757 //----------------------------------------------------------------------
1759 if (stack.size() < 2) {
1761 error_ptr->SetErrorString(
1762 "Expression stack needs at least 2 items for DW_OP_over.");
1765 stack.push_back(stack[stack.size() - 2]);
1768 //----------------------------------------------------------------------
1769 // OPCODE: DW_OP_pick
1770 // OPERANDS: uint8_t index into the current stack
1771 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1772 // inclusive) is pushed on the stack
1773 //----------------------------------------------------------------------
1775 uint8_t pick_idx = opcodes.GetU8(&offset);
1776 if (pick_idx < stack.size())
1777 stack.push_back(stack[pick_idx]);
1780 error_ptr->SetErrorStringWithFormat(
1781 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1786 //----------------------------------------------------------------------
1787 // OPCODE: DW_OP_swap
1789 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1790 // of the stack becomes the second stack entry, and the second entry
1791 // becomes the top of the stack
1792 //----------------------------------------------------------------------
1794 if (stack.size() < 2) {
1796 error_ptr->SetErrorString(
1797 "Expression stack needs at least 2 items for DW_OP_swap.");
1801 stack.back() = stack[stack.size() - 2];
1802 stack[stack.size() - 2] = tmp;
1806 //----------------------------------------------------------------------
1807 // OPCODE: DW_OP_rot
1809 // DESCRIPTION: Rotates the first three stack entries. The entry at
1810 // the top of the stack becomes the third stack entry, the second entry
1811 // becomes the top of the stack, and the third entry becomes the second
1813 //----------------------------------------------------------------------
1815 if (stack.size() < 3) {
1817 error_ptr->SetErrorString(
1818 "Expression stack needs at least 3 items for DW_OP_rot.");
1821 size_t last_idx = stack.size() - 1;
1822 Value old_top = stack[last_idx];
1823 stack[last_idx] = stack[last_idx - 1];
1824 stack[last_idx - 1] = stack[last_idx - 2];
1825 stack[last_idx - 2] = old_top;
1829 //----------------------------------------------------------------------
1830 // OPCODE: DW_OP_abs
1832 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1833 // value and pushes its absolute value. If the absolute value can not be
1834 // represented, the result is undefined.
1835 //----------------------------------------------------------------------
1837 if (stack.empty()) {
1839 error_ptr->SetErrorString(
1840 "Expression stack needs at least 1 item for DW_OP_abs.");
1842 } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
1844 error_ptr->SetErrorString(
1845 "Failed to take the absolute value of the first stack item.");
1850 //----------------------------------------------------------------------
1851 // OPCODE: DW_OP_and
1853 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1854 // operation on the two, and pushes the result.
1855 //----------------------------------------------------------------------
1857 if (stack.size() < 2) {
1859 error_ptr->SetErrorString(
1860 "Expression stack needs at least 2 items for DW_OP_and.");
1865 stack.back().ResolveValue(exe_ctx) =
1866 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1870 //----------------------------------------------------------------------
1871 // OPCODE: DW_OP_div
1873 // DESCRIPTION: pops the top two stack values, divides the former second
1874 // entry by the former top of the stack using signed division, and pushes
1876 //----------------------------------------------------------------------
1878 if (stack.size() < 2) {
1880 error_ptr->SetErrorString(
1881 "Expression stack needs at least 2 items for DW_OP_div.");
1885 if (tmp.ResolveValue(exe_ctx).IsZero()) {
1887 error_ptr->SetErrorString("Divide by zero.");
1892 stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1893 if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1895 error_ptr->SetErrorString("Divide failed.");
1902 //----------------------------------------------------------------------
1903 // OPCODE: DW_OP_minus
1905 // DESCRIPTION: pops the top two stack values, subtracts the former top
1906 // of the stack from the former second entry, and pushes the result.
1907 //----------------------------------------------------------------------
1909 if (stack.size() < 2) {
1911 error_ptr->SetErrorString(
1912 "Expression stack needs at least 2 items for DW_OP_minus.");
1917 stack.back().ResolveValue(exe_ctx) =
1918 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1922 //----------------------------------------------------------------------
1923 // OPCODE: DW_OP_mod
1925 // DESCRIPTION: pops the top two stack values and pushes the result of
1926 // the calculation: former second stack entry modulo the former top of the
1928 //----------------------------------------------------------------------
1930 if (stack.size() < 2) {
1932 error_ptr->SetErrorString(
1933 "Expression stack needs at least 2 items for DW_OP_mod.");
1938 stack.back().ResolveValue(exe_ctx) =
1939 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1943 //----------------------------------------------------------------------
1944 // OPCODE: DW_OP_mul
1946 // DESCRIPTION: pops the top two stack entries, multiplies them
1947 // together, and pushes the result.
1948 //----------------------------------------------------------------------
1950 if (stack.size() < 2) {
1952 error_ptr->SetErrorString(
1953 "Expression stack needs at least 2 items for DW_OP_mul.");
1958 stack.back().ResolveValue(exe_ctx) =
1959 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1963 //----------------------------------------------------------------------
1964 // OPCODE: DW_OP_neg
1966 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1967 //----------------------------------------------------------------------
1969 if (stack.empty()) {
1971 error_ptr->SetErrorString(
1972 "Expression stack needs at least 1 item for DW_OP_neg.");
1975 if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) {
1977 error_ptr->SetErrorString("Unary negate failed.");
1983 //----------------------------------------------------------------------
1984 // OPCODE: DW_OP_not
1986 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1988 //----------------------------------------------------------------------
1990 if (stack.empty()) {
1992 error_ptr->SetErrorString(
1993 "Expression stack needs at least 1 item for DW_OP_not.");
1996 if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) {
1998 error_ptr->SetErrorString("Logical NOT failed.");
2004 //----------------------------------------------------------------------
2007 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
2008 // operation on the two, and pushes the result.
2009 //----------------------------------------------------------------------
2011 if (stack.size() < 2) {
2013 error_ptr->SetErrorString(
2014 "Expression stack needs at least 2 items for DW_OP_or.");
2019 stack.back().ResolveValue(exe_ctx) =
2020 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
2024 //----------------------------------------------------------------------
2025 // OPCODE: DW_OP_plus
2027 // DESCRIPTION: pops the top two stack entries, adds them together, and
2028 // pushes the result.
2029 //----------------------------------------------------------------------
2031 if (stack.size() < 2) {
2033 error_ptr->SetErrorString(
2034 "Expression stack needs at least 2 items for DW_OP_plus.");
2039 stack.back().GetScalar() += tmp.GetScalar();
2043 //----------------------------------------------------------------------
2044 // OPCODE: DW_OP_plus_uconst
2046 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
2047 // constant operand and pushes the result.
2048 //----------------------------------------------------------------------
2049 case DW_OP_plus_uconst:
2050 if (stack.empty()) {
2052 error_ptr->SetErrorString(
2053 "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
2056 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
2057 // Implicit conversion from a UINT to a Scalar...
2058 stack.back().GetScalar() += uconst_value;
2059 if (!stack.back().GetScalar().IsValid()) {
2061 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
2067 //----------------------------------------------------------------------
2068 // OPCODE: DW_OP_shl
2070 // DESCRIPTION: pops the top two stack entries, shifts the former
2071 // second entry left by the number of bits specified by the former top of
2072 // the stack, and pushes the result.
2073 //----------------------------------------------------------------------
2075 if (stack.size() < 2) {
2077 error_ptr->SetErrorString(
2078 "Expression stack needs at least 2 items for DW_OP_shl.");
2083 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
2087 //----------------------------------------------------------------------
2088 // OPCODE: DW_OP_shr
2090 // DESCRIPTION: pops the top two stack entries, shifts the former second
2091 // entry right logically (filling with zero bits) by the number of bits
2092 // specified by the former top of the stack, and pushes the result.
2093 //----------------------------------------------------------------------
2095 if (stack.size() < 2) {
2097 error_ptr->SetErrorString(
2098 "Expression stack needs at least 2 items for DW_OP_shr.");
2103 if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
2104 tmp.ResolveValue(exe_ctx))) {
2106 error_ptr->SetErrorString("DW_OP_shr failed.");
2112 //----------------------------------------------------------------------
2113 // OPCODE: DW_OP_shra
2115 // DESCRIPTION: pops the top two stack entries, shifts the former second
2116 // entry right arithmetically (divide the magnitude by 2, keep the same
2117 // sign for the result) by the number of bits specified by the former top
2118 // of the stack, and pushes the result.
2119 //----------------------------------------------------------------------
2121 if (stack.size() < 2) {
2123 error_ptr->SetErrorString(
2124 "Expression stack needs at least 2 items for DW_OP_shra.");
2129 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
2133 //----------------------------------------------------------------------
2134 // OPCODE: DW_OP_xor
2136 // DESCRIPTION: pops the top two stack entries, performs the bitwise
2137 // exclusive-or operation on the two, and pushes the result.
2138 //----------------------------------------------------------------------
2140 if (stack.size() < 2) {
2142 error_ptr->SetErrorString(
2143 "Expression stack needs at least 2 items for DW_OP_xor.");
2148 stack.back().ResolveValue(exe_ctx) =
2149 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
2153 //----------------------------------------------------------------------
2154 // OPCODE: DW_OP_skip
2155 // OPERANDS: int16_t
2156 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
2157 // signed integer constant. The 2-byte constant is the number of bytes of
2158 // the DWARF expression to skip forward or backward from the current
2159 // operation, beginning after the 2-byte constant.
2160 //----------------------------------------------------------------------
2162 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
2163 lldb::offset_t new_offset = offset + skip_offset;
2164 if (new_offset >= opcodes_offset && new_offset < end_offset)
2165 offset = new_offset;
2168 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
2173 //----------------------------------------------------------------------
2174 // OPCODE: DW_OP_bra
2175 // OPERANDS: int16_t
2176 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
2177 // signed integer constant. This operation pops the top of stack. If the
2178 // value popped is not the constant 0, the 2-byte constant operand is the
2179 // number of bytes of the DWARF expression to skip forward or backward from
2180 // the current operation, beginning after the 2-byte constant.
2181 //----------------------------------------------------------------------
2183 if (stack.empty()) {
2185 error_ptr->SetErrorString(
2186 "Expression stack needs at least 1 item for DW_OP_bra.");
2191 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
2193 if (tmp.ResolveValue(exe_ctx) != zero) {
2194 lldb::offset_t new_offset = offset + bra_offset;
2195 if (new_offset >= opcodes_offset && new_offset < end_offset)
2196 offset = new_offset;
2199 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
2206 //----------------------------------------------------------------------
2209 // DESCRIPTION: pops the top two stack values, compares using the
2210 // equals (==) operator.
2211 // STACK RESULT: push the constant value 1 onto the stack if the result
2212 // of the operation is true or the constant value 0 if the result of the
2213 // operation is false.
2214 //----------------------------------------------------------------------
2216 if (stack.size() < 2) {
2218 error_ptr->SetErrorString(
2219 "Expression stack needs at least 2 items for DW_OP_eq.");
2224 stack.back().ResolveValue(exe_ctx) =
2225 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
2229 //----------------------------------------------------------------------
2232 // DESCRIPTION: pops the top two stack values, compares using the
2233 // greater than or equal to (>=) operator.
2234 // STACK RESULT: push the constant value 1 onto the stack if the result
2235 // of the operation is true or the constant value 0 if the result of the
2236 // operation is false.
2237 //----------------------------------------------------------------------
2239 if (stack.size() < 2) {
2241 error_ptr->SetErrorString(
2242 "Expression stack needs at least 2 items for DW_OP_ge.");
2247 stack.back().ResolveValue(exe_ctx) =
2248 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
2252 //----------------------------------------------------------------------
2255 // DESCRIPTION: pops the top two stack values, compares using the
2256 // greater than (>) operator.
2257 // STACK RESULT: push the constant value 1 onto the stack if the result
2258 // of the operation is true or the constant value 0 if the result of the
2259 // operation is false.
2260 //----------------------------------------------------------------------
2262 if (stack.size() < 2) {
2264 error_ptr->SetErrorString(
2265 "Expression stack needs at least 2 items for DW_OP_gt.");
2270 stack.back().ResolveValue(exe_ctx) =
2271 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
2275 //----------------------------------------------------------------------
2278 // DESCRIPTION: pops the top two stack values, compares using the
2279 // less than or equal to (<=) operator.
2280 // STACK RESULT: push the constant value 1 onto the stack if the result
2281 // of the operation is true or the constant value 0 if the result of the
2282 // operation is false.
2283 //----------------------------------------------------------------------
2285 if (stack.size() < 2) {
2287 error_ptr->SetErrorString(
2288 "Expression stack needs at least 2 items for DW_OP_le.");
2293 stack.back().ResolveValue(exe_ctx) =
2294 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
2298 //----------------------------------------------------------------------
2301 // DESCRIPTION: pops the top two stack values, compares using the
2302 // less than (<) operator.
2303 // STACK RESULT: push the constant value 1 onto the stack if the result
2304 // of the operation is true or the constant value 0 if the result of the
2305 // operation is false.
2306 //----------------------------------------------------------------------
2308 if (stack.size() < 2) {
2310 error_ptr->SetErrorString(
2311 "Expression stack needs at least 2 items for DW_OP_lt.");
2316 stack.back().ResolveValue(exe_ctx) =
2317 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
2321 //----------------------------------------------------------------------
2324 // DESCRIPTION: pops the top two stack values, compares using the
2325 // not equal (!=) operator.
2326 // STACK RESULT: push the constant value 1 onto the stack if the result
2327 // of the operation is true or the constant value 0 if the result of the
2328 // operation is false.
2329 //----------------------------------------------------------------------
2331 if (stack.size() < 2) {
2333 error_ptr->SetErrorString(
2334 "Expression stack needs at least 2 items for DW_OP_ne.");
2339 stack.back().ResolveValue(exe_ctx) =
2340 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
2344 //----------------------------------------------------------------------
2345 // OPCODE: DW_OP_litn
2347 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
2348 // STACK RESULT: push the unsigned literal constant value onto the top
2350 //----------------------------------------------------------------------
2383 stack.push_back(Scalar((uint64_t)(op - DW_OP_lit0)));
2386 //----------------------------------------------------------------------
2387 // OPCODE: DW_OP_regN
2389 // DESCRIPTION: Push the value in register n on the top of the stack.
2390 //----------------------------------------------------------------------
2423 reg_num = op - DW_OP_reg0;
2425 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2426 stack.push_back(tmp);
2430 //----------------------------------------------------------------------
2431 // OPCODE: DW_OP_regx
2433 // ULEB128 literal operand that encodes the register.
2434 // DESCRIPTION: Push the value in register on the top of the stack.
2435 //----------------------------------------------------------------------
2437 reg_num = opcodes.GetULEB128(&offset);
2438 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2439 stack.push_back(tmp);
2444 //----------------------------------------------------------------------
2445 // OPCODE: DW_OP_bregN
2447 // SLEB128 offset from register N
2448 // DESCRIPTION: Value is in memory at the address specified by register
2449 // N plus an offset.
2450 //----------------------------------------------------------------------
2482 case DW_OP_breg31: {
2483 reg_num = op - DW_OP_breg0;
2485 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2487 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2488 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2490 stack.push_back(tmp);
2491 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2495 //----------------------------------------------------------------------
2496 // OPCODE: DW_OP_bregx
2498 // ULEB128 literal operand that encodes the register.
2499 // SLEB128 offset from register N
2500 // DESCRIPTION: Value is in memory at the address specified by register
2501 // N plus an offset.
2502 //----------------------------------------------------------------------
2504 reg_num = opcodes.GetULEB128(&offset);
2506 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2508 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2509 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2511 stack.push_back(tmp);
2512 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2521 if (frame->GetFrameBaseValue(value, error_ptr)) {
2522 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2523 value += fbreg_offset;
2524 stack.push_back(value);
2525 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2530 error_ptr->SetErrorString(
2531 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2536 error_ptr->SetErrorStringWithFormat(
2537 "NULL execution context for DW_OP_fbreg.\n");
2543 //----------------------------------------------------------------------
2544 // OPCODE: DW_OP_nop
2546 // DESCRIPTION: A place holder. It has no effect on the location stack
2547 // or any of its values.
2548 //----------------------------------------------------------------------
2552 //----------------------------------------------------------------------
2553 // OPCODE: DW_OP_piece
2555 // ULEB128: byte size of the piece
2556 // DESCRIPTION: The operand describes the size in bytes of the piece of
2557 // the object referenced by the DWARF expression whose result is at the top
2558 // of the stack. If the piece is located in a register, but does not occupy
2559 // the entire register, the placement of the piece within that register is
2560 // defined by the ABI.
2562 // Many compilers store a single variable in sets of registers, or store a
2563 // variable partially in memory and partially in registers. DW_OP_piece
2564 // provides a way of describing how large a part of a variable a particular
2565 // DWARF expression refers to.
2566 //----------------------------------------------------------------------
2568 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2570 if (piece_byte_size > 0) {
2573 if (stack.empty()) {
2574 // In a multi-piece expression, this means that the current piece is
2575 // not available. Fill with zeros for now by resizing the data and
2577 curr_piece.ResizeData(piece_byte_size);
2578 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2579 pieces.AppendDataToHostBuffer(curr_piece);
2582 // Extract the current piece into "curr_piece"
2583 Value curr_piece_source_value(stack.back());
2586 const Value::ValueType curr_piece_source_value_type =
2587 curr_piece_source_value.GetValueType();
2588 switch (curr_piece_source_value_type) {
2589 case Value::eValueTypeLoadAddress:
2591 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2592 lldb::addr_t load_addr =
2593 curr_piece_source_value.GetScalar().ULongLong(
2594 LLDB_INVALID_ADDRESS);
2595 if (process->ReadMemory(
2596 load_addr, curr_piece.GetBuffer().GetBytes(),
2597 piece_byte_size, error) != piece_byte_size) {
2599 error_ptr->SetErrorStringWithFormat(
2600 "failed to read memory DW_OP_piece(%" PRIu64
2601 ") from 0x%" PRIx64,
2602 piece_byte_size, load_addr);
2607 error_ptr->SetErrorStringWithFormat(
2608 "failed to resize the piece memory buffer for "
2609 "DW_OP_piece(%" PRIu64 ")",
2616 case Value::eValueTypeFileAddress:
2617 case Value::eValueTypeHostAddress:
2619 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2620 LLDB_INVALID_ADDRESS);
2621 error_ptr->SetErrorStringWithFormat(
2622 "failed to read memory DW_OP_piece(%" PRIu64
2623 ") from %s address 0x%" PRIx64,
2624 piece_byte_size, curr_piece_source_value.GetValueType() ==
2625 Value::eValueTypeFileAddress
2632 case Value::eValueTypeScalar: {
2633 uint32_t bit_size = piece_byte_size * 8;
2634 uint32_t bit_offset = 0;
2635 if (!curr_piece_source_value.GetScalar().ExtractBitfield(
2636 bit_size, bit_offset)) {
2638 error_ptr->SetErrorStringWithFormat(
2639 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2640 " byte scalar value.",
2642 (uint64_t)curr_piece_source_value.GetScalar()
2646 curr_piece = curr_piece_source_value;
2649 case Value::eValueTypeVector: {
2650 if (curr_piece_source_value.GetVector().length >= piece_byte_size)
2651 curr_piece_source_value.GetVector().length = piece_byte_size;
2654 error_ptr->SetErrorStringWithFormat(
2655 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2656 " byte vector value.",
2658 (uint64_t)curr_piece_source_value.GetVector().length);
2664 // Check if this is the first piece?
2665 if (op_piece_offset == 0) {
2666 // This is the first piece, we should push it back onto the stack
2667 // so subsequent pieces will be able to access this piece and add
2669 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2671 error_ptr->SetErrorString("failed to append piece data");
2675 // If this is the second or later piece there should be a value on
2677 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2679 error_ptr->SetErrorStringWithFormat(
2680 "DW_OP_piece for offset %" PRIu64
2681 " but top of stack is of size %" PRIu64,
2682 op_piece_offset, pieces.GetBuffer().GetByteSize());
2686 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2688 error_ptr->SetErrorString("failed to append piece data");
2692 op_piece_offset += piece_byte_size;
2697 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2698 if (stack.size() < 1) {
2700 error_ptr->SetErrorString(
2701 "Expression stack needs at least 1 item for DW_OP_bit_piece.");
2704 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2705 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2706 switch (stack.back().GetValueType()) {
2707 case Value::eValueTypeScalar: {
2708 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2709 piece_bit_offset)) {
2711 error_ptr->SetErrorStringWithFormat(
2712 "unable to extract %" PRIu64 " bit value with %" PRIu64
2713 " bit offset from a %" PRIu64 " bit scalar value.",
2714 piece_bit_size, piece_bit_offset,
2715 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2720 case Value::eValueTypeFileAddress:
2721 case Value::eValueTypeLoadAddress:
2722 case Value::eValueTypeHostAddress:
2724 error_ptr->SetErrorStringWithFormat(
2725 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2726 ", bit_offset = %" PRIu64 ") from an address value.",
2727 piece_bit_size, piece_bit_offset);
2731 case Value::eValueTypeVector:
2733 error_ptr->SetErrorStringWithFormat(
2734 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2735 ", bit_offset = %" PRIu64 ") from a vector value.",
2736 piece_bit_size, piece_bit_offset);
2743 //----------------------------------------------------------------------
2744 // OPCODE: DW_OP_push_object_address
2746 // DESCRIPTION: Pushes the address of the object currently being
2747 // evaluated as part of evaluation of a user presented expression. This
2748 // object may correspond to an independent variable described by its own
2749 // DIE or it may be a component of an array, structure, or class whose
2750 // address has been dynamically determined by an earlier step during user
2751 // expression evaluation.
2752 //----------------------------------------------------------------------
2753 case DW_OP_push_object_address:
2754 if (object_address_ptr)
2755 stack.push_back(*object_address_ptr);
2758 error_ptr->SetErrorString("DW_OP_push_object_address used without "
2759 "specifying an object address");
2764 //----------------------------------------------------------------------
2765 // OPCODE: DW_OP_call2
2767 // uint16_t compile unit relative offset of a DIE
2768 // DESCRIPTION: Performs subroutine calls during evaluation
2769 // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2770 // debugging information entry in the current compilation unit.
2772 // Operand interpretation is exactly like that for DW_FORM_ref2.
2774 // This operation transfers control of DWARF expression evaluation to the
2775 // DW_AT_location attribute of the referenced DIE. If there is no such
2776 // attribute, then there is no effect. Execution of the DWARF expression of
2777 // a DW_AT_location attribute may add to and/or remove from values on the
2778 // stack. Execution returns to the point following the call when the end of
2779 // the attribute is reached. Values on the stack at the time of the call
2780 // may be used as parameters by the called expression and values left on
2781 // the stack by the called expression may be used as return values by prior
2782 // agreement between the calling and called expressions.
2783 //----------------------------------------------------------------------
2786 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2788 //----------------------------------------------------------------------
2789 // OPCODE: DW_OP_call4
2791 // uint32_t compile unit relative offset of a DIE
2792 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2793 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2794 // a debugging information entry in the current compilation unit.
2796 // Operand interpretation DW_OP_call4 is exactly like that for
2799 // This operation transfers control of DWARF expression evaluation to the
2800 // DW_AT_location attribute of the referenced DIE. If there is no such
2801 // attribute, then there is no effect. Execution of the DWARF expression of
2802 // a DW_AT_location attribute may add to and/or remove from values on the
2803 // stack. Execution returns to the point following the call when the end of
2804 // the attribute is reached. Values on the stack at the time of the call
2805 // may be used as parameters by the called expression and values left on
2806 // the stack by the called expression may be used as return values by prior
2807 // agreement between the calling and called expressions.
2808 //----------------------------------------------------------------------
2811 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2814 //----------------------------------------------------------------------
2815 // OPCODE: DW_OP_stack_value
2817 // DESCRIPTION: Specifies that the object does not exist in memory but
2818 // rather is a constant value. The value from the top of the stack is the
2819 // value to be used. This is the actual object value and not the location.
2820 //----------------------------------------------------------------------
2821 case DW_OP_stack_value:
2822 stack.back().SetValueType(Value::eValueTypeScalar);
2825 //----------------------------------------------------------------------
2826 // OPCODE: DW_OP_call_frame_cfa
2828 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2829 // the canonical frame address consistent with the call frame information
2830 // located in .debug_frame (or in the FDEs of the eh_frame section).
2831 //----------------------------------------------------------------------
2832 case DW_OP_call_frame_cfa:
2834 // Note that we don't have to parse FDEs because this DWARF expression
2835 // is commonly evaluated with a valid stack frame.
2836 StackID id = frame->GetStackID();
2837 addr_t cfa = id.GetCallFrameAddress();
2838 if (cfa != LLDB_INVALID_ADDRESS) {
2839 stack.push_back(Scalar(cfa));
2840 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2841 } else if (error_ptr)
2842 error_ptr->SetErrorString("Stack frame does not include a canonical "
2843 "frame address for DW_OP_call_frame_cfa "
2847 error_ptr->SetErrorString("Invalid stack frame in context for "
2848 "DW_OP_call_frame_cfa opcode.");
2853 //----------------------------------------------------------------------
2854 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2855 // opcode, DW_OP_GNU_push_tls_address)
2857 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2858 // an address in the current thread's thread-local storage block, and
2859 // pushes it on the stack.
2860 //----------------------------------------------------------------------
2861 case DW_OP_form_tls_address:
2862 case DW_OP_GNU_push_tls_address: {
2863 if (stack.size() < 1) {
2865 if (op == DW_OP_form_tls_address)
2866 error_ptr->SetErrorString(
2867 "DW_OP_form_tls_address needs an argument.");
2869 error_ptr->SetErrorString(
2870 "DW_OP_GNU_push_tls_address needs an argument.");
2875 if (!exe_ctx || !module_sp) {
2877 error_ptr->SetErrorString("No context to evaluate TLS within.");
2881 Thread *thread = exe_ctx->GetThreadPtr();
2884 error_ptr->SetErrorString("No thread to evaluate TLS within.");
2888 // Lookup the TLS block address for this thread and module.
2889 const addr_t tls_file_addr =
2890 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2891 const addr_t tls_load_addr =
2892 thread->GetThreadLocalData(module_sp, tls_file_addr);
2894 if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2896 error_ptr->SetErrorString(
2897 "No TLS data currently exists for this thread.");
2901 stack.back().GetScalar() = tls_load_addr;
2902 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2905 //----------------------------------------------------------------------
2906 // OPCODE: DW_OP_GNU_addr_index
2908 // ULEB128: index to the .debug_addr section
2909 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2910 // section with the base address specified by the DW_AT_addr_base attribute
2911 // and the 0 based index is the ULEB128 encoded index.
2912 //----------------------------------------------------------------------
2913 case DW_OP_GNU_addr_index: {
2916 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2917 "compile unit being specified");
2920 uint64_t index = opcodes.GetULEB128(&offset);
2921 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2922 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2923 lldb::offset_t offset = addr_base + index * index_size;
2925 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
2926 &offset, index_size);
2927 stack.push_back(Scalar(value));
2928 stack.back().SetValueType(Value::eValueTypeFileAddress);
2931 //----------------------------------------------------------------------
2932 // OPCODE: DW_OP_GNU_const_index
2934 // ULEB128: index to the .debug_addr section
2935 // DESCRIPTION: Pushes an constant with the size of a machine address to
2936 // the stack from the .debug_addr section with the base address specified
2937 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2939 //----------------------------------------------------------------------
2940 case DW_OP_GNU_const_index: {
2943 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2944 "compile unit being specified");
2947 uint64_t index = opcodes.GetULEB128(&offset);
2948 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2949 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2950 lldb::offset_t offset = addr_base + index * index_size;
2951 const DWARFDataExtractor &debug_addr =
2952 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data();
2953 switch (index_size) {
2955 stack.push_back(Scalar(debug_addr.GetU32(&offset)));
2958 stack.push_back(Scalar(debug_addr.GetU64(&offset)));
2961 assert(false && "Unhandled index size");
2968 log->Printf("Unhandled opcode %s in DWARFExpression.",
2969 DW_OP_value_to_name(op));
2974 if (stack.empty()) {
2975 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2976 // or DW_OP_bit_piece opcodes
2977 if (pieces.GetBuffer().GetByteSize()) {
2981 error_ptr->SetErrorString("Stack empty after evaluation.");
2985 if (log && log->GetVerbose()) {
2986 size_t count = stack.size();
2987 log->Printf("Stack after operation has %" PRIu64 " values:",
2989 for (size_t i = 0; i < count; ++i) {
2990 StreamString new_value;
2991 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2992 stack[i].Dump(&new_value);
2993 log->Printf(" %s", new_value.GetData());
2996 result = stack.back();
2998 return true; // Return true on success
3001 size_t DWARFExpression::LocationListSize(const DWARFUnit *dwarf_cu,
3002 const DataExtractor &debug_loc_data,
3003 lldb::offset_t offset) {
3004 const lldb::offset_t debug_loc_offset = offset;
3005 while (debug_loc_data.ValidOffset(offset)) {
3006 lldb::addr_t start_addr = LLDB_INVALID_ADDRESS;
3007 lldb::addr_t end_addr = LLDB_INVALID_ADDRESS;
3008 if (!AddressRangeForLocationListEntry(dwarf_cu, debug_loc_data, &offset,
3009 start_addr, end_addr))
3012 if (start_addr == 0 && end_addr == 0)
3015 uint16_t loc_length = debug_loc_data.GetU16(&offset);
3016 offset += loc_length;
3019 if (offset > debug_loc_offset)
3020 return offset - debug_loc_offset;
3024 bool DWARFExpression::AddressRangeForLocationListEntry(
3025 const DWARFUnit *dwarf_cu, const DataExtractor &debug_loc_data,
3026 lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc) {
3027 if (!debug_loc_data.ValidOffset(*offset_ptr))
3030 DWARFExpression::LocationListFormat format =
3031 dwarf_cu->GetSymbolFileDWARF()->GetLocationListFormat();
3033 case NonLocationList:
3035 case RegularLocationList:
3036 low_pc = debug_loc_data.GetAddress(offset_ptr);
3037 high_pc = debug_loc_data.GetAddress(offset_ptr);
3039 case SplitDwarfLocationList:
3041 switch (debug_loc_data.GetU8(offset_ptr)) {
3042 case DW_LLE_end_of_list:
3044 case DW_LLE_startx_endx: {
3045 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3046 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3047 index = debug_loc_data.GetULEB128(offset_ptr);
3048 high_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3051 case DW_LLE_startx_length: {
3052 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3053 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3054 uint64_t length = (format == LocLists)
3055 ? debug_loc_data.GetULEB128(offset_ptr)
3056 : debug_loc_data.GetU32(offset_ptr);
3057 high_pc = low_pc + length;
3060 case DW_LLE_start_length: {
3061 low_pc = debug_loc_data.GetAddress(offset_ptr);
3062 high_pc = low_pc + debug_loc_data.GetULEB128(offset_ptr);
3065 case DW_LLE_start_end: {
3066 low_pc = debug_loc_data.GetAddress(offset_ptr);
3067 high_pc = debug_loc_data.GetAddress(offset_ptr);
3071 // Not supported entry type
3072 lldbassert(false && "Not supported location list type");
3076 assert(false && "Not supported location list type");
3080 static bool print_dwarf_exp_op(Stream &s, const DataExtractor &data,
3081 lldb::offset_t *offset_ptr, int address_size,
3082 int dwarf_ref_size) {
3083 uint8_t opcode = data.GetU8(offset_ptr);
3084 DRC_class opcode_class;
3090 opcode_class = DW_OP_value_to_class(opcode) & (~DRC_DWARFv3);
3092 s.Printf("%s ", DW_OP_value_to_name(opcode));
3094 /* Does this take zero parameters? If so we can shortcut this function. */
3095 if (opcode_class == DRC_ZEROOPERANDS)
3098 if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_bregx) {
3099 uint = data.GetULEB128(offset_ptr);
3100 sint = data.GetSLEB128(offset_ptr);
3101 s.Printf("%" PRIu64 " %" PRIi64, uint, sint);
3104 if (opcode_class != DRC_ONEOPERAND) {
3105 s.Printf("UNKNOWN OP %u", opcode);
3111 size = address_size;
3181 case DW_OP_deref_size:
3182 case DW_OP_xderef_size:
3195 case DW_OP_call_ref:
3196 size = dwarf_ref_size;
3199 case DW_OP_plus_uconst:
3201 case DW_OP_GNU_addr_index:
3202 case DW_OP_GNU_const_index:
3206 s.Printf("UNKNOWN ONE-OPERAND OPCODE, #%u", opcode);
3212 sint = (int8_t)data.GetU8(offset_ptr);
3213 s.Printf("%+" PRIi64, sint);
3216 sint = (int16_t)data.GetU16(offset_ptr);
3217 s.Printf("%+" PRIi64, sint);
3220 sint = (int32_t)data.GetU32(offset_ptr);
3221 s.Printf("%+" PRIi64, sint);
3224 sint = (int64_t)data.GetU64(offset_ptr);
3225 s.Printf("%+" PRIi64, sint);
3228 sint = data.GetSLEB128(offset_ptr);
3229 s.Printf("%+" PRIi64, sint);
3232 uint = data.GetU8(offset_ptr);
3233 s.Printf("0x%2.2" PRIx64, uint);
3236 uint = data.GetU16(offset_ptr);
3237 s.Printf("0x%4.4" PRIx64, uint);
3240 uint = data.GetU32(offset_ptr);
3241 s.Printf("0x%8.8" PRIx64, uint);
3244 uint = data.GetU64(offset_ptr);
3245 s.Printf("0x%16.16" PRIx64, uint);
3248 uint = data.GetULEB128(offset_ptr);
3249 s.Printf("0x%" PRIx64, uint);
3256 bool DWARFExpression::PrintDWARFExpression(Stream &s, const DataExtractor &data,
3257 int address_size, int dwarf_ref_size,
3258 bool location_expression) {
3260 lldb::offset_t offset = 0;
3261 while (data.ValidOffset(offset)) {
3262 if (location_expression && op_count > 0)
3266 if (!print_dwarf_exp_op(s, data, &offset, address_size, dwarf_ref_size))
3274 void DWARFExpression::PrintDWARFLocationList(
3275 Stream &s, const DWARFUnit *cu, const DataExtractor &debug_loc_data,
3276 lldb::offset_t offset) {
3277 uint64_t start_addr, end_addr;
3278 uint32_t addr_size = DWARFUnit::GetAddressByteSize(cu);
3279 s.SetAddressByteSize(DWARFUnit::GetAddressByteSize(cu));
3280 dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
3281 while (debug_loc_data.ValidOffset(offset)) {
3282 start_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3283 end_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3285 if (start_addr == 0 && end_addr == 0)
3288 s.PutCString("\n ");
3291 s.AddressRange(start_addr + base_addr, end_addr + base_addr,
3292 cu->GetAddressByteSize(), NULL, ": ");
3293 uint32_t loc_length = debug_loc_data.GetU16(&offset);
3295 DataExtractor locationData(debug_loc_data, offset, loc_length);
3296 PrintDWARFExpression(s, locationData, addr_size, 4, false);
3297 offset += loc_length;
3301 bool DWARFExpression::GetOpAndEndOffsets(StackFrame &frame,
3302 lldb::offset_t &op_offset,
3303 lldb::offset_t &end_offset) {
3304 SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction);
3309 addr_t loclist_base_file_addr =
3310 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
3311 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS) {
3315 addr_t pc_file_addr = frame.GetFrameCodeAddress().GetFileAddress();
3316 lldb::offset_t opcodes_offset, opcodes_length;
3317 if (!GetLocation(loclist_base_file_addr, pc_file_addr, opcodes_offset,
3322 if (opcodes_length == 0) {
3326 op_offset = opcodes_offset;
3327 end_offset = opcodes_offset + opcodes_length;
3331 bool DWARFExpression::MatchesOperand(StackFrame &frame,
3332 const Instruction::Operand &operand) {
3333 using namespace OperandMatchers;
3335 lldb::offset_t op_offset;
3336 lldb::offset_t end_offset;
3337 if (!GetOpAndEndOffsets(frame, op_offset, end_offset)) {
3341 if (!m_data.ValidOffset(op_offset) || op_offset >= end_offset) {
3345 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
3350 DataExtractor opcodes = m_data;
3351 uint8_t opcode = opcodes.GetU8(&op_offset);
3353 if (opcode == DW_OP_fbreg) {
3354 int64_t offset = opcodes.GetSLEB128(&op_offset);
3356 DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr);
3361 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
3362 return fb_expr->MatchesOperand(frame, child);
3366 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3367 recurse)(operand)) {
3371 return MatchUnaryOp(
3372 MatchOpType(Instruction::Operand::Type::Dereference),
3373 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3374 MatchImmOp(offset), recurse))(operand);
3377 bool dereference = false;
3378 const RegisterInfo *reg = nullptr;
3381 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
3382 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
3383 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
3384 offset = opcodes.GetSLEB128(&op_offset);
3385 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
3386 } else if (opcode == DW_OP_regx) {
3387 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3388 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3389 } else if (opcode == DW_OP_bregx) {
3390 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3391 offset = opcodes.GetSLEB128(&op_offset);
3392 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3403 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3404 MatchRegOp(*reg))(operand)) {
3408 return MatchUnaryOp(
3409 MatchOpType(Instruction::Operand::Type::Dereference),
3410 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3412 MatchImmOp(offset)))(operand);
3414 return MatchRegOp(*reg)(operand);