Beispiel #1
0
static int
gdb_os_poll_quit (host_callback *p)
{
  if (deprecated_ui_loop_hook != NULL)
    deprecated_ui_loop_hook (0);

  if (quit_flag)		/* gdb's idea of quit */
    {
      quit_flag = 0;		/* we've stolen it */
      return 1;
    }
  else if (immediate_quit)
    {
      return 1;
    }
  return 0;
}
Beispiel #2
0
static int
do_ser_base_readchar (struct serial *scb, int timeout)
{
  int status;
  int delta;

  /* 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
         deprecated_ui_loop_hook signals that we should exit by
         returning 1.  */

      if (deprecated_ui_loop_hook)
	{
	  if (deprecated_ui_loop_hook (0))
	    return SERIAL_TIMEOUT;
	}

      status = ser_base_wait_for (scb, delta);
      if (timeout > 0)
        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;
	}

      /* We also need to check and consume the stderr because it could
	 come before the stdout for some stubs.  If we just sit and wait
	 for stdout, we would hit a deadlock for that case.  */
      ser_base_read_error_fd (scb, 0);
    }

  if (status < 0)
    return status;

  status = scb->ops->read_prim (scb, BUFSIZ);

  if (status <= 0)
    {
      if (status == 0)
        return SERIAL_EOF;
      else
	/* Got an error from read.  */
	return SERIAL_ERROR;	
    }

  scb->bufcnt = status;
  scb->bufcnt--;
  scb->bufp = scb->buf;
  return *scb->bufp++;
}
Beispiel #3
0
static int
do_unix_readchar (struct serial *scb, int timeout)
{
  int status;
  int delta;

  /* 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
         deprecated_ui_loop_hook signals that we should exit by
         returning 1.  */

      if (deprecated_ui_loop_hook)
	{
	  if (deprecated_ui_loop_hook (0))
	    return SERIAL_TIMEOUT;
	}

      status = ser_unix_wait_for (scb, delta);
      if (timeout > 0)
        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)
    {
      status = read (scb->fd, scb->buf, BUFSIZ);
      if (status != -1 || errno != EINTR)
	break;
    }

  if (status <= 0)
    {
      if (status == 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 */
    }

  scb->bufcnt = status;
  scb->bufcnt--;
  scb->bufp = scb->buf;
  return *scb->bufp++;
}
Beispiel #4
0
static int
do_hardwire_readchar (struct serial *scb, int timeout)
{
  int status, delta;
  int detach = 0;

  if (timeout > 0)
    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
         deprecated_ui_loop_hook signals that we should exit by
         returning 1.  */

      if (deprecated_ui_loop_hook)
	detach = deprecated_ui_loop_hook (0);

      if (detach)
	return SERIAL_TIMEOUT;

      scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
      status = wait_for (scb, delta);

      if (status < 0)
	return status;

      status = read (scb->fd, scb->buf, BUFSIZ);

      if (status <= 0)
	{
	  if (status == 0)
	    {
	      /* Zero characters means timeout (it could also be EOF, but
	         we don't (yet at least) distinguish).  */
	      if (scb->timeout_remaining > 0)
		{
		  timeout = scb->timeout_remaining;
		  continue;
		}
	      else if (scb->timeout_remaining < 0)
		continue;
	      else
		return SERIAL_TIMEOUT;
	    }
	  else if (errno == EINTR)
	    continue;
	  else
	    return SERIAL_ERROR;	/* Got an error from read */
	}

      scb->bufcnt = status;
      scb->bufcnt--;
      scb->bufp = scb->buf;
      return *scb->bufp++;
    }
}
Beispiel #5
0
static int
net_open (struct serial *scb, const char *name)
{
  char *port_str, hostname[100];
  int n, port, tmp;
  int use_udp;
  struct hostent *hostent;
  struct sockaddr_in sockaddr;

  use_udp = 0;
  if (strncmp (name, "udp:", 4) == 0)
    {
      use_udp = 1;
      name = name + 4;
    }
  else if (strncmp (name, "tcp:", 4) == 0)
    name = name + 4;

  port_str = strchr (name, ':');

  if (!port_str)
    error ("net_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 */
  hostname[tmp] = '\000';	/* Tie off host name */
  port = atoi (port_str + 1);

  /* default hostname is localhost */
  if (!hostname[0])
    strcpy (hostname, "localhost");

  hostent = gethostbyname (hostname);
  if (!hostent)
    {
      fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
      errno = ENOENT;
      return -1;
    }

  if (use_udp)
    scb->fd = socket (PF_INET, SOCK_DGRAM, 0);
  else
    scb->fd = socket (PF_INET, SOCK_STREAM, 0);

  if (scb->fd < 0)
    return -1;
  
  sockaddr.sin_family = PF_INET;
  sockaddr.sin_port = htons (port);
  memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
	  sizeof (struct in_addr));

  /* set socket nonblocking */
  tmp = 1;
  ioctl (scb->fd, FIONBIO, &tmp);

  /* Use Non-blocking connect.  connect() will return 0 if connected already. */
  n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));

  if (n < 0 && errno != EINPROGRESS)
    {
      net_close (scb);
      return -1;
    }

  if (n)
    {
      /* looks like we need to wait for the connect */
      struct timeval t;
      fd_set rset, wset;
      int polls = 0;
      FD_ZERO (&rset);

      do 
	{
	  /* While we wait for the connect to complete 
	     poll the UI so it can update or the user can 
	     interrupt. */
	  if (deprecated_ui_loop_hook)
	    {
	      if (deprecated_ui_loop_hook (0))
		{
		  errno = EINTR;
		  net_close (scb);
		  return -1;
		}
	    }
	  
	  FD_SET (scb->fd, &rset);
	  wset = rset;
	  t.tv_sec = 0;
	  t.tv_usec = 1000000 / POLL_INTERVAL;
	  
	  n = select (scb->fd + 1, &rset, &wset, NULL, &t);
	  polls++;
	} 
      while (n == 0 && polls <= TIMEOUT * POLL_INTERVAL);
      if (n < 0 || polls > TIMEOUT * POLL_INTERVAL)
	{
	  if (polls > TIMEOUT * POLL_INTERVAL)
	    errno = ETIMEDOUT;
	  net_close (scb);
	  return -1;
	}
    }

  /* Got something.  Is it an error? */
  {
    int res, err, len;
    len = sizeof(err);
    res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, &err, &len);
    if (res < 0 || err)
      {
	if (err)
	  errno = err;
	net_close (scb);
	return -1;
      }
  } 

  /* turn off nonblocking */
  tmp = 0;
  ioctl (scb->fd, FIONBIO, &tmp);

  if (use_udp == 0)
    {
      /* Disable Nagle algorithm. Needed in some cases. */
      tmp = 1;
      setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
		  (char *)&tmp, sizeof (tmp));
    }

  /* If we don't do this, then GDB simply exits
     when the remote side dies.  */
  signal (SIGPIPE, SIG_IGN);

  return 0;
}
Beispiel #6
0
int
net_open (struct serial *scb, const char *name)
{
    char *port_str, hostname[100];
    int n, port, tmp;
    int use_udp;
    struct hostent *hostent;
    struct sockaddr_in sockaddr;
#ifdef USE_WIN32API
    u_long ioarg;
#else
    int ioarg;
#endif

    use_udp = 0;
    if (strncmp (name, "udp:", 4) == 0)
    {
        use_udp = 1;
        name = name + 4;
    }
    else if (strncmp (name, "tcp:", 4) == 0)
        name = name + 4;

    port_str = strchr (name, ':');

    if (!port_str)
        error (_("net_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 */
    hostname[tmp] = '\000';	/* Tie off host name */
    port = atoi (port_str + 1);

    /* default hostname is localhost */
    if (!hostname[0])
        strcpy (hostname, "localhost");

    hostent = gethostbyname (hostname);
    if (!hostent)
    {
        fprintf_unfiltered (gdb_stderr, "%s: unknown host\n", hostname);
        errno = ENOENT;
        return -1;
    }

    if (use_udp)
        scb->fd = socket (PF_INET, SOCK_DGRAM, 0);
    else
        scb->fd = socket (PF_INET, SOCK_STREAM, 0);

    if (scb->fd < 0)
        return -1;

    sockaddr.sin_family = PF_INET;
    sockaddr.sin_port = htons (port);
    memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
            sizeof (struct in_addr));

    /* set socket nonblocking */
    ioarg = 1;
    ioctl (scb->fd, FIONBIO, &ioarg);

    /* Use Non-blocking connect.  connect() will return 0 if connected already. */
    n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));

    if (n < 0
#ifdef USE_WIN32API
            /* Under Windows, calling "connect" with a non-blocking socket
            results in WSAEWOULDBLOCK, not WSAEINPROGRESS.  */
            && WSAGetLastError() != WSAEWOULDBLOCK
#else
            && errno != EINPROGRESS
#endif
       )
    {
#ifdef USE_WIN32API
        errno = WSAGetLastError();
#endif
        net_close (scb);
        return -1;
    }

    if (n)
    {
        /* looks like we need to wait for the connect */
        struct timeval t;
        fd_set rset, wset, eset;
        int polls = 0;
        FD_ZERO (&rset);

        do
        {
            /* While we wait for the connect to complete,
               poll the UI so it can update or the user can
               interrupt.  */
            if (deprecated_ui_loop_hook)
            {
                if (deprecated_ui_loop_hook (0))
                {
                    errno = EINTR;
                    net_close (scb);
                    return -1;
                }
            }

            FD_SET (scb->fd, &rset);
            wset = rset;
            eset = rset;
            t.tv_sec = 0;
            t.tv_usec = 1000000 / POLL_INTERVAL;

            /* POSIX systems return connection success or failure by signalling
               wset.  Windows systems return success in wset and failure in
               eset.

               We must call select here, rather than gdb_select, because
               the serial structure has not yet been initialized - the
               MinGW select wrapper will not know that this FD refers
               to a socket.  */
            n = select (scb->fd + 1, &rset, &wset, &eset, &t);
            polls++;
        }
        while (n == 0 && polls <= TIMEOUT * POLL_INTERVAL);
        if (n < 0 || polls > TIMEOUT * POLL_INTERVAL)
        {
            if (polls > TIMEOUT * POLL_INTERVAL)
                errno = ETIMEDOUT;
            net_close (scb);
            return -1;
        }
    }

    /* Got something.  Is it an error? */
    {
        int res, err;
        socklen_t len;
        len = sizeof (err);
        /* On Windows, the fourth parameter to getsockopt is a "char *";
           on UNIX systems it is generally "void *".  The cast to "void *"
           is OK everywhere, since in C "void *" can be implicitly
           converted to any pointer type.  */
        res = getsockopt (scb->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len);
        if (res < 0 || err)
        {
            if (err)
                errno = err;
            net_close (scb);
            return -1;
        }
    }

    /* turn off nonblocking */
    ioarg = 0;
    ioctl (scb->fd, FIONBIO, &ioarg);

    if (use_udp == 0)
    {
        /* Disable Nagle algorithm. Needed in some cases. */
        tmp = 1;
        setsockopt (scb->fd, IPPROTO_TCP, TCP_NODELAY,
                    (char *)&tmp, sizeof (tmp));
    }

#ifdef SIGPIPE
    /* If we don't do this, then GDB simply exits
       when the remote side dies.  */
    signal (SIGPIPE, SIG_IGN);
#endif

    return 0;
}