aboutsummaryrefslogtreecommitdiff
path: root/gdb/unittests
diff options
context:
space:
mode:
authorSergio Durigan Junior <sergiodj@redhat.com>2018-05-18 01:29:24 -0400
committerSergio Durigan Junior <sergiodj@redhat.com>2018-07-11 19:41:31 -0400
commitc7ab0aef11d91b637bf091aa9176b8dc4aadee46 (patch)
tree8dd380d93603e4601912dbf08d8ce861098fadc3 /gdb/unittests
parent4c7333b308f5178813745f40e641231efb1cb763 (diff)
downloadgdb-c7ab0aef11d91b637bf091aa9176b8dc4aadee46.zip
gdb-c7ab0aef11d91b637bf091aa9176b8dc4aadee46.tar.gz
gdb-c7ab0aef11d91b637bf091aa9176b8dc4aadee46.tar.bz2
Implement IPv6 support for GDB/gdbserver
This patch implements IPv6 support for both GDB and gdbserver. Based on my research, it is the fourth attempt to do that since 2006. Since I used ideas from all of the previous patches, I also added their authors's names on the ChangeLogs as a way to recognize their efforts. For reference sake, you can find the previous attempts at: https://sourceware.org/ml/gdb-patches/2006-09/msg00192.html https://sourceware.org/ml/gdb-patches/2014-02/msg00248.html https://sourceware.org/ml/gdb-patches/2016-02/msg00226.html The basic idea behind the patch is to start using the new 'getaddrinfo'/'getnameinfo' calls, which are responsible for translating names and addresses in a protocol-independent way. This means that if we ever have a new version of the IP protocol, we won't need to change the code again (or, at least, won't have to change the majority of the code). The function 'getaddrinfo' returns a linked list of possible addresses to connect to. Dealing with multiple addresses proved to be a hard task with the current TCP auto-retry mechanism implemented on ser-tcp:net_open. For example, when gdbserver listened only on an IPv4 socket: $ ./gdbserver --once 127.0.0.1:1234 ./a.out and GDB was instructed to try to connect to both IPv6 and IPv4 sockets: $ ./gdb -ex 'target extended-remote localhost:1234' ./a.out the user would notice a somewhat big delay before GDB was able to connect to the IPv4 socket. This happened because GDB was trying to connect to the IPv6 socket first, and had to wait until the connection timed out before it tried to connect to the IPv4 socket. For that reason, I had to rewrite the main loop and implement a new method for handling multiple connections. After some discussion, Pedro and I agreed on the following algorithm: 1) For each entry returned by 'getaddrinfo', we try to open a socket and connect to it. 2.a) If we have a successful 'connect', we just use that connection. 2.b) If we don't have a successfull 'connect', but if we've got a ECONNREFUSED (meaning the the connection was refused), we keep track of this fact by using a flag. 2.c) If we don't have a successfull 'connect', but if we've got a EINPROGRESS (meaning that the connection is in progress), we perform a 'select' call on the socket until we have a result (either a successful connection, or an error on the socket). 3) If tcp_auto_retry is true, and we haven't gotten a successful connection, and at least one of our attempts failed with ECONNREFUSED, then we wait a little bit (i.e., call 'wait_for_connect'), check to see if there was a timeout/interruption (in which case we bail out), and then go back to (1). After multiple tests, I was able to connect without delay on the scenario described above, and was also able to connect in all other types of scenarios. I also implemented some hostname parsing functions (along with their corresponding unit tests) which are used to help GDB and gdbserver to parse hostname strings provided by the user. These new functions are living inside common/netstuff.[ch]. I've had to do that since IPv6 introduces a new URL scheme, which defines that square brackets can be used to enclose the host part and differentiate it from the port (e.g., "[::1]:1234" means "host ::1, port 1234"). I spent some time thinking about a reasonable way to interpret what the user wants, and I came up with the following: - If the user has provided a prefix that doesn't specify the protocol version (i.e., "tcp:" or "udp:"), or if the user has not provided any prefix, don't make any assumptions (i.e., assume AF_UNSPEC when dealing with 'getaddrinfo') *unless* the host starts with "[" (in which case, assume it's an IPv6 host). - If the user has provided a prefix that does specify the protocol version (i.e., "tcp4:", "tcp6:", "udp4:" or "udp6:"), then respect that. This method doesn't follow strictly what RFC 2732 proposes (that literal IPv6 addresses should be provided enclosed in "[" and "]") because IPv6 addresses still can be provided without square brackets in our case, but since we have prefixes to specify protocol versions I think this is not an issue. Another thing worth mentioning is the new 'GDB_TEST_SOCKETHOST' testcase parameter, which makes it possible to specify the hostname (without the port) to be used when testing GDB and gdbserver. For example, to run IPv6 tests: $ make check-gdb RUNTESTFLAGS='GDB_TEST_SOCKETHOST=tcp6:[::1]' Or, to run IPv4 tests: $ make check-gdb RUNTESTFLAGS='GDB_TEST_SOCKETHOST=tcp4:127.0.0.1' This required a few changes on the gdbserver-base.exp, and also a minimal adjustment on gdb.server/run-without-local-binary.exp. Finally, I've implemented a new testcase, gdb.server/server-connect.exp, which is supposed to run on the native host and perform various "smoke tests" using different connection methods. This patch has been regression-tested on BuildBot and locally, and also built using a x86_64-w64-mingw32 GCC, and no problems were found. gdb/ChangeLog: 2018-07-11 Sergio Durigan Junior <sergiodj@redhat.com> Jan Kratochvil <jan.kratochvil@redhat.com> Paul Fertser <fercerpav@gmail.com> Tsutomu Seki <sekiriki@gmail.com> Pedro Alves <palves@redhat.com> * Makefile.in (SUBDIR_UNITTESTS_SRCS): Add 'unittests/parse-connection-spec-selftests.c'. (COMMON_SFILES): Add 'common/netstuff.c'. (HFILES_NO_SRCDIR): Add 'common/netstuff.h'. * NEWS (Changes since GDB 8.2): Mention IPv6 support. * common/netstuff.c: New file. * common/netstuff.h: New file. * ser-tcp.c: Include 'netstuff.h' and 'wspiapi.h'. (wait_for_connect): Update comment. New parameter 'gdb::optional<int> sock' instead of 'struct serial *scb'. Use 'sock' directly instead of 'scb->fd'. (try_connect): New function, with code from 'net_open'. (net_open): Rewrite main loop to deal with multiple sockets/addresses. Handle IPv6-style hostnames; implement support for IPv6 connections. * unittests/parse-connection-spec-selftests.c: New file. gdb/gdbserver/ChangeLog: 2018-07-11 Sergio Durigan Junior <sergiodj@redhat.com> Jan Kratochvil <jan.kratochvil@redhat.com> Paul Fertser <fercerpav@gmail.com> Tsutomu Seki <sekiriki@gmail.com> * Makefile.in (SFILES): Add '$(srcdir)/common/netstuff.c'. (OBS): Add 'common/netstuff.o'. (GDBREPLAY_OBS): Likewise. * gdbreplay.c: Include 'wspiapi.h' and 'netstuff.h'. (remote_open): Implement support for IPv6 connections. * remote-utils.c: Include 'netstuff.h', 'filestuff.h' and 'wspiapi.h'. (handle_accept_event): Accept connections from IPv6 sources. (remote_prepare): Handle IPv6-style hostnames; implement support for IPv6 connections. (remote_open): Implement support for printing connections from IPv6 sources. gdb/testsuite/ChangeLog: 2018-07-11 Sergio Durigan Junior <sergiodj@redhat.com> Jan Kratochvil <jan.kratochvil@redhat.com> Paul Fertser <fercerpav@gmail.com> Tsutomu Seki <sekiriki@gmail.com> * README (Testsuite Parameters): Mention new 'GDB_TEST_SOCKETHOST' parameter. * boards/native-extended-gdbserver.exp: Do not set 'sockethost' by default. * boards/native-gdbserver.exp: Likewise. * gdb.server/run-without-local-binary.exp: Improve regexp used for detecting when a remote debugging connection succeeds. * gdb.server/server-connect.exp: New file. * lib/gdbserver-support.exp (gdbserver_default_get_comm_port): Do not prefix the port number with ":". (gdbserver_start): New global GDB_TEST_SOCKETHOST. Implement support for detecting and using it. Add '$debughost_gdbserver' to the list of arguments used to start gdbserver. Handle case when gdbserver cannot resolve a network name. gdb/doc/ChangeLog: 2018-07-11 Sergio Durigan Junior <sergiodj@redhat.com> Jan Kratochvil <jan.kratochvil@redhat.com> Paul Fertser <fercerpav@gmail.com> Tsutomu Seki <sekiriki@gmail.com> * gdb.texinfo (Remote Connection Commands): Add explanation about new IPv6 support. Add new connection prefixes.
Diffstat (limited to 'gdb/unittests')
-rw-r--r--gdb/unittests/parse-connection-spec-selftests.c249
1 files changed, 249 insertions, 0 deletions
diff --git a/gdb/unittests/parse-connection-spec-selftests.c b/gdb/unittests/parse-connection-spec-selftests.c
new file mode 100644
index 0000000..f7446c7
--- /dev/null
+++ b/gdb/unittests/parse-connection-spec-selftests.c
@@ -0,0 +1,249 @@
+/* Self tests for parsing connection specs for GDB, the GNU debugger.
+
+ Copyright (C) 2018 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ 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/>. */
+
+#include "defs.h"
+#include "selftest.h"
+#include "common/netstuff.h"
+#include "diagnostics.h"
+#ifdef USE_WIN32API
+#include <winsock2.h>
+#include <wspiapi.h>
+#else
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <netinet/tcp.h>
+#endif
+
+namespace selftests {
+namespace parse_connection_spec_tests {
+
+/* Auxiliary struct that holds info about a specific test for a
+ connection spec. */
+
+struct parse_conn_test
+{
+ /* The connection spec. */
+ const char *connspec;
+
+ /* Expected result from 'parse_connection_spec'. */
+ parsed_connection_spec expected_result;
+
+ /* True if this test should fail, false otherwise. If true, only
+ the CONNSPEC field should be considered as valid. */
+ bool should_fail;
+
+ /* The expected AI_FAMILY to be found on the 'struct addrinfo'
+ HINT. */
+ int exp_ai_family;
+
+ /* The expected AI_SOCKTYPE to be found on the 'struct addrinfo'
+ HINT. */
+ int exp_ai_socktype;
+
+ /* The expected AI_PROTOCOL to be found on the 'struct addrinfo'
+ HINT. */
+ int exp_ai_protocol;
+};
+
+/* Some defines to help us fill a 'struct parse_conn_test'. */
+
+/* Initialize a full entry. */
+#define INIT_ENTRY(ADDR, EXP_HOST, EXP_PORT, SHOULD_FAIL, EXP_AI_FAMILY, \
+ EXP_AI_SOCKTYPE, EXP_AI_PROTOCOL) \
+ { ADDR, { EXP_HOST, EXP_PORT }, SHOULD_FAIL, EXP_AI_FAMILY, \
+ EXP_AI_SOCKTYPE, EXP_AI_PROTOCOL }
+
+/* Initialize an unprefixed entry. In this case, we don't expect
+ anything on the 'struct addrinfo' HINT. */
+#define INIT_UNPREFIXED_ENTRY(ADDR, EXP_HOST, EXP_PORT) \
+ INIT_ENTRY (ADDR, EXP_HOST, EXP_PORT, false, 0, 0, 0)
+
+/* Initialized an unprefixed IPv6 entry. In this case, we don't
+ expect anything on the 'struct addrinfo' HINT. */
+#define INIT_UNPREFIXED_IPV6_ENTRY(ADDR, EXP_HOST, EXP_PORT) \
+ INIT_ENTRY (ADDR, EXP_HOST, EXP_PORT, false, AF_INET6, 0, 0)
+
+/* Initialize a prefixed entry. */
+#define INIT_PREFIXED_ENTRY(ADDR, EXP_HOST, EXP_PORT, EXP_AI_FAMILY, \
+ EXP_AI_SOCKTYPE, EXP_AI_PROTOCOL) \
+ INIT_ENTRY (ADDR, EXP_HOST, EXP_PORT, false, EXP_AI_FAMILY, \
+ EXP_AI_SOCKTYPE, EXP_AI_PROTOCOL)
+
+/* Initialize an entry prefixed with "tcp4:". */
+#define INIT_PREFIXED_IPV4_TCP(ADDR, EXP_HOST, EXP_PORT) \
+ INIT_PREFIXED_ENTRY (ADDR, EXP_HOST, EXP_PORT, AF_INET, SOCK_STREAM, \
+ IPPROTO_TCP)
+
+/* Initialize an entry prefixed with "tcp6:". */
+#define INIT_PREFIXED_IPV6_TCP(ADDR, EXP_HOST, EXP_PORT) \
+ INIT_PREFIXED_ENTRY (ADDR, EXP_HOST, EXP_PORT, AF_INET6, SOCK_STREAM, \
+ IPPROTO_TCP)
+
+/* Initialize an entry prefixed with "udp4:". */
+#define INIT_PREFIXED_IPV4_UDP(ADDR, EXP_HOST, EXP_PORT) \
+ INIT_PREFIXED_ENTRY (ADDR, EXP_HOST, EXP_PORT, AF_INET, SOCK_DGRAM, \
+ IPPROTO_UDP)
+
+/* Initialize an entry prefixed with "udp6:". */
+#define INIT_PREFIXED_IPV6_UDP(ADDR, EXP_HOST, EXP_PORT) \
+ INIT_PREFIXED_ENTRY (ADDR, EXP_HOST, EXP_PORT, AF_INET6, SOCK_DGRAM, \
+ IPPROTO_UDP)
+
+/* Initialize a bogus entry, i.e., a connection spec that should
+ fail. */
+#define INIT_BOGUS_ENTRY(ADDR) \
+ INIT_ENTRY (ADDR, "", "", true, 0, 0, 0)
+
+/* The variable which holds all of our tests. */
+
+static const parse_conn_test conn_test[] =
+ {
+ /* Unprefixed addresses. */
+
+ /* IPv4, host and port present. */
+ INIT_UNPREFIXED_ENTRY ("127.0.0.1:1234", "127.0.0.1", "1234"),
+ /* IPv4, only host. */
+ INIT_UNPREFIXED_ENTRY ("127.0.0.1", "127.0.0.1", ""),
+ /* IPv4, missing port. */
+ INIT_UNPREFIXED_ENTRY ("127.0.0.1:", "127.0.0.1", ""),
+
+ /* IPv6, host and port present, no brackets. */
+ INIT_UNPREFIXED_ENTRY ("::1:1234", "::1", "1234"),
+ /* IPv6, missing port, no brackets. */
+ INIT_UNPREFIXED_ENTRY ("::1:", "::1", ""),
+ /* IPv6, host and port present, with brackets. */
+ INIT_UNPREFIXED_IPV6_ENTRY ("[::1]:1234", "::1", "1234"),
+ /* IPv6, only host, with brackets. */
+ INIT_UNPREFIXED_IPV6_ENTRY ("[::1]", "::1", ""),
+ /* IPv6, missing port, with brackets. */
+ INIT_UNPREFIXED_IPV6_ENTRY ("[::1]:", "::1", ""),
+
+ /* Unspecified, only port. */
+ INIT_UNPREFIXED_ENTRY (":1234", "localhost", "1234"),
+
+ /* Prefixed addresses. */
+
+ /* Prefixed "tcp4:" IPv4, host and port presents. */
+ INIT_PREFIXED_IPV4_TCP ("tcp4:127.0.0.1:1234", "127.0.0.1", "1234"),
+ /* Prefixed "tcp4:" IPv4, only port. */
+ INIT_PREFIXED_IPV4_TCP ("tcp4::1234", "localhost", "1234"),
+ /* Prefixed "tcp4:" IPv4, only host. */
+ INIT_PREFIXED_IPV4_TCP ("tcp4:127.0.0.1", "127.0.0.1", ""),
+ /* Prefixed "tcp4:" IPv4, missing port. */
+ INIT_PREFIXED_IPV4_TCP ("tcp4:127.0.0.1:", "127.0.0.1", ""),
+
+ /* Prefixed "udp4:" IPv4, host and port present. */
+ INIT_PREFIXED_IPV4_UDP ("udp4:127.0.0.1:1234", "127.0.0.1", "1234"),
+ /* Prefixed "udp4:" IPv4, only port. */
+ INIT_PREFIXED_IPV4_UDP ("udp4::1234", "localhost", "1234"),
+ /* Prefixed "udp4:" IPv4, only host. */
+ INIT_PREFIXED_IPV4_UDP ("udp4:127.0.0.1", "127.0.0.1", ""),
+ /* Prefixed "udp4:" IPv4, missing port. */
+ INIT_PREFIXED_IPV4_UDP ("udp4:127.0.0.1:", "127.0.0.1", ""),
+
+
+ /* Prefixed "tcp6:" IPv6, host and port present. */
+ INIT_PREFIXED_IPV6_TCP ("tcp6:::1:1234", "::1", "1234"),
+ /* Prefixed "tcp6:" IPv6, only port. */
+ INIT_PREFIXED_IPV6_TCP ("tcp6::1234", "localhost", "1234"),
+ /* Prefixed "tcp6:" IPv6, only host. */
+ //INIT_PREFIXED_IPV6_TCP ("tcp6:::1", "::1", ""),
+ /* Prefixed "tcp6:" IPv6, missing port. */
+ INIT_PREFIXED_IPV6_TCP ("tcp6:::1:", "::1", ""),
+
+ /* Prefixed "udp6:" IPv6, host and port present. */
+ INIT_PREFIXED_IPV6_UDP ("udp6:::1:1234", "::1", "1234"),
+ /* Prefixed "udp6:" IPv6, only port. */
+ INIT_PREFIXED_IPV6_UDP ("udp6::1234", "localhost", "1234"),
+ /* Prefixed "udp6:" IPv6, only host. */
+ //INIT_PREFIXED_IPV6_UDP ("udp6:::1", "::1", ""),
+ /* Prefixed "udp6:" IPv6, missing port. */
+ INIT_PREFIXED_IPV6_UDP ("udp6:::1:", "::1", ""),
+
+ /* Prefixed "tcp6:" IPv6 with brackets, host and port present. */
+ INIT_PREFIXED_IPV6_TCP ("tcp6:[::1]:1234", "::1", "1234"),
+ /* Prefixed "tcp6:" IPv6 with brackets, only host. */
+ INIT_PREFIXED_IPV6_TCP ("tcp6:[::1]", "::1", ""),
+ /* Prefixed "tcp6:" IPv6 with brackets, missing port. */
+ INIT_PREFIXED_IPV6_TCP ("tcp6:[::1]:", "::1", ""),
+
+ /* Prefixed "udp6:" IPv6 with brackets, host and port present. */
+ INIT_PREFIXED_IPV6_UDP ("udp6:[::1]:1234", "::1", "1234"),
+ /* Prefixed "udp6:" IPv6 with brackets, only host. */
+ INIT_PREFIXED_IPV6_UDP ("udp6:[::1]", "::1", ""),
+ /* Prefixed "udp6:" IPv6 with brackets, missing port. */
+ INIT_PREFIXED_IPV6_UDP ("udp6:[::1]:", "::1", ""),
+
+
+ /* Bogus addresses. */
+ INIT_BOGUS_ENTRY ("tcp6:[::1]123:44"),
+ INIT_BOGUS_ENTRY ("[::1"),
+ INIT_BOGUS_ENTRY ("tcp6:::1]:"),
+ };
+
+/* Test a connection spec C. */
+
+static void
+test_conn (const parse_conn_test &c)
+{
+ struct addrinfo hint;
+ parsed_connection_spec ret;
+
+ memset (&hint, 0, sizeof (hint));
+
+ TRY
+ {
+ ret = parse_connection_spec (c.connspec, &hint);
+ }
+ CATCH (ex, RETURN_MASK_ERROR)
+ {
+ /* If we caught an error, we should check if this connection
+ spec was supposed to fail. */
+ SELF_CHECK (c.should_fail);
+ return;
+ }
+ END_CATCH
+
+ SELF_CHECK (!c.should_fail);
+ SELF_CHECK (ret.host_str == c.expected_result.host_str);
+ SELF_CHECK (ret.port_str == c.expected_result.port_str);
+ SELF_CHECK (hint.ai_family == c.exp_ai_family);
+ SELF_CHECK (hint.ai_socktype == c.exp_ai_socktype);
+ SELF_CHECK (hint.ai_protocol == c.exp_ai_protocol);
+}
+
+/* Run the tests associated with parsing connection specs. */
+
+static void
+run_tests ()
+{
+ for (const parse_conn_test &c : conn_test)
+ test_conn (c);
+}
+} /* namespace parse_connection_spec_tests */
+} /* namespace selftests */
+
+void
+_initialize_parse_connection_spec_selftests ()
+{
+ selftests::register_test ("parse_connection_spec",
+ selftests::parse_connection_spec_tests::run_tests);
+}