summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.server/server-connect.exp
blob: e69f792a1bb60ccbc052f9bf7c4ea58ecef85692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# This testcase is part of GDB, the GNU debugger.
#
# Copyright 2018-2019 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/>.

# Test multiple types of connection (IPv4, IPv6, TCP, UDP) and make
# sure both gdbserver and GDB work.

load_lib gdbserver-support.exp

standard_testfile normal.c

if {[skip_gdbserver_tests]} {
    return 0
}

# We want to have control over where we start gdbserver.
if { [is_remote target] } {
    return 0
}

if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
    return -1
}

# Make sure we're disconnected, in case we're testing with an
# extended-remote board, therefore already connected.
gdb_test "disconnect" ".*"

set target_exec [gdbserver_download_current_prog]

# An array containing the test instructions for each scenario.  The
# description of each field is as follows:
#
# - The connection specification to be used when starting
#   gdbserver/GDB.  This string will be used to set the
#   GDB_TEST_SOCKETHOST when calling gdbserver_start.
#
# - A flag indicating whether gdbserver should fail when we attempt to
#   start it.  Useful when testing erroneous connection specs such as
#   "tcp8:".
#
# - The prefix that should be prepended to the test messages.
set test_params \
    { \
	  { "tcp4:127.0.0.1" 0 "tcp4" } \
	  { "tcp6:::1"       0 "tcp6" } \
	  { "tcp6:[::1]"     0 "tcp6-with-brackets" } \
	  { "tcp:localhost"  0 "tcp" } \
	  { "udp4:127.0.0.1" 0 "udp4" } \
	  { "udp6:::1"       0 "udp6" } \
	  { "udp6:[::1]"     0 "udp6-with-brackets" } \
	  { "tcp8:123"       1 "tcp8" } \
	  { "udp123:::"      1 "udp123" } \
	  { "garbage:1234"   1 "garbage:1234" } \
    }

# The best way to test different types of connections is to set the
# GDB_TEST_SOCKETHOST variable accordingly.
save_vars { GDB_TEST_SOCKETHOST } {
    foreach line $test_params {
	set sockhost [lindex $line 0]
	set gdbserver_should_fail [lindex $line 1]
	set prefix [lindex $line 2]

	with_test_prefix $prefix {
	    set GDB_TEST_SOCKETHOST $sockhost
	    set test "start gdbserver"

	    # Try to start gdbserver.
	    set catchres [catch {set res [gdbserver_start "" $target_exec]} errmsg]

	    if { $catchres != 0 } {
		if { $gdbserver_should_fail } {
		    pass "$test: gdbserver failed as expected"
		} else {
		    fail "$test: $errmsg"
		}
		continue
	    } else {
		if { $gdbserver_should_fail } {
		    fail "$test: gdbserver should fail but did not"
		} else {
		    pass "$test"
		}
	    }

	    set gdbserver_protocol [lindex $res 0]
	    set gdbserver_gdbport [lindex $res 1]
	    set test "connect to gdbserver using $sockhost"

	    set res [gdb_target_cmd_ext $gdbserver_protocol $gdbserver_gdbport]
	    if { $res == 0 } {
		pass $test
	    } elseif { $res == 1 }  {
		fail $test
	    } else {
		unsupported $test
	    }
	}
    }
}