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/DataEncoder.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/RegisterValue.h"
21 #include "lldb/Core/Scalar.h"
22 #include "lldb/Core/StreamString.h"
23 #include "lldb/Core/VMRange.h"
24 #include "lldb/Core/Value.h"
25 #include "lldb/Core/dwarf.h"
27 #include "Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.h"
28 #include "Plugins/ExpressionParser/Clang/ClangExpressionVariable.h"
30 #include "lldb/Host/Endian.h"
31 #include "lldb/Host/Host.h"
33 #include "lldb/Symbol/Function.h"
35 #include "lldb/Target/ABI.h"
36 #include "lldb/Target/ExecutionContext.h"
37 #include "lldb/Target/Process.h"
38 #include "lldb/Target/RegisterContext.h"
39 #include "lldb/Target/StackFrame.h"
40 #include "lldb/Target/StackID.h"
41 #include "lldb/Target/Thread.h"
43 #include "Plugins/SymbolFile/DWARF/DWARFCompileUnit.h"
46 using namespace lldb_private;
49 ReadAddressFromDebugAddrSection(const DWARFCompileUnit *dwarf_cu,
51 uint32_t index_size = dwarf_cu->GetAddressByteSize();
52 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
53 lldb::offset_t offset = addr_base + index * index_size;
54 return dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
58 //----------------------------------------------------------------------
59 // DWARFExpression constructor
60 //----------------------------------------------------------------------
61 DWARFExpression::DWARFExpression(DWARFCompileUnit *dwarf_cu)
62 : m_module_wp(), m_data(), m_dwarf_cu(dwarf_cu),
63 m_reg_kind(eRegisterKindDWARF), m_loclist_slide(LLDB_INVALID_ADDRESS) {}
65 DWARFExpression::DWARFExpression(const DWARFExpression &rhs)
66 : m_module_wp(rhs.m_module_wp), m_data(rhs.m_data),
67 m_dwarf_cu(rhs.m_dwarf_cu), m_reg_kind(rhs.m_reg_kind),
68 m_loclist_slide(rhs.m_loclist_slide) {}
70 DWARFExpression::DWARFExpression(lldb::ModuleSP module_sp,
71 const DataExtractor &data,
72 DWARFCompileUnit *dwarf_cu,
73 lldb::offset_t data_offset,
74 lldb::offset_t data_length)
75 : m_module_wp(), m_data(data, data_offset, data_length),
76 m_dwarf_cu(dwarf_cu), m_reg_kind(eRegisterKindDWARF),
77 m_loclist_slide(LLDB_INVALID_ADDRESS) {
79 m_module_wp = module_sp;
82 //----------------------------------------------------------------------
84 //----------------------------------------------------------------------
85 DWARFExpression::~DWARFExpression() {}
87 bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
89 void DWARFExpression::SetOpcodeData(const DataExtractor &data) {
93 void DWARFExpression::CopyOpcodeData(lldb::ModuleSP module_sp,
94 const DataExtractor &data,
95 lldb::offset_t data_offset,
96 lldb::offset_t data_length) {
97 const uint8_t *bytes = data.PeekData(data_offset, data_length);
99 m_module_wp = module_sp;
100 m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length)));
101 m_data.SetByteOrder(data.GetByteOrder());
102 m_data.SetAddressByteSize(data.GetAddressByteSize());
106 void DWARFExpression::CopyOpcodeData(const void *data,
107 lldb::offset_t data_length,
108 ByteOrder byte_order,
109 uint8_t addr_byte_size) {
110 if (data && data_length) {
111 m_data.SetData(DataBufferSP(new DataBufferHeap(data, data_length)));
112 m_data.SetByteOrder(byte_order);
113 m_data.SetAddressByteSize(addr_byte_size);
117 void DWARFExpression::CopyOpcodeData(uint64_t const_value,
118 lldb::offset_t const_value_byte_size,
119 uint8_t addr_byte_size) {
120 if (const_value_byte_size) {
122 DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
123 m_data.SetByteOrder(endian::InlHostByteOrder());
124 m_data.SetAddressByteSize(addr_byte_size);
128 void DWARFExpression::SetOpcodeData(lldb::ModuleSP module_sp,
129 const DataExtractor &data,
130 lldb::offset_t data_offset,
131 lldb::offset_t data_length) {
132 m_module_wp = module_sp;
133 m_data.SetData(data, data_offset, data_length);
136 void DWARFExpression::DumpLocation(Stream *s, lldb::offset_t offset,
137 lldb::offset_t length,
138 lldb::DescriptionLevel level,
140 if (!m_data.ValidOffsetForDataOfSize(offset, length))
142 const lldb::offset_t start_offset = offset;
143 const lldb::offset_t end_offset = offset + length;
144 while (m_data.ValidOffset(offset) && offset < end_offset) {
145 const lldb::offset_t op_offset = offset;
146 const uint8_t op = m_data.GetU8(&offset);
152 case lldb::eDescriptionLevelBrief:
153 if (offset > start_offset)
157 case lldb::eDescriptionLevelFull:
158 case lldb::eDescriptionLevelVerbose:
159 if (offset > start_offset)
162 if (level == lldb::eDescriptionLevelFull)
164 // Fall through for verbose and print offset and DW_OP prefix..
165 s->Printf("0x%8.8" PRIx64 ": %s", op_offset,
166 op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
172 *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") ";
173 break; // 0x03 1 address
178 s->Printf("DW_OP_const1u(0x%2.2x) ", m_data.GetU8(&offset));
179 break; // 0x08 1 1-byte constant
181 s->Printf("DW_OP_const1s(0x%2.2x) ", m_data.GetU8(&offset));
182 break; // 0x09 1 1-byte constant
184 s->Printf("DW_OP_const2u(0x%4.4x) ", m_data.GetU16(&offset));
185 break; // 0x0a 1 2-byte constant
187 s->Printf("DW_OP_const2s(0x%4.4x) ", m_data.GetU16(&offset));
188 break; // 0x0b 1 2-byte constant
190 s->Printf("DW_OP_const4u(0x%8.8x) ", m_data.GetU32(&offset));
191 break; // 0x0c 1 4-byte constant
193 s->Printf("DW_OP_const4s(0x%8.8x) ", m_data.GetU32(&offset));
194 break; // 0x0d 1 4-byte constant
196 s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset));
197 break; // 0x0e 1 8-byte constant
199 s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset));
200 break; // 0x0f 1 8-byte constant
202 s->Printf("DW_OP_constu(0x%" PRIx64 ") ", m_data.GetULEB128(&offset));
203 break; // 0x10 1 ULEB128 constant
205 s->Printf("DW_OP_consts(0x%" PRId64 ") ", m_data.GetSLEB128(&offset));
206 break; // 0x11 1 SLEB128 constant
208 s->PutCString("DW_OP_dup");
211 s->PutCString("DW_OP_drop");
214 s->PutCString("DW_OP_over");
217 s->Printf("DW_OP_pick(0x%2.2x) ", m_data.GetU8(&offset));
218 break; // 0x15 1 1-byte stack index
220 s->PutCString("DW_OP_swap");
223 s->PutCString("DW_OP_rot");
226 s->PutCString("DW_OP_xderef");
229 s->PutCString("DW_OP_abs");
232 s->PutCString("DW_OP_and");
235 s->PutCString("DW_OP_div");
238 s->PutCString("DW_OP_minus");
241 s->PutCString("DW_OP_mod");
244 s->PutCString("DW_OP_mul");
247 s->PutCString("DW_OP_neg");
250 s->PutCString("DW_OP_not");
253 s->PutCString("DW_OP_or");
256 s->PutCString("DW_OP_plus");
258 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
259 s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ") ",
260 m_data.GetULEB128(&offset));
264 s->PutCString("DW_OP_shl");
267 s->PutCString("DW_OP_shr");
270 s->PutCString("DW_OP_shra");
273 s->PutCString("DW_OP_xor");
276 s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset));
277 break; // 0x2f 1 signed 2-byte constant
279 s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset));
280 break; // 0x28 1 signed 2-byte constant
282 s->PutCString("DW_OP_eq");
285 s->PutCString("DW_OP_ge");
288 s->PutCString("DW_OP_gt");
291 s->PutCString("DW_OP_le");
294 s->PutCString("DW_OP_lt");
297 s->PutCString("DW_OP_ne");
300 case DW_OP_lit0: // 0x30
301 case DW_OP_lit1: // 0x31
302 case DW_OP_lit2: // 0x32
303 case DW_OP_lit3: // 0x33
304 case DW_OP_lit4: // 0x34
305 case DW_OP_lit5: // 0x35
306 case DW_OP_lit6: // 0x36
307 case DW_OP_lit7: // 0x37
308 case DW_OP_lit8: // 0x38
309 case DW_OP_lit9: // 0x39
310 case DW_OP_lit10: // 0x3A
311 case DW_OP_lit11: // 0x3B
312 case DW_OP_lit12: // 0x3C
313 case DW_OP_lit13: // 0x3D
314 case DW_OP_lit14: // 0x3E
315 case DW_OP_lit15: // 0x3F
316 case DW_OP_lit16: // 0x40
317 case DW_OP_lit17: // 0x41
318 case DW_OP_lit18: // 0x42
319 case DW_OP_lit19: // 0x43
320 case DW_OP_lit20: // 0x44
321 case DW_OP_lit21: // 0x45
322 case DW_OP_lit22: // 0x46
323 case DW_OP_lit23: // 0x47
324 case DW_OP_lit24: // 0x48
325 case DW_OP_lit25: // 0x49
326 case DW_OP_lit26: // 0x4A
327 case DW_OP_lit27: // 0x4B
328 case DW_OP_lit28: // 0x4C
329 case DW_OP_lit29: // 0x4D
330 case DW_OP_lit30: // 0x4E
332 s->Printf("DW_OP_lit%i", op - DW_OP_lit0);
335 case DW_OP_reg0: // 0x50
336 case DW_OP_reg1: // 0x51
337 case DW_OP_reg2: // 0x52
338 case DW_OP_reg3: // 0x53
339 case DW_OP_reg4: // 0x54
340 case DW_OP_reg5: // 0x55
341 case DW_OP_reg6: // 0x56
342 case DW_OP_reg7: // 0x57
343 case DW_OP_reg8: // 0x58
344 case DW_OP_reg9: // 0x59
345 case DW_OP_reg10: // 0x5A
346 case DW_OP_reg11: // 0x5B
347 case DW_OP_reg12: // 0x5C
348 case DW_OP_reg13: // 0x5D
349 case DW_OP_reg14: // 0x5E
350 case DW_OP_reg15: // 0x5F
351 case DW_OP_reg16: // 0x60
352 case DW_OP_reg17: // 0x61
353 case DW_OP_reg18: // 0x62
354 case DW_OP_reg19: // 0x63
355 case DW_OP_reg20: // 0x64
356 case DW_OP_reg21: // 0x65
357 case DW_OP_reg22: // 0x66
358 case DW_OP_reg23: // 0x67
359 case DW_OP_reg24: // 0x68
360 case DW_OP_reg25: // 0x69
361 case DW_OP_reg26: // 0x6A
362 case DW_OP_reg27: // 0x6B
363 case DW_OP_reg28: // 0x6C
364 case DW_OP_reg29: // 0x6D
365 case DW_OP_reg30: // 0x6E
366 case DW_OP_reg31: // 0x6F
368 uint32_t reg_num = op - DW_OP_reg0;
370 RegisterInfo reg_info;
371 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
373 s->PutCString(reg_info.name);
375 } else if (reg_info.alt_name) {
376 s->PutCString(reg_info.alt_name);
381 s->Printf("DW_OP_reg%u", reg_num);
417 uint32_t reg_num = op - DW_OP_breg0;
418 int64_t reg_offset = m_data.GetSLEB128(&offset);
420 RegisterInfo reg_info;
421 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
423 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
425 } else if (reg_info.alt_name) {
426 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
431 s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset);
434 case DW_OP_regx: // 0x90 1 ULEB128 register
436 uint32_t reg_num = m_data.GetULEB128(&offset);
438 RegisterInfo reg_info;
439 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
441 s->PutCString(reg_info.name);
443 } else if (reg_info.alt_name) {
444 s->PutCString(reg_info.alt_name);
449 s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num);
452 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
453 s->Printf("DW_OP_fbreg(%" PRIi64 ")", m_data.GetSLEB128(&offset));
455 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
457 uint32_t reg_num = m_data.GetULEB128(&offset);
458 int64_t reg_offset = m_data.GetSLEB128(&offset);
460 RegisterInfo reg_info;
461 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
463 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
465 } else if (reg_info.alt_name) {
466 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
471 s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num,
474 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
475 s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
477 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
478 s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
480 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
481 s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
484 s->PutCString("DW_OP_nop");
486 case DW_OP_push_object_address:
487 s->PutCString("DW_OP_push_object_address");
488 break; // 0x97 DWARF3
489 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
490 s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
492 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
493 s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
495 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
496 s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset));
498 // case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break;
500 // case DW_OP_bit_piece: // 0x9d DWARF3 2
501 // s->Printf("DW_OP_bit_piece(0x%x, 0x%x)",
502 // m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
504 // case DW_OP_lo_user: s->PutCString("DW_OP_lo_user"); break;
506 // case DW_OP_hi_user: s->PutCString("DW_OP_hi_user"); break;
508 // case DW_OP_APPLE_extern:
509 // s->Printf("DW_OP_APPLE_extern(%" PRIu64 ")",
510 // m_data.GetULEB128(&offset));
512 // case DW_OP_APPLE_array_ref:
513 // s->PutCString("DW_OP_APPLE_array_ref");
515 case DW_OP_form_tls_address:
516 s->PutCString("DW_OP_form_tls_address"); // 0x9b
518 case DW_OP_GNU_addr_index: // 0xfb
519 s->Printf("DW_OP_GNU_addr_index(0x%" PRIx64 ")",
520 m_data.GetULEB128(&offset));
522 case DW_OP_GNU_const_index: // 0xfc
523 s->Printf("DW_OP_GNU_const_index(0x%" PRIx64 ")",
524 m_data.GetULEB128(&offset));
526 case DW_OP_GNU_push_tls_address:
527 s->PutCString("DW_OP_GNU_push_tls_address"); // 0xe0
529 case DW_OP_APPLE_uninit:
530 s->PutCString("DW_OP_APPLE_uninit"); // 0xF0
532 // case DW_OP_APPLE_assign: // 0xF1 - pops value off and
533 // assigns it to second item on stack (2nd item must have
534 // assignable context)
535 // s->PutCString("DW_OP_APPLE_assign");
537 // case DW_OP_APPLE_address_of: // 0xF2 - gets the address of
538 // the top stack item (top item must be a variable, or have
539 // value_type that is an address already)
540 // s->PutCString("DW_OP_APPLE_address_of");
542 // case DW_OP_APPLE_value_of: // 0xF3 - pops the value off the
543 // stack and pushes the value of that object (top item must be a
544 // variable, or expression local)
545 // s->PutCString("DW_OP_APPLE_value_of");
547 // case DW_OP_APPLE_deref_type: // 0xF4 - gets the address of
548 // the top stack item (top item must be a variable, or a clang
550 // s->PutCString("DW_OP_APPLE_deref_type");
552 // case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression
554 // s->Printf("DW_OP_APPLE_expr_local(%" PRIu64 ")",
555 // m_data.GetULEB128(&offset));
557 // case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size,
558 // followed by constant float data
560 // uint8_t float_length = m_data.GetU8(&offset);
561 // s->Printf("DW_OP_APPLE_constf(<%u> ", float_length);
562 // m_data.Dump(s, offset, eFormatHex, float_length, 1,
563 // UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
565 // // Consume the float data
566 // m_data.GetData(&offset, float_length);
569 // case DW_OP_APPLE_scalar_cast:
570 // s->Printf("DW_OP_APPLE_scalar_cast(%s)",
571 // Scalar::GetValueTypeAsCString
572 // ((Scalar::Type)m_data.GetU8(&offset)));
574 // case DW_OP_APPLE_clang_cast:
576 // clang::Type *clang_type = (clang::Type
577 // *)m_data.GetMaxU64(&offset, sizeof(void*));
578 // s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type);
581 // case DW_OP_APPLE_clear:
582 // s->PutCString("DW_OP_APPLE_clear");
584 // case DW_OP_APPLE_error: // 0xFF - Stops expression
585 // evaluation and returns an error (no args)
586 // s->PutCString("DW_OP_APPLE_error");
592 void DWARFExpression::SetLocationListSlide(addr_t slide) {
593 m_loclist_slide = slide;
596 int DWARFExpression::GetRegisterKind() { return m_reg_kind; }
598 void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
599 m_reg_kind = reg_kind;
602 bool DWARFExpression::IsLocationList() const {
603 return m_loclist_slide != LLDB_INVALID_ADDRESS;
606 void DWARFExpression::GetDescription(Stream *s, lldb::DescriptionLevel level,
607 addr_t location_list_base_addr,
609 if (IsLocationList()) {
610 // We have a location list
611 lldb::offset_t offset = 0;
613 addr_t curr_base_addr = location_list_base_addr;
614 while (m_data.ValidOffset(offset)) {
615 addr_t begin_addr_offset = LLDB_INVALID_ADDRESS;
616 addr_t end_addr_offset = LLDB_INVALID_ADDRESS;
617 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
618 begin_addr_offset, end_addr_offset))
621 if (begin_addr_offset == 0 && end_addr_offset == 0)
624 if (begin_addr_offset < end_addr_offset) {
627 VMRange addr_range(curr_base_addr + begin_addr_offset,
628 curr_base_addr + end_addr_offset);
629 addr_range.Dump(s, 0, 8);
631 lldb::offset_t location_length = m_data.GetU16(&offset);
632 DumpLocation(s, offset, location_length, level, abi);
634 offset += location_length;
636 if ((m_data.GetAddressByteSize() == 4 &&
637 (begin_addr_offset == UINT32_MAX)) ||
638 (m_data.GetAddressByteSize() == 8 &&
639 (begin_addr_offset == UINT64_MAX))) {
640 curr_base_addr = end_addr_offset + location_list_base_addr;
641 // We have a new base address
644 *s << "base_addr = " << end_addr_offset;
651 // We have a normal location that contains DW_OP location opcodes
652 DumpLocation(s, 0, m_data.GetByteSize(), level, abi);
656 static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
657 lldb::RegisterKind reg_kind,
658 uint32_t reg_num, Error *error_ptr,
660 if (reg_ctx == NULL) {
662 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
664 uint32_t native_reg =
665 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
666 if (native_reg == LLDB_INVALID_REGNUM) {
668 error_ptr->SetErrorStringWithFormat("Unable to convert register "
669 "kind=%u reg_num=%u to a native "
670 "register number.\n",
673 const RegisterInfo *reg_info =
674 reg_ctx->GetRegisterInfoAtIndex(native_reg);
675 RegisterValue reg_value;
676 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
677 if (reg_value.GetScalarValue(value.GetScalar())) {
678 value.SetValueType(Value::eValueTypeScalar);
679 value.SetContext(Value::eContextTypeRegisterInfo,
680 const_cast<RegisterInfo *>(reg_info));
685 // If we get this error, then we need to implement a value
686 // buffer in the dwarf expression evaluation function...
688 error_ptr->SetErrorStringWithFormat(
689 "register %s can't be converted to a scalar value",
694 error_ptr->SetErrorStringWithFormat("register %s is not available",
703 // DWARFExpression::LocationListContainsLoadAddress (Process* process, const
704 // Address &addr) const
706 // return LocationListContainsLoadAddress(process,
707 // addr.GetLoadAddress(process));
711 // DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t
714 // if (load_addr == LLDB_INVALID_ADDRESS)
717 // if (IsLocationList())
719 // lldb::offset_t offset = 0;
721 // addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
723 // if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
726 // while (m_data.ValidOffset(offset))
728 // // We need to figure out what the value is for the location.
729 // addr_t lo_pc = m_data.GetAddress(&offset);
730 // addr_t hi_pc = m_data.GetAddress(&offset);
731 // if (lo_pc == 0 && hi_pc == 0)
735 // lo_pc += loc_list_base_addr;
736 // hi_pc += loc_list_base_addr;
738 // if (lo_pc <= load_addr && load_addr < hi_pc)
741 // offset += m_data.GetU16(&offset);
748 static offset_t GetOpcodeDataSize(const DataExtractor &data,
749 const lldb::offset_t data_offset,
751 lldb::offset_t offset = data_offset;
754 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
755 return data.GetAddressByteSize();
757 // Opcodes with no arguments
758 case DW_OP_deref: // 0x06
759 case DW_OP_dup: // 0x12
760 case DW_OP_drop: // 0x13
761 case DW_OP_over: // 0x14
762 case DW_OP_swap: // 0x16
763 case DW_OP_rot: // 0x17
764 case DW_OP_xderef: // 0x18
765 case DW_OP_abs: // 0x19
766 case DW_OP_and: // 0x1a
767 case DW_OP_div: // 0x1b
768 case DW_OP_minus: // 0x1c
769 case DW_OP_mod: // 0x1d
770 case DW_OP_mul: // 0x1e
771 case DW_OP_neg: // 0x1f
772 case DW_OP_not: // 0x20
773 case DW_OP_or: // 0x21
774 case DW_OP_plus: // 0x22
775 case DW_OP_shl: // 0x24
776 case DW_OP_shr: // 0x25
777 case DW_OP_shra: // 0x26
778 case DW_OP_xor: // 0x27
779 case DW_OP_eq: // 0x29
780 case DW_OP_ge: // 0x2a
781 case DW_OP_gt: // 0x2b
782 case DW_OP_le: // 0x2c
783 case DW_OP_lt: // 0x2d
784 case DW_OP_ne: // 0x2e
785 case DW_OP_lit0: // 0x30
786 case DW_OP_lit1: // 0x31
787 case DW_OP_lit2: // 0x32
788 case DW_OP_lit3: // 0x33
789 case DW_OP_lit4: // 0x34
790 case DW_OP_lit5: // 0x35
791 case DW_OP_lit6: // 0x36
792 case DW_OP_lit7: // 0x37
793 case DW_OP_lit8: // 0x38
794 case DW_OP_lit9: // 0x39
795 case DW_OP_lit10: // 0x3A
796 case DW_OP_lit11: // 0x3B
797 case DW_OP_lit12: // 0x3C
798 case DW_OP_lit13: // 0x3D
799 case DW_OP_lit14: // 0x3E
800 case DW_OP_lit15: // 0x3F
801 case DW_OP_lit16: // 0x40
802 case DW_OP_lit17: // 0x41
803 case DW_OP_lit18: // 0x42
804 case DW_OP_lit19: // 0x43
805 case DW_OP_lit20: // 0x44
806 case DW_OP_lit21: // 0x45
807 case DW_OP_lit22: // 0x46
808 case DW_OP_lit23: // 0x47
809 case DW_OP_lit24: // 0x48
810 case DW_OP_lit25: // 0x49
811 case DW_OP_lit26: // 0x4A
812 case DW_OP_lit27: // 0x4B
813 case DW_OP_lit28: // 0x4C
814 case DW_OP_lit29: // 0x4D
815 case DW_OP_lit30: // 0x4E
816 case DW_OP_lit31: // 0x4f
817 case DW_OP_reg0: // 0x50
818 case DW_OP_reg1: // 0x51
819 case DW_OP_reg2: // 0x52
820 case DW_OP_reg3: // 0x53
821 case DW_OP_reg4: // 0x54
822 case DW_OP_reg5: // 0x55
823 case DW_OP_reg6: // 0x56
824 case DW_OP_reg7: // 0x57
825 case DW_OP_reg8: // 0x58
826 case DW_OP_reg9: // 0x59
827 case DW_OP_reg10: // 0x5A
828 case DW_OP_reg11: // 0x5B
829 case DW_OP_reg12: // 0x5C
830 case DW_OP_reg13: // 0x5D
831 case DW_OP_reg14: // 0x5E
832 case DW_OP_reg15: // 0x5F
833 case DW_OP_reg16: // 0x60
834 case DW_OP_reg17: // 0x61
835 case DW_OP_reg18: // 0x62
836 case DW_OP_reg19: // 0x63
837 case DW_OP_reg20: // 0x64
838 case DW_OP_reg21: // 0x65
839 case DW_OP_reg22: // 0x66
840 case DW_OP_reg23: // 0x67
841 case DW_OP_reg24: // 0x68
842 case DW_OP_reg25: // 0x69
843 case DW_OP_reg26: // 0x6A
844 case DW_OP_reg27: // 0x6B
845 case DW_OP_reg28: // 0x6C
846 case DW_OP_reg29: // 0x6D
847 case DW_OP_reg30: // 0x6E
848 case DW_OP_reg31: // 0x6F
849 case DW_OP_nop: // 0x96
850 case DW_OP_push_object_address: // 0x97 DWARF3
851 case DW_OP_form_tls_address: // 0x9b DWARF3
852 case DW_OP_call_frame_cfa: // 0x9c DWARF3
853 case DW_OP_stack_value: // 0x9f DWARF4
854 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
857 // Opcodes with a single 1 byte arguments
858 case DW_OP_const1u: // 0x08 1 1-byte constant
859 case DW_OP_const1s: // 0x09 1 1-byte constant
860 case DW_OP_pick: // 0x15 1 1-byte stack index
861 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
862 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
865 // Opcodes with a single 2 byte arguments
866 case DW_OP_const2u: // 0x0a 1 2-byte constant
867 case DW_OP_const2s: // 0x0b 1 2-byte constant
868 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
869 case DW_OP_bra: // 0x28 1 signed 2-byte constant
870 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
873 // Opcodes with a single 4 byte arguments
874 case DW_OP_const4u: // 0x0c 1 4-byte constant
875 case DW_OP_const4s: // 0x0d 1 4-byte constant
876 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
879 // Opcodes with a single 8 byte arguments
880 case DW_OP_const8u: // 0x0e 1 8-byte constant
881 case DW_OP_const8s: // 0x0f 1 8-byte constant
884 // All opcodes that have a single ULEB (signed or unsigned) argument
885 case DW_OP_constu: // 0x10 1 ULEB128 constant
886 case DW_OP_consts: // 0x11 1 SLEB128 constant
887 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
888 case DW_OP_breg0: // 0x70 1 ULEB128 register
889 case DW_OP_breg1: // 0x71 1 ULEB128 register
890 case DW_OP_breg2: // 0x72 1 ULEB128 register
891 case DW_OP_breg3: // 0x73 1 ULEB128 register
892 case DW_OP_breg4: // 0x74 1 ULEB128 register
893 case DW_OP_breg5: // 0x75 1 ULEB128 register
894 case DW_OP_breg6: // 0x76 1 ULEB128 register
895 case DW_OP_breg7: // 0x77 1 ULEB128 register
896 case DW_OP_breg8: // 0x78 1 ULEB128 register
897 case DW_OP_breg9: // 0x79 1 ULEB128 register
898 case DW_OP_breg10: // 0x7a 1 ULEB128 register
899 case DW_OP_breg11: // 0x7b 1 ULEB128 register
900 case DW_OP_breg12: // 0x7c 1 ULEB128 register
901 case DW_OP_breg13: // 0x7d 1 ULEB128 register
902 case DW_OP_breg14: // 0x7e 1 ULEB128 register
903 case DW_OP_breg15: // 0x7f 1 ULEB128 register
904 case DW_OP_breg16: // 0x80 1 ULEB128 register
905 case DW_OP_breg17: // 0x81 1 ULEB128 register
906 case DW_OP_breg18: // 0x82 1 ULEB128 register
907 case DW_OP_breg19: // 0x83 1 ULEB128 register
908 case DW_OP_breg20: // 0x84 1 ULEB128 register
909 case DW_OP_breg21: // 0x85 1 ULEB128 register
910 case DW_OP_breg22: // 0x86 1 ULEB128 register
911 case DW_OP_breg23: // 0x87 1 ULEB128 register
912 case DW_OP_breg24: // 0x88 1 ULEB128 register
913 case DW_OP_breg25: // 0x89 1 ULEB128 register
914 case DW_OP_breg26: // 0x8a 1 ULEB128 register
915 case DW_OP_breg27: // 0x8b 1 ULEB128 register
916 case DW_OP_breg28: // 0x8c 1 ULEB128 register
917 case DW_OP_breg29: // 0x8d 1 ULEB128 register
918 case DW_OP_breg30: // 0x8e 1 ULEB128 register
919 case DW_OP_breg31: // 0x8f 1 ULEB128 register
920 case DW_OP_regx: // 0x90 1 ULEB128 register
921 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
922 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
923 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
924 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
925 data.Skip_LEB128(&offset);
926 return offset - data_offset;
928 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
929 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
930 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
931 data.Skip_LEB128(&offset);
932 data.Skip_LEB128(&offset);
933 return offset - data_offset;
935 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
938 uint64_t block_len = data.Skip_LEB128(&offset);
940 return offset - data_offset;
946 return LLDB_INVALID_OFFSET;
949 lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(uint32_t op_addr_idx,
952 if (IsLocationList())
953 return LLDB_INVALID_ADDRESS;
954 lldb::offset_t offset = 0;
955 uint32_t curr_op_addr_idx = 0;
956 while (m_data.ValidOffset(offset)) {
957 const uint8_t op = m_data.GetU8(&offset);
959 if (op == DW_OP_addr) {
960 const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
961 if (curr_op_addr_idx == op_addr_idx)
965 } else if (op == DW_OP_GNU_addr_index) {
966 uint64_t index = m_data.GetULEB128(&offset);
967 if (curr_op_addr_idx == op_addr_idx) {
973 return ReadAddressFromDebugAddrSection(m_dwarf_cu, index);
977 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
978 if (op_arg_size == LLDB_INVALID_OFFSET) {
982 offset += op_arg_size;
985 return LLDB_INVALID_ADDRESS;
988 bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) {
989 if (IsLocationList())
991 lldb::offset_t offset = 0;
992 while (m_data.ValidOffset(offset)) {
993 const uint8_t op = m_data.GetU8(&offset);
995 if (op == DW_OP_addr) {
996 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
997 // We have to make a copy of the data as we don't know if this
998 // data is from a read only memory mapped buffer, so we duplicate
999 // all of the data first, then modify it, and if all goes well,
1000 // we then replace the data for this expression
1002 // So first we copy the data into a heap buffer
1003 std::unique_ptr<DataBufferHeap> head_data_ap(
1004 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1006 // Make en encoder so we can write the address into the buffer using
1007 // the correct byte order (endianness)
1008 DataEncoder encoder(head_data_ap->GetBytes(), head_data_ap->GetByteSize(),
1009 m_data.GetByteOrder(), addr_byte_size);
1011 // Replace the address in the new buffer
1012 if (encoder.PutMaxU64(offset, addr_byte_size, file_addr) == UINT32_MAX)
1015 // All went well, so now we can reset the data using a shared
1016 // pointer to the heap data so "m_data" will now correctly
1017 // manage the heap data.
1018 m_data.SetData(DataBufferSP(head_data_ap.release()));
1021 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1022 if (op_arg_size == LLDB_INVALID_OFFSET)
1024 offset += op_arg_size;
1030 bool DWARFExpression::ContainsThreadLocalStorage() const {
1031 // We are assuming for now that any thread local variable will not
1032 // have a location list. This has been true for all thread local
1033 // variables we have seen so far produced by any compiler.
1034 if (IsLocationList())
1036 lldb::offset_t offset = 0;
1037 while (m_data.ValidOffset(offset)) {
1038 const uint8_t op = m_data.GetU8(&offset);
1040 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
1042 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1043 if (op_arg_size == LLDB_INVALID_OFFSET)
1046 offset += op_arg_size;
1050 bool DWARFExpression::LinkThreadLocalStorage(
1051 lldb::ModuleSP new_module_sp,
1052 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
1053 &link_address_callback) {
1054 // We are assuming for now that any thread local variable will not
1055 // have a location list. This has been true for all thread local
1056 // variables we have seen so far produced by any compiler.
1057 if (IsLocationList())
1060 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
1061 // We have to make a copy of the data as we don't know if this
1062 // data is from a read only memory mapped buffer, so we duplicate
1063 // all of the data first, then modify it, and if all goes well,
1064 // we then replace the data for this expression
1066 // So first we copy the data into a heap buffer
1067 std::shared_ptr<DataBufferHeap> heap_data_sp(
1068 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1070 // Make en encoder so we can write the address into the buffer using
1071 // the correct byte order (endianness)
1072 DataEncoder encoder(heap_data_sp->GetBytes(), heap_data_sp->GetByteSize(),
1073 m_data.GetByteOrder(), addr_byte_size);
1075 lldb::offset_t offset = 0;
1076 lldb::offset_t const_offset = 0;
1077 lldb::addr_t const_value = 0;
1078 size_t const_byte_size = 0;
1079 while (m_data.ValidOffset(offset)) {
1080 const uint8_t op = m_data.GetU8(&offset);
1082 bool decoded_data = false;
1085 // Remember the const offset in case we later have a
1086 // DW_OP_form_tls_address
1087 // or DW_OP_GNU_push_tls_address
1088 const_offset = offset;
1089 const_value = m_data.GetU32(&offset);
1090 decoded_data = true;
1091 const_byte_size = 4;
1095 // Remember the const offset in case we later have a
1096 // DW_OP_form_tls_address
1097 // or DW_OP_GNU_push_tls_address
1098 const_offset = offset;
1099 const_value = m_data.GetU64(&offset);
1100 decoded_data = true;
1101 const_byte_size = 8;
1104 case DW_OP_form_tls_address:
1105 case DW_OP_GNU_push_tls_address:
1106 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
1108 // a file address on the stack. We assume that DW_OP_const4u or
1110 // is used for these values, and we check that the last opcode we got
1112 // either of these was DW_OP_const4u or DW_OP_const8u. If so, then we can
1114 // the value accodingly. For Darwin, the value in the DW_OP_const4u or
1115 // DW_OP_const8u is the file address of a structure that contains a
1117 // pointer, the pthread key and the offset into the data pointed to by the
1118 // pthread key. So we must link this address and also set the module of
1120 // expression to the new_module_sp so we can resolve the file address
1122 if (const_byte_size > 0) {
1123 lldb::addr_t linked_file_addr = link_address_callback(const_value);
1124 if (linked_file_addr == LLDB_INVALID_ADDRESS)
1126 // Replace the address in the new buffer
1127 if (encoder.PutMaxU64(const_offset, const_byte_size,
1128 linked_file_addr) == UINT32_MAX)
1136 const_byte_size = 0;
1140 if (!decoded_data) {
1141 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1142 if (op_arg_size == LLDB_INVALID_OFFSET)
1145 offset += op_arg_size;
1149 // If we linked the TLS address correctly, update the module so that when the
1151 // is evaluated it can resolve the file address to a load address and read the
1153 m_module_wp = new_module_sp;
1154 m_data.SetData(heap_data_sp);
1158 bool DWARFExpression::LocationListContainsAddress(
1159 lldb::addr_t loclist_base_addr, lldb::addr_t addr) const {
1160 if (addr == LLDB_INVALID_ADDRESS)
1163 if (IsLocationList()) {
1164 lldb::offset_t offset = 0;
1166 if (loclist_base_addr == LLDB_INVALID_ADDRESS)
1169 while (m_data.ValidOffset(offset)) {
1170 // We need to figure out what the value is for the location.
1171 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1172 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1173 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1177 if (lo_pc == 0 && hi_pc == 0)
1180 lo_pc += loclist_base_addr - m_loclist_slide;
1181 hi_pc += loclist_base_addr - m_loclist_slide;
1183 if (lo_pc <= addr && addr < hi_pc)
1186 offset += m_data.GetU16(&offset);
1192 bool DWARFExpression::GetLocation(addr_t base_addr, addr_t pc,
1193 lldb::offset_t &offset,
1194 lldb::offset_t &length) {
1196 if (!IsLocationList()) {
1197 length = m_data.GetByteSize();
1201 if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS) {
1202 addr_t curr_base_addr = base_addr;
1204 while (m_data.ValidOffset(offset)) {
1205 // We need to figure out what the value is for the location.
1206 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1207 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1208 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1212 if (lo_pc == 0 && hi_pc == 0)
1215 lo_pc += curr_base_addr - m_loclist_slide;
1216 hi_pc += curr_base_addr - m_loclist_slide;
1218 length = m_data.GetU16(&offset);
1220 if (length > 0 && lo_pc <= pc && pc < hi_pc)
1226 offset = LLDB_INVALID_OFFSET;
1231 bool DWARFExpression::DumpLocationForAddress(Stream *s,
1232 lldb::DescriptionLevel level,
1233 addr_t base_addr, addr_t address,
1235 lldb::offset_t offset = 0;
1236 lldb::offset_t length = 0;
1238 if (GetLocation(base_addr, address, offset, length)) {
1240 DumpLocation(s, offset, length, level, abi);
1247 bool DWARFExpression::Evaluate(ExecutionContextScope *exe_scope,
1248 ClangExpressionVariableList *expr_locals,
1249 ClangExpressionDeclMap *decl_map,
1250 lldb::addr_t loclist_base_load_addr,
1251 const Value *initial_value_ptr,
1252 const Value *object_address_ptr, Value &result,
1253 Error *error_ptr) const {
1254 ExecutionContext exe_ctx(exe_scope);
1255 return Evaluate(&exe_ctx, expr_locals, decl_map, nullptr,
1256 loclist_base_load_addr, initial_value_ptr, object_address_ptr,
1260 bool DWARFExpression::Evaluate(
1261 ExecutionContext *exe_ctx, ClangExpressionVariableList *expr_locals,
1262 ClangExpressionDeclMap *decl_map, RegisterContext *reg_ctx,
1263 lldb::addr_t loclist_base_load_addr, const Value *initial_value_ptr,
1264 const Value *object_address_ptr, Value &result, Error *error_ptr) const {
1265 ModuleSP module_sp = m_module_wp.lock();
1267 if (IsLocationList()) {
1268 lldb::offset_t offset = 0;
1270 StackFrame *frame = NULL;
1272 pc = reg_ctx->GetPC();
1274 frame = exe_ctx->GetFramePtr();
1277 RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
1280 pc = reg_ctx_sp->GetPC();
1283 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS) {
1284 if (pc == LLDB_INVALID_ADDRESS) {
1286 error_ptr->SetErrorString("Invalid PC in frame.");
1290 addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
1292 while (m_data.ValidOffset(offset)) {
1293 // We need to figure out what the value is for the location.
1294 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1295 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1296 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
1300 if (lo_pc == 0 && hi_pc == 0)
1303 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
1304 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
1306 uint16_t length = m_data.GetU16(&offset);
1308 if (length > 0 && lo_pc <= pc && pc < hi_pc) {
1309 return DWARFExpression::Evaluate(
1310 exe_ctx, expr_locals, decl_map, reg_ctx, module_sp, m_data,
1311 m_dwarf_cu, offset, length, m_reg_kind, initial_value_ptr,
1312 object_address_ptr, result, error_ptr);
1318 error_ptr->SetErrorString("variable not available");
1322 // Not a location list, just a single expression.
1323 return DWARFExpression::Evaluate(
1324 exe_ctx, expr_locals, decl_map, reg_ctx, module_sp, m_data, m_dwarf_cu, 0,
1325 m_data.GetByteSize(), m_reg_kind, initial_value_ptr, object_address_ptr,
1329 bool DWARFExpression::Evaluate(
1330 ExecutionContext *exe_ctx, ClangExpressionVariableList *expr_locals,
1331 ClangExpressionDeclMap *decl_map, RegisterContext *reg_ctx,
1332 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
1333 DWARFCompileUnit *dwarf_cu, const lldb::offset_t opcodes_offset,
1334 const lldb::offset_t opcodes_length, const lldb::RegisterKind reg_kind,
1335 const Value *initial_value_ptr, const Value *object_address_ptr,
1336 Value &result, Error *error_ptr) {
1338 if (opcodes_length == 0) {
1340 error_ptr->SetErrorString(
1341 "no location, value may have been optimized out");
1344 std::vector<Value> stack;
1346 Process *process = NULL;
1347 StackFrame *frame = NULL;
1350 process = exe_ctx->GetProcessPtr();
1351 frame = exe_ctx->GetFramePtr();
1353 if (reg_ctx == NULL && frame)
1354 reg_ctx = frame->GetRegisterContext().get();
1356 if (initial_value_ptr)
1357 stack.push_back(*initial_value_ptr);
1359 lldb::offset_t offset = opcodes_offset;
1360 const lldb::offset_t end_offset = opcodes_offset + opcodes_length;
1364 /// Insertion point for evaluating multi-piece expression.
\13
1365 uint64_t op_piece_offset = 0;
1366 Value pieces; // Used for DW_OP_piece
1368 // Make sure all of the data is available in opcodes.
1369 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length)) {
1371 error_ptr->SetErrorString(
1372 "invalid offset and/or length for opcodes buffer.");
1375 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1377 while (opcodes.ValidOffset(offset) && offset < end_offset) {
1378 const lldb::offset_t op_offset = offset;
1379 const uint8_t op = opcodes.GetU8(&offset);
1381 if (log && log->GetVerbose()) {
1382 size_t count = stack.size();
1383 log->Printf("Stack before operation has %" PRIu64 " values:",
1385 for (size_t i = 0; i < count; ++i) {
1386 StreamString new_value;
1387 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1388 stack[i].Dump(&new_value);
1389 log->Printf(" %s", new_value.GetData());
1391 log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
1394 //----------------------------------------------------------------------
1395 // The DW_OP_addr operation has a single operand that encodes a machine
1396 // address and whose size is the size of an address on the target machine.
1397 //----------------------------------------------------------------------
1399 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1400 stack.back().SetValueType(Value::eValueTypeFileAddress);
1403 //----------------------------------------------------------------------
1404 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1405 // shared libraries that have a location like:
1406 // DW_OP_addr(0x1000)
1407 // If this address resides in a shared library, then this virtual
1408 // address won't make sense when it is evaluated in the context of a
1409 // running process where shared libraries have been slid. To account for
1410 // this, this new address type where we can store the section pointer
1411 // and a 4 byte offset.
1412 //----------------------------------------------------------------------
1413 // case DW_OP_addr_sect_offset4:
1415 // result_type = eResultTypeFileAddress;
1416 // lldb::Section *sect = (lldb::Section
1417 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
1418 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1420 // Address so_addr (sect, sect_offset);
1421 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
1422 // if (load_addr != LLDB_INVALID_ADDRESS)
1424 // // We successfully resolve a file address to a load
1426 // stack.push_back(load_addr);
1433 // error_ptr->SetErrorStringWithFormat ("Section %s in
1434 // %s is not currently loaded.\n",
1435 // sect->GetName().AsCString(),
1436 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1442 //----------------------------------------------------------------------
1443 // OPCODE: DW_OP_deref
1445 // DESCRIPTION: Pops the top stack entry and treats it as an address.
1446 // The value retrieved from that address is pushed. The size of the
1447 // data retrieved from the dereferenced address is the size of an
1448 // address on the target machine.
1449 //----------------------------------------------------------------------
1451 if (stack.empty()) {
1453 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1456 Value::ValueType value_type = stack.back().GetValueType();
1457 switch (value_type) {
1458 case Value::eValueTypeHostAddress: {
1459 void *src = (void *)stack.back().GetScalar().ULongLong();
1461 ::memcpy(&ptr, src, sizeof(void *));
1462 stack.back().GetScalar() = ptr;
1463 stack.back().ClearContext();
1465 case Value::eValueTypeLoadAddress:
1468 lldb::addr_t pointer_addr =
1469 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1471 lldb::addr_t pointer_value =
1472 process->ReadPointerFromMemory(pointer_addr, error);
1473 if (pointer_value != LLDB_INVALID_ADDRESS) {
1474 stack.back().GetScalar() = pointer_value;
1475 stack.back().ClearContext();
1478 error_ptr->SetErrorStringWithFormat(
1479 "Failed to dereference pointer from 0x%" PRIx64
1480 " for DW_OP_deref: %s\n",
1481 pointer_addr, error.AsCString());
1486 error_ptr->SetErrorStringWithFormat(
1487 "NULL process for DW_OP_deref.\n");
1492 error_ptr->SetErrorStringWithFormat(
1493 "NULL execution context for DW_OP_deref.\n");
1504 //----------------------------------------------------------------------
1505 // OPCODE: DW_OP_deref_size
1507 // 1 - uint8_t that specifies the size of the data to dereference.
1508 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1509 // stack entry and treats it as an address. The value retrieved from that
1510 // address is pushed. In the DW_OP_deref_size operation, however, the
1511 // size in bytes of the data retrieved from the dereferenced address is
1512 // specified by the single operand. This operand is a 1-byte unsigned
1513 // integral constant whose value may not be larger than the size of an
1514 // address on the target machine. The data retrieved is zero extended
1515 // to the size of an address on the target machine before being pushed
1516 // on the expression stack.
1517 //----------------------------------------------------------------------
1518 case DW_OP_deref_size: {
1519 if (stack.empty()) {
1521 error_ptr->SetErrorString(
1522 "Expression stack empty for DW_OP_deref_size.");
1525 uint8_t size = opcodes.GetU8(&offset);
1526 Value::ValueType value_type = stack.back().GetValueType();
1527 switch (value_type) {
1528 case Value::eValueTypeHostAddress: {
1529 void *src = (void *)stack.back().GetScalar().ULongLong();
1531 ::memcpy(&ptr, src, sizeof(void *));
1532 // I can't decide whether the size operand should apply to the bytes in
1534 // lldb-host endianness or the target endianness.. I doubt this'll ever
1536 // but I'll opt for assuming big endian regardless.
1545 ptr = ptr & 0xffffff;
1548 ptr = ptr & 0xffffffff;
1550 // the casts are added to work around the case where intptr_t is a 32
1552 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1555 ptr = (intptr_t)ptr & 0xffffffffffULL;
1558 ptr = (intptr_t)ptr & 0xffffffffffffULL;
1561 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1566 stack.back().GetScalar() = ptr;
1567 stack.back().ClearContext();
1569 case Value::eValueTypeLoadAddress:
1572 lldb::addr_t pointer_addr =
1573 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1574 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1576 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1578 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
1579 process->GetByteOrder(), size);
1580 lldb::offset_t addr_data_offset = 0;
1583 stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
1586 stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
1589 stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
1592 stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
1595 stack.back().GetScalar() =
1596 addr_data.GetPointer(&addr_data_offset);
1598 stack.back().ClearContext();
1601 error_ptr->SetErrorStringWithFormat(
1602 "Failed to dereference pointer from 0x%" PRIx64
1603 " for DW_OP_deref: %s\n",
1604 pointer_addr, error.AsCString());
1609 error_ptr->SetErrorStringWithFormat(
1610 "NULL process for DW_OP_deref.\n");
1615 error_ptr->SetErrorStringWithFormat(
1616 "NULL execution context for DW_OP_deref.\n");
1627 //----------------------------------------------------------------------
1628 // OPCODE: DW_OP_xderef_size
1630 // 1 - uint8_t that specifies the size of the data to dereference.
1631 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1632 // the top of the stack is treated as an address. The second stack
1633 // entry is treated as an "address space identifier" for those
1634 // architectures that support multiple address spaces. The top two
1635 // stack elements are popped, a data item is retrieved through an
1636 // implementation-defined address calculation and pushed as the new
1637 // stack top. In the DW_OP_xderef_size operation, however, the size in
1638 // bytes of the data retrieved from the dereferenced address is
1639 // specified by the single operand. This operand is a 1-byte unsigned
1640 // integral constant whose value may not be larger than the size of an
1641 // address on the target machine. The data retrieved is zero extended
1642 // to the size of an address on the target machine before being pushed
1643 // on the expression stack.
1644 //----------------------------------------------------------------------
1645 case DW_OP_xderef_size:
1647 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1649 //----------------------------------------------------------------------
1650 // OPCODE: DW_OP_xderef
1652 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1653 // the top of the stack is treated as an address. The second stack entry
1654 // is treated as an "address space identifier" for those architectures
1655 // that support multiple address spaces. The top two stack elements are
1656 // popped, a data item is retrieved through an implementation-defined
1657 // address calculation and pushed as the new stack top. The size of the
1658 // data retrieved from the dereferenced address is the size of an address
1659 // on the target machine.
1660 //----------------------------------------------------------------------
1663 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1666 //----------------------------------------------------------------------
1667 // All DW_OP_constXXX opcodes have a single operand as noted below:
1670 // --------------- ----------------------------------------------------
1671 // DW_OP_const1u 1-byte unsigned integer constant
1672 // DW_OP_const1s 1-byte signed integer constant
1673 // DW_OP_const2u 2-byte unsigned integer constant
1674 // DW_OP_const2s 2-byte signed integer constant
1675 // DW_OP_const4u 4-byte unsigned integer constant
1676 // DW_OP_const4s 4-byte signed integer constant
1677 // DW_OP_const8u 8-byte unsigned integer constant
1678 // DW_OP_const8s 8-byte signed integer constant
1679 // DW_OP_constu unsigned LEB128 integer constant
1680 // DW_OP_consts signed LEB128 integer constant
1681 //----------------------------------------------------------------------
1683 stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
1686 stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
1689 stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
1692 stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
1695 stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
1698 stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
1701 stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
1704 stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
1707 stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1710 stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1713 //----------------------------------------------------------------------
1714 // OPCODE: DW_OP_dup
1716 // DESCRIPTION: duplicates the value at the top of the stack
1717 //----------------------------------------------------------------------
1719 if (stack.empty()) {
1721 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1724 stack.push_back(stack.back());
1727 //----------------------------------------------------------------------
1728 // OPCODE: DW_OP_drop
1730 // DESCRIPTION: pops the value at the top of the stack
1731 //----------------------------------------------------------------------
1733 if (stack.empty()) {
1735 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1741 //----------------------------------------------------------------------
1742 // OPCODE: DW_OP_over
1744 // DESCRIPTION: Duplicates the entry currently second in the stack at
1745 // the top of the stack.
1746 //----------------------------------------------------------------------
1748 if (stack.size() < 2) {
1750 error_ptr->SetErrorString(
1751 "Expression stack needs at least 2 items for DW_OP_over.");
1754 stack.push_back(stack[stack.size() - 2]);
1757 //----------------------------------------------------------------------
1758 // OPCODE: DW_OP_pick
1759 // OPERANDS: uint8_t index into the current stack
1760 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1761 // inclusive) is pushed on the stack
1762 //----------------------------------------------------------------------
1764 uint8_t pick_idx = opcodes.GetU8(&offset);
1765 if (pick_idx < stack.size())
1766 stack.push_back(stack[pick_idx]);
1769 error_ptr->SetErrorStringWithFormat(
1770 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1775 //----------------------------------------------------------------------
1776 // OPCODE: DW_OP_swap
1778 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1779 // of the stack becomes the second stack entry, and the second entry
1780 // becomes the top of the stack
1781 //----------------------------------------------------------------------
1783 if (stack.size() < 2) {
1785 error_ptr->SetErrorString(
1786 "Expression stack needs at least 2 items for DW_OP_swap.");
1790 stack.back() = stack[stack.size() - 2];
1791 stack[stack.size() - 2] = tmp;
1795 //----------------------------------------------------------------------
1796 // OPCODE: DW_OP_rot
1798 // DESCRIPTION: Rotates the first three stack entries. The entry at
1799 // the top of the stack becomes the third stack entry, the second
1800 // entry becomes the top of the stack, and the third entry becomes
1801 // the second entry.
1802 //----------------------------------------------------------------------
1804 if (stack.size() < 3) {
1806 error_ptr->SetErrorString(
1807 "Expression stack needs at least 3 items for DW_OP_rot.");
1810 size_t last_idx = stack.size() - 1;
1811 Value old_top = stack[last_idx];
1812 stack[last_idx] = stack[last_idx - 1];
1813 stack[last_idx - 1] = stack[last_idx - 2];
1814 stack[last_idx - 2] = old_top;
1818 //----------------------------------------------------------------------
1819 // OPCODE: DW_OP_abs
1821 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1822 // value and pushes its absolute value. If the absolute value can not be
1823 // represented, the result is undefined.
1824 //----------------------------------------------------------------------
1826 if (stack.empty()) {
1828 error_ptr->SetErrorString(
1829 "Expression stack needs at least 1 item for DW_OP_abs.");
1831 } else if (stack.back().ResolveValue(exe_ctx).AbsoluteValue() == false) {
1833 error_ptr->SetErrorString(
1834 "Failed to take the absolute value of the first stack item.");
1839 //----------------------------------------------------------------------
1840 // OPCODE: DW_OP_and
1842 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1843 // operation on the two, and pushes the result.
1844 //----------------------------------------------------------------------
1846 if (stack.size() < 2) {
1848 error_ptr->SetErrorString(
1849 "Expression stack needs at least 2 items for DW_OP_and.");
1854 stack.back().ResolveValue(exe_ctx) =
1855 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1859 //----------------------------------------------------------------------
1860 // OPCODE: DW_OP_div
1862 // DESCRIPTION: pops the top two stack values, divides the former second
1863 // entry by the former top of the stack using signed division, and
1864 // pushes the result.
1865 //----------------------------------------------------------------------
1867 if (stack.size() < 2) {
1869 error_ptr->SetErrorString(
1870 "Expression stack needs at least 2 items for DW_OP_div.");
1874 if (tmp.ResolveValue(exe_ctx).IsZero()) {
1876 error_ptr->SetErrorString("Divide by zero.");
1881 stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1882 if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1884 error_ptr->SetErrorString("Divide failed.");
1891 //----------------------------------------------------------------------
1892 // OPCODE: DW_OP_minus
1894 // DESCRIPTION: pops the top two stack values, subtracts the former top
1895 // of the stack from the former second entry, and pushes the result.
1896 //----------------------------------------------------------------------
1898 if (stack.size() < 2) {
1900 error_ptr->SetErrorString(
1901 "Expression stack needs at least 2 items for DW_OP_minus.");
1906 stack.back().ResolveValue(exe_ctx) =
1907 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1911 //----------------------------------------------------------------------
1912 // OPCODE: DW_OP_mod
1914 // DESCRIPTION: pops the top two stack values and pushes the result of
1915 // the calculation: former second stack entry modulo the former top of
1917 //----------------------------------------------------------------------
1919 if (stack.size() < 2) {
1921 error_ptr->SetErrorString(
1922 "Expression stack needs at least 2 items for DW_OP_mod.");
1927 stack.back().ResolveValue(exe_ctx) =
1928 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1932 //----------------------------------------------------------------------
1933 // OPCODE: DW_OP_mul
1935 // DESCRIPTION: pops the top two stack entries, multiplies them
1936 // together, and pushes the result.
1937 //----------------------------------------------------------------------
1939 if (stack.size() < 2) {
1941 error_ptr->SetErrorString(
1942 "Expression stack needs at least 2 items for DW_OP_mul.");
1947 stack.back().ResolveValue(exe_ctx) =
1948 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1952 //----------------------------------------------------------------------
1953 // OPCODE: DW_OP_neg
1955 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1956 //----------------------------------------------------------------------
1958 if (stack.empty()) {
1960 error_ptr->SetErrorString(
1961 "Expression stack needs at least 1 item for DW_OP_neg.");
1964 if (stack.back().ResolveValue(exe_ctx).UnaryNegate() == false) {
1966 error_ptr->SetErrorString("Unary negate failed.");
1972 //----------------------------------------------------------------------
1973 // OPCODE: DW_OP_not
1975 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1977 //----------------------------------------------------------------------
1979 if (stack.empty()) {
1981 error_ptr->SetErrorString(
1982 "Expression stack needs at least 1 item for DW_OP_not.");
1985 if (stack.back().ResolveValue(exe_ctx).OnesComplement() == false) {
1987 error_ptr->SetErrorString("Logical NOT failed.");
1993 //----------------------------------------------------------------------
1996 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1997 // operation on the two, and pushes the result.
1998 //----------------------------------------------------------------------
2000 if (stack.size() < 2) {
2002 error_ptr->SetErrorString(
2003 "Expression stack needs at least 2 items for DW_OP_or.");
2008 stack.back().ResolveValue(exe_ctx) =
2009 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
2013 //----------------------------------------------------------------------
2014 // OPCODE: DW_OP_plus
2016 // DESCRIPTION: pops the top two stack entries, adds them together, and
2017 // pushes the result.
2018 //----------------------------------------------------------------------
2020 if (stack.size() < 2) {
2022 error_ptr->SetErrorString(
2023 "Expression stack needs at least 2 items for DW_OP_plus.");
2028 stack.back().GetScalar() += tmp.GetScalar();
2032 //----------------------------------------------------------------------
2033 // OPCODE: DW_OP_plus_uconst
2035 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
2036 // constant operand and pushes the result.
2037 //----------------------------------------------------------------------
2038 case DW_OP_plus_uconst:
2039 if (stack.empty()) {
2041 error_ptr->SetErrorString(
2042 "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
2045 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
2046 // Implicit conversion from a UINT to a Scalar...
2047 stack.back().GetScalar() += uconst_value;
2048 if (!stack.back().GetScalar().IsValid()) {
2050 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
2056 //----------------------------------------------------------------------
2057 // OPCODE: DW_OP_shl
2059 // DESCRIPTION: pops the top two stack entries, shifts the former
2060 // second entry left by the number of bits specified by the former top
2061 // of the stack, and pushes the result.
2062 //----------------------------------------------------------------------
2064 if (stack.size() < 2) {
2066 error_ptr->SetErrorString(
2067 "Expression stack needs at least 2 items for DW_OP_shl.");
2072 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
2076 //----------------------------------------------------------------------
2077 // OPCODE: DW_OP_shr
2079 // DESCRIPTION: pops the top two stack entries, shifts the former second
2080 // entry right logically (filling with zero bits) by the number of bits
2081 // specified by the former top of the stack, and pushes the result.
2082 //----------------------------------------------------------------------
2084 if (stack.size() < 2) {
2086 error_ptr->SetErrorString(
2087 "Expression stack needs at least 2 items for DW_OP_shr.");
2092 if (stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
2093 tmp.ResolveValue(exe_ctx)) == false) {
2095 error_ptr->SetErrorString("DW_OP_shr failed.");
2101 //----------------------------------------------------------------------
2102 // OPCODE: DW_OP_shra
2104 // DESCRIPTION: pops the top two stack entries, shifts the former second
2105 // entry right arithmetically (divide the magnitude by 2, keep the same
2106 // sign for the result) by the number of bits specified by the former
2107 // top of the stack, and pushes the result.
2108 //----------------------------------------------------------------------
2110 if (stack.size() < 2) {
2112 error_ptr->SetErrorString(
2113 "Expression stack needs at least 2 items for DW_OP_shra.");
2118 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
2122 //----------------------------------------------------------------------
2123 // OPCODE: DW_OP_xor
2125 // DESCRIPTION: pops the top two stack entries, performs the bitwise
2126 // exclusive-or operation on the two, and pushes the result.
2127 //----------------------------------------------------------------------
2129 if (stack.size() < 2) {
2131 error_ptr->SetErrorString(
2132 "Expression stack needs at least 2 items for DW_OP_xor.");
2137 stack.back().ResolveValue(exe_ctx) =
2138 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
2142 //----------------------------------------------------------------------
2143 // OPCODE: DW_OP_skip
2144 // OPERANDS: int16_t
2145 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
2146 // signed integer constant. The 2-byte constant is the number of bytes
2147 // of the DWARF expression to skip forward or backward from the current
2148 // operation, beginning after the 2-byte constant.
2149 //----------------------------------------------------------------------
2151 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
2152 lldb::offset_t new_offset = offset + skip_offset;
2153 if (new_offset >= opcodes_offset && new_offset < end_offset)
2154 offset = new_offset;
2157 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
2162 //----------------------------------------------------------------------
2163 // OPCODE: DW_OP_bra
2164 // OPERANDS: int16_t
2165 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
2166 // signed integer constant. This operation pops the top of stack. If
2167 // the value popped is not the constant 0, the 2-byte constant operand
2168 // is the number of bytes of the DWARF expression to skip forward or
2169 // backward from the current operation, beginning after the 2-byte
2171 //----------------------------------------------------------------------
2173 if (stack.empty()) {
2175 error_ptr->SetErrorString(
2176 "Expression stack needs at least 1 item for DW_OP_bra.");
2181 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
2183 if (tmp.ResolveValue(exe_ctx) != zero) {
2184 lldb::offset_t new_offset = offset + bra_offset;
2185 if (new_offset >= opcodes_offset && new_offset < end_offset)
2186 offset = new_offset;
2189 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
2196 //----------------------------------------------------------------------
2199 // DESCRIPTION: pops the top two stack values, compares using the
2200 // equals (==) operator.
2201 // STACK RESULT: push the constant value 1 onto the stack if the result
2202 // of the operation is true or the constant value 0 if the result of the
2203 // operation is false.
2204 //----------------------------------------------------------------------
2206 if (stack.size() < 2) {
2208 error_ptr->SetErrorString(
2209 "Expression stack needs at least 2 items for DW_OP_eq.");
2214 stack.back().ResolveValue(exe_ctx) =
2215 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
2219 //----------------------------------------------------------------------
2222 // DESCRIPTION: pops the top two stack values, compares using the
2223 // greater than or equal to (>=) operator.
2224 // STACK RESULT: push the constant value 1 onto the stack if the result
2225 // of the operation is true or the constant value 0 if the result of the
2226 // operation is false.
2227 //----------------------------------------------------------------------
2229 if (stack.size() < 2) {
2231 error_ptr->SetErrorString(
2232 "Expression stack needs at least 2 items for DW_OP_ge.");
2237 stack.back().ResolveValue(exe_ctx) =
2238 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
2242 //----------------------------------------------------------------------
2245 // DESCRIPTION: pops the top two stack values, compares using the
2246 // greater than (>) operator.
2247 // STACK RESULT: push the constant value 1 onto the stack if the result
2248 // of the operation is true or the constant value 0 if the result of the
2249 // operation is false.
2250 //----------------------------------------------------------------------
2252 if (stack.size() < 2) {
2254 error_ptr->SetErrorString(
2255 "Expression stack needs at least 2 items for DW_OP_gt.");
2260 stack.back().ResolveValue(exe_ctx) =
2261 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
2265 //----------------------------------------------------------------------
2268 // DESCRIPTION: pops the top two stack values, compares using the
2269 // less than or equal to (<=) operator.
2270 // STACK RESULT: push the constant value 1 onto the stack if the result
2271 // of the operation is true or the constant value 0 if the result of the
2272 // operation is false.
2273 //----------------------------------------------------------------------
2275 if (stack.size() < 2) {
2277 error_ptr->SetErrorString(
2278 "Expression stack needs at least 2 items for DW_OP_le.");
2283 stack.back().ResolveValue(exe_ctx) =
2284 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
2288 //----------------------------------------------------------------------
2291 // DESCRIPTION: pops the top two stack values, compares using the
2292 // less than (<) operator.
2293 // STACK RESULT: push the constant value 1 onto the stack if the result
2294 // of the operation is true or the constant value 0 if the result of the
2295 // operation is false.
2296 //----------------------------------------------------------------------
2298 if (stack.size() < 2) {
2300 error_ptr->SetErrorString(
2301 "Expression stack needs at least 2 items for DW_OP_lt.");
2306 stack.back().ResolveValue(exe_ctx) =
2307 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
2311 //----------------------------------------------------------------------
2314 // DESCRIPTION: pops the top two stack values, compares using the
2315 // not equal (!=) operator.
2316 // STACK RESULT: push the constant value 1 onto the stack if the result
2317 // of the operation is true or the constant value 0 if the result of the
2318 // operation is false.
2319 //----------------------------------------------------------------------
2321 if (stack.size() < 2) {
2323 error_ptr->SetErrorString(
2324 "Expression stack needs at least 2 items for DW_OP_ne.");
2329 stack.back().ResolveValue(exe_ctx) =
2330 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
2334 //----------------------------------------------------------------------
2335 // OPCODE: DW_OP_litn
2337 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
2338 // STACK RESULT: push the unsigned literal constant value onto the top
2340 //----------------------------------------------------------------------
2373 stack.push_back(Scalar(op - DW_OP_lit0));
2376 //----------------------------------------------------------------------
2377 // OPCODE: DW_OP_regN
2379 // DESCRIPTION: Push the value in register n on the top of the stack.
2380 //----------------------------------------------------------------------
2413 reg_num = op - DW_OP_reg0;
2415 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2416 stack.push_back(tmp);
2420 //----------------------------------------------------------------------
2421 // OPCODE: DW_OP_regx
2423 // ULEB128 literal operand that encodes the register.
2424 // DESCRIPTION: Push the value in register on the top of the stack.
2425 //----------------------------------------------------------------------
2427 reg_num = opcodes.GetULEB128(&offset);
2428 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2429 stack.push_back(tmp);
2434 //----------------------------------------------------------------------
2435 // OPCODE: DW_OP_bregN
2437 // SLEB128 offset from register N
2438 // DESCRIPTION: Value is in memory at the address specified by register
2439 // N plus an offset.
2440 //----------------------------------------------------------------------
2472 case DW_OP_breg31: {
2473 reg_num = op - DW_OP_breg0;
2475 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2477 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2478 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2480 stack.push_back(tmp);
2481 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2485 //----------------------------------------------------------------------
2486 // OPCODE: DW_OP_bregx
2488 // ULEB128 literal operand that encodes the register.
2489 // SLEB128 offset from register N
2490 // DESCRIPTION: Value is in memory at the address specified by register
2491 // N plus an offset.
2492 //----------------------------------------------------------------------
2494 reg_num = opcodes.GetULEB128(&offset);
2496 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2498 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2499 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2501 stack.push_back(tmp);
2502 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2511 if (frame->GetFrameBaseValue(value, error_ptr)) {
2512 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2513 value += fbreg_offset;
2514 stack.push_back(value);
2515 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2520 error_ptr->SetErrorString(
2521 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2526 error_ptr->SetErrorStringWithFormat(
2527 "NULL execution context for DW_OP_fbreg.\n");
2533 //----------------------------------------------------------------------
2534 // OPCODE: DW_OP_nop
2536 // DESCRIPTION: A place holder. It has no effect on the location stack
2537 // or any of its values.
2538 //----------------------------------------------------------------------
2542 //----------------------------------------------------------------------
2543 // OPCODE: DW_OP_piece
2545 // ULEB128: byte size of the piece
2546 // DESCRIPTION: The operand describes the size in bytes of the piece of
2547 // the object referenced by the DWARF expression whose result is at the
2548 // top of the stack. If the piece is located in a register, but does not
2549 // occupy the entire register, the placement of the piece within that
2550 // register is defined by the ABI.
2552 // Many compilers store a single variable in sets of registers, or store
2553 // a variable partially in memory and partially in registers.
2554 // DW_OP_piece provides a way of describing how large a part of a
2555 // variable a particular DWARF expression refers to.
2556 //----------------------------------------------------------------------
2558 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2560 if (piece_byte_size > 0) {
2563 if (stack.empty()) {
2564 // In a multi-piece expression, this means that the current piece is
2566 // Fill with zeros for now by resizing the data and appending it
2567 curr_piece.ResizeData(piece_byte_size);
2568 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2569 pieces.AppendDataToHostBuffer(curr_piece);
2572 // Extract the current piece into "curr_piece"
2573 Value curr_piece_source_value(stack.back());
2576 const Value::ValueType curr_piece_source_value_type =
2577 curr_piece_source_value.GetValueType();
2578 switch (curr_piece_source_value_type) {
2579 case Value::eValueTypeLoadAddress:
2581 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2582 lldb::addr_t load_addr =
2583 curr_piece_source_value.GetScalar().ULongLong(
2584 LLDB_INVALID_ADDRESS);
2585 if (process->ReadMemory(
2586 load_addr, curr_piece.GetBuffer().GetBytes(),
2587 piece_byte_size, error) != piece_byte_size) {
2589 error_ptr->SetErrorStringWithFormat(
2590 "failed to read memory DW_OP_piece(%" PRIu64
2591 ") from 0x%" PRIx64,
2592 piece_byte_size, load_addr);
2597 error_ptr->SetErrorStringWithFormat(
2598 "failed to resize the piece memory buffer for "
2599 "DW_OP_piece(%" PRIu64 ")",
2606 case Value::eValueTypeFileAddress:
2607 case Value::eValueTypeHostAddress:
2609 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2610 LLDB_INVALID_ADDRESS);
2611 error_ptr->SetErrorStringWithFormat(
2612 "failed to read memory DW_OP_piece(%" PRIu64
2613 ") from %s address 0x%" PRIx64,
2614 piece_byte_size, curr_piece_source_value.GetValueType() ==
2615 Value::eValueTypeFileAddress
2622 case Value::eValueTypeScalar: {
2623 uint32_t bit_size = piece_byte_size * 8;
2624 uint32_t bit_offset = 0;
2625 if (!curr_piece_source_value.GetScalar().ExtractBitfield(
2626 bit_size, bit_offset)) {
2628 error_ptr->SetErrorStringWithFormat(
2629 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2630 " byte scalar value.",
2632 (uint64_t)curr_piece_source_value.GetScalar()
2636 curr_piece = curr_piece_source_value;
2639 case Value::eValueTypeVector: {
2640 if (curr_piece_source_value.GetVector().length >= piece_byte_size)
2641 curr_piece_source_value.GetVector().length = piece_byte_size;
2644 error_ptr->SetErrorStringWithFormat(
2645 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2646 " byte vector value.",
2648 (uint64_t)curr_piece_source_value.GetVector().length);
2654 // Check if this is the first piece?
2655 if (op_piece_offset == 0) {
2656 // This is the first piece, we should push it back onto the stack so
2658 // pieces will be able to access this piece and add to it
2659 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2661 error_ptr->SetErrorString("failed to append piece data");
2665 // If this is the second or later piece there should be a value on
2667 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2669 error_ptr->SetErrorStringWithFormat(
2670 "DW_OP_piece for offset %" PRIu64
2671 " but top of stack is of size %" PRIu64,
2672 op_piece_offset, pieces.GetBuffer().GetByteSize());
2676 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2678 error_ptr->SetErrorString("failed to append piece data");
2682 op_piece_offset += piece_byte_size;
2687 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2688 if (stack.size() < 1) {
2690 error_ptr->SetErrorString(
2691 "Expression stack needs at least 1 item for DW_OP_bit_piece.");
2694 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2695 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2696 switch (stack.back().GetValueType()) {
2697 case Value::eValueTypeScalar: {
2698 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2699 piece_bit_offset)) {
2701 error_ptr->SetErrorStringWithFormat(
2702 "unable to extract %" PRIu64 " bit value with %" PRIu64
2703 " bit offset from a %" PRIu64 " bit scalar value.",
2704 piece_bit_size, piece_bit_offset,
2705 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2710 case Value::eValueTypeFileAddress:
2711 case Value::eValueTypeLoadAddress:
2712 case Value::eValueTypeHostAddress:
2714 error_ptr->SetErrorStringWithFormat(
2715 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2716 ", bit_offset = %" PRIu64 ") from an addresss value.",
2717 piece_bit_size, piece_bit_offset);
2721 case Value::eValueTypeVector:
2723 error_ptr->SetErrorStringWithFormat(
2724 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2725 ", bit_offset = %" PRIu64 ") from a vector value.",
2726 piece_bit_size, piece_bit_offset);
2733 //----------------------------------------------------------------------
2734 // OPCODE: DW_OP_push_object_address
2736 // DESCRIPTION: Pushes the address of the object currently being
2737 // evaluated as part of evaluation of a user presented expression.
2738 // This object may correspond to an independent variable described by
2739 // its own DIE or it may be a component of an array, structure, or class
2740 // whose address has been dynamically determined by an earlier step
2741 // during user expression evaluation.
2742 //----------------------------------------------------------------------
2743 case DW_OP_push_object_address:
2744 if (object_address_ptr)
2745 stack.push_back(*object_address_ptr);
2748 error_ptr->SetErrorString("DW_OP_push_object_address used without "
2749 "specifying an object address");
2754 //----------------------------------------------------------------------
2755 // OPCODE: DW_OP_call2
2757 // uint16_t compile unit relative offset of a DIE
2758 // DESCRIPTION: Performs subroutine calls during evaluation
2759 // of a DWARF expression. The operand is the 2-byte unsigned offset
2760 // of a debugging information entry in the current compilation unit.
2762 // Operand interpretation is exactly like that for DW_FORM_ref2.
2764 // This operation transfers control of DWARF expression evaluation
2765 // to the DW_AT_location attribute of the referenced DIE. If there is
2766 // no such attribute, then there is no effect. Execution of the DWARF
2767 // expression of a DW_AT_location attribute may add to and/or remove from
2768 // values on the stack. Execution returns to the point following the call
2769 // when the end of the attribute is reached. Values on the stack at the
2770 // time of the call may be used as parameters by the called expression
2771 // and values left on the stack by the called expression may be used as
2772 // return values by prior agreement between the calling and called
2774 //----------------------------------------------------------------------
2777 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2779 //----------------------------------------------------------------------
2780 // OPCODE: DW_OP_call4
2782 // uint32_t compile unit relative offset of a DIE
2783 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2784 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset
2785 // of a debugging information entry in the current compilation unit.
2787 // Operand interpretation DW_OP_call4 is exactly like that for
2790 // This operation transfers control of DWARF expression evaluation
2791 // to the DW_AT_location attribute of the referenced DIE. If there is
2792 // no such attribute, then there is no effect. Execution of the DWARF
2793 // expression of a DW_AT_location attribute may add to and/or remove from
2794 // values on the stack. Execution returns to the point following the call
2795 // when the end of the attribute is reached. Values on the stack at the
2796 // time of the call may be used as parameters by the called expression
2797 // and values left on the stack by the called expression may be used as
2798 // return values by prior agreement between the calling and called
2800 //----------------------------------------------------------------------
2803 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2806 //----------------------------------------------------------------------
2807 // OPCODE: DW_OP_stack_value
2809 // DESCRIPTION: Specifies that the object does not exist in memory but
2810 // rather is a constant value. The value from the top of the stack is
2811 // the value to be used. This is the actual object value and not the
2813 //----------------------------------------------------------------------
2814 case DW_OP_stack_value:
2815 stack.back().SetValueType(Value::eValueTypeScalar);
2818 //----------------------------------------------------------------------
2819 // OPCODE: DW_OP_call_frame_cfa
2821 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2822 // the canonical frame address consistent with the call frame information
2823 // located in .debug_frame (or in the FDEs of the eh_frame section).
2824 //----------------------------------------------------------------------
2825 case DW_OP_call_frame_cfa:
2827 // Note that we don't have to parse FDEs because this DWARF expression
2828 // is commonly evaluated with a valid stack frame.
2829 StackID id = frame->GetStackID();
2830 addr_t cfa = id.GetCallFrameAddress();
2831 if (cfa != LLDB_INVALID_ADDRESS) {
2832 stack.push_back(Scalar(cfa));
2833 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2834 } else if (error_ptr)
2835 error_ptr->SetErrorString("Stack frame does not include a canonical "
2836 "frame address for DW_OP_call_frame_cfa "
2840 error_ptr->SetErrorString("Invalid stack frame in context for "
2841 "DW_OP_call_frame_cfa opcode.");
2846 //----------------------------------------------------------------------
2847 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2848 // opcode, DW_OP_GNU_push_tls_address)
2850 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2851 // an address in the current thread's thread-local storage block,
2852 // and pushes it on the stack.
2853 //----------------------------------------------------------------------
2854 case DW_OP_form_tls_address:
2855 case DW_OP_GNU_push_tls_address: {
2856 if (stack.size() < 1) {
2858 if (op == DW_OP_form_tls_address)
2859 error_ptr->SetErrorString(
2860 "DW_OP_form_tls_address needs an argument.");
2862 error_ptr->SetErrorString(
2863 "DW_OP_GNU_push_tls_address needs an argument.");
2868 if (!exe_ctx || !module_sp) {
2870 error_ptr->SetErrorString("No context to evaluate TLS within.");
2874 Thread *thread = exe_ctx->GetThreadPtr();
2877 error_ptr->SetErrorString("No thread to evaluate TLS within.");
2881 // Lookup the TLS block address for this thread and module.
2882 const addr_t tls_file_addr =
2883 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2884 const addr_t tls_load_addr =
2885 thread->GetThreadLocalData(module_sp, tls_file_addr);
2887 if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2889 error_ptr->SetErrorString(
2890 "No TLS data currently exists for this thread.");
2894 stack.back().GetScalar() = tls_load_addr;
2895 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2898 //----------------------------------------------------------------------
2899 // OPCODE: DW_OP_GNU_addr_index
2901 // ULEB128: index to the .debug_addr section
2902 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2903 // section with the base address specified by the DW_AT_addr_base
2904 // attribute and the 0 based index is the ULEB128 encoded index.
2905 //----------------------------------------------------------------------
2906 case DW_OP_GNU_addr_index: {
2909 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2910 "compile unit being specified");
2913 uint64_t index = opcodes.GetULEB128(&offset);
2914 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2915 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2916 lldb::offset_t offset = addr_base + index * index_size;
2918 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
2919 &offset, index_size);
2920 stack.push_back(Scalar(value));
2921 stack.back().SetValueType(Value::eValueTypeFileAddress);
2924 //----------------------------------------------------------------------
2925 // OPCODE: DW_OP_GNU_const_index
2927 // ULEB128: index to the .debug_addr section
2928 // DESCRIPTION: Pushes an constant with the size of a machine address to
2929 // the stack from the .debug_addr section with the base address specified
2930 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2932 //----------------------------------------------------------------------
2933 case DW_OP_GNU_const_index: {
2936 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2937 "compile unit being specified");
2940 uint64_t index = opcodes.GetULEB128(&offset);
2941 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2942 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2943 lldb::offset_t offset = addr_base + index * index_size;
2944 const DWARFDataExtractor &debug_addr =
2945 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data();
2946 switch (index_size) {
2948 stack.push_back(Scalar(debug_addr.GetU32(&offset)));
2951 stack.push_back(Scalar(debug_addr.GetU64(&offset)));
2954 assert(false && "Unhandled index size");
2961 log->Printf("Unhandled opcode %s in DWARFExpression.",
2962 DW_OP_value_to_name(op));
2967 if (stack.empty()) {
2968 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2969 // or DW_OP_bit_piece opcodes
2970 if (pieces.GetBuffer().GetByteSize()) {
2974 error_ptr->SetErrorString("Stack empty after evaluation.");
2978 if (log && log->GetVerbose()) {
2979 size_t count = stack.size();
2980 log->Printf("Stack after operation has %" PRIu64 " values:",
2982 for (size_t i = 0; i < count; ++i) {
2983 StreamString new_value;
2984 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2985 stack[i].Dump(&new_value);
2986 log->Printf(" %s", new_value.GetData());
2989 result = stack.back();
2991 return true; // Return true on success
2994 size_t DWARFExpression::LocationListSize(const DWARFCompileUnit *dwarf_cu,
2995 const DataExtractor &debug_loc_data,
2996 lldb::offset_t offset) {
2997 const lldb::offset_t debug_loc_offset = offset;
2998 while (debug_loc_data.ValidOffset(offset)) {
2999 lldb::addr_t start_addr = LLDB_INVALID_ADDRESS;
3000 lldb::addr_t end_addr = LLDB_INVALID_ADDRESS;
3001 if (!AddressRangeForLocationListEntry(dwarf_cu, debug_loc_data, &offset,
3002 start_addr, end_addr))
3005 if (start_addr == 0 && end_addr == 0)
3008 uint16_t loc_length = debug_loc_data.GetU16(&offset);
3009 offset += loc_length;
3012 if (offset > debug_loc_offset)
3013 return offset - debug_loc_offset;
3017 bool DWARFExpression::AddressRangeForLocationListEntry(
3018 const DWARFCompileUnit *dwarf_cu, const DataExtractor &debug_loc_data,
3019 lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc) {
3020 if (!debug_loc_data.ValidOffset(*offset_ptr))
3023 switch (dwarf_cu->GetSymbolFileDWARF()->GetLocationListFormat()) {
3024 case NonLocationList:
3026 case RegularLocationList:
3027 low_pc = debug_loc_data.GetAddress(offset_ptr);
3028 high_pc = debug_loc_data.GetAddress(offset_ptr);
3030 case SplitDwarfLocationList:
3031 switch (debug_loc_data.GetU8(offset_ptr)) {
3032 case DW_LLE_end_of_list:
3034 case DW_LLE_startx_endx: {
3035 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3036 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3037 index = debug_loc_data.GetULEB128(offset_ptr);
3038 high_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3041 case DW_LLE_startx_length: {
3042 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3043 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3044 uint32_t length = debug_loc_data.GetU32(offset_ptr);
3045 high_pc = low_pc + length;
3049 // Not supported entry type
3053 assert(false && "Not supported location list type");
3057 static bool print_dwarf_exp_op(Stream &s, const DataExtractor &data,
3058 lldb::offset_t *offset_ptr, int address_size,
3059 int dwarf_ref_size) {
3060 uint8_t opcode = data.GetU8(offset_ptr);
3061 DRC_class opcode_class;
3067 opcode_class = DW_OP_value_to_class(opcode) & (~DRC_DWARFv3);
3069 s.Printf("%s ", DW_OP_value_to_name(opcode));
3071 /* Does this take zero parameters? If so we can shortcut this function. */
3072 if (opcode_class == DRC_ZEROOPERANDS)
3075 if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_bregx) {
3076 uint = data.GetULEB128(offset_ptr);
3077 sint = data.GetSLEB128(offset_ptr);
3078 s.Printf("%" PRIu64 " %" PRIi64, uint, sint);
3081 if (opcode_class != DRC_ONEOPERAND) {
3082 s.Printf("UNKNOWN OP %u", opcode);
3088 size = address_size;
3158 case DW_OP_deref_size:
3159 case DW_OP_xderef_size:
3172 case DW_OP_call_ref:
3173 size = dwarf_ref_size;
3176 case DW_OP_plus_uconst:
3178 case DW_OP_GNU_addr_index:
3179 case DW_OP_GNU_const_index:
3183 s.Printf("UNKNOWN ONE-OPERAND OPCODE, #%u", opcode);
3189 sint = (int8_t)data.GetU8(offset_ptr);
3190 s.Printf("%+" PRIi64, sint);
3193 sint = (int16_t)data.GetU16(offset_ptr);
3194 s.Printf("%+" PRIi64, sint);
3197 sint = (int32_t)data.GetU32(offset_ptr);
3198 s.Printf("%+" PRIi64, sint);
3201 sint = (int64_t)data.GetU64(offset_ptr);
3202 s.Printf("%+" PRIi64, sint);
3205 sint = data.GetSLEB128(offset_ptr);
3206 s.Printf("%+" PRIi64, sint);
3209 uint = data.GetU8(offset_ptr);
3210 s.Printf("0x%2.2" PRIx64, uint);
3213 uint = data.GetU16(offset_ptr);
3214 s.Printf("0x%4.4" PRIx64, uint);
3217 uint = data.GetU32(offset_ptr);
3218 s.Printf("0x%8.8" PRIx64, uint);
3221 uint = data.GetU64(offset_ptr);
3222 s.Printf("0x%16.16" PRIx64, uint);
3225 uint = data.GetULEB128(offset_ptr);
3226 s.Printf("0x%" PRIx64, uint);
3233 bool DWARFExpression::PrintDWARFExpression(Stream &s, const DataExtractor &data,
3234 int address_size, int dwarf_ref_size,
3235 bool location_expression) {
3237 lldb::offset_t offset = 0;
3238 while (data.ValidOffset(offset)) {
3239 if (location_expression && op_count > 0)
3243 if (!print_dwarf_exp_op(s, data, &offset, address_size, dwarf_ref_size))
3251 void DWARFExpression::PrintDWARFLocationList(
3252 Stream &s, const DWARFCompileUnit *cu, const DataExtractor &debug_loc_data,
3253 lldb::offset_t offset) {
3254 uint64_t start_addr, end_addr;
3255 uint32_t addr_size = DWARFCompileUnit::GetAddressByteSize(cu);
3256 s.SetAddressByteSize(DWARFCompileUnit::GetAddressByteSize(cu));
3257 dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
3258 while (debug_loc_data.ValidOffset(offset)) {
3259 start_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3260 end_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3262 if (start_addr == 0 && end_addr == 0)
3265 s.PutCString("\n ");
3268 s.AddressRange(start_addr + base_addr, end_addr + base_addr,
3269 cu->GetAddressByteSize(), NULL, ": ");
3270 uint32_t loc_length = debug_loc_data.GetU16(&offset);
3272 DataExtractor locationData(debug_loc_data, offset, loc_length);
3273 PrintDWARFExpression(s, locationData, addr_size, 4, false);
3274 offset += loc_length;
3278 bool DWARFExpression::GetOpAndEndOffsets(StackFrame &frame,
3279 lldb::offset_t &op_offset,
3280 lldb::offset_t &end_offset) {
3281 SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction);
3286 addr_t loclist_base_file_addr =
3287 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
3288 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS) {
3292 addr_t pc_file_addr = frame.GetFrameCodeAddress().GetFileAddress();
3293 lldb::offset_t opcodes_offset, opcodes_length;
3294 if (!GetLocation(loclist_base_file_addr, pc_file_addr, opcodes_offset,
3299 if (opcodes_length == 0) {
3303 op_offset = opcodes_offset;
3304 end_offset = opcodes_offset + opcodes_length;
3308 bool DWARFExpression::MatchesOperand(StackFrame &frame,
3309 const Instruction::Operand &operand) {
3310 using namespace OperandMatchers;
3312 lldb::offset_t op_offset;
3313 lldb::offset_t end_offset;
3314 if (!GetOpAndEndOffsets(frame, op_offset, end_offset)) {
3318 if (!m_data.ValidOffset(op_offset) || op_offset >= end_offset) {
3322 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
3327 DataExtractor opcodes = m_data;
3328 uint8_t opcode = opcodes.GetU8(&op_offset);
3330 if (opcode == DW_OP_fbreg) {
3331 int64_t offset = opcodes.GetSLEB128(&op_offset);
3333 DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr);
3338 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
3339 return fb_expr->MatchesOperand(frame, child);
3343 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3344 recurse)(operand)) {
3348 return MatchUnaryOp(
3349 MatchOpType(Instruction::Operand::Type::Dereference),
3350 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3351 MatchImmOp(offset), recurse))(operand);
3354 bool dereference = false;
3355 const RegisterInfo *reg = nullptr;
3358 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
3359 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
3360 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
3361 offset = opcodes.GetSLEB128(&op_offset);
3362 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
3363 } else if (opcode == DW_OP_regx) {
3364 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3365 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3366 } else if (opcode == DW_OP_bregx) {
3367 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3368 offset = opcodes.GetSLEB128(&op_offset);
3369 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3380 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3381 MatchRegOp(*reg))(operand)) {
3385 return MatchUnaryOp(
3386 MatchOpType(Instruction::Operand::Type::Dereference),
3387 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3389 MatchImmOp(offset)))(operand);
3391 return MatchRegOp(*reg)(operand);