1# Copyright 2019-2020 Free Software Foundation, Inc.
2
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program.  If not, see <http://www.gnu.org/licenses/> .
15
16# Further testing of placing breakpoints in nested subroutines.
17
18if {[skip_fortran_tests]} { return -1 }
19load_lib "fortran.exp"
20
21standard_testfile ".f90"
22
23if {[prepare_for_testing ${testfile}.exp ${testfile} \
24	 ${srcfile} {debug f90}]} {
25    return -1
26}
27
28set int4 [fortran_int4]
29
30# When WITH_SRC_PREFIX_P is true then some symbol references will be
31# prefixed with the filename.  When WITH_NEST_PREFIX_P is true then
32# nested subroutine symbols will be prefixed with their parent
33# subroutine scope.
34proc do_bp_tests {with_src_prefix_p with_nest_prefix_p} {
35    global testfile srcfile
36    global int4
37    global hex
38
39    clean_restart ${testfile}
40
41    if { $with_src_prefix_p } {
42	set src_prefix "${srcfile}:"
43    } else {
44	set src_prefix ""
45    }
46
47    if { $with_nest_prefix_p } {
48	set nest_prefix "contains_keyword::"
49    } else {
50	set nest_prefix ""
51    }
52
53    # Test setting up breakpoints and otherwise examining nested
54    # functions before the program starts.
55    with_test_prefix "before start" {
56	foreach entry \
57	    [list \
58		 [list "increment" "${int4} \\\(${int4}\\\)"] \
59		 [list "increment_program_global" "${int4} \\\(void\\\)"] \
60		 [list "hidden_variable" "void \\\(void\\\)"]] {
61		     set function [lindex $entry 0]
62		     set type [lindex $entry 1]
63
64		     # Currently referencing symbols using 'info',
65		     # 'whatis' and 'ptype' before the program is
66		     # started doesn't work.  This is the same
67		     # behaviour we see in C++ so I don't think this
68		     # is a failure, just a limitation in current GDB.
69		     if { ${with_nest_prefix_p} } {
70			 gdb_test "info symbol ${nest_prefix}${function}" \
71			     "${function} in section .*"
72			 gdb_test "whatis ${nest_prefix}${function}" \
73			     "type = ${type}"
74			 gdb_test "ptype ${nest_prefix}${function}" \
75			     "type = ${type}"
76			 gdb_test "print ${nest_prefix}${function}" \
77			     "{${type}} $hex <contains_keyword::${function}>"
78		     }
79
80		     gdb_breakpoint "${src_prefix}${nest_prefix}${function}"
81		 }
82    }
83
84    # Break on a contained function and run to it.
85    if {![runto ${src_prefix}[gdb_get_line_number "pre_init"]]} then {
86	perror "couldn't run to breakpoint pre_init"
87	continue
88    }
89
90    # Call a contained function.
91    if { ${with_nest_prefix_p} } {
92	gdb_test_stdio "call ${nest_prefix}subroutine_to_call()" " called" ""
93    }
94
95    # Break on another contained function and run to it.
96    gdb_breakpoint "${src_prefix}${nest_prefix}increment"
97    gdb_continue_to_breakpoint "increment" ".*increment = i \\\+ 1"
98    gdb_breakpoint ${src_prefix}[gdb_get_line_number "post_increment"]
99    gdb_continue_to_breakpoint "post_increment"
100
101    # Check arguments and locals report the correct values. 12 is
102    # passed in and 13 is the result after adding 1.
103    gdb_test "info args" "i = 12"
104    gdb_test "info locals" " = 13"
105
106    # Check we can see variables from an outer program scope.
107    gdb_breakpoint ${src_prefix}[gdb_get_line_number "post_increment_global"]
108    gdb_continue_to_breakpoint "post_increment_global" \
109	".*print \\\*, program_i ! post_increment_global"
110    gdb_test "info args" "No arguments." \
111	"no argument subroutine has no arguments"
112    gdb_test "p program_i" " = 7" "printing outer scoped variable"
113
114    # Stepping into a contained subroutine.
115    gdb_breakpoint ${src_prefix}[gdb_get_line_number "pre_step"]
116    gdb_continue_to_breakpoint "pre_step" ".*call step\\\(\\\) ! pre_step"
117    gdb_test "step" \
118	".*print '\\\(A\\\)', \\\"step\\\" ! post_step" \
119	"step into the correct place"
120
121    # Local hides program global.
122    gdb_breakpoint ${src_prefix}[gdb_get_line_number "post_hidden"]
123    gdb_continue_to_breakpoint "post_hidden" \
124	".*print \\\*, program_i ! post_hidden"
125    gdb_test "p program_i" " = 30" "printing hidden global"
126
127    # Check info symbol, whatis and ptype can find information on
128    # these nested functions.
129    foreach entry \
130	[list \
131	     [list "increment" "${int4} \\\(${int4}\\\)"] \
132	     [list "increment_program_global" "${int4} \\\(void\\\)"]] {
133		 set function [lindex $entry 0]
134		 set type [lindex $entry 1]
135		 with_test_prefix $function {
136		     gdb_test "info symbol ${nest_prefix}$function" \
137			 "$function in section .*"
138		     gdb_test "whatis ${nest_prefix}$function" \
139			 "type = ${type}"
140		     gdb_test "ptype ${nest_prefix}$function" \
141			 "type = ${type}"
142		 }
143	     }
144}
145
146foreach_with_prefix src_prefix { 0 1 } {
147    foreach_with_prefix nest_prefix { 0 1 } {
148	do_bp_tests ${src_prefix} ${nest_prefix}
149    }
150}
151