87dc868f9d48359ea4e43bd8213c62e50b4cf2e3
[external/binutils.git] / gdb / testsuite / gdb.threads / linux-dp.exp
1 # Copyright (C) 1999 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 2 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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@gnu.org
19
20 #### Dining Philosophers, on LinuxThreads - Jim Blandy <jimb@cygnus.com>
21 ####
22 #### At the moment, GDB's support for LinuxThreads is pretty
23 #### idiosyncratic --- GDB's output doesn't look much like the output
24 #### it produces for other thread implementations, messages appear at
25 #### different times, etc.  So these tests are specific to LinuxThreads.
26 ####
27 #### However, if all goes well, Linux will soon have a libthread_db
28 #### interface, and GDB will manage it the same way it does other
29 #### libthread_db-based systems.  Then, we can adjust this file to
30 #### work with any such system.
31
32 ### Other things we ought to test:
33 ### stepping a thread while others are running
34 ### killing and restarting
35 ### quitting gracefully
36
37 if $tracelevel then {
38         strace $tracelevel
39 }
40
41 set prms_id 0
42 set bug_id 0
43
44 # This only works with Linux configurations.
45 if ![istarget *-*-linux-gnu] then {
46     return
47 }
48
49 set testfile "linux-dp"
50 set srcfile ${testfile}.c
51 set binfile ${objdir}/${subdir}/${testfile}
52 if {[gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug libs=-lpthread}] != ""} {
53     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
54 }
55
56 gdb_start
57 gdb_reinitialize_dir $srcdir/$subdir
58 gdb_load ${binfile}
59 send_gdb "set print sevenbit-strings\n" ; gdb_expect -re "$gdb_prompt $"
60 runto_main
61
62 # There should be no threads initially.
63 gdb_test "info threads" "" "info threads 1"
64
65 # Try stepping over the thread creation function.
66 gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: create philosopher"]
67 for {set i 0} {$i < 5} {incr i} {
68     gdb_continue_to_breakpoint "about to create philosopher: $i"
69     gdb_test "next" "\\\[New Thread .*\\\].*" "create philosopher: $i"
70 }
71
72 # Run until there are some threads.
73 gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: info threads 2"]
74 gdb_continue_to_breakpoint "main thread's sleep"
75 gdb_test "info threads" "7 Thread .*6 Thread .*5 Thread .*4 Thread .*3 Thread .*2 Thread .* \\(initial thread\\)  main \\(argc=1, argv=.*\\) at .*linux-dp.c:.*1 Thread .* \\(manager thread\\).*" "info threads 2"
76
77 # Try setting a thread-specific breakpoint.
78 gdb_breakpoint "print_philosopher thread 5"
79 gdb_continue_to_breakpoint "thread 5's print"
80 gdb_test "where" "print_philosopher.*philosopher.*pthread_start_thread.*" \
81         "first thread-specific breakpoint hit"
82
83 # Make sure it's catching the right thread.  Try hitting the
84 # breakpoint ten times, and make sure we don't get anyone else.
85 set only_five 1
86 for {set i 0} {$only_five > 0 && $i < 10} {incr i} {
87     gdb_continue_to_breakpoint "thread 5's print, pass: $i"
88     send_gdb "info threads\n"
89     gdb_expect {
90         -re "\\* 5 Thread .*  print_philosopher .*\r\n$gdb_prompt $" {
91             # Okay this time.
92         }
93         -re ".*$gdb_prompt $" {
94             set only_five 0
95         }
96         timeout { 
97             set only_five -1
98         }
99     }
100 }
101
102 set name "thread-specific breakpoint is thread-specific"
103 if {$only_five ==  1} { pass $name }
104 if {$only_five ==  0} { fail $name }
105 if {$only_five == -1} { fail "$name (timeout)" }
106
107
108 ### Select a particular thread.
109 proc select_thread {thread} {
110     global gdb_prompt
111
112     send_gdb "thread $thread\n"
113     gdb_expect {
114         -re "\\\[Switching to thread .*\\\].*\r\n$gdb_prompt $" {
115             pass "selected thread: $thread"
116         }
117         -re "$gdb_prompt $" {
118             fail "selected thread: $thread"
119         }
120         timeout {
121             fail "selected thread: $thread (timeout)"
122         }
123     }
124 }
125
126 ### Select THREAD, check for a plausible backtrace, and make sure
127 ### we're actually selecting a different philosopher each time.
128 ### Return true if the thread had a stack which was not only
129 ### acceptable, but interesting.  SEEN should be an array in which
130 ### SEEN(N) exists iff we have found philosopher number N before.
131 proc check_philosopher_stack {thread seen_name} {
132     global gdb_prompt
133     upvar $seen_name seen
134
135     set name "philosopher is distinct: $thread"
136     set interesting 0
137
138     select_thread $thread
139     send_gdb "where\n"
140     gdb_expect {
141         -re ".* in philosopher \\(data=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
142             set data $expect_out(1,string)
143             if {[info exists seen($data)]} {
144                 fail $name
145             } else {
146                 pass $name
147                 set seen($data) yep
148             }
149             set interesting 1
150         }
151         -re "pthread_start_thread.*\r\n$gdb_prompt $" {
152             ## Maybe the thread hasn't started yet.
153             pass $name
154         }
155         -re " in \\?\\?.*\r\n$gdb_prompt $" {
156             ## Sometimes we can't get a backtrace.  I'm going to call
157             ## this a pass, since we do verify that at least one
158             ## thread was interesting, so we can get more consistent
159             ## test suite totals.  But in my heart, I think it should
160             ## be an xfail.
161             pass $name
162         }
163         -re "$gdb_prompt $" {
164             fail $name
165         }
166         timeout {
167             fail "$name (timeout)" 
168         }
169     }
170
171     return $interesting
172 }
173
174 set any_interesting 0
175 array set seen {}
176 for {set i 3} {$i <= 7} {incr i} {
177     if [check_philosopher_stack $i seen] {
178         set any_interesting 1
179     }
180 }
181
182 if {$any_interesting} {
183     pass "found an interesting thread"
184 } else {
185     fail "found an interesting thread"
186 }