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