1 /* Generic serial interface functions.
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2003,
4 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
26 #include "event-loop.h"
28 #include "gdb_select.h"
29 #include "gdb_string.h"
36 static timer_handler_func push_event
;
37 static handler_func fd_event
;
39 /* Event handling for ASYNC serial code.
41 At any time the SERIAL device either: has an empty FIFO and is
42 waiting on a FD event; or has a non-empty FIFO/error condition and
43 is constantly scheduling timer events.
45 ASYNC only stops pestering its client when it is de-async'ed or it
46 is told to go away. */
48 /* Value of scb->async_state: */
50 /* >= 0 (TIMER_SCHEDULED) */
51 /* The ID of the currently scheduled timer event. This state is
52 rarely encountered. Timer events are one-off so as soon as the
53 event is delivered the state is shanged to NOTHING_SCHEDULED. */
55 /* The fd_event() handler is scheduled. It is called when ever the
56 file descriptor becomes ready. */
57 NOTHING_SCHEDULED
= -2
58 /* Either no task is scheduled (just going into ASYNC mode) or a
59 timer event has just gone off and the current state has been
60 forced into nothing scheduled. */
63 /* Identify and schedule the next ASYNC task based on scb->async_state
64 and scb->buf* (the input FIFO). A state machine is used to avoid
65 the need to make redundant calls into the event-loop - the next
66 scheduled task is only changed when needed. */
69 reschedule (struct serial
*scb
)
71 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
;
175 scb
->async_state
= NOTHING_SCHEDULED
; /* Timers are one-off */
176 scb
->async_handler (scb
, scb
->async_context
);
181 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
182 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
185 ser_base_wait_for (struct serial
*scb
, int timeout
)
191 fd_set readfds
, exceptfds
;
193 /* NOTE: Some OS's can scramble the READFDS when the select()
194 call fails (ex the kernel with Red Hat 5.2). Initialize all
195 arguments before each call. */
201 FD_ZERO (&exceptfds
);
202 FD_SET (scb
->fd
, &readfds
);
203 FD_SET (scb
->fd
, &exceptfds
);
206 numfds
= gdb_select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, &tv
);
208 numfds
= gdb_select (scb
->fd
+ 1, &readfds
, 0, &exceptfds
, 0);
213 return SERIAL_TIMEOUT
;
214 else if (errno
== EINTR
)
217 return SERIAL_ERROR
; /* Got an error from select or poll */
224 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
225 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
226 char if successful. Returns -2 if timeout expired, EOF if line dropped
227 dead, or -3 for any other error (see errno in that case). */
230 do_ser_base_readchar (struct serial
*scb
, int timeout
)
235 /* We have to be able to keep the GUI alive here, so we break the
236 original timeout into steps of 1 second, running the "keep the
237 GUI alive" hook each time through the loop.
239 Also, timeout = 0 means to poll, so we just set the delta to 0,
240 so we will only go through the loop once. */
242 delta
= (timeout
== 0 ? 0 : 1);
245 /* N.B. The UI may destroy our world (for instance by calling
246 remote_stop,) in which case we want to get out of here as
247 quickly as possible. It is not safe to touch scb, since
248 someone else might have freed it. The
249 deprecated_ui_loop_hook signals that we should exit by
252 if (deprecated_ui_loop_hook
)
254 if (deprecated_ui_loop_hook (0))
255 return SERIAL_TIMEOUT
;
258 status
= ser_base_wait_for (scb
, delta
);
262 /* If we got a character or an error back from wait_for, then we can
263 break from the loop before the timeout is completed. */
264 if (status
!= SERIAL_TIMEOUT
)
267 /* If we have exhausted the original timeout, then generate
268 a SERIAL_TIMEOUT, and pass it out of the loop. */
269 else if (timeout
== 0)
271 status
= SERIAL_TIMEOUT
;
279 status
= scb
->ops
->read_prim (scb
, BUFSIZ
);
284 /* 0 chars means timeout. (We may need to distinguish between EOF
285 & timeouts someday.) */
286 return SERIAL_TIMEOUT
;
288 /* Got an error from read. */
292 scb
->bufcnt
= status
;
294 scb
->bufp
= scb
->buf
;
298 /* Perform operations common to both old and new readchar. */
300 /* Return the next character from the input FIFO. If the FIFO is
301 empty, call the SERIAL specific routine to try and read in more
304 Initially data from the input FIFO is returned (fd_event()
305 pre-reads the input into that FIFO. Once that has been emptied,
306 further data is obtained by polling the input FD using the device
307 specific readchar() function. Note: reschedule() is called after
308 every read. This is because there is no guarentee that the lower
309 level fd_event() poll_event() code (which also calls reschedule())
313 generic_readchar (struct serial
*scb
, int timeout
,
314 int (do_readchar
) (struct serial
*scb
, int timeout
))
323 else if (scb
->bufcnt
< 0)
325 /* Some errors/eof are are sticky. */
330 ch
= do_readchar (scb
, timeout
);
333 switch ((enum serial_rc
) ch
)
337 /* Make the error/eof stick. */
346 /* Read any error output we might have. */
347 if (scb
->error_fd
!= -1)
360 num_bytes
= (scb
->ops
->avail
)(scb
, scb
->error_fd
);
362 to_read
= (num_bytes
< to_read
) ? num_bytes
: to_read
;
367 s
= read (scb
->error_fd
, &buf
, to_read
);
371 /* In theory, embedded newlines are not a problem.
372 But for MI, we want each output line to have just
373 one newline for legibility. So output things
374 in newline chunks. */
377 while ((newline
= strstr (current
, "\n")) != NULL
)
380 fputs_unfiltered (current
, gdb_stderr
);
381 fputs_unfiltered ("\n", gdb_stderr
);
382 current
= newline
+ 1;
384 fputs_unfiltered (current
, gdb_stderr
);
393 ser_base_readchar (struct serial
*scb
, int timeout
)
395 return generic_readchar (scb
, timeout
, do_ser_base_readchar
);
399 ser_base_write (struct serial
*scb
, const char *str
, int len
)
405 cc
= scb
->ops
->write_prim (scb
, str
, len
);
416 ser_base_flush_output (struct serial
*scb
)
422 ser_base_flush_input (struct serial
*scb
)
424 if (scb
->bufcnt
>= 0)
427 scb
->bufp
= scb
->buf
;
435 ser_base_send_break (struct serial
*scb
)
441 ser_base_drain_output (struct serial
*scb
)
447 ser_base_raw (struct serial
*scb
)
449 return; /* Always in raw mode */
453 ser_base_get_tty_state (struct serial
*scb
)
455 /* allocate a dummy */
456 return (serial_ttystate
) XMALLOC (int);
460 ser_base_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
466 ser_base_noflush_set_tty_state (struct serial
*scb
,
467 serial_ttystate new_ttystate
,
468 serial_ttystate old_ttystate
)
474 ser_base_print_tty_state (struct serial
*scb
,
475 serial_ttystate ttystate
,
476 struct ui_file
*stream
)
478 /* Nothing to print. */
483 ser_base_setbaudrate (struct serial
*scb
, int rate
)
485 return 0; /* Never fails! */
489 ser_base_setstopbits (struct serial
*scb
, int num
)
491 return 0; /* Never fails! */
494 /* Put the SERIAL device into/out-of ASYNC mode. */
497 ser_base_async (struct serial
*scb
,
502 /* Force a re-schedule. */
503 scb
->async_state
= NOTHING_SCHEDULED
;
504 if (serial_debug_p (scb
))
505 fprintf_unfiltered (gdb_stdlog
, "[fd%d->asynchronous]\n",
511 if (serial_debug_p (scb
))
512 fprintf_unfiltered (gdb_stdlog
, "[fd%d->synchronous]\n",
514 /* De-schedule whatever tasks are currently scheduled. */
515 switch (scb
->async_state
)
518 delete_file_handler (scb
->fd
);
520 case NOTHING_SCHEDULED
:
522 default: /* TIMER SCHEDULED */
523 delete_timer (scb
->async_state
);