StackFrame objects now own ValueObjects for any frame variables (locals, args,
function statics, file globals and static variables) that a frame contains. 
The StackFrame objects can give out ValueObjects instances for
each variable which allows us to track when a variable changes and doesn't
depend on variable names when getting value objects.

StackFrame::GetVariableList now takes a boolean to indicate if we want to
get the frame compile unit globals and static variables.

The value objects in the stack frames can now correctly track when they have
been modified. There are a few more tweaks needed to complete this work. The
biggest issue is when stepping creates partial stacks (just frame zero usually)
and causes previous stack frames not to match up with the current stack frames
because the previous frames only has frame zero. We don't really want to 
require that all previous frames be complete since stepping often must check
stack frames to complete their jobs. I will fix this issue tomorrow.



git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@112800 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/API/SBFrame.cpp b/source/API/SBFrame.cpp
index b2d591d..ce294ca 100644
--- a/source/API/SBFrame.cpp
+++ b/source/API/SBFrame.cpp
@@ -304,7 +304,7 @@
     if (m_opaque_sp)
     {
         size_t i;
-        VariableList *variable_list = m_opaque_sp->GetVariableList();
+        VariableList *variable_list = m_opaque_sp->GetVariableList(true);
         if (variable_list)
         {
             const size_t num_variables = variable_list->GetSize();
@@ -339,38 +339,12 @@
                             if (in_scope_only && !variable_sp->IsInScope(m_opaque_sp.get()))
                                 continue;
 
-                            value_list.Append(ValueObjectSP (new ValueObjectVariable (variable_sp)));
+                            value_list.Append(m_opaque_sp->GetValueObjectForFrameVariable (variable_sp));
                         }
                     }
                 }
             }
-        }
-        
-        if (statics)
-        {
-            CompileUnit *frame_comp_unit = m_opaque_sp->GetSymbolContext (eSymbolContextCompUnit).comp_unit;
-            
-            if (frame_comp_unit)
-            {
-                variable_list = frame_comp_unit->GetVariableList(true).get();
-                
-                if (variable_list)
-                {
-                    const size_t num_variables = variable_list->GetSize();
-                    if (num_variables)
-                    {
-                        for (i = 0; i < num_variables; ++i)
-                        {
-                            VariableSP variable_sp (variable_list->GetVariableAtIndex(i));
-                            if (variable_sp)
-                            {
-                                value_list.Append(ValueObjectSP (new ValueObjectVariable (variable_sp)));
-                            }
-                        }
-                    }
-                }
-            }
-        }
+        }        
     }
     return value_list;
 }
diff --git a/source/API/SBValue.cpp b/source/API/SBValue.cpp
index c7839cb..f92be9f 100644
--- a/source/API/SBValue.cpp
+++ b/source/API/SBValue.cpp
@@ -172,15 +172,15 @@
 {
     const char *value_string = NULL;
     if ( m_opaque_sp)
-        value_string = m_opaque_sp->GetValueAsCString(frame.get());
+        value_string = m_opaque_sp->GetValueAsCString (frame.get());
     return value_string;
 }
 
 bool
-SBValue::GetValueDidChange ()
+SBValue::GetValueDidChange (const SBFrame &frame)
 {
     if (IsValid())
-        return m_opaque_sp->GetValueDidChange();
+        return m_opaque_sp->GetValueDidChange (frame.get());
     return false;
 }
 
