* elf-eh-frame.c (_bfd_elf_discard_section_eh_frame): Drop
[deliverable/binutils-gdb.git] / gdb / varobj.c
index c5df3254cfa8861f08199bbe27f38be7b1c342ac..3a0bf5e373dfbc06d7c64d2f6fa072f255cbc280 100644 (file)
@@ -31,6 +31,8 @@
 
 #include "varobj.h"
 #include "vec.h"
+#include "gdbthread.h"
+#include "inferior.h"
 
 /* Non-zero if we want to see trace of varobj level stuff.  */
 
@@ -66,9 +68,17 @@ struct varobj_root
      not NULL.  */
   struct frame_id frame;
 
-  /* If 1, "update" always recomputes the frame & valid block
-     using the currently selected frame. */
-  int use_selected_frame;
+  /* The thread ID that this varobj_root belong to.  This field
+     is only valid if valid_block is not NULL.  
+     When not 0, indicates which thread 'frame' belongs to.
+     When 0, indicates that the thread list was empty when the varobj_root
+     was created.  */
+  int thread_id;
+
+  /* If 1, the -var-update always recomputes the value in the
+     current thread and frame.  Otherwise, variable object is
+     always updated in the specific scope/thread/frame  */
+  int floating;
 
   /* Flag that indicates validity: set to 0 when this varobj_root refers 
      to symbols that do not exist anymore.  */
@@ -218,7 +228,8 @@ static struct value *value_of_root (struct varobj **var_handle, int *);
 
 static struct value *value_of_child (struct varobj *parent, int index);
 
-static char *my_value_of_variable (struct varobj *var);
+static char *my_value_of_variable (struct varobj *var,
+                                  enum varobj_display_formats format);
 
 static char *value_get_print_value (struct value *value,
                                    enum varobj_display_formats format);
@@ -243,7 +254,8 @@ static struct value *c_value_of_child (struct varobj *parent, int index);
 
 static struct type *c_type_of_child (struct varobj *parent, int index);
 
-static char *c_value_of_variable (struct varobj *var);
+static char *c_value_of_variable (struct varobj *var,
+                                 enum varobj_display_formats format);
 
 /* C++ implementation */
 
@@ -263,7 +275,8 @@ static struct value *cplus_value_of_child (struct varobj *parent, int index);
 
 static struct type *cplus_type_of_child (struct varobj *parent, int index);
 
-static char *cplus_value_of_variable (struct varobj *var);
+static char *cplus_value_of_variable (struct varobj *var,
+                                     enum varobj_display_formats format);
 
 /* Java implementation */
 
@@ -281,7 +294,8 @@ static struct value *java_value_of_child (struct varobj *parent, int index);
 
 static struct type *java_type_of_child (struct varobj *parent, int index);
 
-static char *java_value_of_variable (struct varobj *var);
+static char *java_value_of_variable (struct varobj *var,
+                                    enum varobj_display_formats format);
 
 /* The language specific vector */
 
@@ -314,7 +328,8 @@ struct language_specific
   struct type *(*type_of_child) (struct varobj * parent, int index);
 
   /* The current value of VAR. */
-  char *(*value_of_variable) (struct varobj * var);
+  char *(*value_of_variable) (struct varobj * var,
+                             enum varobj_display_formats format);
 };
 
 /* Array of known source language routines. */
