1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "exceptions.h"
22 #include "expression.h"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
35 #include "gdbthread.h"
38 /* Non-zero if we want to see trace of varobj level stuff. */
42 show_varobjdebug (struct ui_file *file, int from_tty,
43 struct cmd_list_element *c, const char *value)
45 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
48 /* String representations of gdb's format codes */
49 char *varobj_format_string[] =
50 { "natural", "binary", "decimal", "hexadecimal", "octal" };
52 /* String representations of gdb's known languages */
53 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
57 /* Every root variable has one of these structures saved in its
58 varobj. Members which must be free'd are noted. */
62 /* Alloc'd expression for this parent. */
63 struct expression *exp;
65 /* Block for which this expression is valid */
66 struct block *valid_block;
68 /* The frame for this expression. This field is set iff valid_block is
70 struct frame_id frame;
72 /* The thread ID that this varobj_root belong to. This field
73 is only valid if valid_block is not NULL.
74 When not 0, indicates which thread 'frame' belongs to.
75 When 0, indicates that the thread list was empty when the varobj_root
79 /* If 1, the -var-update always recomputes the value in the
80 current thread and frame. Otherwise, variable object is
81 always updated in the specific scope/thread/frame */
84 /* Flag that indicates validity: set to 0 when this varobj_root refers
85 to symbols that do not exist anymore. */
88 /* Language info for this variable and its children */
89 struct language_specific *lang;
91 /* The varobj for this root node. */
92 struct varobj *rootvar;
94 /* Next root variable */
95 struct varobj_root *next;
98 /* Every variable in the system has a structure of this type defined
99 for it. This structure holds all information necessary to manipulate
100 a particular object variable. Members which must be freed are noted. */
104 /* Alloc'd name of the variable for this object.. If this variable is a
105 child, then this name will be the child's source name.
106 (bar, not foo.bar) */
107 /* NOTE: This is the "expression" */
110 /* Alloc'd expression for this child. Can be used to create a
111 root variable corresponding to this child. */
114 /* The alloc'd name for this variable's object. This is here for
115 convenience when constructing this object's children. */
118 /* Index of this variable in its parent or -1 */
121 /* The type of this variable. This can be NULL
122 for artifial variable objects -- currently, the "accessibility"
123 variable objects in C++. */
126 /* The value of this expression or subexpression. A NULL value
127 indicates there was an error getting this value.
128 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
129 the value is either NULL, or not lazy. */
132 /* The number of (immediate) children this variable has */
135 /* If this object is a child, this points to its immediate parent. */
136 struct varobj *parent;
138 /* Children of this object. */
139 VEC (varobj_p) *children;
141 /* Description of the root variable. Points to root variable for children. */
142 struct varobj_root *root;
144 /* The format of the output for this object */
145 enum varobj_display_formats format;
147 /* Was this variable updated via a varobj_set_value operation */
150 /* Last print value. */
153 /* Is this variable frozen. Frozen variables are never implicitly
154 updated by -var-update *
155 or -var-update <direct-or-indirect-parent>. */
158 /* Is the value of this variable intentionally not fetched? It is
159 not fetched if either the variable is frozen, or any parents is
167 struct cpstack *next;
170 /* A list of varobjs */
178 /* Private function prototypes */
180 /* Helper functions for the above subcommands. */
182 static int delete_variable (struct cpstack **, struct varobj *, int);
184 static void delete_variable_1 (struct cpstack **, int *,
185 struct varobj *, int, int);
187 static int install_variable (struct varobj *);
189 static void uninstall_variable (struct varobj *);
191 static struct varobj *create_child (struct varobj *, int, char *);
193 /* Utility routines */
195 static struct varobj *new_variable (void);
197 static struct varobj *new_root_variable (void);
199 static void free_variable (struct varobj *var);
201 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
203 static struct type *get_type (struct varobj *var);
205 static struct type *get_value_type (struct varobj *var);
207 static struct type *get_target_type (struct type *);
209 static enum varobj_display_formats variable_default_display (struct varobj *);
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 char *my_value_of_variable (struct varobj *var,
233 enum varobj_display_formats format);
235 static char *value_get_print_value (struct value *value,
236 enum varobj_display_formats format);
238 static int varobj_value_is_changeable_p (struct varobj *var);
240 static int is_root_p (struct varobj *var);
242 /* C implementation */
244 static int c_number_of_children (struct varobj *var);
246 static char *c_name_of_variable (struct varobj *parent);
248 static char *c_name_of_child (struct varobj *parent, int index);
250 static char *c_path_expr_of_child (struct varobj *child);
252 static struct value *c_value_of_root (struct varobj **var_handle);
254 static struct value *c_value_of_child (struct varobj *parent, int index);
256 static struct type *c_type_of_child (struct varobj *parent, int index);
258 static char *c_value_of_variable (struct varobj *var,
259 enum varobj_display_formats format);
261 /* C++ implementation */
263 static int cplus_number_of_children (struct varobj *var);
265 static void cplus_class_num_children (struct type *type, int children[3]);
267 static char *cplus_name_of_variable (struct varobj *parent);
269 static char *cplus_name_of_child (struct varobj *parent, int index);
271 static char *cplus_path_expr_of_child (struct varobj *child);
273 static struct value *cplus_value_of_root (struct varobj **var_handle);
275 static struct value *cplus_value_of_child (struct varobj *parent, int index);
277 static struct type *cplus_type_of_child (struct varobj *parent, int index);
279 static char *cplus_value_of_variable (struct varobj *var,
280 enum varobj_display_formats format);
282 /* Java implementation */
284 static int java_number_of_children (struct varobj *var);
286 static char *java_name_of_variable (struct varobj *parent);
288 static char *java_name_of_child (struct varobj *parent, int index);
290 static char *java_path_expr_of_child (struct varobj *child);
292 static struct value *java_value_of_root (struct varobj **var_handle);
294 static struct value *java_value_of_child (struct varobj *parent, int index);
296 static struct type *java_type_of_child (struct varobj *parent, int index);
298 static char *java_value_of_variable (struct varobj *var,
299 enum varobj_display_formats format);
301 /* The language specific vector */
303 struct language_specific
306 /* The language of this variable */
307 enum varobj_languages language;
309 /* The number of children of PARENT. */
310 int (*number_of_children) (struct varobj * parent);
312 /* The name (expression) of a root varobj. */
313 char *(*name_of_variable) (struct varobj * parent);
315 /* The name of the INDEX'th child of PARENT. */
316 char *(*name_of_child) (struct varobj * parent, int index);
318 /* Returns the rooted expression of CHILD, which is a variable
319 obtain that has some parent. */
320 char *(*path_expr_of_child) (struct varobj * child);
322 /* The ``struct value *'' of the root variable ROOT. */
323 struct value *(*value_of_root) (struct varobj ** root_handle);
325 /* The ``struct value *'' of the INDEX'th child of PARENT. */
326 struct value *(*value_of_child) (struct varobj * parent, int index);
328 /* The type of the INDEX'th child of PARENT. */
329 struct type *(*type_of_child) (struct varobj * parent, int index);
331 /* The current value of VAR. */
332 char *(*value_of_variable) (struct varobj * var,
333 enum varobj_display_formats format);
336 /* Array of known source language routines. */
337 static struct language_specific languages[vlang_end] = {
338 /* Unknown (try treating as C */
341 c_number_of_children,
344 c_path_expr_of_child,
353 c_number_of_children,
356 c_path_expr_of_child,
365 cplus_number_of_children,
366 cplus_name_of_variable,
368 cplus_path_expr_of_child,
370 cplus_value_of_child,
372 cplus_value_of_variable}
377 java_number_of_children,
378 java_name_of_variable,
380 java_path_expr_of_child,
384 java_value_of_variable}
387 /* A little convenience enum for dealing with C++/Java */
390 v_public = 0, v_private, v_protected
395 /* Mappings of varobj_display_formats enums to gdb's format codes */
396 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
398 /* Header of the list of root variable objects */
399 static struct varobj_root *rootlist;
400 static int rootcount = 0; /* number of root varobjs in the list */
402 /* Prime number indicating the number of buckets in the hash table */
403 /* A prime large enough to avoid too many colisions */
404 #define VAROBJ_TABLE_SIZE 227
406 /* Pointer to the varobj hash table (built at run time) */
407 static struct vlist **varobj_table;
409 /* Is the variable X one of our "fake" children? */
410 #define CPLUS_FAKE_CHILD(x) \
411 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
414 /* API Implementation */
416 is_root_p (struct varobj *var)
418 return (var->root->rootvar == var);
421 /* Creates a varobj (not its children) */
423 /* Return the full FRAME which corresponds to the given CORE_ADDR
424 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
426 static struct frame_info *
427 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
429 struct frame_info *frame = NULL;
431 if (frame_addr == (CORE_ADDR) 0)
434 for (frame = get_current_frame ();
436 frame = get_prev_frame (frame))
438 if (get_frame_base_address (frame) == frame_addr)
446 varobj_create (char *objname,
447 char *expression, CORE_ADDR frame, enum varobj_type type)
450 struct frame_info *fi;
451 struct frame_info *old_fi = NULL;
453 struct cleanup *old_chain;
455 /* Fill out a varobj structure for the (root) variable being constructed. */
456 var = new_root_variable ();
457 old_chain = make_cleanup_free_variable (var);
459 if (expression != NULL)
462 enum varobj_languages lang;
463 struct value *value = NULL;
466 /* Parse and evaluate the expression, filling in as much of the
467 variable's data as possible. */
469 if (has_stack_frames ())
471 /* Allow creator to specify context of variable */
472 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
473 fi = get_selected_frame (NULL);
475 /* FIXME: cagney/2002-11-23: This code should be doing a
476 lookup using the frame ID and not just the frame's
477 ``address''. This, of course, means an interface
478 change. However, with out that interface change ISAs,
479 such as the ia64 with its two stacks, won't work.
480 Similar goes for the case where there is a frameless
482 fi = find_frame_addr_in_frame_chain (frame);
487 /* frame = -2 means always use selected frame */
488 if (type == USE_SELECTED_FRAME)
489 var->root->floating = 1;
493 block = get_frame_block (fi, 0);
496 innermost_block = NULL;
497 /* Wrap the call to parse expression, so we can
498 return a sensible error. */
499 if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
504 /* Don't allow variables to be created for types. */
505 if (var->root->exp->elts[0].opcode == OP_TYPE)
507 do_cleanups (old_chain);
508 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
509 " as an expression.\n");
513 var->format = variable_default_display (var);
514 var->root->valid_block = innermost_block;
515 expr_len = strlen (expression);
516 var->name = savestring (expression, expr_len);
517 /* For a root var, the name and the expr are the same. */
518 var->path_expr = savestring (expression, expr_len);
520 /* When the frame is different from the current frame,
521 we must select the appropriate frame before parsing
522 the expression, otherwise the value will not be current.
523 Since select_frame is so benign, just call it for all cases. */
524 if (innermost_block && fi != NULL)
526 var->root->frame = get_frame_id (fi);
527 var->root->thread_id = pid_to_thread_id (inferior_ptid);
528 old_fi = get_selected_frame (NULL);
532 /* We definitely need to catch errors here.
533 If evaluate_expression succeeds we got the value we wanted.
534 But if it fails, we still go on with a call to evaluate_type() */
535 if (!gdb_evaluate_expression (var->root->exp, &value))
537 /* Error getting the value. Try to at least get the
539 struct value *type_only_value = evaluate_type (var->root->exp);
540 var->type = value_type (type_only_value);
543 var->type = value_type (value);
545 install_new_value (var, value, 1 /* Initial assignment */);
547 /* Set language info */
548 lang = variable_language (var);
549 var->root->lang = &languages[lang];
551 /* Set ourselves as our root */
552 var->root->rootvar = var;
554 /* Reset the selected frame */
556 select_frame (old_fi);
559 /* If the variable object name is null, that means this
560 is a temporary variable, so don't install it. */
562 if ((var != NULL) && (objname != NULL))
564 var->obj_name = savestring (objname, strlen (objname));
566 /* If a varobj name is duplicated, the install will fail so
568 if (!install_variable (var))
570 do_cleanups (old_chain);
575 discard_cleanups (old_chain);
579 /* Generates an unique name that can be used for a varobj */
582 varobj_gen_name (void)
587 /* generate a name for this object */
589 obj_name = xstrprintf ("var%d", id);
594 /* Given an "objname", returns the pointer to the corresponding varobj
595 or NULL if not found */
598 varobj_get_handle (char *objname)
602 unsigned int index = 0;
605 for (chp = objname; *chp; chp++)
607 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
610 cv = *(varobj_table + index);
611 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
615 error (_("Variable object not found"));
620 /* Given the handle, return the name of the object */
623 varobj_get_objname (struct varobj *var)
625 return var->obj_name;
628 /* Given the handle, return the expression represented by the object */
631 varobj_get_expression (struct varobj *var)
633 return name_of_variable (var);
636 /* Deletes a varobj and all its children if only_children == 0,
637 otherwise deletes only the children; returns a malloc'ed list of all the
638 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
641 varobj_delete (struct varobj *var, char ***dellist, int only_children)
645 struct cpstack *result = NULL;
648 /* Initialize a stack for temporary results */
649 cppush (&result, NULL);
652 /* Delete only the variable children */
653 delcount = delete_variable (&result, var, 1 /* only the children */ );
655 /* Delete the variable and all its children */
656 delcount = delete_variable (&result, var, 0 /* parent+children */ );
658 /* We may have been asked to return a list of what has been deleted */
661 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
665 *cp = cppop (&result);
666 while ((*cp != NULL) && (mycount > 0))
670 *cp = cppop (&result);
673 if (mycount || (*cp != NULL))
674 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
681 /* Set/Get variable object display format */
683 enum varobj_display_formats
684 varobj_set_display_format (struct varobj *var,
685 enum varobj_display_formats format)
692 case FORMAT_HEXADECIMAL:
694 var->format = format;
698 var->format = variable_default_display (var);
701 if (varobj_value_is_changeable_p (var)
702 && var->value && !value_lazy (var->value))
704 xfree (var->print_value);
705 var->print_value = value_get_print_value (var->value, var->format);
711 enum varobj_display_formats
712 varobj_get_display_format (struct varobj *var)
717 /* If the variable object is bound to a specific thread, that
718 is its evaluation can always be done in context of a frame
719 inside that thread, returns GDB id of the thread -- which
720 is always positive. Otherwise, returns -1. */
722 varobj_get_thread_id (struct varobj *var)
724 if (var->root->valid_block && var->root->thread_id > 0)
725 return var->root->thread_id;
731 varobj_set_frozen (struct varobj *var, int frozen)
733 /* When a variable is unfrozen, we don't fetch its value.
734 The 'not_fetched' flag remains set, so next -var-update
737 We don't fetch the value, because for structures the client
738 should do -var-update anyway. It would be bad to have different
739 client-size logic for structure and other types. */
740 var->frozen = frozen;
744 varobj_get_frozen (struct varobj *var)
751 varobj_get_num_children (struct varobj *var)
753 if (var->num_children == -1)
754 var->num_children = number_of_children (var);
756 return var->num_children;
759 /* Creates a list of the immediate children of a variable object;
760 the return code is the number of such children or -1 on error */
763 varobj_list_children (struct varobj *var)
765 struct varobj *child;
769 if (var->num_children == -1)
770 var->num_children = number_of_children (var);
772 /* If that failed, give up. */
773 if (var->num_children == -1)
774 return var->children;
776 /* If we're called when the list of children is not yet initialized,
777 allocate enough elements in it. */
778 while (VEC_length (varobj_p, var->children) < var->num_children)
779 VEC_safe_push (varobj_p, var->children, NULL);
781 for (i = 0; i < var->num_children; i++)
783 varobj_p existing = VEC_index (varobj_p, var->children, i);
785 if (existing == NULL)
787 /* Either it's the first call to varobj_list_children for
788 this variable object, and the child was never created,
789 or it was explicitly deleted by the client. */
790 name = name_of_child (var, i);
791 existing = create_child (var, i, name);
792 VEC_replace (varobj_p, var->children, i, existing);
796 return var->children;
799 /* Obtain the type of an object Variable as a string similar to the one gdb
800 prints on the console */
803 varobj_get_type (struct varobj *var)
806 struct cleanup *old_chain;
811 /* For the "fake" variables, do not return a type. (It's type is
813 Do not return a type for invalid variables as well. */
814 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
817 stb = mem_fileopen ();
818 old_chain = make_cleanup_ui_file_delete (stb);
820 /* To print the type, we simply create a zero ``struct value *'' and
821 cast it to our type. We then typeprint this variable. */
822 val = value_zero (var->type, not_lval);
823 type_print (value_type (val), "", stb, -1);
825 thetype = ui_file_xstrdup (stb, &length);
826 do_cleanups (old_chain);
830 /* Obtain the type of an object variable. */
833 varobj_get_gdb_type (struct varobj *var)
838 /* Return a pointer to the full rooted expression of varobj VAR.
839 If it has not been computed yet, compute it. */
841 varobj_get_path_expr (struct varobj *var)
843 if (var->path_expr != NULL)
844 return var->path_expr;
847 /* For root varobjs, we initialize path_expr
848 when creating varobj, so here it should be
850 gdb_assert (!is_root_p (var));
851 return (*var->root->lang->path_expr_of_child) (var);
855 enum varobj_languages
856 varobj_get_language (struct varobj *var)
858 return variable_language (var);
862 varobj_get_attributes (struct varobj *var)
866 if (varobj_editable_p (var))
867 /* FIXME: define masks for attributes */
868 attributes |= 0x00000001; /* Editable */
874 varobj_get_formatted_value (struct varobj *var,
875 enum varobj_display_formats format)
877 return my_value_of_variable (var, format);
881 varobj_get_value (struct varobj *var)
883 return my_value_of_variable (var, var->format);
886 /* Set the value of an object variable (if it is editable) to the
887 value of the given expression */
888 /* Note: Invokes functions that can call error() */
891 varobj_set_value (struct varobj *var, char *expression)
897 /* The argument "expression" contains the variable's new value.
898 We need to first construct a legal expression for this -- ugh! */
899 /* Does this cover all the bases? */
900 struct expression *exp;
902 int saved_input_radix = input_radix;
903 char *s = expression;
906 gdb_assert (varobj_editable_p (var));
908 input_radix = 10; /* ALWAYS reset to decimal temporarily */
909 exp = parse_exp_1 (&s, 0, 0);
910 if (!gdb_evaluate_expression (exp, &value))
912 /* We cannot proceed without a valid expression. */
917 /* All types that are editable must also be changeable. */
918 gdb_assert (varobj_value_is_changeable_p (var));
920 /* The value of a changeable variable object must not be lazy. */
921 gdb_assert (!value_lazy (var->value));
923 /* Need to coerce the input. We want to check if the
924 value of the variable object will be different
925 after assignment, and the first thing value_assign
926 does is coerce the input.
927 For example, if we are assigning an array to a pointer variable we
928 should compare the pointer with the the array's address, not with the
930 value = coerce_array (value);
932 /* The new value may be lazy. gdb_value_assign, or
933 rather value_contents, will take care of this.
934 If fetching of the new value will fail, gdb_value_assign
935 with catch the exception. */
936 if (!gdb_value_assign (var->value, value, &val))
939 /* If the value has changed, record it, so that next -var-update can
940 report this change. If a variable had a value of '1', we've set it
941 to '333' and then set again to '1', when -var-update will report this
942 variable as changed -- because the first assignment has set the
943 'updated' flag. There's no need to optimize that, because return value
944 of -var-update should be considered an approximation. */
945 var->updated = install_new_value (var, val, 0 /* Compare values. */);
946 input_radix = saved_input_radix;
950 /* Returns a malloc'ed list with all root variable objects */
952 varobj_list (struct varobj ***varlist)
955 struct varobj_root *croot;
956 int mycount = rootcount;
958 /* Alloc (rootcount + 1) entries for the result */
959 *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
963 while ((croot != NULL) && (mycount > 0))
965 *cv = croot->rootvar;
970 /* Mark the end of the list */
973 if (mycount || (croot != NULL))
975 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
981 /* Assign a new value to a variable object. If INITIAL is non-zero,
982 this is the first assignement after the variable object was just
983 created, or changed type. In that case, just assign the value
985 Otherwise, assign the value and if type_changeable returns non-zero,
986 find if the new value is different from the current value.
987 Return 1 if so, and 0 if the values are equal.
989 The VALUE parameter should not be released -- the function will
990 take care of releasing it when needed. */
992 install_new_value (struct varobj *var, struct value *value, int initial)
997 int intentionally_not_fetched = 0;
998 char *print_value = NULL;
1000 /* We need to know the varobj's type to decide if the value should
1001 be fetched or not. C++ fake children (public/protected/private) don't have
1003 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1004 changeable = varobj_value_is_changeable_p (var);
1005 need_to_fetch = changeable;
1007 /* We are not interested in the address of references, and given
1008 that in C++ a reference is not rebindable, it cannot
1009 meaningfully change. So, get hold of the real value. */
1012 value = coerce_ref (value);
1013 release_value (value);
1016 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
1017 /* For unions, we need to fetch the value implicitly because
1018 of implementation of union member fetch. When gdb
1019 creates a value for a field and the value of the enclosing
1020 structure is not lazy, it immediately copies the necessary
1021 bytes from the enclosing values. If the enclosing value is
1022 lazy, the call to value_fetch_lazy on the field will read
1023 the data from memory. For unions, that means we'll read the
1024 same memory more than once, which is not desirable. So
1028 /* The new value might be lazy. If the type is changeable,
1029 that is we'll be comparing values of this type, fetch the
1030 value now. Otherwise, on the next update the old value
1031 will be lazy, which means we've lost that old value. */
1032 if (need_to_fetch && value && value_lazy (value))
1034 struct varobj *parent = var->parent;
1035 int frozen = var->frozen;
1036 for (; !frozen && parent; parent = parent->parent)
1037 frozen |= parent->frozen;
1039 if (frozen && initial)
1041 /* For variables that are frozen, or are children of frozen
1042 variables, we don't do fetch on initial assignment.
1043 For non-initial assignemnt we do the fetch, since it means we're
1044 explicitly asked to compare the new value with the old one. */
1045 intentionally_not_fetched = 1;
1047 else if (!gdb_value_fetch_lazy (value))
1049 /* Set the value to NULL, so that for the next -var-update,
1050 we don't try to compare the new value with this value,
1051 that we couldn't even read. */
1056 /* Below, we'll be comparing string rendering of old and new
1057 values. Don't get string rendering if the value is
1058 lazy -- if it is, the code above has decided that the value
1059 should not be fetched. */
1060 if (value && !value_lazy (value))
1061 print_value = value_get_print_value (value, var->format);
1063 /* If the type is changeable, compare the old and the new values.
1064 If this is the initial assignment, we don't have any old value
1066 if (!initial && changeable)
1068 /* If the value of the varobj was changed by -var-set-value, then the
1069 value in the varobj and in the target is the same. However, that value
1070 is different from the value that the varobj had after the previous
1071 -var-update. So need to the varobj as changed. */
1078 /* Try to compare the values. That requires that both
1079 values are non-lazy. */
1080 if (var->not_fetched && value_lazy (var->value))
1082 /* This is a frozen varobj and the value was never read.
1083 Presumably, UI shows some "never read" indicator.
1084 Now that we've fetched the real value, we need to report
1085 this varobj as changed so that UI can show the real
1089 else if (var->value == NULL && value == NULL)
1092 else if (var->value == NULL || value == NULL)
1098 gdb_assert (!value_lazy (var->value));
1099 gdb_assert (!value_lazy (value));
1101 gdb_assert (var->print_value != NULL && print_value != NULL);
1102 if (strcmp (var->print_value, print_value) != 0)
1108 /* We must always keep the new value, since children depend on it. */
1109 if (var->value != NULL && var->value != value)
1110 value_free (var->value);
1112 if (var->print_value)
1113 xfree (var->print_value);
1114 var->print_value = print_value;
1115 if (value && value_lazy (value) && intentionally_not_fetched)
1116 var->not_fetched = 1;
1118 var->not_fetched = 0;
1121 gdb_assert (!var->value || value_type (var->value));
1126 /* Update the values for a variable and its children. This is a
1127 two-pronged attack. First, re-parse the value for the root's
1128 expression to see if it's changed. Then go all the way
1129 through its children, reconstructing them and noting if they've
1132 The EXPLICIT parameter specifies if this call is result
1133 of MI request to update this specific variable, or
1134 result of implicit -var-update *. For implicit request, we don't
1135 update frozen variables.
1137 NOTE: This function may delete the caller's varobj. If it
1138 returns TYPE_CHANGED, then it has done this and VARP will be modified
1139 to point to the new varobj. */
1141 VEC(varobj_update_result) *varobj_update (struct varobj **varp, int explicit)
1144 int type_changed = 0;
1149 struct varobj **templist = NULL;
1151 VEC (varobj_p) *stack = NULL;
1152 VEC (varobj_update_result) *result = NULL;
1153 struct frame_info *fi;
1155 /* Frozen means frozen -- we don't check for any change in
1156 this varobj, including its going out of scope, or
1157 changing type. One use case for frozen varobjs is
1158 retaining previously evaluated expressions, and we don't
1159 want them to be reevaluated at all. */
1160 if (!explicit && (*varp)->frozen)
1163 if (!(*varp)->root->is_valid)
1165 varobj_update_result r = {*varp};
1166 r.status = VAROBJ_INVALID;
1167 VEC_safe_push (varobj_update_result, result, &r);
1171 if ((*varp)->root->rootvar == *varp)
1173 varobj_update_result r = {*varp};
1174 r.status = VAROBJ_IN_SCOPE;
1176 /* Update the root variable. value_of_root can return NULL
1177 if the variable is no longer around, i.e. we stepped out of
1178 the frame in which a local existed. We are letting the
1179 value_of_root variable dispose of the varobj if the type
1181 new = value_of_root (varp, &type_changed);
1184 r.type_changed = type_changed;
1185 if (install_new_value ((*varp), new, type_changed))
1189 r.status = VAROBJ_NOT_IN_SCOPE;
1191 if (r.type_changed || r.changed)
1192 VEC_safe_push (varobj_update_result, result, &r);
1194 if (r.status == VAROBJ_NOT_IN_SCOPE)
1198 VEC_safe_push (varobj_p, stack, *varp);
1200 /* Walk through the children, reconstructing them all. */
1201 while (!VEC_empty (varobj_p, stack))
1203 v = VEC_pop (varobj_p, stack);
1205 /* Push any children. Use reverse order so that the first
1206 child is popped from the work stack first, and so
1207 will be added to result first. This does not
1208 affect correctness, just "nicer". */
1209 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
1211 varobj_p c = VEC_index (varobj_p, v->children, i);
1212 /* Child may be NULL if explicitly deleted by -var-delete. */
1213 if (c != NULL && !c->frozen)
1214 VEC_safe_push (varobj_p, stack, c);
1217 /* Update this variable, unless it's a root, which is already
1219 if (v->root->rootvar != v)
1221 new = value_of_child (v->parent, v->index);
1222 if (install_new_value (v, new, 0 /* type not changed */))
1224 /* Note that it's changed */
1225 varobj_update_result r = {v};
1227 VEC_safe_push (varobj_update_result, result, &r);
1233 VEC_free (varobj_p, stack);
1238 /* Helper functions */
1241 * Variable object construction/destruction
1245 delete_variable (struct cpstack **resultp, struct varobj *var,
1246 int only_children_p)
1250 delete_variable_1 (resultp, &delcount, var,
1251 only_children_p, 1 /* remove_from_parent_p */ );
1256 /* Delete the variable object VAR and its children */
1257 /* IMPORTANT NOTE: If we delete a variable which is a child
1258 and the parent is not removed we dump core. It must be always
1259 initially called with remove_from_parent_p set */
1261 delete_variable_1 (struct cpstack **resultp, int *delcountp,
1262 struct varobj *var, int only_children_p,
1263 int remove_from_parent_p)
1267 /* Delete any children of this variable, too. */
1268 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
1270 varobj_p child = VEC_index (varobj_p, var->children, i);
1273 if (!remove_from_parent_p)
1274 child->parent = NULL;
1275 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
1277 VEC_free (varobj_p, var->children);
1279 /* if we were called to delete only the children we are done here */
1280 if (only_children_p)
1283 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1284 /* If the name is null, this is a temporary variable, that has not
1285 yet been installed, don't report it, it belongs to the caller... */
1286 if (var->obj_name != NULL)
1288 cppush (resultp, xstrdup (var->obj_name));
1289 *delcountp = *delcountp + 1;
1292 /* If this variable has a parent, remove it from its parent's list */
1293 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1294 (as indicated by remove_from_parent_p) we don't bother doing an
1295 expensive list search to find the element to remove when we are
1296 discarding the list afterwards */
1297 if ((remove_from_parent_p) && (var->parent != NULL))
1299 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
1302 if (var->obj_name != NULL)
1303 uninstall_variable (var);
1305 /* Free memory associated with this variable */
1306 free_variable (var);
1309 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1311 install_variable (struct varobj *var)
1314 struct vlist *newvl;
1316 unsigned int index = 0;
1319 for (chp = var->obj_name; *chp; chp++)
1321 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1324 cv = *(varobj_table + index);
1325 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1329 error (_("Duplicate variable object name"));
1331 /* Add varobj to hash table */
1332 newvl = xmalloc (sizeof (struct vlist));
1333 newvl->next = *(varobj_table + index);
1335 *(varobj_table + index) = newvl;
1337 /* If root, add varobj to root list */
1338 if (is_root_p (var))
1340 /* Add to list of root variables */
1341 if (rootlist == NULL)
1342 var->root->next = NULL;
1344 var->root->next = rootlist;
1345 rootlist = var->root;
1352 /* Unistall the object VAR. */
1354 uninstall_variable (struct varobj *var)
1358 struct varobj_root *cr;
1359 struct varobj_root *prer;
1361 unsigned int index = 0;
1364 /* Remove varobj from hash table */
1365 for (chp = var->obj_name; *chp; chp++)
1367 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1370 cv = *(varobj_table + index);
1372 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1379 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1384 ("Assertion failed: Could not find variable object \"%s\" to delete",
1390 *(varobj_table + index) = cv->next;
1392 prev->next = cv->next;
1396 /* If root, remove varobj from root list */
1397 if (is_root_p (var))
1399 /* Remove from list of root variables */
1400 if (rootlist == var->root)
1401 rootlist = var->root->next;
1406 while ((cr != NULL) && (cr->rootvar != var))
1414 ("Assertion failed: Could not find varobj \"%s\" in root list",
1421 prer->next = cr->next;
1428 /* Create and install a child of the parent of the given name */
1429 static struct varobj *
1430 create_child (struct varobj *parent, int index, char *name)
1432 struct varobj *child;
1434 struct value *value;
1436 child = new_variable ();
1438 /* name is allocated by name_of_child */
1440 child->index = index;
1441 value = value_of_child (parent, index);
1442 child->parent = parent;
1443 child->root = parent->root;
1444 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
1445 child->obj_name = childs_name;
1446 install_variable (child);
1448 /* Compute the type of the child. Must do this before
1449 calling install_new_value. */
1451 /* If the child had no evaluation errors, var->value
1452 will be non-NULL and contain a valid type. */
1453 child->type = value_type (value);
1455 /* Otherwise, we must compute the type. */
1456 child->type = (*child->root->lang->type_of_child) (child->parent,
1458 install_new_value (child, value, 1);
1465 * Miscellaneous utility functions.
1468 /* Allocate memory and initialize a new variable */
1469 static struct varobj *
1474 var = (struct varobj *) xmalloc (sizeof (struct varobj));
1476 var->path_expr = NULL;
1477 var->obj_name = NULL;
1481 var->num_children = -1;
1483 var->children = NULL;
1487 var->print_value = NULL;
1489 var->not_fetched = 0;
1494 /* Allocate memory and initialize a new root variable */
1495 static struct varobj *
1496 new_root_variable (void)
1498 struct varobj *var = new_variable ();
1499 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1500 var->root->lang = NULL;
1501 var->root->exp = NULL;
1502 var->root->valid_block = NULL;
1503 var->root->frame = null_frame_id;
1504 var->root->floating = 0;
1505 var->root->rootvar = NULL;
1506 var->root->is_valid = 1;
1511 /* Free any allocated memory associated with VAR. */
1513 free_variable (struct varobj *var)
1515 value_free (var->value);
1517 /* Free the expression if this is a root variable. */
1518 if (is_root_p (var))
1520 free_current_contents (&var->root->exp);
1525 xfree (var->obj_name);
1526 xfree (var->print_value);
1527 xfree (var->path_expr);
1532 do_free_variable_cleanup (void *var)
1534 free_variable (var);
1537 static struct cleanup *
1538 make_cleanup_free_variable (struct varobj *var)
1540 return make_cleanup (do_free_variable_cleanup, var);
1543 /* This returns the type of the variable. It also skips past typedefs
1544 to return the real type of the variable.
1546 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1547 except within get_target_type and get_type. */
1548 static struct type *
1549 get_type (struct varobj *var)
1555 type = check_typedef (type);
1560 /* Return the type of the value that's stored in VAR,
1561 or that would have being stored there if the
1562 value were accessible.
1564 This differs from VAR->type in that VAR->type is always
1565 the true type of the expession in the source language.
1566 The return value of this function is the type we're
1567 actually storing in varobj, and using for displaying
1568 the values and for comparing previous and new values.
1570 For example, top-level references are always stripped. */
1571 static struct type *
1572 get_value_type (struct varobj *var)
1577 type = value_type (var->value);
1581 type = check_typedef (type);
1583 if (TYPE_CODE (type) == TYPE_CODE_REF)
1584 type = get_target_type (type);
1586 type = check_typedef (type);
1591 /* This returns the target type (or NULL) of TYPE, also skipping
1592 past typedefs, just like get_type ().
1594 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1595 except within get_target_type and get_type. */
1596 static struct type *
1597 get_target_type (struct type *type)
1601 type = TYPE_TARGET_TYPE (type);
1603 type = check_typedef (type);
1609 /* What is the default display for this variable? We assume that
1610 everything is "natural". Any exceptions? */
1611 static enum varobj_display_formats
1612 variable_default_display (struct varobj *var)
1614 return FORMAT_NATURAL;
1617 /* FIXME: The following should be generic for any pointer */
1619 cppush (struct cpstack **pstack, char *name)
1623 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1629 /* FIXME: The following should be generic for any pointer */
1631 cppop (struct cpstack **pstack)
1636 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1641 *pstack = (*pstack)->next;
1648 * Language-dependencies
1651 /* Common entry points */
1653 /* Get the language of variable VAR. */
1654 static enum varobj_languages
1655 variable_language (struct varobj *var)
1657 enum varobj_languages lang;
1659 switch (var->root->exp->language_defn->la_language)
1665 case language_cplus:
1676 /* Return the number of children for a given variable.
1677 The result of this function is defined by the language
1678 implementation. The number of children returned by this function
1679 is the number of children that the user will see in the variable
1682 number_of_children (struct varobj *var)
1684 return (*var->root->lang->number_of_children) (var);;
1687 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1689 name_of_variable (struct varobj *var)
1691 return (*var->root->lang->name_of_variable) (var);
1694 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1696 name_of_child (struct varobj *var, int index)
1698 return (*var->root->lang->name_of_child) (var, index);
1701 /* What is the ``struct value *'' of the root variable VAR?
1702 For floating variable object, evaluation can get us a value
1703 of different type from what is stored in varobj already. In
1705 - *type_changed will be set to 1
1706 - old varobj will be freed, and new one will be
1707 created, with the same name.
1708 - *var_handle will be set to the new varobj
1709 Otherwise, *type_changed will be set to 0. */
1710 static struct value *
1711 value_of_root (struct varobj **var_handle, int *type_changed)
1715 if (var_handle == NULL)
1720 /* This should really be an exception, since this should
1721 only get called with a root variable. */
1723 if (!is_root_p (var))
1726 if (var->root->floating)
1728 struct varobj *tmp_var;
1729 char *old_type, *new_type;
1731 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1732 USE_SELECTED_FRAME);
1733 if (tmp_var == NULL)
1737 old_type = varobj_get_type (var);
1738 new_type = varobj_get_type (tmp_var);
1739 if (strcmp (old_type, new_type) == 0)
1741 /* The expression presently stored inside var->root->exp
1742 remembers the locations of local variables relatively to
1743 the frame where the expression was created (in DWARF location
1744 button, for example). Naturally, those locations are not
1745 correct in other frames, so update the expression. */
1747 struct expression *tmp_exp = var->root->exp;
1748 var->root->exp = tmp_var->root->exp;
1749 tmp_var->root->exp = tmp_exp;
1751 varobj_delete (tmp_var, NULL, 0);
1757 savestring (var->obj_name, strlen (var->obj_name));
1758 varobj_delete (var, NULL, 0);
1760 install_variable (tmp_var);
1761 *var_handle = tmp_var;
1773 return (*var->root->lang->value_of_root) (var_handle);
1776 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1777 static struct value *
1778 value_of_child (struct varobj *parent, int index)
1780 struct value *value;
1782 value = (*parent->root->lang->value_of_child) (parent, index);
1787 /* GDB already has a command called "value_of_variable". Sigh. */
1789 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
1791 if (var->root->is_valid)
1792 return (*var->root->lang->value_of_variable) (var, format);
1798 value_get_print_value (struct value *value, enum varobj_display_formats format)
1801 struct ui_file *stb;
1802 struct cleanup *old_chain;
1804 struct value_print_options opts;
1809 stb = mem_fileopen ();
1810 old_chain = make_cleanup_ui_file_delete (stb);
1812 get_formatted_print_options (&opts, format_code[(int) format]);
1814 common_val_print (value, stb, 0, &opts, current_language);
1815 thevalue = ui_file_xstrdup (stb, &dummy);
1817 do_cleanups (old_chain);
1822 varobj_editable_p (struct varobj *var)
1825 struct value *value;
1827 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
1830 type = get_value_type (var);
1832 switch (TYPE_CODE (type))
1834 case TYPE_CODE_STRUCT:
1835 case TYPE_CODE_UNION:
1836 case TYPE_CODE_ARRAY:
1837 case TYPE_CODE_FUNC:
1838 case TYPE_CODE_METHOD:
1848 /* Return non-zero if changes in value of VAR
1849 must be detected and reported by -var-update.
1850 Return zero is -var-update should never report
1851 changes of such values. This makes sense for structures
1852 (since the changes in children values will be reported separately),
1853 or for artifical objects (like 'public' pseudo-field in C++).
1855 Return value of 0 means that gdb need not call value_fetch_lazy
1856 for the value of this variable object. */
1858 varobj_value_is_changeable_p (struct varobj *var)
1863 if (CPLUS_FAKE_CHILD (var))
1866 type = get_value_type (var);
1868 switch (TYPE_CODE (type))
1870 case TYPE_CODE_STRUCT:
1871 case TYPE_CODE_UNION:
1872 case TYPE_CODE_ARRAY:
1883 /* Return 1 if that varobj is floating, that is is always evaluated in the
1884 selected frame, and not bound to thread/frame. Such variable objects
1885 are created using '@' as frame specifier to -var-create. */
1887 varobj_floating_p (struct varobj *var)
1889 return var->root->floating;
1892 /* Given the value and the type of a variable object,
1893 adjust the value and type to those necessary
1894 for getting children of the variable object.
1895 This includes dereferencing top-level references
1896 to all types and dereferencing pointers to
1899 Both TYPE and *TYPE should be non-null. VALUE
1900 can be null if we want to only translate type.
1901 *VALUE can be null as well -- if the parent
1904 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
1905 depending on whether pointer was deferenced
1906 in this function. */
1908 adjust_value_for_child_access (struct value **value,
1912 gdb_assert (type && *type);
1917 *type = check_typedef (*type);
1919 /* The type of value stored in varobj, that is passed
1920 to us, is already supposed to be
1921 reference-stripped. */
1923 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
1925 /* Pointers to structures are treated just like
1926 structures when accessing children. Don't
1927 dererences pointers to other types. */
1928 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
1930 struct type *target_type = get_target_type (*type);
1931 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
1932 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
1934 if (value && *value)
1936 int success = gdb_value_ind (*value, value);
1940 *type = target_type;
1946 /* The 'get_target_type' function calls check_typedef on
1947 result, so we can immediately check type code. No
1948 need to call check_typedef here. */
1953 c_number_of_children (struct varobj *var)
1955 struct type *type = get_value_type (var);
1957 struct type *target;
1959 adjust_value_for_child_access (NULL, &type, NULL);
1960 target = get_target_type (type);
1962 switch (TYPE_CODE (type))
1964 case TYPE_CODE_ARRAY:
1965 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
1966 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
1967 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1969 /* If we don't know how many elements there are, don't display
1974 case TYPE_CODE_STRUCT:
1975 case TYPE_CODE_UNION:
1976 children = TYPE_NFIELDS (type);
1980 /* The type here is a pointer to non-struct. Typically, pointers
1981 have one child, except for function ptrs, which have no children,
1982 and except for void*, as we don't know what to show.
1984 We can show char* so we allow it to be dereferenced. If you decide
1985 to test for it, please mind that a little magic is necessary to
1986 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1987 TYPE_NAME == "char" */
1988 if (TYPE_CODE (target) == TYPE_CODE_FUNC
1989 || TYPE_CODE (target) == TYPE_CODE_VOID)
1996 /* Other types have no children */
2004 c_name_of_variable (struct varobj *parent)
2006 return savestring (parent->name, strlen (parent->name));
2009 /* Return the value of element TYPE_INDEX of a structure
2010 value VALUE. VALUE's type should be a structure,
2011 or union, or a typedef to struct/union.
2013 Returns NULL if getting the value fails. Never throws. */
2014 static struct value *
2015 value_struct_element_index (struct value *value, int type_index)
2017 struct value *result = NULL;
2018 volatile struct gdb_exception e;
2020 struct type *type = value_type (value);
2021 type = check_typedef (type);
2023 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
2024 || TYPE_CODE (type) == TYPE_CODE_UNION);
2026 TRY_CATCH (e, RETURN_MASK_ERROR)
2028 if (field_is_static (&TYPE_FIELD (type, type_index)))
2029 result = value_static_field (type, type_index);
2031 result = value_primitive_field (value, 0, type_index, type);
2043 /* Obtain the information about child INDEX of the variable
2045 If CNAME is not null, sets *CNAME to the name of the child relative
2047 If CVALUE is not null, sets *CVALUE to the value of the child.
2048 If CTYPE is not null, sets *CTYPE to the type of the child.
2050 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
2051 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
2054 c_describe_child (struct varobj *parent, int index,
2055 char **cname, struct value **cvalue, struct type **ctype,
2056 char **cfull_expression)
2058 struct value *value = parent->value;
2059 struct type *type = get_value_type (parent);
2060 char *parent_expression = NULL;
2069 if (cfull_expression)
2071 *cfull_expression = NULL;
2072 parent_expression = varobj_get_path_expr (parent);
2074 adjust_value_for_child_access (&value, &type, &was_ptr);
2076 switch (TYPE_CODE (type))
2078 case TYPE_CODE_ARRAY:
2080 *cname = xstrprintf ("%d", index
2081 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2083 if (cvalue && value)
2085 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2086 struct value *indval =
2087 value_from_longest (builtin_type_int32, (LONGEST) real_index);
2088 gdb_value_subscript (value, indval, cvalue);
2092 *ctype = get_target_type (type);
2094 if (cfull_expression)
2095 *cfull_expression = xstrprintf ("(%s)[%d]", parent_expression,
2097 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2102 case TYPE_CODE_STRUCT:
2103 case TYPE_CODE_UNION:
2106 char *string = TYPE_FIELD_NAME (type, index);
2107 *cname = savestring (string, strlen (string));
2110 if (cvalue && value)
2112 /* For C, varobj index is the same as type index. */
2113 *cvalue = value_struct_element_index (value, index);
2117 *ctype = TYPE_FIELD_TYPE (type, index);
2119 if (cfull_expression)
2121 char *join = was_ptr ? "->" : ".";
2122 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, join,
2123 TYPE_FIELD_NAME (type, index));
2130 *cname = xstrprintf ("*%s", parent->name);
2132 if (cvalue && value)
2134 int success = gdb_value_ind (value, cvalue);
2139 /* Don't use get_target_type because it calls
2140 check_typedef and here, we want to show the true
2141 declared type of the variable. */
2143 *ctype = TYPE_TARGET_TYPE (type);
2145 if (cfull_expression)
2146 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
2151 /* This should not happen */
2153 *cname = xstrdup ("???");
2154 if (cfull_expression)
2155 *cfull_expression = xstrdup ("???");
2156 /* Don't set value and type, we don't know then. */
2161 c_name_of_child (struct varobj *parent, int index)
2164 c_describe_child (parent, index, &name, NULL, NULL, NULL);
2169 c_path_expr_of_child (struct varobj *child)
2171 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
2173 return child->path_expr;
2176 /* If frame associated with VAR can be found, switch
2177 to it and return 1. Otherwise, return 0. */
2179 check_scope (struct varobj *var)
2181 struct frame_info *fi;
2184 fi = frame_find_by_id (var->root->frame);
2189 CORE_ADDR pc = get_frame_pc (fi);
2190 if (pc < BLOCK_START (var->root->valid_block) ||
2191 pc >= BLOCK_END (var->root->valid_block))
2199 static struct value *
2200 c_value_of_root (struct varobj **var_handle)
2202 struct value *new_val = NULL;
2203 struct varobj *var = *var_handle;
2204 struct frame_info *fi;
2205 int within_scope = 0;
2206 struct cleanup *back_to;
2208 /* Only root variables can be updated... */
2209 if (!is_root_p (var))
2210 /* Not a root var */
2213 back_to = make_cleanup_restore_current_thread ();
2215 /* Determine whether the variable is still around. */
2216 if (var->root->valid_block == NULL || var->root->floating)
2218 else if (var->root->thread_id == 0)
2220 /* The program was single-threaded when the variable object was
2221 created. Technically, it's possible that the program became
2222 multi-threaded since then, but we don't support such
2224 within_scope = check_scope (var);
2228 ptid_t ptid = thread_id_to_pid (var->root->thread_id);
2229 if (in_thread_list (ptid))
2231 switch_to_thread (ptid);
2232 within_scope = check_scope (var);
2238 /* We need to catch errors here, because if evaluate
2239 expression fails we want to just return NULL. */
2240 gdb_evaluate_expression (var->root->exp, &new_val);
2244 do_cleanups (back_to);
2249 static struct value *
2250 c_value_of_child (struct varobj *parent, int index)
2252 struct value *value = NULL;
2253 c_describe_child (parent, index, NULL, &value, NULL, NULL);
2258 static struct type *
2259 c_type_of_child (struct varobj *parent, int index)
2261 struct type *type = NULL;
2262 c_describe_child (parent, index, NULL, NULL, &type, NULL);
2267 c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2269 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2270 it will print out its children instead of "{...}". So we need to
2271 catch that case explicitly. */
2272 struct type *type = get_type (var);
2274 /* Strip top-level references. */
2275 while (TYPE_CODE (type) == TYPE_CODE_REF)
2276 type = check_typedef (TYPE_TARGET_TYPE (type));
2278 switch (TYPE_CODE (type))
2280 case TYPE_CODE_STRUCT:
2281 case TYPE_CODE_UNION:
2282 return xstrdup ("{...}");
2285 case TYPE_CODE_ARRAY:
2288 number = xstrprintf ("[%d]", var->num_children);
2295 if (var->value == NULL)
2297 /* This can happen if we attempt to get the value of a struct
2298 member when the parent is an invalid pointer. This is an
2299 error condition, so we should tell the caller. */
2304 if (var->not_fetched && value_lazy (var->value))
2305 /* Frozen variable and no value yet. We don't
2306 implicitly fetch the value. MI response will
2307 use empty string for the value, which is OK. */
2310 gdb_assert (varobj_value_is_changeable_p (var));
2311 gdb_assert (!value_lazy (var->value));
2313 /* If the specified format is the current one,
2314 we can reuse print_value */
2315 if (format == var->format)
2316 return xstrdup (var->print_value);
2318 return value_get_print_value (var->value, format);
2328 cplus_number_of_children (struct varobj *var)
2331 int children, dont_know;
2336 if (!CPLUS_FAKE_CHILD (var))
2338 type = get_value_type (var);
2339 adjust_value_for_child_access (NULL, &type, NULL);
2341 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2342 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2346 cplus_class_num_children (type, kids);
2347 if (kids[v_public] != 0)
2349 if (kids[v_private] != 0)
2351 if (kids[v_protected] != 0)
2354 /* Add any baseclasses */
2355 children += TYPE_N_BASECLASSES (type);
2358 /* FIXME: save children in var */
2365 type = get_value_type (var->parent);
2366 adjust_value_for_child_access (NULL, &type, NULL);
2368 cplus_class_num_children (type, kids);
2369 if (strcmp (var->name, "public") == 0)
2370 children = kids[v_public];
2371 else if (strcmp (var->name, "private") == 0)
2372 children = kids[v_private];
2374 children = kids[v_protected];
2379 children = c_number_of_children (var);
2384 /* Compute # of public, private, and protected variables in this class.
2385 That means we need to descend into all baseclasses and find out
2386 how many are there, too. */
2388 cplus_class_num_children (struct type *type, int children[3])
2392 children[v_public] = 0;
2393 children[v_private] = 0;
2394 children[v_protected] = 0;
2396 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2398 /* If we have a virtual table pointer, omit it. */
2399 if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
2402 if (TYPE_FIELD_PROTECTED (type, i))
2403 children[v_protected]++;
2404 else if (TYPE_FIELD_PRIVATE (type, i))
2405 children[v_private]++;
2407 children[v_public]++;
2412 cplus_name_of_variable (struct varobj *parent)
2414 return c_name_of_variable (parent);
2417 enum accessibility { private_field, protected_field, public_field };
2419 /* Check if field INDEX of TYPE has the specified accessibility.
2420 Return 0 if so and 1 otherwise. */
2422 match_accessibility (struct type *type, int index, enum accessibility acc)
2424 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2426 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2428 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2429 && !TYPE_FIELD_PROTECTED (type, index))
2436 cplus_describe_child (struct varobj *parent, int index,
2437 char **cname, struct value **cvalue, struct type **ctype,
2438 char **cfull_expression)
2441 struct value *value;
2444 char *parent_expression = NULL;
2452 if (cfull_expression)
2453 *cfull_expression = NULL;
2455 if (CPLUS_FAKE_CHILD (parent))
2457 value = parent->parent->value;
2458 type = get_value_type (parent->parent);
2459 if (cfull_expression)
2460 parent_expression = varobj_get_path_expr (parent->parent);
2464 value = parent->value;
2465 type = get_value_type (parent);
2466 if (cfull_expression)
2467 parent_expression = varobj_get_path_expr (parent);
2470 adjust_value_for_child_access (&value, &type, &was_ptr);
2472 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2473 || TYPE_CODE (type) == TYPE_CODE_UNION)
2475 char *join = was_ptr ? "->" : ".";
2476 if (CPLUS_FAKE_CHILD (parent))
2478 /* The fields of the class type are ordered as they
2479 appear in the class. We are given an index for a
2480 particular access control type ("public","protected",
2481 or "private"). We must skip over fields that don't
2482 have the access control we are looking for to properly
2483 find the indexed field. */
2484 int type_index = TYPE_N_BASECLASSES (type);
2485 enum accessibility acc = public_field;
2486 if (strcmp (parent->name, "private") == 0)
2487 acc = private_field;
2488 else if (strcmp (parent->name, "protected") == 0)
2489 acc = protected_field;
2493 if (TYPE_VPTR_BASETYPE (type) == type
2494 && type_index == TYPE_VPTR_FIELDNO (type))
2496 else if (match_accessibility (type, type_index, acc))
2503 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2505 if (cvalue && value)
2506 *cvalue = value_struct_element_index (value, type_index);
2509 *ctype = TYPE_FIELD_TYPE (type, type_index);
2511 if (cfull_expression)
2512 *cfull_expression = xstrprintf ("((%s)%s%s)", parent_expression,
2514 TYPE_FIELD_NAME (type, type_index));
2516 else if (index < TYPE_N_BASECLASSES (type))
2518 /* This is a baseclass. */
2520 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2522 if (cvalue && value)
2524 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
2525 release_value (*cvalue);
2530 *ctype = TYPE_FIELD_TYPE (type, index);
2533 if (cfull_expression)
2535 char *ptr = was_ptr ? "*" : "";
2536 /* Cast the parent to the base' type. Note that in gdb,
2539 will create an lvalue, for all appearences, so we don't
2540 need to use more fancy:
2543 *cfull_expression = xstrprintf ("(%s(%s%s) %s)",
2545 TYPE_FIELD_NAME (type, index),
2552 char *access = NULL;
2554 cplus_class_num_children (type, children);
2556 /* Everything beyond the baseclasses can
2557 only be "public", "private", or "protected"
2559 The special "fake" children are always output by varobj in
2560 this order. So if INDEX == 2, it MUST be "protected". */
2561 index -= TYPE_N_BASECLASSES (type);
2565 if (children[v_public] > 0)
2567 else if (children[v_private] > 0)
2570 access = "protected";
2573 if (children[v_public] > 0)
2575 if (children[v_private] > 0)
2578 access = "protected";
2580 else if (children[v_private] > 0)
2581 access = "protected";
2584 /* Must be protected */
2585 access = "protected";
2592 gdb_assert (access);
2594 *cname = xstrdup (access);
2596 /* Value and type and full expression are null here. */
2601 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
2606 cplus_name_of_child (struct varobj *parent, int index)
2609 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
2614 cplus_path_expr_of_child (struct varobj *child)
2616 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
2618 return child->path_expr;
2621 static struct value *
2622 cplus_value_of_root (struct varobj **var_handle)
2624 return c_value_of_root (var_handle);
2627 static struct value *
2628 cplus_value_of_child (struct varobj *parent, int index)
2630 struct value *value = NULL;
2631 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
2635 static struct type *
2636 cplus_type_of_child (struct varobj *parent, int index)
2638 struct type *type = NULL;
2639 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
2644 cplus_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2647 /* If we have one of our special types, don't print out
2649 if (CPLUS_FAKE_CHILD (var))
2650 return xstrdup ("");
2652 return c_value_of_variable (var, format);
2658 java_number_of_children (struct varobj *var)
2660 return cplus_number_of_children (var);
2664 java_name_of_variable (struct varobj *parent)
2668 name = cplus_name_of_variable (parent);
2669 /* If the name has "-" in it, it is because we
2670 needed to escape periods in the name... */
2673 while (*p != '\000')
2684 java_name_of_child (struct varobj *parent, int index)
2688 name = cplus_name_of_child (parent, index);
2689 /* Escape any periods in the name... */
2692 while (*p != '\000')
2703 java_path_expr_of_child (struct varobj *child)
2708 static struct value *
2709 java_value_of_root (struct varobj **var_handle)
2711 return cplus_value_of_root (var_handle);
2714 static struct value *
2715 java_value_of_child (struct varobj *parent, int index)
2717 return cplus_value_of_child (parent, index);
2720 static struct type *
2721 java_type_of_child (struct varobj *parent, int index)
2723 return cplus_type_of_child (parent, index);
2727 java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2729 return cplus_value_of_variable (var, format);
2732 extern void _initialize_varobj (void);
2734 _initialize_varobj (void)
2736 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2738 varobj_table = xmalloc (sizeof_table);
2739 memset (varobj_table, 0, sizeof_table);
2741 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2743 Set varobj debugging."), _("\
2744 Show varobj debugging."), _("\
2745 When non-zero, varobj debugging is enabled."),
2748 &setlist, &showlist);
2751 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2752 are defined on globals.
2753 Invalidated varobjs will be always printed in_scope="invalid". */
2755 varobj_invalidate (void)
2757 struct varobj **all_rootvarobj;
2758 struct varobj **varp;
2760 if (varobj_list (&all_rootvarobj) > 0)
2762 varp = all_rootvarobj;
2763 while (*varp != NULL)
2765 /* Floating varobjs are reparsed on each stop, so we don't care if
2766 the presently parsed expression refers to something that's gone. */
2767 if ((*varp)->root->floating)
2770 /* global var must be re-evaluated. */
2771 if ((*varp)->root->valid_block == NULL)
2773 struct varobj *tmp_var;
2775 /* Try to create a varobj with same expression. If we succeed replace
2776 the old varobj, otherwise invalidate it. */
2777 tmp_var = varobj_create (NULL, (*varp)->name, (CORE_ADDR) 0, USE_CURRENT_FRAME);
2778 if (tmp_var != NULL)
2780 tmp_var->obj_name = xstrdup ((*varp)->obj_name);
2781 varobj_delete (*varp, NULL, 0);
2782 install_variable (tmp_var);
2785 (*varp)->root->is_valid = 0;
2787 else /* locals must be invalidated. */
2788 (*varp)->root->is_valid = 0;
2793 xfree (all_rootvarobj);