1 /* varobj support for C and C++.
3 Copyright (C) 1999-2017 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 (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_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)
97 *value = value_ind (*value);
100 CATCH (except, RETURN_MASK_ERROR)
112 /* The 'get_target_type' function calls check_typedef on
113 result, so we can immediately check type code. No
114 need to call check_typedef here. */
116 /* Access a real type of the value (if necessary and possible). */
117 if (value && *value && lookup_actual_type)
119 struct type *enclosing_type;
120 int real_type_found = 0;
122 enclosing_type = value_actual_type (*value, 1, &real_type_found);
125 *type = enclosing_type;
126 *value = value_cast (enclosing_type, *value);
131 /* Is VAR a path expression parent, i.e., can it be used to construct
132 a valid path expression? */
135 c_is_path_expr_parent (const struct varobj *var)
139 /* "Fake" children are not path_expr parents. */
140 if (CPLUS_FAKE_CHILD (var))
143 type = varobj_get_gdb_type (var);
145 /* Anonymous unions and structs are also not path_expr parents. */
146 if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
147 || TYPE_CODE (type) == TYPE_CODE_UNION)
148 && TYPE_NAME (type) == NULL
149 && TYPE_TAG_NAME (type) == NULL)
151 const struct varobj *parent = var->parent;
153 while (parent != NULL && CPLUS_FAKE_CHILD (parent))
154 parent = parent->parent;
158 struct type *parent_type;
161 parent_type = varobj_get_value_type (parent);
162 adjust_value_for_child_access (NULL, &parent_type, &was_ptr, 0);
164 if (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT
165 || TYPE_CODE (parent_type) == TYPE_CODE_UNION)
167 const char *field_name;
169 gdb_assert (var->index < TYPE_NFIELDS (parent_type));
170 field_name = TYPE_FIELD_NAME (parent_type, var->index);
171 return !(field_name == NULL || *field_name == '\0');
184 c_number_of_children (const struct varobj *var)
186 struct type *type = varobj_get_value_type (var);
190 adjust_value_for_child_access (NULL, &type, NULL, 0);
191 target = get_target_type (type);
193 switch (TYPE_CODE (type))
195 case TYPE_CODE_ARRAY:
196 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
197 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
198 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
200 /* If we don't know how many elements there are, don't display
205 case TYPE_CODE_STRUCT:
206 case TYPE_CODE_UNION:
207 children = TYPE_NFIELDS (type);
211 /* The type here is a pointer to non-struct. Typically, pointers
212 have one child, except for function ptrs, which have no children,
213 and except for void*, as we don't know what to show.
215 We can show char* so we allow it to be dereferenced. If you decide
216 to test for it, please mind that a little magic is necessary to
217 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
218 TYPE_NAME == "char". */
219 if (TYPE_CODE (target) == TYPE_CODE_FUNC
220 || TYPE_CODE (target) == TYPE_CODE_VOID)
227 /* Other types have no children. */
235 c_name_of_variable (const struct varobj *parent)
240 /* Return the value of element TYPE_INDEX of a structure
241 value VALUE. VALUE's type should be a structure,
242 or union, or a typedef to struct/union.
244 Returns NULL if getting the value fails. Never throws. */
246 static struct value *
247 value_struct_element_index (struct value *value, int type_index)
249 struct value *result = NULL;
250 struct type *type = value_type (value);
252 type = check_typedef (type);
254 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
255 || TYPE_CODE (type) == TYPE_CODE_UNION);
259 if (field_is_static (&TYPE_FIELD (type, type_index)))
260 result = value_static_field (type, type_index);
262 result = value_primitive_field (value, 0, type_index, type);
264 CATCH (e, RETURN_MASK_ERROR)
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 (const struct varobj *parent, int index,
286 std::string *cname, struct value **cvalue,
287 struct type **ctype, std::string *cfull_expression)
289 struct value *value = parent->value;
290 struct type *type = varobj_get_value_type (parent);
291 std::string parent_expression;
295 *cname = std::string ();
300 if (cfull_expression)
302 *cfull_expression = std::string ();
304 = varobj_get_path_expr (varobj_get_path_expr_parent (parent));
306 adjust_value_for_child_access (&value, &type, &was_ptr, 0);
308 switch (TYPE_CODE (type))
310 case TYPE_CODE_ARRAY:
312 *cname = int_string (index
313 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
318 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
322 *cvalue = value_subscript (value, real_index);
324 CATCH (except, RETURN_MASK_ERROR)
331 *ctype = get_target_type (type);
333 if (cfull_expression)
335 string_printf ("(%s)[%s]", parent_expression.c_str (),
337 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
343 case TYPE_CODE_STRUCT:
344 case TYPE_CODE_UNION:
346 const char *field_name;
348 /* If the type is anonymous and the field has no name,
349 set an appropriate name. */
350 field_name = TYPE_FIELD_NAME (type, index);
351 if (field_name == NULL || *field_name == '\0')
355 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
357 *cname = ANONYMOUS_STRUCT_NAME;
359 *cname = ANONYMOUS_UNION_NAME;
362 if (cfull_expression)
363 *cfull_expression = "";
370 if (cfull_expression)
372 const char *join = was_ptr ? "->" : ".";
374 *cfull_expression = string_printf ("(%s)%s%s",
375 parent_expression.c_str (),
382 /* For C, varobj index is the same as type index. */
383 *cvalue = value_struct_element_index (value, index);
387 *ctype = TYPE_FIELD_TYPE (type, index);
393 *cname = string_printf ("*%s", parent->name.c_str ());
399 *cvalue = value_ind (value);
402 CATCH (except, RETURN_MASK_ERROR)
409 /* Don't use get_target_type because it calls
410 check_typedef and here, we want to show the true
411 declared type of the variable. */
413 *ctype = TYPE_TARGET_TYPE (type);
415 if (cfull_expression)
416 *cfull_expression = string_printf ("*(%s)", parent_expression.c_str ());
420 /* This should not happen. */
423 if (cfull_expression)
424 *cfull_expression = "???";
425 /* Don't set value and type, we don't know then. */
430 c_name_of_child (const struct varobj *parent, int index)
434 c_describe_child (parent, index, &name, NULL, NULL, NULL);
439 c_path_expr_of_child (const struct varobj *child)
441 std::string path_expr;
443 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
448 static struct value *
449 c_value_of_child (const struct varobj *parent, int index)
451 struct value *value = NULL;
453 c_describe_child (parent, index, NULL, &value, NULL, NULL);
458 c_type_of_child (const struct varobj *parent, int index)
460 struct type *type = NULL;
462 c_describe_child (parent, index, NULL, NULL, &type, NULL);
466 /* This returns the type of the variable. It also skips past typedefs
467 to return the real type of the variable. */
470 get_type (const struct varobj *var)
476 type = check_typedef (type);
482 c_value_of_variable (const struct varobj *var,
483 enum varobj_display_formats format)
485 /* BOGUS: if val_print sees a struct/class, or a reference to one,
486 it will print out its children instead of "{...}". So we need to
487 catch that case explicitly. */
488 struct type *type = get_type (var);
490 /* Strip top-level references. */
491 while (TYPE_CODE (type) == TYPE_CODE_REF)
492 type = check_typedef (TYPE_TARGET_TYPE (type));
494 switch (TYPE_CODE (type))
496 case TYPE_CODE_STRUCT:
497 case TYPE_CODE_UNION:
501 case TYPE_CODE_ARRAY:
502 return string_printf ("[%d]", var->num_children);
507 if (var->value == NULL)
509 /* This can happen if we attempt to get the value of a struct
510 member when the parent is an invalid pointer. This is an
511 error condition, so we should tell the caller. */
512 return std::string ();
516 if (var->not_fetched && value_lazy (var->value))
517 /* Frozen variable and no value yet. We don't
518 implicitly fetch the value. MI response will
519 use empty string for the value, which is OK. */
520 return std::string ();
522 gdb_assert (varobj_value_is_changeable_p (var));
523 gdb_assert (!value_lazy (var->value));
525 /* If the specified format is the current one,
526 we can reuse print_value. */
527 if (format == var->format)
528 return var->print_value;
530 return varobj_value_get_print_value (var->value, format, var);
537 /* varobj operations for c. */
539 const struct lang_varobj_ops c_varobj_ops =
541 c_number_of_children,
544 c_path_expr_of_child,
548 varobj_default_value_is_changeable_p,
549 NULL, /* value_has_mutated */
550 c_is_path_expr_parent /* is_path_expr_parent */
553 /* A little convenience enum for dealing with C++. */
556 v_public = 0, v_private, v_protected
562 cplus_number_of_children (const struct varobj *var)
564 struct value *value = NULL;
566 int children, dont_know;
567 int lookup_actual_type = 0;
568 struct value_print_options opts;
573 get_user_print_options (&opts);
575 if (!CPLUS_FAKE_CHILD (var))
577 type = varobj_get_value_type (var);
579 /* It is necessary to access a real type (via RTTI). */
580 if (opts.objectprint)
583 lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
584 || TYPE_CODE (var->type) == TYPE_CODE_PTR);
586 adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
588 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT)
589 || ((TYPE_CODE (type)) == TYPE_CODE_UNION))
593 cplus_class_num_children (type, kids);
594 if (kids[v_public] != 0)
596 if (kids[v_private] != 0)
598 if (kids[v_protected] != 0)
601 /* Add any baseclasses. */
602 children += TYPE_N_BASECLASSES (type);
605 /* FIXME: save children in var. */
612 type = varobj_get_value_type (var->parent);
614 /* It is necessary to access a real type (via RTTI). */
615 if (opts.objectprint)
617 const struct varobj *parent = var->parent;
619 value = parent->value;
620 lookup_actual_type = (TYPE_CODE (parent->type) == TYPE_CODE_REF
621 || TYPE_CODE (parent->type) == TYPE_CODE_PTR);
623 adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
625 cplus_class_num_children (type, kids);
626 if (var->name == "public")
627 children = kids[v_public];
628 else if (var->name == "private")
629 children = kids[v_private];
631 children = kids[v_protected];
636 children = c_number_of_children (var);
641 /* Compute # of public, private, and protected variables in this class.
642 That means we need to descend into all baseclasses and find out
643 how many are there, too. */
646 cplus_class_num_children (struct type *type, int children[3])
649 struct type *basetype = NULL;
651 children[v_public] = 0;
652 children[v_private] = 0;
653 children[v_protected] = 0;
655 vptr_fieldno = get_vptr_fieldno (type, &basetype);
656 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
658 /* If we have a virtual table pointer, omit it. Even if virtual
659 table pointers are not specifically marked in the debug info,
660 they should be artificial. */
661 if ((type == basetype && i == vptr_fieldno)
662 || TYPE_FIELD_ARTIFICIAL (type, i))
665 if (TYPE_FIELD_PROTECTED (type, i))
666 children[v_protected]++;
667 else if (TYPE_FIELD_PRIVATE (type, i))
668 children[v_private]++;
670 children[v_public]++;
675 cplus_name_of_variable (const struct varobj *parent)
677 return c_name_of_variable (parent);
680 enum accessibility { private_field, protected_field, public_field };
682 /* Check if field INDEX of TYPE has the specified accessibility.
683 Return 0 if so and 1 otherwise. */
686 match_accessibility (struct type *type, int index, enum accessibility acc)
688 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
690 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
692 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
693 && !TYPE_FIELD_PROTECTED (type, index))
700 cplus_describe_child (const struct varobj *parent, int index,
701 std::string *cname, struct value **cvalue, struct type **ctype,
702 std::string *cfull_expression)
707 int lookup_actual_type = 0;
708 const char *parent_expression = NULL;
709 const struct varobj *var;
710 struct value_print_options opts;
713 *cname = std::string ();
718 if (cfull_expression)
719 *cfull_expression = std::string ();
721 get_user_print_options (&opts);
723 var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent;
724 if (opts.objectprint)
725 lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
726 || TYPE_CODE (var->type) == TYPE_CODE_PTR);
728 type = varobj_get_value_type (var);
729 if (cfull_expression)
731 = varobj_get_path_expr (varobj_get_path_expr_parent (var));
733 adjust_value_for_child_access (&value, &type, &was_ptr, lookup_actual_type);
735 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
736 || TYPE_CODE (type) == TYPE_CODE_UNION)
738 const char *join = was_ptr ? "->" : ".";
740 if (CPLUS_FAKE_CHILD (parent))
742 /* The fields of the class type are ordered as they
743 appear in the class. We are given an index for a
744 particular access control type ("public","protected",
745 or "private"). We must skip over fields that don't
746 have the access control we are looking for to properly
747 find the indexed field. */
748 int type_index = TYPE_N_BASECLASSES (type);
749 enum accessibility acc = public_field;
751 struct type *basetype = NULL;
752 const char *field_name;
754 vptr_fieldno = get_vptr_fieldno (type, &basetype);
755 if (parent->name == "private")
757 else if (parent->name == "protected")
758 acc = protected_field;
762 if ((type == basetype && type_index == vptr_fieldno)
763 || TYPE_FIELD_ARTIFICIAL (type, type_index))
765 else if (match_accessibility (type, type_index, acc))
771 /* If the type is anonymous and the field has no name,
772 set an appopriate name. */
773 field_name = TYPE_FIELD_NAME (type, type_index);
774 if (field_name == NULL || *field_name == '\0')
778 if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
780 *cname = ANONYMOUS_STRUCT_NAME;
781 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
783 *cname = ANONYMOUS_UNION_NAME;
786 if (cfull_expression)
787 *cfull_expression = std::string ();
792 *cname = TYPE_FIELD_NAME (type, type_index);
794 if (cfull_expression)
796 = string_printf ("((%s)%s%s)", parent_expression, join,
801 *cvalue = value_struct_element_index (value, type_index);
804 *ctype = TYPE_FIELD_TYPE (type, type_index);
806 else if (index < TYPE_N_BASECLASSES (type))
808 /* This is a baseclass. */
810 *cname = TYPE_FIELD_NAME (type, index);
813 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
817 *ctype = TYPE_FIELD_TYPE (type, index);
820 if (cfull_expression)
822 const char *ptr = was_ptr ? "*" : "";
824 /* Cast the parent to the base' type. Note that in gdb,
827 will create an lvalue, for all appearences, so we don't
828 need to use more fancy:
832 When we are in the scope of the base class or of one
833 of its children, the type field name will be interpreted
834 as a constructor, if it exists. Therefore, we must
835 indicate that the name is a class name by using the
836 'class' keyword. See PR mi/11912 */
837 *cfull_expression = string_printf ("(%s(class %s%s) %s)",
839 TYPE_FIELD_NAME (type, index),
849 cplus_class_num_children (type, children);
851 /* Everything beyond the baseclasses can
852 only be "public", "private", or "protected"
854 The special "fake" children are always output by varobj in
855 this order. So if INDEX == 2, it MUST be "protected". */
856 index -= TYPE_N_BASECLASSES (type);
860 if (children[v_public] > 0)
862 else if (children[v_private] > 0)
865 access = "protected";
868 if (children[v_public] > 0)
870 if (children[v_private] > 0)
873 access = "protected";
875 else if (children[v_private] > 0)
876 access = "protected";
879 /* Must be protected. */
880 access = "protected";
891 /* Value and type and full expression are null here. */
896 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
901 cplus_name_of_child (const struct varobj *parent, int index)
905 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
910 cplus_path_expr_of_child (const struct varobj *child)
912 std::string path_expr;
914 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
919 static struct value *
920 cplus_value_of_child (const struct varobj *parent, int index)
922 struct value *value = NULL;
924 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
929 cplus_type_of_child (const struct varobj *parent, int index)
931 struct type *type = NULL;
933 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
938 cplus_value_of_variable (const struct varobj *var,
939 enum varobj_display_formats format)
942 /* If we have one of our special types, don't print out
944 if (CPLUS_FAKE_CHILD (var))
945 return std::string ();
947 return c_value_of_variable (var, format);
951 /* varobj operations for c++. */
953 const struct lang_varobj_ops cplus_varobj_ops =
955 cplus_number_of_children,
956 cplus_name_of_variable,
958 cplus_path_expr_of_child,
959 cplus_value_of_child,
961 cplus_value_of_variable,
962 varobj_default_value_is_changeable_p,
963 NULL, /* value_has_mutated */
964 c_is_path_expr_parent /* is_path_expr_parent */