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 /* Flag that indicates validity: set to 0 when this varobj_root refers
75 to symbols that do not exist anymore. */
78 /* Language info for this variable and its children */
79 struct language_specific *lang;
81 /* The varobj for this root node. */
82 struct varobj *rootvar;
84 /* Next root variable */
85 struct varobj_root *next;
88 typedef struct varobj *varobj_p;
92 /* Every variable in the system has a structure of this type defined
93 for it. This structure holds all information necessary to manipulate
94 a particular object variable. Members which must be freed are noted. */
98 /* Alloc'd name of the variable for this object.. If this variable is a
99 child, then this name will be the child's source name.
100 (bar, not foo.bar) */
101 /* NOTE: This is the "expression" */
104 /* The alloc'd name for this variable's object. This is here for
105 convenience when constructing this object's children. */
108 /* Index of this variable in its parent or -1 */
111 /* The type of this variable. This may NEVER be NULL. */
114 /* The value of this expression or subexpression. A NULL value
115 indicates there was an error getting this value.
116 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
117 the value is either NULL, or not lazy. */
120 /* The number of (immediate) children this variable has */
123 /* If this object is a child, this points to its immediate parent. */
124 struct varobj *parent;
126 /* Children of this object. */
127 VEC (varobj_p) *children;
129 /* Description of the root variable. Points to root variable for children. */
130 struct varobj_root *root;
132 /* The format of the output for this object */
133 enum varobj_display_formats format;
135 /* Was this variable updated via a varobj_set_value operation */
138 /* Last print value. */
141 /* Is this variable frozen. Frozen variables are never implicitly
142 updated by -var-update *
143 or -var-update <direct-or-indirect-parent>. */
146 /* Is the value of this variable intentionally not fetched? It is
147 not fetched if either the variable is frozen, or any parents is
155 struct cpstack *next;
158 /* A list of varobjs */
166 /* Private function prototypes */
168 /* Helper functions for the above subcommands. */
170 static int delete_variable (struct cpstack **, struct varobj *, int);
172 static void delete_variable_1 (struct cpstack **, int *,
173 struct varobj *, int, int);
175 static int install_variable (struct varobj *);
177 static void uninstall_variable (struct varobj *);
179 static struct varobj *create_child (struct varobj *, int, char *);
181 /* Utility routines */
183 static struct varobj *new_variable (void);
185 static struct varobj *new_root_variable (void);
187 static void free_variable (struct varobj *var);
189 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
191 static struct type *get_type (struct varobj *var);
193 static struct type *get_value_type (struct varobj *var);
195 static struct type *get_target_type (struct type *);
197 static enum varobj_display_formats variable_default_display (struct varobj *);
199 static void cppush (struct cpstack **pstack, char *name);
201 static char *cppop (struct cpstack **pstack);
203 static int install_new_value (struct varobj *var, struct value *value,
206 /* Language-specific routines. */
208 static enum varobj_languages variable_language (struct varobj *var);
210 static int number_of_children (struct varobj *);
212 static char *name_of_variable (struct varobj *);
214 static char *name_of_child (struct varobj *, int);
216 static struct value *value_of_root (struct varobj **var_handle, int *);
218 static struct value *value_of_child (struct varobj *parent, int index);
220 static int variable_editable (struct varobj *var);
222 static char *my_value_of_variable (struct varobj *var);
224 static char *value_get_print_value (struct value *value,
225 enum varobj_display_formats format);
227 static int varobj_value_is_changeable_p (struct varobj *var);
229 static int is_root_p (struct varobj *var);
231 /* C implementation */
233 static int c_number_of_children (struct varobj *var);
235 static char *c_name_of_variable (struct varobj *parent);
237 static char *c_name_of_child (struct varobj *parent, int index);
239 static struct value *c_value_of_root (struct varobj **var_handle);
241 static struct value *c_value_of_child (struct varobj *parent, int index);
243 static struct type *c_type_of_child (struct varobj *parent, int index);
245 static int c_variable_editable (struct varobj *var);
247 static char *c_value_of_variable (struct varobj *var);
249 /* C++ implementation */
251 static int cplus_number_of_children (struct varobj *var);
253 static void cplus_class_num_children (struct type *type, int children[3]);
255 static char *cplus_name_of_variable (struct varobj *parent);
257 static char *cplus_name_of_child (struct varobj *parent, int index);
259 static struct value *cplus_value_of_root (struct varobj **var_handle);
261 static struct value *cplus_value_of_child (struct varobj *parent, int index);
263 static struct type *cplus_type_of_child (struct varobj *parent, int index);
265 static int cplus_variable_editable (struct varobj *var);
267 static char *cplus_value_of_variable (struct varobj *var);
269 /* Java implementation */
271 static int java_number_of_children (struct varobj *var);
273 static char *java_name_of_variable (struct varobj *parent);
275 static char *java_name_of_child (struct varobj *parent, int index);
277 static struct value *java_value_of_root (struct varobj **var_handle);
279 static struct value *java_value_of_child (struct varobj *parent, int index);
281 static struct type *java_type_of_child (struct varobj *parent, int index);
283 static int java_variable_editable (struct varobj *var);
285 static char *java_value_of_variable (struct varobj *var);
287 /* The language specific vector */
289 struct language_specific
292 /* The language of this variable */
293 enum varobj_languages language;
295 /* The number of children of PARENT. */
296 int (*number_of_children) (struct varobj * parent);
298 /* The name (expression) of a root varobj. */
299 char *(*name_of_variable) (struct varobj * parent);
301 /* The name of the INDEX'th child of PARENT. */
302 char *(*name_of_child) (struct varobj * parent, int index);
304 /* The ``struct value *'' of the root variable ROOT. */
305 struct value *(*value_of_root) (struct varobj ** root_handle);
307 /* The ``struct value *'' of the INDEX'th child of PARENT. */
308 struct value *(*value_of_child) (struct varobj * parent, int index);
310 /* The type of the INDEX'th child of PARENT. */
311 struct type *(*type_of_child) (struct varobj * parent, int index);
313 /* Is VAR editable? */
314 int (*variable_editable) (struct varobj * var);
316 /* The current value of VAR. */
317 char *(*value_of_variable) (struct varobj * var);
320 /* Array of known source language routines. */
321 static struct language_specific languages[vlang_end] = {
322 /* Unknown (try treating as C */
325 c_number_of_children,
337 c_number_of_children,
349 cplus_number_of_children,
350 cplus_name_of_variable,
353 cplus_value_of_child,
355 cplus_variable_editable,
356 cplus_value_of_variable}
361 java_number_of_children,
362 java_name_of_variable,
367 java_variable_editable,
368 java_value_of_variable}
371 /* A little convenience enum for dealing with C++/Java */
374 v_public = 0, v_private, v_protected
379 /* Mappings of varobj_display_formats enums to gdb's format codes */
380 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
382 /* Header of the list of root variable objects */
383 static struct varobj_root *rootlist;
384 static int rootcount = 0; /* number of root varobjs in the list */
386 /* Prime number indicating the number of buckets in the hash table */
387 /* A prime large enough to avoid too many colisions */
388 #define VAROBJ_TABLE_SIZE 227
390 /* Pointer to the varobj hash table (built at run time) */
391 static struct vlist **varobj_table;
393 /* Is the variable X one of our "fake" children? */
394 #define CPLUS_FAKE_CHILD(x) \
395 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
398 /* API Implementation */
400 is_root_p (struct varobj *var)
402 return (var->root->rootvar == var);
405 /* Creates a varobj (not its children) */
407 /* Return the full FRAME which corresponds to the given CORE_ADDR
408 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
410 static struct frame_info *
411 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
413 struct frame_info *frame = NULL;
415 if (frame_addr == (CORE_ADDR) 0)
420 frame = get_prev_frame (frame);
423 if (get_frame_base_address (frame) == frame_addr)
429 varobj_create (char *objname,
430 char *expression, CORE_ADDR frame, enum varobj_type type)
433 struct frame_info *fi;
434 struct frame_info *old_fi = NULL;
436 struct cleanup *old_chain;
438 /* Fill out a varobj structure for the (root) variable being constructed. */
439 var = new_root_variable ();
440 old_chain = make_cleanup_free_variable (var);
442 if (expression != NULL)
445 enum varobj_languages lang;
446 struct value *value = NULL;
448 /* Parse and evaluate the expression, filling in as much
449 of the variable's data as possible */
451 /* Allow creator to specify context of variable */
452 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
453 fi = deprecated_safe_get_selected_frame ();
455 /* FIXME: cagney/2002-11-23: This code should be doing a
456 lookup using the frame ID and not just the frame's
457 ``address''. This, of course, means an interface change.
458 However, with out that interface change ISAs, such as the
459 ia64 with its two stacks, won't work. Similar goes for the
460 case where there is a frameless function. */
461 fi = find_frame_addr_in_frame_chain (frame);
463 /* frame = -2 means always use selected frame */
464 if (type == USE_SELECTED_FRAME)
465 var->root->use_selected_frame = 1;
469 block = get_frame_block (fi, 0);
472 innermost_block = NULL;
473 /* Wrap the call to parse expression, so we can
474 return a sensible error. */
475 if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
480 /* Don't allow variables to be created for types. */
481 if (var->root->exp->elts[0].opcode == OP_TYPE)
483 do_cleanups (old_chain);
484 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
485 " as an expression.\n");
489 var->format = variable_default_display (var);
490 var->root->valid_block = innermost_block;
491 var->name = savestring (expression, strlen (expression));
493 /* When the frame is different from the current frame,
494 we must select the appropriate frame before parsing
495 the expression, otherwise the value will not be current.
496 Since select_frame is so benign, just call it for all cases. */
499 var->root->frame = get_frame_id (fi);
500 old_fi = get_selected_frame (NULL);
504 /* We definitively need to catch errors here.
505 If evaluate_expression succeeds we got the value we wanted.
506 But if it fails, we still go on with a call to evaluate_type() */
507 if (!gdb_evaluate_expression (var->root->exp, &value))
509 /* Error getting the value. Try to at least get the
511 struct value *type_only_value = evaluate_type (var->root->exp);
512 var->type = value_type (type_only_value);
515 var->type = value_type (value);
517 install_new_value (var, value, 1 /* Initial assignment */);
519 /* Set language info */
520 lang = variable_language (var);
521 var->root->lang = &languages[lang];
523 /* Set ourselves as our root */
524 var->root->rootvar = var;
526 /* Reset the selected frame */
528 select_frame (old_fi);
531 /* If the variable object name is null, that means this
532 is a temporary variable, so don't install it. */
534 if ((var != NULL) && (objname != NULL))
536 var->obj_name = savestring (objname, strlen (objname));
538 /* If a varobj name is duplicated, the install will fail so
540 if (!install_variable (var))
542 do_cleanups (old_chain);
547 discard_cleanups (old_chain);
551 /* Generates an unique name that can be used for a varobj */
554 varobj_gen_name (void)
559 /* generate a name for this object */
561 obj_name = xstrprintf ("var%d", id);
566 /* Given an "objname", returns the pointer to the corresponding varobj
567 or NULL if not found */
570 varobj_get_handle (char *objname)
574 unsigned int index = 0;
577 for (chp = objname; *chp; chp++)
579 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
582 cv = *(varobj_table + index);
583 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
587 error (_("Variable object not found"));
592 /* Given the handle, return the name of the object */
595 varobj_get_objname (struct varobj *var)
597 return var->obj_name;
600 /* Given the handle, return the expression represented by the object */
603 varobj_get_expression (struct varobj *var)
605 return name_of_variable (var);
608 /* Deletes a varobj and all its children if only_children == 0,
609 otherwise deletes only the children; returns a malloc'ed list of all the
610 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
613 varobj_delete (struct varobj *var, char ***dellist, int only_children)
617 struct cpstack *result = NULL;
620 /* Initialize a stack for temporary results */
621 cppush (&result, NULL);
624 /* Delete only the variable children */
625 delcount = delete_variable (&result, var, 1 /* only the children */ );
627 /* Delete the variable and all its children */
628 delcount = delete_variable (&result, var, 0 /* parent+children */ );
630 /* We may have been asked to return a list of what has been deleted */
633 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
637 *cp = cppop (&result);
638 while ((*cp != NULL) && (mycount > 0))
642 *cp = cppop (&result);
645 if (mycount || (*cp != NULL))
646 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
653 /* Set/Get variable object display format */
655 enum varobj_display_formats
656 varobj_set_display_format (struct varobj *var,
657 enum varobj_display_formats format)
664 case FORMAT_HEXADECIMAL:
666 var->format = format;
670 var->format = variable_default_display (var);
676 enum varobj_display_formats
677 varobj_get_display_format (struct varobj *var)
683 varobj_set_frozen (struct varobj *var, int frozen)
685 /* When a variable is unfrozen, we don't fetch its value.
686 The 'not_fetched' flag remains set, so next -var-update
689 We don't fetch the value, because for structures the client
690 should do -var-update anyway. It would be bad to have different
691 client-size logic for structure and other types. */
692 var->frozen = frozen;
696 varobj_get_frozen (struct varobj *var)
703 varobj_get_num_children (struct varobj *var)
705 if (var->num_children == -1)
706 var->num_children = number_of_children (var);
708 return var->num_children;
711 /* Creates a list of the immediate children of a variable object;
712 the return code is the number of such children or -1 on error */
715 varobj_list_children (struct varobj *var, struct varobj ***childlist)
717 struct varobj *child;
721 /* sanity check: have we been passed a pointer? */
722 if (childlist == NULL)
727 if (var->num_children == -1)
728 var->num_children = number_of_children (var);
730 /* If that failed, give up. */
731 if (var->num_children == -1)
734 /* If we're called when the list of children is not yet initialized,
735 allocate enough elements in it. */
736 while (VEC_length (varobj_p, var->children) < var->num_children)
737 VEC_safe_push (varobj_p, var->children, NULL);
739 /* List of children */
740 *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
742 for (i = 0; i < var->num_children; i++)
746 /* Mark as the end in case we bail out */
747 *((*childlist) + i) = NULL;
749 existing = VEC_index (varobj_p, var->children, i);
751 if (existing == NULL)
753 /* Either it's the first call to varobj_list_children for
754 this variable object, and the child was never created,
755 or it was explicitly deleted by the client. */
756 name = name_of_child (var, i);
757 existing = create_child (var, i, name);
758 VEC_replace (varobj_p, var->children, i, existing);
761 *((*childlist) + i) = existing;
764 /* End of list is marked by a NULL pointer */
765 *((*childlist) + i) = NULL;
767 return var->num_children;
770 /* Obtain the type of an object Variable as a string similar to the one gdb
771 prints on the console */
774 varobj_get_type (struct varobj *var)
777 struct cleanup *old_chain;
782 /* For the "fake" variables, do not return a type. (It's type is
784 Do not return a type for invalid variables as well. */
785 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
788 stb = mem_fileopen ();
789 old_chain = make_cleanup_ui_file_delete (stb);
791 /* To print the type, we simply create a zero ``struct value *'' and
792 cast it to our type. We then typeprint this variable. */
793 val = value_zero (var->type, not_lval);
794 type_print (value_type (val), "", stb, -1);
796 thetype = ui_file_xstrdup (stb, &length);
797 do_cleanups (old_chain);
801 /* Obtain the type of an object variable. */
804 varobj_get_gdb_type (struct varobj *var)
809 enum varobj_languages
810 varobj_get_language (struct varobj *var)
812 return variable_language (var);
816 varobj_get_attributes (struct varobj *var)
820 if (var->root->is_valid && variable_editable (var))
821 /* FIXME: define masks for attributes */
822 attributes |= 0x00000001; /* Editable */
828 varobj_get_value (struct varobj *var)
830 return my_value_of_variable (var);
833 /* Set the value of an object variable (if it is editable) to the
834 value of the given expression */
835 /* Note: Invokes functions that can call error() */
838 varobj_set_value (struct varobj *var, char *expression)
844 /* The argument "expression" contains the variable's new value.
845 We need to first construct a legal expression for this -- ugh! */
846 /* Does this cover all the bases? */
847 struct expression *exp;
849 int saved_input_radix = input_radix;
851 if (var->value != NULL && variable_editable (var))
853 char *s = expression;
856 input_radix = 10; /* ALWAYS reset to decimal temporarily */
857 exp = parse_exp_1 (&s, 0, 0);
858 if (!gdb_evaluate_expression (exp, &value))
860 /* We cannot proceed without a valid expression. */
865 /* All types that are editable must also be changeable. */
866 gdb_assert (varobj_value_is_changeable_p (var));
868 /* The value of a changeable variable object must not be lazy. */
869 gdb_assert (!value_lazy (var->value));
871 /* Need to coerce the input. We want to check if the
872 value of the variable object will be different
873 after assignment, and the first thing value_assign
874 does is coerce the input.
875 For example, if we are assigning an array to a pointer variable we
876 should compare the pointer with the the array's address, not with the
878 value = coerce_array (value);
880 /* The new value may be lazy. gdb_value_assign, or
881 rather value_contents, will take care of this.
882 If fetching of the new value will fail, gdb_value_assign
883 with catch the exception. */
884 if (!gdb_value_assign (var->value, value, &val))
887 /* If the value has changed, record it, so that next -var-update can
888 report this change. If a variable had a value of '1', we've set it
889 to '333' and then set again to '1', when -var-update will report this
890 variable as changed -- because the first assignment has set the
891 'updated' flag. There's no need to optimize that, because return value
892 of -var-update should be considered an approximation. */
893 var->updated = install_new_value (var, val, 0 /* Compare values. */);
894 input_radix = saved_input_radix;
901 /* Returns a malloc'ed list with all root variable objects */
903 varobj_list (struct varobj ***varlist)
906 struct varobj_root *croot;
907 int mycount = rootcount;
909 /* Alloc (rootcount + 1) entries for the result */
910 *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
914 while ((croot != NULL) && (mycount > 0))
916 *cv = croot->rootvar;
921 /* Mark the end of the list */
924 if (mycount || (croot != NULL))
926 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
932 /* Assign a new value to a variable object. If INITIAL is non-zero,
933 this is the first assignement after the variable object was just
934 created, or changed type. In that case, just assign the value
936 Otherwise, assign the value and if type_changeable returns non-zero,
937 find if the new value is different from the current value.
938 Return 1 if so, and 0 if the values are equal.
940 The VALUE parameter should not be released -- the function will
941 take care of releasing it when needed. */
943 install_new_value (struct varobj *var, struct value *value, int initial)
948 int intentionally_not_fetched = 0;
950 /* We need to know the varobj's type to decide if the value should
951 be fetched or not. C++ fake children (public/protected/private) don't have
953 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
954 changeable = varobj_value_is_changeable_p (var);
955 need_to_fetch = changeable;
957 /* We are not interested in the address of references, and given
958 that in C++ a reference is not rebindable, it cannot
959 meaningfully change. So, get hold of the real value. */
962 value = coerce_ref (value);
963 release_value (value);
966 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
967 /* For unions, we need to fetch the value implicitly because
968 of implementation of union member fetch. When gdb
969 creates a value for a field and the value of the enclosing
970 structure is not lazy, it immediately copies the necessary
971 bytes from the enclosing values. If the enclosing value is
972 lazy, the call to value_fetch_lazy on the field will read
973 the data from memory. For unions, that means we'll read the
974 same memory more than once, which is not desirable. So
978 /* The new value might be lazy. If the type is changeable,
979 that is we'll be comparing values of this type, fetch the
980 value now. Otherwise, on the next update the old value
981 will be lazy, which means we've lost that old value. */
982 if (need_to_fetch && value && value_lazy (value))
984 struct varobj *parent = var->parent;
985 int frozen = var->frozen;
986 for (; !frozen && parent; parent = parent->parent)
987 frozen |= parent->frozen;
989 if (frozen && initial)
991 /* For variables that are frozen, or are children of frozen
992 variables, we don't do fetch on initial assignment.
993 For non-initial assignemnt we do the fetch, since it means we're
994 explicitly asked to compare the new value with the old one. */
995 intentionally_not_fetched = 1;
997 else if (!gdb_value_fetch_lazy (value))
999 /* Set the value to NULL, so that for the next -var-update,
1000 we don't try to compare the new value with this value,
1001 that we couldn't even read. */
1006 /* If the type is changeable, compare the old and the new values.
1007 If this is the initial assignment, we don't have any old value
1009 if (initial && changeable)
1010 var->print_value = value_get_print_value (value, var->format);
1011 else if (changeable)
1013 /* If the value of the varobj was changed by -var-set-value, then the
1014 value in the varobj and in the target is the same. However, that value
1015 is different from the value that the varobj had after the previous
1016 -var-update. So need to the varobj as changed. */
1019 xfree (var->print_value);
1020 var->print_value = value_get_print_value (value, var->format);
1025 /* Try to compare the values. That requires that both
1026 values are non-lazy. */
1027 if (var->not_fetched && value_lazy (var->value))
1029 /* This is a frozen varobj and the value was never read.
1030 Presumably, UI shows some "never read" indicator.
1031 Now that we've fetched the real value, we need to report
1032 this varobj as changed so that UI can show the real
1036 else if (var->value == NULL && value == NULL)
1039 else if (var->value == NULL || value == NULL)
1041 xfree (var->print_value);
1042 var->print_value = value_get_print_value (value, var->format);
1048 gdb_assert (!value_lazy (var->value));
1049 gdb_assert (!value_lazy (value));
1050 print_value = value_get_print_value (value, var->format);
1052 gdb_assert (var->print_value != NULL && print_value != NULL);
1053 if (strcmp (var->print_value, print_value) != 0)
1055 xfree (var->print_value);
1056 var->print_value = print_value;
1060 xfree (print_value);
1065 /* We must always keep the new value, since children depend on it. */
1066 if (var->value != NULL && var->value != value)
1067 value_free (var->value);
1069 if (value && value_lazy (value) && intentionally_not_fetched)
1070 var->not_fetched = 1;
1072 var->not_fetched = 0;
1075 gdb_assert (!var->value || value_type (var->value));
1080 /* Update the values for a variable and its children. This is a
1081 two-pronged attack. First, re-parse the value for the root's
1082 expression to see if it's changed. Then go all the way
1083 through its children, reconstructing them and noting if they've
1086 < 0 for error values, see varobj.h.
1087 Otherwise it is the number of children + parent changed.
1089 The EXPLICIT parameter specifies if this call is result
1090 of MI request to update this specific variable, or
1091 result of implicit -var-update *. For implicit request, we don't
1092 update frozen variables.
1094 NOTE: This function may delete the caller's varobj. If it
1095 returns TYPE_CHANGED, then it has done this and VARP will be modified
1096 to point to the new varobj. */
1099 varobj_update (struct varobj **varp, struct varobj ***changelist,
1103 int type_changed = 0;
1108 struct varobj **templist = NULL;
1110 VEC (varobj_p) *stack = NULL;
1111 VEC (varobj_p) *result = NULL;
1112 struct frame_id old_fid;
1113 struct frame_info *fi;
1115 /* sanity check: have we been passed a pointer? */
1116 gdb_assert (changelist);
1118 /* Frozen means frozen -- we don't check for any change in
1119 this varobj, including its going out of scope, or
1120 changing type. One use case for frozen varobjs is
1121 retaining previously evaluated expressions, and we don't
1122 want them to be reevaluated at all. */
1123 if (!explicit && (*varp)->frozen)
1126 if (!(*varp)->root->is_valid)
1129 if ((*varp)->root->rootvar == *varp)
1131 /* Save the selected stack frame, since we will need to change it
1132 in order to evaluate expressions. */
1133 old_fid = get_frame_id (deprecated_safe_get_selected_frame ());
1135 /* Update the root variable. value_of_root can return NULL
1136 if the variable is no longer around, i.e. we stepped out of
1137 the frame in which a local existed. We are letting the
1138 value_of_root variable dispose of the varobj if the type
1141 new = value_of_root (varp, &type_changed);
1143 /* Restore selected frame. */
1144 fi = frame_find_by_id (old_fid);
1148 /* If this is a "use_selected_frame" varobj, and its type has changed,
1149 them note that it's changed. */
1151 VEC_safe_push (varobj_p, result, *varp);
1153 if (install_new_value ((*varp), new, type_changed))
1155 /* If type_changed is 1, install_new_value will never return
1156 non-zero, so we'll never report the same variable twice. */
1157 gdb_assert (!type_changed);
1158 VEC_safe_push (varobj_p, result, *varp);
1163 /* This means the varobj itself is out of scope.
1165 VEC_free (varobj_p, result);
1166 return NOT_IN_SCOPE;
1170 VEC_safe_push (varobj_p, stack, *varp);
1172 /* Walk through the children, reconstructing them all. */
1173 while (!VEC_empty (varobj_p, stack))
1175 v = VEC_pop (varobj_p, stack);
1177 /* Push any children. Use reverse order so that the first
1178 child is popped from the work stack first, and so
1179 will be added to result first. This does not
1180 affect correctness, just "nicer". */
1181 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
1183 varobj_p c = VEC_index (varobj_p, v->children, i);
1184 /* Child may be NULL if explicitly deleted by -var-delete. */
1185 if (c != NULL && !c->frozen)
1186 VEC_safe_push (varobj_p, stack, c);
1189 /* Update this variable, unless it's a root, which is already
1191 if (v->root->rootvar != v)
1193 new = value_of_child (v->parent, v->index);
1194 if (install_new_value (v, new, 0 /* type not changed */))
1196 /* Note that it's changed */
1197 VEC_safe_push (varobj_p, result, v);
1203 /* Alloc (changed + 1) list entries. */
1204 changed = VEC_length (varobj_p, result);
1205 *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1208 for (i = 0; i < changed; ++i)
1210 *cv = VEC_index (varobj_p, result, i);
1211 gdb_assert (*cv != NULL);
1216 VEC_free (varobj_p, stack);
1217 VEC_free (varobj_p, result);
1220 return TYPE_CHANGED;
1226 /* Helper functions */
1229 * Variable object construction/destruction
1233 delete_variable (struct cpstack **resultp, struct varobj *var,
1234 int only_children_p)
1238 delete_variable_1 (resultp, &delcount, var,
1239 only_children_p, 1 /* remove_from_parent_p */ );
1244 /* Delete the variable object VAR and its children */
1245 /* IMPORTANT NOTE: If we delete a variable which is a child
1246 and the parent is not removed we dump core. It must be always
1247 initially called with remove_from_parent_p set */
1249 delete_variable_1 (struct cpstack **resultp, int *delcountp,
1250 struct varobj *var, int only_children_p,
1251 int remove_from_parent_p)
1255 /* Delete any children of this variable, too. */
1256 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
1258 varobj_p child = VEC_index (varobj_p, var->children, i);
1259 if (!remove_from_parent_p)
1260 child->parent = NULL;
1261 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
1263 VEC_free (varobj_p, var->children);
1265 /* if we were called to delete only the children we are done here */
1266 if (only_children_p)
1269 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1270 /* If the name is null, this is a temporary variable, that has not
1271 yet been installed, don't report it, it belongs to the caller... */
1272 if (var->obj_name != NULL)
1274 cppush (resultp, xstrdup (var->obj_name));
1275 *delcountp = *delcountp + 1;
1278 /* If this variable has a parent, remove it from its parent's list */
1279 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1280 (as indicated by remove_from_parent_p) we don't bother doing an
1281 expensive list search to find the element to remove when we are
1282 discarding the list afterwards */
1283 if ((remove_from_parent_p) && (var->parent != NULL))
1285 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
1288 if (var->obj_name != NULL)
1289 uninstall_variable (var);
1291 /* Free memory associated with this variable */
1292 free_variable (var);
1295 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1297 install_variable (struct varobj *var)
1300 struct vlist *newvl;
1302 unsigned int index = 0;
1305 for (chp = var->obj_name; *chp; chp++)
1307 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1310 cv = *(varobj_table + index);
1311 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1315 error (_("Duplicate variable object name"));
1317 /* Add varobj to hash table */
1318 newvl = xmalloc (sizeof (struct vlist));
1319 newvl->next = *(varobj_table + index);
1321 *(varobj_table + index) = newvl;
1323 /* If root, add varobj to root list */
1324 if (is_root_p (var))
1326 /* Add to list of root variables */
1327 if (rootlist == NULL)
1328 var->root->next = NULL;
1330 var->root->next = rootlist;
1331 rootlist = var->root;
1338 /* Unistall the object VAR. */
1340 uninstall_variable (struct varobj *var)
1344 struct varobj_root *cr;
1345 struct varobj_root *prer;
1347 unsigned int index = 0;
1350 /* Remove varobj from hash table */
1351 for (chp = var->obj_name; *chp; chp++)
1353 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1356 cv = *(varobj_table + index);
1358 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1365 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1370 ("Assertion failed: Could not find variable object \"%s\" to delete",
1376 *(varobj_table + index) = cv->next;
1378 prev->next = cv->next;
1382 /* If root, remove varobj from root list */
1383 if (is_root_p (var))
1385 /* Remove from list of root variables */
1386 if (rootlist == var->root)
1387 rootlist = var->root->next;
1392 while ((cr != NULL) && (cr->rootvar != var))
1400 ("Assertion failed: Could not find varobj \"%s\" in root list",
1407 prer->next = cr->next;
1414 /* Create and install a child of the parent of the given name */
1415 static struct varobj *
1416 create_child (struct varobj *parent, int index, char *name)
1418 struct varobj *child;
1420 struct value *value;
1422 child = new_variable ();
1424 /* name is allocated by name_of_child */
1426 child->index = index;
1427 value = value_of_child (parent, index);
1428 child->parent = parent;
1429 child->root = parent->root;
1430 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
1431 child->obj_name = childs_name;
1432 install_variable (child);
1434 /* Compute the type of the child. Must do this before
1435 calling install_new_value. */
1437 /* If the child had no evaluation errors, var->value
1438 will be non-NULL and contain a valid type. */
1439 child->type = value_type (value);
1441 /* Otherwise, we must compute the type. */
1442 child->type = (*child->root->lang->type_of_child) (child->parent,
1444 install_new_value (child, value, 1);
1451 * Miscellaneous utility functions.
1454 /* Allocate memory and initialize a new variable */
1455 static struct varobj *
1460 var = (struct varobj *) xmalloc (sizeof (struct varobj));
1462 var->obj_name = NULL;
1466 var->num_children = -1;
1468 var->children = NULL;
1472 var->print_value = NULL;
1474 var->not_fetched = 0;
1479 /* Allocate memory and initialize a new root variable */
1480 static struct varobj *
1481 new_root_variable (void)
1483 struct varobj *var = new_variable ();
1484 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1485 var->root->lang = NULL;
1486 var->root->exp = NULL;
1487 var->root->valid_block = NULL;
1488 var->root->frame = null_frame_id;
1489 var->root->use_selected_frame = 0;
1490 var->root->rootvar = NULL;
1491 var->root->is_valid = 1;
1496 /* Free any allocated memory associated with VAR. */
1498 free_variable (struct varobj *var)
1500 /* Free the expression if this is a root variable. */
1501 if (is_root_p (var))
1503 free_current_contents (&var->root->exp);
1508 xfree (var->obj_name);
1509 xfree (var->print_value);
1514 do_free_variable_cleanup (void *var)
1516 free_variable (var);
1519 static struct cleanup *
1520 make_cleanup_free_variable (struct varobj *var)
1522 return make_cleanup (do_free_variable_cleanup, var);
1525 /* This returns the type of the variable. It also skips past typedefs
1526 to return the real type of the variable.
1528 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1529 except within get_target_type and get_type. */
1530 static struct type *
1531 get_type (struct varobj *var)
1537 type = check_typedef (type);
1542 /* Return the type of the value that's stored in VAR,
1543 or that would have being stored there if the
1544 value were accessible.
1546 This differs from VAR->type in that VAR->type is always
1547 the true type of the expession in the source language.
1548 The return value of this function is the type we're
1549 actually storing in varobj, and using for displaying
1550 the values and for comparing previous and new values.
1552 For example, top-level references are always stripped. */
1553 static struct type *
1554 get_value_type (struct varobj *var)
1559 type = value_type (var->value);
1563 type = check_typedef (type);
1565 if (TYPE_CODE (type) == TYPE_CODE_REF)
1566 type = get_target_type (type);
1568 type = check_typedef (type);
1573 /* This returns the target type (or NULL) of TYPE, also skipping
1574 past typedefs, just like get_type ().
1576 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1577 except within get_target_type and get_type. */
1578 static struct type *
1579 get_target_type (struct type *type)
1583 type = TYPE_TARGET_TYPE (type);
1585 type = check_typedef (type);
1591 /* What is the default display for this variable? We assume that
1592 everything is "natural". Any exceptions? */
1593 static enum varobj_display_formats
1594 variable_default_display (struct varobj *var)
1596 return FORMAT_NATURAL;
1599 /* FIXME: The following should be generic for any pointer */
1601 cppush (struct cpstack **pstack, char *name)
1605 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1611 /* FIXME: The following should be generic for any pointer */
1613 cppop (struct cpstack **pstack)
1618 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1623 *pstack = (*pstack)->next;
1630 * Language-dependencies
1633 /* Common entry points */
1635 /* Get the language of variable VAR. */
1636 static enum varobj_languages
1637 variable_language (struct varobj *var)
1639 enum varobj_languages lang;
1641 switch (var->root->exp->language_defn->la_language)
1647 case language_cplus:
1658 /* Return the number of children for a given variable.
1659 The result of this function is defined by the language
1660 implementation. The number of children returned by this function
1661 is the number of children that the user will see in the variable
1664 number_of_children (struct varobj *var)
1666 return (*var->root->lang->number_of_children) (var);;
1669 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1671 name_of_variable (struct varobj *var)
1673 return (*var->root->lang->name_of_variable) (var);
1676 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1678 name_of_child (struct varobj *var, int index)
1680 return (*var->root->lang->name_of_child) (var, index);
1683 /* What is the ``struct value *'' of the root variable VAR?
1684 TYPE_CHANGED controls what to do if the type of a
1685 use_selected_frame = 1 variable changes. On input,
1686 TYPE_CHANGED = 1 means discard the old varobj, and replace
1687 it with this one. TYPE_CHANGED = 0 means leave it around.
1688 NB: In both cases, var_handle will point to the new varobj,
1689 so if you use TYPE_CHANGED = 0, you will have to stash the
1690 old varobj pointer away somewhere before calling this.
1691 On return, TYPE_CHANGED will be 1 if the type has changed, and
1693 static struct value *
1694 value_of_root (struct varobj **var_handle, int *type_changed)
1698 if (var_handle == NULL)
1703 /* This should really be an exception, since this should
1704 only get called with a root variable. */
1706 if (!is_root_p (var))
1709 if (var->root->use_selected_frame)
1711 struct varobj *tmp_var;
1712 char *old_type, *new_type;
1714 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1715 USE_SELECTED_FRAME);
1716 if (tmp_var == NULL)
1720 old_type = varobj_get_type (var);
1721 new_type = varobj_get_type (tmp_var);
1722 if (strcmp (old_type, new_type) == 0)
1724 varobj_delete (tmp_var, NULL, 0);
1732 savestring (var->obj_name, strlen (var->obj_name));
1733 varobj_delete (var, NULL, 0);
1737 tmp_var->obj_name = varobj_gen_name ();
1739 install_variable (tmp_var);
1740 *var_handle = tmp_var;
1752 return (*var->root->lang->value_of_root) (var_handle);
1755 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1756 static struct value *
1757 value_of_child (struct varobj *parent, int index)
1759 struct value *value;
1761 value = (*parent->root->lang->value_of_child) (parent, index);
1766 /* Is this variable editable? Use the variable's type to make
1767 this determination. */
1769 variable_editable (struct varobj *var)
1771 return (*var->root->lang->variable_editable) (var);
1774 /* GDB already has a command called "value_of_variable". Sigh. */
1776 my_value_of_variable (struct varobj *var)
1778 if (var->root->is_valid)
1779 return (*var->root->lang->value_of_variable) (var);
1785 value_get_print_value (struct value *value, enum varobj_display_formats format)
1788 struct ui_file *stb;
1789 struct cleanup *old_chain;
1795 stb = mem_fileopen ();
1796 old_chain = make_cleanup_ui_file_delete (stb);
1798 common_val_print (value, stb, format_code[(int) format], 1, 0, 0);
1799 thevalue = ui_file_xstrdup (stb, &dummy);
1801 do_cleanups (old_chain);
1805 /* Return non-zero if changes in value of VAR
1806 must be detected and reported by -var-update.
1807 Return zero is -var-update should never report
1808 changes of such values. This makes sense for structures
1809 (since the changes in children values will be reported separately),
1810 or for artifical objects (like 'public' pseudo-field in C++).
1812 Return value of 0 means that gdb need not call value_fetch_lazy
1813 for the value of this variable object. */
1815 varobj_value_is_changeable_p (struct varobj *var)
1820 if (CPLUS_FAKE_CHILD (var))
1823 type = get_value_type (var);
1825 switch (TYPE_CODE (type))
1827 case TYPE_CODE_STRUCT:
1828 case TYPE_CODE_UNION:
1829 case TYPE_CODE_ARRAY:
1840 /* Given the value and the type of a variable object,
1841 adjust the value and type to those necessary
1842 for getting children of the variable object.
1843 This includes dereferencing top-level references
1844 to all types and dereferencing pointers to
1847 Both TYPE and *TYPE should be non-null. VALUE
1848 can be null if we want to only translate type.
1849 *VALUE can be null as well -- if the parent
1850 value is not known. */
1852 adjust_value_for_child_access (struct value **value,
1855 gdb_assert (type && *type);
1857 *type = check_typedef (*type);
1859 /* The type of value stored in varobj, that is passed
1860 to us, is already supposed to be
1861 reference-stripped. */
1863 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
1865 /* Pointers to structures are treated just like
1866 structures when accessing children. Don't
1867 dererences pointers to other types. */
1868 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
1870 struct type *target_type = get_target_type (*type);
1871 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
1872 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
1874 if (value && *value)
1875 gdb_value_ind (*value, value);
1876 *type = target_type;
1880 /* The 'get_target_type' function calls check_typedef on
1881 result, so we can immediately check type code. No
1882 need to call check_typedef here. */
1887 c_number_of_children (struct varobj *var)
1889 struct type *type = get_value_type (var);
1891 struct type *target;
1893 adjust_value_for_child_access (NULL, &type);
1894 target = get_target_type (type);
1896 switch (TYPE_CODE (type))
1898 case TYPE_CODE_ARRAY:
1899 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
1900 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
1901 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1903 /* If we don't know how many elements there are, don't display
1908 case TYPE_CODE_STRUCT:
1909 case TYPE_CODE_UNION:
1910 children = TYPE_NFIELDS (type);
1914 /* The type here is a pointer to non-struct. Typically, pointers
1915 have one child, except for function ptrs, which have no children,
1916 and except for void*, as we don't know what to show.
1918 We can show char* so we allow it to be dereferenced. If you decide
1919 to test for it, please mind that a little magic is necessary to
1920 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1921 TYPE_NAME == "char" */
1922 if (TYPE_CODE (target) == TYPE_CODE_FUNC
1923 || TYPE_CODE (target) == TYPE_CODE_VOID)
1930 /* Other types have no children */
1938 c_name_of_variable (struct varobj *parent)
1940 return savestring (parent->name, strlen (parent->name));
1943 /* Return the value of element TYPE_INDEX of a structure
1944 value VALUE. VALUE's type should be a structure,
1945 or union, or a typedef to struct/union.
1947 Returns NULL if getting the value fails. Never throws. */
1948 static struct value *
1949 value_struct_element_index (struct value *value, int type_index)
1951 struct value *result = NULL;
1952 volatile struct gdb_exception e;
1954 struct type *type = value_type (value);
1955 type = check_typedef (type);
1957 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1958 || TYPE_CODE (type) == TYPE_CODE_UNION);
1960 TRY_CATCH (e, RETURN_MASK_ERROR)
1962 if (TYPE_FIELD_STATIC (type, type_index))
1963 result = value_static_field (type, type_index);
1965 result = value_primitive_field (value, 0, type_index, type);
1977 /* Obtain the information about child INDEX of the variable
1979 If CNAME is not null, sets *CNAME to the name of the child relative
1981 If CVALUE is not null, sets *CVALUE to the value of the child.
1982 If CTYPE is not null, sets *CTYPE to the type of the child.
1984 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
1985 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
1988 c_describe_child (struct varobj *parent, int index,
1989 char **cname, struct value **cvalue, struct type **ctype)
1991 struct value *value = parent->value;
1992 struct type *type = get_value_type (parent);
2001 adjust_value_for_child_access (&value, &type);
2003 switch (TYPE_CODE (type))
2005 case TYPE_CODE_ARRAY:
2007 *cname = xstrprintf ("%d", index
2008 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2010 if (cvalue && value)
2012 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2013 struct value *indval =
2014 value_from_longest (builtin_type_int, (LONGEST) real_index);
2015 gdb_value_subscript (value, indval, cvalue);
2019 *ctype = get_target_type (type);
2023 case TYPE_CODE_STRUCT:
2024 case TYPE_CODE_UNION:
2027 char *string = TYPE_FIELD_NAME (type, index);
2028 *cname = savestring (string, strlen (string));
2031 if (cvalue && value)
2033 /* For C, varobj index is the same as type index. */
2034 *cvalue = value_struct_element_index (value, index);
2038 *ctype = TYPE_FIELD_TYPE (type, index);
2044 *cname = xstrprintf ("*%s", parent->name);
2046 if (cvalue && value)
2047 gdb_value_ind (value, cvalue);
2049 /* Don't use get_target_type because it calls
2050 check_typedef and here, we want to show the true
2051 declared type of the variable. */
2053 *ctype = TYPE_TARGET_TYPE (type);
2058 /* This should not happen */
2060 *cname = xstrdup ("???");
2061 /* Don't set value and type, we don't know then. */
2066 c_name_of_child (struct varobj *parent, int index)
2069 c_describe_child (parent, index, &name, NULL, NULL);
2073 static struct value *
2074 c_value_of_root (struct varobj **var_handle)
2076 struct value *new_val = NULL;
2077 struct varobj *var = *var_handle;
2078 struct frame_info *fi;
2081 /* Only root variables can be updated... */
2082 if (!is_root_p (var))
2083 /* Not a root var */
2087 /* Determine whether the variable is still around. */
2088 if (var->root->valid_block == NULL || var->root->use_selected_frame)
2092 fi = frame_find_by_id (var->root->frame);
2093 within_scope = fi != NULL;
2094 /* FIXME: select_frame could fail */
2097 CORE_ADDR pc = get_frame_pc (fi);
2098 if (pc < BLOCK_START (var->root->valid_block) ||
2099 pc >= BLOCK_END (var->root->valid_block))
2108 /* We need to catch errors here, because if evaluate
2109 expression fails we want to just return NULL. */
2110 gdb_evaluate_expression (var->root->exp, &new_val);
2117 static struct value *
2118 c_value_of_child (struct varobj *parent, int index)
2120 struct value *value = NULL;
2121 c_describe_child (parent, index, NULL, &value, NULL);
2126 static struct type *
2127 c_type_of_child (struct varobj *parent, int index)
2129 struct type *type = NULL;
2130 c_describe_child (parent, index, NULL, NULL, &type);
2135 c_variable_editable (struct varobj *var)
2137 switch (TYPE_CODE (get_value_type (var)))
2139 case TYPE_CODE_STRUCT:
2140 case TYPE_CODE_UNION:
2141 case TYPE_CODE_ARRAY:
2142 case TYPE_CODE_FUNC:
2143 case TYPE_CODE_METHOD:
2154 c_value_of_variable (struct varobj *var)
2156 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2157 it will print out its children instead of "{...}". So we need to
2158 catch that case explicitly. */
2159 struct type *type = get_type (var);
2161 /* Strip top-level references. */
2162 while (TYPE_CODE (type) == TYPE_CODE_REF)
2163 type = check_typedef (TYPE_TARGET_TYPE (type));
2165 switch (TYPE_CODE (type))
2167 case TYPE_CODE_STRUCT:
2168 case TYPE_CODE_UNION:
2169 return xstrdup ("{...}");
2172 case TYPE_CODE_ARRAY:
2175 number = xstrprintf ("[%d]", var->num_children);
2182 if (var->value == NULL)
2184 /* This can happen if we attempt to get the value of a struct
2185 member when the parent is an invalid pointer. This is an
2186 error condition, so we should tell the caller. */
2191 if (var->not_fetched && value_lazy (var->value))
2192 /* Frozen variable and no value yet. We don't
2193 implicitly fetch the value. MI response will
2194 use empty string for the value, which is OK. */
2197 gdb_assert (varobj_value_is_changeable_p (var));
2198 gdb_assert (!value_lazy (var->value));
2199 return value_get_print_value (var->value, var->format);
2209 cplus_number_of_children (struct varobj *var)
2212 int children, dont_know;
2217 if (!CPLUS_FAKE_CHILD (var))
2219 type = get_value_type (var);
2220 adjust_value_for_child_access (NULL, &type);
2222 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2223 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2227 cplus_class_num_children (type, kids);
2228 if (kids[v_public] != 0)
2230 if (kids[v_private] != 0)
2232 if (kids[v_protected] != 0)
2235 /* Add any baseclasses */
2236 children += TYPE_N_BASECLASSES (type);
2239 /* FIXME: save children in var */
2246 type = get_value_type (var->parent);
2247 adjust_value_for_child_access (NULL, &type);
2249 cplus_class_num_children (type, kids);
2250 if (strcmp (var->name, "public") == 0)
2251 children = kids[v_public];
2252 else if (strcmp (var->name, "private") == 0)
2253 children = kids[v_private];
2255 children = kids[v_protected];
2260 children = c_number_of_children (var);
2265 /* Compute # of public, private, and protected variables in this class.
2266 That means we need to descend into all baseclasses and find out
2267 how many are there, too. */
2269 cplus_class_num_children (struct type *type, int children[3])
2273 children[v_public] = 0;
2274 children[v_private] = 0;
2275 children[v_protected] = 0;
2277 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2279 /* If we have a virtual table pointer, omit it. */
2280 if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
2283 if (TYPE_FIELD_PROTECTED (type, i))
2284 children[v_protected]++;
2285 else if (TYPE_FIELD_PRIVATE (type, i))
2286 children[v_private]++;
2288 children[v_public]++;
2293 cplus_name_of_variable (struct varobj *parent)
2295 return c_name_of_variable (parent);
2298 enum accessibility { private_field, protected_field, public_field };
2300 /* Check if field INDEX of TYPE has the specified accessibility.
2301 Return 0 if so and 1 otherwise. */
2303 match_accessibility (struct type *type, int index, enum accessibility acc)
2305 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2307 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2309 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2310 && !TYPE_FIELD_PROTECTED (type, index))
2317 cplus_describe_child (struct varobj *parent, int index,
2318 char **cname, struct value **cvalue, struct type **ctype)
2321 struct value *value;
2332 if (CPLUS_FAKE_CHILD (parent))
2334 value = parent->parent->value;
2335 type = get_value_type (parent->parent);
2339 value = parent->value;
2340 type = get_value_type (parent);
2343 adjust_value_for_child_access (&value, &type);
2345 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2346 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
2348 if (CPLUS_FAKE_CHILD (parent))
2350 /* The fields of the class type are ordered as they
2351 appear in the class. We are given an index for a
2352 particular access control type ("public","protected",
2353 or "private"). We must skip over fields that don't
2354 have the access control we are looking for to properly
2355 find the indexed field. */
2356 int type_index = TYPE_N_BASECLASSES (type);
2357 enum accessibility acc = public_field;
2358 if (strcmp (parent->name, "private") == 0)
2359 acc = private_field;
2360 else if (strcmp (parent->name, "protected") == 0)
2361 acc = protected_field;
2365 if (TYPE_VPTR_BASETYPE (type) == type
2366 && type_index == TYPE_VPTR_FIELDNO (type))
2368 else if (match_accessibility (type, type_index, acc))
2375 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2377 if (cvalue && value)
2378 *cvalue = value_struct_element_index (value, type_index);
2381 *ctype = TYPE_FIELD_TYPE (type, type_index);
2383 else if (index < TYPE_N_BASECLASSES (type))
2385 /* This is a baseclass. */
2387 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2389 if (cvalue && value)
2391 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
2396 *ctype = TYPE_FIELD_TYPE (type, index);
2401 char *access = NULL;
2403 cplus_class_num_children (type, children);
2405 /* Everything beyond the baseclasses can
2406 only be "public", "private", or "protected"
2408 The special "fake" children are always output by varobj in
2409 this order. So if INDEX == 2, it MUST be "protected". */
2410 index -= TYPE_N_BASECLASSES (type);
2414 if (children[v_public] > 0)
2416 else if (children[v_private] > 0)
2419 access = "protected";
2422 if (children[v_public] > 0)
2424 if (children[v_private] > 0)
2427 access = "protected";
2429 else if (children[v_private] > 0)
2430 access = "protected";
2433 /* Must be protected */
2434 access = "protected";
2441 gdb_assert (access);
2443 *cname = xstrdup (access);
2445 /* Value and type are null here. */
2450 c_describe_child (parent, index, cname, cvalue, ctype);
2455 cplus_name_of_child (struct varobj *parent, int index)
2458 cplus_describe_child (parent, index, &name, NULL, NULL);
2462 static struct value *
2463 cplus_value_of_root (struct varobj **var_handle)
2465 return c_value_of_root (var_handle);
2468 static struct value *
2469 cplus_value_of_child (struct varobj *parent, int index)
2471 struct value *value = NULL;
2472 cplus_describe_child (parent, index, NULL, &value, NULL);
2476 static struct type *
2477 cplus_type_of_child (struct varobj *parent, int index)
2479 struct type *type = NULL;
2480 cplus_describe_child (parent, index, NULL, NULL, &type);
2485 cplus_variable_editable (struct varobj *var)
2487 if (CPLUS_FAKE_CHILD (var))
2490 return c_variable_editable (var);
2494 cplus_value_of_variable (struct varobj *var)
2497 /* If we have one of our special types, don't print out
2499 if (CPLUS_FAKE_CHILD (var))
2500 return xstrdup ("");
2502 return c_value_of_variable (var);
2508 java_number_of_children (struct varobj *var)
2510 return cplus_number_of_children (var);
2514 java_name_of_variable (struct varobj *parent)
2518 name = cplus_name_of_variable (parent);
2519 /* If the name has "-" in it, it is because we
2520 needed to escape periods in the name... */
2523 while (*p != '\000')
2534 java_name_of_child (struct varobj *parent, int index)
2538 name = cplus_name_of_child (parent, index);
2539 /* Escape any periods in the name... */
2542 while (*p != '\000')
2552 static struct value *
2553 java_value_of_root (struct varobj **var_handle)
2555 return cplus_value_of_root (var_handle);
2558 static struct value *
2559 java_value_of_child (struct varobj *parent, int index)
2561 return cplus_value_of_child (parent, index);
2564 static struct type *
2565 java_type_of_child (struct varobj *parent, int index)
2567 return cplus_type_of_child (parent, index);
2571 java_variable_editable (struct varobj *var)
2573 return cplus_variable_editable (var);
2577 java_value_of_variable (struct varobj *var)
2579 return cplus_value_of_variable (var);
2582 extern void _initialize_varobj (void);
2584 _initialize_varobj (void)
2586 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2588 varobj_table = xmalloc (sizeof_table);
2589 memset (varobj_table, 0, sizeof_table);
2591 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2593 Set varobj debugging."), _("\
2594 Show varobj debugging."), _("\
2595 When non-zero, varobj debugging is enabled."),
2598 &setlist, &showlist);
2601 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2602 are defined on globals.
2603 Invalidated varobjs will be always printed in_scope="invalid". */
2605 varobj_invalidate (void)
2607 struct varobj **all_rootvarobj;
2608 struct varobj **varp;
2610 if (varobj_list (&all_rootvarobj) > 0)
2612 varp = all_rootvarobj;
2613 while (*varp != NULL)
2615 /* global var must be re-evaluated. */
2616 if ((*varp)->root->valid_block == NULL)
2618 struct varobj *tmp_var;
2620 /* Try to create a varobj with same expression. If we succeed replace
2621 the old varobj, otherwise invalidate it. */
2622 tmp_var = varobj_create (NULL, (*varp)->name, (CORE_ADDR) 0, USE_CURRENT_FRAME);
2623 if (tmp_var != NULL)
2625 tmp_var->obj_name = xstrdup ((*varp)->obj_name);
2626 varobj_delete (*varp, NULL, 0);
2627 install_variable (tmp_var);
2630 (*varp)->root->is_valid = 0;
2632 else /* locals must be invalidated. */
2633 (*varp)->root->is_valid = 0;
2637 xfree (all_rootvarobj);