]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/attach-many-short-lived-threads.exp
ccb5e9ba5777c77e5170f80fd50a29c8e519e726
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / attach-many-short-lived-threads.exp
1 # Copyright 2008-2016 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 test_spawn_id [spawn_wait_for_attach $binfile]
40 set testpid [spawn_id_get_pid $test_spawn_id]
41
42 set attempts 10
43 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
44 with_test_prefix "iter $attempt" {
45 set attached 0
46 set eperm 0
47 set test "attach"
48 gdb_test_multiple "attach $testpid" $test {
49 -re "new threads in iteration" {
50 # Seen when "set debug libthread_db" is on.
51 exp_continue
52 }
53 -re "warning: Cannot attach to lwp $decimal: Operation not permitted" {
54 # On Linux, PTRACE_ATTACH sometimes fails with
55 # EPERM, even though /proc/PID/status indicates
56 # the thread is running.
57 set eperm 1
58 exp_continue
59 }
60 -re "debugger service failed.*$gdb_prompt $" {
61 fail $test
62 }
63 -re "$gdb_prompt $" {
64 if {$eperm} {
65 xfail "$test (EPERM)"
66 } else {
67 pass $test
68 }
69 }
70 -re "Attaching to program.*process $testpid.*$gdb_prompt $" {
71 pass $test
72 }
73 }
74
75 # Sleep a bit and try updating the thread list. We should
76 # know about all threads already at this point. If we see
77 # "New Thread" or similar being output, then "attach" is
78 # failing to actually attach to all threads in the process,
79 # which would be a bug.
80 sleep 1
81
82 set test "no new threads"
83 gdb_test_multiple "info threads" $test {
84 -re "New .*$gdb_prompt $" {
85 fail $test
86 }
87 -re "$gdb_prompt $" {
88 pass $test
89 }
90 }
91
92 # Force breakpoints always inserted, so that threads we might
93 # have failed to attach to hit them even when threads we do
94 # know about are stopped.
95 gdb_test_no_output "set breakpoint always-inserted on"
96
97 # Run to a breakpoint a few times. A few threads should spawn
98 # and die meanwhile. This checks that thread creation/death
99 # events carry on correctly after attaching. Also, be
100 # detaching from the program and reattaching, we check that
101 # the program doesn't die due to gdb leaving a pending
102 # breakpoint hit on a new thread unprocessed.
103 gdb_test "break break_fn" "Breakpoint.*" "break break_fn"
104
105 # Wait a bit, to give time for most threads to hit the
106 # breakpoint, including threads we might have failed to
107 # attach.
108 sleep 2
109
110 set bps 3
111 for {set bp 1} { $bp <= $bps } { incr bp } {
112 gdb_test "continue" "Breakpoint.*" "break at break_fn: $bp"
113 }
114
115 if {$attempt < $attempts} {
116 # Kick the time out timer for another round.
117 gdb_test "print again = 1" " = 1" "reset timer in the inferior"
118 # Show the time we had left in the logs, in case
119 # something goes wrong.
120 gdb_test "print seconds_left" " = .*"
121
122 gdb_test "detach" "Detaching from.*"
123 } else {
124 gdb_test "kill" "" "kill process" "Kill the program being debugged.*y or n. $" "y"
125 }
126
127 gdb_test_no_output "set breakpoint always-inserted off"
128 delete_breakpoints
129 }
130 }
131 kill_wait_spawned_process $test_spawn_id
132 }
133
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} {
140 return -1
141 }
142
143 test