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