1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
22 #include "exceptions.h"
24 #include "expression.h"
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
36 /* Non-zero if we want to see trace of varobj level stuff. */
40 show_varobjdebug (struct ui_file *file, int from_tty,
41 struct cmd_list_element *c, const char *value)
43 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
46 /* String representations of gdb's format codes */
47 char *varobj_format_string[] =
48 { "natural", "binary", "decimal", "hexadecimal", "octal" };
50 /* String representations of gdb's known languages */
51 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
55 /* Every root variable has one of these structures saved in its
56 varobj. Members which must be free'd are noted. */
60 /* Alloc'd expression for this parent. */
61 struct expression *exp;
63 /* Block for which this expression is valid */
64 struct block *valid_block;
66 /* The frame for this expression */
67 struct frame_id frame;
69 /* If 1, "update" always recomputes the frame & valid block
70 using the currently selected frame. */
71 int use_selected_frame;
73 /* Language info for this variable and its children */
74 struct language_specific *lang;
76 /* The varobj for this root node. */
77 struct varobj *rootvar;
79 /* Next root variable */
80 struct varobj_root *next;
83 /* Every variable in the system has a structure of this type defined
84 for it. This structure holds all information necessary to manipulate
85 a particular object variable. Members which must be freed are noted. */
89 /* Alloc'd name of the variable for this object.. If this variable is a
90 child, then this name will be the child's source name.
92 /* NOTE: This is the "expression" */
95 /* The alloc'd name for this variable's object. This is here for
96 convenience when constructing this object's children. */
99 /* Index of this variable in its parent or -1 */
102 /* The type of this variable. This may NEVER be NULL. */
105 /* The value of this expression or subexpression. This may be NULL.
106 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
107 the value is either NULL, or not lazy. */
110 /* Did an error occur evaluating the expression or getting its value? */
113 /* The number of (immediate) children this variable has */
116 /* If this object is a child, this points to its immediate parent. */
117 struct varobj *parent;
119 /* A list of this object's children */
120 struct varobj_child *children;
122 /* Description of the root variable. Points to root variable for children. */
123 struct varobj_root *root;
125 /* The format of the output for this object */
126 enum varobj_display_formats format;
128 /* Was this variable updated via a varobj_set_value operation */
132 /* Every variable keeps a linked list of its children, described
133 by the following structure. */
134 /* FIXME: Deprecated. All should use vlist instead */
139 /* Pointer to the child's data */
140 struct varobj *child;
142 /* Pointer to the next child */
143 struct varobj_child *next;
146 /* A stack of varobjs */
147 /* FIXME: Deprecated. All should use vlist instead */
158 struct cpstack *next;
161 /* A list of varobjs */
169 /* Private function prototypes */
171 /* Helper functions for the above subcommands. */
173 static int delete_variable (struct cpstack **, struct varobj *, int);
175 static void delete_variable_1 (struct cpstack **, int *,
176 struct varobj *, int, int);
178 static int install_variable (struct varobj *);
180 static void uninstall_variable (struct varobj *);
182 static struct varobj *child_exists (struct varobj *, char *);
184 static struct varobj *create_child (struct varobj *, int, char *);
186 static void save_child_in_parent (struct varobj *, struct varobj *);
188 static void remove_child_from_parent (struct varobj *, struct varobj *);
190 /* Utility routines */
192 static struct varobj *new_variable (void);
194 static struct varobj *new_root_variable (void);
196 static void free_variable (struct varobj *var);
198 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
200 static struct type *get_type (struct varobj *var);
202 static struct type *get_type_deref (struct varobj *var);
204 static struct type *get_target_type (struct type *);
206 static enum varobj_display_formats variable_default_display (struct varobj *);
208 static void vpush (struct vstack **pstack, struct varobj *var);
210 static struct varobj *vpop (struct vstack **pstack);
212 static void cppush (struct cpstack **pstack, char *name);
214 static char *cppop (struct cpstack **pstack);
216 static int install_new_value (struct varobj *var, struct value *value,
219 /* Language-specific routines. */
221 static enum varobj_languages variable_language (struct varobj *var);
223 static int number_of_children (struct varobj *);
225 static char *name_of_variable (struct varobj *);
227 static char *name_of_child (struct varobj *, int);
229 static struct value *value_of_root (struct varobj **var_handle, int *);
231 static struct value *value_of_child (struct varobj *parent, int index);
233 static int variable_editable (struct varobj *var);
235 static char *my_value_of_variable (struct varobj *var);
237 static int varobj_value_is_changeable_p (struct varobj *var);
239 static int is_root_p (struct varobj *var);
241 /* C implementation */
243 static int c_number_of_children (struct varobj *var);
245 static char *c_name_of_variable (struct varobj *parent);
247 static char *c_name_of_child (struct varobj *parent, int index);
249 static struct value *c_value_of_root (struct varobj **var_handle);
251 static struct value *c_value_of_child (struct varobj *parent, int index);
253 static struct type *c_type_of_child (struct varobj *parent, int index);
255 static int c_variable_editable (struct varobj *var);
257 static char *c_value_of_variable (struct varobj *var);
259 /* C++ implementation */
261 static int cplus_number_of_children (struct varobj *var);
263 static void cplus_class_num_children (struct type *type, int children[3]);
265 static char *cplus_name_of_variable (struct varobj *parent);
267 static char *cplus_name_of_child (struct varobj *parent, int index);
269 static struct value *cplus_value_of_root (struct varobj **var_handle);
271 static struct value *cplus_value_of_child (struct varobj *parent, int index);
273 static struct type *cplus_type_of_child (struct varobj *parent, int index);
275 static int cplus_variable_editable (struct varobj *var);
277 static char *cplus_value_of_variable (struct varobj *var);
279 /* Java implementation */
281 static int java_number_of_children (struct varobj *var);
283 static char *java_name_of_variable (struct varobj *parent);
285 static char *java_name_of_child (struct varobj *parent, int index);
287 static struct value *java_value_of_root (struct varobj **var_handle);
289 static struct value *java_value_of_child (struct varobj *parent, int index);
291 static struct type *java_type_of_child (struct varobj *parent, int index);
293 static int java_variable_editable (struct varobj *var);
295 static char *java_value_of_variable (struct varobj *var);
297 /* The language specific vector */
299 struct language_specific
302 /* The language of this variable */
303 enum varobj_languages language;
305 /* The number of children of PARENT. */
306 int (*number_of_children) (struct varobj * parent);
308 /* The name (expression) of a root varobj. */
309 char *(*name_of_variable) (struct varobj * parent);
311 /* The name of the INDEX'th child of PARENT. */
312 char *(*name_of_child) (struct varobj * parent, int index);
314 /* The ``struct value *'' of the root variable ROOT. */
315 struct value *(*value_of_root) (struct varobj ** root_handle);
317 /* The ``struct value *'' of the INDEX'th child of PARENT. */
318 struct value *(*value_of_child) (struct varobj * parent, int index);
320 /* The type of the INDEX'th child of PARENT. */
321 struct type *(*type_of_child) (struct varobj * parent, int index);
323 /* Is VAR editable? */
324 int (*variable_editable) (struct varobj * var);
326 /* The current value of VAR. */
327 char *(*value_of_variable) (struct varobj * var);
330 /* Array of known source language routines. */
331 static struct language_specific languages[vlang_end] = {
332 /* Unknown (try treating as C */
335 c_number_of_children,
347 c_number_of_children,
359 cplus_number_of_children,
360 cplus_name_of_variable,
363 cplus_value_of_child,
365 cplus_variable_editable,
366 cplus_value_of_variable}
371 java_number_of_children,
372 java_name_of_variable,
377 java_variable_editable,
378 java_value_of_variable}
381 /* A little convenience enum for dealing with C++/Java */
384 v_public = 0, v_private, v_protected
389 /* Mappings of varobj_display_formats enums to gdb's format codes */
390 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
392 /* Header of the list of root variable objects */
393 static struct varobj_root *rootlist;
394 static int rootcount = 0; /* number of root varobjs in the list */
396 /* Prime number indicating the number of buckets in the hash table */
397 /* A prime large enough to avoid too many colisions */
398 #define VAROBJ_TABLE_SIZE 227
400 /* Pointer to the varobj hash table (built at run time) */
401 static struct vlist **varobj_table;
403 /* Is the variable X one of our "fake" children? */
404 #define CPLUS_FAKE_CHILD(x) \
405 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
408 /* API Implementation */
410 is_root_p (struct varobj *var)
412 return (var->root->rootvar == var);
415 /* Creates a varobj (not its children) */
417 /* Return the full FRAME which corresponds to the given CORE_ADDR
418 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
420 static struct frame_info *
421 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
423 struct frame_info *frame = NULL;
425 if (frame_addr == (CORE_ADDR) 0)
430 frame = get_prev_frame (frame);
433 if (get_frame_base_address (frame) == frame_addr)
439 varobj_create (char *objname,
440 char *expression, CORE_ADDR frame, enum varobj_type type)
443 struct frame_info *fi;
444 struct frame_info *old_fi = NULL;
446 struct cleanup *old_chain;
448 /* Fill out a varobj structure for the (root) variable being constructed. */
449 var = new_root_variable ();
450 old_chain = make_cleanup_free_variable (var);
452 if (expression != NULL)
455 enum varobj_languages lang;
458 /* Parse and evaluate the expression, filling in as much
459 of the variable's data as possible */
461 /* Allow creator to specify context of variable */
462 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
463 fi = deprecated_selected_frame;
465 /* FIXME: cagney/2002-11-23: This code should be doing a
466 lookup using the frame ID and not just the frame's
467 ``address''. This, of course, means an interface change.
468 However, with out that interface change ISAs, such as the
469 ia64 with its two stacks, won't work. Similar goes for the
470 case where there is a frameless function. */
471 fi = find_frame_addr_in_frame_chain (frame);
473 /* frame = -2 means always use selected frame */
474 if (type == USE_SELECTED_FRAME)
475 var->root->use_selected_frame = 1;
479 block = get_frame_block (fi, 0);
482 innermost_block = NULL;
483 /* Wrap the call to parse expression, so we can
484 return a sensible error. */
485 if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
490 /* Don't allow variables to be created for types. */
491 if (var->root->exp->elts[0].opcode == OP_TYPE)
493 do_cleanups (old_chain);
494 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
495 " as an expression.\n");
499 var->format = variable_default_display (var);
500 var->root->valid_block = innermost_block;
501 var->name = savestring (expression, strlen (expression));
503 /* When the frame is different from the current frame,
504 we must select the appropriate frame before parsing
505 the expression, otherwise the value will not be current.
506 Since select_frame is so benign, just call it for all cases. */
509 var->root->frame = get_frame_id (fi);
510 old_fi = deprecated_selected_frame;
514 /* We definitively need to catch errors here.
515 If evaluate_expression succeeds we got the value we wanted.
516 But if it fails, we still go on with a call to evaluate_type() */
517 if (!gdb_evaluate_expression (var->root->exp, &value))
518 /* Error getting the value. Try to at least get the
520 value = evaluate_type (var->root->exp);
522 var->type = value_type (value);
523 install_new_value (var, value, 1 /* Initial assignment */);
525 /* Set language info */
526 lang = variable_language (var);
527 var->root->lang = &languages[lang];
529 /* Set ourselves as our root */
530 var->root->rootvar = var;
532 /* Reset the selected frame */
534 select_frame (old_fi);
537 /* If the variable object name is null, that means this
538 is a temporary variable, so don't install it. */
540 if ((var != NULL) && (objname != NULL))
542 var->obj_name = savestring (objname, strlen (objname));
544 /* If a varobj name is duplicated, the install will fail so
546 if (!install_variable (var))
548 do_cleanups (old_chain);
553 discard_cleanups (old_chain);
557 /* Generates an unique name that can be used for a varobj */
560 varobj_gen_name (void)
565 /* generate a name for this object */
567 obj_name = xstrprintf ("var%d", id);
572 /* Given an "objname", returns the pointer to the corresponding varobj
573 or NULL if not found */
576 varobj_get_handle (char *objname)
580 unsigned int index = 0;
583 for (chp = objname; *chp; chp++)
585 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
588 cv = *(varobj_table + index);
589 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
593 error (_("Variable object not found"));
598 /* Given the handle, return the name of the object */
601 varobj_get_objname (struct varobj *var)
603 return var->obj_name;
606 /* Given the handle, return the expression represented by the object */
609 varobj_get_expression (struct varobj *var)
611 return name_of_variable (var);
614 /* Deletes a varobj and all its children if only_children == 0,
615 otherwise deletes only the children; returns a malloc'ed list of all the
616 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
619 varobj_delete (struct varobj *var, char ***dellist, int only_children)
623 struct cpstack *result = NULL;
626 /* Initialize a stack for temporary results */
627 cppush (&result, NULL);
630 /* Delete only the variable children */
631 delcount = delete_variable (&result, var, 1 /* only the children */ );
633 /* Delete the variable and all its children */
634 delcount = delete_variable (&result, var, 0 /* parent+children */ );
636 /* We may have been asked to return a list of what has been deleted */
639 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
643 *cp = cppop (&result);
644 while ((*cp != NULL) && (mycount > 0))
648 *cp = cppop (&result);
651 if (mycount || (*cp != NULL))
652 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
659 /* Set/Get variable object display format */
661 enum varobj_display_formats
662 varobj_set_display_format (struct varobj *var,
663 enum varobj_display_formats format)
670 case FORMAT_HEXADECIMAL:
672 var->format = format;
676 var->format = variable_default_display (var);
682 enum varobj_display_formats
683 varobj_get_display_format (struct varobj *var)
689 varobj_get_num_children (struct varobj *var)
691 if (var->num_children == -1)
692 var->num_children = number_of_children (var);
694 return var->num_children;
697 /* Creates a list of the immediate children of a variable object;
698 the return code is the number of such children or -1 on error */
701 varobj_list_children (struct varobj *var, struct varobj ***childlist)
703 struct varobj *child;
707 /* sanity check: have we been passed a pointer? */
708 if (childlist == NULL)
713 if (var->num_children == -1)
714 var->num_children = number_of_children (var);
716 /* List of children */
717 *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
719 for (i = 0; i < var->num_children; i++)
721 /* Mark as the end in case we bail out */
722 *((*childlist) + i) = NULL;
724 /* check if child exists, if not create */
725 name = name_of_child (var, i);
726 child = child_exists (var, name);
728 child = create_child (var, i, name);
730 *((*childlist) + i) = child;
733 /* End of list is marked by a NULL pointer */
734 *((*childlist) + i) = NULL;
736 return var->num_children;
739 /* Obtain the type of an object Variable as a string similar to the one gdb
740 prints on the console */
743 varobj_get_type (struct varobj *var)
746 struct cleanup *old_chain;
751 /* For the "fake" variables, do not return a type. (It's type is
753 if (CPLUS_FAKE_CHILD (var))
756 stb = mem_fileopen ();
757 old_chain = make_cleanup_ui_file_delete (stb);
759 /* To print the type, we simply create a zero ``struct value *'' and
760 cast it to our type. We then typeprint this variable. */
761 val = value_zero (var->type, not_lval);
762 type_print (value_type (val), "", stb, -1);
764 thetype = ui_file_xstrdup (stb, &length);
765 do_cleanups (old_chain);
769 /* Obtain the type of an object variable. */
772 varobj_get_gdb_type (struct varobj *var)
777 enum varobj_languages
778 varobj_get_language (struct varobj *var)
780 return variable_language (var);
784 varobj_get_attributes (struct varobj *var)
788 if (variable_editable (var))
789 /* FIXME: define masks for attributes */
790 attributes |= 0x00000001; /* Editable */
796 varobj_get_value (struct varobj *var)
798 return my_value_of_variable (var);
801 /* Set the value of an object variable (if it is editable) to the
802 value of the given expression */
803 /* Note: Invokes functions that can call error() */
806 varobj_set_value (struct varobj *var, char *expression)
812 /* The argument "expression" contains the variable's new value.
813 We need to first construct a legal expression for this -- ugh! */
814 /* Does this cover all the bases? */
815 struct expression *exp;
817 int saved_input_radix = input_radix;
819 if (var->value != NULL && variable_editable (var) && !var->error)
821 char *s = expression;
824 input_radix = 10; /* ALWAYS reset to decimal temporarily */
825 exp = parse_exp_1 (&s, 0, 0);
826 if (!gdb_evaluate_expression (exp, &value))
828 /* We cannot proceed without a valid expression. */
833 /* All types that are editable must also be changeable. */
834 gdb_assert (varobj_value_is_changeable_p (var));
836 /* The value of a changeable variable object must not be lazy. */
837 gdb_assert (!value_lazy (var->value));
839 /* Need to coerce the input. We want to check if the
840 value of the variable object will be different
841 after assignment, and the first thing value_assign
842 does is coerce the input.
843 For example, if we are assigning an array to a pointer variable we
844 should compare the pointer with the the array's address, not with the
846 value = coerce_array (value);
848 /* The new value may be lazy. gdb_value_assign, or
849 rather value_contents, will take care of this.
850 If fetching of the new value will fail, gdb_value_assign
851 with catch the exception. */
852 if (!gdb_value_assign (var->value, value, &val))
855 /* If the value has changed, record it, so that next -var-update can
856 report this change. If a variable had a value of '1', we've set it
857 to '333' and then set again to '1', when -var-update will report this
858 variable as changed -- because the first assignment has set the
859 'updated' flag. There's no need to optimize that, because return value
860 of -var-update should be considered an approximation. */
861 var->updated = install_new_value (var, val, 0 /* Compare values. */);
862 input_radix = saved_input_radix;
869 /* Returns a malloc'ed list with all root variable objects */
871 varobj_list (struct varobj ***varlist)
874 struct varobj_root *croot;
875 int mycount = rootcount;
877 /* Alloc (rootcount + 1) entries for the result */
878 *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
882 while ((croot != NULL) && (mycount > 0))
884 *cv = croot->rootvar;
889 /* Mark the end of the list */
892 if (mycount || (croot != NULL))
894 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
900 /* Assign a new value to a variable object. If INITIAL is non-zero,
901 this is the first assignement after the variable object was just
902 created, or changed type. In that case, just assign the value
904 Otherwise, assign the value and if type_changeable returns non-zero,
905 find if the new value is different from the current value.
906 Return 1 if so, and 0 if the values are equal.
908 The VALUE parameter should not be released -- the function will
909 take care of releasing it when needed. */
911 install_new_value (struct varobj *var, struct value *value, int initial)
918 /* We need to know the varobj's type to decide if the value should
919 be fetched or not. C++ fake children (public/protected/private) don't have
921 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
922 changeable = varobj_value_is_changeable_p (var);
923 need_to_fetch = changeable;
925 /* We are not interested in the address of references, and given
926 that in C++ a reference is not rebindable, it cannot
927 meaningfully change. So, get hold of the real value. */
930 value = coerce_ref (value);
931 release_value (value);
934 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
935 /* For unions, we need to fetch the value implicitly because
936 of implementation of union member fetch. When gdb
937 creates a value for a field and the value of the enclosing
938 structure is not lazy, it immediately copies the necessary
939 bytes from the enclosing values. If the enclosing value is
940 lazy, the call to value_fetch_lazy on the field will read
941 the data from memory. For unions, that means we'll read the
942 same memory more than once, which is not desirable. So
946 /* The new value might be lazy. If the type is changeable,
947 that is we'll be comparing values of this type, fetch the
948 value now. Otherwise, on the next update the old value
949 will be lazy, which means we've lost that old value. */
950 if (need_to_fetch && value && value_lazy (value))
952 if (!gdb_value_fetch_lazy (value))
955 /* Set the value to NULL, so that for the next -var-update,
956 we don't try to compare the new value with this value,
957 that we couldn't even read. */
964 /* If the type is changeable, compare the old and the new values.
965 If this is the initial assignment, we don't have any old value
967 if (!initial && changeable)
969 /* If the value of the varobj was changed by -var-set-value, then the
970 value in the varobj and in the target is the same. However, that value
971 is different from the value that the varobj had after the previous
972 -var-update. So need to the varobj as changed. */
977 /* Try to compare the values. That requires that both
978 values are non-lazy. */
980 /* Quick comparison of NULL values. */
981 if (var->value == NULL && value == NULL)
984 else if (var->value == NULL || value == NULL)
988 gdb_assert (!value_lazy (var->value));
989 gdb_assert (!value_lazy (value));
991 if (!value_contents_equal (var->value, value))
997 /* We must always keep the new value, since children depend on it. */
998 if (var->value != NULL)
999 value_free (var->value);
1003 gdb_assert (!var->value || value_type (var->value));
1009 /* Update the values for a variable and its children. This is a
1010 two-pronged attack. First, re-parse the value for the root's
1011 expression to see if it's changed. Then go all the way
1012 through its children, reconstructing them and noting if they've
1015 -1 if there was an error updating the varobj
1016 -2 if the type changed
1017 Otherwise it is the number of children + parent changed
1019 Only root variables can be updated...
1021 NOTE: This function may delete the caller's varobj. If it
1022 returns -2, then it has done this and VARP will be modified
1023 to point to the new varobj. */
1026 varobj_update (struct varobj **varp, struct varobj ***changelist)
1035 struct varobj **templist = NULL;
1037 struct vstack *stack = NULL;
1038 struct vstack *result = NULL;
1039 struct frame_id old_fid;
1040 struct frame_info *fi;
1042 /* sanity check: have we been passed a pointer? */
1043 if (changelist == NULL)
1046 /* Only root variables can be updated... */
1047 if (!is_root_p (*varp))
1048 /* Not a root var */
1051 /* Save the selected stack frame, since we will need to change it
1052 in order to evaluate expressions. */
1053 old_fid = get_frame_id (deprecated_selected_frame);
1055 /* Update the root variable. value_of_root can return NULL
1056 if the variable is no longer around, i.e. we stepped out of
1057 the frame in which a local existed. We are letting the
1058 value_of_root variable dispose of the varobj if the type
1061 new = value_of_root (varp, &type_changed);
1063 /* Restore selected frame */
1064 fi = frame_find_by_id (old_fid);
1074 /* Initialize a stack for temporary results */
1075 vpush (&result, NULL);
1077 /* If this is a "use_selected_frame" varobj, and its type has changed,
1078 them note that it's changed. */
1081 vpush (&result, *varp);
1085 if (install_new_value ((*varp), new, type_changed))
1087 /* If type_changed is 1, install_new_value will never return
1088 non-zero, so we'll never report the same variable twice. */
1089 gdb_assert (!type_changed);
1090 vpush (&result, (*varp));
1094 /* Initialize a stack */
1095 vpush (&stack, NULL);
1097 /* Push the root's children */
1098 if ((*varp)->children != NULL)
1100 struct varobj_child *c;
1101 for (c = (*varp)->children; c != NULL; c = c->next)
1102 vpush (&stack, c->child);
1105 /* Walk through the children, reconstructing them all. */
1109 /* Push any children */
1110 if (v->children != NULL)
1112 struct varobj_child *c;
1113 for (c = v->children; c != NULL; c = c->next)
1114 vpush (&stack, c->child);
1117 /* Update this variable */
1118 new = value_of_child (v->parent, v->index);
1119 if (install_new_value (v, new, 0 /* type not changed */))
1121 /* Note that it's changed */
1127 /* Get next child */
1131 /* Alloc (changed + 1) list entries */
1132 /* FIXME: add a cleanup for the allocated list(s)
1133 because one day the select_frame called below can longjump */
1134 *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1137 templist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1143 /* Copy from result stack to list */
1145 *cv = vpop (&result);
1146 while ((*cv != NULL) && (vleft > 0))
1150 *cv = vpop (&result);
1153 warning (_("varobj_update: assertion failed - vleft <> 0"));
1157 /* Now we revert the order. */
1158 for (i = 0; i < changed; i++)
1159 *(*changelist + i) = *(templist + changed - 1 - i);
1160 *(*changelist + changed) = NULL;
1170 /* Helper functions */
1173 * Variable object construction/destruction
1177 delete_variable (struct cpstack **resultp, struct varobj *var,
1178 int only_children_p)
1182 delete_variable_1 (resultp, &delcount, var,
1183 only_children_p, 1 /* remove_from_parent_p */ );
1188 /* Delete the variable object VAR and its children */
1189 /* IMPORTANT NOTE: If we delete a variable which is a child
1190 and the parent is not removed we dump core. It must be always
1191 initially called with remove_from_parent_p set */
1193 delete_variable_1 (struct cpstack **resultp, int *delcountp,
1194 struct varobj *var, int only_children_p,
1195 int remove_from_parent_p)
1197 struct varobj_child *vc;
1198 struct varobj_child *next;
1200 /* Delete any children of this variable, too. */
1201 for (vc = var->children; vc != NULL; vc = next)
1203 if (!remove_from_parent_p)
1204 vc->child->parent = NULL;
1205 delete_variable_1 (resultp, delcountp, vc->child, 0, only_children_p);
1210 /* if we were called to delete only the children we are done here */
1211 if (only_children_p)
1214 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1215 /* If the name is null, this is a temporary variable, that has not
1216 yet been installed, don't report it, it belongs to the caller... */
1217 if (var->obj_name != NULL)
1219 cppush (resultp, xstrdup (var->obj_name));
1220 *delcountp = *delcountp + 1;
1223 /* If this variable has a parent, remove it from its parent's list */
1224 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1225 (as indicated by remove_from_parent_p) we don't bother doing an
1226 expensive list search to find the element to remove when we are
1227 discarding the list afterwards */
1228 if ((remove_from_parent_p) && (var->parent != NULL))
1230 remove_child_from_parent (var->parent, var);
1233 if (var->obj_name != NULL)
1234 uninstall_variable (var);
1236 /* Free memory associated with this variable */
1237 free_variable (var);
1240 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1242 install_variable (struct varobj *var)
1245 struct vlist *newvl;
1247 unsigned int index = 0;
1250 for (chp = var->obj_name; *chp; chp++)
1252 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1255 cv = *(varobj_table + index);
1256 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1260 error (_("Duplicate variable object name"));
1262 /* Add varobj to hash table */
1263 newvl = xmalloc (sizeof (struct vlist));
1264 newvl->next = *(varobj_table + index);
1266 *(varobj_table + index) = newvl;
1268 /* If root, add varobj to root list */
1269 if (is_root_p (var))
1271 /* Add to list of root variables */
1272 if (rootlist == NULL)
1273 var->root->next = NULL;
1275 var->root->next = rootlist;
1276 rootlist = var->root;
1283 /* Unistall the object VAR. */
1285 uninstall_variable (struct varobj *var)
1289 struct varobj_root *cr;
1290 struct varobj_root *prer;
1292 unsigned int index = 0;
1295 /* Remove varobj from hash table */
1296 for (chp = var->obj_name; *chp; chp++)
1298 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1301 cv = *(varobj_table + index);
1303 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1310 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1315 ("Assertion failed: Could not find variable object \"%s\" to delete",
1321 *(varobj_table + index) = cv->next;
1323 prev->next = cv->next;
1327 /* If root, remove varobj from root list */
1328 if (is_root_p (var))
1330 /* Remove from list of root variables */
1331 if (rootlist == var->root)
1332 rootlist = var->root->next;
1337 while ((cr != NULL) && (cr->rootvar != var))
1345 ("Assertion failed: Could not find varobj \"%s\" in root list",
1352 prer->next = cr->next;
1359 /* Does a child with the name NAME exist in VAR? If so, return its data.
1360 If not, return NULL. */
1361 static struct varobj *
1362 child_exists (struct varobj *var, char *name)
1364 struct varobj_child *vc;
1366 for (vc = var->children; vc != NULL; vc = vc->next)
1368 if (strcmp (vc->child->name, name) == 0)
1375 /* Create and install a child of the parent of the given name */
1376 static struct varobj *
1377 create_child (struct varobj *parent, int index, char *name)
1379 struct varobj *child;
1381 struct value *value;
1383 child = new_variable ();
1385 /* name is allocated by name_of_child */
1387 child->index = index;
1388 value = value_of_child (parent, index);
1389 child->parent = parent;
1390 child->root = parent->root;
1391 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
1392 child->obj_name = childs_name;
1393 install_variable (child);
1395 /* Save a pointer to this child in the parent */
1396 save_child_in_parent (parent, child);
1398 /* Compute the type of the child. Must do this before
1399 calling install_new_value. */
1401 /* If the child had no evaluation errors, var->value
1402 will be non-NULL and contain a valid type. */
1403 child->type = value_type (value);
1405 /* Otherwise, we must compute the type. */
1406 child->type = (*child->root->lang->type_of_child) (child->parent,
1408 install_new_value (child, value, 1);
1410 if ((!CPLUS_FAKE_CHILD (child) && child->value == NULL) || parent->error)
1416 /* FIXME: This should be a generic add to list */
1417 /* Save CHILD in the PARENT's data. */
1419 save_child_in_parent (struct varobj *parent, struct varobj *child)
1421 struct varobj_child *vc;
1423 /* Insert the child at the top */
1424 vc = parent->children;
1426 (struct varobj_child *) xmalloc (sizeof (struct varobj_child));
1428 parent->children->next = vc;
1429 parent->children->child = child;
1432 /* FIXME: This should be a generic remove from list */
1433 /* Remove the CHILD from the PARENT's list of children. */
1435 remove_child_from_parent (struct varobj *parent, struct varobj *child)
1437 struct varobj_child *vc, *prev;
1439 /* Find the child in the parent's list */
1441 for (vc = parent->children; vc != NULL;)
1443 if (vc->child == child)
1450 parent->children = vc->next;
1452 prev->next = vc->next;
1458 * Miscellaneous utility functions.
1461 /* Allocate memory and initialize a new variable */
1462 static struct varobj *
1467 var = (struct varobj *) xmalloc (sizeof (struct varobj));
1469 var->obj_name = NULL;
1474 var->num_children = -1;
1476 var->children = NULL;
1484 /* Allocate memory and initialize a new root variable */
1485 static struct varobj *
1486 new_root_variable (void)
1488 struct varobj *var = new_variable ();
1489 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1490 var->root->lang = NULL;
1491 var->root->exp = NULL;
1492 var->root->valid_block = NULL;
1493 var->root->frame = null_frame_id;
1494 var->root->use_selected_frame = 0;
1495 var->root->rootvar = NULL;
1500 /* Free any allocated memory associated with VAR. */
1502 free_variable (struct varobj *var)
1504 /* Free the expression if this is a root variable. */
1505 if (is_root_p (var))
1507 free_current_contents (&var->root->exp);
1512 xfree (var->obj_name);
1517 do_free_variable_cleanup (void *var)
1519 free_variable (var);
1522 static struct cleanup *
1523 make_cleanup_free_variable (struct varobj *var)
1525 return make_cleanup (do_free_variable_cleanup, var);
1528 /* This returns the type of the variable. It also skips past typedefs
1529 to return the real type of the variable.
1531 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1532 except within get_target_type and get_type. */
1533 static struct type *
1534 get_type (struct varobj *var)
1540 type = check_typedef (type);
1545 /* This returns the type of the variable, dereferencing pointers, too. */
1546 static struct type *
1547 get_type_deref (struct varobj *var)
1551 type = get_type (var);
1553 if (type != NULL && (TYPE_CODE (type) == TYPE_CODE_PTR
1554 || TYPE_CODE (type) == TYPE_CODE_REF))
1555 type = get_target_type (type);
1560 /* This returns the target type (or NULL) of TYPE, also skipping
1561 past typedefs, just like get_type ().
1563 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1564 except within get_target_type and get_type. */
1565 static struct type *
1566 get_target_type (struct type *type)
1570 type = TYPE_TARGET_TYPE (type);
1572 type = check_typedef (type);
1578 /* What is the default display for this variable? We assume that
1579 everything is "natural". Any exceptions? */
1580 static enum varobj_display_formats
1581 variable_default_display (struct varobj *var)
1583 return FORMAT_NATURAL;
1586 /* FIXME: The following should be generic for any pointer */
1588 vpush (struct vstack **pstack, struct varobj *var)
1592 s = (struct vstack *) xmalloc (sizeof (struct vstack));
1598 /* FIXME: The following should be generic for any pointer */
1599 static struct varobj *
1600 vpop (struct vstack **pstack)
1605 if ((*pstack)->var == NULL && (*pstack)->next == NULL)
1610 *pstack = (*pstack)->next;
1616 /* FIXME: The following should be generic for any pointer */
1618 cppush (struct cpstack **pstack, char *name)
1622 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1628 /* FIXME: The following should be generic for any pointer */
1630 cppop (struct cpstack **pstack)
1635 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1640 *pstack = (*pstack)->next;
1647 * Language-dependencies
1650 /* Common entry points */
1652 /* Get the language of variable VAR. */
1653 static enum varobj_languages
1654 variable_language (struct varobj *var)
1656 enum varobj_languages lang;
1658 switch (var->root->exp->language_defn->la_language)
1664 case language_cplus:
1675 /* Return the number of children for a given variable.
1676 The result of this function is defined by the language
1677 implementation. The number of children returned by this function
1678 is the number of children that the user will see in the variable
1681 number_of_children (struct varobj *var)
1683 return (*var->root->lang->number_of_children) (var);;
1686 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1688 name_of_variable (struct varobj *var)
1690 return (*var->root->lang->name_of_variable) (var);
1693 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1695 name_of_child (struct varobj *var, int index)
1697 return (*var->root->lang->name_of_child) (var, index);
1700 /* What is the ``struct value *'' of the root variable VAR?
1701 TYPE_CHANGED controls what to do if the type of a
1702 use_selected_frame = 1 variable changes. On input,
1703 TYPE_CHANGED = 1 means discard the old varobj, and replace
1704 it with this one. TYPE_CHANGED = 0 means leave it around.
1705 NB: In both cases, var_handle will point to the new varobj,
1706 so if you use TYPE_CHANGED = 0, you will have to stash the
1707 old varobj pointer away somewhere before calling this.
1708 On return, TYPE_CHANGED will be 1 if the type has changed, and
1710 static struct value *
1711 value_of_root (struct varobj **var_handle, int *type_changed)
1715 if (var_handle == NULL)
1720 /* This should really be an exception, since this should
1721 only get called with a root variable. */
1723 if (!is_root_p (var))
1726 if (var->root->use_selected_frame)
1728 struct varobj *tmp_var;
1729 char *old_type, *new_type;
1730 old_type = varobj_get_type (var);
1731 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1732 USE_SELECTED_FRAME);
1733 if (tmp_var == NULL)
1737 new_type = varobj_get_type (tmp_var);
1738 if (strcmp (old_type, new_type) == 0)
1740 varobj_delete (tmp_var, NULL, 0);
1748 savestring (var->obj_name, strlen (var->obj_name));
1749 varobj_delete (var, NULL, 0);
1753 tmp_var->obj_name = varobj_gen_name ();
1755 install_variable (tmp_var);
1756 *var_handle = tmp_var;
1766 return (*var->root->lang->value_of_root) (var_handle);
1769 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1770 static struct value *
1771 value_of_child (struct varobj *parent, int index)
1773 struct value *value;
1775 value = (*parent->root->lang->value_of_child) (parent, index);
1780 /* Is this variable editable? Use the variable's type to make
1781 this determination. */
1783 variable_editable (struct varobj *var)
1785 return (*var->root->lang->variable_editable) (var);
1788 /* GDB already has a command called "value_of_variable". Sigh. */
1790 my_value_of_variable (struct varobj *var)
1792 return (*var->root->lang->value_of_variable) (var);
1795 /* Return non-zero if changes in value of VAR
1796 must be detected and reported by -var-update.
1797 Return zero is -var-update should never report
1798 changes of such values. This makes sense for structures
1799 (since the changes in children values will be reported separately),
1800 or for artifical objects (like 'public' pseudo-field in C++).
1802 Return value of 0 means that gdb need not call value_fetch_lazy
1803 for the value of this variable object. */
1805 varobj_value_is_changeable_p (struct varobj *var)
1810 if (CPLUS_FAKE_CHILD (var))
1813 type = get_type (var);
1815 switch (TYPE_CODE (type))
1817 case TYPE_CODE_STRUCT:
1818 case TYPE_CODE_UNION:
1819 case TYPE_CODE_ARRAY:
1832 c_number_of_children (struct varobj *var)
1835 struct type *target;
1838 type = get_type (var);
1839 target = get_target_type (type);
1842 switch (TYPE_CODE (type))
1844 case TYPE_CODE_ARRAY:
1845 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
1846 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
1847 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1852 case TYPE_CODE_STRUCT:
1853 case TYPE_CODE_UNION:
1854 children = TYPE_NFIELDS (type);
1858 /* This is where things get compilcated. All pointers have one child.
1859 Except, of course, for struct and union ptr, which we automagically
1860 dereference for the user and function ptrs, which have no children.
1861 We also don't dereference void* as we don't know what to show.
1862 We can show char* so we allow it to be dereferenced. If you decide
1863 to test for it, please mind that a little magic is necessary to
1864 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1865 TYPE_NAME == "char" */
1867 switch (TYPE_CODE (target))
1869 case TYPE_CODE_STRUCT:
1870 case TYPE_CODE_UNION:
1871 children = TYPE_NFIELDS (target);
1874 case TYPE_CODE_FUNC:
1875 case TYPE_CODE_VOID:
1885 /* Other types have no children */
1893 c_name_of_variable (struct varobj *parent)
1895 return savestring (parent->name, strlen (parent->name));
1899 c_name_of_child (struct varobj *parent, int index)
1902 struct type *target;
1906 type = get_type (parent);
1907 target = get_target_type (type);
1909 switch (TYPE_CODE (type))
1911 case TYPE_CODE_ARRAY:
1912 name = xstrprintf ("%d", index
1913 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
1916 case TYPE_CODE_STRUCT:
1917 case TYPE_CODE_UNION:
1918 string = TYPE_FIELD_NAME (type, index);
1919 name = savestring (string, strlen (string));
1923 switch (TYPE_CODE (target))
1925 case TYPE_CODE_STRUCT:
1926 case TYPE_CODE_UNION:
1927 string = TYPE_FIELD_NAME (target, index);
1928 name = savestring (string, strlen (string));
1932 name = xstrprintf ("*%s", parent->name);
1938 /* This should not happen */
1939 name = xstrdup ("???");
1945 static struct value *
1946 c_value_of_root (struct varobj **var_handle)
1948 struct value *new_val;
1949 struct varobj *var = *var_handle;
1950 struct frame_info *fi;
1953 /* Only root variables can be updated... */
1954 if (!is_root_p (var))
1955 /* Not a root var */
1959 /* Determine whether the variable is still around. */
1960 if (var->root->valid_block == NULL)
1964 reinit_frame_cache ();
1965 fi = frame_find_by_id (var->root->frame);
1966 within_scope = fi != NULL;
1967 /* FIXME: select_frame could fail */
1970 CORE_ADDR pc = get_frame_pc (fi);
1971 if (pc < BLOCK_START (var->root->valid_block) ||
1972 pc >= BLOCK_END (var->root->valid_block))
1980 /* We need to catch errors here, because if evaluate
1981 expression fails we just want to make val->error = 1 and
1983 if (gdb_evaluate_expression (var->root->exp, &new_val))
1986 release_value (new_val);
1997 static struct value *
1998 c_value_of_child (struct varobj *parent, int index)
2000 struct value *value;
2002 struct value *indval;
2003 struct type *type, *target;
2007 type = get_type (parent);
2008 target = get_target_type (type);
2009 name = name_of_child (parent, index);
2010 temp = parent->value;
2015 switch (TYPE_CODE (type))
2017 case TYPE_CODE_ARRAY:
2018 real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2020 /* This breaks if the array lives in a (vector) register. */
2021 value = value_slice (temp, real_index, 1);
2022 temp = value_coerce_array (value);
2023 gdb_value_ind (temp, &value);
2025 indval = value_from_longest (builtin_type_int, (LONGEST) real_index);
2026 gdb_value_subscript (temp, indval, &value);
2030 case TYPE_CODE_STRUCT:
2031 case TYPE_CODE_UNION:
2032 gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
2037 switch (TYPE_CODE (target))
2039 case TYPE_CODE_STRUCT:
2040 case TYPE_CODE_UNION:
2041 gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
2046 gdb_value_ind (temp, &value);
2057 release_value (value);
2063 static struct type *
2064 c_type_of_child (struct varobj *parent, int index)
2067 char *name = name_of_child (parent, index);
2069 switch (TYPE_CODE (parent->type))
2071 case TYPE_CODE_ARRAY:
2072 type = get_target_type (parent->type);
2075 case TYPE_CODE_STRUCT:
2076 case TYPE_CODE_UNION:
2077 type = lookup_struct_elt_type (parent->type, name, 0);
2081 switch (TYPE_CODE (get_target_type (parent->type)))
2083 case TYPE_CODE_STRUCT:
2084 case TYPE_CODE_UNION:
2085 type = lookup_struct_elt_type (parent->type, name, 0);
2089 type = get_target_type (parent->type);
2095 /* This should not happen as only the above types have children */
2096 warning (_("Child of parent whose type does not allow children"));
2097 /* FIXME: Can we still go on? */
2107 c_variable_editable (struct varobj *var)
2109 switch (TYPE_CODE (get_type (var)))
2111 case TYPE_CODE_STRUCT:
2112 case TYPE_CODE_UNION:
2113 case TYPE_CODE_ARRAY:
2114 case TYPE_CODE_FUNC:
2115 case TYPE_CODE_METHOD:
2126 c_value_of_variable (struct varobj *var)
2128 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2129 it will print out its children instead of "{...}". So we need to
2130 catch that case explicitly. */
2131 struct type *type = get_type (var);
2133 /* Strip top-level references. */
2134 while (TYPE_CODE (type) == TYPE_CODE_REF)
2135 type = check_typedef (TYPE_TARGET_TYPE (type));
2137 switch (TYPE_CODE (type))
2139 case TYPE_CODE_STRUCT:
2140 case TYPE_CODE_UNION:
2141 return xstrdup ("{...}");
2144 case TYPE_CODE_ARRAY:
2147 number = xstrprintf ("[%d]", var->num_children);
2154 if (var->value == NULL)
2156 /* This can happen if we attempt to get the value of a struct
2157 member when the parent is an invalid pointer. This is an
2158 error condition, so we should tell the caller. */
2164 struct ui_file *stb = mem_fileopen ();
2165 struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
2168 gdb_assert (varobj_value_is_changeable_p (var));
2169 gdb_assert (!value_lazy (var->value));
2170 common_val_print (var->value, stb,
2171 format_code[(int) var->format], 1, 0, 0);
2172 thevalue = ui_file_xstrdup (stb, &dummy);
2173 do_cleanups (old_chain);
2184 cplus_number_of_children (struct varobj *var)
2187 int children, dont_know;
2192 if (!CPLUS_FAKE_CHILD (var))
2194 type = get_type_deref (var);
2196 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2197 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2201 cplus_class_num_children (type, kids);
2202 if (kids[v_public] != 0)
2204 if (kids[v_private] != 0)
2206 if (kids[v_protected] != 0)
2209 /* Add any baseclasses */
2210 children += TYPE_N_BASECLASSES (type);
2213 /* FIXME: save children in var */
2220 type = get_type_deref (var->parent);
2222 cplus_class_num_children (type, kids);
2223 if (strcmp (var->name, "public") == 0)
2224 children = kids[v_public];
2225 else if (strcmp (var->name, "private") == 0)
2226 children = kids[v_private];
2228 children = kids[v_protected];
2233 children = c_number_of_children (var);
2238 /* Compute # of public, private, and protected variables in this class.
2239 That means we need to descend into all baseclasses and find out
2240 how many are there, too. */
2242 cplus_class_num_children (struct type *type, int children[3])
2246 children[v_public] = 0;
2247 children[v_private] = 0;
2248 children[v_protected] = 0;
2250 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2252 /* If we have a virtual table pointer, omit it. */
2253 if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
2256 if (TYPE_FIELD_PROTECTED (type, i))
2257 children[v_protected]++;
2258 else if (TYPE_FIELD_PRIVATE (type, i))
2259 children[v_private]++;
2261 children[v_public]++;
2266 cplus_name_of_variable (struct varobj *parent)
2268 return c_name_of_variable (parent);
2272 cplus_name_of_child (struct varobj *parent, int index)
2277 if (CPLUS_FAKE_CHILD (parent))
2279 /* Looking for children of public, private, or protected. */
2280 type = get_type_deref (parent->parent);
2283 type = get_type_deref (parent);
2286 switch (TYPE_CODE (type))
2288 case TYPE_CODE_STRUCT:
2289 case TYPE_CODE_UNION:
2290 if (CPLUS_FAKE_CHILD (parent))
2292 /* The fields of the class type are ordered as they
2293 appear in the class. We are given an index for a
2294 particular access control type ("public","protected",
2295 or "private"). We must skip over fields that don't
2296 have the access control we are looking for to properly
2297 find the indexed field. */
2298 int type_index = TYPE_N_BASECLASSES (type);
2299 if (strcmp (parent->name, "private") == 0)
2303 if (TYPE_VPTR_BASETYPE (type) == type
2304 && type_index == TYPE_VPTR_FIELDNO (type))
2306 else if (TYPE_FIELD_PRIVATE (type, type_index))
2312 else if (strcmp (parent->name, "protected") == 0)
2316 if (TYPE_VPTR_BASETYPE (type) == type
2317 && type_index == TYPE_VPTR_FIELDNO (type))
2319 else if (TYPE_FIELD_PROTECTED (type, type_index))
2329 if (TYPE_VPTR_BASETYPE (type) == type
2330 && type_index == TYPE_VPTR_FIELDNO (type))
2332 else if (!TYPE_FIELD_PRIVATE (type, type_index) &&
2333 !TYPE_FIELD_PROTECTED (type, type_index))
2340 name = TYPE_FIELD_NAME (type, type_index);
2342 else if (index < TYPE_N_BASECLASSES (type))
2343 /* We are looking up the name of a base class */
2344 name = TYPE_FIELD_NAME (type, index);
2348 cplus_class_num_children(type, children);
2350 /* Everything beyond the baseclasses can
2351 only be "public", "private", or "protected"
2353 The special "fake" children are always output by varobj in
2354 this order. So if INDEX == 2, it MUST be "protected". */
2355 index -= TYPE_N_BASECLASSES (type);
2359 if (children[v_public] > 0)
2361 else if (children[v_private] > 0)
2367 if (children[v_public] > 0)
2369 if (children[v_private] > 0)
2374 else if (children[v_private] > 0)
2378 /* Must be protected */
2393 return c_name_of_child (parent, index);
2397 name = savestring (name, strlen (name));
2403 static struct value *
2404 cplus_value_of_root (struct varobj **var_handle)
2406 return c_value_of_root (var_handle);
2409 static struct value *
2410 cplus_value_of_child (struct varobj *parent, int index)
2413 struct value *value;
2415 if (CPLUS_FAKE_CHILD (parent))
2416 type = get_type_deref (parent->parent);
2418 type = get_type_deref (parent);
2422 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2423 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2425 if (CPLUS_FAKE_CHILD (parent))
2428 struct value *temp = parent->parent->value;
2433 name = name_of_child (parent, index);
2434 gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
2437 release_value (value);
2441 else if (index >= TYPE_N_BASECLASSES (type))
2443 /* public, private, or protected */
2449 if (parent->value != NULL)
2451 struct value *temp = NULL;
2453 /* No special processing for references is needed --
2454 value_cast below handles references. */
2455 if (TYPE_CODE (value_type (parent->value)) == TYPE_CODE_PTR)
2457 if (!gdb_value_ind (parent->value, &temp))
2461 temp = parent->value;
2465 value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
2466 release_value (value);
2470 /* We failed to evaluate the parent's value, so don't even
2471 bother trying to evaluate this child. */
2479 return c_value_of_child (parent, index);
2484 static struct type *
2485 cplus_type_of_child (struct varobj *parent, int index)
2487 struct type *type, *t;
2489 if (CPLUS_FAKE_CHILD (parent))
2491 /* Looking for the type of a child of public, private, or protected. */
2492 t = get_type_deref (parent->parent);
2495 t = get_type_deref (parent);
2498 switch (TYPE_CODE (t))
2500 case TYPE_CODE_STRUCT:
2501 case TYPE_CODE_UNION:
2502 if (CPLUS_FAKE_CHILD (parent))
2504 char *name = cplus_name_of_child (parent, index);
2505 type = lookup_struct_elt_type (t, name, 0);
2508 else if (index < TYPE_N_BASECLASSES (t))
2509 type = TYPE_FIELD_TYPE (t, index);
2522 return c_type_of_child (parent, index);
2528 cplus_variable_editable (struct varobj *var)
2530 if (CPLUS_FAKE_CHILD (var))
2533 return c_variable_editable (var);
2537 cplus_value_of_variable (struct varobj *var)
2540 /* If we have one of our special types, don't print out
2542 if (CPLUS_FAKE_CHILD (var))
2543 return xstrdup ("");
2545 return c_value_of_variable (var);
2551 java_number_of_children (struct varobj *var)
2553 return cplus_number_of_children (var);
2557 java_name_of_variable (struct varobj *parent)
2561 name = cplus_name_of_variable (parent);
2562 /* If the name has "-" in it, it is because we
2563 needed to escape periods in the name... */
2566 while (*p != '\000')
2577 java_name_of_child (struct varobj *parent, int index)
2581 name = cplus_name_of_child (parent, index);
2582 /* Escape any periods in the name... */
2585 while (*p != '\000')
2595 static struct value *
2596 java_value_of_root (struct varobj **var_handle)
2598 return cplus_value_of_root (var_handle);
2601 static struct value *
2602 java_value_of_child (struct varobj *parent, int index)
2604 return cplus_value_of_child (parent, index);
2607 static struct type *
2608 java_type_of_child (struct varobj *parent, int index)
2610 return cplus_type_of_child (parent, index);
2614 java_variable_editable (struct varobj *var)
2616 return cplus_variable_editable (var);
2620 java_value_of_variable (struct varobj *var)
2622 return cplus_value_of_variable (var);
2625 extern void _initialize_varobj (void);
2627 _initialize_varobj (void)
2629 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2631 varobj_table = xmalloc (sizeof_table);
2632 memset (varobj_table, 0, sizeof_table);
2634 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2636 Set varobj debugging."), _("\
2637 Show varobj debugging."), _("\
2638 When non-zero, varobj debugging is enabled."),
2641 &setlist, &showlist);