diff --git a/source/Commands/CommandObjectFrame.cpp b/source/Commands/CommandObjectFrame.cpp
index f78f8cb..30287e2 100644
--- a/source/Commands/CommandObjectFrame.cpp
+++ b/source/Commands/CommandObjectFrame.cpp
@@ -584,13 +584,12 @@
                                 var_sp = global_var_list.GetVariableAtIndex(global_idx);
                                 if (var_sp)
                                 {
-                                    valobj_sp = exe_ctx.frame->GetValueObjectList().FindValueObjectByValueName (m_options.globals[idx].AsCString());
+                                    valobj_sp = exe_ctx.frame->GetValueObjectForFrameVariable (var_sp);
                                     if (!valobj_sp)
-                                        valobj_sp.reset (new ValueObjectVariable (var_sp));
+                                        valobj_sp = exe_ctx.frame->TrackGlobalVariable (var_sp);
 
                                     if (valobj_sp)
                                     {
-                                        exe_ctx.frame->GetValueObjectList().Append (valobj_sp);
                                         DumpValueObject (result, exe_ctx.frame, valobj_sp.get(), name_cstr, m_options.ptr_depth, 0, m_options.max_depth, false);
                                         result.GetOutputStream().EOL();
                                     }
@@ -631,18 +630,7 @@
                     var_sp = variable_list.FindVariable(name_const_string);
                     if (var_sp)
                     {
-                        //DumpVariable (result, &exe_ctx, var_sp.get());
-                        // TODO: redo history variables using a different map
-//                        if (var_path[0] == '$')
-//                            valobj_sp = valobj_list.FindValueObjectByValueObjectName (name_const_string.GetCString());
-//                        else
-                            valobj_sp = exe_ctx.frame->GetValueObjectList().FindValueObjectByValueName (name_const_string.GetCString());
-
-                        if (!valobj_sp)
-                        {
-                            valobj_sp.reset (new ValueObjectVariable (var_sp));
-                            exe_ctx.frame->GetValueObjectList().Append (valobj_sp);
-                        }
+                        valobj_sp = exe_ctx.frame->GetValueObjectForFrameVariable (var_sp);
 
                         var_path.erase (0, name_const_string.GetLength ());
                         // We are dumping at least one child
diff --git a/source/Core/ValueObject.cpp b/source/Core/ValueObject.cpp
index 9c70c3b..e14875b 100644
--- a/source/Core/ValueObject.cpp
+++ b/source/Core/ValueObject.cpp
@@ -44,12 +44,16 @@
     m_data (),
     m_value (),
     m_error (),
-    m_flags (),
-    m_value_str(),
-    m_location_str(),
-    m_summary_str(),
-    m_children(),
-    m_synthetic_children()
+    m_value_str (),
+    m_old_value_str (),
+    m_location_str (),
+    m_summary_str (),
+    m_children (),
+    m_synthetic_children (),
+    m_value_is_valid (false),
+    m_value_did_change (false),
+    m_children_count_valid (false),
+    m_old_value_valid (false)
 {
 }
 
@@ -77,10 +81,19 @@
             const user_id_t stop_id = process->GetStopID();
             if (m_update_id != stop_id)
             {
+                bool first_update = m_update_id == 0;
                 // Save the old value using swap to avoid a string copy which
                 // also will clear our m_value_str
-                std::string old_value_str;
-                old_value_str.swap (m_value_str);
+                if (m_value_str.empty())
+                {
+                    m_old_value_valid = false;
+                }
+                else
+                {
+                    m_old_value_valid = true;
+                    m_old_value_str.swap (m_value_str);
+                    m_value_str.clear();
+                }
                 m_location_str.clear();
                 m_summary_str.clear();
 
@@ -97,22 +110,14 @@
                 m_update_id = stop_id;
                 bool success = m_error.Success();
                 SetValueIsValid (success);
-                // If the variable hasn't already been marked as changed do it
-                // by comparing the old any new value
-                if (!GetValueDidChange())
+                
+                if (first_update)
+                    SetValueDidChange (false);
+                else if (!m_value_did_change && success == false)
                 {
-                    if (success)
-                    {
-                        // The value was gotten successfully, so we consider the
-                        // value as changed if the value string differs
-                        SetValueDidChange (old_value_str != m_value_str);
-                    }
-                    else
-                    {
-                        // The value wasn't gotten successfully, so we mark this
-                        // as changed if the value used to be valid and now isn't
-                        SetValueDidChange (value_was_valid);
-                    }
+                    // The value wasn't gotten successfully, so we mark this
+                    // as changed if the value used to be valid and now isn't
+                    SetValueDidChange (value_was_valid);
                 }
             }
         }
@@ -202,31 +207,29 @@
 }
 
 bool
