File: | build/source/lldb/source/Expression/DWARFExpression.cpp |
Warning: | line 2266, column 7 Value stored to 'dwarf4_location_description_kind' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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 | |
44 | using namespace lldb; |
45 | using namespace lldb_private; |
46 | using namespace lldb_private::dwarf; |
47 | |
48 | // DWARFExpression constructor |
49 | DWARFExpression::DWARFExpression() : m_data() {} |
50 | |
51 | DWARFExpression::DWARFExpression(const DataExtractor &data) : m_data(data) {} |
52 | |
53 | // Destructor |
54 | DWARFExpression::~DWARFExpression() = default; |
55 | |
56 | bool DWARFExpression::IsValid() const { return m_data.GetByteSize() > 0; } |
57 | |
58 | void 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 | |
70 | void 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 | |
77 | RegisterKind DWARFExpression::GetRegisterKind() const { return m_reg_kind; } |
78 | |
79 | void DWARFExpression::SetRegisterKind(RegisterKind reg_kind) { |
80 | m_reg_kind = reg_kind; |
81 | } |
82 | |
83 | |
84 | static 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. |
132 | static 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 | |
341 | lldb::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 | |
378 | bool 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 | |
414 | bool 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 | } |
428 | bool 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 accordingly. 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 | |
511 | static 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 ¶m : 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(¶m_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 = ¶m; |
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 ¶m_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 | |
721 | namespace { |
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. |
725 | enum LocationDescriptionKind { |
726 | Empty, |
727 | Memory, |
728 | Register, |
729 | Implicit |
730 | /* Composite*/ |
731 | }; |
732 | /// Adjust value's ValueType according to the kind of location description. |
733 | void UpdateValueTypeFromLocationDescription(Log *log, const DWARFUnit *dwarf_cu, |
734 | LocationDescriptionKind kind, |
735 | Value *value = nullptr) { |
736 | // Note that this function is conflating DWARF expressions with |
737 | // DWARF location descriptions. Perhaps it would be better to define |
738 | // a wrapper for DWARFExpression::Eval() that deals with DWARF |
739 | // location descriptions (which consist of one or more DWARF |
740 | // expressions). But doing this would mean we'd also need factor the |
741 | // handling of DW_OP_(bit_)piece out of this function. |
742 | if (dwarf_cu && dwarf_cu->GetVersion() >= 4) { |
743 | const char *log_msg = "DWARF location description kind: %s"; |
744 | switch (kind) { |
745 | case Empty: |
746 | LLDB_LOGF(log, log_msg, "Empty")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 address or section offset |
777 | /// \param check_sectionoffset bool which determines if having a section offset |
778 | /// but not a load address is considerd a success |
779 | /// \returns 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. |
783 | static llvm::Optional<lldb::addr_t> |
784 | ResolveLoadAddress(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 |
821 | static 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 | |
833 | bool 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 | Target *target = nullptr; |
851 | |
852 | if (exe_ctx) { |
853 | process = exe_ctx->GetProcessPtr(); |
854 | frame = exe_ctx->GetFramePtr(); |
855 | target = exe_ctx->GetTargetPtr(); |
856 | } |
857 | if (reg_ctx == nullptr && frame) |
858 | reg_ctx = frame->GetRegisterContext().get(); |
859 | |
860 | if (initial_value_ptr) |
861 | stack.push_back(*initial_value_ptr); |
862 | |
863 | lldb::offset_t offset = 0; |
864 | Value tmp; |
865 | uint32_t reg_num; |
866 | |
867 | /// Insertion point for evaluating multi-piece expression. |
868 | uint64_t op_piece_offset = 0; |
869 | Value pieces; // Used for DW_OP_piece |
870 | |
871 | Log *log = GetLog(LLDBLog::Expressions); |
872 | // A generic type is "an integral type that has the size of an address and an |
873 | // unspecified signedness". For now, just use the signedness of the operand. |
874 | // TODO: Implement a real typed stack, and store the genericness of the value |
875 | // there. |
876 | auto to_generic = [&](auto v) { |
877 | bool is_signed = std::is_signed<decltype(v)>::value; |
878 | return Scalar(llvm::APSInt( |
879 | llvm::APInt(8 * opcodes.GetAddressByteSize(), v, is_signed), |
880 | !is_signed)); |
881 | }; |
882 | |
883 | // The default kind is a memory location. This is updated by any |
884 | // operation that changes this, such as DW_OP_stack_value, and reset |
885 | // by composition operations like DW_OP_piece. |
886 | LocationDescriptionKind dwarf4_location_description_kind = Memory; |
887 | |
888 | while (opcodes.ValidOffset(offset)) { |
889 | const lldb::offset_t op_offset = offset; |
890 | const uint8_t op = opcodes.GetU8(&offset); |
891 | |
892 | if (log && log->GetVerbose()) { |
893 | size_t count = stack.size(); |
894 | 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) |
895 | (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); |
896 | for (size_t i = 0; i < count; ++i) { |
897 | StreamString new_value; |
898 | new_value.Printf("[%" PRIu64"l" "u" "]", (uint64_t)i); |
899 | stack[i].Dump(&new_value); |
900 | 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); |
901 | } |
902 | 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) |
903 | 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); |
904 | } |
905 | |
906 | switch (op) { |
907 | // The DW_OP_addr operation has a single operand that encodes a machine |
908 | // address and whose size is the size of an address on the target machine. |
909 | case DW_OP_addr: |
910 | stack.push_back(Scalar(opcodes.GetAddress(&offset))); |
911 | if (target && |
912 | target->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32) { |
913 | // wasm file sections aren't mapped into memory, therefore addresses can |
914 | // never point into a file section and are always LoadAddresses. |
915 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
916 | } else { |
917 | stack.back().SetValueType(Value::ValueType::FileAddress); |
918 | // Convert the file address to a load address, so subsequent |
919 | // DWARF operators can operate on it. |
920 | if (frame) |
921 | stack.back().ConvertToLoadAddress(module_sp.get(), |
922 | frame->CalculateTarget().get()); |
923 | } |
924 | break; |
925 | |
926 | // The DW_OP_addr_sect_offset4 is used for any location expressions in |
927 | // shared libraries that have a location like: |
928 | // DW_OP_addr(0x1000) |
929 | // If this address resides in a shared library, then this virtual address |
930 | // won't make sense when it is evaluated in the context of a running |
931 | // process where shared libraries have been slid. To account for this, this |
932 | // new address type where we can store the section pointer and a 4 byte |
933 | // offset. |
934 | // case DW_OP_addr_sect_offset4: |
935 | // { |
936 | // result_type = eResultTypeFileAddress; |
937 | // lldb::Section *sect = (lldb::Section |
938 | // *)opcodes.GetMaxU64(&offset, sizeof(void *)); |
939 | // lldb::addr_t sect_offset = opcodes.GetU32(&offset); |
940 | // |
941 | // Address so_addr (sect, sect_offset); |
942 | // lldb::addr_t load_addr = so_addr.GetLoadAddress(); |
943 | // if (load_addr != LLDB_INVALID_ADDRESS) |
944 | // { |
945 | // // We successfully resolve a file address to a load |
946 | // // address. |
947 | // stack.push_back(load_addr); |
948 | // break; |
949 | // } |
950 | // else |
951 | // { |
952 | // // We were able |
953 | // if (error_ptr) |
954 | // error_ptr->SetErrorStringWithFormat ("Section %s in |
955 | // %s is not currently loaded.\n", |
956 | // sect->GetName().AsCString(), |
957 | // sect->GetModule()->GetFileSpec().GetFilename().AsCString()); |
958 | // return false; |
959 | // } |
960 | // } |
961 | // break; |
962 | |
963 | // OPCODE: DW_OP_deref |
964 | // OPERANDS: none |
965 | // DESCRIPTION: Pops the top stack entry and treats it as an address. |
966 | // The value retrieved from that address is pushed. The size of the data |
967 | // retrieved from the dereferenced address is the size of an address on the |
968 | // target machine. |
969 | case DW_OP_deref: { |
970 | if (stack.empty()) { |
971 | if (error_ptr) |
972 | error_ptr->SetErrorString("Expression stack empty for DW_OP_deref."); |
973 | return false; |
974 | } |
975 | Value::ValueType value_type = stack.back().GetValueType(); |
976 | switch (value_type) { |
977 | case Value::ValueType::HostAddress: { |
978 | void *src = (void *)stack.back().GetScalar().ULongLong(); |
979 | intptr_t ptr; |
980 | ::memcpy(&ptr, src, sizeof(void *)); |
981 | stack.back().GetScalar() = ptr; |
982 | stack.back().ClearContext(); |
983 | } break; |
984 | case Value::ValueType::FileAddress: { |
985 | auto file_addr = stack.back().GetScalar().ULongLong( |
986 | LLDB_INVALID_ADDRESS(18446744073709551615UL)); |
987 | |
988 | Address so_addr; |
989 | auto maybe_load_addr = ResolveLoadAddress( |
990 | exe_ctx, module_sp, error_ptr, "DW_OP_deref", file_addr, so_addr); |
991 | |
992 | if (!maybe_load_addr) |
993 | return false; |
994 | |
995 | stack.back().GetScalar() = *maybe_load_addr; |
996 | // Fall through to load address promotion code below. |
997 | } |
998 | [[fallthrough]]; |
999 | case Value::ValueType::Scalar: |
1000 | // Promote Scalar to LoadAddress and fall through. |
1001 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
1002 | [[fallthrough]]; |
1003 | case Value::ValueType::LoadAddress: |
1004 | if (exe_ctx) { |
1005 | if (process) { |
1006 | lldb::addr_t pointer_addr = |
1007 | stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS(18446744073709551615UL)); |
1008 | Status error; |
1009 | lldb::addr_t pointer_value = |
1010 | process->ReadPointerFromMemory(pointer_addr, error); |
1011 | if (pointer_value != LLDB_INVALID_ADDRESS(18446744073709551615UL)) { |
1012 | if (ABISP abi_sp = process->GetABI()) |
1013 | pointer_value = abi_sp->FixCodeAddress(pointer_value); |
1014 | stack.back().GetScalar() = pointer_value; |
1015 | stack.back().ClearContext(); |
1016 | } else { |
1017 | if (error_ptr) |
1018 | error_ptr->SetErrorStringWithFormat( |
1019 | "Failed to dereference pointer from 0x%" PRIx64"l" "x" |
1020 | " for DW_OP_deref: %s\n", |
1021 | pointer_addr, error.AsCString()); |
1022 | return false; |
1023 | } |
1024 | } else { |
1025 | if (error_ptr) |
1026 | error_ptr->SetErrorString("NULL process for DW_OP_deref.\n"); |
1027 | return false; |
1028 | } |
1029 | } else { |
1030 | if (error_ptr) |
1031 | error_ptr->SetErrorString( |
1032 | "NULL execution context for DW_OP_deref.\n"); |
1033 | return false; |
1034 | } |
1035 | break; |
1036 | |
1037 | case Value::ValueType::Invalid: |
1038 | if (error_ptr) |
1039 | error_ptr->SetErrorString("Invalid value type for DW_OP_deref.\n"); |
1040 | return false; |
1041 | } |
1042 | |
1043 | } break; |
1044 | |
1045 | // OPCODE: DW_OP_deref_size |
1046 | // OPERANDS: 1 |
1047 | // 1 - uint8_t that specifies the size of the data to dereference. |
1048 | // DESCRIPTION: Behaves like the DW_OP_deref operation: it pops the top |
1049 | // stack entry and treats it as an address. The value retrieved from that |
1050 | // address is pushed. In the DW_OP_deref_size operation, however, the size |
1051 | // in bytes of the data retrieved from the dereferenced address is |
1052 | // specified by the single operand. This operand is a 1-byte unsigned |
1053 | // integral constant whose value may not be larger than the size of an |
1054 | // address on the target machine. The data retrieved is zero extended to |
1055 | // the size of an address on the target machine before being pushed on the |
1056 | // expression stack. |
1057 | case DW_OP_deref_size: { |
1058 | if (stack.empty()) { |
1059 | if (error_ptr) |
1060 | error_ptr->SetErrorString( |
1061 | "Expression stack empty for DW_OP_deref_size."); |
1062 | return false; |
1063 | } |
1064 | uint8_t size = opcodes.GetU8(&offset); |
1065 | Value::ValueType value_type = stack.back().GetValueType(); |
1066 | switch (value_type) { |
1067 | case Value::ValueType::HostAddress: { |
1068 | void *src = (void *)stack.back().GetScalar().ULongLong(); |
1069 | intptr_t ptr; |
1070 | ::memcpy(&ptr, src, sizeof(void *)); |
1071 | // I can't decide whether the size operand should apply to the bytes in |
1072 | // their |
1073 | // lldb-host endianness or the target endianness.. I doubt this'll ever |
1074 | // come up but I'll opt for assuming big endian regardless. |
1075 | switch (size) { |
1076 | case 1: |
1077 | ptr = ptr & 0xff; |
1078 | break; |
1079 | case 2: |
1080 | ptr = ptr & 0xffff; |
1081 | break; |
1082 | case 3: |
1083 | ptr = ptr & 0xffffff; |
1084 | break; |
1085 | case 4: |
1086 | ptr = ptr & 0xffffffff; |
1087 | break; |
1088 | // the casts are added to work around the case where intptr_t is a 32 |
1089 | // bit quantity; |
1090 | // presumably we won't hit the 5..7 cases if (void*) is 32-bits in this |
1091 | // program. |
1092 | case 5: |
1093 | ptr = (intptr_t)ptr & 0xffffffffffULL; |
1094 | break; |
1095 | case 6: |
1096 | ptr = (intptr_t)ptr & 0xffffffffffffULL; |
1097 | break; |
1098 | case 7: |
1099 | ptr = (intptr_t)ptr & 0xffffffffffffffULL; |
1100 | break; |
1101 | default: |
1102 | break; |
1103 | } |
1104 | stack.back().GetScalar() = ptr; |
1105 | stack.back().ClearContext(); |
1106 | } break; |
1107 | case Value::ValueType::FileAddress: { |
1108 | auto file_addr = |
1109 | stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS(18446744073709551615UL)); |
1110 | Address so_addr; |
1111 | auto maybe_load_addr = |
1112 | ResolveLoadAddress(exe_ctx, module_sp, error_ptr, |
1113 | "DW_OP_deref_size", file_addr, so_addr, |
1114 | /*check_sectionoffset=*/true); |
1115 | |
1116 | if (!maybe_load_addr) |
1117 | return false; |
1118 | |
1119 | addr_t load_addr = *maybe_load_addr; |
1120 | |
1121 | if (load_addr == LLDB_INVALID_ADDRESS(18446744073709551615UL) && so_addr.IsSectionOffset()) { |
1122 | uint8_t addr_bytes[8]; |
1123 | Status error; |
1124 | |
1125 | if (exe_ctx->GetTargetRef().ReadMemory( |
1126 | so_addr, &addr_bytes, size, error, |
1127 | /*force_live_memory=*/false) == size) { |
1128 | ObjectFile *objfile = module_sp->GetObjectFile(); |
1129 | |
1130 | stack.back().GetScalar() = DerefSizeExtractDataHelper( |
1131 | addr_bytes, size, objfile->GetByteOrder(), size); |
1132 | stack.back().ClearContext(); |
1133 | break; |
1134 | } else { |
1135 | if (error_ptr) |
1136 | error_ptr->SetErrorStringWithFormat( |
1137 | "Failed to dereference pointer for for DW_OP_deref_size: " |
1138 | "%s\n", |
1139 | error.AsCString()); |
1140 | return false; |
1141 | } |
1142 | } |
1143 | stack.back().GetScalar() = load_addr; |
1144 | // Fall through to load address promotion code below. |
1145 | } |
1146 | |
1147 | [[fallthrough]]; |
1148 | case Value::ValueType::Scalar: |
1149 | case Value::ValueType::LoadAddress: |
1150 | if (exe_ctx) { |
1151 | if (process) { |
1152 | lldb::addr_t pointer_addr = |
1153 | stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS(18446744073709551615UL)); |
1154 | uint8_t addr_bytes[sizeof(lldb::addr_t)]; |
1155 | Status error; |
1156 | if (process->ReadMemory(pointer_addr, &addr_bytes, size, error) == |
1157 | size) { |
1158 | |
1159 | stack.back().GetScalar() = |
1160 | DerefSizeExtractDataHelper(addr_bytes, sizeof(addr_bytes), |
1161 | process->GetByteOrder(), size); |
1162 | stack.back().ClearContext(); |
1163 | } else { |
1164 | if (error_ptr) |
1165 | error_ptr->SetErrorStringWithFormat( |
1166 | "Failed to dereference pointer from 0x%" PRIx64"l" "x" |
1167 | " for DW_OP_deref: %s\n", |
1168 | pointer_addr, error.AsCString()); |
1169 | return false; |
1170 | } |
1171 | } else { |
1172 | if (error_ptr) |
1173 | error_ptr->SetErrorString("NULL process for DW_OP_deref_size.\n"); |
1174 | return false; |
1175 | } |
1176 | } else { |
1177 | if (error_ptr) |
1178 | error_ptr->SetErrorString( |
1179 | "NULL execution context for DW_OP_deref_size.\n"); |
1180 | return false; |
1181 | } |
1182 | break; |
1183 | |
1184 | case Value::ValueType::Invalid: |
1185 | if (error_ptr) |
1186 | error_ptr->SetErrorString("Invalid value for DW_OP_deref_size.\n"); |
1187 | return false; |
1188 | } |
1189 | |
1190 | } break; |
1191 | |
1192 | // OPCODE: DW_OP_xderef_size |
1193 | // OPERANDS: 1 |
1194 | // 1 - uint8_t that specifies the size of the data to dereference. |
1195 | // DESCRIPTION: Behaves like the DW_OP_xderef operation: the entry at |
1196 | // the top of the stack is treated as an address. The second stack entry is |
1197 | // treated as an "address space identifier" for those architectures that |
1198 | // support multiple address spaces. The top two stack elements are popped, |
1199 | // a data item is retrieved through an implementation-defined address |
1200 | // calculation and pushed as the new stack top. In the DW_OP_xderef_size |
1201 | // operation, however, the size in bytes of the data retrieved from the |
1202 | // dereferenced address is specified by the single operand. This operand is |
1203 | // a 1-byte unsigned integral constant whose value may not be larger than |
1204 | // the size of an address on the target machine. The data retrieved is zero |
1205 | // extended to the size of an address on the target machine before being |
1206 | // pushed on the expression stack. |
1207 | case DW_OP_xderef_size: |
1208 | if (error_ptr) |
1209 | error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef_size."); |
1210 | return false; |
1211 | // OPCODE: DW_OP_xderef |
1212 | // OPERANDS: none |
1213 | // DESCRIPTION: Provides an extended dereference mechanism. The entry at |
1214 | // the top of the stack is treated as an address. The second stack entry is |
1215 | // treated as an "address space identifier" for those architectures that |
1216 | // support multiple address spaces. The top two stack elements are popped, |
1217 | // a data item is retrieved through an implementation-defined address |
1218 | // calculation and pushed as the new stack top. The size of the data |
1219 | // retrieved from the dereferenced address is the size of an address on the |
1220 | // target machine. |
1221 | case DW_OP_xderef: |
1222 | if (error_ptr) |
1223 | error_ptr->SetErrorString("Unimplemented opcode: DW_OP_xderef."); |
1224 | return false; |
1225 | |
1226 | // All DW_OP_constXXX opcodes have a single operand as noted below: |
1227 | // |
1228 | // Opcode Operand 1 |
1229 | // DW_OP_const1u 1-byte unsigned integer constant |
1230 | // DW_OP_const1s 1-byte signed integer constant |
1231 | // DW_OP_const2u 2-byte unsigned integer constant |
1232 | // DW_OP_const2s 2-byte signed integer constant |
1233 | // DW_OP_const4u 4-byte unsigned integer constant |
1234 | // DW_OP_const4s 4-byte signed integer constant |
1235 | // DW_OP_const8u 8-byte unsigned integer constant |
1236 | // DW_OP_const8s 8-byte signed integer constant |
1237 | // DW_OP_constu unsigned LEB128 integer constant |
1238 | // DW_OP_consts signed LEB128 integer constant |
1239 | case DW_OP_const1u: |
1240 | stack.push_back(to_generic(opcodes.GetU8(&offset))); |
1241 | break; |
1242 | case DW_OP_const1s: |
1243 | stack.push_back(to_generic((int8_t)opcodes.GetU8(&offset))); |
1244 | break; |
1245 | case DW_OP_const2u: |
1246 | stack.push_back(to_generic(opcodes.GetU16(&offset))); |
1247 | break; |
1248 | case DW_OP_const2s: |
1249 | stack.push_back(to_generic((int16_t)opcodes.GetU16(&offset))); |
1250 | break; |
1251 | case DW_OP_const4u: |
1252 | stack.push_back(to_generic(opcodes.GetU32(&offset))); |
1253 | break; |
1254 | case DW_OP_const4s: |
1255 | stack.push_back(to_generic((int32_t)opcodes.GetU32(&offset))); |
1256 | break; |
1257 | case DW_OP_const8u: |
1258 | stack.push_back(to_generic(opcodes.GetU64(&offset))); |
1259 | break; |
1260 | case DW_OP_const8s: |
1261 | stack.push_back(to_generic((int64_t)opcodes.GetU64(&offset))); |
1262 | break; |
1263 | // These should also use to_generic, but we can't do that due to a |
1264 | // producer-side bug in llvm. See llvm.org/pr48087. |
1265 | case DW_OP_constu: |
1266 | stack.push_back(Scalar(opcodes.GetULEB128(&offset))); |
1267 | break; |
1268 | case DW_OP_consts: |
1269 | stack.push_back(Scalar(opcodes.GetSLEB128(&offset))); |
1270 | break; |
1271 | |
1272 | // OPCODE: DW_OP_dup |
1273 | // OPERANDS: none |
1274 | // DESCRIPTION: duplicates the value at the top of the stack |
1275 | case DW_OP_dup: |
1276 | if (stack.empty()) { |
1277 | if (error_ptr) |
1278 | error_ptr->SetErrorString("Expression stack empty for DW_OP_dup."); |
1279 | return false; |
1280 | } else |
1281 | stack.push_back(stack.back()); |
1282 | break; |
1283 | |
1284 | // OPCODE: DW_OP_drop |
1285 | // OPERANDS: none |
1286 | // DESCRIPTION: pops the value at the top of the stack |
1287 | case DW_OP_drop: |
1288 | if (stack.empty()) { |
1289 | if (error_ptr) |
1290 | error_ptr->SetErrorString("Expression stack empty for DW_OP_drop."); |
1291 | return false; |
1292 | } else |
1293 | stack.pop_back(); |
1294 | break; |
1295 | |
1296 | // OPCODE: DW_OP_over |
1297 | // OPERANDS: none |
1298 | // DESCRIPTION: Duplicates the entry currently second in the stack at |
1299 | // the top of the stack. |
1300 | case DW_OP_over: |
1301 | if (stack.size() < 2) { |
1302 | if (error_ptr) |
1303 | error_ptr->SetErrorString( |
1304 | "Expression stack needs at least 2 items for DW_OP_over."); |
1305 | return false; |
1306 | } else |
1307 | stack.push_back(stack[stack.size() - 2]); |
1308 | break; |
1309 | |
1310 | // OPCODE: DW_OP_pick |
1311 | // OPERANDS: uint8_t index into the current stack |
1312 | // DESCRIPTION: The stack entry with the specified index (0 through 255, |
1313 | // inclusive) is pushed on the stack |
1314 | case DW_OP_pick: { |
1315 | uint8_t pick_idx = opcodes.GetU8(&offset); |
1316 | if (pick_idx < stack.size()) |
1317 | stack.push_back(stack[stack.size() - 1 - pick_idx]); |
1318 | else { |
1319 | if (error_ptr) |
1320 | error_ptr->SetErrorStringWithFormat( |
1321 | "Index %u out of range for DW_OP_pick.\n", pick_idx); |
1322 | return false; |
1323 | } |
1324 | } break; |
1325 | |
1326 | // OPCODE: DW_OP_swap |
1327 | // OPERANDS: none |
1328 | // DESCRIPTION: swaps the top two stack entries. The entry at the top |
1329 | // of the stack becomes the second stack entry, and the second entry |
1330 | // becomes the top of the stack |
1331 | case DW_OP_swap: |
1332 | if (stack.size() < 2) { |
1333 | if (error_ptr) |
1334 | error_ptr->SetErrorString( |
1335 | "Expression stack needs at least 2 items for DW_OP_swap."); |
1336 | return false; |
1337 | } else { |
1338 | tmp = stack.back(); |
1339 | stack.back() = stack[stack.size() - 2]; |
1340 | stack[stack.size() - 2] = tmp; |
1341 | } |
1342 | break; |
1343 | |
1344 | // OPCODE: DW_OP_rot |
1345 | // OPERANDS: none |
1346 | // DESCRIPTION: Rotates the first three stack entries. The entry at |
1347 | // the top of the stack becomes the third stack entry, the second entry |
1348 | // becomes the top of the stack, and the third entry becomes the second |
1349 | // entry. |
1350 | case DW_OP_rot: |
1351 | if (stack.size() < 3) { |
1352 | if (error_ptr) |
1353 | error_ptr->SetErrorString( |
1354 | "Expression stack needs at least 3 items for DW_OP_rot."); |
1355 | return false; |
1356 | } else { |
1357 | size_t last_idx = stack.size() - 1; |
1358 | Value old_top = stack[last_idx]; |
1359 | stack[last_idx] = stack[last_idx - 1]; |
1360 | stack[last_idx - 1] = stack[last_idx - 2]; |
1361 | stack[last_idx - 2] = old_top; |
1362 | } |
1363 | break; |
1364 | |
1365 | // OPCODE: DW_OP_abs |
1366 | // OPERANDS: none |
1367 | // DESCRIPTION: pops the top stack entry, interprets it as a signed |
1368 | // value and pushes its absolute value. If the absolute value can not be |
1369 | // represented, the result is undefined. |
1370 | case DW_OP_abs: |
1371 | if (stack.empty()) { |
1372 | if (error_ptr) |
1373 | error_ptr->SetErrorString( |
1374 | "Expression stack needs at least 1 item for DW_OP_abs."); |
1375 | return false; |
1376 | } else if (!stack.back().ResolveValue(exe_ctx).AbsoluteValue()) { |
1377 | if (error_ptr) |
1378 | error_ptr->SetErrorString( |
1379 | "Failed to take the absolute value of the first stack item."); |
1380 | return false; |
1381 | } |
1382 | break; |
1383 | |
1384 | // OPCODE: DW_OP_and |
1385 | // OPERANDS: none |
1386 | // DESCRIPTION: pops the top two stack values, performs a bitwise and |
1387 | // operation on the two, and pushes the result. |
1388 | case DW_OP_and: |
1389 | if (stack.size() < 2) { |
1390 | if (error_ptr) |
1391 | error_ptr->SetErrorString( |
1392 | "Expression stack needs at least 2 items for DW_OP_and."); |
1393 | return false; |
1394 | } else { |
1395 | tmp = stack.back(); |
1396 | stack.pop_back(); |
1397 | stack.back().ResolveValue(exe_ctx) = |
1398 | stack.back().ResolveValue(exe_ctx) & tmp.ResolveValue(exe_ctx); |
1399 | } |
1400 | break; |
1401 | |
1402 | // OPCODE: DW_OP_div |
1403 | // OPERANDS: none |
1404 | // DESCRIPTION: pops the top two stack values, divides the former second |
1405 | // entry by the former top of the stack using signed division, and pushes |
1406 | // the result. |
1407 | case DW_OP_div: |
1408 | if (stack.size() < 2) { |
1409 | if (error_ptr) |
1410 | error_ptr->SetErrorString( |
1411 | "Expression stack needs at least 2 items for DW_OP_div."); |
1412 | return false; |
1413 | } else { |
1414 | tmp = stack.back(); |
1415 | if (tmp.ResolveValue(exe_ctx).IsZero()) { |
1416 | if (error_ptr) |
1417 | error_ptr->SetErrorString("Divide by zero."); |
1418 | return false; |
1419 | } else { |
1420 | stack.pop_back(); |
1421 | stack.back() = |
1422 | stack.back().ResolveValue(exe_ctx) / tmp.ResolveValue(exe_ctx); |
1423 | if (!stack.back().ResolveValue(exe_ctx).IsValid()) { |
1424 | if (error_ptr) |
1425 | error_ptr->SetErrorString("Divide failed."); |
1426 | return false; |
1427 | } |
1428 | } |
1429 | } |
1430 | break; |
1431 | |
1432 | // OPCODE: DW_OP_minus |
1433 | // OPERANDS: none |
1434 | // DESCRIPTION: pops the top two stack values, subtracts the former top |
1435 | // of the stack from the former second entry, and pushes the result. |
1436 | case DW_OP_minus: |
1437 | if (stack.size() < 2) { |
1438 | if (error_ptr) |
1439 | error_ptr->SetErrorString( |
1440 | "Expression stack needs at least 2 items for DW_OP_minus."); |
1441 | return false; |
1442 | } else { |
1443 | tmp = stack.back(); |
1444 | stack.pop_back(); |
1445 | stack.back().ResolveValue(exe_ctx) = |
1446 | stack.back().ResolveValue(exe_ctx) - tmp.ResolveValue(exe_ctx); |
1447 | } |
1448 | break; |
1449 | |
1450 | // OPCODE: DW_OP_mod |
1451 | // OPERANDS: none |
1452 | // DESCRIPTION: pops the top two stack values and pushes the result of |
1453 | // the calculation: former second stack entry modulo the former top of the |
1454 | // stack. |
1455 | case DW_OP_mod: |
1456 | if (stack.size() < 2) { |
1457 | if (error_ptr) |
1458 | error_ptr->SetErrorString( |
1459 | "Expression stack needs at least 2 items for DW_OP_mod."); |
1460 | return false; |
1461 | } else { |
1462 | tmp = stack.back(); |
1463 | stack.pop_back(); |
1464 | stack.back().ResolveValue(exe_ctx) = |
1465 | stack.back().ResolveValue(exe_ctx) % tmp.ResolveValue(exe_ctx); |
1466 | } |
1467 | break; |
1468 | |
1469 | // OPCODE: DW_OP_mul |
1470 | // OPERANDS: none |
1471 | // DESCRIPTION: pops the top two stack entries, multiplies them |
1472 | // together, and pushes the result. |
1473 | case DW_OP_mul: |
1474 | if (stack.size() < 2) { |
1475 | if (error_ptr) |
1476 | error_ptr->SetErrorString( |
1477 | "Expression stack needs at least 2 items for DW_OP_mul."); |
1478 | return false; |
1479 | } else { |
1480 | tmp = stack.back(); |
1481 | stack.pop_back(); |
1482 | stack.back().ResolveValue(exe_ctx) = |
1483 | stack.back().ResolveValue(exe_ctx) * tmp.ResolveValue(exe_ctx); |
1484 | } |
1485 | break; |
1486 | |
1487 | // OPCODE: DW_OP_neg |
1488 | // OPERANDS: none |
1489 | // DESCRIPTION: pops the top stack entry, and pushes its negation. |
1490 | case DW_OP_neg: |
1491 | if (stack.empty()) { |
1492 | if (error_ptr) |
1493 | error_ptr->SetErrorString( |
1494 | "Expression stack needs at least 1 item for DW_OP_neg."); |
1495 | return false; |
1496 | } else { |
1497 | if (!stack.back().ResolveValue(exe_ctx).UnaryNegate()) { |
1498 | if (error_ptr) |
1499 | error_ptr->SetErrorString("Unary negate failed."); |
1500 | return false; |
1501 | } |
1502 | } |
1503 | break; |
1504 | |
1505 | // OPCODE: DW_OP_not |
1506 | // OPERANDS: none |
1507 | // DESCRIPTION: pops the top stack entry, and pushes its bitwise |
1508 | // complement |
1509 | case DW_OP_not: |
1510 | if (stack.empty()) { |
1511 | if (error_ptr) |
1512 | error_ptr->SetErrorString( |
1513 | "Expression stack needs at least 1 item for DW_OP_not."); |
1514 | return false; |
1515 | } else { |
1516 | if (!stack.back().ResolveValue(exe_ctx).OnesComplement()) { |
1517 | if (error_ptr) |
1518 | error_ptr->SetErrorString("Logical NOT failed."); |
1519 | return false; |
1520 | } |
1521 | } |
1522 | break; |
1523 | |
1524 | // OPCODE: DW_OP_or |
1525 | // OPERANDS: none |
1526 | // DESCRIPTION: pops the top two stack entries, performs a bitwise or |
1527 | // operation on the two, and pushes the result. |
1528 | case DW_OP_or: |
1529 | if (stack.size() < 2) { |
1530 | if (error_ptr) |
1531 | error_ptr->SetErrorString( |
1532 | "Expression stack needs at least 2 items for DW_OP_or."); |
1533 | return false; |
1534 | } else { |
1535 | tmp = stack.back(); |
1536 | stack.pop_back(); |
1537 | stack.back().ResolveValue(exe_ctx) = |
1538 | stack.back().ResolveValue(exe_ctx) | tmp.ResolveValue(exe_ctx); |
1539 | } |
1540 | break; |
1541 | |
1542 | // OPCODE: DW_OP_plus |
1543 | // OPERANDS: none |
1544 | // DESCRIPTION: pops the top two stack entries, adds them together, and |
1545 | // pushes the result. |
1546 | case DW_OP_plus: |
1547 | if (stack.size() < 2) { |
1548 | if (error_ptr) |
1549 | error_ptr->SetErrorString( |
1550 | "Expression stack needs at least 2 items for DW_OP_plus."); |
1551 | return false; |
1552 | } else { |
1553 | tmp = stack.back(); |
1554 | stack.pop_back(); |
1555 | stack.back().GetScalar() += tmp.GetScalar(); |
1556 | } |
1557 | break; |
1558 | |
1559 | // OPCODE: DW_OP_plus_uconst |
1560 | // OPERANDS: none |
1561 | // DESCRIPTION: pops the top stack entry, adds it to the unsigned LEB128 |
1562 | // constant operand and pushes the result. |
1563 | case DW_OP_plus_uconst: |
1564 | if (stack.empty()) { |
1565 | if (error_ptr) |
1566 | error_ptr->SetErrorString( |
1567 | "Expression stack needs at least 1 item for DW_OP_plus_uconst."); |
1568 | return false; |
1569 | } else { |
1570 | const uint64_t uconst_value = opcodes.GetULEB128(&offset); |
1571 | // Implicit conversion from a UINT to a Scalar... |
1572 | stack.back().GetScalar() += uconst_value; |
1573 | if (!stack.back().GetScalar().IsValid()) { |
1574 | if (error_ptr) |
1575 | error_ptr->SetErrorString("DW_OP_plus_uconst failed."); |
1576 | return false; |
1577 | } |
1578 | } |
1579 | break; |
1580 | |
1581 | // OPCODE: DW_OP_shl |
1582 | // OPERANDS: none |
1583 | // DESCRIPTION: pops the top two stack entries, shifts the former |
1584 | // second entry left by the number of bits specified by the former top of |
1585 | // the stack, and pushes the result. |
1586 | case DW_OP_shl: |
1587 | if (stack.size() < 2) { |
1588 | if (error_ptr) |
1589 | error_ptr->SetErrorString( |
1590 | "Expression stack needs at least 2 items for DW_OP_shl."); |
1591 | return false; |
1592 | } else { |
1593 | tmp = stack.back(); |
1594 | stack.pop_back(); |
1595 | stack.back().ResolveValue(exe_ctx) <<= tmp.ResolveValue(exe_ctx); |
1596 | } |
1597 | break; |
1598 | |
1599 | // OPCODE: DW_OP_shr |
1600 | // OPERANDS: none |
1601 | // DESCRIPTION: pops the top two stack entries, shifts the former second |
1602 | // entry right logically (filling with zero bits) by the number of bits |
1603 | // specified by the former top of the stack, and pushes the result. |
1604 | case DW_OP_shr: |
1605 | if (stack.size() < 2) { |
1606 | if (error_ptr) |
1607 | error_ptr->SetErrorString( |
1608 | "Expression stack needs at least 2 items for DW_OP_shr."); |
1609 | return false; |
1610 | } else { |
1611 | tmp = stack.back(); |
1612 | stack.pop_back(); |
1613 | if (!stack.back().ResolveValue(exe_ctx).ShiftRightLogical( |
1614 | tmp.ResolveValue(exe_ctx))) { |
1615 | if (error_ptr) |
1616 | error_ptr->SetErrorString("DW_OP_shr failed."); |
1617 | return false; |
1618 | } |
1619 | } |
1620 | break; |
1621 | |
1622 | // OPCODE: DW_OP_shra |
1623 | // OPERANDS: none |
1624 | // DESCRIPTION: pops the top two stack entries, shifts the former second |
1625 | // entry right arithmetically (divide the magnitude by 2, keep the same |
1626 | // sign for the result) by the number of bits specified by the former top |
1627 | // of the stack, and pushes the result. |
1628 | case DW_OP_shra: |
1629 | if (stack.size() < 2) { |
1630 | if (error_ptr) |
1631 | error_ptr->SetErrorString( |
1632 | "Expression stack needs at least 2 items for DW_OP_shra."); |
1633 | return false; |
1634 | } else { |
1635 | tmp = stack.back(); |
1636 | stack.pop_back(); |
1637 | stack.back().ResolveValue(exe_ctx) >>= tmp.ResolveValue(exe_ctx); |
1638 | } |
1639 | break; |
1640 | |
1641 | // OPCODE: DW_OP_xor |
1642 | // OPERANDS: none |
1643 | // DESCRIPTION: pops the top two stack entries, performs the bitwise |
1644 | // exclusive-or operation on the two, and pushes the result. |
1645 | case DW_OP_xor: |
1646 | if (stack.size() < 2) { |
1647 | if (error_ptr) |
1648 | error_ptr->SetErrorString( |
1649 | "Expression stack needs at least 2 items for DW_OP_xor."); |
1650 | return false; |
1651 | } else { |
1652 | tmp = stack.back(); |
1653 | stack.pop_back(); |
1654 | stack.back().ResolveValue(exe_ctx) = |
1655 | stack.back().ResolveValue(exe_ctx) ^ tmp.ResolveValue(exe_ctx); |
1656 | } |
1657 | break; |
1658 | |
1659 | // OPCODE: DW_OP_skip |
1660 | // OPERANDS: int16_t |
1661 | // DESCRIPTION: An unconditional branch. Its single operand is a 2-byte |
1662 | // signed integer constant. The 2-byte constant is the number of bytes of |
1663 | // the DWARF expression to skip forward or backward from the current |
1664 | // operation, beginning after the 2-byte constant. |
1665 | case DW_OP_skip: { |
1666 | int16_t skip_offset = (int16_t)opcodes.GetU16(&offset); |
1667 | lldb::offset_t new_offset = offset + skip_offset; |
1668 | // New offset can point at the end of the data, in this case we should |
1669 | // terminate the DWARF expression evaluation (will happen in the loop |
1670 | // condition). |
1671 | if (new_offset <= opcodes.GetByteSize()) |
1672 | offset = new_offset; |
1673 | else { |
1674 | if (error_ptr) |
1675 | error_ptr->SetErrorStringWithFormatv( |
1676 | "Invalid opcode offset in DW_OP_skip: {0}+({1}) > {2}", offset, |
1677 | skip_offset, opcodes.GetByteSize()); |
1678 | return false; |
1679 | } |
1680 | } break; |
1681 | |
1682 | // OPCODE: DW_OP_bra |
1683 | // OPERANDS: int16_t |
1684 | // DESCRIPTION: A conditional branch. Its single operand is a 2-byte |
1685 | // signed integer constant. This operation pops the top of stack. If the |
1686 | // value popped is not the constant 0, the 2-byte constant operand is the |
1687 | // number of bytes of the DWARF expression to skip forward or backward from |
1688 | // the current operation, beginning after the 2-byte constant. |
1689 | case DW_OP_bra: |
1690 | if (stack.empty()) { |
1691 | if (error_ptr) |
1692 | error_ptr->SetErrorString( |
1693 | "Expression stack needs at least 1 item for DW_OP_bra."); |
1694 | return false; |
1695 | } else { |
1696 | tmp = stack.back(); |
1697 | stack.pop_back(); |
1698 | int16_t bra_offset = (int16_t)opcodes.GetU16(&offset); |
1699 | Scalar zero(0); |
1700 | if (tmp.ResolveValue(exe_ctx) != zero) { |
1701 | lldb::offset_t new_offset = offset + bra_offset; |
1702 | // New offset can point at the end of the data, in this case we should |
1703 | // terminate the DWARF expression evaluation (will happen in the loop |
1704 | // condition). |
1705 | if (new_offset <= opcodes.GetByteSize()) |
1706 | offset = new_offset; |
1707 | else { |
1708 | if (error_ptr) |
1709 | error_ptr->SetErrorStringWithFormatv( |
1710 | "Invalid opcode offset in DW_OP_bra: {0}+({1}) > {2}", offset, |
1711 | bra_offset, opcodes.GetByteSize()); |
1712 | return false; |
1713 | } |
1714 | } |
1715 | } |
1716 | break; |
1717 | |
1718 | // OPCODE: DW_OP_eq |
1719 | // OPERANDS: none |
1720 | // DESCRIPTION: pops the top two stack values, compares using the |
1721 | // equals (==) operator. |
1722 | // STACK RESULT: push the constant value 1 onto the stack if the result |
1723 | // of the operation is true or the constant value 0 if the result of the |
1724 | // operation is false. |
1725 | case DW_OP_eq: |
1726 | if (stack.size() < 2) { |
1727 | if (error_ptr) |
1728 | error_ptr->SetErrorString( |
1729 | "Expression stack needs at least 2 items for DW_OP_eq."); |
1730 | return false; |
1731 | } else { |
1732 | tmp = stack.back(); |
1733 | stack.pop_back(); |
1734 | stack.back().ResolveValue(exe_ctx) = |
1735 | stack.back().ResolveValue(exe_ctx) == tmp.ResolveValue(exe_ctx); |
1736 | } |
1737 | break; |
1738 | |
1739 | // OPCODE: DW_OP_ge |
1740 | // OPERANDS: none |
1741 | // DESCRIPTION: pops the top two stack values, compares using the |
1742 | // greater than or equal to (>=) operator. |
1743 | // STACK RESULT: push the constant value 1 onto the stack if the result |
1744 | // of the operation is true or the constant value 0 if the result of the |
1745 | // operation is false. |
1746 | case DW_OP_ge: |
1747 | if (stack.size() < 2) { |
1748 | if (error_ptr) |
1749 | error_ptr->SetErrorString( |
1750 | "Expression stack needs at least 2 items for DW_OP_ge."); |
1751 | return false; |
1752 | } else { |
1753 | tmp = stack.back(); |
1754 | stack.pop_back(); |
1755 | stack.back().ResolveValue(exe_ctx) = |
1756 | stack.back().ResolveValue(exe_ctx) >= tmp.ResolveValue(exe_ctx); |
1757 | } |
1758 | break; |
1759 | |
1760 | // OPCODE: DW_OP_gt |
1761 | // OPERANDS: none |
1762 | // DESCRIPTION: pops the top two stack values, compares using the |
1763 | // greater than (>) operator. |
1764 | // STACK RESULT: push the constant value 1 onto the stack if the result |
1765 | // of the operation is true or the constant value 0 if the result of the |
1766 | // operation is false. |
1767 | case DW_OP_gt: |
1768 | if (stack.size() < 2) { |
1769 | if (error_ptr) |
1770 | error_ptr->SetErrorString( |
1771 | "Expression stack needs at least 2 items for DW_OP_gt."); |
1772 | return false; |
1773 | } else { |
1774 | tmp = stack.back(); |
1775 | stack.pop_back(); |
1776 | stack.back().ResolveValue(exe_ctx) = |
1777 | stack.back().ResolveValue(exe_ctx) > tmp.ResolveValue(exe_ctx); |
1778 | } |
1779 | break; |
1780 | |
1781 | // OPCODE: DW_OP_le |
1782 | // OPERANDS: none |
1783 | // DESCRIPTION: pops the top two stack values, compares using the |
1784 | // less than or equal to (<=) operator. |
1785 | // STACK RESULT: push the constant value 1 onto the stack if the result |
1786 | // of the operation is true or the constant value 0 if the result of the |
1787 | // operation is false. |
1788 | case DW_OP_le: |
1789 | if (stack.size() < 2) { |
1790 | if (error_ptr) |
1791 | error_ptr->SetErrorString( |
1792 | "Expression stack needs at least 2 items for DW_OP_le."); |
1793 | return false; |
1794 | } else { |
1795 | tmp = stack.back(); |
1796 | stack.pop_back(); |
1797 | stack.back().ResolveValue(exe_ctx) = |
1798 | stack.back().ResolveValue(exe_ctx) <= tmp.ResolveValue(exe_ctx); |
1799 | } |
1800 | break; |
1801 | |
1802 | // OPCODE: DW_OP_lt |
1803 | // OPERANDS: none |
1804 | // DESCRIPTION: pops the top two stack values, compares using the |
1805 | // less than (<) operator. |
1806 | // STACK RESULT: push the constant value 1 onto the stack if the result |
1807 | // of the operation is true or the constant value 0 if the result of the |
1808 | // operation is false. |
1809 | case DW_OP_lt: |
1810 | if (stack.size() < 2) { |
1811 | if (error_ptr) |
1812 | error_ptr->SetErrorString( |
1813 | "Expression stack needs at least 2 items for DW_OP_lt."); |
1814 | return false; |
1815 | } else { |
1816 | tmp = stack.back(); |
1817 | stack.pop_back(); |
1818 | stack.back().ResolveValue(exe_ctx) = |
1819 | stack.back().ResolveValue(exe_ctx) < tmp.ResolveValue(exe_ctx); |
1820 | } |
1821 | break; |
1822 | |
1823 | // OPCODE: DW_OP_ne |
1824 | // OPERANDS: none |
1825 | // DESCRIPTION: pops the top two stack values, compares using the |
1826 | // not equal (!=) operator. |
1827 | // STACK RESULT: push the constant value 1 onto the stack if the result |
1828 | // of the operation is true or the constant value 0 if the result of the |
1829 | // operation is false. |
1830 | case DW_OP_ne: |
1831 | if (stack.size() < 2) { |
1832 | if (error_ptr) |
1833 | error_ptr->SetErrorString( |
1834 | "Expression stack needs at least 2 items for DW_OP_ne."); |
1835 | return false; |
1836 | } else { |
1837 | tmp = stack.back(); |
1838 | stack.pop_back(); |
1839 | stack.back().ResolveValue(exe_ctx) = |
1840 | stack.back().ResolveValue(exe_ctx) != tmp.ResolveValue(exe_ctx); |
1841 | } |
1842 | break; |
1843 | |
1844 | // OPCODE: DW_OP_litn |
1845 | // OPERANDS: none |
1846 | // DESCRIPTION: encode the unsigned literal values from 0 through 31. |
1847 | // STACK RESULT: push the unsigned literal constant value onto the top |
1848 | // of the stack. |
1849 | case DW_OP_lit0: |
1850 | case DW_OP_lit1: |
1851 | case DW_OP_lit2: |
1852 | case DW_OP_lit3: |
1853 | case DW_OP_lit4: |
1854 | case DW_OP_lit5: |
1855 | case DW_OP_lit6: |
1856 | case DW_OP_lit7: |
1857 | case DW_OP_lit8: |
1858 | case DW_OP_lit9: |
1859 | case DW_OP_lit10: |
1860 | case DW_OP_lit11: |
1861 | case DW_OP_lit12: |
1862 | case DW_OP_lit13: |
1863 | case DW_OP_lit14: |
1864 | case DW_OP_lit15: |
1865 | case DW_OP_lit16: |
1866 | case DW_OP_lit17: |
1867 | case DW_OP_lit18: |
1868 | case DW_OP_lit19: |
1869 | case DW_OP_lit20: |
1870 | case DW_OP_lit21: |
1871 | case DW_OP_lit22: |
1872 | case DW_OP_lit23: |
1873 | case DW_OP_lit24: |
1874 | case DW_OP_lit25: |
1875 | case DW_OP_lit26: |
1876 | case DW_OP_lit27: |
1877 | case DW_OP_lit28: |
1878 | case DW_OP_lit29: |
1879 | case DW_OP_lit30: |
1880 | case DW_OP_lit31: |
1881 | stack.push_back(to_generic(op - DW_OP_lit0)); |
1882 | break; |
1883 | |
1884 | // OPCODE: DW_OP_regN |
1885 | // OPERANDS: none |
1886 | // DESCRIPTION: Push the value in register n on the top of the stack. |
1887 | case DW_OP_reg0: |
1888 | case DW_OP_reg1: |
1889 | case DW_OP_reg2: |
1890 | case DW_OP_reg3: |
1891 | case DW_OP_reg4: |
1892 | case DW_OP_reg5: |
1893 | case DW_OP_reg6: |
1894 | case DW_OP_reg7: |
1895 | case DW_OP_reg8: |
1896 | case DW_OP_reg9: |
1897 | case DW_OP_reg10: |
1898 | case DW_OP_reg11: |
1899 | case DW_OP_reg12: |
1900 | case DW_OP_reg13: |
1901 | case DW_OP_reg14: |
1902 | case DW_OP_reg15: |
1903 | case DW_OP_reg16: |
1904 | case DW_OP_reg17: |
1905 | case DW_OP_reg18: |
1906 | case DW_OP_reg19: |
1907 | case DW_OP_reg20: |
1908 | case DW_OP_reg21: |
1909 | case DW_OP_reg22: |
1910 | case DW_OP_reg23: |
1911 | case DW_OP_reg24: |
1912 | case DW_OP_reg25: |
1913 | case DW_OP_reg26: |
1914 | case DW_OP_reg27: |
1915 | case DW_OP_reg28: |
1916 | case DW_OP_reg29: |
1917 | case DW_OP_reg30: |
1918 | case DW_OP_reg31: { |
1919 | dwarf4_location_description_kind = Register; |
1920 | reg_num = op - DW_OP_reg0; |
1921 | |
1922 | if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp)) |
1923 | stack.push_back(tmp); |
1924 | else |
1925 | return false; |
1926 | } break; |
1927 | // OPCODE: DW_OP_regx |
1928 | // OPERANDS: |
1929 | // ULEB128 literal operand that encodes the register. |
1930 | // DESCRIPTION: Push the value in register on the top of the stack. |
1931 | case DW_OP_regx: { |
1932 | dwarf4_location_description_kind = Register; |
1933 | reg_num = opcodes.GetULEB128(&offset); |
1934 | if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, tmp)) |
1935 | stack.push_back(tmp); |
1936 | else |
1937 | return false; |
1938 | } break; |
1939 | |
1940 | // OPCODE: DW_OP_bregN |
1941 | // OPERANDS: |
1942 | // SLEB128 offset from register N |
1943 | // DESCRIPTION: Value is in memory at the address specified by register |
1944 | // N plus an offset. |
1945 | case DW_OP_breg0: |
1946 | case DW_OP_breg1: |
1947 | case DW_OP_breg2: |
1948 | case DW_OP_breg3: |
1949 | case DW_OP_breg4: |
1950 | case DW_OP_breg5: |
1951 | case DW_OP_breg6: |
1952 | case DW_OP_breg7: |
1953 | case DW_OP_breg8: |
1954 | case DW_OP_breg9: |
1955 | case DW_OP_breg10: |
1956 | case DW_OP_breg11: |
1957 | case DW_OP_breg12: |
1958 | case DW_OP_breg13: |
1959 | case DW_OP_breg14: |
1960 | case DW_OP_breg15: |
1961 | case DW_OP_breg16: |
1962 | case DW_OP_breg17: |
1963 | case DW_OP_breg18: |
1964 | case DW_OP_breg19: |
1965 | case DW_OP_breg20: |
1966 | case DW_OP_breg21: |
1967 | case DW_OP_breg22: |
1968 | case DW_OP_breg23: |
1969 | case DW_OP_breg24: |
1970 | case DW_OP_breg25: |
1971 | case DW_OP_breg26: |
1972 | case DW_OP_breg27: |
1973 | case DW_OP_breg28: |
1974 | case DW_OP_breg29: |
1975 | case DW_OP_breg30: |
1976 | case DW_OP_breg31: { |
1977 | reg_num = op - DW_OP_breg0; |
1978 | |
1979 | if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, |
1980 | tmp)) { |
1981 | int64_t breg_offset = opcodes.GetSLEB128(&offset); |
1982 | tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; |
1983 | tmp.ClearContext(); |
1984 | stack.push_back(tmp); |
1985 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
1986 | } else |
1987 | return false; |
1988 | } break; |
1989 | // OPCODE: DW_OP_bregx |
1990 | // OPERANDS: 2 |
1991 | // ULEB128 literal operand that encodes the register. |
1992 | // SLEB128 offset from register N |
1993 | // DESCRIPTION: Value is in memory at the address specified by register |
1994 | // N plus an offset. |
1995 | case DW_OP_bregx: { |
1996 | reg_num = opcodes.GetULEB128(&offset); |
1997 | |
1998 | if (ReadRegisterValueAsScalar(reg_ctx, reg_kind, reg_num, error_ptr, |
1999 | tmp)) { |
2000 | int64_t breg_offset = opcodes.GetSLEB128(&offset); |
2001 | tmp.ResolveValue(exe_ctx) += (uint64_t)breg_offset; |
2002 | tmp.ClearContext(); |
2003 | stack.push_back(tmp); |
2004 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
2005 | } else |
2006 | return false; |
2007 | } break; |
2008 | |
2009 | case DW_OP_fbreg: |
2010 | if (exe_ctx) { |
2011 | if (frame) { |
2012 | Scalar value; |
2013 | if (frame->GetFrameBaseValue(value, error_ptr)) { |
2014 | int64_t fbreg_offset = opcodes.GetSLEB128(&offset); |
2015 | value += fbreg_offset; |
2016 | stack.push_back(value); |
2017 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
2018 | } else |
2019 | return false; |
2020 | } else { |
2021 | if (error_ptr) |
2022 | error_ptr->SetErrorString( |
2023 | "Invalid stack frame in context for DW_OP_fbreg opcode."); |
2024 | return false; |
2025 | } |
2026 | } else { |
2027 | if (error_ptr) |
2028 | error_ptr->SetErrorString( |
2029 | "NULL execution context for DW_OP_fbreg.\n"); |
2030 | return false; |
2031 | } |
2032 | |
2033 | break; |
2034 | |
2035 | // OPCODE: DW_OP_nop |
2036 | // OPERANDS: none |
2037 | // DESCRIPTION: A place holder. It has no effect on the location stack |
2038 | // or any of its values. |
2039 | case DW_OP_nop: |
2040 | break; |
2041 | |
2042 | // OPCODE: DW_OP_piece |
2043 | // OPERANDS: 1 |
2044 | // ULEB128: byte size of the piece |
2045 | // DESCRIPTION: The operand describes the size in bytes of the piece of |
2046 | // the object referenced by the DWARF expression whose result is at the top |
2047 | // of the stack. If the piece is located in a register, but does not occupy |
2048 | // the entire register, the placement of the piece within that register is |
2049 | // defined by the ABI. |
2050 | // |
2051 | // Many compilers store a single variable in sets of registers, or store a |
2052 | // variable partially in memory and partially in registers. DW_OP_piece |
2053 | // provides a way of describing how large a part of a variable a particular |
2054 | // DWARF expression refers to. |
2055 | case DW_OP_piece: { |
2056 | LocationDescriptionKind piece_locdesc = dwarf4_location_description_kind; |
2057 | // Reset for the next piece. |
2058 | dwarf4_location_description_kind = Memory; |
2059 | |
2060 | const uint64_t piece_byte_size = opcodes.GetULEB128(&offset); |
2061 | |
2062 | if (piece_byte_size > 0) { |
2063 | Value curr_piece; |
2064 | |
2065 | if (stack.empty()) { |
2066 | UpdateValueTypeFromLocationDescription( |
2067 | log, dwarf_cu, LocationDescriptionKind::Empty); |
2068 | // In a multi-piece expression, this means that the current piece is |
2069 | // not available. Fill with zeros for now by resizing the data and |
2070 | // appending it |
2071 | curr_piece.ResizeData(piece_byte_size); |
2072 | // Note that "0" is not a correct value for the unknown bits. |
2073 | // It would be better to also return a mask of valid bits together |
2074 | // with the expression result, so the debugger can print missing |
2075 | // members as "<optimized out>" or something. |
2076 | ::memset(curr_piece.GetBuffer().GetBytes(), 0, piece_byte_size); |
2077 | pieces.AppendDataToHostBuffer(curr_piece); |
2078 | } else { |
2079 | Status error; |
2080 | // Extract the current piece into "curr_piece" |
2081 | Value curr_piece_source_value(stack.back()); |
2082 | stack.pop_back(); |
2083 | UpdateValueTypeFromLocationDescription(log, dwarf_cu, piece_locdesc, |
2084 | &curr_piece_source_value); |
2085 | |
2086 | const Value::ValueType curr_piece_source_value_type = |
2087 | curr_piece_source_value.GetValueType(); |
2088 | switch (curr_piece_source_value_type) { |
2089 | case Value::ValueType::Invalid: |
2090 | return false; |
2091 | case Value::ValueType::LoadAddress: |
2092 | if (process) { |
2093 | if (curr_piece.ResizeData(piece_byte_size) == piece_byte_size) { |
2094 | lldb::addr_t load_addr = |
2095 | curr_piece_source_value.GetScalar().ULongLong( |
2096 | LLDB_INVALID_ADDRESS(18446744073709551615UL)); |
2097 | if (process->ReadMemory( |
2098 | load_addr, curr_piece.GetBuffer().GetBytes(), |
2099 | piece_byte_size, error) != piece_byte_size) { |
2100 | if (error_ptr) |
2101 | error_ptr->SetErrorStringWithFormat( |
2102 | "failed to read memory DW_OP_piece(%" PRIu64"l" "u" |
2103 | ") from 0x%" PRIx64"l" "x", |
2104 | piece_byte_size, load_addr); |
2105 | return false; |
2106 | } |
2107 | } else { |
2108 | if (error_ptr) |
2109 | error_ptr->SetErrorStringWithFormat( |
2110 | "failed to resize the piece memory buffer for " |
2111 | "DW_OP_piece(%" PRIu64"l" "u" ")", |
2112 | piece_byte_size); |
2113 | return false; |
2114 | } |
2115 | } |
2116 | break; |
2117 | |
2118 | case Value::ValueType::FileAddress: |
2119 | case Value::ValueType::HostAddress: |
2120 | if (error_ptr) { |
2121 | lldb::addr_t addr = curr_piece_source_value.GetScalar().ULongLong( |
2122 | LLDB_INVALID_ADDRESS(18446744073709551615UL)); |
2123 | error_ptr->SetErrorStringWithFormat( |
2124 | "failed to read memory DW_OP_piece(%" PRIu64"l" "u" |
2125 | ") from %s address 0x%" PRIx64"l" "x", |
2126 | piece_byte_size, curr_piece_source_value.GetValueType() == |
2127 | Value::ValueType::FileAddress |
2128 | ? "file" |
2129 | : "host", |
2130 | addr); |
2131 | } |
2132 | return false; |
2133 | |
2134 | case Value::ValueType::Scalar: { |
2135 | uint32_t bit_size = piece_byte_size * 8; |
2136 | uint32_t bit_offset = 0; |
2137 | Scalar &scalar = curr_piece_source_value.GetScalar(); |
2138 | if (!scalar.ExtractBitfield( |
2139 | bit_size, bit_offset)) { |
2140 | if (error_ptr) |
2141 | error_ptr->SetErrorStringWithFormat( |
2142 | "unable to extract %" PRIu64"l" "u" " bytes from a %" PRIu64"l" "u" |
2143 | " byte scalar value.", |
2144 | piece_byte_size, |
2145 | (uint64_t)curr_piece_source_value.GetScalar() |
2146 | .GetByteSize()); |
2147 | return false; |
2148 | } |
2149 | // Create curr_piece with bit_size. By default Scalar |
2150 | // grows to the nearest host integer type. |
2151 | llvm::APInt fail_value(1, 0, false); |
2152 | llvm::APInt ap_int = scalar.UInt128(fail_value); |
2153 | 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", 2153, __extension__ __PRETTY_FUNCTION__)); |
2154 | llvm::ArrayRef<uint64_t> buf{ap_int.getRawData(), |
2155 | ap_int.getNumWords()}; |
2156 | curr_piece.GetScalar() = Scalar(llvm::APInt(bit_size, buf)); |
2157 | } break; |
2158 | } |
2159 | |
2160 | // Check if this is the first piece? |
2161 | if (op_piece_offset == 0) { |
2162 | // This is the first piece, we should push it back onto the stack |
2163 | // so subsequent pieces will be able to access this piece and add |
2164 | // to it. |
2165 | if (pieces.AppendDataToHostBuffer(curr_piece) == 0) { |
2166 | if (error_ptr) |
2167 | error_ptr->SetErrorString("failed to append piece data"); |
2168 | return false; |
2169 | } |
2170 | } else { |
2171 | // If this is the second or later piece there should be a value on |
2172 | // the stack. |
2173 | if (pieces.GetBuffer().GetByteSize() != op_piece_offset) { |
2174 | if (error_ptr) |
2175 | error_ptr->SetErrorStringWithFormat( |
2176 | "DW_OP_piece for offset %" PRIu64"l" "u" |
2177 | " but top of stack is of size %" PRIu64"l" "u", |
2178 | op_piece_offset, pieces.GetBuffer().GetByteSize()); |
2179 | return false; |
2180 | } |
2181 | |
2182 | if (pieces.AppendDataToHostBuffer(curr_piece) == 0) { |
2183 | if (error_ptr) |
2184 | error_ptr->SetErrorString("failed to append piece data"); |
2185 | return false; |
2186 | } |
2187 | } |
2188 | } |
2189 | op_piece_offset += piece_byte_size; |
2190 | } |
2191 | } break; |
2192 | |
2193 | case DW_OP_bit_piece: // 0x9d ULEB128 bit size, ULEB128 bit offset (DWARF3); |
2194 | if (stack.size() < 1) { |
2195 | UpdateValueTypeFromLocationDescription(log, dwarf_cu, |
2196 | LocationDescriptionKind::Empty); |
2197 | // Reset for the next piece. |
2198 | dwarf4_location_description_kind = Memory; |
2199 | if (error_ptr) |
2200 | error_ptr->SetErrorString( |
2201 | "Expression stack needs at least 1 item for DW_OP_bit_piece."); |
2202 | return false; |
2203 | } else { |
2204 | UpdateValueTypeFromLocationDescription( |
2205 | log, dwarf_cu, dwarf4_location_description_kind, &stack.back()); |
2206 | // Reset for the next piece. |
2207 | dwarf4_location_description_kind = Memory; |
2208 | const uint64_t piece_bit_size = opcodes.GetULEB128(&offset); |
2209 | const uint64_t piece_bit_offset = opcodes.GetULEB128(&offset); |
2210 | switch (stack.back().GetValueType()) { |
2211 | case Value::ValueType::Invalid: |
2212 | return false; |
2213 | case Value::ValueType::Scalar: { |
2214 | if (!stack.back().GetScalar().ExtractBitfield(piece_bit_size, |
2215 | piece_bit_offset)) { |
2216 | if (error_ptr) |
2217 | error_ptr->SetErrorStringWithFormat( |
2218 | "unable to extract %" PRIu64"l" "u" " bit value with %" PRIu64"l" "u" |
2219 | " bit offset from a %" PRIu64"l" "u" " bit scalar value.", |
2220 | piece_bit_size, piece_bit_offset, |
2221 | (uint64_t)(stack.back().GetScalar().GetByteSize() * 8)); |
2222 | return false; |
2223 | } |
2224 | } break; |
2225 | |
2226 | case Value::ValueType::FileAddress: |
2227 | case Value::ValueType::LoadAddress: |
2228 | case Value::ValueType::HostAddress: |
2229 | if (error_ptr) { |
2230 | error_ptr->SetErrorStringWithFormat( |
2231 | "unable to extract DW_OP_bit_piece(bit_size = %" PRIu64"l" "u" |
2232 | ", bit_offset = %" PRIu64"l" "u" ") from an address value.", |
2233 | piece_bit_size, piece_bit_offset); |
2234 | } |
2235 | return false; |
2236 | } |
2237 | } |
2238 | break; |
2239 | |
2240 | // OPCODE: DW_OP_implicit_value |
2241 | // OPERANDS: 2 |
2242 | // ULEB128 size of the value block in bytes |
2243 | // uint8_t* block bytes encoding value in target's memory |
2244 | // representation |
2245 | // DESCRIPTION: Value is immediately stored in block in the debug info with |
2246 | // the memory representation of the target. |
2247 | case DW_OP_implicit_value: { |
2248 | dwarf4_location_description_kind = Implicit; |
2249 | |
2250 | const uint32_t len = opcodes.GetULEB128(&offset); |
2251 | const void *data = opcodes.GetData(&offset, len); |
2252 | |
2253 | if (!data) { |
2254 | 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); |
2255 | 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); |
2256 | 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);; |
2257 | return false; |
2258 | } |
2259 | |
2260 | Value result(data, len); |
2261 | stack.push_back(result); |
2262 | break; |
2263 | } |
2264 | |
2265 | case DW_OP_implicit_pointer: { |
2266 | dwarf4_location_description_kind = Implicit; |
Value stored to 'dwarf4_location_description_kind' is never read | |
2267 | 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);; |
2268 | return false; |
2269 | } |
2270 | |
2271 | // OPCODE: DW_OP_push_object_address |
2272 | // OPERANDS: none |
2273 | // DESCRIPTION: Pushes the address of the object currently being |
2274 | // evaluated as part of evaluation of a user presented expression. This |
2275 | // object may correspond to an independent variable described by its own |
2276 | // DIE or it may be a component of an array, structure, or class whose |
2277 | // address has been dynamically determined by an earlier step during user |
2278 | // expression evaluation. |
2279 | case DW_OP_push_object_address: |
2280 | if (object_address_ptr) |
2281 | stack.push_back(*object_address_ptr); |
2282 | else { |
2283 | if (error_ptr) |
2284 | error_ptr->SetErrorString("DW_OP_push_object_address used without " |
2285 | "specifying an object address"); |
2286 | return false; |
2287 | } |
2288 | break; |
2289 | |
2290 | // OPCODE: DW_OP_call2 |
2291 | // OPERANDS: |
2292 | // uint16_t compile unit relative offset of a DIE |
2293 | // DESCRIPTION: Performs subroutine calls during evaluation |
2294 | // of a DWARF expression. The operand is the 2-byte unsigned offset of a |
2295 | // debugging information entry in the current compilation unit. |
2296 | // |
2297 | // Operand interpretation is exactly like that for DW_FORM_ref2. |
2298 | // |
2299 | // This operation transfers control of DWARF expression evaluation to the |
2300 | // DW_AT_location attribute of the referenced DIE. If there is no such |
2301 | // attribute, then there is no effect. Execution of the DWARF expression of |
2302 | // a DW_AT_location attribute may add to and/or remove from values on the |
2303 | // stack. Execution returns to the point following the call when the end of |
2304 | // the attribute is reached. Values on the stack at the time of the call |
2305 | // may be used as parameters by the called expression and values left on |
2306 | // the stack by the called expression may be used as return values by prior |
2307 | // agreement between the calling and called expressions. |
2308 | case DW_OP_call2: |
2309 | if (error_ptr) |
2310 | error_ptr->SetErrorString("Unimplemented opcode DW_OP_call2."); |
2311 | return false; |
2312 | // OPCODE: DW_OP_call4 |
2313 | // OPERANDS: 1 |
2314 | // uint32_t compile unit relative offset of a DIE |
2315 | // DESCRIPTION: Performs a subroutine call during evaluation of a DWARF |
2316 | // expression. For DW_OP_call4, the operand is a 4-byte unsigned offset of |
2317 | // a debugging information entry in the current compilation unit. |
2318 | // |
2319 | // Operand interpretation DW_OP_call4 is exactly like that for |
2320 | // DW_FORM_ref4. |
2321 | // |
2322 | // This operation transfers control of DWARF expression evaluation to the |
2323 | // DW_AT_location attribute of the referenced DIE. If there is no such |
2324 | // attribute, then there is no effect. Execution of the DWARF expression of |
2325 | // a DW_AT_location attribute may add to and/or remove from values on the |
2326 | // stack. Execution returns to the point following the call when the end of |
2327 | // the attribute is reached. Values on the stack at the time of the call |
2328 | // may be used as parameters by the called expression and values left on |
2329 | // the stack by the called expression may be used as return values by prior |
2330 | // agreement between the calling and called expressions. |
2331 | case DW_OP_call4: |
2332 | if (error_ptr) |
2333 | error_ptr->SetErrorString("Unimplemented opcode DW_OP_call4."); |
2334 | return false; |
2335 | |
2336 | // OPCODE: DW_OP_stack_value |
2337 | // OPERANDS: None |
2338 | // DESCRIPTION: Specifies that the object does not exist in memory but |
2339 | // rather is a constant value. The value from the top of the stack is the |
2340 | // value to be used. This is the actual object value and not the location. |
2341 | case DW_OP_stack_value: |
2342 | dwarf4_location_description_kind = Implicit; |
2343 | if (stack.empty()) { |
2344 | if (error_ptr) |
2345 | error_ptr->SetErrorString( |
2346 | "Expression stack needs at least 1 item for DW_OP_stack_value."); |
2347 | return false; |
2348 | } |
2349 | stack.back().SetValueType(Value::ValueType::Scalar); |
2350 | break; |
2351 | |
2352 | // OPCODE: DW_OP_convert |
2353 | // OPERANDS: 1 |
2354 | // A ULEB128 that is either a DIE offset of a |
2355 | // DW_TAG_base_type or 0 for the generic (pointer-sized) type. |
2356 | // |
2357 | // DESCRIPTION: Pop the top stack element, convert it to a |
2358 | // different type, and push the result. |
2359 | case DW_OP_convert: { |
2360 | if (stack.size() < 1) { |
2361 | if (error_ptr) |
2362 | error_ptr->SetErrorString( |
2363 | "Expression stack needs at least 1 item for DW_OP_convert."); |
2364 | return false; |
2365 | } |
2366 | const uint64_t die_offset = opcodes.GetULEB128(&offset); |
2367 | uint64_t bit_size; |
2368 | bool sign; |
2369 | if (die_offset == 0) { |
2370 | // The generic type has the size of an address on the target |
2371 | // machine and an unspecified signedness. Scalar has no |
2372 | // "unspecified signedness", so we use unsigned types. |
2373 | if (!module_sp) { |
2374 | if (error_ptr) |
2375 | error_ptr->SetErrorString("No module"); |
2376 | return false; |
2377 | } |
2378 | sign = false; |
2379 | bit_size = module_sp->GetArchitecture().GetAddressByteSize() * 8; |
2380 | if (!bit_size) { |
2381 | if (error_ptr) |
2382 | error_ptr->SetErrorString("unspecified architecture"); |
2383 | return false; |
2384 | } |
2385 | } else { |
2386 | // Retrieve the type DIE that the value is being converted to. This |
2387 | // offset is compile unit relative so we need to fix it up. |
2388 | const uint64_t abs_die_offset = die_offset + dwarf_cu->GetOffset(); |
2389 | // FIXME: the constness has annoying ripple effects. |
2390 | DWARFDIE die = const_cast<DWARFUnit *>(dwarf_cu)->GetDIE(abs_die_offset); |
2391 | if (!die) { |
2392 | if (error_ptr) |
2393 | error_ptr->SetErrorString("Cannot resolve DW_OP_convert type DIE"); |
2394 | return false; |
2395 | } |
2396 | uint64_t encoding = |
2397 | die.GetAttributeValueAsUnsigned(DW_AT_encoding, DW_ATE_hi_user); |
2398 | bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8; |
2399 | if (!bit_size) |
2400 | bit_size = die.GetAttributeValueAsUnsigned(DW_AT_bit_size, 0); |
2401 | if (!bit_size) { |
2402 | if (error_ptr) |
2403 | error_ptr->SetErrorString("Unsupported type size in DW_OP_convert"); |
2404 | return false; |
2405 | } |
2406 | switch (encoding) { |
2407 | case DW_ATE_signed: |
2408 | case DW_ATE_signed_char: |
2409 | sign = true; |
2410 | break; |
2411 | case DW_ATE_unsigned: |
2412 | case DW_ATE_unsigned_char: |
2413 | sign = false; |
2414 | break; |
2415 | default: |
2416 | if (error_ptr) |
2417 | error_ptr->SetErrorString("Unsupported encoding in DW_OP_convert"); |
2418 | return false; |
2419 | } |
2420 | } |
2421 | Scalar &top = stack.back().ResolveValue(exe_ctx); |
2422 | top.TruncOrExtendTo(bit_size, sign); |
2423 | break; |
2424 | } |
2425 | |
2426 | // OPCODE: DW_OP_call_frame_cfa |
2427 | // OPERANDS: None |
2428 | // DESCRIPTION: Specifies a DWARF expression that pushes the value of |
2429 | // the canonical frame address consistent with the call frame information |
2430 | // located in .debug_frame (or in the FDEs of the eh_frame section). |
2431 | case DW_OP_call_frame_cfa: |
2432 | if (frame) { |
2433 | // Note that we don't have to parse FDEs because this DWARF expression |
2434 | // is commonly evaluated with a valid stack frame. |
2435 | StackID id = frame->GetStackID(); |
2436 | addr_t cfa = id.GetCallFrameAddress(); |
2437 | if (cfa != LLDB_INVALID_ADDRESS(18446744073709551615UL)) { |
2438 | stack.push_back(Scalar(cfa)); |
2439 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
2440 | } else if (error_ptr) |
2441 | error_ptr->SetErrorString("Stack frame does not include a canonical " |
2442 | "frame address for DW_OP_call_frame_cfa " |
2443 | "opcode."); |
2444 | } else { |
2445 | if (error_ptr) |
2446 | error_ptr->SetErrorString("Invalid stack frame in context for " |
2447 | "DW_OP_call_frame_cfa opcode."); |
2448 | return false; |
2449 | } |
2450 | break; |
2451 | |
2452 | // OPCODE: DW_OP_form_tls_address (or the old pre-DWARFv3 vendor extension |
2453 | // opcode, DW_OP_GNU_push_tls_address) |
2454 | // OPERANDS: none |
2455 | // DESCRIPTION: Pops a TLS offset from the stack, converts it to |
2456 | // an address in the current thread's thread-local storage block, and |
2457 | // pushes it on the stack. |
2458 | case DW_OP_form_tls_address: |
2459 | case DW_OP_GNU_push_tls_address: { |
2460 | if (stack.size() < 1) { |
2461 | if (error_ptr) { |
2462 | if (op == DW_OP_form_tls_address) |
2463 | error_ptr->SetErrorString( |
2464 | "DW_OP_form_tls_address needs an argument."); |
2465 | else |
2466 | error_ptr->SetErrorString( |
2467 | "DW_OP_GNU_push_tls_address needs an argument."); |
2468 | } |
2469 | return false; |
2470 | } |
2471 | |
2472 | if (!exe_ctx || !module_sp) { |
2473 | if (error_ptr) |
2474 | error_ptr->SetErrorString("No context to evaluate TLS within."); |
2475 | return false; |
2476 | } |
2477 | |
2478 | Thread *thread = exe_ctx->GetThreadPtr(); |
2479 | if (!thread) { |
2480 | if (error_ptr) |
2481 | error_ptr->SetErrorString("No thread to evaluate TLS within."); |
2482 | return false; |
2483 | } |
2484 | |
2485 | // Lookup the TLS block address for this thread and module. |
2486 | const addr_t tls_file_addr = |
2487 | stack.back().GetScalar().ULongLong(LLDB_INVALID_ADDRESS(18446744073709551615UL)); |
2488 | const addr_t tls_load_addr = |
2489 | thread->GetThreadLocalData(module_sp, tls_file_addr); |
2490 | |
2491 | if (tls_load_addr == LLDB_INVALID_ADDRESS(18446744073709551615UL)) { |
2492 | if (error_ptr) |
2493 | error_ptr->SetErrorString( |
2494 | "No TLS data currently exists for this thread."); |
2495 | return false; |
2496 | } |
2497 | |
2498 | stack.back().GetScalar() = tls_load_addr; |
2499 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
2500 | } break; |
2501 | |
2502 | // OPCODE: DW_OP_addrx (DW_OP_GNU_addr_index is the legacy name.) |
2503 | // OPERANDS: 1 |
2504 | // ULEB128: index to the .debug_addr section |
2505 | // DESCRIPTION: Pushes an address to the stack from the .debug_addr |
2506 | // section with the base address specified by the DW_AT_addr_base attribute |
2507 | // and the 0 based index is the ULEB128 encoded index. |
2508 | case DW_OP_addrx: |
2509 | case DW_OP_GNU_addr_index: { |
2510 | if (!dwarf_cu) { |
2511 | if (error_ptr) |
2512 | error_ptr->SetErrorString("DW_OP_GNU_addr_index found without a " |
2513 | "compile unit being specified"); |
2514 | return false; |
2515 | } |
2516 | uint64_t index = opcodes.GetULEB128(&offset); |
2517 | lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index); |
2518 | stack.push_back(Scalar(value)); |
2519 | if (target && |
2520 | target->GetArchitecture().GetCore() == ArchSpec::eCore_wasm32) { |
2521 | // wasm file sections aren't mapped into memory, therefore addresses can |
2522 | // never point into a file section and are always LoadAddresses. |
2523 | stack.back().SetValueType(Value::ValueType::LoadAddress); |
2524 | } else { |
2525 | stack.back().SetValueType(Value::ValueType::FileAddress); |
2526 | } |
2527 | } break; |
2528 | |
2529 | // OPCODE: DW_OP_GNU_const_index |
2530 | // OPERANDS: 1 |
2531 | // ULEB128: index to the .debug_addr section |
2532 | // DESCRIPTION: Pushes an constant with the size of a machine address to |
2533 | // the stack from the .debug_addr section with the base address specified |
2534 | // by the DW_AT_addr_base attribute and the 0 based index is the ULEB128 |
2535 | // encoded index. |
2536 | case DW_OP_GNU_const_index: { |
2537 | if (!dwarf_cu) { |
2538 | if (error_ptr) |
2539 | error_ptr->SetErrorString("DW_OP_GNU_const_index found without a " |
2540 | "compile unit being specified"); |
2541 | return false; |
2542 | } |
2543 | uint64_t index = opcodes.GetULEB128(&offset); |
2544 | lldb::addr_t value = dwarf_cu->ReadAddressFromDebugAddrSection(index); |
2545 | stack.push_back(Scalar(value)); |
2546 | } break; |
2547 | |
2548 | case DW_OP_GNU_entry_value: |
2549 | case DW_OP_entry_value: { |
2550 | if (!Evaluate_DW_OP_entry_value(stack, exe_ctx, reg_ctx, opcodes, offset, |
2551 | error_ptr, log)) { |
2552 | 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); |
2553 | 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);; |
2554 | return false; |
2555 | } |
2556 | break; |
2557 | } |
2558 | |
2559 | default: |
2560 | if (error_ptr) |
2561 | error_ptr->SetErrorStringWithFormatv( |
2562 | "Unhandled opcode {0} in DWARFExpression", LocationAtom(op)); |
2563 | return false; |
2564 | } |
2565 | } |
2566 | |
2567 | if (stack.empty()) { |
2568 | // Nothing on the stack, check if we created a piece value from DW_OP_piece |
2569 | // or DW_OP_bit_piece opcodes |
2570 | if (pieces.GetBuffer().GetByteSize()) { |
2571 | result = pieces; |
2572 | return true; |
2573 | } |
2574 | if (error_ptr) |
2575 | error_ptr->SetErrorString("Stack empty after evaluation."); |
2576 | return false; |
2577 | } |
2578 | |
2579 | UpdateValueTypeFromLocationDescription( |
2580 | log, dwarf_cu, dwarf4_location_description_kind, &stack.back()); |
2581 | |
2582 | if (log && log->GetVerbose()) { |
2583 | size_t count = stack.size(); |
2584 | 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) |
2585 | "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); |
2586 | for (size_t i = 0; i < count; ++i) { |
2587 | StreamString new_value; |
2588 | new_value.Printf("[%" PRIu64"l" "u" "]", (uint64_t)i); |
2589 | stack[i].Dump(&new_value); |
2590 | 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); |
2591 | } |
2592 | } |
2593 | result = stack.back(); |
2594 | return true; // Return true on success |
2595 | } |
2596 | |
2597 | bool DWARFExpression::ParseDWARFLocationList( |
2598 | const DWARFUnit *dwarf_cu, const DataExtractor &data, |
2599 | DWARFExpressionList *location_list) { |
2600 | location_list->Clear(); |
2601 | std::unique_ptr<llvm::DWARFLocationTable> loctable_up = |
2602 | dwarf_cu->GetLocationTable(data); |
2603 | Log *log = GetLog(LLDBLog::Expressions); |
2604 | auto lookup_addr = |
2605 | [&](uint32_t index) -> llvm::Optional<llvm::object::SectionedAddress> { |
2606 | addr_t address = dwarf_cu->ReadAddressFromDebugAddrSection(index); |
2607 | if (address == LLDB_INVALID_ADDRESS(18446744073709551615UL)) |
2608 | return llvm::None; |
2609 | return llvm::object::SectionedAddress{address}; |
2610 | }; |
2611 | auto process_list = [&](llvm::Expected<llvm::DWARFLocationExpression> loc) { |
2612 | if (!loc) { |
2613 | 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); |
2614 | return true; |
2615 | } |
2616 | auto buffer_sp = |
2617 | std::make_shared<DataBufferHeap>(loc->Expr.data(), loc->Expr.size()); |
2618 | DWARFExpression expr = DWARFExpression(DataExtractor( |
2619 | buffer_sp, data.GetByteOrder(), data.GetAddressByteSize())); |
2620 | location_list->AddExpression(loc->Range->LowPC, loc->Range->HighPC, expr); |
2621 | return true; |
2622 | }; |
2623 | llvm::Error error = loctable_up->visitAbsoluteLocationList( |
2624 | 0, llvm::object::SectionedAddress{dwarf_cu->GetBaseAddress()}, |
2625 | lookup_addr, process_list); |
2626 | location_list->Sort(); |
2627 | if (error) { |
2628 | 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); |
2629 | return false; |
2630 | } |
2631 | return true; |
2632 | } |
2633 | |
2634 | bool DWARFExpression::MatchesOperand( |
2635 | StackFrame &frame, const Instruction::Operand &operand) const { |
2636 | using namespace OperandMatchers; |
2637 | |
2638 | RegisterContextSP reg_ctx_sp = frame.GetRegisterContext(); |
2639 | if (!reg_ctx_sp) { |
2640 | return false; |
2641 | } |
2642 | |
2643 | DataExtractor opcodes(m_data); |
2644 | |
2645 | lldb::offset_t op_offset = 0; |
2646 | uint8_t opcode = opcodes.GetU8(&op_offset); |
2647 | |
2648 | if (opcode == DW_OP_fbreg) { |
2649 | int64_t offset = opcodes.GetSLEB128(&op_offset); |
2650 | |
2651 | DWARFExpressionList *fb_expr = frame.GetFrameBaseExpression(nullptr); |
2652 | if (!fb_expr) { |
2653 | return false; |
2654 | } |
2655 | |
2656 | auto recurse = [&frame, fb_expr](const Instruction::Operand &child) { |
2657 | return fb_expr->MatchesOperand(frame, child); |
2658 | }; |
2659 | |
2660 | if (!offset && |
2661 | MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), |
2662 | recurse)(operand)) { |
2663 | return true; |
2664 | } |
2665 | |
2666 | return MatchUnaryOp( |
2667 | MatchOpType(Instruction::Operand::Type::Dereference), |
2668 | MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), |
2669 | MatchImmOp(offset), recurse))(operand); |
2670 | } |
2671 | |
2672 | bool dereference = false; |
2673 | const RegisterInfo *reg = nullptr; |
2674 | int64_t offset = 0; |
2675 | |
2676 | if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31) { |
2677 | reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_reg0); |
2678 | } else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31) { |
2679 | offset = opcodes.GetSLEB128(&op_offset); |
2680 | reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, opcode - DW_OP_breg0); |
2681 | } else if (opcode == DW_OP_regx) { |
2682 | uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset)); |
2683 | reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num); |
2684 | } else if (opcode == DW_OP_bregx) { |
2685 | uint32_t reg_num = static_cast<uint32_t>(opcodes.GetULEB128(&op_offset)); |
2686 | offset = opcodes.GetSLEB128(&op_offset); |
2687 | reg = reg_ctx_sp->GetRegisterInfo(m_reg_kind, reg_num); |
2688 | } else { |
2689 | return false; |
2690 | } |
2691 | |
2692 | if (!reg) { |
2693 | return false; |
2694 | } |
2695 | |
2696 | if (dereference) { |
2697 | if (!offset && |
2698 | MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), |
2699 | MatchRegOp(*reg))(operand)) { |
2700 | return true; |
2701 | } |
2702 | |
2703 | return MatchUnaryOp( |
2704 | MatchOpType(Instruction::Operand::Type::Dereference), |
2705 | MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), |
2706 | MatchRegOp(*reg), |
2707 | MatchImmOp(offset)))(operand); |
2708 | } else { |
2709 | return MatchRegOp(*reg)(operand); |
2710 | } |
2711 | } |