File: | build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/lldb/source/Target/StackFrame.cpp |
Warning: | line 565, column 11 Value stored to 'separator_idx' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- StackFrame.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/Target/StackFrame.h" |
10 | #include "lldb/Core/Debugger.h" |
11 | #include "lldb/Core/Disassembler.h" |
12 | #include "lldb/Core/FormatEntity.h" |
13 | #include "lldb/Core/Mangled.h" |
14 | #include "lldb/Core/Module.h" |
15 | #include "lldb/Core/Value.h" |
16 | #include "lldb/Core/ValueObjectConstResult.h" |
17 | #include "lldb/Core/ValueObjectMemory.h" |
18 | #include "lldb/Core/ValueObjectVariable.h" |
19 | #include "lldb/Symbol/CompileUnit.h" |
20 | #include "lldb/Symbol/Function.h" |
21 | #include "lldb/Symbol/Symbol.h" |
22 | #include "lldb/Symbol/SymbolContextScope.h" |
23 | #include "lldb/Symbol/Type.h" |
24 | #include "lldb/Symbol/VariableList.h" |
25 | #include "lldb/Target/ABI.h" |
26 | #include "lldb/Target/ExecutionContext.h" |
27 | #include "lldb/Target/Process.h" |
28 | #include "lldb/Target/RegisterContext.h" |
29 | #include "lldb/Target/StackFrameRecognizer.h" |
30 | #include "lldb/Target/Target.h" |
31 | #include "lldb/Target/Thread.h" |
32 | #include "lldb/Utility/LLDBLog.h" |
33 | #include "lldb/Utility/Log.h" |
34 | #include "lldb/Utility/RegisterValue.h" |
35 | |
36 | #include "lldb/lldb-enumerations.h" |
37 | |
38 | #include <memory> |
39 | |
40 | using namespace lldb; |
41 | using namespace lldb_private; |
42 | |
43 | // The first bits in the flags are reserved for the SymbolContext::Scope bits |
44 | // so we know if we have tried to look up information in our internal symbol |
45 | // context (m_sc) already. |
46 | #define RESOLVED_FRAME_CODE_ADDR(uint32_t(eSymbolContextEverything + 1)) (uint32_t(eSymbolContextEverything + 1)) |
47 | #define RESOLVED_FRAME_ID_SYMBOL_SCOPE((uint32_t(eSymbolContextEverything + 1)) << 1) (RESOLVED_FRAME_CODE_ADDR(uint32_t(eSymbolContextEverything + 1)) << 1) |
48 | #define GOT_FRAME_BASE(((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) (RESOLVED_FRAME_ID_SYMBOL_SCOPE((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) |
49 | #define RESOLVED_VARIABLES((((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) << 1) (GOT_FRAME_BASE(((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) << 1) |
50 | #define RESOLVED_GLOBAL_VARIABLES(((((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) << 1) << 1) (RESOLVED_VARIABLES((((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) << 1) << 1) |
51 | |
52 | StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, |
53 | user_id_t unwind_frame_index, addr_t cfa, |
54 | bool cfa_is_valid, addr_t pc, StackFrame::Kind kind, |
55 | bool behaves_like_zeroth_frame, |
56 | const SymbolContext *sc_ptr) |
57 | : m_thread_wp(thread_sp), m_frame_index(frame_idx), |
58 | m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(), |
59 | m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(), |
60 | m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid), |
61 | m_stack_frame_kind(kind), |
62 | m_behaves_like_zeroth_frame(behaves_like_zeroth_frame), |
63 | m_variable_list_sp(), m_variable_list_value_objects(), |
64 | m_recognized_frame_sp(), m_disassembly(), m_mutex() { |
65 | // If we don't have a CFA value, use the frame index for our StackID so that |
66 | // recursive functions properly aren't confused with one another on a history |
67 | // stack. |
68 | if (IsHistorical() && !m_cfa_is_valid) { |
69 | m_id.SetCFA(m_frame_index); |
70 | } |
71 | |
72 | if (sc_ptr != nullptr) { |
73 | m_sc = *sc_ptr; |
74 | m_flags.Set(m_sc.GetResolvedMask()); |
75 | } |
76 | } |
77 | |
78 | StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, |
79 | user_id_t unwind_frame_index, |
80 | const RegisterContextSP ®_context_sp, addr_t cfa, |
81 | addr_t pc, bool behaves_like_zeroth_frame, |
82 | const SymbolContext *sc_ptr) |
83 | : m_thread_wp(thread_sp), m_frame_index(frame_idx), |
84 | m_concrete_frame_index(unwind_frame_index), |
85 | m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr), |
86 | m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(), |
87 | m_frame_base_error(), m_cfa_is_valid(true), |
88 | m_stack_frame_kind(StackFrame::Kind::Regular), |
89 | m_behaves_like_zeroth_frame(behaves_like_zeroth_frame), |
90 | m_variable_list_sp(), m_variable_list_value_objects(), |
91 | m_recognized_frame_sp(), m_disassembly(), m_mutex() { |
92 | if (sc_ptr != nullptr) { |
93 | m_sc = *sc_ptr; |
94 | m_flags.Set(m_sc.GetResolvedMask()); |
95 | } |
96 | |
97 | if (reg_context_sp && !m_sc.target_sp) { |
98 | m_sc.target_sp = reg_context_sp->CalculateTarget(); |
99 | if (m_sc.target_sp) |
100 | m_flags.Set(eSymbolContextTarget); |
101 | } |
102 | } |
103 | |
104 | StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx, |
105 | user_id_t unwind_frame_index, |
106 | const RegisterContextSP ®_context_sp, addr_t cfa, |
107 | const Address &pc_addr, bool behaves_like_zeroth_frame, |
108 | const SymbolContext *sc_ptr) |
109 | : m_thread_wp(thread_sp), m_frame_index(frame_idx), |
110 | m_concrete_frame_index(unwind_frame_index), |
111 | m_reg_context_sp(reg_context_sp), |
112 | m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa, |
113 | nullptr), |
114 | m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(), |
115 | m_frame_base_error(), m_cfa_is_valid(true), |
116 | m_stack_frame_kind(StackFrame::Kind::Regular), |
117 | m_behaves_like_zeroth_frame(behaves_like_zeroth_frame), |
118 | m_variable_list_sp(), m_variable_list_value_objects(), |
119 | m_recognized_frame_sp(), m_disassembly(), m_mutex() { |
120 | if (sc_ptr != nullptr) { |
121 | m_sc = *sc_ptr; |
122 | m_flags.Set(m_sc.GetResolvedMask()); |
123 | } |
124 | |
125 | if (!m_sc.target_sp && reg_context_sp) { |
126 | m_sc.target_sp = reg_context_sp->CalculateTarget(); |
127 | if (m_sc.target_sp) |
128 | m_flags.Set(eSymbolContextTarget); |
129 | } |
130 | |
131 | ModuleSP pc_module_sp(pc_addr.GetModule()); |
132 | if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) { |
133 | if (pc_module_sp) { |
134 | m_sc.module_sp = pc_module_sp; |
135 | m_flags.Set(eSymbolContextModule); |
136 | } else { |
137 | m_sc.module_sp.reset(); |
138 | } |
139 | } |
140 | } |
141 | |
142 | StackFrame::~StackFrame() = default; |
143 | |
144 | StackID &StackFrame::GetStackID() { |
145 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
146 | // Make sure we have resolved the StackID object's symbol context scope if we |
147 | // already haven't looked it up. |
148 | |
149 | if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE((uint32_t(eSymbolContextEverything + 1)) << 1))) { |
150 | if (m_id.GetSymbolContextScope()) { |
151 | // We already have a symbol context scope, we just don't have our flag |
152 | // bit set. |
153 | m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE((uint32_t(eSymbolContextEverything + 1)) << 1)); |
154 | } else { |
155 | // Calculate the frame block and use this for the stack ID symbol context |
156 | // scope if we have one. |
157 | SymbolContextScope *scope = GetFrameBlock(); |
158 | if (scope == nullptr) { |
159 | // We don't have a block, so use the symbol |
160 | if (m_flags.IsClear(eSymbolContextSymbol)) |
161 | GetSymbolContext(eSymbolContextSymbol); |
162 | |
163 | // It is ok if m_sc.symbol is nullptr here |
164 | scope = m_sc.symbol; |
165 | } |
166 | // Set the symbol context scope (the accessor will set the |
167 | // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags). |
168 | SetSymbolContextScope(scope); |
169 | } |
170 | } |
171 | return m_id; |
172 | } |
173 | |
174 | uint32_t StackFrame::GetFrameIndex() const { |
175 | ThreadSP thread_sp = GetThread(); |
176 | if (thread_sp) |
177 | return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex( |
178 | m_frame_index); |
179 | else |
180 | return m_frame_index; |
181 | } |
182 | |
183 | void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) { |
184 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
185 | m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE((uint32_t(eSymbolContextEverything + 1)) << 1)); |
186 | m_id.SetSymbolContextScope(symbol_scope); |
187 | } |
188 | |
189 | const Address &StackFrame::GetFrameCodeAddress() { |
190 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
191 | if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR(uint32_t(eSymbolContextEverything + 1))) && |
192 | !m_frame_code_addr.IsSectionOffset()) { |
193 | m_flags.Set(RESOLVED_FRAME_CODE_ADDR(uint32_t(eSymbolContextEverything + 1))); |
194 | |
195 | // Resolve the PC into a temporary address because if ResolveLoadAddress |
196 | // fails to resolve the address, it will clear the address object... |
197 | ThreadSP thread_sp(GetThread()); |
198 | if (thread_sp) { |
199 | TargetSP target_sp(thread_sp->CalculateTarget()); |
200 | if (target_sp) { |
201 | const bool allow_section_end = true; |
202 | if (m_frame_code_addr.SetOpcodeLoadAddress( |
203 | m_frame_code_addr.GetOffset(), target_sp.get(), |
204 | AddressClass::eCode, allow_section_end)) { |
205 | ModuleSP module_sp(m_frame_code_addr.GetModule()); |
206 | if (module_sp) { |
207 | m_sc.module_sp = module_sp; |
208 | m_flags.Set(eSymbolContextModule); |
209 | } |
210 | } |
211 | } |
212 | } |
213 | } |
214 | return m_frame_code_addr; |
215 | } |
216 | |
217 | // This can't be rewritten into a call to |
218 | // RegisterContext::GetPCForSymbolication because this |
219 | // StackFrame may have been constructed with a special pc, |
220 | // e.g. tail-call artificial frames. |
221 | Address StackFrame::GetFrameCodeAddressForSymbolication() { |
222 | Address lookup_addr(GetFrameCodeAddress()); |
223 | if (!lookup_addr.IsValid()) |
224 | return lookup_addr; |
225 | if (m_behaves_like_zeroth_frame) |
226 | return lookup_addr; |
227 | |
228 | addr_t offset = lookup_addr.GetOffset(); |
229 | if (offset > 0) { |
230 | lookup_addr.SetOffset(offset - 1); |
231 | } else { |
232 | // lookup_addr is the start of a section. We need do the math on the |
233 | // actual load address and re-compute the section. We're working with |
234 | // a 'noreturn' function at the end of a section. |
235 | TargetSP target_sp = CalculateTarget(); |
236 | if (target_sp) { |
237 | addr_t addr_minus_one = lookup_addr.GetOpcodeLoadAddress( |
238 | target_sp.get(), AddressClass::eCode) - |
239 | 1; |
240 | lookup_addr.SetOpcodeLoadAddress(addr_minus_one, target_sp.get()); |
241 | } |
242 | } |
243 | return lookup_addr; |
244 | } |
245 | |
246 | bool StackFrame::ChangePC(addr_t pc) { |
247 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
248 | // We can't change the pc value of a history stack frame - it is immutable. |
249 | if (IsHistorical()) |
250 | return false; |
251 | m_frame_code_addr.SetRawAddress(pc); |
252 | m_sc.Clear(false); |
253 | m_flags.Reset(0); |
254 | ThreadSP thread_sp(GetThread()); |
255 | if (thread_sp) |
256 | thread_sp->ClearStackFrames(); |
257 | return true; |
258 | } |
259 | |
260 | const char *StackFrame::Disassemble() { |
261 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
262 | if (!m_disassembly.Empty()) |
263 | return m_disassembly.GetData(); |
264 | |
265 | ExecutionContext exe_ctx(shared_from_this()); |
266 | if (Target *target = exe_ctx.GetTargetPtr()) { |
267 | Disassembler::Disassemble(target->GetDebugger(), target->GetArchitecture(), |
268 | *this, m_disassembly); |
269 | } |
270 | |
271 | return m_disassembly.Empty() ? nullptr : m_disassembly.GetData(); |
272 | } |
273 | |
274 | Block *StackFrame::GetFrameBlock() { |
275 | if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock)) |
276 | GetSymbolContext(eSymbolContextBlock); |
277 | |
278 | if (m_sc.block) { |
279 | Block *inline_block = m_sc.block->GetContainingInlinedBlock(); |
280 | if (inline_block) { |
281 | // Use the block with the inlined function info as the frame block we |
282 | // want this frame to have only the variables for the inlined function |
283 | // and its non-inlined block child blocks. |
284 | return inline_block; |
285 | } else { |
286 | // This block is not contained within any inlined function blocks with so |
287 | // we want to use the top most function block. |
288 | return &m_sc.function->GetBlock(false); |
289 | } |
290 | } |
291 | return nullptr; |
292 | } |
293 | |
294 | // Get the symbol context if we already haven't done so by resolving the |
295 | // PC address as much as possible. This way when we pass around a |
296 | // StackFrame object, everyone will have as much information as possible and no |
297 | // one will ever have to look things up manually. |
298 | const SymbolContext & |
299 | StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) { |
300 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
301 | // Copy our internal symbol context into "sc". |
302 | if ((m_flags.Get() & resolve_scope) != resolve_scope) { |
303 | uint32_t resolved = 0; |
304 | |
305 | // If the target was requested add that: |
306 | if (!m_sc.target_sp) { |
307 | m_sc.target_sp = CalculateTarget(); |
308 | if (m_sc.target_sp) |
309 | resolved |= eSymbolContextTarget; |
310 | } |
311 | |
312 | // Resolve our PC to section offset if we haven't already done so and if we |
313 | // don't have a module. The resolved address section will contain the |
314 | // module to which it belongs |
315 | if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR(uint32_t(eSymbolContextEverything + 1)))) |
316 | GetFrameCodeAddress(); |
317 | |
318 | // If this is not frame zero, then we need to subtract 1 from the PC value |
319 | // when doing address lookups since the PC will be on the instruction |
320 | // following the function call instruction... |
321 | Address lookup_addr(GetFrameCodeAddressForSymbolication()); |
322 | |
323 | if (m_sc.module_sp) { |
324 | // We have something in our stack frame symbol context, lets check if we |
325 | // haven't already tried to lookup one of those things. If we haven't |
326 | // then we will do the query. |
327 | |
328 | SymbolContextItem actual_resolve_scope = SymbolContextItem(0); |
329 | |
330 | if (resolve_scope & eSymbolContextCompUnit) { |
331 | if (m_flags.IsClear(eSymbolContextCompUnit)) { |
332 | if (m_sc.comp_unit) |
333 | resolved |= eSymbolContextCompUnit; |
334 | else |
335 | actual_resolve_scope |= eSymbolContextCompUnit; |
336 | } |
337 | } |
338 | |
339 | if (resolve_scope & eSymbolContextFunction) { |
340 | if (m_flags.IsClear(eSymbolContextFunction)) { |
341 | if (m_sc.function) |
342 | resolved |= eSymbolContextFunction; |
343 | else |
344 | actual_resolve_scope |= eSymbolContextFunction; |
345 | } |
346 | } |
347 | |
348 | if (resolve_scope & eSymbolContextBlock) { |
349 | if (m_flags.IsClear(eSymbolContextBlock)) { |
350 | if (m_sc.block) |
351 | resolved |= eSymbolContextBlock; |
352 | else |
353 | actual_resolve_scope |= eSymbolContextBlock; |
354 | } |
355 | } |
356 | |
357 | if (resolve_scope & eSymbolContextSymbol) { |
358 | if (m_flags.IsClear(eSymbolContextSymbol)) { |
359 | if (m_sc.symbol) |
360 | resolved |= eSymbolContextSymbol; |
361 | else |
362 | actual_resolve_scope |= eSymbolContextSymbol; |
363 | } |
364 | } |
365 | |
366 | if (resolve_scope & eSymbolContextLineEntry) { |
367 | if (m_flags.IsClear(eSymbolContextLineEntry)) { |
368 | if (m_sc.line_entry.IsValid()) |
369 | resolved |= eSymbolContextLineEntry; |
370 | else |
371 | actual_resolve_scope |= eSymbolContextLineEntry; |
372 | } |
373 | } |
374 | |
375 | if (actual_resolve_scope) { |
376 | // We might be resolving less information than what is already in our |
377 | // current symbol context so resolve into a temporary symbol context |
378 | // "sc" so we don't clear out data we have already found in "m_sc" |
379 | SymbolContext sc; |
380 | // Set flags that indicate what we have tried to resolve |
381 | resolved |= m_sc.module_sp->ResolveSymbolContextForAddress( |
382 | lookup_addr, actual_resolve_scope, sc); |
383 | // Only replace what we didn't already have as we may have information |
384 | // for an inlined function scope that won't match what a standard |
385 | // lookup by address would match |
386 | if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr) |
387 | m_sc.comp_unit = sc.comp_unit; |
388 | if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr) |
389 | m_sc.function = sc.function; |
390 | if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr) |
391 | m_sc.block = sc.block; |
392 | if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr) |
393 | m_sc.symbol = sc.symbol; |
394 | if ((resolved & eSymbolContextLineEntry) && |
395 | !m_sc.line_entry.IsValid()) { |
396 | m_sc.line_entry = sc.line_entry; |
397 | m_sc.line_entry.ApplyFileMappings(m_sc.target_sp); |
398 | } |
399 | } |
400 | } else { |
401 | // If we don't have a module, then we can't have the compile unit, |
402 | // function, block, line entry or symbol, so we can safely call |
403 | // ResolveSymbolContextForAddress with our symbol context member m_sc. |
404 | if (m_sc.target_sp) { |
405 | resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress( |
406 | lookup_addr, resolve_scope, m_sc); |
407 | } |
408 | } |
409 | |
410 | // Update our internal flags so we remember what we have tried to locate so |
411 | // we don't have to keep trying when more calls to this function are made. |
412 | // We might have dug up more information that was requested (for example if |
413 | // we were asked to only get the block, we will have gotten the compile |
414 | // unit, and function) so set any additional bits that we resolved |
415 | m_flags.Set(resolve_scope | resolved); |
416 | } |
417 | |
418 | // Return the symbol context with everything that was possible to resolve |
419 | // resolved. |
420 | return m_sc; |
421 | } |
422 | |
423 | VariableList *StackFrame::GetVariableList(bool get_file_globals) { |
424 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
425 | if (m_flags.IsClear(RESOLVED_VARIABLES((((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) << 1))) { |
426 | m_flags.Set(RESOLVED_VARIABLES((((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) << 1)); |
427 | |
428 | Block *frame_block = GetFrameBlock(); |
429 | |
430 | if (frame_block) { |
431 | const bool get_child_variables = true; |
432 | const bool can_create = true; |
433 | const bool stop_if_child_block_is_inlined_function = true; |
434 | m_variable_list_sp = std::make_shared<VariableList>(); |
435 | frame_block->AppendBlockVariables(can_create, get_child_variables, |
436 | stop_if_child_block_is_inlined_function, |
437 | [](Variable *v) { return true; }, |
438 | m_variable_list_sp.get()); |
439 | } |
440 | } |
441 | |
442 | if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES(((((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) << 1) << 1)) && get_file_globals) { |
443 | m_flags.Set(RESOLVED_GLOBAL_VARIABLES(((((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) << 1) << 1)); |
444 | |
445 | if (m_flags.IsClear(eSymbolContextCompUnit)) |
446 | GetSymbolContext(eSymbolContextCompUnit); |
447 | |
448 | if (m_sc.comp_unit) { |
449 | VariableListSP global_variable_list_sp( |
450 | m_sc.comp_unit->GetVariableList(true)); |
451 | if (m_variable_list_sp) |
452 | m_variable_list_sp->AddVariables(global_variable_list_sp.get()); |
453 | else |
454 | m_variable_list_sp = global_variable_list_sp; |
455 | } |
456 | } |
457 | |
458 | return m_variable_list_sp.get(); |
459 | } |
460 | |
461 | VariableListSP |
462 | StackFrame::GetInScopeVariableList(bool get_file_globals, |
463 | bool must_have_valid_location) { |
464 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
465 | // We can't fetch variable information for a history stack frame. |
466 | if (IsHistorical()) |
467 | return VariableListSP(); |
468 | |
469 | VariableListSP var_list_sp(new VariableList); |
470 | GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock); |
471 | |
472 | if (m_sc.block) { |
473 | const bool can_create = true; |
474 | const bool get_parent_variables = true; |
475 | const bool stop_if_block_is_inlined_function = true; |
476 | m_sc.block->AppendVariables( |
477 | can_create, get_parent_variables, stop_if_block_is_inlined_function, |
478 | [this, must_have_valid_location](Variable *v) { |
479 | return v->IsInScope(this) && (!must_have_valid_location || |
480 | v->LocationIsValidForFrame(this)); |
481 | }, |
482 | var_list_sp.get()); |
483 | } |
484 | |
485 | if (m_sc.comp_unit && get_file_globals) { |
486 | VariableListSP global_variable_list_sp( |
487 | m_sc.comp_unit->GetVariableList(true)); |
488 | if (global_variable_list_sp) |
489 | var_list_sp->AddVariables(global_variable_list_sp.get()); |
490 | } |
491 | |
492 | return var_list_sp; |
493 | } |
494 | |
495 | ValueObjectSP StackFrame::GetValueForVariableExpressionPath( |
496 | llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options, |
497 | VariableSP &var_sp, Status &error) { |
498 | llvm::StringRef original_var_expr = var_expr; |
499 | // We can't fetch variable information for a history stack frame. |
500 | if (IsHistorical()) |
501 | return ValueObjectSP(); |
502 | |
503 | if (var_expr.empty()) { |
504 | error.SetErrorStringWithFormat("invalid variable path '%s'", |
505 | var_expr.str().c_str()); |
506 | return ValueObjectSP(); |
507 | } |
508 | |
509 | const bool check_ptr_vs_member = |
510 | (options & eExpressionPathOptionCheckPtrVsMember) != 0; |
511 | const bool no_fragile_ivar = |
512 | (options & eExpressionPathOptionsNoFragileObjcIvar) != 0; |
513 | const bool no_synth_child = |
514 | (options & eExpressionPathOptionsNoSyntheticChildren) != 0; |
515 | // const bool no_synth_array = (options & |
516 | // eExpressionPathOptionsNoSyntheticArrayRange) != 0; |
517 | error.Clear(); |
518 | bool deref = false; |
519 | bool address_of = false; |
520 | ValueObjectSP valobj_sp; |
521 | const bool get_file_globals = true; |
522 | // When looking up a variable for an expression, we need only consider the |
523 | // variables that are in scope. |
524 | VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals)); |
525 | VariableList *variable_list = var_list_sp.get(); |
526 | |
527 | if (!variable_list) |
528 | return ValueObjectSP(); |
529 | |
530 | // If first character is a '*', then show pointer contents |
531 | std::string var_expr_storage; |
532 | if (var_expr[0] == '*') { |
533 | deref = true; |
534 | var_expr = var_expr.drop_front(); // Skip the '*' |
535 | } else if (var_expr[0] == '&') { |
536 | address_of = true; |
537 | var_expr = var_expr.drop_front(); // Skip the '&' |
538 | } |
539 | |
540 | size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}"); |
541 | StreamString var_expr_path_strm; |
542 | |
543 | ConstString name_const_string(var_expr.substr(0, separator_idx)); |
544 | |
545 | var_sp = variable_list->FindVariable(name_const_string, false); |
546 | |
547 | bool synthetically_added_instance_object = false; |
548 | |
549 | if (var_sp) { |
550 | var_expr = var_expr.drop_front(name_const_string.GetLength()); |
551 | } |
552 | |
553 | if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) { |
554 | // Check for direct ivars access which helps us with implicit access to |
555 | // ivars with the "this->" or "self->" |
556 | GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock); |
557 | lldb::LanguageType method_language = eLanguageTypeUnknown; |
558 | bool is_instance_method = false; |
559 | ConstString method_object_name; |
560 | if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method, |
561 | method_object_name)) { |
562 | if (is_instance_method && method_object_name) { |
563 | var_sp = variable_list->FindVariable(method_object_name); |
564 | if (var_sp) { |
565 | separator_idx = 0; |
Value stored to 'separator_idx' is never read | |
566 | var_expr_storage = "->"; |
567 | var_expr_storage += var_expr; |
568 | var_expr = var_expr_storage; |
569 | synthetically_added_instance_object = true; |
570 | } |
571 | } |
572 | } |
573 | } |
574 | |
575 | if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) { |
576 | // Check if any anonymous unions are there which contain a variable with |
577 | // the name we need |
578 | for (const VariableSP &variable_sp : *variable_list) { |
579 | if (!variable_sp) |
580 | continue; |
581 | if (!variable_sp->GetName().IsEmpty()) |
582 | continue; |
583 | |
584 | Type *var_type = variable_sp->GetType(); |
585 | if (!var_type) |
586 | continue; |
587 | |
588 | if (!var_type->GetForwardCompilerType().IsAnonymousType()) |
589 | continue; |
590 | valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic); |
591 | if (!valobj_sp) |
592 | return valobj_sp; |
593 | valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true); |
594 | if (valobj_sp) |
595 | break; |
596 | } |
597 | } |
598 | |
599 | if (var_sp && !valobj_sp) { |
600 | valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic); |
601 | if (!valobj_sp) |
602 | return valobj_sp; |
603 | } |
604 | if (!valobj_sp) { |
605 | error.SetErrorStringWithFormat("no variable named '%s' found in this frame", |
606 | name_const_string.GetCString()); |
607 | return ValueObjectSP(); |
608 | } |
609 | |
610 | // We are dumping at least one child |
611 | while (!var_expr.empty()) { |
612 | // Calculate the next separator index ahead of time |
613 | ValueObjectSP child_valobj_sp; |
614 | const char separator_type = var_expr[0]; |
615 | bool expr_is_ptr = false; |
616 | switch (separator_type) { |
617 | case '-': |
618 | expr_is_ptr = true; |
619 | if (var_expr.size() >= 2 && var_expr[1] != '>') |
620 | return ValueObjectSP(); |
621 | |
622 | if (no_fragile_ivar) { |
623 | // Make sure we aren't trying to deref an objective |
624 | // C ivar if this is not allowed |
625 | const uint32_t pointer_type_flags = |
626 | valobj_sp->GetCompilerType().GetTypeInfo(nullptr); |
627 | if ((pointer_type_flags & eTypeIsObjC) && |
628 | (pointer_type_flags & eTypeIsPointer)) { |
629 | // This was an objective C object pointer and it was requested we |
630 | // skip any fragile ivars so return nothing here |
631 | return ValueObjectSP(); |
632 | } |
633 | } |
634 | |
635 | // If we have a non pointer type with a sythetic value then lets check if |
636 | // we have an sythetic dereference specified. |
637 | if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) { |
638 | Status deref_error; |
639 | if (valobj_sp->GetCompilerType().IsReferenceType()) { |
640 | valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error); |
641 | if (error.Fail()) { |
642 | error.SetErrorStringWithFormatv( |
643 | "Failed to dereference reference type: %s", deref_error); |
644 | return ValueObjectSP(); |
645 | } |
646 | } |
647 | |
648 | valobj_sp = valobj_sp->Dereference(deref_error); |
649 | if (error.Fail()) { |
650 | error.SetErrorStringWithFormatv( |
651 | "Failed to dereference sythetic value: {0}", deref_error); |
652 | return ValueObjectSP(); |
653 | } |
654 | // Some synthetic plug-ins fail to set the error in Dereference |
655 | if (!valobj_sp) { |
656 | error.SetErrorString("Failed to dereference sythetic value"); |
657 | return ValueObjectSP(); |
658 | } |
659 | expr_is_ptr = false; |
660 | } |
661 | |
662 | var_expr = var_expr.drop_front(); // Remove the '-' |
663 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; |
664 | case '.': { |
665 | var_expr = var_expr.drop_front(); // Remove the '.' or '>' |
666 | separator_idx = var_expr.find_first_of(".-["); |
667 | ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-["))); |
668 | |
669 | if (check_ptr_vs_member) { |
670 | // We either have a pointer type and need to verify valobj_sp is a |
671 | // pointer, or we have a member of a class/union/struct being accessed |
672 | // with the . syntax and need to verify we don't have a pointer. |
673 | const bool actual_is_ptr = valobj_sp->IsPointerType(); |
674 | |
675 | if (actual_is_ptr != expr_is_ptr) { |
676 | // Incorrect use of "." with a pointer, or "->" with a |
677 | // class/union/struct instance or reference. |
678 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
679 | if (actual_is_ptr) |
680 | error.SetErrorStringWithFormat( |
681 | "\"%s\" is a pointer and . was used to attempt to access " |
682 | "\"%s\". Did you mean \"%s->%s\"?", |
683 | var_expr_path_strm.GetData(), child_name.GetCString(), |
684 | var_expr_path_strm.GetData(), var_expr.str().c_str()); |
685 | else |
686 | error.SetErrorStringWithFormat( |
687 | "\"%s\" is not a pointer and -> was used to attempt to " |
688 | "access \"%s\". Did you mean \"%s.%s\"?", |
689 | var_expr_path_strm.GetData(), child_name.GetCString(), |
690 | var_expr_path_strm.GetData(), var_expr.str().c_str()); |
691 | return ValueObjectSP(); |
692 | } |
693 | } |
694 | child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true); |
695 | if (!child_valobj_sp) { |
696 | if (!no_synth_child) { |
697 | child_valobj_sp = valobj_sp->GetSyntheticValue(); |
698 | if (child_valobj_sp) |
699 | child_valobj_sp = |
700 | child_valobj_sp->GetChildMemberWithName(child_name, true); |
701 | } |
702 | |
703 | if (no_synth_child || !child_valobj_sp) { |
704 | // No child member with name "child_name" |
705 | if (synthetically_added_instance_object) { |
706 | // We added a "this->" or "self->" to the beginning of the |
707 | // expression and this is the first pointer ivar access, so just |
708 | // return the normal error |
709 | error.SetErrorStringWithFormat( |
710 | "no variable or instance variable named '%s' found in " |
711 | "this frame", |
712 | name_const_string.GetCString()); |
713 | } else { |
714 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
715 | if (child_name) { |
716 | error.SetErrorStringWithFormat( |
717 | "\"%s\" is not a member of \"(%s) %s\"", |
718 | child_name.GetCString(), |
719 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
720 | var_expr_path_strm.GetData()); |
721 | } else { |
722 | error.SetErrorStringWithFormat( |
723 | "incomplete expression path after \"%s\" in \"%s\"", |
724 | var_expr_path_strm.GetData(), |
725 | original_var_expr.str().c_str()); |
726 | } |
727 | } |
728 | return ValueObjectSP(); |
729 | } |
730 | } |
731 | synthetically_added_instance_object = false; |
732 | // Remove the child name from the path |
733 | var_expr = var_expr.drop_front(child_name.GetLength()); |
734 | if (use_dynamic != eNoDynamicValues) { |
735 | ValueObjectSP dynamic_value_sp( |
736 | child_valobj_sp->GetDynamicValue(use_dynamic)); |
737 | if (dynamic_value_sp) |
738 | child_valobj_sp = dynamic_value_sp; |
739 | } |
740 | } break; |
741 | |
742 | case '[': { |
743 | // Array member access, or treating pointer as an array Need at least two |
744 | // brackets and a number |
745 | if (var_expr.size() <= 2) { |
746 | error.SetErrorStringWithFormat( |
747 | "invalid square bracket encountered after \"%s\" in \"%s\"", |
748 | var_expr_path_strm.GetData(), var_expr.str().c_str()); |
749 | return ValueObjectSP(); |
750 | } |
751 | |
752 | // Drop the open brace. |
753 | var_expr = var_expr.drop_front(); |
754 | long child_index = 0; |
755 | |
756 | // If there's no closing brace, this is an invalid expression. |
757 | size_t end_pos = var_expr.find_first_of(']'); |
758 | if (end_pos == llvm::StringRef::npos) { |
759 | error.SetErrorStringWithFormat( |
760 | "missing closing square bracket in expression \"%s\"", |
761 | var_expr_path_strm.GetData()); |
762 | return ValueObjectSP(); |
763 | } |
764 | llvm::StringRef index_expr = var_expr.take_front(end_pos); |
765 | llvm::StringRef original_index_expr = index_expr; |
766 | // Drop all of "[index_expr]" |
767 | var_expr = var_expr.drop_front(end_pos + 1); |
768 | |
769 | if (index_expr.consumeInteger(0, child_index)) { |
770 | // If there was no integer anywhere in the index expression, this is |
771 | // erroneous expression. |
772 | error.SetErrorStringWithFormat("invalid index expression \"%s\"", |
773 | index_expr.str().c_str()); |
774 | return ValueObjectSP(); |
775 | } |
776 | |
777 | if (index_expr.empty()) { |
778 | // The entire index expression was a single integer. |
779 | |
780 | if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) { |
781 | // what we have is *ptr[low]. the most similar C++ syntax is to deref |
782 | // ptr and extract bit low out of it. reading array item low would be |
783 | // done by saying ptr[low], without a deref * sign |
784 | Status error; |
785 | ValueObjectSP temp(valobj_sp->Dereference(error)); |
786 | if (error.Fail()) { |
787 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
788 | error.SetErrorStringWithFormat( |
789 | "could not dereference \"(%s) %s\"", |
790 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
791 | var_expr_path_strm.GetData()); |
792 | return ValueObjectSP(); |
793 | } |
794 | valobj_sp = temp; |
795 | deref = false; |
796 | } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && |
797 | deref) { |
798 | // what we have is *arr[low]. the most similar C++ syntax is to get |
799 | // arr[0] (an operation that is equivalent to deref-ing arr) and |
800 | // extract bit low out of it. reading array item low would be done by |
801 | // saying arr[low], without a deref * sign |
802 | Status error; |
803 | ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true)); |
804 | if (error.Fail()) { |
805 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
806 | error.SetErrorStringWithFormat( |
807 | "could not get item 0 for \"(%s) %s\"", |
808 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
809 | var_expr_path_strm.GetData()); |
810 | return ValueObjectSP(); |
811 | } |
812 | valobj_sp = temp; |
813 | deref = false; |
814 | } |
815 | |
816 | bool is_incomplete_array = false; |
817 | if (valobj_sp->IsPointerType()) { |
818 | bool is_objc_pointer = true; |
819 | |
820 | if (valobj_sp->GetCompilerType().GetMinimumLanguage() != |
821 | eLanguageTypeObjC) |
822 | is_objc_pointer = false; |
823 | else if (!valobj_sp->GetCompilerType().IsPointerType()) |
824 | is_objc_pointer = false; |
825 | |
826 | if (no_synth_child && is_objc_pointer) { |
827 | error.SetErrorStringWithFormat( |
828 | "\"(%s) %s\" is an Objective-C pointer, and cannot be " |
829 | "subscripted", |
830 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
831 | var_expr_path_strm.GetData()); |
832 | |
833 | return ValueObjectSP(); |
834 | } else if (is_objc_pointer) { |
835 | // dereferencing ObjC variables is not valid.. so let's try and |
836 | // recur to synthetic children |
837 | ValueObjectSP synthetic = valobj_sp->GetSyntheticValue(); |
838 | if (!synthetic /* no synthetic */ |
839 | || synthetic == valobj_sp) /* synthetic is the same as |
840 | the original object */ |
841 | { |
842 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
843 | error.SetErrorStringWithFormat( |
844 | "\"(%s) %s\" is not an array type", |
845 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
846 | var_expr_path_strm.GetData()); |
847 | } else if ( |
848 | static_cast<uint32_t>(child_index) >= |
849 | synthetic |
850 | ->GetNumChildren() /* synthetic does not have that many values */) { |
851 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
852 | error.SetErrorStringWithFormat( |
853 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
854 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
855 | var_expr_path_strm.GetData()); |
856 | } else { |
857 | child_valobj_sp = synthetic->GetChildAtIndex(child_index, true); |
858 | if (!child_valobj_sp) { |
859 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
860 | error.SetErrorStringWithFormat( |
861 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
862 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
863 | var_expr_path_strm.GetData()); |
864 | } |
865 | } |
866 | } else { |
867 | child_valobj_sp = |
868 | valobj_sp->GetSyntheticArrayMember(child_index, true); |
869 | if (!child_valobj_sp) { |
870 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
871 | error.SetErrorStringWithFormat( |
872 | "failed to use pointer as array for index %ld for " |
873 | "\"(%s) %s\"", |
874 | child_index, |
875 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
876 | var_expr_path_strm.GetData()); |
877 | } |
878 | } |
879 | } else if (valobj_sp->GetCompilerType().IsArrayType( |
880 | nullptr, nullptr, &is_incomplete_array)) { |
881 | // Pass false to dynamic_value here so we can tell the difference |
882 | // between no dynamic value and no member of this type... |
883 | child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true); |
884 | if (!child_valobj_sp && (is_incomplete_array || !no_synth_child)) |
885 | child_valobj_sp = |
886 | valobj_sp->GetSyntheticArrayMember(child_index, true); |
887 | |
888 | if (!child_valobj_sp) { |
889 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
890 | error.SetErrorStringWithFormat( |
891 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
892 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
893 | var_expr_path_strm.GetData()); |
894 | } |
895 | } else if (valobj_sp->GetCompilerType().IsScalarType()) { |
896 | // this is a bitfield asking to display just one bit |
897 | child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild( |
898 | child_index, child_index, true); |
899 | if (!child_valobj_sp) { |
900 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
901 | error.SetErrorStringWithFormat( |
902 | "bitfield range %ld-%ld is not valid for \"(%s) %s\"", |
903 | child_index, child_index, |
904 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
905 | var_expr_path_strm.GetData()); |
906 | } |
907 | } else { |
908 | ValueObjectSP synthetic = valobj_sp->GetSyntheticValue(); |
909 | if (no_synth_child /* synthetic is forbidden */ || |
910 | !synthetic /* no synthetic */ |
911 | || synthetic == valobj_sp) /* synthetic is the same as the |
912 | original object */ |
913 | { |
914 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
915 | error.SetErrorStringWithFormat( |
916 | "\"(%s) %s\" is not an array type", |
917 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
918 | var_expr_path_strm.GetData()); |
919 | } else if ( |
920 | static_cast<uint32_t>(child_index) >= |
921 | synthetic |
922 | ->GetNumChildren() /* synthetic does not have that many values */) { |
923 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
924 | error.SetErrorStringWithFormat( |
925 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
926 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
927 | var_expr_path_strm.GetData()); |
928 | } else { |
929 | child_valobj_sp = synthetic->GetChildAtIndex(child_index, true); |
930 | if (!child_valobj_sp) { |
931 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
932 | error.SetErrorStringWithFormat( |
933 | "array index %ld is not valid for \"(%s) %s\"", child_index, |
934 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
935 | var_expr_path_strm.GetData()); |
936 | } |
937 | } |
938 | } |
939 | |
940 | if (!child_valobj_sp) { |
941 | // Invalid array index... |
942 | return ValueObjectSP(); |
943 | } |
944 | |
945 | if (use_dynamic != eNoDynamicValues) { |
946 | ValueObjectSP dynamic_value_sp( |
947 | child_valobj_sp->GetDynamicValue(use_dynamic)); |
948 | if (dynamic_value_sp) |
949 | child_valobj_sp = dynamic_value_sp; |
950 | } |
951 | // Break out early from the switch since we were able to find the child |
952 | // member |
953 | break; |
954 | } |
955 | |
956 | // this is most probably a BitField, let's take a look |
957 | if (index_expr.front() != '-') { |
958 | error.SetErrorStringWithFormat("invalid range expression \"'%s'\"", |
959 | original_index_expr.str().c_str()); |
960 | return ValueObjectSP(); |
961 | } |
962 | |
963 | index_expr = index_expr.drop_front(); |
964 | long final_index = 0; |
965 | if (index_expr.getAsInteger(0, final_index)) { |
966 | error.SetErrorStringWithFormat("invalid range expression \"'%s'\"", |
967 | original_index_expr.str().c_str()); |
968 | return ValueObjectSP(); |
969 | } |
970 | |
971 | // if the format given is [high-low], swap range |
972 | if (child_index > final_index) { |
973 | long temp = child_index; |
974 | child_index = final_index; |
975 | final_index = temp; |
976 | } |
977 | |
978 | if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) { |
979 | // what we have is *ptr[low-high]. the most similar C++ syntax is to |
980 | // deref ptr and extract bits low thru high out of it. reading array |
981 | // items low thru high would be done by saying ptr[low-high], without a |
982 | // deref * sign |
983 | Status error; |
984 | ValueObjectSP temp(valobj_sp->Dereference(error)); |
985 | if (error.Fail()) { |
986 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
987 | error.SetErrorStringWithFormat( |
988 | "could not dereference \"(%s) %s\"", |
989 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
990 | var_expr_path_strm.GetData()); |
991 | return ValueObjectSP(); |
992 | } |
993 | valobj_sp = temp; |
994 | deref = false; |
995 | } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) { |
996 | // what we have is *arr[low-high]. the most similar C++ syntax is to |
997 | // get arr[0] (an operation that is equivalent to deref-ing arr) and |
998 | // extract bits low thru high out of it. reading array items low thru |
999 | // high would be done by saying arr[low-high], without a deref * sign |
1000 | Status error; |
1001 | ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true)); |
1002 | if (error.Fail()) { |
1003 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
1004 | error.SetErrorStringWithFormat( |
1005 | "could not get item 0 for \"(%s) %s\"", |
1006 | valobj_sp->GetTypeName().AsCString("<invalid type>"), |
1007 | var_expr_path_strm.GetData()); |
1008 | return ValueObjectSP(); |
1009 | } |
1010 | valobj_sp = temp; |
1011 | deref = false; |
1012 | } |
1013 | |
1014 | child_valobj_sp = |
1015 | valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true); |
1016 | if (!child_valobj_sp) { |
1017 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
1018 | error.SetErrorStringWithFormat( |
1019 | "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index, |
1020 | final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"), |
1021 | var_expr_path_strm.GetData()); |
1022 | } |
1023 | |
1024 | if (!child_valobj_sp) { |
1025 | // Invalid bitfield range... |
1026 | return ValueObjectSP(); |
1027 | } |
1028 | |
1029 | if (use_dynamic != eNoDynamicValues) { |
1030 | ValueObjectSP dynamic_value_sp( |
1031 | child_valobj_sp->GetDynamicValue(use_dynamic)); |
1032 | if (dynamic_value_sp) |
1033 | child_valobj_sp = dynamic_value_sp; |
1034 | } |
1035 | // Break out early from the switch since we were able to find the child |
1036 | // member |
1037 | break; |
1038 | } |
1039 | default: |
1040 | // Failure... |
1041 | { |
1042 | valobj_sp->GetExpressionPath(var_expr_path_strm); |
1043 | error.SetErrorStringWithFormat( |
1044 | "unexpected char '%c' encountered after \"%s\" in \"%s\"", |
1045 | separator_type, var_expr_path_strm.GetData(), |
1046 | var_expr.str().c_str()); |
1047 | |
1048 | return ValueObjectSP(); |
1049 | } |
1050 | } |
1051 | |
1052 | if (child_valobj_sp) |
1053 | valobj_sp = child_valobj_sp; |
1054 | } |
1055 | if (valobj_sp) { |
1056 | if (deref) { |
1057 | ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error)); |
1058 | valobj_sp = deref_valobj_sp; |
1059 | } else if (address_of) { |
1060 | ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error)); |
1061 | valobj_sp = address_of_valobj_sp; |
1062 | } |
1063 | } |
1064 | return valobj_sp; |
1065 | } |
1066 | |
1067 | bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) { |
1068 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1069 | if (!m_cfa_is_valid) { |
1070 | m_frame_base_error.SetErrorString( |
1071 | "No frame base available for this historical stack frame."); |
1072 | return false; |
1073 | } |
1074 | |
1075 | if (m_flags.IsClear(GOT_FRAME_BASE(((uint32_t(eSymbolContextEverything + 1)) << 1) << 1))) { |
1076 | if (m_sc.function) { |
1077 | m_frame_base.Clear(); |
1078 | m_frame_base_error.Clear(); |
1079 | |
1080 | m_flags.Set(GOT_FRAME_BASE(((uint32_t(eSymbolContextEverything + 1)) << 1) << 1)); |
1081 | ExecutionContext exe_ctx(shared_from_this()); |
1082 | Value expr_value; |
1083 | addr_t loclist_base_addr = LLDB_INVALID_ADDRESS(18446744073709551615UL); |
1084 | if (m_sc.function->GetFrameBaseExpression().IsLocationList()) |
1085 | loclist_base_addr = |
1086 | m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress( |
1087 | exe_ctx.GetTargetPtr()); |
1088 | |
1089 | if (!m_sc.function->GetFrameBaseExpression().Evaluate( |
1090 | &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr, |
1091 | expr_value, &m_frame_base_error)) { |
1092 | // We should really have an error if evaluate returns, but in case we |
1093 | // don't, lets set the error to something at least. |
1094 | if (m_frame_base_error.Success()) |
1095 | m_frame_base_error.SetErrorString( |
1096 | "Evaluation of the frame base expression failed."); |
1097 | } else { |
1098 | m_frame_base = expr_value.ResolveValue(&exe_ctx); |
1099 | } |
1100 | } else { |
1101 | m_frame_base_error.SetErrorString("No function in symbol context."); |
1102 | } |
1103 | } |
1104 | |
1105 | if (m_frame_base_error.Success()) |
1106 | frame_base = m_frame_base; |
1107 | |
1108 | if (error_ptr) |
1109 | *error_ptr = m_frame_base_error; |
1110 | return m_frame_base_error.Success(); |
1111 | } |
1112 | |
1113 | DWARFExpression *StackFrame::GetFrameBaseExpression(Status *error_ptr) { |
1114 | if (!m_sc.function) { |
1115 | if (error_ptr) { |
1116 | error_ptr->SetErrorString("No function in symbol context."); |
1117 | } |
1118 | return nullptr; |
1119 | } |
1120 | |
1121 | return &m_sc.function->GetFrameBaseExpression(); |
1122 | } |
1123 | |
1124 | RegisterContextSP StackFrame::GetRegisterContext() { |
1125 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1126 | if (!m_reg_context_sp) { |
1127 | ThreadSP thread_sp(GetThread()); |
1128 | if (thread_sp) |
1129 | m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this); |
1130 | } |
1131 | return m_reg_context_sp; |
1132 | } |
1133 | |
1134 | bool StackFrame::HasDebugInformation() { |
1135 | GetSymbolContext(eSymbolContextLineEntry); |
1136 | return m_sc.line_entry.IsValid(); |
1137 | } |
1138 | |
1139 | ValueObjectSP |
1140 | StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp, |
1141 | DynamicValueType use_dynamic) { |
1142 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1143 | ValueObjectSP valobj_sp; |
1144 | if (IsHistorical()) { |
1145 | return valobj_sp; |
1146 | } |
1147 | VariableList *var_list = GetVariableList(true); |
1148 | if (var_list) { |
1149 | // Make sure the variable is a frame variable |
1150 | const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get()); |
1151 | const uint32_t num_variables = var_list->GetSize(); |
1152 | if (var_idx < num_variables) { |
1153 | valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx); |
1154 | if (!valobj_sp) { |
1155 | if (m_variable_list_value_objects.GetSize() < num_variables) |
1156 | m_variable_list_value_objects.Resize(num_variables); |
1157 | valobj_sp = ValueObjectVariable::Create(this, variable_sp); |
1158 | m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp); |
1159 | } |
1160 | } |
1161 | } |
1162 | if (use_dynamic != eNoDynamicValues && valobj_sp) { |
1163 | ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic); |
1164 | if (dynamic_sp) |
1165 | return dynamic_sp; |
1166 | } |
1167 | return valobj_sp; |
1168 | } |
1169 | |
1170 | bool StackFrame::IsInlined() { |
1171 | if (m_sc.block == nullptr) |
1172 | GetSymbolContext(eSymbolContextBlock); |
1173 | if (m_sc.block) |
1174 | return m_sc.block->GetContainingInlinedBlock() != nullptr; |
1175 | return false; |
1176 | } |
1177 | |
1178 | bool StackFrame::IsHistorical() const { |
1179 | return m_stack_frame_kind == StackFrame::Kind::History; |
1180 | } |
1181 | |
1182 | bool StackFrame::IsArtificial() const { |
1183 | return m_stack_frame_kind == StackFrame::Kind::Artificial; |
1184 | } |
1185 | |
1186 | lldb::LanguageType StackFrame::GetLanguage() { |
1187 | CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit; |
1188 | if (cu) |
1189 | return cu->GetLanguage(); |
1190 | return lldb::eLanguageTypeUnknown; |
1191 | } |
1192 | |
1193 | lldb::LanguageType StackFrame::GuessLanguage() { |
1194 | LanguageType lang_type = GetLanguage(); |
1195 | |
1196 | if (lang_type == eLanguageTypeUnknown) { |
1197 | SymbolContext sc = GetSymbolContext(eSymbolContextFunction |
1198 | | eSymbolContextSymbol); |
1199 | if (sc.function) { |
1200 | lang_type = sc.function->GetMangled().GuessLanguage(); |
1201 | } |
1202 | else if (sc.symbol) |
1203 | { |
1204 | lang_type = sc.symbol->GetMangled().GuessLanguage(); |
1205 | } |
1206 | } |
1207 | |
1208 | return lang_type; |
1209 | } |
1210 | |
1211 | namespace { |
1212 | std::pair<const Instruction::Operand *, int64_t> |
1213 | GetBaseExplainingValue(const Instruction::Operand &operand, |
1214 | RegisterContext ®ister_context, lldb::addr_t value) { |
1215 | switch (operand.m_type) { |
1216 | case Instruction::Operand::Type::Dereference: |
1217 | case Instruction::Operand::Type::Immediate: |
1218 | case Instruction::Operand::Type::Invalid: |
1219 | case Instruction::Operand::Type::Product: |
1220 | // These are not currently interesting |
1221 | return std::make_pair(nullptr, 0); |
1222 | case Instruction::Operand::Type::Sum: { |
1223 | const Instruction::Operand *immediate_child = nullptr; |
1224 | const Instruction::Operand *variable_child = nullptr; |
1225 | if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) { |
1226 | immediate_child = &operand.m_children[0]; |
1227 | variable_child = &operand.m_children[1]; |
1228 | } else if (operand.m_children[1].m_type == |
1229 | Instruction::Operand::Type::Immediate) { |
1230 | immediate_child = &operand.m_children[1]; |
1231 | variable_child = &operand.m_children[0]; |
1232 | } |
1233 | if (!immediate_child) { |
1234 | return std::make_pair(nullptr, 0); |
1235 | } |
1236 | lldb::addr_t adjusted_value = value; |
1237 | if (immediate_child->m_negative) { |
1238 | adjusted_value += immediate_child->m_immediate; |
1239 | } else { |
1240 | adjusted_value -= immediate_child->m_immediate; |
1241 | } |
1242 | std::pair<const Instruction::Operand *, int64_t> base_and_offset = |
1243 | GetBaseExplainingValue(*variable_child, register_context, |
1244 | adjusted_value); |
1245 | if (!base_and_offset.first) { |
1246 | return std::make_pair(nullptr, 0); |
1247 | } |
1248 | if (immediate_child->m_negative) { |
1249 | base_and_offset.second -= immediate_child->m_immediate; |
1250 | } else { |
1251 | base_and_offset.second += immediate_child->m_immediate; |
1252 | } |
1253 | return base_and_offset; |
1254 | } |
1255 | case Instruction::Operand::Type::Register: { |
1256 | const RegisterInfo *info = |
1257 | register_context.GetRegisterInfoByName(operand.m_register.AsCString()); |
1258 | if (!info) { |
1259 | return std::make_pair(nullptr, 0); |
1260 | } |
1261 | RegisterValue reg_value; |
1262 | if (!register_context.ReadRegister(info, reg_value)) { |
1263 | return std::make_pair(nullptr, 0); |
1264 | } |
1265 | if (reg_value.GetAsUInt64() == value) { |
1266 | return std::make_pair(&operand, 0); |
1267 | } else { |
1268 | return std::make_pair(nullptr, 0); |
1269 | } |
1270 | } |
1271 | } |
1272 | return std::make_pair(nullptr, 0); |
1273 | } |
1274 | |
1275 | std::pair<const Instruction::Operand *, int64_t> |
1276 | GetBaseExplainingDereference(const Instruction::Operand &operand, |
1277 | RegisterContext ®ister_context, |
1278 | lldb::addr_t addr) { |
1279 | if (operand.m_type == Instruction::Operand::Type::Dereference) { |
1280 | return GetBaseExplainingValue(operand.m_children[0], register_context, |
1281 | addr); |
1282 | } |
1283 | return std::make_pair(nullptr, 0); |
1284 | } |
1285 | } |
1286 | |
1287 | lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) { |
1288 | TargetSP target_sp = CalculateTarget(); |
1289 | |
1290 | const ArchSpec &target_arch = target_sp->GetArchitecture(); |
1291 | |
1292 | AddressRange pc_range; |
1293 | pc_range.GetBaseAddress() = GetFrameCodeAddress(); |
1294 | pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize()); |
1295 | |
1296 | const char *plugin_name = nullptr; |
1297 | const char *flavor = nullptr; |
1298 | const bool force_live_memory = true; |
1299 | |
1300 | DisassemblerSP disassembler_sp = |
1301 | Disassembler::DisassembleRange(target_arch, plugin_name, flavor, |
1302 | *target_sp, pc_range, force_live_memory); |
1303 | |
1304 | if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) { |
1305 | return ValueObjectSP(); |
1306 | } |
1307 | |
1308 | InstructionSP instruction_sp = |
1309 | disassembler_sp->GetInstructionList().GetInstructionAtIndex(0); |
1310 | |
1311 | llvm::SmallVector<Instruction::Operand, 3> operands; |
1312 | |
1313 | if (!instruction_sp->ParseOperands(operands)) { |
1314 | return ValueObjectSP(); |
1315 | } |
1316 | |
1317 | RegisterContextSP register_context_sp = GetRegisterContext(); |
1318 | |
1319 | if (!register_context_sp) { |
1320 | return ValueObjectSP(); |
1321 | } |
1322 | |
1323 | for (const Instruction::Operand &operand : operands) { |
1324 | std::pair<const Instruction::Operand *, int64_t> base_and_offset = |
1325 | GetBaseExplainingDereference(operand, *register_context_sp, addr); |
1326 | |
1327 | if (!base_and_offset.first) { |
1328 | continue; |
1329 | } |
1330 | |
1331 | switch (base_and_offset.first->m_type) { |
1332 | case Instruction::Operand::Type::Immediate: { |
1333 | lldb_private::Address addr; |
1334 | if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate + |
1335 | base_and_offset.second, |
1336 | addr)) { |
1337 | auto c_type_system_or_err = |
1338 | target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC); |
1339 | if (auto err = c_type_system_or_err.takeError()) { |
1340 | LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), std::move(err),do { ::lldb_private::Log *log_private = (GetLog(LLDBLog::Thread )); ::llvm::Error error_private = (std::move(err)); if (log_private && error_private) { log_private->FormatError(::std ::move(error_private), "lldb/source/Target/StackFrame.cpp", __func__ , "Unable to guess value for given address"); } else ::llvm:: consumeError(::std::move(error_private)); } while (0) |
1341 | "Unable to guess value for given address")do { ::lldb_private::Log *log_private = (GetLog(LLDBLog::Thread )); ::llvm::Error error_private = (std::move(err)); if (log_private && error_private) { log_private->FormatError(::std ::move(error_private), "lldb/source/Target/StackFrame.cpp", __func__ , "Unable to guess value for given address"); } else ::llvm:: consumeError(::std::move(error_private)); } while (0); |
1342 | return ValueObjectSP(); |
1343 | } else { |
1344 | CompilerType void_ptr_type = |
1345 | c_type_system_or_err |
1346 | ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar) |
1347 | .GetPointerType(); |
1348 | return ValueObjectMemory::Create(this, "", addr, void_ptr_type); |
1349 | } |
1350 | } else { |
1351 | return ValueObjectSP(); |
1352 | } |
1353 | break; |
1354 | } |
1355 | case Instruction::Operand::Type::Register: { |
1356 | return GuessValueForRegisterAndOffset(base_and_offset.first->m_register, |
1357 | base_and_offset.second); |
1358 | } |
1359 | default: |
1360 | return ValueObjectSP(); |
1361 | } |
1362 | } |
1363 | |
1364 | return ValueObjectSP(); |
1365 | } |
1366 | |
1367 | namespace { |
1368 | ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent, |
1369 | int64_t offset) { |
1370 | if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) { |
1371 | return ValueObjectSP(); |
1372 | } |
1373 | |
1374 | if (parent->IsPointerOrReferenceType()) { |
1375 | return parent; |
1376 | } |
1377 | |
1378 | for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) { |
1379 | const bool can_create = true; |
1380 | ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create); |
1381 | |
1382 | if (!child_sp) { |
1383 | return ValueObjectSP(); |
1384 | } |
1385 | |
1386 | int64_t child_offset = child_sp->GetByteOffset(); |
1387 | int64_t child_size = child_sp->GetByteSize().getValueOr(0); |
1388 | |
1389 | if (offset >= child_offset && offset < (child_offset + child_size)) { |
1390 | return GetValueForOffset(frame, child_sp, offset - child_offset); |
1391 | } |
1392 | } |
1393 | |
1394 | if (offset == 0) { |
1395 | return parent; |
1396 | } else { |
1397 | return ValueObjectSP(); |
1398 | } |
1399 | } |
1400 | |
1401 | ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame, |
1402 | ValueObjectSP &base, |
1403 | int64_t offset) { |
1404 | // base is a pointer to something |
1405 | // offset is the thing to add to the pointer We return the most sensible |
1406 | // ValueObject for the result of *(base+offset) |
1407 | |
1408 | if (!base->IsPointerOrReferenceType()) { |
1409 | return ValueObjectSP(); |
1410 | } |
1411 | |
1412 | Status error; |
1413 | ValueObjectSP pointee = base->Dereference(error); |
1414 | |
1415 | if (!pointee) { |
1416 | return ValueObjectSP(); |
1417 | } |
1418 | |
1419 | if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) { |
1420 | int64_t index = offset / pointee->GetByteSize().getValueOr(1); |
1421 | offset = offset % pointee->GetByteSize().getValueOr(1); |
1422 | const bool can_create = true; |
1423 | pointee = base->GetSyntheticArrayMember(index, can_create); |
1424 | } |
1425 | |
1426 | if (!pointee || error.Fail()) { |
1427 | return ValueObjectSP(); |
1428 | } |
1429 | |
1430 | return GetValueForOffset(frame, pointee, offset); |
1431 | } |
1432 | |
1433 | /// Attempt to reconstruct the ValueObject for the address contained in a |
1434 | /// given register plus an offset. |
1435 | /// |
1436 | /// \param [in] frame |
1437 | /// The current stack frame. |
1438 | /// |
1439 | /// \param [in] reg |
1440 | /// The register. |
1441 | /// |
1442 | /// \param [in] offset |
1443 | /// The offset from the register. |
1444 | /// |
1445 | /// \param [in] disassembler |
1446 | /// A disassembler containing instructions valid up to the current PC. |
1447 | /// |
1448 | /// \param [in] variables |
1449 | /// The variable list from the current frame, |
1450 | /// |
1451 | /// \param [in] pc |
1452 | /// The program counter for the instruction considered the 'user'. |
1453 | /// |
1454 | /// \return |
1455 | /// A string describing the base for the ExpressionPath. This could be a |
1456 | /// variable, a register value, an argument, or a function return value. |
1457 | /// The ValueObject if found. If valid, it has a valid ExpressionPath. |
1458 | lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg, |
1459 | int64_t offset, Disassembler &disassembler, |
1460 | VariableList &variables, const Address &pc) { |
1461 | // Example of operation for Intel: |
1462 | // |
1463 | // +14: movq -0x8(%rbp), %rdi |
1464 | // +18: movq 0x8(%rdi), %rdi |
1465 | // +22: addl 0x4(%rdi), %eax |
1466 | // |
1467 | // f, a pointer to a struct, is known to be at -0x8(%rbp). |
1468 | // |
1469 | // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at |
1470 | // +18 that assigns to rdi, and calls itself recursively for that dereference |
1471 | // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at |
1472 | // +14 that assigns to rdi, and calls itself recursively for that |
1473 | // dereference |
1474 | // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the |
1475 | // variable list. |
1476 | // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14) |
1477 | // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8 |
1478 | // at +18) |
1479 | // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at |
1480 | // rdi+4 at +22) |
1481 | |
1482 | // First, check the variable list to see if anything is at the specified |
1483 | // location. |
1484 | |
1485 | using namespace OperandMatchers; |
1486 | |
1487 | const RegisterInfo *reg_info = |
1488 | frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString()); |
1489 | if (!reg_info) { |
1490 | return ValueObjectSP(); |
1491 | } |
1492 | |
1493 | Instruction::Operand op = |
1494 | offset ? Instruction::Operand::BuildDereference( |
1495 | Instruction::Operand::BuildSum( |
1496 | Instruction::Operand::BuildRegister(reg), |
1497 | Instruction::Operand::BuildImmediate(offset))) |
1498 | : Instruction::Operand::BuildDereference( |
1499 | Instruction::Operand::BuildRegister(reg)); |
1500 | |
1501 | for (VariableSP var_sp : variables) { |
1502 | if (var_sp->LocationExpression().MatchesOperand(frame, op)) |
1503 | return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues); |
1504 | } |
1505 | |
1506 | const uint32_t current_inst = |
1507 | disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc); |
1508 | if (current_inst == UINT32_MAX(4294967295U)) { |
1509 | return ValueObjectSP(); |
1510 | } |
1511 | |
1512 | for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) { |
1513 | // This is not an exact algorithm, and it sacrifices accuracy for |
1514 | // generality. Recognizing "mov" and "ld" instructions –– and which |
1515 | // are their source and destination operands -- is something the |
1516 | // disassembler should do for us. |
1517 | InstructionSP instruction_sp = |
1518 | disassembler.GetInstructionList().GetInstructionAtIndex(ii); |
1519 | |
1520 | if (instruction_sp->IsCall()) { |
1521 | ABISP abi_sp = frame.CalculateProcess()->GetABI(); |
1522 | if (!abi_sp) { |
1523 | continue; |
1524 | } |
1525 | |
1526 | const char *return_register_name; |
1527 | if (!abi_sp->GetPointerReturnRegister(return_register_name)) { |
1528 | continue; |
1529 | } |
1530 | |
1531 | const RegisterInfo *return_register_info = |
1532 | frame.GetRegisterContext()->GetRegisterInfoByName( |
1533 | return_register_name); |
1534 | if (!return_register_info) { |
1535 | continue; |
1536 | } |
1537 | |
1538 | int64_t offset = 0; |
1539 | |
1540 | if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference), |
1541 | MatchRegOp(*return_register_info))(op) && |
1542 | !MatchUnaryOp( |
1543 | MatchOpType(Instruction::Operand::Type::Dereference), |
1544 | MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), |
1545 | MatchRegOp(*return_register_info), |
1546 | FetchImmOp(offset)))(op)) { |
1547 | continue; |
1548 | } |
1549 | |
1550 | llvm::SmallVector<Instruction::Operand, 1> operands; |
1551 | if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) { |
1552 | continue; |
1553 | } |
1554 | |
1555 | switch (operands[0].m_type) { |
1556 | default: |
1557 | break; |
1558 | case Instruction::Operand::Type::Immediate: { |
1559 | SymbolContext sc; |
1560 | Address load_address; |
1561 | if (!frame.CalculateTarget()->ResolveLoadAddress( |
1562 | operands[0].m_immediate, load_address)) { |
1563 | break; |
1564 | } |
1565 | frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress( |
1566 | load_address, eSymbolContextFunction, sc); |
1567 | if (!sc.function) { |
1568 | break; |
1569 | } |
1570 | CompilerType function_type = sc.function->GetCompilerType(); |
1571 | if (!function_type.IsFunctionType()) { |
1572 | break; |
1573 | } |
1574 | CompilerType return_type = function_type.GetFunctionReturnType(); |
1575 | RegisterValue return_value; |
1576 | if (!frame.GetRegisterContext()->ReadRegister(return_register_info, |
1577 | return_value)) { |
1578 | break; |
1579 | } |
1580 | std::string name_str( |
1581 | sc.function->GetName().AsCString("<unknown function>")); |
1582 | name_str.append("()"); |
1583 | Address return_value_address(return_value.GetAsUInt64()); |
1584 | ValueObjectSP return_value_sp = ValueObjectMemory::Create( |
1585 | &frame, name_str, return_value_address, return_type); |
1586 | return GetValueForDereferincingOffset(frame, return_value_sp, offset); |
1587 | } |
1588 | } |
1589 | |
1590 | continue; |
1591 | } |
1592 | |
1593 | llvm::SmallVector<Instruction::Operand, 2> operands; |
1594 | if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) { |
1595 | continue; |
1596 | } |
1597 | |
1598 | Instruction::Operand *origin_operand = nullptr; |
1599 | auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) { |
1600 | return MatchRegOp(*reg_info)(op) && op.m_clobbered; |
1601 | }; |
1602 | |
1603 | if (clobbered_reg_matcher(operands[0])) { |
1604 | origin_operand = &operands[1]; |
1605 | } |
1606 | else if (clobbered_reg_matcher(operands[1])) { |
1607 | origin_operand = &operands[0]; |
1608 | } |
1609 | else { |
1610 | continue; |
1611 | } |
1612 | |
1613 | // We have an origin operand. Can we track its value down? |
1614 | ValueObjectSP source_path; |
1615 | ConstString origin_register; |
1616 | int64_t origin_offset = 0; |
1617 | |
1618 | if (FetchRegOp(origin_register)(*origin_operand)) { |
1619 | source_path = DoGuessValueAt(frame, origin_register, 0, disassembler, |
1620 | variables, instruction_sp->GetAddress()); |
1621 | } else if (MatchUnaryOp( |
1622 | MatchOpType(Instruction::Operand::Type::Dereference), |
1623 | FetchRegOp(origin_register))(*origin_operand) || |
1624 | MatchUnaryOp( |
1625 | MatchOpType(Instruction::Operand::Type::Dereference), |
1626 | MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum), |
1627 | FetchRegOp(origin_register), |
1628 | FetchImmOp(origin_offset)))(*origin_operand)) { |
1629 | source_path = |
1630 | DoGuessValueAt(frame, origin_register, origin_offset, disassembler, |
1631 | variables, instruction_sp->GetAddress()); |
1632 | if (!source_path) { |
1633 | continue; |
1634 | } |
1635 | source_path = |
1636 | GetValueForDereferincingOffset(frame, source_path, offset); |
1637 | } |
1638 | |
1639 | if (source_path) { |
1640 | return source_path; |
1641 | } |
1642 | } |
1643 | |
1644 | return ValueObjectSP(); |
1645 | } |
1646 | } |
1647 | |
1648 | lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg, |
1649 | int64_t offset) { |
1650 | TargetSP target_sp = CalculateTarget(); |
1651 | |
1652 | const ArchSpec &target_arch = target_sp->GetArchitecture(); |
1653 | |
1654 | Block *frame_block = GetFrameBlock(); |
1655 | |
1656 | if (!frame_block) { |
1657 | return ValueObjectSP(); |
1658 | } |
1659 | |
1660 | Function *function = frame_block->CalculateSymbolContextFunction(); |
1661 | if (!function) { |
1662 | return ValueObjectSP(); |
1663 | } |
1664 | |
1665 | AddressRange pc_range = function->GetAddressRange(); |
1666 | |
1667 | if (GetFrameCodeAddress().GetFileAddress() < |
1668 | pc_range.GetBaseAddress().GetFileAddress() || |
1669 | GetFrameCodeAddress().GetFileAddress() - |
1670 | pc_range.GetBaseAddress().GetFileAddress() >= |
1671 | pc_range.GetByteSize()) { |
1672 | return ValueObjectSP(); |
1673 | } |
1674 | |
1675 | const char *plugin_name = nullptr; |
1676 | const char *flavor = nullptr; |
1677 | const bool force_live_memory = true; |
1678 | DisassemblerSP disassembler_sp = |
1679 | Disassembler::DisassembleRange(target_arch, plugin_name, flavor, |
1680 | *target_sp, pc_range, force_live_memory); |
1681 | |
1682 | if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) { |
1683 | return ValueObjectSP(); |
1684 | } |
1685 | |
1686 | const bool get_file_globals = false; |
1687 | VariableList *variables = GetVariableList(get_file_globals); |
1688 | |
1689 | if (!variables) { |
1690 | return ValueObjectSP(); |
1691 | } |
1692 | |
1693 | return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables, |
1694 | GetFrameCodeAddress()); |
1695 | } |
1696 | |
1697 | lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) { |
1698 | ValueObjectSP value_sp; |
1699 | |
1700 | if (!name) |
1701 | return value_sp; |
1702 | |
1703 | TargetSP target_sp = CalculateTarget(); |
1704 | ProcessSP process_sp = CalculateProcess(); |
1705 | |
1706 | if (!target_sp && !process_sp) |
1707 | return value_sp; |
1708 | |
1709 | VariableList variable_list; |
1710 | VariableSP var_sp; |
1711 | SymbolContext sc(GetSymbolContext(eSymbolContextBlock)); |
1712 | |
1713 | if (sc.block) { |
1714 | const bool can_create = true; |
1715 | const bool get_parent_variables = true; |
1716 | const bool stop_if_block_is_inlined_function = true; |
1717 | |
1718 | if (sc.block->AppendVariables( |
1719 | can_create, get_parent_variables, stop_if_block_is_inlined_function, |
1720 | [this](Variable *v) { return v->IsInScope(this); }, |
1721 | &variable_list)) { |
1722 | var_sp = variable_list.FindVariable(name); |
1723 | } |
1724 | |
1725 | if (var_sp) |
1726 | value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues); |
1727 | } |
1728 | |
1729 | return value_sp; |
1730 | } |
1731 | |
1732 | TargetSP StackFrame::CalculateTarget() { |
1733 | TargetSP target_sp; |
1734 | ThreadSP thread_sp(GetThread()); |
1735 | if (thread_sp) { |
1736 | ProcessSP process_sp(thread_sp->CalculateProcess()); |
1737 | if (process_sp) |
1738 | target_sp = process_sp->CalculateTarget(); |
1739 | } |
1740 | return target_sp; |
1741 | } |
1742 | |
1743 | ProcessSP StackFrame::CalculateProcess() { |
1744 | ProcessSP process_sp; |
1745 | ThreadSP thread_sp(GetThread()); |
1746 | if (thread_sp) |
1747 | process_sp = thread_sp->CalculateProcess(); |
1748 | return process_sp; |
1749 | } |
1750 | |
1751 | ThreadSP StackFrame::CalculateThread() { return GetThread(); } |
1752 | |
1753 | StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); } |
1754 | |
1755 | void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) { |
1756 | exe_ctx.SetContext(shared_from_this()); |
1757 | } |
1758 | |
1759 | void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique, |
1760 | const char *frame_marker) { |
1761 | if (strm == nullptr) |
1762 | return; |
1763 | |
1764 | GetSymbolContext(eSymbolContextEverything); |
1765 | ExecutionContext exe_ctx(shared_from_this()); |
1766 | StreamString s; |
1767 | |
1768 | if (frame_marker) |
1769 | s.PutCString(frame_marker); |
1770 | |
1771 | const FormatEntity::Entry *frame_format = nullptr; |
1772 | Target *target = exe_ctx.GetTargetPtr(); |
1773 | if (target) { |
1774 | if (show_unique) { |
1775 | frame_format = target->GetDebugger().GetFrameFormatUnique(); |
1776 | } else { |
1777 | frame_format = target->GetDebugger().GetFrameFormat(); |
1778 | } |
1779 | } |
1780 | if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx, |
1781 | nullptr, nullptr, false, false)) { |
1782 | strm->PutCString(s.GetString()); |
1783 | } else { |
1784 | Dump(strm, true, false); |
1785 | strm->EOL(); |
1786 | } |
1787 | } |
1788 | |
1789 | void StackFrame::Dump(Stream *strm, bool show_frame_index, |
1790 | bool show_fullpaths) { |
1791 | if (strm == nullptr) |
1792 | return; |
1793 | |
1794 | if (show_frame_index) |
1795 | strm->Printf("frame #%u: ", m_frame_index); |
1796 | ExecutionContext exe_ctx(shared_from_this()); |
1797 | Target *target = exe_ctx.GetTargetPtr(); |
1798 | strm->Printf("0x%0*" PRIx64"l" "x" " ", |
1799 | target ? (target->GetArchitecture().GetAddressByteSize() * 2) |
1800 | : 16, |
1801 | GetFrameCodeAddress().GetLoadAddress(target)); |
1802 | GetSymbolContext(eSymbolContextEverything); |
1803 | const bool show_module = true; |
1804 | const bool show_inline = true; |
1805 | const bool show_function_arguments = true; |
1806 | const bool show_function_name = true; |
1807 | m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(), |
1808 | GetFrameCodeAddress(), show_fullpaths, show_module, |
1809 | show_inline, show_function_arguments, |
1810 | show_function_name); |
1811 | } |
1812 | |
1813 | void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) { |
1814 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1815 | assert(GetStackID() ==(static_cast <bool> (GetStackID() == prev_frame.GetStackID ()) ? void (0) : __assert_fail ("GetStackID() == prev_frame.GetStackID()" , "lldb/source/Target/StackFrame.cpp", 1816, __extension__ __PRETTY_FUNCTION__ )) |
1816 | prev_frame.GetStackID())(static_cast <bool> (GetStackID() == prev_frame.GetStackID ()) ? void (0) : __assert_fail ("GetStackID() == prev_frame.GetStackID()" , "lldb/source/Target/StackFrame.cpp", 1816, __extension__ __PRETTY_FUNCTION__ )); // TODO: remove this after some testing |
1817 | m_variable_list_sp = prev_frame.m_variable_list_sp; |
1818 | m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects); |
1819 | if (!m_disassembly.GetString().empty()) { |
1820 | m_disassembly.Clear(); |
1821 | m_disassembly.PutCString(prev_frame.m_disassembly.GetString()); |
1822 | } |
1823 | } |
1824 | |
1825 | void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) { |
1826 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
1827 | assert(GetStackID() ==(static_cast <bool> (GetStackID() == curr_frame.GetStackID ()) ? void (0) : __assert_fail ("GetStackID() == curr_frame.GetStackID()" , "lldb/source/Target/StackFrame.cpp", 1828, __extension__ __PRETTY_FUNCTION__ )) |
1828 | curr_frame.GetStackID())(static_cast <bool> (GetStackID() == curr_frame.GetStackID ()) ? void (0) : __assert_fail ("GetStackID() == curr_frame.GetStackID()" , "lldb/source/Target/StackFrame.cpp", 1828, __extension__ __PRETTY_FUNCTION__ )); // TODO: remove this after some testing |
1829 | m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value |
1830 | assert(GetThread() == curr_frame.GetThread())(static_cast <bool> (GetThread() == curr_frame.GetThread ()) ? void (0) : __assert_fail ("GetThread() == curr_frame.GetThread()" , "lldb/source/Target/StackFrame.cpp", 1830, __extension__ __PRETTY_FUNCTION__ )); |
1831 | m_frame_index = curr_frame.m_frame_index; |
1832 | m_concrete_frame_index = curr_frame.m_concrete_frame_index; |
1833 | m_reg_context_sp = curr_frame.m_reg_context_sp; |
1834 | m_frame_code_addr = curr_frame.m_frame_code_addr; |
1835 | m_behaves_like_zeroth_frame = curr_frame.m_behaves_like_zeroth_frame; |
1836 | assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||(static_cast <bool> (!m_sc.target_sp || !curr_frame.m_sc .target_sp || m_sc.target_sp.get() == curr_frame.m_sc.target_sp .get()) ? void (0) : __assert_fail ("!m_sc.target_sp || !curr_frame.m_sc.target_sp || m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get()" , "lldb/source/Target/StackFrame.cpp", 1837, __extension__ __PRETTY_FUNCTION__ )) |
1837 | m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get())(static_cast <bool> (!m_sc.target_sp || !curr_frame.m_sc .target_sp || m_sc.target_sp.get() == curr_frame.m_sc.target_sp .get()) ? void (0) : __assert_fail ("!m_sc.target_sp || !curr_frame.m_sc.target_sp || m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get()" , "lldb/source/Target/StackFrame.cpp", 1837, __extension__ __PRETTY_FUNCTION__ )); |
1838 | assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||(static_cast <bool> (!m_sc.module_sp || !curr_frame.m_sc .module_sp || m_sc.module_sp.get() == curr_frame.m_sc.module_sp .get()) ? void (0) : __assert_fail ("!m_sc.module_sp || !curr_frame.m_sc.module_sp || m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get()" , "lldb/source/Target/StackFrame.cpp", 1839, __extension__ __PRETTY_FUNCTION__ )) |
1839 | m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get())(static_cast <bool> (!m_sc.module_sp || !curr_frame.m_sc .module_sp || m_sc.module_sp.get() == curr_frame.m_sc.module_sp .get()) ? void (0) : __assert_fail ("!m_sc.module_sp || !curr_frame.m_sc.module_sp || m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get()" , "lldb/source/Target/StackFrame.cpp", 1839, __extension__ __PRETTY_FUNCTION__ )); |
1840 | assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||(static_cast <bool> (m_sc.comp_unit == nullptr || curr_frame .m_sc.comp_unit == nullptr || m_sc.comp_unit == curr_frame.m_sc .comp_unit) ? void (0) : __assert_fail ("m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr || m_sc.comp_unit == curr_frame.m_sc.comp_unit" , "lldb/source/Target/StackFrame.cpp", 1841, __extension__ __PRETTY_FUNCTION__ )) |
1841 | m_sc.comp_unit == curr_frame.m_sc.comp_unit)(static_cast <bool> (m_sc.comp_unit == nullptr || curr_frame .m_sc.comp_unit == nullptr || m_sc.comp_unit == curr_frame.m_sc .comp_unit) ? void (0) : __assert_fail ("m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr || m_sc.comp_unit == curr_frame.m_sc.comp_unit" , "lldb/source/Target/StackFrame.cpp", 1841, __extension__ __PRETTY_FUNCTION__ )); |
1842 | assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||(static_cast <bool> (m_sc.function == nullptr || curr_frame .m_sc.function == nullptr || m_sc.function == curr_frame.m_sc .function) ? void (0) : __assert_fail ("m_sc.function == nullptr || curr_frame.m_sc.function == nullptr || m_sc.function == curr_frame.m_sc.function" , "lldb/source/Target/StackFrame.cpp", 1843, __extension__ __PRETTY_FUNCTION__ )) |
1843 | m_sc.function == curr_frame.m_sc.function)(static_cast <bool> (m_sc.function == nullptr || curr_frame .m_sc.function == nullptr || m_sc.function == curr_frame.m_sc .function) ? void (0) : __assert_fail ("m_sc.function == nullptr || curr_frame.m_sc.function == nullptr || m_sc.function == curr_frame.m_sc.function" , "lldb/source/Target/StackFrame.cpp", 1843, __extension__ __PRETTY_FUNCTION__ )); |
1844 | m_sc = curr_frame.m_sc; |
1845 | m_flags.Clear(GOT_FRAME_BASE(((uint32_t(eSymbolContextEverything + 1)) << 1) << 1) | eSymbolContextEverything); |
1846 | m_flags.Set(m_sc.GetResolvedMask()); |
1847 | m_frame_base.Clear(); |
1848 | m_frame_base_error.Clear(); |
1849 | } |
1850 | |
1851 | bool StackFrame::HasCachedData() const { |
1852 | if (m_variable_list_sp) |
1853 | return true; |
1854 | if (m_variable_list_value_objects.GetSize() > 0) |
1855 | return true; |
1856 | if (!m_disassembly.GetString().empty()) |
1857 | return true; |
1858 | return false; |
1859 | } |
1860 | |
1861 | bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source, |
1862 | bool show_unique, const char *frame_marker) { |
1863 | if (show_frame_info) { |
1864 | strm.Indent(); |
1865 | DumpUsingSettingsFormat(&strm, show_unique, frame_marker); |
1866 | } |
1867 | |
1868 | if (show_source) { |
1869 | ExecutionContext exe_ctx(shared_from_this()); |
1870 | bool have_source = false, have_debuginfo = false; |
1871 | Debugger::StopDisassemblyType disasm_display = |
1872 | Debugger::eStopDisassemblyTypeNever; |
1873 | Target *target = exe_ctx.GetTargetPtr(); |
1874 | if (target) { |
1875 | Debugger &debugger = target->GetDebugger(); |
1876 | const uint32_t source_lines_before = |
1877 | debugger.GetStopSourceLineCount(true); |
1878 | const uint32_t source_lines_after = |
1879 | debugger.GetStopSourceLineCount(false); |
1880 | disasm_display = debugger.GetStopDisassemblyDisplay(); |
1881 | |
1882 | GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry); |
1883 | if (m_sc.comp_unit && m_sc.line_entry.IsValid()) { |
1884 | have_debuginfo = true; |
1885 | if (source_lines_before > 0 || source_lines_after > 0) { |
1886 | uint32_t start_line = m_sc.line_entry.line; |
1887 | if (!start_line && m_sc.function) { |
1888 | FileSpec source_file; |
1889 | m_sc.function->GetStartLineSourceInfo(source_file, start_line); |
1890 | } |
1891 | |
1892 | size_t num_lines = |
1893 | target->GetSourceManager().DisplaySourceLinesWithLineNumbers( |
1894 | m_sc.line_entry.file, start_line, m_sc.line_entry.column, |
1895 | source_lines_before, source_lines_after, "->", &strm); |
1896 | if (num_lines != 0) |
1897 | have_source = true; |
1898 | // TODO: Give here a one time warning if source file is missing. |
1899 | if (!m_sc.line_entry.line) { |
1900 | ConstString fn_name = m_sc.GetFunctionName(); |
1901 | |
1902 | if (!fn_name.IsEmpty()) |
1903 | strm.Printf( |
1904 | "Note: this address is compiler-generated code in function " |
1905 | "%s that has no source code associated with it.", |
1906 | fn_name.AsCString()); |
1907 | else |
1908 | strm.Printf("Note: this address is compiler-generated code that " |
1909 | "has no source code associated with it."); |
1910 | strm.EOL(); |
1911 | } |
1912 | } |
1913 | } |
1914 | switch (disasm_display) { |
1915 | case Debugger::eStopDisassemblyTypeNever: |
1916 | break; |
1917 | |
1918 | case Debugger::eStopDisassemblyTypeNoDebugInfo: |
1919 | if (have_debuginfo) |
1920 | break; |
1921 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; |
1922 | |
1923 | case Debugger::eStopDisassemblyTypeNoSource: |
1924 | if (have_source) |
1925 | break; |
1926 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; |
1927 | |
1928 | case Debugger::eStopDisassemblyTypeAlways: |
1929 | if (target) { |
1930 | const uint32_t disasm_lines = debugger.GetDisassemblyLineCount(); |
1931 | if (disasm_lines > 0) { |
1932 | const ArchSpec &target_arch = target->GetArchitecture(); |
1933 | const char *plugin_name = nullptr; |
1934 | const char *flavor = nullptr; |
1935 | const bool mixed_source_and_assembly = false; |
1936 | Disassembler::Disassemble( |
1937 | target->GetDebugger(), target_arch, plugin_name, flavor, |
1938 | exe_ctx, GetFrameCodeAddress(), |
1939 | {Disassembler::Limit::Instructions, disasm_lines}, |
1940 | mixed_source_and_assembly, 0, |
1941 | Disassembler::eOptionMarkPCAddress, strm); |
1942 | } |
1943 | } |
1944 | break; |
1945 | } |
1946 | } |
1947 | } |
1948 | return true; |
1949 | } |
1950 | |
1951 | RecognizedStackFrameSP StackFrame::GetRecognizedFrame() { |
1952 | if (!m_recognized_frame_sp) { |
1953 | m_recognized_frame_sp = GetThread() |
1954 | ->GetProcess() |
1955 | ->GetTarget() |
1956 | .GetFrameRecognizerManager() |
1957 | .RecognizeFrame(CalculateStackFrame()); |
1958 | } |
1959 | return m_recognized_frame_sp; |
1960 | } |