Remove path name from test case
[binutils-gdb.git] / gdb / testsuite / gdb.base / attach-wait-input.exp
1 # Copyright 2014-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 # Verify that GDB waits for the "attach" command to finish before
17 # processing the following command.
18 #
19 # GDB used to have a race where on async targets, in the small window
20 # between the attach request and the initial stop for the attach, GDB
21 # was still processing user input.
22 #
23 # The issue was originally detected with:
24 #
25 # echo -e "attach PID\nset xxx=1" | gdb
26 #
27 # In that scenario, stdin is not a tty, which disables readline.
28 # Explicitly turning off editing exercises the same code path, and is
29 # simpler to do, so we test with both editing on and off.
30
31 # The test uses the "attach" command.
32 require !use_gdb_stub
33
34 standard_testfile
35
36 if {[build_executable "failed to build" $testfile $srcfile debug]} {
37 return -1
38 }
39
40 # Start the program running, and return its PID, ready for attaching.
41
42 proc start_program {binfile} {
43 global gdb_prompt
44 global decimal
45
46 clean_restart $binfile
47
48 if {![runto setup_done]} {
49 return 0
50 }
51
52 # Get the PID of the test process.
53 set testpid ""
54 set test "get inferior process ID"
55 gdb_test_multiple "p mypid" $test {
56 -re " = ($decimal)\r\n$gdb_prompt $" {
57 set testpid $expect_out(1,string)
58 pass $test
59 }
60 }
61
62 gdb_test "detach" "Detaching from program: .*"
63
64 if {$testpid == ""} {
65 return
66 }
67
68 return $testpid
69 }
70
71 # Do test proper. EDITING indicates whether "set editing" is on or
72 # off.
73
74 proc test { editing } {
75 global gdb_prompt
76 global binfile
77 global decimal
78
79 with_test_prefix "editing $editing" {
80
81 set testpid [start_program $binfile]
82 if {$testpid == ""} {
83 return
84 }
85
86 # Enable/disable readline.
87 gdb_test_no_output "set editing $editing"
88
89 # Send both commands at once.
90 send_gdb "attach $testpid\nprint should_exit = 1\n"
91
92 # Use gdb_expect directly instead of gdb_test_multiple to
93 # avoid races with the double prompt.
94 set test "attach and print"
95 gdb_expect {
96 -re "Attaching to program.*process $testpid\r\n.*$gdb_prompt.*$decimal = 1\r\n$gdb_prompt $" {
97 pass "$test"
98 }
99 timeout {
100 fail "$test (timeout)"
101 }
102 }
103
104 # As we've used attach, on quit, we'll detach from the
105 # program. Explicitly kill it in case we failed above.
106 gdb_test "kill" \
107 "" \
108 "after attach, exit" \
109 "Kill the program being debugged.*y or n. $" \
110 "y"
111 }
112 }
113
114 foreach editing {"on" "off"} {
115 test $editing
116 }