PR25645, readelf segfault reading fuzzed alpha-vms binary
[deliverable/binutils-gdb.git] / gdb / p-valprint.c
CommitLineData
373a8247 1/* Support for printing Pascal values for GDB, the GNU debugger.
5bcca90b 2
b811d2c2 3 Copyright (C) 2000-2020 Free Software Foundation, Inc.
373a8247
PM
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
373a8247
PM
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
373a8247
PM
19
20/* This file is derived from c-valprint.c */
21
22#include "defs.h"
04ea0df1 23#include "gdb_obstack.h"
373a8247
PM
24#include "symtab.h"
25#include "gdbtypes.h"
26#include "expression.h"
27#include "value.h"
28#include "command.h"
29#include "gdbcmd.h"
30#include "gdbcore.h"
31#include "demangle.h"
32#include "valprint.h"
3172dc30 33#include "typeprint.h"
373a8247
PM
34#include "language.h"
35#include "target.h"
36#include "annotate.h"
37#include "p-lang.h"
eb43544b 38#include "cp-abi.h"
d3cbe7ef 39#include "cp-support.h"
77e371c0 40#include "objfiles.h"
268a13a5 41#include "gdbsupport/byte-vector.h"
7f6aba03 42#include "cli/cli-style.h"
373a8247
PM
43\f
44
e88acd96
TT
45/* Decorations for Pascal. */
46
47static const struct generic_val_print_decorations p_decorations =
48{
49 "",
50 " + ",
51 " * I",
52 "true",
53 "false",
00272ec4
TT
54 "void",
55 "{",
56 "}"
e88acd96
TT
57};
58
32b72a42 59/* See val_print for a description of the various parameters of this
d3eab38a 60 function; they are identical. */
373a8247 61
d3eab38a 62void
e8b24d9f 63pascal_val_print (struct type *type,
a2bd3dcd 64 int embedded_offset, CORE_ADDR address,
79a45b7d 65 struct ui_file *stream, int recurse,
e8b24d9f 66 struct value *original_value,
79a45b7d 67 const struct value_print_options *options)
373a8247 68{
5af949e3 69 struct gdbarch *gdbarch = get_type_arch (type);
34877895 70 enum bfd_endian byte_order = type_byte_order (type);
52f0bd74 71 unsigned int i = 0; /* Number of characters printed */
373a8247
PM
72 unsigned len;
73 struct type *elttype;
74 unsigned eltlen;
5598ce11 75 int length_pos, length_size, string_pos;
6c7a06a3 76 struct type *char_type;
373a8247 77 CORE_ADDR addr;
b012acdd 78 int want_space = 0;
e8b24d9f 79 const gdb_byte *valaddr = value_contents_for_printing (original_value);
373a8247 80
f168693b 81 type = check_typedef (type);
373a8247
PM
82 switch (TYPE_CODE (type))
83 {
84 case TYPE_CODE_ARRAY:
b926417a
TT
85 {
86 LONGEST low_bound, high_bound;
373a8247 87
b926417a
TT
88 if (get_array_bounds (type, &low_bound, &high_bound))
89 {
90 len = high_bound - low_bound + 1;
91 elttype = check_typedef (TYPE_TARGET_TYPE (type));
92 eltlen = TYPE_LENGTH (elttype);
93 if (options->prettyformat_arrays)
94 {
95 print_spaces_filtered (2 + 2 * recurse, stream);
96 }
97 /* If 's' format is used, try to print out as string.
98 If no format is given, print as string if element type
99 is of TYPE_CODE_CHAR and element size is 1,2 or 4. */
100 if (options->format == 's'
101 || ((eltlen == 1 || eltlen == 2 || eltlen == 4)
102 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
103 && options->format == 0))
104 {
105 /* If requested, look for the first null char and only print
106 elements up to it. */
107 if (options->stop_print_at_null)
108 {
109 unsigned int temp_len;
110
111 /* Look for a NULL char. */
112 for (temp_len = 0;
113 extract_unsigned_integer (valaddr + embedded_offset +
114 temp_len * eltlen, eltlen,
115 byte_order)
116 && temp_len < len && temp_len < options->print_max;
117 temp_len++);
118 len = temp_len;
119 }
120
121 LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
122 valaddr + embedded_offset, len, NULL, 0,
123 options);
124 i = len;
125 }
126 else
127 {
128 fprintf_filtered (stream, "{");
129 /* If this is a virtual function table, print the 0th
130 entry specially, and the rest of the members normally. */
131 if (pascal_object_is_vtbl_ptr_type (elttype))
132 {
133 i = 1;
134 fprintf_filtered (stream, "%d vtable entries", len - 1);
135 }
136 else
137 {
138 i = 0;
139 }
140 val_print_array_elements (type, embedded_offset,
141 address, stream, recurse,
142 original_value, options, i);
143 fprintf_filtered (stream, "}");
144 }
145 break;
146 }
147 /* Array of unspecified length: treat like pointer to first elt. */
148 addr = address + embedded_offset;
149 }
373a8247
PM
150 goto print_unpacked_pointer;
151
152 case TYPE_CODE_PTR:
79a45b7d 153 if (options->format && options->format != 's')
373a8247 154 {
e8b24d9f 155 val_print_scalar_formatted (type, embedded_offset,
ab2188aa 156 original_value, options, 0, stream);
373a8247
PM
157 break;
158 }
79a45b7d 159 if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
373a8247
PM
160 {
161 /* Print the unmangled name if desired. */
162 /* Print vtable entry - we only get here if we ARE using
0df8b418 163 -fvtable_thunks. (Otherwise, look under TYPE_CODE_STRUCT.) */
b276f1bb 164 /* Extract the address, assume that it is unsigned. */
e17a4113
UW
165 addr = extract_unsigned_integer (valaddr + embedded_offset,
166 TYPE_LENGTH (type), byte_order);
edf0c1b7 167 print_address_demangle (options, gdbarch, addr, stream, demangle);
373a8247
PM
168 break;
169 }
91e8df85 170 check_typedef (TYPE_TARGET_TYPE (type));
e13eedd5
PM
171
172 addr = unpack_pointer (type, valaddr + embedded_offset);
173 print_unpacked_pointer:
174 elttype = check_typedef (TYPE_TARGET_TYPE (type));
175
176 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
373a8247 177 {
e13eedd5 178 /* Try to print what function it points to. */
edf0c1b7 179 print_address_demangle (options, gdbarch, addr, stream, demangle);
d3eab38a 180 return;
e13eedd5 181 }
373a8247 182
e13eedd5
PM
183 if (options->addressprint && options->format != 's')
184 {
185 fputs_filtered (paddress (gdbarch, addr), stream);
b012acdd 186 want_space = 1;
e13eedd5 187 }
373a8247 188
e13eedd5
PM
189 /* For a pointer to char or unsigned char, also print the string
190 pointed to, unless pointer is null. */
191 if (((TYPE_LENGTH (elttype) == 1
192 && (TYPE_CODE (elttype) == TYPE_CODE_INT
193 || TYPE_CODE (elttype) == TYPE_CODE_CHAR))
194 || ((TYPE_LENGTH (elttype) == 2 || TYPE_LENGTH (elttype) == 4)
195 && TYPE_CODE (elttype) == TYPE_CODE_CHAR))
196 && (options->format == 0 || options->format == 's')
197 && addr != 0)
198 {
b012acdd
TT
199 if (want_space)
200 fputs_filtered (" ", stream);
0df8b418 201 /* No wide string yet. */
09ca9e2e 202 i = val_print_string (elttype, NULL, addr, -1, stream, options);
e13eedd5 203 }
0df8b418 204 /* Also for pointers to pascal strings. */
e13eedd5
PM
205 /* Note: this is Free Pascal specific:
206 as GDB does not recognize stabs pascal strings
207 Pascal strings are mapped to records
0df8b418 208 with lowercase names PM. */
e13eedd5
PM
209 if (is_pascal_string_type (elttype, &length_pos, &length_size,
210 &string_pos, &char_type, NULL)
211 && addr != 0)
212 {
213 ULONGEST string_length;
7c543f7b 214 gdb_byte *buffer;
ad3bbd48 215
b012acdd
TT
216 if (want_space)
217 fputs_filtered (" ", stream);
7c543f7b 218 buffer = (gdb_byte *) xmalloc (length_size);
e13eedd5
PM
219 read_memory (addr + length_pos, buffer, length_size);
220 string_length = extract_unsigned_integer (buffer, length_size,
221 byte_order);
222 xfree (buffer);
09ca9e2e
TT
223 i = val_print_string (char_type, NULL,
224 addr + string_pos, string_length,
225 stream, options);
e13eedd5
PM
226 }
227 else if (pascal_object_is_vtbl_member (type))
228 {
0df8b418 229 /* Print vtbl's nicely. */
3e43a32a
MS
230 CORE_ADDR vt_address = unpack_pointer (type,
231 valaddr + embedded_offset);
7cbd4a93 232 struct bound_minimal_symbol msymbol =
ad3bbd48
MS
233 lookup_minimal_symbol_by_pc (vt_address);
234
9cb709b6
TT
235 /* If 'symbol_print' is set, we did the work above. */
236 if (!options->symbol_print
7cbd4a93 237 && (msymbol.minsym != NULL)
77e371c0 238 && (vt_address == BMSYMBOL_VALUE_ADDRESS (msymbol)))
373a8247 239 {
b012acdd
TT
240 if (want_space)
241 fputs_filtered (" ", stream);
242 fputs_filtered ("<", stream);
c9d95fa3 243 fputs_filtered (msymbol.minsym->print_name (), stream);
e13eedd5 244 fputs_filtered (">", stream);
b012acdd 245 want_space = 1;
373a8247 246 }
e13eedd5 247 if (vt_address && options->vtblprint)
373a8247 248 {
e13eedd5 249 struct value *vt_val;
be903358 250 struct symbol *wsym = NULL;
e13eedd5 251 struct type *wtype;
373a8247 252
b012acdd
TT
253 if (want_space)
254 fputs_filtered (" ", stream);
255
7cbd4a93 256 if (msymbol.minsym != NULL)
de63c46b 257 {
c9d95fa3 258 const char *search_name = msymbol.minsym->search_name ();
582942f4 259 wsym = lookup_symbol_search_name (search_name, NULL,
de63c46b
PA
260 VAR_DOMAIN).symbol;
261 }
e13eedd5
PM
262
263 if (wsym)
373a8247 264 {
e13eedd5 265 wtype = SYMBOL_TYPE (wsym);
373a8247 266 }
e13eedd5 267 else
373a8247 268 {
e13eedd5
PM
269 wtype = TYPE_TARGET_TYPE (type);
270 }
271 vt_val = value_at (wtype, vt_address);
272 common_val_print (vt_val, stream, recurse + 1, options,
273 current_language);
2a998fc0 274 if (options->prettyformat)
e13eedd5
PM
275 {
276 fprintf_filtered (stream, "\n");
277 print_spaces_filtered (2 + 2 * recurse, stream);
373a8247
PM
278 }
279 }
373a8247 280 }
e13eedd5 281
d3eab38a 282 return;
373a8247 283
373a8247 284 case TYPE_CODE_REF:
e88acd96
TT
285 case TYPE_CODE_ENUM:
286 case TYPE_CODE_FLAGS:
287 case TYPE_CODE_FUNC:
288 case TYPE_CODE_RANGE:
289 case TYPE_CODE_INT:
290 case TYPE_CODE_FLT:
291 case TYPE_CODE_VOID:
292 case TYPE_CODE_ERROR:
293 case TYPE_CODE_UNDEF:
294 case TYPE_CODE_BOOL:
295 case TYPE_CODE_CHAR:
e8b24d9f 296 generic_val_print (type, embedded_offset, address,
e88acd96
TT
297 stream, recurse, original_value, options,
298 &p_decorations);
373a8247
PM
299 break;
300
301 case TYPE_CODE_UNION:
79a45b7d 302 if (recurse && !options->unionprint)
373a8247
PM
303 {
304 fprintf_filtered (stream, "{...}");
305 break;
306 }
307 /* Fall through. */
308 case TYPE_CODE_STRUCT:
79a45b7d 309 if (options->vtblprint && pascal_object_is_vtbl_ptr_type (type))
373a8247
PM
310 {
311 /* Print the unmangled name if desired. */
312 /* Print vtable entry - we only get here if NOT using
0df8b418 313 -fvtable_thunks. (Otherwise, look under TYPE_CODE_PTR.) */
b276f1bb
AC
314 /* Extract the address, assume that it is unsigned. */
315 print_address_demangle
edf0c1b7 316 (options, gdbarch,
3e43a32a
MS
317 extract_unsigned_integer (valaddr + embedded_offset
318 + TYPE_FIELD_BITPOS (type,
319 VTBL_FNADDR_OFFSET) / 8,
320 TYPE_LENGTH (TYPE_FIELD_TYPE (type,
321 VTBL_FNADDR_OFFSET)),
322 byte_order),
b276f1bb 323 stream, demangle);
373a8247
PM
324 }
325 else
326 {
5598ce11 327 if (is_pascal_string_type (type, &length_pos, &length_size,
6c7a06a3 328 &string_pos, &char_type, NULL))
373a8247 329 {
3e43a32a
MS
330 len = extract_unsigned_integer (valaddr + embedded_offset
331 + length_pos, length_size,
332 byte_order);
6ced1581 333 LA_PRINT_STRING (stream, char_type,
be759fcf
PM
334 valaddr + embedded_offset + string_pos,
335 len, NULL, 0, options);
373a8247
PM
336 }
337 else
490f124f 338 pascal_object_print_value_fields (type, valaddr, embedded_offset,
3e43a32a
MS
339 address, stream, recurse,
340 original_value, options,
341 NULL, 0);
373a8247
PM
342 }
343 break;
344
373a8247
PM
345 case TYPE_CODE_SET:
346 elttype = TYPE_INDEX_TYPE (type);
f168693b 347 elttype = check_typedef (elttype);
74a9bb82 348 if (TYPE_STUB (elttype))
373a8247 349 {
7f6aba03 350 fprintf_styled (stream, metadata_style.style (), "<incomplete type>");
373a8247
PM
351 break;
352 }
353 else
354 {
355 struct type *range = elttype;
356 LONGEST low_bound, high_bound;
373a8247
PM
357 int need_comma = 0;
358
6b1755ce 359 fputs_filtered ("[", stream);
373a8247 360
b926417a 361 int bound_info = get_discrete_bounds (range, &low_bound, &high_bound);
7a081a30
PM
362 if (low_bound == 0 && high_bound == -1 && TYPE_LENGTH (type) > 0)
363 {
364 /* If we know the size of the set type, we can figure out the
365 maximum value. */
b926417a 366 bound_info = 0;
7a081a30
PM
367 high_bound = TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1;
368 TYPE_HIGH_BOUND (range) = high_bound;
369 }
373a8247 370 maybe_bad_bstring:
b926417a 371 if (bound_info < 0)
373a8247 372 {
7f6aba03 373 fputs_styled ("<error value>", metadata_style.style (), stream);
373a8247
PM
374 goto done;
375 }
376
377 for (i = low_bound; i <= high_bound; i++)
378 {
3e43a32a
MS
379 int element = value_bit_index (type,
380 valaddr + embedded_offset, i);
ad3bbd48 381
373a8247
PM
382 if (element < 0)
383 {
384 i = element;
385 goto maybe_bad_bstring;
386 }
6b1755ce 387 if (element)
373a8247
PM
388 {
389 if (need_comma)
390 fputs_filtered (", ", stream);
391 print_type_scalar (range, i, stream);
392 need_comma = 1;
393
3e43a32a
MS
394 if (i + 1 <= high_bound
395 && value_bit_index (type,
396 valaddr + embedded_offset, ++i))
373a8247
PM
397 {
398 int j = i;
ad3bbd48 399
373a8247
PM
400 fputs_filtered ("..", stream);
401 while (i + 1 <= high_bound
3e43a32a
MS
402 && value_bit_index (type,
403 valaddr + embedded_offset,
404 ++i))
373a8247
PM
405 j = i;
406 print_type_scalar (range, j, stream);
407 }
408 }
409 }
410 done:
6b1755ce 411 fputs_filtered ("]", stream);
373a8247
PM
412 }
413 break;
414
373a8247 415 default:
3e43a32a
MS
416 error (_("Invalid pascal type code %d in symbol table."),
417 TYPE_CODE (type));
373a8247 418 }
373a8247
PM
419}
420\f
8e069a98 421void
79a45b7d
TT
422pascal_value_print (struct value *val, struct ui_file *stream,
423 const struct value_print_options *options)
373a8247 424{
df407dfe 425 struct type *type = value_type (val);
278582cb
PM
426 struct value_print_options opts = *options;
427
428 opts.deref_ref = 1;
373a8247
PM
429
430 /* If it is a pointer, indicate what it points to.
431
432 Print type also if it is a reference.
433
434 Object pascal: if it is a member pointer, we will take care
435 of that when we print it. */
b20a3440
PM
436 if (TYPE_CODE (type) == TYPE_CODE_PTR
437 || TYPE_CODE (type) == TYPE_CODE_REF)
373a8247
PM
438 {
439 /* Hack: remove (char *) for char strings. Their
0df8b418 440 type is indicated by the quoted string anyway. */
6ced1581 441 if (TYPE_CODE (type) == TYPE_CODE_PTR
b20a3440
PM
442 && TYPE_NAME (type) == NULL
443 && TYPE_NAME (TYPE_TARGET_TYPE (type)) != NULL
6314a349 444 && strcmp (TYPE_NAME (TYPE_TARGET_TYPE (type)), "char") == 0)
373a8247 445 {
0df8b418 446 /* Print nothing. */
373a8247
PM
447 }
448 else
449 {
450 fprintf_filtered (stream, "(");
451 type_print (type, "", stream, -1);
452 fprintf_filtered (stream, ") ");
453 }
454 }
8e069a98 455 common_val_print (val, stream, 0, &opts, current_language);
373a8247
PM
456}
457
458
920d2a44
AC
459static void
460show_pascal_static_field_print (struct ui_file *file, int from_tty,
461 struct cmd_list_element *c, const char *value)
462{
463 fprintf_filtered (file, _("Printing of pascal static members is %s.\n"),
464 value);
465}
373a8247
PM
466
467static struct obstack dont_print_vb_obstack;
468static struct obstack dont_print_statmem_obstack;
469
806048c6 470static void pascal_object_print_static_field (struct value *,
79a45b7d
TT
471 struct ui_file *, int,
472 const struct value_print_options *);
373a8247 473
fc1a4b47 474static void pascal_object_print_value (struct type *, const gdb_byte *,
6b850546 475 LONGEST,
79a45b7d 476 CORE_ADDR, struct ui_file *, int,
e8b24d9f 477 struct value *,
79a45b7d 478 const struct value_print_options *,
a2bd3dcd 479 struct type **);
373a8247 480
373a8247
PM
481/* It was changed to this after 2.4.5. */
482const char pascal_vtbl_ptr_name[] =
483{'_', '_', 'v', 't', 'b', 'l', '_', 'p', 't', 'r', '_', 't', 'y', 'p', 'e', 0};
484
485/* Return truth value for assertion that TYPE is of the type
486 "pointer to virtual function". */
487
488int
fba45db2 489pascal_object_is_vtbl_ptr_type (struct type *type)
373a8247 490{
a737d952 491 const char *type_name = TYPE_NAME (type);
373a8247 492
fe978cb0
PA
493 return (type_name != NULL
494 && strcmp (type_name, pascal_vtbl_ptr_name) == 0);
373a8247
PM
495}
496
497/* Return truth value for the assertion that TYPE is of the type
498 "pointer to virtual function table". */
499
500int
fba45db2 501pascal_object_is_vtbl_member (struct type *type)
373a8247
PM
502{
503 if (TYPE_CODE (type) == TYPE_CODE_PTR)
504 {
505 type = TYPE_TARGET_TYPE (type);
506 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
507 {
508 type = TYPE_TARGET_TYPE (type);
0df8b418
MS
509 if (TYPE_CODE (type) == TYPE_CODE_STRUCT /* If not using
510 thunks. */
511 || TYPE_CODE (type) == TYPE_CODE_PTR) /* If using thunks. */
373a8247
PM
512 {
513 /* Virtual functions tables are full of pointers
0df8b418 514 to virtual functions. */
373a8247
PM
515 return pascal_object_is_vtbl_ptr_type (type);
516 }
517 }
518 }
519 return 0;
520}
521
a2bd3dcd
AC
522/* Mutually recursive subroutines of pascal_object_print_value and
523 c_val_print to print out a structure's fields:
524 pascal_object_print_value_fields and pascal_object_print_value.
373a8247 525
79a45b7d 526 TYPE, VALADDR, ADDRESS, STREAM, RECURSE, and OPTIONS have the
373a8247
PM
527 same meanings as in pascal_object_print_value and c_val_print.
528
529 DONT_PRINT is an array of baseclass types that we
530 should not print, or zero if called from top level. */
531
532void
fc1a4b47 533pascal_object_print_value_fields (struct type *type, const gdb_byte *valaddr,
6b850546 534 LONGEST offset,
fba45db2 535 CORE_ADDR address, struct ui_file *stream,
79a45b7d 536 int recurse,
e8b24d9f 537 struct value *val,
79a45b7d 538 const struct value_print_options *options,
fba45db2
KB
539 struct type **dont_print_vb,
540 int dont_print_statmem)
373a8247
PM
541{
542 int i, len, n_baseclasses;
79f33898
SM
543 char *last_dont_print
544 = (char *) obstack_next_free (&dont_print_statmem_obstack);
373a8247 545
f168693b 546 type = check_typedef (type);
373a8247
PM
547
548 fprintf_filtered (stream, "{");
549 len = TYPE_NFIELDS (type);
550 n_baseclasses = TYPE_N_BASECLASSES (type);
551
552 /* Print out baseclasses such that we don't print
553 duplicates of virtual baseclasses. */
554 if (n_baseclasses > 0)
490f124f
PA
555 pascal_object_print_value (type, valaddr, offset, address,
556 stream, recurse + 1, val,
557 options, dont_print_vb);
373a8247
PM
558
559 if (!len && n_baseclasses == 1)
7f6aba03 560 fprintf_styled (stream, metadata_style.style (), "<No data fields>");
373a8247
PM
561 else
562 {
c1b6e682 563 struct obstack tmp_obstack = dont_print_statmem_obstack;
373a8247
PM
564 int fields_seen = 0;
565
566 if (dont_print_statmem == 0)
567 {
568 /* If we're at top level, carve out a completely fresh
569 chunk of the obstack and use that until this particular
570 invocation returns. */
373a8247
PM
571 obstack_finish (&dont_print_statmem_obstack);
572 }
573
574 for (i = n_baseclasses; i < len; i++)
575 {
576 /* If requested, skip printing of static fields. */
79a45b7d 577 if (!options->pascal_static_field_print
d6a843b5 578 && field_is_static (&TYPE_FIELD (type, i)))
373a8247
PM
579 continue;
580 if (fields_seen)
581 fprintf_filtered (stream, ", ");
582 else if (n_baseclasses > 0)
583 {
2a998fc0 584 if (options->prettyformat)
373a8247
PM
585 {
586 fprintf_filtered (stream, "\n");
587 print_spaces_filtered (2 + 2 * recurse, stream);
588 fputs_filtered ("members of ", stream);
a737d952 589 fputs_filtered (TYPE_NAME (type), stream);
373a8247
PM
590 fputs_filtered (": ", stream);
591 }
592 }
593 fields_seen = 1;
594
2a998fc0 595 if (options->prettyformat)
373a8247
PM
596 {
597 fprintf_filtered (stream, "\n");
598 print_spaces_filtered (2 + 2 * recurse, stream);
599 }
600 else
601 {
602 wrap_here (n_spaces (2 + 2 * recurse));
603 }
e93a8774
TT
604
605 annotate_field_begin (TYPE_FIELD_TYPE (type, i));
606
607 if (field_is_static (&TYPE_FIELD (type, i)))
3f0cbb04
TT
608 {
609 fputs_filtered ("static ", stream);
610 fprintf_symbol_filtered (stream,
611 TYPE_FIELD_NAME (type, i),
612 current_language->la_language,
613 DMGL_PARAMS | DMGL_ANSI);
614 }
615 else
616 fputs_styled (TYPE_FIELD_NAME (type, i),
617 variable_name_style.style (), stream);
e93a8774
TT
618 annotate_field_name_end ();
619 fputs_filtered (" = ", stream);
620 annotate_field_value ();
373a8247 621
d6a843b5
JK
622 if (!field_is_static (&TYPE_FIELD (type, i))
623 && TYPE_FIELD_PACKED (type, i))
373a8247 624 {
6943961c 625 struct value *v;
373a8247
PM
626
627 /* Bitfields require special handling, especially due to byte
628 order problems. */
629 if (TYPE_FIELD_IGNORE (type, i))
630 {
7f6aba03
TT
631 fputs_styled ("<optimized out or zero length>",
632 metadata_style.style (), stream);
373a8247 633 }
8cf6f0b1
TT
634 else if (value_bits_synthetic_pointer (val,
635 TYPE_FIELD_BITPOS (type,
636 i),
637 TYPE_FIELD_BITSIZE (type,
638 i)))
639 {
7f6aba03
TT
640 fputs_styled (_("<synthetic pointer>"),
641 metadata_style.style (), stream);
8cf6f0b1 642 }
373a8247
PM
643 else
644 {
79a45b7d 645 struct value_print_options opts = *options;
ad3bbd48 646
5467c6c8 647 v = value_field_bitfield (type, i, valaddr, offset, val);
373a8247 648
79a45b7d
TT
649 opts.deref_ref = 0;
650 common_val_print (v, stream, recurse + 1, &opts,
651 current_language);
373a8247
PM
652 }
653 }
654 else
655 {
656 if (TYPE_FIELD_IGNORE (type, i))
657 {
7f6aba03
TT
658 fputs_styled ("<optimized out or zero length>",
659 metadata_style.style (), stream);
373a8247 660 }
d6a843b5 661 else if (field_is_static (&TYPE_FIELD (type, i)))
373a8247 662 {
3e43a32a 663 /* struct value *v = value_static_field (type, i);
0df8b418 664 v4.17 specific. */
6943961c 665 struct value *v;
ad3bbd48 666
5467c6c8 667 v = value_field_bitfield (type, i, valaddr, offset, val);
373a8247
PM
668
669 if (v == NULL)
901461f8 670 val_print_optimized_out (NULL, stream);
373a8247 671 else
79a45b7d
TT
672 pascal_object_print_static_field (v, stream, recurse + 1,
673 options);
373a8247
PM
674 }
675 else
676 {
79a45b7d 677 struct value_print_options opts = *options;
ad3bbd48 678
79a45b7d 679 opts.deref_ref = 0;
373a8247
PM
680 /* val_print (TYPE_FIELD_TYPE (type, i),
681 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
682 address + TYPE_FIELD_BITPOS (type, i) / 8, 0,
683 stream, format, 0, recurse + 1, pretty); */
684 val_print (TYPE_FIELD_TYPE (type, i),
e8b24d9f 685 offset + TYPE_FIELD_BITPOS (type, i) / 8,
490f124f 686 address, stream, recurse + 1, val, &opts,
d8ca156b 687 current_language);
373a8247
PM
688 }
689 }
690 annotate_field_end ();
691 }
692
693 if (dont_print_statmem == 0)
694 {
695 /* Free the space used to deal with the printing
696 of the members from top level. */
697 obstack_free (&dont_print_statmem_obstack, last_dont_print);
698 dont_print_statmem_obstack = tmp_obstack;
699 }
700
2a998fc0 701 if (options->prettyformat)
373a8247
PM
702 {
703 fprintf_filtered (stream, "\n");
704 print_spaces_filtered (2 * recurse, stream);
705 }
706 }
707 fprintf_filtered (stream, "}");
708}
709
710/* Special val_print routine to avoid printing multiple copies of virtual
711 baseclasses. */
712
7080f20f 713static void
fc1a4b47 714pascal_object_print_value (struct type *type, const gdb_byte *valaddr,
6b850546 715 LONGEST offset,
a2bd3dcd 716 CORE_ADDR address, struct ui_file *stream,
79a45b7d 717 int recurse,
e8b24d9f 718 struct value *val,
79a45b7d 719 const struct value_print_options *options,
fba45db2 720 struct type **dont_print_vb)
373a8247 721{
373a8247 722 struct type **last_dont_print
ad3bbd48 723 = (struct type **) obstack_next_free (&dont_print_vb_obstack);
c1b6e682 724 struct obstack tmp_obstack = dont_print_vb_obstack;
373a8247
PM
725 int i, n_baseclasses = TYPE_N_BASECLASSES (type);
726
727 if (dont_print_vb == 0)
728 {
729 /* If we're at top level, carve out a completely fresh
730 chunk of the obstack and use that until this particular
731 invocation returns. */
373a8247
PM
732 /* Bump up the high-water mark. Now alpha is omega. */
733 obstack_finish (&dont_print_vb_obstack);
734 }
735
736 for (i = 0; i < n_baseclasses; i++)
737 {
6b850546 738 LONGEST boffset = 0;
373a8247 739 struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
a737d952 740 const char *basename = TYPE_NAME (baseclass);
8af8e3bc 741 const gdb_byte *base_valaddr = NULL;
6b850546 742 LONGEST thisoffset;
8af8e3bc 743 int skip = 0;
49663d05 744 gdb::byte_vector buf;
373a8247
PM
745
746 if (BASETYPE_VIA_VIRTUAL (type, i))
747 {
748 struct type **first_dont_print
ad3bbd48 749 = (struct type **) obstack_base (&dont_print_vb_obstack);
373a8247
PM
750
751 int j = (struct type **) obstack_next_free (&dont_print_vb_obstack)
ad3bbd48 752 - first_dont_print;
373a8247
PM
753
754 while (--j >= 0)
755 if (baseclass == first_dont_print[j])
756 goto flush_it;
757
758 obstack_ptr_grow (&dont_print_vb_obstack, baseclass);
759 }
760
490f124f
PA
761 thisoffset = offset;
762
a70b8144 763 try
8af8e3bc
PA
764 {
765 boffset = baseclass_offset (type, i, valaddr, offset, address, val);
766 }
230d2906 767 catch (const gdb_exception_error &ex)
8af8e3bc 768 {
7556d4a4
PA
769 if (ex.error == NOT_AVAILABLE_ERROR)
770 skip = -1;
771 else
772 skip = 1;
773 }
8af8e3bc 774
7556d4a4
PA
775 if (skip == 0)
776 {
8af8e3bc
PA
777 /* The virtual base class pointer might have been clobbered by the
778 user program. Make sure that it still points to a valid memory
779 location. */
780
781 if (boffset < 0 || boffset >= TYPE_LENGTH (type))
782 {
49663d05 783 buf.resize (TYPE_LENGTH (baseclass));
6c18f3e0 784
49663d05
TT
785 base_valaddr = buf.data ();
786 if (target_read_memory (address + boffset, buf.data (),
8af8e3bc
PA
787 TYPE_LENGTH (baseclass)) != 0)
788 skip = 1;
789 address = address + boffset;
790 thisoffset = 0;
791 boffset = 0;
792 }
793 else
794 base_valaddr = valaddr;
795 }
373a8247 796
2a998fc0 797 if (options->prettyformat)
373a8247
PM
798 {
799 fprintf_filtered (stream, "\n");
800 print_spaces_filtered (2 * recurse, stream);
801 }
802 fputs_filtered ("<", stream);
803 /* Not sure what the best notation is in the case where there is no
804 baseclass name. */
805
806 fputs_filtered (basename ? basename : "", stream);
807 fputs_filtered ("> = ", stream);
808
8af8e3bc
PA
809 if (skip < 0)
810 val_print_unavailable (stream);
811 else if (skip > 0)
812 val_print_invalid_address (stream);
373a8247 813 else
3e43a32a 814 pascal_object_print_value_fields (baseclass, base_valaddr,
490f124f
PA
815 thisoffset + boffset, address,
816 stream, recurse, val, options,
373a8247
PM
817 (struct type **) obstack_base (&dont_print_vb_obstack),
818 0);
819 fputs_filtered (", ", stream);
820
821 flush_it:
822 ;
823 }
824
825 if (dont_print_vb == 0)
826 {
827 /* Free the space used to deal with the printing
828 of this type from top level. */
829 obstack_free (&dont_print_vb_obstack, last_dont_print);
830 /* Reset watermark so that we can continue protecting
831 ourselves from whatever we were protecting ourselves. */
832 dont_print_vb_obstack = tmp_obstack;
833 }
834}
835
836/* Print value of a static member.
837 To avoid infinite recursion when printing a class that contains
838 a static instance of the class, we keep the addresses of all printed
839 static member classes in an obstack and refuse to print them more
840 than once.
841
79a45b7d 842 VAL contains the value to print, STREAM, RECURSE, and OPTIONS
373a8247
PM
843 have the same meanings as in c_val_print. */
844
845static void
806048c6 846pascal_object_print_static_field (struct value *val,
79a45b7d
TT
847 struct ui_file *stream,
848 int recurse,
849 const struct value_print_options *options)
373a8247 850{
806048c6 851 struct type *type = value_type (val);
79a45b7d 852 struct value_print_options opts;
806048c6 853
686d4def
PA
854 if (value_entirely_optimized_out (val))
855 {
856 val_print_optimized_out (val, stream);
857 return;
858 }
859
373a8247
PM
860 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
861 {
42ae5230 862 CORE_ADDR *first_dont_print, addr;
373a8247
PM
863 int i;
864
865 first_dont_print
866 = (CORE_ADDR *) obstack_base (&dont_print_statmem_obstack);
867 i = (CORE_ADDR *) obstack_next_free (&dont_print_statmem_obstack)
868 - first_dont_print;
869
870 while (--i >= 0)
871 {
42ae5230 872 if (value_address (val) == first_dont_print[i])
373a8247 873 {
2dbc041e
TT
874 fputs_styled (_("\
875<same as static member of an already seen type>"),
876 metadata_style.style (), stream);
373a8247
PM
877 return;
878 }
879 }
880
42ae5230
TT
881 addr = value_address (val);
882 obstack_grow (&dont_print_statmem_obstack, (char *) &addr,
373a8247
PM
883 sizeof (CORE_ADDR));
884
f168693b 885 type = check_typedef (type);
490f124f
PA
886 pascal_object_print_value_fields (type,
887 value_contents_for_printing (val),
888 value_embedded_offset (val),
889 addr,
890 stream, recurse,
891 val, options, NULL, 1);
373a8247
PM
892 return;
893 }
79a45b7d
TT
894
895 opts = *options;
896 opts.deref_ref = 0;
897 common_val_print (val, stream, recurse, &opts, current_language);
373a8247
PM
898}
899
6c265988 900void _initialize_pascal_valprint ();
373a8247 901void
6c265988 902_initialize_pascal_valprint ()
373a8247 903{
5bf193a2 904 add_setshow_boolean_cmd ("pascal_static-members", class_support,
79a45b7d 905 &user_print_options.pascal_static_field_print, _("\
5bf193a2
AC
906Set printing of pascal static members."), _("\
907Show printing of pascal static members."), NULL,
908 NULL,
920d2a44 909 show_pascal_static_field_print,
5bf193a2 910 &setprintlist, &showprintlist);
373a8247 911}
This page took 2.275142 seconds and 4 git commands to generate.