
When attempting to call a Fortran function for which there is no debug information we currently trigger undefined behaviour in GDB by accessing non-existent type fields. The reason is that in order to prepare the arguments, for a call to a Fortran function, we need to know the type of each argument. If the function being called has no debug information then obviously GDB doesn't know about the argument types and we should either give the user an error or pick a suitable default. What we currently do is just assume the field exist and access undefined memory, which is clearly wrong. The reason GDB needs to know the argument type is to tell if the argument is artificial or not, artificial arguments will be passed by value while non-artificial arguments will be passed by reference. An ideal solution for this problem would be to allow the user to cast the function to the correct type, we already do this to some degree with the return value, for example: (gdb) print some_func_ () 'some_func_' has unknown return type; cast the call to its declared return type (gdb) print (integer) some_func_ () $1 = 1 But if we could extend this to allow casting to the full function type, GDB could figure out from the signature what are real parameters, and what are artificial parameters. Maybe something like this: (gdb) print ((integer () (integer, double)) some_other_func_ (1, 2.3) Alas, right now the Fortran expression parser doesn't seem to support parsing function signatures, and we certainly don't have support for figuring out real vs artificial arguments from a signature. Still, I think we can prevent GDB from accessing undefined memory and provide a reasonable default behaviour. In this commit I: - Only ask if the argument is artificial if the type of the argument is actually known. - Unknown arguments are assumed to be artificial and passed by value (non-artificial arguments are pass by reference). - If an artificial argument is prefixed with '&' by the user then we treat the argument as pass-by-reference. With these three changes we avoid undefined behaviour in GDB, and allow the user, in most cases, to get a reasonably natural default behaviour. gdb/ChangeLog: PR fortran/26155 * f-lang.c (fortran_argument_convert): Delete declaration. (fortran_prepare_argument): New function. (evaluate_subexp_f): Move logic to new function fortran_prepare_argument. gdb/testsuite/ChangeLog: PR fortran/26155 * gdb.fortran/call-no-debug-func.f90: New file. * gdb.fortran/call-no-debug-prog.f90: New file. * gdb.fortran/call-no-debug.exp: New file.
102 lines
3.6 KiB
Text
102 lines
3.6 KiB
Text
# Copyright 2020-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 calling Fortran functions that are compiled without debug
|
|
# information.
|
|
|
|
if {[skip_fortran_tests]} { return -1 }
|
|
|
|
standard_testfile call-no-debug-prog.f90 call-no-debug-func.f90
|
|
load_lib fortran.exp
|
|
|
|
if {[prepare_for_testing_full "failed to prepare" \
|
|
[list ${binfile} [list debug f90] \
|
|
$srcfile [list debug f90] \
|
|
$srcfile2 [list nodebug f90]]]} {
|
|
return -1
|
|
}
|
|
|
|
if ![fortran_runto_main] {
|
|
untested "could not run to main"
|
|
return -1
|
|
}
|
|
|
|
# Find a possibly mangled version of NAME, a function we want to call
|
|
# that has no debug information available. We hope that the mangled
|
|
# version of NAME contains the pattern NAME, and so we use 'info
|
|
# functions' to find a possible suitable symbol.
|
|
#
|
|
# If no suitable function is found then return the empty string.
|
|
proc find_mangled_name { name } {
|
|
global hex gdb_prompt
|
|
|
|
set into_non_debug_symbols false
|
|
set symbol_name "*unknown*"
|
|
gdb_test_multiple "info function $name" "" {
|
|
-re ".*Non-debugging symbols:\r\n" {
|
|
set into_non_debug_symbols true
|
|
exp_continue
|
|
}
|
|
-re "$hex.*\[ \t\]+(\[^\r\n\]+)\r\n" {
|
|
set symbol_name $expect_out(1,string)
|
|
exp_continue
|
|
}
|
|
-re "^$gdb_prompt $" {
|
|
# Done.
|
|
}
|
|
}
|
|
|
|
# If we couldn't find a suitable symbol name return the empty
|
|
# string.
|
|
if { $symbol_name == "*unknown*" } {
|
|
return ""
|
|
}
|
|
|
|
return $symbol_name
|
|
}
|
|
|
|
# Call the function SOME_FUNC, that takes a single integer and returns
|
|
# an integer. As the function has no debug information then we have
|
|
# to pass the integer argument as '&1' so that GDB will send the
|
|
# address of an integer '1' (as Fortran arguments are pass by
|
|
# reference).
|
|
set symbol_name [find_mangled_name "some_func"]
|
|
if { $symbol_name == "" } {
|
|
untested "couldn't find suitable name for 'some_func'"
|
|
} else {
|
|
gdb_test "ptype ${symbol_name}" "type = <unknown return type> \\(\\)"
|
|
gdb_test "print ${symbol_name} (&1)" \
|
|
"'${symbol_name}' has unknown return type; cast the call to its declared return type"
|
|
gdb_test "print (integer) ${symbol_name} (&1)" " = 2"
|
|
}
|
|
|
|
# Call the function STRING_FUNC which takes an assumed shape character
|
|
# array (i.e. a string), and returns an integer.
|
|
#
|
|
# At least for gfortran, passing the string will pass both the data
|
|
# pointer and an artificial argument, the length of the string.
|
|
#
|
|
# The compiled program is expecting the address of the string, so we
|
|
# prefix that argument with '&', but the artificial length parameter
|
|
# is pass by value, so there's no need for '&' in that case.
|
|
set symbol_name [find_mangled_name "string_func"]
|
|
if { $symbol_name == "" } {
|
|
untested "couldn't find suitable name for 'string_func'"
|
|
} else {
|
|
gdb_test "ptype ${symbol_name}" "type = <unknown return type> \\(\\)"
|
|
gdb_test "print ${symbol_name} (&'abcdefg', 3)" \
|
|
"'${symbol_name}' has unknown return type; cast the call to its declared return type"
|
|
gdb_test "call (integer) ${symbol_name} (&'abcdefg', 3)" " abc\r\n\\\$\\d+ = 0"
|
|
}
|