detach-while-running.exp revision 1.1.1.1 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 detaching while the inferior is running. Basically:
17 #
18 # (gdb) attach PID
19 # (gdb) c&
20 # (gdb) detach
21
22 require can_spawn_for_attach
23
24 standard_testfile
25
26 if {[build_executable "failed to prepare" $testfile $srcfile {debug}] == -1} {
27 return
28 }
29
30 # The test proper. See description above.
31
32 proc test {} {
33 global binfile gdb_prompt
34
35 # This test requires executing commands while the target is
36 # running, which, when testing with the remote target, requires
37 # non-stop remote protocol. Until that variant of the RSP is the
38 # default, force target non-stop mode on.
39 set is_remote \
40 [expr {[target_info exists gdb_protocol] \
41 && ([target_info gdb_protocol] == "remote" \
42 || [target_info gdb_protocol] == "extended-remote")}]
43
44 save_vars { ::GDBFLAGS } {
45 if {$is_remote} {
46 append ::GDBFLAGS " -ex \"maint set target-non-stop on\""
47 }
48 clean_restart ${binfile}
49 }
50
51 set test_spawn_id [spawn_wait_for_attach $binfile]
52 set testpid [spawn_id_get_pid $test_spawn_id]
53
54 set any "\[^\r\n\]*"
55
56 # Iterate more than once so that we test re-attaching after
57 # detaching, in case GDB incorrectly detaches and the process
58 # crashes after the detach.
59 set n_iters 2
60 for {set iter 1} {$iter <= $n_iters} {incr iter} {
61 with_test_prefix "iter=$iter" {
62 set attached 0
63
64 gdb_test_multiple "attach $testpid" "attach" {
65 -re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
66 pass $gdb_test_name
67 set attached 1
68 }
69 }
70
71 if {!$attached} {
72 break
73 }
74
75 gdb_test_multiple "continue &" "" {
76 -re "Continuing\.\r\n$::gdb_prompt " {
77 pass $gdb_test_name
78 }
79 }
80
81 gdb_test "detach" "Detaching from.*"
82
83 # Sleep a bit before reattaching to let the detached
84 # process crash and exit if e.g., GDB managed to leave
85 # breakpoint traps behind.
86 if {$iter != $n_iters} {
87 sleep 1
88 }
89 }
90 }
91
92 kill_wait_spawned_process $test_spawn_id
93 }
94
95 test
96