Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / c-varobj.c
blob4751f0a957a6ab8373e6a75d66e55663f400fd86
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/>. */
18 #include "value.h"
19 #include "varobj.h"
20 #include "gdbthread.h"
21 #include "valprint.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. */
36 bool
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
48 structures.
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
57 value is not known.
59 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
60 depending on whether pointer was dereferenced
61 in this function. */
63 static void
64 adjust_value_for_child_access (struct value **value,
65 struct type **type,
66 int *was_ptr,
67 int lookup_actual_type)
69 gdb_assert (type && *type);
71 if (was_ptr)
72 *was_ptr = 0;
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)
91 if (value && *value)
94 try
96 *value = value_ind (*value);
99 catch (const gdb_exception_error &except)
101 *value = NULL;
104 *type = target_type;
105 if (was_ptr)
106 *was_ptr = 1;
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);
121 if (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? */
132 static bool
133 c_is_path_expr_parent (const struct varobj *var)
135 struct type *type;
137 /* "Fake" children are not path_expr parents. */
138 if (CPLUS_FAKE_CHILD (var))
139 return false;
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;
153 if (parent != NULL)
155 struct type *parent_type;
156 int was_ptr;
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');
172 return false;
175 return true;
178 /* C */
180 static int
181 c_number_of_children (const struct varobj *var)
183 struct type *type = varobj_get_value_type (var);
184 int children = 0;
185 struct type *target;
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 ();
196 else
197 /* If we don't know how many elements there are, don't display
198 any. */
199 children = 0;
200 break;
202 case TYPE_CODE_STRUCT:
203 case TYPE_CODE_UNION:
204 children = type->num_fields ();
205 break;
207 case TYPE_CODE_PTR:
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)
218 children = 0;
219 else
220 children = 1;
221 break;
223 default:
224 /* Other types have no children. */
225 break;
228 return children;
231 static std::string
232 c_name_of_variable (const struct varobj *parent)
234 return parent->name;
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);
258 else
259 result = value->primitive_field (0, type_index, type);
261 catch (const gdb_exception_error &e)
263 return NULL;
266 return result;
269 /* Obtain the information about child INDEX of the variable
270 object PARENT.
271 If CNAME is not null, sets *CNAME to the name of the child relative
272 to the parent.
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
278 to empty. */
280 static void
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;
288 int was_ptr;
290 if (cname)
291 *cname = std::string ();
292 if (cvalue)
293 *cvalue = NULL;
294 if (ctype)
295 *ctype = NULL;
296 if (cfull_expression)
298 *cfull_expression = std::string ();
299 parent_expression
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:
307 if (cname)
308 *cname = int_string (index + type->bounds ()->low.const_val (),
309 10, 1, 0, 0);
311 if (cvalue && value)
313 int real_index
314 = index + type->bounds ()->low.const_val ();
318 *cvalue = value_subscript (value, real_index);
320 catch (const gdb_exception_error &except)
325 if (ctype)
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 (),
332 10, 1, 0, 0));
334 break;
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')
346 if (cname)
348 if (type->field (index).type ()->code ()
349 == TYPE_CODE_STRUCT)
350 *cname = ANONYMOUS_STRUCT_NAME;
351 else
352 *cname = ANONYMOUS_UNION_NAME;
355 if (cfull_expression)
356 *cfull_expression = "";
358 else
360 if (cname)
361 *cname = field_name;
363 if (cfull_expression)
365 const char *join = was_ptr ? "->" : ".";
367 *cfull_expression = string_printf ("(%s)%s%s",
368 parent_expression.c_str (),
369 join, field_name);
373 if (cvalue && value)
375 /* For C, varobj index is the same as type index. */
376 *cvalue = value_struct_element_index (value, index);
379 if (ctype)
380 *ctype = type->field (index).type ();
382 break;
384 case TYPE_CODE_PTR:
385 if (cname)
386 *cname = string_printf ("*%s", parent->name.c_str ());
388 if (cvalue && value)
392 *cvalue = value_ind (value);
395 catch (const gdb_exception_error &except)
397 *cvalue = NULL;
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. */
404 if (ctype)
405 *ctype = type->target_type ();
407 if (cfull_expression)
408 *cfull_expression = string_printf ("*(%s)", parent_expression.c_str ());
409 break;
411 default:
412 /* This should not happen. */
413 if (cname)
414 *cname = "???";
415 if (cfull_expression)
416 *cfull_expression = "???";
417 /* Don't set value and type, we don't know then. */
421 static std::string
422 c_name_of_child (const struct varobj *parent, int index)
424 std::string name;
426 c_describe_child (parent, index, &name, NULL, NULL, NULL);
427 return name;
430 static std::string
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,
436 &path_expr);
437 return path_expr;
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);
446 return value;
449 static struct type *
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);
455 return type;
458 /* This returns the type of the variable. It also skips past typedefs
459 to return the real type of the variable. */
461 static struct type *
462 get_type (const struct varobj *var)
464 struct type *type;
466 type = var->type;
467 if (type != NULL)
468 type = check_typedef (type);
470 return type;
473 static std::string
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:
490 return "{...}";
491 /* break; */
493 case TYPE_CODE_ARRAY:
494 return string_printf ("[%d]", var->num_children);
495 /* break; */
497 default:
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 ();
506 else
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;
521 else
522 return varobj_value_get_print_value (var->value.get (), format,
523 var);
530 /* varobj operations for c. */
532 const struct lang_varobj_ops c_varobj_ops =
534 c_number_of_children,
535 c_name_of_variable,
536 c_name_of_child,
537 c_path_expr_of_child,
538 c_value_of_child,
539 c_type_of_child,
540 c_value_of_variable,
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++. */
547 enum vsections
549 v_public = 0, v_private, v_protected
552 /* C++ */
554 static int
555 cplus_number_of_children (const struct varobj *var)
557 struct value *value = NULL;
558 struct type *type;
559 int children, dont_know;
560 int lookup_actual_type = 0;
561 struct value_print_options opts;
563 dont_know = 1;
564 children = 0;
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))
583 int kids[3];
585 cplus_class_num_children (type, kids);
586 if (kids[v_public] != 0)
587 children++;
588 if (kids[v_private] != 0)
589 children++;
590 if (kids[v_protected] != 0)
591 children++;
593 /* Add any baseclasses. */
594 children += TYPE_N_BASECLASSES (type);
595 dont_know = 0;
597 /* FIXME: save children in var. */
600 else
602 int kids[3];
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];
621 else
622 children = kids[v_protected];
623 dont_know = 0;
626 if (dont_know)
627 children = c_number_of_children (var);
629 return children;
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. */
636 static void
637 cplus_class_num_children (struct type *type, int children[3])
639 int i, vptr_fieldno;
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 ())
656 continue;
658 if (fld.is_protected ())
659 children[v_protected]++;
660 else if (fld.is_private ())
661 children[v_private]++;
662 else
663 children[v_public]++;
667 static std::string
668 cplus_name_of_variable (const struct varobj *parent)
670 return c_name_of_variable (parent);
673 static void
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)
678 struct value *value;
679 struct type *type;
680 int was_ptr;
681 int lookup_actual_type = 0;
682 const char *parent_expression = NULL;
683 const struct varobj *var;
684 struct value_print_options opts;
686 if (cname)
687 *cname = std::string ();
688 if (cvalue)
689 *cvalue = NULL;
690 if (ctype)
691 *ctype = NULL;
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)
703 parent_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;
723 int vptr_fieldno;
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;
733 while (index >= 0)
735 if ((type == basetype && type_index == vptr_fieldno)
736 || type->field (type_index).is_artificial ())
737 ; /* ignore vptr */
738 else if (type->field (type_index).accessibility () == acc)
739 --index;
740 ++type_index;
742 --type_index;
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')
749 if (cname)
751 if (type->field (type_index).type ()->code ()
752 == TYPE_CODE_STRUCT)
753 *cname = ANONYMOUS_STRUCT_NAME;
754 else if (type->field (type_index).type ()->code ()
755 == TYPE_CODE_UNION)
756 *cname = ANONYMOUS_UNION_NAME;
759 if (cfull_expression)
760 *cfull_expression = std::string ();
762 else
764 if (cname)
765 *cname = type->field (type_index).name ();
767 if (cfull_expression)
768 *cfull_expression
769 = string_printf ("((%s)%s%s)", parent_expression, join,
770 field_name);
773 if (cvalue && value)
774 *cvalue = value_struct_element_index (value, type_index);
776 if (ctype)
777 *ctype = type->field (type_index).type ();
779 else if (index < TYPE_N_BASECLASSES (type))
781 /* This is a baseclass. */
782 if (cname)
783 *cname = type->field (index).name ();
785 if (cvalue && value)
786 *cvalue = value_cast (type->field (index).type (), value);
788 if (ctype)
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,
798 expression like
799 (Base1)d
800 will create an lvalue, for all appearances, so we don't
801 need to use more fancy:
802 *(Base1*)(&d)
803 construct.
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)",
811 ptr,
812 type->field (index).name (),
813 ptr,
814 parent_expression);
817 else
819 const char *access = NULL;
820 int children[3];
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);
830 switch (index)
832 case 0:
833 if (children[v_public] > 0)
834 access = "public";
835 else if (children[v_private] > 0)
836 access = "private";
837 else
838 access = "protected";
839 break;
840 case 1:
841 if (children[v_public] > 0)
843 if (children[v_private] > 0)
844 access = "private";
845 else
846 access = "protected";
848 else if (children[v_private] > 0)
849 access = "protected";
850 break;
851 case 2:
852 /* Must be protected. */
853 access = "protected";
854 break;
855 default:
856 /* error! */
857 break;
860 gdb_assert (access);
861 if (cname)
862 *cname = access;
864 /* Value and type and full expression are null here. */
867 else
869 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
873 static std::string
874 cplus_name_of_child (const struct varobj *parent, int index)
876 std::string name;
878 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
879 return name;
882 static std::string
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,
888 &path_expr);
889 return path_expr;
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);
898 return value;
901 static struct type *
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);
907 return type;
910 static std::string
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
916 any value. */
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,
930 cplus_name_of_child,
931 cplus_path_expr_of_child,
932 cplus_value_of_child,
933 cplus_type_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 */