add myself to the Write After Approval list.
[deliverable/binutils-gdb.git] / gdb / varobj.c
index 4cf6204bcc3eefc07c43ce0900f28500ee01dd45..16ba528afe67dc2d2534124418932a4cb02adb75 100644 (file)
@@ -23,6 +23,7 @@
 #include "language.h"
 #include "wrapper.h"
 #include "gdbcmd.h"
+#include "gdb_string.h"
 #include <math.h>
 
 #include "varobj.h"
@@ -52,7 +53,7 @@ struct varobj_root
   struct block *valid_block;
 
   /* The frame for this expression */
-  CORE_ADDR frame;
+  struct frame_id frame;
 
   /* If 1, "update" always recomputes the frame & valid block
      using the currently selected frame. */
@@ -426,7 +427,7 @@ varobj_create (char *objname,
 
       block = NULL;
       if (fi != NULL)
-       block = get_frame_block (fi);
+       block = get_frame_block (fi, 0);
 
       p = expression;
       innermost_block = NULL;
@@ -456,9 +457,9 @@ varobj_create (char *objname,
          Since select_frame is so benign, just call it for all cases. */
       if (fi != NULL)
        {
-         var->root->frame = FRAME_FP (fi);
+         get_frame_id (fi, &var->root->frame);
          old_fi = selected_frame;
-         select_frame (fi, -1);
+         select_frame (fi);
        }
 
       /* We definitively need to catch errors here.
@@ -485,7 +486,7 @@ varobj_create (char *objname,
 
       /* Reset the selected frame */
       if (fi != NULL)
-       select_frame (old_fi, -1);
+       select_frame (old_fi);
     }
 
   /* If the variable object name is null, that means this
@@ -514,13 +515,13 @@ char *
 varobj_gen_name (void)
 {
   static int id = 0;
-  char obj_name[31];
+  char *obj_name;
 
   /* generate a name for this object */
   id++;
-  sprintf (obj_name, "var%d", id);
+  xasprintf (&obj_name, "var%d", id);
 
-  return xstrdup (obj_name);
+  return obj_name;
 }
 
 /* Given an "objname", returns the pointer to the corresponding varobj
@@ -761,11 +762,10 @@ varobj_set_value (struct varobj *var, char *expression)
   struct value *value;
   int saved_input_radix = input_radix;
 
-  if (variable_editable (var) && !var->error)
+  if (var->value != NULL && variable_editable (var) && !var->error)
     {
       char *s = expression;
       int i;
-      struct value *temp;
 
       input_radix = 10;                /* ALWAYS reset to decimal temporarily */
       if (!gdb_parse_exp_1 (&s, 0, 0, &exp))
@@ -778,36 +778,8 @@ varobj_set_value (struct varobj *var, char *expression)
          return 0;
        }
 
-      /* If our parent is "public", "private", or "protected", we could
-         be asking to modify the value of a baseclass. If so, we need to
-         adjust our address by the offset of our baseclass in the subclass,
-         since VALUE_ADDRESS (var->value) points at the start of the subclass.
-         For some reason, value_cast doesn't take care of this properly. */
-      temp = var->value;
-      if (var->parent != NULL && CPLUS_FAKE_CHILD (var->parent))
-       {
-         struct varobj *super, *sub;
-         struct type *type;
-         super = var->parent->parent;
-         sub = super->parent;
-         if (sub != NULL)
-           {
-             /* Yes, it is a baseclass */
-             type = get_type_deref (sub);
-
-             if (super->index < TYPE_N_BASECLASSES (type))
-               {
-                 temp = value_copy (var->value);
-                 for (i = 0; i < super->index; i++)
-                   offset += TYPE_LENGTH (TYPE_FIELD_TYPE (type, i));
-               }
-           }
-       }
-
-      VALUE_ADDRESS (temp) += offset;
-      if (!gdb_value_assign (temp, value, &val))
+      if (!gdb_value_assign (var->value, value, &val))
        return 0;
-      VALUE_ADDRESS (val) -= offset;
       value_free (var->value);
       release_value (val);
       var->value = val;
