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