LLDB mainline
DWARFExpression.cpp
Go to the documentation of this file.
1//===-- DWARFExpression.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include <cinttypes>
12
13#include <optional>
14#include <vector>
15
16#include "lldb/Core/Module.h"
17#include "lldb/Core/Value.h"
20#include "lldb/Utility/Log.h"
22#include "lldb/Utility/Scalar.h"
25
26#include "lldb/Host/Host.h"
27#include "lldb/Utility/Endian.h"
28
30
31#include "lldb/Target/ABI.h"
33#include "lldb/Target/Process.h"
36#include "lldb/Target/StackID.h"
37#include "lldb/Target/Target.h"
38#include "lldb/Target/Thread.h"
39#include "llvm/DebugInfo/DWARF/DWARFExpressionPrinter.h"
40#include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
41#include "llvm/Support/ErrorExtras.h"
42
43using namespace lldb;
44using namespace lldb_private;
45using namespace lldb_private::plugin::dwarf;
46using namespace llvm::dwarf;
47
48// DWARFExpression constructor
50
52
53// Destructor
55
56bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
57
58void DWARFExpression::UpdateValue(uint64_t const_value,
59 lldb::offset_t const_value_byte_size,
60 uint8_t addr_byte_size) {
61 if (!const_value_byte_size)
62 return;
63
64 m_data.SetData(
65 DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
66 m_data.SetByteOrder(endian::InlHostByteOrder());
67 m_data.SetAddressByteSize(addr_byte_size);
68}
69
71 ABI *abi,
72 llvm::DIDumpOptions options) const {
73 auto *MCRegInfo = abi ? &abi->GetMCRegisterInfo() : nullptr;
74 auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum,
75 bool IsEH) -> llvm::StringRef {
76 if (!MCRegInfo)
77 return {};
78 if (std::optional<unsigned> LLVMRegNum =
79 MCRegInfo->getLLVMRegNum(DwarfRegNum, IsEH))
80 if (const char *RegName = MCRegInfo->getName(*LLVMRegNum))
81 return llvm::StringRef(RegName);
82 return {};
83 };
84 options.GetNameForDWARFReg = GetRegName;
85 llvm::DWARFExpression E(m_data.GetAsLLVM(), m_data.GetAddressByteSize());
86 llvm::printDwarfExpression(&E, s->AsRawOstream(), options, nullptr);
87}
88
90
92 m_reg_kind = reg_kind;
93}
94
95llvm::Error
97 lldb::RegisterKind reg_kind,
98 uint32_t reg_num, Value &value) {
99 if (reg_ctx == nullptr)
100 return llvm::createStringError("no register context in frame");
101
102 const uint32_t native_reg =
103 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
104 if (native_reg == LLDB_INVALID_REGNUM)
105 return llvm::createStringError(
106 "unable to convert register kind=%u reg_num=%u to a native "
107 "register number",
108 reg_kind, reg_num);
109
110 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(native_reg);
111 RegisterValue reg_value;
112 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
113 if (reg_value.GetScalarValue(value.GetScalar())) {
116 const_cast<RegisterInfo *>(reg_info));
117 return llvm::Error::success();
118 }
119
120 // If we get this error, then we need to implement a value buffer in
121 // the dwarf expression evaluation function...
122 return llvm::createStringError(
123 "register %s can't be converted to a scalar value", reg_info->name);
124 }
125
126 return llvm::createStringError("register %s is not available",
127 reg_info->name);
128}
129
130/// Return the length in bytes of the set of operands for \p op. No guarantees
131/// are made on the state of \p data after this call.
132static lldb::offset_t
133GetOpcodeDataSize(const DataExtractor &data, const lldb::offset_t data_offset,
134 const LocationAtom op,
135 const DWARFExpression::Delegate *dwarf_cu) {
136 lldb::offset_t offset = data_offset;
137 switch (op) {
138 // Only used in LLVM metadata.
139 case DW_OP_LLVM_fragment:
140 case DW_OP_LLVM_convert:
141 case DW_OP_LLVM_tag_offset:
142 case DW_OP_LLVM_entry_value:
143 case DW_OP_LLVM_implicit_pointer:
144 case DW_OP_LLVM_arg:
145 case DW_OP_LLVM_extract_bits_sext:
146 case DW_OP_LLVM_extract_bits_zext:
147 break;
148 // Vendor extensions:
149 case DW_OP_HP_is_value:
150 case DW_OP_HP_fltconst4:
151 case DW_OP_HP_fltconst8:
152 case DW_OP_HP_mod_range:
153 case DW_OP_HP_unmod_range:
154 case DW_OP_HP_tls:
155 case DW_OP_INTEL_bit_piece:
156 case DW_OP_WASM_location:
157 case DW_OP_WASM_location_int:
158 case DW_OP_APPLE_uninit:
159 case DW_OP_PGI_omp_thread_num:
160 case DW_OP_hi_user:
161 case DW_OP_GNU_implicit_pointer:
162 break;
163
164 case DW_OP_addr:
165 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
166 return data.GetAddressByteSize();
167
168 // Opcodes with no arguments
169 case DW_OP_deref: // 0x06
170 case DW_OP_dup: // 0x12
171 case DW_OP_drop: // 0x13
172 case DW_OP_over: // 0x14
173 case DW_OP_swap: // 0x16
174 case DW_OP_rot: // 0x17
175 case DW_OP_xderef: // 0x18
176 case DW_OP_abs: // 0x19
177 case DW_OP_and: // 0x1a
178 case DW_OP_div: // 0x1b
179 case DW_OP_minus: // 0x1c
180 case DW_OP_mod: // 0x1d
181 case DW_OP_mul: // 0x1e
182 case DW_OP_neg: // 0x1f
183 case DW_OP_not: // 0x20
184 case DW_OP_or: // 0x21
185 case DW_OP_plus: // 0x22
186 case DW_OP_shl: // 0x24
187 case DW_OP_shr: // 0x25
188 case DW_OP_shra: // 0x26
189 case DW_OP_xor: // 0x27
190 case DW_OP_eq: // 0x29
191 case DW_OP_ge: // 0x2a
192 case DW_OP_gt: // 0x2b
193 case DW_OP_le: // 0x2c
194 case DW_OP_lt: // 0x2d
195 case DW_OP_ne: // 0x2e
196 case DW_OP_lit0: // 0x30
197 case DW_OP_lit1: // 0x31
198 case DW_OP_lit2: // 0x32
199 case DW_OP_lit3: // 0x33
200 case DW_OP_lit4: // 0x34
201 case DW_OP_lit5: // 0x35
202 case DW_OP_lit6: // 0x36
203 case DW_OP_lit7: // 0x37
204 case DW_OP_lit8: // 0x38
205 case DW_OP_lit9: // 0x39
206 case DW_OP_lit10: // 0x3A
207 case DW_OP_lit11: // 0x3B
208 case DW_OP_lit12: // 0x3C
209 case DW_OP_lit13: // 0x3D
210 case DW_OP_lit14: // 0x3E
211 case DW_OP_lit15: // 0x3F
212 case DW_OP_lit16: // 0x40
213 case DW_OP_lit17: // 0x41
214 case DW_OP_lit18: // 0x42
215 case DW_OP_lit19: // 0x43
216 case DW_OP_lit20: // 0x44
217 case DW_OP_lit21: // 0x45
218 case DW_OP_lit22: // 0x46
219 case DW_OP_lit23: // 0x47
220 case DW_OP_lit24: // 0x48
221 case DW_OP_lit25: // 0x49
222 case DW_OP_lit26: // 0x4A
223 case DW_OP_lit27: // 0x4B
224 case DW_OP_lit28: // 0x4C
225 case DW_OP_lit29: // 0x4D
226 case DW_OP_lit30: // 0x4E
227 case DW_OP_lit31: // 0x4f
228 case DW_OP_reg0: // 0x50
229 case DW_OP_reg1: // 0x51
230 case DW_OP_reg2: // 0x52
231 case DW_OP_reg3: // 0x53
232 case DW_OP_reg4: // 0x54
233 case DW_OP_reg5: // 0x55
234 case DW_OP_reg6: // 0x56
235 case DW_OP_reg7: // 0x57
236 case DW_OP_reg8: // 0x58
237 case DW_OP_reg9: // 0x59
238 case DW_OP_reg10: // 0x5A
239 case DW_OP_reg11: // 0x5B
240 case DW_OP_reg12: // 0x5C
241 case DW_OP_reg13: // 0x5D
242 case DW_OP_reg14: // 0x5E
243 case DW_OP_reg15: // 0x5F
244 case DW_OP_reg16: // 0x60
245 case DW_OP_reg17: // 0x61
246 case DW_OP_reg18: // 0x62
247 case DW_OP_reg19: // 0x63
248 case DW_OP_reg20: // 0x64
249 case DW_OP_reg21: // 0x65
250 case DW_OP_reg22: // 0x66
251 case DW_OP_reg23: // 0x67
252 case DW_OP_reg24: // 0x68
253 case DW_OP_reg25: // 0x69
254 case DW_OP_reg26: // 0x6A
255 case DW_OP_reg27: // 0x6B
256 case DW_OP_reg28: // 0x6C
257 case DW_OP_reg29: // 0x6D
258 case DW_OP_reg30: // 0x6E
259 case DW_OP_reg31: // 0x6F
260 case DW_OP_nop: // 0x96
261 case DW_OP_push_object_address: // 0x97 DWARF3
262 case DW_OP_form_tls_address: // 0x9b DWARF3
263 case DW_OP_call_frame_cfa: // 0x9c DWARF3
264 case DW_OP_stack_value: // 0x9f DWARF4
265 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
266 return 0;
267
268 // Opcodes with a single 1 byte arguments
269 case DW_OP_const1u: // 0x08 1 1-byte constant
270 case DW_OP_const1s: // 0x09 1 1-byte constant
271 case DW_OP_pick: // 0x15 1 1-byte stack index
272 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
273 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
274 case DW_OP_deref_type: // 0xa6 1 1-byte constant
275 return 1;
276
277 // Opcodes with a single 2 byte arguments
278 case DW_OP_const2u: // 0x0a 1 2-byte constant
279 case DW_OP_const2s: // 0x0b 1 2-byte constant
280 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
281 case DW_OP_bra: // 0x28 1 signed 2-byte constant
282 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
283 return 2;
284
285 // Opcodes with a single 4 byte arguments
286 case DW_OP_const4u: // 0x0c 1 4-byte constant
287 case DW_OP_const4s: // 0x0d 1 4-byte constant
288 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
289 return 4;
290
291 // Opcodes with a single 8 byte arguments
292 case DW_OP_const8u: // 0x0e 1 8-byte constant
293 case DW_OP_const8s: // 0x0f 1 8-byte constant
294 return 8;
295
296 // All opcodes that have a single ULEB (signed or unsigned) argument
297 case DW_OP_constu: // 0x10 1 ULEB128 constant
298 case DW_OP_consts: // 0x11 1 SLEB128 constant
299 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
300 case DW_OP_breg0: // 0x70 1 ULEB128 register
301 case DW_OP_breg1: // 0x71 1 ULEB128 register
302 case DW_OP_breg2: // 0x72 1 ULEB128 register
303 case DW_OP_breg3: // 0x73 1 ULEB128 register
304 case DW_OP_breg4: // 0x74 1 ULEB128 register
305 case DW_OP_breg5: // 0x75 1 ULEB128 register
306 case DW_OP_breg6: // 0x76 1 ULEB128 register
307 case DW_OP_breg7: // 0x77 1 ULEB128 register
308 case DW_OP_breg8: // 0x78 1 ULEB128 register
309 case DW_OP_breg9: // 0x79 1 ULEB128 register
310 case DW_OP_breg10: // 0x7a 1 ULEB128 register
311 case DW_OP_breg11: // 0x7b 1 ULEB128 register
312 case DW_OP_breg12: // 0x7c 1 ULEB128 register
313 case DW_OP_breg13: // 0x7d 1 ULEB128 register
314 case DW_OP_breg14: // 0x7e 1 ULEB128 register
315 case DW_OP_breg15: // 0x7f 1 ULEB128 register
316 case DW_OP_breg16: // 0x80 1 ULEB128 register
317 case DW_OP_breg17: // 0x81 1 ULEB128 register
318 case DW_OP_breg18: // 0x82 1 ULEB128 register
319 case DW_OP_breg19: // 0x83 1 ULEB128 register
320 case DW_OP_breg20: // 0x84 1 ULEB128 register
321 case DW_OP_breg21: // 0x85 1 ULEB128 register
322 case DW_OP_breg22: // 0x86 1 ULEB128 register
323 case DW_OP_breg23: // 0x87 1 ULEB128 register
324 case DW_OP_breg24: // 0x88 1 ULEB128 register
325 case DW_OP_breg25: // 0x89 1 ULEB128 register
326 case DW_OP_breg26: // 0x8a 1 ULEB128 register
327 case DW_OP_breg27: // 0x8b 1 ULEB128 register
328 case DW_OP_breg28: // 0x8c 1 ULEB128 register
329 case DW_OP_breg29: // 0x8d 1 ULEB128 register
330 case DW_OP_breg30: // 0x8e 1 ULEB128 register
331 case DW_OP_breg31: // 0x8f 1 ULEB128 register
332 case DW_OP_regx: // 0x90 1 ULEB128 register
333 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
334 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
335 case DW_OP_convert: // 0xa8 1 ULEB128 offset
336 case DW_OP_reinterpret: // 0xa9 1 ULEB128 offset
337 case DW_OP_addrx: // 0xa1 1 ULEB128 index
338 case DW_OP_constx: // 0xa2 1 ULEB128 index
339 case DW_OP_xderef_type: // 0xa7 1 ULEB128 index
340 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
341 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
342 data.Skip_LEB128(&offset);
343 return offset - data_offset;
344
345 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
346 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
347 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
348 case DW_OP_regval_type: // 0xa5 ULEB128 + ULEB128
349 data.Skip_LEB128(&offset);
350 data.Skip_LEB128(&offset);
351 return offset - data_offset;
352
353 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
354 // (DWARF4)
355 {
356 uint64_t block_len = data.Skip_LEB128(&offset);
357 offset += block_len;
358 return offset - data_offset;
359 }
360
361 case DW_OP_implicit_pointer: // 0xa0 4-byte (or 8-byte for DWARF 64) constant
362 // + LEB128
363 {
364 data.Skip_LEB128(&offset);
365 return (dwarf_cu ? dwarf_cu->GetAddressByteSize() : 4) + offset -
366 data_offset;
367 }
368
369 case DW_OP_GNU_entry_value:
370 case DW_OP_entry_value: // 0xa3 ULEB128 size + variable-length block
371 {
372 uint64_t subexpr_len = data.GetULEB128(&offset);
373 return (offset - data_offset) + subexpr_len;
374 }
375
376 case DW_OP_const_type: // 0xa4 ULEB128 + size + variable-length block
377 {
378 data.Skip_LEB128(&offset);
379 uint8_t length = data.GetU8(&offset);
380 return (offset - data_offset) + length;
381 }
382
383 case DW_OP_LLVM_user: // 0xe9: ULEB128 + variable length constant
384 {
385 uint64_t constants = data.GetULEB128(&offset);
386 return (offset - data_offset) + constants;
387 }
388 }
389
390 if (dwarf_cu)
391 return dwarf_cu->GetVendorDWARFOpcodeSize(data, data_offset, op);
392
393 return LLDB_INVALID_OFFSET;
394}
395
396static const char *DW_OP_value_to_name(uint32_t val) {
397 static char invalid[100];
398 llvm::StringRef llvmstr = llvm::dwarf::OperationEncodingString(val);
399 if (llvmstr.empty()) {
400 snprintf(invalid, sizeof(invalid), "Unknown DW_OP constant: 0x%x", val);
401 return invalid;
402 }
403 return llvmstr.data();
404}
405
406llvm::Expected<lldb::addr_t> DWARFExpression::GetLocation_DW_OP_addr(
407 const DWARFExpression::Delegate *dwarf_cu) const {
408 lldb::offset_t offset = 0;
409 while (m_data.ValidOffset(offset)) {
410 const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(&offset));
411
412 if (op == DW_OP_addr)
413 return m_data.GetAddress(&offset);
414
415 if (op == DW_OP_GNU_addr_index || op == DW_OP_addrx) {
416 const uint64_t index = m_data.GetULEB128(&offset);
417 if (dwarf_cu)
418 return dwarf_cu->ReadAddressFromDebugAddrSection(index);
419 return llvm::createStringError("cannot evaluate %s without a DWARF unit",
421 }
422
423 const lldb::offset_t op_arg_size =
424 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
425 if (op_arg_size == LLDB_INVALID_OFFSET)
426 return llvm::createStringError("cannot get opcode data size for %s",
428
429 offset += op_arg_size;
430 }
431
433}
434
436 const DWARFExpression::Delegate *dwarf_cu, lldb::addr_t file_addr) {
437 lldb::offset_t offset = 0;
438 while (m_data.ValidOffset(offset)) {
439 const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(&offset));
440
441 if (op == DW_OP_addr) {
442 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
443 // We have to make a copy of the data as we don't know if this data is
444 // from a read only memory mapped buffer, so we duplicate all of the data
445 // first, then modify it, and if all goes well, we then replace the data
446 // for this expression
447
448 // Make en encoder that contains a copy of the location expression data
449 // so we can write the address into the buffer using the correct byte
450 // order.
451 DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
452 m_data.GetByteOrder(), addr_byte_size);
453
454 // Replace the address in the new buffer
455 if (encoder.PutAddress(offset, file_addr) == UINT32_MAX)
456 return false;
457
458 // All went well, so now we can reset the data using a shared pointer to
459 // the heap data so "m_data" will now correctly manage the heap data.
460 m_data.SetData(encoder.GetDataBuffer());
461 return true;
462 }
463 if (op == DW_OP_addrx) {
464 // Replace DW_OP_addrx with DW_OP_addr, since we can't modify the
465 // read-only debug_addr table.
466 // Subtract one to account for the opcode.
467 llvm::ArrayRef data_before_op = m_data.GetData().take_front(offset - 1);
468
469 // Read the addrx index to determine how many bytes it needs.
470 const lldb::offset_t old_offset = offset;
471 m_data.GetULEB128(&offset);
472 if (old_offset == offset)
473 return false;
474 llvm::ArrayRef data_after_op = m_data.GetData().drop_front(offset);
475
476 DataEncoder encoder(m_data.GetByteOrder(), m_data.GetAddressByteSize());
477 encoder.AppendData(data_before_op);
478 encoder.AppendU8(DW_OP_addr);
479 encoder.AppendAddress(file_addr);
480 encoder.AppendData(data_after_op);
481 m_data.SetData(encoder.GetDataBuffer());
482 return true;
483 }
484 const lldb::offset_t op_arg_size =
485 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
486 if (op_arg_size == LLDB_INVALID_OFFSET)
487 break;
488 offset += op_arg_size;
489 }
490 return false;
491}
492
494 const DWARFExpression::Delegate *dwarf_cu) const {
495 lldb::offset_t offset = 0;
496 while (m_data.ValidOffset(offset)) {
497 const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(&offset));
498
499 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
500 return true;
501 const lldb::offset_t op_arg_size =
502 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
503 if (op_arg_size == LLDB_INVALID_OFFSET)
504 return false;
505 offset += op_arg_size;
506 }
507 return false;
508}
510 const DWARFExpression::Delegate *dwarf_cu,
511 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
512 &link_address_callback) {
513 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
514 // We have to make a copy of the data as we don't know if this data is from a
515 // read only memory mapped buffer, so we duplicate all of the data first,
516 // then modify it, and if all goes well, we then replace the data for this
517 // expression.
518 // Make en encoder that contains a copy of the location expression data so we
519 // can write the address into the buffer using the correct byte order.
520 DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
521 m_data.GetByteOrder(), addr_byte_size);
522
523 lldb::offset_t offset = 0;
524 lldb::offset_t const_offset = 0;
525 lldb::addr_t const_value = 0;
526 size_t const_byte_size = 0;
527 while (m_data.ValidOffset(offset)) {
528 const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(&offset));
529
530 bool decoded_data = false;
531 switch (op) {
532 case DW_OP_const4u:
533 // Remember the const offset in case we later have a
534 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
535 const_offset = offset;
536 const_value = m_data.GetU32(&offset);
537 decoded_data = true;
538 const_byte_size = 4;
539 break;
540
541 case DW_OP_const8u:
542 // Remember the const offset in case we later have a
543 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
544 const_offset = offset;
545 const_value = m_data.GetU64(&offset);
546 decoded_data = true;
547 const_byte_size = 8;
548 break;
549
550 case DW_OP_form_tls_address:
551 case DW_OP_GNU_push_tls_address:
552 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
553 // by a file address on the stack. We assume that DW_OP_const4u or
554 // DW_OP_const8u is used for these values, and we check that the last
555 // opcode we got before either of these was DW_OP_const4u or
556 // DW_OP_const8u. If so, then we can link the value accordingly. For
557 // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
558 // address of a structure that contains a function pointer, the pthread
559 // key and the offset into the data pointed to by the pthread key. So we
560 // must link this address and also set the module of this expression to
561 // the new_module_sp so we can resolve the file address correctly
562 if (const_byte_size > 0) {
563 lldb::addr_t linked_file_addr = link_address_callback(const_value);
564 if (linked_file_addr == LLDB_INVALID_ADDRESS)
565 return false;
566 // Replace the address in the new buffer
567 if (encoder.PutUnsigned(const_offset, const_byte_size,
568 linked_file_addr) == UINT32_MAX)
569 return false;
570 }
571 break;
572
573 default:
574 const_offset = 0;
575 const_value = 0;
576 const_byte_size = 0;
577 break;
578 }
579
580 if (!decoded_data) {
581 const lldb::offset_t op_arg_size =
582 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
583 if (op_arg_size == LLDB_INVALID_OFFSET)
584 return false;
585 else
586 offset += op_arg_size;
587 }
588 }
589
590 m_data.SetData(encoder.GetDataBuffer());
591 return true;
592}
593
595 ExecutionContext *exe_ctx,
596 RegisterContext *reg_ctx,
597 const DataExtractor &opcodes,
598 lldb::offset_t &opcode_offset,
599 Log *log) {
600 // DW_OP_entry_value(sub-expr) describes the location a variable had upon
601 // function entry: this variable location is presumed to be optimized out at
602 // the current PC value. The caller of the function may have call site
603 // information that describes an alternate location for the variable (e.g. a
604 // constant literal, or a spilled stack value) in the parent frame.
605 //
606 // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
607 //
608 // void child(int &sink, int x) {
609 // ...
610 // /* "x" gets optimized out. */
611 //
612 // /* The location of "x" here is: DW_OP_entry_value($reg2). */
613 // ++sink;
614 // }
615 //
616 // void parent() {
617 // int sink;
618 //
619 // /*
620 // * The callsite information emitted here is:
621 // *
622 // * DW_TAG_call_site
623 // * DW_AT_return_pc ... (for "child(sink, 123);")
624 // * DW_TAG_call_site_parameter (for "sink")
625 // * DW_AT_location ($reg1)
626 // * DW_AT_call_value ($SP - 8)
627 // * DW_TAG_call_site_parameter (for "x")
628 // * DW_AT_location ($reg2)
629 // * DW_AT_call_value ($literal 123)
630 // *
631 // * DW_TAG_call_site
632 // * DW_AT_return_pc ... (for "child(sink, 456);")
633 // * ...
634 // */
635 // child(sink, 123);
636 // child(sink, 456);
637 // }
638 //
639 // When the program stops at "++sink" within `child`, the debugger determines
640 // the call site by analyzing the return address. Once the call site is found,
641 // the debugger determines which parameter is referenced by DW_OP_entry_value
642 // and evaluates the corresponding location for that parameter in `parent`.
643
644 // 1. Find the function which pushed the current frame onto the stack.
645 if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) {
646 return llvm::createStringError("no exe/reg context");
647 }
648
649 StackFrame *current_frame = exe_ctx->GetFramePtr();
650 Thread *thread = exe_ctx->GetThreadPtr();
651 if (!current_frame || !thread)
652 return llvm::createStringError("no current frame/thread");
653
654 Target &target = exe_ctx->GetTargetRef();
655 StackFrameSP parent_frame = nullptr;
656 addr_t return_pc = LLDB_INVALID_ADDRESS;
657 uint32_t current_frame_idx = current_frame->GetFrameIndex();
658
659 for (uint32_t parent_frame_idx = current_frame_idx + 1;; parent_frame_idx++) {
660 parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx);
661 // If this is null, we're at the end of the stack.
662 if (!parent_frame)
663 break;
664
665 // Record the first valid return address, even if this is an inlined frame,
666 // in order to look up the associated call edge in the first non-inlined
667 // parent frame.
668 if (return_pc == LLDB_INVALID_ADDRESS) {
669 return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
670 LLDB_LOG(log, "immediate ancestor with pc = {0:x}", return_pc);
671 }
672
673 // If we've found an inlined frame, skip it (these have no call site
674 // parameters).
675 if (parent_frame->IsInlined())
676 continue;
677
678 // We've found the first non-inlined parent frame.
679 break;
680 }
681 if (!parent_frame || !parent_frame->GetRegisterContext()) {
682 return llvm::createStringError("no parent frame with reg ctx");
683 }
684
685 Function *parent_func =
686 parent_frame->GetSymbolContext(eSymbolContextFunction).function;
687 if (!parent_func)
688 return llvm::createStringError("no parent function");
689
690 // 2. Find the call edge in the parent function responsible for creating the
691 // current activation.
692 Function *current_func =
693 current_frame->GetSymbolContext(eSymbolContextFunction).function;
694 if (!current_func)
695 return llvm::createStringError("no current function");
696
697 CallEdge *call_edge = nullptr;
698 ModuleList &modlist = target.GetImages();
699 ExecutionContext parent_exe_ctx = *exe_ctx;
700 parent_exe_ctx.SetFrameSP(parent_frame);
701 if (!parent_frame->IsArtificial()) {
702 // If the parent frame is not artificial, the current activation may be
703 // produced by an ambiguous tail call. In this case, refuse to proceed.
704 call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target);
705 if (!call_edge) {
706 return llvm::createStringErrorV(
707 "no call edge for retn-pc = {0:x} in parent frame {1}", return_pc,
708 parent_func->GetName());
709 }
710 Function *callee_func = call_edge->GetCallee(modlist, parent_exe_ctx);
711 if (callee_func != current_func) {
712 return llvm::createStringError(
713 "ambiguous call sequence, can't find real parent frame");
714 }
715 } else {
716 // The StackFrameList solver machinery has deduced that an unambiguous tail
717 // call sequence that produced the current activation. The first edge in
718 // the parent that points to the current function must be valid.
719 for (auto &edge : parent_func->GetTailCallingEdges()) {
720 if (edge->GetCallee(modlist, parent_exe_ctx) == current_func) {
721 call_edge = edge.get();
722 break;
723 }
724 }
725 }
726 if (!call_edge)
727 return llvm::createStringError("no unambiguous edge from parent "
728 "to current function");
729
730 // 3. Attempt to locate the DW_OP_entry_value expression in the set of
731 // available call site parameters. If found, evaluate the corresponding
732 // parameter in the context of the parent frame.
733 const uint32_t subexpr_len = opcodes.GetULEB128(&opcode_offset);
734 const void *subexpr_data = opcodes.GetData(&opcode_offset, subexpr_len);
735 if (!subexpr_data)
736 return llvm::createStringError("subexpr could not be read");
737
738 const CallSiteParameter *matched_param = nullptr;
739 for (const CallSiteParameter &param : call_edge->GetCallSiteParameters()) {
740 DataExtractor param_subexpr_extractor;
741 if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor))
742 continue;
743 lldb::offset_t param_subexpr_offset = 0;
744 const void *param_subexpr_data =
745 param_subexpr_extractor.GetData(&param_subexpr_offset, subexpr_len);
746 if (!param_subexpr_data ||
747 param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0)
748 continue;
749
750 // At this point, the DW_OP_entry_value sub-expression and the callee-side
751 // expression in the call site parameter are known to have the same length.
752 // Check whether they are equal.
753 //
754 // Note that an equality check is sufficient: the contents of the
755 // DW_OP_entry_value subexpression are only used to identify the right call
756 // site parameter in the parent, and do not require any special handling.
757 if (memcmp(subexpr_data, param_subexpr_data, subexpr_len) == 0) {
758 matched_param = &param;
759 break;
760 }
761 }
762 if (!matched_param)
763 return llvm::createStringError("no matching call site param found");
764
765 // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
766 // subexpresion whenever llvm does.
767 const DWARFExpressionList &param_expr = matched_param->LocationInCaller;
768
769 llvm::Expected<Value> maybe_result = param_expr.Evaluate(
770 &parent_exe_ctx, parent_frame->GetRegisterContext().get(),
772 /*initial_value_ptr=*/nullptr,
773 /*object_address_ptr=*/nullptr);
774 if (!maybe_result) {
775 LLDB_LOG(log,
776 "Evaluate_DW_OP_entry_value: call site param evaluation failed");
777 return maybe_result.takeError();
778 }
779
780 stack.push_back(*maybe_result);
781 return llvm::Error::success();
782}
783
784namespace {
785/// The location description kinds described by the DWARF v5
786/// specification. Composite locations are handled out-of-band and
787/// thus aren't part of the enum.
788enum LocationDescriptionKind {
789 Empty,
790 Memory,
791 Register,
792 Implicit
793 /* Composite*/
794};
795/// Adjust value's ValueType according to the kind of location description.
796void UpdateValueTypeFromLocationDescription(
797 Log *log, const DWARFExpression::Delegate *dwarf_cu,
798 LocationDescriptionKind kind, Value *value = nullptr) {
799 // Note that this function is conflating DWARF expressions with
800 // DWARF location descriptions. Perhaps it would be better to define
801 // a wrapper for DWARFExpression::Eval() that deals with DWARF
802 // location descriptions (which consist of one or more DWARF
803 // expressions). But doing this would mean we'd also need factor the
804 // handling of DW_OP_(bit_)piece out of this function.
805 if (dwarf_cu && dwarf_cu->GetVersion() >= 4) {
806 const char *log_msg = "DWARF location description kind: %s";
807 switch (kind) {
808 case Empty:
809 LLDB_LOGF(log, log_msg, "Empty");
810 break;
811 case Memory:
812 LLDB_LOGF(log, log_msg, "Memory");
813 if (value->GetValueType() == Value::ValueType::Scalar)
814 value->SetValueType(Value::ValueType::LoadAddress);
815 break;
816 case Register:
817 LLDB_LOGF(log, log_msg, "Register");
818 value->SetValueType(Value::ValueType::Scalar);
819 break;
820 case Implicit:
821 LLDB_LOGF(log, log_msg, "Implicit");
822 if (value->GetValueType() == Value::ValueType::LoadAddress)
823 value->SetValueType(Value::ValueType::Scalar);
824 break;
825 }
826 }
827}
828} // namespace
829
830/// Helper function to move common code used to resolve a file address and turn
831/// into a load address.
832///
833/// \param exe_ctx Pointer to the execution context
834/// \param module_sp shared_ptr contains the module if we have one
835/// \param dw_op_type C-style string used to vary the error output
836/// \param file_addr the file address we are trying to resolve and turn into a
837/// load address
838/// \param so_addr out parameter, will be set to load address or section offset
839/// \param check_sectionoffset bool which determines if having a section offset
840/// but not a load address is considerd a success
841/// \returns std::optional containing the load address if resolving and getting
842/// the load address succeed or an empty Optinal otherwise. If
843/// check_sectionoffset is true we consider LLDB_INVALID_ADDRESS a
844/// success if so_addr.IsSectionOffset() is true.
845static llvm::Expected<lldb::addr_t>
847 const char *dw_op_type, lldb::addr_t file_addr,
848 Address &so_addr, bool check_sectionoffset = false) {
849 if (!module_sp)
850 return llvm::createStringError("need module to resolve file address for %s",
851 dw_op_type);
852
853 if (!module_sp->ResolveFileAddress(file_addr, so_addr))
854 return llvm::createStringError("failed to resolve file address in module");
855
856 const addr_t load_addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
857
858 if (load_addr == LLDB_INVALID_ADDRESS &&
859 (check_sectionoffset && !so_addr.IsSectionOffset()))
860 return llvm::createStringError("failed to resolve load address");
861
862 return load_addr;
863}
864
865/// @brief Helper function to load sized data from a uint8_t buffer.
866///
867/// @param addr_bytes The buffer containing raw data.
868/// @param size_addr_bytes How large is the underlying raw data.
869/// @param byte_order What is the byte order of the underlying data.
870/// @param size How much of the underlying data we want to use.
871/// @return The underlying data converted into a Scalar.
872static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
873 size_t size_addr_bytes,
874 ByteOrder byte_order, size_t size) {
875 DataExtractor addr_data(addr_bytes, size_addr_bytes, byte_order, size);
876
877 lldb::offset_t addr_data_offset = 0;
878 if (size <= 8)
879 return addr_data.GetMaxU64(&addr_data_offset, size);
880 return addr_data.GetAddress(&addr_data_offset);
881}
882
883static llvm::Error Evaluate_DW_OP_deref_size(
885 lldb::ModuleSP module_sp, Process *process, Target *target, uint8_t size,
886 size_t size_addr_bytes,
887 LocationDescriptionKind &dwarf4_location_description_kind) {
888 if (stack.empty())
889 return llvm::createStringError(
890 "expression stack empty for DW_OP_deref_size");
891
892 if (size > 8)
893 return llvm::createStringError(
894 "Invalid address size for DW_OP_deref_size: %d\n", size);
895
896 // Deref a register or implicit location and truncate the value to `size`
897 // bytes. See the corresponding comment in DW_OP_deref for more details on
898 // why we deref these locations this way.
899 if (dwarf4_location_description_kind == Register ||
900 dwarf4_location_description_kind == Implicit) {
901 // Reset context to default values.
902 dwarf4_location_description_kind = Memory;
903 stack.back().ClearContext();
904
905 // Truncate the value on top of the stack to *size* bytes then
906 // extend to the size of an address (e.g. generic type).
907 Scalar scalar = stack.back().GetScalar();
908 scalar.TruncOrExtendTo(size * 8, /*sign=*/false);
909 scalar.TruncOrExtendTo(size_addr_bytes * 8,
910 /*sign=*/false);
911 stack.back().GetScalar() = scalar;
912 return llvm::Error::success();
913 }
914
915 Value::ValueType value_type = stack.back().GetValueType();
916 switch (value_type) {
918 void *src = (void *)stack.back().GetScalar().ULongLong();
919 intptr_t ptr;
920 ::memcpy(&ptr, src, sizeof(void *));
921 // I can't decide whether the size operand should apply to the bytes in
922 // their lldb-host endianness or the target endianness.. I doubt this'll
923 // ever come up but I'll opt for assuming big endian regardless.
924 switch (size) {
925 case 1:
926 ptr = ptr & 0xff;
927 break;
928 case 2:
929 ptr = ptr & 0xffff;
930 break;
931 case 3:
932 ptr = ptr & 0xffffff;
933 break;
934 case 4:
935 ptr = ptr & 0xffffffff;
936 break;
937 // The casts are added to work around the case where intptr_t is a 32-bit
938 // quantity. Presumably we won't hit the 5..7 cases if (void*) is 32-bits in
939 // this program.
940 case 5:
941 ptr = (intptr_t)ptr & 0xffffffffffULL;
942 break;
943 case 6:
944 ptr = (intptr_t)ptr & 0xffffffffffffULL;
945 break;
946 case 7:
947 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
948 break;
949 default:
950 break;
951 }
952 stack.back().GetScalar() = ptr;
953 stack.back().ClearContext();
954 } break;
956 auto file_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
957 Address so_addr;
958 auto maybe_load_addr = ResolveLoadAddress(
959 exe_ctx, module_sp, "DW_OP_deref_size", file_addr, so_addr,
960 /*check_sectionoffset=*/true);
961
962 if (!maybe_load_addr)
963 return maybe_load_addr.takeError();
964
965 addr_t load_addr = *maybe_load_addr;
966
967 if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) {
968 uint8_t addr_bytes[8];
970
971 if (!target || target->ReadMemory(so_addr, &addr_bytes, size, error,
972 /*force_live_memory=*/false) != size)
973 return llvm::createStringError(
974 "failed to dereference pointer for DW_OP_deref_size: "
975 "%s\n",
976 error.AsCString());
977
978 ObjectFile *objfile = module_sp->GetObjectFile();
979
980 stack.back().GetScalar() = DerefSizeExtractDataHelper(
981 addr_bytes, size, objfile->GetByteOrder(), size);
982 stack.back().ClearContext();
983 break;
984 }
985 stack.back().GetScalar() = load_addr;
986 // Fall through to load address promotion code below.
987 }
988
989 [[fallthrough]];
991 // Promote Scalar to LoadAddress and fall through.
992 stack.back().SetValueType(Value::ValueType::LoadAddress);
993 [[fallthrough]];
995 if (!exe_ctx)
996 return llvm::createStringError(
997 "no execution context for DW_OP_deref_size");
998 if (!process)
999 return llvm::createStringError("no process for DW_OP_deref_size");
1000
1001 lldb::addr_t pointer_addr =
1002 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1003 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1004 Status error;
1005
1006 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) != size)
1007 return llvm::createStringError(
1008 "failed to dereference pointer from 0x%" PRIx64
1009 " for DW_OP_deref_size: %s\n",
1010 pointer_addr, error.AsCString());
1011
1012 stack.back().GetScalar() = DerefSizeExtractDataHelper(
1013 addr_bytes, sizeof(addr_bytes), process->GetByteOrder(), size);
1014 stack.back().ClearContext();
1015 } break;
1016
1018 return llvm::createStringError("invalid value for DW_OP_deref_size");
1019 }
1020
1021 return llvm::Error::success();
1022}
1023
1024llvm::Expected<Value> DWARFExpression::Evaluate(
1025 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
1026 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
1027 const DWARFExpression::Delegate *dwarf_cu,
1028 const lldb::RegisterKind reg_kind, const Value *initial_value_ptr,
1029 const Value *object_address_ptr) {
1030
1031 if (opcodes.GetByteSize() == 0)
1032 return llvm::createStringError(
1033 "no location, value may have been optimized out");
1034
1035 Stack stack;
1036
1037 Process *process = nullptr;
1038 StackFrame *frame = nullptr;
1039 Target *target = nullptr;
1040
1041 if (exe_ctx) {
1042 process = exe_ctx->GetProcessPtr();
1043 frame = exe_ctx->GetFramePtr();
1044 target = exe_ctx->GetTargetPtr();
1045 }
1046 if (reg_ctx == nullptr && frame)
1047 reg_ctx = frame->GetRegisterContext().get();
1048
1049 if (initial_value_ptr)
1050 stack.push_back(*initial_value_ptr);
1051
1052 lldb::offset_t offset = 0;
1053 Value tmp;
1054 uint32_t reg_num;
1055
1056 /// Insertion point for evaluating multi-piece expression.
1057 uint64_t op_piece_offset = 0;
1058 Value pieces; // Used for DW_OP_piece
1059
1061 // A generic type is "an integral type that has the size of an address and an
1062 // unspecified signedness". For now, just use the signedness of the operand.
1063 // TODO: Implement a real typed stack, and store the genericness of the value
1064 // there.
1065 auto to_generic = [&](auto v) {
1066 // TODO: Avoid implicit trunc?
1067 // See https://github.com/llvm/llvm-project/issues/112510.
1068 bool is_signed = std::is_signed<decltype(v)>::value;
1069 return Scalar(llvm::APSInt(llvm::APInt(8 * opcodes.GetAddressByteSize(), v,
1070 is_signed, /*implicitTrunc=*/true),
1071 !is_signed));
1072 };
1073
1074 // The default kind is a memory location. This is updated by any
1075 // operation that changes this, such as DW_OP_stack_value, and reset
1076 // by composition operations like DW_OP_piece.
1077 LocationDescriptionKind dwarf4_location_description_kind = Memory;
1078
1079 while (opcodes.ValidOffset(offset)) {
1080 const lldb::offset_t op_offset = offset;
1081 const uint8_t op = opcodes.GetU8(&offset);
1082
1083 if (log && log->GetVerbose()) {
1084 size_t count = stack.size();
1085 LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
1086 (uint64_t)count);
1087 for (size_t i = 0; i < count; ++i) {
1088 StreamString new_value;
1089 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1090 stack[i].Dump(&new_value);
1091 LLDB_LOGF(log, " %s", new_value.GetData());
1092 }
1093 LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
1095 }
1096
1097 if (std::optional<unsigned> arity =
1098 llvm::dwarf::OperationArity(static_cast<LocationAtom>(op))) {
1099 if (stack.size() < *arity)
1100 return llvm::createStringError(
1101 "%s needs at least %d stack entries (stack has %d entries)",
1102 DW_OP_value_to_name(op), *arity, stack.size());
1103 }
1104
1105 switch (op) {
1106 // The DW_OP_addr operation has a single operand that encodes a machine
1107 // address and whose size is the size of an address on the target machine.
1108 case DW_OP_addr:
1109 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1110 if (target &&
1112 // wasm file sections aren't mapped into memory, therefore addresses can
1113 // never point into a file section and are always LoadAddresses.
1114 stack.back().SetValueType(Value::ValueType::LoadAddress);
1115 } else {
1116 stack.back().SetValueType(Value::ValueType::FileAddress);
1117 }
1118 break;
1119
1120 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1121 // shared libraries that have a location like:
1122 // DW_OP_addr(0x1000)
1123 // If this address resides in a shared library, then this virtual address
1124 // won't make sense when it is evaluated in the context of a running
1125 // process where shared libraries have been slid. To account for this, this
1126 // new address type where we can store the section pointer and a 4 byte
1127 // offset.
1128 // case DW_OP_addr_sect_offset4:
1129 // {
1130 // result_type = eResultTypeFileAddress;
1131 // lldb::Section *sect = (lldb::Section
1132 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
1133 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1134 //
1135 // Address so_addr (sect, sect_offset);
1136 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
1137 // if (load_addr != LLDB_INVALID_ADDRESS)
1138 // {
1139 // // We successfully resolve a file address to a load
1140 // // address.
1141 // stack.push_back(load_addr);
1142 // break;
1143 // }
1144 // else
1145 // {
1146 // // We were able
1147 // if (error_ptr)
1148 // error_ptr->SetErrorStringWithFormat ("Section %s in
1149 // %s is not currently loaded.\n",
1150 // sect->GetName().AsCString(),
1151 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1152 // return false;
1153 // }
1154 // }
1155 // break;
1156
1157 // OPCODE: DW_OP_deref
1158 // OPERANDS: none
1159 // DESCRIPTION: Pops the top stack entry and treats it as an address.
1160 // The value retrieved from that address is pushed. The size of the data
1161 // retrieved from the dereferenced address is the size of an address on the
1162 // target machine.
1163 case DW_OP_deref: {
1164 size_t size = opcodes.GetAddressByteSize();
1165 if (llvm::Error err = Evaluate_DW_OP_deref_size(
1166 stack, exe_ctx, module_sp, process, target, size, size,
1167 dwarf4_location_description_kind))
1168 return err;
1169 } break;
1170
1171 // OPCODE: DW_OP_deref_size
1172 // OPERANDS: 1
1173 // 1 - uint8_t that specifies the size of the data to dereference.
1174 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1175 // stack entry and treats it as an address. The value retrieved from that
1176 // address is pushed. In the DW_OP_deref_size operation, however, the size
1177 // in bytes of the data retrieved from the dereferenced address is
1178 // specified by the single operand. This operand is a 1-byte unsigned
1179 // integral constant whose value may not be larger than the size of an
1180 // address on the target machine. The data retrieved is zero extended to
1181 // the size of an address on the target machine before being pushed on the
1182 // expression stack.
1183 case DW_OP_deref_size: {
1184 size_t size = opcodes.GetU8(&offset);
1185 if (llvm::Error err = Evaluate_DW_OP_deref_size(
1186 stack, exe_ctx, module_sp, process, target, size,
1187 opcodes.GetAddressByteSize(), dwarf4_location_description_kind))
1188 return err;
1189 } break;
1190
1191 // OPCODE: DW_OP_xderef_size
1192 // OPERANDS: 1
1193 // 1 - uint8_t that specifies the size of the data to dereference.
1194 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1195 // the top of the stack is treated as an address. The second stack entry is
1196 // treated as an "address space identifier" for those architectures that
1197 // support multiple address spaces. The top two stack elements are popped,
1198 // a data item is retrieved through an implementation-defined address
1199 // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1200 // operation, however, the size in bytes of the data retrieved from the
1201 // dereferenced address is specified by the single operand. This operand is
1202 // a 1-byte unsigned integral constant whose value may not be larger than
1203 // the size of an address on the target machine. The data retrieved is zero
1204 // extended to the size of an address on the target machine before being
1205 // pushed on the expression stack.
1206 case DW_OP_xderef_size:
1207 return llvm::createStringError("unimplemented opcode: DW_OP_xderef_size");
1208 // OPCODE: DW_OP_xderef
1209 // OPERANDS: none
1210 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1211 // the top of the stack is treated as an address. The second stack entry is
1212 // treated as an "address space identifier" for those architectures that
1213 // support multiple address spaces. The top two stack elements are popped,
1214 // a data item is retrieved through an implementation-defined address
1215 // calculation and pushed as the new stack top. The size of the data
1216 // retrieved from the dereferenced address is the size of an address on the
1217 // target machine.
1218 case DW_OP_xderef:
1219 return llvm::createStringError("unimplemented opcode: DW_OP_xderef");
1220
1221 // All DW_OP_constXXX opcodes have a single operand as noted below:
1222 //
1223 // Opcode Operand 1
1224 // DW_OP_const1u 1-byte unsigned integer constant
1225 // DW_OP_const1s 1-byte signed integer constant
1226 // DW_OP_const2u 2-byte unsigned integer constant
1227 // DW_OP_const2s 2-byte signed integer constant
1228 // DW_OP_const4u 4-byte unsigned integer constant
1229 // DW_OP_const4s 4-byte signed integer constant
1230 // DW_OP_const8u 8-byte unsigned integer constant
1231 // DW_OP_const8s 8-byte signed integer constant
1232 // DW_OP_constu unsigned LEB128 integer constant
1233 // DW_OP_consts signed LEB128 integer constant
1234 case DW_OP_const1u:
1235 stack.push_back(to_generic(opcodes.GetU8(&offset)));
1236 break;
1237 case DW_OP_const1s:
1238 stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset)));
1239 break;
1240 case DW_OP_const2u:
1241 stack.push_back(to_generic(opcodes.GetU16(&offset)));
1242 break;
1243 case DW_OP_const2s:
1244 stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset)));
1245 break;
1246 case DW_OP_const4u:
1247 stack.push_back(to_generic(opcodes.GetU32(&offset)));
1248 break;
1249 case DW_OP_const4s:
1250 stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset)));
1251 break;
1252 case DW_OP_const8u:
1253 stack.push_back(to_generic(opcodes.GetU64(&offset)));
1254 break;
1255 case DW_OP_const8s:
1256 stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset)));
1257 break;
1258 // These should also use to_generic, but we can't do that due to a
1259 // producer-side bug in llvm. See llvm.org/pr48087.
1260 case DW_OP_constu:
1261 stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1262 break;
1263 case DW_OP_consts:
1264 stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1265 break;
1266
1267 // OPCODE: DW_OP_dup
1268 // OPERANDS: none
1269 // DESCRIPTION: duplicates the value at the top of the stack
1270 case DW_OP_dup:
1271 if (stack.empty()) {
1272 return llvm::createStringError("expression stack empty for DW_OP_dup");
1273 } else
1274 stack.push_back(stack.back());
1275 break;
1276
1277 // OPCODE: DW_OP_drop
1278 // OPERANDS: none
1279 // DESCRIPTION: pops the value at the top of the stack
1280 case DW_OP_drop:
1281 if (stack.empty()) {
1282 return llvm::createStringError("expression stack empty for DW_OP_drop");
1283 } else
1284 stack.pop_back();
1285 break;
1286
1287 // OPCODE: DW_OP_over
1288 // OPERANDS: none
1289 // DESCRIPTION: Duplicates the entry currently second in the stack at
1290 // the top of the stack.
1291 case DW_OP_over:
1292 stack.push_back(stack[stack.size() - 2]);
1293 break;
1294
1295 // OPCODE: DW_OP_pick
1296 // OPERANDS: uint8_t index into the current stack
1297 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1298 // inclusive) is pushed on the stack
1299 case DW_OP_pick: {
1300 uint8_t pick_idx = opcodes.GetU8(&offset);
1301 if (pick_idx < stack.size())
1302 stack.push_back(stack[stack.size() - 1 - pick_idx]);
1303 else {
1304 return llvm::createStringError(
1305 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1306 }
1307 } break;
1308
1309 // OPCODE: DW_OP_swap
1310 // OPERANDS: none
1311 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1312 // of the stack becomes the second stack entry, and the second entry
1313 // becomes the top of the stack
1314 case DW_OP_swap:
1315 tmp = stack.back();
1316 stack.back() = stack[stack.size() - 2];
1317 stack[stack.size() - 2] = tmp;
1318 break;
1319
1320 // OPCODE: DW_OP_rot
1321 // OPERANDS: none
1322 // DESCRIPTION: Rotates the first three stack entries. The entry at
1323 // the top of the stack becomes the third stack entry, the second entry
1324 // becomes the top of the stack, and the third entry becomes the second
1325 // entry.
1326 case DW_OP_rot: {
1327 size_t last_idx = stack.size() - 1;
1328 Value old_top = stack[last_idx];
1329 stack[last_idx] = stack[last_idx - 1];
1330 stack[last_idx - 1] = stack[last_idx - 2];
1331 stack[last_idx - 2] = old_top;
1332 } break;
1333
1334 // OPCODE: DW_OP_abs
1335 // OPERANDS: none
1336 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1337 // value and pushes its absolute value. If the absolute value can not be
1338 // represented, the result is undefined.
1339 case DW_OP_abs:
1340 if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
1341 return llvm::createStringError(
1342 "failed to take the absolute value of the first stack item");
1343 }
1344 break;
1345
1346 // OPCODE: DW_OP_and
1347 // OPERANDS: none
1348 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1349 // operation on the two, and pushes the result.
1350 case DW_OP_and:
1351 tmp = stack.back();
1352 stack.pop_back();
1353 stack.back().ResolveValue(exe_ctx) =
1354 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1355 break;
1356
1357 // OPCODE: DW_OP_div
1358 // OPERANDS: none
1359 // DESCRIPTION: pops the top two stack values, divides the former second
1360 // entry by the former top of the stack using signed division, and pushes
1361 // the result.
1362 case DW_OP_div: {
1363 tmp = stack.back();
1364 if (tmp.ResolveValue(exe_ctx).IsZero())
1365 return llvm::createStringError("divide by zero");
1366
1367 stack.pop_back();
1368 Scalar divisor, dividend;
1369 divisor = tmp.ResolveValue(exe_ctx);
1370 dividend = stack.back().ResolveValue(exe_ctx);
1371 divisor.MakeSigned();
1372 dividend.MakeSigned();
1373 stack.back() = dividend / divisor;
1374
1375 if (!stack.back().ResolveValue(exe_ctx).IsValid())
1376 return llvm::createStringError("divide failed");
1377 } break;
1378
1379 // OPCODE: DW_OP_minus
1380 // OPERANDS: none
1381 // DESCRIPTION: pops the top two stack values, subtracts the former top
1382 // of the stack from the former second entry, and pushes the result.
1383 case DW_OP_minus:
1384 tmp = stack.back();
1385 stack.pop_back();
1386 stack.back().ResolveValue(exe_ctx) =
1387 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1388 break;
1389
1390 // OPCODE: DW_OP_mod
1391 // OPERANDS: none
1392 // DESCRIPTION: pops the top two stack values and pushes the result of
1393 // the calculation: former second stack entry modulo the former top of the
1394 // stack.
1395 case DW_OP_mod:
1396 tmp = stack.back();
1397 stack.pop_back();
1398 stack.back().ResolveValue(exe_ctx) =
1399 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1400 break;
1401
1402 // OPCODE: DW_OP_mul
1403 // OPERANDS: none
1404 // DESCRIPTION: pops the top two stack entries, multiplies them
1405 // together, and pushes the result.
1406 case DW_OP_mul:
1407 tmp = stack.back();
1408 stack.pop_back();
1409 stack.back().ResolveValue(exe_ctx) =
1410 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1411 break;
1412
1413 // OPCODE: DW_OP_neg
1414 // OPERANDS: none
1415 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1416 case DW_OP_neg:
1417 if (!stack.back().ResolveValue(exe_ctx).UnaryNegate())
1418 return llvm::createStringError("unary negate failed");
1419 break;
1420
1421 // OPCODE: DW_OP_not
1422 // OPERANDS: none
1423 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1424 // complement
1425 case DW_OP_not:
1426 if (!stack.back().ResolveValue(exe_ctx).OnesComplement())
1427 return llvm::createStringError("logical NOT failed");
1428 break;
1429
1430 // OPCODE: DW_OP_or
1431 // OPERANDS: none
1432 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1433 // operation on the two, and pushes the result.
1434 case DW_OP_or:
1435 tmp = stack.back();
1436 stack.pop_back();
1437 stack.back().ResolveValue(exe_ctx) =
1438 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
1439 break;
1440
1441 // OPCODE: DW_OP_plus
1442 // OPERANDS: none
1443 // DESCRIPTION: pops the top two stack entries, adds them together, and
1444 // pushes the result.
1445 case DW_OP_plus:
1446 tmp = stack.back();
1447 stack.pop_back();
1448 stack.back().GetScalar() += tmp.GetScalar();
1449 break;
1450
1451 // OPCODE: DW_OP_plus_uconst
1452 // OPERANDS: none
1453 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1454 // constant operand and pushes the result.
1455 case DW_OP_plus_uconst: {
1456 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
1457 // Implicit conversion from a UINT to a Scalar...
1458 stack.back().GetScalar() += uconst_value;
1459 if (!stack.back().GetScalar().IsValid())
1460 return llvm::createStringError("DW_OP_plus_uconst failed");
1461 } break;
1462
1463 // OPCODE: DW_OP_shl
1464 // OPERANDS: none
1465 // DESCRIPTION: pops the top two stack entries, shifts the former
1466 // second entry left by the number of bits specified by the former top of
1467 // the stack, and pushes the result.
1468 case DW_OP_shl:
1469 tmp = stack.back();
1470 stack.pop_back();
1471 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
1472 break;
1473
1474 // OPCODE: DW_OP_shr
1475 // OPERANDS: none
1476 // DESCRIPTION: pops the top two stack entries, shifts the former second
1477 // entry right logically (filling with zero bits) by the number of bits
1478 // specified by the former top of the stack, and pushes the result.
1479 case DW_OP_shr:
1480 tmp = stack.back();
1481 stack.pop_back();
1482 if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
1483 tmp.ResolveValue(exe_ctx)))
1484 return llvm::createStringError("DW_OP_shr failed");
1485 break;
1486
1487 // OPCODE: DW_OP_shra
1488 // OPERANDS: none
1489 // DESCRIPTION: pops the top two stack entries, shifts the former second
1490 // entry right arithmetically (divide the magnitude by 2, keep the same
1491 // sign for the result) by the number of bits specified by the former top
1492 // of the stack, and pushes the result.
1493 case DW_OP_shra:
1494 tmp = stack.back();
1495 stack.pop_back();
1496 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
1497 break;
1498
1499 // OPCODE: DW_OP_xor
1500 // OPERANDS: none
1501 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1502 // exclusive-or operation on the two, and pushes the result.
1503 case DW_OP_xor:
1504 tmp = stack.back();
1505 stack.pop_back();
1506 stack.back().ResolveValue(exe_ctx) =
1507 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
1508 break;
1509
1510 // OPCODE: DW_OP_skip
1511 // OPERANDS: int16_t
1512 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1513 // signed integer constant. The 2-byte constant is the number of bytes of
1514 // the DWARF expression to skip forward or backward from the current
1515 // operation, beginning after the 2-byte constant.
1516 case DW_OP_skip: {
1517 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1518 lldb::offset_t new_offset = offset + skip_offset;
1519 // New offset can point at the end of the data, in this case we should
1520 // terminate the DWARF expression evaluation (will happen in the loop
1521 // condition).
1522 if (new_offset <= opcodes.GetByteSize())
1523 offset = new_offset;
1524 else {
1525 return llvm::createStringErrorV(
1526 "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}", offset,
1527 skip_offset, opcodes.GetByteSize());
1528 }
1529 } break;
1530
1531 // OPCODE: DW_OP_bra
1532 // OPERANDS: int16_t
1533 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1534 // signed integer constant. This operation pops the top of stack. If the
1535 // value popped is not the constant 0, the 2-byte constant operand is the
1536 // number of bytes of the DWARF expression to skip forward or backward from
1537 // the current operation, beginning after the 2-byte constant.
1538 case DW_OP_bra: {
1539 tmp = stack.back();
1540 stack.pop_back();
1541 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1542 Scalar zero(0);
1543 if (tmp.ResolveValue(exe_ctx) != zero) {
1544 lldb::offset_t new_offset = offset + bra_offset;
1545 // New offset can point at the end of the data, in this case we should
1546 // terminate the DWARF expression evaluation (will happen in the loop
1547 // condition).
1548 if (new_offset <= opcodes.GetByteSize())
1549 offset = new_offset;
1550 else {
1551 return llvm::createStringErrorV(
1552 "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}", offset,
1553 bra_offset, opcodes.GetByteSize());
1554 }
1555 }
1556 } break;
1557
1558 // OPCODE: DW_OP_eq
1559 // OPERANDS: none
1560 // DESCRIPTION: pops the top two stack values, compares using the
1561 // equals (==) operator.
1562 // STACK RESULT: push the constant value 1 onto the stack if the result
1563 // of the operation is true or the constant value 0 if the result of the
1564 // operation is false.
1565 case DW_OP_eq:
1566 tmp = stack.back();
1567 stack.pop_back();
1568 stack.back().ResolveValue(exe_ctx) =
1569 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
1570 break;
1571
1572 // OPCODE: DW_OP_ge
1573 // OPERANDS: none
1574 // DESCRIPTION: pops the top two stack values, compares using the
1575 // greater than or equal to (>=) operator.
1576 // STACK RESULT: push the constant value 1 onto the stack if the result
1577 // of the operation is true or the constant value 0 if the result of the
1578 // operation is false.
1579 case DW_OP_ge:
1580 tmp = stack.back();
1581 stack.pop_back();
1582 stack.back().ResolveValue(exe_ctx) =
1583 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
1584 break;
1585
1586 // OPCODE: DW_OP_gt
1587 // OPERANDS: none
1588 // DESCRIPTION: pops the top two stack values, compares using the
1589 // greater than (>) operator.
1590 // STACK RESULT: push the constant value 1 onto the stack if the result
1591 // of the operation is true or the constant value 0 if the result of the
1592 // operation is false.
1593 case DW_OP_gt:
1594 tmp = stack.back();
1595 stack.pop_back();
1596 stack.back().ResolveValue(exe_ctx) =
1597 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
1598 break;
1599
1600 // OPCODE: DW_OP_le
1601 // OPERANDS: none
1602 // DESCRIPTION: pops the top two stack values, compares using the
1603 // less than or equal to (<=) operator.
1604 // STACK RESULT: push the constant value 1 onto the stack if the result
1605 // of the operation is true or the constant value 0 if the result of the
1606 // operation is false.
1607 case DW_OP_le:
1608 tmp = stack.back();
1609 stack.pop_back();
1610 stack.back().ResolveValue(exe_ctx) =
1611 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
1612 break;
1613
1614 // OPCODE: DW_OP_lt
1615 // OPERANDS: none
1616 // DESCRIPTION: pops the top two stack values, compares using the
1617 // less than (<) operator.
1618 // STACK RESULT: push the constant value 1 onto the stack if the result
1619 // of the operation is true or the constant value 0 if the result of the
1620 // operation is false.
1621 case DW_OP_lt:
1622 tmp = stack.back();
1623 stack.pop_back();
1624 stack.back().ResolveValue(exe_ctx) =
1625 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
1626 break;
1627
1628 // OPCODE: DW_OP_ne
1629 // OPERANDS: none
1630 // DESCRIPTION: pops the top two stack values, compares using the
1631 // not equal (!=) operator.
1632 // STACK RESULT: push the constant value 1 onto the stack if the result
1633 // of the operation is true or the constant value 0 if the result of the
1634 // operation is false.
1635 case DW_OP_ne:
1636 tmp = stack.back();
1637 stack.pop_back();
1638 stack.back().ResolveValue(exe_ctx) =
1639 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
1640 break;
1641
1642 // OPCODE: DW_OP_litn
1643 // OPERANDS: none
1644 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1645 // STACK RESULT: push the unsigned literal constant value onto the top
1646 // of the stack.
1647 case DW_OP_lit0:
1648 case DW_OP_lit1:
1649 case DW_OP_lit2:
1650 case DW_OP_lit3:
1651 case DW_OP_lit4:
1652 case DW_OP_lit5:
1653 case DW_OP_lit6:
1654 case DW_OP_lit7:
1655 case DW_OP_lit8:
1656 case DW_OP_lit9:
1657 case DW_OP_lit10:
1658 case DW_OP_lit11:
1659 case DW_OP_lit12:
1660 case DW_OP_lit13:
1661 case DW_OP_lit14:
1662 case DW_OP_lit15:
1663 case DW_OP_lit16:
1664 case DW_OP_lit17:
1665 case DW_OP_lit18:
1666 case DW_OP_lit19:
1667 case DW_OP_lit20:
1668 case DW_OP_lit21:
1669 case DW_OP_lit22:
1670 case DW_OP_lit23:
1671 case DW_OP_lit24:
1672 case DW_OP_lit25:
1673 case DW_OP_lit26:
1674 case DW_OP_lit27:
1675 case DW_OP_lit28:
1676 case DW_OP_lit29:
1677 case DW_OP_lit30:
1678 case DW_OP_lit31:
1679 stack.push_back(to_generic(op - DW_OP_lit0));
1680 break;
1681
1682 // OPCODE: DW_OP_regN
1683 // OPERANDS: none
1684 // DESCRIPTION: Push the value in register n on the top of the stack.
1685 case DW_OP_reg0:
1686 case DW_OP_reg1:
1687 case DW_OP_reg2:
1688 case DW_OP_reg3:
1689 case DW_OP_reg4:
1690 case DW_OP_reg5:
1691 case DW_OP_reg6:
1692 case DW_OP_reg7:
1693 case DW_OP_reg8:
1694 case DW_OP_reg9:
1695 case DW_OP_reg10:
1696 case DW_OP_reg11:
1697 case DW_OP_reg12:
1698 case DW_OP_reg13:
1699 case DW_OP_reg14:
1700 case DW_OP_reg15:
1701 case DW_OP_reg16:
1702 case DW_OP_reg17:
1703 case DW_OP_reg18:
1704 case DW_OP_reg19:
1705 case DW_OP_reg20:
1706 case DW_OP_reg21:
1707 case DW_OP_reg22:
1708 case DW_OP_reg23:
1709 case DW_OP_reg24:
1710 case DW_OP_reg25:
1711 case DW_OP_reg26:
1712 case DW_OP_reg27:
1713 case DW_OP_reg28:
1714 case DW_OP_reg29:
1715 case DW_OP_reg30:
1716 case DW_OP_reg31: {
1717 dwarf4_location_description_kind = Register;
1718 reg_num = op - DW_OP_reg0;
1719
1720 if (llvm::Error err =
1721 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1722 return err;
1723 stack.push_back(tmp);
1724 } break;
1725 // OPCODE: DW_OP_regx
1726 // OPERANDS:
1727 // ULEB128 literal operand that encodes the register.
1728 // DESCRIPTION: Push the value in register on the top of the stack.
1729 case DW_OP_regx: {
1730 dwarf4_location_description_kind = Register;
1731 reg_num = opcodes.GetULEB128(&offset);
1732 Status read_err;
1733 if (llvm::Error err =
1734 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1735 return err;
1736 stack.push_back(tmp);
1737 } break;
1738
1739 // OPCODE: DW_OP_bregN
1740 // OPERANDS:
1741 // SLEB128 offset from register N
1742 // DESCRIPTION: Value is in memory at the address specified by register
1743 // N plus an offset.
1744 case DW_OP_breg0:
1745 case DW_OP_breg1:
1746 case DW_OP_breg2:
1747 case DW_OP_breg3:
1748 case DW_OP_breg4:
1749 case DW_OP_breg5:
1750 case DW_OP_breg6:
1751 case DW_OP_breg7:
1752 case DW_OP_breg8:
1753 case DW_OP_breg9:
1754 case DW_OP_breg10:
1755 case DW_OP_breg11:
1756 case DW_OP_breg12:
1757 case DW_OP_breg13:
1758 case DW_OP_breg14:
1759 case DW_OP_breg15:
1760 case DW_OP_breg16:
1761 case DW_OP_breg17:
1762 case DW_OP_breg18:
1763 case DW_OP_breg19:
1764 case DW_OP_breg20:
1765 case DW_OP_breg21:
1766 case DW_OP_breg22:
1767 case DW_OP_breg23:
1768 case DW_OP_breg24:
1769 case DW_OP_breg25:
1770 case DW_OP_breg26:
1771 case DW_OP_breg27:
1772 case DW_OP_breg28:
1773 case DW_OP_breg29:
1774 case DW_OP_breg30:
1775 case DW_OP_breg31: {
1776 reg_num = op - DW_OP_breg0;
1777 if (llvm::Error err =
1778 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1779 return err;
1780
1781 int64_t breg_offset = opcodes.GetSLEB128(&offset);
1782 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
1783 tmp.ClearContext();
1784 stack.push_back(tmp);
1785 stack.back().SetValueType(Value::ValueType::LoadAddress);
1786 } break;
1787 // OPCODE: DW_OP_bregx
1788 // OPERANDS: 2
1789 // ULEB128 literal operand that encodes the register.
1790 // SLEB128 offset from register N
1791 // DESCRIPTION: Value is in memory at the address specified by register
1792 // N plus an offset.
1793 case DW_OP_bregx: {
1794 reg_num = opcodes.GetULEB128(&offset);
1795 if (llvm::Error err =
1796 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1797 return err;
1798
1799 int64_t breg_offset = opcodes.GetSLEB128(&offset);
1800 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
1801 tmp.ClearContext();
1802 stack.push_back(tmp);
1803 stack.back().SetValueType(Value::ValueType::LoadAddress);
1804 } break;
1805
1806 case DW_OP_fbreg:
1807 if (exe_ctx) {
1808 if (frame) {
1809 Scalar value;
1810 if (llvm::Error err = frame->GetFrameBaseValue(value))
1811 return err;
1812 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
1813 value += fbreg_offset;
1814 stack.push_back(value);
1815 stack.back().SetValueType(Value::ValueType::LoadAddress);
1816 } else {
1817 return llvm::createStringError(
1818 "invalid stack frame in context for DW_OP_fbreg opcode");
1819 }
1820 } else {
1821 return llvm::createStringError(
1822 "NULL execution context for DW_OP_fbreg");
1823 }
1824
1825 break;
1826
1827 // OPCODE: DW_OP_nop
1828 // OPERANDS: none
1829 // DESCRIPTION: A place holder. It has no effect on the location stack
1830 // or any of its values.
1831 case DW_OP_nop:
1832 break;
1833
1834 // OPCODE: DW_OP_piece
1835 // OPERANDS: 1
1836 // ULEB128: byte size of the piece
1837 // DESCRIPTION: The operand describes the size in bytes of the piece of
1838 // the object referenced by the DWARF expression whose result is at the top
1839 // of the stack. If the piece is located in a register, but does not occupy
1840 // the entire register, the placement of the piece within that register is
1841 // defined by the ABI.
1842 //
1843 // Many compilers store a single variable in sets of registers, or store a
1844 // variable partially in memory and partially in registers. DW_OP_piece
1845 // provides a way of describing how large a part of a variable a particular
1846 // DWARF expression refers to.
1847 case DW_OP_piece: {
1848 LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind;
1849 // Reset for the next piece.
1850 dwarf4_location_description_kind = Memory;
1851
1852 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
1853
1854 if (piece_byte_size > 0) {
1855 Value curr_piece;
1856
1857 if (stack.empty()) {
1858 UpdateValueTypeFromLocationDescription(
1859 log, dwarf_cu, LocationDescriptionKind::Empty);
1860 // In a multi-piece expression, this means that the current piece is
1861 // not available. Fill with zeros for now by resizing the data and
1862 // appending it
1863 curr_piece.ResizeData(piece_byte_size);
1864 // Note that "0" is not a correct value for the unknown bits.
1865 // It would be better to also return a mask of valid bits together
1866 // with the expression result, so the debugger can print missing
1867 // members as "<optimized out>" or something.
1868 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
1869 pieces.AppendDataToHostBuffer(curr_piece);
1870 } else {
1871 Status error;
1872 // Extract the current piece into "curr_piece"
1873 Value curr_piece_source_value(stack.back());
1874 stack.pop_back();
1875 UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc,
1876 &curr_piece_source_value);
1877
1878 const Value::ValueType curr_piece_source_value_type =
1879 curr_piece_source_value.GetValueType();
1880 Scalar &scalar = curr_piece_source_value.GetScalar();
1882 switch (curr_piece_source_value_type) {
1884 return llvm::createStringError("invalid value type");
1886 if (target) {
1887 curr_piece_source_value.ConvertToLoadAddress(module_sp.get(),
1888 target);
1889 addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1890 } else {
1891 return llvm::createStringError(
1892 "unable to convert file address 0x%" PRIx64
1893 " to load address "
1894 "for DW_OP_piece(%" PRIu64 "): "
1895 "no target available",
1896 addr, piece_byte_size);
1897 }
1898 [[fallthrough]];
1900 if (target) {
1901 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
1902 if (target->ReadMemory(addr, curr_piece.GetBuffer().GetBytes(),
1903 piece_byte_size, error,
1904 /*force_live_memory=*/false) !=
1905 piece_byte_size) {
1906 const char *addr_type = (curr_piece_source_value_type ==
1908 ? "load"
1909 : "file";
1910 return llvm::createStringError(
1911 "failed to read memory DW_OP_piece(%" PRIu64
1912 ") from %s address 0x%" PRIx64,
1913 piece_byte_size, addr_type, addr);
1914 }
1915 } else {
1916 return llvm::createStringError(
1917 "failed to resize the piece memory buffer for "
1918 "DW_OP_piece(%" PRIu64 ")",
1919 piece_byte_size);
1920 }
1921 }
1922 } break;
1924 return llvm::createStringError(
1925 "failed to read memory DW_OP_piece(%" PRIu64
1926 ") from host address 0x%" PRIx64,
1927 piece_byte_size, addr);
1928 } break;
1929
1931 uint32_t bit_size = piece_byte_size * 8;
1932 uint32_t bit_offset = 0;
1933 if (!scalar.ExtractBitfield(bit_size, bit_offset)) {
1934 return llvm::createStringError(
1935 "unable to extract %" PRIu64 " bytes from a %" PRIu64
1936 " byte scalar value.",
1937 piece_byte_size,
1938 (uint64_t)curr_piece_source_value.GetScalar().GetByteSize());
1939 }
1940
1941 // We have seen a case where we have expression like:
1942 // DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x28
1943 // here we are assuming the compiler was trying to zero
1944 // extend the value that we should append to the buffer.
1945 scalar.TruncOrExtendTo(bit_size, /*sign=*/false);
1946 curr_piece.GetScalar() = scalar;
1947 } break;
1948 }
1949
1950 // Check if this is the first piece?
1951 if (op_piece_offset == 0) {
1952 // This is the first piece, we should push it back onto the stack
1953 // so subsequent pieces will be able to access this piece and add
1954 // to it.
1955 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
1956 return llvm::createStringError("failed to append piece data");
1957 }
1958 } else {
1959 // If this is the second or later piece there should be a value on
1960 // the stack.
1961 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
1962 return llvm::createStringError(
1963 "DW_OP_piece for offset %" PRIu64
1964 " but top of stack is of size %" PRIu64,
1965 op_piece_offset, pieces.GetBuffer().GetByteSize());
1966 }
1967
1968 if (pieces.AppendDataToHostBuffer(curr_piece) == 0)
1969 return llvm::createStringError("failed to append piece data");
1970 }
1971 }
1972 op_piece_offset += piece_byte_size;
1973 }
1974 } break;
1975
1976 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
1977 if (stack.size() < 1) {
1978 UpdateValueTypeFromLocationDescription(log, dwarf_cu,
1979 LocationDescriptionKind::Empty);
1980 // Reset for the next piece.
1981 dwarf4_location_description_kind = Memory;
1982 return llvm::createStringError(
1983 "expression stack needs at least 1 item for DW_OP_bit_piece");
1984 } else {
1985 UpdateValueTypeFromLocationDescription(
1986 log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
1987 // Reset for the next piece.
1988 dwarf4_location_description_kind = Memory;
1989 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
1990 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
1991 switch (stack.back().GetValueType()) {
1993 return llvm::createStringError(
1994 "unable to extract bit value from invalid value");
1996 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
1997 piece_bit_offset)) {
1998 return llvm::createStringError(
1999 "unable to extract %" PRIu64 " bit value with %" PRIu64
2000 " bit offset from a %" PRIu64 " bit scalar value.",
2001 piece_bit_size, piece_bit_offset,
2002 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2003 }
2004 } break;
2005
2009 return llvm::createStringError(
2010 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2011 ", bit_offset = %" PRIu64 ") from an address value.",
2012 piece_bit_size, piece_bit_offset);
2013 }
2014 }
2015 break;
2016
2017 // OPCODE: DW_OP_implicit_value
2018 // OPERANDS: 2
2019 // ULEB128 size of the value block in bytes
2020 // uint8_t* block bytes encoding value in target's memory
2021 // representation
2022 // DESCRIPTION: Value is immediately stored in block in the debug info with
2023 // the memory representation of the target.
2024 case DW_OP_implicit_value: {
2025 dwarf4_location_description_kind = Implicit;
2026
2027 const uint32_t len = opcodes.GetULEB128(&offset);
2028 const void *data = opcodes.GetData(&offset, len);
2029
2030 if (!data) {
2031 LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data");
2032 return llvm::createStringError("could not evaluate %s",
2034 }
2035
2036 Value result(data, len);
2037 stack.push_back(result);
2038 break;
2039 }
2040
2041 case DW_OP_implicit_pointer: {
2042 dwarf4_location_description_kind = Implicit;
2043 return llvm::createStringError("could not evaluate %s",
2045 }
2046
2047 // OPCODE: DW_OP_push_object_address
2048 // OPERANDS: none
2049 // DESCRIPTION: Pushes the address of the object currently being
2050 // evaluated as part of evaluation of a user presented expression. This
2051 // object may correspond to an independent variable described by its own
2052 // DIE or it may be a component of an array, structure, or class whose
2053 // address has been dynamically determined by an earlier step during user
2054 // expression evaluation.
2055 case DW_OP_push_object_address:
2056 if (object_address_ptr)
2057 stack.push_back(*object_address_ptr);
2058 else {
2059 return llvm::createStringError("DW_OP_push_object_address used without "
2060 "specifying an object address");
2061 }
2062 break;
2063
2064 // OPCODE: DW_OP_call2
2065 // OPERANDS:
2066 // uint16_t compile unit relative offset of a DIE
2067 // DESCRIPTION: Performs subroutine calls during evaluation
2068 // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2069 // debugging information entry in the current compilation unit.
2070 //
2071 // Operand interpretation is exactly like that for DW_FORM_ref2.
2072 //
2073 // This operation transfers control of DWARF expression evaluation to the
2074 // DW_AT_location attribute of the referenced DIE. If there is no such
2075 // attribute, then there is no effect. Execution of the DWARF expression of
2076 // a DW_AT_location attribute may add to and/or remove from values on the
2077 // stack. Execution returns to the point following the call when the end of
2078 // the attribute is reached. Values on the stack at the time of the call
2079 // may be used as parameters by the called expression and values left on
2080 // the stack by the called expression may be used as return values by prior
2081 // agreement between the calling and called expressions.
2082 case DW_OP_call2:
2083 return llvm::createStringError("unimplemented opcode DW_OP_call2");
2084 // OPCODE: DW_OP_call4
2085 // OPERANDS: 1
2086 // uint32_t compile unit relative offset of a DIE
2087 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2088 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2089 // a debugging information entry in the current compilation unit.
2090 //
2091 // Operand interpretation DW_OP_call4 is exactly like that for
2092 // DW_FORM_ref4.
2093 //
2094 // This operation transfers control of DWARF expression evaluation to the
2095 // DW_AT_location attribute of the referenced DIE. If there is no such
2096 // attribute, then there is no effect. Execution of the DWARF expression of
2097 // a DW_AT_location attribute may add to and/or remove from values on the
2098 // stack. Execution returns to the point following the call when the end of
2099 // the attribute is reached. Values on the stack at the time of the call
2100 // may be used as parameters by the called expression and values left on
2101 // the stack by the called expression may be used as return values by prior
2102 // agreement between the calling and called expressions.
2103 case DW_OP_call4:
2104 return llvm::createStringError("unimplemented opcode DW_OP_call4");
2105
2106 // OPCODE: DW_OP_stack_value
2107 // OPERANDS: None
2108 // DESCRIPTION: Specifies that the object does not exist in memory but
2109 // rather is a constant value. The value from the top of the stack is the
2110 // value to be used. This is the actual object value and not the location.
2111 case DW_OP_stack_value:
2112 dwarf4_location_description_kind = Implicit;
2113 stack.back().SetValueType(Value::ValueType::Scalar);
2114 break;
2115
2116 // OPCODE: DW_OP_convert
2117 // OPERANDS: 1
2118 // A ULEB128 that is either a DIE offset of a
2119 // DW_TAG_base_type or 0 for the generic (pointer-sized) type.
2120 //
2121 // DESCRIPTION: Pop the top stack element, convert it to a
2122 // different type, and push the result.
2123 case DW_OP_convert: {
2124 const uint64_t relative_die_offset = opcodes.GetULEB128(&offset);
2125 uint64_t bit_size;
2126 bool sign;
2127 if (relative_die_offset == 0) {
2128 // The generic type has the size of an address on the target
2129 // machine and an unspecified signedness. Scalar has no
2130 // "unspecified signedness", so we use unsigned types.
2131 if (!module_sp)
2132 return llvm::createStringError("no module");
2133 sign = false;
2134 bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
2135 if (!bit_size)
2136 return llvm::createStringError("unspecified architecture");
2137 } else {
2138 auto bit_size_sign_or_err =
2139 dwarf_cu->GetDIEBitSizeAndSign(relative_die_offset);
2140 if (!bit_size_sign_or_err)
2141 return bit_size_sign_or_err.takeError();
2142 bit_size = bit_size_sign_or_err->first;
2143 sign = bit_size_sign_or_err->second;
2144 }
2145 Scalar &top = stack.back().ResolveValue(exe_ctx);
2146 top.TruncOrExtendTo(bit_size, sign);
2147 break;
2148 }
2149
2150 // OPCODE: DW_OP_call_frame_cfa
2151 // OPERANDS: None
2152 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2153 // the canonical frame address consistent with the call frame information
2154 // located in .debug_frame (or in the FDEs of the eh_frame section).
2155 case DW_OP_call_frame_cfa:
2156 if (frame) {
2157 // Note that we don't have to parse FDEs because this DWARF expression
2158 // is commonly evaluated with a valid stack frame.
2159 StackID id = frame->GetStackID();
2160 addr_t cfa = id.GetCallFrameAddressWithMetadata();
2161 if (cfa != LLDB_INVALID_ADDRESS) {
2162 stack.push_back(Scalar(cfa));
2163 stack.back().SetValueType(Value::ValueType::LoadAddress);
2164 } else {
2165 return llvm::createStringError(
2166 "stack frame does not include a canonical "
2167 "frame address for DW_OP_call_frame_cfa "
2168 "opcode");
2169 }
2170 } else {
2171 return llvm::createStringError("unvalid stack frame in context for "
2172 "DW_OP_call_frame_cfa opcode");
2173 }
2174 break;
2175
2176 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2177 // opcode, DW_OP_GNU_push_tls_address)
2178 // OPERANDS: none
2179 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2180 // an address in the current thread's thread-local storage block, and
2181 // pushes it on the stack.
2182 case DW_OP_form_tls_address:
2183 case DW_OP_GNU_push_tls_address: {
2184 if (stack.size() < 1) {
2185 if (op == DW_OP_form_tls_address)
2186 return llvm::createStringError(
2187 "DW_OP_form_tls_address needs an argument");
2188 else
2189 return llvm::createStringError(
2190 "DW_OP_GNU_push_tls_address needs an argument");
2191 }
2192
2193 if (!exe_ctx || !module_sp)
2194 return llvm::createStringError("no context to evaluate TLS within");
2195
2196 Thread *thread = exe_ctx->GetThreadPtr();
2197 if (!thread)
2198 return llvm::createStringError("no thread to evaluate TLS within");
2199
2200 // Lookup the TLS block address for this thread and module.
2201 const addr_t tls_file_addr =
2202 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2203 const addr_t tls_load_addr =
2204 thread->GetThreadLocalData(module_sp, tls_file_addr);
2205
2206 if (tls_load_addr == LLDB_INVALID_ADDRESS)
2207 return llvm::createStringError(
2208 "no TLS data currently exists for this thread");
2209
2210 stack.back().GetScalar() = tls_load_addr;
2211 stack.back().SetValueType(Value::ValueType::LoadAddress);
2212 } break;
2213
2214 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
2215 // OPERANDS: 1
2216 // ULEB128: index to the .debug_addr section
2217 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2218 // section with the base address specified by the DW_AT_addr_base attribute
2219 // and the 0 based index is the ULEB128 encoded index.
2220 case DW_OP_addrx:
2221 case DW_OP_GNU_addr_index: {
2222 if (!dwarf_cu)
2223 return llvm::createStringError("DW_OP_GNU_addr_index found without a "
2224 "compile unit being specified");
2225 uint64_t index = opcodes.GetULEB128(&offset);
2226 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2227 stack.push_back(Scalar(value));
2228 if (target &&
2230 // wasm file sections aren't mapped into memory, therefore addresses can
2231 // never point into a file section and are always LoadAddresses.
2232 stack.back().SetValueType(Value::ValueType::LoadAddress);
2233 } else {
2234 stack.back().SetValueType(Value::ValueType::FileAddress);
2235 }
2236 } break;
2237
2238 // OPCODE: DW_OP_GNU_const_index
2239 // OPERANDS: 1
2240 // ULEB128: index to the .debug_addr section
2241 // DESCRIPTION: Pushes an constant with the size of a machine address to
2242 // the stack from the .debug_addr section with the base address specified
2243 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2244 // encoded index.
2245 case DW_OP_GNU_const_index: {
2246 if (!dwarf_cu) {
2247 return llvm::createStringError("DW_OP_GNU_const_index found without a "
2248 "compile unit being specified");
2249 }
2250 uint64_t index = opcodes.GetULEB128(&offset);
2251 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2252 stack.push_back(Scalar(value));
2253 } break;
2254
2255 case DW_OP_GNU_entry_value:
2256 case DW_OP_entry_value: {
2257 if (llvm::Error err = Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx,
2258 opcodes, offset, log))
2259 return llvm::createStringError(
2260 "could not evaluate DW_OP_entry_value: %s",
2261 llvm::toString(std::move(err)).c_str());
2262 break;
2263 }
2264
2265 default:
2266 if (dwarf_cu) {
2267 if (dwarf_cu->ParseVendorDWARFOpcode(op, opcodes, offset, reg_ctx,
2268 reg_kind, stack)) {
2269 break;
2270 }
2271 }
2272 return llvm::createStringErrorV("Unhandled opcode {0} in DWARFExpression",
2273 LocationAtom(op));
2274 }
2275 }
2276
2277 if (stack.empty()) {
2278 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2279 // or DW_OP_bit_piece opcodes
2280 if (pieces.GetBuffer().GetByteSize())
2281 return pieces;
2282
2283 return llvm::createStringError("stack empty after evaluation");
2284 }
2285
2286 UpdateValueTypeFromLocationDescription(
2287 log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
2288
2289 if (log && log->GetVerbose()) {
2290 size_t count = stack.size();
2291 LLDB_LOGF(log,
2292 "Stack after operation has %" PRIu64 " values:", (uint64_t)count);
2293 for (size_t i = 0; i < count; ++i) {
2294 StreamString new_value;
2295 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2296 stack[i].Dump(&new_value);
2297 LLDB_LOGF(log, " %s", new_value.GetData());
2298 }
2299 }
2300 return stack.back();
2301}
2302
2304 StackFrame &frame, const Instruction::Operand &operand) const {
2305 using namespace OperandMatchers;
2306
2307 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
2308 if (!reg_ctx_sp) {
2309 return false;
2310 }
2311
2312 DataExtractor opcodes(m_data);
2313
2314 lldb::offset_t op_offset = 0;
2315 uint8_t opcode = opcodes.GetU8(&op_offset);
2316
2317 if (opcode == DW_OP_fbreg) {
2318 int64_t offset = opcodes.GetSLEB128(&op_offset);
2319
2320 DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr);
2321 if (!fb_expr) {
2322 return false;
2323 }
2324
2325 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
2326 return fb_expr->MatchesOperand(frame, child);
2327 };
2328
2329 if (!offset &&
2330 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2331 recurse)(operand)) {
2332 return true;
2333 }
2334
2335 return MatchUnaryOp(
2337 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2338 MatchImmOp(offset), recurse))(operand);
2339 }
2340
2341 bool dereference = false;
2342 const RegisterInfo *reg = nullptr;
2343 int64_t offset = 0;
2344
2345 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
2346 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
2347 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
2348 offset = opcodes.GetSLEB128(&op_offset);
2349 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
2350 } else if (opcode == DW_OP_regx) {
2351 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2352 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2353 } else if (opcode == DW_OP_bregx) {
2354 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2355 offset = opcodes.GetSLEB128(&op_offset);
2356 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2357 } else {
2358 return false;
2359 }
2360
2361 if (!reg) {
2362 return false;
2363 }
2364
2365 if (dereference) {
2366 if (!offset &&
2367 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2368 MatchRegOp(*reg))(operand)) {
2369 return true;
2370 }
2371
2372 return MatchUnaryOp(
2374 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2375 MatchRegOp(*reg), MatchImmOp(offset)))(operand);
2376 } else {
2377 return MatchRegOp(*reg)(operand);
2378 }
2379}
static llvm::raw_ostream & error(Stream &strm)
static llvm::Expected< lldb::addr_t > ResolveLoadAddress(ExecutionContext *exe_ctx, lldb::ModuleSP &module_sp, const char *dw_op_type, lldb::addr_t file_addr, Address &so_addr, bool check_sectionoffset=false)
Helper function to move common code used to resolve a file address and turn into a load address.
static const char * DW_OP_value_to_name(uint32_t val)
static llvm::Error Evaluate_DW_OP_deref_size(DWARFExpression::Stack &stack, ExecutionContext *exe_ctx, lldb::ModuleSP module_sp, Process *process, Target *target, uint8_t size, size_t size_addr_bytes, LocationDescriptionKind &dwarf4_location_description_kind)
static lldb::offset_t GetOpcodeDataSize(const DataExtractor &data, const lldb::offset_t data_offset, const LocationAtom op, const DWARFExpression::Delegate *dwarf_cu)
Return the length in bytes of the set of operands for op.
static llvm::Error Evaluate_DW_OP_entry_value(DWARFExpression::Stack &stack, ExecutionContext *exe_ctx, RegisterContext *reg_ctx, const DataExtractor &opcodes, lldb::offset_t &opcode_offset, Log *log)
static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes, size_t size_addr_bytes, ByteOrder byte_order, size_t size)
Helper function to load sized data from a uint8_t buffer.
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:369
#define LLDB_LOGF(log,...)
Definition Log.h:376
@ Empty
If the Mangled object has neither a mangled name or demangled name we can encode the object with one ...
Definition Mangled.cpp:457
llvm::MCRegisterInfo & GetMCRegisterInfo()
Definition ABI.h:144
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:301
bool IsSectionOffset() const
Check if an address is section offset.
Definition Address.h:342
Core GetCore() const
Definition ArchSpec.h:447
Represent a call made within a Function.
Definition Function.h:268
virtual Function * GetCallee(ModuleList &images, ExecutionContext &exe_ctx)=0
Get the callee's definition.
llvm::ArrayRef< CallSiteParameter > GetCallSiteParameters() const
Get the call site parameters available at this call edge.
Definition Function.h:297
"lldb/Expression/DWARFExpressionList.h" Encapsulates a range map from file address range to a single ...
bool MatchesOperand(StackFrame &frame, const Instruction::Operand &operand) const
llvm::Expected< Value > Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx, lldb::addr_t func_load_addr, const Value *initial_value_ptr, const Value *object_address_ptr) const
virtual uint16_t GetVersion() const =0
virtual bool ParseVendorDWARFOpcode(uint8_t op, const DataExtractor &opcodes, lldb::offset_t &offset, RegisterContext *reg_ctx, lldb::RegisterKind reg_kind, Stack &stack) const =0
virtual lldb::offset_t GetVendorDWARFOpcodeSize(const DataExtractor &data, const lldb::offset_t data_offset, const uint8_t op) const =0
virtual dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const =0
virtual llvm::Expected< std::pair< uint64_t, bool > > GetDIEBitSizeAndSign(uint64_t relative_die_offset) const =0
virtual uint8_t GetAddressByteSize() const =0
DataExtractor m_data
A data extractor capable of reading opcode bytes.
llvm::Expected< lldb::addr_t > GetLocation_DW_OP_addr(const Delegate *dwarf_cu) const
Return the address specified by the first DW_OP_{addr, addrx, GNU_addr_index} in the operation stream...
void UpdateValue(uint64_t const_value, lldb::offset_t const_value_byte_size, uint8_t addr_byte_size)
static llvm::Expected< Value > Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx, lldb::ModuleSP module_sp, const DataExtractor &opcodes, const Delegate *dwarf_cu, const lldb::RegisterKind reg_set, const Value *initial_value_ptr, const Value *object_address_ptr)
Evaluate a DWARF location expression in a particular context.
bool ContainsThreadLocalStorage(const Delegate *dwarf_cu) const
bool LinkThreadLocalStorage(const Delegate *dwarf_cu, std::function< lldb::addr_t(lldb::addr_t file_addr)> const &link_address_callback)
lldb::RegisterKind m_reg_kind
One of the defines that starts with LLDB_REGKIND_.
bool Update_DW_OP_addr(const Delegate *dwarf_cu, lldb::addr_t file_addr)
void SetRegisterKind(lldb::RegisterKind reg_kind)
Set the call-frame-info style register kind.
static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx, lldb::RegisterKind reg_kind, uint32_t reg_num, Value &value)
bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op) const
void DumpLocation(Stream *s, lldb::DescriptionLevel level, ABI *abi, llvm::DIDumpOptions options={}) const
lldb::RegisterKind GetRegisterKind() const
Return the call-frame-info style register kind.
bool IsValid() const
Return true if the location expression contains data.
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
An binary data encoding class.
Definition DataEncoder.h:42
std::shared_ptr< lldb_private::DataBufferHeap > GetDataBuffer()
Get a shared copy of the heap based memory buffer owned by this object.
uint32_t PutUnsigned(uint32_t offset, uint32_t byte_size, uint64_t value)
Encode an unsigned integer of size byte_size to offset.
uint32_t PutAddress(uint32_t offset, lldb::addr_t addr)
Encode an address in the existing buffer at offset bytes into the buffer.
void AppendAddress(lldb::addr_t addr)
Append an address sized integer to the end of the owned data.
void AppendU8(uint8_t value)
Append a unsigned integer to the end of the owned data.
void AppendData(llvm::StringRef data)
Append a bytes to the end of the owned data.
An data extractor class.
uint64_t GetULEB128(lldb::offset_t *offset_ptr) const
Extract a unsigned LEB128 value from *offset_ptr.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const
Skip an LEB128 number at *offset_ptr.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
bool ValidOffset(lldb::offset_t offset) const
Test the validity of offset.
uint32_t GetAddressByteSize() const
Get the current address size.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
int64_t GetSLEB128(lldb::offset_t *offset_ptr) const
Extract a signed LEB128 value from *offset_ptr.
lldb::offset_t BytesLeft(lldb::offset_t offset) const
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void SetFrameSP(const lldb::StackFrameSP &frame_sp)
Set accessor to set only the frame shared pointer.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Target * GetTargetPtr() const
Returns a pointer to the target object.
bool HasTargetScope() const
Returns true the ExecutionContext object contains a valid target.
Target & GetTargetRef() const
Returns a reference to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
A class that describes a function.
Definition Function.h:400
ConstString GetName() const
Definition Function.cpp:709
CallEdge * GetCallEdgeForReturnAddress(lldb::addr_t return_pc, Target &target)
Get the outgoing call edge from this function which has the given return address return_pc,...
Definition Function.cpp:368
llvm::ArrayRef< std::unique_ptr< CallEdge > > GetTailCallingEdges()
Get the outgoing tail-calling edges from this function.
Definition Function.cpp:361
bool GetVerbose() const
Definition Log.cpp:326
A collection class for Module objects.
Definition ModuleList.h:101
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
virtual lldb::ByteOrder GetByteOrder() const =0
Gets whether endian swapping should occur when extracting data from this object file.
A plug-in interface definition class for debugging a process.
Definition Process.h:354
virtual size_t ReadMemory(lldb::addr_t vm_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition Process.cpp:1931
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3717
virtual uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num)
Convert from a given register numbering scheme to the lldb register numbering scheme.
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
bool GetScalarValue(Scalar &scalar) const
size_t GetByteSize() const
Definition Scalar.cpp:162
bool IsZero() const
Definition Scalar.cpp:174
void TruncOrExtendTo(uint16_t bits, bool sign)
Convert to an integer with bits and the given signedness.
Definition Scalar.cpp:204
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:365
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
Definition Scalar.cpp:813
This base class provides an interface to stack frames.
Definition StackFrame.h:44
virtual DWARFExpressionList * GetFrameBaseExpression(Status *error_ptr)
Get the DWARFExpressionList corresponding to the Canonical Frame Address.
virtual llvm::Error GetFrameBaseValue(Scalar &value)
Return the Canonical Frame Address (DWARF term) for this frame.
virtual lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
virtual StackID & GetStackID()
virtual const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
virtual uint32_t GetFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList.
An error handling class.
Definition Status.h:118
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:406
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
Function * function
The Function for a given query.
virtual size_t ReadMemory(const Address &addr, void *dst, size_t dst_len, Status &error, bool force_live_memory=false, lldb::addr_t *load_addr_ptr=nullptr, bool *did_read_live_memory=nullptr)
Definition Target.cpp:2003
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1111
const ArchSpec & GetArchitecture() const
Definition Target.h:1153
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:113
ValueType
Type that describes Value::m_value.
Definition Value.h:41
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
Definition Value.h:52
@ FileAddress
A file address value.
Definition Value.h:47
@ LoadAddress
A load address value.
Definition Value.h:49
@ Scalar
A raw scalar value.
Definition Value.h:45
void ClearContext()
Definition Value.h:91
size_t AppendDataToHostBuffer(const Value &rhs)
Definition Value.cpp:154
ValueType GetValueType() const
Definition Value.cpp:111
void SetContext(ContextType context_type, void *p)
Definition Value.h:96
Scalar & ResolveValue(ExecutionContext *exe_ctx, Module *module=nullptr)
Definition Value.cpp:589
DataBufferHeap & GetBuffer()
Definition Value.h:122
void SetValueType(ValueType value_type)
Definition Value.h:89
@ RegisterInfo
RegisterInfo * (can be a scalar or a vector register).
Definition Value.h:61
void ConvertToLoadAddress(Module *module, Target *target)
Convert this value's file address to a load address, if possible.
Definition Value.cpp:675
size_t ResizeData(size_t len)
Definition Value.cpp:192
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define LLDB_INVALID_ADDRESS
#define LLDB_INVALID_OFFSET
#define UINT32_MAX
#define LLDB_INVALID_REGNUM
lldb::ByteOrder InlHostByteOrder()
Definition Endian.h:25
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
uint64_t offset_t
Definition lldb-types.h:85
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
std::shared_ptr< lldb_private::Module > ModuleSP
RegisterKind
Register numbering types.
Represent the locations of a parameter at a call site, both in the caller and in the callee.
Definition Function.h:256
DWARFExpressionList LocationInCaller
Definition Function.h:258
Every register is described in detail including its name, alternate name (optional),...
const char * name
Name of this register, can't be NULL.