1# Copyright 2016-2023 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# Test that -exec-run works as expected.  Exercises various testing
17# axes:
18#
19# - MI running on main UI vs separate UI.
20#
21# - inferior tty set to main tty vs separate tty.
22#
23# - forking the child failing and sending output to the right inferior
24#   terminal, vs the child not failing to start.
25
26load_lib mi-support.exp
27set MIFLAGS "-i=mi"
28
29# The purpose of this testcase is to test the -exec-run command. If we
30# cannot use it, then there is no point in running this testcase.
31if [use_gdb_stub] {
32     untested "cannot use -exec-run command"
33     return -1
34}
35
36standard_testfile mi-start.c
37
38if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
39     untested "could not build mi-exec-run"
40     return -1
41}
42
43# The test proper.  INFTTY_MODE determines whether "set inferior-tty"
44# is in effect.  MI_MODE determines whether MI is run on the main UI,
45# or as a separate UI.  FORCE_FAIL is true when we want -exec-run to
46# fail and cause inferior output be sent to the inferior tty.
47
48proc test {inftty_mode mi_mode force_fail} {
49    global srcdir subdir binfile srcfile
50    global gdb_spawn_id gdb_main_spawn_id mi_spawn_id inferior_spawn_id
51    global decimal
52
53    mi_gdb_exit
54
55    set start_ops {}
56    if {$inftty_mode == "separate"} {
57	lappend start_ops "separate-inferior-tty"
58    }
59    if {$mi_mode == "separate"} {
60	lappend start_ops "separate-mi-tty"
61    }
62
63    if [mi_gdb_start $start_ops] {
64	return
65    }
66
67    if {$force_fail} {
68	# Disable the shell so that its the first exec that fails,
69	# instead of the shell starting and then failing with some
70	# unspecified output.
71	mi_gdb_test "-gdb-set startup-with-shell off" ".*"
72	set bin $binfile.nox
73    } else {
74	set bin $binfile
75    }
76
77    mi_delete_breakpoints
78    mi_gdb_reinitialize_dir $srcdir/$subdir
79    mi_gdb_reinitialize_dir $srcdir/$subdir
80    mi_gdb_load ${bin}
81
82    # Useful for debugging:
83    verbose -log "Channels:"
84    verbose -log " inferior_spawn_id=$inferior_spawn_id"
85    verbose -log " gdb_spawn_id=$gdb_spawn_id"
86    verbose -log " gdb_main_spawn_id=$gdb_main_spawn_id"
87    verbose -log " mi_spawn_id=$mi_spawn_id"
88
89    if {$force_fail} {
90	set saw_perm_error 0
91	set saw_mi_error 0
92	set already_failed 0
93	set test "run failure detected"
94	send_gdb "-exec-run --start\n"
95
96	# Redirect through SPAWN_LIST global.  If the
97	# inferior_spawn_id is not the same as gdb_spawn_id, e.g. when
98	# testing with gdbserver, the gdbserver can exit after
99	# emitting it's error message.
100	#
101	# If inferior_spawn_id exits then we may see the eof from that
102	# spawn-id before we see the pattern from the gdb_spawn_id,
103	# which will kick us out of the gdb_expect, and cause us to
104	# fail the test.
105	#
106	# Instead we clean SPAWN_LIST once we've seen the expected
107	# pattern from that spawn-id, and after that we no longer care
108	# when gdbserver exits.
109	global spawn_list
110	set spawn_list "$inferior_spawn_id"
111
112	gdb_expect {
113	    -i spawn_list
114	    -re ".*Cannot exec.*Permission denied" {
115		set saw_perm_error 1
116		set spawn_list ""
117		verbose -log "saw perm error"
118		if {!$saw_mi_error} {
119		    exp_continue
120		}
121	    }
122	    -i "$gdb_spawn_id"
123	    -re "\\^error,msg=\"(During startup program exited with code 127|Running .* on the remote target failed)" {
124		set saw_mi_error 1
125		verbose -log "saw mi error"
126		if {!$saw_perm_error} {
127		    exp_continue
128		}
129	    }
130	    timeout {
131		set already_failed 1
132		fail "$test (timeout)"
133	    }
134	    -i "$gdb_main_spawn_id"
135	    eof {
136		set already_failed 1
137		fail "$test (eof)"
138	    }
139	}
140
141	if {$saw_perm_error && $saw_mi_error} {
142	    pass $test
143	} elseif {!$already_failed} {
144	    verbose -log "saw_perm_error=$saw_perm_error; saw_mi_error=$saw_mi_error"
145	    fail $test
146	}
147    } else {
148	mi_run_cmd "--start"
149	mi_expect_stop "breakpoint-hit" "main" "" ".*$srcfile" "$decimal" \
150	    { "" "disp=\"del\"" } "breakpoint hit reported on mi"
151
152	if {$mi_mode == "separate"} {
153	    # Check that the breakpoint hit is reported on the main
154	    # UI/CLI.  Note no prompt is expected.
155	    switch_gdb_spawn_id $gdb_main_spawn_id
156
157	    set test "breakpoint hit reported on console"
158	    gdb_test_multiple "" $test {
159		-re "Temporary breakpoint .*, main \\(\\) at .*$srcfile:$decimal.*return 0;" {
160		    pass $test
161		}
162	    }
163
164	    # Switch back to the MI UI.
165	    global mi_spawn_id
166	    switch_gdb_spawn_id $mi_spawn_id
167	}
168    }
169}
170
171# Create a not-executable copy of the program, in order to exercise
172# vfork->exec failing.
173gdb_remote_download host $binfile $binfile.nox
174remote_exec target "chmod \"a-x\" $binfile.nox"
175
176foreach_with_prefix inferior-tty {"main" "separate"} {
177    foreach_with_prefix mi {"main" "separate"} {
178	foreach_with_prefix force-fail {0 1} {
179	    test ${inferior-tty} ${mi} ${force-fail}
180	}
181    }
182}
183
184mi_gdb_exit
185