blob: df4170fee9e3b1710291cdac6db700f08beeab85 [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)
849 reg_ctx = exe_ctx->frame->GetRegisterContext();
850
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;
1036 case 5: ptr = ptr & 0xffffffffff; break;
1037 case 6: ptr = ptr & 0xffffffffffff; break;
1038 case 7: ptr = ptr & 0xffffffffffffff; break;
1039 default: break;
1040 }
1041 stack.back().GetScalar() = ptr;
1042 stack.back().ClearContext();
1043 }
1044 break;
1045 case Value::eValueTypeLoadAddress:
1046 if (exe_ctx)
1047 {
1048 if (exe_ctx->process)
1049 {
1050 lldb::addr_t pointer_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1051 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1052 Error error;
1053 if (exe_ctx->process->ReadMemory(pointer_addr, &addr_bytes, size, error) == size)
1054 {
1055 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes), exe_ctx->process->GetByteOrder(), size);
1056 uint32_t addr_data_offset = 0;
1057 switch (size)
1058 {
1059 case 1: stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset); break;
1060 case 2: stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset); break;
1061 case 4: stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset); break;
1062 case 8: stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset); break;
1063 default: stack.back().GetScalar() = addr_data.GetPointer(&addr_data_offset);
1064 }
1065 stack.back().ClearContext();
1066 }
1067 else
1068 {
1069 if (error_ptr)
1070 error_ptr->SetErrorStringWithFormat ("Failed to dereference pointer from 0x%llx for DW_OP_deref: %s\n",
1071 pointer_addr,
1072 error.AsCString());
1073 return false;
1074 }
1075 }
1076 else
1077 {
1078 if (error_ptr)
1079 error_ptr->SetErrorStringWithFormat ("NULL process for DW_OP_deref.\n");
1080 return false;
1081 }
1082 }
1083 else
1084 {
1085 if (error_ptr)
1086 error_ptr->SetErrorStringWithFormat ("NULL execution context for DW_OP_deref.\n");
1087 return false;
1088 }
1089 break;
1090
1091 default:
1092 break;
1093 }
1094
1095 }
1096 break;
Chris Lattner24943d22010-06-08 16:52:24 +00001097
1098 //----------------------------------------------------------------------
1099 // OPCODE: DW_OP_xderef_size
1100 // OPERANDS: 1
1101 // 1 - uint8_t that specifies the size of the data to dereference.
1102 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1103 // the top of the stack is treated as an address. The second stack
Greg Clayton33ed1702010-08-24 00:45:41 +00001104 // entry is treated as an "address space identifier" for those
Chris Lattner24943d22010-06-08 16:52:24 +00001105 // architectures that support multiple address spaces. The top two
1106 // stack elements are popped, a data item is retrieved through an
1107 // implementation-defined address calculation and pushed as the new
1108 // stack top. In the DW_OP_xderef_size operation, however, the size in
1109 // bytes of the data retrieved from the dereferenced address is
1110 // specified by the single operand. This operand is a 1-byte unsigned
1111 // integral constant whose value may not be larger than the size of an
1112 // address on the target machine. The data retrieved is zero extended
1113 // to the size of an address on the target machine before being pushed
1114 // on the expression stack.
1115 //----------------------------------------------------------------------
1116 case DW_OP_xderef_size:
1117 if (error_ptr)
1118 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1119 return false;
1120 //----------------------------------------------------------------------
1121 // OPCODE: DW_OP_xderef
1122 // OPERANDS: none
1123 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1124 // the top of the stack is treated as an address. The second stack entry
1125 // is treated as an "address space identifier" for those architectures
1126 // that support multiple address spaces. The top two stack elements are
1127 // popped, a data item is retrieved through an implementation-defined
1128 // address calculation and pushed as the new stack top. The size of the
1129 // data retrieved from the dereferenced address is the size of an address
1130 // on the target machine.
1131 //----------------------------------------------------------------------
1132 case DW_OP_xderef:
1133 if (error_ptr)
1134 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1135 return false;
1136
1137 //----------------------------------------------------------------------
1138 // All DW_OP_constXXX opcodes have a single operand as noted below:
1139 //
1140 // Opcode Operand 1
1141 // --------------- ----------------------------------------------------
1142 // DW_OP_const1u 1-byte unsigned integer constant
1143 // DW_OP_const1s 1-byte signed integer constant
1144 // DW_OP_const2u 2-byte unsigned integer constant
1145 // DW_OP_const2s 2-byte signed integer constant
1146 // DW_OP_const4u 4-byte unsigned integer constant
1147 // DW_OP_const4s 4-byte signed integer constant
1148 // DW_OP_const8u 8-byte unsigned integer constant
1149 // DW_OP_const8s 8-byte signed integer constant
1150 // DW_OP_constu unsigned LEB128 integer constant
1151 // DW_OP_consts signed LEB128 integer constant
1152 //----------------------------------------------------------------------
1153 case DW_OP_const1u : stack.push_back(( uint8_t)opcodes.GetU8(&offset)); break;
1154 case DW_OP_const1s : stack.push_back(( int8_t)opcodes.GetU8(&offset)); break;
1155 case DW_OP_const2u : stack.push_back((uint16_t)opcodes.GetU16(&offset)); break;
1156 case DW_OP_const2s : stack.push_back(( int16_t)opcodes.GetU16(&offset)); break;
1157 case DW_OP_const4u : stack.push_back((uint32_t)opcodes.GetU32(&offset)); break;
1158 case DW_OP_const4s : stack.push_back(( int32_t)opcodes.GetU32(&offset)); break;
1159 case DW_OP_const8u : stack.push_back((uint64_t)opcodes.GetU64(&offset)); break;
1160 case DW_OP_const8s : stack.push_back(( int64_t)opcodes.GetU64(&offset)); break;
1161 case DW_OP_constu : stack.push_back(opcodes.GetULEB128(&offset)); break;
1162 case DW_OP_consts : stack.push_back(opcodes.GetSLEB128(&offset)); break;
1163
1164 //----------------------------------------------------------------------
1165 // OPCODE: DW_OP_dup
1166 // OPERANDS: none
1167 // DESCRIPTION: duplicates the value at the top of the stack
1168 //----------------------------------------------------------------------
1169 case DW_OP_dup:
1170 if (stack.empty())
1171 {
1172 if (error_ptr)
1173 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1174 return false;
1175 }
1176 else
1177 stack.push_back(stack.back());
1178 break;
1179
1180 //----------------------------------------------------------------------
1181 // OPCODE: DW_OP_drop
1182 // OPERANDS: none
1183 // DESCRIPTION: pops the value at the top of the stack
1184 //----------------------------------------------------------------------
1185 case DW_OP_drop:
1186 if (stack.empty())
1187 {
1188 if (error_ptr)
1189 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1190 return false;
1191 }
1192 else
1193 stack.pop_back();
1194 break;
1195
1196 //----------------------------------------------------------------------
1197 // OPCODE: DW_OP_over
1198 // OPERANDS: none
1199 // DESCRIPTION: Duplicates the entry currently second in the stack at
1200 // the top of the stack.
1201 //----------------------------------------------------------------------
1202 case DW_OP_over:
1203 if (stack.size() < 2)
1204 {
1205 if (error_ptr)
1206 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_over.");
1207 return false;
1208 }
1209 else
1210 stack.push_back(stack[stack.size() - 2]);
1211 break;
1212
1213
1214 //----------------------------------------------------------------------
1215 // OPCODE: DW_OP_pick
1216 // OPERANDS: uint8_t index into the current stack
1217 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1218 // inclusive) is pushed on the stack
1219 //----------------------------------------------------------------------
1220 case DW_OP_pick:
1221 {
1222 uint8_t pick_idx = opcodes.GetU8(&offset);
1223 if (pick_idx < stack.size())
1224 stack.push_back(stack[pick_idx]);
1225 else
1226 {
1227 if (error_ptr)
1228 error_ptr->SetErrorStringWithFormat("Index %u out of range for DW_OP_pick.\n", pick_idx);
1229 return false;
1230 }
1231 }
1232 break;
1233
1234 //----------------------------------------------------------------------
1235 // OPCODE: DW_OP_swap
1236 // OPERANDS: none
1237 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1238 // of the stack becomes the second stack entry, and the second entry
1239 // becomes the top of the stack
1240 //----------------------------------------------------------------------
1241 case DW_OP_swap:
1242 if (stack.size() < 2)
1243 {
1244 if (error_ptr)
1245 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_swap.");
1246 return false;
1247 }
1248 else
1249 {
1250 tmp = stack.back();
1251 stack.back() = stack[stack.size() - 2];
1252 stack[stack.size() - 2] = tmp;
1253 }
1254 break;
1255
1256 //----------------------------------------------------------------------
1257 // OPCODE: DW_OP_rot
1258 // OPERANDS: none
1259 // DESCRIPTION: Rotates the first three stack entries. The entry at
1260 // the top of the stack becomes the third stack entry, the second
1261 // entry becomes the top of the stack, and the third entry becomes
1262 // the second entry.
1263 //----------------------------------------------------------------------
1264 case DW_OP_rot:
1265 if (stack.size() < 3)
1266 {
1267 if (error_ptr)
1268 error_ptr->SetErrorString("Expression stack needs at least 3 items for DW_OP_rot.");
1269 return false;
1270 }
1271 else
1272 {
1273 size_t last_idx = stack.size() - 1;
1274 Value old_top = stack[last_idx];
1275 stack[last_idx] = stack[last_idx - 1];
1276 stack[last_idx - 1] = stack[last_idx - 2];
1277 stack[last_idx - 2] = old_top;
1278 }
1279 break;
1280
1281 //----------------------------------------------------------------------
1282 // OPCODE: DW_OP_abs
1283 // OPERANDS: none
1284 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1285 // value and pushes its absolute value. If the absolute value can not be
1286 // represented, the result is undefined.
1287 //----------------------------------------------------------------------
1288 case DW_OP_abs:
1289 if (stack.empty())
1290 {
1291 if (error_ptr)
1292 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_abs.");
1293 return false;
1294 }
1295 else if (stack.back().ResolveValue(exe_ctx, ast_context).AbsoluteValue() == false)
1296 {
1297 if (error_ptr)
1298 error_ptr->SetErrorString("Failed to take the absolute value of the first stack item.");
1299 return false;
1300 }
1301 break;
1302
1303 //----------------------------------------------------------------------
1304 // OPCODE: DW_OP_and
1305 // OPERANDS: none
1306 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1307 // operation on the two, and pushes the result.
1308 //----------------------------------------------------------------------
1309 case DW_OP_and:
1310 if (stack.size() < 2)
1311 {
1312 if (error_ptr)
1313 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_and.");
1314 return false;
1315 }
1316 else
1317 {
1318 tmp = stack.back();
1319 stack.pop_back();
1320 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) & tmp.ResolveValue(exe_ctx, ast_context);
1321 }
1322 break;
1323
1324 //----------------------------------------------------------------------
1325 // OPCODE: DW_OP_div
1326 // OPERANDS: none
1327 // DESCRIPTION: pops the top two stack values, divides the former second
1328 // entry by the former top of the stack using signed division, and
1329 // pushes the result.
1330 //----------------------------------------------------------------------
1331 case DW_OP_div:
1332 if (stack.size() < 2)
1333 {
1334 if (error_ptr)
1335 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_div.");
1336 return false;
1337 }
1338 else
1339 {
1340 tmp = stack.back();
1341 if (tmp.ResolveValue(exe_ctx, ast_context).IsZero())
1342 {
1343 if (error_ptr)
1344 error_ptr->SetErrorString("Divide by zero.");
1345 return false;
1346 }
1347 else
1348 {
1349 stack.pop_back();
1350 stack.back() = stack.back().ResolveValue(exe_ctx, ast_context) / tmp.ResolveValue(exe_ctx, ast_context);
1351 if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1352 {
1353 if (error_ptr)
1354 error_ptr->SetErrorString("Divide failed.");
1355 return false;
1356 }
1357 }
1358 }
1359 break;
1360
1361 //----------------------------------------------------------------------
1362 // OPCODE: DW_OP_minus
1363 // OPERANDS: none
1364 // DESCRIPTION: pops the top two stack values, subtracts the former top
1365 // of the stack from the former second entry, and pushes the result.
1366 //----------------------------------------------------------------------
1367 case DW_OP_minus:
1368 if (stack.size() < 2)
1369 {
1370 if (error_ptr)
1371 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_minus.");
1372 return false;
1373 }
1374 else
1375 {
1376 tmp = stack.back();
1377 stack.pop_back();
1378 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) - tmp.ResolveValue(exe_ctx, ast_context);
1379 }
1380 break;
1381
1382 //----------------------------------------------------------------------
1383 // OPCODE: DW_OP_mod
1384 // OPERANDS: none
1385 // DESCRIPTION: pops the top two stack values and pushes the result of
1386 // the calculation: former second stack entry modulo the former top of
1387 // the stack.
1388 //----------------------------------------------------------------------
1389 case DW_OP_mod:
1390 if (stack.size() < 2)
1391 {
1392 if (error_ptr)
1393 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mod.");
1394 return false;
1395 }
1396 else
1397 {
1398 tmp = stack.back();
1399 stack.pop_back();
1400 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) % tmp.ResolveValue(exe_ctx, ast_context);
1401 }
1402 break;
1403
1404
1405 //----------------------------------------------------------------------
1406 // OPCODE: DW_OP_mul
1407 // OPERANDS: none
1408 // DESCRIPTION: pops the top two stack entries, multiplies them
1409 // together, and pushes the result.
1410 //----------------------------------------------------------------------
1411 case DW_OP_mul:
1412 if (stack.size() < 2)
1413 {
1414 if (error_ptr)
1415 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_mul.");
1416 return false;
1417 }
1418 else
1419 {
1420 tmp = stack.back();
1421 stack.pop_back();
1422 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) * tmp.ResolveValue(exe_ctx, ast_context);
1423 }
1424 break;
1425
1426 //----------------------------------------------------------------------
1427 // OPCODE: DW_OP_neg
1428 // OPERANDS: none
1429 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1430 //----------------------------------------------------------------------
1431 case DW_OP_neg:
1432 if (stack.empty())
1433 {
1434 if (error_ptr)
1435 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_neg.");
1436 return false;
1437 }
1438 else
1439 {
1440 if (stack.back().ResolveValue(exe_ctx, ast_context).UnaryNegate() == false)
1441 {
1442 if (error_ptr)
1443 error_ptr->SetErrorString("Unary negate failed.");
1444 return false;
1445 }
1446 }
1447 break;
1448
1449 //----------------------------------------------------------------------
1450 // OPCODE: DW_OP_not
1451 // OPERANDS: none
1452 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1453 // complement
1454 //----------------------------------------------------------------------
1455 case DW_OP_not:
1456 if (stack.empty())
1457 {
1458 if (error_ptr)
1459 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_not.");
1460 return false;
1461 }
1462 else
1463 {
1464 if (stack.back().ResolveValue(exe_ctx, ast_context).OnesComplement() == false)
1465 {
1466 if (error_ptr)
1467 error_ptr->SetErrorString("Logical NOT failed.");
1468 return false;
1469 }
1470 }
1471 break;
1472
1473 //----------------------------------------------------------------------
1474 // OPCODE: DW_OP_or
1475 // OPERANDS: none
1476 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1477 // operation on the two, and pushes the result.
1478 //----------------------------------------------------------------------
1479 case DW_OP_or:
1480 if (stack.size() < 2)
1481 {
1482 if (error_ptr)
1483 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_or.");
1484 return false;
1485 }
1486 else
1487 {
1488 tmp = stack.back();
1489 stack.pop_back();
1490 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) | tmp.ResolveValue(exe_ctx, ast_context);
1491 }
1492 break;
1493
1494 //----------------------------------------------------------------------
1495 // OPCODE: DW_OP_plus
1496 // OPERANDS: none
1497 // DESCRIPTION: pops the top two stack entries, adds them together, and
1498 // pushes the result.
1499 //----------------------------------------------------------------------
1500 case DW_OP_plus:
1501 if (stack.size() < 2)
1502 {
1503 if (error_ptr)
1504 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_plus.");
1505 return false;
1506 }
1507 else
1508 {
1509 tmp = stack.back();
1510 stack.pop_back();
1511 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) + tmp.ResolveValue(exe_ctx, ast_context);
1512 }
1513 break;
1514
1515 //----------------------------------------------------------------------
1516 // OPCODE: DW_OP_plus_uconst
1517 // OPERANDS: none
1518 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1519 // constant operand and pushes the result.
1520 //----------------------------------------------------------------------
1521 case DW_OP_plus_uconst:
1522 if (stack.empty())
1523 {
1524 if (error_ptr)
1525 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1526 return false;
1527 }
1528 else
1529 {
1530 uint32_t uconst_value = opcodes.GetULEB128(&offset);
1531 // Implicit conversion from a UINT to a Scalar...
1532 stack.back().ResolveValue(exe_ctx, ast_context) += uconst_value;
1533 if (!stack.back().ResolveValue(exe_ctx, ast_context).IsValid())
1534 {
1535 if (error_ptr)
1536 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1537 return false;
1538 }
1539 }
1540 break;
1541
1542 //----------------------------------------------------------------------
1543 // OPCODE: DW_OP_shl
1544 // OPERANDS: none
1545 // DESCRIPTION: pops the top two stack entries, shifts the former
1546 // second entry left by the number of bits specified by the former top
1547 // of the stack, and pushes the result.
1548 //----------------------------------------------------------------------
1549 case DW_OP_shl:
1550 if (stack.size() < 2)
1551 {
1552 if (error_ptr)
1553 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shl.");
1554 return false;
1555 }
1556 else
1557 {
1558 tmp = stack.back();
1559 stack.pop_back();
1560 stack.back().ResolveValue(exe_ctx, ast_context) <<= tmp.ResolveValue(exe_ctx, ast_context);
1561 }
1562 break;
1563
1564 //----------------------------------------------------------------------
1565 // OPCODE: DW_OP_shr
1566 // OPERANDS: none
1567 // DESCRIPTION: pops the top two stack entries, shifts the former second
1568 // entry right logically (filling with zero bits) by the number of bits
1569 // specified by the former top of the stack, and pushes the result.
1570 //----------------------------------------------------------------------
1571 case DW_OP_shr:
1572 if (stack.size() < 2)
1573 {
1574 if (error_ptr)
1575 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shr.");
1576 return false;
1577 }
1578 else
1579 {
1580 tmp = stack.back();
1581 stack.pop_back();
1582 if (stack.back().ResolveValue(exe_ctx, ast_context).ShiftRightLogical(tmp.ResolveValue(exe_ctx, ast_context)) == false)
1583 {
1584 if (error_ptr)
1585 error_ptr->SetErrorString("DW_OP_shr failed.");
1586 return false;
1587 }
1588 }
1589 break;
1590
1591 //----------------------------------------------------------------------
1592 // OPCODE: DW_OP_shra
1593 // OPERANDS: none
1594 // DESCRIPTION: pops the top two stack entries, shifts the former second
1595 // entry right arithmetically (divide the magnitude by 2, keep the same
1596 // sign for the result) by the number of bits specified by the former
1597 // top of the stack, and pushes the result.
1598 //----------------------------------------------------------------------
1599 case DW_OP_shra:
1600 if (stack.size() < 2)
1601 {
1602 if (error_ptr)
1603 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_shra.");
1604 return false;
1605 }
1606 else
1607 {
1608 tmp = stack.back();
1609 stack.pop_back();
1610 stack.back().ResolveValue(exe_ctx, ast_context) >>= tmp.ResolveValue(exe_ctx, ast_context);
1611 }
1612 break;
1613
1614 //----------------------------------------------------------------------
1615 // OPCODE: DW_OP_xor
1616 // OPERANDS: none
1617 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1618 // exclusive-or operation on the two, and pushes the result.
1619 //----------------------------------------------------------------------
1620 case DW_OP_xor:
1621 if (stack.size() < 2)
1622 {
1623 if (error_ptr)
1624 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_xor.");
1625 return false;
1626 }
1627 else
1628 {
1629 tmp = stack.back();
1630 stack.pop_back();
1631 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) ^ tmp.ResolveValue(exe_ctx, ast_context);
1632 }
1633 break;
1634
1635
1636 //----------------------------------------------------------------------
1637 // OPCODE: DW_OP_skip
1638 // OPERANDS: int16_t
1639 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1640 // signed integer constant. The 2-byte constant is the number of bytes
1641 // of the DWARF expression to skip forward or backward from the current
1642 // operation, beginning after the 2-byte constant.
1643 //----------------------------------------------------------------------
1644 case DW_OP_skip:
1645 {
1646 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1647 uint32_t new_offset = offset + skip_offset;
1648 if (new_offset >= opcodes_offset && new_offset < end_offset)
1649 offset = new_offset;
1650 else
1651 {
1652 if (error_ptr)
1653 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
1654 return false;
1655 }
1656 }
1657 break;
1658
1659 //----------------------------------------------------------------------
1660 // OPCODE: DW_OP_bra
1661 // OPERANDS: int16_t
1662 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1663 // signed integer constant. This operation pops the top of stack. If
1664 // the value popped is not the constant 0, the 2-byte constant operand
1665 // is the number of bytes of the DWARF expression to skip forward or
1666 // backward from the current operation, beginning after the 2-byte
1667 // constant.
1668 //----------------------------------------------------------------------
1669 case DW_OP_bra:
1670 {
1671 tmp = stack.back();
1672 stack.pop_back();
1673 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1674 Scalar zero(0);
1675 if (tmp.ResolveValue(exe_ctx, ast_context) != zero)
1676 {
1677 uint32_t new_offset = offset + bra_offset;
1678 if (new_offset >= opcodes_offset && new_offset < end_offset)
1679 offset = new_offset;
1680 else
1681 {
1682 if (error_ptr)
1683 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
1684 return false;
1685 }
1686 }
1687 }
1688 break;
1689
1690 //----------------------------------------------------------------------
1691 // OPCODE: DW_OP_eq
1692 // OPERANDS: none
1693 // DESCRIPTION: pops the top two stack values, compares using the
1694 // equals (==) operator.
1695 // STACK RESULT: push the constant value 1 onto the stack if the result
1696 // of the operation is true or the constant value 0 if the result of the
1697 // operation is false.
1698 //----------------------------------------------------------------------
1699 case DW_OP_eq:
1700 if (stack.size() < 2)
1701 {
1702 if (error_ptr)
1703 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_eq.");
1704 return false;
1705 }
1706 else
1707 {
1708 tmp = stack.back();
1709 stack.pop_back();
1710 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) == tmp.ResolveValue(exe_ctx, ast_context);
1711 }
1712 break;
1713
1714 //----------------------------------------------------------------------
1715 // OPCODE: DW_OP_ge
1716 // OPERANDS: none
1717 // DESCRIPTION: pops the top two stack values, compares using the
1718 // greater than or equal to (>=) operator.
1719 // STACK RESULT: push the constant value 1 onto the stack if the result
1720 // of the operation is true or the constant value 0 if the result of the
1721 // operation is false.
1722 //----------------------------------------------------------------------
1723 case DW_OP_ge:
1724 if (stack.size() < 2)
1725 {
1726 if (error_ptr)
1727 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ge.");
1728 return false;
1729 }
1730 else
1731 {
1732 tmp = stack.back();
1733 stack.pop_back();
1734 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) >= tmp.ResolveValue(exe_ctx, ast_context);
1735 }
1736 break;
1737
1738 //----------------------------------------------------------------------
1739 // OPCODE: DW_OP_gt
1740 // OPERANDS: none
1741 // DESCRIPTION: pops the top two stack values, compares using the
1742 // greater than (>) operator.
1743 // STACK RESULT: push the constant value 1 onto the stack if the result
1744 // of the operation is true or the constant value 0 if the result of the
1745 // operation is false.
1746 //----------------------------------------------------------------------
1747 case DW_OP_gt:
1748 if (stack.size() < 2)
1749 {
1750 if (error_ptr)
1751 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_gt.");
1752 return false;
1753 }
1754 else
1755 {
1756 tmp = stack.back();
1757 stack.pop_back();
1758 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) > tmp.ResolveValue(exe_ctx, ast_context);
1759 }
1760 break;
1761
1762 //----------------------------------------------------------------------
1763 // OPCODE: DW_OP_le
1764 // OPERANDS: none
1765 // DESCRIPTION: pops the top two stack values, compares using the
1766 // less than or equal to (<=) operator.
1767 // STACK RESULT: push the constant value 1 onto the stack if the result
1768 // of the operation is true or the constant value 0 if the result of the
1769 // operation is false.
1770 //----------------------------------------------------------------------
1771 case DW_OP_le:
1772 if (stack.size() < 2)
1773 {
1774 if (error_ptr)
1775 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_le.");
1776 return false;
1777 }
1778 else
1779 {
1780 tmp = stack.back();
1781 stack.pop_back();
1782 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) <= tmp.ResolveValue(exe_ctx, ast_context);
1783 }
1784 break;
1785
1786 //----------------------------------------------------------------------
1787 // OPCODE: DW_OP_lt
1788 // OPERANDS: none
1789 // DESCRIPTION: pops the top two stack values, compares using the
1790 // less than (<) operator.
1791 // STACK RESULT: push the constant value 1 onto the stack if the result
1792 // of the operation is true or the constant value 0 if the result of the
1793 // operation is false.
1794 //----------------------------------------------------------------------
1795 case DW_OP_lt:
1796 if (stack.size() < 2)
1797 {
1798 if (error_ptr)
1799 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_lt.");
1800 return false;
1801 }
1802 else
1803 {
1804 tmp = stack.back();
1805 stack.pop_back();
1806 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) < tmp.ResolveValue(exe_ctx, ast_context);
1807 }
1808 break;
1809
1810 //----------------------------------------------------------------------
1811 // OPCODE: DW_OP_ne
1812 // OPERANDS: none
1813 // DESCRIPTION: pops the top two stack values, compares using the
1814 // not equal (!=) operator.
1815 // STACK RESULT: push the constant value 1 onto the stack if the result
1816 // of the operation is true or the constant value 0 if the result of the
1817 // operation is false.
1818 //----------------------------------------------------------------------
1819 case DW_OP_ne:
1820 if (stack.size() < 2)
1821 {
1822 if (error_ptr)
1823 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_ne.");
1824 return false;
1825 }
1826 else
1827 {
1828 tmp = stack.back();
1829 stack.pop_back();
1830 stack.back().ResolveValue(exe_ctx, ast_context) = stack.back().ResolveValue(exe_ctx, ast_context) != tmp.ResolveValue(exe_ctx, ast_context);
1831 }
1832 break;
1833
1834 //----------------------------------------------------------------------
1835 // OPCODE: DW_OP_litn
1836 // OPERANDS: none
1837 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1838 // STACK RESULT: push the unsigned literal constant value onto the top
1839 // of the stack.
1840 //----------------------------------------------------------------------
1841 case DW_OP_lit0:
1842 case DW_OP_lit1:
1843 case DW_OP_lit2:
1844 case DW_OP_lit3:
1845 case DW_OP_lit4:
1846 case DW_OP_lit5:
1847 case DW_OP_lit6:
1848 case DW_OP_lit7:
1849 case DW_OP_lit8:
1850 case DW_OP_lit9:
1851 case DW_OP_lit10:
1852 case DW_OP_lit11:
1853 case DW_OP_lit12:
1854 case DW_OP_lit13:
1855 case DW_OP_lit14:
1856 case DW_OP_lit15:
1857 case DW_OP_lit16:
1858 case DW_OP_lit17:
1859 case DW_OP_lit18:
1860 case DW_OP_lit19:
1861 case DW_OP_lit20:
1862 case DW_OP_lit21:
1863 case DW_OP_lit22:
1864 case DW_OP_lit23:
1865 case DW_OP_lit24:
1866 case DW_OP_lit25:
1867 case DW_OP_lit26:
1868 case DW_OP_lit27:
1869 case DW_OP_lit28:
1870 case DW_OP_lit29:
1871 case DW_OP_lit30:
1872 case DW_OP_lit31:
1873 stack.push_back(op - DW_OP_lit0);
1874 break;
1875
1876 //----------------------------------------------------------------------
1877 // OPCODE: DW_OP_regN
1878 // OPERANDS: none
1879 // DESCRIPTION: Push the value in register n on the top of the stack.
1880 //----------------------------------------------------------------------
1881 case DW_OP_reg0:
1882 case DW_OP_reg1:
1883 case DW_OP_reg2:
1884 case DW_OP_reg3:
1885 case DW_OP_reg4:
1886 case DW_OP_reg5:
1887 case DW_OP_reg6:
1888 case DW_OP_reg7:
1889 case DW_OP_reg8:
1890 case DW_OP_reg9:
1891 case DW_OP_reg10:
1892 case DW_OP_reg11:
1893 case DW_OP_reg12:
1894 case DW_OP_reg13:
1895 case DW_OP_reg14:
1896 case DW_OP_reg15:
1897 case DW_OP_reg16:
1898 case DW_OP_reg17:
1899 case DW_OP_reg18:
1900 case DW_OP_reg19:
1901 case DW_OP_reg20:
1902 case DW_OP_reg21:
1903 case DW_OP_reg22:
1904 case DW_OP_reg23:
1905 case DW_OP_reg24:
1906 case DW_OP_reg25:
1907 case DW_OP_reg26:
1908 case DW_OP_reg27:
1909 case DW_OP_reg28:
1910 case DW_OP_reg29:
1911 case DW_OP_reg30:
1912 case DW_OP_reg31:
1913 {
1914 reg_num = op - DW_OP_reg0;
1915
Jason Molenda8e69de42010-11-20 01:28:30 +00001916 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00001917 stack.push_back(tmp);
1918 else
1919 return false;
1920 }
1921 break;
1922 //----------------------------------------------------------------------
1923 // OPCODE: DW_OP_regx
1924 // OPERANDS:
1925 // ULEB128 literal operand that encodes the register.
1926 // DESCRIPTION: Push the value in register on the top of the stack.
1927 //----------------------------------------------------------------------
1928 case DW_OP_regx:
1929 {
1930 reg_num = opcodes.GetULEB128(&offset);
Jason Molenda8e69de42010-11-20 01:28:30 +00001931 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00001932 stack.push_back(tmp);
1933 else
1934 return false;
1935 }
1936 break;
1937
1938 //----------------------------------------------------------------------
1939 // OPCODE: DW_OP_bregN
1940 // OPERANDS:
1941 // SLEB128 offset from register N
1942 // DESCRIPTION: Value is in memory at the address specified by register
1943 // N plus an offset.
1944 //----------------------------------------------------------------------
1945 case DW_OP_breg0:
1946 case DW_OP_breg1:
1947 case DW_OP_breg2:
1948 case DW_OP_breg3:
1949 case DW_OP_breg4:
1950 case DW_OP_breg5:
1951 case DW_OP_breg6:
1952 case DW_OP_breg7:
1953 case DW_OP_breg8:
1954 case DW_OP_breg9:
1955 case DW_OP_breg10:
1956 case DW_OP_breg11:
1957 case DW_OP_breg12:
1958 case DW_OP_breg13:
1959 case DW_OP_breg14:
1960 case DW_OP_breg15:
1961 case DW_OP_breg16:
1962 case DW_OP_breg17:
1963 case DW_OP_breg18:
1964 case DW_OP_breg19:
1965 case DW_OP_breg20:
1966 case DW_OP_breg21:
1967 case DW_OP_breg22:
1968 case DW_OP_breg23:
1969 case DW_OP_breg24:
1970 case DW_OP_breg25:
1971 case DW_OP_breg26:
1972 case DW_OP_breg27:
1973 case DW_OP_breg28:
1974 case DW_OP_breg29:
1975 case DW_OP_breg30:
1976 case DW_OP_breg31:
1977 {
1978 reg_num = op - DW_OP_breg0;
1979
Jason Molenda8e69de42010-11-20 01:28:30 +00001980 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00001981 {
1982 int64_t breg_offset = opcodes.GetSLEB128(&offset);
1983 tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
1984 stack.push_back(tmp);
1985 stack.back().SetValueType (Value::eValueTypeLoadAddress);
1986 }
1987 else
1988 return false;
1989 }
1990 break;
1991 //----------------------------------------------------------------------
1992 // OPCODE: DW_OP_bregx
1993 // OPERANDS: 2
1994 // ULEB128 literal operand that encodes the register.
1995 // SLEB128 offset from register N
1996 // DESCRIPTION: Value is in memory at the address specified by register
1997 // N plus an offset.
1998 //----------------------------------------------------------------------
1999 case DW_OP_bregx:
2000 {
2001 reg_num = opcodes.GetULEB128(&offset);
2002
Jason Molenda8e69de42010-11-20 01:28:30 +00002003 if (ReadRegisterValueAsScalar (reg_ctx, reg_kind, reg_num, error_ptr, tmp))
Chris Lattner24943d22010-06-08 16:52:24 +00002004 {
2005 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2006 tmp.ResolveValue(exe_ctx, ast_context) += (uint64_t)breg_offset;
2007 stack.push_back(tmp);
2008 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2009 }
2010 else
2011 return false;
2012 }
2013 break;
2014
2015 case DW_OP_fbreg:
2016 if (exe_ctx && exe_ctx->frame)
2017 {
2018 Scalar value;
2019 if (exe_ctx->frame->GetFrameBaseValue(value, error_ptr))
2020 {
2021 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2022 value += fbreg_offset;
2023 stack.push_back(value);
2024 stack.back().SetValueType (Value::eValueTypeLoadAddress);
2025 }
2026 else
2027 return false;
2028 }
2029 else
2030 {
2031 if (error_ptr)
2032 error_ptr->SetErrorString ("Invalid stack frame in context for DW_OP_fbreg opcode.");
2033 return false;
2034 }
2035 break;
2036
2037 //----------------------------------------------------------------------
2038 // OPCODE: DW_OP_nop
2039 // OPERANDS: none
2040 // DESCRIPTION: A place holder. It has no effect on the location stack
2041 // or any of its values.
2042 //----------------------------------------------------------------------
2043 case DW_OP_nop:
2044 break;
2045
2046 //----------------------------------------------------------------------
2047 // OPCODE: DW_OP_piece
2048 // OPERANDS: 1
2049 // ULEB128: byte size of the piece
2050 // DESCRIPTION: The operand describes the size in bytes of the piece of
2051 // the object referenced by the DWARF expression whose result is at the
2052 // top of the stack. If the piece is located in a register, but does not
2053 // occupy the entire register, the placement of the piece within that
2054 // register is defined by the ABI.
2055 //
2056 // Many compilers store a single variable in sets of registers, or store
2057 // a variable partially in memory and partially in registers.
2058 // DW_OP_piece provides a way of describing how large a part of a
2059 // variable a particular DWARF expression refers to.
2060 //----------------------------------------------------------------------
2061 case DW_OP_piece:
2062 if (error_ptr)
2063 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_piece.");
2064 return false;
2065
2066 //----------------------------------------------------------------------
2067 // OPCODE: DW_OP_push_object_address
2068 // OPERANDS: none
2069 // DESCRIPTION: Pushes the address of the object currently being
2070 // evaluated as part of evaluation of a user presented expression.
2071 // This object may correspond to an independent variable described by
2072 // its own DIE or it may be a component of an array, structure, or class
2073 // whose address has been dynamically determined by an earlier step
2074 // during user expression evaluation.
2075 //----------------------------------------------------------------------
2076 case DW_OP_push_object_address:
2077 if (error_ptr)
2078 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_push_object_address.");
2079 return false;
2080
2081 //----------------------------------------------------------------------
2082 // OPCODE: DW_OP_call2
2083 // OPERANDS:
2084 // uint16_t compile unit relative offset of a DIE
2085 // DESCRIPTION: Performs subroutine calls during evaluation
2086 // of a DWARF expression. The operand is the 2-byte unsigned offset
2087 // of a debugging information entry in the current compilation unit.
2088 //
2089 // Operand interpretation is exactly like that for DW_FORM_ref2.
2090 //
2091 // This operation transfers control of DWARF expression evaluation
2092 // to the DW_AT_location attribute of the referenced DIE. If there is
2093 // no such attribute, then there is no effect. Execution of the DWARF
2094 // expression of a DW_AT_location attribute may add to and/or remove from
2095 // values on the stack. Execution returns to the point following the call
2096 // when the end of the attribute is reached. Values on the stack at the
2097 // time of the call may be used as parameters by the called expression
2098 // and values left on the stack by the called expression may be used as
2099 // return values by prior agreement between the calling and called
2100 // expressions.
2101 //----------------------------------------------------------------------
2102 case DW_OP_call2:
2103 if (error_ptr)
2104 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call2.");
2105 return false;
2106 //----------------------------------------------------------------------
2107 // OPCODE: DW_OP_call4
2108 // OPERANDS: 1
2109 // uint32_t compile unit relative offset of a DIE
2110 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2111 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset
2112 // of a debugging information entry in the current compilation unit.
2113 //
2114 // Operand interpretation DW_OP_call4 is exactly like that for
2115 // DW_FORM_ref4.
2116 //
2117 // This operation transfers control of DWARF expression evaluation
2118 // to the DW_AT_location attribute of the referenced DIE. If there is
2119 // no such attribute, then there is no effect. Execution of the DWARF
2120 // expression of a DW_AT_location attribute may add to and/or remove from
2121 // values on the stack. Execution returns to the point following the call
2122 // when the end of the attribute is reached. Values on the stack at the
2123 // time of the call may be used as parameters by the called expression
2124 // and values left on the stack by the called expression may be used as
2125 // return values by prior agreement between the calling and called
2126 // expressions.
2127 //----------------------------------------------------------------------
2128 case DW_OP_call4:
2129 if (error_ptr)
2130 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call4.");
2131 return false;
2132
2133
2134 //----------------------------------------------------------------------
2135 // OPCODE: DW_OP_call_ref
2136 // OPERANDS:
2137 // uint32_t absolute DIE offset for 32-bit DWARF or a uint64_t
2138 // absolute DIE offset for 64 bit DWARF.
2139 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2140 // expression. Takes a single operand. In the 32-bit DWARF format, the
2141 // operand is a 4-byte unsigned value; in the 64-bit DWARF format, it
2142 // is an 8-byte unsigned value. The operand is used as the offset of a
2143 // debugging information entry in a .debug_info section which may be
2144 // contained in a shared object for executable other than that
2145 // containing the operator. For references from one shared object or
2146 // executable to another, the relocation must be performed by the
2147 // consumer.
2148 //
2149 // Operand interpretation of DW_OP_call_ref is exactly like that for
2150 // DW_FORM_ref_addr.
2151 //
2152 // This operation transfers control of DWARF expression evaluation
2153 // to the DW_AT_location attribute of the referenced DIE. If there is
2154 // no such attribute, then there is no effect. Execution of the DWARF
2155 // expression of a DW_AT_location attribute may add to and/or remove from
2156 // values on the stack. Execution returns to the point following the call
2157 // when the end of the attribute is reached. Values on the stack at the
2158 // time of the call may be used as parameters by the called expression
2159 // and values left on the stack by the called expression may be used as
2160 // return values by prior agreement between the calling and called
2161 // expressions.
2162 //----------------------------------------------------------------------
2163 case DW_OP_call_ref:
2164 if (error_ptr)
2165 error_ptr->SetErrorString ("Unimplemented opcode DW_OP_call_ref.");
2166 return false;
2167
2168 //----------------------------------------------------------------------
2169 // OPCODE: DW_OP_APPLE_array_ref
2170 // OPERANDS: none
2171 // DESCRIPTION: Pops a value off the stack and uses it as the array
2172 // index. Pops a second value off the stack and uses it as the array
2173 // itself. Pushes a value onto the stack representing the element of
2174 // the array specified by the index.
2175 //----------------------------------------------------------------------
2176 case DW_OP_APPLE_array_ref:
2177 {
2178 if (stack.size() < 2)
2179 {
2180 if (error_ptr)
2181 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_array_ref.");
2182 return false;
2183 }
2184
2185 Value index_val = stack.back();
2186 stack.pop_back();
2187 Value array_val = stack.back();
2188 stack.pop_back();
2189
2190 Scalar &index_scalar = index_val.ResolveValue(exe_ctx, ast_context);
2191 int64_t index = index_scalar.SLongLong(LONG_LONG_MAX);
2192
2193 if (index == LONG_LONG_MAX)
2194 {
2195 if (error_ptr)
2196 error_ptr->SetErrorString("Invalid array index.");
2197 return false;
2198 }
2199
Greg Clayton6916e352010-11-13 03:52:47 +00002200 if (array_val.GetContextType() != Value::eContextTypeClangType)
Chris Lattner24943d22010-06-08 16:52:24 +00002201 {
2202 if (error_ptr)
2203 error_ptr->SetErrorString("Arrays without Clang types are unhandled at this time.");
2204 return false;
2205 }
2206
2207 if (array_val.GetValueType() != Value::eValueTypeLoadAddress &&
2208 array_val.GetValueType() != Value::eValueTypeHostAddress)
2209 {
2210 if (error_ptr)
2211 error_ptr->SetErrorString("Array must be stored in memory.");
2212 return false;
2213 }
2214
Greg Clayton462d4142010-09-29 01:12:09 +00002215 void *array_type = array_val.GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +00002216
2217 void *member_type;
2218 uint64_t size = 0;
2219
2220 if ((!ClangASTContext::IsPointerType(array_type, &member_type)) &&
2221 (!ClangASTContext::IsArrayType(array_type, &member_type, &size)))
2222 {
2223 if (error_ptr)
2224 error_ptr->SetErrorString("Array reference from something that is neither a pointer nor an array.");
2225 return false;
2226 }
2227
2228 if (size && (index >= size || index < 0))
2229 {
2230 if (error_ptr)
2231 error_ptr->SetErrorStringWithFormat("Out of bounds array access. %lld is not in [0, %llu]", index, size);
2232 return false;
2233 }
2234
Greg Clayton960d6a42010-08-03 00:35:52 +00002235 uint64_t member_bit_size = ClangASTType::GetClangTypeBitWidth(ast_context, member_type);
2236 uint64_t member_bit_align = ClangASTType::GetTypeBitAlign(ast_context, member_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002237 uint64_t member_bit_incr = ((member_bit_size + member_bit_align - 1) / member_bit_align) * member_bit_align;
2238 if (member_bit_incr % 8)
2239 {
2240 if (error_ptr)
2241 error_ptr->SetErrorStringWithFormat("Array increment is not byte aligned", index, size);
2242 return false;
2243 }
2244 int64_t member_offset = (int64_t)(member_bit_incr / 8) * index;
2245
2246 Value member;
2247
Greg Clayton6916e352010-11-13 03:52:47 +00002248 member.SetContext(Value::eContextTypeClangType, member_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002249 member.SetValueType(array_val.GetValueType());
2250
2251 addr_t array_base = (addr_t)array_val.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2252 addr_t member_loc = array_base + member_offset;
2253 member.GetScalar() = (uint64_t)member_loc;
2254
2255 stack.push_back(member);
2256 }
2257 break;
2258
2259 //----------------------------------------------------------------------
2260 // OPCODE: DW_OP_APPLE_uninit
2261 // OPERANDS: none
2262 // DESCRIPTION: Lets us know that the value is currently not initialized
2263 //----------------------------------------------------------------------
2264 case DW_OP_APPLE_uninit:
2265 //return eResultTypeErrorUninitialized;
2266 break; // Ignore this as we have seen cases where this value is incorrectly added
2267
2268 //----------------------------------------------------------------------
2269 // OPCODE: DW_OP_APPLE_assign
2270 // OPERANDS: none
2271 // DESCRIPTION: Pops a value off of the stack and assigns it to the next
2272 // item on the stack which must be something assignable (inferior
2273 // Variable, inferior Type with address, inferior register, or
2274 // expression local variable.
2275 //----------------------------------------------------------------------
2276 case DW_OP_APPLE_assign:
2277 if (stack.size() < 2)
2278 {
2279 if (error_ptr)
2280 error_ptr->SetErrorString("Expression stack needs at least 2 items for DW_OP_APPLE_assign.");
2281 return false;
2282 }
2283 else
2284 {
2285 tmp = stack.back();
2286 stack.pop_back();
2287 Value::ContextType context_type = stack.back().GetContextType();
2288 StreamString new_value(Stream::eBinary, 4, eByteOrderHost);
2289 switch (context_type)
2290 {
Greg Clayton6916e352010-11-13 03:52:47 +00002291 case Value::eContextTypeClangType:
Chris Lattner24943d22010-06-08 16:52:24 +00002292 {
Greg Clayton462d4142010-09-29 01:12:09 +00002293 void *clang_type = stack.back().GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +00002294
2295 if (ClangASTContext::IsAggregateType (clang_type))
2296 {
2297 Value::ValueType source_value_type = tmp.GetValueType();
2298 Value::ValueType target_value_type = stack.back().GetValueType();
2299
2300 addr_t source_addr = (addr_t)tmp.GetScalar().ULongLong();
2301 addr_t target_addr = (addr_t)stack.back().GetScalar().ULongLong();
2302
Greg Clayton960d6a42010-08-03 00:35:52 +00002303 size_t byte_size = (ClangASTType::GetClangTypeBitWidth(ast_context, clang_type) + 7) / 8;
Chris Lattner24943d22010-06-08 16:52:24 +00002304
2305 switch (source_value_type)
2306 {
2307 case Value::eValueTypeLoadAddress:
2308 switch (target_value_type)
2309 {
2310 case Value::eValueTypeLoadAddress:
2311 {
2312 DataBufferHeap data;
2313 data.SetByteSize(byte_size);
2314
2315 Error error;
2316 if (exe_ctx->process->ReadMemory (source_addr, data.GetBytes(), byte_size, error) != byte_size)
2317 {
2318 if (error_ptr)
2319 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2320 return false;
2321 }
2322
2323 if (exe_ctx->process->WriteMemory (target_addr, data.GetBytes(), byte_size, error) != byte_size)
2324 {
2325 if (error_ptr)
2326 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2327 return false;
2328 }
2329 }
2330 break;
2331 case Value::eValueTypeHostAddress:
2332 if (exe_ctx->process->GetByteOrder() != Host::GetByteOrder())
2333 {
2334 if (error_ptr)
2335 error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2336 return false;
2337 }
2338 else
2339 {
2340 Error error;
2341 if (exe_ctx->process->ReadMemory (source_addr, (uint8_t*)target_addr, byte_size, error) != byte_size)
2342 {
2343 if (error_ptr)
2344 error_ptr->SetErrorStringWithFormat ("Couldn't read a composite type from the target: %s", error.AsCString());
2345 return false;
2346 }
2347 }
2348 break;
2349 default:
2350 return false;
2351 }
2352 break;
2353 case Value::eValueTypeHostAddress:
2354 switch (target_value_type)
2355 {
2356 case Value::eValueTypeLoadAddress:
2357 if (exe_ctx->process->GetByteOrder() != Host::GetByteOrder())
2358 {
2359 if (error_ptr)
2360 error_ptr->SetErrorStringWithFormat ("Copy of composite types between incompatible byte orders is unimplemented");
2361 return false;
2362 }
2363 else
2364 {
2365 Error error;
2366 if (exe_ctx->process->WriteMemory (target_addr, (uint8_t*)source_addr, byte_size, error) != byte_size)
2367 {
2368 if (error_ptr)
2369 error_ptr->SetErrorStringWithFormat ("Couldn't write a composite type to the target: %s", error.AsCString());
2370 return false;
2371 }
2372 }
2373 case Value::eValueTypeHostAddress:
2374 memcpy ((uint8_t*)target_addr, (uint8_t*)source_addr, byte_size);
2375 break;
2376 default:
2377 return false;
2378 }
2379 }
2380 }
2381 else
2382 {
Greg Clayton1674b122010-07-21 22:12:05 +00002383 if (!ClangASTType::SetValueFromScalar (ast_context,
2384 clang_type,
2385 tmp.ResolveValue(exe_ctx, ast_context),
2386 new_value))
Chris Lattner24943d22010-06-08 16:52:24 +00002387 {
2388 if (error_ptr)
2389 error_ptr->SetErrorStringWithFormat ("Couldn't extract a value from an integral type.\n");
2390 return false;
2391 }
2392
2393 Value::ValueType value_type = stack.back().GetValueType();
2394
2395 switch (value_type)
2396 {
2397 case Value::eValueTypeLoadAddress:
2398 case Value::eValueTypeHostAddress:
2399 {
2400 lldb::AddressType address_type = (value_type == Value::eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost);
2401 lldb::addr_t addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton1674b122010-07-21 22:12:05 +00002402 if (!ClangASTType::WriteToMemory (ast_context,
2403 clang_type,
2404 exe_ctx,
2405 addr,
2406 address_type,
2407 new_value))
Chris Lattner24943d22010-06-08 16:52:24 +00002408 {
2409 if (error_ptr)
2410 error_ptr->SetErrorStringWithFormat ("Failed to write value to memory at 0x%llx.\n", addr);
2411 return false;
2412 }
2413 }
2414 break;
2415
2416 default:
2417 break;
2418 }
2419 }
2420 }
2421 break;
2422
2423 default:
2424 if (error_ptr)
2425 error_ptr->SetErrorString ("Assign failed.");
2426 return false;
2427 }
2428 }
2429 break;
2430
2431 //----------------------------------------------------------------------
2432 // OPCODE: DW_OP_APPLE_address_of
2433 // OPERANDS: none
2434 // DESCRIPTION: Pops a value off of the stack and pushed its address.
2435 // The top item on the stack must be a variable, or already be a memory
2436 // location.
2437 //----------------------------------------------------------------------
2438 case DW_OP_APPLE_address_of:
2439 if (stack.empty())
2440 {
2441 if (error_ptr)
2442 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_address_of.");
2443 return false;
2444 }
2445 else
2446 {
2447 Value::ValueType value_type = stack.back().GetValueType();
2448 switch (value_type)
2449 {
2450 default:
2451 case Value::eValueTypeScalar: // raw scalar value
2452 if (error_ptr)
2453 error_ptr->SetErrorString("Top stack item isn't a memory based object.");
2454 return false;
2455
2456 case Value::eValueTypeLoadAddress: // load address value
2457 case Value::eValueTypeFileAddress: // file address value
2458 case Value::eValueTypeHostAddress: // host address value (for memory in the process that is using liblldb)
2459 // Taking the address of an object reduces it to the address
2460 // of the value and removes any extra context it had.
2461 //stack.back().SetValueType(Value::eValueTypeScalar);
2462 stack.back().ClearContext();
2463 break;
2464 }
2465 }
2466 break;
2467
2468 //----------------------------------------------------------------------
2469 // OPCODE: DW_OP_APPLE_value_of
2470 // OPERANDS: none
2471 // DESCRIPTION: Pops a value off of the stack and pushed its value.
2472 // The top item on the stack must be a variable, expression variable.
2473 //----------------------------------------------------------------------
2474 case DW_OP_APPLE_value_of:
2475 if (stack.empty())
2476 {
2477 if (error_ptr)
2478 error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_value_of.");
2479 return false;
2480 }
2481 else if (!stack.back().ValueOf(exe_ctx, ast_context))
2482 {
2483 if (error_ptr)
2484 error_ptr->SetErrorString ("Top stack item isn't a valid candidate for DW_OP_APPLE_value_of.");
2485 return false;
2486 }
2487 break;
2488
2489 //----------------------------------------------------------------------
2490 // OPCODE: DW_OP_APPLE_deref_type
2491 // OPERANDS: none
2492 // DESCRIPTION: gets the value pointed to by the top stack item
2493 //----------------------------------------------------------------------
2494 case DW_OP_APPLE_deref_type:
2495 {
2496 if (stack.empty())
2497 {
2498 if (error_ptr)
2499 error_ptr->SetErrorString("Expression stack needs at least 1 items for DW_OP_APPLE_deref_type.");
2500 return false;
2501 }
2502
2503 tmp = stack.back();
2504 stack.pop_back();
2505
Greg Clayton6916e352010-11-13 03:52:47 +00002506 if (tmp.GetContextType() != Value::eContextTypeClangType)
Chris Lattner24943d22010-06-08 16:52:24 +00002507 {
2508 if (error_ptr)
2509 error_ptr->SetErrorString("Item at top of expression stack must have a Clang type");
2510 return false;
2511 }
2512
Greg Clayton462d4142010-09-29 01:12:09 +00002513 void *ptr_type = tmp.GetClangType();
Chris Lattner24943d22010-06-08 16:52:24 +00002514 void *target_type;
2515
2516 if (!ClangASTContext::IsPointerType(ptr_type, &target_type))
2517 {
2518 if (error_ptr)
2519 error_ptr->SetErrorString("Dereferencing a non-pointer type");
2520 return false;
2521 }
2522
2523 // TODO do we want all pointers to be dereferenced as load addresses?
2524 Value::ValueType value_type = tmp.GetValueType();
2525
2526 tmp.ResolveValue(exe_ctx, ast_context);
2527
2528 tmp.SetValueType(value_type);
Greg Clayton6916e352010-11-13 03:52:47 +00002529 tmp.SetContext(Value::eContextTypeClangType, target_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002530
2531 stack.push_back(tmp);
2532 }
2533 break;
2534
2535 //----------------------------------------------------------------------
2536 // OPCODE: DW_OP_APPLE_expr_local
2537 // OPERANDS: ULEB128
2538 // DESCRIPTION: pushes the expression local variable index onto the
2539 // stack and set the appropriate context so we know the stack item is
2540 // an expression local variable index.
2541 //----------------------------------------------------------------------
2542 case DW_OP_APPLE_expr_local:
2543 {
Sean Callanana6223432010-08-20 01:02:30 +00002544 /*
Chris Lattner24943d22010-06-08 16:52:24 +00002545 uint32_t idx = opcodes.GetULEB128(&offset);
2546 if (expr_locals == NULL)
2547 {
2548 if (error_ptr)
2549 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) opcode encountered with no local variable list.\n", idx);
2550 return false;
2551 }
2552 Value *expr_local_variable = expr_locals->GetVariableAtIndex(idx);
2553 if (expr_local_variable == NULL)
2554 {
2555 if (error_ptr)
2556 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_expr_local(%u) with invalid index %u.\n", idx, idx);
2557 return false;
2558 }
2559 Value *proxy = expr_local_variable->CreateProxy();
2560 stack.push_back(*proxy);
2561 delete proxy;
Greg Clayton6916e352010-11-13 03:52:47 +00002562 //stack.back().SetContext (Value::eContextTypeClangType, expr_local_variable->GetClangType());
Sean Callanana6223432010-08-20 01:02:30 +00002563 */
Chris Lattner24943d22010-06-08 16:52:24 +00002564 }
2565 break;
2566
2567 //----------------------------------------------------------------------
2568 // OPCODE: DW_OP_APPLE_extern
2569 // OPERANDS: ULEB128
2570 // DESCRIPTION: pushes a proxy for the extern object index onto the
2571 // stack.
2572 //----------------------------------------------------------------------
2573 case DW_OP_APPLE_extern:
2574 {
Sean Callanan8c127202010-08-23 23:09:38 +00002575 /*
Chris Lattner24943d22010-06-08 16:52:24 +00002576 uint32_t idx = opcodes.GetULEB128(&offset);
2577 if (!decl_map)
2578 {
2579 if (error_ptr)
2580 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) opcode encountered with no decl map.\n", idx);
2581 return false;
2582 }
2583 Value *extern_var = decl_map->GetValueForIndex(idx);
2584 if (!extern_var)
2585 {
2586 if (error_ptr)
2587 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_extern(%u) with invalid index %u.\n", idx, idx);
2588 return false;
2589 }
2590 Value *proxy = extern_var->CreateProxy();
2591 stack.push_back(*proxy);
2592 delete proxy;
Sean Callanan8c127202010-08-23 23:09:38 +00002593 */
Chris Lattner24943d22010-06-08 16:52:24 +00002594 }
2595 break;
2596
2597 case DW_OP_APPLE_scalar_cast:
2598 if (stack.empty())
2599 {
2600 if (error_ptr)
2601 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_scalar_cast.");
2602 return false;
2603 }
2604 else
2605 {
2606 // Simple scalar cast
2607 if (!stack.back().ResolveValue(exe_ctx, ast_context).Cast((Scalar::Type)opcodes.GetU8(&offset)))
2608 {
2609 if (error_ptr)
2610 error_ptr->SetErrorString("Cast failed.");
2611 return false;
2612 }
2613 }
2614 break;
2615
2616
2617 case DW_OP_APPLE_clang_cast:
2618 if (stack.empty())
2619 {
2620 if (error_ptr)
2621 error_ptr->SetErrorString("Expression stack needs at least 1 item for DW_OP_APPLE_clang_cast.");
2622 return false;
2623 }
2624 else
2625 {
2626 void *clang_type = (void *)opcodes.GetMaxU64(&offset, sizeof(void*));
Greg Clayton6916e352010-11-13 03:52:47 +00002627 stack.back().SetContext (Value::eContextTypeClangType, clang_type);
Chris Lattner24943d22010-06-08 16:52:24 +00002628 }
2629 break;
2630 //----------------------------------------------------------------------
2631 // OPCODE: DW_OP_APPLE_constf
2632 // OPERANDS: 1 byte float length, followed by that many bytes containing
2633 // the constant float data.
2634 // DESCRIPTION: Push a float value onto the expression stack.
2635 //----------------------------------------------------------------------
2636 case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size, followed by constant float data
2637 {
2638 uint8_t float_length = opcodes.GetU8(&offset);
2639 if (sizeof(float) == float_length)
2640 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetFloat (&offset);
2641 else if (sizeof(double) == float_length)
2642 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetDouble (&offset);
2643 else if (sizeof(long double) == float_length)
2644 tmp.ResolveValue(exe_ctx, ast_context) = opcodes.GetLongDouble (&offset);
2645 else
2646 {
2647 StreamString new_value;
2648 opcodes.Dump(&new_value, offset, eFormatBytes, 1, float_length, UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
2649
2650 if (error_ptr)
2651 error_ptr->SetErrorStringWithFormat ("DW_OP_APPLE_constf(<%u> %s) unsupported float size.\n", float_length, new_value.GetData());
2652 return false;
2653 }
2654 tmp.SetValueType(Value::eValueTypeScalar);
2655 tmp.ClearContext();
2656 stack.push_back(tmp);
2657 }
2658 break;
2659 //----------------------------------------------------------------------
2660 // OPCODE: DW_OP_APPLE_clear
2661 // OPERANDS: none
2662 // DESCRIPTION: Clears the expression stack.
2663 //----------------------------------------------------------------------
2664 case DW_OP_APPLE_clear:
2665 stack.clear();
2666 break;
2667
2668 //----------------------------------------------------------------------
2669 // OPCODE: DW_OP_APPLE_error
2670 // OPERANDS: none
2671 // DESCRIPTION: Pops a value off of the stack and pushed its value.
2672 // The top item on the stack must be a variable, expression variable.
2673 //----------------------------------------------------------------------
2674 case DW_OP_APPLE_error: // 0xFF - Stops expression evaluation and returns an error (no args)
2675 if (error_ptr)
2676 error_ptr->SetErrorString ("Generic error.");
2677 return false;
2678 }
2679 }
2680
2681 if (stack.empty())
2682 {
2683 if (error_ptr)
2684 error_ptr->SetErrorString ("Stack empty after evaluation.");
2685 return false;
2686 }
2687 else if (log)
2688 {
Chris Lattner24943d22010-06-08 16:52:24 +00002689 size_t count = stack.size();
Sean Callanan6184dfe2010-06-23 00:47:48 +00002690 log->Printf("Stack after operation has %d values:", count);
Chris Lattner24943d22010-06-08 16:52:24 +00002691 for (size_t i=0; i<count; ++i)
2692 {
2693 StreamString new_value;
2694 new_value.Printf("[%zu]", i);
2695 stack[i].Dump(&new_value);
Sean Callanan6184dfe2010-06-23 00:47:48 +00002696 log->Printf(" %s", new_value.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +00002697 }
2698 }
2699
2700 result = stack.back();
2701 return true; // Return true on success
2702}
2703