1 /* Generic serial interface functions.
3 Copyright (C) 1992-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "gdbsupport/event-loop.h"
25 #include "gdbsupport/gdb_select.h"
26 #include "gdbsupport/gdb_sys_time.h"
32 static timer_handler_func push_event
;
33 static handler_func fd_event
;
35 /* Event handling for ASYNC serial code.
37 At any time the SERIAL device either: has an empty FIFO and is
38 waiting on a FD event; or has a non-empty FIFO/error condition and
39 is constantly scheduling timer events.
41 ASYNC only stops pestering its client when it is de-async'ed or it
42 is told to go away. */
44 /* Value of scb->async_state: */
46 /* When >= 0, this contains the ID of the currently scheduled timer event.
47 This state is rarely encountered. Timer events are one-off so as soon as
48 the event is delivered the state is changed to NOTHING_SCHEDULED. */
50 /* The fd_event() handler is scheduled. It is called when ever the
51 file descriptor becomes ready. */
54 /* Either no task is scheduled (just going into ASYNC mode) or a
55 timer event has just gone off and the current state has been
56 forced into nothing scheduled. */
57 NOTHING_SCHEDULED
= -2
60 /* Identify and schedule the next ASYNC task based on scb->async_state
61 and scb->buf* (the input FIFO). A state machine is used to avoid
62 the need to make redundant calls into the event-loop - the next
63 scheduled task is only changed when needed. */
66 reschedule (struct serial
*scb
)
68 if (serial_is_async_p (scb
))
72 switch (scb
->async_state
)
76 next_state
= FD_SCHEDULED
;
79 delete_file_handler (scb
->fd
);
80 next_state
= create_timer (0, push_event
, scb
);
83 case NOTHING_SCHEDULED
:
86 add_file_handler (scb
->fd
, fd_event
, scb
, "serial");
87 next_state
= FD_SCHEDULED
;
91 next_state
= create_timer (0, push_event
, scb
);
94 default: /* TIMER SCHEDULED */
97 delete_timer (scb
->async_state
);
98 add_file_handler (scb
->fd
, fd_event
, scb
, "serial");
99 next_state
= FD_SCHEDULED
;
102 next_state
= scb
->async_state
;
105 if (serial_debug_p (scb
))
110 if (scb
->async_state
!= FD_SCHEDULED
)
111 gdb_printf (gdb_stdlog
, "[fd%d->fd-scheduled]\n",
114 default: /* TIMER SCHEDULED */
115 if (scb
->async_state
== FD_SCHEDULED
)
116 gdb_printf (gdb_stdlog
, "[fd%d->timer-scheduled]\n",
121 scb
->async_state
= next_state
;
125 /* Run the SCB's async handle, and reschedule, if the handler doesn't
129 run_async_handler_and_reschedule (struct serial
*scb
)
133 /* Take a reference, so a serial_close call within the handler
134 doesn't make SCB a dangling pointer. */
137 /* Run the handler. */
138 scb
->async_handler (scb
, scb
->async_context
);
140 is_open
= serial_is_open (scb
);
143 /* Get ready for more, if not already closed. */
148 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
149 is no pending error). As soon as data arrives, it is read into the
150 input FIFO and the client notified. The client should then drain
151 the FIFO using readchar(). If the FIFO isn't immediatly emptied,
152 push_event() is used to nag the client until it is. */
155 fd_event (int error
, void *context
)
157 struct serial
*scb
= (struct serial
*) context
;
160 scb
->bufcnt
= SERIAL_ERROR
;
162 else if (scb
->bufcnt
== 0)
164 /* Prime the input FIFO. The readchar() function is used to
165 pull characters out of the buffer. See also
166 generic_readchar(). */
171 nr
= scb
->ops
->read_prim (scb
, BUFSIZ
);
173 while (nr
< 0 && errno
== EINTR
);
177 scb
->bufcnt
= SERIAL_EOF
;
182 scb
->bufp
= scb
->buf
;
186 scb
->bufcnt
= SERIAL_ERROR
;
189 run_async_handler_and_reschedule (scb
);
192 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
193 error). Nag the client until all the data has been read. In the
194 case of errors, the client will need to close or de-async the
195 device before nagging stops. */
198 push_event (void *context
)
200 struct serial
*scb
= (struct serial
*) context
;
202 scb
->async_state
= NOTHING_SCHEDULED
; /* Timers are one-off */
203 run_async_handler_and_reschedule (scb
);
206 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
207 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
209 /* NOTE: Some of the code below is dead. The only possible values of
210 the TIMEOUT parameter are ONE and ZERO. OTOH, we should probably
211 get rid of the deprecated_ui_loop_hook call in do_ser_base_readchar
212 instead and support infinite time outs here. */
215 ser_base_wait_for (struct serial
*scb
, int timeout
)
221 fd_set readfds
, exceptfds
;
224 /* NOTE: Some OS's can scramble the READFDS when the select()
225 call fails (ex the kernel with Red Hat 5.2). Initialize all
226 arguments before each call. */
232 FD_ZERO (&exceptfds
);
233 FD_SET (scb
->fd
, &readfds
);
234 FD_SET (scb
->fd
, &exceptfds
);
240 numfds
= interruptible_select (nfds
, &readfds
, 0, &exceptfds
, &tv
);
242 numfds
= interruptible_select (nfds
, &readfds
, 0, &exceptfds
, 0);
247 return SERIAL_TIMEOUT
;
248 else if (errno
== EINTR
)
251 return SERIAL_ERROR
; /* Got an error from select or
259 /* Read any error output we might have. */
262 ser_base_read_error_fd (struct serial
*scb
, int close_fd
)
264 if (scb
->error_fd
!= -1)
267 char buf
[GDB_MI_MSG_WIDTH
+ 1];
273 int to_read
= GDB_MI_MSG_WIDTH
;
277 num_bytes
= (scb
->ops
->avail
)(scb
, scb
->error_fd
);
280 to_read
= (num_bytes
< to_read
) ? num_bytes
: to_read
;
285 s
= read (scb
->error_fd
, &buf
, to_read
);
286 if ((s
== -1) || (s
== 0 && !close_fd
))
289 if (s
== 0 && close_fd
)
292 if (serial_is_async_p (scb
))
293 delete_file_handler (scb
->error_fd
);
294 close (scb
->error_fd
);
299 /* In theory, embedded newlines are not a problem.
300 But for MI, we want each output line to have just
301 one newline for legibility. So output things
302 in newline chunks. */
303 gdb_assert (s
> 0 && s
<= GDB_MI_MSG_WIDTH
);
306 while ((newline
= strstr (current
, "\n")) != NULL
)
309 gdb_puts (current
, gdb_stderr
);
310 gdb_puts ("\n", gdb_stderr
);
311 current
= newline
+ 1;
314 gdb_puts (current
, gdb_stderr
);
319 /* Event-loop callback for a serial's error_fd. Flushes any error
320 output we might have. */
323 handle_error_fd (int error
, gdb_client_data client_data
)
325 serial
*scb
= (serial
*) client_data
;
327 ser_base_read_error_fd (scb
, 0);
330 /* Read a character with user-specified timeout. TIMEOUT is number of
331 seconds to wait, or -1 to wait forever. Use timeout of 0 to effect
332 a poll. Returns char if successful. Returns SERIAL_TIMEOUT if
333 timeout expired, SERIAL_EOF if line dropped dead, or SERIAL_ERROR
334 for any other error (see errno in that case). */
337 do_ser_base_readchar (struct serial
*scb
, int timeout
)
342 /* We have to be able to keep the GUI alive here, so we break the
343 original timeout into steps of 1 second, running the "keep the
344 GUI alive" hook each time through the loop.
346 Also, timeout = 0 means to poll, so we just set the delta to 0,
347 so we will only go through the loop once. */
349 delta
= (timeout
== 0 ? 0 : 1);
352 /* N.B. The UI may destroy our world (for instance by calling
353 remote_stop,) in which case we want to get out of here as
354 quickly as possible. It is not safe to touch scb, since
355 someone else might have freed it. The
356 deprecated_ui_loop_hook signals that we should exit by
359 if (deprecated_ui_loop_hook
)
361 if (deprecated_ui_loop_hook (0))
362 return SERIAL_TIMEOUT
;
365 status
= ser_base_wait_for (scb
, delta
);
369 /* If we got a character or an error back from wait_for, then we can
370 break from the loop before the timeout is completed. */
371 if (status
!= SERIAL_TIMEOUT
)
374 /* If we have exhausted the original timeout, then generate
375 a SERIAL_TIMEOUT, and pass it out of the loop. */
376 else if (timeout
== 0)
378 status
= SERIAL_TIMEOUT
;
382 /* We also need to check and consume the stderr because it could
383 come before the stdout for some stubs. If we just sit and wait
384 for stdout, we would hit a deadlock for that case. */
385 ser_base_read_error_fd (scb
, 0);
393 status
= scb
->ops
->read_prim (scb
, BUFSIZ
);
395 while (status
< 0 && errno
== EINTR
);
402 /* Got an error from read. */
406 scb
->bufcnt
= status
;
408 scb
->bufp
= scb
->buf
;
412 /* Perform operations common to both old and new readchar. */
414 /* Return the next character from the input FIFO. If the FIFO is
415 empty, call the SERIAL specific routine to try and read in more
418 Initially data from the input FIFO is returned (fd_event()
419 pre-reads the input into that FIFO. Once that has been emptied,
420 further data is obtained by polling the input FD using the device
421 specific readchar() function. Note: reschedule() is called after
422 every read. This is because there is no guarentee that the lower
423 level fd_event() poll_event() code (which also calls reschedule())
427 generic_readchar (struct serial
*scb
, int timeout
,
428 int (do_readchar
) (struct serial
*scb
, int timeout
))
437 else if (scb
->bufcnt
< 0)
439 /* Some errors/eof are are sticky. */
444 ch
= do_readchar (scb
, timeout
);
447 switch ((enum serial_rc
) ch
)
451 /* Make the error/eof stick. */
461 /* Read any error output we might have. */
462 ser_base_read_error_fd (scb
, 1);
469 ser_base_readchar (struct serial
*scb
, int timeout
)
471 return generic_readchar (scb
, timeout
, do_ser_base_readchar
);
475 ser_base_write (struct serial
*scb
, const void *buf
, size_t count
)
477 const char *str
= (const char *) buf
;
484 cc
= scb
->ops
->write_prim (scb
, str
, count
);
499 ser_base_flush_output (struct serial
*scb
)
505 ser_base_flush_input (struct serial
*scb
)
507 if (scb
->bufcnt
>= 0)
510 scb
->bufp
= scb
->buf
;
518 ser_base_send_break (struct serial
*scb
)
524 ser_base_drain_output (struct serial
*scb
)
530 ser_base_raw (struct serial
*scb
)
532 return; /* Always in raw mode. */
536 ser_base_get_tty_state (struct serial
*scb
)
538 /* Allocate a dummy. */
539 return (serial_ttystate
) XNEW (int);
543 ser_base_copy_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
545 /* Allocate another dummy. */
546 return (serial_ttystate
) XNEW (int);
550 ser_base_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
556 ser_base_print_tty_state (struct serial
*scb
,
557 serial_ttystate ttystate
,
558 struct ui_file
*stream
)
560 /* Nothing to print. */
565 ser_base_setbaudrate (struct serial
*scb
, int rate
)
567 return 0; /* Never fails! */
571 ser_base_setstopbits (struct serial
*scb
, int num
)
573 return 0; /* Never fails! */
576 /* Implement the "setparity" serial_ops callback. */
579 ser_base_setparity (struct serial
*scb
, int parity
)
581 return 0; /* Never fails! */
584 /* Put the SERIAL device into/out-of ASYNC mode. */
587 ser_base_async (struct serial
*scb
,
592 /* Force a re-schedule. */
593 scb
->async_state
= NOTHING_SCHEDULED
;
594 if (serial_debug_p (scb
))
595 gdb_printf (gdb_stdlog
, "[fd%d->asynchronous]\n",
599 if (scb
->error_fd
!= -1)
600 add_file_handler (scb
->error_fd
, handle_error_fd
, scb
, "serial-error");
604 if (serial_debug_p (scb
))
605 gdb_printf (gdb_stdlog
, "[fd%d->synchronous]\n",
607 /* De-schedule whatever tasks are currently scheduled. */
608 switch (scb
->async_state
)
611 delete_file_handler (scb
->fd
);
613 case NOTHING_SCHEDULED
:
615 default: /* TIMER SCHEDULED */
616 delete_timer (scb
->async_state
);
620 if (scb
->error_fd
!= -1)
621 delete_file_handler (scb
->error_fd
);