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