Home | History | Annotate | Line # | Download | only in gdb.server
      1      1.1  christos # This testcase is part of GDB, the GNU debugger.
      2      1.1  christos #
      3  1.1.1.2  christos # Copyright 2021-2024 Free Software Foundation, Inc.
      4      1.1  christos #
      5      1.1  christos # This program is free software; you can redistribute it and/or modify
      6      1.1  christos # it under the terms of the GNU General Public License as published by
      7      1.1  christos # the Free Software Foundation; either version 3 of the License, or
      8      1.1  christos # (at your option) any later version.
      9      1.1  christos #
     10      1.1  christos # This program is distributed in the hope that it will be useful,
     11      1.1  christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12      1.1  christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13      1.1  christos # GNU General Public License for more details.
     14      1.1  christos #
     15      1.1  christos # You should have received a copy of the GNU General Public License
     16      1.1  christos # along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17      1.1  christos 
     18      1.1  christos # Test how GDB handles the case where a target either doesn't use 'T'
     19      1.1  christos # packets at all or doesn't include a thread-id in a 'T' packet, AND,
     20      1.1  christos # where the test program contains multiple threads.
     21      1.1  christos #
     22      1.1  christos # In general if multiple threads are executing and the target doesn't
     23      1.1  christos # include a thread-id in its stop response then GDB will not be able
     24      1.1  christos # to correctly figure out which thread the stop applies to.
     25      1.1  christos #
     26      1.1  christos # However, this test covers a very specific case, there are multiple
     27      1.1  christos # threads but only a single thread is actually executing.  So, when
     28      1.1  christos # the stop comes from the target, without a thread-id, GDB should be
     29      1.1  christos # able to correctly figure out which thread has stopped.
     30      1.1  christos 
     31      1.1  christos load_lib gdbserver-support.exp
     32      1.1  christos 
     33  1.1.1.2  christos require allow_gdbserver_tests
     34      1.1  christos 
     35      1.1  christos standard_testfile
     36      1.1  christos if { [build_executable "failed to prepare" $testfile $srcfile {debug pthreads}] == -1 } {
     37      1.1  christos     return -1
     38      1.1  christos }
     39      1.1  christos 
     40  1.1.1.2  christos set target_binfile [gdb_remote_download target $binfile]
     41  1.1.1.2  christos 
     42      1.1  christos # Run the tests with different features of GDBserver disabled.
     43      1.1  christos # TARGET_NON_STOP is passed to "maint set target-non-stop".
     44      1.1  christos proc run_test { target_non_stop disable_feature } {
     45  1.1.1.2  christos     global binfile gdb_prompt decimal hex tdlabel_re
     46      1.1  christos     global GDBFLAGS
     47      1.1  christos 
     48      1.1  christos     save_vars { GDBFLAGS } {
     49      1.1  christos 	append GDBFLAGS " -ex \"maint set target-non-stop $target_non_stop\""
     50      1.1  christos 
     51      1.1  christos 	# If GDB and GDBserver are both running locally, set the sysroot to avoid
     52      1.1  christos 	# reading files via the remote protocol.
     53      1.1  christos 	if { ![is_remote host] && ![is_remote target] } {
     54      1.1  christos 	    set GDBFLAGS "$GDBFLAGS -ex \"set sysroot\""
     55      1.1  christos 	}
     56      1.1  christos 
     57      1.1  christos 	clean_restart ${binfile}
     58      1.1  christos     }
     59      1.1  christos 
     60      1.1  christos     # Make sure we're disconnected, in case we're testing with an
     61      1.1  christos     # extended-remote board, therefore already connected.
     62      1.1  christos     gdb_test "disconnect" ".*"
     63      1.1  christos 
     64      1.1  christos     set packet_arg ""
     65      1.1  christos     if { $disable_feature != "" } {
     66      1.1  christos 	set packet_arg "--disable-packet=${disable_feature}"
     67      1.1  christos     }
     68  1.1.1.2  christos     set res [gdbserver_start $packet_arg $::target_binfile]
     69      1.1  christos     set gdbserver_protocol [lindex $res 0]
     70      1.1  christos     set gdbserver_gdbport [lindex $res 1]
     71      1.1  christos 
     72      1.1  christos     # Disable XML-based thread listing, and multi-process extensions.
     73  1.1.1.2  christos     gdb_test \
     74  1.1.1.2  christos 	"set remote threads-packet off" \
     75  1.1.1.2  christos 	"Support for the 'qXfer:threads:read' packet on future remote targets is set to \"off\"."
     76  1.1.1.2  christos     gdb_test \
     77  1.1.1.2  christos 	"set remote multiprocess-feature-packet off" \
     78  1.1.1.2  christos 	"Support for the 'multiprocess-feature' packet on future remote targets is set to \"off\"."
     79      1.1  christos 
     80      1.1  christos     set res [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport]
     81      1.1  christos     if ![gdb_assert {$res == 0} "connect"] {
     82      1.1  christos 	return
     83      1.1  christos     }
     84      1.1  christos 
     85      1.1  christos     # There should be only one thread listed at this point.
     86      1.1  christos     gdb_test_multiple "info threads" "" {
     87  1.1.1.2  christos 	-re "2 ${tdlabel_re}.*$gdb_prompt $" {
     88      1.1  christos 	    fail $gdb_test_name
     89      1.1  christos 	}
     90      1.1  christos 	-re "has terminated.*$gdb_prompt $" {
     91      1.1  christos 	    fail $gdb_test_name
     92      1.1  christos 	}
     93  1.1.1.2  christos 	-re "\\\* 1\[\t \]*${tdlabel_re}\[^\r\n\]*\r\n$gdb_prompt $" {
     94      1.1  christos 	    pass $gdb_test_name
     95      1.1  christos 	}
     96      1.1  christos     }
     97      1.1  christos 
     98      1.1  christos     gdb_breakpoint "unlock_worker"
     99      1.1  christos     gdb_continue_to_breakpoint "run to unlock_worker"
    100      1.1  christos 
    101      1.1  christos     # There should be two threads at this point with thread 1 selected.
    102      1.1  christos     gdb_test "info threads" \
    103  1.1.1.2  christos 	"\\\* 1\[\t \]*${tdlabel_re}\[^\r\n\]*\r\n  2\[\t \]*${tdlabel_re}\[^\r\n\]*" \
    104      1.1  christos 	"second thread should now exist"
    105      1.1  christos 
    106      1.1  christos     # Switch threads.
    107      1.1  christos     gdb_test "thread 2" ".*" "switch to second thread"
    108      1.1  christos 
    109      1.1  christos     # Now turn on scheduler-locking so that when we step thread 2 only
    110      1.1  christos     # that one thread will be set running.
    111      1.1  christos     gdb_test_no_output "set scheduler-locking on"
    112      1.1  christos 
    113      1.1  christos     # Single step thread 2.  Only the one thread will step.  When the
    114      1.1  christos     # thread stops, if the stop packet doesn't include a thread-id
    115      1.1  christos     # then GDB should still understand which thread stopped.
    116      1.1  christos     gdb_test_multiple "stepi" "" {
    117      1.1  christos 	-re -wrap "Thread 1 received signal SIGTRAP.*" {
    118      1.1  christos 	    fail $gdb_test_name
    119      1.1  christos 	}
    120      1.1  christos 	-re -wrap "$hex.*$decimal.*while \\(worker_blocked\\).*" {
    121      1.1  christos 	    pass $gdb_test_name
    122      1.1  christos 	}
    123      1.1  christos     }
    124      1.1  christos 
    125      1.1  christos     # Check that thread 2 is still selected.
    126      1.1  christos     gdb_test "info threads" \
    127  1.1.1.2  christos 	"  1\[\t \]*${tdlabel_re}\[^\r\n\]*\r\n\\\* 2\[\t \]*${tdlabel_re}\[^\r\n\]*" \
    128      1.1  christos 	"second thread should still be selected after stepi"
    129      1.1  christos 
    130      1.1  christos     # Turn scheduler locking off again so that when we continue all
    131      1.1  christos     # threads will be set running.
    132      1.1  christos     gdb_test_no_output "set scheduler-locking off"
    133      1.1  christos 
    134      1.1  christos     # Continue until exit.  The server sends a 'W' with no PID.
    135      1.1  christos     # Bad GDB gave an error like below when target is nonstop:
    136      1.1  christos     #  (gdb) c
    137      1.1  christos     #  Continuing.
    138      1.1  christos     #  No process or thread specified in stop reply: W00
    139      1.1  christos     gdb_continue_to_end "" continue 1
    140      1.1  christos }
    141      1.1  christos 
    142      1.1  christos # Disable different features within gdbserver:
    143      1.1  christos #
    144      1.1  christos # Tthread: Start GDBserver, with ";thread:NNN" in T stop replies disabled,
    145      1.1  christos #          emulating old gdbservers when debugging single-threaded programs.
    146      1.1  christos #
    147      1.1  christos # T: Start GDBserver with the entire 'T' stop reply packet disabled,
    148      1.1  christos #    GDBserver will instead send the 'S' stop reply.
    149      1.1  christos #
    150      1.1  christos # Also test both all-stop and non-stop variants of the remote
    151      1.1  christos # protocol.
    152      1.1  christos foreach_with_prefix target-non-stop {"off" "on"} {
    153      1.1  christos     foreach_with_prefix to_disable { "" Tthread T } {
    154      1.1  christos 	run_test ${target-non-stop} $to_disable
    155      1.1  christos     }
    156      1.1  christos }
    157