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 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/StackFrame.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/Disassembler.h"
17 #include "lldb/Core/FormatEntity.h"
18 #include "lldb/Core/Mangled.h"
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/Value.h"
21 #include "lldb/Core/ValueObjectConstResult.h"
22 #include "lldb/Core/ValueObjectMemory.h"
23 #include "lldb/Core/ValueObjectVariable.h"
24 #include "lldb/Symbol/CompileUnit.h"
25 #include "lldb/Symbol/Function.h"
26 #include "lldb/Symbol/Symbol.h"
27 #include "lldb/Symbol/SymbolContextScope.h"
28 #include "lldb/Symbol/Type.h"
29 #include "lldb/Symbol/VariableList.h"
30 #include "lldb/Target/ABI.h"
31 #include "lldb/Target/ExecutionContext.h"
32 #include "lldb/Target/Process.h"
33 #include "lldb/Target/RegisterContext.h"
34 #include "lldb/Target/Target.h"
35 #include "lldb/Target/Thread.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, uint32_t stop_id,
52 bool stop_id_is_valid, bool is_history_frame,
53 const SymbolContext *sc_ptr)
54 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
55 m_concrete_frame_index(unwind_frame_index), m_reg_context_sp(),
56 m_id(pc, cfa, nullptr), m_frame_code_addr(pc), m_sc(), m_flags(),
57 m_frame_base(), m_frame_base_error(), m_cfa_is_valid(cfa_is_valid),
58 m_stop_id(stop_id), m_stop_id_is_valid(stop_id_is_valid),
59 m_is_history_frame(is_history_frame), m_variable_list_sp(),
60 m_variable_list_value_objects(), m_disassembly(), m_mutex() {
61 // If we don't have a CFA value, use the frame index for our StackID so that
63 // functions properly aren't confused with one another on a history stack.
64 if (m_is_history_frame && !m_cfa_is_valid) {
65 m_id.SetCFA(m_frame_index);
68 if (sc_ptr != nullptr) {
70 m_flags.Set(m_sc.GetResolvedMask());
74 StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
75 user_id_t unwind_frame_index,
76 const RegisterContextSP ®_context_sp, addr_t cfa,
77 addr_t pc, const SymbolContext *sc_ptr)
78 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
79 m_concrete_frame_index(unwind_frame_index),
80 m_reg_context_sp(reg_context_sp), m_id(pc, cfa, nullptr),
81 m_frame_code_addr(pc), m_sc(), m_flags(), m_frame_base(),
82 m_frame_base_error(), m_cfa_is_valid(true), m_stop_id(0),
83 m_stop_id_is_valid(false), m_is_history_frame(false),
84 m_variable_list_sp(), m_variable_list_value_objects(), m_disassembly(),
86 if (sc_ptr != nullptr) {
88 m_flags.Set(m_sc.GetResolvedMask());
91 if (reg_context_sp && !m_sc.target_sp) {
92 m_sc.target_sp = reg_context_sp->CalculateTarget();
94 m_flags.Set(eSymbolContextTarget);
98 StackFrame::StackFrame(const ThreadSP &thread_sp, user_id_t frame_idx,
99 user_id_t unwind_frame_index,
100 const RegisterContextSP ®_context_sp, addr_t cfa,
101 const Address &pc_addr, const SymbolContext *sc_ptr)
102 : m_thread_wp(thread_sp), m_frame_index(frame_idx),
103 m_concrete_frame_index(unwind_frame_index),
104 m_reg_context_sp(reg_context_sp),
105 m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa,
107 m_frame_code_addr(pc_addr), m_sc(), m_flags(), m_frame_base(),
108 m_frame_base_error(), m_cfa_is_valid(true), m_stop_id(0),
109 m_stop_id_is_valid(false), m_is_history_frame(false),
110 m_variable_list_sp(), m_variable_list_value_objects(), m_disassembly(),
112 if (sc_ptr != nullptr) {
114 m_flags.Set(m_sc.GetResolvedMask());
117 if (!m_sc.target_sp && reg_context_sp) {
118 m_sc.target_sp = reg_context_sp->CalculateTarget();
120 m_flags.Set(eSymbolContextTarget);
123 ModuleSP pc_module_sp(pc_addr.GetModule());
124 if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp) {
126 m_sc.module_sp = pc_module_sp;
127 m_flags.Set(eSymbolContextModule);
129 m_sc.module_sp.reset();
134 StackFrame::~StackFrame() = default;
136 StackID &StackFrame::GetStackID() {
137 std::lock_guard<std::recursive_mutex> guard(m_mutex);
138 // Make sure we have resolved the StackID object's symbol context scope if
139 // we already haven't looked it up.
141 if (m_flags.IsClear(RESOLVED_FRAME_ID_SYMBOL_SCOPE)) {
142 if (m_id.GetSymbolContextScope()) {
143 // We already have a symbol context scope, we just don't have our
145 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
147 // Calculate the frame block and use this for the stack ID symbol
148 // context scope if we have one.
149 SymbolContextScope *scope = GetFrameBlock();
150 if (scope == nullptr) {
151 // We don't have a block, so use the symbol
152 if (m_flags.IsClear(eSymbolContextSymbol))
153 GetSymbolContext(eSymbolContextSymbol);
155 // It is ok if m_sc.symbol is nullptr here
158 // Set the symbol context scope (the accessor will set the
159 // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
160 SetSymbolContextScope(scope);
166 uint32_t StackFrame::GetFrameIndex() const {
167 ThreadSP thread_sp = GetThread();
169 return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(
172 return m_frame_index;
175 void StackFrame::SetSymbolContextScope(SymbolContextScope *symbol_scope) {
176 std::lock_guard<std::recursive_mutex> guard(m_mutex);
177 m_flags.Set(RESOLVED_FRAME_ID_SYMBOL_SCOPE);
178 m_id.SetSymbolContextScope(symbol_scope);
181 const Address &StackFrame::GetFrameCodeAddress() {
182 std::lock_guard<std::recursive_mutex> guard(m_mutex);
183 if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) &&
184 !m_frame_code_addr.IsSectionOffset()) {
185 m_flags.Set(RESOLVED_FRAME_CODE_ADDR);
187 // Resolve the PC into a temporary address because if ResolveLoadAddress
188 // fails to resolve the address, it will clear the address object...
189 ThreadSP thread_sp(GetThread());
191 TargetSP target_sp(thread_sp->CalculateTarget());
193 if (m_frame_code_addr.SetOpcodeLoadAddress(
194 m_frame_code_addr.GetOffset(), target_sp.get(),
195 eAddressClassCode)) {
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.
211 if (m_is_history_frame)
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())
227 ExecutionContext exe_ctx(shared_from_this());
228 Target *target = exe_ctx.GetTargetPtr();
230 const char *plugin_name = nullptr;
231 const char *flavor = nullptr;
232 Disassembler::Disassemble(target->GetDebugger(), target->GetArchitecture(),
233 plugin_name, flavor, exe_ctx, 0, false, 0, 0,
236 return m_disassembly.GetData();
239 Block *StackFrame::GetFrameBlock() {
240 if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
241 GetSymbolContext(eSymbolContextBlock);
244 Block *inline_block = m_sc.block->GetContainingInlinedBlock();
246 // Use the block with the inlined function info
247 // as the frame block we want this frame to have only the variables
248 // for the inlined function and its non-inlined block child blocks.
251 // This block is not contained within any inlined function blocks
252 // with so we want to use the top most function block.
253 return &m_sc.function->GetBlock(false);
259 //----------------------------------------------------------------------
260 // Get the symbol context if we already haven't done so by resolving the
261 // PC address as much as possible. This way when we pass around a
262 // StackFrame object, everyone will have as much information as
263 // possible and no one will ever have to look things up manually.
264 //----------------------------------------------------------------------
265 const SymbolContext &StackFrame::GetSymbolContext(uint32_t resolve_scope) {
266 std::lock_guard<std::recursive_mutex> guard(m_mutex);
267 // Copy our internal symbol context into "sc".
268 if ((m_flags.Get() & resolve_scope) != resolve_scope) {
269 uint32_t resolved = 0;
271 // If the target was requested add that:
272 if (!m_sc.target_sp) {
273 m_sc.target_sp = CalculateTarget();
275 resolved |= eSymbolContextTarget;
278 // Resolve our PC to section offset if we haven't already done so
279 // and if we don't have a module. The resolved address section will
280 // contain the module to which it belongs
281 if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
282 GetFrameCodeAddress();
284 // If this is not frame zero, then we need to subtract 1 from the PC
285 // value when doing address lookups since the PC will be on the
286 // instruction following the function call instruction...
288 Address lookup_addr(GetFrameCodeAddress());
289 if (m_frame_index > 0 && lookup_addr.IsValid()) {
290 addr_t offset = lookup_addr.GetOffset();
292 lookup_addr.SetOffset(offset - 1);
295 // lookup_addr is the start of a section. We need
296 // do the math on the actual load address and re-compute
297 // the section. We're working with a 'noreturn' function
298 // at the end of a section.
299 ThreadSP thread_sp(GetThread());
301 TargetSP target_sp(thread_sp->CalculateTarget());
303 addr_t addr_minus_one =
304 lookup_addr.GetLoadAddress(target_sp.get()) - 1;
305 lookup_addr.SetLoadAddress(addr_minus_one, target_sp.get());
307 lookup_addr.SetOffset(offset - 1);
313 if (m_sc.module_sp) {
314 // We have something in our stack frame symbol context, lets check
315 // if we haven't already tried to lookup one of those things. If we
316 // haven't then we will do the query.
318 uint32_t actual_resolve_scope = 0;
320 if (resolve_scope & eSymbolContextCompUnit) {
321 if (m_flags.IsClear(eSymbolContextCompUnit)) {
323 resolved |= eSymbolContextCompUnit;
325 actual_resolve_scope |= eSymbolContextCompUnit;
329 if (resolve_scope & eSymbolContextFunction) {
330 if (m_flags.IsClear(eSymbolContextFunction)) {
332 resolved |= eSymbolContextFunction;
334 actual_resolve_scope |= eSymbolContextFunction;
338 if (resolve_scope & eSymbolContextBlock) {
339 if (m_flags.IsClear(eSymbolContextBlock)) {
341 resolved |= eSymbolContextBlock;
343 actual_resolve_scope |= eSymbolContextBlock;
347 if (resolve_scope & eSymbolContextSymbol) {
348 if (m_flags.IsClear(eSymbolContextSymbol)) {
350 resolved |= eSymbolContextSymbol;
352 actual_resolve_scope |= eSymbolContextSymbol;
356 if (resolve_scope & eSymbolContextLineEntry) {
357 if (m_flags.IsClear(eSymbolContextLineEntry)) {
358 if (m_sc.line_entry.IsValid())
359 resolved |= eSymbolContextLineEntry;
361 actual_resolve_scope |= eSymbolContextLineEntry;
365 if (actual_resolve_scope) {
366 // We might be resolving less information than what is already
367 // in our current symbol context so resolve into a temporary
368 // symbol context "sc" so we don't clear out data we have
369 // already found in "m_sc"
371 // Set flags that indicate what we have tried to resolve
372 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress(
373 lookup_addr, actual_resolve_scope, sc);
374 // Only replace what we didn't already have as we may have
375 // information for an inlined function scope that won't match
376 // what a standard lookup by address would match
377 if ((resolved & eSymbolContextCompUnit) && m_sc.comp_unit == nullptr)
378 m_sc.comp_unit = sc.comp_unit;
379 if ((resolved & eSymbolContextFunction) && m_sc.function == nullptr)
380 m_sc.function = sc.function;
381 if ((resolved & eSymbolContextBlock) && m_sc.block == nullptr)
382 m_sc.block = sc.block;
383 if ((resolved & eSymbolContextSymbol) && m_sc.symbol == nullptr)
384 m_sc.symbol = sc.symbol;
385 if ((resolved & eSymbolContextLineEntry) &&
386 !m_sc.line_entry.IsValid()) {
387 m_sc.line_entry = sc.line_entry;
388 m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
392 // If we don't have a module, then we can't have the compile unit,
393 // function, block, line entry or symbol, so we can safely call
394 // ResolveSymbolContextForAddress with our symbol context member m_sc.
395 if (m_sc.target_sp) {
396 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress(
397 lookup_addr, resolve_scope, m_sc);
401 // Update our internal flags so we remember what we have tried to locate so
402 // we don't have to keep trying when more calls to this function are made.
403 // We might have dug up more information that was requested (for example
404 // if we were asked to only get the block, we will have gotten the
405 // compile unit, and function) so set any additional bits that we resolved
406 m_flags.Set(resolve_scope | resolved);
409 // Return the symbol context with everything that was possible to resolve
414 VariableList *StackFrame::GetVariableList(bool get_file_globals) {
415 std::lock_guard<std::recursive_mutex> guard(m_mutex);
416 if (m_flags.IsClear(RESOLVED_VARIABLES)) {
417 m_flags.Set(RESOLVED_VARIABLES);
419 Block *frame_block = GetFrameBlock();
422 const bool get_child_variables = true;
423 const bool can_create = true;
424 const bool stop_if_child_block_is_inlined_function = true;
425 m_variable_list_sp.reset(new VariableList());
426 frame_block->AppendBlockVariables(can_create, get_child_variables,
427 stop_if_child_block_is_inlined_function,
428 [this](Variable *v) { return true; },
429 m_variable_list_sp.get());
433 if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) && get_file_globals) {
434 m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
436 if (m_flags.IsClear(eSymbolContextCompUnit))
437 GetSymbolContext(eSymbolContextCompUnit);
439 if (m_sc.comp_unit) {
440 VariableListSP global_variable_list_sp(
441 m_sc.comp_unit->GetVariableList(true));
442 if (m_variable_list_sp)
443 m_variable_list_sp->AddVariables(global_variable_list_sp.get());
445 m_variable_list_sp = global_variable_list_sp;
449 return m_variable_list_sp.get();
453 StackFrame::GetInScopeVariableList(bool get_file_globals,
454 bool must_have_valid_location) {
455 std::lock_guard<std::recursive_mutex> guard(m_mutex);
456 // We can't fetch variable information for a history stack frame.
457 if (m_is_history_frame)
458 return VariableListSP();
460 VariableListSP var_list_sp(new VariableList);
461 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextBlock);
464 const bool can_create = true;
465 const bool get_parent_variables = true;
466 const bool stop_if_block_is_inlined_function = true;
467 m_sc.block->AppendVariables(
468 can_create, get_parent_variables, stop_if_block_is_inlined_function,
469 [this, must_have_valid_location](Variable *v) {
470 return v->IsInScope(this) && (!must_have_valid_location ||
471 v->LocationIsValidForFrame(this));
476 if (m_sc.comp_unit && get_file_globals) {
477 VariableListSP global_variable_list_sp(
478 m_sc.comp_unit->GetVariableList(true));
479 if (global_variable_list_sp)
480 var_list_sp->AddVariables(global_variable_list_sp.get());
486 ValueObjectSP StackFrame::GetValueForVariableExpressionPath(
487 llvm::StringRef var_expr, DynamicValueType use_dynamic, uint32_t options,
488 VariableSP &var_sp, Error &error) {
489 llvm::StringRef original_var_expr = var_expr;
490 // We can't fetch variable information for a history stack frame.
491 if (m_is_history_frame)
492 return ValueObjectSP();
494 if (var_expr.empty()) {
495 error.SetErrorStringWithFormat("invalid variable path '%s'",
496 var_expr.str().c_str());
497 return ValueObjectSP();
500 const bool check_ptr_vs_member =
501 (options & eExpressionPathOptionCheckPtrVsMember) != 0;
502 const bool no_fragile_ivar =
503 (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
504 const bool no_synth_child =
505 (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
506 // const bool no_synth_array = (options &
507 // eExpressionPathOptionsNoSyntheticArrayRange) != 0;
510 bool address_of = false;
511 ValueObjectSP valobj_sp;
512 const bool get_file_globals = true;
513 // When looking up a variable for an expression, we need only consider the
514 // variables that are in scope.
515 VariableListSP var_list_sp(GetInScopeVariableList(get_file_globals));
516 VariableList *variable_list = var_list_sp.get();
519 return ValueObjectSP();
521 // If first character is a '*', then show pointer contents
522 std::string var_expr_storage;
523 if (var_expr[0] == '*') {
525 var_expr = var_expr.drop_front(); // Skip the '*'
526 } else if (var_expr[0] == '&') {
528 var_expr = var_expr.drop_front(); // Skip the '&'
531 size_t separator_idx = var_expr.find_first_of(".-[=+~|&^%#@!/?,<>{}");
532 StreamString var_expr_path_strm;
534 ConstString name_const_string(var_expr.substr(0, separator_idx));
536 var_sp = variable_list->FindVariable(name_const_string, false);
538 bool synthetically_added_instance_object = false;
541 var_expr = var_expr.drop_front(name_const_string.GetLength());
544 if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess)) {
545 // Check for direct ivars access which helps us with implicit
546 // access to ivars with the "this->" or "self->"
547 GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock);
548 lldb::LanguageType method_language = eLanguageTypeUnknown;
549 bool is_instance_method = false;
550 ConstString method_object_name;
551 if (m_sc.GetFunctionMethodInfo(method_language, is_instance_method,
552 method_object_name)) {
553 if (is_instance_method && method_object_name) {
554 var_sp = variable_list->FindVariable(method_object_name);
557 var_expr_storage = "->";
558 var_expr_storage += var_expr;
559 var_expr = var_expr_storage;
560 synthetically_added_instance_object = true;
566 if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions)) {
567 // Check if any anonymous unions are there which contain a variable with
569 for (size_t i = 0; i < variable_list->GetSize(); i++) {
570 VariableSP variable_sp = variable_list->GetVariableAtIndex(i);
573 if (!variable_sp->GetName().IsEmpty())
576 Type *var_type = variable_sp->GetType();
580 if (!var_type->GetForwardCompilerType().IsAnonymousType())
582 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
585 valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
591 if (var_sp && !valobj_sp) {
592 valobj_sp = GetValueObjectForFrameVariable(var_sp, use_dynamic);
597 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
598 name_const_string.GetCString());
599 return ValueObjectSP();
602 // We are dumping at least one child
603 while (separator_idx != std::string::npos) {
604 // Calculate the next separator index ahead of time
605 ValueObjectSP child_valobj_sp;
606 const char separator_type = var_expr[0];
607 switch (separator_type) {
609 if (var_expr.size() >= 2 && var_expr[1] != '>')
610 return ValueObjectSP();
612 if (no_fragile_ivar) {
613 // Make sure we aren't trying to deref an objective
614 // C ivar if this is not allowed
615 const uint32_t pointer_type_flags =
616 valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
617 if ((pointer_type_flags & eTypeIsObjC) &&
618 (pointer_type_flags & eTypeIsPointer)) {
619 // This was an objective C object pointer and
620 // it was requested we skip any fragile ivars
621 // so return nothing here
622 return ValueObjectSP();
625 var_expr = var_expr.drop_front(); // Remove the '-'
628 const bool expr_is_ptr = var_expr[0] == '>';
630 var_expr = var_expr.drop_front(); // Remove the '.' or '>'
631 separator_idx = var_expr.find_first_of(".-[");
632 ConstString child_name(var_expr.substr(0, var_expr.find_first_of(".-[")));
634 if (check_ptr_vs_member) {
635 // We either have a pointer type and need to verify
636 // valobj_sp is a pointer, or we have a member of a
637 // class/union/struct being accessed with the . syntax
638 // and need to verify we don't have a pointer.
639 const bool actual_is_ptr = valobj_sp->IsPointerType();
641 if (actual_is_ptr != expr_is_ptr) {
642 // Incorrect use of "." with a pointer, or "->" with
643 // a class/union/struct instance or reference.
644 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
646 error.SetErrorStringWithFormat(
647 "\"%s\" is a pointer and . was used to attempt to access "
648 "\"%s\". Did you mean \"%s->%s\"?",
649 var_expr_path_strm.GetData(), child_name.GetCString(),
650 var_expr_path_strm.GetData(), var_expr.str().c_str());
652 error.SetErrorStringWithFormat(
653 "\"%s\" is not a pointer and -> was used to attempt to "
654 "access \"%s\". Did you mean \"%s.%s\"?",
655 var_expr_path_strm.GetData(), child_name.GetCString(),
656 var_expr_path_strm.GetData(), var_expr.str().c_str());
657 return ValueObjectSP();
660 child_valobj_sp = valobj_sp->GetChildMemberWithName(child_name, true);
661 if (!child_valobj_sp) {
662 if (!no_synth_child) {
663 child_valobj_sp = valobj_sp->GetSyntheticValue();
666 child_valobj_sp->GetChildMemberWithName(child_name, true);
669 if (no_synth_child || !child_valobj_sp) {
670 // No child member with name "child_name"
671 if (synthetically_added_instance_object) {
672 // We added a "this->" or "self->" to the beginning of the
674 // and this is the first pointer ivar access, so just return
677 error.SetErrorStringWithFormat(
678 "no variable or instance variable named '%s' found in "
680 name_const_string.GetCString());
682 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
684 error.SetErrorStringWithFormat(
685 "\"%s\" is not a member of \"(%s) %s\"",
686 child_name.GetCString(),
687 valobj_sp->GetTypeName().AsCString("<invalid type>"),
688 var_expr_path_strm.GetData());
690 error.SetErrorStringWithFormat(
691 "incomplete expression path after \"%s\" in \"%s\"",
692 var_expr_path_strm.GetData(),
693 original_var_expr.str().c_str());
696 return ValueObjectSP();
699 synthetically_added_instance_object = false;
700 // Remove the child name from the path
701 var_expr = var_expr.drop_front(child_name.GetLength());
702 if (use_dynamic != eNoDynamicValues) {
703 ValueObjectSP dynamic_value_sp(
704 child_valobj_sp->GetDynamicValue(use_dynamic));
705 if (dynamic_value_sp)
706 child_valobj_sp = dynamic_value_sp;
711 // Array member access, or treating pointer as an array
712 // Need at least two brackets and a number
713 if (var_expr.size() <= 2) {
714 error.SetErrorStringWithFormat(
715 "invalid square bracket encountered after \"%s\" in \"%s\"",
716 var_expr_path_strm.GetData(), var_expr.str().c_str());
717 return ValueObjectSP();
720 // Drop the open brace.
721 var_expr = var_expr.drop_front();
722 long child_index = 0;
724 // If there's no closing brace, this is an invalid expression.
725 size_t end_pos = var_expr.find_first_of(']');
726 if (end_pos == llvm::StringRef::npos) {
727 error.SetErrorStringWithFormat(
728 "missing closing square bracket in expression \"%s\"",
729 var_expr_path_strm.GetData());
730 return ValueObjectSP();
732 llvm::StringRef index_expr = var_expr.take_front(end_pos);
733 llvm::StringRef original_index_expr = index_expr;
734 // Drop all of "[index_expr]"
735 var_expr = var_expr.drop_front(end_pos + 1);
737 if (index_expr.consumeInteger(0, child_index)) {
738 // If there was no integer anywhere in the index expression, this is
739 // erroneous expression.
740 error.SetErrorStringWithFormat("invalid index expression \"%s\"",
741 index_expr.str().c_str());
742 return ValueObjectSP();
745 if (index_expr.empty()) {
746 // The entire index expression was a single integer.
748 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
749 // what we have is *ptr[low]. the most similar C++ syntax is to deref
750 // ptr and extract bit low out of it. reading array item low would be
751 // done by saying ptr[low], without a deref * sign
753 ValueObjectSP temp(valobj_sp->Dereference(error));
755 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
756 error.SetErrorStringWithFormat(
757 "could not dereference \"(%s) %s\"",
758 valobj_sp->GetTypeName().AsCString("<invalid type>"),
759 var_expr_path_strm.GetData());
760 return ValueObjectSP();
764 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() &&
766 // what we have is *arr[low]. the most similar C++ syntax is
768 // (an operation that is equivalent to deref-ing arr)
769 // and extract bit low out of it. reading array item low
770 // would be done by saying arr[low], without a deref * sign
772 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
774 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
775 error.SetErrorStringWithFormat(
776 "could not get item 0 for \"(%s) %s\"",
777 valobj_sp->GetTypeName().AsCString("<invalid type>"),
778 var_expr_path_strm.GetData());
779 return ValueObjectSP();
785 bool is_incomplete_array = false;
786 if (valobj_sp->IsPointerType()) {
787 bool is_objc_pointer = true;
789 if (valobj_sp->GetCompilerType().GetMinimumLanguage() !=
791 is_objc_pointer = false;
792 else if (!valobj_sp->GetCompilerType().IsPointerType())
793 is_objc_pointer = false;
795 if (no_synth_child && is_objc_pointer) {
796 error.SetErrorStringWithFormat(
797 "\"(%s) %s\" is an Objective-C pointer, and cannot be "
799 valobj_sp->GetTypeName().AsCString("<invalid type>"),
800 var_expr_path_strm.GetData());
802 return ValueObjectSP();
803 } else if (is_objc_pointer) {
804 // dereferencing ObjC variables is not valid.. so let's try
805 // and recur to synthetic children
806 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
807 if (!synthetic /* no synthetic */
808 || synthetic == valobj_sp) /* synthetic is the same as
809 the original object */
811 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
812 error.SetErrorStringWithFormat(
813 "\"(%s) %s\" is not an array type",
814 valobj_sp->GetTypeName().AsCString("<invalid type>"),
815 var_expr_path_strm.GetData());
817 static_cast<uint32_t>(child_index) >=
819 ->GetNumChildren() /* synthetic does not have that many values */) {
820 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
821 error.SetErrorStringWithFormat(
822 "array index %ld is not valid for \"(%s) %s\"", child_index,
823 valobj_sp->GetTypeName().AsCString("<invalid type>"),
824 var_expr_path_strm.GetData());
826 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
827 if (!child_valobj_sp) {
828 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
829 error.SetErrorStringWithFormat(
830 "array index %ld is not valid for \"(%s) %s\"", child_index,
831 valobj_sp->GetTypeName().AsCString("<invalid type>"),
832 var_expr_path_strm.GetData());
837 valobj_sp->GetSyntheticArrayMember(child_index, true);
838 if (!child_valobj_sp) {
839 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
840 error.SetErrorStringWithFormat(
841 "failed to use pointer as array for index %ld for "
844 valobj_sp->GetTypeName().AsCString("<invalid type>"),
845 var_expr_path_strm.GetData());
848 } else if (valobj_sp->GetCompilerType().IsArrayType(
849 nullptr, nullptr, &is_incomplete_array)) {
850 // Pass false to dynamic_value here so we can tell the
851 // difference between
852 // no dynamic value and no member of this type...
853 child_valobj_sp = valobj_sp->GetChildAtIndex(child_index, true);
854 if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
856 valobj_sp->GetSyntheticArrayMember(child_index, true);
858 if (!child_valobj_sp) {
859 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
860 error.SetErrorStringWithFormat(
861 "array index %ld is not valid for \"(%s) %s\"", child_index,
862 valobj_sp->GetTypeName().AsCString("<invalid type>"),
863 var_expr_path_strm.GetData());
865 } else if (valobj_sp->GetCompilerType().IsScalarType()) {
866 // this is a bitfield asking to display just one bit
867 child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(
868 child_index, child_index, true);
869 if (!child_valobj_sp) {
870 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
871 error.SetErrorStringWithFormat(
872 "bitfield range %ld-%ld is not valid for \"(%s) %s\"",
873 child_index, child_index,
874 valobj_sp->GetTypeName().AsCString("<invalid type>"),
875 var_expr_path_strm.GetData());
878 ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
879 if (no_synth_child /* synthetic is forbidden */ ||
880 !synthetic /* no synthetic */
881 || synthetic == valobj_sp) /* synthetic is the same as the
884 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
885 error.SetErrorStringWithFormat(
886 "\"(%s) %s\" is not an array type",
887 valobj_sp->GetTypeName().AsCString("<invalid type>"),
888 var_expr_path_strm.GetData());
890 static_cast<uint32_t>(child_index) >=
892 ->GetNumChildren() /* synthetic does not have that many values */) {
893 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
894 error.SetErrorStringWithFormat(
895 "array index %ld is not valid for \"(%s) %s\"", child_index,
896 valobj_sp->GetTypeName().AsCString("<invalid type>"),
897 var_expr_path_strm.GetData());
899 child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
900 if (!child_valobj_sp) {
901 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
902 error.SetErrorStringWithFormat(
903 "array index %ld is not valid for \"(%s) %s\"", child_index,
904 valobj_sp->GetTypeName().AsCString("<invalid type>"),
905 var_expr_path_strm.GetData());
910 if (!child_valobj_sp) {
911 // Invalid array index...
912 return ValueObjectSP();
915 separator_idx = var_expr.find_first_of(".-[");
916 if (use_dynamic != eNoDynamicValues) {
917 ValueObjectSP dynamic_value_sp(
918 child_valobj_sp->GetDynamicValue(use_dynamic));
919 if (dynamic_value_sp)
920 child_valobj_sp = dynamic_value_sp;
922 // Break out early from the switch since we were able to find the child
927 // this is most probably a BitField, let's take a look
928 if (index_expr.front() != '-') {
929 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
930 original_index_expr.str().c_str());
931 return ValueObjectSP();
934 index_expr = index_expr.drop_front();
935 long final_index = 0;
936 if (index_expr.getAsInteger(0, final_index)) {
937 error.SetErrorStringWithFormat("invalid range expression \"'%s'\"",
938 original_index_expr.str().c_str());
939 return ValueObjectSP();
942 // if the format given is [high-low], swap range
943 if (child_index > final_index) {
944 long temp = child_index;
945 child_index = final_index;
949 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref) {
950 // what we have is *ptr[low-high]. the most similar C++ syntax is to
951 // deref ptr and extract bits low thru high out of it. reading array
952 // items low thru high would be done by saying ptr[low-high], without
955 ValueObjectSP temp(valobj_sp->Dereference(error));
957 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
958 error.SetErrorStringWithFormat(
959 "could not dereference \"(%s) %s\"",
960 valobj_sp->GetTypeName().AsCString("<invalid type>"),
961 var_expr_path_strm.GetData());
962 return ValueObjectSP();
966 } else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref) {
967 // what we have is *arr[low-high]. the most similar C++ syntax is to get
968 // arr[0] (an operation that is equivalent to deref-ing arr) and extract
969 // bits low thru high out of it. reading array items low thru high would
970 // be done by saying arr[low-high], without a deref * sign
972 ValueObjectSP temp(valobj_sp->GetChildAtIndex(0, true));
974 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
975 error.SetErrorStringWithFormat(
976 "could not get item 0 for \"(%s) %s\"",
977 valobj_sp->GetTypeName().AsCString("<invalid type>"),
978 var_expr_path_strm.GetData());
979 return ValueObjectSP();
986 valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
987 if (!child_valobj_sp) {
988 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
989 error.SetErrorStringWithFormat(
990 "bitfield range %ld-%ld is not valid for \"(%s) %s\"", child_index,
991 final_index, valobj_sp->GetTypeName().AsCString("<invalid type>"),
992 var_expr_path_strm.GetData());
995 if (!child_valobj_sp) {
996 // Invalid bitfield range...
997 return ValueObjectSP();
1000 separator_idx = var_expr.find_first_of(".-[");
1001 if (use_dynamic != eNoDynamicValues) {
1002 ValueObjectSP dynamic_value_sp(
1003 child_valobj_sp->GetDynamicValue(use_dynamic));
1004 if (dynamic_value_sp)
1005 child_valobj_sp = dynamic_value_sp;
1007 // Break out early from the switch since we were able to find the child
1014 valobj_sp->GetExpressionPath(var_expr_path_strm, false);
1015 error.SetErrorStringWithFormat(
1016 "unexpected char '%c' encountered after \"%s\" in \"%s\"",
1017 separator_type, var_expr_path_strm.GetData(),
1018 var_expr.str().c_str());
1020 return ValueObjectSP();
1024 if (child_valobj_sp)
1025 valobj_sp = child_valobj_sp;
1027 if (var_expr.empty())
1032 ValueObjectSP deref_valobj_sp(valobj_sp->Dereference(error));
1033 valobj_sp = deref_valobj_sp;
1034 } else if (address_of) {
1035 ValueObjectSP address_of_valobj_sp(valobj_sp->AddressOf(error));
1036 valobj_sp = address_of_valobj_sp;
1042 bool StackFrame::GetFrameBaseValue(Scalar &frame_base, Error *error_ptr) {
1043 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1044 if (!m_cfa_is_valid) {
1045 m_frame_base_error.SetErrorString(
1046 "No frame base available for this historical stack frame.");
1050 if (m_flags.IsClear(GOT_FRAME_BASE)) {
1051 if (m_sc.function) {
1052 m_frame_base.Clear();
1053 m_frame_base_error.Clear();
1055 m_flags.Set(GOT_FRAME_BASE);
1056 ExecutionContext exe_ctx(shared_from_this());
1058 addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1059 if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1061 m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1062 exe_ctx.GetTargetPtr());
1064 if (m_sc.function->GetFrameBaseExpression().Evaluate(
1065 &exe_ctx, nullptr, nullptr, nullptr, loclist_base_addr, nullptr,
1066 nullptr, expr_value, &m_frame_base_error) == false) {
1067 // We should really have an error if evaluate returns, but in case
1068 // we don't, lets set the error to something at least.
1069 if (m_frame_base_error.Success())
1070 m_frame_base_error.SetErrorString(
1071 "Evaluation of the frame base expression failed.");
1073 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1076 m_frame_base_error.SetErrorString("No function in symbol context.");
1080 if (m_frame_base_error.Success())
1081 frame_base = m_frame_base;
1084 *error_ptr = m_frame_base_error;
1085 return m_frame_base_error.Success();
1088 DWARFExpression *StackFrame::GetFrameBaseExpression(Error *error_ptr) {
1089 if (!m_sc.function) {
1091 error_ptr->SetErrorString("No function in symbol context.");
1096 return &m_sc.function->GetFrameBaseExpression();
1099 RegisterContextSP StackFrame::GetRegisterContext() {
1100 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1101 if (!m_reg_context_sp) {
1102 ThreadSP thread_sp(GetThread());
1104 m_reg_context_sp = thread_sp->CreateRegisterContextForFrame(this);
1106 return m_reg_context_sp;
1109 bool StackFrame::HasDebugInformation() {
1110 GetSymbolContext(eSymbolContextLineEntry);
1111 return m_sc.line_entry.IsValid();
1115 StackFrame::GetValueObjectForFrameVariable(const VariableSP &variable_sp,
1116 DynamicValueType use_dynamic) {
1117 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1118 ValueObjectSP valobj_sp;
1119 if (m_is_history_frame) {
1122 VariableList *var_list = GetVariableList(true);
1124 // Make sure the variable is a frame variable
1125 const uint32_t var_idx = var_list->FindIndexForVariable(variable_sp.get());
1126 const uint32_t num_variables = var_list->GetSize();
1127 if (var_idx < num_variables) {
1128 valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex(var_idx);
1130 if (m_variable_list_value_objects.GetSize() < num_variables)
1131 m_variable_list_value_objects.Resize(num_variables);
1132 valobj_sp = ValueObjectVariable::Create(this, variable_sp);
1133 m_variable_list_value_objects.SetValueObjectAtIndex(var_idx, valobj_sp);
1137 if (use_dynamic != eNoDynamicValues && valobj_sp) {
1138 ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue(use_dynamic);
1145 ValueObjectSP StackFrame::TrackGlobalVariable(const VariableSP &variable_sp,
1146 DynamicValueType use_dynamic) {
1147 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1148 if (m_is_history_frame)
1149 return ValueObjectSP();
1151 // Check to make sure we aren't already tracking this variable?
1152 ValueObjectSP valobj_sp(
1153 GetValueObjectForFrameVariable(variable_sp, use_dynamic));
1155 // We aren't already tracking this global
1156 VariableList *var_list = GetVariableList(true);
1157 // If this frame has no variables, create a new list
1158 if (var_list == nullptr)
1159 m_variable_list_sp.reset(new VariableList());
1161 // Add the global/static variable to this frame
1162 m_variable_list_sp->AddVariable(variable_sp);
1164 // Now make a value object for it so we can track its changes
1165 valobj_sp = GetValueObjectForFrameVariable(variable_sp, use_dynamic);
1170 bool StackFrame::IsInlined() {
1171 if (m_sc.block == nullptr)
1172 GetSymbolContext(eSymbolContextBlock);
1174 return m_sc.block->GetContainingInlinedBlock() != nullptr;
1178 lldb::LanguageType StackFrame::GetLanguage() {
1179 CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1181 return cu->GetLanguage();
1182 return lldb::eLanguageTypeUnknown;
1185 lldb::LanguageType StackFrame::GuessLanguage() {
1186 LanguageType lang_type = GetLanguage();
1188 if (lang_type == eLanguageTypeUnknown) {
1189 Function *f = GetSymbolContext(eSymbolContextFunction).function;
1191 lang_type = f->GetMangled().GuessLanguage();
1199 std::pair<const Instruction::Operand *, int64_t>
1200 GetBaseExplainingValue(const Instruction::Operand &operand,
1201 RegisterContext ®ister_context, lldb::addr_t value) {
1202 switch (operand.m_type) {
1203 case Instruction::Operand::Type::Dereference:
1204 case Instruction::Operand::Type::Immediate:
1205 case Instruction::Operand::Type::Invalid:
1206 case Instruction::Operand::Type::Product:
1207 // These are not currently interesting
1208 return std::make_pair(nullptr, 0);
1209 case Instruction::Operand::Type::Sum: {
1210 const Instruction::Operand *immediate_child = nullptr;
1211 const Instruction::Operand *variable_child = nullptr;
1212 if (operand.m_children[0].m_type == Instruction::Operand::Type::Immediate) {
1213 immediate_child = &operand.m_children[0];
1214 variable_child = &operand.m_children[1];
1215 } else if (operand.m_children[1].m_type ==
1216 Instruction::Operand::Type::Immediate) {
1217 immediate_child = &operand.m_children[1];
1218 variable_child = &operand.m_children[0];
1220 if (!immediate_child) {
1221 return std::make_pair(nullptr, 0);
1223 lldb::addr_t adjusted_value = value;
1224 if (immediate_child->m_negative) {
1225 adjusted_value += immediate_child->m_immediate;
1227 adjusted_value -= immediate_child->m_immediate;
1229 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1230 GetBaseExplainingValue(*variable_child, register_context,
1232 if (!base_and_offset.first) {
1233 return std::make_pair(nullptr, 0);
1235 if (immediate_child->m_negative) {
1236 base_and_offset.second -= immediate_child->m_immediate;
1238 base_and_offset.second += immediate_child->m_immediate;
1240 return base_and_offset;
1242 case Instruction::Operand::Type::Register: {
1243 const RegisterInfo *info =
1244 register_context.GetRegisterInfoByName(operand.m_register.AsCString());
1246 return std::make_pair(nullptr, 0);
1248 RegisterValue reg_value;
1249 if (!register_context.ReadRegister(info, reg_value)) {
1250 return std::make_pair(nullptr, 0);
1252 if (reg_value.GetAsUInt64() == value) {
1253 return std::make_pair(&operand, 0);
1255 return std::make_pair(nullptr, 0);
1259 return std::make_pair(nullptr, 0);
1262 std::pair<const Instruction::Operand *, int64_t>
1263 GetBaseExplainingDereference(const Instruction::Operand &operand,
1264 RegisterContext ®ister_context,
1265 lldb::addr_t addr) {
1266 if (operand.m_type == Instruction::Operand::Type::Dereference) {
1267 return GetBaseExplainingValue(operand.m_children[0], register_context,
1270 return std::make_pair(nullptr, 0);
1274 lldb::ValueObjectSP StackFrame::GuessValueForAddress(lldb::addr_t addr) {
1275 TargetSP target_sp = CalculateTarget();
1277 const ArchSpec &target_arch = target_sp->GetArchitecture();
1279 AddressRange pc_range;
1280 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1281 pc_range.SetByteSize(target_arch.GetMaximumOpcodeByteSize());
1283 ExecutionContext exe_ctx(shared_from_this());
1285 const char *plugin_name = nullptr;
1286 const char *flavor = nullptr;
1287 const bool prefer_file_cache = false;
1289 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1290 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1292 if (!disassembler_sp->GetInstructionList().GetSize()) {
1293 return ValueObjectSP();
1296 InstructionSP instruction_sp =
1297 disassembler_sp->GetInstructionList().GetInstructionAtIndex(0);
1299 llvm::SmallVector<Instruction::Operand, 3> operands;
1301 if (!instruction_sp->ParseOperands(operands)) {
1302 return ValueObjectSP();
1305 RegisterContextSP register_context_sp = GetRegisterContext();
1307 if (!register_context_sp) {
1308 return ValueObjectSP();
1311 for (const Instruction::Operand &operand : operands) {
1312 std::pair<const Instruction::Operand *, int64_t> base_and_offset =
1313 GetBaseExplainingDereference(operand, *register_context_sp, addr);
1315 if (!base_and_offset.first) {
1319 switch (base_and_offset.first->m_type) {
1320 case Instruction::Operand::Type::Immediate: {
1321 lldb_private::Address addr;
1322 if (target_sp->ResolveLoadAddress(base_and_offset.first->m_immediate +
1323 base_and_offset.second,
1325 TypeSystem *c_type_system =
1326 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
1327 if (!c_type_system) {
1328 return ValueObjectSP();
1330 CompilerType void_ptr_type =
1332 ->GetBasicTypeFromAST(lldb::BasicType::eBasicTypeChar)
1334 return ValueObjectMemory::Create(this, "", addr, void_ptr_type);
1337 return ValueObjectSP();
1341 case Instruction::Operand::Type::Register: {
1342 return GuessValueForRegisterAndOffset(base_and_offset.first->m_register,
1343 base_and_offset.second);
1346 return ValueObjectSP();
1350 return ValueObjectSP();
1354 ValueObjectSP GetValueForOffset(StackFrame &frame, ValueObjectSP &parent,
1356 if (offset < 0 || uint64_t(offset) >= parent->GetByteSize()) {
1357 return ValueObjectSP();
1360 if (parent->IsPointerOrReferenceType()) {
1364 for (int ci = 0, ce = parent->GetNumChildren(); ci != ce; ++ci) {
1365 const bool can_create = true;
1366 ValueObjectSP child_sp = parent->GetChildAtIndex(ci, can_create);
1369 return ValueObjectSP();
1372 int64_t child_offset = child_sp->GetByteOffset();
1373 int64_t child_size = child_sp->GetByteSize();
1375 if (offset >= child_offset && offset < (child_offset + child_size)) {
1376 return GetValueForOffset(frame, child_sp, offset - child_offset);
1383 return ValueObjectSP();
1387 ValueObjectSP GetValueForDereferincingOffset(StackFrame &frame,
1388 ValueObjectSP &base,
1390 // base is a pointer to something
1391 // offset is the thing to add to the pointer
1392 // We return the most sensible ValueObject for the result of *(base+offset)
1394 if (!base->IsPointerOrReferenceType()) {
1395 return ValueObjectSP();
1399 ValueObjectSP pointee = base->Dereference(error);
1402 return ValueObjectSP();
1405 if (offset >= 0 && uint64_t(offset) >= pointee->GetByteSize()) {
1406 int64_t index = offset / pointee->GetByteSize();
1407 offset = offset % pointee->GetByteSize();
1408 const bool can_create = true;
1409 pointee = base->GetSyntheticArrayMember(index, can_create);
1412 if (!pointee || error.Fail()) {
1413 return ValueObjectSP();
1416 return GetValueForOffset(frame, pointee, offset);
1419 //------------------------------------------------------------------
1420 /// Attempt to reconstruct the ValueObject for the address contained in a
1421 /// given register plus an offset.
1423 /// @params [in] frame
1424 /// The current stack frame.
1426 /// @params [in] reg
1429 /// @params [in] offset
1430 /// The offset from the register.
1432 /// @param [in] disassembler
1433 /// A disassembler containing instructions valid up to the current PC.
1435 /// @param [in] variables
1436 /// The variable list from the current frame,
1439 /// The program counter for the instruction considered the 'user'.
1442 /// A string describing the base for the ExpressionPath. This could be a
1443 /// variable, a register value, an argument, or a function return value.
1444 /// The ValueObject if found. If valid, it has a valid ExpressionPath.
1445 //------------------------------------------------------------------
1446 lldb::ValueObjectSP DoGuessValueAt(StackFrame &frame, ConstString reg,
1447 int64_t offset, Disassembler &disassembler,
1448 VariableList &variables, const Address &pc) {
1449 // Example of operation for Intel:
1451 // +14: movq -0x8(%rbp), %rdi
1452 // +18: movq 0x8(%rdi), %rdi
1453 // +22: addl 0x4(%rdi), %eax
1455 // f, a pointer to a struct, is known to be at -0x8(%rbp).
1457 // DoGuessValueAt(frame, rdi, 4, dis, vars, 0x22) finds the instruction at +18
1458 // that assigns to rdi, and calls itself recursively for that dereference
1459 // DoGuessValueAt(frame, rdi, 8, dis, vars, 0x18) finds the instruction at
1460 // +14 that assigns to rdi, and calls itself recursively for that
1462 // DoGuessValueAt(frame, rbp, -8, dis, vars, 0x14) finds "f" in the
1464 // Returns a ValueObject for f. (That's what was stored at rbp-8 at +14)
1465 // Returns a ValueObject for *(f+8) or f->b (That's what was stored at rdi+8
1467 // Returns a ValueObject for *(f->b+4) or f->b->a (That's what was stored at
1470 // First, check the variable list to see if anything is at the specified
1473 using namespace OperandMatchers;
1475 const RegisterInfo *reg_info =
1476 frame.GetRegisterContext()->GetRegisterInfoByName(reg.AsCString());
1478 return ValueObjectSP();
1481 Instruction::Operand op =
1482 offset ? Instruction::Operand::BuildDereference(
1483 Instruction::Operand::BuildSum(
1484 Instruction::Operand::BuildRegister(reg),
1485 Instruction::Operand::BuildImmediate(offset)))
1486 : Instruction::Operand::BuildDereference(
1487 Instruction::Operand::BuildRegister(reg));
1489 for (size_t vi = 0, ve = variables.GetSize(); vi != ve; ++vi) {
1490 VariableSP var_sp = variables.GetVariableAtIndex(vi);
1491 if (var_sp->LocationExpression().MatchesOperand(frame, op)) {
1492 return frame.GetValueObjectForFrameVariable(var_sp, eNoDynamicValues);
1496 const uint32_t current_inst =
1497 disassembler.GetInstructionList().GetIndexOfInstructionAtAddress(pc);
1498 if (current_inst == UINT32_MAX) {
1499 return ValueObjectSP();
1502 for (uint32_t ii = current_inst - 1; ii != (uint32_t)-1; --ii) {
1503 // This is not an exact algorithm, and it sacrifices accuracy for
1504 // generality. Recognizing "mov" and "ld" instructions –– and which are
1505 // their source and destination operands -- is something the disassembler
1506 // should do for us.
1507 InstructionSP instruction_sp =
1508 disassembler.GetInstructionList().GetInstructionAtIndex(ii);
1510 if (instruction_sp->IsCall()) {
1511 ABISP abi_sp = frame.CalculateProcess()->GetABI();
1516 const char *return_register_name;
1517 if (!abi_sp->GetPointerReturnRegister(return_register_name)) {
1521 const RegisterInfo *return_register_info =
1522 frame.GetRegisterContext()->GetRegisterInfoByName(
1523 return_register_name);
1524 if (!return_register_info) {
1530 if (!MatchUnaryOp(MatchOpType(Instruction::Operand::Type::Dereference),
1531 MatchRegOp(*return_register_info))(op) &&
1533 MatchOpType(Instruction::Operand::Type::Dereference),
1534 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1535 MatchRegOp(*return_register_info),
1536 FetchImmOp(offset)))(op)) {
1540 llvm::SmallVector<Instruction::Operand, 1> operands;
1541 if (!instruction_sp->ParseOperands(operands) || operands.size() != 1) {
1545 switch (operands[0].m_type) {
1548 case Instruction::Operand::Type::Immediate: {
1550 Address load_address;
1551 if (!frame.CalculateTarget()->ResolveLoadAddress(
1552 operands[0].m_immediate, load_address)) {
1555 frame.CalculateTarget()->GetImages().ResolveSymbolContextForAddress(
1556 load_address, eSymbolContextFunction, sc);
1560 CompilerType function_type = sc.function->GetCompilerType();
1561 if (!function_type.IsFunctionType()) {
1564 CompilerType return_type = function_type.GetFunctionReturnType();
1565 RegisterValue return_value;
1566 if (!frame.GetRegisterContext()->ReadRegister(return_register_info,
1570 std::string name_str(
1571 sc.function->GetName().AsCString("<unknown function>"));
1572 name_str.append("()");
1573 Address return_value_address(return_value.GetAsUInt64());
1574 ValueObjectSP return_value_sp = ValueObjectMemory::Create(
1575 &frame, name_str, return_value_address, return_type);
1576 return GetValueForDereferincingOffset(frame, return_value_sp, offset);
1583 llvm::SmallVector<Instruction::Operand, 2> operands;
1584 if (!instruction_sp->ParseOperands(operands) || operands.size() != 2) {
1588 Instruction::Operand *origin_operand = nullptr;
1589 auto clobbered_reg_matcher = [reg_info](const Instruction::Operand &op) {
1590 return MatchRegOp(*reg_info)(op) && op.m_clobbered;
1593 if (clobbered_reg_matcher(operands[0])) {
1594 origin_operand = &operands[1];
1596 else if (clobbered_reg_matcher(operands[1])) {
1597 origin_operand = &operands[0];
1603 // We have an origin operand. Can we track its value down?
1604 ValueObjectSP source_path;
1605 ConstString origin_register;
1606 int64_t origin_offset = 0;
1608 if (FetchRegOp(origin_register)(*origin_operand)) {
1609 source_path = DoGuessValueAt(frame, origin_register, 0, disassembler,
1610 variables, instruction_sp->GetAddress());
1611 } else if (MatchUnaryOp(
1612 MatchOpType(Instruction::Operand::Type::Dereference),
1613 FetchRegOp(origin_register))(*origin_operand) ||
1615 MatchOpType(Instruction::Operand::Type::Dereference),
1616 MatchBinaryOp(MatchOpType(Instruction::Operand::Type::Sum),
1617 FetchRegOp(origin_register),
1618 FetchImmOp(origin_offset)))(*origin_operand)) {
1620 DoGuessValueAt(frame, origin_register, origin_offset, disassembler,
1621 variables, instruction_sp->GetAddress());
1626 GetValueForDereferincingOffset(frame, source_path, offset);
1634 return ValueObjectSP();
1638 lldb::ValueObjectSP StackFrame::GuessValueForRegisterAndOffset(ConstString reg,
1640 TargetSP target_sp = CalculateTarget();
1642 const ArchSpec &target_arch = target_sp->GetArchitecture();
1644 Block *frame_block = GetFrameBlock();
1647 return ValueObjectSP();
1650 Function *function = frame_block->CalculateSymbolContextFunction();
1652 return ValueObjectSP();
1655 AddressRange pc_range = function->GetAddressRange();
1657 if (GetFrameCodeAddress().GetFileAddress() <
1658 pc_range.GetBaseAddress().GetFileAddress() ||
1659 GetFrameCodeAddress().GetFileAddress() -
1660 pc_range.GetBaseAddress().GetFileAddress() >=
1661 pc_range.GetByteSize()) {
1662 return ValueObjectSP();
1665 ExecutionContext exe_ctx(shared_from_this());
1667 const char *plugin_name = nullptr;
1668 const char *flavor = nullptr;
1669 const bool prefer_file_cache = false;
1670 DisassemblerSP disassembler_sp = Disassembler::DisassembleRange(
1671 target_arch, plugin_name, flavor, exe_ctx, pc_range, prefer_file_cache);
1673 if (!disassembler_sp || !disassembler_sp->GetInstructionList().GetSize()) {
1674 return ValueObjectSP();
1677 const bool get_file_globals = false;
1678 VariableList *variables = GetVariableList(get_file_globals);
1681 return ValueObjectSP();
1684 return DoGuessValueAt(*this, reg, offset, *disassembler_sp, *variables,
1685 GetFrameCodeAddress());
1688 TargetSP StackFrame::CalculateTarget() {
1690 ThreadSP thread_sp(GetThread());
1692 ProcessSP process_sp(thread_sp->CalculateProcess());
1694 target_sp = process_sp->CalculateTarget();
1699 ProcessSP StackFrame::CalculateProcess() {
1700 ProcessSP process_sp;
1701 ThreadSP thread_sp(GetThread());
1703 process_sp = thread_sp->CalculateProcess();
1707 ThreadSP StackFrame::CalculateThread() { return GetThread(); }
1709 StackFrameSP StackFrame::CalculateStackFrame() { return shared_from_this(); }
1711 void StackFrame::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1712 exe_ctx.SetContext(shared_from_this());
1715 void StackFrame::DumpUsingSettingsFormat(Stream *strm,
1716 const char *frame_marker) {
1717 if (strm == nullptr)
1720 GetSymbolContext(eSymbolContextEverything);
1721 ExecutionContext exe_ctx(shared_from_this());
1725 s.PutCString(frame_marker);
1727 const FormatEntity::Entry *frame_format = nullptr;
1728 Target *target = exe_ctx.GetTargetPtr();
1730 frame_format = target->GetDebugger().GetFrameFormat();
1731 if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx,
1732 nullptr, nullptr, false, false)) {
1733 strm->PutCString(s.GetString());
1735 Dump(strm, true, false);
1740 void StackFrame::Dump(Stream *strm, bool show_frame_index,
1741 bool show_fullpaths) {
1742 if (strm == nullptr)
1745 if (show_frame_index)
1746 strm->Printf("frame #%u: ", m_frame_index);
1747 ExecutionContext exe_ctx(shared_from_this());
1748 Target *target = exe_ctx.GetTargetPtr();
1749 strm->Printf("0x%0*" PRIx64 " ",
1750 target ? (target->GetArchitecture().GetAddressByteSize() * 2)
1752 GetFrameCodeAddress().GetLoadAddress(target));
1753 GetSymbolContext(eSymbolContextEverything);
1754 const bool show_module = true;
1755 const bool show_inline = true;
1756 const bool show_function_arguments = true;
1757 const bool show_function_name = true;
1758 m_sc.DumpStopContext(strm, exe_ctx.GetBestExecutionContextScope(),
1759 GetFrameCodeAddress(), show_fullpaths, show_module,
1760 show_inline, show_function_arguments,
1761 show_function_name);
1764 void StackFrame::UpdateCurrentFrameFromPreviousFrame(StackFrame &prev_frame) {
1765 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1766 assert(GetStackID() ==
1767 prev_frame.GetStackID()); // TODO: remove this after some testing
1768 m_variable_list_sp = prev_frame.m_variable_list_sp;
1769 m_variable_list_value_objects.Swap(prev_frame.m_variable_list_value_objects);
1770 if (!m_disassembly.GetString().empty()) {
1771 m_disassembly.Clear();
1772 m_disassembly.PutCString(prev_frame.m_disassembly.GetString());
1776 void StackFrame::UpdatePreviousFrameFromCurrentFrame(StackFrame &curr_frame) {
1777 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1778 assert(GetStackID() ==
1779 curr_frame.GetStackID()); // TODO: remove this after some testing
1780 m_id.SetPC(curr_frame.m_id.GetPC()); // Update the Stack ID PC value
1781 assert(GetThread() == curr_frame.GetThread());
1782 m_frame_index = curr_frame.m_frame_index;
1783 m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1784 m_reg_context_sp = curr_frame.m_reg_context_sp;
1785 m_frame_code_addr = curr_frame.m_frame_code_addr;
1786 assert(!m_sc.target_sp || !curr_frame.m_sc.target_sp ||
1787 m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1788 assert(!m_sc.module_sp || !curr_frame.m_sc.module_sp ||
1789 m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1790 assert(m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr ||
1791 m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1792 assert(m_sc.function == nullptr || curr_frame.m_sc.function == nullptr ||
1793 m_sc.function == curr_frame.m_sc.function);
1794 m_sc = curr_frame.m_sc;
1795 m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1796 m_flags.Set(m_sc.GetResolvedMask());
1797 m_frame_base.Clear();
1798 m_frame_base_error.Clear();
1801 bool StackFrame::HasCachedData() const {
1802 if (m_variable_list_sp)
1804 if (m_variable_list_value_objects.GetSize() > 0)
1806 if (!m_disassembly.GetString().empty())
1811 bool StackFrame::GetStatus(Stream &strm, bool show_frame_info, bool show_source,
1812 const char *frame_marker) {
1814 if (show_frame_info) {
1816 DumpUsingSettingsFormat(&strm, frame_marker);
1820 ExecutionContext exe_ctx(shared_from_this());
1821 bool have_source = false, have_debuginfo = false;
1822 Debugger::StopDisassemblyType disasm_display =
1823 Debugger::eStopDisassemblyTypeNever;
1824 Target *target = exe_ctx.GetTargetPtr();
1826 Debugger &debugger = target->GetDebugger();
1827 const uint32_t source_lines_before =
1828 debugger.GetStopSourceLineCount(true);
1829 const uint32_t source_lines_after =
1830 debugger.GetStopSourceLineCount(false);
1831 disasm_display = debugger.GetStopDisassemblyDisplay();
1833 GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1834 if (m_sc.comp_unit && m_sc.line_entry.IsValid()) {
1835 have_debuginfo = true;
1836 if (source_lines_before > 0 || source_lines_after > 0) {
1838 target->GetSourceManager().DisplaySourceLinesWithLineNumbers(
1839 m_sc.line_entry.file, m_sc.line_entry.line,
1840 m_sc.line_entry.column, source_lines_before,
1841 source_lines_after, "->", &strm);
1844 // TODO: Give here a one time warning if source file is missing.
1847 switch (disasm_display) {
1848 case Debugger::eStopDisassemblyTypeNever:
1851 case Debugger::eStopDisassemblyTypeNoDebugInfo:
1856 case Debugger::eStopDisassemblyTypeNoSource:
1861 case Debugger::eStopDisassemblyTypeAlways:
1863 const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1864 if (disasm_lines > 0) {
1865 const ArchSpec &target_arch = target->GetArchitecture();
1866 AddressRange pc_range;
1867 pc_range.GetBaseAddress() = GetFrameCodeAddress();
1868 pc_range.SetByteSize(disasm_lines *
1869 target_arch.GetMaximumOpcodeByteSize());
1870 const char *plugin_name = nullptr;
1871 const char *flavor = nullptr;
1872 const bool mixed_source_and_assembly = false;
1873 Disassembler::Disassemble(
1874 target->GetDebugger(), target_arch, plugin_name, flavor,
1875 exe_ctx, pc_range, disasm_lines, mixed_source_and_assembly, 0,
1876 Disassembler::eOptionMarkPCAddress, strm);