1 # This testcase is part of GDB, the GNU debugger. 2 # 3 # Copyright 2018-2024 Free Software Foundation, Inc. 4 # 5 # This program is free software; you can redistribute it and/or modify 6 # it under the terms of the GNU General Public License as published by 7 # the Free Software Foundation; either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU General Public License for more details. 14 # 15 # You should have received a copy of the GNU General Public License 16 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 # Test multiple types of connection (IPv4, IPv6, TCP, UDP) and make 19 # sure both gdbserver and GDB work. 20 21 load_lib gdbserver-support.exp 22 23 standard_testfile normal.c 24 25 require allow_gdbserver_tests 26 27 # We want to have control over where we start gdbserver. 28 require {!is_remote target} 29 30 if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } { 31 return -1 32 } 33 34 # Make sure we're disconnected, in case we're testing with an 35 # extended-remote board, therefore already connected. 36 gdb_test "disconnect" ".*" 37 38 set target_exec [gdbserver_download_current_prog] 39 40 # An array containing the test instructions for each scenario. The 41 # description of each field is as follows: 42 # 43 # - The connection specification to be used when starting 44 # gdbserver/GDB. This string will be used to set the 45 # GDB_TEST_SOCKETHOST when calling gdbserver_start. 46 # 47 # - A flag indicating whether gdbserver should fail when we attempt to 48 # start it. Useful when testing erroneous connection specs such as 49 # "tcp8:". 50 # 51 # - The prefix that should be prepended to the test messages. 52 set test_params \ 53 { \ 54 { "tcp4:127.0.0.1" 0 "tcp4" } \ 55 { "tcp6:::1" 0 "tcp6" } \ 56 { "tcp6:[::1]" 0 "tcp6-with-brackets" } \ 57 { "tcp:localhost" 0 "tcp" } \ 58 { "udp4:127.0.0.1" 0 "udp4" } \ 59 { "udp6:::1" 0 "udp6" } \ 60 { "udp6:[::1]" 0 "udp6-with-brackets" } \ 61 { "tcp8:123" 1 "tcp8" } \ 62 { "udp123:::" 1 "udp123" } \ 63 { "garbage:1234" 1 "garbage:1234" } \ 64 } 65 66 # The best way to test different types of connections is to set the 67 # GDB_TEST_SOCKETHOST variable accordingly. 68 save_vars { GDB_TEST_SOCKETHOST } { 69 foreach line $test_params { 70 set sockhost [lindex $line 0] 71 set gdbserver_should_fail [lindex $line 1] 72 set prefix [lindex $line 2] 73 74 with_test_prefix $prefix { 75 set GDB_TEST_SOCKETHOST $sockhost 76 set test "start gdbserver" 77 78 # Try to start gdbserver. 79 set catchres [catch {set res [gdbserver_start "" $target_exec]} errmsg] 80 81 if { $catchres != 0 } { 82 if { $gdbserver_should_fail } { 83 pass "$test: gdbserver failed as expected" 84 } else { 85 fail "$test: $errmsg" 86 } 87 continue 88 } else { 89 if { $gdbserver_should_fail } { 90 fail "$test: gdbserver should fail but did not" 91 } elseif { [llength $res] == 0 } { 92 unsupported $test 93 continue 94 } else { 95 pass "$test" 96 } 97 } 98 99 set gdbserver_protocol [lindex $res 0] 100 set gdbserver_gdbport [lindex $res 1] 101 set test "connect to gdbserver using $sockhost" 102 103 set res [gdb_target_cmd_ext $gdbserver_protocol $gdbserver_gdbport] 104 if { $res == 0 } { 105 pass $test 106 } elseif { $res == 1 } { 107 fail $test 108 } else { 109 unsupported $test 110 } 111 } 112 } 113 } 114