Update copyright year range in all GDB files.
[external/binutils.git] / gdb / testsuite / gdb.base / attach-wait-input.exp
1 # Copyright 2014-2019 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 if [use_gdb_stub] {
33     return
34 }
35
36 standard_testfile
37
38 if {[build_executable "failed to build" $testfile $srcfile debug]} {
39     return -1
40 }
41
42 # Start the program running, and return its PID, ready for attaching.
43
44 proc start_program {binfile} {
45     global gdb_prompt
46     global decimal
47
48     clean_restart $binfile
49
50     if ![runto setup_done] then {
51         fail "can't run to setup_done"
52         return 0
53     }
54
55     # Get the PID of the test process.
56     set testpid ""
57     set test "get inferior process ID"
58     gdb_test_multiple "p mypid" $test {
59         -re " = ($decimal)\r\n$gdb_prompt $" {
60             set testpid $expect_out(1,string)
61             pass $test
62         }
63     }
64
65     gdb_test "detach" "Detaching from program: .*"
66
67     if {$testpid == ""} {
68         return
69     }
70
71     return $testpid
72 }
73
74 # Do test proper.  EDITING indicates whether "set editing" is on or
75 # off.
76
77 proc test { editing } {
78     global gdb_prompt
79     global binfile
80     global decimal
81
82     with_test_prefix "editing $editing" {
83
84         set testpid [start_program $binfile]
85         if {$testpid == ""} {
86             return
87         }
88
89         # Enable/disable readline.
90         gdb_test_no_output "set editing $editing"
91
92         # Send both commands at once.
93         send_gdb "attach $testpid\nprint should_exit = 1\n"
94
95         # Use gdb_expect directly instead of gdb_test_multiple to
96         # avoid races with the double prompt.
97         set test "attach and print"
98         gdb_expect {
99             -re "Attaching to program.*process $testpid\r\n.*$gdb_prompt.*$decimal = 1\r\n$gdb_prompt $" {
100                 pass "$test"
101             }
102             timeout {
103                 fail "$test (timeout)"
104             }
105         }
106
107         # As we've used attach, on quit, we'll detach from the
108         # program.  Explicitly kill it in case we failed above.
109         gdb_test "kill" \
110             "" \
111             "after attach, exit" \
112             "Kill the program being debugged.*y or n. $" \
113             "y"
114     }
115 }
116
117 foreach editing {"on" "off"} {
118     test $editing
119 }