-ValueObject::GetValueIsValid ()
+ValueObject::GetValueIsValid () const
 {
-    return m_flags.IsSet(eValueIsValid);
+    return m_value_is_valid;
 }
 
 
 void
 ValueObject::SetValueIsValid (bool b)
 {
-    if (b)
-        m_flags.Set(eValueIsValid);
-    else
-        m_flags.Clear(eValueIsValid);
+    m_value_is_valid = b;
 }
 
 bool
-ValueObject::GetValueDidChange () const
+ValueObject::GetValueDidChange (ExecutionContextScope *exe_scope)
 {
-    return m_flags.IsSet(eValueChanged);
+    GetValueAsCString (exe_scope);
+    return m_value_did_change;
 }
 
 void
 ValueObject::SetValueDidChange (bool value_changed)
 {
-    m_flags.Set(eValueChanged);
+    m_value_did_change = value_changed;
 }
 
 ValueObjectSP
@@ -301,7 +304,7 @@
 uint32_t
 ValueObject::GetNumChildren ()
 {
-    if (m_flags.IsClear(eNumChildrenHasBeenSet))
+    if (!m_children_count_valid)
     {
         SetNumChildren (CalculateNumChildren());
     }
@@ -310,7 +313,7 @@
 void
 ValueObject::SetNumChildren (uint32_t num_children)
 {
-    m_flags.Set(eNumChildrenHasBeenSet);
+    m_children_count_valid = true;
     m_children.resize(num_children);
 }
 
@@ -552,6 +555,13 @@
                     break;
                 }
             }
+            
+            if (!m_value_did_change && m_old_value_valid)
+            {
+                // The value was gotten successfully, so we consider the
+                // value as changed if the value string differs
+                SetValueDidChange (m_old_value_str != m_value_str);
+            }
         }
     }
     if (m_value_str.empty())
diff --git a/source/Core/ValueObjectList.cpp b/source/Core/ValueObjectList.cpp
index 5feeae7..8913d4d 100644
--- a/source/Core/ValueObjectList.cpp
+++ b/source/Core/ValueObjectList.cpp
@@ -57,6 +57,12 @@
     return m_value_objects.size();
 }
 
+void
+ValueObjectList::Resize (uint32_t size)
+{
+    m_value_objects.resize (size);
+}
+
 lldb::ValueObjectSP
 ValueObjectList::GetValueObjectAtIndex (uint32_t idx)
 {
@@ -66,6 +72,14 @@
     return valobj_sp;
 }
 
