Home | History | Annotate | Line # | Download | only in gdb.threads
access-mem-running-thread-exit.exp revision 1.1.1.1.4.2
      1 # Copyright (C) 2021-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 # Test that we can access memory while all the threads of the inferior
     17 # are running, and even if:
     18 #
     19 # - the leader thread exits
     20 # - the selected thread exits
     21 #
     22 # This test constantly spawns short lived threads to make sure that on
     23 # systems with debug APIs that require passing down a specific thread
     24 # to work with (e.g., GNU/Linux ptrace and /proc filesystem), GDB
     25 # copes with accessing memory just while the thread it is accessing
     26 # memory through exits.
     27 #
     28 # The test spawns two processes and alternates memory accesses between
     29 # them to force flushing per-process caches.  When the testcase was
     30 # originally written, the Linux backend would access inferior memory
     31 # via /proc/PID/mem, and kept one such file open, as a cache.
     32 # Alternating inferiors would force re-opening such file for a
     33 # different process, which would fail if GDB tried to open the file
     34 # for a thread that exited.  The test thus ensured those reopen/fail
     35 # code paths were exercised.  Nowadays, GDB keeps one /proc/PID/mem
     36 # file open per inferior.
     37 
     38 standard_testfile
     39 
     40 if {[build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {
     41     return -1
     42 }
     43 
     44 # The test proper.  NON_STOP indicates whether we're testing in
     45 # non-stop, or all-stop mode.
     46 
     47 proc test { non_stop } {
     48     global binfile
     49     global gdb_prompt
     50     global GDBFLAGS
     51 
     52     save_vars { GDBFLAGS } {
     53       append GDBFLAGS " -ex \"set non-stop $non_stop\""
     54       clean_restart ${binfile}
     55     }
     56 
     57     if ![runto_main] {
     58 	return -1
     59     }
     60 
     61     # If debugging with target remote, check whether the all-stop variant
     62     # of the RSP is being used.  If so, we can't run the background tests.
     63     if {!$non_stop
     64 	&& [target_info exists gdb_protocol]
     65 	&& ([target_info gdb_protocol] == "remote"
     66 	    || [target_info gdb_protocol] == "extended-remote")} {
     67 
     68 	gdb_test_multiple "maint show target-non-stop" "" {
     69 	    -wrap -re "(is|currently) on.*" {
     70 	    }
     71 	    -wrap -re "(is|currently) off.*" {
     72 		unsupported "can't issue commands while target is running"
     73 		return 0
     74 	    }
     75 	}
     76     }
     77 
     78     delete_breakpoints
     79 
     80     # Start the second inferior.
     81     with_test_prefix "second inferior" {
     82 	# With stub targets that do reload on run, if we let the new
     83 	# inferior share inferior 1's connection, runto_main would
     84 	# fail because GDB is already connected to something, like
     85 	# e.g. with --target_board=native-gdbserver:
     86 	#
     87 	#  (gdb) kill
     88 	#  ...
     89 	#  (gdb) target remote localhost:2348
     90 	#  Already connected to a remote target.  Disconnect? (y or n)
     91 	#
     92 	# Instead, start the inferior with no connection, and let
     93 	# gdb_load/runto_main spawn a new remote connection/gdbserver.
     94 	#
     95 	# OTOH, with extended-remote, we must let the new inferior
     96 	# reuse the current connection, so that runto_main below can
     97 	# issue the "run" command, and have the inferior run on the
     98 	# remote target.  If we forced no connection, then "run" would
     99 	# either fail if "set auto-connect-native-target" is on, like
    100 	# the native-extended-gdbserver board enforces, or it would
    101 	# run the inferior on the native target, which isn't what is
    102 	# being tested.
    103 	#
    104 	# Since it's reload_on_run targets that need special care, we
    105 	# default to reusing the connection on most targets.
    106 	if [target_info exists gdb,do_reload_on_run] {
    107 	    gdb_test "add-inferior -no-connection" "New inferior 2.*"
    108 	} else {
    109 	    gdb_test "add-inferior" "New inferior 2.*"
    110 	}
    111 	gdb_test "inferior 2" "Switching to inferior 2 .*"
    112 
    113 	gdb_load $binfile
    114 
    115 	if ![runto_main] {
    116 	    return -1
    117 	}
    118     }
    119 
    120     delete_breakpoints
    121 
    122     # These put too much noise in the logs.
    123     gdb_test_no_output "set print thread-events off"
    124 
    125     # Continue all threads of both processes.
    126     gdb_test_no_output "set schedule-multiple on"
    127     if {$non_stop == "off"} {
    128 	set cmd "continue &"
    129     } else {
    130 	set cmd "continue -a &"
    131     }
    132     gdb_test_multiple $cmd "continuing" {
    133 	-re "Continuing\.\r\n$gdb_prompt " {
    134 	    pass $gdb_test_name
    135 	}
    136     }
    137 
    138     # Like gdb_test, but:
    139     # - don't issue a pass on success.
    140     # - on failure, clear the ok variable in the calling context, and
    141     #   break it.
    142     proc my_gdb_test {cmd pattern message} {
    143 	upvar inf inf
    144 	upvar iter iter
    145 	if {[gdb_test -nopass \
    146 		 $cmd $pattern "access mem ($message, inf=$inf, iter=$iter)"] \
    147 		!= 0} {
    148 	    uplevel 1 {set ok 0}
    149 	    return -code break
    150 	}
    151     }
    152 
    153     # Hammer away for 5 seconds, alternating between inferiors.
    154     set ::done 0
    155     after 5000 { set ::done 1 }
    156 
    157     set inf 1
    158     set ok 1
    159     set iter 0
    160     while {!$::done && $ok} {
    161 	incr iter
    162 	verbose -log "xxxxx: iteration $iter"
    163 	gdb_test -nopass "info threads"
    164 
    165 	if {$inf == 1} {
    166 	    set inf 2
    167 	} else {
    168 	    set inf 1
    169 	}
    170 
    171 	my_gdb_test "inferior $inf" ".*" "inferior $inf"
    172 
    173 	my_gdb_test "print global_var = 555" " = 555" \
    174 	    "write to global_var"
    175 	my_gdb_test "print global_var" " = 555" \
    176 	    "print global_var after writing"
    177 	my_gdb_test "print global_var = 333" " = 333" \
    178 	    "write to global_var again"
    179 	my_gdb_test "print global_var" " = 333" \
    180 	    "print global_var after writing again"
    181     }
    182 
    183    if {$ok} {
    184        pass "access mem"
    185    }
    186 }
    187 
    188 foreach non_stop { "off" "on" } {
    189     set stop_mode [expr ($non_stop=="off")?"all-stop":"non-stop"]
    190     with_test_prefix "$stop_mode" {
    191 	test $non_stop
    192     }
    193 }
    194