]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.threads/tls.exp
gdb/testsuite: resolve duplicate test names in gdb.threads/tls.exp
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.threads / tls.exp
CommitLineData
8bc2021f 1# tls.exp -- Expect script to test thread-local storage
4a94e368 2# Copyright (C) 1992-2022 Free Software Foundation, Inc.
8bc2021f
EZ
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
e22f8b7c 6# the Free Software Foundation; either version 3 of the License, or
8bc2021f 7# (at your option) any later version.
e22f8b7c 8#
8bc2021f
EZ
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.
e22f8b7c 13#
8bc2021f 14# You should have received a copy of the GNU General Public License
e22f8b7c 15# along with this program. If not, see <http://www.gnu.org/licenses/>. */
8bc2021f 16
0b31a4bc
TT
17load_lib gdb-python.exp
18
0efbbabc 19standard_testfile tls.c tls2.c
8bc2021f
EZ
20
21if [istarget "*-*-linux"] then {
22 set target_cflags "-D_MIT_POSIX_THREADS"
23} else {
24 set target_cflags ""
25}
26
0efbbabc 27if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile2}" "${binfile}" executable [list c++ debug]] != "" } {
8bc2021f
EZ
28 return -1
29}
30
31### Compute the value of the a_thread_local variable.
32proc 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.
43proc 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.
67proc 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
b1054b67 75 if {!$::has_python_support} {
0b31a4bc
TT
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
515ed532 86 gdb_test "p K::another_thread_local" \
8bc2021f
EZ
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
515ed532 94 gdb_test "info address K::another_thread_local" \
8bc2021f
EZ
95 ".*another_thread_local.*a thread-local variable at offset.*" \
96 "${number} info address another_thread_local"
97}
98
99### Select a particular thread.
100proc 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.
122proc 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
0ad0e70c
TV
130 with_test_prefix "thread stack" {
131 select_thread $number
132 }
8bc2021f
EZ
133 send_gdb "where\n"
134 gdb_expect {
135 -re ".*(\[0-9\]+)\[ \t\]+$hex in spin \\(vp=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
136 if {[info exists tarr($number)]} {
137 fail "backtrace of thread number $number in spin"
138 } else {
139 pass "backtrace of thread number $number in spin"
140 set level $expect_out(1,string)
141 set tarrl($number) $level
142 set tarr($number) 1
143 }
144 }
145 -re ".*$gdb_prompt $" {
146 set tarr($number) 0
147 set tarrl($number) 0
148 pass "backtrace of thread number $number not relevant"
149 }
150 timeout {
151 fail "backtrace of thread number $number (timeout)"
152 }
153 }
154}
155
0efbbabc 156clean_restart ${binfile}
0b31a4bc 157
b1054b67
AB
158# Set this to avoid calling skip_python_tests repeatedly.
159set has_python_support [skip_python_tests]
160
317d2668
TV
161gdb_test_multiple "print a_thread_local" "" {
162 -re -wrap "Cannot find thread-local variables on this target" {
163 kfail "gdb/25807" $gdb_test_name
164 }
165 -re -wrap "Cannot read .a_thread_local. without registers" {
166 pass $gdb_test_name
167 }
168}
0b31a4bc 169
8bc2021f 170if ![runto_main] then {
8bc2021f
EZ
171 return 0
172}
173
174# Set a breakpoint at the "spin" routine to
175# test the thread local's value.
176#
177gdb_test "b [gdb_get_line_number "here we know tls value"]" \
178 ".*Breakpoint 2.*tls.*" "set breakpoint at all threads"
179
180# Set a bp at a point where we know all threads are alive.
181#
182gdb_test "b [gdb_get_line_number "still alive"]" \
183 ".*Breakpoint 3.*tls.*" "set breakpoint at synch point"
184
185# Set a bp at the end to see if all threads are finished.
186#
187gdb_test "b [gdb_get_line_number "before exit"]" \
188 ".*Breakpoint 4.*tls.*" "set breakpoint at exit"
189
190send_gdb "continue\n"
191gdb_expect {
00dbd492 192 -re ".* received signal SIGSEGV.*a_thread_local = 0;.*$gdb_prompt $" {
4ee6625b
DJ
193 # This is the first symptom if the gcc and binutils versions
194 # in use support TLS, but the system glibc does not.
195 unsupported "continue to first thread: system does not support TLS"
196 return -1
197 }
fda326dd 198 -re ".*$inferior_exited_re normally.*$gdb_prompt $" {
8bc2021f
EZ
199 fail "continue to first thread: program runaway"
200 }
201 -re ".*Pass 0 done.*Pass 1 done.*$gdb_prompt $" {
202 fail "continue to first thread: program runaway 2"
203 }
204 -re ".*Breakpoint 2.*tls value.*$gdb_prompt $" {
205 pass "continue to first thread: get to thread"
206 }
207 -re ".*$gdb_prompt $" {
208 fail "continue to first thread: no progress?"
209 }
210 timeout { fail "continue to first thread (timeout)" }
211}
212
213gdb_test "info thread" ".*Thread.*spin.*" \
214 "at least one th in spin while stopped at first th"
215
216check_thread_local "first"
217
218gdb_test "continue" ".*Breakpoint 2.*tls value.*" "continue to second thread"
219gdb_test "info thread" "Thread.*spin.*" \
220 "at least one th in spin while stopped at second th"
221
222check_thread_local "second"
223
224gdb_test "continue" ".*Breakpoint 2.*tls value.*" "continue to third thread"
225gdb_test "info thread" ".*Thread.*spin.*" \
226 "at least one th in spin while stopped at third th"
227
228check_thread_local "third"
229
230gdb_test "continue" ".*Breakpoint 3.*still alive.*" "continue to synch point"
231
51514e06 232set no_of_threads 0
8bc2021f
EZ
233send_gdb "info thread\n"
234gdb_expect {
b05b1202 235 -re "^info thread\[ \t\r\n\]+ *Id .*Frame\[ \t\r\n\]+.*(\[0-9\]+) *Thread\[^\r\n\]+\r\n$gdb_prompt $" {
8bc2021f
EZ
236 set no_of_threads $expect_out(1,string)
237 pass "get number of threads"
238 }
239 -re "$gdb_prompt $" {
240 fail "get number of threads"
241 }
242 timeout {
243 fail "get number of threads (timeout)"
244 }
245}
246
247array set spin_threads {}
248unset spin_threads
249array set spin_threads_level {}
250unset spin_threads_level
251
252# For each thread check its backtrace to see if it is stopped at the
253# spin routine.
254for {set i 1} {$i <= $no_of_threads} {incr i} {
255 check_thread_stack $i spin_threads spin_threads_level
256}
257
258### Loop through the threads and check the values of the tls variables.
259### keep track of how many threads we find in the spin routine.
260set thrs_in_spin 0
261foreach i [array names spin_threads] {
262 if {$spin_threads($i) == 1} {
263 incr thrs_in_spin
0ad0e70c
TV
264
265 with_test_prefix "spin_threads" {
266 select_thread $i
267 }
8bc2021f 268 set level $spin_threads_level($i)
61a771aa
DJ
269 # We expect to be in sem_wait, but if the thread has not yet
270 # been scheduled, we might be in sem_post still. We could be at
271 # any intermediate point in spin, too, but that is much less
272 # likely.
273 gdb_test "up $level" ".*spin.*sem_(wait|post).*" "thread $i up"
8bc2021f
EZ
274 check_thread_local $i
275 }
276}
277
278if {$thrs_in_spin == 0} {
bc6c7af4 279 fail "no thread backtrace reported spin (vsyscall kernel problem?)"
8bc2021f
EZ
280}
281
282gdb_test "continue" ".*Breakpoint 4.*before exit.*" "threads exited"
283
284send_gdb "info thread\n"
285gdb_expect {
a7658b96 286 -re ".* 1 *Thread.*2 *Thread.*$gdb_prompt $" {
bc6c7af4 287 fail "too many threads left at end"
8bc2021f 288 }
a7658b96 289 -re ".*\\\* 1 *Thread.*main.*$gdb_prompt $" {
bc6c7af4 290 pass "expect only base thread at end"
8bc2021f
EZ
291 }
292 -re ".*No stack.*$gdb_prompt $" {
293 fail "runaway at end"
294 }
295 -re ".*$gdb_prompt $" {
296 fail "mess at end"
297 }
298 timeout { fail "at end (timeout)" }
299}
300
301# Start over and do some "info address" stuff
302#
303runto spin
304
305gdb_test "info address a_global" \
d1e36019 306 ".*a_global.*static storage at address.*"
8bc2021f 307
d1e36019 308gdb_test "info address me" ".*me.*is a (complex DWARF expression:|variable).*"
8bc2021f 309
e0740f77
JK
310
311# Test LOC_UNRESOLVED references resolving for `extern' TLS variables.
312
313gdb_test "p a_thread_local" " = \[0-9\]+"
314# Here it could crash with: Cannot access memory at address 0x0
315gdb_test "p file2_thread_local" " = \[0-9\]+"
316# Depending on the current lookup scope we get LOC_UNRESOLVED or LOC_COMPUTED
317# both printing:
318# Symbol "file2_thread_local" is a thread-local variable at offset 8 in the thread-local storage for `.../gdb.threads/tls'.
319gdb_test "info address file2_thread_local" "Symbol \"file2_thread_local\" is a thread-local variable.*"
320# Here it could also crash with: Cannot access memory at address 0x0
321gdb_test "p a_thread_local" " = \[0-9\]+" "p a_thread_local second time"
322gdb_test "info address a_thread_local" "Symbol \"a_thread_local\" is a thread-local variable.*"
323
8bc2021f
EZ
324# Done!
325#
326gdb_exit
327
328return 0