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/ValueObjectVariable.h"
22 #include "lldb/Core/ValueObjectConstResult.h"
23 #include "lldb/Symbol/CompileUnit.h"
24 #include "lldb/Symbol/Function.h"
25 #include "lldb/Symbol/Symbol.h"
26 #include "lldb/Symbol/SymbolContextScope.h"
27 #include "lldb/Symbol/Type.h"
28 #include "lldb/Symbol/VariableList.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/RegisterContext.h"
32 #include "lldb/Target/Target.h"
33 #include "lldb/Target/Thread.h"
34 
35 using namespace lldb;
36 using namespace lldb_private;
37 
38 // The first bits in the flags are reserved for the SymbolContext::Scope bits
39 // so we know if we have tried to look up information in our internal symbol
40 // context (m_sc) already.
41 #define RESOLVED_FRAME_CODE_ADDR        (uint32_t(eSymbolContextEverything + 1))
42 #define RESOLVED_FRAME_ID_SYMBOL_SCOPE  (RESOLVED_FRAME_CODE_ADDR << 1)
43 #define GOT_FRAME_BASE                  (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
44 #define RESOLVED_VARIABLES              (GOT_FRAME_BASE << 1)
45 #define RESOLVED_GLOBAL_VARIABLES       (RESOLVED_VARIABLES << 1)
46 
47 StackFrame::StackFrame (const ThreadSP &thread_sp,
48                         user_id_t frame_idx,
49                         user_id_t unwind_frame_index,
50                         addr_t cfa,
51                         bool cfa_is_valid,
52                         addr_t pc,
53                         uint32_t stop_id,
54                         bool stop_id_is_valid,
55                         bool is_history_frame,
56                         const SymbolContext *sc_ptr) :
57     m_thread_wp (thread_sp),
58     m_frame_index (frame_idx),
59     m_concrete_frame_index (unwind_frame_index),
60     m_reg_context_sp (),
61     m_id(pc, cfa, nullptr),
62     m_frame_code_addr (pc),
63     m_sc (),
64     m_flags (),
65     m_frame_base (),
66     m_frame_base_error (),
67     m_cfa_is_valid (cfa_is_valid),
68     m_stop_id  (stop_id),
69     m_stop_id_is_valid (stop_id_is_valid),
70     m_is_history_frame (is_history_frame),
71     m_variable_list_sp (),
72     m_variable_list_value_objects (),
73     m_disassembly (),
74     m_mutex (Mutex::eMutexTypeRecursive)
75 {
76     // If we don't have a CFA value, use the frame index for our StackID so that recursive
77     // functions properly aren't confused with one another on a history stack.
78     if (m_is_history_frame && !m_cfa_is_valid)
79     {
80         m_id.SetCFA (m_frame_index);
81     }
82 
83     if (sc_ptr != nullptr)
84     {
85         m_sc = *sc_ptr;
86         m_flags.Set(m_sc.GetResolvedMask ());
87     }
88 }
89 
90 StackFrame::StackFrame (const ThreadSP &thread_sp,
91                         user_id_t frame_idx,
92                         user_id_t unwind_frame_index,
93                         const RegisterContextSP &reg_context_sp,
94                         addr_t cfa,
95                         addr_t pc,
96                         const SymbolContext *sc_ptr) :
97     m_thread_wp (thread_sp),
98     m_frame_index (frame_idx),
99     m_concrete_frame_index (unwind_frame_index),
100     m_reg_context_sp (reg_context_sp),
101     m_id(pc, cfa, nullptr),
102     m_frame_code_addr (pc),
103     m_sc (),
104     m_flags (),
105     m_frame_base (),
106     m_frame_base_error (),
107     m_cfa_is_valid (true),
108     m_stop_id  (0),
109     m_stop_id_is_valid (false),
110     m_is_history_frame (false),
111     m_variable_list_sp (),
112     m_variable_list_value_objects (),
113     m_disassembly (),
114     m_mutex (Mutex::eMutexTypeRecursive)
115 {
116     if (sc_ptr != nullptr)
117     {
118         m_sc = *sc_ptr;
119         m_flags.Set(m_sc.GetResolvedMask ());
120     }
121 
122     if (reg_context_sp && !m_sc.target_sp)
123     {
124         m_sc.target_sp = reg_context_sp->CalculateTarget();
125         if (m_sc.target_sp)
126             m_flags.Set (eSymbolContextTarget);
127     }
128 }
129 
130 StackFrame::StackFrame (const ThreadSP &thread_sp,
131                         user_id_t frame_idx,
132                         user_id_t unwind_frame_index,
133                         const RegisterContextSP &reg_context_sp,
134                         addr_t cfa,
135                         const Address& pc_addr,
136                         const SymbolContext *sc_ptr) :
137     m_thread_wp (thread_sp),
138     m_frame_index (frame_idx),
139     m_concrete_frame_index (unwind_frame_index),
140     m_reg_context_sp (reg_context_sp),
141     m_id(pc_addr.GetLoadAddress(thread_sp->CalculateTarget().get()), cfa, nullptr),
142     m_frame_code_addr (pc_addr),
143     m_sc (),
144     m_flags (),
145     m_frame_base (),
146     m_frame_base_error (),
147     m_cfa_is_valid (true),
148     m_stop_id  (0),
149     m_stop_id_is_valid (false),
150     m_is_history_frame (false),
151     m_variable_list_sp (),
152     m_variable_list_value_objects (),
153     m_disassembly (),
154     m_mutex (Mutex::eMutexTypeRecursive)
155 {
156     if (sc_ptr != nullptr)
157     {
158         m_sc = *sc_ptr;
159         m_flags.Set(m_sc.GetResolvedMask ());
160     }
161 
162     if (!m_sc.target_sp && reg_context_sp)
163     {
164         m_sc.target_sp = reg_context_sp->CalculateTarget();
165         if (m_sc.target_sp)
166             m_flags.Set (eSymbolContextTarget);
167     }
168 
169     ModuleSP pc_module_sp (pc_addr.GetModule());
170     if (!m_sc.module_sp || m_sc.module_sp != pc_module_sp)
171     {
172         if (pc_module_sp)
173         {
174             m_sc.module_sp = pc_module_sp;
175             m_flags.Set (eSymbolContextModule);
176         }
177         else
178         {
179             m_sc.module_sp.reset();
180         }
181     }
182 }
183 
184 StackFrame::~StackFrame() = default;
185 
186 StackID&
187 StackFrame::GetStackID()
188 {
189     Mutex::Locker locker(m_mutex);
190     // Make sure we have resolved the StackID object's symbol context scope if
191     // we already haven't looked it up.
192 
193     if (m_flags.IsClear (RESOLVED_FRAME_ID_SYMBOL_SCOPE))
194     {
195         if (m_id.GetSymbolContextScope ())
196         {
197             // We already have a symbol context scope, we just don't have our
198             // flag bit set.
199             m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
200         }
201         else
202         {
203             // Calculate the frame block and use this for the stack ID symbol
204             // context scope if we have one.
205             SymbolContextScope *scope = GetFrameBlock ();
206             if (scope == nullptr)
207             {
208                 // We don't have a block, so use the symbol
209                 if (m_flags.IsClear (eSymbolContextSymbol))
210                     GetSymbolContext (eSymbolContextSymbol);
211 
212                 // It is ok if m_sc.symbol is nullptr here
213                 scope = m_sc.symbol;
214             }
215             // Set the symbol context scope (the accessor will set the
216             // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
217             SetSymbolContextScope (scope);
218         }
219     }
220     return m_id;
221 }
222 
223 uint32_t
224 StackFrame::GetFrameIndex () const
225 {
226     ThreadSP thread_sp = GetThread();
227     if (thread_sp)
228         return thread_sp->GetStackFrameList()->GetVisibleStackFrameIndex(m_frame_index);
229     else
230         return m_frame_index;
231 }
232 
233 void
234 StackFrame::SetSymbolContextScope (SymbolContextScope *symbol_scope)
235 {
236     Mutex::Locker locker(m_mutex);
237     m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
238     m_id.SetSymbolContextScope (symbol_scope);
239 }
240 
241 const Address&
242 StackFrame::GetFrameCodeAddress()
243 {
244     Mutex::Locker locker(m_mutex);
245     if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) && !m_frame_code_addr.IsSectionOffset())
246     {
247         m_flags.Set (RESOLVED_FRAME_CODE_ADDR);
248 
249         // Resolve the PC into a temporary address because if ResolveLoadAddress
250         // fails to resolve the address, it will clear the address object...
251         ThreadSP thread_sp (GetThread());
252         if (thread_sp)
253         {
254             TargetSP target_sp (thread_sp->CalculateTarget());
255             if (target_sp)
256             {
257                 if (m_frame_code_addr.SetOpcodeLoadAddress (m_frame_code_addr.GetOffset(), target_sp.get(), eAddressClassCode))
258                 {
259                     ModuleSP module_sp (m_frame_code_addr.GetModule());
260                     if (module_sp)
261                     {
262                         m_sc.module_sp = module_sp;
263                         m_flags.Set(eSymbolContextModule);
264                     }
265                 }
266             }
267         }
268     }
269     return m_frame_code_addr;
270 }
271 
272 bool
273 StackFrame::ChangePC (addr_t pc)
274 {
275     Mutex::Locker locker(m_mutex);
276     // We can't change the pc value of a history stack frame - it is immutable.
277     if (m_is_history_frame)
278         return false;
279     m_frame_code_addr.SetRawAddress(pc);
280     m_sc.Clear(false);
281     m_flags.Reset(0);
282     ThreadSP thread_sp (GetThread());
283     if (thread_sp)
284         thread_sp->ClearStackFrames ();
285     return true;
286 }
287 
288 const char *
289 StackFrame::Disassemble ()
290 {
291     Mutex::Locker locker(m_mutex);
292     if (m_disassembly.GetSize() == 0)
293     {
294         ExecutionContext exe_ctx (shared_from_this());
295         Target *target = exe_ctx.GetTargetPtr();
296         if (target)
297         {
298             const char *plugin_name = nullptr;
299             const char *flavor = nullptr;
300             Disassembler::Disassemble (target->GetDebugger(),
301                                        target->GetArchitecture(),
302                                        plugin_name,
303                                        flavor,
304                                        exe_ctx,
305                                        0,
306                                        0,
307                                        0,
308                                        m_disassembly);
309         }
310         if (m_disassembly.GetSize() == 0)
311             return nullptr;
312     }
313     return m_disassembly.GetData();
314 }
315 
316 Block *
317 StackFrame::GetFrameBlock ()
318 {
319     if (m_sc.block == nullptr && m_flags.IsClear(eSymbolContextBlock))
320         GetSymbolContext (eSymbolContextBlock);
321 
322     if (m_sc.block)
323     {
324         Block *inline_block = m_sc.block->GetContainingInlinedBlock();
325         if (inline_block)
326         {
327             // Use the block with the inlined function info
328             // as the frame block we want this frame to have only the variables
329             // for the inlined function and its non-inlined block child blocks.
330             return inline_block;
331         }
332         else
333         {
334             // This block is not contained withing any inlined function blocks
335             // with so we want to use the top most function block.
336             return &m_sc.function->GetBlock (false);
337         }
338     }
339     return nullptr;
340 }
341 
342 //----------------------------------------------------------------------
343 // Get the symbol context if we already haven't done so by resolving the
344 // PC address as much as possible. This way when we pass around a
345 // StackFrame object, everyone will have as much information as
346 // possible and no one will ever have to look things up manually.
347 //----------------------------------------------------------------------
348 const SymbolContext&
349 StackFrame::GetSymbolContext (uint32_t resolve_scope)
350 {
351     Mutex::Locker locker(m_mutex);
352     // Copy our internal symbol context into "sc".
353     if ((m_flags.Get() & resolve_scope) != resolve_scope)
354     {
355         uint32_t resolved = 0;
356 
357         // If the target was requested add that:
358         if (!m_sc.target_sp)
359         {
360             m_sc.target_sp = CalculateTarget();
361             if (m_sc.target_sp)
362                 resolved |= eSymbolContextTarget;
363         }
364 
365         // Resolve our PC to section offset if we haven't already done so
366         // and if we don't have a module. The resolved address section will
367         // contain the module to which it belongs
368         if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
369             GetFrameCodeAddress();
370 
371         // If this is not frame zero, then we need to subtract 1 from the PC
372         // value when doing address lookups since the PC will be on the
373         // instruction following the function call instruction...
374 
375         Address lookup_addr(GetFrameCodeAddress());
376         if (m_frame_index > 0 && lookup_addr.IsValid())
377         {
378             addr_t offset = lookup_addr.GetOffset();
379             if (offset > 0)
380             {
381                 lookup_addr.SetOffset(offset - 1);
382 
383             }
384             else
385             {
386                 // lookup_addr is the start of a section.  We need
387                 // do the math on the actual load address and re-compute
388                 // the section.  We're working with a 'noreturn' function
389                 // at the end of a section.
390                 ThreadSP thread_sp (GetThread());
391                 if (thread_sp)
392                 {
393                     TargetSP target_sp (thread_sp->CalculateTarget());
394                     if (target_sp)
395                     {
396                         addr_t addr_minus_one = lookup_addr.GetLoadAddress(target_sp.get()) - 1;
397                         lookup_addr.SetLoadAddress (addr_minus_one, target_sp.get());
398                     }
399                     else
400                     {
401                     lookup_addr.SetOffset(offset - 1);
402                     }
403                 }
404             }
405         }
406 
407         if (m_sc.module_sp)
408         {
409             // We have something in our stack frame symbol context, lets check
410             // if we haven't already tried to lookup one of those things. If we
411             // haven't then we will do the query.
412 
413             uint32_t actual_resolve_scope = 0;
414 
415             if (resolve_scope & eSymbolContextCompUnit)
416             {
417                 if (m_flags.IsClear (eSymbolContextCompUnit))
418                 {
419                     if (m_sc.comp_unit)
420                         resolved |= eSymbolContextCompUnit;
421                     else
422                         actual_resolve_scope |= eSymbolContextCompUnit;
423                 }
424             }
425 
426             if (resolve_scope & eSymbolContextFunction)
427             {
428                 if (m_flags.IsClear (eSymbolContextFunction))
429                 {
430                     if (m_sc.function)
431                         resolved |= eSymbolContextFunction;
432                     else
433                         actual_resolve_scope |= eSymbolContextFunction;
434                 }
435             }
436 
437             if (resolve_scope & eSymbolContextBlock)
438             {
439                 if (m_flags.IsClear (eSymbolContextBlock))
440                 {
441                     if (m_sc.block)
442                         resolved |= eSymbolContextBlock;
443                     else
444                         actual_resolve_scope |= eSymbolContextBlock;
445                 }
446             }
447 
448             if (resolve_scope & eSymbolContextSymbol)
449             {
450                 if (m_flags.IsClear (eSymbolContextSymbol))
451                 {
452                     if (m_sc.symbol)
453                         resolved |= eSymbolContextSymbol;
454                     else
455                         actual_resolve_scope |= eSymbolContextSymbol;
456                 }
457             }
458 
459             if (resolve_scope & eSymbolContextLineEntry)
460             {
461                 if (m_flags.IsClear (eSymbolContextLineEntry))
462                 {
463                     if (m_sc.line_entry.IsValid())
464                         resolved |= eSymbolContextLineEntry;
465                     else
466                         actual_resolve_scope |= eSymbolContextLineEntry;
467                 }
468             }
469 
470             if (actual_resolve_scope)
471             {
472                 // We might be resolving less information than what is already
473                 // in our current symbol context so resolve into a temporary
474                 // symbol context "sc" so we don't clear out data we have
475                 // already found in "m_sc"
476                 SymbolContext sc;
477                 // Set flags that indicate what we have tried to resolve
478                 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress (lookup_addr, actual_resolve_scope, sc);
479                 // Only replace what we didn't already have as we may have
480                 // information for an inlined function scope that won't match
481                 // what a standard lookup by address would match
482                 if ((resolved & eSymbolContextCompUnit)  && m_sc.comp_unit == nullptr)
483                     m_sc.comp_unit = sc.comp_unit;
484                 if ((resolved & eSymbolContextFunction)  && m_sc.function == nullptr)
485                     m_sc.function = sc.function;
486                 if ((resolved & eSymbolContextBlock)     && m_sc.block == nullptr)
487                     m_sc.block = sc.block;
488                 if ((resolved & eSymbolContextSymbol)    && m_sc.symbol == nullptr)
489                     m_sc.symbol = sc.symbol;
490                 if ((resolved & eSymbolContextLineEntry) && !m_sc.line_entry.IsValid())
491                 {
492                     m_sc.line_entry = sc.line_entry;
493                     m_sc.line_entry.ApplyFileMappings(m_sc.target_sp);
494                 }
495             }
496         }
497         else
498         {
499             // If we don't have a module, then we can't have the compile unit,
500             // function, block, line entry or symbol, so we can safely call
501             // ResolveSymbolContextForAddress with our symbol context member m_sc.
502             if (m_sc.target_sp)
503             {
504                 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress (lookup_addr, resolve_scope, m_sc);
505             }
506         }
507 
508         // Update our internal flags so we remember what we have tried to locate so
509         // we don't have to keep trying when more calls to this function are made.
510         // We might have dug up more information that was requested (for example
511         // if we were asked to only get the block, we will have gotten the
512         // compile unit, and function) so set any additional bits that we resolved
513         m_flags.Set (resolve_scope | resolved);
514     }
515 
516     // Return the symbol context with everything that was possible to resolve
517     // resolved.
518     return m_sc;
519 }
520 
521 VariableList *
522 StackFrame::GetVariableList (bool get_file_globals)
523 {
524     Mutex::Locker locker(m_mutex);
525     if (m_flags.IsClear(RESOLVED_VARIABLES))
526     {
527         m_flags.Set(RESOLVED_VARIABLES);
528 
529         Block *frame_block = GetFrameBlock();
530 
531         if (frame_block)
532         {
533             const bool get_child_variables = true;
534             const bool can_create = true;
535             const bool stop_if_child_block_is_inlined_function = true;
536             m_variable_list_sp.reset(new VariableList());
537             frame_block->AppendBlockVariables(can_create,
538                                               get_child_variables,
539                                               stop_if_child_block_is_inlined_function,
540                                               [this](Variable* v) { return true; },
541                                               m_variable_list_sp.get());
542         }
543     }
544 
545     if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) &&
546         get_file_globals)
547     {
548         m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
549 
550         if (m_flags.IsClear (eSymbolContextCompUnit))
551             GetSymbolContext (eSymbolContextCompUnit);
552 
553         if (m_sc.comp_unit)
554         {
555             VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
556             if (m_variable_list_sp)
557                 m_variable_list_sp->AddVariables (global_variable_list_sp.get());
558             else
559                 m_variable_list_sp = global_variable_list_sp;
560         }
561     }
562 
563     return m_variable_list_sp.get();
564 }
565 
566 VariableListSP
567 StackFrame::GetInScopeVariableList (bool get_file_globals, bool must_have_valid_location)
568 {
569     Mutex::Locker locker(m_mutex);
570     // We can't fetch variable information for a history stack frame.
571     if (m_is_history_frame)
572         return VariableListSP();
573 
574     VariableListSP var_list_sp(new VariableList);
575     GetSymbolContext (eSymbolContextCompUnit | eSymbolContextBlock);
576 
577     if (m_sc.block)
578     {
579         const bool can_create = true;
580         const bool get_parent_variables = true;
581         const bool stop_if_block_is_inlined_function = true;
582         m_sc.block->AppendVariables (can_create,
583                                      get_parent_variables,
584                                      stop_if_block_is_inlined_function,
585                                      [this, must_have_valid_location](Variable* v)
586                                      {
587                                          return v->IsInScope(this) && (!must_have_valid_location || v->LocationIsValidForFrame(this));
588                                      },
589                                      var_list_sp.get());
590     }
591 
592     if (m_sc.comp_unit && get_file_globals)
593     {
594         VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
595         if (global_variable_list_sp)
596             var_list_sp->AddVariables (global_variable_list_sp.get());
597     }
598 
599     return var_list_sp;
600 }
601 
602 ValueObjectSP
603 StackFrame::GetValueForVariableExpressionPath (const char *var_expr_cstr,
604                                                DynamicValueType use_dynamic,
605                                                uint32_t options,
606                                                VariableSP &var_sp,
607                                                Error &error)
608 {
609     // We can't fetch variable information for a history stack frame.
610     if (m_is_history_frame)
611         return ValueObjectSP();
612 
613     if (var_expr_cstr && var_expr_cstr[0])
614     {
615         const bool check_ptr_vs_member = (options & eExpressionPathOptionCheckPtrVsMember) != 0;
616         const bool no_fragile_ivar = (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
617         const bool no_synth_child = (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
618         //const bool no_synth_array = (options & eExpressionPathOptionsNoSyntheticArrayRange) != 0;
619         error.Clear();
620         bool deref = false;
621         bool address_of = false;
622         ValueObjectSP valobj_sp;
623         const bool get_file_globals = true;
624         // When looking up a variable for an expression, we need only consider the
625         // variables that are in scope.
626         VariableListSP var_list_sp (GetInScopeVariableList (get_file_globals));
627         VariableList *variable_list = var_list_sp.get();
628 
629         if (variable_list)
630         {
631             // If first character is a '*', then show pointer contents
632             const char *var_expr = var_expr_cstr;
633             if (var_expr[0] == '*')
634             {
635                 deref = true;
636                 var_expr++; // Skip the '*'
637             }
638             else if (var_expr[0] == '&')
639             {
640                 address_of = true;
641                 var_expr++; // Skip the '&'
642             }
643 
644             std::string var_path (var_expr);
645             size_t separator_idx = var_path.find_first_of(".-[=+~|&^%#@!/?,<>{}");
646             StreamString var_expr_path_strm;
647 
648             ConstString name_const_string;
649             if (separator_idx == std::string::npos)
650                 name_const_string.SetCString (var_path.c_str());
651             else
652                 name_const_string.SetCStringWithLength (var_path.c_str(), separator_idx);
653 
654             var_sp = variable_list->FindVariable(name_const_string, false);
655 
656             bool synthetically_added_instance_object = false;
657 
658             if (var_sp)
659             {
660                 var_path.erase (0, name_const_string.GetLength ());
661             }
662 
663             if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess))
664             {
665                 // Check for direct ivars access which helps us with implicit
666                 // access to ivars with the "this->" or "self->"
667                 GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock);
668                 lldb::LanguageType method_language = eLanguageTypeUnknown;
669                 bool is_instance_method = false;
670                 ConstString method_object_name;
671                 if (m_sc.GetFunctionMethodInfo (method_language, is_instance_method, method_object_name))
672                 {
673                     if (is_instance_method && method_object_name)
674                     {
675                         var_sp = variable_list->FindVariable(method_object_name);
676                         if (var_sp)
677                         {
678                             separator_idx = 0;
679                             var_path.insert(0, "->");
680                             synthetically_added_instance_object = true;
681                         }
682                     }
683                 }
684             }
685 
686             if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions))
687             {
688                 // Check if any anonymous unions are there which contain a variable with the name we need
689                 for (size_t i = 0;
690                      i < variable_list->GetSize();
691                      i++)
692                 {
693                     if (VariableSP variable_sp = variable_list->GetVariableAtIndex(i))
694                     {
695                         if (variable_sp->GetName().IsEmpty())
696                         {
697                             if (Type *var_type = variable_sp->GetType())
698                             {
699                                 if (var_type->GetForwardCompilerType().IsAnonymousType())
700                                 {
701                                     valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
702                                     if (!valobj_sp)
703                                         return valobj_sp;
704                                     valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
705                                     if (valobj_sp)
706                                         break;
707                                 }
708                             }
709                         }
710                     }
711                 }
712             }
713 
714             if (var_sp && !valobj_sp)
715             {
716                 valobj_sp = GetValueObjectForFrameVariable (var_sp, use_dynamic);
717                 if (!valobj_sp)
718                     return valobj_sp;
719             }
720             if (valobj_sp)
721             {
722                 // We are dumping at least one child
723                 while (separator_idx != std::string::npos)
724                 {
725                     // Calculate the next separator index ahead of time
726                     ValueObjectSP child_valobj_sp;
727                     const char separator_type = var_path[0];
728                     switch (separator_type)
729                     {
730                     case '-':
731                         if (var_path.size() >= 2 && var_path[1] != '>')
732                             return ValueObjectSP();
733 
734                         if (no_fragile_ivar)
735                         {
736                             // Make sure we aren't trying to deref an objective
737                             // C ivar if this is not allowed
738                             const uint32_t pointer_type_flags = valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
739                             if ((pointer_type_flags & eTypeIsObjC) &&
740                                 (pointer_type_flags & eTypeIsPointer))
741                             {
742                                 // This was an objective C object pointer and
743                                 // it was requested we skip any fragile ivars
744                                 // so return nothing here
745                                 return ValueObjectSP();
746                             }
747                         }
748                         var_path.erase (0, 1); // Remove the '-'
749                         LLVM_FALLTHROUGH;
750                     case '.':
751                         {
752                             const bool expr_is_ptr = var_path[0] == '>';
753 
754                             var_path.erase (0, 1); // Remove the '.' or '>'
755                             separator_idx = var_path.find_first_of(".-[");
756                             ConstString child_name;
757                             if (separator_idx == std::string::npos)
758                                 child_name.SetCString (var_path.c_str());
759                             else
760                                 child_name.SetCStringWithLength(var_path.c_str(), separator_idx);
761 
762                             if (check_ptr_vs_member)
763                             {
764                                 // We either have a pointer type and need to verify
765                                 // valobj_sp is a pointer, or we have a member of a
766                                 // class/union/struct being accessed with the . syntax
767                                 // and need to verify we don't have a pointer.
768                                 const bool actual_is_ptr = valobj_sp->IsPointerType ();
769 
770                                 if (actual_is_ptr != expr_is_ptr)
771                                 {
772                                     // Incorrect use of "." with a pointer, or "->" with
773                                     // a class/union/struct instance or reference.
774                                     valobj_sp->GetExpressionPath (var_expr_path_strm, false);
775                                     if (actual_is_ptr)
776                                         error.SetErrorStringWithFormat ("\"%s\" is a pointer and . was used to attempt to access \"%s\". Did you mean \"%s->%s\"?",
777                                                                         var_expr_path_strm.GetString().c_str(),
778                                                                         child_name.GetCString(),
779                                                                         var_expr_path_strm.GetString().c_str(),
780                                                                         var_path.c_str());
781                                     else
782                                         error.SetErrorStringWithFormat ("\"%s\" is not a pointer and -> was used to attempt to access \"%s\". Did you mean \"%s.%s\"?",
783                                                                         var_expr_path_strm.GetString().c_str(),
784                                                                         child_name.GetCString(),
785                                                                         var_expr_path_strm.GetString().c_str(),
786                                                                         var_path.c_str());
787                                     return ValueObjectSP();
788                                 }
789                             }
790                             child_valobj_sp = valobj_sp->GetChildMemberWithName (child_name, true);
791                             if (!child_valobj_sp)
792                             {
793                                 if (!no_synth_child)
794                                 {
795                                     child_valobj_sp = valobj_sp->GetSyntheticValue();
796                                     if (child_valobj_sp)
797                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName (child_name, true);
798                                 }
799 
800                                 if (no_synth_child || !child_valobj_sp)
801                                 {
802                                     // No child member with name "child_name"
803                                     if (synthetically_added_instance_object)
804                                     {
805                                         // We added a "this->" or "self->" to the beginning of the expression
806                                         // and this is the first pointer ivar access, so just return the normal
807                                         // error
808                                         error.SetErrorStringWithFormat("no variable or instance variable named '%s' found in this frame",
809                                                                        name_const_string.GetCString());
810                                     }
811                                     else
812                                     {
813                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
814                                         if (child_name)
815                                         {
816                                             error.SetErrorStringWithFormat ("\"%s\" is not a member of \"(%s) %s\"",
817                                                                             child_name.GetCString(),
818                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
819                                                                             var_expr_path_strm.GetString().c_str());
820                                         }
821                                         else
822                                         {
823                                             error.SetErrorStringWithFormat ("incomplete expression path after \"%s\" in \"%s\"",
824                                                                             var_expr_path_strm.GetString().c_str(),
825                                                                             var_expr_cstr);
826                                         }
827                                     }
828                                     return ValueObjectSP();
829                                 }
830                             }
831                             synthetically_added_instance_object = false;
832                             // Remove the child name from the path
833                             var_path.erase(0, child_name.GetLength());
834                             if (use_dynamic != eNoDynamicValues)
835                             {
836                                 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
837                                 if (dynamic_value_sp)
838                                     child_valobj_sp = dynamic_value_sp;
839                             }
840                         }
841                         break;
842 
843                     case '[':
844                         // Array member access, or treating pointer as an array
845                         if (var_path.size() > 2) // Need at least two brackets and a number
846                         {
847                             char *end = nullptr;
848                             long child_index = ::strtol (&var_path[1], &end, 0);
849                             if (end && *end == ']'
850                                 && *(end-1) != '[') // this code forces an error in the case of arr[]. as bitfield[] is not a good syntax we're good to go
851                             {
852                                 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref)
853                                 {
854                                     // what we have is *ptr[low]. the most similar C++ syntax is to deref ptr
855                                     // and extract bit low out of it. reading array item low
856                                     // would be done by saying ptr[low], without a deref * sign
857                                     Error error;
858                                     ValueObjectSP temp(valobj_sp->Dereference(error));
859                                     if (error.Fail())
860                                     {
861                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
862                                         error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
863                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
864                                                                         var_expr_path_strm.GetString().c_str());
865                                         return ValueObjectSP();
866                                     }
867                                     valobj_sp = temp;
868                                     deref = false;
869                                 }
870                                 else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref)
871                                 {
872                                     // what we have is *arr[low]. the most similar C++ syntax is to get arr[0]
873                                     // (an operation that is equivalent to deref-ing arr)
874                                     // and extract bit low out of it. reading array item low
875                                     // would be done by saying arr[low], without a deref * sign
876                                     Error error;
877                                     ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
878                                     if (error.Fail())
879                                     {
880                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
881                                         error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
882                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
883                                                                         var_expr_path_strm.GetString().c_str());
884                                         return ValueObjectSP();
885                                     }
886                                     valobj_sp = temp;
887                                     deref = false;
888                                 }
889 
890                                 bool is_incomplete_array = false;
891                                 if (valobj_sp->IsPointerType ())
892                                 {
893                                     bool is_objc_pointer = true;
894 
895                                     if (valobj_sp->GetCompilerType().GetMinimumLanguage() != eLanguageTypeObjC)
896                                         is_objc_pointer = false;
897                                     else if (!valobj_sp->GetCompilerType().IsPointerType())
898                                         is_objc_pointer = false;
899 
900                                     if (no_synth_child && is_objc_pointer)
901                                     {
902                                         error.SetErrorStringWithFormat("\"(%s) %s\" is an Objective-C pointer, and cannot be subscripted",
903                                                                        valobj_sp->GetTypeName().AsCString("<invalid type>"),
904                                                                        var_expr_path_strm.GetString().c_str());
905 
906                                         return ValueObjectSP();
907                                     }
908                                     else if (is_objc_pointer)
909                                     {
910                                         // dereferencing ObjC variables is not valid.. so let's try and recur to synthetic children
911                                         ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
912                                         if (!synthetic /* no synthetic */
913                                             || synthetic == valobj_sp) /* synthetic is the same as the original object */
914                                         {
915                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
916                                             error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
917                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
918                                                                             var_expr_path_strm.GetString().c_str());
919                                         }
920                                         else if (static_cast<uint32_t>(child_index) >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
921                                         {
922                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
923                                             error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
924                                                                             child_index,
925                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
926                                                                             var_expr_path_strm.GetString().c_str());
927                                         }
928                                         else
929                                         {
930                                             child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
931                                             if (!child_valobj_sp)
932                                             {
933                                                 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
934                                                 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
935                                                                                 child_index,
936                                                                                 valobj_sp->GetTypeName().AsCString("<invalid type>"),
937                                                                                 var_expr_path_strm.GetString().c_str());
938                                             }
939                                         }
940                                     }
941                                     else
942                                     {
943                                         child_valobj_sp = valobj_sp->GetSyntheticArrayMember (child_index, true);
944                                         if (!child_valobj_sp)
945                                         {
946                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
947                                             error.SetErrorStringWithFormat ("failed to use pointer as array for index %ld for \"(%s) %s\"",
948                                                                             child_index,
949                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
950                                                                             var_expr_path_strm.GetString().c_str());
951                                         }
952                                     }
953                                 }
954                                 else if (valobj_sp->GetCompilerType().IsArrayType(nullptr, nullptr, &is_incomplete_array))
955                                 {
956                                     // Pass false to dynamic_value here so we can tell the difference between
957                                     // no dynamic value and no member of this type...
958                                     child_valobj_sp = valobj_sp->GetChildAtIndex (child_index, true);
959                                     if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
960                                         child_valobj_sp = valobj_sp->GetSyntheticArrayMember (child_index, true);
961 
962                                     if (!child_valobj_sp)
963                                     {
964                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
965                                         error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
966                                                                         child_index,
967                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
968                                                                         var_expr_path_strm.GetString().c_str());
969                                     }
970                                 }
971                                 else if (valobj_sp->GetCompilerType().IsScalarType())
972                                 {
973                                     // this is a bitfield asking to display just one bit
974                                     child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, child_index, true);
975                                     if (!child_valobj_sp)
976                                     {
977                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
978                                         error.SetErrorStringWithFormat ("bitfield range %ld-%ld is not valid for \"(%s) %s\"",
979                                                                         child_index, child_index,
980                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
981                                                                         var_expr_path_strm.GetString().c_str());
982                                     }
983                                 }
984                                 else
985                                 {
986                                     ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
987                                     if (no_synth_child /* synthetic is forbidden */ ||
988                                         !synthetic /* no synthetic */
989                                         || synthetic == valobj_sp) /* synthetic is the same as the original object */
990                                     {
991                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
992                                         error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
993                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
994                                                                         var_expr_path_strm.GetString().c_str());
995                                     }
996                                     else if (static_cast<uint32_t>(child_index) >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
997                                     {
998                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
999                                         error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
1000                                                                         child_index,
1001                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
1002                                                                         var_expr_path_strm.GetString().c_str());
1003                                     }
1004                                     else
1005                                     {
1006                                         child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
1007                                         if (!child_valobj_sp)
1008                                         {
1009                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1010                                             error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
1011                                                                             child_index,
1012                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1013                                                                             var_expr_path_strm.GetString().c_str());
1014                                         }
1015                                     }
1016                                 }
1017 
1018                                 if (!child_valobj_sp)
1019                                 {
1020                                     // Invalid array index...
1021                                     return ValueObjectSP();
1022                                 }
1023 
1024                                 // Erase the array member specification '[%i]' where
1025                                 // %i is the array index
1026                                 var_path.erase(0, (end - var_path.c_str()) + 1);
1027                                 separator_idx = var_path.find_first_of(".-[");
1028                                 if (use_dynamic != eNoDynamicValues)
1029                                 {
1030                                     ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
1031                                     if (dynamic_value_sp)
1032                                         child_valobj_sp = dynamic_value_sp;
1033                                 }
1034                                 // Break out early from the switch since we were
1035                                 // able to find the child member
1036                                 break;
1037                             }
1038                             else if (end && *end == '-')
1039                             {
1040                                 // this is most probably a BitField, let's take a look
1041                                 char *real_end = nullptr;
1042                                 long final_index = ::strtol (end+1, &real_end, 0);
1043                                 bool expand_bitfield = true;
1044                                 if (real_end && *real_end == ']')
1045                                 {
1046                                     // if the format given is [high-low], swap range
1047                                     if (child_index > final_index)
1048                                     {
1049                                         long temp = child_index;
1050                                         child_index = final_index;
1051                                         final_index = temp;
1052                                     }
1053 
1054                                     if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref)
1055                                     {
1056                                         // what we have is *ptr[low-high]. the most similar C++ syntax is to deref ptr
1057                                         // and extract bits low thru high out of it. reading array items low thru high
1058                                         // would be done by saying ptr[low-high], without a deref * sign
1059                                         Error error;
1060                                         ValueObjectSP temp(valobj_sp->Dereference(error));
1061                                         if (error.Fail())
1062                                         {
1063                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1064                                             error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
1065                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1066                                                                             var_expr_path_strm.GetString().c_str());
1067                                             return ValueObjectSP();
1068                                         }
1069                                         valobj_sp = temp;
1070                                         deref = false;
1071                                     }
1072                                     else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref)
1073                                     {
1074                                         // what we have is *arr[low-high]. the most similar C++ syntax is to get arr[0]
1075                                         // (an operation that is equivalent to deref-ing arr)
1076                                         // and extract bits low thru high out of it. reading array items low thru high
1077                                         // would be done by saying arr[low-high], without a deref * sign
1078                                         Error error;
1079                                         ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
1080                                         if (error.Fail())
1081                                         {
1082                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1083                                             error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
1084                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1085                                                                             var_expr_path_strm.GetString().c_str());
1086                                             return ValueObjectSP();
1087                                         }
1088                                         valobj_sp = temp;
1089                                         deref = false;
1090                                     }
1091                                     /*else if (valobj_sp->IsArrayType() || valobj_sp->IsPointerType())
1092                                     {
1093                                         child_valobj_sp = valobj_sp->GetSyntheticArrayRangeChild(child_index, final_index, true);
1094                                         expand_bitfield = false;
1095                                         if (!child_valobj_sp)
1096                                         {
1097                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1098                                             error.SetErrorStringWithFormat ("array range %i-%i is not valid for \"(%s) %s\"",
1099                                                                             child_index, final_index,
1100                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1101                                                                             var_expr_path_strm.GetString().c_str());
1102                                         }
1103                                     }*/
1104 
1105                                     if (expand_bitfield)
1106                                     {
1107                                         child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1108                                         if (!child_valobj_sp)
1109                                         {
1110                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1111                                             error.SetErrorStringWithFormat ("bitfield range %ld-%ld is not valid for \"(%s) %s\"",
1112                                                                             child_index, final_index,
1113                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1114                                                                             var_expr_path_strm.GetString().c_str());
1115                                         }
1116                                     }
1117                                 }
1118 
1119                                 if (!child_valobj_sp)
1120                                 {
1121                                     // Invalid bitfield range...
1122                                     return ValueObjectSP();
1123                                 }
1124 
1125                                 // Erase the bitfield member specification '[%i-%i]' where
1126                                 // %i is the index
1127                                 var_path.erase(0, (real_end - var_path.c_str()) + 1);
1128                                 separator_idx = var_path.find_first_of(".-[");
1129                                 if (use_dynamic != eNoDynamicValues)
1130                                 {
1131                                     ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
1132                                     if (dynamic_value_sp)
1133                                         child_valobj_sp = dynamic_value_sp;
1134                                 }
1135                                 // Break out early from the switch since we were
1136                                 // able to find the child member
1137                                 break;
1138 
1139                             }
1140                         }
1141                         else
1142                         {
1143                             error.SetErrorStringWithFormat("invalid square bracket encountered after \"%s\" in \"%s\"",
1144                                                            var_expr_path_strm.GetString().c_str(),
1145                                                            var_path.c_str());
1146                         }
1147                         return ValueObjectSP();
1148 
1149                     default:
1150                         // Failure...
1151                         {
1152                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1153                             error.SetErrorStringWithFormat ("unexpected char '%c' encountered after \"%s\" in \"%s\"",
1154                                                             separator_type,
1155                                                             var_expr_path_strm.GetString().c_str(),
1156                                                             var_path.c_str());
1157 
1158                             return ValueObjectSP();
1159                         }
1160                     }
1161 
1162                     if (child_valobj_sp)
1163                         valobj_sp = child_valobj_sp;
1164 
1165                     if (var_path.empty())
1166                         break;
1167                 }
1168                 if (valobj_sp)
1169                 {
1170                     if (deref)
1171                     {
1172                         ValueObjectSP deref_valobj_sp (valobj_sp->Dereference(error));
1173                         valobj_sp = deref_valobj_sp;
1174                     }
1175                     else if (address_of)
1176                     {
1177                         ValueObjectSP address_of_valobj_sp (valobj_sp->AddressOf(error));
1178                         valobj_sp = address_of_valobj_sp;
1179                     }
1180                 }
1181                 return valobj_sp;
1182             }
1183             else
1184             {
1185                 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
1186                                                name_const_string.GetCString());
1187             }
1188         }
1189     }
1190     else
1191     {
1192         error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
1193     }
1194     return ValueObjectSP();
1195 }
1196 
1197 bool
1198 StackFrame::GetFrameBaseValue (Scalar &frame_base, Error *error_ptr)
1199 {
1200     Mutex::Locker locker(m_mutex);
1201     if (!m_cfa_is_valid)
1202     {
1203         m_frame_base_error.SetErrorString("No frame base available for this historical stack frame.");
1204         return false;
1205     }
1206 
1207     if (m_flags.IsClear(GOT_FRAME_BASE))
1208     {
1209         if (m_sc.function)
1210         {
1211             m_frame_base.Clear();
1212             m_frame_base_error.Clear();
1213 
1214             m_flags.Set(GOT_FRAME_BASE);
1215             ExecutionContext exe_ctx (shared_from_this());
1216             Value expr_value;
1217             addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1218             if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1219                 loclist_base_addr = m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (exe_ctx.GetTargetPtr());
1220 
1221             if (m_sc.function->GetFrameBaseExpression().Evaluate(&exe_ctx,
1222                                                                  nullptr,
1223                                                                  nullptr,
1224                                                                  nullptr,
1225                                                                  loclist_base_addr,
1226                                                                  nullptr,
1227                                                                  nullptr,
1228                                                                  expr_value,
1229                                                                  &m_frame_base_error) == false)
1230             {
1231                 // We should really have an error if evaluate returns, but in case
1232                 // we don't, lets set the error to something at least.
1233                 if (m_frame_base_error.Success())
1234                     m_frame_base_error.SetErrorString("Evaluation of the frame base expression failed.");
1235             }
1236             else
1237             {
1238                 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1239             }
1240         }
1241         else
1242         {
1243             m_frame_base_error.SetErrorString ("No function in symbol context.");
1244         }
1245     }
1246 
1247     if (m_frame_base_error.Success())
1248         frame_base = m_frame_base;
1249 
1250     if (error_ptr)
1251         *error_ptr = m_frame_base_error;
1252     return m_frame_base_error.Success();
1253 }
1254 
1255 RegisterContextSP
1256 StackFrame::GetRegisterContext ()
1257 {
1258     Mutex::Locker locker(m_mutex);
1259     if (!m_reg_context_sp)
1260     {
1261         ThreadSP thread_sp (GetThread());
1262         if (thread_sp)
1263             m_reg_context_sp = thread_sp->CreateRegisterContextForFrame (this);
1264     }
1265     return m_reg_context_sp;
1266 }
1267 
1268 bool
1269 StackFrame::HasDebugInformation ()
1270 {
1271     GetSymbolContext (eSymbolContextLineEntry);
1272     return m_sc.line_entry.IsValid();
1273 }
1274 
1275 ValueObjectSP
1276 StackFrame::GetValueObjectForFrameVariable (const VariableSP &variable_sp, DynamicValueType use_dynamic)
1277 {
1278     Mutex::Locker locker(m_mutex);
1279     ValueObjectSP valobj_sp;
1280     if (m_is_history_frame)
1281     {
1282         return valobj_sp;
1283     }
1284     VariableList *var_list = GetVariableList (true);
1285     if (var_list)
1286     {
1287         // Make sure the variable is a frame variable
1288         const uint32_t var_idx = var_list->FindIndexForVariable (variable_sp.get());
1289         const uint32_t num_variables = var_list->GetSize();
1290         if (var_idx < num_variables)
1291         {
1292             valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex (var_idx);
1293             if (!valobj_sp)
1294             {
1295                 if (m_variable_list_value_objects.GetSize() < num_variables)
1296                     m_variable_list_value_objects.Resize(num_variables);
1297                 valobj_sp = ValueObjectVariable::Create (this, variable_sp);
1298                 m_variable_list_value_objects.SetValueObjectAtIndex (var_idx, valobj_sp);
1299             }
1300         }
1301     }
1302     if (use_dynamic != eNoDynamicValues && valobj_sp)
1303     {
1304         ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue (use_dynamic);
1305         if (dynamic_sp)
1306             return dynamic_sp;
1307     }
1308     return valobj_sp;
1309 }
1310 
1311 ValueObjectSP
1312 StackFrame::TrackGlobalVariable (const VariableSP &variable_sp, DynamicValueType use_dynamic)
1313 {
1314     Mutex::Locker locker(m_mutex);
1315     if (m_is_history_frame)
1316         return ValueObjectSP();
1317 
1318     // Check to make sure we aren't already tracking this variable?
1319     ValueObjectSP valobj_sp (GetValueObjectForFrameVariable (variable_sp, use_dynamic));
1320     if (!valobj_sp)
1321     {
1322         // We aren't already tracking this global
1323         VariableList *var_list = GetVariableList (true);
1324         // If this frame has no variables, create a new list
1325         if (var_list == nullptr)
1326             m_variable_list_sp.reset (new VariableList());
1327 
1328         // Add the global/static variable to this frame
1329         m_variable_list_sp->AddVariable (variable_sp);
1330 
1331         // Now make a value object for it so we can track its changes
1332         valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
1333     }
1334     return valobj_sp;
1335 }
1336 
1337 bool
1338 StackFrame::IsInlined ()
1339 {
1340     if (m_sc.block == nullptr)
1341         GetSymbolContext (eSymbolContextBlock);
1342     if (m_sc.block)
1343         return m_sc.block->GetContainingInlinedBlock() != nullptr;
1344     return false;
1345 }
1346 
1347 lldb::LanguageType
1348 StackFrame::GetLanguage ()
1349 {
1350     CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1351     if (cu)
1352         return cu->GetLanguage();
1353     return lldb::eLanguageTypeUnknown;
1354 }
1355 
1356 lldb::LanguageType
1357 StackFrame::GuessLanguage ()
1358 {
1359     LanguageType lang_type = GetLanguage();
1360 
1361     if (lang_type == eLanguageTypeUnknown)
1362     {
1363         Function *f = GetSymbolContext(eSymbolContextFunction).function;
1364         if (f)
1365         {
1366             lang_type = f->GetMangled().GuessLanguage();
1367         }
1368     }
1369 
1370     return lang_type;
1371 }
1372 
1373 TargetSP
1374 StackFrame::CalculateTarget ()
1375 {
1376     TargetSP target_sp;
1377     ThreadSP thread_sp(GetThread());
1378     if (thread_sp)
1379     {
1380         ProcessSP process_sp (thread_sp->CalculateProcess());
1381         if (process_sp)
1382             target_sp = process_sp->CalculateTarget();
1383     }
1384     return target_sp;
1385 }
1386 
1387 ProcessSP
1388 StackFrame::CalculateProcess ()
1389 {
1390     ProcessSP process_sp;
1391     ThreadSP thread_sp(GetThread());
1392     if (thread_sp)
1393         process_sp = thread_sp->CalculateProcess();
1394     return process_sp;
1395 }
1396 
1397 ThreadSP
1398 StackFrame::CalculateThread ()
1399 {
1400     return GetThread();
1401 }
1402 
1403 StackFrameSP
1404 StackFrame::CalculateStackFrame ()
1405 {
1406     return shared_from_this();
1407 }
1408 
1409 void
1410 StackFrame::CalculateExecutionContext (ExecutionContext &exe_ctx)
1411 {
1412     exe_ctx.SetContext (shared_from_this());
1413 }
1414 
1415 void
1416 StackFrame::DumpUsingSettingsFormat (Stream *strm, const char *frame_marker)
1417 {
1418     if (strm == nullptr)
1419         return;
1420 
1421     GetSymbolContext(eSymbolContextEverything);
1422     ExecutionContext exe_ctx (shared_from_this());
1423     StreamString s;
1424 
1425     if (frame_marker)
1426         s.PutCString(frame_marker);
1427 
1428     const FormatEntity::Entry *frame_format = nullptr;
1429     Target *target = exe_ctx.GetTargetPtr();
1430     if (target)
1431         frame_format = target->GetDebugger().GetFrameFormat();
1432     if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx, nullptr, nullptr, false, false))
1433     {
1434         strm->Write(s.GetData(), s.GetSize());
1435     }
1436     else
1437     {
1438         Dump (strm, true, false);
1439         strm->EOL();
1440     }
1441 }
1442 
1443 void
1444 StackFrame::Dump (Stream *strm, bool show_frame_index, bool show_fullpaths)
1445 {
1446     if (strm == nullptr)
1447         return;
1448 
1449     if (show_frame_index)
1450         strm->Printf("frame #%u: ", m_frame_index);
1451     ExecutionContext exe_ctx (shared_from_this());
1452     Target *target = exe_ctx.GetTargetPtr();
1453     strm->Printf("0x%0*" PRIx64 " ",
1454                  target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16,
1455                  GetFrameCodeAddress().GetLoadAddress(target));
1456     GetSymbolContext(eSymbolContextEverything);
1457     const bool show_module = true;
1458     const bool show_inline = true;
1459     const bool show_function_arguments = true;
1460     const bool show_function_name = true;
1461     m_sc.DumpStopContext (strm,
1462                           exe_ctx.GetBestExecutionContextScope(),
1463                           GetFrameCodeAddress(),
1464                           show_fullpaths,
1465                           show_module,
1466                           show_inline,
1467                           show_function_arguments,
1468                           show_function_name);
1469 }
1470 
1471 void
1472 StackFrame::UpdateCurrentFrameFromPreviousFrame (StackFrame &prev_frame)
1473 {
1474     Mutex::Locker locker(m_mutex);
1475     assert (GetStackID() == prev_frame.GetStackID());    // TODO: remove this after some testing
1476     m_variable_list_sp = prev_frame.m_variable_list_sp;
1477     m_variable_list_value_objects.Swap (prev_frame.m_variable_list_value_objects);
1478     if (!m_disassembly.GetString().empty())
1479         m_disassembly.GetString().swap (m_disassembly.GetString());
1480 }
1481 
1482 void
1483 StackFrame::UpdatePreviousFrameFromCurrentFrame (StackFrame &curr_frame)
1484 {
1485     Mutex::Locker locker(m_mutex);
1486     assert (GetStackID() == curr_frame.GetStackID());        // TODO: remove this after some testing
1487     m_id.SetPC (curr_frame.m_id.GetPC());       // Update the Stack ID PC value
1488     assert (GetThread() == curr_frame.GetThread());
1489     m_frame_index = curr_frame.m_frame_index;
1490     m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1491     m_reg_context_sp = curr_frame.m_reg_context_sp;
1492     m_frame_code_addr = curr_frame.m_frame_code_addr;
1493     assert (!m_sc.target_sp || !curr_frame.m_sc.target_sp || m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1494     assert (!m_sc.module_sp || !curr_frame.m_sc.module_sp || m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1495     assert (m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr || m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1496     assert (m_sc.function == nullptr || curr_frame.m_sc.function == nullptr || m_sc.function == curr_frame.m_sc.function);
1497     m_sc = curr_frame.m_sc;
1498     m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1499     m_flags.Set (m_sc.GetResolvedMask());
1500     m_frame_base.Clear();
1501     m_frame_base_error.Clear();
1502 }
1503 
1504 bool
1505 StackFrame::HasCachedData () const
1506 {
1507     if (m_variable_list_sp)
1508         return true;
1509     if (m_variable_list_value_objects.GetSize() > 0)
1510         return true;
1511     if (!m_disassembly.GetString().empty())
1512         return true;
1513     return false;
1514 }
1515 
1516 bool
1517 StackFrame::GetStatus (Stream& strm,
1518                        bool show_frame_info,
1519                        bool show_source,
1520                        const char *frame_marker)
1521 {
1522 
1523     if (show_frame_info)
1524     {
1525         strm.Indent();
1526         DumpUsingSettingsFormat (&strm, frame_marker);
1527     }
1528 
1529     if (show_source)
1530     {
1531         ExecutionContext exe_ctx (shared_from_this());
1532         bool have_source = false, have_debuginfo = false;
1533         Debugger::StopDisassemblyType disasm_display = Debugger::eStopDisassemblyTypeNever;
1534         Target *target = exe_ctx.GetTargetPtr();
1535         if (target)
1536         {
1537             Debugger &debugger = target->GetDebugger();
1538             const uint32_t source_lines_before = debugger.GetStopSourceLineCount(true);
1539             const uint32_t source_lines_after = debugger.GetStopSourceLineCount(false);
1540             disasm_display = debugger.GetStopDisassemblyDisplay ();
1541 
1542             GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1543             if (m_sc.comp_unit && m_sc.line_entry.IsValid())
1544             {
1545                 have_debuginfo = true;
1546                 if (source_lines_before > 0 || source_lines_after > 0)
1547                 {
1548                     size_t num_lines = target->GetSourceManager().DisplaySourceLinesWithLineNumbers (m_sc.line_entry.file,
1549                                                                                       m_sc.line_entry.line,
1550                                                                                       source_lines_before,
1551                                                                                       source_lines_after,
1552                                                                                       "->",
1553                                                                                       &strm);
1554                     if (num_lines != 0)
1555                         have_source = true;
1556                     // TODO: Give here a one time warning if source file is missing.
1557                 }
1558             }
1559             switch (disasm_display)
1560             {
1561             case Debugger::eStopDisassemblyTypeNever:
1562                 break;
1563 
1564             case Debugger::eStopDisassemblyTypeNoDebugInfo:
1565                 if (have_debuginfo)
1566                     break;
1567                 LLVM_FALLTHROUGH;
1568 
1569             case Debugger::eStopDisassemblyTypeNoSource:
1570                 if (have_source)
1571                     break;
1572                 LLVM_FALLTHROUGH;
1573 
1574             case Debugger::eStopDisassemblyTypeAlways:
1575                 if (target)
1576                 {
1577                     const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1578                     if (disasm_lines > 0)
1579                     {
1580                         const ArchSpec &target_arch = target->GetArchitecture();
1581                         AddressRange pc_range;
1582                         pc_range.GetBaseAddress() = GetFrameCodeAddress();
1583                         pc_range.SetByteSize(disasm_lines * target_arch.GetMaximumOpcodeByteSize());
1584                         const char *plugin_name = nullptr;
1585                         const char *flavor = nullptr;
1586                         Disassembler::Disassemble (target->GetDebugger(),
1587                                                    target_arch,
1588                                                    plugin_name,
1589                                                    flavor,
1590                                                    exe_ctx,
1591                                                    pc_range,
1592                                                    disasm_lines,
1593                                                    0,
1594                                                    Disassembler::eOptionMarkPCAddress,
1595                                                    strm);
1596                     }
1597                 }
1598                 break;
1599             }
1600         }
1601     }
1602     return true;
1603 }
1604