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