]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.threads/tls.exp
d7dce0dbf055415f0c9d4a261e6ebdb55a206a95
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / tls.exp
1 # tls.exp -- Expect script to test thread-local storage
2 # Copyright (C) 1992, 2003, 2007, 2008, 2009, 2010, 2011
3 # Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 set testfile tls
19 set testfile2 tls2
20 set srcfile ${testfile}.c
21 set srcfile2 ${testfile2}.c
22 set binfile ${objdir}/${subdir}/${testfile}
23
24 if [istarget "*-*-linux"] then {
25 set target_cflags "-D_MIT_POSIX_THREADS"
26 } else {
27 set target_cflags ""
28 }
29
30 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile2}" "${binfile}" executable [list c++ debug "incdir=${objdir}"]] != "" } {
31 return -1
32 }
33
34 ### Compute the value of the a_thread_local variable.
35 proc compute_expected_value {value} {
36 set expected_value 0
37 set i 0
38 while { $i <= $value} {
39 incr expected_value $i
40 incr i
41 }
42 return $expected_value
43 }
44
45 ### Get the value of the variable 'me' for the current thread.
46 proc get_me_variable {tnum} {
47 global expect_out
48 global gdb_prompt
49 global decimal
50
51 set value_of_me -1
52 send_gdb "print me\n"
53 gdb_expect {
54 -re ".*= ($decimal).*\r\n$gdb_prompt $" {
55 set value_of_me $expect_out(1,string)
56 pass "$tnum thread print me"
57 }
58 -re "$gdb_prompt $" {
59 fail "$tnum thread print me"
60 }
61 timeout {
62 fail "$tnum thread print me (timeout)"
63 }
64 }
65 return ${value_of_me}
66 }
67
68 ### Check the values of the thread local variables in the thread.
69 ### Also check that info address print the right things.
70 proc check_thread_local {number} {
71 set me_variable [get_me_variable $number]
72 set expected_value [compute_expected_value ${me_variable}]
73
74 gdb_test "p a_thread_local" \
75 "= $expected_value" \
76 "${number} thread local storage"
77
78 gdb_test "p K::another_thread_local" \
79 "= $me_variable" \
80 "${number} another thread local storage"
81
82 gdb_test "info address a_thread_local" \
83 ".*a_thread_local.*a thread-local variable at offset.*" \
84 "${number} info address a_thread_local"
85
86 gdb_test "info address K::another_thread_local" \
87 ".*another_thread_local.*a thread-local variable at offset.*" \
88 "${number} info address another_thread_local"
89 }
90
91 ### Select a particular thread.
92 proc select_thread {thread} {
93 global gdb_prompt
94
95 send_gdb "thread $thread\n"
96 gdb_expect {
97 -re "\\\[Switching to thread .*\\\].*\r\n$gdb_prompt $" {
98 pass "selected thread: $thread"
99 }
100 -re "$gdb_prompt $" {
101 fail "selected thread: $thread"
102 }
103 timeout {
104 fail "selected thread: $thread (timeout)"
105 }
106 }
107 }
108
109 ### Do a backtrace for the current thread, and check that the 'spin' routine
110 ### is in it. This means we have one of the threads we created, rather
111 ### than the main thread. Record the thread in the spin_threads
112 ### array. Also remember the level of the 'spin' routine in the backtrace, for
113 ### later use.
114 proc check_thread_stack {number spin_threads spin_threads_level} {
115 global gdb_prompt
116 global expect_out
117 global decimal
118 global hex
119 upvar $spin_threads tarr
120 upvar $spin_threads_level tarrl
121
122 select_thread $number
123 send_gdb "where\n"
124 gdb_expect {
125 -re ".*(\[0-9\]+)\[ \t\]+$hex in spin \\(vp=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
126 if {[info exists tarr($number)]} {
127 fail "backtrace of thread number $number in spin"
128 } else {
129 pass "backtrace of thread number $number in spin"
130 set level $expect_out(1,string)
131 set tarrl($number) $level
132 set tarr($number) 1
133 }
134 }
135 -re ".*$gdb_prompt $" {
136 set tarr($number) 0
137 set tarrl($number) 0
138 pass "backtrace of thread number $number not relevant"
139 }
140 timeout {
141 fail "backtrace of thread number $number (timeout)"
142 }
143 }
144 }
145
146 gdb_exit
147 gdb_start
148 gdb_reinitialize_dir $srcdir/$subdir
149
150 gdb_load ${binfile}
151 if ![runto_main] then {
152 fail "Can't run to main"
153 return 0
154 }
155
156 # Set a breakpoint at the "spin" routine to
157 # test the thread local's value.
158 #
159 gdb_test "b [gdb_get_line_number "here we know tls value"]" \
160 ".*Breakpoint 2.*tls.*" "set breakpoint at all threads"
161
162 # Set a bp at a point where we know all threads are alive.
163 #
164 gdb_test "b [gdb_get_line_number "still alive"]" \
165 ".*Breakpoint 3.*tls.*" "set breakpoint at synch point"
166
167 # Set a bp at the end to see if all threads are finished.
168 #
169 gdb_test "b [gdb_get_line_number "before exit"]" \
170 ".*Breakpoint 4.*tls.*" "set breakpoint at exit"
171
172 send_gdb "continue\n"
173 gdb_expect {
174 -re ".*Program received signal SIGSEGV.*a_thread_local = 0;.*$gdb_prompt $" {
175 # This is the first symptom if the gcc and binutils versions
176 # in use support TLS, but the system glibc does not.
177 unsupported "continue to first thread: system does not support TLS"
178 return -1
179 }
180 -re ".*$inferior_exited_re normally.*$gdb_prompt $" {
181 fail "continue to first thread: program runaway"
182 }
183 -re ".*Pass 0 done.*Pass 1 done.*$gdb_prompt $" {
184 fail "continue to first thread: program runaway 2"
185 }
186 -re ".*Breakpoint 2.*tls value.*$gdb_prompt $" {
187 pass "continue to first thread: get to thread"
188 }
189 -re ".*$gdb_prompt $" {
190 fail "continue to first thread: no progress?"
191 }
192 timeout { fail "continue to first thread (timeout)" }
193 }
194
195 gdb_test "info thread" ".*Thread.*spin.*" \
196 "at least one th in spin while stopped at first th"
197
198 check_thread_local "first"
199
200 gdb_test "continue" ".*Breakpoint 2.*tls value.*" "continue to second thread"
201 gdb_test "info thread" "Thread.*spin.*" \
202 "at least one th in spin while stopped at second th"
203
204 check_thread_local "second"
205
206 gdb_test "continue" ".*Breakpoint 2.*tls value.*" "continue to third thread"
207 gdb_test "info thread" ".*Thread.*spin.*" \
208 "at least one th in spin while stopped at third th"
209
210 check_thread_local "third"
211
212 gdb_test "continue" ".*Breakpoint 3.*still alive.*" "continue to synch point"
213
214 set no_of_threads 0
215 send_gdb "info thread\n"
216 gdb_expect {
217 -re "^info thread\[ \t\r\n\]+ *Id .*Frame\[ \t\r\n\]+(\[0-9\]+) *Thread.*$gdb_prompt $" {
218 set no_of_threads $expect_out(1,string)
219 pass "get number of threads"
220 }
221 -re "$gdb_prompt $" {
222 fail "get number of threads"
223 }
224 timeout {
225 fail "get number of threads (timeout)"
226 }
227 }
228
229 array set spin_threads {}
230 unset spin_threads
231 array set spin_threads_level {}
232 unset spin_threads_level
233
234 # For each thread check its backtrace to see if it is stopped at the
235 # spin routine.
236 for {set i 1} {$i <= $no_of_threads} {incr i} {
237 check_thread_stack $i spin_threads spin_threads_level
238 }
239
240 ### Loop through the threads and check the values of the tls variables.
241 ### keep track of how many threads we find in the spin routine.
242 set thrs_in_spin 0
243 foreach i [array names spin_threads] {
244 if {$spin_threads($i) == 1} {
245 incr thrs_in_spin
246 select_thread $i
247 set level $spin_threads_level($i)
248 # We expect to be in sem_wait, but if the thread has not yet
249 # been scheduled, we might be in sem_post still. We could be at
250 # any intermediate point in spin, too, but that is much less
251 # likely.
252 gdb_test "up $level" ".*spin.*sem_(wait|post).*" "thread $i up"
253 check_thread_local $i
254 }
255 }
256
257 if {$thrs_in_spin == 0} {
258 fail "No thread backtrace reported spin (vsyscall kernel problem?)"
259 }
260
261 gdb_test "continue" ".*Breakpoint 4.*before exit.*" "threads exited"
262
263 send_gdb "info thread\n"
264 gdb_expect {
265 -re ".* 1 *Thread.*2 *Thread.*$gdb_prompt $" {
266 fail "Too many threads left at end"
267 }
268 -re ".*\\\* 1 *Thread.*main.*$gdb_prompt $" {
269 pass "Expect only base thread at end"
270 }
271 -re ".*No stack.*$gdb_prompt $" {
272 fail "runaway at end"
273 }
274 -re ".*$gdb_prompt $" {
275 fail "mess at end"
276 }
277 timeout { fail "at end (timeout)" }
278 }
279
280 # Start over and do some "info address" stuff
281 #
282 runto spin
283
284 gdb_test "info address a_global" \
285 ".*a_global.*static storage at address.*" "info address a_global"
286
287 gdb_test "info address me" ".*me.*is a (complex DWARF expression:|variable).*" \
288 "info address me"
289
290
291 # Test LOC_UNRESOLVED references resolving for `extern' TLS variables.
292
293 gdb_test "p a_thread_local" " = \[0-9\]+"
294 # Here it could crash with: Cannot access memory at address 0x0
295 gdb_test "p file2_thread_local" " = \[0-9\]+"
296 # Depending on the current lookup scope we get LOC_UNRESOLVED or LOC_COMPUTED
297 # both printing:
298 # Symbol "file2_thread_local" is a thread-local variable at offset 8 in the thread-local storage for `.../gdb.threads/tls'.
299 gdb_test "info address file2_thread_local" "Symbol \"file2_thread_local\" is a thread-local variable.*"
300 # Here it could also crash with: Cannot access memory at address 0x0
301 gdb_test "p a_thread_local" " = \[0-9\]+" "p a_thread_local second time"
302 gdb_test "info address a_thread_local" "Symbol \"a_thread_local\" is a thread-local variable.*"
303
304 # Done!
305 #
306 gdb_exit
307
308 return 0