LLDB mainline
DWARFExpression.cpp
Go to the documentation of this file.
1//===-- DWARFExpression.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// The DWARF expression opcodes evaluated in this file are defined by the DWARF
10// Debugging Information Format specification, available at:
11//
12// https://dwarfstd.org/
13//
14//===----------------------------------------------------------------------===//
15
17
18#include <cinttypes>
19
20#include <optional>
21#include <vector>
22
23#include "lldb/Core/Module.h"
24#include "lldb/Core/Value.h"
27#include "lldb/Utility/Log.h"
29#include "lldb/Utility/Scalar.h"
31
32#include "lldb/Host/Host.h"
33#include "lldb/Utility/Endian.h"
34
36
37#include "lldb/Target/ABI.h"
39#include "lldb/Target/Process.h"
42#include "lldb/Target/StackID.h"
43#include "lldb/Target/Target.h"
44#include "lldb/Target/Thread.h"
45#include "llvm/DebugInfo/DWARF/DWARFExpressionPrinter.h"
46#include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
47#include "llvm/Support/ErrorExtras.h"
48
49using namespace lldb;
50using namespace lldb_private;
51using namespace lldb_private::plugin::dwarf;
52using namespace llvm::dwarf;
53
54namespace {
55/// The location description kinds described by the DWARF v5
56/// specification. Composite locations are handled out-of-band and
57/// thus aren't part of the enum.
58enum LocationDescriptionKind {
59 Empty,
60 Memory,
61 Register,
62 Implicit
63 /* Composite*/
64};
65
66/// Aggregates the inputs, derived pointers, and mutable evaluation state for
67/// a single DWARF expression evaluation. Passed by reference to every helper
68/// so they don't need to re-thread these individually.
69struct EvalContext {
70 ExecutionContext *exe_ctx;
71 RegisterContext *reg_ctx;
72 lldb::ModuleSP module_sp;
73 const DWARFExpression::Delegate *dwarf_cu;
74 lldb::RegisterKind reg_kind;
75 const Value *initial_value_ptr;
76 const Value *object_address_ptr;
77 Process *process = nullptr;
78 Target *target = nullptr;
79 StackFrame *frame = nullptr;
80
81 /// Mutable evaluation state.
82 /// @{
83 std::vector<Value> stack;
84 Value pieces;
85 uint64_t op_piece_offset = 0;
86 LocationDescriptionKind loc_desc_kind = Memory;
87 /// @}
88
89 EvalContext(ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
90 lldb::ModuleSP module_sp,
91 const DWARFExpression::Delegate *dwarf_cu,
92 lldb::RegisterKind reg_kind, const Value *initial_value_ptr,
93 const Value *object_address_ptr)
94 : exe_ctx(exe_ctx), reg_ctx(reg_ctx), module_sp(std::move(module_sp)),
95 dwarf_cu(dwarf_cu), reg_kind(reg_kind),
96 initial_value_ptr(initial_value_ptr),
97 object_address_ptr(object_address_ptr) {
98 if (exe_ctx) {
99 process = exe_ctx->GetProcessPtr();
100 frame = exe_ctx->GetFramePtr();
101 target = exe_ctx->GetTargetPtr();
102 }
103 if (this->reg_ctx == nullptr && frame)
104 this->reg_ctx = frame->GetRegisterContext().get();
105 }
106};
107} // namespace
108
109// DWARFExpression constructor
111
113
114// Destructor
116
117bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; }
118
119void DWARFExpression::UpdateValue(uint64_t const_value,
120 lldb::offset_t const_value_byte_size,
121 uint8_t addr_byte_size) {
122 if (!const_value_byte_size)
123 return;
124
125 m_data.SetData(
126 DataBufferSP(new DataBufferHeap(&const_value, const_value_byte_size)));
127 m_data.SetByteOrder(endian::InlHostByteOrder());
128 m_data.SetAddressByteSize(addr_byte_size);
129}
130
132 ABI *abi,
133 llvm::DIDumpOptions options) const {
134 auto *MCRegInfo = abi ? &abi->GetMCRegisterInfo() : nullptr;
135 auto GetRegName = [&MCRegInfo](uint64_t DwarfRegNum,
136 bool IsEH) -> llvm::StringRef {
137 if (!MCRegInfo)
138 return {};
139 if (std::optional<unsigned> LLVMRegNum =
140 MCRegInfo->getLLVMRegNum(DwarfRegNum, IsEH))
141 if (const char *RegName = MCRegInfo->getName(*LLVMRegNum))
142 return llvm::StringRef(RegName);
143 return {};
144 };
145 options.GetNameForDWARFReg = GetRegName;
146 llvm::DWARFExpression E(m_data.GetAsLLVM(), m_data.GetAddressByteSize());
147 llvm::printDwarfExpression(&E, s->AsRawOstream(), options, nullptr);
148}
149
151
153 m_reg_kind = reg_kind;
154}
155
156llvm::Error
158 lldb::RegisterKind reg_kind,
159 uint32_t reg_num, Value &value) {
160 if (reg_ctx == nullptr)
161 return llvm::createStringError("no register context in frame");
162
163 const uint32_t native_reg =
164 reg_ctx->ConvertRegisterKindToRegisterNumber(reg_kind, reg_num);
165 if (native_reg == LLDB_INVALID_REGNUM)
166 return llvm::createStringError(
167 "unable to convert register kind=%u reg_num=%u to a native "
168 "register number",
169 reg_kind, reg_num);
170
171 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(native_reg);
172 RegisterValue reg_value;
173 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
174 if (reg_value.GetScalarValue(value.GetScalar())) {
177 const_cast<RegisterInfo *>(reg_info));
178 return llvm::Error::success();
179 }
180
181 // If we get this error, then we need to implement a value buffer in
182 // the dwarf expression evaluation function...
183 return llvm::createStringError(
184 "register %s can't be converted to a scalar value", reg_info->name);
185 }
186
187 return llvm::createStringError("register %s is not available",
188 reg_info->name);
189}
190
191/// Return the length in bytes of the set of operands for \p op. No guarantees
192/// are made on the state of \p data after this call.
193static lldb::offset_t
194GetOpcodeDataSize(const DataExtractor &data, const lldb::offset_t data_offset,
195 const LocationAtom op,
196 const DWARFExpression::Delegate *dwarf_cu) {
197 lldb::offset_t offset = data_offset;
198 switch (op) {
199 // Only used in LLVM metadata.
200 case DW_OP_LLVM_fragment:
201 case DW_OP_LLVM_convert:
202 case DW_OP_LLVM_tag_offset:
203 case DW_OP_LLVM_entry_value:
204 case DW_OP_LLVM_implicit_pointer:
205 case DW_OP_LLVM_arg:
206 case DW_OP_LLVM_extract_bits_sext:
207 case DW_OP_LLVM_extract_bits_zext:
208 break;
209 // Vendor extensions:
210 case DW_OP_HP_is_value:
211 case DW_OP_HP_fltconst4:
212 case DW_OP_HP_fltconst8:
213 case DW_OP_HP_mod_range:
214 case DW_OP_HP_unmod_range:
215 case DW_OP_HP_tls:
216 case DW_OP_INTEL_bit_piece:
217 case DW_OP_WASM_location:
218 case DW_OP_WASM_location_int:
219 case DW_OP_APPLE_uninit:
220 case DW_OP_PGI_omp_thread_num:
221 case DW_OP_hi_user:
222 case DW_OP_GNU_implicit_pointer:
223 break;
224
225 case DW_OP_addr:
226 case DW_OP_call_ref: // 0x9a 1 address sized offset of DIE (DWARF3)
227 return data.GetAddressByteSize();
228
229 // Opcodes with no arguments
230 case DW_OP_deref: // 0x06
231 case DW_OP_dup: // 0x12
232 case DW_OP_drop: // 0x13
233 case DW_OP_over: // 0x14
234 case DW_OP_swap: // 0x16
235 case DW_OP_rot: // 0x17
236 case DW_OP_xderef: // 0x18
237 case DW_OP_abs: // 0x19
238 case DW_OP_and: // 0x1a
239 case DW_OP_div: // 0x1b
240 case DW_OP_minus: // 0x1c
241 case DW_OP_mod: // 0x1d
242 case DW_OP_mul: // 0x1e
243 case DW_OP_neg: // 0x1f
244 case DW_OP_not: // 0x20
245 case DW_OP_or: // 0x21
246 case DW_OP_plus: // 0x22
247 case DW_OP_shl: // 0x24
248 case DW_OP_shr: // 0x25
249 case DW_OP_shra: // 0x26
250 case DW_OP_xor: // 0x27
251 case DW_OP_eq: // 0x29
252 case DW_OP_ge: // 0x2a
253 case DW_OP_gt: // 0x2b
254 case DW_OP_le: // 0x2c
255 case DW_OP_lt: // 0x2d
256 case DW_OP_ne: // 0x2e
257 case DW_OP_lit0: // 0x30
258 case DW_OP_lit1: // 0x31
259 case DW_OP_lit2: // 0x32
260 case DW_OP_lit3: // 0x33
261 case DW_OP_lit4: // 0x34
262 case DW_OP_lit5: // 0x35
263 case DW_OP_lit6: // 0x36
264 case DW_OP_lit7: // 0x37
265 case DW_OP_lit8: // 0x38
266 case DW_OP_lit9: // 0x39
267 case DW_OP_lit10: // 0x3A
268 case DW_OP_lit11: // 0x3B
269 case DW_OP_lit12: // 0x3C
270 case DW_OP_lit13: // 0x3D
271 case DW_OP_lit14: // 0x3E
272 case DW_OP_lit15: // 0x3F
273 case DW_OP_lit16: // 0x40
274 case DW_OP_lit17: // 0x41
275 case DW_OP_lit18: // 0x42
276 case DW_OP_lit19: // 0x43
277 case DW_OP_lit20: // 0x44
278 case DW_OP_lit21: // 0x45
279 case DW_OP_lit22: // 0x46
280 case DW_OP_lit23: // 0x47
281 case DW_OP_lit24: // 0x48
282 case DW_OP_lit25: // 0x49
283 case DW_OP_lit26: // 0x4A
284 case DW_OP_lit27: // 0x4B
285 case DW_OP_lit28: // 0x4C
286 case DW_OP_lit29: // 0x4D
287 case DW_OP_lit30: // 0x4E
288 case DW_OP_lit31: // 0x4f
289 case DW_OP_reg0: // 0x50
290 case DW_OP_reg1: // 0x51
291 case DW_OP_reg2: // 0x52
292 case DW_OP_reg3: // 0x53
293 case DW_OP_reg4: // 0x54
294 case DW_OP_reg5: // 0x55
295 case DW_OP_reg6: // 0x56
296 case DW_OP_reg7: // 0x57
297 case DW_OP_reg8: // 0x58
298 case DW_OP_reg9: // 0x59
299 case DW_OP_reg10: // 0x5A
300 case DW_OP_reg11: // 0x5B
301 case DW_OP_reg12: // 0x5C
302 case DW_OP_reg13: // 0x5D
303 case DW_OP_reg14: // 0x5E
304 case DW_OP_reg15: // 0x5F
305 case DW_OP_reg16: // 0x60
306 case DW_OP_reg17: // 0x61
307 case DW_OP_reg18: // 0x62
308 case DW_OP_reg19: // 0x63
309 case DW_OP_reg20: // 0x64
310 case DW_OP_reg21: // 0x65
311 case DW_OP_reg22: // 0x66
312 case DW_OP_reg23: // 0x67
313 case DW_OP_reg24: // 0x68
314 case DW_OP_reg25: // 0x69
315 case DW_OP_reg26: // 0x6A
316 case DW_OP_reg27: // 0x6B
317 case DW_OP_reg28: // 0x6C
318 case DW_OP_reg29: // 0x6D
319 case DW_OP_reg30: // 0x6E
320 case DW_OP_reg31: // 0x6F
321 case DW_OP_nop: // 0x96
322 case DW_OP_push_object_address: // 0x97 DWARF3
323 case DW_OP_form_tls_address: // 0x9b DWARF3
324 case DW_OP_call_frame_cfa: // 0x9c DWARF3
325 case DW_OP_stack_value: // 0x9f DWARF4
326 case DW_OP_GNU_push_tls_address: // 0xe0 GNU extension
327 return 0;
328
329 // Opcodes with a single 1 byte arguments
330 case DW_OP_const1u: // 0x08 1 1-byte constant
331 case DW_OP_const1s: // 0x09 1 1-byte constant
332 case DW_OP_pick: // 0x15 1 1-byte stack index
333 case DW_OP_deref_size: // 0x94 1 1-byte size of data retrieved
334 case DW_OP_xderef_size: // 0x95 1 1-byte size of data retrieved
335 case DW_OP_deref_type: // 0xa6 1 1-byte constant
336 return 1;
337
338 // Opcodes with a single 2 byte arguments
339 case DW_OP_const2u: // 0x0a 1 2-byte constant
340 case DW_OP_const2s: // 0x0b 1 2-byte constant
341 case DW_OP_skip: // 0x2f 1 signed 2-byte constant
342 case DW_OP_bra: // 0x28 1 signed 2-byte constant
343 case DW_OP_call2: // 0x98 1 2-byte offset of DIE (DWARF3)
344 return 2;
345
346 // Opcodes with a single 4 byte arguments
347 case DW_OP_const4u: // 0x0c 1 4-byte constant
348 case DW_OP_const4s: // 0x0d 1 4-byte constant
349 case DW_OP_call4: // 0x99 1 4-byte offset of DIE (DWARF3)
350 return 4;
351
352 // Opcodes with a single 8 byte arguments
353 case DW_OP_const8u: // 0x0e 1 8-byte constant
354 case DW_OP_const8s: // 0x0f 1 8-byte constant
355 return 8;
356
357 // All opcodes that have a single ULEB (signed or unsigned) argument
358 case DW_OP_constu: // 0x10 1 ULEB128 constant
359 case DW_OP_consts: // 0x11 1 SLEB128 constant
360 case DW_OP_plus_uconst: // 0x23 1 ULEB128 addend
361 case DW_OP_breg0: // 0x70 1 ULEB128 register
362 case DW_OP_breg1: // 0x71 1 ULEB128 register
363 case DW_OP_breg2: // 0x72 1 ULEB128 register
364 case DW_OP_breg3: // 0x73 1 ULEB128 register
365 case DW_OP_breg4: // 0x74 1 ULEB128 register
366 case DW_OP_breg5: // 0x75 1 ULEB128 register
367 case DW_OP_breg6: // 0x76 1 ULEB128 register
368 case DW_OP_breg7: // 0x77 1 ULEB128 register
369 case DW_OP_breg8: // 0x78 1 ULEB128 register
370 case DW_OP_breg9: // 0x79 1 ULEB128 register
371 case DW_OP_breg10: // 0x7a 1 ULEB128 register
372 case DW_OP_breg11: // 0x7b 1 ULEB128 register
373 case DW_OP_breg12: // 0x7c 1 ULEB128 register
374 case DW_OP_breg13: // 0x7d 1 ULEB128 register
375 case DW_OP_breg14: // 0x7e 1 ULEB128 register
376 case DW_OP_breg15: // 0x7f 1 ULEB128 register
377 case DW_OP_breg16: // 0x80 1 ULEB128 register
378 case DW_OP_breg17: // 0x81 1 ULEB128 register
379 case DW_OP_breg18: // 0x82 1 ULEB128 register
380 case DW_OP_breg19: // 0x83 1 ULEB128 register
381 case DW_OP_breg20: // 0x84 1 ULEB128 register
382 case DW_OP_breg21: // 0x85 1 ULEB128 register
383 case DW_OP_breg22: // 0x86 1 ULEB128 register
384 case DW_OP_breg23: // 0x87 1 ULEB128 register
385 case DW_OP_breg24: // 0x88 1 ULEB128 register
386 case DW_OP_breg25: // 0x89 1 ULEB128 register
387 case DW_OP_breg26: // 0x8a 1 ULEB128 register
388 case DW_OP_breg27: // 0x8b 1 ULEB128 register
389 case DW_OP_breg28: // 0x8c 1 ULEB128 register
390 case DW_OP_breg29: // 0x8d 1 ULEB128 register
391 case DW_OP_breg30: // 0x8e 1 ULEB128 register
392 case DW_OP_breg31: // 0x8f 1 ULEB128 register
393 case DW_OP_regx: // 0x90 1 ULEB128 register
394 case DW_OP_fbreg: // 0x91 1 SLEB128 offset
395 case DW_OP_piece: // 0x93 1 ULEB128 size of piece addressed
396 case DW_OP_convert: // 0xa8 1 ULEB128 offset
397 case DW_OP_reinterpret: // 0xa9 1 ULEB128 offset
398 case DW_OP_addrx: // 0xa1 1 ULEB128 index
399 case DW_OP_constx: // 0xa2 1 ULEB128 index
400 case DW_OP_xderef_type: // 0xa7 1 ULEB128 index
401 case DW_OP_GNU_addr_index: // 0xfb 1 ULEB128 index
402 case DW_OP_GNU_const_index: // 0xfc 1 ULEB128 index
403 data.Skip_LEB128(&offset);
404 return offset - data_offset;
405
406 // All opcodes that have a 2 ULEB (signed or unsigned) arguments
407 case DW_OP_bregx: // 0x92 2 ULEB128 register followed by SLEB128 offset
408 case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3);
409 case DW_OP_regval_type: // 0xa5 ULEB128 + ULEB128
410 data.Skip_LEB128(&offset);
411 data.Skip_LEB128(&offset);
412 return offset - data_offset;
413
414 case DW_OP_implicit_value: // 0x9e ULEB128 size followed by block of that size
415 // (DWARF4)
416 {
417 uint64_t block_len = data.GetULEB128(&offset);
418 offset += block_len;
419 return offset - data_offset;
420 }
421
422 case DW_OP_implicit_pointer: // 0xa0 4-byte (or 8-byte for DWARF 64) constant
423 // + LEB128
424 {
425 data.Skip_LEB128(&offset);
426 return (dwarf_cu ? dwarf_cu->GetAddressByteSize() : 4) + offset -
427 data_offset;
428 }
429
430 case DW_OP_GNU_entry_value:
431 case DW_OP_entry_value: // 0xa3 ULEB128 size + variable-length block
432 {
433 uint64_t subexpr_len = data.GetULEB128(&offset);
434 return (offset - data_offset) + subexpr_len;
435 }
436
437 case DW_OP_const_type: // 0xa4 ULEB128 + size + variable-length block
438 {
439 data.Skip_LEB128(&offset);
440 uint8_t length = data.GetU8(&offset);
441 return (offset - data_offset) + length;
442 }
443
444 case DW_OP_LLVM_user: // 0xe9: ULEB128 + variable length constant
445 {
446 uint64_t constants = data.GetULEB128(&offset);
447 return (offset - data_offset) + constants;
448 }
449 }
450
451 if (dwarf_cu)
452 return dwarf_cu->GetVendorDWARFOpcodeSize(data, data_offset, op);
453
454 return LLDB_INVALID_OFFSET;
455}
456
457static const char *DW_OP_value_to_name(uint32_t val) {
458 static char invalid[100];
459 llvm::StringRef llvmstr = llvm::dwarf::OperationEncodingString(val);
460 if (llvmstr.empty()) {
461 snprintf(invalid, sizeof(invalid), "Unknown DW_OP constant: 0x%x", val);
462 return invalid;
463 }
464 return llvmstr.data();
465}
466
467llvm::Expected<lldb::addr_t> DWARFExpression::GetLocation_DW_OP_addr(
468 const DWARFExpression::Delegate *dwarf_cu) const {
469 lldb::offset_t offset = 0;
470 while (m_data.ValidOffset(offset)) {
471 const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(&offset));
472
473 if (op == DW_OP_addr)
474 return m_data.GetAddress(&offset);
475
476 if (op == DW_OP_GNU_addr_index || op == DW_OP_addrx) {
477 const uint64_t index = m_data.GetULEB128(&offset);
478 if (dwarf_cu)
479 return dwarf_cu->ReadAddressFromDebugAddrSection(index);
480 return llvm::createStringError("cannot evaluate %s without a DWARF unit",
482 }
483
484 const lldb::offset_t op_arg_size =
485 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
486 if (op_arg_size == LLDB_INVALID_OFFSET)
487 return llvm::createStringError("cannot get opcode data size for %s",
489
490 offset += op_arg_size;
491 }
492
494}
495
497 const DWARFExpression::Delegate *dwarf_cu, lldb::addr_t file_addr) {
498 lldb::offset_t offset = 0;
499 while (m_data.ValidOffset(offset)) {
500 const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(&offset));
501
502 if (op == DW_OP_addr) {
503 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
504 // We have to make a copy of the data as we don't know if this data is
505 // from a read only memory mapped buffer, so we duplicate all of the data
506 // first, then modify it, and if all goes well, we then replace the data
507 // for this expression
508
509 // Make en encoder that contains a copy of the location expression data
510 // so we can write the address into the buffer using the correct byte
511 // order.
512 DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
513 m_data.GetByteOrder(), addr_byte_size);
514
515 // Replace the address in the new buffer
516 if (encoder.PutAddress(offset, file_addr) == UINT32_MAX)
517 return false;
518
519 // All went well, so now we can reset the data using a shared pointer to
520 // the heap data so "m_data" will now correctly manage the heap data.
521 m_data.SetData(encoder.GetDataBuffer());
522 return true;
523 }
524 if (op == DW_OP_addrx) {
525 // Replace DW_OP_addrx with DW_OP_addr, since we can't modify the
526 // read-only debug_addr table.
527 // Subtract one to account for the opcode.
528 llvm::ArrayRef data_before_op = m_data.GetData().take_front(offset - 1);
529
530 // Read the addrx index to determine how many bytes it needs.
531 const lldb::offset_t old_offset = offset;
532 m_data.GetULEB128(&offset);
533 if (old_offset == offset)
534 return false;
535 llvm::ArrayRef data_after_op = m_data.GetData().drop_front(offset);
536
537 DataEncoder encoder(m_data.GetByteOrder(), m_data.GetAddressByteSize());
538 encoder.AppendData(data_before_op);
539 encoder.AppendU8(DW_OP_addr);
540 encoder.AppendAddress(file_addr);
541 encoder.AppendData(data_after_op);
542 m_data.SetData(encoder.GetDataBuffer());
543 return true;
544 }
545 const lldb::offset_t op_arg_size =
546 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
547 if (op_arg_size == LLDB_INVALID_OFFSET)
548 break;
549 offset += op_arg_size;
550 }
551 return false;
552}
553
555 const DWARFExpression::Delegate *dwarf_cu) const {
556 lldb::offset_t offset = 0;
557 while (m_data.ValidOffset(offset)) {
558 const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(&offset));
559
560 if (op == DW_OP_form_tls_address || op == DW_OP_GNU_push_tls_address)
561 return true;
562 const lldb::offset_t op_arg_size =
563 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
564 if (op_arg_size == LLDB_INVALID_OFFSET)
565 return false;
566 offset += op_arg_size;
567 }
568 return false;
569}
570
572 const DWARFExpression::Delegate *dwarf_cu) const {
573 lldb::offset_t offset = 0;
574 while (m_data.ValidOffset(offset)) {
575 const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(&offset));
576
577 switch (op) {
578 // Implicit locations have no storage in the inferior. Composite locations
579 // might, but we conservatively treat them as non-writable because LLDB
580 // does not write their pieces back.
581 case DW_OP_stack_value:
582 case DW_OP_implicit_value:
583 case DW_OP_implicit_pointer:
584 case DW_OP_piece:
585 case DW_OP_bit_piece:
586 return true;
587 default:
588 break;
589 }
590
591 const lldb::offset_t op_arg_size =
592 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
593 if (op_arg_size == LLDB_INVALID_OFFSET)
594 return false;
595 offset += op_arg_size;
596 }
597 return false;
598}
599
601 const DWARFExpression::Delegate *dwarf_cu,
602 std::function<lldb::addr_t(lldb::addr_t file_addr)> const
603 &link_address_callback) {
604 const uint32_t addr_byte_size = m_data.GetAddressByteSize();
605 // We have to make a copy of the data as we don't know if this data is from a
606 // read only memory mapped buffer, so we duplicate all of the data first,
607 // then modify it, and if all goes well, we then replace the data for this
608 // expression.
609 // Make en encoder that contains a copy of the location expression data so we
610 // can write the address into the buffer using the correct byte order.
611 DataEncoder encoder(m_data.GetDataStart(), m_data.GetByteSize(),
612 m_data.GetByteOrder(), addr_byte_size);
613
614 lldb::offset_t offset = 0;
615 lldb::offset_t const_offset = 0;
616 lldb::addr_t const_value = 0;
617 size_t const_byte_size = 0;
618 while (m_data.ValidOffset(offset)) {
619 const LocationAtom op = static_cast<LocationAtom>(m_data.GetU8(&offset));
620
621 bool decoded_data = false;
622 switch (op) {
623 case DW_OP_const4u:
624 // Remember the const offset in case we later have a
625 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
626 const_offset = offset;
627 const_value = m_data.GetU32(&offset);
628 decoded_data = true;
629 const_byte_size = 4;
630 break;
631
632 case DW_OP_const8u:
633 // Remember the const offset in case we later have a
634 // DW_OP_form_tls_address or DW_OP_GNU_push_tls_address
635 const_offset = offset;
636 const_value = m_data.GetU64(&offset);
637 decoded_data = true;
638 const_byte_size = 8;
639 break;
640
641 case DW_OP_form_tls_address:
642 case DW_OP_GNU_push_tls_address:
643 // DW_OP_form_tls_address and DW_OP_GNU_push_tls_address must be preceded
644 // by a file address on the stack. We assume that DW_OP_const4u or
645 // DW_OP_const8u is used for these values, and we check that the last
646 // opcode we got before either of these was DW_OP_const4u or
647 // DW_OP_const8u. If so, then we can link the value accordingly. For
648 // Darwin, the value in the DW_OP_const4u or DW_OP_const8u is the file
649 // address of a structure that contains a function pointer, the pthread
650 // key and the offset into the data pointed to by the pthread key. So we
651 // must link this address and also set the module of this expression to
652 // the new_module_sp so we can resolve the file address correctly
653 if (const_byte_size > 0) {
654 lldb::addr_t linked_file_addr = link_address_callback(const_value);
655 if (linked_file_addr == LLDB_INVALID_ADDRESS)
656 return false;
657 // Replace the address in the new buffer
658 if (encoder.PutUnsigned(const_offset, const_byte_size,
659 linked_file_addr) == UINT32_MAX)
660 return false;
661 }
662 break;
663
664 default:
665 const_offset = 0;
666 const_value = 0;
667 const_byte_size = 0;
668 break;
669 }
670
671 if (!decoded_data) {
672 const lldb::offset_t op_arg_size =
673 GetOpcodeDataSize(m_data, offset, op, dwarf_cu);
674 if (op_arg_size == LLDB_INVALID_OFFSET)
675 return false;
676 else
677 offset += op_arg_size;
678 }
679 }
680
681 m_data.SetData(encoder.GetDataBuffer());
682 return true;
683}
684
685static llvm::Error Evaluate_DW_OP_entry_value(EvalContext &eval_ctx,
686 llvm::ArrayRef<uint8_t> subexpr) {
688 // DW_OP_entry_value(sub-expr) describes the location a variable had upon
689 // function entry: this variable location is presumed to be optimized out at
690 // the current PC value. The caller of the function may have call site
691 // information that describes an alternate location for the variable (e.g. a
692 // constant literal, or a spilled stack value) in the parent frame.
693 //
694 // Example (this is pseudo-code & pseudo-DWARF, but hopefully illustrative):
695 //
696 // void child(int &sink, int x) {
697 // ...
698 // /* "x" gets optimized out. */
699 //
700 // /* The location of "x" here is: DW_OP_entry_value($reg2). */
701 // ++sink;
702 // }
703 //
704 // void parent() {
705 // int sink;
706 //
707 // /*
708 // * The callsite information emitted here is:
709 // *
710 // * DW_TAG_call_site
711 // * DW_AT_return_pc ... (for "child(sink, 123);")
712 // * DW_TAG_call_site_parameter (for "sink")
713 // * DW_AT_location ($reg1)
714 // * DW_AT_call_value ($SP - 8)
715 // * DW_TAG_call_site_parameter (for "x")
716 // * DW_AT_location ($reg2)
717 // * DW_AT_call_value ($literal 123)
718 // *
719 // * DW_TAG_call_site
720 // * DW_AT_return_pc ... (for "child(sink, 456);")
721 // * ...
722 // */
723 // child(sink, 123);
724 // child(sink, 456);
725 // }
726 //
727 // When the program stops at "++sink" within `child`, the debugger determines
728 // the call site by analyzing the return address. Once the call site is found,
729 // the debugger determines which parameter is referenced by DW_OP_entry_value
730 // and evaluates the corresponding location for that parameter in `parent`.
731
732 // 1. Find the function which pushed the current frame onto the stack.
733 if ((!eval_ctx.exe_ctx || !eval_ctx.exe_ctx->HasTargetScope()) ||
734 !eval_ctx.reg_ctx) {
735 return llvm::createStringError("no exe/reg context");
736 }
737
738 StackFrame *current_frame = eval_ctx.exe_ctx->GetFramePtr();
739 Thread *thread = eval_ctx.exe_ctx->GetThreadPtr();
740 if (!current_frame || !thread)
741 return llvm::createStringError("no current frame/thread");
742
743 Target &target = eval_ctx.exe_ctx->GetTargetRef();
744 StackFrameSP parent_frame = nullptr;
745 addr_t return_pc = LLDB_INVALID_ADDRESS;
746 uint32_t current_frame_idx = current_frame->GetFrameIndex();
747
748 for (uint32_t parent_frame_idx = current_frame_idx + 1;; parent_frame_idx++) {
749 parent_frame = thread->GetStackFrameAtIndex(parent_frame_idx);
750 // If this is null, we're at the end of the stack.
751 if (!parent_frame)
752 break;
753
754 // Record the first valid return address, even if this is an inlined frame,
755 // in order to look up the associated call edge in the first non-inlined
756 // parent frame.
757 if (return_pc == LLDB_INVALID_ADDRESS) {
758 return_pc = parent_frame->GetFrameCodeAddress().GetLoadAddress(&target);
759 LLDB_LOG(log, "immediate ancestor with pc = {0:x}", return_pc);
760 }
761
762 // If we've found an inlined frame, skip it (these have no call site
763 // parameters).
764 if (parent_frame->IsInlined())
765 continue;
766
767 // We've found the first non-inlined parent frame.
768 break;
769 }
770 if (!parent_frame || !parent_frame->GetRegisterContext()) {
771 return llvm::createStringError("no parent frame with reg ctx");
772 }
773
774 Function *parent_func =
775 parent_frame->GetSymbolContext(eSymbolContextFunction).function;
776 if (!parent_func)
777 return llvm::createStringError("no parent function");
778
779 // 2. Find the call edge in the parent function responsible for creating the
780 // current activation.
781 Function *current_func =
782 current_frame->GetSymbolContext(eSymbolContextFunction).function;
783 if (!current_func)
784 return llvm::createStringError("no current function");
785
786 CallEdge *call_edge = nullptr;
787 ModuleList &modlist = target.GetImages();
788 ExecutionContext parent_exe_ctx = *eval_ctx.exe_ctx;
789 parent_exe_ctx.SetFrameSP(parent_frame);
790 if (!parent_frame->IsArtificial()) {
791 // If the parent frame is not artificial, the current activation may be
792 // produced by an ambiguous tail call. In this case, refuse to proceed.
793 call_edge = parent_func->GetCallEdgeForReturnAddress(return_pc, target);
794 if (!call_edge) {
795 return llvm::createStringErrorV(
796 "no call edge for retn-pc = {0:x} in parent frame {1}", return_pc,
797 parent_func->GetName());
798 }
799 Function *callee_func = call_edge->GetCallee(modlist, parent_exe_ctx);
800 if (callee_func != current_func) {
801 return llvm::createStringError(
802 "ambiguous call sequence, can't find real parent frame");
803 }
804 } else {
805 // The StackFrameList solver machinery has deduced that an unambiguous tail
806 // call sequence that produced the current activation. The first edge in
807 // the parent that points to the current function must be valid.
808 for (auto &edge : parent_func->GetTailCallingEdges()) {
809 if (edge->GetCallee(modlist, parent_exe_ctx) == current_func) {
810 call_edge = edge.get();
811 break;
812 }
813 }
814 }
815 if (!call_edge)
816 return llvm::createStringError("no unambiguous edge from parent "
817 "to current function");
818
819 // 3. Attempt to locate the DW_OP_entry_value expression in the set of
820 // available call site parameters. If found, evaluate the corresponding
821 // parameter in the context of the parent frame.
822 const CallSiteParameter *matched_param = nullptr;
823 for (const CallSiteParameter &param : call_edge->GetCallSiteParameters()) {
824 DataExtractor param_subexpr_extractor;
825 if (!param.LocationInCallee.GetExpressionData(param_subexpr_extractor))
826 continue;
827 lldb::offset_t param_subexpr_offset = 0;
828 const void *param_subexpr_data =
829 param_subexpr_extractor.GetData(&param_subexpr_offset, subexpr.size());
830 if (!param_subexpr_data ||
831 param_subexpr_extractor.BytesLeft(param_subexpr_offset) != 0)
832 continue;
833
834 // At this point, the DW_OP_entry_value sub-expression and the callee-side
835 // expression in the call site parameter are known to have the same length.
836 // Check whether they are equal.
837 //
838 // Note that an equality check is sufficient: the contents of the
839 // DW_OP_entry_value subexpression are only used to identify the right call
840 // site parameter in the parent, and do not require any special handling.
841 if (memcmp(subexpr.data(), param_subexpr_data, subexpr.size()) == 0) {
842 matched_param = &param;
843 break;
844 }
845 }
846 if (!matched_param)
847 return llvm::createStringError("no matching call site param found");
848
849 // TODO: Add support for DW_OP_push_object_address within a DW_OP_entry_value
850 // subexpresion whenever llvm does.
851 const DWARFExpressionList &param_expr = matched_param->LocationInCaller;
852
853 // Recurse through the public entry point so the call-site parameter is
854 // evaluated with a fresh EvalContext (separate stack and piece state).
855 llvm::Expected<Value> maybe_result = param_expr.Evaluate(
856 &parent_exe_ctx, parent_frame->GetRegisterContext().get(),
858 /*initial_value_ptr=*/nullptr,
859 /*object_address_ptr=*/nullptr);
860 if (!maybe_result) {
861 LLDB_LOG(log,
862 "Evaluate_DW_OP_entry_value: call site param evaluation failed");
863 return maybe_result.takeError();
864 }
865
866 eval_ctx.stack.push_back(*maybe_result);
867 return llvm::Error::success();
868}
869
870/// Adjust value's ValueType according to the kind of location description.
871static void UpdateValueTypeFromLocationDescription(EvalContext &eval_ctx,
872 LocationDescriptionKind kind,
873 Value *value = nullptr) {
874 // Note that this function is conflating DWARF expressions with
875 // DWARF location descriptions. Perhaps it would be better to define
876 // a wrapper for DWARFExpression::Eval() that deals with DWARF
877 // location descriptions (which consist of one or more DWARF
878 // expressions). But doing this would mean we'd also need factor the
879 // handling of DW_OP_(bit_)piece out of this function.
880 if (eval_ctx.dwarf_cu && eval_ctx.dwarf_cu->GetVersion() >= 4) {
882 const char *log_msg = "DWARF location description kind: %s";
883 switch (kind) {
884 case Empty:
885 LLDB_LOGF(log, log_msg, "Empty");
886 break;
887 case Memory:
888 LLDB_LOGF(log, log_msg, "Memory");
889 if (value->GetValueType() == Value::ValueType::Scalar)
890 value->SetValueType(Value::ValueType::LoadAddress);
891 break;
892 case Register:
893 LLDB_LOGF(log, log_msg, "Register");
894 value->SetValueType(Value::ValueType::Scalar);
895 break;
896 case Implicit:
897 LLDB_LOGF(log, log_msg, "Implicit");
898 if (value->GetValueType() == Value::ValueType::LoadAddress)
899 value->SetValueType(Value::ValueType::Scalar);
900 break;
901 }
902 }
903}
904
905/// Helper function to move common code used to resolve a file address and turn
906/// into a load address.
907///
908/// \param eval_ctx Evaluation context (provides exe_ctx and module_sp).
909/// \param dw_op_type C-style string used to vary the error output
910/// \param file_addr the file address we are trying to resolve and turn into a
911/// load address
912/// \param so_addr out parameter, will be set to load address or section offset
913/// \param check_sectionoffset bool which determines if having a section offset
914/// but not a load address is considerd a success
915/// \returns std::optional containing the load address if resolving and getting
916/// the load address succeed or an empty Optinal otherwise. If
917/// check_sectionoffset is true we consider LLDB_INVALID_ADDRESS a
918/// success if so_addr.IsSectionOffset() is true.
919static llvm::Expected<lldb::addr_t>
920ResolveLoadAddress(EvalContext &eval_ctx, const char *dw_op_type,
921 lldb::addr_t file_addr, Address &so_addr,
922 bool check_sectionoffset = false) {
923 if (!eval_ctx.module_sp)
924 return llvm::createStringError("need module to resolve file address for %s",
925 dw_op_type);
926
927 if (!eval_ctx.module_sp->ResolveFileAddress(file_addr, so_addr))
928 return llvm::createStringError("failed to resolve file address in module");
929
930 const addr_t load_addr = so_addr.GetLoadAddress(eval_ctx.target);
931
932 if (load_addr == LLDB_INVALID_ADDRESS &&
933 (check_sectionoffset && !so_addr.IsSectionOffset()))
934 return llvm::createStringError("failed to resolve load address");
935
936 return load_addr;
937}
938
939/// @brief Helper function to load sized data from a uint8_t buffer.
940///
941/// @param addr_bytes The buffer containing raw data.
942/// @param size_addr_bytes How large is the underlying raw data.
943/// @param byte_order What is the byte order of the underlying data.
944/// @param size How much of the underlying data we want to use.
945/// @return The underlying data converted into a Scalar.
946static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes,
947 size_t size_addr_bytes,
948 ByteOrder byte_order, size_t size) {
949 DataExtractor addr_data(addr_bytes, size_addr_bytes, byte_order, size);
950
951 lldb::offset_t addr_data_offset = 0;
952 if (size <= 8)
953 return addr_data.GetMaxU64(&addr_data_offset, size);
954 return addr_data.GetAddress(&addr_data_offset);
955}
956
957static llvm::Error Evaluate_DW_OP_deref(EvalContext &eval_ctx,
958 LocationAtom opcode, unsigned size,
959 size_t size_addr_bytes) {
960 const char *op_name = DW_OP_value_to_name(opcode);
961 if (eval_ctx.stack.empty())
962 return llvm::createStringError("expression stack empty for %s", op_name);
963
964 if (size == 0 || size > 8)
965 return llvm::createStringError("Invalid address size for %s: %u", op_name,
966 size);
967
968 if (opcode == DW_OP_deref_size && size > size_addr_bytes)
969 return llvm::createStringError(
970 "DW_OP_deref_size size (%u) exceeds address size (%zu)", size,
971 size_addr_bytes);
972
973 // Deref a register or implicit location and truncate the value to `size`
974 // bytes. See the corresponding comment in DW_OP_deref for more details on
975 // why we deref these locations this way.
976 if (eval_ctx.loc_desc_kind == Register ||
977 eval_ctx.loc_desc_kind == Implicit) {
978 // Reset context to default values.
979 eval_ctx.loc_desc_kind = Memory;
980 eval_ctx.stack.back().ClearContext();
981
982 // Truncate the value on top of the stack to *size* bytes then
983 // extend to the size of an address (e.g. generic type).
984 Scalar scalar = eval_ctx.stack.back().GetScalar();
985 scalar.TruncOrExtendTo(size * 8, /*sign=*/false);
986 scalar.TruncOrExtendTo(size_addr_bytes * 8,
987 /*sign=*/false);
988 eval_ctx.stack.back().GetScalar() = scalar;
989 return llvm::Error::success();
990 }
991
992 Value::ValueType value_type = eval_ctx.stack.back().GetValueType();
993 switch (value_type) {
995 void *src = (void *)eval_ctx.stack.back().GetScalar().ULongLong();
996 intptr_t ptr;
997 ::memcpy(&ptr, src, sizeof(void *));
998 // I can't decide whether the size operand should apply to the bytes in
999 // their lldb-host endianness or the target endianness.. I doubt this'll
1000 // ever come up but I'll opt for assuming big endian regardless.
1001 switch (size) {
1002 case 1:
1003 ptr = ptr & 0xff;
1004 break;
1005 case 2:
1006 ptr = ptr & 0xffff;
1007 break;
1008 case 3:
1009 ptr = ptr & 0xffffff;
1010 break;
1011 case 4:
1012 ptr = ptr & 0xffffffff;
1013 break;
1014 // The casts are added to work around the case where intptr_t is a 32-bit
1015 // quantity. Presumably we won't hit the 5..7 cases if (void*) is 32-bits in
1016 // this program.
1017 case 5:
1018 ptr = (intptr_t)ptr & 0xffffffffffULL;
1019 break;
1020 case 6:
1021 ptr = (intptr_t)ptr & 0xffffffffffffULL;
1022 break;
1023 case 7:
1024 ptr = (intptr_t)ptr & 0xffffffffffffffULL;
1025 break;
1026 default:
1027 break;
1028 }
1029 eval_ctx.stack.back().GetScalar() = ptr;
1030 eval_ctx.stack.back().ClearContext();
1031 } break;
1033 auto file_addr =
1034 eval_ctx.stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1035 Address so_addr;
1036 auto maybe_load_addr =
1037 ResolveLoadAddress(eval_ctx, op_name, file_addr, so_addr,
1038 /*check_sectionoffset=*/true);
1039
1040 if (!maybe_load_addr)
1041 return maybe_load_addr.takeError();
1042
1043 addr_t load_addr = *maybe_load_addr;
1044
1045 if (load_addr == LLDB_INVALID_ADDRESS && so_addr.IsSectionOffset()) {
1046 uint8_t addr_bytes[8];
1047 Status error;
1048
1049 if (!eval_ctx.target ||
1050 eval_ctx.target->ReadMemory(so_addr, &addr_bytes, size, error,
1051 /*force_live_memory=*/false) != size)
1052 return llvm::createStringError("failed to dereference pointer for %s: "
1053 "%s\n",
1054 op_name, error.AsCString());
1055
1056 ObjectFile *objfile = eval_ctx.module_sp->GetObjectFile();
1057
1058 eval_ctx.stack.back().GetScalar() = DerefSizeExtractDataHelper(
1059 addr_bytes, size, objfile->GetByteOrder(), size);
1060 eval_ctx.stack.back().ClearContext();
1061 break;
1062 }
1063 eval_ctx.stack.back().GetScalar() = load_addr;
1064 // Fall through to load address promotion code below.
1065 }
1066
1067 [[fallthrough]];
1069 // Promote Scalar to LoadAddress and fall through.
1070 eval_ctx.stack.back().SetValueType(Value::ValueType::LoadAddress);
1071 [[fallthrough]];
1073 if (!eval_ctx.exe_ctx)
1074 return llvm::createStringError("no execution context for %s", op_name);
1075 if (!eval_ctx.process)
1076 return llvm::createStringError("no process for %s", op_name);
1077
1078 lldb::addr_t pointer_addr =
1079 eval_ctx.stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1080 uint8_t addr_bytes[sizeof(lldb::addr_t)];
1081 Status error;
1082
1083 if (eval_ctx.process->ReadMemory(pointer_addr, &addr_bytes, size, error) !=
1084 size)
1085 return llvm::createStringError(
1086 "failed to dereference pointer from 0x%" PRIx64 " for %s: %s\n",
1087 pointer_addr, op_name, error.AsCString());
1088
1089 eval_ctx.stack.back().GetScalar() = DerefSizeExtractDataHelper(
1090 addr_bytes, sizeof(addr_bytes), eval_ctx.process->GetByteOrder(), size);
1091 eval_ctx.stack.back().ClearContext();
1092 } break;
1093
1095 return llvm::createStringError("invalid value for %s", op_name);
1096 }
1097
1098 // Both operations push a generic, address-sized result. The truncation to
1099 // `size` bytes is unnecessary here because the branches above already limit
1100 // the value to `size` bytes; it is only done for consistency with the
1101 // Register/Implicit path above.
1102 eval_ctx.stack.back().GetScalar().TruncOrExtendTo(size * 8, /*sign=*/false);
1103 eval_ctx.stack.back().GetScalar().TruncOrExtendTo(size_addr_bytes * 8,
1104 /*sign=*/false);
1105 return llvm::Error::success();
1106}
1107
1108static llvm::Error Evaluate_DW_OP_piece(EvalContext &eval_ctx,
1109 uint64_t piece_byte_size) {
1110 LocationDescriptionKind piece_locdesc = eval_ctx.loc_desc_kind;
1111 // Reset for the next piece.
1112 eval_ctx.loc_desc_kind = Memory;
1113
1114 if (piece_byte_size == 0)
1115 return llvm::Error::success();
1116
1117 Value curr_piece;
1118
1119 if (eval_ctx.stack.empty()) {
1121 LocationDescriptionKind::Empty);
1122 // In a multi-piece expression, this means that the current piece is
1123 // not available. Fill with zeros for now by resizing the data and
1124 // appending it
1125 curr_piece.ResizeData(piece_byte_size);
1126 // Note that "0" is not a correct value for the unknown bits.
1127 // It would be better to also return a mask of valid bits together
1128 // with the expression result, so the debugger can print missing
1129 // members as "<optimized out>" or something.
1130 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
1131 eval_ctx.pieces.AppendDataToHostBuffer(curr_piece);
1132 } else {
1133 Status error;
1134 // Extract the current piece into "curr_piece"
1135 Value curr_piece_source_value(eval_ctx.stack.back());
1136 eval_ctx.stack.pop_back();
1137 UpdateValueTypeFromLocationDescription(eval_ctx, piece_locdesc,
1138 &curr_piece_source_value);
1139
1140 const Value::ValueType curr_piece_source_value_type =
1141 curr_piece_source_value.GetValueType();
1142 Scalar &scalar = curr_piece_source_value.GetScalar();
1144 switch (curr_piece_source_value_type) {
1146 return llvm::createStringError("invalid value type");
1148 if (eval_ctx.target) {
1149 curr_piece_source_value.ConvertToLoadAddress(eval_ctx.module_sp.get(),
1150 eval_ctx.target);
1151 addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1152 } else {
1153 return llvm::createStringError(
1154 "unable to convert file address 0x%" PRIx64 " to load address "
1155 "for DW_OP_piece(%" PRIu64 "): "
1156 "no target available",
1157 addr, piece_byte_size);
1158 }
1159 [[fallthrough]];
1161 if (eval_ctx.target) {
1162 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
1163 if (eval_ctx.target->ReadMemory(
1164 Address(addr), curr_piece.GetBuffer().GetBytes(),
1165 piece_byte_size, error,
1166 /*force_live_memory=*/false) != piece_byte_size) {
1167 const char *addr_type =
1168 (curr_piece_source_value_type == Value::ValueType::LoadAddress)
1169 ? "load"
1170 : "file";
1171 return llvm::createStringError(
1172 "failed to read memory DW_OP_piece(%" PRIu64
1173 ") from %s address 0x%" PRIx64,
1174 piece_byte_size, addr_type, addr);
1175 }
1176 } else {
1177 return llvm::createStringError(
1178 "failed to resize the piece memory buffer for "
1179 "DW_OP_piece(%" PRIu64 ")",
1180 piece_byte_size);
1181 }
1182 }
1183 } break;
1185 return llvm::createStringError(
1186 "failed to read memory DW_OP_piece(%" PRIu64
1187 ") from host address 0x%" PRIx64,
1188 piece_byte_size, addr);
1189 } break;
1190
1192 uint32_t bit_size = piece_byte_size * 8;
1193 uint32_t bit_offset = 0;
1194 if (!scalar.ExtractBitfield(bit_size, bit_offset)) {
1195 return llvm::createStringError(
1196 "unable to extract %" PRIu64 " bytes from a %" PRIu64
1197 " byte scalar value.",
1198 piece_byte_size,
1199 (uint64_t)curr_piece_source_value.GetScalar().GetByteSize());
1200 }
1201
1202 // We have seen a case where we have expression like:
1203 // DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x28
1204 // here we are assuming the compiler was trying to zero
1205 // extend the value that we should append to the buffer.
1206 scalar.TruncOrExtendTo(bit_size, /*sign=*/false);
1207 curr_piece.GetScalar() = scalar;
1208 } break;
1209 }
1210
1211 // Check if this is the first piece?
1212 if (eval_ctx.op_piece_offset == 0) {
1213 // This is the first piece, we should push it back onto the stack
1214 // so subsequent pieces will be able to access this piece and add
1215 // to it.
1216 if (eval_ctx.pieces.AppendDataToHostBuffer(curr_piece) == 0) {
1217 return llvm::createStringError("failed to append piece data");
1218 }
1219 } else {
1220 // If this is the second or later piece there should be a value on
1221 // the stack.
1222 if (eval_ctx.pieces.GetBuffer().GetByteSize() !=
1223 eval_ctx.op_piece_offset) {
1224 return llvm::createStringError(
1225 "DW_OP_piece for offset %" PRIu64
1226 " but top of stack is of size %" PRIu64,
1227 eval_ctx.op_piece_offset,
1228 eval_ctx.pieces.GetBuffer().GetByteSize());
1229 }
1230
1231 if (eval_ctx.pieces.AppendDataToHostBuffer(curr_piece) == 0)
1232 return llvm::createStringError("failed to append piece data");
1233 }
1234 }
1235 eval_ctx.op_piece_offset += piece_byte_size;
1236 return llvm::Error::success();
1237}
1238
1239static llvm::Error Evaluate_DW_OP_convert(EvalContext &eval_ctx,
1240 uint64_t relative_die_offset) {
1241 uint64_t bit_size;
1242 bool sign;
1243 if (relative_die_offset == 0) {
1244 // The generic type has the size of an address on the target
1245 // machine and an unspecified signedness. Scalar has no
1246 // "unspecified signedness", so we use unsigned types.
1247 if (!eval_ctx.module_sp)
1248 return llvm::createStringError("no module");
1249 sign = false;
1250 bit_size = eval_ctx.module_sp->GetArchitecture().GetAddressByteSize() * 8;
1251 if (!bit_size)
1252 return llvm::createStringError("unspecified architecture");
1253 } else {
1254 if (!eval_ctx.dwarf_cu)
1255 return llvm::createStringError(
1256 "DW_OP_convert with a DIE offset requires a DWARF unit");
1257 auto bit_size_sign_or_err =
1258 eval_ctx.dwarf_cu->GetDIEBitSizeAndSign(relative_die_offset);
1259 if (!bit_size_sign_or_err)
1260 return bit_size_sign_or_err.takeError();
1261 bit_size = bit_size_sign_or_err->first;
1262 sign = bit_size_sign_or_err->second;
1263 }
1264 eval_ctx.stack.back().GetScalar().TruncOrExtendTo(bit_size, sign);
1265 return llvm::Error::success();
1266}
1267
1268static llvm::Error Evaluate_DW_OP_form_tls_address(EvalContext &eval_ctx,
1269 LocationAtom opcode) {
1270 if (eval_ctx.stack.empty())
1271 return llvm::createStringError("%s needs an argument",
1272 opcode == DW_OP_form_tls_address
1273 ? "DW_OP_form_tls_address"
1274 : "DW_OP_GNU_push_tls_address");
1275
1276 if (!eval_ctx.exe_ctx || !eval_ctx.module_sp)
1277 return llvm::createStringError("no context to evaluate TLS within");
1278
1279 Thread *thread = eval_ctx.exe_ctx->GetThreadPtr();
1280 if (!thread)
1281 return llvm::createStringError("no thread to evaluate TLS within");
1282
1283 // Lookup the TLS block address for this thread and module.
1284 const addr_t tls_file_addr =
1285 eval_ctx.stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1286 const addr_t tls_load_addr =
1287 thread->GetThreadLocalData(eval_ctx.module_sp, tls_file_addr);
1288
1289 if (tls_load_addr == LLDB_INVALID_ADDRESS)
1290 return llvm::createStringError(
1291 "no TLS data currently exists for this thread");
1292
1293 eval_ctx.stack.back().GetScalar() = tls_load_addr;
1294 eval_ctx.stack.back().SetValueType(Value::ValueType::LoadAddress);
1295 return llvm::Error::success();
1296}
1297
1298static llvm::Error Evaluate_DW_OP_fbreg(EvalContext &eval_ctx,
1299 int64_t fbreg_offset) {
1300 if (!eval_ctx.exe_ctx)
1301 return llvm::createStringError("NULL execution context for DW_OP_fbreg");
1302 if (!eval_ctx.frame)
1303 return llvm::createStringError(
1304 "invalid stack frame in context for DW_OP_fbreg opcode");
1305
1306 Scalar value;
1307 if (llvm::Error err = eval_ctx.frame->GetFrameBaseValue(value))
1308 return err;
1309 value += fbreg_offset;
1310 eval_ctx.stack.push_back(value);
1311 eval_ctx.stack.back().SetValueType(Value::ValueType::LoadAddress);
1312 return llvm::Error::success();
1313}
1314
1315static llvm::Error Evaluate_DW_OP_call_frame_cfa(EvalContext &eval_ctx) {
1316 if (!eval_ctx.frame)
1317 return llvm::createStringError(
1318 "invalid stack frame in context for DW_OP_call_frame_cfa opcode");
1319
1320 // Note that we don't have to parse FDEs because this DWARF expression
1321 // is commonly evaluated with a valid stack frame.
1322 StackID id = eval_ctx.frame->GetStackID();
1323 addr_t cfa = id.GetCallFrameAddressWithMetadata();
1324 if (cfa == LLDB_INVALID_ADDRESS)
1325 return llvm::createStringError("stack frame does not include a canonical "
1326 "frame address for DW_OP_call_frame_cfa "
1327 "opcode");
1328
1329 eval_ctx.stack.push_back(Scalar(cfa));
1330 eval_ctx.stack.back().SetValueType(Value::ValueType::LoadAddress);
1331 return llvm::Error::success();
1332}
1333
1334static llvm::Error CheckScalarOperandsHaveSameType(const Scalar &lhs,
1335 const Scalar &rhs,
1336 LocationAtom opcode,
1337 size_t address_size) {
1338 auto mismatch = [&](const char *what) {
1339 return llvm::createStringError("%s requires operands to have the same %s",
1340 DW_OP_value_to_name(opcode), what);
1341 };
1342
1343 // Scalar does not preserve the original DWARF DIE, but it does carry the
1344 // pieces of base-type information used by the evaluator: kind, size, and
1345 // integer signedness.
1346 if (lhs.GetType() != rhs.GetType())
1347 return mismatch("type");
1348
1349 // Only integer scalars have signedness. Non-integer operands (e.g. floats)
1350 // have no further scalar type information to compare once kind and size
1351 // match.
1352 if (lhs.GetType() != Scalar::e_int) {
1353 if (lhs.GetByteSize() != rhs.GetByteSize())
1354 return mismatch("size");
1355 return llvm::Error::success();
1356 }
1357
1358 // DWARF generic values are address-sized integers with unspecified
1359 // signedness. LLDB does not explicitly preserve genericness on the
1360 // expression stack, so treat integers at least as wide as the generic type
1361 // as potentially generic and compatible with one another, regardless of
1362 // exact width or signedness. This keeps common expressions working: e.g.
1363 // DW_OP_breg produces a register-sized value while DW_OP_const* produces an
1364 // address-sized one, yet both are meant to be generic. DW_OP_constu and
1365 // DW_OP_consts also do not always use to_generic due to
1366 // https://github.com/llvm/llvm-project/issues/47431. A precise fix would
1367 // require tracking genericness directly, which is a larger type-system
1368 // change.
1369 if (address_size != 0 && lhs.GetByteSize() >= address_size &&
1370 rhs.GetByteSize() >= address_size)
1371 return llvm::Error::success();
1372
1373 // For non-generic integer operands, size and signedness are part of the
1374 // base-type information preserved by Scalar, so require them to match.
1375 if (lhs.GetByteSize() != rhs.GetByteSize())
1376 return mismatch("size");
1377 if (lhs.IsSigned() != rhs.IsSigned())
1378 return mismatch("signedness");
1379
1380 return llvm::Error::success();
1381}
1382
1383llvm::Expected<Value> DWARFExpression::Evaluate(
1384 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
1385 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
1386 const DWARFExpression::Delegate *dwarf_cu,
1387 const lldb::RegisterKind reg_kind, const Value *initial_value_ptr,
1388 const Value *object_address_ptr) {
1389 uint32_t address_size = opcodes.GetAddressByteSize();
1390 llvm::DataExtractor expr_data = opcodes.GetAsLLVM();
1391 llvm::DWARFExpression expr(expr_data, address_size);
1392
1393 if (expr_data.size() == 0)
1394 return llvm::createStringError(
1395 "no location, value may have been optimized out");
1396
1397 EvalContext eval_ctx(exe_ctx, reg_ctx, std::move(module_sp), dwarf_cu,
1398 reg_kind, initial_value_ptr, object_address_ptr);
1399
1400 Stack &stack = eval_ctx.stack;
1401
1402 if (initial_value_ptr)
1403 stack.push_back(*initial_value_ptr);
1404
1405 Value tmp;
1406 uint32_t reg_num;
1407
1409 // A generic type is "an integral type that has the size of an address and an
1410 // unspecified signedness". For now, just use the signedness of the operand.
1411 // TODO: Implement a real typed stack, and store the genericness of the value
1412 // there.
1413 auto to_generic = [&](auto v) {
1414 // TODO: Avoid implicit trunc?
1415 // See https://github.com/llvm/llvm-project/issues/112510.
1416 bool is_signed = std::is_signed<decltype(v)>::value;
1417 return Scalar(llvm::APSInt(
1418 llvm::APInt(8 * address_size, v, is_signed, /*implicitTrunc=*/true),
1419 !is_signed));
1420 };
1421
1422 llvm::DWARFExpression::iterator op = expr.begin(), op_end = expr.end();
1423 while (op != op_end) {
1424 const uint64_t op_offset = op.getOffset();
1425 const LocationAtom opcode = static_cast<LocationAtom>(op->getCode());
1426
1427 if (log && log->GetVerbose()) {
1428 size_t count = stack.size();
1429 LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
1430 static_cast<uint64_t>(count));
1431 for (size_t i = 0; i < count; ++i) {
1432 StreamString new_value;
1433 new_value.Printf("[%" PRIu64 "]", static_cast<uint64_t>(i));
1434 stack[i].Dump(&new_value);
1435 LLDB_LOGF(log, " %s", new_value.GetData());
1436 }
1437 LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
1438 DW_OP_value_to_name(opcode));
1439 }
1440
1441 if (std::optional<unsigned> arity = OperationArity(opcode)) {
1442 if (stack.size() < *arity)
1443 return llvm::createStringError(
1444 "%s needs at least %d stack entries (stack has %d entries)",
1445 DW_OP_value_to_name(opcode), *arity, stack.size());
1446 }
1447
1448 switch (opcode) {
1449 case DW_OP_addr:
1450 stack.push_back(to_generic(op->getRawOperand(0)));
1451 stack.back().SetValueType(Value::ValueType::FileAddress);
1452 break;
1453
1454 case DW_OP_deref: {
1455 size_t size = address_size;
1456 if (llvm::Error err = Evaluate_DW_OP_deref(eval_ctx, opcode, size, size))
1457 return err;
1458 } break;
1459
1460 case DW_OP_deref_size: {
1461 size_t size = op->getRawOperand(0);
1462 if (llvm::Error err =
1463 Evaluate_DW_OP_deref(eval_ctx, opcode, size, address_size))
1464 return err;
1465 } break;
1466
1467 case DW_OP_const1u:
1468 stack.push_back(to_generic(op->getRawOperand(0)));
1469 break;
1470 case DW_OP_const1s:
1471 stack.push_back(to_generic(static_cast<int8_t>(op->getRawOperand(0))));
1472 break;
1473 case DW_OP_const2u:
1474 stack.push_back(to_generic(op->getRawOperand(0)));
1475 break;
1476 case DW_OP_const2s:
1477 stack.push_back(to_generic(static_cast<int16_t>(op->getRawOperand(0))));
1478 break;
1479 case DW_OP_const4u:
1480 stack.push_back(to_generic(op->getRawOperand(0)));
1481 break;
1482 case DW_OP_const4s:
1483 stack.push_back(to_generic(static_cast<int32_t>(op->getRawOperand(0))));
1484 break;
1485 case DW_OP_const8u:
1486 stack.push_back(to_generic(op->getRawOperand(0)));
1487 break;
1488 case DW_OP_const8s:
1489 stack.push_back(to_generic(static_cast<int64_t>(op->getRawOperand(0))));
1490 break;
1491 case DW_OP_constu:
1492 stack.push_back(to_generic(op->getRawOperand(0)));
1493 break;
1494 case DW_OP_consts:
1495 stack.push_back(to_generic(static_cast<int64_t>(op->getRawOperand(0))));
1496 break;
1497
1498 case DW_OP_dup:
1499 if (stack.empty()) {
1500 return llvm::createStringError("expression stack empty for DW_OP_dup");
1501 } else
1502 stack.push_back(stack.back());
1503 break;
1504
1505 case DW_OP_drop:
1506 if (stack.empty()) {
1507 return llvm::createStringError("expression stack empty for DW_OP_drop");
1508 } else
1509 stack.pop_back();
1510 break;
1511
1512 case DW_OP_over:
1513 stack.push_back(stack[stack.size() - 2]);
1514 break;
1515
1516 case DW_OP_pick: {
1517 uint8_t pick_idx = op->getRawOperand(0);
1518 if (pick_idx < stack.size())
1519 stack.push_back(stack[stack.size() - 1 - pick_idx]);
1520 else {
1521 return llvm::createStringError(
1522 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1523 }
1524 } break;
1525
1526 case DW_OP_swap:
1527 tmp = stack.back();
1528 stack.back() = stack[stack.size() - 2];
1529 stack[stack.size() - 2] = tmp;
1530 break;
1531
1532 case DW_OP_rot: {
1533 size_t last_idx = stack.size() - 1;
1534 Value old_top = stack[last_idx];
1535 stack[last_idx] = stack[last_idx - 1];
1536 stack[last_idx - 1] = stack[last_idx - 2];
1537 stack[last_idx - 2] = old_top;
1538 } break;
1539
1540 case DW_OP_abs:
1541 if (!stack.back().GetScalar().AbsoluteValue()) {
1542 return llvm::createStringError(
1543 "failed to take the absolute value of the first stack item");
1544 }
1545 break;
1546
1547 case DW_OP_and:
1548 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1549 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1550 opcode, address_size))
1551 return err;
1552 tmp = stack.back();
1553 stack.pop_back();
1554 stack.back().GetScalar() = stack.back().GetScalar() & tmp.GetScalar();
1555 break;
1556
1557 case DW_OP_div: {
1558 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1559 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1560 opcode, address_size))
1561 return err;
1562 tmp = stack.back();
1563 if (tmp.GetScalar().IsZero())
1564 return llvm::createStringError("divide by zero");
1565
1566 stack.pop_back();
1567 Scalar divisor, dividend;
1568 divisor = tmp.GetScalar();
1569 dividend = stack.back().GetScalar();
1570 divisor.MakeSigned();
1571 dividend.MakeSigned();
1572 stack.back() = dividend / divisor;
1573
1574 if (!stack.back().GetScalar().IsValid())
1575 return llvm::createStringError("divide failed");
1576 } break;
1577
1578 case DW_OP_minus:
1579 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1580 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1581 opcode, address_size))
1582 return err;
1583 tmp = stack.back();
1584 stack.pop_back();
1585 stack.back().GetScalar() = stack.back().GetScalar() - tmp.GetScalar();
1586 break;
1587
1588 case DW_OP_mod:
1589 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1590 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1591 opcode, address_size))
1592 return err;
1593 tmp = stack.back();
1594 stack.pop_back();
1595 stack.back().GetScalar() = stack.back().GetScalar() % tmp.GetScalar();
1596 break;
1597
1598 case DW_OP_mul:
1599 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1600 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1601 opcode, address_size))
1602 return err;
1603 tmp = stack.back();
1604 stack.pop_back();
1605 stack.back().GetScalar() = stack.back().GetScalar() * tmp.GetScalar();
1606 break;
1607
1608 case DW_OP_neg:
1609 if (!stack.back().GetScalar().UnaryNegate())
1610 return llvm::createStringError("unary negate failed");
1611 break;
1612
1613 case DW_OP_not:
1614 if (!stack.back().GetScalar().OnesComplement())
1615 return llvm::createStringError("logical NOT failed");
1616 break;
1617
1618 case DW_OP_or:
1619 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1620 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1621 opcode, address_size))
1622 return err;
1623 tmp = stack.back();
1624 stack.pop_back();
1625 stack.back().GetScalar() = stack.back().GetScalar() | tmp.GetScalar();
1626 break;
1627
1628 case DW_OP_plus:
1629 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1630 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1631 opcode, address_size))
1632 return err;
1633 tmp = stack.back();
1634 stack.pop_back();
1635 stack.back().GetScalar() += tmp.GetScalar();
1636 break;
1637
1638 case DW_OP_plus_uconst: {
1639 const uint64_t uconst_value = op->getRawOperand(0);
1640 Scalar &operand = stack.back().GetScalar();
1641 Scalar addend(uconst_value);
1642 // The addend is interpreted as the same type as the popped operand
1643 // (DWARF v5, 2.5.1.4). Give it the operand's exact integer type so
1644 // the addition keeps the operand's width and wraparound semantics
1645 // instead of promoting to a 64-bit unsigned value.
1646 if (operand.GetType() == Scalar::e_int)
1647 addend.TruncOrExtendTo(operand.GetAPSInt().getBitWidth(),
1648 operand.GetAPSInt().isSigned());
1649 operand += addend;
1650 if (!operand.IsValid())
1651 return llvm::createStringError("DW_OP_plus_uconst failed");
1652 } break;
1653
1654 case DW_OP_shl:
1655 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1656 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1657 opcode, address_size))
1658 return err;
1659 tmp = stack.back();
1660 stack.pop_back();
1661 stack.back().GetScalar() <<= tmp.GetScalar();
1662 break;
1663
1664 case DW_OP_shr:
1665 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1666 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1667 opcode, address_size))
1668 return err;
1669 tmp = stack.back();
1670 stack.pop_back();
1671 if (!stack.back().GetScalar().ShiftRightLogical(tmp.GetScalar()))
1672 return llvm::createStringError("DW_OP_shr failed");
1673 break;
1674
1675 case DW_OP_shra:
1676 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1677 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1678 opcode, address_size))
1679 return err;
1680 tmp = stack.back();
1681 stack.pop_back();
1682 stack.back().GetScalar() >>= tmp.GetScalar();
1683 break;
1684
1685 case DW_OP_xor:
1686 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1687 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1688 opcode, address_size))
1689 return err;
1690 tmp = stack.back();
1691 stack.pop_back();
1692 stack.back().GetScalar() = stack.back().GetScalar() ^ tmp.GetScalar();
1693 break;
1694
1695 case DW_OP_skip: {
1696 int16_t skip_offset = static_cast<int16_t>(op->getRawOperand(0));
1697 lldb::offset_t new_offset = op->getEndOffset() + skip_offset;
1698 // New offset can point at the end of the data, in this case we should
1699 // terminate the DWARF expression evaluation (will happen in the loop
1700 // condition).
1701 if (new_offset <= expr_data.size()) {
1702 op = op.skipBytes(skip_offset);
1703 continue;
1704 }
1705 return llvm::createStringErrorV(
1706 "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}",
1707 op->getEndOffset(), skip_offset, expr_data.size());
1708 }
1709
1710 case DW_OP_bra: {
1711 tmp = stack.back();
1712 stack.pop_back();
1713 int16_t bra_offset = static_cast<int16_t>(op->getRawOperand(0));
1714 Scalar zero(0);
1715 if (tmp.GetScalar() != zero) {
1716 lldb::offset_t new_offset = op->getEndOffset() + bra_offset;
1717 // New offset can point at the end of the data, in this case we should
1718 // terminate the DWARF expression evaluation (will happen in the loop
1719 // condition).
1720 if (new_offset <= expr_data.size()) {
1721 op = op.skipBytes(bra_offset);
1722 continue;
1723 }
1724 return llvm::createStringErrorV(
1725 "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}",
1726 op->getEndOffset(), bra_offset, expr_data.size());
1727 }
1728 } break;
1729
1730 case DW_OP_eq:
1731 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1732 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1733 opcode, address_size))
1734 return err;
1735 tmp = stack.back();
1736 stack.pop_back();
1737 stack.back().GetScalar() =
1738 to_generic(stack.back().GetScalar() == tmp.GetScalar());
1739 break;
1740
1741 case DW_OP_ge:
1742 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1743 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1744 opcode, address_size))
1745 return err;
1746 tmp = stack.back();
1747 stack.pop_back();
1748 stack.back().GetScalar() =
1749 to_generic(stack.back().GetScalar() >= tmp.GetScalar());
1750 break;
1751
1752 case DW_OP_gt:
1753 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1754 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1755 opcode, address_size))
1756 return err;
1757 tmp = stack.back();
1758 stack.pop_back();
1759 stack.back().GetScalar() =
1760 to_generic(stack.back().GetScalar() > tmp.GetScalar());
1761 break;
1762
1763 case DW_OP_le:
1764 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1765 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1766 opcode, address_size))
1767 return err;
1768 tmp = stack.back();
1769 stack.pop_back();
1770 stack.back().GetScalar() =
1771 to_generic(stack.back().GetScalar() <= tmp.GetScalar());
1772 break;
1773
1774 case DW_OP_lt:
1775 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1776 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1777 opcode, address_size))
1778 return err;
1779 tmp = stack.back();
1780 stack.pop_back();
1781 stack.back().GetScalar() =
1782 to_generic(stack.back().GetScalar() < tmp.GetScalar());
1783 break;
1784
1785 case DW_OP_ne:
1786 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1787 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1788 opcode, address_size))
1789 return err;
1790 tmp = stack.back();
1791 stack.pop_back();
1792 stack.back().GetScalar() =
1793 to_generic(stack.back().GetScalar() != tmp.GetScalar());
1794 break;
1795
1796 case DW_OP_lit0:
1797 case DW_OP_lit1:
1798 case DW_OP_lit2:
1799 case DW_OP_lit3:
1800 case DW_OP_lit4:
1801 case DW_OP_lit5:
1802 case DW_OP_lit6:
1803 case DW_OP_lit7:
1804 case DW_OP_lit8:
1805 case DW_OP_lit9:
1806 case DW_OP_lit10:
1807 case DW_OP_lit11:
1808 case DW_OP_lit12:
1809 case DW_OP_lit13:
1810 case DW_OP_lit14:
1811 case DW_OP_lit15:
1812 case DW_OP_lit16:
1813 case DW_OP_lit17:
1814 case DW_OP_lit18:
1815 case DW_OP_lit19:
1816 case DW_OP_lit20:
1817 case DW_OP_lit21:
1818 case DW_OP_lit22:
1819 case DW_OP_lit23:
1820 case DW_OP_lit24:
1821 case DW_OP_lit25:
1822 case DW_OP_lit26:
1823 case DW_OP_lit27:
1824 case DW_OP_lit28:
1825 case DW_OP_lit29:
1826 case DW_OP_lit30:
1827 case DW_OP_lit31:
1828 stack.push_back(to_generic(opcode - DW_OP_lit0));
1829 break;
1830
1831 case DW_OP_reg0:
1832 case DW_OP_reg1:
1833 case DW_OP_reg2:
1834 case DW_OP_reg3:
1835 case DW_OP_reg4:
1836 case DW_OP_reg5:
1837 case DW_OP_reg6:
1838 case DW_OP_reg7:
1839 case DW_OP_reg8:
1840 case DW_OP_reg9:
1841 case DW_OP_reg10:
1842 case DW_OP_reg11:
1843 case DW_OP_reg12:
1844 case DW_OP_reg13:
1845 case DW_OP_reg14:
1846 case DW_OP_reg15:
1847 case DW_OP_reg16:
1848 case DW_OP_reg17:
1849 case DW_OP_reg18:
1850 case DW_OP_reg19:
1851 case DW_OP_reg20:
1852 case DW_OP_reg21:
1853 case DW_OP_reg22:
1854 case DW_OP_reg23:
1855 case DW_OP_reg24:
1856 case DW_OP_reg25:
1857 case DW_OP_reg26:
1858 case DW_OP_reg27:
1859 case DW_OP_reg28:
1860 case DW_OP_reg29:
1861 case DW_OP_reg30:
1862 case DW_OP_reg31: {
1863 eval_ctx.loc_desc_kind = Register;
1864 reg_num = opcode - DW_OP_reg0;
1865
1866 if (llvm::Error err = ReadRegisterValueAsScalar(
1867 eval_ctx.reg_ctx, eval_ctx.reg_kind, reg_num, tmp))
1868 return err;
1869 stack.push_back(tmp);
1870 } break;
1871 case DW_OP_regx: {
1872 eval_ctx.loc_desc_kind = Register;
1873 reg_num = op->getRawOperand(0);
1874 Status read_err;
1875 if (llvm::Error err = ReadRegisterValueAsScalar(
1876 eval_ctx.reg_ctx, eval_ctx.reg_kind, reg_num, tmp))
1877 return err;
1878 stack.push_back(tmp);
1879 } break;
1880
1881 case DW_OP_breg0:
1882 case DW_OP_breg1:
1883 case DW_OP_breg2:
1884 case DW_OP_breg3:
1885 case DW_OP_breg4:
1886 case DW_OP_breg5:
1887 case DW_OP_breg6:
1888 case DW_OP_breg7:
1889 case DW_OP_breg8:
1890 case DW_OP_breg9:
1891 case DW_OP_breg10:
1892 case DW_OP_breg11:
1893 case DW_OP_breg12:
1894 case DW_OP_breg13:
1895 case DW_OP_breg14:
1896 case DW_OP_breg15:
1897 case DW_OP_breg16:
1898 case DW_OP_breg17:
1899 case DW_OP_breg18:
1900 case DW_OP_breg19:
1901 case DW_OP_breg20:
1902 case DW_OP_breg21:
1903 case DW_OP_breg22:
1904 case DW_OP_breg23:
1905 case DW_OP_breg24:
1906 case DW_OP_breg25:
1907 case DW_OP_breg26:
1908 case DW_OP_breg27:
1909 case DW_OP_breg28:
1910 case DW_OP_breg29:
1911 case DW_OP_breg30:
1912 case DW_OP_breg31: {
1913 reg_num = opcode - DW_OP_breg0;
1914 if (llvm::Error err = ReadRegisterValueAsScalar(
1915 eval_ctx.reg_ctx, eval_ctx.reg_kind, reg_num, tmp))
1916 return err;
1917
1918 int64_t breg_offset = op->getRawOperand(0);
1919 tmp.GetScalar() = to_generic(tmp.GetScalar().ULongLong());
1920 tmp.GetScalar() += to_generic(breg_offset);
1921 tmp.ClearContext();
1922 stack.push_back(tmp);
1923 stack.back().SetValueType(Value::ValueType::LoadAddress);
1924 } break;
1925 case DW_OP_bregx: {
1926 reg_num = op->getRawOperand(0);
1927 if (llvm::Error err = ReadRegisterValueAsScalar(
1928 eval_ctx.reg_ctx, eval_ctx.reg_kind, reg_num, tmp))
1929 return err;
1930
1931 int64_t breg_offset = op->getRawOperand(1);
1932 tmp.GetScalar() = to_generic(tmp.GetScalar().ULongLong());
1933 tmp.GetScalar() += to_generic(breg_offset);
1934 tmp.ClearContext();
1935 stack.push_back(tmp);
1936 stack.back().SetValueType(Value::ValueType::LoadAddress);
1937 } break;
1938
1939 case DW_OP_fbreg:
1940 if (llvm::Error err =
1941 Evaluate_DW_OP_fbreg(eval_ctx, op->getRawOperand(0)))
1942 return err;
1943 stack.back().GetScalar() =
1944 to_generic(stack.back().GetScalar().ULongLong());
1945 break;
1946
1947 case DW_OP_nop:
1948 break;
1949
1950 case DW_OP_piece: {
1951 if (llvm::Error err =
1952 Evaluate_DW_OP_piece(eval_ctx, op->getRawOperand(0)))
1953 return err;
1954 } break;
1955
1956 case DW_OP_bit_piece:
1957 if (stack.size() < 1) {
1959 LocationDescriptionKind::Empty);
1960 // Reset for the next piece.
1961 eval_ctx.loc_desc_kind = Memory;
1962 return llvm::createStringError(
1963 "expression stack needs at least 1 item for DW_OP_bit_piece");
1964 } else {
1965 UpdateValueTypeFromLocationDescription(eval_ctx, eval_ctx.loc_desc_kind,
1966 &stack.back());
1967 // Reset for the next piece.
1968 eval_ctx.loc_desc_kind = Memory;
1969 const uint64_t piece_bit_size = op->getRawOperand(0);
1970 const uint64_t piece_bit_offset = op->getRawOperand(1);
1971 switch (stack.back().GetValueType()) {
1973 return llvm::createStringError(
1974 "unable to extract bit value from invalid value");
1976 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
1977 piece_bit_offset)) {
1978 return llvm::createStringError(
1979 "unable to extract %" PRIu64 " bit value with %" PRIu64
1980 " bit offset from a %" PRIu64 " bit scalar value.",
1981 piece_bit_size, piece_bit_offset,
1982 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
1983 }
1984 } break;
1985
1989 return llvm::createStringError(
1990 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
1991 ", bit_offset = %" PRIu64 ") from an address value.",
1992 piece_bit_size, piece_bit_offset);
1993 }
1994 }
1995 break;
1996
1997 case DW_OP_implicit_value: {
1998 eval_ctx.loc_desc_kind = Implicit;
1999
2000 // The second operand is a sequence of bytes of the length specified by
2001 // the first operand. LLVM represents it as an offset to that sequence.
2002 const uint64_t block_size = op->getRawOperand(0);
2003 uint64_t block_offset = op->getRawOperand(1);
2004
2005 llvm::Error error = llvm::Error::success();
2006 llvm::StringRef block_data =
2007 expr_data.getBytes(&block_offset, block_size, &error);
2008
2009 if (error)
2010 return error;
2011
2012 Value result(block_data.data(), block_data.size());
2013 stack.push_back(result);
2014 break;
2015 }
2016
2017 case DW_OP_implicit_pointer: {
2018 eval_ctx.loc_desc_kind = Implicit;
2019 return llvm::createStringError("could not evaluate %s",
2020 DW_OP_value_to_name(opcode));
2021 }
2022
2023 case DW_OP_push_object_address:
2024 if (eval_ctx.object_address_ptr)
2025 stack.push_back(*eval_ctx.object_address_ptr);
2026 else {
2027 return llvm::createStringError("DW_OP_push_object_address used without "
2028 "specifying an object address");
2029 }
2030 break;
2031
2032 case DW_OP_stack_value:
2033 eval_ctx.loc_desc_kind = Implicit;
2034 stack.back().SetValueType(Value::ValueType::Scalar);
2035 break;
2036
2037 case DW_OP_convert:
2038 if (llvm::Error err =
2039 Evaluate_DW_OP_convert(eval_ctx, op->getRawOperand(0)))
2040 return err;
2041 break;
2042
2043 case DW_OP_call_frame_cfa:
2044 if (llvm::Error err = Evaluate_DW_OP_call_frame_cfa(eval_ctx))
2045 return err;
2046 break;
2047
2048 case DW_OP_form_tls_address:
2049 case DW_OP_GNU_push_tls_address:
2050 if (llvm::Error err = Evaluate_DW_OP_form_tls_address(eval_ctx, opcode))
2051 return err;
2052 break;
2053
2054 case DW_OP_addrx:
2055 case DW_OP_GNU_addr_index: {
2056 if (!eval_ctx.dwarf_cu)
2057 return llvm::createStringError("DW_OP_GNU_addr_index found without a "
2058 "compile unit being specified");
2059 uint64_t index = op->getRawOperand(0);
2060 lldb::addr_t value =
2061 eval_ctx.dwarf_cu->ReadAddressFromDebugAddrSection(index);
2062 stack.push_back(to_generic(value));
2063 stack.back().SetValueType(Value::ValueType::FileAddress);
2064 } break;
2065
2066 case DW_OP_GNU_const_index: {
2067 if (!eval_ctx.dwarf_cu) {
2068 return llvm::createStringError("DW_OP_GNU_const_index found without a "
2069 "compile unit being specified");
2070 }
2071 uint64_t index = op->getRawOperand(0);
2072 lldb::addr_t value =
2073 eval_ctx.dwarf_cu->ReadAddressFromDebugAddrSection(index);
2074 stack.push_back(to_generic(value));
2075 } break;
2076
2077 case DW_OP_GNU_entry_value:
2078 case DW_OP_entry_value: {
2079 // Technically, DW_OP_entry_value has two operands, but LLVM represents
2080 // it as a single-operand operation (bug?). We can deal with this: the
2081 // second operand immediately follows the first, but have to be careful
2082 // when advancing the iterator, see the comment below.
2083 const uint64_t block_size = op->getRawOperand(0);
2084 uint64_t block_offset = op->getEndOffset();
2085
2086 llvm::Error error = llvm::Error::success();
2087 llvm::ArrayRef<uint8_t> block_data = llvm::arrayRefFromStringRef(
2088 expr_data.getBytes(&block_offset, block_size, &error));
2089
2090 if (error)
2091 return error;
2092
2093 if (llvm::Error err = Evaluate_DW_OP_entry_value(eval_ctx, block_data))
2094 return llvm::createStringError(
2095 "could not evaluate DW_OP_entry_value: %s",
2096 llvm::toString(std::move(err)).c_str());
2097
2098 // We can't use `operator++` here because the iterator currently points
2099 // to the second operand. See the comment above.
2100 op = op.skipBytes(block_size);
2101 continue;
2102 }
2103
2104 // These opcodes are decoded but not evaluated here.
2105 case DW_OP_xderef:
2106 case DW_OP_xderef_size:
2107 case DW_OP_call2:
2108 case DW_OP_call4:
2109 case DW_OP_call_ref:
2110 case DW_OP_constx:
2111 case DW_OP_const_type:
2112 case DW_OP_regval_type:
2113 case DW_OP_deref_type:
2114 case DW_OP_xderef_type:
2115 case DW_OP_reinterpret:
2116 case DW_OP_GNU_implicit_pointer:
2117 return llvm::createStringError("unimplemented opcode %s",
2118 DW_OP_value_to_name(opcode));
2119
2120 default:
2121 if (eval_ctx.dwarf_cu) {
2122 const uint64_t operands_offset = op_offset + 1;
2123 uint64_t offset = operands_offset; // Updated by the callee.
2124 if (eval_ctx.dwarf_cu->ParseVendorDWARFOpcode(
2125 opcode, expr_data, offset, eval_ctx.reg_ctx, eval_ctx.reg_kind,
2126 stack)) {
2127 // This is a little tricky. If LLVM knows about this vendor-specific
2128 // operation, `getEndOffset()` points past its last operand. If LLVM
2129 // knows nothing about this operation, `getEndOffset()` points to its
2130 // opcode. In both cases `offset` will point to the next operation,
2131 // but we can't use it directly because the only available mutating
2132 // method of `iterator` (not counting `operator++`) is `skipBytes()`.
2133 // So we calculate the offset and pass it to `skipBytes()`.
2134 assert(offset >= op->getEndOffset());
2135 uint64_t offset_to_next_op = offset - op->getEndOffset();
2136 op = op.skipBytes(offset_to_next_op);
2137 continue;
2138 }
2139 }
2140 return llvm::createStringErrorV("unhandled opcode {0} in DWARFExpression",
2141 opcode);
2142 }
2143 ++op;
2144 }
2145
2146 if (stack.empty()) {
2147 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2148 // or DW_OP_bit_piece opcodes
2149 if (eval_ctx.pieces.GetBuffer().GetByteSize())
2150 return eval_ctx.pieces;
2151
2152 return llvm::createStringError("stack empty after evaluation");
2153 }
2154
2155 UpdateValueTypeFromLocationDescription(eval_ctx, eval_ctx.loc_desc_kind,
2156 &stack.back());
2157
2158 if (log && log->GetVerbose()) {
2159 size_t count = stack.size();
2160 LLDB_LOGF(log, "Stack after operation has %" PRIu64 " values:",
2161 static_cast<uint64_t>(count));
2162 for (size_t i = 0; i < count; ++i) {
2163 StreamString new_value;
2164 new_value.Printf("[%" PRIu64 "]", static_cast<uint64_t>(i));
2165 stack[i].Dump(&new_value);
2166 LLDB_LOGF(log, " %s", new_value.GetData());
2167 }
2168 }
2169 return stack.back();
2170}
2171
2173 StackFrame &frame, const Instruction::Operand &operand) const {
2174 using namespace OperandMatchers;
2175
2176 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
2177 if (!reg_ctx_sp) {
2178 return false;
2179 }
2180
2181 DataExtractor opcodes(m_data);
2182
2183 lldb::offset_t op_offset = 0;
2184 uint8_t opcode = opcodes.GetU8(&op_offset);
2185
2186 if (opcode == DW_OP_fbreg) {
2187 int64_t offset = opcodes.GetSLEB128(&op_offset);
2188
2189 DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr);
2190 if (!fb_expr) {
2191 return false;
2192 }
2193
2194 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
2195 return fb_expr->MatchesOperand(frame, child);
2196 };
2197
2198 if (!offset &&
2199 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2200 recurse)(operand)) {
2201 return true;
2202 }
2203
2204 return MatchUnaryOp(
2206 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2207 MatchImmOp(offset), recurse))(operand);
2208 }
2209
2210 bool dereference = false;
2211 const RegisterInfo *reg = nullptr;
2212 int64_t offset = 0;
2213
2214 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
2215 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
2216 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
2217 offset = opcodes.GetSLEB128(&op_offset);
2218 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
2219 } else if (opcode == DW_OP_regx) {
2220 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2221 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2222 } else if (opcode == DW_OP_bregx) {
2223 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2224 offset = opcodes.GetSLEB128(&op_offset);
2225 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2226 } else {
2227 return false;
2228 }
2229
2230 if (!reg) {
2231 return false;
2232 }
2233
2234 if (dereference) {
2235 if (!offset &&
2236 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2237 MatchRegOp(*reg))(operand)) {
2238 return true;
2239 }
2240
2241 return MatchUnaryOp(
2243 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2244 MatchRegOp(*reg), MatchImmOp(offset)))(operand);
2245 } else {
2246 return MatchRegOp(*reg)(operand);
2247 }
2248}
static llvm::raw_ostream & error(Stream &strm)
static llvm::Error Evaluate_DW_OP_entry_value(EvalContext &eval_ctx, llvm::ArrayRef< uint8_t > subexpr)
static llvm::Error Evaluate_DW_OP_convert(EvalContext &eval_ctx, uint64_t relative_die_offset)
static llvm::Error CheckScalarOperandsHaveSameType(const Scalar &lhs, const Scalar &rhs, LocationAtom opcode, size_t address_size)
static llvm::Error Evaluate_DW_OP_piece(EvalContext &eval_ctx, uint64_t piece_byte_size)
static llvm::Error Evaluate_DW_OP_fbreg(EvalContext &eval_ctx, int64_t fbreg_offset)
static const char * DW_OP_value_to_name(uint32_t val)
static llvm::Error Evaluate_DW_OP_form_tls_address(EvalContext &eval_ctx, LocationAtom opcode)
static llvm::Expected< lldb::addr_t > ResolveLoadAddress(EvalContext &eval_ctx, const char *dw_op_type, lldb::addr_t file_addr, Address &so_addr, bool check_sectionoffset=false)
Helper function to move common code used to resolve a file address and turn into a load address.
static lldb::offset_t GetOpcodeDataSize(const DataExtractor &data, const lldb::offset_t data_offset, const LocationAtom op, const DWARFExpression::Delegate *dwarf_cu)
Return the length in bytes of the set of operands for op.
static llvm::Error Evaluate_DW_OP_call_frame_cfa(EvalContext &eval_ctx)
static Scalar DerefSizeExtractDataHelper(uint8_t *addr_bytes, size_t size_addr_bytes, ByteOrder byte_order, size_t size)
Helper function to load sized data from a uint8_t buffer.
static void UpdateValueTypeFromLocationDescription(EvalContext &eval_ctx, LocationDescriptionKind kind, Value *value=nullptr)
Adjust value's ValueType according to the kind of location description.
static llvm::Error Evaluate_DW_OP_deref(EvalContext &eval_ctx, LocationAtom opcode, unsigned size, size_t size_addr_bytes)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_LOGF(log,...)
Definition Log.h:389
@ Empty
If the Mangled object has neither a mangled name or demangled name we can encode the object with one ...
Definition Mangled.cpp:448
llvm::MCRegisterInfo & GetMCRegisterInfo()
Definition ABI.h:144
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:301
bool IsSectionOffset() const
Check if an address is section offset.
Definition Address.h:342
Represent a call made within a Function.
Definition Function.h:253
virtual Function * GetCallee(ModuleList &images, ExecutionContext &exe_ctx)=0
Get the callee's definition.
llvm::ArrayRef< CallSiteParameter > GetCallSiteParameters() const
Get the call site parameters available at this call edge.
Definition Function.h:282
"lldb/Expression/DWARFExpressionList.h" Encapsulates a range map from file address range to a single ...
bool MatchesOperand(StackFrame &frame, const Instruction::Operand &operand) const
llvm::Expected< Value > Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx, lldb::addr_t func_load_addr, const Value *initial_value_ptr, const Value *object_address_ptr) const
virtual uint16_t GetVersion() const =0
virtual lldb::offset_t GetVendorDWARFOpcodeSize(const DataExtractor &data, const lldb::offset_t data_offset, const uint8_t op) const =0
virtual dw_addr_t ReadAddressFromDebugAddrSection(uint32_t index) const =0
virtual bool ParseVendorDWARFOpcode(uint8_t op, const llvm::DataExtractor &opcodes, lldb::offset_t &offset, RegisterContext *reg_ctx, lldb::RegisterKind reg_kind, Stack &stack) const =0
virtual llvm::Expected< std::pair< uint64_t, bool > > GetDIEBitSizeAndSign(uint64_t relative_die_offset) const =0
virtual uint8_t GetAddressByteSize() const =0
DataExtractor m_data
A data extractor capable of reading opcode bytes.
llvm::Expected< lldb::addr_t > GetLocation_DW_OP_addr(const Delegate *dwarf_cu) const
Return the address specified by the first DW_OP_{addr, addrx, GNU_addr_index} in the operation stream...
void UpdateValue(uint64_t const_value, lldb::offset_t const_value_byte_size, uint8_t addr_byte_size)
static llvm::Expected< Value > Evaluate(ExecutionContext *exe_ctx, RegisterContext *reg_ctx, lldb::ModuleSP module_sp, const DataExtractor &opcodes, const Delegate *dwarf_cu, const lldb::RegisterKind reg_set, const Value *initial_value_ptr, const Value *object_address_ptr)
Evaluate a DWARF location expression in a particular context.
bool ContainsThreadLocalStorage(const Delegate *dwarf_cu) const
bool LinkThreadLocalStorage(const Delegate *dwarf_cu, std::function< lldb::addr_t(lldb::addr_t file_addr)> const &link_address_callback)
lldb::RegisterKind m_reg_kind
One of the defines that starts with LLDB_REGKIND_.
bool IsImplicit(const Delegate *dwarf_cu) const
Return true if this expression produces a DWARF implicit or composite location description that LLDB ...
bool Update_DW_OP_addr(const Delegate *dwarf_cu, lldb::addr_t file_addr)
void SetRegisterKind(lldb::RegisterKind reg_kind)
Set the call-frame-info style register kind.
static llvm::Error ReadRegisterValueAsScalar(RegisterContext *reg_ctx, lldb::RegisterKind reg_kind, uint32_t reg_num, Value &value)
bool MatchesOperand(StackFrame &frame, const Instruction::Operand &op) const
void DumpLocation(Stream *s, lldb::DescriptionLevel level, ABI *abi, llvm::DIDumpOptions options={}) const
lldb::RegisterKind GetRegisterKind() const
Return the call-frame-info style register kind.
bool IsValid() const
Return true if the location expression contains data.
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
An binary data encoding class.
Definition DataEncoder.h:42
std::shared_ptr< lldb_private::DataBufferHeap > GetDataBuffer()
Get a shared copy of the heap based memory buffer owned by this object.
uint32_t PutUnsigned(uint32_t offset, uint32_t byte_size, uint64_t value)
Encode an unsigned integer of size byte_size to offset.
uint32_t PutAddress(uint32_t offset, lldb::addr_t addr)
Encode an address in the existing buffer at offset bytes into the buffer.
void AppendAddress(lldb::addr_t addr)
Append an address sized integer to the end of the owned data.
void AppendU8(uint8_t value)
Append a unsigned integer to the end of the owned data.
void AppendData(llvm::StringRef data)
Append bytes to the end of the owned data.
An data extractor class.
uint64_t GetULEB128(lldb::offset_t *offset_ptr) const
Extract a unsigned LEB128 value from *offset_ptr.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
virtual lldb::offset_t BytesLeft(lldb::offset_t offset) const
llvm::DataExtractor GetAsLLVM() const
uint32_t Skip_LEB128(lldb::offset_t *offset_ptr) const
Skip an LEB128 number at *offset_ptr.
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
uint32_t GetAddressByteSize() const
Get the current address size.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
int64_t GetSLEB128(lldb::offset_t *offset_ptr) const
Extract a signed LEB128 value from *offset_ptr.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
void SetFrameSP(const lldb::StackFrameSP &frame_sp)
Set accessor to set only the frame shared pointer.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
bool HasTargetScope() const
Returns true the ExecutionContext object contains a valid target.
Target & GetTargetRef() const
Returns a reference to the target object.
Thread * GetThreadPtr() const
Returns a pointer to the thread object.
A class that describes a function.
Definition Function.h:377
ConstString GetName() const
Definition Function.cpp:726
CallEdge * GetCallEdgeForReturnAddress(lldb::addr_t return_pc, Target &target)
Get the outgoing call edge from this function which has the given return address return_pc,...
Definition Function.cpp:388
llvm::ArrayRef< std::unique_ptr< CallEdge > > GetTailCallingEdges()
Get the outgoing tail-calling edges from this function.
Definition Function.cpp:381
bool GetVerbose() const
Definition Log.cpp:329
A collection class for Module objects.
Definition ModuleList.h:125
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
virtual lldb::ByteOrder GetByteOrder() const =0
Gets whether endian swapping should occur when extracting data from this object file.
virtual size_t ReadMemory(const ProcessAddress &process_addr, void *buf, size_t size, Status &error)
Read of memory from a process.
Definition Process.cpp:2038
lldb::ByteOrder GetByteOrder() const
Definition Process.cpp:3928
virtual uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num)
Convert from a given register numbering scheme to the lldb register numbering scheme.
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
bool GetScalarValue(Scalar &scalar) const
size_t GetByteSize() const
Definition Scalar.cpp:163
bool IsZero() const
Definition Scalar.cpp:175
void TruncOrExtendTo(uint16_t bits, bool sign)
Convert to an integer with bits and the given signedness.
Definition Scalar.cpp:205
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:366
Scalar::Type GetType() const
Definition Scalar.h:153
bool IsSigned() const
Definition Scalar.cpp:261
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
Definition Scalar.cpp:816
bool IsValid() const
Definition Scalar.h:111
llvm::APSInt GetAPSInt() const
Definition Scalar.h:188
This base class provides an interface to stack frames.
Definition StackFrame.h:44
virtual DWARFExpressionList * GetFrameBaseExpression(Status *error_ptr)
Get the DWARFExpressionList corresponding to the Canonical Frame Address.
virtual llvm::Error GetFrameBaseValue(Scalar &value)
Return the Canonical Frame Address (DWARF term) for this frame.
virtual lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
virtual StackID & GetStackID()
virtual const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
virtual uint32_t GetFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList.
An error handling class.
Definition Status.h:118
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:405
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
Function * function
The Function for a given query.
virtual size_t ReadMemory(const Address &addr, void *dst, size_t dst_len, Status &error, bool force_live_memory=false, lldb::addr_t *load_addr_ptr=nullptr, bool *did_read_live_memory=nullptr)
Definition Target.cpp:2092
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1254
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:114
ValueType
Type that describes Value::m_value.
Definition Value.h:42
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
Definition Value.h:53
@ FileAddress
A file address value.
Definition Value.h:48
@ LoadAddress
A load address value.
Definition Value.h:50
@ Scalar
A raw scalar value.
Definition Value.h:46
void ClearContext()
Definition Value.h:92
size_t AppendDataToHostBuffer(const Value &rhs)
Definition Value.cpp:154
ValueType GetValueType() const
Definition Value.cpp:111
void SetContext(ContextType context_type, void *p)
Definition Value.h:97
DataBufferHeap & GetBuffer()
Definition Value.h:123
void SetValueType(ValueType value_type)
Definition Value.h:90
@ RegisterInfo
RegisterInfo * (can be a scalar or a vector register).
Definition Value.h:62
void ConvertToLoadAddress(Module *module, Target *target)
Convert this value's file address to a load address, if possible.
Definition Value.cpp:675
size_t ResizeData(size_t len)
Definition Value.cpp:192
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define LLDB_INVALID_ADDRESS
#define LLDB_INVALID_OFFSET
#define UINT32_MAX
#define LLDB_INVALID_REGNUM
lldb::ByteOrder InlHostByteOrder()
Definition Endian.h:25
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
uint64_t offset_t
Definition lldb-types.h:86
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
std::shared_ptr< lldb_private::Module > ModuleSP
RegisterKind
Register numbering types.
Represent the locations of a parameter at a call site, both in the caller and in the callee.
Definition Function.h:241
DWARFExpressionList LocationInCaller
Definition Function.h:243
Every register is described in detail including its name, alternate name (optional),...
const char * name
Name of this register, can't be NULL.