blob: 161fd957d5caa6c160e988ccbd23cf8f49036cb8 [file] [log] [blame]
Chris Lattner24943d22010-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
14#include "lldb/Core/dwarf.h"
15#include "lldb/Core/Log.h"
Greg Clayton061b79d2011-05-09 20:18:18 +000016#include "lldb/Core/RegisterValue.h"
Chris Lattner24943d22010-06-08 16:52:24 +000017#include "lldb/Core/StreamString.h"
18#include "lldb/Core/Scalar.h"
19#include "lldb/Core/Value.h"
Greg Clayton178710c2010-09-14 02:20:48 +000020#include "lldb/Core/VMRange.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
22#include "lldb/Expression/ClangExpressionDeclMap.h"
23#include "lldb/Expression/ClangExpressionVariable.h"
24
Greg Claytoncd548032011-02-01 01:31:41 +000025#include "lldb/Host/Endian.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
27#include "lldb/lldb-private-log.h"
28
Greg Clayton1674b122010-07-21 22:12:05 +000029#include "lldb/Symbol/ClangASTType.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Symbol/ClangASTContext.h"
31#include "lldb/Symbol/Type.h"
32
33#include "lldb/Target/ExecutionContext.h"
34#include "lldb/Target/Process.h"
35#include "lldb/Target/RegisterContext.h"
36#include "lldb/Target/StackFrame.h"
37
38using namespace lldb;
39using namespace lldb_private;
40
41const char *
42DW_OP_value_to_name (uint32_t val)
43{
44 static char invalid[100];
45 switch (val) {
46 case 0x03: return "DW_OP_addr";
47 case 0x06: return "DW_OP_deref";
48 case 0x08: return "DW_OP_const1u";
49 case 0x09: return "DW_OP_const1s";
50 case 0x0a: return "DW_OP_const2u";
51 case 0x0b: return "DW_OP_const2s";
52 case 0x0c: return "DW_OP_const4u";
53 case 0x0d: return "DW_OP_const4s";
54 case 0x0e: return "DW_OP_const8u";
55 case 0x0f: return "DW_OP_const8s";
56 case 0x10: return "DW_OP_constu";
57 case 0x11: return "DW_OP_consts";
58 case 0x12: return "DW_OP_dup";
59 case 0x13: return "DW_OP_drop";
60 case 0x14: return "DW_OP_over";
61 case 0x15: return "DW_OP_pick";
62 case 0x16: return "DW_OP_swap";
63 case 0x17: return "DW_OP_rot";
64 case 0x18: return "DW_OP_xderef";
65 case 0x19: return "DW_OP_abs";
66 case 0x1a: return "DW_OP_and";
67 case 0x1b: return "DW_OP_div";
68 case 0x1c: return "DW_OP_minus";
69 case 0x1d: return "DW_OP_mod";
70 case 0x1e: return "DW_OP_mul";
71 case 0x1f: return "DW_OP_neg";
72 case 0x20: return "DW_OP_not";
73 case 0x21: return "DW_OP_or";
74 case 0x22: return "DW_OP_plus";
75 case 0x23: return "DW_OP_plus_uconst";
76 case 0x24: return "DW_OP_shl";
77 case 0x25: return "DW_OP_shr";
78 case 0x26: return "DW_OP_shra";
79 case 0x27: return "DW_OP_xor";
80 case 0x2f: return "DW_OP_skip";
81 case 0x28: return "DW_OP_bra";
82 case 0x29: return "DW_OP_eq";
83 case 0x2a: return "DW_OP_ge";
84 case 0x2b: return "DW_OP_gt";
85 case 0x2c: return "DW_OP_le";
86 case 0x2d: return "DW_OP_lt";
87 case 0x2e: return "DW_OP_ne";
88 case 0x30: return "DW_OP_lit0";
89 case 0x31: return "DW_OP_lit1";
90 case 0x32: return "DW_OP_lit2";
91 case 0x33: return "DW_OP_lit3";
92 case 0x34: return "DW_OP_lit4";
93 case 0x35: return "DW_OP_lit5";
94 case 0x36: return "DW_OP_lit6";
95 case 0x37: return "DW_OP_lit7";
96 case 0x38: return "DW_OP_lit8";
97 case 0x39: return "DW_OP_lit9";
98 case 0x3a: return "DW_OP_lit10";
99 case 0x3b: return "DW_OP_lit11";
100 case 0x3c: return "DW_OP_lit12";
101 case 0x3d: return "DW_OP_lit13";
102 case 0x3e: return "DW_OP_lit14";
103 case 0x3f: return "DW_OP_lit15";
104 case 0x40: return "DW_OP_lit16";
105 case 0x41: return "DW_OP_lit17";
106 case 0x42: return "DW_OP_lit18";
107 case 0x43: return "DW_OP_lit19";
108 case 0x44: return "DW_OP_lit20";
109 case 0x45: return "DW_OP_lit21";
110 case 0x46: return "DW_OP_lit22";
111 case 0x47: return "DW_OP_lit23";
112 case 0x48: return "DW_OP_lit24";
113 case 0x49: return "DW_OP_lit25";
114 case 0x4a: return "DW_OP_lit26";
115 case 0x4b: return "DW_OP_lit27";
116 case 0x4c: return "DW_OP_lit28";
117 case 0x4d: return "DW_OP_lit29";
118 case 0x4e: return "DW_OP_lit30";
119 case 0x4f: return "DW_OP_lit31";
120 case 0x50: return "DW_OP_reg0";
121 case 0x51: return "DW_OP_reg1";
122 case 0x52: return "DW_OP_reg2";
123 case 0x53: return "DW_OP_reg3";
124 case 0x54: return "DW_OP_reg4";
125 case 0x55: return "DW_OP_reg5";
126 case 0x56: return "DW_OP_reg6";
127 case 0x57: return "DW_OP_reg7";
128 case 0x58: return "DW_OP_reg8";
129 case 0x59: return "DW_OP_reg9";
130 case 0x5a: return "DW_OP_reg10";
131 case 0x5b: return "DW_OP_reg11";
132 case 0x5c: return "DW_OP_reg12";
133 case 0x5d: return "DW_OP_reg13";
134 case 0x5e: return "DW_OP_reg14";
135 case 0x5f: return "DW_OP_reg15";
136 case 0x60: return "DW_OP_reg16";
137 case 0x61: return "DW_OP_reg17";
138 case 0x62: return "DW_OP_reg18";
139 case 0x63: return "DW_OP_reg19";
140 case 0x64: return "DW_OP_reg20";
141 case 0x65: return "DW_OP_reg21";
142 case 0x66: return "DW_OP_reg22";
143 case 0x67: return "DW_OP_reg23";
144 case 0x68: return "DW_OP_reg24";
145 case 0x69: return "DW_OP_reg25";
146 case 0x6a: return "DW_OP_reg26";
147 case 0x6b: return "DW_OP_reg27";
148 case 0x6c: return "DW_OP_reg28";
149 case 0x6d: return "DW_OP_reg29";
150 case 0x6e: return "DW_OP_reg30";
151 case 0x6f: return "DW_OP_reg31";
152 case 0x70: return "DW_OP_breg0";
153 case 0x71: return "DW_OP_breg1";
154 case 0x72: return "DW_OP_breg2";
155 case 0x73: return "DW_OP_breg3";
156 case 0x74: return "DW_OP_breg4";
157 case 0x75: return "DW_OP_breg5";
158 case 0x76: return "DW_OP_breg6";
159 case 0x77: return "DW_OP_breg7";
160 case 0x78: return "DW_OP_breg8";
161 case 0x79: return "DW_OP_breg9";
162 case 0x7a: return "DW_OP_breg10";
163 case 0x7b: return "DW_OP_breg11";
164 case 0x7c: return "DW_OP_breg12";
165 case 0x7d: return "DW_OP_breg13";
166 case 0x7e: return "DW_OP_breg14";
167 case 0x7f: return "DW_OP_breg15";
168 case 0x80: return "DW_OP_breg16";
169 case 0x81: return "DW_OP_breg17";
170 case 0x82: return "DW_OP_breg18";
171 case 0x83: return "DW_OP_breg19";
172 case 0x84: return "DW_OP_breg20";
173 case 0x85: return "DW_OP_breg21";
174 case 0x86: return "DW_OP_breg22";
175 case 0x87: return "DW_OP_breg23";
176 case 0x88: return "DW_OP_breg24";
177 case 0x89: return "DW_OP_breg25";
178 case 0x8a: return "DW_OP_breg26";
179 case 0x8b: return "DW_OP_breg27";
180 case 0x8c: return "DW_OP_breg28";
181 case 0x8d: return "DW_OP_breg29";
182 case 0x8e: return "DW_OP_breg30";
183 case 0x8f: return "DW_OP_breg31";
184 case 0x90: return "DW_OP_regx";
185 case 0x91: return "DW_OP_fbreg";
186 case 0x92: return "DW_OP_bregx";
187 case 0x93: return "DW_OP_piece";
188 case 0x94: return "DW_OP_deref_size";
189 case 0x95: return "DW_OP_xderef_size";
190 case 0x96: return "DW_OP_nop";
191 case 0x97: return "DW_OP_push_object_address";
192 case 0x98: return "DW_OP_call2";
193 case 0x99: return "DW_OP_call4";
194 case 0x9a: return "DW_OP_call_ref";
195 case DW_OP_APPLE_array_ref: return "DW_OP_APPLE_array_ref";
196 case DW_OP_APPLE_extern: return "DW_OP_APPLE_extern";
197 case DW_OP_APPLE_uninit: return "DW_OP_APPLE_uninit";
198 case DW_OP_APPLE_assign: return "DW_OP_APPLE_assign";
199 case DW_OP_APPLE_address_of: return "DW_OP_APPLE_address_of";
200 case DW_OP_APPLE_value_of: return "DW_OP_APPLE_value_of";
201 case DW_OP_APPLE_deref_type: return "DW_OP_APPLE_deref_type";
202 case DW_OP_APPLE_expr_local: return "DW_OP_APPLE_expr_local";
203 case DW_OP_APPLE_constf: return "DW_OP_APPLE_constf";
204 case DW_OP_APPLE_scalar_cast: return "DW_OP_APPLE_scalar_cast";
205 case DW_OP_APPLE_clang_cast: return "DW_OP_APPLE_clang_cast";
206 case DW_OP_APPLE_clear: return "DW_OP_APPLE_clear";
207 case DW_OP_APPLE_error: return "DW_OP_APPLE_error";
208 default:
209 snprintf (invalid, sizeof(invalid), "Unknown DW_OP constant: 0x%x", val);
210 return invalid;
211 }
212}
213
214
215//----------------------------------------------------------------------
216// DWARFExpression constructor
217//----------------------------------------------------------------------
218DWARFExpression::DWARFExpression() :
219 m_data(),
220 m_reg_kind (eRegisterKindDWARF),
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000221 m_loclist_slide (LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000222{
223}
224
225DWARFExpression::DWARFExpression(const DWARFExpression& rhs) :
226 m_data(rhs.m_data),
227 m_reg_kind (rhs.m_reg_kind),
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000228 m_loclist_slide(rhs.m_loclist_slide)
Chris Lattner24943d22010-06-08 16:52:24 +0000229{
230}
231
232
Greg Clayton178710c2010-09-14 02:20:48 +0000233DWARFExpression::DWARFExpression(const DataExtractor& data, uint32_t data_offset, uint32_t data_length) :
Chris Lattner24943d22010-06-08 16:52:24 +0000234 m_data(data, data_offset, data_length),
235 m_reg_kind (eRegisterKindDWARF),
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000236 m_loclist_slide(LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000237{
Chris Lattner24943d22010-06-08 16:52:24 +0000238}
239
240//----------------------------------------------------------------------
241// Destructor
242//----------------------------------------------------------------------
243DWARFExpression::~DWARFExpression()
244{
245}
246
247
248bool
249DWARFExpression::IsValid() const
250{
251 return m_data.GetByteSize() > 0;
252}
253
Chris Lattner24943d22010-06-08 16:52:24 +0000254void
Greg Clayton178710c2010-09-14 02:20:48 +0000255DWARFExpression::SetOpcodeData (const DataExtractor& data)
Chris Lattner24943d22010-06-08 16:52:24 +0000256{
257 m_data = data;
Chris Lattner24943d22010-06-08 16:52:24 +0000258}
259
260void
Greg Clayton178710c2010-09-14 02:20:48 +0000261DWARFExpression::SetOpcodeData (const DataExtractor& data, uint32_t data_offset, uint32_t data_length)
Chris Lattner24943d22010-06-08 16:52:24 +0000262{
263 m_data.SetData(data, data_offset, data_length);
Chris Lattner24943d22010-06-08 16:52:24 +0000264}
265
266void
267DWARFExpression::DumpLocation (Stream *s, uint32_t offset, uint32_t length, lldb::DescriptionLevel level) const
268{
269 if (!m_data.ValidOffsetForDataOfSize(offset, length))
270 return;
271 const uint32_t start_offset = offset;
272 const uint32_t end_offset = offset + length;
273 while (m_data.ValidOffset(offset) && offset < end_offset)
274 {
275 const uint32_t op_offset = offset;
276 const uint8_t op = m_data.GetU8(&offset);
277
278 switch (level)
279 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000280 default:
281 break;
282
Chris Lattner24943d22010-06-08 16:52:24 +0000283 case lldb::eDescriptionLevelBrief:
284 if (offset > start_offset)
285 s->PutChar(' ');
286 break;
287
288 case lldb::eDescriptionLevelFull:
289 case lldb::eDescriptionLevelVerbose:
290 if (offset > start_offset)
291 s->EOL();
292 s->Indent();
293 if (level == lldb::eDescriptionLevelFull)
294 break;
295 // Fall through for verbose and print offset and DW_OP prefix..
296 s->Printf("0x%8.8x: %s", op_offset, op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
297 break;
298 }
299
300 switch (op)
301 {
302 case DW_OP_addr: *s << "addr(" << m_data.GetAddress(&offset) << ") "; break; // 0x03 1 address
303 case DW_OP_deref: *s << "deref"; break; // 0x06
304 case DW_OP_const1u: s->Printf("const1u(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x08 1 1-byte constant
305 case DW_OP_const1s: s->Printf("const1s(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x09 1 1-byte constant
306 case DW_OP_const2u: s->Printf("const2u(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0a 1 2-byte constant
307 case DW_OP_const2s: s->Printf("const2s(0x%4.4x) ", m_data.GetU16(&offset)); break; // 0x0b 1 2-byte constant
308 case DW_OP_const4u: s->Printf("const4u(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0c 1 4-byte constant
309 case DW_OP_const4s: s->Printf("const4s(0x%8.8x) ", m_data.GetU32(&offset)); break; // 0x0d 1 4-byte constant
310 case DW_OP_const8u: s->Printf("const8u(0x%16.16llx) ", m_data.GetU64(&offset)); break; // 0x0e 1 8-byte constant
311 case DW_OP_const8s: s->Printf("const8s(0x%16.16llx) ", m_data.GetU64(&offset)); break; // 0x0f 1 8-byte constant
312 case DW_OP_constu: s->Printf("constu(0x%x) ", m_data.GetULEB128(&offset)); break; // 0x10 1 ULEB128 constant
313 case DW_OP_consts: s->Printf("consts(0x%x) ", m_data.GetSLEB128(&offset)); break; // 0x11 1 SLEB128 constant
314 case DW_OP_dup: s->PutCString("dup"); break; // 0x12
315 case DW_OP_drop: s->PutCString("drop"); break; // 0x13
316 case DW_OP_over: s->PutCString("over"); break; // 0x14
317 case DW_OP_pick: s->Printf("pick(0x%2.2x) ", m_data.GetU8(&offset)); break; // 0x15 1 1-byte stack index
318 case DW_OP_swap: s->PutCString("swap"); break; // 0x16
319 case DW_OP_rot: s->PutCString("rot"); break; // 0x17
320 case DW_OP_xderef: s->PutCString("xderef"); break; // 0x18
321 case DW_OP_abs: s->PutCString("abs"); break; // 0x19
322 case DW_OP_and: s->PutCString("and"); break; // 0x1a
323 case DW_OP_div: s->PutCString("div"); break; // 0x1b
324 case DW_OP_minus: s->PutCString("minus"); break; // 0x1c
325 case DW_OP_mod: s->PutCString("mod"); break; // 0x1d
326 case DW_OP_mul: s->PutCString("mul"); break; // 0x1e
327 case DW_OP_neg: s->PutCString("neg"); break; // 0x1f
328 case DW_OP_not: s->PutCString("not"); break; // 0x20
329 case DW_OP_or: s->PutCString("or"); break; // 0x21
330 case DW_OP_plus: s->PutCString("plus"); break; // 0x22
331 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
332 s->Printf("plus_uconst(0x%x) ", m_data.GetULEB128(&offset));
333 break;
334
335 case DW_OP_shl: s->PutCString("shl"); break; // 0x24
336 case DW_OP_shr: s->PutCString("shr"); break; // 0x25
337 case DW_OP_shra: s->PutCString("shra"); break; // 0x26
338 case DW_OP_xor: s->PutCString("xor"); break; // 0x27
339 case DW_OP_skip: s->Printf("skip(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x2f 1 signed 2-byte constant
340 case DW_OP_bra: s->Printf("bra(0x%4.4x)", m_data.GetU16(&offset)); break; // 0x28 1 signed 2-byte constant
341 case DW_OP_eq: s->PutCString("eq"); break; // 0x29
342 case DW_OP_ge: s->PutCString("ge"); break; // 0x2a
343 case DW_OP_gt: s->PutCString("gt"); break; // 0x2b
344 case DW_OP_le: s->PutCString("le"); break; // 0x2c
345 case DW_OP_lt: s->PutCString("lt"); break; // 0x2d
346 case DW_OP_ne: s->PutCString("ne"); break; // 0x2e
347
348 case DW_OP_lit0: // 0x30
349 case DW_OP_lit1: // 0x31
350 case DW_OP_lit2: // 0x32
351 case DW_OP_lit3: // 0x33
352 case DW_OP_lit4: // 0x34
353 case DW_OP_lit5: // 0x35
354 case DW_OP_lit6: // 0x36
355 case DW_OP_lit7: // 0x37
356 case DW_OP_lit8: // 0x38
357 case DW_OP_lit9: // 0x39
358 case DW_OP_lit10: // 0x3A
359 case DW_OP_lit11: // 0x3B
360 case DW_OP_lit12: // 0x3C
361 case DW_OP_lit13: // 0x3D
362 case DW_OP_lit14: // 0x3E
363 case DW_OP_lit15: // 0x3F
364 case DW_OP_lit16: // 0x40
365 case DW_OP_lit17: // 0x41
366 case DW_OP_lit18: // 0x42
367 case DW_OP_lit19: // 0x43
368 case DW_OP_lit20: // 0x44
369 case DW_OP_lit21: // 0x45
370 case DW_OP_lit22: // 0x46
371 case DW_OP_lit23: // 0x47
372 case DW_OP_lit24: // 0x48
373 case DW_OP_lit25: // 0x49
374 case DW_OP_lit26: // 0x4A
375 case DW_OP_lit27: // 0x4B
376 case DW_OP_lit28: // 0x4C
377 case DW_OP_lit29: // 0x4D
378 case DW_OP_lit30: // 0x4E
379 case DW_OP_lit31: s->Printf("lit%i", op - DW_OP_lit0); break; // 0x4f
380
381 case DW_OP_reg0: // 0x50
382 case DW_OP_reg1: // 0x51
383 case DW_OP_reg2: // 0x52
384 case DW_OP_reg3: // 0x53
385 case DW_OP_reg4: // 0x54
386 case DW_OP_reg5: // 0x55
387 case DW_OP_reg6: // 0x56
388 case DW_OP_reg7: // 0x57
389 case DW_OP_reg8: // 0x58
390 case DW_OP_reg9: // 0x59
391 case DW_OP_reg10: // 0x5A
392 case DW_OP_reg11: // 0x5B
393 case DW_OP_reg12: // 0x5C
394 case DW_OP_reg13: // 0x5D
395 case DW_OP_reg14: // 0x5E
396 case DW_OP_reg15: // 0x5F
397 case DW_OP_reg16: // 0x60
398 case DW_OP_reg17: // 0x61
399 case DW_OP_reg18: // 0x62
400 case DW_OP_reg19: // 0x63
401 case DW_OP_reg20: // 0x64
402 case DW_OP_reg21: // 0x65
403 case DW_OP_reg22: // 0x66
404 case DW_OP_reg23: // 0x67
405 case DW_OP_reg24: // 0x68
406 case DW_OP_reg25: // 0x69
407 case DW_OP_reg26: // 0x6A
408 case DW_OP_reg27: // 0x6B
409 case DW_OP_reg28: // 0x6C
410 case DW_OP_reg29: // 0x6D
411 case DW_OP_reg30: // 0x6E
412 case DW_OP_reg31: s->Printf("reg%i", op - DW_OP_reg0); break; // 0x6f
413
414 case DW_OP_breg0:
415 case DW_OP_breg1:
416 case DW_OP_breg2:
417 case DW_OP_breg3:
418 case DW_OP_breg4:
419 case DW_OP_breg5:
420 case DW_OP_breg6:
421 case DW_OP_breg7:
422 case DW_OP_breg8:
423 case DW_OP_breg9:
424 case DW_OP_breg10:
425 case DW_OP_breg11:
426 case DW_OP_breg12:
427 case DW_OP_breg13:
428 case DW_OP_breg14:
429 case DW_OP_breg15:
430 case DW_OP_breg16:
431 case DW_OP_breg17:
432 case DW_OP_breg18:
433 case DW_OP_breg19:
434 case DW_OP_breg20:
435 case DW_OP_breg21:
436 case DW_OP_breg22:
437 case DW_OP_breg23:
438 case DW_OP_breg24:
439 case DW_OP_breg25:
440 case DW_OP_breg26:
441 case DW_OP_breg27:
442 case DW_OP_breg28:
443 case DW_OP_breg29:
444 case DW_OP_breg30:
445 case DW_OP_breg31: s->Printf("breg%i(0x%x)", op - DW_OP_breg0, m_data.GetULEB128(&offset)); break;
446
447 case DW_OP_regx: // 0x90 1 ULEB128 register
448 s->Printf("regx(0x%x)", m_data.GetULEB128(&offset));
449 break;
450 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
451 s->Printf("fbreg(0x%x)",m_data.GetSLEB128(&offset));
452 break;
453 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
454 s->Printf("bregx(0x%x, 0x%x)", m_data.GetULEB128(&offset), m_data.GetSLEB128(&offset));
455 break;
456 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
457 s->Printf("piece(0x%x)", m_data.GetULEB128(&offset));
458 break;
459 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
460 s->Printf("deref_size(0x%2.2x)", m_data.GetU8(&offset));
461 break;
462 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
463 s->Printf("xderef_size(0x%2.2x)", m_data.GetU8(&offset));
464 break;
465 case DW_OP_nop: s->PutCString("nop"); break; // 0x96
466 case DW_OP_push_object_address: s->PutCString("push_object_address"); break; // 0x97 DWARF3
467 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
468 s->Printf("call2(0x%4.4x)", m_data.GetU16(&offset));
469 break;
470 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
471 s->Printf("call4(0x%8.8x)", m_data.GetU32(&offset));
472 break;
473 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
474 s->Printf("call_ref(0x%8.8llx)", m_data.GetAddress(&offset));
475 break;
476// case DW_OP_form_tls_address: s << "form_tls_address"; break; // 0x9b DWARF3
477// case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break; // 0x9c DWARF3
478// case DW_OP_bit_piece: // 0x9d DWARF3 2
479// s->Printf("bit_piece(0x%x, 0x%x)", m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
480// break;
481// case DW_OP_lo_user: s->PutCString("lo_user"); break; // 0xe0
482// case DW_OP_hi_user: s->PutCString("hi_user"); break; // 0xff
483 case DW_OP_APPLE_extern:
484 s->Printf("extern(%u)", m_data.GetULEB128(&offset));
485 break;
486 case DW_OP_APPLE_array_ref:
487 s->PutCString("array_ref");
488 break;
489 case DW_OP_APPLE_uninit:
490 s->PutCString("uninit"); // 0xF0
491 break;
492 case DW_OP_APPLE_assign: // 0xF1 - pops value off and assigns it to second item on stack (2nd item must have assignable context)
493 s->PutCString("assign");
494 break;
495 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)
496 s->PutCString("address_of");
497 break;
498 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)
499 s->PutCString("value_of");
500 break;
501 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)
502 s->PutCString("deref_type");
503 break;
504 case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression local index
505 s->Printf("expr_local(%u)", m_data.GetULEB128(&offset));
506 break;
507 case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size, followed by constant float data
508 {
509 uint8_t float_length = m_data.GetU8(&offset);
510 s->Printf("constf(<%u> ", float_length);
511 m_data.Dump(s, offset, eFormatHex, float_length, 1, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
512 s->PutChar(')');
513 // Consume the float data
514 m_data.GetData(&offset, float_length);
515 }
516 break;
517 case DW_OP_APPLE_scalar_cast:
518 s->Printf("scalar_cast(%s)", Scalar::GetValueTypeAsCString ((Scalar::Type)m_data.GetU8(&offset)));
519 break;
520 case DW_OP_APPLE_clang_cast:
521 {
522 clang::Type *clang_type = (clang::Type *)m_data.GetMaxU64(&offset, sizeof(void*));
523 s->Printf("clang_cast(%p)", clang_type);
524 }
525 break;
526 case DW_OP_APPLE_clear:
527 s->PutCString("clear");
528 break;
529 case DW_OP_APPLE_error: // 0xFF - Stops expression evaluation and returns an error (no args)
530 s->PutCString("error");
531 break;
532 }
533 }
534}
535
536void
Greg Clayton178710c2010-09-14 02:20:48 +0000537DWARFExpression::SetLocationListSlide (addr_t slide)
Chris Lattner24943d22010-06-08 16:52:24 +0000538{
Greg Clayton178710c2010-09-14 02:20:48 +0000539 m_loclist_slide = slide;
Chris Lattner24943d22010-06-08 16:52:24 +0000540}
541
542int
543DWARFExpression::GetRegisterKind ()
544{
545 return m_reg_kind;
546}
547
548void
549DWARFExpression::SetRegisterKind (int reg_kind)
550{
551 m_reg_kind = reg_kind;
552}
553
554bool
555DWARFExpression::IsLocationList() const
556{
Greg Clayton178710c2010-09-14 02:20:48 +0000557 return m_loclist_slide != LLDB_INVALID_ADDRESS;
Chris Lattner24943d22010-06-08 16:52:24 +0000558}
559
560void
Greg Clayton178710c2010-09-14 02:20:48 +0000561DWARFExpression::GetDescription (Stream *s, lldb::DescriptionLevel level, addr_t location_list_base_addr) const
Chris Lattner24943d22010-06-08 16:52:24 +0000562{
563 if (IsLocationList())
564 {
565 // We have a location list
566 uint32_t offset = 0;
567 uint32_t count = 0;
Greg Clayton178710c2010-09-14 02:20:48 +0000568 addr_t curr_base_addr = location_list_base_addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000569 while (m_data.ValidOffset(offset))
570 {
571 lldb::addr_t begin_addr_offset = m_data.GetAddress(&offset);
572 lldb::addr_t end_addr_offset = m_data.GetAddress(&offset);
573 if (begin_addr_offset < end_addr_offset)
574 {
575 if (count > 0)
576 s->PutCString(", ");
Greg Clayton178710c2010-09-14 02:20:48 +0000577 VMRange addr_range(curr_base_addr + begin_addr_offset, curr_base_addr + end_addr_offset);
578 addr_range.Dump(s, 0, 8);
Chris Lattner24943d22010-06-08 16:52:24 +0000579 s->PutChar('{');
580 uint32_t location_length = m_data.GetU16(&offset);
581 DumpLocation (s, offset, location_length, level);
582 s->PutChar('}');
583 offset += location_length;
584 }
585 else if (begin_addr_offset == 0 && end_addr_offset == 0)
586 {
587 // The end of the location list is marked by both the start and end offset being zero
588 break;
589 }
590 else
591 {
Greg Claytonb3448432011-03-24 21:19:54 +0000592 if ((m_data.GetAddressByteSize() == 4 && (begin_addr_offset == UINT32_MAX)) ||
593 (m_data.GetAddressByteSize() == 8 && (begin_addr_offset == UINT64_MAX)))
Chris Lattner24943d22010-06-08 16:52:24 +0000594 {
Greg Clayton178710c2010-09-14 02:20:48 +0000595 curr_base_addr = end_addr_offset + location_list_base_addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000596 // We have a new base address
597 if (count > 0)
598 s->PutCString(", ");
599 *s << "base_addr = " << end_addr_offset;
600 }
601 }
602
603 count++;
604 }
605 }
606 else
607 {
608 // We have a normal location that contains DW_OP location opcodes
609 DumpLocation (s, 0, m_data.GetByteSize(), level);
610 }
611}
612
613static bool
614ReadRegisterValueAsScalar
615(
Greg Clayton061b79d2011-05-09 20:18:18 +0000616 RegisterContext *reg_ctx,
Chris Lattner24943d22010-06-08 16:52:24 +0000617 uint32_t reg_kind,
618 uint32_t reg_num,
619 Error *error_ptr,
620 Value &value
621)
622{
Greg Clayton061b79d2011-05-09 20:18:18 +0000623 if (reg_ctx == NULL)
Chris Lattner24943d22010-06-08 16:52:24 +0000624 {
Jason Molenda8e69de42010-11-20 01:28:30 +0000625 if (error_ptr)
626 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
Chris Lattner24943d22010-06-08 16:52:24 +0000627 }
628 else
629 {
Greg Clayton061b79d2011-05-09 20:18:18 +0000630 uint32_t native_reg = reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
Jason Molenda8e69de42010-11-20 01:28:30 +0000631 if (native_reg == LLDB_INVALID_REGNUM)
632 {
633 if (error_ptr)
634 error_ptr->SetErrorStringWithFormat("Unable to convert register kind=%u reg_num=%u to a native register number.\n", reg_kind, reg_num);
635 }
636 else
637 {
Greg Clayton061b79d2011-05-09 20:18:18 +0000638 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(native_reg);
639 RegisterValue reg_value;
640 if (reg_ctx->ReadRegister (reg_info, reg_value))
641 {
642 if (reg_value.GetScalarValue(value.GetScalar()))
643 {
644 value.SetValueType (Value::eValueTypeScalar);
645 value.SetContext (Value::eContextTypeRegisterInfo, const_cast<RegisterInfo *>(reg_info));
646 if (error_ptr)
647 error_ptr->Clear();
648 return true;
649 }
650 else
651 {
652 if (error_ptr)
653 error_ptr->SetErrorStringWithFormat("Failed to read register %u.\n", native_reg);
654 }
655 }
656 else
657 {
658 if (error_ptr)
659 error_ptr->SetErrorStringWithFormat("Failed to read register %u.\n", native_reg);
660 }
Jason Molenda8e69de42010-11-20 01:28:30 +0000661 }
Chris Lattner24943d22010-06-08 16:52:24 +0000662 }
663 return false;
664}
665
Greg Clayton178710c2010-09-14 02:20:48 +0000666//bool
667//DWARFExpression::LocationListContainsLoadAddress (Process* process, const Address &addr) const
668//{
669// return LocationListContainsLoadAddress(process, addr.GetLoadAddress(process));
670//}
671//
672//bool
673//DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t load_addr) const
674//{
675// if (load_addr == LLDB_INVALID_ADDRESS)
676// return false;
677//
678// if (IsLocationList())
679// {
680// uint32_t offset = 0;
681//
682// addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
683//
684// if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
685// return false;
686//
687// while (m_data.ValidOffset(offset))
688// {
689// // We need to figure out what the value is for the location.
690// addr_t lo_pc = m_data.GetAddress(&offset);
691// addr_t hi_pc = m_data.GetAddress(&offset);
692// if (lo_pc == 0 && hi_pc == 0)
693// break;
694// else
695// {
696// lo_pc += loc_list_base_addr;
697// hi_pc += loc_list_base_addr;
698//
699// if (lo_pc <= load_addr && load_addr < hi_pc)
700// return true;
701//
702// offset += m_data.GetU16(&offset);
703// }
704// }
705// }
706// return false;
707//}
Greg Claytonb04e7a82010-08-24 21:05:24 +0000708
709bool
Greg Clayton178710c2010-09-14 02:20:48 +0000710DWARFExpression::LocationListContainsAddress (lldb::addr_t loclist_base_addr, lldb::addr_t addr) const
Greg Claytonb04e7a82010-08-24 21:05:24 +0000711{
Greg Clayton178710c2010-09-14 02:20:48 +0000712 if (addr == LLDB_INVALID_ADDRESS)
Greg Claytonb04e7a82010-08-24 21:05:24 +0000713 return false;
714
Chris Lattner24943d22010-06-08 16:52:24 +0000715 if (IsLocationList())
716 {
717 uint32_t offset = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000718
Greg Clayton178710c2010-09-14 02:20:48 +0000719 if (loclist_base_addr == LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000720 return false;
721
722 while (m_data.ValidOffset(offset))
723 {
724 // We need to figure out what the value is for the location.
725 addr_t lo_pc = m_data.GetAddress(&offset);
726 addr_t hi_pc = m_data.GetAddress(&offset);
727 if (lo_pc == 0 && hi_pc == 0)
728 break;
729 else
730 {
Greg Clayton178710c2010-09-14 02:20:48 +0000731 lo_pc += loclist_base_addr - m_loclist_slide;
732 hi_pc += loclist_base_addr - m_loclist_slide;
Chris Lattner24943d22010-06-08 16:52:24 +0000733
Greg Clayton178710c2010-09-14 02:20:48 +0000734 if (lo_pc <= addr && addr < hi_pc)
Chris Lattner24943d22010-06-08 16:52:24 +0000735 return true;
736
737 offset += m_data.GetU16(&offset);
738 }
739 }
740 }
741 return false;
742}
Greg Claytonb04e7a82010-08-24 21:05:24 +0000743
Chris Lattner24943d22010-06-08 16:52:24 +0000744bool
745DWARFExpression::Evaluate
746(
747 ExecutionContextScope *exe_scope,
748 clang::ASTContext *ast_context,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000749 ClangExpressionVariableList *expr_locals,
750 ClangExpressionDeclMap *decl_map,
Greg Clayton178710c2010-09-14 02:20:48 +0000751 lldb::addr_t loclist_base_load_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000752 const Value* initial_value_ptr,
753 Value& result,
754 Error *error_ptr
755) const
756{
757 ExecutionContext exe_ctx (exe_scope);
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000758 return Evaluate(&exe_ctx, ast_context, expr_locals, decl_map, NULL, loclist_base_load_addr, initial_value_ptr, result, error_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000759}
760
761bool
762DWARFExpression::Evaluate
763(
764 ExecutionContext *exe_ctx,
765 clang::ASTContext *ast_context,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000766 ClangExpressionVariableList *expr_locals,
767 ClangExpressionDeclMap *decl_map,
Jason Molenda8e69de42010-11-20 01:28:30 +0000768 RegisterContext *reg_ctx,
Greg Clayton178710c2010-09-14 02:20:48 +0000769 lldb::addr_t loclist_base_load_addr,
Chris Lattner24943d22010-06-08 16:52:24 +0000770 const Value* initial_value_ptr,
771 Value& result,
772 Error *error_ptr
773) const
774{
775 if (IsLocationList())
776 {
777 uint32_t offset = 0;
Jason Molenda8e69de42010-11-20 01:28:30 +0000778 addr_t pc;
779 if (reg_ctx)
780 pc = reg_ctx->GetPC();
781 else
782 pc = exe_ctx->frame->GetRegisterContext()->GetPC();
Chris Lattner24943d22010-06-08 16:52:24 +0000783
Greg Clayton178710c2010-09-14 02:20:48 +0000784 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000785 {
Greg Clayton178710c2010-09-14 02:20:48 +0000786 if (pc == LLDB_INVALID_ADDRESS)
Chris Lattner24943d22010-06-08 16:52:24 +0000787 {
Greg Clayton178710c2010-09-14 02:20:48 +0000788 if (error_ptr)
789 error_ptr->SetErrorString("Invalid PC in frame.");
790 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000791 }
Greg Clayton178710c2010-09-14 02:20:48 +0000792
793 addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
794
795 while (m_data.ValidOffset(offset))
Chris Lattner24943d22010-06-08 16:52:24 +0000796 {
Greg Clayton178710c2010-09-14 02:20:48 +0000797 // We need to figure out what the value is for the location.
798 addr_t lo_pc = m_data.GetAddress(&offset);
799 addr_t hi_pc = m_data.GetAddress(&offset);
800 if (lo_pc == 0 && hi_pc == 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000801 {
Greg Clayton178710c2010-09-14 02:20:48 +0000802 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000803 }
Greg Clayton178710c2010-09-14 02:20:48 +0000804 else
805 {
806 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
807 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
808
809 uint16_t length = m_data.GetU16(&offset);
810
811 if (length > 0 && lo_pc <= pc && pc < hi_pc)
812 {
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000813 return DWARFExpression::Evaluate (exe_ctx, ast_context, expr_locals, decl_map, reg_ctx, m_data, offset, length, m_reg_kind, initial_value_ptr, result, error_ptr);
Greg Clayton178710c2010-09-14 02:20:48 +0000814 }
815 offset += length;
816 }
Chris Lattner24943d22010-06-08 16:52:24 +0000817 }
818 }
819 if (error_ptr)
Greg Clayton33ed1702010-08-24 00:45:41 +0000820 error_ptr->SetErrorStringWithFormat("Out of scope.");
Chris Lattner24943d22010-06-08 16:52:24 +0000821 return false;
822 }
823
824 // Not a location list, just a single expression.
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000825 return DWARFExpression::Evaluate (exe_ctx, ast_context, expr_locals, decl_map, reg_ctx, m_data, 0, m_data.GetByteSize(), m_reg_kind, initial_value_ptr, result, error_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000826}
827
828
829
830bool
831DWARFExpression::Evaluate
832(
833 ExecutionContext *exe_ctx,
834 clang::ASTContext *ast_context,
Chris Lattner24943d22010-06-08 16:52:24 +0000835 ClangExpressionVariableList *expr_locals,
836 ClangExpressionDeclMap *decl_map,
Jason Molenda8e69de42010-11-20 01:28:30 +0000837 RegisterContext *reg_ctx,
Greg Claytonbdcb6ab2011-01-25 23:55:37 +0000838 const DataExtractor& opcodes,
Chris Lattner24943d22010-06-08 16:52:24 +0000839 const uint32_t opcodes_offset,
840 const uint32_t opcodes_length,
841 const uint32_t reg_kind,
842 const Value* initial_value_ptr,
843 Value& result,
844 Error *error_ptr
845)
846{
847 std::vector<Value> stack;
848
Jason Molenda8e69de42010-11-20 01:28:30 +0000849 if (reg_ctx == NULL && exe_ctx && exe_ctx->frame)
Greg Clayton08d7d3a2011-01-06 22:15:06 +0000850 reg_ctx = exe_ctx->frame->GetRegisterContext().get();
Jason Molenda8e69de42010-11-20 01:28:30 +0000851
Chris Lattner24943d22010-06-08 16:52:24 +0000852 if (initial_value_ptr)
853 stack.push_back(*initial_value_ptr);
854
855 uint32_t offset = opcodes_offset;
856 const uint32_t end_offset = opcodes_offset + opcodes_length;
857 Value tmp;
858 uint32_t reg_num;
859
860 // Make sure all of the data is available in opcodes.
861 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length))
862 {
863 if (error_ptr)
864 error_ptr->SetErrorString ("Invalid offset and/or length for opcodes buffer.");
865 return false;
866 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000867 LogSP log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Chris Lattner24943d22010-06-08 16:52:24 +0000868
869
870 while (opcodes.ValidOffset(offset) && offset < end_offset)
871 {
872 const uint32_t op_offset = offset;
873 const uint8_t op = opcodes.GetU8(&offset);
874
875 if (log)
876 {
Chris Lattner24943d22010-06-08 16:52:24 +0000877 size_t count = stack.size();
Sean Callanan6184dfe2010-06-23 00:47:48 +0000878 log->Printf("Stack before operation has %d values:", count);
Chris Lattner24943d22010-06-08 16:52:24 +0000879 for (size_t i=0; i<count; ++i)
880 {
881 StreamString new_value;
882 new_value.Printf("[%zu]", i);
883 stack[i].Dump(&new_value);
Sean Callanan6184dfe2010-06-23 00:47:48 +0000884 log->Printf(" %s", new_value.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000885 }
886 log->Printf("0x%8.8x: %s", op_offset, DW_OP_value_to_name(op));
887 }
888 switch (op)
889 {
890 //----------------------------------------------------------------------
891 // The DW_OP_addr operation has a single operand that encodes a machine
892 // address and whose size is the size of an address on the target machine.
893 //----------------------------------------------------------------------
894 case DW_OP_addr:
895 stack.push_back(opcodes.GetAddress(&offset));
896 stack.back().SetValueType (Value::eValueTypeFileAddress);
897 break;
898
899 //----------------------------------------------------------------------
900 // The DW_OP_addr_sect_offset4 is used for any location expressions in
901 // shared libraries that have a location like:
902 // DW_OP_addr(0x1000)
903 // If this address resides in a shared library, then this virtual
904 // address won't make sense when it is evaluated in the context of a
905 // running process where shared libraries have been slid. To account for
906 // this, this new address type where we can store the section pointer
907 // and a 4 byte offset.
908 //----------------------------------------------------------------------
909// case DW_OP_addr_sect_offset4:
910// {
911// result_type = eResultTypeFileAddress;
912// lldb::Section *sect = (lldb::Section *)opcodes.GetMaxU64(&offset, sizeof(void *));
913// lldb::addr_t sect_offset = opcodes.GetU32(&offset);
914//
915// Address so_addr (sect, sect_offset);
916// lldb::addr_t load_addr = so_addr.GetLoadAddress();
917// if (load_addr != LLDB_INVALID_ADDRESS)
918// {
919// // We successfully resolve a file address to a load
920// // address.
921// stack.push_back(load_addr);
922// break;
923// }
924// else
925// {
926// // We were able
927// if (error_ptr)
928// error_ptr->SetErrorStringWithFormat ("Section %s in %s is not currently loaded.\n", sect->GetName().AsCString(), sect->GetModule()->GetFileSpec().GetFilename().AsCString());
929// return false;
930// }
931// }
932// break;
933
934 //----------------------------------------------------------------------
935 // OPCODE: DW_OP_deref
936 // OPERANDS: none
937 // DESCRIPTION: Pops the top stack entry and treats it as an address.
938 // The value retrieved from that address is pushed. The size of the
939 // data retrieved from the dereferenced address is the size of an
940 // address on the target machine.
941 //----------------------------------------------------------------------
942 case DW_OP_deref:
943 {
944 Value::ValueType value_type = stack.back().GetValueType();
945 switch (value_type)
946 {
947 case Value::eValueTypeHostAddress:
948 {
949 void *src = (void *)stack.back().GetScalar().ULongLong();
950 intptr_t ptr;
951 ::memcpy (&ptr, src, sizeof(void *));
952 stack.back().GetScalar() = ptr;
953 stack.back().ClearContext();
954 }
955 break;
956 case Value::eValueTypeLoadAddress:
957 if (exe_ctx)
958 {
959 if (exe_ctx->process)
960 {
961 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
962 uint8_t addr_bytes[sizeof(lldb::addr_t)];
963 uint32_t addr_size = exe_ctx->process->GetAddressByteSize();
964 Error error;
965 if (exe_ctx->process->ReadMemory(pointer_addr, &addr_bytes, addr_size, error) == addr_size)
966 {
967 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), exe_ctx->process->GetByteOrder(), addr_size);
968 uint32_t addr_data_offset = 0;
969 stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
970 stack.back().ClearContext();
971 }
972 else
973 {
974 if (error_ptr)
975 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%llx for DW_OP_deref: %s\n",
976 pointer_addr,
977 error.AsCString());
978 return false;
979 }
980 }
981 else
982 {
983 if (error_ptr)
984 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
985 return false;
986 }
987 }
988 else
989 {
990 if (error_ptr)
991 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
992 return false;
993 }
994 break;
995
996 default:
997 break;
998 }
999
1000 }
1001 break;
1002
1003 //----------------------------------------------------------------------
1004 // OPCODE: DW_OP_deref_size
1005 // OPERANDS: 1
1006 // 1 - uint8_t that specifies the size of the data to dereference.
1007 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1008 // stack entry and treats it as an address. The value retrieved from that
1009 // address is pushed. In the DW_OP_deref_size operation, however, the
1010 // size in bytes of the data retrieved from the dereferenced address is
1011 // specified by the single operand. This operand is a 1-byte unsigned
1012 // integral constant whose value may not be larger than the size of an
1013 // address on the target machine. The data retrieved is zero extended
1014 // to the size of an address on the target machine before being pushed
1015 // on the expression stack.
1016 //----------------------------------------------------------------------
1017 case DW_OP_deref_size:
Jason Molenda8e69de42010-11-20 01:28:30 +00001018 {
1019 uint8_t size = opcodes.GetU8(&offset);
1020 Value::ValueType value_type = stack.back().GetValueType();
1021 switch (value_type)
1022 {
1023 case Value::eValueTypeHostAddress:
1024 {
1025 void *src = (void *)stack.back().GetScalar().ULongLong();
1026 intptr_t ptr;
1027 ::memcpy (&ptr, src, sizeof(void *));
1028 // I can't decide whether the size operand should apply to the bytes in their
1029 // lldb-host endianness or the target endianness.. I doubt this'll ever come up
1030 // but I'll opt for assuming big endian regardless.
1031 switch (size)
1032 {
1033 case 1: ptr = ptr & 0xff; break;
1034 case 2: ptr = ptr & 0xffff; break;
1035 case 3: ptr = ptr & 0xffffff; break;
1036 case 4: ptr = ptr & 0xffffffff; break;
Jason Molendaa99bcaa2010-11-29 21:38:58 +00001037 // the casts are added to work around the case where intptr_t is a 32 bit quantity;
1038 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this program.
1039 case 5: ptr = (intptr_t) ptr & 0xffffffffffULL; break;
1040 case 6: ptr = (intptr_t) ptr & 0xffffffffffffULL; break;
1041 case 7: ptr = (intptr_t) ptr & 0xffffffffffffffULL; break;
Jason Molenda8e69de42010-11-20 01:28:30 +00001042 default: break;
1043 }
1044 stack.back().GetScalar() = ptr;
1045 stack.back().ClearContext();
1046 }
1047 break;
1048 case Value::eValueTypeLoadAddress:
1049 if (exe_ctx)
1050 {
1051 if (exe_ctx->process)
1052 {
1053 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1054 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1055 Error error;
1056 if (exe_ctx->process->ReadMemory(pointer_addr, &addr_bytes, size, error) == size)
1057 {
1058 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), exe_ctx->process->GetByteOrder(), size);
1059 uint32_t addr_data_offset = 0;
1060 switch (size)
1061 {
1062 case 1: stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset); break;
1063 case 2: stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset); break;
1064 case 4: stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset); break;
1065 case 8: stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset); break;
1066 default: stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
1067 }
1068 stack.back().ClearContext();
1069 }
1070 else
1071 {
1072 if (error_ptr)
1073 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%llx for DW_OP_deref: %s\n",
1074 pointer_addr,
1075 error.AsCString());
1076 return false;
1077 }
1078 }
1079 else
1080 {
1081 if (error_ptr)
1082 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1083 return false;
1084 }
1085 }
1086 else
1087 {
1088 if (error_ptr)
1089 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1090 return false;
1091 }
1092 break;
1093
1094 default:
1095 break;
1096 }
1097
1098 }
1099 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001100
1101 //----------------------------------------------------------------------
1102 // OPCODE: DW_OP_xderef_size
1103 // OPERANDS: 1
1104 // 1 - uint8_t that specifies the size of the data to dereference.
1105 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1106 // the top of the stack is treated as an address. The second stack
Greg Clayton33ed1702010-08-24 00:45:41 +00001107 // entry is treated as an "address space identifier" for those
Chris Lattner24943d22010-06-08 16:52:24 +00001108 // architectures that support multiple address spaces. The top two
1109 // stack elements are popped, a data item is retrieved through an
1110 // implementation-defined address calculation and pushed as the new
1111 // stack top. In the DW_OP_xderef_size operation, however, the size in
1112 // bytes of the data retrieved from the dereferenced address is
1113 // specified by the single operand. This operand is a 1-byte unsigned
1114 // integral constant whose value may not be larger than the size of an
1115 // address on the target machine. The data retrieved is zero extended
1116 // to the size of an address on the target machine before being pushed
1117 // on the expression stack.
1118 //----------------------------------------------------------------------
1119 case DW_OP_xderef_size:
1120 if (error_ptr)
1121 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1122 return false;
1123 //----------------------------------------------------------------------
1124 // OPCODE: DW_OP_xderef
1125 // OPERANDS: none
1126 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1127 // the top of the stack is treated as an address. The second stack entry
1128 // is treated as an "address space identifier" for those architectures
1129 // that support multiple address spaces. The top two stack elements are
1130 // popped, a data item is retrieved through an implementation-defined
1131 // address calculation and pushed as the new stack top. The size of the
1132 // data retrieved from the dereferenced address is the size of an address
1133 // on the target machine.
1134 //----------------------------------------------------------------------
1135 case DW_OP_xderef:
1136 if (error_ptr)
1137 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1138 return false;
1139
1140 //----------------------------------------------------------------------
1141 // All DW_OP_constXXX opcodes have a single operand as noted below:
1142 //
1143 // Opcode Operand 1
1144 // --------------- ----------------------------------------------------
1145 // DW_OP_const1u 1-byte unsigned integer constant
1146 // DW_OP_const1s 1-byte signed integer constant
1147 // DW_OP_const2u 2-byte unsigned integer constant
1148 // DW_OP_const2s 2-byte signed integer constant
1149 // DW_OP_const4u 4-byte unsigned integer constant
1150 // DW_OP_const4s 4-byte signed integer constant
1151 // DW_OP_const8u 8-byte unsigned integer constant
1152 // DW_OP_const8s 8-byte signed integer constant
1153 // DW_OP_constu unsigned LEB128 integer constant
1154 // DW_OP_consts signed LEB128 integer constant
1155 //----------------------------------------------------------------------
1156 case DW_OP_const1u : stack.push_back(( uint8_t)opcodes.GetU8(&offset)); break;
1157 case DW_OP_const1s : stack.push_back(( int8_t)opcodes.GetU8(&offset)); break;
1158 case DW_OP_const2u : stack.push_back((uint16_t)opcodes.GetU16(&offset)); break;
1159 case DW_OP_const2s : stack.push_back(( int16_t)opcodes.GetU16(&offset)); break;
1160 case DW_OP_const4u : stack.push_back((uint32_t)opcodes.GetU32(&offset)); break;
1161 case DW_OP_const4s : stack.push_back(( int32_t)opcodes.GetU32(&offset)); break;
1162 case DW_OP_const8u : stack.push_back((uint64_t)opcodes.GetU64(&offset)); break;
1163 case DW_OP_const8s : stack.push_back(( int64_t)opcodes.GetU64(&offset)); break;
1164 case DW_OP_constu : stack.push_back(opcodes.GetULEB128(&offset)); break;
1165 case DW_OP_consts : stack.push_back(opcodes.GetSLEB128(&offset)); break;
1166
1167 //----------------------------------------------------------------------
1168 // OPCODE: DW_OP_dup
1169 // OPERANDS: none
1170 // DESCRIPTION: duplicates the value at the top of the stack
1171 //----------------------------------------------------------------------
1172 case DW_OP_dup:
1173 if (stack.empty())
1174 {
1175 if (error_ptr)
1176 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1177 return false;
1178 }
1179 else
1180 stack.push_back(stack.back());
1181 break;
1182
1183 //----------------------------------------------------------------------
1184 // OPCODE: DW_OP_drop
1185 // OPERANDS: none
1186 // DESCRIPTION: pops the value at the top of the stack
1187 //----------------------------------------------------------------------
1188 case DW_OP_drop:
1189 if (stack.empty())
1190 {
1191 if (error_ptr)
1192 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1193 return false;
1194 }
1195 else
1196 stack.pop_back();
1197 break;
1198
1199 //----------------------------------------------------------------------
1200 // OPCODE: DW_OP_over
1201 // OPERANDS: none
1202 // DESCRIPTION: Duplicates the entry currently second in the stack at
1203 // the top of the stack.
1204 //----------------------------------------------------------------------
1205 case DW_OP_over:
1206 if (stack.size() < 2)
1207 {
1208 if (error_ptr)
1209 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_over.");
1210 return false;
1211 }
1212 else
1213 stack.push_back(stack[stack.size() - 2]);
1214 break;
1215
1216
1217 //----------------------------------------------------------------------
1218 // OPCODE: DW_OP_pick
1219 // OPERANDS: uint8_t index into the current stack
1220 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1221 // inclusive) is pushed on the stack
1222 //----------------------------------------------------------------------
1223 case DW_OP_pick:
1224 {
1225 uint8_t pick_idx = opcodes.GetU8(&offset);
1226 if (pick_idx < stack.size())
1227 stack.push_back(stack[pick_idx]);
1228 else
1229 {
1230 if (error_ptr)
1231 error_ptr->SetErrorStringWithFormat("Index %u out of range for DW_OP_pick.\n", pick_idx);
1232 return false;
1233 }
1234 }
1235 break;
1236
1237 //----------------------------------------------------------------------
1238 // OPCODE: DW_OP_swap
1239 // OPERANDS: none
1240 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1241 // of the stack becomes the second stack entry, and the second entry
1242 // becomes the top of the stack
1243 //----------------------------------------------------------------------
1244 case DW_OP_swap:
1245 if (stack.size() < 2)
1246 {
1247 if (error_ptr)
1248 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_swap.");
1249 return false;
1250 }
1251 else
1252 {
1253 tmp = stack.back();
1254 stack.back() = stack[stack.size() - 2];
1255 stack[stack.size() - 2] = tmp;
1256 }
1257 break;
1258
1259 //----------------------------------------------------------------------
1260 // OPCODE: DW_OP_rot
1261 // OPERANDS: none
1262 // DESCRIPTION: Rotates the first three stack entries. The entry at
1263 // the top of the stack becomes the third stack entry, the second
1264 // entry becomes the top of the stack, and the third entry becomes
1265 // the second entry.
1266 //----------------------------------------------------------------------
1267 case DW_OP_rot:
1268 if (stack.size() < 3)
1269 {
1270 if (error_ptr)
1271 error_ptr->SetErrorString("Expression stack needs at least 3 items for DW_OP_rot.");
1272 return false;
1273 }
1274 else
1275 {
1276 size_t last_idx = stack.size() - 1;
1277 Value old_top = stack[last_idx];
1278 stack[last_idx] = stack[last_idx - 1];
1279 stack[last_idx - 1] = stack[last_idx - 2];
1280 stack[last_idx - 2] = old_top;
1281 }
1282 break;
1283
1284 //----------------------------------------------------------------------
1285 // OPCODE: DW_OP_abs
1286 // OPERANDS: none
1287 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1288 // value and pushes its absolute value. If the absolute value can not be
1289 // represented, the result is undefined.
1290 //----------------------------------------------------------------------
1291 case DW_OP_abs:
1292 if (stack.empty())
1293 {
1294 if (error_ptr)
1295 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_abs.");
1296 return false;
1297 }
1298 else if (stack.back().ResolveValue(exe_ctx, ast_context).AbsoluteValue() == false)
1299 {
1300 if (error_ptr)
1301 error_ptr->SetErrorString("Failed to take the absolute value of the first stack item.");
1302 return false;
1303 }
1304 break;
1305
1306 //----------------------------------------------------------------------
1307 // OPCODE: DW_OP_and
1308 // OPERANDS: none
1309 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1310 // operation on the two, and pushes the result.
1311 //----------------------------------------------------------------------
1312 case DW_OP_and:
1313 if (stack.size() < 2)
1314 {
1315 if (error_ptr)
1316 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_and.");
1317 return false;
1318 }
1319 else
1320 {
1321 tmp = stack.back();
1322 stack.pop_back();
1323 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) & tmp.ResolveValue(exe_ctx, ast_context);
1324 }
1325 break;
1326
1327 //----------------------------------------------------------------------
1328 // OPCODE: DW_OP_div
1329 // OPERANDS: none
1330 // DESCRIPTION: pops the top two stack values, divides the former second
1331 // entry by the former top of the stack using signed division, and
1332 // pushes the result.
1333 //----------------------------------------------------------------------
1334 case DW_OP_div:
1335 if (stack.size() < 2)
1336 {
1337 if (error_ptr)
1338 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_div.");
1339 return false;
1340 }
1341 else
1342 {
1343 tmp = stack.back();
1344 if (tmp.ResolveValue(exe_ctx, ast_context).IsZero())
1345 {
1346 if (error_ptr)
1347 error_ptr->SetErrorString("Divide by zero.");
1348 return false;
1349 }
1350 else
1351 {
1352 stack.pop_back();
1353 stack.back() = stack.back().ResolveValue(exe_ctx, ast_context) / tmp.ResolveValue(exe_ctx, ast_context);
1354 if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1355 {
1356 if (error_ptr)
1357 error_ptr->SetErrorString("Divide failed.");
1358 return false;
1359 }
1360 }
1361 }
1362 break;
1363
1364 //----------------------------------------------------------------------
1365 // OPCODE: DW_OP_minus
1366 // OPERANDS: none
1367 // DESCRIPTION: pops the top two stack values, subtracts the former top
1368 // of the stack from the former second entry, and pushes the result.
1369 //----------------------------------------------------------------------
1370 case DW_OP_minus:
1371 if (stack.size() < 2)
1372 {
1373 if (error_ptr)
1374 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_minus.");
1375 return false;
1376 }
1377 else
1378 {
1379 tmp = stack.back();
1380 stack.pop_back();
1381 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) - tmp.ResolveValue(exe_ctx, ast_context);
1382 }
1383 break;
1384
1385 //----------------------------------------------------------------------
1386 // OPCODE: DW_OP_mod
1387 // OPERANDS: none
1388 // DESCRIPTION: pops the top two stack values and pushes the result of
1389 // the calculation: former second stack entry modulo the former top of
1390 // the stack.
1391 //----------------------------------------------------------------------
1392 case DW_OP_mod:
1393 if (stack.size() < 2)
1394 {
1395 if (error_ptr)
1396 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mod.");
1397 return false;
1398 }
1399 else
1400 {
1401 tmp = stack.back();
1402 stack.pop_back();
1403 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) % tmp.ResolveValue(exe_ctx, ast_context);
1404 }
1405 break;
1406
1407
1408 //----------------------------------------------------------------------
1409 // OPCODE: DW_OP_mul
1410 // OPERANDS: none
1411 // DESCRIPTION: pops the top two stack entries, multiplies them
1412 // together, and pushes the result.
1413 //----------------------------------------------------------------------
1414 case DW_OP_mul:
1415 if (stack.size() < 2)
1416 {
1417 if (error_ptr)
1418 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mul.");
1419 return false;
1420 }
1421 else
1422 {
1423 tmp = stack.back();
1424 stack.pop_back();
1425 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) * tmp.ResolveValue(exe_ctx, ast_context);
1426 }
1427 break;
1428
1429 //----------------------------------------------------------------------
1430 // OPCODE: DW_OP_neg
1431 // OPERANDS: none
1432 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1433 //----------------------------------------------------------------------
1434 case DW_OP_neg:
1435 if (stack.empty())
1436 {
1437 if (error_ptr)
1438 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_neg.");
1439 return false;
1440 }
1441 else
1442 {
1443 if (stack.back().ResolveValue(exe_ctx, ast_context).UnaryNegate() == false)
1444 {
1445 if (error_ptr)
1446 error_ptr->SetErrorString("Unary negate failed.");
1447 return false;
1448 }
1449 }
1450 break;
1451
1452 //----------------------------------------------------------------------
1453 // OPCODE: DW_OP_not
1454 // OPERANDS: none
1455 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1456 // complement
1457 //----------------------------------------------------------------------
1458 case DW_OP_not:
1459 if (stack.empty())
1460 {
1461 if (error_ptr)
1462 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_not.");
1463 return false;
1464 }
1465 else
1466 {
1467 if (stack.back().ResolveValue(exe_ctx, ast_context).OnesComplement() == false)
1468 {
1469 if (error_ptr)
1470 error_ptr->SetErrorString("Logical NOT failed.");
1471 return false;
1472 }
1473 }
1474 break;
1475
1476 //----------------------------------------------------------------------
1477 // OPCODE: DW_OP_or
1478 // OPERANDS: none
1479 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1480 // operation on the two, and pushes the result.
1481 //----------------------------------------------------------------------
1482 case DW_OP_or:
1483 if (stack.size() < 2)
1484 {
1485 if (error_ptr)
1486 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_or.");
1487 return false;
1488 }
1489 else
1490 {
1491 tmp = stack.back();
1492 stack.pop_back();
1493 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) | tmp.ResolveValue(exe_ctx, ast_context);
1494 }
1495 break;
1496
1497 //----------------------------------------------------------------------
1498 // OPCODE: DW_OP_plus
1499 // OPERANDS: none
1500 // DESCRIPTION: pops the top two stack entries, adds them together, and
1501 // pushes the result.
1502 //----------------------------------------------------------------------
1503 case DW_OP_plus:
1504 if (stack.size() < 2)
1505 {
1506 if (error_ptr)
1507 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_plus.");
1508 return false;
1509 }
1510 else
1511 {
1512 tmp = stack.back();
1513 stack.pop_back();
1514 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) + tmp.ResolveValue(exe_ctx, ast_context);
1515 }
1516 break;
1517
1518 //----------------------------------------------------------------------
1519 // OPCODE: DW_OP_plus_uconst
1520 // OPERANDS: none
1521 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1522 // constant operand and pushes the result.
1523 //----------------------------------------------------------------------
1524 case DW_OP_plus_uconst:
1525 if (stack.empty())
1526 {
1527 if (error_ptr)
1528 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1529 return false;
1530 }
1531 else
1532 {
1533 uint32_t uconst_value = opcodes.GetULEB128(&offset);
1534 // Implicit conversion from a UINT to a Scalar...
1535 stack.back().ResolveValue(exe_ctx, ast_context) += uconst_value;
1536 if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1537 {
1538 if (error_ptr)
1539 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1540 return false;
1541 }
1542 }
1543 break;
1544
1545 //----------------------------------------------------------------------
1546 // OPCODE: DW_OP_shl
1547 // OPERANDS: none
1548 // DESCRIPTION: pops the top two stack entries, shifts the former
1549 // second entry left by the number of bits specified by the former top
1550 // of the stack, and pushes the result.
1551 //----------------------------------------------------------------------
1552 case DW_OP_shl:
1553 if (stack.size() < 2)
1554 {
1555 if (error_ptr)
1556 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shl.");
1557 return false;
1558 }
1559 else
1560 {
1561 tmp = stack.back();
1562 stack.pop_back();
1563 stack.back().ResolveValue(exe_ctx, ast_context) <<= tmp.ResolveValue(exe_ctx, ast_context);
1564 }
1565 break;
1566
1567 //----------------------------------------------------------------------
1568 // OPCODE: DW_OP_shr
1569 // OPERANDS: none
1570 // DESCRIPTION: pops the top two stack entries, shifts the former second
1571 // entry right logically (filling with zero bits) by the number of bits
1572 // specified by the former top of the stack, and pushes the result.
1573 //----------------------------------------------------------------------
1574 case DW_OP_shr:
1575 if (stack.size() < 2)
1576 {
1577 if (error_ptr)
1578 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shr.");
1579 return false;
1580 }
1581 else
1582 {
1583 tmp = stack.back();
1584 stack.pop_back();
1585 if (stack.back().ResolveValue(exe_ctx, ast_context).ShiftRightLogical(tmp.ResolveValue(exe_ctx, ast_context)) == false)
1586 {
1587 if (error_ptr)
1588 error_ptr->SetErrorString("DW_OP_shr failed.");
1589 return false;
1590 }
1591 }
1592 break;
1593
1594 //----------------------------------------------------------------------
1595 // OPCODE: DW_OP_shra
1596 // OPERANDS: none
1597 // DESCRIPTION: pops the top two stack entries, shifts the former second
1598 // entry right arithmetically (divide the magnitude by 2, keep the same
1599 // sign for the result) by the number of bits specified by the former
1600 // top of the stack, and pushes the result.
1601 //----------------------------------------------------------------------
1602 case DW_OP_shra:
1603 if (stack.size() < 2)
1604 {
1605 if (error_ptr)
1606 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shra.");
1607 return false;
1608 }
1609 else
1610 {
1611 tmp = stack.back();
1612 stack.pop_back();
1613 stack.back().ResolveValue(exe_ctx, ast_context) >>= tmp.ResolveValue(exe_ctx, ast_context);
1614 }
1615 break;
1616
1617 //----------------------------------------------------------------------
1618 // OPCODE: DW_OP_xor
1619 // OPERANDS: none
1620 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1621 // exclusive-or operation on the two, and pushes the result.
1622 //----------------------------------------------------------------------
1623 case DW_OP_xor:
1624 if (stack.size() < 2)
1625 {
1626 if (error_ptr)
1627 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_xor.");
1628 return false;
1629 }
1630 else
1631 {
1632 tmp = stack.back();
1633 stack.pop_back();
1634 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) ^ tmp.ResolveValue(exe_ctx, ast_context);
1635 }
1636 break;
1637
1638
1639 //----------------------------------------------------------------------
1640 // OPCODE: DW_OP_skip
1641 // OPERANDS: int16_t
1642 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1643 // signed integer constant. The 2-byte constant is the number of bytes
1644 // of the DWARF expression to skip forward or backward from the current
1645 // operation, beginning after the 2-byte constant.
1646 //----------------------------------------------------------------------
1647 case DW_OP_skip:
1648 {
1649 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1650 uint32_t new_offset = offset + skip_offset;
1651 if (new_offset >= opcodes_offset && new_offset < end_offset)
1652 offset = new_offset;
1653 else
1654 {
1655 if (error_ptr)
1656 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
1657 return false;
1658 }
1659 }
1660 break;
1661
1662 //----------------------------------------------------------------------
1663 // OPCODE: DW_OP_bra
1664 // OPERANDS: int16_t
1665 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1666 // signed integer constant. This operation pops the top of stack. If
1667 // the value popped is not the constant 0, the 2-byte constant operand
1668 // is the number of bytes of the DWARF expression to skip forward or
1669 // backward from the current operation, beginning after the 2-byte
1670 // constant.
1671 //----------------------------------------------------------------------
1672 case DW_OP_bra:
1673 {
1674 tmp = stack.back();
1675 stack.pop_back();
1676 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1677 Scalar zero(0);
1678 if (tmp.ResolveValue(exe_ctx, ast_context) != zero)
1679 {
1680 uint32_t new_offset = offset + bra_offset;
1681 if (new_offset >= opcodes_offset && new_offset < end_offset)
1682 offset = new_offset;
1683 else
1684 {
1685 if (error_ptr)
1686 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
1687 return false;
1688 }
1689 }
1690 }
1691 break;
1692
1693 //----------------------------------------------------------------------
1694 // OPCODE: DW_OP_eq
1695 // OPERANDS: none
1696 // DESCRIPTION: pops the top two stack values, compares using the
1697 // equals (==) operator.
1698 // STACK RESULT: push the constant value 1 onto the stack if the result
1699 // of the operation is true or the constant value 0 if the result of the
1700 // operation is false.
1701 //----------------------------------------------------------------------
1702 case DW_OP_eq:
1703 if (stack.size() < 2)
1704 {
1705 if (error_ptr)
1706 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_eq.");
1707 return false;
1708 }
1709 else
1710 {
1711 tmp = stack.back();
1712 stack.pop_back();
1713 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) == tmp.ResolveValue(exe_ctx, ast_context);
1714 }
1715 break;
1716
1717 //----------------------------------------------------------------------
1718 // OPCODE: DW_OP_ge
1719 // OPERANDS: none
1720 // DESCRIPTION: pops the top two stack values, compares using the
1721 // greater than or equal to (>=) operator.
1722 // STACK RESULT: push the constant value 1 onto the stack if the result
1723 // of the operation is true or the constant value 0 if the result of the
1724 // operation is false.
1725 //----------------------------------------------------------------------
1726 case DW_OP_ge:
1727 if (stack.size() < 2)
1728 {
1729 if (error_ptr)
1730 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ge.");
1731 return false;
1732 }
1733 else
1734 {
1735 tmp = stack.back();
1736 stack.pop_back();
1737 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) >= tmp.ResolveValue(exe_ctx, ast_context);
1738 }
1739 break;
1740
1741 //----------------------------------------------------------------------
1742 // OPCODE: DW_OP_gt
1743 // OPERANDS: none
1744 // DESCRIPTION: pops the top two stack values, compares using the
1745 // greater than (>) operator.
1746 // STACK RESULT: push the constant value 1 onto the stack if the result
1747 // of the operation is true or the constant value 0 if the result of the
1748 // operation is false.
1749 //----------------------------------------------------------------------
1750 case DW_OP_gt:
1751 if (stack.size() < 2)
1752 {
1753 if (error_ptr)
1754 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_gt.");
1755 return false;
1756 }
1757 else
1758 {
1759 tmp = stack.back();
1760 stack.pop_back();
1761 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) > tmp.ResolveValue(exe_ctx, ast_context);
1762 }
1763 break;
1764
1765 //----------------------------------------------------------------------
1766 // OPCODE: DW_OP_le
1767 // OPERANDS: none
1768 // DESCRIPTION: pops the top two stack values, compares using the
1769 // less than or equal to (<=) operator.
1770 // STACK RESULT: push the constant value 1 onto the stack if the result
1771 // of the operation is true or the constant value 0 if the result of the
1772 // operation is false.
1773 //----------------------------------------------------------------------
1774 case DW_OP_le:
1775 if (stack.size() < 2)
1776 {
1777 if (error_ptr)
1778 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_le.");
1779 return false;
1780 }
1781 else
1782 {
1783 tmp = stack.back();
1784 stack.pop_back();
1785 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) <= tmp.ResolveValue(exe_ctx, ast_context);
1786 }
1787 break;
1788
1789 //----------------------------------------------------------------------
1790 // OPCODE: DW_OP_lt
1791 // OPERANDS: none
1792 // DESCRIPTION: pops the top two stack values, compares using the
1793 // less than (<) operator.
1794 // STACK RESULT: push the constant value 1 onto the stack if the result
1795 // of the operation is true or the constant value 0 if the result of the
1796 // operation is false.
1797 //----------------------------------------------------------------------
1798 case DW_OP_lt:
1799 if (stack.size() < 2)
1800 {
1801 if (error_ptr)
1802 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_lt.");
1803 return false;
1804 }
1805 else
1806 {
1807 tmp = stack.back();
1808 stack.pop_back();
1809 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) < tmp.ResolveValue(exe_ctx, ast_context);
1810 }
1811 break;
1812
1813 //----------------------------------------------------------------------
1814 // OPCODE: DW_OP_ne
1815 // OPERANDS: none
1816 // DESCRIPTION: pops the top two stack values, compares using the
1817 // not equal (!=) operator.
1818 // STACK RESULT: push the constant value 1 onto the stack if the result
1819 // of the operation is true or the constant value 0 if the result of the
1820 // operation is false.
1821 //----------------------------------------------------------------------
1822 case DW_OP_ne:
1823 if (stack.size() < 2)
1824 {
1825 if (error_ptr)
1826 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ne.");
1827 return false;
1828 }
1829 else
1830 {
1831 tmp = stack.back();
1832 stack.pop_back();
1833 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) != tmp.ResolveValue(exe_ctx, ast_context);
1834 }
1835 break;
1836
1837 //----------------------------------------------------------------------
1838 // OPCODE: DW_OP_litn
1839 // OPERANDS: none
1840 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1841 // STACK RESULT: push the unsigned literal constant value onto the top
1842 // of the stack.
1843 //----------------------------------------------------------------------
1844 case DW_OP_lit0:
1845 case DW_OP_lit1:
1846 case DW_OP_lit2:
1847 case DW_OP_lit3:
1848 case DW_OP_lit4:
1849 case DW_OP_lit5:
1850 case DW_OP_lit6:
1851 case DW_OP_lit7:
1852 case DW_OP_lit8:
1853 case DW_OP_lit9:
1854 case DW_OP_lit10:
1855 case DW_OP_lit11:
1856 case DW_OP_lit12:
1857 case DW_OP_lit13:
1858 case DW_OP_lit14:
1859 case DW_OP_lit15:
1860 case DW_OP_lit16:
1861 case DW_OP_lit17:
1862 case DW_OP_lit18:
1863 case DW_OP_lit19:
1864 case DW_OP_lit20:
1865 case DW_OP_lit21:
1866 case DW_OP_lit22:
1867 case DW_OP_lit23:
1868 case DW_OP_lit24:
1869 case DW_OP_lit25:
1870 case DW_OP_lit26:
1871 case DW_OP_lit27:
1872 case DW_OP_lit28:
1873 case DW_OP_lit29:
1874 case DW_OP_lit30:
1875 case DW_OP_lit31:
1876 stack.push_back(op - DW_OP_lit0);
1877 break;
1878
1879 //----------------------------------------------------------------------
1880 // OPCODE: DW_OP_regN
1881 // OPERANDS: none
1882 // DESCRIPTION: Push the value in register n on the top of the stack.
1883 //----------------------------------------------------------------------
1884 case DW_OP_reg0:
1885 case DW_OP_reg1:
1886 case DW_OP_reg2:
1887 case DW_OP_reg3:
1888 case DW_OP_reg4:
1889 case DW_OP_reg5:
1890 case DW_OP_reg6:
1891 case DW_OP_reg7:
1892 case DW_OP_reg8:
1893 case DW_OP_reg9:
1894 case DW_OP_reg10:
1895 case DW_OP_reg11:
1896 case DW_OP_reg12:
1897 case DW_OP_reg13:
1898 case DW_OP_reg14:
1899 case DW_OP_reg15:
1900 case DW_OP_reg16:
1901 case DW_OP_reg17:
1902 case DW_OP_reg18:
1903 case DW_OP_reg19:
1904 case DW_OP_reg20:
1905 case DW_OP_reg21:
1906 case DW_OP_reg22:
1907 case DW_OP_reg23:
1908 case DW_OP_reg24:
1909 case DW_OP_reg25:
1910 case DW_OP_reg26:
1911 case DW_OP_reg27:
1912 case DW_OP_reg28:
1913 case DW_OP_reg29:
1914 case DW_OP_reg30:
1915 case DW_OP_reg31:
1916 {
1917 reg_num = op - DW_OP_reg0;
1918
Jason Molenda8e69de42010-11-20 01:28:30 +00001919 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00001920 stack.push_back(tmp);
1921 else
1922 return false;
1923 }
1924 break;
1925 //----------------------------------------------------------------------
1926 // OPCODE: DW_OP_regx
1927 // OPERANDS:
1928 // ULEB128 literal operand that encodes the register.
1929 // DESCRIPTION: Push the value in register on the top of the stack.
1930 //----------------------------------------------------------------------
1931 case DW_OP_regx:
1932 {
1933 reg_num = opcodes.GetULEB128(&offset);
Jason Molenda8e69de42010-11-20 01:28:30 +00001934 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00001935 stack.push_back(tmp);
1936 else
1937 return false;
1938 }
1939 break;
1940
1941 //----------------------------------------------------------------------
1942 // OPCODE: DW_OP_bregN
1943 // OPERANDS:
1944 // SLEB128 offset from register N
1945 // DESCRIPTION: Value is in memory at the address specified by register
1946 // N plus an offset.
1947 //----------------------------------------------------------------------
1948 case DW_OP_breg0:
1949 case DW_OP_breg1:
1950 case DW_OP_breg2:
1951 case DW_OP_breg3:
1952 case DW_OP_breg4:
1953 case DW_OP_breg5:
1954 case DW_OP_breg6:
1955 case DW_OP_breg7:
1956 case DW_OP_breg8:
1957 case DW_OP_breg9:
1958 case DW_OP_breg10:
1959 case DW_OP_breg11:
1960 case DW_OP_breg12:
1961 case DW_OP_breg13:
1962 case DW_OP_breg14:
1963 case DW_OP_breg15:
1964 case DW_OP_breg16:
1965 case DW_OP_breg17:
1966 case DW_OP_breg18:
1967 case DW_OP_breg19:
1968 case DW_OP_breg20:
1969 case DW_OP_breg21:
1970 case DW_OP_breg22:
1971 case DW_OP_breg23:
1972 case DW_OP_breg24:
1973 case DW_OP_breg25:
1974 case DW_OP_breg26:
1975 case DW_OP_breg27:
1976 case DW_OP_breg28:
1977 case DW_OP_breg29:
1978 case DW_OP_breg30:
1979 case DW_OP_breg31:
1980 {
1981 reg_num = op - DW_OP_breg0;
1982
Jason Molenda8e69de42010-11-20 01:28:30 +00001983 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00001984 {
1985 int64_t breg_offset = opcodes.GetSLEB128(&offset);
1986 tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
1987 stack.push_back(tmp);
1988 stack.back().SetValueType (Value::eValueTypeLoadAddress);
1989 }
1990 else
1991 return false;
1992 }
1993 break;
1994 //----------------------------------------------------------------------
1995 // OPCODE: DW_OP_bregx
1996 // OPERANDS: 2
1997 // ULEB128 literal operand that encodes the register.
1998 // SLEB128 offset from register N
1999 // DESCRIPTION: Value is in memory at the address specified by register
2000 // N plus an offset.
2001 //----------------------------------------------------------------------
2002 case DW_OP_bregx:
2003 {
2004 reg_num = opcodes.GetULEB128(&offset);
2005
Jason Molenda8e69de42010-11-20 01:28:30 +00002006 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00002007 {
2008 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2009 tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
2010 stack.push_back(tmp);
2011 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2012 }
2013 else
2014 return false;
2015 }
2016 break;
2017
2018 case DW_OP_fbreg:
2019 if (exe_ctx && exe_ctx->frame)
2020 {
2021 Scalar value;
2022 if (exe_ctx->frame->GetFrameBaseValue(value, error_ptr))
2023 {
2024 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2025 value += fbreg_offset;
2026 stack.push_back(value);
2027 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2028 }
2029 else
2030 return false;
2031 }
2032 else
2033 {
2034 if (error_ptr)
2035 error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_fbreg opcode.");
2036 return false;
2037 }
2038 break;
2039
2040 //----------------------------------------------------------------------
2041 // OPCODE: DW_OP_nop
2042 // OPERANDS: none
2043 // DESCRIPTION: A place holder. It has no effect on the location stack
2044 // or any of its values.
2045 //----------------------------------------------------------------------
2046 case DW_OP_nop:
2047 break;
2048
2049 //----------------------------------------------------------------------
2050 // OPCODE: DW_OP_piece
2051 // OPERANDS: 1
2052 // ULEB128: byte size of the piece
2053 // DESCRIPTION: The operand describes the size in bytes of the piece of
2054 // the object referenced by the DWARF expression whose result is at the
2055 // top of the stack. If the piece is located in a register, but does not
2056 // occupy the entire register, the placement of the piece within that
2057 // register is defined by the ABI.
2058 //
2059 // Many compilers store a single variable in sets of registers, or store
2060 // a variable partially in memory and partially in registers.
2061 // DW_OP_piece provides a way of describing how large a part of a
2062 // variable a particular DWARF expression refers to.
2063 //----------------------------------------------------------------------
2064 case DW_OP_piece:
2065 if (error_ptr)
2066 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_piece.");
2067 return false;
2068
2069 //----------------------------------------------------------------------
2070 // OPCODE: DW_OP_push_object_address
2071 // OPERANDS: none
2072 // DESCRIPTION: Pushes the address of the object currently being
2073 // evaluated as part of evaluation of a user presented expression.
2074 // This object may correspond to an independent variable described by
2075 // its own DIE or it may be a component of an array, structure, or class
2076 // whose address has been dynamically determined by an earlier step
2077 // during user expression evaluation.
2078 //----------------------------------------------------------------------
2079 case DW_OP_push_object_address:
2080 if (error_ptr)
2081 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_push_object_address.");
2082 return false;
2083
2084 //----------------------------------------------------------------------
2085 // OPCODE: DW_OP_call2
2086 // OPERANDS:
2087 // uint16_t compile unit relative offset of a DIE
2088 // DESCRIPTION: Performs subroutine calls during evaluation
2089 // of a DWARF expression. The operand is the 2-byte unsigned offset
2090 // of a debugging information entry in the current compilation unit.
2091 //
2092 // Operand interpretation is exactly like that for DW_FORM_ref2.
2093 //
2094 // This operation transfers control of DWARF expression evaluation
2095 // to the DW_AT_location attribute of the referenced DIE. If there is
2096 // no such attribute, then there is no effect. Execution of the DWARF
2097 // expression of a DW_AT_location attribute may add to and/or remove from
2098 // values on the stack. Execution returns to the point following the call
2099 // when the end of the attribute is reached. Values on the stack at the
2100 // time of the call may be used as parameters by the called expression
2101 // and values left on the stack by the called expression may be used as
2102 // return values by prior agreement between the calling and called
2103 // expressions.
2104 //----------------------------------------------------------------------
2105 case DW_OP_call2:
2106 if (error_ptr)
2107 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call2.");
2108 return false;
2109 //----------------------------------------------------------------------
2110 // OPCODE: DW_OP_call4
2111 // OPERANDS: 1
2112 // uint32_t compile unit relative offset of a DIE
2113 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2114 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset
2115 // of a debugging information entry in the current compilation unit.
2116 //
2117 // Operand interpretation DW_OP_call4 is exactly like that for
2118 // DW_FORM_ref4.
2119 //
2120 // This operation transfers control of DWARF expression evaluation
2121 // to the DW_AT_location attribute of the referenced DIE. If there is
2122 // no such attribute, then there is no effect. Execution of the DWARF
2123 // expression of a DW_AT_location attribute may add to and/or remove from
2124 // values on the stack. Execution returns to the point following the call
2125 // when the end of the attribute is reached. Values on the stack at the
2126 // time of the call may be used as parameters by the called expression
2127 // and values left on the stack by the called expression may be used as
2128 // return values by prior agreement between the calling and called
2129 // expressions.
2130 //----------------------------------------------------------------------
2131 case DW_OP_call4:
2132 if (error_ptr)
2133 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call4.");
2134 return false;
2135
2136
2137 //----------------------------------------------------------------------
2138 // OPCODE: DW_OP_call_ref
2139 // OPERANDS:
2140 // uint32_t absolute DIE offset for 32-bit DWARF or a uint64_t
2141 // absolute DIE offset for 64 bit DWARF.
2142 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2143 // expression. Takes a single operand. In the 32-bit DWARF format, the
2144 // operand is a 4-byte unsigned value; in the 64-bit DWARF format, it
2145 // is an 8-byte unsigned value. The operand is used as the offset of a
2146 // debugging information entry in a .debug_info section which may be
2147 // contained in a shared object for executable other than that
2148 // containing the operator. For references from one shared object or
2149 // executable to another, the relocation must be performed by the
2150 // consumer.
2151 //
2152 // Operand interpretation of DW_OP_call_ref is exactly like that for
2153 // DW_FORM_ref_addr.
2154 //
2155 // This operation transfers control of DWARF expression evaluation
2156 // to the DW_AT_location attribute of the referenced DIE. If there is
2157 // no such attribute, then there is no effect. Execution of the DWARF
2158 // expression of a DW_AT_location attribute may add to and/or remove from
2159 // values on the stack. Execution returns to the point following the call
2160 // when the end of the attribute is reached. Values on the stack at the
2161 // time of the call may be used as parameters by the called expression
2162 // and values left on the stack by the called expression may be used as
2163 // return values by prior agreement between the calling and called
2164 // expressions.
2165 //----------------------------------------------------------------------
2166 case DW_OP_call_ref:
2167 if (error_ptr)
2168 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call_ref.");
2169 return false;
2170
2171 //----------------------------------------------------------------------
2172 // OPCODE: DW_OP_APPLE_array_ref
2173 // OPERANDS: none
2174 // DESCRIPTION: Pops a value off the stack and uses it as the array
2175 // index. Pops a second value off the stack and uses it as the array
2176 // itself. Pushes a value onto the stack representing the element of
2177 // the array specified by the index.
2178 //----------------------------------------------------------------------
2179 case DW_OP_APPLE_array_ref:
2180 {
2181 if (stack.size() < 2)
2182 {
2183 if (error_ptr)
2184 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_array_ref.");
2185 return false;
2186 }
2187
2188 Value index_val = stack.back();
2189 stack.pop_back();
2190 Value array_val = stack.back();
2191 stack.pop_back();
2192
2193 Scalar &index_scalar = index_val.ResolveValue(exe_ctx, ast_context);
Greg Clayton381f9682011-04-01 18:14:08 +00002194 int64_t index = index_scalar.SLongLong(LLONG_MAX);
Chris Lattner24943d22010-06-08 16:52:24 +00002195
Greg Clayton381f9682011-04-01 18:14:08 +00002196 if (index == LLONG_MAX)
Chris Lattner24943d22010-06-08 16:52:24 +00002197 {
2198 if (error_ptr)
2199 error_ptr->SetErrorString("Invalid array index.");
2200 return false;
2201 }
2202
Greg Clayton6916e352010-11-13 03:52:47 +00002203 if (array_val.GetContextType() != Value::eContextTypeClangType)
Chris Lattner24943d22010-06-08 16:52:24 +00002204 {
2205 if (error_ptr)
2206 error_ptr->SetErrorString("Arrays without Clang types are unhandled at this time.");
2207 return false;
2208 }
2209
2210 if (array_val.GetValueType() != Value::eValueTypeLoadAddress &&
2211 array_val.GetValueType() != Value::eValueTypeHostAddress)
2212 {
2213 if (error_ptr)
2214 error_ptr->SetErrorString("Array must be stored in memory.");
2215 return false;
2216 }
2217
Greg Clayton462d4142010-09-29 01:12:09 +00002218 void *array_type = array_val.GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +00002219
2220 void *member_type;
2221 uint64_t size = 0;
2222
2223 if ((!ClangASTContext::IsPointerType(array_type, &member_type)) &&
2224 (!ClangASTContext::IsArrayType(array_type, &member_type, &size)))
2225 {
2226 if (error_ptr)
2227 error_ptr->SetErrorString("Array reference from something that is neither a pointer nor an array.");
2228 return false;
2229 }
2230
2231 if (size && (index >= size || index < 0))
2232 {
2233 if (error_ptr)
2234 error_ptr->SetErrorStringWithFormat("Out of bounds array access. %lld is not in [0, %llu]", index, size);
2235 return false;
2236 }
2237
Greg Clayton960d6a42010-08-03 00:35:52 +00002238 uint64_t member_bit_size = ClangASTType::GetClangTypeBitWidth(ast_context, member_type);
2239 uint64_t member_bit_align = ClangASTType::GetTypeBitAlign(ast_context, member_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002240 uint64_t member_bit_incr = ((member_bit_size + member_bit_align - 1) / member_bit_align) * member_bit_align;
2241 if (member_bit_incr % 8)
2242 {
2243 if (error_ptr)
2244 error_ptr->SetErrorStringWithFormat("Array increment is not byte aligned", index, size);
2245 return false;
2246 }
2247 int64_t member_offset = (int64_t)(member_bit_incr / 8) * index;
2248
2249 Value member;
2250
Greg Clayton6916e352010-11-13 03:52:47 +00002251 member.SetContext(Value::eContextTypeClangType, member_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002252 member.SetValueType(array_val.GetValueType());
2253
2254 addr_t array_base = (addr_t)array_val.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2255 addr_t member_loc = array_base + member_offset;
2256 member.GetScalar() = (uint64_t)member_loc;
2257
2258 stack.push_back(member);
2259 }
2260 break;
2261
2262 //----------------------------------------------------------------------
2263 // OPCODE: DW_OP_APPLE_uninit
2264 // OPERANDS: none
2265 // DESCRIPTION: Lets us know that the value is currently not initialized
2266 //----------------------------------------------------------------------
2267 case DW_OP_APPLE_uninit:
2268 //return eResultTypeErrorUninitialized;
2269 break; // Ignore this as we have seen cases where this value is incorrectly added
2270
2271 //----------------------------------------------------------------------
2272 // OPCODE: DW_OP_APPLE_assign
2273 // OPERANDS: none
2274 // DESCRIPTION: Pops a value off of the stack and assigns it to the next
2275 // item on the stack which must be something assignable (inferior
2276 // Variable, inferior Type with address, inferior register, or
2277 // expression local variable.
2278 //----------------------------------------------------------------------
2279 case DW_OP_APPLE_assign:
2280 if (stack.size() < 2)
2281 {
2282 if (error_ptr)
2283 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_assign.");
2284 return false;
2285 }
2286 else
2287 {
2288 tmp = stack.back();
2289 stack.pop_back();
2290 Value::ContextType context_type = stack.back().GetContextType();
Greg Claytoncd548032011-02-01 01:31:41 +00002291 StreamString new_value(Stream::eBinary, 4, lldb::endian::InlHostByteOrder());
Chris Lattner24943d22010-06-08 16:52:24 +00002292 switch (context_type)
2293 {
Greg Clayton6916e352010-11-13 03:52:47 +00002294 case Value::eContextTypeClangType:
Chris Lattner24943d22010-06-08 16:52:24 +00002295 {
Greg Clayton462d4142010-09-29 01:12:09 +00002296 void *clang_type = stack.back().GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +00002297
2298 if (ClangASTContext::IsAggregateType (clang_type))
2299 {
2300 Value::ValueType source_value_type = tmp.GetValueType();
2301 Value::ValueType target_value_type = stack.back().GetValueType();
2302
2303 addr_t source_addr = (addr_t)tmp.GetScalar().ULongLong();
2304 addr_t target_addr = (addr_t)stack.back().GetScalar().ULongLong();
2305
Greg Clayton960d6a42010-08-03 00:35:52 +00002306 size_t byte_size = (ClangASTType::GetClangTypeBitWidth(ast_context, clang_type) + 7) / 8;
Chris Lattner24943d22010-06-08 16:52:24 +00002307
2308 switch (source_value_type)
2309 {
Greg Clayton4fdf7602011-03-20 04:57:14 +00002310 case Value::eValueTypeScalar:
2311 case Value::eValueTypeFileAddress:
2312 break;
2313
Chris Lattner24943d22010-06-08 16:52:24 +00002314 case Value::eValueTypeLoadAddress:
2315 switch (target_value_type)
2316 {
2317 case Value::eValueTypeLoadAddress:
2318 {
2319 DataBufferHeap data;
2320 data.SetByteSize(byte_size);
2321
2322 Error error;
2323 if (exe_ctx->process->ReadMemory (source_addr, data.GetBytes(), byte_size, error) != byte_size)
2324 {
2325 if (error_ptr)
2326 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2327 return false;
2328 }
2329
2330 if (exe_ctx->process->WriteMemory (target_addr, data.GetBytes(), byte_size, error) != byte_size)
2331 {
2332 if (error_ptr)
2333 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2334 return false;
2335 }
2336 }
2337 break;
2338 case Value::eValueTypeHostAddress:
Greg Claytoncd548032011-02-01 01:31:41 +00002339 if (exe_ctx->process->GetByteOrder() != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +00002340 {
2341 if (error_ptr)
2342 error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2343 return false;
2344 }
2345 else
2346 {
2347 Error error;
2348 if (exe_ctx->process->ReadMemory (source_addr, (uint8_t*)target_addr, byte_size, error) != byte_size)
2349 {
2350 if (error_ptr)
2351 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2352 return false;
2353 }
2354 }
2355 break;
2356 default:
2357 return false;
2358 }
2359 break;
2360 case Value::eValueTypeHostAddress:
2361 switch (target_value_type)
2362 {
2363 case Value::eValueTypeLoadAddress:
Greg Claytoncd548032011-02-01 01:31:41 +00002364 if (exe_ctx->process->GetByteOrder() != lldb::endian::InlHostByteOrder())
Chris Lattner24943d22010-06-08 16:52:24 +00002365 {
2366 if (error_ptr)
2367 error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2368 return false;
2369 }
2370 else
2371 {
2372 Error error;
2373 if (exe_ctx->process->WriteMemory (target_addr, (uint8_t*)source_addr, byte_size, error) != byte_size)
2374 {
2375 if (error_ptr)
2376 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2377 return false;
2378 }
2379 }
2380 case Value::eValueTypeHostAddress:
2381 memcpy ((uint8_t*)target_addr, (uint8_t*)source_addr, byte_size);
2382 break;
2383 default:
2384 return false;
2385 }
2386 }
2387 }
2388 else
2389 {
Greg Clayton1674b122010-07-21 22:12:05 +00002390 if (!ClangASTType::SetValueFromScalar (ast_context,
2391 clang_type,
2392 tmp.ResolveValue(exe_ctx, ast_context),
2393 new_value))
Chris Lattner24943d22010-06-08 16:52:24 +00002394 {
2395 if (error_ptr)
2396 error_ptr->SetErrorStringWithFormat ("Couldn't extract a value from an integral type.\n");
2397 return false;
2398 }
2399
2400 Value::ValueType value_type = stack.back().GetValueType();
2401
2402 switch (value_type)
2403 {
2404 case Value::eValueTypeLoadAddress:
2405 case Value::eValueTypeHostAddress:
2406 {
Greg Claytonb3448432011-03-24 21:19:54 +00002407 AddressType address_type = (value_type == Value::eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost);
Chris Lattner24943d22010-06-08 16:52:24 +00002408 lldb::addr_t addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton1674b122010-07-21 22:12:05 +00002409 if (!ClangASTType::WriteToMemory (ast_context,
2410 clang_type,
2411 exe_ctx,
2412 addr,
2413 address_type,
2414 new_value))
Chris Lattner24943d22010-06-08 16:52:24 +00002415 {
2416 if (error_ptr)
2417 error_ptr->SetErrorStringWithFormat ("Failed to write value to memory at 0x%llx.\n", addr);
2418 return false;
2419 }
2420 }
2421 break;
2422
2423 default:
2424 break;
2425 }
2426 }
2427 }
2428 break;
2429
2430 default:
2431 if (error_ptr)
2432 error_ptr->SetErrorString ("Assign failed.");
2433 return false;
2434 }
2435 }
2436 break;
2437
2438 //----------------------------------------------------------------------
2439 // OPCODE: DW_OP_APPLE_address_of
2440 // OPERANDS: none
2441 // DESCRIPTION: Pops a value off of the stack and pushed its address.
2442 // The top item on the stack must be a variable, or already be a memory
2443 // location.
2444 //----------------------------------------------------------------------
2445 case DW_OP_APPLE_address_of:
2446 if (stack.empty())
2447 {
2448 if (error_ptr)
2449 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_address_of.");
2450 return false;
2451 }
2452 else
2453 {
2454 Value::ValueType value_type = stack.back().GetValueType();
2455 switch (value_type)
2456 {
2457 default:
2458 case Value::eValueTypeScalar: // raw scalar value
2459 if (error_ptr)
2460 error_ptr->SetErrorString("Top stack item isn't a memory based object.");
2461 return false;
2462
2463 case Value::eValueTypeLoadAddress: // load address value
2464 case Value::eValueTypeFileAddress: // file address value
2465 case Value::eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
2466 // Taking the address of an object reduces it to the address
2467 // of the value and removes any extra context it had.
2468 //stack.back().SetValueType(Value::eValueTypeScalar);
2469 stack.back().ClearContext();
2470 break;
2471 }
2472 }
2473 break;
2474
2475 //----------------------------------------------------------------------
2476 // OPCODE: DW_OP_APPLE_value_of
2477 // OPERANDS: none
2478 // DESCRIPTION: Pops a value off of the stack and pushed its value.
2479 // The top item on the stack must be a variable, expression variable.
2480 //----------------------------------------------------------------------
2481 case DW_OP_APPLE_value_of:
2482 if (stack.empty())
2483 {
2484 if (error_ptr)
2485 error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_value_of.");
2486 return false;
2487 }
2488 else if (!stack.back().ValueOf(exe_ctx, ast_context))
2489 {
2490 if (error_ptr)
2491 error_ptr->SetErrorString ("Top stack item isn't a valid candidate for DW_OP_APPLE_value_of.");
2492 return false;
2493 }
2494 break;
2495
2496 //----------------------------------------------------------------------
2497 // OPCODE: DW_OP_APPLE_deref_type
2498 // OPERANDS: none
2499 // DESCRIPTION: gets the value pointed to by the top stack item
2500 //----------------------------------------------------------------------
2501 case DW_OP_APPLE_deref_type:
2502 {
2503 if (stack.empty())
2504 {
2505 if (error_ptr)
2506 error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_deref_type.");
2507 return false;
2508 }
2509
2510 tmp = stack.back();
2511 stack.pop_back();
2512
Greg Clayton6916e352010-11-13 03:52:47 +00002513 if (tmp.GetContextType() != Value::eContextTypeClangType)
Chris Lattner24943d22010-06-08 16:52:24 +00002514 {
2515 if (error_ptr)
2516 error_ptr->SetErrorString("Item at top of expression stack must have a Clang type");
2517 return false;
2518 }
2519
Greg Clayton462d4142010-09-29 01:12:09 +00002520 void *ptr_type = tmp.GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +00002521 void *target_type;
2522
2523 if (!ClangASTContext::IsPointerType(ptr_type, &target_type))
2524 {
2525 if (error_ptr)
2526 error_ptr->SetErrorString("Dereferencing a non-pointer type");
2527 return false;
2528 }
2529
2530 // TODO do we want all pointers to be dereferenced as load addresses?
2531 Value::ValueType value_type = tmp.GetValueType();
2532
2533 tmp.ResolveValue(exe_ctx, ast_context);
2534
2535 tmp.SetValueType(value_type);
Greg Clayton6916e352010-11-13 03:52:47 +00002536 tmp.SetContext(Value::eContextTypeClangType, target_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002537
2538 stack.push_back(tmp);
2539 }
2540 break;
2541
2542 //----------------------------------------------------------------------
2543 // OPCODE: DW_OP_APPLE_expr_local
2544 // OPERANDS: ULEB128
2545 // DESCRIPTION: pushes the expression local variable index onto the
2546 // stack and set the appropriate context so we know the stack item is
2547 // an expression local variable index.
2548 //----------------------------------------------------------------------
2549 case DW_OP_APPLE_expr_local:
2550 {
Sean Callanana6223432010-08-20 01:02:30 +00002551 /*
Chris Lattner24943d22010-06-08 16:52:24 +00002552 uint32_t idx = opcodes.GetULEB128(&offset);
2553 if (expr_locals == NULL)
2554 {
2555 if (error_ptr)
2556 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) opcode encountered with no local variable list.\n", idx);
2557 return false;
2558 }
2559 Value *expr_local_variable = expr_locals->GetVariableAtIndex(idx);
2560 if (expr_local_variable == NULL)
2561 {
2562 if (error_ptr)
2563 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) with invalid index %u.\n", idx, idx);
2564 return false;
2565 }
2566 Value *proxy = expr_local_variable->CreateProxy();
2567 stack.push_back(*proxy);
2568 delete proxy;
Greg Clayton6916e352010-11-13 03:52:47 +00002569 //stack.back().SetContext (Value::eContextTypeClangType, expr_local_variable->GetClangType());
Sean Callanana6223432010-08-20 01:02:30 +00002570 */
Chris Lattner24943d22010-06-08 16:52:24 +00002571 }
2572 break;
2573
2574 //----------------------------------------------------------------------
2575 // OPCODE: DW_OP_APPLE_extern
2576 // OPERANDS: ULEB128
2577 // DESCRIPTION: pushes a proxy for the extern object index onto the
2578 // stack.
2579 //----------------------------------------------------------------------
2580 case DW_OP_APPLE_extern:
2581 {
Sean Callanan8c127202010-08-23 23:09:38 +00002582 /*
Chris Lattner24943d22010-06-08 16:52:24 +00002583 uint32_t idx = opcodes.GetULEB128(&offset);
2584 if (!decl_map)
2585 {
2586 if (error_ptr)
2587 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) opcode encountered with no decl map.\n", idx);
2588 return false;
2589 }
2590 Value *extern_var = decl_map->GetValueForIndex(idx);
2591 if (!extern_var)
2592 {
2593 if (error_ptr)
2594 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) with invalid index %u.\n", idx, idx);
2595 return false;
2596 }
2597 Value *proxy = extern_var->CreateProxy();
2598 stack.push_back(*proxy);
2599 delete proxy;
Sean Callanan8c127202010-08-23 23:09:38 +00002600 */
Chris Lattner24943d22010-06-08 16:52:24 +00002601 }
2602 break;
2603
2604 case DW_OP_APPLE_scalar_cast:
2605 if (stack.empty())
2606 {
2607 if (error_ptr)
2608 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_scalar_cast.");
2609 return false;
2610 }
2611 else
2612 {
2613 // Simple scalar cast
2614 if (!stack.back().ResolveValue(exe_ctx, ast_context).Cast((Scalar::Type)opcodes.GetU8(&offset)))
2615 {
2616 if (error_ptr)
2617 error_ptr->SetErrorString("Cast failed.");
2618 return false;
2619 }
2620 }
2621 break;
2622
2623
2624 case DW_OP_APPLE_clang_cast:
2625 if (stack.empty())
2626 {
2627 if (error_ptr)
2628 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_clang_cast.");
2629 return false;
2630 }
2631 else
2632 {
2633 void *clang_type = (void *)opcodes.GetMaxU64(&offset, sizeof(void*));
Greg Clayton6916e352010-11-13 03:52:47 +00002634 stack.back().SetContext (Value::eContextTypeClangType, clang_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002635 }
2636 break;
2637 //----------------------------------------------------------------------
2638 // OPCODE: DW_OP_APPLE_constf
2639 // OPERANDS: 1 byte float length, followed by that many bytes containing
2640 // the constant float data.
2641 // DESCRIPTION: Push a float value onto the expression stack.
2642 //----------------------------------------------------------------------
2643 case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size, followed by constant float data
2644 {
2645 uint8_t float_length = opcodes.GetU8(&offset);
2646 if (sizeof(float) == float_length)
2647 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetFloat (&offset);
2648 else if (sizeof(double) == float_length)
2649 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetDouble (&offset);
2650 else if (sizeof(long double) == float_length)
2651 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetLongDouble (&offset);
2652 else
2653 {
2654 StreamString new_value;
2655 opcodes.Dump(&new_value, offset, eFormatBytes, 1, float_length, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
2656
2657 if (error_ptr)
2658 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_constf(<%u> %s) unsupported float size.\n", float_length, new_value.GetData());
2659 return false;
2660 }
2661 tmp.SetValueType(Value::eValueTypeScalar);
2662 tmp.ClearContext();
2663 stack.push_back(tmp);
2664 }
2665 break;
2666 //----------------------------------------------------------------------
2667 // OPCODE: DW_OP_APPLE_clear
2668 // OPERANDS: none
2669 // DESCRIPTION: Clears the expression stack.
2670 //----------------------------------------------------------------------
2671 case DW_OP_APPLE_clear:
2672 stack.clear();
2673 break;
2674
2675 //----------------------------------------------------------------------
2676 // OPCODE: DW_OP_APPLE_error
2677 // OPERANDS: none
2678 // DESCRIPTION: Pops a value off of the stack and pushed its value.
2679 // The top item on the stack must be a variable, expression variable.
2680 //----------------------------------------------------------------------
2681 case DW_OP_APPLE_error: // 0xFF - Stops expression evaluation and returns an error (no args)
2682 if (error_ptr)
2683 error_ptr->SetErrorString ("Generic error.");
2684 return false;
2685 }
2686 }
2687
2688 if (stack.empty())
2689 {
2690 if (error_ptr)
2691 error_ptr->SetErrorString ("Stack empty after evaluation.");
2692 return false;
2693 }
2694 else if (log)
2695 {
Chris Lattner24943d22010-06-08 16:52:24 +00002696 size_t count = stack.size();
Sean Callanan6184dfe2010-06-23 00:47:48 +00002697 log->Printf("Stack after operation has %d values:", count);
Chris Lattner24943d22010-06-08 16:52:24 +00002698 for (size_t i=0; i<count; ++i)
2699 {
2700 StreamString new_value;
2701 new_value.Printf("[%zu]", i);
2702 stack[i].Dump(&new_value);
Sean Callanan6184dfe2010-06-23 00:47:48 +00002703 log->Printf(" %s", new_value.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +00002704 }
2705 }
2706
2707 result = stack.back();
2708 return true; // Return true on success
2709}
2710