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 #include "lldb/Target/StackFrame.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/Disassembler.h"
19 #include "lldb/Core/Value.h"
20 #include "lldb/Core/ValueObjectVariable.h"
21 #include "lldb/Core/ValueObjectConstResult.h"
22 #include "lldb/Symbol/Function.h"
23 #include "lldb/Symbol/VariableList.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/RegisterContext.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/Thread.h"
29 
30 using namespace lldb;
31 using namespace lldb_private;
32 
33 // The first bits in the flags are reserved for the SymbolContext::Scope bits
34 // so we know if we have tried to look up information in our internal symbol
35 // context (m_sc) already.
36 #define RESOLVED_FRAME_CODE_ADDR        (uint32_t(eSymbolContextEverything + 1))
37 #define RESOLVED_FRAME_ID_SYMBOL_SCOPE  (RESOLVED_FRAME_CODE_ADDR << 1)
38 #define GOT_FRAME_BASE                  (RESOLVED_FRAME_ID_SYMBOL_SCOPE << 1)
39 #define RESOLVED_VARIABLES              (GOT_FRAME_BASE << 1)
40 #define RESOLVED_GLOBAL_VARIABLES       (RESOLVED_VARIABLES << 1)
41 
42 StackFrame::StackFrame
43 (
44     lldb::user_id_t frame_idx,
45     lldb::user_id_t unwind_frame_index,
46     Thread &thread,
47     lldb::addr_t cfa,
48     lldb::addr_t pc,
49     const SymbolContext *sc_ptr
50 ) :
51     m_thread (thread),
52     m_frame_index (frame_idx),
53     m_concrete_frame_index (unwind_frame_index),
54     m_reg_context_sp (),
55     m_id (pc, cfa, NULL),
56     m_frame_code_addr (NULL, pc),
57     m_sc (),
58     m_flags (),
59     m_frame_base (),
60     m_frame_base_error (),
61     m_variable_list_sp (),
62     m_variable_list_value_objects (),
63     m_disassembly ()
64 {
65     if (sc_ptr != NULL)
66     {
67         m_sc = *sc_ptr;
68         m_flags.Set(m_sc.GetResolvedMask ());
69     }
70 }
71 
72 StackFrame::StackFrame
73 (
74     lldb::user_id_t frame_idx,
75     lldb::user_id_t unwind_frame_index,
76     Thread &thread,
77     const RegisterContextSP &reg_context_sp,
78     lldb::addr_t cfa,
79     lldb::addr_t pc,
80     const SymbolContext *sc_ptr
81 ) :
82     m_thread (thread),
83     m_frame_index (frame_idx),
84     m_concrete_frame_index (unwind_frame_index),
85     m_reg_context_sp (reg_context_sp),
86     m_id (pc, cfa, NULL),
87     m_frame_code_addr (NULL, pc),
88     m_sc (),
89     m_flags (),
90     m_frame_base (),
91     m_frame_base_error (),
92     m_variable_list_sp (),
93     m_variable_list_value_objects (),
94     m_disassembly ()
95 {
96     if (sc_ptr != NULL)
97     {
98         m_sc = *sc_ptr;
99         m_flags.Set(m_sc.GetResolvedMask ());
100     }
101 
102     if (reg_context_sp && !m_sc.target_sp)
103     {
104         m_sc.target_sp = reg_context_sp->GetThread().GetProcess().GetTarget().GetSP();
105         m_flags.Set (eSymbolContextTarget);
106     }
107 }
108 
109 StackFrame::StackFrame
110 (
111     lldb::user_id_t frame_idx,
112     lldb::user_id_t unwind_frame_index,
113     Thread &thread,
114     const RegisterContextSP &reg_context_sp,
115     lldb::addr_t cfa,
116     const Address& pc_addr,
117     const SymbolContext *sc_ptr
118 ) :
119     m_thread (thread),
120     m_frame_index (frame_idx),
121     m_concrete_frame_index (unwind_frame_index),
122     m_reg_context_sp (reg_context_sp),
123     m_id (pc_addr.GetLoadAddress (&thread.GetProcess().GetTarget()), cfa, NULL),
124     m_frame_code_addr (pc_addr),
125     m_sc (),
126     m_flags (),
127     m_frame_base (),
128     m_frame_base_error (),
129     m_variable_list_sp (),
130     m_variable_list_value_objects (),
131     m_disassembly ()
132 {
133     if (sc_ptr != NULL)
134     {
135         m_sc = *sc_ptr;
136         m_flags.Set(m_sc.GetResolvedMask ());
137     }
138 
139     if (m_sc.target_sp.get() == NULL && reg_context_sp)
140     {
141         m_sc.target_sp = reg_context_sp->GetThread().GetProcess().GetTarget().GetSP();
142         m_flags.Set (eSymbolContextTarget);
143     }
144 
145     Module *pc_module = pc_addr.GetModule();
146     if (m_sc.module_sp.get() == NULL || m_sc.module_sp.get() != pc_module)
147     {
148         if (pc_module)
149         {
150             m_sc.module_sp = pc_module->GetSP();
151             m_flags.Set (eSymbolContextModule);
152         }
153         else
154         {
155             m_sc.module_sp.reset();
156         }
157 
158     }
159 }
160 
161 
162 //----------------------------------------------------------------------
163 // Destructor
164 //----------------------------------------------------------------------
165 StackFrame::~StackFrame()
166 {
167 }
168 
169 StackID&
170 StackFrame::GetStackID()
171 {
172     // Make sure we have resolved the StackID object's symbol context scope if
173     // we already haven't looked it up.
174 
175     if (m_flags.IsClear (RESOLVED_FRAME_ID_SYMBOL_SCOPE))
176     {
177         if (m_id.GetSymbolContextScope ())
178         {
179             // We already have a symbol context scope, we just don't have our
180             // flag bit set.
181             m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
182         }
183         else
184         {
185             // Calculate the frame block and use this for the stack ID symbol
186             // context scope if we have one.
187             SymbolContextScope *scope = GetFrameBlock ();
188             if (scope == NULL)
189             {
190                 // We don't have a block, so use the symbol
191                 if (m_flags.IsClear (eSymbolContextSymbol))
192                     GetSymbolContext (eSymbolContextSymbol);
193 
194                 // It is ok if m_sc.symbol is NULL here
195                 scope = m_sc.symbol;
196             }
197             // Set the symbol context scope (the accessor will set the
198             // RESOLVED_FRAME_ID_SYMBOL_SCOPE bit in m_flags).
199             SetSymbolContextScope (scope);
200         }
201     }
202     return m_id;
203 }
204 
205 void
206 StackFrame::SetSymbolContextScope (SymbolContextScope *symbol_scope)
207 {
208     m_flags.Set (RESOLVED_FRAME_ID_SYMBOL_SCOPE);
209     m_id.SetSymbolContextScope (symbol_scope);
210 }
211 
212 Address&
213 StackFrame::GetFrameCodeAddress()
214 {
215     if (m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR) && !m_frame_code_addr.IsSectionOffset())
216     {
217         m_flags.Set (RESOLVED_FRAME_CODE_ADDR);
218 
219         // Resolve the PC into a temporary address because if ResolveLoadAddress
220         // fails to resolve the address, it will clear the address object...
221         Address resolved_pc;
222         if (m_thread.GetProcess().GetTarget().GetSectionLoadList().ResolveLoadAddress(m_frame_code_addr.GetOffset(), resolved_pc))
223         {
224             m_frame_code_addr = resolved_pc;
225             const Section *section = m_frame_code_addr.GetSection();
226             if (section)
227             {
228                 Module *module = section->GetModule();
229                 if (module)
230                 {
231                     m_sc.module_sp = module->GetSP();
232                     if (m_sc.module_sp)
233                         m_flags.Set(eSymbolContextModule);
234                 }
235             }
236         }
237     }
238     return m_frame_code_addr;
239 }
240 
241 void
242 StackFrame::ChangePC (addr_t pc)
243 {
244     m_frame_code_addr.SetOffset(pc);
245     m_frame_code_addr.SetSection(NULL);
246     m_sc.Clear();
247     m_flags.Reset(0);
248     m_thread.ClearStackFrames ();
249 }
250 
251 const char *
252 StackFrame::Disassemble ()
253 {
254     if (m_disassembly.GetSize() == 0)
255     {
256         ExecutionContext exe_ctx;
257         CalculateExecutionContext(exe_ctx);
258         Target &target = m_thread.GetProcess().GetTarget();
259         Disassembler::Disassemble (target.GetDebugger(),
260                                    target.GetArchitecture(),
261                                    exe_ctx,
262                                    0,
263                                    false,
264                                    m_disassembly);
265         if (m_disassembly.GetSize() == 0)
266             return NULL;
267     }
268     return m_disassembly.GetData();
269 }
270 
271 Block *
272 StackFrame::GetFrameBlock ()
273 {
274     if (m_sc.block == NULL && m_flags.IsClear (eSymbolContextBlock))
275         GetSymbolContext (eSymbolContextBlock);
276 
277     if (m_sc.block)
278     {
279         Block *inline_block = m_sc.block->GetContainingInlinedBlock();
280         if (inline_block)
281         {
282             // Use the block with the inlined function info
283             // as the frame block we want this frame to have only the variables
284             // for the inlined function and its non-inlined block child blocks.
285             return inline_block;
286         }
287         else
288         {
289             // This block is not contained withing any inlined function blocks
290             // with so we want to use the top most function block.
291             return &m_sc.function->GetBlock (false);
292         }
293     }
294     return NULL;
295 }
296 
297 //----------------------------------------------------------------------
298 // Get the symbol context if we already haven't done so by resolving the
299 // PC address as much as possible. This way when we pass around a
300 // StackFrame object, everyone will have as much information as
301 // possible and no one will ever have to look things up manually.
302 //----------------------------------------------------------------------
303 const SymbolContext&
304 StackFrame::GetSymbolContext (uint32_t resolve_scope)
305 {
306     // Copy our internal symbol context into "sc".
307     if ((m_flags.Get() & resolve_scope) != resolve_scope)
308     {
309         // Resolve our PC to section offset if we haven't alreday done so
310         // and if we don't have a module. The resolved address section will
311         // contain the module to which it belongs
312         if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
313             GetFrameCodeAddress();
314 
315         // If this is not frame zero, then we need to subtract 1 from the PC
316         // value when doing address lookups since the PC will be on the
317         // instruction following the function call instruction...
318 
319         Address lookup_addr(GetFrameCodeAddress());
320         if (m_frame_index > 0 && lookup_addr.IsValid())
321         {
322             addr_t offset = lookup_addr.GetOffset();
323             if (offset > 0)
324                 lookup_addr.SetOffset(offset - 1);
325         }
326 
327 
328         uint32_t resolved = 0;
329         if (m_sc.module_sp)
330         {
331             // We have something in our stack frame symbol context, lets check
332             // if we haven't already tried to lookup one of those things. If we
333             // haven't then we will do the query.
334 
335             uint32_t actual_resolve_scope = 0;
336 
337             if (resolve_scope & eSymbolContextCompUnit)
338             {
339                 if (m_flags.IsClear (eSymbolContextCompUnit))
340                 {
341                     if (m_sc.comp_unit)
342                         resolved |= eSymbolContextCompUnit;
343                     else
344                         actual_resolve_scope |= eSymbolContextCompUnit;
345                 }
346             }
347 
348             if (resolve_scope & eSymbolContextFunction)
349             {
350                 if (m_flags.IsClear (eSymbolContextFunction))
351                 {
352                     if (m_sc.function)
353                         resolved |= eSymbolContextFunction;
354                     else
355                         actual_resolve_scope |= eSymbolContextFunction;
356                 }
357             }
358 
359             if (resolve_scope & eSymbolContextBlock)
360             {
361                 if (m_flags.IsClear (eSymbolContextBlock))
362                 {
363                     if (m_sc.block)
364                         resolved |= eSymbolContextBlock;
365                     else
366                         actual_resolve_scope |= eSymbolContextBlock;
367                 }
368             }
369 
370             if (resolve_scope & eSymbolContextSymbol)
371             {
372                 if (m_flags.IsClear (eSymbolContextSymbol))
373                 {
374                     if (m_sc.symbol)
375                         resolved |= eSymbolContextSymbol;
376                     else
377                         actual_resolve_scope |= eSymbolContextSymbol;
378                 }
379             }
380 
381             if (resolve_scope & eSymbolContextLineEntry)
382             {
383                 if (m_flags.IsClear (eSymbolContextLineEntry))
384                 {
385                     if (m_sc.line_entry.IsValid())
386                         resolved |= eSymbolContextLineEntry;
387                     else
388                         actual_resolve_scope |= eSymbolContextLineEntry;
389                 }
390             }
391 
392             if (actual_resolve_scope)
393             {
394                 // We might be resolving less information than what is already
395                 // in our current symbol context so resolve into a temporary
396                 // symbol context "sc" so we don't clear out data we have
397                 // already found in "m_sc"
398                 SymbolContext sc;
399                 // Set flags that indicate what we have tried to resolve
400                 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress (lookup_addr, actual_resolve_scope, sc);
401                 // Only replace what we didn't already have as we may have
402                 // information for an inlined function scope that won't match
403                 // what a standard lookup by address would match
404                 if ((resolved & eSymbolContextCompUnit)  && m_sc.comp_unit == NULL)
405                     m_sc.comp_unit = sc.comp_unit;
406                 if ((resolved & eSymbolContextFunction)  && m_sc.function == NULL)
407                     m_sc.function = sc.function;
408                 if ((resolved & eSymbolContextBlock)     && m_sc.block == NULL)
409                     m_sc.block = sc.block;
410                 if ((resolved & eSymbolContextSymbol)    && m_sc.symbol == NULL)
411                     m_sc.symbol = sc.symbol;
412                 if ((resolved & eSymbolContextLineEntry) && !m_sc.line_entry.IsValid())
413                     m_sc.line_entry = sc.line_entry;
414 
415             }
416         }
417         else
418         {
419             // If we don't have a module, then we can't have the compile unit,
420             // function, block, line entry or symbol, so we can safely call
421             // ResolveSymbolContextForAddress with our symbol context member m_sc.
422             resolved |= m_thread.GetProcess().GetTarget().GetImages().ResolveSymbolContextForAddress (lookup_addr, resolve_scope, m_sc);
423         }
424 
425         // If the target was requested add that:
426         if (m_sc.target_sp.get() == NULL)
427         {
428             m_sc.target_sp = CalculateProcess()->GetTarget().GetSP();
429             if (m_sc.target_sp)
430                 resolved |= eSymbolContextTarget;
431         }
432 
433         // Update our internal flags so we remember what we have tried to locate so
434         // we don't have to keep trying when more calls to this function are made.
435         // We might have dug up more information that was requested (for example
436         // if we were asked to only get the block, we will have gotten the
437         // compile unit, and function) so set any additional bits that we resolved
438         m_flags.Set (resolve_scope | resolved);
439     }
440 
441     // Return the symbol context with everything that was possible to resolve
442     // resolved.
443     return m_sc;
444 }
445 
446 
447 VariableList *
448 StackFrame::GetVariableList (bool get_file_globals)
449 {
450     if (m_flags.IsClear(RESOLVED_VARIABLES))
451     {
452         m_flags.Set(RESOLVED_VARIABLES);
453 
454         Block *frame_block = GetFrameBlock();
455 
456         if (frame_block)
457         {
458             const bool get_child_variables = true;
459             const bool can_create = true;
460             m_variable_list_sp = frame_block->GetVariableList (get_child_variables, can_create);
461         }
462     }
463 
464     if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) &&
465         get_file_globals)
466     {
467         m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
468 
469         if (m_flags.IsClear (eSymbolContextCompUnit))
470             GetSymbolContext (eSymbolContextCompUnit);
471 
472         if (m_sc.comp_unit)
473         {
474             VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
475             if (m_variable_list_sp)
476                 m_variable_list_sp->AddVariables (global_variable_list_sp.get());
477             else
478                 m_variable_list_sp = global_variable_list_sp;
479         }
480     }
481 
482     return m_variable_list_sp.get();
483 }
484 
485 ValueObjectSP
486 StackFrame::GetValueForVariableExpressionPath (const char *var_expr_cstr, uint32_t options, Error &error)
487 {
488 
489     if (var_expr_cstr && var_expr_cstr[0])
490     {
491         const bool check_ptr_vs_member = (options & eExpressionPathOptionCheckPtrVsMember) != 0;
492         const bool no_fragile_ivar = (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
493         error.Clear();
494         bool deref = false;
495         bool address_of = false;
496         ValueObjectSP valobj_sp;
497         const bool get_file_globals = true;
498         VariableList *variable_list = GetVariableList (get_file_globals);
499 
500         if (variable_list)
501         {
502             // If first character is a '*', then show pointer contents
503             const char *var_expr = var_expr_cstr;
504             if (var_expr[0] == '*')
505             {
506                 deref = true;
507                 var_expr++; // Skip the '*'
508             }
509             else if (var_expr[0] == '&')
510             {
511                 address_of = true;
512                 var_expr++; // Skip the '&'
513             }
514 
515             std::string var_path (var_expr);
516             size_t separator_idx = var_path.find_first_of(".-[=+~|&^%#@!/?,<>{}");
517             StreamString var_expr_path_strm;
518 
519             ConstString name_const_string;
520             if (separator_idx == std::string::npos)
521                 name_const_string.SetCString (var_path.c_str());
522             else
523                 name_const_string.SetCStringWithLength (var_path.c_str(), separator_idx);
524 
525             VariableSP var_sp (variable_list->FindVariable(name_const_string));
526             if (var_sp)
527             {
528                 valobj_sp = GetValueObjectForFrameVariable (var_sp);
529 
530                 var_path.erase (0, name_const_string.GetLength ());
531                 // We are dumping at least one child
532                 while (separator_idx != std::string::npos)
533                 {
534                     // Calculate the next separator index ahead of time
535                     ValueObjectSP child_valobj_sp;
536                     const char separator_type = var_path[0];
537                     switch (separator_type)
538                     {
539 
540                     case '-':
541                         if (var_path.size() >= 2 && var_path[1] != '>')
542                             return ValueObjectSP();
543 
544                         if (no_fragile_ivar)
545                         {
546                             // Make sure we aren't trying to deref an objective
547                             // C ivar if this is not allowed
548                             const uint32_t pointer_type_flags = ClangASTContext::GetTypeInfo (valobj_sp->GetClangType(), NULL, NULL);
549                             if ((pointer_type_flags & ClangASTContext::eTypeIsObjC) &&
550                                 (pointer_type_flags & ClangASTContext::eTypeIsPointer))
551                             {
552                                 // This was an objective C object pointer and
553                                 // it was requested we skip any fragile ivars
554                                 // so return nothing here
555                                 return ValueObjectSP();
556                             }
557                         }
558                         var_path.erase (0, 1); // Remove the '-'
559                         // Fall through
560                     case '.':
561                         {
562                             const bool expr_is_ptr = var_path[0] == '>';
563 
564                             var_path.erase (0, 1); // Remove the '.' or '>'
565                             separator_idx = var_path.find_first_of(".-[");
566                             ConstString child_name;
567                             if (separator_idx == std::string::npos)
568                                 child_name.SetCString (var_path.c_str());
569                             else
570                                 child_name.SetCStringWithLength(var_path.c_str(), separator_idx);
571 
572                             if (check_ptr_vs_member)
573                             {
574                                 // We either have a pointer type and need to verify
575                                 // valobj_sp is a pointer, or we have a member of a
576                                 // class/union/struct being accessed with the . syntax
577                                 // and need to verify we don't have a pointer.
578                                 const bool actual_is_ptr = valobj_sp->IsPointerType ();
579 
580                                 if (actual_is_ptr != expr_is_ptr)
581                                 {
582                                     // Incorrect use of "." with a pointer, or "->" with
583                                     // a class/union/struct instance or reference.
584                                     valobj_sp->GetExpressionPath (var_expr_path_strm, false);
585                                     if (actual_is_ptr)
586                                         error.SetErrorStringWithFormat ("\"%s\" is a pointer and . was used to attempt to access \"%s\". Did you mean \"%s->%s\"?",
587                                                                         var_expr_path_strm.GetString().c_str(),
588                                                                         child_name.GetCString(),
589                                                                         var_expr_path_strm.GetString().c_str(),
590                                                                         var_path.c_str());
591                                     else
592                                         error.SetErrorStringWithFormat ("\"%s\" is not a pointer and -> was used to attempt to access \"%s\". Did you mean \"%s.%s\"?",
593                                                                         var_expr_path_strm.GetString().c_str(),
594                                                                         child_name.GetCString(),
595                                                                         var_expr_path_strm.GetString().c_str(),
596                                                                         var_path.c_str());
597                                     return ValueObjectSP();
598                                 }
599                             }
600 
601                             child_valobj_sp = valobj_sp->GetChildMemberWithName (child_name, true);
602                             if (!child_valobj_sp)
603                             {
604                                 // No child member with name "child_name"
605                                 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
606                                 if (child_name)
607                                 {
608                                     error.SetErrorStringWithFormat ("\"%s\" is not a member of \"(%s) %s\"",
609                                                                     child_name.GetCString(),
610                                                                     valobj_sp->GetTypeName().AsCString("<invalid type>"),
611                                                                     var_expr_path_strm.GetString().c_str());
612                                 }
613                                 else
614                                 {
615                                     error.SetErrorStringWithFormat ("incomplete expression path after \"%s\" in \"%s\"",
616                                                                     var_expr_path_strm.GetString().c_str(),
617                                                                     var_expr_cstr);
618                                 }
619 
620                                 return ValueObjectSP();
621                             }
622                             // Remove the child name from the path
623                             var_path.erase(0, child_name.GetLength());
624                         }
625                         break;
626 
627                     case '[':
628                         // Array member access, or treating pointer as an array
629                         if (var_path.size() > 2) // Need at least two brackets and a number
630                         {
631                             char *end = NULL;
632                             long child_index = ::strtol (&var_path[1], &end, 0);
633                             if (end && *end == ']')
634                             {
635 
636                                 if (valobj_sp->IsPointerType ())
637                                 {
638                                     child_valobj_sp = valobj_sp->GetSyntheticArrayMemberFromPointer (child_index, true);
639                                     if (!child_valobj_sp)
640                                     {
641                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
642                                         error.SetErrorStringWithFormat ("failed to use pointer as array for index %i for \"(%s) %s\"",
643                                                                         child_index,
644                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
645                                                                         var_expr_path_strm.GetString().c_str());
646                                     }
647                                 }
648                                 else if (ClangASTContext::IsArrayType (valobj_sp->GetClangType(), NULL, NULL))
649                                 {
650                                     child_valobj_sp = valobj_sp->GetChildAtIndex (child_index, true);
651                                     if (!child_valobj_sp)
652                                     {
653                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
654                                         error.SetErrorStringWithFormat ("array index %i is not valid for \"(%s) %s\"",
655                                                                         child_index,
656                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
657                                                                         var_expr_path_strm.GetString().c_str());
658                                     }
659                                 }
660                                 else
661                                 {
662                                     valobj_sp->GetExpressionPath (var_expr_path_strm, false);
663                                     error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
664                                                                     valobj_sp->GetTypeName().AsCString("<invalid type>"),
665                                                                     var_expr_path_strm.GetString().c_str());
666                                 }
667 
668                                 if (!child_valobj_sp)
669                                 {
670                                     // Invalid array index...
671                                     return ValueObjectSP();
672                                 }
673 
674                                 // Erase the array member specification '[%i]' where
675                                 // %i is the array index
676                                 var_path.erase(0, (end - var_path.c_str()) + 1);
677                                 separator_idx = var_path.find_first_of(".-[");
678 
679                                 // Break out early from the switch since we were
680                                 // able to find the child member
681                                 break;
682                             }
683                         }
684                         return ValueObjectSP();
685 
686                     default:
687                         // Failure...
688                         {
689                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
690                             error.SetErrorStringWithFormat ("unexpected char '%c' encountered after \"%s\" in \"%s\"",
691                                                             separator_type,
692                                                             var_expr_path_strm.GetString().c_str(),
693                                                             var_path.c_str());
694 
695                             return ValueObjectSP();
696                         }
697                     }
698 
699                     if (child_valobj_sp)
700                         valobj_sp = child_valobj_sp;
701 
702                     if (var_path.empty())
703                         break;
704 
705                 }
706                 if (valobj_sp)
707                 {
708                     if (deref)
709                     {
710                         ValueObjectSP deref_valobj_sp (valobj_sp->Dereference(error));
711                         valobj_sp = deref_valobj_sp;
712                     }
713                     else if (address_of)
714                     {
715                         ValueObjectSP address_of_valobj_sp (valobj_sp->AddressOf(error));
716                         valobj_sp = address_of_valobj_sp;
717                     }
718                 }
719                 return valobj_sp;
720             }
721             else
722             {
723                 error.SetErrorStringWithFormat("no variable named '%s' found in this frame", name_const_string.GetCString());
724             }
725         }
726     }
727     else
728     {
729         error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
730     }
731     return ValueObjectSP();
732 }
733 
734 bool
735 StackFrame::GetFrameBaseValue (Scalar &frame_base, Error *error_ptr)
736 {
737     if (m_flags.IsClear(GOT_FRAME_BASE))
738     {
739         if (m_sc.function)
740         {
741             m_frame_base.Clear();
742             m_frame_base_error.Clear();
743 
744             m_flags.Set(GOT_FRAME_BASE);
745             ExecutionContext exe_ctx (&m_thread.GetProcess(), &m_thread, this);
746             Value expr_value;
747             addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
748             if (m_sc.function->GetFrameBaseExpression().IsLocationList())
749                 loclist_base_addr = m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (&m_thread.GetProcess().GetTarget());
750 
751             if (m_sc.function->GetFrameBaseExpression().Evaluate(&exe_ctx, NULL, NULL, NULL, NULL, loclist_base_addr, NULL, expr_value, &m_frame_base_error) == false)
752             {
753                 // We should really have an error if evaluate returns, but in case
754                 // we don't, lets set the error to something at least.
755                 if (m_frame_base_error.Success())
756                     m_frame_base_error.SetErrorString("Evaluation of the frame base expression failed.");
757             }
758             else
759             {
760                 m_frame_base = expr_value.ResolveValue(&exe_ctx, NULL);
761             }
762         }
763         else
764         {
765             m_frame_base_error.SetErrorString ("No function in symbol context.");
766         }
767     }
768 
769     if (m_frame_base_error.Success())
770         frame_base = m_frame_base;
771 
772     if (error_ptr)
773         *error_ptr = m_frame_base_error;
774     return m_frame_base_error.Success();
775 }
776 
777 RegisterContextSP
778 StackFrame::GetRegisterContext ()
779 {
780     if (!m_reg_context_sp)
781         m_reg_context_sp = m_thread.CreateRegisterContextForFrame (this);
782     return m_reg_context_sp;
783 }
784 
785 bool
786 StackFrame::HasDebugInformation ()
787 {
788     GetSymbolContext (eSymbolContextLineEntry);
789     return m_sc.line_entry.IsValid();
790 }
791 
792 
793 ValueObjectSP
794 StackFrame::GetValueObjectForFrameVariable (const VariableSP &variable_sp)
795 {
796     ValueObjectSP valobj_sp;
797     VariableList *var_list = GetVariableList (true);
798     if (var_list)
799     {
800         // Make sure the variable is a frame variable
801         const uint32_t var_idx = var_list->FindIndexForVariable (variable_sp.get());
802         const uint32_t num_variables = var_list->GetSize();
803         if (var_idx < num_variables)
804         {
805             valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex (var_idx);
806             if (valobj_sp.get() == NULL)
807             {
808                 if (m_variable_list_value_objects.GetSize() < num_variables)
809                     m_variable_list_value_objects.Resize(num_variables);
810                 valobj_sp.reset (new ValueObjectVariable (variable_sp));
811                 m_variable_list_value_objects.SetValueObjectAtIndex (var_idx, valobj_sp);
812             }
813         }
814     }
815     return valobj_sp;
816 }
817 
818 ValueObjectSP
819 StackFrame::TrackGlobalVariable (const VariableSP &variable_sp)
820 {
821     // Check to make sure we aren't already tracking this variable?
822     ValueObjectSP valobj_sp (GetValueObjectForFrameVariable (variable_sp));
823     if (!valobj_sp)
824     {
825         // We aren't already tracking this global
826         VariableList *var_list = GetVariableList (true);
827         // If this frame has no variables, create a new list
828         if (var_list == NULL)
829             m_variable_list_sp.reset (new VariableList());
830 
831         // Add the global/static variable to this frame
832         m_variable_list_sp->AddVariable (variable_sp);
833 
834         // Now make a value object for it so we can track its changes
835         valobj_sp = GetValueObjectForFrameVariable (variable_sp);
836     }
837     return valobj_sp;
838 }
839 
840 bool
841 StackFrame::IsInlined ()
842 {
843     if (m_sc.block == NULL)
844         GetSymbolContext (eSymbolContextBlock);
845     if (m_sc.block)
846         return m_sc.block->GetContainingInlinedBlock() != NULL;
847     return false;
848 }
849 
850 Target *
851 StackFrame::CalculateTarget ()
852 {
853     return m_thread.CalculateTarget();
854 }
855 
856 Process *
857 StackFrame::CalculateProcess ()
858 {
859     return m_thread.CalculateProcess();
860 }
861 
862 Thread *
863 StackFrame::CalculateThread ()
864 {
865     return &m_thread;
866 }
867 
868 StackFrame *
869 StackFrame::CalculateStackFrame ()
870 {
871     return this;
872 }
873 
874 
875 void
876 StackFrame::CalculateExecutionContext (ExecutionContext &exe_ctx)
877 {
878     m_thread.CalculateExecutionContext (exe_ctx);
879     exe_ctx.frame = this;
880 }
881 
882 void
883 StackFrame::DumpUsingSettingsFormat (Stream *strm)
884 {
885     if (strm == NULL)
886         return;
887 
888     GetSymbolContext(eSymbolContextEverything);
889     ExecutionContext exe_ctx;
890     CalculateExecutionContext(exe_ctx);
891     const char *end = NULL;
892     StreamString s;
893     const char *frame_format = m_thread.GetProcess().GetTarget().GetDebugger().GetFrameFormat();
894     if (frame_format && Debugger::FormatPrompt (frame_format, &m_sc, &exe_ctx, NULL, s, &end))
895     {
896         strm->Write(s.GetData(), s.GetSize());
897     }
898     else
899     {
900         Dump (strm, true, false);
901         strm->EOL();
902     }
903 }
904 
905 void
906 StackFrame::Dump (Stream *strm, bool show_frame_index, bool show_fullpaths)
907 {
908     if (strm == NULL)
909         return;
910 
911     if (show_frame_index)
912         strm->Printf("frame #%u: ", m_frame_index);
913     strm->Printf("0x%0*llx ", m_thread.GetProcess().GetAddressByteSize() * 2, GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess().GetTarget()));
914     GetSymbolContext(eSymbolContextEverything);
915     const bool show_module = true;
916     const bool show_inline = true;
917     m_sc.DumpStopContext(strm, &m_thread.GetProcess(), GetFrameCodeAddress(), show_fullpaths, show_module, show_inline);
918 }
919 
920 void
921 StackFrame::UpdateCurrentFrameFromPreviousFrame (StackFrame &prev_frame)
922 {
923     assert (GetStackID() == prev_frame.GetStackID());    // TODO: remove this after some testing
924     m_variable_list_sp = prev_frame.m_variable_list_sp;
925     m_variable_list_value_objects.Swap (prev_frame.m_variable_list_value_objects);
926     if (!m_disassembly.GetString().empty())
927         m_disassembly.GetString().swap (m_disassembly.GetString());
928 }
929 
930 
931 void
932 StackFrame::UpdatePreviousFrameFromCurrentFrame (StackFrame &curr_frame)
933 {
934     assert (GetStackID() == curr_frame.GetStackID());        // TODO: remove this after some testing
935     m_id.SetPC (curr_frame.m_id.GetPC());       // Update the Stack ID PC value
936     assert (&m_thread == &curr_frame.m_thread);
937     m_frame_index = curr_frame.m_frame_index;
938     m_concrete_frame_index = curr_frame.m_concrete_frame_index;
939     m_reg_context_sp = curr_frame.m_reg_context_sp;
940     m_frame_code_addr = curr_frame.m_frame_code_addr;
941     assert (m_sc.target_sp.get() == NULL || curr_frame.m_sc.target_sp.get() == NULL || m_sc.target_sp.get() == curr_frame.m_sc.target_sp.get());
942     assert (m_sc.module_sp.get() == NULL || curr_frame.m_sc.module_sp.get() == NULL || m_sc.module_sp.get() == curr_frame.m_sc.module_sp.get());
943     assert (m_sc.comp_unit == NULL || curr_frame.m_sc.comp_unit == NULL || m_sc.comp_unit == curr_frame.m_sc.comp_unit);
944     assert (m_sc.function == NULL || curr_frame.m_sc.function == NULL || m_sc.function == curr_frame.m_sc.function);
945     m_sc = curr_frame.m_sc;
946     m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
947     m_flags.Set (m_sc.GetResolvedMask());
948     m_frame_base.Clear();
949     m_frame_base_error.Clear();
950 }
951 
952 
953 bool
954 StackFrame::HasCachedData () const
955 {
956     if (m_variable_list_sp.get())
957         return true;
958     if (m_variable_list_value_objects.GetSize() > 0)
959         return true;
960     if (!m_disassembly.GetString().empty())
961         return true;
962     return false;
963 }
964 
965 lldb::StackFrameSP
966 StackFrame::GetSP ()
967 {
968     return m_thread.GetStackFrameSPForStackFramePtr (this);
969 }
970