Home | History | Annotate | Line # | Download | only in gdb.base
      1  1.1  christos # Copyright 2024 Free Software Foundation, Inc.
      2  1.1  christos 
      3  1.1  christos # This program is free software; you can redistribute it and/or modify
      4  1.1  christos # it under the terms of the GNU General Public License as published by
      5  1.1  christos # the Free Software Foundation; either version 3 of the License, or
      6  1.1  christos # (at your option) any later version.
      7  1.1  christos #
      8  1.1  christos # This program is distributed in the hope that it will be useful,
      9  1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  1.1  christos # GNU General Public License for more details.
     12  1.1  christos #
     13  1.1  christos # You should have received a copy of the GNU General Public License
     14  1.1  christos # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     15  1.1  christos 
     16  1.1  christos # Test detaching while the inferior is running.  Basically:
     17  1.1  christos #
     18  1.1  christos #  (gdb) attach PID
     19  1.1  christos #  (gdb) c&
     20  1.1  christos #  (gdb) detach
     21  1.1  christos 
     22  1.1  christos require can_spawn_for_attach
     23  1.1  christos 
     24  1.1  christos standard_testfile
     25  1.1  christos 
     26  1.1  christos if {[build_executable "failed to prepare" $testfile $srcfile {debug}] == -1} {
     27  1.1  christos     return
     28  1.1  christos }
     29  1.1  christos 
     30  1.1  christos # The test proper.  See description above.
     31  1.1  christos 
     32  1.1  christos proc test {} {
     33  1.1  christos     global binfile gdb_prompt
     34  1.1  christos 
     35  1.1  christos     # This test requires executing commands while the target is
     36  1.1  christos     # running, which, when testing with the remote target, requires
     37  1.1  christos     # non-stop remote protocol.  Until that variant of the RSP is the
     38  1.1  christos     # default, force target non-stop mode on.
     39  1.1  christos     set is_remote \
     40  1.1  christos 	[expr {[target_info exists gdb_protocol] \
     41  1.1  christos 		   && ([target_info gdb_protocol] == "remote" \
     42  1.1  christos 			   || [target_info gdb_protocol] == "extended-remote")}]
     43  1.1  christos 
     44  1.1  christos     save_vars { ::GDBFLAGS } {
     45  1.1  christos 	if {$is_remote} {
     46  1.1  christos 	    append ::GDBFLAGS " -ex \"maint set target-non-stop on\""
     47  1.1  christos 	}
     48  1.1  christos 	clean_restart ${binfile}
     49  1.1  christos     }
     50  1.1  christos 
     51  1.1  christos     set test_spawn_id [spawn_wait_for_attach $binfile]
     52  1.1  christos     set testpid [spawn_id_get_pid $test_spawn_id]
     53  1.1  christos 
     54  1.1  christos     set any "\[^\r\n\]*"
     55  1.1  christos 
     56  1.1  christos     # Iterate more than once so that we test re-attaching after
     57  1.1  christos     # detaching, in case GDB incorrectly detaches and the process
     58  1.1  christos     # crashes after the detach.
     59  1.1  christos     set n_iters 2
     60  1.1  christos     for {set iter 1} {$iter <= $n_iters} {incr iter} {
     61  1.1  christos 	with_test_prefix "iter=$iter" {
     62  1.1  christos 	    set attached 0
     63  1.1  christos 
     64  1.1  christos 	    gdb_test_multiple "attach $testpid" "attach" {
     65  1.1  christos 		-re "Attaching to program:${any}process $testpid\r\n.*$gdb_prompt " {
     66  1.1  christos 		    pass $gdb_test_name
     67  1.1  christos 		    set attached 1
     68  1.1  christos 		}
     69  1.1  christos 	    }
     70  1.1  christos 
     71  1.1  christos 	    if {!$attached} {
     72  1.1  christos 		break
     73  1.1  christos 	    }
     74  1.1  christos 
     75  1.1  christos 	    gdb_test_multiple "continue &" "" {
     76  1.1  christos 		-re "Continuing\.\r\n$::gdb_prompt " {
     77  1.1  christos 		    pass $gdb_test_name
     78  1.1  christos 		}
     79  1.1  christos 	    }
     80  1.1  christos 
     81  1.1  christos 	    gdb_test "detach" "Detaching from.*"
     82  1.1  christos 
     83  1.1  christos 	    # Sleep a bit before reattaching to let the detached
     84  1.1  christos 	    # process crash and exit if e.g., GDB managed to leave
     85  1.1  christos 	    # breakpoint traps behind.
     86  1.1  christos 	    if {$iter != $n_iters} {
     87  1.1  christos 		sleep 1
     88  1.1  christos 	    }
     89  1.1  christos 	}
     90  1.1  christos     }
     91  1.1  christos 
     92  1.1  christos     kill_wait_spawned_process $test_spawn_id
     93  1.1  christos }
     94  1.1  christos 
     95  1.1  christos test
     96