[PATCH 5/6] Respect `set print array-indexes' with Fortran arrays
Andrew Burgess
aburgess@redhat.com
Wed Dec 15 16:49:24 GMT 2021
* Maciej W. Rozycki <macro@embecosm.com> [2021-12-11 11:47:44 +0000]:
> Add `set print array-indexes' handling for Fortran arrays. Currently
> the setting is ignored and indices are never shown.
>
> Keep track of the most recent index handled so that any outstanding
> repeated elements printed when the limit set by `set print elements' is
> hit have the correct index shown.
>
> Output now looks like:
>
> (gdb) set print array-indexes on
> (gdb) print array_1d
> $1 = ((-2) = 1, (-1) = 1, (0) = 1, (1) = 1, (2) = 1)
> (gdb) set print repeats 4
> (gdb) set print elements 12
> (gdb) print array_2d
> $2 = ((-2) = ((-2) = 2, <repeats 5 times>) (-1) = ((-2) = 2, <repeats 5 times>) (0) = ((-2) = 2, (-1) = 2, ...) ...)
> (gdb)
>
> for a 5-element vector and a 5 by 5 array filled with the value of 2.
> ---
> gdb/f-array-walker.h | 13 +
> gdb/f-lang.c | 20 ++
> gdb/f-lang.h | 6
> gdb/f-valprint.c | 40 ++++-
> gdb/testsuite/gdb.fortran/array-indices.exp | 200 ++++++++++++++++++++++++++++
> 5 files changed, 266 insertions(+), 13 deletions(-)
>
> gdb-fortran-set-print-array-indexes.diff
> Index: src/gdb/f-array-walker.h
> ===================================================================
> --- src.orig/gdb/f-array-walker.h
> +++ src/gdb/f-array-walker.h
> @@ -138,7 +138,8 @@ struct fortran_array_walker_base_impl
> true only when this is the last element that will be processed in
> this dimension. */
> void process_dimension (std::function<void (struct type *, int, bool)> walk_1,
> - struct type *elt_type, LONGEST elt_off, bool last_p)
> + struct type *elt_type, LONGEST elt_off,
> + struct type *, LONGEST, bool last_p)
I'd like to see the new arguments given names, and the comment
extended to explain what they're for, given this is the base
implementation from which others inherit it seems like this is the
obvious place someone would look to figure this sort of thing out.
> {
> walk_1 (elt_type, elt_off, last_p);
> }
> @@ -162,7 +163,8 @@ struct fortran_array_walker_base_impl
> process_element (TYPE, OFFSET, true);
> finish_dimension (true, true);
> finish_dimension (false, true); */
> - void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
> + void process_element (struct type *elt_type, LONGEST elt_off,
> + struct type *, LONGEST, bool last_p)
Again, I don't think it's GDB style to skip argument names - at least
it's not something I see often.
> { /* Nothing. */ }
> };
>
> @@ -223,6 +225,8 @@ class fortran_array_walker
> if (m_nss != m_ndimensions)
> {
> struct type *subarray_type = TYPE_TARGET_TYPE (check_typedef (type));
> + gdb_assert (range_type->code () == TYPE_CODE_RANGE);
I think this assert should move up to immediately after we fetch
range_type, I'm pretty sure that if range_type isn't TYPE_CODE_RANGE
then the call to get_discrete_bounds will have already gone wrong.
> + struct type *index_type = TYPE_TARGET_TYPE (range_type);
>
> /* For dimensions other than the inner most, walk each element and
> recurse while peeling off one more dimension of the array. */
> @@ -239,7 +243,7 @@ class fortran_array_walker
> {
> this->walk_1 (w_type, w_offset, w_last_p);
> },
> - subarray_type, new_offset, i == upperbound);
> + subarray_type, new_offset, index_type, i, i == upperbound);
> }
> }
> else
> @@ -260,7 +264,8 @@ class fortran_array_walker
> elt_type = resolve_dynamic_type (elt_type, {}, e_address);
> }
>
> - m_impl.process_element (elt_type, elt_off, (i == upperbound));
> + m_impl.process_element (elt_type, elt_off, range_type, i,
> + i == upperbound);
This seems a little weird, you're passing the range_type through to
the index_type parameter? Is this really what you mean?
Assuming you mean to pass TYPE_TARGET_TYPE(range_type) here, then the
index_type ends up being passed through to both process_element and
process_dimension. You then end up placing the index_type into a
member variable within the m_impl. I wonder if it would be better to
pass the index_type to start_dimension, and stash it there, then we'd
only need to pass the index value through maybe? Or, maybe there
would be problems with handling multi-dimensional arrays? I guess
it's pretty hard to actually uncover bugs in this area as the
index_type is almost always the same I think....
> }
> }
>
> Index: src/gdb/f-lang.c
> ===================================================================
> --- src.orig/gdb/f-lang.c
> +++ src/gdb/f-lang.c
> @@ -330,7 +330,8 @@ class fortran_lazy_array_repacker_impl
> /* Create a lazy value in target memory representing a single element,
> then load the element into GDB's memory and copy the contents into the
> destination value. */
> - void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
> + void process_element (struct type *elt_type, LONGEST elt_off,
> + struct type *, LONGEST, bool last_p)
Missing arg names again here, and below.
Thanks,
Andrew
> {
> copy_element_to_dest (value_at_lazy (elt_type, m_addr + elt_off));
> }
> @@ -368,7 +369,8 @@ class fortran_array_repacker_impl
> /* Extract an element of ELT_TYPE at offset (M_BASE_OFFSET + ELT_OFF)
> from the content buffer of M_VAL then copy this extracted value into
> the repacked destination value. */
> - void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
> + void process_element (struct type *elt_type, LONGEST elt_off,
> + struct type *, LONGEST, bool last_p)
> {
> struct value *elt
> = value_from_component (m_val, elt_type, (elt_off + m_base_offset));
> @@ -1532,6 +1534,20 @@ fortran_structop_operation::evaluate (st
>
> /* See language.h. */
>
> +void
> +f_language::print_array_index (struct type *index_type, LONGEST index,
> + struct ui_file *stream,
> + const value_print_options *options) const
> +{
> + struct value *index_value = value_from_longest (index_type, index);
> +
> + fprintf_filtered (stream, "(");
> + value_print (index_value, stream, options);
> + fprintf_filtered (stream, ") = ");
> +}
> +
> +/* See language.h. */
> +
> void
> f_language::language_arch_info (struct gdbarch *gdbarch,
> struct language_arch_info *lai) const
> Index: src/gdb/f-lang.h
> ===================================================================
> --- src.orig/gdb/f-lang.h
> +++ src/gdb/f-lang.h
> @@ -59,6 +59,12 @@ class f_language : public language_defn
> }
>
> /* See language.h. */
> + void print_array_index (struct type *index_type,
> + LONGEST index,
> + struct ui_file *stream,
> + const value_print_options *options) const override;
> +
> + /* See language.h. */
> void language_arch_info (struct gdbarch *gdbarch,
> struct language_arch_info *lai) const override;
>
> Index: src/gdb/f-valprint.c
> ===================================================================
> --- src.orig/gdb/f-valprint.c
> +++ src/gdb/f-valprint.c
> @@ -185,11 +185,14 @@ class fortran_array_printer_impl : publi
> /* Called when processing dimensions of the array other than the
> innermost one. WALK_1 is the walker to normally call, ELT_TYPE is
> the type of the element being extracted, and ELT_OFF is the offset
> - of the element from the start of array being walked, and LAST_P is
> - true only when this is the last element that will be processed in
> - this dimension. */
> + of the element from the start of array being walked, INDEX_TYPE
> + and INDEX is the type and the value respectively of the element's
> + index in the dimension currently being walked and LAST_P is true
> + only when this is the last element that will be processed in this
> + dimension. */
> void process_dimension (std::function<void (struct type *, int, bool)> walk_1,
> - struct type *elt_type, LONGEST elt_off, bool last_p)
> + struct type *elt_type, LONGEST elt_off,
> + struct type *index_type, LONGEST index, bool last_p)
> {
> size_t dim_indx = m_dimension - 1;
> struct type *elt_type_prev = m_elt_type_prev;
> @@ -220,10 +223,15 @@ class fortran_array_printer_impl : publi
> if (!repeated)
> fputs_filtered (" ", m_stream);
> m_elts += nrepeats * m_stats[dim_indx + 1].nelts;
> + m_index += nrepeats;
> }
> else
> for (LONGEST i = nrepeats; i > 0; i--)
> - walk_1 (elt_type_prev, elt_off_prev, repeated && i == 1);
> + {
> + maybe_print_array_index (index_type, index - nrepeats + repeated,
> + m_stream, m_options);
> + walk_1 (elt_type_prev, elt_off_prev, repeated && i == 1);
> + }
>
> if (!repeated)
> {
> @@ -234,6 +242,8 @@ class fortran_array_printer_impl : publi
> to `continue_walking' from our caller won't do that. */
> if (m_elts < m_options->print_max)
> {
> + maybe_print_array_index (index_type, index,
> + m_stream, m_options);
> walk_1 (elt_type, elt_off, last_p);
> nrepeats++;
> }
> @@ -247,14 +257,20 @@ class fortran_array_printer_impl : publi
>
> m_elt_type_prev = elt_type;
> m_elt_off_prev = elt_off;
> + m_index_type = index_type;
> + m_index = index;
>
> if (last_p)
> m_stats[dim_indx].elts_counted = true;
> }
>
> /* Called to process an element of ELT_TYPE at offset ELT_OFF from the
> - start of the parent object. */
> - void process_element (struct type *elt_type, LONGEST elt_off, bool last_p)
> + start of the parent object, where INDEX_TYPE and INDEX is the type
> + and the value respectively of the element's index in the dimension
> + currently being walked and LAST_P is true only when this is the last
> + element that will be processed in this dimension. */
> + void process_element (struct type *elt_type, LONGEST elt_off,
> + struct type *index_type, LONGEST index, bool last_p)
> {
> size_t dim_indx = m_dimension - 1;
> struct type *elt_type_prev = m_elt_type_prev;
> @@ -284,6 +300,7 @@ class fortran_array_printer_impl : publi
>
> if (printed)
> fputs_filtered (", ", m_stream);
> + maybe_print_array_index (index_type, index, m_stream, m_options);
> common_val_print (e_val, m_stream, m_recurse, m_options,
> current_language);
> }
> @@ -293,6 +310,8 @@ class fortran_array_printer_impl : publi
>
> m_elt_type_prev = elt_type;
> m_elt_off_prev = elt_off;
> + m_index_type = index_type;
> + m_index = index;
> ++m_elts;
>
> if (last_p && !m_stats[dim_indx].elts_counted)
> @@ -318,6 +337,7 @@ class fortran_array_printer_impl : publi
> plongest (nrepeats + 1),
> nullptr);
> annotate_elt_rep_end ();
> + m_index += nrepeats;
> }
> else
> {
> @@ -326,6 +346,8 @@ class fortran_array_printer_impl : publi
>
> for (LONGEST i = nrepeats; i > 0; i--)
> {
> + maybe_print_array_index (m_index_type, m_index - i + 1,
> + m_stream, m_options);
> common_val_print (e_val, m_stream, m_recurse, m_options,
> current_language);
> if (i > 1)
> @@ -398,6 +420,10 @@ class fortran_array_printer_impl : publi
> struct type *m_elt_type_prev;
> LONGEST m_elt_off_prev;
>
> + /* Index type and value tracker. */
> + struct type *m_index_type;
> + LONGEST m_index;
> +
> /* Per-dimension stats. */
> std::vector<struct dimension_stats> m_stats;
> };
> Index: src/gdb/testsuite/gdb.fortran/array-indices.exp
> ===================================================================
> --- /dev/null
> +++ src/gdb/testsuite/gdb.fortran/array-indices.exp
> @@ -0,0 +1,200 @@
> +# Copyright 2021 Free Software Foundation, Inc.
> +
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program. If not, see <http://www.gnu.org/licenses/>.
> +
> +# Test the printing of element indices in Fortran arrays.
> +
> +if {[skip_fortran_tests]} { return -1 }
> +
> +load_lib fortran.exp
> +
> +# Build up the expected output for each array.
> +set n0 {(-2)}
> +set n1 {(-1)}
> +set n2 {(0)}
> +set n3 {(1)}
> +set n4 {(2)}
> +set n5 {(3)}
> +set a9p9o "($n0 = 9, $n1 = 9, $n2 = 9, $n3 = 9, $n4 = 9, $n5 = 9)"
> +set a1p "($n0 = 1, $n1 = 1, $n2 = 1, $n3 = 1, $n4 = 1)"
> +set a1p9 "($n0 = 1, $n1 = 1, $n2 = 1, $n3 = 1, $n4 = 1, $n5 = 9)"
> +set a2po "($n0 = 2, $n1 = 2, $n2 = 2, $n3 = 2, $n4 = 2)"
> +set a2p "($n0 = ${a2po} $n1 = ${a2po} $n2 = ${a2po} $n3 = ${a2po}\
> + $n4 = ${a2po})"
> +set a2p9o "($n0 = 2, $n1 = 2, $n2 = 2, $n3 = 2, $n4 = 2, $n5 = 9)"
> +set a2p9 "($n0 = ${a2p9o} $n1 = ${a2p9o} $n2 = ${a2p9o} $n3 = ${a2p9o}\
> + $n4 = ${a2p9o} $n5 = ${a9p9o})"
> +set a3po "($n0 = 3, $n1 = 3, $n2 = 3, $n3 = 3, $n4 = 3)"
> +set a3p "($n0 = ${a3po} $n1 = ${a3po} $n2 = ${a3po} $n3 = ${a3po}\
> + $n4 = ${a3po})"
> +set a3p "($n0 = ${a3p} $n1 = ${a3p} $n2 = ${a3p} $n3 = ${a3p} $n4 = ${a3p})"
> +set a3p9o "($n0 = 3, $n1 = 3, $n2 = 3, $n3 = 3, $n4 = 3, $n5 = 9)"
> +set a3p9 "($n0 = ${a3p9o} $n1 = ${a3p9o} $n2 = ${a3p9o} $n3 = ${a3p9o}\
> + $n4 = ${a3p9o} $n5 = ${a9p9o})"
> +set a9p9 "($n0 = ${a9p9o} $n1 = ${a9p9o} $n2 = ${a9p9o} $n3 = ${a9p9o}\
> + $n4 = ${a9p9o} $n5 = ${a9p9o})"
> +set a3p9 "($n0 = ${a3p9} $n1 = ${a3p9} $n2 = ${a3p9} $n3 = ${a3p9}\
> + $n4 = ${a3p9} $n5 = ${a9p9})"
> +
> +# Convert the output into a regexp.
> +set r1p [string_to_regexp $a1p]
> +set r1p9 [string_to_regexp $a1p9]
> +set r2po [string_to_regexp $a2po]
> +set r2p9o [string_to_regexp $a2p9o]
> +set r2p [string_to_regexp $a2p]
> +set r2p9 [string_to_regexp $a2p9]
> +set r3po [string_to_regexp $a3po]
> +set r3p9o [string_to_regexp $a3p9o]
> +set r3p [string_to_regexp $a3p]
> +set r3p9 [string_to_regexp $a3p9]
> +
> +set rep5 "<repeats 5 times>"
> +set rep6 "<repeats 6 times>"
> +
> +proc array_repeat { variant } {
> + global testfile srcfile
> + upvar n0 n0 n1 n1 n2 n2 n5 n5
> + upvar r1p r1p r1p9 r1p9 r2po r2po r2p9o r2p9o r2p r2p r2p9 r2p9
> + upvar r3po r3po r3p9o r3p9o r3p r3p r3p9 r3p9
> + upvar a2po a2po a2p9o a2p9o a3po a3po a3p9o a3p9o
> + upvar rep5 rep5 rep6 rep6
> +
> + standard_testfile "${variant}.f90"
> +
> + if {[prepare_for_testing ${testfile}.exp ${variant} ${srcfile} \
> + {debug f90}]} {
> + return -1
> + }
> +
> + with_test_prefix "${variant}" {
> + gdb_test_no_output "set print array-indexes on"
> + }
> +
> + if {![fortran_runto_main]} {
> + perror "Could not run to main."
> + continue
> + }
> +
> + gdb_breakpoint [gdb_get_line_number "Break here"]
> + gdb_continue_to_breakpoint "${variant}"
> +
> + with_test_prefix "${variant}: repeats=unlimited, elements=unlimited" {
> + # Check the arrays print as expected.
> + gdb_test_no_output "set print repeats unlimited"
> + gdb_test_no_output "set print elements unlimited"
> +
> + gdb_test "print array_1d" "${r1p}"
> + gdb_test "print array_1d9" "${r1p9}"
> + gdb_test "print array_2d" "${r2p}"
> + gdb_test "print array_2d9" "${r2p9}"
> + gdb_test "print array_3d" "${r3p}"
> + gdb_test "print array_3d9" "${r3p9}"
> + }
> +
> + with_test_prefix "${variant}: repeats=4, elements=unlimited" {
> + # Now set the repeat limit.
> + gdb_test_no_output "set print repeats 4"
> + gdb_test_no_output "set print elements unlimited"
> +
> + gdb_test "print array_1d" \
> + [string_to_regexp "($n0 = 1, ${rep5})"]
> + gdb_test "print array_1d9" \
> + [string_to_regexp "($n0 = 1, ${rep5}, $n5 = 9)"]
> + gdb_test "print array_2d" \
> + [string_to_regexp "($n0 = ($n0 = 2, ${rep5}) ${rep5})"]
> + gdb_test "print array_2d9" \
> + [string_to_regexp "($n0 = ($n0 = 2, ${rep5}, $n5 = 9) ${rep5}\
> + $n5 = ($n0 = 9, ${rep6}))"]
> + gdb_test "print array_3d" \
> + [string_to_regexp "($n0 = ($n0 = ($n0 = 3, ${rep5}) ${rep5})\
> + ${rep5})"]
> + gdb_test "print array_3d9" \
> + [string_to_regexp "($n0 = ($n0 = ($n0 = 3, ${rep5}, $n5 = 9)\
> + ${rep5} $n5 = ($n0 = 9, ${rep6}))\
> + ${rep5}\
> + $n5 = ($n0 = ($n0 = 9, ${rep6}) ${rep6}))"]
> + }
> +
> + with_test_prefix "${variant}: repeats=unlimited, elements=12" {
> + # Now set the element limit.
> + gdb_test_no_output "set print repeats unlimited"
> + gdb_test_no_output "set print elements 12"
> +
> + gdb_test "print array_1d" "${r1p}"
> + gdb_test "print array_1d9" "${r1p9}"
> + gdb_test "print array_2d" \
> + [string_to_regexp "($n0 = ${a2po} $n1 = ${a2po}\
> + $n2 = ($n0 = 2, $n1 = 2, ...) ...)"]
> + gdb_test "print array_2d9" \
> + [string_to_regexp "($n0 = ${a2p9o} $n1 = ${a2p9o} ...)"]
> + gdb_test "print array_3d" \
> + [string_to_regexp "($n0 = ($n0 = ${a3po} $n1 = ${a3po}\
> + $n2 = ($n0 = 3, $n1 = 3, ...)\
> + ...) ...)"]
> + gdb_test "print array_3d9" \
> + [string_to_regexp "($n0 = ($n0 = ${a3p9o} $n1 = ${a3p9o} ...)\
> + ...)"]
> + }
> +
> + with_test_prefix "${variant}: repeats=4, elements=12" {
> + # Now set both limits.
> + gdb_test_no_output "set print repeats 4"
> + gdb_test_no_output "set print elements 12"
> +
> + gdb_test "print array_1d" \
> + [string_to_regexp "($n0 = 1, ${rep5})"]
> + gdb_test "print array_1d9" \
> + [string_to_regexp "($n0 = 1, ${rep5}, $n5 = 9)"]
> + gdb_test "print array_2d" \
> + [string_to_regexp "($n0 = ($n0 = 2, ${rep5})\
> + $n1 = ($n0 = 2, ${rep5})\
> + $n2 = ($n0 = 2, $n1 = 2, ...) ...)"]
> + gdb_test "print array_2d9" \
> + [string_to_regexp "($n0 = ($n0 = 2, ${rep5}, $n5 = 9)\
> + $n1 = ($n0 = 2, ${rep5}, $n5 = 9) ...)"]
> + gdb_test "print array_3d" \
> + [string_to_regexp "($n0 = ($n0 = ($n0 = 3, ${rep5})\
> + $n1 = ($n0 = 3, ${rep5})\
> + $n2 = ($n0 = 3, $n1 = 3, ...) ...) ...)"]
> + gdb_test "print array_3d9" \
> + [string_to_regexp "($n0 = ($n0 = ($n0 = 3, ${rep5}, $n5 = 9)\
> + $n1 = ($n0 = 3, ${rep5}, $n5 = 9)\
> + ...) ...)"]
> + }
> +
> + with_test_prefix "${variant}: repeats=4, elements=30" {
> + # Now set both limits.
> + gdb_test_no_output "set print repeats 4"
> + gdb_test_no_output "set print elements 30"
> +
> + gdb_test "print array_1d" \
> + [string_to_regexp "($n0 = 1, ${rep5})"]
> + gdb_test "print array_1d9" \
> + [string_to_regexp "($n0 = 1, ${rep5}, $n5 = 9)"]
> + gdb_test "print array_2d" \
> + [string_to_regexp "($n0 = ($n0 = 2, ${rep5}) ${rep5})"]
> + gdb_test "print array_2d9" \
> + [string_to_regexp "($n0 = ($n0 = 2, ${rep5}, $n5 = 9) ${rep5}\
> + ...)"]
> + gdb_test "print array_3d" \
> + [string_to_regexp "($n0 = ($n0 = ($n0 = 3, ${rep5}) ${rep5})\
> + $n1 = ($n0 = ($n0 = 3, ${rep5}) ...) ...)"]
> + gdb_test "print array_3d9" \
> + [string_to_regexp "($n0 = ($n0 = ($n0 = 3, ${rep5}, $n5 = 9)\
> + ${rep5} ...) ...)"]
> + }
> +}
> +
> +array_repeat "array-repeat"
> +array_repeat "array-slices-repeat"
>
More information about the Gdb-patches
mailing list