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