1 //===-- DWARFExpression.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Expression/DWARFExpression.h"
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"
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
) {}
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
)
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
,
74 auto *MCRegInfo
= abi
? &abi
->GetMCRegisterInfo() : nullptr;
75 auto GetRegName
= [&MCRegInfo
](uint64_t DwarfRegNum
,
76 bool IsEH
) -> llvm::StringRef
{
79 if (std::optional
<unsigned> LLVMRegNum
=
80 MCRegInfo
->getLLVMRegNum(DwarfRegNum
, IsEH
))
81 if (const char *RegName
= MCRegInfo
->getName(*LLVMRegNum
))
82 return llvm::StringRef(RegName
);
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
,
102 if (reg_ctx
== nullptr) {
104 error_ptr
->SetErrorString("No register context in frame.\n");
106 uint32_t native_reg
=
107 reg_ctx
->ConvertRegisterKindToRegisterNumber(reg_kind
, reg_num
);
108 if (native_reg
== LLDB_INVALID_REGNUM
) {
110 error_ptr
->SetErrorStringWithFormat("Unable to convert register "
111 "kind=%u reg_num=%u to a native "
112 "register number.\n",
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
));
127 // If we get this error, then we need to implement a value buffer in
128 // the dwarf expression evaluation function...
130 error_ptr
->SetErrorStringWithFormat(
131 "register %s can't be converted to a scalar value",
136 error_ptr
->SetErrorStringWithFormat("register %s is not available",
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
;
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
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
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)
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)
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
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
337 uint64_t block_len
= data
.Skip_LEB128(&offset
);
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
;
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
,
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
);
370 return dwarf_cu
->ReadAddressFromDebugAddrSection(index
);
374 const offset_t op_arg_size
=
375 GetOpcodeDataSize(m_data
, offset
, op
, dwarf_cu
);
376 if (op_arg_size
== LLDB_INVALID_OFFSET
) {
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
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
)
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());
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
)
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());
434 const offset_t op_arg_size
=
435 GetOpcodeDataSize(m_data
, offset
, op
, dwarf_cu
);
436 if (op_arg_size
== LLDB_INVALID_OFFSET
)
438 offset
+= op_arg_size
;
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
)
451 const offset_t op_arg_size
=
452 GetOpcodeDataSize(m_data
, offset
, op
, dwarf_cu
);
453 if (op_arg_size
== LLDB_INVALID_OFFSET
)
455 offset
+= op_arg_size
;
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
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;
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
);
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
);
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
)
516 // Replace the address in the new buffer
517 if (encoder
.PutUnsigned(const_offset
, const_byte_size
,
518 linked_file_addr
) == UINT32_MAX
)
531 const offset_t op_arg_size
=
532 GetOpcodeDataSize(m_data
, offset
, op
, dwarf_cu
);
533 if (op_arg_size
== LLDB_INVALID_OFFSET
)
536 offset
+= op_arg_size
;
540 m_data
.SetData(encoder
.GetDataBuffer());
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) {
560 // /* "x" gets optimized out. */
562 // /* The location of "x" here is: DW_OP_entry_value($reg2). */
570 // * The callsite information emitted here is:
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)
581 // * DW_TAG_call_site
582 // * DW_AT_return_pc ... (for "child(sink, 456);")
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");
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");
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.
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
622 if (return_pc
== LLDB_INVALID_ADDRESS
) {
623 return_pc
= parent_frame
->GetFrameCodeAddress().GetLoadAddress(&target
);
625 "Evaluate_DW_OP_entry_value: immediate ancestor with pc = {0:x}",
629 // If we've found an inlined frame, skip it (these have no call site
631 if (parent_frame
->IsInlined())
634 // We've found the first non-inlined parent frame.
637 if (!parent_frame
|| !parent_frame
->GetRegisterContext()) {
638 LLDB_LOG(log
, "Evaluate_DW_OP_entry_value: no parent frame with reg ctx");
642 Function
*parent_func
=
643 parent_frame
->GetSymbolContext(eSymbolContextFunction
).function
;
645 LLDB_LOG(log
, "Evaluate_DW_OP_entry_value: no parent function");
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
;
654 LLDB_LOG(log
, "Evaluate_DW_OP_entry_value: no current function");
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
);
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());
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");
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();
691 LLDB_LOG(log
, "Evaluate_DW_OP_entry_value: no unambiguous edge from parent "
692 "to current function");
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
);
702 LLDB_LOG(log
, "Evaluate_DW_OP_entry_value: subexpr could not be read");
706 const CallSiteParameter
*matched_param
= nullptr;
707 for (const CallSiteParameter
¶m
: call_edge
->GetCallSiteParameters()) {
708 DataExtractor param_subexpr_extractor
;
709 if (!param
.LocationInCallee
.GetExpressionData(param_subexpr_extractor
))
711 lldb::offset_t param_subexpr_offset
= 0;
712 const void *param_subexpr_data
=
713 param_subexpr_extractor
.GetData(¶m_subexpr_offset
, subexpr_len
);
714 if (!param_subexpr_data
||
715 param_subexpr_extractor
.BytesLeft(param_subexpr_offset
) != 0)
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
= ¶m
;
730 if (!matched_param
) {
732 "Evaluate_DW_OP_entry_value: no matching call site param found");
736 // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
737 // subexpresion whenever llvm does.
739 const DWARFExpressionList
¶m_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
)) {
746 "Evaluate_DW_OP_entry_value: call site param evaluation failed");
750 stack
.push_back(result
);
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
{
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";
779 LLDB_LOGF(log
, log_msg
, "Empty");
782 LLDB_LOGF(log
, log_msg
, "Memory");
783 if (value
->GetValueType() == Value::ValueType::Scalar
)
784 value
->SetValueType(Value::ValueType::LoadAddress
);
787 LLDB_LOGF(log
, log_msg
, "Register");
788 value
->SetValueType(Value::ValueType::Scalar
);
791 LLDB_LOGF(log
, log_msg
, "Implicit");
792 if (value
->GetValueType() == Value::ValueType::LoadAddress
)
793 value
->SetValueType(Value::ValueType::Scalar
);
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
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) {
823 error_ptr
->SetErrorStringWithFormat(
824 "need module to resolve file address for %s", dw_op_type
);
828 if (!module_sp
->ResolveFileAddress(file_addr
, so_addr
)) {
830 error_ptr
->SetErrorString("failed to resolve file address in module");
834 addr_t load_addr
= so_addr
.GetLoadAddress(exe_ctx
->GetTargetPtr());
836 if (load_addr
== LLDB_INVALID_ADDRESS
&&
837 (check_sectionoffset
&& !so_addr
.IsSectionOffset())) {
839 error_ptr
->SetErrorString("failed to resolve load address");
846 /// Helper function to move common code used to load sized data from a uint8_t
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;
861 return addr_data
.GetMaxU64(&addr_data_offset
, size
);
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) {
875 error_ptr
->SetErrorString(
876 "no location, value may have been optimized out");
879 std::vector
<Value
> stack
;
881 Process
*process
= nullptr;
882 StackFrame
*frame
= nullptr;
883 Target
*target
= nullptr;
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;
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
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
),
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:",
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
));
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.
943 stack
.push_back(Scalar(opcodes
.GetAddress(&offset
)));
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
);
950 stack
.back().SetValueType(Value::ValueType::FileAddress
);
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
962 // case DW_OP_addr_sect_offset4:
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)
973 // // We successfully resolve a file address to a load
975 // stack.push_back(load_addr);
982 // error_ptr->SetErrorStringWithFormat ("Section %s in
983 // %s is not currently loaded.\n",
984 // sect->GetName().AsCString(),
985 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
991 // OPCODE: DW_OP_deref
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
1000 error_ptr
->SetErrorString("Expression stack empty for DW_OP_deref.");
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();
1008 ::memcpy(&ptr
, src
, sizeof(void *));
1009 stack
.back().GetScalar() = ptr
;
1010 stack
.back().ClearContext();
1012 case Value::ValueType::FileAddress
: {
1013 auto file_addr
= stack
.back().GetScalar().ULongLong(
1014 LLDB_INVALID_ADDRESS
);
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
)
1023 stack
.back().GetScalar() = *maybe_load_addr
;
1024 // Fall through to load address promotion code below.
1027 case Value::ValueType::Scalar
:
1028 // Promote Scalar to LoadAddress and fall through.
1029 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
1031 case Value::ValueType::LoadAddress
:
1034 lldb::addr_t pointer_addr
=
1035 stack
.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS
);
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();
1046 error_ptr
->SetErrorStringWithFormat(
1047 "Failed to dereference pointer from 0x%" PRIx64
1048 " for DW_OP_deref: %s\n",
1049 pointer_addr
, error
.AsCString());
1054 error_ptr
->SetErrorString("NULL process for DW_OP_deref.\n");
1059 error_ptr
->SetErrorString(
1060 "NULL execution context for DW_OP_deref.\n");
1065 case Value::ValueType::Invalid
:
1067 error_ptr
->SetErrorString("Invalid value type for DW_OP_deref.\n");
1073 // OPCODE: DW_OP_deref_size
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()) {
1088 error_ptr
->SetErrorString(
1089 "Expression stack empty for DW_OP_deref_size.");
1092 uint8_t size
= opcodes
.GetU8(&offset
);
1095 error_ptr
->SetErrorStringWithFormat(
1096 "Invalid address size for DW_OP_deref_size: %d\n",
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();
1105 ::memcpy(&ptr
, src
, sizeof(void *));
1106 // I can't decide whether the size operand should apply to the bytes in
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.
1118 ptr
= ptr
& 0xffffff;
1121 ptr
= ptr
& 0xffffffff;
1123 // the casts are added to work around the case where intptr_t is a 32
1125 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1128 ptr
= (intptr_t)ptr
& 0xffffffffffULL
;
1131 ptr
= (intptr_t)ptr
& 0xffffffffffffULL
;
1134 ptr
= (intptr_t)ptr
& 0xffffffffffffffULL
;
1139 stack
.back().GetScalar() = ptr
;
1140 stack
.back().ClearContext();
1142 case Value::ValueType::FileAddress
: {
1144 stack
.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS
);
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
)
1154 addr_t load_addr
= *maybe_load_addr
;
1156 if (load_addr
== LLDB_INVALID_ADDRESS
&& so_addr
.IsSectionOffset()) {
1157 uint8_t addr_bytes
[8];
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();
1171 error_ptr
->SetErrorStringWithFormat(
1172 "Failed to dereference pointer for DW_OP_deref_size: "
1178 stack
.back().GetScalar() = load_addr
;
1179 // Fall through to load address promotion code below.
1183 case Value::ValueType::Scalar
:
1184 case Value::ValueType::LoadAddress
:
1187 lldb::addr_t pointer_addr
=
1188 stack
.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS
);
1189 uint8_t addr_bytes
[sizeof(lldb::addr_t
)];
1191 if (process
->ReadMemory(pointer_addr
, &addr_bytes
, size
, error
) ==
1194 stack
.back().GetScalar() =
1195 DerefSizeExtractDataHelper(addr_bytes
, sizeof(addr_bytes
),
1196 process
->GetByteOrder(), size
);
1197 stack
.back().ClearContext();
1200 error_ptr
->SetErrorStringWithFormat(
1201 "Failed to dereference pointer from 0x%" PRIx64
1202 " for DW_OP_deref: %s\n",
1203 pointer_addr
, error
.AsCString());
1208 error_ptr
->SetErrorString("NULL process for DW_OP_deref_size.\n");
1213 error_ptr
->SetErrorString(
1214 "NULL execution context for DW_OP_deref_size.\n");
1219 case Value::ValueType::Invalid
:
1221 error_ptr
->SetErrorString("Invalid value for DW_OP_deref_size.\n");
1227 // OPCODE: DW_OP_xderef_size
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
:
1244 error_ptr
->SetErrorString("Unimplemented opcode: DW_OP_xderef_size.");
1246 // OPCODE: DW_OP_xderef
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
1258 error_ptr
->SetErrorString("Unimplemented opcode: DW_OP_xderef.");
1261 // All DW_OP_constXXX opcodes have a single operand as noted below:
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
1275 stack
.push_back(to_generic(opcodes
.GetU8(&offset
)));
1278 stack
.push_back(to_generic((int8_t)opcodes
.GetU8(&offset
)));
1281 stack
.push_back(to_generic(opcodes
.GetU16(&offset
)));
1284 stack
.push_back(to_generic((int16_t)opcodes
.GetU16(&offset
)));
1287 stack
.push_back(to_generic(opcodes
.GetU32(&offset
)));
1290 stack
.push_back(to_generic((int32_t)opcodes
.GetU32(&offset
)));
1293 stack
.push_back(to_generic(opcodes
.GetU64(&offset
)));
1296 stack
.push_back(to_generic((int64_t)opcodes
.GetU64(&offset
)));
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.
1301 stack
.push_back(Scalar(opcodes
.GetULEB128(&offset
)));
1304 stack
.push_back(Scalar(opcodes
.GetSLEB128(&offset
)));
1307 // OPCODE: DW_OP_dup
1309 // DESCRIPTION: duplicates the value at the top of the stack
1311 if (stack
.empty()) {
1313 error_ptr
->SetErrorString("Expression stack empty for DW_OP_dup.");
1316 stack
.push_back(stack
.back());
1319 // OPCODE: DW_OP_drop
1321 // DESCRIPTION: pops the value at the top of the stack
1323 if (stack
.empty()) {
1325 error_ptr
->SetErrorString("Expression stack empty for DW_OP_drop.");
1331 // OPCODE: DW_OP_over
1333 // DESCRIPTION: Duplicates the entry currently second in the stack at
1334 // the top of the stack.
1336 if (stack
.size() < 2) {
1338 error_ptr
->SetErrorString(
1339 "Expression stack needs at least 2 items for DW_OP_over.");
1342 stack
.push_back(stack
[stack
.size() - 2]);
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
1350 uint8_t pick_idx
= opcodes
.GetU8(&offset
);
1351 if (pick_idx
< stack
.size())
1352 stack
.push_back(stack
[stack
.size() - 1 - pick_idx
]);
1355 error_ptr
->SetErrorStringWithFormat(
1356 "Index %u out of range for DW_OP_pick.\n", pick_idx
);
1361 // OPCODE: DW_OP_swap
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
1367 if (stack
.size() < 2) {
1369 error_ptr
->SetErrorString(
1370 "Expression stack needs at least 2 items for DW_OP_swap.");
1374 stack
.back() = stack
[stack
.size() - 2];
1375 stack
[stack
.size() - 2] = tmp
;
1379 // OPCODE: DW_OP_rot
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
1386 if (stack
.size() < 3) {
1388 error_ptr
->SetErrorString(
1389 "Expression stack needs at least 3 items for DW_OP_rot.");
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
;
1400 // OPCODE: DW_OP_abs
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.
1406 if (stack
.empty()) {
1408 error_ptr
->SetErrorString(
1409 "Expression stack needs at least 1 item for DW_OP_abs.");
1411 } else if (!stack
.back().ResolveValue(exe_ctx
).AbsoluteValue()) {
1413 error_ptr
->SetErrorString(
1414 "Failed to take the absolute value of the first stack item.");
1419 // OPCODE: DW_OP_and
1421 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1422 // operation on the two, and pushes the result.
1424 if (stack
.size() < 2) {
1426 error_ptr
->SetErrorString(
1427 "Expression stack needs at least 2 items for DW_OP_and.");
1432 stack
.back().ResolveValue(exe_ctx
) =
1433 stack
.back().ResolveValue(exe_ctx
) & tmp
.ResolveValue(exe_ctx
);
1437 // OPCODE: DW_OP_div
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
1443 if (stack
.size() < 2) {
1445 error_ptr
->SetErrorString(
1446 "Expression stack needs at least 2 items for DW_OP_div.");
1450 if (tmp
.ResolveValue(exe_ctx
).IsZero()) {
1452 error_ptr
->SetErrorString("Divide by zero.");
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()) {
1464 error_ptr
->SetErrorString("Divide failed.");
1471 // OPCODE: DW_OP_minus
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.
1476 if (stack
.size() < 2) {
1478 error_ptr
->SetErrorString(
1479 "Expression stack needs at least 2 items for DW_OP_minus.");
1484 stack
.back().ResolveValue(exe_ctx
) =
1485 stack
.back().ResolveValue(exe_ctx
) - tmp
.ResolveValue(exe_ctx
);
1489 // OPCODE: DW_OP_mod
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
1495 if (stack
.size() < 2) {
1497 error_ptr
->SetErrorString(
1498 "Expression stack needs at least 2 items for DW_OP_mod.");
1503 stack
.back().ResolveValue(exe_ctx
) =
1504 stack
.back().ResolveValue(exe_ctx
) % tmp
.ResolveValue(exe_ctx
);
1508 // OPCODE: DW_OP_mul
1510 // DESCRIPTION: pops the top two stack entries, multiplies them
1511 // together, and pushes the result.
1513 if (stack
.size() < 2) {
1515 error_ptr
->SetErrorString(
1516 "Expression stack needs at least 2 items for DW_OP_mul.");
1521 stack
.back().ResolveValue(exe_ctx
) =
1522 stack
.back().ResolveValue(exe_ctx
) * tmp
.ResolveValue(exe_ctx
);
1526 // OPCODE: DW_OP_neg
1528 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1530 if (stack
.empty()) {
1532 error_ptr
->SetErrorString(
1533 "Expression stack needs at least 1 item for DW_OP_neg.");
1536 if (!stack
.back().ResolveValue(exe_ctx
).UnaryNegate()) {
1538 error_ptr
->SetErrorString("Unary negate failed.");
1544 // OPCODE: DW_OP_not
1546 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1549 if (stack
.empty()) {
1551 error_ptr
->SetErrorString(
1552 "Expression stack needs at least 1 item for DW_OP_not.");
1555 if (!stack
.back().ResolveValue(exe_ctx
).OnesComplement()) {
1557 error_ptr
->SetErrorString("Logical NOT failed.");
1565 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1566 // operation on the two, and pushes the result.
1568 if (stack
.size() < 2) {
1570 error_ptr
->SetErrorString(
1571 "Expression stack needs at least 2 items for DW_OP_or.");
1576 stack
.back().ResolveValue(exe_ctx
) =
1577 stack
.back().ResolveValue(exe_ctx
) | tmp
.ResolveValue(exe_ctx
);
1581 // OPCODE: DW_OP_plus
1583 // DESCRIPTION: pops the top two stack entries, adds them together, and
1584 // pushes the result.
1586 if (stack
.size() < 2) {
1588 error_ptr
->SetErrorString(
1589 "Expression stack needs at least 2 items for DW_OP_plus.");
1594 stack
.back().GetScalar() += tmp
.GetScalar();
1598 // OPCODE: DW_OP_plus_uconst
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()) {
1605 error_ptr
->SetErrorString(
1606 "Expression stack needs at least 1 item for DW_OP_plus_uconst.");
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()) {
1614 error_ptr
->SetErrorString("DW_OP_plus_uconst failed.");
1620 // OPCODE: DW_OP_shl
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.
1626 if (stack
.size() < 2) {
1628 error_ptr
->SetErrorString(
1629 "Expression stack needs at least 2 items for DW_OP_shl.");
1634 stack
.back().ResolveValue(exe_ctx
) <<= tmp
.ResolveValue(exe_ctx
);
1638 // OPCODE: DW_OP_shr
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.
1644 if (stack
.size() < 2) {
1646 error_ptr
->SetErrorString(
1647 "Expression stack needs at least 2 items for DW_OP_shr.");
1652 if (!stack
.back().ResolveValue(exe_ctx
).ShiftRightLogical(
1653 tmp
.ResolveValue(exe_ctx
))) {
1655 error_ptr
->SetErrorString("DW_OP_shr failed.");
1661 // OPCODE: DW_OP_shra
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.
1668 if (stack
.size() < 2) {
1670 error_ptr
->SetErrorString(
1671 "Expression stack needs at least 2 items for DW_OP_shra.");
1676 stack
.back().ResolveValue(exe_ctx
) >>= tmp
.ResolveValue(exe_ctx
);
1680 // OPCODE: DW_OP_xor
1682 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1683 // exclusive-or operation on the two, and pushes the result.
1685 if (stack
.size() < 2) {
1687 error_ptr
->SetErrorString(
1688 "Expression stack needs at least 2 items for DW_OP_xor.");
1693 stack
.back().ResolveValue(exe_ctx
) =
1694 stack
.back().ResolveValue(exe_ctx
) ^ tmp
.ResolveValue(exe_ctx
);
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.
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
1710 if (new_offset
<= opcodes
.GetByteSize())
1711 offset
= new_offset
;
1714 error_ptr
->SetErrorStringWithFormatv(
1715 "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}", offset
,
1716 skip_offset
, opcodes
.GetByteSize());
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.
1729 if (stack
.empty()) {
1731 error_ptr
->SetErrorString(
1732 "Expression stack needs at least 1 item for DW_OP_bra.");
1737 int16_t bra_offset
= (int16_t)opcodes
.GetU16(&offset
);
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
1744 if (new_offset
<= opcodes
.GetByteSize())
1745 offset
= new_offset
;
1748 error_ptr
->SetErrorStringWithFormatv(
1749 "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}", offset
,
1750 bra_offset
, opcodes
.GetByteSize());
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.
1765 if (stack
.size() < 2) {
1767 error_ptr
->SetErrorString(
1768 "Expression stack needs at least 2 items for DW_OP_eq.");
1773 stack
.back().ResolveValue(exe_ctx
) =
1774 stack
.back().ResolveValue(exe_ctx
) == tmp
.ResolveValue(exe_ctx
);
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.
1786 if (stack
.size() < 2) {
1788 error_ptr
->SetErrorString(
1789 "Expression stack needs at least 2 items for DW_OP_ge.");
1794 stack
.back().ResolveValue(exe_ctx
) =
1795 stack
.back().ResolveValue(exe_ctx
) >= tmp
.ResolveValue(exe_ctx
);
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.
1807 if (stack
.size() < 2) {
1809 error_ptr
->SetErrorString(
1810 "Expression stack needs at least 2 items for DW_OP_gt.");
1815 stack
.back().ResolveValue(exe_ctx
) =
1816 stack
.back().ResolveValue(exe_ctx
) > tmp
.ResolveValue(exe_ctx
);
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.
1828 if (stack
.size() < 2) {
1830 error_ptr
->SetErrorString(
1831 "Expression stack needs at least 2 items for DW_OP_le.");
1836 stack
.back().ResolveValue(exe_ctx
) =
1837 stack
.back().ResolveValue(exe_ctx
) <= tmp
.ResolveValue(exe_ctx
);
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.
1849 if (stack
.size() < 2) {
1851 error_ptr
->SetErrorString(
1852 "Expression stack needs at least 2 items for DW_OP_lt.");
1857 stack
.back().ResolveValue(exe_ctx
) =
1858 stack
.back().ResolveValue(exe_ctx
) < tmp
.ResolveValue(exe_ctx
);
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.
1870 if (stack
.size() < 2) {
1872 error_ptr
->SetErrorString(
1873 "Expression stack needs at least 2 items for DW_OP_ne.");
1878 stack
.back().ResolveValue(exe_ctx
) =
1879 stack
.back().ResolveValue(exe_ctx
) != tmp
.ResolveValue(exe_ctx
);
1883 // OPCODE: DW_OP_litn
1885 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1886 // STACK RESULT: push the unsigned literal constant value onto the top
1920 stack
.push_back(to_generic(op
- DW_OP_lit0
));
1923 // OPCODE: DW_OP_regN
1925 // DESCRIPTION: Push the value in register n on the top of the stack.
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
);
1966 // OPCODE: DW_OP_regx
1968 // ULEB128 literal operand that encodes the register.
1969 // DESCRIPTION: Push the value in register on the top of the stack.
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
);
1979 // OPCODE: DW_OP_bregN
1981 // SLEB128 offset from register N
1982 // DESCRIPTION: Value is in memory at the address specified by register
1983 // N plus an offset.
2015 case DW_OP_breg31
: {
2016 reg_num
= op
- DW_OP_breg0
;
2018 if (ReadRegisterValueAsScalar(reg_ctx
, reg_kind
, reg_num
, error_ptr
,
2020 int64_t breg_offset
= opcodes
.GetSLEB128(&offset
);
2021 tmp
.ResolveValue(exe_ctx
) += (uint64_t)breg_offset
;
2023 stack
.push_back(tmp
);
2024 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
2028 // OPCODE: DW_OP_bregx
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.
2035 reg_num
= opcodes
.GetULEB128(&offset
);
2037 if (ReadRegisterValueAsScalar(reg_ctx
, reg_kind
, reg_num
, error_ptr
,
2039 int64_t breg_offset
= opcodes
.GetSLEB128(&offset
);
2040 tmp
.ResolveValue(exe_ctx
) += (uint64_t)breg_offset
;
2042 stack
.push_back(tmp
);
2043 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
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
);
2061 error_ptr
->SetErrorString(
2062 "Invalid stack frame in context for DW_OP_fbreg opcode.");
2067 error_ptr
->SetErrorString(
2068 "NULL execution context for DW_OP_fbreg.\n");
2074 // OPCODE: DW_OP_nop
2076 // DESCRIPTION: A place holder. It has no effect on the location stack
2077 // or any of its values.
2081 // OPCODE: DW_OP_piece
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.
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) {
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
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
);
2119 // Extract the current piece into "curr_piece"
2120 Value
curr_piece_source_value(stack
.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
:
2130 case Value::ValueType::LoadAddress
:
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
) {
2140 error_ptr
->SetErrorStringWithFormat(
2141 "failed to read memory DW_OP_piece(%" PRIu64
2142 ") from 0x%" PRIx64
,
2143 piece_byte_size
, load_addr
);
2148 error_ptr
->SetErrorStringWithFormat(
2149 "failed to resize the piece memory buffer for "
2150 "DW_OP_piece(%" PRIu64
")",
2157 case Value::ValueType::FileAddress
:
2158 case Value::ValueType::HostAddress
:
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
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
)) {
2180 error_ptr
->SetErrorStringWithFormat(
2181 "unable to extract %" PRIu64
" bytes from a %" PRIu64
2182 " byte scalar value.",
2184 (uint64_t)curr_piece_source_value
.GetScalar()
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
));
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
2204 if (pieces
.AppendDataToHostBuffer(curr_piece
) == 0) {
2206 error_ptr
->SetErrorString("failed to append piece data");
2210 // If this is the second or later piece there should be a value on
2212 if (pieces
.GetBuffer().GetByteSize() != op_piece_offset
) {
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());
2221 if (pieces
.AppendDataToHostBuffer(curr_piece
) == 0) {
2223 error_ptr
->SetErrorString("failed to append piece data");
2228 op_piece_offset
+= piece_byte_size
;
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
;
2239 error_ptr
->SetErrorString(
2240 "Expression stack needs at least 1 item for DW_OP_bit_piece.");
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
:
2252 case Value::ValueType::Scalar
: {
2253 if (!stack
.back().GetScalar().ExtractBitfield(piece_bit_size
,
2254 piece_bit_offset
)) {
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));
2265 case Value::ValueType::FileAddress
:
2266 case Value::ValueType::LoadAddress
:
2267 case Value::ValueType::HostAddress
:
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
);
2279 // OPCODE: DW_OP_implicit_value
2281 // ULEB128 size of the value block in bytes
2282 // uint8_t* block bytes encoding value in target's memory
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
);
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
));
2299 Value
result(data
, len
);
2300 stack
.push_back(result
);
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
));
2310 // OPCODE: DW_OP_push_object_address
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
);
2323 error_ptr
->SetErrorString("DW_OP_push_object_address used without "
2324 "specifying an object address");
2329 // OPCODE: DW_OP_call2
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.
2349 error_ptr
->SetErrorString("Unimplemented opcode DW_OP_call2.");
2351 // OPCODE: DW_OP_call4
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
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.
2372 error_ptr
->SetErrorString("Unimplemented opcode DW_OP_call4.");
2375 // OPCODE: DW_OP_stack_value
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()) {
2384 error_ptr
->SetErrorString(
2385 "Expression stack needs at least 1 item for DW_OP_stack_value.");
2388 stack
.back().SetValueType(Value::ValueType::Scalar
);
2391 // OPCODE: DW_OP_convert
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) {
2401 error_ptr
->SetErrorString(
2402 "Expression stack needs at least 1 item for DW_OP_convert.");
2405 const uint64_t die_offset
= opcodes
.GetULEB128(&offset
);
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.
2414 error_ptr
->SetErrorString("No module");
2418 bit_size
= module_sp
->GetArchitecture().GetAddressByteSize() * 8;
2421 error_ptr
->SetErrorString("unspecified architecture");
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
);
2432 error_ptr
->SetErrorString("Cannot resolve DW_OP_convert type DIE");
2436 die
.GetAttributeValueAsUnsigned(DW_AT_encoding
, DW_ATE_hi_user
);
2437 bit_size
= die
.GetAttributeValueAsUnsigned(DW_AT_byte_size
, 0) * 8;
2439 bit_size
= die
.GetAttributeValueAsUnsigned(DW_AT_bit_size
, 0);
2442 error_ptr
->SetErrorString("Unsupported type size in DW_OP_convert");
2447 case DW_ATE_signed_char
:
2450 case DW_ATE_unsigned
:
2451 case DW_ATE_unsigned_char
:
2456 error_ptr
->SetErrorString("Unsupported encoding in DW_OP_convert");
2460 Scalar
&top
= stack
.back().ResolveValue(exe_ctx
);
2461 top
.TruncOrExtendTo(bit_size
, sign
);
2465 // OPCODE: DW_OP_call_frame_cfa
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
:
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 "
2485 error_ptr
->SetErrorString("Invalid stack frame in context for "
2486 "DW_OP_call_frame_cfa opcode.");
2491 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2492 // opcode, DW_OP_GNU_push_tls_address)
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) {
2501 if (op
== DW_OP_form_tls_address
)
2502 error_ptr
->SetErrorString(
2503 "DW_OP_form_tls_address needs an argument.");
2505 error_ptr
->SetErrorString(
2506 "DW_OP_GNU_push_tls_address needs an argument.");
2511 if (!exe_ctx
|| !module_sp
) {
2513 error_ptr
->SetErrorString("No context to evaluate TLS within.");
2517 Thread
*thread
= exe_ctx
->GetThreadPtr();
2520 error_ptr
->SetErrorString("No thread to evaluate TLS within.");
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
) {
2532 error_ptr
->SetErrorString(
2533 "No TLS data currently exists for this thread.");
2537 stack
.back().GetScalar() = tls_load_addr
;
2538 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
2541 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
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.
2548 case DW_OP_GNU_addr_index
: {
2551 error_ptr
->SetErrorString("DW_OP_GNU_addr_index found without a "
2552 "compile unit being specified");
2555 uint64_t index
= opcodes
.GetULEB128(&offset
);
2556 lldb::addr_t value
= dwarf_cu
->ReadAddressFromDebugAddrSection(index
);
2557 stack
.push_back(Scalar(value
));
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
);
2564 stack
.back().SetValueType(Value::ValueType::FileAddress
);
2568 // OPCODE: DW_OP_GNU_const_index
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
2575 case DW_OP_GNU_const_index
: {
2578 error_ptr
->SetErrorString("DW_OP_GNU_const_index found without a "
2579 "compile unit being specified");
2582 uint64_t index
= opcodes
.GetULEB128(&offset
);
2583 lldb::addr_t value
= dwarf_cu
->ReadAddressFromDebugAddrSection(index
);
2584 stack
.push_back(Scalar(value
));
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
,
2591 LLDB_ERRORF(error_ptr
, "Could not evaluate %s.",
2592 DW_OP_value_to_name(op
));
2600 if (dwarf_cu
->GetSymbolFileDWARF().ParseVendorDWARFOpcode(
2601 op
, opcodes
, offset
, stack
)) {
2606 error_ptr
->SetErrorStringWithFormatv(
2607 "Unhandled opcode {0} in DWARFExpression", LocationAtom(op
));
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()) {
2620 error_ptr
->SetErrorString("Stack empty after evaluation.");
2624 UpdateValueTypeFromLocationDescription(
2625 log
, dwarf_cu
, dwarf4_location_description_kind
, &stack
.back());
2627 if (log
&& log
->GetVerbose()) {
2628 size_t count
= stack
.size();
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
);
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
) {
2658 LLDB_LOG_ERROR(log
, loc
.takeError(), "{0}");
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
);
2668 llvm::Error error
= loctable_up
->visitAbsoluteLocationList(
2669 0, llvm::object::SectionedAddress
{dwarf_cu
->GetBaseAddress()},
2670 lookup_addr
, process_list
);
2671 location_list
->Sort();
2673 LLDB_LOG_ERROR(log
, std::move(error
), "{0}");
2679 bool DWARFExpression::MatchesOperand(
2680 StackFrame
&frame
, const Instruction::Operand
&operand
) const {
2681 using namespace OperandMatchers
;
2683 RegisterContextSP reg_ctx_sp
= frame
.GetRegisterContext();
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);
2701 auto recurse
= [&frame
, fb_expr
](const Instruction::Operand
&child
) {
2702 return fb_expr
->MatchesOperand(frame
, child
);
2706 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference
),
2707 recurse
)(operand
)) {
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;
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
);
2743 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference
),
2744 MatchRegOp(*reg
))(operand
)) {
2748 return MatchUnaryOp(
2749 MatchOpType(Instruction::Operand::Type::Dereference
),
2750 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum
),
2752 MatchImmOp(offset
)))(operand
);
2754 return MatchRegOp(*reg
)(operand
);