1 /* Support for printing Fortran values for GDB, the GNU debugger.
3 Copyright (C) 1993-2024 Free Software Foundation, Inc.
5 Contributed by Motorola. Adapted from the C definitions by Farooq Butt
6 (fmbutt@engage.sps.mot.com), additionally worked over by Stan Shebs.
8 This file is part of GDB.
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.
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.
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/>. */
26 #include "expression.h"
35 #include "dictionary.h"
36 #include "cli/cli-style.h"
38 #include "f-array-walker.h"
40 static void f77_get_dynamic_length_of_aggregate (struct type
*);
43 f77_get_lowerbound (struct type
*type
)
45 if (!type
->bounds ()->low
.is_constant ())
46 error (_("Lower bound may not be '*' in F77"));
48 return type
->bounds ()->low
.const_val ();
52 f77_get_upperbound (struct type
*type
)
54 if (!type
->bounds ()->high
.is_constant ())
56 /* We have an assumed size array on our hands. Assume that
57 upper_bound == lower_bound so that we show at least 1 element.
58 If the user wants to see more elements, let him manually ask for 'em
59 and we'll subscript the array and show him. */
61 return f77_get_lowerbound (type
);
64 return type
->bounds ()->high
.const_val ();
67 /* Obtain F77 adjustable array dimensions. */
70 f77_get_dynamic_length_of_aggregate (struct type
*type
)
75 /* Recursively go all the way down into a possibly multi-dimensional
76 F77 array and get the bounds. For simple arrays, this is pretty
77 easy but when the bounds are dynamic, we must be very careful
78 to add up all the lengths correctly. Not doing this right
79 will lead to horrendous-looking arrays in parameter lists.
81 This function also works for strings which behave very
82 similarly to arrays. */
84 if (type
->target_type ()->code () == TYPE_CODE_ARRAY
85 || type
->target_type ()->code () == TYPE_CODE_STRING
)
86 f77_get_dynamic_length_of_aggregate (type
->target_type ());
88 /* Recursion ends here, start setting up lengths. */
89 lower_bound
= f77_get_lowerbound (type
);
90 upper_bound
= f77_get_upperbound (type
);
92 /* Patch in a valid length value. */
93 type
->set_length ((upper_bound
- lower_bound
+ 1)
94 * check_typedef (type
->target_type ())->length ());
97 /* Per-dimension statistics. */
99 struct dimension_stats
101 /* The type of the index used to address elements in the dimension. */
102 struct type
*index_type
;
104 /* Total number of elements in the dimension, counted as we go. */
108 /* A class used by FORTRAN_PRINT_ARRAY as a specialisation of the array
109 walking template. This specialisation prints Fortran arrays. */
111 class fortran_array_printer_impl
: public fortran_array_walker_base_impl
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
,
122 struct ui_file
*stream
,
124 const struct value_print_options
*options
)
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
)
142 bool cont
= should_continue
&& (m_elts
< m_options
->print_max
);
143 if (!cont
&& should_continue
)
144 gdb_puts ("...", m_stream
);
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 (struct type
*index_type
, LONGEST nelts
, bool inner_p
)
152 size_t dim_indx
= m_dimension
++;
154 m_elt_type_prev
= nullptr;
155 if (m_stats
.size () < m_dimension
)
157 m_stats
.resize (m_dimension
);
158 m_stats
[dim_indx
].index_type
= index_type
;
159 m_stats
[dim_indx
].nelts
= nelts
;
162 gdb_puts ("(", m_stream
);
165 /* Called when we finish processing a batch of items within a dimension
166 of the array. Depending on whether this is the inner most dimension
167 or not we print different things, but this is all about adding
168 separators between elements, and dimensions of the array. */
169 void finish_dimension (bool inner_p
, bool last_p
)
171 gdb_puts (")", m_stream
);
173 gdb_puts (" ", m_stream
);
178 /* Called when processing dimensions of the array other than the
179 innermost one. WALK_1 is the walker to normally call, ELT_TYPE is
180 the type of the element being extracted, and ELT_OFF is the offset
181 of the element from the start of array being walked, INDEX_TYPE
182 and INDEX is the type and the value respectively of the element's
183 index in the dimension currently being walked and LAST_P is true
184 only when this is the last element that will be processed in this
186 void process_dimension (gdb::function_view
<void (struct type
*,
188 struct type
*elt_type
, LONGEST elt_off
,
189 LONGEST index
, bool last_p
)
191 size_t dim_indx
= m_dimension
- 1;
192 struct type
*elt_type_prev
= m_elt_type_prev
;
193 LONGEST elt_off_prev
= m_elt_off_prev
;
194 bool repeated
= (m_options
->repeat_count_threshold
< UINT_MAX
195 && elt_type_prev
!= nullptr
196 && (m_elts
+ ((m_nrepeats
+ 1)
197 * m_stats
[dim_indx
+ 1].nelts
)
198 <= m_options
->print_max
)
199 && dimension_contents_eq (m_val
, elt_type
,
200 elt_off_prev
, elt_off
));
204 if (!repeated
|| last_p
)
206 LONGEST nrepeats
= m_nrepeats
;
209 if (nrepeats
>= m_options
->repeat_count_threshold
)
211 annotate_elt_rep (nrepeats
+ 1);
212 gdb_printf (m_stream
, "%p[<repeats %s times>%p]",
213 metadata_style
.style ().ptr (),
214 plongest (nrepeats
+ 1),
216 annotate_elt_rep_end ();
218 gdb_puts (" ", m_stream
);
219 m_elts
+= nrepeats
* m_stats
[dim_indx
+ 1].nelts
;
222 for (LONGEST i
= nrepeats
; i
> 0; i
--)
224 maybe_print_array_index (m_stats
[dim_indx
].index_type
,
225 index
- nrepeats
+ repeated
,
226 m_stream
, m_options
);
227 walk_1 (elt_type_prev
, elt_off_prev
, repeated
&& i
== 1);
232 /* We need to specially handle the case of hitting `print_max'
233 exactly as recursing would cause lone `(...)' to be printed.
234 And we need to print `...' by hand if the skipped element
235 would be the last one processed, because the subsequent call
236 to `continue_walking' from our caller won't do that. */
237 if (m_elts
< m_options
->print_max
)
239 maybe_print_array_index (m_stats
[dim_indx
].index_type
, index
,
240 m_stream
, m_options
);
241 walk_1 (elt_type
, elt_off
, last_p
);
245 gdb_puts ("...", m_stream
);
249 m_elt_type_prev
= elt_type
;
250 m_elt_off_prev
= elt_off
;
253 /* Called to process an element of ELT_TYPE at offset ELT_OFF from the
254 start of the parent object, where INDEX is the value of the element's
255 index in the dimension currently being walked and LAST_P is true only
256 when this is the last element to be processed in this dimension. */
257 void process_element (struct type
*elt_type
, LONGEST elt_off
,
258 LONGEST index
, bool last_p
)
260 size_t dim_indx
= m_dimension
- 1;
261 struct type
*elt_type_prev
= m_elt_type_prev
;
262 LONGEST elt_off_prev
= m_elt_off_prev
;
263 bool repeated
= false;
265 if (m_options
->repeat_count_threshold
< UINT_MAX
266 && elt_type_prev
!= nullptr)
268 /* When printing large arrays this spot is called frequently, so clean
269 up temporary values asap to prevent allocating a large amount of
271 scoped_value_mark free_values
;
272 struct value
*e_val
= value_from_component (m_val
, elt_type
, elt_off
);
273 struct value
*e_prev
= value_from_component (m_val
, elt_type
,
275 repeated
= ((e_prev
->entirely_available ()
276 && e_val
->entirely_available ()
277 && e_prev
->contents_eq (e_val
))
278 || (e_prev
->entirely_unavailable ()
279 && e_val
->entirely_unavailable ()));
284 if (!repeated
|| last_p
|| m_elts
+ 1 == m_options
->print_max
)
286 LONGEST nrepeats
= m_nrepeats
;
287 bool printed
= false;
292 if (nrepeats
>= m_options
->repeat_count_threshold
)
294 annotate_elt_rep (nrepeats
+ 1);
295 gdb_printf (m_stream
, "%p[<repeats %s times>%p]",
296 metadata_style
.style ().ptr (),
297 plongest (nrepeats
+ 1),
299 annotate_elt_rep_end ();
303 /* Extract the element value from the parent value. */
305 = value_from_component (m_val
, elt_type
, elt_off_prev
);
307 for (LONGEST i
= nrepeats
; i
> 0; i
--)
309 maybe_print_array_index (m_stats
[dim_indx
].index_type
,
311 m_stream
, m_options
);
312 common_val_print (e_val
, m_stream
, m_recurse
, m_options
,
315 gdb_puts (", ", m_stream
);
323 /* Extract the element value from the parent value. */
325 = value_from_component (m_val
, elt_type
, elt_off
);
328 gdb_puts (", ", m_stream
);
329 maybe_print_array_index (m_stats
[dim_indx
].index_type
, index
,
330 m_stream
, m_options
);
331 common_val_print (e_val
, m_stream
, m_recurse
, m_options
,
335 gdb_puts (", ", m_stream
);
338 m_elt_type_prev
= elt_type
;
339 m_elt_off_prev
= elt_off
;
344 /* Called to compare two VAL elements of ELT_TYPE at offsets OFFSET1
345 and OFFSET2 each. Handle subarrays recursively, because they may
346 have been sliced and we do not want to compare any memory contents
347 present between the slices requested. */
349 dimension_contents_eq (struct value
*val
, struct type
*type
,
350 LONGEST offset1
, LONGEST offset2
)
352 if (type
->code () == TYPE_CODE_ARRAY
353 && type
->target_type ()->code () != TYPE_CODE_CHAR
)
355 /* Extract the range, and get lower and upper bounds. */
356 struct type
*range_type
= check_typedef (type
)->index_type ();
357 LONGEST lowerbound
, upperbound
;
358 if (!get_discrete_bounds (range_type
, &lowerbound
, &upperbound
))
359 error ("failed to get range bounds");
361 /* CALC is used to calculate the offsets for each element. */
362 fortran_array_offset_calculator
calc (type
);
364 struct type
*subarray_type
= check_typedef (type
->target_type ());
365 for (LONGEST i
= lowerbound
; i
< upperbound
+ 1; i
++)
367 /* Use the index and the stride to work out a new offset. */
368 LONGEST index_offset
= calc
.index_offset (i
);
370 if (!dimension_contents_eq (val
, subarray_type
,
371 offset1
+ index_offset
,
372 offset2
+ index_offset
))
379 struct value
*e_val1
= value_from_component (val
, type
, offset1
);
380 struct value
*e_val2
= value_from_component (val
, type
, offset2
);
382 return ((e_val1
->entirely_available ()
383 && e_val2
->entirely_available ()
384 && e_val1
->contents_eq (e_val2
))
385 || (e_val1
->entirely_unavailable ()
386 && e_val2
->entirely_unavailable ()));
390 /* The number of elements printed so far. */
393 /* The value from which we are printing elements. */
396 /* The stream we should print too. */
397 struct ui_file
*m_stream
;
399 /* The recursion counter, passed through when we print each element. */
402 /* The print control options. Gives us the maximum number of elements to
403 print, and is passed through to each element that we print. */
404 const struct value_print_options
*m_options
= nullptr;
406 /* The number of the current dimension being handled. */
409 /* The number of element repetitions in the current series. */
412 /* The type and offset from M_VAL of the element handled in the previous
413 iteration over the current dimension. */
414 struct type
*m_elt_type_prev
;
415 LONGEST m_elt_off_prev
;
417 /* Per-dimension stats. */
418 std::vector
<struct dimension_stats
> m_stats
;
421 /* This function gets called to print a Fortran array. */
424 fortran_print_array (struct type
*type
, CORE_ADDR address
,
425 struct ui_file
*stream
, int recurse
,
426 const struct value
*val
,
427 const struct value_print_options
*options
)
429 fortran_array_walker
<fortran_array_printer_impl
> p
430 (type
, address
, (struct value
*) val
, stream
, recurse
, options
);
435 /* Decorations for Fortran. */
437 static const struct generic_val_print_decorations f_decorations
=
452 f_language::value_print_inner (struct value
*val
, struct ui_file
*stream
,
454 const struct value_print_options
*options
) const
456 struct type
*type
= check_typedef (val
->type ());
457 struct gdbarch
*gdbarch
= type
->arch ();
458 int printed_field
= 0; /* Number of fields printed. */
459 struct type
*elttype
;
462 const gdb_byte
*valaddr
= val
->contents_for_printing ().data ();
463 const CORE_ADDR address
= val
->address ();
465 switch (type
->code ())
467 case TYPE_CODE_STRING
:
468 f77_get_dynamic_length_of_aggregate (type
);
469 printstr (stream
, builtin_type (gdbarch
)->builtin_char
, valaddr
,
470 type
->length (), NULL
, 0, options
);
473 case TYPE_CODE_ARRAY
:
474 if (type
->target_type ()->code () != TYPE_CODE_CHAR
)
475 fortran_print_array (type
, address
, stream
, recurse
, val
, options
);
478 struct type
*ch_type
= type
->target_type ();
480 f77_get_dynamic_length_of_aggregate (type
);
481 printstr (stream
, ch_type
, valaddr
,
482 type
->length () / ch_type
->length (), NULL
, 0,
488 if (options
->format
&& options
->format
!= 's')
490 value_print_scalar_formatted (val
, options
, 0, stream
);
497 addr
= unpack_pointer (type
, valaddr
);
498 elttype
= check_typedef (type
->target_type ());
500 if (elttype
->code () == TYPE_CODE_FUNC
)
502 /* Try to print what function it points to. */
503 print_function_pointer_address (options
, gdbarch
, addr
, stream
);
507 if (options
->symbol_print
)
508 want_space
= print_address_demangle (options
, gdbarch
, addr
,
510 else if (options
->addressprint
&& options
->format
!= 's')
512 gdb_puts (paddress (gdbarch
, addr
), stream
);
516 /* For a pointer to char or unsigned char, also print the string
517 pointed to, unless pointer is null. */
518 if (elttype
->length () == 1
519 && elttype
->code () == TYPE_CODE_INT
520 && (options
->format
== 0 || options
->format
== 's')
524 gdb_puts (" ", stream
);
525 val_print_string (type
->target_type (), NULL
, addr
, -1,
532 case TYPE_CODE_STRUCT
:
533 case TYPE_CODE_UNION
:
534 case TYPE_CODE_NAMELIST
:
535 /* Starting from the Fortran 90 standard, Fortran supports derived
537 gdb_printf (stream
, "( ");
538 for (index
= 0; index
< type
->num_fields (); index
++)
540 struct type
*field_type
541 = check_typedef (type
->field (index
).type ());
543 if (field_type
->code () != TYPE_CODE_FUNC
)
545 const char *field_name
= type
->field (index
).name ();
548 if (type
->code () == TYPE_CODE_NAMELIST
)
550 /* While printing namelist items, fetch the appropriate
551 value field before printing its value. */
552 struct block_symbol sym
553 = lookup_symbol (field_name
, get_selected_block (nullptr),
554 SEARCH_VFT
, nullptr);
555 if (sym
.symbol
== nullptr)
556 error (_("failed to find symbol for name list component %s"),
558 field
= value_of_variable (sym
.symbol
, sym
.block
);
561 field
= value_field (val
, index
);
563 if (printed_field
> 0)
564 gdb_puts (", ", stream
);
566 if (field_name
!= NULL
)
568 fputs_styled (field_name
, variable_name_style
.style (),
570 gdb_puts (" = ", stream
);
573 common_val_print (field
, stream
, recurse
+ 1,
574 options
, current_language
);
579 gdb_printf (stream
, " )");
583 if (options
->format
|| options
->output_format
)
585 struct value_print_options opts
= *options
;
586 opts
.format
= (options
->format
? options
->format
587 : options
->output_format
);
588 value_print_scalar_formatted (val
, &opts
, 0, stream
);
592 LONGEST longval
= value_as_long (val
);
593 /* The Fortran standard doesn't specify how logical types are
594 represented. Different compilers use different non zero
595 values to represent logical true. */
597 gdb_puts (f_decorations
.false_name
, stream
);
599 gdb_puts (f_decorations
.true_name
, stream
);
606 case TYPE_CODE_FLAGS
:
609 case TYPE_CODE_ERROR
:
610 case TYPE_CODE_RANGE
:
611 case TYPE_CODE_UNDEF
:
612 case TYPE_CODE_COMPLEX
:
615 generic_value_print (val
, stream
, recurse
, options
, &f_decorations
);
621 info_common_command_for_block (const struct block
*block
, const char *comname
,
624 struct value_print_options opts
;
626 get_user_print_options (&opts
);
628 for (struct symbol
*sym
: block_iterator_range (block
))
629 if (sym
->domain () == COMMON_BLOCK_DOMAIN
)
631 const struct common_block
*common
= sym
->value_common_block ();
634 gdb_assert (sym
->aclass () == LOC_COMMON_BLOCK
);
636 if (comname
&& (!sym
->linkage_name ()
637 || strcmp (comname
, sym
->linkage_name ()) != 0))
644 if (sym
->print_name ())
645 gdb_printf (_("Contents of F77 COMMON block '%s':\n"),
648 gdb_printf (_("Contents of blank COMMON block:\n"));
650 for (index
= 0; index
< common
->n_entries
; index
++)
652 struct value
*val
= NULL
;
655 common
->contents
[index
]->print_name ());
659 val
= value_of_variable (common
->contents
[index
], block
);
660 value_print (val
, gdb_stdout
, &opts
);
663 catch (const gdb_exception_error
&except
)
665 fprintf_styled (gdb_stdout
, metadata_style
.style (),
666 "<error reading variable: %s>",
675 /* This function is used to print out the values in a given COMMON
676 block. It will always use the most local common block of the
680 info_common_command (const char *comname
, int from_tty
)
683 const struct block
*block
;
684 int values_printed
= 0;
686 /* We have been told to display the contents of F77 COMMON
687 block supposedly visible in this function. Let us
688 first make sure that it is visible and if so, let
689 us display its contents. */
691 fi
= get_selected_frame (_("No frame selected"));
693 /* The following is generally ripped off from stack.c's routine
694 print_frame_info(). */
696 block
= get_frame_block (fi
, 0);
699 gdb_printf (_("No symbol table info available.\n"));
705 info_common_command_for_block (block
, comname
, &values_printed
);
706 /* After handling the function's top-level block, stop. Don't
707 continue to its superblock, the block of per-file symbols. */
708 if (block
->function ())
710 block
= block
->superblock ();
716 gdb_printf (_("No common block '%s'.\n"), comname
);
718 gdb_printf (_("No common blocks.\n"));
722 void _initialize_f_valprint ();
724 _initialize_f_valprint ()
726 add_info ("common", info_common_command
,
727 _("Print out the values contained in a Fortran COMMON block."));