1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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"
37 /* Non-zero if we want to see trace of varobj level stuff. */
41 show_varobjdebug (struct ui_file *file, int from_tty,
42 struct cmd_list_element *c, const char *value)
44 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
47 /* String representations of gdb's format codes */
48 char *varobj_format_string[] =
49 { "natural", "binary", "decimal", "hexadecimal", "octal" };
51 /* String representations of gdb's known languages */
52 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
56 /* Every root variable has one of these structures saved in its
57 varobj. Members which must be free'd are noted. */
61 /* Alloc'd expression for this parent. */
62 struct expression *exp;
64 /* Block for which this expression is valid */
65 struct block *valid_block;
67 /* The frame for this expression */
68 struct frame_id frame;
70 /* If 1, "update" always recomputes the frame & valid block
71 using the currently selected frame. */
72 int use_selected_frame;
74 /* Language info for this variable and its children */
75 struct language_specific *lang;
77 /* The varobj for this root node. */
78 struct varobj *rootvar;
80 /* Next root variable */
81 struct varobj_root *next;
84 typedef struct varobj *varobj_p;
88 /* Every variable in the system has a structure of this type defined
89 for it. This structure holds all information necessary to manipulate
90 a particular object variable. Members which must be freed are noted. */
94 /* Alloc'd name of the variable for this object.. If this variable is a
95 child, then this name will be the child's source name.
97 /* NOTE: This is the "expression" */
100 /* The alloc'd name for this variable's object. This is here for
101 convenience when constructing this object's children. */
104 /* Index of this variable in its parent or -1 */
107 /* The type of this variable. This may NEVER be NULL. */
110 /* The value of this expression or subexpression. A NULL value
111 indicates there was an error getting this value.
112 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
113 the value is either NULL, or not lazy. */
116 /* The number of (immediate) children this variable has */
119 /* If this object is a child, this points to its immediate parent. */
120 struct varobj *parent;
122 /* Children of this object. */
123 VEC (varobj_p) *children;
125 /* Description of the root variable. Points to root variable for children. */
126 struct varobj_root *root;
128 /* The format of the output for this object */
129 enum varobj_display_formats format;
131 /* Was this variable updated via a varobj_set_value operation */
134 /* Last print value. */
141 struct cpstack *next;
144 /* A list of varobjs */
152 /* Private function prototypes */
154 /* Helper functions for the above subcommands. */
156 static int delete_variable (struct cpstack **, struct varobj *, int);
158 static void delete_variable_1 (struct cpstack **, int *,
159 struct varobj *, int, int);
161 static int install_variable (struct varobj *);
163 static void uninstall_variable (struct varobj *);
165 static struct varobj *create_child (struct varobj *, int, char *);
167 /* Utility routines */
169 static struct varobj *new_variable (void);
171 static struct varobj *new_root_variable (void);
173 static void free_variable (struct varobj *var);
175 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
177 static struct type *get_type (struct varobj *var);
179 static struct type *get_value_type (struct varobj *var);
181 static struct type *get_target_type (struct type *);
183 static enum varobj_display_formats variable_default_display (struct varobj *);
185 static void cppush (struct cpstack **pstack, char *name);
187 static char *cppop (struct cpstack **pstack);
189 static int install_new_value (struct varobj *var, struct value *value,
192 /* Language-specific routines. */
194 static enum varobj_languages variable_language (struct varobj *var);
196 static int number_of_children (struct varobj *);
198 static char *name_of_variable (struct varobj *);
200 static char *name_of_child (struct varobj *, int);
202 static struct value *value_of_root (struct varobj **var_handle, int *);
204 static struct value *value_of_child (struct varobj *parent, int index);
206 static int variable_editable (struct varobj *var);
208 static char *my_value_of_variable (struct varobj *var);
210 static char *value_get_print_value (struct value *value,
211 enum varobj_display_formats format);
213 static int varobj_value_is_changeable_p (struct varobj *var);
215 static int is_root_p (struct varobj *var);
217 /* C implementation */
219 static int c_number_of_children (struct varobj *var);
221 static char *c_name_of_variable (struct varobj *parent);
223 static char *c_name_of_child (struct varobj *parent, int index);
225 static struct value *c_value_of_root (struct varobj **var_handle);
227 static struct value *c_value_of_child (struct varobj *parent, int index);
229 static struct type *c_type_of_child (struct varobj *parent, int index);
231 static int c_variable_editable (struct varobj *var);
233 static char *c_value_of_variable (struct varobj *var);
235 /* C++ implementation */
237 static int cplus_number_of_children (struct varobj *var);
239 static void cplus_class_num_children (struct type *type, int children[3]);
241 static char *cplus_name_of_variable (struct varobj *parent);
243 static char *cplus_name_of_child (struct varobj *parent, int index);
245 static struct value *cplus_value_of_root (struct varobj **var_handle);
247 static struct value *cplus_value_of_child (struct varobj *parent, int index);
249 static struct type *cplus_type_of_child (struct varobj *parent, int index);
251 static int cplus_variable_editable (struct varobj *var);
253 static char *cplus_value_of_variable (struct varobj *var);
255 /* Java implementation */
257 static int java_number_of_children (struct varobj *var);
259 static char *java_name_of_variable (struct varobj *parent);
261 static char *java_name_of_child (struct varobj *parent, int index);
263 static struct value *java_value_of_root (struct varobj **var_handle);
265 static struct value *java_value_of_child (struct varobj *parent, int index);
267 static struct type *java_type_of_child (struct varobj *parent, int index);
269 static int java_variable_editable (struct varobj *var);
271 static char *java_value_of_variable (struct varobj *var);
273 /* The language specific vector */
275 struct language_specific
278 /* The language of this variable */
279 enum varobj_languages language;
281 /* The number of children of PARENT. */
282 int (*number_of_children) (struct varobj * parent);
284 /* The name (expression) of a root varobj. */
285 char *(*name_of_variable) (struct varobj * parent);
287 /* The name of the INDEX'th child of PARENT. */
288 char *(*name_of_child) (struct varobj * parent, int index);
290 /* The ``struct value *'' of the root variable ROOT. */
291 struct value *(*value_of_root) (struct varobj ** root_handle);
293 /* The ``struct value *'' of the INDEX'th child of PARENT. */
294 struct value *(*value_of_child) (struct varobj * parent, int index);
296 /* The type of the INDEX'th child of PARENT. */
297 struct type *(*type_of_child) (struct varobj * parent, int index);
299 /* Is VAR editable? */
300 int (*variable_editable) (struct varobj * var);
302 /* The current value of VAR. */
303 char *(*value_of_variable) (struct varobj * var);
306 /* Array of known source language routines. */
307 static struct language_specific languages[vlang_end] = {
308 /* Unknown (try treating as C */
311 c_number_of_children,
323 c_number_of_children,
335 cplus_number_of_children,
336 cplus_name_of_variable,
339 cplus_value_of_child,
341 cplus_variable_editable,
342 cplus_value_of_variable}
347 java_number_of_children,
348 java_name_of_variable,
353 java_variable_editable,
354 java_value_of_variable}
357 /* A little convenience enum for dealing with C++/Java */
360 v_public = 0, v_private, v_protected
365 /* Mappings of varobj_display_formats enums to gdb's format codes */
366 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
368 /* Header of the list of root variable objects */
369 static struct varobj_root *rootlist;
370 static int rootcount = 0; /* number of root varobjs in the list */
372 /* Prime number indicating the number of buckets in the hash table */
373 /* A prime large enough to avoid too many colisions */
374 #define VAROBJ_TABLE_SIZE 227
376 /* Pointer to the varobj hash table (built at run time) */
377 static struct vlist **varobj_table;
379 /* Is the variable X one of our "fake" children? */
380 #define CPLUS_FAKE_CHILD(x) \
381 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
384 /* API Implementation */
386 is_root_p (struct varobj *var)
388 return (var->root->rootvar == var);
391 /* Creates a varobj (not its children) */
393 /* Return the full FRAME which corresponds to the given CORE_ADDR
394 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
396 static struct frame_info *
397 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
399 struct frame_info *frame = NULL;
401 if (frame_addr == (CORE_ADDR) 0)
406 frame = get_prev_frame (frame);
409 if (get_frame_base_address (frame) == frame_addr)
415 varobj_create (char *objname,
416 char *expression, CORE_ADDR frame, enum varobj_type type)
419 struct frame_info *fi;
420 struct frame_info *old_fi = NULL;
422 struct cleanup *old_chain;
424 /* Fill out a varobj structure for the (root) variable being constructed. */
425 var = new_root_variable ();
426 old_chain = make_cleanup_free_variable (var);
428 if (expression != NULL)
431 enum varobj_languages lang;
434 /* Parse and evaluate the expression, filling in as much
435 of the variable's data as possible */
437 /* Allow creator to specify context of variable */
438 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
439 fi = deprecated_selected_frame;
441 /* FIXME: cagney/2002-11-23: This code should be doing a
442 lookup using the frame ID and not just the frame's
443 ``address''. This, of course, means an interface change.
444 However, with out that interface change ISAs, such as the
445 ia64 with its two stacks, won't work. Similar goes for the
446 case where there is a frameless function. */
447 fi = find_frame_addr_in_frame_chain (frame);
449 /* frame = -2 means always use selected frame */
450 if (type == USE_SELECTED_FRAME)
451 var->root->use_selected_frame = 1;
455 block = get_frame_block (fi, 0);
458 innermost_block = NULL;
459 /* Wrap the call to parse expression, so we can
460 return a sensible error. */
461 if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
466 /* Don't allow variables to be created for types. */
467 if (var->root->exp->elts[0].opcode == OP_TYPE)
469 do_cleanups (old_chain);
470 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
471 " as an expression.\n");
475 var->format = variable_default_display (var);
476 var->root->valid_block = innermost_block;
477 var->name = savestring (expression, strlen (expression));
479 /* When the frame is different from the current frame,
480 we must select the appropriate frame before parsing
481 the expression, otherwise the value will not be current.
482 Since select_frame is so benign, just call it for all cases. */
485 var->root->frame = get_frame_id (fi);
486 old_fi = deprecated_selected_frame;
490 /* We definitively need to catch errors here.
491 If evaluate_expression succeeds we got the value we wanted.
492 But if it fails, we still go on with a call to evaluate_type() */
493 if (!gdb_evaluate_expression (var->root->exp, &value))
494 /* Error getting the value. Try to at least get the
496 value = evaluate_type (var->root->exp);
498 var->type = value_type (value);
499 install_new_value (var, value, 1 /* Initial assignment */);
501 /* Set language info */
502 lang = variable_language (var);
503 var->root->lang = &languages[lang];
505 /* Set ourselves as our root */
506 var->root->rootvar = var;
508 /* Reset the selected frame */
510 select_frame (old_fi);
513 /* If the variable object name is null, that means this
514 is a temporary variable, so don't install it. */
516 if ((var != NULL) && (objname != NULL))
518 var->obj_name = savestring (objname, strlen (objname));
520 /* If a varobj name is duplicated, the install will fail so
522 if (!install_variable (var))
524 do_cleanups (old_chain);
529 discard_cleanups (old_chain);
533 /* Generates an unique name that can be used for a varobj */
536 varobj_gen_name (void)
541 /* generate a name for this object */
543 obj_name = xstrprintf ("var%d", id);
548 /* Given an "objname", returns the pointer to the corresponding varobj
549 or NULL if not found */
552 varobj_get_handle (char *objname)
556 unsigned int index = 0;
559 for (chp = objname; *chp; chp++)
561 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
564 cv = *(varobj_table + index);
565 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
569 error (_("Variable object not found"));
574 /* Given the handle, return the name of the object */
577 varobj_get_objname (struct varobj *var)
579 return var->obj_name;
582 /* Given the handle, return the expression represented by the object */
585 varobj_get_expression (struct varobj *var)
587 return name_of_variable (var);
590 /* Deletes a varobj and all its children if only_children == 0,
591 otherwise deletes only the children; returns a malloc'ed list of all the
592 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
595 varobj_delete (struct varobj *var, char ***dellist, int only_children)
599 struct cpstack *result = NULL;
602 /* Initialize a stack for temporary results */
603 cppush (&result, NULL);
606 /* Delete only the variable children */
607 delcount = delete_variable (&result, var, 1 /* only the children */ );
609 /* Delete the variable and all its children */
610 delcount = delete_variable (&result, var, 0 /* parent+children */ );
612 /* We may have been asked to return a list of what has been deleted */
615 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
619 *cp = cppop (&result);
620 while ((*cp != NULL) && (mycount > 0))
624 *cp = cppop (&result);
627 if (mycount || (*cp != NULL))
628 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
635 /* Set/Get variable object display format */
637 enum varobj_display_formats
638 varobj_set_display_format (struct varobj *var,
639 enum varobj_display_formats format)
646 case FORMAT_HEXADECIMAL:
648 var->format = format;
652 var->format = variable_default_display (var);
658 enum varobj_display_formats
659 varobj_get_display_format (struct varobj *var)
665 varobj_get_num_children (struct varobj *var)
667 if (var->num_children == -1)
668 var->num_children = number_of_children (var);
670 return var->num_children;
673 /* Creates a list of the immediate children of a variable object;
674 the return code is the number of such children or -1 on error */
677 varobj_list_children (struct varobj *var, struct varobj ***childlist)
679 struct varobj *child;
683 /* sanity check: have we been passed a pointer? */
684 if (childlist == NULL)
689 if (var->num_children == -1)
690 var->num_children = number_of_children (var);
692 /* If that failed, give up. */
693 if (var->num_children == -1)
696 /* If we're called when the list of children is not yet initialized,
697 allocate enough elements in it. */
698 while (VEC_length (varobj_p, var->children) < var->num_children)
699 VEC_safe_push (varobj_p, var->children, NULL);
701 /* List of children */
702 *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
704 for (i = 0; i < var->num_children; i++)
708 /* Mark as the end in case we bail out */
709 *((*childlist) + i) = NULL;
711 existing = VEC_index (varobj_p, var->children, i);
713 if (existing == NULL)
715 /* Either it's the first call to varobj_list_children for
716 this variable object, and the child was never created,
717 or it was explicitly deleted by the client. */
718 name = name_of_child (var, i);
719 existing = create_child (var, i, name);
720 VEC_replace (varobj_p, var->children, i, existing);
723 *((*childlist) + i) = existing;
726 /* End of list is marked by a NULL pointer */
727 *((*childlist) + i) = NULL;
729 return var->num_children;
732 /* Obtain the type of an object Variable as a string similar to the one gdb
733 prints on the console */
736 varobj_get_type (struct varobj *var)
739 struct cleanup *old_chain;
744 /* For the "fake" variables, do not return a type. (It's type is
746 if (CPLUS_FAKE_CHILD (var))
749 stb = mem_fileopen ();
750 old_chain = make_cleanup_ui_file_delete (stb);
752 /* To print the type, we simply create a zero ``struct value *'' and
753 cast it to our type. We then typeprint this variable. */
754 val = value_zero (var->type, not_lval);
755 type_print (value_type (val), "", stb, -1);
757 thetype = ui_file_xstrdup (stb, &length);
758 do_cleanups (old_chain);
762 /* Obtain the type of an object variable. */
765 varobj_get_gdb_type (struct varobj *var)
770 enum varobj_languages
771 varobj_get_language (struct varobj *var)
773 return variable_language (var);
777 varobj_get_attributes (struct varobj *var)
781 if (variable_editable (var))
782 /* FIXME: define masks for attributes */
783 attributes |= 0x00000001; /* Editable */
789 varobj_get_value (struct varobj *var)
791 return my_value_of_variable (var);
794 /* Set the value of an object variable (if it is editable) to the
795 value of the given expression */
796 /* Note: Invokes functions that can call error() */
799 varobj_set_value (struct varobj *var, char *expression)
805 /* The argument "expression" contains the variable's new value.
806 We need to first construct a legal expression for this -- ugh! */
807 /* Does this cover all the bases? */
808 struct expression *exp;
810 int saved_input_radix = input_radix;
812 if (var->value != NULL && variable_editable (var))
814 char *s = expression;
817 input_radix = 10; /* ALWAYS reset to decimal temporarily */
818 exp = parse_exp_1 (&s, 0, 0);
819 if (!gdb_evaluate_expression (exp, &value))
821 /* We cannot proceed without a valid expression. */
826 /* All types that are editable must also be changeable. */
827 gdb_assert (varobj_value_is_changeable_p (var));
829 /* The value of a changeable variable object must not be lazy. */
830 gdb_assert (!value_lazy (var->value));
832 /* Need to coerce the input. We want to check if the
833 value of the variable object will be different
834 after assignment, and the first thing value_assign
835 does is coerce the input.
836 For example, if we are assigning an array to a pointer variable we
837 should compare the pointer with the the array's address, not with the
839 value = coerce_array (value);
841 /* The new value may be lazy. gdb_value_assign, or
842 rather value_contents, will take care of this.
843 If fetching of the new value will fail, gdb_value_assign
844 with catch the exception. */
845 if (!gdb_value_assign (var->value, value, &val))
848 /* If the value has changed, record it, so that next -var-update can
849 report this change. If a variable had a value of '1', we've set it
850 to '333' and then set again to '1', when -var-update will report this
851 variable as changed -- because the first assignment has set the
852 'updated' flag. There's no need to optimize that, because return value
853 of -var-update should be considered an approximation. */
854 var->updated = install_new_value (var, val, 0 /* Compare values. */);
855 input_radix = saved_input_radix;
862 /* Returns a malloc'ed list with all root variable objects */
864 varobj_list (struct varobj ***varlist)
867 struct varobj_root *croot;
868 int mycount = rootcount;
870 /* Alloc (rootcount + 1) entries for the result */
871 *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
875 while ((croot != NULL) && (mycount > 0))
877 *cv = croot->rootvar;
882 /* Mark the end of the list */
885 if (mycount || (croot != NULL))
887 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
893 /* Assign a new value to a variable object. If INITIAL is non-zero,
894 this is the first assignement after the variable object was just
895 created, or changed type. In that case, just assign the value
897 Otherwise, assign the value and if type_changeable returns non-zero,
898 find if the new value is different from the current value.
899 Return 1 if so, and 0 if the values are equal.
901 The VALUE parameter should not be released -- the function will
902 take care of releasing it when needed. */
904 install_new_value (struct varobj *var, struct value *value, int initial)
910 /* We need to know the varobj's type to decide if the value should
911 be fetched or not. C++ fake children (public/protected/private) don't have
913 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
914 changeable = varobj_value_is_changeable_p (var);
915 need_to_fetch = changeable;
917 /* We are not interested in the address of references, and given
918 that in C++ a reference is not rebindable, it cannot
919 meaningfully change. So, get hold of the real value. */
922 value = coerce_ref (value);
923 release_value (value);
926 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
927 /* For unions, we need to fetch the value implicitly because
928 of implementation of union member fetch. When gdb
929 creates a value for a field and the value of the enclosing
930 structure is not lazy, it immediately copies the necessary
931 bytes from the enclosing values. If the enclosing value is
932 lazy, the call to value_fetch_lazy on the field will read
933 the data from memory. For unions, that means we'll read the
934 same memory more than once, which is not desirable. So
938 /* The new value might be lazy. If the type is changeable,
939 that is we'll be comparing values of this type, fetch the
940 value now. Otherwise, on the next update the old value
941 will be lazy, which means we've lost that old value. */
942 if (need_to_fetch && value && value_lazy (value))
944 if (!gdb_value_fetch_lazy (value))
946 /* Set the value to NULL, so that for the next -var-update,
947 we don't try to compare the new value with this value,
948 that we couldn't even read. */
953 /* If the type is changeable, compare the old and the new values.
954 If this is the initial assignment, we don't have any old value
956 if (initial && changeable)
957 var->print_value = value_get_print_value (value, var->format);
960 /* If the value of the varobj was changed by -var-set-value, then the
961 value in the varobj and in the target is the same. However, that value
962 is different from the value that the varobj had after the previous
963 -var-update. So need to the varobj as changed. */
966 xfree (var->print_value);
967 var->print_value = value_get_print_value (value, var->format);
972 /* Try to compare the values. That requires that both
973 values are non-lazy. */
975 /* Quick comparison of NULL values. */
976 if (var->value == NULL && value == NULL)
979 else if (var->value == NULL || value == NULL)
981 xfree (var->print_value);
982 var->print_value = value_get_print_value (value, var->format);
988 gdb_assert (!value_lazy (var->value));
989 gdb_assert (!value_lazy (value));
990 print_value = value_get_print_value (value, var->format);
992 gdb_assert (var->print_value != NULL && print_value != NULL);
993 if (strcmp (var->print_value, print_value) != 0)
995 xfree (var->print_value);
996 var->print_value = print_value;
1000 xfree (print_value);
1005 /* We must always keep the new value, since children depend on it. */
1006 if (var->value != NULL)
1007 value_free (var->value);
1011 gdb_assert (!var->value || value_type (var->value));
1016 /* Update the values for a variable and its children. This is a
1017 two-pronged attack. First, re-parse the value for the root's
1018 expression to see if it's changed. Then go all the way
1019 through its children, reconstructing them and noting if they've
1022 -1 if there was an error updating the varobj
1023 -2 if the type changed
1024 Otherwise it is the number of children + parent changed
1026 Only root variables can be updated...
1028 NOTE: This function may delete the caller's varobj. If it
1029 returns -2, then it has done this and VARP will be modified
1030 to point to the new varobj. */
1033 varobj_update (struct varobj **varp, struct varobj ***changelist)
1042 struct varobj **templist = NULL;
1044 VEC (varobj_p) *stack = NULL;
1045 VEC (varobj_p) *result = NULL;
1046 struct frame_id old_fid;
1047 struct frame_info *fi;
1049 /* sanity check: have we been passed a pointer? */
1050 if (changelist == NULL)
1053 /* Only root variables can be updated... */
1054 if (!is_root_p (*varp))
1055 /* Not a root var */
1058 /* Save the selected stack frame, since we will need to change it
1059 in order to evaluate expressions. */
1060 old_fid = get_frame_id (deprecated_selected_frame);
1062 /* Update the root variable. value_of_root can return NULL
1063 if the variable is no longer around, i.e. we stepped out of
1064 the frame in which a local existed. We are letting the
1065 value_of_root variable dispose of the varobj if the type
1068 new = value_of_root (varp, &type_changed);
1070 /* Restore selected frame */
1071 fi = frame_find_by_id (old_fid);
1075 /* If this is a "use_selected_frame" varobj, and its type has changed,
1076 them note that it's changed. */
1078 VEC_safe_push (varobj_p, result, *varp);
1080 if (install_new_value ((*varp), new, type_changed))
1082 /* If type_changed is 1, install_new_value will never return
1083 non-zero, so we'll never report the same variable twice. */
1084 gdb_assert (!type_changed);
1085 VEC_safe_push (varobj_p, result, *varp);
1090 /* This means the varobj itself is out of scope.
1092 VEC_free (varobj_p, result);
1096 VEC_safe_push (varobj_p, stack, *varp);
1098 /* Walk through the children, reconstructing them all. */
1099 while (!VEC_empty (varobj_p, stack))
1101 v = VEC_pop (varobj_p, stack);
1103 /* Push any children. Use reverse order so that the first
1104 child is popped from the work stack first, and so
1105 will be added to result first. This does not
1106 affect correctness, just "nicer". */
1107 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
1109 varobj_p c = VEC_index (varobj_p, v->children, i);
1110 /* Child may be NULL if explicitly deleted by -var-delete. */
1112 VEC_safe_push (varobj_p, stack, c);
1115 /* Update this variable, unless it's a root, which is already
1119 new = value_of_child (v->parent, v->index);
1120 if (install_new_value (v, new, 0 /* type not changed */))
1122 /* Note that it's changed */
1123 VEC_safe_push (varobj_p, result, v);
1129 /* Alloc (changed + 1) list entries */
1130 changed = VEC_length (varobj_p, result);
1131 *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1134 for (i = 0; i < changed; ++i)
1136 *cv = VEC_index (varobj_p, result, i);
1137 gdb_assert (*cv != NULL);
1149 /* Helper functions */
1152 * Variable object construction/destruction
1156 delete_variable (struct cpstack **resultp, struct varobj *var,
1157 int only_children_p)
1161 delete_variable_1 (resultp, &delcount, var,
1162 only_children_p, 1 /* remove_from_parent_p */ );
1167 /* Delete the variable object VAR and its children */
1168 /* IMPORTANT NOTE: If we delete a variable which is a child
1169 and the parent is not removed we dump core. It must be always
1170 initially called with remove_from_parent_p set */
1172 delete_variable_1 (struct cpstack **resultp, int *delcountp,
1173 struct varobj *var, int only_children_p,
1174 int remove_from_parent_p)
1178 /* Delete any children of this variable, too. */
1179 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
1181 varobj_p child = VEC_index (varobj_p, var->children, i);
1182 if (!remove_from_parent_p)
1183 child->parent = NULL;
1184 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
1186 VEC_free (varobj_p, var->children);
1188 /* if we were called to delete only the children we are done here */
1189 if (only_children_p)
1192 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1193 /* If the name is null, this is a temporary variable, that has not
1194 yet been installed, don't report it, it belongs to the caller... */
1195 if (var->obj_name != NULL)
1197 cppush (resultp, xstrdup (var->obj_name));
1198 *delcountp = *delcountp + 1;
1201 /* If this variable has a parent, remove it from its parent's list */
1202 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1203 (as indicated by remove_from_parent_p) we don't bother doing an
1204 expensive list search to find the element to remove when we are
1205 discarding the list afterwards */
1206 if ((remove_from_parent_p) && (var->parent != NULL))
1208 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
1211 if (var->obj_name != NULL)
1212 uninstall_variable (var);
1214 /* Free memory associated with this variable */
1215 free_variable (var);
1218 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1220 install_variable (struct varobj *var)
1223 struct vlist *newvl;
1225 unsigned int index = 0;
1228 for (chp = var->obj_name; *chp; chp++)
1230 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1233 cv = *(varobj_table + index);
1234 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1238 error (_("Duplicate variable object name"));
1240 /* Add varobj to hash table */
1241 newvl = xmalloc (sizeof (struct vlist));
1242 newvl->next = *(varobj_table + index);
1244 *(varobj_table + index) = newvl;
1246 /* If root, add varobj to root list */
1247 if (is_root_p (var))
1249 /* Add to list of root variables */
1250 if (rootlist == NULL)
1251 var->root->next = NULL;
1253 var->root->next = rootlist;
1254 rootlist = var->root;
1261 /* Unistall the object VAR. */
1263 uninstall_variable (struct varobj *var)
1267 struct varobj_root *cr;
1268 struct varobj_root *prer;
1270 unsigned int index = 0;
1273 /* Remove varobj from hash table */
1274 for (chp = var->obj_name; *chp; chp++)
1276 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1279 cv = *(varobj_table + index);
1281 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1288 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1293 ("Assertion failed: Could not find variable object \"%s\" to delete",
1299 *(varobj_table + index) = cv->next;
1301 prev->next = cv->next;
1305 /* If root, remove varobj from root list */
1306 if (is_root_p (var))
1308 /* Remove from list of root variables */
1309 if (rootlist == var->root)
1310 rootlist = var->root->next;
1315 while ((cr != NULL) && (cr->rootvar != var))
1323 ("Assertion failed: Could not find varobj \"%s\" in root list",
1330 prer->next = cr->next;
1337 /* Create and install a child of the parent of the given name */
1338 static struct varobj *
1339 create_child (struct varobj *parent, int index, char *name)
1341 struct varobj *child;
1343 struct value *value;
1345 child = new_variable ();
1347 /* name is allocated by name_of_child */
1349 child->index = index;
1350 value = value_of_child (parent, index);
1351 child->parent = parent;
1352 child->root = parent->root;
1353 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
1354 child->obj_name = childs_name;
1355 install_variable (child);
1357 /* Compute the type of the child. Must do this before
1358 calling install_new_value. */
1360 /* If the child had no evaluation errors, var->value
1361 will be non-NULL and contain a valid type. */
1362 child->type = value_type (value);
1364 /* Otherwise, we must compute the type. */
1365 child->type = (*child->root->lang->type_of_child) (child->parent,
1367 install_new_value (child, value, 1);
1374 * Miscellaneous utility functions.
1377 /* Allocate memory and initialize a new variable */
1378 static struct varobj *
1383 var = (struct varobj *) xmalloc (sizeof (struct varobj));
1385 var->obj_name = NULL;
1389 var->num_children = -1;
1391 var->children = NULL;
1395 var->print_value = NULL;
1400 /* Allocate memory and initialize a new root variable */
1401 static struct varobj *
1402 new_root_variable (void)
1404 struct varobj *var = new_variable ();
1405 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1406 var->root->lang = NULL;
1407 var->root->exp = NULL;
1408 var->root->valid_block = NULL;
1409 var->root->frame = null_frame_id;
1410 var->root->use_selected_frame = 0;
1411 var->root->rootvar = NULL;
1416 /* Free any allocated memory associated with VAR. */
1418 free_variable (struct varobj *var)
1420 /* Free the expression if this is a root variable. */
1421 if (is_root_p (var))
1423 free_current_contents (&var->root->exp);
1428 xfree (var->obj_name);
1429 xfree (var->print_value);
1434 do_free_variable_cleanup (void *var)
1436 free_variable (var);
1439 static struct cleanup *
1440 make_cleanup_free_variable (struct varobj *var)
1442 return make_cleanup (do_free_variable_cleanup, var);
1445 /* This returns the type of the variable. It also skips past typedefs
1446 to return the real type of the variable.
1448 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1449 except within get_target_type and get_type. */
1450 static struct type *
1451 get_type (struct varobj *var)
1457 type = check_typedef (type);
1462 /* Return the type of the value that's stored in VAR,
1463 or that would have being stored there if the
1464 value were accessible.
1466 This differs from VAR->type in that VAR->type is always
1467 the true type of the expession in the source language.
1468 The return value of this function is the type we're
1469 actually storing in varobj, and using for displaying
1470 the values and for comparing previous and new values.
1472 For example, top-level references are always stripped. */
1473 static struct type *
1474 get_value_type (struct varobj *var)
1479 type = value_type (var->value);
1483 type = check_typedef (type);
1485 if (TYPE_CODE (type) == TYPE_CODE_REF)
1486 type = get_target_type (type);
1488 type = check_typedef (type);
1493 /* This returns the target type (or NULL) of TYPE, also skipping
1494 past typedefs, just like get_type ().
1496 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1497 except within get_target_type and get_type. */
1498 static struct type *
1499 get_target_type (struct type *type)
1503 type = TYPE_TARGET_TYPE (type);
1505 type = check_typedef (type);
1511 /* What is the default display for this variable? We assume that
1512 everything is "natural". Any exceptions? */
1513 static enum varobj_display_formats
1514 variable_default_display (struct varobj *var)
1516 return FORMAT_NATURAL;
1519 /* FIXME: The following should be generic for any pointer */
1521 cppush (struct cpstack **pstack, char *name)
1525 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1531 /* FIXME: The following should be generic for any pointer */
1533 cppop (struct cpstack **pstack)
1538 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1543 *pstack = (*pstack)->next;
1550 * Language-dependencies
1553 /* Common entry points */
1555 /* Get the language of variable VAR. */
1556 static enum varobj_languages
1557 variable_language (struct varobj *var)
1559 enum varobj_languages lang;
1561 switch (var->root->exp->language_defn->la_language)
1567 case language_cplus:
1578 /* Return the number of children for a given variable.
1579 The result of this function is defined by the language
1580 implementation. The number of children returned by this function
1581 is the number of children that the user will see in the variable
1584 number_of_children (struct varobj *var)
1586 return (*var->root->lang->number_of_children) (var);;
1589 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1591 name_of_variable (struct varobj *var)
1593 return (*var->root->lang->name_of_variable) (var);
1596 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1598 name_of_child (struct varobj *var, int index)
1600 return (*var->root->lang->name_of_child) (var, index);
1603 /* What is the ``struct value *'' of the root variable VAR?
1604 TYPE_CHANGED controls what to do if the type of a
1605 use_selected_frame = 1 variable changes. On input,
1606 TYPE_CHANGED = 1 means discard the old varobj, and replace
1607 it with this one. TYPE_CHANGED = 0 means leave it around.
1608 NB: In both cases, var_handle will point to the new varobj,
1609 so if you use TYPE_CHANGED = 0, you will have to stash the
1610 old varobj pointer away somewhere before calling this.
1611 On return, TYPE_CHANGED will be 1 if the type has changed, and
1613 static struct value *
1614 value_of_root (struct varobj **var_handle, int *type_changed)
1618 if (var_handle == NULL)
1623 /* This should really be an exception, since this should
1624 only get called with a root variable. */
1626 if (!is_root_p (var))
1629 if (var->root->use_selected_frame)
1631 struct varobj *tmp_var;
1632 char *old_type, *new_type;
1633 old_type = varobj_get_type (var);
1634 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1635 USE_SELECTED_FRAME);
1636 if (tmp_var == NULL)
1640 new_type = varobj_get_type (tmp_var);
1641 if (strcmp (old_type, new_type) == 0)
1643 varobj_delete (tmp_var, NULL, 0);
1651 savestring (var->obj_name, strlen (var->obj_name));
1652 varobj_delete (var, NULL, 0);
1656 tmp_var->obj_name = varobj_gen_name ();
1658 install_variable (tmp_var);
1659 *var_handle = tmp_var;
1669 return (*var->root->lang->value_of_root) (var_handle);
1672 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1673 static struct value *
1674 value_of_child (struct varobj *parent, int index)
1676 struct value *value;
1678 value = (*parent->root->lang->value_of_child) (parent, index);
1683 /* Is this variable editable? Use the variable's type to make
1684 this determination. */
1686 variable_editable (struct varobj *var)
1688 return (*var->root->lang->variable_editable) (var);
1691 /* GDB already has a command called "value_of_variable". Sigh. */
1693 my_value_of_variable (struct varobj *var)
1695 return (*var->root->lang->value_of_variable) (var);
1699 value_get_print_value (struct value *value, enum varobj_display_formats format)
1702 struct ui_file *stb;
1703 struct cleanup *old_chain;
1709 stb = mem_fileopen ();
1710 old_chain = make_cleanup_ui_file_delete (stb);
1712 common_val_print (value, stb, format_code[(int) format], 1, 0, 0);
1713 thevalue = ui_file_xstrdup (stb, &dummy);
1715 do_cleanups (old_chain);
1719 /* Return non-zero if changes in value of VAR
1720 must be detected and reported by -var-update.
1721 Return zero is -var-update should never report
1722 changes of such values. This makes sense for structures
1723 (since the changes in children values will be reported separately),
1724 or for artifical objects (like 'public' pseudo-field in C++).
1726 Return value of 0 means that gdb need not call value_fetch_lazy
1727 for the value of this variable object. */
1729 varobj_value_is_changeable_p (struct varobj *var)
1734 if (CPLUS_FAKE_CHILD (var))
1737 type = get_value_type (var);
1739 switch (TYPE_CODE (type))
1741 case TYPE_CODE_STRUCT:
1742 case TYPE_CODE_UNION:
1743 case TYPE_CODE_ARRAY:
1754 /* Given the value and the type of a variable object,
1755 adjust the value and type to those necessary
1756 for getting children of the variable object.
1757 This includes dereferencing top-level references
1758 to all types and dereferencing pointers to
1761 Both TYPE and *TYPE should be non-null. VALUE
1762 can be null if we want to only translate type.
1763 *VALUE can be null as well -- if the parent
1764 value is not known. */
1766 adjust_value_for_child_access (struct value **value,
1769 gdb_assert (type && *type);
1771 *type = check_typedef (*type);
1773 /* The type of value stored in varobj, that is passed
1774 to us, is already supposed to be
1775 reference-stripped. */
1777 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
1779 /* Pointers to structures are treated just like
1780 structures when accessing children. Don't
1781 dererences pointers to other types. */
1782 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
1784 struct type *target_type = get_target_type (*type);
1785 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
1786 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
1788 if (value && *value)
1789 gdb_value_ind (*value, value);
1790 *type = target_type;
1794 /* The 'get_target_type' function calls check_typedef on
1795 result, so we can immediately check type code. No
1796 need to call check_typedef here. */
1801 c_number_of_children (struct varobj *var)
1803 struct type *type = get_value_type (var);
1805 struct type *target;
1807 adjust_value_for_child_access (NULL, &type);
1808 target = get_target_type (type);
1810 switch (TYPE_CODE (type))
1812 case TYPE_CODE_ARRAY:
1813 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
1814 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
1815 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1817 /* If we don't know how many elements there are, don't display
1822 case TYPE_CODE_STRUCT:
1823 case TYPE_CODE_UNION:
1824 children = TYPE_NFIELDS (type);
1828 /* The type here is a pointer to non-struct. Typically, pointers
1829 have one child, except for function ptrs, which have no children,
1830 and except for void*, as we don't know what to show.
1832 We can show char* so we allow it to be dereferenced. If you decide
1833 to test for it, please mind that a little magic is necessary to
1834 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1835 TYPE_NAME == "char" */
1836 if (TYPE_CODE (target) == TYPE_CODE_FUNC
1837 || TYPE_CODE (target) == TYPE_CODE_VOID)
1844 /* Other types have no children */
1852 c_name_of_variable (struct varobj *parent)
1854 return savestring (parent->name, strlen (parent->name));
1857 /* Return the value of element TYPE_INDEX of a structure
1858 value VALUE. VALUE's type should be a structure,
1859 or union, or a typedef to struct/union.
1861 Returns NULL if getting the value fails. Never throws. */
1862 static struct value *
1863 value_struct_element_index (struct value *value, int type_index)
1865 struct value *result = NULL;
1866 volatile struct gdb_exception e;
1868 struct type *type = value_type (value);
1869 type = check_typedef (type);
1871 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1872 || TYPE_CODE (type) == TYPE_CODE_UNION);
1874 TRY_CATCH (e, RETURN_MASK_ERROR)
1876 if (TYPE_FIELD_STATIC (type, type_index))
1877 result = value_static_field (type, type_index);
1879 result = value_primitive_field (value, 0, type_index, type);
1891 /* Obtain the information about child INDEX of the variable
1893 If CNAME is not null, sets *CNAME to the name of the child relative
1895 If CVALUE is not null, sets *CVALUE to the value of the child.
1896 If CTYPE is not null, sets *CTYPE to the type of the child.
1898 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
1899 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
1902 c_describe_child (struct varobj *parent, int index,
1903 char **cname, struct value **cvalue, struct type **ctype)
1905 struct value *value = parent->value;
1906 struct type *type = get_value_type (parent);
1915 adjust_value_for_child_access (&value, &type);
1917 switch (TYPE_CODE (type))
1919 case TYPE_CODE_ARRAY:
1921 *cname = xstrprintf ("%d", index
1922 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
1924 if (cvalue && value)
1926 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
1927 struct value *indval =
1928 value_from_longest (builtin_type_int, (LONGEST) real_index);
1929 gdb_value_subscript (value, indval, cvalue);
1933 *ctype = get_target_type (type);
1937 case TYPE_CODE_STRUCT:
1938 case TYPE_CODE_UNION:
1941 char *string = TYPE_FIELD_NAME (type, index);
1942 *cname = savestring (string, strlen (string));
1945 if (cvalue && value)
1947 /* For C, varobj index is the same as type index. */
1948 *cvalue = value_struct_element_index (value, index);
1952 *ctype = TYPE_FIELD_TYPE (type, index);
1958 *cname = xstrprintf ("*%s", parent->name);
1960 if (cvalue && value)
1961 gdb_value_ind (value, cvalue);
1963 /* Don't use get_target_type because it calls
1964 check_typedef and here, we want to show the true
1965 declared type of the variable. */
1967 *ctype = TYPE_TARGET_TYPE (type);
1972 /* This should not happen */
1974 *cname = xstrdup ("???");
1975 /* Don't set value and type, we don't know then. */
1980 c_name_of_child (struct varobj *parent, int index)
1983 c_describe_child (parent, index, &name, NULL, NULL);
1987 static struct value *
1988 c_value_of_root (struct varobj **var_handle)
1990 struct value *new_val = NULL;
1991 struct varobj *var = *var_handle;
1992 struct frame_info *fi;
1995 /* Only root variables can be updated... */
1996 if (!is_root_p (var))
1997 /* Not a root var */
2001 /* Determine whether the variable is still around. */
2002 if (var->root->valid_block == NULL || var->root->use_selected_frame)
2006 fi = frame_find_by_id (var->root->frame);
2007 within_scope = fi != NULL;
2008 /* FIXME: select_frame could fail */
2011 CORE_ADDR pc = get_frame_pc (fi);
2012 if (pc < BLOCK_START (var->root->valid_block) ||
2013 pc >= BLOCK_END (var->root->valid_block))
2022 /* We need to catch errors here, because if evaluate
2023 expression fails we want to just return NULL. */
2024 gdb_evaluate_expression (var->root->exp, &new_val);
2031 static struct value *
2032 c_value_of_child (struct varobj *parent, int index)
2034 struct value *value = NULL;
2035 c_describe_child (parent, index, NULL, &value, NULL);
2040 static struct type *
2041 c_type_of_child (struct varobj *parent, int index)
2043 struct type *type = NULL;
2044 c_describe_child (parent, index, NULL, NULL, &type);
2049 c_variable_editable (struct varobj *var)
2051 switch (TYPE_CODE (get_value_type (var)))
2053 case TYPE_CODE_STRUCT:
2054 case TYPE_CODE_UNION:
2055 case TYPE_CODE_ARRAY:
2056 case TYPE_CODE_FUNC:
2057 case TYPE_CODE_METHOD:
2068 c_value_of_variable (struct varobj *var)
2070 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2071 it will print out its children instead of "{...}". So we need to
2072 catch that case explicitly. */
2073 struct type *type = get_type (var);
2075 /* Strip top-level references. */
2076 while (TYPE_CODE (type) == TYPE_CODE_REF)
2077 type = check_typedef (TYPE_TARGET_TYPE (type));
2079 switch (TYPE_CODE (type))
2081 case TYPE_CODE_STRUCT:
2082 case TYPE_CODE_UNION:
2083 return xstrdup ("{...}");
2086 case TYPE_CODE_ARRAY:
2089 number = xstrprintf ("[%d]", var->num_children);
2096 if (var->value == NULL)
2098 /* This can happen if we attempt to get the value of a struct
2099 member when the parent is an invalid pointer. This is an
2100 error condition, so we should tell the caller. */
2105 gdb_assert (varobj_value_is_changeable_p (var));
2106 gdb_assert (!value_lazy (var->value));
2107 return value_get_print_value (var->value, var->format);
2117 cplus_number_of_children (struct varobj *var)
2120 int children, dont_know;
2125 if (!CPLUS_FAKE_CHILD (var))
2127 type = get_value_type (var);
2128 adjust_value_for_child_access (NULL, &type);
2130 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2131 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2135 cplus_class_num_children (type, kids);
2136 if (kids[v_public] != 0)
2138 if (kids[v_private] != 0)
2140 if (kids[v_protected] != 0)
2143 /* Add any baseclasses */
2144 children += TYPE_N_BASECLASSES (type);
2147 /* FIXME: save children in var */
2154 type = get_value_type (var->parent);
2155 adjust_value_for_child_access (NULL, &type);
2157 cplus_class_num_children (type, kids);
2158 if (strcmp (var->name, "public") == 0)
2159 children = kids[v_public];
2160 else if (strcmp (var->name, "private") == 0)
2161 children = kids[v_private];
2163 children = kids[v_protected];
2168 children = c_number_of_children (var);
2173 /* Compute # of public, private, and protected variables in this class.
2174 That means we need to descend into all baseclasses and find out
2175 how many are there, too. */
2177 cplus_class_num_children (struct type *type, int children[3])
2181 children[v_public] = 0;
2182 children[v_private] = 0;
2183 children[v_protected] = 0;
2185 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2187 /* If we have a virtual table pointer, omit it. */
2188 if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
2191 if (TYPE_FIELD_PROTECTED (type, i))
2192 children[v_protected]++;
2193 else if (TYPE_FIELD_PRIVATE (type, i))
2194 children[v_private]++;
2196 children[v_public]++;
2201 cplus_name_of_variable (struct varobj *parent)
2203 return c_name_of_variable (parent);
2206 enum accessibility { private_field, protected_field, public_field };
2208 /* Check if field INDEX of TYPE has the specified accessibility.
2209 Return 0 if so and 1 otherwise. */
2211 match_accessibility (struct type *type, int index, enum accessibility acc)
2213 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2215 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2217 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2218 && !TYPE_FIELD_PROTECTED (type, index))
2225 cplus_describe_child (struct varobj *parent, int index,
2226 char **cname, struct value **cvalue, struct type **ctype)
2229 struct value *value;
2240 if (CPLUS_FAKE_CHILD (parent))
2242 value = parent->parent->value;
2243 type = get_value_type (parent->parent);
2247 value = parent->value;
2248 type = get_value_type (parent);
2251 adjust_value_for_child_access (&value, &type);
2253 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2254 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
2256 if (CPLUS_FAKE_CHILD (parent))
2258 /* The fields of the class type are ordered as they
2259 appear in the class. We are given an index for a
2260 particular access control type ("public","protected",
2261 or "private"). We must skip over fields that don't
2262 have the access control we are looking for to properly
2263 find the indexed field. */
2264 int type_index = TYPE_N_BASECLASSES (type);
2265 enum accessibility acc = public_field;
2266 if (strcmp (parent->name, "private") == 0)
2267 acc = private_field;
2268 else if (strcmp (parent->name, "protected") == 0)
2269 acc = protected_field;
2273 if (TYPE_VPTR_BASETYPE (type) == type
2274 && type_index == TYPE_VPTR_FIELDNO (type))
2276 else if (match_accessibility (type, type_index, acc))
2283 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2285 if (cvalue && value)
2286 *cvalue = value_struct_element_index (value, type_index);
2289 *ctype = TYPE_FIELD_TYPE (type, type_index);
2291 else if (index < TYPE_N_BASECLASSES (type))
2293 /* This is a baseclass. */
2295 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2297 if (cvalue && value)
2299 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
2304 *ctype = TYPE_FIELD_TYPE (type, index);
2311 cplus_class_num_children (type, children);
2313 /* Everything beyond the baseclasses can
2314 only be "public", "private", or "protected"
2316 The special "fake" children are always output by varobj in
2317 this order. So if INDEX == 2, it MUST be "protected". */
2318 index -= TYPE_N_BASECLASSES (type);
2322 if (children[v_public] > 0)
2324 else if (children[v_private] > 0)
2327 access = "protected";
2330 if (children[v_public] > 0)
2332 if (children[v_private] > 0)
2335 access = "protected";
2337 else if (children[v_private] > 0)
2338 access = "protected";
2341 /* Must be protected */
2342 access = "protected";
2350 *cname = xstrdup (access);
2352 /* Value and type are null here. */
2357 c_describe_child (parent, index, cname, cvalue, ctype);
2362 cplus_name_of_child (struct varobj *parent, int index)
2365 cplus_describe_child (parent, index, &name, NULL, NULL);
2369 static struct value *
2370 cplus_value_of_root (struct varobj **var_handle)
2372 return c_value_of_root (var_handle);
2375 static struct value *
2376 cplus_value_of_child (struct varobj *parent, int index)
2378 struct value *value = NULL;
2379 cplus_describe_child (parent, index, NULL, &value, NULL);
2383 static struct type *
2384 cplus_type_of_child (struct varobj *parent, int index)
2386 struct type *type = NULL;
2387 cplus_describe_child (parent, index, NULL, NULL, &type);
2392 cplus_variable_editable (struct varobj *var)
2394 if (CPLUS_FAKE_CHILD (var))
2397 return c_variable_editable (var);
2401 cplus_value_of_variable (struct varobj *var)
2404 /* If we have one of our special types, don't print out
2406 if (CPLUS_FAKE_CHILD (var))
2407 return xstrdup ("");
2409 return c_value_of_variable (var);
2415 java_number_of_children (struct varobj *var)
2417 return cplus_number_of_children (var);
2421 java_name_of_variable (struct varobj *parent)
2425 name = cplus_name_of_variable (parent);
2426 /* If the name has "-" in it, it is because we
2427 needed to escape periods in the name... */
2430 while (*p != '\000')
2441 java_name_of_child (struct varobj *parent, int index)
2445 name = cplus_name_of_child (parent, index);
2446 /* Escape any periods in the name... */
2449 while (*p != '\000')
2459 static struct value *
2460 java_value_of_root (struct varobj **var_handle)
2462 return cplus_value_of_root (var_handle);
2465 static struct value *
2466 java_value_of_child (struct varobj *parent, int index)
2468 return cplus_value_of_child (parent, index);
2471 static struct type *
2472 java_type_of_child (struct varobj *parent, int index)
2474 return cplus_type_of_child (parent, index);
2478 java_variable_editable (struct varobj *var)
2480 return cplus_variable_editable (var);
2484 java_value_of_variable (struct varobj *var)
2486 return cplus_value_of_variable (var);
2489 extern void _initialize_varobj (void);
2491 _initialize_varobj (void)
2493 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2495 varobj_table = xmalloc (sizeof_table);
2496 memset (varobj_table, 0, sizeof_table);
2498 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2500 Set varobj debugging."), _("\
2501 Show varobj debugging."), _("\
2502 When non-zero, varobj debugging is enabled."),
2505 &setlist, &showlist);