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                                    NULL,
262                                    exe_ctx,
263                                    0,
264                                    0,
265                                    0,
266                                    m_disassembly);
267         if (m_disassembly.GetSize() == 0)
268             return NULL;
269     }
270     return m_disassembly.GetData();
271 }
272 
273 Block *
274 StackFrame::GetFrameBlock ()
275 {
276     if (m_sc.block == NULL && m_flags.IsClear (eSymbolContextBlock))
277         GetSymbolContext (eSymbolContextBlock);
278 
279     if (m_sc.block)
280     {
281         Block *inline_block = m_sc.block->GetContainingInlinedBlock();
282         if (inline_block)
283         {
284             // Use the block with the inlined function info
285             // as the frame block we want this frame to have only the variables
286             // for the inlined function and its non-inlined block child blocks.
287             return inline_block;
288         }
289         else
290         {
291             // This block is not contained withing any inlined function blocks
292             // with so we want to use the top most function block.
293             return &m_sc.function->GetBlock (false);
294         }
295     }
296     return NULL;
297 }
298 
299 //----------------------------------------------------------------------
300 // Get the symbol context if we already haven't done so by resolving the
301 // PC address as much as possible. This way when we pass around a
302 // StackFrame object, everyone will have as much information as
303 // possible and no one will ever have to look things up manually.
304 //----------------------------------------------------------------------
305 const SymbolContext&
306 StackFrame::GetSymbolContext (uint32_t resolve_scope)
307 {
308     // Copy our internal symbol context into "sc".
309     if ((m_flags.Get() & resolve_scope) != resolve_scope)
310     {
311         // Resolve our PC to section offset if we haven't alreday done so
312         // and if we don't have a module. The resolved address section will
313         // contain the module to which it belongs
314         if (!m_sc.module_sp && m_flags.IsClear(RESOLVED_FRAME_CODE_ADDR))
315             GetFrameCodeAddress();
316 
317         // If this is not frame zero, then we need to subtract 1 from the PC
318         // value when doing address lookups since the PC will be on the
319         // instruction following the function call instruction...
320 
321         Address lookup_addr(GetFrameCodeAddress());
322         if (m_frame_index > 0 && lookup_addr.IsValid())
323         {
324             addr_t offset = lookup_addr.GetOffset();
325             if (offset > 0)
326                 lookup_addr.SetOffset(offset - 1);
327         }
328 
329 
330         uint32_t resolved = 0;
331         if (m_sc.module_sp)
332         {
333             // We have something in our stack frame symbol context, lets check
334             // if we haven't already tried to lookup one of those things. If we
335             // haven't then we will do the query.
336 
337             uint32_t actual_resolve_scope = 0;
338 
339             if (resolve_scope & eSymbolContextCompUnit)
340             {
341                 if (m_flags.IsClear (eSymbolContextCompUnit))
342                 {
343                     if (m_sc.comp_unit)
344                         resolved |= eSymbolContextCompUnit;
345                     else
346                         actual_resolve_scope |= eSymbolContextCompUnit;
347                 }
348             }
349 
350             if (resolve_scope & eSymbolContextFunction)
351             {
352                 if (m_flags.IsClear (eSymbolContextFunction))
353                 {
354                     if (m_sc.function)
355                         resolved |= eSymbolContextFunction;
356                     else
357                         actual_resolve_scope |= eSymbolContextFunction;
358                 }
359             }
360 
361             if (resolve_scope & eSymbolContextBlock)
362             {
363                 if (m_flags.IsClear (eSymbolContextBlock))
364                 {
365                     if (m_sc.block)
366                         resolved |= eSymbolContextBlock;
367                     else
368                         actual_resolve_scope |= eSymbolContextBlock;
369                 }
370             }
371 
372             if (resolve_scope & eSymbolContextSymbol)
373             {
374                 if (m_flags.IsClear (eSymbolContextSymbol))
375                 {
376                     if (m_sc.symbol)
377                         resolved |= eSymbolContextSymbol;
378                     else
379                         actual_resolve_scope |= eSymbolContextSymbol;
380                 }
381             }
382 
383             if (resolve_scope & eSymbolContextLineEntry)
384             {
385                 if (m_flags.IsClear (eSymbolContextLineEntry))
386                 {
387                     if (m_sc.line_entry.IsValid())
388                         resolved |= eSymbolContextLineEntry;
389                     else
390                         actual_resolve_scope |= eSymbolContextLineEntry;
391                 }
392             }
393 
394             if (actual_resolve_scope)
395             {
396                 // We might be resolving less information than what is already
397                 // in our current symbol context so resolve into a temporary
398                 // symbol context "sc" so we don't clear out data we have
399                 // already found in "m_sc"
400                 SymbolContext sc;
401                 // Set flags that indicate what we have tried to resolve
402                 resolved |= m_sc.module_sp->ResolveSymbolContextForAddress (lookup_addr, actual_resolve_scope, sc);
403                 // Only replace what we didn't already have as we may have
404                 // information for an inlined function scope that won't match
405                 // what a standard lookup by address would match
406                 if ((resolved & eSymbolContextCompUnit)  && m_sc.comp_unit == NULL)
407                     m_sc.comp_unit = sc.comp_unit;
408                 if ((resolved & eSymbolContextFunction)  && m_sc.function == NULL)
409                     m_sc.function = sc.function;
410                 if ((resolved & eSymbolContextBlock)     && m_sc.block == NULL)
411                     m_sc.block = sc.block;
412                 if ((resolved & eSymbolContextSymbol)    && m_sc.symbol == NULL)
413                     m_sc.symbol = sc.symbol;
414                 if ((resolved & eSymbolContextLineEntry) && !m_sc.line_entry.IsValid())
415                     m_sc.line_entry = sc.line_entry;
416 
417             }
418         }
419         else
420         {
421             // If we don't have a module, then we can't have the compile unit,
422             // function, block, line entry or symbol, so we can safely call
423             // ResolveSymbolContextForAddress with our symbol context member m_sc.
424             resolved |= m_thread.GetProcess().GetTarget().GetImages().ResolveSymbolContextForAddress (lookup_addr, resolve_scope, m_sc);
425         }
426 
427         // If the target was requested add that:
428         if (m_sc.target_sp.get() == NULL)
429         {
430             m_sc.target_sp = CalculateProcess()->GetTarget().GetSP();
431             if (m_sc.target_sp)
432                 resolved |= eSymbolContextTarget;
433         }
434 
435         // Update our internal flags so we remember what we have tried to locate so
436         // we don't have to keep trying when more calls to this function are made.
437         // We might have dug up more information that was requested (for example
438         // if we were asked to only get the block, we will have gotten the
439         // compile unit, and function) so set any additional bits that we resolved
440         m_flags.Set (resolve_scope | resolved);
441     }
442 
443     // Return the symbol context with everything that was possible to resolve
444     // resolved.
445     return m_sc;
446 }
447 
448 
449 VariableList *
450 StackFrame::GetVariableList (bool get_file_globals)
451 {
452     if (m_flags.IsClear(RESOLVED_VARIABLES))
453     {
454         m_flags.Set(RESOLVED_VARIABLES);
455 
456         Block *frame_block = GetFrameBlock();
457 
458         if (frame_block)
459         {
460             const bool get_child_variables = true;
461             const bool can_create = true;
462             const bool stop_if_child_block_is_inlined_function = true;
463             m_variable_list_sp.reset(new VariableList());
464             frame_block->AppendBlockVariables(can_create, get_child_variables, stop_if_child_block_is_inlined_function, m_variable_list_sp.get());
465         }
466     }
467 
468     if (m_flags.IsClear(RESOLVED_GLOBAL_VARIABLES) &&
469         get_file_globals)
470     {
471         m_flags.Set(RESOLVED_GLOBAL_VARIABLES);
472 
473         if (m_flags.IsClear (eSymbolContextCompUnit))
474             GetSymbolContext (eSymbolContextCompUnit);
475 
476         if (m_sc.comp_unit)
477         {
478             VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
479             if (m_variable_list_sp)
480                 m_variable_list_sp->AddVariables (global_variable_list_sp.get());
481             else
482                 m_variable_list_sp = global_variable_list_sp;
483         }
484     }
485 
486     return m_variable_list_sp.get();
487 }
488 
489 ValueObjectSP
490 StackFrame::GetValueForVariableExpressionPath (const char *var_expr_cstr,
491                                                lldb::DynamicValueType use_dynamic,
492                                                uint32_t options,
493                                                lldb::VariableSP &var_sp,
494                                                Error &error)
495 {
496 
497     if (var_expr_cstr && var_expr_cstr[0])
498     {
499         const bool check_ptr_vs_member = (options & eExpressionPathOptionCheckPtrVsMember) != 0;
500         const bool no_fragile_ivar = (options & eExpressionPathOptionsNoFragileObjcIvar) != 0;
501         error.Clear();
502         bool deref = false;
503         bool address_of = false;
504         ValueObjectSP valobj_sp;
505         const bool get_file_globals = true;
506         VariableList *variable_list = GetVariableList (get_file_globals);
507 
508         if (variable_list)
509         {
510             // If first character is a '*', then show pointer contents
511             const char *var_expr = var_expr_cstr;
512             if (var_expr[0] == '*')
513             {
514                 deref = true;
515                 var_expr++; // Skip the '*'
516             }
517             else if (var_expr[0] == '&')
518             {
519                 address_of = true;
520                 var_expr++; // Skip the '&'
521             }
522 
523             std::string var_path (var_expr);
524             size_t separator_idx = var_path.find_first_of(".-[=+~|&^%#@!/?,<>{}");
525             StreamString var_expr_path_strm;
526 
527             ConstString name_const_string;
528             if (separator_idx == std::string::npos)
529                 name_const_string.SetCString (var_path.c_str());
530             else
531                 name_const_string.SetCStringWithLength (var_path.c_str(), separator_idx);
532 
533             var_sp = variable_list->FindVariable(name_const_string);
534             if (var_sp)
535             {
536                 valobj_sp = GetValueObjectForFrameVariable (var_sp, use_dynamic);
537                 if (!valobj_sp)
538                     return valobj_sp;
539 
540                 var_path.erase (0, name_const_string.GetLength ());
541                 // We are dumping at least one child
542                 while (separator_idx != std::string::npos)
543                 {
544                     // Calculate the next separator index ahead of time
545                     ValueObjectSP child_valobj_sp;
546                     const char separator_type = var_path[0];
547                     switch (separator_type)
548                     {
549 
550                     case '-':
551                         if (var_path.size() >= 2 && var_path[1] != '>')
552                             return ValueObjectSP();
553 
554                         if (no_fragile_ivar)
555                         {
556                             // Make sure we aren't trying to deref an objective
557                             // C ivar if this is not allowed
558                             const uint32_t pointer_type_flags = ClangASTContext::GetTypeInfo (valobj_sp->GetClangType(), NULL, NULL);
559                             if ((pointer_type_flags & ClangASTContext::eTypeIsObjC) &&
560                                 (pointer_type_flags & ClangASTContext::eTypeIsPointer))
561                             {
562                                 // This was an objective C object pointer and
563                                 // it was requested we skip any fragile ivars
564                                 // so return nothing here
565                                 return ValueObjectSP();
566                             }
567                         }
568                         var_path.erase (0, 1); // Remove the '-'
569                         // Fall through
570                     case '.':
571                         {
572                             const bool expr_is_ptr = var_path[0] == '>';
573 
574                             var_path.erase (0, 1); // Remove the '.' or '>'
575                             separator_idx = var_path.find_first_of(".-[");
576                             ConstString child_name;
577                             if (separator_idx == std::string::npos)
578                                 child_name.SetCString (var_path.c_str());
579                             else
580                                 child_name.SetCStringWithLength(var_path.c_str(), separator_idx);
581 
582                             if (check_ptr_vs_member)
583                             {
584                                 // We either have a pointer type and need to verify
585                                 // valobj_sp is a pointer, or we have a member of a
586                                 // class/union/struct being accessed with the . syntax
587                                 // and need to verify we don't have a pointer.
588                                 const bool actual_is_ptr = valobj_sp->IsPointerType ();
589 
590                                 if (actual_is_ptr != expr_is_ptr)
591                                 {
592                                     // Incorrect use of "." with a pointer, or "->" with
593                                     // a class/union/struct instance or reference.
594                                     valobj_sp->GetExpressionPath (var_expr_path_strm, false);
595                                     if (actual_is_ptr)
596                                         error.SetErrorStringWithFormat ("\"%s\" is a pointer and . was used to attempt to access \"%s\". Did you mean \"%s->%s\"?",
597                                                                         var_expr_path_strm.GetString().c_str(),
598                                                                         child_name.GetCString(),
599                                                                         var_expr_path_strm.GetString().c_str(),
600                                                                         var_path.c_str());
601                                     else
602                                         error.SetErrorStringWithFormat ("\"%s\" is not a pointer and -> was used to attempt to access \"%s\". Did you mean \"%s.%s\"?",
603                                                                         var_expr_path_strm.GetString().c_str(),
604                                                                         child_name.GetCString(),
605                                                                         var_expr_path_strm.GetString().c_str(),
606                                                                         var_path.c_str());
607                                     return ValueObjectSP();
608                                 }
609                             }
610                             child_valobj_sp = valobj_sp->GetChildMemberWithName (child_name, true);
611                             if (!child_valobj_sp)
612                             {
613                                 // No child member with name "child_name"
614                                 valobj_sp->GetExpressionPath (var_expr_path_strm, false);
615                                 if (child_name)
616                                 {
617                                     error.SetErrorStringWithFormat ("\"%s\" is not a member of \"(%s) %s\"",
618                                                                     child_name.GetCString(),
619                                                                     valobj_sp->GetTypeName().AsCString("<invalid type>"),
620                                                                     var_expr_path_strm.GetString().c_str());
621                                 }
622                                 else
623                                 {
624                                     error.SetErrorStringWithFormat ("incomplete expression path after \"%s\" in \"%s\"",
625                                                                     var_expr_path_strm.GetString().c_str(),
626                                                                     var_expr_cstr);
627                                 }
628 
629                                 return ValueObjectSP();
630                             }
631                             // Remove the child name from the path
632                             var_path.erase(0, child_name.GetLength());
633                             if (use_dynamic != lldb::eNoDynamicValues)
634                             {
635                                 ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
636                                 if (dynamic_value_sp)
637                                     child_valobj_sp = dynamic_value_sp;
638                             }
639                         }
640                         break;
641 
642                     case '[':
643                         // Array member access, or treating pointer as an array
644                         if (var_path.size() > 2) // Need at least two brackets and a number
645                         {
646                             char *end = NULL;
647                             long child_index = ::strtol (&var_path[1], &end, 0);
648                             if (end && *end == ']'
649                                 && *(end-1) != '[') // this code forces an error in the case of arr[]. as bitfield[] is not a good syntax we're good to go
650                             {
651                                 if (ClangASTContext::IsPointerToScalarType(valobj_sp->GetClangType()) && deref)
652                                 {
653                                     // what we have is *ptr[low]. the most similar C++ syntax is to deref ptr
654                                     // and extract bit low out of it. reading array item low
655                                     // would be done by saying ptr[low], without a deref * sign
656                                     Error error;
657                                     ValueObjectSP temp(valobj_sp->Dereference(error));
658                                     if (error.Fail())
659                                     {
660                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
661                                         error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
662                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
663                                                                         var_expr_path_strm.GetString().c_str());
664                                         return ValueObjectSP();
665                                     }
666                                     valobj_sp = temp;
667                                     deref = false;
668                                 }
669                                 else if (ClangASTContext::IsArrayOfScalarType(valobj_sp->GetClangType()) && deref)
670                                 {
671                                     // what we have is *arr[low]. the most similar C++ syntax is to get arr[0]
672                                     // (an operation that is equivalent to deref-ing arr)
673                                     // and extract bit low out of it. reading array item low
674                                     // would be done by saying arr[low], without a deref * sign
675                                     Error error;
676                                     ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
677                                     if (error.Fail())
678                                     {
679                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
680                                         error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
681                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
682                                                                         var_expr_path_strm.GetString().c_str());
683                                         return ValueObjectSP();
684                                     }
685                                     valobj_sp = temp;
686                                     deref = false;
687                                 }
688 
689                                 if (valobj_sp->IsPointerType ())
690                                 {
691                                     child_valobj_sp = valobj_sp->GetSyntheticArrayMemberFromPointer (child_index, true);
692                                     if (!child_valobj_sp)
693                                     {
694                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
695                                         error.SetErrorStringWithFormat ("failed to use pointer as array for index %i for \"(%s) %s\"",
696                                                                         child_index,
697                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
698                                                                         var_expr_path_strm.GetString().c_str());
699                                     }
700                                 }
701                                 else if (ClangASTContext::IsArrayType (valobj_sp->GetClangType(), NULL, NULL))
702                                 {
703                                     // Pass false to dynamic_value here so we can tell the difference between
704                                     // no dynamic value and no member of this type...
705                                     child_valobj_sp = valobj_sp->GetChildAtIndex (child_index, true);
706                                     if (!child_valobj_sp)
707                                     {
708                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
709                                         error.SetErrorStringWithFormat ("array index %i is not valid for \"(%s) %s\"",
710                                                                         child_index,
711                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
712                                                                         var_expr_path_strm.GetString().c_str());
713                                     }
714                                 }
715                                 else if (ClangASTContext::IsScalarType(valobj_sp->GetClangType()))
716                                 {
717                                     // this is a bitfield asking to display just one bit
718                                     child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, child_index, true);
719                                     if (!child_valobj_sp)
720                                     {
721                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
722                                         error.SetErrorStringWithFormat ("bitfield range %i-%i is not valid for \"(%s) %s\"",
723                                                                         child_index, child_index,
724                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
725                                                                         var_expr_path_strm.GetString().c_str());
726                                     }
727                                 }
728                                 else
729                                 {
730                                     valobj_sp->GetExpressionPath (var_expr_path_strm, false);
731                                     error.SetErrorStringWithFormat ("\"(%s) %s\" is not an array type",
732                                                                     valobj_sp->GetTypeName().AsCString("<invalid type>"),
733                                                                     var_expr_path_strm.GetString().c_str());
734                                 }
735 
736                                 if (!child_valobj_sp)
737                                 {
738                                     // Invalid array index...
739                                     return ValueObjectSP();
740                                 }
741 
742                                 // Erase the array member specification '[%i]' where
743                                 // %i is the array index
744                                 var_path.erase(0, (end - var_path.c_str()) + 1);
745                                 separator_idx = var_path.find_first_of(".-[");
746                                 if (use_dynamic != lldb::eNoDynamicValues)
747                                 {
748                                     ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
749                                     if (dynamic_value_sp)
750                                         child_valobj_sp = dynamic_value_sp;
751                                 }
752                                 // Break out early from the switch since we were
753                                 // able to find the child member
754                                 break;
755                             }
756                             else if(end && *end == '-')
757                             {
758                                 // this is most probably a BitField, let's take a look
759                                 char *real_end = NULL;
760                                 long final_index = ::strtol (end+1, &real_end, 0);
761                                 if(real_end && *real_end == ']')
762                                 {
763                                     // if the format given is [high-low], swap range
764                                     if(child_index > final_index)
765                                     {
766                                         long temp = child_index;
767                                         child_index = final_index;
768                                         final_index = temp;
769                                     }
770 
771                                     if (ClangASTContext::IsPointerToScalarType(valobj_sp->GetClangType()) && deref)
772                                     {
773                                         // what we have is *ptr[low-high]. the most similar C++ syntax is to deref ptr
774                                         // and extract bits low thru high out of it. reading array items low thru high
775                                         // would be done by saying ptr[low-high], without a deref * sign
776                                         Error error;
777                                         ValueObjectSP temp(valobj_sp->Dereference(error));
778                                         if (error.Fail())
779                                         {
780                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
781                                             error.SetErrorStringWithFormat ("could not dereference \"(%s) %s\"",
782                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
783                                                                             var_expr_path_strm.GetString().c_str());
784                                             return ValueObjectSP();
785                                         }
786                                         valobj_sp = temp;
787                                         deref = false;
788                                     }
789                                     else if (ClangASTContext::IsArrayOfScalarType(valobj_sp->GetClangType()) && deref)
790                                     {
791                                         // what we have is *arr[low-high]. the most similar C++ syntax is to get arr[0]
792                                         // (an operation that is equivalent to deref-ing arr)
793                                         // and extract bits low thru high out of it. reading array items low thru high
794                                         // would be done by saying arr[low-high], without a deref * sign
795                                         Error error;
796                                         ValueObjectSP temp(valobj_sp->GetChildAtIndex (0, true));
797                                         if (error.Fail())
798                                         {
799                                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
800                                             error.SetErrorStringWithFormat ("could not get item 0 for \"(%s) %s\"",
801                                                                             valobj_sp->GetTypeName().AsCString("<invalid type>"),
802                                                                             var_expr_path_strm.GetString().c_str());
803                                             return ValueObjectSP();
804                                         }
805                                         valobj_sp = temp;
806                                         deref = false;
807                                     }
808 
809                                     child_valobj_sp = valobj_sp->GetSyntheticBitFieldChild(child_index, final_index, true);
810                                     if (!child_valobj_sp)
811                                     {
812                                         valobj_sp->GetExpressionPath (var_expr_path_strm, false);
813                                         error.SetErrorStringWithFormat ("bitfield range %i-%i is not valid for \"(%s) %s\"",
814                                                                         child_index, final_index,
815                                                                         valobj_sp->GetTypeName().AsCString("<invalid type>"),
816                                                                         var_expr_path_strm.GetString().c_str());
817                                     }
818                                 }
819 
820                                 if (!child_valobj_sp)
821                                 {
822                                     // Invalid bitfield range...
823                                     return ValueObjectSP();
824                                 }
825 
826                                 // Erase the bitfield member specification '[%i-%i]' where
827                                 // %i is the index
828                                 var_path.erase(0, (real_end - var_path.c_str()) + 1);
829                                 separator_idx = var_path.find_first_of(".-[");
830                                 if (use_dynamic != lldb::eNoDynamicValues)
831                                 {
832                                     ValueObjectSP dynamic_value_sp(child_valobj_sp->GetDynamicValue(use_dynamic));
833                                     if (dynamic_value_sp)
834                                         child_valobj_sp = dynamic_value_sp;
835                                 }
836                                 // Break out early from the switch since we were
837                                 // able to find the child member
838                                 break;
839 
840                             }
841                         }
842                         else
843                         {
844                             error.SetErrorStringWithFormat("invalid square bracket encountered after \"%s\" in \"%s\"",
845                                                            var_expr_path_strm.GetString().c_str(),
846                                                            var_path.c_str());
847                         }
848                         return ValueObjectSP();
849 
850                     default:
851                         // Failure...
852                         {
853                             valobj_sp->GetExpressionPath (var_expr_path_strm, false);
854                             error.SetErrorStringWithFormat ("unexpected char '%c' encountered after \"%s\" in \"%s\"",
855                                                             separator_type,
856                                                             var_expr_path_strm.GetString().c_str(),
857                                                             var_path.c_str());
858 
859                             return ValueObjectSP();
860                         }
861                     }
862 
863                     if (child_valobj_sp)
864                         valobj_sp = child_valobj_sp;
865 
866                     if (var_path.empty())
867                         break;
868 
869                 }
870                 if (valobj_sp)
871                 {
872                     if (deref)
873                     {
874                         ValueObjectSP deref_valobj_sp (valobj_sp->Dereference(error));
875                         valobj_sp = deref_valobj_sp;
876                     }
877                     else if (address_of)
878                     {
879                         ValueObjectSP address_of_valobj_sp (valobj_sp->AddressOf(error));
880                         valobj_sp = address_of_valobj_sp;
881                     }
882                 }
883                 return valobj_sp;
884             }
885             else
886             {
887                 error.SetErrorStringWithFormat("no variable named '%s' found in this frame",
888                                                name_const_string.GetCString());
889             }
890         }
891     }
892     else
893     {
894         error.SetErrorStringWithFormat("invalid variable path '%s'", var_expr_cstr);
895     }
896     return ValueObjectSP();
897 }
898 
899 bool
900 StackFrame::GetFrameBaseValue (Scalar &frame_base, Error *error_ptr)
901 {
902     if (m_flags.IsClear(GOT_FRAME_BASE))
903     {
904         if (m_sc.function)
905         {
906             m_frame_base.Clear();
907             m_frame_base_error.Clear();
908 
909             m_flags.Set(GOT_FRAME_BASE);
910             ExecutionContext exe_ctx (&m_thread.GetProcess(), &m_thread, this);
911             Value expr_value;
912             addr_t loclist_base_addr = LLDB_INVALID_ADDRESS;
913             if (m_sc.function->GetFrameBaseExpression().IsLocationList())
914                 loclist_base_addr = m_sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (&m_thread.GetProcess().GetTarget());
915 
916             if (m_sc.function->GetFrameBaseExpression().Evaluate(&exe_ctx, NULL, NULL, NULL, NULL, loclist_base_addr, NULL, expr_value, &m_frame_base_error) == false)
917             {
918                 // We should really have an error if evaluate returns, but in case
919                 // we don't, lets set the error to something at least.
920                 if (m_frame_base_error.Success())
921                     m_frame_base_error.SetErrorString("Evaluation of the frame base expression failed.");
922             }
923             else
924             {
925                 m_frame_base = expr_value.ResolveValue(&exe_ctx, NULL);
926             }
927         }
928         else
929         {
930             m_frame_base_error.SetErrorString ("No function in symbol context.");
931         }
932     }
933 
934     if (m_frame_base_error.Success())
935         frame_base = m_frame_base;
936 
937     if (error_ptr)
938         *error_ptr = m_frame_base_error;
939     return m_frame_base_error.Success();
940 }
941 
942 RegisterContextSP
943 StackFrame::GetRegisterContext ()
944 {
945     if (!m_reg_context_sp)
946         m_reg_context_sp = m_thread.CreateRegisterContextForFrame (this);
947     return m_reg_context_sp;
948 }
949 
950 bool
951 StackFrame::HasDebugInformation ()
952 {
953     GetSymbolContext (eSymbolContextLineEntry);
954     return m_sc.line_entry.IsValid();
955 }
956 
957 
958 ValueObjectSP
959 StackFrame::GetValueObjectForFrameVariable (const VariableSP &variable_sp, lldb::DynamicValueType use_dynamic)
960 {
961     ValueObjectSP valobj_sp;
962     VariableList *var_list = GetVariableList (true);
963     if (var_list)
964     {
965         // Make sure the variable is a frame variable
966         const uint32_t var_idx = var_list->FindIndexForVariable (variable_sp.get());
967         const uint32_t num_variables = var_list->GetSize();
968         if (var_idx < num_variables)
969         {
970             valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex (var_idx);
971             if (valobj_sp.get() == NULL)
972             {
973                 if (m_variable_list_value_objects.GetSize() < num_variables)
974                     m_variable_list_value_objects.Resize(num_variables);
975                 valobj_sp = ValueObjectVariable::Create (this, variable_sp);
976                 m_variable_list_value_objects.SetValueObjectAtIndex (var_idx, valobj_sp);
977             }
978         }
979     }
980     if (use_dynamic != lldb::eNoDynamicValues && valobj_sp)
981     {
982         ValueObjectSP dynamic_sp = valobj_sp->GetDynamicValue (use_dynamic);
983         if (dynamic_sp)
984             return dynamic_sp;
985     }
986     return valobj_sp;
987 }
988 
989 ValueObjectSP
990 StackFrame::TrackGlobalVariable (const VariableSP &variable_sp, lldb::DynamicValueType use_dynamic)
991 {
992     // Check to make sure we aren't already tracking this variable?
993     ValueObjectSP valobj_sp (GetValueObjectForFrameVariable (variable_sp, use_dynamic));
994     if (!valobj_sp)
995     {
996         // We aren't already tracking this global
997         VariableList *var_list = GetVariableList (true);
998         // If this frame has no variables, create a new list
999         if (var_list == NULL)
1000             m_variable_list_sp.reset (new VariableList());
1001 
1002         // Add the global/static variable to this frame
1003         m_variable_list_sp->AddVariable (variable_sp);
1004 
1005         // Now make a value object for it so we can track its changes
1006         valobj_sp = GetValueObjectForFrameVariable (variable_sp, use_dynamic);
1007     }
1008     return valobj_sp;
1009 }
1010 
1011 bool
1012 StackFrame::IsInlined ()
1013 {
1014     if (m_sc.block == NULL)
1015         GetSymbolContext (eSymbolContextBlock);
1016     if (m_sc.block)
1017         return m_sc.block->GetContainingInlinedBlock() != NULL;
1018     return false;
1019 }
1020 
1021 Target *
1022 StackFrame::CalculateTarget ()
1023 {
1024     return m_thread.CalculateTarget();
1025 }
1026 
1027 Process *
1028 StackFrame::CalculateProcess ()
1029 {
1030     return m_thread.CalculateProcess();
1031 }
1032 
1033 Thread *
1034 StackFrame::CalculateThread ()
1035 {
1036     return &m_thread;
1037 }
1038 
1039 StackFrame *
1040 StackFrame::CalculateStackFrame ()
1041 {
1042     return this;
1043 }
1044 
1045 
1046 void
1047 StackFrame::CalculateExecutionContext (ExecutionContext &exe_ctx)
1048 {
1049     m_thread.CalculateExecutionContext (exe_ctx);
1050     exe_ctx.frame = this;
1051 }
1052 
1053 void
1054 StackFrame::DumpUsingSettingsFormat (Stream *strm)
1055 {
1056     if (strm == NULL)
1057         return;
1058 
1059     GetSymbolContext(eSymbolContextEverything);
1060     ExecutionContext exe_ctx;
1061     CalculateExecutionContext(exe_ctx);
1062     const char *end = NULL;
1063     StreamString s;
1064     const char *frame_format = m_thread.GetProcess().GetTarget().GetDebugger().GetFrameFormat();
1065     if (frame_format && Debugger::FormatPrompt (frame_format, &m_sc, &exe_ctx, NULL, s, &end))
1066     {
1067         strm->Write(s.GetData(), s.GetSize());
1068     }
1069     else
1070     {
1071         Dump (strm, true, false);
1072         strm->EOL();
1073     }
1074 }
1075 
1076 void
1077 StackFrame::Dump (Stream *strm, bool show_frame_index, bool show_fullpaths)
1078 {
1079     if (strm == NULL)
1080         return;
1081 
1082     if (show_frame_index)
1083         strm->Printf("frame #%u: ", m_frame_index);
1084     strm->Printf("0x%0*llx ", m_thread.GetProcess().GetTarget().GetArchitecture().GetAddressByteSize() * 2, GetFrameCodeAddress().GetLoadAddress(&m_thread.GetProcess().GetTarget()));
1085     GetSymbolContext(eSymbolContextEverything);
1086     const bool show_module = true;
1087     const bool show_inline = true;
1088     m_sc.DumpStopContext(strm, &m_thread.GetProcess(), GetFrameCodeAddress(), show_fullpaths, show_module, show_inline);
1089 }
1090 
1091 void
1092 StackFrame::UpdateCurrentFrameFromPreviousFrame (StackFrame &prev_frame)
1093 {
1094     assert (GetStackID() == prev_frame.GetStackID());    // TODO: remove this after some testing
1095     m_variable_list_sp = prev_frame.m_variable_list_sp;
1096     m_variable_list_value_objects.Swap (prev_frame.m_variable_list_value_objects);
1097     if (!m_disassembly.GetString().empty())
1098         m_disassembly.GetString().swap (m_disassembly.GetString());
1099 }
1100 
1101 
1102 void
1103 StackFrame::UpdatePreviousFrameFromCurrentFrame (StackFrame &curr_frame)
1104 {
1105     assert (GetStackID() == curr_frame.GetStackID());        // TODO: remove this after some testing
1106     m_id.SetPC (curr_frame.m_id.GetPC());       // Update the Stack ID PC value
1107     assert (&m_thread == &curr_frame.m_thread);
1108     m_frame_index = curr_frame.m_frame_index;
1109     m_concrete_frame_index = curr_frame.m_concrete_frame_index;
1110     m_reg_context_sp = curr_frame.m_reg_context_sp;
1111     m_frame_code_addr = curr_frame.m_frame_code_addr;
1112     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());
1113     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());
1114     assert (m_sc.comp_unit == NULL || curr_frame.m_sc.comp_unit == NULL || m_sc.comp_unit == curr_frame.m_sc.comp_unit);
1115     assert (m_sc.function == NULL || curr_frame.m_sc.function == NULL || m_sc.function == curr_frame.m_sc.function);
1116     m_sc = curr_frame.m_sc;
1117     m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
1118     m_flags.Set (m_sc.GetResolvedMask());
1119     m_frame_base.Clear();
1120     m_frame_base_error.Clear();
1121 }
1122 
1123 
1124 bool
1125 StackFrame::HasCachedData () const
1126 {
1127     if (m_variable_list_sp.get())
1128         return true;
1129     if (m_variable_list_value_objects.GetSize() > 0)
1130         return true;
1131     if (!m_disassembly.GetString().empty())
1132         return true;
1133     return false;
1134 }
1135 
1136 lldb::StackFrameSP
1137 StackFrame::GetSP ()
1138 {
1139     return m_thread.GetStackFrameSPForStackFramePtr (this);
1140 }
1141 
1142 
1143 
1144 bool
1145 StackFrame::GetStatus (Stream& strm,
1146                        bool show_frame_info,
1147                        bool show_source,
1148                        uint32_t source_lines_before,
1149                        uint32_t source_lines_after)
1150 {
1151     if (show_frame_info)
1152     {
1153         strm.Indent();
1154         DumpUsingSettingsFormat (&strm);
1155     }
1156 
1157     if (show_source)
1158     {
1159         GetSymbolContext(eSymbolContextCompUnit | eSymbolContextLineEntry);
1160 
1161         if (m_sc.comp_unit && m_sc.line_entry.IsValid())
1162         {
1163             Target &target = GetThread().GetProcess().GetTarget();
1164             target.GetDebugger().GetSourceManager().DisplaySourceLinesWithLineNumbers (
1165                 &target,
1166                 m_sc.line_entry.file,
1167                 m_sc.line_entry.line,
1168                 3,
1169                 3,
1170                 "->",
1171                 &strm);
1172 
1173         }
1174     }
1175     return true;
1176 }
1177 
1178