1 /* Support for printing Pascal values for GDB, the GNU debugger.
3 Copyright (C) 2000-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This file is derived from c-valprint.c */
23 #include "gdb_obstack.h"
26 #include "expression.h"
33 #include "typeprint.h"
39 #include "cp-support.h"
43 /* Decorations for Pascal. */
45 static const struct generic_val_print_decorations p_decorations =
57 /* See val_print for a description of the various parameters of this
58 function; they are identical. */
61 pascal_val_print (struct type *type, const gdb_byte *valaddr,
62 int embedded_offset, CORE_ADDR address,
63 struct ui_file *stream, int recurse,
64 const struct value *original_value,
65 const struct value_print_options *options)
67 struct gdbarch *gdbarch = get_type_arch (type);
68 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
69 unsigned int i = 0; /* Number of characters printed */
71 LONGEST low_bound, high_bound;
74 int length_pos, length_size, string_pos;
75 struct type *char_type;
79 type = check_typedef (type);
80 switch (TYPE_CODE (type))
83 if (get_array_bounds (type, &low_bound, &high_bound))
85 len = high_bound - low_bound + 1;
86 elttype = check_typedef (TYPE_TARGET_TYPE (type));
87 eltlen = TYPE_LENGTH (elttype);
88 if (options->prettyformat_arrays)
90 print_spaces_filtered (2 + 2 * recurse, stream);
92 /* If 's' format is used, try to print out as string.
93 If no format is given, print as string if element type
94 is of TYPE_CODE_CHAR and element size is 1,2 or 4. */
95 if (options->format == 's'
96 || ((eltlen == 1 || eltlen == 2 || eltlen == 4)
97 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
98 && options->format == 0))
100 /* If requested, look for the first null char and only print
101 elements up to it. */
102 if (options->stop_print_at_null)
104 unsigned int temp_len;
106 /* Look for a NULL char. */
108 extract_unsigned_integer (valaddr + embedded_offset +
109 temp_len * eltlen, eltlen,
111 && temp_len < len && temp_len < options->print_max;
116 LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
117 valaddr + embedded_offset, len, NULL, 0,
123 fprintf_filtered (stream, "{");
124 /* If this is a virtual function table, print the 0th
125 entry specially, and the rest of the members normally. */
126 if (pascal_object_is_vtbl_ptr_type (elttype))
129 fprintf_filtered (stream, "%d vtable entries", len - 1);
135 val_print_array_elements (type, valaddr, embedded_offset,
136 address, stream, recurse,
137 original_value, options, i);
138 fprintf_filtered (stream, "}");
142 /* Array of unspecified length: treat like pointer to first elt. */
143 addr = address + embedded_offset;
144 goto print_unpacked_pointer;
147 if (options->format && options->format != 's')
149 val_print_scalar_formatted (type, valaddr, embedded_offset,
150 original_value, options, 0, stream);
153 if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
155 /* Print the unmangled name if desired. */
156 /* Print vtable entry - we only get here if we ARE using
157 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
158 /* Extract the address, assume that it is unsigned. */
159 addr = extract_unsigned_integer (valaddr + embedded_offset,
160 TYPE_LENGTH (type), byte_order);
161 print_address_demangle (options, gdbarch, addr, stream, demangle);
164 check_typedef (TYPE_TARGET_TYPE (type));
166 addr = unpack_pointer (type, valaddr + embedded_offset);
167 print_unpacked_pointer:
168 elttype = check_typedef (TYPE_TARGET_TYPE (type));
170 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
172 /* Try to print what function it points to. */
173 print_address_demangle (options, gdbarch, addr, stream, demangle);
177 if (options->addressprint && options->format != 's')
179 fputs_filtered (paddress (gdbarch, addr), stream);
183 /* For a pointer to char or unsigned char, also print the string
184 pointed to, unless pointer is null. */
185 if (((TYPE_LENGTH (elttype) == 1
186 && (TYPE_CODE (elttype) == TYPE_CODE_INT
187 || TYPE_CODE (elttype) == TYPE_CODE_CHAR))
188 || ((TYPE_LENGTH (elttype) == 2 || TYPE_LENGTH (elttype) == 4)
189 && TYPE_CODE (elttype) == TYPE_CODE_CHAR))
190 && (options->format == 0 || options->format == 's')
194 fputs_filtered (" ", stream);
195 /* No wide string yet. */
196 i = val_print_string (elttype, NULL, addr, -1, stream, options);
198 /* Also for pointers to pascal strings. */
199 /* Note: this is Free Pascal specific:
200 as GDB does not recognize stabs pascal strings
201 Pascal strings are mapped to records
202 with lowercase names PM. */
203 if (is_pascal_string_type (elttype, &length_pos, &length_size,
204 &string_pos, &char_type, NULL)
207 ULONGEST string_length;
211 fputs_filtered (" ", stream);
212 buffer = (gdb_byte *) xmalloc (length_size);
213 read_memory (addr + length_pos, buffer, length_size);
214 string_length = extract_unsigned_integer (buffer, length_size,
217 i = val_print_string (char_type, NULL,
218 addr + string_pos, string_length,
221 else if (pascal_object_is_vtbl_member (type))
223 /* Print vtbl's nicely. */
224 CORE_ADDR vt_address = unpack_pointer (type,
225 valaddr + embedded_offset);
226 struct bound_minimal_symbol msymbol =
227 lookup_minimal_symbol_by_pc (vt_address);
229 /* If 'symbol_print' is set, we did the work above. */
230 if (!options->symbol_print
231 && (msymbol.minsym != NULL)
232 && (vt_address == BMSYMBOL_VALUE_ADDRESS (msymbol)))
235 fputs_filtered (" ", stream);
236 fputs_filtered ("<", stream);
237 fputs_filtered (MSYMBOL_PRINT_NAME (msymbol.minsym), stream);
238 fputs_filtered (">", stream);
241 if (vt_address && options->vtblprint)
243 struct value *vt_val;
244 struct symbol *wsym = NULL;
246 struct block *block = NULL;
247 struct field_of_this_result is_this_fld;
250 fputs_filtered (" ", stream);
252 if (msymbol.minsym != NULL)
253 wsym = lookup_symbol (MSYMBOL_LINKAGE_NAME (msymbol.minsym),
255 VAR_DOMAIN, &is_this_fld).symbol;
259 wtype = SYMBOL_TYPE (wsym);
263 wtype = TYPE_TARGET_TYPE (type);
265 vt_val = value_at (wtype, vt_address);
266 common_val_print (vt_val, stream, recurse + 1, options,
268 if (options->prettyformat)
270 fprintf_filtered (stream, "\n");
271 print_spaces_filtered (2 + 2 * recurse, stream);
280 case TYPE_CODE_FLAGS:
282 case TYPE_CODE_RANGE:
286 case TYPE_CODE_ERROR:
287 case TYPE_CODE_UNDEF:
290 generic_val_print (type, valaddr, embedded_offset, address,
291 stream, recurse, original_value, options,
295 case TYPE_CODE_UNION:
296 if (recurse && !options->unionprint)
298 fprintf_filtered (stream, "{...}");
302 case TYPE_CODE_STRUCT:
303 if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
305 /* Print the unmangled name if desired. */
306 /* Print vtable entry - we only get here if NOT using
307 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
308 /* Extract the address, assume that it is unsigned. */
309 print_address_demangle
311 extract_unsigned_integer (valaddr + embedded_offset
312 + TYPE_FIELD_BITPOS (type,
313 VTBL_FNADDR_OFFSET) / 8,
314 TYPE_LENGTH (TYPE_FIELD_TYPE (type,
315 VTBL_FNADDR_OFFSET)),
321 if (is_pascal_string_type (type, &length_pos, &length_size,
322 &string_pos, &char_type, NULL))
324 len = extract_unsigned_integer (valaddr + embedded_offset
325 + length_pos, length_size,
327 LA_PRINT_STRING (stream, char_type,
328 valaddr + embedded_offset + string_pos,
329 len, NULL, 0, options);
332 pascal_object_print_value_fields (type, valaddr, embedded_offset,
333 address, stream, recurse,
334 original_value, options,
340 elttype = TYPE_INDEX_TYPE (type);
341 elttype = check_typedef (elttype);
342 if (TYPE_STUB (elttype))
344 fprintf_filtered (stream, "<incomplete type>");
350 struct type *range = elttype;
351 LONGEST low_bound, high_bound;
355 fputs_filtered ("[", stream);
357 i = get_discrete_bounds (range, &low_bound, &high_bound);
358 if (low_bound == 0 && high_bound == -1 && TYPE_LENGTH (type) > 0)
360 /* If we know the size of the set type, we can figure out the
363 high_bound = TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1;
364 TYPE_HIGH_BOUND (range) = high_bound;
369 fputs_filtered ("<error value>", stream);
373 for (i = low_bound; i <= high_bound; i++)
375 int element = value_bit_index (type,
376 valaddr + embedded_offset, i);
381 goto maybe_bad_bstring;
386 fputs_filtered (", ", stream);
387 print_type_scalar (range, i, stream);
390 if (i + 1 <= high_bound
391 && value_bit_index (type,
392 valaddr + embedded_offset, ++i))
396 fputs_filtered ("..", stream);
397 while (i + 1 <= high_bound
398 && value_bit_index (type,
399 valaddr + embedded_offset,
402 print_type_scalar (range, j, stream);
407 fputs_filtered ("]", stream);
412 error (_("Invalid pascal type code %d in symbol table."),
419 pascal_value_print (struct value *val, struct ui_file *stream,
420 const struct value_print_options *options)
422 struct type *type = value_type (val);
423 struct value_print_options opts = *options;
427 /* If it is a pointer, indicate what it points to.
429 Print type also if it is a reference.
431 Object pascal: if it is a member pointer, we will take care
432 of that when we print it. */
433 if (TYPE_CODE (type) == TYPE_CODE_PTR
434 || TYPE_CODE (type) == TYPE_CODE_REF)
436 /* Hack: remove (char *) for char strings. Their
437 type is indicated by the quoted string anyway. */
438 if (TYPE_CODE (type) == TYPE_CODE_PTR
439 && TYPE_NAME (type) == NULL
440 && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
441 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
447 fprintf_filtered (stream, "(");
448 type_print (type, "", stream, -1);
449 fprintf_filtered (stream, ") ");
452 common_val_print (val, stream, 0, &opts, current_language);
457 show_pascal_static_field_print (struct ui_file *file, int from_tty,
458 struct cmd_list_element *c, const char *value)
460 fprintf_filtered (file, _("Printing of pascal static members is %s.\n"),
464 static struct obstack dont_print_vb_obstack;
465 static struct obstack dont_print_statmem_obstack;
467 static void pascal_object_print_static_field (struct value *,
468 struct ui_file *, int,
469 const struct value_print_options *);
471 static void pascal_object_print_value (struct type *, const gdb_byte *,
473 CORE_ADDR, struct ui_file *, int,
474 const struct value *,
475 const struct value_print_options *,
478 /* It was changed to this after 2.4.5. */
479 const char pascal_vtbl_ptr_name[] =
480 {'_', '_', 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
482 /* Return truth value for assertion that TYPE is of the type
483 "pointer to virtual function". */
486 pascal_object_is_vtbl_ptr_type (struct type *type)
488 const char *type_name = type_name_no_tag (type);
490 return (type_name != NULL
491 && strcmp (type_name, pascal_vtbl_ptr_name) == 0);
494 /* Return truth value for the assertion that TYPE is of the type
495 "pointer to virtual function table". */
498 pascal_object_is_vtbl_member (struct type *type)
500 if (TYPE_CODE (type) == TYPE_CODE_PTR)
502 type = TYPE_TARGET_TYPE (type);
503 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
505 type = TYPE_TARGET_TYPE (type);
506 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* If not using
508 || TYPE_CODE (type) == TYPE_CODE_PTR) /* If using thunks. */
510 /* Virtual functions tables are full of pointers
511 to virtual functions. */
512 return pascal_object_is_vtbl_ptr_type (type);
519 /* Mutually recursive subroutines of pascal_object_print_value and
520 c_val_print to print out a structure's fields:
521 pascal_object_print_value_fields and pascal_object_print_value.
523 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
524 same meanings as in pascal_object_print_value and c_val_print.
526 DONT_PRINT is an array of baseclass types that we
527 should not print, or zero if called from top level. */
530 pascal_object_print_value_fields (struct type *type, const gdb_byte *valaddr,
532 CORE_ADDR address, struct ui_file *stream,
534 const struct value *val,
535 const struct value_print_options *options,
536 struct type **dont_print_vb,
537 int dont_print_statmem)
539 int i, len, n_baseclasses;
540 char *last_dont_print
541 = (char *) obstack_next_free (&dont_print_statmem_obstack);
543 type = check_typedef (type);
545 fprintf_filtered (stream, "{");
546 len = TYPE_NFIELDS (type);
547 n_baseclasses = TYPE_N_BASECLASSES (type);
549 /* Print out baseclasses such that we don't print
550 duplicates of virtual baseclasses. */
551 if (n_baseclasses > 0)
552 pascal_object_print_value (type, valaddr, offset, address,
553 stream, recurse + 1, val,
554 options, dont_print_vb);
556 if (!len && n_baseclasses == 1)
557 fprintf_filtered (stream, "<No data fields>");
560 struct obstack tmp_obstack = dont_print_statmem_obstack;
563 if (dont_print_statmem == 0)
565 /* If we're at top level, carve out a completely fresh
566 chunk of the obstack and use that until this particular
567 invocation returns. */
568 obstack_finish (&dont_print_statmem_obstack);
571 for (i = n_baseclasses; i < len; i++)
573 /* If requested, skip printing of static fields. */
574 if (!options->pascal_static_field_print
575 && field_is_static (&TYPE_FIELD (type, i)))
578 fprintf_filtered (stream, ", ");
579 else if (n_baseclasses > 0)
581 if (options->prettyformat)
583 fprintf_filtered (stream, "\n");
584 print_spaces_filtered (2 + 2 * recurse, stream);
585 fputs_filtered ("members of ", stream);
586 fputs_filtered (type_name_no_tag (type), stream);
587 fputs_filtered (": ", stream);
592 if (options->prettyformat)
594 fprintf_filtered (stream, "\n");
595 print_spaces_filtered (2 + 2 * recurse, stream);
599 wrap_here (n_spaces (2 + 2 * recurse));
602 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
604 if (field_is_static (&TYPE_FIELD (type, i)))
605 fputs_filtered ("static ", stream);
606 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
608 DMGL_PARAMS | DMGL_ANSI);
609 annotate_field_name_end ();
610 fputs_filtered (" = ", stream);
611 annotate_field_value ();
613 if (!field_is_static (&TYPE_FIELD (type, i))
614 && TYPE_FIELD_PACKED (type, i))
618 /* Bitfields require special handling, especially due to byte
620 if (TYPE_FIELD_IGNORE (type, i))
622 fputs_filtered ("<optimized out or zero length>", stream);
624 else if (value_bits_synthetic_pointer (val,
625 TYPE_FIELD_BITPOS (type,
627 TYPE_FIELD_BITSIZE (type,
630 fputs_filtered (_("<synthetic pointer>"), stream);
634 struct value_print_options opts = *options;
636 v = value_field_bitfield (type, i, valaddr, offset, val);
639 common_val_print (v, stream, recurse + 1, &opts,
645 if (TYPE_FIELD_IGNORE (type, i))
647 fputs_filtered ("<optimized out or zero length>", stream);
649 else if (field_is_static (&TYPE_FIELD (type, i)))
651 /* struct value *v = value_static_field (type, i);
655 v = value_field_bitfield (type, i, valaddr, offset, val);
658 val_print_optimized_out (NULL, stream);
660 pascal_object_print_static_field (v, stream, recurse + 1,
665 struct value_print_options opts = *options;
668 /* val_print (TYPE_FIELD_TYPE (type, i),
669 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
670 address + TYPE_FIELD_BITPOS (type, i) / 8, 0,
671 stream, format, 0, recurse + 1, pretty); */
672 val_print (TYPE_FIELD_TYPE (type, i),
673 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
674 address, stream, recurse + 1, val, &opts,
678 annotate_field_end ();
681 if (dont_print_statmem == 0)
683 /* Free the space used to deal with the printing
684 of the members from top level. */
685 obstack_free (&dont_print_statmem_obstack, last_dont_print);
686 dont_print_statmem_obstack = tmp_obstack;
689 if (options->prettyformat)
691 fprintf_filtered (stream, "\n");
692 print_spaces_filtered (2 * recurse, stream);
695 fprintf_filtered (stream, "}");
698 /* Special val_print routine to avoid printing multiple copies of virtual
702 pascal_object_print_value (struct type *type, const gdb_byte *valaddr,
704 CORE_ADDR address, struct ui_file *stream,
706 const struct value *val,
707 const struct value_print_options *options,
708 struct type **dont_print_vb)
710 struct type **last_dont_print
711 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
712 struct obstack tmp_obstack = dont_print_vb_obstack;
713 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
715 if (dont_print_vb == 0)
717 /* If we're at top level, carve out a completely fresh
718 chunk of the obstack and use that until this particular
719 invocation returns. */
720 /* Bump up the high-water mark. Now alpha is omega. */
721 obstack_finish (&dont_print_vb_obstack);
724 for (i = 0; i < n_baseclasses; i++)
727 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
728 const char *basename = type_name_no_tag (baseclass);
729 const gdb_byte *base_valaddr = NULL;
733 if (BASETYPE_VIA_VIRTUAL (type, i))
735 struct type **first_dont_print
736 = (struct type **) obstack_base (&dont_print_vb_obstack);
738 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
742 if (baseclass == first_dont_print[j])
745 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
752 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
754 CATCH (ex, RETURN_MASK_ERROR)
756 if (ex.error == NOT_AVAILABLE_ERROR)
765 /* The virtual base class pointer might have been clobbered by the
766 user program. Make sure that it still points to a valid memory
769 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
772 struct cleanup *back_to;
774 buf = (gdb_byte *) xmalloc (TYPE_LENGTH (baseclass));
775 back_to = make_cleanup (xfree, buf);
778 if (target_read_memory (address + boffset, buf,
779 TYPE_LENGTH (baseclass)) != 0)
781 address = address + boffset;
784 do_cleanups (back_to);
787 base_valaddr = valaddr;
790 if (options->prettyformat)
792 fprintf_filtered (stream, "\n");
793 print_spaces_filtered (2 * recurse, stream);
795 fputs_filtered ("<", stream);
796 /* Not sure what the best notation is in the case where there is no
799 fputs_filtered (basename ? basename : "", stream);
800 fputs_filtered ("> = ", stream);
803 val_print_unavailable (stream);
805 val_print_invalid_address (stream);
807 pascal_object_print_value_fields (baseclass, base_valaddr,
808 thisoffset + boffset, address,
809 stream, recurse, val, options,
810 (struct type **) obstack_base (&dont_print_vb_obstack),
812 fputs_filtered (", ", stream);
818 if (dont_print_vb == 0)
820 /* Free the space used to deal with the printing
821 of this type from top level. */
822 obstack_free (&dont_print_vb_obstack, last_dont_print);
823 /* Reset watermark so that we can continue protecting
824 ourselves from whatever we were protecting ourselves. */
825 dont_print_vb_obstack = tmp_obstack;
829 /* Print value of a static member.
830 To avoid infinite recursion when printing a class that contains
831 a static instance of the class, we keep the addresses of all printed
832 static member classes in an obstack and refuse to print them more
835 VAL contains the value to print, STREAM, RECURSE, and OPTIONS
836 have the same meanings as in c_val_print. */
839 pascal_object_print_static_field (struct value *val,
840 struct ui_file *stream,
842 const struct value_print_options *options)
844 struct type *type = value_type (val);
845 struct value_print_options opts;
847 if (value_entirely_optimized_out (val))
849 val_print_optimized_out (val, stream);
853 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
855 CORE_ADDR *first_dont_print, addr;
859 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
860 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
865 if (value_address (val) == first_dont_print[i])
868 <same as static member of an already seen type>",
874 addr = value_address (val);
875 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
878 type = check_typedef (type);
879 pascal_object_print_value_fields (type,
880 value_contents_for_printing (val),
881 value_embedded_offset (val),
884 val, options, NULL, 1);
890 common_val_print (val, stream, recurse, &opts, current_language);
893 /* -Wmissing-prototypes */
894 extern initialize_file_ftype _initialize_pascal_valprint;
897 _initialize_pascal_valprint (void)
899 add_setshow_boolean_cmd ("pascal_static-members", class_support,
900 &user_print_options.pascal_static_field_print, _("\
901 Set printing of pascal static members."), _("\
902 Show printing of pascal static members."), NULL,
904 show_pascal_static_field_print,
905 &setprintlist, &showprintlist);