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