2005-02-06 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / varobj.c
index a0e1d53f95c57caff82eba575e32b75559b2bf8d..818a31bda7b6576b19b416c1483a728f3c88f23d 100644 (file)
@@ -1,5 +1,6 @@
 /* Implementation of the GDB variable objects API.
-   Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
+
+   Copyright 1999, 2000, 2001, 2005 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -396,6 +397,27 @@ static struct vlist **varobj_table;
 
 /* Creates a varobj (not its children) */
 
+/* Return the full FRAME which corresponds to the given CORE_ADDR
+   or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
+
+static struct frame_info *
+find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
+{
+  struct frame_info *frame = NULL;
+
+  if (frame_addr == (CORE_ADDR) 0)
+    return NULL;
+
+  while (1)
+    {
+      frame = get_prev_frame (frame);
+      if (frame == NULL)
+       return NULL;
+      if (get_frame_base_address (frame) == frame_addr)
+       return frame;
+    }
+}
+
 struct varobj *
 varobj_create (char *objname,
               char *expression, CORE_ADDR frame, enum varobj_type type)
@@ -420,8 +442,14 @@ varobj_create (char *objname,
 
       /* Allow creator to specify context of variable */
       if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
-       fi = selected_frame;
+       fi = deprecated_selected_frame;
       else
+       /* FIXME: cagney/2002-11-23: This code should be doing a
+          lookup using the frame ID and not just the frame's
+          ``address''.  This, of course, means an interface change.
+          However, with out that interface change ISAs, such as the
+          ia64 with its two stacks, won't work.  Similar goes for the
+          case where there is a frameless function.  */
        fi = find_frame_addr_in_frame_chain (frame);
 
       /* frame = -2 means always use selected frame */
@@ -445,8 +473,8 @@ varobj_create (char *objname,
       if (var->root->exp->elts[0].opcode == OP_TYPE)
        {
          do_cleanups (old_chain);
-         fprintf_unfiltered (gdb_stderr,
-                             "Attempt to use a type name as an expression.");
+         fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
+                             " as an expression.\n");
          return NULL;
        }
 
@@ -460,8 +488,8 @@ varobj_create (char *objname,
          Since select_frame is so benign, just call it for all cases. */
       if (fi != NULL)
        {
-         get_frame_id (fi, &var->root->frame);
-         old_fi = selected_frame;
+         var->root->frame = get_frame_id (fi);
+         old_fi = deprecated_selected_frame;
          select_frame (fi);
        }
 
@@ -472,13 +500,13 @@ varobj_create (char *objname,
        {
          /* no error */
          release_value (var->value);
-         if (VALUE_LAZY (var->value))
+         if (value_lazy (var->value))
            gdb_value_fetch_lazy (var->value);
        }
       else
        var->value = evaluate_type (var->root->exp);
 
-      var->type = VALUE_TYPE (var->value);
+      var->type = value_type (var->value);
 
       /* Set language info */
       lang = variable_language (var);
@@ -522,7 +550,7 @@ varobj_gen_name (void)
 
   /* generate a name for this object */
   id++;
-  xasprintf (&obj_name, "var%d", id);
+  obj_name = xstrprintf ("var%d", id);
 
   return obj_name;
 }
@@ -717,7 +745,7 @@ varobj_get_type (struct varobj *var)
   /* To print the type, we simply create a zero ``struct value *'' and
      cast it to our type. We then typeprint this variable. */
   val = value_zero (var->type, not_lval);
-  type_print (VALUE_TYPE (val), "", stb, -1);
+  type_print (value_type (val), "", stb, -1);
 
   thetype = ui_file_xstrdup (stb, &length);
   do_cleanups (old_chain);
@@ -871,7 +899,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. */
-  get_frame_id (selected_frame, &old_fid);
+  old_fid = get_frame_id (deprecated_selected_frame);
 
   /* Update the root variable. value_of_root can return NULL
      if the variable is no longer around, i.e. we stepped out of
@@ -1203,7 +1231,7 @@ child_exists (struct varobj *var, char *name)
 
   for (vc = var->children; vc != NULL; vc = vc->next)
     {
-      if (STREQ (vc->child->name, name))
+      if (strcmp (vc->child->name, name) == 0)
        return vc->child;
     }
 
@@ -1227,7 +1255,7 @@ create_child (struct varobj *parent, int index, char *name)
     child->error = 1;
   child->parent = parent;
   child->root = parent->root;
-  xasprintf (&childs_name, "%s.%s", parent->obj_name, name);
+  childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
   child->obj_name = childs_name;
   install_variable (child);
 
@@ -1317,8 +1345,7 @@ new_root_variable (void)
   var->root->lang = NULL;
   var->root->exp = NULL;
   var->root->valid_block = NULL;
-  var->root->frame.base = 0;
-  var->root->frame.pc = 0;
+  var->root->frame = null_frame_id;
   var->root->use_selected_frame = 0;
   var->root->rootvar = NULL;
 
@@ -1353,9 +1380,8 @@ make_cleanup_free_variable (struct varobj *var)
   return make_cleanup (do_free_variable_cleanup, 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.
+/* This returns the type of the variable. It also skips past typedefs
+   to return the real type of the variable.
 
    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
    except within get_target_type and get_type. */
@@ -1365,8 +1391,8 @@ get_type (struct varobj *var)
   struct type *type;
   type = var->type;
 
-  while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
-    type = TYPE_TARGET_TYPE (type);
+  if (type != NULL)
+    type = check_typedef (type);
 
   return type;
 }
