1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "exceptions.h"
21 #include "expression.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include "gdb_regex.h"
34 #include "gdbthread.h"
36 #include "ada-varobj.h"
40 #include "python/python.h"
41 #include "python/python-internal.h"
46 /* The names of varobjs representing anonymous structs or unions. */
47 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
48 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
50 /* Non-zero if we want to see trace of varobj level stuff. */
52 unsigned int varobjdebug = 0;
54 show_varobjdebug (struct ui_file *file, int from_tty,
55 struct cmd_list_element *c, const char *value)
57 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
60 /* String representations of gdb's format codes. */
61 char *varobj_format_string[] =
62 { "natural", "binary", "decimal", "hexadecimal", "octal" };
64 /* String representations of gdb's known languages. */
65 char *varobj_language_string[] = { "C", "C++", "Java" };
67 /* True if we want to allow Python-based pretty-printing. */
68 static int pretty_printing = 0;
71 varobj_enable_pretty_printing (void)
78 /* Every root variable has one of these structures saved in its
79 varobj. Members which must be free'd are noted. */
83 /* Alloc'd expression for this parent. */
84 struct expression *exp;
86 /* Block for which this expression is valid. */
87 const struct block *valid_block;
89 /* The frame for this expression. This field is set iff valid_block is
91 struct frame_id frame;
93 /* The thread ID that this varobj_root belong to. This field
94 is only valid if valid_block is not NULL.
95 When not 0, indicates which thread 'frame' belongs to.
96 When 0, indicates that the thread list was empty when the varobj_root
100 /* If 1, the -var-update always recomputes the value in the
101 current thread and frame. Otherwise, variable object is
102 always updated in the specific scope/thread/frame. */
105 /* Flag that indicates validity: set to 0 when this varobj_root refers
106 to symbols that do not exist anymore. */
109 /* Language info for this variable and its children. */
110 struct language_specific *lang;
112 /* The varobj for this root node. */
113 struct varobj *rootvar;
115 /* Next root variable */
116 struct varobj_root *next;
119 /* Dynamic part of varobj. */
121 struct varobj_dynamic
123 /* Whether the children of this varobj were requested. This field is
124 used to decide if dynamic varobj should recompute their children.
125 In the event that the frontend never asked for the children, we
127 int children_requested;
129 /* The pretty-printer constructor. If NULL, then the default
130 pretty-printer will be looked up. If None, then no
131 pretty-printer will be installed. */
132 PyObject *constructor;
134 /* The pretty-printer that has been constructed. If NULL, then a
135 new printer object is needed, and one will be constructed. */
136 PyObject *pretty_printer;
138 /* The iterator returned by the printer's 'children' method, or NULL
140 PyObject *child_iter;
142 /* We request one extra item from the iterator, so that we can
143 report to the caller whether there are more items than we have
144 already reported. However, we don't want to install this value
145 when we read it, because that will mess up future updates. So,
146 we stash it here instead. */
147 PyObject *saved_item;
153 struct cpstack *next;
156 /* A list of varobjs */
164 /* Private function prototypes */
166 /* Helper functions for the above subcommands. */
168 static int delete_variable (struct cpstack **, struct varobj *, int);
170 static void delete_variable_1 (struct cpstack **, int *,
171 struct varobj *, int, int);
173 static int install_variable (struct varobj *);
175 static void uninstall_variable (struct varobj *);
177 static struct varobj *create_child (struct varobj *, int, char *);
179 static struct varobj *
180 create_child_with_value (struct varobj *parent, int index, char *name,
181 struct value *value);
183 /* Utility routines */
185 static struct varobj *new_variable (void);
187 static struct varobj *new_root_variable (void);
189 static void free_variable (struct varobj *var);
191 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
193 static struct type *get_type (struct varobj *var);
195 static struct type *get_value_type (struct varobj *var);
197 static struct type *get_target_type (struct type *);
199 static enum varobj_display_formats variable_default_display (struct varobj *);
201 static void cppush (struct cpstack **pstack, char *name);
203 static char *cppop (struct cpstack **pstack);
205 static int update_type_if_necessary (struct varobj *var,
206 struct value *new_value);
208 static int install_new_value (struct varobj *var, struct value *value,
211 /* Language-specific routines. */
213 static enum varobj_languages variable_language (struct varobj *var);
215 static int number_of_children (struct varobj *);
217 static char *name_of_variable (struct varobj *);
219 static char *name_of_child (struct varobj *, int);
221 static struct value *value_of_root (struct varobj **var_handle, int *);
223 static struct value *value_of_child (struct varobj *parent, int index);
225 static char *my_value_of_variable (struct varobj *var,
226 enum varobj_display_formats format);
228 static char *value_get_print_value (struct value *value,
229 enum varobj_display_formats format,
232 static int varobj_value_is_changeable_p (struct varobj *var);
234 static int is_root_p (struct varobj *var);
238 static struct varobj *varobj_add_child (struct varobj *var,
240 struct value *value);
242 #endif /* HAVE_PYTHON */
244 static int default_value_is_changeable_p (struct varobj *var);
246 /* C implementation */
248 static int c_number_of_children (struct varobj *var);
250 static char *c_name_of_variable (struct varobj *parent);
252 static char *c_name_of_child (struct varobj *parent, int index);
254 static char *c_path_expr_of_child (struct varobj *child);
256 static struct value *c_value_of_child (struct varobj *parent, int index);
258 static struct type *c_type_of_child (struct varobj *parent, int index);
260 static char *c_value_of_variable (struct varobj *var,
261 enum varobj_display_formats format);
263 /* C++ implementation */
265 static int cplus_number_of_children (struct varobj *var);
267 static void cplus_class_num_children (struct type *type, int children[3]);
269 static char *cplus_name_of_variable (struct varobj *parent);
271 static char *cplus_name_of_child (struct varobj *parent, int index);
273 static char *cplus_path_expr_of_child (struct varobj *child);
275 static struct value *cplus_value_of_child (struct varobj *parent, int index);
277 static struct type *cplus_type_of_child (struct varobj *parent, int index);
279 static char *cplus_value_of_variable (struct varobj *var,
280 enum varobj_display_formats format);
282 /* Java implementation */
284 static int java_number_of_children (struct varobj *var);
286 static char *java_name_of_variable (struct varobj *parent);
288 static char *java_name_of_child (struct varobj *parent, int index);
290 static char *java_path_expr_of_child (struct varobj *child);
292 static struct value *java_value_of_child (struct varobj *parent, int index);
294 static struct type *java_type_of_child (struct varobj *parent, int index);
296 static char *java_value_of_variable (struct varobj *var,
297 enum varobj_display_formats format);
299 /* Ada implementation */
301 static int ada_number_of_children (struct varobj *var);
303 static char *ada_name_of_variable (struct varobj *parent);
305 static char *ada_name_of_child (struct varobj *parent, int index);
307 static char *ada_path_expr_of_child (struct varobj *child);
309 static struct value *ada_value_of_child (struct varobj *parent, int index);
311 static struct type *ada_type_of_child (struct varobj *parent, int index);
313 static char *ada_value_of_variable (struct varobj *var,
314 enum varobj_display_formats format);
316 static int ada_value_is_changeable_p (struct varobj *var);
318 static int ada_value_has_mutated (struct varobj *var, struct value *new_val,
319 struct type *new_type);
321 /* The language specific vector */
323 struct language_specific
325 /* The number of children of PARENT. */
326 int (*number_of_children) (struct varobj * parent);
328 /* The name (expression) of a root varobj. */
329 char *(*name_of_variable) (struct varobj * parent);
331 /* The name of the INDEX'th child of PARENT. */
332 char *(*name_of_child) (struct varobj * parent, int index);
334 /* Returns the rooted expression of CHILD, which is a variable
335 obtain that has some parent. */
336 char *(*path_expr_of_child) (struct varobj * child);
338 /* The ``struct value *'' of the INDEX'th child of PARENT. */
339 struct value *(*value_of_child) (struct varobj * parent, int index);
341 /* The type of the INDEX'th child of PARENT. */
342 struct type *(*type_of_child) (struct varobj * parent, int index);
344 /* The current value of VAR. */
345 char *(*value_of_variable) (struct varobj * var,
346 enum varobj_display_formats format);
348 /* Return non-zero if changes in value of VAR must be detected and
349 reported by -var-update. Return zero if -var-update should never
350 report changes of such values. This makes sense for structures
351 (since the changes in children values will be reported separately),
352 or for artifical objects (like 'public' pseudo-field in C++).
354 Return value of 0 means that gdb need not call value_fetch_lazy
355 for the value of this variable object. */
356 int (*value_is_changeable_p) (struct varobj *var);
358 /* Return nonzero if the type of VAR has mutated.
360 VAR's value is still the varobj's previous value, while NEW_VALUE
361 is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE
362 may be NULL indicating that there is no value available (the varobj
363 may be out of scope, of may be the child of a null pointer, for
364 instance). NEW_TYPE, on the other hand, must never be NULL.
366 This function should also be able to assume that var's number of
367 children is set (not < 0).
369 Languages where types do not mutate can set this to NULL. */
370 int (*value_has_mutated) (struct varobj *var, struct value *new_value,
371 struct type *new_type);
374 /* Array of known source language routines. */
375 static struct language_specific languages[vlang_end] = {
378 c_number_of_children,
381 c_path_expr_of_child,
385 default_value_is_changeable_p,
386 NULL /* value_has_mutated */}
390 cplus_number_of_children,
391 cplus_name_of_variable,
393 cplus_path_expr_of_child,
394 cplus_value_of_child,
396 cplus_value_of_variable,
397 default_value_is_changeable_p,
398 NULL /* value_has_mutated */}
402 java_number_of_children,
403 java_name_of_variable,
405 java_path_expr_of_child,
408 java_value_of_variable,
409 default_value_is_changeable_p,
410 NULL /* value_has_mutated */},
413 ada_number_of_children,
414 ada_name_of_variable,
416 ada_path_expr_of_child,
419 ada_value_of_variable,
420 ada_value_is_changeable_p,
421 ada_value_has_mutated}
424 /* A little convenience enum for dealing with C++/Java. */
427 v_public = 0, v_private, v_protected
432 /* Mappings of varobj_display_formats enums to gdb's format codes. */
433 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
435 /* Header of the list of root variable objects. */
436 static struct varobj_root *rootlist;
438 /* Prime number indicating the number of buckets in the hash table. */
439 /* A prime large enough to avoid too many colisions. */
440 #define VAROBJ_TABLE_SIZE 227
442 /* Pointer to the varobj hash table (built at run time). */
443 static struct vlist **varobj_table;
445 /* Is the variable X one of our "fake" children? */
446 #define CPLUS_FAKE_CHILD(x) \
447 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
450 /* API Implementation */
452 is_root_p (struct varobj *var)
454 return (var->root->rootvar == var);
458 /* Helper function to install a Python environment suitable for
459 use during operations on VAR. */
460 static struct cleanup *
461 varobj_ensure_python_env (struct varobj *var)
463 return ensure_python_env (var->root->exp->gdbarch,
464 var->root->exp->language_defn);
468 /* Creates a varobj (not its children). */
470 /* Return the full FRAME which corresponds to the given CORE_ADDR
471 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
473 static struct frame_info *
474 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
476 struct frame_info *frame = NULL;
478 if (frame_addr == (CORE_ADDR) 0)
481 for (frame = get_current_frame ();
483 frame = get_prev_frame (frame))
485 /* The CORE_ADDR we get as argument was parsed from a string GDB
486 output as $fp. This output got truncated to gdbarch_addr_bit.
487 Truncate the frame base address in the same manner before
488 comparing it against our argument. */
489 CORE_ADDR frame_base = get_frame_base_address (frame);
490 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
492 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
493 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
495 if (frame_base == frame_addr)
503 varobj_create (char *objname,
504 char *expression, CORE_ADDR frame, enum varobj_type type)
507 struct cleanup *old_chain;
509 /* Fill out a varobj structure for the (root) variable being constructed. */
510 var = new_root_variable ();
511 old_chain = make_cleanup_free_variable (var);
513 if (expression != NULL)
515 struct frame_info *fi;
516 struct frame_id old_id = null_frame_id;
519 enum varobj_languages lang;
520 struct value *value = NULL;
521 volatile struct gdb_exception except;
524 /* Parse and evaluate the expression, filling in as much of the
525 variable's data as possible. */
527 if (has_stack_frames ())
529 /* Allow creator to specify context of variable. */
530 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
531 fi = get_selected_frame (NULL);
533 /* FIXME: cagney/2002-11-23: This code should be doing a
534 lookup using the frame ID and not just the frame's
535 ``address''. This, of course, means an interface
536 change. However, with out that interface change ISAs,
537 such as the ia64 with its two stacks, won't work.
538 Similar goes for the case where there is a frameless
540 fi = find_frame_addr_in_frame_chain (frame);
545 /* frame = -2 means always use selected frame. */
546 if (type == USE_SELECTED_FRAME)
547 var->root->floating = 1;
553 block = get_frame_block (fi, 0);
554 pc = get_frame_pc (fi);
558 innermost_block = NULL;
559 /* Wrap the call to parse expression, so we can
560 return a sensible error. */
561 TRY_CATCH (except, RETURN_MASK_ERROR)
563 var->root->exp = parse_exp_1 (&p, pc, block, 0);
566 if (except.reason < 0)
568 do_cleanups (old_chain);
572 /* Don't allow variables to be created for types. */
573 if (var->root->exp->elts[0].opcode == OP_TYPE
574 || var->root->exp->elts[0].opcode == OP_TYPEOF
575 || var->root->exp->elts[0].opcode == OP_DECLTYPE)
577 do_cleanups (old_chain);
578 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
579 " as an expression.\n");
583 var->format = variable_default_display (var);
584 var->root->valid_block = innermost_block;
585 var->name = xstrdup (expression);
586 /* For a root var, the name and the expr are the same. */
587 var->path_expr = xstrdup (expression);
589 /* When the frame is different from the current frame,
590 we must select the appropriate frame before parsing
591 the expression, otherwise the value will not be current.
592 Since select_frame is so benign, just call it for all cases. */
595 /* User could specify explicit FRAME-ADDR which was not found but
596 EXPRESSION is frame specific and we would not be able to evaluate
597 it correctly next time. With VALID_BLOCK set we must also set
598 FRAME and THREAD_ID. */
600 error (_("Failed to find the specified frame"));
602 var->root->frame = get_frame_id (fi);
603 var->root->thread_id = pid_to_thread_id (inferior_ptid);
604 old_id = get_frame_id (get_selected_frame (NULL));
608 /* We definitely need to catch errors here.
609 If evaluate_expression succeeds we got the value we wanted.
610 But if it fails, we still go on with a call to evaluate_type(). */
611 TRY_CATCH (except, RETURN_MASK_ERROR)
613 value = evaluate_expression (var->root->exp);
616 if (except.reason < 0)
618 /* Error getting the value. Try to at least get the
620 struct value *type_only_value = evaluate_type (var->root->exp);
622 var->type = value_type (type_only_value);
626 int real_type_found = 0;
628 var->type = value_actual_type (value, 0, &real_type_found);
630 value = value_cast (var->type, value);
633 /* Set language info */
634 lang = variable_language (var);
635 var->root->lang = &languages[lang];
637 install_new_value (var, value, 1 /* Initial assignment */);
639 /* Set ourselves as our root. */
640 var->root->rootvar = var;
642 /* Reset the selected frame. */
643 if (frame_id_p (old_id))
644 select_frame (frame_find_by_id (old_id));
647 /* If the variable object name is null, that means this
648 is a temporary variable, so don't install it. */
650 if ((var != NULL) && (objname != NULL))
652 var->obj_name = xstrdup (objname);
654 /* If a varobj name is duplicated, the install will fail so
656 if (!install_variable (var))
658 do_cleanups (old_chain);
663 discard_cleanups (old_chain);
667 /* Generates an unique name that can be used for a varobj. */
670 varobj_gen_name (void)
675 /* Generate a name for this object. */
677 obj_name = xstrprintf ("var%d", id);
682 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
683 error if OBJNAME cannot be found. */
686 varobj_get_handle (char *objname)
690 unsigned int index = 0;
693 for (chp = objname; *chp; chp++)
695 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
698 cv = *(varobj_table + index);
699 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
703 error (_("Variable object not found"));
708 /* Given the handle, return the name of the object. */
711 varobj_get_objname (struct varobj *var)
713 return var->obj_name;
716 /* Given the handle, return the expression represented by the object. */
719 varobj_get_expression (struct varobj *var)
721 return name_of_variable (var);
724 /* Deletes a varobj and all its children if only_children == 0,
725 otherwise deletes only the children; returns a malloc'ed list of
726 all the (malloc'ed) names of the variables that have been deleted
727 (NULL terminated). */
730 varobj_delete (struct varobj *var, char ***dellist, int only_children)
734 struct cpstack *result = NULL;
737 /* Initialize a stack for temporary results. */
738 cppush (&result, NULL);
741 /* Delete only the variable children. */
742 delcount = delete_variable (&result, var, 1 /* only the children */ );
744 /* Delete the variable and all its children. */
745 delcount = delete_variable (&result, var, 0 /* parent+children */ );
747 /* We may have been asked to return a list of what has been deleted. */
750 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
754 *cp = cppop (&result);
755 while ((*cp != NULL) && (mycount > 0))
759 *cp = cppop (&result);
762 if (mycount || (*cp != NULL))
763 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
772 /* Convenience function for varobj_set_visualizer. Instantiate a
773 pretty-printer for a given value. */
775 instantiate_pretty_printer (PyObject *constructor, struct value *value)
777 PyObject *val_obj = NULL;
780 val_obj = value_to_value_object (value);
784 printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
791 /* Set/Get variable object display format. */
793 enum varobj_display_formats
794 varobj_set_display_format (struct varobj *var,
795 enum varobj_display_formats format)
802 case FORMAT_HEXADECIMAL:
804 var->format = format;
808 var->format = variable_default_display (var);
811 if (varobj_value_is_changeable_p (var)
812 && var->value && !value_lazy (var->value))
814 xfree (var->print_value);
815 var->print_value = value_get_print_value (var->value, var->format, var);
821 enum varobj_display_formats
822 varobj_get_display_format (struct varobj *var)
828 varobj_get_display_hint (struct varobj *var)
833 struct cleanup *back_to;
835 if (!gdb_python_initialized)
838 back_to = varobj_ensure_python_env (var);
840 if (var->dynamic->pretty_printer != NULL)
841 result = gdbpy_get_display_hint (var->dynamic->pretty_printer);
843 do_cleanups (back_to);
849 /* Return true if the varobj has items after TO, false otherwise. */
852 varobj_has_more (struct varobj *var, int to)
854 if (VEC_length (varobj_p, var->children) > to)
856 return ((to == -1 || VEC_length (varobj_p, var->children) == to)
857 && (var->dynamic->saved_item != NULL));
860 /* If the variable object is bound to a specific thread, that
861 is its evaluation can always be done in context of a frame
862 inside that thread, returns GDB id of the thread -- which
863 is always positive. Otherwise, returns -1. */
865 varobj_get_thread_id (struct varobj *var)
867 if (var->root->valid_block && var->root->thread_id > 0)
868 return var->root->thread_id;
874 varobj_set_frozen (struct varobj *var, int frozen)
876 /* When a variable is unfrozen, we don't fetch its value.
877 The 'not_fetched' flag remains set, so next -var-update
880 We don't fetch the value, because for structures the client
881 should do -var-update anyway. It would be bad to have different
882 client-size logic for structure and other types. */
883 var->frozen = frozen;
887 varobj_get_frozen (struct varobj *var)
892 /* A helper function that restricts a range to what is actually
893 available in a VEC. This follows the usual rules for the meaning
894 of FROM and TO -- if either is negative, the entire range is
898 restrict_range (VEC (varobj_p) *children, int *from, int *to)
900 if (*from < 0 || *to < 0)
903 *to = VEC_length (varobj_p, children);
907 if (*from > VEC_length (varobj_p, children))
908 *from = VEC_length (varobj_p, children);
909 if (*to > VEC_length (varobj_p, children))
910 *to = VEC_length (varobj_p, children);
918 /* A helper for update_dynamic_varobj_children that installs a new
919 child when needed. */
922 install_dynamic_child (struct varobj *var,
923 VEC (varobj_p) **changed,
924 VEC (varobj_p) **type_changed,
925 VEC (varobj_p) **new,
926 VEC (varobj_p) **unchanged,
932 if (VEC_length (varobj_p, var->children) < index + 1)
934 /* There's no child yet. */
935 struct varobj *child = varobj_add_child (var, name, value);
939 VEC_safe_push (varobj_p, *new, child);
945 varobj_p existing = VEC_index (varobj_p, var->children, index);
946 int type_updated = update_type_if_necessary (existing, value);
951 VEC_safe_push (varobj_p, *type_changed, existing);
953 if (install_new_value (existing, value, 0))
955 if (!type_updated && changed)
956 VEC_safe_push (varobj_p, *changed, existing);
958 else if (!type_updated && unchanged)
959 VEC_safe_push (varobj_p, *unchanged, existing);
964 dynamic_varobj_has_child_method (struct varobj *var)
966 struct cleanup *back_to;
967 PyObject *printer = var->dynamic->pretty_printer;
970 if (!gdb_python_initialized)
973 back_to = varobj_ensure_python_env (var);
974 result = PyObject_HasAttr (printer, gdbpy_children_cst);
975 do_cleanups (back_to);
982 update_dynamic_varobj_children (struct varobj *var,
983 VEC (varobj_p) **changed,
984 VEC (varobj_p) **type_changed,
985 VEC (varobj_p) **new,
986 VEC (varobj_p) **unchanged,
993 struct cleanup *back_to;
996 PyObject *printer = var->dynamic->pretty_printer;
998 if (!gdb_python_initialized)
1001 back_to = varobj_ensure_python_env (var);
1004 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
1006 do_cleanups (back_to);
1010 if (update_children || var->dynamic->child_iter == NULL)
1012 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
1017 gdbpy_print_stack ();
1018 error (_("Null value returned for children"));
1021 make_cleanup_py_decref (children);
1023 Py_XDECREF (var->dynamic->child_iter);
1024 var->dynamic->child_iter = PyObject_GetIter (children);
1025 if (var->dynamic->child_iter == NULL)
1027 gdbpy_print_stack ();
1028 error (_("Could not get children iterator"));
1031 Py_XDECREF (var->dynamic->saved_item);
1032 var->dynamic->saved_item = NULL;
1037 i = VEC_length (varobj_p, var->children);
1039 /* We ask for one extra child, so that MI can report whether there
1040 are more children. */
1041 for (; to < 0 || i < to + 1; ++i)
1046 /* See if there was a leftover from last time. */
1047 if (var->dynamic->saved_item)
1049 item = var->dynamic->saved_item;
1050 var->dynamic->saved_item = NULL;
1053 item = PyIter_Next (var->dynamic->child_iter);
1057 /* Normal end of iteration. */
1058 if (!PyErr_Occurred ())
1061 /* If we got a memory error, just use the text as the
1063 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
1065 PyObject *type, *value, *trace;
1066 char *name_str, *value_str;
1068 PyErr_Fetch (&type, &value, &trace);
1069 value_str = gdbpy_exception_to_string (type, value);
1075 gdbpy_print_stack ();
1079 name_str = xstrprintf ("<error at %d>", i);
1080 item = Py_BuildValue ("(ss)", name_str, value_str);
1085 gdbpy_print_stack ();
1093 /* Any other kind of error. */
1094 gdbpy_print_stack ();
1099 /* We don't want to push the extra child on any report list. */
1100 if (to < 0 || i < to)
1105 struct cleanup *inner;
1106 int can_mention = from < 0 || i >= from;
1108 inner = make_cleanup_py_decref (item);
1110 if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
1112 gdbpy_print_stack ();
1113 error (_("Invalid item from the child list"));
1116 v = convert_value_from_python (py_v);
1118 gdbpy_print_stack ();
1119 install_dynamic_child (var, can_mention ? changed : NULL,
1120 can_mention ? type_changed : NULL,
1121 can_mention ? new : NULL,
1122 can_mention ? unchanged : NULL,
1123 can_mention ? cchanged : NULL, i,
1125 do_cleanups (inner);
1129 Py_XDECREF (var->dynamic->saved_item);
1130 var->dynamic->saved_item = item;
1132 /* We want to truncate the child list just before this
1141 if (i < VEC_length (varobj_p, var->children))
1146 for (j = i; j < VEC_length (varobj_p, var->children); ++j)
1147 varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
1148 VEC_truncate (varobj_p, var->children, i);
1151 /* If there are fewer children than requested, note that the list of
1152 children changed. */
1153 if (to >= 0 && VEC_length (varobj_p, var->children) < to)
1156 var->num_children = VEC_length (varobj_p, var->children);
1158 do_cleanups (back_to);
1162 gdb_assert_not_reached ("should never be called if Python is not enabled");
1167 varobj_get_num_children (struct varobj *var)
1169 if (var->num_children == -1)
1171 if (var->dynamic->pretty_printer != NULL)
1175 /* If we have a dynamic varobj, don't report -1 children.
1176 So, try to fetch some children first. */
1177 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
1181 var->num_children = number_of_children (var);
1184 return var->num_children >= 0 ? var->num_children : 0;
1187 /* Creates a list of the immediate children of a variable object;
1188 the return code is the number of such children or -1 on error. */
1191 varobj_list_children (struct varobj *var, int *from, int *to)
1194 int i, children_changed;
1196 var->dynamic->children_requested = 1;
1198 if (var->dynamic->pretty_printer != NULL)
1200 /* This, in theory, can result in the number of children changing without
1201 frontend noticing. But well, calling -var-list-children on the same
1202 varobj twice is not something a sane frontend would do. */
1203 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
1204 &children_changed, 0, 0, *to);
1205 restrict_range (var->children, from, to);
1206 return var->children;
1209 if (var->num_children == -1)
1210 var->num_children = number_of_children (var);
1212 /* If that failed, give up. */
1213 if (var->num_children == -1)
1214 return var->children;
1216 /* If we're called when the list of children is not yet initialized,
1217 allocate enough elements in it. */
1218 while (VEC_length (varobj_p, var->children) < var->num_children)
1219 VEC_safe_push (varobj_p, var->children, NULL);
1221 for (i = 0; i < var->num_children; i++)
1223 varobj_p existing = VEC_index (varobj_p, var->children, i);
1225 if (existing == NULL)
1227 /* Either it's the first call to varobj_list_children for
1228 this variable object, and the child was never created,
1229 or it was explicitly deleted by the client. */
1230 name = name_of_child (var, i);
1231 existing = create_child (var, i, name);
1232 VEC_replace (varobj_p, var->children, i, existing);
1236 restrict_range (var->children, from, to);
1237 return var->children;
1242 static struct varobj *
1243 varobj_add_child (struct varobj *var, char *name, struct value *value)
1245 varobj_p v = create_child_with_value (var,
1246 VEC_length (varobj_p, var->children),
1249 VEC_safe_push (varobj_p, var->children, v);
1253 #endif /* HAVE_PYTHON */
1255 /* Obtain the type of an object Variable as a string similar to the one gdb
1256 prints on the console. */
1259 varobj_get_type (struct varobj *var)
1261 /* For the "fake" variables, do not return a type. (It's type is
1263 Do not return a type for invalid variables as well. */
1264 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
1267 return type_to_string (var->type);
1270 /* Obtain the type of an object variable. */
1273 varobj_get_gdb_type (struct varobj *var)
1278 /* Is VAR a path expression parent, i.e., can it be used to construct
1279 a valid path expression? */
1282 is_path_expr_parent (struct varobj *var)
1286 /* "Fake" children are not path_expr parents. */
1287 if (CPLUS_FAKE_CHILD (var))
1290 type = get_value_type (var);
1292 /* Anonymous unions and structs are also not path_expr parents. */
1293 return !((TYPE_CODE (type) == TYPE_CODE_STRUCT
1294 || TYPE_CODE (type) == TYPE_CODE_UNION)
1295 && TYPE_NAME (type) == NULL);
1298 /* Return the path expression parent for VAR. */
1300 static struct varobj *
1301 get_path_expr_parent (struct varobj *var)
1303 struct varobj *parent = var;
1305 while (!is_root_p (parent) && !is_path_expr_parent (parent))
1306 parent = parent->parent;
1311 /* Return a pointer to the full rooted expression of varobj VAR.
1312 If it has not been computed yet, compute it. */
1314 varobj_get_path_expr (struct varobj *var)
1316 if (var->path_expr != NULL)
1317 return var->path_expr;
1320 /* For root varobjs, we initialize path_expr
1321 when creating varobj, so here it should be
1323 gdb_assert (!is_root_p (var));
1324 return (*var->root->lang->path_expr_of_child) (var);
1328 enum varobj_languages
1329 varobj_get_language (struct varobj *var)
1331 return variable_language (var);
1335 varobj_get_attributes (struct varobj *var)
1339 if (varobj_editable_p (var))
1340 /* FIXME: define masks for attributes. */
1341 attributes |= 0x00000001; /* Editable */
1347 varobj_pretty_printed_p (struct varobj *var)
1349 return var->dynamic->pretty_printer != NULL;
1353 varobj_get_formatted_value (struct varobj *var,
1354 enum varobj_display_formats format)
1356 return my_value_of_variable (var, format);
1360 varobj_get_value (struct varobj *var)
1362 return my_value_of_variable (var, var->format);
1365 /* Set the value of an object variable (if it is editable) to the
1366 value of the given expression. */
1367 /* Note: Invokes functions that can call error(). */
1370 varobj_set_value (struct varobj *var, char *expression)
1372 struct value *val = NULL; /* Initialize to keep gcc happy. */
1373 /* The argument "expression" contains the variable's new value.
1374 We need to first construct a legal expression for this -- ugh! */
1375 /* Does this cover all the bases? */
1376 struct expression *exp;
1377 struct value *value = NULL; /* Initialize to keep gcc happy. */
1378 int saved_input_radix = input_radix;
1379 const char *s = expression;
1380 volatile struct gdb_exception except;
1382 gdb_assert (varobj_editable_p (var));
1384 input_radix = 10; /* ALWAYS reset to decimal temporarily. */
1385 exp = parse_exp_1 (&s, 0, 0, 0);
1386 TRY_CATCH (except, RETURN_MASK_ERROR)
1388 value = evaluate_expression (exp);
1391 if (except.reason < 0)
1393 /* We cannot proceed without a valid expression. */
1398 /* All types that are editable must also be changeable. */
1399 gdb_assert (varobj_value_is_changeable_p (var));
1401 /* The value of a changeable variable object must not be lazy. */
1402 gdb_assert (!value_lazy (var->value));
1404 /* Need to coerce the input. We want to check if the
1405 value of the variable object will be different
1406 after assignment, and the first thing value_assign
1407 does is coerce the input.
1408 For example, if we are assigning an array to a pointer variable we
1409 should compare the pointer with the array's address, not with the
1411 value = coerce_array (value);
1413 /* The new value may be lazy. value_assign, or
1414 rather value_contents, will take care of this. */
1415 TRY_CATCH (except, RETURN_MASK_ERROR)
1417 val = value_assign (var->value, value);
1420 if (except.reason < 0)
1423 /* If the value has changed, record it, so that next -var-update can
1424 report this change. If a variable had a value of '1', we've set it
1425 to '333' and then set again to '1', when -var-update will report this
1426 variable as changed -- because the first assignment has set the
1427 'updated' flag. There's no need to optimize that, because return value
1428 of -var-update should be considered an approximation. */
1429 var->updated = install_new_value (var, val, 0 /* Compare values. */);
1430 input_radix = saved_input_radix;
1436 /* A helper function to install a constructor function and visualizer
1437 in a varobj_dynamic. */
1440 install_visualizer (struct varobj_dynamic *var, PyObject *constructor,
1441 PyObject *visualizer)
1443 Py_XDECREF (var->constructor);
1444 var->constructor = constructor;
1446 Py_XDECREF (var->pretty_printer);
1447 var->pretty_printer = visualizer;
1449 Py_XDECREF (var->child_iter);
1450 var->child_iter = NULL;
1453 /* Install the default visualizer for VAR. */
1456 install_default_visualizer (struct varobj *var)
1458 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1459 if (CPLUS_FAKE_CHILD (var))
1462 if (pretty_printing)
1464 PyObject *pretty_printer = NULL;
1468 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
1469 if (! pretty_printer)
1471 gdbpy_print_stack ();
1472 error (_("Cannot instantiate printer for default visualizer"));
1476 if (pretty_printer == Py_None)
1478 Py_DECREF (pretty_printer);
1479 pretty_printer = NULL;
1482 install_visualizer (var->dynamic, NULL, pretty_printer);
1486 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1487 make a new object. */
1490 construct_visualizer (struct varobj *var, PyObject *constructor)
1492 PyObject *pretty_printer;
1494 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1495 if (CPLUS_FAKE_CHILD (var))
1498 Py_INCREF (constructor);
1499 if (constructor == Py_None)
1500 pretty_printer = NULL;
1503 pretty_printer = instantiate_pretty_printer (constructor, var->value);
1504 if (! pretty_printer)
1506 gdbpy_print_stack ();
1507 Py_DECREF (constructor);
1508 constructor = Py_None;
1509 Py_INCREF (constructor);
1512 if (pretty_printer == Py_None)
1514 Py_DECREF (pretty_printer);
1515 pretty_printer = NULL;
1519 install_visualizer (var->dynamic, constructor, pretty_printer);
1522 #endif /* HAVE_PYTHON */
1524 /* A helper function for install_new_value. This creates and installs
1525 a visualizer for VAR, if appropriate. */
1528 install_new_value_visualizer (struct varobj *var)
1531 /* If the constructor is None, then we want the raw value. If VAR
1532 does not have a value, just skip this. */
1533 if (!gdb_python_initialized)
1536 if (var->dynamic->constructor != Py_None && var->value != NULL)
1538 struct cleanup *cleanup;
1540 cleanup = varobj_ensure_python_env (var);
1542 if (var->dynamic->constructor == NULL)
1543 install_default_visualizer (var);
1545 construct_visualizer (var, var->dynamic->constructor);
1547 do_cleanups (cleanup);
1554 /* When using RTTI to determine variable type it may be changed in runtime when
1555 the variable value is changed. This function checks whether type of varobj
1556 VAR will change when a new value NEW_VALUE is assigned and if it is so
1557 updates the type of VAR. */
1560 update_type_if_necessary (struct varobj *var, struct value *new_value)
1564 struct value_print_options opts;
1566 get_user_print_options (&opts);
1567 if (opts.objectprint)
1569 struct type *new_type;
1570 char *curr_type_str, *new_type_str;
1572 new_type = value_actual_type (new_value, 0, 0);
1573 new_type_str = type_to_string (new_type);
1574 curr_type_str = varobj_get_type (var);
1575 if (strcmp (curr_type_str, new_type_str) != 0)
1577 var->type = new_type;
1579 /* This information may be not valid for a new type. */
1580 varobj_delete (var, NULL, 1);
1581 VEC_free (varobj_p, var->children);
1582 var->num_children = -1;
1591 /* Assign a new value to a variable object. If INITIAL is non-zero,
1592 this is the first assignement after the variable object was just
1593 created, or changed type. In that case, just assign the value
1595 Otherwise, assign the new value, and return 1 if the value is
1596 different from the current one, 0 otherwise. The comparison is
1597 done on textual representation of value. Therefore, some types
1598 need not be compared. E.g. for structures the reported value is
1599 always "{...}", so no comparison is necessary here. If the old
1600 value was NULL and new one is not, or vice versa, we always return 1.
1602 The VALUE parameter should not be released -- the function will
1603 take care of releasing it when needed. */
1605 install_new_value (struct varobj *var, struct value *value, int initial)
1610 int intentionally_not_fetched = 0;
1611 char *print_value = NULL;
1613 /* We need to know the varobj's type to decide if the value should
1614 be fetched or not. C++ fake children (public/protected/private)
1615 don't have a type. */
1616 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
1617 changeable = varobj_value_is_changeable_p (var);
1619 /* If the type has custom visualizer, we consider it to be always
1620 changeable. FIXME: need to make sure this behaviour will not
1621 mess up read-sensitive values. */
1622 if (var->dynamic->pretty_printer != NULL)
1625 need_to_fetch = changeable;
1627 /* We are not interested in the address of references, and given
1628 that in C++ a reference is not rebindable, it cannot
1629 meaningfully change. So, get hold of the real value. */
1631 value = coerce_ref (value);
1633 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
1634 /* For unions, we need to fetch the value implicitly because
1635 of implementation of union member fetch. When gdb
1636 creates a value for a field and the value of the enclosing
1637 structure is not lazy, it immediately copies the necessary
1638 bytes from the enclosing values. If the enclosing value is
1639 lazy, the call to value_fetch_lazy on the field will read
1640 the data from memory. For unions, that means we'll read the
1641 same memory more than once, which is not desirable. So
1645 /* The new value might be lazy. If the type is changeable,
1646 that is we'll be comparing values of this type, fetch the
1647 value now. Otherwise, on the next update the old value
1648 will be lazy, which means we've lost that old value. */
1649 if (need_to_fetch && value && value_lazy (value))
1651 struct varobj *parent = var->parent;
1652 int frozen = var->frozen;
1654 for (; !frozen && parent; parent = parent->parent)
1655 frozen |= parent->frozen;
1657 if (frozen && initial)
1659 /* For variables that are frozen, or are children of frozen
1660 variables, we don't do fetch on initial assignment.
1661 For non-initial assignemnt we do the fetch, since it means we're
1662 explicitly asked to compare the new value with the old one. */
1663 intentionally_not_fetched = 1;
1667 volatile struct gdb_exception except;
1669 TRY_CATCH (except, RETURN_MASK_ERROR)
1671 value_fetch_lazy (value);
1674 if (except.reason < 0)
1676 /* Set the value to NULL, so that for the next -var-update,
1677 we don't try to compare the new value with this value,
1678 that we couldn't even read. */
1684 /* Get a reference now, before possibly passing it to any Python
1685 code that might release it. */
1687 value_incref (value);
1689 /* Below, we'll be comparing string rendering of old and new
1690 values. Don't get string rendering if the value is
1691 lazy -- if it is, the code above has decided that the value
1692 should not be fetched. */
1693 if (value != NULL && !value_lazy (value)
1694 && var->dynamic->pretty_printer == NULL)
1695 print_value = value_get_print_value (value, var->format, var);
1697 /* If the type is changeable, compare the old and the new values.
1698 If this is the initial assignment, we don't have any old value
1700 if (!initial && changeable)
1702 /* If the value of the varobj was changed by -var-set-value,
1703 then the value in the varobj and in the target is the same.
1704 However, that value is different from the value that the
1705 varobj had after the previous -var-update. So need to the
1706 varobj as changed. */
1711 else if (var->dynamic->pretty_printer == NULL)
1713 /* Try to compare the values. That requires that both
1714 values are non-lazy. */
1715 if (var->not_fetched && value_lazy (var->value))
1717 /* This is a frozen varobj and the value was never read.
1718 Presumably, UI shows some "never read" indicator.
1719 Now that we've fetched the real value, we need to report
1720 this varobj as changed so that UI can show the real
1724 else if (var->value == NULL && value == NULL)
1727 else if (var->value == NULL || value == NULL)
1733 gdb_assert (!value_lazy (var->value));
1734 gdb_assert (!value_lazy (value));
1736 gdb_assert (var->print_value != NULL && print_value != NULL);
1737 if (strcmp (var->print_value, print_value) != 0)
1743 if (!initial && !changeable)
1745 /* For values that are not changeable, we don't compare the values.
1746 However, we want to notice if a value was not NULL and now is NULL,
1747 or vise versa, so that we report when top-level varobjs come in scope
1748 and leave the scope. */
1749 changed = (var->value != NULL) != (value != NULL);
1752 /* We must always keep the new value, since children depend on it. */
1753 if (var->value != NULL && var->value != value)
1754 value_free (var->value);
1756 if (value && value_lazy (value) && intentionally_not_fetched)
1757 var->not_fetched = 1;
1759 var->not_fetched = 0;
1762 install_new_value_visualizer (var);
1764 /* If we installed a pretty-printer, re-compare the printed version
1765 to see if the variable changed. */
1766 if (var->dynamic->pretty_printer != NULL)
1768 xfree (print_value);
1769 print_value = value_get_print_value (var->value, var->format, var);
1770 if ((var->print_value == NULL && print_value != NULL)
1771 || (var->print_value != NULL && print_value == NULL)
1772 || (var->print_value != NULL && print_value != NULL
1773 && strcmp (var->print_value, print_value) != 0))
1776 if (var->print_value)
1777 xfree (var->print_value);
1778 var->print_value = print_value;
1780 gdb_assert (!var->value || value_type (var->value));
1785 /* Return the requested range for a varobj. VAR is the varobj. FROM
1786 and TO are out parameters; *FROM and *TO will be set to the
1787 selected sub-range of VAR. If no range was selected using
1788 -var-set-update-range, then both will be -1. */
1790 varobj_get_child_range (struct varobj *var, int *from, int *to)
1796 /* Set the selected sub-range of children of VAR to start at index
1797 FROM and end at index TO. If either FROM or TO is less than zero,
1798 this is interpreted as a request for all children. */
1800 varobj_set_child_range (struct varobj *var, int from, int to)
1807 varobj_set_visualizer (struct varobj *var, const char *visualizer)
1810 PyObject *mainmod, *globals, *constructor;
1811 struct cleanup *back_to;
1813 if (!gdb_python_initialized)
1816 back_to = varobj_ensure_python_env (var);
1818 mainmod = PyImport_AddModule ("__main__");
1819 globals = PyModule_GetDict (mainmod);
1820 Py_INCREF (globals);
1821 make_cleanup_py_decref (globals);
1823 constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
1827 gdbpy_print_stack ();
1828 error (_("Could not evaluate visualizer expression: %s"), visualizer);
1831 construct_visualizer (var, constructor);
1832 Py_XDECREF (constructor);
1834 /* If there are any children now, wipe them. */
1835 varobj_delete (var, NULL, 1 /* children only */);
1836 var->num_children = -1;
1838 do_cleanups (back_to);
1840 error (_("Python support required"));
1844 /* If NEW_VALUE is the new value of the given varobj (var), return
1845 non-zero if var has mutated. In other words, if the type of
1846 the new value is different from the type of the varobj's old
1849 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1852 varobj_value_has_mutated (struct varobj *var, struct value *new_value,
1853 struct type *new_type)
1855 /* If we haven't previously computed the number of children in var,
1856 it does not matter from the front-end's perspective whether
1857 the type has mutated or not. For all intents and purposes,
1858 it has not mutated. */
1859 if (var->num_children < 0)
1862 if (var->root->lang->value_has_mutated)
1863 return var->root->lang->value_has_mutated (var, new_value, new_type);
1868 /* Update the values for a variable and its children. This is a
1869 two-pronged attack. First, re-parse the value for the root's
1870 expression to see if it's changed. Then go all the way
1871 through its children, reconstructing them and noting if they've
1874 The EXPLICIT parameter specifies if this call is result
1875 of MI request to update this specific variable, or
1876 result of implicit -var-update *. For implicit request, we don't
1877 update frozen variables.
1879 NOTE: This function may delete the caller's varobj. If it
1880 returns TYPE_CHANGED, then it has done this and VARP will be modified
1881 to point to the new varobj. */
1883 VEC(varobj_update_result) *
1884 varobj_update (struct varobj **varp, int explicit)
1886 int type_changed = 0;
1889 VEC (varobj_update_result) *stack = NULL;
1890 VEC (varobj_update_result) *result = NULL;
1892 /* Frozen means frozen -- we don't check for any change in
1893 this varobj, including its going out of scope, or
1894 changing type. One use case for frozen varobjs is
1895 retaining previously evaluated expressions, and we don't
1896 want them to be reevaluated at all. */
1897 if (!explicit && (*varp)->frozen)
1900 if (!(*varp)->root->is_valid)
1902 varobj_update_result r = {0};
1905 r.status = VAROBJ_INVALID;
1906 VEC_safe_push (varobj_update_result, result, &r);
1910 if ((*varp)->root->rootvar == *varp)
1912 varobj_update_result r = {0};
1915 r.status = VAROBJ_IN_SCOPE;
1917 /* Update the root variable. value_of_root can return NULL
1918 if the variable is no longer around, i.e. we stepped out of
1919 the frame in which a local existed. We are letting the
1920 value_of_root variable dispose of the varobj if the type
1922 new = value_of_root (varp, &type_changed);
1923 if (update_type_if_necessary(*varp, new))
1926 r.type_changed = type_changed;
1927 if (install_new_value ((*varp), new, type_changed))
1931 r.status = VAROBJ_NOT_IN_SCOPE;
1932 r.value_installed = 1;
1934 if (r.status == VAROBJ_NOT_IN_SCOPE)
1936 if (r.type_changed || r.changed)
1937 VEC_safe_push (varobj_update_result, result, &r);
1941 VEC_safe_push (varobj_update_result, stack, &r);
1945 varobj_update_result r = {0};
1948 VEC_safe_push (varobj_update_result, stack, &r);
1951 /* Walk through the children, reconstructing them all. */
1952 while (!VEC_empty (varobj_update_result, stack))
1954 varobj_update_result r = *(VEC_last (varobj_update_result, stack));
1955 struct varobj *v = r.varobj;
1957 VEC_pop (varobj_update_result, stack);
1959 /* Update this variable, unless it's a root, which is already
1961 if (!r.value_installed)
1963 struct type *new_type;
1965 new = value_of_child (v->parent, v->index);
1966 if (update_type_if_necessary(v, new))
1969 new_type = value_type (new);
1971 new_type = v->root->lang->type_of_child (v->parent, v->index);
1973 if (varobj_value_has_mutated (v, new, new_type))
1975 /* The children are no longer valid; delete them now.
1976 Report the fact that its type changed as well. */
1977 varobj_delete (v, NULL, 1 /* only_children */);
1978 v->num_children = -1;
1985 if (install_new_value (v, new, r.type_changed))
1992 /* We probably should not get children of a varobj that has a
1993 pretty-printer, but for which -var-list-children was never
1995 if (v->dynamic->pretty_printer != NULL)
1997 VEC (varobj_p) *changed = 0, *type_changed = 0, *unchanged = 0;
1998 VEC (varobj_p) *new = 0;
1999 int i, children_changed = 0;
2004 if (!v->dynamic->children_requested)
2008 /* If we initially did not have potential children, but
2009 now we do, consider the varobj as changed.
2010 Otherwise, if children were never requested, consider
2011 it as unchanged -- presumably, such varobj is not yet
2012 expanded in the UI, so we need not bother getting
2014 if (!varobj_has_more (v, 0))
2016 update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL,
2018 if (varobj_has_more (v, 0))
2023 VEC_safe_push (varobj_update_result, result, &r);
2028 /* If update_dynamic_varobj_children returns 0, then we have
2029 a non-conforming pretty-printer, so we skip it. */
2030 if (update_dynamic_varobj_children (v, &changed, &type_changed, &new,
2031 &unchanged, &children_changed, 1,
2034 if (children_changed || new)
2036 r.children_changed = 1;
2039 /* Push in reverse order so that the first child is
2040 popped from the work stack first, and so will be
2041 added to result first. This does not affect
2042 correctness, just "nicer". */
2043 for (i = VEC_length (varobj_p, type_changed) - 1; i >= 0; --i)
2045 varobj_p tmp = VEC_index (varobj_p, type_changed, i);
2046 varobj_update_result r = {0};
2048 /* Type may change only if value was changed. */
2052 r.value_installed = 1;
2053 VEC_safe_push (varobj_update_result, stack, &r);
2055 for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
2057 varobj_p tmp = VEC_index (varobj_p, changed, i);
2058 varobj_update_result r = {0};
2062 r.value_installed = 1;
2063 VEC_safe_push (varobj_update_result, stack, &r);
2065 for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
2067 varobj_p tmp = VEC_index (varobj_p, unchanged, i);
2071 varobj_update_result r = {0};
2074 r.value_installed = 1;
2075 VEC_safe_push (varobj_update_result, stack, &r);
2078 if (r.changed || r.children_changed)
2079 VEC_safe_push (varobj_update_result, result, &r);
2081 /* Free CHANGED, TYPE_CHANGED and UNCHANGED, but not NEW,
2082 because NEW has been put into the result vector. */
2083 VEC_free (varobj_p, changed);
2084 VEC_free (varobj_p, type_changed);
2085 VEC_free (varobj_p, unchanged);
2091 /* Push any children. Use reverse order so that the first
2092 child is popped from the work stack first, and so
2093 will be added to result first. This does not
2094 affect correctness, just "nicer". */
2095 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
2097 varobj_p c = VEC_index (varobj_p, v->children, i);
2099 /* Child may be NULL if explicitly deleted by -var-delete. */
2100 if (c != NULL && !c->frozen)
2102 varobj_update_result r = {0};
2105 VEC_safe_push (varobj_update_result, stack, &r);
2109 if (r.changed || r.type_changed)
2110 VEC_safe_push (varobj_update_result, result, &r);
2113 VEC_free (varobj_update_result, stack);
2119 /* Helper functions */
2122 * Variable object construction/destruction
2126 delete_variable (struct cpstack **resultp, struct varobj *var,
2127 int only_children_p)
2131 delete_variable_1 (resultp, &delcount, var,
2132 only_children_p, 1 /* remove_from_parent_p */ );
2137 /* Delete the variable object VAR and its children. */
2138 /* IMPORTANT NOTE: If we delete a variable which is a child
2139 and the parent is not removed we dump core. It must be always
2140 initially called with remove_from_parent_p set. */
2142 delete_variable_1 (struct cpstack **resultp, int *delcountp,
2143 struct varobj *var, int only_children_p,
2144 int remove_from_parent_p)
2148 /* Delete any children of this variable, too. */
2149 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
2151 varobj_p child = VEC_index (varobj_p, var->children, i);
2155 if (!remove_from_parent_p)
2156 child->parent = NULL;
2157 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
2159 VEC_free (varobj_p, var->children);
2161 /* if we were called to delete only the children we are done here. */
2162 if (only_children_p)
2165 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
2166 /* If the name is null, this is a temporary variable, that has not
2167 yet been installed, don't report it, it belongs to the caller... */
2168 if (var->obj_name != NULL)
2170 cppush (resultp, xstrdup (var->obj_name));
2171 *delcountp = *delcountp + 1;
2174 /* If this variable has a parent, remove it from its parent's list. */
2175 /* OPTIMIZATION: if the parent of this variable is also being deleted,
2176 (as indicated by remove_from_parent_p) we don't bother doing an
2177 expensive list search to find the element to remove when we are
2178 discarding the list afterwards. */
2179 if ((remove_from_parent_p) && (var->parent != NULL))
2181 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
2184 if (var->obj_name != NULL)
2185 uninstall_variable (var);
2187 /* Free memory associated with this variable. */
2188 free_variable (var);
2191 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
2193 install_variable (struct varobj *var)
2196 struct vlist *newvl;
2198 unsigned int index = 0;
2201 for (chp = var->obj_name; *chp; chp++)
2203 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2206 cv = *(varobj_table + index);
2207 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2211 error (_("Duplicate variable object name"));
2213 /* Add varobj to hash table. */
2214 newvl = xmalloc (sizeof (struct vlist));
2215 newvl->next = *(varobj_table + index);
2217 *(varobj_table + index) = newvl;
2219 /* If root, add varobj to root list. */
2220 if (is_root_p (var))
2222 /* Add to list of root variables. */
2223 if (rootlist == NULL)
2224 var->root->next = NULL;
2226 var->root->next = rootlist;
2227 rootlist = var->root;
2233 /* Unistall the object VAR. */
2235 uninstall_variable (struct varobj *var)
2239 struct varobj_root *cr;
2240 struct varobj_root *prer;
2242 unsigned int index = 0;
2245 /* Remove varobj from hash table. */
2246 for (chp = var->obj_name; *chp; chp++)
2248 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2251 cv = *(varobj_table + index);
2253 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2260 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
2265 ("Assertion failed: Could not find variable object \"%s\" to delete",
2271 *(varobj_table + index) = cv->next;
2273 prev->next = cv->next;
2277 /* If root, remove varobj from root list. */
2278 if (is_root_p (var))
2280 /* Remove from list of root variables. */
2281 if (rootlist == var->root)
2282 rootlist = var->root->next;
2287 while ((cr != NULL) && (cr->rootvar != var))
2294 warning (_("Assertion failed: Could not find "
2295 "varobj \"%s\" in root list"),
2302 prer->next = cr->next;
2308 /* Create and install a child of the parent of the given name. */
2309 static struct varobj *
2310 create_child (struct varobj *parent, int index, char *name)
2312 return create_child_with_value (parent, index, name,
2313 value_of_child (parent, index));
2316 /* Does CHILD represent a child with no name? This happens when
2317 the child is an anonmous struct or union and it has no field name
2318 in its parent variable.
2320 This has already been determined by *_describe_child. The easiest
2321 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
2324 is_anonymous_child (struct varobj *child)
2326 return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
2327 || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
2330 static struct varobj *
2331 create_child_with_value (struct varobj *parent, int index, char *name,
2332 struct value *value)
2334 struct varobj *child;
2337 child = new_variable ();
2339 /* NAME is allocated by caller. */
2341 child->index = index;
2342 child->parent = parent;
2343 child->root = parent->root;
2345 if (is_anonymous_child (child))
2346 childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
2348 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
2349 child->obj_name = childs_name;
2351 install_variable (child);
2353 /* Compute the type of the child. Must do this before
2354 calling install_new_value. */
2356 /* If the child had no evaluation errors, var->value
2357 will be non-NULL and contain a valid type. */
2358 child->type = value_actual_type (value, 0, NULL);
2360 /* Otherwise, we must compute the type. */
2361 child->type = (*child->root->lang->type_of_child) (child->parent,
2363 install_new_value (child, value, 1);
2370 * Miscellaneous utility functions.
2373 /* Allocate memory and initialize a new variable. */
2374 static struct varobj *
2379 var = (struct varobj *) xmalloc (sizeof (struct varobj));
2381 var->path_expr = NULL;
2382 var->obj_name = NULL;
2386 var->num_children = -1;
2388 var->children = NULL;
2392 var->print_value = NULL;
2394 var->not_fetched = 0;
2396 = (struct varobj_dynamic *) xmalloc (sizeof (struct varobj_dynamic));
2397 var->dynamic->children_requested = 0;
2400 var->dynamic->constructor = 0;
2401 var->dynamic->pretty_printer = 0;
2402 var->dynamic->child_iter = 0;
2403 var->dynamic->saved_item = 0;
2408 /* Allocate memory and initialize a new root variable. */
2409 static struct varobj *
2410 new_root_variable (void)
2412 struct varobj *var = new_variable ();
2414 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
2415 var->root->lang = NULL;
2416 var->root->exp = NULL;
2417 var->root->valid_block = NULL;
2418 var->root->frame = null_frame_id;
2419 var->root->floating = 0;
2420 var->root->rootvar = NULL;
2421 var->root->is_valid = 1;
2426 /* Free any allocated memory associated with VAR. */
2428 free_variable (struct varobj *var)
2431 if (var->dynamic->pretty_printer != NULL)
2433 struct cleanup *cleanup = varobj_ensure_python_env (var);
2435 Py_XDECREF (var->dynamic->constructor);
2436 Py_XDECREF (var->dynamic->pretty_printer);
2437 Py_XDECREF (var->dynamic->child_iter);
2438 Py_XDECREF (var->dynamic->saved_item);
2439 do_cleanups (cleanup);
2443 value_free (var->value);
2445 /* Free the expression if this is a root variable. */
2446 if (is_root_p (var))
2448 xfree (var->root->exp);
2453 xfree (var->obj_name);
2454 xfree (var->print_value);
2455 xfree (var->path_expr);
2456 xfree (var->dynamic);
2461 do_free_variable_cleanup (void *var)
2463 free_variable (var);
2466 static struct cleanup *
2467 make_cleanup_free_variable (struct varobj *var)
2469 return make_cleanup (do_free_variable_cleanup, var);
2472 /* This returns the type of the variable. It also skips past typedefs
2473 to return the real type of the variable.
2475 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2476 except within get_target_type and get_type. */
2477 static struct type *
2478 get_type (struct varobj *var)
2484 type = check_typedef (type);
2489 /* Return the type of the value that's stored in VAR,
2490 or that would have being stored there if the
2491 value were accessible.
2493 This differs from VAR->type in that VAR->type is always
2494 the true type of the expession in the source language.
2495 The return value of this function is the type we're
2496 actually storing in varobj, and using for displaying
2497 the values and for comparing previous and new values.
2499 For example, top-level references are always stripped. */
2500 static struct type *
2501 get_value_type (struct varobj *var)
2506 type = value_type (var->value);
2510 type = check_typedef (type);
2512 if (TYPE_CODE (type) == TYPE_CODE_REF)
2513 type = get_target_type (type);
2515 type = check_typedef (type);
2520 /* This returns the target type (or NULL) of TYPE, also skipping
2521 past typedefs, just like get_type ().
2523 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2524 except within get_target_type and get_type. */
2525 static struct type *
2526 get_target_type (struct type *type)
2530 type = TYPE_TARGET_TYPE (type);
2532 type = check_typedef (type);
2538 /* What is the default display for this variable? We assume that
2539 everything is "natural". Any exceptions? */
2540 static enum varobj_display_formats
2541 variable_default_display (struct varobj *var)
2543 return FORMAT_NATURAL;
2546 /* FIXME: The following should be generic for any pointer. */
2548 cppush (struct cpstack **pstack, char *name)
2552 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
2558 /* FIXME: The following should be generic for any pointer. */
2560 cppop (struct cpstack **pstack)
2565 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
2570 *pstack = (*pstack)->next;
2577 * Language-dependencies
2580 /* Common entry points */
2582 /* Get the language of variable VAR. */
2583 static enum varobj_languages
2584 variable_language (struct varobj *var)
2586 enum varobj_languages lang;
2588 switch (var->root->exp->language_defn->la_language)
2594 case language_cplus:
2608 /* Return the number of children for a given variable.
2609 The result of this function is defined by the language
2610 implementation. The number of children returned by this function
2611 is the number of children that the user will see in the variable
2614 number_of_children (struct varobj *var)
2616 return (*var->root->lang->number_of_children) (var);
2619 /* What is the expression for the root varobj VAR? Returns a malloc'd
2622 name_of_variable (struct varobj *var)
2624 return (*var->root->lang->name_of_variable) (var);
2627 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2630 name_of_child (struct varobj *var, int index)
2632 return (*var->root->lang->name_of_child) (var, index);
2635 /* If frame associated with VAR can be found, switch
2636 to it and return 1. Otherwise, return 0. */
2639 check_scope (struct varobj *var)
2641 struct frame_info *fi;
2644 fi = frame_find_by_id (var->root->frame);
2649 CORE_ADDR pc = get_frame_pc (fi);
2651 if (pc < BLOCK_START (var->root->valid_block) ||
2652 pc >= BLOCK_END (var->root->valid_block))
2660 /* Helper function to value_of_root. */
2662 static struct value *
2663 value_of_root_1 (struct varobj **var_handle)
2665 struct value *new_val = NULL;
2666 struct varobj *var = *var_handle;
2667 int within_scope = 0;
2668 struct cleanup *back_to;
2670 /* Only root variables can be updated... */
2671 if (!is_root_p (var))
2672 /* Not a root var. */
2675 back_to = make_cleanup_restore_current_thread ();
2677 /* Determine whether the variable is still around. */
2678 if (var->root->valid_block == NULL || var->root->floating)
2680 else if (var->root->thread_id == 0)
2682 /* The program was single-threaded when the variable object was
2683 created. Technically, it's possible that the program became
2684 multi-threaded since then, but we don't support such
2686 within_scope = check_scope (var);
2690 ptid_t ptid = thread_id_to_pid (var->root->thread_id);
2691 if (in_thread_list (ptid))
2693 switch_to_thread (ptid);
2694 within_scope = check_scope (var);
2700 volatile struct gdb_exception except;
2702 /* We need to catch errors here, because if evaluate
2703 expression fails we want to just return NULL. */
2704 TRY_CATCH (except, RETURN_MASK_ERROR)
2706 new_val = evaluate_expression (var->root->exp);
2710 do_cleanups (back_to);
2715 /* What is the ``struct value *'' of the root variable VAR?
2716 For floating variable object, evaluation can get us a value
2717 of different type from what is stored in varobj already. In
2719 - *type_changed will be set to 1
2720 - old varobj will be freed, and new one will be
2721 created, with the same name.
2722 - *var_handle will be set to the new varobj
2723 Otherwise, *type_changed will be set to 0. */
2724 static struct value *
2725 value_of_root (struct varobj **var_handle, int *type_changed)
2729 if (var_handle == NULL)
2734 /* This should really be an exception, since this should
2735 only get called with a root variable. */
2737 if (!is_root_p (var))
2740 if (var->root->floating)
2742 struct varobj *tmp_var;
2743 char *old_type, *new_type;
2745 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
2746 USE_SELECTED_FRAME);
2747 if (tmp_var == NULL)
2751 old_type = varobj_get_type (var);
2752 new_type = varobj_get_type (tmp_var);
2753 if (strcmp (old_type, new_type) == 0)
2755 /* The expression presently stored inside var->root->exp
2756 remembers the locations of local variables relatively to
2757 the frame where the expression was created (in DWARF location
2758 button, for example). Naturally, those locations are not
2759 correct in other frames, so update the expression. */
2761 struct expression *tmp_exp = var->root->exp;
2763 var->root->exp = tmp_var->root->exp;
2764 tmp_var->root->exp = tmp_exp;
2766 varobj_delete (tmp_var, NULL, 0);
2771 tmp_var->obj_name = xstrdup (var->obj_name);
2772 tmp_var->from = var->from;
2773 tmp_var->to = var->to;
2774 varobj_delete (var, NULL, 0);
2776 install_variable (tmp_var);
2777 *var_handle = tmp_var;
2790 struct value *value;
2792 value = value_of_root_1 (var_handle);
2793 if (var->value == NULL || value == NULL)
2795 /* For root varobj-s, a NULL value indicates a scoping issue.
2796 So, nothing to do in terms of checking for mutations. */
2798 else if (varobj_value_has_mutated (var, value, value_type (value)))
2800 /* The type has mutated, so the children are no longer valid.
2801 Just delete them, and tell our caller that the type has
2803 varobj_delete (var, NULL, 1 /* only_children */);
2804 var->num_children = -1;
2813 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2814 static struct value *
2815 value_of_child (struct varobj *parent, int index)
2817 struct value *value;
2819 value = (*parent->root->lang->value_of_child) (parent, index);
2824 /* GDB already has a command called "value_of_variable". Sigh. */
2826 my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
2828 if (var->root->is_valid)
2830 if (var->dynamic->pretty_printer != NULL)
2831 return value_get_print_value (var->value, var->format, var);
2832 return (*var->root->lang->value_of_variable) (var, format);
2839 value_get_print_value (struct value *value, enum varobj_display_formats format,
2842 struct ui_file *stb;
2843 struct cleanup *old_chain;
2844 char *thevalue = NULL;
2845 struct value_print_options opts;
2846 struct type *type = NULL;
2848 char *encoding = NULL;
2849 struct gdbarch *gdbarch = NULL;
2850 /* Initialize it just to avoid a GCC false warning. */
2851 CORE_ADDR str_addr = 0;
2852 int string_print = 0;
2857 stb = mem_fileopen ();
2858 old_chain = make_cleanup_ui_file_delete (stb);
2860 gdbarch = get_type_arch (value_type (value));
2862 if (gdb_python_initialized)
2864 PyObject *value_formatter = var->dynamic->pretty_printer;
2866 varobj_ensure_python_env (var);
2868 if (value_formatter)
2870 /* First check to see if we have any children at all. If so,
2871 we simply return {...}. */
2872 if (dynamic_varobj_has_child_method (var))
2874 do_cleanups (old_chain);
2875 return xstrdup ("{...}");
2878 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2880 struct value *replacement;
2881 PyObject *output = NULL;
2883 output = apply_varobj_pretty_printer (value_formatter,
2887 /* If we have string like output ... */
2890 make_cleanup_py_decref (output);
2892 /* If this is a lazy string, extract it. For lazy
2893 strings we always print as a string, so set
2895 if (gdbpy_is_lazy_string (output))
2897 gdbpy_extract_lazy_string (output, &str_addr, &type,
2899 make_cleanup (free_current_contents, &encoding);
2904 /* If it is a regular (non-lazy) string, extract
2905 it and copy the contents into THEVALUE. If the
2906 hint says to print it as a string, set
2907 string_print. Otherwise just return the extracted
2908 string as a value. */
2910 char *s = python_string_to_target_string (output);
2916 hint = gdbpy_get_display_hint (value_formatter);
2919 if (!strcmp (hint, "string"))
2925 thevalue = xmemdup (s, len + 1, len + 1);
2926 type = builtin_type (gdbarch)->builtin_char;
2931 do_cleanups (old_chain);
2935 make_cleanup (xfree, thevalue);
2938 gdbpy_print_stack ();
2941 /* If the printer returned a replacement value, set VALUE
2942 to REPLACEMENT. If there is not a replacement value,
2943 just use the value passed to this function. */
2945 value = replacement;
2951 get_formatted_print_options (&opts, format_code[(int) format]);
2955 /* If the THEVALUE has contents, it is a regular string. */
2957 LA_PRINT_STRING (stb, type, (gdb_byte *) thevalue, len, encoding, 0, &opts);
2958 else if (string_print)
2959 /* Otherwise, if string_print is set, and it is not a regular
2960 string, it is a lazy string. */
2961 val_print_string (type, encoding, str_addr, len, stb, &opts);
2963 /* All other cases. */
2964 common_val_print (value, stb, 0, &opts, current_language);
2966 thevalue = ui_file_xstrdup (stb, NULL);
2968 do_cleanups (old_chain);
2973 varobj_editable_p (struct varobj *var)
2977 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
2980 type = get_value_type (var);
2982 switch (TYPE_CODE (type))
2984 case TYPE_CODE_STRUCT:
2985 case TYPE_CODE_UNION:
2986 case TYPE_CODE_ARRAY:
2987 case TYPE_CODE_FUNC:
2988 case TYPE_CODE_METHOD:
2998 /* Call VAR's value_is_changeable_p language-specific callback. */
3001 varobj_value_is_changeable_p (struct varobj *var)
3003 return var->root->lang->value_is_changeable_p (var);
3006 /* Return 1 if that varobj is floating, that is is always evaluated in the
3007 selected frame, and not bound to thread/frame. Such variable objects
3008 are created using '@' as frame specifier to -var-create. */
3010 varobj_floating_p (struct varobj *var)
3012 return var->root->floating;
3015 /* Given the value and the type of a variable object,
3016 adjust the value and type to those necessary
3017 for getting children of the variable object.
3018 This includes dereferencing top-level references
3019 to all types and dereferencing pointers to
3022 If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
3023 value will be fetched and if it differs from static type
3024 the value will be casted to it.
3026 Both TYPE and *TYPE should be non-null. VALUE
3027 can be null if we want to only translate type.
3028 *VALUE can be null as well -- if the parent
3031 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
3032 depending on whether pointer was dereferenced
3033 in this function. */
3035 adjust_value_for_child_access (struct value **value,
3038 int lookup_actual_type)
3040 gdb_assert (type && *type);
3045 *type = check_typedef (*type);
3047 /* The type of value stored in varobj, that is passed
3048 to us, is already supposed to be
3049 reference-stripped. */
3051 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
3053 /* Pointers to structures are treated just like
3054 structures when accessing children. Don't
3055 dererences pointers to other types. */
3056 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
3058 struct type *target_type = get_target_type (*type);
3059 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
3060 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
3062 if (value && *value)
3064 volatile struct gdb_exception except;
3066 TRY_CATCH (except, RETURN_MASK_ERROR)
3068 *value = value_ind (*value);
3071 if (except.reason < 0)
3074 *type = target_type;
3080 /* The 'get_target_type' function calls check_typedef on
3081 result, so we can immediately check type code. No
3082 need to call check_typedef here. */
3084 /* Access a real type of the value (if necessary and possible). */
3085 if (value && *value && lookup_actual_type)
3087 struct type *enclosing_type;
3088 int real_type_found = 0;
3090 enclosing_type = value_actual_type (*value, 1, &real_type_found);
3091 if (real_type_found)
3093 *type = enclosing_type;
3094 *value = value_cast (enclosing_type, *value);
3099 /* Implement the "value_is_changeable_p" varobj callback for most
3103 default_value_is_changeable_p (struct varobj *var)
3108 if (CPLUS_FAKE_CHILD (var))
3111 type = get_value_type (var);
3113 switch (TYPE_CODE (type))
3115 case TYPE_CODE_STRUCT:
3116 case TYPE_CODE_UNION:
3117 case TYPE_CODE_ARRAY:
3131 c_number_of_children (struct varobj *var)
3133 struct type *type = get_value_type (var);
3135 struct type *target;
3137 adjust_value_for_child_access (NULL, &type, NULL, 0);
3138 target = get_target_type (type);
3140 switch (TYPE_CODE (type))
3142 case TYPE_CODE_ARRAY:
3143 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
3144 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
3145 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
3147 /* If we don't know how many elements there are, don't display
3152 case TYPE_CODE_STRUCT:
3153 case TYPE_CODE_UNION:
3154 children = TYPE_NFIELDS (type);
3158 /* The type here is a pointer to non-struct. Typically, pointers
3159 have one child, except for function ptrs, which have no children,
3160 and except for void*, as we don't know what to show.
3162 We can show char* so we allow it to be dereferenced. If you decide
3163 to test for it, please mind that a little magic is necessary to
3164 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
3165 TYPE_NAME == "char". */
3166 if (TYPE_CODE (target) == TYPE_CODE_FUNC
3167 || TYPE_CODE (target) == TYPE_CODE_VOID)
3174 /* Other types have no children. */
3182 c_name_of_variable (struct varobj *parent)
3184 return xstrdup (parent->name);
3187 /* Return the value of element TYPE_INDEX of a structure
3188 value VALUE. VALUE's type should be a structure,
3189 or union, or a typedef to struct/union.
3191 Returns NULL if getting the value fails. Never throws. */
3192 static struct value *
3193 value_struct_element_index (struct value *value, int type_index)
3195 struct value *result = NULL;
3196 volatile struct gdb_exception e;
3197 struct type *type = value_type (value);
3199 type = check_typedef (type);
3201 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
3202 || TYPE_CODE (type) == TYPE_CODE_UNION);
3204 TRY_CATCH (e, RETURN_MASK_ERROR)
3206 if (field_is_static (&TYPE_FIELD (type, type_index)))
3207 result = value_static_field (type, type_index);
3209 result = value_primitive_field (value, 0, type_index, type);
3221 /* Obtain the information about child INDEX of the variable
3223 If CNAME is not null, sets *CNAME to the name of the child relative
3225 If CVALUE is not null, sets *CVALUE to the value of the child.
3226 If CTYPE is not null, sets *CTYPE to the type of the child.
3228 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
3229 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
3232 c_describe_child (struct varobj *parent, int index,
3233 char **cname, struct value **cvalue, struct type **ctype,
3234 char **cfull_expression)
3236 struct value *value = parent->value;
3237 struct type *type = get_value_type (parent);
3238 char *parent_expression = NULL;
3240 volatile struct gdb_exception except;
3248 if (cfull_expression)
3250 *cfull_expression = NULL;
3251 parent_expression = varobj_get_path_expr (get_path_expr_parent (parent));
3253 adjust_value_for_child_access (&value, &type, &was_ptr, 0);
3255 switch (TYPE_CODE (type))
3257 case TYPE_CODE_ARRAY:
3260 = xstrdup (int_string (index
3261 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3264 if (cvalue && value)
3266 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
3268 TRY_CATCH (except, RETURN_MASK_ERROR)
3270 *cvalue = value_subscript (value, real_index);
3275 *ctype = get_target_type (type);
3277 if (cfull_expression)
3279 xstrprintf ("(%s)[%s]", parent_expression,
3281 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3287 case TYPE_CODE_STRUCT:
3288 case TYPE_CODE_UNION:
3290 const char *field_name;
3292 /* If the type is anonymous and the field has no name,
3293 set an appropriate name. */
3294 field_name = TYPE_FIELD_NAME (type, index);
3295 if (field_name == NULL || *field_name == '\0')
3299 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
3300 == TYPE_CODE_STRUCT)
3301 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3303 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3306 if (cfull_expression)
3307 *cfull_expression = xstrdup ("");
3312 *cname = xstrdup (field_name);
3314 if (cfull_expression)
3316 char *join = was_ptr ? "->" : ".";
3318 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
3323 if (cvalue && value)
3325 /* For C, varobj index is the same as type index. */
3326 *cvalue = value_struct_element_index (value, index);
3330 *ctype = TYPE_FIELD_TYPE (type, index);
3336 *cname = xstrprintf ("*%s", parent->name);
3338 if (cvalue && value)
3340 TRY_CATCH (except, RETURN_MASK_ERROR)
3342 *cvalue = value_ind (value);
3345 if (except.reason < 0)
3349 /* Don't use get_target_type because it calls
3350 check_typedef and here, we want to show the true
3351 declared type of the variable. */
3353 *ctype = TYPE_TARGET_TYPE (type);
3355 if (cfull_expression)
3356 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
3361 /* This should not happen. */
3363 *cname = xstrdup ("???");
3364 if (cfull_expression)
3365 *cfull_expression = xstrdup ("???");
3366 /* Don't set value and type, we don't know then. */
3371 c_name_of_child (struct varobj *parent, int index)
3375 c_describe_child (parent, index, &name, NULL, NULL, NULL);
3380 c_path_expr_of_child (struct varobj *child)
3382 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
3384 return child->path_expr;
3387 static struct value *
3388 c_value_of_child (struct varobj *parent, int index)
3390 struct value *value = NULL;
3392 c_describe_child (parent, index, NULL, &value, NULL, NULL);
3396 static struct type *
3397 c_type_of_child (struct varobj *parent, int index)
3399 struct type *type = NULL;
3401 c_describe_child (parent, index, NULL, NULL, &type, NULL);
3406 c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3408 /* BOGUS: if val_print sees a struct/class, or a reference to one,
3409 it will print out its children instead of "{...}". So we need to
3410 catch that case explicitly. */
3411 struct type *type = get_type (var);
3413 /* Strip top-level references. */
3414 while (TYPE_CODE (type) == TYPE_CODE_REF)
3415 type = check_typedef (TYPE_TARGET_TYPE (type));
3417 switch (TYPE_CODE (type))
3419 case TYPE_CODE_STRUCT:
3420 case TYPE_CODE_UNION:
3421 return xstrdup ("{...}");
3424 case TYPE_CODE_ARRAY:
3428 number = xstrprintf ("[%d]", var->num_children);
3435 if (var->value == NULL)
3437 /* This can happen if we attempt to get the value of a struct
3438 member when the parent is an invalid pointer. This is an
3439 error condition, so we should tell the caller. */
3444 if (var->not_fetched && value_lazy (var->value))
3445 /* Frozen variable and no value yet. We don't
3446 implicitly fetch the value. MI response will
3447 use empty string for the value, which is OK. */
3450 gdb_assert (varobj_value_is_changeable_p (var));
3451 gdb_assert (!value_lazy (var->value));
3453 /* If the specified format is the current one,
3454 we can reuse print_value. */
3455 if (format == var->format)
3456 return xstrdup (var->print_value);
3458 return value_get_print_value (var->value, format, var);
3468 cplus_number_of_children (struct varobj *var)
3470 struct value *value = NULL;
3472 int children, dont_know;
3473 int lookup_actual_type = 0;
3474 struct value_print_options opts;
3479 get_user_print_options (&opts);
3481 if (!CPLUS_FAKE_CHILD (var))
3483 type = get_value_type (var);
3485 /* It is necessary to access a real type (via RTTI). */
3486 if (opts.objectprint)
3489 lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
3490 || TYPE_CODE (var->type) == TYPE_CODE_PTR);
3492 adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
3494 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
3495 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
3499 cplus_class_num_children (type, kids);
3500 if (kids[v_public] != 0)
3502 if (kids[v_private] != 0)
3504 if (kids[v_protected] != 0)
3507 /* Add any baseclasses. */
3508 children += TYPE_N_BASECLASSES (type);
3511 /* FIXME: save children in var. */
3518 type = get_value_type (var->parent);
3520 /* It is necessary to access a real type (via RTTI). */
3521 if (opts.objectprint)
3523 struct varobj *parent = var->parent;
3525 value = parent->value;
3526 lookup_actual_type = (TYPE_CODE (parent->type) == TYPE_CODE_REF
3527 || TYPE_CODE (parent->type) == TYPE_CODE_PTR);
3529 adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
3531 cplus_class_num_children (type, kids);
3532 if (strcmp (var->name, "public") == 0)
3533 children = kids[v_public];
3534 else if (strcmp (var->name, "private") == 0)
3535 children = kids[v_private];
3537 children = kids[v_protected];
3542 children = c_number_of_children (var);
3547 /* Compute # of public, private, and protected variables in this class.
3548 That means we need to descend into all baseclasses and find out
3549 how many are there, too. */
3551 cplus_class_num_children (struct type *type, int children[3])
3553 int i, vptr_fieldno;
3554 struct type *basetype = NULL;
3556 children[v_public] = 0;
3557 children[v_private] = 0;
3558 children[v_protected] = 0;
3560 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3561 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
3563 /* If we have a virtual table pointer, omit it. Even if virtual
3564 table pointers are not specifically marked in the debug info,
3565 they should be artificial. */
3566 if ((type == basetype && i == vptr_fieldno)
3567 || TYPE_FIELD_ARTIFICIAL (type, i))
3570 if (TYPE_FIELD_PROTECTED (type, i))
3571 children[v_protected]++;
3572 else if (TYPE_FIELD_PRIVATE (type, i))
3573 children[v_private]++;
3575 children[v_public]++;
3580 cplus_name_of_variable (struct varobj *parent)
3582 return c_name_of_variable (parent);
3585 enum accessibility { private_field, protected_field, public_field };
3587 /* Check if field INDEX of TYPE has the specified accessibility.
3588 Return 0 if so and 1 otherwise. */
3590 match_accessibility (struct type *type, int index, enum accessibility acc)
3592 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
3594 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
3596 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
3597 && !TYPE_FIELD_PROTECTED (type, index))
3604 cplus_describe_child (struct varobj *parent, int index,
3605 char **cname, struct value **cvalue, struct type **ctype,
3606 char **cfull_expression)
3608 struct value *value;
3611 int lookup_actual_type = 0;
3612 char *parent_expression = NULL;
3614 struct value_print_options opts;
3622 if (cfull_expression)
3623 *cfull_expression = NULL;
3625 get_user_print_options (&opts);
3627 var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent;
3628 if (opts.objectprint)
3629 lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
3630 || TYPE_CODE (var->type) == TYPE_CODE_PTR);
3632 type = get_value_type (var);
3633 if (cfull_expression)
3634 parent_expression = varobj_get_path_expr (get_path_expr_parent (var));
3636 adjust_value_for_child_access (&value, &type, &was_ptr, lookup_actual_type);
3638 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3639 || TYPE_CODE (type) == TYPE_CODE_UNION)
3641 char *join = was_ptr ? "->" : ".";
3643 if (CPLUS_FAKE_CHILD (parent))
3645 /* The fields of the class type are ordered as they
3646 appear in the class. We are given an index for a
3647 particular access control type ("public","protected",
3648 or "private"). We must skip over fields that don't
3649 have the access control we are looking for to properly
3650 find the indexed field. */
3651 int type_index = TYPE_N_BASECLASSES (type);
3652 enum accessibility acc = public_field;
3654 struct type *basetype = NULL;
3655 const char *field_name;
3657 vptr_fieldno = get_vptr_fieldno (type, &basetype);
3658 if (strcmp (parent->name, "private") == 0)
3659 acc = private_field;
3660 else if (strcmp (parent->name, "protected") == 0)
3661 acc = protected_field;
3665 if ((type == basetype && type_index == vptr_fieldno)
3666 || TYPE_FIELD_ARTIFICIAL (type, type_index))
3668 else if (match_accessibility (type, type_index, acc))
3674 /* If the type is anonymous and the field has no name,
3675 set an appopriate name. */
3676 field_name = TYPE_FIELD_NAME (type, type_index);
3677 if (field_name == NULL || *field_name == '\0')
3681 if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3682 == TYPE_CODE_STRUCT)
3683 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3684 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3686 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3689 if (cfull_expression)
3690 *cfull_expression = xstrdup ("");
3695 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
3697 if (cfull_expression)
3699 = xstrprintf ("((%s)%s%s)", parent_expression, join,
3703 if (cvalue && value)
3704 *cvalue = value_struct_element_index (value, type_index);
3707 *ctype = TYPE_FIELD_TYPE (type, type_index);
3709 else if (index < TYPE_N_BASECLASSES (type))
3711 /* This is a baseclass. */
3713 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
3715 if (cvalue && value)
3716 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
3720 *ctype = TYPE_FIELD_TYPE (type, index);
3723 if (cfull_expression)
3725 char *ptr = was_ptr ? "*" : "";
3727 /* Cast the parent to the base' type. Note that in gdb,
3730 will create an lvalue, for all appearences, so we don't
3731 need to use more fancy:
3735 When we are in the scope of the base class or of one
3736 of its children, the type field name will be interpreted
3737 as a constructor, if it exists. Therefore, we must
3738 indicate that the name is a class name by using the
3739 'class' keyword. See PR mi/11912 */
3740 *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
3742 TYPE_FIELD_NAME (type, index),
3749 char *access = NULL;
3752 cplus_class_num_children (type, children);
3754 /* Everything beyond the baseclasses can
3755 only be "public", "private", or "protected"
3757 The special "fake" children are always output by varobj in
3758 this order. So if INDEX == 2, it MUST be "protected". */
3759 index -= TYPE_N_BASECLASSES (type);
3763 if (children[v_public] > 0)
3765 else if (children[v_private] > 0)
3768 access = "protected";
3771 if (children[v_public] > 0)
3773 if (children[v_private] > 0)
3776 access = "protected";
3778 else if (children[v_private] > 0)
3779 access = "protected";
3782 /* Must be protected. */
3783 access = "protected";
3790 gdb_assert (access);
3792 *cname = xstrdup (access);
3794 /* Value and type and full expression are null here. */
3799 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
3804 cplus_name_of_child (struct varobj *parent, int index)
3808 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
3813 cplus_path_expr_of_child (struct varobj *child)
3815 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
3817 return child->path_expr;
3820 static struct value *
3821 cplus_value_of_child (struct varobj *parent, int index)
3823 struct value *value = NULL;
3825 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
3829 static struct type *
3830 cplus_type_of_child (struct varobj *parent, int index)
3832 struct type *type = NULL;
3834 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
3839 cplus_value_of_variable (struct varobj *var,
3840 enum varobj_display_formats format)
3843 /* If we have one of our special types, don't print out
3845 if (CPLUS_FAKE_CHILD (var))
3846 return xstrdup ("");
3848 return c_value_of_variable (var, format);
3854 java_number_of_children (struct varobj *var)
3856 return cplus_number_of_children (var);
3860 java_name_of_variable (struct varobj *parent)
3864 name = cplus_name_of_variable (parent);
3865 /* If the name has "-" in it, it is because we
3866 needed to escape periods in the name... */
3869 while (*p != '\000')
3880 java_name_of_child (struct varobj *parent, int index)
3884 name = cplus_name_of_child (parent, index);
3885 /* Escape any periods in the name... */
3888 while (*p != '\000')
3899 java_path_expr_of_child (struct varobj *child)
3904 static struct value *
3905 java_value_of_child (struct varobj *parent, int index)
3907 return cplus_value_of_child (parent, index);
3910 static struct type *
3911 java_type_of_child (struct varobj *parent, int index)
3913 return cplus_type_of_child (parent, index);
3917 java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3919 return cplus_value_of_variable (var, format);
3922 /* Ada specific callbacks for VAROBJs. */
3925 ada_number_of_children (struct varobj *var)
3927 return ada_varobj_get_number_of_children (var->value, var->type);
3931 ada_name_of_variable (struct varobj *parent)
3933 return c_name_of_variable (parent);
3937 ada_name_of_child (struct varobj *parent, int index)
3939 return ada_varobj_get_name_of_child (parent->value, parent->type,
3940 parent->name, index);
3944 ada_path_expr_of_child (struct varobj *child)
3946 struct varobj *parent = child->parent;
3947 const char *parent_path_expr = varobj_get_path_expr (parent);
3949 return ada_varobj_get_path_expr_of_child (parent->value,
3956 static struct value *
3957 ada_value_of_child (struct varobj *parent, int index)
3959 return ada_varobj_get_value_of_child (parent->value, parent->type,
3960 parent->name, index);
3963 static struct type *
3964 ada_type_of_child (struct varobj *parent, int index)
3966 return ada_varobj_get_type_of_child (parent->value, parent->type,
3971 ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
3973 struct value_print_options opts;
3975 get_formatted_print_options (&opts, format_code[(int) format]);
3979 return ada_varobj_get_value_of_variable (var->value, var->type, &opts);
3982 /* Implement the "value_is_changeable_p" routine for Ada. */
3985 ada_value_is_changeable_p (struct varobj *var)
3987 struct type *type = var->value ? value_type (var->value) : var->type;
3989 if (ada_is_array_descriptor_type (type)
3990 && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
3992 /* This is in reality a pointer to an unconstrained array.
3993 its value is changeable. */
3997 if (ada_is_string_type (type))
3999 /* We display the contents of the string in the array's
4000 "value" field. The contents can change, so consider
4001 that the array is changeable. */
4005 return default_value_is_changeable_p (var);
4008 /* Implement the "value_has_mutated" routine for Ada. */
4011 ada_value_has_mutated (struct varobj *var, struct value *new_val,
4012 struct type *new_type)
4018 /* If the number of fields have changed, then for sure the type
4020 if (ada_varobj_get_number_of_children (new_val, new_type)
4021 != var->num_children)
4024 /* If the number of fields have remained the same, then we need
4025 to check the name of each field. If they remain the same,
4026 then chances are the type hasn't mutated. This is technically
4027 an incomplete test, as the child's type might have changed
4028 despite the fact that the name remains the same. But we'll
4029 handle this situation by saying that the child has mutated,
4032 If only part (or none!) of the children have been fetched,
4033 then only check the ones we fetched. It does not matter
4034 to the frontend whether a child that it has not fetched yet
4035 has mutated or not. So just assume it hasn't. */
4037 restrict_range (var->children, &from, &to);
4038 for (i = from; i < to; i++)
4039 if (strcmp (ada_varobj_get_name_of_child (new_val, new_type,
4041 VEC_index (varobj_p, var->children, i)->name) != 0)
4047 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
4048 with an arbitrary caller supplied DATA pointer. */
4051 all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
4053 struct varobj_root *var_root, *var_root_next;
4055 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
4057 for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
4059 var_root_next = var_root->next;
4061 (*func) (var_root->rootvar, data);
4065 extern void _initialize_varobj (void);
4067 _initialize_varobj (void)
4069 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
4071 varobj_table = xmalloc (sizeof_table);
4072 memset (varobj_table, 0, sizeof_table);
4074 add_setshow_zuinteger_cmd ("debugvarobj", class_maintenance,
4076 _("Set varobj debugging."),
4077 _("Show varobj debugging."),
4078 _("When non-zero, varobj debugging is enabled."),
4079 NULL, show_varobjdebug,
4080 &setlist, &showlist);
4083 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
4084 defined on globals. It is a helper for varobj_invalidate.
4086 This function is called after changing the symbol file, in this case the
4087 pointers to "struct type" stored by the varobj are no longer valid. All
4088 varobj must be either re-evaluated, or marked as invalid here. */
4091 varobj_invalidate_iter (struct varobj *var, void *unused)
4093 /* global and floating var must be re-evaluated. */
4094 if (var->root->floating || var->root->valid_block == NULL)
4096 struct varobj *tmp_var;
4098 /* Try to create a varobj with same expression. If we succeed
4099 replace the old varobj, otherwise invalidate it. */
4100 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
4102 if (tmp_var != NULL)
4104 tmp_var->obj_name = xstrdup (var->obj_name);
4105 varobj_delete (var, NULL, 0);
4106 install_variable (tmp_var);
4109 var->root->is_valid = 0;
4111 else /* locals must be invalidated. */
4112 var->root->is_valid = 0;
4115 /* Invalidate the varobjs that are tied to locals and re-create the ones that
4116 are defined on globals.
4117 Invalidated varobjs will be always printed in_scope="invalid". */
4120 varobj_invalidate (void)
4122 all_root_varobjs (varobj_invalidate_iter, NULL);