Bug Summary

File:build/llvm-toolchain-snapshot-16~++20220904122748+c444af1c20b3/lldb/source/Expression/DWARFExpression.cpp
Warning:line 2189, column 9
Value stored to 'dwarf4_location_description_kind' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

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