2008-05-01 Daniel Jacobowitz <dan@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 was created by modifying gdb.threads/attachstop.
18 # This file was created by Jan Kratochvil <jan.kratochvil@redhat.com>.
19 # Regression for: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=197584
20
21 # This test only works on Linux
22 if { ![isnative] || [is_remote host] || ![istarget *-linux*] } {
23 continue
24 }
25
26 set testfile "attachstop-mt"
27 set srcfile ${testfile}.c
28 set binfile ${objdir}/${subdir}/${testfile}
29 set escapedbinfile [string_to_regexp ${objdir}/${subdir}/${testfile}]
30
31 #execute_anywhere "rm -f ${binfile}"
32 remote_exec build "rm -f ${binfile}"
33 # For debugging this test
34 #
35 #log_user 1
36
37 # build the test case
38 #
39 if { [gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
40 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
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 # Do not: set testpid2 [expr $testpid + 1]
56 # as it will not exist on Red Hat 2.6.9-34.0.2.ELsmp
57 set testpid2 [expr $testpid + 2]
58
59 set status2 /proc/${testpid}/task/${testpid2}/status
60 if {[expr ! [file exists $status2]]} {
61 # kernel-2.4
62 set status2 /proc/${testpid2}/status
63 }
64
65 # Initial sanity test it is normally sleeping
66 set fileid0 [open $status2 r];
67 gets $fileid0 line1;
68 gets $fileid0 line2;
69 close $fileid0;
70
71 set test "attach0, initial sanity check of the sleeping state"
72 if {[string match "*(sleeping)*" $line2]} {
73 pass $test
74 } else {
75 fail $test
76 }
77
78 # Sttach and detach to test it will not become stopped
79 gdb_start
80 gdb_reinitialize_dir $srcdir/$subdir
81 gdb_load ${binfile}
82
83 set test "attach0 to sleeping"
84 gdb_test_multiple "attach $testpid" "$test" {
85 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
86 pass "$test"
87 }
88 }
89
90 gdb_test "gcore /dev/null" ".*aved corefile.*" "attach0 to sleeping gcore invocation"
91
92 gdb_test "thread 2" ".*witching to thread 2 .*" "attach0 to sleeping switch thread"
93
94 gdb_test "bt" ".*sleep.*func.*" "attach0 to sleeping bt"
95
96 # Exit and detach the process.
97
98 gdb_exit
99
100 # No race
101 sleep 2
102
103 # Check it did not get stopped by our gdb
104 set fileid1 [open $status2 r];
105 gets $fileid1 line1;
106 gets $fileid1 line2;
107 close $fileid1;
108
109 set test "attach1, post-gdb sanity check of the sleeping state - Red Hat BZ 197584"
110 if {[string match "*(sleeping)*" $line2]} {
111 pass $test
112 } else {
113 fail $test
114 }
115
116 # Stop the program
117 remote_exec build "kill -s STOP ${testpid}"
118
119 # No race
120 sleep 2
121
122 # Check it really got stopped by kill(1)
123 set fileid2 [open $status2 r];
124 gets $fileid2 line1;
125 gets $fileid2 line2;
126 close $fileid2;
127
128 set test "attach2, initial sanity check of the stopped state"
129 if {[string match "*(stopped)*" $line2]} {
130 pass $test
131 } else {
132 fail $test
133 }
134
135 # Start with clean gdb
136 gdb_start
137 gdb_reinitialize_dir $srcdir/$subdir
138 gdb_load ${binfile}
139
140 # Verify that we can attach to the process by first giving its
141 # executable name via the file command, and using attach with the
142 # process ID.
143
144 set test "set file, before attach3 to stopped process"
145 gdb_test_multiple "file $binfile" "$test" {
146 -re "Load new symbol table from.*y or n. $" {
147 gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
148 "$test (re-read)"
149 }
150 -re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
151 pass "$test"
152 }
153 }
154
155 set test "attach3 to stopped, after setting file"
156 gdb_test_multiple "attach $testpid" "$test" {
157 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
158 pass "$test"
159 }
160 }
161
162 # We may be already after the threads phase.
163 # `thread 2' command is important for the test to switch the current thread to
164 # a non-primary one for the detach process.
165
166 gdb_test "thread 2" ".*(witching to thread 2 |hread ID 2 not known).*" "attach3 to stopped switch thread"
167 gdb_test "bt" ".*sleep.*(func|main).*" "attach3 to stopped bt"
168
169 # Exit and detach the process.
170 gdb_exit
171
172 # Stop the program
173 remote_exec build "kill -s STOP ${testpid}"
174
175 # No race
176 sleep 2
177
178 # Continue the test as we would hit another expected bug regarding
179 # Program received signal SIGSTOP, Stopped (signal).
180 # across NPTL threads.
181
182 gdb_start
183 gdb_reinitialize_dir $srcdir/$subdir
184 gdb_load ${binfile}
185
186 # Verify that we can attach to the process just by giving the
187 # process ID.
188
189 set test "attach4 to stopped, after setting file"
190 gdb_test_multiple "attach $testpid" "$test" {
191 -re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*$gdb_prompt $" {
192 pass "$test"
193 }
194 }
195
196 # We may be already after the threads phase.
197 # `thread 2' command is important for the test to switch the current thread to
198 # a non-primary one for the detach process.
199
200 gdb_test "thread 2" ".*(witching to thread 2 |hread ID 2 not known).*" "attach4 to stopped switch thread"
201 gdb_test "bt" ".*sleep.*(func|main).*" "attach4 to stopped bt"
202
203 # RHEL3U8 kernel-2.4.21-47.EL will not return SIGINT but only shorten the sleep.
204 gdb_breakpoint [gdb_get_line_number "Ridiculous time"]
205 gdb_breakpoint [gdb_get_line_number "cut the sleep time"]
206 set test "attach4 continue"
207 send_gdb "continue\n"
208 gdb_expect {
209 -re "Continuing"
210 { pass "continue ($test)" }
211 timeout
212 { fail "continue ($test) (timeout)" }
213 }
214
215 # For this to work we must be sure to consume the "Continuing."
216 # message first, or GDB's signal handler may not be in place.
217 after 1000 {send_gdb "\003"}
218 set test "attach4 stop by interrupt"
219 gdb_expect {
220 -re "Program received signal SIGINT.*$gdb_prompt $"
221 {
222 pass $test
223 }
224 -re "Breakpoint \[0-9\].*$srcfile.*$gdb_prompt $"
225 {
226 pass $test
227 }
228 timeout
229 {
230 fail "$test (timeout)"
231 }
232 }
233
234 gdb_exit
235
236 # No race
237 sleep 2
238
239 # At this point, the process should be sleeping
240
241 set fileid4 [open $status2 r];
242 gets $fileid4 line1;
243 gets $fileid4 line2;
244 close $fileid4;
245
246 set test "attach4, exit leaves process sleeping"
247 if {[string match "*(sleeping)*" $line2]} {
248 pass $test
249 } else {
250 fail $test
251 }
252
253 # Make sure we don't leave a process around to confuse
254 # the next test run (and prevent the compile by keeping
255 # the text file busy), in case the "set should_exit" didn't
256 # work.
257
258 remote_exec build "kill -9 ${testpid}"
259
260 return 0
This page took 0.054339 seconds and 5 git commands to generate.