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
865 ExecutionContext *exe_ctx,
866 lldb::ModuleSP module_sp,
867 Process *process) {
868 if (stack.empty())
869 return llvm::createStringError("expression stack empty for DW_OP_deref");
870
871 const Value::ValueType value_type = stack.back().GetValueType();
872 switch (value_type) {
874 void *src = (void *)stack.back().GetScalar().ULongLong();
875 intptr_t ptr;
876 ::memcpy(&ptr, src, sizeof(void *));
877 stack.back().GetScalar() = ptr;
878 stack.back().ClearContext();
879 } break;
881 auto file_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
882 Address so_addr;
883 auto maybe_load_addr = ResolveLoadAddress(exe_ctx, module_sp, "DW_OP_deref",
884 file_addr, so_addr);
885 if (!maybe_load_addr)
886 return maybe_load_addr.takeError();
887 stack.back().GetScalar() = *maybe_load_addr;
888 // Fall through to load address promotion code below.
889 }
890 [[fallthrough]];
892 // Promote Scalar to LoadAddress and fall through.
893 stack.back().SetValueType(Value::ValueType::LoadAddress);
894 [[fallthrough]];
896 if (!exe_ctx)
897 return llvm::createStringError("NULL execution context for DW_OP_deref");
898 if (!process)
899 return llvm::createStringError("NULL process for DW_OP_deref");
900 lldb::addr_t pointer_addr =
901 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
903 lldb::addr_t pointer_value =
904 process->ReadPointerFromMemory(pointer_addr, error);
905 if (pointer_value == LLDB_INVALID_ADDRESS)
906 return llvm::joinErrors(
907 llvm::createStringError(
908 "Failed to dereference pointer from 0x%" PRIx64
909 " for DW_OP_deref",
910 pointer_addr),
911 error.takeError());
912 stack.back().GetScalar() = pointer_value;
913 stack.back().ClearContext();
914 } break;
916 return llvm::createStringError("invalid value type for DW_OP_deref");
917 }
918
919 return llvm::Error::success();
920}
921
922/// Helper function to move common code used to load sized data from a uint8_t
923/// buffer.
924///
925/// \param addr_bytes uint8_t buffer containg raw data
926/// \param size_addr_bytes how large is the underlying raw data
927/// \param byte_order what is the byter order of the underlyig data
928/// \param size How much of the underlying data we want to use
929/// \return The underlying data converted into a Scalar
930static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
931 size_t size_addr_bytes,
932 ByteOrder byte_order, size_t size) {
933 DataExtractor addr_data(addr_bytes, size_addr_bytes, byte_order, size);
934
935 lldb::offset_t addr_data_offset = 0;
936 if (size <= 8)
937 return addr_data.GetMaxU64(&addr_data_offset, size);
938 else
939 return addr_data.GetAddress(&addr_data_offset);
940}
941
942llvm::Expected<Value> DWARFExpression::Evaluate(
943 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
944 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
945 const DWARFExpression::Delegate *dwarf_cu,
946 const lldb::RegisterKind reg_kind, const Value *initial_value_ptr,
947 const Value *object_address_ptr) {
948
949 if (opcodes.GetByteSize() == 0)
950 return llvm::createStringError(
951 "no location, value may have been optimized out");
952
953 Stack stack;
954
955 Process *process = nullptr;
956 StackFrame *frame = nullptr;
957 Target *target = nullptr;
958
959 if (exe_ctx) {
960 process = exe_ctx->GetProcessPtr();
961 frame = exe_ctx->GetFramePtr();
962 target = exe_ctx->GetTargetPtr();
963 }
964 if (reg_ctx == nullptr && frame)
965 reg_ctx = frame->GetRegisterContext().get();
966
967 if (initial_value_ptr)
968 stack.push_back(*initial_value_ptr);
969
970 lldb::offset_t offset = 0;
971 Value tmp;
972 uint32_t reg_num;
973
974 /// Insertion point for evaluating multi-piece expression.
975 uint64_t op_piece_offset = 0;
976 Value pieces; // Used for DW_OP_piece
977
979 // A generic type is "an integral type that has the size of an address and an
980 // unspecified signedness". For now, just use the signedness of the operand.
981 // TODO: Implement a real typed stack, and store the genericness of the value
982 // there.
983 auto to_generic = [&](auto v) {
984 // TODO: Avoid implicit trunc?
985 // See https://github.com/llvm/llvm-project/issues/112510.
986 bool is_signed = std::is_signed<decltype(v)>::value;
987 return Scalar(llvm::APSInt(llvm::APInt(8 * opcodes.GetAddressByteSize(), v,
988 is_signed, /*implicitTrunc=*/true),
989 !is_signed));
990 };
991
992 // The default kind is a memory location. This is updated by any
993 // operation that changes this, such as DW_OP_stack_value, and reset
994 // by composition operations like DW_OP_piece.
995 LocationDescriptionKind dwarf4_location_description_kind = Memory;
996
997 while (opcodes.ValidOffset(offset)) {
998 const lldb::offset_t op_offset = offset;
999 const uint8_t op = opcodes.GetU8(&offset);
1000
1001 if (log && log->GetVerbose()) {
1002 size_t count = stack.size();
1003 LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
1004 (uint64_t)count);
1005 for (size_t i = 0; i < count; ++i) {
1006 StreamString new_value;
1007 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
1008 stack[i].Dump(&new_value);
1009 LLDB_LOGF(log, " %s", new_value.GetData());
1010 }
1011 LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
1013 }
1014
1015 if (std::optional<unsigned> arity =
1016 llvm::dwarf::OperationArity(static_cast<LocationAtom>(op))) {
1017 if (stack.size() < *arity)
1018 return llvm::createStringError(
1019 "%s needs at least %d stack entries (stack has %d entries)",
1020 DW_OP_value_to_name(op), *arity, stack.size());
1021 }
1022
1023 switch (op) {
1024 // The DW_OP_addr operation has a single operand that encodes a machine
1025 // address and whose size is the size of an address on the target machine.
1026 case DW_OP_addr:
1027 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
1028 if (target &&
1030 // wasm file sections aren't mapped into memory, therefore addresses can
1031 // never point into a file section and are always LoadAddresses.
1032 stack.back().SetValueType(Value::ValueType::LoadAddress);
1033 } else {
1034 stack.back().SetValueType(Value::ValueType::FileAddress);
1035 }
1036 break;
1037
1038 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1039 // shared libraries that have a location like:
1040 // DW_OP_addr(0x1000)
1041 // If this address resides in a shared library, then this virtual address
1042 // won't make sense when it is evaluated in the context of a running
1043 // process where shared libraries have been slid. To account for this, this
1044 // new address type where we can store the section pointer and a 4 byte
1045 // offset.
1046 // case DW_OP_addr_sect_offset4:
1047 // {
1048 // result_type = eResultTypeFileAddress;
1049 // lldb::Section *sect = (lldb::Section
1050 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
1051 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1052 //
1053 // Address so_addr (sect, sect_offset);
1054 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
1055 // if (load_addr != LLDB_INVALID_ADDRESS)
1056 // {
1057 // // We successfully resolve a file address to a load
1058 // // address.
1059 // stack.push_back(load_addr);
1060 // break;
1061 // }
1062 // else
1063 // {
1064 // // We were able
1065 // if (error_ptr)
1066 // error_ptr->SetErrorStringWithFormat ("Section %s in
1067 // %s is not currently loaded.\n",
1068 // sect->GetName().AsCString(),
1069 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1070 // return false;
1071 // }
1072 // }
1073 // break;
1074
1075 // OPCODE: DW_OP_deref
1076 // OPERANDS: none
1077 // DESCRIPTION: Pops the top stack entry and treats it as an address.
1078 // The value retrieved from that address is pushed. The size of the data
1079 // retrieved from the dereferenced address is the size of an address on the
1080 // target machine.
1081 case DW_OP_deref: {
1082 if (llvm::Error err =
1083 Evaluate_DW_OP_deref(stack, exe_ctx, module_sp, process))
1084 return err;
1085 } break;
1086
1087 // OPCODE: DW_OP_deref_size
1088 // OPERANDS: 1
1089 // 1 - uint8_t that specifies the size of the data to dereference.
1090 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1091 // stack entry and treats it as an address. The value retrieved from that
1092 // address is pushed. In the DW_OP_deref_size operation, however, the size
1093 // in bytes of the data retrieved from the dereferenced address is
1094 // specified by the single operand. This operand is a 1-byte unsigned
1095 // integral constant whose value may not be larger than the size of an
1096 // address on the target machine. The data retrieved is zero extended to
1097 // the size of an address on the target machine before being pushed on the
1098 // expression stack.
1099 case DW_OP_deref_size: {
1100 if (stack.empty()) {
1101 return llvm::createStringError(
1102 "expression stack empty for DW_OP_deref_size");
1103 }
1104 uint8_t size = opcodes.GetU8(&offset);
1105 if (size > 8) {
1106 return llvm::createStringError(
1107 "Invalid address size for DW_OP_deref_size: %d\n", size);
1108 }
1109 Value::ValueType value_type = stack.back().GetValueType();
1110 switch (value_type) {
1112 void *src = (void *)stack.back().GetScalar().ULongLong();
1113 intptr_t ptr;
1114 ::memcpy(&ptr, src, sizeof(void *));
1115 // I can't decide whether the size operand should apply to the bytes in
1116 // their
1117 // lldb-host endianness or the target endianness.. I doubt this'll ever
1118 // come up but I'll opt for assuming big endian regardless.
1119 switch (size) {
1120 case 1:
1121 ptr = ptr & 0xff;
1122 break;
1123 case 2:
1124 ptr = ptr & 0xffff;
1125 break;
1126 case 3:
1127 ptr = ptr & 0xffffff;
1128 break;
1129 case 4:
1130 ptr = ptr & 0xffffffff;
1131 break;
1132 // the casts are added to work around the case where intptr_t is a 32
1133 // bit quantity;
1134 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1135 // program.
1136 case 5:
1137 ptr = (intptr_t)ptr & 0xffffffffffULL;
1138 break;
1139 case 6:
1140 ptr = (intptr_t)ptr & 0xffffffffffffULL;
1141 break;
1142 case 7:
1143 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1144 break;
1145 default:
1146 break;
1147 }
1148 stack.back().GetScalar() = ptr;
1149 stack.back().ClearContext();
1150 } break;
1152 auto file_addr =
1153 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1154 Address so_addr;
1155 auto maybe_load_addr = ResolveLoadAddress(
1156 exe_ctx, module_sp, "DW_OP_deref_size", file_addr, so_addr,
1157 /*check_sectionoffset=*/true);
1158
1159 if (!maybe_load_addr)
1160 return maybe_load_addr.takeError();
1161
1162 addr_t load_addr = *maybe_load_addr;
1163
1164 if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) {
1165 uint8_t addr_bytes[8];
1166 Status error;
1167
1168 if (target &&
1169 target->ReadMemory(so_addr, &addr_bytes, size, error,
1170 /*force_live_memory=*/false) == size) {
1171 ObjectFile *objfile = module_sp->GetObjectFile();
1172
1173 stack.back().GetScalar() = DerefSizeExtractDataHelper(
1174 addr_bytes, size, objfile->GetByteOrder(), size);
1175 stack.back().ClearContext();
1176 break;
1177 } else {
1178 return llvm::createStringError(
1179 "Failed to dereference pointer for DW_OP_deref_size: "
1180 "%s\n",
1181 error.AsCString());
1182 }
1183 }
1184 stack.back().GetScalar() = load_addr;
1185 // Fall through to load address promotion code below.
1186 }
1187
1188 [[fallthrough]];
1191 if (exe_ctx) {
1192 if (process) {
1193 lldb::addr_t pointer_addr =
1194 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1195 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1196 Status error;
1197 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1198 size) {
1199
1200 stack.back().GetScalar() =
1201 DerefSizeExtractDataHelper(addr_bytes, sizeof(addr_bytes),
1202 process->GetByteOrder(), size);
1203 stack.back().ClearContext();
1204 } else {
1205 return llvm::createStringError(
1206 "Failed to dereference pointer from 0x%" PRIx64
1207 " for DW_OP_deref: %s\n",
1208 pointer_addr, error.AsCString());
1209 }
1210 } else {
1211
1212 return llvm::createStringError("NULL process for DW_OP_deref_size");
1213 }
1214 } else {
1215 return llvm::createStringError(
1216 "NULL execution context for DW_OP_deref_size");
1217 }
1218 break;
1219
1221
1222 return llvm::createStringError("invalid value for DW_OP_deref_size");
1223 }
1224
1225 } break;
1226
1227 // OPCODE: DW_OP_xderef_size
1228 // OPERANDS: 1
1229 // 1 - uint8_t that specifies the size of the data to dereference.
1230 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1231 // the top of the stack is treated as an address. The second stack entry is
1232 // treated as an "address space identifier" for those architectures that
1233 // support multiple address spaces. The top two stack elements are popped,
1234 // a data item is retrieved through an implementation-defined address
1235 // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1236 // operation, however, the size in bytes of the data retrieved from the
1237 // dereferenced address is specified by the single operand. This operand is
1238 // a 1-byte unsigned integral constant whose value may not be larger than
1239 // the size of an address on the target machine. The data retrieved is zero
1240 // extended to the size of an address on the target machine before being
1241 // pushed on the expression stack.
1242 case DW_OP_xderef_size:
1243 return llvm::createStringError("unimplemented opcode: DW_OP_xderef_size");
1244 // OPCODE: DW_OP_xderef
1245 // OPERANDS: none
1246 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1247 // the top of the stack is treated as an address. The second stack entry is
1248 // treated as an "address space identifier" for those architectures that
1249 // support multiple address spaces. The top two stack elements are popped,
1250 // a data item is retrieved through an implementation-defined address
1251 // calculation and pushed as the new stack top. The size of the data
1252 // retrieved from the dereferenced address is the size of an address on the
1253 // target machine.
1254 case DW_OP_xderef:
1255 return llvm::createStringError("unimplemented opcode: DW_OP_xderef");
1256
1257 // All DW_OP_constXXX opcodes have a single operand as noted below:
1258 //
1259 // Opcode Operand 1
1260 // DW_OP_const1u 1-byte unsigned integer constant
1261 // DW_OP_const1s 1-byte signed integer constant
1262 // DW_OP_const2u 2-byte unsigned integer constant
1263 // DW_OP_const2s 2-byte signed integer constant
1264 // DW_OP_const4u 4-byte unsigned integer constant
1265 // DW_OP_const4s 4-byte signed integer constant
1266 // DW_OP_const8u 8-byte unsigned integer constant
1267 // DW_OP_const8s 8-byte signed integer constant
1268 // DW_OP_constu unsigned LEB128 integer constant
1269 // DW_OP_consts signed LEB128 integer constant
1270 case DW_OP_const1u:
1271 stack.push_back(to_generic(opcodes.GetU8(&offset)));
1272 break;
1273 case DW_OP_const1s:
1274 stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset)));
1275 break;
1276 case DW_OP_const2u:
1277 stack.push_back(to_generic(opcodes.GetU16(&offset)));
1278 break;
1279 case DW_OP_const2s:
1280 stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset)));
1281 break;
1282 case DW_OP_const4u:
1283 stack.push_back(to_generic(opcodes.GetU32(&offset)));
1284 break;
1285 case DW_OP_const4s:
1286 stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset)));
1287 break;
1288 case DW_OP_const8u:
1289 stack.push_back(to_generic(opcodes.GetU64(&offset)));
1290 break;
1291 case DW_OP_const8s:
1292 stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset)));
1293 break;
1294 // These should also use to_generic, but we can't do that due to a
1295 // producer-side bug in llvm. See llvm.org/pr48087.
1296 case DW_OP_constu:
1297 stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1298 break;
1299 case DW_OP_consts:
1300 stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1301 break;
1302
1303 // OPCODE: DW_OP_dup
1304 // OPERANDS: none
1305 // DESCRIPTION: duplicates the value at the top of the stack
1306 case DW_OP_dup:
1307 if (stack.empty()) {
1308 return llvm::createStringError("expression stack empty for DW_OP_dup");
1309 } else
1310 stack.push_back(stack.back());
1311 break;
1312
1313 // OPCODE: DW_OP_drop
1314 // OPERANDS: none
1315 // DESCRIPTION: pops the value at the top of the stack
1316 case DW_OP_drop:
1317 if (stack.empty()) {
1318 return llvm::createStringError("expression stack empty for DW_OP_drop");
1319 } else
1320 stack.pop_back();
1321 break;
1322
1323 // OPCODE: DW_OP_over
1324 // OPERANDS: none
1325 // DESCRIPTION: Duplicates the entry currently second in the stack at
1326 // the top of the stack.
1327 case DW_OP_over:
1328 stack.push_back(stack[stack.size() - 2]);
1329 break;
1330
1331 // OPCODE: DW_OP_pick
1332 // OPERANDS: uint8_t index into the current stack
1333 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1334 // inclusive) is pushed on the stack
1335 case DW_OP_pick: {
1336 uint8_t pick_idx = opcodes.GetU8(&offset);
1337 if (pick_idx < stack.size())
1338 stack.push_back(stack[stack.size() - 1 - pick_idx]);
1339 else {
1340 return llvm::createStringError(
1341 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1342 }
1343 } break;
1344
1345 // OPCODE: DW_OP_swap
1346 // OPERANDS: none
1347 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1348 // of the stack becomes the second stack entry, and the second entry
1349 // becomes the top of the stack
1350 case DW_OP_swap:
1351 tmp = stack.back();
1352 stack.back() = stack[stack.size() - 2];
1353 stack[stack.size() - 2] = tmp;
1354 break;
1355
1356 // OPCODE: DW_OP_rot
1357 // OPERANDS: none
1358 // DESCRIPTION: Rotates the first three stack entries. The entry at
1359 // the top of the stack becomes the third stack entry, the second entry
1360 // becomes the top of the stack, and the third entry becomes the second
1361 // entry.
1362 case DW_OP_rot: {
1363 size_t last_idx = stack.size() - 1;
1364 Value old_top = stack[last_idx];
1365 stack[last_idx] = stack[last_idx - 1];
1366 stack[last_idx - 1] = stack[last_idx - 2];
1367 stack[last_idx - 2] = old_top;
1368 } break;
1369
1370 // OPCODE: DW_OP_abs
1371 // OPERANDS: none
1372 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1373 // value and pushes its absolute value. If the absolute value can not be
1374 // represented, the result is undefined.
1375 case DW_OP_abs:
1376 if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
1377 return llvm::createStringError(
1378 "failed to take the absolute value of the first stack item");
1379 }
1380 break;
1381
1382 // OPCODE: DW_OP_and
1383 // OPERANDS: none
1384 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1385 // operation on the two, and pushes the result.
1386 case DW_OP_and:
1387 tmp = stack.back();
1388 stack.pop_back();
1389 stack.back().ResolveValue(exe_ctx) =
1390 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1391 break;
1392
1393 // OPCODE: DW_OP_div
1394 // OPERANDS: none
1395 // DESCRIPTION: pops the top two stack values, divides the former second
1396 // entry by the former top of the stack using signed division, and pushes
1397 // the result.
1398 case DW_OP_div: {
1399 tmp = stack.back();
1400 if (tmp.ResolveValue(exe_ctx).IsZero())
1401 return llvm::createStringError("divide by zero");
1402
1403 stack.pop_back();
1404 Scalar divisor, dividend;
1405 divisor = tmp.ResolveValue(exe_ctx);
1406 dividend = stack.back().ResolveValue(exe_ctx);
1407 divisor.MakeSigned();
1408 dividend.MakeSigned();
1409 stack.back() = dividend / divisor;
1410
1411 if (!stack.back().ResolveValue(exe_ctx).IsValid())
1412 return llvm::createStringError("divide failed");
1413 } break;
1414
1415 // OPCODE: DW_OP_minus
1416 // OPERANDS: none
1417 // DESCRIPTION: pops the top two stack values, subtracts the former top
1418 // of the stack from the former second entry, and pushes the result.
1419 case DW_OP_minus:
1420 tmp = stack.back();
1421 stack.pop_back();
1422 stack.back().ResolveValue(exe_ctx) =
1423 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1424 break;
1425
1426 // OPCODE: DW_OP_mod
1427 // OPERANDS: none
1428 // DESCRIPTION: pops the top two stack values and pushes the result of
1429 // the calculation: former second stack entry modulo the former top of the
1430 // stack.
1431 case DW_OP_mod:
1432 tmp = stack.back();
1433 stack.pop_back();
1434 stack.back().ResolveValue(exe_ctx) =
1435 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1436 break;
1437
1438 // OPCODE: DW_OP_mul
1439 // OPERANDS: none
1440 // DESCRIPTION: pops the top two stack entries, multiplies them
1441 // together, and pushes the result.
1442 case DW_OP_mul:
1443 tmp = stack.back();
1444 stack.pop_back();
1445 stack.back().ResolveValue(exe_ctx) =
1446 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1447 break;
1448
1449 // OPCODE: DW_OP_neg
1450 // OPERANDS: none
1451 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1452 case DW_OP_neg:
1453 if (!stack.back().ResolveValue(exe_ctx).UnaryNegate())
1454 return llvm::createStringError("unary negate failed");
1455 break;
1456
1457 // OPCODE: DW_OP_not
1458 // OPERANDS: none
1459 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1460 // complement
1461 case DW_OP_not:
1462 if (!stack.back().ResolveValue(exe_ctx).OnesComplement())
1463 return llvm::createStringError("logical NOT failed");
1464 break;
1465
1466 // OPCODE: DW_OP_or
1467 // OPERANDS: none
1468 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1469 // operation on the two, and pushes the result.
1470 case DW_OP_or:
1471 tmp = stack.back();
1472 stack.pop_back();
1473 stack.back().ResolveValue(exe_ctx) =
1474 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
1475 break;
1476
1477 // OPCODE: DW_OP_plus
1478 // OPERANDS: none
1479 // DESCRIPTION: pops the top two stack entries, adds them together, and
1480 // pushes the result.
1481 case DW_OP_plus:
1482 tmp = stack.back();
1483 stack.pop_back();
1484 stack.back().GetScalar() += tmp.GetScalar();
1485 break;
1486
1487 // OPCODE: DW_OP_plus_uconst
1488 // OPERANDS: none
1489 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1490 // constant operand and pushes the result.
1491 case DW_OP_plus_uconst: {
1492 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
1493 // Implicit conversion from a UINT to a Scalar...
1494 stack.back().GetScalar() += uconst_value;
1495 if (!stack.back().GetScalar().IsValid())
1496 return llvm::createStringError("DW_OP_plus_uconst failed");
1497 } break;
1498
1499 // OPCODE: DW_OP_shl
1500 // OPERANDS: none
1501 // DESCRIPTION: pops the top two stack entries, shifts the former
1502 // second entry left by the number of bits specified by the former top of
1503 // the stack, and pushes the result.
1504 case DW_OP_shl:
1505 tmp = stack.back();
1506 stack.pop_back();
1507 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
1508 break;
1509
1510 // OPCODE: DW_OP_shr
1511 // OPERANDS: none
1512 // DESCRIPTION: pops the top two stack entries, shifts the former second
1513 // entry right logically (filling with zero bits) by the number of bits
1514 // specified by the former top of the stack, and pushes the result.
1515 case DW_OP_shr:
1516 tmp = stack.back();
1517 stack.pop_back();
1518 if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
1519 tmp.ResolveValue(exe_ctx)))
1520 return llvm::createStringError("DW_OP_shr failed");
1521 break;
1522
1523 // OPCODE: DW_OP_shra
1524 // OPERANDS: none
1525 // DESCRIPTION: pops the top two stack entries, shifts the former second
1526 // entry right arithmetically (divide the magnitude by 2, keep the same
1527 // sign for the result) by the number of bits specified by the former top
1528 // of the stack, and pushes the result.
1529 case DW_OP_shra:
1530 tmp = stack.back();
1531 stack.pop_back();
1532 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
1533 break;
1534
1535 // OPCODE: DW_OP_xor
1536 // OPERANDS: none
1537 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1538 // exclusive-or operation on the two, and pushes the result.
1539 case DW_OP_xor:
1540 tmp = stack.back();
1541 stack.pop_back();
1542 stack.back().ResolveValue(exe_ctx) =
1543 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
1544 break;
1545
1546 // OPCODE: DW_OP_skip
1547 // OPERANDS: int16_t
1548 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1549 // signed integer constant. The 2-byte constant is the number of bytes of
1550 // the DWARF expression to skip forward or backward from the current
1551 // operation, beginning after the 2-byte constant.
1552 case DW_OP_skip: {
1553 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1554 lldb::offset_t new_offset = offset + skip_offset;
1555 // New offset can point at the end of the data, in this case we should
1556 // terminate the DWARF expression evaluation (will happen in the loop
1557 // condition).
1558 if (new_offset <= opcodes.GetByteSize())
1559 offset = new_offset;
1560 else {
1561 return llvm::createStringError(llvm::formatv(
1562 "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}", offset,
1563 skip_offset, opcodes.GetByteSize()));
1564 }
1565 } break;
1566
1567 // OPCODE: DW_OP_bra
1568 // OPERANDS: int16_t
1569 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1570 // signed integer constant. This operation pops the top of stack. If the
1571 // value popped is not the constant 0, the 2-byte constant operand is the
1572 // number of bytes of the DWARF expression to skip forward or backward from
1573 // the current operation, beginning after the 2-byte constant.
1574 case DW_OP_bra: {
1575 tmp = stack.back();
1576 stack.pop_back();
1577 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1578 Scalar zero(0);
1579 if (tmp.ResolveValue(exe_ctx) != zero) {
1580 lldb::offset_t new_offset = offset + bra_offset;
1581 // New offset can point at the end of the data, in this case we should
1582 // terminate the DWARF expression evaluation (will happen in the loop
1583 // condition).
1584 if (new_offset <= opcodes.GetByteSize())
1585 offset = new_offset;
1586 else {
1587 return llvm::createStringError(llvm::formatv(
1588 "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}", offset,
1589 bra_offset, opcodes.GetByteSize()));
1590 }
1591 }
1592 } break;
1593
1594 // OPCODE: DW_OP_eq
1595 // OPERANDS: none
1596 // DESCRIPTION: pops the top two stack values, compares using the
1597 // equals (==) operator.
1598 // STACK RESULT: push the constant value 1 onto the stack if the result
1599 // of the operation is true or the constant value 0 if the result of the
1600 // operation is false.
1601 case DW_OP_eq:
1602 tmp = stack.back();
1603 stack.pop_back();
1604 stack.back().ResolveValue(exe_ctx) =
1605 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
1606 break;
1607
1608 // OPCODE: DW_OP_ge
1609 // OPERANDS: none
1610 // DESCRIPTION: pops the top two stack values, compares using the
1611 // greater than or equal to (>=) operator.
1612 // STACK RESULT: push the constant value 1 onto the stack if the result
1613 // of the operation is true or the constant value 0 if the result of the
1614 // operation is false.
1615 case DW_OP_ge:
1616 tmp = stack.back();
1617 stack.pop_back();
1618 stack.back().ResolveValue(exe_ctx) =
1619 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
1620 break;
1621
1622 // OPCODE: DW_OP_gt
1623 // OPERANDS: none
1624 // DESCRIPTION: pops the top two stack values, compares using the
1625 // greater than (>) operator.
1626 // STACK RESULT: push the constant value 1 onto the stack if the result
1627 // of the operation is true or the constant value 0 if the result of the
1628 // operation is false.
1629 case DW_OP_gt:
1630 tmp = stack.back();
1631 stack.pop_back();
1632 stack.back().ResolveValue(exe_ctx) =
1633 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
1634 break;
1635
1636 // OPCODE: DW_OP_le
1637 // OPERANDS: none
1638 // DESCRIPTION: pops the top two stack values, compares using the
1639 // less than or equal to (<=) operator.
1640 // STACK RESULT: push the constant value 1 onto the stack if the result
1641 // of the operation is true or the constant value 0 if the result of the
1642 // operation is false.
1643 case DW_OP_le:
1644 tmp = stack.back();
1645 stack.pop_back();
1646 stack.back().ResolveValue(exe_ctx) =
1647 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
1648 break;
1649
1650 // OPCODE: DW_OP_lt
1651 // OPERANDS: none
1652 // DESCRIPTION: pops the top two stack values, compares using the
1653 // less than (<) operator.
1654 // STACK RESULT: push the constant value 1 onto the stack if the result
1655 // of the operation is true or the constant value 0 if the result of the
1656 // operation is false.
1657 case DW_OP_lt:
1658 tmp = stack.back();
1659 stack.pop_back();
1660 stack.back().ResolveValue(exe_ctx) =
1661 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
1662 break;
1663
1664 // OPCODE: DW_OP_ne
1665 // OPERANDS: none
1666 // DESCRIPTION: pops the top two stack values, compares using the
1667 // not equal (!=) operator.
1668 // STACK RESULT: push the constant value 1 onto the stack if the result
1669 // of the operation is true or the constant value 0 if the result of the
1670 // operation is false.
1671 case DW_OP_ne:
1672 tmp = stack.back();
1673 stack.pop_back();
1674 stack.back().ResolveValue(exe_ctx) =
1675 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
1676 break;
1677
1678 // OPCODE: DW_OP_litn
1679 // OPERANDS: none
1680 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1681 // STACK RESULT: push the unsigned literal constant value onto the top
1682 // of the stack.
1683 case DW_OP_lit0:
1684 case DW_OP_lit1:
1685 case DW_OP_lit2:
1686 case DW_OP_lit3:
1687 case DW_OP_lit4:
1688 case DW_OP_lit5:
1689 case DW_OP_lit6:
1690 case DW_OP_lit7:
1691 case DW_OP_lit8:
1692 case DW_OP_lit9:
1693 case DW_OP_lit10:
1694 case DW_OP_lit11:
1695 case DW_OP_lit12:
1696 case DW_OP_lit13:
1697 case DW_OP_lit14:
1698 case DW_OP_lit15:
1699 case DW_OP_lit16:
1700 case DW_OP_lit17:
1701 case DW_OP_lit18:
1702 case DW_OP_lit19:
1703 case DW_OP_lit20:
1704 case DW_OP_lit21:
1705 case DW_OP_lit22:
1706 case DW_OP_lit23:
1707 case DW_OP_lit24:
1708 case DW_OP_lit25:
1709 case DW_OP_lit26:
1710 case DW_OP_lit27:
1711 case DW_OP_lit28:
1712 case DW_OP_lit29:
1713 case DW_OP_lit30:
1714 case DW_OP_lit31:
1715 stack.push_back(to_generic(op - DW_OP_lit0));
1716 break;
1717
1718 // OPCODE: DW_OP_regN
1719 // OPERANDS: none
1720 // DESCRIPTION: Push the value in register n on the top of the stack.
1721 case DW_OP_reg0:
1722 case DW_OP_reg1:
1723 case DW_OP_reg2:
1724 case DW_OP_reg3:
1725 case DW_OP_reg4:
1726 case DW_OP_reg5:
1727 case DW_OP_reg6:
1728 case DW_OP_reg7:
1729 case DW_OP_reg8:
1730 case DW_OP_reg9:
1731 case DW_OP_reg10:
1732 case DW_OP_reg11:
1733 case DW_OP_reg12:
1734 case DW_OP_reg13:
1735 case DW_OP_reg14:
1736 case DW_OP_reg15:
1737 case DW_OP_reg16:
1738 case DW_OP_reg17:
1739 case DW_OP_reg18:
1740 case DW_OP_reg19:
1741 case DW_OP_reg20:
1742 case DW_OP_reg21:
1743 case DW_OP_reg22:
1744 case DW_OP_reg23:
1745 case DW_OP_reg24:
1746 case DW_OP_reg25:
1747 case DW_OP_reg26:
1748 case DW_OP_reg27:
1749 case DW_OP_reg28:
1750 case DW_OP_reg29:
1751 case DW_OP_reg30:
1752 case DW_OP_reg31: {
1753 dwarf4_location_description_kind = Register;
1754 reg_num = op - DW_OP_reg0;
1755
1756 if (llvm::Error err =
1757 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1758 return err;
1759 stack.push_back(tmp);
1760 } break;
1761 // OPCODE: DW_OP_regx
1762 // OPERANDS:
1763 // ULEB128 literal operand that encodes the register.
1764 // DESCRIPTION: Push the value in register on the top of the stack.
1765 case DW_OP_regx: {
1766 dwarf4_location_description_kind = Register;
1767 reg_num = opcodes.GetULEB128(&offset);
1768 Status read_err;
1769 if (llvm::Error err =
1770 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1771 return err;
1772 stack.push_back(tmp);
1773 } break;
1774
1775 // OPCODE: DW_OP_bregN
1776 // OPERANDS:
1777 // SLEB128 offset from register N
1778 // DESCRIPTION: Value is in memory at the address specified by register
1779 // N plus an offset.
1780 case DW_OP_breg0:
1781 case DW_OP_breg1:
1782 case DW_OP_breg2:
1783 case DW_OP_breg3:
1784 case DW_OP_breg4:
1785 case DW_OP_breg5:
1786 case DW_OP_breg6:
1787 case DW_OP_breg7:
1788 case DW_OP_breg8:
1789 case DW_OP_breg9:
1790 case DW_OP_breg10:
1791 case DW_OP_breg11:
1792 case DW_OP_breg12:
1793 case DW_OP_breg13:
1794 case DW_OP_breg14:
1795 case DW_OP_breg15:
1796 case DW_OP_breg16:
1797 case DW_OP_breg17:
1798 case DW_OP_breg18:
1799 case DW_OP_breg19:
1800 case DW_OP_breg20:
1801 case DW_OP_breg21:
1802 case DW_OP_breg22:
1803 case DW_OP_breg23:
1804 case DW_OP_breg24:
1805 case DW_OP_breg25:
1806 case DW_OP_breg26:
1807 case DW_OP_breg27:
1808 case DW_OP_breg28:
1809 case DW_OP_breg29:
1810 case DW_OP_breg30:
1811 case DW_OP_breg31: {
1812 reg_num = op - DW_OP_breg0;
1813 if (llvm::Error err =
1814 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1815 return err;
1816
1817 int64_t breg_offset = opcodes.GetSLEB128(&offset);
1818 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
1819 tmp.ClearContext();
1820 stack.push_back(tmp);
1821 stack.back().SetValueType(Value::ValueType::LoadAddress);
1822 } break;
1823 // OPCODE: DW_OP_bregx
1824 // OPERANDS: 2
1825 // ULEB128 literal operand that encodes the register.
1826 // SLEB128 offset from register N
1827 // DESCRIPTION: Value is in memory at the address specified by register
1828 // N plus an offset.
1829 case DW_OP_bregx: {
1830 reg_num = opcodes.GetULEB128(&offset);
1831 if (llvm::Error err =
1832 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1833 return err;
1834
1835 int64_t breg_offset = opcodes.GetSLEB128(&offset);
1836 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
1837 tmp.ClearContext();
1838 stack.push_back(tmp);
1839 stack.back().SetValueType(Value::ValueType::LoadAddress);
1840 } break;
1841
1842 case DW_OP_fbreg:
1843 if (exe_ctx) {
1844 if (frame) {
1845 Scalar value;
1846 if (llvm::Error err = frame->GetFrameBaseValue(value))
1847 return err;
1848 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
1849 value += fbreg_offset;
1850 stack.push_back(value);
1851 stack.back().SetValueType(Value::ValueType::LoadAddress);
1852 } else {
1853 return llvm::createStringError(
1854 "invalid stack frame in context for DW_OP_fbreg opcode");
1855 }
1856 } else {
1857 return llvm::createStringError(
1858 "NULL execution context for DW_OP_fbreg");
1859 }
1860
1861 break;
1862
1863 // OPCODE: DW_OP_nop
1864 // OPERANDS: none
1865 // DESCRIPTION: A place holder. It has no effect on the location stack
1866 // or any of its values.
1867 case DW_OP_nop:
1868 break;
1869
1870 // OPCODE: DW_OP_piece
1871 // OPERANDS: 1
1872 // ULEB128: byte size of the piece
1873 // DESCRIPTION: The operand describes the size in bytes of the piece of
1874 // the object referenced by the DWARF expression whose result is at the top
1875 // of the stack. If the piece is located in a register, but does not occupy
1876 // the entire register, the placement of the piece within that register is
1877 // defined by the ABI.
1878 //
1879 // Many compilers store a single variable in sets of registers, or store a
1880 // variable partially in memory and partially in registers. DW_OP_piece
1881 // provides a way of describing how large a part of a variable a particular
1882 // DWARF expression refers to.
1883 case DW_OP_piece: {
1884 LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind;
1885 // Reset for the next piece.
1886 dwarf4_location_description_kind = Memory;
1887
1888 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
1889
1890 if (piece_byte_size > 0) {
1891 Value curr_piece;
1892
1893 if (stack.empty()) {
1894 UpdateValueTypeFromLocationDescription(
1895 log, dwarf_cu, LocationDescriptionKind::Empty);
1896 // In a multi-piece expression, this means that the current piece is
1897 // not available. Fill with zeros for now by resizing the data and
1898 // appending it
1899 curr_piece.ResizeData(piece_byte_size);
1900 // Note that "0" is not a correct value for the unknown bits.
1901 // It would be better to also return a mask of valid bits together
1902 // with the expression result, so the debugger can print missing
1903 // members as "<optimized out>" or something.
1904 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
1905 pieces.AppendDataToHostBuffer(curr_piece);
1906 } else {
1907 Status error;
1908 // Extract the current piece into "curr_piece"
1909 Value curr_piece_source_value(stack.back());
1910 stack.pop_back();
1911 UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc,
1912 &curr_piece_source_value);
1913
1914 const Value::ValueType curr_piece_source_value_type =
1915 curr_piece_source_value.GetValueType();
1916 Scalar &scalar = curr_piece_source_value.GetScalar();
1918 switch (curr_piece_source_value_type) {
1920 return llvm::createStringError("invalid value type");
1922 if (target) {
1923 curr_piece_source_value.ConvertToLoadAddress(module_sp.get(),
1924 target);
1925 addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1926 } else {
1927 return llvm::createStringError(
1928 "unable to convert file address 0x%" PRIx64
1929 " to load address "
1930 "for DW_OP_piece(%" PRIu64 "): "
1931 "no target available",
1932 addr, piece_byte_size);
1933 }
1934 [[fallthrough]];
1936 if (target) {
1937 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
1938 if (target->ReadMemory(addr, curr_piece.GetBuffer().GetBytes(),
1939 piece_byte_size, error,
1940 /*force_live_memory=*/false) !=
1941 piece_byte_size) {
1942 const char *addr_type = (curr_piece_source_value_type ==
1944 ? "load"
1945 : "file";
1946 return llvm::createStringError(
1947 "failed to read memory DW_OP_piece(%" PRIu64
1948 ") from %s address 0x%" PRIx64,
1949 piece_byte_size, addr_type, addr);
1950 }
1951 } else {
1952 return llvm::createStringError(
1953 "failed to resize the piece memory buffer for "
1954 "DW_OP_piece(%" PRIu64 ")",
1955 piece_byte_size);
1956 }
1957 }
1958 } break;
1960 return llvm::createStringError(
1961 "failed to read memory DW_OP_piece(%" PRIu64
1962 ") from host address 0x%" PRIx64,
1963 piece_byte_size, addr);
1964 } break;
1965
1967 uint32_t bit_size = piece_byte_size * 8;
1968 uint32_t bit_offset = 0;
1969 if (!scalar.ExtractBitfield(bit_size, bit_offset)) {
1970 return llvm::createStringError(
1971 "unable to extract %" PRIu64 " bytes from a %" PRIu64
1972 " byte scalar value.",
1973 piece_byte_size,
1974 (uint64_t)curr_piece_source_value.GetScalar().GetByteSize());
1975 }
1976
1977 // We have seen a case where we have expression like:
1978 // DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x28
1979 // here we are assuming the compiler was trying to zero
1980 // extend the value that we should append to the buffer.
1981 scalar.TruncOrExtendTo(bit_size, /*sign=*/false);
1982 curr_piece.GetScalar() = scalar;
1983 } break;
1984 }
1985
1986 // Check if this is the first piece?
1987 if (op_piece_offset == 0) {
1988 // This is the first piece, we should push it back onto the stack
1989 // so subsequent pieces will be able to access this piece and add
1990 // to it.
1991 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
1992 return llvm::createStringError("failed to append piece data");
1993 }
1994 } else {
1995 // If this is the second or later piece there should be a value on
1996 // the stack.
1997 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
1998 return llvm::createStringError(
1999 "DW_OP_piece for offset %" PRIu64
2000 " but top of stack is of size %" PRIu64,
2001 op_piece_offset, pieces.GetBuffer().GetByteSize());
2002 }
2003
2004 if (pieces.AppendDataToHostBuffer(curr_piece) == 0)
2005 return llvm::createStringError("failed to append piece data");
2006 }
2007 }
2008 op_piece_offset += piece_byte_size;
2009 }
2010 } break;
2011
2012 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2013 if (stack.size() < 1) {
2014 UpdateValueTypeFromLocationDescription(log, dwarf_cu,
2015 LocationDescriptionKind::Empty);
2016 // Reset for the next piece.
2017 dwarf4_location_description_kind = Memory;
2018 return llvm::createStringError(
2019 "expression stack needs at least 1 item for DW_OP_bit_piece");
2020 } else {
2021 UpdateValueTypeFromLocationDescription(
2022 log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
2023 // Reset for the next piece.
2024 dwarf4_location_description_kind = Memory;
2025 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2026 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2027 switch (stack.back().GetValueType()) {
2029 return llvm::createStringError(
2030 "unable to extract bit value from invalid value");
2032 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2033 piece_bit_offset)) {
2034 return llvm::createStringError(
2035 "unable to extract %" PRIu64 " bit value with %" PRIu64
2036 " bit offset from a %" PRIu64 " bit scalar value.",
2037 piece_bit_size, piece_bit_offset,
2038 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2039 }
2040 } break;
2041
2045 return llvm::createStringError(
2046 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2047 ", bit_offset = %" PRIu64 ") from an address value.",
2048 piece_bit_size, piece_bit_offset);
2049 }
2050 }
2051 break;
2052
2053 // OPCODE: DW_OP_implicit_value
2054 // OPERANDS: 2
2055 // ULEB128 size of the value block in bytes
2056 // uint8_t* block bytes encoding value in target's memory
2057 // representation
2058 // DESCRIPTION: Value is immediately stored in block in the debug info with
2059 // the memory representation of the target.
2060 case DW_OP_implicit_value: {
2061 dwarf4_location_description_kind = Implicit;
2062
2063 const uint32_t len = opcodes.GetULEB128(&offset);
2064 const void *data = opcodes.GetData(&offset, len);
2065
2066 if (!data) {
2067 LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data");
2068 return llvm::createStringError("could not evaluate %s",
2070 }
2071
2072 Value result(data, len);
2073 stack.push_back(result);
2074 break;
2075 }
2076
2077 case DW_OP_implicit_pointer: {
2078 dwarf4_location_description_kind = Implicit;
2079 return llvm::createStringError("could not evaluate %s",
2081 }
2082
2083 // OPCODE: DW_OP_push_object_address
2084 // OPERANDS: none
2085 // DESCRIPTION: Pushes the address of the object currently being
2086 // evaluated as part of evaluation of a user presented expression. This
2087 // object may correspond to an independent variable described by its own
2088 // DIE or it may be a component of an array, structure, or class whose
2089 // address has been dynamically determined by an earlier step during user
2090 // expression evaluation.
2091 case DW_OP_push_object_address:
2092 if (object_address_ptr)
2093 stack.push_back(*object_address_ptr);
2094 else {
2095 return llvm::createStringError("DW_OP_push_object_address used without "
2096 "specifying an object address");
2097 }
2098 break;
2099
2100 // OPCODE: DW_OP_call2
2101 // OPERANDS:
2102 // uint16_t compile unit relative offset of a DIE
2103 // DESCRIPTION: Performs subroutine calls during evaluation
2104 // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2105 // debugging information entry in the current compilation unit.
2106 //
2107 // Operand interpretation is exactly like that for DW_FORM_ref2.
2108 //
2109 // This operation transfers control of DWARF expression evaluation to the
2110 // DW_AT_location attribute of the referenced DIE. If there is no such
2111 // attribute, then there is no effect. Execution of the DWARF expression of
2112 // a DW_AT_location attribute may add to and/or remove from values on the
2113 // stack. Execution returns to the point following the call when the end of
2114 // the attribute is reached. Values on the stack at the time of the call
2115 // may be used as parameters by the called expression and values left on
2116 // the stack by the called expression may be used as return values by prior
2117 // agreement between the calling and called expressions.
2118 case DW_OP_call2:
2119 return llvm::createStringError("unimplemented opcode DW_OP_call2");
2120 // OPCODE: DW_OP_call4
2121 // OPERANDS: 1
2122 // uint32_t compile unit relative offset of a DIE
2123 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2124 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2125 // a debugging information entry in the current compilation unit.
2126 //
2127 // Operand interpretation DW_OP_call4 is exactly like that for
2128 // DW_FORM_ref4.
2129 //
2130 // This operation transfers control of DWARF expression evaluation to the
2131 // DW_AT_location attribute of the referenced DIE. If there is no such
2132 // attribute, then there is no effect. Execution of the DWARF expression of
2133 // a DW_AT_location attribute may add to and/or remove from values on the
2134 // stack. Execution returns to the point following the call when the end of
2135 // the attribute is reached. Values on the stack at the time of the call
2136 // may be used as parameters by the called expression and values left on
2137 // the stack by the called expression may be used as return values by prior
2138 // agreement between the calling and called expressions.
2139 case DW_OP_call4:
2140 return llvm::createStringError("unimplemented opcode DW_OP_call4");
2141
2142 // OPCODE: DW_OP_stack_value
2143 // OPERANDS: None
2144 // DESCRIPTION: Specifies that the object does not exist in memory but
2145 // rather is a constant value. The value from the top of the stack is the
2146 // value to be used. This is the actual object value and not the location.
2147 case DW_OP_stack_value:
2148 dwarf4_location_description_kind = Implicit;
2149 stack.back().SetValueType(Value::ValueType::Scalar);
2150 break;
2151
2152 // OPCODE: DW_OP_convert
2153 // OPERANDS: 1
2154 // A ULEB128 that is either a DIE offset of a
2155 // DW_TAG_base_type or 0 for the generic (pointer-sized) type.
2156 //
2157 // DESCRIPTION: Pop the top stack element, convert it to a
2158 // different type, and push the result.
2159 case DW_OP_convert: {
2160 const uint64_t relative_die_offset = opcodes.GetULEB128(&offset);
2161 uint64_t bit_size;
2162 bool sign;
2163 if (relative_die_offset == 0) {
2164 // The generic type has the size of an address on the target
2165 // machine and an unspecified signedness. Scalar has no
2166 // "unspecified signedness", so we use unsigned types.
2167 if (!module_sp)
2168 return llvm::createStringError("no module");
2169 sign = false;
2170 bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
2171 if (!bit_size)
2172 return llvm::createStringError("unspecified architecture");
2173 } else {
2174 auto bit_size_sign_or_err =
2175 dwarf_cu->GetDIEBitSizeAndSign(relative_die_offset);
2176 if (!bit_size_sign_or_err)
2177 return bit_size_sign_or_err.takeError();
2178 bit_size = bit_size_sign_or_err->first;
2179 sign = bit_size_sign_or_err->second;
2180 }
2181 Scalar &top = stack.back().ResolveValue(exe_ctx);
2182 top.TruncOrExtendTo(bit_size, sign);
2183 break;
2184 }
2185
2186 // OPCODE: DW_OP_call_frame_cfa
2187 // OPERANDS: None
2188 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2189 // the canonical frame address consistent with the call frame information
2190 // located in .debug_frame (or in the FDEs of the eh_frame section).
2191 case DW_OP_call_frame_cfa:
2192 if (frame) {
2193 // Note that we don't have to parse FDEs because this DWARF expression
2194 // is commonly evaluated with a valid stack frame.
2195 StackID id = frame->GetStackID();
2196 addr_t cfa = id.GetCallFrameAddressWithMetadata();
2197 if (cfa != LLDB_INVALID_ADDRESS) {
2198 stack.push_back(Scalar(cfa));
2199 stack.back().SetValueType(Value::ValueType::LoadAddress);
2200 } else {
2201 return llvm::createStringError(
2202 "stack frame does not include a canonical "
2203 "frame address for DW_OP_call_frame_cfa "
2204 "opcode");
2205 }
2206 } else {
2207 return llvm::createStringError("unvalid stack frame in context for "
2208 "DW_OP_call_frame_cfa opcode");
2209 }
2210 break;
2211
2212 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2213 // opcode, DW_OP_GNU_push_tls_address)
2214 // OPERANDS: none
2215 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2216 // an address in the current thread's thread-local storage block, and
2217 // pushes it on the stack.
2218 case DW_OP_form_tls_address:
2219 case DW_OP_GNU_push_tls_address: {
2220 if (stack.size() < 1) {
2221 if (op == DW_OP_form_tls_address)
2222 return llvm::createStringError(
2223 "DW_OP_form_tls_address needs an argument");
2224 else
2225 return llvm::createStringError(
2226 "DW_OP_GNU_push_tls_address needs an argument");
2227 }
2228
2229 if (!exe_ctx || !module_sp)
2230 return llvm::createStringError("no context to evaluate TLS within");
2231
2232 Thread *thread = exe_ctx->GetThreadPtr();
2233 if (!thread)
2234 return llvm::createStringError("no thread to evaluate TLS within");
2235
2236 // Lookup the TLS block address for this thread and module.
2237 const addr_t tls_file_addr =
2238 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2239 const addr_t tls_load_addr =
2240 thread->GetThreadLocalData(module_sp, tls_file_addr);
2241
2242 if (tls_load_addr == LLDB_INVALID_ADDRESS)
2243 return llvm::createStringError(
2244 "no TLS data currently exists for this thread");
2245
2246 stack.back().GetScalar() = tls_load_addr;
2247 stack.back().SetValueType(Value::ValueType::LoadAddress);
2248 } break;
2249
2250 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
2251 // OPERANDS: 1
2252 // ULEB128: index to the .debug_addr section
2253 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2254 // section with the base address specified by the DW_AT_addr_base attribute
2255 // and the 0 based index is the ULEB128 encoded index.
2256 case DW_OP_addrx:
2257 case DW_OP_GNU_addr_index: {
2258 if (!dwarf_cu)
2259 return llvm::createStringError("DW_OP_GNU_addr_index found without a "
2260 "compile unit being specified");
2261 uint64_t index = opcodes.GetULEB128(&offset);
2262 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2263 stack.push_back(Scalar(value));
2264 if (target &&
2266 // wasm file sections aren't mapped into memory, therefore addresses can
2267 // never point into a file section and are always LoadAddresses.
2268 stack.back().SetValueType(Value::ValueType::LoadAddress);
2269 } else {
2270 stack.back().SetValueType(Value::ValueType::FileAddress);
2271 }
2272 } break;
2273
2274 // OPCODE: DW_OP_GNU_const_index
2275 // OPERANDS: 1
2276 // ULEB128: index to the .debug_addr section
2277 // DESCRIPTION: Pushes an constant with the size of a machine address to
2278 // the stack from the .debug_addr section with the base address specified
2279 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2280 // encoded index.
2281 case DW_OP_GNU_const_index: {
2282 if (!dwarf_cu) {
2283 return llvm::createStringError("DW_OP_GNU_const_index found without a "
2284 "compile unit being specified");
2285 }
2286 uint64_t index = opcodes.GetULEB128(&offset);
2287 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2288 stack.push_back(Scalar(value));
2289 } break;
2290
2291 case DW_OP_GNU_entry_value:
2292 case DW_OP_entry_value: {
2293 if (llvm::Error err = Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx,
2294 opcodes, offset, log))
2295 return llvm::createStringError(
2296 "could not evaluate DW_OP_entry_value: %s",
2297 llvm::toString(std::move(err)).c_str());
2298 break;
2299 }
2300
2301 default:
2302 if (dwarf_cu) {
2303 if (dwarf_cu->ParseVendorDWARFOpcode(op, opcodes, offset, reg_ctx,
2304 reg_kind, stack)) {
2305 break;
2306 }
2307 }
2308 return llvm::createStringError(llvm::formatv(
2309 "Unhandled opcode {0} in DWARFExpression", LocationAtom(op)));
2310 }
2311 }
2312
2313 if (stack.empty()) {
2314 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2315 // or DW_OP_bit_piece opcodes
2316 if (pieces.GetBuffer().GetByteSize())
2317 return pieces;
2318
2319 return llvm::createStringError("stack empty after evaluation");
2320 }
2321
2322 UpdateValueTypeFromLocationDescription(
2323 log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
2324
2325 if (log && log->GetVerbose()) {
2326 size_t count = stack.size();
2327 LLDB_LOGF(log,
2328 "Stack after operation has %" PRIu64 " values:", (uint64_t)count);
2329 for (size_t i = 0; i < count; ++i) {
2330 StreamString new_value;
2331 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2332 stack[i].Dump(&new_value);
2333 LLDB_LOGF(log, " %s", new_value.GetData());
2334 }
2335 }
2336 return stack.back();
2337}
2338
2340 StackFrame &frame, const Instruction::Operand &operand) const {
2341 using namespace OperandMatchers;
2342
2343 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
2344 if (!reg_ctx_sp) {
2345 return false;
2346 }
2347
2348 DataExtractor opcodes(m_data);
2349
2350 lldb::offset_t op_offset = 0;
2351 uint8_t opcode = opcodes.GetU8(&op_offset);
2352
2353 if (opcode == DW_OP_fbreg) {
2354 int64_t offset = opcodes.GetSLEB128(&op_offset);
2355
2356 DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr);
2357 if (!fb_expr) {
2358 return false;
2359 }
2360
2361 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
2362 return fb_expr->MatchesOperand(frame, child);
2363 };
2364
2365 if (!offset &&
2366 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2367 recurse)(operand)) {
2368 return true;
2369 }
2370
2371 return MatchUnaryOp(
2373 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2374 MatchImmOp(offset), recurse))(operand);
2375 }
2376
2377 bool dereference = false;
2378 const RegisterInfo *reg = nullptr;
2379 int64_t offset = 0;
2380
2381 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
2382 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
2383 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
2384 offset = opcodes.GetSLEB128(&op_offset);
2385 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
2386 } else if (opcode == DW_OP_regx) {
2387 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2388 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2389 } else if (opcode == DW_OP_bregx) {
2390 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2391 offset = opcodes.GetSLEB128(&op_offset);
2392 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2393 } else {
2394 return false;
2395 }
2396
2397 if (!reg) {
2398 return false;
2399 }
2400
2401 if (dereference) {
2402 if (!offset &&
2403 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2404 MatchRegOp(*reg))(operand)) {
2405 return true;
2406 }
2407
2408 return MatchUnaryOp(
2410 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2411 MatchRegOp(*reg), MatchImmOp(offset)))(operand);
2412 } else {
2413 return MatchRegOp(*reg)(operand);
2414 }
2415}
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 llvm::Error Evaluate_DW_OP_deref(DWARFExpression::Stack &stack, ExecutionContext *exe_ctx, lldb::ModuleSP module_sp, Process *process)
static const char * DW_OP_value_to_name(uint32_t val)
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 move common code used 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.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes 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.
RegisterContext * GetRegisterContext() const
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:45
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:357
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:1930
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3616
lldb::addr_t ReadPointerFromMemory(lldb::addr_t vm_addr, Status &error)
Definition Process.cpp:2260
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
DWARFExpressionList * GetFrameBaseExpression(Status *error_ptr)
Get the DWARFExpressionList corresponding to the Canonical Frame Address.
llvm::Error GetFrameBaseValue(Scalar &value)
Return the Canonical Frame Address (DWARF term) for this frame.
lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
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:392
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:1996
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1025
const ArchSpec & GetArchitecture() const
Definition Target.h:1067
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.