1 /* varobj support for C and C++.
3 Copyright (C) 1999-2015 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 anonmous 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 (struct varobj
*child
)
40 return (strcmp (child
->name
, ANONYMOUS_STRUCT_NAME
) == 0
41 || strcmp (child
->name
, ANONYMOUS_UNION_NAME
) == 0);
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_CODE (*type
) != TYPE_CODE_REF
);
83 /* Pointers to structures are treated just like
84 structures when accessing children. Don't
85 dererences pointers to other types. */
86 if (TYPE_CODE (*type
) == TYPE_CODE_PTR
)
88 struct type
*target_type
= get_target_type (*type
);
89 if (TYPE_CODE (target_type
) == TYPE_CODE_STRUCT
90 || TYPE_CODE (target_type
) == TYPE_CODE_UNION
)
94 volatile struct gdb_exception except
;
96 TRY_CATCH (except
, RETURN_MASK_ERROR
)
98 *value
= value_ind (*value
);
101 if (except
.reason
< 0)
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 (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
) == TYPE_CODE_STRUCT
145 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
146 && TYPE_NAME (type
) == NULL
147 && TYPE_TAG_NAME (type
) == NULL
)
149 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 (TYPE_CODE (parent_type
) == TYPE_CODE_STRUCT
163 || TYPE_CODE (parent_type
) == TYPE_CODE_UNION
)
165 const char *field_name
;
167 gdb_assert (var
->index
< TYPE_NFIELDS (parent_type
));
168 field_name
= TYPE_FIELD_NAME (parent_type
, var
->index
);
169 return !(field_name
== NULL
|| *field_name
== '\0');
182 c_number_of_children (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 (type
))
193 case TYPE_CODE_ARRAY
:
194 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
195 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type
))
196 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
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_NFIELDS (type
);
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 (TYPE_CODE (target
) == TYPE_CODE_FUNC
218 || TYPE_CODE (target
) == TYPE_CODE_VOID
)
225 /* Other types have no children. */
233 c_name_of_variable (struct varobj
*parent
)
235 return xstrdup (parent
->name
);
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 volatile struct gdb_exception e
;
249 struct type
*type
= value_type (value
);
251 type
= check_typedef (type
);
253 gdb_assert (TYPE_CODE (type
) == TYPE_CODE_STRUCT
254 || TYPE_CODE (type
) == TYPE_CODE_UNION
);
256 TRY_CATCH (e
, RETURN_MASK_ERROR
)
258 if (field_is_static (&TYPE_FIELD (type
, type_index
)))
259 result
= value_static_field (type
, type_index
);
261 result
= value_primitive_field (value
, 0, type_index
, type
);
273 /* Obtain the information about child INDEX of the variable
275 If CNAME is not null, sets *CNAME to the name of the child relative
277 If CVALUE is not null, sets *CVALUE to the value of the child.
278 If CTYPE is not null, sets *CTYPE to the type of the child.
280 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
281 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
285 c_describe_child (struct varobj
*parent
, int index
,
286 char **cname
, struct value
**cvalue
, struct type
**ctype
,
287 char **cfull_expression
)
289 struct value
*value
= parent
->value
;
290 struct type
*type
= varobj_get_value_type (parent
);
291 char *parent_expression
= NULL
;
293 volatile struct gdb_exception except
;
301 if (cfull_expression
)
303 *cfull_expression
= NULL
;
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
:
314 = xstrdup (int_string (index
315 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)),
320 int real_index
= index
+ TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
));
322 TRY_CATCH (except
, RETURN_MASK_ERROR
)
324 *cvalue
= value_subscript (value
, real_index
);
329 *ctype
= get_target_type (type
);
331 if (cfull_expression
)
333 xstrprintf ("(%s)[%s]", parent_expression
,
335 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type
)),
341 case TYPE_CODE_STRUCT
:
342 case TYPE_CODE_UNION
:
344 const char *field_name
;
346 /* If the type is anonymous and the field has no name,
347 set an appropriate name. */
348 field_name
= TYPE_FIELD_NAME (type
, index
);
349 if (field_name
== NULL
|| *field_name
== '\0')
353 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, index
))
355 *cname
= xstrdup (ANONYMOUS_STRUCT_NAME
);
357 *cname
= xstrdup (ANONYMOUS_UNION_NAME
);
360 if (cfull_expression
)
361 *cfull_expression
= xstrdup ("");
366 *cname
= xstrdup (field_name
);
368 if (cfull_expression
)
370 char *join
= was_ptr
? "->" : ".";
372 *cfull_expression
= xstrprintf ("(%s)%s%s", parent_expression
,
379 /* For C, varobj index is the same as type index. */
380 *cvalue
= value_struct_element_index (value
, index
);
384 *ctype
= TYPE_FIELD_TYPE (type
, index
);
390 *cname
= xstrprintf ("*%s", parent
->name
);
394 TRY_CATCH (except
, RETURN_MASK_ERROR
)
396 *cvalue
= value_ind (value
);
399 if (except
.reason
< 0)
403 /* Don't use get_target_type because it calls
404 check_typedef and here, we want to show the true
405 declared type of the variable. */
407 *ctype
= TYPE_TARGET_TYPE (type
);
409 if (cfull_expression
)
410 *cfull_expression
= xstrprintf ("*(%s)", parent_expression
);
415 /* This should not happen. */
417 *cname
= xstrdup ("???");
418 if (cfull_expression
)
419 *cfull_expression
= xstrdup ("???");
420 /* Don't set value and type, we don't know then. */
425 c_name_of_child (struct varobj
*parent
, int index
)
429 c_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
434 c_path_expr_of_child (struct varobj
*child
)
438 c_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
443 static struct value
*
444 c_value_of_child (struct varobj
*parent
, int index
)
446 struct value
*value
= NULL
;
448 c_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
453 c_type_of_child (struct varobj
*parent
, int index
)
455 struct type
*type
= NULL
;
457 c_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
461 /* This returns the type of the variable. It also skips past typedefs
462 to return the real type of the variable. */
465 get_type (struct varobj
*var
)
471 type
= check_typedef (type
);
477 c_value_of_variable (struct varobj
*var
, enum varobj_display_formats format
)
479 /* BOGUS: if val_print sees a struct/class, or a reference to one,
480 it will print out its children instead of "{...}". So we need to
481 catch that case explicitly. */
482 struct type
*type
= get_type (var
);
484 /* Strip top-level references. */
485 while (TYPE_CODE (type
) == TYPE_CODE_REF
)
486 type
= check_typedef (TYPE_TARGET_TYPE (type
));
488 switch (TYPE_CODE (type
))
490 case TYPE_CODE_STRUCT
:
491 case TYPE_CODE_UNION
:
492 return xstrdup ("{...}");
495 case TYPE_CODE_ARRAY
:
499 number
= xstrprintf ("[%d]", var
->num_children
);
506 if (var
->value
== NULL
)
508 /* This can happen if we attempt to get the value of a struct
509 member when the parent is an invalid pointer. This is an
510 error condition, so we should tell the caller. */
515 if (var
->not_fetched
&& value_lazy (var
->value
))
516 /* Frozen variable and no value yet. We don't
517 implicitly fetch the value. MI response will
518 use empty string for the value, which is OK. */
521 gdb_assert (varobj_value_is_changeable_p (var
));
522 gdb_assert (!value_lazy (var
->value
));
524 /* If the specified format is the current one,
525 we can reuse print_value. */
526 if (format
== var
->format
)
527 return xstrdup (var
->print_value
);
529 return varobj_value_get_print_value (var
->value
, format
, var
);
536 /* varobj operations for c. */
538 const struct lang_varobj_ops c_varobj_ops
=
540 c_number_of_children
,
543 c_path_expr_of_child
,
547 varobj_default_value_is_changeable_p
,
548 NULL
, /* value_has_mutated */
549 c_is_path_expr_parent
/* is_path_expr_parent */
552 /* A little convenience enum for dealing with C++/Java. */
555 v_public
= 0, v_private
, v_protected
561 cplus_number_of_children (struct varobj
*var
)
563 struct value
*value
= NULL
;
565 int children
, dont_know
;
566 int lookup_actual_type
= 0;
567 struct value_print_options opts
;
572 get_user_print_options (&opts
);
574 if (!CPLUS_FAKE_CHILD (var
))
576 type
= varobj_get_value_type (var
);
578 /* It is necessary to access a real type (via RTTI). */
579 if (opts
.objectprint
)
582 lookup_actual_type
= (TYPE_CODE (var
->type
) == TYPE_CODE_REF
583 || TYPE_CODE (var
->type
) == TYPE_CODE_PTR
);
585 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
587 if (((TYPE_CODE (type
)) == TYPE_CODE_STRUCT
)
588 || ((TYPE_CODE (type
)) == TYPE_CODE_UNION
))
592 cplus_class_num_children (type
, kids
);
593 if (kids
[v_public
] != 0)
595 if (kids
[v_private
] != 0)
597 if (kids
[v_protected
] != 0)
600 /* Add any baseclasses. */
601 children
+= TYPE_N_BASECLASSES (type
);
604 /* FIXME: save children in var. */
611 type
= varobj_get_value_type (var
->parent
);
613 /* It is necessary to access a real type (via RTTI). */
614 if (opts
.objectprint
)
616 struct varobj
*parent
= var
->parent
;
618 value
= parent
->value
;
619 lookup_actual_type
= (TYPE_CODE (parent
->type
) == TYPE_CODE_REF
620 || TYPE_CODE (parent
->type
) == TYPE_CODE_PTR
);
622 adjust_value_for_child_access (&value
, &type
, NULL
, lookup_actual_type
);
624 cplus_class_num_children (type
, kids
);
625 if (strcmp (var
->name
, "public") == 0)
626 children
= kids
[v_public
];
627 else if (strcmp (var
->name
, "private") == 0)
628 children
= kids
[v_private
];
630 children
= kids
[v_protected
];
635 children
= c_number_of_children (var
);
640 /* Compute # of public, private, and protected variables in this class.
641 That means we need to descend into all baseclasses and find out
642 how many are there, too. */
645 cplus_class_num_children (struct type
*type
, int children
[3])
648 struct type
*basetype
= NULL
;
650 children
[v_public
] = 0;
651 children
[v_private
] = 0;
652 children
[v_protected
] = 0;
654 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
655 for (i
= TYPE_N_BASECLASSES (type
); i
< TYPE_NFIELDS (type
); i
++)
657 /* If we have a virtual table pointer, omit it. Even if virtual
658 table pointers are not specifically marked in the debug info,
659 they should be artificial. */
660 if ((type
== basetype
&& i
== vptr_fieldno
)
661 || TYPE_FIELD_ARTIFICIAL (type
, i
))
664 if (TYPE_FIELD_PROTECTED (type
, i
))
665 children
[v_protected
]++;
666 else if (TYPE_FIELD_PRIVATE (type
, i
))
667 children
[v_private
]++;
669 children
[v_public
]++;
674 cplus_name_of_variable (struct varobj
*parent
)
676 return c_name_of_variable (parent
);
679 enum accessibility
{ private_field
, protected_field
, public_field
};
681 /* Check if field INDEX of TYPE has the specified accessibility.
682 Return 0 if so and 1 otherwise. */
685 match_accessibility (struct type
*type
, int index
, enum accessibility acc
)
687 if (acc
== private_field
&& TYPE_FIELD_PRIVATE (type
, index
))
689 else if (acc
== protected_field
&& TYPE_FIELD_PROTECTED (type
, index
))
691 else if (acc
== public_field
&& !TYPE_FIELD_PRIVATE (type
, index
)
692 && !TYPE_FIELD_PROTECTED (type
, index
))
699 cplus_describe_child (struct varobj
*parent
, int index
,
700 char **cname
, struct value
**cvalue
, struct type
**ctype
,
701 char **cfull_expression
)
706 int lookup_actual_type
= 0;
707 char *parent_expression
= NULL
;
709 struct value_print_options opts
;
717 if (cfull_expression
)
718 *cfull_expression
= NULL
;
720 get_user_print_options (&opts
);
722 var
= (CPLUS_FAKE_CHILD (parent
)) ? parent
->parent
: parent
;
723 if (opts
.objectprint
)
724 lookup_actual_type
= (TYPE_CODE (var
->type
) == TYPE_CODE_REF
725 || TYPE_CODE (var
->type
) == TYPE_CODE_PTR
);
727 type
= varobj_get_value_type (var
);
728 if (cfull_expression
)
730 = varobj_get_path_expr (varobj_get_path_expr_parent (var
));
732 adjust_value_for_child_access (&value
, &type
, &was_ptr
, lookup_actual_type
);
734 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
735 || TYPE_CODE (type
) == TYPE_CODE_UNION
)
737 char *join
= was_ptr
? "->" : ".";
739 if (CPLUS_FAKE_CHILD (parent
))
741 /* The fields of the class type are ordered as they
742 appear in the class. We are given an index for a
743 particular access control type ("public","protected",
744 or "private"). We must skip over fields that don't
745 have the access control we are looking for to properly
746 find the indexed field. */
747 int type_index
= TYPE_N_BASECLASSES (type
);
748 enum accessibility acc
= public_field
;
750 struct type
*basetype
= NULL
;
751 const char *field_name
;
753 vptr_fieldno
= get_vptr_fieldno (type
, &basetype
);
754 if (strcmp (parent
->name
, "private") == 0)
756 else if (strcmp (parent
->name
, "protected") == 0)
757 acc
= protected_field
;
761 if ((type
== basetype
&& type_index
== vptr_fieldno
)
762 || TYPE_FIELD_ARTIFICIAL (type
, type_index
))
764 else if (match_accessibility (type
, type_index
, acc
))
770 /* If the type is anonymous and the field has no name,
771 set an appopriate name. */
772 field_name
= TYPE_FIELD_NAME (type
, type_index
);
773 if (field_name
== NULL
|| *field_name
== '\0')
777 if (TYPE_CODE (TYPE_FIELD_TYPE (type
, type_index
))
779 *cname
= xstrdup (ANONYMOUS_STRUCT_NAME
);
780 else if (TYPE_CODE (TYPE_FIELD_TYPE (type
, type_index
))
782 *cname
= xstrdup (ANONYMOUS_UNION_NAME
);
785 if (cfull_expression
)
786 *cfull_expression
= xstrdup ("");
791 *cname
= xstrdup (TYPE_FIELD_NAME (type
, type_index
));
793 if (cfull_expression
)
795 = xstrprintf ("((%s)%s%s)", parent_expression
, join
,
800 *cvalue
= value_struct_element_index (value
, type_index
);
803 *ctype
= TYPE_FIELD_TYPE (type
, type_index
);
805 else if (index
< TYPE_N_BASECLASSES (type
))
807 /* This is a baseclass. */
809 *cname
= xstrdup (TYPE_FIELD_NAME (type
, index
));
812 *cvalue
= value_cast (TYPE_FIELD_TYPE (type
, index
), value
);
816 *ctype
= TYPE_FIELD_TYPE (type
, index
);
819 if (cfull_expression
)
821 char *ptr
= was_ptr
? "*" : "";
823 /* Cast the parent to the base' type. Note that in gdb,
826 will create an lvalue, for all appearences, so we don't
827 need to use more fancy:
831 When we are in the scope of the base class or of one
832 of its children, the type field name will be interpreted
833 as a constructor, if it exists. Therefore, we must
834 indicate that the name is a class name by using the
835 'class' keyword. See PR mi/11912 */
836 *cfull_expression
= xstrprintf ("(%s(class %s%s) %s)",
838 TYPE_FIELD_NAME (type
, index
),
848 cplus_class_num_children (type
, children
);
850 /* Everything beyond the baseclasses can
851 only be "public", "private", or "protected"
853 The special "fake" children are always output by varobj in
854 this order. So if INDEX == 2, it MUST be "protected". */
855 index
-= TYPE_N_BASECLASSES (type
);
859 if (children
[v_public
] > 0)
861 else if (children
[v_private
] > 0)
864 access
= "protected";
867 if (children
[v_public
] > 0)
869 if (children
[v_private
] > 0)
872 access
= "protected";
874 else if (children
[v_private
] > 0)
875 access
= "protected";
878 /* Must be protected. */
879 access
= "protected";
888 *cname
= xstrdup (access
);
890 /* Value and type and full expression are null here. */
895 c_describe_child (parent
, index
, cname
, cvalue
, ctype
, cfull_expression
);
900 cplus_name_of_child (struct varobj
*parent
, int index
)
904 cplus_describe_child (parent
, index
, &name
, NULL
, NULL
, NULL
);
909 cplus_path_expr_of_child (struct varobj
*child
)
913 cplus_describe_child (child
->parent
, child
->index
, NULL
, NULL
, NULL
,
918 static struct value
*
919 cplus_value_of_child (struct varobj
*parent
, int index
)
921 struct value
*value
= NULL
;
923 cplus_describe_child (parent
, index
, NULL
, &value
, NULL
, NULL
);
928 cplus_type_of_child (struct varobj
*parent
, int index
)
930 struct type
*type
= NULL
;
932 cplus_describe_child (parent
, index
, NULL
, NULL
, &type
, NULL
);
937 cplus_value_of_variable (struct varobj
*var
,
938 enum varobj_display_formats format
)
941 /* If we have one of our special types, don't print out
943 if (CPLUS_FAKE_CHILD (var
))
946 return c_value_of_variable (var
, format
);
950 /* varobj operations for c++. */
952 const struct lang_varobj_ops cplus_varobj_ops
=
954 cplus_number_of_children
,
955 cplus_name_of_variable
,
957 cplus_path_expr_of_child
,
958 cplus_value_of_child
,
960 cplus_value_of_variable
,
961 varobj_default_value_is_changeable_p
,
962 NULL
, /* value_has_mutated */
963 c_is_path_expr_parent
/* is_path_expr_parent */