gdb/ser-base.c - gdb

Global variables defined

Functions defined

Source code

  1. /* Generic serial interface functions.

  2.    Copyright (C) 1992-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "serial.h"
  16. #include "ser-base.h"
  17. #include "event-loop.h"

  18. #include "gdb_select.h"
  19. #include <sys/time.h>
  20. #ifdef USE_WIN32API
  21. #include <winsock2.h>
  22. #endif


  23. static timer_handler_func push_event;
  24. static handler_func fd_event;

  25. /* Event handling for ASYNC serial code.

  26.    At any time the SERIAL device either: has an empty FIFO and is
  27.    waiting on a FD event; or has a non-empty FIFO/error condition and
  28.    is constantly scheduling timer events.

  29.    ASYNC only stops pestering its client when it is de-async'ed or it
  30.    is told to go away.  */

  31. /* Value of scb->async_state: */
  32. enum {
  33.   /* >= 0 (TIMER_SCHEDULED) */
  34.   /* The ID of the currently scheduled timer event.  This state is
  35.      rarely encountered.  Timer events are one-off so as soon as the
  36.      event is delivered the state is shanged to NOTHING_SCHEDULED.  */
  37.   FD_SCHEDULED = -1,
  38.   /* The fd_event() handler is scheduled.  It is called when ever the
  39.      file descriptor becomes ready.  */
  40.   NOTHING_SCHEDULED = -2
  41.   /* Either no task is scheduled (just going into ASYNC mode) or a
  42.      timer event has just gone off and the current state has been
  43.      forced into nothing scheduled.  */
  44. };

  45. /* Identify and schedule the next ASYNC task based on scb->async_state
  46.    and scb->buf* (the input FIFO).  A state machine is used to avoid
  47.    the need to make redundant calls into the event-loop - the next
  48.    scheduled task is only changed when needed.  */

  49. static void
  50. reschedule (struct serial *scb)
  51. {
  52.   if (serial_is_async_p (scb))
  53.     {
  54.       int next_state;

  55.       switch (scb->async_state)
  56.         {
  57.         case FD_SCHEDULED:
  58.           if (scb->bufcnt == 0)
  59.             next_state = FD_SCHEDULED;
  60.           else
  61.             {
  62.               delete_file_handler (scb->fd);
  63.               next_state = create_timer (0, push_event, scb);
  64.             }
  65.           break;
  66.         case NOTHING_SCHEDULED:
  67.           if (scb->bufcnt == 0)
  68.             {
  69.               add_file_handler (scb->fd, fd_event, scb);
  70.               next_state = FD_SCHEDULED;
  71.             }
  72.           else
  73.             {
  74.               next_state = create_timer (0, push_event, scb);
  75.             }
  76.           break;
  77.         default: /* TIMER SCHEDULED */
  78.           if (scb->bufcnt == 0)
  79.             {
  80.               delete_timer (scb->async_state);
  81.               add_file_handler (scb->fd, fd_event, scb);
  82.               next_state = FD_SCHEDULED;
  83.             }
  84.           else
  85.             next_state = scb->async_state;
  86.           break;
  87.         }
  88.       if (serial_debug_p (scb))
  89.         {
  90.           switch (next_state)
  91.             {
  92.             case FD_SCHEDULED:
  93.               if (scb->async_state != FD_SCHEDULED)
  94.                 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
  95.                                     scb->fd);
  96.               break;
  97.             default: /* TIMER SCHEDULED */
  98.               if (scb->async_state == FD_SCHEDULED)
  99.                 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
  100.                                     scb->fd);
  101.               break;
  102.             }
  103.         }
  104.       scb->async_state = next_state;
  105.     }
  106. }

  107. /* Run the SCB's async handle, and reschedule, if the handler doesn't
  108.    close SCB.  */

  109. static void
  110. run_async_handler_and_reschedule (struct serial *scb)
  111. {
  112.   int is_open;

  113.   /* Take a reference, so a serial_close call within the handler
  114.      doesn't make SCB a dangling pointer.  */
  115.   serial_ref (scb);

  116.   /* Run the handler.  */
  117.   scb->async_handler (scb, scb->async_context);

  118.   is_open = serial_is_open (scb);
  119.   serial_unref (scb);

  120.   /* Get ready for more, if not already closed.  */
  121.   if (is_open)
  122.     reschedule (scb);
  123. }

  124. /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
  125.    is no pending error).  As soon as data arrives, it is read into the
  126.    input FIFO and the client notified.  The client should then drain
  127.    the FIFO using readchar().  If the FIFO isn't immediatly emptied,
  128.    push_event() is used to nag the client until it is.  */

  129. static void
  130. fd_event (int error, void *context)
  131. {
  132.   struct serial *scb = context;
  133.   if (error != 0)
  134.     {
  135.       scb->bufcnt = SERIAL_ERROR;
  136.     }
  137.   else if (scb->bufcnt == 0)
  138.     {
  139.       /* Prime the input FIFO.  The readchar() function is used to
  140.          pull characters out of the buffer.  See also
  141.          generic_readchar().  */
  142.       int nr;
  143.       nr = scb->ops->read_prim (scb, BUFSIZ);
  144.       if (nr == 0)
  145.         {
  146.           scb->bufcnt = SERIAL_EOF;
  147.         }
  148.       else if (nr > 0)
  149.         {
  150.           scb->bufcnt = nr;
  151.           scb->bufp = scb->buf;
  152.         }
  153.       else
  154.         {
  155.           scb->bufcnt = SERIAL_ERROR;
  156.         }
  157.     }
  158.   run_async_handler_and_reschedule (scb);
  159. }

  160. /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
  161.    error).  Nag the client until all the data has been read.  In the
  162.    case of errors, the client will need to close or de-async the
  163.    device before naging stops.  */

  164. static void
  165. push_event (void *context)
  166. {
  167.   struct serial *scb = context;

  168.   scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
  169.   run_async_handler_and_reschedule (scb);
  170. }

  171. /* Wait for input on scb, with timeout seconds.  Returns 0 on success,
  172.    otherwise SERIAL_TIMEOUT or SERIAL_ERROR.  */

  173. static int
  174. ser_base_wait_for (struct serial *scb, int timeout)
  175. {
  176.   while (1)
  177.     {
  178.       int numfds;
  179.       struct timeval tv;
  180.       fd_set readfds, exceptfds;

  181.       /* NOTE: Some OS's can scramble the READFDS when the select()
  182.          call fails (ex the kernel with Red Hat 5.2).  Initialize all
  183.          arguments before each call.  */

  184.       tv.tv_sec = timeout;
  185.       tv.tv_usec = 0;

  186.       FD_ZERO (&readfds);
  187.       FD_ZERO (&exceptfds);
  188.       FD_SET (scb->fd, &readfds);
  189.       FD_SET (scb->fd, &exceptfds);

  190.       if (timeout >= 0)
  191.         numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
  192.       else
  193.         numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, 0);

  194.       if (numfds <= 0)
  195.         {
  196.           if (numfds == 0)
  197.             return SERIAL_TIMEOUT;
  198.           else if (errno == EINTR)
  199.             continue;
  200.           else
  201.             return SERIAL_ERROR;        /* Got an error from select or
  202.                                            poll.  */
  203.         }

  204.       return 0;
  205.     }
  206. }

  207. /* Read any error output we might have.  */

  208. static void
  209. ser_base_read_error_fd (struct serial *scb, int close_fd)
  210. {
  211.   if (scb->error_fd != -1)
  212.     {
  213.       ssize_t s;
  214.       char buf[GDB_MI_MSG_WIDTH + 1];

  215.       for (;;)
  216.         {
  217.           char *current;
  218.           char *newline;
  219.           int to_read = GDB_MI_MSG_WIDTH;
  220.           int num_bytes = -1;

  221.           if (scb->ops->avail)
  222.             num_bytes = (scb->ops->avail)(scb, scb->error_fd);

  223.           if (num_bytes != -1)
  224.             to_read = (num_bytes < to_read) ? num_bytes : to_read;

  225.           if (to_read == 0)
  226.             break;

  227.           s = read (scb->error_fd, &buf, to_read);
  228.           if ((s == -1) || (s == 0 && !close_fd))
  229.             break;

  230.           if (s == 0 && close_fd)
  231.             {
  232.               /* End of file.  */
  233.               close (scb->error_fd);
  234.               scb->error_fd = -1;
  235.               break;
  236.             }

  237.           /* In theory, embedded newlines are not a problem.
  238.              But for MI, we want each output line to have just
  239.              one newline for legibility.  So output things
  240.              in newline chunks.  */
  241.           gdb_assert (s > 0 && s <= GDB_MI_MSG_WIDTH);
  242.           buf[s] = '\0';
  243.           current = buf;
  244.           while ((newline = strstr (current, "\n")) != NULL)
  245.             {
  246.               *newline = '\0';
  247.               fputs_unfiltered (current, gdb_stderr);
  248.               fputs_unfiltered ("\n", gdb_stderr);
  249.               current = newline + 1;
  250.             }

  251.           fputs_unfiltered (current, gdb_stderr);
  252.        }
  253.     }
  254. }

  255. /* Read a character with user-specified timeoutTIMEOUT is number of seconds
  256.    to wait, or -1 to wait forever.  Use timeout of 0 to effect a poll.  Returns
  257.    char if successful.  Returns -2 if timeout expired, EOF if line dropped
  258.    dead, or -3 for any other error (see errno in that case).  */

  259. static int
  260. do_ser_base_readchar (struct serial *scb, int timeout)
  261. {
  262.   int status;
  263.   int delta;

  264.   /* We have to be able to keep the GUI alive here, so we break the
  265.      original timeout into steps of 1 second, running the "keep the
  266.      GUI alive" hook each time through the loop.

  267.      Also, timeout = 0 means to poll, so we just set the delta to 0,
  268.      so we will only go through the loop once.  */

  269.   delta = (timeout == 0 ? 0 : 1);
  270.   while (1)
  271.     {
  272.       /* N.B. The UI may destroy our world (for instance by calling
  273.          remote_stop,) in which case we want to get out of here as
  274.          quickly as possible.  It is not safe to touch scb, since
  275.          someone else might have freed it.  The
  276.          deprecated_ui_loop_hook signals that we should exit by
  277.          returning 1.  */

  278.       if (deprecated_ui_loop_hook)
  279.         {
  280.           if (deprecated_ui_loop_hook (0))
  281.             return SERIAL_TIMEOUT;
  282.         }

  283.       status = ser_base_wait_for (scb, delta);
  284.       if (timeout > 0)
  285.         timeout -= delta;

  286.       /* If we got a character or an error back from wait_for, then we can
  287.          break from the loop before the timeout is completed.  */
  288.       if (status != SERIAL_TIMEOUT)
  289.         break;

  290.       /* If we have exhausted the original timeout, then generate
  291.          a SERIAL_TIMEOUT, and pass it out of the loop.  */
  292.       else if (timeout == 0)
  293.         {
  294.           status = SERIAL_TIMEOUT;
  295.           break;
  296.         }

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

  302.   if (status < 0)
  303.     return status;

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

  305.   if (status <= 0)
  306.     {
  307.       if (status == 0)
  308.         return SERIAL_EOF;
  309.       else
  310.         /* Got an error from read.  */
  311.         return SERIAL_ERROR;
  312.     }

  313.   scb->bufcnt = status;
  314.   scb->bufcnt--;
  315.   scb->bufp = scb->buf;
  316.   return *scb->bufp++;
  317. }

  318. /* Perform operations common to both old and new readchar.  */

  319. /* Return the next character from the input FIFO.  If the FIFO is
  320.    empty, call the SERIAL specific routine to try and read in more
  321.    characters.

  322.    Initially data from the input FIFO is returned (fd_event()
  323.    pre-reads the input into that FIFO.  Once that has been emptied,
  324.    further data is obtained by polling the input FD using the device
  325.    specific readchar() function.  Note: reschedule() is called after
  326.    every read.  This is because there is no guarentee that the lower
  327.    level fd_event() poll_event() code (which also calls reschedule())
  328.    will be called.  */

  329. int
  330. generic_readchar (struct serial *scb, int timeout,
  331.                   int (do_readchar) (struct serial *scb, int timeout))
  332. {
  333.   int ch;
  334.   if (scb->bufcnt > 0)
  335.     {
  336.       ch = *scb->bufp;
  337.       scb->bufcnt--;
  338.       scb->bufp++;
  339.     }
  340.   else if (scb->bufcnt < 0)
  341.     {
  342.       /* Some errors/eof are are sticky.  */
  343.       ch = scb->bufcnt;
  344.     }
  345.   else
  346.     {
  347.       ch = do_readchar (scb, timeout);
  348.       if (ch < 0)
  349.         {
  350.           switch ((enum serial_rc) ch)
  351.             {
  352.             case SERIAL_EOF:
  353.             case SERIAL_ERROR:
  354.               /* Make the error/eof stick.  */
  355.               scb->bufcnt = ch;
  356.               break;
  357.             case SERIAL_TIMEOUT:
  358.               scb->bufcnt = 0;
  359.               break;
  360.             }
  361.         }
  362.     }

  363.   /* Read any error output we might have.  */
  364.   ser_base_read_error_fd (scb, 1);

  365.   reschedule (scb);
  366.   return ch;
  367. }

  368. int
  369. ser_base_readchar (struct serial *scb, int timeout)
  370. {
  371.   return generic_readchar (scb, timeout, do_ser_base_readchar);
  372. }

  373. int
  374. ser_base_write (struct serial *scb, const void *buf, size_t count)
  375. {
  376.   const char *str = buf;
  377.   int cc;

  378.   while (count > 0)
  379.     {
  380.       cc = scb->ops->write_prim (scb, str, count);

  381.       if (cc < 0)
  382.         return 1;
  383.       count -= cc;
  384.       str += cc;
  385.     }
  386.   return 0;
  387. }

  388. int
  389. ser_base_flush_output (struct serial *scb)
  390. {
  391.   return 0;
  392. }

  393. int
  394. ser_base_flush_input (struct serial *scb)
  395. {
  396.   if (scb->bufcnt >= 0)
  397.     {
  398.       scb->bufcnt = 0;
  399.       scb->bufp = scb->buf;
  400.       return 0;
  401.     }
  402.   else
  403.     return SERIAL_ERROR;
  404. }

  405. int
  406. ser_base_send_break (struct serial *scb)
  407. {
  408.   return 0;
  409. }

  410. int
  411. ser_base_drain_output (struct serial *scb)
  412. {
  413.   return 0;
  414. }

  415. void
  416. ser_base_raw (struct serial *scb)
  417. {
  418.   return;                        /* Always in raw mode.  */
  419. }

  420. serial_ttystate
  421. ser_base_get_tty_state (struct serial *scb)
  422. {
  423.   /* Allocate a dummy.  */
  424.   return (serial_ttystate) XNEW (int);
  425. }

  426. serial_ttystate
  427. ser_base_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
  428. {
  429.   /* Allocate another dummy.  */
  430.   return (serial_ttystate) XNEW (int);
  431. }

  432. int
  433. ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
  434. {
  435.   return 0;
  436. }

  437. int
  438. ser_base_noflush_set_tty_state (struct serial *scb,
  439.                                 serial_ttystate new_ttystate,
  440.                                 serial_ttystate old_ttystate)
  441. {
  442.   return 0;
  443. }

  444. void
  445. ser_base_print_tty_state (struct serial *scb,
  446.                           serial_ttystate ttystate,
  447.                           struct ui_file *stream)
  448. {
  449.   /* Nothing to print.  */
  450.   return;
  451. }

  452. int
  453. ser_base_setbaudrate (struct serial *scb, int rate)
  454. {
  455.   return 0;                        /* Never fails!  */
  456. }

  457. int
  458. ser_base_setstopbits (struct serial *scb, int num)
  459. {
  460.   return 0;                        /* Never fails!  */
  461. }

  462. /* Put the SERIAL device into/out-of ASYNC mode.  */

  463. void
  464. ser_base_async (struct serial *scb,
  465.                 int async_p)
  466. {
  467.   if (async_p)
  468.     {
  469.       /* Force a re-schedule.  */
  470.       scb->async_state = NOTHING_SCHEDULED;
  471.       if (serial_debug_p (scb))
  472.         fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
  473.                             scb->fd);
  474.       reschedule (scb);
  475.     }
  476.   else
  477.     {
  478.       if (serial_debug_p (scb))
  479.         fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
  480.                             scb->fd);
  481.       /* De-schedule whatever tasks are currently scheduled.  */
  482.       switch (scb->async_state)
  483.         {
  484.         case FD_SCHEDULED:
  485.           delete_file_handler (scb->fd);
  486.           break;
  487.         case NOTHING_SCHEDULED:
  488.           break;
  489.         default: /* TIMER SCHEDULED */
  490.           delete_timer (scb->async_state);
  491.           break;
  492.         }
  493.     }
  494. }