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 return llvm::Error::success();
1099}
1100
1101static llvm::Error Evaluate_DW_OP_piece(EvalContext &eval_ctx,
1102 uint64_t piece_byte_size) {
1103 LocationDescriptionKind piece_locdesc = eval_ctx.loc_desc_kind;
1104 // Reset for the next piece.
1105 eval_ctx.loc_desc_kind = Memory;
1106
1107 if (piece_byte_size == 0)
1108 return llvm::Error::success();
1109
1110 Value curr_piece;
1111
1112 if (eval_ctx.stack.empty()) {
1114 LocationDescriptionKind::Empty);
1115 // In a multi-piece expression, this means that the current piece is
1116 // not available. Fill with zeros for now by resizing the data and
1117 // appending it
1118 curr_piece.ResizeData(piece_byte_size);
1119 // Note that "0" is not a correct value for the unknown bits.
1120 // It would be better to also return a mask of valid bits together
1121 // with the expression result, so the debugger can print missing
1122 // members as "<optimized out>" or something.
1123 ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size);
1124 eval_ctx.pieces.AppendDataToHostBuffer(curr_piece);
1125 } else {
1126 Status error;
1127 // Extract the current piece into "curr_piece"
1128 Value curr_piece_source_value(eval_ctx.stack.back());
1129 eval_ctx.stack.pop_back();
1130 UpdateValueTypeFromLocationDescription(eval_ctx, piece_locdesc,
1131 &curr_piece_source_value);
1132
1133 const Value::ValueType curr_piece_source_value_type =
1134 curr_piece_source_value.GetValueType();
1135 Scalar &scalar = curr_piece_source_value.GetScalar();
1137 switch (curr_piece_source_value_type) {
1139 return llvm::createStringError("invalid value type");
1141 if (eval_ctx.target) {
1142 curr_piece_source_value.ConvertToLoadAddress(eval_ctx.module_sp.get(),
1143 eval_ctx.target);
1144 addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1145 } else {
1146 return llvm::createStringError(
1147 "unable to convert file address 0x%" PRIx64 " to load address "
1148 "for DW_OP_piece(%" PRIu64 "): "
1149 "no target available",
1150 addr, piece_byte_size);
1151 }
1152 [[fallthrough]];
1154 if (eval_ctx.target) {
1155 if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) {
1156 if (eval_ctx.target->ReadMemory(
1157 Address(addr), curr_piece.GetBuffer().GetBytes(),
1158 piece_byte_size, error,
1159 /*force_live_memory=*/false) != piece_byte_size) {
1160 const char *addr_type =
1161 (curr_piece_source_value_type == Value::ValueType::LoadAddress)
1162 ? "load"
1163 : "file";
1164 return llvm::createStringError(
1165 "failed to read memory DW_OP_piece(%" PRIu64
1166 ") from %s address 0x%" PRIx64,
1167 piece_byte_size, addr_type, addr);
1168 }
1169 } else {
1170 return llvm::createStringError(
1171 "failed to resize the piece memory buffer for "
1172 "DW_OP_piece(%" PRIu64 ")",
1173 piece_byte_size);
1174 }
1175 }
1176 } break;
1178 return llvm::createStringError(
1179 "failed to read memory DW_OP_piece(%" PRIu64
1180 ") from host address 0x%" PRIx64,
1181 piece_byte_size, addr);
1182 } break;
1183
1185 uint32_t bit_size = piece_byte_size * 8;
1186 uint32_t bit_offset = 0;
1187 if (!scalar.ExtractBitfield(bit_size, bit_offset)) {
1188 return llvm::createStringError(
1189 "unable to extract %" PRIu64 " bytes from a %" PRIu64
1190 " byte scalar value.",
1191 piece_byte_size,
1192 (uint64_t)curr_piece_source_value.GetScalar().GetByteSize());
1193 }
1194
1195 // We have seen a case where we have expression like:
1196 // DW_OP_lit0, DW_OP_stack_value, DW_OP_piece 0x28
1197 // here we are assuming the compiler was trying to zero
1198 // extend the value that we should append to the buffer.
1199 scalar.TruncOrExtendTo(bit_size, /*sign=*/false);
1200 curr_piece.GetScalar() = scalar;
1201 } break;
1202 }
1203
1204 // Check if this is the first piece?
1205 if (eval_ctx.op_piece_offset == 0) {
1206 // This is the first piece, we should push it back onto the stack
1207 // so subsequent pieces will be able to access this piece and add
1208 // to it.
1209 if (eval_ctx.pieces.AppendDataToHostBuffer(curr_piece) == 0) {
1210 return llvm::createStringError("failed to append piece data");
1211 }
1212 } else {
1213 // If this is the second or later piece there should be a value on
1214 // the stack.
1215 if (eval_ctx.pieces.GetBuffer().GetByteSize() !=
1216 eval_ctx.op_piece_offset) {
1217 return llvm::createStringError(
1218 "DW_OP_piece for offset %" PRIu64
1219 " but top of stack is of size %" PRIu64,
1220 eval_ctx.op_piece_offset,
1221 eval_ctx.pieces.GetBuffer().GetByteSize());
1222 }
1223
1224 if (eval_ctx.pieces.AppendDataToHostBuffer(curr_piece) == 0)
1225 return llvm::createStringError("failed to append piece data");
1226 }
1227 }
1228 eval_ctx.op_piece_offset += piece_byte_size;
1229 return llvm::Error::success();
1230}
1231
1232static llvm::Error Evaluate_DW_OP_convert(EvalContext &eval_ctx,
1233 uint64_t relative_die_offset) {
1234 uint64_t bit_size;
1235 bool sign;
1236 if (relative_die_offset == 0) {
1237 // The generic type has the size of an address on the target
1238 // machine and an unspecified signedness. Scalar has no
1239 // "unspecified signedness", so we use unsigned types.
1240 if (!eval_ctx.module_sp)
1241 return llvm::createStringError("no module");
1242 sign = false;
1243 bit_size = eval_ctx.module_sp->GetArchitecture().GetAddressByteSize() * 8;
1244 if (!bit_size)
1245 return llvm::createStringError("unspecified architecture");
1246 } else {
1247 if (!eval_ctx.dwarf_cu)
1248 return llvm::createStringError(
1249 "DW_OP_convert with a DIE offset requires a DWARF unit");
1250 auto bit_size_sign_or_err =
1251 eval_ctx.dwarf_cu->GetDIEBitSizeAndSign(relative_die_offset);
1252 if (!bit_size_sign_or_err)
1253 return bit_size_sign_or_err.takeError();
1254 bit_size = bit_size_sign_or_err->first;
1255 sign = bit_size_sign_or_err->second;
1256 }
1257 eval_ctx.stack.back().GetScalar().TruncOrExtendTo(bit_size, sign);
1258 return llvm::Error::success();
1259}
1260
1261static llvm::Error Evaluate_DW_OP_form_tls_address(EvalContext &eval_ctx,
1262 LocationAtom opcode) {
1263 if (eval_ctx.stack.empty())
1264 return llvm::createStringError("%s needs an argument",
1265 opcode == DW_OP_form_tls_address
1266 ? "DW_OP_form_tls_address"
1267 : "DW_OP_GNU_push_tls_address");
1268
1269 if (!eval_ctx.exe_ctx || !eval_ctx.module_sp)
1270 return llvm::createStringError("no context to evaluate TLS within");
1271
1272 Thread *thread = eval_ctx.exe_ctx->GetThreadPtr();
1273 if (!thread)
1274 return llvm::createStringError("no thread to evaluate TLS within");
1275
1276 // Lookup the TLS block address for this thread and module.
1277 const addr_t tls_file_addr =
1278 eval_ctx.stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1279 const addr_t tls_load_addr =
1280 thread->GetThreadLocalData(eval_ctx.module_sp, tls_file_addr);
1281
1282 if (tls_load_addr == LLDB_INVALID_ADDRESS)
1283 return llvm::createStringError(
1284 "no TLS data currently exists for this thread");
1285
1286 eval_ctx.stack.back().GetScalar() = tls_load_addr;
1287 eval_ctx.stack.back().SetValueType(Value::ValueType::LoadAddress);
1288 return llvm::Error::success();
1289}
1290
1291static llvm::Error Evaluate_DW_OP_fbreg(EvalContext &eval_ctx,
1292 int64_t fbreg_offset) {
1293 if (!eval_ctx.exe_ctx)
1294 return llvm::createStringError("NULL execution context for DW_OP_fbreg");
1295 if (!eval_ctx.frame)
1296 return llvm::createStringError(
1297 "invalid stack frame in context for DW_OP_fbreg opcode");
1298
1299 Scalar value;
1300 if (llvm::Error err = eval_ctx.frame->GetFrameBaseValue(value))
1301 return err;
1302 value += fbreg_offset;
1303 eval_ctx.stack.push_back(value);
1304 eval_ctx.stack.back().SetValueType(Value::ValueType::LoadAddress);
1305 return llvm::Error::success();
1306}
1307
1308static llvm::Error Evaluate_DW_OP_call_frame_cfa(EvalContext &eval_ctx) {
1309 if (!eval_ctx.frame)
1310 return llvm::createStringError(
1311 "invalid stack frame in context for DW_OP_call_frame_cfa opcode");
1312
1313 // Note that we don't have to parse FDEs because this DWARF expression
1314 // is commonly evaluated with a valid stack frame.
1315 StackID id = eval_ctx.frame->GetStackID();
1316 addr_t cfa = id.GetCallFrameAddressWithMetadata();
1317 if (cfa == LLDB_INVALID_ADDRESS)
1318 return llvm::createStringError("stack frame does not include a canonical "
1319 "frame address for DW_OP_call_frame_cfa "
1320 "opcode");
1321
1322 eval_ctx.stack.push_back(Scalar(cfa));
1323 eval_ctx.stack.back().SetValueType(Value::ValueType::LoadAddress);
1324 return llvm::Error::success();
1325}
1326
1327static llvm::Error CheckScalarOperandsHaveSameType(const Scalar &lhs,
1328 const Scalar &rhs,
1329 LocationAtom opcode,
1330 size_t address_size) {
1331 auto mismatch = [&](const char *what) {
1332 return llvm::createStringError("%s requires operands to have the same %s",
1333 DW_OP_value_to_name(opcode), what);
1334 };
1335
1336 // Scalar does not preserve the original DWARF DIE, but it does carry the
1337 // pieces of base-type information used by the evaluator: kind, size, and
1338 // integer signedness.
1339 if (lhs.GetType() != rhs.GetType())
1340 return mismatch("type");
1341
1342 // Only integer scalars have signedness. Non-integer operands (e.g. floats)
1343 // have no further scalar type information to compare once kind and size
1344 // match.
1345 if (lhs.GetType() != Scalar::e_int) {
1346 if (lhs.GetByteSize() != rhs.GetByteSize())
1347 return mismatch("size");
1348 return llvm::Error::success();
1349 }
1350
1351 // DWARF generic values are address-sized integers with unspecified
1352 // signedness. LLDB does not explicitly preserve genericness on the
1353 // expression stack, so treat integers at least as wide as the generic type
1354 // as potentially generic and compatible with one another, regardless of
1355 // exact width or signedness. This keeps common expressions working: e.g.
1356 // DW_OP_breg produces a register-sized value while DW_OP_const* produces an
1357 // address-sized one, yet both are meant to be generic. DW_OP_constu and
1358 // DW_OP_consts also do not always use to_generic due to
1359 // https://github.com/llvm/llvm-project/issues/47431. A precise fix would
1360 // require tracking genericness directly, which is a larger type-system
1361 // change.
1362 if (address_size != 0 && lhs.GetByteSize() >= address_size &&
1363 rhs.GetByteSize() >= address_size)
1364 return llvm::Error::success();
1365
1366 // For non-generic integer operands, size and signedness are part of the
1367 // base-type information preserved by Scalar, so require them to match.
1368 if (lhs.GetByteSize() != rhs.GetByteSize())
1369 return mismatch("size");
1370 if (lhs.IsSigned() != rhs.IsSigned())
1371 return mismatch("signedness");
1372
1373 return llvm::Error::success();
1374}
1375
1376llvm::Expected<Value> DWARFExpression::Evaluate(
1377 ExecutionContext *exe_ctx, RegisterContext *reg_ctx,
1378 lldb::ModuleSP module_sp, const DataExtractor &opcodes,
1379 const DWARFExpression::Delegate *dwarf_cu,
1380 const lldb::RegisterKind reg_kind, const Value *initial_value_ptr,
1381 const Value *object_address_ptr) {
1382 uint32_t address_size = opcodes.GetAddressByteSize();
1383 llvm::DataExtractor expr_data = opcodes.GetAsLLVM();
1384 llvm::DWARFExpression expr(expr_data, address_size);
1385
1386 if (expr_data.size() == 0)
1387 return llvm::createStringError(
1388 "no location, value may have been optimized out");
1389
1390 EvalContext eval_ctx(exe_ctx, reg_ctx, std::move(module_sp), dwarf_cu,
1391 reg_kind, initial_value_ptr, object_address_ptr);
1392
1393 Stack &stack = eval_ctx.stack;
1394
1395 if (initial_value_ptr)
1396 stack.push_back(*initial_value_ptr);
1397
1398 Value tmp;
1399 uint32_t reg_num;
1400
1402 // A generic type is "an integral type that has the size of an address and an
1403 // unspecified signedness". For now, just use the signedness of the operand.
1404 // TODO: Implement a real typed stack, and store the genericness of the value
1405 // there.
1406 auto to_generic = [&](auto v) {
1407 // TODO: Avoid implicit trunc?
1408 // See https://github.com/llvm/llvm-project/issues/112510.
1409 bool is_signed = std::is_signed<decltype(v)>::value;
1410 return Scalar(llvm::APSInt(
1411 llvm::APInt(8 * address_size, v, is_signed, /*implicitTrunc=*/true),
1412 !is_signed));
1413 };
1414
1415 llvm::DWARFExpression::iterator op = expr.begin(), op_end = expr.end();
1416 while (op != op_end) {
1417 const uint64_t op_offset = op.getOffset();
1418 const LocationAtom opcode = static_cast<LocationAtom>(op->getCode());
1419
1420 if (log && log->GetVerbose()) {
1421 size_t count = stack.size();
1422 LLDB_LOGF(log, "Stack before operation has %" PRIu64 " values:",
1423 static_cast<uint64_t>(count));
1424 for (size_t i = 0; i < count; ++i) {
1425 StreamString new_value;
1426 new_value.Printf("[%" PRIu64 "]", static_cast<uint64_t>(i));
1427 stack[i].Dump(&new_value);
1428 LLDB_LOGF(log, " %s", new_value.GetData());
1429 }
1430 LLDB_LOGF(log, "0x%8.8" PRIx64 ": %s", op_offset,
1431 DW_OP_value_to_name(opcode));
1432 }
1433
1434 if (std::optional<unsigned> arity = OperationArity(opcode)) {
1435 if (stack.size() < *arity)
1436 return llvm::createStringError(
1437 "%s needs at least %d stack entries (stack has %d entries)",
1438 DW_OP_value_to_name(opcode), *arity, stack.size());
1439 }
1440
1441 switch (opcode) {
1442 case DW_OP_addr:
1443 stack.push_back(Scalar(op->getRawOperand(0)));
1444 stack.back().SetValueType(Value::ValueType::FileAddress);
1445 break;
1446
1447 case DW_OP_deref: {
1448 size_t size = address_size;
1449 if (llvm::Error err = Evaluate_DW_OP_deref(eval_ctx, opcode, size, size))
1450 return err;
1451 } break;
1452
1453 case DW_OP_deref_size: {
1454 size_t size = op->getRawOperand(0);
1455 if (llvm::Error err =
1456 Evaluate_DW_OP_deref(eval_ctx, opcode, size, address_size))
1457 return err;
1458 } break;
1459
1460 case DW_OP_const1u:
1461 stack.push_back(to_generic(op->getRawOperand(0)));
1462 break;
1463 case DW_OP_const1s:
1464 stack.push_back(to_generic(static_cast<int8_t>(op->getRawOperand(0))));
1465 break;
1466 case DW_OP_const2u:
1467 stack.push_back(to_generic(op->getRawOperand(0)));
1468 break;
1469 case DW_OP_const2s:
1470 stack.push_back(to_generic(static_cast<int16_t>(op->getRawOperand(0))));
1471 break;
1472 case DW_OP_const4u:
1473 stack.push_back(to_generic(op->getRawOperand(0)));
1474 break;
1475 case DW_OP_const4s:
1476 stack.push_back(to_generic(static_cast<int32_t>(op->getRawOperand(0))));
1477 break;
1478 case DW_OP_const8u:
1479 stack.push_back(to_generic(op->getRawOperand(0)));
1480 break;
1481 case DW_OP_const8s:
1482 stack.push_back(to_generic(static_cast<int64_t>(op->getRawOperand(0))));
1483 break;
1484 case DW_OP_constu:
1485 stack.push_back(to_generic(op->getRawOperand(0)));
1486 break;
1487 case DW_OP_consts:
1488 stack.push_back(to_generic(static_cast<int64_t>(op->getRawOperand(0))));
1489 break;
1490
1491 case DW_OP_dup:
1492 if (stack.empty()) {
1493 return llvm::createStringError("expression stack empty for DW_OP_dup");
1494 } else
1495 stack.push_back(stack.back());
1496 break;
1497
1498 case DW_OP_drop:
1499 if (stack.empty()) {
1500 return llvm::createStringError("expression stack empty for DW_OP_drop");
1501 } else
1502 stack.pop_back();
1503 break;
1504
1505 case DW_OP_over:
1506 stack.push_back(stack[stack.size() - 2]);
1507 break;
1508
1509 case DW_OP_pick: {
1510 uint8_t pick_idx = op->getRawOperand(0);
1511 if (pick_idx < stack.size())
1512 stack.push_back(stack[stack.size() - 1 - pick_idx]);
1513 else {
1514 return llvm::createStringError(
1515 "Index %u out of range for DW_OP_pick.\n", pick_idx);
1516 }
1517 } break;
1518
1519 case DW_OP_swap:
1520 tmp = stack.back();
1521 stack.back() = stack[stack.size() - 2];
1522 stack[stack.size() - 2] = tmp;
1523 break;
1524
1525 case DW_OP_rot: {
1526 size_t last_idx = stack.size() - 1;
1527 Value old_top = stack[last_idx];
1528 stack[last_idx] = stack[last_idx - 1];
1529 stack[last_idx - 1] = stack[last_idx - 2];
1530 stack[last_idx - 2] = old_top;
1531 } break;
1532
1533 case DW_OP_abs:
1534 if (!stack.back().GetScalar().AbsoluteValue()) {
1535 return llvm::createStringError(
1536 "failed to take the absolute value of the first stack item");
1537 }
1538 break;
1539
1540 case DW_OP_and:
1541 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1542 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1543 opcode, address_size))
1544 return err;
1545 tmp = stack.back();
1546 stack.pop_back();
1547 stack.back().GetScalar() = stack.back().GetScalar() & tmp.GetScalar();
1548 break;
1549
1550 case DW_OP_div: {
1551 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1552 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1553 opcode, address_size))
1554 return err;
1555 tmp = stack.back();
1556 if (tmp.GetScalar().IsZero())
1557 return llvm::createStringError("divide by zero");
1558
1559 stack.pop_back();
1560 Scalar divisor, dividend;
1561 divisor = tmp.GetScalar();
1562 dividend = stack.back().GetScalar();
1563 divisor.MakeSigned();
1564 dividend.MakeSigned();
1565 stack.back() = dividend / divisor;
1566
1567 if (!stack.back().GetScalar().IsValid())
1568 return llvm::createStringError("divide failed");
1569 } break;
1570
1571 case DW_OP_minus:
1572 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1573 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1574 opcode, address_size))
1575 return err;
1576 tmp = stack.back();
1577 stack.pop_back();
1578 stack.back().GetScalar() = stack.back().GetScalar() - tmp.GetScalar();
1579 break;
1580
1581 case DW_OP_mod:
1582 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1583 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1584 opcode, address_size))
1585 return err;
1586 tmp = stack.back();
1587 stack.pop_back();
1588 stack.back().GetScalar() = stack.back().GetScalar() % tmp.GetScalar();
1589 break;
1590
1591 case DW_OP_mul:
1592 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1593 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1594 opcode, address_size))
1595 return err;
1596 tmp = stack.back();
1597 stack.pop_back();
1598 stack.back().GetScalar() = stack.back().GetScalar() * tmp.GetScalar();
1599 break;
1600
1601 case DW_OP_neg:
1602 if (!stack.back().GetScalar().UnaryNegate())
1603 return llvm::createStringError("unary negate failed");
1604 break;
1605
1606 case DW_OP_not:
1607 if (!stack.back().GetScalar().OnesComplement())
1608 return llvm::createStringError("logical NOT failed");
1609 break;
1610
1611 case DW_OP_or:
1612 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1613 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1614 opcode, address_size))
1615 return err;
1616 tmp = stack.back();
1617 stack.pop_back();
1618 stack.back().GetScalar() = stack.back().GetScalar() | tmp.GetScalar();
1619 break;
1620
1621 case DW_OP_plus:
1622 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1623 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1624 opcode, address_size))
1625 return err;
1626 tmp = stack.back();
1627 stack.pop_back();
1628 stack.back().GetScalar() += tmp.GetScalar();
1629 break;
1630
1631 case DW_OP_plus_uconst: {
1632 const uint64_t uconst_value = op->getRawOperand(0);
1633 // Implicit conversion from a UINT to a Scalar...
1634 stack.back().GetScalar() += uconst_value;
1635 if (!stack.back().GetScalar().IsValid())
1636 return llvm::createStringError("DW_OP_plus_uconst failed");
1637 } break;
1638
1639 case DW_OP_shl:
1640 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1641 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1642 opcode, address_size))
1643 return err;
1644 tmp = stack.back();
1645 stack.pop_back();
1646 stack.back().GetScalar() <<= tmp.GetScalar();
1647 break;
1648
1649 case DW_OP_shr:
1650 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1651 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1652 opcode, address_size))
1653 return err;
1654 tmp = stack.back();
1655 stack.pop_back();
1656 if (!stack.back().GetScalar().ShiftRightLogical(tmp.GetScalar()))
1657 return llvm::createStringError("DW_OP_shr failed");
1658 break;
1659
1660 case DW_OP_shra:
1661 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1662 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1663 opcode, address_size))
1664 return err;
1665 tmp = stack.back();
1666 stack.pop_back();
1667 stack.back().GetScalar() >>= tmp.GetScalar();
1668 break;
1669
1670 case DW_OP_xor:
1671 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1672 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1673 opcode, address_size))
1674 return err;
1675 tmp = stack.back();
1676 stack.pop_back();
1677 stack.back().GetScalar() = stack.back().GetScalar() ^ tmp.GetScalar();
1678 break;
1679
1680 case DW_OP_skip: {
1681 int16_t skip_offset = static_cast<int16_t>(op->getRawOperand(0));
1682 lldb::offset_t new_offset = op->getEndOffset() + skip_offset;
1683 // New offset can point at the end of the data, in this case we should
1684 // terminate the DWARF expression evaluation (will happen in the loop
1685 // condition).
1686 if (new_offset <= expr_data.size()) {
1687 op = op.skipBytes(skip_offset);
1688 continue;
1689 }
1690 return llvm::createStringErrorV(
1691 "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}",
1692 op->getEndOffset(), skip_offset, expr_data.size());
1693 }
1694
1695 case DW_OP_bra: {
1696 tmp = stack.back();
1697 stack.pop_back();
1698 int16_t bra_offset = static_cast<int16_t>(op->getRawOperand(0));
1699 Scalar zero(0);
1700 if (tmp.GetScalar() != zero) {
1701 lldb::offset_t new_offset = op->getEndOffset() + bra_offset;
1702 // New offset can point at the end of the data, in this case we should
1703 // terminate the DWARF expression evaluation (will happen in the loop
1704 // condition).
1705 if (new_offset <= expr_data.size()) {
1706 op = op.skipBytes(bra_offset);
1707 continue;
1708 }
1709 return llvm::createStringErrorV(
1710 "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}",
1711 op->getEndOffset(), bra_offset, expr_data.size());
1712 }
1713 } break;
1714
1715 case DW_OP_eq:
1716 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1717 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1718 opcode, address_size))
1719 return err;
1720 tmp = stack.back();
1721 stack.pop_back();
1722 stack.back().GetScalar() =
1723 to_generic(stack.back().GetScalar() == tmp.GetScalar());
1724 break;
1725
1726 case DW_OP_ge:
1727 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1728 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1729 opcode, address_size))
1730 return err;
1731 tmp = stack.back();
1732 stack.pop_back();
1733 stack.back().GetScalar() =
1734 to_generic(stack.back().GetScalar() >= tmp.GetScalar());
1735 break;
1736
1737 case DW_OP_gt:
1738 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1739 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1740 opcode, address_size))
1741 return err;
1742 tmp = stack.back();
1743 stack.pop_back();
1744 stack.back().GetScalar() =
1745 to_generic(stack.back().GetScalar() > tmp.GetScalar());
1746 break;
1747
1748 case DW_OP_le:
1749 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1750 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1751 opcode, address_size))
1752 return err;
1753 tmp = stack.back();
1754 stack.pop_back();
1755 stack.back().GetScalar() =
1756 to_generic(stack.back().GetScalar() <= tmp.GetScalar());
1757 break;
1758
1759 case DW_OP_lt:
1760 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1761 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1762 opcode, address_size))
1763 return err;
1764 tmp = stack.back();
1765 stack.pop_back();
1766 stack.back().GetScalar() =
1767 to_generic(stack.back().GetScalar() < tmp.GetScalar());
1768 break;
1769
1770 case DW_OP_ne:
1771 if (llvm::Error err = CheckScalarOperandsHaveSameType(
1772 stack[stack.size() - 2].GetScalar(), stack.back().GetScalar(),
1773 opcode, address_size))
1774 return err;
1775 tmp = stack.back();
1776 stack.pop_back();
1777 stack.back().GetScalar() =
1778 to_generic(stack.back().GetScalar() != tmp.GetScalar());
1779 break;
1780
1781 case DW_OP_lit0:
1782 case DW_OP_lit1:
1783 case DW_OP_lit2:
1784 case DW_OP_lit3:
1785 case DW_OP_lit4:
1786 case DW_OP_lit5:
1787 case DW_OP_lit6:
1788 case DW_OP_lit7:
1789 case DW_OP_lit8:
1790 case DW_OP_lit9:
1791 case DW_OP_lit10:
1792 case DW_OP_lit11:
1793 case DW_OP_lit12:
1794 case DW_OP_lit13:
1795 case DW_OP_lit14:
1796 case DW_OP_lit15:
1797 case DW_OP_lit16:
1798 case DW_OP_lit17:
1799 case DW_OP_lit18:
1800 case DW_OP_lit19:
1801 case DW_OP_lit20:
1802 case DW_OP_lit21:
1803 case DW_OP_lit22:
1804 case DW_OP_lit23:
1805 case DW_OP_lit24:
1806 case DW_OP_lit25:
1807 case DW_OP_lit26:
1808 case DW_OP_lit27:
1809 case DW_OP_lit28:
1810 case DW_OP_lit29:
1811 case DW_OP_lit30:
1812 case DW_OP_lit31:
1813 stack.push_back(to_generic(opcode - DW_OP_lit0));
1814 break;
1815
1816 case DW_OP_reg0:
1817 case DW_OP_reg1:
1818 case DW_OP_reg2:
1819 case DW_OP_reg3:
1820 case DW_OP_reg4:
1821 case DW_OP_reg5:
1822 case DW_OP_reg6:
1823 case DW_OP_reg7:
1824 case DW_OP_reg8:
1825 case DW_OP_reg9:
1826 case DW_OP_reg10:
1827 case DW_OP_reg11:
1828 case DW_OP_reg12:
1829 case DW_OP_reg13:
1830 case DW_OP_reg14:
1831 case DW_OP_reg15:
1832 case DW_OP_reg16:
1833 case DW_OP_reg17:
1834 case DW_OP_reg18:
1835 case DW_OP_reg19:
1836 case DW_OP_reg20:
1837 case DW_OP_reg21:
1838 case DW_OP_reg22:
1839 case DW_OP_reg23:
1840 case DW_OP_reg24:
1841 case DW_OP_reg25:
1842 case DW_OP_reg26:
1843 case DW_OP_reg27:
1844 case DW_OP_reg28:
1845 case DW_OP_reg29:
1846 case DW_OP_reg30:
1847 case DW_OP_reg31: {
1848 eval_ctx.loc_desc_kind = Register;
1849 reg_num = opcode - DW_OP_reg0;
1850
1851 if (llvm::Error err = ReadRegisterValueAsScalar(
1852 eval_ctx.reg_ctx, eval_ctx.reg_kind, reg_num, tmp))
1853 return err;
1854 stack.push_back(tmp);
1855 } break;
1856 case DW_OP_regx: {
1857 eval_ctx.loc_desc_kind = Register;
1858 reg_num = op->getRawOperand(0);
1859 Status read_err;
1860 if (llvm::Error err = ReadRegisterValueAsScalar(
1861 eval_ctx.reg_ctx, eval_ctx.reg_kind, reg_num, tmp))
1862 return err;
1863 stack.push_back(tmp);
1864 } break;
1865
1866 case DW_OP_breg0:
1867 case DW_OP_breg1:
1868 case DW_OP_breg2:
1869 case DW_OP_breg3:
1870 case DW_OP_breg4:
1871 case DW_OP_breg5:
1872 case DW_OP_breg6:
1873 case DW_OP_breg7:
1874 case DW_OP_breg8:
1875 case DW_OP_breg9:
1876 case DW_OP_breg10:
1877 case DW_OP_breg11:
1878 case DW_OP_breg12:
1879 case DW_OP_breg13:
1880 case DW_OP_breg14:
1881 case DW_OP_breg15:
1882 case DW_OP_breg16:
1883 case DW_OP_breg17:
1884 case DW_OP_breg18:
1885 case DW_OP_breg19:
1886 case DW_OP_breg20:
1887 case DW_OP_breg21:
1888 case DW_OP_breg22:
1889 case DW_OP_breg23:
1890 case DW_OP_breg24:
1891 case DW_OP_breg25:
1892 case DW_OP_breg26:
1893 case DW_OP_breg27:
1894 case DW_OP_breg28:
1895 case DW_OP_breg29:
1896 case DW_OP_breg30:
1897 case DW_OP_breg31: {
1898 reg_num = opcode - DW_OP_breg0;
1899 if (llvm::Error err = ReadRegisterValueAsScalar(
1900 eval_ctx.reg_ctx, eval_ctx.reg_kind, reg_num, tmp))
1901 return err;
1902
1903 int64_t breg_offset = op->getRawOperand(0);
1904 tmp.GetScalar() += static_cast<uint64_t>(breg_offset);
1905 tmp.ClearContext();
1906 stack.push_back(tmp);
1907 stack.back().SetValueType(Value::ValueType::LoadAddress);
1908 } break;
1909 case DW_OP_bregx: {
1910 reg_num = op->getRawOperand(0);
1911 if (llvm::Error err = ReadRegisterValueAsScalar(
1912 eval_ctx.reg_ctx, eval_ctx.reg_kind, reg_num, tmp))
1913 return err;
1914
1915 int64_t breg_offset = op->getRawOperand(1);
1916 tmp.GetScalar() += static_cast<uint64_t>(breg_offset);
1917 tmp.ClearContext();
1918 stack.push_back(tmp);
1919 stack.back().SetValueType(Value::ValueType::LoadAddress);
1920 } break;
1921
1922 case DW_OP_fbreg:
1923 if (llvm::Error err =
1924 Evaluate_DW_OP_fbreg(eval_ctx, op->getRawOperand(0)))
1925 return err;
1926 break;
1927
1928 case DW_OP_nop:
1929 break;
1930
1931 case DW_OP_piece: {
1932 if (llvm::Error err =
1933 Evaluate_DW_OP_piece(eval_ctx, op->getRawOperand(0)))
1934 return err;
1935 } break;
1936
1937 case DW_OP_bit_piece:
1938 if (stack.size() < 1) {
1940 LocationDescriptionKind::Empty);
1941 // Reset for the next piece.
1942 eval_ctx.loc_desc_kind = Memory;
1943 return llvm::createStringError(
1944 "expression stack needs at least 1 item for DW_OP_bit_piece");
1945 } else {
1946 UpdateValueTypeFromLocationDescription(eval_ctx, eval_ctx.loc_desc_kind,
1947 &stack.back());
1948 // Reset for the next piece.
1949 eval_ctx.loc_desc_kind = Memory;
1950 const uint64_t piece_bit_size = op->getRawOperand(0);
1951 const uint64_t piece_bit_offset = op->getRawOperand(1);
1952 switch (stack.back().GetValueType()) {
1954 return llvm::createStringError(
1955 "unable to extract bit value from invalid value");
1957 if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size,
1958 piece_bit_offset)) {
1959 return llvm::createStringError(
1960 "unable to extract %" PRIu64 " bit value with %" PRIu64
1961 " bit offset from a %" PRIu64 " bit scalar value.",
1962 piece_bit_size, piece_bit_offset,
1963 (uint64_t)(stack.back().GetScalar().GetByteSize() * 8));
1964 }
1965 } break;
1966
1970 return llvm::createStringError(
1971 "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64
1972 ", bit_offset = %" PRIu64 ") from an address value.",
1973 piece_bit_size, piece_bit_offset);
1974 }
1975 }
1976 break;
1977
1978 case DW_OP_implicit_value: {
1979 eval_ctx.loc_desc_kind = Implicit;
1980
1981 // The second operand is a sequence of bytes of the length specified by
1982 // the first operand. LLVM represents it as an offset to that sequence.
1983 const uint64_t block_size = op->getRawOperand(0);
1984 uint64_t block_offset = op->getRawOperand(1);
1985
1986 llvm::Error error = llvm::Error::success();
1987 llvm::StringRef block_data =
1988 expr_data.getBytes(&block_offset, block_size, &error);
1989
1990 if (error)
1991 return error;
1992
1993 Value result(block_data.data(), block_data.size());
1994 stack.push_back(result);
1995 break;
1996 }
1997
1998 case DW_OP_implicit_pointer: {
1999 eval_ctx.loc_desc_kind = Implicit;
2000 return llvm::createStringError("could not evaluate %s",
2001 DW_OP_value_to_name(opcode));
2002 }
2003
2004 case DW_OP_push_object_address:
2005 if (eval_ctx.object_address_ptr)
2006 stack.push_back(*eval_ctx.object_address_ptr);
2007 else {
2008 return llvm::createStringError("DW_OP_push_object_address used without "
2009 "specifying an object address");
2010 }
2011 break;
2012
2013 case DW_OP_stack_value:
2014 eval_ctx.loc_desc_kind = Implicit;
2015 stack.back().SetValueType(Value::ValueType::Scalar);
2016 break;
2017
2018 case DW_OP_convert:
2019 if (llvm::Error err =
2020 Evaluate_DW_OP_convert(eval_ctx, op->getRawOperand(0)))
2021 return err;
2022 break;
2023
2024 case DW_OP_call_frame_cfa:
2025 if (llvm::Error err = Evaluate_DW_OP_call_frame_cfa(eval_ctx))
2026 return err;
2027 break;
2028
2029 case DW_OP_form_tls_address:
2030 case DW_OP_GNU_push_tls_address:
2031 if (llvm::Error err = Evaluate_DW_OP_form_tls_address(eval_ctx, opcode))
2032 return err;
2033 break;
2034
2035 case DW_OP_addrx:
2036 case DW_OP_GNU_addr_index: {
2037 if (!eval_ctx.dwarf_cu)
2038 return llvm::createStringError("DW_OP_GNU_addr_index found without a "
2039 "compile unit being specified");
2040 uint64_t index = op->getRawOperand(0);
2041 lldb::addr_t value =
2042 eval_ctx.dwarf_cu->ReadAddressFromDebugAddrSection(index);
2043 stack.push_back(Scalar(value));
2044 stack.back().SetValueType(Value::ValueType::FileAddress);
2045 } break;
2046
2047 case DW_OP_GNU_const_index: {
2048 if (!eval_ctx.dwarf_cu) {
2049 return llvm::createStringError("DW_OP_GNU_const_index found without a "
2050 "compile unit being specified");
2051 }
2052 uint64_t index = op->getRawOperand(0);
2053 lldb::addr_t value =
2054 eval_ctx.dwarf_cu->ReadAddressFromDebugAddrSection(index);
2055 stack.push_back(Scalar(value));
2056 } break;
2057
2058 case DW_OP_GNU_entry_value:
2059 case DW_OP_entry_value: {
2060 // Technically, DW_OP_entry_value has two operands, but LLVM represents
2061 // it as a single-operand operation (bug?). We can deal with this: the
2062 // second operand immediately follows the first, but have to be careful
2063 // when advancing the iterator, see the comment below.
2064 const uint64_t block_size = op->getRawOperand(0);
2065 uint64_t block_offset = op->getEndOffset();
2066
2067 llvm::Error error = llvm::Error::success();
2068 llvm::ArrayRef<uint8_t> block_data = llvm::arrayRefFromStringRef(
2069 expr_data.getBytes(&block_offset, block_size, &error));
2070
2071 if (error)
2072 return error;
2073
2074 if (llvm::Error err = Evaluate_DW_OP_entry_value(eval_ctx, block_data))
2075 return llvm::createStringError(
2076 "could not evaluate DW_OP_entry_value: %s",
2077 llvm::toString(std::move(err)).c_str());
2078
2079 // We can't use `operator++` here because the iterator currently points
2080 // to the second operand. See the comment above.
2081 op = op.skipBytes(block_size);
2082 continue;
2083 }
2084
2085 // These opcodes are decoded but not evaluated here.
2086 case DW_OP_xderef:
2087 case DW_OP_xderef_size:
2088 case DW_OP_call2:
2089 case DW_OP_call4:
2090 case DW_OP_call_ref:
2091 case DW_OP_constx:
2092 case DW_OP_const_type:
2093 case DW_OP_regval_type:
2094 case DW_OP_deref_type:
2095 case DW_OP_xderef_type:
2096 case DW_OP_reinterpret:
2097 case DW_OP_GNU_implicit_pointer:
2098 return llvm::createStringError("unimplemented opcode %s",
2099 DW_OP_value_to_name(opcode));
2100
2101 default:
2102 if (eval_ctx.dwarf_cu) {
2103 const uint64_t operands_offset = op_offset + 1;
2104 uint64_t offset = operands_offset; // Updated by the callee.
2105 if (eval_ctx.dwarf_cu->ParseVendorDWARFOpcode(
2106 opcode, expr_data, offset, eval_ctx.reg_ctx, eval_ctx.reg_kind,
2107 stack)) {
2108 // This is a little tricky. If LLVM knows about this vendor-specific
2109 // operation, `getEndOffset()` points past its last operand. If LLVM
2110 // knows nothing about this operation, `getEndOffset()` points to its
2111 // opcode. In both cases `offset` will point to the next operation,
2112 // but we can't use it directly because the only available mutating
2113 // method of `iterator` (not counting `operator++`) is `skipBytes()`.
2114 // So we calculate the offset and pass it to `skipBytes()`.
2115 assert(offset >= op->getEndOffset());
2116 uint64_t offset_to_next_op = offset - op->getEndOffset();
2117 op = op.skipBytes(offset_to_next_op);
2118 continue;
2119 }
2120 }
2121 return llvm::createStringErrorV("unhandled opcode {0} in DWARFExpression",
2122 opcode);
2123 }
2124 ++op;
2125 }
2126
2127 if (stack.empty()) {
2128 // Nothing on the stack, check if we created a piece value from DW_OP_piece
2129 // or DW_OP_bit_piece opcodes
2130 if (eval_ctx.pieces.GetBuffer().GetByteSize())
2131 return eval_ctx.pieces;
2132
2133 return llvm::createStringError("stack empty after evaluation");
2134 }
2135
2136 UpdateValueTypeFromLocationDescription(eval_ctx, eval_ctx.loc_desc_kind,
2137 &stack.back());
2138
2139 if (log && log->GetVerbose()) {
2140 size_t count = stack.size();
2141 LLDB_LOGF(log, "Stack after operation has %" PRIu64 " values:",
2142 static_cast<uint64_t>(count));
2143 for (size_t i = 0; i < count; ++i) {
2144 StreamString new_value;
2145 new_value.Printf("[%" PRIu64 "]", static_cast<uint64_t>(i));
2146 stack[i].Dump(&new_value);
2147 LLDB_LOGF(log, " %s", new_value.GetData());
2148 }
2149 }
2150 return stack.back();
2151}
2152
2154 StackFrame &frame, const Instruction::Operand &operand) const {
2155 using namespace OperandMatchers;
2156
2157 RegisterContextSP reg_ctx_sp = frame.GetRegisterContext();
2158 if (!reg_ctx_sp) {
2159 return false;
2160 }
2161
2162 DataExtractor opcodes(m_data);
2163
2164 lldb::offset_t op_offset = 0;
2165 uint8_t opcode = opcodes.GetU8(&op_offset);
2166
2167 if (opcode == DW_OP_fbreg) {
2168 int64_t offset = opcodes.GetSLEB128(&op_offset);
2169
2170 DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr);
2171 if (!fb_expr) {
2172 return false;
2173 }
2174
2175 auto recurse = [&frame, fb_expr](const Instruction::Operand &child) {
2176 return fb_expr->MatchesOperand(frame, child);
2177 };
2178
2179 if (!offset &&
2180 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2181 recurse)(operand)) {
2182 return true;
2183 }
2184
2185 return MatchUnaryOp(
2187 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2188 MatchImmOp(offset), recurse))(operand);
2189 }
2190
2191 bool dereference = false;
2192 const RegisterInfo *reg = nullptr;
2193 int64_t offset = 0;
2194
2195 if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) {
2196 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0);
2197 } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) {
2198 offset = opcodes.GetSLEB128(&op_offset);
2199 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0);
2200 } else if (opcode == DW_OP_regx) {
2201 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2202 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2203 } else if (opcode == DW_OP_bregx) {
2204 uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset));
2205 offset = opcodes.GetSLEB128(&op_offset);
2206 reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num);
2207 } else {
2208 return false;
2209 }
2210
2211 if (!reg) {
2212 return false;
2213 }
2214
2215 if (dereference) {
2216 if (!offset &&
2217 MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
2218 MatchRegOp(*reg))(operand)) {
2219 return true;
2220 }
2221
2222 return MatchUnaryOp(
2224 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
2225 MatchRegOp(*reg), MatchImmOp(offset)))(operand);
2226 } else {
2227 return MatchRegOp(*reg)(operand);
2228 }
2229}
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(lldb::addr_t vm_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:3926
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:162
bool IsZero() const
Definition Scalar.cpp:174
void TruncOrExtendTo(uint16_t bits, bool sign)
Convert to an integer with bits and the given signedness.
Definition Scalar.cpp:204
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:365
Scalar::Type GetType() const
Definition Scalar.h:153
bool IsSigned() const
Definition Scalar.cpp:260
bool ExtractBitfield(uint32_t bit_size, uint32_t bit_offset)
Definition Scalar.cpp:813
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:2091
const ModuleList & GetImages() const
Get accessor for the images for this process.
Definition Target.h:1247
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:85
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.