1 /* varobj support for C and C++.
3 Copyright (C) 1999-2019 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 /* Local non-gdb includes. */
21 #include "gdbthread.h"
26 static void cplus_class_num_children (struct type
*type
, int children
[3]);
28 /* The names of varobjs representing anonymous structs or unions. */
29 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
30 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
32 /* Does CHILD represent a child with no name? This happens when
33 the child is an anonmous struct or union and it has no field name
34 in its parent variable.
36 This has already been determined by *_describe_child. The easiest
37 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
40 varobj_is_anonymous_child (const struct varobj
*child
)
42 return (child
->name
== ANONYMOUS_STRUCT_NAME
43 || child
->name
== ANONYMOUS_UNION_NAME
);
46 /* Given the value and the type of a variable object,
47 adjust the value and type to those necessary
48 for getting children of the variable object.
49 This includes dereferencing top-level references
50 to all types and dereferencing pointers to
53 If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
54 value will be fetched and if it differs from static type
55 the value will be casted to it.
57 Both TYPE and *TYPE should be non-null. VALUE
58 can be null if we want to only translate type.
59 *VALUE can be null as well -- if the parent
62 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
63 depending on whether pointer was dereferenced
67 adjust_value_for_child_access (struct value
**value
,
70 int lookup_actual_type
)
72 gdb_assert (type
&& *type
);
77 *type
= check_typedef (*type
);
79 /* The type of value stored in varobj, that is passed
80 to us, is already supposed to be
81 reference-stripped. */
83 gdb_assert (!TYPE_IS_REFERENCE (*type
));
85 /* Pointers to structures are treated just like
86 structures when accessing children. Don't
87 dererences pointers to other types. */
88 if (TYPE_CODE (*type
) == TYPE_CODE_PTR
)
90 struct type
*target_type
= get_target_type (*type
);
91 if (TYPE_CODE (target_type
) == TYPE_CODE_STRUCT
92 || TYPE_CODE (target_type
) == TYPE_CODE_UNION
)
99 *value
= value_ind (*value
);
102 CATCH (except
, RETURN_MASK_ERROR
)
114 /* The 'get_target_type' function calls check_typedef on
115 result, so we can immediately check type code. No
116 need to call check_typedef here. */
118 /* Access a real type of the value (if necessary and possible). */
119 if (value
&& *value
&& lookup_actual_type
)
121 struct type
*enclosing_type
;
122 int real_type_found
= 0;
124 enclosing_type
= value_actual_type (*value
, 1, &real_type_found
);
127 *type
= enclosing_type
;
128 *value
= value_cast (enclosing_type
, *value
);
133 /* Is VAR a path expression parent, i.e., can it be used to construct
134 a valid path expression? */
137 c_is_path_expr_parent (const struct varobj
*var
)
141 /* "Fake" children are not path_expr parents. */
142 if (CPLUS_FAKE_CHILD (var
))
145 type
= varobj_get_gdb_type (var
);
147 /* Anonymous unions and structs are also not path_expr parents. */
148 if ((TYPE_CODE (type
) == TYPE_CODE_STRUCT
149 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
150 && TYPE_NAME (type
) == NULL
)
152 const struct varobj
*parent
= var
->parent
;
154 while (parent
!= NULL
&& CPLUS_FAKE_CHILD (parent
))
155 parent
= parent
->parent
;
159 struct type
*parent_type
;
162 parent_type
= varobj_get_value_type (parent
);
163 adjust_value_for_child_access (NULL
, &parent_type
, &was_ptr
, 0);
165 if (TYPE_CODE (parent_type
) == TYPE_CODE_STRUCT
166 || TYPE_CODE (parent_type
) == TYPE_CODE_UNION
)
168 const char *field_name
;
170 gdb_assert (var
->index
< TYPE_NFIELDS (parent_type
));
171 field_name
= TYPE_FIELD_NAME (parent_type
, var
->index
);
172 return !(field_name
== NULL
|| *field_name
== '\0');
185 c_number_of_children (const struct varobj
*var
)
187 struct type
*type
= varobj_get_value_type (var
);
191 adjust_value_for_child_access (NULL
, &type
, NULL
, 0);
192 target
= get_target_type (type
);
194 switch (TYPE_CODE (type
))
196 case TYPE_CODE_ARRAY
:
197 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
198 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
199 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
201 /* If we don't know how many elements there are, don't display
206 case TYPE_CODE_STRUCT
:
207 case TYPE_CODE_UNION
:
208 children
= TYPE_NFIELDS (type
);
212 /* The type here is a pointer to non-struct. Typically, pointers
213 have one child, except for function ptrs, which have no children,
214 and except for void*, as we don't know what to show.
216 We can show char* so we allow it to be dereferenced. If you decide
217 to test for it, please mind that a little magic is necessary to
218 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
219 TYPE_NAME == "char". */
220 if (TYPE_CODE (target
) == TYPE_CODE_FUNC
221 || TYPE_CODE (target
) == TYPE_CODE_VOID
)
228 /* Other types have no children. */
236 c_name_of_variable (const struct varobj
*parent
)
241 /* Return the value of element TYPE_INDEX of a structure
242 value VALUE. VALUE's type should be a structure,
243 or union, or a typedef to struct/union.
245 Returns NULL if getting the value fails. Never throws. */
247 static struct value
*
248 value_struct_element_index (struct value
*value
, int type_index
)
250 struct value
*result
= NULL
;
251 struct type
*type
= value_type (value
);
253 type
= check_typedef (type
);
255 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
256 || TYPE_CODE (type
) == TYPE_CODE_UNION
);
260 if (field_is_static (&TYPE_FIELD (type
, type_index
)))
261 result
= value_static_field (type
, type_index
);
263 result
= value_primitive_field (value
, 0, type_index
, type
);
265 CATCH (e
, RETURN_MASK_ERROR
)
274 /* Obtain the information about child INDEX of the variable
276 If CNAME is not null, sets *CNAME to the name of the child relative
278 If CVALUE is not null, sets *CVALUE to the value of the child.
279 If CTYPE is not null, sets *CTYPE to the type of the child.
281 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
282 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
286 c_describe_child (const struct varobj
*parent
, int index
,
287 std::string
*cname
, struct value
**cvalue
,
288 struct type
**ctype
, std::string
*cfull_expression
)
290 struct value
*value
= parent
->value
.get ();
291 struct type
*type
= varobj_get_value_type (parent
);
292 std::string parent_expression
;
296 *cname
= std::string ();
301 if (cfull_expression
)
303 *cfull_expression
= std::string ();
305 = varobj_get_path_expr (varobj_get_path_expr_parent (parent
));
307 adjust_value_for_child_access (&value
, &type
, &was_ptr
, 0);
309 switch (TYPE_CODE (type
))
311 case TYPE_CODE_ARRAY
:
313 *cname
= int_string (index
314 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)),
319 int real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
323 *cvalue
= value_subscript (value
, real_index
);
325 CATCH (except
, RETURN_MASK_ERROR
)
332 *ctype
= get_target_type (type
);
334 if (cfull_expression
)
336 string_printf ("(%s)[%s]", parent_expression
.c_str (),
338 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)),
344 case TYPE_CODE_STRUCT
:
345 case TYPE_CODE_UNION
:
347 const char *field_name
;
349 /* If the type is anonymous and the field has no name,
350 set an appropriate name. */
351 field_name
= TYPE_FIELD_NAME (type
, index
);
352 if (field_name
== NULL
|| *field_name
== '\0')
356 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, index
))
358 *cname
= ANONYMOUS_STRUCT_NAME
;
360 *cname
= ANONYMOUS_UNION_NAME
;
363 if (cfull_expression
)
364 *cfull_expression
= "";
371 if (cfull_expression
)
373 const char *join
= was_ptr
? "->" : ".";
375 *cfull_expression
= string_printf ("(%s)%s%s",
376 parent_expression
.c_str (),
383 /* For C, varobj index is the same as type index. */
384 *cvalue
= value_struct_element_index (value
, index
);
388 *ctype
= TYPE_FIELD_TYPE (type
, index
);
394 *cname
= string_printf ("*%s", parent
->name
.c_str ());
400 *cvalue
= value_ind (value
);
403 CATCH (except
, RETURN_MASK_ERROR
)
410 /* Don't use get_target_type because it calls
411 check_typedef and here, we want to show the true
412 declared type of the variable. */
414 *ctype
= TYPE_TARGET_TYPE (type
);
416 if (cfull_expression
)
417 *cfull_expression
= string_printf ("*(%s)", parent_expression
.c_str ());
421 /* This should not happen. */
424 if (cfull_expression
)
425 *cfull_expression
= "???";
426 /* Don't set value and type, we don't know then. */
431 c_name_of_child (const struct varobj
*parent
, int index
)
435 c_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
440 c_path_expr_of_child (const struct varobj
*child
)
442 std::string path_expr
;
444 c_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
449 static struct value
*
450 c_value_of_child (const struct varobj
*parent
, int index
)
452 struct value
*value
= NULL
;
454 c_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
459 c_type_of_child (const struct varobj
*parent
, int index
)
461 struct type
*type
= NULL
;
463 c_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
467 /* This returns the type of the variable. It also skips past typedefs
468 to return the real type of the variable. */
471 get_type (const struct varobj
*var
)
477 type
= check_typedef (type
);
483 c_value_of_variable (const struct varobj
*var
,
484 enum varobj_display_formats format
)
486 /* BOGUS: if val_print sees a struct/class, or a reference to one,
487 it will print out its children instead of "{...}". So we need to
488 catch that case explicitly. */
489 struct type
*type
= get_type (var
);
491 /* Strip top-level references. */
492 while (TYPE_IS_REFERENCE (type
))
493 type
= check_typedef (TYPE_TARGET_TYPE (type
));
495 switch (TYPE_CODE (type
))
497 case TYPE_CODE_STRUCT
:
498 case TYPE_CODE_UNION
:
502 case TYPE_CODE_ARRAY
:
503 return string_printf ("[%d]", var
->num_children
);
508 if (var
->value
== NULL
)
510 /* This can happen if we attempt to get the value of a struct
511 member when the parent is an invalid pointer. This is an
512 error condition, so we should tell the caller. */
513 return std::string ();
517 if (var
->not_fetched
&& value_lazy (var
->value
.get ()))
518 /* Frozen variable and no value yet. We don't
519 implicitly fetch the value. MI response will
520 use empty string for the value, which is OK. */
521 return std::string ();
523 gdb_assert (varobj_value_is_changeable_p (var
));
524 gdb_assert (!value_lazy (var
->value
.get ()));
526 /* If the specified format is the current one,
527 we can reuse print_value. */
528 if (format
== var
->format
)
529 return var
->print_value
;
531 return varobj_value_get_print_value (var
->value
.get (), format
,
539 /* varobj operations for c. */
541 const struct lang_varobj_ops c_varobj_ops
=
543 c_number_of_children
,
546 c_path_expr_of_child
,
550 varobj_default_value_is_changeable_p
,
551 NULL
, /* value_has_mutated */
552 c_is_path_expr_parent
/* is_path_expr_parent */
555 /* A little convenience enum for dealing with C++. */
558 v_public
= 0, v_private
, v_protected
564 cplus_number_of_children (const struct varobj
*var
)
566 struct value
*value
= NULL
;
568 int children
, dont_know
;
569 int lookup_actual_type
= 0;
570 struct value_print_options opts
;
575 get_user_print_options (&opts
);
577 if (!CPLUS_FAKE_CHILD (var
))
579 type
= varobj_get_value_type (var
);
581 /* It is necessary to access a real type (via RTTI). */
582 if (opts
.objectprint
)
584 value
= var
->value
.get ();
585 lookup_actual_type
= (TYPE_IS_REFERENCE (var
->type
)
586 || TYPE_CODE (var
->type
) == TYPE_CODE_PTR
);
588 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
590 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
)
591 || ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
595 cplus_class_num_children (type
, kids
);
596 if (kids
[v_public
] != 0)
598 if (kids
[v_private
] != 0)
600 if (kids
[v_protected
] != 0)
603 /* Add any baseclasses. */
604 children
+= TYPE_N_BASECLASSES (type
);
607 /* FIXME: save children in var. */
614 type
= varobj_get_value_type (var
->parent
);
616 /* It is necessary to access a real type (via RTTI). */
617 if (opts
.objectprint
)
619 const struct varobj
*parent
= var
->parent
;
621 value
= parent
->value
.get ();
622 lookup_actual_type
= (TYPE_IS_REFERENCE (parent
->type
)
623 || TYPE_CODE (parent
->type
) == TYPE_CODE_PTR
);
625 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
627 cplus_class_num_children (type
, kids
);
628 if (var
->name
== "public")
629 children
= kids
[v_public
];
630 else if (var
->name
== "private")
631 children
= kids
[v_private
];
633 children
= kids
[v_protected
];
638 children
= c_number_of_children (var
);
643 /* Compute # of public, private, and protected variables in this class.
644 That means we need to descend into all baseclasses and find out
645 how many are there, too. */
648 cplus_class_num_children (struct type
*type
, int children
[3])
651 struct type
*basetype
= NULL
;
653 children
[v_public
] = 0;
654 children
[v_private
] = 0;
655 children
[v_protected
] = 0;
657 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
658 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
660 /* If we have a virtual table pointer, omit it. Even if virtual
661 table pointers are not specifically marked in the debug info,
662 they should be artificial. */
663 if ((type
== basetype
&& i
== vptr_fieldno
)
664 || TYPE_FIELD_ARTIFICIAL (type
, i
))
667 if (TYPE_FIELD_PROTECTED (type
, i
))
668 children
[v_protected
]++;
669 else if (TYPE_FIELD_PRIVATE (type
, i
))
670 children
[v_private
]++;
672 children
[v_public
]++;
677 cplus_name_of_variable (const struct varobj
*parent
)
679 return c_name_of_variable (parent
);
682 enum accessibility
{ private_field
, protected_field
, public_field
};
684 /* Check if field INDEX of TYPE has the specified accessibility.
685 Return 0 if so and 1 otherwise. */
688 match_accessibility (struct type
*type
, int index
, enum accessibility acc
)
690 if (acc
== private_field
&& TYPE_FIELD_PRIVATE (type
, index
))
692 else if (acc
== protected_field
&& TYPE_FIELD_PROTECTED (type
, index
))
694 else if (acc
== public_field
&& !TYPE_FIELD_PRIVATE (type
, index
)
695 && !TYPE_FIELD_PROTECTED (type
, index
))
702 cplus_describe_child (const struct varobj
*parent
, int index
,
703 std::string
*cname
, struct value
**cvalue
, struct type
**ctype
,
704 std::string
*cfull_expression
)
709 int lookup_actual_type
= 0;
710 const char *parent_expression
= NULL
;
711 const struct varobj
*var
;
712 struct value_print_options opts
;
715 *cname
= std::string ();
720 if (cfull_expression
)
721 *cfull_expression
= std::string ();
723 get_user_print_options (&opts
);
725 var
= (CPLUS_FAKE_CHILD (parent
)) ? parent
->parent
: parent
;
726 if (opts
.objectprint
)
727 lookup_actual_type
= (TYPE_IS_REFERENCE (var
->type
)
728 || TYPE_CODE (var
->type
) == TYPE_CODE_PTR
);
729 value
= var
->value
.get ();
730 type
= varobj_get_value_type (var
);
731 if (cfull_expression
)
733 = varobj_get_path_expr (varobj_get_path_expr_parent (var
));
735 adjust_value_for_child_access (&value
, &type
, &was_ptr
, lookup_actual_type
);
737 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
738 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
740 const char *join
= was_ptr
? "->" : ".";
742 if (CPLUS_FAKE_CHILD (parent
))
744 /* The fields of the class type are ordered as they
745 appear in the class. We are given an index for a
746 particular access control type ("public","protected",
747 or "private"). We must skip over fields that don't
748 have the access control we are looking for to properly
749 find the indexed field. */
750 int type_index
= TYPE_N_BASECLASSES (type
);
751 enum accessibility acc
= public_field
;
753 struct type
*basetype
= NULL
;
754 const char *field_name
;
756 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
757 if (parent
->name
== "private")
759 else if (parent
->name
== "protected")
760 acc
= protected_field
;
764 if ((type
== basetype
&& type_index
== vptr_fieldno
)
765 || TYPE_FIELD_ARTIFICIAL (type
, type_index
))
767 else if (match_accessibility (type
, type_index
, acc
))
773 /* If the type is anonymous and the field has no name,
774 set an appopriate name. */
775 field_name
= TYPE_FIELD_NAME (type
, type_index
);
776 if (field_name
== NULL
|| *field_name
== '\0')
780 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, type_index
))
782 *cname
= ANONYMOUS_STRUCT_NAME
;
783 else if (TYPE_CODE (TYPE_FIELD_TYPE (type
, type_index
))
785 *cname
= ANONYMOUS_UNION_NAME
;
788 if (cfull_expression
)
789 *cfull_expression
= std::string ();
794 *cname
= TYPE_FIELD_NAME (type
, type_index
);
796 if (cfull_expression
)
798 = string_printf ("((%s)%s%s)", parent_expression
, join
,
803 *cvalue
= value_struct_element_index (value
, type_index
);
806 *ctype
= TYPE_FIELD_TYPE (type
, type_index
);
808 else if (index
< TYPE_N_BASECLASSES (type
))
810 /* This is a baseclass. */
812 *cname
= TYPE_FIELD_NAME (type
, index
);
815 *cvalue
= value_cast (TYPE_FIELD_TYPE (type
, index
), value
);
819 *ctype
= TYPE_FIELD_TYPE (type
, index
);
822 if (cfull_expression
)
824 const char *ptr
= was_ptr
? "*" : "";
826 /* Cast the parent to the base' type. Note that in gdb,
829 will create an lvalue, for all appearences, so we don't
830 need to use more fancy:
834 When we are in the scope of the base class or of one
835 of its children, the type field name will be interpreted
836 as a constructor, if it exists. Therefore, we must
837 indicate that the name is a class name by using the
838 'class' keyword. See PR mi/11912 */
839 *cfull_expression
= string_printf ("(%s(class %s%s) %s)",
841 TYPE_FIELD_NAME (type
, index
),
848 const char *access
= NULL
;
851 cplus_class_num_children (type
, children
);
853 /* Everything beyond the baseclasses can
854 only be "public", "private", or "protected"
856 The special "fake" children are always output by varobj in
857 this order. So if INDEX == 2, it MUST be "protected". */
858 index
-= TYPE_N_BASECLASSES (type
);
862 if (children
[v_public
] > 0)
864 else if (children
[v_private
] > 0)
867 access
= "protected";
870 if (children
[v_public
] > 0)
872 if (children
[v_private
] > 0)
875 access
= "protected";
877 else if (children
[v_private
] > 0)
878 access
= "protected";
881 /* Must be protected. */
882 access
= "protected";
893 /* Value and type and full expression are null here. */
898 c_describe_child (parent
, index
, cname
, cvalue
, ctype
, cfull_expression
);
903 cplus_name_of_child (const struct varobj
*parent
, int index
)
907 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
912 cplus_path_expr_of_child (const struct varobj
*child
)
914 std::string path_expr
;
916 cplus_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
921 static struct value
*
922 cplus_value_of_child (const struct varobj
*parent
, int index
)
924 struct value
*value
= NULL
;
926 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
931 cplus_type_of_child (const struct varobj
*parent
, int index
)
933 struct type
*type
= NULL
;
935 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
940 cplus_value_of_variable (const struct varobj
*var
,
941 enum varobj_display_formats format
)
944 /* If we have one of our special types, don't print out
946 if (CPLUS_FAKE_CHILD (var
))
947 return std::string ();
949 return c_value_of_variable (var
, format
);
953 /* varobj operations for c++. */
955 const struct lang_varobj_ops cplus_varobj_ops
=
957 cplus_number_of_children
,
958 cplus_name_of_variable
,
960 cplus_path_expr_of_child
,
961 cplus_value_of_child
,
963 cplus_value_of_variable
,
964 varobj_default_value_is_changeable_p
,
965 NULL
, /* value_has_mutated */
966 c_is_path_expr_parent
/* is_path_expr_parent */