2008-12-31 Pedro Alves <pedro@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / attachstop-mt.exp
1 # Copyright 2008
2 # Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 # This test is based on gdb.base/attach.exp with modifications by Jeff Johnston
18 # <jjohnstn@redhat.com> and Jan Kratochvil <jan.kratochvil@redhat.com>.
19
20 # This test only works on Linux
21 if { ![isnative] || [is_remote host] || ![istarget *-linux*] } {
22 continue
23 }
24
25 set testfile "attachstop-mt"
26 set srcfile ${testfile}.c
27 set binfile ${objdir}/${subdir}/${testfile}
28 set escapedbinfile [string_to_regexp ${objdir}/${subdir}/${testfile}]
29
30 #execute_anywhere "rm -f ${binfile}"
31 remote_exec build "rm -f ${binfile}"
32 # For debugging this test
33 #
34 #log_user 1
35
36 # build the test case
37 #
38 if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
39 untested attachstop-mt.exp
40 return -1
41 }
42
43 if [get_compiler_info ${binfile}] {
44 return -1
45 }
46
47 # Start the program running and then wait for a bit, to be sure
48 # that it can be attached to.
49
50 set testpid [eval exec $binfile &]
51
52 # No race
53 sleep 2
54
55 # The testcase has three threads, find some other thread TID for $testpid2.
56 set tids [exec sh -c "echo /proc/$testpid/task/*"]
57 regsub -all /proc/$testpid/task/ $tids {} tids
58 if {$tids == "*"} {
59 unresolved "/proc/PID/task is not supported (kernel-2.4?)"
60 remote_exec build "kill -9 ${testpid}"
61 return -1
62 }
63 set tids [lsort -integer [split $tids]]
64 if {[llength $tids] != 3 || [lindex $tids 0] != $testpid} {
65 verbose -log "Invalid TIDs <$tids> for PID $testpid"
66 fail "Invalid TIDs found"
67 remote_exec build "kill -9 ${testpid}"
68 return -1
69 }
70 set testpid2 [lindex $tids 2]
71
72 # Initial sanity test it is normally sleeping
73 set status2 /proc/${testpid}/task/${testpid2}/status
74 set fileid0 [open $status2 r];
75 gets $fileid0 line1;
76 gets $fileid0 line2;
77 close $fileid0;
78
79 set test "attach0, initial sanity check of the sleeping state"
80 if {[string match "*(sleeping)*" $line2]} {
81 pass $test
82 } else {
83 fail $test
84 }
85
86 # Sttach and detach to test it will not become stopped
87 gdb_start
88 gdb_reinitialize_dir $srcdir/$subdir
89 gdb_load ${binfile}
90
91 set test "attach0 to sleeping"
92 gdb_test_multiple "attach $testpid" "$test" {
93 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
94 pass "$test"
95 }
96 }
97
98 gdb_test "gcore /dev/null" ".*aved corefile.*" "attach0 to sleeping gcore invocation"
99
100 gdb_test "thread 2" ".*witching to thread 2 .*" "attach0 to sleeping switch thread"
101
102 gdb_test "bt" ".*sleep.*func.*" "attach0 to sleeping bt"
103
104 # Exit and detach the process.
105
106 gdb_exit
107
108 # No race
109 sleep 2
110
111 # Check it did not get stopped by our gdb
112 set fileid1 [open $status2 r];
113 gets $fileid1 line1;
114 gets $fileid1 line2;
115 close $fileid1;
116
117 set test "attach1, post-gdb sanity check of the sleeping state - Red Hat BZ 197584"
118 if {[string match "*(sleeping)*" $line2]} {
119 pass $test
120 } else {
121 fail $test
122 }
123
124 # Stop the program
125 remote_exec build "kill -s STOP ${testpid}"
126
127 # No race
128 sleep 2
129
130 # Check it really got stopped by kill(1)
131 set fileid2 [open $status2 r];
132 gets $fileid2 line1;
133 gets $fileid2 line2;
134 close $fileid2;
135
136 set test "attach2, initial sanity check of the stopped state"
137 if {[string match "*(stopped)*" $line2]} {
138 pass $test
139 } else {
140 fail $test
141 }
142
143 # Start with clean gdb
144 gdb_start
145 gdb_reinitialize_dir $srcdir/$subdir
146 gdb_load ${binfile}
147
148 # Verify that we can attach to the process by first giving its
149 # executable name via the file command, and using attach with the
150 # process ID.
151
152 set test "set file, before attach3 to stopped process"
153 gdb_test_multiple "file $binfile" "$test" {
154 -re "Load new symbol table from.*y or n. $" {
155 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
156 "$test (re-read)"
157 }
158 -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
159 pass "$test"
160 }
161 }
162
163 set test "attach3 to stopped, after setting file"
164 gdb_test_multiple "attach $testpid" "$test" {
165 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
166 pass "$test"
167 }
168 }
169
170 # We may be already after the threads phase.
171 # `thread 2' command is important for the test to switch the current thread to
172 # a non-primary one for the detach process.
173
174 gdb_test "thread 2" ".*(witching to thread 2 |hread ID 2 not known).*" "attach3 to stopped switch thread"
175 gdb_test "bt" ".*sleep.*(func|main).*" "attach3 to stopped bt"
176
177 # Exit and detach the process.
178 gdb_exit
179
180 # Stop the program
181 remote_exec build "kill -s STOP ${testpid}"
182
183 # No race
184 sleep 2
185
186 # Continue the test as we would hit another expected bug regarding
187 # Program received signal SIGSTOP, Stopped (signal).
188 # across NPTL threads.
189
190 gdb_start
191 gdb_reinitialize_dir $srcdir/$subdir
192 gdb_load ${binfile}
193
194 # Verify that we can attach to the process just by giving the
195 # process ID.
196
197 set test "attach4 to stopped, after setting file"
198 gdb_test_multiple "attach $testpid" "$test" {
199 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
200 pass "$test"
201 }
202 }
203
204 # We may be already after the threads phase.
205 # `thread 2' command is important for the test to switch the current thread to
206 # a non-primary one for the detach process.
207
208 gdb_test "thread 2" ".*(witching to thread 2 |hread ID 2 not known).*" "attach4 to stopped switch thread"
209 gdb_test "bt" ".*sleep.*(func|main).*" "attach4 to stopped bt"
210
211 # RHEL3U8 kernel-2.4.21-47.EL will not return SIGINT but only shorten the sleep.
212 gdb_breakpoint [gdb_get_line_number "Ridiculous time"]
213 gdb_breakpoint [gdb_get_line_number "cut the sleep time"]
214 set test "attach4 continue"
215 send_gdb "continue\n"
216 gdb_expect {
217 -re "Continuing"
218 { pass "continue ($test)" }
219 timeout
220 { fail "continue ($test) (timeout)" }
221 }
222
223 # For this to work we must be sure to consume the "Continuing."
224 # message first, or GDB's signal handler may not be in place.
225 after 1000 {send_gdb "\003"}
226 set test "attach4 stop by interrupt"
227 gdb_expect {
228 -re "Program received signal SIGINT.*$gdb_prompt $"
229 {
230 pass $test
231 }
232 -re "Breakpoint \[0-9\].*$srcfile.*$gdb_prompt $"
233 {
234 pass $test
235 }
236 timeout
237 {
238 fail "$test (timeout)"
239 }
240 }
241
242 gdb_exit
243
244 # No race
245 sleep 2
246
247 # At this point, the process should be sleeping
248
249 set fileid4 [open $status2 r];
250 gets $fileid4 line1;
251 gets $fileid4 line2;
252 close $fileid4;
253
254 set test "attach4, exit leaves process sleeping"
255 if {[string match "*(sleeping)*" $line2]} {
256 pass $test
257 } else {
258 fail $test
259 }
260
261 # Make sure we don't leave a process around to confuse
262 # the next test run (and prevent the compile by keeping
263 # the text file busy), in case the "set should_exit" didn't
264 # work.
265
266 remote_exec build "kill -9 ${testpid}"
267
268 return 0
This page took 0.125074 seconds and 4 git commands to generate.