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 3 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, see <http://www.gnu.org/licenses/>. */
20 #include "exceptions.h"
22 #include "expression.h"
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
35 /* Non-zero if we want to see trace of varobj level stuff. */
39 show_varobjdebug (struct ui_file
*file
, int from_tty
,
40 struct cmd_list_element
*c
, const char *value
)
42 fprintf_filtered (file
, _("Varobj debugging is %s.\n"), value
);
45 /* String representations of gdb's format codes */
46 char *varobj_format_string
[] =
47 { "natural", "binary", "decimal", "hexadecimal", "octal" };
49 /* String representations of gdb's known languages */
50 char *varobj_language_string
[] = { "unknown", "C", "C++", "Java" };
54 /* Every root variable has one of these structures saved in its
55 varobj. Members which must be free'd are noted. */
59 /* Alloc'd expression for this parent. */
60 struct expression
*exp
;
62 /* Block for which this expression is valid */
63 struct block
*valid_block
;
65 /* The frame for this expression */
66 struct frame_id frame
;
68 /* If 1, "update" always recomputes the frame & valid block
69 using the currently selected frame. */
70 int use_selected_frame
;
72 /* Flag that indicates validity: set to 0 when this varobj_root refers
73 to symbols that do not exist anymore. */
76 /* Language info for this variable and its children */
77 struct language_specific
*lang
;
79 /* The varobj for this root node. */
80 struct varobj
*rootvar
;
82 /* Next root variable */
83 struct varobj_root
*next
;
86 typedef struct varobj
*varobj_p
;
90 /* Every variable in the system has a structure of this type defined
91 for it. This structure holds all information necessary to manipulate
92 a particular object variable. Members which must be freed are noted. */
96 /* Alloc'd name of the variable for this object.. If this variable is a
97 child, then this name will be the child's source name.
99 /* NOTE: This is the "expression" */
102 /* The alloc'd name for this variable's object. This is here for
103 convenience when constructing this object's children. */
106 /* Index of this variable in its parent or -1 */
109 /* The type of this variable. This may NEVER be NULL. */
112 /* The value of this expression or subexpression. A NULL value
113 indicates there was an error getting this value.
114 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
115 the value is either NULL, or not lazy. */
118 /* The number of (immediate) children this variable has */
121 /* If this object is a child, this points to its immediate parent. */
122 struct varobj
*parent
;
124 /* Children of this object. */
125 VEC (varobj_p
) *children
;
127 /* Description of the root variable. Points to root variable for children. */
128 struct varobj_root
*root
;
130 /* The format of the output for this object */
131 enum varobj_display_formats format
;
133 /* Was this variable updated via a varobj_set_value operation */
136 /* Last print value. */
139 /* Is this variable frozen. Frozen variables are never implicitly
140 updated by -var-update *
141 or -var-update <direct-or-indirect-parent>. */
144 /* Is the value of this variable intentionally not fetched? It is
145 not fetched if either the variable is frozen, or any parents is
153 struct cpstack
*next
;
156 /* A list of varobjs */
164 /* Private function prototypes */
166 /* Helper functions for the above subcommands. */
168 static int delete_variable (struct cpstack
**, struct varobj
*, int);
170 static void delete_variable_1 (struct cpstack
**, int *,
171 struct varobj
*, int, int);
173 static int install_variable (struct varobj
*);
175 static void uninstall_variable (struct varobj
*);
177 static struct varobj
*create_child (struct varobj
*, int, char *);
179 /* Utility routines */
181 static struct varobj
*new_variable (void);
183 static struct varobj
*new_root_variable (void);
185 static void free_variable (struct varobj
*var
);
187 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
189 static struct type
*get_type (struct varobj
*var
);
191 static struct type
*get_value_type (struct varobj
*var
);
193 static struct type
*get_target_type (struct type
*);
195 static enum varobj_display_formats
variable_default_display (struct varobj
*);
197 static void cppush (struct cpstack
**pstack
, char *name
);
199 static char *cppop (struct cpstack
**pstack
);
201 static int install_new_value (struct varobj
*var
, struct value
*value
,
204 /* Language-specific routines. */
206 static enum varobj_languages
variable_language (struct varobj
*var
);
208 static int number_of_children (struct varobj
*);
210 static char *name_of_variable (struct varobj
*);
212 static char *name_of_child (struct varobj
*, int);
214 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
216 static struct value
*value_of_child (struct varobj
*parent
, int index
);
218 static int variable_editable (struct varobj
*var
);
220 static char *my_value_of_variable (struct varobj
*var
);
222 static char *value_get_print_value (struct value
*value
,
223 enum varobj_display_formats format
);
225 static int varobj_value_is_changeable_p (struct varobj
*var
);
227 static int is_root_p (struct varobj
*var
);
229 /* C implementation */
231 static int c_number_of_children (struct varobj
*var
);
233 static char *c_name_of_variable (struct varobj
*parent
);
235 static char *c_name_of_child (struct varobj
*parent
, int index
);
237 static struct value
*c_value_of_root (struct varobj
**var_handle
);
239 static struct value
*c_value_of_child (struct varobj
*parent
, int index
);
241 static struct type
*c_type_of_child (struct varobj
*parent
, int index
);
243 static int c_variable_editable (struct varobj
*var
);
245 static char *c_value_of_variable (struct varobj
*var
);
247 /* C++ implementation */
249 static int cplus_number_of_children (struct varobj
*var
);
251 static void cplus_class_num_children (struct type
*type
, int children
[3]);
253 static char *cplus_name_of_variable (struct varobj
*parent
);
255 static char *cplus_name_of_child (struct varobj
*parent
, int index
);
257 static struct value
*cplus_value_of_root (struct varobj
**var_handle
);
259 static struct value
*cplus_value_of_child (struct varobj
*parent
, int index
);
261 static struct type
*cplus_type_of_child (struct varobj
*parent
, int index
);
263 static int cplus_variable_editable (struct varobj
*var
);
265 static char *cplus_value_of_variable (struct varobj
*var
);
267 /* Java implementation */
269 static int java_number_of_children (struct varobj
*var
);
271 static char *java_name_of_variable (struct varobj
*parent
);
273 static char *java_name_of_child (struct varobj
*parent
, int index
);
275 static struct value
*java_value_of_root (struct varobj
**var_handle
);
277 static struct value
*java_value_of_child (struct varobj
*parent
, int index
);
279 static struct type
*java_type_of_child (struct varobj
*parent
, int index
);
281 static int java_variable_editable (struct varobj
*var
);
283 static char *java_value_of_variable (struct varobj
*var
);
285 /* The language specific vector */
287 struct language_specific
290 /* The language of this variable */
291 enum varobj_languages language
;
293 /* The number of children of PARENT. */
294 int (*number_of_children
) (struct varobj
* parent
);
296 /* The name (expression) of a root varobj. */
297 char *(*name_of_variable
) (struct varobj
* parent
);
299 /* The name of the INDEX'th child of PARENT. */
300 char *(*name_of_child
) (struct varobj
* parent
, int index
);
302 /* The ``struct value *'' of the root variable ROOT. */
303 struct value
*(*value_of_root
) (struct varobj
** root_handle
);
305 /* The ``struct value *'' of the INDEX'th child of PARENT. */
306 struct value
*(*value_of_child
) (struct varobj
* parent
, int index
);
308 /* The type of the INDEX'th child of PARENT. */
309 struct type
*(*type_of_child
) (struct varobj
* parent
, int index
);
311 /* Is VAR editable? */
312 int (*variable_editable
) (struct varobj
* var
);
314 /* The current value of VAR. */
315 char *(*value_of_variable
) (struct varobj
* var
);
318 /* Array of known source language routines. */
319 static struct language_specific languages
[vlang_end
] = {
320 /* Unknown (try treating as C */
323 c_number_of_children
,
335 c_number_of_children
,
347 cplus_number_of_children
,
348 cplus_name_of_variable
,
351 cplus_value_of_child
,
353 cplus_variable_editable
,
354 cplus_value_of_variable
}
359 java_number_of_children
,
360 java_name_of_variable
,
365 java_variable_editable
,
366 java_value_of_variable
}
369 /* A little convenience enum for dealing with C++/Java */
372 v_public
= 0, v_private
, v_protected
377 /* Mappings of varobj_display_formats enums to gdb's format codes */
378 static int format_code
[] = { 0, 't', 'd', 'x', 'o' };
380 /* Header of the list of root variable objects */
381 static struct varobj_root
*rootlist
;
382 static int rootcount
= 0; /* number of root varobjs in the list */
384 /* Prime number indicating the number of buckets in the hash table */
385 /* A prime large enough to avoid too many colisions */
386 #define VAROBJ_TABLE_SIZE 227
388 /* Pointer to the varobj hash table (built at run time) */
389 static struct vlist
**varobj_table
;
391 /* Is the variable X one of our "fake" children? */
392 #define CPLUS_FAKE_CHILD(x) \
393 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
396 /* API Implementation */
398 is_root_p (struct varobj
*var
)
400 return (var
->root
->rootvar
== var
);
403 /* Creates a varobj (not its children) */
405 /* Return the full FRAME which corresponds to the given CORE_ADDR
406 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
408 static struct frame_info
*
409 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
411 struct frame_info
*frame
= NULL
;
413 if (frame_addr
== (CORE_ADDR
) 0)
418 frame
= get_prev_frame (frame
);
421 if (get_frame_base_address (frame
) == frame_addr
)
427 varobj_create (char *objname
,
428 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
431 struct frame_info
*fi
;
432 struct frame_info
*old_fi
= NULL
;
434 struct cleanup
*old_chain
;
436 /* Fill out a varobj structure for the (root) variable being constructed. */
437 var
= new_root_variable ();
438 old_chain
= make_cleanup_free_variable (var
);
440 if (expression
!= NULL
)
443 enum varobj_languages lang
;
444 struct value
*value
= NULL
;
446 /* Parse and evaluate the expression, filling in as much
447 of the variable's data as possible */
449 /* Allow creator to specify context of variable */
450 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
451 fi
= deprecated_safe_get_selected_frame ();
453 /* FIXME: cagney/2002-11-23: This code should be doing a
454 lookup using the frame ID and not just the frame's
455 ``address''. This, of course, means an interface change.
456 However, with out that interface change ISAs, such as the
457 ia64 with its two stacks, won't work. Similar goes for the
458 case where there is a frameless function. */
459 fi
= find_frame_addr_in_frame_chain (frame
);
461 /* frame = -2 means always use selected frame */
462 if (type
== USE_SELECTED_FRAME
)
463 var
->root
->use_selected_frame
= 1;
467 block
= get_frame_block (fi
, 0);
470 innermost_block
= NULL
;
471 /* Wrap the call to parse expression, so we can
472 return a sensible error. */
473 if (!gdb_parse_exp_1 (&p
, block
, 0, &var
->root
->exp
))
478 /* Don't allow variables to be created for types. */
479 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
)
481 do_cleanups (old_chain
);
482 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
483 " as an expression.\n");
487 var
->format
= variable_default_display (var
);
488 var
->root
->valid_block
= innermost_block
;
489 var
->name
= savestring (expression
, strlen (expression
));
491 /* When the frame is different from the current frame,
492 we must select the appropriate frame before parsing
493 the expression, otherwise the value will not be current.
494 Since select_frame is so benign, just call it for all cases. */
497 var
->root
->frame
= get_frame_id (fi
);
498 old_fi
= get_selected_frame (NULL
);
502 /* We definitively need to catch errors here.
503 If evaluate_expression succeeds we got the value we wanted.
504 But if it fails, we still go on with a call to evaluate_type() */
505 if (!gdb_evaluate_expression (var
->root
->exp
, &value
))
507 /* Error getting the value. Try to at least get the
509 struct value
*type_only_value
= evaluate_type (var
->root
->exp
);
510 var
->type
= value_type (type_only_value
);
513 var
->type
= value_type (value
);
515 install_new_value (var
, value
, 1 /* Initial assignment */);
517 /* Set language info */
518 lang
= variable_language (var
);
519 var
->root
->lang
= &languages
[lang
];
521 /* Set ourselves as our root */
522 var
->root
->rootvar
= var
;
524 /* Reset the selected frame */
526 select_frame (old_fi
);
529 /* If the variable object name is null, that means this
530 is a temporary variable, so don't install it. */
532 if ((var
!= NULL
) && (objname
!= NULL
))
534 var
->obj_name
= savestring (objname
, strlen (objname
));
536 /* If a varobj name is duplicated, the install will fail so
538 if (!install_variable (var
))
540 do_cleanups (old_chain
);
545 discard_cleanups (old_chain
);
549 /* Generates an unique name that can be used for a varobj */
552 varobj_gen_name (void)
557 /* generate a name for this object */
559 obj_name
= xstrprintf ("var%d", id
);
564 /* Given an "objname", returns the pointer to the corresponding varobj
565 or NULL if not found */
568 varobj_get_handle (char *objname
)
572 unsigned int index
= 0;
575 for (chp
= objname
; *chp
; chp
++)
577 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
580 cv
= *(varobj_table
+ index
);
581 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
585 error (_("Variable object not found"));
590 /* Given the handle, return the name of the object */
593 varobj_get_objname (struct varobj
*var
)
595 return var
->obj_name
;
598 /* Given the handle, return the expression represented by the object */
601 varobj_get_expression (struct varobj
*var
)
603 return name_of_variable (var
);
606 /* Deletes a varobj and all its children if only_children == 0,
607 otherwise deletes only the children; returns a malloc'ed list of all the
608 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
611 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
615 struct cpstack
*result
= NULL
;
618 /* Initialize a stack for temporary results */
619 cppush (&result
, NULL
);
622 /* Delete only the variable children */
623 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
625 /* Delete the variable and all its children */
626 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
628 /* We may have been asked to return a list of what has been deleted */
631 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
635 *cp
= cppop (&result
);
636 while ((*cp
!= NULL
) && (mycount
> 0))
640 *cp
= cppop (&result
);
643 if (mycount
|| (*cp
!= NULL
))
644 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
651 /* Set/Get variable object display format */
653 enum varobj_display_formats
654 varobj_set_display_format (struct varobj
*var
,
655 enum varobj_display_formats format
)
662 case FORMAT_HEXADECIMAL
:
664 var
->format
= format
;
668 var
->format
= variable_default_display (var
);
674 enum varobj_display_formats
675 varobj_get_display_format (struct varobj
*var
)
681 varobj_set_frozen (struct varobj
*var
, int frozen
)
683 /* When a variable is unfrozen, we don't fetch its value.
684 The 'not_fetched' flag remains set, so next -var-update
687 We don't fetch the value, because for structures the client
688 should do -var-update anyway. It would be bad to have different
689 client-size logic for structure and other types. */
690 var
->frozen
= frozen
;
694 varobj_get_frozen (struct varobj
*var
)
701 varobj_get_num_children (struct varobj
*var
)
703 if (var
->num_children
== -1)
704 var
->num_children
= number_of_children (var
);
706 return var
->num_children
;
709 /* Creates a list of the immediate children of a variable object;
710 the return code is the number of such children or -1 on error */
713 varobj_list_children (struct varobj
*var
, struct varobj
***childlist
)
715 struct varobj
*child
;
719 /* sanity check: have we been passed a pointer? */
720 if (childlist
== NULL
)
725 if (var
->num_children
== -1)
726 var
->num_children
= number_of_children (var
);
728 /* If that failed, give up. */
729 if (var
->num_children
== -1)
732 /* If we're called when the list of children is not yet initialized,
733 allocate enough elements in it. */
734 while (VEC_length (varobj_p
, var
->children
) < var
->num_children
)
735 VEC_safe_push (varobj_p
, var
->children
, NULL
);
737 /* List of children */
738 *childlist
= xmalloc ((var
->num_children
+ 1) * sizeof (struct varobj
*));
740 for (i
= 0; i
< var
->num_children
; i
++)
744 /* Mark as the end in case we bail out */
745 *((*childlist
) + i
) = NULL
;
747 existing
= VEC_index (varobj_p
, var
->children
, i
);
749 if (existing
== NULL
)
751 /* Either it's the first call to varobj_list_children for
752 this variable object, and the child was never created,
753 or it was explicitly deleted by the client. */
754 name
= name_of_child (var
, i
);
755 existing
= create_child (var
, i
, name
);
756 VEC_replace (varobj_p
, var
->children
, i
, existing
);
759 *((*childlist
) + i
) = existing
;
762 /* End of list is marked by a NULL pointer */
763 *((*childlist
) + i
) = NULL
;
765 return var
->num_children
;
768 /* Obtain the type of an object Variable as a string similar to the one gdb
769 prints on the console */
772 varobj_get_type (struct varobj
*var
)
775 struct cleanup
*old_chain
;
780 /* For the "fake" variables, do not return a type. (It's type is
782 Do not return a type for invalid variables as well. */
783 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
786 stb
= mem_fileopen ();
787 old_chain
= make_cleanup_ui_file_delete (stb
);
789 /* To print the type, we simply create a zero ``struct value *'' and
790 cast it to our type. We then typeprint this variable. */
791 val
= value_zero (var
->type
, not_lval
);
792 type_print (value_type (val
), "", stb
, -1);
794 thetype
= ui_file_xstrdup (stb
, &length
);
795 do_cleanups (old_chain
);
799 /* Obtain the type of an object variable. */
802 varobj_get_gdb_type (struct varobj
*var
)
807 enum varobj_languages
808 varobj_get_language (struct varobj
*var
)
810 return variable_language (var
);
814 varobj_get_attributes (struct varobj
*var
)
818 if (var
->root
->is_valid
&& variable_editable (var
))
819 /* FIXME: define masks for attributes */
820 attributes
|= 0x00000001; /* Editable */
826 varobj_get_value (struct varobj
*var
)
828 return my_value_of_variable (var
);
831 /* Set the value of an object variable (if it is editable) to the
832 value of the given expression */
833 /* Note: Invokes functions that can call error() */
836 varobj_set_value (struct varobj
*var
, char *expression
)
842 /* The argument "expression" contains the variable's new value.
843 We need to first construct a legal expression for this -- ugh! */
844 /* Does this cover all the bases? */
845 struct expression
*exp
;
847 int saved_input_radix
= input_radix
;
849 if (var
->value
!= NULL
&& variable_editable (var
))
851 char *s
= expression
;
854 input_radix
= 10; /* ALWAYS reset to decimal temporarily */
855 exp
= parse_exp_1 (&s
, 0, 0);
856 if (!gdb_evaluate_expression (exp
, &value
))
858 /* We cannot proceed without a valid expression. */
863 /* All types that are editable must also be changeable. */
864 gdb_assert (varobj_value_is_changeable_p (var
));
866 /* The value of a changeable variable object must not be lazy. */
867 gdb_assert (!value_lazy (var
->value
));
869 /* Need to coerce the input. We want to check if the
870 value of the variable object will be different
871 after assignment, and the first thing value_assign
872 does is coerce the input.
873 For example, if we are assigning an array to a pointer variable we
874 should compare the pointer with the the array's address, not with the
876 value
= coerce_array (value
);
878 /* The new value may be lazy. gdb_value_assign, or
879 rather value_contents, will take care of this.
880 If fetching of the new value will fail, gdb_value_assign
881 with catch the exception. */
882 if (!gdb_value_assign (var
->value
, value
, &val
))
885 /* If the value has changed, record it, so that next -var-update can
886 report this change. If a variable had a value of '1', we've set it
887 to '333' and then set again to '1', when -var-update will report this
888 variable as changed -- because the first assignment has set the
889 'updated' flag. There's no need to optimize that, because return value
890 of -var-update should be considered an approximation. */
891 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
892 input_radix
= saved_input_radix
;
899 /* Returns a malloc'ed list with all root variable objects */
901 varobj_list (struct varobj
***varlist
)
904 struct varobj_root
*croot
;
905 int mycount
= rootcount
;
907 /* Alloc (rootcount + 1) entries for the result */
908 *varlist
= xmalloc ((rootcount
+ 1) * sizeof (struct varobj
*));
912 while ((croot
!= NULL
) && (mycount
> 0))
914 *cv
= croot
->rootvar
;
919 /* Mark the end of the list */
922 if (mycount
|| (croot
!= NULL
))
924 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
930 /* Assign a new value to a variable object. If INITIAL is non-zero,
931 this is the first assignement after the variable object was just
932 created, or changed type. In that case, just assign the value
934 Otherwise, assign the value and if type_changeable returns non-zero,
935 find if the new value is different from the current value.
936 Return 1 if so, and 0 if the values are equal.
938 The VALUE parameter should not be released -- the function will
939 take care of releasing it when needed. */
941 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
946 int intentionally_not_fetched
= 0;
948 /* We need to know the varobj's type to decide if the value should
949 be fetched or not. C++ fake children (public/protected/private) don't have
951 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
952 changeable
= varobj_value_is_changeable_p (var
);
953 need_to_fetch
= changeable
;
955 /* We are not interested in the address of references, and given
956 that in C++ a reference is not rebindable, it cannot
957 meaningfully change. So, get hold of the real value. */
960 value
= coerce_ref (value
);
961 release_value (value
);
964 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
965 /* For unions, we need to fetch the value implicitly because
966 of implementation of union member fetch. When gdb
967 creates a value for a field and the value of the enclosing
968 structure is not lazy, it immediately copies the necessary
969 bytes from the enclosing values. If the enclosing value is
970 lazy, the call to value_fetch_lazy on the field will read
971 the data from memory. For unions, that means we'll read the
972 same memory more than once, which is not desirable. So
976 /* The new value might be lazy. If the type is changeable,
977 that is we'll be comparing values of this type, fetch the
978 value now. Otherwise, on the next update the old value
979 will be lazy, which means we've lost that old value. */
980 if (need_to_fetch
&& value
&& value_lazy (value
))
982 struct varobj
*parent
= var
->parent
;
983 int frozen
= var
->frozen
;
984 for (; !frozen
&& parent
; parent
= parent
->parent
)
985 frozen
|= parent
->frozen
;
987 if (frozen
&& initial
)
989 /* For variables that are frozen, or are children of frozen
990 variables, we don't do fetch on initial assignment.
991 For non-initial assignemnt we do the fetch, since it means we're
992 explicitly asked to compare the new value with the old one. */
993 intentionally_not_fetched
= 1;
995 else if (!gdb_value_fetch_lazy (value
))
997 /* Set the value to NULL, so that for the next -var-update,
998 we don't try to compare the new value with this value,
999 that we couldn't even read. */
1004 /* If the type is changeable, compare the old and the new values.
1005 If this is the initial assignment, we don't have any old value
1007 if (initial
&& changeable
)
1008 var
->print_value
= value_get_print_value (value
, var
->format
);
1009 else if (changeable
)
1011 /* If the value of the varobj was changed by -var-set-value, then the
1012 value in the varobj and in the target is the same. However, that value
1013 is different from the value that the varobj had after the previous
1014 -var-update. So need to the varobj as changed. */
1017 xfree (var
->print_value
);
1018 var
->print_value
= value_get_print_value (value
, var
->format
);
1023 /* Try to compare the values. That requires that both
1024 values are non-lazy. */
1025 if (var
->not_fetched
&& value_lazy (var
->value
))
1027 /* This is a frozen varobj and the value was never read.
1028 Presumably, UI shows some "never read" indicator.
1029 Now that we've fetched the real value, we need to report
1030 this varobj as changed so that UI can show the real
1034 else if (var
->value
== NULL
&& value
== NULL
)
1037 else if (var
->value
== NULL
|| value
== NULL
)
1039 xfree (var
->print_value
);
1040 var
->print_value
= value_get_print_value (value
, var
->format
);
1046 gdb_assert (!value_lazy (var
->value
));
1047 gdb_assert (!value_lazy (value
));
1048 print_value
= value_get_print_value (value
, var
->format
);
1050 gdb_assert (var
->print_value
!= NULL
&& print_value
!= NULL
);
1051 if (strcmp (var
->print_value
, print_value
) != 0)
1053 xfree (var
->print_value
);
1054 var
->print_value
= print_value
;
1058 xfree (print_value
);
1063 /* We must always keep the new value, since children depend on it. */
1064 if (var
->value
!= NULL
&& var
->value
!= value
)
1065 value_free (var
->value
);
1067 if (value
&& value_lazy (value
) && intentionally_not_fetched
)
1068 var
->not_fetched
= 1;
1070 var
->not_fetched
= 0;
1073 gdb_assert (!var
->value
|| value_type (var
->value
));
1078 /* Update the values for a variable and its children. This is a
1079 two-pronged attack. First, re-parse the value for the root's
1080 expression to see if it's changed. Then go all the way
1081 through its children, reconstructing them and noting if they've
1084 < 0 for error values, see varobj.h.
1085 Otherwise it is the number of children + parent changed.
1087 The EXPLICIT parameter specifies if this call is result
1088 of MI request to update this specific variable, or
1089 result of implicit -var-update *. For implicit request, we don't
1090 update frozen variables.
1092 NOTE: This function may delete the caller's varobj. If it
1093 returns TYPE_CHANGED, then it has done this and VARP will be modified
1094 to point to the new varobj. */
1097 varobj_update (struct varobj
**varp
, struct varobj
***changelist
,
1101 int type_changed
= 0;
1106 struct varobj
**templist
= NULL
;
1108 VEC (varobj_p
) *stack
= NULL
;
1109 VEC (varobj_p
) *result
= NULL
;
1110 struct frame_id old_fid
;
1111 struct frame_info
*fi
;
1113 /* sanity check: have we been passed a pointer? */
1114 gdb_assert (changelist
);
1116 /* Frozen means frozen -- we don't check for any change in
1117 this varobj, including its going out of scope, or
1118 changing type. One use case for frozen varobjs is
1119 retaining previously evaluated expressions, and we don't
1120 want them to be reevaluated at all. */
1121 if (!explicit && (*varp
)->frozen
)
1124 if (!(*varp
)->root
->is_valid
)
1127 if ((*varp
)->root
->rootvar
== *varp
)
1129 /* Save the selected stack frame, since we will need to change it
1130 in order to evaluate expressions. */
1131 old_fid
= get_frame_id (deprecated_safe_get_selected_frame ());
1133 /* Update the root variable. value_of_root can return NULL
1134 if the variable is no longer around, i.e. we stepped out of
1135 the frame in which a local existed. We are letting the
1136 value_of_root variable dispose of the varobj if the type
1139 new = value_of_root (varp
, &type_changed
);
1141 /* Restore selected frame. */
1142 fi
= frame_find_by_id (old_fid
);
1146 /* If this is a "use_selected_frame" varobj, and its type has changed,
1147 them note that it's changed. */
1149 VEC_safe_push (varobj_p
, result
, *varp
);
1151 if (install_new_value ((*varp
), new, type_changed
))
1153 /* If type_changed is 1, install_new_value will never return
1154 non-zero, so we'll never report the same variable twice. */
1155 gdb_assert (!type_changed
);
1156 VEC_safe_push (varobj_p
, result
, *varp
);
1161 /* This means the varobj itself is out of scope.
1163 VEC_free (varobj_p
, result
);
1164 return NOT_IN_SCOPE
;
1168 VEC_safe_push (varobj_p
, stack
, *varp
);
1170 /* Walk through the children, reconstructing them all. */
1171 while (!VEC_empty (varobj_p
, stack
))
1173 v
= VEC_pop (varobj_p
, stack
);
1175 /* Push any children. Use reverse order so that the first
1176 child is popped from the work stack first, and so
1177 will be added to result first. This does not
1178 affect correctness, just "nicer". */
1179 for (i
= VEC_length (varobj_p
, v
->children
)-1; i
>= 0; --i
)
1181 varobj_p c
= VEC_index (varobj_p
, v
->children
, i
);
1182 /* Child may be NULL if explicitly deleted by -var-delete. */
1183 if (c
!= NULL
&& !c
->frozen
)
1184 VEC_safe_push (varobj_p
, stack
, c
);
1187 /* Update this variable, unless it's a root, which is already
1189 if (v
->root
->rootvar
!= v
)
1191 new = value_of_child (v
->parent
, v
->index
);
1192 if (install_new_value (v
, new, 0 /* type not changed */))
1194 /* Note that it's changed */
1195 VEC_safe_push (varobj_p
, result
, v
);
1201 /* Alloc (changed + 1) list entries. */
1202 changed
= VEC_length (varobj_p
, result
);
1203 *changelist
= xmalloc ((changed
+ 1) * sizeof (struct varobj
*));
1206 for (i
= 0; i
< changed
; ++i
)
1208 *cv
= VEC_index (varobj_p
, result
, i
);
1209 gdb_assert (*cv
!= NULL
);
1214 VEC_free (varobj_p
, stack
);
1215 VEC_free (varobj_p
, result
);
1218 return TYPE_CHANGED
;
1224 /* Helper functions */
1227 * Variable object construction/destruction
1231 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
1232 int only_children_p
)
1236 delete_variable_1 (resultp
, &delcount
, var
,
1237 only_children_p
, 1 /* remove_from_parent_p */ );
1242 /* Delete the variable object VAR and its children */
1243 /* IMPORTANT NOTE: If we delete a variable which is a child
1244 and the parent is not removed we dump core. It must be always
1245 initially called with remove_from_parent_p set */
1247 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
1248 struct varobj
*var
, int only_children_p
,
1249 int remove_from_parent_p
)
1253 /* Delete any children of this variable, too. */
1254 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
1256 varobj_p child
= VEC_index (varobj_p
, var
->children
, i
);
1257 if (!remove_from_parent_p
)
1258 child
->parent
= NULL
;
1259 delete_variable_1 (resultp
, delcountp
, child
, 0, only_children_p
);
1261 VEC_free (varobj_p
, var
->children
);
1263 /* if we were called to delete only the children we are done here */
1264 if (only_children_p
)
1267 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1268 /* If the name is null, this is a temporary variable, that has not
1269 yet been installed, don't report it, it belongs to the caller... */
1270 if (var
->obj_name
!= NULL
)
1272 cppush (resultp
, xstrdup (var
->obj_name
));
1273 *delcountp
= *delcountp
+ 1;
1276 /* If this variable has a parent, remove it from its parent's list */
1277 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1278 (as indicated by remove_from_parent_p) we don't bother doing an
1279 expensive list search to find the element to remove when we are
1280 discarding the list afterwards */
1281 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1283 VEC_replace (varobj_p
, var
->parent
->children
, var
->index
, NULL
);
1286 if (var
->obj_name
!= NULL
)
1287 uninstall_variable (var
);
1289 /* Free memory associated with this variable */
1290 free_variable (var
);
1293 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1295 install_variable (struct varobj
*var
)
1298 struct vlist
*newvl
;
1300 unsigned int index
= 0;
1303 for (chp
= var
->obj_name
; *chp
; chp
++)
1305 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1308 cv
= *(varobj_table
+ index
);
1309 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1313 error (_("Duplicate variable object name"));
1315 /* Add varobj to hash table */
1316 newvl
= xmalloc (sizeof (struct vlist
));
1317 newvl
->next
= *(varobj_table
+ index
);
1319 *(varobj_table
+ index
) = newvl
;
1321 /* If root, add varobj to root list */
1322 if (is_root_p (var
))
1324 /* Add to list of root variables */
1325 if (rootlist
== NULL
)
1326 var
->root
->next
= NULL
;
1328 var
->root
->next
= rootlist
;
1329 rootlist
= var
->root
;
1336 /* Unistall the object VAR. */
1338 uninstall_variable (struct varobj
*var
)
1342 struct varobj_root
*cr
;
1343 struct varobj_root
*prer
;
1345 unsigned int index
= 0;
1348 /* Remove varobj from hash table */
1349 for (chp
= var
->obj_name
; *chp
; chp
++)
1351 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1354 cv
= *(varobj_table
+ index
);
1356 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1363 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
1368 ("Assertion failed: Could not find variable object \"%s\" to delete",
1374 *(varobj_table
+ index
) = cv
->next
;
1376 prev
->next
= cv
->next
;
1380 /* If root, remove varobj from root list */
1381 if (is_root_p (var
))
1383 /* Remove from list of root variables */
1384 if (rootlist
== var
->root
)
1385 rootlist
= var
->root
->next
;
1390 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
1398 ("Assertion failed: Could not find varobj \"%s\" in root list",
1405 prer
->next
= cr
->next
;
1412 /* Create and install a child of the parent of the given name */
1413 static struct varobj
*
1414 create_child (struct varobj
*parent
, int index
, char *name
)
1416 struct varobj
*child
;
1418 struct value
*value
;
1420 child
= new_variable ();
1422 /* name is allocated by name_of_child */
1424 child
->index
= index
;
1425 value
= value_of_child (parent
, index
);
1426 child
->parent
= parent
;
1427 child
->root
= parent
->root
;
1428 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, name
);
1429 child
->obj_name
= childs_name
;
1430 install_variable (child
);
1432 /* Compute the type of the child. Must do this before
1433 calling install_new_value. */
1435 /* If the child had no evaluation errors, var->value
1436 will be non-NULL and contain a valid type. */
1437 child
->type
= value_type (value
);
1439 /* Otherwise, we must compute the type. */
1440 child
->type
= (*child
->root
->lang
->type_of_child
) (child
->parent
,
1442 install_new_value (child
, value
, 1);
1449 * Miscellaneous utility functions.
1452 /* Allocate memory and initialize a new variable */
1453 static struct varobj
*
1458 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
1460 var
->obj_name
= NULL
;
1464 var
->num_children
= -1;
1466 var
->children
= NULL
;
1470 var
->print_value
= NULL
;
1472 var
->not_fetched
= 0;
1477 /* Allocate memory and initialize a new root variable */
1478 static struct varobj
*
1479 new_root_variable (void)
1481 struct varobj
*var
= new_variable ();
1482 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));;
1483 var
->root
->lang
= NULL
;
1484 var
->root
->exp
= NULL
;
1485 var
->root
->valid_block
= NULL
;
1486 var
->root
->frame
= null_frame_id
;
1487 var
->root
->use_selected_frame
= 0;
1488 var
->root
->rootvar
= NULL
;
1489 var
->root
->is_valid
= 1;
1494 /* Free any allocated memory associated with VAR. */
1496 free_variable (struct varobj
*var
)
1498 /* Free the expression if this is a root variable. */
1499 if (is_root_p (var
))
1501 free_current_contents (&var
->root
->exp
);
1506 xfree (var
->obj_name
);
1507 xfree (var
->print_value
);
1512 do_free_variable_cleanup (void *var
)
1514 free_variable (var
);
1517 static struct cleanup
*
1518 make_cleanup_free_variable (struct varobj
*var
)
1520 return make_cleanup (do_free_variable_cleanup
, var
);
1523 /* This returns the type of the variable. It also skips past typedefs
1524 to return the real type of the variable.
1526 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1527 except within get_target_type and get_type. */
1528 static struct type
*
1529 get_type (struct varobj
*var
)
1535 type
= check_typedef (type
);
1540 /* Return the type of the value that's stored in VAR,
1541 or that would have being stored there if the
1542 value were accessible.
1544 This differs from VAR->type in that VAR->type is always
1545 the true type of the expession in the source language.
1546 The return value of this function is the type we're
1547 actually storing in varobj, and using for displaying
1548 the values and for comparing previous and new values.
1550 For example, top-level references are always stripped. */
1551 static struct type
*
1552 get_value_type (struct varobj
*var
)
1557 type
= value_type (var
->value
);
1561 type
= check_typedef (type
);
1563 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
1564 type
= get_target_type (type
);
1566 type
= check_typedef (type
);
1571 /* This returns the target type (or NULL) of TYPE, also skipping
1572 past typedefs, just like get_type ().
1574 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1575 except within get_target_type and get_type. */
1576 static struct type
*
1577 get_target_type (struct type
*type
)
1581 type
= TYPE_TARGET_TYPE (type
);
1583 type
= check_typedef (type
);
1589 /* What is the default display for this variable? We assume that
1590 everything is "natural". Any exceptions? */
1591 static enum varobj_display_formats
1592 variable_default_display (struct varobj
*var
)
1594 return FORMAT_NATURAL
;
1597 /* FIXME: The following should be generic for any pointer */
1599 cppush (struct cpstack
**pstack
, char *name
)
1603 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
1609 /* FIXME: The following should be generic for any pointer */
1611 cppop (struct cpstack
**pstack
)
1616 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
1621 *pstack
= (*pstack
)->next
;
1628 * Language-dependencies
1631 /* Common entry points */
1633 /* Get the language of variable VAR. */
1634 static enum varobj_languages
1635 variable_language (struct varobj
*var
)
1637 enum varobj_languages lang
;
1639 switch (var
->root
->exp
->language_defn
->la_language
)
1645 case language_cplus
:
1656 /* Return the number of children for a given variable.
1657 The result of this function is defined by the language
1658 implementation. The number of children returned by this function
1659 is the number of children that the user will see in the variable
1662 number_of_children (struct varobj
*var
)
1664 return (*var
->root
->lang
->number_of_children
) (var
);;
1667 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1669 name_of_variable (struct varobj
*var
)
1671 return (*var
->root
->lang
->name_of_variable
) (var
);
1674 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1676 name_of_child (struct varobj
*var
, int index
)
1678 return (*var
->root
->lang
->name_of_child
) (var
, index
);
1681 /* What is the ``struct value *'' of the root variable VAR?
1682 TYPE_CHANGED controls what to do if the type of a
1683 use_selected_frame = 1 variable changes. On input,
1684 TYPE_CHANGED = 1 means discard the old varobj, and replace
1685 it with this one. TYPE_CHANGED = 0 means leave it around.
1686 NB: In both cases, var_handle will point to the new varobj,
1687 so if you use TYPE_CHANGED = 0, you will have to stash the
1688 old varobj pointer away somewhere before calling this.
1689 On return, TYPE_CHANGED will be 1 if the type has changed, and
1691 static struct value
*
1692 value_of_root (struct varobj
**var_handle
, int *type_changed
)
1696 if (var_handle
== NULL
)
1701 /* This should really be an exception, since this should
1702 only get called with a root variable. */
1704 if (!is_root_p (var
))
1707 if (var
->root
->use_selected_frame
)
1709 struct varobj
*tmp_var
;
1710 char *old_type
, *new_type
;
1712 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
1713 USE_SELECTED_FRAME
);
1714 if (tmp_var
== NULL
)
1718 old_type
= varobj_get_type (var
);
1719 new_type
= varobj_get_type (tmp_var
);
1720 if (strcmp (old_type
, new_type
) == 0)
1722 varobj_delete (tmp_var
, NULL
, 0);
1730 savestring (var
->obj_name
, strlen (var
->obj_name
));
1731 varobj_delete (var
, NULL
, 0);
1735 tmp_var
->obj_name
= varobj_gen_name ();
1737 install_variable (tmp_var
);
1738 *var_handle
= tmp_var
;
1750 return (*var
->root
->lang
->value_of_root
) (var_handle
);
1753 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1754 static struct value
*
1755 value_of_child (struct varobj
*parent
, int index
)
1757 struct value
*value
;
1759 value
= (*parent
->root
->lang
->value_of_child
) (parent
, index
);
1764 /* Is this variable editable? Use the variable's type to make
1765 this determination. */
1767 variable_editable (struct varobj
*var
)
1769 return (*var
->root
->lang
->variable_editable
) (var
);
1772 /* GDB already has a command called "value_of_variable". Sigh. */
1774 my_value_of_variable (struct varobj
*var
)
1776 if (var
->root
->is_valid
)
1777 return (*var
->root
->lang
->value_of_variable
) (var
);
1783 value_get_print_value (struct value
*value
, enum varobj_display_formats format
)
1786 struct ui_file
*stb
;
1787 struct cleanup
*old_chain
;
1793 stb
= mem_fileopen ();
1794 old_chain
= make_cleanup_ui_file_delete (stb
);
1796 common_val_print (value
, stb
, format_code
[(int) format
], 1, 0, 0);
1797 thevalue
= ui_file_xstrdup (stb
, &dummy
);
1799 do_cleanups (old_chain
);
1803 /* Return non-zero if changes in value of VAR
1804 must be detected and reported by -var-update.
1805 Return zero is -var-update should never report
1806 changes of such values. This makes sense for structures
1807 (since the changes in children values will be reported separately),
1808 or for artifical objects (like 'public' pseudo-field in C++).
1810 Return value of 0 means that gdb need not call value_fetch_lazy
1811 for the value of this variable object. */
1813 varobj_value_is_changeable_p (struct varobj
*var
)
1818 if (CPLUS_FAKE_CHILD (var
))
1821 type
= get_value_type (var
);
1823 switch (TYPE_CODE (type
))
1825 case TYPE_CODE_STRUCT
:
1826 case TYPE_CODE_UNION
:
1827 case TYPE_CODE_ARRAY
:
1838 /* Given the value and the type of a variable object,
1839 adjust the value and type to those necessary
1840 for getting children of the variable object.
1841 This includes dereferencing top-level references
1842 to all types and dereferencing pointers to
1845 Both TYPE and *TYPE should be non-null. VALUE
1846 can be null if we want to only translate type.
1847 *VALUE can be null as well -- if the parent
1848 value is not known. */
1850 adjust_value_for_child_access (struct value
**value
,
1853 gdb_assert (type
&& *type
);
1855 *type
= check_typedef (*type
);
1857 /* The type of value stored in varobj, that is passed
1858 to us, is already supposed to be
1859 reference-stripped. */
1861 gdb_assert (TYPE_CODE (*type
) != TYPE_CODE_REF
);
1863 /* Pointers to structures are treated just like
1864 structures when accessing children. Don't
1865 dererences pointers to other types. */
1866 if (TYPE_CODE (*type
) == TYPE_CODE_PTR
)
1868 struct type
*target_type
= get_target_type (*type
);
1869 if (TYPE_CODE (target_type
) == TYPE_CODE_STRUCT
1870 || TYPE_CODE (target_type
) == TYPE_CODE_UNION
)
1872 if (value
&& *value
)
1873 gdb_value_ind (*value
, value
);
1874 *type
= target_type
;
1878 /* The 'get_target_type' function calls check_typedef on
1879 result, so we can immediately check type code. No
1880 need to call check_typedef here. */
1885 c_number_of_children (struct varobj
*var
)
1887 struct type
*type
= get_value_type (var
);
1889 struct type
*target
;
1891 adjust_value_for_child_access (NULL
, &type
);
1892 target
= get_target_type (type
);
1894 switch (TYPE_CODE (type
))
1896 case TYPE_CODE_ARRAY
:
1897 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
1898 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) != BOUND_CANNOT_BE_DETERMINED
)
1899 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
1901 /* If we don't know how many elements there are, don't display
1906 case TYPE_CODE_STRUCT
:
1907 case TYPE_CODE_UNION
:
1908 children
= TYPE_NFIELDS (type
);
1912 /* The type here is a pointer to non-struct. Typically, pointers
1913 have one child, except for function ptrs, which have no children,
1914 and except for void*, as we don't know what to show.
1916 We can show char* so we allow it to be dereferenced. If you decide
1917 to test for it, please mind that a little magic is necessary to
1918 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1919 TYPE_NAME == "char" */
1920 if (TYPE_CODE (target
) == TYPE_CODE_FUNC
1921 || TYPE_CODE (target
) == TYPE_CODE_VOID
)
1928 /* Other types have no children */
1936 c_name_of_variable (struct varobj
*parent
)
1938 return savestring (parent
->name
, strlen (parent
->name
));
1941 /* Return the value of element TYPE_INDEX of a structure
1942 value VALUE. VALUE's type should be a structure,
1943 or union, or a typedef to struct/union.
1945 Returns NULL if getting the value fails. Never throws. */
1946 static struct value
*
1947 value_struct_element_index (struct value
*value
, int type_index
)
1949 struct value
*result
= NULL
;
1950 volatile struct gdb_exception e
;
1952 struct type
*type
= value_type (value
);
1953 type
= check_typedef (type
);
1955 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
1956 || TYPE_CODE (type
) == TYPE_CODE_UNION
);
1958 TRY_CATCH (e
, RETURN_MASK_ERROR
)
1960 if (TYPE_FIELD_STATIC (type
, type_index
))
1961 result
= value_static_field (type
, type_index
);
1963 result
= value_primitive_field (value
, 0, type_index
, type
);
1975 /* Obtain the information about child INDEX of the variable
1977 If CNAME is not null, sets *CNAME to the name of the child relative
1979 If CVALUE is not null, sets *CVALUE to the value of the child.
1980 If CTYPE is not null, sets *CTYPE to the type of the child.
1982 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
1983 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
1986 c_describe_child (struct varobj
*parent
, int index
,
1987 char **cname
, struct value
**cvalue
, struct type
**ctype
)
1989 struct value
*value
= parent
->value
;
1990 struct type
*type
= get_value_type (parent
);
1999 adjust_value_for_child_access (&value
, &type
);
2001 switch (TYPE_CODE (type
))
2003 case TYPE_CODE_ARRAY
:
2005 *cname
= xstrprintf ("%d", index
2006 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
2008 if (cvalue
&& value
)
2010 int real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
2011 struct value
*indval
=
2012 value_from_longest (builtin_type_int
, (LONGEST
) real_index
);
2013 gdb_value_subscript (value
, indval
, cvalue
);
2017 *ctype
= get_target_type (type
);
2021 case TYPE_CODE_STRUCT
:
2022 case TYPE_CODE_UNION
:
2025 char *string
= TYPE_FIELD_NAME (type
, index
);
2026 *cname
= savestring (string
, strlen (string
));
2029 if (cvalue
&& value
)
2031 /* For C, varobj index is the same as type index. */
2032 *cvalue
= value_struct_element_index (value
, index
);
2036 *ctype
= TYPE_FIELD_TYPE (type
, index
);
2042 *cname
= xstrprintf ("*%s", parent
->name
);
2044 if (cvalue
&& value
)
2045 gdb_value_ind (value
, cvalue
);
2047 /* Don't use get_target_type because it calls
2048 check_typedef and here, we want to show the true
2049 declared type of the variable. */
2051 *ctype
= TYPE_TARGET_TYPE (type
);
2056 /* This should not happen */
2058 *cname
= xstrdup ("???");
2059 /* Don't set value and type, we don't know then. */
2064 c_name_of_child (struct varobj
*parent
, int index
)
2067 c_describe_child (parent
, index
, &name
, NULL
, NULL
);
2071 static struct value
*
2072 c_value_of_root (struct varobj
**var_handle
)
2074 struct value
*new_val
= NULL
;
2075 struct varobj
*var
= *var_handle
;
2076 struct frame_info
*fi
;
2079 /* Only root variables can be updated... */
2080 if (!is_root_p (var
))
2081 /* Not a root var */
2085 /* Determine whether the variable is still around. */
2086 if (var
->root
->valid_block
== NULL
|| var
->root
->use_selected_frame
)
2090 fi
= frame_find_by_id (var
->root
->frame
);
2091 within_scope
= fi
!= NULL
;
2092 /* FIXME: select_frame could fail */
2095 CORE_ADDR pc
= get_frame_pc (fi
);
2096 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
2097 pc
>= BLOCK_END (var
->root
->valid_block
))
2106 /* We need to catch errors here, because if evaluate
2107 expression fails we want to just return NULL. */
2108 gdb_evaluate_expression (var
->root
->exp
, &new_val
);
2115 static struct value
*
2116 c_value_of_child (struct varobj
*parent
, int index
)
2118 struct value
*value
= NULL
;
2119 c_describe_child (parent
, index
, NULL
, &value
, NULL
);
2124 static struct type
*
2125 c_type_of_child (struct varobj
*parent
, int index
)
2127 struct type
*type
= NULL
;
2128 c_describe_child (parent
, index
, NULL
, NULL
, &type
);
2133 c_variable_editable (struct varobj
*var
)
2135 switch (TYPE_CODE (get_value_type (var
)))
2137 case TYPE_CODE_STRUCT
:
2138 case TYPE_CODE_UNION
:
2139 case TYPE_CODE_ARRAY
:
2140 case TYPE_CODE_FUNC
:
2141 case TYPE_CODE_METHOD
:
2152 c_value_of_variable (struct varobj
*var
)
2154 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2155 it will print out its children instead of "{...}". So we need to
2156 catch that case explicitly. */
2157 struct type
*type
= get_type (var
);
2159 /* Strip top-level references. */
2160 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
2161 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2163 switch (TYPE_CODE (type
))
2165 case TYPE_CODE_STRUCT
:
2166 case TYPE_CODE_UNION
:
2167 return xstrdup ("{...}");
2170 case TYPE_CODE_ARRAY
:
2173 number
= xstrprintf ("[%d]", var
->num_children
);
2180 if (var
->value
== NULL
)
2182 /* This can happen if we attempt to get the value of a struct
2183 member when the parent is an invalid pointer. This is an
2184 error condition, so we should tell the caller. */
2189 if (var
->not_fetched
&& value_lazy (var
->value
))
2190 /* Frozen variable and no value yet. We don't
2191 implicitly fetch the value. MI response will
2192 use empty string for the value, which is OK. */
2195 gdb_assert (varobj_value_is_changeable_p (var
));
2196 gdb_assert (!value_lazy (var
->value
));
2197 return value_get_print_value (var
->value
, var
->format
);
2207 cplus_number_of_children (struct varobj
*var
)
2210 int children
, dont_know
;
2215 if (!CPLUS_FAKE_CHILD (var
))
2217 type
= get_value_type (var
);
2218 adjust_value_for_child_access (NULL
, &type
);
2220 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2221 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2225 cplus_class_num_children (type
, kids
);
2226 if (kids
[v_public
] != 0)
2228 if (kids
[v_private
] != 0)
2230 if (kids
[v_protected
] != 0)
2233 /* Add any baseclasses */
2234 children
+= TYPE_N_BASECLASSES (type
);
2237 /* FIXME: save children in var */
2244 type
= get_value_type (var
->parent
);
2245 adjust_value_for_child_access (NULL
, &type
);
2247 cplus_class_num_children (type
, kids
);
2248 if (strcmp (var
->name
, "public") == 0)
2249 children
= kids
[v_public
];
2250 else if (strcmp (var
->name
, "private") == 0)
2251 children
= kids
[v_private
];
2253 children
= kids
[v_protected
];
2258 children
= c_number_of_children (var
);
2263 /* Compute # of public, private, and protected variables in this class.
2264 That means we need to descend into all baseclasses and find out
2265 how many are there, too. */
2267 cplus_class_num_children (struct type
*type
, int children
[3])
2271 children
[v_public
] = 0;
2272 children
[v_private
] = 0;
2273 children
[v_protected
] = 0;
2275 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
2277 /* If we have a virtual table pointer, omit it. */
2278 if (TYPE_VPTR_BASETYPE (type
) == type
&& TYPE_VPTR_FIELDNO (type
) == i
)
2281 if (TYPE_FIELD_PROTECTED (type
, i
))
2282 children
[v_protected
]++;
2283 else if (TYPE_FIELD_PRIVATE (type
, i
))
2284 children
[v_private
]++;
2286 children
[v_public
]++;
2291 cplus_name_of_variable (struct varobj
*parent
)
2293 return c_name_of_variable (parent
);
2296 enum accessibility
{ private_field
, protected_field
, public_field
};
2298 /* Check if field INDEX of TYPE has the specified accessibility.
2299 Return 0 if so and 1 otherwise. */
2301 match_accessibility (struct type
*type
, int index
, enum accessibility acc
)
2303 if (acc
== private_field
&& TYPE_FIELD_PRIVATE (type
, index
))
2305 else if (acc
== protected_field
&& TYPE_FIELD_PROTECTED (type
, index
))
2307 else if (acc
== public_field
&& !TYPE_FIELD_PRIVATE (type
, index
)
2308 && !TYPE_FIELD_PROTECTED (type
, index
))
2315 cplus_describe_child (struct varobj
*parent
, int index
,
2316 char **cname
, struct value
**cvalue
, struct type
**ctype
)
2319 struct value
*value
;
2330 if (CPLUS_FAKE_CHILD (parent
))
2332 value
= parent
->parent
->value
;
2333 type
= get_value_type (parent
->parent
);
2337 value
= parent
->value
;
2338 type
= get_value_type (parent
);
2341 adjust_value_for_child_access (&value
, &type
);
2343 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
2344 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
2346 if (CPLUS_FAKE_CHILD (parent
))
2348 /* The fields of the class type are ordered as they
2349 appear in the class. We are given an index for a
2350 particular access control type ("public","protected",
2351 or "private"). We must skip over fields that don't
2352 have the access control we are looking for to properly
2353 find the indexed field. */
2354 int type_index
= TYPE_N_BASECLASSES (type
);
2355 enum accessibility acc
= public_field
;
2356 if (strcmp (parent
->name
, "private") == 0)
2357 acc
= private_field
;
2358 else if (strcmp (parent
->name
, "protected") == 0)
2359 acc
= protected_field
;
2363 if (TYPE_VPTR_BASETYPE (type
) == type
2364 && type_index
== TYPE_VPTR_FIELDNO (type
))
2366 else if (match_accessibility (type
, type_index
, acc
))
2373 *cname
= xstrdup (TYPE_FIELD_NAME (type
, type_index
));
2375 if (cvalue
&& value
)
2376 *cvalue
= value_struct_element_index (value
, type_index
);
2379 *ctype
= TYPE_FIELD_TYPE (type
, type_index
);
2381 else if (index
< TYPE_N_BASECLASSES (type
))
2383 /* This is a baseclass. */
2385 *cname
= xstrdup (TYPE_FIELD_NAME (type
, index
));
2387 if (cvalue
&& value
)
2389 *cvalue
= value_cast (TYPE_FIELD_TYPE (type
, index
), value
);
2394 *ctype
= TYPE_FIELD_TYPE (type
, index
);
2399 char *access
= NULL
;
2401 cplus_class_num_children (type
, children
);
2403 /* Everything beyond the baseclasses can
2404 only be "public", "private", or "protected"
2406 The special "fake" children are always output by varobj in
2407 this order. So if INDEX == 2, it MUST be "protected". */
2408 index
-= TYPE_N_BASECLASSES (type
);
2412 if (children
[v_public
] > 0)
2414 else if (children
[v_private
] > 0)
2417 access
= "protected";
2420 if (children
[v_public
] > 0)
2422 if (children
[v_private
] > 0)
2425 access
= "protected";
2427 else if (children
[v_private
] > 0)
2428 access
= "protected";
2431 /* Must be protected */
2432 access
= "protected";
2439 gdb_assert (access
);
2441 *cname
= xstrdup (access
);
2443 /* Value and type are null here. */
2448 c_describe_child (parent
, index
, cname
, cvalue
, ctype
);
2453 cplus_name_of_child (struct varobj
*parent
, int index
)
2456 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
);
2460 static struct value
*
2461 cplus_value_of_root (struct varobj
**var_handle
)
2463 return c_value_of_root (var_handle
);
2466 static struct value
*
2467 cplus_value_of_child (struct varobj
*parent
, int index
)
2469 struct value
*value
= NULL
;
2470 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
);
2474 static struct type
*
2475 cplus_type_of_child (struct varobj
*parent
, int index
)
2477 struct type
*type
= NULL
;
2478 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
);
2483 cplus_variable_editable (struct varobj
*var
)
2485 if (CPLUS_FAKE_CHILD (var
))
2488 return c_variable_editable (var
);
2492 cplus_value_of_variable (struct varobj
*var
)
2495 /* If we have one of our special types, don't print out
2497 if (CPLUS_FAKE_CHILD (var
))
2498 return xstrdup ("");
2500 return c_value_of_variable (var
);
2506 java_number_of_children (struct varobj
*var
)
2508 return cplus_number_of_children (var
);
2512 java_name_of_variable (struct varobj
*parent
)
2516 name
= cplus_name_of_variable (parent
);
2517 /* If the name has "-" in it, it is because we
2518 needed to escape periods in the name... */
2521 while (*p
!= '\000')
2532 java_name_of_child (struct varobj
*parent
, int index
)
2536 name
= cplus_name_of_child (parent
, index
);
2537 /* Escape any periods in the name... */
2540 while (*p
!= '\000')
2550 static struct value
*
2551 java_value_of_root (struct varobj
**var_handle
)
2553 return cplus_value_of_root (var_handle
);
2556 static struct value
*
2557 java_value_of_child (struct varobj
*parent
, int index
)
2559 return cplus_value_of_child (parent
, index
);
2562 static struct type
*
2563 java_type_of_child (struct varobj
*parent
, int index
)
2565 return cplus_type_of_child (parent
, index
);
2569 java_variable_editable (struct varobj
*var
)
2571 return cplus_variable_editable (var
);
2575 java_value_of_variable (struct varobj
*var
)
2577 return cplus_value_of_variable (var
);
2580 extern void _initialize_varobj (void);
2582 _initialize_varobj (void)
2584 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
2586 varobj_table
= xmalloc (sizeof_table
);
2587 memset (varobj_table
, 0, sizeof_table
);
2589 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance
,
2591 Set varobj debugging."), _("\
2592 Show varobj debugging."), _("\
2593 When non-zero, varobj debugging is enabled."),
2596 &setlist
, &showlist
);
2599 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2600 are defined on globals.
2601 Invalidated varobjs will be always printed in_scope="invalid". */
2603 varobj_invalidate (void)
2605 struct varobj
**all_rootvarobj
;
2606 struct varobj
**varp
;
2608 if (varobj_list (&all_rootvarobj
) > 0)
2610 varp
= all_rootvarobj
;
2611 while (*varp
!= NULL
)
2613 /* global var must be re-evaluated. */
2614 if ((*varp
)->root
->valid_block
== NULL
)
2616 struct varobj
*tmp_var
;
2618 /* Try to create a varobj with same expression. If we succeed replace
2619 the old varobj, otherwise invalidate it. */
2620 tmp_var
= varobj_create (NULL
, (*varp
)->name
, (CORE_ADDR
) 0, USE_CURRENT_FRAME
);
2621 if (tmp_var
!= NULL
)
2623 tmp_var
->obj_name
= xstrdup ((*varp
)->obj_name
);
2624 varobj_delete (*varp
, NULL
, 0);
2625 install_variable (tmp_var
);
2628 (*varp
)->root
->is_valid
= 0;
2630 else /* locals must be invalidated. */
2631 (*varp
)->root
->is_valid
= 0;
2635 xfree (all_rootvarobj
);