X-Git-Url: https://repo.jachan.dev/binutils.git/blobdiff_plain/3ffbdf15324b373b8a941dc58e421d1420b80ffc..8b237a8965b9c4f4c0ec0bb13571b45fef6ed8ce:/gdb/ser-tcp.c diff --git a/gdb/ser-tcp.c b/gdb/ser-tcp.c index 279cbd0d71..83a84fc7ac 100644 --- a/gdb/ser-tcp.c +++ b/gdb/ser-tcp.c @@ -1,21 +1,22 @@ /* Serial interface for raw TCP connections on Un*x like systems - Copyright 1992, 1993 Free Software Foundation, Inc. + Copyright 1992, 1993, 1998 Free Software Foundation, Inc. -This file is part of GDB. + 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 2 of the License, or -(at your option) any later version. + 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 2 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. + 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, write to the Free Software -Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ #include "defs.h" #include "serial.h" @@ -25,6 +26,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include +#ifdef HAVE_UNISTD_H +#include +#endif #ifndef __CYGWIN32__ #include @@ -33,10 +37,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "signals.h" #include "gdb_string.h" +extern int (*ui_loop_hook) PARAMS ((int)); + struct tcp_ttystate -{ - int bogus; -}; + { + int bogus; + }; static int tcp_open PARAMS ((serial_t scb, const char *name)); static void tcp_raw PARAMS ((serial_t scb)); @@ -54,10 +60,12 @@ static int tcp_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate, serial_ttystate)); static void tcp_print_tty_state PARAMS ((serial_t, serial_ttystate)); +void _initialize_ser_tcp PARAMS ((void)); + /* Open up a raw tcp socket */ static int -tcp_open(scb, name) +tcp_open (scb, name) serial_t scb; const char *name; { @@ -73,10 +81,10 @@ tcp_open(scb, name) port_str = strchr (name, ':'); if (!port_str) - error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */ + error ("tcp_open: No colon in host name!"); /* Shouldn't ever happen */ tmp = min (port_str - name, (int) sizeof hostname - 1); - strncpy (hostname, name, tmp); /* Don't want colon */ + strncpy (hostname, name, tmp); /* Don't want colon */ hostname[tmp] = '\000'; /* Tie off host name */ port = atoi (port_str + 1); @@ -97,18 +105,18 @@ tcp_open(scb, name) /* Allow rapid reuse of this port. */ tmp = 1; - setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&tmp, sizeof(tmp)); + setsockopt (scb->fd, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp)); /* Enable TCP keep alive process. */ tmp = 1; - setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&tmp, sizeof(tmp)); + setsockopt (scb->fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp)); sockaddr.sin_family = PF_INET; - sockaddr.sin_port = htons(port); + sockaddr.sin_port = htons (port); memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr, sizeof (struct in_addr)); - if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof(sockaddr))) + if (!connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr))) break; close (scb->fd); @@ -129,34 +137,34 @@ tcp_open(scb, name) tmp = 1; if (setsockopt (scb->fd, protoent->p_proto, TCP_NODELAY, - (char *)&tmp, sizeof(tmp))) + (char *) &tmp, sizeof (tmp))) return -1; - signal(SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits + signal (SIGPIPE, SIG_IGN); /* If we don't do this, then GDB simply exits when the remote side dies. */ return 0; } static serial_ttystate -tcp_get_tty_state(scb) +tcp_get_tty_state (scb) serial_t scb; { struct tcp_ttystate *state; - state = (struct tcp_ttystate *)xmalloc(sizeof *state); + state = (struct tcp_ttystate *) xmalloc (sizeof *state); - return (serial_ttystate)state; + return (serial_ttystate) state; } static int -tcp_set_tty_state(scb, ttystate) +tcp_set_tty_state (scb, ttystate) serial_t scb; serial_ttystate ttystate; { struct tcp_ttystate *state; - state = (struct tcp_ttystate *)ttystate; + state = (struct tcp_ttystate *) ttystate; return 0; } @@ -169,7 +177,7 @@ tcp_return_0 (scb) } static void -tcp_raw(scb) +tcp_raw (scb) serial_t scb; { return; /* Always in raw mode */ @@ -183,7 +191,7 @@ tcp_raw(scb) */ static int -wait_for(scb, timeout) +wait_for (scb, timeout) serial_t scb; int timeout; { @@ -197,23 +205,25 @@ wait_for(scb, timeout) tv.tv_sec = timeout; tv.tv_usec = 0; - FD_SET(scb->fd, &readfds); - FD_SET(scb->fd, &exceptfds); + FD_SET (scb->fd, &readfds); + FD_SET (scb->fd, &exceptfds); while (1) { if (timeout >= 0) - numfds = select(scb->fd+1, &readfds, 0, &exceptfds, &tv); + numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv); else - numfds = select(scb->fd+1, &readfds, 0, &exceptfds, 0); + numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0); if (numfds <= 0) - if (numfds == 0) - return SERIAL_TIMEOUT; - else if (errno == EINTR) - continue; - else - return SERIAL_ERROR; /* Got an error from select or poll */ + { + if (numfds == 0) + return SERIAL_TIMEOUT; + else if (errno == EINTR) + continue; + else + return SERIAL_ERROR; /* Got an error from select or poll */ + } return 0; } @@ -225,34 +235,79 @@ wait_for(scb, timeout) dead, or -3 for any other error (see errno in that case). */ static int -tcp_readchar(scb, timeout) +tcp_readchar (scb, timeout) serial_t scb; int timeout; { int status; + int delta; if (scb->bufcnt-- > 0) return *scb->bufp++; - status = wait_for(scb, timeout); + /* We have to be able to keep the GUI alive here, so we break the original + timeout into steps of 1 second, running the "keep the GUI alive" hook + each time through the loop. + + Also, timeout = 0 means to poll, so we just set the delta to 0, so we + will only go through the loop once. */ + + delta = (timeout == 0 ? 0 : 1); + while (1) + { + + /* N.B. The UI may destroy our world (for instance by calling + remote_stop,) in which case we want to get out of here as + quickly as possible. It is not safe to touch scb, since + someone else might have freed it. The ui_loop_hook signals that + we should exit by returning 1. */ + + if (ui_loop_hook) + { + if (ui_loop_hook (0)) + return SERIAL_TIMEOUT; + } + + status = wait_for (scb, delta); + timeout -= delta; + + /* If we got a character or an error back from wait_for, then we can + break from the loop before the timeout is completed. */ + + if (status != SERIAL_TIMEOUT) + { + break; + } + + /* If we have exhausted the original timeout, then generate + a SERIAL_TIMEOUT, and pass it out of the loop. */ + + else if (timeout == 0) + { + status == SERIAL_TIMEOUT; + break; + } + } if (status < 0) return status; while (1) { - scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ); + scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ); if (scb->bufcnt != -1 || errno != EINTR) break; } if (scb->bufcnt <= 0) - if (scb->bufcnt == 0) - return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to + { + if (scb->bufcnt == 0) + return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to distinguish between EOF & timeouts someday] */ - else - return SERIAL_ERROR; /* Got an error from read */ + else + return SERIAL_ERROR; /* Got an error from read */ + } scb->bufcnt--; scb->bufp = scb->buf; @@ -278,7 +333,7 @@ tcp_print_tty_state (scb, ttystate) } static int -tcp_setbaudrate(scb, rate) +tcp_setbaudrate (scb, rate) serial_t scb; int rate; { @@ -286,7 +341,7 @@ tcp_setbaudrate(scb, rate) } static int -tcp_setstopbits(scb, num) +tcp_setstopbits (scb, num) serial_t scb; int num; { @@ -294,7 +349,7 @@ tcp_setstopbits(scb, num) } static int -tcp_write(scb, str, len) +tcp_write (scb, str, len) serial_t scb; const char *str; int len; @@ -303,7 +358,7 @@ tcp_write(scb, str, len) while (len > 0) { - cc = write(scb->fd, str, len); + cc = write (scb->fd, str, len); if (cc < 0) return 1; @@ -314,13 +369,13 @@ tcp_write(scb, str, len) } static void -tcp_close(scb) +tcp_close (scb) serial_t scb; { if (scb->fd < 0) return; - close(scb->fd); + close (scb->fd); scb->fd = -1; } @@ -332,9 +387,9 @@ static struct serial_ops tcp_ops = tcp_close, tcp_readchar, tcp_write, - tcp_return_0, /* flush output */ - tcp_return_0, /* flush input */ - tcp_return_0, /* send break */ + tcp_return_0, /* flush output */ + tcp_return_0, /* flush input */ + tcp_return_0, /* send break */ tcp_raw, tcp_get_tty_state, tcp_set_tty_state, @@ -342,7 +397,7 @@ static struct serial_ops tcp_ops = tcp_noflush_set_tty_state, tcp_setbaudrate, tcp_setstopbits, - tcp_return_0, /* wait for output to drain */ + tcp_return_0, /* wait for output to drain */ }; void