1# Copyright 1994-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
16if [target_info exists gdb,nointerrupts] {
17    verbose "Skipping interrupt.exp because of nointerrupts."
18    continue
19}
20
21if [target_info exists gdb,noinferiorio] {
22    verbose "Skipping interrupt.exp because of noinferiorio."
23    return
24}
25
26standard_testfile
27
28set options { debug }
29if { ! [target_info exists gdb,nosignals] } {
30    lappend options "additional_flags=-DSIGNALS"
31}
32
33if {[build_executable $testfile.exp $testfile $srcfile $options] == -1} {
34    untested "failed to compile"
35    return -1
36}
37
38gdb_start
39
40
41if ![file exists $binfile] then {
42    perror "$binfile does not exist."
43    return 0
44} else {
45    gdb_reinitialize_dir $srcdir/$subdir
46    gdb_load $binfile
47    # Hope this is unix :-)
48    gdb_test "shell stty intr '^C'" ".*" \
49	"set interrupt character in interrupt.exp"
50    if [runto_main] then {
51	global inferior_spawn_id gdb_spawn_id
52
53	set msg "process is alive"
54	gdb_test_multiple "continue" $msg {
55	    -i "$inferior_spawn_id" -re "talk to me baby\r\n" {
56		pass $msg
57	    }
58	}
59
60	# This should appear twice, once for the echo and once for the
61	# program's output.
62
63	set msg "child process ate our char"
64	send_inferior "a\n"
65	gdb_test_multiple "" $msg {
66	    -i "$inferior_spawn_id" -re "^a\r\na\r\n$" {
67		pass $msg
68	    }
69	}
70
71	# Wait until the program is in the read system call again.
72	sleep 2
73
74	# Cntrl-c may fail for simulator targets running on a BSD host.
75	# This is the result of a combination of the read syscall
76	# being restarted and gdb capturing the cntrl-c signal.
77
78	# Cntrl-c may fail for simulator targets on slow hosts.
79	# This is because there is a race condition between entering
80	# the read and delivering the cntrl-c.
81
82	send_gdb "\003"
83	set msg "send_gdb control C"
84	gdb_test_multiple "" $msg {
85	    -re "Program received signal SIGINT.*$gdb_prompt $" {
86		pass $msg
87	    }
88	}
89
90	set msg "call function when asleep"
91	send_gdb "p func1 ()\n"
92	gdb_test_multiple "" $msg {
93	    -re " = 4.*$gdb_prompt $" {
94		pass $msg
95	    }
96	    -re ".*Program received signal SIG(SEGV|ILL).*$gdb_prompt $" {
97		setup_xfail "i*86-pc-linux*-gnu*"
98		fail "child died when we called func1, skipped rest of tests"
99		return
100	    }
101	    -re "$gdb_prompt $" {
102		fail "call function when asleep (wrong output)"
103	    }
104	    default {
105
106		# This fail probably happens whenever we use /proc (we
107		# don't use PRSABORT), but apparently also happens on
108		# other machines as well.
109
110		setup_xfail "sparc*-*-solaris2*"
111		setup_xfail "i*86-*-solaris2*"
112		setup_xfail "*-*-sysv4*"
113		setup_xfail "vax-*-*"
114		setup_xfail "alpha-*-*"
115		setup_xfail "*-*-*bsd*"
116		setup_xfail "*-*-*lynx*"
117		fail "$msg (stays asleep)"
118		# Send the inferior a newline to wake it up.
119		send_inferior "\n"
120		gdb_test "" " = 4" "call function after waking it"
121	    }
122	}
123
124	# Now try calling the function again.
125	gdb_test "p func1 ()" " = 4" "call function a second time"
126
127	# And the program should still be doing the same thing.
128	# The optional trailing \r\n is in case we sent a newline above
129	# to wake the program, in which case the program now sends it
130	# back.  We check for it either here or in the next gdb_expect
131	# command, because which one it ends up in is timing dependent.
132	send_gdb "continue\n"
133	# For some reason, i386-*-sysv4 gdb fails to issue the Continuing
134	# message, but otherwise appears normal (FIXME).
135
136	set msg "continue"
137	gdb_test_multiple "" "$msg" {
138	    -re "^continue\r\nContinuing.\r\n(\r\n|)$" {
139		pass $msg
140	    }
141	    -re "^continue\r\n\r\n" {
142		fail "$msg (missing Continuing.)"
143	    }
144	}
145
146	send_inferior "data\n"
147	# The optional leading \r\n is in case we sent a newline above
148	# to wake the program, in which case the program now sends it
149	# back.
150
151	set msg "echo data"
152	gdb_test_multiple "" $msg {
153	    -i "$inferior_spawn_id" -re "^(\r\n|)data\r\ndata\r\n$" {
154		pass $msg
155	    }
156	    -i "$gdb_spawn_id" -re "Undefined command.*$gdb_prompt " {
157		fail $msg
158	    }
159	}
160
161 	if { ! [target_info exists gdb,nosignals] } {
162	    # Wait until the program is in the read system call again.
163	    sleep 2
164
165	    # Stop the program for another test.
166	    set msg "Send Control-C, second time"
167	    send_gdb "\003"
168	    gdb_test_multiple "" "$msg" {
169		-re "Program received signal SIGINT.*$gdb_prompt $" {
170		    pass "$msg"
171		}
172	    }
173
174	    # The "signal" command should deliver the correct signal and
175	    # return to the loop.
176	    set msg "signal SIGINT"
177	    gdb_test_multiple "signal SIGINT" "$msg" {
178		-re "^signal SIGINT\r\nContinuing with signal SIGINT.\r\n(\r\n|)$" {
179		    pass "$msg"
180		}
181	    }
182
183	    # We should be back in the loop.
184	    send_inferior "more data\n"
185
186	    set msg "echo more data"
187	    gdb_test_multiple "" $msg {
188		-i "$inferior_spawn_id" -re "^(\r\n|)more data\r\nmore data\r\n$" {
189		    pass $msg
190		}
191	    }
192	}
193
194	set saw_end_of_file 0
195	set saw_inferior_exit 0
196
197	set msg "send end of file"
198	send_inferior "\004"
199
200	set spawn_list "$inferior_spawn_id"
201
202	gdb_test_multiple "" $msg {
203	    -i spawn_list -re "end of file" {
204		set saw_end_of_file 1
205		verbose -log "saw \"end of file\""
206		if {!$saw_inferior_exit} {
207		    # When $inferior_spawn_id != $gdb_spawn_id, such
208		    # as when testing with gdbserver, we may see the
209		    # eof (the process exit, not the string just
210		    # matched) for $inferior_spawn_id before the
211		    # expected gdb output.  Clear this so we no longer
212		    # expect anything out of $inferior_spawn_id.
213		    set spawn_list ""
214		    exp_continue
215		}
216	    }
217	    -i "$gdb_spawn_id" -re "$inferior_exited_re normally.*$gdb_prompt " {
218		set saw_inferior_exit 1
219		verbose -log "saw inferior exit"
220		if {!$saw_end_of_file} {
221		    exp_continue
222		}
223	    }
224	}
225
226	gdb_assert { $saw_end_of_file && $saw_inferior_exit } $msg
227    }
228}
229return 0
230