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