1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2020 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/>. */
20 #include "expression.h"
26 #include "gdb_regex.h"
29 #include "gdbthread.h"
31 #include "varobj-iter.h"
32 #include "parser-defs.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 fprintf_filtered (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 /* Block for which this expression is valid. */
76 const struct block
*valid_block
= NULL
;
78 /* The frame for this expression. This field is set iff valid_block is
80 struct frame_id frame
= null_frame_id
;
82 /* The global thread ID that this varobj_root belongs to. This field
83 is only valid if valid_block is not NULL.
84 When not 0, indicates which thread 'frame' belongs to.
85 When 0, indicates that the thread list was empty when the varobj_root
89 /* If true, the -var-update always recomputes the value in the
90 current thread and frame. Otherwise, variable object is
91 always updated in the specific scope/thread/frame. */
92 bool floating
= false;
94 /* Flag that indicates validity: set to false when this varobj_root refers
95 to symbols that do not exist anymore. */
98 /* Language-related operations for this variable and its
100 const struct lang_varobj_ops
*lang_ops
= NULL
;
102 /* The varobj for this root node. */
103 struct varobj
*rootvar
= NULL
;
106 /* Dynamic part of varobj. */
108 struct varobj_dynamic
110 /* Whether the children of this varobj were requested. This field is
111 used to decide if dynamic varobj should recompute their children.
112 In the event that the frontend never asked for the children, we
114 bool children_requested
= false;
116 /* The pretty-printer constructor. If NULL, then the default
117 pretty-printer will be looked up. If None, then no
118 pretty-printer will be installed. */
119 PyObject
*constructor
= NULL
;
121 /* The pretty-printer that has been constructed. If NULL, then a
122 new printer object is needed, and one will be constructed. */
123 PyObject
*pretty_printer
= NULL
;
125 /* The iterator returned by the printer's 'children' method, or NULL
127 std::unique_ptr
<varobj_iter
> child_iter
;
129 /* We request one extra item from the iterator, so that we can
130 report to the caller whether there are more items than we have
131 already reported. However, we don't want to install this value
132 when we read it, because that will mess up future updates. So,
133 we stash it here instead. */
134 std::unique_ptr
<varobj_item
> saved_item
;
137 /* Private function prototypes */
139 /* Helper functions for the above subcommands. */
141 static int delete_variable (struct varobj
*, bool);
143 static void delete_variable_1 (int *, struct varobj
*, bool, bool);
145 static bool install_variable (struct varobj
*);
147 static void uninstall_variable (struct varobj
*);
149 static struct varobj
*create_child (struct varobj
*, int, std::string
&);
151 static struct varobj
*
152 create_child_with_value (struct varobj
*parent
, int index
,
153 struct varobj_item
*item
);
155 /* Utility routines */
157 static enum varobj_display_formats
variable_default_display (struct varobj
*);
159 static bool update_type_if_necessary (struct varobj
*var
,
160 struct value
*new_value
);
162 static bool install_new_value (struct varobj
*var
, struct value
*value
,
165 /* Language-specific routines. */
167 static int number_of_children (const struct varobj
*);
169 static std::string
name_of_variable (const struct varobj
*);
171 static std::string
name_of_child (struct varobj
*, int);
173 static struct value
*value_of_root (struct varobj
**var_handle
, bool *);
175 static struct value
*value_of_child (const struct varobj
*parent
, int index
);
177 static std::string
my_value_of_variable (struct varobj
*var
,
178 enum varobj_display_formats format
);
180 static bool is_root_p (const struct varobj
*var
);
182 static struct varobj
*varobj_add_child (struct varobj
*var
,
183 struct varobj_item
*item
);
187 /* Mappings of varobj_display_formats enums to gdb's format codes. */
188 static int format_code
[] = { 0, 't', 'd', 'x', 'o', 'z' };
190 /* List of root variable objects. */
191 static std::list
<struct varobj_root
*> rootlist
;
193 /* Pointer to the varobj hash table (built at run time). */
194 static htab_t varobj_table
;
198 /* API Implementation */
200 is_root_p (const struct varobj
*var
)
202 return (var
->root
->rootvar
== var
);
207 /* See python-internal.h. */
208 gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj
*var
)
209 : gdbpy_enter (var
->root
->exp
->gdbarch
, var
->root
->exp
->language_defn
)
215 /* Return the full FRAME which corresponds to the given CORE_ADDR
216 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
218 static struct frame_info
*
219 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
221 struct frame_info
*frame
= NULL
;
223 if (frame_addr
== (CORE_ADDR
) 0)
226 for (frame
= get_current_frame ();
228 frame
= get_prev_frame (frame
))
230 /* The CORE_ADDR we get as argument was parsed from a string GDB
231 output as $fp. This output got truncated to gdbarch_addr_bit.
232 Truncate the frame base address in the same manner before
233 comparing it against our argument. */
234 CORE_ADDR frame_base
= get_frame_base_address (frame
);
235 int addr_bit
= gdbarch_addr_bit (get_frame_arch (frame
));
237 if (addr_bit
< (sizeof (CORE_ADDR
) * HOST_CHAR_BIT
))
238 frame_base
&= ((CORE_ADDR
) 1 << addr_bit
) - 1;
240 if (frame_base
== frame_addr
)
247 /* Creates a varobj (not its children). */
250 varobj_create (const char *objname
,
251 const char *expression
, CORE_ADDR frame
, enum varobj_type type
)
253 /* Fill out a varobj structure for the (root) variable being constructed. */
254 std::unique_ptr
<varobj
> var (new varobj (new varobj_root
));
256 if (expression
!= NULL
)
258 struct frame_info
*fi
;
259 struct frame_id old_id
= null_frame_id
;
260 const struct block
*block
;
262 struct value
*value
= NULL
;
265 /* Parse and evaluate the expression, filling in as much of the
266 variable's data as possible. */
268 if (has_stack_frames ())
270 /* Allow creator to specify context of variable. */
271 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
272 fi
= get_selected_frame (NULL
);
274 /* FIXME: cagney/2002-11-23: This code should be doing a
275 lookup using the frame ID and not just the frame's
276 ``address''. This, of course, means an interface
277 change. However, with out that interface change ISAs,
278 such as the ia64 with its two stacks, won't work.
279 Similar goes for the case where there is a frameless
281 fi
= find_frame_addr_in_frame_chain (frame
);
286 if (type
== USE_SELECTED_FRAME
)
287 var
->root
->floating
= true;
293 block
= get_frame_block (fi
, 0);
294 pc
= get_frame_pc (fi
);
299 innermost_block_tracker
tracker (INNERMOST_BLOCK_FOR_SYMBOLS
300 | INNERMOST_BLOCK_FOR_REGISTERS
);
301 /* Wrap the call to parse expression, so we can
302 return a sensible error. */
305 var
->root
->exp
= parse_exp_1 (&p
, pc
, block
, 0, &tracker
);
308 catch (const gdb_exception_error
&except
)
313 /* Don't allow variables to be created for types. */
314 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
315 || var
->root
->exp
->elts
[0].opcode
== OP_TYPEOF
316 || var
->root
->exp
->elts
[0].opcode
== OP_DECLTYPE
)
318 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
319 " as an expression.\n");
323 var
->format
= variable_default_display (var
.get ());
324 var
->root
->valid_block
=
325 var
->root
->floating
? NULL
: tracker
.block ();
326 var
->name
= expression
;
327 /* For a root var, the name and the expr are the same. */
328 var
->path_expr
= expression
;
330 /* When the frame is different from the current frame,
331 we must select the appropriate frame before parsing
332 the expression, otherwise the value will not be current.
333 Since select_frame is so benign, just call it for all cases. */
334 if (var
->root
->valid_block
)
336 /* User could specify explicit FRAME-ADDR which was not found but
337 EXPRESSION is frame specific and we would not be able to evaluate
338 it correctly next time. With VALID_BLOCK set we must also set
339 FRAME and THREAD_ID. */
341 error (_("Failed to find the specified frame"));
343 var
->root
->frame
= get_frame_id (fi
);
344 var
->root
->thread_id
= inferior_thread ()->global_num
;
345 old_id
= get_frame_id (get_selected_frame (NULL
));
349 /* We definitely need to catch errors here.
350 If evaluate_expression succeeds we got the value we wanted.
351 But if it fails, we still go on with a call to evaluate_type(). */
354 value
= evaluate_expression (var
->root
->exp
.get ());
356 catch (const gdb_exception_error
&except
)
358 /* Error getting the value. Try to at least get the
360 struct value
*type_only_value
= evaluate_type (var
->root
->exp
.get ());
362 var
->type
= value_type (type_only_value
);
367 int real_type_found
= 0;
369 var
->type
= value_actual_type (value
, 0, &real_type_found
);
371 value
= value_cast (var
->type
, value
);
374 /* Set language info */
375 var
->root
->lang_ops
= var
->root
->exp
->language_defn
->varobj_ops ();
377 install_new_value (var
.get (), value
, 1 /* Initial assignment */);
379 /* Set ourselves as our root. */
380 var
->root
->rootvar
= var
.get ();
382 /* Reset the selected frame. */
383 if (frame_id_p (old_id
))
384 select_frame (frame_find_by_id (old_id
));
387 /* If the variable object name is null, that means this
388 is a temporary variable, so don't install it. */
390 if ((var
!= NULL
) && (objname
!= NULL
))
392 var
->obj_name
= objname
;
394 /* If a varobj name is duplicated, the install will fail so
396 if (!install_variable (var
.get ()))
400 return var
.release ();
403 /* Generates an unique name that can be used for a varobj. */
406 varobj_gen_name (void)
410 /* Generate a name for this object. */
412 return string_printf ("var%d", id
);
415 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
416 error if OBJNAME cannot be found. */
419 varobj_get_handle (const char *objname
)
421 varobj
*var
= (varobj
*) htab_find_with_hash (varobj_table
, objname
,
422 htab_hash_string (objname
));
425 error (_("Variable object not found"));
430 /* Given the handle, return the name of the object. */
433 varobj_get_objname (const struct varobj
*var
)
435 return var
->obj_name
.c_str ();
438 /* Given the handle, return the expression represented by the
442 varobj_get_expression (const struct varobj
*var
)
444 return name_of_variable (var
);
450 varobj_delete (struct varobj
*var
, bool only_children
)
452 return delete_variable (var
, only_children
);
457 /* Convenience function for varobj_set_visualizer. Instantiate a
458 pretty-printer for a given value. */
460 instantiate_pretty_printer (PyObject
*constructor
, struct value
*value
)
462 PyObject
*val_obj
= NULL
;
465 val_obj
= value_to_value_object (value
);
469 printer
= PyObject_CallFunctionObjArgs (constructor
, val_obj
, NULL
);
476 /* Set/Get variable object display format. */
478 enum varobj_display_formats
479 varobj_set_display_format (struct varobj
*var
,
480 enum varobj_display_formats format
)
487 case FORMAT_HEXADECIMAL
:
489 case FORMAT_ZHEXADECIMAL
:
490 var
->format
= format
;
494 var
->format
= variable_default_display (var
);
497 if (varobj_value_is_changeable_p (var
)
498 && var
->value
!= nullptr && !value_lazy (var
->value
.get ()))
500 var
->print_value
= varobj_value_get_print_value (var
->value
.get (),
507 enum varobj_display_formats
508 varobj_get_display_format (const struct varobj
*var
)
513 gdb::unique_xmalloc_ptr
<char>
514 varobj_get_display_hint (const struct varobj
*var
)
516 gdb::unique_xmalloc_ptr
<char> result
;
519 if (!gdb_python_initialized
)
522 gdbpy_enter_varobj
enter_py (var
);
524 if (var
->dynamic
->pretty_printer
!= NULL
)
525 result
= gdbpy_get_display_hint (var
->dynamic
->pretty_printer
);
531 /* Return true if the varobj has items after TO, false otherwise. */
534 varobj_has_more (const struct varobj
*var
, int to
)
536 if (var
->children
.size () > to
)
539 return ((to
== -1 || var
->children
.size () == to
)
540 && (var
->dynamic
->saved_item
!= NULL
));
543 /* If the variable object is bound to a specific thread, that
544 is its evaluation can always be done in context of a frame
545 inside that thread, returns GDB id of the thread -- which
546 is always positive. Otherwise, returns -1. */
548 varobj_get_thread_id (const struct varobj
*var
)
550 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
551 return var
->root
->thread_id
;
557 varobj_set_frozen (struct varobj
*var
, bool frozen
)
559 /* When a variable is unfrozen, we don't fetch its value.
560 The 'not_fetched' flag remains set, so next -var-update
563 We don't fetch the value, because for structures the client
564 should do -var-update anyway. It would be bad to have different
565 client-size logic for structure and other types. */
566 var
->frozen
= frozen
;
570 varobj_get_frozen (const struct varobj
*var
)
575 /* A helper function that updates the contents of FROM and TO based on the
576 size of the vector CHILDREN. If the contents of either FROM or TO are
577 negative the entire range is used. */
580 varobj_restrict_range (const std::vector
<varobj
*> &children
,
583 int len
= children
.size ();
585 if (*from
< 0 || *to
< 0)
601 /* A helper for update_dynamic_varobj_children that installs a new
602 child when needed. */
605 install_dynamic_child (struct varobj
*var
,
606 std::vector
<varobj
*> *changed
,
607 std::vector
<varobj
*> *type_changed
,
608 std::vector
<varobj
*> *newobj
,
609 std::vector
<varobj
*> *unchanged
,
612 struct varobj_item
*item
)
614 if (var
->children
.size () < index
+ 1)
616 /* There's no child yet. */
617 struct varobj
*child
= varobj_add_child (var
, item
);
621 newobj
->push_back (child
);
627 varobj
*existing
= var
->children
[index
];
628 bool type_updated
= update_type_if_necessary (existing
,
633 if (type_changed
!= NULL
)
634 type_changed
->push_back (existing
);
636 if (install_new_value (existing
, item
->value
.get (), 0))
638 if (!type_updated
&& changed
!= NULL
)
639 changed
->push_back (existing
);
641 else if (!type_updated
&& unchanged
!= NULL
)
642 unchanged
->push_back (existing
);
649 dynamic_varobj_has_child_method (const struct varobj
*var
)
651 PyObject
*printer
= var
->dynamic
->pretty_printer
;
653 if (!gdb_python_initialized
)
656 gdbpy_enter_varobj
enter_py (var
);
657 return PyObject_HasAttr (printer
, gdbpy_children_cst
);
661 /* A factory for creating dynamic varobj's iterators. Returns an
662 iterator object suitable for iterating over VAR's children. */
664 static std::unique_ptr
<varobj_iter
>
665 varobj_get_iterator (struct varobj
*var
)
668 if (var
->dynamic
->pretty_printer
)
669 return py_varobj_get_iterator (var
, var
->dynamic
->pretty_printer
);
672 gdb_assert_not_reached (_("\
673 requested an iterator from a non-dynamic varobj"));
677 update_dynamic_varobj_children (struct varobj
*var
,
678 std::vector
<varobj
*> *changed
,
679 std::vector
<varobj
*> *type_changed
,
680 std::vector
<varobj
*> *newobj
,
681 std::vector
<varobj
*> *unchanged
,
683 bool update_children
,
691 if (update_children
|| var
->dynamic
->child_iter
== NULL
)
693 var
->dynamic
->child_iter
= varobj_get_iterator (var
);
694 var
->dynamic
->saved_item
.reset (nullptr);
698 if (var
->dynamic
->child_iter
== NULL
)
702 i
= var
->children
.size ();
704 /* We ask for one extra child, so that MI can report whether there
705 are more children. */
706 for (; to
< 0 || i
< to
+ 1; ++i
)
708 std::unique_ptr
<varobj_item
> item
;
710 /* See if there was a leftover from last time. */
711 if (var
->dynamic
->saved_item
!= NULL
)
712 item
= std::move (var
->dynamic
->saved_item
);
714 item
= var
->dynamic
->child_iter
->next ();
718 /* Iteration is done. Remove iterator from VAR. */
719 var
->dynamic
->child_iter
.reset (nullptr);
722 /* We don't want to push the extra child on any report list. */
723 if (to
< 0 || i
< to
)
725 bool can_mention
= from
< 0 || i
>= from
;
727 install_dynamic_child (var
, can_mention
? changed
: NULL
,
728 can_mention
? type_changed
: NULL
,
729 can_mention
? newobj
: NULL
,
730 can_mention
? unchanged
: NULL
,
731 can_mention
? cchanged
: NULL
, i
,
736 var
->dynamic
->saved_item
= std::move (item
);
738 /* We want to truncate the child list just before this
744 if (i
< var
->children
.size ())
747 for (int j
= i
; j
< var
->children
.size (); ++j
)
748 varobj_delete (var
->children
[j
], 0);
750 var
->children
.resize (i
);
753 /* If there are fewer children than requested, note that the list of
755 if (to
>= 0 && var
->children
.size () < to
)
758 var
->num_children
= var
->children
.size ();
764 varobj_get_num_children (struct varobj
*var
)
766 if (var
->num_children
== -1)
768 if (varobj_is_dynamic_p (var
))
772 /* If we have a dynamic varobj, don't report -1 children.
773 So, try to fetch some children first. */
774 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
, &dummy
,
778 var
->num_children
= number_of_children (var
);
781 return var
->num_children
>= 0 ? var
->num_children
: 0;
784 /* Creates a list of the immediate children of a variable object;
785 the return code is the number of such children or -1 on error. */
787 const std::vector
<varobj
*> &
788 varobj_list_children (struct varobj
*var
, int *from
, int *to
)
790 var
->dynamic
->children_requested
= true;
792 if (varobj_is_dynamic_p (var
))
794 bool children_changed
;
796 /* This, in theory, can result in the number of children changing without
797 frontend noticing. But well, calling -var-list-children on the same
798 varobj twice is not something a sane frontend would do. */
799 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
,
800 &children_changed
, false, 0, *to
);
801 varobj_restrict_range (var
->children
, from
, to
);
802 return var
->children
;
805 if (var
->num_children
== -1)
806 var
->num_children
= number_of_children (var
);
808 /* If that failed, give up. */
809 if (var
->num_children
== -1)
810 return var
->children
;
812 /* If we're called when the list of children is not yet initialized,
813 allocate enough elements in it. */
814 while (var
->children
.size () < var
->num_children
)
815 var
->children
.push_back (NULL
);
817 for (int i
= 0; i
< var
->num_children
; i
++)
819 if (var
->children
[i
] == NULL
)
821 /* Either it's the first call to varobj_list_children for
822 this variable object, and the child was never created,
823 or it was explicitly deleted by the client. */
824 std::string name
= name_of_child (var
, i
);
825 var
->children
[i
] = create_child (var
, i
, name
);
829 varobj_restrict_range (var
->children
, from
, to
);
830 return var
->children
;
833 static struct varobj
*
834 varobj_add_child (struct varobj
*var
, struct varobj_item
*item
)
836 varobj
*v
= create_child_with_value (var
, var
->children
.size (), item
);
838 var
->children
.push_back (v
);
843 /* Obtain the type of an object Variable as a string similar to the one gdb
844 prints on the console. The caller is responsible for freeing the string.
848 varobj_get_type (struct varobj
*var
)
850 /* For the "fake" variables, do not return a type. (Its type is
852 Do not return a type for invalid variables as well. */
853 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
854 return std::string ();
856 return type_to_string (var
->type
);
859 /* Obtain the type of an object variable. */
862 varobj_get_gdb_type (const struct varobj
*var
)
867 /* Is VAR a path expression parent, i.e., can it be used to construct
868 a valid path expression? */
871 is_path_expr_parent (const struct varobj
*var
)
873 gdb_assert (var
->root
->lang_ops
->is_path_expr_parent
!= NULL
);
874 return var
->root
->lang_ops
->is_path_expr_parent (var
);
877 /* Is VAR a path expression parent, i.e., can it be used to construct
878 a valid path expression? By default we assume any VAR can be a path
882 varobj_default_is_path_expr_parent (const struct varobj
*var
)
887 /* Return the path expression parent for VAR. */
889 const struct varobj
*
890 varobj_get_path_expr_parent (const struct varobj
*var
)
892 const struct varobj
*parent
= var
;
894 while (!is_root_p (parent
) && !is_path_expr_parent (parent
))
895 parent
= parent
->parent
;
897 /* Computation of full rooted expression for children of dynamic
898 varobjs is not supported. */
899 if (varobj_is_dynamic_p (parent
))
900 error (_("Invalid variable object (child of a dynamic varobj)"));
905 /* Return a pointer to the full rooted expression of varobj VAR.
906 If it has not been computed yet, compute it. */
909 varobj_get_path_expr (const struct varobj
*var
)
911 if (var
->path_expr
.empty ())
913 /* For root varobjs, we initialize path_expr
914 when creating varobj, so here it should be
916 struct varobj
*mutable_var
= (struct varobj
*) var
;
917 gdb_assert (!is_root_p (var
));
919 mutable_var
->path_expr
= (*var
->root
->lang_ops
->path_expr_of_child
) (var
);
922 return var
->path_expr
.c_str ();
925 const struct language_defn
*
926 varobj_get_language (const struct varobj
*var
)
928 return var
->root
->exp
->language_defn
;
932 varobj_get_attributes (const struct varobj
*var
)
936 if (varobj_editable_p (var
))
937 /* FIXME: define masks for attributes. */
938 attributes
|= 0x00000001; /* Editable */
943 /* Return true if VAR is a dynamic varobj. */
946 varobj_is_dynamic_p (const struct varobj
*var
)
948 return var
->dynamic
->pretty_printer
!= NULL
;
952 varobj_get_formatted_value (struct varobj
*var
,
953 enum varobj_display_formats format
)
955 return my_value_of_variable (var
, format
);
959 varobj_get_value (struct varobj
*var
)
961 return my_value_of_variable (var
, var
->format
);
964 /* Set the value of an object variable (if it is editable) to the
965 value of the given expression. */
966 /* Note: Invokes functions that can call error(). */
969 varobj_set_value (struct varobj
*var
, const char *expression
)
971 struct value
*val
= NULL
; /* Initialize to keep gcc happy. */
972 /* The argument "expression" contains the variable's new value.
973 We need to first construct a legal expression for this -- ugh! */
974 /* Does this cover all the bases? */
975 struct value
*value
= NULL
; /* Initialize to keep gcc happy. */
976 int saved_input_radix
= input_radix
;
977 const char *s
= expression
;
979 gdb_assert (varobj_editable_p (var
));
981 input_radix
= 10; /* ALWAYS reset to decimal temporarily. */
982 expression_up exp
= parse_exp_1 (&s
, 0, 0, 0);
985 value
= evaluate_expression (exp
.get ());
988 catch (const gdb_exception_error
&except
)
990 /* We cannot proceed without a valid expression. */
994 /* All types that are editable must also be changeable. */
995 gdb_assert (varobj_value_is_changeable_p (var
));
997 /* The value of a changeable variable object must not be lazy. */
998 gdb_assert (!value_lazy (var
->value
.get ()));
1000 /* Need to coerce the input. We want to check if the
1001 value of the variable object will be different
1002 after assignment, and the first thing value_assign
1003 does is coerce the input.
1004 For example, if we are assigning an array to a pointer variable we
1005 should compare the pointer with the array's address, not with the
1007 value
= coerce_array (value
);
1009 /* The new value may be lazy. value_assign, or
1010 rather value_contents, will take care of this. */
1013 val
= value_assign (var
->value
.get (), value
);
1016 catch (const gdb_exception_error
&except
)
1021 /* If the value has changed, record it, so that next -var-update can
1022 report this change. If a variable had a value of '1', we've set it
1023 to '333' and then set again to '1', when -var-update will report this
1024 variable as changed -- because the first assignment has set the
1025 'updated' flag. There's no need to optimize that, because return value
1026 of -var-update should be considered an approximation. */
1027 var
->updated
= install_new_value (var
, val
, false /* Compare values. */);
1028 input_radix
= saved_input_radix
;
1034 /* A helper function to install a constructor function and visualizer
1035 in a varobj_dynamic. */
1038 install_visualizer (struct varobj_dynamic
*var
, PyObject
*constructor
,
1039 PyObject
*visualizer
)
1041 Py_XDECREF (var
->constructor
);
1042 var
->constructor
= constructor
;
1044 Py_XDECREF (var
->pretty_printer
);
1045 var
->pretty_printer
= visualizer
;
1047 var
->child_iter
.reset (nullptr);
1050 /* Install the default visualizer for VAR. */
1053 install_default_visualizer (struct varobj
*var
)
1055 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1056 if (CPLUS_FAKE_CHILD (var
))
1059 if (pretty_printing
)
1061 gdbpy_ref
<> pretty_printer
;
1063 if (var
->value
!= nullptr)
1065 pretty_printer
= gdbpy_get_varobj_pretty_printer (var
->value
.get ());
1066 if (pretty_printer
== nullptr)
1068 gdbpy_print_stack ();
1069 error (_("Cannot instantiate printer for default visualizer"));
1073 if (pretty_printer
== Py_None
)
1074 pretty_printer
.reset (nullptr);
1076 install_visualizer (var
->dynamic
, NULL
, pretty_printer
.release ());
1080 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1081 make a new object. */
1084 construct_visualizer (struct varobj
*var
, PyObject
*constructor
)
1086 PyObject
*pretty_printer
;
1088 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1089 if (CPLUS_FAKE_CHILD (var
))
1092 Py_INCREF (constructor
);
1093 if (constructor
== Py_None
)
1094 pretty_printer
= NULL
;
1097 pretty_printer
= instantiate_pretty_printer (constructor
,
1099 if (! pretty_printer
)
1101 gdbpy_print_stack ();
1102 Py_DECREF (constructor
);
1103 constructor
= Py_None
;
1104 Py_INCREF (constructor
);
1107 if (pretty_printer
== Py_None
)
1109 Py_DECREF (pretty_printer
);
1110 pretty_printer
= NULL
;
1114 install_visualizer (var
->dynamic
, constructor
, pretty_printer
);
1117 #endif /* HAVE_PYTHON */
1119 /* A helper function for install_new_value. This creates and installs
1120 a visualizer for VAR, if appropriate. */
1123 install_new_value_visualizer (struct varobj
*var
)
1126 /* If the constructor is None, then we want the raw value. If VAR
1127 does not have a value, just skip this. */
1128 if (!gdb_python_initialized
)
1131 if (var
->dynamic
->constructor
!= Py_None
&& var
->value
!= NULL
)
1133 gdbpy_enter_varobj
enter_py (var
);
1135 if (var
->dynamic
->constructor
== NULL
)
1136 install_default_visualizer (var
);
1138 construct_visualizer (var
, var
->dynamic
->constructor
);
1145 /* When using RTTI to determine variable type it may be changed in runtime when
1146 the variable value is changed. This function checks whether type of varobj
1147 VAR will change when a new value NEW_VALUE is assigned and if it is so
1148 updates the type of VAR. */
1151 update_type_if_necessary (struct varobj
*var
, struct value
*new_value
)
1155 struct value_print_options opts
;
1157 get_user_print_options (&opts
);
1158 if (opts
.objectprint
)
1160 struct type
*new_type
= value_actual_type (new_value
, 0, 0);
1161 std::string new_type_str
= type_to_string (new_type
);
1162 std::string curr_type_str
= varobj_get_type (var
);
1164 /* Did the type name change? */
1165 if (curr_type_str
!= new_type_str
)
1167 var
->type
= new_type
;
1169 /* This information may be not valid for a new type. */
1170 varobj_delete (var
, 1);
1171 var
->children
.clear ();
1172 var
->num_children
= -1;
1181 /* Assign a new value to a variable object. If INITIAL is true,
1182 this is the first assignment after the variable object was just
1183 created, or changed type. In that case, just assign the value
1185 Otherwise, assign the new value, and return true if the value is
1186 different from the current one, false otherwise. The comparison is
1187 done on textual representation of value. Therefore, some types
1188 need not be compared. E.g. for structures the reported value is
1189 always "{...}", so no comparison is necessary here. If the old
1190 value was NULL and new one is not, or vice versa, we always return true.
1192 The VALUE parameter should not be released -- the function will
1193 take care of releasing it when needed. */
1195 install_new_value (struct varobj
*var
, struct value
*value
, bool initial
)
1199 bool changed
= false;
1200 bool intentionally_not_fetched
= false;
1202 /* We need to know the varobj's type to decide if the value should
1203 be fetched or not. C++ fake children (public/protected/private)
1204 don't have a type. */
1205 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
1206 changeable
= varobj_value_is_changeable_p (var
);
1208 /* If the type has custom visualizer, we consider it to be always
1209 changeable. FIXME: need to make sure this behaviour will not
1210 mess up read-sensitive values. */
1211 if (var
->dynamic
->pretty_printer
!= NULL
)
1214 need_to_fetch
= changeable
;
1216 /* We are not interested in the address of references, and given
1217 that in C++ a reference is not rebindable, it cannot
1218 meaningfully change. So, get hold of the real value. */
1220 value
= coerce_ref (value
);
1222 if (var
->type
&& var
->type
->code () == TYPE_CODE_UNION
)
1223 /* For unions, we need to fetch the value implicitly because
1224 of implementation of union member fetch. When gdb
1225 creates a value for a field and the value of the enclosing
1226 structure is not lazy, it immediately copies the necessary
1227 bytes from the enclosing values. If the enclosing value is
1228 lazy, the call to value_fetch_lazy on the field will read
1229 the data from memory. For unions, that means we'll read the
1230 same memory more than once, which is not desirable. So
1232 need_to_fetch
= true;
1234 /* The new value might be lazy. If the type is changeable,
1235 that is we'll be comparing values of this type, fetch the
1236 value now. Otherwise, on the next update the old value
1237 will be lazy, which means we've lost that old value. */
1238 if (need_to_fetch
&& value
&& value_lazy (value
))
1240 const struct varobj
*parent
= var
->parent
;
1241 bool frozen
= var
->frozen
;
1243 for (; !frozen
&& parent
; parent
= parent
->parent
)
1244 frozen
|= parent
->frozen
;
1246 if (frozen
&& initial
)
1248 /* For variables that are frozen, or are children of frozen
1249 variables, we don't do fetch on initial assignment.
1250 For non-initial assignment we do the fetch, since it means we're
1251 explicitly asked to compare the new value with the old one. */
1252 intentionally_not_fetched
= true;
1259 value_fetch_lazy (value
);
1262 catch (const gdb_exception_error
&except
)
1264 /* Set the value to NULL, so that for the next -var-update,
1265 we don't try to compare the new value with this value,
1266 that we couldn't even read. */
1272 /* Get a reference now, before possibly passing it to any Python
1273 code that might release it. */
1274 value_ref_ptr value_holder
;
1276 value_holder
= value_ref_ptr::new_reference (value
);
1278 /* Below, we'll be comparing string rendering of old and new
1279 values. Don't get string rendering if the value is
1280 lazy -- if it is, the code above has decided that the value
1281 should not be fetched. */
1282 std::string print_value
;
1283 if (value
!= NULL
&& !value_lazy (value
)
1284 && var
->dynamic
->pretty_printer
== NULL
)
1285 print_value
= varobj_value_get_print_value (value
, var
->format
, var
);
1287 /* If the type is changeable, compare the old and the new values.
1288 If this is the initial assignment, we don't have any old value
1290 if (!initial
&& changeable
)
1292 /* If the value of the varobj was changed by -var-set-value,
1293 then the value in the varobj and in the target is the same.
1294 However, that value is different from the value that the
1295 varobj had after the previous -var-update. So need to the
1296 varobj as changed. */
1299 else if (var
->dynamic
->pretty_printer
== NULL
)
1301 /* Try to compare the values. That requires that both
1302 values are non-lazy. */
1303 if (var
->not_fetched
&& value_lazy (var
->value
.get ()))
1305 /* This is a frozen varobj and the value was never read.
1306 Presumably, UI shows some "never read" indicator.
1307 Now that we've fetched the real value, we need to report
1308 this varobj as changed so that UI can show the real
1312 else if (var
->value
== NULL
&& value
== NULL
)
1315 else if (var
->value
== NULL
|| value
== NULL
)
1321 gdb_assert (!value_lazy (var
->value
.get ()));
1322 gdb_assert (!value_lazy (value
));
1324 gdb_assert (!var
->print_value
.empty () && !print_value
.empty ());
1325 if (var
->print_value
!= print_value
)
1331 if (!initial
&& !changeable
)
1333 /* For values that are not changeable, we don't compare the values.
1334 However, we want to notice if a value was not NULL and now is NULL,
1335 or vise versa, so that we report when top-level varobjs come in scope
1336 and leave the scope. */
1337 changed
= (var
->value
!= NULL
) != (value
!= NULL
);
1340 /* We must always keep the new value, since children depend on it. */
1341 var
->value
= value_holder
;
1342 if (value
&& value_lazy (value
) && intentionally_not_fetched
)
1343 var
->not_fetched
= true;
1345 var
->not_fetched
= false;
1346 var
->updated
= false;
1348 install_new_value_visualizer (var
);
1350 /* If we installed a pretty-printer, re-compare the printed version
1351 to see if the variable changed. */
1352 if (var
->dynamic
->pretty_printer
!= NULL
)
1354 print_value
= varobj_value_get_print_value (var
->value
.get (),
1356 if ((var
->print_value
.empty () && !print_value
.empty ())
1357 || (!var
->print_value
.empty () && print_value
.empty ())
1358 || (!var
->print_value
.empty () && !print_value
.empty ()
1359 && var
->print_value
!= print_value
))
1362 var
->print_value
= print_value
;
1364 gdb_assert (var
->value
== nullptr || value_type (var
->value
.get ()));
1369 /* Return the requested range for a varobj. VAR is the varobj. FROM
1370 and TO are out parameters; *FROM and *TO will be set to the
1371 selected sub-range of VAR. If no range was selected using
1372 -var-set-update-range, then both will be -1. */
1374 varobj_get_child_range (const struct varobj
*var
, int *from
, int *to
)
1380 /* Set the selected sub-range of children of VAR to start at index
1381 FROM and end at index TO. If either FROM or TO is less than zero,
1382 this is interpreted as a request for all children. */
1384 varobj_set_child_range (struct varobj
*var
, int from
, int to
)
1391 varobj_set_visualizer (struct varobj
*var
, const char *visualizer
)
1396 if (!gdb_python_initialized
)
1399 gdbpy_enter_varobj
enter_py (var
);
1401 mainmod
= PyImport_AddModule ("__main__");
1403 = gdbpy_ref
<>::new_reference (PyModule_GetDict (mainmod
));
1404 gdbpy_ref
<> constructor (PyRun_String (visualizer
, Py_eval_input
,
1405 globals
.get (), globals
.get ()));
1407 if (constructor
== NULL
)
1409 gdbpy_print_stack ();
1410 error (_("Could not evaluate visualizer expression: %s"), visualizer
);
1413 construct_visualizer (var
, constructor
.get ());
1415 /* If there are any children now, wipe them. */
1416 varobj_delete (var
, 1 /* children only */);
1417 var
->num_children
= -1;
1419 error (_("Python support required"));
1423 /* If NEW_VALUE is the new value of the given varobj (var), return
1424 true if var has mutated. In other words, if the type of
1425 the new value is different from the type of the varobj's old
1428 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1431 varobj_value_has_mutated (const struct varobj
*var
, struct value
*new_value
,
1432 struct type
*new_type
)
1434 /* If we haven't previously computed the number of children in var,
1435 it does not matter from the front-end's perspective whether
1436 the type has mutated or not. For all intents and purposes,
1437 it has not mutated. */
1438 if (var
->num_children
< 0)
1441 if (var
->root
->lang_ops
->value_has_mutated
!= NULL
)
1443 /* The varobj module, when installing new values, explicitly strips
1444 references, saying that we're not interested in those addresses.
1445 But detection of mutation happens before installing the new
1446 value, so our value may be a reference that we need to strip
1447 in order to remain consistent. */
1448 if (new_value
!= NULL
)
1449 new_value
= coerce_ref (new_value
);
1450 return var
->root
->lang_ops
->value_has_mutated (var
, new_value
, new_type
);
1456 /* Update the values for a variable and its children. This is a
1457 two-pronged attack. First, re-parse the value for the root's
1458 expression to see if it's changed. Then go all the way
1459 through its children, reconstructing them and noting if they've
1462 The IS_EXPLICIT parameter specifies if this call is result
1463 of MI request to update this specific variable, or
1464 result of implicit -var-update *. For implicit request, we don't
1465 update frozen variables.
1467 NOTE: This function may delete the caller's varobj. If it
1468 returns TYPE_CHANGED, then it has done this and VARP will be modified
1469 to point to the new varobj. */
1471 std::vector
<varobj_update_result
>
1472 varobj_update (struct varobj
**varp
, bool is_explicit
)
1474 bool type_changed
= false;
1475 struct value
*newobj
;
1476 std::vector
<varobj_update_result
> stack
;
1477 std::vector
<varobj_update_result
> result
;
1479 /* Frozen means frozen -- we don't check for any change in
1480 this varobj, including its going out of scope, or
1481 changing type. One use case for frozen varobjs is
1482 retaining previously evaluated expressions, and we don't
1483 want them to be reevaluated at all. */
1484 if (!is_explicit
&& (*varp
)->frozen
)
1487 if (!(*varp
)->root
->is_valid
)
1489 result
.emplace_back (*varp
, VAROBJ_INVALID
);
1493 if ((*varp
)->root
->rootvar
== *varp
)
1495 varobj_update_result
r (*varp
);
1497 /* Update the root variable. value_of_root can return NULL
1498 if the variable is no longer around, i.e. we stepped out of
1499 the frame in which a local existed. We are letting the
1500 value_of_root variable dispose of the varobj if the type
1502 newobj
= value_of_root (varp
, &type_changed
);
1503 if (update_type_if_necessary (*varp
, newobj
))
1504 type_changed
= true;
1506 r
.type_changed
= type_changed
;
1507 if (install_new_value ((*varp
), newobj
, type_changed
))
1511 r
.status
= VAROBJ_NOT_IN_SCOPE
;
1512 r
.value_installed
= true;
1514 if (r
.status
== VAROBJ_NOT_IN_SCOPE
)
1516 if (r
.type_changed
|| r
.changed
)
1517 result
.push_back (std::move (r
));
1522 stack
.push_back (std::move (r
));
1525 stack
.emplace_back (*varp
);
1527 /* Walk through the children, reconstructing them all. */
1528 while (!stack
.empty ())
1530 varobj_update_result r
= std::move (stack
.back ());
1532 struct varobj
*v
= r
.varobj
;
1534 /* Update this variable, unless it's a root, which is already
1536 if (!r
.value_installed
)
1538 struct type
*new_type
;
1540 newobj
= value_of_child (v
->parent
, v
->index
);
1541 if (update_type_if_necessary (v
, newobj
))
1542 r
.type_changed
= true;
1544 new_type
= value_type (newobj
);
1546 new_type
= v
->root
->lang_ops
->type_of_child (v
->parent
, v
->index
);
1548 if (varobj_value_has_mutated (v
, newobj
, new_type
))
1550 /* The children are no longer valid; delete them now.
1551 Report the fact that its type changed as well. */
1552 varobj_delete (v
, 1 /* only_children */);
1553 v
->num_children
= -1;
1557 r
.type_changed
= true;
1560 if (install_new_value (v
, newobj
, r
.type_changed
))
1567 /* We probably should not get children of a dynamic varobj, but
1568 for which -var-list-children was never invoked. */
1569 if (varobj_is_dynamic_p (v
))
1571 std::vector
<varobj
*> changed
, type_changed_vec
, unchanged
, newobj_vec
;
1572 bool children_changed
= false;
1577 if (!v
->dynamic
->children_requested
)
1581 /* If we initially did not have potential children, but
1582 now we do, consider the varobj as changed.
1583 Otherwise, if children were never requested, consider
1584 it as unchanged -- presumably, such varobj is not yet
1585 expanded in the UI, so we need not bother getting
1587 if (!varobj_has_more (v
, 0))
1589 update_dynamic_varobj_children (v
, NULL
, NULL
, NULL
, NULL
,
1590 &dummy
, false, 0, 0);
1591 if (varobj_has_more (v
, 0))
1596 result
.push_back (std::move (r
));
1601 /* If update_dynamic_varobj_children returns false, then we have
1602 a non-conforming pretty-printer, so we skip it. */
1603 if (update_dynamic_varobj_children (v
, &changed
, &type_changed_vec
,
1605 &unchanged
, &children_changed
,
1606 true, v
->from
, v
->to
))
1608 if (children_changed
|| !newobj_vec
.empty ())
1610 r
.children_changed
= true;
1611 r
.newobj
= std::move (newobj_vec
);
1613 /* Push in reverse order so that the first child is
1614 popped from the work stack first, and so will be
1615 added to result first. This does not affect
1616 correctness, just "nicer". */
1617 for (int i
= type_changed_vec
.size () - 1; i
>= 0; --i
)
1619 varobj_update_result
item (type_changed_vec
[i
]);
1621 /* Type may change only if value was changed. */
1622 item
.changed
= true;
1623 item
.type_changed
= true;
1624 item
.value_installed
= true;
1626 stack
.push_back (std::move (item
));
1628 for (int i
= changed
.size () - 1; i
>= 0; --i
)
1630 varobj_update_result
item (changed
[i
]);
1632 item
.changed
= true;
1633 item
.value_installed
= true;
1635 stack
.push_back (std::move (item
));
1637 for (int i
= unchanged
.size () - 1; i
>= 0; --i
)
1639 if (!unchanged
[i
]->frozen
)
1641 varobj_update_result
item (unchanged
[i
]);
1643 item
.value_installed
= true;
1645 stack
.push_back (std::move (item
));
1648 if (r
.changed
|| r
.children_changed
)
1649 result
.push_back (std::move (r
));
1655 /* Push any children. Use reverse order so that the first
1656 child is popped from the work stack first, and so
1657 will be added to result first. This does not
1658 affect correctness, just "nicer". */
1659 for (int i
= v
->children
.size () - 1; i
>= 0; --i
)
1661 varobj
*c
= v
->children
[i
];
1663 /* Child may be NULL if explicitly deleted by -var-delete. */
1664 if (c
!= NULL
&& !c
->frozen
)
1665 stack
.emplace_back (c
);
1668 if (r
.changed
|| r
.type_changed
)
1669 result
.push_back (std::move (r
));
1675 /* Helper functions */
1678 * Variable object construction/destruction
1682 delete_variable (struct varobj
*var
, bool only_children_p
)
1686 delete_variable_1 (&delcount
, var
, only_children_p
,
1687 true /* remove_from_parent_p */ );
1692 /* Delete the variable object VAR and its children. */
1693 /* IMPORTANT NOTE: If we delete a variable which is a child
1694 and the parent is not removed we dump core. It must be always
1695 initially called with remove_from_parent_p set. */
1697 delete_variable_1 (int *delcountp
, struct varobj
*var
, bool only_children_p
,
1698 bool remove_from_parent_p
)
1700 /* Delete any children of this variable, too. */
1701 for (varobj
*child
: var
->children
)
1706 if (!remove_from_parent_p
)
1707 child
->parent
= NULL
;
1709 delete_variable_1 (delcountp
, child
, false, only_children_p
);
1711 var
->children
.clear ();
1713 /* if we were called to delete only the children we are done here. */
1714 if (only_children_p
)
1717 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
1718 /* If the name is empty, this is a temporary variable, that has not
1719 yet been installed, don't report it, it belongs to the caller... */
1720 if (!var
->obj_name
.empty ())
1722 *delcountp
= *delcountp
+ 1;
1725 /* If this variable has a parent, remove it from its parent's list. */
1726 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1727 (as indicated by remove_from_parent_p) we don't bother doing an
1728 expensive list search to find the element to remove when we are
1729 discarding the list afterwards. */
1730 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1731 var
->parent
->children
[var
->index
] = NULL
;
1733 if (!var
->obj_name
.empty ())
1734 uninstall_variable (var
);
1736 /* Free memory associated with this variable. */
1740 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1742 install_variable (struct varobj
*var
)
1744 hashval_t hash
= htab_hash_string (var
->obj_name
.c_str ());
1745 void **slot
= htab_find_slot_with_hash (varobj_table
,
1746 var
->obj_name
.c_str (),
1748 if (*slot
!= nullptr)
1749 error (_("Duplicate variable object name"));
1751 /* Add varobj to hash table. */
1754 /* If root, add varobj to root list. */
1755 if (is_root_p (var
))
1756 rootlist
.push_front (var
->root
);
1758 return true; /* OK */
1761 /* Uninstall the object VAR. */
1763 uninstall_variable (struct varobj
*var
)
1765 hashval_t hash
= htab_hash_string (var
->obj_name
.c_str ());
1766 htab_remove_elt_with_hash (varobj_table
, var
->obj_name
.c_str (), hash
);
1769 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
.c_str ());
1771 /* If root, remove varobj from root list. */
1772 if (is_root_p (var
))
1774 auto iter
= std::find (rootlist
.begin (), rootlist
.end (), var
->root
);
1775 rootlist
.erase (iter
);
1779 /* Create and install a child of the parent of the given name.
1781 The created VAROBJ takes ownership of the allocated NAME. */
1783 static struct varobj
*
1784 create_child (struct varobj
*parent
, int index
, std::string
&name
)
1786 struct varobj_item item
;
1788 std::swap (item
.name
, name
);
1789 item
.value
= release_value (value_of_child (parent
, index
));
1791 return create_child_with_value (parent
, index
, &item
);
1794 static struct varobj
*
1795 create_child_with_value (struct varobj
*parent
, int index
,
1796 struct varobj_item
*item
)
1798 varobj
*child
= new varobj (parent
->root
);
1800 /* NAME is allocated by caller. */
1801 std::swap (child
->name
, item
->name
);
1802 child
->index
= index
;
1803 child
->parent
= parent
;
1805 if (varobj_is_anonymous_child (child
))
1806 child
->obj_name
= string_printf ("%s.%d_anonymous",
1807 parent
->obj_name
.c_str (), index
);
1809 child
->obj_name
= string_printf ("%s.%s",
1810 parent
->obj_name
.c_str (),
1811 child
->name
.c_str ());
1813 install_variable (child
);
1815 /* Compute the type of the child. Must do this before
1816 calling install_new_value. */
1817 if (item
->value
!= NULL
)
1818 /* If the child had no evaluation errors, var->value
1819 will be non-NULL and contain a valid type. */
1820 child
->type
= value_actual_type (item
->value
.get (), 0, NULL
);
1822 /* Otherwise, we must compute the type. */
1823 child
->type
= (*child
->root
->lang_ops
->type_of_child
) (child
->parent
,
1825 install_new_value (child
, item
->value
.get (), 1);
1832 * Miscellaneous utility functions.
1835 /* Allocate memory and initialize a new variable. */
1836 varobj::varobj (varobj_root
*root_
)
1837 : root (root_
), dynamic (new varobj_dynamic
)
1841 /* Free any allocated memory associated with VAR. */
1848 if (var
->dynamic
->pretty_printer
!= NULL
)
1850 gdbpy_enter_varobj
enter_py (var
);
1852 Py_XDECREF (var
->dynamic
->constructor
);
1853 Py_XDECREF (var
->dynamic
->pretty_printer
);
1857 if (is_root_p (var
))
1860 delete var
->dynamic
;
1863 /* Return the type of the value that's stored in VAR,
1864 or that would have being stored there if the
1865 value were accessible.
1867 This differs from VAR->type in that VAR->type is always
1868 the true type of the expression in the source language.
1869 The return value of this function is the type we're
1870 actually storing in varobj, and using for displaying
1871 the values and for comparing previous and new values.
1873 For example, top-level references are always stripped. */
1875 varobj_get_value_type (const struct varobj
*var
)
1879 if (var
->value
!= nullptr)
1880 type
= value_type (var
->value
.get ());
1884 type
= check_typedef (type
);
1886 if (TYPE_IS_REFERENCE (type
))
1887 type
= get_target_type (type
);
1889 type
= check_typedef (type
);
1894 /* What is the default display for this variable? We assume that
1895 everything is "natural". Any exceptions? */
1896 static enum varobj_display_formats
1897 variable_default_display (struct varobj
*var
)
1899 return FORMAT_NATURAL
;
1903 * Language-dependencies
1906 /* Common entry points */
1908 /* Return the number of children for a given variable.
1909 The result of this function is defined by the language
1910 implementation. The number of children returned by this function
1911 is the number of children that the user will see in the variable
1914 number_of_children (const struct varobj
*var
)
1916 return (*var
->root
->lang_ops
->number_of_children
) (var
);
1919 /* What is the expression for the root varobj VAR? */
1922 name_of_variable (const struct varobj
*var
)
1924 return (*var
->root
->lang_ops
->name_of_variable
) (var
);
1927 /* What is the name of the INDEX'th child of VAR? */
1930 name_of_child (struct varobj
*var
, int index
)
1932 return (*var
->root
->lang_ops
->name_of_child
) (var
, index
);
1935 /* If frame associated with VAR can be found, switch
1936 to it and return true. Otherwise, return false. */
1939 check_scope (const struct varobj
*var
)
1941 struct frame_info
*fi
;
1944 fi
= frame_find_by_id (var
->root
->frame
);
1949 CORE_ADDR pc
= get_frame_pc (fi
);
1951 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
1952 pc
>= BLOCK_END (var
->root
->valid_block
))
1960 /* Helper function to value_of_root. */
1962 static struct value
*
1963 value_of_root_1 (struct varobj
**var_handle
)
1965 struct value
*new_val
= NULL
;
1966 struct varobj
*var
= *var_handle
;
1967 bool within_scope
= false;
1969 /* Only root variables can be updated... */
1970 if (!is_root_p (var
))
1971 /* Not a root var. */
1974 scoped_restore_current_thread restore_thread
;
1976 /* Determine whether the variable is still around. */
1977 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
1978 within_scope
= true;
1979 else if (var
->root
->thread_id
== 0)
1981 /* The program was single-threaded when the variable object was
1982 created. Technically, it's possible that the program became
1983 multi-threaded since then, but we don't support such
1985 within_scope
= check_scope (var
);
1989 thread_info
*thread
= find_thread_global_id (var
->root
->thread_id
);
1993 switch_to_thread (thread
);
1994 within_scope
= check_scope (var
);
2001 /* We need to catch errors here, because if evaluate
2002 expression fails we want to just return NULL. */
2005 new_val
= evaluate_expression (var
->root
->exp
.get ());
2007 catch (const gdb_exception_error
&except
)
2015 /* What is the ``struct value *'' of the root variable VAR?
2016 For floating variable object, evaluation can get us a value
2017 of different type from what is stored in varobj already. In
2019 - *type_changed will be set to 1
2020 - old varobj will be freed, and new one will be
2021 created, with the same name.
2022 - *var_handle will be set to the new varobj
2023 Otherwise, *type_changed will be set to 0. */
2024 static struct value
*
2025 value_of_root (struct varobj
**var_handle
, bool *type_changed
)
2029 if (var_handle
== NULL
)
2034 /* This should really be an exception, since this should
2035 only get called with a root variable. */
2037 if (!is_root_p (var
))
2040 if (var
->root
->floating
)
2042 struct varobj
*tmp_var
;
2044 tmp_var
= varobj_create (NULL
, var
->name
.c_str (), (CORE_ADDR
) 0,
2045 USE_SELECTED_FRAME
);
2046 if (tmp_var
== NULL
)
2050 std::string old_type
= varobj_get_type (var
);
2051 std::string new_type
= varobj_get_type (tmp_var
);
2052 if (old_type
== new_type
)
2054 /* The expression presently stored inside var->root->exp
2055 remembers the locations of local variables relatively to
2056 the frame where the expression was created (in DWARF location
2057 button, for example). Naturally, those locations are not
2058 correct in other frames, so update the expression. */
2060 std::swap (var
->root
->exp
, tmp_var
->root
->exp
);
2062 varobj_delete (tmp_var
, 0);
2067 tmp_var
->obj_name
= var
->obj_name
;
2068 tmp_var
->from
= var
->from
;
2069 tmp_var
->to
= var
->to
;
2070 varobj_delete (var
, 0);
2072 install_variable (tmp_var
);
2073 *var_handle
= tmp_var
;
2075 *type_changed
= true;
2084 struct value
*value
;
2086 value
= value_of_root_1 (var_handle
);
2087 if (var
->value
== NULL
|| value
== NULL
)
2089 /* For root varobj-s, a NULL value indicates a scoping issue.
2090 So, nothing to do in terms of checking for mutations. */
2092 else if (varobj_value_has_mutated (var
, value
, value_type (value
)))
2094 /* The type has mutated, so the children are no longer valid.
2095 Just delete them, and tell our caller that the type has
2097 varobj_delete (var
, 1 /* only_children */);
2098 var
->num_children
= -1;
2101 *type_changed
= true;
2107 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2108 static struct value
*
2109 value_of_child (const struct varobj
*parent
, int index
)
2111 struct value
*value
;
2113 value
= (*parent
->root
->lang_ops
->value_of_child
) (parent
, index
);
2118 /* GDB already has a command called "value_of_variable". Sigh. */
2120 my_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2122 if (var
->root
->is_valid
)
2124 if (var
->dynamic
->pretty_printer
!= NULL
)
2125 return varobj_value_get_print_value (var
->value
.get (), var
->format
,
2127 return (*var
->root
->lang_ops
->value_of_variable
) (var
, format
);
2130 return std::string ();
2134 varobj_formatted_print_options (struct value_print_options
*opts
,
2135 enum varobj_display_formats format
)
2137 get_formatted_print_options (opts
, format_code
[(int) format
]);
2138 opts
->deref_ref
= 0;
2139 opts
->raw
= !pretty_printing
;
2143 varobj_value_get_print_value (struct value
*value
,
2144 enum varobj_display_formats format
,
2145 const struct varobj
*var
)
2147 struct value_print_options opts
;
2148 struct type
*type
= NULL
;
2150 gdb::unique_xmalloc_ptr
<char> encoding
;
2151 /* Initialize it just to avoid a GCC false warning. */
2152 CORE_ADDR str_addr
= 0;
2153 bool string_print
= false;
2156 return std::string ();
2159 std::string thevalue
;
2162 if (gdb_python_initialized
)
2164 PyObject
*value_formatter
= var
->dynamic
->pretty_printer
;
2166 gdbpy_enter_varobj
enter_py (var
);
2168 if (value_formatter
)
2170 /* First check to see if we have any children at all. If so,
2171 we simply return {...}. */
2172 if (dynamic_varobj_has_child_method (var
))
2175 if (PyObject_HasAttr (value_formatter
, gdbpy_to_string_cst
))
2177 struct value
*replacement
;
2179 gdbpy_ref
<> output
= apply_varobj_pretty_printer (value_formatter
,
2183 /* If we have string like output ... */
2186 /* If this is a lazy string, extract it. For lazy
2187 strings we always print as a string, so set
2189 if (gdbpy_is_lazy_string (output
.get ()))
2191 gdbpy_extract_lazy_string (output
.get (), &str_addr
,
2192 &type
, &len
, &encoding
);
2193 string_print
= true;
2197 /* If it is a regular (non-lazy) string, extract
2198 it and copy the contents into THEVALUE. If the
2199 hint says to print it as a string, set
2200 string_print. Otherwise just return the extracted
2201 string as a value. */
2203 gdb::unique_xmalloc_ptr
<char> s
2204 = python_string_to_target_string (output
.get ());
2208 struct gdbarch
*gdbarch
;
2210 gdb::unique_xmalloc_ptr
<char> hint
2211 = gdbpy_get_display_hint (value_formatter
);
2214 if (!strcmp (hint
.get (), "string"))
2215 string_print
= true;
2218 thevalue
= std::string (s
.get ());
2219 len
= thevalue
.size ();
2220 gdbarch
= get_type_arch (value_type (value
));
2221 type
= builtin_type (gdbarch
)->builtin_char
;
2227 gdbpy_print_stack ();
2230 /* If the printer returned a replacement value, set VALUE
2231 to REPLACEMENT. If there is not a replacement value,
2232 just use the value passed to this function. */
2234 value
= replacement
;
2240 varobj_formatted_print_options (&opts
, format
);
2242 /* If the THEVALUE has contents, it is a regular string. */
2243 if (!thevalue
.empty ())
2244 LA_PRINT_STRING (&stb
, type
, (gdb_byte
*) thevalue
.c_str (),
2245 len
, encoding
.get (), 0, &opts
);
2246 else if (string_print
)
2247 /* Otherwise, if string_print is set, and it is not a regular
2248 string, it is a lazy string. */
2249 val_print_string (type
, encoding
.get (), str_addr
, len
, &stb
, &opts
);
2251 /* All other cases. */
2252 common_val_print (value
, &stb
, 0, &opts
, current_language
);
2254 return std::move (stb
.string ());
2258 varobj_editable_p (const struct varobj
*var
)
2262 if (!(var
->root
->is_valid
&& var
->value
!= nullptr
2263 && VALUE_LVAL (var
->value
.get ())))
2266 type
= varobj_get_value_type (var
);
2268 switch (type
->code ())
2270 case TYPE_CODE_STRUCT
:
2271 case TYPE_CODE_UNION
:
2272 case TYPE_CODE_ARRAY
:
2273 case TYPE_CODE_FUNC
:
2274 case TYPE_CODE_METHOD
:
2284 /* Call VAR's value_is_changeable_p language-specific callback. */
2287 varobj_value_is_changeable_p (const struct varobj
*var
)
2289 return var
->root
->lang_ops
->value_is_changeable_p (var
);
2292 /* Return true if that varobj is floating, that is is always evaluated in the
2293 selected frame, and not bound to thread/frame. Such variable objects
2294 are created using '@' as frame specifier to -var-create. */
2296 varobj_floating_p (const struct varobj
*var
)
2298 return var
->root
->floating
;
2301 /* Implement the "value_is_changeable_p" varobj callback for most
2305 varobj_default_value_is_changeable_p (const struct varobj
*var
)
2310 if (CPLUS_FAKE_CHILD (var
))
2313 type
= varobj_get_value_type (var
);
2315 switch (type
->code ())
2317 case TYPE_CODE_STRUCT
:
2318 case TYPE_CODE_UNION
:
2319 case TYPE_CODE_ARRAY
:
2330 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback
2334 all_root_varobjs (gdb::function_view
<void (struct varobj
*var
)> func
)
2336 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
2337 auto iter
= rootlist
.begin ();
2338 auto end
= rootlist
.end ();
2342 func ((*self
)->rootvar
);
2346 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
2347 defined on globals. It is a helper for varobj_invalidate.
2349 This function is called after changing the symbol file, in this case the
2350 pointers to "struct type" stored by the varobj are no longer valid. All
2351 varobj must be either re-evaluated, or marked as invalid here. */
2354 varobj_invalidate_iter (struct varobj
*var
)
2356 /* global and floating var must be re-evaluated. */
2357 if (var
->root
->floating
|| var
->root
->valid_block
== NULL
)
2359 struct varobj
*tmp_var
;
2361 /* Try to create a varobj with same expression. If we succeed
2362 replace the old varobj, otherwise invalidate it. */
2363 tmp_var
= varobj_create (NULL
, var
->name
.c_str (), (CORE_ADDR
) 0,
2365 if (tmp_var
!= NULL
)
2367 tmp_var
->obj_name
= var
->obj_name
;
2368 varobj_delete (var
, 0);
2369 install_variable (tmp_var
);
2372 var
->root
->is_valid
= false;
2374 else /* locals must be invalidated. */
2375 var
->root
->is_valid
= false;
2378 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2379 are defined on globals.
2380 Invalidated varobjs will be always printed in_scope="invalid". */
2383 varobj_invalidate (void)
2385 all_root_varobjs (varobj_invalidate_iter
);
2388 /* A hash function for a varobj. */
2391 hash_varobj (const void *a
)
2393 const varobj
*obj
= (const varobj
*) a
;
2394 return htab_hash_string (obj
->obj_name
.c_str ());
2397 /* A hash table equality function for varobjs. */
2400 eq_varobj_and_string (const void *a
, const void *b
)
2402 const varobj
*obj
= (const varobj
*) a
;
2403 const char *name
= (const char *) b
;
2405 return obj
->obj_name
== name
;
2408 void _initialize_varobj ();
2410 _initialize_varobj ()
2412 varobj_table
= htab_create_alloc (5, hash_varobj
, eq_varobj_and_string
,
2413 nullptr, xcalloc
, xfree
);
2415 add_setshow_zuinteger_cmd ("varobj", class_maintenance
,
2417 _("Set varobj debugging."),
2418 _("Show varobj debugging."),
2419 _("When non-zero, varobj debugging is enabled."),
2420 NULL
, show_varobjdebug
,
2421 &setdebuglist
, &showdebuglist
);