[libc] Switch to using the generic `<gpuintrin.h>` implementations (#121810)
[llvm-project.git] / lldb / source / Target / StackFrame.cpp
blob2633c976c13bf495e1323db05155006d1b65008e
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 //===----------------------------------------------------------------------===//
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/Symbol/CompileUnit.h"
17 #include "lldb/Symbol/Function.h"
18 #include "lldb/Symbol/Symbol.h"
19 #include "lldb/Symbol/SymbolContextScope.h"
20 #include "lldb/Symbol/SymbolFile.h"
21 #include "lldb/Symbol/Type.h"
22 #include "lldb/Symbol/VariableList.h"
23 #include "lldb/Target/ABI.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/LanguageRuntime.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/RegisterContext.h"
28 #include "lldb/Target/StackFrameRecognizer.h"
29 #include "lldb/Target/Target.h"
30 #include "lldb/Target/Thread.h"
31 #include "lldb/Utility/LLDBLog.h"
32 #include "lldb/Utility/Log.h"
33 #include "lldb/Utility/RegisterValue.h"
34 #include "lldb/ValueObject/ValueObjectConstResult.h"
35 #include "lldb/ValueObject/ValueObjectMemory.h"
36 #include "lldb/ValueObject/ValueObjectVariable.h"
38 #include "lldb/lldb-enumerations.h"
40 #include <memory>
42 using namespace lldb;
43 using namespace lldb_private;
45 // The first bits in the flags are reserved for the SymbolContext::Scope bits
46 // so we know if we have tried to look up information in our internal symbol
47 // context (m_sc) already.
48 #define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextLastItem) << 1)
49 #define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
50 #define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
51 #define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
52 #define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
54 StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
55 user_id_t unwind_frame_index, addr_t cfa,
56 bool cfa_is_valid, addr_t pc, StackFrame::Kind kind,
57 bool behaves_like_zeroth_frame,
58 const SymbolContext *sc_ptr)
59 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
60 m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
61 m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
62 m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
63 m_stack_frame_kind(kind),
64 m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
65 m_variable_list_sp(), m_variable_list_value_objects(),
66 m_recognized_frame_sp(), m_disassembly(), m_mutex() {
67 // If we don't have a CFA value, use the frame index for our StackID so that
68 // recursive functions properly aren't confused with one another on a history
69 // stack.
70 if (IsHistorical() && !m_cfa_is_valid) {
71 m_id.SetCFA(m_frame_index);
74 if (sc_ptr != nullptr) {
75 m_sc = *sc_ptr;
76 m_flags.Set(m_sc.GetResolvedMask());
80 StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
81 user_id_t unwind_frame_index,
82 const RegisterContextSP &reg_context_sp, addr_t cfa,
83 addr_t pc, bool behaves_like_zeroth_frame,
84 const SymbolContext *sc_ptr)
85 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
86 m_concrete_frame_index(unwind_frame_index),
87 m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
88 m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
89 m_frame_base_error(), m_cfa_is_valid(true),
90 m_stack_frame_kind(StackFrame::Kind::Regular),
91 m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
92 m_variable_list_sp(), m_variable_list_value_objects(),
93 m_recognized_frame_sp(), m_disassembly(), m_mutex() {
94 if (sc_ptr != nullptr) {
95 m_sc = *sc_ptr;
96 m_flags.Set(m_sc.GetResolvedMask());
99 if (reg_context_sp && !m_sc.target_sp) {
100 m_sc.target_sp = reg_context_sp->CalculateTarget();
101 if (m_sc.target_sp)
102 m_flags.Set(eSymbolContextTarget);
106 StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
107 user_id_t unwind_frame_index,
108 const RegisterContextSP &reg_context_sp, addr_t cfa,
109 const Address &pc_addr, bool behaves_like_zeroth_frame,
110 const SymbolContext *sc_ptr)
111 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
112 m_concrete_frame_index(unwind_frame_index),
113 m_reg_context_sp(reg_context_sp),
114 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
115 nullptr),
116 m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
117 m_frame_base_error(), m_cfa_is_valid(true),
118 m_stack_frame_kind(StackFrame::Kind::Regular),
119 m_behaves_like_zeroth_frame(behaves_like_zeroth_frame),
120 m_variable_list_sp(), m_variable_list_value_objects(),
121 m_recognized_frame_sp(), m_disassembly(), m_mutex() {
122 if (sc_ptr != nullptr) {
123 m_sc = *sc_ptr;
124 m_flags.Set(m_sc.GetResolvedMask());
127 if (!m_sc.target_sp && reg_context_sp) {
128 m_sc.target_sp = reg_context_sp->CalculateTarget();
129 if (m_sc.target_sp)
130 m_flags.Set(eSymbolContextTarget);
133 ModuleSP pc_module_sp(pc_addr.GetModule());
134 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
135 if (pc_module_sp) {
136 m_sc.module_sp = pc_module_sp;
137 m_flags.Set(eSymbolContextModule);
138 } else {
139 m_sc.module_sp.reset();
144 StackFrame::~StackFrame() = default;
146 StackID &StackFrame::GetStackID() {
147 std::lock_guard<std::recursive_mutex> guard(m_mutex);
148 // Make sure we have resolved the StackID object's symbol context scope if we
149 // already haven't looked it up.
151 if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
152 if (m_id.GetSymbolContextScope()) {
153 // We already have a symbol context scope, we just don't have our flag
154 // bit set.
155 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
156 } else {
157 // Calculate the frame block and use this for the stack ID symbol context
158 // scope if we have one.
159 SymbolContextScope *scope = GetFrameBlock();
160 if (scope == nullptr) {
161 // We don't have a block, so use the symbol
162 if (m_flags.IsClear(eSymbolContextSymbol))
163 GetSymbolContext(eSymbolContextSymbol);
165 // It is ok if m_sc.symbol is nullptr here
166 scope = m_sc.symbol;
168 // Set the symbol context scope (the accessor will set the
169 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
170 SetSymbolContextScope(scope);
173 return m_id;
176 uint32_t StackFrame::GetFrameIndex() const {
177 ThreadSP thread_sp = GetThread();
178 if (thread_sp)
179 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
180 m_frame_index);
181 else
182 return m_frame_index;
185 void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
186 std::lock_guard<std::recursive_mutex> guard(m_mutex);
187 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
188 m_id.SetSymbolContextScope(symbol_scope);
191 const Address &StackFrame::GetFrameCodeAddress() {
192 std::lock_guard<std::recursive_mutex> guard(m_mutex);
193 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
194 !m_frame_code_addr.IsSectionOffset()) {
195 m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
197 // Resolve the PC into a temporary address because if ResolveLoadAddress
198 // fails to resolve the address, it will clear the address object...
199 ThreadSP thread_sp(GetThread());
200 if (thread_sp) {
201 TargetSP target_sp(thread_sp->CalculateTarget());
202 if (target_sp) {
203 const bool allow_section_end = true;
204 if (m_frame_code_addr.SetOpcodeLoadAddress(
205 m_frame_code_addr.GetOffset(), target_sp.get(),
206 AddressClass::eCode, allow_section_end)) {
207 ModuleSP module_sp(m_frame_code_addr.GetModule());
208 if (module_sp) {
209 m_sc.module_sp = module_sp;
210 m_flags.Set(eSymbolContextModule);
216 return m_frame_code_addr;
219 // This can't be rewritten into a call to
220 // RegisterContext::GetPCForSymbolication because this
221 // StackFrame may have been constructed with a special pc,
222 // e.g. tail-call artificial frames.
223 Address StackFrame::GetFrameCodeAddressForSymbolication() {
224 Address lookup_addr(GetFrameCodeAddress());
225 if (!lookup_addr.IsValid())
226 return lookup_addr;
227 if (m_behaves_like_zeroth_frame)
228 return lookup_addr;
230 addr_t offset = lookup_addr.GetOffset();
231 if (offset > 0) {
232 lookup_addr.SetOffset(offset - 1);
233 } else {
234 // lookup_addr is the start of a section. We need do the math on the
235 // actual load address and re-compute the section. We're working with
236 // a 'noreturn' function at the end of a section.
237 TargetSP target_sp = CalculateTarget();
238 if (target_sp) {
239 addr_t addr_minus_one = lookup_addr.GetOpcodeLoadAddress(
240 target_sp.get(), AddressClass::eCode) -
242 lookup_addr.SetOpcodeLoadAddress(addr_minus_one, target_sp.get());
245 return lookup_addr;
248 bool StackFrame::ChangePC(addr_t pc) {
249 std::lock_guard<std::recursive_mutex> guard(m_mutex);
250 // We can't change the pc value of a history stack frame - it is immutable.
251 if (IsHistorical())
252 return false;
253 m_frame_code_addr.SetRawAddress(pc);
254 m_sc.Clear(false);
255 m_flags.Reset(0);
256 ThreadSP thread_sp(GetThread());
257 if (thread_sp)
258 thread_sp->ClearStackFrames();
259 return true;
262 const char *StackFrame::Disassemble() {
263 std::lock_guard<std::recursive_mutex> guard(m_mutex);
264 if (!m_disassembly.Empty())
265 return m_disassembly.GetData();
267 ExecutionContext exe_ctx(shared_from_this());
268 if (Target *target = exe_ctx.GetTargetPtr()) {
269 Disassembler::Disassemble(target->GetDebugger(), target->GetArchitecture(),
270 *this, m_disassembly);
273 return m_disassembly.Empty() ? nullptr : m_disassembly.GetData();
276 Block *StackFrame::GetFrameBlock() {
277 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
278 GetSymbolContext(eSymbolContextBlock);
280 if (m_sc.block) {
281 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
282 if (inline_block) {
283 // Use the block with the inlined function info as the frame block we
284 // want this frame to have only the variables for the inlined function
285 // and its non-inlined block child blocks.
286 return inline_block;
287 } else {
288 // This block is not contained within any inlined function blocks with so
289 // we want to use the top most function block.
290 return &m_sc.function->GetBlock(false);
293 return nullptr;
296 // Get the symbol context if we already haven't done so by resolving the
297 // PC address as much as possible. This way when we pass around a
298 // StackFrame object, everyone will have as much information as possible and no
299 // one will ever have to look things up manually.
300 const SymbolContext &
301 StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) {
302 std::lock_guard<std::recursive_mutex> guard(m_mutex);
303 // Copy our internal symbol context into "sc".
304 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
305 uint32_t resolved = 0;
307 // If the target was requested add that:
308 if (!m_sc.target_sp) {
309 m_sc.target_sp = CalculateTarget();
310 if (m_sc.target_sp)
311 resolved |= eSymbolContextTarget;
314 // Resolve our PC to section offset if we haven't already done so and if we
315 // don't have a module. The resolved address section will contain the
316 // module to which it belongs
317 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
318 GetFrameCodeAddress();
320 // If this is not frame zero, then we need to subtract 1 from the PC value
321 // when doing address lookups since the PC will be on the instruction
322 // following the function call instruction...
323 Address lookup_addr(GetFrameCodeAddressForSymbolication());
325 if (m_sc.module_sp) {
326 // We have something in our stack frame symbol context, lets check if we
327 // haven't already tried to lookup one of those things. If we haven't
328 // then we will do the query.
330 SymbolContextItem actual_resolve_scope = SymbolContextItem(0);
332 if (resolve_scope & eSymbolContextCompUnit) {
333 if (m_flags.IsClear(eSymbolContextCompUnit)) {
334 if (m_sc.comp_unit)
335 resolved |= eSymbolContextCompUnit;
336 else
337 actual_resolve_scope |= eSymbolContextCompUnit;
341 if (resolve_scope & eSymbolContextFunction) {
342 if (m_flags.IsClear(eSymbolContextFunction)) {
343 if (m_sc.function)
344 resolved |= eSymbolContextFunction;
345 else
346 actual_resolve_scope |= eSymbolContextFunction;
350 if (resolve_scope & eSymbolContextBlock) {
351 if (m_flags.IsClear(eSymbolContextBlock)) {
352 if (m_sc.block)
353 resolved |= eSymbolContextBlock;
354 else
355 actual_resolve_scope |= eSymbolContextBlock;
359 if (resolve_scope & eSymbolContextSymbol) {
360 if (m_flags.IsClear(eSymbolContextSymbol)) {
361 if (m_sc.symbol)
362 resolved |= eSymbolContextSymbol;
363 else
364 actual_resolve_scope |= eSymbolContextSymbol;
368 if (resolve_scope & eSymbolContextLineEntry) {
369 if (m_flags.IsClear(eSymbolContextLineEntry)) {
370 if (m_sc.line_entry.IsValid())
371 resolved |= eSymbolContextLineEntry;
372 else
373 actual_resolve_scope |= eSymbolContextLineEntry;
377 if (actual_resolve_scope) {
378 // We might be resolving less information than what is already in our
379 // current symbol context so resolve into a temporary symbol context
380 // "sc" so we don't clear out data we have already found in "m_sc"
381 SymbolContext sc;
382 // Set flags that indicate what we have tried to resolve
383 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
384 lookup_addr, actual_resolve_scope, sc);
385 // Only replace what we didn't already have as we may have information
386 // for an inlined function scope that won't match what a standard
387 // lookup by address would match
388 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
389 m_sc.comp_unit = sc.comp_unit;
390 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
391 m_sc.function = sc.function;
392 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
393 m_sc.block = sc.block;
394 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
395 m_sc.symbol = sc.symbol;
396 if ((resolved & eSymbolContextLineEntry) &&
397 !m_sc.line_entry.IsValid()) {
398 m_sc.line_entry = sc.line_entry;
399 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
402 } else {
403 // If we don't have a module, then we can't have the compile unit,
404 // function, block, line entry or symbol, so we can safely call
405 // ResolveSymbolContextForAddress with our symbol context member m_sc.
406 if (m_sc.target_sp) {
407 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
408 lookup_addr, resolve_scope, m_sc);
412 // Update our internal flags so we remember what we have tried to locate so
413 // we don't have to keep trying when more calls to this function are made.
414 // We might have dug up more information that was requested (for example if
415 // we were asked to only get the block, we will have gotten the compile
416 // unit, and function) so set any additional bits that we resolved
417 m_flags.Set(resolve_scope | resolved);
420 // Return the symbol context with everything that was possible to resolve
421 // resolved.
422 return m_sc;
425 VariableList *StackFrame::GetVariableList(bool get_file_globals,
426 Status *error_ptr) {
427 std::lock_guard<std::recursive_mutex> guard(m_mutex);
428 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
429 m_flags.Set(RESOLVED_VARIABLES);
430 m_variable_list_sp = std::make_shared<VariableList>();
432 Block *frame_block = GetFrameBlock();
434 if (frame_block) {
435 const bool get_child_variables = true;
436 const bool can_create = true;
437 const bool stop_if_child_block_is_inlined_function = true;
438 frame_block->AppendBlockVariables(can_create, get_child_variables,
439 stop_if_child_block_is_inlined_function,
440 [](Variable *v) { return true; },
441 m_variable_list_sp.get());
445 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
446 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
448 if (m_flags.IsClear(eSymbolContextCompUnit))
449 GetSymbolContext(eSymbolContextCompUnit);
451 if (m_sc.comp_unit) {
452 VariableListSP global_variable_list_sp(
453 m_sc.comp_unit->GetVariableList(true));
454 if (m_variable_list_sp)
455 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
456 else
457 m_variable_list_sp = global_variable_list_sp;
461 if (error_ptr && m_variable_list_sp->GetSize() == 0) {
462 // Check with the symbol file to check if there is an error for why we
463 // don't have variables that the user might need to know about.
464 GetSymbolContext(eSymbolContextEverything);
465 if (m_sc.module_sp) {
466 SymbolFile *sym_file = m_sc.module_sp->GetSymbolFile();
467 if (sym_file)
468 *error_ptr = sym_file->GetFrameVariableError(*this);
472 return m_variable_list_sp.get();
475 VariableListSP
476 StackFrame::GetInScopeVariableList(bool get_file_globals,
477 bool must_have_valid_location) {
478 std::lock_guard<std::recursive_mutex> guard(m_mutex);
479 // We can't fetch variable information for a history stack frame.
480 if (IsHistorical())
481 return VariableListSP();
483 VariableListSP var_list_sp(new VariableList);
484 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
486 if (m_sc.block) {
487 const bool can_create = true;
488 const bool get_parent_variables = true;
489 const bool stop_if_block_is_inlined_function = true;
490 m_sc.block->AppendVariables(
491 can_create, get_parent_variables, stop_if_block_is_inlined_function,
492 [this, must_have_valid_location](Variable *v) {
493 return v->IsInScope(this) && (!must_have_valid_location ||
494 v->LocationIsValidForFrame(this));
496 var_list_sp.get());
499 if (m_sc.comp_unit && get_file_globals) {
500 VariableListSP global_variable_list_sp(
501 m_sc.comp_unit->GetVariableList(true));
502 if (global_variable_list_sp)
503 var_list_sp->AddVariables(global_variable_list_sp.get());
506 return var_list_sp;
509 ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
510 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
511 VariableSP &var_sp, Status &error) {
512 ExecutionContext exe_ctx;
513 CalculateExecutionContext(exe_ctx);
514 bool use_DIL = exe_ctx.GetTargetRef().GetUseDIL(&exe_ctx);
515 if (use_DIL)
516 return DILGetValueForVariableExpressionPath(var_expr, use_dynamic, options,
517 var_sp, error);
519 return LegacyGetValueForVariableExpressionPath(var_expr, use_dynamic, options,
520 var_sp, error);
523 ValueObjectSP StackFrame::DILGetValueForVariableExpressionPath(
524 llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic,
525 uint32_t options, lldb::VariableSP &var_sp, Status &error) {
526 // This is a place-holder for the calls into the DIL parser and
527 // evaluator. For now, just call the "real" frame variable implementation.
528 return LegacyGetValueForVariableExpressionPath(var_expr, use_dynamic, options,
529 var_sp, error);
532 ValueObjectSP StackFrame::LegacyGetValueForVariableExpressionPath(
533 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
534 VariableSP &var_sp, Status &error) {
535 llvm::StringRef original_var_expr = var_expr;
536 // We can't fetch variable information for a history stack frame.
537 if (IsHistorical())
538 return ValueObjectSP();
540 if (var_expr.empty()) {
541 error = Status::FromErrorStringWithFormatv("invalid variable path '{0}'",
542 var_expr);
543 return ValueObjectSP();
546 const bool check_ptr_vs_member =
547 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
548 const bool no_fragile_ivar =
549 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
550 const bool no_synth_child =
551 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
552 // const bool no_synth_array = (options &
553 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
554 error.Clear();
555 bool deref = false;
556 bool address_of = false;
557 ValueObjectSP valobj_sp;
558 const bool get_file_globals = true;
559 // When looking up a variable for an expression, we need only consider the
560 // variables that are in scope.
561 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
562 VariableList *variable_list = var_list_sp.get();
564 if (!variable_list)
565 return ValueObjectSP();
567 // If first character is a '*', then show pointer contents
568 std::string var_expr_storage;
569 if (var_expr[0] == '*') {
570 deref = true;
571 var_expr = var_expr.drop_front(); // Skip the '*'
572 } else if (var_expr[0] == '&') {
573 address_of = true;
574 var_expr = var_expr.drop_front(); // Skip the '&'
577 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
578 StreamString var_expr_path_strm;
580 ConstString name_const_string(var_expr.substr(0, separator_idx));
582 var_sp = variable_list->FindVariable(name_const_string, false);
584 bool synthetically_added_instance_object = false;
586 if (var_sp) {
587 var_expr = var_expr.drop_front(name_const_string.GetLength());
590 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
591 // Check for direct ivars access which helps us with implicit access to
592 // ivars using "this" or "self".
593 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
594 llvm::StringRef instance_var_name = m_sc.GetInstanceVariableName();
595 if (!instance_var_name.empty()) {
596 var_sp = variable_list->FindVariable(ConstString(instance_var_name));
597 if (var_sp) {
598 separator_idx = 0;
599 if (Type *var_type = var_sp->GetType())
600 if (auto compiler_type = var_type->GetForwardCompilerType())
601 if (!compiler_type.IsPointerType())
602 var_expr_storage = ".";
604 if (var_expr_storage.empty())
605 var_expr_storage = "->";
606 var_expr_storage += var_expr;
607 var_expr = var_expr_storage;
608 synthetically_added_instance_object = true;
613 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
614 // Check if any anonymous unions are there which contain a variable with
615 // the name we need
616 for (const VariableSP &variable_sp : *variable_list) {
617 if (!variable_sp)
618 continue;
619 if (!variable_sp->GetName().IsEmpty())
620 continue;
622 Type *var_type = variable_sp->GetType();
623 if (!var_type)
624 continue;
626 if (!var_type->GetForwardCompilerType().IsAnonymousType())
627 continue;
628 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
629 if (!valobj_sp)
630 return valobj_sp;
631 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string);
632 if (valobj_sp)
633 break;
637 if (var_sp && !valobj_sp) {
638 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
639 if (!valobj_sp)
640 return valobj_sp;
642 if (!valobj_sp) {
643 error = Status::FromErrorStringWithFormatv(
644 "no variable named '{0}' found in this frame", name_const_string);
645 return ValueObjectSP();
648 // We are dumping at least one child
649 while (!var_expr.empty()) {
650 // Calculate the next separator index ahead of time
651 ValueObjectSP child_valobj_sp;
652 const char separator_type = var_expr[0];
653 bool expr_is_ptr = false;
654 switch (separator_type) {
655 case '-':
656 expr_is_ptr = true;
657 if (var_expr.size() >= 2 && var_expr[1] != '>')
658 return ValueObjectSP();
660 if (no_fragile_ivar) {
661 // Make sure we aren't trying to deref an objective
662 // C ivar if this is not allowed
663 const uint32_t pointer_type_flags =
664 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
665 if ((pointer_type_flags & eTypeIsObjC) &&
666 (pointer_type_flags & eTypeIsPointer)) {
667 // This was an objective C object pointer and it was requested we
668 // skip any fragile ivars so return nothing here
669 return ValueObjectSP();
673 // If we have a non-pointer type with a synthetic value then lets check if
674 // we have a synthetic dereference specified.
675 if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) {
676 Status deref_error;
677 if (valobj_sp->GetCompilerType().IsReferenceType()) {
678 valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error);
679 if (!valobj_sp || deref_error.Fail()) {
680 error = Status::FromErrorStringWithFormatv(
681 "Failed to dereference reference type: {0}", deref_error);
682 return ValueObjectSP();
686 valobj_sp = valobj_sp->Dereference(deref_error);
687 if (!valobj_sp || deref_error.Fail()) {
688 error = Status::FromErrorStringWithFormatv(
689 "Failed to dereference synthetic value: {0}", deref_error);
690 return ValueObjectSP();
692 // Some synthetic plug-ins fail to set the error in Dereference
693 if (!valobj_sp) {
694 error =
695 Status::FromErrorString("Failed to dereference synthetic value");
696 return ValueObjectSP();
698 expr_is_ptr = false;
701 var_expr = var_expr.drop_front(); // Remove the '-'
702 [[fallthrough]];
703 case '.': {
704 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
705 separator_idx = var_expr.find_first_of(".-[");
706 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
708 if (check_ptr_vs_member) {
709 // We either have a pointer type and need to verify valobj_sp is a
710 // pointer, or we have a member of a class/union/struct being accessed
711 // with the . syntax and need to verify we don't have a pointer.
712 const bool actual_is_ptr = valobj_sp->IsPointerType();
714 if (actual_is_ptr != expr_is_ptr) {
715 // Incorrect use of "." with a pointer, or "->" with a
716 // class/union/struct instance or reference.
717 valobj_sp->GetExpressionPath(var_expr_path_strm);
718 if (actual_is_ptr)
719 error = Status::FromErrorStringWithFormat(
720 "\"%s\" is a pointer and . was used to attempt to access "
721 "\"%s\". Did you mean \"%s->%s\"?",
722 var_expr_path_strm.GetData(), child_name.GetCString(),
723 var_expr_path_strm.GetData(), var_expr.str().c_str());
724 else
725 error = Status::FromErrorStringWithFormat(
726 "\"%s\" is not a pointer and -> was used to attempt to "
727 "access \"%s\". Did you mean \"%s.%s\"?",
728 var_expr_path_strm.GetData(), child_name.GetCString(),
729 var_expr_path_strm.GetData(), var_expr.str().c_str());
730 return ValueObjectSP();
733 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name);
734 if (!child_valobj_sp) {
735 if (!no_synth_child) {
736 child_valobj_sp = valobj_sp->GetSyntheticValue();
737 if (child_valobj_sp)
738 child_valobj_sp =
739 child_valobj_sp->GetChildMemberWithName(child_name);
742 if (no_synth_child || !child_valobj_sp) {
743 // No child member with name "child_name"
744 if (synthetically_added_instance_object) {
745 // We added a "this->" or "self->" to the beginning of the
746 // expression and this is the first pointer ivar access, so just
747 // return the normal error
748 error = Status::FromErrorStringWithFormat(
749 "no variable or instance variable named '%s' found in "
750 "this frame",
751 name_const_string.GetCString());
752 } else {
753 valobj_sp->GetExpressionPath(var_expr_path_strm);
754 if (child_name) {
755 error = Status::FromErrorStringWithFormat(
756 "\"%s\" is not a member of \"(%s) %s\"",
757 child_name.GetCString(),
758 valobj_sp->GetTypeName().AsCString("<invalid type>"),
759 var_expr_path_strm.GetData());
760 } else {
761 error = Status::FromErrorStringWithFormat(
762 "incomplete expression path after \"%s\" in \"%s\"",
763 var_expr_path_strm.GetData(),
764 original_var_expr.str().c_str());
767 return ValueObjectSP();
770 synthetically_added_instance_object = false;
771 // Remove the child name from the path
772 var_expr = var_expr.drop_front(child_name.GetLength());
773 if (use_dynamic != eNoDynamicValues) {
774 ValueObjectSP dynamic_value_sp(
775 child_valobj_sp->GetDynamicValue(use_dynamic));
776 if (dynamic_value_sp)
777 child_valobj_sp = dynamic_value_sp;
779 } break;
781 case '[': {
782 // Array member access, or treating pointer as an array Need at least two
783 // brackets and a number
784 if (var_expr.size() <= 2) {
785 error = Status::FromErrorStringWithFormat(
786 "invalid square bracket encountered after \"%s\" in \"%s\"",
787 var_expr_path_strm.GetData(), var_expr.str().c_str());
788 return ValueObjectSP();
791 // Drop the open brace.
792 var_expr = var_expr.drop_front();
793 long child_index = 0;
795 // If there's no closing brace, this is an invalid expression.
796 size_t end_pos = var_expr.find_first_of(']');
797 if (end_pos == llvm::StringRef::npos) {
798 error = Status::FromErrorStringWithFormat(
799 "missing closing square bracket in expression \"%s\"",
800 var_expr_path_strm.GetData());
801 return ValueObjectSP();
803 llvm::StringRef index_expr = var_expr.take_front(end_pos);
804 llvm::StringRef original_index_expr = index_expr;
805 // Drop all of "[index_expr]"
806 var_expr = var_expr.drop_front(end_pos + 1);
808 if (index_expr.consumeInteger(0, child_index)) {
809 // If there was no integer anywhere in the index expression, this is
810 // erroneous expression.
811 error = Status::FromErrorStringWithFormat(
812 "invalid index expression \"%s\"", index_expr.str().c_str());
813 return ValueObjectSP();
816 if (index_expr.empty()) {
817 // The entire index expression was a single integer.
819 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
820 // what we have is *ptr[low]. the most similar C++ syntax is to deref
821 // ptr and extract bit low out of it. reading array item low would be
822 // done by saying ptr[low], without a deref * sign
823 Status deref_error;
824 ValueObjectSP temp(valobj_sp->Dereference(deref_error));
825 if (!temp || deref_error.Fail()) {
826 valobj_sp->GetExpressionPath(var_expr_path_strm);
827 error = Status::FromErrorStringWithFormat(
828 "could not dereference \"(%s) %s\"",
829 valobj_sp->GetTypeName().AsCString("<invalid type>"),
830 var_expr_path_strm.GetData());
831 return ValueObjectSP();
833 valobj_sp = temp;
834 deref = false;
835 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
836 deref) {
837 // what we have is *arr[low]. the most similar C++ syntax is to get
838 // arr[0] (an operation that is equivalent to deref-ing arr) and
839 // extract bit low out of it. reading array item low would be done by
840 // saying arr[low], without a deref * sign
841 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0));
842 if (!temp) {
843 valobj_sp->GetExpressionPath(var_expr_path_strm);
844 error = Status::FromErrorStringWithFormat(
845 "could not get item 0 for \"(%s) %s\"",
846 valobj_sp->GetTypeName().AsCString("<invalid type>"),
847 var_expr_path_strm.GetData());
848 return ValueObjectSP();
850 valobj_sp = temp;
851 deref = false;
854 bool is_incomplete_array = false;
855 if (valobj_sp->IsPointerType()) {
856 bool is_objc_pointer = true;
858 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
859 eLanguageTypeObjC)
860 is_objc_pointer = false;
861 else if (!valobj_sp->GetCompilerType().IsPointerType())
862 is_objc_pointer = false;
864 if (no_synth_child && is_objc_pointer) {
865 error = Status::FromErrorStringWithFormat(
866 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
867 "subscripted",
868 valobj_sp->GetTypeName().AsCString("<invalid type>"),
869 var_expr_path_strm.GetData());
871 return ValueObjectSP();
872 } else if (is_objc_pointer) {
873 // dereferencing ObjC variables is not valid.. so let's try and
874 // recur to synthetic children
875 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
876 if (!synthetic /* no synthetic */
877 || synthetic == valobj_sp) /* synthetic is the same as
878 the original object */
880 valobj_sp->GetExpressionPath(var_expr_path_strm);
881 error = Status::FromErrorStringWithFormat(
882 "\"(%s) %s\" is not an array type",
883 valobj_sp->GetTypeName().AsCString("<invalid type>"),
884 var_expr_path_strm.GetData());
885 } else if (static_cast<uint32_t>(child_index) >=
886 synthetic
887 ->GetNumChildrenIgnoringErrors() /* synthetic does
888 not have that
889 many values */) {
890 valobj_sp->GetExpressionPath(var_expr_path_strm);
891 error = Status::FromErrorStringWithFormat(
892 "array index %ld is not valid for \"(%s) %s\"", child_index,
893 valobj_sp->GetTypeName().AsCString("<invalid type>"),
894 var_expr_path_strm.GetData());
895 } else {
896 child_valobj_sp = synthetic->GetChildAtIndex(child_index);
897 if (!child_valobj_sp) {
898 valobj_sp->GetExpressionPath(var_expr_path_strm);
899 error = Status::FromErrorStringWithFormat(
900 "array index %ld is not valid for \"(%s) %s\"", child_index,
901 valobj_sp->GetTypeName().AsCString("<invalid type>"),
902 var_expr_path_strm.GetData());
905 } else {
906 child_valobj_sp =
907 valobj_sp->GetSyntheticArrayMember(child_index, true);
908 if (!child_valobj_sp) {
909 valobj_sp->GetExpressionPath(var_expr_path_strm);
910 error = Status::FromErrorStringWithFormat(
911 "failed to use pointer as array for index %ld for "
912 "\"(%s) %s\"",
913 child_index,
914 valobj_sp->GetTypeName().AsCString("<invalid type>"),
915 var_expr_path_strm.GetData());
918 } else if (valobj_sp->GetCompilerType().IsArrayType(
919 nullptr, nullptr, &is_incomplete_array)) {
920 // Pass false to dynamic_value here so we can tell the difference
921 // between no dynamic value and no member of this type...
922 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index);
923 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
924 child_valobj_sp =
925 valobj_sp->GetSyntheticArrayMember(child_index, true);
927 if (!child_valobj_sp) {
928 valobj_sp->GetExpressionPath(var_expr_path_strm);
929 error = Status::FromErrorStringWithFormat(
930 "array index %ld is not valid for \"(%s) %s\"", child_index,
931 valobj_sp->GetTypeName().AsCString("<invalid type>"),
932 var_expr_path_strm.GetData());
934 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
935 // this is a bitfield asking to display just one bit
936 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
937 child_index, child_index, true);
938 if (!child_valobj_sp) {
939 valobj_sp->GetExpressionPath(var_expr_path_strm);
940 error = Status::FromErrorStringWithFormat(
941 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
942 child_index, child_index,
943 valobj_sp->GetTypeName().AsCString("<invalid type>"),
944 var_expr_path_strm.GetData());
946 } else {
947 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
948 if (no_synth_child /* synthetic is forbidden */ ||
949 !synthetic /* no synthetic */
950 || synthetic == valobj_sp) /* synthetic is the same as the
951 original object */
953 valobj_sp->GetExpressionPath(var_expr_path_strm);
954 error = Status::FromErrorStringWithFormat(
955 "\"(%s) %s\" is not an array type",
956 valobj_sp->GetTypeName().AsCString("<invalid type>"),
957 var_expr_path_strm.GetData());
958 } else if (static_cast<uint32_t>(child_index) >=
959 synthetic->GetNumChildrenIgnoringErrors() /* synthetic
960 does not have that many values */) {
961 valobj_sp->GetExpressionPath(var_expr_path_strm);
962 error = Status::FromErrorStringWithFormat(
963 "array index %ld is not valid for \"(%s) %s\"", child_index,
964 valobj_sp->GetTypeName().AsCString("<invalid type>"),
965 var_expr_path_strm.GetData());
966 } else {
967 child_valobj_sp = synthetic->GetChildAtIndex(child_index);
968 if (!child_valobj_sp) {
969 valobj_sp->GetExpressionPath(var_expr_path_strm);
970 error = Status::FromErrorStringWithFormat(
971 "array index %ld is not valid for \"(%s) %s\"", child_index,
972 valobj_sp->GetTypeName().AsCString("<invalid type>"),
973 var_expr_path_strm.GetData());
978 if (!child_valobj_sp) {
979 // Invalid array index...
980 return ValueObjectSP();
983 if (use_dynamic != eNoDynamicValues) {
984 ValueObjectSP dynamic_value_sp(
985 child_valobj_sp->GetDynamicValue(use_dynamic));
986 if (dynamic_value_sp)
987 child_valobj_sp = dynamic_value_sp;
989 // Break out early from the switch since we were able to find the child
990 // member
991 break;
994 // this is most probably a BitField, let's take a look
995 if (index_expr.front() != '-') {
996 error = Status::FromErrorStringWithFormat(
997 "invalid range expression \"'%s'\"",
998 original_index_expr.str().c_str());
999 return ValueObjectSP();
1002 index_expr = index_expr.drop_front();
1003 long final_index = 0;
1004 if (index_expr.getAsInteger(0, final_index)) {
1005 error = Status::FromErrorStringWithFormat(
1006 "invalid range expression \"'%s'\"",
1007 original_index_expr.str().c_str());
1008 return ValueObjectSP();
1011 // if the format given is [high-low], swap range
1012 if (child_index > final_index) {
1013 long temp = child_index;
1014 child_index = final_index;
1015 final_index = temp;
1018 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
1019 // what we have is *ptr[low-high]. the most similar C++ syntax is to
1020 // deref ptr and extract bits low thru high out of it. reading array
1021 // items low thru high would be done by saying ptr[low-high], without a
1022 // deref * sign
1023 Status deref_error;
1024 ValueObjectSP temp(valobj_sp->Dereference(deref_error));
1025 if (!temp || deref_error.Fail()) {
1026 valobj_sp->GetExpressionPath(var_expr_path_strm);
1027 error = Status::FromErrorStringWithFormat(
1028 "could not dereference \"(%s) %s\"",
1029 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1030 var_expr_path_strm.GetData());
1031 return ValueObjectSP();
1033 valobj_sp = temp;
1034 deref = false;
1035 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
1036 // what we have is *arr[low-high]. the most similar C++ syntax is to
1037 // get arr[0] (an operation that is equivalent to deref-ing arr) and
1038 // extract bits low thru high out of it. reading array items low thru
1039 // high would be done by saying arr[low-high], without a deref * sign
1040 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0));
1041 if (!temp) {
1042 valobj_sp->GetExpressionPath(var_expr_path_strm);
1043 error = Status::FromErrorStringWithFormat(
1044 "could not get item 0 for \"(%s) %s\"",
1045 valobj_sp->GetTypeName().AsCString("<invalid type>"),
1046 var_expr_path_strm.GetData());
1047 return ValueObjectSP();
1049 valobj_sp = temp;
1050 deref = false;
1053 child_valobj_sp =
1054 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1055 if (!child_valobj_sp) {
1056 valobj_sp->GetExpressionPath(var_expr_path_strm);
1057 error = Status::FromErrorStringWithFormat(
1058 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
1059 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
1060 var_expr_path_strm.GetData());
1063 if (!child_valobj_sp) {
1064 // Invalid bitfield range...
1065 return ValueObjectSP();
1068 if (use_dynamic != eNoDynamicValues) {
1069 ValueObjectSP dynamic_value_sp(
1070 child_valobj_sp->GetDynamicValue(use_dynamic));
1071 if (dynamic_value_sp)
1072 child_valobj_sp = dynamic_value_sp;
1074 // Break out early from the switch since we were able to find the child
1075 // member
1076 break;
1078 default:
1079 // Failure...
1081 valobj_sp->GetExpressionPath(var_expr_path_strm);
1082 error = Status::FromErrorStringWithFormat(
1083 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1084 separator_type, var_expr_path_strm.GetData(),
1085 var_expr.str().c_str());
1087 return ValueObjectSP();
1091 if (child_valobj_sp)
1092 valobj_sp = child_valobj_sp;
1094 if (valobj_sp) {
1095 if (deref) {
1096 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1097 valobj_sp = deref_valobj_sp;
1098 } else if (address_of) {
1099 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1100 valobj_sp = address_of_valobj_sp;
1103 return valobj_sp;
1106 llvm::Error StackFrame::GetFrameBaseValue(Scalar &frame_base) {
1107 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1108 if (!m_cfa_is_valid) {
1109 m_frame_base_error = Status::FromErrorString(
1110 "No frame base available for this historical stack frame.");
1111 return m_frame_base_error.ToError();
1114 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1115 if (m_sc.function) {
1116 m_frame_base.Clear();
1117 m_frame_base_error.Clear();
1119 m_flags.Set(GOT_FRAME_BASE);
1120 ExecutionContext exe_ctx(shared_from_this());
1121 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1122 if (!m_sc.function->GetFrameBaseExpression().IsAlwaysValidSingleExpr())
1123 loclist_base_addr =
1124 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1125 exe_ctx.GetTargetPtr());
1127 llvm::Expected<Value> expr_value =
1128 m_sc.function->GetFrameBaseExpression().Evaluate(
1129 &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr);
1130 if (!expr_value)
1131 m_frame_base_error = Status::FromError(expr_value.takeError());
1132 else
1133 m_frame_base = expr_value->ResolveValue(&exe_ctx);
1134 } else {
1135 m_frame_base_error =
1136 Status::FromErrorString("No function in symbol context.");
1140 if (m_frame_base_error.Fail())
1141 return m_frame_base_error.ToError();
1143 frame_base = m_frame_base;
1144 return llvm::Error::success();
1147 DWARFExpressionList *StackFrame::GetFrameBaseExpression(Status *error_ptr) {
1148 if (!m_sc.function) {
1149 if (error_ptr) {
1150 *error_ptr = Status::FromErrorString("No function in symbol context.");
1152 return nullptr;
1155 return &m_sc.function->GetFrameBaseExpression();
1158 RegisterContextSP StackFrame::GetRegisterContext() {
1159 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1160 if (!m_reg_context_sp) {
1161 ThreadSP thread_sp(GetThread());
1162 if (thread_sp)
1163 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1165 return m_reg_context_sp;
1168 bool StackFrame::HasDebugInformation() {
1169 GetSymbolContext(eSymbolContextLineEntry);
1170 return m_sc.line_entry.IsValid();
1173 ValueObjectSP
1174 StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1175 DynamicValueType use_dynamic) {
1176 ValueObjectSP valobj_sp;
1177 { // Scope for stack frame mutex. We need to drop this mutex before we figure
1178 // out the dynamic value. That will require converting the StackID in the
1179 // VO back to a StackFrame, which will in turn require locking the
1180 // StackFrameList. If we still hold the StackFrame mutex, we could suffer
1181 // lock inversion against the pattern of getting the StackFrameList and
1182 // then the stack frame, which is fairly common.
1183 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1184 if (IsHistorical()) {
1185 return valobj_sp;
1187 VariableList *var_list = GetVariableList(true, nullptr);
1188 if (var_list) {
1189 // Make sure the variable is a frame variable
1190 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1191 const uint32_t num_variables = var_list->GetSize();
1192 if (var_idx < num_variables) {
1193 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1194 if (!valobj_sp) {
1195 if (m_variable_list_value_objects.GetSize() < num_variables)
1196 m_variable_list_value_objects.Resize(num_variables);
1197 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1198 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx,
1199 valobj_sp);
1203 } // End of StackFrame mutex scope.
1204 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1205 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1206 if (dynamic_sp)
1207 return dynamic_sp;
1209 return valobj_sp;
1212 bool StackFrame::IsInlined() {
1213 if (m_sc.block == nullptr)
1214 GetSymbolContext(eSymbolContextBlock);
1215 if (m_sc.block)
1216 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1217 return false;
1220 bool StackFrame::IsHistorical() const {
1221 return m_stack_frame_kind == StackFrame::Kind::History;
1224 bool StackFrame::IsArtificial() const {
1225 return m_stack_frame_kind == StackFrame::Kind::Artificial;
1228 bool StackFrame::IsHidden() {
1229 if (auto recognized_frame_sp = GetRecognizedFrame())
1230 return recognized_frame_sp->ShouldHide();
1231 return false;
1234 StructuredData::ObjectSP StackFrame::GetLanguageSpecificData() {
1235 auto process_sp = CalculateProcess();
1236 SourceLanguage language = GetLanguage();
1237 if (!language)
1238 return {};
1239 if (auto runtime_sp =
1240 process_sp->GetLanguageRuntime(language.AsLanguageType()))
1241 return runtime_sp->GetLanguageSpecificData(
1242 GetSymbolContext(eSymbolContextFunction));
1243 return {};
1246 const char *StackFrame::GetFunctionName() {
1247 const char *name = nullptr;
1248 SymbolContext sc = GetSymbolContext(
1249 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
1250 if (sc.block) {
1251 Block *inlined_block = sc.block->GetContainingInlinedBlock();
1252 if (inlined_block) {
1253 const InlineFunctionInfo *inlined_info =
1254 inlined_block->GetInlinedFunctionInfo();
1255 if (inlined_info)
1256 name = inlined_info->GetName().AsCString();
1260 if (name == nullptr) {
1261 if (sc.function)
1262 name = sc.function->GetName().GetCString();
1265 if (name == nullptr) {
1266 if (sc.symbol)
1267 name = sc.symbol->GetName().GetCString();
1270 return name;
1273 const char *StackFrame::GetDisplayFunctionName() {
1274 const char *name = nullptr;
1275 SymbolContext sc = GetSymbolContext(
1276 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
1277 if (sc.block) {
1278 Block *inlined_block = sc.block->GetContainingInlinedBlock();
1279 if (inlined_block) {
1280 const InlineFunctionInfo *inlined_info =
1281 inlined_block->GetInlinedFunctionInfo();
1282 if (inlined_info)
1283 name = inlined_info->GetDisplayName().AsCString();
1287 if (name == nullptr) {
1288 if (sc.function)
1289 name = sc.function->GetDisplayName().GetCString();
1292 if (name == nullptr) {
1293 if (sc.symbol)
1294 name = sc.symbol->GetDisplayName().GetCString();
1296 return name;
1299 SourceLanguage StackFrame::GetLanguage() {
1300 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1301 if (cu)
1302 return cu->GetLanguage();
1303 return {};
1306 SourceLanguage StackFrame::GuessLanguage() {
1307 SourceLanguage lang_type = GetLanguage();
1309 if (lang_type == eLanguageTypeUnknown) {
1310 SymbolContext sc =
1311 GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol);
1312 if (sc.function)
1313 lang_type = LanguageType(sc.function->GetMangled().GuessLanguage());
1314 else if (sc.symbol)
1315 lang_type = SourceLanguage(sc.symbol->GetMangled().GuessLanguage());
1318 return lang_type;
1321 namespace {
1322 std::pair<const Instruction::Operand *, int64_t>
1323 GetBaseExplainingValue(const Instruction::Operand &operand,
1324 RegisterContext &register_context, lldb::addr_t value) {
1325 switch (operand.m_type) {
1326 case Instruction::Operand::Type::Dereference:
1327 case Instruction::Operand::Type::Immediate:
1328 case Instruction::Operand::Type::Invalid:
1329 case Instruction::Operand::Type::Product:
1330 // These are not currently interesting
1331 return std::make_pair(nullptr, 0);
1332 case Instruction::Operand::Type::Sum: {
1333 const Instruction::Operand *immediate_child = nullptr;
1334 const Instruction::Operand *variable_child = nullptr;
1335 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1336 immediate_child = &operand.m_children[0];
1337 variable_child = &operand.m_children[1];
1338 } else if (operand.m_children[1].m_type ==
1339 Instruction::Operand::Type::Immediate) {
1340 immediate_child = &operand.m_children[1];
1341 variable_child = &operand.m_children[0];
1343 if (!immediate_child) {
1344 return std::make_pair(nullptr, 0);
1346 lldb::addr_t adjusted_value = value;
1347 if (immediate_child->m_negative) {
1348 adjusted_value += immediate_child->m_immediate;
1349 } else {
1350 adjusted_value -= immediate_child->m_immediate;
1352 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1353 GetBaseExplainingValue(*variable_child, register_context,
1354 adjusted_value);
1355 if (!base_and_offset.first) {
1356 return std::make_pair(nullptr, 0);
1358 if (immediate_child->m_negative) {
1359 base_and_offset.second -= immediate_child->m_immediate;
1360 } else {
1361 base_and_offset.second += immediate_child->m_immediate;
1363 return base_and_offset;
1365 case Instruction::Operand::Type::Register: {
1366 const RegisterInfo *info =
1367 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1368 if (!info) {
1369 return std::make_pair(nullptr, 0);
1371 RegisterValue reg_value;
1372 if (!register_context.ReadRegister(info, reg_value)) {
1373 return std::make_pair(nullptr, 0);
1375 if (reg_value.GetAsUInt64() == value) {
1376 return std::make_pair(&operand, 0);
1377 } else {
1378 return std::make_pair(nullptr, 0);
1382 return std::make_pair(nullptr, 0);
1385 std::pair<const Instruction::Operand *, int64_t>
1386 GetBaseExplainingDereference(const Instruction::Operand &operand,
1387 RegisterContext &register_context,
1388 lldb::addr_t addr) {
1389 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1390 return GetBaseExplainingValue(operand.m_children[0], register_context,
1391 addr);
1393 return std::make_pair(nullptr, 0);
1395 } // namespace
1397 lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1398 TargetSP target_sp = CalculateTarget();
1400 const ArchSpec &target_arch = target_sp->GetArchitecture();
1402 AddressRange pc_range;
1403 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1404 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1406 const char *plugin_name = nullptr;
1407 const char *flavor = nullptr;
1408 const char *cpu = nullptr;
1409 const char *features = nullptr;
1410 const bool force_live_memory = true;
1412 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1413 target_arch, plugin_name, flavor, cpu, features, *target_sp, pc_range,
1414 force_live_memory);
1416 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1417 return ValueObjectSP();
1420 InstructionSP instruction_sp =
1421 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1423 llvm::SmallVector<Instruction::Operand, 3> operands;
1425 if (!instruction_sp->ParseOperands(operands)) {
1426 return ValueObjectSP();
1429 RegisterContextSP register_context_sp = GetRegisterContext();
1431 if (!register_context_sp) {
1432 return ValueObjectSP();
1435 for (const Instruction::Operand &operand : operands) {
1436 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1437 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1439 if (!base_and_offset.first) {
1440 continue;
1443 switch (base_and_offset.first->m_type) {
1444 case Instruction::Operand::Type::Immediate: {
1445 lldb_private::Address addr;
1446 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1447 base_and_offset.second,
1448 addr)) {
1449 auto c_type_system_or_err =
1450 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
1451 if (auto err = c_type_system_or_err.takeError()) {
1452 LLDB_LOG_ERROR(GetLog(LLDBLog::Thread), std::move(err),
1453 "Unable to guess value for given address: {0}");
1454 return ValueObjectSP();
1455 } else {
1456 auto ts = *c_type_system_or_err;
1457 if (!ts)
1458 return {};
1459 CompilerType void_ptr_type =
1460 ts->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1461 .GetPointerType();
1462 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
1464 } else {
1465 return ValueObjectSP();
1467 break;
1469 case Instruction::Operand::Type::Register: {
1470 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1471 base_and_offset.second);
1473 default:
1474 return ValueObjectSP();
1478 return ValueObjectSP();
1481 namespace {
1482 ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1483 int64_t offset) {
1484 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1485 return ValueObjectSP();
1488 if (parent->IsPointerOrReferenceType()) {
1489 return parent;
1492 for (int ci = 0, ce = parent->GetNumChildrenIgnoringErrors(); ci != ce;
1493 ++ci) {
1494 ValueObjectSP child_sp = parent->GetChildAtIndex(ci);
1496 if (!child_sp) {
1497 return ValueObjectSP();
1500 int64_t child_offset = child_sp->GetByteOffset();
1501 int64_t child_size = child_sp->GetByteSize().value_or(0);
1503 if (offset >= child_offset && offset < (child_offset + child_size)) {
1504 return GetValueForOffset(frame, child_sp, offset - child_offset);
1508 if (offset == 0) {
1509 return parent;
1510 } else {
1511 return ValueObjectSP();
1515 ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1516 ValueObjectSP &base,
1517 int64_t offset) {
1518 // base is a pointer to something
1519 // offset is the thing to add to the pointer We return the most sensible
1520 // ValueObject for the result of *(base+offset)
1522 if (!base->IsPointerOrReferenceType()) {
1523 return ValueObjectSP();
1526 Status error;
1527 ValueObjectSP pointee = base->Dereference(error);
1529 if (!pointee) {
1530 return ValueObjectSP();
1533 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
1534 int64_t index = offset / pointee->GetByteSize().value_or(1);
1535 offset = offset % pointee->GetByteSize().value_or(1);
1536 const bool can_create = true;
1537 pointee = base->GetSyntheticArrayMember(index, can_create);
1540 if (!pointee || error.Fail()) {
1541 return ValueObjectSP();
1544 return GetValueForOffset(frame, pointee, offset);
1547 /// Attempt to reconstruct the ValueObject for the address contained in a
1548 /// given register plus an offset.
1550 /// \param [in] frame
1551 /// The current stack frame.
1553 /// \param [in] reg
1554 /// The register.
1556 /// \param [in] offset
1557 /// The offset from the register.
1559 /// \param [in] disassembler
1560 /// A disassembler containing instructions valid up to the current PC.
1562 /// \param [in] variables
1563 /// The variable list from the current frame,
1565 /// \param [in] pc
1566 /// The program counter for the instruction considered the 'user'.
1568 /// \return
1569 /// A string describing the base for the ExpressionPath. This could be a
1570 /// variable, a register value, an argument, or a function return value.
1571 /// The ValueObject if found. If valid, it has a valid ExpressionPath.
1572 lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1573 int64_t offset, Disassembler &disassembler,
1574 VariableList &variables, const Address &pc) {
1575 // Example of operation for Intel:
1577 // +14: movq -0x8(%rbp), %rdi
1578 // +18: movq 0x8(%rdi), %rdi
1579 // +22: addl 0x4(%rdi), %eax
1581 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1583 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at
1584 // +18 that assigns to rdi, and calls itself recursively for that dereference
1585 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1586 // +14 that assigns to rdi, and calls itself recursively for that
1587 // dereference
1588 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1589 // variable list.
1590 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1591 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1592 // at +18)
1593 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1594 // rdi+4 at +22)
1596 // First, check the variable list to see if anything is at the specified
1597 // location.
1599 using namespace OperandMatchers;
1601 const RegisterInfo *reg_info =
1602 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1603 if (!reg_info) {
1604 return ValueObjectSP();
1607 Instruction::Operand op =
1608 offset ? Instruction::Operand::BuildDereference(
1609 Instruction::Operand::BuildSum(
1610 Instruction::Operand::BuildRegister(reg),
1611 Instruction::Operand::BuildImmediate(offset)))
1612 : Instruction::Operand::BuildDereference(
1613 Instruction::Operand::BuildRegister(reg));
1615 for (VariableSP var_sp : variables) {
1616 if (var_sp->LocationExpressionList().MatchesOperand(frame, op))
1617 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1620 const uint32_t current_inst =
1621 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1622 if (current_inst == UINT32_MAX) {
1623 return ValueObjectSP();
1626 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1627 // This is not an exact algorithm, and it sacrifices accuracy for
1628 // generality. Recognizing "mov" and "ld" instructions –– and which
1629 // are their source and destination operands -- is something the
1630 // disassembler should do for us.
1631 InstructionSP instruction_sp =
1632 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
1634 if (instruction_sp->IsCall()) {
1635 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1636 if (!abi_sp) {
1637 continue;
1640 const char *return_register_name;
1641 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1642 continue;
1645 const RegisterInfo *return_register_info =
1646 frame.GetRegisterContext()->GetRegisterInfoByName(
1647 return_register_name);
1648 if (!return_register_info) {
1649 continue;
1652 int64_t offset = 0;
1654 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1655 MatchRegOp(*return_register_info))(op) &&
1656 !MatchUnaryOp(
1657 MatchOpType(Instruction::Operand::Type::Dereference),
1658 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1659 MatchRegOp(*return_register_info),
1660 FetchImmOp(offset)))(op)) {
1661 continue;
1664 llvm::SmallVector<Instruction::Operand, 1> operands;
1665 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1666 continue;
1669 switch (operands[0].m_type) {
1670 default:
1671 break;
1672 case Instruction::Operand::Type::Immediate: {
1673 SymbolContext sc;
1674 Address load_address;
1675 if (!frame.CalculateTarget()->ResolveLoadAddress(
1676 operands[0].m_immediate, load_address)) {
1677 break;
1679 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1680 load_address, eSymbolContextFunction, sc);
1681 if (!sc.function) {
1682 break;
1684 CompilerType function_type = sc.function->GetCompilerType();
1685 if (!function_type.IsFunctionType()) {
1686 break;
1688 CompilerType return_type = function_type.GetFunctionReturnType();
1689 RegisterValue return_value;
1690 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
1691 return_value)) {
1692 break;
1694 std::string name_str(
1695 sc.function->GetName().AsCString("<unknown function>"));
1696 name_str.append("()");
1697 Address return_value_address(return_value.GetAsUInt64());
1698 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
1699 &frame, name_str, return_value_address, return_type);
1700 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1704 continue;
1707 llvm::SmallVector<Instruction::Operand, 2> operands;
1708 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1709 continue;
1712 Instruction::Operand *origin_operand = nullptr;
1713 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1714 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1717 if (clobbered_reg_matcher(operands[0])) {
1718 origin_operand = &operands[1];
1720 else if (clobbered_reg_matcher(operands[1])) {
1721 origin_operand = &operands[0];
1723 else {
1724 continue;
1727 // We have an origin operand. Can we track its value down?
1728 ValueObjectSP source_path;
1729 ConstString origin_register;
1730 int64_t origin_offset = 0;
1732 if (FetchRegOp(origin_register)(*origin_operand)) {
1733 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1734 variables, instruction_sp->GetAddress());
1735 } else if (MatchUnaryOp(
1736 MatchOpType(Instruction::Operand::Type::Dereference),
1737 FetchRegOp(origin_register))(*origin_operand) ||
1738 MatchUnaryOp(
1739 MatchOpType(Instruction::Operand::Type::Dereference),
1740 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1741 FetchRegOp(origin_register),
1742 FetchImmOp(origin_offset)))(*origin_operand)) {
1743 source_path =
1744 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
1745 variables, instruction_sp->GetAddress());
1746 if (!source_path) {
1747 continue;
1749 source_path =
1750 GetValueForDereferincingOffset(frame, source_path, offset);
1753 if (source_path) {
1754 return source_path;
1758 return ValueObjectSP();
1762 lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1763 int64_t offset) {
1764 TargetSP target_sp = CalculateTarget();
1766 const ArchSpec &target_arch = target_sp->GetArchitecture();
1768 Block *frame_block = GetFrameBlock();
1770 if (!frame_block) {
1771 return ValueObjectSP();
1774 Function *function = frame_block->CalculateSymbolContextFunction();
1775 if (!function) {
1776 return ValueObjectSP();
1779 AddressRange pc_range = function->GetAddressRange();
1781 if (GetFrameCodeAddress().GetFileAddress() <
1782 pc_range.GetBaseAddress().GetFileAddress() ||
1783 GetFrameCodeAddress().GetFileAddress() -
1784 pc_range.GetBaseAddress().GetFileAddress() >=
1785 pc_range.GetByteSize()) {
1786 return ValueObjectSP();
1789 const char *plugin_name = nullptr;
1790 const char *flavor = nullptr;
1791 const char *cpu = nullptr;
1792 const char *features = nullptr;
1793 const bool force_live_memory = true;
1794 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1795 target_arch, plugin_name, flavor, cpu, features, *target_sp, pc_range,
1796 force_live_memory);
1798 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1799 return ValueObjectSP();
1802 const bool get_file_globals = false;
1803 VariableList *variables = GetVariableList(get_file_globals, nullptr);
1805 if (!variables) {
1806 return ValueObjectSP();
1809 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1810 GetFrameCodeAddress());
1813 lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) {
1814 ValueObjectSP value_sp;
1816 if (!name)
1817 return value_sp;
1819 TargetSP target_sp = CalculateTarget();
1820 ProcessSP process_sp = CalculateProcess();
1822 if (!target_sp && !process_sp)
1823 return value_sp;
1825 VariableList variable_list;
1826 VariableSP var_sp;
1827 SymbolContext sc(GetSymbolContext(eSymbolContextBlock));
1829 if (sc.block) {
1830 const bool can_create = true;
1831 const bool get_parent_variables = true;
1832 const bool stop_if_block_is_inlined_function = true;
1834 if (sc.block->AppendVariables(
1835 can_create, get_parent_variables, stop_if_block_is_inlined_function,
1836 [this](Variable *v) { return v->IsInScope(this); },
1837 &variable_list)) {
1838 var_sp = variable_list.FindVariable(name);
1841 if (var_sp)
1842 value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1845 return value_sp;
1848 TargetSP StackFrame::CalculateTarget() {
1849 TargetSP target_sp;
1850 ThreadSP thread_sp(GetThread());
1851 if (thread_sp) {
1852 ProcessSP process_sp(thread_sp->CalculateProcess());
1853 if (process_sp)
1854 target_sp = process_sp->CalculateTarget();
1856 return target_sp;
1859 ProcessSP StackFrame::CalculateProcess() {
1860 ProcessSP process_sp;
1861 ThreadSP thread_sp(GetThread());
1862 if (thread_sp)
1863 process_sp = thread_sp->CalculateProcess();
1864 return process_sp;
1867 ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1869 StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1871 void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1872 exe_ctx.SetContext(shared_from_this());
1875 bool StackFrame::DumpUsingFormat(Stream &strm,
1876 const FormatEntity::Entry *format,
1877 llvm::StringRef frame_marker) {
1878 GetSymbolContext(eSymbolContextEverything);
1879 ExecutionContext exe_ctx(shared_from_this());
1880 StreamString s;
1881 s.PutCString(frame_marker);
1883 if (format && FormatEntity::Format(*format, s, &m_sc, &exe_ctx, nullptr,
1884 nullptr, false, false)) {
1885 strm.PutCString(s.GetString());
1886 return true;
1888 return false;
1891 void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique,
1892 const char *frame_marker) {
1893 if (strm == nullptr)
1894 return;
1896 ExecutionContext exe_ctx(shared_from_this());
1898 const FormatEntity::Entry *frame_format = nullptr;
1899 Target *target = exe_ctx.GetTargetPtr();
1900 if (target) {
1901 if (show_unique) {
1902 frame_format = target->GetDebugger().GetFrameFormatUnique();
1903 } else {
1904 frame_format = target->GetDebugger().GetFrameFormat();
1907 if (!DumpUsingFormat(*strm, frame_format, frame_marker)) {
1908 Dump(strm, true, false);
1909 strm->EOL();
1913 void StackFrame::Dump(Stream *strm, bool show_frame_index,
1914 bool show_fullpaths) {
1915 if (strm == nullptr)
1916 return;
1918 if (show_frame_index)
1919 strm->Printf("frame #%u: ", m_frame_index);
1920 ExecutionContext exe_ctx(shared_from_this());
1921 Target *target = exe_ctx.GetTargetPtr();
1922 strm->Printf("0x%0*" PRIx64 " ",
1923 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1924 : 16,
1925 GetFrameCodeAddress().GetLoadAddress(target));
1926 GetSymbolContext(eSymbolContextEverything);
1927 const bool show_module = true;
1928 const bool show_inline = true;
1929 const bool show_function_arguments = true;
1930 const bool show_function_name = true;
1931 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1932 GetFrameCodeAddress(), show_fullpaths, show_module,
1933 show_inline, show_function_arguments,
1934 show_function_name);
1937 void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1938 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1939 assert(GetStackID() ==
1940 prev_frame.GetStackID()); // TODO: remove this after some testing
1941 m_variable_list_sp = prev_frame.m_variable_list_sp;
1942 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
1943 if (!m_disassembly.GetString().empty()) {
1944 m_disassembly.Clear();
1945 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1949 void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1950 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1951 assert(GetStackID() ==
1952 curr_frame.GetStackID()); // TODO: remove this after some testing
1953 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1954 assert(GetThread() == curr_frame.GetThread());
1955 m_frame_index = curr_frame.m_frame_index;
1956 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1957 m_reg_context_sp = curr_frame.m_reg_context_sp;
1958 m_frame_code_addr = curr_frame.m_frame_code_addr;
1959 m_behaves_like_zeroth_frame = curr_frame.m_behaves_like_zeroth_frame;
1960 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1961 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1962 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1963 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1964 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1965 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1966 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1967 m_sc.function == curr_frame.m_sc.function);
1968 m_sc = curr_frame.m_sc;
1969 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1970 m_flags.Set(m_sc.GetResolvedMask());
1971 m_frame_base.Clear();
1972 m_frame_base_error.Clear();
1975 bool StackFrame::HasCachedData() const {
1976 if (m_variable_list_sp)
1977 return true;
1978 if (m_variable_list_value_objects.GetSize() > 0)
1979 return true;
1980 if (!m_disassembly.GetString().empty())
1981 return true;
1982 return false;
1985 bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
1986 bool show_unique, const char *frame_marker) {
1987 if (show_frame_info) {
1988 strm.Indent();
1989 DumpUsingSettingsFormat(&strm, show_unique, frame_marker);
1992 if (show_source) {
1993 ExecutionContext exe_ctx(shared_from_this());
1994 bool have_source = false, have_debuginfo = false;
1995 Debugger::StopDisassemblyType disasm_display =
1996 Debugger::eStopDisassemblyTypeNever;
1997 Target *target = exe_ctx.GetTargetPtr();
1998 if (target) {
1999 Debugger &debugger = target->GetDebugger();
2000 const uint32_t source_lines_before =
2001 debugger.GetStopSourceLineCount(true);
2002 const uint32_t source_lines_after =
2003 debugger.GetStopSourceLineCount(false);
2004 disasm_display = debugger.GetStopDisassemblyDisplay();
2006 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
2007 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
2008 have_debuginfo = true;
2009 if (source_lines_before > 0 || source_lines_after > 0) {
2010 SupportFileSP source_file_sp = m_sc.line_entry.file_sp;
2011 uint32_t start_line = m_sc.line_entry.line;
2012 if (!start_line && m_sc.function) {
2013 m_sc.function->GetStartLineSourceInfo(source_file_sp, start_line);
2016 size_t num_lines =
2017 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
2018 source_file_sp, start_line, m_sc.line_entry.column,
2019 source_lines_before, source_lines_after, "->", &strm);
2020 if (num_lines != 0)
2021 have_source = true;
2022 // TODO: Give here a one time warning if source file is missing.
2023 if (!m_sc.line_entry.line)
2024 strm << "note: This address is not associated with a specific line "
2025 "of code. This may be due to compiler optimizations.\n";
2028 switch (disasm_display) {
2029 case Debugger::eStopDisassemblyTypeNever:
2030 break;
2032 case Debugger::eStopDisassemblyTypeNoDebugInfo:
2033 if (have_debuginfo)
2034 break;
2035 [[fallthrough]];
2037 case Debugger::eStopDisassemblyTypeNoSource:
2038 if (have_source)
2039 break;
2040 [[fallthrough]];
2042 case Debugger::eStopDisassemblyTypeAlways:
2043 if (target) {
2044 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
2045 if (disasm_lines > 0) {
2046 const ArchSpec &target_arch = target->GetArchitecture();
2047 const char *plugin_name = nullptr;
2048 const char *flavor = nullptr;
2049 const bool mixed_source_and_assembly = false;
2050 Disassembler::Disassemble(
2051 target->GetDebugger(), target_arch, plugin_name, flavor,
2052 target->GetDisassemblyCPU(), target->GetDisassemblyFeatures(),
2053 exe_ctx, GetFrameCodeAddress(),
2054 {Disassembler::Limit::Instructions, disasm_lines},
2055 mixed_source_and_assembly, 0,
2056 Disassembler::eOptionMarkPCAddress, strm);
2059 break;
2063 return true;
2066 RecognizedStackFrameSP StackFrame::GetRecognizedFrame() {
2067 auto process = GetThread()->GetProcess();
2068 if (!process)
2069 return {};
2070 // If recognizer list has been modified, discard cache.
2071 auto &manager = process->GetTarget().GetFrameRecognizerManager();
2072 auto new_generation = manager.GetGeneration();
2073 if (m_frame_recognizer_generation != new_generation)
2074 m_recognized_frame_sp.reset();
2075 m_frame_recognizer_generation = new_generation;
2076 if (!m_recognized_frame_sp.has_value())
2077 m_recognized_frame_sp = manager.RecognizeFrame(CalculateStackFrame());
2078 return m_recognized_frame_sp.value();