@@ -463,7 +478,7 @@ varobj_create (char *objname,
 
       /* frame = -2 means always use selected frame */
       if (type == USE_SELECTED_FRAME)
-       var->root->use_selected_frame = 1;
+       var->root->floating = 1;
 
       block = NULL;
       if (fi != NULL)
@@ -501,8 +516,9 @@ varobj_create (char *objname,
       if (innermost_block && fi != NULL)
        {
          var->root->frame = get_frame_id (fi);
+         var->root->thread_id = pid_to_thread_id (inferior_ptid);
          old_fi = get_selected_frame (NULL);
-         select_frame (fi);
+         select_frame (fi);     
        }
 
       /* We definitely need to catch errors here.
@@ -690,6 +706,19 @@ varobj_get_display_format (struct varobj *var)
   return var->format;
 }
 
+/* If the variable object is bound to a specific thread, that
+   is its evaluation can always be done in context of a frame
+   inside that thread, returns GDB id of the thread -- which
+   is always positive.  Otherwise, returns -1. */
+int
+varobj_get_thread_id (struct varobj *var)
+{
+  if (var->root->valid_block && var->root->thread_id > 0)
+    return var->root->thread_id;
+  else
+    return -1;
+}
+
 void
 varobj_set_frozen (struct varobj *var, int frozen)
 {
@@ -833,10 +862,17 @@ varobj_get_attributes (struct varobj *var)
   return attributes;
 }
 
+char *
+varobj_get_formatted_value (struct varobj *var,
+                           enum varobj_display_formats format)
+{
+  return my_value_of_variable (var, format);
+}
+
 char *
 varobj_get_value (struct varobj *var)
 {
-  return my_value_of_variable (var);
+  return my_value_of_variable (var, var->format);
 }
 
 /* Set the value of an object variable (if it is editable) to the
@@ -1084,9 +1120,6 @@ install_new_value (struct varobj *var, struct value *value, int initial)
    expression to see if it's changed.  Then go all the way
    through its children, reconstructing them and noting if they've
    changed.
-   Return value: 
-    < 0 for error values, see varobj.h.
-    Otherwise it is the number of children + parent changed.
 
    The EXPLICIT parameter specifies if this call is result
    of MI request to update this specific variable, or 
@@ -1097,9 +1130,7 @@ install_new_value (struct varobj *var, struct value *value, int initial)
    returns TYPE_CHANGED, then it has done this and VARP will be modified
    to point to the new varobj.  */
 
-int
-varobj_update (struct varobj **varp, struct varobj ***changelist,
-              int explicit)
+VEC(varobj_update_result) *varobj_update (struct varobj **varp, int explicit)
 {
   int changed = 0;
   int type_changed = 0;
@@ -1110,63 +1141,50 @@ varobj_update (struct varobj **varp, struct varobj ***changelist,
   struct varobj **templist = NULL;
   struct value *new;
   VEC (varobj_p) *stack = NULL;
-  VEC (varobj_p) *result = NULL;
-  struct frame_id old_fid;
+  VEC (varobj_update_result) *result = NULL;
   struct frame_info *fi;
 
-  /* sanity check: have we been passed a pointer?  */
-  gdb_assert (changelist);
-
   /* Frozen means frozen -- we don't check for any change in
      this varobj, including its going out of scope, or
      changing type.  One use case for frozen varobjs is
      retaining previously evaluated expressions, and we don't
      want them to be reevaluated at all.  */
   if (!explicit && (*varp)->frozen)
-    return 0;
+    return result;
 
   if (!(*varp)->root->is_valid)
-    return INVALID;
+    {
+      varobj_update_result r = {*varp};
+      r.status = VAROBJ_INVALID;
+      VEC_safe_push (varobj_update_result, result, &r);
+      return result;
+    }
 
   if ((*varp)->root->rootvar == *varp)
     {
-      /* Save the selected stack frame, since we will need to change it
-        in order to evaluate expressions.  */
-      old_fid = get_frame_id (deprecated_safe_get_selected_frame ());
-      
+      varobj_update_result r = {*varp};
+      r.status = VAROBJ_IN_SCOPE;
+
       /* Update the root variable. value_of_root can return NULL
         if the variable is no longer around, i.e. we stepped out of
         the frame in which a local existed. We are letting the 
         value_of_root variable dispose of the varobj if the type
         has changed.  */
-      type_changed = 1;
       new = value_of_root (varp, &type_changed);
+      r.varobj = *varp;
 
-      /* Restore selected frame.  */
-      fi = frame_find_by_id (old_fid);
-      if (fi)
-       select_frame (fi);
-      
-      /* If this is a "use_selected_frame" varobj, and its type has changed,
-        them note that it's changed.  */
-      if (type_changed)
-       VEC_safe_push (varobj_p, result, *varp);
+      r.type_changed = type_changed;
+      if (install_new_value ((*varp), new, type_changed))
+       r.changed = 1;
       
-        if (install_new_value ((*varp), new, type_changed))
-         {
-           /* If type_changed is 1, install_new_value will never return
-              non-zero, so we'll never report the same variable twice.  */
-           gdb_assert (!type_changed);
-           VEC_safe_push (varobj_p, result, *varp);
-         }
-
       if (new == NULL)
-       {
-         /* This means the varobj itself is out of scope.
-            Report it.  */
-         VEC_free (varobj_p, result);
-         return NOT_IN_SCOPE;
-       }
+       r.status = VAROBJ_NOT_IN_SCOPE;
+
+      if (r.type_changed || r.changed)
+       VEC_safe_push (varobj_update_result, result, &r);
+
+      if (r.status == VAROBJ_NOT_IN_SCOPE)
+       return result;
     }
 
   VEC_safe_push (varobj_p, stack, *varp);
@@ -1196,32 +1214,16 @@ varobj_update (struct varobj **varp, struct varobj ***changelist,
          if (install_new_value (v, new, 0 /* type not changed */))
            {
              /* Note that it's changed */
-             VEC_safe_push (varobj_p, result, v);
+             varobj_update_result r = {v};
+             r.changed = 1;
+             VEC_safe_push (varobj_update_result, result, &r);
              v->updated = 0;
            }
        }
     }
 
-  /* Alloc (changed + 1) list entries.  */
-  changed = VEC_length (varobj_p, result);
-  *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
-  cv = *changelist;
-
-  for (i = 0; i < changed; ++i)
-    {
-      *cv = VEC_index (varobj_p, result, i);
-      gdb_assert (*cv != NULL);
-      ++cv;
-    }
-  *cv = 0;
-
   VEC_free (varobj_p, stack);
-  VEC_free (varobj_p, result);
-
-  if (type_changed)
-    return TYPE_CHANGED;
-  else
-    return changed;
+  return result;
 }
 \f
 
@@ -1491,7 +1493,7 @@ new_root_variable (void)
   var->root->exp = NULL;
   var->root->valid_block = NULL;
   var->root->frame = null_frame_id;
-  var->root->use_selected_frame = 0;
+  var->root->floating = 0;
   var->root->rootvar = NULL;
   var->root->is_valid = 1;
 
@@ -1686,16 +1688,15 @@ name_of_child (struct varobj *var, int index)
   return (*var->root->lang->name_of_child) (var, index);
 }
 
-/* What is the ``struct value *'' of the root variable VAR? 
-   TYPE_CHANGED controls what to do if the type of a
-   use_selected_frame = 1 variable changes.  On input,
-   TYPE_CHANGED = 1 means discard the old varobj, and replace
-   it with this one.  TYPE_CHANGED = 0 means leave it around.
-   NB: In both cases, var_handle will point to the new varobj,
-   so if you use TYPE_CHANGED = 0, you will have to stash the
-   old varobj pointer away somewhere before calling this.
-   On return, TYPE_CHANGED will be 1 if the type has changed, and 
-   0 otherwise. */
+/* What is the ``struct value *'' of the root variable VAR?
+   For floating variable object, evaluation can get us a value
+   of different type from what is stored in varobj already.  In
+   that case:
+   - *type_changed will be set to 1
+   - old varobj will be freed, and new one will be
+   created, with the same name.
+   - *var_handle will be set to the new varobj 
+   Otherwise, *type_changed will be set to 0.  */
 static struct value *
 value_of_root (struct varobj **var_handle, int *type_changed)
 {
@@ -1712,7 +1713,7 @@ value_of_root (struct varobj **var_handle, int *type_changed)
   if (!is_root_p (var))
     return NULL;
 
-  if (var->root->use_selected_frame)
+  if (var->root->floating)
     {
       struct varobj *tmp_var;
       char *old_type, *new_type;
@@ -1727,21 +1728,25 @@ value_of_root (struct varobj **var_handle, int *type_changed)
       new_type = varobj_get_type (tmp_var);
       if (strcmp (old_type, new_type) == 0)
        {
+         /* The expression presently stored inside var->root->exp
+            remembers the locations of local variables relatively to
+            the frame where the expression was created (in DWARF location
+            button, for example).  Naturally, those locations are not
+            correct in other frames, so update the expression.  */
+
+         struct expression *tmp_exp = var->root->exp;
+         var->root->exp = tmp_var->root->exp;
+         tmp_var->root->exp = tmp_exp;
+
          varobj_delete (tmp_var, NULL, 0);
          *type_changed = 0;
        }
       else
        {
-         if (*type_changed)
-           {
-             tmp_var->obj_name =
-               savestring (var->obj_name, strlen (var->obj_name));
-             varobj_delete (var, NULL, 0);
-           }
-         else
-           {
-             tmp_var->obj_name = varobj_gen_name ();
-           }
+         tmp_var->obj_name =
+           savestring (var->obj_name, strlen (var->obj_name));
+         varobj_delete (var, NULL, 0);
+
          install_variable (tmp_var);
          *var_handle = tmp_var;
          var = *var_handle;
@@ -1771,10 +1776,10 @@ value_of_child (struct varobj *parent, int index)
 
 /* GDB already has a command called "value_of_variable". Sigh. */
 static char *
-my_value_of_variable (struct varobj *var)
+my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
 {
   if (var->root->is_valid)
-    return (*var->root->lang->value_of_variable) (var);
+    return (*var->root->lang->value_of_variable) (var, format);
   else
     return NULL;
 }
@@ -1793,7 +1798,8 @@ value_get_print_value (struct value *value, enum varobj_display_formats format)
   stb = mem_fileopen ();
   old_chain = make_cleanup_ui_file_delete (stb);
 
-  common_val_print (value, stb, format_code[(int) format], 1, 0, 0);
+  common_val_print (value, stb, format_code[(int) format], 1, 0, 0,
+                   current_language);
   thevalue = ui_file_xstrdup (stb, &dummy);
 
   do_cleanups (old_chain);
@@ -1862,6 +1868,15 @@ varobj_value_is_changeable_p (struct varobj *var)
   return r;
 }
 
+/* Return 1 if that varobj is floating, that is is always evaluated in the
+   selected frame, and not bound to thread/frame.  Such variable objects
+   are created using '@' as frame specifier to -var-create.  */
+int
+varobj_floating_p (struct varobj *var)
+{
+  return var->root->floating;
+}
+
 /* Given the value and the type of a variable object,
    adjust the value and type to those necessary
    for getting children of the variable object.
@@ -2146,37 +2161,64 @@ c_path_expr_of_child (struct varobj *child)
   return child->path_expr;
 }
 
+/* If frame associated with VAR can be found, switch
+   to it and return 1.  Otherwise, return 0.  */
+static int
+check_scope (struct varobj *var)
+{
+  struct frame_info *fi;
+  int scope;
+
+  fi = frame_find_by_id (var->root->frame);
+  scope = fi != NULL;
+
+  if (fi)
+    {
+      CORE_ADDR pc = get_frame_pc (fi);
+      if (pc <  BLOCK_START (var->root->valid_block) ||
+         pc >= BLOCK_END (var->root->valid_block))
+       scope = 0;
+      else
+       select_frame (fi);
+    }
+  return scope;
+}
+
 static struct value *
 c_value_of_root (struct varobj **var_handle)
 {
   struct value *new_val = NULL;
   struct varobj *var = *var_handle;
   struct frame_info *fi;
-  int within_scope;
-
+  int within_scope = 0;
+  struct cleanup *back_to;
+                                                                
   /*  Only root variables can be updated... */
   if (!is_root_p (var))
     /* Not a root var */
     return NULL;
 
+  back_to = make_cleanup_restore_current_thread ();
 
   /* Determine whether the variable is still around. */
-  if (var->root->valid_block == NULL || var->root->use_selected_frame)
+  if (var->root->valid_block == NULL || var->root->floating)
     within_scope = 1;
+  else if (var->root->thread_id == 0)
+    {
+      /* The program was single-threaded when the variable object was
+        created.  Technically, it's possible that the program became
+        multi-threaded since then, but we don't support such
+        scenario yet.  */
+      within_scope = check_scope (var);          
+    }
   else
     {
-      fi = frame_find_by_id (var->root->frame);
-      within_scope = fi != NULL;
-      /* FIXME: select_frame could fail */
-      if (fi)
+      ptid_t ptid = thread_id_to_pid (var->root->thread_id);
+      if (in_thread_list (ptid))
        {
-         CORE_ADDR pc = get_frame_pc (fi);
-         if (pc <  BLOCK_START (var->root->valid_block) ||
-             pc >= BLOCK_END (var->root->valid_block))
-           within_scope = 0;
-         else
-           select_frame (fi);
-       }         
+         switch_to_thread (ptid);
+         within_scope = check_scope (var);
+       }
     }
 
   if (within_scope)
@@ -2187,6 +2229,8 @@ c_value_of_root (struct varobj **var_handle)
       return new_val;
     }
 
+  do_cleanups (back_to);
+
   return NULL;
 }
 
@@ -2208,7 +2252,7 @@ c_type_of_child (struct varobj *parent, int index)
 }
 
 static char *