+void
+ValueObjectList::SetValueObjectAtIndex (uint32_t idx, const ValueObjectSP &valobj_sp)
+{
+    if (idx >= m_value_objects.size())
+        m_value_objects.resize (idx + 1);
+    m_value_objects[idx] = valobj_sp;
+}
+
 ValueObjectSP
 ValueObjectList::FindValueObjectByValueName (const char *name)
 {
@@ -74,7 +88,8 @@
     collection::iterator pos, end = m_value_objects.end();
     for (pos = m_value_objects.begin(); pos != end; ++pos)
     {
-        if ((*pos)->GetName() == name_const_str)
+        ValueObject *valobj = (*pos).get();
+        if (valobj && valobj->GetName() == name_const_str)
         {
             val_obj_sp = *pos;
             break;
@@ -91,7 +106,10 @@
 
     for (pos = m_value_objects.begin(); pos != end; ++pos)
     {
-        if ((*pos)->GetID() == uid)
+        // Watch out for NULL objects in our list as the list
+        // might get resized to a specific size and lazily filled in
+        ValueObject *valobj = (*pos).get();
+        if (valobj && valobj->GetID() == uid)
         {
             valobj_sp = *pos;
             break;
@@ -102,14 +120,15 @@
 
 
 ValueObjectSP
-ValueObjectList::FindValueObjectByPointer (ValueObject *valobj)
+ValueObjectList::FindValueObjectByPointer (ValueObject *find_valobj)
 {
     ValueObjectSP valobj_sp;
     collection::iterator pos, end = m_value_objects.end();
 
     for (pos = m_value_objects.begin(); pos != end; ++pos)
     {
-        if ((*pos).get() == valobj)
+        ValueObject *valobj = (*pos).get();
+        if (valobj && valobj == find_valobj)
         {
             valobj_sp = *pos;
             break;
diff --git a/source/Core/ValueObjectVariable.cpp b/source/Core/ValueObjectVariable.cpp
index c15bbef..cbd583f 100644
--- a/source/Core/ValueObjectVariable.cpp
+++ b/source/Core/ValueObjectVariable.cpp
@@ -32,7 +32,7 @@
 
 using namespace lldb_private;
 
-ValueObjectVariable::ValueObjectVariable (lldb::VariableSP &var_sp) :
+ValueObjectVariable::ValueObjectVariable (const lldb::VariableSP &var_sp) :
     ValueObject(),
     m_variable_sp(var_sp)
 {
diff --git a/source/Symbol/VariableList.cpp b/source/Symbol/VariableList.cpp
index 7f864f2..5ee5299 100644
--- a/source/Symbol/VariableList.cpp
+++ b/source/Symbol/VariableList.cpp
@@ -32,7 +32,7 @@
 
 
 void
-VariableList::AddVariable(VariableSP &variable_sp)
+VariableList::AddVariable(const VariableSP &variable_sp)
 {
     m_variables.push_back(variable_sp);
 }
@@ -82,6 +82,20 @@
     return var_sp;
 }
 
+uint32_t
+VariableList::FindIndexForVariable (Variable* variable)
+{
+    VariableSP var_sp;
+    iterator pos;
+    const iterator begin = m_variables.begin();
+    const iterator end = m_variables.end();
+    for (pos = m_variables.begin(); pos != end; ++pos)
+    {
+        if ((*pos).get() == variable)
+            return std::distance (begin, pos);
+    }
+    return UINT32_MAX;
+}
 
 size_t
 VariableList::MemorySize() const
diff --git a/source/Target/StackFrame.cpp b/source/Target/StackFrame.cpp
index 4aef251..f812285 100644
--- a/source/Target/StackFrame.cpp
+++ b/source/Target/StackFrame.cpp
@@ -16,7 +16,9 @@
 #include "lldb/Core/Module.h"
 #include "lldb/Core/Disassembler.h"
 #include "lldb/Core/Value.h"
+#include "lldb/Core/ValueObjectVariable.h"
 #include "lldb/Symbol/Function.h"
+#include "lldb/Symbol/VariableList.h"
 #include "lldb/Target/ExecutionContext.h"
 #include "lldb/Target/Process.h"
 #include "lldb/Target/RegisterContext.h"
@@ -55,7 +57,7 @@
     m_frame_base (),
     m_frame_base_error (),
     m_variable_list_sp (),
-    m_value_object_list ()
+    m_variable_list_value_objects ()
 {
     if (sc_ptr != NULL)
     {
@@ -85,7 +87,7 @@
     m_frame_base (),
     m_frame_base_error (),
     m_variable_list_sp (),
-    m_value_object_list ()
+    m_variable_list_value_objects ()
 {
     if (sc_ptr != NULL)
     {
@@ -121,7 +123,7 @@
     m_frame_base (),
     m_frame_base_error (),
     m_variable_list_sp (),
-    m_value_object_list ()
+    m_variable_list_value_objects ()
 {
     if (sc_ptr != NULL)
     {
@@ -450,18 +452,31 @@
 
 
 VariableList *
-StackFrame::GetVariableList ()
+StackFrame::GetVariableList (bool get_file_globals)
 {
     if (m_flags.IsClear(RESOLVED_VARIABLES))
     {
         m_flags.Set(RESOLVED_VARIABLES);
 
-        if (GetSymbolContext (eSymbolContextFunction).function)
+        GetSymbolContext (eSymbolContextCompUnit | 
+                          eSymbolContextFunction | 
+                          eSymbolContextBlock);
+
+        if (m_sc.block)
         {
             bool get_child_variables = true;
             bool can_create = true;
             m_variable_list_sp = m_sc.function->GetBlock (can_create).GetVariableList (get_child_variables, can_create);
         }
+        
+        if (get_file_globals && m_sc.comp_unit)
+        {
+            VariableListSP global_variable_list_sp (m_sc.comp_unit->GetVariableList(true));
+            if (m_variable_list_sp)
+                m_variable_list_sp->AddVariables (global_variable_list_sp.get());
+            else
+                m_variable_list_sp = global_variable_list_sp;
+        }
     }
     return m_variable_list_sp.get();
 }
@@ -521,10 +536,52 @@
     return m_sc.line_entry.IsValid();
 }
 
-ValueObjectList &
-StackFrame::GetValueObjectList()
+
+ValueObjectSP
+StackFrame::GetValueObjectForFrameVariable (const VariableSP &variable_sp)
 {
-    return m_value_object_list;
+    ValueObjectSP valobj_sp;
+    VariableList *var_list = GetVariableList (true);
+    if (var_list)
+    {
+        // Make sure the variable is a frame variable
+        const uint32_t var_idx = var_list->FindIndexForVariable (variable_sp.get());
+        const uint32_t num_variables = var_list->GetSize();
+        if (var_idx < num_variables)
+        {
+            valobj_sp = m_variable_list_value_objects.GetValueObjectAtIndex (var_idx);
+            if (valobj_sp.get() == NULL)
+            {
+                if (m_variable_list_value_objects.GetSize() < num_variables)
+                    m_variable_list_value_objects.Resize(num_variables);
+                valobj_sp.reset (new ValueObjectVariable (variable_sp));
+                m_variable_list_value_objects.SetValueObjectAtIndex (var_idx, valobj_sp);
+            }
+        }
+    }
+    return valobj_sp;
+}
+
+ValueObjectSP
+StackFrame::TrackGlobalVariable (const VariableSP &variable_sp)
+{
+    // Check to make sure we aren't already tracking this variable?
+    ValueObjectSP valobj_sp (GetValueObjectForFrameVariable (variable_sp));
+    if (!valobj_sp)
+    {
+        // We aren't already tracking this global
+        VariableList *var_list = GetVariableList (true);
+        // If this frame has no variables, create a new list
+        if (var_list == NULL)
+            m_variable_list_sp.reset (new VariableList());
+
+        // Add the global/static variable to this frame
+        m_variable_list_sp->AddVariable (variable_sp);
+
+        // Now make a value object for it so we can track its changes
+        valobj_sp = GetValueObjectForFrameVariable (variable_sp);
+    }
+    return valobj_sp;
 }
 
 bool
@@ -591,7 +648,7 @@
 {
     assert (GetStackID() == prev_frame.GetStackID());    // TODO: remove this after some testing
     m_variable_list_sp = prev_frame.m_variable_list_sp;
-    m_value_object_list.Swap (prev_frame.m_value_object_list);
+    m_variable_list_value_objects.Swap (prev_frame.m_variable_list_value_objects);
     if (!m_disassembly.GetString().empty())
         m_disassembly.GetString().swap (m_disassembly.GetString());
 }
@@ -610,7 +667,6 @@
     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());
     assert (m_sc.comp_unit == NULL || curr_frame.m_sc.comp_unit == NULL || m_sc.comp_unit == curr_frame.m_sc.comp_unit);
     assert (m_sc.function == NULL || curr_frame.m_sc.function == NULL || m_sc.function == curr_frame.m_sc.function);
-    assert (m_sc.symbol == NULL || curr_frame.m_sc.symbol == NULL || m_sc.symbol == curr_frame.m_sc.symbol);
     m_sc = curr_frame.m_sc;
     m_flags.Clear(GOT_FRAME_BASE | eSymbolContextEverything);
     m_flags.Set (m_sc.GetResolvedMask());