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