1 /* varobj support for C and C++.
3 Copyright (C) 1999-2024 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbthread.h"
23 static void cplus_class_num_children (struct type
*type
, int children
[3]);
25 /* The names of varobjs representing anonymous structs or unions. */
26 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
27 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
29 /* Does CHILD represent a child with no name? This happens when
30 the child is an anonymous struct or union and it has no field name
31 in its parent variable.
33 This has already been determined by *_describe_child. The easiest
34 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
37 varobj_is_anonymous_child (const struct varobj
*child
)
39 return (child
->name
== ANONYMOUS_STRUCT_NAME
40 || child
->name
== ANONYMOUS_UNION_NAME
);
43 /* Given the value and the type of a variable object,
44 adjust the value and type to those necessary
45 for getting children of the variable object.
46 This includes dereferencing top-level references
47 to all types and dereferencing pointers to
50 If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
51 value will be fetched and if it differs from static type
52 the value will be casted to it.
54 Both TYPE and *TYPE should be non-null. VALUE
55 can be null if we want to only translate type.
56 *VALUE can be null as well -- if the parent
59 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
60 depending on whether pointer was dereferenced
64 adjust_value_for_child_access (struct value
**value
,
67 int lookup_actual_type
)
69 gdb_assert (type
&& *type
);
74 *type
= check_typedef (*type
);
76 /* The type of value stored in varobj, that is passed
77 to us, is already supposed to be
78 reference-stripped. */
80 gdb_assert (!TYPE_IS_REFERENCE (*type
));
82 /* Pointers to structures are treated just like
83 structures when accessing children. Don't
84 dereference pointers to other types. */
85 if ((*type
)->code () == TYPE_CODE_PTR
)
87 struct type
*target_type
= get_target_type (*type
);
88 if (target_type
->code () == TYPE_CODE_STRUCT
89 || target_type
->code () == TYPE_CODE_UNION
)
96 *value
= value_ind (*value
);
99 catch (const gdb_exception_error
&except
)
110 /* The 'get_target_type' function calls check_typedef on
111 result, so we can immediately check type code. No
112 need to call check_typedef here. */
114 /* Access a real type of the value (if necessary and possible). */
115 if (value
&& *value
&& lookup_actual_type
)
117 struct type
*enclosing_type
;
118 int real_type_found
= 0;
120 enclosing_type
= value_actual_type (*value
, 1, &real_type_found
);
123 *type
= enclosing_type
;
124 *value
= value_cast (enclosing_type
, *value
);
129 /* Is VAR a path expression parent, i.e., can it be used to construct
130 a valid path expression? */
133 c_is_path_expr_parent (const struct varobj
*var
)
137 /* "Fake" children are not path_expr parents. */
138 if (CPLUS_FAKE_CHILD (var
))
141 type
= varobj_get_gdb_type (var
);
143 /* Anonymous unions and structs are also not path_expr parents. */
144 if ((type
->code () == TYPE_CODE_STRUCT
145 || type
->code () == TYPE_CODE_UNION
)
146 && type
->name () == NULL
)
148 const struct varobj
*parent
= var
->parent
;
150 while (parent
!= NULL
&& CPLUS_FAKE_CHILD (parent
))
151 parent
= parent
->parent
;
155 struct type
*parent_type
;
158 parent_type
= varobj_get_value_type (parent
);
159 adjust_value_for_child_access (NULL
, &parent_type
, &was_ptr
, 0);
161 if (parent_type
->code () == TYPE_CODE_STRUCT
162 || parent_type
->code () == TYPE_CODE_UNION
)
164 const char *field_name
;
166 gdb_assert (var
->index
< parent_type
->num_fields ());
167 field_name
= parent_type
->field (var
->index
).name ();
168 return !(field_name
== NULL
|| *field_name
== '\0');
181 c_number_of_children (const struct varobj
*var
)
183 struct type
*type
= varobj_get_value_type (var
);
187 adjust_value_for_child_access (NULL
, &type
, NULL
, 0);
188 target
= get_target_type (type
);
190 switch (type
->code ())
192 case TYPE_CODE_ARRAY
:
193 if (type
->length () > 0 && target
->length () > 0
194 && type
->bounds ()->high
.is_available ())
195 children
= type
->length () / target
->length ();
197 /* If we don't know how many elements there are, don't display
202 case TYPE_CODE_STRUCT
:
203 case TYPE_CODE_UNION
:
204 children
= type
->num_fields ();
208 /* The type here is a pointer to non-struct. Typically, pointers
209 have one child, except for function ptrs, which have no children,
210 and except for void*, as we don't know what to show.
212 We can show char* so we allow it to be dereferenced. If you decide
213 to test for it, please mind that a little magic is necessary to
214 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
215 TYPE_NAME == "char". */
216 if (target
->code () == TYPE_CODE_FUNC
217 || target
->code () == TYPE_CODE_VOID
)
224 /* Other types have no children. */
232 c_name_of_variable (const struct varobj
*parent
)
237 /* Return the value of element TYPE_INDEX of a structure
238 value VALUE. VALUE's type should be a structure,
239 or union, or a typedef to struct/union.
241 Returns NULL if getting the value fails. Never throws. */
243 static struct value
*
244 value_struct_element_index (struct value
*value
, int type_index
)
246 struct value
*result
= NULL
;
247 struct type
*type
= value
->type ();
249 type
= check_typedef (type
);
251 gdb_assert (type
->code () == TYPE_CODE_STRUCT
252 || type
->code () == TYPE_CODE_UNION
);
256 if (type
->field (type_index
).is_static ())
257 result
= value_static_field (type
, type_index
);
259 result
= value
->primitive_field (0, type_index
, type
);
261 catch (const gdb_exception_error
&e
)
269 /* Obtain the information about child INDEX of the variable
271 If CNAME is not null, sets *CNAME to the name of the child relative
273 If CVALUE is not null, sets *CVALUE to the value of the child.
274 If CTYPE is not null, sets *CTYPE to the type of the child.
276 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
277 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
281 c_describe_child (const struct varobj
*parent
, int index
,
282 std::string
*cname
, struct value
**cvalue
,
283 struct type
**ctype
, std::string
*cfull_expression
)
285 struct value
*value
= parent
->value
.get ();
286 struct type
*type
= varobj_get_value_type (parent
);
287 std::string parent_expression
;
291 *cname
= std::string ();
296 if (cfull_expression
)
298 *cfull_expression
= std::string ();
300 = varobj_get_path_expr (varobj_get_path_expr_parent (parent
));
302 adjust_value_for_child_access (&value
, &type
, &was_ptr
, 0);
304 switch (type
->code ())
306 case TYPE_CODE_ARRAY
:
308 *cname
= int_string (index
+ type
->bounds ()->low
.const_val (),
314 = index
+ type
->bounds ()->low
.const_val ();
318 *cvalue
= value_subscript (value
, real_index
);
320 catch (const gdb_exception_error
&except
)
326 *ctype
= get_target_type (type
);
328 if (cfull_expression
)
329 *cfull_expression
= string_printf
330 ("(%s)[%s]", parent_expression
.c_str (),
331 int_string (index
+ type
->bounds ()->low
.const_val (),
336 case TYPE_CODE_STRUCT
:
337 case TYPE_CODE_UNION
:
339 const char *field_name
;
341 /* If the type is anonymous and the field has no name,
342 set an appropriate name. */
343 field_name
= type
->field (index
).name ();
344 if (field_name
== NULL
|| *field_name
== '\0')
348 if (type
->field (index
).type ()->code ()
350 *cname
= ANONYMOUS_STRUCT_NAME
;
352 *cname
= ANONYMOUS_UNION_NAME
;
355 if (cfull_expression
)
356 *cfull_expression
= "";
363 if (cfull_expression
)
365 const char *join
= was_ptr
? "->" : ".";
367 *cfull_expression
= string_printf ("(%s)%s%s",
368 parent_expression
.c_str (),
375 /* For C, varobj index is the same as type index. */
376 *cvalue
= value_struct_element_index (value
, index
);
380 *ctype
= type
->field (index
).type ();
386 *cname
= string_printf ("*%s", parent
->name
.c_str ());
392 *cvalue
= value_ind (value
);
395 catch (const gdb_exception_error
&except
)
401 /* Don't use get_target_type because it calls
402 check_typedef and here, we want to show the true
403 declared type of the variable. */
405 *ctype
= type
->target_type ();
407 if (cfull_expression
)
408 *cfull_expression
= string_printf ("*(%s)", parent_expression
.c_str ());
412 /* This should not happen. */
415 if (cfull_expression
)
416 *cfull_expression
= "???";
417 /* Don't set value and type, we don't know then. */
422 c_name_of_child (const struct varobj
*parent
, int index
)
426 c_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
431 c_path_expr_of_child (const struct varobj
*child
)
433 std::string path_expr
;
435 c_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
440 static struct value
*
441 c_value_of_child (const struct varobj
*parent
, int index
)
443 struct value
*value
= NULL
;
445 c_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
450 c_type_of_child (const struct varobj
*parent
, int index
)
452 struct type
*type
= NULL
;
454 c_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
458 /* This returns the type of the variable. It also skips past typedefs
459 to return the real type of the variable. */
462 get_type (const struct varobj
*var
)
468 type
= check_typedef (type
);
474 c_value_of_variable (const struct varobj
*var
,
475 enum varobj_display_formats format
)
477 /* BOGUS: if val_print sees a struct/class, or a reference to one,
478 it will print out its children instead of "{...}". So we need to
479 catch that case explicitly. */
480 struct type
*type
= get_type (var
);
482 /* Strip top-level references. */
483 while (TYPE_IS_REFERENCE (type
))
484 type
= check_typedef (type
->target_type ());
486 switch (type
->code ())
488 case TYPE_CODE_STRUCT
:
489 case TYPE_CODE_UNION
:
493 case TYPE_CODE_ARRAY
:
494 return string_printf ("[%d]", var
->num_children
);
499 if (var
->value
== NULL
)
501 /* This can happen if we attempt to get the value of a struct
502 member when the parent is an invalid pointer. This is an
503 error condition, so we should tell the caller. */
504 return std::string ();
508 if (var
->not_fetched
&& var
->value
->lazy ())
509 /* Frozen variable and no value yet. We don't
510 implicitly fetch the value. MI response will
511 use empty string for the value, which is OK. */
512 return std::string ();
514 gdb_assert (varobj_value_is_changeable_p (var
));
515 gdb_assert (!var
->value
->lazy ());
517 /* If the specified format is the current one,
518 we can reuse print_value. */
519 if (format
== var
->format
)
520 return var
->print_value
;
522 return varobj_value_get_print_value (var
->value
.get (), format
,
530 /* varobj operations for c. */
532 const struct lang_varobj_ops c_varobj_ops
=
534 c_number_of_children
,
537 c_path_expr_of_child
,
541 varobj_default_value_is_changeable_p
,
542 NULL
, /* value_has_mutated */
543 c_is_path_expr_parent
/* is_path_expr_parent */
546 /* A little convenience enum for dealing with C++. */
549 v_public
= 0, v_private
, v_protected
555 cplus_number_of_children (const struct varobj
*var
)
557 struct value
*value
= NULL
;
559 int children
, dont_know
;
560 int lookup_actual_type
= 0;
561 struct value_print_options opts
;
566 get_user_print_options (&opts
);
568 if (!CPLUS_FAKE_CHILD (var
))
570 type
= varobj_get_value_type (var
);
572 /* It is necessary to access a real type (via RTTI). */
573 if (opts
.objectprint
)
575 value
= var
->value
.get ();
576 lookup_actual_type
= var
->type
->is_pointer_or_reference ();
578 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
580 if (((type
->code ()) == TYPE_CODE_STRUCT
)
581 || ((type
->code ()) == TYPE_CODE_UNION
))
585 cplus_class_num_children (type
, kids
);
586 if (kids
[v_public
] != 0)
588 if (kids
[v_private
] != 0)
590 if (kids
[v_protected
] != 0)
593 /* Add any baseclasses. */
594 children
+= TYPE_N_BASECLASSES (type
);
597 /* FIXME: save children in var. */
604 type
= varobj_get_value_type (var
->parent
);
606 /* It is necessary to access a real type (via RTTI). */
607 if (opts
.objectprint
)
609 const struct varobj
*parent
= var
->parent
;
611 value
= parent
->value
.get ();
612 lookup_actual_type
= parent
->type
->is_pointer_or_reference ();
614 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
616 cplus_class_num_children (type
, kids
);
617 if (var
->name
== "public")
618 children
= kids
[v_public
];
619 else if (var
->name
== "private")
620 children
= kids
[v_private
];
622 children
= kids
[v_protected
];
627 children
= c_number_of_children (var
);
632 /* Compute # of public, private, and protected variables in this class.
633 That means we need to descend into all baseclasses and find out
634 how many are there, too. */
637 cplus_class_num_children (struct type
*type
, int children
[3])
640 struct type
*basetype
= NULL
;
642 children
[v_public
] = 0;
643 children
[v_private
] = 0;
644 children
[v_protected
] = 0;
646 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
647 for (i
= TYPE_N_BASECLASSES (type
); i
< type
->num_fields (); i
++)
649 field
&fld
= type
->field (i
);
651 /* If we have a virtual table pointer, omit it. Even if virtual
652 table pointers are not specifically marked in the debug info,
653 they should be artificial. */
654 if ((type
== basetype
&& i
== vptr_fieldno
)
655 || fld
.is_artificial ())
658 if (fld
.is_protected ())
659 children
[v_protected
]++;
660 else if (fld
.is_private ())
661 children
[v_private
]++;
663 children
[v_public
]++;
668 cplus_name_of_variable (const struct varobj
*parent
)
670 return c_name_of_variable (parent
);
674 cplus_describe_child (const struct varobj
*parent
, int index
,
675 std::string
*cname
, struct value
**cvalue
, struct type
**ctype
,
676 std::string
*cfull_expression
)
681 int lookup_actual_type
= 0;
682 const char *parent_expression
= NULL
;
683 const struct varobj
*var
;
684 struct value_print_options opts
;
687 *cname
= std::string ();
692 if (cfull_expression
)
693 *cfull_expression
= std::string ();
695 get_user_print_options (&opts
);
697 var
= (CPLUS_FAKE_CHILD (parent
)) ? parent
->parent
: parent
;
698 if (opts
.objectprint
)
699 lookup_actual_type
= var
->type
->is_pointer_or_reference ();
700 value
= var
->value
.get ();
701 type
= varobj_get_value_type (var
);
702 if (cfull_expression
)
704 = varobj_get_path_expr (varobj_get_path_expr_parent (var
));
706 adjust_value_for_child_access (&value
, &type
, &was_ptr
, lookup_actual_type
);
708 if (type
->code () == TYPE_CODE_STRUCT
709 || type
->code () == TYPE_CODE_UNION
)
711 const char *join
= was_ptr
? "->" : ".";
713 if (CPLUS_FAKE_CHILD (parent
))
715 /* The fields of the class type are ordered as they
716 appear in the class. We are given an index for a
717 particular access control type ("public","protected",
718 or "private"). We must skip over fields that don't
719 have the access control we are looking for to properly
720 find the indexed field. */
721 int type_index
= TYPE_N_BASECLASSES (type
);
722 enum accessibility acc
= accessibility::PUBLIC
;
724 struct type
*basetype
= NULL
;
725 const char *field_name
;
727 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
728 if (parent
->name
== "private")
729 acc
= accessibility::PRIVATE
;
730 else if (parent
->name
== "protected")
731 acc
= accessibility::PROTECTED
;
735 if ((type
== basetype
&& type_index
== vptr_fieldno
)
736 || type
->field (type_index
).is_artificial ())
738 else if (type
->field (type_index
).accessibility () == acc
)
744 /* If the type is anonymous and the field has no name,
745 set an appropriate name. */
746 field_name
= type
->field (type_index
).name ();
747 if (field_name
== NULL
|| *field_name
== '\0')
751 if (type
->field (type_index
).type ()->code ()
753 *cname
= ANONYMOUS_STRUCT_NAME
;
754 else if (type
->field (type_index
).type ()->code ()
756 *cname
= ANONYMOUS_UNION_NAME
;
759 if (cfull_expression
)
760 *cfull_expression
= std::string ();
765 *cname
= type
->field (type_index
).name ();
767 if (cfull_expression
)
769 = string_printf ("((%s)%s%s)", parent_expression
, join
,
774 *cvalue
= value_struct_element_index (value
, type_index
);
777 *ctype
= type
->field (type_index
).type ();
779 else if (index
< TYPE_N_BASECLASSES (type
))
781 /* This is a baseclass. */
783 *cname
= type
->field (index
).name ();
786 *cvalue
= value_cast (type
->field (index
).type (), value
);
790 *ctype
= type
->field (index
).type ();
793 if (cfull_expression
)
795 const char *ptr
= was_ptr
? "*" : "";
797 /* Cast the parent to the base' type. Note that in gdb,
800 will create an lvalue, for all appearances, so we don't
801 need to use more fancy:
805 When we are in the scope of the base class or of one
806 of its children, the type field name will be interpreted
807 as a constructor, if it exists. Therefore, we must
808 indicate that the name is a class name by using the
809 'class' keyword. See PR mi/11912 */
810 *cfull_expression
= string_printf ("(%s(class %s%s) %s)",
812 type
->field (index
).name (),
819 const char *access
= NULL
;
822 cplus_class_num_children (type
, children
);
824 /* Everything beyond the baseclasses can
825 only be "public", "private", or "protected"
827 The special "fake" children are always output by varobj in
828 this order. So if INDEX == 2, it MUST be "protected". */
829 index
-= TYPE_N_BASECLASSES (type
);
833 if (children
[v_public
] > 0)
835 else if (children
[v_private
] > 0)
838 access
= "protected";
841 if (children
[v_public
] > 0)
843 if (children
[v_private
] > 0)
846 access
= "protected";
848 else if (children
[v_private
] > 0)
849 access
= "protected";
852 /* Must be protected. */
853 access
= "protected";
864 /* Value and type and full expression are null here. */
869 c_describe_child (parent
, index
, cname
, cvalue
, ctype
, cfull_expression
);
874 cplus_name_of_child (const struct varobj
*parent
, int index
)
878 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
883 cplus_path_expr_of_child (const struct varobj
*child
)
885 std::string path_expr
;
887 cplus_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
892 static struct value
*
893 cplus_value_of_child (const struct varobj
*parent
, int index
)
895 struct value
*value
= NULL
;
897 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
902 cplus_type_of_child (const struct varobj
*parent
, int index
)
904 struct type
*type
= NULL
;
906 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
911 cplus_value_of_variable (const struct varobj
*var
,
912 enum varobj_display_formats format
)
915 /* If we have one of our special types, don't print out
917 if (CPLUS_FAKE_CHILD (var
))
918 return std::string ();
920 return c_value_of_variable (var
, format
);
924 /* varobj operations for c++. */
926 const struct lang_varobj_ops cplus_varobj_ops
=
928 cplus_number_of_children
,
929 cplus_name_of_variable
,
931 cplus_path_expr_of_child
,
932 cplus_value_of_child
,
934 cplus_value_of_variable
,
935 varobj_default_value_is_changeable_p
,
936 NULL
, /* value_has_mutated */
937 c_is_path_expr_parent
/* is_path_expr_parent */