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