1 //===-- DWARFExpression.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Expression/DWARFExpression.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/RegisterValue.h"
20 #include "lldb/Core/Scalar.h"
21 #include "lldb/Core/Value.h"
22 #include "lldb/Core/dwarf.h"
23 #include "lldb/Utility/DataEncoder.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/StreamString.h"
26 #include "lldb/Utility/VMRange.h"
28 #include "lldb/Host/Host.h"
29 #include "lldb/Utility/Endian.h"
31 #include "lldb/Symbol/Function.h"
33 #include "lldb/Target/ABI.h"
34 #include "lldb/Target/ExecutionContext.h"
35 #include "lldb/Target/Process.h"
36 #include "lldb/Target/RegisterContext.h"
37 #include "lldb/Target/StackFrame.h"
38 #include "lldb/Target/StackID.h"
39 #include "lldb/Target/Thread.h"
41 #include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
44 using namespace lldb_private;
47 ReadAddressFromDebugAddrSection(const DWARFUnit *dwarf_cu,
49 uint32_t index_size = dwarf_cu->GetAddressByteSize();
50 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
51 lldb::offset_t offset = addr_base + index * index_size;
52 return dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
56 //----------------------------------------------------------------------
57 // DWARFExpression constructor
58 //----------------------------------------------------------------------
59 DWARFExpression::DWARFExpression(DWARFUnit *dwarf_cu)
60 : m_module_wp(), m_data(), m_dwarf_cu(dwarf_cu),
61 m_reg_kind(eRegisterKindDWARF), m_loclist_slide(LLDB_INVALID_ADDRESS) {}
63 DWARFExpression::DWARFExpression(const DWARFExpression &rhs)
64 : m_module_wp(rhs.m_module_wp), m_data(rhs.m_data),
65 m_dwarf_cu(rhs.m_dwarf_cu), m_reg_kind(rhs.m_reg_kind),
66 m_loclist_slide(rhs.m_loclist_slide) {}
68 DWARFExpression::DWARFExpression(lldb::ModuleSP module_sp,
69 const DataExtractor &data,
71 lldb::offset_t data_offset,
72 lldb::offset_t data_length)
73 : m_module_wp(), m_data(data, data_offset, data_length),
74 m_dwarf_cu(dwarf_cu), m_reg_kind(eRegisterKindDWARF),
75 m_loclist_slide(LLDB_INVALID_ADDRESS) {
77 m_module_wp = module_sp;
80 //----------------------------------------------------------------------
82 //----------------------------------------------------------------------
83 DWARFExpression::~DWARFExpression() {}
85 bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
87 void DWARFExpression::SetOpcodeData(const DataExtractor &data) {
91 void DWARFExpression::CopyOpcodeData(lldb::ModuleSP module_sp,
92 const DataExtractor &data,
93 lldb::offset_t data_offset,
94 lldb::offset_t data_length) {
95 const uint8_t *bytes = data.PeekData(data_offset, data_length);
97 m_module_wp = module_sp;
98 m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length)));
99 m_data.SetByteOrder(data.GetByteOrder());
100 m_data.SetAddressByteSize(data.GetAddressByteSize());
104 void DWARFExpression::CopyOpcodeData(const void *data,
105 lldb::offset_t data_length,
106 ByteOrder byte_order,
107 uint8_t addr_byte_size) {
108 if (data && data_length) {
109 m_data.SetData(DataBufferSP(new DataBufferHeap(data, data_length)));
110 m_data.SetByteOrder(byte_order);
111 m_data.SetAddressByteSize(addr_byte_size);
115 void DWARFExpression::CopyOpcodeData(uint64_t const_value,
116 lldb::offset_t const_value_byte_size,
117 uint8_t addr_byte_size) {
118 if (const_value_byte_size) {
120 DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
121 m_data.SetByteOrder(endian::InlHostByteOrder());
122 m_data.SetAddressByteSize(addr_byte_size);
126 void DWARFExpression::SetOpcodeData(lldb::ModuleSP module_sp,
127 const DataExtractor &data,
128 lldb::offset_t data_offset,
129 lldb::offset_t data_length) {
130 m_module_wp = module_sp;
131 m_data.SetData(data, data_offset, data_length);
134 void DWARFExpression::DumpLocation(Stream *s, lldb::offset_t offset,
135 lldb::offset_t length,
136 lldb::DescriptionLevel level,
138 if (!m_data.ValidOffsetForDataOfSize(offset, length))
140 const lldb::offset_t start_offset = offset;
141 const lldb::offset_t end_offset = offset + length;
142 while (m_data.ValidOffset(offset) && offset < end_offset) {
143 const lldb::offset_t op_offset = offset;
144 const uint8_t op = m_data.GetU8(&offset);
150 case lldb::eDescriptionLevelBrief:
151 if (op_offset > start_offset)
155 case lldb::eDescriptionLevelFull:
156 case lldb::eDescriptionLevelVerbose:
157 if (op_offset > start_offset)
160 if (level == lldb::eDescriptionLevelFull)
162 // Fall through for verbose and print offset and DW_OP prefix..
163 s->Printf("0x%8.8" PRIx64 ": %s", op_offset,
164 op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
170 *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") ";
171 break; // 0x03 1 address
176 s->Printf("DW_OP_const1u(0x%2.2x)", m_data.GetU8(&offset));
177 break; // 0x08 1 1-byte constant
179 s->Printf("DW_OP_const1s(0x%2.2x)", m_data.GetU8(&offset));
180 break; // 0x09 1 1-byte constant
182 s->Printf("DW_OP_const2u(0x%4.4x)", m_data.GetU16(&offset));
183 break; // 0x0a 1 2-byte constant
185 s->Printf("DW_OP_const2s(0x%4.4x)", m_data.GetU16(&offset));
186 break; // 0x0b 1 2-byte constant
188 s->Printf("DW_OP_const4u(0x%8.8x)", m_data.GetU32(&offset));
189 break; // 0x0c 1 4-byte constant
191 s->Printf("DW_OP_const4s(0x%8.8x)", m_data.GetU32(&offset));
192 break; // 0x0d 1 4-byte constant
194 s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ")", m_data.GetU64(&offset));
195 break; // 0x0e 1 8-byte constant
197 s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ")", m_data.GetU64(&offset));
198 break; // 0x0f 1 8-byte constant
200 s->Printf("DW_OP_constu(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
201 break; // 0x10 1 ULEB128 constant
203 s->Printf("DW_OP_consts(0x%" PRId64 ")", m_data.GetSLEB128(&offset));
204 break; // 0x11 1 SLEB128 constant
206 s->PutCString("DW_OP_dup");
209 s->PutCString("DW_OP_drop");
212 s->PutCString("DW_OP_over");
215 s->Printf("DW_OP_pick(0x%2.2x)", m_data.GetU8(&offset));
216 break; // 0x15 1 1-byte stack index
218 s->PutCString("DW_OP_swap");
221 s->PutCString("DW_OP_rot");
224 s->PutCString("DW_OP_xderef");
227 s->PutCString("DW_OP_abs");
230 s->PutCString("DW_OP_and");
233 s->PutCString("DW_OP_div");
236 s->PutCString("DW_OP_minus");
239 s->PutCString("DW_OP_mod");
242 s->PutCString("DW_OP_mul");
245 s->PutCString("DW_OP_neg");
248 s->PutCString("DW_OP_not");
251 s->PutCString("DW_OP_or");
254 s->PutCString("DW_OP_plus");
256 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
257 s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ")",
258 m_data.GetULEB128(&offset));
262 s->PutCString("DW_OP_shl");
265 s->PutCString("DW_OP_shr");
268 s->PutCString("DW_OP_shra");
271 s->PutCString("DW_OP_xor");
274 s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset));
275 break; // 0x2f 1 signed 2-byte constant
277 s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset));
278 break; // 0x28 1 signed 2-byte constant
280 s->PutCString("DW_OP_eq");
283 s->PutCString("DW_OP_ge");
286 s->PutCString("DW_OP_gt");
289 s->PutCString("DW_OP_le");
292 s->PutCString("DW_OP_lt");
295 s->PutCString("DW_OP_ne");
298 case DW_OP_lit0: // 0x30
299 case DW_OP_lit1: // 0x31
300 case DW_OP_lit2: // 0x32
301 case DW_OP_lit3: // 0x33
302 case DW_OP_lit4: // 0x34
303 case DW_OP_lit5: // 0x35
304 case DW_OP_lit6: // 0x36
305 case DW_OP_lit7: // 0x37
306 case DW_OP_lit8: // 0x38
307 case DW_OP_lit9: // 0x39
308 case DW_OP_lit10: // 0x3A
309 case DW_OP_lit11: // 0x3B
310 case DW_OP_lit12: // 0x3C
311 case DW_OP_lit13: // 0x3D
312 case DW_OP_lit14: // 0x3E
313 case DW_OP_lit15: // 0x3F
314 case DW_OP_lit16: // 0x40
315 case DW_OP_lit17: // 0x41
316 case DW_OP_lit18: // 0x42
317 case DW_OP_lit19: // 0x43
318 case DW_OP_lit20: // 0x44
319 case DW_OP_lit21: // 0x45
320 case DW_OP_lit22: // 0x46
321 case DW_OP_lit23: // 0x47
322 case DW_OP_lit24: // 0x48
323 case DW_OP_lit25: // 0x49
324 case DW_OP_lit26: // 0x4A
325 case DW_OP_lit27: // 0x4B
326 case DW_OP_lit28: // 0x4C
327 case DW_OP_lit29: // 0x4D
328 case DW_OP_lit30: // 0x4E
330 s->Printf("DW_OP_lit%i", op - DW_OP_lit0);
333 case DW_OP_reg0: // 0x50
334 case DW_OP_reg1: // 0x51
335 case DW_OP_reg2: // 0x52
336 case DW_OP_reg3: // 0x53
337 case DW_OP_reg4: // 0x54
338 case DW_OP_reg5: // 0x55
339 case DW_OP_reg6: // 0x56
340 case DW_OP_reg7: // 0x57
341 case DW_OP_reg8: // 0x58
342 case DW_OP_reg9: // 0x59
343 case DW_OP_reg10: // 0x5A
344 case DW_OP_reg11: // 0x5B
345 case DW_OP_reg12: // 0x5C
346 case DW_OP_reg13: // 0x5D
347 case DW_OP_reg14: // 0x5E
348 case DW_OP_reg15: // 0x5F
349 case DW_OP_reg16: // 0x60
350 case DW_OP_reg17: // 0x61
351 case DW_OP_reg18: // 0x62
352 case DW_OP_reg19: // 0x63
353 case DW_OP_reg20: // 0x64
354 case DW_OP_reg21: // 0x65
355 case DW_OP_reg22: // 0x66
356 case DW_OP_reg23: // 0x67
357 case DW_OP_reg24: // 0x68
358 case DW_OP_reg25: // 0x69
359 case DW_OP_reg26: // 0x6A
360 case DW_OP_reg27: // 0x6B
361 case DW_OP_reg28: // 0x6C
362 case DW_OP_reg29: // 0x6D
363 case DW_OP_reg30: // 0x6E
364 case DW_OP_reg31: // 0x6F
366 uint32_t reg_num = op - DW_OP_reg0;
368 RegisterInfo reg_info;
369 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
371 s->PutCString(reg_info.name);
373 } else if (reg_info.alt_name) {
374 s->PutCString(reg_info.alt_name);
379 s->Printf("DW_OP_reg%u", reg_num);
415 uint32_t reg_num = op - DW_OP_breg0;
416 int64_t reg_offset = m_data.GetSLEB128(&offset);
418 RegisterInfo reg_info;
419 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
421 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
423 } else if (reg_info.alt_name) {
424 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
429 s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset);
432 case DW_OP_regx: // 0x90 1 ULEB128 register
434 uint32_t reg_num = m_data.GetULEB128(&offset);
436 RegisterInfo reg_info;
437 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
439 s->PutCString(reg_info.name);
441 } else if (reg_info.alt_name) {
442 s->PutCString(reg_info.alt_name);
447 s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num);
450 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
451 s->Printf("DW_OP_fbreg(%" PRIi64 ")", m_data.GetSLEB128(&offset));
453 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
455 uint32_t reg_num = m_data.GetULEB128(&offset);
456 int64_t reg_offset = m_data.GetSLEB128(&offset);
458 RegisterInfo reg_info;
459 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
461 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
463 } else if (reg_info.alt_name) {
464 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
469 s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num,
472 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
473 s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
475 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
476 s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
478 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
479 s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
482 s->PutCString("DW_OP_nop");
484 case DW_OP_push_object_address:
485 s->PutCString("DW_OP_push_object_address");
486 break; // 0x97 DWARF3
487 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
488 s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
490 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
491 s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
493 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
494 s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset));
496 // case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break;
498 // case DW_OP_bit_piece: // 0x9d DWARF3 2
499 // s->Printf("DW_OP_bit_piece(0x%x, 0x%x)",
500 // m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
502 // case DW_OP_lo_user: s->PutCString("DW_OP_lo_user"); break;
504 // case DW_OP_hi_user: s->PutCString("DW_OP_hi_user"); break;
506 // case DW_OP_APPLE_extern:
507 // s->Printf("DW_OP_APPLE_extern(%" PRIu64 ")",
508 // m_data.GetULEB128(&offset));
510 // case DW_OP_APPLE_array_ref:
511 // s->PutCString("DW_OP_APPLE_array_ref");
513 case DW_OP_form_tls_address:
514 s->PutCString("DW_OP_form_tls_address"); // 0x9b
516 case DW_OP_GNU_addr_index: // 0xfb
517 s->Printf("DW_OP_GNU_addr_index(0x%" PRIx64 ")",
518 m_data.GetULEB128(&offset));
520 case DW_OP_GNU_const_index: // 0xfc
521 s->Printf("DW_OP_GNU_const_index(0x%" PRIx64 ")",
522 m_data.GetULEB128(&offset));
524 case DW_OP_GNU_push_tls_address:
525 s->PutCString("DW_OP_GNU_push_tls_address"); // 0xe0
527 case DW_OP_APPLE_uninit:
528 s->PutCString("DW_OP_APPLE_uninit"); // 0xF0
530 // case DW_OP_APPLE_assign: // 0xF1 - pops value off and
531 // assigns it to second item on stack (2nd item must have
532 // assignable context)
533 // s->PutCString("DW_OP_APPLE_assign");
535 // case DW_OP_APPLE_address_of: // 0xF2 - gets the address of
536 // the top stack item (top item must be a variable, or have
537 // value_type that is an address already)
538 // s->PutCString("DW_OP_APPLE_address_of");
540 // case DW_OP_APPLE_value_of: // 0xF3 - pops the value off the
541 // stack and pushes the value of that object (top item must be a
542 // variable, or expression local)
543 // s->PutCString("DW_OP_APPLE_value_of");
545 // case DW_OP_APPLE_deref_type: // 0xF4 - gets the address of
546 // the top stack item (top item must be a variable, or a clang
548 // s->PutCString("DW_OP_APPLE_deref_type");
550 // case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression
552 // s->Printf("DW_OP_APPLE_expr_local(%" PRIu64 ")",
553 // m_data.GetULEB128(&offset));
555 // case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size,
556 // followed by constant float data
558 // uint8_t float_length = m_data.GetU8(&offset);
559 // s->Printf("DW_OP_APPLE_constf(<%u> ", float_length);
560 // m_data.Dump(s, offset, eFormatHex, float_length, 1,
561 // UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
563 // // Consume the float data
564 // m_data.GetData(&offset, float_length);
567 // case DW_OP_APPLE_scalar_cast:
568 // s->Printf("DW_OP_APPLE_scalar_cast(%s)",
569 // Scalar::GetValueTypeAsCString
570 // ((Scalar::Type)m_data.GetU8(&offset)));
572 // case DW_OP_APPLE_clang_cast:
574 // clang::Type *clang_type = (clang::Type
575 // *)m_data.GetMaxU64(&offset, sizeof(void*));
576 // s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type);
579 // case DW_OP_APPLE_clear:
580 // s->PutCString("DW_OP_APPLE_clear");
582 // case DW_OP_APPLE_error: // 0xFF - Stops expression
583 // evaluation and returns an error (no args)
584 // s->PutCString("DW_OP_APPLE_error");
590 void DWARFExpression::SetLocationListSlide(addr_t slide) {
591 m_loclist_slide = slide;
594 int DWARFExpression::GetRegisterKind() { return m_reg_kind; }
596 void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
597 m_reg_kind = reg_kind;
600 bool DWARFExpression::IsLocationList() const {
601 return m_loclist_slide != LLDB_INVALID_ADDRESS;
604 void DWARFExpression::GetDescription(Stream *s, lldb::DescriptionLevel level,
605 addr_t location_list_base_addr,
607 if (IsLocationList()) {
608 // We have a location list
609 lldb::offset_t offset = 0;
611 addr_t curr_base_addr = location_list_base_addr;
612 while (m_data.ValidOffset(offset)) {
613 addr_t begin_addr_offset = LLDB_INVALID_ADDRESS;
614 addr_t end_addr_offset = LLDB_INVALID_ADDRESS;
615 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
616 begin_addr_offset, end_addr_offset))
619 if (begin_addr_offset == 0 && end_addr_offset == 0)
622 if (begin_addr_offset < end_addr_offset) {
625 VMRange addr_range(curr_base_addr + begin_addr_offset,
626 curr_base_addr + end_addr_offset);
627 addr_range.Dump(s, 0, 8);
629 lldb::offset_t location_length = m_data.GetU16(&offset);
630 DumpLocation(s, offset, location_length, level, abi);
632 offset += location_length;
634 if ((m_data.GetAddressByteSize() == 4 &&
635 (begin_addr_offset == UINT32_MAX)) ||
636 (m_data.GetAddressByteSize() == 8 &&
637 (begin_addr_offset == UINT64_MAX))) {
638 curr_base_addr = end_addr_offset + location_list_base_addr;
639 // We have a new base address
642 *s << "base_addr = " << end_addr_offset;
649 // We have a normal location that contains DW_OP location opcodes
650 DumpLocation(s, 0, m_data.GetByteSize(), level, abi);
654 static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
655 lldb::RegisterKind reg_kind,
656 uint32_t reg_num, Status *error_ptr,
658 if (reg_ctx == NULL) {
660 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
662 uint32_t native_reg =
663 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
664 if (native_reg == LLDB_INVALID_REGNUM) {
666 error_ptr->SetErrorStringWithFormat("Unable to convert register "
667 "kind=%u reg_num=%u to a native "
668 "register number.\n",
671 const RegisterInfo *reg_info =
672 reg_ctx->GetRegisterInfoAtIndex(native_reg);
673 RegisterValue reg_value;
674 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
675 if (reg_value.GetScalarValue(value.GetScalar())) {
676 value.SetValueType(Value::eValueTypeScalar);
677 value.SetContext(Value::eContextTypeRegisterInfo,
678 const_cast<RegisterInfo *>(reg_info));
683 // If we get this error, then we need to implement a value buffer in
684 // the dwarf expression evaluation function...
686 error_ptr->SetErrorStringWithFormat(
687 "register %s can't be converted to a scalar value",
692 error_ptr->SetErrorStringWithFormat("register %s is not available",
701 // DWARFExpression::LocationListContainsLoadAddress (Process* process, const
702 // Address &addr) const
704 // return LocationListContainsLoadAddress(process,
705 // addr.GetLoadAddress(process));
709 // DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t
712 // if (load_addr == LLDB_INVALID_ADDRESS)
715 // if (IsLocationList())
717 // lldb::offset_t offset = 0;
719 // addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
721 // if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
724 // while (m_data.ValidOffset(offset))
726 // // We need to figure out what the value is for the location.
727 // addr_t lo_pc = m_data.GetAddress(&offset);
728 // addr_t hi_pc = m_data.GetAddress(&offset);
729 // if (lo_pc == 0 && hi_pc == 0)
733 // lo_pc += loc_list_base_addr;
734 // hi_pc += loc_list_base_addr;
736 // if (lo_pc <= load_addr && load_addr < hi_pc)
739 // offset += m_data.GetU16(&offset);
746 static offset_t GetOpcodeDataSize(const DataExtractor &data,
747 const lldb::offset_t data_offset,
749 lldb::offset_t offset = data_offset;
752 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
753 return data.GetAddressByteSize();
755 // Opcodes with no arguments
756 case DW_OP_deref: // 0x06
757 case DW_OP_dup: // 0x12
758 case DW_OP_drop: // 0x13
759 case DW_OP_over: // 0x14
760 case DW_OP_swap: // 0x16
761 case DW_OP_rot: // 0x17
762 case DW_OP_xderef: // 0x18
763 case DW_OP_abs: // 0x19
764 case DW_OP_and: // 0x1a
765 case DW_OP_div: // 0x1b
766 case DW_OP_minus: // 0x1c
767 case DW_OP_mod: // 0x1d
768 case DW_OP_mul: // 0x1e
769 case DW_OP_neg: // 0x1f
770 case DW_OP_not: // 0x20
771 case DW_OP_or: // 0x21
772 case DW_OP_plus: // 0x22
773 case DW_OP_shl: // 0x24
774 case DW_OP_shr: // 0x25
775 case DW_OP_shra: // 0x26
776 case DW_OP_xor: // 0x27
777 case DW_OP_eq: // 0x29
778 case DW_OP_ge: // 0x2a
779 case DW_OP_gt: // 0x2b
780 case DW_OP_le: // 0x2c
781 case DW_OP_lt: // 0x2d
782 case DW_OP_ne: // 0x2e
783 case DW_OP_lit0: // 0x30
784 case DW_OP_lit1: // 0x31
785 case DW_OP_lit2: // 0x32
786 case DW_OP_lit3: // 0x33
787 case DW_OP_lit4: // 0x34
788 case DW_OP_lit5: // 0x35
789 case DW_OP_lit6: // 0x36
790 case DW_OP_lit7: // 0x37
791 case DW_OP_lit8: // 0x38
792 case DW_OP_lit9: // 0x39
793 case DW_OP_lit10: // 0x3A
794 case DW_OP_lit11: // 0x3B
795 case DW_OP_lit12: // 0x3C
796 case DW_OP_lit13: // 0x3D
797 case DW_OP_lit14: // 0x3E
798 case DW_OP_lit15: // 0x3F
799 case DW_OP_lit16: // 0x40
800 case DW_OP_lit17: // 0x41
801 case DW_OP_lit18: // 0x42
802 case DW_OP_lit19: // 0x43
803 case DW_OP_lit20: // 0x44
804 case DW_OP_lit21: // 0x45
805 case DW_OP_lit22: // 0x46
806 case DW_OP_lit23: // 0x47
807 case DW_OP_lit24: // 0x48
808 case DW_OP_lit25: // 0x49
809 case DW_OP_lit26: // 0x4A
810 case DW_OP_lit27: // 0x4B
811 case DW_OP_lit28: // 0x4C
812 case DW_OP_lit29: // 0x4D
813 case DW_OP_lit30: // 0x4E
814 case DW_OP_lit31: // 0x4f
815 case DW_OP_reg0: // 0x50
816 case DW_OP_reg1: // 0x51
817 case DW_OP_reg2: // 0x52
818 case DW_OP_reg3: // 0x53
819 case DW_OP_reg4: // 0x54
820 case DW_OP_reg5: // 0x55
821 case DW_OP_reg6: // 0x56
822 case DW_OP_reg7: // 0x57
823 case DW_OP_reg8: // 0x58
824 case DW_OP_reg9: // 0x59
825 case DW_OP_reg10: // 0x5A
826 case DW_OP_reg11: // 0x5B
827 case DW_OP_reg12: // 0x5C
828 case DW_OP_reg13: // 0x5D
829 case DW_OP_reg14: // 0x5E
830 case DW_OP_reg15: // 0x5F
831 case DW_OP_reg16: // 0x60
832 case DW_OP_reg17: // 0x61
833 case DW_OP_reg18: // 0x62
834 case DW_OP_reg19: // 0x63
835 case DW_OP_reg20: // 0x64
836 case DW_OP_reg21: // 0x65
837 case DW_OP_reg22: // 0x66
838 case DW_OP_reg23: // 0x67
839 case DW_OP_reg24: // 0x68
840 case DW_OP_reg25: // 0x69
841 case DW_OP_reg26: // 0x6A
842 case DW_OP_reg27: // 0x6B
843 case DW_OP_reg28: // 0x6C
844 case DW_OP_reg29: // 0x6D
845 case DW_OP_reg30: // 0x6E
846 case DW_OP_reg31: // 0x6F
847 case DW_OP_nop: // 0x96
848 case DW_OP_push_object_address: // 0x97 DWARF3
849 case DW_OP_form_tls_address: // 0x9b DWARF3
850 case DW_OP_call_frame_cfa: // 0x9c DWARF3
851 case DW_OP_stack_value: // 0x9f DWARF4
852 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
855 // Opcodes with a single 1 byte arguments
856 case DW_OP_const1u: // 0x08 1 1-byte constant
857 case DW_OP_const1s: // 0x09 1 1-byte constant
858 case DW_OP_pick: // 0x15 1 1-byte stack index
859 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
860 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
863 // Opcodes with a single 2 byte arguments
864 case DW_OP_const2u: // 0x0a 1 2-byte constant
865 case DW_OP_const2s: // 0x0b 1 2-byte constant
866 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
867 case DW_OP_bra: // 0x28 1 signed 2-byte constant
868 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
871 // Opcodes with a single 4 byte arguments
872 case DW_OP_const4u: // 0x0c 1 4-byte constant
873 case DW_OP_const4s: // 0x0d 1 4-byte constant
874 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
877 // Opcodes with a single 8 byte arguments
878 case DW_OP_const8u: // 0x0e 1 8-byte constant
879 case DW_OP_const8s: // 0x0f 1 8-byte constant
882 // All opcodes that have a single ULEB (signed or unsigned) argument
883 case DW_OP_constu: // 0x10 1 ULEB128 constant
884 case DW_OP_consts: // 0x11 1 SLEB128 constant
885 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
886 case DW_OP_breg0: // 0x70 1 ULEB128 register
887 case DW_OP_breg1: // 0x71 1 ULEB128 register
888 case DW_OP_breg2: // 0x72 1 ULEB128 register
889 case DW_OP_breg3: // 0x73 1 ULEB128 register
890 case DW_OP_breg4: // 0x74 1 ULEB128 register
891 case DW_OP_breg5: // 0x75 1 ULEB128 register
892 case DW_OP_breg6: // 0x76 1 ULEB128 register
893 case DW_OP_breg7: // 0x77 1 ULEB128 register
894 case DW_OP_breg8: // 0x78 1 ULEB128 register
895 case DW_OP_breg9: // 0x79 1 ULEB128 register
896 case DW_OP_breg10: // 0x7a 1 ULEB128 register
897 case DW_OP_breg11: // 0x7b 1 ULEB128 register
898 case DW_OP_breg12: // 0x7c 1 ULEB128 register
899 case DW_OP_breg13: // 0x7d 1 ULEB128 register
900 case DW_OP_breg14: // 0x7e 1 ULEB128 register
901 case DW_OP_breg15: // 0x7f 1 ULEB128 register
902 case DW_OP_breg16: // 0x80 1 ULEB128 register
903 case DW_OP_breg17: // 0x81 1 ULEB128 register
904 case DW_OP_breg18: // 0x82 1 ULEB128 register
905 case DW_OP_breg19: // 0x83 1 ULEB128 register
906 case DW_OP_breg20: // 0x84 1 ULEB128 register
907 case DW_OP_breg21: // 0x85 1 ULEB128 register
908 case DW_OP_breg22: // 0x86 1 ULEB128 register
909 case DW_OP_breg23: // 0x87 1 ULEB128 register
910 case DW_OP_breg24: // 0x88 1 ULEB128 register
911 case DW_OP_breg25: // 0x89 1 ULEB128 register
912 case DW_OP_breg26: // 0x8a 1 ULEB128 register
913 case DW_OP_breg27: // 0x8b 1 ULEB128 register
914 case DW_OP_breg28: // 0x8c 1 ULEB128 register
915 case DW_OP_breg29: // 0x8d 1 ULEB128 register
916 case DW_OP_breg30: // 0x8e 1 ULEB128 register
917 case DW_OP_breg31: // 0x8f 1 ULEB128 register
918 case DW_OP_regx: // 0x90 1 ULEB128 register
919 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
920 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
921 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
922 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
923 data.Skip_LEB128(&offset);
924 return offset - data_offset;
926 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
927 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
928 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
929 data.Skip_LEB128(&offset);
930 data.Skip_LEB128(&offset);
931 return offset - data_offset;
933 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
936 uint64_t block_len = data.Skip_LEB128(&offset);
938 return offset - data_offset;
944 return LLDB_INVALID_OFFSET;
947 lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(uint32_t op_addr_idx,
950 if (IsLocationList())
951 return LLDB_INVALID_ADDRESS;
952 lldb::offset_t offset = 0;
953 uint32_t curr_op_addr_idx = 0;
954 while (m_data.ValidOffset(offset)) {
955 const uint8_t op = m_data.GetU8(&offset);
957 if (op == DW_OP_addr) {
958 const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
959 if (curr_op_addr_idx == op_addr_idx)
963 } else if (op == DW_OP_GNU_addr_index) {
964 uint64_t index = m_data.GetULEB128(&offset);
965 if (curr_op_addr_idx == op_addr_idx) {
971 return ReadAddressFromDebugAddrSection(m_dwarf_cu, index);
975 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
976 if (op_arg_size == LLDB_INVALID_OFFSET) {
980 offset += op_arg_size;
983 return LLDB_INVALID_ADDRESS;
986 bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) {
987 if (IsLocationList())
989 lldb::offset_t offset = 0;
990 while (m_data.ValidOffset(offset)) {
991 const uint8_t op = m_data.GetU8(&offset);
993 if (op == DW_OP_addr) {
994 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
995 // We have to make a copy of the data as we don't know if this data is
996 // from a read only memory mapped buffer, so we duplicate all of the data
997 // first, then modify it, and if all goes well, we then replace the data
998 // for this expression
1000 // So first we copy the data into a heap buffer
1001 std::unique_ptr<DataBufferHeap> head_data_ap(
1002 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1004 // Make en encoder so we can write the address into the buffer using the
1005 // correct byte order (endianness)
1006 DataEncoder encoder(head_data_ap->GetBytes(), head_data_ap->GetByteSize(),
1007 m_data.GetByteOrder(), addr_byte_size);
1009 // Replace the address in the new buffer
1010 if (encoder.PutMaxU64(offset, addr_byte_size, file_addr) == UINT32_MAX)
1013 // All went well, so now we can reset the data using a shared pointer to
1014 // the heap data so "m_data" will now correctly manage the heap data.
1015 m_data.SetData(DataBufferSP(head_data_ap.release()));
1018 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1019 if (op_arg_size == LLDB_INVALID_OFFSET)
1021 offset += op_arg_size;
1027 bool DWARFExpression::ContainsThreadLocalStorage() const {
1028 // We are assuming for now that any thread local variable will not have a
1029 // location list. This has been true for all thread local variables we have
1030 // seen so far produced by any compiler.
1031 if (IsLocationList())
1033 lldb::offset_t offset = 0;
1034 while (m_data.ValidOffset(offset)) {
1035 const uint8_t op = m_data.GetU8(&offset);
1037 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
1039 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1040 if (op_arg_size == LLDB_INVALID_OFFSET)
1043 offset += op_arg_size;
1047 bool DWARFExpression::LinkThreadLocalStorage(
1048 lldb::ModuleSP new_module_sp,
1049 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
1050 &link_address_callback) {
1051 // We are assuming for now that any thread local variable will not have a
1052 // location list. This has been true for all thread local variables we have
1053 // seen so far produced by any compiler.
1054 if (IsLocationList())
1057 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1058 // We have to make a copy of the data as we don't know if this data is from a
1059 // read only memory mapped buffer, so we duplicate all of the data first,
1060 // then modify it, and if all goes well, we then replace the data for this
1063 // So first we copy the data into a heap buffer
1064 std::shared_ptr<DataBufferHeap> heap_data_sp(
1065 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1067 // Make en encoder so we can write the address into the buffer using the
1068 // correct byte order (endianness)
1069 DataEncoder encoder(heap_data_sp->GetBytes(), heap_data_sp->GetByteSize(),
1070 m_data.GetByteOrder(), addr_byte_size);
1072 lldb::offset_t offset = 0;
1073 lldb::offset_t const_offset = 0;
1074 lldb::addr_t const_value = 0;
1075 size_t const_byte_size = 0;
1076 while (m_data.ValidOffset(offset)) {
1077 const uint8_t op = m_data.GetU8(&offset);
1079 bool decoded_data = false;
1082 // Remember the const offset in case we later have a
1083 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
1084 const_offset = offset;
1085 const_value = m_data.GetU32(&offset);
1086 decoded_data = true;
1087 const_byte_size = 4;
1091 // Remember the const offset in case we later have a
1092 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
1093 const_offset = offset;
1094 const_value = m_data.GetU64(&offset);
1095 decoded_data = true;
1096 const_byte_size = 8;
1099 case DW_OP_form_tls_address:
1100 case DW_OP_GNU_push_tls_address:
1101 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
1102 // by a file address on the stack. We assume that DW_OP_const4u or
1103 // DW_OP_const8u is used for these values, and we check that the last
1104 // opcode we got before either of these was DW_OP_const4u or
1105 // DW_OP_const8u. If so, then we can link the value accodingly. For
1106 // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
1107 // address of a structure that contains a function pointer, the pthread
1108 // key and the offset into the data pointed to by the pthread key. So we
1109 // must link this address and also set the module of this expression to
1110 // the new_module_sp so we can resolve the file address correctly
1111 if (const_byte_size > 0) {
1112 lldb::addr_t linked_file_addr = link_address_callback(const_value);
1113 if (linked_file_addr == LLDB_INVALID_ADDRESS)
1115 // Replace the address in the new buffer
1116 if (encoder.PutMaxU64(const_offset, const_byte_size,
1117 linked_file_addr) == UINT32_MAX)
1125 const_byte_size = 0;
1129 if (!decoded_data) {
1130 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1131 if (op_arg_size == LLDB_INVALID_OFFSET)
1134 offset += op_arg_size;
1138 // If we linked the TLS address correctly, update the module so that when the
1139 // expression is evaluated it can resolve the file address to a load address
1142 m_module_wp = new_module_sp;
1143 m_data.SetData(heap_data_sp);
1147 bool DWARFExpression::LocationListContainsAddress(
1148 lldb::addr_t loclist_base_addr, lldb::addr_t addr) const {
1149 if (addr == LLDB_INVALID_ADDRESS)
1152 if (IsLocationList()) {
1153 lldb::offset_t offset = 0;
1155 if (loclist_base_addr == LLDB_INVALID_ADDRESS)
1158 while (m_data.ValidOffset(offset)) {
1159 // We need to figure out what the value is for the location.
1160 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1161 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1162 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1166 if (lo_pc == 0 && hi_pc == 0)
1169 lo_pc += loclist_base_addr - m_loclist_slide;
1170 hi_pc += loclist_base_addr - m_loclist_slide;
1172 if (lo_pc <= addr && addr < hi_pc)
1175 offset += m_data.GetU16(&offset);
1181 bool DWARFExpression::GetLocation(addr_t base_addr, addr_t pc,
1182 lldb::offset_t &offset,
1183 lldb::offset_t &length) {
1185 if (!IsLocationList()) {
1186 length = m_data.GetByteSize();
1190 if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS) {
1191 addr_t curr_base_addr = base_addr;
1193 while (m_data.ValidOffset(offset)) {
1194 // We need to figure out what the value is for the location.
1195 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1196 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1197 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1201 if (lo_pc == 0 && hi_pc == 0)
1204 lo_pc += curr_base_addr - m_loclist_slide;
1205 hi_pc += curr_base_addr - m_loclist_slide;
1207 length = m_data.GetU16(&offset);
1209 if (length > 0 && lo_pc <= pc && pc < hi_pc)
1215 offset = LLDB_INVALID_OFFSET;
1220 bool DWARFExpression::DumpLocationForAddress(Stream *s,
1221 lldb::DescriptionLevel level,
1222 addr_t base_addr, addr_t address,
1224 lldb::offset_t offset = 0;
1225 lldb::offset_t length = 0;
1227 if (GetLocation(base_addr, address, offset, length)) {
1229 DumpLocation(s, offset, length, level, abi);
1236 bool DWARFExpression::Evaluate(ExecutionContextScope *exe_scope,
1237 lldb::addr_t loclist_base_load_addr,
1238 const Value *initial_value_ptr,
1239 const Value *object_address_ptr, Value &result,
1240 Status *error_ptr) const {
1241 ExecutionContext exe_ctx(exe_scope);
1242 return Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, initial_value_ptr,
1243 object_address_ptr, result, error_ptr);
1246 bool DWARFExpression::Evaluate(ExecutionContext *exe_ctx,
1247 RegisterContext *reg_ctx,
1248 lldb::addr_t loclist_base_load_addr,
1249 const Value *initial_value_ptr,
1250 const Value *object_address_ptr, Value &result,
1251 Status *error_ptr) const {
1252 ModuleSP module_sp = m_module_wp.lock();
1254 if (IsLocationList()) {
1255 lldb::offset_t offset = 0;
1257 StackFrame *frame = NULL;
1259 pc = reg_ctx->GetPC();
1261 frame = exe_ctx->GetFramePtr();
1264 RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
1267 pc = reg_ctx_sp->GetPC();
1270 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS) {
1271 if (pc == LLDB_INVALID_ADDRESS) {
1273 error_ptr->SetErrorString("Invalid PC in frame.");
1277 addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
1279 while (m_data.ValidOffset(offset)) {
1280 // We need to figure out what the value is for the location.
1281 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1282 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1283 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
1287 if (lo_pc == 0 && hi_pc == 0)
1290 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
1291 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
1293 uint16_t length = m_data.GetU16(&offset);
1295 if (length > 0 && lo_pc <= pc && pc < hi_pc) {
1296 return DWARFExpression::Evaluate(
1297 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, offset, length,
1298 m_reg_kind, initial_value_ptr, object_address_ptr, result,
1305 error_ptr->SetErrorString("variable not available");
1309 // Not a location list, just a single expression.
1310 return DWARFExpression::Evaluate(
1311 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, 0, m_data.GetByteSize(),
1312 m_reg_kind, initial_value_ptr, object_address_ptr, result, error_ptr);
1315 bool DWARFExpression::Evaluate(
1316 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
1317 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
1318 DWARFUnit *dwarf_cu, const lldb::offset_t opcodes_offset,
1319 const lldb::offset_t opcodes_length, const lldb::RegisterKind reg_kind,
1320 const Value *initial_value_ptr, const Value *object_address_ptr,
1321 Value &result, Status *error_ptr) {
1323 if (opcodes_length == 0) {
1325 error_ptr->SetErrorString(
1326 "no location, value may have been optimized out");
1329 std::vector<Value> stack;
1331 Process *process = NULL;
1332 StackFrame *frame = NULL;
1335 process = exe_ctx->GetProcessPtr();
1336 frame = exe_ctx->GetFramePtr();
1338 if (reg_ctx == NULL && frame)
1339 reg_ctx = frame->GetRegisterContext().get();
1341 if (initial_value_ptr)
1342 stack.push_back(*initial_value_ptr);
1344 lldb::offset_t offset = opcodes_offset;
1345 const lldb::offset_t end_offset = opcodes_offset + opcodes_length;
1349 /// Insertion point for evaluating multi-piece expression.
\13
1350 uint64_t op_piece_offset = 0;
1351 Value pieces; // Used for DW_OP_piece
1353 // Make sure all of the data is available in opcodes.
1354 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length)) {
1356 error_ptr->SetErrorString(
1357 "invalid offset and/or length for opcodes buffer.");
1360 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1362 while (opcodes.ValidOffset(offset) && offset < end_offset) {
1363 const lldb::offset_t op_offset = offset;
1364 const uint8_t op = opcodes.GetU8(&offset);
1366 if (log && log->GetVerbose()) {
1367 size_t count = stack.size();
1368 log->Printf("Stack before operation has %" PRIu64 " values:",
1370 for (size_t i = 0; i < count; ++i) {
1371 StreamString new_value;
1372 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1373 stack[i].Dump(&new_value);
1374 log->Printf(" %s", new_value.GetData());
1376 log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
1380 //----------------------------------------------------------------------
1381 // The DW_OP_addr operation has a single operand that encodes a machine
1382 // address and whose size is the size of an address on the target machine.
1383 //----------------------------------------------------------------------
1385 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1386 stack.back().SetValueType(Value::eValueTypeFileAddress);
1387 // Convert the file address to a load address, so subsequent
1388 // DWARF operators can operate on it.
1390 stack.back().ConvertToLoadAddress(module_sp.get(),
1391 frame->CalculateTarget().get());
1394 //----------------------------------------------------------------------
1395 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1396 // shared libraries that have a location like:
1397 // DW_OP_addr(0x1000)
1398 // If this address resides in a shared library, then this virtual address
1399 // won't make sense when it is evaluated in the context of a running
1400 // process where shared libraries have been slid. To account for this, this
1401 // new address type where we can store the section pointer and a 4 byte
1403 //----------------------------------------------------------------------
1404 // case DW_OP_addr_sect_offset4:
1406 // result_type = eResultTypeFileAddress;
1407 // lldb::Section *sect = (lldb::Section
1408 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
1409 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1411 // Address so_addr (sect, sect_offset);
1412 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
1413 // if (load_addr != LLDB_INVALID_ADDRESS)
1415 // // We successfully resolve a file address to a load
1417 // stack.push_back(load_addr);
1424 // error_ptr->SetErrorStringWithFormat ("Section %s in
1425 // %s is not currently loaded.\n",
1426 // sect->GetName().AsCString(),
1427 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1433 //----------------------------------------------------------------------
1434 // OPCODE: DW_OP_deref
1436 // DESCRIPTION: Pops the top stack entry and treats it as an address.
1437 // The value retrieved from that address is pushed. The size of the data
1438 // retrieved from the dereferenced address is the size of an address on the
1440 //----------------------------------------------------------------------
1442 if (stack.empty()) {
1444 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1447 Value::ValueType value_type = stack.back().GetValueType();
1448 switch (value_type) {
1449 case Value::eValueTypeHostAddress: {
1450 void *src = (void *)stack.back().GetScalar().ULongLong();
1452 ::memcpy(&ptr, src, sizeof(void *));
1453 stack.back().GetScalar() = ptr;
1454 stack.back().ClearContext();
1456 case Value::eValueTypeFileAddress: {
1457 auto file_addr = stack.back().GetScalar().ULongLong(
1458 LLDB_INVALID_ADDRESS);
1461 error_ptr->SetErrorStringWithFormat(
1462 "need module to resolve file address for DW_OP_deref");
1466 if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
1468 error_ptr->SetErrorStringWithFormat(
1469 "failed to resolve file address in module");
1472 addr_t load_Addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
1473 if (load_Addr == LLDB_INVALID_ADDRESS) {
1475 error_ptr->SetErrorStringWithFormat(
1476 "failed to resolve load address");
1479 stack.back().GetScalar() = load_Addr;
1480 stack.back().SetValueType(Value::eValueTypeLoadAddress);
1481 // Fall through to load address code below...
1483 case Value::eValueTypeLoadAddress:
1486 lldb::addr_t pointer_addr =
1487 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1489 lldb::addr_t pointer_value =
1490 process->ReadPointerFromMemory(pointer_addr, error);
1491 if (pointer_value != LLDB_INVALID_ADDRESS) {
1492 stack.back().GetScalar() = pointer_value;
1493 stack.back().ClearContext();
1496 error_ptr->SetErrorStringWithFormat(
1497 "Failed to dereference pointer from 0x%" PRIx64
1498 " for DW_OP_deref: %s\n",
1499 pointer_addr, error.AsCString());
1504 error_ptr->SetErrorStringWithFormat(
1505 "NULL process for DW_OP_deref.\n");
1510 error_ptr->SetErrorStringWithFormat(
1511 "NULL execution context for DW_OP_deref.\n");
1522 //----------------------------------------------------------------------
1523 // OPCODE: DW_OP_deref_size
1525 // 1 - uint8_t that specifies the size of the data to dereference.
1526 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1527 // stack entry and treats it as an address. The value retrieved from that
1528 // address is pushed. In the DW_OP_deref_size operation, however, the size
1529 // in bytes of the data retrieved from the dereferenced address is
1530 // specified by the single operand. This operand is a 1-byte unsigned
1531 // integral constant whose value may not be larger than the size of an
1532 // address on the target machine. The data retrieved is zero extended to
1533 // the size of an address on the target machine before being pushed on the
1534 // expression stack.
1535 //----------------------------------------------------------------------
1536 case DW_OP_deref_size: {
1537 if (stack.empty()) {
1539 error_ptr->SetErrorString(
1540 "Expression stack empty for DW_OP_deref_size.");
1543 uint8_t size = opcodes.GetU8(&offset);
1544 Value::ValueType value_type = stack.back().GetValueType();
1545 switch (value_type) {
1546 case Value::eValueTypeHostAddress: {
1547 void *src = (void *)stack.back().GetScalar().ULongLong();
1549 ::memcpy(&ptr, src, sizeof(void *));
1550 // I can't decide whether the size operand should apply to the bytes in
1552 // lldb-host endianness or the target endianness.. I doubt this'll ever
1553 // come up but I'll opt for assuming big endian regardless.
1562 ptr = ptr & 0xffffff;
1565 ptr = ptr & 0xffffffff;
1567 // the casts are added to work around the case where intptr_t is a 32
1569 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1572 ptr = (intptr_t)ptr & 0xffffffffffULL;
1575 ptr = (intptr_t)ptr & 0xffffffffffffULL;
1578 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1583 stack.back().GetScalar() = ptr;
1584 stack.back().ClearContext();
1586 case Value::eValueTypeLoadAddress:
1589 lldb::addr_t pointer_addr =
1590 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1591 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1593 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1595 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
1596 process->GetByteOrder(), size);
1597 lldb::offset_t addr_data_offset = 0;
1600 stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
1603 stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
1606 stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
1609 stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
1612 stack.back().GetScalar() =
1613 addr_data.GetPointer(&addr_data_offset);
1615 stack.back().ClearContext();
1618 error_ptr->SetErrorStringWithFormat(
1619 "Failed to dereference pointer from 0x%" PRIx64
1620 " for DW_OP_deref: %s\n",
1621 pointer_addr, error.AsCString());
1626 error_ptr->SetErrorStringWithFormat(
1627 "NULL process for DW_OP_deref.\n");
1632 error_ptr->SetErrorStringWithFormat(
1633 "NULL execution context for DW_OP_deref.\n");
1644 //----------------------------------------------------------------------
1645 // OPCODE: DW_OP_xderef_size
1647 // 1 - uint8_t that specifies the size of the data to dereference.
1648 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1649 // the top of the stack is treated as an address. The second stack entry is
1650 // treated as an "address space identifier" for those architectures that
1651 // support multiple address spaces. The top two stack elements are popped,
1652 // a data item is retrieved through an implementation-defined address
1653 // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1654 // operation, however, the size in bytes of the data retrieved from the
1655 // dereferenced address is specified by the single operand. This operand is
1656 // a 1-byte unsigned integral constant whose value may not be larger than
1657 // the size of an address on the target machine. The data retrieved is zero
1658 // extended to the size of an address on the target machine before being
1659 // pushed on the expression stack.
1660 //----------------------------------------------------------------------
1661 case DW_OP_xderef_size:
1663 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1665 //----------------------------------------------------------------------
1666 // OPCODE: DW_OP_xderef
1668 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1669 // the top of the stack is treated as an address. The second stack entry is
1670 // treated as an "address space identifier" for those architectures that
1671 // support multiple address spaces. The top two stack elements are popped,
1672 // a data item is retrieved through an implementation-defined address
1673 // calculation and pushed as the new stack top. The size of the data
1674 // retrieved from the dereferenced address is the size of an address on the
1676 //----------------------------------------------------------------------
1679 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1682 //----------------------------------------------------------------------
1683 // All DW_OP_constXXX opcodes have a single operand as noted below:
1686 // --------------- ----------------------------------------------------
1687 // DW_OP_const1u 1-byte unsigned integer constant DW_OP_const1s
1688 // 1-byte signed integer constant DW_OP_const2u 2-byte unsigned integer
1689 // constant DW_OP_const2s 2-byte signed integer constant DW_OP_const4u
1690 // 4-byte unsigned integer constant DW_OP_const4s 4-byte signed integer
1691 // constant DW_OP_const8u 8-byte unsigned integer constant DW_OP_const8s
1692 // 8-byte signed integer constant DW_OP_constu unsigned LEB128 integer
1693 // constant DW_OP_consts signed LEB128 integer constant
1694 //----------------------------------------------------------------------
1696 stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
1699 stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
1702 stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
1705 stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
1708 stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
1711 stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
1714 stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
1717 stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
1720 stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1723 stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1726 //----------------------------------------------------------------------
1727 // OPCODE: DW_OP_dup
1729 // DESCRIPTION: duplicates the value at the top of the stack
1730 //----------------------------------------------------------------------
1732 if (stack.empty()) {
1734 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1737 stack.push_back(stack.back());
1740 //----------------------------------------------------------------------
1741 // OPCODE: DW_OP_drop
1743 // DESCRIPTION: pops the value at the top of the stack
1744 //----------------------------------------------------------------------
1746 if (stack.empty()) {
1748 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1754 //----------------------------------------------------------------------
1755 // OPCODE: DW_OP_over
1757 // DESCRIPTION: Duplicates the entry currently second in the stack at
1758 // the top of the stack.
1759 //----------------------------------------------------------------------
1761 if (stack.size() < 2) {
1763 error_ptr->SetErrorString(
1764 "Expression stack needs at least 2 items for DW_OP_over.");
1767 stack.push_back(stack[stack.size() - 2]);
1770 //----------------------------------------------------------------------
1771 // OPCODE: DW_OP_pick
1772 // OPERANDS: uint8_t index into the current stack
1773 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1774 // inclusive) is pushed on the stack
1775 //----------------------------------------------------------------------
1777 uint8_t pick_idx = opcodes.GetU8(&offset);
1778 if (pick_idx < stack.size())
1779 stack.push_back(stack[pick_idx]);
1782 error_ptr->SetErrorStringWithFormat(
1783 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1788 //----------------------------------------------------------------------
1789 // OPCODE: DW_OP_swap
1791 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1792 // of the stack becomes the second stack entry, and the second entry
1793 // becomes the top of the stack
1794 //----------------------------------------------------------------------
1796 if (stack.size() < 2) {
1798 error_ptr->SetErrorString(
1799 "Expression stack needs at least 2 items for DW_OP_swap.");
1803 stack.back() = stack[stack.size() - 2];
1804 stack[stack.size() - 2] = tmp;
1808 //----------------------------------------------------------------------
1809 // OPCODE: DW_OP_rot
1811 // DESCRIPTION: Rotates the first three stack entries. The entry at
1812 // the top of the stack becomes the third stack entry, the second entry
1813 // becomes the top of the stack, and the third entry becomes the second
1815 //----------------------------------------------------------------------
1817 if (stack.size() < 3) {
1819 error_ptr->SetErrorString(
1820 "Expression stack needs at least 3 items for DW_OP_rot.");
1823 size_t last_idx = stack.size() - 1;
1824 Value old_top = stack[last_idx];
1825 stack[last_idx] = stack[last_idx - 1];
1826 stack[last_idx - 1] = stack[last_idx - 2];
1827 stack[last_idx - 2] = old_top;
1831 //----------------------------------------------------------------------
1832 // OPCODE: DW_OP_abs
1834 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1835 // value and pushes its absolute value. If the absolute value can not be
1836 // represented, the result is undefined.
1837 //----------------------------------------------------------------------
1839 if (stack.empty()) {
1841 error_ptr->SetErrorString(
1842 "Expression stack needs at least 1 item for DW_OP_abs.");
1844 } else if (stack.back().ResolveValue(exe_ctx).AbsoluteValue() == false) {
1846 error_ptr->SetErrorString(
1847 "Failed to take the absolute value of the first stack item.");
1852 //----------------------------------------------------------------------
1853 // OPCODE: DW_OP_and
1855 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1856 // operation on the two, and pushes the result.
1857 //----------------------------------------------------------------------
1859 if (stack.size() < 2) {
1861 error_ptr->SetErrorString(
1862 "Expression stack needs at least 2 items for DW_OP_and.");
1867 stack.back().ResolveValue(exe_ctx) =
1868 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1872 //----------------------------------------------------------------------
1873 // OPCODE: DW_OP_div
1875 // DESCRIPTION: pops the top two stack values, divides the former second
1876 // entry by the former top of the stack using signed division, and pushes
1878 //----------------------------------------------------------------------
1880 if (stack.size() < 2) {
1882 error_ptr->SetErrorString(
1883 "Expression stack needs at least 2 items for DW_OP_div.");
1887 if (tmp.ResolveValue(exe_ctx).IsZero()) {
1889 error_ptr->SetErrorString("Divide by zero.");
1894 stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1895 if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1897 error_ptr->SetErrorString("Divide failed.");
1904 //----------------------------------------------------------------------
1905 // OPCODE: DW_OP_minus
1907 // DESCRIPTION: pops the top two stack values, subtracts the former top
1908 // of the stack from the former second entry, and pushes the result.
1909 //----------------------------------------------------------------------
1911 if (stack.size() < 2) {
1913 error_ptr->SetErrorString(
1914 "Expression stack needs at least 2 items for DW_OP_minus.");
1919 stack.back().ResolveValue(exe_ctx) =
1920 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1924 //----------------------------------------------------------------------
1925 // OPCODE: DW_OP_mod
1927 // DESCRIPTION: pops the top two stack values and pushes the result of
1928 // the calculation: former second stack entry modulo the former top of the
1930 //----------------------------------------------------------------------
1932 if (stack.size() < 2) {
1934 error_ptr->SetErrorString(
1935 "Expression stack needs at least 2 items for DW_OP_mod.");
1940 stack.back().ResolveValue(exe_ctx) =
1941 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1945 //----------------------------------------------------------------------
1946 // OPCODE: DW_OP_mul
1948 // DESCRIPTION: pops the top two stack entries, multiplies them
1949 // together, and pushes the result.
1950 //----------------------------------------------------------------------
1952 if (stack.size() < 2) {
1954 error_ptr->SetErrorString(
1955 "Expression stack needs at least 2 items for DW_OP_mul.");
1960 stack.back().ResolveValue(exe_ctx) =
1961 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1965 //----------------------------------------------------------------------
1966 // OPCODE: DW_OP_neg
1968 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1969 //----------------------------------------------------------------------
1971 if (stack.empty()) {
1973 error_ptr->SetErrorString(
1974 "Expression stack needs at least 1 item for DW_OP_neg.");
1977 if (stack.back().ResolveValue(exe_ctx).UnaryNegate() == false) {
1979 error_ptr->SetErrorString("Unary negate failed.");
1985 //----------------------------------------------------------------------
1986 // OPCODE: DW_OP_not
1988 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1990 //----------------------------------------------------------------------
1992 if (stack.empty()) {
1994 error_ptr->SetErrorString(
1995 "Expression stack needs at least 1 item for DW_OP_not.");
1998 if (stack.back().ResolveValue(exe_ctx).OnesComplement() == false) {
2000 error_ptr->SetErrorString("Logical NOT failed.");
2006 //----------------------------------------------------------------------
2009 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
2010 // operation on the two, and pushes the result.
2011 //----------------------------------------------------------------------
2013 if (stack.size() < 2) {
2015 error_ptr->SetErrorString(
2016 "Expression stack needs at least 2 items for DW_OP_or.");
2021 stack.back().ResolveValue(exe_ctx) =
2022 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
2026 //----------------------------------------------------------------------
2027 // OPCODE: DW_OP_plus
2029 // DESCRIPTION: pops the top two stack entries, adds them together, and
2030 // pushes the result.
2031 //----------------------------------------------------------------------
2033 if (stack.size() < 2) {
2035 error_ptr->SetErrorString(
2036 "Expression stack needs at least 2 items for DW_OP_plus.");
2041 stack.back().GetScalar() += tmp.GetScalar();
2045 //----------------------------------------------------------------------
2046 // OPCODE: DW_OP_plus_uconst
2048 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
2049 // constant operand and pushes the result.
2050 //----------------------------------------------------------------------
2051 case DW_OP_plus_uconst:
2052 if (stack.empty()) {
2054 error_ptr->SetErrorString(
2055 "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
2058 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
2059 // Implicit conversion from a UINT to a Scalar...
2060 stack.back().GetScalar() += uconst_value;
2061 if (!stack.back().GetScalar().IsValid()) {
2063 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
2069 //----------------------------------------------------------------------
2070 // OPCODE: DW_OP_shl
2072 // DESCRIPTION: pops the top two stack entries, shifts the former
2073 // second entry left by the number of bits specified by the former top of
2074 // the stack, and pushes the result.
2075 //----------------------------------------------------------------------
2077 if (stack.size() < 2) {
2079 error_ptr->SetErrorString(
2080 "Expression stack needs at least 2 items for DW_OP_shl.");
2085 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
2089 //----------------------------------------------------------------------
2090 // OPCODE: DW_OP_shr
2092 // DESCRIPTION: pops the top two stack entries, shifts the former second
2093 // entry right logically (filling with zero bits) by the number of bits
2094 // specified by the former top of the stack, and pushes the result.
2095 //----------------------------------------------------------------------
2097 if (stack.size() < 2) {
2099 error_ptr->SetErrorString(
2100 "Expression stack needs at least 2 items for DW_OP_shr.");
2105 if (stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
2106 tmp.ResolveValue(exe_ctx)) == false) {
2108 error_ptr->SetErrorString("DW_OP_shr failed.");
2114 //----------------------------------------------------------------------
2115 // OPCODE: DW_OP_shra
2117 // DESCRIPTION: pops the top two stack entries, shifts the former second
2118 // entry right arithmetically (divide the magnitude by 2, keep the same
2119 // sign for the result) by the number of bits specified by the former top
2120 // of the stack, and pushes the result.
2121 //----------------------------------------------------------------------
2123 if (stack.size() < 2) {
2125 error_ptr->SetErrorString(
2126 "Expression stack needs at least 2 items for DW_OP_shra.");
2131 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
2135 //----------------------------------------------------------------------
2136 // OPCODE: DW_OP_xor
2138 // DESCRIPTION: pops the top two stack entries, performs the bitwise
2139 // exclusive-or operation on the two, and pushes the result.
2140 //----------------------------------------------------------------------
2142 if (stack.size() < 2) {
2144 error_ptr->SetErrorString(
2145 "Expression stack needs at least 2 items for DW_OP_xor.");
2150 stack.back().ResolveValue(exe_ctx) =
2151 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
2155 //----------------------------------------------------------------------
2156 // OPCODE: DW_OP_skip
2157 // OPERANDS: int16_t
2158 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
2159 // signed integer constant. The 2-byte constant is the number of bytes of
2160 // the DWARF expression to skip forward or backward from the current
2161 // operation, beginning after the 2-byte constant.
2162 //----------------------------------------------------------------------
2164 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
2165 lldb::offset_t new_offset = offset + skip_offset;
2166 if (new_offset >= opcodes_offset && new_offset < end_offset)
2167 offset = new_offset;
2170 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
2175 //----------------------------------------------------------------------
2176 // OPCODE: DW_OP_bra
2177 // OPERANDS: int16_t
2178 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
2179 // signed integer constant. This operation pops the top of stack. If the
2180 // value popped is not the constant 0, the 2-byte constant operand is the
2181 // number of bytes of the DWARF expression to skip forward or backward from
2182 // the current operation, beginning after the 2-byte constant.
2183 //----------------------------------------------------------------------
2185 if (stack.empty()) {
2187 error_ptr->SetErrorString(
2188 "Expression stack needs at least 1 item for DW_OP_bra.");
2193 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
2195 if (tmp.ResolveValue(exe_ctx) != zero) {
2196 lldb::offset_t new_offset = offset + bra_offset;
2197 if (new_offset >= opcodes_offset && new_offset < end_offset)
2198 offset = new_offset;
2201 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
2208 //----------------------------------------------------------------------
2211 // DESCRIPTION: pops the top two stack values, compares using the
2212 // equals (==) operator.
2213 // STACK RESULT: push the constant value 1 onto the stack if the result
2214 // of the operation is true or the constant value 0 if the result of the
2215 // operation is false.
2216 //----------------------------------------------------------------------
2218 if (stack.size() < 2) {
2220 error_ptr->SetErrorString(
2221 "Expression stack needs at least 2 items for DW_OP_eq.");
2226 stack.back().ResolveValue(exe_ctx) =
2227 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
2231 //----------------------------------------------------------------------
2234 // DESCRIPTION: pops the top two stack values, compares using the
2235 // greater than or equal to (>=) operator.
2236 // STACK RESULT: push the constant value 1 onto the stack if the result
2237 // of the operation is true or the constant value 0 if the result of the
2238 // operation is false.
2239 //----------------------------------------------------------------------
2241 if (stack.size() < 2) {
2243 error_ptr->SetErrorString(
2244 "Expression stack needs at least 2 items for DW_OP_ge.");
2249 stack.back().ResolveValue(exe_ctx) =
2250 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
2254 //----------------------------------------------------------------------
2257 // DESCRIPTION: pops the top two stack values, compares using the
2258 // greater than (>) operator.
2259 // STACK RESULT: push the constant value 1 onto the stack if the result
2260 // of the operation is true or the constant value 0 if the result of the
2261 // operation is false.
2262 //----------------------------------------------------------------------
2264 if (stack.size() < 2) {
2266 error_ptr->SetErrorString(
2267 "Expression stack needs at least 2 items for DW_OP_gt.");
2272 stack.back().ResolveValue(exe_ctx) =
2273 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
2277 //----------------------------------------------------------------------
2280 // DESCRIPTION: pops the top two stack values, compares using the
2281 // less than or equal to (<=) operator.
2282 // STACK RESULT: push the constant value 1 onto the stack if the result
2283 // of the operation is true or the constant value 0 if the result of the
2284 // operation is false.
2285 //----------------------------------------------------------------------
2287 if (stack.size() < 2) {
2289 error_ptr->SetErrorString(
2290 "Expression stack needs at least 2 items for DW_OP_le.");
2295 stack.back().ResolveValue(exe_ctx) =
2296 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
2300 //----------------------------------------------------------------------
2303 // DESCRIPTION: pops the top two stack values, compares using the
2304 // less than (<) operator.
2305 // STACK RESULT: push the constant value 1 onto the stack if the result
2306 // of the operation is true or the constant value 0 if the result of the
2307 // operation is false.
2308 //----------------------------------------------------------------------
2310 if (stack.size() < 2) {
2312 error_ptr->SetErrorString(
2313 "Expression stack needs at least 2 items for DW_OP_lt.");
2318 stack.back().ResolveValue(exe_ctx) =
2319 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
2323 //----------------------------------------------------------------------
2326 // DESCRIPTION: pops the top two stack values, compares using the
2327 // not equal (!=) operator.
2328 // STACK RESULT: push the constant value 1 onto the stack if the result
2329 // of the operation is true or the constant value 0 if the result of the
2330 // operation is false.
2331 //----------------------------------------------------------------------
2333 if (stack.size() < 2) {
2335 error_ptr->SetErrorString(
2336 "Expression stack needs at least 2 items for DW_OP_ne.");
2341 stack.back().ResolveValue(exe_ctx) =
2342 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
2346 //----------------------------------------------------------------------
2347 // OPCODE: DW_OP_litn
2349 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
2350 // STACK RESULT: push the unsigned literal constant value onto the top
2352 //----------------------------------------------------------------------
2385 stack.push_back(Scalar(op - DW_OP_lit0));
2388 //----------------------------------------------------------------------
2389 // OPCODE: DW_OP_regN
2391 // DESCRIPTION: Push the value in register n on the top of the stack.
2392 //----------------------------------------------------------------------
2425 reg_num = op - DW_OP_reg0;
2427 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2428 stack.push_back(tmp);
2432 //----------------------------------------------------------------------
2433 // OPCODE: DW_OP_regx
2435 // ULEB128 literal operand that encodes the register.
2436 // DESCRIPTION: Push the value in register on the top of the stack.
2437 //----------------------------------------------------------------------
2439 reg_num = opcodes.GetULEB128(&offset);
2440 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2441 stack.push_back(tmp);
2446 //----------------------------------------------------------------------
2447 // OPCODE: DW_OP_bregN
2449 // SLEB128 offset from register N
2450 // DESCRIPTION: Value is in memory at the address specified by register
2451 // N plus an offset.
2452 //----------------------------------------------------------------------
2484 case DW_OP_breg31: {
2485 reg_num = op - DW_OP_breg0;
2487 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2489 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2490 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2492 stack.push_back(tmp);
2493 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2497 //----------------------------------------------------------------------
2498 // OPCODE: DW_OP_bregx
2500 // ULEB128 literal operand that encodes the register.
2501 // SLEB128 offset from register N
2502 // DESCRIPTION: Value is in memory at the address specified by register
2503 // N plus an offset.
2504 //----------------------------------------------------------------------
2506 reg_num = opcodes.GetULEB128(&offset);
2508 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2510 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2511 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2513 stack.push_back(tmp);
2514 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2523 if (frame->GetFrameBaseValue(value, error_ptr)) {
2524 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2525 value += fbreg_offset;
2526 stack.push_back(value);
2527 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2532 error_ptr->SetErrorString(
2533 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2538 error_ptr->SetErrorStringWithFormat(
2539 "NULL execution context for DW_OP_fbreg.\n");
2545 //----------------------------------------------------------------------
2546 // OPCODE: DW_OP_nop
2548 // DESCRIPTION: A place holder. It has no effect on the location stack
2549 // or any of its values.
2550 //----------------------------------------------------------------------
2554 //----------------------------------------------------------------------
2555 // OPCODE: DW_OP_piece
2557 // ULEB128: byte size of the piece
2558 // DESCRIPTION: The operand describes the size in bytes of the piece of
2559 // the object referenced by the DWARF expression whose result is at the top
2560 // of the stack. If the piece is located in a register, but does not occupy
2561 // the entire register, the placement of the piece within that register is
2562 // defined by the ABI.
2564 // Many compilers store a single variable in sets of registers, or store a
2565 // variable partially in memory and partially in registers. DW_OP_piece
2566 // provides a way of describing how large a part of a variable a particular
2567 // DWARF expression refers to.
2568 //----------------------------------------------------------------------
2570 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2572 if (piece_byte_size > 0) {
2575 if (stack.empty()) {
2576 // In a multi-piece expression, this means that the current piece is
2577 // not available. Fill with zeros for now by resizing the data and
2579 curr_piece.ResizeData(piece_byte_size);
2580 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2581 pieces.AppendDataToHostBuffer(curr_piece);
2584 // Extract the current piece into "curr_piece"
2585 Value curr_piece_source_value(stack.back());
2588 const Value::ValueType curr_piece_source_value_type =
2589 curr_piece_source_value.GetValueType();
2590 switch (curr_piece_source_value_type) {
2591 case Value::eValueTypeLoadAddress:
2593 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2594 lldb::addr_t load_addr =
2595 curr_piece_source_value.GetScalar().ULongLong(
2596 LLDB_INVALID_ADDRESS);
2597 if (process->ReadMemory(
2598 load_addr, curr_piece.GetBuffer().GetBytes(),
2599 piece_byte_size, error) != piece_byte_size) {
2601 error_ptr->SetErrorStringWithFormat(
2602 "failed to read memory DW_OP_piece(%" PRIu64
2603 ") from 0x%" PRIx64,
2604 piece_byte_size, load_addr);
2609 error_ptr->SetErrorStringWithFormat(
2610 "failed to resize the piece memory buffer for "
2611 "DW_OP_piece(%" PRIu64 ")",
2618 case Value::eValueTypeFileAddress:
2619 case Value::eValueTypeHostAddress:
2621 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2622 LLDB_INVALID_ADDRESS);
2623 error_ptr->SetErrorStringWithFormat(
2624 "failed to read memory DW_OP_piece(%" PRIu64
2625 ") from %s address 0x%" PRIx64,
2626 piece_byte_size, curr_piece_source_value.GetValueType() ==
2627 Value::eValueTypeFileAddress
2634 case Value::eValueTypeScalar: {
2635 uint32_t bit_size = piece_byte_size * 8;
2636 uint32_t bit_offset = 0;
2637 if (!curr_piece_source_value.GetScalar().ExtractBitfield(
2638 bit_size, bit_offset)) {
2640 error_ptr->SetErrorStringWithFormat(
2641 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2642 " byte scalar value.",
2644 (uint64_t)curr_piece_source_value.GetScalar()
2648 curr_piece = curr_piece_source_value;
2651 case Value::eValueTypeVector: {
2652 if (curr_piece_source_value.GetVector().length >= piece_byte_size)
2653 curr_piece_source_value.GetVector().length = piece_byte_size;
2656 error_ptr->SetErrorStringWithFormat(
2657 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2658 " byte vector value.",
2660 (uint64_t)curr_piece_source_value.GetVector().length);
2666 // Check if this is the first piece?
2667 if (op_piece_offset == 0) {
2668 // This is the first piece, we should push it back onto the stack
2669 // so subsequent pieces will be able to access this piece and add
2671 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2673 error_ptr->SetErrorString("failed to append piece data");
2677 // If this is the second or later piece there should be a value on
2679 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2681 error_ptr->SetErrorStringWithFormat(
2682 "DW_OP_piece for offset %" PRIu64
2683 " but top of stack is of size %" PRIu64,
2684 op_piece_offset, pieces.GetBuffer().GetByteSize());
2688 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2690 error_ptr->SetErrorString("failed to append piece data");
2694 op_piece_offset += piece_byte_size;
2699 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2700 if (stack.size() < 1) {
2702 error_ptr->SetErrorString(
2703 "Expression stack needs at least 1 item for DW_OP_bit_piece.");
2706 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2707 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2708 switch (stack.back().GetValueType()) {
2709 case Value::eValueTypeScalar: {
2710 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2711 piece_bit_offset)) {
2713 error_ptr->SetErrorStringWithFormat(
2714 "unable to extract %" PRIu64 " bit value with %" PRIu64
2715 " bit offset from a %" PRIu64 " bit scalar value.",
2716 piece_bit_size, piece_bit_offset,
2717 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2722 case Value::eValueTypeFileAddress:
2723 case Value::eValueTypeLoadAddress:
2724 case Value::eValueTypeHostAddress:
2726 error_ptr->SetErrorStringWithFormat(
2727 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2728 ", bit_offset = %" PRIu64 ") from an address value.",
2729 piece_bit_size, piece_bit_offset);
2733 case Value::eValueTypeVector:
2735 error_ptr->SetErrorStringWithFormat(
2736 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2737 ", bit_offset = %" PRIu64 ") from a vector value.",
2738 piece_bit_size, piece_bit_offset);
2745 //----------------------------------------------------------------------
2746 // OPCODE: DW_OP_push_object_address
2748 // DESCRIPTION: Pushes the address of the object currently being
2749 // evaluated as part of evaluation of a user presented expression. This
2750 // object may correspond to an independent variable described by its own
2751 // DIE or it may be a component of an array, structure, or class whose
2752 // address has been dynamically determined by an earlier step during user
2753 // expression evaluation.
2754 //----------------------------------------------------------------------
2755 case DW_OP_push_object_address:
2756 if (object_address_ptr)
2757 stack.push_back(*object_address_ptr);
2760 error_ptr->SetErrorString("DW_OP_push_object_address used without "
2761 "specifying an object address");
2766 //----------------------------------------------------------------------
2767 // OPCODE: DW_OP_call2
2769 // uint16_t compile unit relative offset of a DIE
2770 // DESCRIPTION: Performs subroutine calls during evaluation
2771 // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2772 // debugging information entry in the current compilation unit.
2774 // Operand interpretation is exactly like that for DW_FORM_ref2.
2776 // This operation transfers control of DWARF expression evaluation to the
2777 // DW_AT_location attribute of the referenced DIE. If there is no such
2778 // attribute, then there is no effect. Execution of the DWARF expression of
2779 // a DW_AT_location attribute may add to and/or remove from values on the
2780 // stack. Execution returns to the point following the call when the end of
2781 // the attribute is reached. Values on the stack at the time of the call
2782 // may be used as parameters by the called expression and values left on
2783 // the stack by the called expression may be used as return values by prior
2784 // agreement between the calling and called expressions.
2785 //----------------------------------------------------------------------
2788 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2790 //----------------------------------------------------------------------
2791 // OPCODE: DW_OP_call4
2793 // uint32_t compile unit relative offset of a DIE
2794 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2795 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2796 // a debugging information entry in the current compilation unit.
2798 // Operand interpretation DW_OP_call4 is exactly like that for
2801 // This operation transfers control of DWARF expression evaluation to the
2802 // DW_AT_location attribute of the referenced DIE. If there is no such
2803 // attribute, then there is no effect. Execution of the DWARF expression of
2804 // a DW_AT_location attribute may add to and/or remove from values on the
2805 // stack. Execution returns to the point following the call when the end of
2806 // the attribute is reached. Values on the stack at the time of the call
2807 // may be used as parameters by the called expression and values left on
2808 // the stack by the called expression may be used as return values by prior
2809 // agreement between the calling and called expressions.
2810 //----------------------------------------------------------------------
2813 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2816 //----------------------------------------------------------------------
2817 // OPCODE: DW_OP_stack_value
2819 // DESCRIPTION: Specifies that the object does not exist in memory but
2820 // rather is a constant value. The value from the top of the stack is the
2821 // value to be used. This is the actual object value and not the location.
2822 //----------------------------------------------------------------------
2823 case DW_OP_stack_value:
2824 stack.back().SetValueType(Value::eValueTypeScalar);
2827 //----------------------------------------------------------------------
2828 // OPCODE: DW_OP_call_frame_cfa
2830 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2831 // the canonical frame address consistent with the call frame information
2832 // located in .debug_frame (or in the FDEs of the eh_frame section).
2833 //----------------------------------------------------------------------
2834 case DW_OP_call_frame_cfa:
2836 // Note that we don't have to parse FDEs because this DWARF expression
2837 // is commonly evaluated with a valid stack frame.
2838 StackID id = frame->GetStackID();
2839 addr_t cfa = id.GetCallFrameAddress();
2840 if (cfa != LLDB_INVALID_ADDRESS) {
2841 stack.push_back(Scalar(cfa));
2842 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2843 } else if (error_ptr)
2844 error_ptr->SetErrorString("Stack frame does not include a canonical "
2845 "frame address for DW_OP_call_frame_cfa "
2849 error_ptr->SetErrorString("Invalid stack frame in context for "
2850 "DW_OP_call_frame_cfa opcode.");
2855 //----------------------------------------------------------------------
2856 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2857 // opcode, DW_OP_GNU_push_tls_address)
2859 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2860 // an address in the current thread's thread-local storage block, and
2861 // pushes it on the stack.
2862 //----------------------------------------------------------------------
2863 case DW_OP_form_tls_address:
2864 case DW_OP_GNU_push_tls_address: {
2865 if (stack.size() < 1) {
2867 if (op == DW_OP_form_tls_address)
2868 error_ptr->SetErrorString(
2869 "DW_OP_form_tls_address needs an argument.");
2871 error_ptr->SetErrorString(
2872 "DW_OP_GNU_push_tls_address needs an argument.");
2877 if (!exe_ctx || !module_sp) {
2879 error_ptr->SetErrorString("No context to evaluate TLS within.");
2883 Thread *thread = exe_ctx->GetThreadPtr();
2886 error_ptr->SetErrorString("No thread to evaluate TLS within.");
2890 // Lookup the TLS block address for this thread and module.
2891 const addr_t tls_file_addr =
2892 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2893 const addr_t tls_load_addr =
2894 thread->GetThreadLocalData(module_sp, tls_file_addr);
2896 if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2898 error_ptr->SetErrorString(
2899 "No TLS data currently exists for this thread.");
2903 stack.back().GetScalar() = tls_load_addr;
2904 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2907 //----------------------------------------------------------------------
2908 // OPCODE: DW_OP_GNU_addr_index
2910 // ULEB128: index to the .debug_addr section
2911 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2912 // section with the base address specified by the DW_AT_addr_base attribute
2913 // and the 0 based index is the ULEB128 encoded index.
2914 //----------------------------------------------------------------------
2915 case DW_OP_GNU_addr_index: {
2918 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2919 "compile unit being specified");
2922 uint64_t index = opcodes.GetULEB128(&offset);
2923 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2924 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2925 lldb::offset_t offset = addr_base + index * index_size;
2927 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
2928 &offset, index_size);
2929 stack.push_back(Scalar(value));
2930 stack.back().SetValueType(Value::eValueTypeFileAddress);
2933 //----------------------------------------------------------------------
2934 // OPCODE: DW_OP_GNU_const_index
2936 // ULEB128: index to the .debug_addr section
2937 // DESCRIPTION: Pushes an constant with the size of a machine address to
2938 // the stack from the .debug_addr section with the base address specified
2939 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2941 //----------------------------------------------------------------------
2942 case DW_OP_GNU_const_index: {
2945 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2946 "compile unit being specified");
2949 uint64_t index = opcodes.GetULEB128(&offset);
2950 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2951 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2952 lldb::offset_t offset = addr_base + index * index_size;
2953 const DWARFDataExtractor &debug_addr =
2954 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data();
2955 switch (index_size) {
2957 stack.push_back(Scalar(debug_addr.GetU32(&offset)));
2960 stack.push_back(Scalar(debug_addr.GetU64(&offset)));
2963 assert(false && "Unhandled index size");
2970 log->Printf("Unhandled opcode %s in DWARFExpression.",
2971 DW_OP_value_to_name(op));
2976 if (stack.empty()) {
2977 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2978 // or DW_OP_bit_piece opcodes
2979 if (pieces.GetBuffer().GetByteSize()) {
2983 error_ptr->SetErrorString("Stack empty after evaluation.");
2987 if (log && log->GetVerbose()) {
2988 size_t count = stack.size();
2989 log->Printf("Stack after operation has %" PRIu64 " values:",
2991 for (size_t i = 0; i < count; ++i) {
2992 StreamString new_value;
2993 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2994 stack[i].Dump(&new_value);
2995 log->Printf(" %s", new_value.GetData());
2998 result = stack.back();
3000 return true; // Return true on success
3003 size_t DWARFExpression::LocationListSize(const DWARFUnit *dwarf_cu,
3004 const DataExtractor &debug_loc_data,
3005 lldb::offset_t offset) {
3006 const lldb::offset_t debug_loc_offset = offset;
3007 while (debug_loc_data.ValidOffset(offset)) {
3008 lldb::addr_t start_addr = LLDB_INVALID_ADDRESS;
3009 lldb::addr_t end_addr = LLDB_INVALID_ADDRESS;
3010 if (!AddressRangeForLocationListEntry(dwarf_cu, debug_loc_data, &offset,
3011 start_addr, end_addr))
3014 if (start_addr == 0 && end_addr == 0)
3017 uint16_t loc_length = debug_loc_data.GetU16(&offset);
3018 offset += loc_length;
3021 if (offset > debug_loc_offset)
3022 return offset - debug_loc_offset;
3026 bool DWARFExpression::AddressRangeForLocationListEntry(
3027 const DWARFUnit *dwarf_cu, const DataExtractor &debug_loc_data,
3028 lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc) {
3029 if (!debug_loc_data.ValidOffset(*offset_ptr))
3032 switch (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:
3040 switch (debug_loc_data.GetU8(offset_ptr)) {
3041 case DW_LLE_end_of_list:
3043 case DW_LLE_startx_endx: {
3044 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3045 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3046 index = debug_loc_data.GetULEB128(offset_ptr);
3047 high_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3050 case DW_LLE_startx_length: {
3051 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3052 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3053 uint32_t length = debug_loc_data.GetU32(offset_ptr);
3054 high_pc = low_pc + length;
3058 // Not supported entry type
3062 assert(false && "Not supported location list type");
3066 static bool print_dwarf_exp_op(Stream &s, const DataExtractor &data,
3067 lldb::offset_t *offset_ptr, int address_size,
3068 int dwarf_ref_size) {
3069 uint8_t opcode = data.GetU8(offset_ptr);
3070 DRC_class opcode_class;
3076 opcode_class = DW_OP_value_to_class(opcode) & (~DRC_DWARFv3);
3078 s.Printf("%s ", DW_OP_value_to_name(opcode));
3080 /* Does this take zero parameters? If so we can shortcut this function. */
3081 if (opcode_class == DRC_ZEROOPERANDS)
3084 if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_bregx) {
3085 uint = data.GetULEB128(offset_ptr);
3086 sint = data.GetSLEB128(offset_ptr);
3087 s.Printf("%" PRIu64 " %" PRIi64, uint, sint);
3090 if (opcode_class != DRC_ONEOPERAND) {
3091 s.Printf("UNKNOWN OP %u", opcode);
3097 size = address_size;
3167 case DW_OP_deref_size:
3168 case DW_OP_xderef_size:
3181 case DW_OP_call_ref:
3182 size = dwarf_ref_size;
3185 case DW_OP_plus_uconst:
3187 case DW_OP_GNU_addr_index:
3188 case DW_OP_GNU_const_index:
3192 s.Printf("UNKNOWN ONE-OPERAND OPCODE, #%u", opcode);
3198 sint = (int8_t)data.GetU8(offset_ptr);
3199 s.Printf("%+" PRIi64, sint);
3202 sint = (int16_t)data.GetU16(offset_ptr);
3203 s.Printf("%+" PRIi64, sint);
3206 sint = (int32_t)data.GetU32(offset_ptr);
3207 s.Printf("%+" PRIi64, sint);
3210 sint = (int64_t)data.GetU64(offset_ptr);
3211 s.Printf("%+" PRIi64, sint);
3214 sint = data.GetSLEB128(offset_ptr);
3215 s.Printf("%+" PRIi64, sint);
3218 uint = data.GetU8(offset_ptr);
3219 s.Printf("0x%2.2" PRIx64, uint);
3222 uint = data.GetU16(offset_ptr);
3223 s.Printf("0x%4.4" PRIx64, uint);
3226 uint = data.GetU32(offset_ptr);
3227 s.Printf("0x%8.8" PRIx64, uint);
3230 uint = data.GetU64(offset_ptr);
3231 s.Printf("0x%16.16" PRIx64, uint);
3234 uint = data.GetULEB128(offset_ptr);
3235 s.Printf("0x%" PRIx64, uint);
3242 bool DWARFExpression::PrintDWARFExpression(Stream &s, const DataExtractor &data,
3243 int address_size, int dwarf_ref_size,
3244 bool location_expression) {
3246 lldb::offset_t offset = 0;
3247 while (data.ValidOffset(offset)) {
3248 if (location_expression && op_count > 0)
3252 if (!print_dwarf_exp_op(s, data, &offset, address_size, dwarf_ref_size))
3260 void DWARFExpression::PrintDWARFLocationList(
3261 Stream &s, const DWARFUnit *cu, const DataExtractor &debug_loc_data,
3262 lldb::offset_t offset) {
3263 uint64_t start_addr, end_addr;
3264 uint32_t addr_size = DWARFUnit::GetAddressByteSize(cu);
3265 s.SetAddressByteSize(DWARFUnit::GetAddressByteSize(cu));
3266 dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
3267 while (debug_loc_data.ValidOffset(offset)) {
3268 start_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3269 end_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3271 if (start_addr == 0 && end_addr == 0)
3274 s.PutCString("\n ");
3277 s.AddressRange(start_addr + base_addr, end_addr + base_addr,
3278 cu->GetAddressByteSize(), NULL, ": ");
3279 uint32_t loc_length = debug_loc_data.GetU16(&offset);
3281 DataExtractor locationData(debug_loc_data, offset, loc_length);
3282 PrintDWARFExpression(s, locationData, addr_size, 4, false);
3283 offset += loc_length;
3287 bool DWARFExpression::GetOpAndEndOffsets(StackFrame &frame,
3288 lldb::offset_t &op_offset,
3289 lldb::offset_t &end_offset) {
3290 SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction);
3295 addr_t loclist_base_file_addr =
3296 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
3297 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS) {
3301 addr_t pc_file_addr = frame.GetFrameCodeAddress().GetFileAddress();
3302 lldb::offset_t opcodes_offset, opcodes_length;
3303 if (!GetLocation(loclist_base_file_addr, pc_file_addr, opcodes_offset,
3308 if (opcodes_length == 0) {
3312 op_offset = opcodes_offset;
3313 end_offset = opcodes_offset + opcodes_length;
3317 bool DWARFExpression::MatchesOperand(StackFrame &frame,
3318 const Instruction::Operand &operand) {
3319 using namespace OperandMatchers;
3321 lldb::offset_t op_offset;
3322 lldb::offset_t end_offset;
3323 if (!GetOpAndEndOffsets(frame, op_offset, end_offset)) {
3327 if (!m_data.ValidOffset(op_offset) || op_offset >= end_offset) {
3331 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
3336 DataExtractor opcodes = m_data;
3337 uint8_t opcode = opcodes.GetU8(&op_offset);
3339 if (opcode == DW_OP_fbreg) {
3340 int64_t offset = opcodes.GetSLEB128(&op_offset);
3342 DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr);
3347 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
3348 return fb_expr->MatchesOperand(frame, child);
3352 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3353 recurse)(operand)) {
3357 return MatchUnaryOp(
3358 MatchOpType(Instruction::Operand::Type::Dereference),
3359 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3360 MatchImmOp(offset), recurse))(operand);
3363 bool dereference = false;
3364 const RegisterInfo *reg = nullptr;
3367 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
3368 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
3369 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
3370 offset = opcodes.GetSLEB128(&op_offset);
3371 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
3372 } else if (opcode == DW_OP_regx) {
3373 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3374 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3375 } else if (opcode == DW_OP_bregx) {
3376 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3377 offset = opcodes.GetSLEB128(&op_offset);
3378 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3389 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3390 MatchRegOp(*reg))(operand)) {
3394 return MatchUnaryOp(
3395 MatchOpType(Instruction::Operand::Type::Dereference),
3396 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3398 MatchImmOp(offset)))(operand);
3400 return MatchRegOp(*reg)(operand);