1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2025 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 if (var
->root
->exp
->type_p ())
328 gdb_printf (gdb_stderr
, "Attempt to use a type name"
329 " as an expression.\n");
333 var
->format
= FORMAT_NATURAL
;
334 var
->root
->valid_block
=
335 var
->root
->floating
? NULL
: tracker
.block ();
337 = var
->root
->floating
? false : var
->root
->valid_block
== nullptr;
338 var
->name
= expression
;
339 /* For a root var, the name and the expr are the same. */
340 var
->path_expr
= expression
;
342 /* When the frame is different from the current frame,
343 we must select the appropriate frame before parsing
344 the expression, otherwise the value will not be current.
345 Since select_frame is so benign, just call it for all cases. */
346 if (var
->root
->valid_block
)
348 /* User could specify explicit FRAME-ADDR which was not found but
349 EXPRESSION is frame specific and we would not be able to evaluate
350 it correctly next time. With VALID_BLOCK set we must also set
351 FRAME and THREAD_ID. */
353 error (_("Failed to find the specified frame"));
355 var
->root
->frame
= get_frame_id (fi
);
356 var
->root
->thread_id
= inferior_thread ()->global_num
;
357 old_id
= get_frame_id (get_selected_frame (NULL
));
361 /* We definitely need to catch errors here. If evaluation of
362 the expression succeeds, we got the value we wanted. But if
363 it fails, we still go on with a call to evaluate_type(). */
366 value
= var
->root
->exp
->evaluate ();
368 catch (const gdb_exception_error
&except
)
370 /* Error getting the value. Try to at least get the
372 struct value
*type_only_value
= var
->root
->exp
->evaluate_type ();
374 var
->type
= type_only_value
->type ();
379 int real_type_found
= 0;
381 var
->type
= value_actual_type (value
, 0, &real_type_found
);
383 value
= value_cast (var
->type
, value
);
386 /* Set language info */
387 var
->root
->lang_ops
= var
->root
->exp
->language_defn
->varobj_ops ();
389 install_new_value (var
.get (), value
, 1 /* Initial assignment */);
391 /* Set ourselves as our root. */
392 var
->root
->rootvar
= var
.get ();
394 /* Reset the selected frame. */
395 if (frame_id_p (old_id
))
396 select_frame (frame_find_by_id (old_id
));
399 /* If the variable object name is null, that means this
400 is a temporary variable, so don't install it. */
402 if ((var
!= NULL
) && (objname
!= NULL
))
404 var
->obj_name
= objname
;
405 install_variable (var
.get ());
408 return var
.release ();
411 /* Generates an unique name that can be used for a varobj. */
414 varobj_gen_name (void)
418 /* Generate a name for this object. */
420 return string_printf ("var%d", id
);
423 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
424 error if OBJNAME cannot be found. */
427 varobj_get_handle (const char *objname
)
429 varobj
*var
= (varobj
*) htab_find_with_hash (varobj_table
, objname
,
430 htab_hash_string (objname
));
433 error (_("Variable object not found"));
438 /* Given the handle, return the name of the object. */
441 varobj_get_objname (const struct varobj
*var
)
443 return var
->obj_name
.c_str ();
446 /* Given the handle, return the expression represented by the
450 varobj_get_expression (const struct varobj
*var
)
452 return name_of_variable (var
);
458 varobj_delete (struct varobj
*var
, bool only_children
)
460 return delete_variable (var
, only_children
);
465 /* Convenience function for varobj_set_visualizer. Instantiate a
466 pretty-printer for a given value. */
468 instantiate_pretty_printer (PyObject
*constructor
, struct value
*value
)
470 gdbpy_ref
<> val_obj (value_to_value_object (value
));
471 if (val_obj
== nullptr)
474 return PyObject_CallFunctionObjArgs (constructor
, val_obj
.get (), NULL
);
479 /* Set/Get variable object display format. */
481 enum varobj_display_formats
482 varobj_set_display_format (struct varobj
*var
,
483 enum varobj_display_formats format
)
485 var
->format
= format
;
487 if (varobj_value_is_changeable_p (var
)
488 && var
->value
!= nullptr && !var
->value
->lazy ())
490 var
->print_value
= varobj_value_get_print_value (var
->value
.get (),
497 enum varobj_display_formats
498 varobj_get_display_format (const struct varobj
*var
)
503 gdb::unique_xmalloc_ptr
<char>
504 varobj_get_display_hint (const struct varobj
*var
)
506 gdb::unique_xmalloc_ptr
<char> result
;
509 if (!gdb_python_initialized
)
512 gdbpy_enter_varobj
enter_py (var
);
514 if (var
->dynamic
->pretty_printer
!= NULL
)
515 result
= gdbpy_get_display_hint (var
->dynamic
->pretty_printer
);
521 /* Return true if the varobj has items after TO, false otherwise. */
524 varobj_has_more (const struct varobj
*var
, int to
)
526 if (var
->children
.size () > to
)
529 return ((to
== -1 || var
->children
.size () == to
)
530 && (var
->dynamic
->saved_item
!= NULL
));
533 /* If the variable object is bound to a specific thread, that
534 is its evaluation can always be done in context of a frame
535 inside that thread, returns GDB id of the thread -- which
536 is always positive. Otherwise, returns -1. */
538 varobj_get_thread_id (const struct varobj
*var
)
540 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
541 return var
->root
->thread_id
;
547 varobj_set_frozen (struct varobj
*var
, bool frozen
)
549 /* When a variable is unfrozen, we don't fetch its value.
550 The 'not_fetched' flag remains set, so next -var-update
553 We don't fetch the value, because for structures the client
554 should do -var-update anyway. It would be bad to have different
555 client-size logic for structure and other types. */
556 var
->frozen
= frozen
;
560 varobj_get_frozen (const struct varobj
*var
)
565 /* A helper function that updates the contents of FROM and TO based on the
566 size of the vector CHILDREN. If the contents of either FROM or TO are
567 negative the entire range is used. */
570 varobj_restrict_range (const std::vector
<varobj
*> &children
,
573 int len
= children
.size ();
575 if (*from
< 0 || *to
< 0)
591 /* A helper for update_dynamic_varobj_children that installs a new
592 child when needed. */
595 install_dynamic_child (struct varobj
*var
,
596 std::vector
<varobj
*> *changed
,
597 std::vector
<varobj
*> *type_changed
,
598 std::vector
<varobj
*> *newobj
,
599 std::vector
<varobj
*> *unchanged
,
602 struct varobj_item
*item
)
604 if (var
->children
.size () < index
+ 1)
606 /* There's no child yet. */
607 struct varobj
*child
= varobj_add_child (var
, item
);
611 newobj
->push_back (child
);
617 varobj
*existing
= var
->children
[index
];
618 bool type_updated
= update_type_if_necessary (existing
,
623 if (type_changed
!= NULL
)
624 type_changed
->push_back (existing
);
626 if (install_new_value (existing
, item
->value
.get (), 0))
628 if (!type_updated
&& changed
!= NULL
)
629 changed
->push_back (existing
);
631 else if (!type_updated
&& unchanged
!= NULL
)
632 unchanged
->push_back (existing
);
636 /* A factory for creating dynamic varobj's iterators. Returns an
637 iterator object suitable for iterating over VAR's children. */
639 static std::unique_ptr
<varobj_iter
>
640 varobj_get_iterator (struct varobj
*var
)
643 if (var
->dynamic
->pretty_printer
)
645 value_print_options opts
;
646 varobj_formatted_print_options (&opts
, var
->format
);
647 return py_varobj_get_iterator (var
, var
->dynamic
->pretty_printer
, &opts
);
651 gdb_assert_not_reached ("requested an iterator from a non-dynamic varobj");
655 update_dynamic_varobj_children (struct varobj
*var
,
656 std::vector
<varobj
*> *changed
,
657 std::vector
<varobj
*> *type_changed
,
658 std::vector
<varobj
*> *newobj
,
659 std::vector
<varobj
*> *unchanged
,
661 bool update_children
,
669 if (update_children
|| var
->dynamic
->child_iter
== NULL
)
671 var
->dynamic
->child_iter
= varobj_get_iterator (var
);
672 var
->dynamic
->saved_item
.reset (nullptr);
676 if (var
->dynamic
->child_iter
== NULL
)
680 i
= var
->children
.size ();
682 /* We ask for one extra child, so that MI can report whether there
683 are more children. */
684 for (; to
< 0 || i
< to
+ 1; ++i
)
686 std::unique_ptr
<varobj_item
> item
;
688 /* See if there was a leftover from last time. */
689 if (var
->dynamic
->saved_item
!= NULL
)
690 item
= std::move (var
->dynamic
->saved_item
);
692 item
= var
->dynamic
->child_iter
->next ();
696 /* Iteration is done. Remove iterator from VAR. */
697 var
->dynamic
->child_iter
.reset (nullptr);
700 /* We don't want to push the extra child on any report list. */
701 if (to
< 0 || i
< to
)
703 bool can_mention
= from
< 0 || i
>= from
;
705 install_dynamic_child (var
, can_mention
? changed
: NULL
,
706 can_mention
? type_changed
: NULL
,
707 can_mention
? newobj
: NULL
,
708 can_mention
? unchanged
: NULL
,
709 can_mention
? cchanged
: NULL
, i
,
714 var
->dynamic
->saved_item
= std::move (item
);
716 /* We want to truncate the child list just before this
722 if (i
< var
->children
.size ())
725 for (int j
= i
; j
< var
->children
.size (); ++j
)
726 varobj_delete (var
->children
[j
], 0);
728 var
->children
.resize (i
);
731 /* If there are fewer children than requested, note that the list of
733 if (to
>= 0 && var
->children
.size () < to
)
736 var
->num_children
= var
->children
.size ();
742 varobj_get_num_children (struct varobj
*var
)
744 if (var
->num_children
== -1)
746 if (varobj_is_dynamic_p (var
))
750 /* If we have a dynamic varobj, don't report -1 children.
751 So, try to fetch some children first. */
752 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
, &dummy
,
756 var
->num_children
= number_of_children (var
);
759 return var
->num_children
>= 0 ? var
->num_children
: 0;
762 /* Creates a list of the immediate children of a variable object;
763 the return code is the number of such children or -1 on error. */
765 const std::vector
<varobj
*> &
766 varobj_list_children (struct varobj
*var
, int *from
, int *to
)
768 var
->dynamic
->children_requested
= true;
770 if (varobj_is_dynamic_p (var
))
772 bool children_changed
;
774 /* This, in theory, can result in the number of children changing without
775 frontend noticing. But well, calling -var-list-children on the same
776 varobj twice is not something a sane frontend would do. */
777 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
,
778 &children_changed
, false, 0, *to
);
779 varobj_restrict_range (var
->children
, from
, to
);
780 return var
->children
;
783 if (var
->num_children
== -1)
784 var
->num_children
= number_of_children (var
);
786 /* If that failed, give up. */
787 if (var
->num_children
== -1)
788 return var
->children
;
790 /* If we're called when the list of children is not yet initialized,
791 allocate enough elements in it. */
792 while (var
->children
.size () < var
->num_children
)
793 var
->children
.push_back (NULL
);
795 for (int i
= 0; i
< var
->num_children
; i
++)
797 if (var
->children
[i
] == NULL
)
799 /* Either it's the first call to varobj_list_children for
800 this variable object, and the child was never created,
801 or it was explicitly deleted by the client. */
802 std::string name
= name_of_child (var
, i
);
803 var
->children
[i
] = create_child (var
, i
, name
);
807 varobj_restrict_range (var
->children
, from
, to
);
808 return var
->children
;
811 static struct varobj
*
812 varobj_add_child (struct varobj
*var
, struct varobj_item
*item
)
814 varobj
*v
= create_child_with_value (var
, var
->children
.size (), item
);
816 var
->children
.push_back (v
);
821 /* Obtain the type of an object Variable as a string similar to the one gdb
822 prints on the console. The caller is responsible for freeing the string.
826 varobj_get_type (struct varobj
*var
)
828 /* For the "fake" variables, do not return a type. (Its type is
830 Do not return a type for invalid variables as well. */
831 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
832 return std::string ();
834 return type_to_string (var
->type
);
837 /* Obtain the type of an object variable. */
840 varobj_get_gdb_type (const struct varobj
*var
)
845 /* Is VAR a path expression parent, i.e., can it be used to construct
846 a valid path expression? */
849 is_path_expr_parent (const struct varobj
*var
)
851 gdb_assert (var
->root
->lang_ops
->is_path_expr_parent
!= NULL
);
852 return var
->root
->lang_ops
->is_path_expr_parent (var
);
855 /* Is VAR a path expression parent, i.e., can it be used to construct
856 a valid path expression? By default we assume any VAR can be a path
860 varobj_default_is_path_expr_parent (const struct varobj
*var
)
865 /* Return the path expression parent for VAR. */
867 const struct varobj
*
868 varobj_get_path_expr_parent (const struct varobj
*var
)
870 const struct varobj
*parent
= var
;
872 while (!is_root_p (parent
) && !is_path_expr_parent (parent
))
873 parent
= parent
->parent
;
875 /* Computation of full rooted expression for children of dynamic
876 varobjs is not supported. */
877 if (varobj_is_dynamic_p (parent
))
878 error (_("Invalid variable object (child of a dynamic varobj)"));
883 /* Return a pointer to the full rooted expression of varobj VAR.
884 If it has not been computed yet, compute it. */
887 varobj_get_path_expr (const struct varobj
*var
)
889 if (var
->path_expr
.empty ())
891 /* For root varobjs, we initialize path_expr
892 when creating varobj, so here it should be
894 struct varobj
*mutable_var
= (struct varobj
*) var
;
895 gdb_assert (!is_root_p (var
));
897 mutable_var
->path_expr
= (*var
->root
->lang_ops
->path_expr_of_child
) (var
);
900 return var
->path_expr
.c_str ();
903 const struct language_defn
*
904 varobj_get_language (const struct varobj
*var
)
906 return var
->root
->exp
->language_defn
;
910 varobj_get_attributes (const struct varobj
*var
)
914 if (varobj_editable_p (var
))
915 /* FIXME: define masks for attributes. */
916 attributes
|= 0x00000001; /* Editable */
921 /* Return true if VAR is a dynamic varobj. */
924 varobj_is_dynamic_p (const struct varobj
*var
)
926 return var
->dynamic
->pretty_printer
!= NULL
;
930 varobj_get_formatted_value (struct varobj
*var
,
931 enum varobj_display_formats format
)
933 return my_value_of_variable (var
, format
);
937 varobj_get_value (struct varobj
*var
)
939 return my_value_of_variable (var
, var
->format
);
942 /* Set the value of an object variable (if it is editable) to the
943 value of the given expression. */
944 /* Note: Invokes functions that can call error(). */
947 varobj_set_value (struct varobj
*var
, const char *expression
)
949 struct value
*val
= NULL
; /* Initialize to keep gcc happy. */
950 /* The argument "expression" contains the variable's new value.
951 We need to first construct a legal expression for this -- ugh! */
952 /* Does this cover all the bases? */
953 struct value
*value
= NULL
; /* Initialize to keep gcc happy. */
954 const char *s
= expression
;
956 gdb_assert (varobj_editable_p (var
));
958 /* ALWAYS reset to decimal temporarily. */
959 auto save_input_radix
= make_scoped_restore (&input_radix
, 10);
960 expression_up exp
= parse_exp_1 (&s
, 0, 0, 0);
963 value
= exp
->evaluate ();
966 catch (const gdb_exception_error
&except
)
968 /* We cannot proceed without a valid expression. */
972 /* All types that are editable must also be changeable. */
973 gdb_assert (varobj_value_is_changeable_p (var
));
975 /* The value of a changeable variable object must not be lazy. */
976 gdb_assert (!var
->value
->lazy ());
978 /* Need to coerce the input. We want to check if the
979 value of the variable object will be different
980 after assignment, and the first thing value_assign
981 does is coerce the input.
982 For example, if we are assigning an array to a pointer variable we
983 should compare the pointer with the array's address, not with the
985 value
= coerce_array (value
);
987 /* The new value may be lazy. value_assign, or
988 rather value_contents, will take care of this. */
991 val
= value_assign (var
->value
.get (), value
);
994 catch (const gdb_exception_error
&except
)
999 /* If the value has changed, record it, so that next -var-update can
1000 report this change. If a variable had a value of '1', we've set it
1001 to '333' and then set again to '1', when -var-update will report this
1002 variable as changed -- because the first assignment has set the
1003 'updated' flag. There's no need to optimize that, because return value
1004 of -var-update should be considered an approximation. */
1005 var
->updated
= install_new_value (var
, val
, false /* Compare values. */);
1011 /* A helper function to install a constructor function and visualizer
1012 in a varobj_dynamic. */
1015 install_visualizer (struct varobj_dynamic
*var
, PyObject
*constructor
,
1016 PyObject
*visualizer
)
1018 Py_XDECREF (var
->constructor
);
1019 var
->constructor
= constructor
;
1021 Py_XDECREF (var
->pretty_printer
);
1022 var
->pretty_printer
= visualizer
;
1024 var
->child_iter
.reset (nullptr);
1027 /* Install the default visualizer for VAR. */
1030 install_default_visualizer (struct varobj
*var
)
1032 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1033 if (CPLUS_FAKE_CHILD (var
))
1036 if (pretty_printing
)
1038 gdbpy_ref
<> pretty_printer
;
1040 if (var
->value
!= nullptr)
1042 pretty_printer
= gdbpy_get_varobj_pretty_printer (var
->value
.get ());
1043 if (pretty_printer
== nullptr)
1045 gdbpy_print_stack ();
1046 error (_("Cannot instantiate printer for default visualizer"));
1050 if (pretty_printer
== Py_None
)
1051 pretty_printer
.reset (nullptr);
1053 install_visualizer (var
->dynamic
, NULL
, pretty_printer
.release ());
1057 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1058 make a new object. */
1061 construct_visualizer (struct varobj
*var
, PyObject
*constructor
)
1063 PyObject
*pretty_printer
;
1065 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1066 if (CPLUS_FAKE_CHILD (var
))
1069 Py_INCREF (constructor
);
1070 if (constructor
== Py_None
)
1071 pretty_printer
= NULL
;
1074 pretty_printer
= instantiate_pretty_printer (constructor
,
1076 if (! pretty_printer
)
1078 gdbpy_print_stack ();
1079 Py_DECREF (constructor
);
1080 constructor
= Py_None
;
1081 Py_INCREF (constructor
);
1084 if (pretty_printer
== Py_None
)
1086 Py_DECREF (pretty_printer
);
1087 pretty_printer
= NULL
;
1091 install_visualizer (var
->dynamic
, constructor
, pretty_printer
);
1094 #endif /* HAVE_PYTHON */
1096 /* A helper function for install_new_value. This creates and installs
1097 a visualizer for VAR, if appropriate. */
1100 install_new_value_visualizer (struct varobj
*var
)
1103 /* If the constructor is None, then we want the raw value. If VAR
1104 does not have a value, just skip this. */
1105 if (!gdb_python_initialized
)
1108 if (var
->dynamic
->constructor
!= Py_None
&& var
->value
!= NULL
)
1110 gdbpy_enter_varobj
enter_py (var
);
1112 if (var
->dynamic
->constructor
== NULL
)
1113 install_default_visualizer (var
);
1115 construct_visualizer (var
, var
->dynamic
->constructor
);
1122 /* When using RTTI to determine variable type it may be changed in runtime when
1123 the variable value is changed. This function checks whether type of varobj
1124 VAR will change when a new value NEW_VALUE is assigned and if it is so
1125 updates the type of VAR. */
1128 update_type_if_necessary (struct varobj
*var
, struct value
*new_value
)
1132 struct value_print_options opts
;
1134 get_user_print_options (&opts
);
1135 if (opts
.objectprint
)
1137 struct type
*new_type
= value_actual_type (new_value
, 0, 0);
1138 std::string new_type_str
= type_to_string (new_type
);
1139 std::string curr_type_str
= varobj_get_type (var
);
1141 /* Did the type name change? */
1142 if (curr_type_str
!= new_type_str
)
1144 var
->type
= new_type
;
1146 /* This information may be not valid for a new type. */
1147 varobj_delete (var
, 1);
1148 var
->children
.clear ();
1149 var
->num_children
= -1;
1158 /* Assign a new value to a variable object. If INITIAL is true,
1159 this is the first assignment after the variable object was just
1160 created, or changed type. In that case, just assign the value
1162 Otherwise, assign the new value, and return true if the value is
1163 different from the current one, false otherwise. The comparison is
1164 done on textual representation of value. Therefore, some types
1165 need not be compared. E.g. for structures the reported value is
1166 always "{...}", so no comparison is necessary here. If the old
1167 value was NULL and new one is not, or vice versa, we always return true.
1169 The VALUE parameter should not be released -- the function will
1170 take care of releasing it when needed. */
1172 install_new_value (struct varobj
*var
, struct value
*value
, bool initial
)
1176 bool changed
= false;
1177 bool intentionally_not_fetched
= false;
1179 /* We need to know the varobj's type to decide if the value should
1180 be fetched or not. C++ fake children (public/protected/private)
1181 don't have a type. */
1182 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
1183 changeable
= varobj_value_is_changeable_p (var
);
1185 /* If the type has custom visualizer, we consider it to be always
1186 changeable. FIXME: need to make sure this behavior will not
1187 mess up read-sensitive values. */
1188 if (var
->dynamic
->pretty_printer
!= NULL
)
1191 need_to_fetch
= changeable
;
1193 /* We are not interested in the address of references, and given
1194 that in C++ a reference is not rebindable, it cannot
1195 meaningfully change. So, get hold of the real value. */
1197 value
= coerce_ref (value
);
1199 if (var
->type
&& var
->type
->code () == TYPE_CODE_UNION
)
1200 /* For unions, we need to fetch the value implicitly because
1201 of implementation of union member fetch. When gdb
1202 creates a value for a field and the value of the enclosing
1203 structure is not lazy, it immediately copies the necessary
1204 bytes from the enclosing values. If the enclosing value is
1205 lazy, the call to value_fetch_lazy on the field will read
1206 the data from memory. For unions, that means we'll read the
1207 same memory more than once, which is not desirable. So
1209 need_to_fetch
= true;
1211 /* The new value might be lazy. If the type is changeable,
1212 that is we'll be comparing values of this type, fetch the
1213 value now. Otherwise, on the next update the old value
1214 will be lazy, which means we've lost that old value. */
1215 if (need_to_fetch
&& value
&& value
->lazy ())
1217 const struct varobj
*parent
= var
->parent
;
1218 bool frozen
= var
->frozen
;
1220 for (; !frozen
&& parent
; parent
= parent
->parent
)
1221 frozen
|= parent
->frozen
;
1223 if (frozen
&& initial
)
1225 /* For variables that are frozen, or are children of frozen
1226 variables, we don't do fetch on initial assignment.
1227 For non-initial assignment we do the fetch, since it means we're
1228 explicitly asked to compare the new value with the old one. */
1229 intentionally_not_fetched
= true;
1236 value
->fetch_lazy ();
1239 catch (const gdb_exception_error
&except
)
1241 /* Set the value to NULL, so that for the next -var-update,
1242 we don't try to compare the new value with this value,
1243 that we couldn't even read. */
1249 /* Get a reference now, before possibly passing it to any Python
1250 code that might release it. */
1251 value_ref_ptr value_holder
;
1253 value_holder
= value_ref_ptr::new_reference (value
);
1255 /* Below, we'll be comparing string rendering of old and new
1256 values. Don't get string rendering if the value is
1257 lazy -- if it is, the code above has decided that the value
1258 should not be fetched. */
1259 std::string print_value
;
1260 if (value
!= NULL
&& !value
->lazy ()
1261 && var
->dynamic
->pretty_printer
== NULL
)
1262 print_value
= varobj_value_get_print_value (value
, var
->format
, var
);
1264 /* If the type is changeable, compare the old and the new values.
1265 If this is the initial assignment, we don't have any old value
1267 if (!initial
&& changeable
)
1269 /* If the value of the varobj was changed by -var-set-value,
1270 then the value in the varobj and in the target is the same.
1271 However, that value is different from the value that the
1272 varobj had after the previous -var-update. So need to the
1273 varobj as changed. */
1276 else if (var
->dynamic
->pretty_printer
== NULL
)
1278 /* Try to compare the values. That requires that both
1279 values are non-lazy. */
1280 if (var
->not_fetched
&& var
->value
->lazy ())
1282 /* This is a frozen varobj and the value was never read.
1283 Presumably, UI shows some "never read" indicator.
1284 Now that we've fetched the real value, we need to report
1285 this varobj as changed so that UI can show the real
1289 else if (var
->value
== NULL
&& value
== NULL
)
1292 else if (var
->value
== NULL
|| value
== NULL
)
1298 gdb_assert (!var
->value
->lazy ());
1299 gdb_assert (!value
->lazy ());
1301 gdb_assert (!var
->print_value
.empty () && !print_value
.empty ());
1302 if (var
->print_value
!= print_value
)
1308 if (!initial
&& !changeable
)
1310 /* For values that are not changeable, we don't compare the values.
1311 However, we want to notice if a value was not NULL and now is NULL,
1312 or vice versa, so that we report when top-level varobjs come in scope
1313 and leave the scope. */
1314 changed
= (var
->value
!= NULL
) != (value
!= NULL
);
1317 /* We must always keep the new value, since children depend on it. */
1318 var
->value
= value_holder
;
1319 if (value
&& value
->lazy () && intentionally_not_fetched
)
1320 var
->not_fetched
= true;
1322 var
->not_fetched
= false;
1323 var
->updated
= false;
1325 install_new_value_visualizer (var
);
1327 /* If we installed a pretty-printer, re-compare the printed version
1328 to see if the variable changed. */
1329 if (var
->dynamic
->pretty_printer
!= NULL
)
1331 print_value
= varobj_value_get_print_value (var
->value
.get (),
1333 if (var
->print_value
!= print_value
)
1336 var
->print_value
= print_value
;
1338 gdb_assert (var
->value
== nullptr || var
->value
->type ());
1343 /* Return the requested range for a varobj. VAR is the varobj. FROM
1344 and TO are out parameters; *FROM and *TO will be set to the
1345 selected sub-range of VAR. If no range was selected using
1346 -var-set-update-range, then both will be -1. */
1348 varobj_get_child_range (const struct varobj
*var
, int *from
, int *to
)
1354 /* Set the selected sub-range of children of VAR to start at index
1355 FROM and end at index TO. If either FROM or TO is less than zero,
1356 this is interpreted as a request for all children. */
1358 varobj_set_child_range (struct varobj
*var
, int from
, int to
)
1365 varobj_set_visualizer (struct varobj
*var
, const char *visualizer
)
1370 if (!gdb_python_initialized
)
1373 gdbpy_enter_varobj
enter_py (var
);
1375 mainmod
= PyImport_AddModule ("__main__");
1377 = gdbpy_ref
<>::new_reference (PyModule_GetDict (mainmod
));
1378 gdbpy_ref
<> constructor (PyRun_String (visualizer
, Py_eval_input
,
1379 globals
.get (), globals
.get ()));
1381 if (constructor
== NULL
)
1383 gdbpy_print_stack ();
1384 error (_("Could not evaluate visualizer expression: %s"), visualizer
);
1387 construct_visualizer (var
, constructor
.get ());
1389 /* If there are any children now, wipe them. */
1390 varobj_delete (var
, 1 /* children only */);
1391 var
->num_children
= -1;
1393 /* Also be sure to reset the print value. */
1394 varobj_set_display_format (var
, var
->format
);
1396 error (_("Python support required"));
1400 /* If NEW_VALUE is the new value of the given varobj (var), return
1401 true if var has mutated. In other words, if the type of
1402 the new value is different from the type of the varobj's old
1405 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1408 varobj_value_has_mutated (const struct varobj
*var
, struct value
*new_value
,
1409 struct type
*new_type
)
1411 /* If we haven't previously computed the number of children in var,
1412 it does not matter from the front-end's perspective whether
1413 the type has mutated or not. For all intents and purposes,
1414 it has not mutated. */
1415 if (var
->num_children
< 0)
1418 if (var
->root
->lang_ops
->value_has_mutated
!= NULL
)
1420 /* The varobj module, when installing new values, explicitly strips
1421 references, saying that we're not interested in those addresses.
1422 But detection of mutation happens before installing the new
1423 value, so our value may be a reference that we need to strip
1424 in order to remain consistent. */
1425 if (new_value
!= NULL
)
1426 new_value
= coerce_ref (new_value
);
1427 return var
->root
->lang_ops
->value_has_mutated (var
, new_value
, new_type
);
1433 /* Update the values for a variable and its children. This is a
1434 two-pronged attack. First, re-parse the value for the root's
1435 expression to see if it's changed. Then go all the way
1436 through its children, reconstructing them and noting if they've
1439 The IS_EXPLICIT parameter specifies if this call is result
1440 of MI request to update this specific variable, or
1441 result of implicit -var-update *. For implicit request, we don't
1442 update frozen variables.
1444 NOTE: This function may delete the caller's varobj. If it
1445 returns TYPE_CHANGED, then it has done this and VARP will be modified
1446 to point to the new varobj. */
1448 std::vector
<varobj_update_result
>
1449 varobj_update (struct varobj
**varp
, bool is_explicit
)
1451 bool type_changed
= false;
1452 struct value
*newobj
;
1453 std::vector
<varobj_update_result
> stack
;
1454 std::vector
<varobj_update_result
> result
;
1456 /* Frozen means frozen -- we don't check for any change in
1457 this varobj, including its going out of scope, or
1458 changing type. One use case for frozen varobjs is
1459 retaining previously evaluated expressions, and we don't
1460 want them to be reevaluated at all. */
1461 if (!is_explicit
&& (*varp
)->frozen
)
1464 if (!(*varp
)->root
->is_valid
)
1466 result
.emplace_back (*varp
, VAROBJ_INVALID
);
1470 if ((*varp
)->root
->rootvar
== *varp
)
1472 varobj_update_result
r (*varp
);
1474 /* Update the root variable. value_of_root can return NULL
1475 if the variable is no longer around, i.e. we stepped out of
1476 the frame in which a local existed. We are letting the
1477 value_of_root variable dispose of the varobj if the type
1479 newobj
= value_of_root (varp
, &type_changed
);
1480 if (update_type_if_necessary (*varp
, newobj
))
1481 type_changed
= true;
1483 r
.type_changed
= type_changed
;
1484 if (install_new_value ((*varp
), newobj
, type_changed
))
1488 r
.status
= VAROBJ_NOT_IN_SCOPE
;
1489 r
.value_installed
= true;
1491 if (r
.status
== VAROBJ_NOT_IN_SCOPE
)
1493 if (r
.type_changed
|| r
.changed
)
1494 result
.push_back (std::move (r
));
1499 stack
.push_back (std::move (r
));
1502 stack
.emplace_back (*varp
);
1504 /* Walk through the children, reconstructing them all. */
1505 while (!stack
.empty ())
1507 varobj_update_result r
= std::move (stack
.back ());
1509 struct varobj
*v
= r
.varobj
;
1511 /* Update this variable, unless it's a root, which is already
1513 if (!r
.value_installed
)
1515 struct type
*new_type
;
1517 newobj
= value_of_child (v
->parent
, v
->index
);
1518 if (update_type_if_necessary (v
, newobj
))
1519 r
.type_changed
= true;
1521 new_type
= newobj
->type ();
1523 new_type
= v
->root
->lang_ops
->type_of_child (v
->parent
, v
->index
);
1525 if (varobj_value_has_mutated (v
, newobj
, new_type
))
1527 /* The children are no longer valid; delete them now.
1528 Report the fact that its type changed as well. */
1529 varobj_delete (v
, 1 /* only_children */);
1530 v
->num_children
= -1;
1534 r
.type_changed
= true;
1537 if (install_new_value (v
, newobj
, r
.type_changed
))
1544 /* We probably should not get children of a dynamic varobj, but
1545 for which -var-list-children was never invoked. */
1546 if (varobj_is_dynamic_p (v
))
1548 std::vector
<varobj
*> changed
, type_changed_vec
, unchanged
, newobj_vec
;
1549 bool children_changed
= false;
1554 if (!v
->dynamic
->children_requested
)
1558 /* If we initially did not have potential children, but
1559 now we do, consider the varobj as changed.
1560 Otherwise, if children were never requested, consider
1561 it as unchanged -- presumably, such varobj is not yet
1562 expanded in the UI, so we need not bother getting
1564 if (!varobj_has_more (v
, 0))
1566 update_dynamic_varobj_children (v
, NULL
, NULL
, NULL
, NULL
,
1567 &dummy
, false, 0, 0);
1568 if (varobj_has_more (v
, 0))
1573 result
.push_back (std::move (r
));
1578 /* If update_dynamic_varobj_children returns false, then we have
1579 a non-conforming pretty-printer, so we skip it. */
1580 if (update_dynamic_varobj_children (v
, &changed
, &type_changed_vec
,
1582 &unchanged
, &children_changed
,
1583 true, v
->from
, v
->to
))
1585 if (children_changed
|| !newobj_vec
.empty ())
1587 r
.children_changed
= true;
1588 r
.newobj
= std::move (newobj_vec
);
1590 /* Push in reverse order so that the first child is
1591 popped from the work stack first, and so will be
1592 added to result first. This does not affect
1593 correctness, just "nicer". */
1594 for (int i
= type_changed_vec
.size () - 1; i
>= 0; --i
)
1596 varobj_update_result
item (type_changed_vec
[i
]);
1598 /* Type may change only if value was changed. */
1599 item
.changed
= true;
1600 item
.type_changed
= true;
1601 item
.value_installed
= true;
1603 stack
.push_back (std::move (item
));
1605 for (int i
= changed
.size () - 1; i
>= 0; --i
)
1607 varobj_update_result
item (changed
[i
]);
1609 item
.changed
= true;
1610 item
.value_installed
= true;
1612 stack
.push_back (std::move (item
));
1614 for (int i
= unchanged
.size () - 1; i
>= 0; --i
)
1616 if (!unchanged
[i
]->frozen
)
1618 varobj_update_result
item (unchanged
[i
]);
1620 item
.value_installed
= true;
1622 stack
.push_back (std::move (item
));
1625 if (r
.changed
|| r
.children_changed
)
1626 result
.push_back (std::move (r
));
1632 /* Push any children. Use reverse order so that the first
1633 child is popped from the work stack first, and so
1634 will be added to result first. This does not
1635 affect correctness, just "nicer". */
1636 for (int i
= v
->children
.size () - 1; i
>= 0; --i
)
1638 varobj
*c
= v
->children
[i
];
1640 /* Child may be NULL if explicitly deleted by -var-delete. */
1641 if (c
!= NULL
&& !c
->frozen
)
1642 stack
.emplace_back (c
);
1645 if (r
.changed
|| r
.type_changed
)
1646 result
.push_back (std::move (r
));
1652 /* Helper functions */
1655 * Variable object construction/destruction
1659 delete_variable (struct varobj
*var
, bool only_children_p
)
1663 delete_variable_1 (&delcount
, var
, only_children_p
,
1664 true /* remove_from_parent_p */ );
1669 /* Delete the variable object VAR and its children. */
1670 /* IMPORTANT NOTE: If we delete a variable which is a child
1671 and the parent is not removed we dump core. It must be always
1672 initially called with remove_from_parent_p set. */
1674 delete_variable_1 (int *delcountp
, struct varobj
*var
, bool only_children_p
,
1675 bool remove_from_parent_p
)
1677 /* Delete any children of this variable, too. */
1678 for (varobj
*child
: var
->children
)
1683 if (!remove_from_parent_p
)
1684 child
->parent
= NULL
;
1686 delete_variable_1 (delcountp
, child
, false, only_children_p
);
1688 var
->children
.clear ();
1690 /* if we were called to delete only the children we are done here. */
1691 if (only_children_p
)
1694 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
1695 /* If the name is empty, this is a temporary variable, that has not
1696 yet been installed, don't report it, it belongs to the caller... */
1697 if (!var
->obj_name
.empty ())
1699 *delcountp
= *delcountp
+ 1;
1702 /* If this variable has a parent, remove it from its parent's list. */
1703 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1704 (as indicated by remove_from_parent_p) we don't bother doing an
1705 expensive list search to find the element to remove when we are
1706 discarding the list afterwards. */
1707 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1708 var
->parent
->children
[var
->index
] = NULL
;
1710 if (!var
->obj_name
.empty ())
1711 uninstall_variable (var
);
1713 /* Free memory associated with this variable. */
1717 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1719 install_variable (struct varobj
*var
)
1721 hashval_t hash
= htab_hash_string (var
->obj_name
.c_str ());
1722 void **slot
= htab_find_slot_with_hash (varobj_table
,
1723 var
->obj_name
.c_str (),
1725 if (*slot
!= nullptr)
1726 error (_("Duplicate variable object name"));
1728 /* Add varobj to hash table. */
1731 /* If root, add varobj to root list. */
1732 if (is_root_p (var
))
1733 rootlist
.push_front (var
->root
);
1736 /* Uninstall the object VAR. */
1738 uninstall_variable (struct varobj
*var
)
1740 hashval_t hash
= htab_hash_string (var
->obj_name
.c_str ());
1741 htab_remove_elt_with_hash (varobj_table
, var
->obj_name
.c_str (), hash
);
1744 gdb_printf (gdb_stdlog
, "Deleting %s\n", var
->obj_name
.c_str ());
1746 /* If root, remove varobj from root list. */
1747 if (is_root_p (var
))
1749 auto iter
= std::find (rootlist
.begin (), rootlist
.end (), var
->root
);
1750 rootlist
.erase (iter
);
1754 /* Create and install a child of the parent of the given name.
1756 The created VAROBJ takes ownership of the allocated NAME. */
1758 static struct varobj
*
1759 create_child (struct varobj
*parent
, int index
, std::string
&name
)
1761 struct varobj_item item
;
1763 std::swap (item
.name
, name
);
1764 item
.value
= release_value (value_of_child (parent
, index
));
1766 return create_child_with_value (parent
, index
, &item
);
1769 static struct varobj
*
1770 create_child_with_value (struct varobj
*parent
, int index
,
1771 struct varobj_item
*item
)
1773 varobj
*child
= new varobj (parent
->root
);
1775 /* NAME is allocated by caller. */
1776 std::swap (child
->name
, item
->name
);
1777 child
->index
= index
;
1778 child
->parent
= parent
;
1780 if (varobj_is_anonymous_child (child
))
1781 child
->obj_name
= string_printf ("%s.%d_anonymous",
1782 parent
->obj_name
.c_str (), index
);
1784 child
->obj_name
= string_printf ("%s.%s",
1785 parent
->obj_name
.c_str (),
1786 child
->name
.c_str ());
1788 install_variable (child
);
1790 /* Compute the type of the child. Must do this before
1791 calling install_new_value. */
1792 if (item
->value
!= NULL
)
1793 /* If the child had no evaluation errors, var->value
1794 will be non-NULL and contain a valid type. */
1795 child
->type
= value_actual_type (item
->value
.get (), 0, NULL
);
1797 /* Otherwise, we must compute the type. */
1798 child
->type
= (*child
->root
->lang_ops
->type_of_child
) (child
->parent
,
1800 install_new_value (child
, item
->value
.get (), 1);
1807 * Miscellaneous utility functions.
1810 /* Allocate memory and initialize a new variable. */
1811 varobj::varobj (varobj_root
*root_
)
1812 : root (root_
), dynamic (new varobj_dynamic
)
1816 /* Free any allocated memory associated with VAR. */
1823 if (var
->dynamic
->pretty_printer
!= NULL
)
1825 gdbpy_enter_varobj
enter_py (var
);
1827 Py_XDECREF (var
->dynamic
->constructor
);
1828 Py_XDECREF (var
->dynamic
->pretty_printer
);
1832 /* This must be deleted before the root object, because Python-based
1833 destructors need access to some components. */
1834 delete var
->dynamic
;
1836 if (is_root_p (var
))
1840 /* Return the type of the value that's stored in VAR,
1841 or that would have being stored there if the
1842 value were accessible.
1844 This differs from VAR->type in that VAR->type is always
1845 the true type of the expression in the source language.
1846 The return value of this function is the type we're
1847 actually storing in varobj, and using for displaying
1848 the values and for comparing previous and new values.
1850 For example, top-level references are always stripped. */
1852 varobj_get_value_type (const struct varobj
*var
)
1856 if (var
->value
!= nullptr)
1857 type
= var
->value
->type ();
1861 type
= check_typedef (type
);
1863 if (TYPE_IS_REFERENCE (type
))
1864 type
= get_target_type (type
);
1866 type
= check_typedef (type
);
1872 * Language-dependencies
1875 /* Common entry points */
1877 /* Return the number of children for a given variable.
1878 The result of this function is defined by the language
1879 implementation. The number of children returned by this function
1880 is the number of children that the user will see in the variable
1883 number_of_children (const struct varobj
*var
)
1885 return (*var
->root
->lang_ops
->number_of_children
) (var
);
1888 /* What is the expression for the root varobj VAR? */
1891 name_of_variable (const struct varobj
*var
)
1893 return (*var
->root
->lang_ops
->name_of_variable
) (var
);
1896 /* What is the name of the INDEX'th child of VAR? */
1899 name_of_child (struct varobj
*var
, int index
)
1901 return (*var
->root
->lang_ops
->name_of_child
) (var
, index
);
1904 /* If frame associated with VAR can be found, switch
1905 to it and return true. Otherwise, return false. */
1908 check_scope (const struct varobj
*var
)
1913 fi
= frame_find_by_id (var
->root
->frame
);
1918 CORE_ADDR pc
= get_frame_pc (fi
);
1920 if (pc
< var
->root
->valid_block
->start () ||
1921 pc
>= var
->root
->valid_block
->end ())
1929 /* Helper function to value_of_root. */
1931 static struct value
*
1932 value_of_root_1 (struct varobj
**var_handle
)
1934 struct value
*new_val
= NULL
;
1935 struct varobj
*var
= *var_handle
;
1936 bool within_scope
= false;
1938 /* Only root variables can be updated... */
1939 if (!is_root_p (var
))
1940 /* Not a root var. */
1943 scoped_restore_current_thread restore_thread
;
1945 /* Determine whether the variable is still around. */
1946 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
1947 within_scope
= true;
1948 else if (var
->root
->thread_id
== 0)
1950 /* The program was single-threaded when the variable object was
1951 created. Technically, it's possible that the program became
1952 multi-threaded since then, but we don't support such
1954 within_scope
= check_scope (var
);
1958 thread_info
*thread
= find_thread_global_id (var
->root
->thread_id
);
1962 switch_to_thread (thread
);
1963 within_scope
= check_scope (var
);
1970 /* We need to catch errors here, because if evaluate
1971 expression fails we want to just return NULL. */
1974 new_val
= var
->root
->exp
->evaluate ();
1976 catch (const gdb_exception_error
&except
)
1984 /* What is the ``struct value *'' of the root variable VAR?
1985 For floating variable object, evaluation can get us a value
1986 of different type from what is stored in varobj already. In
1988 - *type_changed will be set to 1
1989 - old varobj will be freed, and new one will be
1990 created, with the same name.
1991 - *var_handle will be set to the new varobj
1992 Otherwise, *type_changed will be set to 0. */
1993 static struct value
*
1994 value_of_root (struct varobj
**var_handle
, bool *type_changed
)
1998 if (var_handle
== NULL
)
2003 /* This should really be an exception, since this should
2004 only get called with a root variable. */
2006 if (!is_root_p (var
))
2009 if (var
->root
->floating
)
2011 struct varobj
*tmp_var
;
2013 tmp_var
= varobj_create (NULL
, var
->name
.c_str (), (CORE_ADDR
) 0,
2014 USE_SELECTED_FRAME
);
2015 if (tmp_var
== NULL
)
2019 std::string old_type
= varobj_get_type (var
);
2020 std::string new_type
= varobj_get_type (tmp_var
);
2021 if (old_type
== new_type
)
2023 /* The expression presently stored inside var->root->exp
2024 remembers the locations of local variables relatively to
2025 the frame where the expression was created (in DWARF location
2026 button, for example). Naturally, those locations are not
2027 correct in other frames, so update the expression. */
2029 std::swap (var
->root
->exp
, tmp_var
->root
->exp
);
2031 varobj_delete (tmp_var
, 0);
2036 tmp_var
->obj_name
= var
->obj_name
;
2037 tmp_var
->from
= var
->from
;
2038 tmp_var
->to
= var
->to
;
2039 varobj_delete (var
, 0);
2041 install_variable (tmp_var
);
2042 *var_handle
= tmp_var
;
2044 *type_changed
= true;
2053 struct value
*value
;
2055 value
= value_of_root_1 (var_handle
);
2056 if (var
->value
== NULL
|| value
== NULL
)
2058 /* For root varobj-s, a NULL value indicates a scoping issue.
2059 So, nothing to do in terms of checking for mutations. */
2061 else if (varobj_value_has_mutated (var
, value
, value
->type ()))
2063 /* The type has mutated, so the children are no longer valid.
2064 Just delete them, and tell our caller that the type has
2066 varobj_delete (var
, 1 /* only_children */);
2067 var
->num_children
= -1;
2070 *type_changed
= true;
2076 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2077 static struct value
*
2078 value_of_child (const struct varobj
*parent
, int index
)
2080 struct value
*value
;
2082 value
= (*parent
->root
->lang_ops
->value_of_child
) (parent
, index
);
2087 /* GDB already has a command called "value_of_variable". Sigh. */
2089 my_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2091 if (var
->root
->is_valid
)
2093 if (var
->dynamic
->pretty_printer
!= NULL
)
2094 return varobj_value_get_print_value (var
->value
.get (), var
->format
,
2096 else if (var
->parent
!= nullptr && varobj_is_dynamic_p (var
->parent
))
2097 return var
->print_value
;
2099 return (*var
->root
->lang_ops
->value_of_variable
) (var
, format
);
2102 return std::string ();
2106 varobj_formatted_print_options (struct value_print_options
*opts
,
2107 enum varobj_display_formats format
)
2109 get_formatted_print_options (opts
, format_code
[(int) format
]);
2110 opts
->deref_ref
= false;
2111 opts
->raw
= !pretty_printing
;
2115 varobj_value_get_print_value (struct value
*value
,
2116 enum varobj_display_formats format
,
2117 const struct varobj
*var
)
2119 struct value_print_options opts
;
2120 struct type
*type
= NULL
;
2122 gdb::unique_xmalloc_ptr
<char> encoding
;
2123 /* Initialize it just to avoid a GCC false warning. */
2124 CORE_ADDR str_addr
= 0;
2125 bool string_print
= false;
2128 return std::string ();
2131 std::string thevalue
;
2133 varobj_formatted_print_options (&opts
, format
);
2136 if (gdb_python_initialized
)
2138 PyObject
*value_formatter
= var
->dynamic
->pretty_printer
;
2140 gdbpy_enter_varobj
enter_py (var
);
2142 if (value_formatter
)
2144 if (PyObject_HasAttr (value_formatter
, gdbpy_to_string_cst
))
2146 struct value
*replacement
;
2148 gdbpy_ref
<> output
= apply_varobj_pretty_printer (value_formatter
,
2153 /* If we have string like output ... */
2154 if (output
!= nullptr && output
!= Py_None
)
2156 /* If this is a lazy string, extract it. For lazy
2157 strings we always print as a string, so set
2159 if (gdbpy_is_lazy_string (output
.get ()))
2161 gdbpy_extract_lazy_string (output
.get (), &str_addr
,
2162 &type
, &len
, &encoding
);
2163 string_print
= true;
2167 /* If it is a regular (non-lazy) string, extract
2168 it and copy the contents into THEVALUE. If the
2169 hint says to print it as a string, set
2170 string_print. Otherwise just return the extracted
2171 string as a value. */
2173 gdb::unique_xmalloc_ptr
<char> s
2174 = python_string_to_target_string (output
.get ());
2178 struct gdbarch
*gdbarch
;
2180 gdb::unique_xmalloc_ptr
<char> hint
2181 = gdbpy_get_display_hint (value_formatter
);
2184 if (!strcmp (hint
.get (), "string"))
2185 string_print
= true;
2188 thevalue
= std::string (s
.get ());
2189 len
= thevalue
.size ();
2190 gdbarch
= value
->type ()->arch ();
2191 type
= builtin_type (gdbarch
)->builtin_char
;
2197 gdbpy_print_stack ();
2200 /* If the printer returned a replacement value, set VALUE
2201 to REPLACEMENT. If there is not a replacement value,
2202 just use the value passed to this function. */
2204 value
= replacement
;
2208 /* No to_string method, so if there is a 'children'
2209 method, return the default. */
2210 if (PyObject_HasAttr (value_formatter
, gdbpy_children_cst
))
2216 /* If we've made it here, we don't want a pretty-printer --
2217 if we had one, it would already have been used. */
2223 /* If the THEVALUE has contents, it is a regular string. */
2224 if (!thevalue
.empty ())
2225 current_language
->printstr (&stb
, type
, (gdb_byte
*) thevalue
.c_str (),
2226 len
, encoding
.get (), 0, &opts
);
2227 else if (string_print
)
2228 /* Otherwise, if string_print is set, and it is not a regular
2229 string, it is a lazy string. */
2230 val_print_string (type
, encoding
.get (), str_addr
, len
, &stb
, &opts
);
2232 /* All other cases. */
2233 common_val_print (value
, &stb
, 0, &opts
, current_language
);
2235 return stb
.release ();
2239 varobj_editable_p (const struct varobj
*var
)
2243 if (!(var
->root
->is_valid
&& var
->value
!= nullptr
2244 && var
->value
->lval ()))
2247 type
= varobj_get_value_type (var
);
2249 switch (type
->code ())
2251 case TYPE_CODE_STRUCT
:
2252 case TYPE_CODE_UNION
:
2253 case TYPE_CODE_ARRAY
:
2254 case TYPE_CODE_FUNC
:
2255 case TYPE_CODE_METHOD
:
2265 /* Call VAR's value_is_changeable_p language-specific callback. */
2268 varobj_value_is_changeable_p (const struct varobj
*var
)
2270 return var
->root
->lang_ops
->value_is_changeable_p (var
);
2273 /* Return true if that varobj is floating, that is is always evaluated in the
2274 selected frame, and not bound to thread/frame. Such variable objects
2275 are created using '@' as frame specifier to -var-create. */
2277 varobj_floating_p (const struct varobj
*var
)
2279 return var
->root
->floating
;
2282 /* Implement the "value_is_changeable_p" varobj callback for most
2286 varobj_default_value_is_changeable_p (const struct varobj
*var
)
2291 if (CPLUS_FAKE_CHILD (var
))
2294 type
= varobj_get_value_type (var
);
2296 switch (type
->code ())
2298 case TYPE_CODE_STRUCT
:
2299 case TYPE_CODE_UNION
:
2300 case TYPE_CODE_ARRAY
:
2311 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback
2315 all_root_varobjs (gdb::function_view
<void (struct varobj
*var
)> func
)
2317 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
2318 auto iter
= rootlist
.begin ();
2319 auto end
= rootlist
.end ();
2323 func ((*self
)->rootvar
);
2327 /* Try to recreate the varobj VAR if it is a global or floating. This is a
2328 helper function for varobj_re_set. */
2331 varobj_re_set_iter (struct varobj
*var
)
2333 /* Invalidated global varobjs must be re-evaluated. */
2334 if (!var
->root
->is_valid
&& var
->root
->global
)
2336 struct varobj
*tmp_var
;
2338 /* Try to create a varobj with same expression. If we succeed
2339 and have a global replace the old varobj. */
2340 tmp_var
= varobj_create (nullptr, var
->name
.c_str (), (CORE_ADDR
) 0,
2342 if (tmp_var
!= nullptr && tmp_var
->root
->global
)
2344 tmp_var
->obj_name
= var
->obj_name
;
2345 varobj_delete (var
, 0);
2346 install_variable (tmp_var
);
2354 varobj_re_set (void)
2356 all_root_varobjs (varobj_re_set_iter
);
2359 /* Ensure that no varobj keep references to OBJFILE. */
2362 varobj_invalidate_if_uses_objfile (struct objfile
*objfile
)
2364 if (objfile
->separate_debug_objfile_backlink
!= nullptr)
2365 objfile
= objfile
->separate_debug_objfile_backlink
;
2367 all_root_varobjs ([objfile
] (struct varobj
*var
)
2369 if (var
->root
->valid_block
!= nullptr)
2371 struct objfile
*bl_objfile
= var
->root
->valid_block
->objfile ();
2372 if (bl_objfile
->separate_debug_objfile_backlink
!= nullptr)
2373 bl_objfile
= bl_objfile
->separate_debug_objfile_backlink
;
2375 if (bl_objfile
== objfile
)
2377 /* The varobj is tied to a block which is going away. There is
2378 no way to reconstruct something later, so invalidate the
2379 varobj completely and drop the reference to the block which is
2381 var
->root
->is_valid
= false;
2382 var
->root
->valid_block
= nullptr;
2386 if (var
->root
->exp
!= nullptr && var
->root
->exp
->uses_objfile (objfile
))
2388 /* The varobj's current expression references the objfile. For
2389 globals and floating, it is possible that when we try to
2390 re-evaluate the expression later it is still valid with
2391 whatever is in scope at that moment. Just invalidate the
2392 expression for now. */
2393 var
->root
->exp
.reset ();
2395 /* It only makes sense to keep a floating varobj around. */
2396 if (!var
->root
->floating
)
2397 var
->root
->is_valid
= false;
2400 /* var->value->type and var->type might also reference the objfile.
2401 This is taken care of in value.c:preserve_values which deals with
2402 making sure that objfile-owned types are replaced with
2403 gdbarch-owned equivalents. */
2407 /* A hash function for a varobj. */
2410 hash_varobj (const void *a
)
2412 const varobj
*obj
= (const varobj
*) a
;
2413 return htab_hash_string (obj
->obj_name
.c_str ());
2416 /* A hash table equality function for varobjs. */
2419 eq_varobj_and_string (const void *a
, const void *b
)
2421 const varobj
*obj
= (const varobj
*) a
;
2422 const char *name
= (const char *) b
;
2424 return obj
->obj_name
== name
;
2427 INIT_GDB_FILE (varobj
)
2429 varobj_table
= htab_create_alloc (5, hash_varobj
, eq_varobj_and_string
,
2430 nullptr, xcalloc
, xfree
);
2432 add_setshow_zuinteger_cmd ("varobj", class_maintenance
,
2434 _("Set varobj debugging."),
2435 _("Show varobj debugging."),
2436 _("When non-zero, varobj debugging is enabled."),
2437 NULL
, show_varobjdebug
,
2438 &setdebuglist
, &showdebuglist
);
2440 gdb::observers::free_objfile
.attach (varobj_invalidate_if_uses_objfile
,