1 /* varobj support for C and C++.
3 Copyright (C) 1999-2023 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/>. */
21 #include "gdbthread.h"
24 static void cplus_class_num_children (struct type
*type
, int children
[3]);
26 /* The names of varobjs representing anonymous structs or unions. */
27 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
28 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
30 /* Does CHILD represent a child with no name? This happens when
31 the child is an anonymous struct or union and it has no field name
32 in its parent variable.
34 This has already been determined by *_describe_child. The easiest
35 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
38 varobj_is_anonymous_child (const struct varobj
*child
)
40 return (child
->name
== ANONYMOUS_STRUCT_NAME
41 || child
->name
== ANONYMOUS_UNION_NAME
);
44 /* Given the value and the type of a variable object,
45 adjust the value and type to those necessary
46 for getting children of the variable object.
47 This includes dereferencing top-level references
48 to all types and dereferencing pointers to
51 If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
52 value will be fetched and if it differs from static type
53 the value will be casted to it.
55 Both TYPE and *TYPE should be non-null. VALUE
56 can be null if we want to only translate type.
57 *VALUE can be null as well -- if the parent
60 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
61 depending on whether pointer was dereferenced
65 adjust_value_for_child_access (struct value
**value
,
68 int lookup_actual_type
)
70 gdb_assert (type
&& *type
);
75 *type
= check_typedef (*type
);
77 /* The type of value stored in varobj, that is passed
78 to us, is already supposed to be
79 reference-stripped. */
81 gdb_assert (!TYPE_IS_REFERENCE (*type
));
83 /* Pointers to structures are treated just like
84 structures when accessing children. Don't
85 dereference pointers to other types. */
86 if ((*type
)->code () == TYPE_CODE_PTR
)
88 struct type
*target_type
= get_target_type (*type
);
89 if (target_type
->code () == TYPE_CODE_STRUCT
90 || target_type
->code () == TYPE_CODE_UNION
)
97 *value
= value_ind (*value
);
100 catch (const gdb_exception_error
&except
)
111 /* The 'get_target_type' function calls check_typedef on
112 result, so we can immediately check type code. No
113 need to call check_typedef here. */
115 /* Access a real type of the value (if necessary and possible). */
116 if (value
&& *value
&& lookup_actual_type
)
118 struct type
*enclosing_type
;
119 int real_type_found
= 0;
121 enclosing_type
= value_actual_type (*value
, 1, &real_type_found
);
124 *type
= enclosing_type
;
125 *value
= value_cast (enclosing_type
, *value
);
130 /* Is VAR a path expression parent, i.e., can it be used to construct
131 a valid path expression? */
134 c_is_path_expr_parent (const struct varobj
*var
)
138 /* "Fake" children are not path_expr parents. */
139 if (CPLUS_FAKE_CHILD (var
))
142 type
= varobj_get_gdb_type (var
);
144 /* Anonymous unions and structs are also not path_expr parents. */
145 if ((type
->code () == TYPE_CODE_STRUCT
146 || type
->code () == TYPE_CODE_UNION
)
147 && type
->name () == NULL
)
149 const struct varobj
*parent
= var
->parent
;
151 while (parent
!= NULL
&& CPLUS_FAKE_CHILD (parent
))
152 parent
= parent
->parent
;
156 struct type
*parent_type
;
159 parent_type
= varobj_get_value_type (parent
);
160 adjust_value_for_child_access (NULL
, &parent_type
, &was_ptr
, 0);
162 if (parent_type
->code () == TYPE_CODE_STRUCT
163 || parent_type
->code () == TYPE_CODE_UNION
)
165 const char *field_name
;
167 gdb_assert (var
->index
< parent_type
->num_fields ());
168 field_name
= parent_type
->field (var
->index
).name ();
169 return !(field_name
== NULL
|| *field_name
== '\0');
182 c_number_of_children (const struct varobj
*var
)
184 struct type
*type
= varobj_get_value_type (var
);
188 adjust_value_for_child_access (NULL
, &type
, NULL
, 0);
189 target
= get_target_type (type
);
191 switch (type
->code ())
193 case TYPE_CODE_ARRAY
:
194 if (type
->length () > 0 && target
->length () > 0
195 && (type
->bounds ()->high
.kind () != PROP_UNDEFINED
))
196 children
= type
->length () / target
->length ();
198 /* If we don't know how many elements there are, don't display
203 case TYPE_CODE_STRUCT
:
204 case TYPE_CODE_UNION
:
205 children
= type
->num_fields ();
209 /* The type here is a pointer to non-struct. Typically, pointers
210 have one child, except for function ptrs, which have no children,
211 and except for void*, as we don't know what to show.
213 We can show char* so we allow it to be dereferenced. If you decide
214 to test for it, please mind that a little magic is necessary to
215 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
216 TYPE_NAME == "char". */
217 if (target
->code () == TYPE_CODE_FUNC
218 || target
->code () == TYPE_CODE_VOID
)
225 /* Other types have no children. */
233 c_name_of_variable (const struct varobj
*parent
)
238 /* Return the value of element TYPE_INDEX of a structure
239 value VALUE. VALUE's type should be a structure,
240 or union, or a typedef to struct/union.
242 Returns NULL if getting the value fails. Never throws. */
244 static struct value
*
245 value_struct_element_index (struct value
*value
, int type_index
)
247 struct value
*result
= NULL
;
248 struct type
*type
= value
->type ();
250 type
= check_typedef (type
);
252 gdb_assert (type
->code () == TYPE_CODE_STRUCT
253 || type
->code () == TYPE_CODE_UNION
);
257 if (type
->field (type_index
).is_static ())
258 result
= value_static_field (type
, type_index
);
260 result
= value
->primitive_field (0, type_index
, type
);
262 catch (const gdb_exception_error
&e
)
270 /* Obtain the information about child INDEX of the variable
272 If CNAME is not null, sets *CNAME to the name of the child relative
274 If CVALUE is not null, sets *CVALUE to the value of the child.
275 If CTYPE is not null, sets *CTYPE to the type of the child.
277 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
278 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
282 c_describe_child (const struct varobj
*parent
, int index
,
283 std::string
*cname
, struct value
**cvalue
,
284 struct type
**ctype
, std::string
*cfull_expression
)
286 struct value
*value
= parent
->value
.get ();
287 struct type
*type
= varobj_get_value_type (parent
);
288 std::string parent_expression
;
292 *cname
= std::string ();
297 if (cfull_expression
)
299 *cfull_expression
= std::string ();
301 = varobj_get_path_expr (varobj_get_path_expr_parent (parent
));
303 adjust_value_for_child_access (&value
, &type
, &was_ptr
, 0);
305 switch (type
->code ())
307 case TYPE_CODE_ARRAY
:
309 *cname
= int_string (index
+ type
->bounds ()->low
.const_val (),
315 = index
+ type
->bounds ()->low
.const_val ();
319 *cvalue
= value_subscript (value
, real_index
);
321 catch (const gdb_exception_error
&except
)
327 *ctype
= get_target_type (type
);
329 if (cfull_expression
)
330 *cfull_expression
= string_printf
331 ("(%s)[%s]", parent_expression
.c_str (),
332 int_string (index
+ type
->bounds ()->low
.const_val (),
337 case TYPE_CODE_STRUCT
:
338 case TYPE_CODE_UNION
:
340 const char *field_name
;
342 /* If the type is anonymous and the field has no name,
343 set an appropriate name. */
344 field_name
= type
->field (index
).name ();
345 if (field_name
== NULL
|| *field_name
== '\0')
349 if (type
->field (index
).type ()->code ()
351 *cname
= ANONYMOUS_STRUCT_NAME
;
353 *cname
= ANONYMOUS_UNION_NAME
;
356 if (cfull_expression
)
357 *cfull_expression
= "";
364 if (cfull_expression
)
366 const char *join
= was_ptr
? "->" : ".";
368 *cfull_expression
= string_printf ("(%s)%s%s",
369 parent_expression
.c_str (),
376 /* For C, varobj index is the same as type index. */
377 *cvalue
= value_struct_element_index (value
, index
);
381 *ctype
= type
->field (index
).type ();
387 *cname
= string_printf ("*%s", parent
->name
.c_str ());
393 *cvalue
= value_ind (value
);
396 catch (const gdb_exception_error
&except
)
402 /* Don't use get_target_type because it calls
403 check_typedef and here, we want to show the true
404 declared type of the variable. */
406 *ctype
= type
->target_type ();
408 if (cfull_expression
)
409 *cfull_expression
= string_printf ("*(%s)", parent_expression
.c_str ());
413 /* This should not happen. */
416 if (cfull_expression
)
417 *cfull_expression
= "???";
418 /* Don't set value and type, we don't know then. */
423 c_name_of_child (const struct varobj
*parent
, int index
)
427 c_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
432 c_path_expr_of_child (const struct varobj
*child
)
434 std::string path_expr
;
436 c_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
441 static struct value
*
442 c_value_of_child (const struct varobj
*parent
, int index
)
444 struct value
*value
= NULL
;
446 c_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
451 c_type_of_child (const struct varobj
*parent
, int index
)
453 struct type
*type
= NULL
;
455 c_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
459 /* This returns the type of the variable. It also skips past typedefs
460 to return the real type of the variable. */
463 get_type (const struct varobj
*var
)
469 type
= check_typedef (type
);
475 c_value_of_variable (const struct varobj
*var
,
476 enum varobj_display_formats format
)
478 /* BOGUS: if val_print sees a struct/class, or a reference to one,
479 it will print out its children instead of "{...}". So we need to
480 catch that case explicitly. */
481 struct type
*type
= get_type (var
);
483 /* Strip top-level references. */
484 while (TYPE_IS_REFERENCE (type
))
485 type
= check_typedef (type
->target_type ());
487 switch (type
->code ())
489 case TYPE_CODE_STRUCT
:
490 case TYPE_CODE_UNION
:
494 case TYPE_CODE_ARRAY
:
495 return string_printf ("[%d]", var
->num_children
);
500 if (var
->value
== NULL
)
502 /* This can happen if we attempt to get the value of a struct
503 member when the parent is an invalid pointer. This is an
504 error condition, so we should tell the caller. */
505 return std::string ();
509 if (var
->not_fetched
&& var
->value
->lazy ())
510 /* Frozen variable and no value yet. We don't
511 implicitly fetch the value. MI response will
512 use empty string for the value, which is OK. */
513 return std::string ();
515 gdb_assert (varobj_value_is_changeable_p (var
));
516 gdb_assert (!var
->value
->lazy ());
518 /* If the specified format is the current one,
519 we can reuse print_value. */
520 if (format
== var
->format
)
521 return var
->print_value
;
523 return varobj_value_get_print_value (var
->value
.get (), format
,
531 /* varobj operations for c. */
533 const struct lang_varobj_ops c_varobj_ops
=
535 c_number_of_children
,
538 c_path_expr_of_child
,
542 varobj_default_value_is_changeable_p
,
543 NULL
, /* value_has_mutated */
544 c_is_path_expr_parent
/* is_path_expr_parent */
547 /* A little convenience enum for dealing with C++. */
550 v_public
= 0, v_private
, v_protected
556 cplus_number_of_children (const struct varobj
*var
)
558 struct value
*value
= NULL
;
560 int children
, dont_know
;
561 int lookup_actual_type
= 0;
562 struct value_print_options opts
;
567 get_user_print_options (&opts
);
569 if (!CPLUS_FAKE_CHILD (var
))
571 type
= varobj_get_value_type (var
);
573 /* It is necessary to access a real type (via RTTI). */
574 if (opts
.objectprint
)
576 value
= var
->value
.get ();
577 lookup_actual_type
= var
->type
->is_pointer_or_reference ();
579 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
581 if (((type
->code ()) == TYPE_CODE_STRUCT
)
582 || ((type
->code ()) == TYPE_CODE_UNION
))
586 cplus_class_num_children (type
, kids
);
587 if (kids
[v_public
] != 0)
589 if (kids
[v_private
] != 0)
591 if (kids
[v_protected
] != 0)
594 /* Add any baseclasses. */
595 children
+= TYPE_N_BASECLASSES (type
);
598 /* FIXME: save children in var. */
605 type
= varobj_get_value_type (var
->parent
);
607 /* It is necessary to access a real type (via RTTI). */
608 if (opts
.objectprint
)
610 const struct varobj
*parent
= var
->parent
;
612 value
= parent
->value
.get ();
613 lookup_actual_type
= parent
->type
->is_pointer_or_reference ();
615 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
617 cplus_class_num_children (type
, kids
);
618 if (var
->name
== "public")
619 children
= kids
[v_public
];
620 else if (var
->name
== "private")
621 children
= kids
[v_private
];
623 children
= kids
[v_protected
];
628 children
= c_number_of_children (var
);
633 /* Compute # of public, private, and protected variables in this class.
634 That means we need to descend into all baseclasses and find out
635 how many are there, too. */
638 cplus_class_num_children (struct type
*type
, int children
[3])
641 struct type
*basetype
= NULL
;
643 children
[v_public
] = 0;
644 children
[v_private
] = 0;
645 children
[v_protected
] = 0;
647 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
648 for (i
= TYPE_N_BASECLASSES (type
); i
< type
->num_fields (); i
++)
650 field
&fld
= type
->field (i
);
652 /* If we have a virtual table pointer, omit it. Even if virtual
653 table pointers are not specifically marked in the debug info,
654 they should be artificial. */
655 if ((type
== basetype
&& i
== vptr_fieldno
)
656 || fld
.is_artificial ())
659 if (fld
.is_protected ())
660 children
[v_protected
]++;
661 else if (fld
.is_private ())
662 children
[v_private
]++;
664 children
[v_public
]++;
669 cplus_name_of_variable (const struct varobj
*parent
)
671 return c_name_of_variable (parent
);
675 cplus_describe_child (const struct varobj
*parent
, int index
,
676 std::string
*cname
, struct value
**cvalue
, struct type
**ctype
,
677 std::string
*cfull_expression
)
682 int lookup_actual_type
= 0;
683 const char *parent_expression
= NULL
;
684 const struct varobj
*var
;
685 struct value_print_options opts
;
688 *cname
= std::string ();
693 if (cfull_expression
)
694 *cfull_expression
= std::string ();
696 get_user_print_options (&opts
);
698 var
= (CPLUS_FAKE_CHILD (parent
)) ? parent
->parent
: parent
;
699 if (opts
.objectprint
)
700 lookup_actual_type
= var
->type
->is_pointer_or_reference ();
701 value
= var
->value
.get ();
702 type
= varobj_get_value_type (var
);
703 if (cfull_expression
)
705 = varobj_get_path_expr (varobj_get_path_expr_parent (var
));
707 adjust_value_for_child_access (&value
, &type
, &was_ptr
, lookup_actual_type
);
709 if (type
->code () == TYPE_CODE_STRUCT
710 || type
->code () == TYPE_CODE_UNION
)
712 const char *join
= was_ptr
? "->" : ".";
714 if (CPLUS_FAKE_CHILD (parent
))
716 /* The fields of the class type are ordered as they
717 appear in the class. We are given an index for a
718 particular access control type ("public","protected",
719 or "private"). We must skip over fields that don't
720 have the access control we are looking for to properly
721 find the indexed field. */
722 int type_index
= TYPE_N_BASECLASSES (type
);
723 enum accessibility acc
= accessibility::PUBLIC
;
725 struct type
*basetype
= NULL
;
726 const char *field_name
;
728 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
729 if (parent
->name
== "private")
730 acc
= accessibility::PRIVATE
;
731 else if (parent
->name
== "protected")
732 acc
= accessibility::PROTECTED
;
736 if ((type
== basetype
&& type_index
== vptr_fieldno
)
737 || type
->field (type_index
).is_artificial ())
739 else if (type
->field (type_index
).accessibility () == acc
)
745 /* If the type is anonymous and the field has no name,
746 set an appropriate name. */
747 field_name
= type
->field (type_index
).name ();
748 if (field_name
== NULL
|| *field_name
== '\0')
752 if (type
->field (type_index
).type ()->code ()
754 *cname
= ANONYMOUS_STRUCT_NAME
;
755 else if (type
->field (type_index
).type ()->code ()
757 *cname
= ANONYMOUS_UNION_NAME
;
760 if (cfull_expression
)
761 *cfull_expression
= std::string ();
766 *cname
= type
->field (type_index
).name ();
768 if (cfull_expression
)
770 = string_printf ("((%s)%s%s)", parent_expression
, join
,
775 *cvalue
= value_struct_element_index (value
, type_index
);
778 *ctype
= type
->field (type_index
).type ();
780 else if (index
< TYPE_N_BASECLASSES (type
))
782 /* This is a baseclass. */
784 *cname
= type
->field (index
).name ();
787 *cvalue
= value_cast (type
->field (index
).type (), value
);
791 *ctype
= type
->field (index
).type ();
794 if (cfull_expression
)
796 const char *ptr
= was_ptr
? "*" : "";
798 /* Cast the parent to the base' type. Note that in gdb,
801 will create an lvalue, for all appearances, so we don't
802 need to use more fancy:
806 When we are in the scope of the base class or of one
807 of its children, the type field name will be interpreted
808 as a constructor, if it exists. Therefore, we must
809 indicate that the name is a class name by using the
810 'class' keyword. See PR mi/11912 */
811 *cfull_expression
= string_printf ("(%s(class %s%s) %s)",
813 type
->field (index
).name (),
820 const char *access
= NULL
;
823 cplus_class_num_children (type
, children
);
825 /* Everything beyond the baseclasses can
826 only be "public", "private", or "protected"
828 The special "fake" children are always output by varobj in
829 this order. So if INDEX == 2, it MUST be "protected". */
830 index
-= TYPE_N_BASECLASSES (type
);
834 if (children
[v_public
] > 0)
836 else if (children
[v_private
] > 0)
839 access
= "protected";
842 if (children
[v_public
] > 0)
844 if (children
[v_private
] > 0)
847 access
= "protected";
849 else if (children
[v_private
] > 0)
850 access
= "protected";
853 /* Must be protected. */
854 access
= "protected";
865 /* Value and type and full expression are null here. */
870 c_describe_child (parent
, index
, cname
, cvalue
, ctype
, cfull_expression
);
875 cplus_name_of_child (const struct varobj
*parent
, int index
)
879 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
884 cplus_path_expr_of_child (const struct varobj
*child
)
886 std::string path_expr
;
888 cplus_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
893 static struct value
*
894 cplus_value_of_child (const struct varobj
*parent
, int index
)
896 struct value
*value
= NULL
;
898 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
903 cplus_type_of_child (const struct varobj
*parent
, int index
)
905 struct type
*type
= NULL
;
907 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
912 cplus_value_of_variable (const struct varobj
*var
,
913 enum varobj_display_formats format
)
916 /* If we have one of our special types, don't print out
918 if (CPLUS_FAKE_CHILD (var
))
919 return std::string ();
921 return c_value_of_variable (var
, format
);
925 /* varobj operations for c++. */
927 const struct lang_varobj_ops cplus_varobj_ops
=
929 cplus_number_of_children
,
930 cplus_name_of_variable
,
932 cplus_path_expr_of_child
,
933 cplus_value_of_child
,
935 cplus_value_of_variable
,
936 varobj_default_value_is_changeable_p
,
937 NULL
, /* value_has_mutated */
938 c_is_path_expr_parent
/* is_path_expr_parent */