1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2022 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 "gdbsupport/gdb_regex.h"
29 #include "gdbthread.h"
31 #include "varobj-iter.h"
32 #include "parser-defs.h"
35 #include "observable.h"
38 #include "python/python.h"
39 #include "python/python-internal.h"
46 unsigned int varobjdebug
= 0;
48 show_varobjdebug (struct ui_file
*file
, int from_tty
,
49 struct cmd_list_element
*c
, const char *value
)
51 gdb_printf (file
, _("Varobj debugging is %s.\n"), value
);
54 /* String representations of gdb's format codes. */
55 const char *varobj_format_string
[] =
56 { "natural", "binary", "decimal", "hexadecimal", "octal", "zero-hexadecimal" };
58 /* True if we want to allow Python-based pretty-printing. */
59 static bool pretty_printing
= false;
62 varobj_enable_pretty_printing (void)
64 pretty_printing
= true;
69 /* Every root variable has one of these structures saved in its
73 /* The expression for this parent. */
76 /* Cached arch from exp, for use in case exp gets invalidated. */
77 struct gdbarch
*gdbarch
= nullptr;
79 /* Cached language from exp, for use in case exp gets invalidated. */
80 const struct language_defn
*language_defn
= nullptr;
82 /* Block for which this expression is valid. */
83 const struct block
*valid_block
= NULL
;
85 /* The frame for this expression. This field is set iff valid_block is
87 struct frame_id frame
= null_frame_id
;
89 /* The global thread ID that this varobj_root belongs to. This field
90 is only valid if valid_block is not NULL.
91 When not 0, indicates which thread 'frame' belongs to.
92 When 0, indicates that the thread list was empty when the varobj_root
96 /* If true, the -var-update always recomputes the value in the
97 current thread and frame. Otherwise, variable object is
98 always updated in the specific scope/thread/frame. */
99 bool floating
= false;
101 /* Flag that indicates validity: set to false when this varobj_root refers
102 to symbols that do not exist anymore. */
103 bool is_valid
= true;
105 /* Language-related operations for this variable and its
107 const struct lang_varobj_ops
*lang_ops
= NULL
;
109 /* The varobj for this root node. */
110 struct varobj
*rootvar
= NULL
;
113 /* Dynamic part of varobj. */
115 struct varobj_dynamic
117 /* Whether the children of this varobj were requested. This field is
118 used to decide if dynamic varobj should recompute their children.
119 In the event that the frontend never asked for the children, we
121 bool children_requested
= false;
123 /* The pretty-printer constructor. If NULL, then the default
124 pretty-printer will be looked up. If None, then no
125 pretty-printer will be installed. */
126 PyObject
*constructor
= NULL
;
128 /* The pretty-printer that has been constructed. If NULL, then a
129 new printer object is needed, and one will be constructed. */
130 PyObject
*pretty_printer
= NULL
;
132 /* The iterator returned by the printer's 'children' method, or NULL
134 std::unique_ptr
<varobj_iter
> child_iter
;
136 /* We request one extra item from the iterator, so that we can
137 report to the caller whether there are more items than we have
138 already reported. However, we don't want to install this value
139 when we read it, because that will mess up future updates. So,
140 we stash it here instead. */
141 std::unique_ptr
<varobj_item
> saved_item
;
144 /* Private function prototypes */
146 /* Helper functions for the above subcommands. */
148 static int delete_variable (struct varobj
*, bool);
150 static void delete_variable_1 (int *, struct varobj
*, bool, bool);
152 static void install_variable (struct varobj
*);
154 static void uninstall_variable (struct varobj
*);
156 static struct varobj
*create_child (struct varobj
*, int, std::string
&);
158 static struct varobj
*
159 create_child_with_value (struct varobj
*parent
, int index
,
160 struct varobj_item
*item
);
162 /* Utility routines */
164 static enum varobj_display_formats
variable_default_display (struct varobj
*);
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 struct frame_info
*
226 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
228 struct frame_info
*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 std::unique_ptr
<varobj
> var (new varobj (new varobj_root
));
263 if (expression
!= NULL
)
265 struct frame_info
*fi
;
266 struct frame_id old_id
= null_frame_id
;
267 const struct block
*block
;
269 struct value
*value
= NULL
;
272 /* Parse and evaluate the expression, filling in as much of the
273 variable's data as possible. */
275 if (has_stack_frames ())
277 /* Allow creator to specify context of variable. */
278 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
279 fi
= get_selected_frame (NULL
);
281 /* FIXME: cagney/2002-11-23: This code should be doing a
282 lookup using the frame ID and not just the frame's
283 ``address''. This, of course, means an interface
284 change. However, with out that interface change ISAs,
285 such as the ia64 with its two stacks, won't work.
286 Similar goes for the case where there is a frameless
288 fi
= find_frame_addr_in_frame_chain (frame
);
293 if (type
== USE_SELECTED_FRAME
)
294 var
->root
->floating
= true;
300 block
= get_frame_block (fi
, 0);
301 pc
= get_frame_pc (fi
);
306 innermost_block_tracker
tracker (INNERMOST_BLOCK_FOR_SYMBOLS
307 | INNERMOST_BLOCK_FOR_REGISTERS
);
308 /* Wrap the call to parse expression, so we can
309 return a sensible error. */
312 var
->root
->exp
= parse_exp_1 (&p
, pc
, block
, 0, &tracker
);
314 /* Cache gdbarch and language_defn as they might be used even
315 after var is invalidated and var->root->exp cleared. */
316 var
->root
->gdbarch
= var
->root
->exp
->gdbarch
;
317 var
->root
->language_defn
= var
->root
->exp
->language_defn
;
320 catch (const gdb_exception_error
&except
)
325 /* Don't allow variables to be created for types. */
326 enum exp_opcode opcode
= var
->root
->exp
->first_opcode ();
327 if (opcode
== OP_TYPE
328 || opcode
== OP_TYPEOF
329 || opcode
== OP_DECLTYPE
)
331 gdb_printf (gdb_stderr
, "Attempt to use a type name"
332 " as an expression.\n");
336 var
->format
= variable_default_display (var
.get ());
337 var
->root
->valid_block
=
338 var
->root
->floating
? NULL
: tracker
.block ();
339 var
->name
= expression
;
340 /* For a root var, the name and the expr are the same. */
341 var
->path_expr
= expression
;
343 /* When the frame is different from the current frame,
344 we must select the appropriate frame before parsing
345 the expression, otherwise the value will not be current.
346 Since select_frame is so benign, just call it for all cases. */
347 if (var
->root
->valid_block
)
349 /* User could specify explicit FRAME-ADDR which was not found but
350 EXPRESSION is frame specific and we would not be able to evaluate
351 it correctly next time. With VALID_BLOCK set we must also set
352 FRAME and THREAD_ID. */
354 error (_("Failed to find the specified frame"));
356 var
->root
->frame
= get_frame_id (fi
);
357 var
->root
->thread_id
= inferior_thread ()->global_num
;
358 old_id
= get_frame_id (get_selected_frame (NULL
));
362 /* We definitely need to catch errors here.
363 If evaluate_expression succeeds we got the value we wanted.
364 But if it fails, we still go on with a call to evaluate_type(). */
367 value
= evaluate_expression (var
->root
->exp
.get ());
369 catch (const gdb_exception_error
&except
)
371 /* Error getting the value. Try to at least get the
373 struct value
*type_only_value
= evaluate_type (var
->root
->exp
.get ());
375 var
->type
= value_type (type_only_value
);
380 int real_type_found
= 0;
382 var
->type
= value_actual_type (value
, 0, &real_type_found
);
384 value
= value_cast (var
->type
, value
);
387 /* Set language info */
388 var
->root
->lang_ops
= var
->root
->exp
->language_defn
->varobj_ops ();
390 install_new_value (var
.get (), value
, 1 /* Initial assignment */);
392 /* Set ourselves as our root. */
393 var
->root
->rootvar
= var
.get ();
395 /* Reset the selected frame. */
396 if (frame_id_p (old_id
))
397 select_frame (frame_find_by_id (old_id
));
400 /* If the variable object name is null, that means this
401 is a temporary variable, so don't install it. */
403 if ((var
!= NULL
) && (objname
!= NULL
))
405 var
->obj_name
= objname
;
406 install_variable (var
.get ());
409 return var
.release ();
412 /* Generates an unique name that can be used for a varobj. */
415 varobj_gen_name (void)
419 /* Generate a name for this object. */
421 return string_printf ("var%d", id
);
424 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
425 error if OBJNAME cannot be found. */
428 varobj_get_handle (const char *objname
)
430 varobj
*var
= (varobj
*) htab_find_with_hash (varobj_table
, objname
,
431 htab_hash_string (objname
));
434 error (_("Variable object not found"));
439 /* Given the handle, return the name of the object. */
442 varobj_get_objname (const struct varobj
*var
)
444 return var
->obj_name
.c_str ();
447 /* Given the handle, return the expression represented by the
451 varobj_get_expression (const struct varobj
*var
)
453 return name_of_variable (var
);
459 varobj_delete (struct varobj
*var
, bool only_children
)
461 return delete_variable (var
, only_children
);
466 /* Convenience function for varobj_set_visualizer. Instantiate a
467 pretty-printer for a given value. */
469 instantiate_pretty_printer (PyObject
*constructor
, struct value
*value
)
471 gdbpy_ref
<> val_obj (value_to_value_object (value
));
472 if (val_obj
== nullptr)
475 return PyObject_CallFunctionObjArgs (constructor
, val_obj
.get (), NULL
);
480 /* Set/Get variable object display format. */
482 enum varobj_display_formats
483 varobj_set_display_format (struct varobj
*var
,
484 enum varobj_display_formats format
)
491 case FORMAT_HEXADECIMAL
:
493 case FORMAT_ZHEXADECIMAL
:
494 var
->format
= format
;
498 var
->format
= variable_default_display (var
);
501 if (varobj_value_is_changeable_p (var
)
502 && var
->value
!= nullptr && !value_lazy (var
->value
.get ()))
504 var
->print_value
= varobj_value_get_print_value (var
->value
.get (),
511 enum varobj_display_formats
512 varobj_get_display_format (const struct varobj
*var
)
517 gdb::unique_xmalloc_ptr
<char>
518 varobj_get_display_hint (const struct varobj
*var
)
520 gdb::unique_xmalloc_ptr
<char> result
;
523 if (!gdb_python_initialized
)
526 gdbpy_enter_varobj
enter_py (var
);
528 if (var
->dynamic
->pretty_printer
!= NULL
)
529 result
= gdbpy_get_display_hint (var
->dynamic
->pretty_printer
);
535 /* Return true if the varobj has items after TO, false otherwise. */
538 varobj_has_more (const struct varobj
*var
, int to
)
540 if (var
->children
.size () > to
)
543 return ((to
== -1 || var
->children
.size () == to
)
544 && (var
->dynamic
->saved_item
!= NULL
));
547 /* If the variable object is bound to a specific thread, that
548 is its evaluation can always be done in context of a frame
549 inside that thread, returns GDB id of the thread -- which
550 is always positive. Otherwise, returns -1. */
552 varobj_get_thread_id (const struct varobj
*var
)
554 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
555 return var
->root
->thread_id
;
561 varobj_set_frozen (struct varobj
*var
, bool frozen
)
563 /* When a variable is unfrozen, we don't fetch its value.
564 The 'not_fetched' flag remains set, so next -var-update
567 We don't fetch the value, because for structures the client
568 should do -var-update anyway. It would be bad to have different
569 client-size logic for structure and other types. */
570 var
->frozen
= frozen
;
574 varobj_get_frozen (const struct varobj
*var
)
579 /* A helper function that updates the contents of FROM and TO based on the
580 size of the vector CHILDREN. If the contents of either FROM or TO are
581 negative the entire range is used. */
584 varobj_restrict_range (const std::vector
<varobj
*> &children
,
587 int len
= children
.size ();
589 if (*from
< 0 || *to
< 0)
605 /* A helper for update_dynamic_varobj_children that installs a new
606 child when needed. */
609 install_dynamic_child (struct varobj
*var
,
610 std::vector
<varobj
*> *changed
,
611 std::vector
<varobj
*> *type_changed
,
612 std::vector
<varobj
*> *newobj
,
613 std::vector
<varobj
*> *unchanged
,
616 struct varobj_item
*item
)
618 if (var
->children
.size () < index
+ 1)
620 /* There's no child yet. */
621 struct varobj
*child
= varobj_add_child (var
, item
);
625 newobj
->push_back (child
);
631 varobj
*existing
= var
->children
[index
];
632 bool type_updated
= update_type_if_necessary (existing
,
637 if (type_changed
!= NULL
)
638 type_changed
->push_back (existing
);
640 if (install_new_value (existing
, item
->value
.get (), 0))
642 if (!type_updated
&& changed
!= NULL
)
643 changed
->push_back (existing
);
645 else if (!type_updated
&& unchanged
!= NULL
)
646 unchanged
->push_back (existing
);
653 dynamic_varobj_has_child_method (const struct varobj
*var
)
655 PyObject
*printer
= var
->dynamic
->pretty_printer
;
657 if (!gdb_python_initialized
)
660 gdbpy_enter_varobj
enter_py (var
);
661 return PyObject_HasAttr (printer
, gdbpy_children_cst
);
665 /* A factory for creating dynamic varobj's iterators. Returns an
666 iterator object suitable for iterating over VAR's children. */
668 static std::unique_ptr
<varobj_iter
>
669 varobj_get_iterator (struct varobj
*var
)
672 if (var
->dynamic
->pretty_printer
)
674 value_print_options opts
;
675 varobj_formatted_print_options (&opts
, var
->format
);
676 return py_varobj_get_iterator (var
, var
->dynamic
->pretty_printer
, &opts
);
680 gdb_assert_not_reached ("requested an iterator from a non-dynamic varobj");
684 update_dynamic_varobj_children (struct varobj
*var
,
685 std::vector
<varobj
*> *changed
,
686 std::vector
<varobj
*> *type_changed
,
687 std::vector
<varobj
*> *newobj
,
688 std::vector
<varobj
*> *unchanged
,
690 bool update_children
,
698 if (update_children
|| var
->dynamic
->child_iter
== NULL
)
700 var
->dynamic
->child_iter
= varobj_get_iterator (var
);
701 var
->dynamic
->saved_item
.reset (nullptr);
705 if (var
->dynamic
->child_iter
== NULL
)
709 i
= var
->children
.size ();
711 /* We ask for one extra child, so that MI can report whether there
712 are more children. */
713 for (; to
< 0 || i
< to
+ 1; ++i
)
715 std::unique_ptr
<varobj_item
> item
;
717 /* See if there was a leftover from last time. */
718 if (var
->dynamic
->saved_item
!= NULL
)
719 item
= std::move (var
->dynamic
->saved_item
);
721 item
= var
->dynamic
->child_iter
->next ();
725 /* Iteration is done. Remove iterator from VAR. */
726 var
->dynamic
->child_iter
.reset (nullptr);
729 /* We don't want to push the extra child on any report list. */
730 if (to
< 0 || i
< to
)
732 bool can_mention
= from
< 0 || i
>= from
;
734 install_dynamic_child (var
, can_mention
? changed
: NULL
,
735 can_mention
? type_changed
: NULL
,
736 can_mention
? newobj
: NULL
,
737 can_mention
? unchanged
: NULL
,
738 can_mention
? cchanged
: NULL
, i
,
743 var
->dynamic
->saved_item
= std::move (item
);
745 /* We want to truncate the child list just before this
751 if (i
< var
->children
.size ())
754 for (int j
= i
; j
< var
->children
.size (); ++j
)
755 varobj_delete (var
->children
[j
], 0);
757 var
->children
.resize (i
);
760 /* If there are fewer children than requested, note that the list of
762 if (to
>= 0 && var
->children
.size () < to
)
765 var
->num_children
= var
->children
.size ();
771 varobj_get_num_children (struct varobj
*var
)
773 if (var
->num_children
== -1)
775 if (varobj_is_dynamic_p (var
))
779 /* If we have a dynamic varobj, don't report -1 children.
780 So, try to fetch some children first. */
781 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
, &dummy
,
785 var
->num_children
= number_of_children (var
);
788 return var
->num_children
>= 0 ? var
->num_children
: 0;
791 /* Creates a list of the immediate children of a variable object;
792 the return code is the number of such children or -1 on error. */
794 const std::vector
<varobj
*> &
795 varobj_list_children (struct varobj
*var
, int *from
, int *to
)
797 var
->dynamic
->children_requested
= true;
799 if (varobj_is_dynamic_p (var
))
801 bool children_changed
;
803 /* This, in theory, can result in the number of children changing without
804 frontend noticing. But well, calling -var-list-children on the same
805 varobj twice is not something a sane frontend would do. */
806 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
,
807 &children_changed
, false, 0, *to
);
808 varobj_restrict_range (var
->children
, from
, to
);
809 return var
->children
;
812 if (var
->num_children
== -1)
813 var
->num_children
= number_of_children (var
);
815 /* If that failed, give up. */
816 if (var
->num_children
== -1)
817 return var
->children
;
819 /* If we're called when the list of children is not yet initialized,
820 allocate enough elements in it. */
821 while (var
->children
.size () < var
->num_children
)
822 var
->children
.push_back (NULL
);
824 for (int i
= 0; i
< var
->num_children
; i
++)
826 if (var
->children
[i
] == NULL
)
828 /* Either it's the first call to varobj_list_children for
829 this variable object, and the child was never created,
830 or it was explicitly deleted by the client. */
831 std::string name
= name_of_child (var
, i
);
832 var
->children
[i
] = create_child (var
, i
, name
);
836 varobj_restrict_range (var
->children
, from
, to
);
837 return var
->children
;
840 static struct varobj
*
841 varobj_add_child (struct varobj
*var
, struct varobj_item
*item
)
843 varobj
*v
= create_child_with_value (var
, var
->children
.size (), item
);
845 var
->children
.push_back (v
);
850 /* Obtain the type of an object Variable as a string similar to the one gdb
851 prints on the console. The caller is responsible for freeing the string.
855 varobj_get_type (struct varobj
*var
)
857 /* For the "fake" variables, do not return a type. (Its type is
859 Do not return a type for invalid variables as well. */
860 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
861 return std::string ();
863 return type_to_string (var
->type
);
866 /* Obtain the type of an object variable. */
869 varobj_get_gdb_type (const struct varobj
*var
)
874 /* Is VAR a path expression parent, i.e., can it be used to construct
875 a valid path expression? */
878 is_path_expr_parent (const struct varobj
*var
)
880 gdb_assert (var
->root
->lang_ops
->is_path_expr_parent
!= NULL
);
881 return var
->root
->lang_ops
->is_path_expr_parent (var
);
884 /* Is VAR a path expression parent, i.e., can it be used to construct
885 a valid path expression? By default we assume any VAR can be a path
889 varobj_default_is_path_expr_parent (const struct varobj
*var
)
894 /* Return the path expression parent for VAR. */
896 const struct varobj
*
897 varobj_get_path_expr_parent (const struct varobj
*var
)
899 const struct varobj
*parent
= var
;
901 while (!is_root_p (parent
) && !is_path_expr_parent (parent
))
902 parent
= parent
->parent
;
904 /* Computation of full rooted expression for children of dynamic
905 varobjs is not supported. */
906 if (varobj_is_dynamic_p (parent
))
907 error (_("Invalid variable object (child of a dynamic varobj)"));
912 /* Return a pointer to the full rooted expression of varobj VAR.
913 If it has not been computed yet, compute it. */
916 varobj_get_path_expr (const struct varobj
*var
)
918 if (var
->path_expr
.empty ())
920 /* For root varobjs, we initialize path_expr
921 when creating varobj, so here it should be
923 struct varobj
*mutable_var
= (struct varobj
*) var
;
924 gdb_assert (!is_root_p (var
));
926 mutable_var
->path_expr
= (*var
->root
->lang_ops
->path_expr_of_child
) (var
);
929 return var
->path_expr
.c_str ();
932 const struct language_defn
*
933 varobj_get_language (const struct varobj
*var
)
935 return var
->root
->exp
->language_defn
;
939 varobj_get_attributes (const struct varobj
*var
)
943 if (varobj_editable_p (var
))
944 /* FIXME: define masks for attributes. */
945 attributes
|= 0x00000001; /* Editable */
950 /* Return true if VAR is a dynamic varobj. */
953 varobj_is_dynamic_p (const struct varobj
*var
)
955 return var
->dynamic
->pretty_printer
!= NULL
;
959 varobj_get_formatted_value (struct varobj
*var
,
960 enum varobj_display_formats format
)
962 return my_value_of_variable (var
, format
);
966 varobj_get_value (struct varobj
*var
)
968 return my_value_of_variable (var
, var
->format
);
971 /* Set the value of an object variable (if it is editable) to the
972 value of the given expression. */
973 /* Note: Invokes functions that can call error(). */
976 varobj_set_value (struct varobj
*var
, const char *expression
)
978 struct value
*val
= NULL
; /* Initialize to keep gcc happy. */
979 /* The argument "expression" contains the variable's new value.
980 We need to first construct a legal expression for this -- ugh! */
981 /* Does this cover all the bases? */
982 struct value
*value
= NULL
; /* Initialize to keep gcc happy. */
983 int saved_input_radix
= input_radix
;
984 const char *s
= expression
;
986 gdb_assert (varobj_editable_p (var
));
988 input_radix
= 10; /* ALWAYS reset to decimal temporarily. */
989 expression_up exp
= parse_exp_1 (&s
, 0, 0, 0);
992 value
= evaluate_expression (exp
.get ());
995 catch (const gdb_exception_error
&except
)
997 /* We cannot proceed without a valid expression. */
1001 /* All types that are editable must also be changeable. */
1002 gdb_assert (varobj_value_is_changeable_p (var
));
1004 /* The value of a changeable variable object must not be lazy. */
1005 gdb_assert (!value_lazy (var
->value
.get ()));
1007 /* Need to coerce the input. We want to check if the
1008 value of the variable object will be different
1009 after assignment, and the first thing value_assign
1010 does is coerce the input.
1011 For example, if we are assigning an array to a pointer variable we
1012 should compare the pointer with the array's address, not with the
1014 value
= coerce_array (value
);
1016 /* The new value may be lazy. value_assign, or
1017 rather value_contents, will take care of this. */
1020 val
= value_assign (var
->value
.get (), value
);
1023 catch (const gdb_exception_error
&except
)
1028 /* If the value has changed, record it, so that next -var-update can
1029 report this change. If a variable had a value of '1', we've set it
1030 to '333' and then set again to '1', when -var-update will report this
1031 variable as changed -- because the first assignment has set the
1032 'updated' flag. There's no need to optimize that, because return value
1033 of -var-update should be considered an approximation. */
1034 var
->updated
= install_new_value (var
, val
, false /* Compare values. */);
1035 input_radix
= saved_input_radix
;
1041 /* A helper function to install a constructor function and visualizer
1042 in a varobj_dynamic. */
1045 install_visualizer (struct varobj_dynamic
*var
, PyObject
*constructor
,
1046 PyObject
*visualizer
)
1048 Py_XDECREF (var
->constructor
);
1049 var
->constructor
= constructor
;
1051 Py_XDECREF (var
->pretty_printer
);
1052 var
->pretty_printer
= visualizer
;
1054 var
->child_iter
.reset (nullptr);
1057 /* Install the default visualizer for VAR. */
1060 install_default_visualizer (struct varobj
*var
)
1062 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1063 if (CPLUS_FAKE_CHILD (var
))
1066 if (pretty_printing
)
1068 gdbpy_ref
<> pretty_printer
;
1070 if (var
->value
!= nullptr)
1072 pretty_printer
= gdbpy_get_varobj_pretty_printer (var
->value
.get ());
1073 if (pretty_printer
== nullptr)
1075 gdbpy_print_stack ();
1076 error (_("Cannot instantiate printer for default visualizer"));
1080 if (pretty_printer
== Py_None
)
1081 pretty_printer
.reset (nullptr);
1083 install_visualizer (var
->dynamic
, NULL
, pretty_printer
.release ());
1087 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1088 make a new object. */
1091 construct_visualizer (struct varobj
*var
, PyObject
*constructor
)
1093 PyObject
*pretty_printer
;
1095 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1096 if (CPLUS_FAKE_CHILD (var
))
1099 Py_INCREF (constructor
);
1100 if (constructor
== Py_None
)
1101 pretty_printer
= NULL
;
1104 pretty_printer
= instantiate_pretty_printer (constructor
,
1106 if (! pretty_printer
)
1108 gdbpy_print_stack ();
1109 Py_DECREF (constructor
);
1110 constructor
= Py_None
;
1111 Py_INCREF (constructor
);
1114 if (pretty_printer
== Py_None
)
1116 Py_DECREF (pretty_printer
);
1117 pretty_printer
= NULL
;
1121 install_visualizer (var
->dynamic
, constructor
, pretty_printer
);
1124 #endif /* HAVE_PYTHON */
1126 /* A helper function for install_new_value. This creates and installs
1127 a visualizer for VAR, if appropriate. */
1130 install_new_value_visualizer (struct varobj
*var
)
1133 /* If the constructor is None, then we want the raw value. If VAR
1134 does not have a value, just skip this. */
1135 if (!gdb_python_initialized
)
1138 if (var
->dynamic
->constructor
!= Py_None
&& var
->value
!= NULL
)
1140 gdbpy_enter_varobj
enter_py (var
);
1142 if (var
->dynamic
->constructor
== NULL
)
1143 install_default_visualizer (var
);
1145 construct_visualizer (var
, var
->dynamic
->constructor
);
1152 /* When using RTTI to determine variable type it may be changed in runtime when
1153 the variable value is changed. This function checks whether type of varobj
1154 VAR will change when a new value NEW_VALUE is assigned and if it is so
1155 updates the type of VAR. */
1158 update_type_if_necessary (struct varobj
*var
, struct value
*new_value
)
1162 struct value_print_options opts
;
1164 get_user_print_options (&opts
);
1165 if (opts
.objectprint
)
1167 struct type
*new_type
= value_actual_type (new_value
, 0, 0);
1168 std::string new_type_str
= type_to_string (new_type
);
1169 std::string curr_type_str
= varobj_get_type (var
);
1171 /* Did the type name change? */
1172 if (curr_type_str
!= new_type_str
)
1174 var
->type
= new_type
;
1176 /* This information may be not valid for a new type. */
1177 varobj_delete (var
, 1);
1178 var
->children
.clear ();
1179 var
->num_children
= -1;
1188 /* Assign a new value to a variable object. If INITIAL is true,
1189 this is the first assignment after the variable object was just
1190 created, or changed type. In that case, just assign the value
1192 Otherwise, assign the new value, and return true if the value is
1193 different from the current one, false otherwise. The comparison is
1194 done on textual representation of value. Therefore, some types
1195 need not be compared. E.g. for structures the reported value is
1196 always "{...}", so no comparison is necessary here. If the old
1197 value was NULL and new one is not, or vice versa, we always return true.
1199 The VALUE parameter should not be released -- the function will
1200 take care of releasing it when needed. */
1202 install_new_value (struct varobj
*var
, struct value
*value
, bool initial
)
1206 bool changed
= false;
1207 bool intentionally_not_fetched
= false;
1209 /* We need to know the varobj's type to decide if the value should
1210 be fetched or not. C++ fake children (public/protected/private)
1211 don't have a type. */
1212 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
1213 changeable
= varobj_value_is_changeable_p (var
);
1215 /* If the type has custom visualizer, we consider it to be always
1216 changeable. FIXME: need to make sure this behaviour will not
1217 mess up read-sensitive values. */
1218 if (var
->dynamic
->pretty_printer
!= NULL
)
1221 need_to_fetch
= changeable
;
1223 /* We are not interested in the address of references, and given
1224 that in C++ a reference is not rebindable, it cannot
1225 meaningfully change. So, get hold of the real value. */
1227 value
= coerce_ref (value
);
1229 if (var
->type
&& var
->type
->code () == TYPE_CODE_UNION
)
1230 /* For unions, we need to fetch the value implicitly because
1231 of implementation of union member fetch. When gdb
1232 creates a value for a field and the value of the enclosing
1233 structure is not lazy, it immediately copies the necessary
1234 bytes from the enclosing values. If the enclosing value is
1235 lazy, the call to value_fetch_lazy on the field will read
1236 the data from memory. For unions, that means we'll read the
1237 same memory more than once, which is not desirable. So
1239 need_to_fetch
= true;
1241 /* The new value might be lazy. If the type is changeable,
1242 that is we'll be comparing values of this type, fetch the
1243 value now. Otherwise, on the next update the old value
1244 will be lazy, which means we've lost that old value. */
1245 if (need_to_fetch
&& value
&& value_lazy (value
))
1247 const struct varobj
*parent
= var
->parent
;
1248 bool frozen
= var
->frozen
;
1250 for (; !frozen
&& parent
; parent
= parent
->parent
)
1251 frozen
|= parent
->frozen
;
1253 if (frozen
&& initial
)
1255 /* For variables that are frozen, or are children of frozen
1256 variables, we don't do fetch on initial assignment.
1257 For non-initial assignment we do the fetch, since it means we're
1258 explicitly asked to compare the new value with the old one. */
1259 intentionally_not_fetched
= true;
1266 value_fetch_lazy (value
);
1269 catch (const gdb_exception_error
&except
)
1271 /* Set the value to NULL, so that for the next -var-update,
1272 we don't try to compare the new value with this value,
1273 that we couldn't even read. */
1279 /* Get a reference now, before possibly passing it to any Python
1280 code that might release it. */
1281 value_ref_ptr value_holder
;
1283 value_holder
= value_ref_ptr::new_reference (value
);
1285 /* Below, we'll be comparing string rendering of old and new
1286 values. Don't get string rendering if the value is
1287 lazy -- if it is, the code above has decided that the value
1288 should not be fetched. */
1289 std::string print_value
;
1290 if (value
!= NULL
&& !value_lazy (value
)
1291 && var
->dynamic
->pretty_printer
== NULL
)
1292 print_value
= varobj_value_get_print_value (value
, var
->format
, var
);
1294 /* If the type is changeable, compare the old and the new values.
1295 If this is the initial assignment, we don't have any old value
1297 if (!initial
&& changeable
)
1299 /* If the value of the varobj was changed by -var-set-value,
1300 then the value in the varobj and in the target is the same.
1301 However, that value is different from the value that the
1302 varobj had after the previous -var-update. So need to the
1303 varobj as changed. */
1306 else if (var
->dynamic
->pretty_printer
== NULL
)
1308 /* Try to compare the values. That requires that both
1309 values are non-lazy. */
1310 if (var
->not_fetched
&& value_lazy (var
->value
.get ()))
1312 /* This is a frozen varobj and the value was never read.
1313 Presumably, UI shows some "never read" indicator.
1314 Now that we've fetched the real value, we need to report
1315 this varobj as changed so that UI can show the real
1319 else if (var
->value
== NULL
&& value
== NULL
)
1322 else if (var
->value
== NULL
|| value
== NULL
)
1328 gdb_assert (!value_lazy (var
->value
.get ()));
1329 gdb_assert (!value_lazy (value
));
1331 gdb_assert (!var
->print_value
.empty () && !print_value
.empty ());
1332 if (var
->print_value
!= print_value
)
1338 if (!initial
&& !changeable
)
1340 /* For values that are not changeable, we don't compare the values.
1341 However, we want to notice if a value was not NULL and now is NULL,
1342 or vise versa, so that we report when top-level varobjs come in scope
1343 and leave the scope. */
1344 changed
= (var
->value
!= NULL
) != (value
!= NULL
);
1347 /* We must always keep the new value, since children depend on it. */
1348 var
->value
= value_holder
;
1349 if (value
&& value_lazy (value
) && intentionally_not_fetched
)
1350 var
->not_fetched
= true;
1352 var
->not_fetched
= false;
1353 var
->updated
= false;
1355 install_new_value_visualizer (var
);
1357 /* If we installed a pretty-printer, re-compare the printed version
1358 to see if the variable changed. */
1359 if (var
->dynamic
->pretty_printer
!= NULL
)
1361 print_value
= varobj_value_get_print_value (var
->value
.get (),
1363 if (var
->print_value
!= print_value
)
1366 var
->print_value
= print_value
;
1368 gdb_assert (var
->value
== nullptr || value_type (var
->value
.get ()));
1373 /* Return the requested range for a varobj. VAR is the varobj. FROM
1374 and TO are out parameters; *FROM and *TO will be set to the
1375 selected sub-range of VAR. If no range was selected using
1376 -var-set-update-range, then both will be -1. */
1378 varobj_get_child_range (const struct varobj
*var
, int *from
, int *to
)
1384 /* Set the selected sub-range of children of VAR to start at index
1385 FROM and end at index TO. If either FROM or TO is less than zero,
1386 this is interpreted as a request for all children. */
1388 varobj_set_child_range (struct varobj
*var
, int from
, int to
)
1395 varobj_set_visualizer (struct varobj
*var
, const char *visualizer
)
1400 if (!gdb_python_initialized
)
1403 gdbpy_enter_varobj
enter_py (var
);
1405 mainmod
= PyImport_AddModule ("__main__");
1407 = gdbpy_ref
<>::new_reference (PyModule_GetDict (mainmod
));
1408 gdbpy_ref
<> constructor (PyRun_String (visualizer
, Py_eval_input
,
1409 globals
.get (), globals
.get ()));
1411 if (constructor
== NULL
)
1413 gdbpy_print_stack ();
1414 error (_("Could not evaluate visualizer expression: %s"), visualizer
);
1417 construct_visualizer (var
, constructor
.get ());
1419 /* If there are any children now, wipe them. */
1420 varobj_delete (var
, 1 /* children only */);
1421 var
->num_children
= -1;
1423 error (_("Python support required"));
1427 /* If NEW_VALUE is the new value of the given varobj (var), return
1428 true if var has mutated. In other words, if the type of
1429 the new value is different from the type of the varobj's old
1432 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1435 varobj_value_has_mutated (const struct varobj
*var
, struct value
*new_value
,
1436 struct type
*new_type
)
1438 /* If we haven't previously computed the number of children in var,
1439 it does not matter from the front-end's perspective whether
1440 the type has mutated or not. For all intents and purposes,
1441 it has not mutated. */
1442 if (var
->num_children
< 0)
1445 if (var
->root
->lang_ops
->value_has_mutated
!= NULL
)
1447 /* The varobj module, when installing new values, explicitly strips
1448 references, saying that we're not interested in those addresses.
1449 But detection of mutation happens before installing the new
1450 value, so our value may be a reference that we need to strip
1451 in order to remain consistent. */
1452 if (new_value
!= NULL
)
1453 new_value
= coerce_ref (new_value
);
1454 return var
->root
->lang_ops
->value_has_mutated (var
, new_value
, new_type
);
1460 /* Update the values for a variable and its children. This is a
1461 two-pronged attack. First, re-parse the value for the root's
1462 expression to see if it's changed. Then go all the way
1463 through its children, reconstructing them and noting if they've
1466 The IS_EXPLICIT parameter specifies if this call is result
1467 of MI request to update this specific variable, or
1468 result of implicit -var-update *. For implicit request, we don't
1469 update frozen variables.
1471 NOTE: This function may delete the caller's varobj. If it
1472 returns TYPE_CHANGED, then it has done this and VARP will be modified
1473 to point to the new varobj. */
1475 std::vector
<varobj_update_result
>
1476 varobj_update (struct varobj
**varp
, bool is_explicit
)
1478 bool type_changed
= false;
1479 struct value
*newobj
;
1480 std::vector
<varobj_update_result
> stack
;
1481 std::vector
<varobj_update_result
> result
;
1483 /* Frozen means frozen -- we don't check for any change in
1484 this varobj, including its going out of scope, or
1485 changing type. One use case for frozen varobjs is
1486 retaining previously evaluated expressions, and we don't
1487 want them to be reevaluated at all. */
1488 if (!is_explicit
&& (*varp
)->frozen
)
1491 if (!(*varp
)->root
->is_valid
)
1493 result
.emplace_back (*varp
, VAROBJ_INVALID
);
1497 if ((*varp
)->root
->rootvar
== *varp
)
1499 varobj_update_result
r (*varp
);
1501 /* Update the root variable. value_of_root can return NULL
1502 if the variable is no longer around, i.e. we stepped out of
1503 the frame in which a local existed. We are letting the
1504 value_of_root variable dispose of the varobj if the type
1506 newobj
= value_of_root (varp
, &type_changed
);
1507 if (update_type_if_necessary (*varp
, newobj
))
1508 type_changed
= true;
1510 r
.type_changed
= type_changed
;
1511 if (install_new_value ((*varp
), newobj
, type_changed
))
1515 r
.status
= VAROBJ_NOT_IN_SCOPE
;
1516 r
.value_installed
= true;
1518 if (r
.status
== VAROBJ_NOT_IN_SCOPE
)
1520 if (r
.type_changed
|| r
.changed
)
1521 result
.push_back (std::move (r
));
1526 stack
.push_back (std::move (r
));
1529 stack
.emplace_back (*varp
);
1531 /* Walk through the children, reconstructing them all. */
1532 while (!stack
.empty ())
1534 varobj_update_result r
= std::move (stack
.back ());
1536 struct varobj
*v
= r
.varobj
;
1538 /* Update this variable, unless it's a root, which is already
1540 if (!r
.value_installed
)
1542 struct type
*new_type
;
1544 newobj
= value_of_child (v
->parent
, v
->index
);
1545 if (update_type_if_necessary (v
, newobj
))
1546 r
.type_changed
= true;
1548 new_type
= value_type (newobj
);
1550 new_type
= v
->root
->lang_ops
->type_of_child (v
->parent
, v
->index
);
1552 if (varobj_value_has_mutated (v
, newobj
, new_type
))
1554 /* The children are no longer valid; delete them now.
1555 Report the fact that its type changed as well. */
1556 varobj_delete (v
, 1 /* only_children */);
1557 v
->num_children
= -1;
1561 r
.type_changed
= true;
1564 if (install_new_value (v
, newobj
, r
.type_changed
))
1571 /* We probably should not get children of a dynamic varobj, but
1572 for which -var-list-children was never invoked. */
1573 if (varobj_is_dynamic_p (v
))
1575 std::vector
<varobj
*> changed
, type_changed_vec
, unchanged
, newobj_vec
;
1576 bool children_changed
= false;
1581 if (!v
->dynamic
->children_requested
)
1585 /* If we initially did not have potential children, but
1586 now we do, consider the varobj as changed.
1587 Otherwise, if children were never requested, consider
1588 it as unchanged -- presumably, such varobj is not yet
1589 expanded in the UI, so we need not bother getting
1591 if (!varobj_has_more (v
, 0))
1593 update_dynamic_varobj_children (v
, NULL
, NULL
, NULL
, NULL
,
1594 &dummy
, false, 0, 0);
1595 if (varobj_has_more (v
, 0))
1600 result
.push_back (std::move (r
));
1605 /* If update_dynamic_varobj_children returns false, then we have
1606 a non-conforming pretty-printer, so we skip it. */
1607 if (update_dynamic_varobj_children (v
, &changed
, &type_changed_vec
,
1609 &unchanged
, &children_changed
,
1610 true, v
->from
, v
->to
))
1612 if (children_changed
|| !newobj_vec
.empty ())
1614 r
.children_changed
= true;
1615 r
.newobj
= std::move (newobj_vec
);
1617 /* Push in reverse order so that the first child is
1618 popped from the work stack first, and so will be
1619 added to result first. This does not affect
1620 correctness, just "nicer". */
1621 for (int i
= type_changed_vec
.size () - 1; i
>= 0; --i
)
1623 varobj_update_result
item (type_changed_vec
[i
]);
1625 /* Type may change only if value was changed. */
1626 item
.changed
= true;
1627 item
.type_changed
= true;
1628 item
.value_installed
= true;
1630 stack
.push_back (std::move (item
));
1632 for (int i
= changed
.size () - 1; i
>= 0; --i
)
1634 varobj_update_result
item (changed
[i
]);
1636 item
.changed
= true;
1637 item
.value_installed
= true;
1639 stack
.push_back (std::move (item
));
1641 for (int i
= unchanged
.size () - 1; i
>= 0; --i
)
1643 if (!unchanged
[i
]->frozen
)
1645 varobj_update_result
item (unchanged
[i
]);
1647 item
.value_installed
= true;
1649 stack
.push_back (std::move (item
));
1652 if (r
.changed
|| r
.children_changed
)
1653 result
.push_back (std::move (r
));
1659 /* Push any children. Use reverse order so that the first
1660 child is popped from the work stack first, and so
1661 will be added to result first. This does not
1662 affect correctness, just "nicer". */
1663 for (int i
= v
->children
.size () - 1; i
>= 0; --i
)
1665 varobj
*c
= v
->children
[i
];
1667 /* Child may be NULL if explicitly deleted by -var-delete. */
1668 if (c
!= NULL
&& !c
->frozen
)
1669 stack
.emplace_back (c
);
1672 if (r
.changed
|| r
.type_changed
)
1673 result
.push_back (std::move (r
));
1679 /* Helper functions */
1682 * Variable object construction/destruction
1686 delete_variable (struct varobj
*var
, bool only_children_p
)
1690 delete_variable_1 (&delcount
, var
, only_children_p
,
1691 true /* remove_from_parent_p */ );
1696 /* Delete the variable object VAR and its children. */
1697 /* IMPORTANT NOTE: If we delete a variable which is a child
1698 and the parent is not removed we dump core. It must be always
1699 initially called with remove_from_parent_p set. */
1701 delete_variable_1 (int *delcountp
, struct varobj
*var
, bool only_children_p
,
1702 bool remove_from_parent_p
)
1704 /* Delete any children of this variable, too. */
1705 for (varobj
*child
: var
->children
)
1710 if (!remove_from_parent_p
)
1711 child
->parent
= NULL
;
1713 delete_variable_1 (delcountp
, child
, false, only_children_p
);
1715 var
->children
.clear ();
1717 /* if we were called to delete only the children we are done here. */
1718 if (only_children_p
)
1721 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
1722 /* If the name is empty, this is a temporary variable, that has not
1723 yet been installed, don't report it, it belongs to the caller... */
1724 if (!var
->obj_name
.empty ())
1726 *delcountp
= *delcountp
+ 1;
1729 /* If this variable has a parent, remove it from its parent's list. */
1730 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1731 (as indicated by remove_from_parent_p) we don't bother doing an
1732 expensive list search to find the element to remove when we are
1733 discarding the list afterwards. */
1734 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1735 var
->parent
->children
[var
->index
] = NULL
;
1737 if (!var
->obj_name
.empty ())
1738 uninstall_variable (var
);
1740 /* Free memory associated with this variable. */
1744 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1746 install_variable (struct varobj
*var
)
1748 hashval_t hash
= htab_hash_string (var
->obj_name
.c_str ());
1749 void **slot
= htab_find_slot_with_hash (varobj_table
,
1750 var
->obj_name
.c_str (),
1752 if (*slot
!= nullptr)
1753 error (_("Duplicate variable object name"));
1755 /* Add varobj to hash table. */
1758 /* If root, add varobj to root list. */
1759 if (is_root_p (var
))
1760 rootlist
.push_front (var
->root
);
1763 /* Uninstall the object VAR. */
1765 uninstall_variable (struct varobj
*var
)
1767 hashval_t hash
= htab_hash_string (var
->obj_name
.c_str ());
1768 htab_remove_elt_with_hash (varobj_table
, var
->obj_name
.c_str (), hash
);
1771 gdb_printf (gdb_stdlog
, "Deleting %s\n", var
->obj_name
.c_str ());
1773 /* If root, remove varobj from root list. */
1774 if (is_root_p (var
))
1776 auto iter
= std::find (rootlist
.begin (), rootlist
.end (), var
->root
);
1777 rootlist
.erase (iter
);
1781 /* Create and install a child of the parent of the given name.
1783 The created VAROBJ takes ownership of the allocated NAME. */
1785 static struct varobj
*
1786 create_child (struct varobj
*parent
, int index
, std::string
&name
)
1788 struct varobj_item item
;
1790 std::swap (item
.name
, name
);
1791 item
.value
= release_value (value_of_child (parent
, index
));
1793 return create_child_with_value (parent
, index
, &item
);
1796 static struct varobj
*
1797 create_child_with_value (struct varobj
*parent
, int index
,
1798 struct varobj_item
*item
)
1800 varobj
*child
= new varobj (parent
->root
);
1802 /* NAME is allocated by caller. */
1803 std::swap (child
->name
, item
->name
);
1804 child
->index
= index
;
1805 child
->parent
= parent
;
1807 if (varobj_is_anonymous_child (child
))
1808 child
->obj_name
= string_printf ("%s.%d_anonymous",
1809 parent
->obj_name
.c_str (), index
);
1811 child
->obj_name
= string_printf ("%s.%s",
1812 parent
->obj_name
.c_str (),
1813 child
->name
.c_str ());
1815 install_variable (child
);
1817 /* Compute the type of the child. Must do this before
1818 calling install_new_value. */
1819 if (item
->value
!= NULL
)
1820 /* If the child had no evaluation errors, var->value
1821 will be non-NULL and contain a valid type. */
1822 child
->type
= value_actual_type (item
->value
.get (), 0, NULL
);
1824 /* Otherwise, we must compute the type. */
1825 child
->type
= (*child
->root
->lang_ops
->type_of_child
) (child
->parent
,
1827 install_new_value (child
, item
->value
.get (), 1);
1834 * Miscellaneous utility functions.
1837 /* Allocate memory and initialize a new variable. */
1838 varobj::varobj (varobj_root
*root_
)
1839 : root (root_
), dynamic (new varobj_dynamic
)
1843 /* Free any allocated memory associated with VAR. */
1850 if (var
->dynamic
->pretty_printer
!= NULL
)
1852 gdbpy_enter_varobj
enter_py (var
);
1854 Py_XDECREF (var
->dynamic
->constructor
);
1855 Py_XDECREF (var
->dynamic
->pretty_printer
);
1859 /* This must be deleted before the root object, because Python-based
1860 destructors need access to some components. */
1861 delete var
->dynamic
;
1863 if (is_root_p (var
))
1867 /* Return the type of the value that's stored in VAR,
1868 or that would have being stored there if the
1869 value were accessible.
1871 This differs from VAR->type in that VAR->type is always
1872 the true type of the expression in the source language.
1873 The return value of this function is the type we're
1874 actually storing in varobj, and using for displaying
1875 the values and for comparing previous and new values.
1877 For example, top-level references are always stripped. */
1879 varobj_get_value_type (const struct varobj
*var
)
1883 if (var
->value
!= nullptr)
1884 type
= value_type (var
->value
.get ());
1888 type
= check_typedef (type
);
1890 if (TYPE_IS_REFERENCE (type
))
1891 type
= get_target_type (type
);
1893 type
= check_typedef (type
);
1898 /* What is the default display for this variable? We assume that
1899 everything is "natural". Any exceptions? */
1900 static enum varobj_display_formats
1901 variable_default_display (struct varobj
*var
)
1903 return FORMAT_NATURAL
;
1907 * Language-dependencies
1910 /* Common entry points */
1912 /* Return the number of children for a given variable.
1913 The result of this function is defined by the language
1914 implementation. The number of children returned by this function
1915 is the number of children that the user will see in the variable
1918 number_of_children (const struct varobj
*var
)
1920 return (*var
->root
->lang_ops
->number_of_children
) (var
);
1923 /* What is the expression for the root varobj VAR? */
1926 name_of_variable (const struct varobj
*var
)
1928 return (*var
->root
->lang_ops
->name_of_variable
) (var
);
1931 /* What is the name of the INDEX'th child of VAR? */
1934 name_of_child (struct varobj
*var
, int index
)
1936 return (*var
->root
->lang_ops
->name_of_child
) (var
, index
);
1939 /* If frame associated with VAR can be found, switch
1940 to it and return true. Otherwise, return false. */
1943 check_scope (const struct varobj
*var
)
1945 struct frame_info
*fi
;
1948 fi
= frame_find_by_id (var
->root
->frame
);
1953 CORE_ADDR pc
= get_frame_pc (fi
);
1955 if (pc
< var
->root
->valid_block
->start () ||
1956 pc
>= var
->root
->valid_block
->end ())
1964 /* Helper function to value_of_root. */
1966 static struct value
*
1967 value_of_root_1 (struct varobj
**var_handle
)
1969 struct value
*new_val
= NULL
;
1970 struct varobj
*var
= *var_handle
;
1971 bool within_scope
= false;
1973 /* Only root variables can be updated... */
1974 if (!is_root_p (var
))
1975 /* Not a root var. */
1978 scoped_restore_current_thread restore_thread
;
1980 /* Determine whether the variable is still around. */
1981 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
1982 within_scope
= true;
1983 else if (var
->root
->thread_id
== 0)
1985 /* The program was single-threaded when the variable object was
1986 created. Technically, it's possible that the program became
1987 multi-threaded since then, but we don't support such
1989 within_scope
= check_scope (var
);
1993 thread_info
*thread
= find_thread_global_id (var
->root
->thread_id
);
1997 switch_to_thread (thread
);
1998 within_scope
= check_scope (var
);
2005 /* We need to catch errors here, because if evaluate
2006 expression fails we want to just return NULL. */
2009 new_val
= evaluate_expression (var
->root
->exp
.get ());
2011 catch (const gdb_exception_error
&except
)
2019 /* What is the ``struct value *'' of the root variable VAR?
2020 For floating variable object, evaluation can get us a value
2021 of different type from what is stored in varobj already. In
2023 - *type_changed will be set to 1
2024 - old varobj will be freed, and new one will be
2025 created, with the same name.
2026 - *var_handle will be set to the new varobj
2027 Otherwise, *type_changed will be set to 0. */
2028 static struct value
*
2029 value_of_root (struct varobj
**var_handle
, bool *type_changed
)
2033 if (var_handle
== NULL
)
2038 /* This should really be an exception, since this should
2039 only get called with a root variable. */
2041 if (!is_root_p (var
))
2044 if (var
->root
->floating
)
2046 struct varobj
*tmp_var
;
2048 tmp_var
= varobj_create (NULL
, var
->name
.c_str (), (CORE_ADDR
) 0,
2049 USE_SELECTED_FRAME
);
2050 if (tmp_var
== NULL
)
2054 std::string old_type
= varobj_get_type (var
);
2055 std::string new_type
= varobj_get_type (tmp_var
);
2056 if (old_type
== new_type
)
2058 /* The expression presently stored inside var->root->exp
2059 remembers the locations of local variables relatively to
2060 the frame where the expression was created (in DWARF location
2061 button, for example). Naturally, those locations are not
2062 correct in other frames, so update the expression. */
2064 std::swap (var
->root
->exp
, tmp_var
->root
->exp
);
2066 varobj_delete (tmp_var
, 0);
2071 tmp_var
->obj_name
= var
->obj_name
;
2072 tmp_var
->from
= var
->from
;
2073 tmp_var
->to
= var
->to
;
2074 varobj_delete (var
, 0);
2076 install_variable (tmp_var
);
2077 *var_handle
= tmp_var
;
2079 *type_changed
= true;
2088 struct value
*value
;
2090 value
= value_of_root_1 (var_handle
);
2091 if (var
->value
== NULL
|| value
== NULL
)
2093 /* For root varobj-s, a NULL value indicates a scoping issue.
2094 So, nothing to do in terms of checking for mutations. */
2096 else if (varobj_value_has_mutated (var
, value
, value_type (value
)))
2098 /* The type has mutated, so the children are no longer valid.
2099 Just delete them, and tell our caller that the type has
2101 varobj_delete (var
, 1 /* only_children */);
2102 var
->num_children
= -1;
2105 *type_changed
= true;
2111 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2112 static struct value
*
2113 value_of_child (const struct varobj
*parent
, int index
)
2115 struct value
*value
;
2117 value
= (*parent
->root
->lang_ops
->value_of_child
) (parent
, index
);
2122 /* GDB already has a command called "value_of_variable". Sigh. */
2124 my_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2126 if (var
->root
->is_valid
)
2128 if (var
->dynamic
->pretty_printer
!= NULL
)
2129 return varobj_value_get_print_value (var
->value
.get (), var
->format
,
2131 return (*var
->root
->lang_ops
->value_of_variable
) (var
, format
);
2134 return std::string ();
2138 varobj_formatted_print_options (struct value_print_options
*opts
,
2139 enum varobj_display_formats format
)
2141 get_formatted_print_options (opts
, format_code
[(int) format
]);
2142 opts
->deref_ref
= 0;
2143 opts
->raw
= !pretty_printing
;
2147 varobj_value_get_print_value (struct value
*value
,
2148 enum varobj_display_formats format
,
2149 const struct varobj
*var
)
2151 struct value_print_options opts
;
2152 struct type
*type
= NULL
;
2154 gdb::unique_xmalloc_ptr
<char> encoding
;
2155 /* Initialize it just to avoid a GCC false warning. */
2156 CORE_ADDR str_addr
= 0;
2157 bool string_print
= false;
2160 return std::string ();
2163 std::string thevalue
;
2165 varobj_formatted_print_options (&opts
, format
);
2168 if (gdb_python_initialized
)
2170 PyObject
*value_formatter
= var
->dynamic
->pretty_printer
;
2172 gdbpy_enter_varobj
enter_py (var
);
2174 if (value_formatter
)
2176 /* First check to see if we have any children at all. If so,
2177 we simply return {...}. */
2178 if (dynamic_varobj_has_child_method (var
))
2181 if (PyObject_HasAttr (value_formatter
, gdbpy_to_string_cst
))
2183 struct value
*replacement
;
2185 gdbpy_ref
<> output
= apply_varobj_pretty_printer (value_formatter
,
2190 /* If we have string like output ... */
2193 /* If this is a lazy string, extract it. For lazy
2194 strings we always print as a string, so set
2196 if (gdbpy_is_lazy_string (output
.get ()))
2198 gdbpy_extract_lazy_string (output
.get (), &str_addr
,
2199 &type
, &len
, &encoding
);
2200 string_print
= true;
2204 /* If it is a regular (non-lazy) string, extract
2205 it and copy the contents into THEVALUE. If the
2206 hint says to print it as a string, set
2207 string_print. Otherwise just return the extracted
2208 string as a value. */
2210 gdb::unique_xmalloc_ptr
<char> s
2211 = python_string_to_target_string (output
.get ());
2215 struct gdbarch
*gdbarch
;
2217 gdb::unique_xmalloc_ptr
<char> hint
2218 = gdbpy_get_display_hint (value_formatter
);
2221 if (!strcmp (hint
.get (), "string"))
2222 string_print
= true;
2225 thevalue
= std::string (s
.get ());
2226 len
= thevalue
.size ();
2227 gdbarch
= value_type (value
)->arch ();
2228 type
= builtin_type (gdbarch
)->builtin_char
;
2234 gdbpy_print_stack ();
2237 /* If the printer returned a replacement value, set VALUE
2238 to REPLACEMENT. If there is not a replacement value,
2239 just use the value passed to this function. */
2241 value
= replacement
;
2247 /* If the THEVALUE has contents, it is a regular string. */
2248 if (!thevalue
.empty ())
2249 current_language
->printstr (&stb
, type
, (gdb_byte
*) thevalue
.c_str (),
2250 len
, encoding
.get (), 0, &opts
);
2251 else if (string_print
)
2252 /* Otherwise, if string_print is set, and it is not a regular
2253 string, it is a lazy string. */
2254 val_print_string (type
, encoding
.get (), str_addr
, len
, &stb
, &opts
);
2256 /* All other cases. */
2257 common_val_print (value
, &stb
, 0, &opts
, current_language
);
2259 return stb
.release ();
2263 varobj_editable_p (const struct varobj
*var
)
2267 if (!(var
->root
->is_valid
&& var
->value
!= nullptr
2268 && VALUE_LVAL (var
->value
.get ())))
2271 type
= varobj_get_value_type (var
);
2273 switch (type
->code ())
2275 case TYPE_CODE_STRUCT
:
2276 case TYPE_CODE_UNION
:
2277 case TYPE_CODE_ARRAY
:
2278 case TYPE_CODE_FUNC
:
2279 case TYPE_CODE_METHOD
:
2289 /* Call VAR's value_is_changeable_p language-specific callback. */
2292 varobj_value_is_changeable_p (const struct varobj
*var
)
2294 return var
->root
->lang_ops
->value_is_changeable_p (var
);
2297 /* Return true if that varobj is floating, that is is always evaluated in the
2298 selected frame, and not bound to thread/frame. Such variable objects
2299 are created using '@' as frame specifier to -var-create. */
2301 varobj_floating_p (const struct varobj
*var
)
2303 return var
->root
->floating
;
2306 /* Implement the "value_is_changeable_p" varobj callback for most
2310 varobj_default_value_is_changeable_p (const struct varobj
*var
)
2315 if (CPLUS_FAKE_CHILD (var
))
2318 type
= varobj_get_value_type (var
);
2320 switch (type
->code ())
2322 case TYPE_CODE_STRUCT
:
2323 case TYPE_CODE_UNION
:
2324 case TYPE_CODE_ARRAY
:
2335 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback
2339 all_root_varobjs (gdb::function_view
<void (struct varobj
*var
)> func
)
2341 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
2342 auto iter
= rootlist
.begin ();
2343 auto end
= rootlist
.end ();
2347 func ((*self
)->rootvar
);
2351 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
2352 defined on globals. It is a helper for varobj_invalidate.
2354 This function is called after changing the symbol file, in this case the
2355 pointers to "struct type" stored by the varobj are no longer valid. All
2356 varobj must be either re-evaluated, or marked as invalid here. */
2359 varobj_invalidate_iter (struct varobj
*var
)
2361 /* global and floating var must be re-evaluated. */
2362 if (var
->root
->floating
|| var
->root
->valid_block
== nullptr)
2364 struct varobj
*tmp_var
;
2366 /* Try to create a varobj with same expression. If we succeed
2367 replace the old varobj, otherwise invalidate it. */
2368 tmp_var
= varobj_create (nullptr, var
->name
.c_str (), (CORE_ADDR
) 0,
2370 ? USE_SELECTED_FRAME
: USE_CURRENT_FRAME
);
2371 if (tmp_var
!= nullptr)
2373 gdb_assert (var
->root
->floating
== tmp_var
->root
->floating
);
2374 tmp_var
->obj_name
= var
->obj_name
;
2375 varobj_delete (var
, 0);
2376 install_variable (tmp_var
);
2378 else if (!var
->root
->floating
)
2380 /* Only invalidate globals as floating vars might still be valid in
2381 some other frame. */
2382 var
->root
->is_valid
= false;
2385 else /* locals must be invalidated. */
2386 var
->root
->is_valid
= false;
2389 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2390 are defined on globals.
2391 Invalidated varobjs will be always printed in_scope="invalid". */
2394 varobj_invalidate (void)
2396 all_root_varobjs (varobj_invalidate_iter
);
2399 /* Ensure that no varobj keep references to OBJFILE. */
2402 varobj_invalidate_if_uses_objfile (struct objfile
*objfile
)
2404 if (objfile
->separate_debug_objfile_backlink
!= nullptr)
2405 objfile
= objfile
->separate_debug_objfile_backlink
;
2407 all_root_varobjs ([objfile
] (struct varobj
*var
)
2409 if (var
->root
->valid_block
!= nullptr)
2411 struct objfile
*bl_objfile
= block_objfile (var
->root
->valid_block
);
2412 if (bl_objfile
->separate_debug_objfile_backlink
!= nullptr)
2413 bl_objfile
= bl_objfile
->separate_debug_objfile_backlink
;
2415 if (bl_objfile
== objfile
)
2417 /* The varobj is tied to a block which is going away. There is
2418 no way to reconstruct something later, so invalidate the
2419 varobj completly and drop the reference to the block which is
2421 var
->root
->is_valid
= false;
2422 var
->root
->valid_block
= nullptr;
2426 if (var
->root
->exp
!= nullptr
2427 && exp_uses_objfile (var
->root
->exp
.get (), objfile
))
2429 /* The varobj's current expression references the objfile. For
2430 globals and floating, it is possible that when we try to
2431 re-evaluate the expression later it is still valid with
2432 whatever is in scope at that moment. Just invalidate the
2433 expression for now. */
2434 var
->root
->exp
.reset ();
2436 /* It only makes sense to keep a floating varobj around. */
2437 if (!var
->root
->floating
)
2438 var
->root
->is_valid
= false;
2441 /* var->value->type and var->type might also reference the objfile.
2442 This is taken care of in value.c:preserve_values which deals with
2443 making sure that objfile-owend types are replaced with
2444 gdbarch-owned equivalents. */
2448 /* A hash function for a varobj. */
2451 hash_varobj (const void *a
)
2453 const varobj
*obj
= (const varobj
*) a
;
2454 return htab_hash_string (obj
->obj_name
.c_str ());
2457 /* A hash table equality function for varobjs. */
2460 eq_varobj_and_string (const void *a
, const void *b
)
2462 const varobj
*obj
= (const varobj
*) a
;
2463 const char *name
= (const char *) b
;
2465 return obj
->obj_name
== name
;
2468 void _initialize_varobj ();
2470 _initialize_varobj ()
2472 varobj_table
= htab_create_alloc (5, hash_varobj
, eq_varobj_and_string
,
2473 nullptr, xcalloc
, xfree
);
2475 add_setshow_zuinteger_cmd ("varobj", class_maintenance
,
2477 _("Set varobj debugging."),
2478 _("Show varobj debugging."),
2479 _("When non-zero, varobj debugging is enabled."),
2480 NULL
, show_varobjdebug
,
2481 &setdebuglist
, &showdebuglist
);
2483 gdb::observers::free_objfile
.attach (varobj_invalidate_if_uses_objfile
,