aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads/linux-dp.exp
blob: 7deae8d3a9731da4b725db709299858091f0dc4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Copyright 1999, 2001, 2002, 2003 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

# Please email any bugs, comments, and/or additions to this file to:
# bug-gdb@gnu.org

#### Dining Philosophers, on LinuxThreads - Jim Blandy <jimb@cygnus.com>
####
#### At the moment, GDB's support for LinuxThreads is pretty
#### idiosyncratic --- GDB's output doesn't look much like the output
#### it produces for other thread implementations, messages appear at
#### different times, etc.  So these tests are specific to LinuxThreads.
####
#### However, if all goes well, Linux will soon have a libthread_db
#### interface, and GDB will manage it the same way it does other
#### libthread_db-based systems.  Then, we can adjust this file to
#### work with any such system.

### Other things we ought to test:
### stepping a thread while others are running
### killing and restarting
### quitting gracefully

if $tracelevel then {
	strace $tracelevel
}

set prms_id 0
set bug_id 0

# This only works with Linux configurations.
if ![istarget *-*-linux-gnu*] then {
    return
}

set testfile "linux-dp"
set srcfile ${testfile}.c
set binfile ${objdir}/${subdir}/${testfile}
if {[gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug libs=-lpthread}] != ""} {
    return -1
}

gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_load ${binfile}
send_gdb "set print sevenbit-strings\n" ; gdb_expect -re "$gdb_prompt $"
runto_main

# There should be no threads initially.
gdb_test "info threads" "" "info threads 1"

# Try stepping over the thread creation function.
gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: create philosopher"]
set expect_manager -1
for {set i 0} {$i < 5} {incr i} {
    gdb_continue_to_breakpoint "about to create philosopher: $i"
    send_gdb "next\n"
    gdb_expect {
	-re "\\\[New .*\\\].*\\\[New .*\\\].*$gdb_prompt $" {
	    # Two threads are created the first time in LinuxThreads,
	    # where the second is the manager thread.  In NPTL, there is none.
	    if {$i == 0} {
		set expect_manager 1
	    }
	    pass "create philosopher: $i"
	}
	-re "\\\[New .*\\\].*$gdb_prompt $" {
	    if {$i == 0} {
		set expect_manager 0
	    }
	    pass "create philosopher: $i"
	}
	-re "Program received signal.*(Unknown signal|SIGUSR|Real-time event).*$gdb_prompt $" {
	    # It would be nice if we could catch the message that GDB prints
	    # when it first notices that the thread library doesn't support
	    # debugging, or if we could explicitly ask GDB somehow.
	    unsupported "This GDB does not support threads on this system."
	    return -1
	}
	-re "$gdb_prompt $" {
	    # We used to fail here, but not all targets announce new
	    # threads as they are created.  For example, the GDB
	    # remote protocol target only finds out about threads when
	    # they actually report some event like a breakpoint hit,
	    # or when the user types 'info threads'.
	    unresolved "create philosopher: $i"
	}
	timeout {
	    fail "(timeout) create philosopher: $i"
	}
    }
}

set nthreads 6

# Run until there are some threads.
gdb_breakpoint [gdb_get_line_number "linuxthreads.exp: info threads 2"]
gdb_continue_to_breakpoint "main thread's sleep"
set info_threads_ptn ""
for {set i $nthreads} {$i > 0} {incr i -1} {
    append info_threads_ptn "$i Thread .*"
}
append info_threads_ptn "\[\r\n\]+$gdb_prompt $"
set info_threads_manager_ptn "[expr $nthreads + 1] Thread .*$info_threads_ptn"

gdb_test_multiple "info threads" "info threads 2" {
    -re "$info_threads_manager_ptn" {
	# We did see a manager thread.  Check that against what we expected.
	switch -exact -- $expect_manager {
	    -1 {
		# We weren't sure whether to expect a manager thread.
		pass "info threads 2"
	    }
	    1 {
		# We were expecting a manager thread.
		pass "info threads 2"
	    }
	    0 {
		# We were not expecting to see the manager thread.
		fail "info threads 2"
	    }
	}
	set expect_manager 1
	incr nthreads
    }
    -re "$info_threads_ptn" {
	# We did not see a manager thread.  Check that against what we
	# expected.
	switch -exact -- $expect_manager {
	    -1 {
		# We weren't sure whether to expect a manager thread.
		# Don't expect it from here on out.
		pass "info threads 2"
	    }
	    1 {
		# We were expecting a manager thread, but we didn't see one.
		fail "info threads 2"
	    }
	    0 {
		# We were not expecting to see the manager thread.
		pass "info threads 2"
	    }
	}
	set expect_manager 0
    }
}


