AArch64: View the pseudo V registers as vectors
[deliverable/binutils-gdb.git] / gdb / ser-tcp.c
CommitLineData
f9f87d2c
MK
1/* Serial interface for raw TCP connections on Un*x like systems.
2
42a4f53d 3 Copyright (C) 1992-2019 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
21#include "serial.h"
3eb25fda 22#include "ser-base.h"
0ea3f30e 23#include "ser-tcp.h"
84603566
SL
24#include "gdbcmd.h"
25#include "cli/cli-decode.h"
26#include "cli/cli-setshow.h"
0747795c
TT
27#include "common/filestuff.h"
28#include "common/netstuff.h"
c2c6d25f 29
c906108c 30#include <sys/types.h>
0cf3e697
MH
31
32#ifdef HAVE_SYS_FILIO_H
c378eb4e 33#include <sys/filio.h> /* For FIONBIO. */
0cf3e697
MH
34#endif
35#ifdef HAVE_SYS_IOCTL_H
c378eb4e 36#include <sys/ioctl.h> /* For FIONBIO. */
0cf3e697
MH
37#endif
38
0747795c 39#include "common/gdb_sys_time.h"
b4505029
MM
40
41#ifdef USE_WIN32API
41fa577f
EZ
42#if _WIN32_WINNT < 0x0501
43# undef _WIN32_WINNT
44# define _WIN32_WINNT 0x0501
45#endif
46#include <ws2tcpip.h>
8e7ebaf5 47#ifndef ETIMEDOUT
b4505029 48#define ETIMEDOUT WSAETIMEDOUT
8e7ebaf5 49#endif
6ec2e0f5
SDJ
50/* Gnulib defines close too, but gnulib's replacement
51 doesn't call closesocket unless we import the
52 socketlib module. */
53#undef close
056d7646 54#define close(fd) closesocket (fd)
b4505029
MM
55#define ioctl ioctlsocket
56#else
9eb1356e 57#include <netinet/in.h>
c906108c
SS
58#include <arpa/inet.h>
59#include <netdb.h>
9eb1356e 60#include <sys/socket.h>
c906108c 61#include <netinet/tcp.h>
b4505029 62#endif
c906108c 63
042be3a9 64#include <signal.h>
84603566 65#include "gdb_select.h"
325fac50 66#include <algorithm>
c906108c 67
f9f87d2c
MK
68#ifndef HAVE_SOCKLEN_T
69typedef int socklen_t;
70#endif
71
84603566
SL
72/* For "set tcp" and "show tcp". */
73
74static struct cmd_list_element *tcp_set_cmdlist;
75static struct cmd_list_element *tcp_show_cmdlist;
76
77/* Whether to auto-retry refused connections. */
78
79static int tcp_auto_retry = 1;
80
81/* Timeout period for connections, in seconds. */
82
964b8317 83static unsigned int tcp_retry_limit = 15;
84603566 84
c378eb4e 85/* How many times per second to poll deprecated_ui_loop_hook. */
84603566
SL
86
87#define POLL_INTERVAL 5
88
c7ab0aef
SDJ
89/* Helper function to wait a while. If SOCK is not -1, wait on its
90 file descriptor. Otherwise just wait on a timeout, updating
91 *POLLS. Returns -1 on timeout or interrupt, otherwise the value of
92 select. */
84603566
SL
93
94static int
c7ab0aef 95wait_for_connect (int sock, unsigned int *polls)
84603566
SL
96{
97 struct timeval t;
98 int n;
99
100 /* While we wait for the connect to complete,
101 poll the UI so it can update or the user can
102 interrupt. */
103 if (deprecated_ui_loop_hook && deprecated_ui_loop_hook (0))
104 {
105 errno = EINTR;
106 return -1;
107 }
108
109 /* Check for timeout. */
110 if (*polls > tcp_retry_limit * POLL_INTERVAL)
111 {
112 errno = ETIMEDOUT;
113 return -1;
114 }
115
116 /* Back off to polling once per second after the first POLL_INTERVAL
117 polls. */
118 if (*polls < POLL_INTERVAL)
119 {
120 t.tv_sec = 0;
121 t.tv_usec = 1000000 / POLL_INTERVAL;
122 }
123 else
124 {
125 t.tv_sec = 1;
126 t.tv_usec = 0;
127 }
128
c7ab0aef 129 if (sock >= 0)
84603566
SL
130 {
131 fd_set rset, wset, eset;
433759f7 132
84603566 133 FD_ZERO (&rset);
c7ab0aef 134 FD_SET (sock, &rset);
84603566
SL
135 wset = rset;
136 eset = rset;
c7ab0aef 137
84603566
SL
138 /* POSIX systems return connection success or failure by signalling
139 wset. Windows systems return success in wset and failure in
140 eset.
c7ab0aef 141
84603566
SL
142 We must call select here, rather than gdb_select, because
143 the serial structure has not yet been initialized - the
144 MinGW select wrapper will not know that this FD refers
145 to a socket. */
c7ab0aef 146 n = select (sock + 1, &rset, &wset, &eset, &t);
84603566
SL
147 }
148 else
149 /* Use gdb_select here, since we have no file descriptors, and on
150 Windows, plain select doesn't work in that case. */
151 n = gdb_select (0, NULL, NULL, NULL, &t);
152
153 /* If we didn't time out, only count it as one poll. */
154 if (n > 0 || *polls < POLL_INTERVAL)
155 (*polls)++;
156 else
157 (*polls) += POLL_INTERVAL;
158
159 return n;
160}
7c7a201a 161
c7ab0aef
SDJ
162/* Try to connect to the host represented by AINFO. If the connection
163 succeeds, return its socket. Otherwise, return -1 and set ERRNO
164 accordingly. POLLS is used when 'connect' returns EINPROGRESS, and
165 we need to invoke 'wait_for_connect' to obtain the status. */
c906108c 166
c7ab0aef
SDJ
167static int
168try_connect (const struct addrinfo *ainfo, unsigned int *polls)
c906108c 169{
c7ab0aef
SDJ
170 int sock = gdb_socket_cloexec (ainfo->ai_family, ainfo->ai_socktype,
171 ainfo->ai_protocol);
84603566 172
c7ab0aef 173 if (sock < 0)
7c7a201a 174 return -1;
c7ab0aef 175
c378eb4e 176 /* Set socket nonblocking. */
64b58472
SDJ
177#ifdef USE_WIN32API
178 u_long ioarg = 1;
179#else
c7ab0aef 180 int ioarg = 1;
64b58472 181#endif
c7ab0aef
SDJ
182
183 ioctl (sock, FIONBIO, &ioarg);
c906108c 184
3e43a32a 185 /* Use Non-blocking connect. connect() will return 0 if connected
c378eb4e 186 already. */
c7ab0aef 187 if (connect (sock, ainfo->ai_addr, ainfo->ai_addrlen) < 0)
84603566 188 {
b4505029 189#ifdef USE_WIN32API
84603566 190 int err = WSAGetLastError();
b4505029 191#else
84603566 192 int err = errno;
b4505029 193#endif
84603566 194
c7ab0aef
SDJ
195 /* If we've got a "connection refused" error, just return
196 -1. The caller will know what to do. */
197 if (
b4505029 198#ifdef USE_WIN32API
c7ab0aef 199 err == WSAECONNREFUSED
84603566 200#else
c7ab0aef 201 err == ECONNREFUSED
b4505029 202#endif
c7ab0aef 203 )
84603566 204 {
c7ab0aef
SDJ
205 close (sock);
206 errno = err;
207 return -1;
84603566 208 }
c906108c 209
84603566 210 if (
c7ab0aef
SDJ
211 /* Any other error (except EINPROGRESS) will be "swallowed"
212 here. We return without specifying a return value, and
213 set errno if the caller wants to inspect what
214 happened. */
84603566
SL
215#ifdef USE_WIN32API
216 /* Under Windows, calling "connect" with a non-blocking socket
217 results in WSAEWOULDBLOCK, not WSAEINPROGRESS. */
218 err != WSAEWOULDBLOCK
219#else
220 err != EINPROGRESS
221#endif
222 )
223 {
c7ab0aef 224 close (sock);
84603566 225 errno = err;
84603566
SL
226 return -1;
227 }
7c7a201a 228
c378eb4e 229 /* Looks like we need to wait for the connect. */
c7ab0aef
SDJ
230 int n;
231
232 do
233 n = wait_for_connect (sock, polls);
84603566 234 while (n == 0);
c7ab0aef 235
84603566 236 if (n < 0)
7c7a201a 237 {
c7ab0aef
SDJ
238 int saved_errno = errno;
239
240 /* A negative value here means that we either timed out or
241 got interrupted by the user. Just return. */
242 close (sock);
243 errno = saved_errno;
7c7a201a
MH
244 return -1;
245 }
246 }
c906108c 247
c378eb4e 248 /* Got something. Is it an error? */
c7ab0aef
SDJ
249 int err;
250 socklen_t len = sizeof (err);
251
252 /* On Windows, the fourth parameter to getsockopt is a "char *";
253 on UNIX systems it is generally "void *". The cast to "char *"
254 is OK everywhere, since in C++ any data pointer type can be
255 implicitly converted to "void *". */
256 int ret = getsockopt (sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len);
257
258 if (ret < 0)
259 {
260 int saved_errno = errno;
261
262 close (sock);
263 errno = saved_errno;
264 return -1;
265 }
266 else if (ret == 0 && err != 0)
267 {
268 close (sock);
269 errno = err;
270 return -1;
271 }
272
273 /* The connection succeeded. Return the socket. */
274 return sock;
275}
276
277/* Open a tcp socket. */
278
279int
280net_open (struct serial *scb, const char *name)
281{
282 struct addrinfo hint;
283 struct addrinfo *ainfo;
284
285 memset (&hint, 0, sizeof (hint));
286 /* Assume no prefix will be passed, therefore we should use
287 AF_UNSPEC. */
288 hint.ai_family = AF_UNSPEC;
289 hint.ai_socktype = SOCK_STREAM;
290 hint.ai_protocol = IPPROTO_TCP;
291
292 parsed_connection_spec parsed = parse_connection_spec (name, &hint);
293
294 if (parsed.port_str.empty ())
295 error (_("Missing port on hostname '%s'"), name);
296
297 int r = getaddrinfo (parsed.host_str.c_str (),
298 parsed.port_str.c_str (),
299 &hint, &ainfo);
300
301 if (r != 0)
302 {
303 fprintf_unfiltered (gdb_stderr, _("%s: cannot resolve name: %s\n"),
304 name, gai_strerror (r));
305 errno = ENOENT;
306 return -1;
307 }
308
309 scoped_free_addrinfo free_ainfo (ainfo);
310
311 /* Flag to indicate whether we've got a connection refused. It will
312 be true if any of the connections tried was refused. */
313 bool got_connrefused;
314 /* If a connection succeeeds, SUCCESS_AINFO will point to the
315 'struct addrinfo' that succeed. */
316 struct addrinfo *success_ainfo = NULL;
317 unsigned int polls = 0;
318
319 /* Assume the worst. */
320 scb->fd = -1;
321
322 do
323 {
324 got_connrefused = false;
325
326 for (struct addrinfo *iter = ainfo; iter != NULL; iter = iter->ai_next)
327 {
328 /* Iterate over the list of possible addresses to connect
329 to. For each, we'll try to connect and see if it
330 succeeds. */
331 int sock = try_connect (iter, &polls);
332
333 if (sock >= 0)
334 {
335 /* We've gotten a successful connection. Save its
336 'struct addrinfo', the socket, and break. */
337 success_ainfo = iter;
338 scb->fd = sock;
339 break;
340 }
341 else if (
84603566 342#ifdef USE_WIN32API
c7ab0aef 343 errno == WSAECONNREFUSED
84603566 344#else
c7ab0aef 345 errno == ECONNREFUSED
84603566 346#endif
c7ab0aef
SDJ
347 )
348 got_connrefused = true;
349 }
350 }
351 /* Just retry if:
352
353 - tcp_auto_retry is true, and
354 - We haven't gotten a connection yet, and
355 - Any of our connection attempts returned with ECONNREFUSED, and
356 - wait_for_connect signals that we can keep going. */
357 while (tcp_auto_retry
358 && success_ainfo == NULL
359 && got_connrefused
360 && wait_for_connect (-1, &polls) >= 0);
361
362 if (success_ainfo == NULL)
363 {
364 net_close (scb);
365 return -1;
366 }
9db8d71f 367
c378eb4e 368 /* Turn off nonblocking. */
c7ab0aef
SDJ
369#ifdef USE_WIN32API
370 u_long ioarg = 0;
371#else
372 int ioarg = 0;
373#endif
374
b4505029 375 ioctl (scb->fd, FIONBIO, &ioarg);
7c7a201a 376
425699f5 377 if (success_ainfo->ai_protocol == IPPROTO_TCP)
9db8d71f 378 {
c378eb4e 379 /* Disable Nagle algorithm. Needed in some cases. */
c7ab0aef
SDJ
380 int tmp = 1;
381
9db8d71f 382 setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
c7ab0aef 383 (char *) &tmp, sizeof (tmp));
9db8d71f
DJ
384 }
385
6d318c73 386#ifdef SIGPIPE
7c7a201a
MH
387 /* If we don't do this, then GDB simply exits
388 when the remote side dies. */
389 signal (SIGPIPE, SIG_IGN);
6d318c73 390#endif
c906108c
SS
391
392 return 0;
393}
394
0ea3f30e 395void
9db8d71f 396net_close (struct serial *scb)
c906108c 397{
363a6e9f 398 if (scb->fd == -1)
c906108c
SS
399 return;
400
c5aa993b 401 close (scb->fd);
c906108c
SS
402 scb->fd = -1;
403}
404
0ea3f30e 405int
b4505029
MM
406net_read_prim (struct serial *scb, size_t count)
407{
c49e7f76
PA
408 /* Need to cast to silence -Wpointer-sign on MinGW, as Winsock's
409 'recv' takes 'char *' as second argument, while 'scb->buf' is
410 'unsigned char *'. */
69e976f8 411 return recv (scb->fd, (char *) scb->buf, count, 0);
b4505029
MM
412}
413
0ea3f30e 414int
b4505029
MM
415net_write_prim (struct serial *scb, const void *buf, size_t count)
416{
69e976f8
PA
417 /* On Windows, the second parameter to send is a "const char *"; on
418 UNIX systems it is generally "const void *". The cast to "const
419 char *" is OK everywhere, since in C++ any data pointer type can
420 be implicitly converted to "const void *". */
421 return send (scb->fd, (const char *) buf, count, 0);
b4505029
MM
422}
423
8775bb90
MS
424int
425ser_tcp_send_break (struct serial *scb)
426{
c378eb4e 427 /* Send telnet IAC and BREAK characters. */
8775bb90
MS
428 return (serial_write (scb, "\377\363", 2));
429}
430
84603566
SL
431/* Support for "set tcp" and "show tcp" commands. */
432
433static void
981a3fb3 434set_tcp_cmd (const char *args, int from_tty)
84603566 435{
635c7e8a 436 help_list (tcp_set_cmdlist, "set tcp ", all_commands, gdb_stdout);
84603566
SL
437}
438
439static void
981a3fb3 440show_tcp_cmd (const char *args, int from_tty)
84603566 441{
635c7e8a 442 help_list (tcp_show_cmdlist, "show tcp ", all_commands, gdb_stdout);
84603566
SL
443}
444
12e8c7d7
TT
445#ifndef USE_WIN32API
446
447/* The TCP ops. */
448
449static const struct serial_ops tcp_ops =
450{
451 "tcp",
452 net_open,
453 net_close,
454 NULL,
455 ser_base_readchar,
456 ser_base_write,
457 ser_base_flush_output,
458 ser_base_flush_input,
459 ser_tcp_send_break,
460 ser_base_raw,
461 ser_base_get_tty_state,
462 ser_base_copy_tty_state,
463 ser_base_set_tty_state,
464 ser_base_print_tty_state,
12e8c7d7
TT
465 ser_base_setbaudrate,
466 ser_base_setstopbits,
236af5e3 467 ser_base_setparity,
12e8c7d7
TT
468 ser_base_drain_output,
469 ser_base_async,
470 net_read_prim,
471 net_write_prim
472};
473
474#endif /* USE_WIN32API */
84603566 475
c906108c 476void
c2c6d25f 477_initialize_ser_tcp (void)
c906108c 478{
b4505029 479#ifdef USE_WIN32API
0ea3f30e
DJ
480 /* Do nothing; the TCP serial operations will be initialized in
481 ser-mingw.c. */
0ea3f30e 482#else
12e8c7d7 483 serial_add_interface (&tcp_ops);
0ea3f30e 484#endif /* USE_WIN32API */
84603566
SL
485
486 add_prefix_cmd ("tcp", class_maintenance, set_tcp_cmd, _("\
487TCP protocol specific variables\n\
488Configure variables specific to remote TCP connections"),
489 &tcp_set_cmdlist, "set tcp ",
490 0 /* allow-unknown */, &setlist);
491 add_prefix_cmd ("tcp", class_maintenance, show_tcp_cmd, _("\
492TCP protocol specific variables\n\
493Configure variables specific to remote TCP connections"),
494 &tcp_show_cmdlist, "show tcp ",
495 0 /* allow-unknown */, &showlist);
496
497 add_setshow_boolean_cmd ("auto-retry", class_obscure,
498 &tcp_auto_retry, _("\
499Set auto-retry on socket connect"), _("\
500Show auto-retry on socket connect"),
501 NULL, NULL, NULL,
502 &tcp_set_cmdlist, &tcp_show_cmdlist);
503
504 add_setshow_uinteger_cmd ("connect-timeout", class_obscure,
505 &tcp_retry_limit, _("\
f81d1120
PA
506Set timeout limit in seconds for socket connection"), _("\
507Show timeout limit in seconds for socket connection"), _("\
508If set to \"unlimited\", GDB will keep attempting to establish a\n\
509connection forever, unless interrupted with Ctrl-c.\n\
510The default is 15 seconds."),
511 NULL, NULL,
512 &tcp_set_cmdlist, &tcp_show_cmdlist);
c906108c 513}
This page took 1.81494 seconds and 4 git commands to generate.