@@ -1397,8 +1423,8 @@ get_target_type (struct type *type)
   if (type != NULL)
     {
       type = TYPE_TARGET_TYPE (type);
-      while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
-       type = TYPE_TARGET_TYPE (type);
+      if (type != NULL)
+       type = check_typedef (type);
     }
 
   return type;
@@ -1660,7 +1686,7 @@ value_of_child (struct varobj *parent, int index)
   value = (*parent->root->lang->value_of_child) (parent, index);
 
   /* If we're being lazy, fetch the real value of the variable. */
-  if (value != NULL && VALUE_LAZY (value))
+  if (value != NULL && value_lazy (value))
     {
       /* If we fail to fetch the value of the child, return
          NULL so that callers notice that we're leaving an
@@ -1680,7 +1706,7 @@ type_of_child (struct varobj *var)
   /* If the child had no evaluation errors, var->value
      will be non-NULL and contain a valid type. */
   if (var->value != NULL)
-    return VALUE_TYPE (var->value);
+    return value_type (var->value);
 
   /* Otherwise, we must compute the type. */
   return (*var->root->lang->type_of_child) (var->parent, var->index);
@@ -1812,7 +1838,7 @@ c_name_of_child (struct varobj *parent, int index)
   switch (TYPE_CODE (type))
     {
     case TYPE_CODE_ARRAY:
-      xasprintf (&name, "%d", index);
+      name = xstrprintf ("%d", index);
       break;
 
     case TYPE_CODE_STRUCT:
@@ -1831,7 +1857,7 @@ c_name_of_child (struct varobj *parent, int index)
          break;
 
        default:
-         xasprintf (&name, "*%s", parent->name);
+         name = xstrprintf ("*%s", parent->name);
          break;
        }
       break;
@@ -1878,14 +1904,14 @@ c_value_of_root (struct varobj **var_handle)
          go on */
       if (gdb_evaluate_expression (var->root->exp, &new_val))
        {
-         if (VALUE_LAZY (new_val))
+         if (value_lazy (new_val))
            {
              /* We need to catch errors because if
                 value_fetch_lazy fails we still want to continue
                 (after making val->error = 1) */
-             /* FIXME: Shouldn't be using VALUE_CONTENTS?  The
-                comment on value_fetch_lazy() says it is only
-                called from the macro... */
+             /* FIXME: Shouldn't be using value_contents()?  The
+                comment on value_fetch_lazy() says it is only called
+                from the macro... */
              if (!gdb_value_fetch_lazy (new_val))
                var->error = 1;
              else
@@ -2045,7 +2071,7 @@ c_value_of_variable (struct varobj *var)
     case TYPE_CODE_ARRAY:
       {
        char *number;
-       xasprintf (&number, "[%d]", var->num_children);
+       number = xstrprintf ("[%d]", var->num_children);
        return (number);
       }
       /* break; */
@@ -2066,10 +2092,10 @@ c_value_of_variable (struct varobj *var)
            struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
            char *thevalue;
 
-           if (VALUE_LAZY (var->value))
+           if (value_lazy (var->value))
              gdb_value_fetch_lazy (var->value);
-           val_print (VALUE_TYPE (var->value),
-                      VALUE_CONTENTS_RAW (var->value), 0,
+           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);
@@ -2123,9 +2149,9 @@ cplus_number_of_children (struct varobj *var)
       type = get_type_deref (var->parent);
 
       cplus_class_num_children (type, kids);
-      if (STREQ (var->name, "public"))
+      if (strcmp (var->name, "public") == 0)
        children = kids[v_public];
-      else if (STREQ (var->name, "private"))
+      else if (strcmp (var->name, "private") == 0)
        children = kids[v_private];
       else
        children = kids[v_protected];
@@ -2176,7 +2202,6 @@ cplus_name_of_child (struct varobj *parent, int index)
 {
   char *name;
   struct type *type;
-  int children[3];
 
   if (CPLUS_FAKE_CHILD (parent))
     {
@@ -2191,55 +2216,97 @@ cplus_name_of_child (struct varobj *parent, int index)
     {
     case TYPE_CODE_STRUCT:
     case TYPE_CODE_UNION:
-      cplus_class_num_children (type, children);
-
       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 */
-         i = index + TYPE_N_BASECLASSES (type);
-         if (STREQ (parent->name, "private")
-             || STREQ (parent->name, "protected"))
-           i += children[v_public];
-         if (STREQ (parent->name, "protected"))
-           i += children[v_private];
-
-         name = TYPE_FIELD_NAME (type, i);
+         /* The fields of the class type are ordered as they
+            appear in the class.  We are given an index for a
+            particular access control type ("public","protected",
+            or "private").  We must skip over fields that don't
+            have the access control we are looking for to properly
+            find the indexed field. */
+         int type_index = TYPE_N_BASECLASSES (type);
+         if (strcmp (parent->name, "private") == 0)
+           {
+             while (index >= 0)
+               {
+                 if (TYPE_VPTR_BASETYPE (type) == type
+                     && type_index == TYPE_VPTR_FIELDNO (type))
+                   ; /* ignore vptr */
+                 else if (TYPE_FIELD_PRIVATE (type, type_index))
+                   --index;
+                 ++type_index;
+               }
+             --type_index;
+           }
+         else if (strcmp (parent->name, "protected") == 0)
+           {
+             while (index >= 0)
+               {
+                 if (TYPE_VPTR_BASETYPE (type) == type
+                     && type_index == TYPE_VPTR_FIELDNO (type))
+                   ; /* ignore vptr */
+                 else if (TYPE_FIELD_PROTECTED (type, type_index))
+                   --index;
+                 ++type_index;
+               }
+             --type_index;
+           }
+         else
+           {
+             while (index >= 0)
+               {
+                 if (TYPE_VPTR_BASETYPE (type) == type
+                     && type_index == TYPE_VPTR_FIELDNO (type))
+                   ; /* ignore vptr */
+                 else if (!TYPE_FIELD_PRIVATE (type, type_index) &&
+                     !TYPE_FIELD_PROTECTED (type, type_index))
+                   --index;
+                 ++type_index;
+               }
+             --type_index;
+           }
+
+         name = TYPE_FIELD_NAME (type, type_index);
        }
       else if (index < TYPE_N_BASECLASSES (type))
+       /* We are looking up the name of a base class */
        name = TYPE_FIELD_NAME (type, index);
       else
        {
+         int children[3];
+         cplus_class_num_children(type, children);
+
          /* Everything beyond the baseclasses can
-            only be "public", "private", or "protected" */
+            only be "public", "private", or "protected"
+
+            The special "fake" children are always output by varobj in
+            this order. So if INDEX == 2, it MUST be "protected". */
          index -= TYPE_N_BASECLASSES (type);
          switch (index)
            {
            case 0:
-             if (children[v_public] != 0)
-               {
-                 name = "public";
-                 break;
-               }
+             if (children[v_public] > 0)
+               name = "public";
+             else if (children[v_private] > 0)
+               name = "private";
+             else 
+               name = "protected";
+             break;
            case 1:
-             if (children[v_private] != 0)
+             if (children[v_public] > 0)
                {
-                 name = "private";
-                 break;
+                 if (children[v_private] > 0)
+                   name = "private";
+                 else
+                   name = "protected";
                }
+             else if (children[v_private] > 0)
+               name = "protected";
+             break;
            case 2:
-             if (children[v_protected] != 0)
-               {
-                 name = "protected";
-                 break;
-               }
+             /* Must be protected */
+             name = "protected";
+             break;
            default:
              /* error! */
              break;
@@ -2312,8 +2379,8 @@ cplus_value_of_child (struct varobj *parent, int index)
            {
              struct value *temp = NULL;
 
-             if (TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_PTR
-                 || TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_REF)
+             if (TYPE_CODE (value_type (parent->value)) == TYPE_CODE_PTR
+                 || TYPE_CODE (value_type (parent->value)) == TYPE_CODE_REF)
                {
                  if (!gdb_value_ind (parent->value, &temp))
                    return NULL;
@@ -2492,7 +2559,7 @@ _initialize_varobj (void)
   varobj_table = xmalloc (sizeof_table);
   memset (varobj_table, 0, sizeof_table);
 
-  add_show_from_set (add_set_cmd ("debugvarobj", class_maintenance, var_zinteger, (char *) &varobjdebug, "Set varobj debugging.\n\
+  deprecated_add_show_from_set (add_set_cmd ("debugvarobj", class_maintenance, var_zinteger, (char *) &varobjdebug, "Set varobj debugging.\n\
 When non-zero, varobj debugging is enabled.", &setlist),
                     &showlist);
 }
This page took 0.030955 seconds and 4 git commands to generate.