blob: 7797205ffb1625a39fd0694cdc0075bca5cd14c8 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- DWARFExpression.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Expression/DWARFExpression.h"
11
12#include <vector>
13
Greg Clayton2fc93ea2011-11-13 04:15:56 +000014#include "lldb/Core/DataEncoder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/dwarf.h"
16#include "lldb/Core/Log.h"
Greg Clayton7349bd92011-05-09 20:18:18 +000017#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/StreamString.h"
19#include "lldb/Core/Scalar.h"
20#include "lldb/Core/Value.h"
Greg Clayton016a95e2010-09-14 02:20:48 +000021#include "lldb/Core/VMRange.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23#include "lldb/Expression/ClangExpressionDeclMap.h"
24#include "lldb/Expression/ClangExpressionVariable.h"
25
Greg Clayton7fb56d02011-02-01 01:31:41 +000026#include "lldb/Host/Endian.h"
Jim Inghamf220d592011-12-01 03:01:30 +000027#include "lldb/Host/Host.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
29#include "lldb/lldb-private-log.h"
30
Greg Claytone1a916a2010-07-21 22:12:05 +000031#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Symbol/ClangASTContext.h"
33#include "lldb/Symbol/Type.h"
34
Greg Claytonafacd142011-09-02 01:15:17 +000035#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/ExecutionContext.h"
37#include "lldb/Target/Process.h"
38#include "lldb/Target/RegisterContext.h"
39#include "lldb/Target/StackFrame.h"
40
41using namespace lldb;
42using namespace lldb_private;
43
44const char *
45DW_OP_value_to_name (uint32_t val)
46{
47 static char invalid[100];
48 switch (val) {
49 case 0x03: return "DW_OP_addr";
50 case 0x06: return "DW_OP_deref";
51 case 0x08: return "DW_OP_const1u";
52 case 0x09: return "DW_OP_const1s";
53 case 0x0a: return "DW_OP_const2u";
54 case 0x0b: return "DW_OP_const2s";
55 case 0x0c: return "DW_OP_const4u";
56 case 0x0d: return "DW_OP_const4s";
57 case 0x0e: return "DW_OP_const8u";
58 case 0x0f: return "DW_OP_const8s";
59 case 0x10: return "DW_OP_constu";
60 case 0x11: return "DW_OP_consts";
61 case 0x12: return "DW_OP_dup";
62 case 0x13: return "DW_OP_drop";
63 case 0x14: return "DW_OP_over";
64 case 0x15: return "DW_OP_pick";
65 case 0x16: return "DW_OP_swap";
66 case 0x17: return "DW_OP_rot";
67 case 0x18: return "DW_OP_xderef";
68 case 0x19: return "DW_OP_abs";
69 case 0x1a: return "DW_OP_and";
70 case 0x1b: return "DW_OP_div";
71 case 0x1c: return "DW_OP_minus";
72 case 0x1d: return "DW_OP_mod";
73 case 0x1e: return "DW_OP_mul";
74 case 0x1f: return "DW_OP_neg";
75 case 0x20: return "DW_OP_not";
76 case 0x21: return "DW_OP_or";
77 case 0x22: return "DW_OP_plus";
78 case 0x23: return "DW_OP_plus_uconst";
79 case 0x24: return "DW_OP_shl";
80 case 0x25: return "DW_OP_shr";
81 case 0x26: return "DW_OP_shra";
82 case 0x27: return "DW_OP_xor";
83 case 0x2f: return "DW_OP_skip";
84 case 0x28: return "DW_OP_bra";
85 case 0x29: return "DW_OP_eq";
86 case 0x2a: return "DW_OP_ge";
87 case 0x2b: return "DW_OP_gt";
88 case 0x2c: return "DW_OP_le";
89 case 0x2d: return "DW_OP_lt";
90 case 0x2e: return "DW_OP_ne";
91 case 0x30: return "DW_OP_lit0";
92 case 0x31: return "DW_OP_lit1";
93 case 0x32: return "DW_OP_lit2";
94 case 0x33: return "DW_OP_lit3";
95 case 0x34: return "DW_OP_lit4";
96 case 0x35: return "DW_OP_lit5";
97 case 0x36: return "DW_OP_lit6";
98 case 0x37: return "DW_OP_lit7";
99 case 0x38: return "DW_OP_lit8";
100 case 0x39: return "DW_OP_lit9";
101 case 0x3a: return "DW_OP_lit10";
102 case 0x3b: return "DW_OP_lit11";
103 case 0x3c: return "DW_OP_lit12";
104 case 0x3d: return "DW_OP_lit13";
105 case 0x3e: return "DW_OP_lit14";
106 case 0x3f: return "DW_OP_lit15";
107 case 0x40: return "DW_OP_lit16";
108 case 0x41: return "DW_OP_lit17";
109 case 0x42: return "DW_OP_lit18";
110 case 0x43: return "DW_OP_lit19";
111 case 0x44: return "DW_OP_lit20";
112 case 0x45: return "DW_OP_lit21";
113 case 0x46: return "DW_OP_lit22";
114 case 0x47: return "DW_OP_lit23";
115 case 0x48: return "DW_OP_lit24";
116 case 0x49: return "DW_OP_lit25";
117 case 0x4a: return "DW_OP_lit26";
118 case 0x4b: return "DW_OP_lit27";
119 case 0x4c: return "DW_OP_lit28";
120 case 0x4d: return "DW_OP_lit29";
121 case 0x4e: return "DW_OP_lit30";
122 case 0x4f: return "DW_OP_lit31";
123 case 0x50: return "DW_OP_reg0";
124 case 0x51: return "DW_OP_reg1";
125 case 0x52: return "DW_OP_reg2";
126 case 0x53: return "DW_OP_reg3";
127 case 0x54: return "DW_OP_reg4";
128 case 0x55: return "DW_OP_reg5";
129 case 0x56: return "DW_OP_reg6";
130 case 0x57: return "DW_OP_reg7";
131 case 0x58: return "DW_OP_reg8";
132 case 0x59: return "DW_OP_reg9";
133 case 0x5a: return "DW_OP_reg10";
134 case 0x5b: return "DW_OP_reg11";
135 case 0x5c: return "DW_OP_reg12";
136 case 0x5d: return "DW_OP_reg13";
137 case 0x5e: return "DW_OP_reg14";
138 case 0x5f: return "DW_OP_reg15";
139 case 0x60: return "DW_OP_reg16";
140 case 0x61: return "DW_OP_reg17";
141 case 0x62: return "DW_OP_reg18";
142 case 0x63: return "DW_OP_reg19";
143 case 0x64: return "DW_OP_reg20";
144 case 0x65: return "DW_OP_reg21";
145 case 0x66: return "DW_OP_reg22";
146 case 0x67: return "DW_OP_reg23";
147 case 0x68: return "DW_OP_reg24";
148 case 0x69: return "DW_OP_reg25";
149 case 0x6a: return "DW_OP_reg26";
150 case 0x6b: return "DW_OP_reg27";
151 case 0x6c: return "DW_OP_reg28";
152 case 0x6d: return "DW_OP_reg29";
153 case 0x6e: return "DW_OP_reg30";
154 case 0x6f: return "DW_OP_reg31";
155 case 0x70: return "DW_OP_breg0";
156 case 0x71: return "DW_OP_breg1";
157 case 0x72: return "DW_OP_breg2";
158 case 0x73: return "DW_OP_breg3";
159 case 0x74: return "DW_OP_breg4";
160 case 0x75: return "DW_OP_breg5";
161 case 0x76: return "DW_OP_breg6";
162 case 0x77: return "DW_OP_breg7";
163 case 0x78: return "DW_OP_breg8";
164 case 0x79: return "DW_OP_breg9";
165 case 0x7a: return "DW_OP_breg10";
166 case 0x7b: return "DW_OP_breg11";
167 case 0x7c: return "DW_OP_breg12";
168 case 0x7d: return "DW_OP_breg13";
169 case 0x7e: return "DW_OP_breg14";
170 case 0x7f: return "DW_OP_breg15";
171 case 0x80: return "DW_OP_breg16";
172 case 0x81: return "DW_OP_breg17";
173 case 0x82: return "DW_OP_breg18";
174 case 0x83: return "DW_OP_breg19";
175 case 0x84: return "DW_OP_breg20";
176 case 0x85: return "DW_OP_breg21";
177 case 0x86: return "DW_OP_breg22";
178 case 0x87: return "DW_OP_breg23";
179 case 0x88: return "DW_OP_breg24";
180 case 0x89: return "DW_OP_breg25";
181 case 0x8a: return "DW_OP_breg26";
182 case 0x8b: return "DW_OP_breg27";
183 case 0x8c: return "DW_OP_breg28";
184 case 0x8d: return "DW_OP_breg29";
185 case 0x8e: return "DW_OP_breg30";
186 case 0x8f: return "DW_OP_breg31";
187 case 0x90: return "DW_OP_regx";
188 case 0x91: return "DW_OP_fbreg";
189 case 0x92: return "DW_OP_bregx";
190 case 0x93: return "DW_OP_piece";
191 case 0x94: return "DW_OP_deref_size";
192 case 0x95: return "DW_OP_xderef_size";
193 case 0x96: return "DW_OP_nop";
194 case 0x97: return "DW_OP_push_object_address";
195 case 0x98: return "DW_OP_call2";
196 case 0x99: return "DW_OP_call4";
197 case 0x9a: return "DW_OP_call_ref";
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000198// case DW_OP_APPLE_array_ref: return "DW_OP_APPLE_array_ref";
199// case DW_OP_APPLE_extern: return "DW_OP_APPLE_extern";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200 case DW_OP_APPLE_uninit: return "DW_OP_APPLE_uninit";
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000201// case DW_OP_APPLE_assign: return "DW_OP_APPLE_assign";
202// case DW_OP_APPLE_address_of: return "DW_OP_APPLE_address_of";
203// case DW_OP_APPLE_value_of: return "DW_OP_APPLE_value_of";
204// case DW_OP_APPLE_deref_type: return "DW_OP_APPLE_deref_type";
205// case DW_OP_APPLE_expr_local: return "DW_OP_APPLE_expr_local";
206// case DW_OP_APPLE_constf: return "DW_OP_APPLE_constf";
207// case DW_OP_APPLE_scalar_cast: return "DW_OP_APPLE_scalar_cast";
208// case DW_OP_APPLE_clang_cast: return "DW_OP_APPLE_clang_cast";
209// case DW_OP_APPLE_clear: return "DW_OP_APPLE_clear";
210// case DW_OP_APPLE_error: return "DW_OP_APPLE_error";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 default:
212 snprintf (invalid, sizeof(invalid), "Unknown DW_OP constant: 0x%x", val);
213 return invalid;
214 }
215}
216
217
218//----------------------------------------------------------------------
219// DWARFExpression constructor
220//----------------------------------------------------------------------
221DWARFExpression::DWARFExpression() :
222 m_data(),
223 m_reg_kind (eRegisterKindDWARF),
Greg Clayton1a65ae12011-01-25 23:55:37 +0000224 m_loclist_slide (LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225{
226}
227
228DWARFExpression::DWARFExpression(const DWARFExpression& rhs) :
229 m_data(rhs.m_data),
230 m_reg_kind (rhs.m_reg_kind),
Greg Clayton1a65ae12011-01-25 23:55:37 +0000231 m_loclist_slide(rhs.m_loclist_slide)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232{
233}
234
235
Greg Claytonc7bece562013-01-25 18:06:21 +0000236DWARFExpression::DWARFExpression(const DataExtractor& data, lldb::offset_t data_offset, lldb::offset_t data_length) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 m_data(data, data_offset, data_length),
238 m_reg_kind (eRegisterKindDWARF),
Greg Clayton1a65ae12011-01-25 23:55:37 +0000239 m_loclist_slide(LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241}
242
243//----------------------------------------------------------------------
244// Destructor
245//----------------------------------------------------------------------
246DWARFExpression::~DWARFExpression()
247{
248}
249
250
251bool
252DWARFExpression::IsValid() const
253{
254 return m_data.GetByteSize() > 0;
255}
256
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257void
Greg Clayton016a95e2010-09-14 02:20:48 +0000258DWARFExpression::SetOpcodeData (const DataExtractor& data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259{
260 m_data = data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261}
262
263void
Greg Claytonc7bece562013-01-25 18:06:21 +0000264DWARFExpression::CopyOpcodeData (const DataExtractor& data, lldb::offset_t data_offset, lldb::offset_t data_length)
Greg Clayton8179bca2012-08-10 23:33:27 +0000265{
266 const uint8_t *bytes = data.PeekData(data_offset, data_length);
Greg Clayton7f4c7432012-08-11 00:49:14 +0000267 if (bytes)
268 {
269 m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length)));
270 m_data.SetByteOrder(data.GetByteOrder());
271 m_data.SetAddressByteSize(data.GetAddressByteSize());
272 }
Greg Clayton8179bca2012-08-10 23:33:27 +0000273}
274
275void
Greg Claytonc7bece562013-01-25 18:06:21 +0000276DWARFExpression::SetOpcodeData (const DataExtractor& data, lldb::offset_t data_offset, lldb::offset_t data_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277{
278 m_data.SetData(data, data_offset, data_length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279}
280
281void
Greg Claytonc7bece562013-01-25 18:06:21 +0000282DWARFExpression::DumpLocation (Stream *s, lldb::offset_t offset, lldb::offset_t length, lldb::DescriptionLevel level, ABI *abi) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283{
284 if (!m_data.ValidOffsetForDataOfSize(offset, length))
285 return;
Greg Claytonc7bece562013-01-25 18:06:21 +0000286 const lldb::offset_t start_offset = offset;
287 const lldb::offset_t end_offset = offset + length;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 while (m_data.ValidOffset(offset) && offset < end_offset)
289 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000290 const lldb::offset_t op_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 const uint8_t op = m_data.GetU8(&offset);
292
293 switch (level)
294 {
Greg Claytonc982c762010-07-09 20:39:50 +0000295 default:
296 break;
297
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 case lldb::eDescriptionLevelBrief:
299 if (offset > start_offset)
300 s->PutChar(' ');
301 break;
302
303 case lldb::eDescriptionLevelFull:
304 case lldb::eDescriptionLevelVerbose:
305 if (offset > start_offset)
306 s->EOL();
307 s->Indent();
308 if (level == lldb::eDescriptionLevelFull)
309 break;
310 // Fall through for verbose and print offset and DW_OP prefix..
Greg Claytonc7bece562013-01-25 18:06:21 +0000311 s->Printf("0x%8.8" PRIx64 ": %s", op_offset, op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312 break;
313 }
314
315 switch (op)
316 {
Greg Claytonc749eb82011-07-11 05:12:02 +0000317 case DW_OP_addr: *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") "; break; // 0x03 1 address
318 case DW_OP_deref: *s << "DW_OP_deref"; break; // 0x06
319 case DW_OP_const1u: s->Printf("DW_OP_const1u(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x08 1 1-byte constant
320 case DW_OP_const1s: s->Printf("DW_OP_const1s(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x09 1 1-byte constant
321 case DW_OP_const2u: s->Printf("DW_OP_const2u(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0a 1 2-byte constant
322 case DW_OP_const2s: s->Printf("DW_OP_const2s(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0b 1 2-byte constant
323 case DW_OP_const4u: s->Printf("DW_OP_const4u(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0c 1 4-byte constant
324 case DW_OP_const4s: s->Printf("DW_OP_const4s(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0d 1 4-byte constant
Daniel Malead01b2952012-11-29 21:49:15 +0000325 case DW_OP_const8u: s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset)); break; // 0x0e 1 8-byte constant
326 case DW_OP_const8s: s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset)); break; // 0x0f 1 8-byte constant
327 case DW_OP_constu: s->Printf("DW_OP_constu(0x%" PRIx64 ") ", m_data.GetULEB128(&offset)); break; // 0x10 1 ULEB128 constant
328 case DW_OP_consts: s->Printf("DW_OP_consts(0x%" PRId64 ") ", m_data.GetSLEB128(&offset)); break; // 0x11 1 SLEB128 constant
Greg Claytonc749eb82011-07-11 05:12:02 +0000329 case DW_OP_dup: s->PutCString("DW_OP_dup"); break; // 0x12
330 case DW_OP_drop: s->PutCString("DW_OP_drop"); break; // 0x13
331 case DW_OP_over: s->PutCString("DW_OP_over"); break; // 0x14
332 case DW_OP_pick: s->Printf("DW_OP_pick(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x15 1 1-byte stack index
333 case DW_OP_swap: s->PutCString("DW_OP_swap"); break; // 0x16
334 case DW_OP_rot: s->PutCString("DW_OP_rot"); break; // 0x17
335 case DW_OP_xderef: s->PutCString("DW_OP_xderef"); break; // 0x18
336 case DW_OP_abs: s->PutCString("DW_OP_abs"); break; // 0x19
337 case DW_OP_and: s->PutCString("DW_OP_and"); break; // 0x1a
338 case DW_OP_div: s->PutCString("DW_OP_div"); break; // 0x1b
339 case DW_OP_minus: s->PutCString("DW_OP_minus"); break; // 0x1c
340 case DW_OP_mod: s->PutCString("DW_OP_mod"); break; // 0x1d
341 case DW_OP_mul: s->PutCString("DW_OP_mul"); break; // 0x1e
342 case DW_OP_neg: s->PutCString("DW_OP_neg"); break; // 0x1f
343 case DW_OP_not: s->PutCString("DW_OP_not"); break; // 0x20
344 case DW_OP_or: s->PutCString("DW_OP_or"); break; // 0x21
345 case DW_OP_plus: s->PutCString("DW_OP_plus"); break; // 0x22
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
Daniel Malead01b2952012-11-29 21:49:15 +0000347 s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ") ", m_data.GetULEB128(&offset));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 break;
349
Greg Claytonc749eb82011-07-11 05:12:02 +0000350 case DW_OP_shl: s->PutCString("DW_OP_shl"); break; // 0x24
351 case DW_OP_shr: s->PutCString("DW_OP_shr"); break; // 0x25
352 case DW_OP_shra: s->PutCString("DW_OP_shra"); break; // 0x26
353 case DW_OP_xor: s->PutCString("DW_OP_xor"); break; // 0x27
354 case DW_OP_skip: s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x2f 1 signed 2-byte constant
355 case DW_OP_bra: s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x28 1 signed 2-byte constant
356 case DW_OP_eq: s->PutCString("DW_OP_eq"); break; // 0x29
357 case DW_OP_ge: s->PutCString("DW_OP_ge"); break; // 0x2a
358 case DW_OP_gt: s->PutCString("DW_OP_gt"); break; // 0x2b
359 case DW_OP_le: s->PutCString("DW_OP_le"); break; // 0x2c
360 case DW_OP_lt: s->PutCString("DW_OP_lt"); break; // 0x2d
361 case DW_OP_ne: s->PutCString("DW_OP_ne"); break; // 0x2e
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362
363 case DW_OP_lit0: // 0x30
364 case DW_OP_lit1: // 0x31
365 case DW_OP_lit2: // 0x32
366 case DW_OP_lit3: // 0x33
367 case DW_OP_lit4: // 0x34
368 case DW_OP_lit5: // 0x35
369 case DW_OP_lit6: // 0x36
370 case DW_OP_lit7: // 0x37
371 case DW_OP_lit8: // 0x38
372 case DW_OP_lit9: // 0x39
373 case DW_OP_lit10: // 0x3A
374 case DW_OP_lit11: // 0x3B
375 case DW_OP_lit12: // 0x3C
376 case DW_OP_lit13: // 0x3D
377 case DW_OP_lit14: // 0x3E
378 case DW_OP_lit15: // 0x3F
379 case DW_OP_lit16: // 0x40
380 case DW_OP_lit17: // 0x41
381 case DW_OP_lit18: // 0x42
382 case DW_OP_lit19: // 0x43
383 case DW_OP_lit20: // 0x44
384 case DW_OP_lit21: // 0x45
385 case DW_OP_lit22: // 0x46
386 case DW_OP_lit23: // 0x47
387 case DW_OP_lit24: // 0x48
388 case DW_OP_lit25: // 0x49
389 case DW_OP_lit26: // 0x4A
390 case DW_OP_lit27: // 0x4B
391 case DW_OP_lit28: // 0x4C
392 case DW_OP_lit29: // 0x4D
393 case DW_OP_lit30: // 0x4E
Greg Claytonc749eb82011-07-11 05:12:02 +0000394 case DW_OP_lit31: s->Printf("DW_OP_lit%i", op - DW_OP_lit0); break; // 0x4f
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395
396 case DW_OP_reg0: // 0x50
397 case DW_OP_reg1: // 0x51
398 case DW_OP_reg2: // 0x52
399 case DW_OP_reg3: // 0x53
400 case DW_OP_reg4: // 0x54
401 case DW_OP_reg5: // 0x55
402 case DW_OP_reg6: // 0x56
403 case DW_OP_reg7: // 0x57
404 case DW_OP_reg8: // 0x58
405 case DW_OP_reg9: // 0x59
406 case DW_OP_reg10: // 0x5A
407 case DW_OP_reg11: // 0x5B
408 case DW_OP_reg12: // 0x5C
409 case DW_OP_reg13: // 0x5D
410 case DW_OP_reg14: // 0x5E
411 case DW_OP_reg15: // 0x5F
412 case DW_OP_reg16: // 0x60
413 case DW_OP_reg17: // 0x61
414 case DW_OP_reg18: // 0x62
415 case DW_OP_reg19: // 0x63
416 case DW_OP_reg20: // 0x64
417 case DW_OP_reg21: // 0x65
418 case DW_OP_reg22: // 0x66
419 case DW_OP_reg23: // 0x67
420 case DW_OP_reg24: // 0x68
421 case DW_OP_reg25: // 0x69
422 case DW_OP_reg26: // 0x6A
423 case DW_OP_reg27: // 0x6B
424 case DW_OP_reg28: // 0x6C
425 case DW_OP_reg29: // 0x6D
426 case DW_OP_reg30: // 0x6E
Greg Claytonafacd142011-09-02 01:15:17 +0000427 case DW_OP_reg31: // 0x6F
428 {
429 uint32_t reg_num = op - DW_OP_reg0;
430 if (abi)
431 {
432 RegisterInfo reg_info;
433 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info))
434 {
435 if (reg_info.name)
436 {
437 s->PutCString (reg_info.name);
438 break;
439 }
440 else if (reg_info.alt_name)
441 {
442 s->PutCString (reg_info.alt_name);
443 break;
444 }
445 }
446 }
447 s->Printf("DW_OP_reg%u", reg_num); break;
448 }
449 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450
451 case DW_OP_breg0:
452 case DW_OP_breg1:
453 case DW_OP_breg2:
454 case DW_OP_breg3:
455 case DW_OP_breg4:
456 case DW_OP_breg5:
457 case DW_OP_breg6:
458 case DW_OP_breg7:
459 case DW_OP_breg8:
460 case DW_OP_breg9:
461 case DW_OP_breg10:
462 case DW_OP_breg11:
463 case DW_OP_breg12:
464 case DW_OP_breg13:
465 case DW_OP_breg14:
466 case DW_OP_breg15:
467 case DW_OP_breg16:
468 case DW_OP_breg17:
469 case DW_OP_breg18:
470 case DW_OP_breg19:
471 case DW_OP_breg20:
472 case DW_OP_breg21:
473 case DW_OP_breg22:
474 case DW_OP_breg23:
475 case DW_OP_breg24:
476 case DW_OP_breg25:
477 case DW_OP_breg26:
478 case DW_OP_breg27:
479 case DW_OP_breg28:
480 case DW_OP_breg29:
481 case DW_OP_breg30:
Greg Claytonafacd142011-09-02 01:15:17 +0000482 case DW_OP_breg31:
483 {
484 uint32_t reg_num = op - DW_OP_breg0;
485 int64_t reg_offset = m_data.GetSLEB128(&offset);
486 if (abi)
487 {
488 RegisterInfo reg_info;
489 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info))
490 {
491 if (reg_info.name)
492 {
Daniel Malead01b2952012-11-29 21:49:15 +0000493 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
Greg Claytonafacd142011-09-02 01:15:17 +0000494 break;
495 }
496 else if (reg_info.alt_name)
497 {
Daniel Malead01b2952012-11-29 21:49:15 +0000498 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
Greg Claytonafacd142011-09-02 01:15:17 +0000499 break;
500 }
501 }
502 }
Daniel Malead01b2952012-11-29 21:49:15 +0000503 s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset);
Greg Claytonafacd142011-09-02 01:15:17 +0000504 }
505 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506
507 case DW_OP_regx: // 0x90 1 ULEB128 register
Greg Claytonafacd142011-09-02 01:15:17 +0000508 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000509 uint32_t reg_num = m_data.GetULEB128(&offset);
Greg Claytonafacd142011-09-02 01:15:17 +0000510 if (abi)
511 {
512 RegisterInfo reg_info;
513 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info))
514 {
515 if (reg_info.name)
516 {
517 s->PutCString (reg_info.name);
518 break;
519 }
520 else if (reg_info.alt_name)
521 {
522 s->PutCString (reg_info.alt_name);
523 break;
524 }
525 }
526 }
Greg Claytonc7bece562013-01-25 18:06:21 +0000527 s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num); break;
Greg Claytonafacd142011-09-02 01:15:17 +0000528 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529 break;
530 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
Daniel Malead01b2952012-11-29 21:49:15 +0000531 s->Printf("DW_OP_fbreg(%" PRIi64 ")",m_data.GetSLEB128(&offset));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 break;
533 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
Greg Claytonafacd142011-09-02 01:15:17 +0000534 {
535 uint32_t reg_num = m_data.GetULEB128(&offset);
536 int64_t reg_offset = m_data.GetSLEB128(&offset);
537 if (abi)
538 {
539 RegisterInfo reg_info;
540 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info))
541 {
542 if (reg_info.name)
543 {
Daniel Malead01b2952012-11-29 21:49:15 +0000544 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
Greg Claytonafacd142011-09-02 01:15:17 +0000545 break;
546 }
547 else if (reg_info.alt_name)
548 {
Daniel Malead01b2952012-11-29 21:49:15 +0000549 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
Greg Claytonafacd142011-09-02 01:15:17 +0000550 break;
551 }
552 }
553 }
Greg Claytonc7bece562013-01-25 18:06:21 +0000554 s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num, reg_offset);
Greg Claytonafacd142011-09-02 01:15:17 +0000555 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 break;
557 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
Daniel Malead01b2952012-11-29 21:49:15 +0000558 s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 break;
560 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
Greg Claytonc749eb82011-07-11 05:12:02 +0000561 s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 break;
563 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
Greg Claytonc749eb82011-07-11 05:12:02 +0000564 s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 break;
Greg Claytonc749eb82011-07-11 05:12:02 +0000566 case DW_OP_nop: s->PutCString("DW_OP_nop"); break; // 0x96
567 case DW_OP_push_object_address: s->PutCString("DW_OP_push_object_address"); break; // 0x97 DWARF3
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
Greg Claytonc749eb82011-07-11 05:12:02 +0000569 s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570 break;
571 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
Greg Claytonc749eb82011-07-11 05:12:02 +0000572 s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 break;
574 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
Daniel Malead01b2952012-11-29 21:49:15 +0000575 s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 break;
577// case DW_OP_form_tls_address: s << "form_tls_address"; break; // 0x9b DWARF3
578// case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break; // 0x9c DWARF3
579// case DW_OP_bit_piece: // 0x9d DWARF3 2
Greg Claytonc749eb82011-07-11 05:12:02 +0000580// s->Printf("DW_OP_bit_piece(0x%x, 0x%x)", m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581// break;
Greg Claytonc749eb82011-07-11 05:12:02 +0000582// case DW_OP_lo_user: s->PutCString("DW_OP_lo_user"); break; // 0xe0
583// case DW_OP_hi_user: s->PutCString("DW_OP_hi_user"); break; // 0xff
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000584// case DW_OP_APPLE_extern:
Daniel Malead01b2952012-11-29 21:49:15 +0000585// s->Printf("DW_OP_APPLE_extern(%" PRIu64 ")", m_data.GetULEB128(&offset));
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000586// break;
587// case DW_OP_APPLE_array_ref:
588// s->PutCString("DW_OP_APPLE_array_ref");
589// break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590 case DW_OP_APPLE_uninit:
Greg Claytonc749eb82011-07-11 05:12:02 +0000591 s->PutCString("DW_OP_APPLE_uninit"); // 0xF0
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592 break;
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000593// case DW_OP_APPLE_assign: // 0xF1 - pops value off and assigns it to second item on stack (2nd item must have assignable context)
594// s->PutCString("DW_OP_APPLE_assign");
595// break;
596// case DW_OP_APPLE_address_of: // 0xF2 - gets the address of the top stack item (top item must be a variable, or have value_type that is an address already)
597// s->PutCString("DW_OP_APPLE_address_of");
598// break;
599// case DW_OP_APPLE_value_of: // 0xF3 - pops the value off the stack and pushes the value of that object (top item must be a variable, or expression local)
600// s->PutCString("DW_OP_APPLE_value_of");
601// break;
602// case DW_OP_APPLE_deref_type: // 0xF4 - gets the address of the top stack item (top item must be a variable, or a clang type)
603// s->PutCString("DW_OP_APPLE_deref_type");
604// break;
605// case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression local index
Daniel Malead01b2952012-11-29 21:49:15 +0000606// s->Printf("DW_OP_APPLE_expr_local(%" PRIu64 ")", m_data.GetULEB128(&offset));
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000607// break;
608// case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size, followed by constant float data
609// {
610// uint8_t float_length = m_data.GetU8(&offset);
611// s->Printf("DW_OP_APPLE_constf(<%u> ", float_length);
612// m_data.Dump(s, offset, eFormatHex, float_length, 1, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
613// s->PutChar(')');
614// // Consume the float data
615// m_data.GetData(&offset, float_length);
616// }
617// break;
618// case DW_OP_APPLE_scalar_cast:
619// s->Printf("DW_OP_APPLE_scalar_cast(%s)", Scalar::GetValueTypeAsCString ((Scalar::Type)m_data.GetU8(&offset)));
620// break;
621// case DW_OP_APPLE_clang_cast:
622// {
623// clang::Type *clang_type = (clang::Type *)m_data.GetMaxU64(&offset, sizeof(void*));
624// s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type);
625// }
626// break;
627// case DW_OP_APPLE_clear:
628// s->PutCString("DW_OP_APPLE_clear");
629// break;
630// case DW_OP_APPLE_error: // 0xFF - Stops expression evaluation and returns an error (no args)
631// s->PutCString("DW_OP_APPLE_error");
632// break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 }
634 }
635}
636
637void
Greg Clayton016a95e2010-09-14 02:20:48 +0000638DWARFExpression::SetLocationListSlide (addr_t slide)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639{
Greg Clayton016a95e2010-09-14 02:20:48 +0000640 m_loclist_slide = slide;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641}
642
643int
644DWARFExpression::GetRegisterKind ()
645{
646 return m_reg_kind;
647}
648
649void
Greg Claytonafacd142011-09-02 01:15:17 +0000650DWARFExpression::SetRegisterKind (RegisterKind reg_kind)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651{
652 m_reg_kind = reg_kind;
653}
654
655bool
656DWARFExpression::IsLocationList() const
657{
Greg Clayton016a95e2010-09-14 02:20:48 +0000658 return m_loclist_slide != LLDB_INVALID_ADDRESS;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659}
660
661void
Greg Claytonafacd142011-09-02 01:15:17 +0000662DWARFExpression::GetDescription (Stream *s, lldb::DescriptionLevel level, addr_t location_list_base_addr, ABI *abi) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663{
664 if (IsLocationList())
665 {
666 // We have a location list
Greg Claytonc7bece562013-01-25 18:06:21 +0000667 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 uint32_t count = 0;
Greg Clayton016a95e2010-09-14 02:20:48 +0000669 addr_t curr_base_addr = location_list_base_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 while (m_data.ValidOffset(offset))
671 {
672 lldb::addr_t begin_addr_offset = m_data.GetAddress(&offset);
673 lldb::addr_t end_addr_offset = m_data.GetAddress(&offset);
674 if (begin_addr_offset < end_addr_offset)
675 {
676 if (count > 0)
677 s->PutCString(", ");
Greg Clayton016a95e2010-09-14 02:20:48 +0000678 VMRange addr_range(curr_base_addr + begin_addr_offset, curr_base_addr + end_addr_offset);
679 addr_range.Dump(s, 0, 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000680 s->PutChar('{');
Greg Claytonc7bece562013-01-25 18:06:21 +0000681 lldb::offset_t location_length = m_data.GetU16(&offset);
Greg Claytonafacd142011-09-02 01:15:17 +0000682 DumpLocation (s, offset, location_length, level, abi);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 s->PutChar('}');
684 offset += location_length;
685 }
686 else if (begin_addr_offset == 0 && end_addr_offset == 0)
687 {
688 // The end of the location list is marked by both the start and end offset being zero
689 break;
690 }
691 else
692 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000693 if ((m_data.GetAddressByteSize() == 4 && (begin_addr_offset == UINT32_MAX)) ||
694 (m_data.GetAddressByteSize() == 8 && (begin_addr_offset == UINT64_MAX)))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 {
Greg Clayton016a95e2010-09-14 02:20:48 +0000696 curr_base_addr = end_addr_offset + location_list_base_addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697 // We have a new base address
698 if (count > 0)
699 s->PutCString(", ");
700 *s << "base_addr = " << end_addr_offset;
701 }
702 }
703
704 count++;
705 }
706 }
707 else
708 {
709 // We have a normal location that contains DW_OP location opcodes
Greg Claytonafacd142011-09-02 01:15:17 +0000710 DumpLocation (s, 0, m_data.GetByteSize(), level, abi);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 }
712}
713
714static bool
715ReadRegisterValueAsScalar
716(
Greg Clayton7349bd92011-05-09 20:18:18 +0000717 RegisterContext *reg_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 uint32_t reg_kind,
719 uint32_t reg_num,
720 Error *error_ptr,
721 Value &value
722)
723{
Greg Clayton7349bd92011-05-09 20:18:18 +0000724 if (reg_ctx == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 {
Jason Molenda2d107dd2010-11-20 01:28:30 +0000726 if (error_ptr)
727 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 }
729 else
730 {
Greg Clayton7349bd92011-05-09 20:18:18 +0000731 uint32_t native_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
Jason Molenda2d107dd2010-11-20 01:28:30 +0000732 if (native_reg == LLDB_INVALID_REGNUM)
733 {
734 if (error_ptr)
735 error_ptr->SetErrorStringWithFormat("Unable to convert register kind=%u reg_num=%u to a native register number.\n", reg_kind, reg_num);
736 }
737 else
738 {
Greg Clayton7349bd92011-05-09 20:18:18 +0000739 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(native_reg);
740 RegisterValue reg_value;
741 if (reg_ctx->ReadRegister (reg_info, reg_value))
742 {
743 if (reg_value.GetScalarValue(value.GetScalar()))
744 {
745 value.SetValueType (Value::eValueTypeScalar);
Greg Clayton007d5be2011-05-30 00:49:24 +0000746 value.SetContext (Value::eContextTypeRegisterInfo,
747 const_cast<RegisterInfo *>(reg_info));
Greg Clayton7349bd92011-05-09 20:18:18 +0000748 if (error_ptr)
749 error_ptr->Clear();
750 return true;
751 }
752 else
753 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000754 // If we get this error, then we need to implement a value
755 // buffer in the dwarf expression evaluation function...
Greg Clayton7349bd92011-05-09 20:18:18 +0000756 if (error_ptr)
Greg Clayton007d5be2011-05-30 00:49:24 +0000757 error_ptr->SetErrorStringWithFormat ("register %s can't be converted to a scalar value",
758 reg_info->name);
Greg Clayton7349bd92011-05-09 20:18:18 +0000759 }
760 }
761 else
762 {
763 if (error_ptr)
Greg Clayton007d5be2011-05-30 00:49:24 +0000764 error_ptr->SetErrorStringWithFormat("register %s is not available", reg_info->name);
Greg Clayton7349bd92011-05-09 20:18:18 +0000765 }
Jason Molenda2d107dd2010-11-20 01:28:30 +0000766 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767 }
768 return false;
769}
770
Greg Clayton016a95e2010-09-14 02:20:48 +0000771//bool
772//DWARFExpression::LocationListContainsLoadAddress (Process* process, const Address &addr) const
773//{
774// return LocationListContainsLoadAddress(process, addr.GetLoadAddress(process));
775//}
776//
777//bool
778//DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t load_addr) const
779//{
780// if (load_addr == LLDB_INVALID_ADDRESS)
781// return false;
782//
783// if (IsLocationList())
784// {
Greg Claytonc7bece562013-01-25 18:06:21 +0000785// lldb::offset_t offset = 0;
Greg Clayton016a95e2010-09-14 02:20:48 +0000786//
787// addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
788//
789// if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
790// return false;
791//
792// while (m_data.ValidOffset(offset))
793// {
794// // We need to figure out what the value is for the location.
795// addr_t lo_pc = m_data.GetAddress(&offset);
796// addr_t hi_pc = m_data.GetAddress(&offset);
797// if (lo_pc == 0 && hi_pc == 0)
798// break;
799// else
800// {
801// lo_pc += loc_list_base_addr;
802// hi_pc += loc_list_base_addr;
803//
804// if (lo_pc <= load_addr && load_addr < hi_pc)
805// return true;
806//
807// offset += m_data.GetU16(&offset);
808// }
809// }
810// }
811// return false;
812//}
Greg Clayton9da7bd02010-08-24 21:05:24 +0000813
Greg Claytonc7bece562013-01-25 18:06:21 +0000814static offset_t
815GetOpcodeDataSize (const DataExtractor &data, const lldb::offset_t data_offset, const uint8_t op)
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000816{
Greg Claytonc7bece562013-01-25 18:06:21 +0000817 lldb::offset_t offset = data_offset;
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000818 switch (op)
819 {
820 case DW_OP_addr:
821 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
822 return data.GetAddressByteSize();
823
824 // Opcodes with no arguments
825 case DW_OP_deref: // 0x06
826 case DW_OP_dup: // 0x12
827 case DW_OP_drop: // 0x13
828 case DW_OP_over: // 0x14
829 case DW_OP_swap: // 0x16
830 case DW_OP_rot: // 0x17
831 case DW_OP_xderef: // 0x18
832 case DW_OP_abs: // 0x19
833 case DW_OP_and: // 0x1a
834 case DW_OP_div: // 0x1b
835 case DW_OP_minus: // 0x1c
836 case DW_OP_mod: // 0x1d
837 case DW_OP_mul: // 0x1e
838 case DW_OP_neg: // 0x1f
839 case DW_OP_not: // 0x20
840 case DW_OP_or: // 0x21
841 case DW_OP_plus: // 0x22
842 case DW_OP_shl: // 0x24
843 case DW_OP_shr: // 0x25
844 case DW_OP_shra: // 0x26
845 case DW_OP_xor: // 0x27
846 case DW_OP_eq: // 0x29
847 case DW_OP_ge: // 0x2a
848 case DW_OP_gt: // 0x2b
849 case DW_OP_le: // 0x2c
850 case DW_OP_lt: // 0x2d
851 case DW_OP_ne: // 0x2e
852 case DW_OP_lit0: // 0x30
853 case DW_OP_lit1: // 0x31
854 case DW_OP_lit2: // 0x32
855 case DW_OP_lit3: // 0x33
856 case DW_OP_lit4: // 0x34
857 case DW_OP_lit5: // 0x35
858 case DW_OP_lit6: // 0x36
859 case DW_OP_lit7: // 0x37
860 case DW_OP_lit8: // 0x38
861 case DW_OP_lit9: // 0x39
862 case DW_OP_lit10: // 0x3A
863 case DW_OP_lit11: // 0x3B
864 case DW_OP_lit12: // 0x3C
865 case DW_OP_lit13: // 0x3D
866 case DW_OP_lit14: // 0x3E
867 case DW_OP_lit15: // 0x3F
868 case DW_OP_lit16: // 0x40
869 case DW_OP_lit17: // 0x41
870 case DW_OP_lit18: // 0x42
871 case DW_OP_lit19: // 0x43
872 case DW_OP_lit20: // 0x44
873 case DW_OP_lit21: // 0x45
874 case DW_OP_lit22: // 0x46
875 case DW_OP_lit23: // 0x47
876 case DW_OP_lit24: // 0x48
877 case DW_OP_lit25: // 0x49
878 case DW_OP_lit26: // 0x4A
879 case DW_OP_lit27: // 0x4B
880 case DW_OP_lit28: // 0x4C
881 case DW_OP_lit29: // 0x4D
882 case DW_OP_lit30: // 0x4E
883 case DW_OP_lit31: // 0x4f
884 case DW_OP_reg0: // 0x50
885 case DW_OP_reg1: // 0x51
886 case DW_OP_reg2: // 0x52
887 case DW_OP_reg3: // 0x53
888 case DW_OP_reg4: // 0x54
889 case DW_OP_reg5: // 0x55
890 case DW_OP_reg6: // 0x56
891 case DW_OP_reg7: // 0x57
892 case DW_OP_reg8: // 0x58
893 case DW_OP_reg9: // 0x59
894 case DW_OP_reg10: // 0x5A
895 case DW_OP_reg11: // 0x5B
896 case DW_OP_reg12: // 0x5C
897 case DW_OP_reg13: // 0x5D
898 case DW_OP_reg14: // 0x5E
899 case DW_OP_reg15: // 0x5F
900 case DW_OP_reg16: // 0x60
901 case DW_OP_reg17: // 0x61
902 case DW_OP_reg18: // 0x62
903 case DW_OP_reg19: // 0x63
904 case DW_OP_reg20: // 0x64
905 case DW_OP_reg21: // 0x65
906 case DW_OP_reg22: // 0x66
907 case DW_OP_reg23: // 0x67
908 case DW_OP_reg24: // 0x68
909 case DW_OP_reg25: // 0x69
910 case DW_OP_reg26: // 0x6A
911 case DW_OP_reg27: // 0x6B
912 case DW_OP_reg28: // 0x6C
913 case DW_OP_reg29: // 0x6D
914 case DW_OP_reg30: // 0x6E
915 case DW_OP_reg31: // 0x6F
916 case DW_OP_nop: // 0x96
917 case DW_OP_push_object_address: // 0x97 DWARF3
918 case DW_OP_form_tls_address: // 0x9b DWARF3
919 case DW_OP_call_frame_cfa: // 0x9c DWARF3
Greg Claytoneaeaf6f2011-12-01 04:06:15 +0000920 case DW_OP_stack_value: // 0x9f DWARF4
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000921 return 0;
922
923 // Opcodes with a single 1 byte arguments
924 case DW_OP_const1u: // 0x08 1 1-byte constant
925 case DW_OP_const1s: // 0x09 1 1-byte constant
926 case DW_OP_pick: // 0x15 1 1-byte stack index
927 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
928 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
929 return 1;
930
931 // Opcodes with a single 2 byte arguments
932 case DW_OP_const2u: // 0x0a 1 2-byte constant
933 case DW_OP_const2s: // 0x0b 1 2-byte constant
934 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
935 case DW_OP_bra: // 0x28 1 signed 2-byte constant
936 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
937 return 2;
938
939 // Opcodes with a single 4 byte arguments
940 case DW_OP_const4u: // 0x0c 1 4-byte constant
941 case DW_OP_const4s: // 0x0d 1 4-byte constant
942 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
943 return 4;
944
945 // Opcodes with a single 8 byte arguments
946 case DW_OP_const8u: // 0x0e 1 8-byte constant
947 case DW_OP_const8s: // 0x0f 1 8-byte constant
948 return 8;
949
950 // All opcodes that have a single ULEB (signed or unsigned) argument
951 case DW_OP_constu: // 0x10 1 ULEB128 constant
952 case DW_OP_consts: // 0x11 1 SLEB128 constant
953 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
954 case DW_OP_breg0: // 0x70 1 ULEB128 register
955 case DW_OP_breg1: // 0x71 1 ULEB128 register
956 case DW_OP_breg2: // 0x72 1 ULEB128 register
957 case DW_OP_breg3: // 0x73 1 ULEB128 register
958 case DW_OP_breg4: // 0x74 1 ULEB128 register
959 case DW_OP_breg5: // 0x75 1 ULEB128 register
960 case DW_OP_breg6: // 0x76 1 ULEB128 register
961 case DW_OP_breg7: // 0x77 1 ULEB128 register
962 case DW_OP_breg8: // 0x78 1 ULEB128 register
963 case DW_OP_breg9: // 0x79 1 ULEB128 register
964 case DW_OP_breg10: // 0x7a 1 ULEB128 register
965 case DW_OP_breg11: // 0x7b 1 ULEB128 register
966 case DW_OP_breg12: // 0x7c 1 ULEB128 register
967 case DW_OP_breg13: // 0x7d 1 ULEB128 register
968 case DW_OP_breg14: // 0x7e 1 ULEB128 register
969 case DW_OP_breg15: // 0x7f 1 ULEB128 register
970 case DW_OP_breg16: // 0x80 1 ULEB128 register
971 case DW_OP_breg17: // 0x81 1 ULEB128 register
972 case DW_OP_breg18: // 0x82 1 ULEB128 register
973 case DW_OP_breg19: // 0x83 1 ULEB128 register
974 case DW_OP_breg20: // 0x84 1 ULEB128 register
975 case DW_OP_breg21: // 0x85 1 ULEB128 register
976 case DW_OP_breg22: // 0x86 1 ULEB128 register
977 case DW_OP_breg23: // 0x87 1 ULEB128 register
978 case DW_OP_breg24: // 0x88 1 ULEB128 register
979 case DW_OP_breg25: // 0x89 1 ULEB128 register
980 case DW_OP_breg26: // 0x8a 1 ULEB128 register
981 case DW_OP_breg27: // 0x8b 1 ULEB128 register
982 case DW_OP_breg28: // 0x8c 1 ULEB128 register
983 case DW_OP_breg29: // 0x8d 1 ULEB128 register
984 case DW_OP_breg30: // 0x8e 1 ULEB128 register
985 case DW_OP_breg31: // 0x8f 1 ULEB128 register
986 case DW_OP_regx: // 0x90 1 ULEB128 register
987 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
988 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
989 data.Skip_LEB128(&offset);
990 return offset - data_offset;
991
Greg Claytoneaeaf6f2011-12-01 04:06:15 +0000992 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000993 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
994 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
995 data.Skip_LEB128(&offset);
996 data.Skip_LEB128(&offset);
997 return offset - data_offset;
Greg Claytoneaeaf6f2011-12-01 04:06:15 +0000998
999 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size (DWARF4)
1000 {
1001 uint64_t block_len = data.Skip_LEB128(&offset);
1002 offset += block_len;
1003 return offset - data_offset;
1004 }
1005
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001006 default:
Greg Clayton96c09682012-01-04 22:56:43 +00001007 break;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001008 }
Greg Claytonc7bece562013-01-25 18:06:21 +00001009 return LLDB_INVALID_OFFSET;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001010}
1011
Greg Clayton9422dd62013-03-04 21:46:16 +00001012lldb::addr_t
1013DWARFExpression::GetLocation_DW_OP_addr (uint32_t op_addr_idx, bool &error) const
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001014{
Greg Clayton96c09682012-01-04 22:56:43 +00001015 error = false;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001016 if (IsLocationList())
Greg Clayton9422dd62013-03-04 21:46:16 +00001017 return LLDB_INVALID_ADDRESS;
Greg Claytonc7bece562013-01-25 18:06:21 +00001018 lldb::offset_t offset = 0;
Greg Clayton9422dd62013-03-04 21:46:16 +00001019 uint32_t curr_op_addr_idx = 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001020 while (m_data.ValidOffset(offset))
1021 {
1022 const uint8_t op = m_data.GetU8(&offset);
1023
1024 if (op == DW_OP_addr)
1025 {
Greg Clayton9422dd62013-03-04 21:46:16 +00001026 const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
1027 if (curr_op_addr_idx == op_addr_idx)
1028 return op_file_addr;
1029 else
1030 ++curr_op_addr_idx;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001031 }
1032 else
1033 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001034 const offset_t op_arg_size = GetOpcodeDataSize (m_data, offset, op);
1035 if (op_arg_size == LLDB_INVALID_OFFSET)
Greg Clayton96c09682012-01-04 22:56:43 +00001036 {
1037 error = true;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001038 break;
Greg Clayton96c09682012-01-04 22:56:43 +00001039 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001040 offset += op_arg_size;
1041 }
1042 }
Greg Clayton9422dd62013-03-04 21:46:16 +00001043 return LLDB_INVALID_ADDRESS;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001044}
1045
1046bool
1047DWARFExpression::Update_DW_OP_addr (lldb::addr_t file_addr)
1048{
1049 if (IsLocationList())
1050 return false;
Greg Claytonc7bece562013-01-25 18:06:21 +00001051 lldb::offset_t offset = 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001052 while (m_data.ValidOffset(offset))
1053 {
1054 const uint8_t op = m_data.GetU8(&offset);
1055
1056 if (op == DW_OP_addr)
1057 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001058 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001059 // We have to make a copy of the data as we don't know if this
1060 // data is from a read only memory mapped buffer, so we duplicate
1061 // all of the data first, then modify it, and if all goes well,
1062 // we then replace the data for this expression
1063
1064 // So first we copy the data into a heap buffer
Greg Clayton7b0992d2013-04-18 22:45:39 +00001065 std::unique_ptr<DataBufferHeap> head_data_ap (new DataBufferHeap (m_data.GetDataStart(),
Greg Claytone01e07b2013-04-18 18:10:51 +00001066 m_data.GetByteSize()));
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001067
1068 // Make en encoder so we can write the address into the buffer using
1069 // the correct byte order (endianness)
1070 DataEncoder encoder (head_data_ap->GetBytes(),
1071 head_data_ap->GetByteSize(),
1072 m_data.GetByteOrder(),
1073 addr_byte_size);
1074
1075 // Replace the address in the new buffer
1076 if (encoder.PutMaxU64 (offset, addr_byte_size, file_addr) == UINT32_MAX)
1077 return false;
1078
1079 // All went well, so now we can reset the data using a shared
1080 // pointer to the heap data so "m_data" will now correctly
1081 // manage the heap data.
1082 m_data.SetData (DataBufferSP (head_data_ap.release()));
1083 return true;
1084 }
1085 else
1086 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001087 const offset_t op_arg_size = GetOpcodeDataSize (m_data, offset, op);
1088 if (op_arg_size == LLDB_INVALID_OFFSET)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00001089 break;
1090 offset += op_arg_size;
1091 }
1092 }
1093 return false;
1094}
1095
Greg Clayton9da7bd02010-08-24 21:05:24 +00001096bool
Greg Clayton016a95e2010-09-14 02:20:48 +00001097DWARFExpression::LocationListContainsAddress (lldb::addr_t loclist_base_addr, lldb::addr_t addr) const
Greg Clayton9da7bd02010-08-24 21:05:24 +00001098{
Greg Clayton016a95e2010-09-14 02:20:48 +00001099 if (addr == LLDB_INVALID_ADDRESS)
Greg Clayton9da7bd02010-08-24 21:05:24 +00001100 return false;
1101
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102 if (IsLocationList())
1103 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001104 lldb::offset_t offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105
Greg Clayton016a95e2010-09-14 02:20:48 +00001106 if (loclist_base_addr == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107 return false;
1108
1109 while (m_data.ValidOffset(offset))
1110 {
1111 // We need to figure out what the value is for the location.
1112 addr_t lo_pc = m_data.GetAddress(&offset);
1113 addr_t hi_pc = m_data.GetAddress(&offset);
1114 if (lo_pc == 0 && hi_pc == 0)
1115 break;
1116 else
1117 {
Greg Clayton016a95e2010-09-14 02:20:48 +00001118 lo_pc += loclist_base_addr - m_loclist_slide;
1119 hi_pc += loclist_base_addr - m_loclist_slide;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120
Greg Clayton016a95e2010-09-14 02:20:48 +00001121 if (lo_pc <= addr && addr < hi_pc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 return true;
1123
1124 offset += m_data.GetU16(&offset);
1125 }
1126 }
1127 }
1128 return false;
1129}
Greg Clayton9da7bd02010-08-24 21:05:24 +00001130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131bool
Greg Claytonc7bece562013-01-25 18:06:21 +00001132DWARFExpression::GetLocation (addr_t base_addr, addr_t pc, lldb::offset_t &offset, lldb::offset_t &length)
Greg Claytonc749eb82011-07-11 05:12:02 +00001133{
1134 offset = 0;
1135 if (!IsLocationList())
1136 {
1137 length = m_data.GetByteSize();
1138 return true;
1139 }
1140
1141 if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS)
1142 {
1143 addr_t curr_base_addr = base_addr;
1144
1145 while (m_data.ValidOffset(offset))
1146 {
1147 // We need to figure out what the value is for the location.
1148 addr_t lo_pc = m_data.GetAddress(&offset);
1149 addr_t hi_pc = m_data.GetAddress(&offset);
1150 if (lo_pc == 0 && hi_pc == 0)
1151 {
1152 break;
1153 }
1154 else
1155 {
1156 lo_pc += curr_base_addr - m_loclist_slide;
1157 hi_pc += curr_base_addr - m_loclist_slide;
1158
1159 length = m_data.GetU16(&offset);
1160
1161 if (length > 0 && lo_pc <= pc && pc < hi_pc)
1162 return true;
1163
1164 offset += length;
1165 }
1166 }
1167 }
Greg Claytonc7bece562013-01-25 18:06:21 +00001168 offset = LLDB_INVALID_OFFSET;
Greg Claytonc749eb82011-07-11 05:12:02 +00001169 length = 0;
1170 return false;
1171}
1172
1173bool
1174DWARFExpression::DumpLocationForAddress (Stream *s,
1175 lldb::DescriptionLevel level,
1176 addr_t base_addr,
Greg Claytonafacd142011-09-02 01:15:17 +00001177 addr_t address,
1178 ABI *abi)
Greg Claytonc749eb82011-07-11 05:12:02 +00001179{
Greg Claytonc7bece562013-01-25 18:06:21 +00001180 lldb::offset_t offset = 0;
1181 lldb::offset_t length = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001182
1183 if (GetLocation (base_addr, address, offset, length))
1184 {
1185 if (length > 0)
1186 {
Greg Claytonafacd142011-09-02 01:15:17 +00001187 DumpLocation(s, offset, length, level, abi);
Greg Claytonc749eb82011-07-11 05:12:02 +00001188 return true;
1189 }
1190 }
1191 return false;
1192}
1193
1194bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195DWARFExpression::Evaluate
1196(
1197 ExecutionContextScope *exe_scope,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001198 ClangExpressionVariableList *expr_locals,
1199 ClangExpressionDeclMap *decl_map,
Greg Clayton016a95e2010-09-14 02:20:48 +00001200 lldb::addr_t loclist_base_load_addr,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201 const Value* initial_value_ptr,
1202 Value& result,
1203 Error *error_ptr
1204) const
1205{
1206 ExecutionContext exe_ctx (exe_scope);
Greg Clayton57ee3062013-07-11 22:46:58 +00001207 return Evaluate(&exe_ctx, expr_locals, decl_map, NULL, loclist_base_load_addr, initial_value_ptr, result, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208}
1209
1210bool
1211DWARFExpression::Evaluate
1212(
1213 ExecutionContext *exe_ctx,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001214 ClangExpressionVariableList *expr_locals,
1215 ClangExpressionDeclMap *decl_map,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001216 RegisterContext *reg_ctx,
Greg Clayton016a95e2010-09-14 02:20:48 +00001217 lldb::addr_t loclist_base_load_addr,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218 const Value* initial_value_ptr,
1219 Value& result,
1220 Error *error_ptr
1221) const
1222{
1223 if (IsLocationList())
1224 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001225 lldb::offset_t offset = 0;
Jason Molenda2d107dd2010-11-20 01:28:30 +00001226 addr_t pc;
Greg Claytonc14ee322011-09-22 04:58:26 +00001227 StackFrame *frame = NULL;
Jason Molenda2d107dd2010-11-20 01:28:30 +00001228 if (reg_ctx)
1229 pc = reg_ctx->GetPC();
1230 else
Greg Claytonc14ee322011-09-22 04:58:26 +00001231 {
1232 frame = exe_ctx->GetFramePtr();
Sean Callananb7de9602012-03-10 03:03:46 +00001233 if (!frame)
1234 return false;
1235 RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
1236 if (!reg_ctx_sp)
1237 return false;
1238 pc = reg_ctx_sp->GetPC();
Greg Claytonc14ee322011-09-22 04:58:26 +00001239 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240
Greg Clayton016a95e2010-09-14 02:20:48 +00001241 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001242 {
Greg Clayton016a95e2010-09-14 02:20:48 +00001243 if (pc == LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 {
Greg Clayton016a95e2010-09-14 02:20:48 +00001245 if (error_ptr)
1246 error_ptr->SetErrorString("Invalid PC in frame.");
1247 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001248 }
Greg Clayton016a95e2010-09-14 02:20:48 +00001249
1250 addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
1251
1252 while (m_data.ValidOffset(offset))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253 {
Greg Clayton016a95e2010-09-14 02:20:48 +00001254 // We need to figure out what the value is for the location.
1255 addr_t lo_pc = m_data.GetAddress(&offset);
1256 addr_t hi_pc = m_data.GetAddress(&offset);
1257 if (lo_pc == 0 && hi_pc == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 {
Greg Clayton016a95e2010-09-14 02:20:48 +00001259 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001260 }
Greg Clayton016a95e2010-09-14 02:20:48 +00001261 else
1262 {
1263 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
1264 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
1265
1266 uint16_t length = m_data.GetU16(&offset);
1267
1268 if (length > 0 && lo_pc <= pc && pc < hi_pc)
1269 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001270 return DWARFExpression::Evaluate (exe_ctx, expr_locals, decl_map, reg_ctx, m_data, offset, length, m_reg_kind, initial_value_ptr, result, error_ptr);
Greg Clayton016a95e2010-09-14 02:20:48 +00001271 }
1272 offset += length;
1273 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274 }
1275 }
1276 if (error_ptr)
Greg Clayton007d5be2011-05-30 00:49:24 +00001277 error_ptr->SetErrorString ("variable not available");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001278 return false;
1279 }
1280
1281 // Not a location list, just a single expression.
Greg Clayton57ee3062013-07-11 22:46:58 +00001282 return DWARFExpression::Evaluate (exe_ctx, expr_locals, decl_map, reg_ctx, m_data, 0, m_data.GetByteSize(), m_reg_kind, initial_value_ptr, result, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283}
1284
1285
1286
1287bool
1288DWARFExpression::Evaluate
1289(
1290 ExecutionContext *exe_ctx,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291 ClangExpressionVariableList *expr_locals,
1292 ClangExpressionDeclMap *decl_map,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001293 RegisterContext *reg_ctx,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001294 const DataExtractor& opcodes,
Greg Claytonc7bece562013-01-25 18:06:21 +00001295 const lldb::offset_t opcodes_offset,
1296 const lldb::offset_t opcodes_length,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297 const uint32_t reg_kind,
1298 const Value* initial_value_ptr,
1299 Value& result,
1300 Error *error_ptr
1301)
1302{
Greg Clayton9e9f2192013-05-17 00:55:28 +00001303
1304 if (opcodes_length == 0)
1305 {
1306 if (error_ptr)
1307 error_ptr->SetErrorString ("no location, value may have been optimized out");
1308 return false;
1309 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310 std::vector<Value> stack;
1311
Greg Claytonc14ee322011-09-22 04:58:26 +00001312 Process *process = NULL;
1313 StackFrame *frame = NULL;
1314
1315 if (exe_ctx)
1316 {
1317 process = exe_ctx->GetProcessPtr();
1318 frame = exe_ctx->GetFramePtr();
1319 }
1320 if (reg_ctx == NULL && frame)
1321 reg_ctx = frame->GetRegisterContext().get();
Jason Molenda2d107dd2010-11-20 01:28:30 +00001322
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001323 if (initial_value_ptr)
1324 stack.push_back(*initial_value_ptr);
1325
Greg Claytonc7bece562013-01-25 18:06:21 +00001326 lldb::offset_t offset = opcodes_offset;
1327 const lldb::offset_t end_offset = opcodes_offset + opcodes_length;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328 Value tmp;
1329 uint32_t reg_num;
1330
1331 // Make sure all of the data is available in opcodes.
1332 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length))
1333 {
1334 if (error_ptr)
Greg Clayton9e9f2192013-05-17 00:55:28 +00001335 error_ptr->SetErrorString ("invalid offset and/or length for opcodes buffer.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336 return false;
1337 }
Greg Clayton5160ce52013-03-27 23:08:40 +00001338 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339
1340
1341 while (opcodes.ValidOffset(offset) && offset < end_offset)
1342 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001343 const lldb::offset_t op_offset = offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001344 const uint8_t op = opcodes.GetU8(&offset);
1345
Sean Callanan7dd98122011-10-14 20:34:21 +00001346 if (log && log->GetVerbose())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001348 size_t count = stack.size();
Jason Molendafd54b362011-09-20 21:44:10 +00001349 log->Printf("Stack before operation has %lu values:", count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001350 for (size_t i=0; i<count; ++i)
1351 {
1352 StreamString new_value;
Daniel Malead01b2952012-11-29 21:49:15 +00001353 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 stack[i].Dump(&new_value);
Sean Callananf06ba8d2010-06-23 00:47:48 +00001355 log->Printf(" %s", new_value.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356 }
Greg Claytonc7bece562013-01-25 18:06:21 +00001357 log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 }
1359 switch (op)
1360 {
1361 //----------------------------------------------------------------------
1362 // The DW_OP_addr operation has a single operand that encodes a machine
1363 // address and whose size is the size of an address on the target machine.
1364 //----------------------------------------------------------------------
1365 case DW_OP_addr:
Greg Clayton644247c2011-07-07 01:59:51 +00001366 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367 stack.back().SetValueType (Value::eValueTypeFileAddress);
1368 break;
1369
1370 //----------------------------------------------------------------------
1371 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1372 // shared libraries that have a location like:
1373 // DW_OP_addr(0x1000)
1374 // If this address resides in a shared library, then this virtual
1375 // address won't make sense when it is evaluated in the context of a
1376 // running process where shared libraries have been slid. To account for
1377 // this, this new address type where we can store the section pointer
1378 // and a 4 byte offset.
1379 //----------------------------------------------------------------------
1380// case DW_OP_addr_sect_offset4:
1381// {
1382// result_type = eResultTypeFileAddress;
1383// lldb::Section *sect = (lldb::Section *)opcodes.GetMaxU64(&offset, sizeof(void *));
1384// lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1385//
1386// Address so_addr (sect, sect_offset);
1387// lldb::addr_t load_addr = so_addr.GetLoadAddress();
1388// if (load_addr != LLDB_INVALID_ADDRESS)
1389// {
1390// // We successfully resolve a file address to a load
1391// // address.
1392// stack.push_back(load_addr);
1393// break;
1394// }
1395// else
1396// {
1397// // We were able
1398// if (error_ptr)
1399// error_ptr->SetErrorStringWithFormat ("Section %s in %s is not currently loaded.\n", sect->GetName().AsCString(), sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1400// return false;
1401// }
1402// }
1403// break;
1404
1405 //----------------------------------------------------------------------
1406 // OPCODE: DW_OP_deref
1407 // OPERANDS: none
1408 // DESCRIPTION: Pops the top stack entry and treats it as an address.
1409 // The value retrieved from that address is pushed. The size of the
1410 // data retrieved from the dereferenced address is the size of an
1411 // address on the target machine.
1412 //----------------------------------------------------------------------
1413 case DW_OP_deref:
1414 {
1415 Value::ValueType value_type = stack.back().GetValueType();
1416 switch (value_type)
1417 {
1418 case Value::eValueTypeHostAddress:
1419 {
1420 void *src = (void *)stack.back().GetScalar().ULongLong();
1421 intptr_t ptr;
1422 ::memcpy (&ptr, src, sizeof(void *));
1423 stack.back().GetScalar() = ptr;
1424 stack.back().ClearContext();
1425 }
1426 break;
1427 case Value::eValueTypeLoadAddress:
1428 if (exe_ctx)
1429 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001430 if (process)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 {
1432 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1433 uint8_t addr_bytes[sizeof(lldb::addr_t)];
Greg Claytonc14ee322011-09-22 04:58:26 +00001434 uint32_t addr_size = process->GetAddressByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001435 Error error;
Greg Claytonc14ee322011-09-22 04:58:26 +00001436 if (process->ReadMemory(pointer_addr, &addr_bytes, addr_size, error) == addr_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001438 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), process->GetByteOrder(), addr_size);
Greg Claytonc7bece562013-01-25 18:06:21 +00001439 lldb::offset_t addr_data_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440 stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
1441 stack.back().ClearContext();
1442 }
1443 else
1444 {
1445 if (error_ptr)
Daniel Malead01b2952012-11-29 21:49:15 +00001446 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%" PRIx64 " for DW_OP_deref: %s\n",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447 pointer_addr,
1448 error.AsCString());
1449 return false;
1450 }
1451 }
1452 else
1453 {
1454 if (error_ptr)
1455 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1456 return false;
1457 }
1458 }
1459 else
1460 {
1461 if (error_ptr)
1462 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1463 return false;
1464 }
1465 break;
1466
1467 default:
1468 break;
1469 }
1470
1471 }
1472 break;
1473
1474 //----------------------------------------------------------------------
1475 // OPCODE: DW_OP_deref_size
1476 // OPERANDS: 1
1477 // 1 - uint8_t that specifies the size of the data to dereference.
1478 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1479 // stack entry and treats it as an address. The value retrieved from that
1480 // address is pushed. In the DW_OP_deref_size operation, however, the
1481 // size in bytes of the data retrieved from the dereferenced address is
1482 // specified by the single operand. This operand is a 1-byte unsigned
1483 // integral constant whose value may not be larger than the size of an
1484 // address on the target machine. The data retrieved is zero extended
1485 // to the size of an address on the target machine before being pushed
1486 // on the expression stack.
1487 //----------------------------------------------------------------------
1488 case DW_OP_deref_size:
Jason Molenda2d107dd2010-11-20 01:28:30 +00001489 {
1490 uint8_t size = opcodes.GetU8(&offset);
1491 Value::ValueType value_type = stack.back().GetValueType();
1492 switch (value_type)
1493 {
1494 case Value::eValueTypeHostAddress:
1495 {
1496 void *src = (void *)stack.back().GetScalar().ULongLong();
1497 intptr_t ptr;
1498 ::memcpy (&ptr, src, sizeof(void *));
1499 // I can't decide whether the size operand should apply to the bytes in their
1500 // lldb-host endianness or the target endianness.. I doubt this'll ever come up
1501 // but I'll opt for assuming big endian regardless.
1502 switch (size)
1503 {
1504 case 1: ptr = ptr & 0xff; break;
1505 case 2: ptr = ptr & 0xffff; break;
1506 case 3: ptr = ptr & 0xffffff; break;
1507 case 4: ptr = ptr & 0xffffffff; break;
Jason Molendac1903402010-11-29 21:38:58 +00001508 // the casts are added to work around the case where intptr_t is a 32 bit quantity;
1509 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this program.
1510 case 5: ptr = (intptr_t) ptr & 0xffffffffffULL; break;
1511 case 6: ptr = (intptr_t) ptr & 0xffffffffffffULL; break;
1512 case 7: ptr = (intptr_t) ptr & 0xffffffffffffffULL; break;
Jason Molenda2d107dd2010-11-20 01:28:30 +00001513 default: break;
1514 }
1515 stack.back().GetScalar() = ptr;
1516 stack.back().ClearContext();
1517 }
1518 break;
1519 case Value::eValueTypeLoadAddress:
1520 if (exe_ctx)
1521 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001522 if (process)
Jason Molenda2d107dd2010-11-20 01:28:30 +00001523 {
1524 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1525 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1526 Error error;
Greg Claytonc14ee322011-09-22 04:58:26 +00001527 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) == size)
Jason Molenda2d107dd2010-11-20 01:28:30 +00001528 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001529 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), process->GetByteOrder(), size);
Greg Claytonc7bece562013-01-25 18:06:21 +00001530 lldb::offset_t addr_data_offset = 0;
Jason Molenda2d107dd2010-11-20 01:28:30 +00001531 switch (size)
1532 {
1533 case 1: stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset); break;
1534 case 2: stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset); break;
1535 case 4: stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset); break;
1536 case 8: stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset); break;
1537 default: stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
1538 }
1539 stack.back().ClearContext();
1540 }
1541 else
1542 {
1543 if (error_ptr)
Daniel Malead01b2952012-11-29 21:49:15 +00001544 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%" PRIx64 " for DW_OP_deref: %s\n",
Jason Molenda2d107dd2010-11-20 01:28:30 +00001545 pointer_addr,
1546 error.AsCString());
1547 return false;
1548 }
1549 }
1550 else
1551 {
1552 if (error_ptr)
1553 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1554 return false;
1555 }
1556 }
1557 else
1558 {
1559 if (error_ptr)
1560 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1561 return false;
1562 }
1563 break;
1564
1565 default:
1566 break;
1567 }
1568
1569 }
1570 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571
1572 //----------------------------------------------------------------------
1573 // OPCODE: DW_OP_xderef_size
1574 // OPERANDS: 1
1575 // 1 - uint8_t that specifies the size of the data to dereference.
1576 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1577 // the top of the stack is treated as an address. The second stack
Greg Clayton1b72fcb2010-08-24 00:45:41 +00001578 // entry is treated as an "address space identifier" for those
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579 // architectures that support multiple address spaces. The top two
1580 // stack elements are popped, a data item is retrieved through an
1581 // implementation-defined address calculation and pushed as the new
1582 // stack top. In the DW_OP_xderef_size operation, however, the size in
1583 // bytes of the data retrieved from the dereferenced address is
1584 // specified by the single operand. This operand is a 1-byte unsigned
1585 // integral constant whose value may not be larger than the size of an
1586 // address on the target machine. The data retrieved is zero extended
1587 // to the size of an address on the target machine before being pushed
1588 // on the expression stack.
1589 //----------------------------------------------------------------------
1590 case DW_OP_xderef_size:
1591 if (error_ptr)
1592 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1593 return false;
1594 //----------------------------------------------------------------------
1595 // OPCODE: DW_OP_xderef
1596 // OPERANDS: none
1597 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1598 // the top of the stack is treated as an address. The second stack entry
1599 // is treated as an "address space identifier" for those architectures
1600 // that support multiple address spaces. The top two stack elements are
1601 // popped, a data item is retrieved through an implementation-defined
1602 // address calculation and pushed as the new stack top. The size of the
1603 // data retrieved from the dereferenced address is the size of an address
1604 // on the target machine.
1605 //----------------------------------------------------------------------
1606 case DW_OP_xderef:
1607 if (error_ptr)
1608 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1609 return false;
1610
1611 //----------------------------------------------------------------------
1612 // All DW_OP_constXXX opcodes have a single operand as noted below:
1613 //
1614 // Opcode Operand 1
1615 // --------------- ----------------------------------------------------
1616 // DW_OP_const1u 1-byte unsigned integer constant
1617 // DW_OP_const1s 1-byte signed integer constant
1618 // DW_OP_const2u 2-byte unsigned integer constant
1619 // DW_OP_const2s 2-byte signed integer constant
1620 // DW_OP_const4u 4-byte unsigned integer constant
1621 // DW_OP_const4s 4-byte signed integer constant
1622 // DW_OP_const8u 8-byte unsigned integer constant
1623 // DW_OP_const8s 8-byte signed integer constant
1624 // DW_OP_constu unsigned LEB128 integer constant
1625 // DW_OP_consts signed LEB128 integer constant
1626 //----------------------------------------------------------------------
Greg Clayton644247c2011-07-07 01:59:51 +00001627 case DW_OP_const1u : stack.push_back(Scalar(( uint8_t)opcodes.GetU8 (&offset))); break;
1628 case DW_OP_const1s : stack.push_back(Scalar(( int8_t)opcodes.GetU8 (&offset))); break;
1629 case DW_OP_const2u : stack.push_back(Scalar((uint16_t)opcodes.GetU16 (&offset))); break;
1630 case DW_OP_const2s : stack.push_back(Scalar(( int16_t)opcodes.GetU16 (&offset))); break;
1631 case DW_OP_const4u : stack.push_back(Scalar((uint32_t)opcodes.GetU32 (&offset))); break;
1632 case DW_OP_const4s : stack.push_back(Scalar(( int32_t)opcodes.GetU32 (&offset))); break;
1633 case DW_OP_const8u : stack.push_back(Scalar((uint64_t)opcodes.GetU64 (&offset))); break;
1634 case DW_OP_const8s : stack.push_back(Scalar(( int64_t)opcodes.GetU64 (&offset))); break;
1635 case DW_OP_constu : stack.push_back(Scalar(opcodes.GetULEB128 (&offset))); break;
1636 case DW_OP_consts : stack.push_back(Scalar(opcodes.GetSLEB128 (&offset))); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637
1638 //----------------------------------------------------------------------
1639 // OPCODE: DW_OP_dup
1640 // OPERANDS: none
1641 // DESCRIPTION: duplicates the value at the top of the stack
1642 //----------------------------------------------------------------------
1643 case DW_OP_dup:
1644 if (stack.empty())
1645 {
1646 if (error_ptr)
1647 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1648 return false;
1649 }
1650 else
1651 stack.push_back(stack.back());
1652 break;
1653
1654 //----------------------------------------------------------------------
1655 // OPCODE: DW_OP_drop
1656 // OPERANDS: none
1657 // DESCRIPTION: pops the value at the top of the stack
1658 //----------------------------------------------------------------------
1659 case DW_OP_drop:
1660 if (stack.empty())
1661 {
1662 if (error_ptr)
1663 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1664 return false;
1665 }
1666 else
1667 stack.pop_back();
1668 break;
1669
1670 //----------------------------------------------------------------------
1671 // OPCODE: DW_OP_over
1672 // OPERANDS: none
1673 // DESCRIPTION: Duplicates the entry currently second in the stack at
1674 // the top of the stack.
1675 //----------------------------------------------------------------------
1676 case DW_OP_over:
1677 if (stack.size() < 2)
1678 {
1679 if (error_ptr)
1680 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_over.");
1681 return false;
1682 }
1683 else
1684 stack.push_back(stack[stack.size() - 2]);
1685 break;
1686
1687
1688 //----------------------------------------------------------------------
1689 // OPCODE: DW_OP_pick
1690 // OPERANDS: uint8_t index into the current stack
1691 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1692 // inclusive) is pushed on the stack
1693 //----------------------------------------------------------------------
1694 case DW_OP_pick:
1695 {
1696 uint8_t pick_idx = opcodes.GetU8(&offset);
1697 if (pick_idx < stack.size())
1698 stack.push_back(stack[pick_idx]);
1699 else
1700 {
1701 if (error_ptr)
1702 error_ptr->SetErrorStringWithFormat("Index %u out of range for DW_OP_pick.\n", pick_idx);
1703 return false;
1704 }
1705 }
1706 break;
1707
1708 //----------------------------------------------------------------------
1709 // OPCODE: DW_OP_swap
1710 // OPERANDS: none
1711 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1712 // of the stack becomes the second stack entry, and the second entry
1713 // becomes the top of the stack
1714 //----------------------------------------------------------------------
1715 case DW_OP_swap:
1716 if (stack.size() < 2)
1717 {
1718 if (error_ptr)
1719 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_swap.");
1720 return false;
1721 }
1722 else
1723 {
1724 tmp = stack.back();
1725 stack.back() = stack[stack.size() - 2];
1726 stack[stack.size() - 2] = tmp;
1727 }
1728 break;
1729
1730 //----------------------------------------------------------------------
1731 // OPCODE: DW_OP_rot
1732 // OPERANDS: none
1733 // DESCRIPTION: Rotates the first three stack entries. The entry at
1734 // the top of the stack becomes the third stack entry, the second
1735 // entry becomes the top of the stack, and the third entry becomes
1736 // the second entry.
1737 //----------------------------------------------------------------------
1738 case DW_OP_rot:
1739 if (stack.size() < 3)
1740 {
1741 if (error_ptr)
1742 error_ptr->SetErrorString("Expression stack needs at least 3 items for DW_OP_rot.");
1743 return false;
1744 }
1745 else
1746 {
1747 size_t last_idx = stack.size() - 1;
1748 Value old_top = stack[last_idx];
1749 stack[last_idx] = stack[last_idx - 1];
1750 stack[last_idx - 1] = stack[last_idx - 2];
1751 stack[last_idx - 2] = old_top;
1752 }
1753 break;
1754
1755 //----------------------------------------------------------------------
1756 // OPCODE: DW_OP_abs
1757 // OPERANDS: none
1758 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1759 // value and pushes its absolute value. If the absolute value can not be
1760 // represented, the result is undefined.
1761 //----------------------------------------------------------------------
1762 case DW_OP_abs:
1763 if (stack.empty())
1764 {
1765 if (error_ptr)
1766 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_abs.");
1767 return false;
1768 }
Greg Clayton57ee3062013-07-11 22:46:58 +00001769 else if (stack.back().ResolveValue(exe_ctx).AbsoluteValue() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001770 {
1771 if (error_ptr)
1772 error_ptr->SetErrorString("Failed to take the absolute value of the first stack item.");
1773 return false;
1774 }
1775 break;
1776
1777 //----------------------------------------------------------------------
1778 // OPCODE: DW_OP_and
1779 // OPERANDS: none
1780 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1781 // operation on the two, and pushes the result.
1782 //----------------------------------------------------------------------
1783 case DW_OP_and:
1784 if (stack.size() < 2)
1785 {
1786 if (error_ptr)
1787 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_and.");
1788 return false;
1789 }
1790 else
1791 {
1792 tmp = stack.back();
1793 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00001794 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001795 }
1796 break;
1797
1798 //----------------------------------------------------------------------
1799 // OPCODE: DW_OP_div
1800 // OPERANDS: none
1801 // DESCRIPTION: pops the top two stack values, divides the former second
1802 // entry by the former top of the stack using signed division, and
1803 // pushes the result.
1804 //----------------------------------------------------------------------
1805 case DW_OP_div:
1806 if (stack.size() < 2)
1807 {
1808 if (error_ptr)
1809 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_div.");
1810 return false;
1811 }
1812 else
1813 {
1814 tmp = stack.back();
Greg Clayton57ee3062013-07-11 22:46:58 +00001815 if (tmp.ResolveValue(exe_ctx).IsZero())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001816 {
1817 if (error_ptr)
1818 error_ptr->SetErrorString("Divide by zero.");
1819 return false;
1820 }
1821 else
1822 {
1823 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00001824 stack.back() = stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1825 if (!stack.back().ResolveValue(exe_ctx).IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826 {
1827 if (error_ptr)
1828 error_ptr->SetErrorString("Divide failed.");
1829 return false;
1830 }
1831 }
1832 }
1833 break;
1834
1835 //----------------------------------------------------------------------
1836 // OPCODE: DW_OP_minus
1837 // OPERANDS: none
1838 // DESCRIPTION: pops the top two stack values, subtracts the former top
1839 // of the stack from the former second entry, and pushes the result.
1840 //----------------------------------------------------------------------
1841 case DW_OP_minus:
1842 if (stack.size() < 2)
1843 {
1844 if (error_ptr)
1845 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_minus.");
1846 return false;
1847 }
1848 else
1849 {
1850 tmp = stack.back();
1851 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00001852 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853 }
1854 break;
1855
1856 //----------------------------------------------------------------------
1857 // OPCODE: DW_OP_mod
1858 // OPERANDS: none
1859 // DESCRIPTION: pops the top two stack values and pushes the result of
1860 // the calculation: former second stack entry modulo the former top of
1861 // the stack.
1862 //----------------------------------------------------------------------
1863 case DW_OP_mod:
1864 if (stack.size() < 2)
1865 {
1866 if (error_ptr)
1867 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mod.");
1868 return false;
1869 }
1870 else
1871 {
1872 tmp = stack.back();
1873 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00001874 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001875 }
1876 break;
1877
1878
1879 //----------------------------------------------------------------------
1880 // OPCODE: DW_OP_mul
1881 // OPERANDS: none
1882 // DESCRIPTION: pops the top two stack entries, multiplies them
1883 // together, and pushes the result.
1884 //----------------------------------------------------------------------
1885 case DW_OP_mul:
1886 if (stack.size() < 2)
1887 {
1888 if (error_ptr)
1889 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mul.");
1890 return false;
1891 }
1892 else
1893 {
1894 tmp = stack.back();
1895 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00001896 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897 }
1898 break;
1899
1900 //----------------------------------------------------------------------
1901 // OPCODE: DW_OP_neg
1902 // OPERANDS: none
1903 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1904 //----------------------------------------------------------------------
1905 case DW_OP_neg:
1906 if (stack.empty())
1907 {
1908 if (error_ptr)
1909 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_neg.");
1910 return false;
1911 }
1912 else
1913 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001914 if (stack.back().ResolveValue(exe_ctx).UnaryNegate() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001915 {
1916 if (error_ptr)
1917 error_ptr->SetErrorString("Unary negate failed.");
1918 return false;
1919 }
1920 }
1921 break;
1922
1923 //----------------------------------------------------------------------
1924 // OPCODE: DW_OP_not
1925 // OPERANDS: none
1926 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1927 // complement
1928 //----------------------------------------------------------------------
1929 case DW_OP_not:
1930 if (stack.empty())
1931 {
1932 if (error_ptr)
1933 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_not.");
1934 return false;
1935 }
1936 else
1937 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001938 if (stack.back().ResolveValue(exe_ctx).OnesComplement() == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001939 {
1940 if (error_ptr)
1941 error_ptr->SetErrorString("Logical NOT failed.");
1942 return false;
1943 }
1944 }
1945 break;
1946
1947 //----------------------------------------------------------------------
1948 // OPCODE: DW_OP_or
1949 // OPERANDS: none
1950 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1951 // operation on the two, and pushes the result.
1952 //----------------------------------------------------------------------
1953 case DW_OP_or:
1954 if (stack.size() < 2)
1955 {
1956 if (error_ptr)
1957 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_or.");
1958 return false;
1959 }
1960 else
1961 {
1962 tmp = stack.back();
1963 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00001964 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965 }
1966 break;
1967
1968 //----------------------------------------------------------------------
1969 // OPCODE: DW_OP_plus
1970 // OPERANDS: none
1971 // DESCRIPTION: pops the top two stack entries, adds them together, and
1972 // pushes the result.
1973 //----------------------------------------------------------------------
1974 case DW_OP_plus:
1975 if (stack.size() < 2)
1976 {
1977 if (error_ptr)
1978 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_plus.");
1979 return false;
1980 }
1981 else
1982 {
1983 tmp = stack.back();
1984 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00001985 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) + tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986 }
1987 break;
1988
1989 //----------------------------------------------------------------------
1990 // OPCODE: DW_OP_plus_uconst
1991 // OPERANDS: none
1992 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1993 // constant operand and pushes the result.
1994 //----------------------------------------------------------------------
1995 case DW_OP_plus_uconst:
1996 if (stack.empty())
1997 {
1998 if (error_ptr)
1999 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_plus_uconst.");
2000 return false;
2001 }
2002 else
2003 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002004 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002005 // Implicit conversion from a UINT to a Scalar...
Greg Clayton57ee3062013-07-11 22:46:58 +00002006 stack.back().ResolveValue(exe_ctx) += uconst_value;
2007 if (!stack.back().ResolveValue(exe_ctx).IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002008 {
2009 if (error_ptr)
2010 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
2011 return false;
2012 }
2013 }
2014 break;
2015
2016 //----------------------------------------------------------------------
2017 // OPCODE: DW_OP_shl
2018 // OPERANDS: none
2019 // DESCRIPTION: pops the top two stack entries, shifts the former
2020 // second entry left by the number of bits specified by the former top
2021 // of the stack, and pushes the result.
2022 //----------------------------------------------------------------------
2023 case DW_OP_shl:
2024 if (stack.size() < 2)
2025 {
2026 if (error_ptr)
2027 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shl.");
2028 return false;
2029 }
2030 else
2031 {
2032 tmp = stack.back();
2033 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002034 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035 }
2036 break;
2037
2038 //----------------------------------------------------------------------
2039 // OPCODE: DW_OP_shr
2040 // OPERANDS: none
2041 // DESCRIPTION: pops the top two stack entries, shifts the former second
2042 // entry right logically (filling with zero bits) by the number of bits
2043 // specified by the former top of the stack, and pushes the result.
2044 //----------------------------------------------------------------------
2045 case DW_OP_shr:
2046 if (stack.size() < 2)
2047 {
2048 if (error_ptr)
2049 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shr.");
2050 return false;
2051 }
2052 else
2053 {
2054 tmp = stack.back();
2055 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002056 if (stack.back().ResolveValue(exe_ctx).ShiftRightLogical(tmp.ResolveValue(exe_ctx)) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002057 {
2058 if (error_ptr)
2059 error_ptr->SetErrorString("DW_OP_shr failed.");
2060 return false;
2061 }
2062 }
2063 break;
2064
2065 //----------------------------------------------------------------------
2066 // OPCODE: DW_OP_shra
2067 // OPERANDS: none
2068 // DESCRIPTION: pops the top two stack entries, shifts the former second
2069 // entry right arithmetically (divide the magnitude by 2, keep the same
2070 // sign for the result) by the number of bits specified by the former
2071 // top of the stack, and pushes the result.
2072 //----------------------------------------------------------------------
2073 case DW_OP_shra:
2074 if (stack.size() < 2)
2075 {
2076 if (error_ptr)
2077 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shra.");
2078 return false;
2079 }
2080 else
2081 {
2082 tmp = stack.back();
2083 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002084 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002085 }
2086 break;
2087
2088 //----------------------------------------------------------------------
2089 // OPCODE: DW_OP_xor
2090 // OPERANDS: none
2091 // DESCRIPTION: pops the top two stack entries, performs the bitwise
2092 // exclusive-or operation on the two, and pushes the result.
2093 //----------------------------------------------------------------------
2094 case DW_OP_xor:
2095 if (stack.size() < 2)
2096 {
2097 if (error_ptr)
2098 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_xor.");
2099 return false;
2100 }
2101 else
2102 {
2103 tmp = stack.back();
2104 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002105 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002106 }
2107 break;
2108
2109
2110 //----------------------------------------------------------------------
2111 // OPCODE: DW_OP_skip
2112 // OPERANDS: int16_t
2113 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
2114 // signed integer constant. The 2-byte constant is the number of bytes
2115 // of the DWARF expression to skip forward or backward from the current
2116 // operation, beginning after the 2-byte constant.
2117 //----------------------------------------------------------------------
2118 case DW_OP_skip:
2119 {
2120 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
Greg Claytonc7bece562013-01-25 18:06:21 +00002121 lldb::offset_t new_offset = offset + skip_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002122 if (new_offset >= opcodes_offset && new_offset < end_offset)
2123 offset = new_offset;
2124 else
2125 {
2126 if (error_ptr)
2127 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
2128 return false;
2129 }
2130 }
2131 break;
2132
2133 //----------------------------------------------------------------------
2134 // OPCODE: DW_OP_bra
2135 // OPERANDS: int16_t
2136 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
2137 // signed integer constant. This operation pops the top of stack. If
2138 // the value popped is not the constant 0, the 2-byte constant operand
2139 // is the number of bytes of the DWARF expression to skip forward or
2140 // backward from the current operation, beginning after the 2-byte
2141 // constant.
2142 //----------------------------------------------------------------------
2143 case DW_OP_bra:
2144 {
2145 tmp = stack.back();
2146 stack.pop_back();
2147 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
2148 Scalar zero(0);
Greg Clayton57ee3062013-07-11 22:46:58 +00002149 if (tmp.ResolveValue(exe_ctx) != zero)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002150 {
Greg Claytonc7bece562013-01-25 18:06:21 +00002151 lldb::offset_t new_offset = offset + bra_offset;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002152 if (new_offset >= opcodes_offset && new_offset < end_offset)
2153 offset = new_offset;
2154 else
2155 {
2156 if (error_ptr)
2157 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
2158 return false;
2159 }
2160 }
2161 }
2162 break;
2163
2164 //----------------------------------------------------------------------
2165 // OPCODE: DW_OP_eq
2166 // OPERANDS: none
2167 // DESCRIPTION: pops the top two stack values, compares using the
2168 // equals (==) operator.
2169 // STACK RESULT: push the constant value 1 onto the stack if the result
2170 // of the operation is true or the constant value 0 if the result of the
2171 // operation is false.
2172 //----------------------------------------------------------------------
2173 case DW_OP_eq:
2174 if (stack.size() < 2)
2175 {
2176 if (error_ptr)
2177 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_eq.");
2178 return false;
2179 }
2180 else
2181 {
2182 tmp = stack.back();
2183 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002184 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002185 }
2186 break;
2187
2188 //----------------------------------------------------------------------
2189 // OPCODE: DW_OP_ge
2190 // OPERANDS: none
2191 // DESCRIPTION: pops the top two stack values, compares using the
2192 // greater than or equal to (>=) operator.
2193 // STACK RESULT: push the constant value 1 onto the stack if the result
2194 // of the operation is true or the constant value 0 if the result of the
2195 // operation is false.
2196 //----------------------------------------------------------------------
2197 case DW_OP_ge:
2198 if (stack.size() < 2)
2199 {
2200 if (error_ptr)
2201 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ge.");
2202 return false;
2203 }
2204 else
2205 {
2206 tmp = stack.back();
2207 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002208 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002209 }
2210 break;
2211
2212 //----------------------------------------------------------------------
2213 // OPCODE: DW_OP_gt
2214 // OPERANDS: none
2215 // DESCRIPTION: pops the top two stack values, compares using the
2216 // greater than (>) operator.
2217 // STACK RESULT: push the constant value 1 onto the stack if the result
2218 // of the operation is true or the constant value 0 if the result of the
2219 // operation is false.
2220 //----------------------------------------------------------------------
2221 case DW_OP_gt:
2222 if (stack.size() < 2)
2223 {
2224 if (error_ptr)
2225 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_gt.");
2226 return false;
2227 }
2228 else
2229 {
2230 tmp = stack.back();
2231 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002232 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002233 }
2234 break;
2235
2236 //----------------------------------------------------------------------
2237 // OPCODE: DW_OP_le
2238 // OPERANDS: none
2239 // DESCRIPTION: pops the top two stack values, compares using the
2240 // less than or equal to (<=) operator.
2241 // STACK RESULT: push the constant value 1 onto the stack if the result
2242 // of the operation is true or the constant value 0 if the result of the
2243 // operation is false.
2244 //----------------------------------------------------------------------
2245 case DW_OP_le:
2246 if (stack.size() < 2)
2247 {
2248 if (error_ptr)
2249 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_le.");
2250 return false;
2251 }
2252 else
2253 {
2254 tmp = stack.back();
2255 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002256 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002257 }
2258 break;
2259
2260 //----------------------------------------------------------------------
2261 // OPCODE: DW_OP_lt
2262 // OPERANDS: none
2263 // DESCRIPTION: pops the top two stack values, compares using the
2264 // less than (<) operator.
2265 // STACK RESULT: push the constant value 1 onto the stack if the result
2266 // of the operation is true or the constant value 0 if the result of the
2267 // operation is false.
2268 //----------------------------------------------------------------------
2269 case DW_OP_lt:
2270 if (stack.size() < 2)
2271 {
2272 if (error_ptr)
2273 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_lt.");
2274 return false;
2275 }
2276 else
2277 {
2278 tmp = stack.back();
2279 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002280 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002281 }
2282 break;
2283
2284 //----------------------------------------------------------------------
2285 // OPCODE: DW_OP_ne
2286 // OPERANDS: none
2287 // DESCRIPTION: pops the top two stack values, compares using the
2288 // not equal (!=) operator.
2289 // STACK RESULT: push the constant value 1 onto the stack if the result
2290 // of the operation is true or the constant value 0 if the result of the
2291 // operation is false.
2292 //----------------------------------------------------------------------
2293 case DW_OP_ne:
2294 if (stack.size() < 2)
2295 {
2296 if (error_ptr)
2297 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ne.");
2298 return false;
2299 }
2300 else
2301 {
2302 tmp = stack.back();
2303 stack.pop_back();
Greg Clayton57ee3062013-07-11 22:46:58 +00002304 stack.back().ResolveValue(exe_ctx) = stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002305 }
2306 break;
2307
2308 //----------------------------------------------------------------------
2309 // OPCODE: DW_OP_litn
2310 // OPERANDS: none
2311 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
2312 // STACK RESULT: push the unsigned literal constant value onto the top
2313 // of the stack.
2314 //----------------------------------------------------------------------
2315 case DW_OP_lit0:
2316 case DW_OP_lit1:
2317 case DW_OP_lit2:
2318 case DW_OP_lit3:
2319 case DW_OP_lit4:
2320 case DW_OP_lit5:
2321 case DW_OP_lit6:
2322 case DW_OP_lit7:
2323 case DW_OP_lit8:
2324 case DW_OP_lit9:
2325 case DW_OP_lit10:
2326 case DW_OP_lit11:
2327 case DW_OP_lit12:
2328 case DW_OP_lit13:
2329 case DW_OP_lit14:
2330 case DW_OP_lit15:
2331 case DW_OP_lit16:
2332 case DW_OP_lit17:
2333 case DW_OP_lit18:
2334 case DW_OP_lit19:
2335 case DW_OP_lit20:
2336 case DW_OP_lit21:
2337 case DW_OP_lit22:
2338 case DW_OP_lit23:
2339 case DW_OP_lit24:
2340 case DW_OP_lit25:
2341 case DW_OP_lit26:
2342 case DW_OP_lit27:
2343 case DW_OP_lit28:
2344 case DW_OP_lit29:
2345 case DW_OP_lit30:
2346 case DW_OP_lit31:
Greg Clayton644247c2011-07-07 01:59:51 +00002347 stack.push_back(Scalar(op - DW_OP_lit0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002348 break;
2349
2350 //----------------------------------------------------------------------
2351 // OPCODE: DW_OP_regN
2352 // OPERANDS: none
2353 // DESCRIPTION: Push the value in register n on the top of the stack.
2354 //----------------------------------------------------------------------
2355 case DW_OP_reg0:
2356 case DW_OP_reg1:
2357 case DW_OP_reg2:
2358 case DW_OP_reg3:
2359 case DW_OP_reg4:
2360 case DW_OP_reg5:
2361 case DW_OP_reg6:
2362 case DW_OP_reg7:
2363 case DW_OP_reg8:
2364 case DW_OP_reg9:
2365 case DW_OP_reg10:
2366 case DW_OP_reg11:
2367 case DW_OP_reg12:
2368 case DW_OP_reg13:
2369 case DW_OP_reg14:
2370 case DW_OP_reg15:
2371 case DW_OP_reg16:
2372 case DW_OP_reg17:
2373 case DW_OP_reg18:
2374 case DW_OP_reg19:
2375 case DW_OP_reg20:
2376 case DW_OP_reg21:
2377 case DW_OP_reg22:
2378 case DW_OP_reg23:
2379 case DW_OP_reg24:
2380 case DW_OP_reg25:
2381 case DW_OP_reg26:
2382 case DW_OP_reg27:
2383 case DW_OP_reg28:
2384 case DW_OP_reg29:
2385 case DW_OP_reg30:
2386 case DW_OP_reg31:
2387 {
2388 reg_num = op - DW_OP_reg0;
2389
Jason Molenda2d107dd2010-11-20 01:28:30 +00002390 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002391 stack.push_back(tmp);
2392 else
2393 return false;
2394 }
2395 break;
2396 //----------------------------------------------------------------------
2397 // OPCODE: DW_OP_regx
2398 // OPERANDS:
2399 // ULEB128 literal operand that encodes the register.
2400 // DESCRIPTION: Push the value in register on the top of the stack.
2401 //----------------------------------------------------------------------
2402 case DW_OP_regx:
2403 {
2404 reg_num = opcodes.GetULEB128(&offset);
Jason Molenda2d107dd2010-11-20 01:28:30 +00002405 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002406 stack.push_back(tmp);
2407 else
2408 return false;
2409 }
2410 break;
2411
2412 //----------------------------------------------------------------------
2413 // OPCODE: DW_OP_bregN
2414 // OPERANDS:
2415 // SLEB128 offset from register N
2416 // DESCRIPTION: Value is in memory at the address specified by register
2417 // N plus an offset.
2418 //----------------------------------------------------------------------
2419 case DW_OP_breg0:
2420 case DW_OP_breg1:
2421 case DW_OP_breg2:
2422 case DW_OP_breg3:
2423 case DW_OP_breg4:
2424 case DW_OP_breg5:
2425 case DW_OP_breg6:
2426 case DW_OP_breg7:
2427 case DW_OP_breg8:
2428 case DW_OP_breg9:
2429 case DW_OP_breg10:
2430 case DW_OP_breg11:
2431 case DW_OP_breg12:
2432 case DW_OP_breg13:
2433 case DW_OP_breg14:
2434 case DW_OP_breg15:
2435 case DW_OP_breg16:
2436 case DW_OP_breg17:
2437 case DW_OP_breg18:
2438 case DW_OP_breg19:
2439 case DW_OP_breg20:
2440 case DW_OP_breg21:
2441 case DW_OP_breg22:
2442 case DW_OP_breg23:
2443 case DW_OP_breg24:
2444 case DW_OP_breg25:
2445 case DW_OP_breg26:
2446 case DW_OP_breg27:
2447 case DW_OP_breg28:
2448 case DW_OP_breg29:
2449 case DW_OP_breg30:
2450 case DW_OP_breg31:
2451 {
2452 reg_num = op - DW_OP_breg0;
2453
Jason Molenda2d107dd2010-11-20 01:28:30 +00002454 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002455 {
2456 int64_t breg_offset = opcodes.GetSLEB128(&offset);
Greg Clayton57ee3062013-07-11 22:46:58 +00002457 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
Sean Callanand1a5e012012-01-06 18:24:47 +00002458 tmp.ClearContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002459 stack.push_back(tmp);
2460 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2461 }
2462 else
2463 return false;
2464 }
2465 break;
2466 //----------------------------------------------------------------------
2467 // OPCODE: DW_OP_bregx
2468 // OPERANDS: 2
2469 // ULEB128 literal operand that encodes the register.
2470 // SLEB128 offset from register N
2471 // DESCRIPTION: Value is in memory at the address specified by register
2472 // N plus an offset.
2473 //----------------------------------------------------------------------
2474 case DW_OP_bregx:
2475 {
2476 reg_num = opcodes.GetULEB128(&offset);
2477
Jason Molenda2d107dd2010-11-20 01:28:30 +00002478 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002479 {
2480 int64_t breg_offset = opcodes.GetSLEB128(&offset);
Greg Clayton57ee3062013-07-11 22:46:58 +00002481 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
Sean Callanand1a5e012012-01-06 18:24:47 +00002482 tmp.ClearContext();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002483 stack.push_back(tmp);
2484 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2485 }
2486 else
2487 return false;
2488 }
2489 break;
2490
2491 case DW_OP_fbreg:
Greg Claytonc14ee322011-09-22 04:58:26 +00002492 if (exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002493 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002494 if (frame)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002495 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002496 Scalar value;
2497 if (frame->GetFrameBaseValue(value, error_ptr))
2498 {
2499 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2500 value += fbreg_offset;
2501 stack.push_back(value);
2502 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2503 }
2504 else
2505 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002506 }
2507 else
Greg Claytonc14ee322011-09-22 04:58:26 +00002508 {
2509 if (error_ptr)
2510 error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_fbreg opcode.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002511 return false;
Greg Claytonc14ee322011-09-22 04:58:26 +00002512 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002513 }
2514 else
2515 {
2516 if (error_ptr)
Greg Claytonc14ee322011-09-22 04:58:26 +00002517 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_fbreg.\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002518 return false;
2519 }
Greg Claytonc14ee322011-09-22 04:58:26 +00002520
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002521 break;
2522
2523 //----------------------------------------------------------------------
2524 // OPCODE: DW_OP_nop
2525 // OPERANDS: none
2526 // DESCRIPTION: A place holder. It has no effect on the location stack
2527 // or any of its values.
2528 //----------------------------------------------------------------------
2529 case DW_OP_nop:
2530 break;
2531
2532 //----------------------------------------------------------------------
2533 // OPCODE: DW_OP_piece
2534 // OPERANDS: 1
2535 // ULEB128: byte size of the piece
2536 // DESCRIPTION: The operand describes the size in bytes of the piece of
2537 // the object referenced by the DWARF expression whose result is at the
2538 // top of the stack. If the piece is located in a register, but does not
2539 // occupy the entire register, the placement of the piece within that
2540 // register is defined by the ABI.
2541 //
2542 // Many compilers store a single variable in sets of registers, or store
2543 // a variable partially in memory and partially in registers.
2544 // DW_OP_piece provides a way of describing how large a part of a
2545 // variable a particular DWARF expression refers to.
2546 //----------------------------------------------------------------------
2547 case DW_OP_piece:
2548 if (error_ptr)
2549 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_piece.");
2550 return false;
2551
2552 //----------------------------------------------------------------------
2553 // OPCODE: DW_OP_push_object_address
2554 // OPERANDS: none
2555 // DESCRIPTION: Pushes the address of the object currently being
2556 // evaluated as part of evaluation of a user presented expression.
2557 // This object may correspond to an independent variable described by
2558 // its own DIE or it may be a component of an array, structure, or class
2559 // whose address has been dynamically determined by an earlier step
2560 // during user expression evaluation.
2561 //----------------------------------------------------------------------
2562 case DW_OP_push_object_address:
2563 if (error_ptr)
2564 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_push_object_address.");
2565 return false;
2566
2567 //----------------------------------------------------------------------
2568 // OPCODE: DW_OP_call2
2569 // OPERANDS:
2570 // uint16_t compile unit relative offset of a DIE
2571 // DESCRIPTION: Performs subroutine calls during evaluation
2572 // of a DWARF expression. The operand is the 2-byte unsigned offset
2573 // of a debugging information entry in the current compilation unit.
2574 //
2575 // Operand interpretation is exactly like that for DW_FORM_ref2.
2576 //
2577 // This operation transfers control of DWARF expression evaluation
2578 // to the DW_AT_location attribute of the referenced DIE. If there is
2579 // no such attribute, then there is no effect. Execution of the DWARF
2580 // expression of a DW_AT_location attribute may add to and/or remove from
2581 // values on the stack. Execution returns to the point following the call
2582 // when the end of the attribute is reached. Values on the stack at the
2583 // time of the call may be used as parameters by the called expression
2584 // and values left on the stack by the called expression may be used as
2585 // return values by prior agreement between the calling and called
2586 // expressions.
2587 //----------------------------------------------------------------------
2588 case DW_OP_call2:
2589 if (error_ptr)
2590 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call2.");
2591 return false;
2592 //----------------------------------------------------------------------
2593 // OPCODE: DW_OP_call4
2594 // OPERANDS: 1
2595 // uint32_t compile unit relative offset of a DIE
2596 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2597 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset
2598 // of a debugging information entry in the current compilation unit.
2599 //
2600 // Operand interpretation DW_OP_call4 is exactly like that for
2601 // DW_FORM_ref4.
2602 //
2603 // This operation transfers control of DWARF expression evaluation
2604 // to the DW_AT_location attribute of the referenced DIE. If there is
2605 // no such attribute, then there is no effect. Execution of the DWARF
2606 // expression of a DW_AT_location attribute may add to and/or remove from
2607 // values on the stack. Execution returns to the point following the call
2608 // when the end of the attribute is reached. Values on the stack at the
2609 // time of the call may be used as parameters by the called expression
2610 // and values left on the stack by the called expression may be used as
2611 // return values by prior agreement between the calling and called
2612 // expressions.
2613 //----------------------------------------------------------------------
2614 case DW_OP_call4:
2615 if (error_ptr)
2616 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call4.");
2617 return false;
2618
Andrew Kaylor9e9f1db2013-03-06 21:13:09 +00002619 //----------------------------------------------------------------------
2620 // OPCODE: DW_OP_stack_value
2621 // OPERANDS: None
2622 // DESCRIPTION: Specifies that the object does not exist in memory but
2623 // rather is a constant value. The value from the top of the stack is
2624 // the value to be used. This is the actual object value and not the
2625 // location.
2626 //----------------------------------------------------------------------
2627 case DW_OP_stack_value:
2628 stack.back().SetValueType(Value::eValueTypeScalar);
2629 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002630 }
2631 }
2632
2633 if (stack.empty())
2634 {
2635 if (error_ptr)
2636 error_ptr->SetErrorString ("Stack empty after evaluation.");
2637 return false;
2638 }
Sean Callanan7dd98122011-10-14 20:34:21 +00002639 else if (log && log->GetVerbose())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002640 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002641 size_t count = stack.size();
Jason Molendafd54b362011-09-20 21:44:10 +00002642 log->Printf("Stack after operation has %lu values:", count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002643 for (size_t i=0; i<count; ++i)
2644 {
2645 StreamString new_value;
Daniel Malead01b2952012-11-29 21:49:15 +00002646 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002647 stack[i].Dump(&new_value);
Sean Callananf06ba8d2010-06-23 00:47:48 +00002648 log->Printf(" %s", new_value.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002649 }
2650 }
2651
2652 result = stack.back();
2653 return true; // Return true on success
2654}
2655