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)
436 frame
= get_prev_frame (frame
);
439 if (get_frame_base_address (frame
) == frame_addr
)
445 varobj_create (char *objname
,
446 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
449 struct frame_info
*fi
;
450 struct frame_info
*old_fi
= NULL
;
452 struct cleanup
*old_chain
;
454 /* Fill out a varobj structure for the (root) variable being constructed. */
455 var
= new_root_variable ();
456 old_chain
= make_cleanup_free_variable (var
);
458 if (expression
!= NULL
)
461 enum varobj_languages lang
;
462 struct value
*value
= NULL
;
465 /* Parse and evaluate the expression, filling in as much
466 of the variable's data as possible */
468 /* Allow creator to specify context of variable */
469 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
470 fi
= deprecated_safe_get_selected_frame ();
472 /* FIXME: cagney/2002-11-23: This code should be doing a
473 lookup using the frame ID and not just the frame's
474 ``address''. This, of course, means an interface change.
475 However, with out that interface change ISAs, such as the
476 ia64 with its two stacks, won't work. Similar goes for the
477 case where there is a frameless function. */
478 fi
= find_frame_addr_in_frame_chain (frame
);
480 /* frame = -2 means always use selected frame */
481 if (type
== USE_SELECTED_FRAME
)
482 var
->root
->floating
= 1;
486 block
= get_frame_block (fi
, 0);
489 innermost_block
= NULL
;
490 /* Wrap the call to parse expression, so we can
491 return a sensible error. */
492 if (!gdb_parse_exp_1 (&p
, block
, 0, &var
->root
->exp
))
497 /* Don't allow variables to be created for types. */
498 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
)
500 do_cleanups (old_chain
);
501 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
502 " as an expression.\n");
506 var
->format
= variable_default_display (var
);
507 var
->root
->valid_block
= innermost_block
;
508 expr_len
= strlen (expression
);
509 var
->name
= savestring (expression
, expr_len
);
510 /* For a root var, the name and the expr are the same. */
511 var
->path_expr
= savestring (expression
, expr_len
);
513 /* When the frame is different from the current frame,
514 we must select the appropriate frame before parsing
515 the expression, otherwise the value will not be current.
516 Since select_frame is so benign, just call it for all cases. */
517 if (innermost_block
&& fi
!= NULL
)
519 var
->root
->frame
= get_frame_id (fi
);
520 var
->root
->thread_id
= pid_to_thread_id (inferior_ptid
);
521 old_fi
= get_selected_frame (NULL
);
525 /* We definitely need to catch errors here.
526 If evaluate_expression succeeds we got the value we wanted.
527 But if it fails, we still go on with a call to evaluate_type() */
528 if (!gdb_evaluate_expression (var
->root
->exp
, &value
))
530 /* Error getting the value. Try to at least get the
532 struct value
*type_only_value
= evaluate_type (var
->root
->exp
);
533 var
->type
= value_type (type_only_value
);
536 var
->type
= value_type (value
);
538 install_new_value (var
, value
, 1 /* Initial assignment */);
540 /* Set language info */
541 lang
= variable_language (var
);
542 var
->root
->lang
= &languages
[lang
];
544 /* Set ourselves as our root */
545 var
->root
->rootvar
= var
;
547 /* Reset the selected frame */
549 select_frame (old_fi
);
552 /* If the variable object name is null, that means this
553 is a temporary variable, so don't install it. */
555 if ((var
!= NULL
) && (objname
!= NULL
))
557 var
->obj_name
= savestring (objname
, strlen (objname
));
559 /* If a varobj name is duplicated, the install will fail so
561 if (!install_variable (var
))
563 do_cleanups (old_chain
);
568 discard_cleanups (old_chain
);
572 /* Generates an unique name that can be used for a varobj */
575 varobj_gen_name (void)
580 /* generate a name for this object */
582 obj_name
= xstrprintf ("var%d", id
);
587 /* Given an "objname", returns the pointer to the corresponding varobj
588 or NULL if not found */
591 varobj_get_handle (char *objname
)
595 unsigned int index
= 0;
598 for (chp
= objname
; *chp
; chp
++)
600 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
603 cv
= *(varobj_table
+ index
);
604 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
608 error (_("Variable object not found"));
613 /* Given the handle, return the name of the object */
616 varobj_get_objname (struct varobj
*var
)
618 return var
->obj_name
;
621 /* Given the handle, return the expression represented by the object */
624 varobj_get_expression (struct varobj
*var
)
626 return name_of_variable (var
);
629 /* Deletes a varobj and all its children if only_children == 0,
630 otherwise deletes only the children; returns a malloc'ed list of all the
631 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
634 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
638 struct cpstack
*result
= NULL
;
641 /* Initialize a stack for temporary results */
642 cppush (&result
, NULL
);
645 /* Delete only the variable children */
646 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
648 /* Delete the variable and all its children */
649 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
651 /* We may have been asked to return a list of what has been deleted */
654 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
658 *cp
= cppop (&result
);
659 while ((*cp
!= NULL
) && (mycount
> 0))
663 *cp
= cppop (&result
);
666 if (mycount
|| (*cp
!= NULL
))
667 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
674 /* Set/Get variable object display format */
676 enum varobj_display_formats
677 varobj_set_display_format (struct varobj
*var
,
678 enum varobj_display_formats format
)
685 case FORMAT_HEXADECIMAL
:
687 var
->format
= format
;
691 var
->format
= variable_default_display (var
);
694 if (varobj_value_is_changeable_p (var
)
695 && var
->value
&& !value_lazy (var
->value
))
697 xfree (var
->print_value
);
698 var
->print_value
= value_get_print_value (var
->value
, var
->format
);
704 enum varobj_display_formats
705 varobj_get_display_format (struct varobj
*var
)
710 /* If the variable object is bound to a specific thread, that
711 is its evaluation can always be done in context of a frame
712 inside that thread, returns GDB id of the thread -- which
713 is always positive. Otherwise, returns -1. */
715 varobj_get_thread_id (struct varobj
*var
)
717 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
718 return var
->root
->thread_id
;
724 varobj_set_frozen (struct varobj
*var
, int frozen
)
726 /* When a variable is unfrozen, we don't fetch its value.
727 The 'not_fetched' flag remains set, so next -var-update
730 We don't fetch the value, because for structures the client
731 should do -var-update anyway. It would be bad to have different
732 client-size logic for structure and other types. */
733 var
->frozen
= frozen
;
737 varobj_get_frozen (struct varobj
*var
)
744 varobj_get_num_children (struct varobj
*var
)
746 if (var
->num_children
== -1)
747 var
->num_children
= number_of_children (var
);
749 return var
->num_children
;
752 /* Creates a list of the immediate children of a variable object;
753 the return code is the number of such children or -1 on error */
756 varobj_list_children (struct varobj
*var
)
758 struct varobj
*child
;
762 if (var
->num_children
== -1)
763 var
->num_children
= number_of_children (var
);
765 /* If that failed, give up. */
766 if (var
->num_children
== -1)
767 return var
->children
;
769 /* If we're called when the list of children is not yet initialized,
770 allocate enough elements in it. */
771 while (VEC_length (varobj_p
, var
->children
) < var
->num_children
)
772 VEC_safe_push (varobj_p
, var
->children
, NULL
);
774 for (i
= 0; i
< var
->num_children
; i
++)
776 varobj_p existing
= VEC_index (varobj_p
, var
->children
, i
);
778 if (existing
== NULL
)
780 /* Either it's the first call to varobj_list_children for
781 this variable object, and the child was never created,
782 or it was explicitly deleted by the client. */
783 name
= name_of_child (var
, i
);
784 existing
= create_child (var
, i
, name
);
785 VEC_replace (varobj_p
, var
->children
, i
, existing
);
789 return var
->children
;
792 /* Obtain the type of an object Variable as a string similar to the one gdb
793 prints on the console */
796 varobj_get_type (struct varobj
*var
)
799 struct cleanup
*old_chain
;
804 /* For the "fake" variables, do not return a type. (It's type is
806 Do not return a type for invalid variables as well. */
807 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
810 stb
= mem_fileopen ();
811 old_chain
= make_cleanup_ui_file_delete (stb
);
813 /* To print the type, we simply create a zero ``struct value *'' and
814 cast it to our type. We then typeprint this variable. */
815 val
= value_zero (var
->type
, not_lval
);
816 type_print (value_type (val
), "", stb
, -1);
818 thetype
= ui_file_xstrdup (stb
, &length
);
819 do_cleanups (old_chain
);
823 /* Obtain the type of an object variable. */
826 varobj_get_gdb_type (struct varobj
*var
)
831 /* Return a pointer to the full rooted expression of varobj VAR.
832 If it has not been computed yet, compute it. */
834 varobj_get_path_expr (struct varobj
*var
)
836 if (var
->path_expr
!= NULL
)
837 return var
->path_expr
;
840 /* For root varobjs, we initialize path_expr
841 when creating varobj, so here it should be
843 gdb_assert (!is_root_p (var
));
844 return (*var
->root
->lang
->path_expr_of_child
) (var
);
848 enum varobj_languages
849 varobj_get_language (struct varobj
*var
)
851 return variable_language (var
);
855 varobj_get_attributes (struct varobj
*var
)
859 if (varobj_editable_p (var
))
860 /* FIXME: define masks for attributes */
861 attributes
|= 0x00000001; /* Editable */
867 varobj_get_formatted_value (struct varobj
*var
,
868 enum varobj_display_formats format
)
870 return my_value_of_variable (var
, format
);
874 varobj_get_value (struct varobj
*var
)
876 return my_value_of_variable (var
, var
->format
);
879 /* Set the value of an object variable (if it is editable) to the
880 value of the given expression */
881 /* Note: Invokes functions that can call error() */
884 varobj_set_value (struct varobj
*var
, char *expression
)
890 /* The argument "expression" contains the variable's new value.
891 We need to first construct a legal expression for this -- ugh! */
892 /* Does this cover all the bases? */
893 struct expression
*exp
;
895 int saved_input_radix
= input_radix
;
896 char *s
= expression
;
899 gdb_assert (varobj_editable_p (var
));
901 input_radix
= 10; /* ALWAYS reset to decimal temporarily */
902 exp
= parse_exp_1 (&s
, 0, 0);
903 if (!gdb_evaluate_expression (exp
, &value
))
905 /* We cannot proceed without a valid expression. */
910 /* All types that are editable must also be changeable. */
911 gdb_assert (varobj_value_is_changeable_p (var
));
913 /* The value of a changeable variable object must not be lazy. */
914 gdb_assert (!value_lazy (var
->value
));
916 /* Need to coerce the input. We want to check if the
917 value of the variable object will be different
918 after assignment, and the first thing value_assign
919 does is coerce the input.
920 For example, if we are assigning an array to a pointer variable we
921 should compare the pointer with the the array's address, not with the
923 value
= coerce_array (value
);
925 /* The new value may be lazy. gdb_value_assign, or
926 rather value_contents, will take care of this.
927 If fetching of the new value will fail, gdb_value_assign
928 with catch the exception. */
929 if (!gdb_value_assign (var
->value
, value
, &val
))
932 /* If the value has changed, record it, so that next -var-update can
933 report this change. If a variable had a value of '1', we've set it
934 to '333' and then set again to '1', when -var-update will report this
935 variable as changed -- because the first assignment has set the
936 'updated' flag. There's no need to optimize that, because return value
937 of -var-update should be considered an approximation. */
938 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
939 input_radix
= saved_input_radix
;
943 /* Returns a malloc'ed list with all root variable objects */
945 varobj_list (struct varobj
***varlist
)
948 struct varobj_root
*croot
;
949 int mycount
= rootcount
;
951 /* Alloc (rootcount + 1) entries for the result */
952 *varlist
= xmalloc ((rootcount
+ 1) * sizeof (struct varobj
*));
956 while ((croot
!= NULL
) && (mycount
> 0))
958 *cv
= croot
->rootvar
;
963 /* Mark the end of the list */
966 if (mycount
|| (croot
!= NULL
))
968 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
974 /* Assign a new value to a variable object. If INITIAL is non-zero,
975 this is the first assignement after the variable object was just
976 created, or changed type. In that case, just assign the value
978 Otherwise, assign the value and if type_changeable returns non-zero,
979 find if the new value is different from the current value.
980 Return 1 if so, and 0 if the values are equal.
982 The VALUE parameter should not be released -- the function will
983 take care of releasing it when needed. */
985 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
990 int intentionally_not_fetched
= 0;
991 char *print_value
= NULL
;
993 /* We need to know the varobj's type to decide if the value should
994 be fetched or not. C++ fake children (public/protected/private) don't have
996 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
997 changeable
= varobj_value_is_changeable_p (var
);
998 need_to_fetch
= changeable
;
1000 /* We are not interested in the address of references, and given
1001 that in C++ a reference is not rebindable, it cannot
1002 meaningfully change. So, get hold of the real value. */
1005 value
= coerce_ref (value
);
1006 release_value (value
);
1009 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
1010 /* For unions, we need to fetch the value implicitly because
1011 of implementation of union member fetch. When gdb
1012 creates a value for a field and the value of the enclosing
1013 structure is not lazy, it immediately copies the necessary
1014 bytes from the enclosing values. If the enclosing value is
1015 lazy, the call to value_fetch_lazy on the field will read
1016 the data from memory. For unions, that means we'll read the
1017 same memory more than once, which is not desirable. So
1021 /* The new value might be lazy. If the type is changeable,
1022 that is we'll be comparing values of this type, fetch the
1023 value now. Otherwise, on the next update the old value
1024 will be lazy, which means we've lost that old value. */
1025 if (need_to_fetch
&& value
&& value_lazy (value
))
1027 struct varobj
*parent
= var
->parent
;
1028 int frozen
= var
->frozen
;
1029 for (; !frozen
&& parent
; parent
= parent
->parent
)
1030 frozen
|= parent
->frozen
;
1032 if (frozen
&& initial
)
1034 /* For variables that are frozen, or are children of frozen
1035 variables, we don't do fetch on initial assignment.
1036 For non-initial assignemnt we do the fetch, since it means we're
1037 explicitly asked to compare the new value with the old one. */
1038 intentionally_not_fetched
= 1;
1040 else if (!gdb_value_fetch_lazy (value
))
1042 /* Set the value to NULL, so that for the next -var-update,
1043 we don't try to compare the new value with this value,
1044 that we couldn't even read. */
1049 /* Below, we'll be comparing string rendering of old and new
1050 values. Don't get string rendering if the value is
1051 lazy -- if it is, the code above has decided that the value
1052 should not be fetched. */
1053 if (value
&& !value_lazy (value
))
1054 print_value
= value_get_print_value (value
, var
->format
);
1056 /* If the type is changeable, compare the old and the new values.
1057 If this is the initial assignment, we don't have any old value
1059 if (!initial
&& changeable
)
1061 /* If the value of the varobj was changed by -var-set-value, then the
1062 value in the varobj and in the target is the same. However, that value
1063 is different from the value that the varobj had after the previous
1064 -var-update. So need to the varobj as changed. */
1071 /* Try to compare the values. That requires that both
1072 values are non-lazy. */
1073 if (var
->not_fetched
&& value_lazy (var
->value
))
1075 /* This is a frozen varobj and the value was never read.
1076 Presumably, UI shows some "never read" indicator.
1077 Now that we've fetched the real value, we need to report
1078 this varobj as changed so that UI can show the real
1082 else if (var
->value
== NULL
&& value
== NULL
)
1085 else if (var
->value
== NULL
|| value
== NULL
)
1091 gdb_assert (!value_lazy (var
->value
));
1092 gdb_assert (!value_lazy (value
));
1094 gdb_assert (var
->print_value
!= NULL
&& print_value
!= NULL
);
1095 if (strcmp (var
->print_value
, print_value
) != 0)
1101 /* We must always keep the new value, since children depend on it. */
1102 if (var
->value
!= NULL
&& var
->value
!= value
)
1103 value_free (var
->value
);
1105 if (var
->print_value
)
1106 xfree (var
->print_value
);
1107 var
->print_value
= print_value
;
1108 if (value
&& value_lazy (value
) && intentionally_not_fetched
)
1109 var
->not_fetched
= 1;
1111 var
->not_fetched
= 0;
1114 gdb_assert (!var
->value
|| value_type (var
->value
));
1119 /* Update the values for a variable and its children. This is a
1120 two-pronged attack. First, re-parse the value for the root's
1121 expression to see if it's changed. Then go all the way
1122 through its children, reconstructing them and noting if they've
1125 The EXPLICIT parameter specifies if this call is result
1126 of MI request to update this specific variable, or
1127 result of implicit -var-update *. For implicit request, we don't
1128 update frozen variables.
1130 NOTE: This function may delete the caller's varobj. If it
1131 returns TYPE_CHANGED, then it has done this and VARP will be modified
1132 to point to the new varobj. */
1134 VEC(varobj_update_result
) *varobj_update (struct varobj
**varp
, int explicit)
1137 int type_changed
= 0;
1142 struct varobj
**templist
= NULL
;
1144 VEC (varobj_p
) *stack
= NULL
;
1145 VEC (varobj_update_result
) *result
= NULL
;
1146 struct frame_info
*fi
;
1148 /* Frozen means frozen -- we don't check for any change in
1149 this varobj, including its going out of scope, or
1150 changing type. One use case for frozen varobjs is
1151 retaining previously evaluated expressions, and we don't
1152 want them to be reevaluated at all. */
1153 if (!explicit && (*varp
)->frozen
)
1156 if (!(*varp
)->root
->is_valid
)
1158 varobj_update_result r
= {*varp
};
1159 r
.status
= VAROBJ_INVALID
;
1160 VEC_safe_push (varobj_update_result
, result
, &r
);
1164 if ((*varp
)->root
->rootvar
== *varp
)
1166 varobj_update_result r
= {*varp
};
1167 r
.status
= VAROBJ_IN_SCOPE
;
1169 /* Update the root variable. value_of_root can return NULL
1170 if the variable is no longer around, i.e. we stepped out of
1171 the frame in which a local existed. We are letting the
1172 value_of_root variable dispose of the varobj if the type
1174 new = value_of_root (varp
, &type_changed
);
1177 r
.type_changed
= type_changed
;
1178 if (install_new_value ((*varp
), new, type_changed
))
1182 r
.status
= VAROBJ_NOT_IN_SCOPE
;
1184 if (r
.type_changed
|| r
.changed
)
1185 VEC_safe_push (varobj_update_result
, result
, &r
);
1187 if (r
.status
== VAROBJ_NOT_IN_SCOPE
)
1191 VEC_safe_push (varobj_p
, stack
, *varp
);
1193 /* Walk through the children, reconstructing them all. */
1194 while (!VEC_empty (varobj_p
, stack
))
1196 v
= VEC_pop (varobj_p
, stack
);
1198 /* Push any children. Use reverse order so that the first
1199 child is popped from the work stack first, and so
1200 will be added to result first. This does not
1201 affect correctness, just "nicer". */
1202 for (i
= VEC_length (varobj_p
, v
->children
)-1; i
>= 0; --i
)
1204 varobj_p c
= VEC_index (varobj_p
, v
->children
, i
);
1205 /* Child may be NULL if explicitly deleted by -var-delete. */
1206 if (c
!= NULL
&& !c
->frozen
)
1207 VEC_safe_push (varobj_p
, stack
, c
);
1210 /* Update this variable, unless it's a root, which is already
1212 if (v
->root
->rootvar
!= v
)
1214 new = value_of_child (v
->parent
, v
->index
);
1215 if (install_new_value (v
, new, 0 /* type not changed */))
1217 /* Note that it's changed */
1218 varobj_update_result r
= {v
};
1220 VEC_safe_push (varobj_update_result
, result
, &r
);
1226 VEC_free (varobj_p
, stack
);
1231 /* Helper functions */
1234 * Variable object construction/destruction
1238 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
1239 int only_children_p
)
1243 delete_variable_1 (resultp
, &delcount
, var
,
1244 only_children_p
, 1 /* remove_from_parent_p */ );
1249 /* Delete the variable object VAR and its children */
1250 /* IMPORTANT NOTE: If we delete a variable which is a child
1251 and the parent is not removed we dump core. It must be always
1252 initially called with remove_from_parent_p set */
1254 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
1255 struct varobj
*var
, int only_children_p
,
1256 int remove_from_parent_p
)
1260 /* Delete any children of this variable, too. */
1261 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
1263 varobj_p child
= VEC_index (varobj_p
, var
->children
, i
);
1266 if (!remove_from_parent_p
)
1267 child
->parent
= NULL
;
1268 delete_variable_1 (resultp
, delcountp
, child
, 0, only_children_p
);
1270 VEC_free (varobj_p
, var
->children
);
1272 /* if we were called to delete only the children we are done here */
1273 if (only_children_p
)
1276 /* Otherwise, add it to the list of deleted ones and proceed to do so */
1277 /* If the name is null, this is a temporary variable, that has not
1278 yet been installed, don't report it, it belongs to the caller... */
1279 if (var
->obj_name
!= NULL
)
1281 cppush (resultp
, xstrdup (var
->obj_name
));
1282 *delcountp
= *delcountp
+ 1;
1285 /* If this variable has a parent, remove it from its parent's list */
1286 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1287 (as indicated by remove_from_parent_p) we don't bother doing an
1288 expensive list search to find the element to remove when we are
1289 discarding the list afterwards */
1290 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1292 VEC_replace (varobj_p
, var
->parent
->children
, var
->index
, NULL
);
1295 if (var
->obj_name
!= NULL
)
1296 uninstall_variable (var
);
1298 /* Free memory associated with this variable */
1299 free_variable (var
);
1302 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1304 install_variable (struct varobj
*var
)
1307 struct vlist
*newvl
;
1309 unsigned int index
= 0;
1312 for (chp
= var
->obj_name
; *chp
; chp
++)
1314 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1317 cv
= *(varobj_table
+ index
);
1318 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1322 error (_("Duplicate variable object name"));
1324 /* Add varobj to hash table */
1325 newvl
= xmalloc (sizeof (struct vlist
));
1326 newvl
->next
= *(varobj_table
+ index
);
1328 *(varobj_table
+ index
) = newvl
;
1330 /* If root, add varobj to root list */
1331 if (is_root_p (var
))
1333 /* Add to list of root variables */
1334 if (rootlist
== NULL
)
1335 var
->root
->next
= NULL
;
1337 var
->root
->next
= rootlist
;
1338 rootlist
= var
->root
;
1345 /* Unistall the object VAR. */
1347 uninstall_variable (struct varobj
*var
)
1351 struct varobj_root
*cr
;
1352 struct varobj_root
*prer
;
1354 unsigned int index
= 0;
1357 /* Remove varobj from hash table */
1358 for (chp
= var
->obj_name
; *chp
; chp
++)
1360 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1363 cv
= *(varobj_table
+ index
);
1365 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1372 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
1377 ("Assertion failed: Could not find variable object \"%s\" to delete",
1383 *(varobj_table
+ index
) = cv
->next
;
1385 prev
->next
= cv
->next
;
1389 /* If root, remove varobj from root list */
1390 if (is_root_p (var
))
1392 /* Remove from list of root variables */
1393 if (rootlist
== var
->root
)
1394 rootlist
= var
->root
->next
;
1399 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
1407 ("Assertion failed: Could not find varobj \"%s\" in root list",
1414 prer
->next
= cr
->next
;
1421 /* Create and install a child of the parent of the given name */
1422 static struct varobj
*
1423 create_child (struct varobj
*parent
, int index
, char *name
)
1425 struct varobj
*child
;
1427 struct value
*value
;
1429 child
= new_variable ();
1431 /* name is allocated by name_of_child */
1433 child
->index
= index
;
1434 value
= value_of_child (parent
, index
);
1435 child
->parent
= parent
;
1436 child
->root
= parent
->root
;
1437 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, name
);
1438 child
->obj_name
= childs_name
;
1439 install_variable (child
);
1441 /* Compute the type of the child. Must do this before
1442 calling install_new_value. */
1444 /* If the child had no evaluation errors, var->value
1445 will be non-NULL and contain a valid type. */
1446 child
->type
= value_type (value
);
1448 /* Otherwise, we must compute the type. */
1449 child
->type
= (*child
->root
->lang
->type_of_child
) (child
->parent
,
1451 install_new_value (child
, value
, 1);
1458 * Miscellaneous utility functions.
1461 /* Allocate memory and initialize a new variable */
1462 static struct varobj
*
1467 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
1469 var
->path_expr
= NULL
;
1470 var
->obj_name
= NULL
;
1474 var
->num_children
= -1;
1476 var
->children
= NULL
;
1480 var
->print_value
= NULL
;
1482 var
->not_fetched
= 0;
1487 /* Allocate memory and initialize a new root variable */
1488 static struct varobj
*
1489 new_root_variable (void)
1491 struct varobj
*var
= new_variable ();
1492 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));;
1493 var
->root
->lang
= NULL
;
1494 var
->root
->exp
= NULL
;
1495 var
->root
->valid_block
= NULL
;
1496 var
->root
->frame
= null_frame_id
;
1497 var
->root
->floating
= 0;
1498 var
->root
->rootvar
= NULL
;
1499 var
->root
->is_valid
= 1;
1504 /* Free any allocated memory associated with VAR. */
1506 free_variable (struct varobj
*var
)
1508 /* Free the expression if this is a root variable. */
1509 if (is_root_p (var
))
1511 free_current_contents (&var
->root
->exp
);
1516 xfree (var
->obj_name
);
1517 xfree (var
->print_value
);
1518 xfree (var
->path_expr
);
1523 do_free_variable_cleanup (void *var
)
1525 free_variable (var
);
1528 static struct cleanup
*
1529 make_cleanup_free_variable (struct varobj
*var
)
1531 return make_cleanup (do_free_variable_cleanup
, var
);
1534 /* This returns the type of the variable. It also skips past typedefs
1535 to return the real type of the variable.
1537 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1538 except within get_target_type and get_type. */
1539 static struct type
*
1540 get_type (struct varobj
*var
)
1546 type
= check_typedef (type
);
1551 /* Return the type of the value that's stored in VAR,
1552 or that would have being stored there if the
1553 value were accessible.
1555 This differs from VAR->type in that VAR->type is always
1556 the true type of the expession in the source language.
1557 The return value of this function is the type we're
1558 actually storing in varobj, and using for displaying
1559 the values and for comparing previous and new values.
1561 For example, top-level references are always stripped. */
1562 static struct type
*
1563 get_value_type (struct varobj
*var
)
1568 type
= value_type (var
->value
);
1572 type
= check_typedef (type
);
1574 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
1575 type
= get_target_type (type
);
1577 type
= check_typedef (type
);
1582 /* This returns the target type (or NULL) of TYPE, also skipping
1583 past typedefs, just like get_type ().
1585 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1586 except within get_target_type and get_type. */
1587 static struct type
*
1588 get_target_type (struct type
*type
)
1592 type
= TYPE_TARGET_TYPE (type
);
1594 type
= check_typedef (type
);
1600 /* What is the default display for this variable? We assume that
1601 everything is "natural". Any exceptions? */
1602 static enum varobj_display_formats
1603 variable_default_display (struct varobj
*var
)
1605 return FORMAT_NATURAL
;
1608 /* FIXME: The following should be generic for any pointer */
1610 cppush (struct cpstack
**pstack
, char *name
)
1614 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
1620 /* FIXME: The following should be generic for any pointer */
1622 cppop (struct cpstack
**pstack
)
1627 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
1632 *pstack
= (*pstack
)->next
;
1639 * Language-dependencies
1642 /* Common entry points */
1644 /* Get the language of variable VAR. */
1645 static enum varobj_languages
1646 variable_language (struct varobj
*var
)
1648 enum varobj_languages lang
;
1650 switch (var
->root
->exp
->language_defn
->la_language
)
1656 case language_cplus
:
1667 /* Return the number of children for a given variable.
1668 The result of this function is defined by the language
1669 implementation. The number of children returned by this function
1670 is the number of children that the user will see in the variable
1673 number_of_children (struct varobj
*var
)
1675 return (*var
->root
->lang
->number_of_children
) (var
);;
1678 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1680 name_of_variable (struct varobj
*var
)
1682 return (*var
->root
->lang
->name_of_variable
) (var
);
1685 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1687 name_of_child (struct varobj
*var
, int index
)
1689 return (*var
->root
->lang
->name_of_child
) (var
, index
);
1692 /* What is the ``struct value *'' of the root variable VAR?
1693 For floating variable object, evaluation can get us a value
1694 of different type from what is stored in varobj already. In
1696 - *type_changed will be set to 1
1697 - old varobj will be freed, and new one will be
1698 created, with the same name.
1699 - *var_handle will be set to the new varobj
1700 Otherwise, *type_changed will be set to 0. */
1701 static struct value
*
1702 value_of_root (struct varobj
**var_handle
, int *type_changed
)
1706 if (var_handle
== NULL
)
1711 /* This should really be an exception, since this should
1712 only get called with a root variable. */
1714 if (!is_root_p (var
))
1717 if (var
->root
->floating
)
1719 struct varobj
*tmp_var
;
1720 char *old_type
, *new_type
;
1722 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
1723 USE_SELECTED_FRAME
);
1724 if (tmp_var
== NULL
)
1728 old_type
= varobj_get_type (var
);
1729 new_type
= varobj_get_type (tmp_var
);
1730 if (strcmp (old_type
, new_type
) == 0)
1732 /* The expression presently stored inside var->root->exp
1733 remembers the locations of local variables relatively to
1734 the frame where the expression was created (in DWARF location
1735 button, for example). Naturally, those locations are not
1736 correct in other frames, so update the expression. */
1738 struct expression
*tmp_exp
= var
->root
->exp
;
1739 var
->root
->exp
= tmp_var
->root
->exp
;
1740 tmp_var
->root
->exp
= tmp_exp
;
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
, enum varobj_display_formats format
)
1782 if (var
->root
->is_valid
)
1783 return (*var
->root
->lang
->value_of_variable
) (var
, format
);
1789 value_get_print_value (struct value
*value
, enum varobj_display_formats format
)
1792 struct ui_file
*stb
;
1793 struct cleanup
*old_chain
;
1795 struct value_print_options opts
;
1800 stb
= mem_fileopen ();
1801 old_chain
= make_cleanup_ui_file_delete (stb
);
1803 get_formatted_print_options (&opts
, format_code
[(int) format
]);
1805 common_val_print (value
, stb
, 0, &opts
, current_language
);
1806 thevalue
= ui_file_xstrdup (stb
, &dummy
);
1808 do_cleanups (old_chain
);
1813 varobj_editable_p (struct varobj
*var
)
1816 struct value
*value
;
1818 if (!(var
->root
->is_valid
&& var
->value
&& VALUE_LVAL (var
->value
)))
1821 type
= get_value_type (var
);
1823 switch (TYPE_CODE (type
))
1825 case TYPE_CODE_STRUCT
:
1826 case TYPE_CODE_UNION
:
1827 case TYPE_CODE_ARRAY
:
1828 case TYPE_CODE_FUNC
:
1829 case TYPE_CODE_METHOD
:
1839 /* Return non-zero if changes in value of VAR
1840 must be detected and reported by -var-update.
1841 Return zero is -var-update should never report
1842 changes of such values. This makes sense for structures
1843 (since the changes in children values will be reported separately),
1844 or for artifical objects (like 'public' pseudo-field in C++).
1846 Return value of 0 means that gdb need not call value_fetch_lazy
1847 for the value of this variable object. */
1849 varobj_value_is_changeable_p (struct varobj
*var
)
1854 if (CPLUS_FAKE_CHILD (var
))
1857 type
= get_value_type (var
);
1859 switch (TYPE_CODE (type
))
1861 case TYPE_CODE_STRUCT
:
1862 case TYPE_CODE_UNION
:
1863 case TYPE_CODE_ARRAY
:
1874 /* Return 1 if that varobj is floating, that is is always evaluated in the
1875 selected frame, and not bound to thread/frame. Such variable objects
1876 are created using '@' as frame specifier to -var-create. */
1878 varobj_floating_p (struct varobj
*var
)
1880 return var
->root
->floating
;
1883 /* Given the value and the type of a variable object,
1884 adjust the value and type to those necessary
1885 for getting children of the variable object.
1886 This includes dereferencing top-level references
1887 to all types and dereferencing pointers to
1890 Both TYPE and *TYPE should be non-null. VALUE
1891 can be null if we want to only translate type.
1892 *VALUE can be null as well -- if the parent
1895 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
1896 depending on whether pointer was deferenced
1897 in this function. */
1899 adjust_value_for_child_access (struct value
**value
,
1903 gdb_assert (type
&& *type
);
1908 *type
= check_typedef (*type
);
1910 /* The type of value stored in varobj, that is passed
1911 to us, is already supposed to be
1912 reference-stripped. */
1914 gdb_assert (TYPE_CODE (*type
) != TYPE_CODE_REF
);
1916 /* Pointers to structures are treated just like
1917 structures when accessing children. Don't
1918 dererences pointers to other types. */
1919 if (TYPE_CODE (*type
) == TYPE_CODE_PTR
)
1921 struct type
*target_type
= get_target_type (*type
);
1922 if (TYPE_CODE (target_type
) == TYPE_CODE_STRUCT
1923 || TYPE_CODE (target_type
) == TYPE_CODE_UNION
)
1925 if (value
&& *value
)
1927 int success
= gdb_value_ind (*value
, value
);
1931 *type
= target_type
;
1937 /* The 'get_target_type' function calls check_typedef on
1938 result, so we can immediately check type code. No
1939 need to call check_typedef here. */
1944 c_number_of_children (struct varobj
*var
)
1946 struct type
*type
= get_value_type (var
);
1948 struct type
*target
;
1950 adjust_value_for_child_access (NULL
, &type
, NULL
);
1951 target
= get_target_type (type
);
1953 switch (TYPE_CODE (type
))
1955 case TYPE_CODE_ARRAY
:
1956 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
1957 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
1958 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
1960 /* If we don't know how many elements there are, don't display
1965 case TYPE_CODE_STRUCT
:
1966 case TYPE_CODE_UNION
:
1967 children
= TYPE_NFIELDS (type
);
1971 /* The type here is a pointer to non-struct. Typically, pointers
1972 have one child, except for function ptrs, which have no children,
1973 and except for void*, as we don't know what to show.
1975 We can show char* so we allow it to be dereferenced. If you decide
1976 to test for it, please mind that a little magic is necessary to
1977 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1978 TYPE_NAME == "char" */
1979 if (TYPE_CODE (target
) == TYPE_CODE_FUNC
1980 || TYPE_CODE (target
) == TYPE_CODE_VOID
)
1987 /* Other types have no children */
1995 c_name_of_variable (struct varobj
*parent
)
1997 return savestring (parent
->name
, strlen (parent
->name
));
2000 /* Return the value of element TYPE_INDEX of a structure
2001 value VALUE. VALUE's type should be a structure,
2002 or union, or a typedef to struct/union.
2004 Returns NULL if getting the value fails. Never throws. */
2005 static struct value
*
2006 value_struct_element_index (struct value
*value
, int type_index
)
2008 struct value
*result
= NULL
;
2009 volatile struct gdb_exception e
;
2011 struct type
*type
= value_type (value
);
2012 type
= check_typedef (type
);
2014 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
2015 || TYPE_CODE (type
) == TYPE_CODE_UNION
);
2017 TRY_CATCH (e
, RETURN_MASK_ERROR
)
2019 if (field_is_static (&TYPE_FIELD (type
, type_index
)))
2020 result
= value_static_field (type
, type_index
);
2022 result
= value_primitive_field (value
, 0, type_index
, type
);
2034 /* Obtain the information about child INDEX of the variable
2036 If CNAME is not null, sets *CNAME to the name of the child relative
2038 If CVALUE is not null, sets *CVALUE to the value of the child.
2039 If CTYPE is not null, sets *CTYPE to the type of the child.
2041 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
2042 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
2045 c_describe_child (struct varobj
*parent
, int index
,
2046 char **cname
, struct value
**cvalue
, struct type
**ctype
,
2047 char **cfull_expression
)
2049 struct value
*value
= parent
->value
;
2050 struct type
*type
= get_value_type (parent
);
2051 char *parent_expression
= NULL
;
2060 if (cfull_expression
)
2062 *cfull_expression
= NULL
;
2063 parent_expression
= varobj_get_path_expr (parent
);
2065 adjust_value_for_child_access (&value
, &type
, &was_ptr
);
2067 switch (TYPE_CODE (type
))
2069 case TYPE_CODE_ARRAY
:
2071 *cname
= xstrprintf ("%d", index
2072 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
2074 if (cvalue
&& value
)
2076 int real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
2077 struct value
*indval
=
2078 value_from_longest (builtin_type_int32
, (LONGEST
) real_index
);
2079 gdb_value_subscript (value
, indval
, cvalue
);
2083 *ctype
= get_target_type (type
);
2085 if (cfull_expression
)
2086 *cfull_expression
= xstrprintf ("(%s)[%d]", parent_expression
,
2088 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)));
2093 case TYPE_CODE_STRUCT
:
2094 case TYPE_CODE_UNION
:
2097 char *string
= TYPE_FIELD_NAME (type
, index
);
2098 *cname
= savestring (string
, strlen (string
));
2101 if (cvalue
&& value
)
2103 /* For C, varobj index is the same as type index. */
2104 *cvalue
= value_struct_element_index (value
, index
);
2108 *ctype
= TYPE_FIELD_TYPE (type
, index
);
2110 if (cfull_expression
)
2112 char *join
= was_ptr
? "->" : ".";
2113 *cfull_expression
= xstrprintf ("(%s)%s%s", parent_expression
, join
,
2114 TYPE_FIELD_NAME (type
, index
));
2121 *cname
= xstrprintf ("*%s", parent
->name
);
2123 if (cvalue
&& value
)
2125 int success
= gdb_value_ind (value
, cvalue
);
2130 /* Don't use get_target_type because it calls
2131 check_typedef and here, we want to show the true
2132 declared type of the variable. */
2134 *ctype
= TYPE_TARGET_TYPE (type
);
2136 if (cfull_expression
)
2137 *cfull_expression
= xstrprintf ("*(%s)", parent_expression
);
2142 /* This should not happen */
2144 *cname
= xstrdup ("???");
2145 if (cfull_expression
)
2146 *cfull_expression
= xstrdup ("???");
2147 /* Don't set value and type, we don't know then. */
2152 c_name_of_child (struct varobj
*parent
, int index
)
2155 c_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
2160 c_path_expr_of_child (struct varobj
*child
)
2162 c_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
2164 return child
->path_expr
;
2167 /* If frame associated with VAR can be found, switch
2168 to it and return 1. Otherwise, return 0. */
2170 check_scope (struct varobj
*var
)
2172 struct frame_info
*fi
;
2175 fi
= frame_find_by_id (var
->root
->frame
);
2180 CORE_ADDR pc
= get_frame_pc (fi
);
2181 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
2182 pc
>= BLOCK_END (var
->root
->valid_block
))
2190 static struct value
*
2191 c_value_of_root (struct varobj
**var_handle
)
2193 struct value
*new_val
= NULL
;
2194 struct varobj
*var
= *var_handle
;
2195 struct frame_info
*fi
;
2196 int within_scope
= 0;
2197 struct cleanup
*back_to
;
2199 /* Only root variables can be updated... */
2200 if (!is_root_p (var
))
2201 /* Not a root var */
2204 back_to
= make_cleanup_restore_current_thread ();
2206 /* Determine whether the variable is still around. */
2207 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
2209 else if (var
->root
->thread_id
== 0)
2211 /* The program was single-threaded when the variable object was
2212 created. Technically, it's possible that the program became
2213 multi-threaded since then, but we don't support such
2215 within_scope
= check_scope (var
);
2219 ptid_t ptid
= thread_id_to_pid (var
->root
->thread_id
);
2220 if (in_thread_list (ptid
))
2222 switch_to_thread (ptid
);
2223 within_scope
= check_scope (var
);
2229 /* We need to catch errors here, because if evaluate
2230 expression fails we want to just return NULL. */
2231 gdb_evaluate_expression (var
->root
->exp
, &new_val
);
2235 do_cleanups (back_to
);
2240 static struct value
*
2241 c_value_of_child (struct varobj
*parent
, int index
)
2243 struct value
*value
= NULL
;
2244 c_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
2249 static struct type
*
2250 c_type_of_child (struct varobj
*parent
, int index
)
2252 struct type
*type
= NULL
;
2253 c_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
2258 c_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2260 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2261 it will print out its children instead of "{...}". So we need to
2262 catch that case explicitly. */
2263 struct type
*type
= get_type (var
);
2265 /* Strip top-level references. */
2266 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
2267 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2269 switch (TYPE_CODE (type
))
2271 case TYPE_CODE_STRUCT
:
2272 case TYPE_CODE_UNION
:
2273 return xstrdup ("{...}");
2276 case TYPE_CODE_ARRAY
:
2279 number
= xstrprintf ("[%d]", var
->num_children
);
2286 if (var
->value
== NULL
)
2288 /* This can happen if we attempt to get the value of a struct
2289 member when the parent is an invalid pointer. This is an
2290 error condition, so we should tell the caller. */
2295 if (var
->not_fetched
&& value_lazy (var
->value
))
2296 /* Frozen variable and no value yet. We don't
2297 implicitly fetch the value. MI response will
2298 use empty string for the value, which is OK. */
2301 gdb_assert (varobj_value_is_changeable_p (var
));
2302 gdb_assert (!value_lazy (var
->value
));
2304 /* If the specified format is the current one,
2305 we can reuse print_value */
2306 if (format
== var
->format
)
2307 return xstrdup (var
->print_value
);
2309 return value_get_print_value (var
->value
, format
);
2319 cplus_number_of_children (struct varobj
*var
)
2322 int children
, dont_know
;
2327 if (!CPLUS_FAKE_CHILD (var
))
2329 type
= get_value_type (var
);
2330 adjust_value_for_child_access (NULL
, &type
, NULL
);
2332 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
2333 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
2337 cplus_class_num_children (type
, kids
);
2338 if (kids
[v_public
] != 0)
2340 if (kids
[v_private
] != 0)
2342 if (kids
[v_protected
] != 0)
2345 /* Add any baseclasses */
2346 children
+= TYPE_N_BASECLASSES (type
);
2349 /* FIXME: save children in var */
2356 type
= get_value_type (var
->parent
);
2357 adjust_value_for_child_access (NULL
, &type
, NULL
);
2359 cplus_class_num_children (type
, kids
);
2360 if (strcmp (var
->name
, "public") == 0)
2361 children
= kids
[v_public
];
2362 else if (strcmp (var
->name
, "private") == 0)
2363 children
= kids
[v_private
];
2365 children
= kids
[v_protected
];
2370 children
= c_number_of_children (var
);
2375 /* Compute # of public, private, and protected variables in this class.
2376 That means we need to descend into all baseclasses and find out
2377 how many are there, too. */
2379 cplus_class_num_children (struct type
*type
, int children
[3])
2383 children
[v_public
] = 0;
2384 children
[v_private
] = 0;
2385 children
[v_protected
] = 0;
2387 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
2389 /* If we have a virtual table pointer, omit it. */
2390 if (TYPE_VPTR_BASETYPE (type
) == type
&& TYPE_VPTR_FIELDNO (type
) == i
)
2393 if (TYPE_FIELD_PROTECTED (type
, i
))
2394 children
[v_protected
]++;
2395 else if (TYPE_FIELD_PRIVATE (type
, i
))
2396 children
[v_private
]++;
2398 children
[v_public
]++;
2403 cplus_name_of_variable (struct varobj
*parent
)
2405 return c_name_of_variable (parent
);
2408 enum accessibility
{ private_field
, protected_field
, public_field
};
2410 /* Check if field INDEX of TYPE has the specified accessibility.
2411 Return 0 if so and 1 otherwise. */
2413 match_accessibility (struct type
*type
, int index
, enum accessibility acc
)
2415 if (acc
== private_field
&& TYPE_FIELD_PRIVATE (type
, index
))
2417 else if (acc
== protected_field
&& TYPE_FIELD_PROTECTED (type
, index
))
2419 else if (acc
== public_field
&& !TYPE_FIELD_PRIVATE (type
, index
)
2420 && !TYPE_FIELD_PROTECTED (type
, index
))
2427 cplus_describe_child (struct varobj
*parent
, int index
,
2428 char **cname
, struct value
**cvalue
, struct type
**ctype
,
2429 char **cfull_expression
)
2432 struct value
*value
;
2435 char *parent_expression
= NULL
;
2443 if (cfull_expression
)
2444 *cfull_expression
= NULL
;
2446 if (CPLUS_FAKE_CHILD (parent
))
2448 value
= parent
->parent
->value
;
2449 type
= get_value_type (parent
->parent
);
2450 if (cfull_expression
)
2451 parent_expression
= varobj_get_path_expr (parent
->parent
);
2455 value
= parent
->value
;
2456 type
= get_value_type (parent
);
2457 if (cfull_expression
)
2458 parent_expression
= varobj_get_path_expr (parent
);
2461 adjust_value_for_child_access (&value
, &type
, &was_ptr
);
2463 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
2464 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
2466 char *join
= was_ptr
? "->" : ".";
2467 if (CPLUS_FAKE_CHILD (parent
))
2469 /* The fields of the class type are ordered as they
2470 appear in the class. We are given an index for a
2471 particular access control type ("public","protected",
2472 or "private"). We must skip over fields that don't
2473 have the access control we are looking for to properly
2474 find the indexed field. */
2475 int type_index
= TYPE_N_BASECLASSES (type
);
2476 enum accessibility acc
= public_field
;
2477 if (strcmp (parent
->name
, "private") == 0)
2478 acc
= private_field
;
2479 else if (strcmp (parent
->name
, "protected") == 0)
2480 acc
= protected_field
;
2484 if (TYPE_VPTR_BASETYPE (type
) == type
2485 && type_index
== TYPE_VPTR_FIELDNO (type
))
2487 else if (match_accessibility (type
, type_index
, acc
))
2494 *cname
= xstrdup (TYPE_FIELD_NAME (type
, type_index
));
2496 if (cvalue
&& value
)
2497 *cvalue
= value_struct_element_index (value
, type_index
);
2500 *ctype
= TYPE_FIELD_TYPE (type
, type_index
);
2502 if (cfull_expression
)
2503 *cfull_expression
= xstrprintf ("((%s)%s%s)", parent_expression
,
2505 TYPE_FIELD_NAME (type
, type_index
));
2507 else if (index
< TYPE_N_BASECLASSES (type
))
2509 /* This is a baseclass. */
2511 *cname
= xstrdup (TYPE_FIELD_NAME (type
, index
));
2513 if (cvalue
&& value
)
2515 *cvalue
= value_cast (TYPE_FIELD_TYPE (type
, index
), value
);
2516 release_value (*cvalue
);
2521 *ctype
= TYPE_FIELD_TYPE (type
, index
);
2524 if (cfull_expression
)
2526 char *ptr
= was_ptr
? "*" : "";
2527 /* Cast the parent to the base' type. Note that in gdb,
2530 will create an lvalue, for all appearences, so we don't
2531 need to use more fancy:
2534 *cfull_expression
= xstrprintf ("(%s(%s%s) %s)",
2536 TYPE_FIELD_NAME (type
, index
),
2543 char *access
= NULL
;
2545 cplus_class_num_children (type
, children
);
2547 /* Everything beyond the baseclasses can
2548 only be "public", "private", or "protected"
2550 The special "fake" children are always output by varobj in
2551 this order. So if INDEX == 2, it MUST be "protected". */
2552 index
-= TYPE_N_BASECLASSES (type
);
2556 if (children
[v_public
] > 0)
2558 else if (children
[v_private
] > 0)
2561 access
= "protected";
2564 if (children
[v_public
] > 0)
2566 if (children
[v_private
] > 0)
2569 access
= "protected";
2571 else if (children
[v_private
] > 0)
2572 access
= "protected";
2575 /* Must be protected */
2576 access
= "protected";
2583 gdb_assert (access
);
2585 *cname
= xstrdup (access
);
2587 /* Value and type and full expression are null here. */
2592 c_describe_child (parent
, index
, cname
, cvalue
, ctype
, cfull_expression
);
2597 cplus_name_of_child (struct varobj
*parent
, int index
)
2600 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
2605 cplus_path_expr_of_child (struct varobj
*child
)
2607 cplus_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
2609 return child
->path_expr
;
2612 static struct value
*
2613 cplus_value_of_root (struct varobj
**var_handle
)
2615 return c_value_of_root (var_handle
);
2618 static struct value
*
2619 cplus_value_of_child (struct varobj
*parent
, int index
)
2621 struct value
*value
= NULL
;
2622 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
2626 static struct type
*
2627 cplus_type_of_child (struct varobj
*parent
, int index
)
2629 struct type
*type
= NULL
;
2630 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
2635 cplus_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2638 /* If we have one of our special types, don't print out
2640 if (CPLUS_FAKE_CHILD (var
))
2641 return xstrdup ("");
2643 return c_value_of_variable (var
, format
);
2649 java_number_of_children (struct varobj
*var
)
2651 return cplus_number_of_children (var
);
2655 java_name_of_variable (struct varobj
*parent
)
2659 name
= cplus_name_of_variable (parent
);
2660 /* If the name has "-" in it, it is because we
2661 needed to escape periods in the name... */
2664 while (*p
!= '\000')
2675 java_name_of_child (struct varobj
*parent
, int index
)
2679 name
= cplus_name_of_child (parent
, index
);
2680 /* Escape any periods in the name... */
2683 while (*p
!= '\000')
2694 java_path_expr_of_child (struct varobj
*child
)
2699 static struct value
*
2700 java_value_of_root (struct varobj
**var_handle
)
2702 return cplus_value_of_root (var_handle
);
2705 static struct value
*
2706 java_value_of_child (struct varobj
*parent
, int index
)
2708 return cplus_value_of_child (parent
, index
);
2711 static struct type
*
2712 java_type_of_child (struct varobj
*parent
, int index
)
2714 return cplus_type_of_child (parent
, index
);
2718 java_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2720 return cplus_value_of_variable (var
, format
);
2723 extern void _initialize_varobj (void);
2725 _initialize_varobj (void)
2727 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
2729 varobj_table
= xmalloc (sizeof_table
);
2730 memset (varobj_table
, 0, sizeof_table
);
2732 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance
,
2734 Set varobj debugging."), _("\
2735 Show varobj debugging."), _("\
2736 When non-zero, varobj debugging is enabled."),
2739 &setlist
, &showlist
);
2742 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2743 are defined on globals.
2744 Invalidated varobjs will be always printed in_scope="invalid". */
2746 varobj_invalidate (void)
2748 struct varobj
**all_rootvarobj
;
2749 struct varobj
**varp
;
2751 if (varobj_list (&all_rootvarobj
) > 0)
2753 varp
= all_rootvarobj
;
2754 while (*varp
!= NULL
)
2756 /* Floating varobjs are reparsed on each stop, so we don't care if
2757 the presently parsed expression refers to something that's gone. */
2758 if ((*varp
)->root
->floating
)
2761 /* global var must be re-evaluated. */
2762 if ((*varp
)->root
->valid_block
== NULL
)
2764 struct varobj
*tmp_var
;
2766 /* Try to create a varobj with same expression. If we succeed replace
2767 the old varobj, otherwise invalidate it. */
2768 tmp_var
= varobj_create (NULL
, (*varp
)->name
, (CORE_ADDR
) 0, USE_CURRENT_FRAME
);
2769 if (tmp_var
!= NULL
)
2771 tmp_var
->obj_name
= xstrdup ((*varp
)->obj_name
);
2772 varobj_delete (*varp
, NULL
, 0);
2773 install_variable (tmp_var
);
2776 (*varp
)->root
->is_valid
= 0;
2778 else /* locals must be invalidated. */
2779 (*varp
)->root
->is_valid
= 0;
2783 xfree (all_rootvarobj
);