1 # Copyright 2024 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 doing a "attach" that fails, and then another "attach". 17 18 require can_spawn_for_attach 19 20 standard_testfile 21 22 if {[build_executable "failed to build" $testfile $srcfile {debug}]} { 23 return -1 24 } 25 26 set test_spawn_id [spawn_wait_for_attach $binfile] 27 set testpid [spawn_id_get_pid $test_spawn_id] 28 29 # Test an attach that fails. 30 31 proc test_bad_attach {test} { 32 global testpid gdb_prompt 33 34 set boguspid 0 35 if { [istarget "*-*-*bsd*"] } { 36 # In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead 37 # (which should have the desired effect on any version of 38 # FreeBSD, and probably other *BSD's too). 39 set boguspid -1 40 } 41 gdb_test_multiple "attach $boguspid" $test { 42 -re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" { 43 # Response expected on ptrace-based systems (i.e. GNU/Linux). 44 pass "$test" 45 } 46 -re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" { 47 pass "$gdb_test_name" 48 } 49 -re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" { 50 pass "$gdb_test_name" 51 } 52 -re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" { 53 # Response expected from /proc-based systems. 54 pass "$gdb_test_name" 55 } 56 -re "Can't attach to process..*$gdb_prompt $" { 57 # Response expected on Windows. 58 pass "$gdb_test_name" 59 } 60 -re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" { 61 # Response expected on the extended-remote target. 62 pass "$gdb_test_name" 63 } 64 } 65 } 66 67 # Test an attach that succeeds. 68 69 proc test_good_attach {test} { 70 gdb_test "attach $::testpid" \ 71 "Attaching to program.*, process $::testpid.*" \ 72 "$test" 73 74 set thread_count [get_valueof "" "\$_inferior_thread_count" -1] 75 gdb_assert {$thread_count > 0} \ 76 "attached" 77 } 78 79 proc_with_prefix test {} { 80 clean_restart $::binfile 81 82 # GDB used to have a bug on Windows where failing to attach once 83 # made a subsequent "attach" or "run" hang. So it's important for 84 # this regression test that we try to attach more than once. 85 86 test_bad_attach "bad attach 1" 87 test_bad_attach "bad attach 2" 88 89 # For good measure, test that we can attach to something after 90 # failing to attach previously. 91 test_good_attach "good attach" 92 } 93 94 test 95