1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2017 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"
30 #include "gdbthread.h"
32 #include "varobj-iter.h"
35 #include "python/python.h"
36 #include "python/python-internal.h"
41 /* Non-zero if we want to see trace of varobj level stuff. */
43 unsigned int varobjdebug
= 0;
45 show_varobjdebug (struct ui_file
*file
, int from_tty
,
46 struct cmd_list_element
*c
, const char *value
)
48 fprintf_filtered (file
, _("Varobj debugging is %s.\n"), value
);
51 /* String representations of gdb's format codes. */
52 char *varobj_format_string
[] =
53 { "natural", "binary", "decimal", "hexadecimal", "octal", "zero-hexadecimal" };
55 /* True if we want to allow Python-based pretty-printing. */
56 static int pretty_printing
= 0;
59 varobj_enable_pretty_printing (void)
66 /* Every root variable has one of these structures saved in its
71 /* The expression for this parent. */
74 /* Block for which this expression is valid. */
75 const struct block
*valid_block
;
77 /* The frame for this expression. This field is set iff valid_block is
79 struct frame_id frame
;
81 /* The global thread ID that this varobj_root belongs to. This field
82 is only valid if valid_block is not NULL.
83 When not 0, indicates which thread 'frame' belongs to.
84 When 0, indicates that the thread list was empty when the varobj_root
88 /* If 1, the -var-update always recomputes the value in the
89 current thread and frame. Otherwise, variable object is
90 always updated in the specific scope/thread/frame. */
93 /* Flag that indicates validity: set to 0 when this varobj_root refers
94 to symbols that do not exist anymore. */
97 /* Language-related operations for this variable and its
99 const struct lang_varobj_ops
*lang_ops
;
101 /* The varobj for this root node. */
102 struct varobj
*rootvar
;
104 /* Next root variable */
105 struct varobj_root
*next
;
108 /* Dynamic part of varobj. */
110 struct varobj_dynamic
112 /* Whether the children of this varobj were requested. This field is
113 used to decide if dynamic varobj should recompute their children.
114 In the event that the frontend never asked for the children, we
116 int children_requested
;
118 /* The pretty-printer constructor. If NULL, then the default
119 pretty-printer will be looked up. If None, then no
120 pretty-printer will be installed. */
121 PyObject
*constructor
;
123 /* The pretty-printer that has been constructed. If NULL, then a
124 new printer object is needed, and one will be constructed. */
125 PyObject
*pretty_printer
;
127 /* The iterator returned by the printer's 'children' method, or NULL
129 struct varobj_iter
*child_iter
;
131 /* We request one extra item from the iterator, so that we can
132 report to the caller whether there are more items than we have
133 already reported. However, we don't want to install this value
134 when we read it, because that will mess up future updates. So,
135 we stash it here instead. */
136 varobj_item
*saved_item
;
139 /* A list of varobjs */
147 /* Private function prototypes */
149 /* Helper functions for the above subcommands. */
151 static int delete_variable (struct varobj
*, int);
153 static void delete_variable_1 (int *, struct varobj
*, int, int);
155 static int install_variable (struct varobj
*);
157 static void uninstall_variable (struct varobj
*);
159 static struct varobj
*create_child (struct varobj
*, int, std::string
&);
161 static struct varobj
*
162 create_child_with_value (struct varobj
*parent
, int index
,
163 struct varobj_item
*item
);
165 /* Utility routines */
167 static struct varobj
*new_variable (void);
169 static struct varobj
*new_root_variable (void);
171 static void free_variable (struct varobj
*var
);
173 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
175 static enum varobj_display_formats
variable_default_display (struct varobj
*);
177 static int update_type_if_necessary (struct varobj
*var
,
178 struct value
*new_value
);
180 static int install_new_value (struct varobj
*var
, struct value
*value
,
183 /* Language-specific routines. */
185 static int number_of_children (const struct varobj
*);
187 static std::string
name_of_variable (const struct varobj
*);
189 static std::string
name_of_child (struct varobj
*, int);
191 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
193 static struct value
*value_of_child (const struct varobj
*parent
, int index
);
195 static std::string
my_value_of_variable (struct varobj
*var
,
196 enum varobj_display_formats format
);
198 static int is_root_p (const struct varobj
*var
);
200 static struct varobj
*varobj_add_child (struct varobj
*var
,
201 struct varobj_item
*item
);
205 /* Mappings of varobj_display_formats enums to gdb's format codes. */
206 static int format_code
[] = { 0, 't', 'd', 'x', 'o', 'z' };
208 /* Header of the list of root variable objects. */
209 static struct varobj_root
*rootlist
;
211 /* Prime number indicating the number of buckets in the hash table. */
212 /* A prime large enough to avoid too many collisions. */
213 #define VAROBJ_TABLE_SIZE 227
215 /* Pointer to the varobj hash table (built at run time). */
216 static struct vlist
**varobj_table
;
220 /* API Implementation */
222 is_root_p (const struct varobj
*var
)
224 return (var
->root
->rootvar
== var
);
228 /* Helper function to install a Python environment suitable for
229 use during operations on VAR. */
231 varobj_ensure_python_env (const struct varobj
*var
)
233 return ensure_python_env (var
->root
->exp
->gdbarch
,
234 var
->root
->exp
->language_defn
);
237 /* See python-internal.h. */
238 gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj
*var
)
239 : gdbpy_enter (var
->root
->exp
->gdbarch
, var
->root
->exp
->language_defn
)
245 /* Return the full FRAME which corresponds to the given CORE_ADDR
246 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
248 static struct frame_info
*
249 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
251 struct frame_info
*frame
= NULL
;
253 if (frame_addr
== (CORE_ADDR
) 0)
256 for (frame
= get_current_frame ();
258 frame
= get_prev_frame (frame
))
260 /* The CORE_ADDR we get as argument was parsed from a string GDB
261 output as $fp. This output got truncated to gdbarch_addr_bit.
262 Truncate the frame base address in the same manner before
263 comparing it against our argument. */
264 CORE_ADDR frame_base
= get_frame_base_address (frame
);
265 int addr_bit
= gdbarch_addr_bit (get_frame_arch (frame
));
267 if (addr_bit
< (sizeof (CORE_ADDR
) * HOST_CHAR_BIT
))
268 frame_base
&= ((CORE_ADDR
) 1 << addr_bit
) - 1;
270 if (frame_base
== frame_addr
)
277 /* Creates a varobj (not its children). */
280 varobj_create (const char *objname
,
281 const char *expression
, CORE_ADDR frame
, enum varobj_type type
)
284 struct cleanup
*old_chain
;
286 /* Fill out a varobj structure for the (root) variable being constructed. */
287 var
= new_root_variable ();
288 old_chain
= make_cleanup_free_variable (var
);
290 if (expression
!= NULL
)
292 struct frame_info
*fi
;
293 struct frame_id old_id
= null_frame_id
;
294 const struct block
*block
;
296 struct value
*value
= NULL
;
299 /* Parse and evaluate the expression, filling in as much of the
300 variable's data as possible. */
302 if (has_stack_frames ())
304 /* Allow creator to specify context of variable. */
305 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
306 fi
= get_selected_frame (NULL
);
308 /* FIXME: cagney/2002-11-23: This code should be doing a
309 lookup using the frame ID and not just the frame's
310 ``address''. This, of course, means an interface
311 change. However, with out that interface change ISAs,
312 such as the ia64 with its two stacks, won't work.
313 Similar goes for the case where there is a frameless
315 fi
= find_frame_addr_in_frame_chain (frame
);
320 /* frame = -2 means always use selected frame. */
321 if (type
== USE_SELECTED_FRAME
)
322 var
->root
->floating
= 1;
328 block
= get_frame_block (fi
, 0);
329 pc
= get_frame_pc (fi
);
333 innermost_block
= NULL
;
334 /* Wrap the call to parse expression, so we can
335 return a sensible error. */
338 var
->root
->exp
= parse_exp_1 (&p
, pc
, block
, 0);
341 CATCH (except
, RETURN_MASK_ERROR
)
343 do_cleanups (old_chain
);
348 /* Don't allow variables to be created for types. */
349 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
350 || var
->root
->exp
->elts
[0].opcode
== OP_TYPEOF
351 || var
->root
->exp
->elts
[0].opcode
== OP_DECLTYPE
)
353 do_cleanups (old_chain
);
354 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
355 " as an expression.\n");
359 var
->format
= variable_default_display (var
);
360 var
->root
->valid_block
= innermost_block
;
361 var
->name
= expression
;
362 /* For a root var, the name and the expr are the same. */
363 var
->path_expr
= expression
;
365 /* When the frame is different from the current frame,
366 we must select the appropriate frame before parsing
367 the expression, otherwise the value will not be current.
368 Since select_frame is so benign, just call it for all cases. */
371 /* User could specify explicit FRAME-ADDR which was not found but
372 EXPRESSION is frame specific and we would not be able to evaluate
373 it correctly next time. With VALID_BLOCK set we must also set
374 FRAME and THREAD_ID. */
376 error (_("Failed to find the specified frame"));
378 var
->root
->frame
= get_frame_id (fi
);
379 var
->root
->thread_id
= ptid_to_global_thread_id (inferior_ptid
);
380 old_id
= get_frame_id (get_selected_frame (NULL
));
384 /* We definitely need to catch errors here.
385 If evaluate_expression succeeds we got the value we wanted.
386 But if it fails, we still go on with a call to evaluate_type(). */
389 value
= evaluate_expression (var
->root
->exp
.get ());
391 CATCH (except
, RETURN_MASK_ERROR
)
393 /* Error getting the value. Try to at least get the
395 struct value
*type_only_value
= evaluate_type (var
->root
->exp
.get ());
397 var
->type
= value_type (type_only_value
);
403 int real_type_found
= 0;
405 var
->type
= value_actual_type (value
, 0, &real_type_found
);
407 value
= value_cast (var
->type
, value
);
410 /* Set language info */
411 var
->root
->lang_ops
= var
->root
->exp
->language_defn
->la_varobj_ops
;
413 install_new_value (var
, value
, 1 /* Initial assignment */);
415 /* Set ourselves as our root. */
416 var
->root
->rootvar
= var
;
418 /* Reset the selected frame. */
419 if (frame_id_p (old_id
))
420 select_frame (frame_find_by_id (old_id
));
423 /* If the variable object name is null, that means this
424 is a temporary variable, so don't install it. */
426 if ((var
!= NULL
) && (objname
!= NULL
))
428 var
->obj_name
= objname
;
430 /* If a varobj name is duplicated, the install will fail so
432 if (!install_variable (var
))
434 do_cleanups (old_chain
);
439 discard_cleanups (old_chain
);
443 /* Generates an unique name that can be used for a varobj. */
446 varobj_gen_name (void)
451 /* Generate a name for this object. */
453 obj_name
= xstrprintf ("var%d", id
);
458 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
459 error if OBJNAME cannot be found. */
462 varobj_get_handle (const char *objname
)
466 unsigned int index
= 0;
469 for (chp
= objname
; *chp
; chp
++)
471 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
474 cv
= *(varobj_table
+ index
);
475 while (cv
!= NULL
&& cv
->var
->obj_name
!= objname
)
479 error (_("Variable object not found"));
484 /* Given the handle, return the name of the object. */
487 varobj_get_objname (const struct varobj
*var
)
489 return var
->obj_name
.c_str ();
492 /* Given the handle, return the expression represented by the
496 varobj_get_expression (const struct varobj
*var
)
498 return name_of_variable (var
);
504 varobj_delete (struct varobj
*var
, int only_children
)
506 return delete_variable (var
, only_children
);
511 /* Convenience function for varobj_set_visualizer. Instantiate a
512 pretty-printer for a given value. */
514 instantiate_pretty_printer (PyObject
*constructor
, struct value
*value
)
516 PyObject
*val_obj
= NULL
;
519 val_obj
= value_to_value_object (value
);
523 printer
= PyObject_CallFunctionObjArgs (constructor
, val_obj
, NULL
);
530 /* Set/Get variable object display format. */
532 enum varobj_display_formats
533 varobj_set_display_format (struct varobj
*var
,
534 enum varobj_display_formats format
)
541 case FORMAT_HEXADECIMAL
:
543 case FORMAT_ZHEXADECIMAL
:
544 var
->format
= format
;
548 var
->format
= variable_default_display (var
);
551 if (varobj_value_is_changeable_p (var
)
552 && var
->value
&& !value_lazy (var
->value
))
554 var
->print_value
= varobj_value_get_print_value (var
->value
,
561 enum varobj_display_formats
562 varobj_get_display_format (const struct varobj
*var
)
567 gdb::unique_xmalloc_ptr
<char>
568 varobj_get_display_hint (const struct varobj
*var
)
570 gdb::unique_xmalloc_ptr
<char> result
;
573 struct cleanup
*back_to
;
575 if (!gdb_python_initialized
)
578 back_to
= varobj_ensure_python_env (var
);
580 if (var
->dynamic
->pretty_printer
!= NULL
)
581 result
= gdbpy_get_display_hint (var
->dynamic
->pretty_printer
);
583 do_cleanups (back_to
);
589 /* Return true if the varobj has items after TO, false otherwise. */
592 varobj_has_more (const struct varobj
*var
, int to
)
594 if (VEC_length (varobj_p
, var
->children
) > to
)
596 return ((to
== -1 || VEC_length (varobj_p
, var
->children
) == to
)
597 && (var
->dynamic
->saved_item
!= NULL
));
600 /* If the variable object is bound to a specific thread, that
601 is its evaluation can always be done in context of a frame
602 inside that thread, returns GDB id of the thread -- which
603 is always positive. Otherwise, returns -1. */
605 varobj_get_thread_id (const struct varobj
*var
)
607 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
608 return var
->root
->thread_id
;
614 varobj_set_frozen (struct varobj
*var
, int frozen
)
616 /* When a variable is unfrozen, we don't fetch its value.
617 The 'not_fetched' flag remains set, so next -var-update
620 We don't fetch the value, because for structures the client
621 should do -var-update anyway. It would be bad to have different
622 client-size logic for structure and other types. */
623 var
->frozen
= frozen
;
627 varobj_get_frozen (const struct varobj
*var
)
632 /* A helper function that restricts a range to what is actually
633 available in a VEC. This follows the usual rules for the meaning
634 of FROM and TO -- if either is negative, the entire range is
638 varobj_restrict_range (VEC (varobj_p
) *children
, int *from
, int *to
)
640 if (*from
< 0 || *to
< 0)
643 *to
= VEC_length (varobj_p
, children
);
647 if (*from
> VEC_length (varobj_p
, children
))
648 *from
= VEC_length (varobj_p
, children
);
649 if (*to
> VEC_length (varobj_p
, children
))
650 *to
= VEC_length (varobj_p
, children
);
656 /* A helper for update_dynamic_varobj_children that installs a new
657 child when needed. */
660 install_dynamic_child (struct varobj
*var
,
661 VEC (varobj_p
) **changed
,
662 VEC (varobj_p
) **type_changed
,
663 VEC (varobj_p
) **newobj
,
664 VEC (varobj_p
) **unchanged
,
667 struct varobj_item
*item
)
669 if (VEC_length (varobj_p
, var
->children
) < index
+ 1)
671 /* There's no child yet. */
672 struct varobj
*child
= varobj_add_child (var
, item
);
676 VEC_safe_push (varobj_p
, *newobj
, child
);
682 varobj_p existing
= VEC_index (varobj_p
, var
->children
, index
);
683 int type_updated
= update_type_if_necessary (existing
, item
->value
);
688 VEC_safe_push (varobj_p
, *type_changed
, existing
);
690 if (install_new_value (existing
, item
->value
, 0))
692 if (!type_updated
&& changed
)
693 VEC_safe_push (varobj_p
, *changed
, existing
);
695 else if (!type_updated
&& unchanged
)
696 VEC_safe_push (varobj_p
, *unchanged
, existing
);
703 dynamic_varobj_has_child_method (const struct varobj
*var
)
705 struct cleanup
*back_to
;
706 PyObject
*printer
= var
->dynamic
->pretty_printer
;
709 if (!gdb_python_initialized
)
712 back_to
= varobj_ensure_python_env (var
);
713 result
= PyObject_HasAttr (printer
, gdbpy_children_cst
);
714 do_cleanups (back_to
);
719 /* A factory for creating dynamic varobj's iterators. Returns an
720 iterator object suitable for iterating over VAR's children. */
722 static struct varobj_iter
*
723 varobj_get_iterator (struct varobj
*var
)
726 if (var
->dynamic
->pretty_printer
)
727 return py_varobj_get_iterator (var
, var
->dynamic
->pretty_printer
);
730 gdb_assert_not_reached (_("\
731 requested an iterator from a non-dynamic varobj"));
734 /* Release and clear VAR's saved item, if any. */
737 varobj_clear_saved_item (struct varobj_dynamic
*var
)
739 if (var
->saved_item
!= NULL
)
741 value_free (var
->saved_item
->value
);
742 xfree (var
->saved_item
);
743 var
->saved_item
= NULL
;
748 update_dynamic_varobj_children (struct varobj
*var
,
749 VEC (varobj_p
) **changed
,
750 VEC (varobj_p
) **type_changed
,
751 VEC (varobj_p
) **newobj
,
752 VEC (varobj_p
) **unchanged
,
762 if (update_children
|| var
->dynamic
->child_iter
== NULL
)
764 varobj_iter_delete (var
->dynamic
->child_iter
);
765 var
->dynamic
->child_iter
= varobj_get_iterator (var
);
767 varobj_clear_saved_item (var
->dynamic
);
771 if (var
->dynamic
->child_iter
== NULL
)
775 i
= VEC_length (varobj_p
, var
->children
);
777 /* We ask for one extra child, so that MI can report whether there
778 are more children. */
779 for (; to
< 0 || i
< to
+ 1; ++i
)
783 /* See if there was a leftover from last time. */
784 if (var
->dynamic
->saved_item
!= NULL
)
786 item
= var
->dynamic
->saved_item
;
787 var
->dynamic
->saved_item
= NULL
;
791 item
= varobj_iter_next (var
->dynamic
->child_iter
);
792 /* Release vitem->value so its lifetime is not bound to the
793 execution of a command. */
794 if (item
!= NULL
&& item
->value
!= NULL
)
795 release_value_or_incref (item
->value
);
800 /* Iteration is done. Remove iterator from VAR. */
801 varobj_iter_delete (var
->dynamic
->child_iter
);
802 var
->dynamic
->child_iter
= NULL
;
805 /* We don't want to push the extra child on any report list. */
806 if (to
< 0 || i
< to
)
808 int can_mention
= from
< 0 || i
>= from
;
810 install_dynamic_child (var
, can_mention
? changed
: NULL
,
811 can_mention
? type_changed
: NULL
,
812 can_mention
? newobj
: NULL
,
813 can_mention
? unchanged
: NULL
,
814 can_mention
? cchanged
: NULL
, i
,
821 var
->dynamic
->saved_item
= item
;
823 /* We want to truncate the child list just before this
829 if (i
< VEC_length (varobj_p
, var
->children
))
834 for (j
= i
; j
< VEC_length (varobj_p
, var
->children
); ++j
)
835 varobj_delete (VEC_index (varobj_p
, var
->children
, j
), 0);
836 VEC_truncate (varobj_p
, var
->children
, i
);
839 /* If there are fewer children than requested, note that the list of
841 if (to
>= 0 && VEC_length (varobj_p
, var
->children
) < to
)
844 var
->num_children
= VEC_length (varobj_p
, var
->children
);
850 varobj_get_num_children (struct varobj
*var
)
852 if (var
->num_children
== -1)
854 if (varobj_is_dynamic_p (var
))
858 /* If we have a dynamic varobj, don't report -1 children.
859 So, try to fetch some children first. */
860 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
, &dummy
,
864 var
->num_children
= number_of_children (var
);
867 return var
->num_children
>= 0 ? var
->num_children
: 0;
870 /* Creates a list of the immediate children of a variable object;
871 the return code is the number of such children or -1 on error. */
874 varobj_list_children (struct varobj
*var
, int *from
, int *to
)
876 int i
, children_changed
;
878 var
->dynamic
->children_requested
= 1;
880 if (varobj_is_dynamic_p (var
))
882 /* This, in theory, can result in the number of children changing without
883 frontend noticing. But well, calling -var-list-children on the same
884 varobj twice is not something a sane frontend would do. */
885 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
,
886 &children_changed
, 0, 0, *to
);
887 varobj_restrict_range (var
->children
, from
, to
);
888 return var
->children
;
891 if (var
->num_children
== -1)
892 var
->num_children
= number_of_children (var
);
894 /* If that failed, give up. */
895 if (var
->num_children
== -1)
896 return var
->children
;
898 /* If we're called when the list of children is not yet initialized,
899 allocate enough elements in it. */
900 while (VEC_length (varobj_p
, var
->children
) < var
->num_children
)
901 VEC_safe_push (varobj_p
, var
->children
, NULL
);
903 for (i
= 0; i
< var
->num_children
; i
++)
905 varobj_p existing
= VEC_index (varobj_p
, var
->children
, i
);
907 if (existing
== NULL
)
909 /* Either it's the first call to varobj_list_children for
910 this variable object, and the child was never created,
911 or it was explicitly deleted by the client. */
912 std::string name
= name_of_child (var
, i
);
913 existing
= create_child (var
, i
, name
);
914 VEC_replace (varobj_p
, var
->children
, i
, existing
);
918 varobj_restrict_range (var
->children
, from
, to
);
919 return var
->children
;
922 static struct varobj
*
923 varobj_add_child (struct varobj
*var
, struct varobj_item
*item
)
925 varobj_p v
= create_child_with_value (var
,
926 VEC_length (varobj_p
, var
->children
),
929 VEC_safe_push (varobj_p
, var
->children
, v
);
933 /* Obtain the type of an object Variable as a string similar to the one gdb
934 prints on the console. The caller is responsible for freeing the string.
938 varobj_get_type (struct varobj
*var
)
940 /* For the "fake" variables, do not return a type. (Its type is
942 Do not return a type for invalid variables as well. */
943 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
944 return std::string ();
946 return type_to_string (var
->type
);
949 /* Obtain the type of an object variable. */
952 varobj_get_gdb_type (const struct varobj
*var
)
957 /* Is VAR a path expression parent, i.e., can it be used to construct
958 a valid path expression? */
961 is_path_expr_parent (const struct varobj
*var
)
963 gdb_assert (var
->root
->lang_ops
->is_path_expr_parent
!= NULL
);
964 return var
->root
->lang_ops
->is_path_expr_parent (var
);
967 /* Is VAR a path expression parent, i.e., can it be used to construct
968 a valid path expression? By default we assume any VAR can be a path
972 varobj_default_is_path_expr_parent (const struct varobj
*var
)
977 /* Return the path expression parent for VAR. */
979 const struct varobj
*
980 varobj_get_path_expr_parent (const struct varobj
*var
)
982 const struct varobj
*parent
= var
;
984 while (!is_root_p (parent
) && !is_path_expr_parent (parent
))
985 parent
= parent
->parent
;
990 /* Return a pointer to the full rooted expression of varobj VAR.
991 If it has not been computed yet, compute it. */
994 varobj_get_path_expr (const struct varobj
*var
)
996 if (var
->path_expr
.empty ())
998 /* For root varobjs, we initialize path_expr
999 when creating varobj, so here it should be
1001 struct varobj
*mutable_var
= (struct varobj
*) var
;
1002 gdb_assert (!is_root_p (var
));
1004 mutable_var
->path_expr
= (*var
->root
->lang_ops
->path_expr_of_child
) (var
);
1007 return var
->path_expr
.c_str ();
1010 const struct language_defn
*
1011 varobj_get_language (const struct varobj
*var
)
1013 return var
->root
->exp
->language_defn
;
1017 varobj_get_attributes (const struct varobj
*var
)
1021 if (varobj_editable_p (var
))
1022 /* FIXME: define masks for attributes. */
1023 attributes
|= 0x00000001; /* Editable */
1028 /* Return true if VAR is a dynamic varobj. */
1031 varobj_is_dynamic_p (const struct varobj
*var
)
1033 return var
->dynamic
->pretty_printer
!= NULL
;
1037 varobj_get_formatted_value (struct varobj
*var
,
1038 enum varobj_display_formats format
)
1040 return my_value_of_variable (var
, format
);
1044 varobj_get_value (struct varobj
*var
)
1046 return my_value_of_variable (var
, var
->format
);
1049 /* Set the value of an object variable (if it is editable) to the
1050 value of the given expression. */
1051 /* Note: Invokes functions that can call error(). */
1054 varobj_set_value (struct varobj
*var
, const char *expression
)
1056 struct value
*val
= NULL
; /* Initialize to keep gcc happy. */
1057 /* The argument "expression" contains the variable's new value.
1058 We need to first construct a legal expression for this -- ugh! */
1059 /* Does this cover all the bases? */
1060 struct value
*value
= NULL
; /* Initialize to keep gcc happy. */
1061 int saved_input_radix
= input_radix
;
1062 const char *s
= expression
;
1064 gdb_assert (varobj_editable_p (var
));
1066 input_radix
= 10; /* ALWAYS reset to decimal temporarily. */
1067 expression_up exp
= parse_exp_1 (&s
, 0, 0, 0);
1070 value
= evaluate_expression (exp
.get ());
1073 CATCH (except
, RETURN_MASK_ERROR
)
1075 /* We cannot proceed without a valid expression. */
1080 /* All types that are editable must also be changeable. */
1081 gdb_assert (varobj_value_is_changeable_p (var
));
1083 /* The value of a changeable variable object must not be lazy. */
1084 gdb_assert (!value_lazy (var
->value
));
1086 /* Need to coerce the input. We want to check if the
1087 value of the variable object will be different
1088 after assignment, and the first thing value_assign
1089 does is coerce the input.
1090 For example, if we are assigning an array to a pointer variable we
1091 should compare the pointer with the array's address, not with the
1093 value
= coerce_array (value
);
1095 /* The new value may be lazy. value_assign, or
1096 rather value_contents, will take care of this. */
1099 val
= value_assign (var
->value
, value
);
1102 CATCH (except
, RETURN_MASK_ERROR
)
1108 /* If the value has changed, record it, so that next -var-update can
1109 report this change. If a variable had a value of '1', we've set it
1110 to '333' and then set again to '1', when -var-update will report this
1111 variable as changed -- because the first assignment has set the
1112 'updated' flag. There's no need to optimize that, because return value
1113 of -var-update should be considered an approximation. */
1114 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
1115 input_radix
= saved_input_radix
;
1121 /* A helper function to install a constructor function and visualizer
1122 in a varobj_dynamic. */
1125 install_visualizer (struct varobj_dynamic
*var
, PyObject
*constructor
,
1126 PyObject
*visualizer
)
1128 Py_XDECREF (var
->constructor
);
1129 var
->constructor
= constructor
;
1131 Py_XDECREF (var
->pretty_printer
);
1132 var
->pretty_printer
= visualizer
;
1134 varobj_iter_delete (var
->child_iter
);
1135 var
->child_iter
= NULL
;
1138 /* Install the default visualizer for VAR. */
1141 install_default_visualizer (struct varobj
*var
)
1143 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1144 if (CPLUS_FAKE_CHILD (var
))
1147 if (pretty_printing
)
1149 PyObject
*pretty_printer
= NULL
;
1153 pretty_printer
= gdbpy_get_varobj_pretty_printer (var
->value
);
1154 if (! pretty_printer
)
1156 gdbpy_print_stack ();
1157 error (_("Cannot instantiate printer for default visualizer"));
1161 if (pretty_printer
== Py_None
)
1163 Py_DECREF (pretty_printer
);
1164 pretty_printer
= NULL
;
1167 install_visualizer (var
->dynamic
, NULL
, pretty_printer
);
1171 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1172 make a new object. */
1175 construct_visualizer (struct varobj
*var
, PyObject
*constructor
)
1177 PyObject
*pretty_printer
;
1179 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1180 if (CPLUS_FAKE_CHILD (var
))
1183 Py_INCREF (constructor
);
1184 if (constructor
== Py_None
)
1185 pretty_printer
= NULL
;
1188 pretty_printer
= instantiate_pretty_printer (constructor
, var
->value
);
1189 if (! pretty_printer
)
1191 gdbpy_print_stack ();
1192 Py_DECREF (constructor
);
1193 constructor
= Py_None
;
1194 Py_INCREF (constructor
);
1197 if (pretty_printer
== Py_None
)
1199 Py_DECREF (pretty_printer
);
1200 pretty_printer
= NULL
;
1204 install_visualizer (var
->dynamic
, constructor
, pretty_printer
);
1207 #endif /* HAVE_PYTHON */
1209 /* A helper function for install_new_value. This creates and installs
1210 a visualizer for VAR, if appropriate. */
1213 install_new_value_visualizer (struct varobj
*var
)
1216 /* If the constructor is None, then we want the raw value. If VAR
1217 does not have a value, just skip this. */
1218 if (!gdb_python_initialized
)
1221 if (var
->dynamic
->constructor
!= Py_None
&& var
->value
!= NULL
)
1223 struct cleanup
*cleanup
;
1225 cleanup
= varobj_ensure_python_env (var
);
1227 if (var
->dynamic
->constructor
== NULL
)
1228 install_default_visualizer (var
);
1230 construct_visualizer (var
, var
->dynamic
->constructor
);
1232 do_cleanups (cleanup
);
1239 /* When using RTTI to determine variable type it may be changed in runtime when
1240 the variable value is changed. This function checks whether type of varobj
1241 VAR will change when a new value NEW_VALUE is assigned and if it is so
1242 updates the type of VAR. */
1245 update_type_if_necessary (struct varobj
*var
, struct value
*new_value
)
1249 struct value_print_options opts
;
1251 get_user_print_options (&opts
);
1252 if (opts
.objectprint
)
1254 struct type
*new_type
= value_actual_type (new_value
, 0, 0);
1255 std::string new_type_str
= type_to_string (new_type
);
1256 std::string curr_type_str
= varobj_get_type (var
);
1258 /* Did the type name change? */
1259 if (curr_type_str
!= new_type_str
)
1261 var
->type
= new_type
;
1263 /* This information may be not valid for a new type. */
1264 varobj_delete (var
, 1);
1265 VEC_free (varobj_p
, var
->children
);
1266 var
->num_children
= -1;
1275 /* Assign a new value to a variable object. If INITIAL is non-zero,
1276 this is the first assignement after the variable object was just
1277 created, or changed type. In that case, just assign the value
1279 Otherwise, assign the new value, and return 1 if the value is
1280 different from the current one, 0 otherwise. The comparison is
1281 done on textual representation of value. Therefore, some types
1282 need not be compared. E.g. for structures the reported value is
1283 always "{...}", so no comparison is necessary here. If the old
1284 value was NULL and new one is not, or vice versa, we always return 1.
1286 The VALUE parameter should not be released -- the function will
1287 take care of releasing it when needed. */
1289 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
1294 int intentionally_not_fetched
= 0;
1296 /* We need to know the varobj's type to decide if the value should
1297 be fetched or not. C++ fake children (public/protected/private)
1298 don't have a type. */
1299 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
1300 changeable
= varobj_value_is_changeable_p (var
);
1302 /* If the type has custom visualizer, we consider it to be always
1303 changeable. FIXME: need to make sure this behaviour will not
1304 mess up read-sensitive values. */
1305 if (var
->dynamic
->pretty_printer
!= NULL
)
1308 need_to_fetch
= changeable
;
1310 /* We are not interested in the address of references, and given
1311 that in C++ a reference is not rebindable, it cannot
1312 meaningfully change. So, get hold of the real value. */
1314 value
= coerce_ref (value
);
1316 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
1317 /* For unions, we need to fetch the value implicitly because
1318 of implementation of union member fetch. When gdb
1319 creates a value for a field and the value of the enclosing
1320 structure is not lazy, it immediately copies the necessary
1321 bytes from the enclosing values. If the enclosing value is
1322 lazy, the call to value_fetch_lazy on the field will read
1323 the data from memory. For unions, that means we'll read the
1324 same memory more than once, which is not desirable. So
1328 /* The new value might be lazy. If the type is changeable,
1329 that is we'll be comparing values of this type, fetch the
1330 value now. Otherwise, on the next update the old value
1331 will be lazy, which means we've lost that old value. */
1332 if (need_to_fetch
&& value
&& value_lazy (value
))
1334 const struct varobj
*parent
= var
->parent
;
1335 int frozen
= var
->frozen
;
1337 for (; !frozen
&& parent
; parent
= parent
->parent
)
1338 frozen
|= parent
->frozen
;
1340 if (frozen
&& initial
)
1342 /* For variables that are frozen, or are children of frozen
1343 variables, we don't do fetch on initial assignment.
1344 For non-initial assignemnt we do the fetch, since it means we're
1345 explicitly asked to compare the new value with the old one. */
1346 intentionally_not_fetched
= 1;
1353 value_fetch_lazy (value
);
1356 CATCH (except
, RETURN_MASK_ERROR
)
1358 /* Set the value to NULL, so that for the next -var-update,
1359 we don't try to compare the new value with this value,
1360 that we couldn't even read. */
1367 /* Get a reference now, before possibly passing it to any Python
1368 code that might release it. */
1370 value_incref (value
);
1372 /* Below, we'll be comparing string rendering of old and new
1373 values. Don't get string rendering if the value is
1374 lazy -- if it is, the code above has decided that the value
1375 should not be fetched. */
1376 std::string print_value
;
1377 if (value
!= NULL
&& !value_lazy (value
)
1378 && var
->dynamic
->pretty_printer
== NULL
)
1379 print_value
= varobj_value_get_print_value (value
, var
->format
, var
);
1381 /* If the type is changeable, compare the old and the new values.
1382 If this is the initial assignment, we don't have any old value
1384 if (!initial
&& changeable
)
1386 /* If the value of the varobj was changed by -var-set-value,
1387 then the value in the varobj and in the target is the same.
1388 However, that value is different from the value that the
1389 varobj had after the previous -var-update. So need to the
1390 varobj as changed. */
1395 else if (var
->dynamic
->pretty_printer
== NULL
)
1397 /* Try to compare the values. That requires that both
1398 values are non-lazy. */
1399 if (var
->not_fetched
&& value_lazy (var
->value
))
1401 /* This is a frozen varobj and the value was never read.
1402 Presumably, UI shows some "never read" indicator.
1403 Now that we've fetched the real value, we need to report
1404 this varobj as changed so that UI can show the real
1408 else if (var
->value
== NULL
&& value
== NULL
)
1411 else if (var
->value
== NULL
|| value
== NULL
)
1417 gdb_assert (!value_lazy (var
->value
));
1418 gdb_assert (!value_lazy (value
));
1420 gdb_assert (!var
->print_value
.empty () && !print_value
.empty ());
1421 if (var
->print_value
!= print_value
)
1427 if (!initial
&& !changeable
)
1429 /* For values that are not changeable, we don't compare the values.
1430 However, we want to notice if a value was not NULL and now is NULL,
1431 or vise versa, so that we report when top-level varobjs come in scope
1432 and leave the scope. */
1433 changed
= (var
->value
!= NULL
) != (value
!= NULL
);
1436 /* We must always keep the new value, since children depend on it. */
1437 if (var
->value
!= NULL
&& var
->value
!= value
)
1438 value_free (var
->value
);
1440 if (value
&& value_lazy (value
) && intentionally_not_fetched
)
1441 var
->not_fetched
= 1;
1443 var
->not_fetched
= 0;
1446 install_new_value_visualizer (var
);
1448 /* If we installed a pretty-printer, re-compare the printed version
1449 to see if the variable changed. */
1450 if (var
->dynamic
->pretty_printer
!= NULL
)
1452 print_value
= varobj_value_get_print_value (var
->value
, var
->format
,
1454 if ((var
->print_value
.empty () && !print_value
.empty ())
1455 || (!var
->print_value
.empty () && print_value
.empty ())
1456 || (!var
->print_value
.empty () && !print_value
.empty ()
1457 && var
->print_value
!= print_value
))
1460 var
->print_value
= print_value
;
1462 gdb_assert (!var
->value
|| value_type (var
->value
));
1467 /* Return the requested range for a varobj. VAR is the varobj. FROM
1468 and TO are out parameters; *FROM and *TO will be set to the
1469 selected sub-range of VAR. If no range was selected using
1470 -var-set-update-range, then both will be -1. */
1472 varobj_get_child_range (const struct varobj
*var
, int *from
, int *to
)
1478 /* Set the selected sub-range of children of VAR to start at index
1479 FROM and end at index TO. If either FROM or TO is less than zero,
1480 this is interpreted as a request for all children. */
1482 varobj_set_child_range (struct varobj
*var
, int from
, int to
)
1489 varobj_set_visualizer (struct varobj
*var
, const char *visualizer
)
1492 PyObject
*mainmod
, *globals
, *constructor
;
1493 struct cleanup
*back_to
;
1495 if (!gdb_python_initialized
)
1498 back_to
= varobj_ensure_python_env (var
);
1500 mainmod
= PyImport_AddModule ("__main__");
1501 globals
= PyModule_GetDict (mainmod
);
1502 Py_INCREF (globals
);
1503 make_cleanup_py_decref (globals
);
1505 constructor
= PyRun_String (visualizer
, Py_eval_input
, globals
, globals
);
1509 gdbpy_print_stack ();
1510 error (_("Could not evaluate visualizer expression: %s"), visualizer
);
1513 construct_visualizer (var
, constructor
);
1514 Py_XDECREF (constructor
);
1516 /* If there are any children now, wipe them. */
1517 varobj_delete (var
, 1 /* children only */);
1518 var
->num_children
= -1;
1520 do_cleanups (back_to
);
1522 error (_("Python support required"));
1526 /* If NEW_VALUE is the new value of the given varobj (var), return
1527 non-zero if var has mutated. In other words, if the type of
1528 the new value is different from the type of the varobj's old
1531 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1534 varobj_value_has_mutated (const struct varobj
*var
, struct value
*new_value
,
1535 struct type
*new_type
)
1537 /* If we haven't previously computed the number of children in var,
1538 it does not matter from the front-end's perspective whether
1539 the type has mutated or not. For all intents and purposes,
1540 it has not mutated. */
1541 if (var
->num_children
< 0)
1544 if (var
->root
->lang_ops
->value_has_mutated
)
1546 /* The varobj module, when installing new values, explicitly strips
1547 references, saying that we're not interested in those addresses.
1548 But detection of mutation happens before installing the new
1549 value, so our value may be a reference that we need to strip
1550 in order to remain consistent. */
1551 if (new_value
!= NULL
)
1552 new_value
= coerce_ref (new_value
);
1553 return var
->root
->lang_ops
->value_has_mutated (var
, new_value
, new_type
);
1559 /* Update the values for a variable and its children. This is a
1560 two-pronged attack. First, re-parse the value for the root's
1561 expression to see if it's changed. Then go all the way
1562 through its children, reconstructing them and noting if they've
1565 The EXPLICIT parameter specifies if this call is result
1566 of MI request to update this specific variable, or
1567 result of implicit -var-update *. For implicit request, we don't
1568 update frozen variables.
1570 NOTE: This function may delete the caller's varobj. If it
1571 returns TYPE_CHANGED, then it has done this and VARP will be modified
1572 to point to the new varobj. */
1574 VEC(varobj_update_result
) *
1575 varobj_update (struct varobj
**varp
, int is_explicit
)
1577 int type_changed
= 0;
1579 struct value
*newobj
;
1580 VEC (varobj_update_result
) *stack
= NULL
;
1581 VEC (varobj_update_result
) *result
= NULL
;
1583 /* Frozen means frozen -- we don't check for any change in
1584 this varobj, including its going out of scope, or
1585 changing type. One use case for frozen varobjs is
1586 retaining previously evaluated expressions, and we don't
1587 want them to be reevaluated at all. */
1588 if (!is_explicit
&& (*varp
)->frozen
)
1591 if (!(*varp
)->root
->is_valid
)
1593 varobj_update_result r
= {0};
1596 r
.status
= VAROBJ_INVALID
;
1597 VEC_safe_push (varobj_update_result
, result
, &r
);
1601 if ((*varp
)->root
->rootvar
== *varp
)
1603 varobj_update_result r
= {0};
1606 r
.status
= VAROBJ_IN_SCOPE
;
1608 /* Update the root variable. value_of_root can return NULL
1609 if the variable is no longer around, i.e. we stepped out of
1610 the frame in which a local existed. We are letting the
1611 value_of_root variable dispose of the varobj if the type
1613 newobj
= value_of_root (varp
, &type_changed
);
1614 if (update_type_if_necessary(*varp
, newobj
))
1617 r
.type_changed
= type_changed
;
1618 if (install_new_value ((*varp
), newobj
, type_changed
))
1622 r
.status
= VAROBJ_NOT_IN_SCOPE
;
1623 r
.value_installed
= 1;
1625 if (r
.status
== VAROBJ_NOT_IN_SCOPE
)
1627 if (r
.type_changed
|| r
.changed
)
1628 VEC_safe_push (varobj_update_result
, result
, &r
);
1632 VEC_safe_push (varobj_update_result
, stack
, &r
);
1636 varobj_update_result r
= {0};
1639 VEC_safe_push (varobj_update_result
, stack
, &r
);
1642 /* Walk through the children, reconstructing them all. */
1643 while (!VEC_empty (varobj_update_result
, stack
))
1645 varobj_update_result r
= *(VEC_last (varobj_update_result
, stack
));
1646 struct varobj
*v
= r
.varobj
;
1648 VEC_pop (varobj_update_result
, stack
);
1650 /* Update this variable, unless it's a root, which is already
1652 if (!r
.value_installed
)
1654 struct type
*new_type
;
1656 newobj
= value_of_child (v
->parent
, v
->index
);
1657 if (update_type_if_necessary(v
, newobj
))
1660 new_type
= value_type (newobj
);
1662 new_type
= v
->root
->lang_ops
->type_of_child (v
->parent
, v
->index
);
1664 if (varobj_value_has_mutated (v
, newobj
, new_type
))
1666 /* The children are no longer valid; delete them now.
1667 Report the fact that its type changed as well. */
1668 varobj_delete (v
, 1 /* only_children */);
1669 v
->num_children
= -1;
1676 if (install_new_value (v
, newobj
, r
.type_changed
))
1683 /* We probably should not get children of a dynamic varobj, but
1684 for which -var-list-children was never invoked. */
1685 if (varobj_is_dynamic_p (v
))
1687 VEC (varobj_p
) *changed
= 0, *type_changed
= 0, *unchanged
= 0;
1688 VEC (varobj_p
) *newobj
= 0;
1689 int i
, children_changed
= 0;
1694 if (!v
->dynamic
->children_requested
)
1698 /* If we initially did not have potential children, but
1699 now we do, consider the varobj as changed.
1700 Otherwise, if children were never requested, consider
1701 it as unchanged -- presumably, such varobj is not yet
1702 expanded in the UI, so we need not bother getting
1704 if (!varobj_has_more (v
, 0))
1706 update_dynamic_varobj_children (v
, NULL
, NULL
, NULL
, NULL
,
1708 if (varobj_has_more (v
, 0))
1713 VEC_safe_push (varobj_update_result
, result
, &r
);
1718 /* If update_dynamic_varobj_children returns 0, then we have
1719 a non-conforming pretty-printer, so we skip it. */
1720 if (update_dynamic_varobj_children (v
, &changed
, &type_changed
, &newobj
,
1721 &unchanged
, &children_changed
, 1,
1724 if (children_changed
|| newobj
)
1726 r
.children_changed
= 1;
1729 /* Push in reverse order so that the first child is
1730 popped from the work stack first, and so will be
1731 added to result first. This does not affect
1732 correctness, just "nicer". */
1733 for (i
= VEC_length (varobj_p
, type_changed
) - 1; i
>= 0; --i
)
1735 varobj_p tmp
= VEC_index (varobj_p
, type_changed
, i
);
1736 varobj_update_result r
= {0};
1738 /* Type may change only if value was changed. */
1742 r
.value_installed
= 1;
1743 VEC_safe_push (varobj_update_result
, stack
, &r
);
1745 for (i
= VEC_length (varobj_p
, changed
) - 1; i
>= 0; --i
)
1747 varobj_p tmp
= VEC_index (varobj_p
, changed
, i
);
1748 varobj_update_result r
= {0};
1752 r
.value_installed
= 1;
1753 VEC_safe_push (varobj_update_result
, stack
, &r
);
1755 for (i
= VEC_length (varobj_p
, unchanged
) - 1; i
>= 0; --i
)
1757 varobj_p tmp
= VEC_index (varobj_p
, unchanged
, i
);
1761 varobj_update_result r
= {0};
1764 r
.value_installed
= 1;
1765 VEC_safe_push (varobj_update_result
, stack
, &r
);
1768 if (r
.changed
|| r
.children_changed
)
1769 VEC_safe_push (varobj_update_result
, result
, &r
);
1771 /* Free CHANGED, TYPE_CHANGED and UNCHANGED, but not NEW,
1772 because NEW has been put into the result vector. */
1773 VEC_free (varobj_p
, changed
);
1774 VEC_free (varobj_p
, type_changed
);
1775 VEC_free (varobj_p
, unchanged
);
1781 /* Push any children. Use reverse order so that the first
1782 child is popped from the work stack first, and so
1783 will be added to result first. This does not
1784 affect correctness, just "nicer". */
1785 for (i
= VEC_length (varobj_p
, v
->children
)-1; i
>= 0; --i
)
1787 varobj_p c
= VEC_index (varobj_p
, v
->children
, i
);
1789 /* Child may be NULL if explicitly deleted by -var-delete. */
1790 if (c
!= NULL
&& !c
->frozen
)
1792 varobj_update_result r
= {0};
1795 VEC_safe_push (varobj_update_result
, stack
, &r
);
1799 if (r
.changed
|| r
.type_changed
)
1800 VEC_safe_push (varobj_update_result
, result
, &r
);
1803 VEC_free (varobj_update_result
, stack
);
1809 /* Helper functions */
1812 * Variable object construction/destruction
1816 delete_variable (struct varobj
*var
, int only_children_p
)
1820 delete_variable_1 (&delcount
, var
, only_children_p
,
1821 1 /* remove_from_parent_p */ );
1826 /* Delete the variable object VAR and its children. */
1827 /* IMPORTANT NOTE: If we delete a variable which is a child
1828 and the parent is not removed we dump core. It must be always
1829 initially called with remove_from_parent_p set. */
1831 delete_variable_1 (int *delcountp
, struct varobj
*var
, int only_children_p
,
1832 int remove_from_parent_p
)
1836 /* Delete any children of this variable, too. */
1837 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
1839 varobj_p child
= VEC_index (varobj_p
, var
->children
, i
);
1843 if (!remove_from_parent_p
)
1844 child
->parent
= NULL
;
1845 delete_variable_1 (delcountp
, child
, 0, only_children_p
);
1847 VEC_free (varobj_p
, var
->children
);
1849 /* if we were called to delete only the children we are done here. */
1850 if (only_children_p
)
1853 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
1854 /* If the name is empty, this is a temporary variable, that has not
1855 yet been installed, don't report it, it belongs to the caller... */
1856 if (!var
->obj_name
.empty ())
1858 *delcountp
= *delcountp
+ 1;
1861 /* If this variable has a parent, remove it from its parent's list. */
1862 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1863 (as indicated by remove_from_parent_p) we don't bother doing an
1864 expensive list search to find the element to remove when we are
1865 discarding the list afterwards. */
1866 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
1868 VEC_replace (varobj_p
, var
->parent
->children
, var
->index
, NULL
);
1871 if (!var
->obj_name
.empty ())
1872 uninstall_variable (var
);
1874 /* Free memory associated with this variable. */
1875 free_variable (var
);
1878 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1880 install_variable (struct varobj
*var
)
1883 struct vlist
*newvl
;
1885 unsigned int index
= 0;
1888 for (chp
= var
->obj_name
.c_str (); *chp
; chp
++)
1890 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1893 cv
= *(varobj_table
+ index
);
1894 while (cv
!= NULL
&& cv
->var
->obj_name
!= var
->obj_name
)
1898 error (_("Duplicate variable object name"));
1900 /* Add varobj to hash table. */
1901 newvl
= XNEW (struct vlist
);
1902 newvl
->next
= *(varobj_table
+ index
);
1904 *(varobj_table
+ index
) = newvl
;
1906 /* If root, add varobj to root list. */
1907 if (is_root_p (var
))
1909 /* Add to list of root variables. */
1910 if (rootlist
== NULL
)
1911 var
->root
->next
= NULL
;
1913 var
->root
->next
= rootlist
;
1914 rootlist
= var
->root
;
1920 /* Unistall the object VAR. */
1922 uninstall_variable (struct varobj
*var
)
1926 struct varobj_root
*cr
;
1927 struct varobj_root
*prer
;
1929 unsigned int index
= 0;
1932 /* Remove varobj from hash table. */
1933 for (chp
= var
->obj_name
.c_str (); *chp
; chp
++)
1935 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
1938 cv
= *(varobj_table
+ index
);
1940 while (cv
!= NULL
&& cv
->var
->obj_name
!= var
->obj_name
)
1947 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
.c_str ());
1952 ("Assertion failed: Could not find variable object \"%s\" to delete",
1953 var
->obj_name
.c_str ());
1958 *(varobj_table
+ index
) = cv
->next
;
1960 prev
->next
= cv
->next
;
1964 /* If root, remove varobj from root list. */
1965 if (is_root_p (var
))
1967 /* Remove from list of root variables. */
1968 if (rootlist
== var
->root
)
1969 rootlist
= var
->root
->next
;
1974 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
1981 warning (_("Assertion failed: Could not find "
1982 "varobj \"%s\" in root list"),
1983 var
->obj_name
.c_str ());
1989 prer
->next
= cr
->next
;
1995 /* Create and install a child of the parent of the given name.
1997 The created VAROBJ takes ownership of the allocated NAME. */
1999 static struct varobj
*
2000 create_child (struct varobj
*parent
, int index
, std::string
&name
)
2002 struct varobj_item item
;
2004 std::swap (item
.name
, name
);
2005 item
.value
= value_of_child (parent
, index
);
2007 return create_child_with_value (parent
, index
, &item
);
2010 static struct varobj
*
2011 create_child_with_value (struct varobj
*parent
, int index
,
2012 struct varobj_item
*item
)
2014 struct varobj
*child
;
2016 child
= new_variable ();
2018 /* NAME is allocated by caller. */
2019 std::swap (child
->name
, item
->name
);
2020 child
->index
= index
;
2021 child
->parent
= parent
;
2022 child
->root
= parent
->root
;
2024 if (varobj_is_anonymous_child (child
))
2025 child
->obj_name
= string_printf ("%s.%d_anonymous",
2026 parent
->obj_name
.c_str (), index
);
2028 child
->obj_name
= string_printf ("%s.%s",
2029 parent
->obj_name
.c_str (),
2030 child
->name
.c_str ());
2032 install_variable (child
);
2034 /* Compute the type of the child. Must do this before
2035 calling install_new_value. */
2036 if (item
->value
!= NULL
)
2037 /* If the child had no evaluation errors, var->value
2038 will be non-NULL and contain a valid type. */
2039 child
->type
= value_actual_type (item
->value
, 0, NULL
);
2041 /* Otherwise, we must compute the type. */
2042 child
->type
= (*child
->root
->lang_ops
->type_of_child
) (child
->parent
,
2044 install_new_value (child
, item
->value
, 1);
2051 * Miscellaneous utility functions.
2054 /* Allocate memory and initialize a new variable. */
2055 static struct varobj
*
2060 var
= new varobj ();
2064 var
->num_children
= -1;
2066 var
->children
= NULL
;
2067 var
->format
= FORMAT_NATURAL
;
2071 var
->not_fetched
= 0;
2072 var
->dynamic
= XNEW (struct varobj_dynamic
);
2073 var
->dynamic
->children_requested
= 0;
2076 var
->dynamic
->constructor
= 0;
2077 var
->dynamic
->pretty_printer
= 0;
2078 var
->dynamic
->child_iter
= 0;
2079 var
->dynamic
->saved_item
= 0;
2084 /* Allocate memory and initialize a new root variable. */
2085 static struct varobj
*
2086 new_root_variable (void)
2088 struct varobj
*var
= new_variable ();
2090 var
->root
= new varobj_root ();
2091 var
->root
->lang_ops
= NULL
;
2092 var
->root
->exp
= NULL
;
2093 var
->root
->valid_block
= NULL
;
2094 var
->root
->frame
= null_frame_id
;
2095 var
->root
->floating
= 0;
2096 var
->root
->rootvar
= NULL
;
2097 var
->root
->is_valid
= 1;
2102 /* Free any allocated memory associated with VAR. */
2104 free_variable (struct varobj
*var
)
2107 if (var
->dynamic
->pretty_printer
!= NULL
)
2109 struct cleanup
*cleanup
= varobj_ensure_python_env (var
);
2111 Py_XDECREF (var
->dynamic
->constructor
);
2112 Py_XDECREF (var
->dynamic
->pretty_printer
);
2113 do_cleanups (cleanup
);
2117 varobj_iter_delete (var
->dynamic
->child_iter
);
2118 varobj_clear_saved_item (var
->dynamic
);
2119 value_free (var
->value
);
2121 if (is_root_p (var
))
2124 xfree (var
->dynamic
);
2129 do_free_variable_cleanup (void *var
)
2131 free_variable ((struct varobj
*) var
);
2134 static struct cleanup
*
2135 make_cleanup_free_variable (struct varobj
*var
)
2137 return make_cleanup (do_free_variable_cleanup
, var
);
2140 /* Return the type of the value that's stored in VAR,
2141 or that would have being stored there if the
2142 value were accessible.
2144 This differs from VAR->type in that VAR->type is always
2145 the true type of the expession in the source language.
2146 The return value of this function is the type we're
2147 actually storing in varobj, and using for displaying
2148 the values and for comparing previous and new values.
2150 For example, top-level references are always stripped. */
2152 varobj_get_value_type (const struct varobj
*var
)
2157 type
= value_type (var
->value
);
2161 type
= check_typedef (type
);
2163 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
2164 type
= get_target_type (type
);
2166 type
= check_typedef (type
);
2171 /* What is the default display for this variable? We assume that
2172 everything is "natural". Any exceptions? */
2173 static enum varobj_display_formats
2174 variable_default_display (struct varobj
*var
)
2176 return FORMAT_NATURAL
;
2180 * Language-dependencies
2183 /* Common entry points */
2185 /* Return the number of children for a given variable.
2186 The result of this function is defined by the language
2187 implementation. The number of children returned by this function
2188 is the number of children that the user will see in the variable
2191 number_of_children (const struct varobj
*var
)
2193 return (*var
->root
->lang_ops
->number_of_children
) (var
);
2196 /* What is the expression for the root varobj VAR? */
2199 name_of_variable (const struct varobj
*var
)
2201 return (*var
->root
->lang_ops
->name_of_variable
) (var
);
2204 /* What is the name of the INDEX'th child of VAR? */
2207 name_of_child (struct varobj
*var
, int index
)
2209 return (*var
->root
->lang_ops
->name_of_child
) (var
, index
);
2212 /* If frame associated with VAR can be found, switch
2213 to it and return 1. Otherwise, return 0. */
2216 check_scope (const struct varobj
*var
)
2218 struct frame_info
*fi
;
2221 fi
= frame_find_by_id (var
->root
->frame
);
2226 CORE_ADDR pc
= get_frame_pc (fi
);
2228 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
2229 pc
>= BLOCK_END (var
->root
->valid_block
))
2237 /* Helper function to value_of_root. */
2239 static struct value
*
2240 value_of_root_1 (struct varobj
**var_handle
)
2242 struct value
*new_val
= NULL
;
2243 struct varobj
*var
= *var_handle
;
2244 int within_scope
= 0;
2245 struct cleanup
*back_to
;
2247 /* Only root variables can be updated... */
2248 if (!is_root_p (var
))
2249 /* Not a root var. */
2252 back_to
= make_cleanup_restore_current_thread ();
2254 /* Determine whether the variable is still around. */
2255 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
2257 else if (var
->root
->thread_id
== 0)
2259 /* The program was single-threaded when the variable object was
2260 created. Technically, it's possible that the program became
2261 multi-threaded since then, but we don't support such
2263 within_scope
= check_scope (var
);
2267 ptid_t ptid
= global_thread_id_to_ptid (var
->root
->thread_id
);
2269 if (!ptid_equal (minus_one_ptid
, ptid
))
2271 switch_to_thread (ptid
);
2272 within_scope
= check_scope (var
);
2279 /* We need to catch errors here, because if evaluate
2280 expression fails we want to just return NULL. */
2283 new_val
= evaluate_expression (var
->root
->exp
.get ());
2285 CATCH (except
, RETURN_MASK_ERROR
)
2291 do_cleanups (back_to
);
2296 /* What is the ``struct value *'' of the root variable VAR?
2297 For floating variable object, evaluation can get us a value
2298 of different type from what is stored in varobj already. In
2300 - *type_changed will be set to 1
2301 - old varobj will be freed, and new one will be
2302 created, with the same name.
2303 - *var_handle will be set to the new varobj
2304 Otherwise, *type_changed will be set to 0. */
2305 static struct value
*
2306 value_of_root (struct varobj
**var_handle
, int *type_changed
)
2310 if (var_handle
== NULL
)
2315 /* This should really be an exception, since this should
2316 only get called with a root variable. */
2318 if (!is_root_p (var
))
2321 if (var
->root
->floating
)
2323 struct varobj
*tmp_var
;
2325 tmp_var
= varobj_create (NULL
, var
->name
.c_str (), (CORE_ADDR
) 0,
2326 USE_SELECTED_FRAME
);
2327 if (tmp_var
== NULL
)
2331 std::string old_type
= varobj_get_type (var
);
2332 std::string new_type
= varobj_get_type (tmp_var
);
2333 if (old_type
== new_type
)
2335 /* The expression presently stored inside var->root->exp
2336 remembers the locations of local variables relatively to
2337 the frame where the expression was created (in DWARF location
2338 button, for example). Naturally, those locations are not
2339 correct in other frames, so update the expression. */
2341 std::swap (var
->root
->exp
, tmp_var
->root
->exp
);
2343 varobj_delete (tmp_var
, 0);
2348 tmp_var
->obj_name
= var
->obj_name
;
2349 tmp_var
->from
= var
->from
;
2350 tmp_var
->to
= var
->to
;
2351 varobj_delete (var
, 0);
2353 install_variable (tmp_var
);
2354 *var_handle
= tmp_var
;
2365 struct value
*value
;
2367 value
= value_of_root_1 (var_handle
);
2368 if (var
->value
== NULL
|| value
== NULL
)
2370 /* For root varobj-s, a NULL value indicates a scoping issue.
2371 So, nothing to do in terms of checking for mutations. */
2373 else if (varobj_value_has_mutated (var
, value
, value_type (value
)))
2375 /* The type has mutated, so the children are no longer valid.
2376 Just delete them, and tell our caller that the type has
2378 varobj_delete (var
, 1 /* only_children */);
2379 var
->num_children
= -1;
2388 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2389 static struct value
*
2390 value_of_child (const struct varobj
*parent
, int index
)
2392 struct value
*value
;
2394 value
= (*parent
->root
->lang_ops
->value_of_child
) (parent
, index
);
2399 /* GDB already has a command called "value_of_variable". Sigh. */
2401 my_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2403 if (var
->root
->is_valid
)
2405 if (var
->dynamic
->pretty_printer
!= NULL
)
2406 return varobj_value_get_print_value (var
->value
, var
->format
, var
);
2407 return (*var
->root
->lang_ops
->value_of_variable
) (var
, format
);
2410 return std::string ();
2414 varobj_formatted_print_options (struct value_print_options
*opts
,
2415 enum varobj_display_formats format
)
2417 get_formatted_print_options (opts
, format_code
[(int) format
]);
2418 opts
->deref_ref
= 0;
2423 varobj_value_get_print_value (struct value
*value
,
2424 enum varobj_display_formats format
,
2425 const struct varobj
*var
)
2427 struct ui_file
*stb
;
2428 struct cleanup
*old_chain
;
2429 struct value_print_options opts
;
2430 struct type
*type
= NULL
;
2432 char *encoding
= NULL
;
2433 /* Initialize it just to avoid a GCC false warning. */
2434 CORE_ADDR str_addr
= 0;
2435 int string_print
= 0;
2438 return std::string ();
2440 stb
= mem_fileopen ();
2441 old_chain
= make_cleanup_ui_file_delete (stb
);
2443 std::string thevalue
;
2446 if (gdb_python_initialized
)
2448 PyObject
*value_formatter
= var
->dynamic
->pretty_printer
;
2450 varobj_ensure_python_env (var
);
2452 if (value_formatter
)
2454 /* First check to see if we have any children at all. If so,
2455 we simply return {...}. */
2456 if (dynamic_varobj_has_child_method (var
))
2458 do_cleanups (old_chain
);
2459 return xstrdup ("{...}");
2462 if (PyObject_HasAttr (value_formatter
, gdbpy_to_string_cst
))
2464 struct value
*replacement
;
2465 PyObject
*output
= NULL
;
2467 output
= apply_varobj_pretty_printer (value_formatter
,
2471 /* If we have string like output ... */
2474 make_cleanup_py_decref (output
);
2476 /* If this is a lazy string, extract it. For lazy
2477 strings we always print as a string, so set
2479 if (gdbpy_is_lazy_string (output
))
2481 gdbpy_extract_lazy_string (output
, &str_addr
, &type
,
2483 make_cleanup (free_current_contents
, &encoding
);
2488 /* If it is a regular (non-lazy) string, extract
2489 it and copy the contents into THEVALUE. If the
2490 hint says to print it as a string, set
2491 string_print. Otherwise just return the extracted
2492 string as a value. */
2494 gdb::unique_xmalloc_ptr
<char> s
2495 = python_string_to_target_string (output
);
2499 struct gdbarch
*gdbarch
;
2501 gdb::unique_xmalloc_ptr
<char> hint
2502 = gdbpy_get_display_hint (value_formatter
);
2505 if (!strcmp (hint
.get (), "string"))
2509 thevalue
= std::string (s
.get ());
2510 len
= thevalue
.size ();
2511 gdbarch
= get_type_arch (value_type (value
));
2512 type
= builtin_type (gdbarch
)->builtin_char
;
2516 do_cleanups (old_chain
);
2521 gdbpy_print_stack ();
2524 /* If the printer returned a replacement value, set VALUE
2525 to REPLACEMENT. If there is not a replacement value,
2526 just use the value passed to this function. */
2528 value
= replacement
;
2534 varobj_formatted_print_options (&opts
, format
);
2536 /* If the THEVALUE has contents, it is a regular string. */
2537 if (!thevalue
.empty ())
2538 LA_PRINT_STRING (stb
, type
, (gdb_byte
*) thevalue
.c_str (),
2539 len
, encoding
, 0, &opts
);
2540 else if (string_print
)
2541 /* Otherwise, if string_print is set, and it is not a regular
2542 string, it is a lazy string. */
2543 val_print_string (type
, encoding
, str_addr
, len
, stb
, &opts
);
2545 /* All other cases. */
2546 common_val_print (value
, stb
, 0, &opts
, current_language
);
2548 thevalue
= ui_file_as_string (stb
);
2550 do_cleanups (old_chain
);
2555 varobj_editable_p (const struct varobj
*var
)
2559 if (!(var
->root
->is_valid
&& var
->value
&& VALUE_LVAL (var
->value
)))
2562 type
= varobj_get_value_type (var
);
2564 switch (TYPE_CODE (type
))
2566 case TYPE_CODE_STRUCT
:
2567 case TYPE_CODE_UNION
:
2568 case TYPE_CODE_ARRAY
:
2569 case TYPE_CODE_FUNC
:
2570 case TYPE_CODE_METHOD
:
2580 /* Call VAR's value_is_changeable_p language-specific callback. */
2583 varobj_value_is_changeable_p (const struct varobj
*var
)
2585 return var
->root
->lang_ops
->value_is_changeable_p (var
);
2588 /* Return 1 if that varobj is floating, that is is always evaluated in the
2589 selected frame, and not bound to thread/frame. Such variable objects
2590 are created using '@' as frame specifier to -var-create. */
2592 varobj_floating_p (const struct varobj
*var
)
2594 return var
->root
->floating
;
2597 /* Implement the "value_is_changeable_p" varobj callback for most
2601 varobj_default_value_is_changeable_p (const struct varobj
*var
)
2606 if (CPLUS_FAKE_CHILD (var
))
2609 type
= varobj_get_value_type (var
);
2611 switch (TYPE_CODE (type
))
2613 case TYPE_CODE_STRUCT
:
2614 case TYPE_CODE_UNION
:
2615 case TYPE_CODE_ARRAY
:
2626 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
2627 with an arbitrary caller supplied DATA pointer. */
2630 all_root_varobjs (void (*func
) (struct varobj
*var
, void *data
), void *data
)
2632 struct varobj_root
*var_root
, *var_root_next
;
2634 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
2636 for (var_root
= rootlist
; var_root
!= NULL
; var_root
= var_root_next
)
2638 var_root_next
= var_root
->next
;
2640 (*func
) (var_root
->rootvar
, data
);
2644 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
2645 defined on globals. It is a helper for varobj_invalidate.
2647 This function is called after changing the symbol file, in this case the
2648 pointers to "struct type" stored by the varobj are no longer valid. All
2649 varobj must be either re-evaluated, or marked as invalid here. */
2652 varobj_invalidate_iter (struct varobj
*var
, void *unused
)
2654 /* global and floating var must be re-evaluated. */
2655 if (var
->root
->floating
|| var
->root
->valid_block
== NULL
)
2657 struct varobj
*tmp_var
;
2659 /* Try to create a varobj with same expression. If we succeed
2660 replace the old varobj, otherwise invalidate it. */
2661 tmp_var
= varobj_create (NULL
, var
->name
.c_str (), (CORE_ADDR
) 0,
2663 if (tmp_var
!= NULL
)
2665 tmp_var
->obj_name
= var
->obj_name
;
2666 varobj_delete (var
, 0);
2667 install_variable (tmp_var
);
2670 var
->root
->is_valid
= 0;
2672 else /* locals must be invalidated. */
2673 var
->root
->is_valid
= 0;
2676 /* Invalidate the varobjs that are tied to locals and re-create the ones that
2677 are defined on globals.
2678 Invalidated varobjs will be always printed in_scope="invalid". */
2681 varobj_invalidate (void)
2683 all_root_varobjs (varobj_invalidate_iter
, NULL
);
2686 extern void _initialize_varobj (void);
2688 _initialize_varobj (void)
2690 varobj_table
= XCNEWVEC (struct vlist
*, VAROBJ_TABLE_SIZE
);
2692 add_setshow_zuinteger_cmd ("varobj", class_maintenance
,
2694 _("Set varobj debugging."),
2695 _("Show varobj debugging."),
2696 _("When non-zero, varobj debugging is enabled."),
2697 NULL
, show_varobjdebug
,
2698 &setdebuglist
, &showdebuglist
);