]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/attach-many-short-lived-threads.exp
cd752ca49691b280b8bcd3a3309f66ddca48d691
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / attach-many-short-lived-threads.exp
1 # Copyright 2008-2017 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 attaching to a program that is constantly spawning short-lived
17 # threads. The stresses the edge cases of attaching to threads that
18 # have just been created or are in process of dying. In addition, the
19 # test attaches, debugs, detaches, reattaches in a loop a few times,
20 # to stress the behavior of the debug API around detach (some systems
21 # end up leaving stale state behind that confuse the following
22 # attach).
23
24 # Return true if the running version of DejaGnu is known to not be
25 # able to run this test.
26 proc bad_dejagnu {} {
27 global frame_version
28
29 verbose -log "DejaGnu version: $frame_version"
30 verbose -log "Expect version: [exp_version]"
31 verbose -log "Tcl version: [info tclversion]"
32
33 set dj_ver [split $frame_version .]
34 set dj_ver_major [lindex $dj_ver 0]
35 set dj_ver_minor [lindex $dj_ver 1]
36
37 # DejaGnu versions prior to 1.6 manage to kill the wrong process
38 # due to PID-reuse races. Since this test spawns many threads, it
39 # widens the race window a whole lot, enough that the inferior is
40 # often killed, and thus the test randomly fails. See:
41 # http://lists.gnu.org/archive/html/dejagnu/2015-07/msg00005.html
42 # The fix added a close_wait_program procedure. If that procedure
43 # is defined, and DejaGnu is older than 1.6, assume that means the
44 # fix was backported.
45 if {$dj_ver_major == 1
46 && ($dj_ver_minor < 6 && [info procs close_wait_program] == "")} {
47 return 1
48 }
49
50 return 0
51 }
52
53 if {[bad_dejagnu]} {
54 unsupported "broken DejaGnu"
55 return 0
56 }
57
58 if {![can_spawn_for_attach]} {
59 return 0
60 }
61
62 standard_testfile
63
64 # The test proper. See description above.
65
66 proc test {} {
67 global binfile
68 global gdb_prompt
69 global decimal
70
71 clean_restart ${binfile}
72
73 set test_spawn_id [spawn_wait_for_attach $binfile]
74 set testpid [spawn_id_get_pid $test_spawn_id]
75
76 set attempts 10
77 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
78 with_test_prefix "iter $attempt" {
79 set attached 0
80 set eperm 0
81 set test "attach"
82 gdb_test_multiple "attach $testpid" $test {
83 -re "new threads in iteration" {
84 # Seen when "set debug libthread_db" is on.
85 exp_continue
86 }
87 -re "warning: Cannot attach to lwp $decimal: Operation not permitted" {
88 # On Linux, PTRACE_ATTACH sometimes fails with
89 # EPERM, even though /proc/PID/status indicates
90 # the thread is running.
91 set eperm 1
92 exp_continue
93 }
94 -re "debugger service failed.*$gdb_prompt $" {
95 fail $test
96 }
97 -re "$gdb_prompt $" {
98 if {$eperm} {
99 xfail "$test (EPERM)"
100 } else {
101 pass $test
102 }
103 }
104 -re "Attaching to program.*process $testpid.*$gdb_prompt $" {
105 pass $test
106 }
107 }
108
109 # Sleep a bit and try updating the thread list. We should
110 # know about all threads already at this point. If we see
111 # "New Thread" or similar being output, then "attach" is
112 # failing to actually attach to all threads in the process,
113 # which would be a bug.
114 sleep 1
115
116 set test "no new threads"
117 gdb_test_multiple "info threads" $test {
118 -re "New .*$gdb_prompt $" {
119 fail $test
120 }
121 -re "$gdb_prompt $" {
122 pass $test
123 }
124 }
125
126 # Force breakpoints always inserted, so that threads we might
127 # have failed to attach to hit them even when threads we do
128 # know about are stopped.
129 gdb_test_no_output "set breakpoint always-inserted on"
130
131 # Run to a breakpoint a few times. A few threads should spawn
132 # and die meanwhile. This checks that thread creation/death
133 # events carry on correctly after attaching. Also, be
134 # detaching from the program and reattaching, we check that
135 # the program doesn't die due to gdb leaving a pending
136 # breakpoint hit on a new thread unprocessed.
137 gdb_test "break break_fn" "Breakpoint.*" "break break_fn"
138
139 # Wait a bit, to give time for most threads to hit the
140 # breakpoint, including threads we might have failed to
141 # attach.
142 sleep 2
143
144 set bps 3
145 for {set bp 1} { $bp <= $bps } { incr bp } {
146 gdb_test "continue" "Breakpoint.*" "break at break_fn: $bp"
147 }
148
149 if {$attempt < $attempts} {
150 # Kick the time out timer for another round.
151 gdb_test "print again = 1" " = 1" "reset timer in the inferior"
152 # Show the time we had left in the logs, in case
153 # something goes wrong.
154 gdb_test "print seconds_left" " = .*"
155
156 gdb_test "detach" "Detaching from.*"
157 } else {
158 gdb_test "kill" "" "kill process" "Kill the program being debugged.*y or n. $" "y"
159 }
160
161 gdb_test_no_output "set breakpoint always-inserted off"
162 delete_breakpoints
163 }
164 }
165 kill_wait_spawned_process $test_spawn_id
166 }
167
168 # The test program exits after a while, in case GDB crashes. Make it
169 # wait at least as long as we may wait before declaring a time out
170 # failure.
171 set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads }
172
173 if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} {
174 return -1
175 }
176
177 test