blob: 8f54bf37ea0fd3ba0bb938a9496a70b4c09ac1c7 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- DWARFExpression.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Expression/DWARFExpression.h"
11
Virgile Bellobdae3782013-08-28 12:14:27 +000012// C Includes
13#include <inttypes.h>
14
15// C++ Includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include <vector>
17
Greg Clayton7349bd92011-05-09 20:18:18 +000018#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Scalar.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include "lldb/Core/Value.h"
21#include "lldb/Core/dwarf.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000022#include "lldb/Utility/DataEncoder.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000023#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000024#include "lldb/Utility/StreamString.h"
Zachary Turnerfb1a0a02017-03-06 18:34:25 +000025#include "lldb/Utility/VMRange.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Jim Inghamf220d592011-12-01 03:01:30 +000027#include "lldb/Host/Host.h"
Zachary Turner01c32432017-02-14 19:06:07 +000028#include "lldb/Utility/Endian.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Sean Callanan4740a732016-09-06 04:48:36 +000030#include "lldb/Symbol/Function.h"
31
Greg Claytonafacd142011-09-02 01:15:17 +000032#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Target/ExecutionContext.h"
34#include "lldb/Target/Process.h"
35#include "lldb/Target/RegisterContext.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000036#include "lldb/Target/StackFrame.h"
Ashok Thirumurthi6e264d32013-07-29 16:05:11 +000037#include "lldb/Target/StackID.h"
Richard Mitton0a558352013-10-17 21:14:00 +000038#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
Jan Kratochvilc4d65752018-03-18 20:11:02 +000040#include "Plugins/SymbolFile/DWARF/DWARFUnit.h"
Tamas Berghammer35d9d2d2015-08-25 11:46:06 +000041
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042using namespace lldb;
43using namespace lldb_private;
44
Tamas Berghammer1f5e4482015-09-16 12:37:06 +000045static lldb::addr_t
Jan Kratochvilc4d65752018-03-18 20:11:02 +000046ReadAddressFromDebugAddrSection(const DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 uint32_t index) {
48 uint32_t index_size = dwarf_cu->GetAddressByteSize();
49 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
50 lldb::offset_t offset = addr_base + index * index_size;
51 return dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
52 &offset, index_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053}
54
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055//----------------------------------------------------------------------
56// DWARFExpression constructor
57//----------------------------------------------------------------------
Jan Kratochvilc4d65752018-03-18 20:11:02 +000058DWARFExpression::DWARFExpression(DWARFUnit *dwarf_cu)
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 : m_module_wp(), m_data(), m_dwarf_cu(dwarf_cu),
60 m_reg_kind(eRegisterKindDWARF), m_loclist_slide(LLDB_INVALID_ADDRESS) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062DWARFExpression::DWARFExpression(const DWARFExpression &rhs)
63 : m_module_wp(rhs.m_module_wp), m_data(rhs.m_data),
64 m_dwarf_cu(rhs.m_dwarf_cu), m_reg_kind(rhs.m_reg_kind),
65 m_loclist_slide(rhs.m_loclist_slide) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
Tamas Berghammer35d9d2d2015-08-25 11:46:06 +000067DWARFExpression::DWARFExpression(lldb::ModuleSP module_sp,
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 const DataExtractor &data,
Jan Kratochvilc4d65752018-03-18 20:11:02 +000069 DWARFUnit *dwarf_cu,
Tamas Berghammer35d9d2d2015-08-25 11:46:06 +000070 lldb::offset_t data_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +000071 lldb::offset_t data_length)
72 : m_module_wp(), m_data(data, data_offset, data_length),
73 m_dwarf_cu(dwarf_cu), m_reg_kind(eRegisterKindDWARF),
74 m_loclist_slide(LLDB_INVALID_ADDRESS) {
75 if (module_sp)
76 m_module_wp = module_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077}
78
79//----------------------------------------------------------------------
80// Destructor
81//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000082DWARFExpression::~DWARFExpression() {}
83
84bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
85
86void DWARFExpression::SetOpcodeData(const DataExtractor &data) {
87 m_data = data;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088}
89
Kate Stoneb9c1b512016-09-06 20:57:50 +000090void DWARFExpression::CopyOpcodeData(lldb::ModuleSP module_sp,
91 const DataExtractor &data,
92 lldb::offset_t data_offset,
93 lldb::offset_t data_length) {
94 const uint8_t *bytes = data.PeekData(data_offset, data_length);
95 if (bytes) {
Richard Mitton0a558352013-10-17 21:14:00 +000096 m_module_wp = module_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 m_data.SetData(DataBufferSP(new DataBufferHeap(bytes, data_length)));
98 m_data.SetByteOrder(data.GetByteOrder());
99 m_data.SetAddressByteSize(data.GetAddressByteSize());
100 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101}
102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103void DWARFExpression::CopyOpcodeData(const void *data,
104 lldb::offset_t data_length,
105 ByteOrder byte_order,
106 uint8_t addr_byte_size) {
107 if (data && data_length) {
108 m_data.SetData(DataBufferSP(new DataBufferHeap(data, data_length)));
109 m_data.SetByteOrder(byte_order);
110 m_data.SetAddressByteSize(addr_byte_size);
111 }
112}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114void DWARFExpression::CopyOpcodeData(uint64_t const_value,
115 lldb::offset_t const_value_byte_size,
116 uint8_t addr_byte_size) {
117 if (const_value_byte_size) {
118 m_data.SetData(
119 DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
120 m_data.SetByteOrder(endian::InlHostByteOrder());
121 m_data.SetAddressByteSize(addr_byte_size);
122 }
123}
Greg Claytonc982c762010-07-09 20:39:50 +0000124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125void DWARFExpression::SetOpcodeData(lldb::ModuleSP module_sp,
126 const DataExtractor &data,
127 lldb::offset_t data_offset,
128 lldb::offset_t data_length) {
129 m_module_wp = module_sp;
130 m_data.SetData(data, data_offset, data_length);
131}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133void DWARFExpression::DumpLocation(Stream *s, lldb::offset_t offset,
134 lldb::offset_t length,
135 lldb::DescriptionLevel level,
136 ABI *abi) const {
137 if (!m_data.ValidOffsetForDataOfSize(offset, length))
138 return;
139 const lldb::offset_t start_offset = offset;
140 const lldb::offset_t end_offset = offset + length;
141 while (m_data.ValidOffset(offset) && offset < end_offset) {
142 const lldb::offset_t op_offset = offset;
143 const uint8_t op = m_data.GetU8(&offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145 switch (level) {
146 default:
147 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 case lldb::eDescriptionLevelBrief:
150 if (offset > start_offset)
151 s->PutChar(' ');
152 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 case lldb::eDescriptionLevelFull:
155 case lldb::eDescriptionLevelVerbose:
156 if (offset > start_offset)
157 s->EOL();
158 s->Indent();
159 if (level == lldb::eDescriptionLevelFull)
160 break;
161 // Fall through for verbose and print offset and DW_OP prefix..
162 s->Printf("0x%8.8" PRIx64 ": %s", op_offset,
163 op >= DW_OP_APPLE_uninit ? "DW_OP_APPLE_" : "DW_OP_");
164 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 switch (op) {
168 case DW_OP_addr:
169 *s << "DW_OP_addr(" << m_data.GetAddress(&offset) << ") ";
170 break; // 0x03 1 address
171 case DW_OP_deref:
172 *s << "DW_OP_deref";
173 break; // 0x06
174 case DW_OP_const1u:
175 s->Printf("DW_OP_const1u(0x%2.2x) ", m_data.GetU8(&offset));
176 break; // 0x08 1 1-byte constant
177 case DW_OP_const1s:
178 s->Printf("DW_OP_const1s(0x%2.2x) ", m_data.GetU8(&offset));
179 break; // 0x09 1 1-byte constant
180 case DW_OP_const2u:
181 s->Printf("DW_OP_const2u(0x%4.4x) ", m_data.GetU16(&offset));
182 break; // 0x0a 1 2-byte constant
183 case DW_OP_const2s:
184 s->Printf("DW_OP_const2s(0x%4.4x) ", m_data.GetU16(&offset));
185 break; // 0x0b 1 2-byte constant
186 case DW_OP_const4u:
187 s->Printf("DW_OP_const4u(0x%8.8x) ", m_data.GetU32(&offset));
188 break; // 0x0c 1 4-byte constant
189 case DW_OP_const4s:
190 s->Printf("DW_OP_const4s(0x%8.8x) ", m_data.GetU32(&offset));
191 break; // 0x0d 1 4-byte constant
192 case DW_OP_const8u:
193 s->Printf("DW_OP_const8u(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset));
194 break; // 0x0e 1 8-byte constant
195 case DW_OP_const8s:
196 s->Printf("DW_OP_const8s(0x%16.16" PRIx64 ") ", m_data.GetU64(&offset));
197 break; // 0x0f 1 8-byte constant
198 case DW_OP_constu:
199 s->Printf("DW_OP_constu(0x%" PRIx64 ") ", m_data.GetULEB128(&offset));
200 break; // 0x10 1 ULEB128 constant
201 case DW_OP_consts:
202 s->Printf("DW_OP_consts(0x%" PRId64 ") ", m_data.GetSLEB128(&offset));
203 break; // 0x11 1 SLEB128 constant
204 case DW_OP_dup:
205 s->PutCString("DW_OP_dup");
206 break; // 0x12
207 case DW_OP_drop:
208 s->PutCString("DW_OP_drop");
209 break; // 0x13
210 case DW_OP_over:
211 s->PutCString("DW_OP_over");
212 break; // 0x14
213 case DW_OP_pick:
214 s->Printf("DW_OP_pick(0x%2.2x) ", m_data.GetU8(&offset));
215 break; // 0x15 1 1-byte stack index
216 case DW_OP_swap:
217 s->PutCString("DW_OP_swap");
218 break; // 0x16
219 case DW_OP_rot:
220 s->PutCString("DW_OP_rot");
221 break; // 0x17
222 case DW_OP_xderef:
223 s->PutCString("DW_OP_xderef");
224 break; // 0x18
225 case DW_OP_abs:
226 s->PutCString("DW_OP_abs");
227 break; // 0x19
228 case DW_OP_and:
229 s->PutCString("DW_OP_and");
230 break; // 0x1a
231 case DW_OP_div:
232 s->PutCString("DW_OP_div");
233 break; // 0x1b
234 case DW_OP_minus:
235 s->PutCString("DW_OP_minus");
236 break; // 0x1c
237 case DW_OP_mod:
238 s->PutCString("DW_OP_mod");
239 break; // 0x1d
240 case DW_OP_mul:
241 s->PutCString("DW_OP_mul");
242 break; // 0x1e
243 case DW_OP_neg:
244 s->PutCString("DW_OP_neg");
245 break; // 0x1f
246 case DW_OP_not:
247 s->PutCString("DW_OP_not");
248 break; // 0x20
249 case DW_OP_or:
250 s->PutCString("DW_OP_or");
251 break; // 0x21
252 case DW_OP_plus:
253 s->PutCString("DW_OP_plus");
254 break; // 0x22
255 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
256 s->Printf("DW_OP_plus_uconst(0x%" PRIx64 ") ",
257 m_data.GetULEB128(&offset));
258 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 case DW_OP_shl:
261 s->PutCString("DW_OP_shl");
262 break; // 0x24
263 case DW_OP_shr:
264 s->PutCString("DW_OP_shr");
265 break; // 0x25
266 case DW_OP_shra:
267 s->PutCString("DW_OP_shra");
268 break; // 0x26
269 case DW_OP_xor:
270 s->PutCString("DW_OP_xor");
271 break; // 0x27
272 case DW_OP_skip:
273 s->Printf("DW_OP_skip(0x%4.4x)", m_data.GetU16(&offset));
274 break; // 0x2f 1 signed 2-byte constant
275 case DW_OP_bra:
276 s->Printf("DW_OP_bra(0x%4.4x)", m_data.GetU16(&offset));
277 break; // 0x28 1 signed 2-byte constant
278 case DW_OP_eq:
279 s->PutCString("DW_OP_eq");
280 break; // 0x29
281 case DW_OP_ge:
282 s->PutCString("DW_OP_ge");
283 break; // 0x2a
284 case DW_OP_gt:
285 s->PutCString("DW_OP_gt");
286 break; // 0x2b
287 case DW_OP_le:
288 s->PutCString("DW_OP_le");
289 break; // 0x2c
290 case DW_OP_lt:
291 s->PutCString("DW_OP_lt");
292 break; // 0x2d
293 case DW_OP_ne:
294 s->PutCString("DW_OP_ne");
295 break; // 0x2e
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 case DW_OP_lit0: // 0x30
298 case DW_OP_lit1: // 0x31
299 case DW_OP_lit2: // 0x32
300 case DW_OP_lit3: // 0x33
301 case DW_OP_lit4: // 0x34
302 case DW_OP_lit5: // 0x35
303 case DW_OP_lit6: // 0x36
304 case DW_OP_lit7: // 0x37
305 case DW_OP_lit8: // 0x38
306 case DW_OP_lit9: // 0x39
307 case DW_OP_lit10: // 0x3A
308 case DW_OP_lit11: // 0x3B
309 case DW_OP_lit12: // 0x3C
310 case DW_OP_lit13: // 0x3D
311 case DW_OP_lit14: // 0x3E
312 case DW_OP_lit15: // 0x3F
313 case DW_OP_lit16: // 0x40
314 case DW_OP_lit17: // 0x41
315 case DW_OP_lit18: // 0x42
316 case DW_OP_lit19: // 0x43
317 case DW_OP_lit20: // 0x44
318 case DW_OP_lit21: // 0x45
319 case DW_OP_lit22: // 0x46
320 case DW_OP_lit23: // 0x47
321 case DW_OP_lit24: // 0x48
322 case DW_OP_lit25: // 0x49
323 case DW_OP_lit26: // 0x4A
324 case DW_OP_lit27: // 0x4B
325 case DW_OP_lit28: // 0x4C
326 case DW_OP_lit29: // 0x4D
327 case DW_OP_lit30: // 0x4E
328 case DW_OP_lit31:
329 s->Printf("DW_OP_lit%i", op - DW_OP_lit0);
330 break; // 0x4f
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 case DW_OP_reg0: // 0x50
333 case DW_OP_reg1: // 0x51
334 case DW_OP_reg2: // 0x52
335 case DW_OP_reg3: // 0x53
336 case DW_OP_reg4: // 0x54
337 case DW_OP_reg5: // 0x55
338 case DW_OP_reg6: // 0x56
339 case DW_OP_reg7: // 0x57
340 case DW_OP_reg8: // 0x58
341 case DW_OP_reg9: // 0x59
342 case DW_OP_reg10: // 0x5A
343 case DW_OP_reg11: // 0x5B
344 case DW_OP_reg12: // 0x5C
345 case DW_OP_reg13: // 0x5D
346 case DW_OP_reg14: // 0x5E
347 case DW_OP_reg15: // 0x5F
348 case DW_OP_reg16: // 0x60
349 case DW_OP_reg17: // 0x61
350 case DW_OP_reg18: // 0x62
351 case DW_OP_reg19: // 0x63
352 case DW_OP_reg20: // 0x64
353 case DW_OP_reg21: // 0x65
354 case DW_OP_reg22: // 0x66
355 case DW_OP_reg23: // 0x67
356 case DW_OP_reg24: // 0x68
357 case DW_OP_reg25: // 0x69
358 case DW_OP_reg26: // 0x6A
359 case DW_OP_reg27: // 0x6B
360 case DW_OP_reg28: // 0x6C
361 case DW_OP_reg29: // 0x6D
362 case DW_OP_reg30: // 0x6E
363 case DW_OP_reg31: // 0x6F
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 uint32_t reg_num = op - DW_OP_reg0;
366 if (abi) {
367 RegisterInfo reg_info;
368 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
369 if (reg_info.name) {
370 s->PutCString(reg_info.name);
371 break;
372 } else if (reg_info.alt_name) {
373 s->PutCString(reg_info.alt_name);
374 break;
375 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 }
378 s->Printf("DW_OP_reg%u", reg_num);
379 break;
380 } break;
381
382 case DW_OP_breg0:
383 case DW_OP_breg1:
384 case DW_OP_breg2:
385 case DW_OP_breg3:
386 case DW_OP_breg4:
387 case DW_OP_breg5:
388 case DW_OP_breg6:
389 case DW_OP_breg7:
390 case DW_OP_breg8:
391 case DW_OP_breg9:
392 case DW_OP_breg10:
393 case DW_OP_breg11:
394 case DW_OP_breg12:
395 case DW_OP_breg13:
396 case DW_OP_breg14:
397 case DW_OP_breg15:
398 case DW_OP_breg16:
399 case DW_OP_breg17:
400 case DW_OP_breg18:
401 case DW_OP_breg19:
402 case DW_OP_breg20:
403 case DW_OP_breg21:
404 case DW_OP_breg22:
405 case DW_OP_breg23:
406 case DW_OP_breg24:
407 case DW_OP_breg25:
408 case DW_OP_breg26:
409 case DW_OP_breg27:
410 case DW_OP_breg28:
411 case DW_OP_breg29:
412 case DW_OP_breg30:
413 case DW_OP_breg31: {
414 uint32_t reg_num = op - DW_OP_breg0;
415 int64_t reg_offset = m_data.GetSLEB128(&offset);
416 if (abi) {
417 RegisterInfo reg_info;
418 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
419 if (reg_info.name) {
420 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
421 break;
422 } else if (reg_info.alt_name) {
423 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
424 break;
425 }
426 }
427 }
428 s->Printf("DW_OP_breg%i(0x%" PRIx64 ")", reg_num, reg_offset);
429 } break;
430
431 case DW_OP_regx: // 0x90 1 ULEB128 register
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 uint32_t reg_num = m_data.GetULEB128(&offset);
434 if (abi) {
435 RegisterInfo reg_info;
436 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
437 if (reg_info.name) {
438 s->PutCString(reg_info.name);
439 break;
440 } else if (reg_info.alt_name) {
441 s->PutCString(reg_info.alt_name);
442 break;
443 }
444 }
445 }
446 s->Printf("DW_OP_regx(%" PRIu32 ")", reg_num);
447 break;
448 } break;
449 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
450 s->Printf("DW_OP_fbreg(%" PRIi64 ")", m_data.GetSLEB128(&offset));
451 break;
452 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
453 {
454 uint32_t reg_num = m_data.GetULEB128(&offset);
455 int64_t reg_offset = m_data.GetSLEB128(&offset);
456 if (abi) {
457 RegisterInfo reg_info;
458 if (abi->GetRegisterInfoByKind(m_reg_kind, reg_num, reg_info)) {
459 if (reg_info.name) {
460 s->Printf("[%s%+" PRIi64 "]", reg_info.name, reg_offset);
461 break;
462 } else if (reg_info.alt_name) {
463 s->Printf("[%s%+" PRIi64 "]", reg_info.alt_name, reg_offset);
464 break;
465 }
466 }
467 }
468 s->Printf("DW_OP_bregx(reg=%" PRIu32 ",offset=%" PRIi64 ")", reg_num,
469 reg_offset);
470 } break;
471 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
472 s->Printf("DW_OP_piece(0x%" PRIx64 ")", m_data.GetULEB128(&offset));
473 break;
474 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
475 s->Printf("DW_OP_deref_size(0x%2.2x)", m_data.GetU8(&offset));
476 break;
477 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
478 s->Printf("DW_OP_xderef_size(0x%2.2x)", m_data.GetU8(&offset));
479 break;
480 case DW_OP_nop:
481 s->PutCString("DW_OP_nop");
482 break; // 0x96
483 case DW_OP_push_object_address:
484 s->PutCString("DW_OP_push_object_address");
485 break; // 0x97 DWARF3
486 case DW_OP_call2: // 0x98 DWARF3 1 2-byte offset of DIE
487 s->Printf("DW_OP_call2(0x%4.4x)", m_data.GetU16(&offset));
488 break;
489 case DW_OP_call4: // 0x99 DWARF3 1 4-byte offset of DIE
490 s->Printf("DW_OP_call4(0x%8.8x)", m_data.GetU32(&offset));
491 break;
492 case DW_OP_call_ref: // 0x9a DWARF3 1 4- or 8-byte offset of DIE
493 s->Printf("DW_OP_call_ref(0x%8.8" PRIx64 ")", m_data.GetAddress(&offset));
494 break;
495 // case DW_OP_call_frame_cfa: s << "call_frame_cfa"; break;
496 // // 0x9c DWARF3
497 // case DW_OP_bit_piece: // 0x9d DWARF3 2
498 // s->Printf("DW_OP_bit_piece(0x%x, 0x%x)",
499 // m_data.GetULEB128(&offset), m_data.GetULEB128(&offset));
500 // break;
501 // case DW_OP_lo_user: s->PutCString("DW_OP_lo_user"); break;
502 // // 0xe0
503 // case DW_OP_hi_user: s->PutCString("DW_OP_hi_user"); break;
504 // // 0xff
505 // case DW_OP_APPLE_extern:
506 // s->Printf("DW_OP_APPLE_extern(%" PRIu64 ")",
507 // m_data.GetULEB128(&offset));
508 // break;
509 // case DW_OP_APPLE_array_ref:
510 // s->PutCString("DW_OP_APPLE_array_ref");
511 // break;
512 case DW_OP_form_tls_address:
513 s->PutCString("DW_OP_form_tls_address"); // 0x9b
514 break;
515 case DW_OP_GNU_addr_index: // 0xfb
516 s->Printf("DW_OP_GNU_addr_index(0x%" PRIx64 ")",
517 m_data.GetULEB128(&offset));
518 break;
519 case DW_OP_GNU_const_index: // 0xfc
520 s->Printf("DW_OP_GNU_const_index(0x%" PRIx64 ")",
521 m_data.GetULEB128(&offset));
522 break;
523 case DW_OP_GNU_push_tls_address:
524 s->PutCString("DW_OP_GNU_push_tls_address"); // 0xe0
525 break;
526 case DW_OP_APPLE_uninit:
527 s->PutCString("DW_OP_APPLE_uninit"); // 0xF0
528 break;
529 // case DW_OP_APPLE_assign: // 0xF1 - pops value off and
530 // assigns it to second item on stack (2nd item must have
531 // assignable context)
532 // s->PutCString("DW_OP_APPLE_assign");
533 // break;
534 // case DW_OP_APPLE_address_of: // 0xF2 - gets the address of
535 // the top stack item (top item must be a variable, or have
536 // value_type that is an address already)
537 // s->PutCString("DW_OP_APPLE_address_of");
538 // break;
539 // case DW_OP_APPLE_value_of: // 0xF3 - pops the value off the
540 // stack and pushes the value of that object (top item must be a
541 // variable, or expression local)
542 // s->PutCString("DW_OP_APPLE_value_of");
543 // break;
544 // case DW_OP_APPLE_deref_type: // 0xF4 - gets the address of
545 // the top stack item (top item must be a variable, or a clang
546 // type)
547 // s->PutCString("DW_OP_APPLE_deref_type");
548 // break;
549 // case DW_OP_APPLE_expr_local: // 0xF5 - ULEB128 expression
550 // local index
551 // s->Printf("DW_OP_APPLE_expr_local(%" PRIu64 ")",
552 // m_data.GetULEB128(&offset));
553 // break;
554 // case DW_OP_APPLE_constf: // 0xF6 - 1 byte float size,
555 // followed by constant float data
556 // {
557 // uint8_t float_length = m_data.GetU8(&offset);
558 // s->Printf("DW_OP_APPLE_constf(<%u> ", float_length);
559 // m_data.Dump(s, offset, eFormatHex, float_length, 1,
560 // UINT32_MAX, DW_INVALID_ADDRESS, 0, 0);
561 // s->PutChar(')');
562 // // Consume the float data
563 // m_data.GetData(&offset, float_length);
564 // }
565 // break;
566 // case DW_OP_APPLE_scalar_cast:
567 // s->Printf("DW_OP_APPLE_scalar_cast(%s)",
568 // Scalar::GetValueTypeAsCString
569 // ((Scalar::Type)m_data.GetU8(&offset)));
570 // break;
571 // case DW_OP_APPLE_clang_cast:
572 // {
573 // clang::Type *clang_type = (clang::Type
574 // *)m_data.GetMaxU64(&offset, sizeof(void*));
575 // s->Printf("DW_OP_APPLE_clang_cast(%p)", clang_type);
576 // }
577 // break;
578 // case DW_OP_APPLE_clear:
579 // s->PutCString("DW_OP_APPLE_clear");
580 // break;
581 // case DW_OP_APPLE_error: // 0xFF - Stops expression
582 // evaluation and returns an error (no args)
583 // s->PutCString("DW_OP_APPLE_error");
584 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
Kate Stoneb9c1b512016-09-06 20:57:50 +0000589void DWARFExpression::SetLocationListSlide(addr_t slide) {
590 m_loclist_slide = slide;
591}
592
593int DWARFExpression::GetRegisterKind() { return m_reg_kind; }
594
595void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) {
596 m_reg_kind = reg_kind;
597}
598
599bool DWARFExpression::IsLocationList() const {
600 return m_loclist_slide != LLDB_INVALID_ADDRESS;
601}
602
603void DWARFExpression::GetDescription(Stream *s, lldb::DescriptionLevel level,
604 addr_t location_list_base_addr,
605 ABI *abi) const {
606 if (IsLocationList()) {
607 // We have a location list
608 lldb::offset_t offset = 0;
609 uint32_t count = 0;
610 addr_t curr_base_addr = location_list_base_addr;
611 while (m_data.ValidOffset(offset)) {
612 addr_t begin_addr_offset = LLDB_INVALID_ADDRESS;
613 addr_t end_addr_offset = LLDB_INVALID_ADDRESS;
614 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
615 begin_addr_offset, end_addr_offset))
616 break;
617
618 if (begin_addr_offset == 0 && end_addr_offset == 0)
619 break;
620
621 if (begin_addr_offset < end_addr_offset) {
622 if (count > 0)
623 s->PutCString(", ");
624 VMRange addr_range(curr_base_addr + begin_addr_offset,
625 curr_base_addr + end_addr_offset);
626 addr_range.Dump(s, 0, 8);
627 s->PutChar('{');
628 lldb::offset_t location_length = m_data.GetU16(&offset);
629 DumpLocation(s, offset, location_length, level, abi);
630 s->PutChar('}');
631 offset += location_length;
632 } else {
633 if ((m_data.GetAddressByteSize() == 4 &&
634 (begin_addr_offset == UINT32_MAX)) ||
635 (m_data.GetAddressByteSize() == 8 &&
636 (begin_addr_offset == UINT64_MAX))) {
637 curr_base_addr = end_addr_offset + location_list_base_addr;
638 // We have a new base address
639 if (count > 0)
640 s->PutCString(", ");
641 *s << "base_addr = " << end_addr_offset;
642 }
643 }
644
645 count++;
646 }
647 } else {
648 // We have a normal location that contains DW_OP location opcodes
649 DumpLocation(s, 0, m_data.GetByteSize(), level, abi);
650 }
651}
652
653static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
654 lldb::RegisterKind reg_kind,
Zachary Turner97206d52017-05-12 04:51:55 +0000655 uint32_t reg_num, Status *error_ptr,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 Value &value) {
657 if (reg_ctx == NULL) {
658 if (error_ptr)
659 error_ptr->SetErrorStringWithFormat("No register context in frame.\n");
660 } else {
661 uint32_t native_reg =
662 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
663 if (native_reg == LLDB_INVALID_REGNUM) {
664 if (error_ptr)
665 error_ptr->SetErrorStringWithFormat("Unable to convert register "
666 "kind=%u reg_num=%u to a native "
667 "register number.\n",
668 reg_kind, reg_num);
669 } else {
670 const RegisterInfo *reg_info =
671 reg_ctx->GetRegisterInfoAtIndex(native_reg);
672 RegisterValue reg_value;
673 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
674 if (reg_value.GetScalarValue(value.GetScalar())) {
675 value.SetValueType(Value::eValueTypeScalar);
676 value.SetContext(Value::eContextTypeRegisterInfo,
677 const_cast<RegisterInfo *>(reg_info));
678 if (error_ptr)
679 error_ptr->Clear();
680 return true;
681 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000682 // If we get this error, then we need to implement a value buffer in
683 // the dwarf expression evaluation function...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000684 if (error_ptr)
685 error_ptr->SetErrorStringWithFormat(
686 "register %s can't be converted to a scalar value",
687 reg_info->name);
688 }
689 } else {
Jason Molenda2d107dd2010-11-20 01:28:30 +0000690 if (error_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000691 error_ptr->SetErrorStringWithFormat("register %s is not available",
692 reg_info->name);
693 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000695 }
696 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697}
698
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699// bool
700// DWARFExpression::LocationListContainsLoadAddress (Process* process, const
701// Address &addr) const
Greg Clayton016a95e2010-09-14 02:20:48 +0000702//{
Kate Stoneb9c1b512016-09-06 20:57:50 +0000703// return LocationListContainsLoadAddress(process,
704// addr.GetLoadAddress(process));
Greg Clayton016a95e2010-09-14 02:20:48 +0000705//}
706//
Kate Stoneb9c1b512016-09-06 20:57:50 +0000707// bool
708// DWARFExpression::LocationListContainsLoadAddress (Process* process, addr_t
709// load_addr) const
Greg Clayton016a95e2010-09-14 02:20:48 +0000710//{
711// if (load_addr == LLDB_INVALID_ADDRESS)
712// return false;
713//
714// if (IsLocationList())
715// {
Greg Claytonc7bece562013-01-25 18:06:21 +0000716// lldb::offset_t offset = 0;
Greg Clayton016a95e2010-09-14 02:20:48 +0000717//
718// addr_t loc_list_base_addr = m_loclist_slide.GetLoadAddress(process);
719//
720// if (loc_list_base_addr == LLDB_INVALID_ADDRESS)
721// return false;
722//
723// while (m_data.ValidOffset(offset))
724// {
725// // We need to figure out what the value is for the location.
726// addr_t lo_pc = m_data.GetAddress(&offset);
727// addr_t hi_pc = m_data.GetAddress(&offset);
728// if (lo_pc == 0 && hi_pc == 0)
729// break;
730// else
731// {
732// lo_pc += loc_list_base_addr;
733// hi_pc += loc_list_base_addr;
734//
735// if (lo_pc <= load_addr && load_addr < hi_pc)
736// return true;
737//
738// offset += m_data.GetU16(&offset);
739// }
740// }
741// }
742// return false;
743//}
Greg Clayton9da7bd02010-08-24 21:05:24 +0000744
Kate Stoneb9c1b512016-09-06 20:57:50 +0000745static offset_t GetOpcodeDataSize(const DataExtractor &data,
746 const lldb::offset_t data_offset,
747 const uint8_t op) {
748 lldb::offset_t offset = data_offset;
749 switch (op) {
750 case DW_OP_addr:
751 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
752 return data.GetAddressByteSize();
Greg Clayton2fc93ea2011-11-13 04:15:56 +0000753
Kate Stoneb9c1b512016-09-06 20:57:50 +0000754 // Opcodes with no arguments
755 case DW_OP_deref: // 0x06
756 case DW_OP_dup: // 0x12
757 case DW_OP_drop: // 0x13
758 case DW_OP_over: // 0x14
759 case DW_OP_swap: // 0x16
760 case DW_OP_rot: // 0x17
761 case DW_OP_xderef: // 0x18
762 case DW_OP_abs: // 0x19
763 case DW_OP_and: // 0x1a
764 case DW_OP_div: // 0x1b
765 case DW_OP_minus: // 0x1c
766 case DW_OP_mod: // 0x1d
767 case DW_OP_mul: // 0x1e
768 case DW_OP_neg: // 0x1f
769 case DW_OP_not: // 0x20
770 case DW_OP_or: // 0x21
771 case DW_OP_plus: // 0x22
772 case DW_OP_shl: // 0x24
773 case DW_OP_shr: // 0x25
774 case DW_OP_shra: // 0x26
775 case DW_OP_xor: // 0x27
776 case DW_OP_eq: // 0x29
777 case DW_OP_ge: // 0x2a
778 case DW_OP_gt: // 0x2b
779 case DW_OP_le: // 0x2c
780 case DW_OP_lt: // 0x2d
781 case DW_OP_ne: // 0x2e
782 case DW_OP_lit0: // 0x30
783 case DW_OP_lit1: // 0x31
784 case DW_OP_lit2: // 0x32
785 case DW_OP_lit3: // 0x33
786 case DW_OP_lit4: // 0x34
787 case DW_OP_lit5: // 0x35
788 case DW_OP_lit6: // 0x36
789 case DW_OP_lit7: // 0x37
790 case DW_OP_lit8: // 0x38
791 case DW_OP_lit9: // 0x39
792 case DW_OP_lit10: // 0x3A
793 case DW_OP_lit11: // 0x3B
794 case DW_OP_lit12: // 0x3C
795 case DW_OP_lit13: // 0x3D
796 case DW_OP_lit14: // 0x3E
797 case DW_OP_lit15: // 0x3F
798 case DW_OP_lit16: // 0x40
799 case DW_OP_lit17: // 0x41
800 case DW_OP_lit18: // 0x42
801 case DW_OP_lit19: // 0x43
802 case DW_OP_lit20: // 0x44
803 case DW_OP_lit21: // 0x45
804 case DW_OP_lit22: // 0x46
805 case DW_OP_lit23: // 0x47
806 case DW_OP_lit24: // 0x48
807 case DW_OP_lit25: // 0x49
808 case DW_OP_lit26: // 0x4A
809 case DW_OP_lit27: // 0x4B
810 case DW_OP_lit28: // 0x4C
811 case DW_OP_lit29: // 0x4D
812 case DW_OP_lit30: // 0x4E
813 case DW_OP_lit31: // 0x4f
814 case DW_OP_reg0: // 0x50
815 case DW_OP_reg1: // 0x51
816 case DW_OP_reg2: // 0x52
817 case DW_OP_reg3: // 0x53
818 case DW_OP_reg4: // 0x54
819 case DW_OP_reg5: // 0x55
820 case DW_OP_reg6: // 0x56
821 case DW_OP_reg7: // 0x57
822 case DW_OP_reg8: // 0x58
823 case DW_OP_reg9: // 0x59
824 case DW_OP_reg10: // 0x5A
825 case DW_OP_reg11: // 0x5B
826 case DW_OP_reg12: // 0x5C
827 case DW_OP_reg13: // 0x5D
828 case DW_OP_reg14: // 0x5E
829 case DW_OP_reg15: // 0x5F
830 case DW_OP_reg16: // 0x60
831 case DW_OP_reg17: // 0x61
832 case DW_OP_reg18: // 0x62
833 case DW_OP_reg19: // 0x63
834 case DW_OP_reg20: // 0x64
835 case DW_OP_reg21: // 0x65
836 case DW_OP_reg22: // 0x66
837 case DW_OP_reg23: // 0x67
838 case DW_OP_reg24: // 0x68
839 case DW_OP_reg25: // 0x69
840 case DW_OP_reg26: // 0x6A
841 case DW_OP_reg27: // 0x6B
842 case DW_OP_reg28: // 0x6C
843 case DW_OP_reg29: // 0x6D
844 case DW_OP_reg30: // 0x6E
845 case DW_OP_reg31: // 0x6F
846 case DW_OP_nop: // 0x96
847 case DW_OP_push_object_address: // 0x97 DWARF3
848 case DW_OP_form_tls_address: // 0x9b DWARF3
849 case DW_OP_call_frame_cfa: // 0x9c DWARF3
850 case DW_OP_stack_value: // 0x9f DWARF4
851 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
Tamas Berghammer1f5e4482015-09-16 12:37:06 +0000852 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853
854 // Opcodes with a single 1 byte arguments
855 case DW_OP_const1u: // 0x08 1 1-byte constant
856 case DW_OP_const1s: // 0x09 1 1-byte constant
857 case DW_OP_pick: // 0x15 1 1-byte stack index
858 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
859 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
860 return 1;
861
862 // Opcodes with a single 2 byte arguments
863 case DW_OP_const2u: // 0x0a 1 2-byte constant
864 case DW_OP_const2s: // 0x0b 1 2-byte constant
865 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
866 case DW_OP_bra: // 0x28 1 signed 2-byte constant
867 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
868 return 2;
869
870 // Opcodes with a single 4 byte arguments
871 case DW_OP_const4u: // 0x0c 1 4-byte constant
872 case DW_OP_const4s: // 0x0d 1 4-byte constant
873 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
874 return 4;
875
876 // Opcodes with a single 8 byte arguments
877 case DW_OP_const8u: // 0x0e 1 8-byte constant
878 case DW_OP_const8s: // 0x0f 1 8-byte constant
879 return 8;
880
881 // All opcodes that have a single ULEB (signed or unsigned) argument
882 case DW_OP_constu: // 0x10 1 ULEB128 constant
883 case DW_OP_consts: // 0x11 1 SLEB128 constant
884 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
885 case DW_OP_breg0: // 0x70 1 ULEB128 register
886 case DW_OP_breg1: // 0x71 1 ULEB128 register
887 case DW_OP_breg2: // 0x72 1 ULEB128 register
888 case DW_OP_breg3: // 0x73 1 ULEB128 register
889 case DW_OP_breg4: // 0x74 1 ULEB128 register
890 case DW_OP_breg5: // 0x75 1 ULEB128 register
891 case DW_OP_breg6: // 0x76 1 ULEB128 register
892 case DW_OP_breg7: // 0x77 1 ULEB128 register
893 case DW_OP_breg8: // 0x78 1 ULEB128 register
894 case DW_OP_breg9: // 0x79 1 ULEB128 register
895 case DW_OP_breg10: // 0x7a 1 ULEB128 register
896 case DW_OP_breg11: // 0x7b 1 ULEB128 register
897 case DW_OP_breg12: // 0x7c 1 ULEB128 register
898 case DW_OP_breg13: // 0x7d 1 ULEB128 register
899 case DW_OP_breg14: // 0x7e 1 ULEB128 register
900 case DW_OP_breg15: // 0x7f 1 ULEB128 register
901 case DW_OP_breg16: // 0x80 1 ULEB128 register
902 case DW_OP_breg17: // 0x81 1 ULEB128 register
903 case DW_OP_breg18: // 0x82 1 ULEB128 register
904 case DW_OP_breg19: // 0x83 1 ULEB128 register
905 case DW_OP_breg20: // 0x84 1 ULEB128 register
906 case DW_OP_breg21: // 0x85 1 ULEB128 register
907 case DW_OP_breg22: // 0x86 1 ULEB128 register
908 case DW_OP_breg23: // 0x87 1 ULEB128 register
909 case DW_OP_breg24: // 0x88 1 ULEB128 register
910 case DW_OP_breg25: // 0x89 1 ULEB128 register
911 case DW_OP_breg26: // 0x8a 1 ULEB128 register
912 case DW_OP_breg27: // 0x8b 1 ULEB128 register
913 case DW_OP_breg28: // 0x8c 1 ULEB128 register
914 case DW_OP_breg29: // 0x8d 1 ULEB128 register
915 case DW_OP_breg30: // 0x8e 1 ULEB128 register
916 case DW_OP_breg31: // 0x8f 1 ULEB128 register
917 case DW_OP_regx: // 0x90 1 ULEB128 register
918 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
919 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
920 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
921 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
922 data.Skip_LEB128(&offset);
923 return offset - data_offset;
924
925 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
926 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
927 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
928 data.Skip_LEB128(&offset);
929 data.Skip_LEB128(&offset);
930 return offset - data_offset;
931
932 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
933 // (DWARF4)
934 {
935 uint64_t block_len = data.Skip_LEB128(&offset);
936 offset += block_len;
937 return offset - data_offset;
938 }
939
940 default:
941 break;
942 }
943 return LLDB_INVALID_OFFSET;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +0000944}
945
Kate Stoneb9c1b512016-09-06 20:57:50 +0000946lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(uint32_t op_addr_idx,
947 bool &error) const {
948 error = false;
949 if (IsLocationList())
950 return LLDB_INVALID_ADDRESS;
951 lldb::offset_t offset = 0;
952 uint32_t curr_op_addr_idx = 0;
953 while (m_data.ValidOffset(offset)) {
954 const uint8_t op = m_data.GetU8(&offset);
955
956 if (op == DW_OP_addr) {
957 const lldb::addr_t op_file_addr = m_data.GetAddress(&offset);
958 if (curr_op_addr_idx == op_addr_idx)
959 return op_file_addr;
960 else
961 ++curr_op_addr_idx;
962 } else if (op == DW_OP_GNU_addr_index) {
963 uint64_t index = m_data.GetULEB128(&offset);
964 if (curr_op_addr_idx == op_addr_idx) {
965 if (!m_dwarf_cu) {
966 error = true;
967 break;
968 }
969
970 return ReadAddressFromDebugAddrSection(m_dwarf_cu, index);
971 } else
972 ++curr_op_addr_idx;
973 } else {
974 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
975 if (op_arg_size == LLDB_INVALID_OFFSET) {
976 error = true;
977 break;
978 }
979 offset += op_arg_size;
980 }
981 }
982 return LLDB_INVALID_ADDRESS;
983}
984
985bool DWARFExpression::Update_DW_OP_addr(lldb::addr_t file_addr) {
986 if (IsLocationList())
987 return false;
988 lldb::offset_t offset = 0;
989 while (m_data.ValidOffset(offset)) {
990 const uint8_t op = m_data.GetU8(&offset);
991
992 if (op == DW_OP_addr) {
993 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
Adrian Prantl05097242018-04-30 16:49:04 +0000994 // We have to make a copy of the data as we don't know if this data is
995 // from a read only memory mapped buffer, so we duplicate all of the data
996 // first, then modify it, and if all goes well, we then replace the data
997 // for this expression
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998
999 // So first we copy the data into a heap buffer
1000 std::unique_ptr<DataBufferHeap> head_data_ap(
1001 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
1002
Adrian Prantl05097242018-04-30 16:49:04 +00001003 // Make en encoder so we can write the address into the buffer using the
1004 // correct byte order (endianness)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 DataEncoder encoder(head_data_ap->GetBytes(), head_data_ap->GetByteSize(),
1006 m_data.GetByteOrder(), addr_byte_size);
1007
1008 // Replace the address in the new buffer
1009 if (encoder.PutMaxU64(offset, addr_byte_size, file_addr) == UINT32_MAX)
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001010 return false;
1011
Adrian Prantl05097242018-04-30 16:49:04 +00001012 // All went well, so now we can reset the data using a shared pointer to
1013 // the heap data so "m_data" will now correctly manage the heap data.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001014 m_data.SetData(DataBufferSP(head_data_ap.release()));
1015 return true;
1016 } else {
1017 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1018 if (op_arg_size == LLDB_INVALID_OFFSET)
1019 break;
1020 offset += op_arg_size;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001021 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001022 }
1023 return false;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001024}
1025
Kate Stoneb9c1b512016-09-06 20:57:50 +00001026bool DWARFExpression::ContainsThreadLocalStorage() const {
Adrian Prantl05097242018-04-30 16:49:04 +00001027 // We are assuming for now that any thread local variable will not have a
1028 // location list. This has been true for all thread local variables we have
1029 // seen so far produced by any compiler.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001030 if (IsLocationList())
1031 return false;
1032 lldb::offset_t offset = 0;
1033 while (m_data.ValidOffset(offset)) {
1034 const uint8_t op = m_data.GetU8(&offset);
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001035
Kate Stoneb9c1b512016-09-06 20:57:50 +00001036 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
1037 return true;
1038 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1039 if (op_arg_size == LLDB_INVALID_OFFSET)
1040 return false;
1041 else
1042 offset += op_arg_size;
1043 }
1044 return false;
1045}
1046bool DWARFExpression::LinkThreadLocalStorage(
1047 lldb::ModuleSP new_module_sp,
1048 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
1049 &link_address_callback) {
Adrian Prantl05097242018-04-30 16:49:04 +00001050 // We are assuming for now that any thread local variable will not have a
1051 // location list. This has been true for all thread local variables we have
1052 // seen so far produced by any compiler.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001053 if (IsLocationList())
1054 return false;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001055
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
Adrian Prantl05097242018-04-30 16:49:04 +00001057 // We have to make a copy of the data as we don't know if this data is from a
1058 // read only memory mapped buffer, so we duplicate all of the data first,
1059 // then modify it, and if all goes well, we then replace the data for this
1060 // expression
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001061
Kate Stoneb9c1b512016-09-06 20:57:50 +00001062 // So first we copy the data into a heap buffer
1063 std::shared_ptr<DataBufferHeap> heap_data_sp(
1064 new DataBufferHeap(m_data.GetDataStart(), m_data.GetByteSize()));
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001065
Adrian Prantl05097242018-04-30 16:49:04 +00001066 // Make en encoder so we can write the address into the buffer using the
1067 // correct byte order (endianness)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068 DataEncoder encoder(heap_data_sp->GetBytes(), heap_data_sp->GetByteSize(),
1069 m_data.GetByteOrder(), addr_byte_size);
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001070
Kate Stoneb9c1b512016-09-06 20:57:50 +00001071 lldb::offset_t offset = 0;
1072 lldb::offset_t const_offset = 0;
1073 lldb::addr_t const_value = 0;
1074 size_t const_byte_size = 0;
1075 while (m_data.ValidOffset(offset)) {
1076 const uint8_t op = m_data.GetU8(&offset);
1077
1078 bool decoded_data = false;
1079 switch (op) {
1080 case DW_OP_const4u:
1081 // Remember the const offset in case we later have a
Adrian Prantl05097242018-04-30 16:49:04 +00001082 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
Kate Stoneb9c1b512016-09-06 20:57:50 +00001083 const_offset = offset;
1084 const_value = m_data.GetU32(&offset);
1085 decoded_data = true;
1086 const_byte_size = 4;
1087 break;
1088
1089 case DW_OP_const8u:
1090 // Remember the const offset in case we later have a
Adrian Prantl05097242018-04-30 16:49:04 +00001091 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
Kate Stoneb9c1b512016-09-06 20:57:50 +00001092 const_offset = offset;
1093 const_value = m_data.GetU64(&offset);
1094 decoded_data = true;
1095 const_byte_size = 8;
1096 break;
1097
1098 case DW_OP_form_tls_address:
1099 case DW_OP_GNU_push_tls_address:
1100 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
Adrian Prantl05097242018-04-30 16:49:04 +00001101 // by a file address on the stack. We assume that DW_OP_const4u or
1102 // DW_OP_const8u is used for these values, and we check that the last
1103 // opcode we got before either of these was DW_OP_const4u or
1104 // DW_OP_const8u. If so, then we can link the value accodingly. For
1105 // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
1106 // address of a structure that contains a function pointer, the pthread
1107 // key and the offset into the data pointed to by the pthread key. So we
1108 // must link this address and also set the module of this expression to
1109 // the new_module_sp so we can resolve the file address correctly
Kate Stoneb9c1b512016-09-06 20:57:50 +00001110 if (const_byte_size > 0) {
1111 lldb::addr_t linked_file_addr = link_address_callback(const_value);
1112 if (linked_file_addr == LLDB_INVALID_ADDRESS)
1113 return false;
1114 // Replace the address in the new buffer
1115 if (encoder.PutMaxU64(const_offset, const_byte_size,
1116 linked_file_addr) == UINT32_MAX)
1117 return false;
1118 }
1119 break;
1120
1121 default:
1122 const_offset = 0;
1123 const_value = 0;
1124 const_byte_size = 0;
1125 break;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001126 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001127
1128 if (!decoded_data) {
1129 const offset_t op_arg_size = GetOpcodeDataSize(m_data, offset, op);
1130 if (op_arg_size == LLDB_INVALID_OFFSET)
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001131 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001132 else
1133 offset += op_arg_size;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001134 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001135 }
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001136
Kate Stoneb9c1b512016-09-06 20:57:50 +00001137 // If we linked the TLS address correctly, update the module so that when the
Adrian Prantl05097242018-04-30 16:49:04 +00001138 // expression is evaluated it can resolve the file address to a load address
1139 // and read the
Kate Stoneb9c1b512016-09-06 20:57:50 +00001140 // TLS data
1141 m_module_wp = new_module_sp;
1142 m_data.SetData(heap_data_sp);
1143 return true;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001144}
1145
Kate Stoneb9c1b512016-09-06 20:57:50 +00001146bool DWARFExpression::LocationListContainsAddress(
1147 lldb::addr_t loclist_base_addr, lldb::addr_t addr) const {
1148 if (addr == LLDB_INVALID_ADDRESS)
1149 return false;
1150
1151 if (IsLocationList()) {
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001152 lldb::offset_t offset = 0;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001153
Kate Stoneb9c1b512016-09-06 20:57:50 +00001154 if (loclist_base_addr == LLDB_INVALID_ADDRESS)
1155 return false;
1156
1157 while (m_data.ValidOffset(offset)) {
1158 // We need to figure out what the value is for the location.
1159 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1160 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1161 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1162 hi_pc))
1163 break;
1164
1165 if (lo_pc == 0 && hi_pc == 0)
1166 break;
1167
1168 lo_pc += loclist_base_addr - m_loclist_slide;
1169 hi_pc += loclist_base_addr - m_loclist_slide;
1170
1171 if (lo_pc <= addr && addr < hi_pc)
1172 return true;
1173
1174 offset += m_data.GetU16(&offset);
1175 }
1176 }
1177 return false;
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001178}
1179
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180bool DWARFExpression::GetLocation(addr_t base_addr, addr_t pc,
1181 lldb::offset_t &offset,
1182 lldb::offset_t &length) {
1183 offset = 0;
1184 if (!IsLocationList()) {
1185 length = m_data.GetByteSize();
1186 return true;
1187 }
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00001188
Kate Stoneb9c1b512016-09-06 20:57:50 +00001189 if (base_addr != LLDB_INVALID_ADDRESS && pc != LLDB_INVALID_ADDRESS) {
1190 addr_t curr_base_addr = base_addr;
1191
1192 while (m_data.ValidOffset(offset)) {
1193 // We need to figure out what the value is for the location.
1194 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1195 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1196 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset, lo_pc,
1197 hi_pc))
1198 break;
1199
1200 if (lo_pc == 0 && hi_pc == 0)
1201 break;
1202
1203 lo_pc += curr_base_addr - m_loclist_slide;
1204 hi_pc += curr_base_addr - m_loclist_slide;
1205
1206 length = m_data.GetU16(&offset);
1207
1208 if (length > 0 && lo_pc <= pc && pc < hi_pc)
1209 return true;
1210
1211 offset += length;
1212 }
1213 }
1214 offset = LLDB_INVALID_OFFSET;
1215 length = 0;
1216 return false;
1217}
1218
1219bool DWARFExpression::DumpLocationForAddress(Stream *s,
1220 lldb::DescriptionLevel level,
1221 addr_t base_addr, addr_t address,
1222 ABI *abi) {
1223 lldb::offset_t offset = 0;
1224 lldb::offset_t length = 0;
1225
1226 if (GetLocation(base_addr, address, offset, length)) {
1227 if (length > 0) {
1228 DumpLocation(s, offset, length, level, abi);
1229 return true;
1230 }
1231 }
1232 return false;
1233}
1234
1235bool DWARFExpression::Evaluate(ExecutionContextScope *exe_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 lldb::addr_t loclist_base_load_addr,
1237 const Value *initial_value_ptr,
1238 const Value *object_address_ptr, Value &result,
Zachary Turner97206d52017-05-12 04:51:55 +00001239 Status *error_ptr) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001240 ExecutionContext exe_ctx(exe_scope);
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001241 return Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, initial_value_ptr,
1242 object_address_ptr, result, error_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001243}
1244
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001245bool DWARFExpression::Evaluate(ExecutionContext *exe_ctx,
1246 RegisterContext *reg_ctx,
1247 lldb::addr_t loclist_base_load_addr,
1248 const Value *initial_value_ptr,
1249 const Value *object_address_ptr, Value &result,
1250 Status *error_ptr) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001251 ModuleSP module_sp = m_module_wp.lock();
1252
1253 if (IsLocationList()) {
1254 lldb::offset_t offset = 0;
1255 addr_t pc;
1256 StackFrame *frame = NULL;
1257 if (reg_ctx)
1258 pc = reg_ctx->GetPC();
1259 else {
1260 frame = exe_ctx->GetFramePtr();
1261 if (!frame)
1262 return false;
1263 RegisterContextSP reg_ctx_sp = frame->GetRegisterContext();
1264 if (!reg_ctx_sp)
1265 return false;
1266 pc = reg_ctx_sp->GetPC();
1267 }
1268
1269 if (loclist_base_load_addr != LLDB_INVALID_ADDRESS) {
1270 if (pc == LLDB_INVALID_ADDRESS) {
1271 if (error_ptr)
1272 error_ptr->SetErrorString("Invalid PC in frame.");
1273 return false;
1274 }
1275
1276 addr_t curr_loclist_base_load_addr = loclist_base_load_addr;
1277
1278 while (m_data.ValidOffset(offset)) {
1279 // We need to figure out what the value is for the location.
1280 addr_t lo_pc = LLDB_INVALID_ADDRESS;
1281 addr_t hi_pc = LLDB_INVALID_ADDRESS;
1282 if (!AddressRangeForLocationListEntry(m_dwarf_cu, m_data, &offset,
1283 lo_pc, hi_pc))
1284 break;
1285
1286 if (lo_pc == 0 && hi_pc == 0)
1287 break;
1288
1289 lo_pc += curr_loclist_base_load_addr - m_loclist_slide;
1290 hi_pc += curr_loclist_base_load_addr - m_loclist_slide;
1291
1292 uint16_t length = m_data.GetU16(&offset);
1293
1294 if (length > 0 && lo_pc <= pc && pc < hi_pc) {
1295 return DWARFExpression::Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001296 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, offset, length,
1297 m_reg_kind, initial_value_ptr, object_address_ptr, result,
1298 error_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001299 }
1300 offset += length;
1301 }
1302 }
1303 if (error_ptr)
1304 error_ptr->SetErrorString("variable not available");
1305 return false;
1306 }
1307
1308 // Not a location list, just a single expression.
1309 return DWARFExpression::Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001310 exe_ctx, reg_ctx, module_sp, m_data, m_dwarf_cu, 0, m_data.GetByteSize(),
1311 m_reg_kind, initial_value_ptr, object_address_ptr, result, error_ptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312}
1313
1314bool DWARFExpression::Evaluate(
Tamas Berghammerbba2c832017-08-16 11:45:10 +00001315 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
Jan Kratochvilc4d65752018-03-18 20:11:02 +00001317 DWARFUnit *dwarf_cu, const lldb::offset_t opcodes_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001318 const lldb::offset_t opcodes_length, const lldb::RegisterKind reg_kind,
1319 const Value *initial_value_ptr, const Value *object_address_ptr,
Zachary Turner97206d52017-05-12 04:51:55 +00001320 Value &result, Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001321
1322 if (opcodes_length == 0) {
1323 if (error_ptr)
1324 error_ptr->SetErrorString(
1325 "no location, value may have been optimized out");
1326 return false;
1327 }
1328 std::vector<Value> stack;
1329
1330 Process *process = NULL;
1331 StackFrame *frame = NULL;
1332
1333 if (exe_ctx) {
1334 process = exe_ctx->GetProcessPtr();
1335 frame = exe_ctx->GetFramePtr();
1336 }
1337 if (reg_ctx == NULL && frame)
1338 reg_ctx = frame->GetRegisterContext().get();
1339
1340 if (initial_value_ptr)
1341 stack.push_back(*initial_value_ptr);
1342
1343 lldb::offset_t offset = opcodes_offset;
1344 const lldb::offset_t end_offset = opcodes_offset + opcodes_length;
1345 Value tmp;
1346 uint32_t reg_num;
1347
1348 /// Insertion point for evaluating multi-piece expression.
1349 uint64_t op_piece_offset = 0;
1350 Value pieces; // Used for DW_OP_piece
1351
1352 // Make sure all of the data is available in opcodes.
1353 if (!opcodes.ValidOffsetForDataOfSize(opcodes_offset, opcodes_length)) {
1354 if (error_ptr)
1355 error_ptr->SetErrorString(
1356 "invalid offset and/or length for opcodes buffer.");
1357 return false;
1358 }
1359 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1360
1361 while (opcodes.ValidOffset(offset) && offset < end_offset) {
1362 const lldb::offset_t op_offset = offset;
1363 const uint8_t op = opcodes.GetU8(&offset);
1364
1365 if (log && log->GetVerbose()) {
1366 size_t count = stack.size();
1367 log->Printf("Stack before operation has %" PRIu64 " values:",
1368 (uint64_t)count);
1369 for (size_t i = 0; i < count; ++i) {
1370 StreamString new_value;
1371 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1372 stack[i].Dump(&new_value);
1373 log->Printf(" %s", new_value.GetData());
1374 }
1375 log->Printf("0x%8.8" PRIx64 ": %s", op_offset, DW_OP_value_to_name(op));
1376 }
Adrian Prantlb51804e2018-05-03 23:32:47 +00001377
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378 switch (op) {
1379 //----------------------------------------------------------------------
1380 // The DW_OP_addr operation has a single operand that encodes a machine
1381 // address and whose size is the size of an address on the target machine.
1382 //----------------------------------------------------------------------
Adrian Prantlb78a5772018-05-03 20:19:39 +00001383 case DW_OP_addr:
Kate Stoneb9c1b512016-09-06 20:57:50 +00001384 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1385 stack.back().SetValueType(Value::eValueTypeFileAddress);
Adrian Prantlb51804e2018-05-03 23:32:47 +00001386 // Convert the file address to a load address, so subsequent
1387 // DWARF operators can operate on it.
1388 if (frame)
1389 stack.back().ConvertToLoadAddress(module_sp.get(),
1390 frame->CalculateTarget().get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001391 break;
1392
1393 //----------------------------------------------------------------------
1394 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1395 // shared libraries that have a location like:
1396 // DW_OP_addr(0x1000)
Adrian Prantl05097242018-04-30 16:49:04 +00001397 // If this address resides in a shared library, then this virtual address
1398 // won't make sense when it is evaluated in the context of a running
1399 // process where shared libraries have been slid. To account for this, this
1400 // new address type where we can store the section pointer and a 4 byte
1401 // offset.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001402 //----------------------------------------------------------------------
1403 // case DW_OP_addr_sect_offset4:
1404 // {
1405 // result_type = eResultTypeFileAddress;
1406 // lldb::Section *sect = (lldb::Section
1407 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
1408 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1409 //
1410 // Address so_addr (sect, sect_offset);
1411 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
1412 // if (load_addr != LLDB_INVALID_ADDRESS)
1413 // {
1414 // // We successfully resolve a file address to a load
1415 // // address.
1416 // stack.push_back(load_addr);
1417 // break;
1418 // }
1419 // else
1420 // {
1421 // // We were able
1422 // if (error_ptr)
1423 // error_ptr->SetErrorStringWithFormat ("Section %s in
1424 // %s is not currently loaded.\n",
1425 // sect->GetName().AsCString(),
1426 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1427 // return false;
1428 // }
1429 // }
1430 // break;
1431
1432 //----------------------------------------------------------------------
1433 // OPCODE: DW_OP_deref
1434 // OPERANDS: none
1435 // DESCRIPTION: Pops the top stack entry and treats it as an address.
Adrian Prantl05097242018-04-30 16:49:04 +00001436 // The value retrieved from that address is pushed. The size of the data
1437 // retrieved from the dereferenced address is the size of an address on the
1438 // target machine.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001439 //----------------------------------------------------------------------
1440 case DW_OP_deref: {
1441 if (stack.empty()) {
1442 if (error_ptr)
1443 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1444 return false;
1445 }
1446 Value::ValueType value_type = stack.back().GetValueType();
1447 switch (value_type) {
1448 case Value::eValueTypeHostAddress: {
1449 void *src = (void *)stack.back().GetScalar().ULongLong();
1450 intptr_t ptr;
1451 ::memcpy(&ptr, src, sizeof(void *));
1452 stack.back().GetScalar() = ptr;
1453 stack.back().ClearContext();
1454 } break;
1455 case Value::eValueTypeLoadAddress:
1456 if (exe_ctx) {
1457 if (process) {
1458 lldb::addr_t pointer_addr =
1459 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Zachary Turner97206d52017-05-12 04:51:55 +00001460 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001461 lldb::addr_t pointer_value =
1462 process->ReadPointerFromMemory(pointer_addr, error);
1463 if (pointer_value != LLDB_INVALID_ADDRESS) {
1464 stack.back().GetScalar() = pointer_value;
1465 stack.back().ClearContext();
1466 } else {
1467 if (error_ptr)
1468 error_ptr->SetErrorStringWithFormat(
1469 "Failed to dereference pointer from 0x%" PRIx64
1470 " for DW_OP_deref: %s\n",
1471 pointer_addr, error.AsCString());
1472 return false;
1473 }
1474 } else {
1475 if (error_ptr)
1476 error_ptr->SetErrorStringWithFormat(
1477 "NULL process for DW_OP_deref.\n");
1478 return false;
1479 }
1480 } else {
1481 if (error_ptr)
1482 error_ptr->SetErrorStringWithFormat(
1483 "NULL execution context for DW_OP_deref.\n");
1484 return false;
1485 }
1486 break;
1487
1488 default:
1489 break;
1490 }
1491
1492 } break;
1493
1494 //----------------------------------------------------------------------
1495 // OPCODE: DW_OP_deref_size
1496 // OPERANDS: 1
1497 // 1 - uint8_t that specifies the size of the data to dereference.
1498 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1499 // stack entry and treats it as an address. The value retrieved from that
Adrian Prantl05097242018-04-30 16:49:04 +00001500 // address is pushed. In the DW_OP_deref_size operation, however, the size
1501 // in bytes of the data retrieved from the dereferenced address is
Kate Stoneb9c1b512016-09-06 20:57:50 +00001502 // specified by the single operand. This operand is a 1-byte unsigned
1503 // integral constant whose value may not be larger than the size of an
Adrian Prantl05097242018-04-30 16:49:04 +00001504 // address on the target machine. The data retrieved is zero extended to
1505 // the size of an address on the target machine before being pushed on the
1506 // expression stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001507 //----------------------------------------------------------------------
1508 case DW_OP_deref_size: {
1509 if (stack.empty()) {
1510 if (error_ptr)
1511 error_ptr->SetErrorString(
1512 "Expression stack empty for DW_OP_deref_size.");
1513 return false;
1514 }
1515 uint8_t size = opcodes.GetU8(&offset);
1516 Value::ValueType value_type = stack.back().GetValueType();
1517 switch (value_type) {
1518 case Value::eValueTypeHostAddress: {
1519 void *src = (void *)stack.back().GetScalar().ULongLong();
1520 intptr_t ptr;
1521 ::memcpy(&ptr, src, sizeof(void *));
1522 // I can't decide whether the size operand should apply to the bytes in
1523 // their
1524 // lldb-host endianness or the target endianness.. I doubt this'll ever
Adrian Prantl05097242018-04-30 16:49:04 +00001525 // come up but I'll opt for assuming big endian regardless.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526 switch (size) {
1527 case 1:
1528 ptr = ptr & 0xff;
1529 break;
1530 case 2:
1531 ptr = ptr & 0xffff;
1532 break;
1533 case 3:
1534 ptr = ptr & 0xffffff;
1535 break;
1536 case 4:
1537 ptr = ptr & 0xffffffff;
1538 break;
1539 // the casts are added to work around the case where intptr_t is a 32
1540 // bit quantity;
1541 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1542 // program.
1543 case 5:
1544 ptr = (intptr_t)ptr & 0xffffffffffULL;
1545 break;
1546 case 6:
1547 ptr = (intptr_t)ptr & 0xffffffffffffULL;
1548 break;
1549 case 7:
1550 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1551 break;
1552 default:
1553 break;
1554 }
1555 stack.back().GetScalar() = ptr;
1556 stack.back().ClearContext();
1557 } break;
1558 case Value::eValueTypeLoadAddress:
1559 if (exe_ctx) {
1560 if (process) {
1561 lldb::addr_t pointer_addr =
1562 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1563 uint8_t addr_bytes[sizeof(lldb::addr_t)];
Zachary Turner97206d52017-05-12 04:51:55 +00001564 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001565 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1566 size) {
1567 DataExtractor addr_data(addr_bytes, sizeof(addr_bytes),
1568 process->GetByteOrder(), size);
1569 lldb::offset_t addr_data_offset = 0;
1570 switch (size) {
1571 case 1:
1572 stack.back().GetScalar() = addr_data.GetU8(&addr_data_offset);
1573 break;
1574 case 2:
1575 stack.back().GetScalar() = addr_data.GetU16(&addr_data_offset);
1576 break;
1577 case 4:
1578 stack.back().GetScalar() = addr_data.GetU32(&addr_data_offset);
1579 break;
1580 case 8:
1581 stack.back().GetScalar() = addr_data.GetU64(&addr_data_offset);
1582 break;
1583 default:
1584 stack.back().GetScalar() =
1585 addr_data.GetPointer(&addr_data_offset);
1586 }
1587 stack.back().ClearContext();
1588 } else {
1589 if (error_ptr)
1590 error_ptr->SetErrorStringWithFormat(
1591 "Failed to dereference pointer from 0x%" PRIx64
1592 " for DW_OP_deref: %s\n",
1593 pointer_addr, error.AsCString());
1594 return false;
1595 }
1596 } else {
1597 if (error_ptr)
1598 error_ptr->SetErrorStringWithFormat(
1599 "NULL process for DW_OP_deref.\n");
1600 return false;
1601 }
1602 } else {
1603 if (error_ptr)
1604 error_ptr->SetErrorStringWithFormat(
1605 "NULL execution context for DW_OP_deref.\n");
1606 return false;
1607 }
1608 break;
1609
1610 default:
1611 break;
1612 }
1613
1614 } break;
1615
1616 //----------------------------------------------------------------------
1617 // OPCODE: DW_OP_xderef_size
1618 // OPERANDS: 1
1619 // 1 - uint8_t that specifies the size of the data to dereference.
1620 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
Adrian Prantl05097242018-04-30 16:49:04 +00001621 // the top of the stack is treated as an address. The second stack entry is
1622 // treated as an "address space identifier" for those architectures that
1623 // support multiple address spaces. The top two stack elements are popped,
1624 // a data item is retrieved through an implementation-defined address
1625 // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1626 // operation, however, the size in bytes of the data retrieved from the
1627 // dereferenced address is specified by the single operand. This operand is
1628 // a 1-byte unsigned integral constant whose value may not be larger than
1629 // the size of an address on the target machine. The data retrieved is zero
1630 // extended to the size of an address on the target machine before being
1631 // pushed on the expression stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001632 //----------------------------------------------------------------------
1633 case DW_OP_xderef_size:
1634 if (error_ptr)
1635 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1636 return false;
1637 //----------------------------------------------------------------------
1638 // OPCODE: DW_OP_xderef
1639 // OPERANDS: none
1640 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
Adrian Prantl05097242018-04-30 16:49:04 +00001641 // the top of the stack is treated as an address. The second stack entry is
1642 // treated as an "address space identifier" for those architectures that
1643 // support multiple address spaces. The top two stack elements are popped,
1644 // a data item is retrieved through an implementation-defined address
1645 // calculation and pushed as the new stack top. The size of the data
1646 // retrieved from the dereferenced address is the size of an address on the
1647 // target machine.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001648 //----------------------------------------------------------------------
1649 case DW_OP_xderef:
1650 if (error_ptr)
1651 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1652 return false;
1653
1654 //----------------------------------------------------------------------
1655 // All DW_OP_constXXX opcodes have a single operand as noted below:
1656 //
1657 // Opcode Operand 1
1658 // --------------- ----------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +00001659 // DW_OP_const1u 1-byte unsigned integer constant DW_OP_const1s
1660 // 1-byte signed integer constant DW_OP_const2u 2-byte unsigned integer
1661 // constant DW_OP_const2s 2-byte signed integer constant DW_OP_const4u
1662 // 4-byte unsigned integer constant DW_OP_const4s 4-byte signed integer
1663 // constant DW_OP_const8u 8-byte unsigned integer constant DW_OP_const8s
1664 // 8-byte signed integer constant DW_OP_constu unsigned LEB128 integer
1665 // constant DW_OP_consts signed LEB128 integer constant
Kate Stoneb9c1b512016-09-06 20:57:50 +00001666 //----------------------------------------------------------------------
1667 case DW_OP_const1u:
1668 stack.push_back(Scalar((uint8_t)opcodes.GetU8(&offset)));
1669 break;
1670 case DW_OP_const1s:
1671 stack.push_back(Scalar((int8_t)opcodes.GetU8(&offset)));
1672 break;
1673 case DW_OP_const2u:
1674 stack.push_back(Scalar((uint16_t)opcodes.GetU16(&offset)));
1675 break;
1676 case DW_OP_const2s:
1677 stack.push_back(Scalar((int16_t)opcodes.GetU16(&offset)));
1678 break;
1679 case DW_OP_const4u:
1680 stack.push_back(Scalar((uint32_t)opcodes.GetU32(&offset)));
1681 break;
1682 case DW_OP_const4s:
1683 stack.push_back(Scalar((int32_t)opcodes.GetU32(&offset)));
1684 break;
1685 case DW_OP_const8u:
1686 stack.push_back(Scalar((uint64_t)opcodes.GetU64(&offset)));
1687 break;
1688 case DW_OP_const8s:
1689 stack.push_back(Scalar((int64_t)opcodes.GetU64(&offset)));
1690 break;
1691 case DW_OP_constu:
1692 stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1693 break;
1694 case DW_OP_consts:
1695 stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1696 break;
1697
1698 //----------------------------------------------------------------------
1699 // OPCODE: DW_OP_dup
1700 // OPERANDS: none
1701 // DESCRIPTION: duplicates the value at the top of the stack
1702 //----------------------------------------------------------------------
1703 case DW_OP_dup:
1704 if (stack.empty()) {
1705 if (error_ptr)
1706 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1707 return false;
1708 } else
1709 stack.push_back(stack.back());
1710 break;
1711
1712 //----------------------------------------------------------------------
1713 // OPCODE: DW_OP_drop
1714 // OPERANDS: none
1715 // DESCRIPTION: pops the value at the top of the stack
1716 //----------------------------------------------------------------------
1717 case DW_OP_drop:
1718 if (stack.empty()) {
1719 if (error_ptr)
1720 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1721 return false;
1722 } else
1723 stack.pop_back();
1724 break;
1725
1726 //----------------------------------------------------------------------
1727 // OPCODE: DW_OP_over
1728 // OPERANDS: none
1729 // DESCRIPTION: Duplicates the entry currently second in the stack at
1730 // the top of the stack.
1731 //----------------------------------------------------------------------
1732 case DW_OP_over:
1733 if (stack.size() < 2) {
1734 if (error_ptr)
1735 error_ptr->SetErrorString(
1736 "Expression stack needs at least 2 items for DW_OP_over.");
1737 return false;
1738 } else
1739 stack.push_back(stack[stack.size() - 2]);
1740 break;
1741
1742 //----------------------------------------------------------------------
1743 // OPCODE: DW_OP_pick
1744 // OPERANDS: uint8_t index into the current stack
1745 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1746 // inclusive) is pushed on the stack
1747 //----------------------------------------------------------------------
1748 case DW_OP_pick: {
1749 uint8_t pick_idx = opcodes.GetU8(&offset);
1750 if (pick_idx < stack.size())
1751 stack.push_back(stack[pick_idx]);
1752 else {
1753 if (error_ptr)
1754 error_ptr->SetErrorStringWithFormat(
1755 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1756 return false;
1757 }
1758 } break;
1759
1760 //----------------------------------------------------------------------
1761 // OPCODE: DW_OP_swap
1762 // OPERANDS: none
1763 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1764 // of the stack becomes the second stack entry, and the second entry
1765 // becomes the top of the stack
1766 //----------------------------------------------------------------------
1767 case DW_OP_swap:
1768 if (stack.size() < 2) {
1769 if (error_ptr)
1770 error_ptr->SetErrorString(
1771 "Expression stack needs at least 2 items for DW_OP_swap.");
1772 return false;
1773 } else {
1774 tmp = stack.back();
1775 stack.back() = stack[stack.size() - 2];
1776 stack[stack.size() - 2] = tmp;
1777 }
1778 break;
1779
1780 //----------------------------------------------------------------------
1781 // OPCODE: DW_OP_rot
1782 // OPERANDS: none
1783 // DESCRIPTION: Rotates the first three stack entries. The entry at
Adrian Prantl05097242018-04-30 16:49:04 +00001784 // the top of the stack becomes the third stack entry, the second entry
1785 // becomes the top of the stack, and the third entry becomes the second
1786 // entry.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001787 //----------------------------------------------------------------------
1788 case DW_OP_rot:
1789 if (stack.size() < 3) {
1790 if (error_ptr)
1791 error_ptr->SetErrorString(
1792 "Expression stack needs at least 3 items for DW_OP_rot.");
1793 return false;
1794 } else {
1795 size_t last_idx = stack.size() - 1;
1796 Value old_top = stack[last_idx];
1797 stack[last_idx] = stack[last_idx - 1];
1798 stack[last_idx - 1] = stack[last_idx - 2];
1799 stack[last_idx - 2] = old_top;
1800 }
1801 break;
1802
1803 //----------------------------------------------------------------------
1804 // OPCODE: DW_OP_abs
1805 // OPERANDS: none
1806 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1807 // value and pushes its absolute value. If the absolute value can not be
1808 // represented, the result is undefined.
1809 //----------------------------------------------------------------------
1810 case DW_OP_abs:
1811 if (stack.empty()) {
1812 if (error_ptr)
1813 error_ptr->SetErrorString(
1814 "Expression stack needs at least 1 item for DW_OP_abs.");
1815 return false;
1816 } else if (stack.back().ResolveValue(exe_ctx).AbsoluteValue() == false) {
1817 if (error_ptr)
1818 error_ptr->SetErrorString(
1819 "Failed to take the absolute value of the first stack item.");
1820 return false;
1821 }
1822 break;
1823
1824 //----------------------------------------------------------------------
1825 // OPCODE: DW_OP_and
1826 // OPERANDS: none
1827 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1828 // operation on the two, and pushes the result.
1829 //----------------------------------------------------------------------
1830 case DW_OP_and:
1831 if (stack.size() < 2) {
1832 if (error_ptr)
1833 error_ptr->SetErrorString(
1834 "Expression stack needs at least 2 items for DW_OP_and.");
1835 return false;
1836 } else {
1837 tmp = stack.back();
1838 stack.pop_back();
1839 stack.back().ResolveValue(exe_ctx) =
1840 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1841 }
1842 break;
1843
1844 //----------------------------------------------------------------------
1845 // OPCODE: DW_OP_div
1846 // OPERANDS: none
1847 // DESCRIPTION: pops the top two stack values, divides the former second
Adrian Prantl05097242018-04-30 16:49:04 +00001848 // entry by the former top of the stack using signed division, and pushes
1849 // the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001850 //----------------------------------------------------------------------
1851 case DW_OP_div:
1852 if (stack.size() < 2) {
1853 if (error_ptr)
1854 error_ptr->SetErrorString(
1855 "Expression stack needs at least 2 items for DW_OP_div.");
1856 return false;
1857 } else {
1858 tmp = stack.back();
1859 if (tmp.ResolveValue(exe_ctx).IsZero()) {
1860 if (error_ptr)
1861 error_ptr->SetErrorString("Divide by zero.");
1862 return false;
1863 } else {
1864 stack.pop_back();
1865 stack.back() =
1866 stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx);
1867 if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1868 if (error_ptr)
1869 error_ptr->SetErrorString("Divide failed.");
1870 return false;
1871 }
1872 }
1873 }
1874 break;
1875
1876 //----------------------------------------------------------------------
1877 // OPCODE: DW_OP_minus
1878 // OPERANDS: none
1879 // DESCRIPTION: pops the top two stack values, subtracts the former top
1880 // of the stack from the former second entry, and pushes the result.
1881 //----------------------------------------------------------------------
1882 case DW_OP_minus:
1883 if (stack.size() < 2) {
1884 if (error_ptr)
1885 error_ptr->SetErrorString(
1886 "Expression stack needs at least 2 items for DW_OP_minus.");
1887 return false;
1888 } else {
1889 tmp = stack.back();
1890 stack.pop_back();
1891 stack.back().ResolveValue(exe_ctx) =
1892 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1893 }
1894 break;
1895
1896 //----------------------------------------------------------------------
1897 // OPCODE: DW_OP_mod
1898 // OPERANDS: none
1899 // DESCRIPTION: pops the top two stack values and pushes the result of
Adrian Prantl05097242018-04-30 16:49:04 +00001900 // the calculation: former second stack entry modulo the former top of the
1901 // stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001902 //----------------------------------------------------------------------
1903 case DW_OP_mod:
1904 if (stack.size() < 2) {
1905 if (error_ptr)
1906 error_ptr->SetErrorString(
1907 "Expression stack needs at least 2 items for DW_OP_mod.");
1908 return false;
1909 } else {
1910 tmp = stack.back();
1911 stack.pop_back();
1912 stack.back().ResolveValue(exe_ctx) =
1913 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1914 }
1915 break;
1916
1917 //----------------------------------------------------------------------
1918 // OPCODE: DW_OP_mul
1919 // OPERANDS: none
1920 // DESCRIPTION: pops the top two stack entries, multiplies them
1921 // together, and pushes the result.
1922 //----------------------------------------------------------------------
1923 case DW_OP_mul:
1924 if (stack.size() < 2) {
1925 if (error_ptr)
1926 error_ptr->SetErrorString(
1927 "Expression stack needs at least 2 items for DW_OP_mul.");
1928 return false;
1929 } else {
1930 tmp = stack.back();
1931 stack.pop_back();
1932 stack.back().ResolveValue(exe_ctx) =
1933 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1934 }
1935 break;
1936
1937 //----------------------------------------------------------------------
1938 // OPCODE: DW_OP_neg
1939 // OPERANDS: none
1940 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1941 //----------------------------------------------------------------------
1942 case DW_OP_neg:
1943 if (stack.empty()) {
1944 if (error_ptr)
1945 error_ptr->SetErrorString(
1946 "Expression stack needs at least 1 item for DW_OP_neg.");
1947 return false;
1948 } else {
1949 if (stack.back().ResolveValue(exe_ctx).UnaryNegate() == false) {
1950 if (error_ptr)
1951 error_ptr->SetErrorString("Unary negate failed.");
1952 return false;
1953 }
1954 }
1955 break;
1956
1957 //----------------------------------------------------------------------
1958 // OPCODE: DW_OP_not
1959 // OPERANDS: none
1960 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1961 // complement
1962 //----------------------------------------------------------------------
1963 case DW_OP_not:
1964 if (stack.empty()) {
1965 if (error_ptr)
1966 error_ptr->SetErrorString(
1967 "Expression stack needs at least 1 item for DW_OP_not.");
1968 return false;
1969 } else {
1970 if (stack.back().ResolveValue(exe_ctx).OnesComplement() == false) {
1971 if (error_ptr)
1972 error_ptr->SetErrorString("Logical NOT failed.");
1973 return false;
1974 }
1975 }
1976 break;
1977
1978 //----------------------------------------------------------------------
1979 // OPCODE: DW_OP_or
1980 // OPERANDS: none
1981 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1982 // operation on the two, and pushes the result.
1983 //----------------------------------------------------------------------
1984 case DW_OP_or:
1985 if (stack.size() < 2) {
1986 if (error_ptr)
1987 error_ptr->SetErrorString(
1988 "Expression stack needs at least 2 items for DW_OP_or.");
1989 return false;
1990 } else {
1991 tmp = stack.back();
1992 stack.pop_back();
1993 stack.back().ResolveValue(exe_ctx) =
1994 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
1995 }
1996 break;
1997
1998 //----------------------------------------------------------------------
1999 // OPCODE: DW_OP_plus
2000 // OPERANDS: none
2001 // DESCRIPTION: pops the top two stack entries, adds them together, and
2002 // pushes the result.
2003 //----------------------------------------------------------------------
2004 case DW_OP_plus:
2005 if (stack.size() < 2) {
2006 if (error_ptr)
2007 error_ptr->SetErrorString(
2008 "Expression stack needs at least 2 items for DW_OP_plus.");
2009 return false;
2010 } else {
2011 tmp = stack.back();
2012 stack.pop_back();
2013 stack.back().GetScalar() += tmp.GetScalar();
2014 }
2015 break;
2016
2017 //----------------------------------------------------------------------
2018 // OPCODE: DW_OP_plus_uconst
2019 // OPERANDS: none
2020 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
2021 // constant operand and pushes the result.
2022 //----------------------------------------------------------------------
2023 case DW_OP_plus_uconst:
2024 if (stack.empty()) {
2025 if (error_ptr)
2026 error_ptr->SetErrorString(
2027 "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
2028 return false;
2029 } else {
2030 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
2031 // Implicit conversion from a UINT to a Scalar...
2032 stack.back().GetScalar() += uconst_value;
2033 if (!stack.back().GetScalar().IsValid()) {
2034 if (error_ptr)
2035 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
2036 return false;
2037 }
2038 }
2039 break;
2040
2041 //----------------------------------------------------------------------
2042 // OPCODE: DW_OP_shl
2043 // OPERANDS: none
2044 // DESCRIPTION: pops the top two stack entries, shifts the former
Adrian Prantl05097242018-04-30 16:49:04 +00002045 // second entry left by the number of bits specified by the former top of
2046 // the stack, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002047 //----------------------------------------------------------------------
2048 case DW_OP_shl:
2049 if (stack.size() < 2) {
2050 if (error_ptr)
2051 error_ptr->SetErrorString(
2052 "Expression stack needs at least 2 items for DW_OP_shl.");
2053 return false;
2054 } else {
2055 tmp = stack.back();
2056 stack.pop_back();
2057 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
2058 }
2059 break;
2060
2061 //----------------------------------------------------------------------
2062 // OPCODE: DW_OP_shr
2063 // OPERANDS: none
2064 // DESCRIPTION: pops the top two stack entries, shifts the former second
2065 // entry right logically (filling with zero bits) by the number of bits
2066 // specified by the former top of the stack, and pushes the result.
2067 //----------------------------------------------------------------------
2068 case DW_OP_shr:
2069 if (stack.size() < 2) {
2070 if (error_ptr)
2071 error_ptr->SetErrorString(
2072 "Expression stack needs at least 2 items for DW_OP_shr.");
2073 return false;
2074 } else {
2075 tmp = stack.back();
2076 stack.pop_back();
2077 if (stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
2078 tmp.ResolveValue(exe_ctx)) == false) {
2079 if (error_ptr)
2080 error_ptr->SetErrorString("DW_OP_shr failed.");
2081 return false;
2082 }
2083 }
2084 break;
2085
2086 //----------------------------------------------------------------------
2087 // OPCODE: DW_OP_shra
2088 // OPERANDS: none
2089 // DESCRIPTION: pops the top two stack entries, shifts the former second
2090 // entry right arithmetically (divide the magnitude by 2, keep the same
Adrian Prantl05097242018-04-30 16:49:04 +00002091 // sign for the result) by the number of bits specified by the former top
2092 // of the stack, and pushes the result.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002093 //----------------------------------------------------------------------
2094 case DW_OP_shra:
2095 if (stack.size() < 2) {
2096 if (error_ptr)
2097 error_ptr->SetErrorString(
2098 "Expression stack needs at least 2 items for DW_OP_shra.");
2099 return false;
2100 } else {
2101 tmp = stack.back();
2102 stack.pop_back();
2103 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
2104 }
2105 break;
2106
2107 //----------------------------------------------------------------------
2108 // OPCODE: DW_OP_xor
2109 // OPERANDS: none
2110 // DESCRIPTION: pops the top two stack entries, performs the bitwise
2111 // exclusive-or operation on the two, and pushes the result.
2112 //----------------------------------------------------------------------
2113 case DW_OP_xor:
2114 if (stack.size() < 2) {
2115 if (error_ptr)
2116 error_ptr->SetErrorString(
2117 "Expression stack needs at least 2 items for DW_OP_xor.");
2118 return false;
2119 } else {
2120 tmp = stack.back();
2121 stack.pop_back();
2122 stack.back().ResolveValue(exe_ctx) =
2123 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
2124 }
2125 break;
2126
2127 //----------------------------------------------------------------------
2128 // OPCODE: DW_OP_skip
2129 // OPERANDS: int16_t
2130 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
Adrian Prantl05097242018-04-30 16:49:04 +00002131 // signed integer constant. The 2-byte constant is the number of bytes of
2132 // the DWARF expression to skip forward or backward from the current
Kate Stoneb9c1b512016-09-06 20:57:50 +00002133 // operation, beginning after the 2-byte constant.
2134 //----------------------------------------------------------------------
2135 case DW_OP_skip: {
2136 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
2137 lldb::offset_t new_offset = offset + skip_offset;
2138 if (new_offset >= opcodes_offset && new_offset < end_offset)
2139 offset = new_offset;
2140 else {
2141 if (error_ptr)
2142 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_skip.");
2143 return false;
2144 }
2145 } break;
2146
2147 //----------------------------------------------------------------------
2148 // OPCODE: DW_OP_bra
2149 // OPERANDS: int16_t
2150 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
Adrian Prantl05097242018-04-30 16:49:04 +00002151 // signed integer constant. This operation pops the top of stack. If the
2152 // value popped is not the constant 0, the 2-byte constant operand is the
2153 // number of bytes of the DWARF expression to skip forward or backward from
2154 // the current operation, beginning after the 2-byte constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002155 //----------------------------------------------------------------------
2156 case DW_OP_bra:
2157 if (stack.empty()) {
2158 if (error_ptr)
2159 error_ptr->SetErrorString(
2160 "Expression stack needs at least 1 item for DW_OP_bra.");
2161 return false;
2162 } else {
2163 tmp = stack.back();
2164 stack.pop_back();
2165 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
2166 Scalar zero(0);
2167 if (tmp.ResolveValue(exe_ctx) != zero) {
2168 lldb::offset_t new_offset = offset + bra_offset;
2169 if (new_offset >= opcodes_offset && new_offset < end_offset)
2170 offset = new_offset;
2171 else {
2172 if (error_ptr)
2173 error_ptr->SetErrorString("Invalid opcode offset in DW_OP_bra.");
2174 return false;
2175 }
2176 }
2177 }
2178 break;
2179
2180 //----------------------------------------------------------------------
2181 // OPCODE: DW_OP_eq
2182 // OPERANDS: none
2183 // DESCRIPTION: pops the top two stack values, compares using the
2184 // equals (==) operator.
2185 // STACK RESULT: push the constant value 1 onto the stack if the result
2186 // of the operation is true or the constant value 0 if the result of the
2187 // operation is false.
2188 //----------------------------------------------------------------------
2189 case DW_OP_eq:
2190 if (stack.size() < 2) {
2191 if (error_ptr)
2192 error_ptr->SetErrorString(
2193 "Expression stack needs at least 2 items for DW_OP_eq.");
2194 return false;
2195 } else {
2196 tmp = stack.back();
2197 stack.pop_back();
2198 stack.back().ResolveValue(exe_ctx) =
2199 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
2200 }
2201 break;
2202
2203 //----------------------------------------------------------------------
2204 // OPCODE: DW_OP_ge
2205 // OPERANDS: none
2206 // DESCRIPTION: pops the top two stack values, compares using the
2207 // greater than or equal to (>=) operator.
2208 // STACK RESULT: push the constant value 1 onto the stack if the result
2209 // of the operation is true or the constant value 0 if the result of the
2210 // operation is false.
2211 //----------------------------------------------------------------------
2212 case DW_OP_ge:
2213 if (stack.size() < 2) {
2214 if (error_ptr)
2215 error_ptr->SetErrorString(
2216 "Expression stack needs at least 2 items for DW_OP_ge.");
2217 return false;
2218 } else {
2219 tmp = stack.back();
2220 stack.pop_back();
2221 stack.back().ResolveValue(exe_ctx) =
2222 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
2223 }
2224 break;
2225
2226 //----------------------------------------------------------------------
2227 // OPCODE: DW_OP_gt
2228 // OPERANDS: none
2229 // DESCRIPTION: pops the top two stack values, compares using the
2230 // greater than (>) operator.
2231 // STACK RESULT: push the constant value 1 onto the stack if the result
2232 // of the operation is true or the constant value 0 if the result of the
2233 // operation is false.
2234 //----------------------------------------------------------------------
2235 case DW_OP_gt:
2236 if (stack.size() < 2) {
2237 if (error_ptr)
2238 error_ptr->SetErrorString(
2239 "Expression stack needs at least 2 items for DW_OP_gt.");
2240 return false;
2241 } else {
2242 tmp = stack.back();
2243 stack.pop_back();
2244 stack.back().ResolveValue(exe_ctx) =
2245 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
2246 }
2247 break;
2248
2249 //----------------------------------------------------------------------
2250 // OPCODE: DW_OP_le
2251 // OPERANDS: none
2252 // DESCRIPTION: pops the top two stack values, compares using the
2253 // less than or equal to (<=) operator.
2254 // STACK RESULT: push the constant value 1 onto the stack if the result
2255 // of the operation is true or the constant value 0 if the result of the
2256 // operation is false.
2257 //----------------------------------------------------------------------
2258 case DW_OP_le:
2259 if (stack.size() < 2) {
2260 if (error_ptr)
2261 error_ptr->SetErrorString(
2262 "Expression stack needs at least 2 items for DW_OP_le.");
2263 return false;
2264 } else {
2265 tmp = stack.back();
2266 stack.pop_back();
2267 stack.back().ResolveValue(exe_ctx) =
2268 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
2269 }
2270 break;
2271
2272 //----------------------------------------------------------------------
2273 // OPCODE: DW_OP_lt
2274 // OPERANDS: none
2275 // DESCRIPTION: pops the top two stack values, compares using the
2276 // less than (<) operator.
2277 // STACK RESULT: push the constant value 1 onto the stack if the result
2278 // of the operation is true or the constant value 0 if the result of the
2279 // operation is false.
2280 //----------------------------------------------------------------------
2281 case DW_OP_lt:
2282 if (stack.size() < 2) {
2283 if (error_ptr)
2284 error_ptr->SetErrorString(
2285 "Expression stack needs at least 2 items for DW_OP_lt.");
2286 return false;
2287 } else {
2288 tmp = stack.back();
2289 stack.pop_back();
2290 stack.back().ResolveValue(exe_ctx) =
2291 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
2292 }
2293 break;
2294
2295 //----------------------------------------------------------------------
2296 // OPCODE: DW_OP_ne
2297 // OPERANDS: none
2298 // DESCRIPTION: pops the top two stack values, compares using the
2299 // not equal (!=) operator.
2300 // STACK RESULT: push the constant value 1 onto the stack if the result
2301 // of the operation is true or the constant value 0 if the result of the
2302 // operation is false.
2303 //----------------------------------------------------------------------
2304 case DW_OP_ne:
2305 if (stack.size() < 2) {
2306 if (error_ptr)
2307 error_ptr->SetErrorString(
2308 "Expression stack needs at least 2 items for DW_OP_ne.");
2309 return false;
2310 } else {
2311 tmp = stack.back();
2312 stack.pop_back();
2313 stack.back().ResolveValue(exe_ctx) =
2314 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
2315 }
2316 break;
2317
2318 //----------------------------------------------------------------------
2319 // OPCODE: DW_OP_litn
2320 // OPERANDS: none
2321 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
2322 // STACK RESULT: push the unsigned literal constant value onto the top
2323 // of the stack.
2324 //----------------------------------------------------------------------
2325 case DW_OP_lit0:
2326 case DW_OP_lit1:
2327 case DW_OP_lit2:
2328 case DW_OP_lit3:
2329 case DW_OP_lit4:
2330 case DW_OP_lit5:
2331 case DW_OP_lit6:
2332 case DW_OP_lit7:
2333 case DW_OP_lit8:
2334 case DW_OP_lit9:
2335 case DW_OP_lit10:
2336 case DW_OP_lit11:
2337 case DW_OP_lit12:
2338 case DW_OP_lit13:
2339 case DW_OP_lit14:
2340 case DW_OP_lit15:
2341 case DW_OP_lit16:
2342 case DW_OP_lit17:
2343 case DW_OP_lit18:
2344 case DW_OP_lit19:
2345 case DW_OP_lit20:
2346 case DW_OP_lit21:
2347 case DW_OP_lit22:
2348 case DW_OP_lit23:
2349 case DW_OP_lit24:
2350 case DW_OP_lit25:
2351 case DW_OP_lit26:
2352 case DW_OP_lit27:
2353 case DW_OP_lit28:
2354 case DW_OP_lit29:
2355 case DW_OP_lit30:
2356 case DW_OP_lit31:
2357 stack.push_back(Scalar(op - DW_OP_lit0));
2358 break;
2359
2360 //----------------------------------------------------------------------
2361 // OPCODE: DW_OP_regN
2362 // OPERANDS: none
2363 // DESCRIPTION: Push the value in register n on the top of the stack.
2364 //----------------------------------------------------------------------
2365 case DW_OP_reg0:
2366 case DW_OP_reg1:
2367 case DW_OP_reg2:
2368 case DW_OP_reg3:
2369 case DW_OP_reg4:
2370 case DW_OP_reg5:
2371 case DW_OP_reg6:
2372 case DW_OP_reg7:
2373 case DW_OP_reg8:
2374 case DW_OP_reg9:
2375 case DW_OP_reg10:
2376 case DW_OP_reg11:
2377 case DW_OP_reg12:
2378 case DW_OP_reg13:
2379 case DW_OP_reg14:
2380 case DW_OP_reg15:
2381 case DW_OP_reg16:
2382 case DW_OP_reg17:
2383 case DW_OP_reg18:
2384 case DW_OP_reg19:
2385 case DW_OP_reg20:
2386 case DW_OP_reg21:
2387 case DW_OP_reg22:
2388 case DW_OP_reg23:
2389 case DW_OP_reg24:
2390 case DW_OP_reg25:
2391 case DW_OP_reg26:
2392 case DW_OP_reg27:
2393 case DW_OP_reg28:
2394 case DW_OP_reg29:
2395 case DW_OP_reg30:
2396 case DW_OP_reg31: {
2397 reg_num = op - DW_OP_reg0;
2398
2399 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2400 stack.push_back(tmp);
2401 else
2402 return false;
2403 } break;
2404 //----------------------------------------------------------------------
2405 // OPCODE: DW_OP_regx
2406 // OPERANDS:
2407 // ULEB128 literal operand that encodes the register.
2408 // DESCRIPTION: Push the value in register on the top of the stack.
2409 //----------------------------------------------------------------------
2410 case DW_OP_regx: {
2411 reg_num = opcodes.GetULEB128(&offset);
2412 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
2413 stack.push_back(tmp);
2414 else
2415 return false;
2416 } break;
2417
2418 //----------------------------------------------------------------------
2419 // OPCODE: DW_OP_bregN
2420 // OPERANDS:
2421 // SLEB128 offset from register N
2422 // DESCRIPTION: Value is in memory at the address specified by register
2423 // N plus an offset.
2424 //----------------------------------------------------------------------
2425 case DW_OP_breg0:
2426 case DW_OP_breg1:
2427 case DW_OP_breg2:
2428 case DW_OP_breg3:
2429 case DW_OP_breg4:
2430 case DW_OP_breg5:
2431 case DW_OP_breg6:
2432 case DW_OP_breg7:
2433 case DW_OP_breg8:
2434 case DW_OP_breg9:
2435 case DW_OP_breg10:
2436 case DW_OP_breg11:
2437 case DW_OP_breg12:
2438 case DW_OP_breg13:
2439 case DW_OP_breg14:
2440 case DW_OP_breg15:
2441 case DW_OP_breg16:
2442 case DW_OP_breg17:
2443 case DW_OP_breg18:
2444 case DW_OP_breg19:
2445 case DW_OP_breg20:
2446 case DW_OP_breg21:
2447 case DW_OP_breg22:
2448 case DW_OP_breg23:
2449 case DW_OP_breg24:
2450 case DW_OP_breg25:
2451 case DW_OP_breg26:
2452 case DW_OP_breg27:
2453 case DW_OP_breg28:
2454 case DW_OP_breg29:
2455 case DW_OP_breg30:
2456 case DW_OP_breg31: {
2457 reg_num = op - DW_OP_breg0;
2458
2459 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2460 tmp)) {
2461 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2462 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2463 tmp.ClearContext();
2464 stack.push_back(tmp);
2465 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2466 } else
2467 return false;
2468 } break;
2469 //----------------------------------------------------------------------
2470 // OPCODE: DW_OP_bregx
2471 // OPERANDS: 2
2472 // ULEB128 literal operand that encodes the register.
2473 // SLEB128 offset from register N
2474 // DESCRIPTION: Value is in memory at the address specified by register
2475 // N plus an offset.
2476 //----------------------------------------------------------------------
2477 case DW_OP_bregx: {
2478 reg_num = opcodes.GetULEB128(&offset);
2479
2480 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2481 tmp)) {
2482 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2483 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2484 tmp.ClearContext();
2485 stack.push_back(tmp);
2486 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2487 } else
2488 return false;
2489 } break;
2490
2491 case DW_OP_fbreg:
2492 if (exe_ctx) {
2493 if (frame) {
2494 Scalar value;
2495 if (frame->GetFrameBaseValue(value, error_ptr)) {
2496 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2497 value += fbreg_offset;
2498 stack.push_back(value);
2499 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2500 } else
2501 return false;
2502 } else {
2503 if (error_ptr)
2504 error_ptr->SetErrorString(
2505 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2506 return false;
2507 }
2508 } else {
2509 if (error_ptr)
2510 error_ptr->SetErrorStringWithFormat(
2511 "NULL execution context for DW_OP_fbreg.\n");
2512 return false;
2513 }
2514
2515 break;
2516
2517 //----------------------------------------------------------------------
2518 // OPCODE: DW_OP_nop
2519 // OPERANDS: none
2520 // DESCRIPTION: A place holder. It has no effect on the location stack
2521 // or any of its values.
2522 //----------------------------------------------------------------------
2523 case DW_OP_nop:
2524 break;
2525
2526 //----------------------------------------------------------------------
2527 // OPCODE: DW_OP_piece
2528 // OPERANDS: 1
2529 // ULEB128: byte size of the piece
2530 // DESCRIPTION: The operand describes the size in bytes of the piece of
Adrian Prantl05097242018-04-30 16:49:04 +00002531 // the object referenced by the DWARF expression whose result is at the top
2532 // of the stack. If the piece is located in a register, but does not occupy
2533 // the entire register, the placement of the piece within that register is
2534 // defined by the ABI.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002535 //
Adrian Prantl05097242018-04-30 16:49:04 +00002536 // Many compilers store a single variable in sets of registers, or store a
2537 // variable partially in memory and partially in registers. DW_OP_piece
2538 // provides a way of describing how large a part of a variable a particular
2539 // DWARF expression refers to.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002540 //----------------------------------------------------------------------
2541 case DW_OP_piece: {
2542 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2543
2544 if (piece_byte_size > 0) {
2545 Value curr_piece;
2546
2547 if (stack.empty()) {
2548 // In a multi-piece expression, this means that the current piece is
Adrian Prantl05097242018-04-30 16:49:04 +00002549 // not available. Fill with zeros for now by resizing the data and
2550 // appending it
Kate Stoneb9c1b512016-09-06 20:57:50 +00002551 curr_piece.ResizeData(piece_byte_size);
2552 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2553 pieces.AppendDataToHostBuffer(curr_piece);
2554 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00002555 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002556 // Extract the current piece into "curr_piece"
2557 Value curr_piece_source_value(stack.back());
2558 stack.pop_back();
2559
2560 const Value::ValueType curr_piece_source_value_type =
2561 curr_piece_source_value.GetValueType();
2562 switch (curr_piece_source_value_type) {
2563 case Value::eValueTypeLoadAddress:
2564 if (process) {
2565 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2566 lldb::addr_t load_addr =
2567 curr_piece_source_value.GetScalar().ULongLong(
2568 LLDB_INVALID_ADDRESS);
2569 if (process->ReadMemory(
2570 load_addr, curr_piece.GetBuffer().GetBytes(),
2571 piece_byte_size, error) != piece_byte_size) {
2572 if (error_ptr)
2573 error_ptr->SetErrorStringWithFormat(
2574 "failed to read memory DW_OP_piece(%" PRIu64
2575 ") from 0x%" PRIx64,
2576 piece_byte_size, load_addr);
2577 return false;
2578 }
2579 } else {
2580 if (error_ptr)
2581 error_ptr->SetErrorStringWithFormat(
2582 "failed to resize the piece memory buffer for "
2583 "DW_OP_piece(%" PRIu64 ")",
2584 piece_byte_size);
2585 return false;
2586 }
2587 }
Tamas Berghammer1f5e4482015-09-16 12:37:06 +00002588 break;
2589
Kate Stoneb9c1b512016-09-06 20:57:50 +00002590 case Value::eValueTypeFileAddress:
2591 case Value::eValueTypeHostAddress:
2592 if (error_ptr) {
2593 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2594 LLDB_INVALID_ADDRESS);
2595 error_ptr->SetErrorStringWithFormat(
2596 "failed to read memory DW_OP_piece(%" PRIu64
2597 ") from %s address 0x%" PRIx64,
2598 piece_byte_size, curr_piece_source_value.GetValueType() ==
2599 Value::eValueTypeFileAddress
2600 ? "file"
2601 : "host",
2602 addr);
2603 }
Sean Callanan4740a732016-09-06 04:48:36 +00002604 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002605
2606 case Value::eValueTypeScalar: {
2607 uint32_t bit_size = piece_byte_size * 8;
2608 uint32_t bit_offset = 0;
2609 if (!curr_piece_source_value.GetScalar().ExtractBitfield(
2610 bit_size, bit_offset)) {
2611 if (error_ptr)
2612 error_ptr->SetErrorStringWithFormat(
2613 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2614 " byte scalar value.",
2615 piece_byte_size,
2616 (uint64_t)curr_piece_source_value.GetScalar()
2617 .GetByteSize());
2618 return false;
2619 }
2620 curr_piece = curr_piece_source_value;
2621 } break;
2622
2623 case Value::eValueTypeVector: {
2624 if (curr_piece_source_value.GetVector().length >= piece_byte_size)
2625 curr_piece_source_value.GetVector().length = piece_byte_size;
2626 else {
2627 if (error_ptr)
2628 error_ptr->SetErrorStringWithFormat(
2629 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2630 " byte vector value.",
2631 piece_byte_size,
2632 (uint64_t)curr_piece_source_value.GetVector().length);
2633 return false;
2634 }
2635 } break;
2636 }
2637
2638 // Check if this is the first piece?
2639 if (op_piece_offset == 0) {
Adrian Prantl05097242018-04-30 16:49:04 +00002640 // This is the first piece, we should push it back onto the stack
2641 // so subsequent pieces will be able to access this piece and add
2642 // to it
Kate Stoneb9c1b512016-09-06 20:57:50 +00002643 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2644 if (error_ptr)
2645 error_ptr->SetErrorString("failed to append piece data");
2646 return false;
2647 }
2648 } else {
2649 // If this is the second or later piece there should be a value on
2650 // the stack
2651 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2652 if (error_ptr)
2653 error_ptr->SetErrorStringWithFormat(
2654 "DW_OP_piece for offset %" PRIu64
2655 " but top of stack is of size %" PRIu64,
2656 op_piece_offset, pieces.GetBuffer().GetByteSize());
2657 return false;
2658 }
2659
2660 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2661 if (error_ptr)
2662 error_ptr->SetErrorString("failed to append piece data");
2663 return false;
2664 }
2665 }
2666 op_piece_offset += piece_byte_size;
Sean Callanan4740a732016-09-06 04:48:36 +00002667 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002668 }
2669 } break;
Sean Callanan4740a732016-09-06 04:48:36 +00002670
Kate Stoneb9c1b512016-09-06 20:57:50 +00002671 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2672 if (stack.size() < 1) {
2673 if (error_ptr)
2674 error_ptr->SetErrorString(
2675 "Expression stack needs at least 1 item for DW_OP_bit_piece.");
Sean Callanan4740a732016-09-06 04:48:36 +00002676 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002677 } else {
2678 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2679 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2680 switch (stack.back().GetValueType()) {
2681 case Value::eValueTypeScalar: {
2682 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2683 piece_bit_offset)) {
2684 if (error_ptr)
2685 error_ptr->SetErrorStringWithFormat(
2686 "unable to extract %" PRIu64 " bit value with %" PRIu64
2687 " bit offset from a %" PRIu64 " bit scalar value.",
2688 piece_bit_size, piece_bit_offset,
2689 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2690 return false;
2691 }
2692 } break;
Sean Callanan4740a732016-09-06 04:48:36 +00002693
Kate Stoneb9c1b512016-09-06 20:57:50 +00002694 case Value::eValueTypeFileAddress:
2695 case Value::eValueTypeLoadAddress:
2696 case Value::eValueTypeHostAddress:
2697 if (error_ptr) {
2698 error_ptr->SetErrorStringWithFormat(
2699 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2700 ", bit_offset = %" PRIu64 ") from an addresss value.",
2701 piece_bit_size, piece_bit_offset);
2702 }
2703 return false;
2704
2705 case Value::eValueTypeVector:
2706 if (error_ptr) {
2707 error_ptr->SetErrorStringWithFormat(
2708 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2709 ", bit_offset = %" PRIu64 ") from a vector value.",
2710 piece_bit_size, piece_bit_offset);
2711 }
2712 return false;
2713 }
2714 }
2715 break;
2716
2717 //----------------------------------------------------------------------
2718 // OPCODE: DW_OP_push_object_address
2719 // OPERANDS: none
2720 // DESCRIPTION: Pushes the address of the object currently being
Adrian Prantl05097242018-04-30 16:49:04 +00002721 // evaluated as part of evaluation of a user presented expression. This
2722 // object may correspond to an independent variable described by its own
2723 // DIE or it may be a component of an array, structure, or class whose
2724 // address has been dynamically determined by an earlier step during user
2725 // expression evaluation.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002726 //----------------------------------------------------------------------
2727 case DW_OP_push_object_address:
2728 if (object_address_ptr)
2729 stack.push_back(*object_address_ptr);
2730 else {
2731 if (error_ptr)
2732 error_ptr->SetErrorString("DW_OP_push_object_address used without "
2733 "specifying an object address");
2734 return false;
2735 }
2736 break;
2737
2738 //----------------------------------------------------------------------
2739 // OPCODE: DW_OP_call2
2740 // OPERANDS:
2741 // uint16_t compile unit relative offset of a DIE
2742 // DESCRIPTION: Performs subroutine calls during evaluation
Adrian Prantl05097242018-04-30 16:49:04 +00002743 // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2744 // debugging information entry in the current compilation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002745 //
2746 // Operand interpretation is exactly like that for DW_FORM_ref2.
2747 //
Adrian Prantl05097242018-04-30 16:49:04 +00002748 // This operation transfers control of DWARF expression evaluation to the
2749 // DW_AT_location attribute of the referenced DIE. If there is no such
2750 // attribute, then there is no effect. Execution of the DWARF expression of
2751 // a DW_AT_location attribute may add to and/or remove from values on the
2752 // stack. Execution returns to the point following the call when the end of
2753 // the attribute is reached. Values on the stack at the time of the call
2754 // may be used as parameters by the called expression and values left on
2755 // the stack by the called expression may be used as return values by prior
2756 // agreement between the calling and called expressions.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002757 //----------------------------------------------------------------------
2758 case DW_OP_call2:
2759 if (error_ptr)
2760 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2761 return false;
2762 //----------------------------------------------------------------------
2763 // OPCODE: DW_OP_call4
2764 // OPERANDS: 1
2765 // uint32_t compile unit relative offset of a DIE
2766 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
Adrian Prantl05097242018-04-30 16:49:04 +00002767 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2768 // a debugging information entry in the current compilation unit.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002769 //
2770 // Operand interpretation DW_OP_call4 is exactly like that for
2771 // DW_FORM_ref4.
2772 //
Adrian Prantl05097242018-04-30 16:49:04 +00002773 // This operation transfers control of DWARF expression evaluation to the
2774 // DW_AT_location attribute of the referenced DIE. If there is no such
2775 // attribute, then there is no effect. Execution of the DWARF expression of
2776 // a DW_AT_location attribute may add to and/or remove from values on the
2777 // stack. Execution returns to the point following the call when the end of
2778 // the attribute is reached. Values on the stack at the time of the call
2779 // may be used as parameters by the called expression and values left on
2780 // the stack by the called expression may be used as return values by prior
2781 // agreement between the calling and called expressions.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002782 //----------------------------------------------------------------------
2783 case DW_OP_call4:
2784 if (error_ptr)
2785 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2786 return false;
2787
2788 //----------------------------------------------------------------------
2789 // OPCODE: DW_OP_stack_value
2790 // OPERANDS: None
2791 // DESCRIPTION: Specifies that the object does not exist in memory but
Adrian Prantl05097242018-04-30 16:49:04 +00002792 // rather is a constant value. The value from the top of the stack is the
2793 // value to be used. This is the actual object value and not the location.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002794 //----------------------------------------------------------------------
2795 case DW_OP_stack_value:
2796 stack.back().SetValueType(Value::eValueTypeScalar);
2797 break;
2798
2799 //----------------------------------------------------------------------
2800 // OPCODE: DW_OP_call_frame_cfa
2801 // OPERANDS: None
2802 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2803 // the canonical frame address consistent with the call frame information
2804 // located in .debug_frame (or in the FDEs of the eh_frame section).
2805 //----------------------------------------------------------------------
2806 case DW_OP_call_frame_cfa:
2807 if (frame) {
2808 // Note that we don't have to parse FDEs because this DWARF expression
2809 // is commonly evaluated with a valid stack frame.
2810 StackID id = frame->GetStackID();
2811 addr_t cfa = id.GetCallFrameAddress();
2812 if (cfa != LLDB_INVALID_ADDRESS) {
2813 stack.push_back(Scalar(cfa));
2814 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2815 } else if (error_ptr)
2816 error_ptr->SetErrorString("Stack frame does not include a canonical "
2817 "frame address for DW_OP_call_frame_cfa "
2818 "opcode.");
2819 } else {
2820 if (error_ptr)
2821 error_ptr->SetErrorString("Invalid stack frame in context for "
2822 "DW_OP_call_frame_cfa opcode.");
2823 return false;
2824 }
2825 break;
2826
2827 //----------------------------------------------------------------------
2828 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2829 // opcode, DW_OP_GNU_push_tls_address)
2830 // OPERANDS: none
2831 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
Adrian Prantl05097242018-04-30 16:49:04 +00002832 // an address in the current thread's thread-local storage block, and
2833 // pushes it on the stack.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002834 //----------------------------------------------------------------------
2835 case DW_OP_form_tls_address:
2836 case DW_OP_GNU_push_tls_address: {
2837 if (stack.size() < 1) {
2838 if (error_ptr) {
2839 if (op == DW_OP_form_tls_address)
2840 error_ptr->SetErrorString(
2841 "DW_OP_form_tls_address needs an argument.");
2842 else
2843 error_ptr->SetErrorString(
2844 "DW_OP_GNU_push_tls_address needs an argument.");
2845 }
2846 return false;
2847 }
2848
2849 if (!exe_ctx || !module_sp) {
2850 if (error_ptr)
2851 error_ptr->SetErrorString("No context to evaluate TLS within.");
2852 return false;
2853 }
2854
2855 Thread *thread = exe_ctx->GetThreadPtr();
2856 if (!thread) {
2857 if (error_ptr)
2858 error_ptr->SetErrorString("No thread to evaluate TLS within.");
2859 return false;
2860 }
2861
2862 // Lookup the TLS block address for this thread and module.
2863 const addr_t tls_file_addr =
2864 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2865 const addr_t tls_load_addr =
2866 thread->GetThreadLocalData(module_sp, tls_file_addr);
2867
2868 if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2869 if (error_ptr)
2870 error_ptr->SetErrorString(
2871 "No TLS data currently exists for this thread.");
2872 return false;
2873 }
2874
2875 stack.back().GetScalar() = tls_load_addr;
2876 stack.back().SetValueType(Value::eValueTypeLoadAddress);
2877 } break;
2878
2879 //----------------------------------------------------------------------
2880 // OPCODE: DW_OP_GNU_addr_index
2881 // OPERANDS: 1
2882 // ULEB128: index to the .debug_addr section
2883 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
Adrian Prantl05097242018-04-30 16:49:04 +00002884 // section with the base address specified by the DW_AT_addr_base attribute
2885 // and the 0 based index is the ULEB128 encoded index.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002886 //----------------------------------------------------------------------
2887 case DW_OP_GNU_addr_index: {
2888 if (!dwarf_cu) {
2889 if (error_ptr)
2890 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2891 "compile unit being specified");
2892 return false;
2893 }
2894 uint64_t index = opcodes.GetULEB128(&offset);
2895 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2896 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2897 lldb::offset_t offset = addr_base + index * index_size;
2898 uint64_t value =
2899 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data().GetMaxU64(
2900 &offset, index_size);
2901 stack.push_back(Scalar(value));
2902 stack.back().SetValueType(Value::eValueTypeFileAddress);
2903 } break;
2904
2905 //----------------------------------------------------------------------
2906 // OPCODE: DW_OP_GNU_const_index
2907 // OPERANDS: 1
2908 // ULEB128: index to the .debug_addr section
2909 // DESCRIPTION: Pushes an constant with the size of a machine address to
2910 // the stack from the .debug_addr section with the base address specified
2911 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2912 // encoded index.
2913 //----------------------------------------------------------------------
2914 case DW_OP_GNU_const_index: {
2915 if (!dwarf_cu) {
2916 if (error_ptr)
2917 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2918 "compile unit being specified");
2919 return false;
2920 }
2921 uint64_t index = opcodes.GetULEB128(&offset);
2922 uint32_t index_size = dwarf_cu->GetAddressByteSize();
2923 dw_offset_t addr_base = dwarf_cu->GetAddrBase();
2924 lldb::offset_t offset = addr_base + index * index_size;
2925 const DWARFDataExtractor &debug_addr =
2926 dwarf_cu->GetSymbolFileDWARF()->get_debug_addr_data();
2927 switch (index_size) {
2928 case 4:
2929 stack.push_back(Scalar(debug_addr.GetU32(&offset)));
2930 break;
2931 case 8:
2932 stack.push_back(Scalar(debug_addr.GetU64(&offset)));
2933 break;
2934 default:
2935 assert(false && "Unhandled index size");
2936 return false;
2937 }
2938 } break;
2939
Sean Callanan4740a732016-09-06 04:48:36 +00002940 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00002941 if (log)
2942 log->Printf("Unhandled opcode %s in DWARFExpression.",
2943 DW_OP_value_to_name(op));
2944 break;
Sean Callanan4740a732016-09-06 04:48:36 +00002945 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002946 }
2947
2948 if (stack.empty()) {
2949 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2950 // or DW_OP_bit_piece opcodes
2951 if (pieces.GetBuffer().GetByteSize()) {
2952 result = pieces;
2953 } else {
2954 if (error_ptr)
2955 error_ptr->SetErrorString("Stack empty after evaluation.");
2956 return false;
2957 }
2958 } else {
2959 if (log && log->GetVerbose()) {
2960 size_t count = stack.size();
2961 log->Printf("Stack after operation has %" PRIu64 " values:",
2962 (uint64_t)count);
2963 for (size_t i = 0; i < count; ++i) {
2964 StreamString new_value;
2965 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2966 stack[i].Dump(&new_value);
2967 log->Printf(" %s", new_value.GetData());
2968 }
2969 }
2970 result = stack.back();
2971 }
2972 return true; // Return true on success
Sean Callanan4740a732016-09-06 04:48:36 +00002973}
2974
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002975size_t DWARFExpression::LocationListSize(const DWARFUnit *dwarf_cu,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002976 const DataExtractor &debug_loc_data,
2977 lldb::offset_t offset) {
2978 const lldb::offset_t debug_loc_offset = offset;
2979 while (debug_loc_data.ValidOffset(offset)) {
2980 lldb::addr_t start_addr = LLDB_INVALID_ADDRESS;
2981 lldb::addr_t end_addr = LLDB_INVALID_ADDRESS;
2982 if (!AddressRangeForLocationListEntry(dwarf_cu, debug_loc_data, &offset,
2983 start_addr, end_addr))
2984 break;
2985
2986 if (start_addr == 0 && end_addr == 0)
2987 break;
2988
2989 uint16_t loc_length = debug_loc_data.GetU16(&offset);
2990 offset += loc_length;
2991 }
2992
2993 if (offset > debug_loc_offset)
2994 return offset - debug_loc_offset;
2995 return 0;
2996}
2997
2998bool DWARFExpression::AddressRangeForLocationListEntry(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00002999 const DWARFUnit *dwarf_cu, const DataExtractor &debug_loc_data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003000 lldb::offset_t *offset_ptr, lldb::addr_t &low_pc, lldb::addr_t &high_pc) {
3001 if (!debug_loc_data.ValidOffset(*offset_ptr))
3002 return false;
3003
3004 switch (dwarf_cu->GetSymbolFileDWARF()->GetLocationListFormat()) {
3005 case NonLocationList:
3006 return false;
3007 case RegularLocationList:
3008 low_pc = debug_loc_data.GetAddress(offset_ptr);
3009 high_pc = debug_loc_data.GetAddress(offset_ptr);
3010 return true;
3011 case SplitDwarfLocationList:
3012 switch (debug_loc_data.GetU8(offset_ptr)) {
Adrian Prantle7045062016-10-28 20:11:27 +00003013 case DW_LLE_end_of_list:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003014 return false;
Pavel Labath6b91e402016-10-31 11:53:13 +00003015 case DW_LLE_startx_endx: {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003016 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3017 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3018 index = debug_loc_data.GetULEB128(offset_ptr);
3019 high_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3020 return true;
3021 }
Pavel Labath6b91e402016-10-31 11:53:13 +00003022 case DW_LLE_startx_length: {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003023 uint64_t index = debug_loc_data.GetULEB128(offset_ptr);
3024 low_pc = ReadAddressFromDebugAddrSection(dwarf_cu, index);
3025 uint32_t length = debug_loc_data.GetU32(offset_ptr);
3026 high_pc = low_pc + length;
3027 return true;
3028 }
3029 default:
3030 // Not supported entry type
3031 return false;
3032 }
3033 }
3034 assert(false && "Not supported location list type");
3035 return false;
3036}
3037
3038static bool print_dwarf_exp_op(Stream &s, const DataExtractor &data,
3039 lldb::offset_t *offset_ptr, int address_size,
3040 int dwarf_ref_size) {
3041 uint8_t opcode = data.GetU8(offset_ptr);
3042 DRC_class opcode_class;
3043 uint64_t uint;
3044 int64_t sint;
3045
3046 int size;
3047
3048 opcode_class = DW_OP_value_to_class(opcode) & (~DRC_DWARFv3);
3049
3050 s.Printf("%s ", DW_OP_value_to_name(opcode));
3051
3052 /* Does this take zero parameters? If so we can shortcut this function. */
3053 if (opcode_class == DRC_ZEROOPERANDS)
3054 return true;
3055
3056 if (opcode_class == DRC_TWOOPERANDS && opcode == DW_OP_bregx) {
3057 uint = data.GetULEB128(offset_ptr);
3058 sint = data.GetSLEB128(offset_ptr);
3059 s.Printf("%" PRIu64 " %" PRIi64, uint, sint);
3060 return true;
3061 }
3062 if (opcode_class != DRC_ONEOPERAND) {
3063 s.Printf("UNKNOWN OP %u", opcode);
3064 return false;
3065 }
3066
3067 switch (opcode) {
3068 case DW_OP_addr:
3069 size = address_size;
3070 break;
3071 case DW_OP_const1u:
3072 size = 1;
3073 break;
3074 case DW_OP_const1s:
3075 size = -1;
3076 break;
3077 case DW_OP_const2u:
3078 size = 2;
3079 break;
3080 case DW_OP_const2s:
3081 size = -2;
3082 break;
3083 case DW_OP_const4u:
3084 size = 4;
3085 break;
3086 case DW_OP_const4s:
3087 size = -4;
3088 break;
3089 case DW_OP_const8u:
3090 size = 8;
3091 break;
3092 case DW_OP_const8s:
3093 size = -8;
3094 break;
3095 case DW_OP_constu:
3096 size = 128;
3097 break;
3098 case DW_OP_consts:
3099 size = -128;
3100 break;
3101 case DW_OP_fbreg:
3102 size = -128;
3103 break;
3104 case DW_OP_breg0:
3105 case DW_OP_breg1:
3106 case DW_OP_breg2:
3107 case DW_OP_breg3:
3108 case DW_OP_breg4:
3109 case DW_OP_breg5:
3110 case DW_OP_breg6:
3111 case DW_OP_breg7:
3112 case DW_OP_breg8:
3113 case DW_OP_breg9:
3114 case DW_OP_breg10:
3115 case DW_OP_breg11:
3116 case DW_OP_breg12:
3117 case DW_OP_breg13:
3118 case DW_OP_breg14:
3119 case DW_OP_breg15:
3120 case DW_OP_breg16:
3121 case DW_OP_breg17:
3122 case DW_OP_breg18:
3123 case DW_OP_breg19:
3124 case DW_OP_breg20:
3125 case DW_OP_breg21:
3126 case DW_OP_breg22:
3127 case DW_OP_breg23:
3128 case DW_OP_breg24:
3129 case DW_OP_breg25:
3130 case DW_OP_breg26:
3131 case DW_OP_breg27:
3132 case DW_OP_breg28:
3133 case DW_OP_breg29:
3134 case DW_OP_breg30:
3135 case DW_OP_breg31:
3136 size = -128;
3137 break;
3138 case DW_OP_pick:
3139 case DW_OP_deref_size:
3140 case DW_OP_xderef_size:
3141 size = 1;
3142 break;
3143 case DW_OP_skip:
3144 case DW_OP_bra:
3145 size = -2;
3146 break;
3147 case DW_OP_call2:
3148 size = 2;
3149 break;
3150 case DW_OP_call4:
3151 size = 4;
3152 break;
3153 case DW_OP_call_ref:
3154 size = dwarf_ref_size;
3155 break;
3156 case DW_OP_piece:
3157 case DW_OP_plus_uconst:
3158 case DW_OP_regx:
3159 case DW_OP_GNU_addr_index:
3160 case DW_OP_GNU_const_index:
3161 size = 128;
3162 break;
3163 default:
3164 s.Printf("UNKNOWN ONE-OPERAND OPCODE, #%u", opcode);
3165 return true;
3166 }
3167
3168 switch (size) {
3169 case -1:
3170 sint = (int8_t)data.GetU8(offset_ptr);
3171 s.Printf("%+" PRIi64, sint);
3172 break;
3173 case -2:
3174 sint = (int16_t)data.GetU16(offset_ptr);
3175 s.Printf("%+" PRIi64, sint);
3176 break;
3177 case -4:
3178 sint = (int32_t)data.GetU32(offset_ptr);
3179 s.Printf("%+" PRIi64, sint);
3180 break;
3181 case -8:
3182 sint = (int64_t)data.GetU64(offset_ptr);
3183 s.Printf("%+" PRIi64, sint);
3184 break;
3185 case -128:
3186 sint = data.GetSLEB128(offset_ptr);
3187 s.Printf("%+" PRIi64, sint);
3188 break;
3189 case 1:
3190 uint = data.GetU8(offset_ptr);
3191 s.Printf("0x%2.2" PRIx64, uint);
3192 break;
3193 case 2:
3194 uint = data.GetU16(offset_ptr);
3195 s.Printf("0x%4.4" PRIx64, uint);
3196 break;
3197 case 4:
3198 uint = data.GetU32(offset_ptr);
3199 s.Printf("0x%8.8" PRIx64, uint);
3200 break;
3201 case 8:
3202 uint = data.GetU64(offset_ptr);
3203 s.Printf("0x%16.16" PRIx64, uint);
3204 break;
3205 case 128:
3206 uint = data.GetULEB128(offset_ptr);
3207 s.Printf("0x%" PRIx64, uint);
3208 break;
3209 }
3210
3211 return false;
3212}
3213
3214bool DWARFExpression::PrintDWARFExpression(Stream &s, const DataExtractor &data,
3215 int address_size, int dwarf_ref_size,
3216 bool location_expression) {
3217 int op_count = 0;
3218 lldb::offset_t offset = 0;
3219 while (data.ValidOffset(offset)) {
3220 if (location_expression && op_count > 0)
3221 return false;
3222 if (op_count > 0)
3223 s.PutCString(", ");
3224 if (!print_dwarf_exp_op(s, data, &offset, address_size, dwarf_ref_size))
3225 return false;
3226 op_count++;
3227 }
3228
3229 return true;
3230}
3231
3232void DWARFExpression::PrintDWARFLocationList(
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003233 Stream &s, const DWARFUnit *cu, const DataExtractor &debug_loc_data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003234 lldb::offset_t offset) {
3235 uint64_t start_addr, end_addr;
Jan Kratochvilc4d65752018-03-18 20:11:02 +00003236 uint32_t addr_size = DWARFUnit::GetAddressByteSize(cu);
3237 s.SetAddressByteSize(DWARFUnit::GetAddressByteSize(cu));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003238 dw_addr_t base_addr = cu ? cu->GetBaseAddress() : 0;
3239 while (debug_loc_data.ValidOffset(offset)) {
3240 start_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3241 end_addr = debug_loc_data.GetMaxU64(&offset, addr_size);
3242
3243 if (start_addr == 0 && end_addr == 0)
3244 break;
3245
3246 s.PutCString("\n ");
3247 s.Indent();
3248 if (cu)
3249 s.AddressRange(start_addr + base_addr, end_addr + base_addr,
3250 cu->GetAddressByteSize(), NULL, ": ");
3251 uint32_t loc_length = debug_loc_data.GetU16(&offset);
3252
3253 DataExtractor locationData(debug_loc_data, offset, loc_length);
3254 PrintDWARFExpression(s, locationData, addr_size, 4, false);
3255 offset += loc_length;
3256 }
3257}
3258
3259bool DWARFExpression::GetOpAndEndOffsets(StackFrame &frame,
3260 lldb::offset_t &op_offset,
3261 lldb::offset_t &end_offset) {
3262 SymbolContext sc = frame.GetSymbolContext(eSymbolContextFunction);
3263 if (!sc.function) {
3264 return false;
3265 }
3266
3267 addr_t loclist_base_file_addr =
3268 sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
3269 if (loclist_base_file_addr == LLDB_INVALID_ADDRESS) {
3270 return false;
3271 }
3272
3273 addr_t pc_file_addr = frame.GetFrameCodeAddress().GetFileAddress();
3274 lldb::offset_t opcodes_offset, opcodes_length;
3275 if (!GetLocation(loclist_base_file_addr, pc_file_addr, opcodes_offset,
3276 opcodes_length)) {
3277 return false;
3278 }
3279
3280 if (opcodes_length == 0) {
3281 return false;
3282 }
3283
3284 op_offset = opcodes_offset;
3285 end_offset = opcodes_offset + opcodes_length;
3286 return true;
3287}
3288
Sean Callanan807ee2f2016-09-13 21:18:27 +00003289bool DWARFExpression::MatchesOperand(StackFrame &frame,
3290 const Instruction::Operand &operand) {
3291 using namespace OperandMatchers;
3292
Kate Stoneb9c1b512016-09-06 20:57:50 +00003293 lldb::offset_t op_offset;
3294 lldb::offset_t end_offset;
3295 if (!GetOpAndEndOffsets(frame, op_offset, end_offset)) {
3296 return false;
3297 }
3298
3299 if (!m_data.ValidOffset(op_offset) || op_offset >= end_offset) {
3300 return false;
3301 }
3302
3303 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
3304 if (!reg_ctx_sp) {
3305 return false;
3306 }
3307
3308 DataExtractor opcodes = m_data;
3309 uint8_t opcode = opcodes.GetU8(&op_offset);
3310
Sean Callanan807ee2f2016-09-13 21:18:27 +00003311 if (opcode == DW_OP_fbreg) {
3312 int64_t offset = opcodes.GetSLEB128(&op_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003313
Sean Callanan807ee2f2016-09-13 21:18:27 +00003314 DWARFExpression *fb_expr = frame.GetFrameBaseExpression(nullptr);
3315 if (!fb_expr) {
3316 return false;
3317 }
3318
Sean Callananaa4b44c2016-09-14 20:58:31 +00003319 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
3320 return fb_expr->MatchesOperand(frame, child);
3321 };
Sean Callanan807ee2f2016-09-13 21:18:27 +00003322
3323 if (!offset &&
3324 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3325 recurse)(operand)) {
3326 return true;
3327 }
3328
3329 return MatchUnaryOp(
3330 MatchOpType(Instruction::Operand::Type::Dereference),
3331 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3332 MatchImmOp(offset), recurse))(operand);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003333 }
3334
Sean Callanan807ee2f2016-09-13 21:18:27 +00003335 bool dereference = false;
3336 const RegisterInfo *reg = nullptr;
3337 int64_t offset = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003338
Sean Callanan807ee2f2016-09-13 21:18:27 +00003339 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
3340 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
3341 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
3342 offset = opcodes.GetSLEB128(&op_offset);
3343 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
3344 } else if (opcode == DW_OP_regx) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003345 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
Sean Callanan807ee2f2016-09-13 21:18:27 +00003346 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3347 } else if (opcode == DW_OP_bregx) {
3348 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
3349 offset = opcodes.GetSLEB128(&op_offset);
3350 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
3351 } else {
3352 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003353 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003354
Sean Callanan807ee2f2016-09-13 21:18:27 +00003355 if (!reg) {
3356 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003357 }
Sean Callanan807ee2f2016-09-13 21:18:27 +00003358
3359 if (dereference) {
3360 if (!offset &&
3361 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
3362 MatchRegOp(*reg))(operand)) {
3363 return true;
3364 }
3365
3366 return MatchUnaryOp(
3367 MatchOpType(Instruction::Operand::Type::Dereference),
3368 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
3369 MatchRegOp(*reg),
3370 MatchImmOp(offset)))(operand);
3371 } else {
3372 return MatchRegOp(*reg)(operand);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003373 }
3374}
Sean Callanan807ee2f2016-09-13 21:18:27 +00003375