* gdb.threads/thread-find.exp: Support remote targets.
[external/binutils.git] / gdb / testsuite / gdb.threads / thread-find.exp
1 # Copyright 2011
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 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@gnu.org
19
20 if $tracelevel then {
21         strace $tracelevel
22 }
23
24 set testfile "linux-dp"
25 set srcfile ${testfile}.c
26 set binfile ${objdir}/${subdir}/${testfile}
27 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != ""} {
28     return -1
29 }
30
31 gdb_start
32 gdb_reinitialize_dir $srcdir/$subdir
33 gdb_load ${binfile}
34 gdb_test_no_output "set print sevenbit-strings"
35 runto_main
36
37 # Run until there are some threads.
38 gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: info threads 2"]
39 gdb_continue_to_breakpoint "main thread's sleep"
40
41 # Make sure thread list is up-to-date (in case remote targets have not yet
42 # reported thread creation events)
43 gdb_test "info threads"
44
45 # Create thread names.
46 gdb_test "thread apply 1 thread name threadname_1" \
47     "Thread 1 .*" \
48     "name thread 1"
49
50 gdb_test "thread apply 2 thread name threadname_2" \
51     "Thread 2 .*" \
52     "name thread 2"
53
54 gdb_test "thread apply 3 thread name threadname_3" \
55     "Thread 3 .*" \
56     "name thread 3"
57
58 gdb_test "thread apply 4 thread name threadname_4" \
59     "Thread 4 .*" \
60     "name thread 4"
61
62 gdb_test "thread apply 5 thread name threadname_5" \
63     "Thread 5 .*" \
64     "name thread 5"
65
66 gdb_test "thread apply 6 thread name threadname_6" \
67     "Thread 6 .*" \
68     "name thread 6"
69
70 # Collect thread ids, if any.
71 gdb_test_multiple "info threads" "collect thread id" {
72     -re ". 6 .*\[Tt\]hread (\[0-9a-fA-Fx\]+).* \"threadname_6\" \[^\r\n\]*" {
73         set thread6 $expect_out(1,string)
74         exp_continue
75     }
76     -re ". 5 .*\[Tt\]hread (\[0-9a-fA-Fx\]+).* \"threadname_5\" \[^\r\n\]*" {
77         set thread5 $expect_out(1,string)
78         exp_continue
79     }
80     -re ". 4 .*\[Tt\]hread (\[0-9a-fA-Fx\]+).* \"threadname_4\" \[^\r\n\]*" {
81         set thread4 $expect_out(1,string)
82         exp_continue
83     }
84     -re ". 3 .*\[Tt\]hread (\[0-9a-fA-Fx\]+).* \"threadname_3\" \[^\r\n\]*" {
85         set thread3 $expect_out(1,string)
86         exp_continue
87     }
88     -re ". 2 .*\[Tt\]hread (\[0-9a-fA-Fx\]+).* \"threadname_2\" \[^\r\n\]*" {
89         set thread2 $expect_out(1,string)
90         exp_continue
91     }
92     -re ". 1 .*\[Tt\]hread (\[0-9a-fA-Fx\]+).* \"threadname_1\" \[^\r\n\]*" {
93         set thread1 $expect_out(1,string)
94         exp_continue
95     }
96     -re ".*$gdb_prompt $" {
97         pass "collect thread id"
98     }
99 }
100
101 if { [info exists thread6] } then {
102     gdb_test "echo $thread6\\n" "$thread6" "got thread ids"
103 }
104
105 # Collect process ids, if any.
106 gdb_test_multiple "info threads" "collect thread id" {
107     -re ". 6 .*\[Pp\]rocess (\[0-9a-fA-Fx\]+).* \"threadname_6\" \[^\r\n\]*" {
108         set process6 $expect_out(1,string)
109         exp_continue
110     }
111     -re ". 5 .*\[Pp\]rocess (\[0-9a-fA-Fx\]+).* \"threadname_5\" \[^\r\n\]*" {
112         set process5 $expect_out(1,string)
113         exp_continue
114     }
115     -re ". 4 .*\[Pp\]rocess (\[0-9a-fA-Fx\]+).* \"threadname_4\" \[^\r\n\]*" {
116         set process4 $expect_out(1,string)
117         exp_continue
118     }
119     -re ". 3 .*\[Pp\]rocess (\[0-9a-fA-Fx\]+).* \"threadname_3\" \[^\r\n\]*" {
120         set process3 $expect_out(1,string)
121         exp_continue
122     }
123     -re ". 2 .*\[Pp\]rocess (\[0-9a-fA-Fx\]+).* \"threadname_2\" \[^\r\n\]*" {
124         set process2 $expect_out(1,string)
125         exp_continue
126     }
127     -re ". 1 .*\[Pp\]rocess (\[0-9a-fA-Fx\]+).* \"threadname_1\" \[^\r\n\]*" {
128         set process1 $expect_out(1,string)
129         exp_continue
130     }
131     -re ".*$gdb_prompt $" {
132         pass "collect process id"
133     }
134 }
135
136 if { [info exists process6] } then {
137     gdb_test "echo $process6\\n" "$process6" "got process ids"
138 }
139
140 # Collect lwp ids, if any.
141 gdb_test_multiple "info threads" "collect thread id" {
142     -re ". 6 .*LWP (\[0-9a-fA-Fx\]+).* \"threadname_6\" \[^\r\n\]*" {
143         set lwp6 $expect_out(1,string)
144         exp_continue
145     }
146     -re ". 5 .*LWP (\[0-9a-fA-Fx\]+).* \"threadname_5\" \[^\r\n\]*" {
147         set lwp5 $expect_out(1,string)
148         exp_continue
149     }
150     -re ". 4 .*LWP (\[0-9a-fA-Fx\]+).* \"threadname_4\" \[^\r\n\]*" {
151         set lwp4 $expect_out(1,string)
152         exp_continue
153     }
154     -re ". 3 .*LWP (\[0-9a-fA-Fx\]+).* \"threadname_3\" \[^\r\n\]*" {
155         set lwp3 $expect_out(1,string)
156         exp_continue
157     }
158     -re ". 2 .*LWP (\[0-9a-fA-Fx\]+).* \"threadname_2\" \[^\r\n\]*" {
159         set lwp2 $expect_out(1,string)
160         exp_continue
161     }
162     -re ". 1 .*LWP (\[0-9a-fA-Fx\]+).* \"threadname_1\" \[^\r\n\]*" {
163         set lwp1 $expect_out(1,string)
164         exp_continue
165     }
166     -re ".*$gdb_prompt $" {
167         pass "collect lwp id"
168     }
169 }
170
171 if { [info exists lwp6] } then {
172     gdb_test "echo $lwp6\\n" "$lwp6" "got lwp ids"
173 }
174
175 #
176 # Now: test 'thread find' with names.
177 #
178
179 gdb_test "thread find threadname_6" \
180     "Thread 6 has name 'threadname_6'" "find thread name 6"
181 gdb_test "thread find threadname_5" \
182     "Thread 5 has name 'threadname_5'" "find thread name 5"
183 gdb_test "thread find threadname_4" \
184     "Thread 4 has name 'threadname_4'" "find thread name 4"
185 gdb_test "thread find threadname_3" \
186     "Thread 3 has name 'threadname_3'" "find thread name 3"
187 gdb_test "thread find threadname_2" \
188     "Thread 2 has name 'threadname_2'" "find thread name 2"
189 gdb_test "thread find threadname_1" \
190     "Thread 1 has name 'threadname_1'" "find thread name 1"
191
192 #
193 # Test 'thread find' with thread ids, if any.
194 #
195
196 if { [info exists thread6] } then {
197     gdb_test "thread find $thread6" \
198         "Thread 6 has .*$thread6.*" "find thread id 6"
199     gdb_test "thread find $thread5" \
200         "Thread 5 has .*$thread5.*" "find thread id 5"
201     gdb_test "thread find $thread4" \
202         "Thread 4 has .*$thread4.*" "find thread id 4"
203     gdb_test "thread find $thread3" \
204         "Thread 3 has .*$thread3.*" "find thread id 3"
205     gdb_test "thread find $thread2" \
206         "Thread 2 has .*$thread2.*" "find thread id 2"
207     gdb_test "thread find $thread1" \
208         "Thread 1 has .*$thread1.*" "find thread id 1"
209 }
210
211 #
212 # Test 'thread find' with process ids, if any.
213 #
214
215 if { [info exists process6] } then {
216     gdb_test "thread find $process6" \
217         "Thread 6 has .*$process6.*" "find process id 6"
218     gdb_test "thread find $process5" \
219         "Thread 5 has .*$process5.*" "find process id 5"
220     gdb_test "thread find $process4" \
221         "Thread 4 has .*$process4.*" "find process id 4"
222     gdb_test "thread find $process3" \
223         "Thread 3 has .*$process3.*" "find process id 3"
224     gdb_test "thread find $process2" \
225         "Thread 2 has .*$process2.*" "find process id 2"
226     gdb_test "thread find $process1" \
227         "Thread 1 has .*$process1.*" "find process id 1"
228 }
229
230 #
231 # Test 'thread find' with lwp ids, if any.
232 #
233
234 if { [info exists lwp6] } then {
235     gdb_test "thread find $lwp6" \
236         "Thread 6 has .*$lwp6.*" "find lwp id 6"
237     gdb_test "thread find $lwp5" \
238         "Thread 5 has .*$lwp5.*" "find lwp id 5"
239     gdb_test "thread find $lwp4" \
240         "Thread 4 has .*$lwp4.*" "find lwp id 4"
241     gdb_test "thread find $lwp3" \
242         "Thread 3 has .*$lwp3.*" "find lwp id 3"
243     gdb_test "thread find $lwp2" \
244         "Thread 2 has .*$lwp2.*" "find lwp id 2"
245     gdb_test "thread find $lwp1" \
246         "Thread 1 has .*$lwp1.*" "find lwp id 1"
247 }
248
249 # Test no match.
250
251 gdb_test "thread find foobarbaz" "No threads match .*" "no thread"
252
253 #
254 # Test regular expression
255 #
256
257 set see1 0
258 set see2 0
259 set see3 0
260 set see4 0
261 set see5 0
262 set see6 0
263
264 gdb_test_multiple "thread find threadname_\[345\]" "test regular exp" {
265     -re "Thread 6 has name \[^\r\n\]*" {
266         set see6 1
267         exp_continue
268     }
269     -re "Thread 5 has name \[^\r\n\]*" {
270         set see5 1
271         exp_continue
272     }
273     -re "Thread 4 has name \[^\r\n\]*" {
274         set see4 1
275         exp_continue
276     }
277     -re "Thread 3 has name \[^\r\n\]*" {
278         set see3 1
279         exp_continue
280     }
281     -re "Thread 2 has name \[^\r\n\]*" {
282         set see2 1
283         exp_continue
284     }
285     -re "Thread 1 has name \[^\r\n\]*" {
286         set see1 1
287         exp_continue
288     }
289     -re ".*$gdb_prompt $" {
290         if { $see3 && $see4 && $see5 && !$see1 && !$see2 && !$see6 } then {
291             pass "test regular exp"
292         } else {
293             fail "test regular exp"
294         }
295     }
296 }
297
298 #
299 # Test info threads on a subset of threads
300 #
301
302 set see1 0
303 set see2 0
304 set see3 0
305 set see4 0
306 set see5 0
307 set see6 0
308
309 gdb_test_multiple "info threads 2 4 6" "info threads 2 4 6" {
310     -re ". 6 \[^\r\n\]*\"threadname_6\" \[^\r\n\]*" {
311         set see6 1
312         exp_continue
313     }
314     -re ". 5 \[^\r\n\]*\"threadname_5\" \[^\r\n\]*" {
315         set see5 1
316         exp_continue
317     }
318     -re ". 4 \[^\r\n\]*\"threadname_4\" \[^\r\n\]*" {
319         set see4 1
320         exp_continue
321     }
322     -re ". 3 \[^\r\n\]*\"threadname_3\" \[^\r\n\]*" {
323         set see3 1
324         exp_continue
325     }
326     -re ". 2 \[^\r\n\]*\"threadname_2\" \[^\r\n\]*" {
327         set see2 1
328         exp_continue
329     }
330     -re ". 1 \[^\r\n\]*\"threadname_1\" \[^\r\n\]*" {
331         set see1 1
332         exp_continue
333     }
334     -re "$gdb_prompt $" {
335         if { $see2 && $see4 && $see6 && !$see1 && !$see3 && !$see5 } then {
336             pass "info threads 2 4 6"
337         } else {
338             fail "info threads 2 4 6"
339         }
340     }
341 }
342
343 #
344 # Test info threads on a range
345 #
346
347 set see1 0
348 set see2 0
349 set see3 0
350 set see4 0
351 set see5 0
352 set see6 0
353
354 gdb_test_multiple "info threads 3-5" "info threads 3-5" {
355     -re ". 6 .*\"threadname_6\" \[^\r\n\]*" {
356         set see6 1
357         exp_continue
358     }
359     -re ". 5 .*\"threadname_5\" \[^\r\n\]*" {
360         set see5 1
361         exp_continue
362     }
363     -re ". 4 .*\"threadname_4\" \[^\r\n\]*" {
364         set see4 1
365         exp_continue
366     }
367     -re ". 3 .*\"threadname_3\" \[^\r\n\]*" {
368         set see3 1
369         exp_continue
370     }
371     -re ". 2 .*\"threadname_2\" \[^\r\n\]*" {
372         set see2 1
373         exp_continue
374     }
375     -re ". 1 .*\"threadname_1\" \[^\r\n\]*" {
376         set see1 1
377         exp_continue
378     }
379     -re "$gdb_prompt $" {
380         if { $see3 && $see4 && $see5 && !$see1 && !$see2 && !$see6 } then {
381             pass "info threads 3-5"
382         } else {
383             fail "info threads 3-5"
384         }
385     }
386 }
387
388 # Test inverted range
389
390 gdb_test "info threads 5-3" "inverted range" "test inverted range"
391
392 # Test degenerate range
393
394 set see1 0
395 set see2 0
396 set see3 0
397 set see4 0
398 set see5 0
399 set see6 0
400
401 gdb_test_multiple "info threads 3-3" "info threads 3-3" {
402     -re ". 6 .*\"threadname_6\" \[^\r\n\]*" {
403         set see6 1
404         exp_continue
405     }
406     -re ". 5 .*\"threadname_5\" \[^\r\n\]*" {
407         set see5 1
408         exp_continue
409     }
410     -re ". 4 .*\"threadname_4\" \[^\r\n\]*" {
411         set see4 1
412         exp_continue
413     }
414     -re ". 3 .*\"threadname_3\" \[^\r\n\]*" {
415         set see3 1
416         exp_continue
417     }
418     -re ". 2 .*\"threadname_2\" \[^\r\n\]*" {
419         set see2 1
420         exp_continue
421     }
422     -re ". 1 .*\"threadname_1\" \[^\r\n\]*" {
423         set see1 1
424         exp_continue
425     }
426     -re ".*$gdb_prompt $" {
427         if { $see3 && !$see1 && !$see2 && !$see4 && !$see5 && !$see6 } then {
428             pass "info threads 3-3"
429         } else {
430             fail "info threads 3-3"
431         }
432     }
433 }
434
435 # Test bad input
436
437 gdb_test "info thread foo" \
438     "Args must be numbers or '.' variables." \
439     "info thread foo"
440
441 gdb_test "info thread foo -1" \
442     "Args must be numbers or '.' variables." \
443     "info thread foo -1"