1 /* Implementation of the GDB variable objects API.
3 Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "exceptions.h"
21 #include "expression.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include "gdb_regex.h"
34 #include "gdbthread.h"
36 #include "ada-varobj.h"
40 #include "python/python.h"
41 #include "python/python-internal.h"
46 /* The names of varobjs representing anonymous structs or unions. */
47 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
48 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
50 /* Non-zero if we want to see trace of varobj level stuff. */
52 unsigned int varobjdebug
= 0;
54 show_varobjdebug (struct ui_file
*file
, int from_tty
,
55 struct cmd_list_element
*c
, const char *value
)
57 fprintf_filtered (file
, _("Varobj debugging is %s.\n"), value
);
60 /* String representations of gdb's format codes. */
61 char *varobj_format_string
[] =
62 { "natural", "binary", "decimal", "hexadecimal", "octal" };
64 /* String representations of gdb's known languages. */
65 char *varobj_language_string
[] = { "C", "C++", "Java" };
67 /* True if we want to allow Python-based pretty-printing. */
68 static int pretty_printing
= 0;
71 varobj_enable_pretty_printing (void)
78 /* Every root variable has one of these structures saved in its
79 varobj. Members which must be free'd are noted. */
83 /* Alloc'd expression for this parent. */
84 struct expression
*exp
;
86 /* Block for which this expression is valid. */
87 const struct block
*valid_block
;
89 /* The frame for this expression. This field is set iff valid_block is
91 struct frame_id frame
;
93 /* The thread ID that this varobj_root belong to. This field
94 is only valid if valid_block is not NULL.
95 When not 0, indicates which thread 'frame' belongs to.
96 When 0, indicates that the thread list was empty when the varobj_root
100 /* If 1, the -var-update always recomputes the value in the
101 current thread and frame. Otherwise, variable object is
102 always updated in the specific scope/thread/frame. */
105 /* Flag that indicates validity: set to 0 when this varobj_root refers
106 to symbols that do not exist anymore. */
109 /* Language info for this variable and its children. */
110 struct language_specific
*lang
;
112 /* The varobj for this root node. */
113 struct varobj
*rootvar
;
115 /* Next root variable */
116 struct varobj_root
*next
;
119 /* Every variable in the system has a structure of this type defined
120 for it. This structure holds all information necessary to manipulate
121 a particular object variable. Members which must be freed are noted. */
125 /* Alloc'd name of the variable for this object. If this variable is a
126 child, then this name will be the child's source name.
127 (bar, not foo.bar). */
128 /* NOTE: This is the "expression". */
131 /* Alloc'd expression for this child. Can be used to create a
132 root variable corresponding to this child. */
135 /* The alloc'd name for this variable's object. This is here for
136 convenience when constructing this object's children. */
139 /* Index of this variable in its parent or -1. */
142 /* The type of this variable. This can be NULL
143 for artifial variable objects -- currently, the "accessibility"
144 variable objects in C++. */
147 /* The value of this expression or subexpression. A NULL value
148 indicates there was an error getting this value.
149 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
150 the value is either NULL, or not lazy. */
153 /* The number of (immediate) children this variable has. */
156 /* If this object is a child, this points to its immediate parent. */
157 struct varobj
*parent
;
159 /* Children of this object. */
160 VEC (varobj_p
) *children
;
162 /* Whether the children of this varobj were requested. This field is
163 used to decide if dynamic varobj should recompute their children.
164 In the event that the frontend never asked for the children, we
166 int children_requested
;
168 /* Description of the root variable. Points to root variable for
170 struct varobj_root
*root
;
172 /* The format of the output for this object. */
173 enum varobj_display_formats format
;
175 /* Was this variable updated via a varobj_set_value operation. */
178 /* Last print value. */
181 /* Is this variable frozen. Frozen variables are never implicitly
182 updated by -var-update *
183 or -var-update <direct-or-indirect-parent>. */
186 /* Is the value of this variable intentionally not fetched? It is
187 not fetched if either the variable is frozen, or any parents is
191 /* Sub-range of children which the MI consumer has requested. If
192 FROM < 0 or TO < 0, means that all children have been
197 /* The pretty-printer constructor. If NULL, then the default
198 pretty-printer will be looked up. If None, then no
199 pretty-printer will be installed. */
200 PyObject
*constructor
;
202 /* The pretty-printer that has been constructed. If NULL, then a
203 new printer object is needed, and one will be constructed. */
204 PyObject
*pretty_printer
;
206 /* The iterator returned by the printer's 'children' method, or NULL
208 PyObject
*child_iter
;
210 /* We request one extra item from the iterator, so that we can
211 report to the caller whether there are more items than we have
212 already reported. However, we don't want to install this value
213 when we read it, because that will mess up future updates. So,
214 we stash it here instead. */
215 PyObject
*saved_item
;
221 struct cpstack
*next
;
224 /* A list of varobjs */
232 /* Private function prototypes */
234 /* Helper functions for the above subcommands. */
236 static int delete_variable (struct cpstack
**, struct varobj
*, int);
238 static void delete_variable_1 (struct cpstack
**, int *,
239 struct varobj
*, int, int);
241 static int install_variable (struct varobj
*);
243 static void uninstall_variable (struct varobj
*);
245 static struct varobj
*create_child (struct varobj
*, int, char *);
247 static struct varobj
*
248 create_child_with_value (struct varobj
*parent
, int index
, const char *name
,
249 struct value
*value
);
251 /* Utility routines */
253 static struct varobj
*new_variable (void);
255 static struct varobj
*new_root_variable (void);
257 static void free_variable (struct varobj
*var
);
259 static struct cleanup
*make_cleanup_free_variable (struct varobj
*var
);
261 static struct type
*get_type (struct varobj
*var
);
263 static struct type
*get_value_type (struct varobj
*var
);
265 static struct type
*get_target_type (struct type
*);
267 static enum varobj_display_formats
variable_default_display (struct varobj
*);
269 static void cppush (struct cpstack
**pstack
, char *name
);
271 static char *cppop (struct cpstack
**pstack
);
273 static int update_type_if_necessary (struct varobj
*var
,
274 struct value
*new_value
);
276 static int install_new_value (struct varobj
*var
, struct value
*value
,
279 /* Language-specific routines. */
281 static enum varobj_languages
variable_language (struct varobj
*var
);
283 static int number_of_children (struct varobj
*);
285 static char *name_of_variable (struct varobj
*);
287 static char *name_of_child (struct varobj
*, int);
289 static struct value
*value_of_root (struct varobj
**var_handle
, int *);
291 static struct value
*value_of_child (struct varobj
*parent
, int index
);
293 static char *my_value_of_variable (struct varobj
*var
,
294 enum varobj_display_formats format
);
296 static char *value_get_print_value (struct value
*value
,
297 enum varobj_display_formats format
,
300 static int varobj_value_is_changeable_p (struct varobj
*var
);
302 static int is_root_p (struct varobj
*var
);
306 static struct varobj
*varobj_add_child (struct varobj
*var
,
308 struct value
*value
);
310 #endif /* HAVE_PYTHON */
312 static int default_value_is_changeable_p (struct varobj
*var
);
314 /* C implementation */
316 static int c_number_of_children (struct varobj
*var
);
318 static char *c_name_of_variable (struct varobj
*parent
);
320 static char *c_name_of_child (struct varobj
*parent
, int index
);
322 static char *c_path_expr_of_child (struct varobj
*child
);
324 static struct value
*c_value_of_child (struct varobj
*parent
, int index
);
326 static struct type
*c_type_of_child (struct varobj
*parent
, int index
);
328 static char *c_value_of_variable (struct varobj
*var
,
329 enum varobj_display_formats format
);
331 /* C++ implementation */
333 static int cplus_number_of_children (struct varobj
*var
);
335 static void cplus_class_num_children (struct type
*type
, int children
[3]);
337 static char *cplus_name_of_variable (struct varobj
*parent
);
339 static char *cplus_name_of_child (struct varobj
*parent
, int index
);
341 static char *cplus_path_expr_of_child (struct varobj
*child
);
343 static struct value
*cplus_value_of_child (struct varobj
*parent
, int index
);
345 static struct type
*cplus_type_of_child (struct varobj
*parent
, int index
);
347 static char *cplus_value_of_variable (struct varobj
*var
,
348 enum varobj_display_formats format
);
350 /* Java implementation */
352 static int java_number_of_children (struct varobj
*var
);
354 static char *java_name_of_variable (struct varobj
*parent
);
356 static char *java_name_of_child (struct varobj
*parent
, int index
);
358 static char *java_path_expr_of_child (struct varobj
*child
);
360 static struct value
*java_value_of_child (struct varobj
*parent
, int index
);
362 static struct type
*java_type_of_child (struct varobj
*parent
, int index
);
364 static char *java_value_of_variable (struct varobj
*var
,
365 enum varobj_display_formats format
);
367 /* Ada implementation */
369 static int ada_number_of_children (struct varobj
*var
);
371 static char *ada_name_of_variable (struct varobj
*parent
);
373 static char *ada_name_of_child (struct varobj
*parent
, int index
);
375 static char *ada_path_expr_of_child (struct varobj
*child
);
377 static struct value
*ada_value_of_child (struct varobj
*parent
, int index
);
379 static struct type
*ada_type_of_child (struct varobj
*parent
, int index
);
381 static char *ada_value_of_variable (struct varobj
*var
,
382 enum varobj_display_formats format
);
384 static int ada_value_is_changeable_p (struct varobj
*var
);
386 static int ada_value_has_mutated (struct varobj
*var
, struct value
*new_val
,
387 struct type
*new_type
);
389 /* The language specific vector */
391 struct language_specific
393 /* The number of children of PARENT. */
394 int (*number_of_children
) (struct varobj
* parent
);
396 /* The name (expression) of a root varobj. */
397 char *(*name_of_variable
) (struct varobj
* parent
);
399 /* The name of the INDEX'th child of PARENT. */
400 char *(*name_of_child
) (struct varobj
* parent
, int index
);
402 /* Returns the rooted expression of CHILD, which is a variable
403 obtain that has some parent. */
404 char *(*path_expr_of_child
) (struct varobj
* child
);
406 /* The ``struct value *'' of the INDEX'th child of PARENT. */
407 struct value
*(*value_of_child
) (struct varobj
* parent
, int index
);
409 /* The type of the INDEX'th child of PARENT. */
410 struct type
*(*type_of_child
) (struct varobj
* parent
, int index
);
412 /* The current value of VAR. */
413 char *(*value_of_variable
) (struct varobj
* var
,
414 enum varobj_display_formats format
);
416 /* Return non-zero if changes in value of VAR must be detected and
417 reported by -var-update. Return zero if -var-update should never
418 report changes of such values. This makes sense for structures
419 (since the changes in children values will be reported separately),
420 or for artifical objects (like 'public' pseudo-field in C++).
422 Return value of 0 means that gdb need not call value_fetch_lazy
423 for the value of this variable object. */
424 int (*value_is_changeable_p
) (struct varobj
*var
);
426 /* Return nonzero if the type of VAR has mutated.
428 VAR's value is still the varobj's previous value, while NEW_VALUE
429 is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE
430 may be NULL indicating that there is no value available (the varobj
431 may be out of scope, of may be the child of a null pointer, for
432 instance). NEW_TYPE, on the other hand, must never be NULL.
434 This function should also be able to assume that var's number of
435 children is set (not < 0).
437 Languages where types do not mutate can set this to NULL. */
438 int (*value_has_mutated
) (struct varobj
*var
, struct value
*new_value
,
439 struct type
*new_type
);
442 /* Array of known source language routines. */
443 static struct language_specific languages
[vlang_end
] = {
446 c_number_of_children
,
449 c_path_expr_of_child
,
453 default_value_is_changeable_p
,
454 NULL
/* value_has_mutated */}
458 cplus_number_of_children
,
459 cplus_name_of_variable
,
461 cplus_path_expr_of_child
,
462 cplus_value_of_child
,
464 cplus_value_of_variable
,
465 default_value_is_changeable_p
,
466 NULL
/* value_has_mutated */}
470 java_number_of_children
,
471 java_name_of_variable
,
473 java_path_expr_of_child
,
476 java_value_of_variable
,
477 default_value_is_changeable_p
,
478 NULL
/* value_has_mutated */},
481 ada_number_of_children
,
482 ada_name_of_variable
,
484 ada_path_expr_of_child
,
487 ada_value_of_variable
,
488 ada_value_is_changeable_p
,
489 ada_value_has_mutated
}
492 /* A little convenience enum for dealing with C++/Java. */
495 v_public
= 0, v_private
, v_protected
500 /* Mappings of varobj_display_formats enums to gdb's format codes. */
501 static int format_code
[] = { 0, 't', 'd', 'x', 'o' };
503 /* Header of the list of root variable objects. */
504 static struct varobj_root
*rootlist
;
506 /* Prime number indicating the number of buckets in the hash table. */
507 /* A prime large enough to avoid too many colisions. */
508 #define VAROBJ_TABLE_SIZE 227
510 /* Pointer to the varobj hash table (built at run time). */
511 static struct vlist
**varobj_table
;
513 /* Is the variable X one of our "fake" children? */
514 #define CPLUS_FAKE_CHILD(x) \
515 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
518 /* API Implementation */
520 is_root_p (struct varobj
*var
)
522 return (var
->root
->rootvar
== var
);
526 /* Helper function to install a Python environment suitable for
527 use during operations on VAR. */
528 static struct cleanup
*
529 varobj_ensure_python_env (struct varobj
*var
)
531 return ensure_python_env (var
->root
->exp
->gdbarch
,
532 var
->root
->exp
->language_defn
);
536 /* Creates a varobj (not its children). */
538 /* Return the full FRAME which corresponds to the given CORE_ADDR
539 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
541 static struct frame_info
*
542 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr
)
544 struct frame_info
*frame
= NULL
;
546 if (frame_addr
== (CORE_ADDR
) 0)
549 for (frame
= get_current_frame ();
551 frame
= get_prev_frame (frame
))
553 /* The CORE_ADDR we get as argument was parsed from a string GDB
554 output as $fp. This output got truncated to gdbarch_addr_bit.
555 Truncate the frame base address in the same manner before
556 comparing it against our argument. */
557 CORE_ADDR frame_base
= get_frame_base_address (frame
);
558 int addr_bit
= gdbarch_addr_bit (get_frame_arch (frame
));
560 if (addr_bit
< (sizeof (CORE_ADDR
) * HOST_CHAR_BIT
))
561 frame_base
&= ((CORE_ADDR
) 1 << addr_bit
) - 1;
563 if (frame_base
== frame_addr
)
571 varobj_create (char *objname
,
572 char *expression
, CORE_ADDR frame
, enum varobj_type type
)
575 struct cleanup
*old_chain
;
577 /* Fill out a varobj structure for the (root) variable being constructed. */
578 var
= new_root_variable ();
579 old_chain
= make_cleanup_free_variable (var
);
581 if (expression
!= NULL
)
583 struct frame_info
*fi
;
584 struct frame_id old_id
= null_frame_id
;
587 enum varobj_languages lang
;
588 struct value
*value
= NULL
;
589 volatile struct gdb_exception except
;
592 /* Parse and evaluate the expression, filling in as much of the
593 variable's data as possible. */
595 if (has_stack_frames ())
597 /* Allow creator to specify context of variable. */
598 if ((type
== USE_CURRENT_FRAME
) || (type
== USE_SELECTED_FRAME
))
599 fi
= get_selected_frame (NULL
);
601 /* FIXME: cagney/2002-11-23: This code should be doing a
602 lookup using the frame ID and not just the frame's
603 ``address''. This, of course, means an interface
604 change. However, with out that interface change ISAs,
605 such as the ia64 with its two stacks, won't work.
606 Similar goes for the case where there is a frameless
608 fi
= find_frame_addr_in_frame_chain (frame
);
613 /* frame = -2 means always use selected frame. */
614 if (type
== USE_SELECTED_FRAME
)
615 var
->root
->floating
= 1;
621 block
= get_frame_block (fi
, 0);
622 pc
= get_frame_pc (fi
);
626 innermost_block
= NULL
;
627 /* Wrap the call to parse expression, so we can
628 return a sensible error. */
629 TRY_CATCH (except
, RETURN_MASK_ERROR
)
631 var
->root
->exp
= parse_exp_1 (&p
, pc
, block
, 0);
634 if (except
.reason
< 0)
636 do_cleanups (old_chain
);
640 /* Don't allow variables to be created for types. */
641 if (var
->root
->exp
->elts
[0].opcode
== OP_TYPE
642 || var
->root
->exp
->elts
[0].opcode
== OP_TYPEOF
643 || var
->root
->exp
->elts
[0].opcode
== OP_DECLTYPE
)
645 do_cleanups (old_chain
);
646 fprintf_unfiltered (gdb_stderr
, "Attempt to use a type name"
647 " as an expression.\n");
651 var
->format
= variable_default_display (var
);
652 var
->root
->valid_block
= innermost_block
;
653 var
->name
= xstrdup (expression
);
654 /* For a root var, the name and the expr are the same. */
655 var
->path_expr
= xstrdup (expression
);
657 /* When the frame is different from the current frame,
658 we must select the appropriate frame before parsing
659 the expression, otherwise the value will not be current.
660 Since select_frame is so benign, just call it for all cases. */
663 /* User could specify explicit FRAME-ADDR which was not found but
664 EXPRESSION is frame specific and we would not be able to evaluate
665 it correctly next time. With VALID_BLOCK set we must also set
666 FRAME and THREAD_ID. */
668 error (_("Failed to find the specified frame"));
670 var
->root
->frame
= get_frame_id (fi
);
671 var
->root
->thread_id
= pid_to_thread_id (inferior_ptid
);
672 old_id
= get_frame_id (get_selected_frame (NULL
));
676 /* We definitely need to catch errors here.
677 If evaluate_expression succeeds we got the value we wanted.
678 But if it fails, we still go on with a call to evaluate_type(). */
679 TRY_CATCH (except
, RETURN_MASK_ERROR
)
681 value
= evaluate_expression (var
->root
->exp
);
684 if (except
.reason
< 0)
686 /* Error getting the value. Try to at least get the
688 struct value
*type_only_value
= evaluate_type (var
->root
->exp
);
690 var
->type
= value_type (type_only_value
);
694 int real_type_found
= 0;
696 var
->type
= value_actual_type (value
, 0, &real_type_found
);
698 value
= value_cast (var
->type
, value
);
701 /* Set language info */
702 lang
= variable_language (var
);
703 var
->root
->lang
= &languages
[lang
];
705 install_new_value (var
, value
, 1 /* Initial assignment */);
707 /* Set ourselves as our root. */
708 var
->root
->rootvar
= var
;
710 /* Reset the selected frame. */
711 if (frame_id_p (old_id
))
712 select_frame (frame_find_by_id (old_id
));
715 /* If the variable object name is null, that means this
716 is a temporary variable, so don't install it. */
718 if ((var
!= NULL
) && (objname
!= NULL
))
720 var
->obj_name
= xstrdup (objname
);
722 /* If a varobj name is duplicated, the install will fail so
724 if (!install_variable (var
))
726 do_cleanups (old_chain
);
731 discard_cleanups (old_chain
);
735 /* Generates an unique name that can be used for a varobj. */
738 varobj_gen_name (void)
743 /* Generate a name for this object. */
745 obj_name
= xstrprintf ("var%d", id
);
750 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
751 error if OBJNAME cannot be found. */
754 varobj_get_handle (char *objname
)
758 unsigned int index
= 0;
761 for (chp
= objname
; *chp
; chp
++)
763 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
766 cv
= *(varobj_table
+ index
);
767 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, objname
) != 0))
771 error (_("Variable object not found"));
776 /* Given the handle, return the name of the object. */
779 varobj_get_objname (struct varobj
*var
)
781 return var
->obj_name
;
784 /* Given the handle, return the expression represented by the object. */
787 varobj_get_expression (struct varobj
*var
)
789 return name_of_variable (var
);
792 /* Deletes a varobj and all its children if only_children == 0,
793 otherwise deletes only the children; returns a malloc'ed list of
794 all the (malloc'ed) names of the variables that have been deleted
795 (NULL terminated). */
798 varobj_delete (struct varobj
*var
, char ***dellist
, int only_children
)
802 struct cpstack
*result
= NULL
;
805 /* Initialize a stack for temporary results. */
806 cppush (&result
, NULL
);
809 /* Delete only the variable children. */
810 delcount
= delete_variable (&result
, var
, 1 /* only the children */ );
812 /* Delete the variable and all its children. */
813 delcount
= delete_variable (&result
, var
, 0 /* parent+children */ );
815 /* We may have been asked to return a list of what has been deleted. */
818 *dellist
= xmalloc ((delcount
+ 1) * sizeof (char *));
822 *cp
= cppop (&result
);
823 while ((*cp
!= NULL
) && (mycount
> 0))
827 *cp
= cppop (&result
);
830 if (mycount
|| (*cp
!= NULL
))
831 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
840 /* Convenience function for varobj_set_visualizer. Instantiate a
841 pretty-printer for a given value. */
843 instantiate_pretty_printer (PyObject
*constructor
, struct value
*value
)
845 PyObject
*val_obj
= NULL
;
848 val_obj
= value_to_value_object (value
);
852 printer
= PyObject_CallFunctionObjArgs (constructor
, val_obj
, NULL
);
859 /* Set/Get variable object display format. */
861 enum varobj_display_formats
862 varobj_set_display_format (struct varobj
*var
,
863 enum varobj_display_formats format
)
870 case FORMAT_HEXADECIMAL
:
872 var
->format
= format
;
876 var
->format
= variable_default_display (var
);
879 if (varobj_value_is_changeable_p (var
)
880 && var
->value
&& !value_lazy (var
->value
))
882 xfree (var
->print_value
);
883 var
->print_value
= value_get_print_value (var
->value
, var
->format
, var
);
889 enum varobj_display_formats
890 varobj_get_display_format (struct varobj
*var
)
896 varobj_get_display_hint (struct varobj
*var
)
901 struct cleanup
*back_to
;
903 if (!gdb_python_initialized
)
906 back_to
= varobj_ensure_python_env (var
);
908 if (var
->pretty_printer
)
909 result
= gdbpy_get_display_hint (var
->pretty_printer
);
911 do_cleanups (back_to
);
917 /* Return true if the varobj has items after TO, false otherwise. */
920 varobj_has_more (struct varobj
*var
, int to
)
922 if (VEC_length (varobj_p
, var
->children
) > to
)
924 return ((to
== -1 || VEC_length (varobj_p
, var
->children
) == to
)
925 && var
->saved_item
!= NULL
);
928 /* If the variable object is bound to a specific thread, that
929 is its evaluation can always be done in context of a frame
930 inside that thread, returns GDB id of the thread -- which
931 is always positive. Otherwise, returns -1. */
933 varobj_get_thread_id (struct varobj
*var
)
935 if (var
->root
->valid_block
&& var
->root
->thread_id
> 0)
936 return var
->root
->thread_id
;
942 varobj_set_frozen (struct varobj
*var
, int frozen
)
944 /* When a variable is unfrozen, we don't fetch its value.
945 The 'not_fetched' flag remains set, so next -var-update
948 We don't fetch the value, because for structures the client
949 should do -var-update anyway. It would be bad to have different
950 client-size logic for structure and other types. */
951 var
->frozen
= frozen
;
955 varobj_get_frozen (struct varobj
*var
)
960 /* A helper function that restricts a range to what is actually
961 available in a VEC. This follows the usual rules for the meaning
962 of FROM and TO -- if either is negative, the entire range is
966 restrict_range (VEC (varobj_p
) *children
, int *from
, int *to
)
968 if (*from
< 0 || *to
< 0)
971 *to
= VEC_length (varobj_p
, children
);
975 if (*from
> VEC_length (varobj_p
, children
))
976 *from
= VEC_length (varobj_p
, children
);
977 if (*to
> VEC_length (varobj_p
, children
))
978 *to
= VEC_length (varobj_p
, children
);
986 /* A helper for update_dynamic_varobj_children that installs a new
987 child when needed. */
990 install_dynamic_child (struct varobj
*var
,
991 VEC (varobj_p
) **changed
,
992 VEC (varobj_p
) **type_changed
,
993 VEC (varobj_p
) **new,
994 VEC (varobj_p
) **unchanged
,
1000 if (VEC_length (varobj_p
, var
->children
) < index
+ 1)
1002 /* There's no child yet. */
1003 struct varobj
*child
= varobj_add_child (var
, name
, value
);
1007 VEC_safe_push (varobj_p
, *new, child
);
1013 varobj_p existing
= VEC_index (varobj_p
, var
->children
, index
);
1014 int type_updated
= update_type_if_necessary (existing
, value
);
1019 VEC_safe_push (varobj_p
, *type_changed
, existing
);
1021 if (install_new_value (existing
, value
, 0))
1023 if (!type_updated
&& changed
)
1024 VEC_safe_push (varobj_p
, *changed
, existing
);
1026 else if (!type_updated
&& unchanged
)
1027 VEC_safe_push (varobj_p
, *unchanged
, existing
);
1032 dynamic_varobj_has_child_method (struct varobj
*var
)
1034 struct cleanup
*back_to
;
1035 PyObject
*printer
= var
->pretty_printer
;
1038 if (!gdb_python_initialized
)
1041 back_to
= varobj_ensure_python_env (var
);
1042 result
= PyObject_HasAttr (printer
, gdbpy_children_cst
);
1043 do_cleanups (back_to
);
1050 update_dynamic_varobj_children (struct varobj
*var
,
1051 VEC (varobj_p
) **changed
,
1052 VEC (varobj_p
) **type_changed
,
1053 VEC (varobj_p
) **new,
1054 VEC (varobj_p
) **unchanged
,
1056 int update_children
,
1061 struct cleanup
*back_to
;
1064 PyObject
*printer
= var
->pretty_printer
;
1066 if (!gdb_python_initialized
)
1069 back_to
= varobj_ensure_python_env (var
);
1072 if (!PyObject_HasAttr (printer
, gdbpy_children_cst
))
1074 do_cleanups (back_to
);
1078 if (update_children
|| !var
->child_iter
)
1080 children
= PyObject_CallMethodObjArgs (printer
, gdbpy_children_cst
,
1085 gdbpy_print_stack ();
1086 error (_("Null value returned for children"));
1089 make_cleanup_py_decref (children
);
1091 Py_XDECREF (var
->child_iter
);
1092 var
->child_iter
= PyObject_GetIter (children
);
1093 if (!var
->child_iter
)
1095 gdbpy_print_stack ();
1096 error (_("Could not get children iterator"));
1099 Py_XDECREF (var
->saved_item
);
1100 var
->saved_item
= NULL
;
1105 i
= VEC_length (varobj_p
, var
->children
);
1107 /* We ask for one extra child, so that MI can report whether there
1108 are more children. */
1109 for (; to
< 0 || i
< to
+ 1; ++i
)
1114 /* See if there was a leftover from last time. */
1115 if (var
->saved_item
)
1117 item
= var
->saved_item
;
1118 var
->saved_item
= NULL
;
1121 item
= PyIter_Next (var
->child_iter
);
1125 /* Normal end of iteration. */
1126 if (!PyErr_Occurred ())
1129 /* If we got a memory error, just use the text as the
1131 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error
))
1133 PyObject
*type
, *value
, *trace
;
1134 char *name_str
, *value_str
;
1136 PyErr_Fetch (&type
, &value
, &trace
);
1137 value_str
= gdbpy_exception_to_string (type
, value
);
1143 gdbpy_print_stack ();
1147 name_str
= xstrprintf ("<error at %d>", i
);
1148 item
= Py_BuildValue ("(ss)", name_str
, value_str
);
1153 gdbpy_print_stack ();
1161 /* Any other kind of error. */
1162 gdbpy_print_stack ();
1167 /* We don't want to push the extra child on any report list. */
1168 if (to
< 0 || i
< to
)
1173 struct cleanup
*inner
;
1174 int can_mention
= from
< 0 || i
>= from
;
1176 inner
= make_cleanup_py_decref (item
);
1178 if (!PyArg_ParseTuple (item
, "sO", &name
, &py_v
))
1180 gdbpy_print_stack ();
1181 error (_("Invalid item from the child list"));
1184 v
= convert_value_from_python (py_v
);
1186 gdbpy_print_stack ();
1187 install_dynamic_child (var
, can_mention
? changed
: NULL
,
1188 can_mention
? type_changed
: NULL
,
1189 can_mention
? new : NULL
,
1190 can_mention
? unchanged
: NULL
,
1191 can_mention
? cchanged
: NULL
, i
, name
, v
);
1192 do_cleanups (inner
);
1196 Py_XDECREF (var
->saved_item
);
1197 var
->saved_item
= item
;
1199 /* We want to truncate the child list just before this
1208 if (i
< VEC_length (varobj_p
, var
->children
))
1213 for (j
= i
; j
< VEC_length (varobj_p
, var
->children
); ++j
)
1214 varobj_delete (VEC_index (varobj_p
, var
->children
, j
), NULL
, 0);
1215 VEC_truncate (varobj_p
, var
->children
, i
);
1218 /* If there are fewer children than requested, note that the list of
1219 children changed. */
1220 if (to
>= 0 && VEC_length (varobj_p
, var
->children
) < to
)
1223 var
->num_children
= VEC_length (varobj_p
, var
->children
);
1225 do_cleanups (back_to
);
1229 gdb_assert_not_reached ("should never be called if Python is not enabled");
1234 varobj_get_num_children (struct varobj
*var
)
1236 if (var
->num_children
== -1)
1238 if (var
->pretty_printer
)
1242 /* If we have a dynamic varobj, don't report -1 children.
1243 So, try to fetch some children first. */
1244 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
, &dummy
,
1248 var
->num_children
= number_of_children (var
);
1251 return var
->num_children
>= 0 ? var
->num_children
: 0;
1254 /* Creates a list of the immediate children of a variable object;
1255 the return code is the number of such children or -1 on error. */
1258 varobj_list_children (struct varobj
*var
, int *from
, int *to
)
1261 int i
, children_changed
;
1263 var
->children_requested
= 1;
1265 if (var
->pretty_printer
)
1267 /* This, in theory, can result in the number of children changing without
1268 frontend noticing. But well, calling -var-list-children on the same
1269 varobj twice is not something a sane frontend would do. */
1270 update_dynamic_varobj_children (var
, NULL
, NULL
, NULL
, NULL
,
1271 &children_changed
, 0, 0, *to
);
1272 restrict_range (var
->children
, from
, to
);
1273 return var
->children
;
1276 if (var
->num_children
== -1)
1277 var
->num_children
= number_of_children (var
);
1279 /* If that failed, give up. */
1280 if (var
->num_children
== -1)
1281 return var
->children
;
1283 /* If we're called when the list of children is not yet initialized,
1284 allocate enough elements in it. */
1285 while (VEC_length (varobj_p
, var
->children
) < var
->num_children
)
1286 VEC_safe_push (varobj_p
, var
->children
, NULL
);
1288 for (i
= 0; i
< var
->num_children
; i
++)
1290 varobj_p existing
= VEC_index (varobj_p
, var
->children
, i
);
1292 if (existing
== NULL
)
1294 /* Either it's the first call to varobj_list_children for
1295 this variable object, and the child was never created,
1296 or it was explicitly deleted by the client. */
1297 name
= name_of_child (var
, i
);
1298 existing
= create_child (var
, i
, name
);
1299 VEC_replace (varobj_p
, var
->children
, i
, existing
);
1303 restrict_range (var
->children
, from
, to
);
1304 return var
->children
;
1309 static struct varobj
*
1310 varobj_add_child (struct varobj
*var
, const char *name
, struct value
*value
)
1312 varobj_p v
= create_child_with_value (var
,
1313 VEC_length (varobj_p
, var
->children
),
1316 VEC_safe_push (varobj_p
, var
->children
, v
);
1320 #endif /* HAVE_PYTHON */
1322 /* Obtain the type of an object Variable as a string similar to the one gdb
1323 prints on the console. */
1326 varobj_get_type (struct varobj
*var
)
1328 /* For the "fake" variables, do not return a type. (It's type is
1330 Do not return a type for invalid variables as well. */
1331 if (CPLUS_FAKE_CHILD (var
) || !var
->root
->is_valid
)
1334 return type_to_string (var
->type
);
1337 /* Obtain the type of an object variable. */
1340 varobj_get_gdb_type (struct varobj
*var
)
1345 /* Is VAR a path expression parent, i.e., can it be used to construct
1346 a valid path expression? */
1349 is_path_expr_parent (struct varobj
*var
)
1353 /* "Fake" children are not path_expr parents. */
1354 if (CPLUS_FAKE_CHILD (var
))
1357 type
= get_value_type (var
);
1359 /* Anonymous unions and structs are also not path_expr parents. */
1360 return !((TYPE_CODE (type
) == TYPE_CODE_STRUCT
1361 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
1362 && TYPE_NAME (type
) == NULL
);
1365 /* Return the path expression parent for VAR. */
1367 static struct varobj
*
1368 get_path_expr_parent (struct varobj
*var
)
1370 struct varobj
*parent
= var
;
1372 while (!is_root_p (parent
) && !is_path_expr_parent (parent
))
1373 parent
= parent
->parent
;
1378 /* Return a pointer to the full rooted expression of varobj VAR.
1379 If it has not been computed yet, compute it. */
1381 varobj_get_path_expr (struct varobj
*var
)
1383 if (var
->path_expr
!= NULL
)
1384 return var
->path_expr
;
1387 /* For root varobjs, we initialize path_expr
1388 when creating varobj, so here it should be
1390 gdb_assert (!is_root_p (var
));
1391 return (*var
->root
->lang
->path_expr_of_child
) (var
);
1395 enum varobj_languages
1396 varobj_get_language (struct varobj
*var
)
1398 return variable_language (var
);
1402 varobj_get_attributes (struct varobj
*var
)
1406 if (varobj_editable_p (var
))
1407 /* FIXME: define masks for attributes. */
1408 attributes
|= 0x00000001; /* Editable */
1414 varobj_pretty_printed_p (struct varobj
*var
)
1416 return var
->pretty_printer
!= NULL
;
1420 varobj_get_formatted_value (struct varobj
*var
,
1421 enum varobj_display_formats format
)
1423 return my_value_of_variable (var
, format
);
1427 varobj_get_value (struct varobj
*var
)
1429 return my_value_of_variable (var
, var
->format
);
1432 /* Set the value of an object variable (if it is editable) to the
1433 value of the given expression. */
1434 /* Note: Invokes functions that can call error(). */
1437 varobj_set_value (struct varobj
*var
, char *expression
)
1439 struct value
*val
= NULL
; /* Initialize to keep gcc happy. */
1440 /* The argument "expression" contains the variable's new value.
1441 We need to first construct a legal expression for this -- ugh! */
1442 /* Does this cover all the bases? */
1443 struct expression
*exp
;
1444 struct value
*value
= NULL
; /* Initialize to keep gcc happy. */
1445 int saved_input_radix
= input_radix
;
1446 const char *s
= expression
;
1447 volatile struct gdb_exception except
;
1449 gdb_assert (varobj_editable_p (var
));
1451 input_radix
= 10; /* ALWAYS reset to decimal temporarily. */
1452 exp
= parse_exp_1 (&s
, 0, 0, 0);
1453 TRY_CATCH (except
, RETURN_MASK_ERROR
)
1455 value
= evaluate_expression (exp
);
1458 if (except
.reason
< 0)
1460 /* We cannot proceed without a valid expression. */
1465 /* All types that are editable must also be changeable. */
1466 gdb_assert (varobj_value_is_changeable_p (var
));
1468 /* The value of a changeable variable object must not be lazy. */
1469 gdb_assert (!value_lazy (var
->value
));
1471 /* Need to coerce the input. We want to check if the
1472 value of the variable object will be different
1473 after assignment, and the first thing value_assign
1474 does is coerce the input.
1475 For example, if we are assigning an array to a pointer variable we
1476 should compare the pointer with the array's address, not with the
1478 value
= coerce_array (value
);
1480 /* The new value may be lazy. value_assign, or
1481 rather value_contents, will take care of this. */
1482 TRY_CATCH (except
, RETURN_MASK_ERROR
)
1484 val
= value_assign (var
->value
, value
);
1487 if (except
.reason
< 0)
1490 /* If the value has changed, record it, so that next -var-update can
1491 report this change. If a variable had a value of '1', we've set it
1492 to '333' and then set again to '1', when -var-update will report this
1493 variable as changed -- because the first assignment has set the
1494 'updated' flag. There's no need to optimize that, because return value
1495 of -var-update should be considered an approximation. */
1496 var
->updated
= install_new_value (var
, val
, 0 /* Compare values. */);
1497 input_radix
= saved_input_radix
;
1503 /* A helper function to install a constructor function and visualizer
1507 install_visualizer (struct varobj
*var
, PyObject
*constructor
,
1508 PyObject
*visualizer
)
1510 Py_XDECREF (var
->constructor
);
1511 var
->constructor
= constructor
;
1513 Py_XDECREF (var
->pretty_printer
);
1514 var
->pretty_printer
= visualizer
;
1516 Py_XDECREF (var
->child_iter
);
1517 var
->child_iter
= NULL
;
1520 /* Install the default visualizer for VAR. */
1523 install_default_visualizer (struct varobj
*var
)
1525 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1526 if (CPLUS_FAKE_CHILD (var
))
1529 if (pretty_printing
)
1531 PyObject
*pretty_printer
= NULL
;
1535 pretty_printer
= gdbpy_get_varobj_pretty_printer (var
->value
);
1536 if (! pretty_printer
)
1538 gdbpy_print_stack ();
1539 error (_("Cannot instantiate printer for default visualizer"));
1543 if (pretty_printer
== Py_None
)
1545 Py_DECREF (pretty_printer
);
1546 pretty_printer
= NULL
;
1549 install_visualizer (var
, NULL
, pretty_printer
);
1553 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1554 make a new object. */
1557 construct_visualizer (struct varobj
*var
, PyObject
*constructor
)
1559 PyObject
*pretty_printer
;
1561 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1562 if (CPLUS_FAKE_CHILD (var
))
1565 Py_INCREF (constructor
);
1566 if (constructor
== Py_None
)
1567 pretty_printer
= NULL
;
1570 pretty_printer
= instantiate_pretty_printer (constructor
, var
->value
);
1571 if (! pretty_printer
)
1573 gdbpy_print_stack ();
1574 Py_DECREF (constructor
);
1575 constructor
= Py_None
;
1576 Py_INCREF (constructor
);
1579 if (pretty_printer
== Py_None
)
1581 Py_DECREF (pretty_printer
);
1582 pretty_printer
= NULL
;
1586 install_visualizer (var
, constructor
, pretty_printer
);
1589 #endif /* HAVE_PYTHON */
1591 /* A helper function for install_new_value. This creates and installs
1592 a visualizer for VAR, if appropriate. */
1595 install_new_value_visualizer (struct varobj
*var
)
1598 /* If the constructor is None, then we want the raw value. If VAR
1599 does not have a value, just skip this. */
1600 if (!gdb_python_initialized
)
1603 if (var
->constructor
!= Py_None
&& var
->value
)
1605 struct cleanup
*cleanup
;
1607 cleanup
= varobj_ensure_python_env (var
);
1609 if (!var
->constructor
)
1610 install_default_visualizer (var
);
1612 construct_visualizer (var
, var
->constructor
);
1614 do_cleanups (cleanup
);
1621 /* When using RTTI to determine variable type it may be changed in runtime when
1622 the variable value is changed. This function checks whether type of varobj
1623 VAR will change when a new value NEW_VALUE is assigned and if it is so
1624 updates the type of VAR. */
1627 update_type_if_necessary (struct varobj
*var
, struct value
*new_value
)
1631 struct value_print_options opts
;
1633 get_user_print_options (&opts
);
1634 if (opts
.objectprint
)
1636 struct type
*new_type
;
1637 char *curr_type_str
, *new_type_str
;
1639 new_type
= value_actual_type (new_value
, 0, 0);
1640 new_type_str
= type_to_string (new_type
);
1641 curr_type_str
= varobj_get_type (var
);
1642 if (strcmp (curr_type_str
, new_type_str
) != 0)
1644 var
->type
= new_type
;
1646 /* This information may be not valid for a new type. */
1647 varobj_delete (var
, NULL
, 1);
1648 VEC_free (varobj_p
, var
->children
);
1649 var
->num_children
= -1;
1658 /* Assign a new value to a variable object. If INITIAL is non-zero,
1659 this is the first assignement after the variable object was just
1660 created, or changed type. In that case, just assign the value
1662 Otherwise, assign the new value, and return 1 if the value is
1663 different from the current one, 0 otherwise. The comparison is
1664 done on textual representation of value. Therefore, some types
1665 need not be compared. E.g. for structures the reported value is
1666 always "{...}", so no comparison is necessary here. If the old
1667 value was NULL and new one is not, or vice versa, we always return 1.
1669 The VALUE parameter should not be released -- the function will
1670 take care of releasing it when needed. */
1672 install_new_value (struct varobj
*var
, struct value
*value
, int initial
)
1677 int intentionally_not_fetched
= 0;
1678 char *print_value
= NULL
;
1680 /* We need to know the varobj's type to decide if the value should
1681 be fetched or not. C++ fake children (public/protected/private)
1682 don't have a type. */
1683 gdb_assert (var
->type
|| CPLUS_FAKE_CHILD (var
));
1684 changeable
= varobj_value_is_changeable_p (var
);
1686 /* If the type has custom visualizer, we consider it to be always
1687 changeable. FIXME: need to make sure this behaviour will not
1688 mess up read-sensitive values. */
1689 if (var
->pretty_printer
)
1692 need_to_fetch
= changeable
;
1694 /* We are not interested in the address of references, and given
1695 that in C++ a reference is not rebindable, it cannot
1696 meaningfully change. So, get hold of the real value. */
1698 value
= coerce_ref (value
);
1700 if (var
->type
&& TYPE_CODE (var
->type
) == TYPE_CODE_UNION
)
1701 /* For unions, we need to fetch the value implicitly because
1702 of implementation of union member fetch. When gdb
1703 creates a value for a field and the value of the enclosing
1704 structure is not lazy, it immediately copies the necessary
1705 bytes from the enclosing values. If the enclosing value is
1706 lazy, the call to value_fetch_lazy on the field will read
1707 the data from memory. For unions, that means we'll read the
1708 same memory more than once, which is not desirable. So
1712 /* The new value might be lazy. If the type is changeable,
1713 that is we'll be comparing values of this type, fetch the
1714 value now. Otherwise, on the next update the old value
1715 will be lazy, which means we've lost that old value. */
1716 if (need_to_fetch
&& value
&& value_lazy (value
))
1718 struct varobj
*parent
= var
->parent
;
1719 int frozen
= var
->frozen
;
1721 for (; !frozen
&& parent
; parent
= parent
->parent
)
1722 frozen
|= parent
->frozen
;
1724 if (frozen
&& initial
)
1726 /* For variables that are frozen, or are children of frozen
1727 variables, we don't do fetch on initial assignment.
1728 For non-initial assignemnt we do the fetch, since it means we're
1729 explicitly asked to compare the new value with the old one. */
1730 intentionally_not_fetched
= 1;
1734 volatile struct gdb_exception except
;
1736 TRY_CATCH (except
, RETURN_MASK_ERROR
)
1738 value_fetch_lazy (value
);
1741 if (except
.reason
< 0)
1743 /* Set the value to NULL, so that for the next -var-update,
1744 we don't try to compare the new value with this value,
1745 that we couldn't even read. */
1751 /* Get a reference now, before possibly passing it to any Python
1752 code that might release it. */
1754 value_incref (value
);
1756 /* Below, we'll be comparing string rendering of old and new
1757 values. Don't get string rendering if the value is
1758 lazy -- if it is, the code above has decided that the value
1759 should not be fetched. */
1760 if (value
&& !value_lazy (value
) && !var
->pretty_printer
)
1761 print_value
= value_get_print_value (value
, var
->format
, var
);
1763 /* If the type is changeable, compare the old and the new values.
1764 If this is the initial assignment, we don't have any old value
1766 if (!initial
&& changeable
)
1768 /* If the value of the varobj was changed by -var-set-value,
1769 then the value in the varobj and in the target is the same.
1770 However, that value is different from the value that the
1771 varobj had after the previous -var-update. So need to the
1772 varobj as changed. */
1777 else if (! var
->pretty_printer
)
1779 /* Try to compare the values. That requires that both
1780 values are non-lazy. */
1781 if (var
->not_fetched
&& value_lazy (var
->value
))
1783 /* This is a frozen varobj and the value was never read.
1784 Presumably, UI shows some "never read" indicator.
1785 Now that we've fetched the real value, we need to report
1786 this varobj as changed so that UI can show the real
1790 else if (var
->value
== NULL
&& value
== NULL
)
1793 else if (var
->value
== NULL
|| value
== NULL
)
1799 gdb_assert (!value_lazy (var
->value
));
1800 gdb_assert (!value_lazy (value
));
1802 gdb_assert (var
->print_value
!= NULL
&& print_value
!= NULL
);
1803 if (strcmp (var
->print_value
, print_value
) != 0)
1809 if (!initial
&& !changeable
)
1811 /* For values that are not changeable, we don't compare the values.
1812 However, we want to notice if a value was not NULL and now is NULL,
1813 or vise versa, so that we report when top-level varobjs come in scope
1814 and leave the scope. */
1815 changed
= (var
->value
!= NULL
) != (value
!= NULL
);
1818 /* We must always keep the new value, since children depend on it. */
1819 if (var
->value
!= NULL
&& var
->value
!= value
)
1820 value_free (var
->value
);
1822 if (value
&& value_lazy (value
) && intentionally_not_fetched
)
1823 var
->not_fetched
= 1;
1825 var
->not_fetched
= 0;
1828 install_new_value_visualizer (var
);
1830 /* If we installed a pretty-printer, re-compare the printed version
1831 to see if the variable changed. */
1832 if (var
->pretty_printer
)
1834 xfree (print_value
);
1835 print_value
= value_get_print_value (var
->value
, var
->format
, var
);
1836 if ((var
->print_value
== NULL
&& print_value
!= NULL
)
1837 || (var
->print_value
!= NULL
&& print_value
== NULL
)
1838 || (var
->print_value
!= NULL
&& print_value
!= NULL
1839 && strcmp (var
->print_value
, print_value
) != 0))
1842 if (var
->print_value
)
1843 xfree (var
->print_value
);
1844 var
->print_value
= print_value
;
1846 gdb_assert (!var
->value
|| value_type (var
->value
));
1851 /* Return the requested range for a varobj. VAR is the varobj. FROM
1852 and TO are out parameters; *FROM and *TO will be set to the
1853 selected sub-range of VAR. If no range was selected using
1854 -var-set-update-range, then both will be -1. */
1856 varobj_get_child_range (struct varobj
*var
, int *from
, int *to
)
1862 /* Set the selected sub-range of children of VAR to start at index
1863 FROM and end at index TO. If either FROM or TO is less than zero,
1864 this is interpreted as a request for all children. */
1866 varobj_set_child_range (struct varobj
*var
, int from
, int to
)
1873 varobj_set_visualizer (struct varobj
*var
, const char *visualizer
)
1876 PyObject
*mainmod
, *globals
, *constructor
;
1877 struct cleanup
*back_to
;
1879 if (!gdb_python_initialized
)
1882 back_to
= varobj_ensure_python_env (var
);
1884 mainmod
= PyImport_AddModule ("__main__");
1885 globals
= PyModule_GetDict (mainmod
);
1886 Py_INCREF (globals
);
1887 make_cleanup_py_decref (globals
);
1889 constructor
= PyRun_String (visualizer
, Py_eval_input
, globals
, globals
);
1893 gdbpy_print_stack ();
1894 error (_("Could not evaluate visualizer expression: %s"), visualizer
);
1897 construct_visualizer (var
, constructor
);
1898 Py_XDECREF (constructor
);
1900 /* If there are any children now, wipe them. */
1901 varobj_delete (var
, NULL
, 1 /* children only */);
1902 var
->num_children
= -1;
1904 do_cleanups (back_to
);
1906 error (_("Python support required"));
1910 /* If NEW_VALUE is the new value of the given varobj (var), return
1911 non-zero if var has mutated. In other words, if the type of
1912 the new value is different from the type of the varobj's old
1915 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1918 varobj_value_has_mutated (struct varobj
*var
, struct value
*new_value
,
1919 struct type
*new_type
)
1921 /* If we haven't previously computed the number of children in var,
1922 it does not matter from the front-end's perspective whether
1923 the type has mutated or not. For all intents and purposes,
1924 it has not mutated. */
1925 if (var
->num_children
< 0)
1928 if (var
->root
->lang
->value_has_mutated
)
1929 return var
->root
->lang
->value_has_mutated (var
, new_value
, new_type
);
1934 /* Update the values for a variable and its children. This is a
1935 two-pronged attack. First, re-parse the value for the root's
1936 expression to see if it's changed. Then go all the way
1937 through its children, reconstructing them and noting if they've
1940 The EXPLICIT parameter specifies if this call is result
1941 of MI request to update this specific variable, or
1942 result of implicit -var-update *. For implicit request, we don't
1943 update frozen variables.
1945 NOTE: This function may delete the caller's varobj. If it
1946 returns TYPE_CHANGED, then it has done this and VARP will be modified
1947 to point to the new varobj. */
1949 VEC(varobj_update_result
) *
1950 varobj_update (struct varobj
**varp
, int explicit)
1952 int type_changed
= 0;
1955 VEC (varobj_update_result
) *stack
= NULL
;
1956 VEC (varobj_update_result
) *result
= NULL
;
1958 /* Frozen means frozen -- we don't check for any change in
1959 this varobj, including its going out of scope, or
1960 changing type. One use case for frozen varobjs is
1961 retaining previously evaluated expressions, and we don't
1962 want them to be reevaluated at all. */
1963 if (!explicit && (*varp
)->frozen
)
1966 if (!(*varp
)->root
->is_valid
)
1968 varobj_update_result r
= {0};
1971 r
.status
= VAROBJ_INVALID
;
1972 VEC_safe_push (varobj_update_result
, result
, &r
);
1976 if ((*varp
)->root
->rootvar
== *varp
)
1978 varobj_update_result r
= {0};
1981 r
.status
= VAROBJ_IN_SCOPE
;
1983 /* Update the root variable. value_of_root can return NULL
1984 if the variable is no longer around, i.e. we stepped out of
1985 the frame in which a local existed. We are letting the
1986 value_of_root variable dispose of the varobj if the type
1988 new = value_of_root (varp
, &type_changed
);
1989 if (update_type_if_necessary(*varp
, new))
1992 r
.type_changed
= type_changed
;
1993 if (install_new_value ((*varp
), new, type_changed
))
1997 r
.status
= VAROBJ_NOT_IN_SCOPE
;
1998 r
.value_installed
= 1;
2000 if (r
.status
== VAROBJ_NOT_IN_SCOPE
)
2002 if (r
.type_changed
|| r
.changed
)
2003 VEC_safe_push (varobj_update_result
, result
, &r
);
2007 VEC_safe_push (varobj_update_result
, stack
, &r
);
2011 varobj_update_result r
= {0};
2014 VEC_safe_push (varobj_update_result
, stack
, &r
);
2017 /* Walk through the children, reconstructing them all. */
2018 while (!VEC_empty (varobj_update_result
, stack
))
2020 varobj_update_result r
= *(VEC_last (varobj_update_result
, stack
));
2021 struct varobj
*v
= r
.varobj
;
2023 VEC_pop (varobj_update_result
, stack
);
2025 /* Update this variable, unless it's a root, which is already
2027 if (!r
.value_installed
)
2029 struct type
*new_type
;
2031 new = value_of_child (v
->parent
, v
->index
);
2032 if (update_type_if_necessary(v
, new))
2035 new_type
= value_type (new);
2037 new_type
= v
->root
->lang
->type_of_child (v
->parent
, v
->index
);
2039 if (varobj_value_has_mutated (v
, new, new_type
))
2041 /* The children are no longer valid; delete them now.
2042 Report the fact that its type changed as well. */
2043 varobj_delete (v
, NULL
, 1 /* only_children */);
2044 v
->num_children
= -1;
2051 if (install_new_value (v
, new, r
.type_changed
))
2058 /* We probably should not get children of a varobj that has a
2059 pretty-printer, but for which -var-list-children was never
2061 if (v
->pretty_printer
)
2063 VEC (varobj_p
) *changed
= 0, *type_changed
= 0, *unchanged
= 0;
2064 VEC (varobj_p
) *new = 0;
2065 int i
, children_changed
= 0;
2070 if (!v
->children_requested
)
2074 /* If we initially did not have potential children, but
2075 now we do, consider the varobj as changed.
2076 Otherwise, if children were never requested, consider
2077 it as unchanged -- presumably, such varobj is not yet
2078 expanded in the UI, so we need not bother getting
2080 if (!varobj_has_more (v
, 0))
2082 update_dynamic_varobj_children (v
, NULL
, NULL
, NULL
, NULL
,
2084 if (varobj_has_more (v
, 0))
2089 VEC_safe_push (varobj_update_result
, result
, &r
);
2094 /* If update_dynamic_varobj_children returns 0, then we have
2095 a non-conforming pretty-printer, so we skip it. */
2096 if (update_dynamic_varobj_children (v
, &changed
, &type_changed
, &new,
2097 &unchanged
, &children_changed
, 1,
2100 if (children_changed
|| new)
2102 r
.children_changed
= 1;
2105 /* Push in reverse order so that the first child is
2106 popped from the work stack first, and so will be
2107 added to result first. This does not affect
2108 correctness, just "nicer". */
2109 for (i
= VEC_length (varobj_p
, type_changed
) - 1; i
>= 0; --i
)
2111 varobj_p tmp
= VEC_index (varobj_p
, type_changed
, i
);
2112 varobj_update_result r
= {0};
2114 /* Type may change only if value was changed. */
2118 r
.value_installed
= 1;
2119 VEC_safe_push (varobj_update_result
, stack
, &r
);
2121 for (i
= VEC_length (varobj_p
, changed
) - 1; i
>= 0; --i
)
2123 varobj_p tmp
= VEC_index (varobj_p
, changed
, i
);
2124 varobj_update_result r
= {0};
2128 r
.value_installed
= 1;
2129 VEC_safe_push (varobj_update_result
, stack
, &r
);
2131 for (i
= VEC_length (varobj_p
, unchanged
) - 1; i
>= 0; --i
)
2133 varobj_p tmp
= VEC_index (varobj_p
, unchanged
, i
);
2137 varobj_update_result r
= {0};
2140 r
.value_installed
= 1;
2141 VEC_safe_push (varobj_update_result
, stack
, &r
);
2144 if (r
.changed
|| r
.children_changed
)
2145 VEC_safe_push (varobj_update_result
, result
, &r
);
2147 /* Free CHANGED, TYPE_CHANGED and UNCHANGED, but not NEW,
2148 because NEW has been put into the result vector. */
2149 VEC_free (varobj_p
, changed
);
2150 VEC_free (varobj_p
, type_changed
);
2151 VEC_free (varobj_p
, unchanged
);
2157 /* Push any children. Use reverse order so that the first
2158 child is popped from the work stack first, and so
2159 will be added to result first. This does not
2160 affect correctness, just "nicer". */
2161 for (i
= VEC_length (varobj_p
, v
->children
)-1; i
>= 0; --i
)
2163 varobj_p c
= VEC_index (varobj_p
, v
->children
, i
);
2165 /* Child may be NULL if explicitly deleted by -var-delete. */
2166 if (c
!= NULL
&& !c
->frozen
)
2168 varobj_update_result r
= {0};
2171 VEC_safe_push (varobj_update_result
, stack
, &r
);
2175 if (r
.changed
|| r
.type_changed
)
2176 VEC_safe_push (varobj_update_result
, result
, &r
);
2179 VEC_free (varobj_update_result
, stack
);
2185 /* Helper functions */
2188 * Variable object construction/destruction
2192 delete_variable (struct cpstack
**resultp
, struct varobj
*var
,
2193 int only_children_p
)
2197 delete_variable_1 (resultp
, &delcount
, var
,
2198 only_children_p
, 1 /* remove_from_parent_p */ );
2203 /* Delete the variable object VAR and its children. */
2204 /* IMPORTANT NOTE: If we delete a variable which is a child
2205 and the parent is not removed we dump core. It must be always
2206 initially called with remove_from_parent_p set. */
2208 delete_variable_1 (struct cpstack
**resultp
, int *delcountp
,
2209 struct varobj
*var
, int only_children_p
,
2210 int remove_from_parent_p
)
2214 /* Delete any children of this variable, too. */
2215 for (i
= 0; i
< VEC_length (varobj_p
, var
->children
); ++i
)
2217 varobj_p child
= VEC_index (varobj_p
, var
->children
, i
);
2221 if (!remove_from_parent_p
)
2222 child
->parent
= NULL
;
2223 delete_variable_1 (resultp
, delcountp
, child
, 0, only_children_p
);
2225 VEC_free (varobj_p
, var
->children
);
2227 /* if we were called to delete only the children we are done here. */
2228 if (only_children_p
)
2231 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
2232 /* If the name is null, this is a temporary variable, that has not
2233 yet been installed, don't report it, it belongs to the caller... */
2234 if (var
->obj_name
!= NULL
)
2236 cppush (resultp
, xstrdup (var
->obj_name
));
2237 *delcountp
= *delcountp
+ 1;
2240 /* If this variable has a parent, remove it from its parent's list. */
2241 /* OPTIMIZATION: if the parent of this variable is also being deleted,
2242 (as indicated by remove_from_parent_p) we don't bother doing an
2243 expensive list search to find the element to remove when we are
2244 discarding the list afterwards. */
2245 if ((remove_from_parent_p
) && (var
->parent
!= NULL
))
2247 VEC_replace (varobj_p
, var
->parent
->children
, var
->index
, NULL
);
2250 if (var
->obj_name
!= NULL
)
2251 uninstall_variable (var
);
2253 /* Free memory associated with this variable. */
2254 free_variable (var
);
2257 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
2259 install_variable (struct varobj
*var
)
2262 struct vlist
*newvl
;
2264 unsigned int index
= 0;
2267 for (chp
= var
->obj_name
; *chp
; chp
++)
2269 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
2272 cv
= *(varobj_table
+ index
);
2273 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
2277 error (_("Duplicate variable object name"));
2279 /* Add varobj to hash table. */
2280 newvl
= xmalloc (sizeof (struct vlist
));
2281 newvl
->next
= *(varobj_table
+ index
);
2283 *(varobj_table
+ index
) = newvl
;
2285 /* If root, add varobj to root list. */
2286 if (is_root_p (var
))
2288 /* Add to list of root variables. */
2289 if (rootlist
== NULL
)
2290 var
->root
->next
= NULL
;
2292 var
->root
->next
= rootlist
;
2293 rootlist
= var
->root
;
2299 /* Unistall the object VAR. */
2301 uninstall_variable (struct varobj
*var
)
2305 struct varobj_root
*cr
;
2306 struct varobj_root
*prer
;
2308 unsigned int index
= 0;
2311 /* Remove varobj from hash table. */
2312 for (chp
= var
->obj_name
; *chp
; chp
++)
2314 index
= (index
+ (i
++ * (unsigned int) *chp
)) % VAROBJ_TABLE_SIZE
;
2317 cv
= *(varobj_table
+ index
);
2319 while ((cv
!= NULL
) && (strcmp (cv
->var
->obj_name
, var
->obj_name
) != 0))
2326 fprintf_unfiltered (gdb_stdlog
, "Deleting %s\n", var
->obj_name
);
2331 ("Assertion failed: Could not find variable object \"%s\" to delete",
2337 *(varobj_table
+ index
) = cv
->next
;
2339 prev
->next
= cv
->next
;
2343 /* If root, remove varobj from root list. */
2344 if (is_root_p (var
))
2346 /* Remove from list of root variables. */
2347 if (rootlist
== var
->root
)
2348 rootlist
= var
->root
->next
;
2353 while ((cr
!= NULL
) && (cr
->rootvar
!= var
))
2360 warning (_("Assertion failed: Could not find "
2361 "varobj \"%s\" in root list"),
2368 prer
->next
= cr
->next
;
2374 /* Create and install a child of the parent of the given name. */
2375 static struct varobj
*
2376 create_child (struct varobj
*parent
, int index
, char *name
)
2378 return create_child_with_value (parent
, index
, name
,
2379 value_of_child (parent
, index
));
2382 /* Does CHILD represent a child with no name? This happens when
2383 the child is an anonmous struct or union and it has no field name
2384 in its parent variable.
2386 This has already been determined by *_describe_child. The easiest
2387 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
2390 is_anonymous_child (struct varobj
*child
)
2392 return (strcmp (child
->name
, ANONYMOUS_STRUCT_NAME
) == 0
2393 || strcmp (child
->name
, ANONYMOUS_UNION_NAME
) == 0);
2396 static struct varobj
*
2397 create_child_with_value (struct varobj
*parent
, int index
, const char *name
,
2398 struct value
*value
)
2400 struct varobj
*child
;
2403 child
= new_variable ();
2405 /* Name is allocated by name_of_child. */
2406 /* FIXME: xstrdup should not be here. */
2407 child
->name
= xstrdup (name
);
2408 child
->index
= index
;
2409 child
->parent
= parent
;
2410 child
->root
= parent
->root
;
2412 if (is_anonymous_child (child
))
2413 childs_name
= xstrprintf ("%s.%d_anonymous", parent
->obj_name
, index
);
2415 childs_name
= xstrprintf ("%s.%s", parent
->obj_name
, name
);
2416 child
->obj_name
= childs_name
;
2418 install_variable (child
);
2420 /* Compute the type of the child. Must do this before
2421 calling install_new_value. */
2423 /* If the child had no evaluation errors, var->value
2424 will be non-NULL and contain a valid type. */
2425 child
->type
= value_actual_type (value
, 0, NULL
);
2427 /* Otherwise, we must compute the type. */
2428 child
->type
= (*child
->root
->lang
->type_of_child
) (child
->parent
,
2430 install_new_value (child
, value
, 1);
2437 * Miscellaneous utility functions.
2440 /* Allocate memory and initialize a new variable. */
2441 static struct varobj
*
2446 var
= (struct varobj
*) xmalloc (sizeof (struct varobj
));
2448 var
->path_expr
= NULL
;
2449 var
->obj_name
= NULL
;
2453 var
->num_children
= -1;
2455 var
->children
= NULL
;
2459 var
->print_value
= NULL
;
2461 var
->not_fetched
= 0;
2462 var
->children_requested
= 0;
2465 var
->constructor
= 0;
2466 var
->pretty_printer
= 0;
2467 var
->child_iter
= 0;
2468 var
->saved_item
= 0;
2473 /* Allocate memory and initialize a new root variable. */
2474 static struct varobj
*
2475 new_root_variable (void)
2477 struct varobj
*var
= new_variable ();
2479 var
->root
= (struct varobj_root
*) xmalloc (sizeof (struct varobj_root
));
2480 var
->root
->lang
= NULL
;
2481 var
->root
->exp
= NULL
;
2482 var
->root
->valid_block
= NULL
;
2483 var
->root
->frame
= null_frame_id
;
2484 var
->root
->floating
= 0;
2485 var
->root
->rootvar
= NULL
;
2486 var
->root
->is_valid
= 1;
2491 /* Free any allocated memory associated with VAR. */
2493 free_variable (struct varobj
*var
)
2496 if (var
->pretty_printer
)
2498 struct cleanup
*cleanup
= varobj_ensure_python_env (var
);
2499 Py_XDECREF (var
->constructor
);
2500 Py_XDECREF (var
->pretty_printer
);
2501 Py_XDECREF (var
->child_iter
);
2502 Py_XDECREF (var
->saved_item
);
2503 do_cleanups (cleanup
);
2507 value_free (var
->value
);
2509 /* Free the expression if this is a root variable. */
2510 if (is_root_p (var
))
2512 xfree (var
->root
->exp
);
2517 xfree (var
->obj_name
);
2518 xfree (var
->print_value
);
2519 xfree (var
->path_expr
);
2524 do_free_variable_cleanup (void *var
)
2526 free_variable (var
);
2529 static struct cleanup
*
2530 make_cleanup_free_variable (struct varobj
*var
)
2532 return make_cleanup (do_free_variable_cleanup
, var
);
2535 /* This returns the type of the variable. It also skips past typedefs
2536 to return the real type of the variable.
2538 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2539 except within get_target_type and get_type. */
2540 static struct type
*
2541 get_type (struct varobj
*var
)
2547 type
= check_typedef (type
);
2552 /* Return the type of the value that's stored in VAR,
2553 or that would have being stored there if the
2554 value were accessible.
2556 This differs from VAR->type in that VAR->type is always
2557 the true type of the expession in the source language.
2558 The return value of this function is the type we're
2559 actually storing in varobj, and using for displaying
2560 the values and for comparing previous and new values.
2562 For example, top-level references are always stripped. */
2563 static struct type
*
2564 get_value_type (struct varobj
*var
)
2569 type
= value_type (var
->value
);
2573 type
= check_typedef (type
);
2575 if (TYPE_CODE (type
) == TYPE_CODE_REF
)
2576 type
= get_target_type (type
);
2578 type
= check_typedef (type
);
2583 /* This returns the target type (or NULL) of TYPE, also skipping
2584 past typedefs, just like get_type ().
2586 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
2587 except within get_target_type and get_type. */
2588 static struct type
*
2589 get_target_type (struct type
*type
)
2593 type
= TYPE_TARGET_TYPE (type
);
2595 type
= check_typedef (type
);
2601 /* What is the default display for this variable? We assume that
2602 everything is "natural". Any exceptions? */
2603 static enum varobj_display_formats
2604 variable_default_display (struct varobj
*var
)
2606 return FORMAT_NATURAL
;
2609 /* FIXME: The following should be generic for any pointer. */
2611 cppush (struct cpstack
**pstack
, char *name
)
2615 s
= (struct cpstack
*) xmalloc (sizeof (struct cpstack
));
2621 /* FIXME: The following should be generic for any pointer. */
2623 cppop (struct cpstack
**pstack
)
2628 if ((*pstack
)->name
== NULL
&& (*pstack
)->next
== NULL
)
2633 *pstack
= (*pstack
)->next
;
2640 * Language-dependencies
2643 /* Common entry points */
2645 /* Get the language of variable VAR. */
2646 static enum varobj_languages
2647 variable_language (struct varobj
*var
)
2649 enum varobj_languages lang
;
2651 switch (var
->root
->exp
->language_defn
->la_language
)
2657 case language_cplus
:
2671 /* Return the number of children for a given variable.
2672 The result of this function is defined by the language
2673 implementation. The number of children returned by this function
2674 is the number of children that the user will see in the variable
2677 number_of_children (struct varobj
*var
)
2679 return (*var
->root
->lang
->number_of_children
) (var
);
2682 /* What is the expression for the root varobj VAR? Returns a malloc'd
2685 name_of_variable (struct varobj
*var
)
2687 return (*var
->root
->lang
->name_of_variable
) (var
);
2690 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd
2693 name_of_child (struct varobj
*var
, int index
)
2695 return (*var
->root
->lang
->name_of_child
) (var
, index
);
2698 /* If frame associated with VAR can be found, switch
2699 to it and return 1. Otherwise, return 0. */
2702 check_scope (struct varobj
*var
)
2704 struct frame_info
*fi
;
2707 fi
= frame_find_by_id (var
->root
->frame
);
2712 CORE_ADDR pc
= get_frame_pc (fi
);
2714 if (pc
< BLOCK_START (var
->root
->valid_block
) ||
2715 pc
>= BLOCK_END (var
->root
->valid_block
))
2723 /* Helper function to value_of_root. */
2725 static struct value
*
2726 value_of_root_1 (struct varobj
**var_handle
)
2728 struct value
*new_val
= NULL
;
2729 struct varobj
*var
= *var_handle
;
2730 int within_scope
= 0;
2731 struct cleanup
*back_to
;
2733 /* Only root variables can be updated... */
2734 if (!is_root_p (var
))
2735 /* Not a root var. */
2738 back_to
= make_cleanup_restore_current_thread ();
2740 /* Determine whether the variable is still around. */
2741 if (var
->root
->valid_block
== NULL
|| var
->root
->floating
)
2743 else if (var
->root
->thread_id
== 0)
2745 /* The program was single-threaded when the variable object was
2746 created. Technically, it's possible that the program became
2747 multi-threaded since then, but we don't support such
2749 within_scope
= check_scope (var
);
2753 ptid_t ptid
= thread_id_to_pid (var
->root
->thread_id
);
2754 if (in_thread_list (ptid
))
2756 switch_to_thread (ptid
);
2757 within_scope
= check_scope (var
);
2763 volatile struct gdb_exception except
;
2765 /* We need to catch errors here, because if evaluate
2766 expression fails we want to just return NULL. */
2767 TRY_CATCH (except
, RETURN_MASK_ERROR
)
2769 new_val
= evaluate_expression (var
->root
->exp
);
2773 do_cleanups (back_to
);
2778 /* What is the ``struct value *'' of the root variable VAR?
2779 For floating variable object, evaluation can get us a value
2780 of different type from what is stored in varobj already. In
2782 - *type_changed will be set to 1
2783 - old varobj will be freed, and new one will be
2784 created, with the same name.
2785 - *var_handle will be set to the new varobj
2786 Otherwise, *type_changed will be set to 0. */
2787 static struct value
*
2788 value_of_root (struct varobj
**var_handle
, int *type_changed
)
2792 if (var_handle
== NULL
)
2797 /* This should really be an exception, since this should
2798 only get called with a root variable. */
2800 if (!is_root_p (var
))
2803 if (var
->root
->floating
)
2805 struct varobj
*tmp_var
;
2806 char *old_type
, *new_type
;
2808 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
2809 USE_SELECTED_FRAME
);
2810 if (tmp_var
== NULL
)
2814 old_type
= varobj_get_type (var
);
2815 new_type
= varobj_get_type (tmp_var
);
2816 if (strcmp (old_type
, new_type
) == 0)
2818 /* The expression presently stored inside var->root->exp
2819 remembers the locations of local variables relatively to
2820 the frame where the expression was created (in DWARF location
2821 button, for example). Naturally, those locations are not
2822 correct in other frames, so update the expression. */
2824 struct expression
*tmp_exp
= var
->root
->exp
;
2826 var
->root
->exp
= tmp_var
->root
->exp
;
2827 tmp_var
->root
->exp
= tmp_exp
;
2829 varobj_delete (tmp_var
, NULL
, 0);
2834 tmp_var
->obj_name
= xstrdup (var
->obj_name
);
2835 tmp_var
->from
= var
->from
;
2836 tmp_var
->to
= var
->to
;
2837 varobj_delete (var
, NULL
, 0);
2839 install_variable (tmp_var
);
2840 *var_handle
= tmp_var
;
2853 struct value
*value
;
2855 value
= value_of_root_1 (var_handle
);
2856 if (var
->value
== NULL
|| value
== NULL
)
2858 /* For root varobj-s, a NULL value indicates a scoping issue.
2859 So, nothing to do in terms of checking for mutations. */
2861 else if (varobj_value_has_mutated (var
, value
, value_type (value
)))
2863 /* The type has mutated, so the children are no longer valid.
2864 Just delete them, and tell our caller that the type has
2866 varobj_delete (var
, NULL
, 1 /* only_children */);
2867 var
->num_children
= -1;
2876 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
2877 static struct value
*
2878 value_of_child (struct varobj
*parent
, int index
)
2880 struct value
*value
;
2882 value
= (*parent
->root
->lang
->value_of_child
) (parent
, index
);
2887 /* GDB already has a command called "value_of_variable". Sigh. */
2889 my_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
2891 if (var
->root
->is_valid
)
2893 if (var
->pretty_printer
)
2894 return value_get_print_value (var
->value
, var
->format
, var
);
2895 return (*var
->root
->lang
->value_of_variable
) (var
, format
);
2902 value_get_print_value (struct value
*value
, enum varobj_display_formats format
,
2905 struct ui_file
*stb
;
2906 struct cleanup
*old_chain
;
2907 char *thevalue
= NULL
;
2908 struct value_print_options opts
;
2909 struct type
*type
= NULL
;
2911 char *encoding
= NULL
;
2912 struct gdbarch
*gdbarch
= NULL
;
2913 /* Initialize it just to avoid a GCC false warning. */
2914 CORE_ADDR str_addr
= 0;
2915 int string_print
= 0;
2920 stb
= mem_fileopen ();
2921 old_chain
= make_cleanup_ui_file_delete (stb
);
2923 gdbarch
= get_type_arch (value_type (value
));
2925 if (gdb_python_initialized
)
2927 PyObject
*value_formatter
= var
->pretty_printer
;
2929 varobj_ensure_python_env (var
);
2931 if (value_formatter
)
2933 /* First check to see if we have any children at all. If so,
2934 we simply return {...}. */
2935 if (dynamic_varobj_has_child_method (var
))
2937 do_cleanups (old_chain
);
2938 return xstrdup ("{...}");
2941 if (PyObject_HasAttr (value_formatter
, gdbpy_to_string_cst
))
2943 struct value
*replacement
;
2944 PyObject
*output
= NULL
;
2946 output
= apply_varobj_pretty_printer (value_formatter
,
2950 /* If we have string like output ... */
2953 make_cleanup_py_decref (output
);
2955 /* If this is a lazy string, extract it. For lazy
2956 strings we always print as a string, so set
2958 if (gdbpy_is_lazy_string (output
))
2960 gdbpy_extract_lazy_string (output
, &str_addr
, &type
,
2962 make_cleanup (free_current_contents
, &encoding
);
2967 /* If it is a regular (non-lazy) string, extract
2968 it and copy the contents into THEVALUE. If the
2969 hint says to print it as a string, set
2970 string_print. Otherwise just return the extracted
2971 string as a value. */
2973 char *s
= python_string_to_target_string (output
);
2979 hint
= gdbpy_get_display_hint (value_formatter
);
2982 if (!strcmp (hint
, "string"))
2988 thevalue
= xmemdup (s
, len
+ 1, len
+ 1);
2989 type
= builtin_type (gdbarch
)->builtin_char
;
2994 do_cleanups (old_chain
);
2998 make_cleanup (xfree
, thevalue
);
3001 gdbpy_print_stack ();
3004 /* If the printer returned a replacement value, set VALUE
3005 to REPLACEMENT. If there is not a replacement value,
3006 just use the value passed to this function. */
3008 value
= replacement
;
3014 get_formatted_print_options (&opts
, format_code
[(int) format
]);
3018 /* If the THEVALUE has contents, it is a regular string. */
3020 LA_PRINT_STRING (stb
, type
, (gdb_byte
*) thevalue
, len
, encoding
, 0, &opts
);
3021 else if (string_print
)
3022 /* Otherwise, if string_print is set, and it is not a regular
3023 string, it is a lazy string. */
3024 val_print_string (type
, encoding
, str_addr
, len
, stb
, &opts
);
3026 /* All other cases. */
3027 common_val_print (value
, stb
, 0, &opts
, current_language
);
3029 thevalue
= ui_file_xstrdup (stb
, NULL
);
3031 do_cleanups (old_chain
);
3036 varobj_editable_p (struct varobj
*var
)
3040 if (!(var
->root
->is_valid
&& var
->value
&& VALUE_LVAL (var
->value
)))
3043 type
= get_value_type (var
);
3045 switch (TYPE_CODE (type
))
3047 case TYPE_CODE_STRUCT
:
3048 case TYPE_CODE_UNION
:
3049 case TYPE_CODE_ARRAY
:
3050 case TYPE_CODE_FUNC
:
3051 case TYPE_CODE_METHOD
:
3061 /* Call VAR's value_is_changeable_p language-specific callback. */
3064 varobj_value_is_changeable_p (struct varobj
*var
)
3066 return var
->root
->lang
->value_is_changeable_p (var
);
3069 /* Return 1 if that varobj is floating, that is is always evaluated in the
3070 selected frame, and not bound to thread/frame. Such variable objects
3071 are created using '@' as frame specifier to -var-create. */
3073 varobj_floating_p (struct varobj
*var
)
3075 return var
->root
->floating
;
3078 /* Given the value and the type of a variable object,
3079 adjust the value and type to those necessary
3080 for getting children of the variable object.
3081 This includes dereferencing top-level references
3082 to all types and dereferencing pointers to
3085 If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
3086 value will be fetched and if it differs from static type
3087 the value will be casted to it.
3089 Both TYPE and *TYPE should be non-null. VALUE
3090 can be null if we want to only translate type.
3091 *VALUE can be null as well -- if the parent
3094 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
3095 depending on whether pointer was dereferenced
3096 in this function. */
3098 adjust_value_for_child_access (struct value
**value
,
3101 int lookup_actual_type
)
3103 gdb_assert (type
&& *type
);
3108 *type
= check_typedef (*type
);
3110 /* The type of value stored in varobj, that is passed
3111 to us, is already supposed to be
3112 reference-stripped. */
3114 gdb_assert (TYPE_CODE (*type
) != TYPE_CODE_REF
);
3116 /* Pointers to structures are treated just like
3117 structures when accessing children. Don't
3118 dererences pointers to other types. */
3119 if (TYPE_CODE (*type
) == TYPE_CODE_PTR
)
3121 struct type
*target_type
= get_target_type (*type
);
3122 if (TYPE_CODE (target_type
) == TYPE_CODE_STRUCT
3123 || TYPE_CODE (target_type
) == TYPE_CODE_UNION
)
3125 if (value
&& *value
)
3127 volatile struct gdb_exception except
;
3129 TRY_CATCH (except
, RETURN_MASK_ERROR
)
3131 *value
= value_ind (*value
);
3134 if (except
.reason
< 0)
3137 *type
= target_type
;
3143 /* The 'get_target_type' function calls check_typedef on
3144 result, so we can immediately check type code. No
3145 need to call check_typedef here. */
3147 /* Access a real type of the value (if necessary and possible). */
3148 if (value
&& *value
&& lookup_actual_type
)
3150 struct type
*enclosing_type
;
3151 int real_type_found
= 0;
3153 enclosing_type
= value_actual_type (*value
, 1, &real_type_found
);
3154 if (real_type_found
)
3156 *type
= enclosing_type
;
3157 *value
= value_cast (enclosing_type
, *value
);
3162 /* Implement the "value_is_changeable_p" varobj callback for most
3166 default_value_is_changeable_p (struct varobj
*var
)
3171 if (CPLUS_FAKE_CHILD (var
))
3174 type
= get_value_type (var
);
3176 switch (TYPE_CODE (type
))
3178 case TYPE_CODE_STRUCT
:
3179 case TYPE_CODE_UNION
:
3180 case TYPE_CODE_ARRAY
:
3194 c_number_of_children (struct varobj
*var
)
3196 struct type
*type
= get_value_type (var
);
3198 struct type
*target
;
3200 adjust_value_for_child_access (NULL
, &type
, NULL
, 0);
3201 target
= get_target_type (type
);
3203 switch (TYPE_CODE (type
))
3205 case TYPE_CODE_ARRAY
:
3206 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
3207 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
3208 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
3210 /* If we don't know how many elements there are, don't display
3215 case TYPE_CODE_STRUCT
:
3216 case TYPE_CODE_UNION
:
3217 children
= TYPE_NFIELDS (type
);
3221 /* The type here is a pointer to non-struct. Typically, pointers
3222 have one child, except for function ptrs, which have no children,
3223 and except for void*, as we don't know what to show.
3225 We can show char* so we allow it to be dereferenced. If you decide
3226 to test for it, please mind that a little magic is necessary to
3227 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
3228 TYPE_NAME == "char". */
3229 if (TYPE_CODE (target
) == TYPE_CODE_FUNC
3230 || TYPE_CODE (target
) == TYPE_CODE_VOID
)
3237 /* Other types have no children. */
3245 c_name_of_variable (struct varobj
*parent
)
3247 return xstrdup (parent
->name
);
3250 /* Return the value of element TYPE_INDEX of a structure
3251 value VALUE. VALUE's type should be a structure,
3252 or union, or a typedef to struct/union.
3254 Returns NULL if getting the value fails. Never throws. */
3255 static struct value
*
3256 value_struct_element_index (struct value
*value
, int type_index
)
3258 struct value
*result
= NULL
;
3259 volatile struct gdb_exception e
;
3260 struct type
*type
= value_type (value
);
3262 type
= check_typedef (type
);
3264 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
3265 || TYPE_CODE (type
) == TYPE_CODE_UNION
);
3267 TRY_CATCH (e
, RETURN_MASK_ERROR
)
3269 if (field_is_static (&TYPE_FIELD (type
, type_index
)))
3270 result
= value_static_field (type
, type_index
);
3272 result
= value_primitive_field (value
, 0, type_index
, type
);
3284 /* Obtain the information about child INDEX of the variable
3286 If CNAME is not null, sets *CNAME to the name of the child relative
3288 If CVALUE is not null, sets *CVALUE to the value of the child.
3289 If CTYPE is not null, sets *CTYPE to the type of the child.
3291 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
3292 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
3295 c_describe_child (struct varobj
*parent
, int index
,
3296 char **cname
, struct value
**cvalue
, struct type
**ctype
,
3297 char **cfull_expression
)
3299 struct value
*value
= parent
->value
;
3300 struct type
*type
= get_value_type (parent
);
3301 char *parent_expression
= NULL
;
3303 volatile struct gdb_exception except
;
3311 if (cfull_expression
)
3313 *cfull_expression
= NULL
;
3314 parent_expression
= varobj_get_path_expr (get_path_expr_parent (parent
));
3316 adjust_value_for_child_access (&value
, &type
, &was_ptr
, 0);
3318 switch (TYPE_CODE (type
))
3320 case TYPE_CODE_ARRAY
:
3323 = xstrdup (int_string (index
3324 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)),
3327 if (cvalue
&& value
)
3329 int real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
3331 TRY_CATCH (except
, RETURN_MASK_ERROR
)
3333 *cvalue
= value_subscript (value
, real_index
);
3338 *ctype
= get_target_type (type
);
3340 if (cfull_expression
)
3342 xstrprintf ("(%s)[%s]", parent_expression
,
3344 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)),
3350 case TYPE_CODE_STRUCT
:
3351 case TYPE_CODE_UNION
:
3353 const char *field_name
;
3355 /* If the type is anonymous and the field has no name,
3356 set an appropriate name. */
3357 field_name
= TYPE_FIELD_NAME (type
, index
);
3358 if (field_name
== NULL
|| *field_name
== '\0')
3362 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, index
))
3363 == TYPE_CODE_STRUCT
)
3364 *cname
= xstrdup (ANONYMOUS_STRUCT_NAME
);
3366 *cname
= xstrdup (ANONYMOUS_UNION_NAME
);
3369 if (cfull_expression
)
3370 *cfull_expression
= xstrdup ("");
3375 *cname
= xstrdup (field_name
);
3377 if (cfull_expression
)
3379 char *join
= was_ptr
? "->" : ".";
3381 *cfull_expression
= xstrprintf ("(%s)%s%s", parent_expression
,
3386 if (cvalue
&& value
)
3388 /* For C, varobj index is the same as type index. */
3389 *cvalue
= value_struct_element_index (value
, index
);
3393 *ctype
= TYPE_FIELD_TYPE (type
, index
);
3399 *cname
= xstrprintf ("*%s", parent
->name
);
3401 if (cvalue
&& value
)
3403 TRY_CATCH (except
, RETURN_MASK_ERROR
)
3405 *cvalue
= value_ind (value
);
3408 if (except
.reason
< 0)
3412 /* Don't use get_target_type because it calls
3413 check_typedef and here, we want to show the true
3414 declared type of the variable. */
3416 *ctype
= TYPE_TARGET_TYPE (type
);
3418 if (cfull_expression
)
3419 *cfull_expression
= xstrprintf ("*(%s)", parent_expression
);
3424 /* This should not happen. */
3426 *cname
= xstrdup ("???");
3427 if (cfull_expression
)
3428 *cfull_expression
= xstrdup ("???");
3429 /* Don't set value and type, we don't know then. */
3434 c_name_of_child (struct varobj
*parent
, int index
)
3438 c_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
3443 c_path_expr_of_child (struct varobj
*child
)
3445 c_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
3447 return child
->path_expr
;
3450 static struct value
*
3451 c_value_of_child (struct varobj
*parent
, int index
)
3453 struct value
*value
= NULL
;
3455 c_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
3459 static struct type
*
3460 c_type_of_child (struct varobj
*parent
, int index
)
3462 struct type
*type
= NULL
;
3464 c_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
3469 c_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
3471 /* BOGUS: if val_print sees a struct/class, or a reference to one,
3472 it will print out its children instead of "{...}". So we need to
3473 catch that case explicitly. */
3474 struct type
*type
= get_type (var
);
3476 /* Strip top-level references. */
3477 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
3478 type
= check_typedef (TYPE_TARGET_TYPE (type
));
3480 switch (TYPE_CODE (type
))
3482 case TYPE_CODE_STRUCT
:
3483 case TYPE_CODE_UNION
:
3484 return xstrdup ("{...}");
3487 case TYPE_CODE_ARRAY
:
3491 number
= xstrprintf ("[%d]", var
->num_children
);
3498 if (var
->value
== NULL
)
3500 /* This can happen if we attempt to get the value of a struct
3501 member when the parent is an invalid pointer. This is an
3502 error condition, so we should tell the caller. */
3507 if (var
->not_fetched
&& value_lazy (var
->value
))
3508 /* Frozen variable and no value yet. We don't
3509 implicitly fetch the value. MI response will
3510 use empty string for the value, which is OK. */
3513 gdb_assert (varobj_value_is_changeable_p (var
));
3514 gdb_assert (!value_lazy (var
->value
));
3516 /* If the specified format is the current one,
3517 we can reuse print_value. */
3518 if (format
== var
->format
)
3519 return xstrdup (var
->print_value
);
3521 return value_get_print_value (var
->value
, format
, var
);
3531 cplus_number_of_children (struct varobj
*var
)
3533 struct value
*value
= NULL
;
3535 int children
, dont_know
;
3536 int lookup_actual_type
= 0;
3537 struct value_print_options opts
;
3542 get_user_print_options (&opts
);
3544 if (!CPLUS_FAKE_CHILD (var
))
3546 type
= get_value_type (var
);
3548 /* It is necessary to access a real type (via RTTI). */
3549 if (opts
.objectprint
)
3552 lookup_actual_type
= (TYPE_CODE (var
->type
) == TYPE_CODE_REF
3553 || TYPE_CODE (var
->type
) == TYPE_CODE_PTR
);
3555 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
3557 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
) ||
3558 ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
3562 cplus_class_num_children (type
, kids
);
3563 if (kids
[v_public
] != 0)
3565 if (kids
[v_private
] != 0)
3567 if (kids
[v_protected
] != 0)
3570 /* Add any baseclasses. */
3571 children
+= TYPE_N_BASECLASSES (type
);
3574 /* FIXME: save children in var. */
3581 type
= get_value_type (var
->parent
);
3583 /* It is necessary to access a real type (via RTTI). */
3584 if (opts
.objectprint
)
3586 struct varobj
*parent
= var
->parent
;
3588 value
= parent
->value
;
3589 lookup_actual_type
= (TYPE_CODE (parent
->type
) == TYPE_CODE_REF
3590 || TYPE_CODE (parent
->type
) == TYPE_CODE_PTR
);
3592 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
3594 cplus_class_num_children (type
, kids
);
3595 if (strcmp (var
->name
, "public") == 0)
3596 children
= kids
[v_public
];
3597 else if (strcmp (var
->name
, "private") == 0)
3598 children
= kids
[v_private
];
3600 children
= kids
[v_protected
];
3605 children
= c_number_of_children (var
);
3610 /* Compute # of public, private, and protected variables in this class.
3611 That means we need to descend into all baseclasses and find out
3612 how many are there, too. */
3614 cplus_class_num_children (struct type
*type
, int children
[3])
3616 int i
, vptr_fieldno
;
3617 struct type
*basetype
= NULL
;
3619 children
[v_public
] = 0;
3620 children
[v_private
] = 0;
3621 children
[v_protected
] = 0;
3623 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
3624 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
3626 /* If we have a virtual table pointer, omit it. Even if virtual
3627 table pointers are not specifically marked in the debug info,
3628 they should be artificial. */
3629 if ((type
== basetype
&& i
== vptr_fieldno
)
3630 || TYPE_FIELD_ARTIFICIAL (type
, i
))
3633 if (TYPE_FIELD_PROTECTED (type
, i
))
3634 children
[v_protected
]++;
3635 else if (TYPE_FIELD_PRIVATE (type
, i
))
3636 children
[v_private
]++;
3638 children
[v_public
]++;
3643 cplus_name_of_variable (struct varobj
*parent
)
3645 return c_name_of_variable (parent
);
3648 enum accessibility
{ private_field
, protected_field
, public_field
};
3650 /* Check if field INDEX of TYPE has the specified accessibility.
3651 Return 0 if so and 1 otherwise. */
3653 match_accessibility (struct type
*type
, int index
, enum accessibility acc
)
3655 if (acc
== private_field
&& TYPE_FIELD_PRIVATE (type
, index
))
3657 else if (acc
== protected_field
&& TYPE_FIELD_PROTECTED (type
, index
))
3659 else if (acc
== public_field
&& !TYPE_FIELD_PRIVATE (type
, index
)
3660 && !TYPE_FIELD_PROTECTED (type
, index
))
3667 cplus_describe_child (struct varobj
*parent
, int index
,
3668 char **cname
, struct value
**cvalue
, struct type
**ctype
,
3669 char **cfull_expression
)
3671 struct value
*value
;
3674 int lookup_actual_type
= 0;
3675 char *parent_expression
= NULL
;
3677 struct value_print_options opts
;
3685 if (cfull_expression
)
3686 *cfull_expression
= NULL
;
3688 get_user_print_options (&opts
);
3690 var
= (CPLUS_FAKE_CHILD (parent
)) ? parent
->parent
: parent
;
3691 if (opts
.objectprint
)
3692 lookup_actual_type
= (TYPE_CODE (var
->type
) == TYPE_CODE_REF
3693 || TYPE_CODE (var
->type
) == TYPE_CODE_PTR
);
3695 type
= get_value_type (var
);
3696 if (cfull_expression
)
3697 parent_expression
= varobj_get_path_expr (get_path_expr_parent (var
));
3699 adjust_value_for_child_access (&value
, &type
, &was_ptr
, lookup_actual_type
);
3701 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
3702 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
3704 char *join
= was_ptr
? "->" : ".";
3706 if (CPLUS_FAKE_CHILD (parent
))
3708 /* The fields of the class type are ordered as they
3709 appear in the class. We are given an index for a
3710 particular access control type ("public","protected",
3711 or "private"). We must skip over fields that don't
3712 have the access control we are looking for to properly
3713 find the indexed field. */
3714 int type_index
= TYPE_N_BASECLASSES (type
);
3715 enum accessibility acc
= public_field
;
3717 struct type
*basetype
= NULL
;
3718 const char *field_name
;
3720 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
3721 if (strcmp (parent
->name
, "private") == 0)
3722 acc
= private_field
;
3723 else if (strcmp (parent
->name
, "protected") == 0)
3724 acc
= protected_field
;
3728 if ((type
== basetype
&& type_index
== vptr_fieldno
)
3729 || TYPE_FIELD_ARTIFICIAL (type
, type_index
))
3731 else if (match_accessibility (type
, type_index
, acc
))
3737 /* If the type is anonymous and the field has no name,
3738 set an appopriate name. */
3739 field_name
= TYPE_FIELD_NAME (type
, type_index
);
3740 if (field_name
== NULL
|| *field_name
== '\0')
3744 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, type_index
))
3745 == TYPE_CODE_STRUCT
)
3746 *cname
= xstrdup (ANONYMOUS_STRUCT_NAME
);
3747 else if (TYPE_CODE (TYPE_FIELD_TYPE (type
, type_index
))
3749 *cname
= xstrdup (ANONYMOUS_UNION_NAME
);
3752 if (cfull_expression
)
3753 *cfull_expression
= xstrdup ("");
3758 *cname
= xstrdup (TYPE_FIELD_NAME (type
, type_index
));
3760 if (cfull_expression
)
3762 = xstrprintf ("((%s)%s%s)", parent_expression
, join
,
3766 if (cvalue
&& value
)
3767 *cvalue
= value_struct_element_index (value
, type_index
);
3770 *ctype
= TYPE_FIELD_TYPE (type
, type_index
);
3772 else if (index
< TYPE_N_BASECLASSES (type
))
3774 /* This is a baseclass. */
3776 *cname
= xstrdup (TYPE_FIELD_NAME (type
, index
));
3778 if (cvalue
&& value
)
3779 *cvalue
= value_cast (TYPE_FIELD_TYPE (type
, index
), value
);
3783 *ctype
= TYPE_FIELD_TYPE (type
, index
);
3786 if (cfull_expression
)
3788 char *ptr
= was_ptr
? "*" : "";
3790 /* Cast the parent to the base' type. Note that in gdb,
3793 will create an lvalue, for all appearences, so we don't
3794 need to use more fancy:
3798 When we are in the scope of the base class or of one
3799 of its children, the type field name will be interpreted
3800 as a constructor, if it exists. Therefore, we must
3801 indicate that the name is a class name by using the
3802 'class' keyword. See PR mi/11912 */
3803 *cfull_expression
= xstrprintf ("(%s(class %s%s) %s)",
3805 TYPE_FIELD_NAME (type
, index
),
3812 char *access
= NULL
;
3815 cplus_class_num_children (type
, children
);
3817 /* Everything beyond the baseclasses can
3818 only be "public", "private", or "protected"
3820 The special "fake" children are always output by varobj in
3821 this order. So if INDEX == 2, it MUST be "protected". */
3822 index
-= TYPE_N_BASECLASSES (type
);
3826 if (children
[v_public
] > 0)
3828 else if (children
[v_private
] > 0)
3831 access
= "protected";
3834 if (children
[v_public
] > 0)
3836 if (children
[v_private
] > 0)
3839 access
= "protected";
3841 else if (children
[v_private
] > 0)
3842 access
= "protected";
3845 /* Must be protected. */
3846 access
= "protected";
3853 gdb_assert (access
);
3855 *cname
= xstrdup (access
);
3857 /* Value and type and full expression are null here. */
3862 c_describe_child (parent
, index
, cname
, cvalue
, ctype
, cfull_expression
);
3867 cplus_name_of_child (struct varobj
*parent
, int index
)
3871 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
3876 cplus_path_expr_of_child (struct varobj
*child
)
3878 cplus_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
3880 return child
->path_expr
;
3883 static struct value
*
3884 cplus_value_of_child (struct varobj
*parent
, int index
)
3886 struct value
*value
= NULL
;
3888 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
3892 static struct type
*
3893 cplus_type_of_child (struct varobj
*parent
, int index
)
3895 struct type
*type
= NULL
;
3897 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
3902 cplus_value_of_variable (struct varobj
*var
,
3903 enum varobj_display_formats format
)
3906 /* If we have one of our special types, don't print out
3908 if (CPLUS_FAKE_CHILD (var
))
3909 return xstrdup ("");
3911 return c_value_of_variable (var
, format
);
3917 java_number_of_children (struct varobj
*var
)
3919 return cplus_number_of_children (var
);
3923 java_name_of_variable (struct varobj
*parent
)
3927 name
= cplus_name_of_variable (parent
);
3928 /* If the name has "-" in it, it is because we
3929 needed to escape periods in the name... */
3932 while (*p
!= '\000')
3943 java_name_of_child (struct varobj
*parent
, int index
)
3947 name
= cplus_name_of_child (parent
, index
);
3948 /* Escape any periods in the name... */
3951 while (*p
!= '\000')
3962 java_path_expr_of_child (struct varobj
*child
)
3967 static struct value
*
3968 java_value_of_child (struct varobj
*parent
, int index
)
3970 return cplus_value_of_child (parent
, index
);
3973 static struct type
*
3974 java_type_of_child (struct varobj
*parent
, int index
)
3976 return cplus_type_of_child (parent
, index
);
3980 java_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
3982 return cplus_value_of_variable (var
, format
);
3985 /* Ada specific callbacks for VAROBJs. */
3988 ada_number_of_children (struct varobj
*var
)
3990 return ada_varobj_get_number_of_children (var
->value
, var
->type
);
3994 ada_name_of_variable (struct varobj
*parent
)
3996 return c_name_of_variable (parent
);
4000 ada_name_of_child (struct varobj
*parent
, int index
)
4002 return ada_varobj_get_name_of_child (parent
->value
, parent
->type
,
4003 parent
->name
, index
);
4007 ada_path_expr_of_child (struct varobj
*child
)
4009 struct varobj
*parent
= child
->parent
;
4010 const char *parent_path_expr
= varobj_get_path_expr (parent
);
4012 return ada_varobj_get_path_expr_of_child (parent
->value
,
4019 static struct value
*
4020 ada_value_of_child (struct varobj
*parent
, int index
)
4022 return ada_varobj_get_value_of_child (parent
->value
, parent
->type
,
4023 parent
->name
, index
);
4026 static struct type
*
4027 ada_type_of_child (struct varobj
*parent
, int index
)
4029 return ada_varobj_get_type_of_child (parent
->value
, parent
->type
,
4034 ada_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
4036 struct value_print_options opts
;
4038 get_formatted_print_options (&opts
, format_code
[(int) format
]);
4042 return ada_varobj_get_value_of_variable (var
->value
, var
->type
, &opts
);
4045 /* Implement the "value_is_changeable_p" routine for Ada. */
4048 ada_value_is_changeable_p (struct varobj
*var
)
4050 struct type
*type
= var
->value
? value_type (var
->value
) : var
->type
;
4052 if (ada_is_array_descriptor_type (type
)
4053 && TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
4055 /* This is in reality a pointer to an unconstrained array.
4056 its value is changeable. */
4060 if (ada_is_string_type (type
))
4062 /* We display the contents of the string in the array's
4063 "value" field. The contents can change, so consider
4064 that the array is changeable. */
4068 return default_value_is_changeable_p (var
);
4071 /* Implement the "value_has_mutated" routine for Ada. */
4074 ada_value_has_mutated (struct varobj
*var
, struct value
*new_val
,
4075 struct type
*new_type
)
4081 /* If the number of fields have changed, then for sure the type
4083 if (ada_varobj_get_number_of_children (new_val
, new_type
)
4084 != var
->num_children
)
4087 /* If the number of fields have remained the same, then we need
4088 to check the name of each field. If they remain the same,
4089 then chances are the type hasn't mutated. This is technically
4090 an incomplete test, as the child's type might have changed
4091 despite the fact that the name remains the same. But we'll
4092 handle this situation by saying that the child has mutated,
4095 If only part (or none!) of the children have been fetched,
4096 then only check the ones we fetched. It does not matter
4097 to the frontend whether a child that it has not fetched yet
4098 has mutated or not. So just assume it hasn't. */
4100 restrict_range (var
->children
, &from
, &to
);
4101 for (i
= from
; i
< to
; i
++)
4102 if (strcmp (ada_varobj_get_name_of_child (new_val
, new_type
,
4104 VEC_index (varobj_p
, var
->children
, i
)->name
) != 0)
4110 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
4111 with an arbitrary caller supplied DATA pointer. */
4114 all_root_varobjs (void (*func
) (struct varobj
*var
, void *data
), void *data
)
4116 struct varobj_root
*var_root
, *var_root_next
;
4118 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
4120 for (var_root
= rootlist
; var_root
!= NULL
; var_root
= var_root_next
)
4122 var_root_next
= var_root
->next
;
4124 (*func
) (var_root
->rootvar
, data
);
4128 extern void _initialize_varobj (void);
4130 _initialize_varobj (void)
4132 int sizeof_table
= sizeof (struct vlist
*) * VAROBJ_TABLE_SIZE
;
4134 varobj_table
= xmalloc (sizeof_table
);
4135 memset (varobj_table
, 0, sizeof_table
);
4137 add_setshow_zuinteger_cmd ("debugvarobj", class_maintenance
,
4139 _("Set varobj debugging."),
4140 _("Show varobj debugging."),
4141 _("When non-zero, varobj debugging is enabled."),
4142 NULL
, show_varobjdebug
,
4143 &setlist
, &showlist
);
4146 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
4147 defined on globals. It is a helper for varobj_invalidate.
4149 This function is called after changing the symbol file, in this case the
4150 pointers to "struct type" stored by the varobj are no longer valid. All
4151 varobj must be either re-evaluated, or marked as invalid here. */
4154 varobj_invalidate_iter (struct varobj
*var
, void *unused
)
4156 /* global and floating var must be re-evaluated. */
4157 if (var
->root
->floating
|| var
->root
->valid_block
== NULL
)
4159 struct varobj
*tmp_var
;
4161 /* Try to create a varobj with same expression. If we succeed
4162 replace the old varobj, otherwise invalidate it. */
4163 tmp_var
= varobj_create (NULL
, var
->name
, (CORE_ADDR
) 0,
4165 if (tmp_var
!= NULL
)
4167 tmp_var
->obj_name
= xstrdup (var
->obj_name
);
4168 varobj_delete (var
, NULL
, 0);
4169 install_variable (tmp_var
);
4172 var
->root
->is_valid
= 0;
4174 else /* locals must be invalidated. */
4175 var
->root
->is_valid
= 0;
4178 /* Invalidate the varobjs that are tied to locals and re-create the ones that
4179 are defined on globals.
4180 Invalidated varobjs will be always printed in_scope="invalid". */
4183 varobj_invalidate (void)
4185 all_root_varobjs (varobj_invalidate_iter
, NULL
);