Files
binutils-gdb/gdb/testsuite/gdb.threads/tls.exp
Kevin Buettner 8b87fe90e8 Don't attempt to find TLS address when target has no registers
This commit fixes two bugs, one of which is Bug 25807, which occurs
when target_translate_tls_address() is called from
language_defn::read_var_value in findvar.c.  I found it while testing on
aarch64; it turned a KFAIL for gdb.threads/tls.exp: print a_thread_local
into a FAIL due to a GDB internal error.  Now, with this commit in place,
the KFAIL/FAIL turns into a PASS.

In addition to the existing test just noted, I've also added a test to
the new test case gdb.base/tls-nothreads.exp.  It'll be tested, using
different scenarios, up to 8 times:

PASS: gdb.base/tls-nothreads.exp: default: force_internal_tls=false: after exit: print tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: default: force_internal_tls=true: after exit: print tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: static: force_internal_tls=false: after exit: print tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: static: force_internal_tls=true: after exit: print tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: pthreads: force_internal_tls=false: after exit: print tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: pthreads: force_internal_tls=true: after exit: print tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: pthreads-static: force_internal_tls=false: after exit: print tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: pthreads-static: force_internal_tls=true: after exit: print tls_tbss_1

There is a related problem that occurs when target_translate_tls_address
is called from find_minsym_type_and_address() in minsyms.c.  It can be
observed when debugging a program without debugging symbols when the
program is not executing.  I've written a new test for this, but it's
(also) included in the new test case gdb.base/tls-nothreads.exp, found
later in this series.  Depending on the target, it can run up to 8
times using different scenarios.  E.g., on aarch64, I'm seeing these
PASSes, all of which test this change:

PASS: gdb.base/tls-nothreads.exp: default: force_internal_tls=false: stripped: after exit: print (int) tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: default: force_internal_tls=true: stripped: after exit: print (int) tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: static: force_internal_tls=false: stripped: after exit: print (int) tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: static: force_internal_tls=true: stripped: after exit: print (int) tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: pthreads: force_internal_tls=false: stripped: after exit: print (int) tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: pthreads: force_internal_tls=true: stripped: after exit: print (int) tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: pthreads-static: force_internal_tls=false: stripped: after exit: print (int) tls_tbss_1
PASS: gdb.base/tls-nothreads.exp: pthreads-static: force_internal_tls=true: stripped: after exit: print (int) tls_tbss_1

In an earlier version of this commit (v4), I was checking whether the
target has registers in language_defn::read_var_value in findvar.c and
in find_minsym_type_and_address in minsyms.c, printing suitable error
messages in each case.  In his review of this commit for the v4
series, Tom Tromey asked whether it would be better to do this check
in target_translate_tls_address.  I had considered doing that for the
v4 (and earlier) series, but I wanted to print slightly different
messages at each check.  Also, read_var_value in findvar.c was already
printing a message in some cases and I had arranged for the later
check in that function to match the original message.

However, while I had added a target-has-registers check at two of the
call sites for target_translate_tls_address, I hadn't added it at the
third call site which is in dwarf_expr_context::execute_stack_op() in
dwarf2/expr.c.  I believe that in most cases, this is handled by the
early check in language_defn::read_var_value...

  else if (sym_need == SYMBOL_NEEDS_REGISTERS && !target_has_registers ())
    error (_("Cannot read `%s' without registers"), var->print_name ());

...but it's entirely possible that dwarf_expr_context::execute_stack_op()
might get called in some other context.  So it makes sense to do the
target-has-registers check for that case too.  And rather than add yet
another check at that call site, I decided that moving the check and
error message to target_translate_tls_address makes sense.

I had to make the error messages that it prints somewhat more generic.
In particular, when called from language_defn::read_var_value, the
message printed by target_translate_tls_address no longer matches the
earlier message that could be printed (as shown above).  That meant
that the test cases which check for this message, gdb.threads/tls.exp,
and gdb.base/tls-nothreads.exp had to be adjusted to account for the
new message.  Also, I think it's valuable to the user to know (if
possible) the name of the variable that caused the error, so I've
added an optional parameter to target_translate_tls_address, providing
the name of the variable, if it's known.  Therefore, the message
that's printed when the target-has-registers test fails is one of the
following:

When the TLS variable isn't known (due to being called from
dwarf_expr_context::execute_stack_op):

    "Cannot translate TLS address without registers"

When the TLS variable is known (from either of the other two call sites
for target_translate_tls_address):

    "Cannot find address of TLS symbol `%s' without registers"

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25807
Tested-By: Luis Machado <luis.machado@arm.com>
Approved-By: Luis Machado <luis.machado@arm.com>
2025-04-24 09:54:42 -07:00

324 lines
9.8 KiB
Plaintext

