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"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
35 /* Non-zero if we want to see trace of varobj level stuff. */
39 show_varobjdebug (struct ui_file
*file
, int from_tty
,
40 struct cmd_list_element
*c
, const char *value
)
42 fprintf_filtered (file
, _("Varobj debugging is %s.\n"), value
);
45 /* String representations of gdb's format codes */
46 char *varobj_format_string
[] =
47 { "natural", "binary", "decimal", "hexadecimal", "octal" };
49 /* String representations of gdb's known languages */
50 char *varobj_language_string
[] = { "unknown", "C", "C++", "Java" };
54 /* Every root variable has one of these structures saved in its
55 varobj. Members which must be free'd are noted. */
59 /* Alloc'd expression for this parent. */
60 struct expression
*exp
;
62 /* Block for which this expression is valid */
63 struct block
*valid_block
;
65 /* The frame for this expression */
66 struct frame_id frame
;
68 /* If 1, "update" always recomputes the frame & valid block
69 using the currently selected frame. */
70 int use_selected_frame
;
72 /* Language info for this variable and its children */
73 struct language_specific
*lang
;
75 /* The varobj for this root node. */
76 struct varobj
*rootvar
;
78 /* Next root variable */
79 struct varobj_root
*next
;
82 /* Every variable in the system has a structure of this type defined
83 for it. This structure holds all information necessary to manipulate
84 a particular object variable. Members which must be freed are noted. */
88 /* Alloc'd name of the variable for this object.. If this variable is a
89 child, then this name will be the child's source name.
91 /* NOTE: This is the "expression" */
94 /* The alloc'd name for this variable's object. This is here for
95 convenience when constructing this object's children. */
98 /* Index of this variable in its parent or -1 */
101 /* The type of this variable. This may NEVER be NULL. */
104 /* The value of this expression or subexpression. This may be NULL.
105 Invariant: if type_changeable (this) is non-zero, the value is either
106 NULL, or not lazy. */
109 /* Did an error occur evaluating the expression or getting its value? */
112 /* The number of (immediate) children this variable has */
115 /* If this object is a child, this points to its immediate parent. */
116 struct varobj
*parent
;
118 /* A list of this object's children */
119 struct varobj_child
*children
;
121 /* Description of the root variable. Points to root variable for children. */
122 struct varobj_root
*root
;
124 /* The format of the output for this object */
125 enum varobj_display_formats format
;
127 /* Was this variable updated via a varobj_set_value operation */
131 /* Every variable keeps a linked list of its children, described
132 by the following structure. */
133 /* FIXME: Deprecated. All should use vlist instead */
138 /* Pointer to the child's data */
139 struct varobj
*child
;
141 /* Pointer to the next child */
142 struct varobj_child
*next
;
145 /* A stack of varobjs */
146 /* FIXME: Deprecated. All should use vlist instead */
157 struct cpstack
*next
;
160 /* A list of varobjs */
168 /* Private function prototypes */
170 /* Helper functions for the above subcommands. */
172 static int delete_variable (struct cpstack
**, struct varobj
*, int);
174 static void delete_variable_1 (struct cpstack
**, int *,
175 struct varobj
*, int, int);
177 static int install_variable (struct varobj
*);
179 static void uninstall_variable (struct varobj
*);
181 static struct varobj
*child_exists (struct varobj
*, char *);
183 static struct varobj
*create_child (struct varobj
*, int, char *);
185 static void save_child_in_parent (struct varobj
*, struct varobj
*);
187 static void remove_child_from_parent (struct varobj
*, struct varobj
*);
189 /* Utility routines */
191 static struct varobj
*new_variable (void);
193 static struct varobj
*new_root_variable (void);
195 static void free_variable (struct varobj
*var
);
197 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
199 static struct type
*get_type (struct varobj
*var
);
201 static struct type
*get_type_deref (struct varobj
*var
);
203 static struct type
*get_target_type (struct type
*);
205 static enum varobj_display_formats
variable_default_display (struct varobj
*);
207 static void vpush (struct vstack
**pstack
, struct varobj
*var
);
209 static struct varobj
*vpop (struct vstack
**pstack
);
211 static void cppush (struct cpstack
**pstack
, char *name
);
213 static char *cppop (struct cpstack
**pstack
);
215 static int install_new_value (struct varobj
*var
, struct value
*value
,
218 /* Language-specific routines. */
220 static enum varobj_languages
variable_language (struct varobj
*var
);
222 static int number_of_children (struct varobj
*);
224 static char *name_of_variable (struct varobj
*);
226 static char *name_of_child (struct varobj
*, int);
228 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
230 static struct value
*value_of_child (struct varobj
*parent
, int index
);
232 static int variable_editable (struct varobj
*var
);
234 static char *my_value_of_variable (struct varobj
*var
);
236 static int type_changeable (struct varobj
*var
);
238 /* C implementation */
240 static int c_number_of_children (struct varobj
*var
);
242 static char *c_name_of_variable (struct varobj
*parent
);
244 static char *c_name_of_child (struct varobj
*parent
, int index
);
246 static struct value
*c_value_of_root (struct varobj
**var_handle
);
248 static struct value
*c_value_of_child (struct varobj
*parent
, int index
);
250 static struct type
*c_type_of_child (struct varobj
*parent
, int index
);
252 static int c_variable_editable (struct varobj
*var
);
254 static char *c_value_of_variable (struct varobj
*var
);
256 /* C++ implementation */
258 static int cplus_number_of_children (struct varobj
*var
);
260 static void cplus_class_num_children (struct type
*type
, int children
[3]);
262 static char *cplus_name_of_variable (struct varobj
*parent
);
264 static char *cplus_name_of_child (struct varobj
*parent
, int index
);
266 static struct value
*cplus_value_of_root (struct varobj
**var_handle
);
268 static struct value
*cplus_value_of_child (struct varobj
*parent
, int index
);
270 static struct type
*cplus_type_of_child (struct varobj
*parent
, int index
);
272 static int cplus_variable_editable (struct varobj
*var
);
274 static char *cplus_value_of_variable (struct varobj
*var
);
276 /* Java implementation */
278 static int java_number_of_children (struct varobj
*var
);
280 static char *java_name_of_variable (struct varobj
*parent
);
282 static char *java_name_of_child (struct varobj
*parent
, int index
);
284 static struct value
*java_value_of_root (struct varobj
**var_handle
);
286 static struct value
*java_value_of_child (struct varobj
*parent
, int index
);
288 static struct type
*java_type_of_child (struct varobj
*parent
, int index
);
290 static int java_variable_editable (struct varobj
*var
);
292 static char *java_value_of_variable (struct varobj
*var
);
294 /* The language specific vector */
296 struct language_specific
299 /* The language of this variable */
300 enum varobj_languages language
;
302 /* The number of children of PARENT. */
303 int (*number_of_children
) (struct varobj
* parent
);
305 /* The name (expression) of a root varobj. */
306 char *(*name_of_variable
) (struct varobj
* parent
);
308 /* The name of the INDEX'th child of PARENT. */
309 char *(*name_of_child
) (struct varobj
* parent
, int index
);
311 /* The ``struct value *'' of the root variable ROOT. */
312 struct value
*(*value_of_root
) (struct varobj
** root_handle
);
314 /* The ``struct value *'' of the INDEX'th child of PARENT. */
315 struct value
*(*value_of_child
) (struct varobj
* parent
, int index
);
317 /* The type of the INDEX'th child of PARENT. */
318 struct type
*(*type_of_child
) (struct varobj
* parent
, int index
);
320 /* Is VAR editable? */
321 int (*variable_editable
) (struct varobj
* var
);
323 /* The current value of VAR. */
324 char *(*value_of_variable
) (struct varobj
* var
);
327 /* Array of known source language routines. */
328 static struct language_specific
329 languages
[vlang_end
][sizeof (struct language_specific
)] = {
330 /* Unknown (try treating as C */
333 c_number_of_children
,
345 c_number_of_children
,
357 cplus_number_of_children
,
358 cplus_name_of_variable
,
361 cplus_value_of_child
,
363 cplus_variable_editable
,
364 cplus_value_of_variable
}
369 java_number_of_children
,
370 java_name_of_variable
,
375 java_variable_editable
,
376 java_value_of_variable
}
379 /* A little convenience enum for dealing with C++/Java */
382 v_public
= 0, v_private
, v_protected
387 /* Mappings of varobj_display_formats enums to gdb's format codes */
388 static int format_code
[] = { 0, 't', 'd', 'x', 'o' };
390 /* Header of the list of root variable objects */
391 static struct varobj_root
*rootlist
;
392 static int rootcount
= 0; /* number of root varobjs in the list */
394 /* Prime number indicating the number of buckets in the hash table */
395 /* A prime large enough to avoid too many colisions */
396 #define VAROBJ_TABLE_SIZE 227
398 /* Pointer to the varobj hash table (built at run time) */
399 static struct vlist
**varobj_table
;
401 /* Is the variable X one of our "fake" children? */
402 #define CPLUS_FAKE_CHILD(x) \
403 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
406 /* API Implementation */
408 /* Creates a varobj (not its children) */
410 /* Return the full FRAME which corresponds to the given CORE_ADDR
411 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
413 static struct frame_info
*
414 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
416 struct frame_info
*frame
= NULL
;
418 if (frame_addr
== (CORE_ADDR
) 0)
423 frame
= get_prev_frame (frame
);
426 if (get_frame_base_address (frame
) == frame_addr
)
432 varobj_create (char *objname
,
433 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
436 struct frame_info
*fi
;
437 struct frame_info
*old_fi
= NULL
;
439 struct cleanup
*old_chain
;
441 /* Fill out a varobj structure for the (root) variable being constructed. */
442 var
= new_root_variable ();
443 old_chain
= make_cleanup_free_variable (var
);
445 if (expression
!= NULL
)
448 enum varobj_languages lang
;
451 /* Parse and evaluate the expression, filling in as much
452 of the variable's data as possible */
454 /* Allow creator to specify context of variable */
455 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
456 fi
= deprecated_selected_frame
;
458 /* FIXME: cagney/2002-11-23: This code should be doing a
459 lookup using the frame ID and not just the frame's
460 ``address''. This, of course, means an interface change.
461 However, with out that interface change ISAs, such as the
462 ia64 with its two stacks, won't work. Similar goes for the
463 case where there is a frameless function. */
464 fi
= find_frame_addr_in_frame_chain (frame
);
466 /* frame = -2 means always use selected frame */
467 if (type
== USE_SELECTED_FRAME
)
468 var
->root
->use_selected_frame
= 1;
472 block
= get_frame_block (fi
, 0);
475 innermost_block
= NULL
;
476 /* Wrap the call to parse expression, so we can
477 return a sensible error. */
478 if (!gdb_parse_exp_1 (&p
, block
, 0, &var
->root
->exp
))
483 /* Don't allow variables to be created for types. */
484 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
)
486 do_cleanups (old_chain
);
487 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
488 " as an expression.\n");
492 var
->format
= variable_default_display (var
);
493 var
->root
->valid_block
= innermost_block
;
494 var
->name
= savestring (expression
, strlen (expression
));
496 /* When the frame is different from the current frame,
497 we must select the appropriate frame before parsing
498 the expression, otherwise the value will not be current.
499 Since select_frame is so benign, just call it for all cases. */
502 var
->root
->frame
= get_frame_id (fi
);
503 old_fi
= deprecated_selected_frame
;
507 /* We definitively need to catch errors here.
508 If evaluate_expression succeeds we got the value we wanted.
509 But if it fails, we still go on with a call to evaluate_type() */
510 if (!gdb_evaluate_expression (var
->root
->exp
, &value
))
511 /* Error getting the value. Try to at least get the
513 value
= evaluate_type (var
->root
->exp
);
515 var
->type
= value_type (value
);
516 install_new_value (var
, value
, 1 /* Initial assignment */);
518 /* Set language info */
519 lang
= variable_language (var
);
520 var
->root
->lang
= languages
[lang
];
522 /* Set ourselves as our root */
523 var
->root
->rootvar
= var
;
525 /* Reset the selected frame */
527 select_frame (old_fi
);
530 /* If the variable object name is null, that means this
531 is a temporary variable, so don't install it. */
533 if ((var
!= NULL
) && (objname
!= NULL
))
535 var
->obj_name
= savestring (objname
, strlen (objname
));
537 /* If a varobj name is duplicated, the install will fail so
539 if (!install_variable (var
))
541 do_cleanups (old_chain
);
546 discard_cleanups (old_chain
);
550 /* Generates an unique name that can be used for a varobj */
553 varobj_gen_name (void)
558 /* generate a name for this object */
560 obj_name
= xstrprintf ("var%d", id
);
565 /* Given an "objname", returns the pointer to the corresponding varobj
566 or NULL if not found */
569 varobj_get_handle (char *objname
)
573 unsigned int index
= 0;
576 for (chp
= objname
; *chp
; chp
++)
578 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
581 cv
= *(varobj_table
+ index
);
582 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
586 error (_("Variable object not found"));
591 /* Given the handle, return the name of the object */
594 varobj_get_objname (struct varobj
*var
)
596 return var
->obj_name
;
599 /* Given the handle, return the expression represented by the object */
602 varobj_get_expression (struct varobj
*var
)
604 return name_of_variable (var
);
607 /* Deletes a varobj and all its children if only_children == 0,
608 otherwise deletes only the children; returns a malloc'ed list of all the
609 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
612 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
616 struct cpstack
*result
= NULL
;
619 /* Initialize a stack for temporary results */
620 cppush (&result
, NULL
);
623 /* Delete only the variable children */
624 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
626 /* Delete the variable and all its children */
627 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
629 /* We may have been asked to return a list of what has been deleted */
632 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
636 *cp
= cppop (&result
);
637 while ((*cp
!= NULL
) && (mycount
> 0))
641 *cp
= cppop (&result
);
644 if (mycount
|| (*cp
!= NULL
))
645 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
652 /* Set/Get variable object display format */
654 enum varobj_display_formats
655 varobj_set_display_format (struct varobj
*var
,
656 enum varobj_display_formats format
)
663 case FORMAT_HEXADECIMAL
:
665 var
->format
= format
;
669 var
->format
= variable_default_display (var
);
675 enum varobj_display_formats
676 varobj_get_display_format (struct varobj
*var
)
682 varobj_get_num_children (struct varobj
*var
)
684 if (var
->num_children
== -1)
685 var
->num_children
= number_of_children (var
);
687 return var
->num_children
;
690 /* Creates a list of the immediate children of a variable object;
691 the return code is the number of such children or -1 on error */
694 varobj_list_children (struct varobj
*var
, struct varobj
***childlist
)
696 struct varobj
*child
;
700 /* sanity check: have we been passed a pointer? */
701 if (childlist
== NULL
)
706 if (var
->num_children
== -1)
707 var
->num_children
= number_of_children (var
);
709 /* List of children */
710 *childlist
= xmalloc ((var
->num_children
+ 1) * sizeof (struct varobj
*));
712 for (i
= 0; i
< var
->num_children
; i
++)
714 /* Mark as the end in case we bail out */
715 *((*childlist
) + i
) = NULL
;
717 /* check if child exists, if not create */
718 name
= name_of_child (var
, i
);
719 child
= child_exists (var
, name
);
721 child
= create_child (var
, i
, name
);
723 *((*childlist
) + i
) = child
;
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
) && !var
->error
)
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 (type_changeable (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
)
911 /* We need to know the varobj's type to decide if the value should
912 be fetched or not. C++ fake children (public/protected/private) don't have
914 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
915 changeable
= type_changeable (var
);
916 need_to_fetch
= changeable
;
918 /* We are not interested in the address of references, and given
919 that in C++ a reference is not rebindable, it cannot
920 meaningfully change. So, get hold of the real value. */
923 value
= coerce_ref (value
);
924 release_value (value
);
927 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
928 /* For unions, we need to fetch the value implicitly because
929 of implementation of union member fetch. When gdb
930 creates a value for a field and the value of the enclosing
931 structure is not lazy, it immediately copies the necessary
932 bytes from the enclosing values. If the enclosing value is
933 lazy, the call to value_fetch_lazy on the field will read
934 the data from memory. For unions, that means we'll read the
935 same memory more than once, which is not desirable. So
939 /* The new value might be lazy. If the type is changeable,
940 that is we'll be comparing values of this type, fetch the
941 value now. Otherwise, on the next update the old value
942 will be lazy, which means we've lost that old value. */
943 if (need_to_fetch
&& value
&& value_lazy (value
))
945 if (!gdb_value_fetch_lazy (value
))
948 /* Set the value to NULL, so that for the next -var-update,
949 we don't try to compare the new value with this value,
950 that we couldn't even read. */
957 /* If the type is changeable, compare the old and the new values.
958 If this is the initial assignment, we don't have any old value
960 if (!initial
&& changeable
)
962 /* If the value of the varobj was changed by -var-set-value, then the
963 value in the varobj and in the target is the same. However, that value
964 is different from the value that the varobj had after the previous
965 -var-update. So need to the varobj as changed. */
970 /* Try to compare the values. That requires that both
971 values are non-lazy. */
973 /* Quick comparison of NULL values. */
974 if (var
->value
== NULL
&& value
== NULL
)
977 else if (var
->value
== NULL
|| value
== NULL
)
981 gdb_assert (!value_lazy (var
->value
));
982 gdb_assert (!value_lazy (value
));
984 if (!value_contents_equal (var
->value
, value
))
990 /* We must always keep the new value, since children depend on it. */
991 if (var
->value
!= NULL
)
992 value_free (var
->value
);
996 gdb_assert (!var
->value
|| value_type (var
->value
));
1002 /* Update the values for a variable and its children. This is a
1003 two-pronged attack. First, re-parse the value for the root's
1004 expression to see if it's changed. Then go all the way
1005 through its children, reconstructing them and noting if they've
1008 -1 if there was an error updating the varobj
1009 -2 if the type changed
1010 Otherwise it is the number of children + parent changed
1012 Only root variables can be updated...
1014 NOTE: This function may delete the caller's varobj. If it
1015 returns -2, then it has done this and VARP will be modified
1016 to point to the new varobj. */
1019 varobj_update (struct varobj
**varp
, struct varobj
***changelist
)
1028 struct varobj
**templist
= NULL
;
1030 struct vstack
*stack
= NULL
;
1031 struct vstack
*result
= NULL
;
1032 struct frame_id old_fid
;
1033 struct frame_info
*fi
;
1035 /* sanity check: have we been passed a pointer? */
1036 if (changelist
== NULL
)
1039 /* Only root variables can be updated... */
1040 if ((*varp
)->root
->rootvar
!= *varp
)
1041 /* Not a root var */
1044 /* Save the selected stack frame, since we will need to change it
1045 in order to evaluate expressions. */
1046 old_fid
= get_frame_id (deprecated_selected_frame
);
1048 /* Update the root variable. value_of_root can return NULL
1049 if the variable is no longer around, i.e. we stepped out of
1050 the frame in which a local existed. We are letting the
1051 value_of_root variable dispose of the varobj if the type
1054 new = value_of_root (varp
, &type_changed
);
1056 /* Restore selected frame */
1057 fi
= frame_find_by_id (old_fid
);
1067 /* Initialize a stack for temporary results */
1068 vpush (&result
, NULL
);
1070 /* If this is a "use_selected_frame" varobj, and its type has changed,
1071 them note that it's changed. */
1074 vpush (&result
, *varp
);
1078 if (install_new_value ((*varp
), new, type_changed
))
1080 /* If type_changed is 1, install_new_value will never return
1081 non-zero, so we'll never report the same variable twice. */
1082 gdb_assert (!type_changed
);
1083 vpush (&result
, (*varp
));
1087 /* Initialize a stack */
1088 vpush (&stack
, NULL
);
1090 /* Push the root's children */
1091 if ((*varp
)->children
!= NULL
)
1093 struct varobj_child
*c
;
1094 for (c
= (*varp
)->children
; c
!= NULL
; c
= c
->next
)
1095 vpush (&stack
, c
->child
);
1098 /* Walk through the children, reconstructing them all. */
1102 /* Push any children */
1103 if (v
->children
!= NULL
)
1105 struct varobj_child
*c
;
1106 for (c
= v
->children
; c
!= NULL
; c
= c
->next
)
1107 vpush (&stack
, c
->child
);
1110 /* Update this variable */
1111 new = value_of_child (v
->parent
, v
->index
);
1112 if (install_new_value (v
, new, 0 /* type not changed */))
1114 /* Note that it's changed */
1120 /* Get next child */
1124 /* Alloc (changed + 1) list entries */
1125 /* FIXME: add a cleanup for the allocated list(s)
1126 because one day the select_frame called below can longjump */
1127 *changelist
= xmalloc ((changed
+ 1) * sizeof (struct varobj
*));
1130 templist
= xmalloc ((changed
+ 1) * sizeof (struct varobj
*));
1136 /* Copy from result stack to list */
1138 *cv
= vpop (&result
);
1139 while ((*cv
!= NULL
) && (vleft
> 0))
1143 *cv
= vpop (&result
);
1146 warning (_("varobj_update: assertion failed - vleft <> 0"));
1150 /* Now we revert the order. */
1151 for (i
= 0; i
< changed
; i
++)
1152 *(*changelist
+ i
) = *(templist
+ changed
- 1 - i
);
1153 *(*changelist
+ changed
) = NULL
;
1163 /* Helper functions */
1166 * Variable object construction/destruction
1170 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
1171 int only_children_p
)
1175 delete_variable_1 (resultp
, &delcount
, var
,
1176 only_children_p
, 1 /* remove_from_parent_p */ );
1181 /* Delete the variable object VAR and its children */
1182 /* IMPORTANT NOTE: If we delete a variable which is a child
1183 and the parent is not removed we dump core. It must be always
1184 initially called with remove_from_parent_p set */
1186 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
1187 struct varobj
*var
, int only_children_p
,
1188 int remove_from_parent_p
)
1190 struct varobj_child
*vc
;
1191 struct varobj_child
*next
;
1193 /* Delete any children of this variable, too. */
1194 for (vc
= var
->children
; vc
!= NULL
; vc
= next
)
1196 if (!remove_from_parent_p
)
1197 vc
->child
->parent
= NULL
;
1198 delete_variable_1 (resultp
, delcountp
, vc
->child
, 0, only_children_p
);
1203 /* if we were called to delete only the children we are done here */
1204 if (only_children_p
)
1207 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1208 /* If the name is null, this is a temporary variable, that has not
1209 yet been installed, don't report it, it belongs to the caller... */
1210 if (var
->obj_name
!= NULL
)
1212 cppush (resultp
, xstrdup (var
->obj_name
));
1213 *delcountp
= *delcountp
+ 1;
1216 /* If this variable has a parent, remove it from its parent's list */
1217 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1218 (as indicated by remove_from_parent_p) we don't bother doing an
1219 expensive list search to find the element to remove when we are
1220 discarding the list afterwards */
1221 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1223 remove_child_from_parent (var
->parent
, var
);
1226 if (var
->obj_name
!= NULL
)
1227 uninstall_variable (var
);
1229 /* Free memory associated with this variable */
1230 free_variable (var
);
1233 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1235 install_variable (struct varobj
*var
)
1238 struct vlist
*newvl
;
1240 unsigned int index
= 0;
1243 for (chp
= var
->obj_name
; *chp
; chp
++)
1245 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1248 cv
= *(varobj_table
+ index
);
1249 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1253 error (_("Duplicate variable object name"));
1255 /* Add varobj to hash table */
1256 newvl
= xmalloc (sizeof (struct vlist
));
1257 newvl
->next
= *(varobj_table
+ index
);
1259 *(varobj_table
+ index
) = newvl
;
1261 /* If root, add varobj to root list */
1262 if (var
->root
->rootvar
== var
)
1264 /* Add to list of root variables */
1265 if (rootlist
== NULL
)
1266 var
->root
->next
= NULL
;
1268 var
->root
->next
= rootlist
;
1269 rootlist
= var
->root
;
1276 /* Unistall the object VAR. */
1278 uninstall_variable (struct varobj
*var
)
1282 struct varobj_root
*cr
;
1283 struct varobj_root
*prer
;
1285 unsigned int index
= 0;
1288 /* Remove varobj from hash table */
1289 for (chp
= var
->obj_name
; *chp
; chp
++)
1291 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1294 cv
= *(varobj_table
+ index
);
1296 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1303 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
1308 ("Assertion failed: Could not find variable object \"%s\" to delete",
1314 *(varobj_table
+ index
) = cv
->next
;
1316 prev
->next
= cv
->next
;
1320 /* If root, remove varobj from root list */
1321 if (var
->root
->rootvar
== var
)
1323 /* Remove from list of root variables */
1324 if (rootlist
== var
->root
)
1325 rootlist
= var
->root
->next
;
1330 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
1338 ("Assertion failed: Could not find varobj \"%s\" in root list",
1345 prer
->next
= cr
->next
;
1352 /* Does a child with the name NAME exist in VAR? If so, return its data.
1353 If not, return NULL. */
1354 static struct varobj
*
1355 child_exists (struct varobj
*var
, char *name
)
1357 struct varobj_child
*vc
;
1359 for (vc
= var
->children
; vc
!= NULL
; vc
= vc
->next
)
1361 if (strcmp (vc
->child
->name
, name
) == 0)
1368 /* Create and install a child of the parent of the given name */
1369 static struct varobj
*
1370 create_child (struct varobj
*parent
, int index
, char *name
)
1372 struct varobj
*child
;
1374 struct value
*value
;
1376 child
= new_variable ();
1378 /* name is allocated by name_of_child */
1380 child
->index
= index
;
1381 value
= value_of_child (parent
, index
);
1382 child
->parent
= parent
;
1383 child
->root
= parent
->root
;
1384 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, name
);
1385 child
->obj_name
= childs_name
;
1386 install_variable (child
);
1388 /* Save a pointer to this child in the parent */
1389 save_child_in_parent (parent
, child
);
1391 /* Compute the type of the child. Must do this before
1392 calling install_new_value. */
1394 /* If the child had no evaluation errors, var->value
1395 will be non-NULL and contain a valid type. */
1396 child
->type
= value_type (value
);
1398 /* Otherwise, we must compute the type. */
1399 child
->type
= (*child
->root
->lang
->type_of_child
) (child
->parent
,
1401 install_new_value (child
, value
, 1);
1403 if ((!CPLUS_FAKE_CHILD (child
) && child
->value
== NULL
) || parent
->error
)
1409 /* FIXME: This should be a generic add to list */
1410 /* Save CHILD in the PARENT's data. */
1412 save_child_in_parent (struct varobj
*parent
, struct varobj
*child
)
1414 struct varobj_child
*vc
;
1416 /* Insert the child at the top */
1417 vc
= parent
->children
;
1419 (struct varobj_child
*) xmalloc (sizeof (struct varobj_child
));
1421 parent
->children
->next
= vc
;
1422 parent
->children
->child
= child
;
1425 /* FIXME: This should be a generic remove from list */
1426 /* Remove the CHILD from the PARENT's list of children. */
1428 remove_child_from_parent (struct varobj
*parent
, struct varobj
*child
)
1430 struct varobj_child
*vc
, *prev
;
1432 /* Find the child in the parent's list */
1434 for (vc
= parent
->children
; vc
!= NULL
;)
1436 if (vc
->child
== child
)
1443 parent
->children
= vc
->next
;
1445 prev
->next
= vc
->next
;
1451 * Miscellaneous utility functions.
1454 /* Allocate memory and initialize a new variable */
1455 static struct varobj
*
1460 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
1462 var
->obj_name
= NULL
;
1467 var
->num_children
= -1;
1469 var
->children
= NULL
;
1477 /* Allocate memory and initialize a new root variable */
1478 static struct varobj
*
1479 new_root_variable (void)
1481 struct varobj
*var
= new_variable ();
1482 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));;
1483 var
->root
->lang
= NULL
;
1484 var
->root
->exp
= NULL
;
1485 var
->root
->valid_block
= NULL
;
1486 var
->root
->frame
= null_frame_id
;
1487 var
->root
->use_selected_frame
= 0;
1488 var
->root
->rootvar
= NULL
;
1493 /* Free any allocated memory associated with VAR. */
1495 free_variable (struct varobj
*var
)
1497 /* Free the expression if this is a root variable. */
1498 if (var
->root
->rootvar
== var
)
1500 free_current_contents (&var
->root
->exp
);
1505 xfree (var
->obj_name
);
1510 do_free_variable_cleanup (void *var
)
1512 free_variable (var
);
1515 static struct cleanup
*
1516 make_cleanup_free_variable (struct varobj
*var
)
1518 return make_cleanup (do_free_variable_cleanup
, var
);
1521 /* This returns the type of the variable. It also skips past typedefs
1522 to return the real type of the variable.
1524 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1525 except within get_target_type and get_type. */
1526 static struct type
*
1527 get_type (struct varobj
*var
)
1533 type
= check_typedef (type
);
1538 /* This returns the type of the variable, dereferencing pointers, too. */
1539 static struct type
*
1540 get_type_deref (struct varobj
*var
)
1544 type
= get_type (var
);
1546 if (type
!= NULL
&& (TYPE_CODE (type
) == TYPE_CODE_PTR
1547 || TYPE_CODE (type
) == TYPE_CODE_REF
))
1548 type
= get_target_type (type
);
1553 /* This returns the target type (or NULL) of TYPE, also skipping
1554 past typedefs, just like get_type ().
1556 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1557 except within get_target_type and get_type. */
1558 static struct type
*
1559 get_target_type (struct type
*type
)
1563 type
= TYPE_TARGET_TYPE (type
);
1565 type
= check_typedef (type
);
1571 /* What is the default display for this variable? We assume that
1572 everything is "natural". Any exceptions? */
1573 static enum varobj_display_formats
1574 variable_default_display (struct varobj
*var
)
1576 return FORMAT_NATURAL
;
1579 /* FIXME: The following should be generic for any pointer */
1581 vpush (struct vstack
**pstack
, struct varobj
*var
)
1585 s
= (struct vstack
*) xmalloc (sizeof (struct vstack
));
1591 /* FIXME: The following should be generic for any pointer */
1592 static struct varobj
*
1593 vpop (struct vstack
**pstack
)
1598 if ((*pstack
)->var
== NULL
&& (*pstack
)->next
== NULL
)
1603 *pstack
= (*pstack
)->next
;
1609 /* FIXME: The following should be generic for any pointer */
1611 cppush (struct cpstack
**pstack
, char *name
)
1615 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
1621 /* FIXME: The following should be generic for any pointer */
1623 cppop (struct cpstack
**pstack
)
1628 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
1633 *pstack
= (*pstack
)->next
;
1640 * Language-dependencies
1643 /* Common entry points */
1645 /* Get the language of variable VAR. */
1646 static enum varobj_languages
1647 variable_language (struct varobj
*var
)
1649 enum varobj_languages lang
;
1651 switch (var
->root
->exp
->language_defn
->la_language
)
1657 case language_cplus
:
1668 /* Return the number of children for a given variable.
1669 The result of this function is defined by the language
1670 implementation. The number of children returned by this function
1671 is the number of children that the user will see in the variable
1674 number_of_children (struct varobj
*var
)
1676 return (*var
->root
->lang
->number_of_children
) (var
);;
1679 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1681 name_of_variable (struct varobj
*var
)
1683 return (*var
->root
->lang
->name_of_variable
) (var
);
1686 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1688 name_of_child (struct varobj
*var
, int index
)
1690 return (*var
->root
->lang
->name_of_child
) (var
, index
);
1693 /* What is the ``struct value *'' of the root variable VAR?
1694 TYPE_CHANGED controls what to do if the type of a
1695 use_selected_frame = 1 variable changes. On input,
1696 TYPE_CHANGED = 1 means discard the old varobj, and replace
1697 it with this one. TYPE_CHANGED = 0 means leave it around.
1698 NB: In both cases, var_handle will point to the new varobj,
1699 so if you use TYPE_CHANGED = 0, you will have to stash the
1700 old varobj pointer away somewhere before calling this.
1701 On return, TYPE_CHANGED will be 1 if the type has changed, and
1703 static struct value
*
1704 value_of_root (struct varobj
**var_handle
, int *type_changed
)
1708 if (var_handle
== NULL
)
1713 /* This should really be an exception, since this should
1714 only get called with a root variable. */
1716 if (var
->root
->rootvar
!= var
)
1719 if (var
->root
->use_selected_frame
)
1721 struct varobj
*tmp_var
;
1722 char *old_type
, *new_type
;
1723 old_type
= varobj_get_type (var
);
1724 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
1725 USE_SELECTED_FRAME
);
1726 if (tmp_var
== NULL
)
1730 new_type
= varobj_get_type (tmp_var
);
1731 if (strcmp (old_type
, new_type
) == 0)
1733 varobj_delete (tmp_var
, NULL
, 0);
1741 savestring (var
->obj_name
, strlen (var
->obj_name
));
1742 varobj_delete (var
, NULL
, 0);
1746 tmp_var
->obj_name
= varobj_gen_name ();
1748 install_variable (tmp_var
);
1749 *var_handle
= tmp_var
;
1759 return (*var
->root
->lang
->value_of_root
) (var_handle
);
1762 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1763 static struct value
*
1764 value_of_child (struct varobj
*parent
, int index
)
1766 struct value
*value
;
1768 value
= (*parent
->root
->lang
->value_of_child
) (parent
, index
);
1773 /* Is this variable editable? Use the variable's type to make
1774 this determination. */
1776 variable_editable (struct varobj
*var
)
1778 return (*var
->root
->lang
->variable_editable
) (var
);
1781 /* GDB already has a command called "value_of_variable". Sigh. */
1783 my_value_of_variable (struct varobj
*var
)
1785 return (*var
->root
->lang
->value_of_variable
) (var
);
1788 /* Return non-zero if changes in value of VAR
1789 must be detected and reported by -var-update.
1790 Return zero is -var-update should never report
1791 changes of such values. This makes sense for structures
1792 (since the changes in children values will be reported separately),
1793 or for artifical objects (like 'public' pseudo-field in C++).
1795 Return value of 0 means that gdb need not call value_fetch_lazy
1796 for the value of this variable object. */
1798 type_changeable (struct varobj
*var
)
1803 if (CPLUS_FAKE_CHILD (var
))
1806 type
= get_type (var
);
1808 switch (TYPE_CODE (type
))
1810 case TYPE_CODE_STRUCT
:
1811 case TYPE_CODE_UNION
:
1812 case TYPE_CODE_ARRAY
:
1825 c_number_of_children (struct varobj
*var
)
1828 struct type
*target
;
1831 type
= get_type (var
);
1832 target
= get_target_type (type
);
1835 switch (TYPE_CODE (type
))
1837 case TYPE_CODE_ARRAY
:
1838 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
1839 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) != BOUND_CANNOT_BE_DETERMINED
)
1840 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
1845 case TYPE_CODE_STRUCT
:
1846 case TYPE_CODE_UNION
:
1847 children
= TYPE_NFIELDS (type
);
1851 /* This is where things get compilcated. All pointers have one child.
1852 Except, of course, for struct and union ptr, which we automagically
1853 dereference for the user and function ptrs, which have no children.
1854 We also don't dereference void* as we don't know what to show.
1855 We can show char* so we allow it to be dereferenced. If you decide
1856 to test for it, please mind that a little magic is necessary to
1857 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1858 TYPE_NAME == "char" */
1860 switch (TYPE_CODE (target
))
1862 case TYPE_CODE_STRUCT
:
1863 case TYPE_CODE_UNION
:
1864 children
= TYPE_NFIELDS (target
);
1867 case TYPE_CODE_FUNC
:
1868 case TYPE_CODE_VOID
:
1878 /* Other types have no children */
1886 c_name_of_variable (struct varobj
*parent
)
1888 return savestring (parent
->name
, strlen (parent
->name
));
1892 c_name_of_child (struct varobj
*parent
, int index
)
1895 struct type
*target
;
1899 type
= get_type (parent
);
1900 target
= get_target_type (type
);
1902 switch (TYPE_CODE (type
))
1904 case TYPE_CODE_ARRAY
:
1905 name
= xstrprintf ("%d", index
1906 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
1909 case TYPE_CODE_STRUCT
:
1910 case TYPE_CODE_UNION
:
1911 string
= TYPE_FIELD_NAME (type
, index
);
1912 name
= savestring (string
, strlen (string
));
1916 switch (TYPE_CODE (target
))
1918 case TYPE_CODE_STRUCT
:
1919 case TYPE_CODE_UNION
:
1920 string
= TYPE_FIELD_NAME (target
, index
);
1921 name
= savestring (string
, strlen (string
));
1925 name
= xstrprintf ("*%s", parent
->name
);
1931 /* This should not happen */
1932 name
= xstrdup ("???");
1938 static struct value
*
1939 c_value_of_root (struct varobj
**var_handle
)
1941 struct value
*new_val
;
1942 struct varobj
*var
= *var_handle
;
1943 struct frame_info
*fi
;
1946 /* Only root variables can be updated... */
1947 if (var
->root
->rootvar
!= var
)
1948 /* Not a root var */
1952 /* Determine whether the variable is still around. */
1953 if (var
->root
->valid_block
== NULL
)
1957 reinit_frame_cache ();
1958 fi
= frame_find_by_id (var
->root
->frame
);
1959 within_scope
= fi
!= NULL
;
1960 /* FIXME: select_frame could fail */
1967 /* We need to catch errors here, because if evaluate
1968 expression fails we just want to make val->error = 1 and
1970 if (gdb_evaluate_expression (var
->root
->exp
, &new_val
))
1973 release_value (new_val
);
1984 static struct value
*
1985 c_value_of_child (struct varobj
*parent
, int index
)
1987 struct value
*value
;
1989 struct value
*indval
;
1990 struct type
*type
, *target
;
1994 type
= get_type (parent
);
1995 target
= get_target_type (type
);
1996 name
= name_of_child (parent
, index
);
1997 temp
= parent
->value
;
2002 switch (TYPE_CODE (type
))
2004 case TYPE_CODE_ARRAY
:
2005 real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
2007 /* This breaks if the array lives in a (vector) register. */
2008 value
= value_slice (temp
, real_index
, 1);
2009 temp
= value_coerce_array (value
);
2010 gdb_value_ind (temp
, &value
);
2012 indval
= value_from_longest (builtin_type_int
, (LONGEST
) real_index
);
2013 gdb_value_subscript (temp
, indval
, &value
);
2017 case TYPE_CODE_STRUCT
:
2018 case TYPE_CODE_UNION
:
2019 gdb_value_struct_elt (NULL
, &value
, &temp
, NULL
, name
, NULL
,
2024 switch (TYPE_CODE (target
))
2026 case TYPE_CODE_STRUCT
:
2027 case TYPE_CODE_UNION
:
2028 gdb_value_struct_elt (NULL
, &value
, &temp
, NULL
, name
, NULL
,
2033 gdb_value_ind (temp
, &value
);
2044 release_value (value
);
2050 static struct type
*
2051 c_type_of_child (struct varobj
*parent
, int index
)
2054 char *name
= name_of_child (parent
, index
);
2056 switch (TYPE_CODE (parent
->type
))
2058 case TYPE_CODE_ARRAY
:
2059 type
= get_target_type (parent
->type
);
2062 case TYPE_CODE_STRUCT
:
2063 case TYPE_CODE_UNION
:
2064 type
= lookup_struct_elt_type (parent
->type
, name
, 0);
2068 switch (TYPE_CODE (get_target_type (parent
->type
)))
2070 case TYPE_CODE_STRUCT
:
2071 case TYPE_CODE_UNION
:
2072 type
= lookup_struct_elt_type (parent
->type
, name
, 0);
2076 type
= get_target_type (parent
->type
);
2082 /* This should not happen as only the above types have children */
2083 warning (_("Child of parent whose type does not allow children"));
2084 /* FIXME: Can we still go on? */
2094 c_variable_editable (struct varobj
*var
)
2096 switch (TYPE_CODE (get_type (var
)))
2098 case TYPE_CODE_STRUCT
:
2099 case TYPE_CODE_UNION
:
2100 case TYPE_CODE_ARRAY
:
2101 case TYPE_CODE_FUNC
:
2102 case TYPE_CODE_MEMBER
:
2103 case TYPE_CODE_METHOD
:
2114 c_value_of_variable (struct varobj
*var
)
2116 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2117 it will print out its children instead of "{...}". So we need to
2118 catch that case explicitly. */
2119 struct type
*type
= get_type (var
);
2121 /* Strip top-level references. */
2122 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
2123 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2125 switch (TYPE_CODE (type
))
2127 case TYPE_CODE_STRUCT
:
2128 case TYPE_CODE_UNION
:
2129 return xstrdup ("{...}");
2132 case TYPE_CODE_ARRAY
:
2135 number
= xstrprintf ("[%d]", var
->num_children
);
2142 if (var
->value
== NULL
)
2144 /* This can happen if we attempt to get the value of a struct
2145 member when the parent is an invalid pointer. This is an
2146 error condition, so we should tell the caller. */
2152 struct ui_file
*stb
= mem_fileopen ();
2153 struct cleanup
*old_chain
= make_cleanup_ui_file_delete (stb
);
2156 gdb_assert (type_changeable (var
));
2157 gdb_assert (!value_lazy (var
->value
));
2158 common_val_print (var
->value
, stb
,
2159 format_code
[(int) var
->format
], 1, 0, 0);
2160 thevalue
= ui_file_xstrdup (stb
, &dummy
);
2161 do_cleanups (old_chain
);
2172 cplus_number_of_children (struct varobj
*var
)
2175 int children
, dont_know
;
2180 if (!CPLUS_FAKE_CHILD (var
))
2182 type
= get_type_deref (var
);
2184 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2185 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2189 cplus_class_num_children (type
, kids
);
2190 if (kids
[v_public
] != 0)
2192 if (kids
[v_private
] != 0)
2194 if (kids
[v_protected
] != 0)
2197 /* Add any baseclasses */
2198 children
+= TYPE_N_BASECLASSES (type
);
2201 /* FIXME: save children in var */
2208 type
= get_type_deref (var
->parent
);
2210 cplus_class_num_children (type
, kids
);
2211 if (strcmp (var
->name
, "public") == 0)
2212 children
= kids
[v_public
];
2213 else if (strcmp (var
->name
, "private") == 0)
2214 children
= kids
[v_private
];
2216 children
= kids
[v_protected
];
2221 children
= c_number_of_children (var
);
2226 /* Compute # of public, private, and protected variables in this class.
2227 That means we need to descend into all baseclasses and find out
2228 how many are there, too. */
2230 cplus_class_num_children (struct type
*type
, int children
[3])
2234 children
[v_public
] = 0;
2235 children
[v_private
] = 0;
2236 children
[v_protected
] = 0;
2238 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
2240 /* If we have a virtual table pointer, omit it. */
2241 if (TYPE_VPTR_BASETYPE (type
) == type
&& TYPE_VPTR_FIELDNO (type
) == i
)
2244 if (TYPE_FIELD_PROTECTED (type
, i
))
2245 children
[v_protected
]++;
2246 else if (TYPE_FIELD_PRIVATE (type
, i
))
2247 children
[v_private
]++;
2249 children
[v_public
]++;
2254 cplus_name_of_variable (struct varobj
*parent
)
2256 return c_name_of_variable (parent
);
2260 cplus_name_of_child (struct varobj
*parent
, int index
)
2265 if (CPLUS_FAKE_CHILD (parent
))
2267 /* Looking for children of public, private, or protected. */
2268 type
= get_type_deref (parent
->parent
);
2271 type
= get_type_deref (parent
);
2274 switch (TYPE_CODE (type
))
2276 case TYPE_CODE_STRUCT
:
2277 case TYPE_CODE_UNION
:
2278 if (CPLUS_FAKE_CHILD (parent
))
2280 /* The fields of the class type are ordered as they
2281 appear in the class. We are given an index for a
2282 particular access control type ("public","protected",
2283 or "private"). We must skip over fields that don't
2284 have the access control we are looking for to properly
2285 find the indexed field. */
2286 int type_index
= TYPE_N_BASECLASSES (type
);
2287 if (strcmp (parent
->name
, "private") == 0)
2291 if (TYPE_VPTR_BASETYPE (type
) == type
2292 && type_index
== TYPE_VPTR_FIELDNO (type
))
2294 else if (TYPE_FIELD_PRIVATE (type
, type_index
))
2300 else if (strcmp (parent
->name
, "protected") == 0)
2304 if (TYPE_VPTR_BASETYPE (type
) == type
2305 && type_index
== TYPE_VPTR_FIELDNO (type
))
2307 else if (TYPE_FIELD_PROTECTED (type
, type_index
))
2317 if (TYPE_VPTR_BASETYPE (type
) == type
2318 && type_index
== TYPE_VPTR_FIELDNO (type
))
2320 else if (!TYPE_FIELD_PRIVATE (type
, type_index
) &&
2321 !TYPE_FIELD_PROTECTED (type
, type_index
))
2328 name
= TYPE_FIELD_NAME (type
, type_index
);
2330 else if (index
< TYPE_N_BASECLASSES (type
))
2331 /* We are looking up the name of a base class */
2332 name
= TYPE_FIELD_NAME (type
, index
);
2336 cplus_class_num_children(type
, children
);
2338 /* Everything beyond the baseclasses can
2339 only be "public", "private", or "protected"
2341 The special "fake" children are always output by varobj in
2342 this order. So if INDEX == 2, it MUST be "protected". */
2343 index
-= TYPE_N_BASECLASSES (type
);
2347 if (children
[v_public
] > 0)
2349 else if (children
[v_private
] > 0)
2355 if (children
[v_public
] > 0)
2357 if (children
[v_private
] > 0)
2362 else if (children
[v_private
] > 0)
2366 /* Must be protected */
2381 return c_name_of_child (parent
, index
);
2385 name
= savestring (name
, strlen (name
));
2391 static struct value
*
2392 cplus_value_of_root (struct varobj
**var_handle
)
2394 return c_value_of_root (var_handle
);
2397 static struct value
*
2398 cplus_value_of_child (struct varobj
*parent
, int index
)
2401 struct value
*value
;
2403 if (CPLUS_FAKE_CHILD (parent
))
2404 type
= get_type_deref (parent
->parent
);
2406 type
= get_type_deref (parent
);
2410 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2411 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2413 if (CPLUS_FAKE_CHILD (parent
))
2416 struct value
*temp
= parent
->parent
->value
;
2421 name
= name_of_child (parent
, index
);
2422 gdb_value_struct_elt (NULL
, &value
, &temp
, NULL
, name
, NULL
,
2425 release_value (value
);
2429 else if (index
>= TYPE_N_BASECLASSES (type
))
2431 /* public, private, or protected */
2437 if (parent
->value
!= NULL
)
2439 struct value
*temp
= NULL
;
2441 /* No special processing for references is needed --
2442 value_cast below handles references. */
2443 if (TYPE_CODE (value_type (parent
->value
)) == TYPE_CODE_PTR
)
2445 if (!gdb_value_ind (parent
->value
, &temp
))
2449 temp
= parent
->value
;
2453 value
= value_cast (TYPE_FIELD_TYPE (type
, index
), temp
);
2454 release_value (value
);
2458 /* We failed to evaluate the parent's value, so don't even
2459 bother trying to evaluate this child. */
2467 return c_value_of_child (parent
, index
);
2472 static struct type
*
2473 cplus_type_of_child (struct varobj
*parent
, int index
)
2475 struct type
*type
, *t
;
2477 if (CPLUS_FAKE_CHILD (parent
))
2479 /* Looking for the type of a child of public, private, or protected. */
2480 t
= get_type_deref (parent
->parent
);
2483 t
= get_type_deref (parent
);
2486 switch (TYPE_CODE (t
))
2488 case TYPE_CODE_STRUCT
:
2489 case TYPE_CODE_UNION
:
2490 if (CPLUS_FAKE_CHILD (parent
))
2492 char *name
= cplus_name_of_child (parent
, index
);
2493 type
= lookup_struct_elt_type (t
, name
, 0);
2496 else if (index
< TYPE_N_BASECLASSES (t
))
2497 type
= TYPE_FIELD_TYPE (t
, index
);
2510 return c_type_of_child (parent
, index
);
2516 cplus_variable_editable (struct varobj
*var
)
2518 if (CPLUS_FAKE_CHILD (var
))
2521 return c_variable_editable (var
);
2525 cplus_value_of_variable (struct varobj
*var
)
2528 /* If we have one of our special types, don't print out
2530 if (CPLUS_FAKE_CHILD (var
))
2531 return xstrdup ("");
2533 return c_value_of_variable (var
);
2539 java_number_of_children (struct varobj
*var
)
2541 return cplus_number_of_children (var
);
2545 java_name_of_variable (struct varobj
*parent
)
2549 name
= cplus_name_of_variable (parent
);
2550 /* If the name has "-" in it, it is because we
2551 needed to escape periods in the name... */
2554 while (*p
!= '\000')
2565 java_name_of_child (struct varobj
*parent
, int index
)
2569 name
= cplus_name_of_child (parent
, index
);
2570 /* Escape any periods in the name... */
2573 while (*p
!= '\000')
2583 static struct value
*
2584 java_value_of_root (struct varobj
**var_handle
)
2586 return cplus_value_of_root (var_handle
);
2589 static struct value
*
2590 java_value_of_child (struct varobj
*parent
, int index
)
2592 return cplus_value_of_child (parent
, index
);
2595 static struct type
*
2596 java_type_of_child (struct varobj
*parent
, int index
)
2598 return cplus_type_of_child (parent
, index
);
2602 java_variable_editable (struct varobj
*var
)
2604 return cplus_variable_editable (var
);
2608 java_value_of_variable (struct varobj
*var
)
2610 return cplus_value_of_variable (var
);
2613 extern void _initialize_varobj (void);
2615 _initialize_varobj (void)
2617 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
2619 varobj_table
= xmalloc (sizeof_table
);
2620 memset (varobj_table
, 0, sizeof_table
);
2622 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance
,
2624 Set varobj debugging."), _("\
2625 Show varobj debugging."), _("\
2626 When non-zero, varobj debugging is enabled."),
2629 &setlist
, &showlist
);