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
;
97 static llvm::Error
ReadRegisterValueAsScalar(RegisterContext
*reg_ctx
,
98 lldb::RegisterKind reg_kind
,
99 uint32_t reg_num
, Value
&value
) {
100 if (reg_ctx
== nullptr)
101 return llvm::createStringError("no register context in frame");
103 const uint32_t native_reg
=
104 reg_ctx
->ConvertRegisterKindToRegisterNumber(reg_kind
, reg_num
);
105 if (native_reg
== LLDB_INVALID_REGNUM
)
106 return llvm::createStringError(
107 "unable to convert register kind=%u reg_num=%u to a native "
111 const RegisterInfo
*reg_info
= reg_ctx
->GetRegisterInfoAtIndex(native_reg
);
112 RegisterValue reg_value
;
113 if (reg_ctx
->ReadRegister(reg_info
, reg_value
)) {
114 if (reg_value
.GetScalarValue(value
.GetScalar())) {
115 value
.SetValueType(Value::ValueType::Scalar
);
116 value
.SetContext(Value::ContextType::RegisterInfo
,
117 const_cast<RegisterInfo
*>(reg_info
));
118 return llvm::Error::success();
121 // If we get this error, then we need to implement a value buffer in
122 // the dwarf expression evaluation function...
123 return llvm::createStringError(
124 "register %s can't be converted to a scalar value", reg_info
->name
);
127 return llvm::createStringError("register %s is not available",
131 /// Return the length in bytes of the set of operands for \p op. No guarantees
132 /// are made on the state of \p data after this call.
133 static lldb::offset_t
GetOpcodeDataSize(const DataExtractor
&data
,
134 const lldb::offset_t data_offset
,
136 const DWARFUnit
*dwarf_cu
) {
137 lldb::offset_t offset
= data_offset
;
140 case DW_OP_call_ref
: // 0x9a 1 address sized offset of DIE (DWARF3)
141 return data
.GetAddressByteSize();
143 // Opcodes with no arguments
144 case DW_OP_deref
: // 0x06
145 case DW_OP_dup
: // 0x12
146 case DW_OP_drop
: // 0x13
147 case DW_OP_over
: // 0x14
148 case DW_OP_swap
: // 0x16
149 case DW_OP_rot
: // 0x17
150 case DW_OP_xderef
: // 0x18
151 case DW_OP_abs
: // 0x19
152 case DW_OP_and
: // 0x1a
153 case DW_OP_div
: // 0x1b
154 case DW_OP_minus
: // 0x1c
155 case DW_OP_mod
: // 0x1d
156 case DW_OP_mul
: // 0x1e
157 case DW_OP_neg
: // 0x1f
158 case DW_OP_not
: // 0x20
159 case DW_OP_or
: // 0x21
160 case DW_OP_plus
: // 0x22
161 case DW_OP_shl
: // 0x24
162 case DW_OP_shr
: // 0x25
163 case DW_OP_shra
: // 0x26
164 case DW_OP_xor
: // 0x27
165 case DW_OP_eq
: // 0x29
166 case DW_OP_ge
: // 0x2a
167 case DW_OP_gt
: // 0x2b
168 case DW_OP_le
: // 0x2c
169 case DW_OP_lt
: // 0x2d
170 case DW_OP_ne
: // 0x2e
171 case DW_OP_lit0
: // 0x30
172 case DW_OP_lit1
: // 0x31
173 case DW_OP_lit2
: // 0x32
174 case DW_OP_lit3
: // 0x33
175 case DW_OP_lit4
: // 0x34
176 case DW_OP_lit5
: // 0x35
177 case DW_OP_lit6
: // 0x36
178 case DW_OP_lit7
: // 0x37
179 case DW_OP_lit8
: // 0x38
180 case DW_OP_lit9
: // 0x39
181 case DW_OP_lit10
: // 0x3A
182 case DW_OP_lit11
: // 0x3B
183 case DW_OP_lit12
: // 0x3C
184 case DW_OP_lit13
: // 0x3D
185 case DW_OP_lit14
: // 0x3E
186 case DW_OP_lit15
: // 0x3F
187 case DW_OP_lit16
: // 0x40
188 case DW_OP_lit17
: // 0x41
189 case DW_OP_lit18
: // 0x42
190 case DW_OP_lit19
: // 0x43
191 case DW_OP_lit20
: // 0x44
192 case DW_OP_lit21
: // 0x45
193 case DW_OP_lit22
: // 0x46
194 case DW_OP_lit23
: // 0x47
195 case DW_OP_lit24
: // 0x48
196 case DW_OP_lit25
: // 0x49
197 case DW_OP_lit26
: // 0x4A
198 case DW_OP_lit27
: // 0x4B
199 case DW_OP_lit28
: // 0x4C
200 case DW_OP_lit29
: // 0x4D
201 case DW_OP_lit30
: // 0x4E
202 case DW_OP_lit31
: // 0x4f
203 case DW_OP_reg0
: // 0x50
204 case DW_OP_reg1
: // 0x51
205 case DW_OP_reg2
: // 0x52
206 case DW_OP_reg3
: // 0x53
207 case DW_OP_reg4
: // 0x54
208 case DW_OP_reg5
: // 0x55
209 case DW_OP_reg6
: // 0x56
210 case DW_OP_reg7
: // 0x57
211 case DW_OP_reg8
: // 0x58
212 case DW_OP_reg9
: // 0x59
213 case DW_OP_reg10
: // 0x5A
214 case DW_OP_reg11
: // 0x5B
215 case DW_OP_reg12
: // 0x5C
216 case DW_OP_reg13
: // 0x5D
217 case DW_OP_reg14
: // 0x5E
218 case DW_OP_reg15
: // 0x5F
219 case DW_OP_reg16
: // 0x60
220 case DW_OP_reg17
: // 0x61
221 case DW_OP_reg18
: // 0x62
222 case DW_OP_reg19
: // 0x63
223 case DW_OP_reg20
: // 0x64
224 case DW_OP_reg21
: // 0x65
225 case DW_OP_reg22
: // 0x66
226 case DW_OP_reg23
: // 0x67
227 case DW_OP_reg24
: // 0x68
228 case DW_OP_reg25
: // 0x69
229 case DW_OP_reg26
: // 0x6A
230 case DW_OP_reg27
: // 0x6B
231 case DW_OP_reg28
: // 0x6C
232 case DW_OP_reg29
: // 0x6D
233 case DW_OP_reg30
: // 0x6E
234 case DW_OP_reg31
: // 0x6F
235 case DW_OP_nop
: // 0x96
236 case DW_OP_push_object_address
: // 0x97 DWARF3
237 case DW_OP_form_tls_address
: // 0x9b DWARF3
238 case DW_OP_call_frame_cfa
: // 0x9c DWARF3
239 case DW_OP_stack_value
: // 0x9f DWARF4
240 case DW_OP_GNU_push_tls_address
: // 0xe0 GNU extension
243 // Opcodes with a single 1 byte arguments
244 case DW_OP_const1u
: // 0x08 1 1-byte constant
245 case DW_OP_const1s
: // 0x09 1 1-byte constant
246 case DW_OP_pick
: // 0x15 1 1-byte stack index
247 case DW_OP_deref_size
: // 0x94 1 1-byte size of data retrieved
248 case DW_OP_xderef_size
: // 0x95 1 1-byte size of data retrieved
251 // Opcodes with a single 2 byte arguments
252 case DW_OP_const2u
: // 0x0a 1 2-byte constant
253 case DW_OP_const2s
: // 0x0b 1 2-byte constant
254 case DW_OP_skip
: // 0x2f 1 signed 2-byte constant
255 case DW_OP_bra
: // 0x28 1 signed 2-byte constant
256 case DW_OP_call2
: // 0x98 1 2-byte offset of DIE (DWARF3)
259 // Opcodes with a single 4 byte arguments
260 case DW_OP_const4u
: // 0x0c 1 4-byte constant
261 case DW_OP_const4s
: // 0x0d 1 4-byte constant
262 case DW_OP_call4
: // 0x99 1 4-byte offset of DIE (DWARF3)
265 // Opcodes with a single 8 byte arguments
266 case DW_OP_const8u
: // 0x0e 1 8-byte constant
267 case DW_OP_const8s
: // 0x0f 1 8-byte constant
270 // All opcodes that have a single ULEB (signed or unsigned) argument
271 case DW_OP_addrx
: // 0xa1 1 ULEB128 index
272 case DW_OP_constu
: // 0x10 1 ULEB128 constant
273 case DW_OP_consts
: // 0x11 1 SLEB128 constant
274 case DW_OP_plus_uconst
: // 0x23 1 ULEB128 addend
275 case DW_OP_breg0
: // 0x70 1 ULEB128 register
276 case DW_OP_breg1
: // 0x71 1 ULEB128 register
277 case DW_OP_breg2
: // 0x72 1 ULEB128 register
278 case DW_OP_breg3
: // 0x73 1 ULEB128 register
279 case DW_OP_breg4
: // 0x74 1 ULEB128 register
280 case DW_OP_breg5
: // 0x75 1 ULEB128 register
281 case DW_OP_breg6
: // 0x76 1 ULEB128 register
282 case DW_OP_breg7
: // 0x77 1 ULEB128 register
283 case DW_OP_breg8
: // 0x78 1 ULEB128 register
284 case DW_OP_breg9
: // 0x79 1 ULEB128 register
285 case DW_OP_breg10
: // 0x7a 1 ULEB128 register
286 case DW_OP_breg11
: // 0x7b 1 ULEB128 register
287 case DW_OP_breg12
: // 0x7c 1 ULEB128 register
288 case DW_OP_breg13
: // 0x7d 1 ULEB128 register
289 case DW_OP_breg14
: // 0x7e 1 ULEB128 register
290 case DW_OP_breg15
: // 0x7f 1 ULEB128 register
291 case DW_OP_breg16
: // 0x80 1 ULEB128 register
292 case DW_OP_breg17
: // 0x81 1 ULEB128 register
293 case DW_OP_breg18
: // 0x82 1 ULEB128 register
294 case DW_OP_breg19
: // 0x83 1 ULEB128 register
295 case DW_OP_breg20
: // 0x84 1 ULEB128 register
296 case DW_OP_breg21
: // 0x85 1 ULEB128 register
297 case DW_OP_breg22
: // 0x86 1 ULEB128 register
298 case DW_OP_breg23
: // 0x87 1 ULEB128 register
299 case DW_OP_breg24
: // 0x88 1 ULEB128 register
300 case DW_OP_breg25
: // 0x89 1 ULEB128 register
301 case DW_OP_breg26
: // 0x8a 1 ULEB128 register
302 case DW_OP_breg27
: // 0x8b 1 ULEB128 register
303 case DW_OP_breg28
: // 0x8c 1 ULEB128 register
304 case DW_OP_breg29
: // 0x8d 1 ULEB128 register
305 case DW_OP_breg30
: // 0x8e 1 ULEB128 register
306 case DW_OP_breg31
: // 0x8f 1 ULEB128 register
307 case DW_OP_regx
: // 0x90 1 ULEB128 register
308 case DW_OP_fbreg
: // 0x91 1 SLEB128 offset
309 case DW_OP_piece
: // 0x93 1 ULEB128 size of piece addressed
310 case DW_OP_GNU_addr_index
: // 0xfb 1 ULEB128 index
311 case DW_OP_GNU_const_index
: // 0xfc 1 ULEB128 index
312 data
.Skip_LEB128(&offset
);
313 return offset
- data_offset
;
315 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
316 case DW_OP_bregx
: // 0x92 2 ULEB128 register followed by SLEB128 offset
317 case DW_OP_bit_piece
: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
318 data
.Skip_LEB128(&offset
);
319 data
.Skip_LEB128(&offset
);
320 return offset
- data_offset
;
322 case DW_OP_implicit_value
: // 0x9e ULEB128 size followed by block of that size
325 uint64_t block_len
= data
.Skip_LEB128(&offset
);
327 return offset
- data_offset
;
330 case DW_OP_GNU_entry_value
:
331 case DW_OP_entry_value
: // 0xa3 ULEB128 size + variable-length block
333 uint64_t subexpr_len
= data
.GetULEB128(&offset
);
334 return (offset
- data_offset
) + subexpr_len
;
339 return LLDB_INVALID_OFFSET
;
341 return dwarf_cu
->GetSymbolFileDWARF().GetVendorDWARFOpcodeSize(
342 data
, data_offset
, op
);
346 llvm::Expected
<lldb::addr_t
>
347 DWARFExpression::GetLocation_DW_OP_addr(const DWARFUnit
*dwarf_cu
) const {
348 lldb::offset_t offset
= 0;
349 while (m_data
.ValidOffset(offset
)) {
350 const uint8_t op
= m_data
.GetU8(&offset
);
352 if (op
== DW_OP_addr
)
353 return m_data
.GetAddress(&offset
);
355 if (op
== DW_OP_GNU_addr_index
|| op
== DW_OP_addrx
) {
356 const uint64_t index
= m_data
.GetULEB128(&offset
);
358 return dwarf_cu
->ReadAddressFromDebugAddrSection(index
);
359 return llvm::createStringError("cannot evaluate %s without a DWARF unit",
360 DW_OP_value_to_name(op
));
363 const lldb::offset_t op_arg_size
=
364 GetOpcodeDataSize(m_data
, offset
, op
, dwarf_cu
);
365 if (op_arg_size
== LLDB_INVALID_OFFSET
)
366 return llvm::createStringError("cannot get opcode data size for %s",
367 DW_OP_value_to_name(op
));
369 offset
+= op_arg_size
;
372 return LLDB_INVALID_ADDRESS
;
375 bool DWARFExpression::Update_DW_OP_addr(const DWARFUnit
*dwarf_cu
,
376 lldb::addr_t file_addr
) {
377 lldb::offset_t offset
= 0;
378 while (m_data
.ValidOffset(offset
)) {
379 const uint8_t op
= m_data
.GetU8(&offset
);
381 if (op
== DW_OP_addr
) {
382 const uint32_t addr_byte_size
= m_data
.GetAddressByteSize();
383 // We have to make a copy of the data as we don't know if this data is
384 // from a read only memory mapped buffer, so we duplicate all of the data
385 // first, then modify it, and if all goes well, we then replace the data
386 // for this expression
388 // Make en encoder that contains a copy of the location expression data
389 // so we can write the address into the buffer using the correct byte
391 DataEncoder
encoder(m_data
.GetDataStart(), m_data
.GetByteSize(),
392 m_data
.GetByteOrder(), addr_byte_size
);
394 // Replace the address in the new buffer
395 if (encoder
.PutAddress(offset
, file_addr
) == UINT32_MAX
)
398 // All went well, so now we can reset the data using a shared pointer to
399 // the heap data so "m_data" will now correctly manage the heap data.
400 m_data
.SetData(encoder
.GetDataBuffer());
403 if (op
== DW_OP_addrx
) {
404 // Replace DW_OP_addrx with DW_OP_addr, since we can't modify the
405 // read-only debug_addr table.
406 // Subtract one to account for the opcode.
407 llvm::ArrayRef data_before_op
= m_data
.GetData().take_front(offset
- 1);
409 // Read the addrx index to determine how many bytes it needs.
410 const lldb::offset_t old_offset
= offset
;
411 m_data
.GetULEB128(&offset
);
412 if (old_offset
== offset
)
414 llvm::ArrayRef data_after_op
= m_data
.GetData().drop_front(offset
);
416 DataEncoder
encoder(m_data
.GetByteOrder(), m_data
.GetAddressByteSize());
417 encoder
.AppendData(data_before_op
);
418 encoder
.AppendU8(DW_OP_addr
);
419 encoder
.AppendAddress(file_addr
);
420 encoder
.AppendData(data_after_op
);
421 m_data
.SetData(encoder
.GetDataBuffer());
424 const lldb::offset_t op_arg_size
=
425 GetOpcodeDataSize(m_data
, offset
, op
, dwarf_cu
);
426 if (op_arg_size
== LLDB_INVALID_OFFSET
)
428 offset
+= op_arg_size
;
433 bool DWARFExpression::ContainsThreadLocalStorage(
434 const DWARFUnit
*dwarf_cu
) const {
435 lldb::offset_t offset
= 0;
436 while (m_data
.ValidOffset(offset
)) {
437 const uint8_t op
= m_data
.GetU8(&offset
);
439 if (op
== DW_OP_form_tls_address
|| op
== DW_OP_GNU_push_tls_address
)
441 const lldb::offset_t op_arg_size
=
442 GetOpcodeDataSize(m_data
, offset
, op
, dwarf_cu
);
443 if (op_arg_size
== LLDB_INVALID_OFFSET
)
445 offset
+= op_arg_size
;
449 bool DWARFExpression::LinkThreadLocalStorage(
450 const DWARFUnit
*dwarf_cu
,
451 std::function
<lldb::addr_t(lldb::addr_t file_addr
)> const
452 &link_address_callback
) {
453 const uint32_t addr_byte_size
= m_data
.GetAddressByteSize();
454 // We have to make a copy of the data as we don't know if this data is from a
455 // read only memory mapped buffer, so we duplicate all of the data first,
456 // then modify it, and if all goes well, we then replace the data for this
458 // Make en encoder that contains a copy of the location expression data so we
459 // can write the address into the buffer using the correct byte order.
460 DataEncoder
encoder(m_data
.GetDataStart(), m_data
.GetByteSize(),
461 m_data
.GetByteOrder(), addr_byte_size
);
463 lldb::offset_t offset
= 0;
464 lldb::offset_t const_offset
= 0;
465 lldb::addr_t const_value
= 0;
466 size_t const_byte_size
= 0;
467 while (m_data
.ValidOffset(offset
)) {
468 const uint8_t op
= m_data
.GetU8(&offset
);
470 bool decoded_data
= false;
473 // Remember the const offset in case we later have a
474 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
475 const_offset
= offset
;
476 const_value
= m_data
.GetU32(&offset
);
482 // Remember the const offset in case we later have a
483 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
484 const_offset
= offset
;
485 const_value
= m_data
.GetU64(&offset
);
490 case DW_OP_form_tls_address
:
491 case DW_OP_GNU_push_tls_address
:
492 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
493 // by a file address on the stack. We assume that DW_OP_const4u or
494 // DW_OP_const8u is used for these values, and we check that the last
495 // opcode we got before either of these was DW_OP_const4u or
496 // DW_OP_const8u. If so, then we can link the value accordingly. For
497 // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
498 // address of a structure that contains a function pointer, the pthread
499 // key and the offset into the data pointed to by the pthread key. So we
500 // must link this address and also set the module of this expression to
501 // the new_module_sp so we can resolve the file address correctly
502 if (const_byte_size
> 0) {
503 lldb::addr_t linked_file_addr
= link_address_callback(const_value
);
504 if (linked_file_addr
== LLDB_INVALID_ADDRESS
)
506 // Replace the address in the new buffer
507 if (encoder
.PutUnsigned(const_offset
, const_byte_size
,
508 linked_file_addr
) == UINT32_MAX
)
521 const lldb::offset_t op_arg_size
=
522 GetOpcodeDataSize(m_data
, offset
, op
, dwarf_cu
);
523 if (op_arg_size
== LLDB_INVALID_OFFSET
)
526 offset
+= op_arg_size
;
530 m_data
.SetData(encoder
.GetDataBuffer());
534 static llvm::Error
Evaluate_DW_OP_entry_value(std::vector
<Value
> &stack
,
535 ExecutionContext
*exe_ctx
,
536 RegisterContext
*reg_ctx
,
537 const DataExtractor
&opcodes
,
538 lldb::offset_t
&opcode_offset
,
540 // DW_OP_entry_value(sub-expr) describes the location a variable had upon
541 // function entry: this variable location is presumed to be optimized out at
542 // the current PC value. The caller of the function may have call site
543 // information that describes an alternate location for the variable (e.g. a
544 // constant literal, or a spilled stack value) in the parent frame.
546 // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
548 // void child(int &sink, int x) {
550 // /* "x" gets optimized out. */
552 // /* The location of "x" here is: DW_OP_entry_value($reg2). */
560 // * The callsite information emitted here is:
562 // * DW_TAG_call_site
563 // * DW_AT_return_pc ... (for "child(sink, 123);")
564 // * DW_TAG_call_site_parameter (for "sink")
565 // * DW_AT_location ($reg1)
566 // * DW_AT_call_value ($SP - 8)
567 // * DW_TAG_call_site_parameter (for "x")
568 // * DW_AT_location ($reg2)
569 // * DW_AT_call_value ($literal 123)
571 // * DW_TAG_call_site
572 // * DW_AT_return_pc ... (for "child(sink, 456);")
579 // When the program stops at "++sink" within `child`, the debugger determines
580 // the call site by analyzing the return address. Once the call site is found,
581 // the debugger determines which parameter is referenced by DW_OP_entry_value
582 // and evaluates the corresponding location for that parameter in `parent`.
584 // 1. Find the function which pushed the current frame onto the stack.
585 if ((!exe_ctx
|| !exe_ctx
->HasTargetScope()) || !reg_ctx
) {
586 return llvm::createStringError("no exe/reg context");
589 StackFrame
*current_frame
= exe_ctx
->GetFramePtr();
590 Thread
*thread
= exe_ctx
->GetThreadPtr();
591 if (!current_frame
|| !thread
)
592 return llvm::createStringError("no current frame/thread");
594 Target
&target
= exe_ctx
->GetTargetRef();
595 StackFrameSP parent_frame
= nullptr;
596 addr_t return_pc
= LLDB_INVALID_ADDRESS
;
597 uint32_t current_frame_idx
= current_frame
->GetFrameIndex();
599 for (uint32_t parent_frame_idx
= current_frame_idx
+ 1;;parent_frame_idx
++) {
600 parent_frame
= thread
->GetStackFrameAtIndex(parent_frame_idx
);
601 // If this is null, we're at the end of the stack.
605 // Record the first valid return address, even if this is an inlined frame,
606 // in order to look up the associated call edge in the first non-inlined
608 if (return_pc
== LLDB_INVALID_ADDRESS
) {
609 return_pc
= parent_frame
->GetFrameCodeAddress().GetLoadAddress(&target
);
610 LLDB_LOG(log
, "immediate ancestor with pc = {0:x}", return_pc
);
613 // If we've found an inlined frame, skip it (these have no call site
615 if (parent_frame
->IsInlined())
618 // We've found the first non-inlined parent frame.
621 if (!parent_frame
|| !parent_frame
->GetRegisterContext()) {
622 return llvm::createStringError("no parent frame with reg ctx");
625 Function
*parent_func
=
626 parent_frame
->GetSymbolContext(eSymbolContextFunction
).function
;
628 return llvm::createStringError("no parent function");
630 // 2. Find the call edge in the parent function responsible for creating the
631 // current activation.
632 Function
*current_func
=
633 current_frame
->GetSymbolContext(eSymbolContextFunction
).function
;
635 return llvm::createStringError("no current function");
637 CallEdge
*call_edge
= nullptr;
638 ModuleList
&modlist
= target
.GetImages();
639 ExecutionContext parent_exe_ctx
= *exe_ctx
;
640 parent_exe_ctx
.SetFrameSP(parent_frame
);
641 if (!parent_frame
->IsArtificial()) {
642 // If the parent frame is not artificial, the current activation may be
643 // produced by an ambiguous tail call. In this case, refuse to proceed.
644 call_edge
= parent_func
->GetCallEdgeForReturnAddress(return_pc
, target
);
646 return llvm::createStringError(
647 llvm::formatv("no call edge for retn-pc = {0:x} in parent frame {1}",
648 return_pc
, parent_func
->GetName()));
650 Function
*callee_func
= call_edge
->GetCallee(modlist
, parent_exe_ctx
);
651 if (callee_func
!= current_func
) {
652 return llvm::createStringError(
653 "ambiguous call sequence, can't find real parent frame");
656 // The StackFrameList solver machinery has deduced that an unambiguous tail
657 // call sequence that produced the current activation. The first edge in
658 // the parent that points to the current function must be valid.
659 for (auto &edge
: parent_func
->GetTailCallingEdges()) {
660 if (edge
->GetCallee(modlist
, parent_exe_ctx
) == current_func
) {
661 call_edge
= edge
.get();
667 return llvm::createStringError("no unambiguous edge from parent "
668 "to current function");
670 // 3. Attempt to locate the DW_OP_entry_value expression in the set of
671 // available call site parameters. If found, evaluate the corresponding
672 // parameter in the context of the parent frame.
673 const uint32_t subexpr_len
= opcodes
.GetULEB128(&opcode_offset
);
674 const void *subexpr_data
= opcodes
.GetData(&opcode_offset
, subexpr_len
);
676 return llvm::createStringError("subexpr could not be read");
678 const CallSiteParameter
*matched_param
= nullptr;
679 for (const CallSiteParameter
¶m
: call_edge
->GetCallSiteParameters()) {
680 DataExtractor param_subexpr_extractor
;
681 if (!param
.LocationInCallee
.GetExpressionData(param_subexpr_extractor
))
683 lldb::offset_t param_subexpr_offset
= 0;
684 const void *param_subexpr_data
=
685 param_subexpr_extractor
.GetData(¶m_subexpr_offset
, subexpr_len
);
686 if (!param_subexpr_data
||
687 param_subexpr_extractor
.BytesLeft(param_subexpr_offset
) != 0)
690 // At this point, the DW_OP_entry_value sub-expression and the callee-side
691 // expression in the call site parameter are known to have the same length.
692 // Check whether they are equal.
694 // Note that an equality check is sufficient: the contents of the
695 // DW_OP_entry_value subexpression are only used to identify the right call
696 // site parameter in the parent, and do not require any special handling.
697 if (memcmp(subexpr_data
, param_subexpr_data
, subexpr_len
) == 0) {
698 matched_param
= ¶m
;
703 return llvm::createStringError("no matching call site param found");
705 // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
706 // subexpresion whenever llvm does.
707 const DWARFExpressionList
¶m_expr
= matched_param
->LocationInCaller
;
709 llvm::Expected
<Value
> maybe_result
= param_expr
.Evaluate(
710 &parent_exe_ctx
, parent_frame
->GetRegisterContext().get(),
711 LLDB_INVALID_ADDRESS
,
712 /*initial_value_ptr=*/nullptr,
713 /*object_address_ptr=*/nullptr);
716 "Evaluate_DW_OP_entry_value: call site param evaluation failed");
717 return maybe_result
.takeError();
720 stack
.push_back(*maybe_result
);
721 return llvm::Error::success();
725 /// The location description kinds described by the DWARF v5
726 /// specification. Composite locations are handled out-of-band and
727 /// thus aren't part of the enum.
728 enum LocationDescriptionKind
{
735 /// Adjust value's ValueType according to the kind of location description.
736 void UpdateValueTypeFromLocationDescription(Log
*log
, const DWARFUnit
*dwarf_cu
,
737 LocationDescriptionKind kind
,
738 Value
*value
= nullptr) {
739 // Note that this function is conflating DWARF expressions with
740 // DWARF location descriptions. Perhaps it would be better to define
741 // a wrapper for DWARFExpression::Eval() that deals with DWARF
742 // location descriptions (which consist of one or more DWARF
743 // expressions). But doing this would mean we'd also need factor the
744 // handling of DW_OP_(bit_)piece out of this function.
745 if (dwarf_cu
&& dwarf_cu
->GetVersion() >= 4) {
746 const char *log_msg
= "DWARF location description kind: %s";
749 LLDB_LOGF(log
, log_msg
, "Empty");
752 LLDB_LOGF(log
, log_msg
, "Memory");
753 if (value
->GetValueType() == Value::ValueType::Scalar
)
754 value
->SetValueType(Value::ValueType::LoadAddress
);
757 LLDB_LOGF(log
, log_msg
, "Register");
758 value
->SetValueType(Value::ValueType::Scalar
);
761 LLDB_LOGF(log
, log_msg
, "Implicit");
762 if (value
->GetValueType() == Value::ValueType::LoadAddress
)
763 value
->SetValueType(Value::ValueType::Scalar
);
770 /// Helper function to move common code used to resolve a file address and turn
771 /// into a load address.
773 /// \param exe_ctx Pointer to the execution context
774 /// \param module_sp shared_ptr contains the module if we have one
775 /// \param dw_op_type C-style string used to vary the error output
776 /// \param file_addr the file address we are trying to resolve and turn into a
778 /// \param so_addr out parameter, will be set to load address or section offset
779 /// \param check_sectionoffset bool which determines if having a section offset
780 /// but not a load address is considerd a success
781 /// \returns std::optional containing the load address if resolving and getting
782 /// the load address succeed or an empty Optinal otherwise. If
783 /// check_sectionoffset is true we consider LLDB_INVALID_ADDRESS a
784 /// success if so_addr.IsSectionOffset() is true.
785 static llvm::Expected
<lldb::addr_t
>
786 ResolveLoadAddress(ExecutionContext
*exe_ctx
, lldb::ModuleSP
&module_sp
,
787 const char *dw_op_type
, lldb::addr_t file_addr
,
788 Address
&so_addr
, bool check_sectionoffset
= false) {
790 return llvm::createStringError("need module to resolve file address for %s",
793 if (!module_sp
->ResolveFileAddress(file_addr
, so_addr
))
794 return llvm::createStringError("failed to resolve file address in module");
796 const addr_t load_addr
= so_addr
.GetLoadAddress(exe_ctx
->GetTargetPtr());
798 if (load_addr
== LLDB_INVALID_ADDRESS
&&
799 (check_sectionoffset
&& !so_addr
.IsSectionOffset()))
800 return llvm::createStringError("failed to resolve load address");
805 /// Helper function to move common code used to load sized data from a uint8_t
808 /// \param addr_bytes uint8_t buffer containg raw data
809 /// \param size_addr_bytes how large is the underlying raw data
810 /// \param byte_order what is the byter order of the underlyig data
811 /// \param size How much of the underlying data we want to use
812 /// \return The underlying data converted into a Scalar
813 static Scalar
DerefSizeExtractDataHelper(uint8_t *addr_bytes
,
814 size_t size_addr_bytes
,
815 ByteOrder byte_order
, size_t size
) {
816 DataExtractor
addr_data(addr_bytes
, size_addr_bytes
, byte_order
, size
);
818 lldb::offset_t addr_data_offset
= 0;
820 return addr_data
.GetMaxU64(&addr_data_offset
, size
);
822 return addr_data
.GetAddress(&addr_data_offset
);
825 llvm::Expected
<Value
> DWARFExpression::Evaluate(
826 ExecutionContext
*exe_ctx
, RegisterContext
*reg_ctx
,
827 lldb::ModuleSP module_sp
, const DataExtractor
&opcodes
,
828 const DWARFUnit
*dwarf_cu
, const lldb::RegisterKind reg_kind
,
829 const Value
*initial_value_ptr
, const Value
*object_address_ptr
) {
831 if (opcodes
.GetByteSize() == 0)
832 return llvm::createStringError(
833 "no location, value may have been optimized out");
834 std::vector
<Value
> stack
;
836 Process
*process
= nullptr;
837 StackFrame
*frame
= nullptr;
838 Target
*target
= nullptr;
841 process
= exe_ctx
->GetProcessPtr();
842 frame
= exe_ctx
->GetFramePtr();
843 target
= exe_ctx
->GetTargetPtr();
845 if (reg_ctx
== nullptr && frame
)
846 reg_ctx
= frame
->GetRegisterContext().get();
848 if (initial_value_ptr
)
849 stack
.push_back(*initial_value_ptr
);
851 lldb::offset_t offset
= 0;
855 /// Insertion point for evaluating multi-piece expression.
856 uint64_t op_piece_offset
= 0;
857 Value pieces
; // Used for DW_OP_piece
859 Log
*log
= GetLog(LLDBLog::Expressions
);
860 // A generic type is "an integral type that has the size of an address and an
861 // unspecified signedness". For now, just use the signedness of the operand.
862 // TODO: Implement a real typed stack, and store the genericness of the value
864 auto to_generic
= [&](auto v
) {
865 // TODO: Avoid implicit trunc?
866 // See https://github.com/llvm/llvm-project/issues/112510.
867 bool is_signed
= std::is_signed
<decltype(v
)>::value
;
868 return Scalar(llvm::APSInt(llvm::APInt(8 * opcodes
.GetAddressByteSize(), v
,
869 is_signed
, /*implicitTrunc=*/true),
873 // The default kind is a memory location. This is updated by any
874 // operation that changes this, such as DW_OP_stack_value, and reset
875 // by composition operations like DW_OP_piece.
876 LocationDescriptionKind dwarf4_location_description_kind
= Memory
;
878 while (opcodes
.ValidOffset(offset
)) {
879 const lldb::offset_t op_offset
= offset
;
880 const uint8_t op
= opcodes
.GetU8(&offset
);
882 if (log
&& log
->GetVerbose()) {
883 size_t count
= stack
.size();
884 LLDB_LOGF(log
, "Stack before operation has %" PRIu64
" values:",
886 for (size_t i
= 0; i
< count
; ++i
) {
887 StreamString new_value
;
888 new_value
.Printf("[%" PRIu64
"]", (uint64_t)i
);
889 stack
[i
].Dump(&new_value
);
890 LLDB_LOGF(log
, " %s", new_value
.GetData());
892 LLDB_LOGF(log
, "0x%8.8" PRIx64
": %s", op_offset
,
893 DW_OP_value_to_name(op
));
896 if (std::optional
<unsigned> arity
=
897 llvm::dwarf::OperationArity(static_cast<LocationAtom
>(op
))) {
898 if (stack
.size() < *arity
)
899 return llvm::createStringError(
900 "%s needs at least %d stack entries (stack has %d entries)",
901 DW_OP_value_to_name(op
), *arity
, stack
.size());
905 // The DW_OP_addr operation has a single operand that encodes a machine
906 // address and whose size is the size of an address on the target machine.
908 stack
.push_back(Scalar(opcodes
.GetAddress(&offset
)));
910 target
->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32
) {
911 // wasm file sections aren't mapped into memory, therefore addresses can
912 // never point into a file section and are always LoadAddresses.
913 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
915 stack
.back().SetValueType(Value::ValueType::FileAddress
);
919 // The DW_OP_addr_sect_offset4 is used for any location expressions in
920 // shared libraries that have a location like:
921 // DW_OP_addr(0x1000)
922 // If this address resides in a shared library, then this virtual address
923 // won't make sense when it is evaluated in the context of a running
924 // process where shared libraries have been slid. To account for this, this
925 // new address type where we can store the section pointer and a 4 byte
927 // case DW_OP_addr_sect_offset4:
929 // result_type = eResultTypeFileAddress;
930 // lldb::Section *sect = (lldb::Section
931 // *)opcodes.GetMaxU64(&offset, sizeof(void *));
932 // lldb::addr_t sect_offset = opcodes.GetU32(&offset);
934 // Address so_addr (sect, sect_offset);
935 // lldb::addr_t load_addr = so_addr.GetLoadAddress();
936 // if (load_addr != LLDB_INVALID_ADDRESS)
938 // // We successfully resolve a file address to a load
940 // stack.push_back(load_addr);
947 // error_ptr->SetErrorStringWithFormat ("Section %s in
948 // %s is not currently loaded.\n",
949 // sect->GetName().AsCString(),
950 // sect->GetModule()->GetFileSpec().GetFilename().AsCString());
956 // OPCODE: DW_OP_deref
958 // DESCRIPTION: Pops the top stack entry and treats it as an address.
959 // The value retrieved from that address is pushed. The size of the data
960 // retrieved from the dereferenced address is the size of an address on the
964 return llvm::createStringError(
965 "expression stack empty for DW_OP_deref");
966 Value::ValueType value_type
= stack
.back().GetValueType();
967 switch (value_type
) {
968 case Value::ValueType::HostAddress
: {
969 void *src
= (void *)stack
.back().GetScalar().ULongLong();
971 ::memcpy(&ptr
, src
, sizeof(void *));
972 stack
.back().GetScalar() = ptr
;
973 stack
.back().ClearContext();
975 case Value::ValueType::FileAddress
: {
976 auto file_addr
= stack
.back().GetScalar().ULongLong(
977 LLDB_INVALID_ADDRESS
);
980 auto maybe_load_addr
= ResolveLoadAddress(
981 exe_ctx
, module_sp
, "DW_OP_deref", file_addr
, so_addr
);
983 if (!maybe_load_addr
)
984 return maybe_load_addr
.takeError();
986 stack
.back().GetScalar() = *maybe_load_addr
;
987 // Fall through to load address promotion code below.
990 case Value::ValueType::Scalar
:
991 // Promote Scalar to LoadAddress and fall through.
992 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
994 case Value::ValueType::LoadAddress
:
997 lldb::addr_t pointer_addr
=
998 stack
.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS
);
1000 lldb::addr_t pointer_value
=
1001 process
->ReadPointerFromMemory(pointer_addr
, error
);
1002 if (pointer_value
!= LLDB_INVALID_ADDRESS
) {
1003 if (ABISP abi_sp
= process
->GetABI())
1004 pointer_value
= abi_sp
->FixCodeAddress(pointer_value
);
1005 stack
.back().GetScalar() = pointer_value
;
1006 stack
.back().ClearContext();
1008 return llvm::createStringError(
1009 "Failed to dereference pointer from 0x%" PRIx64
1010 " for DW_OP_deref: %s\n",
1011 pointer_addr
, error
.AsCString());
1014 return llvm::createStringError("NULL process for DW_OP_deref");
1017 return llvm::createStringError(
1018 "NULL execution context for DW_OP_deref");
1022 case Value::ValueType::Invalid
:
1023 return llvm::createStringError("invalid value type for DW_OP_deref");
1028 // OPCODE: DW_OP_deref_size
1030 // 1 - uint8_t that specifies the size of the data to dereference.
1031 // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top
1032 // stack entry and treats it as an address. The value retrieved from that
1033 // address is pushed. In the DW_OP_deref_size operation, however, the size
1034 // in bytes of the data retrieved from the dereferenced address is
1035 // specified by the single operand. This operand is a 1-byte unsigned
1036 // integral constant whose value may not be larger than the size of an
1037 // address on the target machine. The data retrieved is zero extended to
1038 // the size of an address on the target machine before being pushed on the
1039 // expression stack.
1040 case DW_OP_deref_size
: {
1041 if (stack
.empty()) {
1042 return llvm::createStringError(
1043 "expression stack empty for DW_OP_deref_size");
1045 uint8_t size
= opcodes
.GetU8(&offset
);
1047 return llvm::createStringError(
1048 "Invalid address size for DW_OP_deref_size: %d\n", size
);
1050 Value::ValueType value_type
= stack
.back().GetValueType();
1051 switch (value_type
) {
1052 case Value::ValueType::HostAddress
: {
1053 void *src
= (void *)stack
.back().GetScalar().ULongLong();
1055 ::memcpy(&ptr
, src
, sizeof(void *));
1056 // I can't decide whether the size operand should apply to the bytes in
1058 // lldb-host endianness or the target endianness.. I doubt this'll ever
1059 // come up but I'll opt for assuming big endian regardless.
1068 ptr
= ptr
& 0xffffff;
1071 ptr
= ptr
& 0xffffffff;
1073 // the casts are added to work around the case where intptr_t is a 32
1075 // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this
1078 ptr
= (intptr_t)ptr
& 0xffffffffffULL
;
1081 ptr
= (intptr_t)ptr
& 0xffffffffffffULL
;
1084 ptr
= (intptr_t)ptr
& 0xffffffffffffffULL
;
1089 stack
.back().GetScalar() = ptr
;
1090 stack
.back().ClearContext();
1092 case Value::ValueType::FileAddress
: {
1094 stack
.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS
);
1096 auto maybe_load_addr
= ResolveLoadAddress(
1097 exe_ctx
, module_sp
, "DW_OP_deref_size", file_addr
, so_addr
,
1098 /*check_sectionoffset=*/true);
1100 if (!maybe_load_addr
)
1101 return maybe_load_addr
.takeError();
1103 addr_t load_addr
= *maybe_load_addr
;
1105 if (load_addr
== LLDB_INVALID_ADDRESS
&& so_addr
.IsSectionOffset()) {
1106 uint8_t addr_bytes
[8];
1110 target
->ReadMemory(so_addr
, &addr_bytes
, size
, error
,
1111 /*force_live_memory=*/false) == size
) {
1112 ObjectFile
*objfile
= module_sp
->GetObjectFile();
1114 stack
.back().GetScalar() = DerefSizeExtractDataHelper(
1115 addr_bytes
, size
, objfile
->GetByteOrder(), size
);
1116 stack
.back().ClearContext();
1119 return llvm::createStringError(
1120 "Failed to dereference pointer for DW_OP_deref_size: "
1125 stack
.back().GetScalar() = load_addr
;
1126 // Fall through to load address promotion code below.
1130 case Value::ValueType::Scalar
:
1131 case Value::ValueType::LoadAddress
:
1134 lldb::addr_t pointer_addr
=
1135 stack
.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS
);
1136 uint8_t addr_bytes
[sizeof(lldb::addr_t
)];
1138 if (process
->ReadMemory(pointer_addr
, &addr_bytes
, size
, error
) ==
1141 stack
.back().GetScalar() =
1142 DerefSizeExtractDataHelper(addr_bytes
, sizeof(addr_bytes
),
1143 process
->GetByteOrder(), size
);
1144 stack
.back().ClearContext();
1146 return llvm::createStringError(
1147 "Failed to dereference pointer from 0x%" PRIx64
1148 " for DW_OP_deref: %s\n",
1149 pointer_addr
, error
.AsCString());
1153 return llvm::createStringError("NULL process for DW_OP_deref_size");
1156 return llvm::createStringError(
1157 "NULL execution context for DW_OP_deref_size");
1161 case Value::ValueType::Invalid
:
1163 return llvm::createStringError("invalid value for DW_OP_deref_size");
1168 // OPCODE: DW_OP_xderef_size
1170 // 1 - uint8_t that specifies the size of the data to dereference.
1171 // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at
1172 // the top of the stack is treated as an address. The second stack entry is
1173 // treated as an "address space identifier" for those architectures that
1174 // support multiple address spaces. The top two stack elements are popped,
1175 // a data item is retrieved through an implementation-defined address
1176 // calculation and pushed as the new stack top. In the DW_OP_xderef_size
1177 // operation, however, the size in bytes of the data retrieved from the
1178 // dereferenced address is specified by the single operand. This operand is
1179 // a 1-byte unsigned integral constant whose value may not be larger than
1180 // the size of an address on the target machine. The data retrieved is zero
1181 // extended to the size of an address on the target machine before being
1182 // pushed on the expression stack.
1183 case DW_OP_xderef_size
:
1184 return llvm::createStringError("unimplemented opcode: DW_OP_xderef_size");
1185 // OPCODE: DW_OP_xderef
1187 // DESCRIPTION: Provides an extended dereference mechanism. The entry at
1188 // the top of the stack is treated as an address. The second stack entry is
1189 // treated as an "address space identifier" for those architectures that
1190 // support multiple address spaces. The top two stack elements are popped,
1191 // a data item is retrieved through an implementation-defined address
1192 // calculation and pushed as the new stack top. The size of the data
1193 // retrieved from the dereferenced address is the size of an address on the
1196 return llvm::createStringError("unimplemented opcode: DW_OP_xderef");
1198 // All DW_OP_constXXX opcodes have a single operand as noted below:
1201 // DW_OP_const1u 1-byte unsigned integer constant
1202 // DW_OP_const1s 1-byte signed integer constant
1203 // DW_OP_const2u 2-byte unsigned integer constant
1204 // DW_OP_const2s 2-byte signed integer constant
1205 // DW_OP_const4u 4-byte unsigned integer constant
1206 // DW_OP_const4s 4-byte signed integer constant
1207 // DW_OP_const8u 8-byte unsigned integer constant
1208 // DW_OP_const8s 8-byte signed integer constant
1209 // DW_OP_constu unsigned LEB128 integer constant
1210 // DW_OP_consts signed LEB128 integer constant
1212 stack
.push_back(to_generic(opcodes
.GetU8(&offset
)));
1215 stack
.push_back(to_generic((int8_t)opcodes
.GetU8(&offset
)));
1218 stack
.push_back(to_generic(opcodes
.GetU16(&offset
)));
1221 stack
.push_back(to_generic((int16_t)opcodes
.GetU16(&offset
)));
1224 stack
.push_back(to_generic(opcodes
.GetU32(&offset
)));
1227 stack
.push_back(to_generic((int32_t)opcodes
.GetU32(&offset
)));
1230 stack
.push_back(to_generic(opcodes
.GetU64(&offset
)));
1233 stack
.push_back(to_generic((int64_t)opcodes
.GetU64(&offset
)));
1235 // These should also use to_generic, but we can't do that due to a
1236 // producer-side bug in llvm. See llvm.org/pr48087.
1238 stack
.push_back(Scalar(opcodes
.GetULEB128(&offset
)));
1241 stack
.push_back(Scalar(opcodes
.GetSLEB128(&offset
)));
1244 // OPCODE: DW_OP_dup
1246 // DESCRIPTION: duplicates the value at the top of the stack
1248 if (stack
.empty()) {
1249 return llvm::createStringError("expression stack empty for DW_OP_dup");
1251 stack
.push_back(stack
.back());
1254 // OPCODE: DW_OP_drop
1256 // DESCRIPTION: pops the value at the top of the stack
1258 if (stack
.empty()) {
1259 return llvm::createStringError("expression stack empty for DW_OP_drop");
1264 // OPCODE: DW_OP_over
1266 // DESCRIPTION: Duplicates the entry currently second in the stack at
1267 // the top of the stack.
1269 stack
.push_back(stack
[stack
.size() - 2]);
1272 // OPCODE: DW_OP_pick
1273 // OPERANDS: uint8_t index into the current stack
1274 // DESCRIPTION: The stack entry with the specified index (0 through 255,
1275 // inclusive) is pushed on the stack
1277 uint8_t pick_idx
= opcodes
.GetU8(&offset
);
1278 if (pick_idx
< stack
.size())
1279 stack
.push_back(stack
[stack
.size() - 1 - pick_idx
]);
1281 return llvm::createStringError(
1282 "Index %u out of range for DW_OP_pick.\n", pick_idx
);
1286 // OPCODE: DW_OP_swap
1288 // DESCRIPTION: swaps the top two stack entries. The entry at the top
1289 // of the stack becomes the second stack entry, and the second entry
1290 // becomes the top of the stack
1293 stack
.back() = stack
[stack
.size() - 2];
1294 stack
[stack
.size() - 2] = tmp
;
1297 // OPCODE: DW_OP_rot
1299 // DESCRIPTION: Rotates the first three stack entries. The entry at
1300 // the top of the stack becomes the third stack entry, the second entry
1301 // becomes the top of the stack, and the third entry becomes the second
1304 size_t last_idx
= stack
.size() - 1;
1305 Value old_top
= stack
[last_idx
];
1306 stack
[last_idx
] = stack
[last_idx
- 1];
1307 stack
[last_idx
- 1] = stack
[last_idx
- 2];
1308 stack
[last_idx
- 2] = old_top
;
1311 // OPCODE: DW_OP_abs
1313 // DESCRIPTION: pops the top stack entry, interprets it as a signed
1314 // value and pushes its absolute value. If the absolute value can not be
1315 // represented, the result is undefined.
1317 if (!stack
.back().ResolveValue(exe_ctx
).AbsoluteValue()) {
1318 return llvm::createStringError(
1319 "failed to take the absolute value of the first stack item");
1323 // OPCODE: DW_OP_and
1325 // DESCRIPTION: pops the top two stack values, performs a bitwise and
1326 // operation on the two, and pushes the result.
1330 stack
.back().ResolveValue(exe_ctx
) =
1331 stack
.back().ResolveValue(exe_ctx
) & tmp
.ResolveValue(exe_ctx
);
1334 // OPCODE: DW_OP_div
1336 // DESCRIPTION: pops the top two stack values, divides the former second
1337 // entry by the former top of the stack using signed division, and pushes
1341 if (tmp
.ResolveValue(exe_ctx
).IsZero())
1342 return llvm::createStringError("divide by zero");
1345 Scalar divisor
, dividend
;
1346 divisor
= tmp
.ResolveValue(exe_ctx
);
1347 dividend
= stack
.back().ResolveValue(exe_ctx
);
1348 divisor
.MakeSigned();
1349 dividend
.MakeSigned();
1350 stack
.back() = dividend
/ divisor
;
1352 if (!stack
.back().ResolveValue(exe_ctx
).IsValid())
1353 return llvm::createStringError("divide failed");
1356 // OPCODE: DW_OP_minus
1358 // DESCRIPTION: pops the top two stack values, subtracts the former top
1359 // of the stack from the former second entry, and pushes the result.
1363 stack
.back().ResolveValue(exe_ctx
) =
1364 stack
.back().ResolveValue(exe_ctx
) - tmp
.ResolveValue(exe_ctx
);
1367 // OPCODE: DW_OP_mod
1369 // DESCRIPTION: pops the top two stack values and pushes the result of
1370 // the calculation: former second stack entry modulo the former top of the
1375 stack
.back().ResolveValue(exe_ctx
) =
1376 stack
.back().ResolveValue(exe_ctx
) % tmp
.ResolveValue(exe_ctx
);
1379 // OPCODE: DW_OP_mul
1381 // DESCRIPTION: pops the top two stack entries, multiplies them
1382 // together, and pushes the result.
1386 stack
.back().ResolveValue(exe_ctx
) =
1387 stack
.back().ResolveValue(exe_ctx
) * tmp
.ResolveValue(exe_ctx
);
1390 // OPCODE: DW_OP_neg
1392 // DESCRIPTION: pops the top stack entry, and pushes its negation.
1394 if (!stack
.back().ResolveValue(exe_ctx
).UnaryNegate())
1395 return llvm::createStringError("unary negate failed");
1398 // OPCODE: DW_OP_not
1400 // DESCRIPTION: pops the top stack entry, and pushes its bitwise
1403 if (!stack
.back().ResolveValue(exe_ctx
).OnesComplement())
1404 return llvm::createStringError("logical NOT failed");
1409 // DESCRIPTION: pops the top two stack entries, performs a bitwise or
1410 // operation on the two, and pushes the result.
1414 stack
.back().ResolveValue(exe_ctx
) =
1415 stack
.back().ResolveValue(exe_ctx
) | tmp
.ResolveValue(exe_ctx
);
1418 // OPCODE: DW_OP_plus
1420 // DESCRIPTION: pops the top two stack entries, adds them together, and
1421 // pushes the result.
1425 stack
.back().GetScalar() += tmp
.GetScalar();
1428 // OPCODE: DW_OP_plus_uconst
1430 // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128
1431 // constant operand and pushes the result.
1432 case DW_OP_plus_uconst
: {
1433 const uint64_t uconst_value
= opcodes
.GetULEB128(&offset
);
1434 // Implicit conversion from a UINT to a Scalar...
1435 stack
.back().GetScalar() += uconst_value
;
1436 if (!stack
.back().GetScalar().IsValid())
1437 return llvm::createStringError("DW_OP_plus_uconst failed");
1440 // OPCODE: DW_OP_shl
1442 // DESCRIPTION: pops the top two stack entries, shifts the former
1443 // second entry left by the number of bits specified by the former top of
1444 // the stack, and pushes the result.
1448 stack
.back().ResolveValue(exe_ctx
) <<= tmp
.ResolveValue(exe_ctx
);
1451 // OPCODE: DW_OP_shr
1453 // DESCRIPTION: pops the top two stack entries, shifts the former second
1454 // entry right logically (filling with zero bits) by the number of bits
1455 // specified by the former top of the stack, and pushes the result.
1459 if (!stack
.back().ResolveValue(exe_ctx
).ShiftRightLogical(
1460 tmp
.ResolveValue(exe_ctx
)))
1461 return llvm::createStringError("DW_OP_shr failed");
1464 // OPCODE: DW_OP_shra
1466 // DESCRIPTION: pops the top two stack entries, shifts the former second
1467 // entry right arithmetically (divide the magnitude by 2, keep the same
1468 // sign for the result) by the number of bits specified by the former top
1469 // of the stack, and pushes the result.
1473 stack
.back().ResolveValue(exe_ctx
) >>= tmp
.ResolveValue(exe_ctx
);
1476 // OPCODE: DW_OP_xor
1478 // DESCRIPTION: pops the top two stack entries, performs the bitwise
1479 // exclusive-or operation on the two, and pushes the result.
1483 stack
.back().ResolveValue(exe_ctx
) =
1484 stack
.back().ResolveValue(exe_ctx
) ^ tmp
.ResolveValue(exe_ctx
);
1487 // OPCODE: DW_OP_skip
1488 // OPERANDS: int16_t
1489 // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte
1490 // signed integer constant. The 2-byte constant is the number of bytes of
1491 // the DWARF expression to skip forward or backward from the current
1492 // operation, beginning after the 2-byte constant.
1494 int16_t skip_offset
= (int16_t)opcodes
.GetU16(&offset
);
1495 lldb::offset_t new_offset
= offset
+ skip_offset
;
1496 // New offset can point at the end of the data, in this case we should
1497 // terminate the DWARF expression evaluation (will happen in the loop
1499 if (new_offset
<= opcodes
.GetByteSize())
1500 offset
= new_offset
;
1502 return llvm::createStringError(llvm::formatv(
1503 "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}", offset
,
1504 skip_offset
, opcodes
.GetByteSize()));
1508 // OPCODE: DW_OP_bra
1509 // OPERANDS: int16_t
1510 // DESCRIPTION: A conditional branch. Its single operand is a 2-byte
1511 // signed integer constant. This operation pops the top of stack. If the
1512 // value popped is not the constant 0, the 2-byte constant operand is the
1513 // number of bytes of the DWARF expression to skip forward or backward from
1514 // the current operation, beginning after the 2-byte constant.
1518 int16_t bra_offset
= (int16_t)opcodes
.GetU16(&offset
);
1520 if (tmp
.ResolveValue(exe_ctx
) != zero
) {
1521 lldb::offset_t new_offset
= offset
+ bra_offset
;
1522 // New offset can point at the end of the data, in this case we should
1523 // terminate the DWARF expression evaluation (will happen in the loop
1525 if (new_offset
<= opcodes
.GetByteSize())
1526 offset
= new_offset
;
1528 return llvm::createStringError(llvm::formatv(
1529 "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}", offset
,
1530 bra_offset
, opcodes
.GetByteSize()));
1537 // DESCRIPTION: pops the top two stack values, compares using the
1538 // equals (==) operator.
1539 // STACK RESULT: push the constant value 1 onto the stack if the result
1540 // of the operation is true or the constant value 0 if the result of the
1541 // operation is false.
1545 stack
.back().ResolveValue(exe_ctx
) =
1546 stack
.back().ResolveValue(exe_ctx
) == tmp
.ResolveValue(exe_ctx
);
1551 // DESCRIPTION: pops the top two stack values, compares using the
1552 // greater than or equal to (>=) operator.
1553 // STACK RESULT: push the constant value 1 onto the stack if the result
1554 // of the operation is true or the constant value 0 if the result of the
1555 // operation is false.
1559 stack
.back().ResolveValue(exe_ctx
) =
1560 stack
.back().ResolveValue(exe_ctx
) >= tmp
.ResolveValue(exe_ctx
);
1565 // DESCRIPTION: pops the top two stack values, compares using the
1566 // greater than (>) operator.
1567 // STACK RESULT: push the constant value 1 onto the stack if the result
1568 // of the operation is true or the constant value 0 if the result of the
1569 // operation is false.
1573 stack
.back().ResolveValue(exe_ctx
) =
1574 stack
.back().ResolveValue(exe_ctx
) > tmp
.ResolveValue(exe_ctx
);
1579 // DESCRIPTION: pops the top two stack values, compares using the
1580 // less than or equal to (<=) operator.
1581 // STACK RESULT: push the constant value 1 onto the stack if the result
1582 // of the operation is true or the constant value 0 if the result of the
1583 // operation is false.
1587 stack
.back().ResolveValue(exe_ctx
) =
1588 stack
.back().ResolveValue(exe_ctx
) <= tmp
.ResolveValue(exe_ctx
);
1593 // DESCRIPTION: pops the top two stack values, compares using the
1594 // less than (<) operator.
1595 // STACK RESULT: push the constant value 1 onto the stack if the result
1596 // of the operation is true or the constant value 0 if the result of the
1597 // operation is false.
1601 stack
.back().ResolveValue(exe_ctx
) =
1602 stack
.back().ResolveValue(exe_ctx
) < tmp
.ResolveValue(exe_ctx
);
1607 // DESCRIPTION: pops the top two stack values, compares using the
1608 // not equal (!=) operator.
1609 // STACK RESULT: push the constant value 1 onto the stack if the result
1610 // of the operation is true or the constant value 0 if the result of the
1611 // operation is false.
1615 stack
.back().ResolveValue(exe_ctx
) =
1616 stack
.back().ResolveValue(exe_ctx
) != tmp
.ResolveValue(exe_ctx
);
1619 // OPCODE: DW_OP_litn
1621 // DESCRIPTION: encode the unsigned literal values from 0 through 31.
1622 // STACK RESULT: push the unsigned literal constant value onto the top
1656 stack
.push_back(to_generic(op
- DW_OP_lit0
));
1659 // OPCODE: DW_OP_regN
1661 // DESCRIPTION: Push the value in register n on the top of the stack.
1694 dwarf4_location_description_kind
= Register
;
1695 reg_num
= op
- DW_OP_reg0
;
1697 if (llvm::Error err
=
1698 ReadRegisterValueAsScalar(reg_ctx
, reg_kind
, reg_num
, tmp
))
1700 stack
.push_back(tmp
);
1702 // OPCODE: DW_OP_regx
1704 // ULEB128 literal operand that encodes the register.
1705 // DESCRIPTION: Push the value in register on the top of the stack.
1707 dwarf4_location_description_kind
= Register
;
1708 reg_num
= opcodes
.GetULEB128(&offset
);
1710 if (llvm::Error err
=
1711 ReadRegisterValueAsScalar(reg_ctx
, reg_kind
, reg_num
, tmp
))
1713 stack
.push_back(tmp
);
1716 // OPCODE: DW_OP_bregN
1718 // SLEB128 offset from register N
1719 // DESCRIPTION: Value is in memory at the address specified by register
1720 // N plus an offset.
1752 case DW_OP_breg31
: {
1753 reg_num
= op
- DW_OP_breg0
;
1754 if (llvm::Error err
=
1755 ReadRegisterValueAsScalar(reg_ctx
, reg_kind
, reg_num
, tmp
))
1758 int64_t breg_offset
= opcodes
.GetSLEB128(&offset
);
1759 tmp
.ResolveValue(exe_ctx
) += (uint64_t)breg_offset
;
1761 stack
.push_back(tmp
);
1762 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
1764 // OPCODE: DW_OP_bregx
1766 // ULEB128 literal operand that encodes the register.
1767 // SLEB128 offset from register N
1768 // DESCRIPTION: Value is in memory at the address specified by register
1769 // N plus an offset.
1771 reg_num
= opcodes
.GetULEB128(&offset
);
1772 if (llvm::Error err
=
1773 ReadRegisterValueAsScalar(reg_ctx
, reg_kind
, reg_num
, tmp
))
1776 int64_t breg_offset
= opcodes
.GetSLEB128(&offset
);
1777 tmp
.ResolveValue(exe_ctx
) += (uint64_t)breg_offset
;
1779 stack
.push_back(tmp
);
1780 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
1787 if (llvm::Error err
= frame
->GetFrameBaseValue(value
))
1789 int64_t fbreg_offset
= opcodes
.GetSLEB128(&offset
);
1790 value
+= fbreg_offset
;
1791 stack
.push_back(value
);
1792 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
1794 return llvm::createStringError(
1795 "invalid stack frame in context for DW_OP_fbreg opcode");
1798 return llvm::createStringError(
1799 "NULL execution context for DW_OP_fbreg");
1804 // OPCODE: DW_OP_nop
1806 // DESCRIPTION: A place holder. It has no effect on the location stack
1807 // or any of its values.
1811 // OPCODE: DW_OP_piece
1813 // ULEB128: byte size of the piece
1814 // DESCRIPTION: The operand describes the size in bytes of the piece of
1815 // the object referenced by the DWARF expression whose result is at the top
1816 // of the stack. If the piece is located in a register, but does not occupy
1817 // the entire register, the placement of the piece within that register is
1818 // defined by the ABI.
1820 // Many compilers store a single variable in sets of registers, or store a
1821 // variable partially in memory and partially in registers. DW_OP_piece
1822 // provides a way of describing how large a part of a variable a particular
1823 // DWARF expression refers to.
1825 LocationDescriptionKind piece_locdesc
= dwarf4_location_description_kind
;
1826 // Reset for the next piece.
1827 dwarf4_location_description_kind
= Memory
;
1829 const uint64_t piece_byte_size
= opcodes
.GetULEB128(&offset
);
1831 if (piece_byte_size
> 0) {
1834 if (stack
.empty()) {
1835 UpdateValueTypeFromLocationDescription(
1836 log
, dwarf_cu
, LocationDescriptionKind::Empty
);
1837 // In a multi-piece expression, this means that the current piece is
1838 // not available. Fill with zeros for now by resizing the data and
1840 curr_piece
.ResizeData(piece_byte_size
);
1841 // Note that "0" is not a correct value for the unknown bits.
1842 // It would be better to also return a mask of valid bits together
1843 // with the expression result, so the debugger can print missing
1844 // members as "<optimized out>" or something.
1845 ::memset(curr_piece
.GetBuffer().GetBytes(), 0, piece_byte_size
);
1846 pieces
.AppendDataToHostBuffer(curr_piece
);
1849 // Extract the current piece into "curr_piece"
1850 Value
curr_piece_source_value(stack
.back());
1852 UpdateValueTypeFromLocationDescription(log
, dwarf_cu
, piece_locdesc
,
1853 &curr_piece_source_value
);
1855 const Value::ValueType curr_piece_source_value_type
=
1856 curr_piece_source_value
.GetValueType();
1857 Scalar
&scalar
= curr_piece_source_value
.GetScalar();
1858 lldb::addr_t addr
= scalar
.ULongLong(LLDB_INVALID_ADDRESS
);
1859 switch (curr_piece_source_value_type
) {
1860 case Value::ValueType::Invalid
:
1861 return llvm::createStringError("invalid value type");
1862 case Value::ValueType::FileAddress
:
1864 curr_piece_source_value
.ConvertToLoadAddress(module_sp
.get(),
1866 addr
= scalar
.ULongLong(LLDB_INVALID_ADDRESS
);
1868 return llvm::createStringError(
1869 "unable to convert file address 0x%" PRIx64
1871 "for DW_OP_piece(%" PRIu64
"): "
1872 "no target available",
1873 addr
, piece_byte_size
);
1876 case Value::ValueType::LoadAddress
: {
1878 if (curr_piece
.ResizeData(piece_byte_size
) == piece_byte_size
) {
1879 if (target
->ReadMemory(addr
, curr_piece
.GetBuffer().GetBytes(),
1880 piece_byte_size
, error
,
1881 /*force_live_memory=*/false) !=
1883 const char *addr_type
= (curr_piece_source_value_type
==
1884 Value::ValueType::LoadAddress
)
1887 return llvm::createStringError(
1888 "failed to read memory DW_OP_piece(%" PRIu64
1889 ") from %s address 0x%" PRIx64
,
1890 piece_byte_size
, addr_type
, addr
);
1893 return llvm::createStringError(
1894 "failed to resize the piece memory buffer for "
1895 "DW_OP_piece(%" PRIu64
")",
1900 case Value::ValueType::HostAddress
: {
1901 return llvm::createStringError(
1902 "failed to read memory DW_OP_piece(%" PRIu64
1903 ") from host address 0x%" PRIx64
,
1904 piece_byte_size
, addr
);
1907 case Value::ValueType::Scalar
: {
1908 uint32_t bit_size
= piece_byte_size
* 8;
1909 uint32_t bit_offset
= 0;
1910 if (!scalar
.ExtractBitfield(
1911 bit_size
, bit_offset
)) {
1912 return llvm::createStringError(
1913 "unable to extract %" PRIu64
" bytes from a %" PRIu64
1914 " byte scalar value.",
1916 (uint64_t)curr_piece_source_value
.GetScalar().GetByteSize());
1918 // Create curr_piece with bit_size. By default Scalar
1919 // grows to the nearest host integer type.
1920 llvm::APInt
fail_value(1, 0, false);
1921 llvm::APInt ap_int
= scalar
.UInt128(fail_value
);
1922 assert(ap_int
.getBitWidth() >= bit_size
);
1923 llvm::ArrayRef
<uint64_t> buf
{ap_int
.getRawData(),
1924 ap_int
.getNumWords()};
1925 curr_piece
.GetScalar() = Scalar(llvm::APInt(bit_size
, buf
));
1929 // Check if this is the first piece?
1930 if (op_piece_offset
== 0) {
1931 // This is the first piece, we should push it back onto the stack
1932 // so subsequent pieces will be able to access this piece and add
1934 if (pieces
.AppendDataToHostBuffer(curr_piece
) == 0) {
1935 return llvm::createStringError("failed to append piece data");
1938 // If this is the second or later piece there should be a value on
1940 if (pieces
.GetBuffer().GetByteSize() != op_piece_offset
) {
1941 return llvm::createStringError(
1942 "DW_OP_piece for offset %" PRIu64
1943 " but top of stack is of size %" PRIu64
,
1944 op_piece_offset
, pieces
.GetBuffer().GetByteSize());
1947 if (pieces
.AppendDataToHostBuffer(curr_piece
) == 0)
1948 return llvm::createStringError("failed to append piece data");
1951 op_piece_offset
+= piece_byte_size
;
1955 case DW_OP_bit_piece
: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
1956 if (stack
.size() < 1) {
1957 UpdateValueTypeFromLocationDescription(log
, dwarf_cu
,
1958 LocationDescriptionKind::Empty
);
1959 // Reset for the next piece.
1960 dwarf4_location_description_kind
= Memory
;
1961 return llvm::createStringError(
1962 "expression stack needs at least 1 item for DW_OP_bit_piece");
1964 UpdateValueTypeFromLocationDescription(
1965 log
, dwarf_cu
, dwarf4_location_description_kind
, &stack
.back());
1966 // Reset for the next piece.
1967 dwarf4_location_description_kind
= Memory
;
1968 const uint64_t piece_bit_size
= opcodes
.GetULEB128(&offset
);
1969 const uint64_t piece_bit_offset
= opcodes
.GetULEB128(&offset
);
1970 switch (stack
.back().GetValueType()) {
1971 case Value::ValueType::Invalid
:
1972 return llvm::createStringError(
1973 "unable to extract bit value from invalid value");
1974 case Value::ValueType::Scalar
: {
1975 if (!stack
.back().GetScalar().ExtractBitfield(piece_bit_size
,
1976 piece_bit_offset
)) {
1977 return llvm::createStringError(
1978 "unable to extract %" PRIu64
" bit value with %" PRIu64
1979 " bit offset from a %" PRIu64
" bit scalar value.",
1980 piece_bit_size
, piece_bit_offset
,
1981 (uint64_t)(stack
.back().GetScalar().GetByteSize() * 8));
1985 case Value::ValueType::FileAddress
:
1986 case Value::ValueType::LoadAddress
:
1987 case Value::ValueType::HostAddress
:
1988 return llvm::createStringError(
1989 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
1990 ", bit_offset = %" PRIu64
") from an address value.",
1991 piece_bit_size
, piece_bit_offset
);
1996 // OPCODE: DW_OP_implicit_value
1998 // ULEB128 size of the value block in bytes
1999 // uint8_t* block bytes encoding value in target's memory
2001 // DESCRIPTION: Value is immediately stored in block in the debug info with
2002 // the memory representation of the target.
2003 case DW_OP_implicit_value
: {
2004 dwarf4_location_description_kind
= Implicit
;
2006 const uint32_t len
= opcodes
.GetULEB128(&offset
);
2007 const void *data
= opcodes
.GetData(&offset
, len
);
2010 LLDB_LOG(log
, "Evaluate_DW_OP_implicit_value: could not be read data");
2011 return llvm::createStringError("could not evaluate %s",
2012 DW_OP_value_to_name(op
));
2015 Value
result(data
, len
);
2016 stack
.push_back(result
);
2020 case DW_OP_implicit_pointer
: {
2021 dwarf4_location_description_kind
= Implicit
;
2022 return llvm::createStringError("Could not evaluate %s.",
2023 DW_OP_value_to_name(op
));
2026 // OPCODE: DW_OP_push_object_address
2028 // DESCRIPTION: Pushes the address of the object currently being
2029 // evaluated as part of evaluation of a user presented expression. This
2030 // object may correspond to an independent variable described by its own
2031 // DIE or it may be a component of an array, structure, or class whose
2032 // address has been dynamically determined by an earlier step during user
2033 // expression evaluation.
2034 case DW_OP_push_object_address
:
2035 if (object_address_ptr
)
2036 stack
.push_back(*object_address_ptr
);
2038 return llvm::createStringError("DW_OP_push_object_address used without "
2039 "specifying an object address");
2043 // OPCODE: DW_OP_call2
2045 // uint16_t compile unit relative offset of a DIE
2046 // DESCRIPTION: Performs subroutine calls during evaluation
2047 // of a DWARF expression. The operand is the 2-byte unsigned offset of a
2048 // debugging information entry in the current compilation unit.
2050 // Operand interpretation is exactly like that for DW_FORM_ref2.
2052 // This operation transfers control of DWARF expression evaluation to the
2053 // DW_AT_location attribute of the referenced DIE. If there is no such
2054 // attribute, then there is no effect. Execution of the DWARF expression of
2055 // a DW_AT_location attribute may add to and/or remove from values on the
2056 // stack. Execution returns to the point following the call when the end of
2057 // the attribute is reached. Values on the stack at the time of the call
2058 // may be used as parameters by the called expression and values left on
2059 // the stack by the called expression may be used as return values by prior
2060 // agreement between the calling and called expressions.
2062 return llvm::createStringError("unimplemented opcode DW_OP_call2");
2063 // OPCODE: DW_OP_call4
2065 // uint32_t compile unit relative offset of a DIE
2066 // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF
2067 // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of
2068 // a debugging information entry in the current compilation unit.
2070 // Operand interpretation DW_OP_call4 is exactly like that for
2073 // This operation transfers control of DWARF expression evaluation to the
2074 // DW_AT_location attribute of the referenced DIE. If there is no such
2075 // attribute, then there is no effect. Execution of the DWARF expression of
2076 // a DW_AT_location attribute may add to and/or remove from values on the
2077 // stack. Execution returns to the point following the call when the end of
2078 // the attribute is reached. Values on the stack at the time of the call
2079 // may be used as parameters by the called expression and values left on
2080 // the stack by the called expression may be used as return values by prior
2081 // agreement between the calling and called expressions.
2083 return llvm::createStringError("unimplemented opcode DW_OP_call4");
2085 // OPCODE: DW_OP_stack_value
2087 // DESCRIPTION: Specifies that the object does not exist in memory but
2088 // rather is a constant value. The value from the top of the stack is the
2089 // value to be used. This is the actual object value and not the location.
2090 case DW_OP_stack_value
:
2091 dwarf4_location_description_kind
= Implicit
;
2092 stack
.back().SetValueType(Value::ValueType::Scalar
);
2095 // OPCODE: DW_OP_convert
2097 // A ULEB128 that is either a DIE offset of a
2098 // DW_TAG_base_type or 0 for the generic (pointer-sized) type.
2100 // DESCRIPTION: Pop the top stack element, convert it to a
2101 // different type, and push the result.
2102 case DW_OP_convert
: {
2103 const uint64_t die_offset
= opcodes
.GetULEB128(&offset
);
2106 if (die_offset
== 0) {
2107 // The generic type has the size of an address on the target
2108 // machine and an unspecified signedness. Scalar has no
2109 // "unspecified signedness", so we use unsigned types.
2111 return llvm::createStringError("no module");
2113 bit_size
= module_sp
->GetArchitecture().GetAddressByteSize() * 8;
2115 return llvm::createStringError("unspecified architecture");
2117 // Retrieve the type DIE that the value is being converted to. This
2118 // offset is compile unit relative so we need to fix it up.
2119 const uint64_t abs_die_offset
= die_offset
+ dwarf_cu
->GetOffset();
2120 // FIXME: the constness has annoying ripple effects.
2121 DWARFDIE die
= const_cast<DWARFUnit
*>(dwarf_cu
)->GetDIE(abs_die_offset
);
2123 return llvm::createStringError(
2124 "cannot resolve DW_OP_convert type DIE");
2126 die
.GetAttributeValueAsUnsigned(DW_AT_encoding
, DW_ATE_hi_user
);
2127 bit_size
= die
.GetAttributeValueAsUnsigned(DW_AT_byte_size
, 0) * 8;
2129 bit_size
= die
.GetAttributeValueAsUnsigned(DW_AT_bit_size
, 0);
2131 return llvm::createStringError(
2132 "unsupported type size in DW_OP_convert");
2135 case DW_ATE_signed_char
:
2138 case DW_ATE_unsigned
:
2139 case DW_ATE_unsigned_char
:
2143 return llvm::createStringError(
2144 "unsupported encoding in DW_OP_convert");
2147 Scalar
&top
= stack
.back().ResolveValue(exe_ctx
);
2148 top
.TruncOrExtendTo(bit_size
, sign
);
2152 // OPCODE: DW_OP_call_frame_cfa
2154 // DESCRIPTION: Specifies a DWARF expression that pushes the value of
2155 // the canonical frame address consistent with the call frame information
2156 // located in .debug_frame (or in the FDEs of the eh_frame section).
2157 case DW_OP_call_frame_cfa
:
2159 // Note that we don't have to parse FDEs because this DWARF expression
2160 // is commonly evaluated with a valid stack frame.
2161 StackID id
= frame
->GetStackID();
2162 addr_t cfa
= id
.GetCallFrameAddress();
2163 if (cfa
!= LLDB_INVALID_ADDRESS
) {
2164 stack
.push_back(Scalar(cfa
));
2165 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
2167 return llvm::createStringError(
2168 "stack frame does not include a canonical "
2169 "frame address for DW_OP_call_frame_cfa "
2173 return llvm::createStringError("unvalid stack frame in context for "
2174 "DW_OP_call_frame_cfa opcode");
2178 // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension
2179 // opcode, DW_OP_GNU_push_tls_address)
2181 // DESCRIPTION: Pops a TLS offset from the stack, converts it to
2182 // an address in the current thread's thread-local storage block, and
2183 // pushes it on the stack.
2184 case DW_OP_form_tls_address
:
2185 case DW_OP_GNU_push_tls_address
: {
2186 if (stack
.size() < 1) {
2187 if (op
== DW_OP_form_tls_address
)
2188 return llvm::createStringError(
2189 "DW_OP_form_tls_address needs an argument");
2191 return llvm::createStringError(
2192 "DW_OP_GNU_push_tls_address needs an argument");
2195 if (!exe_ctx
|| !module_sp
)
2196 return llvm::createStringError("no context to evaluate TLS within");
2198 Thread
*thread
= exe_ctx
->GetThreadPtr();
2200 return llvm::createStringError("no thread to evaluate TLS within");
2202 // Lookup the TLS block address for this thread and module.
2203 const addr_t tls_file_addr
=
2204 stack
.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS
);
2205 const addr_t tls_load_addr
=
2206 thread
->GetThreadLocalData(module_sp
, tls_file_addr
);
2208 if (tls_load_addr
== LLDB_INVALID_ADDRESS
)
2209 return llvm::createStringError(
2210 "no TLS data currently exists for this thread");
2212 stack
.back().GetScalar() = tls_load_addr
;
2213 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
2216 // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.)
2218 // ULEB128: index to the .debug_addr section
2219 // DESCRIPTION: Pushes an address to the stack from the .debug_addr
2220 // section with the base address specified by the DW_AT_addr_base attribute
2221 // and the 0 based index is the ULEB128 encoded index.
2223 case DW_OP_GNU_addr_index
: {
2225 return llvm::createStringError("DW_OP_GNU_addr_index found without a "
2226 "compile unit being specified");
2227 uint64_t index
= opcodes
.GetULEB128(&offset
);
2228 lldb::addr_t value
= dwarf_cu
->ReadAddressFromDebugAddrSection(index
);
2229 stack
.push_back(Scalar(value
));
2231 target
->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32
) {
2232 // wasm file sections aren't mapped into memory, therefore addresses can
2233 // never point into a file section and are always LoadAddresses.
2234 stack
.back().SetValueType(Value::ValueType::LoadAddress
);
2236 stack
.back().SetValueType(Value::ValueType::FileAddress
);
2240 // OPCODE: DW_OP_GNU_const_index
2242 // ULEB128: index to the .debug_addr section
2243 // DESCRIPTION: Pushes an constant with the size of a machine address to
2244 // the stack from the .debug_addr section with the base address specified
2245 // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128
2247 case DW_OP_GNU_const_index
: {
2249 return llvm::createStringError("DW_OP_GNU_const_index found without a "
2250 "compile unit being specified");
2252 uint64_t index
= opcodes
.GetULEB128(&offset
);
2253 lldb::addr_t value
= dwarf_cu
->ReadAddressFromDebugAddrSection(index
);
2254 stack
.push_back(Scalar(value
));
2257 case DW_OP_GNU_entry_value
:
2258 case DW_OP_entry_value
: {
2259 if (llvm::Error err
= Evaluate_DW_OP_entry_value(stack
, exe_ctx
, reg_ctx
,
2260 opcodes
, offset
, log
))
2261 return llvm::createStringError(
2262 "could not evaluate DW_OP_entry_value: %s",
2263 llvm::toString(std::move(err
)).c_str());
2269 if (dwarf_cu
->GetSymbolFileDWARF().ParseVendorDWARFOpcode(
2270 op
, opcodes
, offset
, stack
)) {
2274 return llvm::createStringError(llvm::formatv(
2275 "Unhandled opcode {0} in DWARFExpression", LocationAtom(op
)));
2279 if (stack
.empty()) {
2280 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2281 // or DW_OP_bit_piece opcodes
2282 if (pieces
.GetBuffer().GetByteSize())
2285 return llvm::createStringError("stack empty after evaluation");
2288 UpdateValueTypeFromLocationDescription(
2289 log
, dwarf_cu
, dwarf4_location_description_kind
, &stack
.back());
2291 if (log
&& log
->GetVerbose()) {
2292 size_t count
= stack
.size();
2294 "Stack after operation has %" PRIu64
" values:", (uint64_t)count
);
2295 for (size_t i
= 0; i
< count
; ++i
) {
2296 StreamString new_value
;
2297 new_value
.Printf("[%" PRIu64
"]", (uint64_t)i
);
2298 stack
[i
].Dump(&new_value
);
2299 LLDB_LOGF(log
, " %s", new_value
.GetData());
2302 return stack
.back();
2305 bool DWARFExpression::ParseDWARFLocationList(
2306 const DWARFUnit
*dwarf_cu
, const DataExtractor
&data
,
2307 DWARFExpressionList
*location_list
) {
2308 location_list
->Clear();
2309 std::unique_ptr
<llvm::DWARFLocationTable
> loctable_up
=
2310 dwarf_cu
->GetLocationTable(data
);
2311 Log
*log
= GetLog(LLDBLog::Expressions
);
2313 [&](uint32_t index
) -> std::optional
<llvm::object::SectionedAddress
> {
2314 addr_t address
= dwarf_cu
->ReadAddressFromDebugAddrSection(index
);
2315 if (address
== LLDB_INVALID_ADDRESS
)
2316 return std::nullopt
;
2317 return llvm::object::SectionedAddress
{address
};
2319 auto process_list
= [&](llvm::Expected
<llvm::DWARFLocationExpression
> loc
) {
2321 LLDB_LOG_ERROR(log
, loc
.takeError(), "{0}");
2325 std::make_shared
<DataBufferHeap
>(loc
->Expr
.data(), loc
->Expr
.size());
2326 DWARFExpression expr
= DWARFExpression(DataExtractor(
2327 buffer_sp
, data
.GetByteOrder(), data
.GetAddressByteSize()));
2328 location_list
->AddExpression(loc
->Range
->LowPC
, loc
->Range
->HighPC
, expr
);
2331 llvm::Error error
= loctable_up
->visitAbsoluteLocationList(
2332 0, llvm::object::SectionedAddress
{dwarf_cu
->GetBaseAddress()},
2333 lookup_addr
, process_list
);
2334 location_list
->Sort();
2336 LLDB_LOG_ERROR(log
, std::move(error
), "{0}");
2342 bool DWARFExpression::MatchesOperand(
2343 StackFrame
&frame
, const Instruction::Operand
&operand
) const {
2344 using namespace OperandMatchers
;
2346 RegisterContextSP reg_ctx_sp
= frame
.GetRegisterContext();
2351 DataExtractor
opcodes(m_data
);
2353 lldb::offset_t op_offset
= 0;
2354 uint8_t opcode
= opcodes
.GetU8(&op_offset
);
2356 if (opcode
== DW_OP_fbreg
) {
2357 int64_t offset
= opcodes
.GetSLEB128(&op_offset
);
2359 DWARFExpressionList
*fb_expr
= frame
.GetFrameBaseExpression(nullptr);
2364 auto recurse
= [&frame
, fb_expr
](const Instruction::Operand
&child
) {
2365 return fb_expr
->MatchesOperand(frame
, child
);
2369 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference
),
2370 recurse
)(operand
)) {
2374 return MatchUnaryOp(
2375 MatchOpType(Instruction::Operand::Type::Dereference
),
2376 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum
),
2377 MatchImmOp(offset
), recurse
))(operand
);
2380 bool dereference
= false;
2381 const RegisterInfo
*reg
= nullptr;
2384 if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
) {
2385 reg
= reg_ctx_sp
->GetRegisterInfo(m_reg_kind
, opcode
- DW_OP_reg0
);
2386 } else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
) {
2387 offset
= opcodes
.GetSLEB128(&op_offset
);
2388 reg
= reg_ctx_sp
->GetRegisterInfo(m_reg_kind
, opcode
- DW_OP_breg0
);
2389 } else if (opcode
== DW_OP_regx
) {
2390 uint32_t reg_num
= static_cast<uint32_t>(opcodes
.GetULEB128(&op_offset
));
2391 reg
= reg_ctx_sp
->GetRegisterInfo(m_reg_kind
, reg_num
);
2392 } else if (opcode
== DW_OP_bregx
) {
2393 uint32_t reg_num
= static_cast<uint32_t>(opcodes
.GetULEB128(&op_offset
));
2394 offset
= opcodes
.GetSLEB128(&op_offset
);
2395 reg
= reg_ctx_sp
->GetRegisterInfo(m_reg_kind
, reg_num
);
2406 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference
),
2407 MatchRegOp(*reg
))(operand
)) {
2411 return MatchUnaryOp(
2412 MatchOpType(Instruction::Operand::Type::Dereference
),
2413 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum
),
2415 MatchImmOp(offset
)))(operand
);
2417 return MatchRegOp(*reg
)(operand
);