1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
22 #include "exceptions.h"
24 #include "expression.h"
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
37 /* Non-zero if we want to see trace of varobj level stuff. */
41 show_varobjdebug (struct ui_file
*file
, int from_tty
,
42 struct cmd_list_element
*c
, const char *value
)
44 fprintf_filtered (file
, _("Varobj debugging is %s.\n"), value
);
47 /* String representations of gdb's format codes */
48 char *varobj_format_string
[] =
49 { "natural", "binary", "decimal", "hexadecimal", "octal" };
51 /* String representations of gdb's known languages */
52 char *varobj_language_string
[] = { "unknown", "C", "C++", "Java" };
56 /* Every root variable has one of these structures saved in its
57 varobj. Members which must be free'd are noted. */
61 /* Alloc'd expression for this parent. */
62 struct expression
*exp
;
64 /* Block for which this expression is valid */
65 struct block
*valid_block
;
67 /* The frame for this expression */
68 struct frame_id frame
;
70 /* If 1, "update" always recomputes the frame & valid block
71 using the currently selected frame. */
72 int use_selected_frame
;
74 /* Language info for this variable and its children */
75 struct language_specific
*lang
;
77 /* The varobj for this root node. */
78 struct varobj
*rootvar
;
80 /* Next root variable */
81 struct varobj_root
*next
;
84 typedef struct varobj
*varobj_p
;
88 /* Every variable in the system has a structure of this type defined
89 for it. This structure holds all information necessary to manipulate
90 a particular object variable. Members which must be freed are noted. */
94 /* Alloc'd name of the variable for this object.. If this variable is a
95 child, then this name will be the child's source name.
97 /* NOTE: This is the "expression" */
100 /* The alloc'd name for this variable's object. This is here for
101 convenience when constructing this object's children. */
104 /* Index of this variable in its parent or -1 */
107 /* The type of this variable. This may NEVER be NULL. */
110 /* The value of this expression or subexpression. A NULL value
111 indicates there was an error getting this value.
112 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
113 the value is either NULL, or not lazy. */
116 /* The number of (immediate) children this variable has */
119 /* If this object is a child, this points to its immediate parent. */
120 struct varobj
*parent
;
122 /* Children of this object. */
123 VEC (varobj_p
) *children
;
125 /* Description of the root variable. Points to root variable for children. */
126 struct varobj_root
*root
;
128 /* The format of the output for this object */
129 enum varobj_display_formats format
;
131 /* Was this variable updated via a varobj_set_value operation */
134 /* Last print value. */
141 struct cpstack
*next
;
144 /* A list of varobjs */
152 /* Private function prototypes */
154 /* Helper functions for the above subcommands. */
156 static int delete_variable (struct cpstack
**, struct varobj
*, int);
158 static void delete_variable_1 (struct cpstack
**, int *,
159 struct varobj
*, int, int);
161 static int install_variable (struct varobj
*);
163 static void uninstall_variable (struct varobj
*);
165 static struct varobj
*create_child (struct varobj
*, int, char *);
167 /* Utility routines */
169 static struct varobj
*new_variable (void);
171 static struct varobj
*new_root_variable (void);
173 static void free_variable (struct varobj
*var
);
175 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
177 static struct type
*get_type (struct varobj
*var
);
179 static struct type
*get_type_deref (struct varobj
*var
);
181 static struct type
*get_target_type (struct type
*);
183 static enum varobj_display_formats
variable_default_display (struct varobj
*);
185 static void cppush (struct cpstack
**pstack
, char *name
);
187 static char *cppop (struct cpstack
**pstack
);
189 static int install_new_value (struct varobj
*var
, struct value
*value
,
192 /* Language-specific routines. */
194 static enum varobj_languages
variable_language (struct varobj
*var
);
196 static int number_of_children (struct varobj
*);
198 static char *name_of_variable (struct varobj
*);
200 static char *name_of_child (struct varobj
*, int);
202 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
204 static struct value
*value_of_child (struct varobj
*parent
, int index
);
206 static int variable_editable (struct varobj
*var
);
208 static char *my_value_of_variable (struct varobj
*var
);
210 static char *value_get_print_value (struct value
*value
,
211 enum varobj_display_formats format
);
213 static int varobj_value_is_changeable_p (struct varobj
*var
);
215 static int is_root_p (struct varobj
*var
);
217 /* C implementation */
219 static int c_number_of_children (struct varobj
*var
);
221 static char *c_name_of_variable (struct varobj
*parent
);
223 static char *c_name_of_child (struct varobj
*parent
, int index
);
225 static struct value
*c_value_of_root (struct varobj
**var_handle
);
227 static struct value
*c_value_of_child (struct varobj
*parent
, int index
);
229 static struct type
*c_type_of_child (struct varobj
*parent
, int index
);
231 static int c_variable_editable (struct varobj
*var
);
233 static char *c_value_of_variable (struct varobj
*var
);
235 /* C++ implementation */
237 static int cplus_number_of_children (struct varobj
*var
);
239 static void cplus_class_num_children (struct type
*type
, int children
[3]);
241 static char *cplus_name_of_variable (struct varobj
*parent
);
243 static char *cplus_name_of_child (struct varobj
*parent
, int index
);
245 static struct value
*cplus_value_of_root (struct varobj
**var_handle
);
247 static struct value
*cplus_value_of_child (struct varobj
*parent
, int index
);
249 static struct type
*cplus_type_of_child (struct varobj
*parent
, int index
);
251 static int cplus_variable_editable (struct varobj
*var
);
253 static char *cplus_value_of_variable (struct varobj
*var
);
255 /* Java implementation */
257 static int java_number_of_children (struct varobj
*var
);
259 static char *java_name_of_variable (struct varobj
*parent
);
261 static char *java_name_of_child (struct varobj
*parent
, int index
);
263 static struct value
*java_value_of_root (struct varobj
**var_handle
);
265 static struct value
*java_value_of_child (struct varobj
*parent
, int index
);
267 static struct type
*java_type_of_child (struct varobj
*parent
, int index
);
269 static int java_variable_editable (struct varobj
*var
);
271 static char *java_value_of_variable (struct varobj
*var
);
273 /* The language specific vector */
275 struct language_specific
278 /* The language of this variable */
279 enum varobj_languages language
;
281 /* The number of children of PARENT. */
282 int (*number_of_children
) (struct varobj
* parent
);
284 /* The name (expression) of a root varobj. */
285 char *(*name_of_variable
) (struct varobj
* parent
);
287 /* The name of the INDEX'th child of PARENT. */
288 char *(*name_of_child
) (struct varobj
* parent
, int index
);
290 /* The ``struct value *'' of the root variable ROOT. */
291 struct value
*(*value_of_root
) (struct varobj
** root_handle
);
293 /* The ``struct value *'' of the INDEX'th child of PARENT. */
294 struct value
*(*value_of_child
) (struct varobj
* parent
, int index
);
296 /* The type of the INDEX'th child of PARENT. */
297 struct type
*(*type_of_child
) (struct varobj
* parent
, int index
);
299 /* Is VAR editable? */
300 int (*variable_editable
) (struct varobj
* var
);
302 /* The current value of VAR. */
303 char *(*value_of_variable
) (struct varobj
* var
);
306 /* Array of known source language routines. */
307 static struct language_specific languages
[vlang_end
] = {
308 /* Unknown (try treating as C */
311 c_number_of_children
,
323 c_number_of_children
,
335 cplus_number_of_children
,
336 cplus_name_of_variable
,
339 cplus_value_of_child
,
341 cplus_variable_editable
,
342 cplus_value_of_variable
}
347 java_number_of_children
,
348 java_name_of_variable
,
353 java_variable_editable
,
354 java_value_of_variable
}
357 /* A little convenience enum for dealing with C++/Java */
360 v_public
= 0, v_private
, v_protected
365 /* Mappings of varobj_display_formats enums to gdb's format codes */
366 static int format_code
[] = { 0, 't', 'd', 'x', 'o' };
368 /* Header of the list of root variable objects */
369 static struct varobj_root
*rootlist
;
370 static int rootcount
= 0; /* number of root varobjs in the list */
372 /* Prime number indicating the number of buckets in the hash table */
373 /* A prime large enough to avoid too many colisions */
374 #define VAROBJ_TABLE_SIZE 227
376 /* Pointer to the varobj hash table (built at run time) */
377 static struct vlist
**varobj_table
;
379 /* Is the variable X one of our "fake" children? */
380 #define CPLUS_FAKE_CHILD(x) \
381 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
384 /* API Implementation */
386 is_root_p (struct varobj
*var
)
388 return (var
->root
->rootvar
== var
);
391 /* Creates a varobj (not its children) */
393 /* Return the full FRAME which corresponds to the given CORE_ADDR
394 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
396 static struct frame_info
*
397 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
399 struct frame_info
*frame
= NULL
;
401 if (frame_addr
== (CORE_ADDR
) 0)
406 frame
= get_prev_frame (frame
);
409 if (get_frame_base_address (frame
) == frame_addr
)
415 varobj_create (char *objname
,
416 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
419 struct frame_info
*fi
;
420 struct frame_info
*old_fi
= NULL
;
422 struct cleanup
*old_chain
;
424 /* Fill out a varobj structure for the (root) variable being constructed. */
425 var
= new_root_variable ();
426 old_chain
= make_cleanup_free_variable (var
);
428 if (expression
!= NULL
)
431 enum varobj_languages lang
;
434 /* Parse and evaluate the expression, filling in as much
435 of the variable's data as possible */
437 /* Allow creator to specify context of variable */
438 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
439 fi
= deprecated_selected_frame
;
441 /* FIXME: cagney/2002-11-23: This code should be doing a
442 lookup using the frame ID and not just the frame's
443 ``address''. This, of course, means an interface change.
444 However, with out that interface change ISAs, such as the
445 ia64 with its two stacks, won't work. Similar goes for the
446 case where there is a frameless function. */
447 fi
= find_frame_addr_in_frame_chain (frame
);
449 /* frame = -2 means always use selected frame */
450 if (type
== USE_SELECTED_FRAME
)
451 var
->root
->use_selected_frame
= 1;
455 block
= get_frame_block (fi
, 0);
458 innermost_block
= NULL
;
459 /* Wrap the call to parse expression, so we can
460 return a sensible error. */
461 if (!gdb_parse_exp_1 (&p
, block
, 0, &var
->root
->exp
))
466 /* Don't allow variables to be created for types. */
467 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
)
469 do_cleanups (old_chain
);
470 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
471 " as an expression.\n");
475 var
->format
= variable_default_display (var
);
476 var
->root
->valid_block
= innermost_block
;
477 var
->name
= savestring (expression
, strlen (expression
));
479 /* When the frame is different from the current frame,
480 we must select the appropriate frame before parsing
481 the expression, otherwise the value will not be current.
482 Since select_frame is so benign, just call it for all cases. */
485 var
->root
->frame
= get_frame_id (fi
);
486 old_fi
= deprecated_selected_frame
;
490 /* We definitively need to catch errors here.
491 If evaluate_expression succeeds we got the value we wanted.
492 But if it fails, we still go on with a call to evaluate_type() */
493 if (!gdb_evaluate_expression (var
->root
->exp
, &value
))
494 /* Error getting the value. Try to at least get the
496 value
= evaluate_type (var
->root
->exp
);
498 var
->type
= value_type (value
);
499 install_new_value (var
, value
, 1 /* Initial assignment */);
501 /* Set language info */
502 lang
= variable_language (var
);
503 var
->root
->lang
= &languages
[lang
];
505 /* Set ourselves as our root */
506 var
->root
->rootvar
= var
;
508 /* Reset the selected frame */
510 select_frame (old_fi
);
513 /* If the variable object name is null, that means this
514 is a temporary variable, so don't install it. */
516 if ((var
!= NULL
) && (objname
!= NULL
))
518 var
->obj_name
= savestring (objname
, strlen (objname
));
520 /* If a varobj name is duplicated, the install will fail so
522 if (!install_variable (var
))
524 do_cleanups (old_chain
);
529 discard_cleanups (old_chain
);
533 /* Generates an unique name that can be used for a varobj */
536 varobj_gen_name (void)
541 /* generate a name for this object */
543 obj_name
= xstrprintf ("var%d", id
);
548 /* Given an "objname", returns the pointer to the corresponding varobj
549 or NULL if not found */
552 varobj_get_handle (char *objname
)
556 unsigned int index
= 0;
559 for (chp
= objname
; *chp
; chp
++)
561 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
564 cv
= *(varobj_table
+ index
);
565 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
569 error (_("Variable object not found"));
574 /* Given the handle, return the name of the object */
577 varobj_get_objname (struct varobj
*var
)
579 return var
->obj_name
;
582 /* Given the handle, return the expression represented by the object */
585 varobj_get_expression (struct varobj
*var
)
587 return name_of_variable (var
);
590 /* Deletes a varobj and all its children if only_children == 0,
591 otherwise deletes only the children; returns a malloc'ed list of all the
592 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
595 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
599 struct cpstack
*result
= NULL
;
602 /* Initialize a stack for temporary results */
603 cppush (&result
, NULL
);
606 /* Delete only the variable children */
607 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
609 /* Delete the variable and all its children */
610 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
612 /* We may have been asked to return a list of what has been deleted */
615 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
619 *cp
= cppop (&result
);
620 while ((*cp
!= NULL
) && (mycount
> 0))
624 *cp
= cppop (&result
);
627 if (mycount
|| (*cp
!= NULL
))
628 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
635 /* Set/Get variable object display format */
637 enum varobj_display_formats
638 varobj_set_display_format (struct varobj
*var
,
639 enum varobj_display_formats format
)
646 case FORMAT_HEXADECIMAL
:
648 var
->format
= format
;
652 var
->format
= variable_default_display (var
);
658 enum varobj_display_formats
659 varobj_get_display_format (struct varobj
*var
)
665 varobj_get_num_children (struct varobj
*var
)
667 if (var
->num_children
== -1)
668 var
->num_children
= number_of_children (var
);
670 return var
->num_children
;
673 /* Creates a list of the immediate children of a variable object;
674 the return code is the number of such children or -1 on error */
677 varobj_list_children (struct varobj
*var
, struct varobj
***childlist
)
679 struct varobj
*child
;
683 /* sanity check: have we been passed a pointer? */
684 if (childlist
== NULL
)
689 if (var
->num_children
== -1)
690 var
->num_children
= number_of_children (var
);
692 /* If that failed, give up. */
693 if (var
->num_children
== -1)
696 /* If we're called when the list of children is not yet initialized,
697 allocate enough elements in it. */
698 while (VEC_length (varobj_p
, var
->children
) < var
->num_children
)
699 VEC_safe_push (varobj_p
, var
->children
, NULL
);
701 /* List of children */
702 *childlist
= xmalloc ((var
->num_children
+ 1) * sizeof (struct varobj
*));
704 for (i
= 0; i
< var
->num_children
; i
++)
708 /* Mark as the end in case we bail out */
709 *((*childlist
) + i
) = NULL
;
711 existing
= VEC_index (varobj_p
, var
->children
, i
);
713 if (existing
== NULL
)
715 /* Either it's the first call to varobj_list_children for
716 this variable object, and the child was never created,
717 or it was explicitly deleted by the client. */
718 name
= name_of_child (var
, i
);
719 existing
= create_child (var
, i
, name
);
720 VEC_replace (varobj_p
, var
->children
, i
, existing
);
723 *((*childlist
) + i
) = existing
;
726 /* End of list is marked by a NULL pointer */
727 *((*childlist
) + i
) = NULL
;
729 return var
->num_children
;
732 /* Obtain the type of an object Variable as a string similar to the one gdb
733 prints on the console */
736 varobj_get_type (struct varobj
*var
)
739 struct cleanup
*old_chain
;
744 /* For the "fake" variables, do not return a type. (It's type is
746 if (CPLUS_FAKE_CHILD (var
))
749 stb
= mem_fileopen ();
750 old_chain
= make_cleanup_ui_file_delete (stb
);
752 /* To print the type, we simply create a zero ``struct value *'' and
753 cast it to our type. We then typeprint this variable. */
754 val
= value_zero (var
->type
, not_lval
);
755 type_print (value_type (val
), "", stb
, -1);
757 thetype
= ui_file_xstrdup (stb
, &length
);
758 do_cleanups (old_chain
);
762 /* Obtain the type of an object variable. */
765 varobj_get_gdb_type (struct varobj
*var
)
770 enum varobj_languages
771 varobj_get_language (struct varobj
*var
)
773 return variable_language (var
);
777 varobj_get_attributes (struct varobj
*var
)
781 if (variable_editable (var
))
782 /* FIXME: define masks for attributes */
783 attributes
|= 0x00000001; /* Editable */
789 varobj_get_value (struct varobj
*var
)
791 return my_value_of_variable (var
);
794 /* Set the value of an object variable (if it is editable) to the
795 value of the given expression */
796 /* Note: Invokes functions that can call error() */
799 varobj_set_value (struct varobj
*var
, char *expression
)
805 /* The argument "expression" contains the variable's new value.
806 We need to first construct a legal expression for this -- ugh! */
807 /* Does this cover all the bases? */
808 struct expression
*exp
;
810 int saved_input_radix
= input_radix
;
812 if (var
->value
!= NULL
&& variable_editable (var
))
814 char *s
= expression
;
817 input_radix
= 10; /* ALWAYS reset to decimal temporarily */
818 exp
= parse_exp_1 (&s
, 0, 0);
819 if (!gdb_evaluate_expression (exp
, &value
))
821 /* We cannot proceed without a valid expression. */
826 /* All types that are editable must also be changeable. */
827 gdb_assert (varobj_value_is_changeable_p (var
));
829 /* The value of a changeable variable object must not be lazy. */
830 gdb_assert (!value_lazy (var
->value
));
832 /* Need to coerce the input. We want to check if the
833 value of the variable object will be different
834 after assignment, and the first thing value_assign
835 does is coerce the input.
836 For example, if we are assigning an array to a pointer variable we
837 should compare the pointer with the the array's address, not with the
839 value
= coerce_array (value
);
841 /* The new value may be lazy. gdb_value_assign, or
842 rather value_contents, will take care of this.
843 If fetching of the new value will fail, gdb_value_assign
844 with catch the exception. */
845 if (!gdb_value_assign (var
->value
, value
, &val
))
848 /* If the value has changed, record it, so that next -var-update can
849 report this change. If a variable had a value of '1', we've set it
850 to '333' and then set again to '1', when -var-update will report this
851 variable as changed -- because the first assignment has set the
852 'updated' flag. There's no need to optimize that, because return value
853 of -var-update should be considered an approximation. */
854 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
855 input_radix
= saved_input_radix
;
862 /* Returns a malloc'ed list with all root variable objects */
864 varobj_list (struct varobj
***varlist
)
867 struct varobj_root
*croot
;
868 int mycount
= rootcount
;
870 /* Alloc (rootcount + 1) entries for the result */
871 *varlist
= xmalloc ((rootcount
+ 1) * sizeof (struct varobj
*));
875 while ((croot
!= NULL
) && (mycount
> 0))
877 *cv
= croot
->rootvar
;
882 /* Mark the end of the list */
885 if (mycount
|| (croot
!= NULL
))
887 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
893 /* Assign a new value to a variable object. If INITIAL is non-zero,
894 this is the first assignement after the variable object was just
895 created, or changed type. In that case, just assign the value
897 Otherwise, assign the value and if type_changeable returns non-zero,
898 find if the new value is different from the current value.
899 Return 1 if so, and 0 if the values are equal.
901 The VALUE parameter should not be released -- the function will
902 take care of releasing it when needed. */
904 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
910 /* We need to know the varobj's type to decide if the value should
911 be fetched or not. C++ fake children (public/protected/private) don't have
913 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
914 changeable
= varobj_value_is_changeable_p (var
);
915 need_to_fetch
= changeable
;
917 /* We are not interested in the address of references, and given
918 that in C++ a reference is not rebindable, it cannot
919 meaningfully change. So, get hold of the real value. */
922 value
= coerce_ref (value
);
923 release_value (value
);
926 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
927 /* For unions, we need to fetch the value implicitly because
928 of implementation of union member fetch. When gdb
929 creates a value for a field and the value of the enclosing
930 structure is not lazy, it immediately copies the necessary
931 bytes from the enclosing values. If the enclosing value is
932 lazy, the call to value_fetch_lazy on the field will read
933 the data from memory. For unions, that means we'll read the
934 same memory more than once, which is not desirable. So
938 /* The new value might be lazy. If the type is changeable,
939 that is we'll be comparing values of this type, fetch the
940 value now. Otherwise, on the next update the old value
941 will be lazy, which means we've lost that old value. */
942 if (need_to_fetch
&& value
&& value_lazy (value
))
944 if (!gdb_value_fetch_lazy (value
))
946 /* Set the value to NULL, so that for the next -var-update,
947 we don't try to compare the new value with this value,
948 that we couldn't even read. */
953 /* If the type is changeable, compare the old and the new values.
954 If this is the initial assignment, we don't have any old value
957 var
->print_value
= value_get_print_value (value
, var
->format
);
960 /* If the value of the varobj was changed by -var-set-value, then the
961 value in the varobj and in the target is the same. However, that value
962 is different from the value that the varobj had after the previous
963 -var-update. So need to the varobj as changed. */
966 xfree (var
->print_value
);
967 var
->print_value
= value_get_print_value (value
, var
->format
);
972 /* Try to compare the values. That requires that both
973 values are non-lazy. */
975 /* Quick comparison of NULL values. */
976 if (var
->value
== NULL
&& value
== NULL
)
979 else if (var
->value
== NULL
|| value
== NULL
)
981 xfree (var
->print_value
);
982 var
->print_value
= value_get_print_value (value
, var
->format
);
988 gdb_assert (!value_lazy (var
->value
));
989 gdb_assert (!value_lazy (value
));
990 print_value
= value_get_print_value (value
, var
->format
);
992 gdb_assert (var
->print_value
!= NULL
&& print_value
!= NULL
);
993 if (strcmp (var
->print_value
, print_value
) != 0)
995 xfree (var
->print_value
);
996 var
->print_value
= print_value
;
1000 xfree (print_value
);
1005 /* We must always keep the new value, since children depend on it. */
1006 if (var
->value
!= NULL
)
1007 value_free (var
->value
);
1011 gdb_assert (!var
->value
|| value_type (var
->value
));
1016 /* Update the values for a variable and its children. This is a
1017 two-pronged attack. First, re-parse the value for the root's
1018 expression to see if it's changed. Then go all the way
1019 through its children, reconstructing them and noting if they've
1022 -1 if there was an error updating the varobj
1023 -2 if the type changed
1024 Otherwise it is the number of children + parent changed
1026 Only root variables can be updated...
1028 NOTE: This function may delete the caller's varobj. If it
1029 returns -2, then it has done this and VARP will be modified
1030 to point to the new varobj. */
1033 varobj_update (struct varobj
**varp
, struct varobj
***changelist
)
1042 struct varobj
**templist
= NULL
;
1044 VEC (varobj_p
) *stack
= NULL
;
1045 VEC (varobj_p
) *result
= NULL
;
1046 struct frame_id old_fid
;
1047 struct frame_info
*fi
;
1049 /* sanity check: have we been passed a pointer? */
1050 if (changelist
== NULL
)
1053 /* Only root variables can be updated... */
1054 if (!is_root_p (*varp
))
1055 /* Not a root var */
1058 /* Save the selected stack frame, since we will need to change it
1059 in order to evaluate expressions. */
1060 old_fid
= get_frame_id (deprecated_selected_frame
);
1062 /* Update the root variable. value_of_root can return NULL
1063 if the variable is no longer around, i.e. we stepped out of
1064 the frame in which a local existed. We are letting the
1065 value_of_root variable dispose of the varobj if the type
1068 new = value_of_root (varp
, &type_changed
);
1070 /* Restore selected frame */
1071 fi
= frame_find_by_id (old_fid
);
1075 /* If this is a "use_selected_frame" varobj, and its type has changed,
1076 them note that it's changed. */
1078 VEC_safe_push (varobj_p
, result
, *varp
);
1080 if (install_new_value ((*varp
), new, type_changed
))
1082 /* If type_changed is 1, install_new_value will never return
1083 non-zero, so we'll never report the same variable twice. */
1084 gdb_assert (!type_changed
);
1085 VEC_safe_push (varobj_p
, result
, *varp
);
1090 /* This means the varobj itself is out of scope.
1092 VEC_free (varobj_p
, result
);
1096 VEC_safe_push (varobj_p
, stack
, *varp
);
1098 /* Walk through the children, reconstructing them all. */
1099 while (!VEC_empty (varobj_p
, stack
))
1101 v
= VEC_pop (varobj_p
, stack
);
1103 /* Push any children. Use reverse order so that the first
1104 child is popped from the work stack first, and so
1105 will be added to result first. This does not
1106 affect correctness, just "nicer". */
1107 for (i
= VEC_length (varobj_p
, v
->children
)-1; i
>= 0; --i
)
1109 varobj_p c
= VEC_index (varobj_p
, v
->children
, i
);
1110 /* Child may be NULL if explicitly deleted by -var-delete. */
1112 VEC_safe_push (varobj_p
, stack
, c
);
1115 /* Update this variable, unless it's a root, which is already
1119 new = value_of_child (v
->parent
, v
->index
);
1120 if (install_new_value (v
, new, 0 /* type not changed */))
1122 /* Note that it's changed */
1123 VEC_safe_push (varobj_p
, result
, v
);
1129 /* Alloc (changed + 1) list entries */
1130 changed
= VEC_length (varobj_p
, result
);
1131 *changelist
= xmalloc ((changed
+ 1) * sizeof (struct varobj
*));
1134 for (i
= 0; i
< changed
; ++i
)
1136 *cv
= VEC_index (varobj_p
, result
, i
);
1137 gdb_assert (*cv
!= NULL
);
1149 /* Helper functions */
1152 * Variable object construction/destruction
1156 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
1157 int only_children_p
)
1161 delete_variable_1 (resultp
, &delcount
, var
,
1162 only_children_p
, 1 /* remove_from_parent_p */ );
1167 /* Delete the variable object VAR and its children */
1168 /* IMPORTANT NOTE: If we delete a variable which is a child
1169 and the parent is not removed we dump core. It must be always
1170 initially called with remove_from_parent_p set */
1172 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
1173 struct varobj
*var
, int only_children_p
,
1174 int remove_from_parent_p
)
1178 /* Delete any children of this variable, too. */
1179 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
1181 varobj_p child
= VEC_index (varobj_p
, var
->children
, i
);
1182 if (!remove_from_parent_p
)
1183 child
->parent
= NULL
;
1184 delete_variable_1 (resultp
, delcountp
, child
, 0, only_children_p
);
1186 VEC_free (varobj_p
, var
->children
);
1188 /* if we were called to delete only the children we are done here */
1189 if (only_children_p
)
1192 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1193 /* If the name is null, this is a temporary variable, that has not
1194 yet been installed, don't report it, it belongs to the caller... */
1195 if (var
->obj_name
!= NULL
)
1197 cppush (resultp
, xstrdup (var
->obj_name
));
1198 *delcountp
= *delcountp
+ 1;
1201 /* If this variable has a parent, remove it from its parent's list */
1202 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1203 (as indicated by remove_from_parent_p) we don't bother doing an
1204 expensive list search to find the element to remove when we are
1205 discarding the list afterwards */
1206 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1208 VEC_replace (varobj_p
, var
->parent
->children
, var
->index
, NULL
);
1211 if (var
->obj_name
!= NULL
)
1212 uninstall_variable (var
);
1214 /* Free memory associated with this variable */
1215 free_variable (var
);
1218 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1220 install_variable (struct varobj
*var
)
1223 struct vlist
*newvl
;
1225 unsigned int index
= 0;
1228 for (chp
= var
->obj_name
; *chp
; chp
++)
1230 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1233 cv
= *(varobj_table
+ index
);
1234 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1238 error (_("Duplicate variable object name"));
1240 /* Add varobj to hash table */
1241 newvl
= xmalloc (sizeof (struct vlist
));
1242 newvl
->next
= *(varobj_table
+ index
);
1244 *(varobj_table
+ index
) = newvl
;
1246 /* If root, add varobj to root list */
1247 if (is_root_p (var
))
1249 /* Add to list of root variables */
1250 if (rootlist
== NULL
)
1251 var
->root
->next
= NULL
;
1253 var
->root
->next
= rootlist
;
1254 rootlist
= var
->root
;
1261 /* Unistall the object VAR. */
1263 uninstall_variable (struct varobj
*var
)
1267 struct varobj_root
*cr
;
1268 struct varobj_root
*prer
;
1270 unsigned int index
= 0;
1273 /* Remove varobj from hash table */
1274 for (chp
= var
->obj_name
; *chp
; chp
++)
1276 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1279 cv
= *(varobj_table
+ index
);
1281 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1288 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
1293 ("Assertion failed: Could not find variable object \"%s\" to delete",
1299 *(varobj_table
+ index
) = cv
->next
;
1301 prev
->next
= cv
->next
;
1305 /* If root, remove varobj from root list */
1306 if (is_root_p (var
))
1308 /* Remove from list of root variables */
1309 if (rootlist
== var
->root
)
1310 rootlist
= var
->root
->next
;
1315 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
1323 ("Assertion failed: Could not find varobj \"%s\" in root list",
1330 prer
->next
= cr
->next
;
1337 /* Create and install a child of the parent of the given name */
1338 static struct varobj
*
1339 create_child (struct varobj
*parent
, int index
, char *name
)
1341 struct varobj
*child
;
1343 struct value
*value
;
1345 child
= new_variable ();
1347 /* name is allocated by name_of_child */
1349 child
->index
= index
;
1350 value
= value_of_child (parent
, index
);
1351 child
->parent
= parent
;
1352 child
->root
= parent
->root
;
1353 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, name
);
1354 child
->obj_name
= childs_name
;
1355 install_variable (child
);
1357 /* Compute the type of the child. Must do this before
1358 calling install_new_value. */
1360 /* If the child had no evaluation errors, var->value
1361 will be non-NULL and contain a valid type. */
1362 child
->type
= value_type (value
);
1364 /* Otherwise, we must compute the type. */
1365 child
->type
= (*child
->root
->lang
->type_of_child
) (child
->parent
,
1367 install_new_value (child
, value
, 1);
1374 * Miscellaneous utility functions.
1377 /* Allocate memory and initialize a new variable */
1378 static struct varobj
*
1383 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
1385 var
->obj_name
= NULL
;
1389 var
->num_children
= -1;
1391 var
->children
= NULL
;
1395 var
->print_value
= NULL
;
1400 /* Allocate memory and initialize a new root variable */
1401 static struct varobj
*
1402 new_root_variable (void)
1404 struct varobj
*var
= new_variable ();
1405 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));;
1406 var
->root
->lang
= NULL
;
1407 var
->root
->exp
= NULL
;
1408 var
->root
->valid_block
= NULL
;
1409 var
->root
->frame
= null_frame_id
;
1410 var
->root
->use_selected_frame
= 0;
1411 var
->root
->rootvar
= NULL
;
1416 /* Free any allocated memory associated with VAR. */
1418 free_variable (struct varobj
*var
)
1420 /* Free the expression if this is a root variable. */
1421 if (is_root_p (var
))
1423 free_current_contents (&var
->root
->exp
);
1428 xfree (var
->obj_name
);
1429 xfree (var
->print_value
);
1434 do_free_variable_cleanup (void *var
)
1436 free_variable (var
);
1439 static struct cleanup
*
1440 make_cleanup_free_variable (struct varobj
*var
)
1442 return make_cleanup (do_free_variable_cleanup
, var
);
1445 /* This returns the type of the variable. It also skips past typedefs
1446 to return the real type of the variable.
1448 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1449 except within get_target_type and get_type. */
1450 static struct type
*
1451 get_type (struct varobj
*var
)
1457 type
= check_typedef (type
);
1462 /* This returns the type of the variable, dereferencing references, pointers
1463 and references to pointers, too. */
1464 static struct type
*
1465 get_type_deref (struct varobj
*var
)
1469 type
= get_type (var
);
1473 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
1474 type
= get_target_type (type
);
1475 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1476 type
= get_target_type (type
);
1482 /* This returns the target type (or NULL) of TYPE, also skipping
1483 past typedefs, just like get_type ().
1485 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1486 except within get_target_type and get_type. */
1487 static struct type
*
1488 get_target_type (struct type
*type
)
1492 type
= TYPE_TARGET_TYPE (type
);
1494 type
= check_typedef (type
);
1500 /* What is the default display for this variable? We assume that
1501 everything is "natural". Any exceptions? */
1502 static enum varobj_display_formats
1503 variable_default_display (struct varobj
*var
)
1505 return FORMAT_NATURAL
;
1508 /* FIXME: The following should be generic for any pointer */
1510 cppush (struct cpstack
**pstack
, char *name
)
1514 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
1520 /* FIXME: The following should be generic for any pointer */
1522 cppop (struct cpstack
**pstack
)
1527 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
1532 *pstack
= (*pstack
)->next
;
1539 * Language-dependencies
1542 /* Common entry points */
1544 /* Get the language of variable VAR. */
1545 static enum varobj_languages
1546 variable_language (struct varobj
*var
)
1548 enum varobj_languages lang
;
1550 switch (var
->root
->exp
->language_defn
->la_language
)
1556 case language_cplus
:
1567 /* Return the number of children for a given variable.
1568 The result of this function is defined by the language
1569 implementation. The number of children returned by this function
1570 is the number of children that the user will see in the variable
1573 number_of_children (struct varobj
*var
)
1575 return (*var
->root
->lang
->number_of_children
) (var
);;
1578 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1580 name_of_variable (struct varobj
*var
)
1582 return (*var
->root
->lang
->name_of_variable
) (var
);
1585 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1587 name_of_child (struct varobj
*var
, int index
)
1589 return (*var
->root
->lang
->name_of_child
) (var
, index
);
1592 /* What is the ``struct value *'' of the root variable VAR?
1593 TYPE_CHANGED controls what to do if the type of a
1594 use_selected_frame = 1 variable changes. On input,
1595 TYPE_CHANGED = 1 means discard the old varobj, and replace
1596 it with this one. TYPE_CHANGED = 0 means leave it around.
1597 NB: In both cases, var_handle will point to the new varobj,
1598 so if you use TYPE_CHANGED = 0, you will have to stash the
1599 old varobj pointer away somewhere before calling this.
1600 On return, TYPE_CHANGED will be 1 if the type has changed, and
1602 static struct value
*
1603 value_of_root (struct varobj
**var_handle
, int *type_changed
)
1607 if (var_handle
== NULL
)
1612 /* This should really be an exception, since this should
1613 only get called with a root variable. */
1615 if (!is_root_p (var
))
1618 if (var
->root
->use_selected_frame
)
1620 struct varobj
*tmp_var
;
1621 char *old_type
, *new_type
;
1622 old_type
= varobj_get_type (var
);
1623 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
1624 USE_SELECTED_FRAME
);
1625 if (tmp_var
== NULL
)
1629 new_type
= varobj_get_type (tmp_var
);
1630 if (strcmp (old_type
, new_type
) == 0)
1632 varobj_delete (tmp_var
, NULL
, 0);
1640 savestring (var
->obj_name
, strlen (var
->obj_name
));
1641 varobj_delete (var
, NULL
, 0);
1645 tmp_var
->obj_name
= varobj_gen_name ();
1647 install_variable (tmp_var
);
1648 *var_handle
= tmp_var
;
1658 return (*var
->root
->lang
->value_of_root
) (var_handle
);
1661 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1662 static struct value
*
1663 value_of_child (struct varobj
*parent
, int index
)
1665 struct value
*value
;
1667 value
= (*parent
->root
->lang
->value_of_child
) (parent
, index
);
1672 /* Is this variable editable? Use the variable's type to make
1673 this determination. */
1675 variable_editable (struct varobj
*var
)
1677 return (*var
->root
->lang
->variable_editable
) (var
);
1680 /* GDB already has a command called "value_of_variable". Sigh. */
1682 my_value_of_variable (struct varobj
*var
)
1684 return (*var
->root
->lang
->value_of_variable
) (var
);
1688 value_get_print_value (struct value
*value
, enum varobj_display_formats format
)
1691 struct ui_file
*stb
;
1692 struct cleanup
*old_chain
;
1698 stb
= mem_fileopen ();
1699 old_chain
= make_cleanup_ui_file_delete (stb
);
1701 common_val_print (value
, stb
, format_code
[(int) format
], 1, 0, 0);
1702 thevalue
= ui_file_xstrdup (stb
, &dummy
);
1704 do_cleanups (old_chain
);
1708 /* Return non-zero if changes in value of VAR
1709 must be detected and reported by -var-update.
1710 Return zero is -var-update should never report
1711 changes of such values. This makes sense for structures
1712 (since the changes in children values will be reported separately),
1713 or for artifical objects (like 'public' pseudo-field in C++).
1715 Return value of 0 means that gdb need not call value_fetch_lazy
1716 for the value of this variable object. */
1718 varobj_value_is_changeable_p (struct varobj
*var
)
1723 if (CPLUS_FAKE_CHILD (var
))
1726 type
= get_type (var
);
1728 switch (TYPE_CODE (type
))
1730 case TYPE_CODE_STRUCT
:
1731 case TYPE_CODE_UNION
:
1732 case TYPE_CODE_ARRAY
:
1745 c_number_of_children (struct varobj
*var
)
1748 struct type
*target
;
1751 type
= get_type (var
);
1752 target
= get_target_type (type
);
1755 switch (TYPE_CODE (type
))
1757 case TYPE_CODE_ARRAY
:
1758 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
1759 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) != BOUND_CANNOT_BE_DETERMINED
)
1760 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
1762 /* If we don't know how many elements there are, don't display
1767 case TYPE_CODE_STRUCT
:
1768 case TYPE_CODE_UNION
:
1769 children
= TYPE_NFIELDS (type
);
1773 /* This is where things get complicated. All pointers have one child.
1774 Except, of course, for struct and union ptr, which we automagically
1775 dereference for the user, and function ptrs which have no children.
1776 We also don't dereference void* as we don't know what to show.
1777 We can show char* so we allow it to be dereferenced. If you decide
1778 to test for it, please mind that a little magic is necessary to
1779 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1780 TYPE_NAME == "char" */
1782 switch (TYPE_CODE (target
))
1784 case TYPE_CODE_STRUCT
:
1785 case TYPE_CODE_UNION
:
1786 children
= TYPE_NFIELDS (target
);
1789 case TYPE_CODE_FUNC
:
1790 case TYPE_CODE_VOID
:
1800 /* Other types have no children */
1808 c_name_of_variable (struct varobj
*parent
)
1810 return savestring (parent
->name
, strlen (parent
->name
));
1813 /* Return the value of element TYPE_INDEX of a structure
1814 value VALUE. VALUE's type should be a structure,
1815 or union, or a typedef to struct/union.
1817 Returns NULL if getting the value fails. Never throws. */
1818 static struct value
*
1819 value_struct_element_index (struct value
*value
, int type_index
)
1821 struct value
*result
= NULL
;
1822 volatile struct gdb_exception e
;
1824 struct type
*type
= value_type (value
);
1825 type
= check_typedef (type
);
1827 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
1828 || TYPE_CODE (type
) == TYPE_CODE_UNION
);
1830 TRY_CATCH (e
, RETURN_MASK_ERROR
)
1832 if (TYPE_FIELD_STATIC (type
, type_index
))
1833 result
= value_static_field (type
, type_index
);
1835 result
= value_primitive_field (value
, 0, type_index
, type
);
1847 /* Obtain the information about child INDEX of the variable
1849 If CNAME is not null, sets *CNAME to the name of the child relative
1851 If CVALUE is not null, sets *CVALUE to the value of the child.
1852 If CTYPE is not null, sets *CTYPE to the type of the child.
1854 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
1855 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
1858 c_describe_child (struct varobj
*parent
, int index
,
1859 char **cname
, struct value
**cvalue
, struct type
**ctype
)
1861 struct value
*value
= parent
->value
;
1862 struct type
*type
= get_type (parent
);
1871 /* Pointers to structures are treated just like
1872 structures when accessing children. */
1873 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1875 struct type
*target_type
= get_target_type (type
);
1876 if (TYPE_CODE (target_type
) == TYPE_CODE_STRUCT
1877 || TYPE_CODE (target_type
) == TYPE_CODE_UNION
)
1880 gdb_value_ind (value
, &value
);
1885 switch (TYPE_CODE (type
))
1887 case TYPE_CODE_ARRAY
:
1889 *cname
= xstrprintf ("%d", index
1890 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
1892 if (cvalue
&& value
)
1894 int real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
1895 struct value
*indval
=
1896 value_from_longest (builtin_type_int
, (LONGEST
) real_index
);
1897 gdb_value_subscript (value
, indval
, cvalue
);
1901 *ctype
= get_target_type (type
);
1905 case TYPE_CODE_STRUCT
:
1906 case TYPE_CODE_UNION
:
1909 char *string
= TYPE_FIELD_NAME (type
, index
);
1910 *cname
= savestring (string
, strlen (string
));
1913 if (cvalue
&& value
)
1915 /* For C, varobj index is the same as type index. */
1916 *cvalue
= value_struct_element_index (value
, index
);
1920 *ctype
= TYPE_FIELD_TYPE (type
, index
);
1926 *cname
= xstrprintf ("*%s", parent
->name
);
1928 if (cvalue
&& value
)
1929 gdb_value_ind (value
, cvalue
);
1932 *ctype
= get_target_type (type
);
1937 /* This should not happen */
1939 *cname
= xstrdup ("???");
1940 /* Don't set value and type, we don't know then. */
1945 c_name_of_child (struct varobj
*parent
, int index
)
1948 c_describe_child (parent
, index
, &name
, NULL
, NULL
);
1952 static struct value
*
1953 c_value_of_root (struct varobj
**var_handle
)
1955 struct value
*new_val
= NULL
;
1956 struct varobj
*var
= *var_handle
;
1957 struct frame_info
*fi
;
1960 /* Only root variables can be updated... */
1961 if (!is_root_p (var
))
1962 /* Not a root var */
1966 /* Determine whether the variable is still around. */
1967 if (var
->root
->valid_block
== NULL
|| var
->root
->use_selected_frame
)
1971 fi
= frame_find_by_id (var
->root
->frame
);
1972 within_scope
= fi
!= NULL
;
1973 /* FIXME: select_frame could fail */
1976 CORE_ADDR pc
= get_frame_pc (fi
);
1977 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
1978 pc
>= BLOCK_END (var
->root
->valid_block
))
1987 /* We need to catch errors here, because if evaluate
1988 expression fails we just want to make val->error = 1 and
1990 if (gdb_evaluate_expression (var
->root
->exp
, &new_val
))
1992 release_value (new_val
);
2001 static struct value
*
2002 c_value_of_child (struct varobj
*parent
, int index
)
2004 struct value
*value
= NULL
;
2005 c_describe_child (parent
, index
, NULL
, &value
, NULL
);
2007 release_value (value
);
2012 static struct type
*
2013 c_type_of_child (struct varobj
*parent
, int index
)
2015 struct type
*type
= NULL
;
2016 c_describe_child (parent
, index
, NULL
, NULL
, &type
);
2021 c_variable_editable (struct varobj
*var
)
2023 switch (TYPE_CODE (get_type (var
)))
2025 case TYPE_CODE_STRUCT
:
2026 case TYPE_CODE_UNION
:
2027 case TYPE_CODE_ARRAY
:
2028 case TYPE_CODE_FUNC
:
2029 case TYPE_CODE_METHOD
:
2040 c_value_of_variable (struct varobj
*var
)
2042 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2043 it will print out its children instead of "{...}". So we need to
2044 catch that case explicitly. */
2045 struct type
*type
= get_type (var
);
2047 /* Strip top-level references. */
2048 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
2049 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2051 switch (TYPE_CODE (type
))
2053 case TYPE_CODE_STRUCT
:
2054 case TYPE_CODE_UNION
:
2055 return xstrdup ("{...}");
2058 case TYPE_CODE_ARRAY
:
2061 number
= xstrprintf ("[%d]", var
->num_children
);
2068 if (var
->value
== NULL
)
2070 /* This can happen if we attempt to get the value of a struct
2071 member when the parent is an invalid pointer. This is an
2072 error condition, so we should tell the caller. */
2077 gdb_assert (varobj_value_is_changeable_p (var
));
2078 gdb_assert (!value_lazy (var
->value
));
2079 return value_get_print_value (var
->value
, var
->format
);
2089 cplus_number_of_children (struct varobj
*var
)
2092 int children
, dont_know
;
2097 if (!CPLUS_FAKE_CHILD (var
))
2099 type
= get_type_deref (var
);
2101 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2102 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2106 cplus_class_num_children (type
, kids
);
2107 if (kids
[v_public
] != 0)
2109 if (kids
[v_private
] != 0)
2111 if (kids
[v_protected
] != 0)
2114 /* Add any baseclasses */
2115 children
+= TYPE_N_BASECLASSES (type
);
2118 /* FIXME: save children in var */
2125 type
= get_type_deref (var
->parent
);
2127 cplus_class_num_children (type
, kids
);
2128 if (strcmp (var
->name
, "public") == 0)
2129 children
= kids
[v_public
];
2130 else if (strcmp (var
->name
, "private") == 0)
2131 children
= kids
[v_private
];
2133 children
= kids
[v_protected
];
2138 children
= c_number_of_children (var
);
2143 /* Compute # of public, private, and protected variables in this class.
2144 That means we need to descend into all baseclasses and find out
2145 how many are there, too. */
2147 cplus_class_num_children (struct type
*type
, int children
[3])
2151 children
[v_public
] = 0;
2152 children
[v_private
] = 0;
2153 children
[v_protected
] = 0;
2155 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
2157 /* If we have a virtual table pointer, omit it. */
2158 if (TYPE_VPTR_BASETYPE (type
) == type
&& TYPE_VPTR_FIELDNO (type
) == i
)
2161 if (TYPE_FIELD_PROTECTED (type
, i
))
2162 children
[v_protected
]++;
2163 else if (TYPE_FIELD_PRIVATE (type
, i
))
2164 children
[v_private
]++;
2166 children
[v_public
]++;
2171 cplus_name_of_variable (struct varobj
*parent
)
2173 return c_name_of_variable (parent
);
2177 cplus_name_of_child (struct varobj
*parent
, int index
)
2182 if (CPLUS_FAKE_CHILD (parent
))
2184 /* Looking for children of public, private, or protected. */
2185 type
= get_type_deref (parent
->parent
);
2188 type
= get_type_deref (parent
);
2191 switch (TYPE_CODE (type
))
2193 case TYPE_CODE_STRUCT
:
2194 case TYPE_CODE_UNION
:
2195 if (CPLUS_FAKE_CHILD (parent
))
2197 /* The fields of the class type are ordered as they
2198 appear in the class. We are given an index for a
2199 particular access control type ("public","protected",
2200 or "private"). We must skip over fields that don't
2201 have the access control we are looking for to properly
2202 find the indexed field. */
2203 int type_index
= TYPE_N_BASECLASSES (type
);
2204 if (strcmp (parent
->name
, "private") == 0)
2208 if (TYPE_VPTR_BASETYPE (type
) == type
2209 && type_index
== TYPE_VPTR_FIELDNO (type
))
2211 else if (TYPE_FIELD_PRIVATE (type
, type_index
))
2217 else if (strcmp (parent
->name
, "protected") == 0)
2221 if (TYPE_VPTR_BASETYPE (type
) == type
2222 && type_index
== TYPE_VPTR_FIELDNO (type
))
2224 else if (TYPE_FIELD_PROTECTED (type
, type_index
))
2234 if (TYPE_VPTR_BASETYPE (type
) == type
2235 && type_index
== TYPE_VPTR_FIELDNO (type
))
2237 else if (!TYPE_FIELD_PRIVATE (type
, type_index
) &&
2238 !TYPE_FIELD_PROTECTED (type
, type_index
))
2245 name
= TYPE_FIELD_NAME (type
, type_index
);
2247 else if (index
< TYPE_N_BASECLASSES (type
))
2248 /* We are looking up the name of a base class */
2249 name
= TYPE_FIELD_NAME (type
, index
);
2253 cplus_class_num_children(type
, children
);
2255 /* Everything beyond the baseclasses can
2256 only be "public", "private", or "protected"
2258 The special "fake" children are always output by varobj in
2259 this order. So if INDEX == 2, it MUST be "protected". */
2260 index
-= TYPE_N_BASECLASSES (type
);
2264 if (children
[v_public
] > 0)
2266 else if (children
[v_private
] > 0)
2272 if (children
[v_public
] > 0)
2274 if (children
[v_private
] > 0)
2279 else if (children
[v_private
] > 0)
2283 /* Must be protected */
2298 return c_name_of_child (parent
, index
);
2302 name
= savestring (name
, strlen (name
));
2308 static struct value
*
2309 cplus_value_of_root (struct varobj
**var_handle
)
2311 return c_value_of_root (var_handle
);
2314 static struct value
*
2315 cplus_value_of_child (struct varobj
*parent
, int index
)
2318 struct value
*value
;
2320 if (CPLUS_FAKE_CHILD (parent
))
2321 type
= get_type_deref (parent
->parent
);
2323 type
= get_type_deref (parent
);
2327 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2328 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2330 if (CPLUS_FAKE_CHILD (parent
))
2333 struct value
*temp
= parent
->parent
->value
;
2338 name
= name_of_child (parent
, index
);
2339 gdb_value_struct_elt (NULL
, &value
, &temp
, NULL
, name
, NULL
,
2342 release_value (value
);
2346 else if (index
>= TYPE_N_BASECLASSES (type
))
2348 /* public, private, or protected */
2354 if (parent
->value
!= NULL
)
2356 struct value
*temp
= NULL
;
2358 /* No special processing for references is needed --
2359 value_cast below handles references. */
2360 if (TYPE_CODE (value_type (parent
->value
)) == TYPE_CODE_PTR
)
2362 if (!gdb_value_ind (parent
->value
, &temp
))
2366 temp
= parent
->value
;
2370 value
= value_cast (TYPE_FIELD_TYPE (type
, index
), temp
);
2371 release_value (value
);
2375 /* We failed to evaluate the parent's value, so don't even
2376 bother trying to evaluate this child. */
2384 return c_value_of_child (parent
, index
);
2389 static struct type
*
2390 cplus_type_of_child (struct varobj
*parent
, int index
)
2392 struct type
*type
, *t
;
2394 if (CPLUS_FAKE_CHILD (parent
))
2396 /* Looking for the type of a child of public, private, or protected. */
2397 t
= get_type_deref (parent
->parent
);
2400 t
= get_type_deref (parent
);
2403 switch (TYPE_CODE (t
))
2405 case TYPE_CODE_STRUCT
:
2406 case TYPE_CODE_UNION
:
2407 if (CPLUS_FAKE_CHILD (parent
))
2409 char *name
= cplus_name_of_child (parent
, index
);
2410 type
= lookup_struct_elt_type (t
, name
, 0);
2413 else if (index
< TYPE_N_BASECLASSES (t
))
2414 type
= TYPE_FIELD_TYPE (t
, index
);
2427 return c_type_of_child (parent
, index
);
2433 cplus_variable_editable (struct varobj
*var
)
2435 if (CPLUS_FAKE_CHILD (var
))
2438 return c_variable_editable (var
);
2442 cplus_value_of_variable (struct varobj
*var
)
2445 /* If we have one of our special types, don't print out
2447 if (CPLUS_FAKE_CHILD (var
))
2448 return xstrdup ("");
2450 return c_value_of_variable (var
);
2456 java_number_of_children (struct varobj
*var
)
2458 return cplus_number_of_children (var
);
2462 java_name_of_variable (struct varobj
*parent
)
2466 name
= cplus_name_of_variable (parent
);
2467 /* If the name has "-" in it, it is because we
2468 needed to escape periods in the name... */
2471 while (*p
!= '\000')
2482 java_name_of_child (struct varobj
*parent
, int index
)
2486 name
= cplus_name_of_child (parent
, index
);
2487 /* Escape any periods in the name... */
2490 while (*p
!= '\000')
2500 static struct value
*
2501 java_value_of_root (struct varobj
**var_handle
)
2503 return cplus_value_of_root (var_handle
);
2506 static struct value
*
2507 java_value_of_child (struct varobj
*parent
, int index
)
2509 return cplus_value_of_child (parent
, index
);
2512 static struct type
*
2513 java_type_of_child (struct varobj
*parent
, int index
)
2515 return cplus_type_of_child (parent
, index
);
2519 java_variable_editable (struct varobj
*var
)
2521 return cplus_variable_editable (var
);
2525 java_value_of_variable (struct varobj
*var
)
2527 return cplus_value_of_variable (var
);
2530 extern void _initialize_varobj (void);
2532 _initialize_varobj (void)
2534 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
2536 varobj_table
= xmalloc (sizeof_table
);
2537 memset (varobj_table
, 0, sizeof_table
);
2539 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance
,
2541 Set varobj debugging."), _("\
2542 Show varobj debugging."), _("\
2543 When non-zero, varobj debugging is enabled."),
2546 &setlist
, &showlist
);