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 /* The language specific vector */
387 struct language_specific
390 /* The language of this variable. */
391 enum varobj_languages language;
393 /* The number of children of PARENT. */
394 int (*number_of_children) (struct varobj * parent);
396 /* The name (expression) of a root varobj. */
397 char *(*name_of_variable) (struct varobj * parent);
399 /* The name of the INDEX'th child of PARENT. */
400 char *(*name_of_child) (struct varobj * parent, int index);
402 /* Returns the rooted expression of CHILD, which is a variable
403 obtain that has some parent. */
404 char *(*path_expr_of_child) (struct varobj * child);
406 /* The ``struct value *'' of the root variable ROOT. */
407 struct value *(*value_of_root) (struct varobj ** root_handle);
409 /* The ``struct value *'' of the INDEX'th child of PARENT. */
410 struct value *(*value_of_child) (struct varobj * parent, int index);
412 /* The type of the INDEX'th child of PARENT. */
413 struct type *(*type_of_child) (struct varobj * parent, int index);
415 /* The current value of VAR. */
416 char *(*value_of_variable) (struct varobj * var,
417 enum varobj_display_formats format);
420 /* Array of known source language routines. */
421 static struct language_specific languages[vlang_end] = {
422 /* Unknown (try treating as C). */
425 c_number_of_children,
428 c_path_expr_of_child,
437 c_number_of_children,
440 c_path_expr_of_child,
449 cplus_number_of_children,
450 cplus_name_of_variable,
452 cplus_path_expr_of_child,
454 cplus_value_of_child,
456 cplus_value_of_variable}
461 java_number_of_children,
462 java_name_of_variable,
464 java_path_expr_of_child,
468 java_value_of_variable},
472 ada_number_of_children,
473 ada_name_of_variable,
475 ada_path_expr_of_child,
479 ada_value_of_variable}
482 /* A little convenience enum for dealing with C++/Java. */
485 v_public = 0, v_private, v_protected
490 /* Mappings of varobj_display_formats enums to gdb's format codes. */
491 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
493 /* Header of the list of root variable objects. */
494 static struct varobj_root *rootlist;
496 /* Prime number indicating the number of buckets in the hash table. */
497 /* A prime large enough to avoid too many colisions. */
498 #define VAROBJ_TABLE_SIZE 227
500 /* Pointer to the varobj hash table (built at run time). */
501 static struct vlist **varobj_table;
503 /* Is the variable X one of our "fake" children? */
504 #define CPLUS_FAKE_CHILD(x) \
505 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
508 /* API Implementation */
510 is_root_p (struct varobj *var)
512 return (var->root->rootvar == var);
516 /* Helper function to install a Python environment suitable for
517 use during operations on VAR. */
519 varobj_ensure_python_env (struct varobj *var)
521 return ensure_python_env (var->root->exp->gdbarch,
522 var->root->exp->language_defn);
526 /* Creates a varobj (not its children). */
528 /* Return the full FRAME which corresponds to the given CORE_ADDR
529 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
531 static struct frame_info *
532 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
534 struct frame_info *frame = NULL;
536 if (frame_addr == (CORE_ADDR) 0)
539 for (frame = get_current_frame ();
541 frame = get_prev_frame (frame))
543 /* The CORE_ADDR we get as argument was parsed from a string GDB
544 output as $fp. This output got truncated to gdbarch_addr_bit.
545 Truncate the frame base address in the same manner before
546 comparing it against our argument. */
547 CORE_ADDR frame_base = get_frame_base_address (frame);
548 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
550 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
551 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
553 if (frame_base == frame_addr)
561 varobj_create (char *objname,
562 char *expression, CORE_ADDR frame, enum varobj_type type)
565 struct cleanup *old_chain;
567 /* Fill out a varobj structure for the (root) variable being constructed. */
568 var = new_root_variable ();
569 old_chain = make_cleanup_free_variable (var);
571 if (expression != NULL)
573 struct frame_info *fi;
574 struct frame_id old_id = null_frame_id;
577 enum varobj_languages lang;
578 struct value *value = NULL;
579 volatile struct gdb_exception except;
581 /* Parse and evaluate the expression, filling in as much of the
582 variable's data as possible. */
584 if (has_stack_frames ())
586 /* Allow creator to specify context of variable. */
587 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
588 fi = get_selected_frame (NULL);
590 /* FIXME: cagney/2002-11-23: This code should be doing a
591 lookup using the frame ID and not just the frame's
592 ``address''. This, of course, means an interface
593 change. However, with out that interface change ISAs,
594 such as the ia64 with its two stacks, won't work.
595 Similar goes for the case where there is a frameless
597 fi = find_frame_addr_in_frame_chain (frame);
602 /* frame = -2 means always use selected frame. */
603 if (type == USE_SELECTED_FRAME)
604 var->root->floating = 1;
608 block = get_frame_block (fi, 0);
611 innermost_block = NULL;
612 /* Wrap the call to parse expression, so we can
613 return a sensible error. */
614 TRY_CATCH (except, RETURN_MASK_ERROR)
616 var->root->exp = parse_exp_1 (&p, block, 0);
619 if (except.reason < 0)
621 do_cleanups (old_chain);
625 /* Don't allow variables to be created for types. */
626 if (var->root->exp->elts[0].opcode == OP_TYPE)
628 do_cleanups (old_chain);
629 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
630 " as an expression.\n");
634 var->format = variable_default_display (var);
635 var->root->valid_block = innermost_block;
636 var->name = xstrdup (expression);
637 /* For a root var, the name and the expr are the same. */
638 var->path_expr = xstrdup (expression);
640 /* When the frame is different from the current frame,
641 we must select the appropriate frame before parsing
642 the expression, otherwise the value will not be current.
643 Since select_frame is so benign, just call it for all cases. */
646 /* User could specify explicit FRAME-ADDR which was not found but
647 EXPRESSION is frame specific and we would not be able to evaluate
648 it correctly next time. With VALID_BLOCK set we must also set
649 FRAME and THREAD_ID. */
651 error (_("Failed to find the specified frame"));
653 var->root->frame = get_frame_id (fi);
654 var->root->thread_id = pid_to_thread_id (inferior_ptid);
655 old_id = get_frame_id (get_selected_frame (NULL));
659 /* We definitely need to catch errors here.
660 If evaluate_expression succeeds we got the value we wanted.
661 But if it fails, we still go on with a call to evaluate_type(). */
662 TRY_CATCH (except, RETURN_MASK_ERROR)
664 value = evaluate_expression (var->root->exp);
667 if (except.reason < 0)
669 /* Error getting the value. Try to at least get the
671 struct value *type_only_value = evaluate_type (var->root->exp);
673 var->type = value_type (type_only_value);
676 var->type = value_type (value);
678 install_new_value (var, value, 1 /* Initial assignment */);
680 /* Set language info */
681 lang = variable_language (var);
682 var->root->lang = &languages[lang];
684 /* Set ourselves as our root. */
685 var->root->rootvar = var;
687 /* Reset the selected frame. */
688 if (frame_id_p (old_id))
689 select_frame (frame_find_by_id (old_id));
692 /* If the variable object name is null, that means this
693 is a temporary variable, so don't install it. */
695 if ((var != NULL) && (objname != NULL))
697 var->obj_name = xstrdup (objname);
699 /* If a varobj name is duplicated, the install will fail so
701 if (!install_variable (var))
703 do_cleanups (old_chain);
708 discard_cleanups (old_chain);
712 /* Generates an unique name that can be used for a varobj. */
715 varobj_gen_name (void)
720 /* Generate a name for this object. */
722 obj_name = xstrprintf ("var%d", id);
727 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
728 error if OBJNAME cannot be found. */
731 varobj_get_handle (char *objname)
735 unsigned int index = 0;
738 for (chp = objname; *chp; chp++)
740 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
743 cv = *(varobj_table + index);
744 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
748 error (_("Variable object not found"));
753 /* Given the handle, return the name of the object. */
756 varobj_get_objname (struct varobj *var)
758 return var->obj_name;
761 /* Given the handle, return the expression represented by the object. */
764 varobj_get_expression (struct varobj *var)
766 return name_of_variable (var);
769 /* Deletes a varobj and all its children if only_children == 0,
770 otherwise deletes only the children; returns a malloc'ed list of
771 all the (malloc'ed) names of the variables that have been deleted
772 (NULL terminated). */
775 varobj_delete (struct varobj *var, char ***dellist, int only_children)
779 struct cpstack *result = NULL;
782 /* Initialize a stack for temporary results. */
783 cppush (&result, NULL);
786 /* Delete only the variable children. */
787 delcount = delete_variable (&result, var, 1 /* only the children */ );
789 /* Delete the variable and all its children. */
790 delcount = delete_variable (&result, var, 0 /* parent+children */ );
792 /* We may have been asked to return a list of what has been deleted. */
795 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
799 *cp = cppop (&result);
800 while ((*cp != NULL) && (mycount > 0))
804 *cp = cppop (&result);
807 if (mycount || (*cp != NULL))
808 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
817 /* Convenience function for varobj_set_visualizer. Instantiate a
818 pretty-printer for a given value. */
820 instantiate_pretty_printer (PyObject *constructor, struct value *value)
822 PyObject *val_obj = NULL;
825 val_obj = value_to_value_object (value);
829 printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
836 /* Set/Get variable object display format. */
838 enum varobj_display_formats
839 varobj_set_display_format (struct varobj *var,
840 enum varobj_display_formats format)
847 case FORMAT_HEXADECIMAL:
849 var->format = format;
853 var->format = variable_default_display (var);
856 if (varobj_value_is_changeable_p (var)
857 && var->value && !value_lazy (var->value))
859 xfree (var->print_value);
860 var->print_value = value_get_print_value (var->value, var->format, var);
866 enum varobj_display_formats
867 varobj_get_display_format (struct varobj *var)
873 varobj_get_display_hint (struct varobj *var)
878 struct cleanup *back_to = varobj_ensure_python_env (var);
880 if (var->pretty_printer)
881 result = gdbpy_get_display_hint (var->pretty_printer);
883 do_cleanups (back_to);
889 /* Return true if the varobj has items after TO, false otherwise. */
892 varobj_has_more (struct varobj *var, int to)
894 if (VEC_length (varobj_p, var->children) > to)
896 return ((to == -1 || VEC_length (varobj_p, var->children) == to)
897 && var->saved_item != NULL);
900 /* If the variable object is bound to a specific thread, that
901 is its evaluation can always be done in context of a frame
902 inside that thread, returns GDB id of the thread -- which
903 is always positive. Otherwise, returns -1. */
905 varobj_get_thread_id (struct varobj *var)
907 if (var->root->valid_block && var->root->thread_id > 0)
908 return var->root->thread_id;
914 varobj_set_frozen (struct varobj *var, int frozen)
916 /* When a variable is unfrozen, we don't fetch its value.
917 The 'not_fetched' flag remains set, so next -var-update
920 We don't fetch the value, because for structures the client
921 should do -var-update anyway. It would be bad to have different
922 client-size logic for structure and other types. */
923 var->frozen = frozen;
927 varobj_get_frozen (struct varobj *var)
932 /* A helper function that restricts a range to what is actually
933 available in a VEC. This follows the usual rules for the meaning
934 of FROM and TO -- if either is negative, the entire range is
938 restrict_range (VEC (varobj_p) *children, int *from, int *to)
940 if (*from < 0 || *to < 0)
943 *to = VEC_length (varobj_p, children);
947 if (*from > VEC_length (varobj_p, children))
948 *from = VEC_length (varobj_p, children);
949 if (*to > VEC_length (varobj_p, children))
950 *to = VEC_length (varobj_p, children);
958 /* A helper for update_dynamic_varobj_children that installs a new
959 child when needed. */
962 install_dynamic_child (struct varobj *var,
963 VEC (varobj_p) **changed,
964 VEC (varobj_p) **new,
965 VEC (varobj_p) **unchanged,
971 if (VEC_length (varobj_p, var->children) < index + 1)
973 /* There's no child yet. */
974 struct varobj *child = varobj_add_child (var, name, value);
978 VEC_safe_push (varobj_p, *new, child);
984 varobj_p existing = VEC_index (varobj_p, var->children, index);
986 if (install_new_value (existing, value, 0))
989 VEC_safe_push (varobj_p, *changed, existing);
992 VEC_safe_push (varobj_p, *unchanged, existing);
997 dynamic_varobj_has_child_method (struct varobj *var)
999 struct cleanup *back_to;
1000 PyObject *printer = var->pretty_printer;
1003 back_to = varobj_ensure_python_env (var);
1004 result = PyObject_HasAttr (printer, gdbpy_children_cst);
1005 do_cleanups (back_to);
1012 update_dynamic_varobj_children (struct varobj *var,
1013 VEC (varobj_p) **changed,
1014 VEC (varobj_p) **new,
1015 VEC (varobj_p) **unchanged,
1017 int update_children,
1022 struct cleanup *back_to;
1025 PyObject *printer = var->pretty_printer;
1027 back_to = varobj_ensure_python_env (var);
1030 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
1032 do_cleanups (back_to);
1036 if (update_children || !var->child_iter)
1038 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
1043 gdbpy_print_stack ();
1044 error (_("Null value returned for children"));
1047 make_cleanup_py_decref (children);
1049 if (!PyIter_Check (children))
1050 error (_("Returned value is not iterable"));
1052 Py_XDECREF (var->child_iter);
1053 var->child_iter = PyObject_GetIter (children);
1054 if (!var->child_iter)
1056 gdbpy_print_stack ();
1057 error (_("Could not get children iterator"));
1060 Py_XDECREF (var->saved_item);
1061 var->saved_item = NULL;
1066 i = VEC_length (varobj_p, var->children);
1068 /* We ask for one extra child, so that MI can report whether there
1069 are more children. */
1070 for (; to < 0 || i < to + 1; ++i)
1075 /* See if there was a leftover from last time. */
1076 if (var->saved_item)
1078 item = var->saved_item;
1079 var->saved_item = NULL;
1082 item = PyIter_Next (var->child_iter);
1086 /* Normal end of iteration. */
1087 if (!PyErr_Occurred ())
1090 /* If we got a memory error, just use the text as the
1092 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
1094 PyObject *type, *value, *trace;
1095 char *name_str, *value_str;
1097 PyErr_Fetch (&type, &value, &trace);
1098 value_str = gdbpy_exception_to_string (type, value);
1104 gdbpy_print_stack ();
1108 name_str = xstrprintf ("<error at %d>", i);
1109 item = Py_BuildValue ("(ss)", name_str, value_str);
1114 gdbpy_print_stack ();
1122 /* Any other kind of error. */
1123 gdbpy_print_stack ();
1128 /* We don't want to push the extra child on any report list. */
1129 if (to < 0 || i < to)
1134 struct cleanup *inner;
1135 int can_mention = from < 0 || i >= from;
1137 inner = make_cleanup_py_decref (item);
1139 if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
1141 gdbpy_print_stack ();
1142 error (_("Invalid item from the child list"));
1145 v = convert_value_from_python (py_v);
1147 gdbpy_print_stack ();
1148 install_dynamic_child (var, can_mention ? changed : NULL,
1149 can_mention ? new : NULL,
1150 can_mention ? unchanged : NULL,
1151 can_mention ? cchanged : NULL, i, name, v);
1152 do_cleanups (inner);
1156 Py_XDECREF (var->saved_item);
1157 var->saved_item = item;
1159 /* We want to truncate the child list just before this
1168 if (i < VEC_length (varobj_p, var->children))
1173 for (j = i; j < VEC_length (varobj_p, var->children); ++j)
1174 varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
1175 VEC_truncate (varobj_p, var->children, i);
1178 /* If there are fewer children than requested, note that the list of
1179 children changed. */
1180 if (to >= 0 && VEC_length (varobj_p, var->children) < to)
1183 var->num_children = VEC_length (varobj_p, var->children);
1185 do_cleanups (back_to);
1189 gdb_assert (0 && "should never be called if Python is not enabled");
1194 varobj_get_num_children (struct varobj *var)
1196 if (var->num_children == -1)
1198 if (var->pretty_printer)
1202 /* If we have a dynamic varobj, don't report -1 children.
1203 So, try to fetch some children first. */
1204 update_dynamic_varobj_children (var, NULL, NULL, NULL, &dummy,
1208 var->num_children = number_of_children (var);
1211 return var->num_children >= 0 ? var->num_children : 0;
1214 /* Creates a list of the immediate children of a variable object;
1215 the return code is the number of such children or -1 on error. */
1218 varobj_list_children (struct varobj *var, int *from, int *to)
1221 int i, children_changed;
1223 var->children_requested = 1;
1225 if (var->pretty_printer)
1227 /* This, in theory, can result in the number of children changing without
1228 frontend noticing. But well, calling -var-list-children on the same
1229 varobj twice is not something a sane frontend would do. */
1230 update_dynamic_varobj_children (var, NULL, NULL, NULL, &children_changed,
1232 restrict_range (var->children, from, to);
1233 return var->children;
1236 if (var->num_children == -1)
1237 var->num_children = number_of_children (var);
1239 /* If that failed, give up. */
1240 if (var->num_children == -1)
1241 return var->children;
1243 /* If we're called when the list of children is not yet initialized,
1244 allocate enough elements in it. */
1245 while (VEC_length (varobj_p, var->children) < var->num_children)
1246 VEC_safe_push (varobj_p, var->children, NULL);
1248 for (i = 0; i < var->num_children; i++)
1250 varobj_p existing = VEC_index (varobj_p, var->children, i);
1252 if (existing == NULL)
1254 /* Either it's the first call to varobj_list_children for
1255 this variable object, and the child was never created,
1256 or it was explicitly deleted by the client. */
1257 name = name_of_child (var, i);
1258 existing = create_child (var, i, name);
1259 VEC_replace (varobj_p, var->children, i, existing);
1263 restrict_range (var->children, from, to);
1264 return var->children;
1269 static struct varobj *
1270 varobj_add_child (struct varobj *var, const char *name, struct value *value)
1272 varobj_p v = create_child_with_value (var,
1273 VEC_length (varobj_p, var->children),
1276 VEC_safe_push (varobj_p, var->children, v);
1280 #endif /* HAVE_PYTHON */
1282 /* Obtain the type of an object Variable as a string similar to the one gdb
1283 prints on the console. */
1286 varobj_get_type (struct varobj *var)
1288 /* For the "fake" variables, do not return a type. (It's type is
1290 Do not return a type for invalid variables as well. */
1291 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
1294 return type_to_string (var->type);
1297 /* Obtain the type of an object variable. */
1300 varobj_get_gdb_type (struct varobj *var)
1305 /* Is VAR a path expression parent, i.e., can it be used to construct
1306 a valid path expression? */
1309 is_path_expr_parent (struct varobj *var)
1313 /* "Fake" children are not path_expr parents. */
1314 if (CPLUS_FAKE_CHILD (var))
1317 type = get_value_type (var);
1319 /* Anonymous unions and structs are also not path_expr parents. */
1320 return !((TYPE_CODE (type) == TYPE_CODE_STRUCT
1321 || TYPE_CODE (type) == TYPE_CODE_UNION)
1322 && TYPE_NAME (type) == NULL);
1325 /* Return the path expression parent for VAR. */
1327 static struct varobj *
1328 get_path_expr_parent (struct varobj *var)
1330 struct varobj *parent = var;
1332 while (!is_root_p (parent) && !is_path_expr_parent (parent))
1333 parent = parent->parent;
1338 /* Return a pointer to the full rooted expression of varobj VAR.
1339 If it has not been computed yet, compute it. */
1341 varobj_get_path_expr (struct varobj *var)
1343 if (var->path_expr != NULL)
1344 return var->path_expr;
1347 /* For root varobjs, we initialize path_expr
1348 when creating varobj, so here it should be
1350 gdb_assert (!is_root_p (var));
1351 return (*var->root->lang->path_expr_of_child) (var);
1355 enum varobj_languages
1356 varobj_get_language (struct varobj *var)
1358 return variable_language (var);
1362 varobj_get_attributes (struct varobj *var)
1366 if (varobj_editable_p (var))
1367 /* FIXME: define masks for attributes. */
1368 attributes |= 0x00000001; /* Editable */
1374 varobj_pretty_printed_p (struct varobj *var)
1376 return var->pretty_printer != NULL;
1380 varobj_get_formatted_value (struct varobj *var,
1381 enum varobj_display_formats format)
1383 return my_value_of_variable (var, format);
1387 varobj_get_value (struct varobj *var)
1389 return my_value_of_variable (var, var->format);
1392 /* Set the value of an object variable (if it is editable) to the
1393 value of the given expression. */
1394 /* Note: Invokes functions that can call error(). */
1397 varobj_set_value (struct varobj *var, char *expression)
1399 struct value *val = NULL; /* Initialize to keep gcc happy. */
1400 /* The argument "expression" contains the variable's new value.
1401 We need to first construct a legal expression for this -- ugh! */
1402 /* Does this cover all the bases? */
1403 struct expression *exp;
1404 struct value *value = NULL; /* Initialize to keep gcc happy. */
1405 int saved_input_radix = input_radix;
1406 char *s = expression;
1407 volatile struct gdb_exception except;
1409 gdb_assert (varobj_editable_p (var));
1411 input_radix = 10; /* ALWAYS reset to decimal temporarily. */
1412 exp = parse_exp_1 (&s, 0, 0);
1413 TRY_CATCH (except, RETURN_MASK_ERROR)
1415 value = evaluate_expression (exp);
1418 if (except.reason < 0)
1420 /* We cannot proceed without a valid expression. */
1425 /* All types that are editable must also be changeable. */
1426 gdb_assert (varobj_value_is_changeable_p (var));
1428 /* The value of a changeable variable object must not be lazy. */
1429 gdb_assert (!value_lazy (var->value));
1431 /* Need to coerce the input. We want to check if the
1432 value of the variable object will be different
1433 after assignment, and the first thing value_assign
1434 does is coerce the input.
1435 For example, if we are assigning an array to a pointer variable we
1436 should compare the pointer with the array's address, not with the
1438 value = coerce_array (value);
1440 /* The new value may be lazy. value_assign, or
1441 rather value_contents, will take care of this. */
1442 TRY_CATCH (except, RETURN_MASK_ERROR)
1444 val = value_assign (var->value, value);
1447 if (except.reason < 0)
1450 /* If the value has changed, record it, so that next -var-update can
1451 report this change. If a variable had a value of '1', we've set it
1452 to '333' and then set again to '1', when -var-update will report this
1453 variable as changed -- because the first assignment has set the
1454 'updated' flag. There's no need to optimize that, because return value
1455 of -var-update should be considered an approximation. */
1456 var->updated = install_new_value (var, val, 0 /* Compare values. */);
1457 input_radix = saved_input_radix;
1463 /* A helper function to install a constructor function and visualizer
1467 install_visualizer (struct varobj *var, PyObject *constructor,
1468 PyObject *visualizer)
1470 Py_XDECREF (var->constructor);
1471 var->constructor = constructor;
1473 Py_XDECREF (var->pretty_printer);
1474 var->pretty_printer = visualizer;
1476 Py_XDECREF (var->child_iter);
1477 var->child_iter = NULL;
1480 /* Install the default visualizer for VAR. */
1483 install_default_visualizer (struct varobj *var)
1485 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1486 if (CPLUS_FAKE_CHILD (var))
1489 if (pretty_printing)
1491 PyObject *pretty_printer = NULL;
1495 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
1496 if (! pretty_printer)
1498 gdbpy_print_stack ();
1499 error (_("Cannot instantiate printer for default visualizer"));
1503 if (pretty_printer == Py_None)
1505 Py_DECREF (pretty_printer);
1506 pretty_printer = NULL;
1509 install_visualizer (var, NULL, pretty_printer);
1513 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1514 make a new object. */
1517 construct_visualizer (struct varobj *var, PyObject *constructor)
1519 PyObject *pretty_printer;
1521 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1522 if (CPLUS_FAKE_CHILD (var))
1525 Py_INCREF (constructor);
1526 if (constructor == Py_None)
1527 pretty_printer = NULL;
1530 pretty_printer = instantiate_pretty_printer (constructor, var->value);
1531 if (! pretty_printer)
1533 gdbpy_print_stack ();
1534 Py_DECREF (constructor);
1535 constructor = Py_None;
1536 Py_INCREF (constructor);
1539 if (pretty_printer == Py_None)
1541 Py_DECREF (pretty_printer);
1542 pretty_printer = NULL;
1546 install_visualizer (var, constructor, pretty_printer);
1549 #endif /* HAVE_PYTHON */
1551 /* A helper function for install_new_value. This creates and installs
1552 a visualizer for VAR, if appropriate. */
1555 install_new_value_visualizer (struct varobj *var)
1558 /* If the constructor is None, then we want the raw value. If VAR
1559 does not have a value, just skip this. */
1560 if (var->constructor != Py_None && var->value)
1562 struct cleanup *cleanup;
1564 cleanup = varobj_ensure_python_env (var);
1566 if (!var->constructor)
1567 install_default_visualizer (var);
1569 construct_visualizer (var, var->constructor);
1571 do_cleanups (cleanup);
1578 /* Assign a new value to a variable object. If INITIAL is non-zero,
1579 this is the first assignement after the variable object was just
1580 created, or changed type. In that case, just assign the value
1582 Otherwise, assign the new value, and return 1 if the value is
1583 different from the current one, 0 otherwise. The comparison is
1584 done on textual representation of value. Therefore, some types
1585 need not be compared. E.g. for structures the reported value is
1586 always "{...}", so no comparison is necessary here. If the old
1587 value was NULL and new one is not, or vice versa, we always return 1.
1589 The VALUE parameter should not be released -- the function will
1590 take care of releasing it when needed. */
1592 install_new_value (struct varobj *var, struct value *value, int initial)
1597 int intentionally_not_fetched = 0;
1598 char *print_value = NULL;
1600 /* We need to know the varobj's type to decide if the value should
1601 be fetched or not. C++ fake children (public/protected/private)
1602 don't have a type. */
1603 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1604 changeable = varobj_value_is_changeable_p (var);
1606 /* If the type has custom visualizer, we consider it to be always
1607 changeable. FIXME: need to make sure this behaviour will not
1608 mess up read-sensitive values. */
1609 if (var->pretty_printer)
1612 need_to_fetch = changeable;
1614 /* We are not interested in the address of references, and given
1615 that in C++ a reference is not rebindable, it cannot
1616 meaningfully change. So, get hold of the real value. */
1618 value = coerce_ref (value);
1620 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
1621 /* For unions, we need to fetch the value implicitly because
1622 of implementation of union member fetch. When gdb
1623 creates a value for a field and the value of the enclosing
1624 structure is not lazy, it immediately copies the necessary
1625 bytes from the enclosing values. If the enclosing value is
1626 lazy, the call to value_fetch_lazy on the field will read
1627 the data from memory. For unions, that means we'll read the
1628 same memory more than once, which is not desirable. So
1632 /* The new value might be lazy. If the type is changeable,
1633 that is we'll be comparing values of this type, fetch the
1634 value now. Otherwise, on the next update the old value
1635 will be lazy, which means we've lost that old value. */
1636 if (need_to_fetch && value && value_lazy (value))
1638 struct varobj *parent = var->parent;
1639 int frozen = var->frozen;
1641 for (; !frozen && parent; parent = parent->parent)
1642 frozen |= parent->frozen;
1644 if (frozen && initial)
1646 /* For variables that are frozen, or are children of frozen
1647 variables, we don't do fetch on initial assignment.
1648 For non-initial assignemnt we do the fetch, since it means we're
1649 explicitly asked to compare the new value with the old one. */
1650 intentionally_not_fetched = 1;
1654 volatile struct gdb_exception except;
1656 TRY_CATCH (except, RETURN_MASK_ERROR)
1658 value_fetch_lazy (value);
1661 if (except.reason < 0)
1663 /* Set the value to NULL, so that for the next -var-update,
1664 we don't try to compare the new value with this value,
1665 that we couldn't even read. */
1671 /* Get a reference now, before possibly passing it to any Python
1672 code that might release it. */
1674 value_incref (value);
1676 /* Below, we'll be comparing string rendering of old and new
1677 values. Don't get string rendering if the value is
1678 lazy -- if it is, the code above has decided that the value
1679 should not be fetched. */
1680 if (value && !value_lazy (value) && !var->pretty_printer)
1681 print_value = value_get_print_value (value, var->format, var);
1683 /* If the type is changeable, compare the old and the new values.
1684 If this is the initial assignment, we don't have any old value
1686 if (!initial && changeable)
1688 /* If the value of the varobj was changed by -var-set-value,
1689 then the value in the varobj and in the target is the same.
1690 However, that value is different from the value that the
1691 varobj had after the previous -var-update. So need to the
1692 varobj as changed. */
1697 else if (! var->pretty_printer)
1699 /* Try to compare the values. That requires that both
1700 values are non-lazy. */
1701 if (var->not_fetched && value_lazy (var->value))
1703 /* This is a frozen varobj and the value was never read.
1704 Presumably, UI shows some "never read" indicator.
1705 Now that we've fetched the real value, we need to report
1706 this varobj as changed so that UI can show the real
1710 else if (var->value == NULL && value == NULL)
1713 else if (var->value == NULL || value == NULL)
1719 gdb_assert (!value_lazy (var->value));
1720 gdb_assert (!value_lazy (value));
1722 gdb_assert (var->print_value != NULL && print_value != NULL);
1723 if (strcmp (var->print_value, print_value) != 0)
1729 if (!initial && !changeable)
1731 /* For values that are not changeable, we don't compare the values.
1732 However, we want to notice if a value was not NULL and now is NULL,
1733 or vise versa, so that we report when top-level varobjs come in scope
1734 and leave the scope. */
1735 changed = (var->value != NULL) != (value != NULL);
1738 /* We must always keep the new value, since children depend on it. */
1739 if (var->value != NULL && var->value != value)
1740 value_free (var->value);
1742 if (value && value_lazy (value) && intentionally_not_fetched)
1743 var->not_fetched = 1;
1745 var->not_fetched = 0;
1748 install_new_value_visualizer (var);
1750 /* If we installed a pretty-printer, re-compare the printed version
1751 to see if the variable changed. */
1752 if (var->pretty_printer)
1754 xfree (print_value);
1755 print_value = value_get_print_value (var->value, var->format, var);
1756 if ((var->print_value == NULL && print_value != NULL)
1757 || (var->print_value != NULL && print_value == NULL)
1758 || (var->print_value != NULL && print_value != NULL
1759 && strcmp (var->print_value, print_value) != 0))
1762 if (var->print_value)
1763 xfree (var->print_value);
1764 var->print_value = print_value;
1766 gdb_assert (!var->value || value_type (var->value));
1771 /* Return the requested range for a varobj. VAR is the varobj. FROM
1772 and TO are out parameters; *FROM and *TO will be set to the
1773 selected sub-range of VAR. If no range was selected using
1774 -var-set-update-range, then both will be -1. */
1776 varobj_get_child_range (struct varobj *var, int *from, int *to)
1782 /* Set the selected sub-range of children of VAR to start at index
1783 FROM and end at index TO. If either FROM or TO is less than zero,
1784 this is interpreted as a request for all children. */
1786 varobj_set_child_range (struct varobj *var, int from, int to)
1793 varobj_set_visualizer (struct varobj *var, const char *visualizer)
1796 PyObject *mainmod, *globals, *constructor;
1797 struct cleanup *back_to;
1799 back_to = varobj_ensure_python_env (var);
1801 mainmod = PyImport_AddModule ("__main__");
1802 globals = PyModule_GetDict (mainmod);
1803 Py_INCREF (globals);
1804 make_cleanup_py_decref (globals);
1806 constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
1810 gdbpy_print_stack ();
1811 error (_("Could not evaluate visualizer expression: %s"), visualizer);
1814 construct_visualizer (var, constructor);
1815 Py_XDECREF (constructor);
1817 /* If there are any children now, wipe them. */
1818 varobj_delete (var, NULL, 1 /* children only */);
1819 var->num_children = -1;
1821 do_cleanups (back_to);
1823 error (_("Python support required"));
1827 /* Update the values for a variable and its children. This is a
1828 two-pronged attack. First, re-parse the value for the root's
1829 expression to see if it's changed. Then go all the way
1830 through its children, reconstructing them and noting if they've
1833 The EXPLICIT parameter specifies if this call is result
1834 of MI request to update this specific variable, or
1835 result of implicit -var-update *. For implicit request, we don't
1836 update frozen variables.
1838 NOTE: This function may delete the caller's varobj. If it
1839 returns TYPE_CHANGED, then it has done this and VARP will be modified
1840 to point to the new varobj. */
1842 VEC(varobj_update_result) *
1843 varobj_update (struct varobj **varp, int explicit)
1846 int type_changed = 0;
1849 VEC (varobj_update_result) *stack = NULL;
1850 VEC (varobj_update_result) *result = NULL;
1852 /* Frozen means frozen -- we don't check for any change in
1853 this varobj, including its going out of scope, or
1854 changing type. One use case for frozen varobjs is
1855 retaining previously evaluated expressions, and we don't
1856 want them to be reevaluated at all. */
1857 if (!explicit && (*varp)->frozen)
1860 if (!(*varp)->root->is_valid)
1862 varobj_update_result r = {0};
1865 r.status = VAROBJ_INVALID;
1866 VEC_safe_push (varobj_update_result, result, &r);
1870 if ((*varp)->root->rootvar == *varp)
1872 varobj_update_result r = {0};
1875 r.status = VAROBJ_IN_SCOPE;
1877 /* Update the root variable. value_of_root can return NULL
1878 if the variable is no longer around, i.e. we stepped out of
1879 the frame in which a local existed. We are letting the
1880 value_of_root variable dispose of the varobj if the type
1882 new = value_of_root (varp, &type_changed);
1885 r.type_changed = type_changed;
1886 if (install_new_value ((*varp), new, type_changed))
1890 r.status = VAROBJ_NOT_IN_SCOPE;
1891 r.value_installed = 1;
1893 if (r.status == VAROBJ_NOT_IN_SCOPE)
1895 if (r.type_changed || r.changed)
1896 VEC_safe_push (varobj_update_result, result, &r);
1900 VEC_safe_push (varobj_update_result, stack, &r);
1904 varobj_update_result r = {0};
1907 VEC_safe_push (varobj_update_result, stack, &r);
1910 /* Walk through the children, reconstructing them all. */
1911 while (!VEC_empty (varobj_update_result, stack))
1913 varobj_update_result r = *(VEC_last (varobj_update_result, stack));
1914 struct varobj *v = r.varobj;
1916 VEC_pop (varobj_update_result, stack);
1918 /* Update this variable, unless it's a root, which is already
1920 if (!r.value_installed)
1922 new = value_of_child (v->parent, v->index);
1923 if (install_new_value (v, new, 0 /* type not changed */))
1930 /* We probably should not get children of a varobj that has a
1931 pretty-printer, but for which -var-list-children was never
1933 if (v->pretty_printer)
1935 VEC (varobj_p) *changed = 0, *new = 0, *unchanged = 0;
1936 int i, children_changed = 0;
1941 if (!v->children_requested)
1945 /* If we initially did not have potential children, but
1946 now we do, consider the varobj as changed.
1947 Otherwise, if children were never requested, consider
1948 it as unchanged -- presumably, such varobj is not yet
1949 expanded in the UI, so we need not bother getting
1951 if (!varobj_has_more (v, 0))
1953 update_dynamic_varobj_children (v, NULL, NULL, NULL,
1955 if (varobj_has_more (v, 0))
1960 VEC_safe_push (varobj_update_result, result, &r);
1965 /* If update_dynamic_varobj_children returns 0, then we have
1966 a non-conforming pretty-printer, so we skip it. */
1967 if (update_dynamic_varobj_children (v, &changed, &new, &unchanged,
1968 &children_changed, 1,
1971 if (children_changed || new)
1973 r.children_changed = 1;
1976 /* Push in reverse order so that the first child is
1977 popped from the work stack first, and so will be
1978 added to result first. This does not affect
1979 correctness, just "nicer". */
1980 for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
1982 varobj_p tmp = VEC_index (varobj_p, changed, i);
1983 varobj_update_result r = {0};
1987 r.value_installed = 1;
1988 VEC_safe_push (varobj_update_result, stack, &r);
1990 for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
1992 varobj_p tmp = VEC_index (varobj_p, unchanged, i);
1996 varobj_update_result r = {0};
1999 r.value_installed = 1;
2000 VEC_safe_push (varobj_update_result, stack, &r);
2003 if (r.changed || r.children_changed)
2004 VEC_safe_push (varobj_update_result, result, &r);
2006 /* Free CHANGED and UNCHANGED, but not NEW, because NEW
2007 has been put into the result vector. */
2008 VEC_free (varobj_p, changed);
2009 VEC_free (varobj_p, unchanged);
2015 /* Push any children. Use reverse order so that the first
2016 child is popped from the work stack first, and so
2017 will be added to result first. This does not
2018 affect correctness, just "nicer". */
2019 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
2021 varobj_p c = VEC_index (varobj_p, v->children, i);
2023 /* Child may be NULL if explicitly deleted by -var-delete. */
2024 if (c != NULL && !c->frozen)
2026 varobj_update_result r = {0};
2029 VEC_safe_push (varobj_update_result, stack, &r);
2033 if (r.changed || r.type_changed)
2034 VEC_safe_push (varobj_update_result, result, &r);
2037 VEC_free (varobj_update_result, stack);
2043 /* Helper functions */
2046 * Variable object construction/destruction
2050 delete_variable (struct cpstack **resultp, struct varobj *var,
2051 int only_children_p)
2055 delete_variable_1 (resultp, &delcount, var,
2056 only_children_p, 1 /* remove_from_parent_p */ );
2061 /* Delete the variable object VAR and its children. */
2062 /* IMPORTANT NOTE: If we delete a variable which is a child
2063 and the parent is not removed we dump core. It must be always
2064 initially called with remove_from_parent_p set. */
2066 delete_variable_1 (struct cpstack **resultp, int *delcountp,
2067 struct varobj *var, int only_children_p,
2068 int remove_from_parent_p)
2072 /* Delete any children of this variable, too. */
2073 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
2075 varobj_p child = VEC_index (varobj_p, var->children, i);
2079 if (!remove_from_parent_p)
2080 child->parent = NULL;
2081 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
2083 VEC_free (varobj_p, var->children);
2085 /* if we were called to delete only the children we are done here. */
2086 if (only_children_p)
2089 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
2090 /* If the name is null, this is a temporary variable, that has not
2091 yet been installed, don't report it, it belongs to the caller... */
2092 if (var->obj_name != NULL)
2094 cppush (resultp, xstrdup (var->obj_name));
2095 *delcountp = *delcountp + 1;
2098 /* If this variable has a parent, remove it from its parent's list. */
2099 /* OPTIMIZATION: if the parent of this variable is also being deleted,
2100 (as indicated by remove_from_parent_p) we don't bother doing an
2101 expensive list search to find the element to remove when we are
2102 discarding the list afterwards. */
2103 if ((remove_from_parent_p) && (var->parent != NULL))
2105 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
2108 if (var->obj_name != NULL)
2109 uninstall_variable (var);
2111 /* Free memory associated with this variable. */
2112 free_variable (var);
2115 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
2117 install_variable (struct varobj *var)
2120 struct vlist *newvl;
2122 unsigned int index = 0;
2125 for (chp = var->obj_name; *chp; chp++)
2127 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2130 cv = *(varobj_table + index);
2131 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2135 error (_("Duplicate variable object name"));
2137 /* Add varobj to hash table. */
2138 newvl = xmalloc (sizeof (struct vlist));
2139 newvl->next = *(varobj_table + index);
2141 *(varobj_table + index) = newvl;
2143 /* If root, add varobj to root list. */
2144 if (is_root_p (var))
2146 /* Add to list of root variables. */
2147 if (rootlist == NULL)
2148 var->root->next = NULL;
2150 var->root->next = rootlist;
2151 rootlist = var->root;
2157 /* Unistall the object VAR. */
2159 uninstall_variable (struct varobj *var)
2163 struct varobj_root *cr;
2164 struct varobj_root *prer;
2166 unsigned int index = 0;
2169 /* Remove varobj from hash table. */
2170 for (chp = var->obj_name; *chp; chp++)
2172 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2175 cv = *(varobj_table + index);
2177 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2184 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
2189 ("Assertion failed: Could not find variable object \"%s\" to delete",
2195 *(varobj_table + index) = cv->next;
2197 prev->next = cv->next;
2201 /* If root, remove varobj from root list. */
2202 if (is_root_p (var))
2204 /* Remove from list of root variables. */
2205 if (rootlist == var->root)
2206 rootlist = var->root->next;
2211 while ((cr != NULL) && (cr->rootvar != var))
2218 warning (_("Assertion failed: Could not find "
2219 "varobj \"%s\" in root list"),
2226 prer->next = cr->next;
2232 /* Create and install a child of the parent of the given name. */
2233 static struct varobj *
2234 create_child (struct varobj *parent, int index, char *name)
2236 return create_child_with_value (parent, index, name,
2237 value_of_child (parent, index));
2240 /* Does CHILD represent a child with no name? This happens when
2241 the child is an anonmous struct or union and it has no field name
2242 in its parent variable.
2244 This has already been determined by *_describe_child. The easiest
2245 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
2248 is_anonymous_child (struct varobj *child)
2250 return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
2251 || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
2254 static struct varobj *
2255 create_child_with_value (struct varobj *parent, int index, const char *name,
2256 struct value *value)
2258 struct varobj *child;
2261 child = new_variable ();
2263 /* Name is allocated by name_of_child. */
2264 /* FIXME: xstrdup should not be here. */
2265 child->name = xstrdup (name);
2266 child->index = index;
2267 child->parent = parent;
2268 child->root = parent->root;
2270 if (is_anonymous_child (child))
2271 childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
2273 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
2274 child->obj_name = childs_name;
2276 install_variable (child);
2278 /* Compute the type of the child. Must do this before
2279 calling install_new_value. */
2281 /* If the child had no evaluation errors, var->value
2282 will be non-NULL and contain a valid type. */
2283 child->type = value_type (value);
2285 /* Otherwise, we must compute the type. */
2286 child->type = (*child->root->lang->type_of_child) (child->parent,
2288 install_new_value (child, value, 1);
2295 * Miscellaneous utility functions.
2298 /* Allocate memory and initialize a new variable. */
2299 static struct varobj *
2304 var = (struct varobj *) xmalloc (sizeof (struct varobj));
2306 var->path_expr = NULL;
2307 var->obj_name = NULL;
2311 var->num_children = -1;
2313 var->children = NULL;
2317 var->print_value = NULL;
2319 var->not_fetched = 0;
2320 var->children_requested = 0;
2323 var->constructor = 0;
2324 var->pretty_printer = 0;
2325 var->child_iter = 0;
2326 var->saved_item = 0;
2331 /* Allocate memory and initialize a new root variable. */
2332 static struct varobj *
2333 new_root_variable (void)
2335 struct varobj *var = new_variable ();
2337 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
2338 var->root->lang = NULL;
2339 var->root->exp = NULL;
2340 var->root->valid_block = NULL;
2341 var->root->frame = null_frame_id;
2342 var->root->floating = 0;
2343 var->root->rootvar = NULL;
2344 var->root->is_valid = 1;
2349 /* Free any allocated memory associated with VAR. */
2351 free_variable (struct varobj *var)
2354 if (var->pretty_printer)
2356 struct cleanup *cleanup = varobj_ensure_python_env (var);
2357 Py_XDECREF (var->constructor);
2358 Py_XDECREF (var->pretty_printer);
2359 Py_XDECREF (var->child_iter);
2360 Py_XDECREF (var->saved_item);
2361 do_cleanups (cleanup);
2365 value_free (var->value);
2367 /* Free the expression if this is a root variable. */
2368 if (is_root_p (var))
2370 xfree (var->root->exp);
2375 xfree (var->obj_name);
2376 xfree (var->print_value);
2377 xfree (var->path_expr);
2382 do_free_variable_cleanup (void *var)
2384 free_variable (var);
2387 static struct cleanup *
2388 make_cleanup_free_variable (struct varobj *var)
2390 return make_cleanup (do_free_variable_cleanup, var);
2393 /* This returns the type of the variable. It also skips past typedefs
2394 to return the real type of the variable.
2396 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2397 except within get_target_type and get_type. */
2398 static struct type *
2399 get_type (struct varobj *var)
2405 type = check_typedef (type);
2410 /* Return the type of the value that's stored in VAR,
2411 or that would have being stored there if the
2412 value were accessible.
2414 This differs from VAR->type in that VAR->type is always
2415 the true type of the expession in the source language.
2416 The return value of this function is the type we're
2417 actually storing in varobj, and using for displaying
2418 the values and for comparing previous and new values.
2420 For example, top-level references are always stripped. */
2421 static struct type *
2422 get_value_type (struct varobj *var)
2427 type = value_type (var->value);
2431 type = check_typedef (type);
2433 if (TYPE_CODE (type) == TYPE_CODE_REF)
2434 type = get_target_type (type);
2436 type = check_typedef (type);
2441 /* This returns the target type (or NULL) of TYPE, also skipping
2442 past typedefs, just like get_type ().
2444 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2445 except within get_target_type and get_type. */
2446 static struct type *
2447 get_target_type (struct type *type)
2451 type = TYPE_TARGET_TYPE (type);
2453 type = check_typedef (type);
2459 /* What is the default display for this variable? We assume that
2460 everything is "natural". Any exceptions? */
2461 static enum varobj_display_formats
2462 variable_default_display (struct varobj *var)
2464 return FORMAT_NATURAL;
2467 /* FIXME: The following should be generic for any pointer. */
2469 cppush (struct cpstack **pstack, char *name)
2473 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
2479 /* FIXME: The following should be generic for any pointer. */
2481 cppop (struct cpstack **pstack)
2486 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
2491 *pstack = (*pstack)->next;
2498 * Language-dependencies
2501 /* Common entry points */
2503 /* Get the language of variable VAR. */
2504 static enum varobj_languages
2505 variable_language (struct varobj *var)
2507 enum varobj_languages lang;
2509 switch (var->root->exp->language_defn->la_language)
2515 case language_cplus:
2529 /* Return the number of children for a given variable.
2530 The result of this function is defined by the language
2531 implementation. The number of children returned by this function
2532 is the number of children that the user will see in the variable
2535 number_of_children (struct varobj *var)
2537 return (*var->root->lang->number_of_children) (var);
2540 /* What is the expression for the root varobj VAR? Returns a malloc'd
2543 name_of_variable (struct varobj *var)
2545 return (*var->root->lang->name_of_variable) (var);
2548 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2551 name_of_child (struct varobj *var, int index)
2553 return (*var->root->lang->name_of_child) (var, index);
2556 /* What is the ``struct value *'' of the root variable VAR?
2557 For floating variable object, evaluation can get us a value
2558 of different type from what is stored in varobj already. In
2560 - *type_changed will be set to 1
2561 - old varobj will be freed, and new one will be
2562 created, with the same name.
2563 - *var_handle will be set to the new varobj
2564 Otherwise, *type_changed will be set to 0. */
2565 static struct value *
2566 value_of_root (struct varobj **var_handle, int *type_changed)
2570 if (var_handle == NULL)
2575 /* This should really be an exception, since this should
2576 only get called with a root variable. */
2578 if (!is_root_p (var))
2581 if (var->root->floating)
2583 struct varobj *tmp_var;
2584 char *old_type, *new_type;
2586 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
2587 USE_SELECTED_FRAME);
2588 if (tmp_var == NULL)
2592 old_type = varobj_get_type (var);
2593 new_type = varobj_get_type (tmp_var);
2594 if (strcmp (old_type, new_type) == 0)
2596 /* The expression presently stored inside var->root->exp
2597 remembers the locations of local variables relatively to
2598 the frame where the expression was created (in DWARF location
2599 button, for example). Naturally, those locations are not
2600 correct in other frames, so update the expression. */
2602 struct expression *tmp_exp = var->root->exp;
2604 var->root->exp = tmp_var->root->exp;
2605 tmp_var->root->exp = tmp_exp;
2607 varobj_delete (tmp_var, NULL, 0);
2612 tmp_var->obj_name = xstrdup (var->obj_name);
2613 tmp_var->from = var->from;
2614 tmp_var->to = var->to;
2615 varobj_delete (var, NULL, 0);
2617 install_variable (tmp_var);
2618 *var_handle = tmp_var;
2630 return (*var->root->lang->value_of_root) (var_handle);
2633 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2634 static struct value *
2635 value_of_child (struct varobj *parent, int index)
2637 struct value *value;
2639 value = (*parent->root->lang->value_of_child) (parent, index);
2644 /* GDB already has a command called "value_of_variable". Sigh. */
2646 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2648 if (var->root->is_valid)
2650 if (var->pretty_printer)
2651 return value_get_print_value (var->value, var->format, var);
2652 return (*var->root->lang->value_of_variable) (var, format);
2659 value_get_print_value (struct value *value, enum varobj_display_formats format,
2662 struct ui_file *stb;
2663 struct cleanup *old_chain;
2664 gdb_byte *thevalue = NULL;
2665 struct value_print_options opts;
2666 struct type *type = NULL;
2668 char *encoding = NULL;
2669 struct gdbarch *gdbarch = NULL;
2670 /* Initialize it just to avoid a GCC false warning. */
2671 CORE_ADDR str_addr = 0;
2672 int string_print = 0;
2677 stb = mem_fileopen ();
2678 old_chain = make_cleanup_ui_file_delete (stb);
2680 gdbarch = get_type_arch (value_type (value));
2683 PyObject *value_formatter = var->pretty_printer;
2685 varobj_ensure_python_env (var);
2687 if (value_formatter)
2689 /* First check to see if we have any children at all. If so,
2690 we simply return {...}. */
2691 if (dynamic_varobj_has_child_method (var))
2693 do_cleanups (old_chain);
2694 return xstrdup ("{...}");
2697 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2699 struct value *replacement;
2700 PyObject *output = NULL;
2702 output = apply_varobj_pretty_printer (value_formatter,
2706 /* If we have string like output ... */
2709 make_cleanup_py_decref (output);
2711 /* If this is a lazy string, extract it. For lazy
2712 strings we always print as a string, so set
2714 if (gdbpy_is_lazy_string (output))
2716 gdbpy_extract_lazy_string (output, &str_addr, &type,
2718 make_cleanup (free_current_contents, &encoding);
2723 /* If it is a regular (non-lazy) string, extract
2724 it and copy the contents into THEVALUE. If the
2725 hint says to print it as a string, set
2726 string_print. Otherwise just return the extracted
2727 string as a value. */
2730 = python_string_to_target_python_string (output);
2734 char *s = PyString_AsString (py_str);
2737 hint = gdbpy_get_display_hint (value_formatter);
2740 if (!strcmp (hint, "string"))
2745 len = PyString_Size (py_str);
2746 thevalue = xmemdup (s, len + 1, len + 1);
2747 type = builtin_type (gdbarch)->builtin_char;
2752 do_cleanups (old_chain);
2756 make_cleanup (xfree, thevalue);
2759 gdbpy_print_stack ();
2762 /* If the printer returned a replacement value, set VALUE
2763 to REPLACEMENT. If there is not a replacement value,
2764 just use the value passed to this function. */
2766 value = replacement;
2772 get_formatted_print_options (&opts, format_code[(int) format]);
2776 /* If the THEVALUE has contents, it is a regular string. */
2778 LA_PRINT_STRING (stb, type, thevalue, len, encoding, 0, &opts);
2779 else if (string_print)
2780 /* Otherwise, if string_print is set, and it is not a regular
2781 string, it is a lazy string. */
2782 val_print_string (type, encoding, str_addr, len, stb, &opts);
2784 /* All other cases. */
2785 common_val_print (value, stb, 0, &opts, current_language);
2787 thevalue = ui_file_xstrdup (stb, NULL);
2789 do_cleanups (old_chain);
2794 varobj_editable_p (struct varobj *var)
2798 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
2801 type = get_value_type (var);
2803 switch (TYPE_CODE (type))
2805 case TYPE_CODE_STRUCT:
2806 case TYPE_CODE_UNION:
2807 case TYPE_CODE_ARRAY:
2808 case TYPE_CODE_FUNC:
2809 case TYPE_CODE_METHOD:
2819 /* Return non-zero if changes in value of VAR
2820 must be detected and reported by -var-update.
2821 Return zero is -var-update should never report
2822 changes of such values. This makes sense for structures
2823 (since the changes in children values will be reported separately),
2824 or for artifical objects (like 'public' pseudo-field in C++).
2826 Return value of 0 means that gdb need not call value_fetch_lazy
2827 for the value of this variable object. */
2829 varobj_value_is_changeable_p (struct varobj *var)
2834 if (CPLUS_FAKE_CHILD (var))
2837 type = get_value_type (var);
2839 switch (TYPE_CODE (type))
2841 case TYPE_CODE_STRUCT:
2842 case TYPE_CODE_UNION:
2843 case TYPE_CODE_ARRAY:
2854 /* Return 1 if that varobj is floating, that is is always evaluated in the
2855 selected frame, and not bound to thread/frame. Such variable objects
2856 are created using '@' as frame specifier to -var-create. */
2858 varobj_floating_p (struct varobj *var)
2860 return var->root->floating;
2863 /* Given the value and the type of a variable object,
2864 adjust the value and type to those necessary
2865 for getting children of the variable object.
2866 This includes dereferencing top-level references
2867 to all types and dereferencing pointers to
2870 Both TYPE and *TYPE should be non-null. VALUE
2871 can be null if we want to only translate type.
2872 *VALUE can be null as well -- if the parent
2875 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
2876 depending on whether pointer was dereferenced
2877 in this function. */
2879 adjust_value_for_child_access (struct value **value,
2883 gdb_assert (type && *type);
2888 *type = check_typedef (*type);
2890 /* The type of value stored in varobj, that is passed
2891 to us, is already supposed to be
2892 reference-stripped. */
2894 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
2896 /* Pointers to structures are treated just like
2897 structures when accessing children. Don't
2898 dererences pointers to other types. */
2899 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
2901 struct type *target_type = get_target_type (*type);
2902 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
2903 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
2905 if (value && *value)
2907 volatile struct gdb_exception except;
2909 TRY_CATCH (except, RETURN_MASK_ERROR)
2911 *value = value_ind (*value);
2914 if (except.reason < 0)
2917 *type = target_type;
2923 /* The 'get_target_type' function calls check_typedef on
2924 result, so we can immediately check type code. No
2925 need to call check_typedef here. */
2930 c_number_of_children (struct varobj *var)
2932 struct type *type = get_value_type (var);
2934 struct type *target;
2936 adjust_value_for_child_access (NULL, &type, NULL);
2937 target = get_target_type (type);
2939 switch (TYPE_CODE (type))
2941 case TYPE_CODE_ARRAY:
2942 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
2943 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
2944 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
2946 /* If we don't know how many elements there are, don't display
2951 case TYPE_CODE_STRUCT:
2952 case TYPE_CODE_UNION:
2953 children = TYPE_NFIELDS (type);
2957 /* The type here is a pointer to non-struct. Typically, pointers
2958 have one child, except for function ptrs, which have no children,
2959 and except for void*, as we don't know what to show.
2961 We can show char* so we allow it to be dereferenced. If you decide
2962 to test for it, please mind that a little magic is necessary to
2963 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
2964 TYPE_NAME == "char". */
2965 if (TYPE_CODE (target) == TYPE_CODE_FUNC
2966 || TYPE_CODE (target) == TYPE_CODE_VOID)
2973 /* Other types have no children. */
2981 c_name_of_variable (struct varobj *parent)
2983 return xstrdup (parent->name);
2986 /* Return the value of element TYPE_INDEX of a structure
2987 value VALUE. VALUE's type should be a structure,
2988 or union, or a typedef to struct/union.
2990 Returns NULL if getting the value fails. Never throws. */
2991 static struct value *
2992 value_struct_element_index (struct value *value, int type_index)
2994 struct value *result = NULL;
2995 volatile struct gdb_exception e;
2996 struct type *type = value_type (value);
2998 type = check_typedef (type);
3000 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
3001 || TYPE_CODE (type) == TYPE_CODE_UNION);
3003 TRY_CATCH (e, RETURN_MASK_ERROR)
3005 if (field_is_static (&TYPE_FIELD (type, type_index)))
3006 result = value_static_field (type, type_index);
3008 result = value_primitive_field (value, 0, type_index, type);
3020 /* Obtain the information about child INDEX of the variable
3022 If CNAME is not null, sets *CNAME to the name of the child relative
3024 If CVALUE is not null, sets *CVALUE to the value of the child.
3025 If CTYPE is not null, sets *CTYPE to the type of the child.
3027 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
3028 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
3031 c_describe_child (struct varobj *parent, int index,
3032 char **cname, struct value **cvalue, struct type **ctype,
3033 char **cfull_expression)
3035 struct value *value = parent->value;
3036 struct type *type = get_value_type (parent);
3037 char *parent_expression = NULL;
3039 volatile struct gdb_exception except;
3047 if (cfull_expression)
3049 *cfull_expression = NULL;
3050 parent_expression = varobj_get_path_expr (get_path_expr_parent (parent));
3052 adjust_value_for_child_access (&value, &type, &was_ptr);
3054 switch (TYPE_CODE (type))
3056 case TYPE_CODE_ARRAY:
3059 = xstrdup (int_string (index
3060 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3063 if (cvalue && value)
3065 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
3067 TRY_CATCH (except, RETURN_MASK_ERROR)
3069 *cvalue = value_subscript (value, real_index);
3074 *ctype = get_target_type (type);
3076 if (cfull_expression)
3078 xstrprintf ("(%s)[%s]", parent_expression,
3080 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3086 case TYPE_CODE_STRUCT:
3087 case TYPE_CODE_UNION:
3091 /* If the type is anonymous and the field has no name,
3092 set an appropriate name. */
3093 field_name = TYPE_FIELD_NAME (type, index);
3094 if (field_name == NULL || *field_name == '\0')
3098 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
3099 == TYPE_CODE_STRUCT)
3100 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3102 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3105 if (cfull_expression)
3106 *cfull_expression = xstrdup ("");
3111 *cname = xstrdup (field_name);
3113 if (cfull_expression)
3115 char *join = was_ptr ? "->" : ".";
3117 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
3122 if (cvalue && value)
3124 /* For C, varobj index is the same as type index. */
3125 *cvalue = value_struct_element_index (value, index);
3129 *ctype = TYPE_FIELD_TYPE (type, index);
3135 *cname = xstrprintf ("*%s", parent->name);
3137 if (cvalue && value)
3139 TRY_CATCH (except, RETURN_MASK_ERROR)
3141 *cvalue = value_ind (value);
3144 if (except.reason < 0)
3148 /* Don't use get_target_type because it calls
3149 check_typedef and here, we want to show the true
3150 declared type of the variable. */
3152 *ctype = TYPE_TARGET_TYPE (type);
3154 if (cfull_expression)
3155 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
3160 /* This should not happen. */
3162 *cname = xstrdup ("???");
3163 if (cfull_expression)
3164 *cfull_expression = xstrdup ("???");
3165 /* Don't set value and type, we don't know then. */
3170 c_name_of_child (struct varobj *parent, int index)
3174 c_describe_child (parent, index, &name, NULL, NULL, NULL);
3179 c_path_expr_of_child (struct varobj *child)
3181 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
3183 return child->path_expr;
3186 /* If frame associated with VAR can be found, switch
3187 to it and return 1. Otherwise, return 0. */
3189 check_scope (struct varobj *var)
3191 struct frame_info *fi;
3194 fi = frame_find_by_id (var->root->frame);
3199 CORE_ADDR pc = get_frame_pc (fi);
3201 if (pc < BLOCK_START (var->root->valid_block) ||
3202 pc >= BLOCK_END (var->root->valid_block))
3210 static struct value *
3211 c_value_of_root (struct varobj **var_handle)
3213 struct value *new_val = NULL;
3214 struct varobj *var = *var_handle;
3215 int within_scope = 0;
3216 struct cleanup *back_to;
3218 /* Only root variables can be updated... */
3219 if (!is_root_p (var))
3220 /* Not a root var. */
3223 back_to = make_cleanup_restore_current_thread ();
3225 /* Determine whether the variable is still around. */
3226 if (var->root->valid_block == NULL || var->root->floating)
3228 else if (var->root->thread_id == 0)
3230 /* The program was single-threaded when the variable object was
3231 created. Technically, it's possible that the program became
3232 multi-threaded since then, but we don't support such
3234 within_scope = check_scope (var);
3238 ptid_t ptid = thread_id_to_pid (var->root->thread_id);
3239 if (in_thread_list (ptid))
3241 switch_to_thread (ptid);
3242 within_scope = check_scope (var);
3248 volatile struct gdb_exception except;
3250 /* We need to catch errors here, because if evaluate
3251 expression fails we want to just return NULL. */
3252 TRY_CATCH (except, RETURN_MASK_ERROR)
3254 new_val = evaluate_expression (var->root->exp);
3260 do_cleanups (back_to);
3265 static struct value *
3266 c_value_of_child (struct varobj *parent, int index)
3268 struct value *value = NULL;
3270 c_describe_child (parent, index, NULL, &value, NULL, NULL);
3274 static struct type *
3275 c_type_of_child (struct varobj *parent, int index)
3277 struct type *type = NULL;
3279 c_describe_child (parent, index, NULL, NULL, &type, NULL);
3284 c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3286 /* BOGUS: if val_print sees a struct/class, or a reference to one,
3287 it will print out its children instead of "{...}". So we need to
3288 catch that case explicitly. */
3289 struct type *type = get_type (var);
3291 /* If we have a custom formatter, return whatever string it has
3293 if (var->pretty_printer && var->print_value)
3294 return xstrdup (var->print_value);
3296 /* Strip top-level references. */
3297 while (TYPE_CODE (type) == TYPE_CODE_REF)
3298 type = check_typedef (TYPE_TARGET_TYPE (type));
3300 switch (TYPE_CODE (type))
3302 case TYPE_CODE_STRUCT:
3303 case TYPE_CODE_UNION:
3304 return xstrdup ("{...}");
3307 case TYPE_CODE_ARRAY:
3311 number = xstrprintf ("[%d]", var->num_children);
3318 if (var->value == NULL)
3320 /* This can happen if we attempt to get the value of a struct
3321 member when the parent is an invalid pointer. This is an
3322 error condition, so we should tell the caller. */
3327 if (var->not_fetched && value_lazy (var->value))
3328 /* Frozen variable and no value yet. We don't
3329 implicitly fetch the value. MI response will
3330 use empty string for the value, which is OK. */
3333 gdb_assert (varobj_value_is_changeable_p (var));
3334 gdb_assert (!value_lazy (var->value));
3336 /* If the specified format is the current one,
3337 we can reuse print_value. */
3338 if (format == var->format)
3339 return xstrdup (var->print_value);
3341 return value_get_print_value (var->value, format, var);
3351 cplus_number_of_children (struct varobj *var)
3354 int children, dont_know;
3359 if (!CPLUS_FAKE_CHILD (var))
3361 type = get_value_type (var);
3362 adjust_value_for_child_access (NULL, &type, NULL);
3364 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
3365 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
3369 cplus_class_num_children (type, kids);
3370 if (kids[v_public] != 0)
3372 if (kids[v_private] != 0)
3374 if (kids[v_protected] != 0)
3377 /* Add any baseclasses. */
3378 children += TYPE_N_BASECLASSES (type);
3381 /* FIXME: save children in var. */
3388 type = get_value_type (var->parent);
3389 adjust_value_for_child_access (NULL, &type, NULL);
3391 cplus_class_num_children (type, kids);
3392 if (strcmp (var->name, "public") == 0)
3393 children = kids[v_public];
3394 else if (strcmp (var->name, "private") == 0)
3395 children = kids[v_private];
3397 children = kids[v_protected];
3402 children = c_number_of_children (var);
3407 /* Compute # of public, private, and protected variables in this class.
3408 That means we need to descend into all baseclasses and find out
3409 how many are there, too. */
3411 cplus_class_num_children (struct type *type, int children[3])
3413 int i, vptr_fieldno;
3414 struct type *basetype = NULL;
3416 children[v_public] = 0;
3417 children[v_private] = 0;
3418 children[v_protected] = 0;
3420 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3421 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
3423 /* If we have a virtual table pointer, omit it. Even if virtual
3424 table pointers are not specifically marked in the debug info,
3425 they should be artificial. */
3426 if ((type == basetype && i == vptr_fieldno)
3427 || TYPE_FIELD_ARTIFICIAL (type, i))
3430 if (TYPE_FIELD_PROTECTED (type, i))
3431 children[v_protected]++;
3432 else if (TYPE_FIELD_PRIVATE (type, i))
3433 children[v_private]++;
3435 children[v_public]++;
3440 cplus_name_of_variable (struct varobj *parent)
3442 return c_name_of_variable (parent);
3445 enum accessibility { private_field, protected_field, public_field };
3447 /* Check if field INDEX of TYPE has the specified accessibility.
3448 Return 0 if so and 1 otherwise. */
3450 match_accessibility (struct type *type, int index, enum accessibility acc)
3452 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
3454 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
3456 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
3457 && !TYPE_FIELD_PROTECTED (type, index))
3464 cplus_describe_child (struct varobj *parent, int index,
3465 char **cname, struct value **cvalue, struct type **ctype,
3466 char **cfull_expression)
3468 struct value *value;
3471 char *parent_expression = NULL;
3479 if (cfull_expression)
3480 *cfull_expression = NULL;
3482 if (CPLUS_FAKE_CHILD (parent))
3484 value = parent->parent->value;
3485 type = get_value_type (parent->parent);
3486 if (cfull_expression)
3488 = varobj_get_path_expr (get_path_expr_parent (parent->parent));
3492 value = parent->value;
3493 type = get_value_type (parent);
3494 if (cfull_expression)
3496 = varobj_get_path_expr (get_path_expr_parent (parent));
3499 adjust_value_for_child_access (&value, &type, &was_ptr);
3501 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3502 || TYPE_CODE (type) == TYPE_CODE_UNION)
3504 char *join = was_ptr ? "->" : ".";
3506 if (CPLUS_FAKE_CHILD (parent))
3508 /* The fields of the class type are ordered as they
3509 appear in the class. We are given an index for a
3510 particular access control type ("public","protected",
3511 or "private"). We must skip over fields that don't
3512 have the access control we are looking for to properly
3513 find the indexed field. */
3514 int type_index = TYPE_N_BASECLASSES (type);
3515 enum accessibility acc = public_field;
3517 struct type *basetype = NULL;
3520 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3521 if (strcmp (parent->name, "private") == 0)
3522 acc = private_field;
3523 else if (strcmp (parent->name, "protected") == 0)
3524 acc = protected_field;
3528 if ((type == basetype && type_index == vptr_fieldno)
3529 || TYPE_FIELD_ARTIFICIAL (type, type_index))
3531 else if (match_accessibility (type, type_index, acc))
3537 /* If the type is anonymous and the field has no name,
3538 set an appopriate name. */
3539 field_name = TYPE_FIELD_NAME (type, type_index);
3540 if (field_name == NULL || *field_name == '\0')
3544 if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3545 == TYPE_CODE_STRUCT)
3546 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3547 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3549 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3552 if (cfull_expression)
3553 *cfull_expression = xstrdup ("");
3558 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
3560 if (cfull_expression)
3562 = xstrprintf ("((%s)%s%s)", parent_expression, join,
3566 if (cvalue && value)
3567 *cvalue = value_struct_element_index (value, type_index);
3570 *ctype = TYPE_FIELD_TYPE (type, type_index);
3572 else if (index < TYPE_N_BASECLASSES (type))
3574 /* This is a baseclass. */
3576 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
3578 if (cvalue && value)
3579 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
3583 *ctype = TYPE_FIELD_TYPE (type, index);
3586 if (cfull_expression)
3588 char *ptr = was_ptr ? "*" : "";
3590 /* Cast the parent to the base' type. Note that in gdb,
3593 will create an lvalue, for all appearences, so we don't
3594 need to use more fancy:
3598 When we are in the scope of the base class or of one
3599 of its children, the type field name will be interpreted
3600 as a constructor, if it exists. Therefore, we must
3601 indicate that the name is a class name by using the
3602 'class' keyword. See PR mi/11912 */
3603 *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
3605 TYPE_FIELD_NAME (type, index),
3612 char *access = NULL;
3615 cplus_class_num_children (type, children);
3617 /* Everything beyond the baseclasses can
3618 only be "public", "private", or "protected"
3620 The special "fake" children are always output by varobj in
3621 this order. So if INDEX == 2, it MUST be "protected". */
3622 index -= TYPE_N_BASECLASSES (type);
3626 if (children[v_public] > 0)
3628 else if (children[v_private] > 0)
3631 access = "protected";
3634 if (children[v_public] > 0)
3636 if (children[v_private] > 0)
3639 access = "protected";
3641 else if (children[v_private] > 0)
3642 access = "protected";
3645 /* Must be protected. */
3646 access = "protected";
3653 gdb_assert (access);
3655 *cname = xstrdup (access);
3657 /* Value and type and full expression are null here. */
3662 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
3667 cplus_name_of_child (struct varobj *parent, int index)
3671 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
3676 cplus_path_expr_of_child (struct varobj *child)
3678 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
3680 return child->path_expr;
3683 static struct value *
3684 cplus_value_of_root (struct varobj **var_handle)
3686 return c_value_of_root (var_handle);
3689 static struct value *
3690 cplus_value_of_child (struct varobj *parent, int index)
3692 struct value *value = NULL;
3694 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
3698 static struct type *
3699 cplus_type_of_child (struct varobj *parent, int index)
3701 struct type *type = NULL;
3703 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
3708 cplus_value_of_variable (struct varobj *var,
3709 enum varobj_display_formats format)
3712 /* If we have one of our special types, don't print out
3714 if (CPLUS_FAKE_CHILD (var))
3715 return xstrdup ("");
3717 return c_value_of_variable (var, format);
3723 java_number_of_children (struct varobj *var)
3725 return cplus_number_of_children (var);
3729 java_name_of_variable (struct varobj *parent)
3733 name = cplus_name_of_variable (parent);
3734 /* If the name has "-" in it, it is because we
3735 needed to escape periods in the name... */
3738 while (*p != '\000')
3749 java_name_of_child (struct varobj *parent, int index)
3753 name = cplus_name_of_child (parent, index);
3754 /* Escape any periods in the name... */
3757 while (*p != '\000')
3768 java_path_expr_of_child (struct varobj *child)
3773 static struct value *
3774 java_value_of_root (struct varobj **var_handle)
3776 return cplus_value_of_root (var_handle);
3779 static struct value *
3780 java_value_of_child (struct varobj *parent, int index)
3782 return cplus_value_of_child (parent, index);
3785 static struct type *
3786 java_type_of_child (struct varobj *parent, int index)
3788 return cplus_type_of_child (parent, index);
3792 java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3794 return cplus_value_of_variable (var, format);
3797 /* Ada specific callbacks for VAROBJs. */
3800 ada_number_of_children (struct varobj *var)
3802 return c_number_of_children (var);
3806 ada_name_of_variable (struct varobj *parent)
3808 return c_name_of_variable (parent);
3812 ada_name_of_child (struct varobj *parent, int index)
3814 return c_name_of_child (parent, index);
3818 ada_path_expr_of_child (struct varobj *child)
3820 return c_path_expr_of_child (child);
3823 static struct value *
3824 ada_value_of_root (struct varobj **var_handle)
3826 return c_value_of_root (var_handle);
3829 static struct value *
3830 ada_value_of_child (struct varobj *parent, int index)
3832 return c_value_of_child (parent, index);
3835 static struct type *
3836 ada_type_of_child (struct varobj *parent, int index)
3838 return c_type_of_child (parent, index);
3842 ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3844 return c_value_of_variable (var, format);
3847 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
3848 with an arbitrary caller supplied DATA pointer. */
3851 all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
3853 struct varobj_root *var_root, *var_root_next;
3855 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
3857 for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
3859 var_root_next = var_root->next;
3861 (*func) (var_root->rootvar, data);
3865 extern void _initialize_varobj (void);
3867 _initialize_varobj (void)
3869 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
3871 varobj_table = xmalloc (sizeof_table);
3872 memset (varobj_table, 0, sizeof_table);
3874 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
3876 _("Set varobj debugging."),
3877 _("Show varobj debugging."),
3878 _("When non-zero, varobj debugging is enabled."),
3879 NULL, show_varobjdebug,
3880 &setlist, &showlist);
3883 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
3884 defined on globals. It is a helper for varobj_invalidate. */
3887 varobj_invalidate_iter (struct varobj *var, void *unused)
3889 /* Floating varobjs are reparsed on each stop, so we don't care if the
3890 presently parsed expression refers to something that's gone. */
3891 if (var->root->floating)
3894 /* global var must be re-evaluated. */
3895 if (var->root->valid_block == NULL)
3897 struct varobj *tmp_var;
3899 /* Try to create a varobj with same expression. If we succeed
3900 replace the old varobj, otherwise invalidate it. */
3901 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
3903 if (tmp_var != NULL)
3905 tmp_var->obj_name = xstrdup (var->obj_name);
3906 varobj_delete (var, NULL, 0);
3907 install_variable (tmp_var);
3910 var->root->is_valid = 0;
3912 else /* locals must be invalidated. */
3913 var->root->is_valid = 0;
3916 /* Invalidate the varobjs that are tied to locals and re-create the ones that
3917 are defined on globals.
3918 Invalidated varobjs will be always printed in_scope="invalid". */
3921 varobj_invalidate (void)
3923 all_root_varobjs (varobj_invalidate_iter, NULL);