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"
38 #include "python/python.h"
39 #include "python/python-internal.h"
44 /* The names of varobjs representing anonymous structs or unions. */
45 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
46 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
48 /* Non-zero if we want to see trace of varobj level stuff. */
52 show_varobjdebug (struct ui_file *file, int from_tty,
53 struct cmd_list_element *c, const char *value)
55 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
58 /* String representations of gdb's format codes. */
59 char *varobj_format_string[] =
60 { "natural", "binary", "decimal", "hexadecimal", "octal" };
62 /* String representations of gdb's known languages. */
63 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
65 /* True if we want to allow Python-based pretty-printing. */
66 static int pretty_printing = 0;
69 varobj_enable_pretty_printing (void)
76 /* Every root variable has one of these structures saved in its
77 varobj. Members which must be free'd are noted. */
81 /* Alloc'd expression for this parent. */
82 struct expression *exp;
84 /* Block for which this expression is valid. */
85 struct block *valid_block;
87 /* The frame for this expression. This field is set iff valid_block is
89 struct frame_id frame;
91 /* The thread ID that this varobj_root belong to. This field
92 is only valid if valid_block is not NULL.
93 When not 0, indicates which thread 'frame' belongs to.
94 When 0, indicates that the thread list was empty when the varobj_root
98 /* If 1, the -var-update always recomputes the value in the
99 current thread and frame. Otherwise, variable object is
100 always updated in the specific scope/thread/frame. */
103 /* Flag that indicates validity: set to 0 when this varobj_root refers
104 to symbols that do not exist anymore. */
107 /* Language info for this variable and its children. */
108 struct language_specific *lang;
110 /* The varobj for this root node. */
111 struct varobj *rootvar;
113 /* Next root variable */
114 struct varobj_root *next;
117 /* Every variable in the system has a structure of this type defined
118 for it. This structure holds all information necessary to manipulate
119 a particular object variable. Members which must be freed are noted. */
123 /* Alloc'd name of the variable for this object. If this variable is a
124 child, then this name will be the child's source name.
125 (bar, not foo.bar). */
126 /* NOTE: This is the "expression". */
129 /* Alloc'd expression for this child. Can be used to create a
130 root variable corresponding to this child. */
133 /* The alloc'd name for this variable's object. This is here for
134 convenience when constructing this object's children. */
137 /* Index of this variable in its parent or -1. */
140 /* The type of this variable. This can be NULL
141 for artifial variable objects -- currently, the "accessibility"
142 variable objects in C++. */
145 /* The value of this expression or subexpression. A NULL value
146 indicates there was an error getting this value.
147 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
148 the value is either NULL, or not lazy. */
151 /* The number of (immediate) children this variable has. */
154 /* If this object is a child, this points to its immediate parent. */
155 struct varobj *parent;
157 /* Children of this object. */
158 VEC (varobj_p) *children;
160 /* Whether the children of this varobj were requested. This field is
161 used to decide if dynamic varobj should recompute their children.
162 In the event that the frontend never asked for the children, we
164 int children_requested;
166 /* Description of the root variable. Points to root variable for
168 struct varobj_root *root;
170 /* The format of the output for this object. */
171 enum varobj_display_formats format;
173 /* Was this variable updated via a varobj_set_value operation. */
176 /* Last print value. */
179 /* Is this variable frozen. Frozen variables are never implicitly
180 updated by -var-update *
181 or -var-update <direct-or-indirect-parent>. */
184 /* Is the value of this variable intentionally not fetched? It is
185 not fetched if either the variable is frozen, or any parents is
189 /* Sub-range of children which the MI consumer has requested. If
190 FROM < 0 or TO < 0, means that all children have been
195 /* The pretty-printer constructor. If NULL, then the default
196 pretty-printer will be looked up. If None, then no
197 pretty-printer will be installed. */
198 PyObject *constructor;
200 /* The pretty-printer that has been constructed. If NULL, then a
201 new printer object is needed, and one will be constructed. */
202 PyObject *pretty_printer;
204 /* The iterator returned by the printer's 'children' method, or NULL
206 PyObject *child_iter;
208 /* We request one extra item from the iterator, so that we can
209 report to the caller whether there are more items than we have
210 already reported. However, we don't want to install this value
211 when we read it, because that will mess up future updates. So,
212 we stash it here instead. */
213 PyObject *saved_item;
219 struct cpstack *next;
222 /* A list of varobjs */
230 /* Private function prototypes */
232 /* Helper functions for the above subcommands. */
234 static int delete_variable (struct cpstack **, struct varobj *, int);
236 static void delete_variable_1 (struct cpstack **, int *,
237 struct varobj *, int, int);
239 static int install_variable (struct varobj *);
241 static void uninstall_variable (struct varobj *);
243 static struct varobj *create_child (struct varobj *, int, char *);
245 static struct varobj *
246 create_child_with_value (struct varobj *parent, int index, const char *name,
247 struct value *value);
249 /* Utility routines */
251 static struct varobj *new_variable (void);
253 static struct varobj *new_root_variable (void);
255 static void free_variable (struct varobj *var);
257 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
259 static struct type *get_type (struct varobj *var);
261 static struct type *get_value_type (struct varobj *var);
263 static struct type *get_target_type (struct type *);
265 static enum varobj_display_formats variable_default_display (struct varobj *);
267 static void cppush (struct cpstack **pstack, char *name);
269 static char *cppop (struct cpstack **pstack);
271 static int install_new_value (struct varobj *var, struct value *value,
274 /* Language-specific routines. */
276 static enum varobj_languages variable_language (struct varobj *var);
278 static int number_of_children (struct varobj *);
280 static char *name_of_variable (struct varobj *);
282 static char *name_of_child (struct varobj *, int);
284 static struct value *value_of_root (struct varobj **var_handle, int *);
286 static struct value *value_of_child (struct varobj *parent, int index);
288 static char *my_value_of_variable (struct varobj *var,
289 enum varobj_display_formats format);
291 static char *value_get_print_value (struct value *value,
292 enum varobj_display_formats format,
295 static int varobj_value_is_changeable_p (struct varobj *var);
297 static int is_root_p (struct varobj *var);
301 static struct varobj *varobj_add_child (struct varobj *var,
303 struct value *value);
305 #endif /* HAVE_PYTHON */
307 /* C implementation */
309 static int c_number_of_children (struct varobj *var);
311 static char *c_name_of_variable (struct varobj *parent);
313 static char *c_name_of_child (struct varobj *parent, int index);
315 static char *c_path_expr_of_child (struct varobj *child);
317 static struct value *c_value_of_root (struct varobj **var_handle);
319 static struct value *c_value_of_child (struct varobj *parent, int index);
321 static struct type *c_type_of_child (struct varobj *parent, int index);
323 static char *c_value_of_variable (struct varobj *var,
324 enum varobj_display_formats format);
326 /* C++ implementation */
328 static int cplus_number_of_children (struct varobj *var);
330 static void cplus_class_num_children (struct type *type, int children[3]);
332 static char *cplus_name_of_variable (struct varobj *parent);
334 static char *cplus_name_of_child (struct varobj *parent, int index);
336 static char *cplus_path_expr_of_child (struct varobj *child);
338 static struct value *cplus_value_of_root (struct varobj **var_handle);
340 static struct value *cplus_value_of_child (struct varobj *parent, int index);
342 static struct type *cplus_type_of_child (struct varobj *parent, int index);
344 static char *cplus_value_of_variable (struct varobj *var,
345 enum varobj_display_formats format);
347 /* Java implementation */
349 static int java_number_of_children (struct varobj *var);
351 static char *java_name_of_variable (struct varobj *parent);
353 static char *java_name_of_child (struct varobj *parent, int index);
355 static char *java_path_expr_of_child (struct varobj *child);
357 static struct value *java_value_of_root (struct varobj **var_handle);
359 static struct value *java_value_of_child (struct varobj *parent, int index);
361 static struct type *java_type_of_child (struct varobj *parent, int index);
363 static char *java_value_of_variable (struct varobj *var,
364 enum varobj_display_formats format);
366 /* Ada implementation */
368 static int ada_number_of_children (struct varobj *var);
370 static char *ada_name_of_variable (struct varobj *parent);
372 static char *ada_name_of_child (struct varobj *parent, int index);
374 static char *ada_path_expr_of_child (struct varobj *child);
376 static struct value *ada_value_of_root (struct varobj **var_handle);
378 static struct value *ada_value_of_child (struct varobj *parent, int index);
380 static struct type *ada_type_of_child (struct varobj *parent, int index);
382 static char *ada_value_of_variable (struct varobj *var,
383 enum varobj_display_formats format);
385 static int ada_value_has_mutated (struct varobj *var, struct value *new_val,
386 struct type *new_type);
388 /* The language specific vector */
390 struct language_specific
393 /* The language of this variable. */
394 enum varobj_languages language;
396 /* The number of children of PARENT. */
397 int (*number_of_children) (struct varobj * parent);
399 /* The name (expression) of a root varobj. */
400 char *(*name_of_variable) (struct varobj * parent);
402 /* The name of the INDEX'th child of PARENT. */
403 char *(*name_of_child) (struct varobj * parent, int index);
405 /* Returns the rooted expression of CHILD, which is a variable
406 obtain that has some parent. */
407 char *(*path_expr_of_child) (struct varobj * child);
409 /* The ``struct value *'' of the root variable ROOT. */
410 struct value *(*value_of_root) (struct varobj ** root_handle);
412 /* The ``struct value *'' of the INDEX'th child of PARENT. */
413 struct value *(*value_of_child) (struct varobj * parent, int index);
415 /* The type of the INDEX'th child of PARENT. */
416 struct type *(*type_of_child) (struct varobj * parent, int index);
418 /* The current value of VAR. */
419 char *(*value_of_variable) (struct varobj * var,
420 enum varobj_display_formats format);
422 /* Return nonzero if the type of VAR has mutated.
424 VAR's value is still the varobj's previous value, while NEW_VALUE
425 is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE
426 may be NULL indicating that there is no value available (the varobj
427 may be out of scope, of may be the child of a null pointer, for
428 instance). NEW_TYPE, on the other hand, must never be NULL.
430 This function should also be able to assume that var's number of
431 children is set (not < 0).
433 Languages where types do not mutate can set this to NULL. */
434 int (*value_has_mutated) (struct varobj *var, struct value *new_value,
435 struct type *new_type);
438 /* Array of known source language routines. */
439 static struct language_specific languages[vlang_end] = {
440 /* Unknown (try treating as C). */
443 c_number_of_children,
446 c_path_expr_of_child,
451 NULL /* value_has_mutated */}
456 c_number_of_children,
459 c_path_expr_of_child,
464 NULL /* value_has_mutated */}
469 cplus_number_of_children,
470 cplus_name_of_variable,
472 cplus_path_expr_of_child,
474 cplus_value_of_child,
476 cplus_value_of_variable,
477 NULL /* value_has_mutated */}
482 java_number_of_children,
483 java_name_of_variable,
485 java_path_expr_of_child,
489 java_value_of_variable,
490 NULL /* value_has_mutated */},
494 ada_number_of_children,
495 ada_name_of_variable,
497 ada_path_expr_of_child,
501 ada_value_of_variable,
502 ada_value_has_mutated}
505 /* A little convenience enum for dealing with C++/Java. */
508 v_public = 0, v_private, v_protected
513 /* Mappings of varobj_display_formats enums to gdb's format codes. */
514 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
516 /* Header of the list of root variable objects. */
517 static struct varobj_root *rootlist;
519 /* Prime number indicating the number of buckets in the hash table. */
520 /* A prime large enough to avoid too many colisions. */
521 #define VAROBJ_TABLE_SIZE 227
523 /* Pointer to the varobj hash table (built at run time). */
524 static struct vlist **varobj_table;
526 /* Is the variable X one of our "fake" children? */
527 #define CPLUS_FAKE_CHILD(x) \
528 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
531 /* API Implementation */
533 is_root_p (struct varobj *var)
535 return (var->root->rootvar == var);
539 /* Helper function to install a Python environment suitable for
540 use during operations on VAR. */
541 static struct cleanup *
542 varobj_ensure_python_env (struct varobj *var)
544 return ensure_python_env (var->root->exp->gdbarch,
545 var->root->exp->language_defn);
549 /* Creates a varobj (not its children). */
551 /* Return the full FRAME which corresponds to the given CORE_ADDR
552 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
554 static struct frame_info *
555 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
557 struct frame_info *frame = NULL;
559 if (frame_addr == (CORE_ADDR) 0)
562 for (frame = get_current_frame ();
564 frame = get_prev_frame (frame))
566 /* The CORE_ADDR we get as argument was parsed from a string GDB
567 output as $fp. This output got truncated to gdbarch_addr_bit.
568 Truncate the frame base address in the same manner before
569 comparing it against our argument. */
570 CORE_ADDR frame_base = get_frame_base_address (frame);
571 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
573 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
574 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
576 if (frame_base == frame_addr)
584 varobj_create (char *objname,
585 char *expression, CORE_ADDR frame, enum varobj_type type)
588 struct cleanup *old_chain;
590 /* Fill out a varobj structure for the (root) variable being constructed. */
591 var = new_root_variable ();
592 old_chain = make_cleanup_free_variable (var);
594 if (expression != NULL)
596 struct frame_info *fi;
597 struct frame_id old_id = null_frame_id;
600 enum varobj_languages lang;
601 struct value *value = NULL;
602 volatile struct gdb_exception except;
604 /* Parse and evaluate the expression, filling in as much of the
605 variable's data as possible. */
607 if (has_stack_frames ())
609 /* Allow creator to specify context of variable. */
610 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
611 fi = get_selected_frame (NULL);
613 /* FIXME: cagney/2002-11-23: This code should be doing a
614 lookup using the frame ID and not just the frame's
615 ``address''. This, of course, means an interface
616 change. However, with out that interface change ISAs,
617 such as the ia64 with its two stacks, won't work.
618 Similar goes for the case where there is a frameless
620 fi = find_frame_addr_in_frame_chain (frame);
625 /* frame = -2 means always use selected frame. */
626 if (type == USE_SELECTED_FRAME)
627 var->root->floating = 1;
631 block = get_frame_block (fi, 0);
634 innermost_block = NULL;
635 /* Wrap the call to parse expression, so we can
636 return a sensible error. */
637 TRY_CATCH (except, RETURN_MASK_ERROR)
639 var->root->exp = parse_exp_1 (&p, block, 0);
642 if (except.reason < 0)
644 do_cleanups (old_chain);
648 /* Don't allow variables to be created for types. */
649 if (var->root->exp->elts[0].opcode == OP_TYPE)
651 do_cleanups (old_chain);
652 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
653 " as an expression.\n");
657 var->format = variable_default_display (var);
658 var->root->valid_block = innermost_block;
659 var->name = xstrdup (expression);
660 /* For a root var, the name and the expr are the same. */
661 var->path_expr = xstrdup (expression);
663 /* When the frame is different from the current frame,
664 we must select the appropriate frame before parsing
665 the expression, otherwise the value will not be current.
666 Since select_frame is so benign, just call it for all cases. */
669 /* User could specify explicit FRAME-ADDR which was not found but
670 EXPRESSION is frame specific and we would not be able to evaluate
671 it correctly next time. With VALID_BLOCK set we must also set
672 FRAME and THREAD_ID. */
674 error (_("Failed to find the specified frame"));
676 var->root->frame = get_frame_id (fi);
677 var->root->thread_id = pid_to_thread_id (inferior_ptid);
678 old_id = get_frame_id (get_selected_frame (NULL));
682 /* We definitely need to catch errors here.
683 If evaluate_expression succeeds we got the value we wanted.
684 But if it fails, we still go on with a call to evaluate_type(). */
685 TRY_CATCH (except, RETURN_MASK_ERROR)
687 value = evaluate_expression (var->root->exp);
690 if (except.reason < 0)
692 /* Error getting the value. Try to at least get the
694 struct value *type_only_value = evaluate_type (var->root->exp);
696 var->type = value_type (type_only_value);
699 var->type = value_type (value);
701 install_new_value (var, value, 1 /* Initial assignment */);
703 /* Set language info */
704 lang = variable_language (var);
705 var->root->lang = &languages[lang];
707 /* Set ourselves as our root. */
708 var->root->rootvar = var;
710 /* Reset the selected frame. */
711 if (frame_id_p (old_id))
712 select_frame (frame_find_by_id (old_id));
715 /* If the variable object name is null, that means this
716 is a temporary variable, so don't install it. */
718 if ((var != NULL) && (objname != NULL))
720 var->obj_name = xstrdup (objname);
722 /* If a varobj name is duplicated, the install will fail so
724 if (!install_variable (var))
726 do_cleanups (old_chain);
731 discard_cleanups (old_chain);
735 /* Generates an unique name that can be used for a varobj. */
738 varobj_gen_name (void)
743 /* Generate a name for this object. */
745 obj_name = xstrprintf ("var%d", id);
750 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
751 error if OBJNAME cannot be found. */
754 varobj_get_handle (char *objname)
758 unsigned int index = 0;
761 for (chp = objname; *chp; chp++)
763 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
766 cv = *(varobj_table + index);
767 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
771 error (_("Variable object not found"));
776 /* Given the handle, return the name of the object. */
779 varobj_get_objname (struct varobj *var)
781 return var->obj_name;
784 /* Given the handle, return the expression represented by the object. */
787 varobj_get_expression (struct varobj *var)
789 return name_of_variable (var);
792 /* Deletes a varobj and all its children if only_children == 0,
793 otherwise deletes only the children; returns a malloc'ed list of
794 all the (malloc'ed) names of the variables that have been deleted
795 (NULL terminated). */
798 varobj_delete (struct varobj *var, char ***dellist, int only_children)
802 struct cpstack *result = NULL;
805 /* Initialize a stack for temporary results. */
806 cppush (&result, NULL);
809 /* Delete only the variable children. */
810 delcount = delete_variable (&result, var, 1 /* only the children */ );
812 /* Delete the variable and all its children. */
813 delcount = delete_variable (&result, var, 0 /* parent+children */ );
815 /* We may have been asked to return a list of what has been deleted. */
818 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
822 *cp = cppop (&result);
823 while ((*cp != NULL) && (mycount > 0))
827 *cp = cppop (&result);
830 if (mycount || (*cp != NULL))
831 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
840 /* Convenience function for varobj_set_visualizer. Instantiate a
841 pretty-printer for a given value. */
843 instantiate_pretty_printer (PyObject *constructor, struct value *value)
845 PyObject *val_obj = NULL;
848 val_obj = value_to_value_object (value);
852 printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
859 /* Set/Get variable object display format. */
861 enum varobj_display_formats
862 varobj_set_display_format (struct varobj *var,
863 enum varobj_display_formats format)
870 case FORMAT_HEXADECIMAL:
872 var->format = format;
876 var->format = variable_default_display (var);
879 if (varobj_value_is_changeable_p (var)
880 && var->value && !value_lazy (var->value))
882 xfree (var->print_value);
883 var->print_value = value_get_print_value (var->value, var->format, var);
889 enum varobj_display_formats
890 varobj_get_display_format (struct varobj *var)
896 varobj_get_display_hint (struct varobj *var)
901 struct cleanup *back_to = varobj_ensure_python_env (var);
903 if (var->pretty_printer)
904 result = gdbpy_get_display_hint (var->pretty_printer);
906 do_cleanups (back_to);
912 /* Return true if the varobj has items after TO, false otherwise. */
915 varobj_has_more (struct varobj *var, int to)
917 if (VEC_length (varobj_p, var->children) > to)
919 return ((to == -1 || VEC_length (varobj_p, var->children) == to)
920 && var->saved_item != NULL);
923 /* If the variable object is bound to a specific thread, that
924 is its evaluation can always be done in context of a frame
925 inside that thread, returns GDB id of the thread -- which
926 is always positive. Otherwise, returns -1. */
928 varobj_get_thread_id (struct varobj *var)
930 if (var->root->valid_block && var->root->thread_id > 0)
931 return var->root->thread_id;
937 varobj_set_frozen (struct varobj *var, int frozen)
939 /* When a variable is unfrozen, we don't fetch its value.
940 The 'not_fetched' flag remains set, so next -var-update
943 We don't fetch the value, because for structures the client
944 should do -var-update anyway. It would be bad to have different
945 client-size logic for structure and other types. */
946 var->frozen = frozen;
950 varobj_get_frozen (struct varobj *var)
955 /* A helper function that restricts a range to what is actually
956 available in a VEC. This follows the usual rules for the meaning
957 of FROM and TO -- if either is negative, the entire range is
961 restrict_range (VEC (varobj_p) *children, int *from, int *to)
963 if (*from < 0 || *to < 0)
966 *to = VEC_length (varobj_p, children);
970 if (*from > VEC_length (varobj_p, children))
971 *from = VEC_length (varobj_p, children);
972 if (*to > VEC_length (varobj_p, children))
973 *to = VEC_length (varobj_p, children);
981 /* A helper for update_dynamic_varobj_children that installs a new
982 child when needed. */
985 install_dynamic_child (struct varobj *var,
986 VEC (varobj_p) **changed,
987 VEC (varobj_p) **new,
988 VEC (varobj_p) **unchanged,
994 if (VEC_length (varobj_p, var->children) < index + 1)
996 /* There's no child yet. */
997 struct varobj *child = varobj_add_child (var, name, value);
1001 VEC_safe_push (varobj_p, *new, child);
1007 varobj_p existing = VEC_index (varobj_p, var->children, index);
1009 if (install_new_value (existing, value, 0))
1012 VEC_safe_push (varobj_p, *changed, existing);
1015 VEC_safe_push (varobj_p, *unchanged, existing);
1020 dynamic_varobj_has_child_method (struct varobj *var)
1022 struct cleanup *back_to;
1023 PyObject *printer = var->pretty_printer;
1026 back_to = varobj_ensure_python_env (var);
1027 result = PyObject_HasAttr (printer, gdbpy_children_cst);
1028 do_cleanups (back_to);
1035 update_dynamic_varobj_children (struct varobj *var,
1036 VEC (varobj_p) **changed,
1037 VEC (varobj_p) **new,
1038 VEC (varobj_p) **unchanged,
1040 int update_children,
1045 struct cleanup *back_to;
1048 PyObject *printer = var->pretty_printer;
1050 back_to = varobj_ensure_python_env (var);
1053 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
1055 do_cleanups (back_to);
1059 if (update_children || !var->child_iter)
1061 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
1066 gdbpy_print_stack ();
1067 error (_("Null value returned for children"));
1070 make_cleanup_py_decref (children);
1072 if (!PyIter_Check (children))
1073 error (_("Returned value is not iterable"));
1075 Py_XDECREF (var->child_iter);
1076 var->child_iter = PyObject_GetIter (children);
1077 if (!var->child_iter)
1079 gdbpy_print_stack ();
1080 error (_("Could not get children iterator"));
1083 Py_XDECREF (var->saved_item);
1084 var->saved_item = NULL;
1089 i = VEC_length (varobj_p, var->children);
1091 /* We ask for one extra child, so that MI can report whether there
1092 are more children. */
1093 for (; to < 0 || i < to + 1; ++i)
1098 /* See if there was a leftover from last time. */
1099 if (var->saved_item)
1101 item = var->saved_item;
1102 var->saved_item = NULL;
1105 item = PyIter_Next (var->child_iter);
1109 /* Normal end of iteration. */
1110 if (!PyErr_Occurred ())
1113 /* If we got a memory error, just use the text as the
1115 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
1117 PyObject *type, *value, *trace;
1118 char *name_str, *value_str;
1120 PyErr_Fetch (&type, &value, &trace);
1121 value_str = gdbpy_exception_to_string (type, value);
1127 gdbpy_print_stack ();
1131 name_str = xstrprintf ("<error at %d>", i);
1132 item = Py_BuildValue ("(ss)", name_str, value_str);
1137 gdbpy_print_stack ();
1145 /* Any other kind of error. */
1146 gdbpy_print_stack ();
1151 /* We don't want to push the extra child on any report list. */
1152 if (to < 0 || i < to)
1157 struct cleanup *inner;
1158 int can_mention = from < 0 || i >= from;
1160 inner = make_cleanup_py_decref (item);
1162 if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
1164 gdbpy_print_stack ();
1165 error (_("Invalid item from the child list"));
1168 v = convert_value_from_python (py_v);
1170 gdbpy_print_stack ();
1171 install_dynamic_child (var, can_mention ? changed : NULL,
1172 can_mention ? new : NULL,
1173 can_mention ? unchanged : NULL,
1174 can_mention ? cchanged : NULL, i, name, v);
1175 do_cleanups (inner);
1179 Py_XDECREF (var->saved_item);
1180 var->saved_item = item;
1182 /* We want to truncate the child list just before this
1191 if (i < VEC_length (varobj_p, var->children))
1196 for (j = i; j < VEC_length (varobj_p, var->children); ++j)
1197 varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
1198 VEC_truncate (varobj_p, var->children, i);
1201 /* If there are fewer children than requested, note that the list of
1202 children changed. */
1203 if (to >= 0 && VEC_length (varobj_p, var->children) < to)
1206 var->num_children = VEC_length (varobj_p, var->children);
1208 do_cleanups (back_to);
1212 gdb_assert (0 && "should never be called if Python is not enabled");
1217 varobj_get_num_children (struct varobj *var)
1219 if (var->num_children == -1)
1221 if (var->pretty_printer)
1225 /* If we have a dynamic varobj, don't report -1 children.
1226 So, try to fetch some children first. */
1227 update_dynamic_varobj_children (var, NULL, NULL, NULL, &dummy,
1231 var->num_children = number_of_children (var);
1234 return var->num_children >= 0 ? var->num_children : 0;
1237 /* Creates a list of the immediate children of a variable object;
1238 the return code is the number of such children or -1 on error. */
1241 varobj_list_children (struct varobj *var, int *from, int *to)
1244 int i, children_changed;
1246 var->children_requested = 1;
1248 if (var->pretty_printer)
1250 /* This, in theory, can result in the number of children changing without
1251 frontend noticing. But well, calling -var-list-children on the same
1252 varobj twice is not something a sane frontend would do. */
1253 update_dynamic_varobj_children (var, NULL, NULL, NULL, &children_changed,
1255 restrict_range (var->children, from, to);
1256 return var->children;
1259 if (var->num_children == -1)
1260 var->num_children = number_of_children (var);
1262 /* If that failed, give up. */
1263 if (var->num_children == -1)
1264 return var->children;
1266 /* If we're called when the list of children is not yet initialized,
1267 allocate enough elements in it. */
1268 while (VEC_length (varobj_p, var->children) < var->num_children)
1269 VEC_safe_push (varobj_p, var->children, NULL);
1271 for (i = 0; i < var->num_children; i++)
1273 varobj_p existing = VEC_index (varobj_p, var->children, i);
1275 if (existing == NULL)
1277 /* Either it's the first call to varobj_list_children for
1278 this variable object, and the child was never created,
1279 or it was explicitly deleted by the client. */
1280 name = name_of_child (var, i);
1281 existing = create_child (var, i, name);
1282 VEC_replace (varobj_p, var->children, i, existing);
1286 restrict_range (var->children, from, to);
1287 return var->children;
1292 static struct varobj *
1293 varobj_add_child (struct varobj *var, const char *name, struct value *value)
1295 varobj_p v = create_child_with_value (var,
1296 VEC_length (varobj_p, var->children),
1299 VEC_safe_push (varobj_p, var->children, v);
1303 #endif /* HAVE_PYTHON */
1305 /* Obtain the type of an object Variable as a string similar to the one gdb
1306 prints on the console. */
1309 varobj_get_type (struct varobj *var)
1311 /* For the "fake" variables, do not return a type. (It's type is
1313 Do not return a type for invalid variables as well. */
1314 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
1317 return type_to_string (var->type);
1320 /* Obtain the type of an object variable. */
1323 varobj_get_gdb_type (struct varobj *var)
1328 /* Is VAR a path expression parent, i.e., can it be used to construct
1329 a valid path expression? */
1332 is_path_expr_parent (struct varobj *var)
1336 /* "Fake" children are not path_expr parents. */
1337 if (CPLUS_FAKE_CHILD (var))
1340 type = get_value_type (var);
1342 /* Anonymous unions and structs are also not path_expr parents. */
1343 return !((TYPE_CODE (type) == TYPE_CODE_STRUCT
1344 || TYPE_CODE (type) == TYPE_CODE_UNION)
1345 && TYPE_NAME (type) == NULL);
1348 /* Return the path expression parent for VAR. */
1350 static struct varobj *
1351 get_path_expr_parent (struct varobj *var)
1353 struct varobj *parent = var;
1355 while (!is_root_p (parent) && !is_path_expr_parent (parent))
1356 parent = parent->parent;
1361 /* Return a pointer to the full rooted expression of varobj VAR.
1362 If it has not been computed yet, compute it. */
1364 varobj_get_path_expr (struct varobj *var)
1366 if (var->path_expr != NULL)
1367 return var->path_expr;
1370 /* For root varobjs, we initialize path_expr
1371 when creating varobj, so here it should be
1373 gdb_assert (!is_root_p (var));
1374 return (*var->root->lang->path_expr_of_child) (var);
1378 enum varobj_languages
1379 varobj_get_language (struct varobj *var)
1381 return variable_language (var);
1385 varobj_get_attributes (struct varobj *var)
1389 if (varobj_editable_p (var))
1390 /* FIXME: define masks for attributes. */
1391 attributes |= 0x00000001; /* Editable */
1397 varobj_pretty_printed_p (struct varobj *var)
1399 return var->pretty_printer != NULL;
1403 varobj_get_formatted_value (struct varobj *var,
1404 enum varobj_display_formats format)
1406 return my_value_of_variable (var, format);
1410 varobj_get_value (struct varobj *var)
1412 return my_value_of_variable (var, var->format);
1415 /* Set the value of an object variable (if it is editable) to the
1416 value of the given expression. */
1417 /* Note: Invokes functions that can call error(). */
1420 varobj_set_value (struct varobj *var, char *expression)
1422 struct value *val = NULL; /* Initialize to keep gcc happy. */
1423 /* The argument "expression" contains the variable's new value.
1424 We need to first construct a legal expression for this -- ugh! */
1425 /* Does this cover all the bases? */
1426 struct expression *exp;
1427 struct value *value = NULL; /* Initialize to keep gcc happy. */
1428 int saved_input_radix = input_radix;
1429 char *s = expression;
1430 volatile struct gdb_exception except;
1432 gdb_assert (varobj_editable_p (var));
1434 input_radix = 10; /* ALWAYS reset to decimal temporarily. */
1435 exp = parse_exp_1 (&s, 0, 0);
1436 TRY_CATCH (except, RETURN_MASK_ERROR)
1438 value = evaluate_expression (exp);
1441 if (except.reason < 0)
1443 /* We cannot proceed without a valid expression. */
1448 /* All types that are editable must also be changeable. */
1449 gdb_assert (varobj_value_is_changeable_p (var));
1451 /* The value of a changeable variable object must not be lazy. */
1452 gdb_assert (!value_lazy (var->value));
1454 /* Need to coerce the input. We want to check if the
1455 value of the variable object will be different
1456 after assignment, and the first thing value_assign
1457 does is coerce the input.
1458 For example, if we are assigning an array to a pointer variable we
1459 should compare the pointer with the array's address, not with the
1461 value = coerce_array (value);
1463 /* The new value may be lazy. value_assign, or
1464 rather value_contents, will take care of this. */
1465 TRY_CATCH (except, RETURN_MASK_ERROR)
1467 val = value_assign (var->value, value);
1470 if (except.reason < 0)
1473 /* If the value has changed, record it, so that next -var-update can
1474 report this change. If a variable had a value of '1', we've set it
1475 to '333' and then set again to '1', when -var-update will report this
1476 variable as changed -- because the first assignment has set the
1477 'updated' flag. There's no need to optimize that, because return value
1478 of -var-update should be considered an approximation. */
1479 var->updated = install_new_value (var, val, 0 /* Compare values. */);
1480 input_radix = saved_input_radix;
1486 /* A helper function to install a constructor function and visualizer
1490 install_visualizer (struct varobj *var, PyObject *constructor,
1491 PyObject *visualizer)
1493 Py_XDECREF (var->constructor);
1494 var->constructor = constructor;
1496 Py_XDECREF (var->pretty_printer);
1497 var->pretty_printer = visualizer;
1499 Py_XDECREF (var->child_iter);
1500 var->child_iter = NULL;
1503 /* Install the default visualizer for VAR. */
1506 install_default_visualizer (struct varobj *var)
1508 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1509 if (CPLUS_FAKE_CHILD (var))
1512 if (pretty_printing)
1514 PyObject *pretty_printer = NULL;
1518 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
1519 if (! pretty_printer)
1521 gdbpy_print_stack ();
1522 error (_("Cannot instantiate printer for default visualizer"));
1526 if (pretty_printer == Py_None)
1528 Py_DECREF (pretty_printer);
1529 pretty_printer = NULL;
1532 install_visualizer (var, NULL, pretty_printer);
1536 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1537 make a new object. */
1540 construct_visualizer (struct varobj *var, PyObject *constructor)
1542 PyObject *pretty_printer;
1544 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1545 if (CPLUS_FAKE_CHILD (var))
1548 Py_INCREF (constructor);
1549 if (constructor == Py_None)
1550 pretty_printer = NULL;
1553 pretty_printer = instantiate_pretty_printer (constructor, var->value);
1554 if (! pretty_printer)
1556 gdbpy_print_stack ();
1557 Py_DECREF (constructor);
1558 constructor = Py_None;
1559 Py_INCREF (constructor);
1562 if (pretty_printer == Py_None)
1564 Py_DECREF (pretty_printer);
1565 pretty_printer = NULL;
1569 install_visualizer (var, constructor, pretty_printer);
1572 #endif /* HAVE_PYTHON */
1574 /* A helper function for install_new_value. This creates and installs
1575 a visualizer for VAR, if appropriate. */
1578 install_new_value_visualizer (struct varobj *var)
1581 /* If the constructor is None, then we want the raw value. If VAR
1582 does not have a value, just skip this. */
1583 if (var->constructor != Py_None && var->value)
1585 struct cleanup *cleanup;
1587 cleanup = varobj_ensure_python_env (var);
1589 if (!var->constructor)
1590 install_default_visualizer (var);
1592 construct_visualizer (var, var->constructor);
1594 do_cleanups (cleanup);
1601 /* Assign a new value to a variable object. If INITIAL is non-zero,
1602 this is the first assignement after the variable object was just
1603 created, or changed type. In that case, just assign the value
1605 Otherwise, assign the new value, and return 1 if the value is
1606 different from the current one, 0 otherwise. The comparison is
1607 done on textual representation of value. Therefore, some types
1608 need not be compared. E.g. for structures the reported value is
1609 always "{...}", so no comparison is necessary here. If the old
1610 value was NULL and new one is not, or vice versa, we always return 1.
1612 The VALUE parameter should not be released -- the function will
1613 take care of releasing it when needed. */
1615 install_new_value (struct varobj *var, struct value *value, int initial)
1620 int intentionally_not_fetched = 0;
1621 char *print_value = NULL;
1623 /* We need to know the varobj's type to decide if the value should
1624 be fetched or not. C++ fake children (public/protected/private)
1625 don't have a type. */
1626 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1627 changeable = varobj_value_is_changeable_p (var);
1629 /* If the type has custom visualizer, we consider it to be always
1630 changeable. FIXME: need to make sure this behaviour will not
1631 mess up read-sensitive values. */
1632 if (var->pretty_printer)
1635 need_to_fetch = changeable;
1637 /* We are not interested in the address of references, and given
1638 that in C++ a reference is not rebindable, it cannot
1639 meaningfully change. So, get hold of the real value. */
1641 value = coerce_ref (value);
1643 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
1644 /* For unions, we need to fetch the value implicitly because
1645 of implementation of union member fetch. When gdb
1646 creates a value for a field and the value of the enclosing
1647 structure is not lazy, it immediately copies the necessary
1648 bytes from the enclosing values. If the enclosing value is
1649 lazy, the call to value_fetch_lazy on the field will read
1650 the data from memory. For unions, that means we'll read the
1651 same memory more than once, which is not desirable. So
1655 /* The new value might be lazy. If the type is changeable,
1656 that is we'll be comparing values of this type, fetch the
1657 value now. Otherwise, on the next update the old value
1658 will be lazy, which means we've lost that old value. */
1659 if (need_to_fetch && value && value_lazy (value))
1661 struct varobj *parent = var->parent;
1662 int frozen = var->frozen;
1664 for (; !frozen && parent; parent = parent->parent)
1665 frozen |= parent->frozen;
1667 if (frozen && initial)
1669 /* For variables that are frozen, or are children of frozen
1670 variables, we don't do fetch on initial assignment.
1671 For non-initial assignemnt we do the fetch, since it means we're
1672 explicitly asked to compare the new value with the old one. */
1673 intentionally_not_fetched = 1;
1677 volatile struct gdb_exception except;
1679 TRY_CATCH (except, RETURN_MASK_ERROR)
1681 value_fetch_lazy (value);
1684 if (except.reason < 0)
1686 /* Set the value to NULL, so that for the next -var-update,
1687 we don't try to compare the new value with this value,
1688 that we couldn't even read. */
1694 /* Get a reference now, before possibly passing it to any Python
1695 code that might release it. */
1697 value_incref (value);
1699 /* Below, we'll be comparing string rendering of old and new
1700 values. Don't get string rendering if the value is
1701 lazy -- if it is, the code above has decided that the value
1702 should not be fetched. */
1703 if (value && !value_lazy (value) && !var->pretty_printer)
1704 print_value = value_get_print_value (value, var->format, var);
1706 /* If the type is changeable, compare the old and the new values.
1707 If this is the initial assignment, we don't have any old value
1709 if (!initial && changeable)
1711 /* If the value of the varobj was changed by -var-set-value,
1712 then the value in the varobj and in the target is the same.
1713 However, that value is different from the value that the
1714 varobj had after the previous -var-update. So need to the
1715 varobj as changed. */
1720 else if (! var->pretty_printer)
1722 /* Try to compare the values. That requires that both
1723 values are non-lazy. */
1724 if (var->not_fetched && value_lazy (var->value))
1726 /* This is a frozen varobj and the value was never read.
1727 Presumably, UI shows some "never read" indicator.
1728 Now that we've fetched the real value, we need to report
1729 this varobj as changed so that UI can show the real
1733 else if (var->value == NULL && value == NULL)
1736 else if (var->value == NULL || value == NULL)
1742 gdb_assert (!value_lazy (var->value));
1743 gdb_assert (!value_lazy (value));
1745 gdb_assert (var->print_value != NULL && print_value != NULL);
1746 if (strcmp (var->print_value, print_value) != 0)
1752 if (!initial && !changeable)
1754 /* For values that are not changeable, we don't compare the values.
1755 However, we want to notice if a value was not NULL and now is NULL,
1756 or vise versa, so that we report when top-level varobjs come in scope
1757 and leave the scope. */
1758 changed = (var->value != NULL) != (value != NULL);
1761 /* We must always keep the new value, since children depend on it. */
1762 if (var->value != NULL && var->value != value)
1763 value_free (var->value);
1765 if (value && value_lazy (value) && intentionally_not_fetched)
1766 var->not_fetched = 1;
1768 var->not_fetched = 0;
1771 install_new_value_visualizer (var);
1773 /* If we installed a pretty-printer, re-compare the printed version
1774 to see if the variable changed. */
1775 if (var->pretty_printer)
1777 xfree (print_value);
1778 print_value = value_get_print_value (var->value, var->format, var);
1779 if ((var->print_value == NULL && print_value != NULL)
1780 || (var->print_value != NULL && print_value == NULL)
1781 || (var->print_value != NULL && print_value != NULL
1782 && strcmp (var->print_value, print_value) != 0))
1785 if (var->print_value)
1786 xfree (var->print_value);
1787 var->print_value = print_value;
1789 gdb_assert (!var->value || value_type (var->value));
1794 /* Return the requested range for a varobj. VAR is the varobj. FROM
1795 and TO are out parameters; *FROM and *TO will be set to the
1796 selected sub-range of VAR. If no range was selected using
1797 -var-set-update-range, then both will be -1. */
1799 varobj_get_child_range (struct varobj *var, int *from, int *to)
1805 /* Set the selected sub-range of children of VAR to start at index
1806 FROM and end at index TO. If either FROM or TO is less than zero,
1807 this is interpreted as a request for all children. */
1809 varobj_set_child_range (struct varobj *var, int from, int to)
1816 varobj_set_visualizer (struct varobj *var, const char *visualizer)
1819 PyObject *mainmod, *globals, *constructor;
1820 struct cleanup *back_to;
1822 back_to = varobj_ensure_python_env (var);
1824 mainmod = PyImport_AddModule ("__main__");
1825 globals = PyModule_GetDict (mainmod);
1826 Py_INCREF (globals);
1827 make_cleanup_py_decref (globals);
1829 constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
1833 gdbpy_print_stack ();
1834 error (_("Could not evaluate visualizer expression: %s"), visualizer);
1837 construct_visualizer (var, constructor);
1838 Py_XDECREF (constructor);
1840 /* If there are any children now, wipe them. */
1841 varobj_delete (var, NULL, 1 /* children only */);
1842 var->num_children = -1;
1844 do_cleanups (back_to);
1846 error (_("Python support required"));
1850 /* If NEW_VALUE is the new value of the given varobj (var), return
1851 non-zero if var has mutated. In other words, if the type of
1852 the new value is different from the type of the varobj's old
1855 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1858 varobj_value_has_mutated (struct varobj *var, struct value *new_value,
1859 struct type *new_type)
1861 /* If we haven't previously computed the number of children in var,
1862 it does not matter from the front-end's perspective whether
1863 the type has mutated or not. For all intents and purposes,
1864 it has not mutated. */
1865 if (var->num_children < 0)
1868 if (var->root->lang->value_has_mutated)
1869 return var->root->lang->value_has_mutated (var, new_value, new_type);
1874 /* Update the values for a variable and its children. This is a
1875 two-pronged attack. First, re-parse the value for the root's
1876 expression to see if it's changed. Then go all the way
1877 through its children, reconstructing them and noting if they've
1880 The EXPLICIT parameter specifies if this call is result
1881 of MI request to update this specific variable, or
1882 result of implicit -var-update *. For implicit request, we don't
1883 update frozen variables.
1885 NOTE: This function may delete the caller's varobj. If it
1886 returns TYPE_CHANGED, then it has done this and VARP will be modified
1887 to point to the new varobj. */
1889 VEC(varobj_update_result) *
1890 varobj_update (struct varobj **varp, int explicit)
1893 int type_changed = 0;
1896 VEC (varobj_update_result) *stack = NULL;
1897 VEC (varobj_update_result) *result = NULL;
1899 /* Frozen means frozen -- we don't check for any change in
1900 this varobj, including its going out of scope, or
1901 changing type. One use case for frozen varobjs is
1902 retaining previously evaluated expressions, and we don't
1903 want them to be reevaluated at all. */
1904 if (!explicit && (*varp)->frozen)
1907 if (!(*varp)->root->is_valid)
1909 varobj_update_result r = {0};
1912 r.status = VAROBJ_INVALID;
1913 VEC_safe_push (varobj_update_result, result, &r);
1917 if ((*varp)->root->rootvar == *varp)
1919 varobj_update_result r = {0};
1922 r.status = VAROBJ_IN_SCOPE;
1924 /* Update the root variable. value_of_root can return NULL
1925 if the variable is no longer around, i.e. we stepped out of
1926 the frame in which a local existed. We are letting the
1927 value_of_root variable dispose of the varobj if the type
1929 new = value_of_root (varp, &type_changed);
1932 r.type_changed = type_changed;
1933 if (install_new_value ((*varp), new, type_changed))
1937 r.status = VAROBJ_NOT_IN_SCOPE;
1938 r.value_installed = 1;
1940 if (r.status == VAROBJ_NOT_IN_SCOPE)
1942 if (r.type_changed || r.changed)
1943 VEC_safe_push (varobj_update_result, result, &r);
1947 VEC_safe_push (varobj_update_result, stack, &r);
1951 varobj_update_result r = {0};
1954 VEC_safe_push (varobj_update_result, stack, &r);
1957 /* Walk through the children, reconstructing them all. */
1958 while (!VEC_empty (varobj_update_result, stack))
1960 varobj_update_result r = *(VEC_last (varobj_update_result, stack));
1961 struct varobj *v = r.varobj;
1963 VEC_pop (varobj_update_result, stack);
1965 /* Update this variable, unless it's a root, which is already
1967 if (!r.value_installed)
1969 struct type *new_type;
1971 new = value_of_child (v->parent, v->index);
1973 new_type = value_type (new);
1975 new_type = v->root->lang->type_of_child (v->parent, v->index);
1977 if (varobj_value_has_mutated (v, new, new_type))
1979 /* The children are no longer valid; delete them now.
1980 Report the fact that its type changed as well. */
1981 varobj_delete (v, NULL, 1 /* only_children */);
1982 v->num_children = -1;
1989 if (install_new_value (v, new, r.type_changed))
1996 /* We probably should not get children of a varobj that has a
1997 pretty-printer, but for which -var-list-children was never
1999 if (v->pretty_printer)
2001 VEC (varobj_p) *changed = 0, *new = 0, *unchanged = 0;
2002 int i, children_changed = 0;
2007 if (!v->children_requested)
2011 /* If we initially did not have potential children, but
2012 now we do, consider the varobj as changed.
2013 Otherwise, if children were never requested, consider
2014 it as unchanged -- presumably, such varobj is not yet
2015 expanded in the UI, so we need not bother getting
2017 if (!varobj_has_more (v, 0))
2019 update_dynamic_varobj_children (v, NULL, NULL, NULL,
2021 if (varobj_has_more (v, 0))
2026 VEC_safe_push (varobj_update_result, result, &r);
2031 /* If update_dynamic_varobj_children returns 0, then we have
2032 a non-conforming pretty-printer, so we skip it. */
2033 if (update_dynamic_varobj_children (v, &changed, &new, &unchanged,
2034 &children_changed, 1,
2037 if (children_changed || new)
2039 r.children_changed = 1;
2042 /* Push in reverse order so that the first child is
2043 popped from the work stack first, and so will be
2044 added to result first. This does not affect
2045 correctness, just "nicer". */
2046 for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
2048 varobj_p tmp = VEC_index (varobj_p, changed, i);
2049 varobj_update_result r = {0};
2053 r.value_installed = 1;
2054 VEC_safe_push (varobj_update_result, stack, &r);
2056 for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
2058 varobj_p tmp = VEC_index (varobj_p, unchanged, i);
2062 varobj_update_result r = {0};
2065 r.value_installed = 1;
2066 VEC_safe_push (varobj_update_result, stack, &r);
2069 if (r.changed || r.children_changed)
2070 VEC_safe_push (varobj_update_result, result, &r);
2072 /* Free CHANGED and UNCHANGED, but not NEW, because NEW
2073 has been put into the result vector. */
2074 VEC_free (varobj_p, changed);
2075 VEC_free (varobj_p, unchanged);
2081 /* Push any children. Use reverse order so that the first
2082 child is popped from the work stack first, and so
2083 will be added to result first. This does not
2084 affect correctness, just "nicer". */
2085 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
2087 varobj_p c = VEC_index (varobj_p, v->children, i);
2089 /* Child may be NULL if explicitly deleted by -var-delete. */
2090 if (c != NULL && !c->frozen)
2092 varobj_update_result r = {0};
2095 VEC_safe_push (varobj_update_result, stack, &r);
2099 if (r.changed || r.type_changed)
2100 VEC_safe_push (varobj_update_result, result, &r);
2103 VEC_free (varobj_update_result, stack);
2109 /* Helper functions */
2112 * Variable object construction/destruction
2116 delete_variable (struct cpstack **resultp, struct varobj *var,
2117 int only_children_p)
2121 delete_variable_1 (resultp, &delcount, var,
2122 only_children_p, 1 /* remove_from_parent_p */ );
2127 /* Delete the variable object VAR and its children. */
2128 /* IMPORTANT NOTE: If we delete a variable which is a child
2129 and the parent is not removed we dump core. It must be always
2130 initially called with remove_from_parent_p set. */
2132 delete_variable_1 (struct cpstack **resultp, int *delcountp,
2133 struct varobj *var, int only_children_p,
2134 int remove_from_parent_p)
2138 /* Delete any children of this variable, too. */
2139 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
2141 varobj_p child = VEC_index (varobj_p, var->children, i);
2145 if (!remove_from_parent_p)
2146 child->parent = NULL;
2147 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
2149 VEC_free (varobj_p, var->children);
2151 /* if we were called to delete only the children we are done here. */
2152 if (only_children_p)
2155 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
2156 /* If the name is null, this is a temporary variable, that has not
2157 yet been installed, don't report it, it belongs to the caller... */
2158 if (var->obj_name != NULL)
2160 cppush (resultp, xstrdup (var->obj_name));
2161 *delcountp = *delcountp + 1;
2164 /* If this variable has a parent, remove it from its parent's list. */
2165 /* OPTIMIZATION: if the parent of this variable is also being deleted,
2166 (as indicated by remove_from_parent_p) we don't bother doing an
2167 expensive list search to find the element to remove when we are
2168 discarding the list afterwards. */
2169 if ((remove_from_parent_p) && (var->parent != NULL))
2171 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
2174 if (var->obj_name != NULL)
2175 uninstall_variable (var);
2177 /* Free memory associated with this variable. */
2178 free_variable (var);
2181 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
2183 install_variable (struct varobj *var)
2186 struct vlist *newvl;
2188 unsigned int index = 0;
2191 for (chp = var->obj_name; *chp; chp++)
2193 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2196 cv = *(varobj_table + index);
2197 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2201 error (_("Duplicate variable object name"));
2203 /* Add varobj to hash table. */
2204 newvl = xmalloc (sizeof (struct vlist));
2205 newvl->next = *(varobj_table + index);
2207 *(varobj_table + index) = newvl;
2209 /* If root, add varobj to root list. */
2210 if (is_root_p (var))
2212 /* Add to list of root variables. */
2213 if (rootlist == NULL)
2214 var->root->next = NULL;
2216 var->root->next = rootlist;
2217 rootlist = var->root;
2223 /* Unistall the object VAR. */
2225 uninstall_variable (struct varobj *var)
2229 struct varobj_root *cr;
2230 struct varobj_root *prer;
2232 unsigned int index = 0;
2235 /* Remove varobj from hash table. */
2236 for (chp = var->obj_name; *chp; chp++)
2238 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2241 cv = *(varobj_table + index);
2243 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2250 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
2255 ("Assertion failed: Could not find variable object \"%s\" to delete",
2261 *(varobj_table + index) = cv->next;
2263 prev->next = cv->next;
2267 /* If root, remove varobj from root list. */
2268 if (is_root_p (var))
2270 /* Remove from list of root variables. */
2271 if (rootlist == var->root)
2272 rootlist = var->root->next;
2277 while ((cr != NULL) && (cr->rootvar != var))
2284 warning (_("Assertion failed: Could not find "
2285 "varobj \"%s\" in root list"),
2292 prer->next = cr->next;
2298 /* Create and install a child of the parent of the given name. */
2299 static struct varobj *
2300 create_child (struct varobj *parent, int index, char *name)
2302 return create_child_with_value (parent, index, name,
2303 value_of_child (parent, index));
2306 /* Does CHILD represent a child with no name? This happens when
2307 the child is an anonmous struct or union and it has no field name
2308 in its parent variable.
2310 This has already been determined by *_describe_child. The easiest
2311 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
2314 is_anonymous_child (struct varobj *child)
2316 return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
2317 || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
2320 static struct varobj *
2321 create_child_with_value (struct varobj *parent, int index, const char *name,
2322 struct value *value)
2324 struct varobj *child;
2327 child = new_variable ();
2329 /* Name is allocated by name_of_child. */
2330 /* FIXME: xstrdup should not be here. */
2331 child->name = xstrdup (name);
2332 child->index = index;
2333 child->parent = parent;
2334 child->root = parent->root;
2336 if (is_anonymous_child (child))
2337 childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
2339 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
2340 child->obj_name = childs_name;
2342 install_variable (child);
2344 /* Compute the type of the child. Must do this before
2345 calling install_new_value. */
2347 /* If the child had no evaluation errors, var->value
2348 will be non-NULL and contain a valid type. */
2349 child->type = value_type (value);
2351 /* Otherwise, we must compute the type. */
2352 child->type = (*child->root->lang->type_of_child) (child->parent,
2354 install_new_value (child, value, 1);
2361 * Miscellaneous utility functions.
2364 /* Allocate memory and initialize a new variable. */
2365 static struct varobj *
2370 var = (struct varobj *) xmalloc (sizeof (struct varobj));
2372 var->path_expr = NULL;
2373 var->obj_name = NULL;
2377 var->num_children = -1;
2379 var->children = NULL;
2383 var->print_value = NULL;
2385 var->not_fetched = 0;
2386 var->children_requested = 0;
2389 var->constructor = 0;
2390 var->pretty_printer = 0;
2391 var->child_iter = 0;
2392 var->saved_item = 0;
2397 /* Allocate memory and initialize a new root variable. */
2398 static struct varobj *
2399 new_root_variable (void)
2401 struct varobj *var = new_variable ();
2403 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
2404 var->root->lang = NULL;
2405 var->root->exp = NULL;
2406 var->root->valid_block = NULL;
2407 var->root->frame = null_frame_id;
2408 var->root->floating = 0;
2409 var->root->rootvar = NULL;
2410 var->root->is_valid = 1;
2415 /* Free any allocated memory associated with VAR. */
2417 free_variable (struct varobj *var)
2420 if (var->pretty_printer)
2422 struct cleanup *cleanup = varobj_ensure_python_env (var);
2423 Py_XDECREF (var->constructor);
2424 Py_XDECREF (var->pretty_printer);
2425 Py_XDECREF (var->child_iter);
2426 Py_XDECREF (var->saved_item);
2427 do_cleanups (cleanup);
2431 value_free (var->value);
2433 /* Free the expression if this is a root variable. */
2434 if (is_root_p (var))
2436 xfree (var->root->exp);
2441 xfree (var->obj_name);
2442 xfree (var->print_value);
2443 xfree (var->path_expr);
2448 do_free_variable_cleanup (void *var)
2450 free_variable (var);
2453 static struct cleanup *
2454 make_cleanup_free_variable (struct varobj *var)
2456 return make_cleanup (do_free_variable_cleanup, var);
2459 /* This returns the type of the variable. It also skips past typedefs
2460 to return the real type of the variable.
2462 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2463 except within get_target_type and get_type. */
2464 static struct type *
2465 get_type (struct varobj *var)
2471 type = check_typedef (type);
2476 /* Return the type of the value that's stored in VAR,
2477 or that would have being stored there if the
2478 value were accessible.
2480 This differs from VAR->type in that VAR->type is always
2481 the true type of the expession in the source language.
2482 The return value of this function is the type we're
2483 actually storing in varobj, and using for displaying
2484 the values and for comparing previous and new values.
2486 For example, top-level references are always stripped. */
2487 static struct type *
2488 get_value_type (struct varobj *var)
2493 type = value_type (var->value);
2497 type = check_typedef (type);
2499 if (TYPE_CODE (type) == TYPE_CODE_REF)
2500 type = get_target_type (type);
2502 type = check_typedef (type);
2507 /* This returns the target type (or NULL) of TYPE, also skipping
2508 past typedefs, just like get_type ().
2510 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2511 except within get_target_type and get_type. */
2512 static struct type *
2513 get_target_type (struct type *type)
2517 type = TYPE_TARGET_TYPE (type);
2519 type = check_typedef (type);
2525 /* What is the default display for this variable? We assume that
2526 everything is "natural". Any exceptions? */
2527 static enum varobj_display_formats
2528 variable_default_display (struct varobj *var)
2530 return FORMAT_NATURAL;
2533 /* FIXME: The following should be generic for any pointer. */
2535 cppush (struct cpstack **pstack, char *name)
2539 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
2545 /* FIXME: The following should be generic for any pointer. */
2547 cppop (struct cpstack **pstack)
2552 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
2557 *pstack = (*pstack)->next;
2564 * Language-dependencies
2567 /* Common entry points */
2569 /* Get the language of variable VAR. */
2570 static enum varobj_languages
2571 variable_language (struct varobj *var)
2573 enum varobj_languages lang;
2575 switch (var->root->exp->language_defn->la_language)
2581 case language_cplus:
2595 /* Return the number of children for a given variable.
2596 The result of this function is defined by the language
2597 implementation. The number of children returned by this function
2598 is the number of children that the user will see in the variable
2601 number_of_children (struct varobj *var)
2603 return (*var->root->lang->number_of_children) (var);
2606 /* What is the expression for the root varobj VAR? Returns a malloc'd
2609 name_of_variable (struct varobj *var)
2611 return (*var->root->lang->name_of_variable) (var);
2614 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2617 name_of_child (struct varobj *var, int index)
2619 return (*var->root->lang->name_of_child) (var, index);
2622 /* What is the ``struct value *'' of the root variable VAR?
2623 For floating variable object, evaluation can get us a value
2624 of different type from what is stored in varobj already. In
2626 - *type_changed will be set to 1
2627 - old varobj will be freed, and new one will be
2628 created, with the same name.
2629 - *var_handle will be set to the new varobj
2630 Otherwise, *type_changed will be set to 0. */
2631 static struct value *
2632 value_of_root (struct varobj **var_handle, int *type_changed)
2636 if (var_handle == NULL)
2641 /* This should really be an exception, since this should
2642 only get called with a root variable. */
2644 if (!is_root_p (var))
2647 if (var->root->floating)
2649 struct varobj *tmp_var;
2650 char *old_type, *new_type;
2652 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
2653 USE_SELECTED_FRAME);
2654 if (tmp_var == NULL)
2658 old_type = varobj_get_type (var);
2659 new_type = varobj_get_type (tmp_var);
2660 if (strcmp (old_type, new_type) == 0)
2662 /* The expression presently stored inside var->root->exp
2663 remembers the locations of local variables relatively to
2664 the frame where the expression was created (in DWARF location
2665 button, for example). Naturally, those locations are not
2666 correct in other frames, so update the expression. */
2668 struct expression *tmp_exp = var->root->exp;
2670 var->root->exp = tmp_var->root->exp;
2671 tmp_var->root->exp = tmp_exp;
2673 varobj_delete (tmp_var, NULL, 0);
2678 tmp_var->obj_name = xstrdup (var->obj_name);
2679 tmp_var->from = var->from;
2680 tmp_var->to = var->to;
2681 varobj_delete (var, NULL, 0);
2683 install_variable (tmp_var);
2684 *var_handle = tmp_var;
2697 struct value *value;
2699 value = (*var->root->lang->value_of_root) (var_handle);
2700 if (var->value == NULL || value == NULL)
2702 /* For root varobj-s, a NULL value indicates a scoping issue.
2703 So, nothing to do in terms of checking for mutations. */
2705 else if (varobj_value_has_mutated (var, value, value_type (value)))
2707 /* The type has mutated, so the children are no longer valid.
2708 Just delete them, and tell our caller that the type has
2710 varobj_delete (var, NULL, 1 /* only_children */);
2711 var->num_children = -1;
2720 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2721 static struct value *
2722 value_of_child (struct varobj *parent, int index)
2724 struct value *value;
2726 value = (*parent->root->lang->value_of_child) (parent, index);
2731 /* GDB already has a command called "value_of_variable". Sigh. */
2733 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2735 if (var->root->is_valid)
2737 if (var->pretty_printer)
2738 return value_get_print_value (var->value, var->format, var);
2739 return (*var->root->lang->value_of_variable) (var, format);
2746 value_get_print_value (struct value *value, enum varobj_display_formats format,
2749 struct ui_file *stb;
2750 struct cleanup *old_chain;
2751 gdb_byte *thevalue = NULL;
2752 struct value_print_options opts;
2753 struct type *type = NULL;
2755 char *encoding = NULL;
2756 struct gdbarch *gdbarch = NULL;
2757 /* Initialize it just to avoid a GCC false warning. */
2758 CORE_ADDR str_addr = 0;
2759 int string_print = 0;
2764 stb = mem_fileopen ();
2765 old_chain = make_cleanup_ui_file_delete (stb);
2767 gdbarch = get_type_arch (value_type (value));
2770 PyObject *value_formatter = var->pretty_printer;
2772 varobj_ensure_python_env (var);
2774 if (value_formatter)
2776 /* First check to see if we have any children at all. If so,
2777 we simply return {...}. */
2778 if (dynamic_varobj_has_child_method (var))
2780 do_cleanups (old_chain);
2781 return xstrdup ("{...}");
2784 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2786 struct value *replacement;
2787 PyObject *output = NULL;
2789 output = apply_varobj_pretty_printer (value_formatter,
2793 /* If we have string like output ... */
2796 make_cleanup_py_decref (output);
2798 /* If this is a lazy string, extract it. For lazy
2799 strings we always print as a string, so set
2801 if (gdbpy_is_lazy_string (output))
2803 gdbpy_extract_lazy_string (output, &str_addr, &type,
2805 make_cleanup (free_current_contents, &encoding);
2810 /* If it is a regular (non-lazy) string, extract
2811 it and copy the contents into THEVALUE. If the
2812 hint says to print it as a string, set
2813 string_print. Otherwise just return the extracted
2814 string as a value. */
2817 = python_string_to_target_python_string (output);
2821 char *s = PyString_AsString (py_str);
2824 hint = gdbpy_get_display_hint (value_formatter);
2827 if (!strcmp (hint, "string"))
2832 len = PyString_Size (py_str);
2833 thevalue = xmemdup (s, len + 1, len + 1);
2834 type = builtin_type (gdbarch)->builtin_char;
2839 do_cleanups (old_chain);
2843 make_cleanup (xfree, thevalue);
2846 gdbpy_print_stack ();
2849 /* If the printer returned a replacement value, set VALUE
2850 to REPLACEMENT. If there is not a replacement value,
2851 just use the value passed to this function. */
2853 value = replacement;
2859 get_formatted_print_options (&opts, format_code[(int) format]);
2863 /* If the THEVALUE has contents, it is a regular string. */
2865 LA_PRINT_STRING (stb, type, thevalue, len, encoding, 0, &opts);
2866 else if (string_print)
2867 /* Otherwise, if string_print is set, and it is not a regular
2868 string, it is a lazy string. */
2869 val_print_string (type, encoding, str_addr, len, stb, &opts);
2871 /* All other cases. */
2872 common_val_print (value, stb, 0, &opts, current_language);
2874 thevalue = ui_file_xstrdup (stb, NULL);
2876 do_cleanups (old_chain);
2881 varobj_editable_p (struct varobj *var)
2885 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
2888 type = get_value_type (var);
2890 switch (TYPE_CODE (type))
2892 case TYPE_CODE_STRUCT:
2893 case TYPE_CODE_UNION:
2894 case TYPE_CODE_ARRAY:
2895 case TYPE_CODE_FUNC:
2896 case TYPE_CODE_METHOD:
2906 /* Return non-zero if changes in value of VAR
2907 must be detected and reported by -var-update.
2908 Return zero is -var-update should never report
2909 changes of such values. This makes sense for structures
2910 (since the changes in children values will be reported separately),
2911 or for artifical objects (like 'public' pseudo-field in C++).
2913 Return value of 0 means that gdb need not call value_fetch_lazy
2914 for the value of this variable object. */
2916 varobj_value_is_changeable_p (struct varobj *var)
2921 if (CPLUS_FAKE_CHILD (var))
2924 type = get_value_type (var);
2926 switch (TYPE_CODE (type))
2928 case TYPE_CODE_STRUCT:
2929 case TYPE_CODE_UNION:
2930 case TYPE_CODE_ARRAY:
2941 /* Return 1 if that varobj is floating, that is is always evaluated in the
2942 selected frame, and not bound to thread/frame. Such variable objects
2943 are created using '@' as frame specifier to -var-create. */
2945 varobj_floating_p (struct varobj *var)
2947 return var->root->floating;
2950 /* Given the value and the type of a variable object,
2951 adjust the value and type to those necessary
2952 for getting children of the variable object.
2953 This includes dereferencing top-level references
2954 to all types and dereferencing pointers to
2957 Both TYPE and *TYPE should be non-null. VALUE
2958 can be null if we want to only translate type.
2959 *VALUE can be null as well -- if the parent
2962 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
2963 depending on whether pointer was dereferenced
2964 in this function. */
2966 adjust_value_for_child_access (struct value **value,
2970 gdb_assert (type && *type);
2975 *type = check_typedef (*type);
2977 /* The type of value stored in varobj, that is passed
2978 to us, is already supposed to be
2979 reference-stripped. */
2981 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
2983 /* Pointers to structures are treated just like
2984 structures when accessing children. Don't
2985 dererences pointers to other types. */
2986 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
2988 struct type *target_type = get_target_type (*type);
2989 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
2990 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
2992 if (value && *value)
2994 volatile struct gdb_exception except;
2996 TRY_CATCH (except, RETURN_MASK_ERROR)
2998 *value = value_ind (*value);
3001 if (except.reason < 0)
3004 *type = target_type;
3010 /* The 'get_target_type' function calls check_typedef on
3011 result, so we can immediately check type code. No
3012 need to call check_typedef here. */
3017 c_number_of_children (struct varobj *var)
3019 struct type *type = get_value_type (var);
3021 struct type *target;
3023 adjust_value_for_child_access (NULL, &type, NULL);
3024 target = get_target_type (type);
3026 switch (TYPE_CODE (type))
3028 case TYPE_CODE_ARRAY:
3029 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
3030 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
3031 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
3033 /* If we don't know how many elements there are, don't display
3038 case TYPE_CODE_STRUCT:
3039 case TYPE_CODE_UNION:
3040 children = TYPE_NFIELDS (type);
3044 /* The type here is a pointer to non-struct. Typically, pointers
3045 have one child, except for function ptrs, which have no children,
3046 and except for void*, as we don't know what to show.
3048 We can show char* so we allow it to be dereferenced. If you decide
3049 to test for it, please mind that a little magic is necessary to
3050 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
3051 TYPE_NAME == "char". */
3052 if (TYPE_CODE (target) == TYPE_CODE_FUNC
3053 || TYPE_CODE (target) == TYPE_CODE_VOID)
3060 /* Other types have no children. */
3068 c_name_of_variable (struct varobj *parent)
3070 return xstrdup (parent->name);
3073 /* Return the value of element TYPE_INDEX of a structure
3074 value VALUE. VALUE's type should be a structure,
3075 or union, or a typedef to struct/union.
3077 Returns NULL if getting the value fails. Never throws. */
3078 static struct value *
3079 value_struct_element_index (struct value *value, int type_index)
3081 struct value *result = NULL;
3082 volatile struct gdb_exception e;
3083 struct type *type = value_type (value);
3085 type = check_typedef (type);
3087 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
3088 || TYPE_CODE (type) == TYPE_CODE_UNION);
3090 TRY_CATCH (e, RETURN_MASK_ERROR)
3092 if (field_is_static (&TYPE_FIELD (type, type_index)))
3093 result = value_static_field (type, type_index);
3095 result = value_primitive_field (value, 0, type_index, type);
3107 /* Obtain the information about child INDEX of the variable
3109 If CNAME is not null, sets *CNAME to the name of the child relative
3111 If CVALUE is not null, sets *CVALUE to the value of the child.
3112 If CTYPE is not null, sets *CTYPE to the type of the child.
3114 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
3115 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
3118 c_describe_child (struct varobj *parent, int index,
3119 char **cname, struct value **cvalue, struct type **ctype,
3120 char **cfull_expression)
3122 struct value *value = parent->value;
3123 struct type *type = get_value_type (parent);
3124 char *parent_expression = NULL;
3126 volatile struct gdb_exception except;
3134 if (cfull_expression)
3136 *cfull_expression = NULL;
3137 parent_expression = varobj_get_path_expr (get_path_expr_parent (parent));
3139 adjust_value_for_child_access (&value, &type, &was_ptr);
3141 switch (TYPE_CODE (type))
3143 case TYPE_CODE_ARRAY:
3146 = xstrdup (int_string (index
3147 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3150 if (cvalue && value)
3152 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
3154 TRY_CATCH (except, RETURN_MASK_ERROR)
3156 *cvalue = value_subscript (value, real_index);
3161 *ctype = get_target_type (type);
3163 if (cfull_expression)
3165 xstrprintf ("(%s)[%s]", parent_expression,
3167 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3173 case TYPE_CODE_STRUCT:
3174 case TYPE_CODE_UNION:
3176 const char *field_name;
3178 /* If the type is anonymous and the field has no name,
3179 set an appropriate name. */
3180 field_name = TYPE_FIELD_NAME (type, index);
3181 if (field_name == NULL || *field_name == '\0')
3185 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
3186 == TYPE_CODE_STRUCT)
3187 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3189 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3192 if (cfull_expression)
3193 *cfull_expression = xstrdup ("");
3198 *cname = xstrdup (field_name);
3200 if (cfull_expression)
3202 char *join = was_ptr ? "->" : ".";
3204 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
3209 if (cvalue && value)
3211 /* For C, varobj index is the same as type index. */
3212 *cvalue = value_struct_element_index (value, index);
3216 *ctype = TYPE_FIELD_TYPE (type, index);
3222 *cname = xstrprintf ("*%s", parent->name);
3224 if (cvalue && value)
3226 TRY_CATCH (except, RETURN_MASK_ERROR)
3228 *cvalue = value_ind (value);
3231 if (except.reason < 0)
3235 /* Don't use get_target_type because it calls
3236 check_typedef and here, we want to show the true
3237 declared type of the variable. */
3239 *ctype = TYPE_TARGET_TYPE (type);
3241 if (cfull_expression)
3242 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
3247 /* This should not happen. */
3249 *cname = xstrdup ("???");
3250 if (cfull_expression)
3251 *cfull_expression = xstrdup ("???");
3252 /* Don't set value and type, we don't know then. */
3257 c_name_of_child (struct varobj *parent, int index)
3261 c_describe_child (parent, index, &name, NULL, NULL, NULL);
3266 c_path_expr_of_child (struct varobj *child)
3268 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
3270 return child->path_expr;
3273 /* If frame associated with VAR can be found, switch
3274 to it and return 1. Otherwise, return 0. */
3276 check_scope (struct varobj *var)
3278 struct frame_info *fi;
3281 fi = frame_find_by_id (var->root->frame);
3286 CORE_ADDR pc = get_frame_pc (fi);
3288 if (pc < BLOCK_START (var->root->valid_block) ||
3289 pc >= BLOCK_END (var->root->valid_block))
3297 static struct value *
3298 c_value_of_root (struct varobj **var_handle)
3300 struct value *new_val = NULL;
3301 struct varobj *var = *var_handle;
3302 int within_scope = 0;
3303 struct cleanup *back_to;
3305 /* Only root variables can be updated... */
3306 if (!is_root_p (var))
3307 /* Not a root var. */
3310 back_to = make_cleanup_restore_current_thread ();
3312 /* Determine whether the variable is still around. */
3313 if (var->root->valid_block == NULL || var->root->floating)
3315 else if (var->root->thread_id == 0)
3317 /* The program was single-threaded when the variable object was
3318 created. Technically, it's possible that the program became
3319 multi-threaded since then, but we don't support such
3321 within_scope = check_scope (var);
3325 ptid_t ptid = thread_id_to_pid (var->root->thread_id);
3326 if (in_thread_list (ptid))
3328 switch_to_thread (ptid);
3329 within_scope = check_scope (var);
3335 volatile struct gdb_exception except;
3337 /* We need to catch errors here, because if evaluate
3338 expression fails we want to just return NULL. */
3339 TRY_CATCH (except, RETURN_MASK_ERROR)
3341 new_val = evaluate_expression (var->root->exp);
3347 do_cleanups (back_to);
3352 static struct value *
3353 c_value_of_child (struct varobj *parent, int index)
3355 struct value *value = NULL;
3357 c_describe_child (parent, index, NULL, &value, NULL, NULL);
3361 static struct type *
3362 c_type_of_child (struct varobj *parent, int index)
3364 struct type *type = NULL;
3366 c_describe_child (parent, index, NULL, NULL, &type, NULL);
3371 c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3373 /* BOGUS: if val_print sees a struct/class, or a reference to one,
3374 it will print out its children instead of "{...}". So we need to
3375 catch that case explicitly. */
3376 struct type *type = get_type (var);
3378 /* Strip top-level references. */
3379 while (TYPE_CODE (type) == TYPE_CODE_REF)
3380 type = check_typedef (TYPE_TARGET_TYPE (type));
3382 switch (TYPE_CODE (type))
3384 case TYPE_CODE_STRUCT:
3385 case TYPE_CODE_UNION:
3386 return xstrdup ("{...}");
3389 case TYPE_CODE_ARRAY:
3393 number = xstrprintf ("[%d]", var->num_children);
3400 if (var->value == NULL)
3402 /* This can happen if we attempt to get the value of a struct
3403 member when the parent is an invalid pointer. This is an
3404 error condition, so we should tell the caller. */
3409 if (var->not_fetched && value_lazy (var->value))
3410 /* Frozen variable and no value yet. We don't
3411 implicitly fetch the value. MI response will
3412 use empty string for the value, which is OK. */
3415 gdb_assert (varobj_value_is_changeable_p (var));
3416 gdb_assert (!value_lazy (var->value));
3418 /* If the specified format is the current one,
3419 we can reuse print_value. */
3420 if (format == var->format)
3421 return xstrdup (var->print_value);
3423 return value_get_print_value (var->value, format, var);
3433 cplus_number_of_children (struct varobj *var)
3436 int children, dont_know;
3441 if (!CPLUS_FAKE_CHILD (var))
3443 type = get_value_type (var);
3444 adjust_value_for_child_access (NULL, &type, NULL);
3446 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
3447 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
3451 cplus_class_num_children (type, kids);
3452 if (kids[v_public] != 0)
3454 if (kids[v_private] != 0)
3456 if (kids[v_protected] != 0)
3459 /* Add any baseclasses. */
3460 children += TYPE_N_BASECLASSES (type);
3463 /* FIXME: save children in var. */
3470 type = get_value_type (var->parent);
3471 adjust_value_for_child_access (NULL, &type, NULL);
3473 cplus_class_num_children (type, kids);
3474 if (strcmp (var->name, "public") == 0)
3475 children = kids[v_public];
3476 else if (strcmp (var->name, "private") == 0)
3477 children = kids[v_private];
3479 children = kids[v_protected];
3484 children = c_number_of_children (var);
3489 /* Compute # of public, private, and protected variables in this class.
3490 That means we need to descend into all baseclasses and find out
3491 how many are there, too. */
3493 cplus_class_num_children (struct type *type, int children[3])
3495 int i, vptr_fieldno;
3496 struct type *basetype = NULL;
3498 children[v_public] = 0;
3499 children[v_private] = 0;
3500 children[v_protected] = 0;
3502 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3503 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
3505 /* If we have a virtual table pointer, omit it. Even if virtual
3506 table pointers are not specifically marked in the debug info,
3507 they should be artificial. */
3508 if ((type == basetype && i == vptr_fieldno)
3509 || TYPE_FIELD_ARTIFICIAL (type, i))
3512 if (TYPE_FIELD_PROTECTED (type, i))
3513 children[v_protected]++;
3514 else if (TYPE_FIELD_PRIVATE (type, i))
3515 children[v_private]++;
3517 children[v_public]++;
3522 cplus_name_of_variable (struct varobj *parent)
3524 return c_name_of_variable (parent);
3527 enum accessibility { private_field, protected_field, public_field };
3529 /* Check if field INDEX of TYPE has the specified accessibility.
3530 Return 0 if so and 1 otherwise. */
3532 match_accessibility (struct type *type, int index, enum accessibility acc)
3534 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
3536 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
3538 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
3539 && !TYPE_FIELD_PROTECTED (type, index))
3546 cplus_describe_child (struct varobj *parent, int index,
3547 char **cname, struct value **cvalue, struct type **ctype,
3548 char **cfull_expression)
3550 struct value *value;
3553 char *parent_expression = NULL;
3561 if (cfull_expression)
3562 *cfull_expression = NULL;
3564 if (CPLUS_FAKE_CHILD (parent))
3566 value = parent->parent->value;
3567 type = get_value_type (parent->parent);
3568 if (cfull_expression)
3570 = varobj_get_path_expr (get_path_expr_parent (parent->parent));
3574 value = parent->value;
3575 type = get_value_type (parent);
3576 if (cfull_expression)
3578 = varobj_get_path_expr (get_path_expr_parent (parent));
3581 adjust_value_for_child_access (&value, &type, &was_ptr);
3583 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3584 || TYPE_CODE (type) == TYPE_CODE_UNION)
3586 char *join = was_ptr ? "->" : ".";
3588 if (CPLUS_FAKE_CHILD (parent))
3590 /* The fields of the class type are ordered as they
3591 appear in the class. We are given an index for a
3592 particular access control type ("public","protected",
3593 or "private"). We must skip over fields that don't
3594 have the access control we are looking for to properly
3595 find the indexed field. */
3596 int type_index = TYPE_N_BASECLASSES (type);
3597 enum accessibility acc = public_field;
3599 struct type *basetype = NULL;
3600 const char *field_name;
3602 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3603 if (strcmp (parent->name, "private") == 0)
3604 acc = private_field;
3605 else if (strcmp (parent->name, "protected") == 0)
3606 acc = protected_field;
3610 if ((type == basetype && type_index == vptr_fieldno)
3611 || TYPE_FIELD_ARTIFICIAL (type, type_index))
3613 else if (match_accessibility (type, type_index, acc))
3619 /* If the type is anonymous and the field has no name,
3620 set an appopriate name. */
3621 field_name = TYPE_FIELD_NAME (type, type_index);
3622 if (field_name == NULL || *field_name == '\0')
3626 if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3627 == TYPE_CODE_STRUCT)
3628 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3629 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3631 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3634 if (cfull_expression)
3635 *cfull_expression = xstrdup ("");
3640 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
3642 if (cfull_expression)
3644 = xstrprintf ("((%s)%s%s)", parent_expression, join,
3648 if (cvalue && value)
3649 *cvalue = value_struct_element_index (value, type_index);
3652 *ctype = TYPE_FIELD_TYPE (type, type_index);
3654 else if (index < TYPE_N_BASECLASSES (type))
3656 /* This is a baseclass. */
3658 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
3660 if (cvalue && value)
3661 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
3665 *ctype = TYPE_FIELD_TYPE (type, index);
3668 if (cfull_expression)
3670 char *ptr = was_ptr ? "*" : "";
3672 /* Cast the parent to the base' type. Note that in gdb,
3675 will create an lvalue, for all appearences, so we don't
3676 need to use more fancy:
3680 When we are in the scope of the base class or of one
3681 of its children, the type field name will be interpreted
3682 as a constructor, if it exists. Therefore, we must
3683 indicate that the name is a class name by using the
3684 'class' keyword. See PR mi/11912 */
3685 *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
3687 TYPE_FIELD_NAME (type, index),
3694 char *access = NULL;
3697 cplus_class_num_children (type, children);
3699 /* Everything beyond the baseclasses can
3700 only be "public", "private", or "protected"
3702 The special "fake" children are always output by varobj in
3703 this order. So if INDEX == 2, it MUST be "protected". */
3704 index -= TYPE_N_BASECLASSES (type);
3708 if (children[v_public] > 0)
3710 else if (children[v_private] > 0)
3713 access = "protected";
3716 if (children[v_public] > 0)
3718 if (children[v_private] > 0)
3721 access = "protected";
3723 else if (children[v_private] > 0)
3724 access = "protected";
3727 /* Must be protected. */
3728 access = "protected";
3735 gdb_assert (access);
3737 *cname = xstrdup (access);
3739 /* Value and type and full expression are null here. */
3744 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
3749 cplus_name_of_child (struct varobj *parent, int index)
3753 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
3758 cplus_path_expr_of_child (struct varobj *child)
3760 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
3762 return child->path_expr;
3765 static struct value *
3766 cplus_value_of_root (struct varobj **var_handle)
3768 return c_value_of_root (var_handle);
3771 static struct value *
3772 cplus_value_of_child (struct varobj *parent, int index)
3774 struct value *value = NULL;
3776 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
3780 static struct type *
3781 cplus_type_of_child (struct varobj *parent, int index)
3783 struct type *type = NULL;
3785 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
3790 cplus_value_of_variable (struct varobj *var,
3791 enum varobj_display_formats format)
3794 /* If we have one of our special types, don't print out
3796 if (CPLUS_FAKE_CHILD (var))
3797 return xstrdup ("");
3799 return c_value_of_variable (var, format);
3805 java_number_of_children (struct varobj *var)
3807 return cplus_number_of_children (var);
3811 java_name_of_variable (struct varobj *parent)
3815 name = cplus_name_of_variable (parent);
3816 /* If the name has "-" in it, it is because we
3817 needed to escape periods in the name... */
3820 while (*p != '\000')
3831 java_name_of_child (struct varobj *parent, int index)
3835 name = cplus_name_of_child (parent, index);
3836 /* Escape any periods in the name... */
3839 while (*p != '\000')
3850 java_path_expr_of_child (struct varobj *child)
3855 static struct value *
3856 java_value_of_root (struct varobj **var_handle)
3858 return cplus_value_of_root (var_handle);
3861 static struct value *
3862 java_value_of_child (struct varobj *parent, int index)
3864 return cplus_value_of_child (parent, index);
3867 static struct type *
3868 java_type_of_child (struct varobj *parent, int index)
3870 return cplus_type_of_child (parent, index);
3874 java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3876 return cplus_value_of_variable (var, format);
3879 /* Ada specific callbacks for VAROBJs. */
3882 ada_number_of_children (struct varobj *var)
3884 return c_number_of_children (var);
3888 ada_name_of_variable (struct varobj *parent)
3890 return c_name_of_variable (parent);
3894 ada_name_of_child (struct varobj *parent, int index)
3896 return c_name_of_child (parent, index);
3900 ada_path_expr_of_child (struct varobj *child)
3902 return c_path_expr_of_child (child);
3905 static struct value *
3906 ada_value_of_root (struct varobj **var_handle)
3908 return c_value_of_root (var_handle);
3911 static struct value *
3912 ada_value_of_child (struct varobj *parent, int index)
3914 return c_value_of_child (parent, index);
3917 static struct type *
3918 ada_type_of_child (struct varobj *parent, int index)
3920 return c_type_of_child (parent, index);
3924 ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3926 return c_value_of_variable (var, format);
3929 /* Implement the "value_has_mutated" routine for Ada. */
3932 ada_value_has_mutated (struct varobj *var, struct value *new_val,
3933 struct type *new_type)
3935 /* Unimplemented for now. */
3939 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
3940 with an arbitrary caller supplied DATA pointer. */
3943 all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
3945 struct varobj_root *var_root, *var_root_next;
3947 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
3949 for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
3951 var_root_next = var_root->next;
3953 (*func) (var_root->rootvar, data);
3957 extern void _initialize_varobj (void);
3959 _initialize_varobj (void)
3961 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
3963 varobj_table = xmalloc (sizeof_table);
3964 memset (varobj_table, 0, sizeof_table);
3966 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
3968 _("Set varobj debugging."),
3969 _("Show varobj debugging."),
3970 _("When non-zero, varobj debugging is enabled."),
3971 NULL, show_varobjdebug,
3972 &setlist, &showlist);
3975 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
3976 defined on globals. It is a helper for varobj_invalidate. */
3979 varobj_invalidate_iter (struct varobj *var, void *unused)
3981 /* Floating varobjs are reparsed on each stop, so we don't care if the
3982 presently parsed expression refers to something that's gone. */
3983 if (var->root->floating)
3986 /* global var must be re-evaluated. */
3987 if (var->root->valid_block == NULL)
3989 struct varobj *tmp_var;
3991 /* Try to create a varobj with same expression. If we succeed
3992 replace the old varobj, otherwise invalidate it. */
3993 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
3995 if (tmp_var != NULL)
3997 tmp_var->obj_name = xstrdup (var->obj_name);
3998 varobj_delete (var, NULL, 0);
3999 install_variable (tmp_var);
4002 var->root->is_valid = 0;
4004 else /* locals must be invalidated. */
4005 var->root->is_valid = 0;
4008 /* Invalidate the varobjs that are tied to locals and re-create the ones that
4009 are defined on globals.
4010 Invalidated varobjs will be always printed in_scope="invalid". */
4013 varobj_invalidate (void)
4015 all_root_varobjs (varobj_invalidate_iter, NULL);