Commit | Line | Data |
---|---|---|
c945a99f PA |
1 | # Copyright 2008-2015 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 | if {![can_spawn_for_attach]} { | |
25 | return 0 | |
26 | } | |
27 | ||
28 | standard_testfile | |
29 | ||
30 | # The test proper. See description above. | |
31 | ||
32 | proc test {} { | |
33 | global binfile | |
34 | global gdb_prompt | |
35 | global decimal | |
36 | ||
37 | clean_restart ${binfile} | |
38 | ||
39 | set testpid [spawn_wait_for_attach $binfile] | |
40 | ||
41 | set attempts 10 | |
42 | for {set attempt 1} { $attempt <= $attempts } { incr attempt } { | |
43 | with_test_prefix "iter $attempt" { | |
44 | set attached 0 | |
45 | set eperm 0 | |
46 | set test "attach" | |
47 | gdb_test_multiple "attach $testpid" $test { | |
48 | -re "new threads in iteration" { | |
49 | # Seen when "set debug libthread_db" is on. | |
50 | exp_continue | |
51 | } | |
52 | -re "warning: Cannot attach to lwp $decimal: Operation not permitted" { | |
53 | # On Linux, PTRACE_ATTACH sometimes fails with | |
54 | # EPERM, even though /proc/PID/status indicates | |
55 | # the thread is running. | |
56 | set eperm 1 | |
57 | exp_continue | |
58 | } | |
59 | -re "debugger service failed.*$gdb_prompt $" { | |
60 | fail $test | |
61 | } | |
62 | -re "$gdb_prompt $" { | |
63 | if {$eperm} { | |
64 | xfail "$test (EPERM)" | |
65 | } else { | |
66 | pass $test | |
67 | } | |
68 | } | |
69 | -re "Attaching to program.*process $testpid.*$gdb_prompt $" { | |
70 | pass $test | |
71 | } | |
72 | } | |
73 | ||
74 | # Sleep a bit and try updating the thread list. We should | |
75 | # know about all threads already at this point. If we see | |
76 | # "New Thread" or similar being output, then "attach" is | |
77 | # failing to actually attach to all threads in the process, | |
78 | # which would be a bug. | |
79 | sleep 1 | |
80 | ||
81 | set test "no new threads" | |
82 | gdb_test_multiple "info threads" $test { | |
83 | -re "New .*$gdb_prompt $" { | |
84 | fail $test | |
85 | } | |
86 | -re "$gdb_prompt $" { | |
87 | pass $test | |
88 | } | |
89 | } | |
90 | ||
91 | # Force breakpoints always inserted, so that threads we might | |
92 | # have failed to attach to hit them even when threads we do | |
93 | # know about are stopped. | |
94 | gdb_test_no_output "set breakpoint always-inserted on" | |
95 | ||
96 | # Run to a breakpoint a few times. A few threads should spawn | |
97 | # and die meanwhile. This checks that thread creation/death | |
98 | # events carry on correctly after attaching. Also, be | |
99 | # detaching from the program and reattaching, we check that | |
100 | # the program doesn't die due to gdb leaving a pending | |
101 | # breakpoint hit on a new thread unprocessed. | |
102 | gdb_test "break break_fn" "Breakpoint.*" "break break_fn" | |
103 | ||
104 | # Wait a bit, to give time for most threads to hit the | |
105 | # breakpoint, including threads we might have failed to | |
106 | # attach. | |
107 | sleep 2 | |
108 | ||
109 | set bps 3 | |
110 | for {set bp 1} { $bp <= $bps } { incr bp } { | |
111 | gdb_test "continue" "Breakpoint.*" "break at break_fn: $bp" | |
112 | } | |
113 | ||
114 | if {$attempt < $attempts} { | |
e584fdbc PA |
115 | # Kick the time out timer for another round. |
116 | gdb_test "print again = 1" " = 1" "reset timer in the inferior" | |
117 | # Show the time we had left in the logs, in case | |
118 | # something goes wrong. | |
119 | gdb_test "print seconds_left" " = .*" | |
120 | ||
c945a99f PA |
121 | gdb_test "detach" "Detaching from.*" |
122 | } else { | |
123 | gdb_test "kill" "" "kill process" "Kill the program being debugged.*y or n. $" "y" | |
124 | } | |
125 | ||
126 | gdb_test_no_output "set breakpoint always-inserted off" | |
127 | delete_breakpoints | |
128 | } | |
129 | } | |
130 | ||
131 | remote_exec target "kill -9 ${testpid}" | |
132 | } | |
133 | ||
e584fdbc PA |
134 | # The test program exits after a while, in case GDB crashes. Make it |
135 | # wait at least as long as we may wait before declaring a time out | |
136 | # failure. | |
137 | set options { "additional_flags=-DTIMEOUT=$timeout" debug pthreads } | |
138 | ||
139 | if {[prepare_for_testing "failed to prepare" $testfile $srcfile $options] == -1} { | |
c945a99f PA |
140 | return -1 |
141 | } | |
142 | ||
143 | test |