aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads/schedlock.exp
blob: 8faee9fdb87bb27810fa443d109b38b4aad2a47f (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Copyright (C) 1996, 1997, 2002, 2003, 2007, 2008
# 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 3 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, see <http://www.gnu.org/licenses/>.

# Please email any bugs, comments, and/or additions to this file to:
# bug-gdb@prep.ai.mit.edu

# This file was written by Daniel Jacobowitz <drow@mvista.com>
# (parts based on pthreads.exp by Fred Fish (fnf@cygnus.com).
#
# This test covers the various forms of "set scheduler-locking".

if $tracelevel then {
	strace $tracelevel
}

set prms_id 0
set bug_id 0

set testfile "schedlock"
set srcfile ${testfile}.c
set binfile ${objdir}/${subdir}/${testfile}

# The number of threads, including the main thread.
set NUM 2

if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
    return -1
}

# Now we can proceed with the real testing.

proc get_args { } {
  global list_count
  global gdb_prompt
  global NUM

  set pattern "(\[0-9\]+)"
  for {set i 1} {[expr $i < $NUM]} {incr i} {
    append pattern ", (\[0-9\]+)"
  }

  send_gdb "print args\n"
  gdb_expect {
    -re "\\\$\[0-9\]+ = {$pattern}.*$gdb_prompt"
      {
	set list_count [expr $list_count + 1]
	pass "listed args ($list_count)"

	set result ""
	for {set i 1} {[expr $i <= $NUM]} {incr i} {
	  lappend result $expect_out($i,string)
	}
	return $result
      }
    -re "$gdb_prompt"
      {
	fail "listed args ($list_count) (unknown output)"
      }
    timeout
      {
	fail "listed args ($list_count) (timeout)"
      }
  }
}

proc stop_process { description } {
  global gdb_prompt

  # For this to work we must be sure to consume the "Continuing."
  # message first, or GDB's signal handler may not be in place.
  after 1000 {send_gdb "\003"}
  gdb_expect {
    -re "Program received signal SIGINT.*$gdb_prompt $"
      {
	pass $description
      }
    timeout
      {
	fail "$description (timeout)"
      }
  }
}

proc get_current_thread { description } {
  global gdb_prompt

  send_gdb "bt\n"
  gdb_expect {
    -re "thread_function \\(arg=0x(\[0-9\])\\).*$gdb_prompt $"
      {
	pass $description
	return $expect_out(1,string)
      }
    -re "$gdb_prompt $"
      {
	fail "$description (unknown output)"
      }
    timeout
      {
	fail "$description (timeout)"
      }
  }
}

proc my_continue { msg } {
  send_gdb "continue\n"
  gdb_expect {
    -re "Continuing"
      { pass "continue ($msg)" }
    timeout
      { fail "continue ($msg) (timeout)" }
  }

  stop_process "stop all threads ($msg)"

  # Make sure we're in one of the non-main looping threads.
  gdb_breakpoint [concat [gdb_get_line_number "schedlock.exp: main loop"] " if arg != 0"]
  gdb_continue_to_breakpoint "return to loop ($msg)"
  delete_breakpoints
}

proc step_ten_loops { msg } {
    global gdb_prompt

    for {set i 0} {[expr $i < 10]} {set i [expr $i + 1]} {
	send_gdb "step\n"
	set other_step 0
	gdb_expect {
	    -re ".*myp\\) \\+\\+;\[\r\n\]+$gdb_prompt $" {
		pass "step to increment ($msg $i)"
	    }
	    -re "$gdb_prompt $" {
		if {$other_step == 0} {
		    set other_step 1
		    send_gdb "step\n"
		    exp_continue
		} else {
		    fail "step to increment ($msg $i)"
		    # FIXME cascade?
		}
	    }
	    timeout {
		fail "step to increment ($msg $i) (timeout)"
	    }
	}
    }
}

# Start with a fresh gdb.

gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir

# We'll need this when we send_gdb a ^C to GDB.  Need to do it before we
# run the program and gdb starts saving and restoring tty states.
# On Ultrix, we don't need it and it is really slow (because shell_escape
# doesn't use vfork).
if ![istarget "*-*-ultrix*"] then {
    gdb_test "shell stty intr '^C'" ""
}

