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