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"
24
25#include "lldb/Host/Host.h"
26#include "lldb/Utility/Endian.h"
27
29
30#include "lldb/Target/ABI.h"
32#include "lldb/Target/Process.h"
35#include "lldb/Target/StackID.h"
36#include "lldb/Target/Target.h"
37#include "lldb/Target/Thread.h"
38#include "llvm/DebugInfo/DWARF/DWARFExpressionPrinter.h"
39#include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
40#include "llvm/Support/ErrorExtras.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 llvm::ArrayRef<uint8_t> subexpr,
597 Log *log) {
598 // DW_OP_entry_value(sub-expr) describes the location a variable had upon
599 // function entry: this variable location is presumed to be optimized out at
600 // the current PC value. The caller of the function may have call site
601 // information that describes an alternate location for the variable (e.g. a
602 // constant literal, or a spilled stack value) in the parent frame.
603 //
604 // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
605 //
606 // void child(int &sink, int x) {
607 // ...
608 // /* "x" gets optimized out. */
609 //
610 // /* The location of "x" here is: DW_OP_entry_value($reg2). */
611 // ++sink;
612 // }
613 //
614 // void parent() {
615 // int sink;
616 //
617 // /*
618 // * The callsite information emitted here is:
619 // *
620 // * DW_TAG_call_site
621 // * DW_AT_return_pc ... (for "child(sink, 123);")
622 // * DW_TAG_call_site_parameter (for "sink")
623 // * DW_AT_location ($reg1)
624 // * DW_AT_call_value ($SP - 8)
625 // * DW_TAG_call_site_parameter (for "x")
626 // * DW_AT_location ($reg2)
627 // * DW_AT_call_value ($literal 123)
628 // *
629 // * DW_TAG_call_site
630 // * DW_AT_return_pc ... (for "child(sink, 456);")
631 // * ...
632 // */
633 // child(sink, 123);
634 // child(sink, 456);
635 // }
636 //
637 // When the program stops at "++sink" within `child`, the debugger determines
638 // the call site by analyzing the return address. Once the call site is found,
639 // the debugger determines which parameter is referenced by DW_OP_entry_value
640 // and evaluates the corresponding location for that parameter in `parent`.
641
642 // 1. Find the function which pushed the current frame onto the stack.
643 if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) {
644 return llvm::createStringError("no exe/reg context");
645 }
646
647 StackFrame *current_frame = exe_ctx->GetFramePtr();
648 Thread *thread = exe_ctx->GetThreadPtr();
649 if (!current_frame || !thread)
650 return llvm::createStringError("no current frame/thread");
651
652 Target &target = exe_ctx->GetTargetRef();
653 StackFrameSP parent_frame = nullptr;
654 addr_t return_pc = LLDB_INVALID_ADDRESS;
655 uint32_t current_frame_idx = current_frame->GetFrameIndex();
656
657 for (uint32_t parent_frame_idx = current_frame_idx + 1;; parent_frame_idx++) {
658 parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx);
659 // If this is null, we're at the end of the stack.
660 if (!parent_frame)
661 break;
662
663 // Record the first valid return address, even if this is an inlined frame,
664 // in order to look up the associated call edge in the first non-inlined
665 // parent frame.
666 if (return_pc == LLDB_INVALID_ADDRESS) {
667 return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
668 LLDB_LOG(log, "immediate ancestor with pc = {0:x}", return_pc);
669 }
670
671 // If we've found an inlined frame, skip it (these have no call site
672 // parameters).
673 if (parent_frame->IsInlined())
674 continue;
675
676 // We've found the first non-inlined parent frame.
677 break;
678 }
679 if (!parent_frame || !parent_frame->GetRegisterContext()) {
680 return llvm::createStringError("no parent frame with reg ctx");
681 }
682
683 Function *parent_func =
684 parent_frame->GetSymbolContext(eSymbolContextFunction).function;
685 if (!parent_func)
686 return llvm::createStringError("no parent function");
687
688 // 2. Find the call edge in the parent function responsible for creating the
689 // current activation.
690 Function *current_func =
691 current_frame->GetSymbolContext(eSymbolContextFunction).function;
692 if (!current_func)
693 return llvm::createStringError("no current function");
694
695 CallEdge *call_edge = nullptr;
696 ModuleList &modlist = target.GetImages();
697 ExecutionContext parent_exe_ctx = *exe_ctx;
698 parent_exe_ctx.SetFrameSP(parent_frame);
699 if (!parent_frame->IsArtificial()) {
700 // If the parent frame is not artificial, the current activation may be
701 // produced by an ambiguous tail call. In this case, refuse to proceed.
702 call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target);
703 if (!call_edge) {
704 return llvm::createStringErrorV(
705 "no call edge for retn-pc = {0:x} in parent frame {1}", return_pc,
706 parent_func->GetName());
707 }
708 Function *callee_func = call_edge->GetCallee(modlist, parent_exe_ctx);
709 if (callee_func != current_func) {
710 return llvm::createStringError(
711 "ambiguous call sequence, can't find real parent frame");
712 }
713 } else {
714 // The StackFrameList solver machinery has deduced that an unambiguous tail
715 // call sequence that produced the current activation. The first edge in
716 // the parent that points to the current function must be valid.
717 for (auto &edge : parent_func->GetTailCallingEdges()) {
718 if (edge->GetCallee(modlist, parent_exe_ctx) == current_func) {
719 call_edge = edge.get();
720 break;
721 }
722 }
723 }
724 if (!call_edge)
725 return llvm::createStringError("no unambiguous edge from parent "
726 "to current function");
727
728 // 3. Attempt to locate the DW_OP_entry_value expression in the set of
729 // available call site parameters. If found, evaluate the corresponding
730 // parameter in the context of the parent frame.
731 const CallSiteParameter *matched_param = nullptr;
732 for (const CallSiteParameter &param : call_edge->GetCallSiteParameters()) {
733 DataExtractor param_subexpr_extractor;
734 if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor))
735 continue;
736 lldb::offset_t param_subexpr_offset = 0;
737 const void *param_subexpr_data =
738 param_subexpr_extractor.GetData(&param_subexpr_offset, subexpr.size());
739 if (!param_subexpr_data ||
740 param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0)
741 continue;
742
743 // At this point, the DW_OP_entry_value sub-expression and the callee-side
744 // expression in the call site parameter are known to have the same length.
745 // Check whether they are equal.
746 //
747 // Note that an equality check is sufficient: the contents of the
748 // DW_OP_entry_value subexpression are only used to identify the right call
749 // site parameter in the parent, and do not require any special handling.
750 if (memcmp(subexpr.data(), param_subexpr_data, subexpr.size()) == 0) {
751 matched_param = &param;
752 break;
753 }
754 }
755 if (!matched_param)
756 return llvm::createStringError("no matching call site param found");
757
758 // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
759 // subexpresion whenever llvm does.
760 const DWARFExpressionList &param_expr = matched_param->LocationInCaller;
761
762 llvm::Expected<Value> maybe_result = param_expr.Evaluate(
763 &parent_exe_ctx, parent_frame->GetRegisterContext().get(),
765 /*initial_value_ptr=*/nullptr,
766 /*object_address_ptr=*/nullptr);
767 if (!maybe_result) {
768 LLDB_LOG(log,
769 "Evaluate_DW_OP_entry_value: call site param evaluation failed");
770 return maybe_result.takeError();
771 }
772
773 stack.push_back(*maybe_result);
774 return llvm::Error::success();
775}
776
777namespace {
778/// The location description kinds described by the DWARF v5
779/// specification. Composite locations are handled out-of-band and
780/// thus aren't part of the enum.
781enum LocationDescriptionKind {
782 Empty,
783 Memory,
784 Register,
785 Implicit
786 /* Composite*/
787};
788/// Adjust value's ValueType according to the kind of location description.
789void UpdateValueTypeFromLocationDescription(
790 Log *log, const DWARFExpression::Delegate *dwarf_cu,
791 LocationDescriptionKind kind, Value *value = nullptr) {
792 // Note that this function is conflating DWARF expressions with
793 // DWARF location descriptions. Perhaps it would be better to define
794 // a wrapper for DWARFExpression::Eval() that deals with DWARF
795 // location descriptions (which consist of one or more DWARF
796 // expressions). But doing this would mean we'd also need factor the
797 // handling of DW_OP_(bit_)piece out of this function.
798 if (dwarf_cu && dwarf_cu->GetVersion() >= 4) {
799 const char *log_msg = "DWARF location description kind: %s";
800 switch (kind) {
801 case Empty:
802 LLDB_LOGF(log, log_msg, "Empty");
803 break;
804 case Memory:
805 LLDB_LOGF(log, log_msg, "Memory");
806 if (value->GetValueType() == Value::ValueType::Scalar)
807 value->SetValueType(Value::ValueType::LoadAddress);
808 break;
809 case Register:
810 LLDB_LOGF(log, log_msg, "Register");
811 value->SetValueType(Value::ValueType::Scalar);
812 break;
813 case Implicit:
814 LLDB_LOGF(log, log_msg, "Implicit");
815 if (value->GetValueType() == Value::ValueType::LoadAddress)
816 value->SetValueType(Value::ValueType::Scalar);
817 break;
818 }
819 }
820}
821} // namespace
822
823/// Helper function to move common code used to resolve a file address and turn
824/// into a load address.
825///
826/// \param exe_ctx Pointer to the execution context
827/// \param module_sp shared_ptr contains the module if we have one
828/// \param dw_op_type C-style string used to vary the error output
829/// \param file_addr the file address we are trying to resolve and turn into a
830/// load address
831/// \param so_addr out parameter, will be set to load address or section offset
832/// \param check_sectionoffset bool which determines if having a section offset
833/// but not a load address is considerd a success
834/// \returns std::optional containing the load address if resolving and getting
835/// the load address succeed or an empty Optinal otherwise. If
836/// check_sectionoffset is true we consider LLDB_INVALID_ADDRESS a
837/// success if so_addr.IsSectionOffset() is true.
838static llvm::Expected<lldb::addr_t>
840 const char *dw_op_type, lldb::addr_t file_addr,
841 Address &so_addr, bool check_sectionoffset = false) {
842 if (!module_sp)
843 return llvm::createStringError("need module to resolve file address for %s",
844 dw_op_type);
845
846 if (!module_sp->ResolveFileAddress(file_addr, so_addr))
847 return llvm::createStringError("failed to resolve file address in module");
848
849 const addr_t load_addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
850
851 if (load_addr == LLDB_INVALID_ADDRESS &&
852 (check_sectionoffset && !so_addr.IsSectionOffset()))
853 return llvm::createStringError("failed to resolve load address");
854
855 return load_addr;
856}
857
858/// @brief Helper function to load sized data from a uint8_t buffer.
859///
860/// @param addr_bytes The buffer containing raw data.
861/// @param size_addr_bytes How large is the underlying raw data.
862/// @param byte_order What is the byte order of the underlying data.
863/// @param size How much of the underlying data we want to use.
864/// @return The underlying data converted into a Scalar.
865static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
866 size_t size_addr_bytes,
867 ByteOrder byte_order, size_t size) {
868 DataExtractor addr_data(addr_bytes, size_addr_bytes, byte_order, size);
869
870 lldb::offset_t addr_data_offset = 0;
871 if (size <= 8)
872 return addr_data.GetMaxU64(&addr_data_offset, size);
873 return addr_data.GetAddress(&addr_data_offset);
874}
875
876static llvm::Error Evaluate_DW_OP_deref_size(
878 lldb::ModuleSP module_sp, Process *process, Target *target, uint8_t size,
879 size_t size_addr_bytes,
880 LocationDescriptionKind &dwarf4_location_description_kind) {
881 if (stack.empty())
882 return llvm::createStringError(
883 "expression stack empty for DW_OP_deref_size");
884
885 if (size > 8)
886 return llvm::createStringError(
887 "Invalid address size for DW_OP_deref_size: %d\n", size);
888
889 // Deref a register or implicit location and truncate the value to `size`
890 // bytes. See the corresponding comment in DW_OP_deref for more details on
891 // why we deref these locations this way.
892 if (dwarf4_location_description_kind == Register ||
893 dwarf4_location_description_kind == Implicit) {
894 // Reset context to default values.
895 dwarf4_location_description_kind = Memory;
896 stack.back().ClearContext();
897
898 // Truncate the value on top of the stack to *size* bytes then
899 // extend to the size of an address (e.g. generic type).
900 Scalar scalar = stack.back().GetScalar();
901 scalar.TruncOrExtendTo(size * 8, /*sign=*/false);
902 scalar.TruncOrExtendTo(size_addr_bytes * 8,
903 /*sign=*/false);
904 stack.back().GetScalar() = scalar;
905 return llvm::Error::success();
906 }
907
908 Value::ValueType value_type = stack.back().GetValueType();
909 switch (value_type) {
911 void *src = (void *)stack.back().GetScalar().ULongLong();
912 intptr_t ptr;
913 ::memcpy(&ptr, src, sizeof(void *));
914 // I can't decide whether the size operand should apply to the bytes in
915 // their lldb-host endianness or the target endianness.. I doubt this'll
916 // ever come up but I'll opt for assuming big endian regardless.
917 switch (size) {
918 case 1:
919 ptr = ptr & 0xff;
920 break;
921 case 2:
922 ptr = ptr & 0xffff;
923 break;
924 case 3:
925 ptr = ptr & 0xffffff;
926 break;
927 case 4:
928 ptr = ptr & 0xffffffff;
929 break;
930 // The casts are added to work around the case where intptr_t is a 32-bit
931 // quantity. Presumably we won't hit the 5..7 cases if (void*) is 32-bits in
932 // this program.
933 case 5:
934 ptr = (intptr_t)ptr & 0xffffffffffULL;
935 break;
936 case 6:
937 ptr = (intptr_t)ptr & 0xffffffffffffULL;
938 break;
939 case 7:
940 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
941 break;
942 default:
943 break;
944 }
945 stack.back().GetScalar() = ptr;
946 stack.back().ClearContext();
947 } break;
949 auto file_addr = stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
950 Address so_addr;
951 auto maybe_load_addr = ResolveLoadAddress(
952 exe_ctx, module_sp, "DW_OP_deref_size", file_addr, so_addr,
953 /*check_sectionoffset=*/true);
954
955 if (!maybe_load_addr)
956 return maybe_load_addr.takeError();
957
958 addr_t load_addr = *maybe_load_addr;
959
960 if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) {
961 uint8_t addr_bytes[8];
963
964 if (!target || target->ReadMemory(so_addr, &addr_bytes, size, error,
965 /*force_live_memory=*/false) != size)
966 return llvm::createStringError(
967 "failed to dereference pointer for DW_OP_deref_size: "
968 "%s\n",
969 error.AsCString());
970
971 ObjectFile *objfile = module_sp->GetObjectFile();
972
973 stack.back().GetScalar() = DerefSizeExtractDataHelper(
974 addr_bytes, size, objfile->GetByteOrder(), size);
975 stack.back().ClearContext();
976 break;
977 }
978 stack.back().GetScalar() = load_addr;
979 // Fall through to load address promotion code below.
980 }
981
982 [[fallthrough]];
984 // Promote Scalar to LoadAddress and fall through.
985 stack.back().SetValueType(Value::ValueType::LoadAddress);
986 [[fallthrough]];
988 if (!exe_ctx)
989 return llvm::createStringError(
990 "no execution context for DW_OP_deref_size");
991 if (!process)
992 return llvm::createStringError("no process for DW_OP_deref_size");
993
994 lldb::addr_t pointer_addr =
995 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
996 uint8_t addr_bytes[sizeof(lldb::addr_t)];
998
999 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) != size)
1000 return llvm::createStringError(
1001 "failed to dereference pointer from 0x%" PRIx64
1002 " for DW_OP_deref_size: %s\n",
1003 pointer_addr, error.AsCString());
1004
1005 stack.back().GetScalar() = DerefSizeExtractDataHelper(
1006 addr_bytes, sizeof(addr_bytes), process->GetByteOrder(), size);
1007 stack.back().ClearContext();
1008 } break;
1009
1011 return llvm::createStringError("invalid value for DW_OP_deref_size");
1012 }
1013
1014 return llvm::Error::success();
1015}
1016
1017llvm::Expected<Value> DWARFExpression::Evaluate(
1018 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
1019 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
1020 const DWARFExpression::Delegate *dwarf_cu,
1021 const lldb::RegisterKind reg_kind, const Value *initial_value_ptr,
1022 const Value *object_address_ptr) {
1023 uint32_t address_size = opcodes.GetAddressByteSize();
1024 llvm::DataExtractor expr_data = opcodes.GetAsLLVM();
1025 llvm::DWARFExpression expr(expr_data, address_size);
1026
1027 if (expr_data.size() == 0)
1028 return llvm::createStringError(
1029 "no location, value may have been optimized out");
1030
1031 Stack stack;
1032
1033 Process *process = nullptr;
1034 StackFrame *frame = nullptr;
1035 Target *target = nullptr;
1036
1037 if (exe_ctx) {
1038 process = exe_ctx->GetProcessPtr();
1039 frame = exe_ctx->GetFramePtr();
1040 target = exe_ctx->GetTargetPtr();
1041 }
1042 if (reg_ctx == nullptr && frame)
1043 reg_ctx = frame->GetRegisterContext().get();
1044
1045 if (initial_value_ptr)
1046 stack.push_back(*initial_value_ptr);
1047
1048 Value tmp;
1049 uint32_t reg_num;
1050
1051 /// Insertion point for evaluating multi-piece expression.
1052 uint64_t op_piece_offset = 0;
1053 Value pieces; // Used for DW_OP_piece
1054
1056 // A generic type is "an integral type that has the size of an address and an
1057 // unspecified signedness". For now, just use the signedness of the operand.
1058 // TODO: Implement a real typed stack, and store the genericness of the value
1059 // there.
1060 auto to_generic = [&](auto v) {
1061 // TODO: Avoid implicit trunc?
1062 // See https://github.com/llvm/llvm-project/issues/112510.
1063 bool is_signed = std::is_signed<decltype(v)>::value;
1064 return Scalar(llvm::APSInt(
1065 llvm::APInt(8 * address_size, v, is_signed, /*implicitTrunc=*/true),
1066 !is_signed));
1067 };
1068
1069 // The default kind is a memory location. This is updated by any
1070 // operation that changes this, such as DW_OP_stack_value, and reset
1071 // by composition operations like DW_OP_piece.
1072 LocationDescriptionKind dwarf4_location_description_kind = Memory;
1073
1074 llvm::DWARFExpression::iterator op = expr.begin(), op_end = expr.end();
1075 while (op != op_end) {
1076 const uint64_t op_offset = op.getOffset();
1077 const LocationAtom opcode = static_cast<LocationAtom>(op->getCode());
1078
1079 if (log && log->GetVerbose()) {
1080 size_t count = stack.size();
1081 LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
1082 static_cast<uint64_t>(count));
1083 for (size_t i = 0; i < count; ++i) {
1084 StreamString new_value;
1085 new_value.Printf("[%" PRIu64 "]", static_cast<uint64_t>(i));
1086 stack[i].Dump(&new_value);
1087 LLDB_LOGF(log, " %s", new_value.GetData());
1088 }
1089 LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
1090 DW_OP_value_to_name(opcode));
1091 }
1092
1093 if (std::optional<unsigned> arity = OperationArity(opcode)) {
1094 if (stack.size() < *arity)
1095 return llvm::createStringError(
1096 "%s needs at least %d stack entries (stack has %d entries)",
1097 DW_OP_value_to_name(opcode), *arity, stack.size());
1098 }
1099
1100 switch (opcode) {
1101 // The DW_OP_addr operation has a single operand that encodes a machine
1102 // address and whose size is the size of an address on the target machine.
1103 case DW_OP_addr:
1104 stack.push_back(Scalar(op->getRawOperand(0)));
1105 if (target &&
1107 // wasm file sections aren't mapped into memory, therefore addresses can
1108 // never point into a file section and are always LoadAddresses.
1109 stack.back().SetValueType(Value::ValueType::LoadAddress);
1110 } else {
1111 stack.back().SetValueType(Value::ValueType::FileAddress);
1112 }
1113 break;
1114
1115 // The DW_OP_addr_sect_offset4 is used for any location expressions in
1116 // shared libraries that have a location like:
1117 // DW_OP_addr(0x1000)
1118 // If this address resides in a shared library, then this virtual address
1119 // won't make sense when it is evaluated in the context of a running
1120 // process where shared libraries have been slid. To account for this, this
1121 // new address type where we can store the section pointer and a 4 byte
1122 // offset.
1123 // case DW_OP_addr_sect_offset4:
1124 // {
1125 // result_type = eResultTypeFileAddress;
1126 // lldb::Section *sect = (lldb::Section
1127 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
1128 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
1129 //
1130 // Address so_addr (sect, sect_offset);
1131 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
1132 // if (load_addr != LLDB_INVALID_ADDRESS)
1133 // {
1134 // // We successfully resolve a file address to a load
1135 // // address.
1136 // stack.push_back(load_addr);
1137 // break;
1138 // }
1139 // else
1140 // {
1141 // // We were able
1142 // if (error_ptr)
1143 // error_ptr->SetErrorStringWithFormat ("Section %s in
1144 // %s is not currently loaded.\n",
1145 // sect->GetName().AsCString(),
1146 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
1147 // return false;
1148 // }
1149 // }
1150 // break;
1151
1152 // OPCODE: DW_OP_deref
1153 // OPERANDS: none
1154 // DESCRIPTION: Pops the top stack entry and treats it as an address.
1155 // The value retrieved from that address is pushed. The size of the data
1156 // retrieved from the dereferenced address is the size of an address on the
1157 // target machine.
1158 case DW_OP_deref: {
1159 size_t size = address_size;
1160 if (llvm::Error err = Evaluate_DW_OP_deref_size(
1161 stack, exe_ctx, module_sp, process, target, size, size,
1162 dwarf4_location_description_kind))
1163 return err;
1164 } break;
1165
1166 // OPCODE: DW_OP_deref_size
1167 // OPERANDS: 1
1168 // 1 - uint8_t that specifies the size of the data to dereference.
1169 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1170 // stack entry and treats it as an address. The value retrieved from that
1171 // address is pushed. In the DW_OP_deref_size operation, however, the size
1172 // in bytes of the data retrieved from the dereferenced address is
1173 // specified by the single operand. This operand is a 1-byte unsigned
1174 // integral constant whose value may not be larger than the size of an
1175 // address on the target machine. The data retrieved is zero extended to
1176 // the size of an address on the target machine before being pushed on the
1177 // expression stack.
1178 case DW_OP_deref_size: {
1179 size_t size = op->getRawOperand(0);
1180 if (llvm::Error err = Evaluate_DW_OP_deref_size(
1181 stack, exe_ctx, module_sp, process, target, size, address_size,
1182 dwarf4_location_description_kind))
1183 return err;
1184 } break;
1185
1186 // OPCODE: DW_OP_xderef_size
1187 // OPERANDS: 1
1188 // 1 - uint8_t that specifies the size of the data to dereference.
1189 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1190 // the top of the stack is treated as an address. The second stack entry is
1191 // treated as an "address space identifier" for those architectures that
1192 // support multiple address spaces. The top two stack elements are popped,
1193 // a data item is retrieved through an implementation-defined address
1194 // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1195 // operation, however, the size in bytes of the data retrieved from the
1196 // dereferenced address is specified by the single operand. This operand is
1197 // a 1-byte unsigned integral constant whose value may not be larger than
1198 // the size of an address on the target machine. The data retrieved is zero
1199 // extended to the size of an address on the target machine before being
1200 // pushed on the expression stack.
1201 case DW_OP_xderef_size:
1202 return llvm::createStringError("unimplemented opcode: DW_OP_xderef_size");
1203 // OPCODE: DW_OP_xderef
1204 // OPERANDS: none
1205 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1206 // the top of the stack is treated as an address. The second stack entry is
1207 // treated as an "address space identifier" for those architectures that
1208 // support multiple address spaces. The top two stack elements are popped,
1209 // a data item is retrieved through an implementation-defined address
1210 // calculation and pushed as the new stack top. The size of the data
1211 // retrieved from the dereferenced address is the size of an address on the
1212 // target machine.
1213 case DW_OP_xderef:
1214 return llvm::createStringError("unimplemented opcode: DW_OP_xderef");
1215
1216 // All DW_OP_constXXX opcodes have a single operand as noted below:
1217 //
1218 // Opcode Operand 1
1219 // DW_OP_const1u 1-byte unsigned integer constant
1220 // DW_OP_const1s 1-byte signed integer constant
1221 // DW_OP_const2u 2-byte unsigned integer constant
1222 // DW_OP_const2s 2-byte signed integer constant
1223 // DW_OP_const4u 4-byte unsigned integer constant
1224 // DW_OP_const4s 4-byte signed integer constant
1225 // DW_OP_const8u 8-byte unsigned integer constant
1226 // DW_OP_const8s 8-byte signed integer constant
1227 // DW_OP_constu unsigned LEB128 integer constant
1228 // DW_OP_consts signed LEB128 integer constant
1229 case DW_OP_const1u:
1230 stack.push_back(to_generic(op->getRawOperand(0)));
1231 break;
1232 case DW_OP_const1s:
1233 stack.push_back(to_generic(static_cast<int8_t>(op->getRawOperand(0))));
1234 break;
1235 case DW_OP_const2u:
1236 stack.push_back(to_generic(op->getRawOperand(0)));
1237 break;
1238 case DW_OP_const2s:
1239 stack.push_back(to_generic(static_cast<int16_t>(op->getRawOperand(0))));
1240 break;
1241 case DW_OP_const4u:
1242 stack.push_back(to_generic(op->getRawOperand(0)));
1243 break;
1244 case DW_OP_const4s:
1245 stack.push_back(to_generic(static_cast<int32_t>(op->getRawOperand(0))));
1246 break;
1247 case DW_OP_const8u:
1248 stack.push_back(to_generic(op->getRawOperand(0)));
1249 break;
1250 case DW_OP_const8s:
1251 stack.push_back(to_generic(static_cast<int64_t>(op->getRawOperand(0))));
1252 break;
1253 // These should also use to_generic, but we can't do that due to a
1254 // producer-side bug in llvm. See llvm.org/pr48087.
1255 case DW_OP_constu:
1256 stack.push_back(Scalar(op->getRawOperand(0)));
1257 break;
1258 case DW_OP_consts:
1259 stack.push_back(Scalar(static_cast<int64_t>(op->getRawOperand(0))));
1260 break;
1261
1262 // OPCODE: DW_OP_dup
1263 // OPERANDS: none
1264 // DESCRIPTION: duplicates the value at the top of the stack
1265 case DW_OP_dup:
1266 if (stack.empty()) {
1267 return llvm::createStringError("expression stack empty for DW_OP_dup");
1268 } else
1269 stack.push_back(stack.back());
1270 break;
1271
1272 // OPCODE: DW_OP_drop
1273 // OPERANDS: none
1274 // DESCRIPTION: pops the value at the top of the stack
1275 case DW_OP_drop:
1276 if (stack.empty()) {
1277 return llvm::createStringError("expression stack empty for DW_OP_drop");
1278 } else
1279 stack.pop_back();
1280 break;
1281
1282 // OPCODE: DW_OP_over
1283 // OPERANDS: none
1284 // DESCRIPTION: Duplicates the entry currently second in the stack at
1285 // the top of the stack.
1286 case DW_OP_over:
1287 stack.push_back(stack[stack.size() - 2]);
1288 break;
1289
1290 // OPCODE: DW_OP_pick
1291 // OPERANDS: uint8_t index into the current stack
1292 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1293 // inclusive) is pushed on the stack
1294 case DW_OP_pick: {
1295 uint8_t pick_idx = op->getRawOperand(0);
1296 if (pick_idx < stack.size())
1297 stack.push_back(stack[stack.size() - 1 - pick_idx]);
1298 else {
1299 return llvm::createStringError(
1300 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1301 }
1302 } break;
1303
1304 // OPCODE: DW_OP_swap
1305 // OPERANDS: none
1306 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1307 // of the stack becomes the second stack entry, and the second entry
1308 // becomes the top of the stack
1309 case DW_OP_swap:
1310 tmp = stack.back();
1311 stack.back() = stack[stack.size() - 2];
1312 stack[stack.size() - 2] = tmp;
1313 break;
1314
1315 // OPCODE: DW_OP_rot
1316 // OPERANDS: none
1317 // DESCRIPTION: Rotates the first three stack entries. The entry at
1318 // the top of the stack becomes the third stack entry, the second entry
1319 // becomes the top of the stack, and the third entry becomes the second
1320 // entry.
1321 case DW_OP_rot: {
1322 size_t last_idx = stack.size() - 1;
1323 Value old_top = stack[last_idx];
1324 stack[last_idx] = stack[last_idx - 1];
1325 stack[last_idx - 1] = stack[last_idx - 2];
1326 stack[last_idx - 2] = old_top;
1327 } break;
1328
1329 // OPCODE: DW_OP_abs
1330 // OPERANDS: none
1331 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1332 // value and pushes its absolute value. If the absolute value can not be
1333 // represented, the result is undefined.
1334 case DW_OP_abs:
1335 if (!stack.back().GetScalar().AbsoluteValue()) {
1336 return llvm::createStringError(
1337 "failed to take the absolute value of the first stack item");
1338 }
1339 break;
1340
1341 // OPCODE: DW_OP_and
1342 // OPERANDS: none
1343 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1344 // operation on the two, and pushes the result.
1345 case DW_OP_and:
1346 tmp = stack.back();
1347 stack.pop_back();
1348 stack.back().GetScalar() = stack.back().GetScalar() & tmp.GetScalar();
1349 break;
1350
1351 // OPCODE: DW_OP_div
1352 // OPERANDS: none
1353 // DESCRIPTION: pops the top two stack values, divides the former second
1354 // entry by the former top of the stack using signed division, and pushes
1355 // the result.
1356 case DW_OP_div: {
1357 tmp = stack.back();
1358 if (tmp.GetScalar().IsZero())
1359 return llvm::createStringError("divide by zero");
1360
1361 stack.pop_back();
1362 Scalar divisor, dividend;
1363 divisor = tmp.GetScalar();
1364 dividend = stack.back().GetScalar();
1365 divisor.MakeSigned();
1366 dividend.MakeSigned();
1367 stack.back() = dividend / divisor;
1368
1369 if (!stack.back().GetScalar().IsValid())
1370 return llvm::createStringError("divide failed");
1371 } break;
1372
1373 // OPCODE: DW_OP_minus
1374 // OPERANDS: none
1375 // DESCRIPTION: pops the top two stack values, subtracts the former top
1376 // of the stack from the former second entry, and pushes the result.
1377 case DW_OP_minus:
1378 tmp = stack.back();
1379 stack.pop_back();
1380 stack.back().GetScalar() = stack.back().GetScalar() - tmp.GetScalar();
1381 break;
1382
1383 // OPCODE: DW_OP_mod
1384 // OPERANDS: none
1385 // DESCRIPTION: pops the top two stack values and pushes the result of
1386 // the calculation: former second stack entry modulo the former top of the
1387 // stack.
1388 case DW_OP_mod:
1389 tmp = stack.back();
1390 stack.pop_back();
1391 stack.back().GetScalar() = stack.back().GetScalar() % tmp.GetScalar();
1392 break;
1393
1394 // OPCODE: DW_OP_mul
1395 // OPERANDS: none
1396 // DESCRIPTION: pops the top two stack entries, multiplies them
1397 // together, and pushes the result.
1398 case DW_OP_mul:
1399 tmp = stack.back();
1400 stack.pop_back();
1401 stack.back().GetScalar() = stack.back().GetScalar() * tmp.GetScalar();
1402 break;
1403
1404 // OPCODE: DW_OP_neg
1405 // OPERANDS: none
1406 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1407 case DW_OP_neg:
1408 if (!stack.back().GetScalar().UnaryNegate())
1409 return llvm::createStringError("unary negate failed");
1410 break;
1411
1412 // OPCODE: DW_OP_not
1413 // OPERANDS: none
1414 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1415 // complement
1416 case DW_OP_not:
1417 if (!stack.back().GetScalar().OnesComplement())
1418 return llvm::createStringError("logical NOT failed");
1419 break;
1420
1421 // OPCODE: DW_OP_or
1422 // OPERANDS: none
1423 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1424 // operation on the two, and pushes the result.
1425 case DW_OP_or:
1426 tmp = stack.back();
1427 stack.pop_back();
1428 stack.back().GetScalar() = stack.back().GetScalar() | tmp.GetScalar();
1429 break;
1430
1431 // OPCODE: DW_OP_plus
1432 // OPERANDS: none
1433 // DESCRIPTION: pops the top two stack entries, adds them together, and
1434 // pushes the result.
1435 case DW_OP_plus:
1436 tmp = stack.back();
1437 stack.pop_back();
1438 stack.back().GetScalar() += tmp.GetScalar();
1439 break;
1440
1441 // OPCODE: DW_OP_plus_uconst
1442 // OPERANDS: none
1443 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1444 // constant operand and pushes the result.
1445 case DW_OP_plus_uconst: {
1446 const uint64_t uconst_value = op->getRawOperand(0);
1447 // Implicit conversion from a UINT to a Scalar...
1448 stack.back().GetScalar() += uconst_value;
1449 if (!stack.back().GetScalar().IsValid())
1450 return llvm::createStringError("DW_OP_plus_uconst failed");
1451 } break;
1452
1453 // OPCODE: DW_OP_shl
1454 // OPERANDS: none
1455 // DESCRIPTION: pops the top two stack entries, shifts the former
1456 // second entry left by the number of bits specified by the former top of
1457 // the stack, and pushes the result.
1458 case DW_OP_shl:
1459 tmp = stack.back();
1460 stack.pop_back();
1461 stack.back().GetScalar() <<= tmp.GetScalar();
1462 break;
1463
1464 // OPCODE: DW_OP_shr
1465 // OPERANDS: none
1466 // DESCRIPTION: pops the top two stack entries, shifts the former second
1467 // entry right logically (filling with zero bits) by the number of bits
1468 // specified by the former top of the stack, and pushes the result.
1469 case DW_OP_shr:
1470 tmp = stack.back();
1471 stack.pop_back();
1472 if (!stack.back().GetScalar().ShiftRightLogical(tmp.GetScalar()))
1473 return llvm::createStringError("DW_OP_shr failed");
1474 break;
1475
1476 // OPCODE: DW_OP_shra
1477 // OPERANDS: none
1478 // DESCRIPTION: pops the top two stack entries, shifts the former second
1479 // entry right arithmetically (divide the magnitude by 2, keep the same
1480 // sign for the result) by the number of bits specified by the former top
1481 // of the stack, and pushes the result.
1482 case DW_OP_shra:
1483 tmp = stack.back();
1484 stack.pop_back();
1485 stack.back().GetScalar() >>= tmp.GetScalar();
1486 break;
1487
1488 // OPCODE: DW_OP_xor
1489 // OPERANDS: none
1490 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1491 // exclusive-or operation on the two, and pushes the result.
1492 case DW_OP_xor:
1493 tmp = stack.back();
1494 stack.pop_back();
1495 stack.back().GetScalar() = stack.back().GetScalar() ^ tmp.GetScalar();
1496 break;
1497
1498 // OPCODE: DW_OP_skip
1499 // OPERANDS: int16_t
1500 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1501 // signed integer constant. The 2-byte constant is the number of bytes of
1502 // the DWARF expression to skip forward or backward from the current
1503 // operation, beginning after the 2-byte constant.
1504 case DW_OP_skip: {
1505 int16_t skip_offset = static_cast<int16_t>(op->getRawOperand(0));
1506 lldb::offset_t new_offset = op->getEndOffset() + skip_offset;
1507 // New offset can point at the end of the data, in this case we should
1508 // terminate the DWARF expression evaluation (will happen in the loop
1509 // condition).
1510 if (new_offset <= expr_data.size()) {
1511 op = op.skipBytes(skip_offset);
1512 continue;
1513 }
1514 return llvm::createStringErrorV(
1515 "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}",
1516 op->getEndOffset(), skip_offset, expr_data.size());
1517 }
1518
1519 // OPCODE: DW_OP_bra
1520 // OPERANDS: int16_t
1521 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1522 // signed integer constant. This operation pops the top of stack. If the
1523 // value popped is not the constant 0, the 2-byte constant operand is the
1524 // number of bytes of the DWARF expression to skip forward or backward from
1525 // the current operation, beginning after the 2-byte constant.
1526 case DW_OP_bra: {
1527 tmp = stack.back();
1528 stack.pop_back();
1529 int16_t bra_offset = static_cast<int16_t>(op->getRawOperand(0));
1530 Scalar zero(0);
1531 if (tmp.GetScalar() != zero) {
1532 lldb::offset_t new_offset = op->getEndOffset() + bra_offset;
1533 // New offset can point at the end of the data, in this case we should
1534 // terminate the DWARF expression evaluation (will happen in the loop
1535 // condition).
1536 if (new_offset <= expr_data.size()) {
1537 op = op.skipBytes(bra_offset);
1538 continue;
1539 }
1540 return llvm::createStringErrorV(
1541 "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}",
1542 op->getEndOffset(), bra_offset, expr_data.size());
1543 }
1544 } break;
1545
1546 // OPCODE: DW_OP_eq
1547 // OPERANDS: none
1548 // DESCRIPTION: pops the top two stack values, compares using the
1549 // equals (==) operator.
1550 // STACK RESULT: push the constant value 1 onto the stack if the result
1551 // of the operation is true or the constant value 0 if the result of the
1552 // operation is false.
1553 case DW_OP_eq:
1554 tmp = stack.back();
1555 stack.pop_back();
1556 stack.back().GetScalar() = stack.back().GetScalar() == tmp.GetScalar();
1557 break;
1558
1559 // OPCODE: DW_OP_ge
1560 // OPERANDS: none
1561 // DESCRIPTION: pops the top two stack values, compares using the
1562 // greater than or equal to (>=) operator.
1563 // STACK RESULT: push the constant value 1 onto the stack if the result
1564 // of the operation is true or the constant value 0 if the result of the
1565 // operation is false.
1566 case DW_OP_ge:
1567 tmp = stack.back();
1568 stack.pop_back();
1569 stack.back().GetScalar() = stack.back().GetScalar() >= tmp.GetScalar();
1570 break;
1571
1572 // OPCODE: DW_OP_gt
1573 // OPERANDS: none
1574 // DESCRIPTION: pops the top two stack values, compares using the
1575 // greater than (>) operator.
1576 // STACK RESULT: push the constant value 1 onto the stack if the result
1577 // of the operation is true or the constant value 0 if the result of the
1578 // operation is false.
1579 case DW_OP_gt:
1580 tmp = stack.back();
1581 stack.pop_back();
1582 stack.back().GetScalar() = stack.back().GetScalar() > tmp.GetScalar();
1583 break;
1584
1585 // OPCODE: DW_OP_le
1586 // OPERANDS: none
1587 // DESCRIPTION: pops the top two stack values, compares using the
1588 // less than or equal to (<=) operator.
1589 // STACK RESULT: push the constant value 1 onto the stack if the result
1590 // of the operation is true or the constant value 0 if the result of the
1591 // operation is false.
1592 case DW_OP_le:
1593 tmp = stack.back();
1594 stack.pop_back();
1595 stack.back().GetScalar() = stack.back().GetScalar() <= tmp.GetScalar();
1596 break;
1597
1598 // OPCODE: DW_OP_lt
1599 // OPERANDS: none
1600 // DESCRIPTION: pops the top two stack values, compares using the
1601 // less than (<) operator.
1602 // STACK RESULT: push the constant value 1 onto the stack if the result
1603 // of the operation is true or the constant value 0 if the result of the
1604 // operation is false.
1605 case DW_OP_lt:
1606 tmp = stack.back();
1607 stack.pop_back();
1608 stack.back().GetScalar() = stack.back().GetScalar() < tmp.GetScalar();
1609 break;
1610
1611 // OPCODE: DW_OP_ne
1612 // OPERANDS: none
1613 // DESCRIPTION: pops the top two stack values, compares using the
1614 // not equal (!=) operator.
1615 // STACK RESULT: push the constant value 1 onto the stack if the result
1616 // of the operation is true or the constant value 0 if the result of the
1617 // operation is false.
1618 case DW_OP_ne:
1619 tmp = stack.back();
1620 stack.pop_back();
1621 stack.back().GetScalar() = stack.back().GetScalar() != tmp.GetScalar();
1622 break;
1623
1624 // OPCODE: DW_OP_litn
1625 // OPERANDS: none
1626 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1627 // STACK RESULT: push the unsigned literal constant value onto the top
1628 // of the stack.
1629 case DW_OP_lit0:
1630 case DW_OP_lit1:
1631 case DW_OP_lit2:
1632 case DW_OP_lit3:
1633 case DW_OP_lit4:
1634 case DW_OP_lit5:
1635 case DW_OP_lit6:
1636 case DW_OP_lit7:
1637 case DW_OP_lit8:
1638 case DW_OP_lit9:
1639 case DW_OP_lit10:
1640 case DW_OP_lit11:
1641 case DW_OP_lit12:
1642 case DW_OP_lit13:
1643 case DW_OP_lit14:
1644 case DW_OP_lit15:
1645 case DW_OP_lit16:
1646 case DW_OP_lit17:
1647 case DW_OP_lit18:
1648 case DW_OP_lit19:
1649 case DW_OP_lit20:
1650 case DW_OP_lit21:
1651 case DW_OP_lit22:
1652 case DW_OP_lit23:
1653 case DW_OP_lit24:
1654 case DW_OP_lit25:
1655 case DW_OP_lit26:
1656 case DW_OP_lit27:
1657 case DW_OP_lit28:
1658 case DW_OP_lit29:
1659 case DW_OP_lit30:
1660 case DW_OP_lit31:
1661 stack.push_back(to_generic(opcode - DW_OP_lit0));
1662 break;
1663
1664 // OPCODE: DW_OP_regN
1665 // OPERANDS: none
1666 // DESCRIPTION: Push the value in register n on the top of the stack.
1667 case DW_OP_reg0:
1668 case DW_OP_reg1:
1669 case DW_OP_reg2:
1670 case DW_OP_reg3:
1671 case DW_OP_reg4:
1672 case DW_OP_reg5:
1673 case DW_OP_reg6:
1674 case DW_OP_reg7:
1675 case DW_OP_reg8:
1676 case DW_OP_reg9:
1677 case DW_OP_reg10:
1678 case DW_OP_reg11:
1679 case DW_OP_reg12:
1680 case DW_OP_reg13:
1681 case DW_OP_reg14:
1682 case DW_OP_reg15:
1683 case DW_OP_reg16:
1684 case DW_OP_reg17:
1685 case DW_OP_reg18:
1686 case DW_OP_reg19:
1687 case DW_OP_reg20:
1688 case DW_OP_reg21:
1689 case DW_OP_reg22:
1690 case DW_OP_reg23:
1691 case DW_OP_reg24:
1692 case DW_OP_reg25:
1693 case DW_OP_reg26:
1694 case DW_OP_reg27:
1695 case DW_OP_reg28:
1696 case DW_OP_reg29:
1697 case DW_OP_reg30:
1698 case DW_OP_reg31: {
1699 dwarf4_location_description_kind = Register;
1700 reg_num = opcode - DW_OP_reg0;
1701
1702 if (llvm::Error err =
1703 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1704 return err;
1705 stack.push_back(tmp);
1706 } break;
1707 // OPCODE: DW_OP_regx
1708 // OPERANDS:
1709 // ULEB128 literal operand that encodes the register.
1710 // DESCRIPTION: Push the value in register on the top of the stack.
1711 case DW_OP_regx: {
1712 dwarf4_location_description_kind = Register;
1713 reg_num = op->getRawOperand(0);
1714 Status read_err;
1715 if (llvm::Error err =
1716 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1717 return err;
1718 stack.push_back(tmp);
1719 } break;
1720
1721 // OPCODE: DW_OP_bregN
1722 // OPERANDS:
1723 // SLEB128 offset from register N
1724 // DESCRIPTION: Value is in memory at the address specified by register
1725 // N plus an offset.
1726 case DW_OP_breg0:
1727 case DW_OP_breg1:
1728 case DW_OP_breg2:
1729 case DW_OP_breg3:
1730 case DW_OP_breg4:
1731 case DW_OP_breg5:
1732 case DW_OP_breg6:
1733 case DW_OP_breg7:
1734 case DW_OP_breg8:
1735 case DW_OP_breg9:
1736 case DW_OP_breg10:
1737 case DW_OP_breg11:
1738 case DW_OP_breg12:
1739 case DW_OP_breg13:
1740 case DW_OP_breg14:
1741 case DW_OP_breg15:
1742 case DW_OP_breg16:
1743 case DW_OP_breg17:
1744 case DW_OP_breg18:
1745 case DW_OP_breg19:
1746 case DW_OP_breg20:
1747 case DW_OP_breg21:
1748 case DW_OP_breg22:
1749 case DW_OP_breg23:
1750 case DW_OP_breg24:
1751 case DW_OP_breg25:
1752 case DW_OP_breg26:
1753 case DW_OP_breg27:
1754 case DW_OP_breg28:
1755 case DW_OP_breg29:
1756 case DW_OP_breg30:
1757 case DW_OP_breg31: {
1758 reg_num = opcode - DW_OP_breg0;
1759 if (llvm::Error err =
1760 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1761 return err;
1762
1763 int64_t breg_offset = op->getRawOperand(0);
1764 tmp.GetScalar() += static_cast<uint64_t>(breg_offset);
1765 tmp.ClearContext();
1766 stack.push_back(tmp);
1767 stack.back().SetValueType(Value::ValueType::LoadAddress);
1768 } break;
1769 // OPCODE: DW_OP_bregx
1770 // OPERANDS: 2
1771 // ULEB128 literal operand that encodes the register.
1772 // SLEB128 offset from register N
1773 // DESCRIPTION: Value is in memory at the address specified by register
1774 // N plus an offset.
1775 case DW_OP_bregx: {
1776 reg_num = op->getRawOperand(0);
1777 if (llvm::Error err =
1778 ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, tmp))
1779 return err;
1780
1781 int64_t breg_offset = op->getRawOperand(1);
1782 tmp.GetScalar() += static_cast<uint64_t>(breg_offset);
1783 tmp.ClearContext();
1784 stack.push_back(tmp);
1785 stack.back().SetValueType(Value::ValueType::LoadAddress);
1786 } break;
1787
1788 case DW_OP_fbreg:
1789 if (exe_ctx) {
1790 if (frame) {
1791 Scalar value;
1792 if (llvm::Error err = frame->GetFrameBaseValue(value))
1793 return err;
1794 int64_t fbreg_offset = op->getRawOperand(0);
1795 value += fbreg_offset;
1796 stack.push_back(value);
1797 stack.back().SetValueType(Value::ValueType::LoadAddress);
1798 } else {
1799 return llvm::createStringError(
1800 "invalid stack frame in context for DW_OP_fbreg opcode");
1801 }
1802 } else {
1803 return llvm::createStringError(
1804 "NULL execution context for DW_OP_fbreg");
1805 }
1806
1807 break;
1808
1809 // OPCODE: DW_OP_nop
1810 // OPERANDS: none
1811 // DESCRIPTION: A place holder. It has no effect on the location stack
1812 // or any of its values.
1813 case DW_OP_nop:
1814 break;
1815
1816 // OPCODE: DW_OP_piece
1817 // OPERANDS: 1
1818 // ULEB128: byte size of the piece
1819 // DESCRIPTION: The operand describes the size in bytes of the piece of
1820 // the object referenced by the DWARF expression whose result is at the top
1821 // of the stack. If the piece is located in a register, but does not occupy
1822 // the entire register, the placement of the piece within that register is
1823 // defined by the ABI.
1824 //
1825 // Many compilers store a single variable in sets of registers, or store a
1826 // variable partially in memory and partially in registers. DW_OP_piece
1827 // provides a way of describing how large a part of a variable a particular
1828 // DWARF expression refers to.
1829 case DW_OP_piece: {
1830 LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind;
1831 // Reset for the next piece.
1832 dwarf4_location_description_kind = Memory;
1833
1834 const uint64_t piece_byte_size = op->getRawOperand(0);
1835
1836 if (piece_byte_size > 0) {
1837 Value curr_piece;
1838
1839 if (stack.empty()) {
1840 UpdateValueTypeFromLocationDescription(
1841 log, dwarf_cu, LocationDescriptionKind::Empty);
1842 // In a multi-piece expression, this means that the current piece is
1843 // not available. Fill with zeros for now by resizing the data and
1844 // appending it
1845 curr_piece.ResizeData(piece_byte_size);
1846 // Note that "0" is not a correct value for the unknown bits.
1847 // It would be better to also return a mask of valid bits together
1848 // with the expression result, so the debugger can print missing
1849 // members as "<optimized out>" or something.
1850 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
1851 pieces.AppendDataToHostBuffer(curr_piece);
1852 } else {
1853 Status error;
1854 // Extract the current piece into "curr_piece"
1855 Value curr_piece_source_value(stack.back());
1856 stack.pop_back();
1857 UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc,
1858 &curr_piece_source_value);
1859
1860 const Value::ValueType curr_piece_source_value_type =
1861 curr_piece_source_value.GetValueType();
1862 Scalar &scalar = curr_piece_source_value.GetScalar();
1864 switch (curr_piece_source_value_type) {
1866 return llvm::createStringError("invalid value type");
1868 if (target) {
1869 curr_piece_source_value.ConvertToLoadAddress(module_sp.get(),
1870 target);
1871 addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1872 } else {
1873 return llvm::createStringError(
1874 "unable to convert file address 0x%" PRIx64
1875 " to load address "
1876 "for DW_OP_piece(%" PRIu64 "): "
1877 "no target available",
1878 addr, piece_byte_size);
1879 }
1880 [[fallthrough]];
1882 if (target) {
1883 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
1884 if (target->ReadMemory(
1885 Address(addr), curr_piece.GetBuffer().GetBytes(),
1886 piece_byte_size, error,
1887 /*force_live_memory=*/false) != piece_byte_size) {
1888 const char *addr_type = (curr_piece_source_value_type ==
1890 ? "load"
1891 : "file";
1892 return llvm::createStringError(
1893 "failed to read memory DW_OP_piece(%" PRIu64
1894 ") from %s address 0x%" PRIx64,
1895 piece_byte_size, addr_type, addr);
1896 }
1897 } else {
1898 return llvm::createStringError(
1899 "failed to resize the piece memory buffer for "
1900 "DW_OP_piece(%" PRIu64 ")",
1901 piece_byte_size);
1902 }
1903 }
1904 } break;
1906 return llvm::createStringError(
1907 "failed to read memory DW_OP_piece(%" PRIu64
1908 ") from host address 0x%" PRIx64,
1909 piece_byte_size, addr);
1910 } break;
1911
1913 uint32_t bit_size = piece_byte_size * 8;
1914 uint32_t bit_offset = 0;
1915 if (!scalar.ExtractBitfield(bit_size, bit_offset)) {
1916 return llvm::createStringError(
1917 "unable to extract %" PRIu64 " bytes from a %" PRIu64
1918 " byte scalar value.",
1919 piece_byte_size,
1920 (uint64_t)curr_piece_source_value.GetScalar().GetByteSize());
1921 }
1922
1923 // We have seen a case where we have expression like:
1924 // DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x28
1925 // here we are assuming the compiler was trying to zero
1926 // extend the value that we should append to the buffer.
1927 scalar.TruncOrExtendTo(bit_size, /*sign=*/false);
1928 curr_piece.GetScalar() = scalar;
1929 } break;
1930 }
1931
1932 // Check if this is the first piece?
1933 if (op_piece_offset == 0) {
1934 // This is the first piece, we should push it back onto the stack
1935 // so subsequent pieces will be able to access this piece and add
1936 // to it.
1937 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
1938 return llvm::createStringError("failed to append piece data");
1939 }
1940 } else {
1941 // If this is the second or later piece there should be a value on
1942 // the stack.
1943 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
1944 return llvm::createStringError(
1945 "DW_OP_piece for offset %" PRIu64
1946 " but top of stack is of size %" PRIu64,
1947 op_piece_offset, pieces.GetBuffer().GetByteSize());
1948 }
1949
1950 if (pieces.AppendDataToHostBuffer(curr_piece) == 0)
1951 return llvm::createStringError("failed to append piece data");
1952 }
1953 }
1954 op_piece_offset += piece_byte_size;
1955 }
1956 } break;
1957
1958 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
1959 if (stack.size() < 1) {
1960 UpdateValueTypeFromLocationDescription(log, dwarf_cu,
1961 LocationDescriptionKind::Empty);
1962 // Reset for the next piece.
1963 dwarf4_location_description_kind = Memory;
1964 return llvm::createStringError(
1965 "expression stack needs at least 1 item for DW_OP_bit_piece");
1966 } else {
1967 UpdateValueTypeFromLocationDescription(
1968 log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
1969 // Reset for the next piece.
1970 dwarf4_location_description_kind = Memory;
1971 const uint64_t piece_bit_size = op->getRawOperand(0);
1972 const uint64_t piece_bit_offset = op->getRawOperand(1);
1973 switch (stack.back().GetValueType()) {
1975 return llvm::createStringError(
1976 "unable to extract bit value from invalid value");
1978 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
1979 piece_bit_offset)) {
1980 return llvm::createStringError(
1981 "unable to extract %" PRIu64 " bit value with %" PRIu64
1982 " bit offset from a %" PRIu64 " bit scalar value.",
1983 piece_bit_size, piece_bit_offset,
1984 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
1985 }
1986 } break;
1987
1991 return llvm::createStringError(
1992 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
1993 ", bit_offset = %" PRIu64 ") from an address value.",
1994 piece_bit_size, piece_bit_offset);
1995 }
1996 }
1997 break;
1998
1999 // OPCODE: DW_OP_implicit_value
2000 // OPERANDS: 2
2001 // ULEB128 size of the value block in bytes
2002 // uint8_t* block bytes encoding value in target's memory
2003 // representation
2004 // DESCRIPTION: Value is immediately stored in block in the debug info with
2005 // the memory representation of the target.
2006 case DW_OP_implicit_value: {
2007 dwarf4_location_description_kind = Implicit;
2008
2009 // The second operand is a sequence of bytes of the length specified by
2010 // the first operand. LLVM represents it as an offset to that sequence.
2011 const uint64_t block_size = op->getRawOperand(0);
2012 uint64_t block_offset = op->getRawOperand(1);
2013
2014 llvm::Error error = llvm::Error::success();
2015 llvm::StringRef block_data =
2016 expr_data.getBytes(&block_offset, block_size, &error);
2017
2018 if (error)
2019 return error;
2020
2021 Value result(block_data.data(), block_data.size());
2022 stack.push_back(result);
2023 break;
2024 }
2025
2026 case DW_OP_implicit_pointer: {
2027 dwarf4_location_description_kind = Implicit;
2028 return llvm::createStringError("could not evaluate %s",
2029 DW_OP_value_to_name(opcode));
2030 }
2031
2032 // OPCODE: DW_OP_push_object_address
2033 // OPERANDS: none
2034 // DESCRIPTION: Pushes the address of the object currently being
2035 // evaluated as part of evaluation of a user presented expression. This
2036 // object may correspond to an independent variable described by its own
2037 // DIE or it may be a component of an array, structure, or class whose
2038 // address has been dynamically determined by an earlier step during user
2039 // expression evaluation.
2040 case DW_OP_push_object_address:
2041 if (object_address_ptr)
2042 stack.push_back(*object_address_ptr);
2043 else {
2044 return llvm::createStringError("DW_OP_push_object_address used without "
2045 "specifying an object address");
2046 }
2047 break;
2048
2049 // OPCODE: DW_OP_call2
2050 // OPERANDS:
2051 // uint16_t compile unit relative offset of a DIE
2052 // DESCRIPTION: Performs subroutine calls during evaluation
2053 // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2054 // debugging information entry in the current compilation unit.
2055 //
2056 // Operand interpretation is exactly like that for DW_FORM_ref2.
2057 //
2058 // This operation transfers control of DWARF expression evaluation to the
2059 // DW_AT_location attribute of the referenced DIE. If there is no such
2060 // attribute, then there is no effect. Execution of the DWARF expression of
2061 // a DW_AT_location attribute may add to and/or remove from values on the
2062 // stack. Execution returns to the point following the call when the end of
2063 // the attribute is reached. Values on the stack at the time of the call
2064 // may be used as parameters by the called expression and values left on
2065 // the stack by the called expression may be used as return values by prior
2066 // agreement between the calling and called expressions.
2067 case DW_OP_call2:
2068 return llvm::createStringError("unimplemented opcode DW_OP_call2");
2069 // OPCODE: DW_OP_call4
2070 // OPERANDS: 1
2071 // uint32_t compile unit relative offset of a DIE
2072 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2073 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2074 // a debugging information entry in the current compilation unit.
2075 //
2076 // Operand interpretation DW_OP_call4 is exactly like that for
2077 // DW_FORM_ref4.
2078 //
2079 // This operation transfers control of DWARF expression evaluation to the
2080 // DW_AT_location attribute of the referenced DIE. If there is no such
2081 // attribute, then there is no effect. Execution of the DWARF expression of
2082 // a DW_AT_location attribute may add to and/or remove from values on the
2083 // stack. Execution returns to the point following the call when the end of
2084 // the attribute is reached. Values on the stack at the time of the call
2085 // may be used as parameters by the called expression and values left on
2086 // the stack by the called expression may be used as return values by prior
2087 // agreement between the calling and called expressions.
2088 case DW_OP_call4:
2089 return llvm::createStringError("unimplemented opcode DW_OP_call4");
2090
2091 // OPCODE: DW_OP_stack_value
2092 // OPERANDS: None
2093 // DESCRIPTION: Specifies that the object does not exist in memory but
2094 // rather is a constant value. The value from the top of the stack is the
2095 // value to be used. This is the actual object value and not the location.
2096 case DW_OP_stack_value:
2097 dwarf4_location_description_kind = Implicit;
2098 stack.back().SetValueType(Value::ValueType::Scalar);
2099 break;
2100
2101 // OPCODE: DW_OP_convert
2102 // OPERANDS: 1
2103 // A ULEB128 that is either a DIE offset of a
2104 // DW_TAG_base_type or 0 for the generic (pointer-sized) type.
2105 //
2106 // DESCRIPTION: Pop the top stack element, convert it to a
2107 // different type, and push the result.
2108 case DW_OP_convert: {
2109 const uint64_t relative_die_offset = op->getRawOperand(0);
2110 uint64_t bit_size;
2111 bool sign;
2112 if (relative_die_offset == 0) {
2113 // The generic type has the size of an address on the target
2114 // machine and an unspecified signedness. Scalar has no
2115 // "unspecified signedness", so we use unsigned types.
2116 if (!module_sp)
2117 return llvm::createStringError("no module");
2118 sign = false;
2119 bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
2120 if (!bit_size)
2121 return llvm::createStringError("unspecified architecture");
2122 } else {
2123 auto bit_size_sign_or_err =
2124 dwarf_cu->GetDIEBitSizeAndSign(relative_die_offset);
2125 if (!bit_size_sign_or_err)
2126 return bit_size_sign_or_err.takeError();
2127 bit_size = bit_size_sign_or_err->first;
2128 sign = bit_size_sign_or_err->second;
2129 }
2130 Scalar &top = stack.back().GetScalar();
2131 top.TruncOrExtendTo(bit_size, sign);
2132 break;
2133 }
2134
2135 // OPCODE: DW_OP_call_frame_cfa
2136 // OPERANDS: None
2137 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2138 // the canonical frame address consistent with the call frame information
2139 // located in .debug_frame (or in the FDEs of the eh_frame section).
2140 case DW_OP_call_frame_cfa:
2141 if (frame) {
2142 // Note that we don't have to parse FDEs because this DWARF expression
2143 // is commonly evaluated with a valid stack frame.
2144 StackID id = frame->GetStackID();
2145 addr_t cfa = id.GetCallFrameAddressWithMetadata();
2146 if (cfa != LLDB_INVALID_ADDRESS) {
2147 stack.push_back(Scalar(cfa));
2148 stack.back().SetValueType(Value::ValueType::LoadAddress);
2149 } else {
2150 return llvm::createStringError(
2151 "stack frame does not include a canonical "
2152 "frame address for DW_OP_call_frame_cfa "
2153 "opcode");
2154 }
2155 } else {
2156 return llvm::createStringError("unvalid stack frame in context for "
2157 "DW_OP_call_frame_cfa opcode");
2158 }
2159 break;
2160
2161 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2162 // opcode, DW_OP_GNU_push_tls_address)
2163 // OPERANDS: none
2164 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2165 // an address in the current thread's thread-local storage block, and
2166 // pushes it on the stack.
2167 case DW_OP_form_tls_address:
2168 case DW_OP_GNU_push_tls_address: {
2169 if (stack.size() < 1) {
2170 if (opcode == DW_OP_form_tls_address)
2171 return llvm::createStringError(
2172 "DW_OP_form_tls_address needs an argument");
2173 else
2174 return llvm::createStringError(
2175 "DW_OP_GNU_push_tls_address needs an argument");
2176 }
2177
2178 if (!exe_ctx || !module_sp)
2179 return llvm::createStringError("no context to evaluate TLS within");
2180
2181 Thread *thread = exe_ctx->GetThreadPtr();
2182 if (!thread)
2183 return llvm::createStringError("no thread to evaluate TLS within");
2184
2185 // Lookup the TLS block address for this thread and module.
2186 const addr_t tls_file_addr =
2187 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2188 const addr_t tls_load_addr =
2189 thread->GetThreadLocalData(module_sp, tls_file_addr);
2190
2191 if (tls_load_addr == LLDB_INVALID_ADDRESS)
2192 return llvm::createStringError(
2193 "no TLS data currently exists for this thread");
2194
2195 stack.back().GetScalar() = tls_load_addr;
2196 stack.back().SetValueType(Value::ValueType::LoadAddress);
2197 } break;
2198
2199 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
2200 // OPERANDS: 1
2201 // ULEB128: index to the .debug_addr section
2202 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2203 // section with the base address specified by the DW_AT_addr_base attribute
2204 // and the 0 based index is the ULEB128 encoded index.
2205 case DW_OP_addrx:
2206 case DW_OP_GNU_addr_index: {
2207 if (!dwarf_cu)
2208 return llvm::createStringError("DW_OP_GNU_addr_index found without a "
2209 "compile unit being specified");
2210 uint64_t index = op->getRawOperand(0);
2211 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2212 stack.push_back(Scalar(value));
2213 if (target &&
2215 // wasm file sections aren't mapped into memory, therefore addresses can
2216 // never point into a file section and are always LoadAddresses.
2217 stack.back().SetValueType(Value::ValueType::LoadAddress);
2218 } else {
2219 stack.back().SetValueType(Value::ValueType::FileAddress);
2220 }
2221 } break;
2222
2223 // OPCODE: DW_OP_GNU_const_index
2224 // OPERANDS: 1
2225 // ULEB128: index to the .debug_addr section
2226 // DESCRIPTION: Pushes an constant with the size of a machine address to
2227 // the stack from the .debug_addr section with the base address specified
2228 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2229 // encoded index.
2230 case DW_OP_GNU_const_index: {
2231 if (!dwarf_cu) {
2232 return llvm::createStringError("DW_OP_GNU_const_index found without a "
2233 "compile unit being specified");
2234 }
2235 uint64_t index = op->getRawOperand(0);
2236 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2237 stack.push_back(Scalar(value));
2238 } break;
2239
2240 case DW_OP_GNU_entry_value:
2241 case DW_OP_entry_value: {
2242 // Technically, DW_OP_entry_value has two operands, but LLVM represents
2243 // it as a single-operand operation (bug?). We can deal with this: the
2244 // second operand immediately follows the first, but have to be careful
2245 // when advancing the iterator, see the comment below.
2246 const uint64_t block_size = op->getRawOperand(0);
2247 uint64_t block_offset = op->getEndOffset();
2248
2249 llvm::Error error = llvm::Error::success();
2250 llvm::ArrayRef<uint8_t> block_data = llvm::arrayRefFromStringRef(
2251 expr_data.getBytes(&block_offset, block_size, &error));
2252
2253 if (error)
2254 return error;
2255
2256 if (llvm::Error err = Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx,
2257 block_data, log))
2258 return llvm::createStringError(
2259 "could not evaluate DW_OP_entry_value: %s",
2260 llvm::toString(std::move(err)).c_str());
2261
2262 // We can't use `operator++` here because the iterator currently points
2263 // to the second operand. See the comment above.
2264 op = op.skipBytes(block_size);
2265 continue;
2266 }
2267
2268 default:
2269 if (dwarf_cu) {
2270 const uint64_t operands_offset = op_offset + 1;
2271 uint64_t offset = operands_offset; // Updated by the callee.
2272 if (dwarf_cu->ParseVendorDWARFOpcode(opcode, expr_data, offset, reg_ctx,
2273 reg_kind, stack)) {
2274 // This is a little tricky. If LLVM knows about this vendor-specific
2275 // operation, `getEndOffset()` points past its last operand. If LLVM
2276 // knows nothing about this operation, `getEndOffset()` points to its
2277 // opcode. In both cases `offset` will point to the next operation,
2278 // but we can't use it directly because the only available mutating
2279 // method of `iterator` (not counting `operator++`) is `skipBytes()`.
2280 // So we calculate the offset and pass it to `skipBytes()`.
2281 assert(offset >= op->getEndOffset());
2282 uint64_t offset_to_next_op = offset - op->getEndOffset();
2283 op = op.skipBytes(offset_to_next_op);
2284 continue;
2285 }
2286 }
2287 return llvm::createStringErrorV("unhandled opcode {0} in DWARFExpression",
2288 opcode);
2289 }
2290 ++op;
2291 }
2292
2293 if (stack.empty()) {
2294 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2295 // or DW_OP_bit_piece opcodes
2296 if (pieces.GetBuffer().GetByteSize())
2297 return pieces;
2298
2299 return llvm::createStringError("stack empty after evaluation");
2300 }
2301
2302 UpdateValueTypeFromLocationDescription(
2303 log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
2304
2305 if (log && log->GetVerbose()) {
2306 size_t count = stack.size();
2307 LLDB_LOGF(log, "Stack after operation has %" PRIu64 " values:",
2308 static_cast<uint64_t>(count));
2309 for (size_t i = 0; i < count; ++i) {
2310 StreamString new_value;
2311 new_value.Printf("[%" PRIu64 "]", static_cast<uint64_t>(i));
2312 stack[i].Dump(&new_value);
2313 LLDB_LOGF(log, " %s", new_value.GetData());
2314 }
2315 }
2316 return stack.back();
2317}
2318
2320 StackFrame &frame, const Instruction::Operand &operand) const {
2321 using namespace OperandMatchers;
2322
2323 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
2324 if (!reg_ctx_sp) {
2325 return false;
2326 }
2327
2328 DataExtractor opcodes(m_data);
2329
2330 lldb::offset_t op_offset = 0;
2331 uint8_t opcode = opcodes.GetU8(&op_offset);
2332
2333 if (opcode == DW_OP_fbreg) {
2334 int64_t offset = opcodes.GetSLEB128(&op_offset);
2335
2336 DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr);
2337 if (!fb_expr) {
2338 return false;
2339 }
2340
2341 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
2342 return fb_expr->MatchesOperand(frame, child);
2343 };
2344
2345 if (!offset &&
2346 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2347 recurse)(operand)) {
2348 return true;
2349 }
2350
2351 return MatchUnaryOp(
2353 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2354 MatchImmOp(offset), recurse))(operand);
2355 }
2356
2357 bool dereference = false;
2358 const RegisterInfo *reg = nullptr;
2359 int64_t offset = 0;
2360
2361 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
2362 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
2363 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
2364 offset = opcodes.GetSLEB128(&op_offset);
2365 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
2366 } else if (opcode == DW_OP_regx) {
2367 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2368 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2369 } else if (opcode == DW_OP_bregx) {
2370 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2371 offset = opcodes.GetSLEB128(&op_offset);
2372 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2373 } else {
2374 return false;
2375 }
2376
2377 if (!reg) {
2378 return false;
2379 }
2380
2381 if (dereference) {
2382 if (!offset &&
2383 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2384 MatchRegOp(*reg))(operand)) {
2385 return true;
2386 }
2387
2388 return MatchUnaryOp(
2390 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2391 MatchRegOp(*reg), MatchImmOp(offset)))(operand);
2392 } else {
2393 return MatchRegOp(*reg)(operand);
2394 }
2395}
static llvm::raw_ostream & error(Stream &strm)
static llvm::Expected< lldb::addr_t > ResolveLoadAddress(ExecutionContext *exe_ctx, lldb::ModuleSP &module_sp, const char *dw_op_type, lldb::addr_t file_addr, Address &so_addr, bool check_sectionoffset=false)
Helper function to move common code used to resolve a file address and turn into a load address.
static const char * DW_OP_value_to_name(uint32_t val)
static llvm::Error Evaluate_DW_OP_deref_size(DWARFExpression::Stack &stack, ExecutionContext *exe_ctx, lldb::ModuleSP module_sp, Process *process, Target *target, uint8_t size, size_t size_addr_bytes, LocationDescriptionKind &dwarf4_location_description_kind)
static lldb::offset_t GetOpcodeDataSize(const DataExtractor &data, const lldb::offset_t data_offset, const LocationAtom op, const DWARFExpression::Delegate *dwarf_cu)
Return the length in bytes of the set of operands for op.
static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes, size_t size_addr_bytes, ByteOrder byte_order, size_t size)
Helper function to load sized data from a uint8_t buffer.
static llvm::Error Evaluate_DW_OP_entry_value(DWARFExpression::Stack &stack, ExecutionContext *exe_ctx, RegisterContext *reg_ctx, llvm::ArrayRef< uint8_t > subexpr, Log *log)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:364
#define LLDB_LOGF(log,...)
Definition Log.h:378
@ Empty
If the Mangled object has neither a mangled name or demangled name we can encode the object with one ...
Definition Mangled.cpp:455
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:448
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 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 bool ParseVendorDWARFOpcode(uint8_t op, const llvm::DataExtractor &opcodes, lldb::offset_t &offset, RegisterContext *reg_ctx, lldb::RegisterKind reg_kind, Stack &stack) const =0
virtual llvm::Expected< std::pair< uint64_t, bool > > GetDIEBitSizeAndSign(uint64_t relative_die_offset) const =0
virtual uint8_t GetAddressByteSize() const =0
DataExtractor m_data
A data extractor capable of reading opcode bytes.
llvm::Expected< lldb::addr_t > GetLocation_DW_OP_addr(const Delegate *dwarf_cu) const
Return the address specified by the first DW_OP_{addr, addrx, GNU_addr_index} in the operation stream...
void UpdateValue(uint64_t const_value, lldb::offset_t const_value_byte_size, uint8_t addr_byte_size)
static llvm::Expected< Value > Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx, lldb::ModuleSP module_sp, const DataExtractor &opcodes, const Delegate *dwarf_cu, const lldb::RegisterKind reg_set, const Value *initial_value_ptr, const Value *object_address_ptr)
Evaluate a DWARF location expression in a particular context.
bool ContainsThreadLocalStorage(const Delegate *dwarf_cu) const
bool LinkThreadLocalStorage(const Delegate *dwarf_cu, std::function< lldb::addr_t(lldb::addr_t file_addr)> const &link_address_callback)
lldb::RegisterKind m_reg_kind
One of the defines that starts with LLDB_REGKIND_.
bool Update_DW_OP_addr(const Delegate *dwarf_cu, lldb::addr_t file_addr)
void SetRegisterKind(lldb::RegisterKind reg_kind)
Set the call-frame-info style register kind.
static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx, lldb::RegisterKind reg_kind, uint32_t reg_num, Value &value)
bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op) const
void DumpLocation(Stream *s, lldb::DescriptionLevel level, ABI *abi, llvm::DIDumpOptions options={}) const
lldb::RegisterKind GetRegisterKind() const
Return the call-frame-info style register kind.
bool IsValid() const
Return true if the location expression contains data.
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
An binary data encoding class.
Definition DataEncoder.h:42
std::shared_ptr< lldb_private::DataBufferHeap > GetDataBuffer()
Get a shared copy of the heap based memory buffer owned by this object.
uint32_t PutUnsigned(uint32_t offset, uint32_t byte_size, uint64_t value)
Encode an unsigned integer of size byte_size to offset.
uint32_t PutAddress(uint32_t offset, lldb::addr_t addr)
Encode an address in the existing buffer at offset bytes into the buffer.
void AppendAddress(lldb::addr_t addr)
Append an address sized integer to the end of the owned data.
void AppendU8(uint8_t value)
Append a unsigned integer to the end of the owned data.
void AppendData(llvm::StringRef data)
Append a bytes to the end of the owned data.
An data extractor class.
uint64_t GetULEB128(lldb::offset_t *offset_ptr) const
Extract a unsigned LEB128 value from *offset_ptr.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
virtual lldb::offset_t BytesLeft(lldb::offset_t offset) const
llvm::DataExtractor GetAsLLVM() const
uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const
Skip an LEB128 number at *offset_ptr.
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
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.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void SetFrameSP(const lldb::StackFrameSP &frame_sp)
Set accessor to set only the frame shared pointer.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Target * GetTargetPtr() const
Returns a pointer to the target object.
bool HasTargetScope() const
Returns true the ExecutionContext object contains a valid target.
Target & GetTargetRef() const
Returns a reference to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
A class that describes a function.
Definition Function.h:400
ConstString GetName() const
Definition Function.cpp:709
CallEdge * GetCallEdgeForReturnAddress(lldb::addr_t return_pc, Target &target)
Get the outgoing call edge from this function which has the given return address return_pc,...
Definition Function.cpp:368
llvm::ArrayRef< std::unique_ptr< CallEdge > > GetTailCallingEdges()
Get the outgoing tail-calling edges from this function.
Definition Function.cpp:361
bool GetVerbose() const
Definition Log.cpp:300
A collection class for Module objects.
Definition ModuleList.h:125
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
virtual lldb::ByteOrder GetByteOrder() const =0
Gets whether endian swapping should occur when extracting data from this object file.
A plug-in interface definition class for debugging a process.
Definition Process.h:355
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:1911
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3770
virtual uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num)
Convert from a given register numbering scheme to the lldb register numbering scheme.
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
bool GetScalarValue(Scalar &scalar) const
size_t GetByteSize() const
Definition Scalar.cpp:162
bool IsZero() const
Definition Scalar.cpp:174
void TruncOrExtendTo(uint16_t bits, bool sign)
Convert to an integer with bits and the given signedness.
Definition Scalar.cpp:204
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:365
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
Definition Scalar.cpp:813
This base class provides an interface to stack frames.
Definition StackFrame.h:44
virtual DWARFExpressionList * GetFrameBaseExpression(Status *error_ptr)
Get the DWARFExpressionList corresponding to the Canonical Frame Address.
virtual llvm::Error GetFrameBaseValue(Scalar &value)
Return the Canonical Frame Address (DWARF term) for this frame.
virtual lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
virtual StackID & GetStackID()
virtual const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
virtual uint32_t GetFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList.
An error handling class.
Definition Status.h:118
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:402
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:132
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:1990
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1157
const ArchSpec & GetArchitecture() const
Definition Target.h:1199
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
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:327
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.