Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / Expression / DWARFExpression.cpp
blobfe4928d4f43a4346a5e833973acdbc7a7b1c1cf5
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;
98 static bool ReadRegisterValueAsScalar(RegisterContext *reg_ctx,
99 lldb::RegisterKind reg_kind,
100 uint32_t reg_num, Status *error_ptr,
101 Value &value) {
102 if (reg_ctx == nullptr) {
103 if (error_ptr)
104 error_ptr->SetErrorString("No register context in frame.\n");
105 } else {
106 uint32_t native_reg =
107 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
108 if (native_reg == LLDB_INVALID_REGNUM) {
109 if (error_ptr)
110 error_ptr->SetErrorStringWithFormat("Unable to convert register "
111 "kind=%u reg_num=%u to a native "
112 "register number.\n",
113 reg_kind, reg_num);
114 } else {
115 const RegisterInfo *reg_info =
116 reg_ctx->GetRegisterInfoAtIndex(native_reg);
117 RegisterValue reg_value;
118 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
119 if (reg_value.GetScalarValue(value.GetScalar())) {
120 value.SetValueType(Value::ValueType::Scalar);
121 value.SetContext(Value::ContextType::RegisterInfo,
122 const_cast<RegisterInfo *>(reg_info));
123 if (error_ptr)
124 error_ptr->Clear();
125 return true;
126 } else {
127 // If we get this error, then we need to implement a value buffer in
128 // the dwarf expression evaluation function...
129 if (error_ptr)
130 error_ptr->SetErrorStringWithFormat(
131 "register %s can't be converted to a scalar value",
132 reg_info->name);
134 } else {
135 if (error_ptr)
136 error_ptr->SetErrorStringWithFormat("register %s is not available",
137 reg_info->name);
141 return false;
144 /// Return the length in bytes of the set of operands for \p op. No guarantees
145 /// are made on the state of \p data after this call.
146 static offset_t GetOpcodeDataSize(const DataExtractor &data,
147 const lldb::offset_t data_offset,
148 const uint8_t op, const DWARFUnit *dwarf_cu) {
149 lldb::offset_t offset = data_offset;
150 switch (op) {
151 case DW_OP_addr:
152 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
153 return data.GetAddressByteSize();
155 // Opcodes with no arguments
156 case DW_OP_deref: // 0x06
157 case DW_OP_dup: // 0x12
158 case DW_OP_drop: // 0x13
159 case DW_OP_over: // 0x14
160 case DW_OP_swap: // 0x16
161 case DW_OP_rot: // 0x17
162 case DW_OP_xderef: // 0x18
163 case DW_OP_abs: // 0x19
164 case DW_OP_and: // 0x1a
165 case DW_OP_div: // 0x1b
166 case DW_OP_minus: // 0x1c
167 case DW_OP_mod: // 0x1d
168 case DW_OP_mul: // 0x1e
169 case DW_OP_neg: // 0x1f
170 case DW_OP_not: // 0x20
171 case DW_OP_or: // 0x21
172 case DW_OP_plus: // 0x22
173 case DW_OP_shl: // 0x24
174 case DW_OP_shr: // 0x25
175 case DW_OP_shra: // 0x26
176 case DW_OP_xor: // 0x27
177 case DW_OP_eq: // 0x29
178 case DW_OP_ge: // 0x2a
179 case DW_OP_gt: // 0x2b
180 case DW_OP_le: // 0x2c
181 case DW_OP_lt: // 0x2d
182 case DW_OP_ne: // 0x2e
183 case DW_OP_lit0: // 0x30
184 case DW_OP_lit1: // 0x31
185 case DW_OP_lit2: // 0x32
186 case DW_OP_lit3: // 0x33
187 case DW_OP_lit4: // 0x34
188 case DW_OP_lit5: // 0x35
189 case DW_OP_lit6: // 0x36
190 case DW_OP_lit7: // 0x37
191 case DW_OP_lit8: // 0x38
192 case DW_OP_lit9: // 0x39
193 case DW_OP_lit10: // 0x3A
194 case DW_OP_lit11: // 0x3B
195 case DW_OP_lit12: // 0x3C
196 case DW_OP_lit13: // 0x3D
197 case DW_OP_lit14: // 0x3E
198 case DW_OP_lit15: // 0x3F
199 case DW_OP_lit16: // 0x40
200 case DW_OP_lit17: // 0x41
201 case DW_OP_lit18: // 0x42
202 case DW_OP_lit19: // 0x43
203 case DW_OP_lit20: // 0x44
204 case DW_OP_lit21: // 0x45
205 case DW_OP_lit22: // 0x46
206 case DW_OP_lit23: // 0x47
207 case DW_OP_lit24: // 0x48
208 case DW_OP_lit25: // 0x49
209 case DW_OP_lit26: // 0x4A
210 case DW_OP_lit27: // 0x4B
211 case DW_OP_lit28: // 0x4C
212 case DW_OP_lit29: // 0x4D
213 case DW_OP_lit30: // 0x4E
214 case DW_OP_lit31: // 0x4f
215 case DW_OP_reg0: // 0x50
216 case DW_OP_reg1: // 0x51
217 case DW_OP_reg2: // 0x52
218 case DW_OP_reg3: // 0x53
219 case DW_OP_reg4: // 0x54
220 case DW_OP_reg5: // 0x55
221 case DW_OP_reg6: // 0x56
222 case DW_OP_reg7: // 0x57
223 case DW_OP_reg8: // 0x58
224 case DW_OP_reg9: // 0x59
225 case DW_OP_reg10: // 0x5A
226 case DW_OP_reg11: // 0x5B
227 case DW_OP_reg12: // 0x5C
228 case DW_OP_reg13: // 0x5D
229 case DW_OP_reg14: // 0x5E
230 case DW_OP_reg15: // 0x5F
231 case DW_OP_reg16: // 0x60
232 case DW_OP_reg17: // 0x61
233 case DW_OP_reg18: // 0x62
234 case DW_OP_reg19: // 0x63
235 case DW_OP_reg20: // 0x64
236 case DW_OP_reg21: // 0x65
237 case DW_OP_reg22: // 0x66
238 case DW_OP_reg23: // 0x67
239 case DW_OP_reg24: // 0x68
240 case DW_OP_reg25: // 0x69
241 case DW_OP_reg26: // 0x6A
242 case DW_OP_reg27: // 0x6B
243 case DW_OP_reg28: // 0x6C
244 case DW_OP_reg29: // 0x6D
245 case DW_OP_reg30: // 0x6E
246 case DW_OP_reg31: // 0x6F
247 case DW_OP_nop: // 0x96
248 case DW_OP_push_object_address: // 0x97 DWARF3
249 case DW_OP_form_tls_address: // 0x9b DWARF3
250 case DW_OP_call_frame_cfa: // 0x9c DWARF3
251 case DW_OP_stack_value: // 0x9f DWARF4
252 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
253 return 0;
255 // Opcodes with a single 1 byte arguments
256 case DW_OP_const1u: // 0x08 1 1-byte constant
257 case DW_OP_const1s: // 0x09 1 1-byte constant
258 case DW_OP_pick: // 0x15 1 1-byte stack index
259 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
260 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
261 return 1;
263 // Opcodes with a single 2 byte arguments
264 case DW_OP_const2u: // 0x0a 1 2-byte constant
265 case DW_OP_const2s: // 0x0b 1 2-byte constant
266 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
267 case DW_OP_bra: // 0x28 1 signed 2-byte constant
268 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
269 return 2;
271 // Opcodes with a single 4 byte arguments
272 case DW_OP_const4u: // 0x0c 1 4-byte constant
273 case DW_OP_const4s: // 0x0d 1 4-byte constant
274 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
275 return 4;
277 // Opcodes with a single 8 byte arguments
278 case DW_OP_const8u: // 0x0e 1 8-byte constant
279 case DW_OP_const8s: // 0x0f 1 8-byte constant
280 return 8;
282 // All opcodes that have a single ULEB (signed or unsigned) argument
283 case DW_OP_addrx: // 0xa1 1 ULEB128 index
284 case DW_OP_constu: // 0x10 1 ULEB128 constant
285 case DW_OP_consts: // 0x11 1 SLEB128 constant
286 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
287 case DW_OP_breg0: // 0x70 1 ULEB128 register
288 case DW_OP_breg1: // 0x71 1 ULEB128 register
289 case DW_OP_breg2: // 0x72 1 ULEB128 register
290 case DW_OP_breg3: // 0x73 1 ULEB128 register
291 case DW_OP_breg4: // 0x74 1 ULEB128 register
292 case DW_OP_breg5: // 0x75 1 ULEB128 register
293 case DW_OP_breg6: // 0x76 1 ULEB128 register
294 case DW_OP_breg7: // 0x77 1 ULEB128 register
295 case DW_OP_breg8: // 0x78 1 ULEB128 register
296 case DW_OP_breg9: // 0x79 1 ULEB128 register
297 case DW_OP_breg10: // 0x7a 1 ULEB128 register
298 case DW_OP_breg11: // 0x7b 1 ULEB128 register
299 case DW_OP_breg12: // 0x7c 1 ULEB128 register
300 case DW_OP_breg13: // 0x7d 1 ULEB128 register
301 case DW_OP_breg14: // 0x7e 1 ULEB128 register
302 case DW_OP_breg15: // 0x7f 1 ULEB128 register
303 case DW_OP_breg16: // 0x80 1 ULEB128 register
304 case DW_OP_breg17: // 0x81 1 ULEB128 register
305 case DW_OP_breg18: // 0x82 1 ULEB128 register
306 case DW_OP_breg19: // 0x83 1 ULEB128 register
307 case DW_OP_breg20: // 0x84 1 ULEB128 register
308 case DW_OP_breg21: // 0x85 1 ULEB128 register
309 case DW_OP_breg22: // 0x86 1 ULEB128 register
310 case DW_OP_breg23: // 0x87 1 ULEB128 register
311 case DW_OP_breg24: // 0x88 1 ULEB128 register
312 case DW_OP_breg25: // 0x89 1 ULEB128 register
313 case DW_OP_breg26: // 0x8a 1 ULEB128 register
314 case DW_OP_breg27: // 0x8b 1 ULEB128 register
315 case DW_OP_breg28: // 0x8c 1 ULEB128 register
316 case DW_OP_breg29: // 0x8d 1 ULEB128 register
317 case DW_OP_breg30: // 0x8e 1 ULEB128 register
318 case DW_OP_breg31: // 0x8f 1 ULEB128 register
319 case DW_OP_regx: // 0x90 1 ULEB128 register
320 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
321 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
322 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
323 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
324 data.Skip_LEB128(&offset);
325 return offset - data_offset;
327 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
328 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
329 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
330 data.Skip_LEB128(&offset);
331 data.Skip_LEB128(&offset);
332 return offset - data_offset;
334 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
335 // (DWARF4)
337 uint64_t block_len = data.Skip_LEB128(&offset);
338 offset += block_len;
339 return offset - data_offset;
342 case DW_OP_GNU_entry_value:
343 case DW_OP_entry_value: // 0xa3 ULEB128 size + variable-length block
345 uint64_t subexpr_len = data.GetULEB128(&offset);
346 return (offset - data_offset) + subexpr_len;
349 default:
350 if (!dwarf_cu) {
351 return LLDB_INVALID_OFFSET;
353 return dwarf_cu->GetSymbolFileDWARF().GetVendorDWARFOpcodeSize(
354 data, data_offset, op);
358 lldb::addr_t DWARFExpression::GetLocation_DW_OP_addr(const DWARFUnit *dwarf_cu,
359 bool &error) const {
360 error = false;
361 lldb::offset_t offset = 0;
362 while (m_data.ValidOffset(offset)) {
363 const uint8_t op = m_data.GetU8(&offset);
365 if (op == DW_OP_addr)
366 return m_data.GetAddress(&offset);
367 if (op == DW_OP_GNU_addr_index || op == DW_OP_addrx) {
368 uint64_t index = m_data.GetULEB128(&offset);
369 if (dwarf_cu)
370 return dwarf_cu->ReadAddressFromDebugAddrSection(index);
371 error = true;
372 break;
374 const offset_t op_arg_size =
375 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
376 if (op_arg_size == LLDB_INVALID_OFFSET) {
377 error = true;
378 break;
380 offset += op_arg_size;
382 return LLDB_INVALID_ADDRESS;
385 bool DWARFExpression::Update_DW_OP_addr(const DWARFUnit *dwarf_cu,
386 lldb::addr_t file_addr) {
387 lldb::offset_t offset = 0;
388 while (m_data.ValidOffset(offset)) {
389 const uint8_t op = m_data.GetU8(&offset);
391 if (op == DW_OP_addr) {
392 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
393 // We have to make a copy of the data as we don't know if this data is
394 // from a read only memory mapped buffer, so we duplicate all of the data
395 // first, then modify it, and if all goes well, we then replace the data
396 // for this expression
398 // Make en encoder that contains a copy of the location expression data
399 // so we can write the address into the buffer using the correct byte
400 // order.
401 DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
402 m_data.GetByteOrder(), addr_byte_size);
404 // Replace the address in the new buffer
405 if (encoder.PutAddress(offset, file_addr) == UINT32_MAX)
406 return false;
408 // All went well, so now we can reset the data using a shared pointer to
409 // the heap data so "m_data" will now correctly manage the heap data.
410 m_data.SetData(encoder.GetDataBuffer());
411 return true;
413 if (op == DW_OP_addrx) {
414 // Replace DW_OP_addrx with DW_OP_addr, since we can't modify the
415 // read-only debug_addr table.
416 // Subtract one to account for the opcode.
417 llvm::ArrayRef data_before_op = m_data.GetData().take_front(offset - 1);
419 // Read the addrx index to determine how many bytes it needs.
420 const lldb::offset_t old_offset = offset;
421 m_data.GetULEB128(&offset);
422 if (old_offset == offset)
423 return false;
424 llvm::ArrayRef data_after_op = m_data.GetData().drop_front(offset);
426 DataEncoder encoder(m_data.GetByteOrder(), m_data.GetAddressByteSize());
427 encoder.AppendData(data_before_op);
428 encoder.AppendU8(DW_OP_addr);
429 encoder.AppendAddress(file_addr);
430 encoder.AppendData(data_after_op);
431 m_data.SetData(encoder.GetDataBuffer());
432 return true;
434 const offset_t op_arg_size =
435 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
436 if (op_arg_size == LLDB_INVALID_OFFSET)
437 break;
438 offset += op_arg_size;
440 return false;
443 bool DWARFExpression::ContainsThreadLocalStorage(
444 const DWARFUnit *dwarf_cu) const {
445 lldb::offset_t offset = 0;
446 while (m_data.ValidOffset(offset)) {
447 const uint8_t op = m_data.GetU8(&offset);
449 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
450 return true;
451 const offset_t op_arg_size =
452 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
453 if (op_arg_size == LLDB_INVALID_OFFSET)
454 return false;
455 offset += op_arg_size;
457 return false;
459 bool DWARFExpression::LinkThreadLocalStorage(
460 const DWARFUnit *dwarf_cu,
461 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
462 &link_address_callback) {
463 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
464 // We have to make a copy of the data as we don't know if this data is from a
465 // read only memory mapped buffer, so we duplicate all of the data first,
466 // then modify it, and if all goes well, we then replace the data for this
467 // expression.
468 // Make en encoder that contains a copy of the location expression data so we
469 // can write the address into the buffer using the correct byte order.
470 DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
471 m_data.GetByteOrder(), addr_byte_size);
473 lldb::offset_t offset = 0;
474 lldb::offset_t const_offset = 0;
475 lldb::addr_t const_value = 0;
476 size_t const_byte_size = 0;
477 while (m_data.ValidOffset(offset)) {
478 const uint8_t op = m_data.GetU8(&offset);
480 bool decoded_data = false;
481 switch (op) {
482 case DW_OP_const4u:
483 // Remember the const offset in case we later have a
484 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
485 const_offset = offset;
486 const_value = m_data.GetU32(&offset);
487 decoded_data = true;
488 const_byte_size = 4;
489 break;
491 case DW_OP_const8u:
492 // Remember the const offset in case we later have a
493 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
494 const_offset = offset;
495 const_value = m_data.GetU64(&offset);
496 decoded_data = true;
497 const_byte_size = 8;
498 break;
500 case DW_OP_form_tls_address:
501 case DW_OP_GNU_push_tls_address:
502 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
503 // by a file address on the stack. We assume that DW_OP_const4u or
504 // DW_OP_const8u is used for these values, and we check that the last
505 // opcode we got before either of these was DW_OP_const4u or
506 // DW_OP_const8u. If so, then we can link the value accordingly. For
507 // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
508 // address of a structure that contains a function pointer, the pthread
509 // key and the offset into the data pointed to by the pthread key. So we
510 // must link this address and also set the module of this expression to
511 // the new_module_sp so we can resolve the file address correctly
512 if (const_byte_size > 0) {
513 lldb::addr_t linked_file_addr = link_address_callback(const_value);
514 if (linked_file_addr == LLDB_INVALID_ADDRESS)
515 return false;
516 // Replace the address in the new buffer
517 if (encoder.PutUnsigned(const_offset, const_byte_size,
518 linked_file_addr) == UINT32_MAX)
519 return false;
521 break;
523 default:
524 const_offset = 0;
525 const_value = 0;
526 const_byte_size = 0;
527 break;
530 if (!decoded_data) {
531 const offset_t op_arg_size =
532 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
533 if (op_arg_size == LLDB_INVALID_OFFSET)
534 return false;
535 else
536 offset += op_arg_size;
540 m_data.SetData(encoder.GetDataBuffer());
541 return true;
544 static bool Evaluate_DW_OP_entry_value(std::vector<Value> &stack,
545 ExecutionContext *exe_ctx,
546 RegisterContext *reg_ctx,
547 const DataExtractor &opcodes,
548 lldb::offset_t &opcode_offset,
549 Status *error_ptr, Log *log) {
550 // DW_OP_entry_value(sub-expr) describes the location a variable had upon
551 // function entry: this variable location is presumed to be optimized out at
552 // the current PC value. The caller of the function may have call site
553 // information that describes an alternate location for the variable (e.g. a
554 // constant literal, or a spilled stack value) in the parent frame.
556 // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
558 // void child(int &sink, int x) {
559 // ...
560 // /* "x" gets optimized out. */
562 // /* The location of "x" here is: DW_OP_entry_value($reg2). */
563 // ++sink;
564 // }
566 // void parent() {
567 // int sink;
569 // /*
570 // * The callsite information emitted here is:
571 // *
572 // * DW_TAG_call_site
573 // * DW_AT_return_pc ... (for "child(sink, 123);")
574 // * DW_TAG_call_site_parameter (for "sink")
575 // * DW_AT_location ($reg1)
576 // * DW_AT_call_value ($SP - 8)
577 // * DW_TAG_call_site_parameter (for "x")
578 // * DW_AT_location ($reg2)
579 // * DW_AT_call_value ($literal 123)
580 // *
581 // * DW_TAG_call_site
582 // * DW_AT_return_pc ... (for "child(sink, 456);")
583 // * ...
584 // */
585 // child(sink, 123);
586 // child(sink, 456);
587 // }
589 // When the program stops at "++sink" within `child`, the debugger determines
590 // the call site by analyzing the return address. Once the call site is found,
591 // the debugger determines which parameter is referenced by DW_OP_entry_value
592 // and evaluates the corresponding location for that parameter in `parent`.
594 // 1. Find the function which pushed the current frame onto the stack.
595 if ((!exe_ctx || !exe_ctx->HasTargetScope()) || !reg_ctx) {
596 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no exe/reg context");
597 return false;
600 StackFrame *current_frame = exe_ctx->GetFramePtr();
601 Thread *thread = exe_ctx->GetThreadPtr();
602 if (!current_frame || !thread) {
603 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current frame/thread");
604 return false;
607 Target &target = exe_ctx->GetTargetRef();
608 StackFrameSP parent_frame = nullptr;
609 addr_t return_pc = LLDB_INVALID_ADDRESS;
610 uint32_t current_frame_idx = current_frame->GetFrameIndex();
611 uint32_t num_frames = thread->GetStackFrameCount();
612 for (uint32_t parent_frame_idx = current_frame_idx + 1;
613 parent_frame_idx < num_frames; ++parent_frame_idx) {
614 parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx);
615 // Require a valid sequence of frames.
616 if (!parent_frame)
617 break;
619 // Record the first valid return address, even if this is an inlined frame,
620 // in order to look up the associated call edge in the first non-inlined
621 // parent frame.
622 if (return_pc == LLDB_INVALID_ADDRESS) {
623 return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
624 LLDB_LOG(log,
625 "Evaluate_DW_OP_entry_value: immediate ancestor with pc = {0:x}",
626 return_pc);
629 // If we've found an inlined frame, skip it (these have no call site
630 // parameters).
631 if (parent_frame->IsInlined())
632 continue;
634 // We've found the first non-inlined parent frame.
635 break;
637 if (!parent_frame || !parent_frame->GetRegisterContext()) {
638 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent frame with reg ctx");
639 return false;
642 Function *parent_func =
643 parent_frame->GetSymbolContext(eSymbolContextFunction).function;
644 if (!parent_func) {
645 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no parent function");
646 return false;
649 // 2. Find the call edge in the parent function responsible for creating the
650 // current activation.
651 Function *current_func =
652 current_frame->GetSymbolContext(eSymbolContextFunction).function;
653 if (!current_func) {
654 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no current function");
655 return false;
658 CallEdge *call_edge = nullptr;
659 ModuleList &modlist = target.GetImages();
660 ExecutionContext parent_exe_ctx = *exe_ctx;
661 parent_exe_ctx.SetFrameSP(parent_frame);
662 if (!parent_frame->IsArtificial()) {
663 // If the parent frame is not artificial, the current activation may be
664 // produced by an ambiguous tail call. In this case, refuse to proceed.
665 call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target);
666 if (!call_edge) {
667 LLDB_LOG(log,
668 "Evaluate_DW_OP_entry_value: no call edge for retn-pc = {0:x} "
669 "in parent frame {1}",
670 return_pc, parent_func->GetName());
671 return false;
673 Function *callee_func = call_edge->GetCallee(modlist, parent_exe_ctx);
674 if (callee_func != current_func) {
675 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: ambiguous call sequence, "
676 "can't find real parent frame");
677 return false;
679 } else {
680 // The StackFrameList solver machinery has deduced that an unambiguous tail
681 // call sequence that produced the current activation. The first edge in
682 // the parent that points to the current function must be valid.
683 for (auto &edge : parent_func->GetTailCallingEdges()) {
684 if (edge->GetCallee(modlist, parent_exe_ctx) == current_func) {
685 call_edge = edge.get();
686 break;
690 if (!call_edge) {
691 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: no unambiguous edge from parent "
692 "to current function");
693 return false;
696 // 3. Attempt to locate the DW_OP_entry_value expression in the set of
697 // available call site parameters. If found, evaluate the corresponding
698 // parameter in the context of the parent frame.
699 const uint32_t subexpr_len = opcodes.GetULEB128(&opcode_offset);
700 const void *subexpr_data = opcodes.GetData(&opcode_offset, subexpr_len);
701 if (!subexpr_data) {
702 LLDB_LOG(log, "Evaluate_DW_OP_entry_value: subexpr could not be read");
703 return false;
706 const CallSiteParameter *matched_param = nullptr;
707 for (const CallSiteParameter &param : call_edge->GetCallSiteParameters()) {
708 DataExtractor param_subexpr_extractor;
709 if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor))
710 continue;
711 lldb::offset_t param_subexpr_offset = 0;
712 const void *param_subexpr_data =
713 param_subexpr_extractor.GetData(&param_subexpr_offset, subexpr_len);
714 if (!param_subexpr_data ||
715 param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0)
716 continue;
718 // At this point, the DW_OP_entry_value sub-expression and the callee-side
719 // expression in the call site parameter are known to have the same length.
720 // Check whether they are equal.
722 // Note that an equality check is sufficient: the contents of the
723 // DW_OP_entry_value subexpression are only used to identify the right call
724 // site parameter in the parent, and do not require any special handling.
725 if (memcmp(subexpr_data, param_subexpr_data, subexpr_len) == 0) {
726 matched_param = &param;
727 break;
730 if (!matched_param) {
731 LLDB_LOG(log,
732 "Evaluate_DW_OP_entry_value: no matching call site param found");
733 return false;
736 // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
737 // subexpresion whenever llvm does.
738 Value result;
739 const DWARFExpressionList &param_expr = matched_param->LocationInCaller;
740 if (!param_expr.Evaluate(&parent_exe_ctx,
741 parent_frame->GetRegisterContext().get(),
742 LLDB_INVALID_ADDRESS,
743 /*initial_value_ptr=*/nullptr,
744 /*object_address_ptr=*/nullptr, result, error_ptr)) {
745 LLDB_LOG(log,
746 "Evaluate_DW_OP_entry_value: call site param evaluation failed");
747 return false;
750 stack.push_back(result);
751 return true;
754 namespace {
755 /// The location description kinds described by the DWARF v5
756 /// specification. Composite locations are handled out-of-band and
757 /// thus aren't part of the enum.
758 enum LocationDescriptionKind {
759 Empty,
760 Memory,
761 Register,
762 Implicit
763 /* Composite*/
765 /// Adjust value's ValueType according to the kind of location description.
766 void UpdateValueTypeFromLocationDescription(Log *log, const DWARFUnit *dwarf_cu,
767 LocationDescriptionKind kind,
768 Value *value = nullptr) {
769 // Note that this function is conflating DWARF expressions with
770 // DWARF location descriptions. Perhaps it would be better to define
771 // a wrapper for DWARFExpression::Eval() that deals with DWARF
772 // location descriptions (which consist of one or more DWARF
773 // expressions). But doing this would mean we'd also need factor the
774 // handling of DW_OP_(bit_)piece out of this function.
775 if (dwarf_cu && dwarf_cu->GetVersion() >= 4) {
776 const char *log_msg = "DWARF location description kind: %s";
777 switch (kind) {
778 case Empty:
779 LLDB_LOGF(log, log_msg, "Empty");
780 break;
781 case Memory:
782 LLDB_LOGF(log, log_msg, "Memory");
783 if (value->GetValueType() == Value::ValueType::Scalar)
784 value->SetValueType(Value::ValueType::LoadAddress);
785 break;
786 case Register:
787 LLDB_LOGF(log, log_msg, "Register");
788 value->SetValueType(Value::ValueType::Scalar);
789 break;
790 case Implicit:
791 LLDB_LOGF(log, log_msg, "Implicit");
792 if (value->GetValueType() == Value::ValueType::LoadAddress)
793 value->SetValueType(Value::ValueType::Scalar);
794 break;
798 } // namespace
800 /// Helper function to move common code used to resolve a file address and turn
801 /// into a load address.
803 /// \param exe_ctx Pointer to the execution context
804 /// \param module_sp shared_ptr contains the module if we have one
805 /// \param error_ptr pointer to Status object if we have one
806 /// \param dw_op_type C-style string used to vary the error output
807 /// \param file_addr the file address we are trying to resolve and turn into a
808 /// load address
809 /// \param so_addr out parameter, will be set to load address or section offset
810 /// \param check_sectionoffset bool which determines if having a section offset
811 /// but not a load address is considerd a success
812 /// \returns std::optional containing the load address if resolving and getting
813 /// the load address succeed or an empty Optinal otherwise. If
814 /// check_sectionoffset is true we consider LLDB_INVALID_ADDRESS a
815 /// success if so_addr.IsSectionOffset() is true.
816 static std::optional<lldb::addr_t>
817 ResolveLoadAddress(ExecutionContext *exe_ctx, lldb::ModuleSP &module_sp,
818 Status *error_ptr, const char *dw_op_type,
819 lldb::addr_t file_addr, Address &so_addr,
820 bool check_sectionoffset = false) {
821 if (!module_sp) {
822 if (error_ptr)
823 error_ptr->SetErrorStringWithFormat(
824 "need module to resolve file address for %s", dw_op_type);
825 return {};
828 if (!module_sp->ResolveFileAddress(file_addr, so_addr)) {
829 if (error_ptr)
830 error_ptr->SetErrorString("failed to resolve file address in module");
831 return {};
834 addr_t load_addr = so_addr.GetLoadAddress(exe_ctx->GetTargetPtr());
836 if (load_addr == LLDB_INVALID_ADDRESS &&
837 (check_sectionoffset && !so_addr.IsSectionOffset())) {
838 if (error_ptr)
839 error_ptr->SetErrorString("failed to resolve load address");
840 return {};
843 return load_addr;
846 /// Helper function to move common code used to load sized data from a uint8_t
847 /// buffer.
849 /// \param addr_bytes uint8_t buffer containg raw data
850 /// \param size_addr_bytes how large is the underlying raw data
851 /// \param byte_order what is the byter order of the underlyig data
852 /// \param size How much of the underlying data we want to use
853 /// \return The underlying data converted into a Scalar
854 static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
855 size_t size_addr_bytes,
856 ByteOrder byte_order, size_t size) {
857 DataExtractor addr_data(addr_bytes, size_addr_bytes, byte_order, size);
859 lldb::offset_t addr_data_offset = 0;
860 if (size <= 8)
861 return addr_data.GetMaxU64(&addr_data_offset, size);
862 else
863 return addr_data.GetAddress(&addr_data_offset);
866 bool DWARFExpression::Evaluate(
867 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
868 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
869 const DWARFUnit *dwarf_cu, const lldb::RegisterKind reg_kind,
870 const Value *initial_value_ptr, const Value *object_address_ptr,
871 Value &result, Status *error_ptr) {
873 if (opcodes.GetByteSize() == 0) {
874 if (error_ptr)
875 error_ptr->SetErrorString(
876 "no location, value may have been optimized out");
877 return false;
879 std::vector<Value> stack;
881 Process *process = nullptr;
882 StackFrame *frame = nullptr;
883 Target *target = nullptr;
885 if (exe_ctx) {
886 process = exe_ctx->GetProcessPtr();
887 frame = exe_ctx->GetFramePtr();
888 target = exe_ctx->GetTargetPtr();
890 if (reg_ctx == nullptr && frame)
891 reg_ctx = frame->GetRegisterContext().get();
893 if (initial_value_ptr)
894 stack.push_back(*initial_value_ptr);
896 lldb::offset_t offset = 0;
897 Value tmp;
898 uint32_t reg_num;
900 /// Insertion point for evaluating multi-piece expression.
901 uint64_t op_piece_offset = 0;
902 Value pieces; // Used for DW_OP_piece
904 Log *log = GetLog(LLDBLog::Expressions);
905 // A generic type is "an integral type that has the size of an address and an
906 // unspecified signedness". For now, just use the signedness of the operand.
907 // TODO: Implement a real typed stack, and store the genericness of the value
908 // there.
909 auto to_generic = [&](auto v) {
910 bool is_signed = std::is_signed<decltype(v)>::value;
911 return Scalar(llvm::APSInt(
912 llvm::APInt(8 * opcodes.GetAddressByteSize(), v, is_signed),
913 !is_signed));
916 // The default kind is a memory location. This is updated by any
917 // operation that changes this, such as DW_OP_stack_value, and reset
918 // by composition operations like DW_OP_piece.
919 LocationDescriptionKind dwarf4_location_description_kind = Memory;
921 while (opcodes.ValidOffset(offset)) {
922 const lldb::offset_t op_offset = offset;
923 const uint8_t op = opcodes.GetU8(&offset);
925 if (log && log->GetVerbose()) {
926 size_t count = stack.size();
927 LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
928 (uint64_t)count);
929 for (size_t i = 0; i < count; ++i) {
930 StreamString new_value;
931 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
932 stack[i].Dump(&new_value);
933 LLDB_LOGF(log, " %s", new_value.GetData());
935 LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
936 DW_OP_value_to_name(op));
939 switch (op) {
940 // The DW_OP_addr operation has a single operand that encodes a machine
941 // address and whose size is the size of an address on the target machine.
942 case DW_OP_addr:
943 stack.push_back(Scalar(opcodes.GetAddress(&offset)));
944 if (target &&
945 target->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32) {
946 // wasm file sections aren't mapped into memory, therefore addresses can
947 // never point into a file section and are always LoadAddresses.
948 stack.back().SetValueType(Value::ValueType::LoadAddress);
949 } else {
950 stack.back().SetValueType(Value::ValueType::FileAddress);
952 break;
954 // The DW_OP_addr_sect_offset4 is used for any location expressions in
955 // shared libraries that have a location like:
956 // DW_OP_addr(0x1000)
957 // If this address resides in a shared library, then this virtual address
958 // won't make sense when it is evaluated in the context of a running
959 // process where shared libraries have been slid. To account for this, this
960 // new address type where we can store the section pointer and a 4 byte
961 // offset.
962 // case DW_OP_addr_sect_offset4:
963 // {
964 // result_type = eResultTypeFileAddress;
965 // lldb::Section *sect = (lldb::Section
966 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
967 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
969 // Address so_addr (sect, sect_offset);
970 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
971 // if (load_addr != LLDB_INVALID_ADDRESS)
972 // {
973 // // We successfully resolve a file address to a load
974 // // address.
975 // stack.push_back(load_addr);
976 // break;
977 // }
978 // else
979 // {
980 // // We were able
981 // if (error_ptr)
982 // error_ptr->SetErrorStringWithFormat ("Section %s in
983 // %s is not currently loaded.\n",
984 // sect->GetName().AsCString(),
985 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
986 // return false;
987 // }
988 // }
989 // break;
991 // OPCODE: DW_OP_deref
992 // OPERANDS: none
993 // DESCRIPTION: Pops the top stack entry and treats it as an address.
994 // The value retrieved from that address is pushed. The size of the data
995 // retrieved from the dereferenced address is the size of an address on the
996 // target machine.
997 case DW_OP_deref: {
998 if (stack.empty()) {
999 if (error_ptr)
1000 error_ptr->SetErrorString("Expression stack empty for DW_OP_deref.");
1001 return false;
1003 Value::ValueType value_type = stack.back().GetValueType();
1004 switch (value_type) {
1005 case Value::ValueType::HostAddress: {
1006 void *src = (void *)stack.back().GetScalar().ULongLong();
1007 intptr_t ptr;
1008 ::memcpy(&ptr, src, sizeof(void *));
1009 stack.back().GetScalar() = ptr;
1010 stack.back().ClearContext();
1011 } break;
1012 case Value::ValueType::FileAddress: {
1013 auto file_addr = stack.back().GetScalar().ULongLong(
1014 LLDB_INVALID_ADDRESS);
1016 Address so_addr;
1017 auto maybe_load_addr = ResolveLoadAddress(
1018 exe_ctx, module_sp, error_ptr, "DW_OP_deref", file_addr, so_addr);
1020 if (!maybe_load_addr)
1021 return false;
1023 stack.back().GetScalar() = *maybe_load_addr;
1024 // Fall through to load address promotion code below.
1026 [[fallthrough]];
1027 case Value::ValueType::Scalar:
1028 // Promote Scalar to LoadAddress and fall through.
1029 stack.back().SetValueType(Value::ValueType::LoadAddress);
1030 [[fallthrough]];
1031 case Value::ValueType::LoadAddress:
1032 if (exe_ctx) {
1033 if (process) {
1034 lldb::addr_t pointer_addr =
1035 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1036 Status error;
1037 lldb::addr_t pointer_value =
1038 process->ReadPointerFromMemory(pointer_addr, error);
1039 if (pointer_value != LLDB_INVALID_ADDRESS) {
1040 if (ABISP abi_sp = process->GetABI())
1041 pointer_value = abi_sp->FixCodeAddress(pointer_value);
1042 stack.back().GetScalar() = pointer_value;
1043 stack.back().ClearContext();
1044 } else {
1045 if (error_ptr)
1046 error_ptr->SetErrorStringWithFormat(
1047 "Failed to dereference pointer from 0x%" PRIx64
1048 " for DW_OP_deref: %s\n",
1049 pointer_addr, error.AsCString());
1050 return false;
1052 } else {
1053 if (error_ptr)
1054 error_ptr->SetErrorString("NULL process for DW_OP_deref.\n");
1055 return false;
1057 } else {
1058 if (error_ptr)
1059 error_ptr->SetErrorString(
1060 "NULL execution context for DW_OP_deref.\n");
1061 return false;
1063 break;
1065 case Value::ValueType::Invalid:
1066 if (error_ptr)
1067 error_ptr->SetErrorString("Invalid value type for DW_OP_deref.\n");
1068 return false;
1071 } break;
1073 // OPCODE: DW_OP_deref_size
1074 // OPERANDS: 1
1075 // 1 - uint8_t that specifies the size of the data to dereference.
1076 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1077 // stack entry and treats it as an address. The value retrieved from that
1078 // address is pushed. In the DW_OP_deref_size operation, however, the size
1079 // in bytes of the data retrieved from the dereferenced address is
1080 // specified by the single operand. This operand is a 1-byte unsigned
1081 // integral constant whose value may not be larger than the size of an
1082 // address on the target machine. The data retrieved is zero extended to
1083 // the size of an address on the target machine before being pushed on the
1084 // expression stack.
1085 case DW_OP_deref_size: {
1086 if (stack.empty()) {
1087 if (error_ptr)
1088 error_ptr->SetErrorString(
1089 "Expression stack empty for DW_OP_deref_size.");
1090 return false;
1092 uint8_t size = opcodes.GetU8(&offset);
1093 if (size > 8) {
1094 if (error_ptr)
1095 error_ptr->SetErrorStringWithFormat(
1096 "Invalid address size for DW_OP_deref_size: %d\n",
1097 size);
1098 return false;
1100 Value::ValueType value_type = stack.back().GetValueType();
1101 switch (value_type) {
1102 case Value::ValueType::HostAddress: {
1103 void *src = (void *)stack.back().GetScalar().ULongLong();
1104 intptr_t ptr;
1105 ::memcpy(&ptr, src, sizeof(void *));
1106 // I can't decide whether the size operand should apply to the bytes in
1107 // their
1108 // lldb-host endianness or the target endianness.. I doubt this'll ever
1109 // come up but I'll opt for assuming big endian regardless.
1110 switch (size) {
1111 case 1:
1112 ptr = ptr & 0xff;
1113 break;
1114 case 2:
1115 ptr = ptr & 0xffff;
1116 break;
1117 case 3:
1118 ptr = ptr & 0xffffff;
1119 break;
1120 case 4:
1121 ptr = ptr & 0xffffffff;
1122 break;
1123 // the casts are added to work around the case where intptr_t is a 32
1124 // bit quantity;
1125 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1126 // program.
1127 case 5:
1128 ptr = (intptr_t)ptr & 0xffffffffffULL;
1129 break;
1130 case 6:
1131 ptr = (intptr_t)ptr & 0xffffffffffffULL;
1132 break;
1133 case 7:
1134 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1135 break;
1136 default:
1137 break;
1139 stack.back().GetScalar() = ptr;
1140 stack.back().ClearContext();
1141 } break;
1142 case Value::ValueType::FileAddress: {
1143 auto file_addr =
1144 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1145 Address so_addr;
1146 auto maybe_load_addr =
1147 ResolveLoadAddress(exe_ctx, module_sp, error_ptr,
1148 "DW_OP_deref_size", file_addr, so_addr,
1149 /*check_sectionoffset=*/true);
1151 if (!maybe_load_addr)
1152 return false;
1154 addr_t load_addr = *maybe_load_addr;
1156 if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) {
1157 uint8_t addr_bytes[8];
1158 Status error;
1160 if (target &&
1161 target->ReadMemory(so_addr, &addr_bytes, size, error,
1162 /*force_live_memory=*/false) == size) {
1163 ObjectFile *objfile = module_sp->GetObjectFile();
1165 stack.back().GetScalar() = DerefSizeExtractDataHelper(
1166 addr_bytes, size, objfile->GetByteOrder(), size);
1167 stack.back().ClearContext();
1168 break;
1169 } else {
1170 if (error_ptr)
1171 error_ptr->SetErrorStringWithFormat(
1172 "Failed to dereference pointer for DW_OP_deref_size: "
1173 "%s\n",
1174 error.AsCString());
1175 return false;
1178 stack.back().GetScalar() = load_addr;
1179 // Fall through to load address promotion code below.
1182 [[fallthrough]];
1183 case Value::ValueType::Scalar:
1184 case Value::ValueType::LoadAddress:
1185 if (exe_ctx) {
1186 if (process) {
1187 lldb::addr_t pointer_addr =
1188 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1189 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1190 Status error;
1191 if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) ==
1192 size) {
1194 stack.back().GetScalar() =
1195 DerefSizeExtractDataHelper(addr_bytes, sizeof(addr_bytes),
1196 process->GetByteOrder(), size);
1197 stack.back().ClearContext();
1198 } else {
1199 if (error_ptr)
1200 error_ptr->SetErrorStringWithFormat(
1201 "Failed to dereference pointer from 0x%" PRIx64
1202 " for DW_OP_deref: %s\n",
1203 pointer_addr, error.AsCString());
1204 return false;
1206 } else {
1207 if (error_ptr)
1208 error_ptr->SetErrorString("NULL process for DW_OP_deref_size.\n");
1209 return false;
1211 } else {
1212 if (error_ptr)
1213 error_ptr->SetErrorString(
1214 "NULL execution context for DW_OP_deref_size.\n");
1215 return false;
1217 break;
1219 case Value::ValueType::Invalid:
1220 if (error_ptr)
1221 error_ptr->SetErrorString("Invalid value for DW_OP_deref_size.\n");
1222 return false;
1225 } break;
1227 // OPCODE: DW_OP_xderef_size
1228 // OPERANDS: 1
1229 // 1 - uint8_t that specifies the size of the data to dereference.
1230 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1231 // the top of the stack is treated as an address. The second stack entry is
1232 // treated as an "address space identifier" for those architectures that
1233 // support multiple address spaces. The top two stack elements are popped,
1234 // a data item is retrieved through an implementation-defined address
1235 // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1236 // operation, however, the size in bytes of the data retrieved from the
1237 // dereferenced address is specified by the single operand. This operand is
1238 // a 1-byte unsigned integral constant whose value may not be larger than
1239 // the size of an address on the target machine. The data retrieved is zero
1240 // extended to the size of an address on the target machine before being
1241 // pushed on the expression stack.
1242 case DW_OP_xderef_size:
1243 if (error_ptr)
1244 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1245 return false;
1246 // OPCODE: DW_OP_xderef
1247 // OPERANDS: none
1248 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1249 // the top of the stack is treated as an address. The second stack entry is
1250 // treated as an "address space identifier" for those architectures that
1251 // support multiple address spaces. The top two stack elements are popped,
1252 // a data item is retrieved through an implementation-defined address
1253 // calculation and pushed as the new stack top. The size of the data
1254 // retrieved from the dereferenced address is the size of an address on the
1255 // target machine.
1256 case DW_OP_xderef:
1257 if (error_ptr)
1258 error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1259 return false;
1261 // All DW_OP_constXXX opcodes have a single operand as noted below:
1263 // Opcode Operand 1
1264 // DW_OP_const1u 1-byte unsigned integer constant
1265 // DW_OP_const1s 1-byte signed integer constant
1266 // DW_OP_const2u 2-byte unsigned integer constant
1267 // DW_OP_const2s 2-byte signed integer constant
1268 // DW_OP_const4u 4-byte unsigned integer constant
1269 // DW_OP_const4s 4-byte signed integer constant
1270 // DW_OP_const8u 8-byte unsigned integer constant
1271 // DW_OP_const8s 8-byte signed integer constant
1272 // DW_OP_constu unsigned LEB128 integer constant
1273 // DW_OP_consts signed LEB128 integer constant
1274 case DW_OP_const1u:
1275 stack.push_back(to_generic(opcodes.GetU8(&offset)));
1276 break;
1277 case DW_OP_const1s:
1278 stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset)));
1279 break;
1280 case DW_OP_const2u:
1281 stack.push_back(to_generic(opcodes.GetU16(&offset)));
1282 break;
1283 case DW_OP_const2s:
1284 stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset)));
1285 break;
1286 case DW_OP_const4u:
1287 stack.push_back(to_generic(opcodes.GetU32(&offset)));
1288 break;
1289 case DW_OP_const4s:
1290 stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset)));
1291 break;
1292 case DW_OP_const8u:
1293 stack.push_back(to_generic(opcodes.GetU64(&offset)));
1294 break;
1295 case DW_OP_const8s:
1296 stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset)));
1297 break;
1298 // These should also use to_generic, but we can't do that due to a
1299 // producer-side bug in llvm. See llvm.org/pr48087.
1300 case DW_OP_constu:
1301 stack.push_back(Scalar(opcodes.GetULEB128(&offset)));
1302 break;
1303 case DW_OP_consts:
1304 stack.push_back(Scalar(opcodes.GetSLEB128(&offset)));
1305 break;
1307 // OPCODE: DW_OP_dup
1308 // OPERANDS: none
1309 // DESCRIPTION: duplicates the value at the top of the stack
1310 case DW_OP_dup:
1311 if (stack.empty()) {
1312 if (error_ptr)
1313 error_ptr->SetErrorString("Expression stack empty for DW_OP_dup.");
1314 return false;
1315 } else
1316 stack.push_back(stack.back());
1317 break;
1319 // OPCODE: DW_OP_drop
1320 // OPERANDS: none
1321 // DESCRIPTION: pops the value at the top of the stack
1322 case DW_OP_drop:
1323 if (stack.empty()) {
1324 if (error_ptr)
1325 error_ptr->SetErrorString("Expression stack empty for DW_OP_drop.");
1326 return false;
1327 } else
1328 stack.pop_back();
1329 break;
1331 // OPCODE: DW_OP_over
1332 // OPERANDS: none
1333 // DESCRIPTION: Duplicates the entry currently second in the stack at
1334 // the top of the stack.
1335 case DW_OP_over:
1336 if (stack.size() < 2) {
1337 if (error_ptr)
1338 error_ptr->SetErrorString(
1339 "Expression stack needs at least 2 items for DW_OP_over.");
1340 return false;
1341 } else
1342 stack.push_back(stack[stack.size() - 2]);
1343 break;
1345 // OPCODE: DW_OP_pick
1346 // OPERANDS: uint8_t index into the current stack
1347 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1348 // inclusive) is pushed on the stack
1349 case DW_OP_pick: {
1350 uint8_t pick_idx = opcodes.GetU8(&offset);
1351 if (pick_idx < stack.size())
1352 stack.push_back(stack[stack.size() - 1 - pick_idx]);
1353 else {
1354 if (error_ptr)
1355 error_ptr->SetErrorStringWithFormat(
1356 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1357 return false;
1359 } break;
1361 // OPCODE: DW_OP_swap
1362 // OPERANDS: none
1363 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1364 // of the stack becomes the second stack entry, and the second entry
1365 // becomes the top of the stack
1366 case DW_OP_swap:
1367 if (stack.size() < 2) {
1368 if (error_ptr)
1369 error_ptr->SetErrorString(
1370 "Expression stack needs at least 2 items for DW_OP_swap.");
1371 return false;
1372 } else {
1373 tmp = stack.back();
1374 stack.back() = stack[stack.size() - 2];
1375 stack[stack.size() - 2] = tmp;
1377 break;
1379 // OPCODE: DW_OP_rot
1380 // OPERANDS: none
1381 // DESCRIPTION: Rotates the first three stack entries. The entry at
1382 // the top of the stack becomes the third stack entry, the second entry
1383 // becomes the top of the stack, and the third entry becomes the second
1384 // entry.
1385 case DW_OP_rot:
1386 if (stack.size() < 3) {
1387 if (error_ptr)
1388 error_ptr->SetErrorString(
1389 "Expression stack needs at least 3 items for DW_OP_rot.");
1390 return false;
1391 } else {
1392 size_t last_idx = stack.size() - 1;
1393 Value old_top = stack[last_idx];
1394 stack[last_idx] = stack[last_idx - 1];
1395 stack[last_idx - 1] = stack[last_idx - 2];
1396 stack[last_idx - 2] = old_top;
1398 break;
1400 // OPCODE: DW_OP_abs
1401 // OPERANDS: none
1402 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1403 // value and pushes its absolute value. If the absolute value can not be
1404 // represented, the result is undefined.
1405 case DW_OP_abs:
1406 if (stack.empty()) {
1407 if (error_ptr)
1408 error_ptr->SetErrorString(
1409 "Expression stack needs at least 1 item for DW_OP_abs.");
1410 return false;
1411 } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) {
1412 if (error_ptr)
1413 error_ptr->SetErrorString(
1414 "Failed to take the absolute value of the first stack item.");
1415 return false;
1417 break;
1419 // OPCODE: DW_OP_and
1420 // OPERANDS: none
1421 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1422 // operation on the two, and pushes the result.
1423 case DW_OP_and:
1424 if (stack.size() < 2) {
1425 if (error_ptr)
1426 error_ptr->SetErrorString(
1427 "Expression stack needs at least 2 items for DW_OP_and.");
1428 return false;
1429 } else {
1430 tmp = stack.back();
1431 stack.pop_back();
1432 stack.back().ResolveValue(exe_ctx) =
1433 stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx);
1435 break;
1437 // OPCODE: DW_OP_div
1438 // OPERANDS: none
1439 // DESCRIPTION: pops the top two stack values, divides the former second
1440 // entry by the former top of the stack using signed division, and pushes
1441 // the result.
1442 case DW_OP_div:
1443 if (stack.size() < 2) {
1444 if (error_ptr)
1445 error_ptr->SetErrorString(
1446 "Expression stack needs at least 2 items for DW_OP_div.");
1447 return false;
1448 } else {
1449 tmp = stack.back();
1450 if (tmp.ResolveValue(exe_ctx).IsZero()) {
1451 if (error_ptr)
1452 error_ptr->SetErrorString("Divide by zero.");
1453 return false;
1454 } else {
1455 stack.pop_back();
1456 Scalar divisor, dividend;
1457 divisor = tmp.ResolveValue(exe_ctx);
1458 dividend = stack.back().ResolveValue(exe_ctx);
1459 divisor.MakeSigned();
1460 dividend.MakeSigned();
1461 stack.back() = dividend / divisor;
1462 if (!stack.back().ResolveValue(exe_ctx).IsValid()) {
1463 if (error_ptr)
1464 error_ptr->SetErrorString("Divide failed.");
1465 return false;
1469 break;
1471 // OPCODE: DW_OP_minus
1472 // OPERANDS: none
1473 // DESCRIPTION: pops the top two stack values, subtracts the former top
1474 // of the stack from the former second entry, and pushes the result.
1475 case DW_OP_minus:
1476 if (stack.size() < 2) {
1477 if (error_ptr)
1478 error_ptr->SetErrorString(
1479 "Expression stack needs at least 2 items for DW_OP_minus.");
1480 return false;
1481 } else {
1482 tmp = stack.back();
1483 stack.pop_back();
1484 stack.back().ResolveValue(exe_ctx) =
1485 stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx);
1487 break;
1489 // OPCODE: DW_OP_mod
1490 // OPERANDS: none
1491 // DESCRIPTION: pops the top two stack values and pushes the result of
1492 // the calculation: former second stack entry modulo the former top of the
1493 // stack.
1494 case DW_OP_mod:
1495 if (stack.size() < 2) {
1496 if (error_ptr)
1497 error_ptr->SetErrorString(
1498 "Expression stack needs at least 2 items for DW_OP_mod.");
1499 return false;
1500 } else {
1501 tmp = stack.back();
1502 stack.pop_back();
1503 stack.back().ResolveValue(exe_ctx) =
1504 stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx);
1506 break;
1508 // OPCODE: DW_OP_mul
1509 // OPERANDS: none
1510 // DESCRIPTION: pops the top two stack entries, multiplies them
1511 // together, and pushes the result.
1512 case DW_OP_mul:
1513 if (stack.size() < 2) {
1514 if (error_ptr)
1515 error_ptr->SetErrorString(
1516 "Expression stack needs at least 2 items for DW_OP_mul.");
1517 return false;
1518 } else {
1519 tmp = stack.back();
1520 stack.pop_back();
1521 stack.back().ResolveValue(exe_ctx) =
1522 stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx);
1524 break;
1526 // OPCODE: DW_OP_neg
1527 // OPERANDS: none
1528 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1529 case DW_OP_neg:
1530 if (stack.empty()) {
1531 if (error_ptr)
1532 error_ptr->SetErrorString(
1533 "Expression stack needs at least 1 item for DW_OP_neg.");
1534 return false;
1535 } else {
1536 if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) {
1537 if (error_ptr)
1538 error_ptr->SetErrorString("Unary negate failed.");
1539 return false;
1542 break;
1544 // OPCODE: DW_OP_not
1545 // OPERANDS: none
1546 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1547 // complement
1548 case DW_OP_not:
1549 if (stack.empty()) {
1550 if (error_ptr)
1551 error_ptr->SetErrorString(
1552 "Expression stack needs at least 1 item for DW_OP_not.");
1553 return false;
1554 } else {
1555 if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) {
1556 if (error_ptr)
1557 error_ptr->SetErrorString("Logical NOT failed.");
1558 return false;
1561 break;
1563 // OPCODE: DW_OP_or
1564 // OPERANDS: none
1565 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1566 // operation on the two, and pushes the result.
1567 case DW_OP_or:
1568 if (stack.size() < 2) {
1569 if (error_ptr)
1570 error_ptr->SetErrorString(
1571 "Expression stack needs at least 2 items for DW_OP_or.");
1572 return false;
1573 } else {
1574 tmp = stack.back();
1575 stack.pop_back();
1576 stack.back().ResolveValue(exe_ctx) =
1577 stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx);
1579 break;
1581 // OPCODE: DW_OP_plus
1582 // OPERANDS: none
1583 // DESCRIPTION: pops the top two stack entries, adds them together, and
1584 // pushes the result.
1585 case DW_OP_plus:
1586 if (stack.size() < 2) {
1587 if (error_ptr)
1588 error_ptr->SetErrorString(
1589 "Expression stack needs at least 2 items for DW_OP_plus.");
1590 return false;
1591 } else {
1592 tmp = stack.back();
1593 stack.pop_back();
1594 stack.back().GetScalar() += tmp.GetScalar();
1596 break;
1598 // OPCODE: DW_OP_plus_uconst
1599 // OPERANDS: none
1600 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1601 // constant operand and pushes the result.
1602 case DW_OP_plus_uconst:
1603 if (stack.empty()) {
1604 if (error_ptr)
1605 error_ptr->SetErrorString(
1606 "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
1607 return false;
1608 } else {
1609 const uint64_t uconst_value = opcodes.GetULEB128(&offset);
1610 // Implicit conversion from a UINT to a Scalar...
1611 stack.back().GetScalar() += uconst_value;
1612 if (!stack.back().GetScalar().IsValid()) {
1613 if (error_ptr)
1614 error_ptr->SetErrorString("DW_OP_plus_uconst failed.");
1615 return false;
1618 break;
1620 // OPCODE: DW_OP_shl
1621 // OPERANDS: none
1622 // DESCRIPTION: pops the top two stack entries, shifts the former
1623 // second entry left by the number of bits specified by the former top of
1624 // the stack, and pushes the result.
1625 case DW_OP_shl:
1626 if (stack.size() < 2) {
1627 if (error_ptr)
1628 error_ptr->SetErrorString(
1629 "Expression stack needs at least 2 items for DW_OP_shl.");
1630 return false;
1631 } else {
1632 tmp = stack.back();
1633 stack.pop_back();
1634 stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx);
1636 break;
1638 // OPCODE: DW_OP_shr
1639 // OPERANDS: none
1640 // DESCRIPTION: pops the top two stack entries, shifts the former second
1641 // entry right logically (filling with zero bits) by the number of bits
1642 // specified by the former top of the stack, and pushes the result.
1643 case DW_OP_shr:
1644 if (stack.size() < 2) {
1645 if (error_ptr)
1646 error_ptr->SetErrorString(
1647 "Expression stack needs at least 2 items for DW_OP_shr.");
1648 return false;
1649 } else {
1650 tmp = stack.back();
1651 stack.pop_back();
1652 if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical(
1653 tmp.ResolveValue(exe_ctx))) {
1654 if (error_ptr)
1655 error_ptr->SetErrorString("DW_OP_shr failed.");
1656 return false;
1659 break;
1661 // OPCODE: DW_OP_shra
1662 // OPERANDS: none
1663 // DESCRIPTION: pops the top two stack entries, shifts the former second
1664 // entry right arithmetically (divide the magnitude by 2, keep the same
1665 // sign for the result) by the number of bits specified by the former top
1666 // of the stack, and pushes the result.
1667 case DW_OP_shra:
1668 if (stack.size() < 2) {
1669 if (error_ptr)
1670 error_ptr->SetErrorString(
1671 "Expression stack needs at least 2 items for DW_OP_shra.");
1672 return false;
1673 } else {
1674 tmp = stack.back();
1675 stack.pop_back();
1676 stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx);
1678 break;
1680 // OPCODE: DW_OP_xor
1681 // OPERANDS: none
1682 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1683 // exclusive-or operation on the two, and pushes the result.
1684 case DW_OP_xor:
1685 if (stack.size() < 2) {
1686 if (error_ptr)
1687 error_ptr->SetErrorString(
1688 "Expression stack needs at least 2 items for DW_OP_xor.");
1689 return false;
1690 } else {
1691 tmp = stack.back();
1692 stack.pop_back();
1693 stack.back().ResolveValue(exe_ctx) =
1694 stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx);
1696 break;
1698 // OPCODE: DW_OP_skip
1699 // OPERANDS: int16_t
1700 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1701 // signed integer constant. The 2-byte constant is the number of bytes of
1702 // the DWARF expression to skip forward or backward from the current
1703 // operation, beginning after the 2-byte constant.
1704 case DW_OP_skip: {
1705 int16_t skip_offset = (int16_t)opcodes.GetU16(&offset);
1706 lldb::offset_t new_offset = offset + skip_offset;
1707 // New offset can point at the end of the data, in this case we should
1708 // terminate the DWARF expression evaluation (will happen in the loop
1709 // condition).
1710 if (new_offset <= opcodes.GetByteSize())
1711 offset = new_offset;
1712 else {
1713 if (error_ptr)
1714 error_ptr->SetErrorStringWithFormatv(
1715 "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}", offset,
1716 skip_offset, opcodes.GetByteSize());
1717 return false;
1719 } break;
1721 // OPCODE: DW_OP_bra
1722 // OPERANDS: int16_t
1723 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1724 // signed integer constant. This operation pops the top of stack. If the
1725 // value popped is not the constant 0, the 2-byte constant operand is the
1726 // number of bytes of the DWARF expression to skip forward or backward from
1727 // the current operation, beginning after the 2-byte constant.
1728 case DW_OP_bra:
1729 if (stack.empty()) {
1730 if (error_ptr)
1731 error_ptr->SetErrorString(
1732 "Expression stack needs at least 1 item for DW_OP_bra.");
1733 return false;
1734 } else {
1735 tmp = stack.back();
1736 stack.pop_back();
1737 int16_t bra_offset = (int16_t)opcodes.GetU16(&offset);
1738 Scalar zero(0);
1739 if (tmp.ResolveValue(exe_ctx) != zero) {
1740 lldb::offset_t new_offset = offset + bra_offset;
1741 // New offset can point at the end of the data, in this case we should
1742 // terminate the DWARF expression evaluation (will happen in the loop
1743 // condition).
1744 if (new_offset <= opcodes.GetByteSize())
1745 offset = new_offset;
1746 else {
1747 if (error_ptr)
1748 error_ptr->SetErrorStringWithFormatv(
1749 "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}", offset,
1750 bra_offset, opcodes.GetByteSize());
1751 return false;
1755 break;
1757 // OPCODE: DW_OP_eq
1758 // OPERANDS: none
1759 // DESCRIPTION: pops the top two stack values, compares using the
1760 // equals (==) operator.
1761 // STACK RESULT: push the constant value 1 onto the stack if the result
1762 // of the operation is true or the constant value 0 if the result of the
1763 // operation is false.
1764 case DW_OP_eq:
1765 if (stack.size() < 2) {
1766 if (error_ptr)
1767 error_ptr->SetErrorString(
1768 "Expression stack needs at least 2 items for DW_OP_eq.");
1769 return false;
1770 } else {
1771 tmp = stack.back();
1772 stack.pop_back();
1773 stack.back().ResolveValue(exe_ctx) =
1774 stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx);
1776 break;
1778 // OPCODE: DW_OP_ge
1779 // OPERANDS: none
1780 // DESCRIPTION: pops the top two stack values, compares using the
1781 // greater than or equal to (>=) operator.
1782 // STACK RESULT: push the constant value 1 onto the stack if the result
1783 // of the operation is true or the constant value 0 if the result of the
1784 // operation is false.
1785 case DW_OP_ge:
1786 if (stack.size() < 2) {
1787 if (error_ptr)
1788 error_ptr->SetErrorString(
1789 "Expression stack needs at least 2 items for DW_OP_ge.");
1790 return false;
1791 } else {
1792 tmp = stack.back();
1793 stack.pop_back();
1794 stack.back().ResolveValue(exe_ctx) =
1795 stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx);
1797 break;
1799 // OPCODE: DW_OP_gt
1800 // OPERANDS: none
1801 // DESCRIPTION: pops the top two stack values, compares using the
1802 // greater than (>) operator.
1803 // STACK RESULT: push the constant value 1 onto the stack if the result
1804 // of the operation is true or the constant value 0 if the result of the
1805 // operation is false.
1806 case DW_OP_gt:
1807 if (stack.size() < 2) {
1808 if (error_ptr)
1809 error_ptr->SetErrorString(
1810 "Expression stack needs at least 2 items for DW_OP_gt.");
1811 return false;
1812 } else {
1813 tmp = stack.back();
1814 stack.pop_back();
1815 stack.back().ResolveValue(exe_ctx) =
1816 stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx);
1818 break;
1820 // OPCODE: DW_OP_le
1821 // OPERANDS: none
1822 // DESCRIPTION: pops the top two stack values, compares using the
1823 // less than or equal to (<=) operator.
1824 // STACK RESULT: push the constant value 1 onto the stack if the result
1825 // of the operation is true or the constant value 0 if the result of the
1826 // operation is false.
1827 case DW_OP_le:
1828 if (stack.size() < 2) {
1829 if (error_ptr)
1830 error_ptr->SetErrorString(
1831 "Expression stack needs at least 2 items for DW_OP_le.");
1832 return false;
1833 } else {
1834 tmp = stack.back();
1835 stack.pop_back();
1836 stack.back().ResolveValue(exe_ctx) =
1837 stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx);
1839 break;
1841 // OPCODE: DW_OP_lt
1842 // OPERANDS: none
1843 // DESCRIPTION: pops the top two stack values, compares using the
1844 // less than (<) operator.
1845 // STACK RESULT: push the constant value 1 onto the stack if the result
1846 // of the operation is true or the constant value 0 if the result of the
1847 // operation is false.
1848 case DW_OP_lt:
1849 if (stack.size() < 2) {
1850 if (error_ptr)
1851 error_ptr->SetErrorString(
1852 "Expression stack needs at least 2 items for DW_OP_lt.");
1853 return false;
1854 } else {
1855 tmp = stack.back();
1856 stack.pop_back();
1857 stack.back().ResolveValue(exe_ctx) =
1858 stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx);
1860 break;
1862 // OPCODE: DW_OP_ne
1863 // OPERANDS: none
1864 // DESCRIPTION: pops the top two stack values, compares using the
1865 // not equal (!=) operator.
1866 // STACK RESULT: push the constant value 1 onto the stack if the result
1867 // of the operation is true or the constant value 0 if the result of the
1868 // operation is false.
1869 case DW_OP_ne:
1870 if (stack.size() < 2) {
1871 if (error_ptr)
1872 error_ptr->SetErrorString(
1873 "Expression stack needs at least 2 items for DW_OP_ne.");
1874 return false;
1875 } else {
1876 tmp = stack.back();
1877 stack.pop_back();
1878 stack.back().ResolveValue(exe_ctx) =
1879 stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx);
1881 break;
1883 // OPCODE: DW_OP_litn
1884 // OPERANDS: none
1885 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1886 // STACK RESULT: push the unsigned literal constant value onto the top
1887 // of the stack.
1888 case DW_OP_lit0:
1889 case DW_OP_lit1:
1890 case DW_OP_lit2:
1891 case DW_OP_lit3:
1892 case DW_OP_lit4:
1893 case DW_OP_lit5:
1894 case DW_OP_lit6:
1895 case DW_OP_lit7:
1896 case DW_OP_lit8:
1897 case DW_OP_lit9:
1898 case DW_OP_lit10:
1899 case DW_OP_lit11:
1900 case DW_OP_lit12:
1901 case DW_OP_lit13:
1902 case DW_OP_lit14:
1903 case DW_OP_lit15:
1904 case DW_OP_lit16:
1905 case DW_OP_lit17:
1906 case DW_OP_lit18:
1907 case DW_OP_lit19:
1908 case DW_OP_lit20:
1909 case DW_OP_lit21:
1910 case DW_OP_lit22:
1911 case DW_OP_lit23:
1912 case DW_OP_lit24:
1913 case DW_OP_lit25:
1914 case DW_OP_lit26:
1915 case DW_OP_lit27:
1916 case DW_OP_lit28:
1917 case DW_OP_lit29:
1918 case DW_OP_lit30:
1919 case DW_OP_lit31:
1920 stack.push_back(to_generic(op - DW_OP_lit0));
1921 break;
1923 // OPCODE: DW_OP_regN
1924 // OPERANDS: none
1925 // DESCRIPTION: Push the value in register n on the top of the stack.
1926 case DW_OP_reg0:
1927 case DW_OP_reg1:
1928 case DW_OP_reg2:
1929 case DW_OP_reg3:
1930 case DW_OP_reg4:
1931 case DW_OP_reg5:
1932 case DW_OP_reg6:
1933 case DW_OP_reg7:
1934 case DW_OP_reg8:
1935 case DW_OP_reg9:
1936 case DW_OP_reg10:
1937 case DW_OP_reg11:
1938 case DW_OP_reg12:
1939 case DW_OP_reg13:
1940 case DW_OP_reg14:
1941 case DW_OP_reg15:
1942 case DW_OP_reg16:
1943 case DW_OP_reg17:
1944 case DW_OP_reg18:
1945 case DW_OP_reg19:
1946 case DW_OP_reg20:
1947 case DW_OP_reg21:
1948 case DW_OP_reg22:
1949 case DW_OP_reg23:
1950 case DW_OP_reg24:
1951 case DW_OP_reg25:
1952 case DW_OP_reg26:
1953 case DW_OP_reg27:
1954 case DW_OP_reg28:
1955 case DW_OP_reg29:
1956 case DW_OP_reg30:
1957 case DW_OP_reg31: {
1958 dwarf4_location_description_kind = Register;
1959 reg_num = op - DW_OP_reg0;
1961 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
1962 stack.push_back(tmp);
1963 else
1964 return false;
1965 } break;
1966 // OPCODE: DW_OP_regx
1967 // OPERANDS:
1968 // ULEB128 literal operand that encodes the register.
1969 // DESCRIPTION: Push the value in register on the top of the stack.
1970 case DW_OP_regx: {
1971 dwarf4_location_description_kind = Register;
1972 reg_num = opcodes.GetULEB128(&offset);
1973 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp))
1974 stack.push_back(tmp);
1975 else
1976 return false;
1977 } break;
1979 // OPCODE: DW_OP_bregN
1980 // OPERANDS:
1981 // SLEB128 offset from register N
1982 // DESCRIPTION: Value is in memory at the address specified by register
1983 // N plus an offset.
1984 case DW_OP_breg0:
1985 case DW_OP_breg1:
1986 case DW_OP_breg2:
1987 case DW_OP_breg3:
1988 case DW_OP_breg4:
1989 case DW_OP_breg5:
1990 case DW_OP_breg6:
1991 case DW_OP_breg7:
1992 case DW_OP_breg8:
1993 case DW_OP_breg9:
1994 case DW_OP_breg10:
1995 case DW_OP_breg11:
1996 case DW_OP_breg12:
1997 case DW_OP_breg13:
1998 case DW_OP_breg14:
1999 case DW_OP_breg15:
2000 case DW_OP_breg16:
2001 case DW_OP_breg17:
2002 case DW_OP_breg18:
2003 case DW_OP_breg19:
2004 case DW_OP_breg20:
2005 case DW_OP_breg21:
2006 case DW_OP_breg22:
2007 case DW_OP_breg23:
2008 case DW_OP_breg24:
2009 case DW_OP_breg25:
2010 case DW_OP_breg26:
2011 case DW_OP_breg27:
2012 case DW_OP_breg28:
2013 case DW_OP_breg29:
2014 case DW_OP_breg30:
2015 case DW_OP_breg31: {
2016 reg_num = op - DW_OP_breg0;
2018 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2019 tmp)) {
2020 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2021 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2022 tmp.ClearContext();
2023 stack.push_back(tmp);
2024 stack.back().SetValueType(Value::ValueType::LoadAddress);
2025 } else
2026 return false;
2027 } break;
2028 // OPCODE: DW_OP_bregx
2029 // OPERANDS: 2
2030 // ULEB128 literal operand that encodes the register.
2031 // SLEB128 offset from register N
2032 // DESCRIPTION: Value is in memory at the address specified by register
2033 // N plus an offset.
2034 case DW_OP_bregx: {
2035 reg_num = opcodes.GetULEB128(&offset);
2037 if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr,
2038 tmp)) {
2039 int64_t breg_offset = opcodes.GetSLEB128(&offset);
2040 tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset;
2041 tmp.ClearContext();
2042 stack.push_back(tmp);
2043 stack.back().SetValueType(Value::ValueType::LoadAddress);
2044 } else
2045 return false;
2046 } break;
2048 case DW_OP_fbreg:
2049 if (exe_ctx) {
2050 if (frame) {
2051 Scalar value;
2052 if (frame->GetFrameBaseValue(value, error_ptr)) {
2053 int64_t fbreg_offset = opcodes.GetSLEB128(&offset);
2054 value += fbreg_offset;
2055 stack.push_back(value);
2056 stack.back().SetValueType(Value::ValueType::LoadAddress);
2057 } else
2058 return false;
2059 } else {
2060 if (error_ptr)
2061 error_ptr->SetErrorString(
2062 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2063 return false;
2065 } else {
2066 if (error_ptr)
2067 error_ptr->SetErrorString(
2068 "NULL execution context for DW_OP_fbreg.\n");
2069 return false;
2072 break;
2074 // OPCODE: DW_OP_nop
2075 // OPERANDS: none
2076 // DESCRIPTION: A place holder. It has no effect on the location stack
2077 // or any of its values.
2078 case DW_OP_nop:
2079 break;
2081 // OPCODE: DW_OP_piece
2082 // OPERANDS: 1
2083 // ULEB128: byte size of the piece
2084 // DESCRIPTION: The operand describes the size in bytes of the piece of
2085 // the object referenced by the DWARF expression whose result is at the top
2086 // of the stack. If the piece is located in a register, but does not occupy
2087 // the entire register, the placement of the piece within that register is
2088 // defined by the ABI.
2090 // Many compilers store a single variable in sets of registers, or store a
2091 // variable partially in memory and partially in registers. DW_OP_piece
2092 // provides a way of describing how large a part of a variable a particular
2093 // DWARF expression refers to.
2094 case DW_OP_piece: {
2095 LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind;
2096 // Reset for the next piece.
2097 dwarf4_location_description_kind = Memory;
2099 const uint64_t piece_byte_size = opcodes.GetULEB128(&offset);
2101 if (piece_byte_size > 0) {
2102 Value curr_piece;
2104 if (stack.empty()) {
2105 UpdateValueTypeFromLocationDescription(
2106 log, dwarf_cu, LocationDescriptionKind::Empty);
2107 // In a multi-piece expression, this means that the current piece is
2108 // not available. Fill with zeros for now by resizing the data and
2109 // appending it
2110 curr_piece.ResizeData(piece_byte_size);
2111 // Note that "0" is not a correct value for the unknown bits.
2112 // It would be better to also return a mask of valid bits together
2113 // with the expression result, so the debugger can print missing
2114 // members as "<optimized out>" or something.
2115 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
2116 pieces.AppendDataToHostBuffer(curr_piece);
2117 } else {
2118 Status error;
2119 // Extract the current piece into "curr_piece"
2120 Value curr_piece_source_value(stack.back());
2121 stack.pop_back();
2122 UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc,
2123 &curr_piece_source_value);
2125 const Value::ValueType curr_piece_source_value_type =
2126 curr_piece_source_value.GetValueType();
2127 switch (curr_piece_source_value_type) {
2128 case Value::ValueType::Invalid:
2129 return false;
2130 case Value::ValueType::LoadAddress:
2131 if (process) {
2132 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
2133 lldb::addr_t load_addr =
2134 curr_piece_source_value.GetScalar().ULongLong(
2135 LLDB_INVALID_ADDRESS);
2136 if (process->ReadMemory(
2137 load_addr, curr_piece.GetBuffer().GetBytes(),
2138 piece_byte_size, error) != piece_byte_size) {
2139 if (error_ptr)
2140 error_ptr->SetErrorStringWithFormat(
2141 "failed to read memory DW_OP_piece(%" PRIu64
2142 ") from 0x%" PRIx64,
2143 piece_byte_size, load_addr);
2144 return false;
2146 } else {
2147 if (error_ptr)
2148 error_ptr->SetErrorStringWithFormat(
2149 "failed to resize the piece memory buffer for "
2150 "DW_OP_piece(%" PRIu64 ")",
2151 piece_byte_size);
2152 return false;
2155 break;
2157 case Value::ValueType::FileAddress:
2158 case Value::ValueType::HostAddress:
2159 if (error_ptr) {
2160 lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong(
2161 LLDB_INVALID_ADDRESS);
2162 error_ptr->SetErrorStringWithFormat(
2163 "failed to read memory DW_OP_piece(%" PRIu64
2164 ") from %s address 0x%" PRIx64,
2165 piece_byte_size, curr_piece_source_value.GetValueType() ==
2166 Value::ValueType::FileAddress
2167 ? "file"
2168 : "host",
2169 addr);
2171 return false;
2173 case Value::ValueType::Scalar: {
2174 uint32_t bit_size = piece_byte_size * 8;
2175 uint32_t bit_offset = 0;
2176 Scalar &scalar = curr_piece_source_value.GetScalar();
2177 if (!scalar.ExtractBitfield(
2178 bit_size, bit_offset)) {
2179 if (error_ptr)
2180 error_ptr->SetErrorStringWithFormat(
2181 "unable to extract %" PRIu64 " bytes from a %" PRIu64
2182 " byte scalar value.",
2183 piece_byte_size,
2184 (uint64_t)curr_piece_source_value.GetScalar()
2185 .GetByteSize());
2186 return false;
2188 // Create curr_piece with bit_size. By default Scalar
2189 // grows to the nearest host integer type.
2190 llvm::APInt fail_value(1, 0, false);
2191 llvm::APInt ap_int = scalar.UInt128(fail_value);
2192 assert(ap_int.getBitWidth() >= bit_size);
2193 llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(),
2194 ap_int.getNumWords()};
2195 curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf));
2196 } break;
2199 // Check if this is the first piece?
2200 if (op_piece_offset == 0) {
2201 // This is the first piece, we should push it back onto the stack
2202 // so subsequent pieces will be able to access this piece and add
2203 // to it.
2204 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2205 if (error_ptr)
2206 error_ptr->SetErrorString("failed to append piece data");
2207 return false;
2209 } else {
2210 // If this is the second or later piece there should be a value on
2211 // the stack.
2212 if (pieces.GetBuffer().GetByteSize() != op_piece_offset) {
2213 if (error_ptr)
2214 error_ptr->SetErrorStringWithFormat(
2215 "DW_OP_piece for offset %" PRIu64
2216 " but top of stack is of size %" PRIu64,
2217 op_piece_offset, pieces.GetBuffer().GetByteSize());
2218 return false;
2221 if (pieces.AppendDataToHostBuffer(curr_piece) == 0) {
2222 if (error_ptr)
2223 error_ptr->SetErrorString("failed to append piece data");
2224 return false;
2228 op_piece_offset += piece_byte_size;
2230 } break;
2232 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
2233 if (stack.size() < 1) {
2234 UpdateValueTypeFromLocationDescription(log, dwarf_cu,
2235 LocationDescriptionKind::Empty);
2236 // Reset for the next piece.
2237 dwarf4_location_description_kind = Memory;
2238 if (error_ptr)
2239 error_ptr->SetErrorString(
2240 "Expression stack needs at least 1 item for DW_OP_bit_piece.");
2241 return false;
2242 } else {
2243 UpdateValueTypeFromLocationDescription(
2244 log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
2245 // Reset for the next piece.
2246 dwarf4_location_description_kind = Memory;
2247 const uint64_t piece_bit_size = opcodes.GetULEB128(&offset);
2248 const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset);
2249 switch (stack.back().GetValueType()) {
2250 case Value::ValueType::Invalid:
2251 return false;
2252 case Value::ValueType::Scalar: {
2253 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
2254 piece_bit_offset)) {
2255 if (error_ptr)
2256 error_ptr->SetErrorStringWithFormat(
2257 "unable to extract %" PRIu64 " bit value with %" PRIu64
2258 " bit offset from a %" PRIu64 " bit scalar value.",
2259 piece_bit_size, piece_bit_offset,
2260 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
2261 return false;
2263 } break;
2265 case Value::ValueType::FileAddress:
2266 case Value::ValueType::LoadAddress:
2267 case Value::ValueType::HostAddress:
2268 if (error_ptr) {
2269 error_ptr->SetErrorStringWithFormat(
2270 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
2271 ", bit_offset = %" PRIu64 ") from an address value.",
2272 piece_bit_size, piece_bit_offset);
2274 return false;
2277 break;
2279 // OPCODE: DW_OP_implicit_value
2280 // OPERANDS: 2
2281 // ULEB128 size of the value block in bytes
2282 // uint8_t* block bytes encoding value in target's memory
2283 // representation
2284 // DESCRIPTION: Value is immediately stored in block in the debug info with
2285 // the memory representation of the target.
2286 case DW_OP_implicit_value: {
2287 dwarf4_location_description_kind = Implicit;
2289 const uint32_t len = opcodes.GetULEB128(&offset);
2290 const void *data = opcodes.GetData(&offset, len);
2292 if (!data) {
2293 LLDB_LOG(log, "Evaluate_DW_OP_implicit_value: could not be read data");
2294 LLDB_ERRORF(error_ptr, "Could not evaluate %s.",
2295 DW_OP_value_to_name(op));
2296 return false;
2299 Value result(data, len);
2300 stack.push_back(result);
2301 break;
2304 case DW_OP_implicit_pointer: {
2305 dwarf4_location_description_kind = Implicit;
2306 LLDB_ERRORF(error_ptr, "Could not evaluate %s.", DW_OP_value_to_name(op));
2307 return false;
2310 // OPCODE: DW_OP_push_object_address
2311 // OPERANDS: none
2312 // DESCRIPTION: Pushes the address of the object currently being
2313 // evaluated as part of evaluation of a user presented expression. This
2314 // object may correspond to an independent variable described by its own
2315 // DIE or it may be a component of an array, structure, or class whose
2316 // address has been dynamically determined by an earlier step during user
2317 // expression evaluation.
2318 case DW_OP_push_object_address:
2319 if (object_address_ptr)
2320 stack.push_back(*object_address_ptr);
2321 else {
2322 if (error_ptr)
2323 error_ptr->SetErrorString("DW_OP_push_object_address used without "
2324 "specifying an object address");
2325 return false;
2327 break;
2329 // OPCODE: DW_OP_call2
2330 // OPERANDS:
2331 // uint16_t compile unit relative offset of a DIE
2332 // DESCRIPTION: Performs subroutine calls during evaluation
2333 // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2334 // debugging information entry in the current compilation unit.
2336 // Operand interpretation is exactly like that for DW_FORM_ref2.
2338 // This operation transfers control of DWARF expression evaluation to the
2339 // DW_AT_location attribute of the referenced DIE. If there is no such
2340 // attribute, then there is no effect. Execution of the DWARF expression of
2341 // a DW_AT_location attribute may add to and/or remove from values on the
2342 // stack. Execution returns to the point following the call when the end of
2343 // the attribute is reached. Values on the stack at the time of the call
2344 // may be used as parameters by the called expression and values left on
2345 // the stack by the called expression may be used as return values by prior
2346 // agreement between the calling and called expressions.
2347 case DW_OP_call2:
2348 if (error_ptr)
2349 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2.");
2350 return false;
2351 // OPCODE: DW_OP_call4
2352 // OPERANDS: 1
2353 // uint32_t compile unit relative offset of a DIE
2354 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2355 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2356 // a debugging information entry in the current compilation unit.
2358 // Operand interpretation DW_OP_call4 is exactly like that for
2359 // DW_FORM_ref4.
2361 // This operation transfers control of DWARF expression evaluation to the
2362 // DW_AT_location attribute of the referenced DIE. If there is no such
2363 // attribute, then there is no effect. Execution of the DWARF expression of
2364 // a DW_AT_location attribute may add to and/or remove from values on the
2365 // stack. Execution returns to the point following the call when the end of
2366 // the attribute is reached. Values on the stack at the time of the call
2367 // may be used as parameters by the called expression and values left on
2368 // the stack by the called expression may be used as return values by prior
2369 // agreement between the calling and called expressions.
2370 case DW_OP_call4:
2371 if (error_ptr)
2372 error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4.");
2373 return false;
2375 // OPCODE: DW_OP_stack_value
2376 // OPERANDS: None
2377 // DESCRIPTION: Specifies that the object does not exist in memory but
2378 // rather is a constant value. The value from the top of the stack is the
2379 // value to be used. This is the actual object value and not the location.
2380 case DW_OP_stack_value:
2381 dwarf4_location_description_kind = Implicit;
2382 if (stack.empty()) {
2383 if (error_ptr)
2384 error_ptr->SetErrorString(
2385 "Expression stack needs at least 1 item for DW_OP_stack_value.");
2386 return false;
2388 stack.back().SetValueType(Value::ValueType::Scalar);
2389 break;
2391 // OPCODE: DW_OP_convert
2392 // OPERANDS: 1
2393 // A ULEB128 that is either a DIE offset of a
2394 // DW_TAG_base_type or 0 for the generic (pointer-sized) type.
2396 // DESCRIPTION: Pop the top stack element, convert it to a
2397 // different type, and push the result.
2398 case DW_OP_convert: {
2399 if (stack.size() < 1) {
2400 if (error_ptr)
2401 error_ptr->SetErrorString(
2402 "Expression stack needs at least 1 item for DW_OP_convert.");
2403 return false;
2405 const uint64_t die_offset = opcodes.GetULEB128(&offset);
2406 uint64_t bit_size;
2407 bool sign;
2408 if (die_offset == 0) {
2409 // The generic type has the size of an address on the target
2410 // machine and an unspecified signedness. Scalar has no
2411 // "unspecified signedness", so we use unsigned types.
2412 if (!module_sp) {
2413 if (error_ptr)
2414 error_ptr->SetErrorString("No module");
2415 return false;
2417 sign = false;
2418 bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8;
2419 if (!bit_size) {
2420 if (error_ptr)
2421 error_ptr->SetErrorString("unspecified architecture");
2422 return false;
2424 } else {
2425 // Retrieve the type DIE that the value is being converted to. This
2426 // offset is compile unit relative so we need to fix it up.
2427 const uint64_t abs_die_offset = die_offset + dwarf_cu->GetOffset();
2428 // FIXME: the constness has annoying ripple effects.
2429 DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(abs_die_offset);
2430 if (!die) {
2431 if (error_ptr)
2432 error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE");
2433 return false;
2435 uint64_t encoding =
2436 die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user);
2437 bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2438 if (!bit_size)
2439 bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0);
2440 if (!bit_size) {
2441 if (error_ptr)
2442 error_ptr->SetErrorString("Unsupported type size in DW_OP_convert");
2443 return false;
2445 switch (encoding) {
2446 case DW_ATE_signed:
2447 case DW_ATE_signed_char:
2448 sign = true;
2449 break;
2450 case DW_ATE_unsigned:
2451 case DW_ATE_unsigned_char:
2452 sign = false;
2453 break;
2454 default:
2455 if (error_ptr)
2456 error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert");
2457 return false;
2460 Scalar &top = stack.back().ResolveValue(exe_ctx);
2461 top.TruncOrExtendTo(bit_size, sign);
2462 break;
2465 // OPCODE: DW_OP_call_frame_cfa
2466 // OPERANDS: None
2467 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2468 // the canonical frame address consistent with the call frame information
2469 // located in .debug_frame (or in the FDEs of the eh_frame section).
2470 case DW_OP_call_frame_cfa:
2471 if (frame) {
2472 // Note that we don't have to parse FDEs because this DWARF expression
2473 // is commonly evaluated with a valid stack frame.
2474 StackID id = frame->GetStackID();
2475 addr_t cfa = id.GetCallFrameAddress();
2476 if (cfa != LLDB_INVALID_ADDRESS) {
2477 stack.push_back(Scalar(cfa));
2478 stack.back().SetValueType(Value::ValueType::LoadAddress);
2479 } else if (error_ptr)
2480 error_ptr->SetErrorString("Stack frame does not include a canonical "
2481 "frame address for DW_OP_call_frame_cfa "
2482 "opcode.");
2483 } else {
2484 if (error_ptr)
2485 error_ptr->SetErrorString("Invalid stack frame in context for "
2486 "DW_OP_call_frame_cfa opcode.");
2487 return false;
2489 break;
2491 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2492 // opcode, DW_OP_GNU_push_tls_address)
2493 // OPERANDS: none
2494 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2495 // an address in the current thread's thread-local storage block, and
2496 // pushes it on the stack.
2497 case DW_OP_form_tls_address:
2498 case DW_OP_GNU_push_tls_address: {
2499 if (stack.size() < 1) {
2500 if (error_ptr) {
2501 if (op == DW_OP_form_tls_address)
2502 error_ptr->SetErrorString(
2503 "DW_OP_form_tls_address needs an argument.");
2504 else
2505 error_ptr->SetErrorString(
2506 "DW_OP_GNU_push_tls_address needs an argument.");
2508 return false;
2511 if (!exe_ctx || !module_sp) {
2512 if (error_ptr)
2513 error_ptr->SetErrorString("No context to evaluate TLS within.");
2514 return false;
2517 Thread *thread = exe_ctx->GetThreadPtr();
2518 if (!thread) {
2519 if (error_ptr)
2520 error_ptr->SetErrorString("No thread to evaluate TLS within.");
2521 return false;
2524 // Lookup the TLS block address for this thread and module.
2525 const addr_t tls_file_addr =
2526 stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2527 const addr_t tls_load_addr =
2528 thread->GetThreadLocalData(module_sp, tls_file_addr);
2530 if (tls_load_addr == LLDB_INVALID_ADDRESS) {
2531 if (error_ptr)
2532 error_ptr->SetErrorString(
2533 "No TLS data currently exists for this thread.");
2534 return false;
2537 stack.back().GetScalar() = tls_load_addr;
2538 stack.back().SetValueType(Value::ValueType::LoadAddress);
2539 } break;
2541 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
2542 // OPERANDS: 1
2543 // ULEB128: index to the .debug_addr section
2544 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2545 // section with the base address specified by the DW_AT_addr_base attribute
2546 // and the 0 based index is the ULEB128 encoded index.
2547 case DW_OP_addrx:
2548 case DW_OP_GNU_addr_index: {
2549 if (!dwarf_cu) {
2550 if (error_ptr)
2551 error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a "
2552 "compile unit being specified");
2553 return false;
2555 uint64_t index = opcodes.GetULEB128(&offset);
2556 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2557 stack.push_back(Scalar(value));
2558 if (target &&
2559 target->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32) {
2560 // wasm file sections aren't mapped into memory, therefore addresses can
2561 // never point into a file section and are always LoadAddresses.
2562 stack.back().SetValueType(Value::ValueType::LoadAddress);
2563 } else {
2564 stack.back().SetValueType(Value::ValueType::FileAddress);
2566 } break;
2568 // OPCODE: DW_OP_GNU_const_index
2569 // OPERANDS: 1
2570 // ULEB128: index to the .debug_addr section
2571 // DESCRIPTION: Pushes an constant with the size of a machine address to
2572 // the stack from the .debug_addr section with the base address specified
2573 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2574 // encoded index.
2575 case DW_OP_GNU_const_index: {
2576 if (!dwarf_cu) {
2577 if (error_ptr)
2578 error_ptr->SetErrorString("DW_OP_GNU_const_index found without a "
2579 "compile unit being specified");
2580 return false;
2582 uint64_t index = opcodes.GetULEB128(&offset);
2583 lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2584 stack.push_back(Scalar(value));
2585 } break;
2587 case DW_OP_GNU_entry_value:
2588 case DW_OP_entry_value: {
2589 if (!Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, opcodes, offset,
2590 error_ptr, log)) {
2591 LLDB_ERRORF(error_ptr, "Could not evaluate %s.",
2592 DW_OP_value_to_name(op));
2593 return false;
2595 break;
2598 default:
2599 if (dwarf_cu) {
2600 if (dwarf_cu->GetSymbolFileDWARF().ParseVendorDWARFOpcode(
2601 op, opcodes, offset, stack)) {
2602 break;
2605 if (error_ptr)
2606 error_ptr->SetErrorStringWithFormatv(
2607 "Unhandled opcode {0} in DWARFExpression", LocationAtom(op));
2608 return false;
2612 if (stack.empty()) {
2613 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2614 // or DW_OP_bit_piece opcodes
2615 if (pieces.GetBuffer().GetByteSize()) {
2616 result = pieces;
2617 return true;
2619 if (error_ptr)
2620 error_ptr->SetErrorString("Stack empty after evaluation.");
2621 return false;
2624 UpdateValueTypeFromLocationDescription(
2625 log, dwarf_cu, dwarf4_location_description_kind, &stack.back());
2627 if (log && log->GetVerbose()) {
2628 size_t count = stack.size();
2629 LLDB_LOGF(log,
2630 "Stack after operation has %" PRIu64 " values:", (uint64_t)count);
2631 for (size_t i = 0; i < count; ++i) {
2632 StreamString new_value;
2633 new_value.Printf("[%" PRIu64 "]", (uint64_t)i);
2634 stack[i].Dump(&new_value);
2635 LLDB_LOGF(log, " %s", new_value.GetData());
2638 result = stack.back();
2639 return true; // Return true on success
2642 bool DWARFExpression::ParseDWARFLocationList(
2643 const DWARFUnit *dwarf_cu, const DataExtractor &data,
2644 DWARFExpressionList *location_list) {
2645 location_list->Clear();
2646 std::unique_ptr<llvm::DWARFLocationTable> loctable_up =
2647 dwarf_cu->GetLocationTable(data);
2648 Log *log = GetLog(LLDBLog::Expressions);
2649 auto lookup_addr =
2650 [&](uint32_t index) -> std::optional<llvm::object::SectionedAddress> {
2651 addr_t address = dwarf_cu->ReadAddressFromDebugAddrSection(index);
2652 if (address == LLDB_INVALID_ADDRESS)
2653 return std::nullopt;
2654 return llvm::object::SectionedAddress{address};
2656 auto process_list = [&](llvm::Expected<llvm::DWARFLocationExpression> loc) {
2657 if (!loc) {
2658 LLDB_LOG_ERROR(log, loc.takeError(), "{0}");
2659 return true;
2661 auto buffer_sp =
2662 std::make_shared<DataBufferHeap>(loc->Expr.data(), loc->Expr.size());
2663 DWARFExpression expr = DWARFExpression(DataExtractor(
2664 buffer_sp, data.GetByteOrder(), data.GetAddressByteSize()));
2665 location_list->AddExpression(loc->Range->LowPC, loc->Range->HighPC, expr);
2666 return true;
2668 llvm::Error error = loctable_up->visitAbsoluteLocationList(
2669 0, llvm::object::SectionedAddress{dwarf_cu->GetBaseAddress()},
2670 lookup_addr, process_list);
2671 location_list->Sort();
2672 if (error) {
2673 LLDB_LOG_ERROR(log, std::move(error), "{0}");
2674 return false;
2676 return true;
2679 bool DWARFExpression::MatchesOperand(
2680 StackFrame &frame, const Instruction::Operand &operand) const {
2681 using namespace OperandMatchers;
2683 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
2684 if (!reg_ctx_sp) {
2685 return false;
2688 DataExtractor opcodes(m_data);
2690 lldb::offset_t op_offset = 0;
2691 uint8_t opcode = opcodes.GetU8(&op_offset);
2693 if (opcode == DW_OP_fbreg) {
2694 int64_t offset = opcodes.GetSLEB128(&op_offset);
2696 DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr);
2697 if (!fb_expr) {
2698 return false;
2701 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
2702 return fb_expr->MatchesOperand(frame, child);
2705 if (!offset &&
2706 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2707 recurse)(operand)) {
2708 return true;
2711 return MatchUnaryOp(
2712 MatchOpType(Instruction::Operand::Type::Dereference),
2713 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2714 MatchImmOp(offset), recurse))(operand);
2717 bool dereference = false;
2718 const RegisterInfo *reg = nullptr;
2719 int64_t offset = 0;
2721 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
2722 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
2723 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
2724 offset = opcodes.GetSLEB128(&op_offset);
2725 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
2726 } else if (opcode == DW_OP_regx) {
2727 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2728 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2729 } else if (opcode == DW_OP_bregx) {
2730 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2731 offset = opcodes.GetSLEB128(&op_offset);
2732 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2733 } else {
2734 return false;
2737 if (!reg) {
2738 return false;
2741 if (dereference) {
2742 if (!offset &&
2743 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2744 MatchRegOp(*reg))(operand)) {
2745 return true;
2748 return MatchUnaryOp(
2749 MatchOpType(Instruction::Operand::Type::Dereference),
2750 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2751 MatchRegOp(*reg),
2752 MatchImmOp(offset)))(operand);
2753 } else {
2754 return MatchRegOp(*reg)(operand);