1 //===-- StackFrame.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/StackFrame.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Core/Disassembler.h"
13 #include "lldb/Core/FormatEntity.h"
14 #include "lldb/Core/Mangled.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/Value.h"
17 #include "lldb/Core/ValueObjectConstResult.h"
18 #include "lldb/Core/ValueObjectMemory.h"
19 #include "lldb/Core/ValueObjectVariable.h"
20 #include "lldb/Symbol/CompileUnit.h"
21 #include "lldb/Symbol/Function.h"
22 #include "lldb/Symbol/Symbol.h"
23 #include "lldb/Symbol/SymbolContextScope.h"
24 #include "lldb/Symbol/Type.h"
25 #include "lldb/Symbol/VariableList.h"
26 #include "lldb/Target/ABI.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/RegisterContext.h"
30 #include "lldb/Target/StackFrameRecognizer.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Thread.h"
33 #include "lldb/Utility/RegisterValue.h"
35 #include "lldb/lldb-enumerations.h"
38 using namespace lldb_private;
40 // The first bits in the flags are reserved for the SymbolContext::Scope bits
41 // so we know if we have tried to look up information in our internal symbol
42 // context (m_sc) already.
43 #define RESOLVED_FRAME_CODE_ADDR (uint32_t(eSymbolContextEverything + 1))
44 #define RESOLVED_FRAME_ID_SYMBOL_SCOPE (RESOLVED_FRAME_CODE_ADDR << 1)
45 #define GOT_FRAME_BASE (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
46 #define RESOLVED_VARIABLES (GOT_FRAME_BASE << 1)
47 #define RESOLVED_GLOBAL_VARIABLES (RESOLVED_VARIABLES << 1)
49 StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
50 user_id_t unwind_frame_index, addr_t cfa,
51 bool cfa_is_valid, addr_t pc, StackFrame::Kind kind,
52 const SymbolContext *sc_ptr)
53 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
54 m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
55 m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
56 m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
57 m_stack_frame_kind(kind), m_variable_list_sp(),
58 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
60 // If we don't have a CFA value, use the frame index for our StackID so that
61 // recursive functions properly aren't confused with one another on a history
63 if (IsHistorical() && !m_cfa_is_valid) {
64 m_id.SetCFA(m_frame_index);
67 if (sc_ptr != nullptr) {
69 m_flags.Set(m_sc.GetResolvedMask());
73 StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
74 user_id_t unwind_frame_index,
75 const RegisterContextSP ®_context_sp, addr_t cfa,
76 addr_t pc, const SymbolContext *sc_ptr)
77 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
78 m_concrete_frame_index(unwind_frame_index),
79 m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
80 m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
81 m_frame_base_error(), m_cfa_is_valid(true),
82 m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(),
83 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
85 if (sc_ptr != nullptr) {
87 m_flags.Set(m_sc.GetResolvedMask());
90 if (reg_context_sp && !m_sc.target_sp) {
91 m_sc.target_sp = reg_context_sp->CalculateTarget();
93 m_flags.Set(eSymbolContextTarget);
97 StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
98 user_id_t unwind_frame_index,
99 const RegisterContextSP ®_context_sp, addr_t cfa,
100 const Address &pc_addr, const SymbolContext *sc_ptr)
101 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
102 m_concrete_frame_index(unwind_frame_index),
103 m_reg_context_sp(reg_context_sp),
104 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
106 m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
107 m_frame_base_error(), m_cfa_is_valid(true),
108 m_stack_frame_kind(StackFrame::Kind::Regular), m_variable_list_sp(),
109 m_variable_list_value_objects(), m_recognized_frame_sp(), m_disassembly(),
111 if (sc_ptr != nullptr) {
113 m_flags.Set(m_sc.GetResolvedMask());
116 if (!m_sc.target_sp && reg_context_sp) {
117 m_sc.target_sp = reg_context_sp->CalculateTarget();
119 m_flags.Set(eSymbolContextTarget);
122 ModuleSP pc_module_sp(pc_addr.GetModule());
123 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
125 m_sc.module_sp = pc_module_sp;
126 m_flags.Set(eSymbolContextModule);
128 m_sc.module_sp.reset();
133 StackFrame::~StackFrame() = default;
135 StackID &StackFrame::GetStackID() {
136 std::lock_guard<std::recursive_mutex> guard(m_mutex);
137 // Make sure we have resolved the StackID object's symbol context scope if we
138 // already haven't looked it up.
140 if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
141 if (m_id.GetSymbolContextScope()) {
142 // We already have a symbol context scope, we just don't have our flag
144 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
146 // Calculate the frame block and use this for the stack ID symbol context
147 // scope if we have one.
148 SymbolContextScope *scope = GetFrameBlock();
149 if (scope == nullptr) {
150 // We don't have a block, so use the symbol
151 if (m_flags.IsClear(eSymbolContextSymbol))
152 GetSymbolContext(eSymbolContextSymbol);
154 // It is ok if m_sc.symbol is nullptr here
157 // Set the symbol context scope (the accessor will set the
158 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
159 SetSymbolContextScope(scope);
165 uint32_t StackFrame::GetFrameIndex() const {
166 ThreadSP thread_sp = GetThread();
168 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
171 return m_frame_index;
174 void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
175 std::lock_guard<std::recursive_mutex> guard(m_mutex);
176 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
177 m_id.SetSymbolContextScope(symbol_scope);
180 const Address &StackFrame::GetFrameCodeAddress() {
181 std::lock_guard<std::recursive_mutex> guard(m_mutex);
182 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
183 !m_frame_code_addr.IsSectionOffset()) {
184 m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
186 // Resolve the PC into a temporary address because if ResolveLoadAddress
187 // fails to resolve the address, it will clear the address object...
188 ThreadSP thread_sp(GetThread());
190 TargetSP target_sp(thread_sp->CalculateTarget());
192 const bool allow_section_end = true;
193 if (m_frame_code_addr.SetOpcodeLoadAddress(
194 m_frame_code_addr.GetOffset(), target_sp.get(),
195 AddressClass::eCode, allow_section_end)) {
196 ModuleSP module_sp(m_frame_code_addr.GetModule());
198 m_sc.module_sp = module_sp;
199 m_flags.Set(eSymbolContextModule);
205 return m_frame_code_addr;
208 bool StackFrame::ChangePC(addr_t pc) {
209 std::lock_guard<std::recursive_mutex> guard(m_mutex);
210 // We can't change the pc value of a history stack frame - it is immutable.
213 m_frame_code_addr.SetRawAddress(pc);
216 ThreadSP thread_sp(GetThread());
218 thread_sp->ClearStackFrames();
222 const char *StackFrame::Disassemble() {
223 std::lock_guard<std::recursive_mutex> guard(m_mutex);
224 if (m_disassembly.Empty()) {
225 ExecutionContext exe_ctx(shared_from_this());
226 Target *target = exe_ctx.GetTargetPtr();
228 const char *plugin_name = nullptr;
229 const char *flavor = nullptr;
230 Disassembler::Disassemble(target->GetDebugger(),
231 target->GetArchitecture(), plugin_name, flavor,
232 exe_ctx, 0, false, 0, 0, m_disassembly);
234 if (m_disassembly.Empty())
238 return m_disassembly.GetData();
241 Block *StackFrame::GetFrameBlock() {
242 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
243 GetSymbolContext(eSymbolContextBlock);
246 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
248 // Use the block with the inlined function info as the frame block we
249 // want this frame to have only the variables for the inlined function
250 // and its non-inlined block child blocks.
253 // This block is not contained within any inlined function blocks with so
254 // we want to use the top most function block.
255 return &m_sc.function->GetBlock(false);
261 //----------------------------------------------------------------------
262 // Get the symbol context if we already haven't done so by resolving the
263 // PC address as much as possible. This way when we pass around a
264 // StackFrame object, everyone will have as much information as possible and no
265 // one will ever have to look things up manually.
266 //----------------------------------------------------------------------
267 const SymbolContext &
268 StackFrame::GetSymbolContext(SymbolContextItem resolve_scope) {
269 std::lock_guard<std::recursive_mutex> guard(m_mutex);
270 // Copy our internal symbol context into "sc".
271 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
272 uint32_t resolved = 0;
274 // If the target was requested add that:
275 if (!m_sc.target_sp) {
276 m_sc.target_sp = CalculateTarget();
278 resolved |= eSymbolContextTarget;
281 // Resolve our PC to section offset if we haven't already done so and if we
282 // don't have a module. The resolved address section will contain the
283 // module to which it belongs
284 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
285 GetFrameCodeAddress();
287 // If this is not frame zero, then we need to subtract 1 from the PC value
288 // when doing address lookups since the PC will be on the instruction
289 // following the function call instruction...
291 Address lookup_addr(GetFrameCodeAddress());
292 if (m_frame_index > 0 && lookup_addr.IsValid()) {
293 addr_t offset = lookup_addr.GetOffset();
295 lookup_addr.SetOffset(offset - 1);
298 // lookup_addr is the start of a section. We need do the math on the
299 // actual load address and re-compute the section. We're working with
300 // a 'noreturn' function at the end of a section.
301 ThreadSP thread_sp(GetThread());
303 TargetSP target_sp(thread_sp->CalculateTarget());
305 addr_t addr_minus_one =
306 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
307 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
309 lookup_addr.SetOffset(offset - 1);
315 if (m_sc.module_sp) {
316 // We have something in our stack frame symbol context, lets check if we
317 // haven't already tried to lookup one of those things. If we haven't
318 // then we will do the query.
320 SymbolContextItem actual_resolve_scope = SymbolContextItem(0);
322 if (resolve_scope & eSymbolContextCompUnit) {
323 if (m_flags.IsClear(eSymbolContextCompUnit)) {
325 resolved |= eSymbolContextCompUnit;
327 actual_resolve_scope |= eSymbolContextCompUnit;
331 if (resolve_scope & eSymbolContextFunction) {
332 if (m_flags.IsClear(eSymbolContextFunction)) {
334 resolved |= eSymbolContextFunction;
336 actual_resolve_scope |= eSymbolContextFunction;
340 if (resolve_scope & eSymbolContextBlock) {
341 if (m_flags.IsClear(eSymbolContextBlock)) {
343 resolved |= eSymbolContextBlock;
345 actual_resolve_scope |= eSymbolContextBlock;
349 if (resolve_scope & eSymbolContextSymbol) {
350 if (m_flags.IsClear(eSymbolContextSymbol)) {
352 resolved |= eSymbolContextSymbol;
354 actual_resolve_scope |= eSymbolContextSymbol;
358 if (resolve_scope & eSymbolContextLineEntry) {
359 if (m_flags.IsClear(eSymbolContextLineEntry)) {
360 if (m_sc.line_entry.IsValid())
361 resolved |= eSymbolContextLineEntry;
363 actual_resolve_scope |= eSymbolContextLineEntry;
367 if (actual_resolve_scope) {
368 // We might be resolving less information than what is already in our
369 // current symbol context so resolve into a temporary symbol context
370 // "sc" so we don't clear out data we have already found in "m_sc"
372 // Set flags that indicate what we have tried to resolve
373 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
374 lookup_addr, actual_resolve_scope, sc);
375 // Only replace what we didn't already have as we may have information
376 // for an inlined function scope that won't match what a standard
377 // lookup by address would match
378 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
379 m_sc.comp_unit = sc.comp_unit;
380 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
381 m_sc.function = sc.function;
382 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
383 m_sc.block = sc.block;
384 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
385 m_sc.symbol = sc.symbol;
386 if ((resolved & eSymbolContextLineEntry) &&
387 !m_sc.line_entry.IsValid()) {
388 m_sc.line_entry = sc.line_entry;
389 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
393 // If we don't have a module, then we can't have the compile unit,
394 // function, block, line entry or symbol, so we can safely call
395 // ResolveSymbolContextForAddress with our symbol context member m_sc.
396 if (m_sc.target_sp) {
397 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
398 lookup_addr, resolve_scope, m_sc);
402 // Update our internal flags so we remember what we have tried to locate so
403 // we don't have to keep trying when more calls to this function are made.
404 // We might have dug up more information that was requested (for example if
405 // we were asked to only get the block, we will have gotten the compile
406 // unit, and function) so set any additional bits that we resolved
407 m_flags.Set(resolve_scope | resolved);
410 // Return the symbol context with everything that was possible to resolve
415 VariableList *StackFrame::GetVariableList(bool get_file_globals) {
416 std::lock_guard<std::recursive_mutex> guard(m_mutex);
417 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
418 m_flags.Set(RESOLVED_VARIABLES);
420 Block *frame_block = GetFrameBlock();
423 const bool get_child_variables = true;
424 const bool can_create = true;
425 const bool stop_if_child_block_is_inlined_function = true;
426 m_variable_list_sp.reset(new VariableList());
427 frame_block->AppendBlockVariables(can_create, get_child_variables,
428 stop_if_child_block_is_inlined_function,
429 [](Variable *v) { return true; },
430 m_variable_list_sp.get());
434 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
435 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
437 if (m_flags.IsClear(eSymbolContextCompUnit))
438 GetSymbolContext(eSymbolContextCompUnit);
440 if (m_sc.comp_unit) {
441 VariableListSP global_variable_list_sp(
442 m_sc.comp_unit->GetVariableList(true));
443 if (m_variable_list_sp)
444 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
446 m_variable_list_sp = global_variable_list_sp;
450 return m_variable_list_sp.get();
454 StackFrame::GetInScopeVariableList(bool get_file_globals,
455 bool must_have_valid_location) {
456 std::lock_guard<std::recursive_mutex> guard(m_mutex);
457 // We can't fetch variable information for a history stack frame.
459 return VariableListSP();
461 VariableListSP var_list_sp(new VariableList);
462 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
465 const bool can_create = true;
466 const bool get_parent_variables = true;
467 const bool stop_if_block_is_inlined_function = true;
468 m_sc.block->AppendVariables(
469 can_create, get_parent_variables, stop_if_block_is_inlined_function,
470 [this, must_have_valid_location](Variable *v) {
471 return v->IsInScope(this) && (!must_have_valid_location ||
472 v->LocationIsValidForFrame(this));
477 if (m_sc.comp_unit && get_file_globals) {
478 VariableListSP global_variable_list_sp(
479 m_sc.comp_unit->GetVariableList(true));
480 if (global_variable_list_sp)
481 var_list_sp->AddVariables(global_variable_list_sp.get());
487 ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
488 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
489 VariableSP &var_sp, Status &error) {
490 llvm::StringRef original_var_expr = var_expr;
491 // We can't fetch variable information for a history stack frame.
493 return ValueObjectSP();
495 if (var_expr.empty()) {
496 error.SetErrorStringWithFormat("invalid variable path '%s'",
497 var_expr.str().c_str());
498 return ValueObjectSP();
501 const bool check_ptr_vs_member =
502 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
503 const bool no_fragile_ivar =
504 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
505 const bool no_synth_child =
506 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
507 // const bool no_synth_array = (options &
508 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
511 bool address_of = false;
512 ValueObjectSP valobj_sp;
513 const bool get_file_globals = true;
514 // When looking up a variable for an expression, we need only consider the
515 // variables that are in scope.
516 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
517 VariableList *variable_list = var_list_sp.get();
520 return ValueObjectSP();
522 // If first character is a '*', then show pointer contents
523 std::string var_expr_storage;
524 if (var_expr[0] == '*') {
526 var_expr = var_expr.drop_front(); // Skip the '*'
527 } else if (var_expr[0] == '&') {
529 var_expr = var_expr.drop_front(); // Skip the '&'
532 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
533 StreamString var_expr_path_strm;
535 ConstString name_const_string(var_expr.substr(0, separator_idx));
537 var_sp = variable_list->FindVariable(name_const_string, false);
539 bool synthetically_added_instance_object = false;
542 var_expr = var_expr.drop_front(name_const_string.GetLength());
545 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
546 // Check for direct ivars access which helps us with implicit access to
547 // ivars with the "this->" or "self->"
548 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
549 lldb::LanguageType method_language = eLanguageTypeUnknown;
550 bool is_instance_method = false;
551 ConstString method_object_name;
552 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
553 method_object_name)) {
554 if (is_instance_method && method_object_name) {
555 var_sp = variable_list->FindVariable(method_object_name);
558 var_expr_storage = "->";
559 var_expr_storage += var_expr;
560 var_expr = var_expr_storage;
561 synthetically_added_instance_object = true;
567 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
568 // Check if any anonymous unions are there which contain a variable with
570 for (size_t i = 0; i < variable_list->GetSize(); i++) {
571 VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
574 if (!variable_sp->GetName().IsEmpty())
577 Type *var_type = variable_sp->GetType();
581 if (!var_type->GetForwardCompilerType().IsAnonymousType())
583 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
586 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
592 if (var_sp && !valobj_sp) {
593 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
598 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
599 name_const_string.GetCString());
600 return ValueObjectSP();
603 // We are dumping at least one child
604 while (separator_idx != std::string::npos) {
605 // Calculate the next separator index ahead of time
606 ValueObjectSP child_valobj_sp;
607 const char separator_type = var_expr[0];
608 bool expr_is_ptr = false;
609 switch (separator_type) {
612 if (var_expr.size() >= 2 && var_expr[1] != '>')
613 return ValueObjectSP();
615 if (no_fragile_ivar) {
616 // Make sure we aren't trying to deref an objective
617 // C ivar if this is not allowed
618 const uint32_t pointer_type_flags =
619 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
620 if ((pointer_type_flags & eTypeIsObjC) &&
621 (pointer_type_flags & eTypeIsPointer)) {
622 // This was an objective C object pointer and it was requested we
623 // skip any fragile ivars so return nothing here
624 return ValueObjectSP();
628 // If we have a non pointer type with a sythetic value then lets check if
629 // we have an sythetic dereference specified.
630 if (!valobj_sp->IsPointerType() && valobj_sp->HasSyntheticValue()) {
632 if (valobj_sp->GetCompilerType().IsReferenceType()) {
633 valobj_sp = valobj_sp->GetSyntheticValue()->Dereference(deref_error);
635 error.SetErrorStringWithFormatv(
636 "Failed to dereference reference type: %s", deref_error);
637 return ValueObjectSP();
641 valobj_sp = valobj_sp->Dereference(deref_error);
643 error.SetErrorStringWithFormatv(
644 "Failed to dereference sythetic value: %s", deref_error);
645 return ValueObjectSP();
650 var_expr = var_expr.drop_front(); // Remove the '-'
653 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
654 separator_idx = var_expr.find_first_of(".-[");
655 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
657 if (check_ptr_vs_member) {
658 // We either have a pointer type and need to verify valobj_sp is a
659 // pointer, or we have a member of a class/union/struct being accessed
660 // with the . syntax and need to verify we don't have a pointer.
661 const bool actual_is_ptr = valobj_sp->IsPointerType();
663 if (actual_is_ptr != expr_is_ptr) {
664 // Incorrect use of "." with a pointer, or "->" with a
665 // class/union/struct instance or reference.
666 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
668 error.SetErrorStringWithFormat(
669 "\"%s\" is a pointer and . was used to attempt to access "
670 "\"%s\". Did you mean \"%s->%s\"?",
671 var_expr_path_strm.GetData(), child_name.GetCString(),
672 var_expr_path_strm.GetData(), var_expr.str().c_str());
674 error.SetErrorStringWithFormat(
675 "\"%s\" is not a pointer and -> was used to attempt to "
676 "access \"%s\". Did you mean \"%s.%s\"?",
677 var_expr_path_strm.GetData(), child_name.GetCString(),
678 var_expr_path_strm.GetData(), var_expr.str().c_str());
679 return ValueObjectSP();
682 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
683 if (!child_valobj_sp) {
684 if (!no_synth_child) {
685 child_valobj_sp = valobj_sp->GetSyntheticValue();
688 child_valobj_sp->GetChildMemberWithName(child_name, true);
691 if (no_synth_child || !child_valobj_sp) {
692 // No child member with name "child_name"
693 if (synthetically_added_instance_object) {
694 // We added a "this->" or "self->" to the beginning of the
695 // expression and this is the first pointer ivar access, so just
696 // return the normal error
697 error.SetErrorStringWithFormat(
698 "no variable or instance variable named '%s' found in "
700 name_const_string.GetCString());
702 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
704 error.SetErrorStringWithFormat(
705 "\"%s\" is not a member of \"(%s) %s\"",
706 child_name.GetCString(),
707 valobj_sp->GetTypeName().AsCString("<invalid type>"),
708 var_expr_path_strm.GetData());
710 error.SetErrorStringWithFormat(
711 "incomplete expression path after \"%s\" in \"%s\"",
712 var_expr_path_strm.GetData(),
713 original_var_expr.str().c_str());
716 return ValueObjectSP();
719 synthetically_added_instance_object = false;
720 // Remove the child name from the path
721 var_expr = var_expr.drop_front(child_name.GetLength());
722 if (use_dynamic != eNoDynamicValues) {
723 ValueObjectSP dynamic_value_sp(
724 child_valobj_sp->GetDynamicValue(use_dynamic));
725 if (dynamic_value_sp)
726 child_valobj_sp = dynamic_value_sp;
731 // Array member access, or treating pointer as an array Need at least two
732 // brackets and a number
733 if (var_expr.size() <= 2) {
734 error.SetErrorStringWithFormat(
735 "invalid square bracket encountered after \"%s\" in \"%s\"",
736 var_expr_path_strm.GetData(), var_expr.str().c_str());
737 return ValueObjectSP();
740 // Drop the open brace.
741 var_expr = var_expr.drop_front();
742 long child_index = 0;
744 // If there's no closing brace, this is an invalid expression.
745 size_t end_pos = var_expr.find_first_of(']');
746 if (end_pos == llvm::StringRef::npos) {
747 error.SetErrorStringWithFormat(
748 "missing closing square bracket in expression \"%s\"",
749 var_expr_path_strm.GetData());
750 return ValueObjectSP();
752 llvm::StringRef index_expr = var_expr.take_front(end_pos);
753 llvm::StringRef original_index_expr = index_expr;
754 // Drop all of "[index_expr]"
755 var_expr = var_expr.drop_front(end_pos + 1);
757 if (index_expr.consumeInteger(0, child_index)) {
758 // If there was no integer anywhere in the index expression, this is
759 // erroneous expression.
760 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
761 index_expr.str().c_str());
762 return ValueObjectSP();
765 if (index_expr.empty()) {
766 // The entire index expression was a single integer.
768 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
769 // what we have is *ptr[low]. the most similar C++ syntax is to deref
770 // ptr and extract bit low out of it. reading array item low would be
771 // done by saying ptr[low], without a deref * sign
773 ValueObjectSP temp(valobj_sp->Dereference(error));
775 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
776 error.SetErrorStringWithFormat(
777 "could not dereference \"(%s) %s\"",
778 valobj_sp->GetTypeName().AsCString("<invalid type>"),
779 var_expr_path_strm.GetData());
780 return ValueObjectSP();
784 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
786 // what we have is *arr[low]. the most similar C++ syntax is to get
787 // arr[0] (an operation that is equivalent to deref-ing arr) and
788 // extract bit low out of it. reading array item low would be done by
789 // saying arr[low], without a deref * sign
791 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
793 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
794 error.SetErrorStringWithFormat(
795 "could not get item 0 for \"(%s) %s\"",
796 valobj_sp->GetTypeName().AsCString("<invalid type>"),
797 var_expr_path_strm.GetData());
798 return ValueObjectSP();
804 bool is_incomplete_array = false;
805 if (valobj_sp->IsPointerType()) {
806 bool is_objc_pointer = true;
808 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
810 is_objc_pointer = false;
811 else if (!valobj_sp->GetCompilerType().IsPointerType())
812 is_objc_pointer = false;
814 if (no_synth_child && is_objc_pointer) {
815 error.SetErrorStringWithFormat(
816 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
818 valobj_sp->GetTypeName().AsCString("<invalid type>"),
819 var_expr_path_strm.GetData());
821 return ValueObjectSP();
822 } else if (is_objc_pointer) {
823 // dereferencing ObjC variables is not valid.. so let's try and
824 // recur to synthetic children
825 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
826 if (!synthetic /* no synthetic */
827 || synthetic == valobj_sp) /* synthetic is the same as
828 the original object */
830 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
831 error.SetErrorStringWithFormat(
832 "\"(%s) %s\" is not an array type",
833 valobj_sp->GetTypeName().AsCString("<invalid type>"),
834 var_expr_path_strm.GetData());
836 static_cast<uint32_t>(child_index) >=
838 ->GetNumChildren() /* synthetic does not have that many values */) {
839 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
840 error.SetErrorStringWithFormat(
841 "array index %ld is not valid for \"(%s) %s\"", child_index,
842 valobj_sp->GetTypeName().AsCString("<invalid type>"),
843 var_expr_path_strm.GetData());
845 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
846 if (!child_valobj_sp) {
847 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
848 error.SetErrorStringWithFormat(
849 "array index %ld is not valid for \"(%s) %s\"", child_index,
850 valobj_sp->GetTypeName().AsCString("<invalid type>"),
851 var_expr_path_strm.GetData());
856 valobj_sp->GetSyntheticArrayMember(child_index, true);
857 if (!child_valobj_sp) {
858 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
859 error.SetErrorStringWithFormat(
860 "failed to use pointer as array for index %ld for "
863 valobj_sp->GetTypeName().AsCString("<invalid type>"),
864 var_expr_path_strm.GetData());
867 } else if (valobj_sp->GetCompilerType().IsArrayType(
868 nullptr, nullptr, &is_incomplete_array)) {
869 // Pass false to dynamic_value here so we can tell the difference
870 // between no dynamic value and no member of this type...
871 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
872 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
874 valobj_sp->GetSyntheticArrayMember(child_index, true);
876 if (!child_valobj_sp) {
877 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
878 error.SetErrorStringWithFormat(
879 "array index %ld is not valid for \"(%s) %s\"", child_index,
880 valobj_sp->GetTypeName().AsCString("<invalid type>"),
881 var_expr_path_strm.GetData());
883 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
884 // this is a bitfield asking to display just one bit
885 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
886 child_index, child_index, true);
887 if (!child_valobj_sp) {
888 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
889 error.SetErrorStringWithFormat(
890 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
891 child_index, child_index,
892 valobj_sp->GetTypeName().AsCString("<invalid type>"),
893 var_expr_path_strm.GetData());
896 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
897 if (no_synth_child /* synthetic is forbidden */ ||
898 !synthetic /* no synthetic */
899 || synthetic == valobj_sp) /* synthetic is the same as the
902 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
903 error.SetErrorStringWithFormat(
904 "\"(%s) %s\" is not an array type",
905 valobj_sp->GetTypeName().AsCString("<invalid type>"),
906 var_expr_path_strm.GetData());
908 static_cast<uint32_t>(child_index) >=
910 ->GetNumChildren() /* synthetic does not have that many values */) {
911 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
912 error.SetErrorStringWithFormat(
913 "array index %ld is not valid for \"(%s) %s\"", child_index,
914 valobj_sp->GetTypeName().AsCString("<invalid type>"),
915 var_expr_path_strm.GetData());
917 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
918 if (!child_valobj_sp) {
919 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
920 error.SetErrorStringWithFormat(
921 "array index %ld is not valid for \"(%s) %s\"", child_index,
922 valobj_sp->GetTypeName().AsCString("<invalid type>"),
923 var_expr_path_strm.GetData());
928 if (!child_valobj_sp) {
929 // Invalid array index...
930 return ValueObjectSP();
933 separator_idx = var_expr.find_first_of(".-[");
934 if (use_dynamic != eNoDynamicValues) {
935 ValueObjectSP dynamic_value_sp(
936 child_valobj_sp->GetDynamicValue(use_dynamic));
937 if (dynamic_value_sp)
938 child_valobj_sp = dynamic_value_sp;
940 // Break out early from the switch since we were able to find the child
945 // this is most probably a BitField, let's take a look
946 if (index_expr.front() != '-') {
947 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
948 original_index_expr.str().c_str());
949 return ValueObjectSP();
952 index_expr = index_expr.drop_front();
953 long final_index = 0;
954 if (index_expr.getAsInteger(0, final_index)) {
955 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
956 original_index_expr.str().c_str());
957 return ValueObjectSP();
960 // if the format given is [high-low], swap range
961 if (child_index > final_index) {
962 long temp = child_index;
963 child_index = final_index;
967 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
968 // what we have is *ptr[low-high]. the most similar C++ syntax is to
969 // deref ptr and extract bits low thru high out of it. reading array
970 // items low thru high would be done by saying ptr[low-high], without a
973 ValueObjectSP temp(valobj_sp->Dereference(error));
975 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
976 error.SetErrorStringWithFormat(
977 "could not dereference \"(%s) %s\"",
978 valobj_sp->GetTypeName().AsCString("<invalid type>"),
979 var_expr_path_strm.GetData());
980 return ValueObjectSP();
984 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
985 // what we have is *arr[low-high]. the most similar C++ syntax is to
986 // get arr[0] (an operation that is equivalent to deref-ing arr) and
987 // extract bits low thru high out of it. reading array items low thru
988 // high would be done by saying arr[low-high], without a deref * sign
990 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
992 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
993 error.SetErrorStringWithFormat(
994 "could not get item 0 for \"(%s) %s\"",
995 valobj_sp->GetTypeName().AsCString("<invalid type>"),
996 var_expr_path_strm.GetData());
997 return ValueObjectSP();
1004 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1005 if (!child_valobj_sp) {
1006 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1007 error.SetErrorStringWithFormat(
1008 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
1009 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
1010 var_expr_path_strm.GetData());
1013 if (!child_valobj_sp) {
1014 // Invalid bitfield range...
1015 return ValueObjectSP();
1018 separator_idx = var_expr.find_first_of(".-[");
1019 if (use_dynamic != eNoDynamicValues) {
1020 ValueObjectSP dynamic_value_sp(
1021 child_valobj_sp->GetDynamicValue(use_dynamic));
1022 if (dynamic_value_sp)
1023 child_valobj_sp = dynamic_value_sp;
1025 // Break out early from the switch since we were able to find the child
1032 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1033 error.SetErrorStringWithFormat(
1034 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1035 separator_type, var_expr_path_strm.GetData(),
1036 var_expr.str().c_str());
1038 return ValueObjectSP();
1042 if (child_valobj_sp)
1043 valobj_sp = child_valobj_sp;
1045 if (var_expr.empty())
1050 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1051 valobj_sp = deref_valobj_sp;
1052 } else if (address_of) {
1053 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1054 valobj_sp = address_of_valobj_sp;
1060 bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Status *error_ptr) {
1061 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1062 if (!m_cfa_is_valid) {
1063 m_frame_base_error.SetErrorString(
1064 "No frame base available for this historical stack frame.");
1068 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1069 if (m_sc.function) {
1070 m_frame_base.Clear();
1071 m_frame_base_error.Clear();
1073 m_flags.Set(GOT_FRAME_BASE);
1074 ExecutionContext exe_ctx(shared_from_this());
1076 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1077 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1079 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1080 exe_ctx.GetTargetPtr());
1082 if (!m_sc.function->GetFrameBaseExpression().Evaluate(
1083 &exe_ctx, nullptr, loclist_base_addr, nullptr, nullptr,
1084 expr_value, &m_frame_base_error)) {
1085 // We should really have an error if evaluate returns, but in case we
1086 // don't, lets set the error to something at least.
1087 if (m_frame_base_error.Success())
1088 m_frame_base_error.SetErrorString(
1089 "Evaluation of the frame base expression failed.");
1091 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1094 m_frame_base_error.SetErrorString("No function in symbol context.");
1098 if (m_frame_base_error.Success())
1099 frame_base = m_frame_base;
1102 *error_ptr = m_frame_base_error;
1103 return m_frame_base_error.Success();
1106 DWARFExpression *StackFrame::GetFrameBaseExpression(Status *error_ptr) {
1107 if (!m_sc.function) {
1109 error_ptr->SetErrorString("No function in symbol context.");
1114 return &m_sc.function->GetFrameBaseExpression();
1117 RegisterContextSP StackFrame::GetRegisterContext() {
1118 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1119 if (!m_reg_context_sp) {
1120 ThreadSP thread_sp(GetThread());
1122 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1124 return m_reg_context_sp;
1127 bool StackFrame::HasDebugInformation() {
1128 GetSymbolContext(eSymbolContextLineEntry);
1129 return m_sc.line_entry.IsValid();
1133 StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1134 DynamicValueType use_dynamic) {
1135 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1136 ValueObjectSP valobj_sp;
1137 if (IsHistorical()) {
1140 VariableList *var_list = GetVariableList(true);
1142 // Make sure the variable is a frame variable
1143 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1144 const uint32_t num_variables = var_list->GetSize();
1145 if (var_idx < num_variables) {
1146 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1148 if (m_variable_list_value_objects.GetSize() < num_variables)
1149 m_variable_list_value_objects.Resize(num_variables);
1150 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1151 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1155 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1156 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1163 ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1164 DynamicValueType use_dynamic) {
1165 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1167 return ValueObjectSP();
1169 // Check to make sure we aren't already tracking this variable?
1170 ValueObjectSP valobj_sp(
1171 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1173 // We aren't already tracking this global
1174 VariableList *var_list = GetVariableList(true);
1175 // If this frame has no variables, create a new list
1176 if (var_list == nullptr)
1177 m_variable_list_sp.reset(new VariableList());
1179 // Add the global/static variable to this frame
1180 m_variable_list_sp->AddVariable(variable_sp);
1182 // Now make a value object for it so we can track its changes
1183 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1188 bool StackFrame::IsInlined() {
1189 if (m_sc.block == nullptr)
1190 GetSymbolContext(eSymbolContextBlock);
1192 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1196 bool StackFrame::IsHistorical() const {
1197 return m_stack_frame_kind == StackFrame::Kind::History;
1200 bool StackFrame::IsArtificial() const {
1201 return m_stack_frame_kind == StackFrame::Kind::Artificial;
1204 lldb::LanguageType StackFrame::GetLanguage() {
1205 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1207 return cu->GetLanguage();
1208 return lldb::eLanguageTypeUnknown;
1211 lldb::LanguageType StackFrame::GuessLanguage() {
1212 LanguageType lang_type = GetLanguage();
1214 if (lang_type == eLanguageTypeUnknown) {
1215 SymbolContext sc = GetSymbolContext(eSymbolContextFunction
1216 | eSymbolContextSymbol);
1218 lang_type = sc.function->GetMangled().GuessLanguage();
1222 lang_type = sc.symbol->GetMangled().GuessLanguage();
1230 std::pair<const Instruction::Operand *, int64_t>
1231 GetBaseExplainingValue(const Instruction::Operand &operand,
1232 RegisterContext ®ister_context, lldb::addr_t value) {
1233 switch (operand.m_type) {
1234 case Instruction::Operand::Type::Dereference:
1235 case Instruction::Operand::Type::Immediate:
1236 case Instruction::Operand::Type::Invalid:
1237 case Instruction::Operand::Type::Product:
1238 // These are not currently interesting
1239 return std::make_pair(nullptr, 0);
1240 case Instruction::Operand::Type::Sum: {
1241 const Instruction::Operand *immediate_child = nullptr;
1242 const Instruction::Operand *variable_child = nullptr;
1243 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1244 immediate_child = &operand.m_children[0];
1245 variable_child = &operand.m_children[1];
1246 } else if (operand.m_children[1].m_type ==
1247 Instruction::Operand::Type::Immediate) {
1248 immediate_child = &operand.m_children[1];
1249 variable_child = &operand.m_children[0];
1251 if (!immediate_child) {
1252 return std::make_pair(nullptr, 0);
1254 lldb::addr_t adjusted_value = value;
1255 if (immediate_child->m_negative) {
1256 adjusted_value += immediate_child->m_immediate;
1258 adjusted_value -= immediate_child->m_immediate;
1260 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1261 GetBaseExplainingValue(*variable_child, register_context,
1263 if (!base_and_offset.first) {
1264 return std::make_pair(nullptr, 0);
1266 if (immediate_child->m_negative) {
1267 base_and_offset.second -= immediate_child->m_immediate;
1269 base_and_offset.second += immediate_child->m_immediate;
1271 return base_and_offset;
1273 case Instruction::Operand::Type::Register: {
1274 const RegisterInfo *info =
1275 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1277 return std::make_pair(nullptr, 0);
1279 RegisterValue reg_value;
1280 if (!register_context.ReadRegister(info, reg_value)) {
1281 return std::make_pair(nullptr, 0);
1283 if (reg_value.GetAsUInt64() == value) {
1284 return std::make_pair(&operand, 0);
1286 return std::make_pair(nullptr, 0);
1290 return std::make_pair(nullptr, 0);
1293 std::pair<const Instruction::Operand *, int64_t>
1294 GetBaseExplainingDereference(const Instruction::Operand &operand,
1295 RegisterContext ®ister_context,
1296 lldb::addr_t addr) {
1297 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1298 return GetBaseExplainingValue(operand.m_children[0], register_context,
1301 return std::make_pair(nullptr, 0);
1305 lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1306 TargetSP target_sp = CalculateTarget();
1308 const ArchSpec &target_arch = target_sp->GetArchitecture();
1310 AddressRange pc_range;
1311 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1312 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1314 ExecutionContext exe_ctx(shared_from_this());
1316 const char *plugin_name = nullptr;
1317 const char *flavor = nullptr;
1318 const bool prefer_file_cache = false;
1320 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1321 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1323 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1324 return ValueObjectSP();
1327 InstructionSP instruction_sp =
1328 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1330 llvm::SmallVector<Instruction::Operand, 3> operands;
1332 if (!instruction_sp->ParseOperands(operands)) {
1333 return ValueObjectSP();
1336 RegisterContextSP register_context_sp = GetRegisterContext();
1338 if (!register_context_sp) {
1339 return ValueObjectSP();
1342 for (const Instruction::Operand &operand : operands) {
1343 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1344 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1346 if (!base_and_offset.first) {
1350 switch (base_and_offset.first->m_type) {
1351 case Instruction::Operand::Type::Immediate: {
1352 lldb_private::Address addr;
1353 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1354 base_and_offset.second,
1356 TypeSystem *c_type_system =
1357 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1358 if (!c_type_system) {
1359 return ValueObjectSP();
1361 CompilerType void_ptr_type =
1363 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1365 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
1368 return ValueObjectSP();
1372 case Instruction::Operand::Type::Register: {
1373 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1374 base_and_offset.second);
1377 return ValueObjectSP();
1381 return ValueObjectSP();
1385 ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1387 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1388 return ValueObjectSP();
1391 if (parent->IsPointerOrReferenceType()) {
1395 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1396 const bool can_create = true;
1397 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1400 return ValueObjectSP();
1403 int64_t child_offset = child_sp->GetByteOffset();
1404 int64_t child_size = child_sp->GetByteSize();
1406 if (offset >= child_offset && offset < (child_offset + child_size)) {
1407 return GetValueForOffset(frame, child_sp, offset - child_offset);
1414 return ValueObjectSP();
1418 ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1419 ValueObjectSP &base,
1421 // base is a pointer to something
1422 // offset is the thing to add to the pointer We return the most sensible
1423 // ValueObject for the result of *(base+offset)
1425 if (!base->IsPointerOrReferenceType()) {
1426 return ValueObjectSP();
1430 ValueObjectSP pointee = base->Dereference(error);
1433 return ValueObjectSP();
1436 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
1437 int64_t index = offset / pointee->GetByteSize();
1438 offset = offset % pointee->GetByteSize();
1439 const bool can_create = true;
1440 pointee = base->GetSyntheticArrayMember(index, can_create);
1443 if (!pointee || error.Fail()) {
1444 return ValueObjectSP();
1447 return GetValueForOffset(frame, pointee, offset);
1450 //------------------------------------------------------------------
1451 /// Attempt to reconstruct the ValueObject for the address contained in a
1452 /// given register plus an offset.
1454 /// @params [in] frame
1455 /// The current stack frame.
1457 /// @params [in] reg
1460 /// @params [in] offset
1461 /// The offset from the register.
1463 /// @param [in] disassembler
1464 /// A disassembler containing instructions valid up to the current PC.
1466 /// @param [in] variables
1467 /// The variable list from the current frame,
1470 /// The program counter for the instruction considered the 'user'.
1473 /// A string describing the base for the ExpressionPath. This could be a
1474 /// variable, a register value, an argument, or a function return value.
1475 /// The ValueObject if found. If valid, it has a valid ExpressionPath.
1476 //------------------------------------------------------------------
1477 lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1478 int64_t offset, Disassembler &disassembler,
1479 VariableList &variables, const Address &pc) {
1480 // Example of operation for Intel:
1482 // +14: movq -0x8(%rbp), %rdi
1483 // +18: movq 0x8(%rdi), %rdi
1484 // +22: addl 0x4(%rdi), %eax
1486 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1488 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at
1489 // +18 that assigns to rdi, and calls itself recursively for that dereference
1490 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1491 // +14 that assigns to rdi, and calls itself recursively for that
1493 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1495 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1496 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1498 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1501 // First, check the variable list to see if anything is at the specified
1504 using namespace OperandMatchers;
1506 const RegisterInfo *reg_info =
1507 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1509 return ValueObjectSP();
1512 Instruction::Operand op =
1513 offset ? Instruction::Operand::BuildDereference(
1514 Instruction::Operand::BuildSum(
1515 Instruction::Operand::BuildRegister(reg),
1516 Instruction::Operand::BuildImmediate(offset)))
1517 : Instruction::Operand::BuildDereference(
1518 Instruction::Operand::BuildRegister(reg));
1520 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1521 VariableSP var_sp = variables.GetVariableAtIndex(vi);
1522 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1523 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1527 const uint32_t current_inst =
1528 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1529 if (current_inst == UINT32_MAX) {
1530 return ValueObjectSP();
1533 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1534 // This is not an exact algorithm, and it sacrifices accuracy for
1535 // generality. Recognizing "mov" and "ld" instructions –– and which
1536 // are their source and destination operands -- is something the
1537 // disassembler should do for us.
1538 InstructionSP instruction_sp =
1539 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
1541 if (instruction_sp->IsCall()) {
1542 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1547 const char *return_register_name;
1548 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1552 const RegisterInfo *return_register_info =
1553 frame.GetRegisterContext()->GetRegisterInfoByName(
1554 return_register_name);
1555 if (!return_register_info) {
1561 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1562 MatchRegOp(*return_register_info))(op) &&
1564 MatchOpType(Instruction::Operand::Type::Dereference),
1565 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1566 MatchRegOp(*return_register_info),
1567 FetchImmOp(offset)))(op)) {
1571 llvm::SmallVector<Instruction::Operand, 1> operands;
1572 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1576 switch (operands[0].m_type) {
1579 case Instruction::Operand::Type::Immediate: {
1581 Address load_address;
1582 if (!frame.CalculateTarget()->ResolveLoadAddress(
1583 operands[0].m_immediate, load_address)) {
1586 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1587 load_address, eSymbolContextFunction, sc);
1591 CompilerType function_type = sc.function->GetCompilerType();
1592 if (!function_type.IsFunctionType()) {
1595 CompilerType return_type = function_type.GetFunctionReturnType();
1596 RegisterValue return_value;
1597 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
1601 std::string name_str(
1602 sc.function->GetName().AsCString("<unknown function>"));
1603 name_str.append("()");
1604 Address return_value_address(return_value.GetAsUInt64());
1605 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
1606 &frame, name_str, return_value_address, return_type);
1607 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1614 llvm::SmallVector<Instruction::Operand, 2> operands;
1615 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1619 Instruction::Operand *origin_operand = nullptr;
1620 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1621 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1624 if (clobbered_reg_matcher(operands[0])) {
1625 origin_operand = &operands[1];
1627 else if (clobbered_reg_matcher(operands[1])) {
1628 origin_operand = &operands[0];
1634 // We have an origin operand. Can we track its value down?
1635 ValueObjectSP source_path;
1636 ConstString origin_register;
1637 int64_t origin_offset = 0;
1639 if (FetchRegOp(origin_register)(*origin_operand)) {
1640 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1641 variables, instruction_sp->GetAddress());
1642 } else if (MatchUnaryOp(
1643 MatchOpType(Instruction::Operand::Type::Dereference),
1644 FetchRegOp(origin_register))(*origin_operand) ||
1646 MatchOpType(Instruction::Operand::Type::Dereference),
1647 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1648 FetchRegOp(origin_register),
1649 FetchImmOp(origin_offset)))(*origin_operand)) {
1651 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
1652 variables, instruction_sp->GetAddress());
1657 GetValueForDereferincingOffset(frame, source_path, offset);
1665 return ValueObjectSP();
1669 lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1671 TargetSP target_sp = CalculateTarget();
1673 const ArchSpec &target_arch = target_sp->GetArchitecture();
1675 Block *frame_block = GetFrameBlock();
1678 return ValueObjectSP();
1681 Function *function = frame_block->CalculateSymbolContextFunction();
1683 return ValueObjectSP();
1686 AddressRange pc_range = function->GetAddressRange();
1688 if (GetFrameCodeAddress().GetFileAddress() <
1689 pc_range.GetBaseAddress().GetFileAddress() ||
1690 GetFrameCodeAddress().GetFileAddress() -
1691 pc_range.GetBaseAddress().GetFileAddress() >=
1692 pc_range.GetByteSize()) {
1693 return ValueObjectSP();
1696 ExecutionContext exe_ctx(shared_from_this());
1698 const char *plugin_name = nullptr;
1699 const char *flavor = nullptr;
1700 const bool prefer_file_cache = false;
1701 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1702 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1704 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1705 return ValueObjectSP();
1708 const bool get_file_globals = false;
1709 VariableList *variables = GetVariableList(get_file_globals);
1712 return ValueObjectSP();
1715 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1716 GetFrameCodeAddress());
1719 lldb::ValueObjectSP StackFrame::FindVariable(ConstString name) {
1720 ValueObjectSP value_sp;
1725 TargetSP target_sp = CalculateTarget();
1726 ProcessSP process_sp = CalculateProcess();
1728 if (!target_sp && !process_sp)
1731 VariableList variable_list;
1733 SymbolContext sc(GetSymbolContext(eSymbolContextBlock));
1736 const bool can_create = true;
1737 const bool get_parent_variables = true;
1738 const bool stop_if_block_is_inlined_function = true;
1740 if (sc.block->AppendVariables(
1741 can_create, get_parent_variables, stop_if_block_is_inlined_function,
1742 [this](Variable *v) { return v->IsInScope(this); },
1744 var_sp = variable_list.FindVariable(name);
1748 value_sp = GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1754 TargetSP StackFrame::CalculateTarget() {
1756 ThreadSP thread_sp(GetThread());
1758 ProcessSP process_sp(thread_sp->CalculateProcess());
1760 target_sp = process_sp->CalculateTarget();
1765 ProcessSP StackFrame::CalculateProcess() {
1766 ProcessSP process_sp;
1767 ThreadSP thread_sp(GetThread());
1769 process_sp = thread_sp->CalculateProcess();
1773 ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1775 StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1777 void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1778 exe_ctx.SetContext(shared_from_this());
1781 void StackFrame::DumpUsingSettingsFormat(Stream *strm, bool show_unique,
1782 const char *frame_marker) {
1783 if (strm == nullptr)
1786 GetSymbolContext(eSymbolContextEverything);
1787 ExecutionContext exe_ctx(shared_from_this());
1791 s.PutCString(frame_marker);
1793 const FormatEntity::Entry *frame_format = nullptr;
1794 Target *target = exe_ctx.GetTargetPtr();
1797 frame_format = target->GetDebugger().GetFrameFormatUnique();
1799 frame_format = target->GetDebugger().GetFrameFormat();
1802 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1803 nullptr, nullptr, false, false)) {
1804 strm->PutCString(s.GetString());
1806 Dump(strm, true, false);
1811 void StackFrame::Dump(Stream *strm, bool show_frame_index,
1812 bool show_fullpaths) {
1813 if (strm == nullptr)
1816 if (show_frame_index)
1817 strm->Printf("frame #%u: ", m_frame_index);
1818 ExecutionContext exe_ctx(shared_from_this());
1819 Target *target = exe_ctx.GetTargetPtr();
1820 strm->Printf("0x%0*" PRIx64 " ",
1821 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1823 GetFrameCodeAddress().GetLoadAddress(target));
1824 GetSymbolContext(eSymbolContextEverything);
1825 const bool show_module = true;
1826 const bool show_inline = true;
1827 const bool show_function_arguments = true;
1828 const bool show_function_name = true;
1829 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1830 GetFrameCodeAddress(), show_fullpaths, show_module,
1831 show_inline, show_function_arguments,
1832 show_function_name);
1835 void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1836 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1837 assert(GetStackID() ==
1838 prev_frame.GetStackID()); // TODO: remove this after some testing
1839 m_variable_list_sp = prev_frame.m_variable_list_sp;
1840 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
1841 if (!m_disassembly.GetString().empty()) {
1842 m_disassembly.Clear();
1843 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1847 void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1848 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1849 assert(GetStackID() ==
1850 curr_frame.GetStackID()); // TODO: remove this after some testing
1851 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1852 assert(GetThread() == curr_frame.GetThread());
1853 m_frame_index = curr_frame.m_frame_index;
1854 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1855 m_reg_context_sp = curr_frame.m_reg_context_sp;
1856 m_frame_code_addr = curr_frame.m_frame_code_addr;
1857 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1858 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1859 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1860 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1861 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1862 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1863 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1864 m_sc.function == curr_frame.m_sc.function);
1865 m_sc = curr_frame.m_sc;
1866 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1867 m_flags.Set(m_sc.GetResolvedMask());
1868 m_frame_base.Clear();
1869 m_frame_base_error.Clear();
1872 bool StackFrame::HasCachedData() const {
1873 if (m_variable_list_sp)
1875 if (m_variable_list_value_objects.GetSize() > 0)
1877 if (!m_disassembly.GetString().empty())
1882 bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
1883 bool show_unique, const char *frame_marker) {
1884 if (show_frame_info) {
1886 DumpUsingSettingsFormat(&strm, show_unique, frame_marker);
1890 ExecutionContext exe_ctx(shared_from_this());
1891 bool have_source = false, have_debuginfo = false;
1892 Debugger::StopDisassemblyType disasm_display =
1893 Debugger::eStopDisassemblyTypeNever;
1894 Target *target = exe_ctx.GetTargetPtr();
1896 Debugger &debugger = target->GetDebugger();
1897 const uint32_t source_lines_before =
1898 debugger.GetStopSourceLineCount(true);
1899 const uint32_t source_lines_after =
1900 debugger.GetStopSourceLineCount(false);
1901 disasm_display = debugger.GetStopDisassemblyDisplay();
1903 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1904 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1905 have_debuginfo = true;
1906 if (source_lines_before > 0 || source_lines_after > 0) {
1908 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1909 m_sc.line_entry.file, m_sc.line_entry.line,
1910 m_sc.line_entry.column, source_lines_before,
1911 source_lines_after, "->", &strm);
1914 // TODO: Give here a one time warning if source file is missing.
1917 switch (disasm_display) {
1918 case Debugger::eStopDisassemblyTypeNever:
1921 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1926 case Debugger::eStopDisassemblyTypeNoSource:
1931 case Debugger::eStopDisassemblyTypeAlways:
1933 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1934 if (disasm_lines > 0) {
1935 const ArchSpec &target_arch = target->GetArchitecture();
1936 AddressRange pc_range;
1937 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1938 pc_range.SetByteSize(disasm_lines *
1939 target_arch.GetMaximumOpcodeByteSize());
1940 const char *plugin_name = nullptr;
1941 const char *flavor = nullptr;
1942 const bool mixed_source_and_assembly = false;
1943 Disassembler::Disassemble(
1944 target->GetDebugger(), target_arch, plugin_name, flavor,
1945 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1946 Disassembler::eOptionMarkPCAddress, strm);
1956 RecognizedStackFrameSP StackFrame::GetRecognizedFrame() {
1957 if (!m_recognized_frame_sp) {
1958 m_recognized_frame_sp =
1959 StackFrameRecognizerManager::RecognizeFrame(CalculateStackFrame());
1961 return m_recognized_frame_sp;