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