1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 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"
29 #include "gdb_assert.h"
30 #include "gdb_string.h"
34 #include "gdbthread.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. This field is set iff valid_block is
69 struct frame_id frame
;
71 /* The thread ID that this varobj_root belong to. This field
72 is only valid if valid_block is not NULL.
73 When not 0, indicates which thread 'frame' belongs to.
74 When 0, indicates that the thread list was empty when the varobj_root
78 /* If 1, the -var-update always recomputes the value in the
79 current thread and frame. Otherwise, variable object is
80 always updated in the specific scope/thread/frame */
83 /* Flag that indicates validity: set to 0 when this varobj_root refers
84 to symbols that do not exist anymore. */
87 /* Language info for this variable and its children */
88 struct language_specific
*lang
;
90 /* The varobj for this root node. */
91 struct varobj
*rootvar
;
93 /* Next root variable */
94 struct varobj_root
*next
;
97 /* Every variable in the system has a structure of this type defined
98 for it. This structure holds all information necessary to manipulate
99 a particular object variable. Members which must be freed are noted. */
103 /* Alloc'd name of the variable for this object.. If this variable is a
104 child, then this name will be the child's source name.
105 (bar, not foo.bar) */
106 /* NOTE: This is the "expression" */
109 /* Alloc'd expression for this child. Can be used to create a
110 root variable corresponding to this child. */
113 /* The alloc'd name for this variable's object. This is here for
114 convenience when constructing this object's children. */
117 /* Index of this variable in its parent or -1 */
120 /* The type of this variable. This can be NULL
121 for artifial variable objects -- currently, the "accessibility"
122 variable objects in C++. */
125 /* The value of this expression or subexpression. A NULL value
126 indicates there was an error getting this value.
127 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
128 the value is either NULL, or not lazy. */
131 /* The number of (immediate) children this variable has */
134 /* If this object is a child, this points to its immediate parent. */
135 struct varobj
*parent
;
137 /* Children of this object. */
138 VEC (varobj_p
) *children
;
140 /* Description of the root variable. Points to root variable for children. */
141 struct varobj_root
*root
;
143 /* The format of the output for this object */
144 enum varobj_display_formats format
;
146 /* Was this variable updated via a varobj_set_value operation */
149 /* Last print value. */
152 /* Is this variable frozen. Frozen variables are never implicitly
153 updated by -var-update *
154 or -var-update <direct-or-indirect-parent>. */
157 /* Is the value of this variable intentionally not fetched? It is
158 not fetched if either the variable is frozen, or any parents is
166 struct cpstack
*next
;
169 /* A list of varobjs */
177 /* Private function prototypes */
179 /* Helper functions for the above subcommands. */
181 static int delete_variable (struct cpstack
**, struct varobj
*, int);
183 static void delete_variable_1 (struct cpstack
**, int *,
184 struct varobj
*, int, int);
186 static int install_variable (struct varobj
*);
188 static void uninstall_variable (struct varobj
*);
190 static struct varobj
*create_child (struct varobj
*, int, char *);
192 /* Utility routines */
194 static struct varobj
*new_variable (void);
196 static struct varobj
*new_root_variable (void);
198 static void free_variable (struct varobj
*var
);
200 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
202 static struct type
*get_type (struct varobj
*var
);
204 static struct type
*get_value_type (struct varobj
*var
);
206 static struct type
*get_target_type (struct type
*);
208 static enum varobj_display_formats
variable_default_display (struct varobj
*);
210 static void cppush (struct cpstack
**pstack
, char *name
);
212 static char *cppop (struct cpstack
**pstack
);
214 static int install_new_value (struct varobj
*var
, struct value
*value
,
217 /* Language-specific routines. */
219 static enum varobj_languages
variable_language (struct varobj
*var
);
221 static int number_of_children (struct varobj
*);
223 static char *name_of_variable (struct varobj
*);
225 static char *name_of_child (struct varobj
*, int);
227 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
229 static struct value
*value_of_child (struct varobj
*parent
, int index
);
231 static char *my_value_of_variable (struct varobj
*var
);
233 static char *value_get_print_value (struct value
*value
,
234 enum varobj_display_formats format
);
236 static int varobj_value_is_changeable_p (struct varobj
*var
);
238 static int is_root_p (struct varobj
*var
);
240 /* C implementation */
242 static int c_number_of_children (struct varobj
*var
);
244 static char *c_name_of_variable (struct varobj
*parent
);
246 static char *c_name_of_child (struct varobj
*parent
, int index
);
248 static char *c_path_expr_of_child (struct varobj
*child
);
250 static struct value
*c_value_of_root (struct varobj
**var_handle
);
252 static struct value
*c_value_of_child (struct varobj
*parent
, int index
);
254 static struct type
*c_type_of_child (struct varobj
*parent
, int index
);
256 static char *c_value_of_variable (struct varobj
*var
);
258 /* C++ implementation */
260 static int cplus_number_of_children (struct varobj
*var
);
262 static void cplus_class_num_children (struct type
*type
, int children
[3]);
264 static char *cplus_name_of_variable (struct varobj
*parent
);
266 static char *cplus_name_of_child (struct varobj
*parent
, int index
);
268 static char *cplus_path_expr_of_child (struct varobj
*child
);
270 static struct value
*cplus_value_of_root (struct varobj
**var_handle
);
272 static struct value
*cplus_value_of_child (struct varobj
*parent
, int index
);
274 static struct type
*cplus_type_of_child (struct varobj
*parent
, int index
);
276 static char *cplus_value_of_variable (struct varobj
*var
);
278 /* Java implementation */
280 static int java_number_of_children (struct varobj
*var
);
282 static char *java_name_of_variable (struct varobj
*parent
);
284 static char *java_name_of_child (struct varobj
*parent
, int index
);
286 static char *java_path_expr_of_child (struct varobj
*child
);
288 static struct value
*java_value_of_root (struct varobj
**var_handle
);
290 static struct value
*java_value_of_child (struct varobj
*parent
, int index
);
292 static struct type
*java_type_of_child (struct varobj
*parent
, int index
);
294 static char *java_value_of_variable (struct varobj
*var
);
296 /* The language specific vector */
298 struct language_specific
301 /* The language of this variable */
302 enum varobj_languages language
;
304 /* The number of children of PARENT. */
305 int (*number_of_children
) (struct varobj
* parent
);
307 /* The name (expression) of a root varobj. */
308 char *(*name_of_variable
) (struct varobj
* parent
);
310 /* The name of the INDEX'th child of PARENT. */
311 char *(*name_of_child
) (struct varobj
* parent
, int index
);
313 /* Returns the rooted expression of CHILD, which is a variable
314 obtain that has some parent. */
315 char *(*path_expr_of_child
) (struct varobj
* child
);
317 /* The ``struct value *'' of the root variable ROOT. */
318 struct value
*(*value_of_root
) (struct varobj
** root_handle
);
320 /* The ``struct value *'' of the INDEX'th child of PARENT. */
321 struct value
*(*value_of_child
) (struct varobj
* parent
, int index
);
323 /* The type of the INDEX'th child of PARENT. */
324 struct type
*(*type_of_child
) (struct varobj
* parent
, int index
);
326 /* The current value of VAR. */
327 char *(*value_of_variable
) (struct varobj
* var
);
330 /* Array of known source language routines. */
331 static struct language_specific languages
[vlang_end
] = {
332 /* Unknown (try treating as C */
335 c_number_of_children
,
338 c_path_expr_of_child
,
347 c_number_of_children
,
350 c_path_expr_of_child
,
359 cplus_number_of_children
,
360 cplus_name_of_variable
,
362 cplus_path_expr_of_child
,
364 cplus_value_of_child
,
366 cplus_value_of_variable
}
371 java_number_of_children
,
372 java_name_of_variable
,
374 java_path_expr_of_child
,
378 java_value_of_variable
}
381 /* A little convenience enum for dealing with C++/Java */
384 v_public
= 0, v_private
, v_protected
389 /* Mappings of varobj_display_formats enums to gdb's format codes */
390 static int format_code
[] = { 0, 't', 'd', 'x', 'o' };
392 /* Header of the list of root variable objects */
393 static struct varobj_root
*rootlist
;
394 static int rootcount
= 0; /* number of root varobjs in the list */
396 /* Prime number indicating the number of buckets in the hash table */
397 /* A prime large enough to avoid too many colisions */
398 #define VAROBJ_TABLE_SIZE 227
400 /* Pointer to the varobj hash table (built at run time) */
401 static struct vlist
**varobj_table
;
403 /* Is the variable X one of our "fake" children? */
404 #define CPLUS_FAKE_CHILD(x) \
405 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
408 /* API Implementation */
410 is_root_p (struct varobj
*var
)
412 return (var
->root
->rootvar
== var
);
415 /* Creates a varobj (not its children) */
417 /* Return the full FRAME which corresponds to the given CORE_ADDR
418 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
420 static struct frame_info
*
421 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
423 struct frame_info
*frame
= NULL
;
425 if (frame_addr
== (CORE_ADDR
) 0)
430 frame
= get_prev_frame (frame
);
433 if (get_frame_base_address (frame
) == frame_addr
)
439 varobj_create (char *objname
,
440 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
443 struct frame_info
*fi
;
444 struct frame_info
*old_fi
= NULL
;
446 struct cleanup
*old_chain
;
448 /* Fill out a varobj structure for the (root) variable being constructed. */
449 var
= new_root_variable ();
450 old_chain
= make_cleanup_free_variable (var
);
452 if (expression
!= NULL
)
455 enum varobj_languages lang
;
456 struct value
*value
= NULL
;
459 /* Parse and evaluate the expression, filling in as much
460 of the variable's data as possible */
462 /* Allow creator to specify context of variable */
463 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
464 fi
= deprecated_safe_get_selected_frame ();
466 /* FIXME: cagney/2002-11-23: This code should be doing a
467 lookup using the frame ID and not just the frame's
468 ``address''. This, of course, means an interface change.
469 However, with out that interface change ISAs, such as the
470 ia64 with its two stacks, won't work. Similar goes for the
471 case where there is a frameless function. */
472 fi
= find_frame_addr_in_frame_chain (frame
);
474 /* frame = -2 means always use selected frame */
475 if (type
== USE_SELECTED_FRAME
)
476 var
->root
->floating
= 1;
480 block
= get_frame_block (fi
, 0);
483 innermost_block
= NULL
;
484 /* Wrap the call to parse expression, so we can
485 return a sensible error. */
486 if (!gdb_parse_exp_1 (&p
, block
, 0, &var
->root
->exp
))
491 /* Don't allow variables to be created for types. */
492 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
)
494 do_cleanups (old_chain
);
495 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
496 " as an expression.\n");
500 var
->format
= variable_default_display (var
);
501 var
->root
->valid_block
= innermost_block
;
502 expr_len
= strlen (expression
);
503 var
->name
= savestring (expression
, expr_len
);
504 /* For a root var, the name and the expr are the same. */
505 var
->path_expr
= savestring (expression
, expr_len
);
507 /* When the frame is different from the current frame,
508 we must select the appropriate frame before parsing
509 the expression, otherwise the value will not be current.
510 Since select_frame is so benign, just call it for all cases. */
511 if (innermost_block
&& fi
!= NULL
)
513 var
->root
->frame
= get_frame_id (fi
);
514 var
->root
->thread_id
= pid_to_thread_id (inferior_ptid
);
515 old_fi
= get_selected_frame (NULL
);
519 /* We definitely need to catch errors here.
520 If evaluate_expression succeeds we got the value we wanted.
521 But if it fails, we still go on with a call to evaluate_type() */
522 if (!gdb_evaluate_expression (var
->root
->exp
, &value
))
524 /* Error getting the value. Try to at least get the
526 struct value
*type_only_value
= evaluate_type (var
->root
->exp
);
527 var
->type
= value_type (type_only_value
);
530 var
->type
= value_type (value
);
532 install_new_value (var
, value
, 1 /* Initial assignment */);
534 /* Set language info */
535 lang
= variable_language (var
);
536 var
->root
->lang
= &languages
[lang
];
538 /* Set ourselves as our root */
539 var
->root
->rootvar
= var
;
541 /* Reset the selected frame */
543 select_frame (old_fi
);
546 /* If the variable object name is null, that means this
547 is a temporary variable, so don't install it. */
549 if ((var
!= NULL
) && (objname
!= NULL
))
551 var
->obj_name
= savestring (objname
, strlen (objname
));
553 /* If a varobj name is duplicated, the install will fail so
555 if (!install_variable (var
))
557 do_cleanups (old_chain
);
562 discard_cleanups (old_chain
);
566 /* Generates an unique name that can be used for a varobj */
569 varobj_gen_name (void)
574 /* generate a name for this object */
576 obj_name
= xstrprintf ("var%d", id
);
581 /* Given an "objname", returns the pointer to the corresponding varobj
582 or NULL if not found */
585 varobj_get_handle (char *objname
)
589 unsigned int index
= 0;
592 for (chp
= objname
; *chp
; chp
++)
594 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
597 cv
= *(varobj_table
+ index
);
598 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
602 error (_("Variable object not found"));
607 /* Given the handle, return the name of the object */
610 varobj_get_objname (struct varobj
*var
)
612 return var
->obj_name
;
615 /* Given the handle, return the expression represented by the object */
618 varobj_get_expression (struct varobj
*var
)
620 return name_of_variable (var
);
623 /* Deletes a varobj and all its children if only_children == 0,
624 otherwise deletes only the children; returns a malloc'ed list of all the
625 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
628 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
632 struct cpstack
*result
= NULL
;
635 /* Initialize a stack for temporary results */
636 cppush (&result
, NULL
);
639 /* Delete only the variable children */
640 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
642 /* Delete the variable and all its children */
643 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
645 /* We may have been asked to return a list of what has been deleted */
648 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
652 *cp
= cppop (&result
);
653 while ((*cp
!= NULL
) && (mycount
> 0))
657 *cp
= cppop (&result
);
660 if (mycount
|| (*cp
!= NULL
))
661 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
668 /* Set/Get variable object display format */
670 enum varobj_display_formats
671 varobj_set_display_format (struct varobj
*var
,
672 enum varobj_display_formats format
)
679 case FORMAT_HEXADECIMAL
:
681 var
->format
= format
;
685 var
->format
= variable_default_display (var
);
688 if (varobj_value_is_changeable_p (var
)
689 && var
->value
&& !value_lazy (var
->value
))
691 free (var
->print_value
);
692 var
->print_value
= value_get_print_value (var
->value
, var
->format
);
698 enum varobj_display_formats
699 varobj_get_display_format (struct varobj
*var
)
704 /* If the variable object is bound to a specific thread, that
705 is its evaluation can always be done in context of a frame
706 inside that thread, returns GDB id of the thread -- which
707 is always positive. Otherwise, returns -1. */
709 varobj_get_thread_id (struct varobj
*var
)
711 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
712 return var
->root
->thread_id
;
718 varobj_set_frozen (struct varobj
*var
, int frozen
)
720 /* When a variable is unfrozen, we don't fetch its value.
721 The 'not_fetched' flag remains set, so next -var-update
724 We don't fetch the value, because for structures the client
725 should do -var-update anyway. It would be bad to have different
726 client-size logic for structure and other types. */
727 var
->frozen
= frozen
;
731 varobj_get_frozen (struct varobj
*var
)
738 varobj_get_num_children (struct varobj
*var
)
740 if (var
->num_children
== -1)
741 var
->num_children
= number_of_children (var
);
743 return var
->num_children
;
746 /* Creates a list of the immediate children of a variable object;
747 the return code is the number of such children or -1 on error */
750 varobj_list_children (struct varobj
*var
)
752 struct varobj
*child
;
756 if (var
->num_children
== -1)
757 var
->num_children
= number_of_children (var
);
759 /* If that failed, give up. */
760 if (var
->num_children
== -1)
761 return var
->children
;
763 /* If we're called when the list of children is not yet initialized,
764 allocate enough elements in it. */
765 while (VEC_length (varobj_p
, var
->children
) < var
->num_children
)
766 VEC_safe_push (varobj_p
, var
->children
, NULL
);
768 for (i
= 0; i
< var
->num_children
; i
++)
770 varobj_p existing
= VEC_index (varobj_p
, var
->children
, i
);
772 if (existing
== NULL
)
774 /* Either it's the first call to varobj_list_children for
775 this variable object, and the child was never created,
776 or it was explicitly deleted by the client. */
777 name
= name_of_child (var
, i
);
778 existing
= create_child (var
, i
, name
);
779 VEC_replace (varobj_p
, var
->children
, i
, existing
);
783 return var
->children
;
786 /* Obtain the type of an object Variable as a string similar to the one gdb
787 prints on the console */
790 varobj_get_type (struct varobj
*var
)
793 struct cleanup
*old_chain
;
798 /* For the "fake" variables, do not return a type. (It's type is
800 Do not return a type for invalid variables as well. */
801 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
804 stb
= mem_fileopen ();
805 old_chain
= make_cleanup_ui_file_delete (stb
);
807 /* To print the type, we simply create a zero ``struct value *'' and
808 cast it to our type. We then typeprint this variable. */
809 val
= value_zero (var
->type
, not_lval
);
810 type_print (value_type (val
), "", stb
, -1);
812 thetype
= ui_file_xstrdup (stb
, &length
);
813 do_cleanups (old_chain
);
817 /* Obtain the type of an object variable. */
820 varobj_get_gdb_type (struct varobj
*var
)
825 /* Return a pointer to the full rooted expression of varobj VAR.
826 If it has not been computed yet, compute it. */
828 varobj_get_path_expr (struct varobj
*var
)
830 if (var
->path_expr
!= NULL
)
831 return var
->path_expr
;
834 /* For root varobjs, we initialize path_expr
835 when creating varobj, so here it should be
837 gdb_assert (!is_root_p (var
));
838 return (*var
->root
->lang
->path_expr_of_child
) (var
);
842 enum varobj_languages
843 varobj_get_language (struct varobj
*var
)
845 return variable_language (var
);
849 varobj_get_attributes (struct varobj
*var
)
853 if (varobj_editable_p (var
))
854 /* FIXME: define masks for attributes */
855 attributes
|= 0x00000001; /* Editable */
861 varobj_get_value (struct varobj
*var
)
863 return my_value_of_variable (var
);
866 /* Set the value of an object variable (if it is editable) to the
867 value of the given expression */
868 /* Note: Invokes functions that can call error() */
871 varobj_set_value (struct varobj
*var
, char *expression
)
877 /* The argument "expression" contains the variable's new value.
878 We need to first construct a legal expression for this -- ugh! */
879 /* Does this cover all the bases? */
880 struct expression
*exp
;
882 int saved_input_radix
= input_radix
;
883 char *s
= expression
;
886 gdb_assert (varobj_editable_p (var
));
888 input_radix
= 10; /* ALWAYS reset to decimal temporarily */
889 exp
= parse_exp_1 (&s
, 0, 0);
890 if (!gdb_evaluate_expression (exp
, &value
))
892 /* We cannot proceed without a valid expression. */
897 /* All types that are editable must also be changeable. */
898 gdb_assert (varobj_value_is_changeable_p (var
));
900 /* The value of a changeable variable object must not be lazy. */
901 gdb_assert (!value_lazy (var
->value
));
903 /* Need to coerce the input. We want to check if the
904 value of the variable object will be different
905 after assignment, and the first thing value_assign
906 does is coerce the input.
907 For example, if we are assigning an array to a pointer variable we
908 should compare the pointer with the the array's address, not with the
910 value
= coerce_array (value
);
912 /* The new value may be lazy. gdb_value_assign, or
913 rather value_contents, will take care of this.
914 If fetching of the new value will fail, gdb_value_assign
915 with catch the exception. */
916 if (!gdb_value_assign (var
->value
, value
, &val
))
919 /* If the value has changed, record it, so that next -var-update can
920 report this change. If a variable had a value of '1', we've set it
921 to '333' and then set again to '1', when -var-update will report this
922 variable as changed -- because the first assignment has set the
923 'updated' flag. There's no need to optimize that, because return value
924 of -var-update should be considered an approximation. */
925 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
926 input_radix
= saved_input_radix
;
930 /* Returns a malloc'ed list with all root variable objects */
932 varobj_list (struct varobj
***varlist
)
935 struct varobj_root
*croot
;
936 int mycount
= rootcount
;
938 /* Alloc (rootcount + 1) entries for the result */
939 *varlist
= xmalloc ((rootcount
+ 1) * sizeof (struct varobj
*));
943 while ((croot
!= NULL
) && (mycount
> 0))
945 *cv
= croot
->rootvar
;
950 /* Mark the end of the list */
953 if (mycount
|| (croot
!= NULL
))
955 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
961 /* Assign a new value to a variable object. If INITIAL is non-zero,
962 this is the first assignement after the variable object was just
963 created, or changed type. In that case, just assign the value
965 Otherwise, assign the value and if type_changeable returns non-zero,
966 find if the new value is different from the current value.
967 Return 1 if so, and 0 if the values are equal.
969 The VALUE parameter should not be released -- the function will
970 take care of releasing it when needed. */
972 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
977 int intentionally_not_fetched
= 0;
978 char *print_value
= NULL
;
980 /* We need to know the varobj's type to decide if the value should
981 be fetched or not. C++ fake children (public/protected/private) don't have
983 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
984 changeable
= varobj_value_is_changeable_p (var
);
985 need_to_fetch
= changeable
;
987 /* We are not interested in the address of references, and given
988 that in C++ a reference is not rebindable, it cannot
989 meaningfully change. So, get hold of the real value. */
992 value
= coerce_ref (value
);
993 release_value (value
);
996 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
997 /* For unions, we need to fetch the value implicitly because
998 of implementation of union member fetch. When gdb
999 creates a value for a field and the value of the enclosing
1000 structure is not lazy, it immediately copies the necessary
1001 bytes from the enclosing values. If the enclosing value is
1002 lazy, the call to value_fetch_lazy on the field will read
1003 the data from memory. For unions, that means we'll read the
1004 same memory more than once, which is not desirable. So
1008 /* The new value might be lazy. If the type is changeable,
1009 that is we'll be comparing values of this type, fetch the
1010 value now. Otherwise, on the next update the old value
1011 will be lazy, which means we've lost that old value. */
1012 if (need_to_fetch
&& value
&& value_lazy (value
))
1014 struct varobj
*parent
= var
->parent
;
1015 int frozen
= var
->frozen
;
1016 for (; !frozen
&& parent
; parent
= parent
->parent
)
1017 frozen
|= parent
->frozen
;
1019 if (frozen
&& initial
)
1021 /* For variables that are frozen, or are children of frozen
1022 variables, we don't do fetch on initial assignment.
1023 For non-initial assignemnt we do the fetch, since it means we're
1024 explicitly asked to compare the new value with the old one. */
1025 intentionally_not_fetched
= 1;
1027 else if (!gdb_value_fetch_lazy (value
))
1029 /* Set the value to NULL, so that for the next -var-update,
1030 we don't try to compare the new value with this value,
1031 that we couldn't even read. */
1036 /* Below, we'll be comparing string rendering of old and new
1037 values. Don't get string rendering if the value is
1038 lazy -- if it is, the code above has decided that the value
1039 should not be fetched. */
1040 if (value
&& !value_lazy (value
))
1041 print_value
= value_get_print_value (value
, var
->format
);
1043 /* If the type is changeable, compare the old and the new values.
1044 If this is the initial assignment, we don't have any old value
1046 if (!initial
&& changeable
)
1048 /* If the value of the varobj was changed by -var-set-value, then the
1049 value in the varobj and in the target is the same. However, that value
1050 is different from the value that the varobj had after the previous
1051 -var-update. So need to the varobj as changed. */
1058 /* Try to compare the values. That requires that both
1059 values are non-lazy. */
1060 if (var
->not_fetched
&& value_lazy (var
->value
))
1062 /* This is a frozen varobj and the value was never read.
1063 Presumably, UI shows some "never read" indicator.
1064 Now that we've fetched the real value, we need to report
1065 this varobj as changed so that UI can show the real
1069 else if (var
->value
== NULL
&& value
== NULL
)
1072 else if (var
->value
== NULL
|| value
== NULL
)
1078 gdb_assert (!value_lazy (var
->value
));
1079 gdb_assert (!value_lazy (value
));
1081 gdb_assert (var
->print_value
!= NULL
&& print_value
!= NULL
);
1082 if (strcmp (var
->print_value
, print_value
) != 0)
1088 /* We must always keep the new value, since children depend on it. */
1089 if (var
->value
!= NULL
&& var
->value
!= value
)
1090 value_free (var
->value
);
1092 if (var
->print_value
)
1093 xfree (var
->print_value
);
1094 var
->print_value
= print_value
;
1095 if (value
&& value_lazy (value
) && intentionally_not_fetched
)
1096 var
->not_fetched
= 1;
1098 var
->not_fetched
= 0;
1101 gdb_assert (!var
->value
|| value_type (var
->value
));
1106 /* Update the values for a variable and its children. This is a
1107 two-pronged attack. First, re-parse the value for the root's
1108 expression to see if it's changed. Then go all the way
1109 through its children, reconstructing them and noting if they've
1112 < 0 for error values, see varobj.h.
1113 Otherwise it is the number of children + parent changed.
1115 The EXPLICIT parameter specifies if this call is result
1116 of MI request to update this specific variable, or
1117 result of implicit -var-update *. For implicit request, we don't
1118 update frozen variables.
1120 NOTE: This function may delete the caller's varobj. If it
1121 returns TYPE_CHANGED, then it has done this and VARP will be modified
1122 to point to the new varobj. */
1125 varobj_update (struct varobj
**varp
, struct varobj
***changelist
,
1129 int type_changed
= 0;
1134 struct varobj
**templist
= NULL
;
1136 VEC (varobj_p
) *stack
= NULL
;
1137 VEC (varobj_p
) *result
= NULL
;
1138 struct frame_info
*fi
;
1140 /* sanity check: have we been passed a pointer? */
1141 gdb_assert (changelist
);
1143 /* Frozen means frozen -- we don't check for any change in
1144 this varobj, including its going out of scope, or
1145 changing type. One use case for frozen varobjs is
1146 retaining previously evaluated expressions, and we don't
1147 want them to be reevaluated at all. */
1148 if (!explicit && (*varp
)->frozen
)
1151 if (!(*varp
)->root
->is_valid
)
1154 if ((*varp
)->root
->rootvar
== *varp
)
1156 /* Update the root variable. value_of_root can return NULL
1157 if the variable is no longer around, i.e. we stepped out of
1158 the frame in which a local existed. We are letting the
1159 value_of_root variable dispose of the varobj if the type
1161 new = value_of_root (varp
, &type_changed
);
1163 /* If this is a floating varobj, and its type has changed,
1164 them note that it's changed. */
1166 VEC_safe_push (varobj_p
, result
, *varp
);
1168 if (install_new_value ((*varp
), new, type_changed
))
1170 /* If type_changed is 1, install_new_value will never return
1171 non-zero, so we'll never report the same variable twice. */
1172 gdb_assert (!type_changed
);
1173 VEC_safe_push (varobj_p
, result
, *varp
);
1178 /* This means the varobj itself is out of scope.
1180 VEC_free (varobj_p
, result
);
1181 return NOT_IN_SCOPE
;
1185 VEC_safe_push (varobj_p
, stack
, *varp
);
1187 /* Walk through the children, reconstructing them all. */
1188 while (!VEC_empty (varobj_p
, stack
))
1190 v
= VEC_pop (varobj_p
, stack
);
1192 /* Push any children. Use reverse order so that the first
1193 child is popped from the work stack first, and so
1194 will be added to result first. This does not
1195 affect correctness, just "nicer". */
1196 for (i
= VEC_length (varobj_p
, v
->children
)-1; i
>= 0; --i
)
1198 varobj_p c
= VEC_index (varobj_p
, v
->children
, i
);
1199 /* Child may be NULL if explicitly deleted by -var-delete. */
1200 if (c
!= NULL
&& !c
->frozen
)
1201 VEC_safe_push (varobj_p
, stack
, c
);
1204 /* Update this variable, unless it's a root, which is already
1206 if (v
->root
->rootvar
!= v
)
1208 new = value_of_child (v
->parent
, v
->index
);
1209 if (install_new_value (v
, new, 0 /* type not changed */))
1211 /* Note that it's changed */
1212 VEC_safe_push (varobj_p
, result
, v
);
1218 /* Alloc (changed + 1) list entries. */
1219 changed
= VEC_length (varobj_p
, result
);
1220 *changelist
= xmalloc ((changed
+ 1) * sizeof (struct varobj
*));
1223 for (i
= 0; i
< changed
; ++i
)
1225 *cv
= VEC_index (varobj_p
, result
, i
);
1226 gdb_assert (*cv
!= NULL
);
1231 VEC_free (varobj_p
, stack
);
1232 VEC_free (varobj_p
, result
);
1235 return TYPE_CHANGED
;
1241 /* Helper functions */
1244 * Variable object construction/destruction
1248 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
1249 int only_children_p
)
1253 delete_variable_1 (resultp
, &delcount
, var
,
1254 only_children_p
, 1 /* remove_from_parent_p */ );
1259 /* Delete the variable object VAR and its children */
1260 /* IMPORTANT NOTE: If we delete a variable which is a child
1261 and the parent is not removed we dump core. It must be always
1262 initially called with remove_from_parent_p set */
1264 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
1265 struct varobj
*var
, int only_children_p
,
1266 int remove_from_parent_p
)
1270 /* Delete any children of this variable, too. */
1271 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
1273 varobj_p child
= VEC_index (varobj_p
, var
->children
, i
);
1276 if (!remove_from_parent_p
)
1277 child
->parent
= NULL
;
1278 delete_variable_1 (resultp
, delcountp
, child
, 0, only_children_p
);
1280 VEC_free (varobj_p
, var
->children
);
1282 /* if we were called to delete only the children we are done here */
1283 if (only_children_p
)
1286 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1287 /* If the name is null, this is a temporary variable, that has not
1288 yet been installed, don't report it, it belongs to the caller... */
1289 if (var
->obj_name
!= NULL
)
1291 cppush (resultp
, xstrdup (var
->obj_name
));
1292 *delcountp
= *delcountp
+ 1;
1295 /* If this variable has a parent, remove it from its parent's list */
1296 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1297 (as indicated by remove_from_parent_p) we don't bother doing an
1298 expensive list search to find the element to remove when we are
1299 discarding the list afterwards */
1300 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1302 VEC_replace (varobj_p
, var
->parent
->children
, var
->index
, NULL
);
1305 if (var
->obj_name
!= NULL
)
1306 uninstall_variable (var
);
1308 /* Free memory associated with this variable */
1309 free_variable (var
);
1312 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1314 install_variable (struct varobj
*var
)
1317 struct vlist
*newvl
;
1319 unsigned int index
= 0;
1322 for (chp
= var
->obj_name
; *chp
; chp
++)
1324 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1327 cv
= *(varobj_table
+ index
);
1328 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1332 error (_("Duplicate variable object name"));
1334 /* Add varobj to hash table */
1335 newvl
= xmalloc (sizeof (struct vlist
));
1336 newvl
->next
= *(varobj_table
+ index
);
1338 *(varobj_table
+ index
) = newvl
;
1340 /* If root, add varobj to root list */
1341 if (is_root_p (var
))
1343 /* Add to list of root variables */
1344 if (rootlist
== NULL
)
1345 var
->root
->next
= NULL
;
1347 var
->root
->next
= rootlist
;
1348 rootlist
= var
->root
;
1355 /* Unistall the object VAR. */
1357 uninstall_variable (struct varobj
*var
)
1361 struct varobj_root
*cr
;
1362 struct varobj_root
*prer
;
1364 unsigned int index
= 0;
1367 /* Remove varobj from hash table */
1368 for (chp
= var
->obj_name
; *chp
; chp
++)
1370 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1373 cv
= *(varobj_table
+ index
);
1375 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1382 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
1387 ("Assertion failed: Could not find variable object \"%s\" to delete",
1393 *(varobj_table
+ index
) = cv
->next
;
1395 prev
->next
= cv
->next
;
1399 /* If root, remove varobj from root list */
1400 if (is_root_p (var
))
1402 /* Remove from list of root variables */
1403 if (rootlist
== var
->root
)
1404 rootlist
= var
->root
->next
;
1409 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
1417 ("Assertion failed: Could not find varobj \"%s\" in root list",
1424 prer
->next
= cr
->next
;
1431 /* Create and install a child of the parent of the given name */
1432 static struct varobj
*
1433 create_child (struct varobj
*parent
, int index
, char *name
)
1435 struct varobj
*child
;
1437 struct value
*value
;
1439 child
= new_variable ();
1441 /* name is allocated by name_of_child */
1443 child
->index
= index
;
1444 value
= value_of_child (parent
, index
);
1445 child
->parent
= parent
;
1446 child
->root
= parent
->root
;
1447 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, name
);
1448 child
->obj_name
= childs_name
;
1449 install_variable (child
);
1451 /* Compute the type of the child. Must do this before
1452 calling install_new_value. */
1454 /* If the child had no evaluation errors, var->value
1455 will be non-NULL and contain a valid type. */
1456 child
->type
= value_type (value
);
1458 /* Otherwise, we must compute the type. */
1459 child
->type
= (*child
->root
->lang
->type_of_child
) (child
->parent
,
1461 install_new_value (child
, value
, 1);
1468 * Miscellaneous utility functions.
1471 /* Allocate memory and initialize a new variable */
1472 static struct varobj
*
1477 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
1479 var
->path_expr
= NULL
;
1480 var
->obj_name
= NULL
;
1484 var
->num_children
= -1;
1486 var
->children
= NULL
;
1490 var
->print_value
= NULL
;
1492 var
->not_fetched
= 0;
1497 /* Allocate memory and initialize a new root variable */
1498 static struct varobj
*
1499 new_root_variable (void)
1501 struct varobj
*var
= new_variable ();
1502 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));;
1503 var
->root
->lang
= NULL
;
1504 var
->root
->exp
= NULL
;
1505 var
->root
->valid_block
= NULL
;
1506 var
->root
->frame
= null_frame_id
;
1507 var
->root
->floating
= 0;
1508 var
->root
->rootvar
= NULL
;
1509 var
->root
->is_valid
= 1;
1514 /* Free any allocated memory associated with VAR. */
1516 free_variable (struct varobj
*var
)
1518 /* Free the expression if this is a root variable. */
1519 if (is_root_p (var
))
1521 free_current_contents (&var
->root
->exp
);
1526 xfree (var
->obj_name
);
1527 xfree (var
->print_value
);
1528 xfree (var
->path_expr
);
1533 do_free_variable_cleanup (void *var
)
1535 free_variable (var
);
1538 static struct cleanup
*
1539 make_cleanup_free_variable (struct varobj
*var
)
1541 return make_cleanup (do_free_variable_cleanup
, var
);
1544 /* This returns the type of the variable. It also skips past typedefs
1545 to return the real type of the variable.
1547 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1548 except within get_target_type and get_type. */
1549 static struct type
*
1550 get_type (struct varobj
*var
)
1556 type
= check_typedef (type
);
1561 /* Return the type of the value that's stored in VAR,
1562 or that would have being stored there if the
1563 value were accessible.
1565 This differs from VAR->type in that VAR->type is always
1566 the true type of the expession in the source language.
1567 The return value of this function is the type we're
1568 actually storing in varobj, and using for displaying
1569 the values and for comparing previous and new values.
1571 For example, top-level references are always stripped. */
1572 static struct type
*
1573 get_value_type (struct varobj
*var
)
1578 type
= value_type (var
->value
);
1582 type
= check_typedef (type
);
1584 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
1585 type
= get_target_type (type
);
1587 type
= check_typedef (type
);
1592 /* This returns the target type (or NULL) of TYPE, also skipping
1593 past typedefs, just like get_type ().
1595 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1596 except within get_target_type and get_type. */
1597 static struct type
*
1598 get_target_type (struct type
*type
)
1602 type
= TYPE_TARGET_TYPE (type
);
1604 type
= check_typedef (type
);
1610 /* What is the default display for this variable? We assume that
1611 everything is "natural". Any exceptions? */
1612 static enum varobj_display_formats
1613 variable_default_display (struct varobj
*var
)
1615 return FORMAT_NATURAL
;
1618 /* FIXME: The following should be generic for any pointer */
1620 cppush (struct cpstack
**pstack
, char *name
)
1624 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
1630 /* FIXME: The following should be generic for any pointer */
1632 cppop (struct cpstack
**pstack
)
1637 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
1642 *pstack
= (*pstack
)->next
;
1649 * Language-dependencies
1652 /* Common entry points */
1654 /* Get the language of variable VAR. */
1655 static enum varobj_languages
1656 variable_language (struct varobj
*var
)
1658 enum varobj_languages lang
;
1660 switch (var
->root
->exp
->language_defn
->la_language
)
1666 case language_cplus
:
1677 /* Return the number of children for a given variable.
1678 The result of this function is defined by the language
1679 implementation. The number of children returned by this function
1680 is the number of children that the user will see in the variable
1683 number_of_children (struct varobj
*var
)
1685 return (*var
->root
->lang
->number_of_children
) (var
);;
1688 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1690 name_of_variable (struct varobj
*var
)
1692 return (*var
->root
->lang
->name_of_variable
) (var
);
1695 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1697 name_of_child (struct varobj
*var
, int index
)
1699 return (*var
->root
->lang
->name_of_child
) (var
, index
);
1702 /* What is the ``struct value *'' of the root variable VAR?
1703 For floating variable object, evaluation can get us a value
1704 of different type from what is stored in varobj already. In
1706 - *type_changed will be set to 1
1707 - old varobj will be freed, and new one will be
1708 created, with the same name.
1709 - *var_handle will be set to the new varobj
1710 Otherwise, *type_changed will be set to 0. */
1711 static struct value
*
1712 value_of_root (struct varobj
**var_handle
, int *type_changed
)
1716 if (var_handle
== NULL
)
1721 /* This should really be an exception, since this should
1722 only get called with a root variable. */
1724 if (!is_root_p (var
))
1727 if (var
->root
->floating
)
1729 struct varobj
*tmp_var
;
1730 char *old_type
, *new_type
;
1732 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
1733 USE_SELECTED_FRAME
);
1734 if (tmp_var
== NULL
)
1738 old_type
= varobj_get_type (var
);
1739 new_type
= varobj_get_type (tmp_var
);
1740 if (strcmp (old_type
, new_type
) == 0)
1742 varobj_delete (tmp_var
, NULL
, 0);
1748 savestring (var
->obj_name
, strlen (var
->obj_name
));
1749 varobj_delete (var
, NULL
, 0);
1751 install_variable (tmp_var
);
1752 *var_handle
= tmp_var
;
1764 return (*var
->root
->lang
->value_of_root
) (var_handle
);
1767 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1768 static struct value
*
1769 value_of_child (struct varobj
*parent
, int index
)
1771 struct value
*value
;
1773 value
= (*parent
->root
->lang
->value_of_child
) (parent
, index
);
1778 /* GDB already has a command called "value_of_variable". Sigh. */
1780 my_value_of_variable (struct varobj
*var
)
1782 if (var
->root
->is_valid
)
1783 return (*var
->root
->lang
->value_of_variable
) (var
);
1789 value_get_print_value (struct value
*value
, enum varobj_display_formats format
)
1792 struct ui_file
*stb
;
1793 struct cleanup
*old_chain
;
1799 stb
= mem_fileopen ();
1800 old_chain
= make_cleanup_ui_file_delete (stb
);
1802 common_val_print (value
, stb
, format_code
[(int) format
], 1, 0, 0);
1803 thevalue
= ui_file_xstrdup (stb
, &dummy
);
1805 do_cleanups (old_chain
);
1810 varobj_editable_p (struct varobj
*var
)
1813 struct value
*value
;
1815 if (!(var
->root
->is_valid
&& var
->value
&& VALUE_LVAL (var
->value
)))
1818 type
= get_value_type (var
);
1820 switch (TYPE_CODE (type
))
1822 case TYPE_CODE_STRUCT
:
1823 case TYPE_CODE_UNION
:
1824 case TYPE_CODE_ARRAY
:
1825 case TYPE_CODE_FUNC
:
1826 case TYPE_CODE_METHOD
:
1836 /* Return non-zero if changes in value of VAR
1837 must be detected and reported by -var-update.
1838 Return zero is -var-update should never report
1839 changes of such values. This makes sense for structures
1840 (since the changes in children values will be reported separately),
1841 or for artifical objects (like 'public' pseudo-field in C++).
1843 Return value of 0 means that gdb need not call value_fetch_lazy
1844 for the value of this variable object. */
1846 varobj_value_is_changeable_p (struct varobj
*var
)
1851 if (CPLUS_FAKE_CHILD (var
))
1854 type
= get_value_type (var
);
1856 switch (TYPE_CODE (type
))
1858 case TYPE_CODE_STRUCT
:
1859 case TYPE_CODE_UNION
:
1860 case TYPE_CODE_ARRAY
:
1871 /* Return 1 if that varobj is floating, that is is always evaluated in the
1872 selected frame, and not bound to thread/frame. Such variable objects
1873 are created using '@' as frame specifier to -var-create. */
1875 varobj_floating_p (struct varobj
*var
)
1877 return var
->root
->floating
;
1880 /* Given the value and the type of a variable object,
1881 adjust the value and type to those necessary
1882 for getting children of the variable object.
1883 This includes dereferencing top-level references
1884 to all types and dereferencing pointers to
1887 Both TYPE and *TYPE should be non-null. VALUE
1888 can be null if we want to only translate type.
1889 *VALUE can be null as well -- if the parent
1892 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
1893 depending on whether pointer was deferenced
1894 in this function. */
1896 adjust_value_for_child_access (struct value
**value
,
1900 gdb_assert (type
&& *type
);
1905 *type
= check_typedef (*type
);
1907 /* The type of value stored in varobj, that is passed
1908 to us, is already supposed to be
1909 reference-stripped. */
1911 gdb_assert (TYPE_CODE (*type
) != TYPE_CODE_REF
);
1913 /* Pointers to structures are treated just like
1914 structures when accessing children. Don't
1915 dererences pointers to other types. */
1916 if (TYPE_CODE (*type
) == TYPE_CODE_PTR
)
1918 struct type
*target_type
= get_target_type (*type
);
1919 if (TYPE_CODE (target_type
) == TYPE_CODE_STRUCT
1920 || TYPE_CODE (target_type
) == TYPE_CODE_UNION
)
1922 if (value
&& *value
)
1924 int success
= gdb_value_ind (*value
, value
);
1928 *type
= target_type
;
1934 /* The 'get_target_type' function calls check_typedef on
1935 result, so we can immediately check type code. No
1936 need to call check_typedef here. */
1941 c_number_of_children (struct varobj
*var
)
1943 struct type
*type
= get_value_type (var
);
1945 struct type
*target
;
1947 adjust_value_for_child_access (NULL
, &type
, NULL
);
1948 target
= get_target_type (type
);
1950 switch (TYPE_CODE (type
))
1952 case TYPE_CODE_ARRAY
:
1953 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
1954 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) != BOUND_CANNOT_BE_DETERMINED
)
1955 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
1957 /* If we don't know how many elements there are, don't display
1962 case TYPE_CODE_STRUCT
:
1963 case TYPE_CODE_UNION
:
1964 children
= TYPE_NFIELDS (type
);
1968 /* The type here is a pointer to non-struct. Typically, pointers
1969 have one child, except for function ptrs, which have no children,
1970 and except for void*, as we don't know what to show.
1972 We can show char* so we allow it to be dereferenced. If you decide
1973 to test for it, please mind that a little magic is necessary to
1974 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1975 TYPE_NAME == "char" */
1976 if (TYPE_CODE (target
) == TYPE_CODE_FUNC
1977 || TYPE_CODE (target
) == TYPE_CODE_VOID
)
1984 /* Other types have no children */
1992 c_name_of_variable (struct varobj
*parent
)
1994 return savestring (parent
->name
, strlen (parent
->name
));
1997 /* Return the value of element TYPE_INDEX of a structure
1998 value VALUE. VALUE's type should be a structure,
1999 or union, or a typedef to struct/union.
2001 Returns NULL if getting the value fails. Never throws. */
2002 static struct value
*
2003 value_struct_element_index (struct value
*value
, int type_index
)
2005 struct value
*result
= NULL
;
2006 volatile struct gdb_exception e
;
2008 struct type
*type
= value_type (value
);
2009 type
= check_typedef (type
);
2011 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
2012 || TYPE_CODE (type
) == TYPE_CODE_UNION
);
2014 TRY_CATCH (e
, RETURN_MASK_ERROR
)
2016 if (TYPE_FIELD_STATIC (type
, type_index
))
2017 result
= value_static_field (type
, type_index
);
2019 result
= value_primitive_field (value
, 0, type_index
, type
);
2031 /* Obtain the information about child INDEX of the variable
2033 If CNAME is not null, sets *CNAME to the name of the child relative
2035 If CVALUE is not null, sets *CVALUE to the value of the child.
2036 If CTYPE is not null, sets *CTYPE to the type of the child.
2038 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
2039 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
2042 c_describe_child (struct varobj
*parent
, int index
,
2043 char **cname
, struct value
**cvalue
, struct type
**ctype
,
2044 char **cfull_expression
)
2046 struct value
*value
= parent
->value
;
2047 struct type
*type
= get_value_type (parent
);
2048 char *parent_expression
= NULL
;
2057 if (cfull_expression
)
2059 *cfull_expression
= NULL
;
2060 parent_expression
= varobj_get_path_expr (parent
);
2062 adjust_value_for_child_access (&value
, &type
, &was_ptr
);
2064 switch (TYPE_CODE (type
))
2066 case TYPE_CODE_ARRAY
:
2068 *cname
= xstrprintf ("%d", index
2069 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
2071 if (cvalue
&& value
)
2073 int real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
2074 struct value
*indval
=
2075 value_from_longest (builtin_type_int
, (LONGEST
) real_index
);
2076 gdb_value_subscript (value
, indval
, cvalue
);
2080 *ctype
= get_target_type (type
);
2082 if (cfull_expression
)
2083 *cfull_expression
= xstrprintf ("(%s)[%d]", parent_expression
,
2085 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
2090 case TYPE_CODE_STRUCT
:
2091 case TYPE_CODE_UNION
:
2094 char *string
= TYPE_FIELD_NAME (type
, index
);
2095 *cname
= savestring (string
, strlen (string
));
2098 if (cvalue
&& value
)
2100 /* For C, varobj index is the same as type index. */
2101 *cvalue
= value_struct_element_index (value
, index
);
2105 *ctype
= TYPE_FIELD_TYPE (type
, index
);
2107 if (cfull_expression
)
2109 char *join
= was_ptr
? "->" : ".";
2110 *cfull_expression
= xstrprintf ("(%s)%s%s", parent_expression
, join
,
2111 TYPE_FIELD_NAME (type
, index
));
2118 *cname
= xstrprintf ("*%s", parent
->name
);
2120 if (cvalue
&& value
)
2122 int success
= gdb_value_ind (value
, cvalue
);
2127 /* Don't use get_target_type because it calls
2128 check_typedef and here, we want to show the true
2129 declared type of the variable. */
2131 *ctype
= TYPE_TARGET_TYPE (type
);
2133 if (cfull_expression
)
2134 *cfull_expression
= xstrprintf ("*(%s)", parent_expression
);
2139 /* This should not happen */
2141 *cname
= xstrdup ("???");
2142 if (cfull_expression
)
2143 *cfull_expression
= xstrdup ("???");
2144 /* Don't set value and type, we don't know then. */
2149 c_name_of_child (struct varobj
*parent
, int index
)
2152 c_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
2157 c_path_expr_of_child (struct varobj
*child
)
2159 c_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
2161 return child
->path_expr
;
2164 /* If frame associated with VAR can be found, switch
2165 to it and return 1. Otherwise, return 0. */
2167 check_scope (struct varobj
*var
)
2169 struct frame_info
*fi
;
2172 fi
= frame_find_by_id (var
->root
->frame
);
2177 CORE_ADDR pc
= get_frame_pc (fi
);
2178 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
2179 pc
>= BLOCK_END (var
->root
->valid_block
))
2187 static struct value
*
2188 c_value_of_root (struct varobj
**var_handle
)
2190 struct value
*new_val
= NULL
;
2191 struct varobj
*var
= *var_handle
;
2192 struct frame_info
*fi
;
2193 int within_scope
= 0;
2194 struct cleanup
*back_to
;
2196 /* Only root variables can be updated... */
2197 if (!is_root_p (var
))
2198 /* Not a root var */
2201 back_to
= make_cleanup_restore_current_thread (
2202 inferior_ptid
, get_frame_id (deprecated_safe_get_selected_frame ()));
2204 /* Determine whether the variable is still around. */
2205 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
2207 else if (var
->root
->thread_id
== 0)
2209 /* The program was single-threaded when the variable object was
2210 created. Technically, it's possible that the program became
2211 multi-threaded since then, but we don't support such
2213 within_scope
= check_scope (var
);
2217 ptid_t ptid
= thread_id_to_pid (var
->root
->thread_id
);
2218 if (in_thread_list (ptid
))
2220 switch_to_thread (ptid
);
2221 within_scope
= check_scope (var
);
2227 /* We need to catch errors here, because if evaluate
2228 expression fails we want to just return NULL. */
2229 gdb_evaluate_expression (var
->root
->exp
, &new_val
);
2233 do_cleanups (back_to
);
2238 static struct value
*
2239 c_value_of_child (struct varobj
*parent
, int index
)
2241 struct value
*value
= NULL
;
2242 c_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
2247 static struct type
*
2248 c_type_of_child (struct varobj
*parent
, int index
)
2250 struct type
*type
= NULL
;
2251 c_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
2256 c_value_of_variable (struct varobj
*var
)
2258 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2259 it will print out its children instead of "{...}". So we need to
2260 catch that case explicitly. */
2261 struct type
*type
= get_type (var
);
2263 /* Strip top-level references. */
2264 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
2265 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2267 switch (TYPE_CODE (type
))
2269 case TYPE_CODE_STRUCT
:
2270 case TYPE_CODE_UNION
:
2271 return xstrdup ("{...}");
2274 case TYPE_CODE_ARRAY
:
2277 number
= xstrprintf ("[%d]", var
->num_children
);
2284 if (var
->value
== NULL
)
2286 /* This can happen if we attempt to get the value of a struct
2287 member when the parent is an invalid pointer. This is an
2288 error condition, so we should tell the caller. */
2293 if (var
->not_fetched
&& value_lazy (var
->value
))
2294 /* Frozen variable and no value yet. We don't
2295 implicitly fetch the value. MI response will
2296 use empty string for the value, which is OK. */
2299 gdb_assert (varobj_value_is_changeable_p (var
));
2300 gdb_assert (!value_lazy (var
->value
));
2301 return xstrdup (var
->print_value
);
2311 cplus_number_of_children (struct varobj
*var
)
2314 int children
, dont_know
;
2319 if (!CPLUS_FAKE_CHILD (var
))
2321 type
= get_value_type (var
);
2322 adjust_value_for_child_access (NULL
, &type
, NULL
);
2324 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2325 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2329 cplus_class_num_children (type
, kids
);
2330 if (kids
[v_public
] != 0)
2332 if (kids
[v_private
] != 0)
2334 if (kids
[v_protected
] != 0)
2337 /* Add any baseclasses */
2338 children
+= TYPE_N_BASECLASSES (type
);
2341 /* FIXME: save children in var */
2348 type
= get_value_type (var
->parent
);
2349 adjust_value_for_child_access (NULL
, &type
, NULL
);
2351 cplus_class_num_children (type
, kids
);
2352 if (strcmp (var
->name
, "public") == 0)
2353 children
= kids
[v_public
];
2354 else if (strcmp (var
->name
, "private") == 0)
2355 children
= kids
[v_private
];
2357 children
= kids
[v_protected
];
2362 children
= c_number_of_children (var
);
2367 /* Compute # of public, private, and protected variables in this class.
2368 That means we need to descend into all baseclasses and find out
2369 how many are there, too. */
2371 cplus_class_num_children (struct type
*type
, int children
[3])
2375 children
[v_public
] = 0;
2376 children
[v_private
] = 0;
2377 children
[v_protected
] = 0;
2379 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
2381 /* If we have a virtual table pointer, omit it. */
2382 if (TYPE_VPTR_BASETYPE (type
) == type
&& TYPE_VPTR_FIELDNO (type
) == i
)
2385 if (TYPE_FIELD_PROTECTED (type
, i
))
2386 children
[v_protected
]++;
2387 else if (TYPE_FIELD_PRIVATE (type
, i
))
2388 children
[v_private
]++;
2390 children
[v_public
]++;
2395 cplus_name_of_variable (struct varobj
*parent
)
2397 return c_name_of_variable (parent
);
2400 enum accessibility
{ private_field
, protected_field
, public_field
};
2402 /* Check if field INDEX of TYPE has the specified accessibility.
2403 Return 0 if so and 1 otherwise. */
2405 match_accessibility (struct type
*type
, int index
, enum accessibility acc
)
2407 if (acc
== private_field
&& TYPE_FIELD_PRIVATE (type
, index
))
2409 else if (acc
== protected_field
&& TYPE_FIELD_PROTECTED (type
, index
))
2411 else if (acc
== public_field
&& !TYPE_FIELD_PRIVATE (type
, index
)
2412 && !TYPE_FIELD_PROTECTED (type
, index
))
2419 cplus_describe_child (struct varobj
*parent
, int index
,
2420 char **cname
, struct value
**cvalue
, struct type
**ctype
,
2421 char **cfull_expression
)
2424 struct value
*value
;
2427 char *parent_expression
= NULL
;
2435 if (cfull_expression
)
2436 *cfull_expression
= NULL
;
2438 if (CPLUS_FAKE_CHILD (parent
))
2440 value
= parent
->parent
->value
;
2441 type
= get_value_type (parent
->parent
);
2442 if (cfull_expression
)
2443 parent_expression
= varobj_get_path_expr (parent
->parent
);
2447 value
= parent
->value
;
2448 type
= get_value_type (parent
);
2449 if (cfull_expression
)
2450 parent_expression
= varobj_get_path_expr (parent
);
2453 adjust_value_for_child_access (&value
, &type
, &was_ptr
);
2455 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
2456 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
2458 char *join
= was_ptr
? "->" : ".";
2459 if (CPLUS_FAKE_CHILD (parent
))
2461 /* The fields of the class type are ordered as they
2462 appear in the class. We are given an index for a
2463 particular access control type ("public","protected",
2464 or "private"). We must skip over fields that don't
2465 have the access control we are looking for to properly
2466 find the indexed field. */
2467 int type_index
= TYPE_N_BASECLASSES (type
);
2468 enum accessibility acc
= public_field
;
2469 if (strcmp (parent
->name
, "private") == 0)
2470 acc
= private_field
;
2471 else if (strcmp (parent
->name
, "protected") == 0)
2472 acc
= protected_field
;
2476 if (TYPE_VPTR_BASETYPE (type
) == type
2477 && type_index
== TYPE_VPTR_FIELDNO (type
))
2479 else if (match_accessibility (type
, type_index
, acc
))
2486 *cname
= xstrdup (TYPE_FIELD_NAME (type
, type_index
));
2488 if (cvalue
&& value
)
2489 *cvalue
= value_struct_element_index (value
, type_index
);
2492 *ctype
= TYPE_FIELD_TYPE (type
, type_index
);
2494 if (cfull_expression
)
2495 *cfull_expression
= xstrprintf ("((%s)%s%s)", parent_expression
,
2497 TYPE_FIELD_NAME (type
, type_index
));
2499 else if (index
< TYPE_N_BASECLASSES (type
))
2501 /* This is a baseclass. */
2503 *cname
= xstrdup (TYPE_FIELD_NAME (type
, index
));
2505 if (cvalue
&& value
)
2507 *cvalue
= value_cast (TYPE_FIELD_TYPE (type
, index
), value
);
2508 release_value (*cvalue
);
2513 *ctype
= TYPE_FIELD_TYPE (type
, index
);
2516 if (cfull_expression
)
2518 char *ptr
= was_ptr
? "*" : "";
2519 /* Cast the parent to the base' type. Note that in gdb,
2522 will create an lvalue, for all appearences, so we don't
2523 need to use more fancy:
2526 *cfull_expression
= xstrprintf ("(%s(%s%s) %s)",
2528 TYPE_FIELD_NAME (type
, index
),
2535 char *access
= NULL
;
2537 cplus_class_num_children (type
, children
);
2539 /* Everything beyond the baseclasses can
2540 only be "public", "private", or "protected"
2542 The special "fake" children are always output by varobj in
2543 this order. So if INDEX == 2, it MUST be "protected". */
2544 index
-= TYPE_N_BASECLASSES (type
);
2548 if (children
[v_public
] > 0)
2550 else if (children
[v_private
] > 0)
2553 access
= "protected";
2556 if (children
[v_public
] > 0)
2558 if (children
[v_private
] > 0)
2561 access
= "protected";
2563 else if (children
[v_private
] > 0)
2564 access
= "protected";
2567 /* Must be protected */
2568 access
= "protected";
2575 gdb_assert (access
);
2577 *cname
= xstrdup (access
);
2579 /* Value and type and full expression are null here. */
2584 c_describe_child (parent
, index
, cname
, cvalue
, ctype
, cfull_expression
);
2589 cplus_name_of_child (struct varobj
*parent
, int index
)
2592 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
2597 cplus_path_expr_of_child (struct varobj
*child
)
2599 cplus_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
2601 return child
->path_expr
;
2604 static struct value
*
2605 cplus_value_of_root (struct varobj
**var_handle
)
2607 return c_value_of_root (var_handle
);
2610 static struct value
*
2611 cplus_value_of_child (struct varobj
*parent
, int index
)
2613 struct value
*value
= NULL
;
2614 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
2618 static struct type
*
2619 cplus_type_of_child (struct varobj
*parent
, int index
)
2621 struct type
*type
= NULL
;
2622 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
2627 cplus_value_of_variable (struct varobj
*var
)
2630 /* If we have one of our special types, don't print out
2632 if (CPLUS_FAKE_CHILD (var
))
2633 return xstrdup ("");
2635 return c_value_of_variable (var
);
2641 java_number_of_children (struct varobj
*var
)
2643 return cplus_number_of_children (var
);
2647 java_name_of_variable (struct varobj
*parent
)
2651 name
= cplus_name_of_variable (parent
);
2652 /* If the name has "-" in it, it is because we
2653 needed to escape periods in the name... */
2656 while (*p
!= '\000')
2667 java_name_of_child (struct varobj
*parent
, int index
)
2671 name
= cplus_name_of_child (parent
, index
);
2672 /* Escape any periods in the name... */
2675 while (*p
!= '\000')
2686 java_path_expr_of_child (struct varobj
*child
)
2691 static struct value
*
2692 java_value_of_root (struct varobj
**var_handle
)
2694 return cplus_value_of_root (var_handle
);
2697 static struct value
*
2698 java_value_of_child (struct varobj
*parent
, int index
)
2700 return cplus_value_of_child (parent
, index
);
2703 static struct type
*
2704 java_type_of_child (struct varobj
*parent
, int index
)
2706 return cplus_type_of_child (parent
, index
);
2710 java_value_of_variable (struct varobj
*var
)
2712 return cplus_value_of_variable (var
);
2715 extern void _initialize_varobj (void);
2717 _initialize_varobj (void)
2719 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
2721 varobj_table
= xmalloc (sizeof_table
);
2722 memset (varobj_table
, 0, sizeof_table
);
2724 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance
,
2726 Set varobj debugging."), _("\
2727 Show varobj debugging."), _("\
2728 When non-zero, varobj debugging is enabled."),
2731 &setlist
, &showlist
);
2734 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2735 are defined on globals.
2736 Invalidated varobjs will be always printed in_scope="invalid". */
2738 varobj_invalidate (void)
2740 struct varobj
**all_rootvarobj
;
2741 struct varobj
**varp
;
2743 if (varobj_list (&all_rootvarobj
) > 0)
2745 varp
= all_rootvarobj
;
2746 while (*varp
!= NULL
)
2748 /* global var must be re-evaluated. */
2749 if ((*varp
)->root
->valid_block
== NULL
)
2751 struct varobj
*tmp_var
;
2753 /* Try to create a varobj with same expression. If we succeed replace
2754 the old varobj, otherwise invalidate it. */
2755 tmp_var
= varobj_create (NULL
, (*varp
)->name
, (CORE_ADDR
) 0, USE_CURRENT_FRAME
);
2756 if (tmp_var
!= NULL
)
2758 tmp_var
->obj_name
= xstrdup ((*varp
)->obj_name
);
2759 varobj_delete (*varp
, NULL
, 0);
2760 install_variable (tmp_var
);
2763 (*varp
)->root
->is_valid
= 0;
2765 else /* locals must be invalidated. */
2766 (*varp
)->root
->is_valid
= 0;
2770 xfree (all_rootvarobj
);