1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2012 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "exceptions.h"
21 #include "expression.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include "gdb_regex.h"
34 #include "gdbthread.h"
36 #include "ada-varobj.h"
40 #include "python/python.h"
41 #include "python/python-internal.h"
46 /* The names of varobjs representing anonymous structs or unions. */
47 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
48 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
50 /* Non-zero if we want to see trace of varobj level stuff. */
54 show_varobjdebug (struct ui_file *file, int from_tty,
55 struct cmd_list_element *c, const char *value)
57 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
60 /* String representations of gdb's format codes. */
61 char *varobj_format_string[] =
62 { "natural", "binary", "decimal", "hexadecimal", "octal" };
64 /* String representations of gdb's known languages. */
65 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
67 /* True if we want to allow Python-based pretty-printing. */
68 static int pretty_printing = 0;
71 varobj_enable_pretty_printing (void)
78 /* Every root variable has one of these structures saved in its
79 varobj. Members which must be free'd are noted. */
83 /* Alloc'd expression for this parent. */
84 struct expression *exp;
86 /* Block for which this expression is valid. */
87 struct block *valid_block;
89 /* The frame for this expression. This field is set iff valid_block is
91 struct frame_id frame;
93 /* The thread ID that this varobj_root belong to. This field
94 is only valid if valid_block is not NULL.
95 When not 0, indicates which thread 'frame' belongs to.
96 When 0, indicates that the thread list was empty when the varobj_root
100 /* If 1, the -var-update always recomputes the value in the
101 current thread and frame. Otherwise, variable object is
102 always updated in the specific scope/thread/frame. */
105 /* Flag that indicates validity: set to 0 when this varobj_root refers
106 to symbols that do not exist anymore. */
109 /* Language info for this variable and its children. */
110 struct language_specific *lang;
112 /* The varobj for this root node. */
113 struct varobj *rootvar;
115 /* Next root variable */
116 struct varobj_root *next;
119 /* Every variable in the system has a structure of this type defined
120 for it. This structure holds all information necessary to manipulate
121 a particular object variable. Members which must be freed are noted. */
125 /* Alloc'd name of the variable for this object. If this variable is a
126 child, then this name will be the child's source name.
127 (bar, not foo.bar). */
128 /* NOTE: This is the "expression". */
131 /* Alloc'd expression for this child. Can be used to create a
132 root variable corresponding to this child. */
135 /* The alloc'd name for this variable's object. This is here for
136 convenience when constructing this object's children. */
139 /* Index of this variable in its parent or -1. */
142 /* The type of this variable. This can be NULL
143 for artifial variable objects -- currently, the "accessibility"
144 variable objects in C++. */
147 /* The value of this expression or subexpression. A NULL value
148 indicates there was an error getting this value.
149 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
150 the value is either NULL, or not lazy. */
153 /* The number of (immediate) children this variable has. */
156 /* If this object is a child, this points to its immediate parent. */
157 struct varobj *parent;
159 /* Children of this object. */
160 VEC (varobj_p) *children;
162 /* Whether the children of this varobj were requested. This field is
163 used to decide if dynamic varobj should recompute their children.
164 In the event that the frontend never asked for the children, we
166 int children_requested;
168 /* Description of the root variable. Points to root variable for
170 struct varobj_root *root;
172 /* The format of the output for this object. */
173 enum varobj_display_formats format;
175 /* Was this variable updated via a varobj_set_value operation. */
178 /* Last print value. */
181 /* Is this variable frozen. Frozen variables are never implicitly
182 updated by -var-update *
183 or -var-update <direct-or-indirect-parent>. */
186 /* Is the value of this variable intentionally not fetched? It is
187 not fetched if either the variable is frozen, or any parents is
191 /* Sub-range of children which the MI consumer has requested. If
192 FROM < 0 or TO < 0, means that all children have been
197 /* The pretty-printer constructor. If NULL, then the default
198 pretty-printer will be looked up. If None, then no
199 pretty-printer will be installed. */
200 PyObject *constructor;
202 /* The pretty-printer that has been constructed. If NULL, then a
203 new printer object is needed, and one will be constructed. */
204 PyObject *pretty_printer;
206 /* The iterator returned by the printer's 'children' method, or NULL
208 PyObject *child_iter;
210 /* We request one extra item from the iterator, so that we can
211 report to the caller whether there are more items than we have
212 already reported. However, we don't want to install this value
213 when we read it, because that will mess up future updates. So,
214 we stash it here instead. */
215 PyObject *saved_item;
221 struct cpstack *next;
224 /* A list of varobjs */
232 /* Private function prototypes */
234 /* Helper functions for the above subcommands. */
236 static int delete_variable (struct cpstack **, struct varobj *, int);
238 static void delete_variable_1 (struct cpstack **, int *,
239 struct varobj *, int, int);
241 static int install_variable (struct varobj *);
243 static void uninstall_variable (struct varobj *);
245 static struct varobj *create_child (struct varobj *, int, char *);
247 static struct varobj *
248 create_child_with_value (struct varobj *parent, int index, const char *name,
249 struct value *value);
251 /* Utility routines */
253 static struct varobj *new_variable (void);
255 static struct varobj *new_root_variable (void);
257 static void free_variable (struct varobj *var);
259 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
261 static struct type *get_type (struct varobj *var);
263 static struct type *get_value_type (struct varobj *var);
265 static struct type *get_target_type (struct type *);
267 static enum varobj_display_formats variable_default_display (struct varobj *);
269 static void cppush (struct cpstack **pstack, char *name);
271 static char *cppop (struct cpstack **pstack);
273 static int install_new_value (struct varobj *var, struct value *value,
276 /* Language-specific routines. */
278 static enum varobj_languages variable_language (struct varobj *var);
280 static int number_of_children (struct varobj *);
282 static char *name_of_variable (struct varobj *);
284 static char *name_of_child (struct varobj *, int);
286 static struct value *value_of_root (struct varobj **var_handle, int *);
288 static struct value *value_of_child (struct varobj *parent, int index);
290 static char *my_value_of_variable (struct varobj *var,
291 enum varobj_display_formats format);
293 static char *value_get_print_value (struct value *value,
294 enum varobj_display_formats format,
297 static int varobj_value_is_changeable_p (struct varobj *var);
299 static int is_root_p (struct varobj *var);
303 static struct varobj *varobj_add_child (struct varobj *var,
305 struct value *value);
307 #endif /* HAVE_PYTHON */
309 static int default_value_is_changeable_p (struct varobj *var);
311 /* C implementation */
313 static int c_number_of_children (struct varobj *var);
315 static char *c_name_of_variable (struct varobj *parent);
317 static char *c_name_of_child (struct varobj *parent, int index);
319 static char *c_path_expr_of_child (struct varobj *child);
321 static struct value *c_value_of_root (struct varobj **var_handle);
323 static struct value *c_value_of_child (struct varobj *parent, int index);
325 static struct type *c_type_of_child (struct varobj *parent, int index);
327 static char *c_value_of_variable (struct varobj *var,
328 enum varobj_display_formats format);
330 /* C++ implementation */
332 static int cplus_number_of_children (struct varobj *var);
334 static void cplus_class_num_children (struct type *type, int children[3]);
336 static char *cplus_name_of_variable (struct varobj *parent);
338 static char *cplus_name_of_child (struct varobj *parent, int index);
340 static char *cplus_path_expr_of_child (struct varobj *child);
342 static struct value *cplus_value_of_root (struct varobj **var_handle);
344 static struct value *cplus_value_of_child (struct varobj *parent, int index);
346 static struct type *cplus_type_of_child (struct varobj *parent, int index);
348 static char *cplus_value_of_variable (struct varobj *var,
349 enum varobj_display_formats format);
351 /* Java implementation */
353 static int java_number_of_children (struct varobj *var);
355 static char *java_name_of_variable (struct varobj *parent);
357 static char *java_name_of_child (struct varobj *parent, int index);
359 static char *java_path_expr_of_child (struct varobj *child);
361 static struct value *java_value_of_root (struct varobj **var_handle);
363 static struct value *java_value_of_child (struct varobj *parent, int index);
365 static struct type *java_type_of_child (struct varobj *parent, int index);
367 static char *java_value_of_variable (struct varobj *var,
368 enum varobj_display_formats format);
370 /* Ada implementation */
372 static int ada_number_of_children (struct varobj *var);
374 static char *ada_name_of_variable (struct varobj *parent);
376 static char *ada_name_of_child (struct varobj *parent, int index);
378 static char *ada_path_expr_of_child (struct varobj *child);
380 static struct value *ada_value_of_root (struct varobj **var_handle);
382 static struct value *ada_value_of_child (struct varobj *parent, int index);
384 static struct type *ada_type_of_child (struct varobj *parent, int index);
386 static char *ada_value_of_variable (struct varobj *var,
387 enum varobj_display_formats format);
389 static int ada_value_is_changeable_p (struct varobj *var);
391 static int ada_value_has_mutated (struct varobj *var, struct value *new_val,
392 struct type *new_type);
394 /* The language specific vector */
396 struct language_specific
399 /* The language of this variable. */
400 enum varobj_languages language;
402 /* The number of children of PARENT. */
403 int (*number_of_children) (struct varobj * parent);
405 /* The name (expression) of a root varobj. */
406 char *(*name_of_variable) (struct varobj * parent);
408 /* The name of the INDEX'th child of PARENT. */
409 char *(*name_of_child) (struct varobj * parent, int index);
411 /* Returns the rooted expression of CHILD, which is a variable
412 obtain that has some parent. */
413 char *(*path_expr_of_child) (struct varobj * child);
415 /* The ``struct value *'' of the root variable ROOT. */
416 struct value *(*value_of_root) (struct varobj ** root_handle);
418 /* The ``struct value *'' of the INDEX'th child of PARENT. */
419 struct value *(*value_of_child) (struct varobj * parent, int index);
421 /* The type of the INDEX'th child of PARENT. */
422 struct type *(*type_of_child) (struct varobj * parent, int index);
424 /* The current value of VAR. */
425 char *(*value_of_variable) (struct varobj * var,
426 enum varobj_display_formats format);
428 /* Return non-zero if changes in value of VAR must be detected and
429 reported by -var-update. Return zero if -var-update should never
430 report changes of such values. This makes sense for structures
431 (since the changes in children values will be reported separately),
432 or for artifical objects (like 'public' pseudo-field in C++).
434 Return value of 0 means that gdb need not call value_fetch_lazy
435 for the value of this variable object. */
436 int (*value_is_changeable_p) (struct varobj *var);
438 /* Return nonzero if the type of VAR has mutated.
440 VAR's value is still the varobj's previous value, while NEW_VALUE
441 is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE
442 may be NULL indicating that there is no value available (the varobj
443 may be out of scope, of may be the child of a null pointer, for
444 instance). NEW_TYPE, on the other hand, must never be NULL.
446 This function should also be able to assume that var's number of
447 children is set (not < 0).
449 Languages where types do not mutate can set this to NULL. */
450 int (*value_has_mutated) (struct varobj *var, struct value *new_value,
451 struct type *new_type);
454 /* Array of known source language routines. */
455 static struct language_specific languages[vlang_end] = {
456 /* Unknown (try treating as C). */
459 c_number_of_children,
462 c_path_expr_of_child,
467 default_value_is_changeable_p,
468 NULL /* value_has_mutated */}
473 c_number_of_children,
476 c_path_expr_of_child,
481 default_value_is_changeable_p,
482 NULL /* value_has_mutated */}
487 cplus_number_of_children,
488 cplus_name_of_variable,
490 cplus_path_expr_of_child,
492 cplus_value_of_child,
494 cplus_value_of_variable,
495 default_value_is_changeable_p,
496 NULL /* value_has_mutated */}
501 java_number_of_children,
502 java_name_of_variable,
504 java_path_expr_of_child,
508 java_value_of_variable,
509 default_value_is_changeable_p,
510 NULL /* value_has_mutated */},
514 ada_number_of_children,
515 ada_name_of_variable,
517 ada_path_expr_of_child,
521 ada_value_of_variable,
522 ada_value_is_changeable_p,
523 ada_value_has_mutated}
526 /* A little convenience enum for dealing with C++/Java. */
529 v_public = 0, v_private, v_protected
534 /* Mappings of varobj_display_formats enums to gdb's format codes. */
535 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
537 /* Header of the list of root variable objects. */
538 static struct varobj_root *rootlist;
540 /* Prime number indicating the number of buckets in the hash table. */
541 /* A prime large enough to avoid too many colisions. */
542 #define VAROBJ_TABLE_SIZE 227
544 /* Pointer to the varobj hash table (built at run time). */
545 static struct vlist **varobj_table;
547 /* Is the variable X one of our "fake" children? */
548 #define CPLUS_FAKE_CHILD(x) \
549 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
552 /* API Implementation */
554 is_root_p (struct varobj *var)
556 return (var->root->rootvar == var);
560 /* Helper function to install a Python environment suitable for
561 use during operations on VAR. */
562 static struct cleanup *
563 varobj_ensure_python_env (struct varobj *var)
565 return ensure_python_env (var->root->exp->gdbarch,
566 var->root->exp->language_defn);
570 /* Creates a varobj (not its children). */
572 /* Return the full FRAME which corresponds to the given CORE_ADDR
573 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
575 static struct frame_info *
576 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
578 struct frame_info *frame = NULL;
580 if (frame_addr == (CORE_ADDR) 0)
583 for (frame = get_current_frame ();
585 frame = get_prev_frame (frame))
587 /* The CORE_ADDR we get as argument was parsed from a string GDB
588 output as $fp. This output got truncated to gdbarch_addr_bit.
589 Truncate the frame base address in the same manner before
590 comparing it against our argument. */
591 CORE_ADDR frame_base = get_frame_base_address (frame);
592 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
594 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
595 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
597 if (frame_base == frame_addr)
605 varobj_create (char *objname,
606 char *expression, CORE_ADDR frame, enum varobj_type type)
609 struct cleanup *old_chain;
611 /* Fill out a varobj structure for the (root) variable being constructed. */
612 var = new_root_variable ();
613 old_chain = make_cleanup_free_variable (var);
615 if (expression != NULL)
617 struct frame_info *fi;
618 struct frame_id old_id = null_frame_id;
621 enum varobj_languages lang;
622 struct value *value = NULL;
623 volatile struct gdb_exception except;
625 /* Parse and evaluate the expression, filling in as much of the
626 variable's data as possible. */
628 if (has_stack_frames ())
630 /* Allow creator to specify context of variable. */
631 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
632 fi = get_selected_frame (NULL);
634 /* FIXME: cagney/2002-11-23: This code should be doing a
635 lookup using the frame ID and not just the frame's
636 ``address''. This, of course, means an interface
637 change. However, with out that interface change ISAs,
638 such as the ia64 with its two stacks, won't work.
639 Similar goes for the case where there is a frameless
641 fi = find_frame_addr_in_frame_chain (frame);
646 /* frame = -2 means always use selected frame. */
647 if (type == USE_SELECTED_FRAME)
648 var->root->floating = 1;
652 block = get_frame_block (fi, 0);
655 innermost_block = NULL;
656 /* Wrap the call to parse expression, so we can
657 return a sensible error. */
658 TRY_CATCH (except, RETURN_MASK_ERROR)
660 var->root->exp = parse_exp_1 (&p, block, 0);
663 if (except.reason < 0)
665 do_cleanups (old_chain);
669 /* Don't allow variables to be created for types. */
670 if (var->root->exp->elts[0].opcode == OP_TYPE)
672 do_cleanups (old_chain);
673 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
674 " as an expression.\n");
678 var->format = variable_default_display (var);
679 var->root->valid_block = innermost_block;
680 var->name = xstrdup (expression);
681 /* For a root var, the name and the expr are the same. */
682 var->path_expr = xstrdup (expression);
684 /* When the frame is different from the current frame,
685 we must select the appropriate frame before parsing
686 the expression, otherwise the value will not be current.
687 Since select_frame is so benign, just call it for all cases. */
690 /* User could specify explicit FRAME-ADDR which was not found but
691 EXPRESSION is frame specific and we would not be able to evaluate
692 it correctly next time. With VALID_BLOCK set we must also set
693 FRAME and THREAD_ID. */
695 error (_("Failed to find the specified frame"));
697 var->root->frame = get_frame_id (fi);
698 var->root->thread_id = pid_to_thread_id (inferior_ptid);
699 old_id = get_frame_id (get_selected_frame (NULL));
703 /* We definitely need to catch errors here.
704 If evaluate_expression succeeds we got the value we wanted.
705 But if it fails, we still go on with a call to evaluate_type(). */
706 TRY_CATCH (except, RETURN_MASK_ERROR)
708 value = evaluate_expression (var->root->exp);
711 if (except.reason < 0)
713 /* Error getting the value. Try to at least get the
715 struct value *type_only_value = evaluate_type (var->root->exp);
717 var->type = value_type (type_only_value);
720 var->type = value_type (value);
722 /* Set language info */
723 lang = variable_language (var);
724 var->root->lang = &languages[lang];
726 install_new_value (var, value, 1 /* Initial assignment */);
728 /* Set ourselves as our root. */
729 var->root->rootvar = var;
731 /* Reset the selected frame. */
732 if (frame_id_p (old_id))
733 select_frame (frame_find_by_id (old_id));
736 /* If the variable object name is null, that means this
737 is a temporary variable, so don't install it. */
739 if ((var != NULL) && (objname != NULL))
741 var->obj_name = xstrdup (objname);
743 /* If a varobj name is duplicated, the install will fail so
745 if (!install_variable (var))
747 do_cleanups (old_chain);
752 discard_cleanups (old_chain);
756 /* Generates an unique name that can be used for a varobj. */
759 varobj_gen_name (void)
764 /* Generate a name for this object. */
766 obj_name = xstrprintf ("var%d", id);
771 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
772 error if OBJNAME cannot be found. */
775 varobj_get_handle (char *objname)
779 unsigned int index = 0;
782 for (chp = objname; *chp; chp++)
784 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
787 cv = *(varobj_table + index);
788 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
792 error (_("Variable object not found"));
797 /* Given the handle, return the name of the object. */
800 varobj_get_objname (struct varobj *var)
802 return var->obj_name;
805 /* Given the handle, return the expression represented by the object. */
808 varobj_get_expression (struct varobj *var)
810 return name_of_variable (var);
813 /* Deletes a varobj and all its children if only_children == 0,
814 otherwise deletes only the children; returns a malloc'ed list of
815 all the (malloc'ed) names of the variables that have been deleted
816 (NULL terminated). */
819 varobj_delete (struct varobj *var, char ***dellist, int only_children)
823 struct cpstack *result = NULL;
826 /* Initialize a stack for temporary results. */
827 cppush (&result, NULL);
830 /* Delete only the variable children. */
831 delcount = delete_variable (&result, var, 1 /* only the children */ );
833 /* Delete the variable and all its children. */
834 delcount = delete_variable (&result, var, 0 /* parent+children */ );
836 /* We may have been asked to return a list of what has been deleted. */
839 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
843 *cp = cppop (&result);
844 while ((*cp != NULL) && (mycount > 0))
848 *cp = cppop (&result);
851 if (mycount || (*cp != NULL))
852 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
861 /* Convenience function for varobj_set_visualizer. Instantiate a
862 pretty-printer for a given value. */
864 instantiate_pretty_printer (PyObject *constructor, struct value *value)
866 PyObject *val_obj = NULL;
869 val_obj = value_to_value_object (value);
873 printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
880 /* Set/Get variable object display format. */
882 enum varobj_display_formats
883 varobj_set_display_format (struct varobj *var,
884 enum varobj_display_formats format)
891 case FORMAT_HEXADECIMAL:
893 var->format = format;
897 var->format = variable_default_display (var);
900 if (varobj_value_is_changeable_p (var)
901 && var->value && !value_lazy (var->value))
903 xfree (var->print_value);
904 var->print_value = value_get_print_value (var->value, var->format, var);
910 enum varobj_display_formats
911 varobj_get_display_format (struct varobj *var)
917 varobj_get_display_hint (struct varobj *var)
922 struct cleanup *back_to = varobj_ensure_python_env (var);
924 if (var->pretty_printer)
925 result = gdbpy_get_display_hint (var->pretty_printer);
927 do_cleanups (back_to);
933 /* Return true if the varobj has items after TO, false otherwise. */
936 varobj_has_more (struct varobj *var, int to)
938 if (VEC_length (varobj_p, var->children) > to)
940 return ((to == -1 || VEC_length (varobj_p, var->children) == to)
941 && var->saved_item != NULL);
944 /* If the variable object is bound to a specific thread, that
945 is its evaluation can always be done in context of a frame
946 inside that thread, returns GDB id of the thread -- which
947 is always positive. Otherwise, returns -1. */
949 varobj_get_thread_id (struct varobj *var)
951 if (var->root->valid_block && var->root->thread_id > 0)
952 return var->root->thread_id;
958 varobj_set_frozen (struct varobj *var, int frozen)
960 /* When a variable is unfrozen, we don't fetch its value.
961 The 'not_fetched' flag remains set, so next -var-update
964 We don't fetch the value, because for structures the client
965 should do -var-update anyway. It would be bad to have different
966 client-size logic for structure and other types. */
967 var->frozen = frozen;
971 varobj_get_frozen (struct varobj *var)
976 /* A helper function that restricts a range to what is actually
977 available in a VEC. This follows the usual rules for the meaning
978 of FROM and TO -- if either is negative, the entire range is
982 restrict_range (VEC (varobj_p) *children, int *from, int *to)
984 if (*from < 0 || *to < 0)
987 *to = VEC_length (varobj_p, children);
991 if (*from > VEC_length (varobj_p, children))
992 *from = VEC_length (varobj_p, children);
993 if (*to > VEC_length (varobj_p, children))
994 *to = VEC_length (varobj_p, children);
1002 /* A helper for update_dynamic_varobj_children that installs a new
1003 child when needed. */
1006 install_dynamic_child (struct varobj *var,
1007 VEC (varobj_p) **changed,
1008 VEC (varobj_p) **new,
1009 VEC (varobj_p) **unchanged,
1013 struct value *value)
1015 if (VEC_length (varobj_p, var->children) < index + 1)
1017 /* There's no child yet. */
1018 struct varobj *child = varobj_add_child (var, name, value);
1022 VEC_safe_push (varobj_p, *new, child);
1028 varobj_p existing = VEC_index (varobj_p, var->children, index);
1030 if (install_new_value (existing, value, 0))
1033 VEC_safe_push (varobj_p, *changed, existing);
1036 VEC_safe_push (varobj_p, *unchanged, existing);
1041 dynamic_varobj_has_child_method (struct varobj *var)
1043 struct cleanup *back_to;
1044 PyObject *printer = var->pretty_printer;
1047 back_to = varobj_ensure_python_env (var);
1048 result = PyObject_HasAttr (printer, gdbpy_children_cst);
1049 do_cleanups (back_to);
1056 update_dynamic_varobj_children (struct varobj *var,
1057 VEC (varobj_p) **changed,
1058 VEC (varobj_p) **new,
1059 VEC (varobj_p) **unchanged,
1061 int update_children,
1066 struct cleanup *back_to;
1069 PyObject *printer = var->pretty_printer;
1071 back_to = varobj_ensure_python_env (var);
1074 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
1076 do_cleanups (back_to);
1080 if (update_children || !var->child_iter)
1082 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
1087 gdbpy_print_stack ();
1088 error (_("Null value returned for children"));
1091 make_cleanup_py_decref (children);
1093 if (!PyIter_Check (children))
1094 error (_("Returned value is not iterable"));
1096 Py_XDECREF (var->child_iter);
1097 var->child_iter = PyObject_GetIter (children);
1098 if (!var->child_iter)
1100 gdbpy_print_stack ();
1101 error (_("Could not get children iterator"));
1104 Py_XDECREF (var->saved_item);
1105 var->saved_item = NULL;
1110 i = VEC_length (varobj_p, var->children);
1112 /* We ask for one extra child, so that MI can report whether there
1113 are more children. */
1114 for (; to < 0 || i < to + 1; ++i)
1119 /* See if there was a leftover from last time. */
1120 if (var->saved_item)
1122 item = var->saved_item;
1123 var->saved_item = NULL;
1126 item = PyIter_Next (var->child_iter);
1130 /* Normal end of iteration. */
1131 if (!PyErr_Occurred ())
1134 /* If we got a memory error, just use the text as the
1136 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
1138 PyObject *type, *value, *trace;
1139 char *name_str, *value_str;
1141 PyErr_Fetch (&type, &value, &trace);
1142 value_str = gdbpy_exception_to_string (type, value);
1148 gdbpy_print_stack ();
1152 name_str = xstrprintf ("<error at %d>", i);
1153 item = Py_BuildValue ("(ss)", name_str, value_str);
1158 gdbpy_print_stack ();
1166 /* Any other kind of error. */
1167 gdbpy_print_stack ();
1172 /* We don't want to push the extra child on any report list. */
1173 if (to < 0 || i < to)
1178 struct cleanup *inner;
1179 int can_mention = from < 0 || i >= from;
1181 inner = make_cleanup_py_decref (item);
1183 if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
1185 gdbpy_print_stack ();
1186 error (_("Invalid item from the child list"));
1189 v = convert_value_from_python (py_v);
1191 gdbpy_print_stack ();
1192 install_dynamic_child (var, can_mention ? changed : NULL,
1193 can_mention ? new : NULL,
1194 can_mention ? unchanged : NULL,
1195 can_mention ? cchanged : NULL, i, name, v);
1196 do_cleanups (inner);
1200 Py_XDECREF (var->saved_item);
1201 var->saved_item = item;
1203 /* We want to truncate the child list just before this
1212 if (i < VEC_length (varobj_p, var->children))
1217 for (j = i; j < VEC_length (varobj_p, var->children); ++j)
1218 varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
1219 VEC_truncate (varobj_p, var->children, i);
1222 /* If there are fewer children than requested, note that the list of
1223 children changed. */
1224 if (to >= 0 && VEC_length (varobj_p, var->children) < to)
1227 var->num_children = VEC_length (varobj_p, var->children);
1229 do_cleanups (back_to);
1233 gdb_assert (0 && "should never be called if Python is not enabled");
1238 varobj_get_num_children (struct varobj *var)
1240 if (var->num_children == -1)
1242 if (var->pretty_printer)
1246 /* If we have a dynamic varobj, don't report -1 children.
1247 So, try to fetch some children first. */
1248 update_dynamic_varobj_children (var, NULL, NULL, NULL, &dummy,
1252 var->num_children = number_of_children (var);
1255 return var->num_children >= 0 ? var->num_children : 0;
1258 /* Creates a list of the immediate children of a variable object;
1259 the return code is the number of such children or -1 on error. */
1262 varobj_list_children (struct varobj *var, int *from, int *to)
1265 int i, children_changed;
1267 var->children_requested = 1;
1269 if (var->pretty_printer)
1271 /* This, in theory, can result in the number of children changing without
1272 frontend noticing. But well, calling -var-list-children on the same
1273 varobj twice is not something a sane frontend would do. */
1274 update_dynamic_varobj_children (var, NULL, NULL, NULL, &children_changed,
1276 restrict_range (var->children, from, to);
1277 return var->children;
1280 if (var->num_children == -1)
1281 var->num_children = number_of_children (var);
1283 /* If that failed, give up. */
1284 if (var->num_children == -1)
1285 return var->children;
1287 /* If we're called when the list of children is not yet initialized,
1288 allocate enough elements in it. */
1289 while (VEC_length (varobj_p, var->children) < var->num_children)
1290 VEC_safe_push (varobj_p, var->children, NULL);
1292 for (i = 0; i < var->num_children; i++)
1294 varobj_p existing = VEC_index (varobj_p, var->children, i);
1296 if (existing == NULL)
1298 /* Either it's the first call to varobj_list_children for
1299 this variable object, and the child was never created,
1300 or it was explicitly deleted by the client. */
1301 name = name_of_child (var, i);
1302 existing = create_child (var, i, name);
1303 VEC_replace (varobj_p, var->children, i, existing);
1307 restrict_range (var->children, from, to);
1308 return var->children;
1313 static struct varobj *
1314 varobj_add_child (struct varobj *var, const char *name, struct value *value)
1316 varobj_p v = create_child_with_value (var,
1317 VEC_length (varobj_p, var->children),
1320 VEC_safe_push (varobj_p, var->children, v);
1324 #endif /* HAVE_PYTHON */
1326 /* Obtain the type of an object Variable as a string similar to the one gdb
1327 prints on the console. */
1330 varobj_get_type (struct varobj *var)
1332 /* For the "fake" variables, do not return a type. (It's type is
1334 Do not return a type for invalid variables as well. */
1335 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
1338 return type_to_string (var->type);
1341 /* Obtain the type of an object variable. */
1344 varobj_get_gdb_type (struct varobj *var)
1349 /* Is VAR a path expression parent, i.e., can it be used to construct
1350 a valid path expression? */
1353 is_path_expr_parent (struct varobj *var)
1357 /* "Fake" children are not path_expr parents. */
1358 if (CPLUS_FAKE_CHILD (var))
1361 type = get_value_type (var);
1363 /* Anonymous unions and structs are also not path_expr parents. */
1364 return !((TYPE_CODE (type) == TYPE_CODE_STRUCT
1365 || TYPE_CODE (type) == TYPE_CODE_UNION)
1366 && TYPE_NAME (type) == NULL);
1369 /* Return the path expression parent for VAR. */
1371 static struct varobj *
1372 get_path_expr_parent (struct varobj *var)
1374 struct varobj *parent = var;
1376 while (!is_root_p (parent) && !is_path_expr_parent (parent))
1377 parent = parent->parent;
1382 /* Return a pointer to the full rooted expression of varobj VAR.
1383 If it has not been computed yet, compute it. */
1385 varobj_get_path_expr (struct varobj *var)
1387 if (var->path_expr != NULL)
1388 return var->path_expr;
1391 /* For root varobjs, we initialize path_expr
1392 when creating varobj, so here it should be
1394 gdb_assert (!is_root_p (var));
1395 return (*var->root->lang->path_expr_of_child) (var);
1399 enum varobj_languages
1400 varobj_get_language (struct varobj *var)
1402 return variable_language (var);
1406 varobj_get_attributes (struct varobj *var)
1410 if (varobj_editable_p (var))
1411 /* FIXME: define masks for attributes. */
1412 attributes |= 0x00000001; /* Editable */
1418 varobj_pretty_printed_p (struct varobj *var)
1420 return var->pretty_printer != NULL;
1424 varobj_get_formatted_value (struct varobj *var,
1425 enum varobj_display_formats format)
1427 return my_value_of_variable (var, format);
1431 varobj_get_value (struct varobj *var)
1433 return my_value_of_variable (var, var->format);
1436 /* Set the value of an object variable (if it is editable) to the
1437 value of the given expression. */
1438 /* Note: Invokes functions that can call error(). */
1441 varobj_set_value (struct varobj *var, char *expression)
1443 struct value *val = NULL; /* Initialize to keep gcc happy. */
1444 /* The argument "expression" contains the variable's new value.
1445 We need to first construct a legal expression for this -- ugh! */
1446 /* Does this cover all the bases? */
1447 struct expression *exp;
1448 struct value *value = NULL; /* Initialize to keep gcc happy. */
1449 int saved_input_radix = input_radix;
1450 char *s = expression;
1451 volatile struct gdb_exception except;
1453 gdb_assert (varobj_editable_p (var));
1455 input_radix = 10; /* ALWAYS reset to decimal temporarily. */
1456 exp = parse_exp_1 (&s, 0, 0);
1457 TRY_CATCH (except, RETURN_MASK_ERROR)
1459 value = evaluate_expression (exp);
1462 if (except.reason < 0)
1464 /* We cannot proceed without a valid expression. */
1469 /* All types that are editable must also be changeable. */
1470 gdb_assert (varobj_value_is_changeable_p (var));
1472 /* The value of a changeable variable object must not be lazy. */
1473 gdb_assert (!value_lazy (var->value));
1475 /* Need to coerce the input. We want to check if the
1476 value of the variable object will be different
1477 after assignment, and the first thing value_assign
1478 does is coerce the input.
1479 For example, if we are assigning an array to a pointer variable we
1480 should compare the pointer with the array's address, not with the
1482 value = coerce_array (value);
1484 /* The new value may be lazy. value_assign, or
1485 rather value_contents, will take care of this. */
1486 TRY_CATCH (except, RETURN_MASK_ERROR)
1488 val = value_assign (var->value, value);
1491 if (except.reason < 0)
1494 /* If the value has changed, record it, so that next -var-update can
1495 report this change. If a variable had a value of '1', we've set it
1496 to '333' and then set again to '1', when -var-update will report this
1497 variable as changed -- because the first assignment has set the
1498 'updated' flag. There's no need to optimize that, because return value
1499 of -var-update should be considered an approximation. */
1500 var->updated = install_new_value (var, val, 0 /* Compare values. */);
1501 input_radix = saved_input_radix;
1507 /* A helper function to install a constructor function and visualizer
1511 install_visualizer (struct varobj *var, PyObject *constructor,
1512 PyObject *visualizer)
1514 Py_XDECREF (var->constructor);
1515 var->constructor = constructor;
1517 Py_XDECREF (var->pretty_printer);
1518 var->pretty_printer = visualizer;
1520 Py_XDECREF (var->child_iter);
1521 var->child_iter = NULL;
1524 /* Install the default visualizer for VAR. */
1527 install_default_visualizer (struct varobj *var)
1529 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1530 if (CPLUS_FAKE_CHILD (var))
1533 if (pretty_printing)
1535 PyObject *pretty_printer = NULL;
1539 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
1540 if (! pretty_printer)
1542 gdbpy_print_stack ();
1543 error (_("Cannot instantiate printer for default visualizer"));
1547 if (pretty_printer == Py_None)
1549 Py_DECREF (pretty_printer);
1550 pretty_printer = NULL;
1553 install_visualizer (var, NULL, pretty_printer);
1557 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1558 make a new object. */
1561 construct_visualizer (struct varobj *var, PyObject *constructor)
1563 PyObject *pretty_printer;
1565 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1566 if (CPLUS_FAKE_CHILD (var))
1569 Py_INCREF (constructor);
1570 if (constructor == Py_None)
1571 pretty_printer = NULL;
1574 pretty_printer = instantiate_pretty_printer (constructor, var->value);
1575 if (! pretty_printer)
1577 gdbpy_print_stack ();
1578 Py_DECREF (constructor);
1579 constructor = Py_None;
1580 Py_INCREF (constructor);
1583 if (pretty_printer == Py_None)
1585 Py_DECREF (pretty_printer);
1586 pretty_printer = NULL;
1590 install_visualizer (var, constructor, pretty_printer);
1593 #endif /* HAVE_PYTHON */
1595 /* A helper function for install_new_value. This creates and installs
1596 a visualizer for VAR, if appropriate. */
1599 install_new_value_visualizer (struct varobj *var)
1602 /* If the constructor is None, then we want the raw value. If VAR
1603 does not have a value, just skip this. */
1604 if (var->constructor != Py_None && var->value)
1606 struct cleanup *cleanup;
1608 cleanup = varobj_ensure_python_env (var);
1610 if (!var->constructor)
1611 install_default_visualizer (var);
1613 construct_visualizer (var, var->constructor);
1615 do_cleanups (cleanup);
1622 /* Assign a new value to a variable object. If INITIAL is non-zero,
1623 this is the first assignement after the variable object was just
1624 created, or changed type. In that case, just assign the value
1626 Otherwise, assign the new value, and return 1 if the value is
1627 different from the current one, 0 otherwise. The comparison is
1628 done on textual representation of value. Therefore, some types
1629 need not be compared. E.g. for structures the reported value is
1630 always "{...}", so no comparison is necessary here. If the old
1631 value was NULL and new one is not, or vice versa, we always return 1.
1633 The VALUE parameter should not be released -- the function will
1634 take care of releasing it when needed. */
1636 install_new_value (struct varobj *var, struct value *value, int initial)
1641 int intentionally_not_fetched = 0;
1642 char *print_value = NULL;
1644 /* We need to know the varobj's type to decide if the value should
1645 be fetched or not. C++ fake children (public/protected/private)
1646 don't have a type. */
1647 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1648 changeable = varobj_value_is_changeable_p (var);
1650 /* If the type has custom visualizer, we consider it to be always
1651 changeable. FIXME: need to make sure this behaviour will not
1652 mess up read-sensitive values. */
1653 if (var->pretty_printer)
1656 need_to_fetch = changeable;
1658 /* We are not interested in the address of references, and given
1659 that in C++ a reference is not rebindable, it cannot
1660 meaningfully change. So, get hold of the real value. */
1662 value = coerce_ref (value);
1664 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
1665 /* For unions, we need to fetch the value implicitly because
1666 of implementation of union member fetch. When gdb
1667 creates a value for a field and the value of the enclosing
1668 structure is not lazy, it immediately copies the necessary
1669 bytes from the enclosing values. If the enclosing value is
1670 lazy, the call to value_fetch_lazy on the field will read
1671 the data from memory. For unions, that means we'll read the
1672 same memory more than once, which is not desirable. So
1676 /* The new value might be lazy. If the type is changeable,
1677 that is we'll be comparing values of this type, fetch the
1678 value now. Otherwise, on the next update the old value
1679 will be lazy, which means we've lost that old value. */
1680 if (need_to_fetch && value && value_lazy (value))
1682 struct varobj *parent = var->parent;
1683 int frozen = var->frozen;
1685 for (; !frozen && parent; parent = parent->parent)
1686 frozen |= parent->frozen;
1688 if (frozen && initial)
1690 /* For variables that are frozen, or are children of frozen
1691 variables, we don't do fetch on initial assignment.
1692 For non-initial assignemnt we do the fetch, since it means we're
1693 explicitly asked to compare the new value with the old one. */
1694 intentionally_not_fetched = 1;
1698 volatile struct gdb_exception except;
1700 TRY_CATCH (except, RETURN_MASK_ERROR)
1702 value_fetch_lazy (value);
1705 if (except.reason < 0)
1707 /* Set the value to NULL, so that for the next -var-update,
1708 we don't try to compare the new value with this value,
1709 that we couldn't even read. */
1715 /* Get a reference now, before possibly passing it to any Python
1716 code that might release it. */
1718 value_incref (value);
1720 /* Below, we'll be comparing string rendering of old and new
1721 values. Don't get string rendering if the value is
1722 lazy -- if it is, the code above has decided that the value
1723 should not be fetched. */
1724 if (value && !value_lazy (value) && !var->pretty_printer)
1725 print_value = value_get_print_value (value, var->format, var);
1727 /* If the type is changeable, compare the old and the new values.
1728 If this is the initial assignment, we don't have any old value
1730 if (!initial && changeable)
1732 /* If the value of the varobj was changed by -var-set-value,
1733 then the value in the varobj and in the target is the same.
1734 However, that value is different from the value that the
1735 varobj had after the previous -var-update. So need to the
1736 varobj as changed. */
1741 else if (! var->pretty_printer)
1743 /* Try to compare the values. That requires that both
1744 values are non-lazy. */
1745 if (var->not_fetched && value_lazy (var->value))
1747 /* This is a frozen varobj and the value was never read.
1748 Presumably, UI shows some "never read" indicator.
1749 Now that we've fetched the real value, we need to report
1750 this varobj as changed so that UI can show the real
1754 else if (var->value == NULL && value == NULL)
1757 else if (var->value == NULL || value == NULL)
1763 gdb_assert (!value_lazy (var->value));
1764 gdb_assert (!value_lazy (value));
1766 gdb_assert (var->print_value != NULL && print_value != NULL);
1767 if (strcmp (var->print_value, print_value) != 0)
1773 if (!initial && !changeable)
1775 /* For values that are not changeable, we don't compare the values.
1776 However, we want to notice if a value was not NULL and now is NULL,
1777 or vise versa, so that we report when top-level varobjs come in scope
1778 and leave the scope. */
1779 changed = (var->value != NULL) != (value != NULL);
1782 /* We must always keep the new value, since children depend on it. */
1783 if (var->value != NULL && var->value != value)
1784 value_free (var->value);
1786 if (value && value_lazy (value) && intentionally_not_fetched)
1787 var->not_fetched = 1;
1789 var->not_fetched = 0;
1792 install_new_value_visualizer (var);
1794 /* If we installed a pretty-printer, re-compare the printed version
1795 to see if the variable changed. */
1796 if (var->pretty_printer)
1798 xfree (print_value);
1799 print_value = value_get_print_value (var->value, var->format, var);
1800 if ((var->print_value == NULL && print_value != NULL)
1801 || (var->print_value != NULL && print_value == NULL)
1802 || (var->print_value != NULL && print_value != NULL
1803 && strcmp (var->print_value, print_value) != 0))
1806 if (var->print_value)
1807 xfree (var->print_value);
1808 var->print_value = print_value;
1810 gdb_assert (!var->value || value_type (var->value));
1815 /* Return the requested range for a varobj. VAR is the varobj. FROM
1816 and TO are out parameters; *FROM and *TO will be set to the
1817 selected sub-range of VAR. If no range was selected using
1818 -var-set-update-range, then both will be -1. */
1820 varobj_get_child_range (struct varobj *var, int *from, int *to)
1826 /* Set the selected sub-range of children of VAR to start at index
1827 FROM and end at index TO. If either FROM or TO is less than zero,
1828 this is interpreted as a request for all children. */
1830 varobj_set_child_range (struct varobj *var, int from, int to)
1837 varobj_set_visualizer (struct varobj *var, const char *visualizer)
1840 PyObject *mainmod, *globals, *constructor;
1841 struct cleanup *back_to;
1843 back_to = varobj_ensure_python_env (var);
1845 mainmod = PyImport_AddModule ("__main__");
1846 globals = PyModule_GetDict (mainmod);
1847 Py_INCREF (globals);
1848 make_cleanup_py_decref (globals);
1850 constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
1854 gdbpy_print_stack ();
1855 error (_("Could not evaluate visualizer expression: %s"), visualizer);
1858 construct_visualizer (var, constructor);
1859 Py_XDECREF (constructor);
1861 /* If there are any children now, wipe them. */
1862 varobj_delete (var, NULL, 1 /* children only */);
1863 var->num_children = -1;
1865 do_cleanups (back_to);
1867 error (_("Python support required"));
1871 /* If NEW_VALUE is the new value of the given varobj (var), return
1872 non-zero if var has mutated. In other words, if the type of
1873 the new value is different from the type of the varobj's old
1876 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1879 varobj_value_has_mutated (struct varobj *var, struct value *new_value,
1880 struct type *new_type)
1882 /* If we haven't previously computed the number of children in var,
1883 it does not matter from the front-end's perspective whether
1884 the type has mutated or not. For all intents and purposes,
1885 it has not mutated. */
1886 if (var->num_children < 0)
1889 if (var->root->lang->value_has_mutated)
1890 return var->root->lang->value_has_mutated (var, new_value, new_type);
1895 /* Update the values for a variable and its children. This is a
1896 two-pronged attack. First, re-parse the value for the root's
1897 expression to see if it's changed. Then go all the way
1898 through its children, reconstructing them and noting if they've
1901 The EXPLICIT parameter specifies if this call is result
1902 of MI request to update this specific variable, or
1903 result of implicit -var-update *. For implicit request, we don't
1904 update frozen variables.
1906 NOTE: This function may delete the caller's varobj. If it
1907 returns TYPE_CHANGED, then it has done this and VARP will be modified
1908 to point to the new varobj. */
1910 VEC(varobj_update_result) *
1911 varobj_update (struct varobj **varp, int explicit)
1914 int type_changed = 0;
1917 VEC (varobj_update_result) *stack = NULL;
1918 VEC (varobj_update_result) *result = NULL;
1920 /* Frozen means frozen -- we don't check for any change in
1921 this varobj, including its going out of scope, or
1922 changing type. One use case for frozen varobjs is
1923 retaining previously evaluated expressions, and we don't
1924 want them to be reevaluated at all. */
1925 if (!explicit && (*varp)->frozen)
1928 if (!(*varp)->root->is_valid)
1930 varobj_update_result r = {0};
1933 r.status = VAROBJ_INVALID;
1934 VEC_safe_push (varobj_update_result, result, &r);
1938 if ((*varp)->root->rootvar == *varp)
1940 varobj_update_result r = {0};
1943 r.status = VAROBJ_IN_SCOPE;
1945 /* Update the root variable. value_of_root can return NULL
1946 if the variable is no longer around, i.e. we stepped out of
1947 the frame in which a local existed. We are letting the
1948 value_of_root variable dispose of the varobj if the type
1950 new = value_of_root (varp, &type_changed);
1953 r.type_changed = type_changed;
1954 if (install_new_value ((*varp), new, type_changed))
1958 r.status = VAROBJ_NOT_IN_SCOPE;
1959 r.value_installed = 1;
1961 if (r.status == VAROBJ_NOT_IN_SCOPE)
1963 if (r.type_changed || r.changed)
1964 VEC_safe_push (varobj_update_result, result, &r);
1968 VEC_safe_push (varobj_update_result, stack, &r);
1972 varobj_update_result r = {0};
1975 VEC_safe_push (varobj_update_result, stack, &r);
1978 /* Walk through the children, reconstructing them all. */
1979 while (!VEC_empty (varobj_update_result, stack))
1981 varobj_update_result r = *(VEC_last (varobj_update_result, stack));
1982 struct varobj *v = r.varobj;
1984 VEC_pop (varobj_update_result, stack);
1986 /* Update this variable, unless it's a root, which is already
1988 if (!r.value_installed)
1990 struct type *new_type;
1992 new = value_of_child (v->parent, v->index);
1994 new_type = value_type (new);
1996 new_type = v->root->lang->type_of_child (v->parent, v->index);
1998 if (varobj_value_has_mutated (v, new, new_type))
2000 /* The children are no longer valid; delete them now.
2001 Report the fact that its type changed as well. */
2002 varobj_delete (v, NULL, 1 /* only_children */);
2003 v->num_children = -1;
2010 if (install_new_value (v, new, r.type_changed))
2017 /* We probably should not get children of a varobj that has a
2018 pretty-printer, but for which -var-list-children was never
2020 if (v->pretty_printer)
2022 VEC (varobj_p) *changed = 0, *new = 0, *unchanged = 0;
2023 int i, children_changed = 0;
2028 if (!v->children_requested)
2032 /* If we initially did not have potential children, but
2033 now we do, consider the varobj as changed.
2034 Otherwise, if children were never requested, consider
2035 it as unchanged -- presumably, such varobj is not yet
2036 expanded in the UI, so we need not bother getting
2038 if (!varobj_has_more (v, 0))
2040 update_dynamic_varobj_children (v, NULL, NULL, NULL,
2042 if (varobj_has_more (v, 0))
2047 VEC_safe_push (varobj_update_result, result, &r);
2052 /* If update_dynamic_varobj_children returns 0, then we have
2053 a non-conforming pretty-printer, so we skip it. */
2054 if (update_dynamic_varobj_children (v, &changed, &new, &unchanged,
2055 &children_changed, 1,
2058 if (children_changed || new)
2060 r.children_changed = 1;
2063 /* Push in reverse order so that the first child is
2064 popped from the work stack first, and so will be
2065 added to result first. This does not affect
2066 correctness, just "nicer". */
2067 for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
2069 varobj_p tmp = VEC_index (varobj_p, changed, i);
2070 varobj_update_result r = {0};
2074 r.value_installed = 1;
2075 VEC_safe_push (varobj_update_result, stack, &r);
2077 for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
2079 varobj_p tmp = VEC_index (varobj_p, unchanged, i);
2083 varobj_update_result r = {0};
2086 r.value_installed = 1;
2087 VEC_safe_push (varobj_update_result, stack, &r);
2090 if (r.changed || r.children_changed)
2091 VEC_safe_push (varobj_update_result, result, &r);
2093 /* Free CHANGED and UNCHANGED, but not NEW, because NEW
2094 has been put into the result vector. */
2095 VEC_free (varobj_p, changed);
2096 VEC_free (varobj_p, unchanged);
2102 /* Push any children. Use reverse order so that the first
2103 child is popped from the work stack first, and so
2104 will be added to result first. This does not
2105 affect correctness, just "nicer". */
2106 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
2108 varobj_p c = VEC_index (varobj_p, v->children, i);
2110 /* Child may be NULL if explicitly deleted by -var-delete. */
2111 if (c != NULL && !c->frozen)
2113 varobj_update_result r = {0};
2116 VEC_safe_push (varobj_update_result, stack, &r);
2120 if (r.changed || r.type_changed)
2121 VEC_safe_push (varobj_update_result, result, &r);
2124 VEC_free (varobj_update_result, stack);
2130 /* Helper functions */
2133 * Variable object construction/destruction
2137 delete_variable (struct cpstack **resultp, struct varobj *var,
2138 int only_children_p)
2142 delete_variable_1 (resultp, &delcount, var,
2143 only_children_p, 1 /* remove_from_parent_p */ );
2148 /* Delete the variable object VAR and its children. */
2149 /* IMPORTANT NOTE: If we delete a variable which is a child
2150 and the parent is not removed we dump core. It must be always
2151 initially called with remove_from_parent_p set. */
2153 delete_variable_1 (struct cpstack **resultp, int *delcountp,
2154 struct varobj *var, int only_children_p,
2155 int remove_from_parent_p)
2159 /* Delete any children of this variable, too. */
2160 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
2162 varobj_p child = VEC_index (varobj_p, var->children, i);
2166 if (!remove_from_parent_p)
2167 child->parent = NULL;
2168 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
2170 VEC_free (varobj_p, var->children);
2172 /* if we were called to delete only the children we are done here. */
2173 if (only_children_p)
2176 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
2177 /* If the name is null, this is a temporary variable, that has not
2178 yet been installed, don't report it, it belongs to the caller... */
2179 if (var->obj_name != NULL)
2181 cppush (resultp, xstrdup (var->obj_name));
2182 *delcountp = *delcountp + 1;
2185 /* If this variable has a parent, remove it from its parent's list. */
2186 /* OPTIMIZATION: if the parent of this variable is also being deleted,
2187 (as indicated by remove_from_parent_p) we don't bother doing an
2188 expensive list search to find the element to remove when we are
2189 discarding the list afterwards. */
2190 if ((remove_from_parent_p) && (var->parent != NULL))
2192 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
2195 if (var->obj_name != NULL)
2196 uninstall_variable (var);
2198 /* Free memory associated with this variable. */
2199 free_variable (var);
2202 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
2204 install_variable (struct varobj *var)
2207 struct vlist *newvl;
2209 unsigned int index = 0;
2212 for (chp = var->obj_name; *chp; chp++)
2214 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2217 cv = *(varobj_table + index);
2218 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2222 error (_("Duplicate variable object name"));
2224 /* Add varobj to hash table. */
2225 newvl = xmalloc (sizeof (struct vlist));
2226 newvl->next = *(varobj_table + index);
2228 *(varobj_table + index) = newvl;
2230 /* If root, add varobj to root list. */
2231 if (is_root_p (var))
2233 /* Add to list of root variables. */
2234 if (rootlist == NULL)
2235 var->root->next = NULL;
2237 var->root->next = rootlist;
2238 rootlist = var->root;
2244 /* Unistall the object VAR. */
2246 uninstall_variable (struct varobj *var)
2250 struct varobj_root *cr;
2251 struct varobj_root *prer;
2253 unsigned int index = 0;
2256 /* Remove varobj from hash table. */
2257 for (chp = var->obj_name; *chp; chp++)
2259 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2262 cv = *(varobj_table + index);
2264 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2271 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
2276 ("Assertion failed: Could not find variable object \"%s\" to delete",
2282 *(varobj_table + index) = cv->next;
2284 prev->next = cv->next;
2288 /* If root, remove varobj from root list. */
2289 if (is_root_p (var))
2291 /* Remove from list of root variables. */
2292 if (rootlist == var->root)
2293 rootlist = var->root->next;
2298 while ((cr != NULL) && (cr->rootvar != var))
2305 warning (_("Assertion failed: Could not find "
2306 "varobj \"%s\" in root list"),
2313 prer->next = cr->next;
2319 /* Create and install a child of the parent of the given name. */
2320 static struct varobj *
2321 create_child (struct varobj *parent, int index, char *name)
2323 return create_child_with_value (parent, index, name,
2324 value_of_child (parent, index));
2327 /* Does CHILD represent a child with no name? This happens when
2328 the child is an anonmous struct or union and it has no field name
2329 in its parent variable.
2331 This has already been determined by *_describe_child. The easiest
2332 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
2335 is_anonymous_child (struct varobj *child)
2337 return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
2338 || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
2341 static struct varobj *
2342 create_child_with_value (struct varobj *parent, int index, const char *name,
2343 struct value *value)
2345 struct varobj *child;
2348 child = new_variable ();
2350 /* Name is allocated by name_of_child. */
2351 /* FIXME: xstrdup should not be here. */
2352 child->name = xstrdup (name);
2353 child->index = index;
2354 child->parent = parent;
2355 child->root = parent->root;
2357 if (is_anonymous_child (child))
2358 childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
2360 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
2361 child->obj_name = childs_name;
2363 install_variable (child);
2365 /* Compute the type of the child. Must do this before
2366 calling install_new_value. */
2368 /* If the child had no evaluation errors, var->value
2369 will be non-NULL and contain a valid type. */
2370 child->type = value_type (value);
2372 /* Otherwise, we must compute the type. */
2373 child->type = (*child->root->lang->type_of_child) (child->parent,
2375 install_new_value (child, value, 1);
2382 * Miscellaneous utility functions.
2385 /* Allocate memory and initialize a new variable. */
2386 static struct varobj *
2391 var = (struct varobj *) xmalloc (sizeof (struct varobj));
2393 var->path_expr = NULL;
2394 var->obj_name = NULL;
2398 var->num_children = -1;
2400 var->children = NULL;
2404 var->print_value = NULL;
2406 var->not_fetched = 0;
2407 var->children_requested = 0;
2410 var->constructor = 0;
2411 var->pretty_printer = 0;
2412 var->child_iter = 0;
2413 var->saved_item = 0;
2418 /* Allocate memory and initialize a new root variable. */
2419 static struct varobj *
2420 new_root_variable (void)
2422 struct varobj *var = new_variable ();
2424 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
2425 var->root->lang = NULL;
2426 var->root->exp = NULL;
2427 var->root->valid_block = NULL;
2428 var->root->frame = null_frame_id;
2429 var->root->floating = 0;
2430 var->root->rootvar = NULL;
2431 var->root->is_valid = 1;
2436 /* Free any allocated memory associated with VAR. */
2438 free_variable (struct varobj *var)
2441 if (var->pretty_printer)
2443 struct cleanup *cleanup = varobj_ensure_python_env (var);
2444 Py_XDECREF (var->constructor);
2445 Py_XDECREF (var->pretty_printer);
2446 Py_XDECREF (var->child_iter);
2447 Py_XDECREF (var->saved_item);
2448 do_cleanups (cleanup);
2452 value_free (var->value);
2454 /* Free the expression if this is a root variable. */
2455 if (is_root_p (var))
2457 xfree (var->root->exp);
2462 xfree (var->obj_name);
2463 xfree (var->print_value);
2464 xfree (var->path_expr);
2469 do_free_variable_cleanup (void *var)
2471 free_variable (var);
2474 static struct cleanup *
2475 make_cleanup_free_variable (struct varobj *var)
2477 return make_cleanup (do_free_variable_cleanup, var);
2480 /* This returns the type of the variable. It also skips past typedefs
2481 to return the real type of the variable.
2483 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2484 except within get_target_type and get_type. */
2485 static struct type *
2486 get_type (struct varobj *var)
2492 type = check_typedef (type);
2497 /* Return the type of the value that's stored in VAR,
2498 or that would have being stored there if the
2499 value were accessible.
2501 This differs from VAR->type in that VAR->type is always
2502 the true type of the expession in the source language.
2503 The return value of this function is the type we're
2504 actually storing in varobj, and using for displaying
2505 the values and for comparing previous and new values.
2507 For example, top-level references are always stripped. */
2508 static struct type *
2509 get_value_type (struct varobj *var)
2514 type = value_type (var->value);
2518 type = check_typedef (type);
2520 if (TYPE_CODE (type) == TYPE_CODE_REF)
2521 type = get_target_type (type);
2523 type = check_typedef (type);
2528 /* This returns the target type (or NULL) of TYPE, also skipping
2529 past typedefs, just like get_type ().
2531 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2532 except within get_target_type and get_type. */
2533 static struct type *
2534 get_target_type (struct type *type)
2538 type = TYPE_TARGET_TYPE (type);
2540 type = check_typedef (type);
2546 /* What is the default display for this variable? We assume that
2547 everything is "natural". Any exceptions? */
2548 static enum varobj_display_formats
2549 variable_default_display (struct varobj *var)
2551 return FORMAT_NATURAL;
2554 /* FIXME: The following should be generic for any pointer. */
2556 cppush (struct cpstack **pstack, char *name)
2560 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
2566 /* FIXME: The following should be generic for any pointer. */
2568 cppop (struct cpstack **pstack)
2573 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
2578 *pstack = (*pstack)->next;
2585 * Language-dependencies
2588 /* Common entry points */
2590 /* Get the language of variable VAR. */
2591 static enum varobj_languages
2592 variable_language (struct varobj *var)
2594 enum varobj_languages lang;
2596 switch (var->root->exp->language_defn->la_language)
2602 case language_cplus:
2616 /* Return the number of children for a given variable.
2617 The result of this function is defined by the language
2618 implementation. The number of children returned by this function
2619 is the number of children that the user will see in the variable
2622 number_of_children (struct varobj *var)
2624 return (*var->root->lang->number_of_children) (var);
2627 /* What is the expression for the root varobj VAR? Returns a malloc'd
2630 name_of_variable (struct varobj *var)
2632 return (*var->root->lang->name_of_variable) (var);
2635 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2638 name_of_child (struct varobj *var, int index)
2640 return (*var->root->lang->name_of_child) (var, index);
2643 /* What is the ``struct value *'' of the root variable VAR?
2644 For floating variable object, evaluation can get us a value
2645 of different type from what is stored in varobj already. In
2647 - *type_changed will be set to 1
2648 - old varobj will be freed, and new one will be
2649 created, with the same name.
2650 - *var_handle will be set to the new varobj
2651 Otherwise, *type_changed will be set to 0. */
2652 static struct value *
2653 value_of_root (struct varobj **var_handle, int *type_changed)
2657 if (var_handle == NULL)
2662 /* This should really be an exception, since this should
2663 only get called with a root variable. */
2665 if (!is_root_p (var))
2668 if (var->root->floating)
2670 struct varobj *tmp_var;
2671 char *old_type, *new_type;
2673 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
2674 USE_SELECTED_FRAME);
2675 if (tmp_var == NULL)
2679 old_type = varobj_get_type (var);
2680 new_type = varobj_get_type (tmp_var);
2681 if (strcmp (old_type, new_type) == 0)
2683 /* The expression presently stored inside var->root->exp
2684 remembers the locations of local variables relatively to
2685 the frame where the expression was created (in DWARF location
2686 button, for example). Naturally, those locations are not
2687 correct in other frames, so update the expression. */
2689 struct expression *tmp_exp = var->root->exp;
2691 var->root->exp = tmp_var->root->exp;
2692 tmp_var->root->exp = tmp_exp;
2694 varobj_delete (tmp_var, NULL, 0);
2699 tmp_var->obj_name = xstrdup (var->obj_name);
2700 tmp_var->from = var->from;
2701 tmp_var->to = var->to;
2702 varobj_delete (var, NULL, 0);
2704 install_variable (tmp_var);
2705 *var_handle = tmp_var;
2718 struct value *value;
2720 value = (*var->root->lang->value_of_root) (var_handle);
2721 if (var->value == NULL || value == NULL)
2723 /* For root varobj-s, a NULL value indicates a scoping issue.
2724 So, nothing to do in terms of checking for mutations. */
2726 else if (varobj_value_has_mutated (var, value, value_type (value)))
2728 /* The type has mutated, so the children are no longer valid.
2729 Just delete them, and tell our caller that the type has
2731 varobj_delete (var, NULL, 1 /* only_children */);
2732 var->num_children = -1;
2741 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2742 static struct value *
2743 value_of_child (struct varobj *parent, int index)
2745 struct value *value;
2747 value = (*parent->root->lang->value_of_child) (parent, index);
2752 /* GDB already has a command called "value_of_variable". Sigh. */
2754 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2756 if (var->root->is_valid)
2758 if (var->pretty_printer)
2759 return value_get_print_value (var->value, var->format, var);
2760 return (*var->root->lang->value_of_variable) (var, format);
2767 value_get_print_value (struct value *value, enum varobj_display_formats format,
2770 struct ui_file *stb;
2771 struct cleanup *old_chain;
2772 gdb_byte *thevalue = NULL;
2773 struct value_print_options opts;
2774 struct type *type = NULL;
2776 char *encoding = NULL;
2777 struct gdbarch *gdbarch = NULL;
2778 /* Initialize it just to avoid a GCC false warning. */
2779 CORE_ADDR str_addr = 0;
2780 int string_print = 0;
2785 stb = mem_fileopen ();
2786 old_chain = make_cleanup_ui_file_delete (stb);
2788 gdbarch = get_type_arch (value_type (value));
2791 PyObject *value_formatter = var->pretty_printer;
2793 varobj_ensure_python_env (var);
2795 if (value_formatter)
2797 /* First check to see if we have any children at all. If so,
2798 we simply return {...}. */
2799 if (dynamic_varobj_has_child_method (var))
2801 do_cleanups (old_chain);
2802 return xstrdup ("{...}");
2805 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2807 struct value *replacement;
2808 PyObject *output = NULL;
2810 output = apply_varobj_pretty_printer (value_formatter,
2814 /* If we have string like output ... */
2817 make_cleanup_py_decref (output);
2819 /* If this is a lazy string, extract it. For lazy
2820 strings we always print as a string, so set
2822 if (gdbpy_is_lazy_string (output))
2824 gdbpy_extract_lazy_string (output, &str_addr, &type,
2826 make_cleanup (free_current_contents, &encoding);
2831 /* If it is a regular (non-lazy) string, extract
2832 it and copy the contents into THEVALUE. If the
2833 hint says to print it as a string, set
2834 string_print. Otherwise just return the extracted
2835 string as a value. */
2838 = python_string_to_target_python_string (output);
2842 char *s = PyString_AsString (py_str);
2845 hint = gdbpy_get_display_hint (value_formatter);
2848 if (!strcmp (hint, "string"))
2853 len = PyString_Size (py_str);
2854 thevalue = xmemdup (s, len + 1, len + 1);
2855 type = builtin_type (gdbarch)->builtin_char;
2860 do_cleanups (old_chain);
2864 make_cleanup (xfree, thevalue);
2867 gdbpy_print_stack ();
2870 /* If the printer returned a replacement value, set VALUE
2871 to REPLACEMENT. If there is not a replacement value,
2872 just use the value passed to this function. */
2874 value = replacement;
2880 get_formatted_print_options (&opts, format_code[(int) format]);
2884 /* If the THEVALUE has contents, it is a regular string. */
2886 LA_PRINT_STRING (stb, type, thevalue, len, encoding, 0, &opts);
2887 else if (string_print)
2888 /* Otherwise, if string_print is set, and it is not a regular
2889 string, it is a lazy string. */
2890 val_print_string (type, encoding, str_addr, len, stb, &opts);
2892 /* All other cases. */
2893 common_val_print (value, stb, 0, &opts, current_language);
2895 thevalue = ui_file_xstrdup (stb, NULL);
2897 do_cleanups (old_chain);
2902 varobj_editable_p (struct varobj *var)
2906 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
2909 type = get_value_type (var);
2911 switch (TYPE_CODE (type))
2913 case TYPE_CODE_STRUCT:
2914 case TYPE_CODE_UNION:
2915 case TYPE_CODE_ARRAY:
2916 case TYPE_CODE_FUNC:
2917 case TYPE_CODE_METHOD:
2927 /* Call VAR's value_is_changeable_p language-specific callback. */
2930 varobj_value_is_changeable_p (struct varobj *var)
2932 return var->root->lang->value_is_changeable_p (var);
2935 /* Return 1 if that varobj is floating, that is is always evaluated in the
2936 selected frame, and not bound to thread/frame. Such variable objects
2937 are created using '@' as frame specifier to -var-create. */
2939 varobj_floating_p (struct varobj *var)
2941 return var->root->floating;
2944 /* Given the value and the type of a variable object,
2945 adjust the value and type to those necessary
2946 for getting children of the variable object.
2947 This includes dereferencing top-level references
2948 to all types and dereferencing pointers to
2951 Both TYPE and *TYPE should be non-null. VALUE
2952 can be null if we want to only translate type.
2953 *VALUE can be null as well -- if the parent
2956 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
2957 depending on whether pointer was dereferenced
2958 in this function. */
2960 adjust_value_for_child_access (struct value **value,
2964 gdb_assert (type && *type);
2969 *type = check_typedef (*type);
2971 /* The type of value stored in varobj, that is passed
2972 to us, is already supposed to be
2973 reference-stripped. */
2975 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
2977 /* Pointers to structures are treated just like
2978 structures when accessing children. Don't
2979 dererences pointers to other types. */
2980 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
2982 struct type *target_type = get_target_type (*type);
2983 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
2984 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
2986 if (value && *value)
2988 volatile struct gdb_exception except;
2990 TRY_CATCH (except, RETURN_MASK_ERROR)
2992 *value = value_ind (*value);
2995 if (except.reason < 0)
2998 *type = target_type;
3004 /* The 'get_target_type' function calls check_typedef on
3005 result, so we can immediately check type code. No
3006 need to call check_typedef here. */
3009 /* Implement the "value_is_changeable_p" varobj callback for most
3013 default_value_is_changeable_p (struct varobj *var)
3018 if (CPLUS_FAKE_CHILD (var))
3021 type = get_value_type (var);
3023 switch (TYPE_CODE (type))
3025 case TYPE_CODE_STRUCT:
3026 case TYPE_CODE_UNION:
3027 case TYPE_CODE_ARRAY:
3041 c_number_of_children (struct varobj *var)
3043 struct type *type = get_value_type (var);
3045 struct type *target;
3047 adjust_value_for_child_access (NULL, &type, NULL);
3048 target = get_target_type (type);
3050 switch (TYPE_CODE (type))
3052 case TYPE_CODE_ARRAY:
3053 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
3054 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
3055 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
3057 /* If we don't know how many elements there are, don't display
3062 case TYPE_CODE_STRUCT:
3063 case TYPE_CODE_UNION:
3064 children = TYPE_NFIELDS (type);
3068 /* The type here is a pointer to non-struct. Typically, pointers
3069 have one child, except for function ptrs, which have no children,
3070 and except for void*, as we don't know what to show.
3072 We can show char* so we allow it to be dereferenced. If you decide
3073 to test for it, please mind that a little magic is necessary to
3074 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
3075 TYPE_NAME == "char". */
3076 if (TYPE_CODE (target) == TYPE_CODE_FUNC
3077 || TYPE_CODE (target) == TYPE_CODE_VOID)
3084 /* Other types have no children. */
3092 c_name_of_variable (struct varobj *parent)
3094 return xstrdup (parent->name);
3097 /* Return the value of element TYPE_INDEX of a structure
3098 value VALUE. VALUE's type should be a structure,
3099 or union, or a typedef to struct/union.
3101 Returns NULL if getting the value fails. Never throws. */
3102 static struct value *
3103 value_struct_element_index (struct value *value, int type_index)
3105 struct value *result = NULL;
3106 volatile struct gdb_exception e;
3107 struct type *type = value_type (value);
3109 type = check_typedef (type);
3111 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
3112 || TYPE_CODE (type) == TYPE_CODE_UNION);
3114 TRY_CATCH (e, RETURN_MASK_ERROR)
3116 if (field_is_static (&TYPE_FIELD (type, type_index)))
3117 result = value_static_field (type, type_index);
3119 result = value_primitive_field (value, 0, type_index, type);
3131 /* Obtain the information about child INDEX of the variable
3133 If CNAME is not null, sets *CNAME to the name of the child relative
3135 If CVALUE is not null, sets *CVALUE to the value of the child.
3136 If CTYPE is not null, sets *CTYPE to the type of the child.
3138 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
3139 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
3142 c_describe_child (struct varobj *parent, int index,
3143 char **cname, struct value **cvalue, struct type **ctype,
3144 char **cfull_expression)
3146 struct value *value = parent->value;
3147 struct type *type = get_value_type (parent);
3148 char *parent_expression = NULL;
3150 volatile struct gdb_exception except;
3158 if (cfull_expression)
3160 *cfull_expression = NULL;
3161 parent_expression = varobj_get_path_expr (get_path_expr_parent (parent));
3163 adjust_value_for_child_access (&value, &type, &was_ptr);
3165 switch (TYPE_CODE (type))
3167 case TYPE_CODE_ARRAY:
3170 = xstrdup (int_string (index
3171 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3174 if (cvalue && value)
3176 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
3178 TRY_CATCH (except, RETURN_MASK_ERROR)
3180 *cvalue = value_subscript (value, real_index);
3185 *ctype = get_target_type (type);
3187 if (cfull_expression)
3189 xstrprintf ("(%s)[%s]", parent_expression,
3191 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3197 case TYPE_CODE_STRUCT:
3198 case TYPE_CODE_UNION:
3200 const char *field_name;
3202 /* If the type is anonymous and the field has no name,
3203 set an appropriate name. */
3204 field_name = TYPE_FIELD_NAME (type, index);
3205 if (field_name == NULL || *field_name == '\0')
3209 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
3210 == TYPE_CODE_STRUCT)
3211 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3213 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3216 if (cfull_expression)
3217 *cfull_expression = xstrdup ("");
3222 *cname = xstrdup (field_name);
3224 if (cfull_expression)
3226 char *join = was_ptr ? "->" : ".";
3228 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
3233 if (cvalue && value)
3235 /* For C, varobj index is the same as type index. */
3236 *cvalue = value_struct_element_index (value, index);
3240 *ctype = TYPE_FIELD_TYPE (type, index);
3246 *cname = xstrprintf ("*%s", parent->name);
3248 if (cvalue && value)
3250 TRY_CATCH (except, RETURN_MASK_ERROR)
3252 *cvalue = value_ind (value);
3255 if (except.reason < 0)
3259 /* Don't use get_target_type because it calls
3260 check_typedef and here, we want to show the true
3261 declared type of the variable. */
3263 *ctype = TYPE_TARGET_TYPE (type);
3265 if (cfull_expression)
3266 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
3271 /* This should not happen. */
3273 *cname = xstrdup ("???");
3274 if (cfull_expression)
3275 *cfull_expression = xstrdup ("???");
3276 /* Don't set value and type, we don't know then. */
3281 c_name_of_child (struct varobj *parent, int index)
3285 c_describe_child (parent, index, &name, NULL, NULL, NULL);
3290 c_path_expr_of_child (struct varobj *child)
3292 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
3294 return child->path_expr;
3297 /* If frame associated with VAR can be found, switch
3298 to it and return 1. Otherwise, return 0. */
3300 check_scope (struct varobj *var)
3302 struct frame_info *fi;
3305 fi = frame_find_by_id (var->root->frame);
3310 CORE_ADDR pc = get_frame_pc (fi);
3312 if (pc < BLOCK_START (var->root->valid_block) ||
3313 pc >= BLOCK_END (var->root->valid_block))
3321 static struct value *
3322 c_value_of_root (struct varobj **var_handle)
3324 struct value *new_val = NULL;
3325 struct varobj *var = *var_handle;
3326 int within_scope = 0;
3327 struct cleanup *back_to;
3329 /* Only root variables can be updated... */
3330 if (!is_root_p (var))
3331 /* Not a root var. */
3334 back_to = make_cleanup_restore_current_thread ();
3336 /* Determine whether the variable is still around. */
3337 if (var->root->valid_block == NULL || var->root->floating)
3339 else if (var->root->thread_id == 0)
3341 /* The program was single-threaded when the variable object was
3342 created. Technically, it's possible that the program became
3343 multi-threaded since then, but we don't support such
3345 within_scope = check_scope (var);
3349 ptid_t ptid = thread_id_to_pid (var->root->thread_id);
3350 if (in_thread_list (ptid))
3352 switch_to_thread (ptid);
3353 within_scope = check_scope (var);
3359 volatile struct gdb_exception except;
3361 /* We need to catch errors here, because if evaluate
3362 expression fails we want to just return NULL. */
3363 TRY_CATCH (except, RETURN_MASK_ERROR)
3365 new_val = evaluate_expression (var->root->exp);
3371 do_cleanups (back_to);
3376 static struct value *
3377 c_value_of_child (struct varobj *parent, int index)
3379 struct value *value = NULL;
3381 c_describe_child (parent, index, NULL, &value, NULL, NULL);
3385 static struct type *
3386 c_type_of_child (struct varobj *parent, int index)
3388 struct type *type = NULL;
3390 c_describe_child (parent, index, NULL, NULL, &type, NULL);
3395 c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3397 /* BOGUS: if val_print sees a struct/class, or a reference to one,
3398 it will print out its children instead of "{...}". So we need to
3399 catch that case explicitly. */
3400 struct type *type = get_type (var);
3402 /* Strip top-level references. */
3403 while (TYPE_CODE (type) == TYPE_CODE_REF)
3404 type = check_typedef (TYPE_TARGET_TYPE (type));
3406 switch (TYPE_CODE (type))
3408 case TYPE_CODE_STRUCT:
3409 case TYPE_CODE_UNION:
3410 return xstrdup ("{...}");
3413 case TYPE_CODE_ARRAY:
3417 number = xstrprintf ("[%d]", var->num_children);
3424 if (var->value == NULL)
3426 /* This can happen if we attempt to get the value of a struct
3427 member when the parent is an invalid pointer. This is an
3428 error condition, so we should tell the caller. */
3433 if (var->not_fetched && value_lazy (var->value))
3434 /* Frozen variable and no value yet. We don't
3435 implicitly fetch the value. MI response will
3436 use empty string for the value, which is OK. */
3439 gdb_assert (varobj_value_is_changeable_p (var));
3440 gdb_assert (!value_lazy (var->value));
3442 /* If the specified format is the current one,
3443 we can reuse print_value. */
3444 if (format == var->format)
3445 return xstrdup (var->print_value);
3447 return value_get_print_value (var->value, format, var);
3457 cplus_number_of_children (struct varobj *var)
3460 int children, dont_know;
3465 if (!CPLUS_FAKE_CHILD (var))
3467 type = get_value_type (var);
3468 adjust_value_for_child_access (NULL, &type, NULL);
3470 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
3471 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
3475 cplus_class_num_children (type, kids);
3476 if (kids[v_public] != 0)
3478 if (kids[v_private] != 0)
3480 if (kids[v_protected] != 0)
3483 /* Add any baseclasses. */
3484 children += TYPE_N_BASECLASSES (type);
3487 /* FIXME: save children in var. */
3494 type = get_value_type (var->parent);
3495 adjust_value_for_child_access (NULL, &type, NULL);
3497 cplus_class_num_children (type, kids);
3498 if (strcmp (var->name, "public") == 0)
3499 children = kids[v_public];
3500 else if (strcmp (var->name, "private") == 0)
3501 children = kids[v_private];
3503 children = kids[v_protected];
3508 children = c_number_of_children (var);
3513 /* Compute # of public, private, and protected variables in this class.
3514 That means we need to descend into all baseclasses and find out
3515 how many are there, too. */
3517 cplus_class_num_children (struct type *type, int children[3])
3519 int i, vptr_fieldno;
3520 struct type *basetype = NULL;
3522 children[v_public] = 0;
3523 children[v_private] = 0;
3524 children[v_protected] = 0;
3526 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3527 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
3529 /* If we have a virtual table pointer, omit it. Even if virtual
3530 table pointers are not specifically marked in the debug info,
3531 they should be artificial. */
3532 if ((type == basetype && i == vptr_fieldno)
3533 || TYPE_FIELD_ARTIFICIAL (type, i))
3536 if (TYPE_FIELD_PROTECTED (type, i))
3537 children[v_protected]++;
3538 else if (TYPE_FIELD_PRIVATE (type, i))
3539 children[v_private]++;
3541 children[v_public]++;
3546 cplus_name_of_variable (struct varobj *parent)
3548 return c_name_of_variable (parent);
3551 enum accessibility { private_field, protected_field, public_field };
3553 /* Check if field INDEX of TYPE has the specified accessibility.
3554 Return 0 if so and 1 otherwise. */
3556 match_accessibility (struct type *type, int index, enum accessibility acc)
3558 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
3560 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
3562 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
3563 && !TYPE_FIELD_PROTECTED (type, index))
3570 cplus_describe_child (struct varobj *parent, int index,
3571 char **cname, struct value **cvalue, struct type **ctype,
3572 char **cfull_expression)
3574 struct value *value;
3577 char *parent_expression = NULL;
3585 if (cfull_expression)
3586 *cfull_expression = NULL;
3588 if (CPLUS_FAKE_CHILD (parent))
3590 value = parent->parent->value;
3591 type = get_value_type (parent->parent);
3592 if (cfull_expression)
3594 = varobj_get_path_expr (get_path_expr_parent (parent->parent));
3598 value = parent->value;
3599 type = get_value_type (parent);
3600 if (cfull_expression)
3602 = varobj_get_path_expr (get_path_expr_parent (parent));
3605 adjust_value_for_child_access (&value, &type, &was_ptr);
3607 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3608 || TYPE_CODE (type) == TYPE_CODE_UNION)
3610 char *join = was_ptr ? "->" : ".";
3612 if (CPLUS_FAKE_CHILD (parent))
3614 /* The fields of the class type are ordered as they
3615 appear in the class. We are given an index for a
3616 particular access control type ("public","protected",
3617 or "private"). We must skip over fields that don't
3618 have the access control we are looking for to properly
3619 find the indexed field. */
3620 int type_index = TYPE_N_BASECLASSES (type);
3621 enum accessibility acc = public_field;
3623 struct type *basetype = NULL;
3624 const char *field_name;
3626 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3627 if (strcmp (parent->name, "private") == 0)
3628 acc = private_field;
3629 else if (strcmp (parent->name, "protected") == 0)
3630 acc = protected_field;
3634 if ((type == basetype && type_index == vptr_fieldno)
3635 || TYPE_FIELD_ARTIFICIAL (type, type_index))
3637 else if (match_accessibility (type, type_index, acc))
3643 /* If the type is anonymous and the field has no name,
3644 set an appopriate name. */
3645 field_name = TYPE_FIELD_NAME (type, type_index);
3646 if (field_name == NULL || *field_name == '\0')
3650 if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3651 == TYPE_CODE_STRUCT)
3652 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3653 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3655 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3658 if (cfull_expression)
3659 *cfull_expression = xstrdup ("");
3664 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
3666 if (cfull_expression)
3668 = xstrprintf ("((%s)%s%s)", parent_expression, join,
3672 if (cvalue && value)
3673 *cvalue = value_struct_element_index (value, type_index);
3676 *ctype = TYPE_FIELD_TYPE (type, type_index);
3678 else if (index < TYPE_N_BASECLASSES (type))
3680 /* This is a baseclass. */
3682 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
3684 if (cvalue && value)
3685 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
3689 *ctype = TYPE_FIELD_TYPE (type, index);
3692 if (cfull_expression)
3694 char *ptr = was_ptr ? "*" : "";
3696 /* Cast the parent to the base' type. Note that in gdb,
3699 will create an lvalue, for all appearences, so we don't
3700 need to use more fancy:
3704 When we are in the scope of the base class or of one
3705 of its children, the type field name will be interpreted
3706 as a constructor, if it exists. Therefore, we must
3707 indicate that the name is a class name by using the
3708 'class' keyword. See PR mi/11912 */
3709 *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
3711 TYPE_FIELD_NAME (type, index),
3718 char *access = NULL;
3721 cplus_class_num_children (type, children);
3723 /* Everything beyond the baseclasses can
3724 only be "public", "private", or "protected"
3726 The special "fake" children are always output by varobj in
3727 this order. So if INDEX == 2, it MUST be "protected". */
3728 index -= TYPE_N_BASECLASSES (type);
3732 if (children[v_public] > 0)
3734 else if (children[v_private] > 0)
3737 access = "protected";
3740 if (children[v_public] > 0)
3742 if (children[v_private] > 0)
3745 access = "protected";
3747 else if (children[v_private] > 0)
3748 access = "protected";
3751 /* Must be protected. */
3752 access = "protected";
3759 gdb_assert (access);
3761 *cname = xstrdup (access);
3763 /* Value and type and full expression are null here. */
3768 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
3773 cplus_name_of_child (struct varobj *parent, int index)
3777 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
3782 cplus_path_expr_of_child (struct varobj *child)
3784 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
3786 return child->path_expr;
3789 static struct value *
3790 cplus_value_of_root (struct varobj **var_handle)
3792 return c_value_of_root (var_handle);
3795 static struct value *
3796 cplus_value_of_child (struct varobj *parent, int index)
3798 struct value *value = NULL;
3800 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
3804 static struct type *
3805 cplus_type_of_child (struct varobj *parent, int index)
3807 struct type *type = NULL;
3809 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
3814 cplus_value_of_variable (struct varobj *var,
3815 enum varobj_display_formats format)
3818 /* If we have one of our special types, don't print out
3820 if (CPLUS_FAKE_CHILD (var))
3821 return xstrdup ("");
3823 return c_value_of_variable (var, format);
3829 java_number_of_children (struct varobj *var)
3831 return cplus_number_of_children (var);
3835 java_name_of_variable (struct varobj *parent)
3839 name = cplus_name_of_variable (parent);
3840 /* If the name has "-" in it, it is because we
3841 needed to escape periods in the name... */
3844 while (*p != '\000')
3855 java_name_of_child (struct varobj *parent, int index)
3859 name = cplus_name_of_child (parent, index);
3860 /* Escape any periods in the name... */
3863 while (*p != '\000')
3874 java_path_expr_of_child (struct varobj *child)
3879 static struct value *
3880 java_value_of_root (struct varobj **var_handle)
3882 return cplus_value_of_root (var_handle);
3885 static struct value *
3886 java_value_of_child (struct varobj *parent, int index)
3888 return cplus_value_of_child (parent, index);
3891 static struct type *
3892 java_type_of_child (struct varobj *parent, int index)
3894 return cplus_type_of_child (parent, index);
3898 java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3900 return cplus_value_of_variable (var, format);
3903 /* Ada specific callbacks for VAROBJs. */
3906 ada_number_of_children (struct varobj *var)
3908 return ada_varobj_get_number_of_children (var->value, var->type);
3912 ada_name_of_variable (struct varobj *parent)
3914 return c_name_of_variable (parent);
3918 ada_name_of_child (struct varobj *parent, int index)
3920 return ada_varobj_get_name_of_child (parent->value, parent->type,
3921 parent->name, index);
3925 ada_path_expr_of_child (struct varobj *child)
3927 struct varobj *parent = child->parent;
3928 const char *parent_path_expr = varobj_get_path_expr (parent);
3930 return ada_varobj_get_path_expr_of_child (parent->value,
3937 static struct value *
3938 ada_value_of_root (struct varobj **var_handle)
3940 return c_value_of_root (var_handle);
3943 static struct value *
3944 ada_value_of_child (struct varobj *parent, int index)
3946 return ada_varobj_get_value_of_child (parent->value, parent->type,
3947 parent->name, index);
3950 static struct type *
3951 ada_type_of_child (struct varobj *parent, int index)
3953 return ada_varobj_get_type_of_child (parent->value, parent->type,
3958 ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3960 struct value_print_options opts;
3962 get_formatted_print_options (&opts, format_code[(int) format]);
3966 return ada_varobj_get_value_of_variable (var->value, var->type, &opts);
3969 /* Implement the "value_is_changeable_p" routine for Ada. */
3972 ada_value_is_changeable_p (struct varobj *var)
3974 struct type *type = var->value ? value_type (var->value) : var->type;
3976 if (ada_is_array_descriptor_type (type)
3977 && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
3979 /* This is in reality a pointer to an unconstrained array.
3980 its value is changeable. */
3984 if (ada_is_string_type (type))
3986 /* We display the contents of the string in the array's
3987 "value" field. The contents can change, so consider
3988 that the array is changeable. */
3992 return default_value_is_changeable_p (var);
3995 /* Implement the "value_has_mutated" routine for Ada. */
3998 ada_value_has_mutated (struct varobj *var, struct value *new_val,
3999 struct type *new_type)
4005 /* If the number of fields have changed, then for sure the type
4007 if (ada_varobj_get_number_of_children (new_val, new_type)
4008 != var->num_children)
4011 /* If the number of fields have remained the same, then we need
4012 to check the name of each field. If they remain the same,
4013 then chances are the type hasn't mutated. This is technically
4014 an incomplete test, as the child's type might have changed
4015 despite the fact that the name remains the same. But we'll
4016 handle this situation by saying that the child has mutated,
4019 If only part (or none!) of the children have been fetched,
4020 then only check the ones we fetched. It does not matter
4021 to the frontend whether a child that it has not fetched yet
4022 has mutated or not. So just assume it hasn't. */
4024 restrict_range (var->children, &from, &to);
4025 for (i = from; i < to; i++)
4026 if (strcmp (ada_varobj_get_name_of_child (new_val, new_type,
4028 VEC_index (varobj_p, var->children, i)->name) != 0)
4034 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
4035 with an arbitrary caller supplied DATA pointer. */
4038 all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
4040 struct varobj_root *var_root, *var_root_next;
4042 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
4044 for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
4046 var_root_next = var_root->next;
4048 (*func) (var_root->rootvar, data);
4052 extern void _initialize_varobj (void);
4054 _initialize_varobj (void)
4056 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
4058 varobj_table = xmalloc (sizeof_table);
4059 memset (varobj_table, 0, sizeof_table);
4061 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
4063 _("Set varobj debugging."),
4064 _("Show varobj debugging."),
4065 _("When non-zero, varobj debugging is enabled."),
4066 NULL, show_varobjdebug,
4067 &setlist, &showlist);
4070 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
4071 defined on globals. It is a helper for varobj_invalidate. */
4074 varobj_invalidate_iter (struct varobj *var, void *unused)
4076 /* Floating varobjs are reparsed on each stop, so we don't care if the
4077 presently parsed expression refers to something that's gone. */
4078 if (var->root->floating)
4081 /* global var must be re-evaluated. */
4082 if (var->root->valid_block == NULL)
4084 struct varobj *tmp_var;
4086 /* Try to create a varobj with same expression. If we succeed
4087 replace the old varobj, otherwise invalidate it. */
4088 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
4090 if (tmp_var != NULL)
4092 tmp_var->obj_name = xstrdup (var->obj_name);
4093 varobj_delete (var, NULL, 0);
4094 install_variable (tmp_var);
4097 var->root->is_valid = 0;
4099 else /* locals must be invalidated. */
4100 var->root->is_valid = 0;
4103 /* Invalidate the varobjs that are tied to locals and re-create the ones that
4104 are defined on globals.
4105 Invalidated varobjs will be always printed in_scope="invalid". */
4108 varobj_invalidate (void)
4110 all_root_varobjs (varobj_invalidate_iter, NULL);