1# Copyright 2018-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# This test script tries to expose a bug in some of the uses of 17# waitpid in the Linux native support within GDB. The problem was 18# spotted on systems which were heavily loaded when attaching to 19# threaded test programs. What happened was that during the initial 20# attach, the loop of waitpid calls that normally received the stop 21# events from each of the threads in the inferior was not receiving a 22# stop event for some threads (the kernel just hadn't sent the stop 23# event yet). 24# 25# GDB would then trigger a call to stop_all_threads which would 26# continue to wait for all of the outstanding threads to stop, when 27# the outstanding stop events finally arrived GDB would then 28# (incorrectly) discard the stop event, resume the thread, and 29# continue to wait for the thread to stop.... which it now never 30# would. 31# 32# In order to try and expose this issue reliably, this test preloads a 33# library that intercepts waitpid calls. All waitpid calls targeting 34# pid -1 with the WNOHANG flag are rate limited so that only 1 per 35# second can complete. Additional calls are forced to return 0 36# indicating no event waiting. This is enough to trigger the bug 37# during the attach phase. 38 39# This test only works on Linux 40if { ![isnative] || [is_remote host] || [use_gdb_stub] 41 || ![istarget *-linux*] } { 42 return 43} 44 45standard_testfile 46 47set libfile slow-waitpid 48set libsrc "${srcdir}/${subdir}/${libfile}.c" 49set libobj [standard_output_file ${libfile}.so] 50 51with_test_prefix "compile preload library" { 52 # Compile the preload library. We only get away with this as we 53 # limit this test to running when ISNATIVE is true. 54 if { [gdb_compile_shlib_pthreads \ 55 $libsrc $libobj {debug}] != "" } then { 56 return -1 57 } 58} 59 60with_test_prefix "compile test executable" { 61 # Compile the test program 62 if { [gdb_compile_pthreads \ 63 "${srcdir}/${subdir}/${srcfile}" "${binfile}" \ 64 executable {debug}] != "" } { 65 return -1 66 } 67} 68 69# Spawn GDB with LIB preloaded with LD_PRELOAD. 70 71proc gdb_spawn_with_ld_preload {lib} { 72 global env 73 74 save_vars { env(LD_PRELOAD) env(ASAN_OPTIONS) } { 75 if { ![info exists env(LD_PRELOAD) ] 76 || $env(LD_PRELOAD) == "" } { 77 set env(LD_PRELOAD) "$lib" 78 } else { 79 append env(LD_PRELOAD) ":$lib" 80 } 81 82 # Prevent address sanitizer error: 83 # ASan runtime does not come first in initial library list; you should 84 # either link runtime to your application or manually preload it with 85 # LD_PRELOAD. 86 set_sanitizer_default ASAN_OPTIONS verify_asan_link_order 0 87 88 gdb_start 89 } 90} 91 92# Run test program in the background. 93set test_spawn_id [spawn_wait_for_attach $binfile] 94set testpid [spawn_id_get_pid $test_spawn_id] 95 96# Start GDB with preload library in place. 97if { [gdb_spawn_with_ld_preload $libobj] == -1 } { 98 # Make sure we get UNTESTED rather than UNRESOLVED. 99 set errcnt 0 100 untested "Couldn't start GDB with preloaded lib" 101 return -1 102} 103 104# Load binary, and attach to running program. 105gdb_load ${binfile} 106gdb_test "attach $testpid" "Attaching to program.*" "attach to target" 107 108gdb_exit 109 110# Kill of test program. 111kill_wait_spawned_process $test_spawn_id 112