Home | History | Annotate | Line # | Download | only in gdb.fortran
      1  1.1.1.2  christos # Copyright 2020-2024 Free Software Foundation, Inc.
      2      1.1  christos 
      3      1.1  christos # This program is free software; you can redistribute it and/or modify
      4      1.1  christos # it under the terms of the GNU General Public License as published by
      5      1.1  christos # the Free Software Foundation; either version 3 of the License, or
      6      1.1  christos # (at your option) any later version.
      7      1.1  christos #
      8      1.1  christos # This program is distributed in the hope that it will be useful,
      9      1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10      1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11      1.1  christos # GNU General Public License for more details.
     12      1.1  christos #
     13      1.1  christos # You should have received a copy of the GNU General Public License
     14      1.1  christos # along with this program.  If not, see <http://www.gnu.org/licenses/> .
     15      1.1  christos 
     16      1.1  christos # Test calling Fortran functions that are compiled without debug
     17      1.1  christos # information.
     18      1.1  christos 
     19  1.1.1.2  christos require allow_fortran_tests
     20      1.1  christos 
     21      1.1  christos standard_testfile call-no-debug-prog.f90 call-no-debug-func.f90
     22      1.1  christos load_lib fortran.exp
     23      1.1  christos 
     24      1.1  christos if {[prepare_for_testing_full "failed to prepare" \
     25      1.1  christos 	 [list ${binfile} [list debug f90] \
     26      1.1  christos 	      $srcfile [list debug f90] \
     27      1.1  christos 	      $srcfile2 [list nodebug f90]]]} {
     28      1.1  christos     return -1
     29      1.1  christos }
     30      1.1  christos 
     31      1.1  christos # Find a possibly mangled version of NAME, a function we want to call
     32      1.1  christos # that has no debug information available.  We hope that the mangled
     33      1.1  christos # version of NAME contains the pattern NAME, and so we use 'info
     34      1.1  christos # functions' to find a possible suitable symbol.
     35      1.1  christos #
     36      1.1  christos # If no suitable function is found then return the empty string.
     37      1.1  christos proc find_mangled_name { name } {
     38      1.1  christos     global hex gdb_prompt
     39      1.1  christos 
     40      1.1  christos     set into_non_debug_symbols false
     41      1.1  christos     set symbol_name "*unknown*"
     42      1.1  christos     gdb_test_multiple "info function $name" "" {
     43      1.1  christos 	-re ".*Non-debugging symbols:\r\n" {
     44      1.1  christos 	    set into_non_debug_symbols true
     45      1.1  christos 	    exp_continue
     46      1.1  christos 	}
     47      1.1  christos 	-re "$hex.*\[ \t\]+(\[^\r\n\]+)\r\n" {
     48      1.1  christos 	    set symbol_name $expect_out(1,string)
     49      1.1  christos 	    exp_continue
     50      1.1  christos 	}
     51      1.1  christos 	-re "^$gdb_prompt $" {
     52      1.1  christos 	    # Done.
     53      1.1  christos 	}
     54      1.1  christos     }
     55      1.1  christos 
     56      1.1  christos     # If we couldn't find a suitable symbol name return the empty
     57      1.1  christos     # string.
     58      1.1  christos     if { $symbol_name == "*unknown*" } {
     59      1.1  christos 	return ""
     60      1.1  christos     }
     61      1.1  christos 
     62      1.1  christos     return $symbol_name
     63      1.1  christos }
     64      1.1  christos 
     65      1.1  christos # Sample before before starting the exec, in order to avoid picking up symbols
     66      1.1  christos # from shared libs.
     67      1.1  christos set some_func [find_mangled_name "some_func"]
     68      1.1  christos set string_func [find_mangled_name "string_func"]
     69      1.1  christos 
     70      1.1  christos if ![fortran_runto_main] {
     71      1.1  christos     return -1
     72      1.1  christos }
     73      1.1  christos 
     74      1.1  christos # Call the function SOME_FUNC, that takes a single integer and returns
     75      1.1  christos # an integer.  As the function has no debug information then we have
     76      1.1  christos # to pass the integer argument as '&1' so that GDB will send the
     77      1.1  christos # address of an integer '1' (as Fortran arguments are pass by
     78      1.1  christos # reference).
     79      1.1  christos set symbol_name $some_func
     80      1.1  christos if { $symbol_name == "" } {
     81      1.1  christos     untested "couldn't find suitable name for 'some_func'"
     82      1.1  christos } else {
     83      1.1  christos     gdb_test "ptype ${symbol_name}" "type = <unknown return type> \\(\\)"
     84      1.1  christos     gdb_test "print ${symbol_name} (&1)" \
     85      1.1  christos 	"'${symbol_name}' has unknown return type; cast the call to its declared return type"
     86      1.1  christos     gdb_test "print (integer) ${symbol_name} (&1)" " = 2"
     87      1.1  christos }
     88      1.1  christos 
     89      1.1  christos # Call the function STRING_FUNC which takes an assumed shape character
     90      1.1  christos # array (i.e. a string), and returns an integer.
     91      1.1  christos #
     92      1.1  christos # At least for gfortran, passing the string will pass both the data
     93      1.1  christos # pointer and an artificial argument, the length of the string.
     94      1.1  christos #
     95      1.1  christos # The compiled program is expecting the address of the string, so we
     96      1.1  christos # prefix that argument with '&', but the artificial length parameter
     97      1.1  christos # is pass by value, so there's no need for '&' in that case.
     98      1.1  christos set symbol_name $string_func
     99      1.1  christos if { $symbol_name == "" } {
    100      1.1  christos     untested "couldn't find suitable name for 'string_func'"
    101      1.1  christos } else {
    102      1.1  christos     gdb_test "ptype ${symbol_name}" "type = <unknown return type> \\(\\)"
    103      1.1  christos     gdb_test "print ${symbol_name} (&'abcdefg', 3)" \
    104      1.1  christos 	"'${symbol_name}' has unknown return type; cast the call to its declared return type"
    105      1.1  christos     gdb_test_stdio "call (integer) ${symbol_name} (&'abcdefg', 3)" \
    106      1.1  christos 	" abc" \
    107      1.1  christos 	"\\\$\\d+ = 0"
    108      1.1  christos }
    109