Home | History | Annotate | Line # | Download | only in gdb.base
run-attach-while-running.exp revision 1.1
      1  1.1  christos # Copyright 2021-2023 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 doing a "run" or an "attach" while the program is running.
     17  1.1  christos #
     18  1.1  christos # We test a non-threaded and a threaded configuration, so that targets
     19  1.1  christos # that don't support threads get some testing, but we also test with
     20  1.1  christos # threads when possible in case that triggers some
     21  1.1  christos # multi-thread-specific bugs.
     22  1.1  christos 
     23  1.1  christos standard_testfile
     24  1.1  christos 
     25  1.1  christos set binfile_threads ${binfile}-threads
     26  1.1  christos set binfile_nothreads ${binfile}-nothreads
     27  1.1  christos unset binfile
     28  1.1  christos 
     29  1.1  christos # Valid parameter / axis values:
     30  1.1  christos #
     31  1.1  christos #   - non-stop: "off" of "on"
     32  1.1  christos #   - threaded: 0 or 1
     33  1.1  christos #   - run-or-attach: "run" or "attach"
     34  1.1  christos 
     35  1.1  christos proc_with_prefix test { non-stop threaded run-or-attach } {
     36  1.1  christos     global gdb_prompt
     37  1.1  christos 
     38  1.1  christos     if { ${run-or-attach} == "attach" && ![can_spawn_for_attach] } {
     39  1.1  christos 	unsupported "attach not supported"
     40  1.1  christos 	return
     41  1.1  christos     }
     42  1.1  christos 
     43  1.1  christos     save_vars ::GDBFLAGS {
     44  1.1  christos 	set ::GDBFLAGS "$::GDBFLAGS -ex \"set non-stop ${non-stop}\""
     45  1.1  christos 
     46  1.1  christos 	# The test doesn't work when the remote target uses the
     47  1.1  christos 	# synchronous remote protocol, because GDB can't kill the
     48  1.1  christos 	# remote inferior while it is running, when we "run" or
     49  1.1  christos 	# "attach" again.  When aswering "yes" to the "Start it from
     50  1.1  christos 	# the beginning?" question, we otherwise get:
     51  1.1  christos 	#
     52  1.1  christos 	#   Cannot execute this command while the target is running.  Use the
     53  1.1  christos 	#   "interrupt" command to stop the target and then try again.
     54  1.1  christos 	#
     55  1.1  christos 	# Interrupting the target would defeat the purpose of the
     56  1.1  christos 	# test.  So when non-stop is off and using the remote target,
     57  1.1  christos 	# force the target to use the async / non-stop version of the
     58  1.1  christos 	# protocol.
     59  1.1  christos 	if { [target_info exists gdb_protocol] && ${non-stop} == "off" } {
     60  1.1  christos 	    set ::GDBFLAGS "$::GDBFLAGS -ex \"maint set target-non-stop on\""
     61  1.1  christos 	}
     62  1.1  christos 
     63  1.1  christos 	clean_restart $::binfile
     64  1.1  christos     }
     65  1.1  christos 
     66  1.1  christos     if { ![runto_main] } {
     67  1.1  christos 	return
     68  1.1  christos     }
     69  1.1  christos 
     70  1.1  christos     gdb_breakpoint "all_started" "temporary"
     71  1.1  christos     gdb_continue_to_breakpoint "continue to all_started"
     72  1.1  christos 
     73  1.1  christos     # If all-stop, everything stopped when we hit the all_started
     74  1.1  christos     # breakpoint, so resume execution in background.  If running the
     75  1.1  christos     # non-threaded version, our only thread is stopped in any case, so
     76  1.1  christos     # resume as well.  But if we are in non-stop with two threads, we
     77  1.1  christos     # have one running and one stopped, leave it like this, it makes
     78  1.1  christos     # an interesting test case.
     79  1.1  christos     if { ${non-stop} == "off" || !${threaded} } {
     80  1.1  christos 	gdb_test "continue &" "Continuing."
     81  1.1  christos     }
     82  1.1  christos 
     83  1.1  christos     gdb_test_no_output "set confirm off"
     84  1.1  christos 
     85  1.1  christos     # Run again (or, connect to a new stub if using a stub), take
     86  1.1  christos     # advantage of the fact that runto_main leaves the breakpoint on
     87  1.1  christos     # main in place.
     88  1.1  christos     if { ${run-or-attach} == "run" } {
     89  1.1  christos 	gdb_run_cmd
     90  1.1  christos 	gdb_test "" "Breakpoint $::decimal, .*main.*" "hit main breakpoint after re-run"
     91  1.1  christos     } elseif { ${run-or-attach} == "attach" } {
     92  1.1  christos 	set test_spawn_id [spawn_wait_for_attach $::binfile]
     93  1.1  christos 	set test_pid [spawn_id_get_pid $test_spawn_id]
     94  1.1  christos 
     95  1.1  christos 	gdb_test_multiple "attach $test_pid" "attach to process" {
     96  1.1  christos 	    -re "Attaching to program: .*$gdb_prompt " {
     97  1.1  christos 		pass $gdb_test_name
     98  1.1  christos 	    }
     99  1.1  christos 	}
    100  1.1  christos 
    101  1.1  christos 	gdb_exit
    102  1.1  christos 	kill_wait_spawned_process $test_spawn_id
    103  1.1  christos     } else {
    104  1.1  christos 	error "Invalid value for run-or-attach"
    105  1.1  christos     }
    106  1.1  christos }
    107  1.1  christos 
    108  1.1  christos foreach_with_prefix threaded {0 1} {
    109  1.1  christos     set options [list debug additional_flags=-DWITH_THREADS=$threaded \
    110  1.1  christos 		    additional_flags=-std=gnu99]
    111  1.1  christos     if { $threaded } {
    112  1.1  christos 	set binfile $binfile_threads
    113  1.1  christos 	lappend options pthreads
    114  1.1  christos     } else {
    115  1.1  christos 	set binfile $binfile_nothreads
    116  1.1  christos     }
    117  1.1  christos     if { [build_executable "failed to prepare" ${binfile} ${srcfile} $options] } {
    118  1.1  christos 	continue
    119  1.1  christos     }
    120  1.1  christos 
    121  1.1  christos     foreach_with_prefix run-or-attach {run attach} {
    122  1.1  christos 	foreach_with_prefix non-stop {off on} {
    123  1.1  christos 	    test ${non-stop} ${threaded} ${run-or-attach}
    124  1.1  christos 	}
    125  1.1  christos     }
    126  1.1  christos }
    127