1 /* Generic serial interface functions.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "event-loop.h"
27 #include "gdb_select.h"
28 #include "gdb_string.h"
35 static timer_handler_func push_event;
36 static handler_func fd_event;
38 /* Event handling for ASYNC serial code.
40 At any time the SERIAL device either: has an empty FIFO and is
41 waiting on a FD event; or has a non-empty FIFO/error condition and
42 is constantly scheduling timer events.
44 ASYNC only stops pestering its client when it is de-async'ed or it
45 is told to go away. */
47 /* Value of scb->async_state: */
49 /* >= 0 (TIMER_SCHEDULED) */
50 /* The ID of the currently scheduled timer event. This state is
51 rarely encountered. Timer events are one-off so as soon as the
52 event is delivered the state is shanged to NOTHING_SCHEDULED. */
54 /* The fd_event() handler is scheduled. It is called when ever the
55 file descriptor becomes ready. */
56 NOTHING_SCHEDULED = -2
57 /* Either no task is scheduled (just going into ASYNC mode) or a
58 timer event has just gone off and the current state has been
59 forced into nothing scheduled. */
62 /* Identify and schedule the next ASYNC task based on scb->async_state
63 and scb->buf* (the input FIFO). A state machine is used to avoid
64 the need to make redundant calls into the event-loop - the next
65 scheduled task is only changed when needed. */
68 reschedule (struct serial *scb)
70 if (serial_is_async_p (scb))
74 switch (scb->async_state)
78 next_state = FD_SCHEDULED;
81 delete_file_handler (scb->fd);
82 next_state = create_timer (0, push_event, scb);
85 case NOTHING_SCHEDULED:
88 add_file_handler (scb->fd, fd_event, scb);
89 next_state = FD_SCHEDULED;
93 next_state = create_timer (0, push_event, scb);
96 default: /* TIMER SCHEDULED */
99 delete_timer (scb->async_state);
100 add_file_handler (scb->fd, fd_event, scb);
101 next_state = FD_SCHEDULED;
104 next_state = scb->async_state;
107 if (serial_debug_p (scb))
112 if (scb->async_state != FD_SCHEDULED)
113 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
116 default: /* TIMER SCHEDULED */
117 if (scb->async_state == FD_SCHEDULED)
118 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
123 scb->async_state = next_state;
127 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
128 is no pending error). As soon as data arrives, it is read into the
129 input FIFO and the client notified. The client should then drain
130 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
131 push_event() is used to nag the client until it is. */
134 fd_event (int error, void *context)
136 struct serial *scb = context;
139 scb->bufcnt = SERIAL_ERROR;
141 else if (scb->bufcnt == 0)
143 /* Prime the input FIFO. The readchar() function is used to
144 pull characters out of the buffer. See also
145 generic_readchar(). */
147 nr = scb->ops->read_prim (scb, BUFSIZ);
150 scb->bufcnt = SERIAL_EOF;
155 scb->bufp = scb->buf;
159 scb->bufcnt = SERIAL_ERROR;
162 scb->async_handler (scb, scb->async_context);
166 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
167 error). Nag the client until all the data has been read. In the
168 case of errors, the client will need to close or de-async the
169 device before naging stops. */
172 push_event (void *context)
174 struct serial *scb = context;
176 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
177 scb->async_handler (scb, scb->async_context);
182 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
183 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
186 ser_base_wait_for (struct serial *scb, int timeout)
192 fd_set readfds, exceptfds;
194 /* NOTE: Some OS's can scramble the READFDS when the select()
195 call fails (ex the kernel with Red Hat 5.2). Initialize all
196 arguments before each call. */
202 FD_ZERO (&exceptfds);
203 FD_SET (scb->fd, &readfds);
204 FD_SET (scb->fd, &exceptfds);
207 numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
209 numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
214 return SERIAL_TIMEOUT;
215 else if (errno == EINTR)
218 return SERIAL_ERROR; /* Got an error from select or poll */
225 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
226 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
227 char if successful. Returns -2 if timeout expired, EOF if line dropped
228 dead, or -3 for any other error (see errno in that case). */
231 do_ser_base_readchar (struct serial *scb, int timeout)
236 /* We have to be able to keep the GUI alive here, so we break the
237 original timeout into steps of 1 second, running the "keep the
238 GUI alive" hook each time through the loop.
240 Also, timeout = 0 means to poll, so we just set the delta to 0,
241 so we will only go through the loop once. */
243 delta = (timeout == 0 ? 0 : 1);
246 /* N.B. The UI may destroy our world (for instance by calling
247 remote_stop,) in which case we want to get out of here as
248 quickly as possible. It is not safe to touch scb, since
249 someone else might have freed it. The
250 deprecated_ui_loop_hook signals that we should exit by
253 if (deprecated_ui_loop_hook)
255 if (deprecated_ui_loop_hook (0))
256 return SERIAL_TIMEOUT;
259 status = ser_base_wait_for (scb, delta);
263 /* If we got a character or an error back from wait_for, then we can
264 break from the loop before the timeout is completed. */
265 if (status != SERIAL_TIMEOUT)
268 /* If we have exhausted the original timeout, then generate
269 a SERIAL_TIMEOUT, and pass it out of the loop. */
270 else if (timeout == 0)
272 status = SERIAL_TIMEOUT;
280 status = scb->ops->read_prim (scb, BUFSIZ);
287 /* Got an error from read. */
291 scb->bufcnt = status;
293 scb->bufp = scb->buf;
297 /* Perform operations common to both old and new readchar. */
299 /* Return the next character from the input FIFO. If the FIFO is
300 empty, call the SERIAL specific routine to try and read in more
303 Initially data from the input FIFO is returned (fd_event()
304 pre-reads the input into that FIFO. Once that has been emptied,
305 further data is obtained by polling the input FD using the device
306 specific readchar() function. Note: reschedule() is called after
307 every read. This is because there is no guarentee that the lower
308 level fd_event() poll_event() code (which also calls reschedule())
312 generic_readchar (struct serial *scb, int timeout,
313 int (do_readchar) (struct serial *scb, int timeout))
322 else if (scb->bufcnt < 0)
324 /* Some errors/eof are are sticky. */
329 ch = do_readchar (scb, timeout);
332 switch ((enum serial_rc) ch)
336 /* Make the error/eof stick. */
345 /* Read any error output we might have. */
346 if (scb->error_fd != -1)
359 num_bytes = (scb->ops->avail)(scb, scb->error_fd);
361 to_read = (num_bytes < to_read) ? num_bytes : to_read;
366 s = read (scb->error_fd, &buf, to_read);
372 close (scb->error_fd);
377 /* In theory, embedded newlines are not a problem.
378 But for MI, we want each output line to have just
379 one newline for legibility. So output things
380 in newline chunks. */
383 while ((newline = strstr (current, "\n")) != NULL)
386 fputs_unfiltered (current, gdb_stderr);
387 fputs_unfiltered ("\n", gdb_stderr);
388 current = newline + 1;
390 fputs_unfiltered (current, gdb_stderr);
399 ser_base_readchar (struct serial *scb, int timeout)
401 return generic_readchar (scb, timeout, do_ser_base_readchar);
405 ser_base_write (struct serial *scb, const char *str, int len)
411 cc = scb->ops->write_prim (scb, str, len);
422 ser_base_flush_output (struct serial *scb)
428 ser_base_flush_input (struct serial *scb)
430 if (scb->bufcnt >= 0)
433 scb->bufp = scb->buf;
441 ser_base_send_break (struct serial *scb)
447 ser_base_drain_output (struct serial *scb)
453 ser_base_raw (struct serial *scb)
455 return; /* Always in raw mode */
459 ser_base_get_tty_state (struct serial *scb)
461 /* allocate a dummy */
462 return (serial_ttystate) XMALLOC (int);
466 ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
472 ser_base_noflush_set_tty_state (struct serial *scb,
473 serial_ttystate new_ttystate,
474 serial_ttystate old_ttystate)
480 ser_base_print_tty_state (struct serial *scb,
481 serial_ttystate ttystate,
482 struct ui_file *stream)
484 /* Nothing to print. */
489 ser_base_setbaudrate (struct serial *scb, int rate)
491 return 0; /* Never fails! */
495 ser_base_setstopbits (struct serial *scb, int num)
497 return 0; /* Never fails! */
500 /* Put the SERIAL device into/out-of ASYNC mode. */
503 ser_base_async (struct serial *scb,
508 /* Force a re-schedule. */
509 scb->async_state = NOTHING_SCHEDULED;
510 if (serial_debug_p (scb))
511 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
517 if (serial_debug_p (scb))
518 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
520 /* De-schedule whatever tasks are currently scheduled. */
521 switch (scb->async_state)
524 delete_file_handler (scb->fd);
526 case NOTHING_SCHEDULED:
528 default: /* TIMER SCHEDULED */
529 delete_timer (scb->async_state);