]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/attach-slow-waitpid.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / attach-slow-waitpid.exp
1 # Copyright 2018-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 # 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
40 require !use_gdb_stub isnative
41 require {!is_remote host}
42 require {istarget *-linux*}
43
44 standard_testfile
45
46 set libfile slow-waitpid
47 set libsrc "${srcdir}/${subdir}/${libfile}.c"
48 set libobj [standard_output_file ${libfile}.so]
49
50 with_test_prefix "compile preload library" {
51 # Compile the preload library. We only get away with this as we
52 # limit this test to running when ISNATIVE is true.
53 if { [gdb_compile_shlib_pthreads \
54 $libsrc $libobj {debug}] != "" } then {
55 return -1
56 }
57 }
58
59 with_test_prefix "compile test executable" {
60 # Compile the test program
61 if { [gdb_compile_pthreads \
62 "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
63 executable {debug}] != "" } {
64 return -1
65 }
66 }
67
68 # Spawn GDB with LIB preloaded with LD_PRELOAD.
69
70 proc gdb_spawn_with_ld_preload {lib} {
71 global env
72
73 save_vars { env(LD_PRELOAD) env(ASAN_OPTIONS) } {
74 if { ![info exists env(LD_PRELOAD) ]
75 || $env(LD_PRELOAD) == "" } {
76 set env(LD_PRELOAD) "$lib"
77 } else {
78 append env(LD_PRELOAD) ":$lib"
79 }
80
81 # Prevent address sanitizer error:
82 # ASan runtime does not come first in initial library list; you should
83 # either link runtime to your application or manually preload it with
84 # LD_PRELOAD.
85 set_sanitizer_default ASAN_OPTIONS verify_asan_link_order 0
86
87 gdb_start
88 }
89 }
90
91 # Run test program in the background.
92 set test_spawn_id [spawn_wait_for_attach $binfile]
93 set testpid [spawn_id_get_pid $test_spawn_id]
94
95 # Start GDB with preload library in place.
96 if { [gdb_spawn_with_ld_preload $libobj] == -1 } {
97 # Make sure we get UNTESTED rather than UNRESOLVED.
98 set errcnt 0
99 untested "Couldn't start GDB with preloaded lib"
100 return -1
101 }
102
103 # Load binary, and attach to running program.
104 gdb_load ${binfile}
105 gdb_test "attach $testpid" "Attaching to program.*" "attach to target"
106
107 gdb_exit
108
109 # Kill of test program.
110 kill_wait_spawned_process $test_spawn_id