1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2024 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 "expression.h"
22 #include "cli/cli-cmds.h"
25 #include "gdbsupport/gdb_regex.h"
28 #include "gdbthread.h"
30 #include "varobj-iter.h"
31 #include "parser-defs.h"
34 #include "observable.h"
37 #include "python/python.h"
38 #include "python/python-internal.h"
45 unsigned int varobjdebug
= 0;
47 show_varobjdebug (struct ui_file
*file
, int from_tty
,
48 struct cmd_list_element
*c
, const char *value
)
50 gdb_printf (file
, _("Varobj debugging is %s.\n"), value
);
53 /* String representations of gdb's format codes. */
54 const char *varobj_format_string
[] =
55 { "natural", "binary", "decimal", "hexadecimal", "octal", "zero-hexadecimal" };
57 /* True if we want to allow Python-based pretty-printing. */
58 static bool pretty_printing
= false;
61 varobj_enable_pretty_printing (void)
63 pretty_printing
= true;
68 /* Every root variable has one of these structures saved in its
72 /* The expression for this parent. */
75 /* Cached arch from exp, for use in case exp gets invalidated. */
76 struct gdbarch
*gdbarch
= nullptr;
78 /* Cached language from exp, for use in case exp gets invalidated. */
79 const struct language_defn
*language_defn
= nullptr;
81 /* Block for which this expression is valid. */
82 const struct block
*valid_block
= NULL
;
84 /* The frame for this expression. This field is set iff valid_block is
86 struct frame_id frame
= null_frame_id
;
88 /* The global thread ID that this varobj_root belongs to. This field
89 is only valid if valid_block is not NULL.
90 When not 0, indicates which thread 'frame' belongs to.
91 When 0, indicates that the thread list was empty when the varobj_root
95 /* If true, the -var-update always recomputes the value in the
96 current thread and frame. Otherwise, variable object is
97 always updated in the specific scope/thread/frame. */
98 bool floating
= false;
100 /* Flag that indicates validity: set to false when this varobj_root refers
101 to symbols that do not exist anymore. */
102 bool is_valid
= true;
104 /* Set to true if the varobj was created as tracking a global. */
107 /* Language-related operations for this variable and its
109 const struct lang_varobj_ops
*lang_ops
= NULL
;
111 /* The varobj for this root node. */
112 struct varobj
*rootvar
= NULL
;
115 /* Dynamic part of varobj. */
117 struct varobj_dynamic
119 /* Whether the children of this varobj were requested. This field is
120 used to decide if dynamic varobj should recompute their children.
121 In the event that the frontend never asked for the children, we
123 bool children_requested
= false;
125 /* The pretty-printer constructor. If NULL, then the default
126 pretty-printer will be looked up. If None, then no
127 pretty-printer will be installed. */
128 PyObject
*constructor
= NULL
;
130 /* The pretty-printer that has been constructed. If NULL, then a
131 new printer object is needed, and one will be constructed. */
132 PyObject
*pretty_printer
= NULL
;
134 /* The iterator returned by the printer's 'children' method, or NULL
136 std::unique_ptr
<varobj_iter
> child_iter
;
138 /* We request one extra item from the iterator, so that we can
139 report to the caller whether there are more items than we have
140 already reported. However, we don't want to install this value
141 when we read it, because that will mess up future updates. So,
142 we stash it here instead. */
143 std::unique_ptr
<varobj_item
> saved_item
;
146 /* Private function prototypes */
148 /* Helper functions for the above subcommands. */
150 static int delete_variable (struct varobj
*, bool);
152 static void delete_variable_1 (int *, struct varobj
*, bool, bool);
154 static void install_variable (struct varobj
*);
156 static void uninstall_variable (struct varobj
*);
158 static struct varobj
*create_child (struct varobj
*, int, std::string
&);
160 static struct varobj
*
161 create_child_with_value (struct varobj
*parent
, int index
,
162 struct varobj_item
*item
);
164 /* Utility routines */
166 static bool update_type_if_necessary (struct varobj
*var
,
167 struct value
*new_value
);
169 static bool install_new_value (struct varobj
*var
, struct value
*value
,
172 /* Language-specific routines. */
174 static int number_of_children (const struct varobj
*);
176 static std::string
name_of_variable (const struct varobj
*);
178 static std::string
name_of_child (struct varobj
*, int);
180 static struct value
*value_of_root (struct varobj
**var_handle
, bool *);
182 static struct value
*value_of_child (const struct varobj
*parent
, int index
);
184 static std::string
my_value_of_variable (struct varobj
*var
,
185 enum varobj_display_formats format
);
187 static bool is_root_p (const struct varobj
*var
);
189 static struct varobj
*varobj_add_child (struct varobj
*var
,
190 struct varobj_item
*item
);
194 /* Mappings of varobj_display_formats enums to gdb's format codes. */
195 static int format_code
[] = { 0, 't', 'd', 'x', 'o', 'z' };
197 /* List of root variable objects. */
198 static std::list
<struct varobj_root
*> rootlist
;
200 /* Pointer to the varobj hash table (built at run time). */
201 static htab_t varobj_table
;
205 /* API Implementation */
207 is_root_p (const struct varobj
*var
)
209 return (var
->root
->rootvar
== var
);
214 /* See python-internal.h. */
215 gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj
*var
)
216 : gdbpy_enter (var
->root
->gdbarch
, var
->root
->language_defn
)
222 /* Return the full FRAME which corresponds to the given CORE_ADDR
223 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
225 static frame_info_ptr
226 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
228 frame_info_ptr frame
= NULL
;
230 if (frame_addr
== (CORE_ADDR
) 0)
233 for (frame
= get_current_frame ();
235 frame
= get_prev_frame (frame
))
237 /* The CORE_ADDR we get as argument was parsed from a string GDB
238 output as $fp. This output got truncated to gdbarch_addr_bit.
239 Truncate the frame base address in the same manner before
240 comparing it against our argument. */
241 CORE_ADDR frame_base
= get_frame_base_address (frame
);
242 int addr_bit
= gdbarch_addr_bit (get_frame_arch (frame
));
244 if (addr_bit
< (sizeof (CORE_ADDR
) * HOST_CHAR_BIT
))
245 frame_base
&= ((CORE_ADDR
) 1 << addr_bit
) - 1;
247 if (frame_base
== frame_addr
)
254 /* Creates a varobj (not its children). */
257 varobj_create (const char *objname
,
258 const char *expression
, CORE_ADDR frame
, enum varobj_type type
)
260 /* Fill out a varobj structure for the (root) variable being constructed. */
261 auto var
= std::make_unique
<varobj
> (new varobj_root
);
263 if (expression
!= NULL
)
266 struct frame_id old_id
= null_frame_id
;
267 const struct block
*block
;
269 struct value
*value
= NULL
;
272 /* Parse and evaluate the expression, filling in as much of the
273 variable's data as possible. */
275 if (has_stack_frames ())
277 /* Allow creator to specify context of variable. */
278 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
279 fi
= get_selected_frame (NULL
);
281 /* FIXME: cagney/2002-11-23: This code should be doing a
282 lookup using the frame ID and not just the frame's
283 ``address''. This, of course, means an interface
284 change. However, with out that interface change ISAs,
285 such as the ia64 with its two stacks, won't work.
286 Similar goes for the case where there is a frameless
288 fi
= find_frame_addr_in_frame_chain (frame
);
293 if (type
== USE_SELECTED_FRAME
)
294 var
->root
->floating
= true;
300 block
= get_frame_block (fi
, 0);
301 pc
= get_frame_pc (fi
);
306 innermost_block_tracker
tracker (INNERMOST_BLOCK_FOR_SYMBOLS
307 | INNERMOST_BLOCK_FOR_REGISTERS
);
308 /* Wrap the call to parse expression, so we can
309 return a sensible error. */
312 var
->root
->exp
= parse_exp_1 (&p
, pc
, block
, 0, &tracker
);
314 /* Cache gdbarch and language_defn as they might be used even
315 after var is invalidated and var->root->exp cleared. */
316 var
->root
->gdbarch
= var
->root
->exp
->gdbarch
;
317 var
->root
->language_defn
= var
->root
->exp
->language_defn
;
320 catch (const gdb_exception_error
&except
)
325 /* Don't allow variables to be created for types. */
326 enum exp_opcode opcode
= var
->root
->exp
->first_opcode ();
327 if (opcode
== OP_TYPE
328 || opcode
== OP_TYPEOF
329 || opcode
== OP_DECLTYPE
)
331 gdb_printf (gdb_stderr
, "Attempt to use a type name"
332 " as an expression.\n");
336 var
->format
= FORMAT_NATURAL
;
337 var
->root
->valid_block
=
338 var
->root
->floating
? NULL
: tracker
.block ();
340 = var
->root
->floating
? false : var
->root
->valid_block
== nullptr;
341 var
->name
= expression
;
342 /* For a root var, the name and the expr are the same. */
343 var
->path_expr
= expression
;
345 /* When the frame is different from the current frame,
346 we must select the appropriate frame before parsing
347 the expression, otherwise the value will not be current.
348 Since select_frame is so benign, just call it for all cases. */
349 if (var
->root
->valid_block
)
351 /* User could specify explicit FRAME-ADDR which was not found but
352 EXPRESSION is frame specific and we would not be able to evaluate
353 it correctly next time. With VALID_BLOCK set we must also set
354 FRAME and THREAD_ID. */
356 error (_("Failed to find the specified frame"));
358 var
->root
->frame
= get_frame_id (fi
);
359 var
->root
->thread_id
= inferior_thread ()->global_num
;
360 old_id
= get_frame_id (get_selected_frame (NULL
));
364 /* We definitely need to catch errors here. If evaluation of
365 the expression succeeds, we got the value we wanted. But if
366 it fails, we still go on with a call to evaluate_type(). */
369 value
= var
->root
->exp
->evaluate ();
371 catch (const gdb_exception_error
&except
)
373 /* Error getting the value. Try to at least get the
375 struct value
*type_only_value
= var
->root
->exp
->evaluate_type ();
377 var
->type
= type_only_value
->type ();
382 int real_type_found
= 0;
384 var
->type
= value_actual_type (value
, 0, &real_type_found
);
386 value
= value_cast (var
->type
, value
);
389 /* Set language info */
390 var
->root
->lang_ops
= var
->root
->exp
->language_defn
->varobj_ops ();
392 install_new_value (var
.get (), value
, 1 /* Initial assignment */);
394 /* Set ourselves as our root. */
395 var
->root
->rootvar
= var
.get ();
397 /* Reset the selected frame. */
398 if (frame_id_p (old_id
))
399 select_frame (frame_find_by_id (old_id
));
402 /* If the variable object name is null, that means this
403 is a temporary variable, so don't install it. */
405 if ((var
!= NULL
) && (objname
!= NULL
))
407 var
->obj_name
= objname
;
408 install_variable (var
.get ());
411 return var
.release ();
414 /* Generates an unique name that can be used for a varobj. */
417 varobj_gen_name (void)
421 /* Generate a name for this object. */
423 return string_printf ("var%d", id
);
426 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
427 error if OBJNAME cannot be found. */
430 varobj_get_handle (const char *objname
)
432 varobj
*var
= (varobj
*) htab_find_with_hash (varobj_table
, objname
,
433 htab_hash_string (objname
));
436 error (_("Variable object not found"));
441 /* Given the handle, return the name of the object. */
444 varobj_get_objname (const struct varobj
*var
)
446 return var
->obj_name
.c_str ();
449 /* Given the handle, return the expression represented by the
453 varobj_get_expression (const struct varobj
*var
)
455 return name_of_variable (var
);
461 varobj_delete (struct varobj
*var
, bool only_children
)
463 return delete_variable (var
, only_children
);
468 /* Convenience function for varobj_set_visualizer. Instantiate a
469 pretty-printer for a given value. */
471 instantiate_pretty_printer (PyObject
*constructor
, struct value
*value
)
473 gdbpy_ref
<> val_obj (value_to_value_object (value
));
474 if (val_obj
== nullptr)
477 return PyObject_CallFunctionObjArgs (constructor
, val_obj
.get (), NULL
);
482 /* Set/Get variable object display format. */
484 enum varobj_display_formats
485 varobj_set_display_format (struct varobj
*var
,
486 enum varobj_display_formats format
)
488 var
->format
= format
;
490 if (varobj_value_is_changeable_p (var
)
491 && var
->value
!= nullptr && !var
->value
->lazy ())
493 var
->print_value
= varobj_value_get_print_value (var
->value
.get (),
500 enum varobj_display_formats
501 varobj_get_display_format (const struct varobj
*var
)
506 gdb::unique_xmalloc_ptr
<char>
507 varobj_get_display_hint (const struct varobj
*var
)
509 gdb::unique_xmalloc_ptr
<char> result
;
512 if (!gdb_python_initialized
)
515 gdbpy_enter_varobj
enter_py (var
);
517 if (var
->dynamic
->pretty_printer
!= NULL
)
518 result
= gdbpy_get_display_hint (var
->dynamic
->pretty_printer
);
524 /* Return true if the varobj has items after TO, false otherwise. */
527 varobj_has_more (const struct varobj
*var
, int to
)
529 if (var
->children
.size () > to
)
532 return ((to
== -1 || var
->children
.size () == to
)
533 && (var
->dynamic
->saved_item
!= NULL
));
536 /* If the variable object is bound to a specific thread, that
537 is its evaluation can always be done in context of a frame
538 inside that thread, returns GDB id of the thread -- which
539 is always positive. Otherwise, returns -1. */
541 varobj_get_thread_id (const struct varobj
*var
)
543 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
544 return var
->root
->thread_id
;
550 varobj_set_frozen (struct varobj
*var
, bool frozen
)
552 /* When a variable is unfrozen, we don't fetch its value.
553 The 'not_fetched' flag remains set, so next -var-update
556 We don't fetch the value, because for structures the client
557 should do -var-update anyway. It would be bad to have different
558 client-size logic for structure and other types. */
559 var
->frozen
= frozen
;
563 varobj_get_frozen (const struct varobj
*var
)
568 /* A helper function that updates the contents of FROM and TO based on the
569 size of the vector CHILDREN. If the contents of either FROM or TO are
570 negative the entire range is used. */
573 varobj_restrict_range (const std::vector
<varobj
*> &children
,
576 int len
= children
.size ();
578 if (*from
< 0 || *to
< 0)
594 /* A helper for update_dynamic_varobj_children that installs a new
595 child when needed. */
598 install_dynamic_child (struct varobj
*var
,
599 std::vector
<varobj
*> *changed
,
600 std::vector
<varobj
*> *type_changed
,
601 std::vector
<varobj
*> *newobj
,
602 std::vector
<varobj
*> *unchanged
,
605 struct varobj_item
*item
)
607 if (var
->children
.size () < index
+ 1)
609 /* There's no child yet. */
610 struct varobj
*child
= varobj_add_child (var
, item
);
614 newobj
->push_back (child
);
620 varobj
*existing
= var
->children
[index
];
621 bool type_updated
= update_type_if_necessary (existing
,
626 if (type_changed
!= NULL
)
627 type_changed
->push_back (existing
);
629 if (install_new_value (existing
, item
->value
.get (), 0))
631 if (!type_updated
&& changed
!= NULL
)
632 changed
->push_back (existing
);
634 else if (!type_updated
&& unchanged
!= NULL
)
635 unchanged
->push_back (existing
);
639 /* A factory for creating dynamic varobj's iterators. Returns an
640 iterator object suitable for iterating over VAR's children. */
642 static std::unique_ptr
<varobj_iter
>
643 varobj_get_iterator (struct varobj
*var
)
646 if (var
->dynamic
->pretty_printer
)
648 value_print_options opts
;
649 varobj_formatted_print_options (&opts
, var
->format
);
650 return py_varobj_get_iterator (var
, var
->dynamic
->pretty_printer
, &opts
);
654 gdb_assert_not_reached ("requested an iterator from a non-dynamic varobj");
658 update_dynamic_varobj_children (struct varobj
*var
,
659 std::vector
<varobj
*> *changed
,
660 std::vector
<varobj
*> *type_changed
,
661 std::vector
<varobj
*> *newobj
,
662 std::vector
<varobj
*> *unchanged
,
664 bool update_children
,
672 if (update_children
|| var
->dynamic
->child_iter
== NULL
)
674 var
->dynamic
->child_iter
= varobj_get_iterator (var
);
675 var
->dynamic
->saved_item
.reset (nullptr);
679 if (var
->dynamic
->child_iter
== NULL
)
683 i
= var
->children
.size ();
685 /* We ask for one extra child, so that MI can report whether there
686 are more children. */
687 for (; to
< 0 || i
< to
+ 1; ++i
)
689 std::unique_ptr
<varobj_item
> item
;
691 /* See if there was a leftover from last time. */
692 if (var
->dynamic
->saved_item
!= NULL
)
693 item
= std::move (var
->dynamic
->saved_item
);
695 item
= var
->dynamic
->child_iter
->next ();
699 /* Iteration is done. Remove iterator from VAR. */
700 var
->dynamic
->child_iter
.reset (nullptr);
703 /* We don't want to push the extra child on any report list. */
704 if (to
< 0 || i
< to
)
706 bool can_mention
= from
< 0 || i
>= from
;
708 install_dynamic_child (var
, can_mention
? changed
: NULL
,
709 can_mention
? type_changed
: NULL
,
710 can_mention
? newobj
: NULL
,
711 can_mention
? unchanged
: NULL
,
712 can_mention
? cchanged
: NULL
, i
,
717 var
->dynamic
->saved_item
= std::move (item
);
719 /* We want to truncate the child list just before this
725 if (i
< var
->children
.size ())
728 for (int j
= i
; j
< var
->children
.size (); ++j
)
729 varobj_delete (var
->children
[j
], 0);
731 var
->children
.resize (i
);
734 /* If there are fewer children than requested, note that the list of
736 if (to
>= 0 && var
->children
.size () < to
)
739 var
->num_children
= var
->children
.size ();
745 varobj_get_num_children (struct varobj
*var
)
747 if (var
->num_children
== -1)
749 if (varobj_is_dynamic_p (var
))
753 /* If we have a dynamic varobj, don't report -1 children.
754 So, try to fetch some children first. */
755 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
, &dummy
,
759 var
->num_children
= number_of_children (var
);
762 return var
->num_children
>= 0 ? var
->num_children
: 0;
765 /* Creates a list of the immediate children of a variable object;
766 the return code is the number of such children or -1 on error. */
768 const std::vector
<varobj
*> &
769 varobj_list_children (struct varobj
*var
, int *from
, int *to
)
771 var
->dynamic
->children_requested
= true;
773 if (varobj_is_dynamic_p (var
))
775 bool children_changed
;
777 /* This, in theory, can result in the number of children changing without
778 frontend noticing. But well, calling -var-list-children on the same
779 varobj twice is not something a sane frontend would do. */
780 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
,
781 &children_changed
, false, 0, *to
);
782 varobj_restrict_range (var
->children
, from
, to
);
783 return var
->children
;
786 if (var
->num_children
== -1)
787 var
->num_children
= number_of_children (var
);
789 /* If that failed, give up. */
790 if (var
->num_children
== -1)
791 return var
->children
;
793 /* If we're called when the list of children is not yet initialized,
794 allocate enough elements in it. */
795 while (var
->children
.size () < var
->num_children
)
796 var
->children
.push_back (NULL
);
798 for (int i
= 0; i
< var
->num_children
; i
++)
800 if (var
->children
[i
] == NULL
)
802 /* Either it's the first call to varobj_list_children for
803 this variable object, and the child was never created,
804 or it was explicitly deleted by the client. */
805 std::string name
= name_of_child (var
, i
);
806 var
->children
[i
] = create_child (var
, i
, name
);
810 varobj_restrict_range (var
->children
, from
, to
);
811 return var
->children
;
814 static struct varobj
*
815 varobj_add_child (struct varobj
*var
, struct varobj_item
*item
)
817 varobj
*v
= create_child_with_value (var
, var
->children
.size (), item
);
819 var
->children
.push_back (v
);
824 /* Obtain the type of an object Variable as a string similar to the one gdb
825 prints on the console. The caller is responsible for freeing the string.
829 varobj_get_type (struct varobj
*var
)
831 /* For the "fake" variables, do not return a type. (Its type is
833 Do not return a type for invalid variables as well. */
834 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
835 return std::string ();
837 return type_to_string (var
->type
);
840 /* Obtain the type of an object variable. */
843 varobj_get_gdb_type (const struct varobj
*var
)
848 /* Is VAR a path expression parent, i.e., can it be used to construct
849 a valid path expression? */
852 is_path_expr_parent (const struct varobj
*var
)
854 gdb_assert (var
->root
->lang_ops
->is_path_expr_parent
!= NULL
);
855 return var
->root
->lang_ops
->is_path_expr_parent (var
);
858 /* Is VAR a path expression parent, i.e., can it be used to construct
859 a valid path expression? By default we assume any VAR can be a path
863 varobj_default_is_path_expr_parent (const struct varobj
*var
)
868 /* Return the path expression parent for VAR. */
870 const struct varobj
*
871 varobj_get_path_expr_parent (const struct varobj
*var
)
873 const struct varobj
*parent
= var
;
875 while (!is_root_p (parent
) && !is_path_expr_parent (parent
))
876 parent
= parent
->parent
;
878 /* Computation of full rooted expression for children of dynamic
879 varobjs is not supported. */
880 if (varobj_is_dynamic_p (parent
))
881 error (_("Invalid variable object (child of a dynamic varobj)"));
886 /* Return a pointer to the full rooted expression of varobj VAR.
887 If it has not been computed yet, compute it. */
890 varobj_get_path_expr (const struct varobj
*var
)
892 if (var
->path_expr
.empty ())
894 /* For root varobjs, we initialize path_expr
895 when creating varobj, so here it should be
897 struct varobj
*mutable_var
= (struct varobj
*) var
;
898 gdb_assert (!is_root_p (var
));
900 mutable_var
->path_expr
= (*var
->root
->lang_ops
->path_expr_of_child
) (var
);
903 return var
->path_expr
.c_str ();
906 const struct language_defn
*
907 varobj_get_language (const struct varobj
*var
)
909 return var
->root
->exp
->language_defn
;
913 varobj_get_attributes (const struct varobj
*var
)
917 if (varobj_editable_p (var
))
918 /* FIXME: define masks for attributes. */
919 attributes
|= 0x00000001; /* Editable */
924 /* Return true if VAR is a dynamic varobj. */
927 varobj_is_dynamic_p (const struct varobj
*var
)
929 return var
->dynamic
->pretty_printer
!= NULL
;
933 varobj_get_formatted_value (struct varobj
*var
,
934 enum varobj_display_formats format
)
936 return my_value_of_variable (var
, format
);
940 varobj_get_value (struct varobj
*var
)
942 return my_value_of_variable (var
, var
->format
);
945 /* Set the value of an object variable (if it is editable) to the
946 value of the given expression. */
947 /* Note: Invokes functions that can call error(). */
950 varobj_set_value (struct varobj
*var
, const char *expression
)
952 struct value
*val
= NULL
; /* Initialize to keep gcc happy. */
953 /* The argument "expression" contains the variable's new value.
954 We need to first construct a legal expression for this -- ugh! */
955 /* Does this cover all the bases? */
956 struct value
*value
= NULL
; /* Initialize to keep gcc happy. */
957 const char *s
= expression
;
959 gdb_assert (varobj_editable_p (var
));
961 /* ALWAYS reset to decimal temporarily. */
962 auto save_input_radix
= make_scoped_restore (&input_radix
, 10);
963 expression_up exp
= parse_exp_1 (&s
, 0, 0, 0);
966 value
= exp
->evaluate ();
969 catch (const gdb_exception_error
&except
)
971 /* We cannot proceed without a valid expression. */
975 /* All types that are editable must also be changeable. */
976 gdb_assert (varobj_value_is_changeable_p (var
));
978 /* The value of a changeable variable object must not be lazy. */
979 gdb_assert (!var
->value
->lazy ());
981 /* Need to coerce the input. We want to check if the
982 value of the variable object will be different
983 after assignment, and the first thing value_assign
984 does is coerce the input.
985 For example, if we are assigning an array to a pointer variable we
986 should compare the pointer with the array's address, not with the
988 value
= coerce_array (value
);
990 /* The new value may be lazy. value_assign, or
991 rather value_contents, will take care of this. */
994 val
= value_assign (var
->value
.get (), value
);
997 catch (const gdb_exception_error
&except
)
1002 /* If the value has changed, record it, so that next -var-update can
1003 report this change. If a variable had a value of '1', we've set it
1004 to '333' and then set again to '1', when -var-update will report this
1005 variable as changed -- because the first assignment has set the
1006 'updated' flag. There's no need to optimize that, because return value
1007 of -var-update should be considered an approximation. */
1008 var
->updated
= install_new_value (var
, val
, false /* Compare values. */);
1014 /* A helper function to install a constructor function and visualizer
1015 in a varobj_dynamic. */
1018 install_visualizer (struct varobj_dynamic
*var
, PyObject
*constructor
,
1019 PyObject
*visualizer
)
1021 Py_XDECREF (var
->constructor
);
1022 var
->constructor
= constructor
;
1024 Py_XDECREF (var
->pretty_printer
);
1025 var
->pretty_printer
= visualizer
;
1027 var
->child_iter
.reset (nullptr);
1030 /* Install the default visualizer for VAR. */
1033 install_default_visualizer (struct varobj
*var
)
1035 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1036 if (CPLUS_FAKE_CHILD (var
))
1039 if (pretty_printing
)
1041 gdbpy_ref
<> pretty_printer
;
1043 if (var
->value
!= nullptr)
1045 pretty_printer
= gdbpy_get_varobj_pretty_printer (var
->value
.get ());
1046 if (pretty_printer
== nullptr)
1048 gdbpy_print_stack ();
1049 error (_("Cannot instantiate printer for default visualizer"));
1053 if (pretty_printer
== Py_None
)
1054 pretty_printer
.reset (nullptr);
1056 install_visualizer (var
->dynamic
, NULL
, pretty_printer
.release ());
1060 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1061 make a new object. */
1064 construct_visualizer (struct varobj
*var
, PyObject
*constructor
)
1066 PyObject
*pretty_printer
;
1068 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1069 if (CPLUS_FAKE_CHILD (var
))
1072 Py_INCREF (constructor
);
1073 if (constructor
== Py_None
)
1074 pretty_printer
= NULL
;
1077 pretty_printer
= instantiate_pretty_printer (constructor
,
1079 if (! pretty_printer
)
1081 gdbpy_print_stack ();
1082 Py_DECREF (constructor
);
1083 constructor
= Py_None
;
1084 Py_INCREF (constructor
);
1087 if (pretty_printer
== Py_None
)
1089 Py_DECREF (pretty_printer
);
1090 pretty_printer
= NULL
;
1094 install_visualizer (var
->dynamic
, constructor
, pretty_printer
);
1097 #endif /* HAVE_PYTHON */
1099 /* A helper function for install_new_value. This creates and installs
1100 a visualizer for VAR, if appropriate. */
1103 install_new_value_visualizer (struct varobj
*var
)
1106 /* If the constructor is None, then we want the raw value. If VAR
1107 does not have a value, just skip this. */
1108 if (!gdb_python_initialized
)
1111 if (var
->dynamic
->constructor
!= Py_None
&& var
->value
!= NULL
)
1113 gdbpy_enter_varobj
enter_py (var
);
1115 if (var
->dynamic
->constructor
== NULL
)
1116 install_default_visualizer (var
);
1118 construct_visualizer (var
, var
->dynamic
->constructor
);
1125 /* When using RTTI to determine variable type it may be changed in runtime when
1126 the variable value is changed. This function checks whether type of varobj
1127 VAR will change when a new value NEW_VALUE is assigned and if it is so
1128 updates the type of VAR. */
1131 update_type_if_necessary (struct varobj
*var
, struct value
*new_value
)
1135 struct value_print_options opts
;
1137 get_user_print_options (&opts
);
1138 if (opts
.objectprint
)
1140 struct type
*new_type
= value_actual_type (new_value
, 0, 0);
1141 std::string new_type_str
= type_to_string (new_type
);
1142 std::string curr_type_str
= varobj_get_type (var
);
1144 /* Did the type name change? */
1145 if (curr_type_str
!= new_type_str
)
1147 var
->type
= new_type
;
1149 /* This information may be not valid for a new type. */
1150 varobj_delete (var
, 1);
1151 var
->children
.clear ();
1152 var
->num_children
= -1;
1161 /* Assign a new value to a variable object. If INITIAL is true,
1162 this is the first assignment after the variable object was just
1163 created, or changed type. In that case, just assign the value
1165 Otherwise, assign the new value, and return true if the value is
1166 different from the current one, false otherwise. The comparison is
1167 done on textual representation of value. Therefore, some types
1168 need not be compared. E.g. for structures the reported value is
1169 always "{...}", so no comparison is necessary here. If the old
1170 value was NULL and new one is not, or vice versa, we always return true.
1172 The VALUE parameter should not be released -- the function will
1173 take care of releasing it when needed. */
1175 install_new_value (struct varobj
*var
, struct value
*value
, bool initial
)
1179 bool changed
= false;
1180 bool intentionally_not_fetched
= false;
1182 /* We need to know the varobj's type to decide if the value should
1183 be fetched or not. C++ fake children (public/protected/private)
1184 don't have a type. */
1185 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
1186 changeable
= varobj_value_is_changeable_p (var
);
1188 /* If the type has custom visualizer, we consider it to be always
1189 changeable. FIXME: need to make sure this behaviour will not
1190 mess up read-sensitive values. */
1191 if (var
->dynamic
->pretty_printer
!= NULL
)
1194 need_to_fetch
= changeable
;
1196 /* We are not interested in the address of references, and given
1197 that in C++ a reference is not rebindable, it cannot
1198 meaningfully change. So, get hold of the real value. */
1200 value
= coerce_ref (value
);
1202 if (var
->type
&& var
->type
->code () == TYPE_CODE_UNION
)
1203 /* For unions, we need to fetch the value implicitly because
1204 of implementation of union member fetch. When gdb
1205 creates a value for a field and the value of the enclosing
1206 structure is not lazy, it immediately copies the necessary
1207 bytes from the enclosing values. If the enclosing value is
1208 lazy, the call to value_fetch_lazy on the field will read
1209 the data from memory. For unions, that means we'll read the
1210 same memory more than once, which is not desirable. So
1212 need_to_fetch
= true;
1214 /* The new value might be lazy. If the type is changeable,
1215 that is we'll be comparing values of this type, fetch the
1216 value now. Otherwise, on the next update the old value
1217 will be lazy, which means we've lost that old value. */
1218 if (need_to_fetch
&& value
&& value
->lazy ())
1220 const struct varobj
*parent
= var
->parent
;
1221 bool frozen
= var
->frozen
;
1223 for (; !frozen
&& parent
; parent
= parent
->parent
)
1224 frozen
|= parent
->frozen
;
1226 if (frozen
&& initial
)
1228 /* For variables that are frozen, or are children of frozen
1229 variables, we don't do fetch on initial assignment.
1230 For non-initial assignment we do the fetch, since it means we're
1231 explicitly asked to compare the new value with the old one. */
1232 intentionally_not_fetched
= true;
1239 value
->fetch_lazy ();
1242 catch (const gdb_exception_error
&except
)
1244 /* Set the value to NULL, so that for the next -var-update,
1245 we don't try to compare the new value with this value,
1246 that we couldn't even read. */
1252 /* Get a reference now, before possibly passing it to any Python
1253 code that might release it. */
1254 value_ref_ptr value_holder
;
1256 value_holder
= value_ref_ptr::new_reference (value
);
1258 /* Below, we'll be comparing string rendering of old and new
1259 values. Don't get string rendering if the value is
1260 lazy -- if it is, the code above has decided that the value
1261 should not be fetched. */
1262 std::string print_value
;
1263 if (value
!= NULL
&& !value
->lazy ()
1264 && var
->dynamic
->pretty_printer
== NULL
)
1265 print_value
= varobj_value_get_print_value (value
, var
->format
, var
);
1267 /* If the type is changeable, compare the old and the new values.
1268 If this is the initial assignment, we don't have any old value
1270 if (!initial
&& changeable
)
1272 /* If the value of the varobj was changed by -var-set-value,
1273 then the value in the varobj and in the target is the same.
1274 However, that value is different from the value that the
1275 varobj had after the previous -var-update. So need to the
1276 varobj as changed. */
1279 else if (var
->dynamic
->pretty_printer
== NULL
)
1281 /* Try to compare the values. That requires that both
1282 values are non-lazy. */
1283 if (var
->not_fetched
&& var
->value
->lazy ())
1285 /* This is a frozen varobj and the value was never read.
1286 Presumably, UI shows some "never read" indicator.
1287 Now that we've fetched the real value, we need to report
1288 this varobj as changed so that UI can show the real
1292 else if (var
->value
== NULL
&& value
== NULL
)
1295 else if (var
->value
== NULL
|| value
== NULL
)
1301 gdb_assert (!var
->value
->lazy ());
1302 gdb_assert (!value
->lazy ());
1304 gdb_assert (!var
->print_value
.empty () && !print_value
.empty ());
1305 if (var
->print_value
!= print_value
)
1311 if (!initial
&& !changeable
)
1313 /* For values that are not changeable, we don't compare the values.
1314 However, we want to notice if a value was not NULL and now is NULL,
1315 or vise versa, so that we report when top-level varobjs come in scope
1316 and leave the scope. */
1317 changed
= (var
->value
!= NULL
) != (value
!= NULL
);
1320 /* We must always keep the new value, since children depend on it. */
1321 var
->value
= value_holder
;
1322 if (value
&& value
->lazy () && intentionally_not_fetched
)
1323 var
->not_fetched
= true;
1325 var
->not_fetched
= false;
1326 var
->updated
= false;
1328 install_new_value_visualizer (var
);
1330 /* If we installed a pretty-printer, re-compare the printed version
1331 to see if the variable changed. */
1332 if (var
->dynamic
->pretty_printer
!= NULL
)
1334 print_value
= varobj_value_get_print_value (var
->value
.get (),
1336 if (var
->print_value
!= print_value
)
1339 var
->print_value
= print_value
;
1341 gdb_assert (var
->value
== nullptr || var
->value
->type ());
1346 /* Return the requested range for a varobj. VAR is the varobj. FROM
1347 and TO are out parameters; *FROM and *TO will be set to the
1348 selected sub-range of VAR. If no range was selected using
1349 -var-set-update-range, then both will be -1. */
1351 varobj_get_child_range (const struct varobj
*var
, int *from
, int *to
)
1357 /* Set the selected sub-range of children of VAR to start at index
1358 FROM and end at index TO. If either FROM or TO is less than zero,
1359 this is interpreted as a request for all children. */
1361 varobj_set_child_range (struct varobj
*var
, int from
, int to
)
1368 varobj_set_visualizer (struct varobj
*var
, const char *visualizer
)
1373 if (!gdb_python_initialized
)
1376 gdbpy_enter_varobj
enter_py (var
);
1378 mainmod
= PyImport_AddModule ("__main__");
1380 = gdbpy_ref
<>::new_reference (PyModule_GetDict (mainmod
));
1381 gdbpy_ref
<> constructor (PyRun_String (visualizer
, Py_eval_input
,
1382 globals
.get (), globals
.get ()));
1384 if (constructor
== NULL
)
1386 gdbpy_print_stack ();
1387 error (_("Could not evaluate visualizer expression: %s"), visualizer
);
1390 construct_visualizer (var
, constructor
.get ());
1392 /* If there are any children now, wipe them. */
1393 varobj_delete (var
, 1 /* children only */);
1394 var
->num_children
= -1;
1396 /* Also be sure to reset the print value. */
1397 varobj_set_display_format (var
, var
->format
);
1399 error (_("Python support required"));
1403 /* If NEW_VALUE is the new value of the given varobj (var), return
1404 true if var has mutated. In other words, if the type of
1405 the new value is different from the type of the varobj's old
1408 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1411 varobj_value_has_mutated (const struct varobj
*var
, struct value
*new_value
,
1412 struct type
*new_type
)
1414 /* If we haven't previously computed the number of children in var,
1415 it does not matter from the front-end's perspective whether
1416 the type has mutated or not. For all intents and purposes,
1417 it has not mutated. */
1418 if (var
->num_children
< 0)
1421 if (var
->root
->lang_ops
->value_has_mutated
!= NULL
)
1423 /* The varobj module, when installing new values, explicitly strips
1424 references, saying that we're not interested in those addresses.
1425 But detection of mutation happens before installing the new
1426 value, so our value may be a reference that we need to strip
1427 in order to remain consistent. */
1428 if (new_value
!= NULL
)
1429 new_value
= coerce_ref (new_value
);
1430 return var
->root
->lang_ops
->value_has_mutated (var
, new_value
, new_type
);
1436 /* Update the values for a variable and its children. This is a
1437 two-pronged attack. First, re-parse the value for the root's
1438 expression to see if it's changed. Then go all the way
1439 through its children, reconstructing them and noting if they've
1442 The IS_EXPLICIT parameter specifies if this call is result
1443 of MI request to update this specific variable, or
1444 result of implicit -var-update *. For implicit request, we don't
1445 update frozen variables.
1447 NOTE: This function may delete the caller's varobj. If it
1448 returns TYPE_CHANGED, then it has done this and VARP will be modified
1449 to point to the new varobj. */
1451 std::vector
<varobj_update_result
>
1452 varobj_update (struct varobj
**varp
, bool is_explicit
)
1454 bool type_changed
= false;
1455 struct value
*newobj
;
1456 std::vector
<varobj_update_result
> stack
;
1457 std::vector
<varobj_update_result
> result
;
1459 /* Frozen means frozen -- we don't check for any change in
1460 this varobj, including its going out of scope, or
1461 changing type. One use case for frozen varobjs is
1462 retaining previously evaluated expressions, and we don't
1463 want them to be reevaluated at all. */
1464 if (!is_explicit
&& (*varp
)->frozen
)
1467 if (!(*varp
)->root
->is_valid
)
1469 result
.emplace_back (*varp
, VAROBJ_INVALID
);
1473 if ((*varp
)->root
->rootvar
== *varp
)
1475 varobj_update_result
r (*varp
);
1477 /* Update the root variable. value_of_root can return NULL
1478 if the variable is no longer around, i.e. we stepped out of
1479 the frame in which a local existed. We are letting the
1480 value_of_root variable dispose of the varobj if the type
1482 newobj
= value_of_root (varp
, &type_changed
);
1483 if (update_type_if_necessary (*varp
, newobj
))
1484 type_changed
= true;
1486 r
.type_changed
= type_changed
;
1487 if (install_new_value ((*varp
), newobj
, type_changed
))
1491 r
.status
= VAROBJ_NOT_IN_SCOPE
;
1492 r
.value_installed
= true;
1494 if (r
.status
== VAROBJ_NOT_IN_SCOPE
)
1496 if (r
.type_changed
|| r
.changed
)
1497 result
.push_back (std::move (r
));
1502 stack
.push_back (std::move (r
));
1505 stack
.emplace_back (*varp
);
1507 /* Walk through the children, reconstructing them all. */
1508 while (!stack
.empty ())
1510 varobj_update_result r
= std::move (stack
.back ());
1512 struct varobj
*v
= r
.varobj
;
1514 /* Update this variable, unless it's a root, which is already
1516 if (!r
.value_installed
)
1518 struct type
*new_type
;
1520 newobj
= value_of_child (v
->parent
, v
->index
);
1521 if (update_type_if_necessary (v
, newobj
))
1522 r
.type_changed
= true;
1524 new_type
= newobj
->type ();
1526 new_type
= v
->root
->lang_ops
->type_of_child (v
->parent
, v
->index
);
1528 if (varobj_value_has_mutated (v
, newobj
, new_type
))
1530 /* The children are no longer valid; delete them now.
1531 Report the fact that its type changed as well. */
1532 varobj_delete (v
, 1 /* only_children */);
1533 v
->num_children
= -1;
1537 r
.type_changed
= true;
1540 if (install_new_value (v
, newobj
, r
.type_changed
))
1547 /* We probably should not get children of a dynamic varobj, but
1548 for which -var-list-children was never invoked. */
1549 if (varobj_is_dynamic_p (v
))
1551 std::vector
<varobj
*> changed
, type_changed_vec
, unchanged
, newobj_vec
;
1552 bool children_changed
= false;
1557 if (!v
->dynamic
->children_requested
)
1561 /* If we initially did not have potential children, but
1562 now we do, consider the varobj as changed.
1563 Otherwise, if children were never requested, consider
1564 it as unchanged -- presumably, such varobj is not yet
1565 expanded in the UI, so we need not bother getting
1567 if (!varobj_has_more (v
, 0))
1569 update_dynamic_varobj_children (v
, NULL
, NULL
, NULL
, NULL
,
1570 &dummy
, false, 0, 0);
1571 if (varobj_has_more (v
, 0))
1576 result
.push_back (std::move (r
));
1581 /* If update_dynamic_varobj_children returns false, then we have
1582 a non-conforming pretty-printer, so we skip it. */
1583 if (update_dynamic_varobj_children (v
, &changed
, &type_changed_vec
,
1585 &unchanged
, &children_changed
,
1586 true, v
->from
, v
->to
))
1588 if (children_changed
|| !newobj_vec
.empty ())
1590 r
.children_changed
= true;
1591 r
.newobj
= std::move (newobj_vec
);
1593 /* Push in reverse order so that the first child is
1594 popped from the work stack first, and so will be
1595 added to result first. This does not affect
1596 correctness, just "nicer". */
1597 for (int i
= type_changed_vec
.size () - 1; i
>= 0; --i
)
1599 varobj_update_result
item (type_changed_vec
[i
]);
1601 /* Type may change only if value was changed. */
1602 item
.changed
= true;
1603 item
.type_changed
= true;
1604 item
.value_installed
= true;
1606 stack
.push_back (std::move (item
));
1608 for (int i
= changed
.size () - 1; i
>= 0; --i
)
1610 varobj_update_result
item (changed
[i
]);
1612 item
.changed
= true;
1613 item
.value_installed
= true;
1615 stack
.push_back (std::move (item
));
1617 for (int i
= unchanged
.size () - 1; i
>= 0; --i
)
1619 if (!unchanged
[i
]->frozen
)
1621 varobj_update_result
item (unchanged
[i
]);
1623 item
.value_installed
= true;
1625 stack
.push_back (std::move (item
));
1628 if (r
.changed
|| r
.children_changed
)
1629 result
.push_back (std::move (r
));
1635 /* Push any children. Use reverse order so that the first
1636 child is popped from the work stack first, and so
1637 will be added to result first. This does not
1638 affect correctness, just "nicer". */
1639 for (int i
= v
->children
.size () - 1; i
>= 0; --i
)
1641 varobj
*c
= v
->children
[i
];
1643 /* Child may be NULL if explicitly deleted by -var-delete. */
1644 if (c
!= NULL
&& !c
->frozen
)
1645 stack
.emplace_back (c
);
1648 if (r
.changed
|| r
.type_changed
)
1649 result
.push_back (std::move (r
));
1655 /* Helper functions */
1658 * Variable object construction/destruction
1662 delete_variable (struct varobj
*var
, bool only_children_p
)
1666 delete_variable_1 (&delcount
, var
, only_children_p
,
1667 true /* remove_from_parent_p */ );
1672 /* Delete the variable object VAR and its children. */
1673 /* IMPORTANT NOTE: If we delete a variable which is a child
1674 and the parent is not removed we dump core. It must be always
1675 initially called with remove_from_parent_p set. */
1677 delete_variable_1 (int *delcountp
, struct varobj
*var
, bool only_children_p
,
1678 bool remove_from_parent_p
)
1680 /* Delete any children of this variable, too. */
1681 for (varobj
*child
: var
->children
)
1686 if (!remove_from_parent_p
)
1687 child
->parent
= NULL
;
1689 delete_variable_1 (delcountp
, child
, false, only_children_p
);
1691 var
->children
.clear ();
1693 /* if we were called to delete only the children we are done here. */
1694 if (only_children_p
)
1697 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
1698 /* If the name is empty, this is a temporary variable, that has not
1699 yet been installed, don't report it, it belongs to the caller... */
1700 if (!var
->obj_name
.empty ())
1702 *delcountp
= *delcountp
+ 1;
1705 /* If this variable has a parent, remove it from its parent's list. */
1706 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1707 (as indicated by remove_from_parent_p) we don't bother doing an
1708 expensive list search to find the element to remove when we are
1709 discarding the list afterwards. */
1710 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1711 var
->parent
->children
[var
->index
] = NULL
;
1713 if (!var
->obj_name
.empty ())
1714 uninstall_variable (var
);
1716 /* Free memory associated with this variable. */
1720 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1722 install_variable (struct varobj
*var
)
1724 hashval_t hash
= htab_hash_string (var
->obj_name
.c_str ());
1725 void **slot
= htab_find_slot_with_hash (varobj_table
,
1726 var
->obj_name
.c_str (),
1728 if (*slot
!= nullptr)
1729 error (_("Duplicate variable object name"));
1731 /* Add varobj to hash table. */
1734 /* If root, add varobj to root list. */
1735 if (is_root_p (var
))
1736 rootlist
.push_front (var
->root
);
1739 /* Uninstall the object VAR. */
1741 uninstall_variable (struct varobj
*var
)
1743 hashval_t hash
= htab_hash_string (var
->obj_name
.c_str ());
1744 htab_remove_elt_with_hash (varobj_table
, var
->obj_name
.c_str (), hash
);
1747 gdb_printf (gdb_stdlog
, "Deleting %s\n", var
->obj_name
.c_str ());
1749 /* If root, remove varobj from root list. */
1750 if (is_root_p (var
))
1752 auto iter
= std::find (rootlist
.begin (), rootlist
.end (), var
->root
);
1753 rootlist
.erase (iter
);
1757 /* Create and install a child of the parent of the given name.
1759 The created VAROBJ takes ownership of the allocated NAME. */
1761 static struct varobj
*
1762 create_child (struct varobj
*parent
, int index
, std::string
&name
)
1764 struct varobj_item item
;
1766 std::swap (item
.name
, name
);
1767 item
.value
= release_value (value_of_child (parent
, index
));
1769 return create_child_with_value (parent
, index
, &item
);
1772 static struct varobj
*
1773 create_child_with_value (struct varobj
*parent
, int index
,
1774 struct varobj_item
*item
)
1776 varobj
*child
= new varobj (parent
->root
);
1778 /* NAME is allocated by caller. */
1779 std::swap (child
->name
, item
->name
);
1780 child
->index
= index
;
1781 child
->parent
= parent
;
1783 if (varobj_is_anonymous_child (child
))
1784 child
->obj_name
= string_printf ("%s.%d_anonymous",
1785 parent
->obj_name
.c_str (), index
);
1787 child
->obj_name
= string_printf ("%s.%s",
1788 parent
->obj_name
.c_str (),
1789 child
->name
.c_str ());
1791 install_variable (child
);
1793 /* Compute the type of the child. Must do this before
1794 calling install_new_value. */
1795 if (item
->value
!= NULL
)
1796 /* If the child had no evaluation errors, var->value
1797 will be non-NULL and contain a valid type. */
1798 child
->type
= value_actual_type (item
->value
.get (), 0, NULL
);
1800 /* Otherwise, we must compute the type. */
1801 child
->type
= (*child
->root
->lang_ops
->type_of_child
) (child
->parent
,
1803 install_new_value (child
, item
->value
.get (), 1);
1810 * Miscellaneous utility functions.
1813 /* Allocate memory and initialize a new variable. */
1814 varobj::varobj (varobj_root
*root_
)
1815 : root (root_
), dynamic (new varobj_dynamic
)
1819 /* Free any allocated memory associated with VAR. */
1826 if (var
->dynamic
->pretty_printer
!= NULL
)
1828 gdbpy_enter_varobj
enter_py (var
);
1830 Py_XDECREF (var
->dynamic
->constructor
);
1831 Py_XDECREF (var
->dynamic
->pretty_printer
);
1835 /* This must be deleted before the root object, because Python-based
1836 destructors need access to some components. */
1837 delete var
->dynamic
;
1839 if (is_root_p (var
))
1843 /* Return the type of the value that's stored in VAR,
1844 or that would have being stored there if the
1845 value were accessible.
1847 This differs from VAR->type in that VAR->type is always
1848 the true type of the expression in the source language.
1849 The return value of this function is the type we're
1850 actually storing in varobj, and using for displaying
1851 the values and for comparing previous and new values.
1853 For example, top-level references are always stripped. */
1855 varobj_get_value_type (const struct varobj
*var
)
1859 if (var
->value
!= nullptr)
1860 type
= var
->value
->type ();
1864 type
= check_typedef (type
);
1866 if (TYPE_IS_REFERENCE (type
))
1867 type
= get_target_type (type
);
1869 type
= check_typedef (type
);
1875 * Language-dependencies
1878 /* Common entry points */
1880 /* Return the number of children for a given variable.
1881 The result of this function is defined by the language
1882 implementation. The number of children returned by this function
1883 is the number of children that the user will see in the variable
1886 number_of_children (const struct varobj
*var
)
1888 return (*var
->root
->lang_ops
->number_of_children
) (var
);
1891 /* What is the expression for the root varobj VAR? */
1894 name_of_variable (const struct varobj
*var
)
1896 return (*var
->root
->lang_ops
->name_of_variable
) (var
);
1899 /* What is the name of the INDEX'th child of VAR? */
1902 name_of_child (struct varobj
*var
, int index
)
1904 return (*var
->root
->lang_ops
->name_of_child
) (var
, index
);
1907 /* If frame associated with VAR can be found, switch
1908 to it and return true. Otherwise, return false. */
1911 check_scope (const struct varobj
*var
)
1916 fi
= frame_find_by_id (var
->root
->frame
);
1921 CORE_ADDR pc
= get_frame_pc (fi
);
1923 if (pc
< var
->root
->valid_block
->start () ||
1924 pc
>= var
->root
->valid_block
->end ())
1932 /* Helper function to value_of_root. */
1934 static struct value
*
1935 value_of_root_1 (struct varobj
**var_handle
)
1937 struct value
*new_val
= NULL
;
1938 struct varobj
*var
= *var_handle
;
1939 bool within_scope
= false;
1941 /* Only root variables can be updated... */
1942 if (!is_root_p (var
))
1943 /* Not a root var. */
1946 scoped_restore_current_thread restore_thread
;
1948 /* Determine whether the variable is still around. */
1949 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
1950 within_scope
= true;
1951 else if (var
->root
->thread_id
== 0)
1953 /* The program was single-threaded when the variable object was
1954 created. Technically, it's possible that the program became
1955 multi-threaded since then, but we don't support such
1957 within_scope
= check_scope (var
);
1961 thread_info
*thread
= find_thread_global_id (var
->root
->thread_id
);
1965 switch_to_thread (thread
);
1966 within_scope
= check_scope (var
);
1973 /* We need to catch errors here, because if evaluate
1974 expression fails we want to just return NULL. */
1977 new_val
= var
->root
->exp
->evaluate ();
1979 catch (const gdb_exception_error
&except
)
1987 /* What is the ``struct value *'' of the root variable VAR?
1988 For floating variable object, evaluation can get us a value
1989 of different type from what is stored in varobj already. In
1991 - *type_changed will be set to 1
1992 - old varobj will be freed, and new one will be
1993 created, with the same name.
1994 - *var_handle will be set to the new varobj
1995 Otherwise, *type_changed will be set to 0. */
1996 static struct value
*
1997 value_of_root (struct varobj
**var_handle
, bool *type_changed
)
2001 if (var_handle
== NULL
)
2006 /* This should really be an exception, since this should
2007 only get called with a root variable. */
2009 if (!is_root_p (var
))
2012 if (var
->root
->floating
)
2014 struct varobj
*tmp_var
;
2016 tmp_var
= varobj_create (NULL
, var
->name
.c_str (), (CORE_ADDR
) 0,
2017 USE_SELECTED_FRAME
);
2018 if (tmp_var
== NULL
)
2022 std::string old_type
= varobj_get_type (var
);
2023 std::string new_type
= varobj_get_type (tmp_var
);
2024 if (old_type
== new_type
)
2026 /* The expression presently stored inside var->root->exp
2027 remembers the locations of local variables relatively to
2028 the frame where the expression was created (in DWARF location
2029 button, for example). Naturally, those locations are not
2030 correct in other frames, so update the expression. */
2032 std::swap (var
->root
->exp
, tmp_var
->root
->exp
);
2034 varobj_delete (tmp_var
, 0);
2039 tmp_var
->obj_name
= var
->obj_name
;
2040 tmp_var
->from
= var
->from
;
2041 tmp_var
->to
= var
->to
;
2042 varobj_delete (var
, 0);
2044 install_variable (tmp_var
);
2045 *var_handle
= tmp_var
;
2047 *type_changed
= true;
2056 struct value
*value
;
2058 value
= value_of_root_1 (var_handle
);
2059 if (var
->value
== NULL
|| value
== NULL
)
2061 /* For root varobj-s, a NULL value indicates a scoping issue.
2062 So, nothing to do in terms of checking for mutations. */
2064 else if (varobj_value_has_mutated (var
, value
, value
->type ()))
2066 /* The type has mutated, so the children are no longer valid.
2067 Just delete them, and tell our caller that the type has
2069 varobj_delete (var
, 1 /* only_children */);
2070 var
->num_children
= -1;
2073 *type_changed
= true;
2079 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2080 static struct value
*
2081 value_of_child (const struct varobj
*parent
, int index
)
2083 struct value
*value
;
2085 value
= (*parent
->root
->lang_ops
->value_of_child
) (parent
, index
);
2090 /* GDB already has a command called "value_of_variable". Sigh. */
2092 my_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2094 if (var
->root
->is_valid
)
2096 if (var
->dynamic
->pretty_printer
!= NULL
)
2097 return varobj_value_get_print_value (var
->value
.get (), var
->format
,
2099 else if (var
->parent
!= nullptr && varobj_is_dynamic_p (var
->parent
))
2100 return var
->print_value
;
2102 return (*var
->root
->lang_ops
->value_of_variable
) (var
, format
);
2105 return std::string ();
2109 varobj_formatted_print_options (struct value_print_options
*opts
,
2110 enum varobj_display_formats format
)
2112 get_formatted_print_options (opts
, format_code
[(int) format
]);
2113 opts
->deref_ref
= false;
2114 opts
->raw
= !pretty_printing
;
2118 varobj_value_get_print_value (struct value
*value
,
2119 enum varobj_display_formats format
,
2120 const struct varobj
*var
)
2122 struct value_print_options opts
;
2123 struct type
*type
= NULL
;
2125 gdb::unique_xmalloc_ptr
<char> encoding
;
2126 /* Initialize it just to avoid a GCC false warning. */
2127 CORE_ADDR str_addr
= 0;
2128 bool string_print
= false;
2131 return std::string ();
2134 std::string thevalue
;
2136 varobj_formatted_print_options (&opts
, format
);
2139 if (gdb_python_initialized
)
2141 PyObject
*value_formatter
= var
->dynamic
->pretty_printer
;
2143 gdbpy_enter_varobj
enter_py (var
);
2145 if (value_formatter
)
2147 if (PyObject_HasAttr (value_formatter
, gdbpy_to_string_cst
))
2149 struct value
*replacement
;
2151 gdbpy_ref
<> output
= apply_varobj_pretty_printer (value_formatter
,
2156 /* If we have string like output ... */
2157 if (output
!= nullptr && output
!= Py_None
)
2159 /* If this is a lazy string, extract it. For lazy
2160 strings we always print as a string, so set
2162 if (gdbpy_is_lazy_string (output
.get ()))
2164 gdbpy_extract_lazy_string (output
.get (), &str_addr
,
2165 &type
, &len
, &encoding
);
2166 string_print
= true;
2170 /* If it is a regular (non-lazy) string, extract
2171 it and copy the contents into THEVALUE. If the
2172 hint says to print it as a string, set
2173 string_print. Otherwise just return the extracted
2174 string as a value. */
2176 gdb::unique_xmalloc_ptr
<char> s
2177 = python_string_to_target_string (output
.get ());
2181 struct gdbarch
*gdbarch
;
2183 gdb::unique_xmalloc_ptr
<char> hint
2184 = gdbpy_get_display_hint (value_formatter
);
2187 if (!strcmp (hint
.get (), "string"))
2188 string_print
= true;
2191 thevalue
= std::string (s
.get ());
2192 len
= thevalue
.size ();
2193 gdbarch
= value
->type ()->arch ();
2194 type
= builtin_type (gdbarch
)->builtin_char
;
2200 gdbpy_print_stack ();
2203 /* If the printer returned a replacement value, set VALUE
2204 to REPLACEMENT. If there is not a replacement value,
2205 just use the value passed to this function. */
2207 value
= replacement
;
2211 /* No to_string method, so if there is a 'children'
2212 method, return the default. */
2213 if (PyObject_HasAttr (value_formatter
, gdbpy_children_cst
))
2219 /* If we've made it here, we don't want a pretty-printer --
2220 if we had one, it would already have been used. */
2226 /* If the THEVALUE has contents, it is a regular string. */
2227 if (!thevalue
.empty ())
2228 current_language
->printstr (&stb
, type
, (gdb_byte
*) thevalue
.c_str (),
2229 len
, encoding
.get (), 0, &opts
);
2230 else if (string_print
)
2231 /* Otherwise, if string_print is set, and it is not a regular
2232 string, it is a lazy string. */
2233 val_print_string (type
, encoding
.get (), str_addr
, len
, &stb
, &opts
);
2235 /* All other cases. */
2236 common_val_print (value
, &stb
, 0, &opts
, current_language
);
2238 return stb
.release ();
2242 varobj_editable_p (const struct varobj
*var
)
2246 if (!(var
->root
->is_valid
&& var
->value
!= nullptr
2247 && var
->value
->lval ()))
2250 type
= varobj_get_value_type (var
);
2252 switch (type
->code ())
2254 case TYPE_CODE_STRUCT
:
2255 case TYPE_CODE_UNION
:
2256 case TYPE_CODE_ARRAY
:
2257 case TYPE_CODE_FUNC
:
2258 case TYPE_CODE_METHOD
:
2268 /* Call VAR's value_is_changeable_p language-specific callback. */
2271 varobj_value_is_changeable_p (const struct varobj
*var
)
2273 return var
->root
->lang_ops
->value_is_changeable_p (var
);
2276 /* Return true if that varobj is floating, that is is always evaluated in the
2277 selected frame, and not bound to thread/frame. Such variable objects
2278 are created using '@' as frame specifier to -var-create. */
2280 varobj_floating_p (const struct varobj
*var
)
2282 return var
->root
->floating
;
2285 /* Implement the "value_is_changeable_p" varobj callback for most
2289 varobj_default_value_is_changeable_p (const struct varobj
*var
)
2294 if (CPLUS_FAKE_CHILD (var
))
2297 type
= varobj_get_value_type (var
);
2299 switch (type
->code ())
2301 case TYPE_CODE_STRUCT
:
2302 case TYPE_CODE_UNION
:
2303 case TYPE_CODE_ARRAY
:
2314 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback
2318 all_root_varobjs (gdb::function_view
<void (struct varobj
*var
)> func
)
2320 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
2321 auto iter
= rootlist
.begin ();
2322 auto end
= rootlist
.end ();
2326 func ((*self
)->rootvar
);
2330 /* Try to recreate the varobj VAR if it is a global or floating. This is a
2331 helper function for varobj_re_set. */
2334 varobj_re_set_iter (struct varobj
*var
)
2336 /* Invalidated global varobjs must be re-evaluated. */
2337 if (!var
->root
->is_valid
&& var
->root
->global
)
2339 struct varobj
*tmp_var
;
2341 /* Try to create a varobj with same expression. If we succeed
2342 and have a global replace the old varobj. */
2343 tmp_var
= varobj_create (nullptr, var
->name
.c_str (), (CORE_ADDR
) 0,
2345 if (tmp_var
!= nullptr && tmp_var
->root
->global
)
2347 tmp_var
->obj_name
= var
->obj_name
;
2348 varobj_delete (var
, 0);
2349 install_variable (tmp_var
);
2357 varobj_re_set (void)
2359 all_root_varobjs (varobj_re_set_iter
);
2362 /* Ensure that no varobj keep references to OBJFILE. */
2365 varobj_invalidate_if_uses_objfile (struct objfile
*objfile
)
2367 if (objfile
->separate_debug_objfile_backlink
!= nullptr)
2368 objfile
= objfile
->separate_debug_objfile_backlink
;
2370 all_root_varobjs ([objfile
] (struct varobj
*var
)
2372 if (var
->root
->valid_block
!= nullptr)
2374 struct objfile
*bl_objfile
= var
->root
->valid_block
->objfile ();
2375 if (bl_objfile
->separate_debug_objfile_backlink
!= nullptr)
2376 bl_objfile
= bl_objfile
->separate_debug_objfile_backlink
;
2378 if (bl_objfile
== objfile
)
2380 /* The varobj is tied to a block which is going away. There is
2381 no way to reconstruct something later, so invalidate the
2382 varobj completely and drop the reference to the block which is
2384 var
->root
->is_valid
= false;
2385 var
->root
->valid_block
= nullptr;
2389 if (var
->root
->exp
!= nullptr && var
->root
->exp
->uses_objfile (objfile
))
2391 /* The varobj's current expression references the objfile. For
2392 globals and floating, it is possible that when we try to
2393 re-evaluate the expression later it is still valid with
2394 whatever is in scope at that moment. Just invalidate the
2395 expression for now. */
2396 var
->root
->exp
.reset ();
2398 /* It only makes sense to keep a floating varobj around. */
2399 if (!var
->root
->floating
)
2400 var
->root
->is_valid
= false;
2403 /* var->value->type and var->type might also reference the objfile.
2404 This is taken care of in value.c:preserve_values which deals with
2405 making sure that objfile-owned types are replaced with
2406 gdbarch-owned equivalents. */
2410 /* A hash function for a varobj. */
2413 hash_varobj (const void *a
)
2415 const varobj
*obj
= (const varobj
*) a
;
2416 return htab_hash_string (obj
->obj_name
.c_str ());
2419 /* A hash table equality function for varobjs. */
2422 eq_varobj_and_string (const void *a
, const void *b
)
2424 const varobj
*obj
= (const varobj
*) a
;
2425 const char *name
= (const char *) b
;
2427 return obj
->obj_name
== name
;
2430 void _initialize_varobj ();
2432 _initialize_varobj ()
2434 varobj_table
= htab_create_alloc (5, hash_varobj
, eq_varobj_and_string
,
2435 nullptr, xcalloc
, xfree
);
2437 add_setshow_zuinteger_cmd ("varobj", class_maintenance
,
2439 _("Set varobj debugging."),
2440 _("Show varobj debugging."),
2441 _("When non-zero, varobj debugging is enabled."),
2442 NULL
, show_varobjdebug
,
2443 &setdebuglist
, &showdebuglist
);
2445 gdb::observers::free_objfile
.attach (varobj_invalidate_if_uses_objfile
,