# tls.exp -- Expect script to test thread-local storage
# Copyright (C) 1992-2025 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/>. */
load_lib gdb-python.exp
standard_testfile tls.c tls2.c
if {[istarget "*-*-linux"]} {
set target_cflags "-D_MIT_POSIX_THREADS"
} else {
set target_cflags ""
}
if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile} ${srcdir}/${subdir}/${srcfile2}" "${binfile}" executable [list c++ debug]] != "" } {
return -1
}
### Compute the value of the a_thread_local variable.
proc compute_expected_value {value} {
set expected_value 0
set i 0
while { $i <= $value} {
incr expected_value $i
incr i
}
return $expected_value
}
### Get the value of the variable 'me' for the current thread.
proc get_me_variable {tnum} {
global expect_out
global gdb_prompt
global decimal
set value_of_me -1
send_gdb "print me\n"
gdb_expect {
-re ".*= ($decimal).*\r\n$gdb_prompt $" {
set value_of_me $expect_out(1,string)
pass "$tnum thread print me"
}
-re "$gdb_prompt $" {
fail "$tnum thread print me"
}
timeout {
fail "$tnum thread print me (timeout)"
}
}
return ${value_of_me}
}
### Check the values of the thread local variables in the thread.
### Also check that info address print the right things.
proc check_thread_local {number} {
set me_variable [get_me_variable $number]
set expected_value [compute_expected_value ${me_variable}]
gdb_test "p a_thread_local" \
"= $expected_value" \
"${number} thread local storage"
if {[allow_python_tests]} {
gdb_test_no_output \
"python sym = gdb.lookup_symbol('a_thread_local')\[0\]" \
"${number} look up a_thread_local symbol"
# We intentionally do not pass a frame to "value" here. If a
# TLS variable requires a frame, this will fail. However, if
# it does not require a frame, then it will succeed.
gdb_test "python print(sym.value())" "$expected_value" \
"${number} get symbol value without frame"
}
gdb_test "p K::another_thread_local" \
"= $me_variable" \
"${number} another thread local storage"
gdb_test "info address a_thread_local" \
".*a_thread_local.*a thread-local variable at offset.*" \
"${number} info address a_thread_local"
gdb_test "info address K::another_thread_local" \
".*another_thread_local.*a thread-local variable at offset.*" \
"${number} info address another_thread_local"
}
### Select a particular thread.
proc select_thread {thread} {
global gdb_prompt
send_gdb "thread $thread\n"
gdb_expect {
-re "\\\[Switching to thread .*\\\].*\r\n$gdb_prompt $" {
pass "selected thread: $thread"
}
-re "$gdb_prompt $" {
fail "selected thread: $thread"
}
timeout {
fail "selected thread: $thread (timeout)"
}
}
}
### Do a backtrace for the current thread, and check that the 'spin' routine
### is in it. This means we have one of the threads we created, rather
### than the main thread. Record the thread in the spin_threads
### array. Also remember the level of the 'spin' routine in the backtrace, for
### later use.
proc check_thread_stack {number spin_threads spin_threads_level} {
global gdb_prompt
global expect_out
global decimal
global hex
upvar $spin_threads tarr
upvar $spin_threads_level tarrl
with_test_prefix "thread stack" {
select_thread $number
}
send_gdb "where\n"
gdb_expect {
-re ".*(\[0-9\]+)\[ \t\]+$hex (\\\[PAC\\\] )?in spin \\(vp=(0x\[0-9a-f\]+).*\r\n$gdb_prompt $" {
if {[info exists tarr($number)]} {
fail "backtrace of thread number $number in spin"
} else {
pass "backtrace of thread number $number in spin"
set level $expect_out(1,string)
set tarrl($number) $level
set tarr($number) 1
}
}
-re ".*$gdb_prompt $" {
set tarr($number) 0
set tarrl($number) 0
pass "backtrace of thread number $number not relevant"
}
timeout {
fail "backtrace of thread number $number (timeout)"
}
}
}
clean_restart ${binfile}
gdb_test_multiple "print a_thread_local" "" {
-re -wrap "Cannot find thread-local variables on this target" {
kfail "gdb/25807" $gdb_test_name
}
-re -wrap "Cannot (?:read|find address of TLS symbol) .a_thread_local. without registers" {
pass $gdb_test_name
}
}
if {![runto_main]} {
return 0
}
# Set a breakpoint at the "spin" routine to
# test the thread local's value.
#
gdb_test "b [gdb_get_line_number "here we know tls value"]" \
".*Breakpoint 2.*tls.*" "set breakpoint at all threads"
# Set a bp at a point where we know all threads are alive.
#
gdb_test "b [gdb_get_line_number "still alive"]" \
".*Breakpoint 3.*tls.*" "set breakpoint at synch point"
# Set a bp at the end to see if all threads are finished.
#
gdb_test "b [gdb_get_line_number "before exit"]" \
".*Breakpoint 4.*tls.*" "set breakpoint at exit"
send_gdb "continue\n"
gdb_expect {
-re ".* received signal SIGSEGV.*a_thread_local = 0;.*$gdb_prompt $" {
# This is the first symptom if the gcc and binutils versions
# in use support TLS, but the system glibc does not.
unsupported "continue to first thread: system does not support TLS"
return -1
}
-re ".*$inferior_exited_re normally.*$gdb_prompt $" {
fail "continue to first thread: program runaway"
}
-re ".*Pass 0 done.*Pass 1 done.*$gdb_prompt $" {
fail "continue to first thread: program runaway 2"
}
-re ".*Breakpoint 2.*tls value.*$gdb_prompt $" {
pass "continue to first thread: get to thread"
}
-re ".*$gdb_prompt $" {
fail "continue to first thread: no progress?"
}
timeout { fail "continue to first thread (timeout)" }
}
gdb_test "info thread" ".*${tdlabel_re}.*spin.*" \
"at least one th in spin while stopped at first th"
check_thread_local "first"
gdb_test "continue" ".*Breakpoint 2.*tls value.*" "continue to second thread"
gdb_test "info thread" "${tdlabel_re}.*spin.*" \
"at least one th in spin while stopped at second th"
check_thread_local "second"
gdb_test "continue" ".*Breakpoint 2.*tls value.*" "continue to third thread"
gdb_test "info thread" ".*${tdlabel_re}.*spin.*" \
"at least one th in spin while stopped at third th"
check_thread_local "third"
gdb_test "continue" ".*Breakpoint 3.*still alive.*" "continue to synch point"
set no_of_threads 0
send_gdb "info thread\n"
gdb_expect {
-re "^info thread\[ \t\r\n\]+ *Id .*Frame\[ \t\r\n\]+.*(\[0-9\]+) *${tdlabel_re}\[^\r\n\]+\r\n$gdb_prompt $" {
set no_of_threads $expect_out(1,string)
pass "get number of threads"
}
-re "$gdb_prompt $" {
fail "get number of threads"
}
timeout {
fail "get number of threads (timeout)"
}
}
array set spin_threads {}
unset spin_threads
array set spin_threads_level {}
unset spin_threads_level
# For each thread check its backtrace to see if it is stopped at the
# spin routine.
for {set i 1} {$i <= $no_of_threads} {incr i} {
check_thread_stack $i spin_threads spin_threads_level
}
### Loop through the threads and check the values of the tls variables.
### keep track of how many threads we find in the spin routine.
set thrs_in_spin 0
foreach i [array names spin_threads] {
if {$spin_threads($i) == 1} {
incr thrs_in_spin
with_test_prefix "spin_threads" {
select_thread $i
}
set level $spin_threads_level($i)
# We expect to be in sem_wait, but if the thread has not yet
# been scheduled, we might be in sem_post still. We could be at
# any intermediate point in spin, too, but that is much less
# likely.
gdb_test "up $level" ".*spin.*sem_(wait|post).*" "thread $i up"
check_thread_local $i
}
}
if {$thrs_in_spin == 0} {
fail "no thread backtrace reported spin (vsyscall kernel problem?)"
}
gdb_test "continue" ".*Breakpoint 4.*before exit.*" "threads exited"
send_gdb "info thread\n"
gdb_expect {
-re ".* 1 *${tdlabel_re}.*2 *${tdlabel_re}.*$gdb_prompt $" {
fail "too many threads left at end"
}
-re ".*\\\* 1 *${tdlabel_re}.*main.*$gdb_prompt $" {
pass "expect only base thread at end"
}
-re ".*No stack.*$gdb_prompt $" {
fail "runaway at end"
}
-re ".*$gdb_prompt $" {
fail "mess at end"
}
timeout { fail "at end (timeout)" }
}
# Start over and do some "info address" stuff
#
runto spin
gdb_test "info address a_global" \
".*a_global.*static storage at address.*"
gdb_test "info address me" ".*me.*is a (complex DWARF expression:|variable).*"
# Test LOC_UNRESOLVED references resolving for `extern' TLS variables.
gdb_test "p a_thread_local" " = \[0-9\]+"
# Here it could crash with: Cannot access memory at address 0x0
gdb_test "p file2_thread_local" " = \[0-9\]+"
# Depending on the current lookup scope we get LOC_UNRESOLVED or LOC_COMPUTED
# both printing:
# Symbol "file2_thread_local" is a thread-local variable at offset 8 in the thread-local storage for `.../gdb.threads/tls'.
gdb_test "info address file2_thread_local" "Symbol \"file2_thread_local\" is a thread-local variable.*"
# Here it could also crash with: Cannot access memory at address 0x0
gdb_test "p a_thread_local" " = \[0-9\]+" "p a_thread_local second time"
gdb_test "info address a_thread_local" "Symbol \"a_thread_local\" is a thread-local variable.*"
# Done!
#
gdb_exit