gdb_load ${binfile}

gdb_test "set print sevenbit-strings" ""
gdb_test "set width 0" ""

runto_main

# See if scheduler locking is available on this target.
send_gdb "set scheduler-locking off\n"
global gdb_prompt
gdb_expect {
  -re "Target .* cannot support this command"
    {
      unsupported "target does not support scheduler locking"
      return
    }
  -re "$gdb_prompt $"
    {
      pass "scheduler locking set to none"
    }
  timeout
    {
      unsupported "target does not support scheduler locking (timeout)"
      return
    }
}

gdb_breakpoint [gdb_get_line_number "schedlock.exp: last thread start"]
gdb_continue_to_breakpoint "all threads started"

global list_count
set list_count 0

set start_args [get_args]

# First make sure that all threads are alive.
my_continue "initial"

set cont_args [get_args]

set bad 0
for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
  if {[lindex $start_args $i] == [lindex $cont_args $i]} {
    incr bad
  }
}
if { $bad == 0 } {
  pass "all threads alive"
} else {
  fail "all threads alive ($bad/$NUM did not run)"
}

# We can't change threads, unfortunately, in current GDB.  Use
# whichever we stopped in.
set curthread [get_current_thread "find current thread (1)"]




# Test stepping without scheduler locking.
gdb_test "set scheduler-locking off"  ""

step_ten_loops "unlocked"

# Make sure we're still in the same thread.
set newthread [get_current_thread "find current thread (2)"]
if {$curthread == $newthread} {
    pass "step without lock does not change thread"
} else {
    fail "step without lock does not change thread (switched to thread $newthread)"
}

set start_args $cont_args
set cont_args [get_args]

set num_other_threads 0
for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
  if {[lindex $start_args $i] == [lindex $cont_args $i]} {
    if {$i == $curthread} {
      fail "current thread stepped (didn't run)"
    }
  } else {
    if {$i == $curthread} {
	if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
	    pass "current thread stepped"
	} else {
	    fail "current thread stepped (wrong amount)"
	}
    } else {
      set num_other_threads [expr $num_other_threads + 1]
    }
  }
}
if {$num_other_threads > 0} {
  pass "other threads ran - unlocked"
} else {
  fail "other threads ran - unlocked"
}

# Test continue with scheduler locking
gdb_test "set scheduler-locking on"  ""

my_continue "with lock"

# Make sure we're still in the same thread.
set newthread [get_current_thread "find current thread (3)"]
if {$curthread == $newthread} {
    pass "continue with lock does not change thread"
} else {
    fail "continue with lock does not change thread (switched to thread $newthread)"
}

set start_args $cont_args
set cont_args [get_args]

set num_other_threads 0
for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
  if {[lindex $start_args $i] == [lindex $cont_args $i]} {
    if {$i == $curthread} {
      fail "current thread ran (didn't run)"
    }
  } else {
    if {$i == $curthread} {
      pass "current thread ran"
    } else {
      incr num_other_threads
    }
  }
}
if {$num_other_threads > 0} {
  fail "other threads didn't run - locked"
} else {
  pass "other threads didn't run - locked"
}

# Test stepping with scheduler locking
step_ten_loops "locked"

# Make sure we're still in the same thread.
set newthread [get_current_thread "find current thread (2)"]
if {$curthread == $newthread} {
    pass "step with lock does not change thread"
} else {
    fail "step with lock does not change thread (switched to thread $newthread)"
}

set start_args $cont_args
set cont_args [get_args]

set num_other_threads 0
for {set i 0} {[expr $i < $NUM]} {set i [expr $i + 1]} {
  if {[lindex $start_args $i] == [lindex $cont_args $i]} {
    if {$i == $curthread} {
      fail "current thread stepped locked (didn't run)"
    }
  } else {
    if {$i == $curthread} {
	if {[lindex $start_args $i] == [expr [lindex $cont_args $i] - 10]} {
	    pass "current thread stepped locked"
	} else {
	    fail "current thread stepped locked (wrong amount)"
	}
    } else {
      incr num_other_threads
    }
  }
}
if {$num_other_threads > 0} {
  fail "other threads didn't run - step locked"
} else {
  pass "other threads didn't run - step locked"
}

return 0