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                     if (m_sc.target_sp)
494                     {
495                         // Be sure to apply and file remappings to our file and line
496                         // entries when handing out a line entry
497                         FileSpec new_file_spec;
498                         if (m_sc.target_sp->GetSourcePathMap().FindFile (m_sc.line_entry.file, new_file_spec))
499                             m_sc.line_entry.file = new_file_spec;
500                     }
501                 }
502             }
503         }
504         else
505         {
506             // If we don't have a module, then we can't have the compile unit,
507             // function, block, line entry or symbol, so we can safely call
508             // ResolveSymbolContextForAddress with our symbol context member m_sc.
509             if (m_sc.target_sp)
510             {
511                 resolved |= m_sc.target_sp->GetImages().ResolveSymbolContextForAddress (lookup_addr, resolve_scope, m_sc);
512             }
513         }
514 
515         // Update our internal flags so we remember what we have tried to locate so
516         // we don't have to keep trying when more calls to this function are made.
517         // We might have dug up more information that was requested (for example
518         // if we were asked to only get the block, we will have gotten the
519         // compile unit, and function) so set any additional bits that we resolved
520         m_flags.Set (resolve_scope | resolved);
521     }
522 
523     // Return the symbol context with everything that was possible to resolve
524     // resolved.
525     return m_sc;
526 }
527 
528 VariableList *
529 StackFrame::GetVariableList (bool get_file_globals)
530 {
531     Mutex::Locker locker(m_mutex);
532     if (m_flags.IsClear(RESOLVED_VARIABLES))
533     {
534         m_flags.Set(RESOLVED_VARIABLES);
535 
536         Block *frame_block = GetFrameBlock();
537 
538         if (frame_block)
539         {
540             const bool get_child_variables = true;
541             const bool can_create = true;
542             const bool stop_if_child_block_is_inlined_function = true;
543             m_variable_list_sp.reset(new VariableList());
544             frame_block->AppendBlockVariables(can_create,
545                                               get_child_variables,
546                                               stop_if_child_block_is_inlined_function,
547                                               [this](Variable* v) { return true; },
548                                               m_variable_list_sp.get());
549         }
550     }
551 
552     if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) &&
553         get_file_globals)
554     {
555         m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
556 
557         if (m_flags.IsClear (eSymbolContextCompUnit))
558             GetSymbolContext (eSymbolContextCompUnit);
559 
560         if (m_sc.comp_unit)
561         {
562             VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
563             if (m_variable_list_sp)
564                 m_variable_list_sp->AddVariables (global_variable_list_sp.get());
565             else
566                 m_variable_list_sp = global_variable_list_sp;
567         }
568     }
569 
570     return m_variable_list_sp.get();
571 }
572 
573 VariableListSP
574 StackFrame::GetInScopeVariableList (bool get_file_globals, bool must_have_valid_location)
575 {
576     Mutex::Locker locker(m_mutex);
577     // We can't fetch variable information for a history stack frame.
578     if (m_is_history_frame)
579         return VariableListSP();
580 
581     VariableListSP var_list_sp(new VariableList);
582     GetSymbolContext (eSymbolContextCompUnit | eSymbolContextBlock);
583 
584     if (m_sc.block)
585     {
586         const bool can_create = true;
587         const bool get_parent_variables = true;
588         const bool stop_if_block_is_inlined_function = true;
589         m_sc.block->AppendVariables (can_create,
590                                      get_parent_variables,
591                                      stop_if_block_is_inlined_function,
592                                      [this, must_have_valid_location](Variable* v)
593                                      {
594                                          return v->IsInScope(this) && (!must_have_valid_location || v->LocationIsValidForFrame(this));
595                                      },
596                                      var_list_sp.get());
597     }
598 
599     if (m_sc.comp_unit && get_file_globals)
600     {
601         VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
602         if (global_variable_list_sp)
603             var_list_sp->AddVariables (global_variable_list_sp.get());
604     }
605 
606     return var_list_sp;
607 }
608 
609 ValueObjectSP
610 StackFrame::GetValueForVariableExpressionPath (const char *var_expr_cstr,
611                                                DynamicValueType use_dynamic,
612                                                uint32_t options,
613                                                VariableSP &var_sp,
614                                                Error &error)
615 {
616     // We can't fetch variable information for a history stack frame.
617     if (m_is_history_frame)
618         return ValueObjectSP();
619 
620     if (var_expr_cstr && var_expr_cstr[0])
621     {
622         const bool check_ptr_vs_member = (options & eExpressionPathOptionCheckPtrVsMember) != 0;
623         const bool no_fragile_ivar = (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
624         const bool no_synth_child = (options & eExpressionPathOptionsNoSyntheticChildren) != 0;
625         //const bool no_synth_array = (options & eExpressionPathOptionsNoSyntheticArrayRange) != 0;
626         error.Clear();
627         bool deref = false;
628         bool address_of = false;
629         ValueObjectSP valobj_sp;
630         const bool get_file_globals = true;
631         // When looking up a variable for an expression, we need only consider the
632         // variables that are in scope.
633         VariableListSP var_list_sp (GetInScopeVariableList (get_file_globals));
634         VariableList *variable_list = var_list_sp.get();
635 
636         if (variable_list)
637         {
638             // If first character is a '*', then show pointer contents
639             const char *var_expr = var_expr_cstr;
640             if (var_expr[0] == '*')
641             {
642                 deref = true;
643                 var_expr++; // Skip the '*'
644             }
645             else if (var_expr[0] == '&')
646             {
647                 address_of = true;
648                 var_expr++; // Skip the '&'
649             }
650 
651             std::string var_path (var_expr);
652             size_t separator_idx = var_path.find_first_of(".-[=+~|&^%#@!/?,<>{}");
653             StreamString var_expr_path_strm;
654 
655             ConstString name_const_string;
656             if (separator_idx == std::string::npos)
657                 name_const_string.SetCString (var_path.c_str());
658             else
659                 name_const_string.SetCStringWithLength (var_path.c_str(), separator_idx);
660 
661             var_sp = variable_list->FindVariable(name_const_string, false);
662 
663             bool synthetically_added_instance_object = false;
664 
665             if (var_sp)
666             {
667                 var_path.erase (0, name_const_string.GetLength ());
668             }
669 
670             if (!var_sp && (options & eExpressionPathOptionsAllowDirectIVarAccess))
671             {
672                 // Check for direct ivars access which helps us with implicit
673                 // access to ivars with the "this->" or "self->"
674                 GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock);
675                 lldb::LanguageType method_language = eLanguageTypeUnknown;
676                 bool is_instance_method = false;
677                 ConstString method_object_name;
678                 if (m_sc.GetFunctionMethodInfo (method_language, is_instance_method, method_object_name))
679                 {
680                     if (is_instance_method && method_object_name)
681                     {
682                         var_sp = variable_list->FindVariable(method_object_name);
683                         if (var_sp)
684                         {
685                             separator_idx = 0;
686                             var_path.insert(0, "->");
687                             synthetically_added_instance_object = true;
688                         }
689                     }
690                 }
691             }
692 
693             if (!var_sp && (options & eExpressionPathOptionsInspectAnonymousUnions))
694             {
695                 // Check if any anonymous unions are there which contain a variable with the name we need
696                 for (size_t i = 0;
697                      i < variable_list->GetSize();
698                      i++)
699                 {
700                     if (VariableSP variable_sp = variable_list->GetVariableAtIndex(i))
701                     {
702                         if (variable_sp->GetName().IsEmpty())
703                         {
704                             if (Type *var_type = variable_sp->GetType())
705                             {
706                                 if (var_type->GetForwardCompilerType().IsAnonymousType())
707                                 {
708                                     valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
709                                     if (!valobj_sp)
710                                         return valobj_sp;
711                                     valobj_sp = valobj_sp->GetChildMemberWithName(name_const_string, true);
712                                     if (valobj_sp)
713                                         break;
714                                 }
715                             }
716                         }
717                     }
718                 }
719             }
720 
721             if (var_sp && !valobj_sp)
722             {
723                 valobj_sp = GetValueObjectForFrameVariable (var_sp, use_dynamic);
724                 if (!valobj_sp)
725                     return valobj_sp;
726             }
727             if (valobj_sp)
728             {
729                 // We are dumping at least one child
730                 while (separator_idx != std::string::npos)
731                 {
732                     // Calculate the next separator index ahead of time
733                     ValueObjectSP child_valobj_sp;
734                     const char separator_type = var_path[0];
735                     switch (separator_type)
736                     {
737                     case '-':
738                         if (var_path.size() >= 2 && var_path[1] != '>')
739                             return ValueObjectSP();
740 
741                         if (no_fragile_ivar)
742                         {
743                             // Make sure we aren't trying to deref an objective
744                             // C ivar if this is not allowed
745                             const uint32_t pointer_type_flags = valobj_sp->GetCompilerType().GetTypeInfo(nullptr);
746                             if ((pointer_type_flags & eTypeIsObjC) &&
747                                 (pointer_type_flags & eTypeIsPointer))
748                             {
749                                 // This was an objective C object pointer and
750                                 // it was requested we skip any fragile ivars
751                                 // so return nothing here
752                                 return ValueObjectSP();
753                             }
754                         }
755                         var_path.erase (0, 1); // Remove the '-'
756                         LLVM_FALLTHROUGH;
757                     case '.':
758                         {
759                             const bool expr_is_ptr = var_path[0] == '>';
760 
761                             var_path.erase (0, 1); // Remove the '.' or '>'
762                             separator_idx = var_path.find_first_of(".-[");
763                             ConstString child_name;
764                             if (separator_idx == std::string::npos)
765                                 child_name.SetCString (var_path.c_str());
766                             else
767                                 child_name.SetCStringWithLength(var_path.c_str(), separator_idx);
768 
769                             if (check_ptr_vs_member)
770                             {
771                                 // We either have a pointer type and need to verify
772                                 // valobj_sp is a pointer, or we have a member of a
773                                 // class/union/struct being accessed with the . syntax
774                                 // and need to verify we don't have a pointer.
775                                 const bool actual_is_ptr = valobj_sp->IsPointerType ();
776 
777                                 if (actual_is_ptr != expr_is_ptr)
778                                 {
779                                     // Incorrect use of "." with a pointer, or "->" with
780                                     // a class/union/struct instance or reference.
781                                     valobj_sp->GetExpressionPath (var_expr_path_strm, false);
782                                     if (actual_is_ptr)
783                                         error.SetErrorStringWithFormat ("\"%s\" is a pointer and . was used to attempt to access \"%s\". Did you mean \"%s->%s\"?",
784                                                                         var_expr_path_strm.GetString().c_str(),
785                                                                         child_name.GetCString(),
786                                                                         var_expr_path_strm.GetString().c_str(),
787                                                                         var_path.c_str());
788                                     else
789                                         error.SetErrorStringWithFormat ("\"%s\" is not a pointer and -> was used to attempt to access \"%s\". Did you mean \"%s.%s\"?",
790                                                                         var_expr_path_strm.GetString().c_str(),
791                                                                         child_name.GetCString(),
792                                                                         var_expr_path_strm.GetString().c_str(),
793                                                                         var_path.c_str());
794                                     return ValueObjectSP();
795                                 }
796                             }
797                             child_valobj_sp = valobj_sp->GetChildMemberWithName (child_name, true);
798                             if (!child_valobj_sp)
799                             {
800                                 if (!no_synth_child)
801                                 {
802                                     child_valobj_sp = valobj_sp->GetSyntheticValue();
803                                     if (child_valobj_sp)
804                                         child_valobj_sp = child_valobj_sp->GetChildMemberWithName (child_name, true);
805                                 }
806 
807                                 if (no_synth_child || !child_valobj_sp)
808                                 {
809                                     // No child member with name "child_name"
810                                     if (synthetically_added_instance_object)
811                                     {
812                                         // We added a "this->" or "self->" to the beginning of the expression
813                                         // and this is the first pointer ivar access, so just return the normal
814                                         // error
815                                         error.SetErrorStringWithFormat("no variable or instance variable named '%s' found in this frame",
816                                                                        name_const_string.GetCString());
817                                     }
818                                     else
819                                     {
820                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
821                                         if (child_name)
822                                         {
823                                             error.SetErrorStringWithFormat ("\"%s\" is not a member of \"(%s) %s\"",
824                                                                             child_name.GetCString(),
825                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
826                                                                             var_expr_path_strm.GetString().c_str());
827                                         }
828                                         else
829                                         {
830                                             error.SetErrorStringWithFormat ("incomplete expression path after \"%s\" in \"%s\"",
831                                                                             var_expr_path_strm.GetString().c_str(),
832                                                                             var_expr_cstr);
833                                         }
834                                     }
835                                     return ValueObjectSP();
836                                 }
837                             }
838                             synthetically_added_instance_object = false;
839                             // Remove the child name from the path
840                             var_path.erase(0, child_name.GetLength());
841                             if (use_dynamic != eNoDynamicValues)
842                             {
843                                 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
844                                 if (dynamic_value_sp)
845                                     child_valobj_sp = dynamic_value_sp;
846                             }
847                         }
848                         break;
849 
850                     case '[':
851                         // Array member access, or treating pointer as an array
852                         if (var_path.size() > 2) // Need at least two brackets and a number
853                         {
854                             char *end = nullptr;
855                             long child_index = ::strtol (&var_path[1], &end, 0);
856                             if (end && *end == ']'
857                                 && *(end-1) != '[') // this code forces an error in the case of arr[]. as bitfield[] is not a good syntax we're good to go
858                             {
859                                 if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref)
860                                 {
861                                     // what we have is *ptr[low]. the most similar C++ syntax is to deref ptr
862                                     // and extract bit low out of it. reading array item low
863                                     // would be done by saying ptr[low], without a deref * sign
864                                     Error error;
865                                     ValueObjectSP temp(valobj_sp->Dereference(error));
866                                     if (error.Fail())
867                                     {
868                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
869                                         error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
870                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
871                                                                         var_expr_path_strm.GetString().c_str());
872                                         return ValueObjectSP();
873                                     }
874                                     valobj_sp = temp;
875                                     deref = false;
876                                 }
877                                 else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref)
878                                 {
879                                     // what we have is *arr[low]. the most similar C++ syntax is to get arr[0]
880                                     // (an operation that is equivalent to deref-ing arr)
881                                     // and extract bit low out of it. reading array item low
882                                     // would be done by saying arr[low], without a deref * sign
883                                     Error error;
884                                     ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
885                                     if (error.Fail())
886                                     {
887                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
888                                         error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
889                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
890                                                                         var_expr_path_strm.GetString().c_str());
891                                         return ValueObjectSP();
892                                     }
893                                     valobj_sp = temp;
894                                     deref = false;
895                                 }
896 
897                                 bool is_incomplete_array = false;
898                                 if (valobj_sp->IsPointerType ())
899                                 {
900                                     bool is_objc_pointer = true;
901 
902                                     if (valobj_sp->GetCompilerType().GetMinimumLanguage() != eLanguageTypeObjC)
903                                         is_objc_pointer = false;
904                                     else if (!valobj_sp->GetCompilerType().IsPointerType())
905                                         is_objc_pointer = false;
906 
907                                     if (no_synth_child && is_objc_pointer)
908                                     {
909                                         error.SetErrorStringWithFormat("\"(%s) %s\" is an Objective-C pointer, and cannot be subscripted",
910                                                                        valobj_sp->GetTypeName().AsCString("<invalid type>"),
911                                                                        var_expr_path_strm.GetString().c_str());
912 
913                                         return ValueObjectSP();
914                                     }
915                                     else if (is_objc_pointer)
916                                     {
917                                         // dereferencing ObjC variables is not valid.. so let's try and recur to synthetic children
918                                         ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
919                                         if (!synthetic /* no synthetic */
920                                             || synthetic == valobj_sp) /* synthetic is the same as the original object */
921                                         {
922                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
923                                             error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
924                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
925                                                                             var_expr_path_strm.GetString().c_str());
926                                         }
927                                         else if (static_cast<uint32_t>(child_index) >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
928                                         {
929                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
930                                             error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
931                                                                             child_index,
932                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
933                                                                             var_expr_path_strm.GetString().c_str());
934                                         }
935                                         else
936                                         {
937                                             child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
938                                             if (!child_valobj_sp)
939                                             {
940                                                 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
941                                                 error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
942                                                                                 child_index,
943                                                                                 valobj_sp->GetTypeName().AsCString("<invalid type>"),
944                                                                                 var_expr_path_strm.GetString().c_str());
945                                             }
946                                         }
947                                     }
948                                     else
949                                     {
950                                         child_valobj_sp = valobj_sp->GetSyntheticArrayMember (child_index, true);
951                                         if (!child_valobj_sp)
952                                         {
953                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
954                                             error.SetErrorStringWithFormat ("failed to use pointer as array for index %ld for \"(%s) %s\"",
955                                                                             child_index,
956                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
957                                                                             var_expr_path_strm.GetString().c_str());
958                                         }
959                                     }
960                                 }
961                                 else if (valobj_sp->GetCompilerType().IsArrayType(nullptr, nullptr, &is_incomplete_array))
962                                 {
963                                     // Pass false to dynamic_value here so we can tell the difference between
964                                     // no dynamic value and no member of this type...
965                                     child_valobj_sp = valobj_sp->GetChildAtIndex (child_index, true);
966                                     if (!child_valobj_sp && (is_incomplete_array || !no_synth_child))
967                                         child_valobj_sp = valobj_sp->GetSyntheticArrayMember (child_index, true);
968 
969                                     if (!child_valobj_sp)
970                                     {
971                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
972                                         error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
973                                                                         child_index,
974                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
975                                                                         var_expr_path_strm.GetString().c_str());
976                                     }
977                                 }
978                                 else if (valobj_sp->GetCompilerType().IsScalarType())
979                                 {
980                                     // this is a bitfield asking to display just one bit
981                                     child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, child_index, true);
982                                     if (!child_valobj_sp)
983                                     {
984                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
985                                         error.SetErrorStringWithFormat ("bitfield range %ld-%ld is not valid for \"(%s) %s\"",
986                                                                         child_index, child_index,
987                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
988                                                                         var_expr_path_strm.GetString().c_str());
989                                     }
990                                 }
991                                 else
992                                 {
993                                     ValueObjectSP synthetic = valobj_sp->GetSyntheticValue();
994                                     if (no_synth_child /* synthetic is forbidden */ ||
995                                         !synthetic /* no synthetic */
996                                         || synthetic == valobj_sp) /* synthetic is the same as the original object */
997                                     {
998                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
999                                         error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
1000                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
1001                                                                         var_expr_path_strm.GetString().c_str());
1002                                     }
1003                                     else if (static_cast<uint32_t>(child_index) >= synthetic->GetNumChildren() /* synthetic does not have that many values */)
1004                                     {
1005                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1006                                         error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
1007                                                                         child_index,
1008                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
1009                                                                         var_expr_path_strm.GetString().c_str());
1010                                     }
1011                                     else
1012                                     {
1013                                         child_valobj_sp = synthetic->GetChildAtIndex(child_index, true);
1014                                         if (!child_valobj_sp)
1015                                         {
1016                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1017                                             error.SetErrorStringWithFormat ("array index %ld is not valid for \"(%s) %s\"",
1018                                                                             child_index,
1019                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1020                                                                             var_expr_path_strm.GetString().c_str());
1021                                         }
1022                                     }
1023                                 }
1024 
1025                                 if (!child_valobj_sp)
1026                                 {
1027                                     // Invalid array index...
1028                                     return ValueObjectSP();
1029                                 }
1030 
1031                                 // Erase the array member specification '[%i]' where
1032                                 // %i is the array index
1033                                 var_path.erase(0, (end - var_path.c_str()) + 1);
1034                                 separator_idx = var_path.find_first_of(".-[");
1035                                 if (use_dynamic != eNoDynamicValues)
1036                                 {
1037                                     ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
1038                                     if (dynamic_value_sp)
1039                                         child_valobj_sp = dynamic_value_sp;
1040                                 }
1041                                 // Break out early from the switch since we were
1042                                 // able to find the child member
1043                                 break;
1044                             }
1045                             else if (end && *end == '-')
1046                             {
1047                                 // this is most probably a BitField, let's take a look
1048                                 char *real_end = nullptr;
1049                                 long final_index = ::strtol (end+1, &real_end, 0);
1050                                 bool expand_bitfield = true;
1051                                 if (real_end && *real_end == ']')
1052                                 {
1053                                     // if the format given is [high-low], swap range
1054                                     if (child_index > final_index)
1055                                     {
1056                                         long temp = child_index;
1057                                         child_index = final_index;
1058                                         final_index = temp;
1059                                     }
1060 
1061                                     if (valobj_sp->GetCompilerType().IsPointerToScalarType() && deref)
1062                                     {
1063                                         // what we have is *ptr[low-high]. the most similar C++ syntax is to deref ptr
1064                                         // and extract bits low thru high out of it. reading array items low thru high
1065                                         // would be done by saying ptr[low-high], without a deref * sign
1066                                         Error error;
1067                                         ValueObjectSP temp(valobj_sp->Dereference(error));
1068                                         if (error.Fail())
1069                                         {
1070                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1071                                             error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
1072                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1073                                                                             var_expr_path_strm.GetString().c_str());
1074                                             return ValueObjectSP();
1075                                         }
1076                                         valobj_sp = temp;
1077                                         deref = false;
1078                                     }
1079                                     else if (valobj_sp->GetCompilerType().IsArrayOfScalarType() && deref)
1080                                     {
1081                                         // what we have is *arr[low-high]. the most similar C++ syntax is to get arr[0]
1082                                         // (an operation that is equivalent to deref-ing arr)
1083                                         // and extract bits low thru high out of it. reading array items low thru high
1084                                         // would be done by saying arr[low-high], without a deref * sign
1085                                         Error error;
1086                                         ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
1087                                         if (error.Fail())
1088                                         {
1089                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1090                                             error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
1091                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1092                                                                             var_expr_path_strm.GetString().c_str());
1093                                             return ValueObjectSP();
1094                                         }
1095                                         valobj_sp = temp;
1096                                         deref = false;
1097                                     }
1098                                     /*else if (valobj_sp->IsArrayType() || valobj_sp->IsPointerType())
1099                                     {
1100                                         child_valobj_sp = valobj_sp->GetSyntheticArrayRangeChild(child_index, final_index, true);
1101                                         expand_bitfield = false;
1102                                         if (!child_valobj_sp)
1103                                         {
1104                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1105                                             error.SetErrorStringWithFormat ("array range %i-%i is not valid for \"(%s) %s\"",
1106                                                                             child_index, final_index,
1107                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1108                                                                             var_expr_path_strm.GetString().c_str());
1109                                         }
1110                                     }*/
1111 
1112                                     if (expand_bitfield)
1113                                     {
1114                                         child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
1115                                         if (!child_valobj_sp)
1116                                         {
1117                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1118                                             error.SetErrorStringWithFormat ("bitfield range %ld-%ld is not valid for \"(%s) %s\"",
1119                                                                             child_index, final_index,
1120                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
1121                                                                             var_expr_path_strm.GetString().c_str());
1122                                         }
1123                                     }
1124                                 }
1125 
1126                                 if (!child_valobj_sp)
1127                                 {
1128                                     // Invalid bitfield range...
1129                                     return ValueObjectSP();
1130                                 }
1131 
1132                                 // Erase the bitfield member specification '[%i-%i]' where
1133                                 // %i is the index
1134                                 var_path.erase(0, (real_end - var_path.c_str()) + 1);
1135                                 separator_idx = var_path.find_first_of(".-[");
1136                                 if (use_dynamic != eNoDynamicValues)
1137                                 {
1138                                     ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
1139                                     if (dynamic_value_sp)
1140                                         child_valobj_sp = dynamic_value_sp;
1141                                 }
1142                                 // Break out early from the switch since we were
1143                                 // able to find the child member
1144                                 break;
1145 
1146                             }
1147                         }
1148                         else
1149                         {
1150                             error.SetErrorStringWithFormat("invalid square bracket encountered after \"%s\" in \"%s\"",
1151                                                            var_expr_path_strm.GetString().c_str(),
1152                                                            var_path.c_str());
1153                         }
1154                         return ValueObjectSP();
1155 
1156                     default:
1157                         // Failure...
1158                         {
1159                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
1160                             error.SetErrorStringWithFormat ("unexpected char '%c' encountered after \"%s\" in \"%s\"",
1161                                                             separator_type,
1162                                                             var_expr_path_strm.GetString().c_str(),
1163                                                             var_path.c_str());
1164 
1165                             return ValueObjectSP();
1166                         }
1167                     }
1168 
1169                     if (child_valobj_sp)
1170                         valobj_sp = child_valobj_sp;
1171 
1172                     if (var_path.empty())
1173                         break;
1174                 }
1175                 if (valobj_sp)
1176                 {
1177                     if (deref)
1178                     {
1179                         ValueObjectSP deref_valobj_sp (valobj_sp->Dereference(error));
1180                         valobj_sp = deref_valobj_sp;
1181                     }
1182                     else if (address_of)
1183                     {
1184                         ValueObjectSP address_of_valobj_sp (valobj_sp->AddressOf(error));
1185                         valobj_sp = address_of_valobj_sp;
1186                     }
1187                 }
1188                 return valobj_sp;
1189             }
1190             else
1191             {
1192                 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
1193                                                name_const_string.GetCString());
1194             }
1195         }
1196     }
1197     else
1198     {
1199         error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
1200     }
1201     return ValueObjectSP();
1202 }
1203 
1204 bool
1205 StackFrame::GetFrameBaseValue (Scalar &frame_base, Error *error_ptr)
1206 {
1207     Mutex::Locker locker(m_mutex);
1208     if (!m_cfa_is_valid)
1209     {
1210         m_frame_base_error.SetErrorString("No frame base available for this historical stack frame.");
1211         return false;
1212     }
1213 
1214     if (m_flags.IsClear(GOT_FRAME_BASE))
1215     {
1216         if (m_sc.function)
1217         {
1218             m_frame_base.Clear();
1219             m_frame_base_error.Clear();
1220 
1221             m_flags.Set(GOT_FRAME_BASE);
1222             ExecutionContext exe_ctx (shared_from_this());
1223             Value expr_value;
1224             addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
1225             if (m_sc.function->GetFrameBaseExpression().IsLocationList())
1226                 loclist_base_addr = m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (exe_ctx.GetTargetPtr());
1227 
1228             if (m_sc.function->GetFrameBaseExpression().Evaluate(&exe_ctx,
1229                                                                  nullptr,
1230                                                                  nullptr,
1231                                                                  nullptr,
1232                                                                  loclist_base_addr,
1233                                                                  nullptr,
1234                                                                  nullptr,
1235                                                                  expr_value,
1236                                                                  &m_frame_base_error) == false)
1237             {
1238                 // We should really have an error if evaluate returns, but in case
1239                 // we don't, lets set the error to something at least.
1240                 if (m_frame_base_error.Success())
1241                     m_frame_base_error.SetErrorString("Evaluation of the frame base expression failed.");
1242             }
1243             else
1244             {
1245                 m_frame_base = expr_value.ResolveValue(&exe_ctx);
1246             }
1247         }
1248         else
1249         {
1250             m_frame_base_error.SetErrorString ("No function in symbol context.");
1251         }
1252     }
1253 
1254     if (m_frame_base_error.Success())
1255         frame_base = m_frame_base;
1256 
1257     if (error_ptr)
1258         *error_ptr = m_frame_base_error;
1259     return m_frame_base_error.Success();
1260 }
1261 
1262 RegisterContextSP
1263 StackFrame::GetRegisterContext ()
1264 {
1265     Mutex::Locker locker(m_mutex);
1266     if (!m_reg_context_sp)
1267     {
1268         ThreadSP thread_sp (GetThread());
1269         if (thread_sp)
1270             m_reg_context_sp = thread_sp->CreateRegisterContextForFrame (this);
1271     }
1272     return m_reg_context_sp;
1273 }
1274 
1275 bool
1276 StackFrame::HasDebugInformation ()
1277 {
1278     GetSymbolContext (eSymbolContextLineEntry);
1279     return m_sc.line_entry.IsValid();
1280 }
1281 
1282 ValueObjectSP
1283 StackFrame::GetValueObjectForFrameVariable (const VariableSP &variable_sp, DynamicValueType use_dynamic)
1284 {
1285     Mutex::Locker locker(m_mutex);
1286     ValueObjectSP valobj_sp;
1287     if (m_is_history_frame)
1288     {
1289         return valobj_sp;
1290     }
1291     VariableList *var_list = GetVariableList (true);
1292     if (var_list)
1293     {
1294         // Make sure the variable is a frame variable
1295         const uint32_t var_idx = var_list->FindIndexForVariable (variable_sp.get());
1296         const uint32_t num_variables = var_list->GetSize();
1297         if (var_idx < num_variables)
1298         {
1299             valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex (var_idx);
1300             if (!valobj_sp)
1301             {
1302                 if (m_variable_list_value_objects.GetSize() < num_variables)
1303                     m_variable_list_value_objects.Resize(num_variables);
1304                 valobj_sp = ValueObjectVariable::Create (this, variable_sp);
1305                 m_variable_list_value_objects.SetValueObjectAtIndex (var_idx, valobj_sp);
1306             }
1307         }
1308     }
1309     if (use_dynamic != eNoDynamicValues && valobj_sp)
1310     {
1311         ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue (use_dynamic);
1312         if (dynamic_sp)
1313             return dynamic_sp;
1314     }
1315     return valobj_sp;
1316 }
1317 
1318 ValueObjectSP
1319 StackFrame::TrackGlobalVariable (const VariableSP &variable_sp, DynamicValueType use_dynamic)
1320 {
1321     Mutex::Locker locker(m_mutex);
1322     if (m_is_history_frame)
1323         return ValueObjectSP();
1324 
1325     // Check to make sure we aren't already tracking this variable?
1326     ValueObjectSP valobj_sp (GetValueObjectForFrameVariable (variable_sp, use_dynamic));
1327     if (!valobj_sp)
1328     {
1329         // We aren't already tracking this global
1330         VariableList *var_list = GetVariableList (true);
1331         // If this frame has no variables, create a new list
1332         if (var_list == nullptr)
1333             m_variable_list_sp.reset (new VariableList());
1334 
1335         // Add the global/static variable to this frame
1336         m_variable_list_sp->AddVariable (variable_sp);
1337 
1338         // Now make a value object for it so we can track its changes
1339         valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
1340     }
1341     return valobj_sp;
1342 }
1343 
1344 bool
1345 StackFrame::IsInlined ()
1346 {
1347     if (m_sc.block == nullptr)
1348         GetSymbolContext (eSymbolContextBlock);
1349     if (m_sc.block)
1350         return m_sc.block->GetContainingInlinedBlock() != nullptr;
1351     return false;
1352 }
1353 
1354 lldb::LanguageType
1355 StackFrame::GetLanguage ()
1356 {
1357     CompileUnit *cu = GetSymbolContext(eSymbolContextCompUnit).comp_unit;
1358     if (cu)
1359         return cu->GetLanguage();
1360     return lldb::eLanguageTypeUnknown;
1361 }
1362 
1363 lldb::LanguageType
1364 StackFrame::GuessLanguage ()
1365 {
1366     LanguageType lang_type = GetLanguage();
1367 
1368     if (lang_type == eLanguageTypeUnknown)
1369     {
1370         Function *f = GetSymbolContext(eSymbolContextFunction).function;
1371         if (f)
1372         {
1373             lang_type = f->GetMangled().GuessLanguage();
1374         }
1375     }
1376 
1377     return lang_type;
1378 }
1379 
1380 TargetSP
1381 StackFrame::CalculateTarget ()
1382 {
1383     TargetSP target_sp;
1384     ThreadSP thread_sp(GetThread());
1385     if (thread_sp)
1386     {
1387         ProcessSP process_sp (thread_sp->CalculateProcess());
1388         if (process_sp)
1389             target_sp = process_sp->CalculateTarget();
1390     }
1391     return target_sp;
1392 }
1393 
1394 ProcessSP
1395 StackFrame::CalculateProcess ()
1396 {
1397     ProcessSP process_sp;
1398     ThreadSP thread_sp(GetThread());
1399     if (thread_sp)
1400         process_sp = thread_sp->CalculateProcess();
1401     return process_sp;
1402 }
1403 
1404 ThreadSP
1405 StackFrame::CalculateThread ()
1406 {
1407     return GetThread();
1408 }
1409 
1410 StackFrameSP
1411 StackFrame::CalculateStackFrame ()
1412 {
1413     return shared_from_this();
1414 }
1415 
1416 void
1417 StackFrame::CalculateExecutionContext (ExecutionContext &exe_ctx)
1418 {
1419     exe_ctx.SetContext (shared_from_this());
1420 }
1421 
1422 void
1423 StackFrame::DumpUsingSettingsFormat (Stream *strm, const char *frame_marker)
1424 {
1425     if (strm == nullptr)
1426         return;
1427 
1428     GetSymbolContext(eSymbolContextEverything);
1429     ExecutionContext exe_ctx (shared_from_this());
1430     StreamString s;
1431 
1432     if (frame_marker)
1433         s.PutCString(frame_marker);
1434 
1435     const FormatEntity::Entry *frame_format = nullptr;
1436     Target *target = exe_ctx.GetTargetPtr();
1437     if (target)
1438         frame_format = target->GetDebugger().GetFrameFormat();
1439     if (frame_format && FormatEntity::Format(*frame_format, s, &m_sc, &exe_ctx, nullptr, nullptr, false, false))
1440     {
1441         strm->Write(s.GetData(), s.GetSize());
1442     }
1443     else
1444     {
1445         Dump (strm, true, false);
1446         strm->EOL();
1447     }
1448 }
1449 
1450 void
1451 StackFrame::Dump (Stream *strm, bool show_frame_index, bool show_fullpaths)
1452 {
1453     if (strm == nullptr)
1454         return;
1455 
1456     if (show_frame_index)
1457         strm->Printf("frame #%u: ", m_frame_index);
1458     ExecutionContext exe_ctx (shared_from_this());
1459     Target *target = exe_ctx.GetTargetPtr();
1460     strm->Printf("0x%0*" PRIx64 " ",
1461                  target ? (target->GetArchitecture().GetAddressByteSize() * 2) : 16,
1462                  GetFrameCodeAddress().GetLoadAddress(target));
1463     GetSymbolContext(eSymbolContextEverything);
1464     const bool show_module = true;
1465     const bool show_inline = true;
1466     const bool show_function_arguments = true;
1467     const bool show_function_name = true;
1468     m_sc.DumpStopContext (strm,
1469                           exe_ctx.GetBestExecutionContextScope(),
1470                           GetFrameCodeAddress(),
1471                           show_fullpaths,
1472                           show_module,
1473                           show_inline,
1474                           show_function_arguments,
1475                           show_function_name);
1476 }
1477 
1478 void
1479 StackFrame::UpdateCurrentFrameFromPreviousFrame (StackFrame &prev_frame)
1480 {
1481     Mutex::Locker locker(m_mutex);
1482     assert (GetStackID() == prev_frame.GetStackID());    // TODO: remove this after some testing
1483     m_variable_list_sp = prev_frame.m_variable_list_sp;
1484     m_variable_list_value_objects.Swap (prev_frame.m_variable_list_value_objects);
1485     if (!m_disassembly.GetString().empty())
1486         m_disassembly.GetString().swap (m_disassembly.GetString());
1487 }
1488 
1489 void
1490 StackFrame::UpdatePreviousFrameFromCurrentFrame (StackFrame &curr_frame)
1491 {
1492     Mutex::Locker locker(m_mutex);
1493     assert (GetStackID() == curr_frame.GetStackID());        // TODO: remove this after some testing
1494     m_id.SetPC (curr_frame.m_id.GetPC());       // Update the Stack ID PC value
1495     assert (GetThread() == curr_frame.GetThread());
1496     m_frame_index = curr_frame.m_frame_index;
1497     m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1498     m_reg_context_sp = curr_frame.m_reg_context_sp;
1499     m_frame_code_addr = curr_frame.m_frame_code_addr;
1500     assert (!m_sc.target_sp || !curr_frame.m_sc.target_sp || m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
1501     assert (!m_sc.module_sp || !curr_frame.m_sc.module_sp || m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
1502     assert (m_sc.comp_unit == nullptr || curr_frame.m_sc.comp_unit == nullptr || m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1503     assert (m_sc.function == nullptr || curr_frame.m_sc.function == nullptr || m_sc.function == curr_frame.m_sc.function);
1504     m_sc = curr_frame.m_sc;
1505     m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1506     m_flags.Set (m_sc.GetResolvedMask());
1507     m_frame_base.Clear();
1508     m_frame_base_error.Clear();
1509 }
1510 
1511 bool
1512 StackFrame::HasCachedData () const
1513 {
1514     if (m_variable_list_sp)
1515         return true;
1516     if (m_variable_list_value_objects.GetSize() > 0)
1517         return true;
1518     if (!m_disassembly.GetString().empty())
1519         return true;
1520     return false;
1521 }
1522 
1523 bool
1524 StackFrame::GetStatus (Stream& strm,
1525                        bool show_frame_info,
1526                        bool show_source,
1527                        const char *frame_marker)
1528 {
1529 
1530     if (show_frame_info)
1531     {
1532         strm.Indent();
1533         DumpUsingSettingsFormat (&strm, frame_marker);
1534     }
1535 
1536     if (show_source)
1537     {
1538         ExecutionContext exe_ctx (shared_from_this());
1539         bool have_source = false, have_debuginfo = false;
1540         Debugger::StopDisassemblyType disasm_display = Debugger::eStopDisassemblyTypeNever;
1541         Target *target = exe_ctx.GetTargetPtr();
1542         if (target)
1543         {
1544             Debugger &debugger = target->GetDebugger();
1545             const uint32_t source_lines_before = debugger.GetStopSourceLineCount(true);
1546             const uint32_t source_lines_after = debugger.GetStopSourceLineCount(false);
1547             disasm_display = debugger.GetStopDisassemblyDisplay ();
1548 
1549             GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1550             if (m_sc.comp_unit && m_sc.line_entry.IsValid())
1551             {
1552                 have_debuginfo = true;
1553                 if (source_lines_before > 0 || source_lines_after > 0)
1554                 {
1555                     size_t num_lines = target->GetSourceManager().DisplaySourceLinesWithLineNumbers (m_sc.line_entry.file,
1556                                                                                       m_sc.line_entry.line,
1557                                                                                       source_lines_before,
1558                                                                                       source_lines_after,
1559                                                                                       "->",
1560                                                                                       &strm);
1561                     if (num_lines != 0)
1562                         have_source = true;
1563                     // TODO: Give here a one time warning if source file is missing.
1564                 }
1565             }
1566             switch (disasm_display)
1567             {
1568             case Debugger::eStopDisassemblyTypeNever:
1569                 break;
1570 
1571             case Debugger::eStopDisassemblyTypeNoDebugInfo:
1572                 if (have_debuginfo)
1573                     break;
1574                 LLVM_FALLTHROUGH;
1575 
1576             case Debugger::eStopDisassemblyTypeNoSource:
1577                 if (have_source)
1578                     break;
1579                 LLVM_FALLTHROUGH;
1580 
1581             case Debugger::eStopDisassemblyTypeAlways:
1582                 if (target)
1583                 {
1584                     const uint32_t disasm_lines = debugger.GetDisassemblyLineCount();
1585                     if (disasm_lines > 0)
1586                     {
1587                         const ArchSpec &target_arch = target->GetArchitecture();
1588                         AddressRange pc_range;
1589                         pc_range.GetBaseAddress() = GetFrameCodeAddress();
1590                         pc_range.SetByteSize(disasm_lines * target_arch.GetMaximumOpcodeByteSize());
1591                         const char *plugin_name = nullptr;
1592                         const char *flavor = nullptr;
1593                         Disassembler::Disassemble (target->GetDebugger(),
1594                                                    target_arch,
1595                                                    plugin_name,
1596                                                    flavor,
1597                                                    exe_ctx,
1598                                                    pc_range,
1599                                                    disasm_lines,
1600                                                    0,
1601                                                    Disassembler::eOptionMarkPCAddress,
1602                                                    strm);
1603                     }
1604                 }
1605                 break;
1606             }
1607         }
1608     }
1609     return true;
1610 }
1611