1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
22 #include "exceptions.h"
24 #include "expression.h"
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
37 /* Non-zero if we want to see trace of varobj level stuff. */
41 show_varobjdebug (struct ui_file
*file
, int from_tty
,
42 struct cmd_list_element
*c
, const char *value
)
44 fprintf_filtered (file
, _("Varobj debugging is %s.\n"), value
);
47 /* String representations of gdb's format codes */
48 char *varobj_format_string
[] =
49 { "natural", "binary", "decimal", "hexadecimal", "octal" };
51 /* String representations of gdb's known languages */
52 char *varobj_language_string
[] = { "unknown", "C", "C++", "Java" };
56 /* Every root variable has one of these structures saved in its
57 varobj. Members which must be free'd are noted. */
61 /* Alloc'd expression for this parent. */
62 struct expression
*exp
;
64 /* Block for which this expression is valid */
65 struct block
*valid_block
;
67 /* The frame for this expression */
68 struct frame_id frame
;
70 /* If 1, "update" always recomputes the frame & valid block
71 using the currently selected frame. */
72 int use_selected_frame
;
74 /* Flag that indicates validity: set to 0 when this varobj_root refers
75 to symbols that do not exist anymore. */
78 /* Language info for this variable and its children */
79 struct language_specific
*lang
;
81 /* The varobj for this root node. */
82 struct varobj
*rootvar
;
84 /* Next root variable */
85 struct varobj_root
*next
;
88 typedef struct varobj
*varobj_p
;
92 /* Every variable in the system has a structure of this type defined
93 for it. This structure holds all information necessary to manipulate
94 a particular object variable. Members which must be freed are noted. */
98 /* Alloc'd name of the variable for this object.. If this variable is a
99 child, then this name will be the child's source name.
100 (bar, not foo.bar) */
101 /* NOTE: This is the "expression" */
104 /* The alloc'd name for this variable's object. This is here for
105 convenience when constructing this object's children. */
108 /* Index of this variable in its parent or -1 */
111 /* The type of this variable. This may NEVER be NULL. */
114 /* The value of this expression or subexpression. A NULL value
115 indicates there was an error getting this value.
116 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
117 the value is either NULL, or not lazy. */
120 /* The number of (immediate) children this variable has */
123 /* If this object is a child, this points to its immediate parent. */
124 struct varobj
*parent
;
126 /* Children of this object. */
127 VEC (varobj_p
) *children
;
129 /* Description of the root variable. Points to root variable for children. */
130 struct varobj_root
*root
;
132 /* The format of the output for this object */
133 enum varobj_display_formats format
;
135 /* Was this variable updated via a varobj_set_value operation */
138 /* Last print value. */
145 struct cpstack
*next
;
148 /* A list of varobjs */
156 /* Private function prototypes */
158 /* Helper functions for the above subcommands. */
160 static int delete_variable (struct cpstack
**, struct varobj
*, int);
162 static void delete_variable_1 (struct cpstack
**, int *,
163 struct varobj
*, int, int);
165 static int install_variable (struct varobj
*);
167 static void uninstall_variable (struct varobj
*);
169 static struct varobj
*create_child (struct varobj
*, int, char *);
171 /* Utility routines */
173 static struct varobj
*new_variable (void);
175 static struct varobj
*new_root_variable (void);
177 static void free_variable (struct varobj
*var
);
179 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
181 static struct type
*get_type (struct varobj
*var
);
183 static struct type
*get_value_type (struct varobj
*var
);
185 static struct type
*get_target_type (struct type
*);
187 static enum varobj_display_formats
variable_default_display (struct varobj
*);
189 static void cppush (struct cpstack
**pstack
, char *name
);
191 static char *cppop (struct cpstack
**pstack
);
193 static int install_new_value (struct varobj
*var
, struct value
*value
,
196 /* Language-specific routines. */
198 static enum varobj_languages
variable_language (struct varobj
*var
);
200 static int number_of_children (struct varobj
*);
202 static char *name_of_variable (struct varobj
*);
204 static char *name_of_child (struct varobj
*, int);
206 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
208 static struct value
*value_of_child (struct varobj
*parent
, int index
);
210 static int variable_editable (struct varobj
*var
);
212 static char *my_value_of_variable (struct varobj
*var
);
214 static char *value_get_print_value (struct value
*value
,
215 enum varobj_display_formats format
);
217 static int varobj_value_is_changeable_p (struct varobj
*var
);
219 static int is_root_p (struct varobj
*var
);
221 /* C implementation */
223 static int c_number_of_children (struct varobj
*var
);
225 static char *c_name_of_variable (struct varobj
*parent
);
227 static char *c_name_of_child (struct varobj
*parent
, int index
);
229 static struct value
*c_value_of_root (struct varobj
**var_handle
);
231 static struct value
*c_value_of_child (struct varobj
*parent
, int index
);
233 static struct type
*c_type_of_child (struct varobj
*parent
, int index
);
235 static int c_variable_editable (struct varobj
*var
);
237 static char *c_value_of_variable (struct varobj
*var
);
239 /* C++ implementation */
241 static int cplus_number_of_children (struct varobj
*var
);
243 static void cplus_class_num_children (struct type
*type
, int children
[3]);
245 static char *cplus_name_of_variable (struct varobj
*parent
);
247 static char *cplus_name_of_child (struct varobj
*parent
, int index
);
249 static struct value
*cplus_value_of_root (struct varobj
**var_handle
);
251 static struct value
*cplus_value_of_child (struct varobj
*parent
, int index
);
253 static struct type
*cplus_type_of_child (struct varobj
*parent
, int index
);
255 static int cplus_variable_editable (struct varobj
*var
);
257 static char *cplus_value_of_variable (struct varobj
*var
);
259 /* Java implementation */
261 static int java_number_of_children (struct varobj
*var
);
263 static char *java_name_of_variable (struct varobj
*parent
);
265 static char *java_name_of_child (struct varobj
*parent
, int index
);
267 static struct value
*java_value_of_root (struct varobj
**var_handle
);
269 static struct value
*java_value_of_child (struct varobj
*parent
, int index
);
271 static struct type
*java_type_of_child (struct varobj
*parent
, int index
);
273 static int java_variable_editable (struct varobj
*var
);
275 static char *java_value_of_variable (struct varobj
*var
);
277 /* The language specific vector */
279 struct language_specific
282 /* The language of this variable */
283 enum varobj_languages language
;
285 /* The number of children of PARENT. */
286 int (*number_of_children
) (struct varobj
* parent
);
288 /* The name (expression) of a root varobj. */
289 char *(*name_of_variable
) (struct varobj
* parent
);
291 /* The name of the INDEX'th child of PARENT. */
292 char *(*name_of_child
) (struct varobj
* parent
, int index
);
294 /* The ``struct value *'' of the root variable ROOT. */
295 struct value
*(*value_of_root
) (struct varobj
** root_handle
);
297 /* The ``struct value *'' of the INDEX'th child of PARENT. */
298 struct value
*(*value_of_child
) (struct varobj
* parent
, int index
);
300 /* The type of the INDEX'th child of PARENT. */
301 struct type
*(*type_of_child
) (struct varobj
* parent
, int index
);
303 /* Is VAR editable? */
304 int (*variable_editable
) (struct varobj
* var
);
306 /* The current value of VAR. */
307 char *(*value_of_variable
) (struct varobj
* var
);
310 /* Array of known source language routines. */
311 static struct language_specific languages
[vlang_end
] = {
312 /* Unknown (try treating as C */
315 c_number_of_children
,
327 c_number_of_children
,
339 cplus_number_of_children
,
340 cplus_name_of_variable
,
343 cplus_value_of_child
,
345 cplus_variable_editable
,
346 cplus_value_of_variable
}
351 java_number_of_children
,
352 java_name_of_variable
,
357 java_variable_editable
,
358 java_value_of_variable
}
361 /* A little convenience enum for dealing with C++/Java */
364 v_public
= 0, v_private
, v_protected
369 /* Mappings of varobj_display_formats enums to gdb's format codes */
370 static int format_code
[] = { 0, 't', 'd', 'x', 'o' };
372 /* Header of the list of root variable objects */
373 static struct varobj_root
*rootlist
;
374 static int rootcount
= 0; /* number of root varobjs in the list */
376 /* Prime number indicating the number of buckets in the hash table */
377 /* A prime large enough to avoid too many colisions */
378 #define VAROBJ_TABLE_SIZE 227
380 /* Pointer to the varobj hash table (built at run time) */
381 static struct vlist
**varobj_table
;
383 /* Is the variable X one of our "fake" children? */
384 #define CPLUS_FAKE_CHILD(x) \
385 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
388 /* API Implementation */
390 is_root_p (struct varobj
*var
)
392 return (var
->root
->rootvar
== var
);
395 /* Creates a varobj (not its children) */
397 /* Return the full FRAME which corresponds to the given CORE_ADDR
398 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
400 static struct frame_info
*
401 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
403 struct frame_info
*frame
= NULL
;
405 if (frame_addr
== (CORE_ADDR
) 0)
410 frame
= get_prev_frame (frame
);
413 if (get_frame_base_address (frame
) == frame_addr
)
419 varobj_create (char *objname
,
420 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
423 struct frame_info
*fi
;
424 struct frame_info
*old_fi
= NULL
;
426 struct cleanup
*old_chain
;
428 /* Fill out a varobj structure for the (root) variable being constructed. */
429 var
= new_root_variable ();
430 old_chain
= make_cleanup_free_variable (var
);
432 if (expression
!= NULL
)
435 enum varobj_languages lang
;
438 /* Parse and evaluate the expression, filling in as much
439 of the variable's data as possible */
441 /* Allow creator to specify context of variable */
442 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
443 fi
= deprecated_safe_get_selected_frame ();
445 /* FIXME: cagney/2002-11-23: This code should be doing a
446 lookup using the frame ID and not just the frame's
447 ``address''. This, of course, means an interface change.
448 However, with out that interface change ISAs, such as the
449 ia64 with its two stacks, won't work. Similar goes for the
450 case where there is a frameless function. */
451 fi
= find_frame_addr_in_frame_chain (frame
);
453 /* frame = -2 means always use selected frame */
454 if (type
== USE_SELECTED_FRAME
)
455 var
->root
->use_selected_frame
= 1;
459 block
= get_frame_block (fi
, 0);
462 innermost_block
= NULL
;
463 /* Wrap the call to parse expression, so we can
464 return a sensible error. */
465 if (!gdb_parse_exp_1 (&p
, block
, 0, &var
->root
->exp
))
470 /* Don't allow variables to be created for types. */
471 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
)
473 do_cleanups (old_chain
);
474 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
475 " as an expression.\n");
479 var
->format
= variable_default_display (var
);
480 var
->root
->valid_block
= innermost_block
;
481 var
->name
= savestring (expression
, strlen (expression
));
483 /* When the frame is different from the current frame,
484 we must select the appropriate frame before parsing
485 the expression, otherwise the value will not be current.
486 Since select_frame is so benign, just call it for all cases. */
489 var
->root
->frame
= get_frame_id (fi
);
490 old_fi
= get_selected_frame (NULL
);
494 /* We definitively need to catch errors here.
495 If evaluate_expression succeeds we got the value we wanted.
496 But if it fails, we still go on with a call to evaluate_type() */
497 if (!gdb_evaluate_expression (var
->root
->exp
, &value
))
498 /* Error getting the value. Try to at least get the
500 value
= evaluate_type (var
->root
->exp
);
502 var
->type
= value_type (value
);
503 install_new_value (var
, value
, 1 /* Initial assignment */);
505 /* Set language info */
506 lang
= variable_language (var
);
507 var
->root
->lang
= &languages
[lang
];
509 /* Set ourselves as our root */
510 var
->root
->rootvar
= var
;
512 /* Reset the selected frame */
514 select_frame (old_fi
);
517 /* If the variable object name is null, that means this
518 is a temporary variable, so don't install it. */
520 if ((var
!= NULL
) && (objname
!= NULL
))
522 var
->obj_name
= savestring (objname
, strlen (objname
));
524 /* If a varobj name is duplicated, the install will fail so
526 if (!install_variable (var
))
528 do_cleanups (old_chain
);
533 discard_cleanups (old_chain
);
537 /* Generates an unique name that can be used for a varobj */
540 varobj_gen_name (void)
545 /* generate a name for this object */
547 obj_name
= xstrprintf ("var%d", id
);
552 /* Given an "objname", returns the pointer to the corresponding varobj
553 or NULL if not found */
556 varobj_get_handle (char *objname
)
560 unsigned int index
= 0;
563 for (chp
= objname
; *chp
; chp
++)
565 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
568 cv
= *(varobj_table
+ index
);
569 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
573 error (_("Variable object not found"));
578 /* Given the handle, return the name of the object */
581 varobj_get_objname (struct varobj
*var
)
583 return var
->obj_name
;
586 /* Given the handle, return the expression represented by the object */
589 varobj_get_expression (struct varobj
*var
)
591 return name_of_variable (var
);
594 /* Deletes a varobj and all its children if only_children == 0,
595 otherwise deletes only the children; returns a malloc'ed list of all the
596 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
599 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
603 struct cpstack
*result
= NULL
;
606 /* Initialize a stack for temporary results */
607 cppush (&result
, NULL
);
610 /* Delete only the variable children */
611 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
613 /* Delete the variable and all its children */
614 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
616 /* We may have been asked to return a list of what has been deleted */
619 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
623 *cp
= cppop (&result
);
624 while ((*cp
!= NULL
) && (mycount
> 0))
628 *cp
= cppop (&result
);
631 if (mycount
|| (*cp
!= NULL
))
632 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
639 /* Set/Get variable object display format */
641 enum varobj_display_formats
642 varobj_set_display_format (struct varobj
*var
,
643 enum varobj_display_formats format
)
650 case FORMAT_HEXADECIMAL
:
652 var
->format
= format
;
656 var
->format
= variable_default_display (var
);
662 enum varobj_display_formats
663 varobj_get_display_format (struct varobj
*var
)
669 varobj_get_num_children (struct varobj
*var
)
671 if (var
->num_children
== -1)
672 var
->num_children
= number_of_children (var
);
674 return var
->num_children
;
677 /* Creates a list of the immediate children of a variable object;
678 the return code is the number of such children or -1 on error */
681 varobj_list_children (struct varobj
*var
, struct varobj
***childlist
)
683 struct varobj
*child
;
687 /* sanity check: have we been passed a pointer? */
688 if (childlist
== NULL
)
693 if (var
->num_children
== -1)
694 var
->num_children
= number_of_children (var
);
696 /* If that failed, give up. */
697 if (var
->num_children
== -1)
700 /* If we're called when the list of children is not yet initialized,
701 allocate enough elements in it. */
702 while (VEC_length (varobj_p
, var
->children
) < var
->num_children
)
703 VEC_safe_push (varobj_p
, var
->children
, NULL
);
705 /* List of children */
706 *childlist
= xmalloc ((var
->num_children
+ 1) * sizeof (struct varobj
*));
708 for (i
= 0; i
< var
->num_children
; i
++)
712 /* Mark as the end in case we bail out */
713 *((*childlist
) + i
) = NULL
;
715 existing
= VEC_index (varobj_p
, var
->children
, i
);
717 if (existing
== NULL
)
719 /* Either it's the first call to varobj_list_children for
720 this variable object, and the child was never created,
721 or it was explicitly deleted by the client. */
722 name
= name_of_child (var
, i
);
723 existing
= create_child (var
, i
, name
);
724 VEC_replace (varobj_p
, var
->children
, i
, existing
);
727 *((*childlist
) + i
) = existing
;
730 /* End of list is marked by a NULL pointer */
731 *((*childlist
) + i
) = NULL
;
733 return var
->num_children
;
736 /* Obtain the type of an object Variable as a string similar to the one gdb
737 prints on the console */
740 varobj_get_type (struct varobj
*var
)
743 struct cleanup
*old_chain
;
748 /* For the "fake" variables, do not return a type. (It's type is
750 Do not return a type for invalid variables as well. */
751 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
754 stb
= mem_fileopen ();
755 old_chain
= make_cleanup_ui_file_delete (stb
);
757 /* To print the type, we simply create a zero ``struct value *'' and
758 cast it to our type. We then typeprint this variable. */
759 val
= value_zero (var
->type
, not_lval
);
760 type_print (value_type (val
), "", stb
, -1);
762 thetype
= ui_file_xstrdup (stb
, &length
);
763 do_cleanups (old_chain
);
767 /* Obtain the type of an object variable. */
770 varobj_get_gdb_type (struct varobj
*var
)
775 enum varobj_languages
776 varobj_get_language (struct varobj
*var
)
778 return variable_language (var
);
782 varobj_get_attributes (struct varobj
*var
)
786 if (var
->root
->is_valid
&& variable_editable (var
))
787 /* FIXME: define masks for attributes */
788 attributes
|= 0x00000001; /* Editable */
794 varobj_get_value (struct varobj
*var
)
796 return my_value_of_variable (var
);
799 /* Set the value of an object variable (if it is editable) to the
800 value of the given expression */
801 /* Note: Invokes functions that can call error() */
804 varobj_set_value (struct varobj
*var
, char *expression
)
810 /* The argument "expression" contains the variable's new value.
811 We need to first construct a legal expression for this -- ugh! */
812 /* Does this cover all the bases? */
813 struct expression
*exp
;
815 int saved_input_radix
= input_radix
;
817 if (var
->value
!= NULL
&& variable_editable (var
))
819 char *s
= expression
;
822 input_radix
= 10; /* ALWAYS reset to decimal temporarily */
823 exp
= parse_exp_1 (&s
, 0, 0);
824 if (!gdb_evaluate_expression (exp
, &value
))
826 /* We cannot proceed without a valid expression. */
831 /* All types that are editable must also be changeable. */
832 gdb_assert (varobj_value_is_changeable_p (var
));
834 /* The value of a changeable variable object must not be lazy. */
835 gdb_assert (!value_lazy (var
->value
));
837 /* Need to coerce the input. We want to check if the
838 value of the variable object will be different
839 after assignment, and the first thing value_assign
840 does is coerce the input.
841 For example, if we are assigning an array to a pointer variable we
842 should compare the pointer with the the array's address, not with the
844 value
= coerce_array (value
);
846 /* The new value may be lazy. gdb_value_assign, or
847 rather value_contents, will take care of this.
848 If fetching of the new value will fail, gdb_value_assign
849 with catch the exception. */
850 if (!gdb_value_assign (var
->value
, value
, &val
))
853 /* If the value has changed, record it, so that next -var-update can
854 report this change. If a variable had a value of '1', we've set it
855 to '333' and then set again to '1', when -var-update will report this
856 variable as changed -- because the first assignment has set the
857 'updated' flag. There's no need to optimize that, because return value
858 of -var-update should be considered an approximation. */
859 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
860 input_radix
= saved_input_radix
;
867 /* Returns a malloc'ed list with all root variable objects */
869 varobj_list (struct varobj
***varlist
)
872 struct varobj_root
*croot
;
873 int mycount
= rootcount
;
875 /* Alloc (rootcount + 1) entries for the result */
876 *varlist
= xmalloc ((rootcount
+ 1) * sizeof (struct varobj
*));
880 while ((croot
!= NULL
) && (mycount
> 0))
882 *cv
= croot
->rootvar
;
887 /* Mark the end of the list */
890 if (mycount
|| (croot
!= NULL
))
892 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
898 /* Assign a new value to a variable object. If INITIAL is non-zero,
899 this is the first assignement after the variable object was just
900 created, or changed type. In that case, just assign the value
902 Otherwise, assign the value and if type_changeable returns non-zero,
903 find if the new value is different from the current value.
904 Return 1 if so, and 0 if the values are equal.
906 The VALUE parameter should not be released -- the function will
907 take care of releasing it when needed. */
909 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
915 /* We need to know the varobj's type to decide if the value should
916 be fetched or not. C++ fake children (public/protected/private) don't have
918 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
919 changeable
= varobj_value_is_changeable_p (var
);
920 need_to_fetch
= changeable
;
922 /* We are not interested in the address of references, and given
923 that in C++ a reference is not rebindable, it cannot
924 meaningfully change. So, get hold of the real value. */
927 value
= coerce_ref (value
);
928 release_value (value
);
931 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
932 /* For unions, we need to fetch the value implicitly because
933 of implementation of union member fetch. When gdb
934 creates a value for a field and the value of the enclosing
935 structure is not lazy, it immediately copies the necessary
936 bytes from the enclosing values. If the enclosing value is
937 lazy, the call to value_fetch_lazy on the field will read
938 the data from memory. For unions, that means we'll read the
939 same memory more than once, which is not desirable. So
943 /* The new value might be lazy. If the type is changeable,
944 that is we'll be comparing values of this type, fetch the
945 value now. Otherwise, on the next update the old value
946 will be lazy, which means we've lost that old value. */
947 if (need_to_fetch
&& value
&& value_lazy (value
))
949 if (!gdb_value_fetch_lazy (value
))
951 /* Set the value to NULL, so that for the next -var-update,
952 we don't try to compare the new value with this value,
953 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
)
962 var
->print_value
= value_get_print_value (value
, var
->format
);
965 /* If the value of the varobj was changed by -var-set-value, then the
966 value in the varobj and in the target is the same. However, that value
967 is different from the value that the varobj had after the previous
968 -var-update. So need to the varobj as changed. */
971 xfree (var
->print_value
);
972 var
->print_value
= value_get_print_value (value
, var
->format
);
977 /* Try to compare the values. That requires that both
978 values are non-lazy. */
980 /* Quick comparison of NULL values. */
981 if (var
->value
== NULL
&& value
== NULL
)
984 else if (var
->value
== NULL
|| value
== NULL
)
986 xfree (var
->print_value
);
987 var
->print_value
= value_get_print_value (value
, var
->format
);
993 gdb_assert (!value_lazy (var
->value
));
994 gdb_assert (!value_lazy (value
));
995 print_value
= value_get_print_value (value
, var
->format
);
997 gdb_assert (var
->print_value
!= NULL
&& print_value
!= NULL
);
998 if (strcmp (var
->print_value
, print_value
) != 0)
1000 xfree (var
->print_value
);
1001 var
->print_value
= print_value
;
1005 xfree (print_value
);
1010 /* We must always keep the new value, since children depend on it. */
1011 if (var
->value
!= NULL
)
1012 value_free (var
->value
);
1016 gdb_assert (!var
->value
|| value_type (var
->value
));
1021 /* Update the values for a variable and its children. This is a
1022 two-pronged attack. First, re-parse the value for the root's
1023 expression to see if it's changed. Then go all the way
1024 through its children, reconstructing them and noting if they've
1027 < 0 for error values, see varobj.h.
1028 Otherwise it is the number of children + parent changed.
1030 Only root variables can be updated...
1032 NOTE: This function may delete the caller's varobj. If it
1033 returns TYPE_CHANGED, then it has done this and VARP will be modified
1034 to point to the new varobj. */
1037 varobj_update (struct varobj
**varp
, struct varobj
***changelist
)
1045 struct varobj
**templist
= NULL
;
1047 VEC (varobj_p
) *stack
= NULL
;
1048 VEC (varobj_p
) *result
= NULL
;
1049 struct frame_id old_fid
;
1050 struct frame_info
*fi
;
1052 /* sanity check: have we been passed a pointer? */
1053 gdb_assert (changelist
);
1055 if (!is_root_p (*varp
))
1056 error (_("Only root variables can be updated"));
1058 if (!(*varp
)->root
->is_valid
)
1061 /* Save the selected stack frame, since we will need to change it
1062 in order to evaluate expressions. */
1063 old_fid
= get_frame_id (deprecated_safe_get_selected_frame ());
1065 /* Update the root variable. value_of_root can return NULL
1066 if the variable is no longer around, i.e. we stepped out of
1067 the frame in which a local existed. We are letting the
1068 value_of_root variable dispose of the varobj if the type
1071 new = value_of_root (varp
, &type_changed
);
1073 /* Restore selected frame. */
1074 fi
= frame_find_by_id (old_fid
);
1078 /* If this is a "use_selected_frame" varobj, and its type has changed,
1079 them note that it's changed. */
1081 VEC_safe_push (varobj_p
, result
, *varp
);
1083 if (install_new_value ((*varp
), new, type_changed
))
1085 /* If type_changed is 1, install_new_value will never return
1086 non-zero, so we'll never report the same variable twice. */
1087 gdb_assert (!type_changed
);
1088 VEC_safe_push (varobj_p
, result
, *varp
);
1093 /* This means the varobj itself is out of scope.
1095 VEC_free (varobj_p
, result
);
1096 return NOT_IN_SCOPE
;
1099 VEC_safe_push (varobj_p
, stack
, *varp
);
1101 /* Walk through the children, reconstructing them all. */
1102 while (!VEC_empty (varobj_p
, stack
))
1104 v
= VEC_pop (varobj_p
, stack
);
1106 /* Push any children. Use reverse order so that the first
1107 child is popped from the work stack first, and so
1108 will be added to result first. This does not
1109 affect correctness, just "nicer". */
1110 for (i
= VEC_length (varobj_p
, v
->children
)-1; i
>= 0; --i
)
1112 varobj_p c
= VEC_index (varobj_p
, v
->children
, i
);
1113 /* Child may be NULL if explicitly deleted by -var-delete. */
1115 VEC_safe_push (varobj_p
, stack
, c
);
1118 /* Update this variable, unless it's a root, which is already
1122 new = value_of_child (v
->parent
, v
->index
);
1123 if (install_new_value (v
, new, 0 /* type not changed */))
1125 /* Note that it's changed */
1126 VEC_safe_push (varobj_p
, result
, v
);
1132 /* Alloc (changed + 1) list entries. */
1133 changed
= VEC_length (varobj_p
, result
);
1134 *changelist
= xmalloc ((changed
+ 1) * sizeof (struct varobj
*));
1137 for (i
= 0; i
< changed
; ++i
)
1139 *cv
= VEC_index (varobj_p
, result
, i
);
1140 gdb_assert (*cv
!= NULL
);
1145 VEC_free (varobj_p
, stack
);
1146 VEC_free (varobj_p
, result
);
1149 return TYPE_CHANGED
;
1155 /* Helper functions */
1158 * Variable object construction/destruction
1162 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
1163 int only_children_p
)
1167 delete_variable_1 (resultp
, &delcount
, var
,
1168 only_children_p
, 1 /* remove_from_parent_p */ );
1173 /* Delete the variable object VAR and its children */
1174 /* IMPORTANT NOTE: If we delete a variable which is a child
1175 and the parent is not removed we dump core. It must be always
1176 initially called with remove_from_parent_p set */
1178 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
1179 struct varobj
*var
, int only_children_p
,
1180 int remove_from_parent_p
)
1184 /* Delete any children of this variable, too. */
1185 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
1187 varobj_p child
= VEC_index (varobj_p
, var
->children
, i
);
1188 if (!remove_from_parent_p
)
1189 child
->parent
= NULL
;
1190 delete_variable_1 (resultp
, delcountp
, child
, 0, only_children_p
);
1192 VEC_free (varobj_p
, var
->children
);
1194 /* if we were called to delete only the children we are done here */
1195 if (only_children_p
)
1198 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1199 /* If the name is null, this is a temporary variable, that has not
1200 yet been installed, don't report it, it belongs to the caller... */
1201 if (var
->obj_name
!= NULL
)
1203 cppush (resultp
, xstrdup (var
->obj_name
));
1204 *delcountp
= *delcountp
+ 1;
1207 /* If this variable has a parent, remove it from its parent's list */
1208 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1209 (as indicated by remove_from_parent_p) we don't bother doing an
1210 expensive list search to find the element to remove when we are
1211 discarding the list afterwards */
1212 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1214 VEC_replace (varobj_p
, var
->parent
->children
, var
->index
, NULL
);
1217 if (var
->obj_name
!= NULL
)
1218 uninstall_variable (var
);
1220 /* Free memory associated with this variable */
1221 free_variable (var
);
1224 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1226 install_variable (struct varobj
*var
)
1229 struct vlist
*newvl
;
1231 unsigned int index
= 0;
1234 for (chp
= var
->obj_name
; *chp
; chp
++)
1236 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1239 cv
= *(varobj_table
+ index
);
1240 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1244 error (_("Duplicate variable object name"));
1246 /* Add varobj to hash table */
1247 newvl
= xmalloc (sizeof (struct vlist
));
1248 newvl
->next
= *(varobj_table
+ index
);
1250 *(varobj_table
+ index
) = newvl
;
1252 /* If root, add varobj to root list */
1253 if (is_root_p (var
))
1255 /* Add to list of root variables */
1256 if (rootlist
== NULL
)
1257 var
->root
->next
= NULL
;
1259 var
->root
->next
= rootlist
;
1260 rootlist
= var
->root
;
1267 /* Unistall the object VAR. */
1269 uninstall_variable (struct varobj
*var
)
1273 struct varobj_root
*cr
;
1274 struct varobj_root
*prer
;
1276 unsigned int index
= 0;
1279 /* Remove varobj from hash table */
1280 for (chp
= var
->obj_name
; *chp
; chp
++)
1282 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1285 cv
= *(varobj_table
+ index
);
1287 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1294 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
1299 ("Assertion failed: Could not find variable object \"%s\" to delete",
1305 *(varobj_table
+ index
) = cv
->next
;
1307 prev
->next
= cv
->next
;
1311 /* If root, remove varobj from root list */
1312 if (is_root_p (var
))
1314 /* Remove from list of root variables */
1315 if (rootlist
== var
->root
)
1316 rootlist
= var
->root
->next
;
1321 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
1329 ("Assertion failed: Could not find varobj \"%s\" in root list",
1336 prer
->next
= cr
->next
;
1343 /* Create and install a child of the parent of the given name */
1344 static struct varobj
*
1345 create_child (struct varobj
*parent
, int index
, char *name
)
1347 struct varobj
*child
;
1349 struct value
*value
;
1351 child
= new_variable ();
1353 /* name is allocated by name_of_child */
1355 child
->index
= index
;
1356 value
= value_of_child (parent
, index
);
1357 child
->parent
= parent
;
1358 child
->root
= parent
->root
;
1359 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, name
);
1360 child
->obj_name
= childs_name
;
1361 install_variable (child
);
1363 /* Compute the type of the child. Must do this before
1364 calling install_new_value. */
1366 /* If the child had no evaluation errors, var->value
1367 will be non-NULL and contain a valid type. */
1368 child
->type
= value_type (value
);
1370 /* Otherwise, we must compute the type. */
1371 child
->type
= (*child
->root
->lang
->type_of_child
) (child
->parent
,
1373 install_new_value (child
, value
, 1);
1380 * Miscellaneous utility functions.
1383 /* Allocate memory and initialize a new variable */
1384 static struct varobj
*
1389 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
1391 var
->obj_name
= NULL
;
1395 var
->num_children
= -1;
1397 var
->children
= NULL
;
1401 var
->print_value
= NULL
;
1406 /* Allocate memory and initialize a new root variable */
1407 static struct varobj
*
1408 new_root_variable (void)
1410 struct varobj
*var
= new_variable ();
1411 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));;
1412 var
->root
->lang
= NULL
;
1413 var
->root
->exp
= NULL
;
1414 var
->root
->valid_block
= NULL
;
1415 var
->root
->frame
= null_frame_id
;
1416 var
->root
->use_selected_frame
= 0;
1417 var
->root
->rootvar
= NULL
;
1418 var
->root
->is_valid
= 1;
1423 /* Free any allocated memory associated with VAR. */
1425 free_variable (struct varobj
*var
)
1427 /* Free the expression if this is a root variable. */
1428 if (is_root_p (var
))
1430 free_current_contents (&var
->root
->exp
);
1435 xfree (var
->obj_name
);
1436 xfree (var
->print_value
);
1441 do_free_variable_cleanup (void *var
)
1443 free_variable (var
);
1446 static struct cleanup
*
1447 make_cleanup_free_variable (struct varobj
*var
)
1449 return make_cleanup (do_free_variable_cleanup
, var
);
1452 /* This returns the type of the variable. It also skips past typedefs
1453 to return the real type of the variable.
1455 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1456 except within get_target_type and get_type. */
1457 static struct type
*
1458 get_type (struct varobj
*var
)
1464 type
= check_typedef (type
);
1469 /* Return the type of the value that's stored in VAR,
1470 or that would have being stored there if the
1471 value were accessible.
1473 This differs from VAR->type in that VAR->type is always
1474 the true type of the expession in the source language.
1475 The return value of this function is the type we're
1476 actually storing in varobj, and using for displaying
1477 the values and for comparing previous and new values.
1479 For example, top-level references are always stripped. */
1480 static struct type
*
1481 get_value_type (struct varobj
*var
)
1486 type
= value_type (var
->value
);
1490 type
= check_typedef (type
);
1492 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
1493 type
= get_target_type (type
);
1495 type
= check_typedef (type
);
1500 /* This returns the target type (or NULL) of TYPE, also skipping
1501 past typedefs, just like get_type ().
1503 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1504 except within get_target_type and get_type. */
1505 static struct type
*
1506 get_target_type (struct type
*type
)
1510 type
= TYPE_TARGET_TYPE (type
);
1512 type
= check_typedef (type
);
1518 /* What is the default display for this variable? We assume that
1519 everything is "natural". Any exceptions? */
1520 static enum varobj_display_formats
1521 variable_default_display (struct varobj
*var
)
1523 return FORMAT_NATURAL
;
1526 /* FIXME: The following should be generic for any pointer */
1528 cppush (struct cpstack
**pstack
, char *name
)
1532 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
1538 /* FIXME: The following should be generic for any pointer */
1540 cppop (struct cpstack
**pstack
)
1545 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
1550 *pstack
= (*pstack
)->next
;
1557 * Language-dependencies
1560 /* Common entry points */
1562 /* Get the language of variable VAR. */
1563 static enum varobj_languages
1564 variable_language (struct varobj
*var
)
1566 enum varobj_languages lang
;
1568 switch (var
->root
->exp
->language_defn
->la_language
)
1574 case language_cplus
:
1585 /* Return the number of children for a given variable.
1586 The result of this function is defined by the language
1587 implementation. The number of children returned by this function
1588 is the number of children that the user will see in the variable
1591 number_of_children (struct varobj
*var
)
1593 return (*var
->root
->lang
->number_of_children
) (var
);;
1596 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1598 name_of_variable (struct varobj
*var
)
1600 return (*var
->root
->lang
->name_of_variable
) (var
);
1603 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1605 name_of_child (struct varobj
*var
, int index
)
1607 return (*var
->root
->lang
->name_of_child
) (var
, index
);
1610 /* What is the ``struct value *'' of the root variable VAR?
1611 TYPE_CHANGED controls what to do if the type of a
1612 use_selected_frame = 1 variable changes. On input,
1613 TYPE_CHANGED = 1 means discard the old varobj, and replace
1614 it with this one. TYPE_CHANGED = 0 means leave it around.
1615 NB: In both cases, var_handle will point to the new varobj,
1616 so if you use TYPE_CHANGED = 0, you will have to stash the
1617 old varobj pointer away somewhere before calling this.
1618 On return, TYPE_CHANGED will be 1 if the type has changed, and
1620 static struct value
*
1621 value_of_root (struct varobj
**var_handle
, int *type_changed
)
1625 if (var_handle
== NULL
)
1630 /* This should really be an exception, since this should
1631 only get called with a root variable. */
1633 if (!is_root_p (var
))
1636 if (var
->root
->use_selected_frame
)
1638 struct varobj
*tmp_var
;
1639 char *old_type
, *new_type
;
1640 old_type
= varobj_get_type (var
);
1641 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
1642 USE_SELECTED_FRAME
);
1643 if (tmp_var
== NULL
)
1647 new_type
= varobj_get_type (tmp_var
);
1648 if (strcmp (old_type
, new_type
) == 0)
1650 varobj_delete (tmp_var
, NULL
, 0);
1658 savestring (var
->obj_name
, strlen (var
->obj_name
));
1659 varobj_delete (var
, NULL
, 0);
1663 tmp_var
->obj_name
= varobj_gen_name ();
1665 install_variable (tmp_var
);
1666 *var_handle
= tmp_var
;
1676 return (*var
->root
->lang
->value_of_root
) (var_handle
);
1679 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1680 static struct value
*
1681 value_of_child (struct varobj
*parent
, int index
)
1683 struct value
*value
;
1685 value
= (*parent
->root
->lang
->value_of_child
) (parent
, index
);
1690 /* Is this variable editable? Use the variable's type to make
1691 this determination. */
1693 variable_editable (struct varobj
*var
)
1695 return (*var
->root
->lang
->variable_editable
) (var
);
1698 /* GDB already has a command called "value_of_variable". Sigh. */
1700 my_value_of_variable (struct varobj
*var
)
1702 if (var
->root
->is_valid
)
1703 return (*var
->root
->lang
->value_of_variable
) (var
);
1709 value_get_print_value (struct value
*value
, enum varobj_display_formats format
)
1712 struct ui_file
*stb
;
1713 struct cleanup
*old_chain
;
1719 stb
= mem_fileopen ();
1720 old_chain
= make_cleanup_ui_file_delete (stb
);
1722 common_val_print (value
, stb
, format_code
[(int) format
], 1, 0, 0);
1723 thevalue
= ui_file_xstrdup (stb
, &dummy
);
1725 do_cleanups (old_chain
);
1729 /* Return non-zero if changes in value of VAR
1730 must be detected and reported by -var-update.
1731 Return zero is -var-update should never report
1732 changes of such values. This makes sense for structures
1733 (since the changes in children values will be reported separately),
1734 or for artifical objects (like 'public' pseudo-field in C++).
1736 Return value of 0 means that gdb need not call value_fetch_lazy
1737 for the value of this variable object. */
1739 varobj_value_is_changeable_p (struct varobj
*var
)
1744 if (CPLUS_FAKE_CHILD (var
))
1747 type
= get_value_type (var
);
1749 switch (TYPE_CODE (type
))
1751 case TYPE_CODE_STRUCT
:
1752 case TYPE_CODE_UNION
:
1753 case TYPE_CODE_ARRAY
:
1764 /* Given the value and the type of a variable object,
1765 adjust the value and type to those necessary
1766 for getting children of the variable object.
1767 This includes dereferencing top-level references
1768 to all types and dereferencing pointers to
1771 Both TYPE and *TYPE should be non-null. VALUE
1772 can be null if we want to only translate type.
1773 *VALUE can be null as well -- if the parent
1774 value is not known. */
1776 adjust_value_for_child_access (struct value
**value
,
1779 gdb_assert (type
&& *type
);
1781 *type
= check_typedef (*type
);
1783 /* The type of value stored in varobj, that is passed
1784 to us, is already supposed to be
1785 reference-stripped. */
1787 gdb_assert (TYPE_CODE (*type
) != TYPE_CODE_REF
);
1789 /* Pointers to structures are treated just like
1790 structures when accessing children. Don't
1791 dererences pointers to other types. */
1792 if (TYPE_CODE (*type
) == TYPE_CODE_PTR
)
1794 struct type
*target_type
= get_target_type (*type
);
1795 if (TYPE_CODE (target_type
) == TYPE_CODE_STRUCT
1796 || TYPE_CODE (target_type
) == TYPE_CODE_UNION
)
1798 if (value
&& *value
)
1799 gdb_value_ind (*value
, value
);
1800 *type
= target_type
;
1804 /* The 'get_target_type' function calls check_typedef on
1805 result, so we can immediately check type code. No
1806 need to call check_typedef here. */
1811 c_number_of_children (struct varobj
*var
)
1813 struct type
*type
= get_value_type (var
);
1815 struct type
*target
;
1817 adjust_value_for_child_access (NULL
, &type
);
1818 target
= get_target_type (type
);
1820 switch (TYPE_CODE (type
))
1822 case TYPE_CODE_ARRAY
:
1823 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
1824 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) != BOUND_CANNOT_BE_DETERMINED
)
1825 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
1827 /* If we don't know how many elements there are, don't display
1832 case TYPE_CODE_STRUCT
:
1833 case TYPE_CODE_UNION
:
1834 children
= TYPE_NFIELDS (type
);
1838 /* The type here is a pointer to non-struct. Typically, pointers
1839 have one child, except for function ptrs, which have no children,
1840 and except for void*, as we don't know what to show.
1842 We can show char* so we allow it to be dereferenced. If you decide
1843 to test for it, please mind that a little magic is necessary to
1844 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1845 TYPE_NAME == "char" */
1846 if (TYPE_CODE (target
) == TYPE_CODE_FUNC
1847 || TYPE_CODE (target
) == TYPE_CODE_VOID
)
1854 /* Other types have no children */
1862 c_name_of_variable (struct varobj
*parent
)
1864 return savestring (parent
->name
, strlen (parent
->name
));
1867 /* Return the value of element TYPE_INDEX of a structure
1868 value VALUE. VALUE's type should be a structure,
1869 or union, or a typedef to struct/union.
1871 Returns NULL if getting the value fails. Never throws. */
1872 static struct value
*
1873 value_struct_element_index (struct value
*value
, int type_index
)
1875 struct value
*result
= NULL
;
1876 volatile struct gdb_exception e
;
1878 struct type
*type
= value_type (value
);
1879 type
= check_typedef (type
);
1881 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
1882 || TYPE_CODE (type
) == TYPE_CODE_UNION
);
1884 TRY_CATCH (e
, RETURN_MASK_ERROR
)
1886 if (TYPE_FIELD_STATIC (type
, type_index
))
1887 result
= value_static_field (type
, type_index
);
1889 result
= value_primitive_field (value
, 0, type_index
, type
);
1901 /* Obtain the information about child INDEX of the variable
1903 If CNAME is not null, sets *CNAME to the name of the child relative
1905 If CVALUE is not null, sets *CVALUE to the value of the child.
1906 If CTYPE is not null, sets *CTYPE to the type of the child.
1908 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
1909 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
1912 c_describe_child (struct varobj
*parent
, int index
,
1913 char **cname
, struct value
**cvalue
, struct type
**ctype
)
1915 struct value
*value
= parent
->value
;
1916 struct type
*type
= get_value_type (parent
);
1925 adjust_value_for_child_access (&value
, &type
);
1927 switch (TYPE_CODE (type
))
1929 case TYPE_CODE_ARRAY
:
1931 *cname
= xstrprintf ("%d", index
1932 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
1934 if (cvalue
&& value
)
1936 int real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
1937 struct value
*indval
=
1938 value_from_longest (builtin_type_int
, (LONGEST
) real_index
);
1939 gdb_value_subscript (value
, indval
, cvalue
);
1943 *ctype
= get_target_type (type
);
1947 case TYPE_CODE_STRUCT
:
1948 case TYPE_CODE_UNION
:
1951 char *string
= TYPE_FIELD_NAME (type
, index
);
1952 *cname
= savestring (string
, strlen (string
));
1955 if (cvalue
&& value
)
1957 /* For C, varobj index is the same as type index. */
1958 *cvalue
= value_struct_element_index (value
, index
);
1962 *ctype
= TYPE_FIELD_TYPE (type
, index
);
1968 *cname
= xstrprintf ("*%s", parent
->name
);
1970 if (cvalue
&& value
)
1971 gdb_value_ind (value
, cvalue
);
1973 /* Don't use get_target_type because it calls
1974 check_typedef and here, we want to show the true
1975 declared type of the variable. */
1977 *ctype
= TYPE_TARGET_TYPE (type
);
1982 /* This should not happen */
1984 *cname
= xstrdup ("???");
1985 /* Don't set value and type, we don't know then. */
1990 c_name_of_child (struct varobj
*parent
, int index
)
1993 c_describe_child (parent
, index
, &name
, NULL
, NULL
);
1997 static struct value
*
1998 c_value_of_root (struct varobj
**var_handle
)
2000 struct value
*new_val
= NULL
;
2001 struct varobj
*var
= *var_handle
;
2002 struct frame_info
*fi
;
2005 /* Only root variables can be updated... */
2006 if (!is_root_p (var
))
2007 /* Not a root var */
2011 /* Determine whether the variable is still around. */
2012 if (var
->root
->valid_block
== NULL
|| var
->root
->use_selected_frame
)
2016 fi
= frame_find_by_id (var
->root
->frame
);
2017 within_scope
= fi
!= NULL
;
2018 /* FIXME: select_frame could fail */
2021 CORE_ADDR pc
= get_frame_pc (fi
);
2022 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
2023 pc
>= BLOCK_END (var
->root
->valid_block
))
2032 /* We need to catch errors here, because if evaluate
2033 expression fails we want to just return NULL. */
2034 gdb_evaluate_expression (var
->root
->exp
, &new_val
);
2041 static struct value
*
2042 c_value_of_child (struct varobj
*parent
, int index
)
2044 struct value
*value
= NULL
;
2045 c_describe_child (parent
, index
, NULL
, &value
, NULL
);
2050 static struct type
*
2051 c_type_of_child (struct varobj
*parent
, int index
)
2053 struct type
*type
= NULL
;
2054 c_describe_child (parent
, index
, NULL
, NULL
, &type
);
2059 c_variable_editable (struct varobj
*var
)
2061 switch (TYPE_CODE (get_value_type (var
)))
2063 case TYPE_CODE_STRUCT
:
2064 case TYPE_CODE_UNION
:
2065 case TYPE_CODE_ARRAY
:
2066 case TYPE_CODE_FUNC
:
2067 case TYPE_CODE_METHOD
:
2078 c_value_of_variable (struct varobj
*var
)
2080 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2081 it will print out its children instead of "{...}". So we need to
2082 catch that case explicitly. */
2083 struct type
*type
= get_type (var
);
2085 /* Strip top-level references. */
2086 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
2087 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2089 switch (TYPE_CODE (type
))
2091 case TYPE_CODE_STRUCT
:
2092 case TYPE_CODE_UNION
:
2093 return xstrdup ("{...}");
2096 case TYPE_CODE_ARRAY
:
2099 number
= xstrprintf ("[%d]", var
->num_children
);
2106 if (var
->value
== NULL
)
2108 /* This can happen if we attempt to get the value of a struct
2109 member when the parent is an invalid pointer. This is an
2110 error condition, so we should tell the caller. */
2115 gdb_assert (varobj_value_is_changeable_p (var
));
2116 gdb_assert (!value_lazy (var
->value
));
2117 return value_get_print_value (var
->value
, var
->format
);
2127 cplus_number_of_children (struct varobj
*var
)
2130 int children
, dont_know
;
2135 if (!CPLUS_FAKE_CHILD (var
))
2137 type
= get_value_type (var
);
2138 adjust_value_for_child_access (NULL
, &type
);
2140 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2141 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2145 cplus_class_num_children (type
, kids
);
2146 if (kids
[v_public
] != 0)
2148 if (kids
[v_private
] != 0)
2150 if (kids
[v_protected
] != 0)
2153 /* Add any baseclasses */
2154 children
+= TYPE_N_BASECLASSES (type
);
2157 /* FIXME: save children in var */
2164 type
= get_value_type (var
->parent
);
2165 adjust_value_for_child_access (NULL
, &type
);
2167 cplus_class_num_children (type
, kids
);
2168 if (strcmp (var
->name
, "public") == 0)
2169 children
= kids
[v_public
];
2170 else if (strcmp (var
->name
, "private") == 0)
2171 children
= kids
[v_private
];
2173 children
= kids
[v_protected
];
2178 children
= c_number_of_children (var
);
2183 /* Compute # of public, private, and protected variables in this class.
2184 That means we need to descend into all baseclasses and find out
2185 how many are there, too. */
2187 cplus_class_num_children (struct type
*type
, int children
[3])
2191 children
[v_public
] = 0;
2192 children
[v_private
] = 0;
2193 children
[v_protected
] = 0;
2195 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
2197 /* If we have a virtual table pointer, omit it. */
2198 if (TYPE_VPTR_BASETYPE (type
) == type
&& TYPE_VPTR_FIELDNO (type
) == i
)
2201 if (TYPE_FIELD_PROTECTED (type
, i
))
2202 children
[v_protected
]++;
2203 else if (TYPE_FIELD_PRIVATE (type
, i
))
2204 children
[v_private
]++;
2206 children
[v_public
]++;
2211 cplus_name_of_variable (struct varobj
*parent
)
2213 return c_name_of_variable (parent
);
2216 enum accessibility
{ private_field
, protected_field
, public_field
};
2218 /* Check if field INDEX of TYPE has the specified accessibility.
2219 Return 0 if so and 1 otherwise. */
2221 match_accessibility (struct type
*type
, int index
, enum accessibility acc
)
2223 if (acc
== private_field
&& TYPE_FIELD_PRIVATE (type
, index
))
2225 else if (acc
== protected_field
&& TYPE_FIELD_PROTECTED (type
, index
))
2227 else if (acc
== public_field
&& !TYPE_FIELD_PRIVATE (type
, index
)
2228 && !TYPE_FIELD_PROTECTED (type
, index
))
2235 cplus_describe_child (struct varobj
*parent
, int index
,
2236 char **cname
, struct value
**cvalue
, struct type
**ctype
)
2239 struct value
*value
;
2250 if (CPLUS_FAKE_CHILD (parent
))
2252 value
= parent
->parent
->value
;
2253 type
= get_value_type (parent
->parent
);
2257 value
= parent
->value
;
2258 type
= get_value_type (parent
);
2261 adjust_value_for_child_access (&value
, &type
);
2263 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
2264 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
2266 if (CPLUS_FAKE_CHILD (parent
))
2268 /* The fields of the class type are ordered as they
2269 appear in the class. We are given an index for a
2270 particular access control type ("public","protected",
2271 or "private"). We must skip over fields that don't
2272 have the access control we are looking for to properly
2273 find the indexed field. */
2274 int type_index
= TYPE_N_BASECLASSES (type
);
2275 enum accessibility acc
= public_field
;
2276 if (strcmp (parent
->name
, "private") == 0)
2277 acc
= private_field
;
2278 else if (strcmp (parent
->name
, "protected") == 0)
2279 acc
= protected_field
;
2283 if (TYPE_VPTR_BASETYPE (type
) == type
2284 && type_index
== TYPE_VPTR_FIELDNO (type
))
2286 else if (match_accessibility (type
, type_index
, acc
))
2293 *cname
= xstrdup (TYPE_FIELD_NAME (type
, type_index
));
2295 if (cvalue
&& value
)
2296 *cvalue
= value_struct_element_index (value
, type_index
);
2299 *ctype
= TYPE_FIELD_TYPE (type
, type_index
);
2301 else if (index
< TYPE_N_BASECLASSES (type
))
2303 /* This is a baseclass. */
2305 *cname
= xstrdup (TYPE_FIELD_NAME (type
, index
));
2307 if (cvalue
&& value
)
2309 *cvalue
= value_cast (TYPE_FIELD_TYPE (type
, index
), value
);
2314 *ctype
= TYPE_FIELD_TYPE (type
, index
);
2321 cplus_class_num_children (type
, children
);
2323 /* Everything beyond the baseclasses can
2324 only be "public", "private", or "protected"
2326 The special "fake" children are always output by varobj in
2327 this order. So if INDEX == 2, it MUST be "protected". */
2328 index
-= TYPE_N_BASECLASSES (type
);
2332 if (children
[v_public
] > 0)
2334 else if (children
[v_private
] > 0)
2337 access
= "protected";
2340 if (children
[v_public
] > 0)
2342 if (children
[v_private
] > 0)
2345 access
= "protected";
2347 else if (children
[v_private
] > 0)
2348 access
= "protected";
2351 /* Must be protected */
2352 access
= "protected";
2360 *cname
= xstrdup (access
);
2362 /* Value and type are null here. */
2367 c_describe_child (parent
, index
, cname
, cvalue
, ctype
);
2372 cplus_name_of_child (struct varobj
*parent
, int index
)
2375 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
);
2379 static struct value
*
2380 cplus_value_of_root (struct varobj
**var_handle
)
2382 return c_value_of_root (var_handle
);
2385 static struct value
*
2386 cplus_value_of_child (struct varobj
*parent
, int index
)
2388 struct value
*value
= NULL
;
2389 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
);
2393 static struct type
*
2394 cplus_type_of_child (struct varobj
*parent
, int index
)
2396 struct type
*type
= NULL
;
2397 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
);
2402 cplus_variable_editable (struct varobj
*var
)
2404 if (CPLUS_FAKE_CHILD (var
))
2407 return c_variable_editable (var
);
2411 cplus_value_of_variable (struct varobj
*var
)
2414 /* If we have one of our special types, don't print out
2416 if (CPLUS_FAKE_CHILD (var
))
2417 return xstrdup ("");
2419 return c_value_of_variable (var
);
2425 java_number_of_children (struct varobj
*var
)
2427 return cplus_number_of_children (var
);
2431 java_name_of_variable (struct varobj
*parent
)
2435 name
= cplus_name_of_variable (parent
);
2436 /* If the name has "-" in it, it is because we
2437 needed to escape periods in the name... */
2440 while (*p
!= '\000')
2451 java_name_of_child (struct varobj
*parent
, int index
)
2455 name
= cplus_name_of_child (parent
, index
);
2456 /* Escape any periods in the name... */
2459 while (*p
!= '\000')
2469 static struct value
*
2470 java_value_of_root (struct varobj
**var_handle
)
2472 return cplus_value_of_root (var_handle
);
2475 static struct value
*
2476 java_value_of_child (struct varobj
*parent
, int index
)
2478 return cplus_value_of_child (parent
, index
);
2481 static struct type
*
2482 java_type_of_child (struct varobj
*parent
, int index
)
2484 return cplus_type_of_child (parent
, index
);
2488 java_variable_editable (struct varobj
*var
)
2490 return cplus_variable_editable (var
);
2494 java_value_of_variable (struct varobj
*var
)
2496 return cplus_value_of_variable (var
);
2499 extern void _initialize_varobj (void);
2501 _initialize_varobj (void)
2503 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
2505 varobj_table
= xmalloc (sizeof_table
);
2506 memset (varobj_table
, 0, sizeof_table
);
2508 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance
,
2510 Set varobj debugging."), _("\
2511 Show varobj debugging."), _("\
2512 When non-zero, varobj debugging is enabled."),
2515 &setlist
, &showlist
);
2518 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2519 are defined on globals.
2520 Invalidated varobjs will be always printed in_scope="invalid". */
2522 varobj_invalidate (void)
2524 struct varobj
**all_rootvarobj
;
2525 struct varobj
**varp
;
2527 if (varobj_list (&all_rootvarobj
) > 0)
2529 varp
= all_rootvarobj
;
2530 while (*varp
!= NULL
)
2532 /* global var must be re-evaluated. */
2533 if ((*varp
)->root
->valid_block
== NULL
)
2535 struct varobj
*tmp_var
;
2537 /* Try to create a varobj with same expression. If we succeed replace
2538 the old varobj, otherwise invalidate it. */
2539 tmp_var
= varobj_create (NULL
, (*varp
)->name
, (CORE_ADDR
) 0, USE_CURRENT_FRAME
);
2540 if (tmp_var
!= NULL
)
2542 tmp_var
->obj_name
= xstrdup ((*varp
)->obj_name
);
2543 varobj_delete (*varp
, NULL
, 0);
2544 install_variable (tmp_var
);
2547 (*varp
)->root
->is_valid
= 0;
2549 else /* locals must be invalidated. */
2550 (*varp
)->root
->is_valid
= 0;
2554 xfree (all_rootvarobj
);