@@ -879,7 +851,8 @@ varobj_update (struct varobj **varp, struct varobj ***changelist)
   struct value *new;
   struct vstack *stack = NULL;
   struct vstack *result = NULL;
-  struct frame_info *old_fi;
+  struct frame_id old_fid;
+  struct frame_info *fi;
 
   /* sanity check: have we been passed a pointer? */
   if (changelist == NULL)
@@ -892,7 +865,7 @@ varobj_update (struct varobj **varp, struct varobj ***changelist)
 
   /* Save the selected stack frame, since we will need to change it
      in order to evaluate expressions. */
-  old_fi = selected_frame;
+  get_frame_id (selected_frame, &old_fid);
 
   /* Update the root variable. value_of_root can return NULL
      if the variable is no longer around, i.e. we stepped out of
@@ -1012,7 +985,9 @@ varobj_update (struct varobj **varp, struct varobj ***changelist)
     }
 
   /* Restore selected frame */
-  select_frame (old_fi, -1);
+  fi = frame_find_by_id (old_fid);
+  if (fi)
+    select_frame (fi);
 
   if (type_changed)
     return -2;
@@ -1239,14 +1214,11 @@ create_child (struct varobj *parent, int index, char *name)
   child->name = name;
   child->index = index;
   child->value = value_of_child (parent, index);
-  if (child->value == NULL || parent->error)
+  if ((!CPLUS_FAKE_CHILD (child) && child->value == NULL) || parent->error)
     child->error = 1;
   child->parent = parent;
   child->root = parent->root;
-  childs_name =
-    (char *) xmalloc ((strlen (parent->obj_name) + strlen (name) + 2) *
-                     sizeof (char));
-  sprintf (childs_name, "%s.%s", parent->obj_name, name);
+  xasprintf (&childs_name, "%s.%s", parent->obj_name, name);
   child->obj_name = childs_name;
   install_variable (child);
 
@@ -1335,7 +1307,8 @@ new_root_variable (void)
   var->root->lang = NULL;
   var->root->exp = NULL;
   var->root->valid_block = NULL;
-  var->root->frame = (CORE_ADDR) -1;
+  var->root->frame.base = 0;
+  var->root->frame.pc = 0;
   var->root->use_selected_frame = 0;
   var->root->rootvar = NULL;
 
@@ -1372,7 +1345,10 @@ make_cleanup_free_variable (struct varobj *var)
 
 /* This returns the type of the variable. This skips past typedefs
    and returns the real type of the variable. It also dereferences
-   pointers and references. */
+   pointers and references.
+
+   NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
+   except within get_target_type and get_type. */
 static struct type *
 get_type (struct varobj *var)
 {
@@ -1401,7 +1377,10 @@ get_type_deref (struct varobj *var)
 }
 
 /* This returns the target type (or NULL) of TYPE, also skipping
-   past typedefs, just like get_type (). */
+   past typedefs, just like get_type ().
+
+   NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
+   except within get_target_type and get_type. */
 static struct type *
 get_target_type (struct type *type)
 {
@@ -1672,7 +1651,13 @@ value_of_child (struct varobj *parent, int index)
 
   /* If we're being lazy, fetch the real value of the variable. */
   if (value != NULL && VALUE_LAZY (value))
-    gdb_value_fetch_lazy (value);
+    {
+      /* If we fail to fetch the value of the child, return
+         NULL so that callers notice that we're leaving an
+         error message. */
+      if (!gdb_value_fetch_lazy (value))
+       value = NULL;
+    }
 
   return value;
 }
@@ -1817,14 +1802,7 @@ c_name_of_child (struct varobj *parent, int index)
   switch (TYPE_CODE (type))
     {
     case TYPE_CODE_ARRAY:
-      {
-       /* We never get here unless parent->num_children is greater than 0... */
-       int len = 1;
-       while ((int) pow ((double) 10, (double) len) < index)
-         len++;
-       name = (char *) xmalloc (1 + len * sizeof (char));
-       sprintf (name, "%d", index);
-      }
+      xasprintf (&name, "%d", index);
       break;
 
     case TYPE_CODE_STRUCT:
@@ -1843,9 +1821,7 @@ c_name_of_child (struct varobj *parent, int index)
          break;
 
        default:
-         name =
-           (char *) xmalloc ((strlen (parent->name) + 2) * sizeof (char));
-         sprintf (name, "*%s", parent->name);
+         xasprintf (&name, "*%s", parent->name);
          break;
        }
       break;
