1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2014 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"
30 #include "gdb_regex.h"
34 #include "gdbthread.h"
38 #include "python/python.h"
39 #include "python/python-internal.h"
44 #include "varobj-iter.h"
46 /* Non-zero if we want to see trace of varobj level stuff. */
48 unsigned int varobjdebug
= 0;
50 show_varobjdebug (struct ui_file
*file
, int from_tty
,
51 struct cmd_list_element
*c
, const char *value
)
53 fprintf_filtered (file
, _("Varobj debugging is %s.\n"), value
);
56 /* String representations of gdb's format codes. */
57 char *varobj_format_string
[] =
58 { "natural", "binary", "decimal", "hexadecimal", "octal" };
60 /* True if we want to allow Python-based pretty-printing. */
61 static int pretty_printing
= 0;
64 varobj_enable_pretty_printing (void)
71 /* Every root variable has one of these structures saved in its
72 varobj. Members which must be free'd are noted. */
76 /* Alloc'd expression for this parent. */
77 struct expression
*exp
;
79 /* Block for which this expression is valid. */
80 const struct block
*valid_block
;
82 /* The frame for this expression. This field is set iff valid_block is
84 struct frame_id frame
;
86 /* The thread ID that this varobj_root belong to. This field
87 is only valid if valid_block is not NULL.
88 When not 0, indicates which thread 'frame' belongs to.
89 When 0, indicates that the thread list was empty when the varobj_root
93 /* If 1, the -var-update always recomputes the value in the
94 current thread and frame. Otherwise, variable object is
95 always updated in the specific scope/thread/frame. */
98 /* Flag that indicates validity: set to 0 when this varobj_root refers
99 to symbols that do not exist anymore. */
102 /* Language-related operations for this variable and its
104 const struct lang_varobj_ops
*lang_ops
;
106 /* The varobj for this root node. */
107 struct varobj
*rootvar
;
109 /* Next root variable */
110 struct varobj_root
*next
;
113 /* A node or item of varobj, composed of the name and the value. */
117 /* Name of this item. */
120 /* Value of this item. */
124 /* Dynamic part of varobj. */
126 struct varobj_dynamic
128 /* Whether the children of this varobj were requested. This field is
129 used to decide if dynamic varobj should recompute their children.
130 In the event that the frontend never asked for the children, we
132 int children_requested
;
134 /* The pretty-printer constructor. If NULL, then the default
135 pretty-printer will be looked up. If None, then no
136 pretty-printer will be installed. */
137 PyObject
*constructor
;
139 /* The pretty-printer that has been constructed. If NULL, then a
140 new printer object is needed, and one will be constructed. */
141 PyObject
*pretty_printer
;
143 /* The iterator returned by the printer's 'children' method, or NULL
145 struct varobj_iter
*child_iter
;
147 /* We request one extra item from the iterator, so that we can
148 report to the caller whether there are more items than we have
149 already reported. However, we don't want to install this value
150 when we read it, because that will mess up future updates. So,
151 we stash it here instead. */
152 varobj_item
*saved_item
;
158 struct cpstack
*next
;
161 /* A list of varobjs */
169 /* Private function prototypes */
171 /* Helper functions for the above subcommands. */
173 static int delete_variable (struct cpstack
**, struct varobj
*, int);
175 static void delete_variable_1 (struct cpstack
**, int *,
176 struct varobj
*, int, int);
178 static int install_variable (struct varobj
*);
180 static void uninstall_variable (struct varobj
*);
182 static struct varobj
*create_child (struct varobj
*, int, char *);
184 static struct varobj
*
185 create_child_with_value (struct varobj
*parent
, int index
,
186 struct varobj_item
*item
);
188 /* Utility routines */
190 static struct varobj
*new_variable (void);
192 static struct varobj
*new_root_variable (void);
194 static void free_variable (struct varobj
*var
);
196 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
198 static enum varobj_display_formats
variable_default_display (struct varobj
*);
200 static void cppush (struct cpstack
**pstack
, char *name
);
202 static char *cppop (struct cpstack
**pstack
);
204 static int update_type_if_necessary (struct varobj
*var
,
205 struct value
*new_value
);
207 static int install_new_value (struct varobj
*var
, struct value
*value
,
210 /* Language-specific routines. */
212 static int number_of_children (struct varobj
*);
214 static char *name_of_variable (struct varobj
*);
216 static char *name_of_child (struct varobj
*, int);
218 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
220 static struct value
*value_of_child (struct varobj
*parent
, int index
);
222 static char *my_value_of_variable (struct varobj
*var
,
223 enum varobj_display_formats format
);
225 static int is_root_p (struct varobj
*var
);
229 static struct varobj
*varobj_add_child (struct varobj
*var
,
230 struct varobj_item
*item
);
232 #endif /* HAVE_PYTHON */
236 /* Mappings of varobj_display_formats enums to gdb's format codes. */
237 static int format_code
[] = { 0, 't', 'd', 'x', 'o' };
239 /* Header of the list of root variable objects. */
240 static struct varobj_root
*rootlist
;
242 /* Prime number indicating the number of buckets in the hash table. */
243 /* A prime large enough to avoid too many colisions. */
244 #define VAROBJ_TABLE_SIZE 227
246 /* Pointer to the varobj hash table (built at run time). */
247 static struct vlist
**varobj_table
;
251 /* API Implementation */
253 is_root_p (struct varobj
*var
)
255 return (var
->root
->rootvar
== var
);
259 /* Helper function to install a Python environment suitable for
260 use during operations on VAR. */
262 varobj_ensure_python_env (struct varobj
*var
)
264 return ensure_python_env (var
->root
->exp
->gdbarch
,
265 var
->root
->exp
->language_defn
);
269 /* Creates a varobj (not its children). */
271 /* Return the full FRAME which corresponds to the given CORE_ADDR
272 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
274 static struct frame_info
*
275 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
277 struct frame_info
*frame
= NULL
;
279 if (frame_addr
== (CORE_ADDR
) 0)
282 for (frame
= get_current_frame ();
284 frame
= get_prev_frame (frame
))
286 /* The CORE_ADDR we get as argument was parsed from a string GDB
287 output as $fp. This output got truncated to gdbarch_addr_bit.
288 Truncate the frame base address in the same manner before
289 comparing it against our argument. */
290 CORE_ADDR frame_base
= get_frame_base_address (frame
);
291 int addr_bit
= gdbarch_addr_bit (get_frame_arch (frame
));
293 if (addr_bit
< (sizeof (CORE_ADDR
) * HOST_CHAR_BIT
))
294 frame_base
&= ((CORE_ADDR
) 1 << addr_bit
) - 1;
296 if (frame_base
== frame_addr
)
304 varobj_create (char *objname
,
305 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
308 struct cleanup
*old_chain
;
310 /* Fill out a varobj structure for the (root) variable being constructed. */
311 var
= new_root_variable ();
312 old_chain
= make_cleanup_free_variable (var
);
314 if (expression
!= NULL
)
316 struct frame_info
*fi
;
317 struct frame_id old_id
= null_frame_id
;
320 struct value
*value
= NULL
;
321 volatile struct gdb_exception except
;
324 /* Parse and evaluate the expression, filling in as much of the
325 variable's data as possible. */
327 if (has_stack_frames ())
329 /* Allow creator to specify context of variable. */
330 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
331 fi
= get_selected_frame (NULL
);
333 /* FIXME: cagney/2002-11-23: This code should be doing a
334 lookup using the frame ID and not just the frame's
335 ``address''. This, of course, means an interface
336 change. However, with out that interface change ISAs,
337 such as the ia64 with its two stacks, won't work.
338 Similar goes for the case where there is a frameless
340 fi
= find_frame_addr_in_frame_chain (frame
);
345 /* frame = -2 means always use selected frame. */
346 if (type
== USE_SELECTED_FRAME
)
347 var
->root
->floating
= 1;
353 block
= get_frame_block (fi
, 0);
354 pc
= get_frame_pc (fi
);
358 innermost_block
= NULL
;
359 /* Wrap the call to parse expression, so we can
360 return a sensible error. */
361 TRY_CATCH (except
, RETURN_MASK_ERROR
)
363 var
->root
->exp
= parse_exp_1 (&p
, pc
, block
, 0);
366 if (except
.reason
< 0)
368 do_cleanups (old_chain
);
372 /* Don't allow variables to be created for types. */
373 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
374 || var
->root
->exp
->elts
[0].opcode
== OP_TYPEOF
375 || var
->root
->exp
->elts
[0].opcode
== OP_DECLTYPE
)
377 do_cleanups (old_chain
);
378 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
379 " as an expression.\n");
383 var
->format
= variable_default_display (var
);
384 var
->root
->valid_block
= innermost_block
;
385 var
->name
= xstrdup (expression
);
386 /* For a root var, the name and the expr are the same. */
387 var
->path_expr
= xstrdup (expression
);
389 /* When the frame is different from the current frame,
390 we must select the appropriate frame before parsing
391 the expression, otherwise the value will not be current.
392 Since select_frame is so benign, just call it for all cases. */
395 /* User could specify explicit FRAME-ADDR which was not found but
396 EXPRESSION is frame specific and we would not be able to evaluate
397 it correctly next time. With VALID_BLOCK set we must also set
398 FRAME and THREAD_ID. */
400 error (_("Failed to find the specified frame"));
402 var
->root
->frame
= get_frame_id (fi
);
403 var
->root
->thread_id
= pid_to_thread_id (inferior_ptid
);
404 old_id
= get_frame_id (get_selected_frame (NULL
));
408 /* We definitely need to catch errors here.
409 If evaluate_expression succeeds we got the value we wanted.
410 But if it fails, we still go on with a call to evaluate_type(). */
411 TRY_CATCH (except
, RETURN_MASK_ERROR
)
413 value
= evaluate_expression (var
->root
->exp
);
416 if (except
.reason
< 0)
418 /* Error getting the value. Try to at least get the
420 struct value
*type_only_value
= evaluate_type (var
->root
->exp
);
422 var
->type
= value_type (type_only_value
);
426 int real_type_found
= 0;
428 var
->type
= value_actual_type (value
, 0, &real_type_found
);
430 value
= value_cast (var
->type
, value
);
433 /* Set language info */
434 var
->root
->lang_ops
= var
->root
->exp
->language_defn
->la_varobj_ops
;
436 install_new_value (var
, value
, 1 /* Initial assignment */);
438 /* Set ourselves as our root. */
439 var
->root
->rootvar
= var
;
441 /* Reset the selected frame. */
442 if (frame_id_p (old_id
))
443 select_frame (frame_find_by_id (old_id
));
446 /* If the variable object name is null, that means this
447 is a temporary variable, so don't install it. */
449 if ((var
!= NULL
) && (objname
!= NULL
))
451 var
->obj_name
= xstrdup (objname
);
453 /* If a varobj name is duplicated, the install will fail so
455 if (!install_variable (var
))
457 do_cleanups (old_chain
);
462 discard_cleanups (old_chain
);
466 /* Generates an unique name that can be used for a varobj. */
469 varobj_gen_name (void)
474 /* Generate a name for this object. */
476 obj_name
= xstrprintf ("var%d", id
);
481 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
482 error if OBJNAME cannot be found. */
485 varobj_get_handle (char *objname
)
489 unsigned int index
= 0;
492 for (chp
= objname
; *chp
; chp
++)
494 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
497 cv
= *(varobj_table
+ index
);
498 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
502 error (_("Variable object not found"));
507 /* Given the handle, return the name of the object. */
510 varobj_get_objname (struct varobj
*var
)
512 return var
->obj_name
;
515 /* Given the handle, return the expression represented by the object. */
518 varobj_get_expression (struct varobj
*var
)
520 return name_of_variable (var
);
523 /* Deletes a varobj and all its children if only_children == 0,
524 otherwise deletes only the children; returns a malloc'ed list of
525 all the (malloc'ed) names of the variables that have been deleted
526 (NULL terminated). */
529 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
533 struct cpstack
*result
= NULL
;
536 /* Initialize a stack for temporary results. */
537 cppush (&result
, NULL
);
540 /* Delete only the variable children. */
541 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
543 /* Delete the variable and all its children. */
544 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
546 /* We may have been asked to return a list of what has been deleted. */
549 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
553 *cp
= cppop (&result
);
554 while ((*cp
!= NULL
) && (mycount
> 0))
558 *cp
= cppop (&result
);
561 if (mycount
|| (*cp
!= NULL
))
562 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
571 /* Convenience function for varobj_set_visualizer. Instantiate a
572 pretty-printer for a given value. */
574 instantiate_pretty_printer (PyObject
*constructor
, struct value
*value
)
576 PyObject
*val_obj
= NULL
;
579 val_obj
= value_to_value_object (value
);
583 printer
= PyObject_CallFunctionObjArgs (constructor
, val_obj
, NULL
);
590 /* Set/Get variable object display format. */
592 enum varobj_display_formats
593 varobj_set_display_format (struct varobj
*var
,
594 enum varobj_display_formats format
)
601 case FORMAT_HEXADECIMAL
:
603 var
->format
= format
;
607 var
->format
= variable_default_display (var
);
610 if (varobj_value_is_changeable_p (var
)
611 && var
->value
&& !value_lazy (var
->value
))
613 xfree (var
->print_value
);
614 var
->print_value
= varobj_value_get_print_value (var
->value
,
621 enum varobj_display_formats
622 varobj_get_display_format (struct varobj
*var
)
628 varobj_get_display_hint (struct varobj
*var
)
633 struct cleanup
*back_to
;
635 if (!gdb_python_initialized
)
638 back_to
= varobj_ensure_python_env (var
);
640 if (var
->dynamic
->pretty_printer
!= NULL
)
641 result
= gdbpy_get_display_hint (var
->dynamic
->pretty_printer
);
643 do_cleanups (back_to
);
649 /* Return true if the varobj has items after TO, false otherwise. */
652 varobj_has_more (struct varobj
*var
, int to
)
654 if (VEC_length (varobj_p
, var
->children
) > to
)
656 return ((to
== -1 || VEC_length (varobj_p
, var
->children
) == to
)
657 && (var
->dynamic
->saved_item
!= NULL
));
660 /* If the variable object is bound to a specific thread, that
661 is its evaluation can always be done in context of a frame
662 inside that thread, returns GDB id of the thread -- which
663 is always positive. Otherwise, returns -1. */
665 varobj_get_thread_id (struct varobj
*var
)
667 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
668 return var
->root
->thread_id
;
674 varobj_set_frozen (struct varobj
*var
, int frozen
)
676 /* When a variable is unfrozen, we don't fetch its value.
677 The 'not_fetched' flag remains set, so next -var-update
680 We don't fetch the value, because for structures the client
681 should do -var-update anyway. It would be bad to have different
682 client-size logic for structure and other types. */
683 var
->frozen
= frozen
;
687 varobj_get_frozen (struct varobj
*var
)
692 /* A helper function that restricts a range to what is actually
693 available in a VEC. This follows the usual rules for the meaning
694 of FROM and TO -- if either is negative, the entire range is
698 varobj_restrict_range (VEC (varobj_p
) *children
, int *from
, int *to
)
700 if (*from
< 0 || *to
< 0)
703 *to
= VEC_length (varobj_p
, children
);
707 if (*from
> VEC_length (varobj_p
, children
))
708 *from
= VEC_length (varobj_p
, children
);
709 if (*to
> VEC_length (varobj_p
, children
))
710 *to
= VEC_length (varobj_p
, children
);
718 /* A helper for update_dynamic_varobj_children that installs a new
719 child when needed. */
722 install_dynamic_child (struct varobj
*var
,
723 VEC (varobj_p
) **changed
,
724 VEC (varobj_p
) **type_changed
,
725 VEC (varobj_p
) **new,
726 VEC (varobj_p
) **unchanged
,
729 struct varobj_item
*item
)
731 if (VEC_length (varobj_p
, var
->children
) < index
+ 1)
733 /* There's no child yet. */
734 struct varobj
*child
= varobj_add_child (var
, item
);
738 VEC_safe_push (varobj_p
, *new, child
);
744 varobj_p existing
= VEC_index (varobj_p
, var
->children
, index
);
745 int type_updated
= update_type_if_necessary (existing
, item
->value
);
750 VEC_safe_push (varobj_p
, *type_changed
, existing
);
752 if (install_new_value (existing
, item
->value
, 0))
754 if (!type_updated
&& changed
)
755 VEC_safe_push (varobj_p
, *changed
, existing
);
757 else if (!type_updated
&& unchanged
)
758 VEC_safe_push (varobj_p
, *unchanged
, existing
);
763 dynamic_varobj_has_child_method (struct varobj
*var
)
765 struct cleanup
*back_to
;
766 PyObject
*printer
= var
->dynamic
->pretty_printer
;
769 if (!gdb_python_initialized
)
772 back_to
= varobj_ensure_python_env (var
);
773 result
= PyObject_HasAttr (printer
, gdbpy_children_cst
);
774 do_cleanups (back_to
);
778 /* A factory for creating dynamic varobj's iterators. Returns an
779 iterator object suitable for iterating over VAR's children. */
781 static struct varobj_iter
*
782 varobj_get_iterator (struct varobj
*var
)
784 if (var
->dynamic
->pretty_printer
)
785 return py_varobj_get_iterator (var
, var
->dynamic
->pretty_printer
);
787 gdb_assert_not_reached (_("\
788 requested an iterator from a non-dynamic varobj"));
794 update_dynamic_varobj_children (struct varobj
*var
,
795 VEC (varobj_p
) **changed
,
796 VEC (varobj_p
) **type_changed
,
797 VEC (varobj_p
) **new,
798 VEC (varobj_p
) **unchanged
,
805 struct cleanup
*back_to
;
808 if (!gdb_python_initialized
)
811 back_to
= varobj_ensure_python_env (var
);
815 if (update_children
|| var
->dynamic
->child_iter
== NULL
)
817 varobj_iter_delete (var
->dynamic
->child_iter
);
818 var
->dynamic
->child_iter
= varobj_get_iterator (var
);
820 Py_XDECREF (var
->dynamic
->saved_item
);
821 var
->dynamic
->saved_item
= NULL
;
825 if (var
->dynamic
->child_iter
== NULL
)
827 do_cleanups (back_to
);
832 i
= VEC_length (varobj_p
, var
->children
);
834 /* We ask for one extra child, so that MI can report whether there
835 are more children. */
836 for (; to
< 0 || i
< to
+ 1; ++i
)
840 /* See if there was a leftover from last time. */
841 if (var
->dynamic
->saved_item
)
843 item
= var
->dynamic
->saved_item
;
844 var
->dynamic
->saved_item
= NULL
;
848 item
= varobj_iter_next (var
->dynamic
->child_iter
);
853 /* Iteration is done. Remove iterator from VAR. */
854 varobj_iter_delete (var
->dynamic
->child_iter
);
855 var
->dynamic
->child_iter
= NULL
;
858 /* We don't want to push the extra child on any report list. */
859 if (to
< 0 || i
< to
)
863 struct varobj_item varobj_item
;
864 struct cleanup
*inner
;
865 int can_mention
= from
< 0 || i
>= from
;
867 inner
= make_cleanup_py_decref (item
);
869 if (!PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
871 gdbpy_print_stack ();
872 error (_("Invalid item from the child list"));
875 varobj_item
.value
= convert_value_from_python (py_v
);
876 if (varobj_item
.value
== NULL
)
877 gdbpy_print_stack ();
878 varobj_item
.name
= xstrdup (name
);
880 install_dynamic_child (var
, can_mention
? changed
: NULL
,
881 can_mention
? type_changed
: NULL
,
882 can_mention
? new : NULL
,
883 can_mention
? unchanged
: NULL
,
884 can_mention
? cchanged
: NULL
, i
,
890 Py_XDECREF (var
->dynamic
->saved_item
);
891 var
->dynamic
->saved_item
= item
;
893 /* We want to truncate the child list just before this
899 if (i
< VEC_length (varobj_p
, var
->children
))
904 for (j
= i
; j
< VEC_length (varobj_p
, var
->children
); ++j
)
905 varobj_delete (VEC_index (varobj_p
, var
->children
, j
), NULL
, 0);
906 VEC_truncate (varobj_p
, var
->children
, i
);
909 /* If there are fewer children than requested, note that the list of
911 if (to
>= 0 && VEC_length (varobj_p
, var
->children
) < to
)
914 var
->num_children
= VEC_length (varobj_p
, var
->children
);
916 do_cleanups (back_to
);
919 gdb_assert_not_reached ("should never be called if Python is not enabled");
924 varobj_get_num_children (struct varobj
*var
)
926 if (var
->num_children
== -1)
928 if (var
->dynamic
->pretty_printer
!= NULL
)
932 /* If we have a dynamic varobj, don't report -1 children.
933 So, try to fetch some children first. */
934 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
, &dummy
,
938 var
->num_children
= number_of_children (var
);
941 return var
->num_children
>= 0 ? var
->num_children
: 0;
944 /* Creates a list of the immediate children of a variable object;
945 the return code is the number of such children or -1 on error. */
948 varobj_list_children (struct varobj
*var
, int *from
, int *to
)
951 int i
, children_changed
;
953 var
->dynamic
->children_requested
= 1;
955 if (var
->dynamic
->pretty_printer
!= NULL
)
957 /* This, in theory, can result in the number of children changing without
958 frontend noticing. But well, calling -var-list-children on the same
959 varobj twice is not something a sane frontend would do. */
960 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
,
961 &children_changed
, 0, 0, *to
);
962 varobj_restrict_range (var
->children
, from
, to
);
963 return var
->children
;
966 if (var
->num_children
== -1)
967 var
->num_children
= number_of_children (var
);
969 /* If that failed, give up. */
970 if (var
->num_children
== -1)
971 return var
->children
;
973 /* If we're called when the list of children is not yet initialized,
974 allocate enough elements in it. */
975 while (VEC_length (varobj_p
, var
->children
) < var
->num_children
)
976 VEC_safe_push (varobj_p
, var
->children
, NULL
);
978 for (i
= 0; i
< var
->num_children
; i
++)
980 varobj_p existing
= VEC_index (varobj_p
, var
->children
, i
);
982 if (existing
== NULL
)
984 /* Either it's the first call to varobj_list_children for
985 this variable object, and the child was never created,
986 or it was explicitly deleted by the client. */
987 name
= name_of_child (var
, i
);
988 existing
= create_child (var
, i
, name
);
989 VEC_replace (varobj_p
, var
->children
, i
, existing
);
993 varobj_restrict_range (var
->children
, from
, to
);
994 return var
->children
;
999 static struct varobj
*
1000 varobj_add_child (struct varobj
*var
, struct varobj_item
*item
)
1002 varobj_p v
= create_child_with_value (var
,
1003 VEC_length (varobj_p
, var
->children
),
1006 VEC_safe_push (varobj_p
, var
->children
, v
);
1010 #endif /* HAVE_PYTHON */
1012 /* Obtain the type of an object Variable as a string similar to the one gdb
1013 prints on the console. */
1016 varobj_get_type (struct varobj
*var
)
1018 /* For the "fake" variables, do not return a type. (Its type is
1020 Do not return a type for invalid variables as well. */
1021 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
1024 return type_to_string (var
->type
);
1027 /* Obtain the type of an object variable. */
1030 varobj_get_gdb_type (struct varobj
*var
)
1035 /* Is VAR a path expression parent, i.e., can it be used to construct
1036 a valid path expression? */
1039 is_path_expr_parent (struct varobj
*var
)
1043 /* "Fake" children are not path_expr parents. */
1044 if (CPLUS_FAKE_CHILD (var
))
1047 type
= varobj_get_value_type (var
);
1049 /* Anonymous unions and structs are also not path_expr parents. */
1050 return !((TYPE_CODE (type
) == TYPE_CODE_STRUCT
1051 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
1052 && TYPE_NAME (type
) == NULL
);
1055 /* Return the path expression parent for VAR. */
1058 varobj_get_path_expr_parent (struct varobj
*var
)
1060 struct varobj
*parent
= var
;
1062 while (!is_root_p (parent
) && !is_path_expr_parent (parent
))
1063 parent
= parent
->parent
;
1068 /* Return a pointer to the full rooted expression of varobj VAR.
1069 If it has not been computed yet, compute it. */
1071 varobj_get_path_expr (struct varobj
*var
)
1073 if (var
->path_expr
!= NULL
)
1074 return var
->path_expr
;
1077 /* For root varobjs, we initialize path_expr
1078 when creating varobj, so here it should be
1080 gdb_assert (!is_root_p (var
));
1081 return (*var
->root
->lang_ops
->path_expr_of_child
) (var
);
1085 const struct language_defn
*
1086 varobj_get_language (struct varobj
*var
)
1088 return var
->root
->exp
->language_defn
;
1092 varobj_get_attributes (struct varobj
*var
)
1096 if (varobj_editable_p (var
))
1097 /* FIXME: define masks for attributes. */
1098 attributes
|= 0x00000001; /* Editable */
1104 varobj_pretty_printed_p (struct varobj
*var
)
1106 return var
->dynamic
->pretty_printer
!= NULL
;
1110 varobj_get_formatted_value (struct varobj
*var
,
1111 enum varobj_display_formats format
)
1113 return my_value_of_variable (var
, format
);
1117 varobj_get_value (struct varobj
*var
)
1119 return my_value_of_variable (var
, var
->format
);
1122 /* Set the value of an object variable (if it is editable) to the
1123 value of the given expression. */
1124 /* Note: Invokes functions that can call error(). */
1127 varobj_set_value (struct varobj
*var
, char *expression
)
1129 struct value
*val
= NULL
; /* Initialize to keep gcc happy. */
1130 /* The argument "expression" contains the variable's new value.
1131 We need to first construct a legal expression for this -- ugh! */
1132 /* Does this cover all the bases? */
1133 struct expression
*exp
;
1134 struct value
*value
= NULL
; /* Initialize to keep gcc happy. */
1135 int saved_input_radix
= input_radix
;
1136 const char *s
= expression
;
1137 volatile struct gdb_exception except
;
1139 gdb_assert (varobj_editable_p (var
));
1141 input_radix
= 10; /* ALWAYS reset to decimal temporarily. */
1142 exp
= parse_exp_1 (&s
, 0, 0, 0);
1143 TRY_CATCH (except
, RETURN_MASK_ERROR
)
1145 value
= evaluate_expression (exp
);
1148 if (except
.reason
< 0)
1150 /* We cannot proceed without a valid expression. */
1155 /* All types that are editable must also be changeable. */
1156 gdb_assert (varobj_value_is_changeable_p (var
));
1158 /* The value of a changeable variable object must not be lazy. */
1159 gdb_assert (!value_lazy (var
->value
));
1161 /* Need to coerce the input. We want to check if the
1162 value of the variable object will be different
1163 after assignment, and the first thing value_assign
1164 does is coerce the input.
1165 For example, if we are assigning an array to a pointer variable we
1166 should compare the pointer with the array's address, not with the
1168 value
= coerce_array (value
);
1170 /* The new value may be lazy. value_assign, or
1171 rather value_contents, will take care of this. */
1172 TRY_CATCH (except
, RETURN_MASK_ERROR
)
1174 val
= value_assign (var
->value
, value
);
1177 if (except
.reason
< 0)
1180 /* If the value has changed, record it, so that next -var-update can
1181 report this change. If a variable had a value of '1', we've set it
1182 to '333' and then set again to '1', when -var-update will report this
1183 variable as changed -- because the first assignment has set the
1184 'updated' flag. There's no need to optimize that, because return value
1185 of -var-update should be considered an approximation. */
1186 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
1187 input_radix
= saved_input_radix
;
1193 /* A helper function to install a constructor function and visualizer
1194 in a varobj_dynamic. */
1197 install_visualizer (struct varobj_dynamic
*var
, PyObject
*constructor
,
1198 PyObject
*visualizer
)
1200 Py_XDECREF (var
->constructor
);
1201 var
->constructor
= constructor
;
1203 Py_XDECREF (var
->pretty_printer
);
1204 var
->pretty_printer
= visualizer
;
1206 varobj_iter_delete (var
->child_iter
);
1207 var
->child_iter
= NULL
;
1210 /* Install the default visualizer for VAR. */
1213 install_default_visualizer (struct varobj
*var
)
1215 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1216 if (CPLUS_FAKE_CHILD (var
))
1219 if (pretty_printing
)
1221 PyObject
*pretty_printer
= NULL
;
1225 pretty_printer
= gdbpy_get_varobj_pretty_printer (var
->value
);
1226 if (! pretty_printer
)
1228 gdbpy_print_stack ();
1229 error (_("Cannot instantiate printer for default visualizer"));
1233 if (pretty_printer
== Py_None
)
1235 Py_DECREF (pretty_printer
);
1236 pretty_printer
= NULL
;
1239 install_visualizer (var
->dynamic
, NULL
, pretty_printer
);
1243 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1244 make a new object. */
1247 construct_visualizer (struct varobj
*var
, PyObject
*constructor
)
1249 PyObject
*pretty_printer
;
1251 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1252 if (CPLUS_FAKE_CHILD (var
))
1255 Py_INCREF (constructor
);
1256 if (constructor
== Py_None
)
1257 pretty_printer
= NULL
;
1260 pretty_printer
= instantiate_pretty_printer (constructor
, var
->value
);
1261 if (! pretty_printer
)
1263 gdbpy_print_stack ();
1264 Py_DECREF (constructor
);
1265 constructor
= Py_None
;
1266 Py_INCREF (constructor
);
1269 if (pretty_printer
== Py_None
)
1271 Py_DECREF (pretty_printer
);
1272 pretty_printer
= NULL
;
1276 install_visualizer (var
->dynamic
, constructor
, pretty_printer
);
1279 #endif /* HAVE_PYTHON */
1281 /* A helper function for install_new_value. This creates and installs
1282 a visualizer for VAR, if appropriate. */
1285 install_new_value_visualizer (struct varobj
*var
)
1288 /* If the constructor is None, then we want the raw value. If VAR
1289 does not have a value, just skip this. */
1290 if (!gdb_python_initialized
)
1293 if (var
->dynamic
->constructor
!= Py_None
&& var
->value
!= NULL
)
1295 struct cleanup
*cleanup
;
1297 cleanup
= varobj_ensure_python_env (var
);
1299 if (var
->dynamic
->constructor
== NULL
)
1300 install_default_visualizer (var
);
1302 construct_visualizer (var
, var
->dynamic
->constructor
);
1304 do_cleanups (cleanup
);
1311 /* When using RTTI to determine variable type it may be changed in runtime when
1312 the variable value is changed. This function checks whether type of varobj
1313 VAR will change when a new value NEW_VALUE is assigned and if it is so
1314 updates the type of VAR. */
1317 update_type_if_necessary (struct varobj
*var
, struct value
*new_value
)
1321 struct value_print_options opts
;
1323 get_user_print_options (&opts
);
1324 if (opts
.objectprint
)
1326 struct type
*new_type
;
1327 char *curr_type_str
, *new_type_str
;
1329 new_type
= value_actual_type (new_value
, 0, 0);
1330 new_type_str
= type_to_string (new_type
);
1331 curr_type_str
= varobj_get_type (var
);
1332 if (strcmp (curr_type_str
, new_type_str
) != 0)
1334 var
->type
= new_type
;
1336 /* This information may be not valid for a new type. */
1337 varobj_delete (var
, NULL
, 1);
1338 VEC_free (varobj_p
, var
->children
);
1339 var
->num_children
= -1;
1348 /* Assign a new value to a variable object. If INITIAL is non-zero,
1349 this is the first assignement after the variable object was just
1350 created, or changed type. In that case, just assign the value
1352 Otherwise, assign the new value, and return 1 if the value is
1353 different from the current one, 0 otherwise. The comparison is
1354 done on textual representation of value. Therefore, some types
1355 need not be compared. E.g. for structures the reported value is
1356 always "{...}", so no comparison is necessary here. If the old
1357 value was NULL and new one is not, or vice versa, we always return 1.
1359 The VALUE parameter should not be released -- the function will
1360 take care of releasing it when needed. */
1362 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
1367 int intentionally_not_fetched
= 0;
1368 char *print_value
= NULL
;
1370 /* We need to know the varobj's type to decide if the value should
1371 be fetched or not. C++ fake children (public/protected/private)
1372 don't have a type. */
1373 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
1374 changeable
= varobj_value_is_changeable_p (var
);
1376 /* If the type has custom visualizer, we consider it to be always
1377 changeable. FIXME: need to make sure this behaviour will not
1378 mess up read-sensitive values. */
1379 if (var
->dynamic
->pretty_printer
!= NULL
)
1382 need_to_fetch
= changeable
;
1384 /* We are not interested in the address of references, and given
1385 that in C++ a reference is not rebindable, it cannot
1386 meaningfully change. So, get hold of the real value. */
1388 value
= coerce_ref (value
);
1390 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
1391 /* For unions, we need to fetch the value implicitly because
1392 of implementation of union member fetch. When gdb
1393 creates a value for a field and the value of the enclosing
1394 structure is not lazy, it immediately copies the necessary
1395 bytes from the enclosing values. If the enclosing value is
1396 lazy, the call to value_fetch_lazy on the field will read
1397 the data from memory. For unions, that means we'll read the
1398 same memory more than once, which is not desirable. So
1402 /* The new value might be lazy. If the type is changeable,
1403 that is we'll be comparing values of this type, fetch the
1404 value now. Otherwise, on the next update the old value
1405 will be lazy, which means we've lost that old value. */
1406 if (need_to_fetch
&& value
&& value_lazy (value
))
1408 struct varobj
*parent
= var
->parent
;
1409 int frozen
= var
->frozen
;
1411 for (; !frozen
&& parent
; parent
= parent
->parent
)
1412 frozen
|= parent
->frozen
;
1414 if (frozen
&& initial
)
1416 /* For variables that are frozen, or are children of frozen
1417 variables, we don't do fetch on initial assignment.
1418 For non-initial assignemnt we do the fetch, since it means we're
1419 explicitly asked to compare the new value with the old one. */
1420 intentionally_not_fetched
= 1;
1424 volatile struct gdb_exception except
;
1426 TRY_CATCH (except
, RETURN_MASK_ERROR
)
1428 value_fetch_lazy (value
);
1431 if (except
.reason
< 0)
1433 /* Set the value to NULL, so that for the next -var-update,
1434 we don't try to compare the new value with this value,
1435 that we couldn't even read. */
1441 /* Get a reference now, before possibly passing it to any Python
1442 code that might release it. */
1444 value_incref (value
);
1446 /* Below, we'll be comparing string rendering of old and new
1447 values. Don't get string rendering if the value is
1448 lazy -- if it is, the code above has decided that the value
1449 should not be fetched. */
1450 if (value
!= NULL
&& !value_lazy (value
)
1451 && var
->dynamic
->pretty_printer
== NULL
)
1452 print_value
= varobj_value_get_print_value (value
, var
->format
, var
);
1454 /* If the type is changeable, compare the old and the new values.
1455 If this is the initial assignment, we don't have any old value
1457 if (!initial
&& changeable
)
1459 /* If the value of the varobj was changed by -var-set-value,
1460 then the value in the varobj and in the target is the same.
1461 However, that value is different from the value that the
1462 varobj had after the previous -var-update. So need to the
1463 varobj as changed. */
1468 else if (var
->dynamic
->pretty_printer
== NULL
)
1470 /* Try to compare the values. That requires that both
1471 values are non-lazy. */
1472 if (var
->not_fetched
&& value_lazy (var
->value
))
1474 /* This is a frozen varobj and the value was never read.
1475 Presumably, UI shows some "never read" indicator.
1476 Now that we've fetched the real value, we need to report
1477 this varobj as changed so that UI can show the real
1481 else if (var
->value
== NULL
&& value
== NULL
)
1484 else if (var
->value
== NULL
|| value
== NULL
)
1490 gdb_assert (!value_lazy (var
->value
));
1491 gdb_assert (!value_lazy (value
));
1493 gdb_assert (var
->print_value
!= NULL
&& print_value
!= NULL
);
1494 if (strcmp (var
->print_value
, print_value
) != 0)
1500 if (!initial
&& !changeable
)
1502 /* For values that are not changeable, we don't compare the values.
1503 However, we want to notice if a value was not NULL and now is NULL,
1504 or vise versa, so that we report when top-level varobjs come in scope
1505 and leave the scope. */
1506 changed
= (var
->value
!= NULL
) != (value
!= NULL
);
1509 /* We must always keep the new value, since children depend on it. */
1510 if (var
->value
!= NULL
&& var
->value
!= value
)
1511 value_free (var
->value
);
1513 if (value
&& value_lazy (value
) && intentionally_not_fetched
)
1514 var
->not_fetched
= 1;
1516 var
->not_fetched
= 0;
1519 install_new_value_visualizer (var
);
1521 /* If we installed a pretty-printer, re-compare the printed version
1522 to see if the variable changed. */
1523 if (var
->dynamic
->pretty_printer
!= NULL
)
1525 xfree (print_value
);
1526 print_value
= varobj_value_get_print_value (var
->value
, var
->format
,
1528 if ((var
->print_value
== NULL
&& print_value
!= NULL
)
1529 || (var
->print_value
!= NULL
&& print_value
== NULL
)
1530 || (var
->print_value
!= NULL
&& print_value
!= NULL
1531 && strcmp (var
->print_value
, print_value
) != 0))
1534 if (var
->print_value
)
1535 xfree (var
->print_value
);
1536 var
->print_value
= print_value
;
1538 gdb_assert (!var
->value
|| value_type (var
->value
));
1543 /* Return the requested range for a varobj. VAR is the varobj. FROM
1544 and TO are out parameters; *FROM and *TO will be set to the
1545 selected sub-range of VAR. If no range was selected using
1546 -var-set-update-range, then both will be -1. */
1548 varobj_get_child_range (struct varobj
*var
, int *from
, int *to
)
1554 /* Set the selected sub-range of children of VAR to start at index
1555 FROM and end at index TO. If either FROM or TO is less than zero,
1556 this is interpreted as a request for all children. */
1558 varobj_set_child_range (struct varobj
*var
, int from
, int to
)
1565 varobj_set_visualizer (struct varobj
*var
, const char *visualizer
)
1568 PyObject
*mainmod
, *globals
, *constructor
;
1569 struct cleanup
*back_to
;
1571 if (!gdb_python_initialized
)
1574 back_to
= varobj_ensure_python_env (var
);
1576 mainmod
= PyImport_AddModule ("__main__");
1577 globals
= PyModule_GetDict (mainmod
);
1578 Py_INCREF (globals
);
1579 make_cleanup_py_decref (globals
);
1581 constructor
= PyRun_String (visualizer
, Py_eval_input
, globals
, globals
);
1585 gdbpy_print_stack ();
1586 error (_("Could not evaluate visualizer expression: %s"), visualizer
);
1589 construct_visualizer (var
, constructor
);
1590 Py_XDECREF (constructor
);
1592 /* If there are any children now, wipe them. */
1593 varobj_delete (var
, NULL
, 1 /* children only */);
1594 var
->num_children
= -1;
1596 do_cleanups (back_to
);
1598 error (_("Python support required"));
1602 /* If NEW_VALUE is the new value of the given varobj (var), return
1603 non-zero if var has mutated. In other words, if the type of
1604 the new value is different from the type of the varobj's old
1607 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1610 varobj_value_has_mutated (struct varobj
*var
, struct value
*new_value
,
1611 struct type
*new_type
)
1613 /* If we haven't previously computed the number of children in var,
1614 it does not matter from the front-end's perspective whether
1615 the type has mutated or not. For all intents and purposes,
1616 it has not mutated. */
1617 if (var
->num_children
< 0)
1620 if (var
->root
->lang_ops
->value_has_mutated
)
1622 /* The varobj module, when installing new values, explicitly strips
1623 references, saying that we're not interested in those addresses.
1624 But detection of mutation happens before installing the new
1625 value, so our value may be a reference that we need to strip
1626 in order to remain consistent. */
1627 if (new_value
!= NULL
)
1628 new_value
= coerce_ref (new_value
);
1629 return var
->root
->lang_ops
->value_has_mutated (var
, new_value
, new_type
);
1635 /* Update the values for a variable and its children. This is a
1636 two-pronged attack. First, re-parse the value for the root's
1637 expression to see if it's changed. Then go all the way
1638 through its children, reconstructing them and noting if they've
1641 The EXPLICIT parameter specifies if this call is result
1642 of MI request to update this specific variable, or
1643 result of implicit -var-update *. For implicit request, we don't
1644 update frozen variables.
1646 NOTE: This function may delete the caller's varobj. If it
1647 returns TYPE_CHANGED, then it has done this and VARP will be modified
1648 to point to the new varobj. */
1650 VEC(varobj_update_result
) *
1651 varobj_update (struct varobj
**varp
, int explicit)
1653 int type_changed
= 0;
1656 VEC (varobj_update_result
) *stack
= NULL
;
1657 VEC (varobj_update_result
) *result
= NULL
;
1659 /* Frozen means frozen -- we don't check for any change in
1660 this varobj, including its going out of scope, or
1661 changing type. One use case for frozen varobjs is
1662 retaining previously evaluated expressions, and we don't
1663 want them to be reevaluated at all. */
1664 if (!explicit && (*varp
)->frozen
)
1667 if (!(*varp
)->root
->is_valid
)
1669 varobj_update_result r
= {0};
1672 r
.status
= VAROBJ_INVALID
;
1673 VEC_safe_push (varobj_update_result
, result
, &r
);
1677 if ((*varp
)->root
->rootvar
== *varp
)
1679 varobj_update_result r
= {0};
1682 r
.status
= VAROBJ_IN_SCOPE
;
1684 /* Update the root variable. value_of_root can return NULL
1685 if the variable is no longer around, i.e. we stepped out of
1686 the frame in which a local existed. We are letting the
1687 value_of_root variable dispose of the varobj if the type
1689 new = value_of_root (varp
, &type_changed
);
1690 if (update_type_if_necessary(*varp
, new))
1693 r
.type_changed
= type_changed
;
1694 if (install_new_value ((*varp
), new, type_changed
))
1698 r
.status
= VAROBJ_NOT_IN_SCOPE
;
1699 r
.value_installed
= 1;
1701 if (r
.status
== VAROBJ_NOT_IN_SCOPE
)
1703 if (r
.type_changed
|| r
.changed
)
1704 VEC_safe_push (varobj_update_result
, result
, &r
);
1708 VEC_safe_push (varobj_update_result
, stack
, &r
);
1712 varobj_update_result r
= {0};
1715 VEC_safe_push (varobj_update_result
, stack
, &r
);
1718 /* Walk through the children, reconstructing them all. */
1719 while (!VEC_empty (varobj_update_result
, stack
))
1721 varobj_update_result r
= *(VEC_last (varobj_update_result
, stack
));
1722 struct varobj
*v
= r
.varobj
;
1724 VEC_pop (varobj_update_result
, stack
);
1726 /* Update this variable, unless it's a root, which is already
1728 if (!r
.value_installed
)
1730 struct type
*new_type
;
1732 new = value_of_child (v
->parent
, v
->index
);
1733 if (update_type_if_necessary(v
, new))
1736 new_type
= value_type (new);
1738 new_type
= v
->root
->lang_ops
->type_of_child (v
->parent
, v
->index
);
1740 if (varobj_value_has_mutated (v
, new, new_type
))
1742 /* The children are no longer valid; delete them now.
1743 Report the fact that its type changed as well. */
1744 varobj_delete (v
, NULL
, 1 /* only_children */);
1745 v
->num_children
= -1;
1752 if (install_new_value (v
, new, r
.type_changed
))
1759 /* We probably should not get children of a varobj that has a
1760 pretty-printer, but for which -var-list-children was never
1762 if (v
->dynamic
->pretty_printer
!= NULL
)
1764 VEC (varobj_p
) *changed
= 0, *type_changed
= 0, *unchanged
= 0;
1765 VEC (varobj_p
) *new = 0;
1766 int i
, children_changed
= 0;
1771 if (!v
->dynamic
->children_requested
)
1775 /* If we initially did not have potential children, but
1776 now we do, consider the varobj as changed.
1777 Otherwise, if children were never requested, consider
1778 it as unchanged -- presumably, such varobj is not yet
1779 expanded in the UI, so we need not bother getting
1781 if (!varobj_has_more (v
, 0))
1783 update_dynamic_varobj_children (v
, NULL
, NULL
, NULL
, NULL
,
1785 if (varobj_has_more (v
, 0))
1790 VEC_safe_push (varobj_update_result
, result
, &r
);
1795 /* If update_dynamic_varobj_children returns 0, then we have
1796 a non-conforming pretty-printer, so we skip it. */
1797 if (update_dynamic_varobj_children (v
, &changed
, &type_changed
, &new,
1798 &unchanged
, &children_changed
, 1,
1801 if (children_changed
|| new)
1803 r
.children_changed
= 1;
1806 /* Push in reverse order so that the first child is
1807 popped from the work stack first, and so will be
1808 added to result first. This does not affect
1809 correctness, just "nicer". */
1810 for (i
= VEC_length (varobj_p
, type_changed
) - 1; i
>= 0; --i
)
1812 varobj_p tmp
= VEC_index (varobj_p
, type_changed
, i
);
1813 varobj_update_result r
= {0};
1815 /* Type may change only if value was changed. */
1819 r
.value_installed
= 1;
1820 VEC_safe_push (varobj_update_result
, stack
, &r
);
1822 for (i
= VEC_length (varobj_p
, changed
) - 1; i
>= 0; --i
)
1824 varobj_p tmp
= VEC_index (varobj_p
, changed
, i
);
1825 varobj_update_result r
= {0};
1829 r
.value_installed
= 1;
1830 VEC_safe_push (varobj_update_result
, stack
, &r
);
1832 for (i
= VEC_length (varobj_p
, unchanged
) - 1; i
>= 0; --i
)
1834 varobj_p tmp
= VEC_index (varobj_p
, unchanged
, i
);
1838 varobj_update_result r
= {0};
1841 r
.value_installed
= 1;
1842 VEC_safe_push (varobj_update_result
, stack
, &r
);
1845 if (r
.changed
|| r
.children_changed
)
1846 VEC_safe_push (varobj_update_result
, result
, &r
);
1848 /* Free CHANGED, TYPE_CHANGED and UNCHANGED, but not NEW,
1849 because NEW has been put into the result vector. */
1850 VEC_free (varobj_p
, changed
);
1851 VEC_free (varobj_p
, type_changed
);
1852 VEC_free (varobj_p
, unchanged
);
1858 /* Push any children. Use reverse order so that the first
1859 child is popped from the work stack first, and so
1860 will be added to result first. This does not
1861 affect correctness, just "nicer". */
1862 for (i
= VEC_length (varobj_p
, v
->children
)-1; i
>= 0; --i
)
1864 varobj_p c
= VEC_index (varobj_p
, v
->children
, i
);
1866 /* Child may be NULL if explicitly deleted by -var-delete. */
1867 if (c
!= NULL
&& !c
->frozen
)
1869 varobj_update_result r
= {0};
1872 VEC_safe_push (varobj_update_result
, stack
, &r
);
1876 if (r
.changed
|| r
.type_changed
)
1877 VEC_safe_push (varobj_update_result
, result
, &r
);
1880 VEC_free (varobj_update_result
, stack
);
1886 /* Helper functions */
1889 * Variable object construction/destruction
1893 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
1894 int only_children_p
)
1898 delete_variable_1 (resultp
, &delcount
, var
,
1899 only_children_p
, 1 /* remove_from_parent_p */ );
1904 /* Delete the variable object VAR and its children. */
1905 /* IMPORTANT NOTE: If we delete a variable which is a child
1906 and the parent is not removed we dump core. It must be always
1907 initially called with remove_from_parent_p set. */
1909 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
1910 struct varobj
*var
, int only_children_p
,
1911 int remove_from_parent_p
)
1915 /* Delete any children of this variable, too. */
1916 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
1918 varobj_p child
= VEC_index (varobj_p
, var
->children
, i
);
1922 if (!remove_from_parent_p
)
1923 child
->parent
= NULL
;
1924 delete_variable_1 (resultp
, delcountp
, child
, 0, only_children_p
);
1926 VEC_free (varobj_p
, var
->children
);
1928 /* if we were called to delete only the children we are done here. */
1929 if (only_children_p
)
1932 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
1933 /* If the name is null, this is a temporary variable, that has not
1934 yet been installed, don't report it, it belongs to the caller... */
1935 if (var
->obj_name
!= NULL
)
1937 cppush (resultp
, xstrdup (var
->obj_name
));
1938 *delcountp
= *delcountp
+ 1;
1941 /* If this variable has a parent, remove it from its parent's list. */
1942 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1943 (as indicated by remove_from_parent_p) we don't bother doing an
1944 expensive list search to find the element to remove when we are
1945 discarding the list afterwards. */
1946 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1948 VEC_replace (varobj_p
, var
->parent
->children
, var
->index
, NULL
);
1951 if (var
->obj_name
!= NULL
)
1952 uninstall_variable (var
);
1954 /* Free memory associated with this variable. */
1955 free_variable (var
);
1958 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1960 install_variable (struct varobj
*var
)
1963 struct vlist
*newvl
;
1965 unsigned int index
= 0;
1968 for (chp
= var
->obj_name
; *chp
; chp
++)
1970 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1973 cv
= *(varobj_table
+ index
);
1974 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
1978 error (_("Duplicate variable object name"));
1980 /* Add varobj to hash table. */
1981 newvl
= xmalloc (sizeof (struct vlist
));
1982 newvl
->next
= *(varobj_table
+ index
);
1984 *(varobj_table
+ index
) = newvl
;
1986 /* If root, add varobj to root list. */
1987 if (is_root_p (var
))
1989 /* Add to list of root variables. */
1990 if (rootlist
== NULL
)
1991 var
->root
->next
= NULL
;
1993 var
->root
->next
= rootlist
;
1994 rootlist
= var
->root
;
2000 /* Unistall the object VAR. */
2002 uninstall_variable (struct varobj
*var
)
2006 struct varobj_root
*cr
;
2007 struct varobj_root
*prer
;
2009 unsigned int index
= 0;
2012 /* Remove varobj from hash table. */
2013 for (chp
= var
->obj_name
; *chp
; chp
++)
2015 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
2018 cv
= *(varobj_table
+ index
);
2020 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
2027 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
2032 ("Assertion failed: Could not find variable object \"%s\" to delete",
2038 *(varobj_table
+ index
) = cv
->next
;
2040 prev
->next
= cv
->next
;
2044 /* If root, remove varobj from root list. */
2045 if (is_root_p (var
))
2047 /* Remove from list of root variables. */
2048 if (rootlist
== var
->root
)
2049 rootlist
= var
->root
->next
;
2054 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
2061 warning (_("Assertion failed: Could not find "
2062 "varobj \"%s\" in root list"),
2069 prer
->next
= cr
->next
;
2075 /* Create and install a child of the parent of the given name. */
2076 static struct varobj
*
2077 create_child (struct varobj
*parent
, int index
, char *name
)
2079 struct varobj_item item
;
2082 item
.value
= value_of_child (parent
, index
);
2084 return create_child_with_value (parent
, index
, &item
);
2087 static struct varobj
*
2088 create_child_with_value (struct varobj
*parent
, int index
,
2089 struct varobj_item
*item
)
2091 struct varobj
*child
;
2094 child
= new_variable ();
2096 /* NAME is allocated by caller. */
2097 child
->name
= item
->name
;
2098 child
->index
= index
;
2099 child
->parent
= parent
;
2100 child
->root
= parent
->root
;
2102 if (varobj_is_anonymous_child (child
))
2103 childs_name
= xstrprintf ("%s.%d_anonymous", parent
->obj_name
, index
);
2105 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, item
->name
);
2106 child
->obj_name
= childs_name
;
2108 install_variable (child
);
2110 /* Compute the type of the child. Must do this before
2111 calling install_new_value. */
2112 if (item
->value
!= NULL
)
2113 /* If the child had no evaluation errors, var->value
2114 will be non-NULL and contain a valid type. */
2115 child
->type
= value_actual_type (item
->value
, 0, NULL
);
2117 /* Otherwise, we must compute the type. */
2118 child
->type
= (*child
->root
->lang_ops
->type_of_child
) (child
->parent
,
2120 install_new_value (child
, item
->value
, 1);
2127 * Miscellaneous utility functions.
2130 /* Allocate memory and initialize a new variable. */
2131 static struct varobj
*
2136 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
2138 var
->path_expr
= NULL
;
2139 var
->obj_name
= NULL
;
2143 var
->num_children
= -1;
2145 var
->children
= NULL
;
2149 var
->print_value
= NULL
;
2151 var
->not_fetched
= 0;
2153 = (struct varobj_dynamic
*) xmalloc (sizeof (struct varobj_dynamic
));
2154 var
->dynamic
->children_requested
= 0;
2157 var
->dynamic
->constructor
= 0;
2158 var
->dynamic
->pretty_printer
= 0;
2159 var
->dynamic
->child_iter
= 0;
2160 var
->dynamic
->saved_item
= 0;
2165 /* Allocate memory and initialize a new root variable. */
2166 static struct varobj
*
2167 new_root_variable (void)
2169 struct varobj
*var
= new_variable ();
2171 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));
2172 var
->root
->lang_ops
= NULL
;
2173 var
->root
->exp
= NULL
;
2174 var
->root
->valid_block
= NULL
;
2175 var
->root
->frame
= null_frame_id
;
2176 var
->root
->floating
= 0;
2177 var
->root
->rootvar
= NULL
;
2178 var
->root
->is_valid
= 1;
2183 /* Free any allocated memory associated with VAR. */
2185 free_variable (struct varobj
*var
)
2188 if (var
->dynamic
->pretty_printer
!= NULL
)
2190 struct cleanup
*cleanup
= varobj_ensure_python_env (var
);
2192 Py_XDECREF (var
->dynamic
->constructor
);
2193 Py_XDECREF (var
->dynamic
->pretty_printer
);
2194 Py_XDECREF (var
->dynamic
->child_iter
);
2195 Py_XDECREF (var
->dynamic
->saved_item
);
2196 do_cleanups (cleanup
);
2200 value_free (var
->value
);
2202 /* Free the expression if this is a root variable. */
2203 if (is_root_p (var
))
2205 xfree (var
->root
->exp
);
2210 xfree (var
->obj_name
);
2211 xfree (var
->print_value
);
2212 xfree (var
->path_expr
);
2213 xfree (var
->dynamic
);
2218 do_free_variable_cleanup (void *var
)
2220 free_variable (var
);
2223 static struct cleanup
*
2224 make_cleanup_free_variable (struct varobj
*var
)
2226 return make_cleanup (do_free_variable_cleanup
, var
);
2229 /* Return the type of the value that's stored in VAR,
2230 or that would have being stored there if the
2231 value were accessible.
2233 This differs from VAR->type in that VAR->type is always
2234 the true type of the expession in the source language.
2235 The return value of this function is the type we're
2236 actually storing in varobj, and using for displaying
2237 the values and for comparing previous and new values.
2239 For example, top-level references are always stripped. */
2241 varobj_get_value_type (struct varobj
*var
)
2246 type
= value_type (var
->value
);
2250 type
= check_typedef (type
);
2252 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
2253 type
= get_target_type (type
);
2255 type
= check_typedef (type
);
2260 /* What is the default display for this variable? We assume that
2261 everything is "natural". Any exceptions? */
2262 static enum varobj_display_formats
2263 variable_default_display (struct varobj
*var
)
2265 return FORMAT_NATURAL
;
2268 /* FIXME: The following should be generic for any pointer. */
2270 cppush (struct cpstack
**pstack
, char *name
)
2274 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
2280 /* FIXME: The following should be generic for any pointer. */
2282 cppop (struct cpstack
**pstack
)
2287 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
2292 *pstack
= (*pstack
)->next
;
2299 * Language-dependencies
2302 /* Common entry points */
2304 /* Return the number of children for a given variable.
2305 The result of this function is defined by the language
2306 implementation. The number of children returned by this function
2307 is the number of children that the user will see in the variable
2310 number_of_children (struct varobj
*var
)
2312 return (*var
->root
->lang_ops
->number_of_children
) (var
);
2315 /* What is the expression for the root varobj VAR? Returns a malloc'd
2318 name_of_variable (struct varobj
*var
)
2320 return (*var
->root
->lang_ops
->name_of_variable
) (var
);
2323 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2326 name_of_child (struct varobj
*var
, int index
)
2328 return (*var
->root
->lang_ops
->name_of_child
) (var
, index
);
2331 /* If frame associated with VAR can be found, switch
2332 to it and return 1. Otherwise, return 0. */
2335 check_scope (struct varobj
*var
)
2337 struct frame_info
*fi
;
2340 fi
= frame_find_by_id (var
->root
->frame
);
2345 CORE_ADDR pc
= get_frame_pc (fi
);
2347 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
2348 pc
>= BLOCK_END (var
->root
->valid_block
))
2356 /* Helper function to value_of_root. */
2358 static struct value
*
2359 value_of_root_1 (struct varobj
**var_handle
)
2361 struct value
*new_val
= NULL
;
2362 struct varobj
*var
= *var_handle
;
2363 int within_scope
= 0;
2364 struct cleanup
*back_to
;
2366 /* Only root variables can be updated... */
2367 if (!is_root_p (var
))
2368 /* Not a root var. */
2371 back_to
= make_cleanup_restore_current_thread ();
2373 /* Determine whether the variable is still around. */
2374 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
2376 else if (var
->root
->thread_id
== 0)
2378 /* The program was single-threaded when the variable object was
2379 created. Technically, it's possible that the program became
2380 multi-threaded since then, but we don't support such
2382 within_scope
= check_scope (var
);
2386 ptid_t ptid
= thread_id_to_pid (var
->root
->thread_id
);
2387 if (in_thread_list (ptid
))
2389 switch_to_thread (ptid
);
2390 within_scope
= check_scope (var
);
2396 volatile struct gdb_exception except
;
2398 /* We need to catch errors here, because if evaluate
2399 expression fails we want to just return NULL. */
2400 TRY_CATCH (except
, RETURN_MASK_ERROR
)
2402 new_val
= evaluate_expression (var
->root
->exp
);
2406 do_cleanups (back_to
);
2411 /* What is the ``struct value *'' of the root variable VAR?
2412 For floating variable object, evaluation can get us a value
2413 of different type from what is stored in varobj already. In
2415 - *type_changed will be set to 1
2416 - old varobj will be freed, and new one will be
2417 created, with the same name.
2418 - *var_handle will be set to the new varobj
2419 Otherwise, *type_changed will be set to 0. */
2420 static struct value
*
2421 value_of_root (struct varobj
**var_handle
, int *type_changed
)
2425 if (var_handle
== NULL
)
2430 /* This should really be an exception, since this should
2431 only get called with a root variable. */
2433 if (!is_root_p (var
))
2436 if (var
->root
->floating
)
2438 struct varobj
*tmp_var
;
2439 char *old_type
, *new_type
;
2441 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
2442 USE_SELECTED_FRAME
);
2443 if (tmp_var
== NULL
)
2447 old_type
= varobj_get_type (var
);
2448 new_type
= varobj_get_type (tmp_var
);
2449 if (strcmp (old_type
, new_type
) == 0)
2451 /* The expression presently stored inside var->root->exp
2452 remembers the locations of local variables relatively to
2453 the frame where the expression was created (in DWARF location
2454 button, for example). Naturally, those locations are not
2455 correct in other frames, so update the expression. */
2457 struct expression
*tmp_exp
= var
->root
->exp
;
2459 var
->root
->exp
= tmp_var
->root
->exp
;
2460 tmp_var
->root
->exp
= tmp_exp
;
2462 varobj_delete (tmp_var
, NULL
, 0);
2467 tmp_var
->obj_name
= xstrdup (var
->obj_name
);
2468 tmp_var
->from
= var
->from
;
2469 tmp_var
->to
= var
->to
;
2470 varobj_delete (var
, NULL
, 0);
2472 install_variable (tmp_var
);
2473 *var_handle
= tmp_var
;
2486 struct value
*value
;
2488 value
= value_of_root_1 (var_handle
);
2489 if (var
->value
== NULL
|| value
== NULL
)
2491 /* For root varobj-s, a NULL value indicates a scoping issue.
2492 So, nothing to do in terms of checking for mutations. */
2494 else if (varobj_value_has_mutated (var
, value
, value_type (value
)))
2496 /* The type has mutated, so the children are no longer valid.
2497 Just delete them, and tell our caller that the type has
2499 varobj_delete (var
, NULL
, 1 /* only_children */);
2500 var
->num_children
= -1;
2509 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2510 static struct value
*
2511 value_of_child (struct varobj
*parent
, int index
)
2513 struct value
*value
;
2515 value
= (*parent
->root
->lang_ops
->value_of_child
) (parent
, index
);
2520 /* GDB already has a command called "value_of_variable". Sigh. */
2522 my_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2524 if (var
->root
->is_valid
)
2526 if (var
->dynamic
->pretty_printer
!= NULL
)
2527 return varobj_value_get_print_value (var
->value
, var
->format
, var
);
2528 return (*var
->root
->lang_ops
->value_of_variable
) (var
, format
);
2535 varobj_formatted_print_options (struct value_print_options
*opts
,
2536 enum varobj_display_formats format
)
2538 get_formatted_print_options (opts
, format_code
[(int) format
]);
2539 opts
->deref_ref
= 0;
2544 varobj_value_get_print_value (struct value
*value
,
2545 enum varobj_display_formats format
,
2548 struct ui_file
*stb
;
2549 struct cleanup
*old_chain
;
2550 char *thevalue
= NULL
;
2551 struct value_print_options opts
;
2552 struct type
*type
= NULL
;
2554 char *encoding
= NULL
;
2555 struct gdbarch
*gdbarch
= NULL
;
2556 /* Initialize it just to avoid a GCC false warning. */
2557 CORE_ADDR str_addr
= 0;
2558 int string_print
= 0;
2563 stb
= mem_fileopen ();
2564 old_chain
= make_cleanup_ui_file_delete (stb
);
2566 gdbarch
= get_type_arch (value_type (value
));
2568 if (gdb_python_initialized
)
2570 PyObject
*value_formatter
= var
->dynamic
->pretty_printer
;
2572 varobj_ensure_python_env (var
);
2574 if (value_formatter
)
2576 /* First check to see if we have any children at all. If so,
2577 we simply return {...}. */
2578 if (dynamic_varobj_has_child_method (var
))
2580 do_cleanups (old_chain
);
2581 return xstrdup ("{...}");
2584 if (PyObject_HasAttr (value_formatter
, gdbpy_to_string_cst
))
2586 struct value
*replacement
;
2587 PyObject
*output
= NULL
;
2589 output
= apply_varobj_pretty_printer (value_formatter
,
2593 /* If we have string like output ... */
2596 make_cleanup_py_decref (output
);
2598 /* If this is a lazy string, extract it. For lazy
2599 strings we always print as a string, so set
2601 if (gdbpy_is_lazy_string (output
))
2603 gdbpy_extract_lazy_string (output
, &str_addr
, &type
,
2605 make_cleanup (free_current_contents
, &encoding
);
2610 /* If it is a regular (non-lazy) string, extract
2611 it and copy the contents into THEVALUE. If the
2612 hint says to print it as a string, set
2613 string_print. Otherwise just return the extracted
2614 string as a value. */
2616 char *s
= python_string_to_target_string (output
);
2622 hint
= gdbpy_get_display_hint (value_formatter
);
2625 if (!strcmp (hint
, "string"))
2631 thevalue
= xmemdup (s
, len
+ 1, len
+ 1);
2632 type
= builtin_type (gdbarch
)->builtin_char
;
2637 do_cleanups (old_chain
);
2641 make_cleanup (xfree
, thevalue
);
2644 gdbpy_print_stack ();
2647 /* If the printer returned a replacement value, set VALUE
2648 to REPLACEMENT. If there is not a replacement value,
2649 just use the value passed to this function. */
2651 value
= replacement
;
2657 varobj_formatted_print_options (&opts
, format
);
2659 /* If the THEVALUE has contents, it is a regular string. */
2661 LA_PRINT_STRING (stb
, type
, (gdb_byte
*) thevalue
, len
, encoding
, 0, &opts
);
2662 else if (string_print
)
2663 /* Otherwise, if string_print is set, and it is not a regular
2664 string, it is a lazy string. */
2665 val_print_string (type
, encoding
, str_addr
, len
, stb
, &opts
);
2667 /* All other cases. */
2668 common_val_print (value
, stb
, 0, &opts
, current_language
);
2670 thevalue
= ui_file_xstrdup (stb
, NULL
);
2672 do_cleanups (old_chain
);
2677 varobj_editable_p (struct varobj
*var
)
2681 if (!(var
->root
->is_valid
&& var
->value
&& VALUE_LVAL (var
->value
)))
2684 type
= varobj_get_value_type (var
);
2686 switch (TYPE_CODE (type
))
2688 case TYPE_CODE_STRUCT
:
2689 case TYPE_CODE_UNION
:
2690 case TYPE_CODE_ARRAY
:
2691 case TYPE_CODE_FUNC
:
2692 case TYPE_CODE_METHOD
:
2702 /* Call VAR's value_is_changeable_p language-specific callback. */
2705 varobj_value_is_changeable_p (struct varobj
*var
)
2707 return var
->root
->lang_ops
->value_is_changeable_p (var
);
2710 /* Return 1 if that varobj is floating, that is is always evaluated in the
2711 selected frame, and not bound to thread/frame. Such variable objects
2712 are created using '@' as frame specifier to -var-create. */
2714 varobj_floating_p (struct varobj
*var
)
2716 return var
->root
->floating
;
2719 /* Implement the "value_is_changeable_p" varobj callback for most
2723 varobj_default_value_is_changeable_p (struct varobj
*var
)
2728 if (CPLUS_FAKE_CHILD (var
))
2731 type
= varobj_get_value_type (var
);
2733 switch (TYPE_CODE (type
))
2735 case TYPE_CODE_STRUCT
:
2736 case TYPE_CODE_UNION
:
2737 case TYPE_CODE_ARRAY
:
2748 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
2749 with an arbitrary caller supplied DATA pointer. */
2752 all_root_varobjs (void (*func
) (struct varobj
*var
, void *data
), void *data
)
2754 struct varobj_root
*var_root
, *var_root_next
;
2756 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
2758 for (var_root
= rootlist
; var_root
!= NULL
; var_root
= var_root_next
)
2760 var_root_next
= var_root
->next
;
2762 (*func
) (var_root
->rootvar
, data
);
2766 extern void _initialize_varobj (void);
2768 _initialize_varobj (void)
2770 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
2772 varobj_table
= xmalloc (sizeof_table
);
2773 memset (varobj_table
, 0, sizeof_table
);
2775 add_setshow_zuinteger_cmd ("varobj", class_maintenance
,
2777 _("Set varobj debugging."),
2778 _("Show varobj debugging."),
2779 _("When non-zero, varobj debugging is enabled."),
2780 NULL
, show_varobjdebug
,
2781 &setdebuglist
, &showdebuglist
);
2784 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
2785 defined on globals. It is a helper for varobj_invalidate.
2787 This function is called after changing the symbol file, in this case the
2788 pointers to "struct type" stored by the varobj are no longer valid. All
2789 varobj must be either re-evaluated, or marked as invalid here. */
2792 varobj_invalidate_iter (struct varobj
*var
, void *unused
)
2794 /* global and floating var must be re-evaluated. */
2795 if (var
->root
->floating
|| var
->root
->valid_block
== NULL
)
2797 struct varobj
*tmp_var
;
2799 /* Try to create a varobj with same expression. If we succeed
2800 replace the old varobj, otherwise invalidate it. */
2801 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
2803 if (tmp_var
!= NULL
)
2805 tmp_var
->obj_name
= xstrdup (var
->obj_name
);
2806 varobj_delete (var
, NULL
, 0);
2807 install_variable (tmp_var
);
2810 var
->root
->is_valid
= 0;
2812 else /* locals must be invalidated. */
2813 var
->root
->is_valid
= 0;
2816 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2817 are defined on globals.
2818 Invalidated varobjs will be always printed in_scope="invalid". */
2821 varobj_invalidate (void)
2823 all_root_varobjs (varobj_invalidate_iter
, NULL
);