GDB (API)
/home/stan/gdb/src/gdb/ser-base.c
Go to the documentation of this file.
00001 /* Generic serial interface functions.
00002 
00003    Copyright (C) 1992-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "serial.h"
00022 #include "ser-base.h"
00023 #include "event-loop.h"
00024 
00025 #include "gdb_select.h"
00026 #include "gdb_string.h"
00027 #include "gdb_assert.h"
00028 #include <sys/time.h>
00029 #ifdef USE_WIN32API
00030 #include <winsock2.h>
00031 #endif
00032 
00033 
00034 static timer_handler_func push_event;
00035 static handler_func fd_event;
00036 
00037 /* Event handling for ASYNC serial code.
00038 
00039    At any time the SERIAL device either: has an empty FIFO and is
00040    waiting on a FD event; or has a non-empty FIFO/error condition and
00041    is constantly scheduling timer events.
00042 
00043    ASYNC only stops pestering its client when it is de-async'ed or it
00044    is told to go away.  */
00045 
00046 /* Value of scb->async_state: */
00047 enum {
00048   /* >= 0 (TIMER_SCHEDULED) */
00049   /* The ID of the currently scheduled timer event.  This state is
00050      rarely encountered.  Timer events are one-off so as soon as the
00051      event is delivered the state is shanged to NOTHING_SCHEDULED.  */
00052   FD_SCHEDULED = -1,
00053   /* The fd_event() handler is scheduled.  It is called when ever the
00054      file descriptor becomes ready.  */
00055   NOTHING_SCHEDULED = -2
00056   /* Either no task is scheduled (just going into ASYNC mode) or a
00057      timer event has just gone off and the current state has been
00058      forced into nothing scheduled.  */
00059 };
00060 
00061 /* Identify and schedule the next ASYNC task based on scb->async_state
00062    and scb->buf* (the input FIFO).  A state machine is used to avoid
00063    the need to make redundant calls into the event-loop - the next
00064    scheduled task is only changed when needed.  */
00065 
00066 static void
00067 reschedule (struct serial *scb)
00068 {
00069   if (serial_is_async_p (scb))
00070     {
00071       int next_state;
00072 
00073       switch (scb->async_state)
00074         {
00075         case FD_SCHEDULED:
00076           if (scb->bufcnt == 0)
00077             next_state = FD_SCHEDULED;
00078           else
00079             {
00080               delete_file_handler (scb->fd);
00081               next_state = create_timer (0, push_event, scb);
00082             }
00083           break;
00084         case NOTHING_SCHEDULED:
00085           if (scb->bufcnt == 0)
00086             {
00087               add_file_handler (scb->fd, fd_event, scb);
00088               next_state = FD_SCHEDULED;
00089             }
00090           else
00091             {
00092               next_state = create_timer (0, push_event, scb);
00093             }
00094           break;
00095         default: /* TIMER SCHEDULED */
00096           if (scb->bufcnt == 0)
00097             {
00098               delete_timer (scb->async_state);
00099               add_file_handler (scb->fd, fd_event, scb);
00100               next_state = FD_SCHEDULED;
00101             }
00102           else
00103             next_state = scb->async_state;
00104           break;
00105         }
00106       if (serial_debug_p (scb))
00107         {
00108           switch (next_state)
00109             {
00110             case FD_SCHEDULED:
00111               if (scb->async_state != FD_SCHEDULED)
00112                 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
00113                                     scb->fd);
00114               break;
00115             default: /* TIMER SCHEDULED */
00116               if (scb->async_state == FD_SCHEDULED)
00117                 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
00118                                     scb->fd);
00119               break;
00120             }
00121         }
00122       scb->async_state = next_state;
00123     }
00124 }
00125 
00126 /* Run the SCB's async handle, and reschedule, if the handler doesn't
00127    close SCB.  */
00128 
00129 static void
00130 run_async_handler_and_reschedule (struct serial *scb)
00131 {
00132   int is_open;
00133 
00134   /* Take a reference, so a serial_close call within the handler
00135      doesn't make SCB a dangling pointer.  */
00136   serial_ref (scb);
00137 
00138   /* Run the handler.  */
00139   scb->async_handler (scb, scb->async_context);
00140 
00141   is_open = serial_is_open (scb);
00142   serial_unref (scb);
00143 
00144   /* Get ready for more, if not already closed.  */
00145   if (is_open)
00146     reschedule (scb);
00147 }
00148 
00149 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
00150    is no pending error).  As soon as data arrives, it is read into the
00151    input FIFO and the client notified.  The client should then drain
00152    the FIFO using readchar().  If the FIFO isn't immediatly emptied,
00153    push_event() is used to nag the client until it is.  */
00154 
00155 static void
00156 fd_event (int error, void *context)
00157 {
00158   struct serial *scb = context;
00159   if (error != 0)
00160     {
00161       scb->bufcnt = SERIAL_ERROR;
00162     }
00163   else if (scb->bufcnt == 0)
00164     {
00165       /* Prime the input FIFO.  The readchar() function is used to
00166          pull characters out of the buffer.  See also
00167          generic_readchar().  */
00168       int nr;
00169       nr = scb->ops->read_prim (scb, BUFSIZ);
00170       if (nr == 0)
00171         {
00172           scb->bufcnt = SERIAL_EOF;
00173         }
00174       else if (nr > 0)
00175         {
00176           scb->bufcnt = nr;
00177           scb->bufp = scb->buf;
00178         }
00179       else
00180         {
00181           scb->bufcnt = SERIAL_ERROR;
00182         }
00183     }
00184   run_async_handler_and_reschedule (scb);
00185 }
00186 
00187 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
00188    error).  Nag the client until all the data has been read.  In the
00189    case of errors, the client will need to close or de-async the
00190    device before naging stops.  */
00191 
00192 static void
00193 push_event (void *context)
00194 {
00195   struct serial *scb = context;
00196 
00197   scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
00198   run_async_handler_and_reschedule (scb);
00199 }
00200 
00201 /* Wait for input on scb, with timeout seconds.  Returns 0 on success,
00202    otherwise SERIAL_TIMEOUT or SERIAL_ERROR.  */
00203 
00204 static int
00205 ser_base_wait_for (struct serial *scb, int timeout)
00206 {
00207   while (1)
00208     {
00209       int numfds;
00210       struct timeval tv;
00211       fd_set readfds, exceptfds;
00212 
00213       /* NOTE: Some OS's can scramble the READFDS when the select()
00214          call fails (ex the kernel with Red Hat 5.2).  Initialize all
00215          arguments before each call.  */
00216 
00217       tv.tv_sec = timeout;
00218       tv.tv_usec = 0;
00219 
00220       FD_ZERO (&readfds);
00221       FD_ZERO (&exceptfds);
00222       FD_SET (scb->fd, &readfds);
00223       FD_SET (scb->fd, &exceptfds);
00224 
00225       if (timeout >= 0)
00226         numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
00227       else
00228         numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
00229 
00230       if (numfds <= 0)
00231         {
00232           if (numfds == 0)
00233             return SERIAL_TIMEOUT;
00234           else if (errno == EINTR)
00235             continue;
00236           else
00237             return SERIAL_ERROR;        /* Got an error from select or
00238                                            poll.  */
00239         }
00240 
00241       return 0;
00242     }
00243 }
00244 
00245 /* Read any error output we might have.  */
00246 
00247 static void
00248 ser_base_read_error_fd (struct serial *scb, int close_fd)
00249 {
00250   if (scb->error_fd != -1)
00251     {
00252       ssize_t s;
00253       char buf[GDB_MI_MSG_WIDTH + 1];
00254 
00255       for (;;)
00256         {
00257           char *current;
00258           char *newline;
00259           int to_read = GDB_MI_MSG_WIDTH;
00260           int num_bytes = -1;
00261 
00262           if (scb->ops->avail)
00263             num_bytes = (scb->ops->avail)(scb, scb->error_fd);
00264 
00265           if (num_bytes != -1)
00266             to_read = (num_bytes < to_read) ? num_bytes : to_read;
00267 
00268           if (to_read == 0)
00269             break;
00270 
00271           s = read (scb->error_fd, &buf, to_read);
00272           if ((s == -1) || (s == 0 && !close_fd))
00273             break;
00274 
00275           if (s == 0 && close_fd)
00276             {
00277               /* End of file.  */
00278               close (scb->error_fd);
00279               scb->error_fd = -1;
00280               break;
00281             }
00282 
00283           /* In theory, embedded newlines are not a problem.
00284              But for MI, we want each output line to have just
00285              one newline for legibility.  So output things
00286              in newline chunks.  */
00287           gdb_assert (s > 0 && s <= GDB_MI_MSG_WIDTH);
00288           buf[s] = '\0';
00289           current = buf;
00290           while ((newline = strstr (current, "\n")) != NULL)
00291             {
00292               *newline = '\0';
00293               fputs_unfiltered (current, gdb_stderr);
00294               fputs_unfiltered ("\n", gdb_stderr);
00295               current = newline + 1;
00296             }
00297 
00298           fputs_unfiltered (current, gdb_stderr);
00299        }
00300     }
00301 }
00302 
00303 /* Read a character with user-specified timeout.  TIMEOUT is number of seconds
00304    to wait, or -1 to wait forever.  Use timeout of 0 to effect a poll.  Returns
00305    char if successful.  Returns -2 if timeout expired, EOF if line dropped
00306    dead, or -3 for any other error (see errno in that case).  */
00307 
00308 static int
00309 do_ser_base_readchar (struct serial *scb, int timeout)
00310 {
00311   int status;
00312   int delta;
00313 
00314   /* We have to be able to keep the GUI alive here, so we break the
00315      original timeout into steps of 1 second, running the "keep the
00316      GUI alive" hook each time through the loop.
00317 
00318      Also, timeout = 0 means to poll, so we just set the delta to 0,
00319      so we will only go through the loop once.  */
00320 
00321   delta = (timeout == 0 ? 0 : 1);
00322   while (1)
00323     {
00324       /* N.B. The UI may destroy our world (for instance by calling
00325          remote_stop,) in which case we want to get out of here as
00326          quickly as possible.  It is not safe to touch scb, since
00327          someone else might have freed it.  The
00328          deprecated_ui_loop_hook signals that we should exit by
00329          returning 1.  */
00330 
00331       if (deprecated_ui_loop_hook)
00332         {
00333           if (deprecated_ui_loop_hook (0))
00334             return SERIAL_TIMEOUT;
00335         }
00336 
00337       status = ser_base_wait_for (scb, delta);
00338       if (timeout > 0)
00339         timeout -= delta;
00340 
00341       /* If we got a character or an error back from wait_for, then we can 
00342          break from the loop before the timeout is completed.  */
00343       if (status != SERIAL_TIMEOUT)
00344         break;
00345 
00346       /* If we have exhausted the original timeout, then generate
00347          a SERIAL_TIMEOUT, and pass it out of the loop.  */
00348       else if (timeout == 0)
00349         {
00350           status = SERIAL_TIMEOUT;
00351           break;
00352         }
00353 
00354       /* We also need to check and consume the stderr because it could
00355          come before the stdout for some stubs.  If we just sit and wait
00356          for stdout, we would hit a deadlock for that case.  */
00357       ser_base_read_error_fd (scb, 0);
00358     }
00359 
00360   if (status < 0)
00361     return status;
00362 
00363   status = scb->ops->read_prim (scb, BUFSIZ);
00364 
00365   if (status <= 0)
00366     {
00367       if (status == 0)
00368         return SERIAL_EOF;
00369       else
00370         /* Got an error from read.  */
00371         return SERIAL_ERROR;    
00372     }
00373 
00374   scb->bufcnt = status;
00375   scb->bufcnt--;
00376   scb->bufp = scb->buf;
00377   return *scb->bufp++;
00378 }
00379 
00380 /* Perform operations common to both old and new readchar.  */
00381 
00382 /* Return the next character from the input FIFO.  If the FIFO is
00383    empty, call the SERIAL specific routine to try and read in more
00384    characters.
00385 
00386    Initially data from the input FIFO is returned (fd_event()
00387    pre-reads the input into that FIFO.  Once that has been emptied,
00388    further data is obtained by polling the input FD using the device
00389    specific readchar() function.  Note: reschedule() is called after
00390    every read.  This is because there is no guarentee that the lower
00391    level fd_event() poll_event() code (which also calls reschedule())
00392    will be called.  */
00393 
00394 int
00395 generic_readchar (struct serial *scb, int timeout,
00396                   int (do_readchar) (struct serial *scb, int timeout))
00397 {
00398   int ch;
00399   if (scb->bufcnt > 0)
00400     {
00401       ch = *scb->bufp;
00402       scb->bufcnt--;
00403       scb->bufp++;
00404     }
00405   else if (scb->bufcnt < 0)
00406     {
00407       /* Some errors/eof are are sticky.  */
00408       ch = scb->bufcnt;
00409     }
00410   else
00411     {
00412       ch = do_readchar (scb, timeout);
00413       if (ch < 0)
00414         {
00415           switch ((enum serial_rc) ch)
00416             {
00417             case SERIAL_EOF:
00418             case SERIAL_ERROR:
00419               /* Make the error/eof stick.  */
00420               scb->bufcnt = ch;
00421               break;
00422             case SERIAL_TIMEOUT:
00423               scb->bufcnt = 0;
00424               break;
00425             }
00426         }
00427     }
00428 
00429   /* Read any error output we might have.  */
00430   ser_base_read_error_fd (scb, 1);
00431 
00432   reschedule (scb);
00433   return ch;
00434 }
00435 
00436 int
00437 ser_base_readchar (struct serial *scb, int timeout)
00438 {
00439   return generic_readchar (scb, timeout, do_ser_base_readchar);
00440 }
00441 
00442 int
00443 ser_base_write (struct serial *scb, const void *buf, size_t count)
00444 {
00445   const char *str = buf;
00446   int cc;
00447 
00448   while (count > 0)
00449     {
00450       cc = scb->ops->write_prim (scb, str, count);
00451 
00452       if (cc < 0)
00453         return 1;
00454       count -= cc;
00455       str += cc;
00456     }
00457   return 0;
00458 }
00459 
00460 int
00461 ser_base_flush_output (struct serial *scb)
00462 {
00463   return 0;
00464 }
00465 
00466 int
00467 ser_base_flush_input (struct serial *scb)
00468 {
00469   if (scb->bufcnt >= 0)
00470     {
00471       scb->bufcnt = 0;
00472       scb->bufp = scb->buf;
00473       return 0;
00474     }
00475   else
00476     return SERIAL_ERROR;
00477 }
00478 
00479 int
00480 ser_base_send_break (struct serial *scb)
00481 {
00482   return 0;
00483 }
00484 
00485 int
00486 ser_base_drain_output (struct serial *scb)
00487 {
00488   return 0;
00489 }
00490 
00491 void
00492 ser_base_raw (struct serial *scb)
00493 {
00494   return;                       /* Always in raw mode.  */
00495 }
00496 
00497 serial_ttystate
00498 ser_base_get_tty_state (struct serial *scb)
00499 {
00500   /* Allocate a dummy.  */
00501   return (serial_ttystate) XMALLOC (int);
00502 }
00503 
00504 serial_ttystate
00505 ser_base_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
00506 {
00507   /* Allocate another dummy.  */
00508   return (serial_ttystate) XMALLOC (int);
00509 }
00510 
00511 int
00512 ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
00513 {
00514   return 0;
00515 }
00516 
00517 int
00518 ser_base_noflush_set_tty_state (struct serial *scb,
00519                                 serial_ttystate new_ttystate,
00520                                 serial_ttystate old_ttystate)
00521 {
00522   return 0;
00523 }
00524 
00525 void
00526 ser_base_print_tty_state (struct serial *scb, 
00527                           serial_ttystate ttystate,
00528                           struct ui_file *stream)
00529 {
00530   /* Nothing to print.  */
00531   return;
00532 }
00533 
00534 int
00535 ser_base_setbaudrate (struct serial *scb, int rate)
00536 {
00537   return 0;                     /* Never fails!  */
00538 }
00539 
00540 int
00541 ser_base_setstopbits (struct serial *scb, int num)
00542 {
00543   return 0;                     /* Never fails!  */
00544 }
00545 
00546 /* Put the SERIAL device into/out-of ASYNC mode.  */
00547 
00548 void
00549 ser_base_async (struct serial *scb,
00550                 int async_p)
00551 {
00552   if (async_p)
00553     {
00554       /* Force a re-schedule.  */
00555       scb->async_state = NOTHING_SCHEDULED;
00556       if (serial_debug_p (scb))
00557         fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
00558                             scb->fd);
00559       reschedule (scb);
00560     }
00561   else
00562     {
00563       if (serial_debug_p (scb))
00564         fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
00565                             scb->fd);
00566       /* De-schedule whatever tasks are currently scheduled.  */
00567       switch (scb->async_state)
00568         {
00569         case FD_SCHEDULED:
00570           delete_file_handler (scb->fd);
00571           break;
00572         case NOTHING_SCHEDULED:
00573           break;
00574         default: /* TIMER SCHEDULED */
00575           delete_timer (scb->async_state);
00576           break;
00577         }
00578     }
00579 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines