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