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