1 # Copyright 2008-2015 Free Software Foundation, Inc.
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.
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.
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/>.
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
24 if {![can_spawn_for_attach]} {
30 # The test proper. See description above.
37 clean_restart ${binfile}
39 set testpid [spawn_wait_for_attach $binfile]
42 for {set attempt 1} { $attempt <= $attempts } { incr attempt } {
43 with_test_prefix "iter $attempt" {
47 gdb_test_multiple "attach $testpid" $test {
48 -re "new threads in iteration" {
49 # Seen when "set debug libthread_db" is on.
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.
59 -re "debugger service failed.*$gdb_prompt $" {
69 -re "Attaching to program.*process $testpid.*$gdb_prompt $" {
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.
81 set test "no new threads"
82 gdb_test_multiple "info threads" $test {
83 -re "New .*$gdb_prompt $" {
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"
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"
104 # Wait a bit, to give time for most threads to hit the
105 # breakpoint, including threads we might have failed to
110 for {set bp 1} { $bp <= $bps } { incr bp } {
111 gdb_test "continue" "Breakpoint.*" "break at break_fn: $bp"
114 if {$attempt < $attempts} {
115 gdb_test "detach" "Detaching from.*"
117 gdb_test "kill" "" "kill process" "Kill the program being debugged.*y or n. $" "y"
120 gdb_test_no_output "set breakpoint always-inserted off"
125 remote_exec target "kill -9 ${testpid}"
128 if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug pthreads}] == -1} {