]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/f-valprint.c
Respect `set print repeats' with Fortran arrays
[thirdparty/binutils-gdb.git] / gdb / f-valprint.c
1 /* Support for printing Fortran values for GDB, the GNU debugger.
2
3 Copyright (C) 1993-2022 Free Software Foundation, Inc.
4
5 Contributed by Motorola. Adapted from the C definitions by Farooq Butt
6 (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 #include "defs.h"
24 #include "annotate.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "value.h"
29 #include "valprint.h"
30 #include "language.h"
31 #include "f-lang.h"
32 #include "frame.h"
33 #include "gdbcore.h"
34 #include "command.h"
35 #include "block.h"
36 #include "dictionary.h"
37 #include "cli/cli-style.h"
38 #include "gdbarch.h"
39 #include "f-array-walker.h"
40
41 static void f77_get_dynamic_length_of_aggregate (struct type *);
42
43 LONGEST
44 f77_get_lowerbound (struct type *type)
45 {
46 if (type->bounds ()->low.kind () == PROP_UNDEFINED)
47 error (_("Lower bound may not be '*' in F77"));
48
49 return type->bounds ()->low.const_val ();
50 }
51
52 LONGEST
53 f77_get_upperbound (struct type *type)
54 {
55 if (type->bounds ()->high.kind () == PROP_UNDEFINED)
56 {
57 /* We have an assumed size array on our hands. Assume that
58 upper_bound == lower_bound so that we show at least 1 element.
59 If the user wants to see more elements, let him manually ask for 'em
60 and we'll subscript the array and show him. */
61
62 return f77_get_lowerbound (type);
63 }
64
65 return type->bounds ()->high.const_val ();
66 }
67
68 /* Obtain F77 adjustable array dimensions. */
69
70 static void
71 f77_get_dynamic_length_of_aggregate (struct type *type)
72 {
73 int upper_bound = -1;
74 int lower_bound = 1;
75
76 /* Recursively go all the way down into a possibly multi-dimensional
77 F77 array and get the bounds. For simple arrays, this is pretty
78 easy but when the bounds are dynamic, we must be very careful
79 to add up all the lengths correctly. Not doing this right
80 will lead to horrendous-looking arrays in parameter lists.
81
82 This function also works for strings which behave very
83 similarly to arrays. */
84
85 if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY
86 || TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_STRING)
87 f77_get_dynamic_length_of_aggregate (TYPE_TARGET_TYPE (type));
88
89 /* Recursion ends here, start setting up lengths. */
90 lower_bound = f77_get_lowerbound (type);
91 upper_bound = f77_get_upperbound (type);
92
93 /* Patch in a valid length value. */
94
95 TYPE_LENGTH (type) =
96 (upper_bound - lower_bound + 1)
97 * TYPE_LENGTH (check_typedef (TYPE_TARGET_TYPE (type)));
98 }
99
100 /* Per-dimension statistics. */
101
102 struct dimension_stats
103 {
104 /* Total number of elements in the dimension, counted as we go. */
105 int nelts;
106 };
107
108 /* A class used by FORTRAN_PRINT_ARRAY as a specialisation of the array
109 walking template. This specialisation prints Fortran arrays. */
110
111 class fortran_array_printer_impl : public fortran_array_walker_base_impl
112 {
113 public:
114 /* Constructor. TYPE is the array type being printed, ADDRESS is the
115 address in target memory for the object of TYPE being printed. VAL is
116 the GDB value (of TYPE) being printed. STREAM is where to print to,
117 RECOURSE is passed through (and prevents infinite recursion), and
118 OPTIONS are the printing control options. */
119 explicit fortran_array_printer_impl (struct type *type,
120 CORE_ADDR address,
121 struct value *val,
122 struct ui_file *stream,
123 int recurse,
124 const struct value_print_options *options)
125 : m_elts (0),
126 m_val (val),
127 m_stream (stream),
128 m_recurse (recurse),
129 m_options (options),
130 m_dimension (0),
131 m_nrepeats (0),
132 m_stats (0)
133 { /* Nothing. */ }
134
135 /* Called while iterating over the array bounds. When SHOULD_CONTINUE is
136 false then we must return false, as we have reached the end of the
137 array bounds for this dimension. However, we also return false if we
138 have printed too many elements (after printing '...'). In all other
139 cases, return true. */
140 bool continue_walking (bool should_continue)
141 {
142 bool cont = should_continue && (m_elts < m_options->print_max);
143 if (!cont && should_continue)
144 fputs_filtered ("...", m_stream);
145 return cont;
146 }
147
148 /* Called when we start iterating over a dimension. If it's not the
149 inner most dimension then print an opening '(' character. */
150 void start_dimension (LONGEST nelts, bool inner_p)
151 {
152 size_t dim_indx = m_dimension++;
153
154 m_elt_type_prev = nullptr;
155 if (m_stats.size () < m_dimension)
156 {
157 m_stats.resize (m_dimension);
158 m_stats[dim_indx].nelts = nelts;
159 }
160
161 fputs_filtered ("(", m_stream);
162 }
163
164 /* Called when we finish processing a batch of items within a dimension
165 of the array. Depending on whether this is the inner most dimension
166 or not we print different things, but this is all about adding
167 separators between elements, and dimensions of the array. */
168 void finish_dimension (bool inner_p, bool last_p)
169 {
170 fputs_filtered (")", m_stream);
171 if (!last_p)
172 fputs_filtered (" ", m_stream);
173
174 m_dimension--;
175 }
176
177 /* Called when processing dimensions of the array other than the
178 innermost one. WALK_1 is the walker to normally call, ELT_TYPE is
179 the type of the element being extracted, and ELT_OFF is the offset
180 of the element from the start of array being walked, and LAST_P is
181 true only when this is the last element that will be processed in
182 this dimension. */
183 void process_dimension (gdb::function_view<void (struct type *,
184 int, bool)> walk_1,
185 struct type *elt_type, LONGEST elt_off, bool last_p)
186 {
187 size_t dim_indx = m_dimension - 1;
188 struct type *elt_type_prev = m_elt_type_prev;
189 LONGEST elt_off_prev = m_elt_off_prev;
190 bool repeated = (m_options->repeat_count_threshold < UINT_MAX
191 && elt_type_prev != nullptr
192 && (m_elts + ((m_nrepeats + 1)
193 * m_stats[dim_indx + 1].nelts)
194 <= m_options->print_max)
195 && dimension_contents_eq (m_val, elt_type,
196 elt_off_prev, elt_off));
197
198 if (repeated)
199 m_nrepeats++;
200 if (!repeated || last_p)
201 {
202 LONGEST nrepeats = m_nrepeats;
203
204 m_nrepeats = 0;
205 if (nrepeats >= m_options->repeat_count_threshold)
206 {
207 annotate_elt_rep (nrepeats + 1);
208 fprintf_filtered (m_stream, "%p[<repeats %s times>%p]",
209 metadata_style.style ().ptr (),
210 plongest (nrepeats + 1),
211 nullptr);
212 annotate_elt_rep_end ();
213 if (!repeated)
214 fputs_filtered (" ", m_stream);
215 m_elts += nrepeats * m_stats[dim_indx + 1].nelts;
216 }
217 else
218 for (LONGEST i = nrepeats; i > 0; i--)
219 walk_1 (elt_type_prev, elt_off_prev, repeated && i == 1);
220
221 if (!repeated)
222 {
223 /* We need to specially handle the case of hitting `print_max'
224 exactly as recursing would cause lone `(...)' to be printed.
225 And we need to print `...' by hand if the skipped element
226 would be the last one processed, because the subsequent call
227 to `continue_walking' from our caller won't do that. */
228 if (m_elts < m_options->print_max)
229 {
230 walk_1 (elt_type, elt_off, last_p);
231 nrepeats++;
232 }
233 else if (last_p)
234 fputs_filtered ("...", m_stream);
235 }
236 }
237
238 m_elt_type_prev = elt_type;
239 m_elt_off_prev = elt_off;
240 }
241
242 /* Called to process an element of ELT_TYPE at offset ELT_OFF from the
243 start of the parent object. */
244 void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
245 {
246 struct type *elt_type_prev = m_elt_type_prev;
247 LONGEST elt_off_prev = m_elt_off_prev;
248 bool repeated = (m_options->repeat_count_threshold < UINT_MAX
249 && elt_type_prev != nullptr
250 && value_contents_eq (m_val, elt_off_prev, m_val, elt_off,
251 TYPE_LENGTH (elt_type)));
252
253 if (repeated)
254 m_nrepeats++;
255 if (!repeated || last_p || m_elts + 1 == m_options->print_max)
256 {
257 LONGEST nrepeats = m_nrepeats;
258 bool printed = false;
259
260 if (nrepeats != 0)
261 {
262 m_nrepeats = 0;
263 if (nrepeats >= m_options->repeat_count_threshold)
264 {
265 annotate_elt_rep (nrepeats + 1);
266 fprintf_filtered (m_stream, "%p[<repeats %s times>%p]",
267 metadata_style.style ().ptr (),
268 plongest (nrepeats + 1),
269 nullptr);
270 annotate_elt_rep_end ();
271 }
272 else
273 {
274 /* Extract the element value from the parent value. */
275 struct value *e_val
276 = value_from_component (m_val, elt_type, elt_off_prev);
277
278 for (LONGEST i = nrepeats; i > 0; i--)
279 {
280 common_val_print (e_val, m_stream, m_recurse, m_options,
281 current_language);
282 if (i > 1)
283 fputs_filtered (", ", m_stream);
284 }
285 }
286 printed = true;
287 }
288
289 if (!repeated)
290 {
291 /* Extract the element value from the parent value. */
292 struct value *e_val
293 = value_from_component (m_val, elt_type, elt_off);
294
295 if (printed)
296 fputs_filtered (", ", m_stream);
297 common_val_print (e_val, m_stream, m_recurse, m_options,
298 current_language);
299 }
300 if (!last_p)
301 fputs_filtered (", ", m_stream);
302 }
303
304 m_elt_type_prev = elt_type;
305 m_elt_off_prev = elt_off;
306 ++m_elts;
307 }
308
309 private:
310 /* Called to compare two VAL elements of ELT_TYPE at offsets OFFSET1
311 and OFFSET2 each. Handle subarrays recursively, because they may
312 have been sliced and we do not want to compare any memory contents
313 present between the slices requested. */
314 bool
315 dimension_contents_eq (const struct value *val, struct type *type,
316 LONGEST offset1, LONGEST offset2)
317 {
318 if (type->code () == TYPE_CODE_ARRAY
319 && TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_CHAR)
320 {
321 /* Extract the range, and get lower and upper bounds. */
322 struct type *range_type = check_typedef (type)->index_type ();
323 LONGEST lowerbound, upperbound;
324 if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
325 error ("failed to get range bounds");
326
327 /* CALC is used to calculate the offsets for each element. */
328 fortran_array_offset_calculator calc (type);
329
330 struct type *subarray_type = check_typedef (TYPE_TARGET_TYPE (type));
331 for (LONGEST i = lowerbound; i < upperbound + 1; i++)
332 {
333 /* Use the index and the stride to work out a new offset. */
334 LONGEST index_offset = calc.index_offset (i);
335
336 if (!dimension_contents_eq (val, subarray_type,
337 offset1 + index_offset,
338 offset2 + index_offset))
339 return false;
340 }
341 return true;
342 }
343 else
344 return value_contents_eq (val, offset1, val, offset2,
345 TYPE_LENGTH (type));
346 }
347
348 /* The number of elements printed so far. */
349 int m_elts;
350
351 /* The value from which we are printing elements. */
352 struct value *m_val;
353
354 /* The stream we should print too. */
355 struct ui_file *m_stream;
356
357 /* The recursion counter, passed through when we print each element. */
358 int m_recurse;
359
360 /* The print control options. Gives us the maximum number of elements to
361 print, and is passed through to each element that we print. */
362 const struct value_print_options *m_options = nullptr;
363
364 /* The number of the current dimension being handled. */
365 LONGEST m_dimension;
366
367 /* The number of element repetitions in the current series. */
368 LONGEST m_nrepeats;
369
370 /* The type and offset from M_VAL of the element handled in the previous
371 iteration over the current dimension. */
372 struct type *m_elt_type_prev;
373 LONGEST m_elt_off_prev;
374
375 /* Per-dimension stats. */
376 std::vector<struct dimension_stats> m_stats;
377 };
378
379 /* This function gets called to print a Fortran array. */
380
381 static void
382 fortran_print_array (struct type *type, CORE_ADDR address,
383 struct ui_file *stream, int recurse,
384 const struct value *val,
385 const struct value_print_options *options)
386 {
387 fortran_array_walker<fortran_array_printer_impl> p
388 (type, address, (struct value *) val, stream, recurse, options);
389 p.walk ();
390 }
391 \f
392
393 /* Decorations for Fortran. */
394
395 static const struct generic_val_print_decorations f_decorations =
396 {
397 "(",
398 ",",
399 ")",
400 ".TRUE.",
401 ".FALSE.",
402 "void",
403 "{",
404 "}"
405 };
406
407 /* See f-lang.h. */
408
409 void
410 f_language::value_print_inner (struct value *val, struct ui_file *stream,
411 int recurse,
412 const struct value_print_options *options) const
413 {
414 struct type *type = check_typedef (value_type (val));
415 struct gdbarch *gdbarch = type->arch ();
416 int printed_field = 0; /* Number of fields printed. */
417 struct type *elttype;
418 CORE_ADDR addr;
419 int index;
420 const gdb_byte *valaddr = value_contents_for_printing (val).data ();
421 const CORE_ADDR address = value_address (val);
422
423 switch (type->code ())
424 {
425 case TYPE_CODE_STRING:
426 f77_get_dynamic_length_of_aggregate (type);
427 printstr (stream, builtin_type (gdbarch)->builtin_char, valaddr,
428 TYPE_LENGTH (type), NULL, 0, options);
429 break;
430
431 case TYPE_CODE_ARRAY:
432 if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_CHAR)
433 fortran_print_array (type, address, stream, recurse, val, options);
434 else
435 {
436 struct type *ch_type = TYPE_TARGET_TYPE (type);
437
438 f77_get_dynamic_length_of_aggregate (type);
439 printstr (stream, ch_type, valaddr,
440 TYPE_LENGTH (type) / TYPE_LENGTH (ch_type), NULL, 0,
441 options);
442 }
443 break;
444
445 case TYPE_CODE_PTR:
446 if (options->format && options->format != 's')
447 {
448 value_print_scalar_formatted (val, options, 0, stream);
449 break;
450 }
451 else
452 {
453 int want_space = 0;
454
455 addr = unpack_pointer (type, valaddr);
456 elttype = check_typedef (TYPE_TARGET_TYPE (type));
457
458 if (elttype->code () == TYPE_CODE_FUNC)
459 {
460 /* Try to print what function it points to. */
461 print_function_pointer_address (options, gdbarch, addr, stream);
462 return;
463 }
464
465 if (options->symbol_print)
466 want_space = print_address_demangle (options, gdbarch, addr,
467 stream, demangle);
468 else if (options->addressprint && options->format != 's')
469 {
470 fputs_filtered (paddress (gdbarch, addr), stream);
471 want_space = 1;
472 }
473
474 /* For a pointer to char or unsigned char, also print the string
475 pointed to, unless pointer is null. */
476 if (TYPE_LENGTH (elttype) == 1
477 && elttype->code () == TYPE_CODE_INT
478 && (options->format == 0 || options->format == 's')
479 && addr != 0)
480 {
481 if (want_space)
482 fputs_filtered (" ", stream);
483 val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
484 stream, options);
485 }
486 return;
487 }
488 break;
489
490 case TYPE_CODE_STRUCT:
491 case TYPE_CODE_UNION:
492 /* Starting from the Fortran 90 standard, Fortran supports derived
493 types. */
494 fprintf_filtered (stream, "( ");
495 for (index = 0; index < type->num_fields (); index++)
496 {
497 struct value *field = value_field (val, index);
498
499 struct type *field_type = check_typedef (type->field (index).type ());
500
501
502 if (field_type->code () != TYPE_CODE_FUNC)
503 {
504 const char *field_name;
505
506 if (printed_field > 0)
507 fputs_filtered (", ", stream);
508
509 field_name = type->field (index).name ();
510 if (field_name != NULL)
511 {
512 fputs_styled (field_name, variable_name_style.style (),
513 stream);
514 fputs_filtered (" = ", stream);
515 }
516
517 common_val_print (field, stream, recurse + 1,
518 options, current_language);
519
520 ++printed_field;
521 }
522 }
523 fprintf_filtered (stream, " )");
524 break;
525
526 case TYPE_CODE_BOOL:
527 if (options->format || options->output_format)
528 {
529 struct value_print_options opts = *options;
530 opts.format = (options->format ? options->format
531 : options->output_format);
532 value_print_scalar_formatted (val, &opts, 0, stream);
533 }
534 else
535 {
536 LONGEST longval = value_as_long (val);
537 /* The Fortran standard doesn't specify how logical types are
538 represented. Different compilers use different non zero
539 values to represent logical true. */
540 if (longval == 0)
541 fputs_filtered (f_decorations.false_name, stream);
542 else
543 fputs_filtered (f_decorations.true_name, stream);
544 }
545 break;
546
547 case TYPE_CODE_INT:
548 case TYPE_CODE_REF:
549 case TYPE_CODE_FUNC:
550 case TYPE_CODE_FLAGS:
551 case TYPE_CODE_FLT:
552 case TYPE_CODE_VOID:
553 case TYPE_CODE_ERROR:
554 case TYPE_CODE_RANGE:
555 case TYPE_CODE_UNDEF:
556 case TYPE_CODE_COMPLEX:
557 case TYPE_CODE_CHAR:
558 default:
559 generic_value_print (val, stream, recurse, options, &f_decorations);
560 break;
561 }
562 }
563
564 static void
565 info_common_command_for_block (const struct block *block, const char *comname,
566 int *any_printed)
567 {
568 struct block_iterator iter;
569 struct symbol *sym;
570 struct value_print_options opts;
571
572 get_user_print_options (&opts);
573
574 ALL_BLOCK_SYMBOLS (block, iter, sym)
575 if (SYMBOL_DOMAIN (sym) == COMMON_BLOCK_DOMAIN)
576 {
577 const struct common_block *common = SYMBOL_VALUE_COMMON_BLOCK (sym);
578 size_t index;
579
580 gdb_assert (SYMBOL_CLASS (sym) == LOC_COMMON_BLOCK);
581
582 if (comname && (!sym->linkage_name ()
583 || strcmp (comname, sym->linkage_name ()) != 0))
584 continue;
585
586 if (*any_printed)
587 putchar_filtered ('\n');
588 else
589 *any_printed = 1;
590 if (sym->print_name ())
591 printf_filtered (_("Contents of F77 COMMON block '%s':\n"),
592 sym->print_name ());
593 else
594 printf_filtered (_("Contents of blank COMMON block:\n"));
595
596 for (index = 0; index < common->n_entries; index++)
597 {
598 struct value *val = NULL;
599
600 printf_filtered ("%s = ",
601 common->contents[index]->print_name ());
602
603 try
604 {
605 val = value_of_variable (common->contents[index], block);
606 value_print (val, gdb_stdout, &opts);
607 }
608
609 catch (const gdb_exception_error &except)
610 {
611 fprintf_styled (gdb_stdout, metadata_style.style (),
612 "<error reading variable: %s>",
613 except.what ());
614 }
615
616 putchar_filtered ('\n');
617 }
618 }
619 }
620
621 /* This function is used to print out the values in a given COMMON
622 block. It will always use the most local common block of the
623 given name. */
624
625 static void
626 info_common_command (const char *comname, int from_tty)
627 {
628 struct frame_info *fi;
629 const struct block *block;
630 int values_printed = 0;
631
632 /* We have been told to display the contents of F77 COMMON
633 block supposedly visible in this function. Let us
634 first make sure that it is visible and if so, let
635 us display its contents. */
636
637 fi = get_selected_frame (_("No frame selected"));
638
639 /* The following is generally ripped off from stack.c's routine
640 print_frame_info(). */
641
642 block = get_frame_block (fi, 0);
643 if (block == NULL)
644 {
645 printf_filtered (_("No symbol table info available.\n"));
646 return;
647 }
648
649 while (block)
650 {
651 info_common_command_for_block (block, comname, &values_printed);
652 /* After handling the function's top-level block, stop. Don't
653 continue to its superblock, the block of per-file symbols. */
654 if (BLOCK_FUNCTION (block))
655 break;
656 block = BLOCK_SUPERBLOCK (block);
657 }
658
659 if (!values_printed)
660 {
661 if (comname)
662 printf_filtered (_("No common block '%s'.\n"), comname);
663 else
664 printf_filtered (_("No common blocks.\n"));
665 }
666 }
667
668 void _initialize_f_valprint ();
669 void
670 _initialize_f_valprint ()
671 {
672 add_info ("common", info_common_command,
673 _("Print out the values contained in a Fortran COMMON block."));
674 }