@@ -1878,14 +1854,11 @@ c_value_of_root (struct varobj **var_handle)
   else
     {
       reinit_frame_cache ();
-
-
-      fi = find_frame_addr_in_frame_chain (var->root->frame);
-
+      fi = frame_find_by_id (var->root->frame);
       within_scope = fi != NULL;
       /* FIXME: select_frame could fail */
       if (within_scope)
-       select_frame (fi, -1);
+       select_frame (fi);
     }
 
   if (within_scope)
@@ -1952,7 +1925,8 @@ c_value_of_child (struct varobj *parent, int index)
 
        case TYPE_CODE_STRUCT:
        case TYPE_CODE_UNION:
-         gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL, "vstructure");
+         gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
+                               "vstructure");
          break;
 
        case TYPE_CODE_PTR:
@@ -1960,7 +1934,8 @@ c_value_of_child (struct varobj *parent, int index)
            {
            case TYPE_CODE_STRUCT:
            case TYPE_CODE_UNION:
-             gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL, "vstructure");
+             gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
+                                   "vstructure");
              break;
 
            default:
@@ -1990,7 +1965,7 @@ c_type_of_child (struct varobj *parent, int index)
   switch (TYPE_CODE (parent->type))
     {
     case TYPE_CODE_ARRAY:
-      type = TYPE_TARGET_TYPE (parent->type);
+      type = get_target_type (parent->type);
       break;
 
     case TYPE_CODE_STRUCT:
@@ -1999,7 +1974,7 @@ c_type_of_child (struct varobj *parent, int index)
       break;
 
     case TYPE_CODE_PTR:
-      switch (TYPE_CODE (TYPE_TARGET_TYPE (parent->type)))
+      switch (TYPE_CODE (get_target_type (parent->type)))
        {
        case TYPE_CODE_STRUCT:
        case TYPE_CODE_UNION:
@@ -2007,7 +1982,7 @@ c_type_of_child (struct varobj *parent, int index)
          break;
 
        default:
-         type = TYPE_TARGET_TYPE (parent->type);
+         type = get_target_type (parent->type);
          break;
        }
       break;
@@ -2047,22 +2022,10 @@ c_variable_editable (struct varobj *var)
 static char *
 c_value_of_variable (struct varobj *var)
 {
-  struct type *type;
-  struct value *val;
-
-  if (var->value != NULL)
-    val = var->value;
-  else
-    {
-      /* This can happen if we attempt to get the value of a struct
-         member when the parent is an invalid pointer. */
-      return xstrdup ("???");
-    }
-
   /* BOGUS: if val_print sees a struct/class, it will print out its
      children instead of "{...}" */
-  type = get_type (var);
-  switch (TYPE_CODE (type))
+
+  switch (TYPE_CODE (get_type (var)))
     {
     case TYPE_CODE_STRUCT:
     case TYPE_CODE_UNION:
@@ -2071,29 +2034,39 @@ c_value_of_variable (struct varobj *var)
 
     case TYPE_CODE_ARRAY:
       {
-       char number[18];
-       sprintf (number, "[%d]", var->num_children);
-       return xstrdup (number);
+       char *number;
+       xasprintf (&number, "[%d]", var->num_children);
+       return (number);
       }
       /* break; */
 
     default:
       {
-       long dummy;
-       struct ui_file *stb = mem_fileopen ();
-       struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
-       char *thevalue;
-
-       if (VALUE_LAZY (val))
-         gdb_value_fetch_lazy (val);
-       val_print (VALUE_TYPE (val), VALUE_CONTENTS_RAW (val), 0,
-                  VALUE_ADDRESS (val),
-                  stb, format_code[(int) var->format], 1, 0, 0);
-       thevalue = ui_file_xstrdup (stb, &dummy);
-       do_cleanups (old_chain);
+       if (var->value == NULL)
+         {
+           /* This can happen if we attempt to get the value of a struct
+              member when the parent is an invalid pointer. This is an
+              error condition, so we should tell the caller. */
+           return NULL;
+         }
+       else
+         {
+           long dummy;
+           struct ui_file *stb = mem_fileopen ();
+           struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
+           char *thevalue;
+
+           if (VALUE_LAZY (var->value))
+             gdb_value_fetch_lazy (var->value);
+           val_print (VALUE_TYPE (var->value),
+                      VALUE_CONTENTS_RAW (var->value), 0,
+                      VALUE_ADDRESS (var->value), stb,
+                      format_code[(int) var->format], 1, 0, 0);
+           thevalue = ui_file_xstrdup (stb, &dummy);
+           do_cleanups (old_chain);
        return thevalue;
       }
-      /* break; */
+      }
     }
 }
 \f
@@ -2212,9 +2185,16 @@ cplus_name_of_child (struct varobj *parent, int index)
 
       if (CPLUS_FAKE_CHILD (parent))
        {
+         int i;
+
+         /* Skip over vptr, if it exists. */
+         if (TYPE_VPTR_BASETYPE (type) == type
+             && index >= TYPE_VPTR_FIELDNO (type))
+           index++;
+
          /* FIXME: This assumes that type orders
             inherited, public, private, protected */
-         int i = index + TYPE_N_BASECLASSES (type);
+         i = index + TYPE_N_BASECLASSES (type);
          if (STREQ (parent->name, "private")
              || STREQ (parent->name, "protected"))
            i += children[v_public];
@@ -2299,6 +2279,9 @@ cplus_value_of_child (struct varobj *parent, int index)
          char *name;
          struct value *temp = parent->parent->value;
 
+         if (temp == NULL)
+           return NULL;
+
          name = name_of_child (parent, index);
          gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
                                "cplus_structure");
@@ -2317,7 +2300,7 @@ cplus_value_of_child (struct varobj *parent, int index)
          /* Baseclass */
          if (parent->value != NULL)
            {
-             struct value *temp;
+             struct value *temp = NULL;
 
              if (TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_PTR
                  || TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_REF)
@@ -2328,8 +2311,17 @@ cplus_value_of_child (struct varobj *parent, int index)
              else
                temp = parent->value;
 
-             value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
-             release_value (value);
+             if (temp != NULL)
+               {
+                 value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
+                 release_value (value);
+               }
+             else
+               {
+                 /* We failed to evaluate the parent's value, so don't even
+                    bother trying to evaluate this child. */
+                 return NULL;
+               }
            }
        }
     }
@@ -2345,21 +2337,31 @@ cplus_type_of_child (struct varobj *parent, int index)
 {
   struct type *type, *t;
 
-  t = get_type_deref (parent);
+  if (CPLUS_FAKE_CHILD (parent))
+    {
+      /* Looking for the type of a child of public, private, or protected. */
+      t = get_type_deref (parent->parent);
+    }
+  else
+    t = get_type_deref (parent);
+
   type = NULL;
   switch (TYPE_CODE (t))
     {
     case TYPE_CODE_STRUCT:
     case TYPE_CODE_UNION:
-      if (index >= TYPE_N_BASECLASSES (t))
+      if (CPLUS_FAKE_CHILD (parent))
        {
-         /* special */
-         return NULL;
+         char *name = cplus_name_of_child (parent, index);
+         type = lookup_struct_elt_type (t, name, 0);
+         xfree (name);
        }
+      else if (index < TYPE_N_BASECLASSES (t))
+       type = TYPE_FIELD_TYPE (t, index);
       else
        {
-         /* Baseclass */
-         type = TYPE_FIELD_TYPE (t, index);
+         /* special */
+         return NULL;
        }
       break;
 
This page took 0.035427 seconds and 4 git commands to generate.