1 /* Support for printing Pascal values for GDB, the GNU debugger.
3 Copyright (C) 2000-2013 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"
40 #include "exceptions.h"
43 /* Decorations for Pascal. */
45 static const struct generic_val_print_decorations p_decorations =
55 /* See val_print for a description of the various parameters of this
56 function; they are identical. */
59 pascal_val_print (struct type *type, const gdb_byte *valaddr,
60 int embedded_offset, CORE_ADDR address,
61 struct ui_file *stream, int recurse,
62 const struct value *original_value,
63 const struct value_print_options *options)
65 struct gdbarch *gdbarch = get_type_arch (type);
66 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
67 unsigned int i = 0; /* Number of characters printed */
69 LONGEST low_bound, high_bound;
72 int length_pos, length_size, string_pos;
73 struct type *char_type;
78 switch (TYPE_CODE (type))
81 if (get_array_bounds (type, &low_bound, &high_bound))
83 len = high_bound - low_bound + 1;
84 elttype = check_typedef (TYPE_TARGET_TYPE (type));
85 eltlen = TYPE_LENGTH (elttype);
86 if (options->prettyprint_arrays)
88 print_spaces_filtered (2 + 2 * recurse, stream);
90 /* If 's' format is used, try to print out as string.
91 If no format is given, print as string if element type
92 is of TYPE_CODE_CHAR and element size is 1,2 or 4. */
93 if (options->format == 's'
94 || ((eltlen == 1 || eltlen == 2 || eltlen == 4)
95 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
96 && options->format == 0))
98 /* If requested, look for the first null char and only print
100 if (options->stop_print_at_null)
102 unsigned int temp_len;
104 /* Look for a NULL char. */
106 extract_unsigned_integer (valaddr + embedded_offset +
107 temp_len * eltlen, eltlen,
109 && temp_len < len && temp_len < options->print_max;
114 LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
115 valaddr + embedded_offset, len, NULL, 0,
121 fprintf_filtered (stream, "{");
122 /* If this is a virtual function table, print the 0th
123 entry specially, and the rest of the members normally. */
124 if (pascal_object_is_vtbl_ptr_type (elttype))
127 fprintf_filtered (stream, "%d vtable entries", len - 1);
133 val_print_array_elements (type, valaddr, embedded_offset,
134 address, stream, recurse,
135 original_value, options, i);
136 fprintf_filtered (stream, "}");
140 /* Array of unspecified length: treat like pointer to first elt. */
141 addr = address + embedded_offset;
142 goto print_unpacked_pointer;
145 if (options->format && options->format != 's')
147 val_print_scalar_formatted (type, valaddr, embedded_offset,
148 original_value, options, 0, stream);
151 if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
153 /* Print the unmangled name if desired. */
154 /* Print vtable entry - we only get here if we ARE using
155 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
156 /* Extract the address, assume that it is unsigned. */
157 addr = extract_unsigned_integer (valaddr + embedded_offset,
158 TYPE_LENGTH (type), byte_order);
159 print_address_demangle (options, gdbarch, addr, stream, demangle);
162 check_typedef (TYPE_TARGET_TYPE (type));
164 addr = unpack_pointer (type, valaddr + embedded_offset);
165 print_unpacked_pointer:
166 elttype = check_typedef (TYPE_TARGET_TYPE (type));
168 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
170 /* Try to print what function it points to. */
171 print_address_demangle (options, gdbarch, addr, stream, demangle);
175 if (options->addressprint && options->format != 's')
177 fputs_filtered (paddress (gdbarch, addr), stream);
181 /* For a pointer to char or unsigned char, also print the string
182 pointed to, unless pointer is null. */
183 if (((TYPE_LENGTH (elttype) == 1
184 && (TYPE_CODE (elttype) == TYPE_CODE_INT
185 || TYPE_CODE (elttype) == TYPE_CODE_CHAR))
186 || ((TYPE_LENGTH (elttype) == 2 || TYPE_LENGTH (elttype) == 4)
187 && TYPE_CODE (elttype) == TYPE_CODE_CHAR))
188 && (options->format == 0 || options->format == 's')
192 fputs_filtered (" ", stream);
193 /* No wide string yet. */
194 i = val_print_string (elttype, NULL, addr, -1, stream, options);
196 /* Also for pointers to pascal strings. */
197 /* Note: this is Free Pascal specific:
198 as GDB does not recognize stabs pascal strings
199 Pascal strings are mapped to records
200 with lowercase names PM. */
201 if (is_pascal_string_type (elttype, &length_pos, &length_size,
202 &string_pos, &char_type, NULL)
205 ULONGEST string_length;
209 fputs_filtered (" ", stream);
210 buffer = xmalloc (length_size);
211 read_memory (addr + length_pos, buffer, length_size);
212 string_length = extract_unsigned_integer (buffer, length_size,
215 i = val_print_string (char_type, NULL,
216 addr + string_pos, string_length,
219 else if (pascal_object_is_vtbl_member (type))
221 /* Print vtbl's nicely. */
222 CORE_ADDR vt_address = unpack_pointer (type,
223 valaddr + embedded_offset);
224 struct minimal_symbol *msymbol =
225 lookup_minimal_symbol_by_pc (vt_address);
227 /* If 'symbol_print' is set, we did the work above. */
228 if (!options->symbol_print
230 && (vt_address == SYMBOL_VALUE_ADDRESS (msymbol)))
233 fputs_filtered (" ", stream);
234 fputs_filtered ("<", stream);
235 fputs_filtered (SYMBOL_PRINT_NAME (msymbol), stream);
236 fputs_filtered (">", stream);
239 if (vt_address && options->vtblprint)
241 struct value *vt_val;
242 struct symbol *wsym = (struct symbol *) NULL;
244 struct block *block = (struct block *) NULL;
245 struct field_of_this_result is_this_fld;
248 fputs_filtered (" ", stream);
251 wsym = lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol), block,
252 VAR_DOMAIN, &is_this_fld);
256 wtype = SYMBOL_TYPE (wsym);
260 wtype = TYPE_TARGET_TYPE (type);
262 vt_val = value_at (wtype, vt_address);
263 common_val_print (vt_val, stream, recurse + 1, options,
267 fprintf_filtered (stream, "\n");
268 print_spaces_filtered (2 + 2 * recurse, stream);
277 case TYPE_CODE_FLAGS:
279 case TYPE_CODE_RANGE:
283 case TYPE_CODE_ERROR:
284 case TYPE_CODE_UNDEF:
287 generic_val_print (type, valaddr, embedded_offset, address,
288 stream, recurse, original_value, options,
292 case TYPE_CODE_UNION:
293 if (recurse && !options->unionprint)
295 fprintf_filtered (stream, "{...}");
299 case TYPE_CODE_STRUCT:
300 if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
302 /* Print the unmangled name if desired. */
303 /* Print vtable entry - we only get here if NOT using
304 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
305 /* Extract the address, assume that it is unsigned. */
306 print_address_demangle
308 extract_unsigned_integer (valaddr + embedded_offset
309 + TYPE_FIELD_BITPOS (type,
310 VTBL_FNADDR_OFFSET) / 8,
311 TYPE_LENGTH (TYPE_FIELD_TYPE (type,
312 VTBL_FNADDR_OFFSET)),
318 if (is_pascal_string_type (type, &length_pos, &length_size,
319 &string_pos, &char_type, NULL))
321 len = extract_unsigned_integer (valaddr + embedded_offset
322 + length_pos, length_size,
324 LA_PRINT_STRING (stream, char_type,
325 valaddr + embedded_offset + string_pos,
326 len, NULL, 0, options);
329 pascal_object_print_value_fields (type, valaddr, embedded_offset,
330 address, stream, recurse,
331 original_value, options,
337 elttype = TYPE_INDEX_TYPE (type);
338 CHECK_TYPEDEF (elttype);
339 if (TYPE_STUB (elttype))
341 fprintf_filtered (stream, "<incomplete type>");
347 struct type *range = elttype;
348 LONGEST low_bound, high_bound;
352 fputs_filtered ("[", stream);
354 i = get_discrete_bounds (range, &low_bound, &high_bound);
355 if (low_bound == 0 && high_bound == -1 && TYPE_LENGTH (type) > 0)
357 /* If we know the size of the set type, we can figure out the
360 high_bound = TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1;
361 TYPE_HIGH_BOUND (range) = high_bound;
366 fputs_filtered ("<error value>", stream);
370 for (i = low_bound; i <= high_bound; i++)
372 int element = value_bit_index (type,
373 valaddr + embedded_offset, i);
378 goto maybe_bad_bstring;
383 fputs_filtered (", ", stream);
384 print_type_scalar (range, i, stream);
387 if (i + 1 <= high_bound
388 && value_bit_index (type,
389 valaddr + embedded_offset, ++i))
393 fputs_filtered ("..", stream);
394 while (i + 1 <= high_bound
395 && value_bit_index (type,
396 valaddr + embedded_offset,
399 print_type_scalar (range, j, stream);
404 fputs_filtered ("]", stream);
409 error (_("Invalid pascal type code %d in symbol table."),
416 pascal_value_print (struct value *val, struct ui_file *stream,
417 const struct value_print_options *options)
419 struct type *type = value_type (val);
420 struct value_print_options opts = *options;
424 /* If it is a pointer, indicate what it points to.
426 Print type also if it is a reference.
428 Object pascal: if it is a member pointer, we will take care
429 of that when we print it. */
430 if (TYPE_CODE (type) == TYPE_CODE_PTR
431 || TYPE_CODE (type) == TYPE_CODE_REF)
433 /* Hack: remove (char *) for char strings. Their
434 type is indicated by the quoted string anyway. */
435 if (TYPE_CODE (type) == TYPE_CODE_PTR
436 && TYPE_NAME (type) == NULL
437 && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
438 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
444 fprintf_filtered (stream, "(");
445 type_print (type, "", stream, -1);
446 fprintf_filtered (stream, ") ");
449 common_val_print (val, stream, 0, &opts, current_language);
454 show_pascal_static_field_print (struct ui_file *file, int from_tty,
455 struct cmd_list_element *c, const char *value)
457 fprintf_filtered (file, _("Printing of pascal static members is %s.\n"),
461 static struct obstack dont_print_vb_obstack;
462 static struct obstack dont_print_statmem_obstack;
464 static void pascal_object_print_static_field (struct value *,
465 struct ui_file *, int,
466 const struct value_print_options *);
468 static void pascal_object_print_value (struct type *, const gdb_byte *,
470 CORE_ADDR, struct ui_file *, int,
471 const struct value *,
472 const struct value_print_options *,
475 /* It was changed to this after 2.4.5. */
476 const char pascal_vtbl_ptr_name[] =
477 {'_', '_', 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
479 /* Return truth value for assertion that TYPE is of the type
480 "pointer to virtual function". */
483 pascal_object_is_vtbl_ptr_type (struct type *type)
485 const char *typename = type_name_no_tag (type);
487 return (typename != NULL
488 && strcmp (typename, pascal_vtbl_ptr_name) == 0);
491 /* Return truth value for the assertion that TYPE is of the type
492 "pointer to virtual function table". */
495 pascal_object_is_vtbl_member (struct type *type)
497 if (TYPE_CODE (type) == TYPE_CODE_PTR)
499 type = TYPE_TARGET_TYPE (type);
500 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
502 type = TYPE_TARGET_TYPE (type);
503 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* If not using
505 || TYPE_CODE (type) == TYPE_CODE_PTR) /* If using thunks. */
507 /* Virtual functions tables are full of pointers
508 to virtual functions. */
509 return pascal_object_is_vtbl_ptr_type (type);
516 /* Mutually recursive subroutines of pascal_object_print_value and
517 c_val_print to print out a structure's fields:
518 pascal_object_print_value_fields and pascal_object_print_value.
520 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
521 same meanings as in pascal_object_print_value and c_val_print.
523 DONT_PRINT is an array of baseclass types that we
524 should not print, or zero if called from top level. */
527 pascal_object_print_value_fields (struct type *type, const gdb_byte *valaddr,
529 CORE_ADDR address, struct ui_file *stream,
531 const struct value *val,
532 const struct value_print_options *options,
533 struct type **dont_print_vb,
534 int dont_print_statmem)
536 int i, len, n_baseclasses;
537 char *last_dont_print = obstack_next_free (&dont_print_statmem_obstack);
539 CHECK_TYPEDEF (type);
541 fprintf_filtered (stream, "{");
542 len = TYPE_NFIELDS (type);
543 n_baseclasses = TYPE_N_BASECLASSES (type);
545 /* Print out baseclasses such that we don't print
546 duplicates of virtual baseclasses. */
547 if (n_baseclasses > 0)
548 pascal_object_print_value (type, valaddr, offset, address,
549 stream, recurse + 1, val,
550 options, dont_print_vb);
552 if (!len && n_baseclasses == 1)
553 fprintf_filtered (stream, "<No data fields>");
556 struct obstack tmp_obstack = dont_print_statmem_obstack;
559 if (dont_print_statmem == 0)
561 /* If we're at top level, carve out a completely fresh
562 chunk of the obstack and use that until this particular
563 invocation returns. */
564 obstack_finish (&dont_print_statmem_obstack);
567 for (i = n_baseclasses; i < len; i++)
569 /* If requested, skip printing of static fields. */
570 if (!options->pascal_static_field_print
571 && field_is_static (&TYPE_FIELD (type, i)))
574 fprintf_filtered (stream, ", ");
575 else if (n_baseclasses > 0)
579 fprintf_filtered (stream, "\n");
580 print_spaces_filtered (2 + 2 * recurse, stream);
581 fputs_filtered ("members of ", stream);
582 fputs_filtered (type_name_no_tag (type), stream);
583 fputs_filtered (": ", stream);
590 fprintf_filtered (stream, "\n");
591 print_spaces_filtered (2 + 2 * recurse, stream);
595 wrap_here (n_spaces (2 + 2 * recurse));
598 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
600 if (field_is_static (&TYPE_FIELD (type, i)))
601 fputs_filtered ("static ", stream);
602 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
604 DMGL_PARAMS | DMGL_ANSI);
605 annotate_field_name_end ();
606 fputs_filtered (" = ", stream);
607 annotate_field_value ();
609 if (!field_is_static (&TYPE_FIELD (type, i))
610 && TYPE_FIELD_PACKED (type, i))
614 /* Bitfields require special handling, especially due to byte
616 if (TYPE_FIELD_IGNORE (type, i))
618 fputs_filtered ("<optimized out or zero length>", stream);
620 else if (value_bits_synthetic_pointer (val,
621 TYPE_FIELD_BITPOS (type,
623 TYPE_FIELD_BITSIZE (type,
626 fputs_filtered (_("<synthetic pointer>"), stream);
628 else if (!value_bits_valid (val, TYPE_FIELD_BITPOS (type, i),
629 TYPE_FIELD_BITSIZE (type, i)))
631 val_print_optimized_out (stream);
635 struct value_print_options opts = *options;
637 v = value_field_bitfield (type, i, valaddr, offset, val);
640 common_val_print (v, stream, recurse + 1, &opts,
646 if (TYPE_FIELD_IGNORE (type, i))
648 fputs_filtered ("<optimized out or zero length>", stream);
650 else if (field_is_static (&TYPE_FIELD (type, i)))
652 /* struct value *v = value_static_field (type, i);
656 v = value_field_bitfield (type, i, valaddr, offset, val);
659 val_print_optimized_out (stream);
661 pascal_object_print_static_field (v, stream, recurse + 1,
666 struct value_print_options opts = *options;
669 /* val_print (TYPE_FIELD_TYPE (type, i),
670 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
671 address + TYPE_FIELD_BITPOS (type, i) / 8, 0,
672 stream, format, 0, recurse + 1, pretty); */
673 val_print (TYPE_FIELD_TYPE (type, i),
674 valaddr, offset + TYPE_FIELD_BITPOS (type, i) / 8,
675 address, stream, recurse + 1, val, &opts,
679 annotate_field_end ();
682 if (dont_print_statmem == 0)
684 /* Free the space used to deal with the printing
685 of the members from top level. */
686 obstack_free (&dont_print_statmem_obstack, last_dont_print);
687 dont_print_statmem_obstack = tmp_obstack;
692 fprintf_filtered (stream, "\n");
693 print_spaces_filtered (2 * recurse, stream);
696 fprintf_filtered (stream, "}");
699 /* Special val_print routine to avoid printing multiple copies of virtual
703 pascal_object_print_value (struct type *type, const gdb_byte *valaddr,
705 CORE_ADDR address, struct ui_file *stream,
707 const struct value *val,
708 const struct value_print_options *options,
709 struct type **dont_print_vb)
711 struct type **last_dont_print
712 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
713 struct obstack tmp_obstack = dont_print_vb_obstack;
714 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
716 if (dont_print_vb == 0)
718 /* If we're at top level, carve out a completely fresh
719 chunk of the obstack and use that until this particular
720 invocation returns. */
721 /* Bump up the high-water mark. Now alpha is omega. */
722 obstack_finish (&dont_print_vb_obstack);
725 for (i = 0; i < n_baseclasses; i++)
728 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
729 const char *basename = type_name_no_tag (baseclass);
730 const gdb_byte *base_valaddr = NULL;
732 volatile struct gdb_exception ex;
735 if (BASETYPE_VIA_VIRTUAL (type, i))
737 struct type **first_dont_print
738 = (struct type **) obstack_base (&dont_print_vb_obstack);
740 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
744 if (baseclass == first_dont_print[j])
747 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
752 TRY_CATCH (ex, RETURN_MASK_ERROR)
754 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
756 if (ex.reason < 0 && ex.error == NOT_AVAILABLE_ERROR)
758 else if (ex.reason < 0)
764 /* The virtual base class pointer might have been clobbered by the
765 user program. Make sure that it still points to a valid memory
768 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
771 struct cleanup *back_to;
773 buf = xmalloc (TYPE_LENGTH (baseclass));
774 back_to = make_cleanup (xfree, buf);
777 if (target_read_memory (address + boffset, buf,
778 TYPE_LENGTH (baseclass)) != 0)
780 address = address + boffset;
783 do_cleanups (back_to);
786 base_valaddr = valaddr;
791 fprintf_filtered (stream, "\n");
792 print_spaces_filtered (2 * recurse, stream);
794 fputs_filtered ("<", stream);
795 /* Not sure what the best notation is in the case where there is no
798 fputs_filtered (basename ? basename : "", stream);
799 fputs_filtered ("> = ", stream);
802 val_print_unavailable (stream);
804 val_print_invalid_address (stream);
806 pascal_object_print_value_fields (baseclass, base_valaddr,
807 thisoffset + boffset, address,
808 stream, recurse, val, options,
809 (struct type **) obstack_base (&dont_print_vb_obstack),
811 fputs_filtered (", ", stream);
817 if (dont_print_vb == 0)
819 /* Free the space used to deal with the printing
820 of this type from top level. */
821 obstack_free (&dont_print_vb_obstack, last_dont_print);
822 /* Reset watermark so that we can continue protecting
823 ourselves from whatever we were protecting ourselves. */
824 dont_print_vb_obstack = tmp_obstack;
828 /* Print value of a static member.
829 To avoid infinite recursion when printing a class that contains
830 a static instance of the class, we keep the addresses of all printed
831 static member classes in an obstack and refuse to print them more
834 VAL contains the value to print, STREAM, RECURSE, and OPTIONS
835 have the same meanings as in c_val_print. */
838 pascal_object_print_static_field (struct value *val,
839 struct ui_file *stream,
841 const struct value_print_options *options)
843 struct type *type = value_type (val);
844 struct value_print_options opts;
846 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
848 CORE_ADDR *first_dont_print, addr;
852 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
853 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
858 if (value_address (val) == first_dont_print[i])
861 <same as static member of an already seen type>",
867 addr = value_address (val);
868 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
871 CHECK_TYPEDEF (type);
872 pascal_object_print_value_fields (type,
873 value_contents_for_printing (val),
874 value_embedded_offset (val),
877 val, options, NULL, 1);
883 common_val_print (val, stream, recurse, &opts, current_language);
886 /* -Wmissing-prototypes */
887 extern initialize_file_ftype _initialize_pascal_valprint;
890 _initialize_pascal_valprint (void)
892 add_setshow_boolean_cmd ("pascal_static-members", class_support,
893 &user_print_options.pascal_static_field_print, _("\
894 Set printing of pascal static members."), _("\
895 Show printing of pascal static members."), NULL,
897 show_pascal_static_field_print,
898 &setprintlist, &showprintlist);