diff options
author | Andrew Burgess <andrew.burgess@embecosm.com> | 2020-10-08 16:45:59 +0100 |
---|---|---|
committer | Andrew Burgess <andrew.burgess@embecosm.com> | 2020-11-19 11:23:23 +0000 |
commit | a5c641b57b0b5e245b8a011cccc93a4120c8bd63 (patch) | |
tree | 4780ab64fb1549c549ff7a8b369ec57ca36aadb0 /gdb/testsuite/gdb.fortran/array-slices.exp | |
parent | a15a5258b5b422645faca888c1279f249903512e (diff) | |
download | binutils-a5c641b57b0b5e245b8a011cccc93a4120c8bd63.zip binutils-a5c641b57b0b5e245b8a011cccc93a4120c8bd63.tar.gz binutils-a5c641b57b0b5e245b8a011cccc93a4120c8bd63.tar.bz2 |
gdb/fortran: Add support for Fortran array slices at the GDB prompt
This commit brings array slice support to GDB.
WARNING: This patch contains a rather big hack which is limited to
Fortran arrays, this can be seen in gdbtypes.c and f-lang.c. More
details on this below.
This patch rewrites two areas of GDB's Fortran support, the code to
extract an array slice, and the code to print an array.
After this commit a user can, from the GDB prompt, ask for a slice of
a Fortran array and should get the correct result back. Slices can
(optionally) have the lower bound, upper bound, and a stride
specified. Slices can also have a negative stride.
Fortran has the concept of repacking array slices. Within a compiled
Fortran program if a user passes a non-contiguous array slice to a
function then the compiler may have to repack the slice, this involves
copying the elements of the slice to a new area of memory before the
call, and copying the elements back to the original array after the
call. Whether repacking occurs will depend on which version of
Fortran is being used, and what type of function is being called.
This commit adds support for both packed, and unpacked array slicing,
with the default being unpacked.
With an unpacked array slice, when the user asks for a slice of an
array GDB creates a new type that accurately describes where the
elements of the slice can be found within the original array, a
value of this type is then returned to the user. The address of an
element within the slice will be equal to the address of an element
within the original array.
A user can choose to select packed array slices instead using:
(gdb) set fortran repack-array-slices on|off
(gdb) show fortran repack-array-slices
With packed array slices GDB creates a new type that reflects how the
elements of the slice would look if they were laid out in contiguous
memory, allocates a value of this type, and then fetches the elements
from the original array and places then into the contents buffer of
the new value.
One benefit of using packed slices over unpacked slices is the memory
usage, taking a small slice of N elements from a large array will
require (in GDB) N * ELEMENT_SIZE bytes of memory, while an unpacked
array will also include all of the "padding" between the
non-contiguous elements. There are new tests added that highlight
this difference.
There is also a new debugging flag added with this commit that
introduces these commands:
(gdb) set debug fortran-array-slicing on|off
(gdb) show debug fortran-array-slicing
This prints information about how the array slices are being built.
As both the repacking, and the array printing requires GDB to walk
through a multi-dimensional Fortran array visiting each element, this
commit adds the file f-array-walk.h, which introduces some
infrastructure to support this process. This means the array printing
code in f-valprint.c is significantly reduced.
The only slight issue with this commit is the "rather big hack" that I
mentioned above. This hack allows us to handle one specific case,
array slices with negative strides. This is something that I don't
believe the current GDB value contents model will allow us to
correctly handle, and rather than rewrite the value contents code
right now, I'm hoping to slip this hack in as a work around.
The problem is that, as I see it, the current value contents model
assumes that an object base address will be the lowest address within
that object, and that the contents of the object start at this base
address and occupy the TYPE_LENGTH bytes after that.
( We do have the embedded_offset, which is used for C++ sub-classes,
such that an object can start at some offset from the content buffer,
however, the assumption that the object then occupies the next
TYPE_LENGTH bytes is still true within GDB. )
The problem is that Fortran arrays with a negative stride don't follow
this pattern. In this case the base address of the object points to
the element with the highest address, the contents of the array then
start at some offset _before_ the base address, and proceed for one
element _past_ the base address.
As the stride for such an array would be negative then, in theory the
TYPE_LENGTH for this type would also be negative. However, in many
places a value in GDB will degrade to a pointer + length, and the
length almost always comes from the TYPE_LENGTH.
It is my belief that in order to correctly model this case the value
content handling of GDB will need to be reworked to split apart the
value's content buffer (which is a block of memory with a length), and
the object's in memory base address and length, which could be
negative.
Things are further complicated because arrays with negative strides
like this are always dynamic types. When a value has a dynamic type
and its base address needs resolving we actually store the address of
the object within the resolved dynamic type, not within the value
object itself.
In short I don't currently see an easy path to cleanly support this
situation within GDB. And so I believe that leaves two options,
either add a work around, or catch cases where the user tries to make
use of a negative stride, or access an array with a negative stride,
and throw an error.
This patch currently goes with adding a work around, which is that
when we resolve a dynamic Fortran array type, if the stride is
negative, then we adjust the base address to point to the lowest
address required by the array. The printing and slicing code is aware
of this adjustment and will correctly slice and print Fortran arrays.
Where this hack will show through to the user is if they ask for the
address of an array in their program with a negative array stride, the
address they get from GDB will not match the address that would be
computed within the Fortran program.
gdb/ChangeLog:
* Makefile.in (HFILES_NO_SRCDIR): Add f-array-walker.h.
* NEWS: Mention new options.
* f-array-walker.h: New file.
* f-lang.c: Include 'gdbcmd.h' and 'f-array-walker.h'.
(repack_array_slices): New static global.
(show_repack_array_slices): New function.
(fortran_array_slicing_debug): New static global.
(show_fortran_array_slicing_debug): New function.
(value_f90_subarray): Delete.
(skip_undetermined_arglist): Delete.
(class fortran_array_repacker_base_impl): New class.
(class fortran_lazy_array_repacker_impl): New class.
(class fortran_array_repacker_impl): New class.
(fortran_value_subarray): Complete rewrite.
(set_fortran_list): New static global.
(show_fortran_list): Likewise.
(_initialize_f_language): Register new commands.
(fortran_adjust_dynamic_array_base_address_hack): New function.
* f-lang.h (fortran_adjust_dynamic_array_base_address_hack):
Declare.
* f-valprint.c: Include 'f-array-walker.h'.
(class fortran_array_printer_impl): New class.
(f77_print_array_1): Delete.
(f77_print_array): Delete.
(fortran_print_array): New.
(f_value_print_inner): Update to call fortran_print_array.
* gdbtypes.c: Include 'f-lang.h'.
(resolve_dynamic_type_internal): Call
fortran_adjust_dynamic_array_base_address_hack.
gdb/testsuite/ChangeLog:
* gdb.fortran/array-slices-bad.exp: New file.
* gdb.fortran/array-slices-bad.f90: New file.
* gdb.fortran/array-slices-sub-slices.exp: New file.
* gdb.fortran/array-slices-sub-slices.f90: New file.
* gdb.fortran/array-slices.exp: Rewrite tests.
* gdb.fortran/array-slices.f90: Rewrite tests.
* gdb.fortran/vla-sizeof.exp: Correct expected results.
gdb/doc/ChangeLog:
* gdb.texinfo (Debugging Output): Document 'set/show debug
fortran-array-slicing'.
(Special Fortran Commands): Document 'set/show fortran
repack-array-slices'.
Diffstat (limited to 'gdb/testsuite/gdb.fortran/array-slices.exp')
-rw-r--r-- | gdb/testsuite/gdb.fortran/array-slices.exp | 277 |
1 files changed, 228 insertions, 49 deletions
diff --git a/gdb/testsuite/gdb.fortran/array-slices.exp b/gdb/testsuite/gdb.fortran/array-slices.exp index aa6bc63..ff00fae 100644 --- a/gdb/testsuite/gdb.fortran/array-slices.exp +++ b/gdb/testsuite/gdb.fortran/array-slices.exp @@ -18,6 +18,21 @@ # the subroutine. This should exercise GDB's ability to handle # different strides for the different dimensions. +# Testing GDB's ability to print array (and string) slices, including +# slices that make use of array strides. +# +# In the Fortran code various arrays of different ranks are filled +# with data, and slices are passed to a series of show functions. +# +# In this test script we break in each of the show functions, print +# the array slice that was passed in, and then move up the stack to +# the parent frame and check GDB can manually extract the same slice. +# +# This test also checks that the size of the array slice passed to the +# function (so as extracted and described by the compiler and the +# debug information) matches the size of the slice manually extracted +# by GDB. + if {[skip_fortran_tests]} { return -1 } standard_testfile ".f90" @@ -28,60 +43,224 @@ if {[prepare_for_testing ${testfile}.exp ${testfile} ${srcfile} \ return -1 } -if ![fortran_runto_main] { - untested "could not run to main" - return -1 +# Takes the name of an array slice as used in the test source, and extracts +# the base array name. For example: 'array (1,2)' becomes 'array'. +proc array_slice_to_var { slice_str } { + regexp "^(?:\\s*\\()*(\[^( \t\]+)" $slice_str matchvar varname + return $varname } -gdb_breakpoint "show" -gdb_breakpoint [gdb_get_line_number "Final Breakpoint"] - -set array_contents \ - [list \ - " = \\(\\(1, 2, 3, 4, 5, 6, 7, 8, 9, 10\\) \\(11, 12, 13, 14, 15, 16, 17, 18, 19, 20\\) \\(21, 22, 23, 24, 25, 26, 27, 28, 29, 30\\) \\(31, 32, 33, 34, 35, 36, 37, 38, 39, 40\\) \\(41, 42, 43, 44, 45, 46, 47, 48, 49, 50\\) \\(51, 52, 53, 54, 55, 56, 57, 58, 59, 60\\) \\(61, 62, 63, 64, 65, 66, 67, 68, 69, 70\\) \\(71, 72, 73, 74, 75, 76, 77, 78, 79, 80\\) \\(81, 82, 83, 84, 85, 86, 87, 88, 89, 90\\) \\(91, 92, 93, 94, 95, 96, 97, 98, 99, 100\\)\\)" \ - " = \\(\\(1, 2, 3, 4, 5\\) \\(11, 12, 13, 14, 15\\) \\(21, 22, 23, 24, 25\\) \\(31, 32, 33, 34, 35\\) \\(41, 42, 43, 44, 45\\)\\)" \ - " = \\(\\(1, 3, 5, 7, 9\\) \\(21, 23, 25, 27, 29\\) \\(41, 43, 45, 47, 49\\) \\(61, 63, 65, 67, 69\\) \\(81, 83, 85, 87, 89\\)\\)" \ - " = \\(\\(1, 4, 7, 10\\) \\(21, 24, 27, 30\\) \\(41, 44, 47, 50\\) \\(61, 64, 67, 70\\) \\(81, 84, 87, 90\\)\\)" \ - " = \\(\\(1, 5, 9\\) \\(31, 35, 39\\) \\(61, 65, 69\\) \\(91, 95, 99\\)\\)" \ - " = \\(\\(-26, -25, -24, -23, -22, -21, -20, -19, -18, -17\\) \\(-19, -18, -17, -16, -15, -14, -13, -12, -11, -10\\) \\(-12, -11, -10, -9, -8, -7, -6, -5, -4, -3\\) \\(-5, -4, -3, -2, -1, 0, 1, 2, 3, 4\\) \\(2, 3, 4, 5, 6, 7, 8, 9, 10, 11\\) \\(9, 10, 11, 12, 13, 14, 15, 16, 17, 18\\) \\(16, 17, 18, 19, 20, 21, 22, 23, 24, 25\\) \\(23, 24, 25, 26, 27, 28, 29, 30, 31, 32\\) \\(30, 31, 32, 33, 34, 35, 36, 37, 38, 39\\) \\(37, 38, 39, 40, 41, 42, 43, 44, 45, 46\\)\\)" \ - " = \\(\\(-26, -25, -24, -23, -22, -21\\) \\(-19, -18, -17, -16, -15, -14\\) \\(-12, -11, -10, -9, -8, -7\\)\\)" \ - " = \\(\\(-26, -24, -22, -20, -18\\) \\(-5, -3, -1, 1, 3\\) \\(16, 18, 20, 22, 24\\) \\(37, 39, 41, 43, 45\\)\\)" ] - -set message_strings \ - [list \ - " = 'array'" \ - " = 'array \\(1:5,1:5\\)'" \ - " = 'array \\(1:10:2,1:10:2\\)'" \ - " = 'array \\(1:10:3,1:10:2\\)'" \ - " = 'array \\(1:10:5,1:10:3\\)'" \ - " = 'other'" \ - " = 'other \\(-5:0, -2:0\\)'" \ - " = 'other \\(-5:4:2, -2:7:3\\)'" ] - -set i 0 -foreach result $array_contents msg $message_strings { - incr i - with_test_prefix "test $i" { - gdb_continue_to_breakpoint "show" - gdb_test "p array" $result - gdb_test "p message" "$msg" +proc run_test { repack } { + global binfile gdb_prompt + + clean_restart ${binfile} + + if ![fortran_runto_main] { + untested "could not run to main" + return -1 } -} -gdb_continue_to_breakpoint "continue to Final Breakpoint" + gdb_test_no_output "set fortran repack-array-slices $repack" + + # gdb_breakpoint [gdb_get_line_number "Display Message Breakpoint"] + gdb_breakpoint [gdb_get_line_number "Display Element"] + gdb_breakpoint [gdb_get_line_number "Display String"] + gdb_breakpoint [gdb_get_line_number "Display Array Slice 1D"] + gdb_breakpoint [gdb_get_line_number "Display Array Slice 2D"] + gdb_breakpoint [gdb_get_line_number "Display Array Slice 3D"] + gdb_breakpoint [gdb_get_line_number "Display Array Slice 4D"] + gdb_breakpoint [gdb_get_line_number "Final Breakpoint"] + + # We're going to print some reasonably large arrays. + gdb_test_no_output "set print elements unlimited" + + set found_final_breakpoint false + + # We place a limit on the number of tests that can be run, just in + # case something goes wrong, and GDB gets stuck in an loop here. + set test_count 0 + while { $test_count < 500 } { + with_test_prefix "test $test_count" { + incr test_count + + set found_final_breakpoint false + set expected_result "" + set func_name "" + gdb_test_multiple "continue" "continue" { + -re ".*GDB = (\[^\r\n\]+)\r\n" { + set expected_result $expect_out(1,string) + exp_continue + } + -re "! Display Element" { + set func_name "show_elem" + exp_continue + } + -re "! Display String" { + set func_name "show_str" + exp_continue + } + -re "! Display Array Slice (.)D" { + set func_name "show_$expect_out(1,string)d" + exp_continue + } + -re "! Final Breakpoint" { + set found_final_breakpoint true + exp_continue + } + -re "$gdb_prompt $" { + # We're done. + } + } -# Next test that asking for an array with stride at the CLI gives an -# error. -clean_restart ${testfile} + if ($found_final_breakpoint) { + break + } -if ![fortran_runto_main] then { - perror "couldn't run to main" - continue + # We want to take a look at the line in the previous frame that + # called the current function. I couldn't find a better way of + # doing this than 'up', which will print the line, then 'down' + # again. + # + # I don't want to fill the log with passes for these up/down + # commands, so we don't report any. If something goes wrong then we + # should get a fail from gdb_test_multiple. + set array_slice_name "" + set unique_id "" + array unset replacement_vars + array set replacement_vars {} + gdb_test_multiple "up" "up" { + -re "\r\n\[0-9\]+\[ \t\]+call ${func_name} \\((\[^\r\n\]+)\\)\r\n$gdb_prompt $" { + set array_slice_name $expect_out(1,string) + } + -re "\r\n\[0-9\]+\[ \t\]+call ${func_name} \\((\[^\r\n\]+)\\)\[ \t\]+! VARS=(\[^ \t\r\n\]+)\r\n$gdb_prompt $" { + set array_slice_name $expect_out(1,string) + set unique_id $expect_out(2,string) + } + } + if {$unique_id != ""} { + set str "" + foreach v [split $unique_id ,] { + set val [get_integer_valueof "${v}" "??"\ + "get variable '$v' for '$array_slice_name'"] + set replacement_vars($v) $val + if {$str != ""} { + set str "Str," + } + set str "$str$v=$val" + } + set unique_id " $str" + } + gdb_test_multiple "down" "down" { + -re "\r\n$gdb_prompt $" { + # Don't issue a pass here. + } + } + + # Check we have all the information we need to successfully run one + # of these tests. + if { $expected_result == "" } { + perror "failed to extract expected results" + return 0 + } + if { $array_slice_name == "" } { + perror "failed to extract array slice name" + return 0 + } + + # Check GDB can correctly print the array slice that was passed into + # the current frame. + set pattern [string_to_regexp " = $expected_result"] + gdb_test "p array" "$pattern" \ + "check value of '$array_slice_name'$unique_id" + + # Get the size of the slice. + set size_in_show \ + [get_integer_valueof "sizeof (array)" "show_unknown" \ + "get sizeof '$array_slice_name'$unique_id in show"] + set addr_in_show \ + [get_hexadecimal_valueof "&array" "show_unknown" \ + "get address '$array_slice_name'$unique_id in show"] + + # Now move into the previous frame, and see if GDB can extract the + # array slice from the original parent object. Again, use of + # gdb_test_multiple to avoid filling the logs with unnecessary + # passes. + gdb_test_multiple "up" "up" { + -re "\r\n$gdb_prompt $" { + # Do nothing. + } + } + + # Print the array slice, this will force GDB to manually extract the + # slice from the parent array. + gdb_test "p $array_slice_name" "$pattern" \ + "check array slice '$array_slice_name'$unique_id can be extracted" + + # Get the size of the slice in the calling frame. + set size_in_parent \ + [get_integer_valueof "sizeof ($array_slice_name)" \ + "parent_unknown" \ + "get sizeof '$array_slice_name'$unique_id in parent"] + + # Figure out the start and end addresses of the full array in the + # parent frame. + set full_var_name [array_slice_to_var $array_slice_name] + set start_addr [get_hexadecimal_valueof "&${full_var_name}" \ + "start unknown"] + set end_addr [get_hexadecimal_valueof \ + "(&${full_var_name}) + sizeof (${full_var_name})" \ + "end unknown"] + + # The Fortran compiler can choose to either send a descriptor that + # describes the array slice to the subroutine, or it can repack the + # slice into an array section and send that. + # + # We find the address range of the original array in the parent, + # and the address of the slice in the show function, if the + # address of the slice (from show) is in the range of the original + # array then repacking has not occurred, otherwise, the slice is + # outside of the parent, and repacking must have occurred. + # + # The goal here is to compare the sizes of the slice in show with + # the size of the slice extracted by GDB. So we can only compare + # sizes when GDB's repacking setting matches the repacking + # behaviour we got from the compiler. + if { ($addr_in_show < $start_addr || $addr_in_show >= $end_addr) \ + == ($repack == "on") } { + gdb_assert {$size_in_show == $size_in_parent} \ + "check sizes match" + } elseif { $repack == "off" } { + # GDB's repacking is off (so slices are left unpacked), but + # the compiler did pack this one. As a result we can't + # compare the sizes between the compiler's slice and GDB's + # slice. + verbose -log "slice '$array_slice_name' was repacked, sizes can't be compared" + } else { + # Like the above, but the reverse, GDB's repacking is on, but + # the compiler didn't repack this slice. + verbose -log "slice '$array_slice_name' was not repacked, sizes can't be compared" + } + + # If the array name we just tested included variable names, then + # test again with all the variables expanded. + if {$unique_id != ""} { + foreach v [array names replacement_vars] { + set val $replacement_vars($v) + set array_slice_name \ + [regsub "\\y${v}\\y" $array_slice_name $val] + } + gdb_test "p $array_slice_name" "$pattern" \ + "check array slice '$array_slice_name'$unique_id can be extracted, with variables expanded" + } + } + } + + # Ensure we reached the final breakpoint. If more tests have been added + # to the test script, and this starts failing, then the safety 'while' + # loop above might need to be increased. + gdb_assert {$found_final_breakpoint} "ran all tests" } -gdb_breakpoint "show" -gdb_continue_to_breakpoint "show" -gdb_test "up" ".*" -gdb_test "p array (1:10:2, 1:10:2)" \ - "Fortran array strides are not currently supported" \ - "using array stride gives an error" +foreach_with_prefix repack { on off } { + run_test $repack +} |