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