1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
22 #include "exceptions.h"
24 #include "expression.h"
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
36 /* Non-zero if we want to see trace of varobj level stuff. */
40 show_varobjdebug (struct ui_file
*file
, int from_tty
,
41 struct cmd_list_element
*c
, const char *value
)
43 fprintf_filtered (file
, _("Varobj debugging is %s.\n"), value
);
46 /* String representations of gdb's format codes */
47 char *varobj_format_string
[] =
48 { "natural", "binary", "decimal", "hexadecimal", "octal" };
50 /* String representations of gdb's known languages */
51 char *varobj_language_string
[] = { "unknown", "C", "C++", "Java" };
55 /* Every root variable has one of these structures saved in its
56 varobj. Members which must be free'd are noted. */
60 /* Alloc'd expression for this parent. */
61 struct expression
*exp
;
63 /* Block for which this expression is valid */
64 struct block
*valid_block
;
66 /* The frame for this expression */
67 struct frame_id frame
;
69 /* If 1, "update" always recomputes the frame & valid block
70 using the currently selected frame. */
71 int use_selected_frame
;
73 /* Language info for this variable and its children */
74 struct language_specific
*lang
;
76 /* The varobj for this root node. */
77 struct varobj
*rootvar
;
79 /* Next root variable */
80 struct varobj_root
*next
;
83 /* Every variable in the system has a structure of this type defined
84 for it. This structure holds all information necessary to manipulate
85 a particular object variable. Members which must be freed are noted. */
89 /* Alloc'd name of the variable for this object.. If this variable is a
90 child, then this name will be the child's source name.
92 /* NOTE: This is the "expression" */
95 /* The alloc'd name for this variable's object. This is here for
96 convenience when constructing this object's children. */
99 /* Index of this variable in its parent or -1 */
102 /* The type of this variable. This may NEVER be NULL. */
105 /* The value of this expression or subexpression. This may be NULL.
106 Invariant: if type_changeable (this) is non-zero, the value is either
107 NULL, or not lazy. */
110 /* Did an error occur evaluating the expression or getting its value? */
113 /* The number of (immediate) children this variable has */
116 /* If this object is a child, this points to its immediate parent. */
117 struct varobj
*parent
;
119 /* A list of this object's children */
120 struct varobj_child
*children
;
122 /* Description of the root variable. Points to root variable for children. */
123 struct varobj_root
*root
;
125 /* The format of the output for this object */
126 enum varobj_display_formats format
;
128 /* Was this variable updated via a varobj_set_value operation */
132 /* Every variable keeps a linked list of its children, described
133 by the following structure. */
134 /* FIXME: Deprecated. All should use vlist instead */
139 /* Pointer to the child's data */
140 struct varobj
*child
;
142 /* Pointer to the next child */
143 struct varobj_child
*next
;
146 /* A stack of varobjs */
147 /* FIXME: Deprecated. All should use vlist instead */
158 struct cpstack
*next
;
161 /* A list of varobjs */
169 /* Private function prototypes */
171 /* Helper functions for the above subcommands. */
173 static int delete_variable (struct cpstack
**, struct varobj
*, int);
175 static void delete_variable_1 (struct cpstack
**, int *,
176 struct varobj
*, int, int);
178 static int install_variable (struct varobj
*);
180 static void uninstall_variable (struct varobj
*);
182 static struct varobj
*child_exists (struct varobj
*, char *);
184 static struct varobj
*create_child (struct varobj
*, int, char *);
186 static void save_child_in_parent (struct varobj
*, struct varobj
*);
188 static void remove_child_from_parent (struct varobj
*, struct varobj
*);
190 /* Utility routines */
192 static struct varobj
*new_variable (void);
194 static struct varobj
*new_root_variable (void);
196 static void free_variable (struct varobj
*var
);
198 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
200 static struct type
*get_type (struct varobj
*var
);
202 static struct type
*get_type_deref (struct varobj
*var
);
204 static struct type
*get_target_type (struct type
*);
206 static enum varobj_display_formats
variable_default_display (struct varobj
*);
208 static void vpush (struct vstack
**pstack
, struct varobj
*var
);
210 static struct varobj
*vpop (struct vstack
**pstack
);
212 static void cppush (struct cpstack
**pstack
, char *name
);
214 static char *cppop (struct cpstack
**pstack
);
216 static int install_new_value (struct varobj
*var
, struct value
*value
,
219 /* Language-specific routines. */
221 static enum varobj_languages
variable_language (struct varobj
*var
);
223 static int number_of_children (struct varobj
*);
225 static char *name_of_variable (struct varobj
*);
227 static char *name_of_child (struct varobj
*, int);
229 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
231 static struct value
*value_of_child (struct varobj
*parent
, int index
);
233 static int variable_editable (struct varobj
*var
);
235 static char *my_value_of_variable (struct varobj
*var
);
237 static int type_changeable (struct varobj
*var
);
239 /* C implementation */
241 static int c_number_of_children (struct varobj
*var
);
243 static char *c_name_of_variable (struct varobj
*parent
);
245 static char *c_name_of_child (struct varobj
*parent
, int index
);
247 static struct value
*c_value_of_root (struct varobj
**var_handle
);
249 static struct value
*c_value_of_child (struct varobj
*parent
, int index
);
251 static struct type
*c_type_of_child (struct varobj
*parent
, int index
);
253 static int c_variable_editable (struct varobj
*var
);
255 static char *c_value_of_variable (struct varobj
*var
);
257 /* C++ implementation */
259 static int cplus_number_of_children (struct varobj
*var
);
261 static void cplus_class_num_children (struct type
*type
, int children
[3]);
263 static char *cplus_name_of_variable (struct varobj
*parent
);
265 static char *cplus_name_of_child (struct varobj
*parent
, int index
);
267 static struct value
*cplus_value_of_root (struct varobj
**var_handle
);
269 static struct value
*cplus_value_of_child (struct varobj
*parent
, int index
);
271 static struct type
*cplus_type_of_child (struct varobj
*parent
, int index
);
273 static int cplus_variable_editable (struct varobj
*var
);
275 static char *cplus_value_of_variable (struct varobj
*var
);
277 /* Java implementation */
279 static int java_number_of_children (struct varobj
*var
);
281 static char *java_name_of_variable (struct varobj
*parent
);
283 static char *java_name_of_child (struct varobj
*parent
, int index
);
285 static struct value
*java_value_of_root (struct varobj
**var_handle
);
287 static struct value
*java_value_of_child (struct varobj
*parent
, int index
);
289 static struct type
*java_type_of_child (struct varobj
*parent
, int index
);
291 static int java_variable_editable (struct varobj
*var
);
293 static char *java_value_of_variable (struct varobj
*var
);
295 /* The language specific vector */
297 struct language_specific
300 /* The language of this variable */
301 enum varobj_languages language
;
303 /* The number of children of PARENT. */
304 int (*number_of_children
) (struct varobj
* parent
);
306 /* The name (expression) of a root varobj. */
307 char *(*name_of_variable
) (struct varobj
* parent
);
309 /* The name of the INDEX'th child of PARENT. */
310 char *(*name_of_child
) (struct varobj
* parent
, int index
);
312 /* The ``struct value *'' of the root variable ROOT. */
313 struct value
*(*value_of_root
) (struct varobj
** root_handle
);
315 /* The ``struct value *'' of the INDEX'th child of PARENT. */
316 struct value
*(*value_of_child
) (struct varobj
* parent
, int index
);
318 /* The type of the INDEX'th child of PARENT. */
319 struct type
*(*type_of_child
) (struct varobj
* parent
, int index
);
321 /* Is VAR editable? */
322 int (*variable_editable
) (struct varobj
* var
);
324 /* The current value of VAR. */
325 char *(*value_of_variable
) (struct varobj
* var
);
328 /* Array of known source language routines. */
329 static struct language_specific
330 languages
[vlang_end
][sizeof (struct language_specific
)] = {
331 /* Unknown (try treating as C */
334 c_number_of_children
,
346 c_number_of_children
,
358 cplus_number_of_children
,
359 cplus_name_of_variable
,
362 cplus_value_of_child
,
364 cplus_variable_editable
,
365 cplus_value_of_variable
}
370 java_number_of_children
,
371 java_name_of_variable
,
376 java_variable_editable
,
377 java_value_of_variable
}
380 /* A little convenience enum for dealing with C++/Java */
383 v_public
= 0, v_private
, v_protected
388 /* Mappings of varobj_display_formats enums to gdb's format codes */
389 static int format_code
[] = { 0, 't', 'd', 'x', 'o' };
391 /* Header of the list of root variable objects */
392 static struct varobj_root
*rootlist
;
393 static int rootcount
= 0; /* number of root varobjs in the list */
395 /* Prime number indicating the number of buckets in the hash table */
396 /* A prime large enough to avoid too many colisions */
397 #define VAROBJ_TABLE_SIZE 227
399 /* Pointer to the varobj hash table (built at run time) */
400 static struct vlist
**varobj_table
;
402 /* Is the variable X one of our "fake" children? */
403 #define CPLUS_FAKE_CHILD(x) \
404 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
407 /* API Implementation */
409 /* Creates a varobj (not its children) */
411 /* Return the full FRAME which corresponds to the given CORE_ADDR
412 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
414 static struct frame_info
*
415 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
417 struct frame_info
*frame
= NULL
;
419 if (frame_addr
== (CORE_ADDR
) 0)
424 frame
= get_prev_frame (frame
);
427 if (get_frame_base_address (frame
) == frame_addr
)
433 varobj_create (char *objname
,
434 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
437 struct frame_info
*fi
;
438 struct frame_info
*old_fi
= NULL
;
440 struct cleanup
*old_chain
;
442 /* Fill out a varobj structure for the (root) variable being constructed. */
443 var
= new_root_variable ();
444 old_chain
= make_cleanup_free_variable (var
);
446 if (expression
!= NULL
)
449 enum varobj_languages lang
;
452 /* Parse and evaluate the expression, filling in as much
453 of the variable's data as possible */
455 /* Allow creator to specify context of variable */
456 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
457 fi
= deprecated_selected_frame
;
459 /* FIXME: cagney/2002-11-23: This code should be doing a
460 lookup using the frame ID and not just the frame's
461 ``address''. This, of course, means an interface change.
462 However, with out that interface change ISAs, such as the
463 ia64 with its two stacks, won't work. Similar goes for the
464 case where there is a frameless function. */
465 fi
= find_frame_addr_in_frame_chain (frame
);
467 /* frame = -2 means always use selected frame */
468 if (type
== USE_SELECTED_FRAME
)
469 var
->root
->use_selected_frame
= 1;
473 block
= get_frame_block (fi
, 0);
476 innermost_block
= NULL
;
477 /* Wrap the call to parse expression, so we can
478 return a sensible error. */
479 if (!gdb_parse_exp_1 (&p
, block
, 0, &var
->root
->exp
))
484 /* Don't allow variables to be created for types. */
485 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
)
487 do_cleanups (old_chain
);
488 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
489 " as an expression.\n");
493 var
->format
= variable_default_display (var
);
494 var
->root
->valid_block
= innermost_block
;
495 var
->name
= savestring (expression
, strlen (expression
));
497 /* When the frame is different from the current frame,
498 we must select the appropriate frame before parsing
499 the expression, otherwise the value will not be current.
500 Since select_frame is so benign, just call it for all cases. */
503 var
->root
->frame
= get_frame_id (fi
);
504 old_fi
= deprecated_selected_frame
;
508 /* We definitively need to catch errors here.
509 If evaluate_expression succeeds we got the value we wanted.
510 But if it fails, we still go on with a call to evaluate_type() */
511 if (!gdb_evaluate_expression (var
->root
->exp
, &value
))
512 /* Error getting the value. Try to at least get the
514 value
= evaluate_type (var
->root
->exp
);
516 var
->type
= value_type (value
);
517 install_new_value (var
, value
, 1 /* Initial assignment */);
519 /* Set language info */
520 lang
= variable_language (var
);
521 var
->root
->lang
= languages
[lang
];
523 /* Set ourselves as our root */
524 var
->root
->rootvar
= var
;
526 /* Reset the selected frame */
528 select_frame (old_fi
);
531 /* If the variable object name is null, that means this
532 is a temporary variable, so don't install it. */
534 if ((var
!= NULL
) && (objname
!= NULL
))
536 var
->obj_name
= savestring (objname
, strlen (objname
));
538 /* If a varobj name is duplicated, the install will fail so
540 if (!install_variable (var
))
542 do_cleanups (old_chain
);
547 discard_cleanups (old_chain
);
551 /* Generates an unique name that can be used for a varobj */
554 varobj_gen_name (void)
559 /* generate a name for this object */
561 obj_name
= xstrprintf ("var%d", id
);
566 /* Given an "objname", returns the pointer to the corresponding varobj
567 or NULL if not found */
570 varobj_get_handle (char *objname
)
574 unsigned int index
= 0;
577 for (chp
= objname
; *chp
; chp
++)
579 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
582 cv
= *(varobj_table
+ index
);
583 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
587 error (_("Variable object not found"));
592 /* Given the handle, return the name of the object */
595 varobj_get_objname (struct varobj
*var
)
597 return var
->obj_name
;
600 /* Given the handle, return the expression represented by the object */
603 varobj_get_expression (struct varobj
*var
)
605 return name_of_variable (var
);
608 /* Deletes a varobj and all its children if only_children == 0,
609 otherwise deletes only the children; returns a malloc'ed list of all the
610 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
613 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
617 struct cpstack
*result
= NULL
;
620 /* Initialize a stack for temporary results */
621 cppush (&result
, NULL
);
624 /* Delete only the variable children */
625 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
627 /* Delete the variable and all its children */
628 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
630 /* We may have been asked to return a list of what has been deleted */
633 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
637 *cp
= cppop (&result
);
638 while ((*cp
!= NULL
) && (mycount
> 0))
642 *cp
= cppop (&result
);
645 if (mycount
|| (*cp
!= NULL
))
646 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
653 /* Set/Get variable object display format */
655 enum varobj_display_formats
656 varobj_set_display_format (struct varobj
*var
,
657 enum varobj_display_formats format
)
664 case FORMAT_HEXADECIMAL
:
666 var
->format
= format
;
670 var
->format
= variable_default_display (var
);
676 enum varobj_display_formats
677 varobj_get_display_format (struct varobj
*var
)
683 varobj_get_num_children (struct varobj
*var
)
685 if (var
->num_children
== -1)
686 var
->num_children
= number_of_children (var
);
688 return var
->num_children
;
691 /* Creates a list of the immediate children of a variable object;
692 the return code is the number of such children or -1 on error */
695 varobj_list_children (struct varobj
*var
, struct varobj
***childlist
)
697 struct varobj
*child
;
701 /* sanity check: have we been passed a pointer? */
702 if (childlist
== NULL
)
707 if (var
->num_children
== -1)
708 var
->num_children
= number_of_children (var
);
710 /* List of children */
711 *childlist
= xmalloc ((var
->num_children
+ 1) * sizeof (struct varobj
*));
713 for (i
= 0; i
< var
->num_children
; i
++)
715 /* Mark as the end in case we bail out */
716 *((*childlist
) + i
) = NULL
;
718 /* check if child exists, if not create */
719 name
= name_of_child (var
, i
);
720 child
= child_exists (var
, name
);
722 child
= create_child (var
, i
, name
);
724 *((*childlist
) + i
) = child
;
727 /* End of list is marked by a NULL pointer */
728 *((*childlist
) + i
) = NULL
;
730 return var
->num_children
;
733 /* Obtain the type of an object Variable as a string similar to the one gdb
734 prints on the console */
737 varobj_get_type (struct varobj
*var
)
740 struct cleanup
*old_chain
;
745 /* For the "fake" variables, do not return a type. (It's type is
747 if (CPLUS_FAKE_CHILD (var
))
750 stb
= mem_fileopen ();
751 old_chain
= make_cleanup_ui_file_delete (stb
);
753 /* To print the type, we simply create a zero ``struct value *'' and
754 cast it to our type. We then typeprint this variable. */
755 val
= value_zero (var
->type
, not_lval
);
756 type_print (value_type (val
), "", stb
, -1);
758 thetype
= ui_file_xstrdup (stb
, &length
);
759 do_cleanups (old_chain
);
763 /* Obtain the type of an object variable. */
766 varobj_get_gdb_type (struct varobj
*var
)
771 enum varobj_languages
772 varobj_get_language (struct varobj
*var
)
774 return variable_language (var
);
778 varobj_get_attributes (struct varobj
*var
)
782 if (variable_editable (var
))
783 /* FIXME: define masks for attributes */
784 attributes
|= 0x00000001; /* Editable */
790 varobj_get_value (struct varobj
*var
)
792 return my_value_of_variable (var
);
795 /* Set the value of an object variable (if it is editable) to the
796 value of the given expression */
797 /* Note: Invokes functions that can call error() */
800 varobj_set_value (struct varobj
*var
, char *expression
)
806 /* The argument "expression" contains the variable's new value.
807 We need to first construct a legal expression for this -- ugh! */
808 /* Does this cover all the bases? */
809 struct expression
*exp
;
811 int saved_input_radix
= input_radix
;
813 if (var
->value
!= NULL
&& variable_editable (var
) && !var
->error
)
815 char *s
= expression
;
818 input_radix
= 10; /* ALWAYS reset to decimal temporarily */
819 exp
= parse_exp_1 (&s
, 0, 0);
820 if (!gdb_evaluate_expression (exp
, &value
))
822 /* We cannot proceed without a valid expression. */
827 /* All types that are editable must also be changeable. */
828 gdb_assert (type_changeable (var
));
830 /* The value of a changeable variable object must not be lazy. */
831 gdb_assert (!value_lazy (var
->value
));
833 /* Need to coerce the input. We want to check if the
834 value of the variable object will be different
835 after assignment, and the first thing value_assign
836 does is coerce the input.
837 For example, if we are assigning an array to a pointer variable we
838 should compare the pointer with the the array's address, not with the
840 value
= coerce_array (value
);
842 /* The new value may be lazy. gdb_value_assign, or
843 rather value_contents, will take care of this.
844 If fetching of the new value will fail, gdb_value_assign
845 with catch the exception. */
846 if (!gdb_value_assign (var
->value
, value
, &val
))
849 /* If the value has changed, record it, so that next -var-update can
850 report this change. If a variable had a value of '1', we've set it
851 to '333' and then set again to '1', when -var-update will report this
852 variable as changed -- because the first assignment has set the
853 'updated' flag. There's no need to optimize that, because return value
854 of -var-update should be considered an approximation. */
855 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
856 input_radix
= saved_input_radix
;
863 /* Returns a malloc'ed list with all root variable objects */
865 varobj_list (struct varobj
***varlist
)
868 struct varobj_root
*croot
;
869 int mycount
= rootcount
;
871 /* Alloc (rootcount + 1) entries for the result */
872 *varlist
= xmalloc ((rootcount
+ 1) * sizeof (struct varobj
*));
876 while ((croot
!= NULL
) && (mycount
> 0))
878 *cv
= croot
->rootvar
;
883 /* Mark the end of the list */
886 if (mycount
|| (croot
!= NULL
))
888 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
894 /* Assign a new value to a variable object. If INITIAL is non-zero,
895 this is the first assignement after the variable object was just
896 created, or changed type. In that case, just assign the value
898 Otherwise, assign the value and if type_changeable returns non-zero,
899 find if the new value is different from the current value.
900 Return 1 if so, and 0 if the values are equal.
902 The VALUE parameter should not be released -- the function will
903 take care of releasing it when needed. */
905 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
912 /* We need to know the varobj's type to decide if the value should
913 be fetched or not. C++ fake children (public/protected/private) don't have
915 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
916 changeable
= type_changeable (var
);
917 need_to_fetch
= changeable
;
919 /* We are not interested in the address of references, and given
920 that in C++ a reference is not rebindable, it cannot
921 meaningfully change. So, get hold of the real value. */
924 value
= coerce_ref (value
);
925 release_value (value
);
928 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
929 /* For unions, we need to fetch the value implicitly because
930 of implementation of union member fetch. When gdb
931 creates a value for a field and the value of the enclosing
932 structure is not lazy, it immediately copies the necessary
933 bytes from the enclosing values. If the enclosing value is
934 lazy, the call to value_fetch_lazy on the field will read
935 the data from memory. For unions, that means we'll read the
936 same memory more than once, which is not desirable. So
940 /* The new value might be lazy. If the type is changeable,
941 that is we'll be comparing values of this type, fetch the
942 value now. Otherwise, on the next update the old value
943 will be lazy, which means we've lost that old value. */
944 if (need_to_fetch
&& value
&& value_lazy (value
))
946 if (!gdb_value_fetch_lazy (value
))
949 /* Set the value to NULL, so that for the next -var-update,
950 we don't try to compare the new value with this value,
951 that we couldn't even read. */
958 /* If the type is changeable, compare the old and the new values.
959 If this is the initial assignment, we don't have any old value
961 if (!initial
&& changeable
)
963 /* If the value of the varobj was changed by -var-set-value, then the
964 value in the varobj and in the target is the same. However, that value
965 is different from the value that the varobj had after the previous
966 -var-update. So need to the varobj as changed. */
971 /* Try to compare the values. That requires that both
972 values are non-lazy. */
974 /* Quick comparison of NULL values. */
975 if (var
->value
== NULL
&& value
== NULL
)
978 else if (var
->value
== NULL
|| value
== NULL
)
982 gdb_assert (!value_lazy (var
->value
));
983 gdb_assert (!value_lazy (value
));
985 if (!value_contents_equal (var
->value
, value
))
991 /* We must always keep the new value, since children depend on it. */
992 if (var
->value
!= NULL
)
993 value_free (var
->value
);
997 gdb_assert (!var
->value
|| value_type (var
->value
));
1003 /* Update the values for a variable and its children. This is a
1004 two-pronged attack. First, re-parse the value for the root's
1005 expression to see if it's changed. Then go all the way
1006 through its children, reconstructing them and noting if they've
1009 -1 if there was an error updating the varobj
1010 -2 if the type changed
1011 Otherwise it is the number of children + parent changed
1013 Only root variables can be updated...
1015 NOTE: This function may delete the caller's varobj. If it
1016 returns -2, then it has done this and VARP will be modified
1017 to point to the new varobj. */
1020 varobj_update (struct varobj
**varp
, struct varobj
***changelist
)
1029 struct varobj
**templist
= NULL
;
1031 struct vstack
*stack
= NULL
;
1032 struct vstack
*result
= NULL
;
1033 struct frame_id old_fid
;
1034 struct frame_info
*fi
;
1036 /* sanity check: have we been passed a pointer? */
1037 if (changelist
== NULL
)
1040 /* Only root variables can be updated... */
1041 if ((*varp
)->root
->rootvar
!= *varp
)
1042 /* Not a root var */
1045 /* Save the selected stack frame, since we will need to change it
1046 in order to evaluate expressions. */
1047 old_fid
= get_frame_id (deprecated_selected_frame
);
1049 /* Update the root variable. value_of_root can return NULL
1050 if the variable is no longer around, i.e. we stepped out of
1051 the frame in which a local existed. We are letting the
1052 value_of_root variable dispose of the varobj if the type
1055 new = value_of_root (varp
, &type_changed
);
1057 /* Restore selected frame */
1058 fi
= frame_find_by_id (old_fid
);
1068 /* Initialize a stack for temporary results */
1069 vpush (&result
, NULL
);
1071 /* If this is a "use_selected_frame" varobj, and its type has changed,
1072 them note that it's changed. */
1075 vpush (&result
, *varp
);
1079 if (install_new_value ((*varp
), new, type_changed
))
1081 /* If type_changed is 1, install_new_value will never return
1082 non-zero, so we'll never report the same variable twice. */
1083 gdb_assert (!type_changed
);
1084 vpush (&result
, (*varp
));
1088 /* Initialize a stack */
1089 vpush (&stack
, NULL
);
1091 /* Push the root's children */
1092 if ((*varp
)->children
!= NULL
)
1094 struct varobj_child
*c
;
1095 for (c
= (*varp
)->children
; c
!= NULL
; c
= c
->next
)
1096 vpush (&stack
, c
->child
);
1099 /* Walk through the children, reconstructing them all. */
1103 /* Push any children */
1104 if (v
->children
!= NULL
)
1106 struct varobj_child
*c
;
1107 for (c
= v
->children
; c
!= NULL
; c
= c
->next
)
1108 vpush (&stack
, c
->child
);
1111 /* Update this variable */
1112 new = value_of_child (v
->parent
, v
->index
);
1113 if (install_new_value (v
, new, 0 /* type not changed */))
1115 /* Note that it's changed */
1121 /* Get next child */
1125 /* Alloc (changed + 1) list entries */
1126 /* FIXME: add a cleanup for the allocated list(s)
1127 because one day the select_frame called below can longjump */
1128 *changelist
= xmalloc ((changed
+ 1) * sizeof (struct varobj
*));
1131 templist
= xmalloc ((changed
+ 1) * sizeof (struct varobj
*));
1137 /* Copy from result stack to list */
1139 *cv
= vpop (&result
);
1140 while ((*cv
!= NULL
) && (vleft
> 0))
1144 *cv
= vpop (&result
);
1147 warning (_("varobj_update: assertion failed - vleft <> 0"));
1151 /* Now we revert the order. */
1152 for (i
= 0; i
< changed
; i
++)
1153 *(*changelist
+ i
) = *(templist
+ changed
- 1 - i
);
1154 *(*changelist
+ changed
) = NULL
;
1164 /* Helper functions */
1167 * Variable object construction/destruction
1171 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
1172 int only_children_p
)
1176 delete_variable_1 (resultp
, &delcount
, var
,
1177 only_children_p
, 1 /* remove_from_parent_p */ );
1182 /* Delete the variable object VAR and its children */
1183 /* IMPORTANT NOTE: If we delete a variable which is a child
1184 and the parent is not removed we dump core. It must be always
1185 initially called with remove_from_parent_p set */
1187 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
1188 struct varobj
*var
, int only_children_p
,
1189 int remove_from_parent_p
)
1191 struct varobj_child
*vc
;
1192 struct varobj_child
*next
;
1194 /* Delete any children of this variable, too. */
1195 for (vc
= var
->children
; vc
!= NULL
; vc
= next
)
1197 if (!remove_from_parent_p
)
1198 vc
->child
->parent
= NULL
;
1199 delete_variable_1 (resultp
, delcountp
, vc
->child
, 0, only_children_p
);
1204 /* if we were called to delete only the children we are done here */
1205 if (only_children_p
)
1208 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1209 /* If the name is null, this is a temporary variable, that has not
1210 yet been installed, don't report it, it belongs to the caller... */
1211 if (var
->obj_name
!= NULL
)
1213 cppush (resultp
, xstrdup (var
->obj_name
));
1214 *delcountp
= *delcountp
+ 1;
1217 /* If this variable has a parent, remove it from its parent's list */
1218 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1219 (as indicated by remove_from_parent_p) we don't bother doing an
1220 expensive list search to find the element to remove when we are
1221 discarding the list afterwards */
1222 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1224 remove_child_from_parent (var
->parent
, var
);
1227 if (var
->obj_name
!= NULL
)
1228 uninstall_variable (var
);
1230 /* Free memory associated with this variable */
1231 free_variable (var
);
1234 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1236 install_variable (struct varobj
*var
)
1239 struct vlist
*newvl
;
1241 unsigned int index
= 0;
1244 for (chp
= var
->obj_name
; *chp
; chp
++)
1246 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1249 cv
= *(varobj_table
+ index
);
1250 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1254 error (_("Duplicate variable object name"));
1256 /* Add varobj to hash table */
1257 newvl
= xmalloc (sizeof (struct vlist
));
1258 newvl
->next
= *(varobj_table
+ index
);
1260 *(varobj_table
+ index
) = newvl
;
1262 /* If root, add varobj to root list */
1263 if (var
->root
->rootvar
== var
)
1265 /* Add to list of root variables */
1266 if (rootlist
== NULL
)
1267 var
->root
->next
= NULL
;
1269 var
->root
->next
= rootlist
;
1270 rootlist
= var
->root
;
1277 /* Unistall the object VAR. */
1279 uninstall_variable (struct varobj
*var
)
1283 struct varobj_root
*cr
;
1284 struct varobj_root
*prer
;
1286 unsigned int index
= 0;
1289 /* Remove varobj from hash table */
1290 for (chp
= var
->obj_name
; *chp
; chp
++)
1292 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1295 cv
= *(varobj_table
+ index
);
1297 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1304 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
1309 ("Assertion failed: Could not find variable object \"%s\" to delete",
1315 *(varobj_table
+ index
) = cv
->next
;
1317 prev
->next
= cv
->next
;
1321 /* If root, remove varobj from root list */
1322 if (var
->root
->rootvar
== var
)
1324 /* Remove from list of root variables */
1325 if (rootlist
== var
->root
)
1326 rootlist
= var
->root
->next
;
1331 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
1339 ("Assertion failed: Could not find varobj \"%s\" in root list",
1346 prer
->next
= cr
->next
;
1353 /* Does a child with the name NAME exist in VAR? If so, return its data.
1354 If not, return NULL. */
1355 static struct varobj
*
1356 child_exists (struct varobj
*var
, char *name
)
1358 struct varobj_child
*vc
;
1360 for (vc
= var
->children
; vc
!= NULL
; vc
= vc
->next
)
1362 if (strcmp (vc
->child
->name
, name
) == 0)
1369 /* Create and install a child of the parent of the given name */
1370 static struct varobj
*
1371 create_child (struct varobj
*parent
, int index
, char *name
)
1373 struct varobj
*child
;
1375 struct value
*value
;
1377 child
= new_variable ();
1379 /* name is allocated by name_of_child */
1381 child
->index
= index
;
1382 value
= value_of_child (parent
, index
);
1383 child
->parent
= parent
;
1384 child
->root
= parent
->root
;
1385 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, name
);
1386 child
->obj_name
= childs_name
;
1387 install_variable (child
);
1389 /* Save a pointer to this child in the parent */
1390 save_child_in_parent (parent
, child
);
1392 /* Compute the type of the child. Must do this before
1393 calling install_new_value. */
1395 /* If the child had no evaluation errors, var->value
1396 will be non-NULL and contain a valid type. */
1397 child
->type
= value_type (value
);
1399 /* Otherwise, we must compute the type. */
1400 child
->type
= (*child
->root
->lang
->type_of_child
) (child
->parent
,
1402 install_new_value (child
, value
, 1);
1404 if ((!CPLUS_FAKE_CHILD (child
) && child
->value
== NULL
) || parent
->error
)
1410 /* FIXME: This should be a generic add to list */
1411 /* Save CHILD in the PARENT's data. */
1413 save_child_in_parent (struct varobj
*parent
, struct varobj
*child
)
1415 struct varobj_child
*vc
;
1417 /* Insert the child at the top */
1418 vc
= parent
->children
;
1420 (struct varobj_child
*) xmalloc (sizeof (struct varobj_child
));
1422 parent
->children
->next
= vc
;
1423 parent
->children
->child
= child
;
1426 /* FIXME: This should be a generic remove from list */
1427 /* Remove the CHILD from the PARENT's list of children. */
1429 remove_child_from_parent (struct varobj
*parent
, struct varobj
*child
)
1431 struct varobj_child
*vc
, *prev
;
1433 /* Find the child in the parent's list */
1435 for (vc
= parent
->children
; vc
!= NULL
;)
1437 if (vc
->child
== child
)
1444 parent
->children
= vc
->next
;
1446 prev
->next
= vc
->next
;
1452 * Miscellaneous utility functions.
1455 /* Allocate memory and initialize a new variable */
1456 static struct varobj
*
1461 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
1463 var
->obj_name
= NULL
;
1468 var
->num_children
= -1;
1470 var
->children
= NULL
;
1478 /* Allocate memory and initialize a new root variable */
1479 static struct varobj
*
1480 new_root_variable (void)
1482 struct varobj
*var
= new_variable ();
1483 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));;
1484 var
->root
->lang
= NULL
;
1485 var
->root
->exp
= NULL
;
1486 var
->root
->valid_block
= NULL
;
1487 var
->root
->frame
= null_frame_id
;
1488 var
->root
->use_selected_frame
= 0;
1489 var
->root
->rootvar
= NULL
;
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 (var
->root
->rootvar
== var
)
1501 free_current_contents (&var
->root
->exp
);
1506 xfree (var
->obj_name
);
1511 do_free_variable_cleanup (void *var
)
1513 free_variable (var
);
1516 static struct cleanup
*
1517 make_cleanup_free_variable (struct varobj
*var
)
1519 return make_cleanup (do_free_variable_cleanup
, var
);
1522 /* This returns the type of the variable. It also skips past typedefs
1523 to return the real type of the variable.
1525 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1526 except within get_target_type and get_type. */
1527 static struct type
*
1528 get_type (struct varobj
*var
)
1534 type
= check_typedef (type
);
1539 /* This returns the type of the variable, dereferencing pointers, too. */
1540 static struct type
*
1541 get_type_deref (struct varobj
*var
)
1545 type
= get_type (var
);
1547 if (type
!= NULL
&& (TYPE_CODE (type
) == TYPE_CODE_PTR
1548 || TYPE_CODE (type
) == TYPE_CODE_REF
))
1549 type
= get_target_type (type
);
1554 /* This returns the target type (or NULL) of TYPE, also skipping
1555 past typedefs, just like get_type ().
1557 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1558 except within get_target_type and get_type. */
1559 static struct type
*
1560 get_target_type (struct type
*type
)
1564 type
= TYPE_TARGET_TYPE (type
);
1566 type
= check_typedef (type
);
1572 /* What is the default display for this variable? We assume that
1573 everything is "natural". Any exceptions? */
1574 static enum varobj_display_formats
1575 variable_default_display (struct varobj
*var
)
1577 return FORMAT_NATURAL
;
1580 /* FIXME: The following should be generic for any pointer */
1582 vpush (struct vstack
**pstack
, struct varobj
*var
)
1586 s
= (struct vstack
*) xmalloc (sizeof (struct vstack
));
1592 /* FIXME: The following should be generic for any pointer */
1593 static struct varobj
*
1594 vpop (struct vstack
**pstack
)
1599 if ((*pstack
)->var
== NULL
&& (*pstack
)->next
== NULL
)
1604 *pstack
= (*pstack
)->next
;
1610 /* FIXME: The following should be generic for any pointer */
1612 cppush (struct cpstack
**pstack
, char *name
)
1616 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
1622 /* FIXME: The following should be generic for any pointer */
1624 cppop (struct cpstack
**pstack
)
1629 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
1634 *pstack
= (*pstack
)->next
;
1641 * Language-dependencies
1644 /* Common entry points */
1646 /* Get the language of variable VAR. */
1647 static enum varobj_languages
1648 variable_language (struct varobj
*var
)
1650 enum varobj_languages lang
;
1652 switch (var
->root
->exp
->language_defn
->la_language
)
1658 case language_cplus
:
1669 /* Return the number of children for a given variable.
1670 The result of this function is defined by the language
1671 implementation. The number of children returned by this function
1672 is the number of children that the user will see in the variable
1675 number_of_children (struct varobj
*var
)
1677 return (*var
->root
->lang
->number_of_children
) (var
);;
1680 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1682 name_of_variable (struct varobj
*var
)
1684 return (*var
->root
->lang
->name_of_variable
) (var
);
1687 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1689 name_of_child (struct varobj
*var
, int index
)
1691 return (*var
->root
->lang
->name_of_child
) (var
, index
);
1694 /* What is the ``struct value *'' of the root variable VAR?
1695 TYPE_CHANGED controls what to do if the type of a
1696 use_selected_frame = 1 variable changes. On input,
1697 TYPE_CHANGED = 1 means discard the old varobj, and replace
1698 it with this one. TYPE_CHANGED = 0 means leave it around.
1699 NB: In both cases, var_handle will point to the new varobj,
1700 so if you use TYPE_CHANGED = 0, you will have to stash the
1701 old varobj pointer away somewhere before calling this.
1702 On return, TYPE_CHANGED will be 1 if the type has changed, and
1704 static struct value
*
1705 value_of_root (struct varobj
**var_handle
, int *type_changed
)
1709 if (var_handle
== NULL
)
1714 /* This should really be an exception, since this should
1715 only get called with a root variable. */
1717 if (var
->root
->rootvar
!= var
)
1720 if (var
->root
->use_selected_frame
)
1722 struct varobj
*tmp_var
;
1723 char *old_type
, *new_type
;
1724 old_type
= varobj_get_type (var
);
1725 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
1726 USE_SELECTED_FRAME
);
1727 if (tmp_var
== NULL
)
1731 new_type
= varobj_get_type (tmp_var
);
1732 if (strcmp (old_type
, new_type
) == 0)
1734 varobj_delete (tmp_var
, NULL
, 0);
1742 savestring (var
->obj_name
, strlen (var
->obj_name
));
1743 varobj_delete (var
, NULL
, 0);
1747 tmp_var
->obj_name
= varobj_gen_name ();
1749 install_variable (tmp_var
);
1750 *var_handle
= tmp_var
;
1760 return (*var
->root
->lang
->value_of_root
) (var_handle
);
1763 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1764 static struct value
*
1765 value_of_child (struct varobj
*parent
, int index
)
1767 struct value
*value
;
1769 value
= (*parent
->root
->lang
->value_of_child
) (parent
, index
);
1774 /* Is this variable editable? Use the variable's type to make
1775 this determination. */
1777 variable_editable (struct varobj
*var
)
1779 return (*var
->root
->lang
->variable_editable
) (var
);
1782 /* GDB already has a command called "value_of_variable". Sigh. */
1784 my_value_of_variable (struct varobj
*var
)
1786 return (*var
->root
->lang
->value_of_variable
) (var
);
1789 /* Return non-zero if changes in value of VAR
1790 must be detected and reported by -var-update.
1791 Return zero is -var-update should never report
1792 changes of such values. This makes sense for structures
1793 (since the changes in children values will be reported separately),
1794 or for artifical objects (like 'public' pseudo-field in C++).
1796 Return value of 0 means that gdb need not call value_fetch_lazy
1797 for the value of this variable object. */
1799 type_changeable (struct varobj
*var
)
1804 if (CPLUS_FAKE_CHILD (var
))
1807 type
= get_type (var
);
1809 switch (TYPE_CODE (type
))
1811 case TYPE_CODE_STRUCT
:
1812 case TYPE_CODE_UNION
:
1813 case TYPE_CODE_ARRAY
:
1826 c_number_of_children (struct varobj
*var
)
1829 struct type
*target
;
1832 type
= get_type (var
);
1833 target
= get_target_type (type
);
1836 switch (TYPE_CODE (type
))
1838 case TYPE_CODE_ARRAY
:
1839 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
1840 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) != BOUND_CANNOT_BE_DETERMINED
)
1841 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
1846 case TYPE_CODE_STRUCT
:
1847 case TYPE_CODE_UNION
:
1848 children
= TYPE_NFIELDS (type
);
1852 /* This is where things get compilcated. All pointers have one child.
1853 Except, of course, for struct and union ptr, which we automagically
1854 dereference for the user and function ptrs, which have no children.
1855 We also don't dereference void* as we don't know what to show.
1856 We can show char* so we allow it to be dereferenced. If you decide
1857 to test for it, please mind that a little magic is necessary to
1858 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1859 TYPE_NAME == "char" */
1861 switch (TYPE_CODE (target
))
1863 case TYPE_CODE_STRUCT
:
1864 case TYPE_CODE_UNION
:
1865 children
= TYPE_NFIELDS (target
);
1868 case TYPE_CODE_FUNC
:
1869 case TYPE_CODE_VOID
:
1879 /* Other types have no children */
1887 c_name_of_variable (struct varobj
*parent
)
1889 return savestring (parent
->name
, strlen (parent
->name
));
1893 c_name_of_child (struct varobj
*parent
, int index
)
1896 struct type
*target
;
1900 type
= get_type (parent
);
1901 target
= get_target_type (type
);
1903 switch (TYPE_CODE (type
))
1905 case TYPE_CODE_ARRAY
:
1906 name
= xstrprintf ("%d", index
1907 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
1910 case TYPE_CODE_STRUCT
:
1911 case TYPE_CODE_UNION
:
1912 string
= TYPE_FIELD_NAME (type
, index
);
1913 name
= savestring (string
, strlen (string
));
1917 switch (TYPE_CODE (target
))
1919 case TYPE_CODE_STRUCT
:
1920 case TYPE_CODE_UNION
:
1921 string
= TYPE_FIELD_NAME (target
, index
);
1922 name
= savestring (string
, strlen (string
));
1926 name
= xstrprintf ("*%s", parent
->name
);
1932 /* This should not happen */
1933 name
= xstrdup ("???");
1939 static struct value
*
1940 c_value_of_root (struct varobj
**var_handle
)
1942 struct value
*new_val
;
1943 struct varobj
*var
= *var_handle
;
1944 struct frame_info
*fi
;
1947 /* Only root variables can be updated... */
1948 if (var
->root
->rootvar
!= var
)
1949 /* Not a root var */
1953 /* Determine whether the variable is still around. */
1954 if (var
->root
->valid_block
== NULL
)
1958 reinit_frame_cache ();
1959 fi
= frame_find_by_id (var
->root
->frame
);
1960 within_scope
= fi
!= NULL
;
1961 /* FIXME: select_frame could fail */
1964 CORE_ADDR pc
= get_frame_pc (fi
);
1965 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
1966 pc
>= BLOCK_END (var
->root
->valid_block
))
1974 /* We need to catch errors here, because if evaluate
1975 expression fails we just want to make val->error = 1 and
1977 if (gdb_evaluate_expression (var
->root
->exp
, &new_val
))
1980 release_value (new_val
);
1991 static struct value
*
1992 c_value_of_child (struct varobj
*parent
, int index
)
1994 struct value
*value
;
1996 struct value
*indval
;
1997 struct type
*type
, *target
;
2001 type
= get_type (parent
);
2002 target
= get_target_type (type
);
2003 name
= name_of_child (parent
, index
);
2004 temp
= parent
->value
;
2009 switch (TYPE_CODE (type
))
2011 case TYPE_CODE_ARRAY
:
2012 real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
2014 /* This breaks if the array lives in a (vector) register. */
2015 value
= value_slice (temp
, real_index
, 1);
2016 temp
= value_coerce_array (value
);
2017 gdb_value_ind (temp
, &value
);
2019 indval
= value_from_longest (builtin_type_int
, (LONGEST
) real_index
);
2020 gdb_value_subscript (temp
, indval
, &value
);
2024 case TYPE_CODE_STRUCT
:
2025 case TYPE_CODE_UNION
:
2026 gdb_value_struct_elt (NULL
, &value
, &temp
, NULL
, name
, NULL
,
2031 switch (TYPE_CODE (target
))
2033 case TYPE_CODE_STRUCT
:
2034 case TYPE_CODE_UNION
:
2035 gdb_value_struct_elt (NULL
, &value
, &temp
, NULL
, name
, NULL
,
2040 gdb_value_ind (temp
, &value
);
2051 release_value (value
);
2057 static struct type
*
2058 c_type_of_child (struct varobj
*parent
, int index
)
2061 char *name
= name_of_child (parent
, index
);
2063 switch (TYPE_CODE (parent
->type
))
2065 case TYPE_CODE_ARRAY
:
2066 type
= get_target_type (parent
->type
);
2069 case TYPE_CODE_STRUCT
:
2070 case TYPE_CODE_UNION
:
2071 type
= lookup_struct_elt_type (parent
->type
, name
, 0);
2075 switch (TYPE_CODE (get_target_type (parent
->type
)))
2077 case TYPE_CODE_STRUCT
:
2078 case TYPE_CODE_UNION
:
2079 type
= lookup_struct_elt_type (parent
->type
, name
, 0);
2083 type
= get_target_type (parent
->type
);
2089 /* This should not happen as only the above types have children */
2090 warning (_("Child of parent whose type does not allow children"));
2091 /* FIXME: Can we still go on? */
2101 c_variable_editable (struct varobj
*var
)
2103 switch (TYPE_CODE (get_type (var
)))
2105 case TYPE_CODE_STRUCT
:
2106 case TYPE_CODE_UNION
:
2107 case TYPE_CODE_ARRAY
:
2108 case TYPE_CODE_FUNC
:
2109 case TYPE_CODE_MEMBER
:
2110 case TYPE_CODE_METHOD
:
2121 c_value_of_variable (struct varobj
*var
)
2123 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2124 it will print out its children instead of "{...}". So we need to
2125 catch that case explicitly. */
2126 struct type
*type
= get_type (var
);
2128 /* Strip top-level references. */
2129 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
2130 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2132 switch (TYPE_CODE (type
))
2134 case TYPE_CODE_STRUCT
:
2135 case TYPE_CODE_UNION
:
2136 return xstrdup ("{...}");
2139 case TYPE_CODE_ARRAY
:
2142 number
= xstrprintf ("[%d]", var
->num_children
);
2149 if (var
->value
== NULL
)
2151 /* This can happen if we attempt to get the value of a struct
2152 member when the parent is an invalid pointer. This is an
2153 error condition, so we should tell the caller. */
2159 struct ui_file
*stb
= mem_fileopen ();
2160 struct cleanup
*old_chain
= make_cleanup_ui_file_delete (stb
);
2163 gdb_assert (type_changeable (var
));
2164 gdb_assert (!value_lazy (var
->value
));
2165 common_val_print (var
->value
, stb
,
2166 format_code
[(int) var
->format
], 1, 0, 0);
2167 thevalue
= ui_file_xstrdup (stb
, &dummy
);
2168 do_cleanups (old_chain
);
2179 cplus_number_of_children (struct varobj
*var
)
2182 int children
, dont_know
;
2187 if (!CPLUS_FAKE_CHILD (var
))
2189 type
= get_type_deref (var
);
2191 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2192 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2196 cplus_class_num_children (type
, kids
);
2197 if (kids
[v_public
] != 0)
2199 if (kids
[v_private
] != 0)
2201 if (kids
[v_protected
] != 0)
2204 /* Add any baseclasses */
2205 children
+= TYPE_N_BASECLASSES (type
);
2208 /* FIXME: save children in var */
2215 type
= get_type_deref (var
->parent
);
2217 cplus_class_num_children (type
, kids
);
2218 if (strcmp (var
->name
, "public") == 0)
2219 children
= kids
[v_public
];
2220 else if (strcmp (var
->name
, "private") == 0)
2221 children
= kids
[v_private
];
2223 children
= kids
[v_protected
];
2228 children
= c_number_of_children (var
);
2233 /* Compute # of public, private, and protected variables in this class.
2234 That means we need to descend into all baseclasses and find out
2235 how many are there, too. */
2237 cplus_class_num_children (struct type
*type
, int children
[3])
2241 children
[v_public
] = 0;
2242 children
[v_private
] = 0;
2243 children
[v_protected
] = 0;
2245 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
2247 /* If we have a virtual table pointer, omit it. */
2248 if (TYPE_VPTR_BASETYPE (type
) == type
&& TYPE_VPTR_FIELDNO (type
) == i
)
2251 if (TYPE_FIELD_PROTECTED (type
, i
))
2252 children
[v_protected
]++;
2253 else if (TYPE_FIELD_PRIVATE (type
, i
))
2254 children
[v_private
]++;
2256 children
[v_public
]++;
2261 cplus_name_of_variable (struct varobj
*parent
)
2263 return c_name_of_variable (parent
);
2267 cplus_name_of_child (struct varobj
*parent
, int index
)
2272 if (CPLUS_FAKE_CHILD (parent
))
2274 /* Looking for children of public, private, or protected. */
2275 type
= get_type_deref (parent
->parent
);
2278 type
= get_type_deref (parent
);
2281 switch (TYPE_CODE (type
))
2283 case TYPE_CODE_STRUCT
:
2284 case TYPE_CODE_UNION
:
2285 if (CPLUS_FAKE_CHILD (parent
))
2287 /* The fields of the class type are ordered as they
2288 appear in the class. We are given an index for a
2289 particular access control type ("public","protected",
2290 or "private"). We must skip over fields that don't
2291 have the access control we are looking for to properly
2292 find the indexed field. */
2293 int type_index
= TYPE_N_BASECLASSES (type
);
2294 if (strcmp (parent
->name
, "private") == 0)
2298 if (TYPE_VPTR_BASETYPE (type
) == type
2299 && type_index
== TYPE_VPTR_FIELDNO (type
))
2301 else if (TYPE_FIELD_PRIVATE (type
, type_index
))
2307 else if (strcmp (parent
->name
, "protected") == 0)
2311 if (TYPE_VPTR_BASETYPE (type
) == type
2312 && type_index
== TYPE_VPTR_FIELDNO (type
))
2314 else if (TYPE_FIELD_PROTECTED (type
, type_index
))
2324 if (TYPE_VPTR_BASETYPE (type
) == type
2325 && type_index
== TYPE_VPTR_FIELDNO (type
))
2327 else if (!TYPE_FIELD_PRIVATE (type
, type_index
) &&
2328 !TYPE_FIELD_PROTECTED (type
, type_index
))
2335 name
= TYPE_FIELD_NAME (type
, type_index
);
2337 else if (index
< TYPE_N_BASECLASSES (type
))
2338 /* We are looking up the name of a base class */
2339 name
= TYPE_FIELD_NAME (type
, index
);
2343 cplus_class_num_children(type
, children
);
2345 /* Everything beyond the baseclasses can
2346 only be "public", "private", or "protected"
2348 The special "fake" children are always output by varobj in
2349 this order. So if INDEX == 2, it MUST be "protected". */
2350 index
-= TYPE_N_BASECLASSES (type
);
2354 if (children
[v_public
] > 0)
2356 else if (children
[v_private
] > 0)
2362 if (children
[v_public
] > 0)
2364 if (children
[v_private
] > 0)
2369 else if (children
[v_private
] > 0)
2373 /* Must be protected */
2388 return c_name_of_child (parent
, index
);
2392 name
= savestring (name
, strlen (name
));
2398 static struct value
*
2399 cplus_value_of_root (struct varobj
**var_handle
)
2401 return c_value_of_root (var_handle
);
2404 static struct value
*
2405 cplus_value_of_child (struct varobj
*parent
, int index
)
2408 struct value
*value
;
2410 if (CPLUS_FAKE_CHILD (parent
))
2411 type
= get_type_deref (parent
->parent
);
2413 type
= get_type_deref (parent
);
2417 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2418 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2420 if (CPLUS_FAKE_CHILD (parent
))
2423 struct value
*temp
= parent
->parent
->value
;
2428 name
= name_of_child (parent
, index
);
2429 gdb_value_struct_elt (NULL
, &value
, &temp
, NULL
, name
, NULL
,
2432 release_value (value
);
2436 else if (index
>= TYPE_N_BASECLASSES (type
))
2438 /* public, private, or protected */
2444 if (parent
->value
!= NULL
)
2446 struct value
*temp
= NULL
;
2448 /* No special processing for references is needed --
2449 value_cast below handles references. */
2450 if (TYPE_CODE (value_type (parent
->value
)) == TYPE_CODE_PTR
)
2452 if (!gdb_value_ind (parent
->value
, &temp
))
2456 temp
= parent
->value
;
2460 value
= value_cast (TYPE_FIELD_TYPE (type
, index
), temp
);
2461 release_value (value
);
2465 /* We failed to evaluate the parent's value, so don't even
2466 bother trying to evaluate this child. */
2474 return c_value_of_child (parent
, index
);
2479 static struct type
*
2480 cplus_type_of_child (struct varobj
*parent
, int index
)
2482 struct type
*type
, *t
;
2484 if (CPLUS_FAKE_CHILD (parent
))
2486 /* Looking for the type of a child of public, private, or protected. */
2487 t
= get_type_deref (parent
->parent
);
2490 t
= get_type_deref (parent
);
2493 switch (TYPE_CODE (t
))
2495 case TYPE_CODE_STRUCT
:
2496 case TYPE_CODE_UNION
:
2497 if (CPLUS_FAKE_CHILD (parent
))
2499 char *name
= cplus_name_of_child (parent
, index
);
2500 type
= lookup_struct_elt_type (t
, name
, 0);
2503 else if (index
< TYPE_N_BASECLASSES (t
))
2504 type
= TYPE_FIELD_TYPE (t
, index
);
2517 return c_type_of_child (parent
, index
);
2523 cplus_variable_editable (struct varobj
*var
)
2525 if (CPLUS_FAKE_CHILD (var
))
2528 return c_variable_editable (var
);
2532 cplus_value_of_variable (struct varobj
*var
)
2535 /* If we have one of our special types, don't print out
2537 if (CPLUS_FAKE_CHILD (var
))
2538 return xstrdup ("");
2540 return c_value_of_variable (var
);
2546 java_number_of_children (struct varobj
*var
)
2548 return cplus_number_of_children (var
);
2552 java_name_of_variable (struct varobj
*parent
)
2556 name
= cplus_name_of_variable (parent
);
2557 /* If the name has "-" in it, it is because we
2558 needed to escape periods in the name... */
2561 while (*p
!= '\000')
2572 java_name_of_child (struct varobj
*parent
, int index
)
2576 name
= cplus_name_of_child (parent
, index
);
2577 /* Escape any periods in the name... */
2580 while (*p
!= '\000')
2590 static struct value
*
2591 java_value_of_root (struct varobj
**var_handle
)
2593 return cplus_value_of_root (var_handle
);
2596 static struct value
*
2597 java_value_of_child (struct varobj
*parent
, int index
)
2599 return cplus_value_of_child (parent
, index
);
2602 static struct type
*
2603 java_type_of_child (struct varobj
*parent
, int index
)
2605 return cplus_type_of_child (parent
, index
);
2609 java_variable_editable (struct varobj
*var
)
2611 return cplus_variable_editable (var
);
2615 java_value_of_variable (struct varobj
*var
)
2617 return cplus_value_of_variable (var
);
2620 extern void _initialize_varobj (void);
2622 _initialize_varobj (void)
2624 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
2626 varobj_table
= xmalloc (sizeof_table
);
2627 memset (varobj_table
, 0, sizeof_table
);
2629 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance
,
2631 Set varobj debugging."), _("\
2632 Show varobj debugging."), _("\
2633 When non-zero, varobj debugging is enabled."),
2636 &setlist
, &showlist
);