# Try setting a thread-specific breakpoint.
gdb_breakpoint "print_philosopher thread 5"
gdb_continue_to_breakpoint "thread 5's print"
# When there is no debugging info available for the thread library,
# the backtrace entry for philosopher's caller looks like:
#    #1  0x4001c548 in pthread_create () from /lib/libpthread.so.0
# If you do have debug info, the output obviously depends more on the
# exact library in use; under NPTL, you get:
#    #2  0x0012b7fc in start_thread (arg=0x21) at pthread_create.c:264
gdb_test "where" "print_philosopher.*philosopher.* \(from .*libpthread\|at pthread_create\).*" \
	"first thread-specific breakpoint hit"

# Make sure it's catching the right thread.  Try hitting the
# breakpoint ten times, and make sure we don't get anyone else.
set only_five 1
for {set i 0} {$only_five > 0 && $i < 10} {incr i} {
    gdb_continue_to_breakpoint "thread 5's print, pass: $i"
    send_gdb "info threads\n"
    gdb_expect {
	-re "\\* 5 Thread .*  print_philosopher .*\r\n$gdb_prompt $" {
	    # Okay this time.
	}
	-re ".*$gdb_prompt $" {
	    set only_five 0
	}
	timeout { 
	    set only_five -1
	}
    }
}

set name "thread-specific breakpoint is thread-specific"
if {$only_five ==  1} { pass $name }
if {$only_five ==  0} { fail $name }
if {$only_five == -1} { fail "$name (timeout)" }


### Select a particular thread.
proc select_thread {thread} {
    global gdb_prompt

    send_gdb "thread $thread\n"
    gdb_expect {
	-re "\\\[Switching to thread .*\\\].*\r\n$gdb_prompt $" {
	    pass "selected thread: $thread"
	}
	-re "$gdb_prompt $" {
	    fail "selected thread: $thread"
	}
	timeout {
	    fail "selected thread: $thread (timeout)"
	}
    }
}

### Select THREAD, check for a plausible backtrace, and make sure
### we're actually selecting a different philosopher each time.
### Return true if the thread had a stack which was not only
### acceptable, but interesting.  SEEN should be an array in which
### SEEN(N) exists iff we have found philosopher number N before.

set main_seen 0
set manager_seen 0

proc check_philosopher_stack {thread seen_name} {
    global gdb_prompt
    upvar $seen_name seen
    global main_seen
    global expect_manager manager_seen

    set name "philosopher is distinct: $thread"
    set interesting 0

    select_thread $thread
    send_gdb "where\n"
    gdb_expect {
	-re ".* in philosopher \\(data=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
	    set data $expect_out(1,string)
	    if {[info exists seen($data)]} {
		fail $name
	    } else {
		pass $name
		set seen($data) yep
	    }
	    set interesting 1
	}
	-re ".* in __pthread_manager \\(.*$gdb_prompt $" {
	    if {$manager_seen == 1} {
		fail "manager thread is distinct: $thread"
	    } else {
		set manager_seen 1
		pass "manager thread is distinct: $thread"
	    }
	    set interesting 1
	}
	-re "pthread_start_thread.*\r\n$gdb_prompt $" {
	    ## Maybe the thread hasn't started yet.
	    pass $name
	}
	-re ".* in main \\(.*$gdb_prompt $" {
	    if {$main_seen == 1} {
		fail "main is distinct: $thread"
	    } else {
		set main_seen 1
		pass "main is distinct: $thread"
	    }
	    set interesting 1
	}
	-re " in \\?\\?.*\r\n$gdb_prompt $" {
	    ## Sometimes we can't get a backtrace.  I'm going to call
	    ## this a pass, since we do verify that at least one
	    ## thread was interesting, so we can get more consistent
	    ## test suite totals.  But in my heart, I think it should
	    ## be an xfail.
	    pass $name
	}
	-re "$gdb_prompt $" {
	    fail $name
	}
	timeout {
	    fail "$name (timeout)" 
	}
    }

    return $interesting
}

set any_interesting 0
array set seen {}
unset seen
for {set i 1} {$i <= $nthreads} {incr i} {
    if [check_philosopher_stack $i seen] {
	set any_interesting 1
    }
}

if {$any_interesting} {
    pass "found an interesting thread"
} else {
    fail "found an interesting thread"
}

if {$manager_seen == $expect_manager} {
    pass "manager thread found (not found) when expected"
} else {
    fail "manager thread found (not found) when expected"
}