-c_value_of_variable (struct varobj *var)
+c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
 {
   /* BOGUS: if val_print sees a struct/class, or a reference to one,
      it will print out its children instead of "{...}".  So we need to
@@ -2253,7 +2297,13 @@ c_value_of_variable (struct varobj *var)
 
            gdb_assert (varobj_value_is_changeable_p (var));
            gdb_assert (!value_lazy (var->value));
-           return xstrdup (var->print_value);
+           
+           /* If the specified format is the current one,
+              we can reuse print_value */
+           if (format == var->format)
+             return xstrdup (var->print_value);
+           else
+             return value_get_print_value (var->value, format);
          }
       }
     }
@@ -2579,7 +2629,7 @@ cplus_type_of_child (struct varobj *parent, int index)
 }
 
 static char *
-cplus_value_of_variable (struct varobj *var)
+cplus_value_of_variable (struct varobj *var, enum varobj_display_formats format)
 {
 
   /* If we have one of our special types, don't print out
@@ -2587,7 +2637,7 @@ cplus_value_of_variable (struct varobj *var)
   if (CPLUS_FAKE_CHILD (var))
     return xstrdup ("");
 
-  return c_value_of_variable (var);
+  return c_value_of_variable (var, format);
 }
 \f
 /* Java */
@@ -2662,9 +2712,9 @@ java_type_of_child (struct varobj *parent, int index)
 }
 
 static char *
-java_value_of_variable (struct varobj *var)
+java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
 {
-  return cplus_value_of_variable (var);
+  return cplus_value_of_variable (var, format);
 }
 \f
 extern void _initialize_varobj (void);
@@ -2700,6 +2750,11 @@ varobj_invalidate (void)
     varp = all_rootvarobj;
     while (*varp != NULL)
       {
+       /* Floating varobjs are reparsed on each stop, so we don't care if
+          the presently parsed expression refers to something that's gone.  */
+       if ((*varp)->root->floating)
+         continue;
+
         /* global var must be re-evaluated.  */     
         if ((*varp)->root->valid_block == NULL)
         {
This page took 0.030146 seconds and 4 git commands to generate.