]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/ser-base.c
Automatic date update in version.in
[thirdparty/binutils-gdb.git] / gdb / ser-base.c
CommitLineData
3eb25fda
MM
1/* Generic serial interface functions.
2
1d506c26 3 Copyright (C) 1992-2024 Free Software Foundation, Inc.
3eb25fda
MM
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
3eb25fda
MM
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
3eb25fda 19
e5dc0d5d 20#include "event-top.h"
3eb25fda 21#include "serial.h"
a07ff70c 22#include "ser-base.h"
400b5eca 23#include "gdbsupport/event-loop.h"
a07ff70c 24
06cc9596 25#include "gdbsupport/gdb_select.h"
268a13a5 26#include "gdbsupport/gdb_sys_time.h"
b4505029
MM
27#ifdef USE_WIN32API
28#include <winsock2.h>
29#endif
3eb25fda 30
401e7faf 31
3eb25fda
MM
32static timer_handler_func push_event;
33static handler_func fd_event;
34
35/* Event handling for ASYNC serial code.
36
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.
40
41 ASYNC only stops pestering its client when it is de-async'ed or it
c378eb4e 42 is told to go away. */
3eb25fda
MM
43
44/* Value of scb->async_state: */
45enum {
bd356ec6
SM
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. */
49
3eb25fda 50 /* The fd_event() handler is scheduled. It is called when ever the
c378eb4e 51 file descriptor becomes ready. */
bd356ec6
SM
52 FD_SCHEDULED = -1,
53
3eb25fda
MM
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
c378eb4e 56 forced into nothing scheduled. */
bd356ec6 57 NOTHING_SCHEDULED = -2
3eb25fda
MM
58};
59
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
c378eb4e 63 scheduled task is only changed when needed. */
3eb25fda 64
a26d8d11 65static void
3eb25fda
MM
66reschedule (struct serial *scb)
67{
68 if (serial_is_async_p (scb))
69 {
70 int next_state;
433759f7 71
3eb25fda
MM
72 switch (scb->async_state)
73 {
74 case FD_SCHEDULED:
75 if (scb->bufcnt == 0)
76 next_state = FD_SCHEDULED;
77 else
78 {
79 delete_file_handler (scb->fd);
80 next_state = create_timer (0, push_event, scb);
81 }
82 break;
83 case NOTHING_SCHEDULED:
84 if (scb->bufcnt == 0)
85 {
2554f6f5 86 add_file_handler (scb->fd, fd_event, scb, "serial");
3eb25fda
MM
87 next_state = FD_SCHEDULED;
88 }
89 else
90 {
91 next_state = create_timer (0, push_event, scb);
92 }
93 break;
94 default: /* TIMER SCHEDULED */
95 if (scb->bufcnt == 0)
96 {
97 delete_timer (scb->async_state);
2554f6f5 98 add_file_handler (scb->fd, fd_event, scb, "serial");
3eb25fda
MM
99 next_state = FD_SCHEDULED;
100 }
101 else
102 next_state = scb->async_state;
103 break;
104 }
105 if (serial_debug_p (scb))
106 {
107 switch (next_state)
108 {
109 case FD_SCHEDULED:
110 if (scb->async_state != FD_SCHEDULED)
6cb06a8c
TT
111 gdb_printf (gdb_stdlog, "[fd%d->fd-scheduled]\n",
112 scb->fd);
3eb25fda
MM
113 break;
114 default: /* TIMER SCHEDULED */
115 if (scb->async_state == FD_SCHEDULED)
6cb06a8c
TT
116 gdb_printf (gdb_stdlog, "[fd%d->timer-scheduled]\n",
117 scb->fd);
3eb25fda
MM
118 break;
119 }
120 }
121 scb->async_state = next_state;
122 }
123}
124
ddefb60f
PA
125/* Run the SCB's async handle, and reschedule, if the handler doesn't
126 close SCB. */
127
128static void
129run_async_handler_and_reschedule (struct serial *scb)
130{
131 int is_open;
132
133 /* Take a reference, so a serial_close call within the handler
134 doesn't make SCB a dangling pointer. */
135 serial_ref (scb);
136
137 /* Run the handler. */
138 scb->async_handler (scb, scb->async_context);
139
140 is_open = serial_is_open (scb);
141 serial_unref (scb);
142
143 /* Get ready for more, if not already closed. */
144 if (is_open)
145 reschedule (scb);
146}
147
3eb25fda
MM
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,
c378eb4e 152 push_event() is used to nag the client until it is. */
3eb25fda
MM
153
154static void
155fd_event (int error, void *context)
156{
19ba03f4 157 struct serial *scb = (struct serial *) context;
3eb25fda
MM
158 if (error != 0)
159 {
160 scb->bufcnt = SERIAL_ERROR;
161 }
162 else if (scb->bufcnt == 0)
163 {
164 /* Prime the input FIFO. The readchar() function is used to
dda83cd7
SM
165 pull characters out of the buffer. See also
166 generic_readchar(). */
3eb25fda 167 int nr;
75ee5925
PA
168
169 do
170 {
171 nr = scb->ops->read_prim (scb, BUFSIZ);
172 }
173 while (nr < 0 && errno == EINTR);
174
3eb25fda
MM
175 if (nr == 0)
176 {
177 scb->bufcnt = SERIAL_EOF;
178 }
179 else if (nr > 0)
180 {
181 scb->bufcnt = nr;
182 scb->bufp = scb->buf;
183 }
184 else
185 {
186 scb->bufcnt = SERIAL_ERROR;
187 }
188 }
ddefb60f 189 run_async_handler_and_reschedule (scb);
3eb25fda
MM
190}
191
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
85102364 195 device before nagging stops. */
3eb25fda
MM
196
197static void
198push_event (void *context)
199{
19ba03f4 200 struct serial *scb = (struct serial *) context;
433759f7 201
3eb25fda 202 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
ddefb60f 203 run_async_handler_and_reschedule (scb);
3eb25fda
MM
204}
205
b4505029 206/* Wait for input on scb, with timeout seconds. Returns 0 on success,
c378eb4e 207 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
b4505029 208
9bcbdca8
PA
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. */
213
b4505029
MM
214static int
215ser_base_wait_for (struct serial *scb, int timeout)
216{
217 while (1)
218 {
219 int numfds;
220 struct timeval tv;
221 fd_set readfds, exceptfds;
048094ac 222 int nfds;
b4505029
MM
223
224 /* NOTE: Some OS's can scramble the READFDS when the select()
dda83cd7
SM
225 call fails (ex the kernel with Red Hat 5.2). Initialize all
226 arguments before each call. */
b4505029
MM
227
228 tv.tv_sec = timeout;
229 tv.tv_usec = 0;
230
231 FD_ZERO (&readfds);
232 FD_ZERO (&exceptfds);
233 FD_SET (scb->fd, &readfds);
234 FD_SET (scb->fd, &exceptfds);
235
048094ac
PA
236 QUIT;
237
238 nfds = scb->fd + 1;
b4505029 239 if (timeout >= 0)
048094ac 240 numfds = interruptible_select (nfds, &readfds, 0, &exceptfds, &tv);
b4505029 241 else
048094ac 242 numfds = interruptible_select (nfds, &readfds, 0, &exceptfds, 0);
b4505029
MM
243
244 if (numfds <= 0)
245 {
246 if (numfds == 0)
247 return SERIAL_TIMEOUT;
248 else if (errno == EINTR)
249 continue;
250 else
c378eb4e
MS
251 return SERIAL_ERROR; /* Got an error from select or
252 poll. */
b4505029
MM
253 }
254
255 return 0;
256 }
257}
258
3347eb1a 259/* Read any error output we might have. */
260
261static void
262ser_base_read_error_fd (struct serial *scb, int close_fd)
263{
264 if (scb->error_fd != -1)
265 {
266 ssize_t s;
267 char buf[GDB_MI_MSG_WIDTH + 1];
268
269 for (;;)
270 {
271 char *current;
272 char *newline;
273 int to_read = GDB_MI_MSG_WIDTH;
274 int num_bytes = -1;
275
276 if (scb->ops->avail)
277 num_bytes = (scb->ops->avail)(scb, scb->error_fd);
278
279 if (num_bytes != -1)
280 to_read = (num_bytes < to_read) ? num_bytes : to_read;
281
282 if (to_read == 0)
283 break;
284
285 s = read (scb->error_fd, &buf, to_read);
286 if ((s == -1) || (s == 0 && !close_fd))
287 break;
288
289 if (s == 0 && close_fd)
290 {
291 /* End of file. */
a75868f5
PA
292 if (serial_is_async_p (scb))
293 delete_file_handler (scb->error_fd);
3347eb1a 294 close (scb->error_fd);
295 scb->error_fd = -1;
296 break;
297 }
298
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);
304 buf[s] = '\0';
305 current = buf;
306 while ((newline = strstr (current, "\n")) != NULL)
307 {
308 *newline = '\0';
0426ad51
TT
309 gdb_puts (current, gdb_stderr);
310 gdb_puts ("\n", gdb_stderr);
3347eb1a 311 current = newline + 1;
312 }
313
0426ad51 314 gdb_puts (current, gdb_stderr);
3347eb1a 315 }
316 }
317}
318
a75868f5
PA
319/* Event-loop callback for a serial's error_fd. Flushes any error
320 output we might have. */
321
322static void
323handle_error_fd (int error, gdb_client_data client_data)
324{
325 serial *scb = (serial *) client_data;
326
327 ser_base_read_error_fd (scb, 0);
328}
329
9bcbdca8
PA
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). */
b4505029
MM
335
336static int
337do_ser_base_readchar (struct serial *scb, int timeout)
338{
339 int status;
340 int delta;
341
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.
345
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. */
348
349 delta = (timeout == 0 ? 0 : 1);
350 while (1)
351 {
352 /* N.B. The UI may destroy our world (for instance by calling
dda83cd7
SM
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
357 returning 1. */
b4505029
MM
358
359 if (deprecated_ui_loop_hook)
360 {
361 if (deprecated_ui_loop_hook (0))
362 return SERIAL_TIMEOUT;
363 }
364
365 status = ser_base_wait_for (scb, delta);
366 if (timeout > 0)
dda83cd7 367 timeout -= delta;
b4505029
MM
368
369 /* If we got a character or an error back from wait_for, then we can
dda83cd7 370 break from the loop before the timeout is completed. */
b4505029
MM
371 if (status != SERIAL_TIMEOUT)
372 break;
373
374 /* If we have exhausted the original timeout, then generate
dda83cd7 375 a SERIAL_TIMEOUT, and pass it out of the loop. */
b4505029
MM
376 else if (timeout == 0)
377 {
378 status = SERIAL_TIMEOUT;
379 break;
380 }
3347eb1a 381
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);
b4505029
MM
386 }
387
388 if (status < 0)
389 return status;
390
75ee5925
PA
391 do
392 {
393 status = scb->ops->read_prim (scb, BUFSIZ);
394 }
395 while (status < 0 && errno == EINTR);
b4505029
MM
396
397 if (status <= 0)
398 {
399 if (status == 0)
dda83cd7 400 return SERIAL_EOF;
b4505029
MM
401 else
402 /* Got an error from read. */
403 return SERIAL_ERROR;
404 }
405
406 scb->bufcnt = status;
407 scb->bufcnt--;
408 scb->bufp = scb->buf;
409 return *scb->bufp++;
410}
411
c378eb4e 412/* Perform operations common to both old and new readchar. */
b4505029
MM
413
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
416 characters.
417
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())
c378eb4e 424 will be called. */
b4505029
MM
425
426int
427generic_readchar (struct serial *scb, int timeout,
428 int (do_readchar) (struct serial *scb, int timeout))
429{
430 int ch;
431 if (scb->bufcnt > 0)
432 {
433 ch = *scb->bufp;
434 scb->bufcnt--;
435 scb->bufp++;
436 }
437 else if (scb->bufcnt < 0)
438 {
c378eb4e 439 /* Some errors/eof are are sticky. */
b4505029
MM
440 ch = scb->bufcnt;
441 }
442 else
443 {
444 ch = do_readchar (scb, timeout);
445 if (ch < 0)
446 {
447 switch ((enum serial_rc) ch)
448 {
449 case SERIAL_EOF:
450 case SERIAL_ERROR:
c378eb4e 451 /* Make the error/eof stick. */
b4505029
MM
452 scb->bufcnt = ch;
453 break;
454 case SERIAL_TIMEOUT:
455 scb->bufcnt = 0;
456 break;
457 }
458 }
459 }
65cc4390 460
3347eb1a 461 /* Read any error output we might have. */
462 ser_base_read_error_fd (scb, 1);
65cc4390 463
b4505029
MM
464 reschedule (scb);
465 return ch;
466}
467
468int
469ser_base_readchar (struct serial *scb, int timeout)
470{
471 return generic_readchar (scb, timeout, do_ser_base_readchar);
472}
473
d69939bd 474void
c628b528 475ser_base_write (struct serial *scb, const void *buf, size_t count)
3eb25fda 476{
19ba03f4 477 const char *str = (const char *) buf;
3eb25fda
MM
478 int cc;
479
c628b528 480 while (count > 0)
3eb25fda 481 {
048094ac
PA
482 QUIT;
483
c628b528 484 cc = scb->ops->write_prim (scb, str, count);
3eb25fda
MM
485
486 if (cc < 0)
75ee5925
PA
487 {
488 if (errno == EINTR)
489 continue;
d69939bd 490 perror_with_name ("error while writing");
75ee5925 491 }
c628b528 492 count -= cc;
3eb25fda
MM
493 str += cc;
494 }
3eb25fda
MM
495}
496
497int
dd5da072 498ser_base_flush_output (struct serial *scb)
3eb25fda
MM
499{
500 return 0;
501}
502
503int
dd5da072 504ser_base_flush_input (struct serial *scb)
3eb25fda
MM
505{
506 if (scb->bufcnt >= 0)
507 {
508 scb->bufcnt = 0;
509 scb->bufp = scb->buf;
510 return 0;
511 }
512 else
513 return SERIAL_ERROR;
514}
515
d69939bd 516void
dd5da072 517ser_base_send_break (struct serial *scb)
3eb25fda 518{
3eb25fda
MM
519}
520
521int
dd5da072 522ser_base_drain_output (struct serial *scb)
3eb25fda
MM
523{
524 return 0;
525}
526
527void
dd5da072 528ser_base_raw (struct serial *scb)
3eb25fda 529{
c378eb4e 530 return; /* Always in raw mode. */
3eb25fda
MM
531}
532
533serial_ttystate
dd5da072 534ser_base_get_tty_state (struct serial *scb)
3eb25fda 535{
c378eb4e 536 /* Allocate a dummy. */
70ba0933 537 return (serial_ttystate) XNEW (int);
3eb25fda
MM
538}
539
1e182ce8
UW
540serial_ttystate
541ser_base_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
542{
543 /* Allocate another dummy. */
70ba0933 544 return (serial_ttystate) XNEW (int);
1e182ce8
UW
545}
546
3eb25fda 547int
dd5da072 548ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
3eb25fda
MM
549{
550 return 0;
551}
552
3eb25fda 553void
dd5da072
MM
554ser_base_print_tty_state (struct serial *scb,
555 serial_ttystate ttystate,
556 struct ui_file *stream)
3eb25fda
MM
557{
558 /* Nothing to print. */
559 return;
560}
561
ad3cf8c6 562void
dd5da072 563ser_base_setbaudrate (struct serial *scb, int rate)
3eb25fda 564{
ad3cf8c6 565 /* Never fails! */
3eb25fda
MM
566}
567
568int
dd5da072 569ser_base_setstopbits (struct serial *scb, int num)
3eb25fda 570{
c378eb4e 571 return 0; /* Never fails! */
3eb25fda
MM
572}
573
236af5e3
YG
574/* Implement the "setparity" serial_ops callback. */
575
576int
577ser_base_setparity (struct serial *scb, int parity)
578{
579 return 0; /* Never fails! */
580}
581
3eb25fda
MM
582/* Put the SERIAL device into/out-of ASYNC mode. */
583
584void
dd5da072 585ser_base_async (struct serial *scb,
3eb25fda
MM
586 int async_p)
587{
588 if (async_p)
589 {
c378eb4e 590 /* Force a re-schedule. */
3eb25fda
MM
591 scb->async_state = NOTHING_SCHEDULED;
592 if (serial_debug_p (scb))
6cb06a8c
TT
593 gdb_printf (gdb_stdlog, "[fd%d->asynchronous]\n",
594 scb->fd);
3eb25fda 595 reschedule (scb);
a75868f5
PA
596
597 if (scb->error_fd != -1)
2554f6f5 598 add_file_handler (scb->error_fd, handle_error_fd, scb, "serial-error");
3eb25fda
MM
599 }
600 else
601 {
602 if (serial_debug_p (scb))
6cb06a8c
TT
603 gdb_printf (gdb_stdlog, "[fd%d->synchronous]\n",
604 scb->fd);
c378eb4e 605 /* De-schedule whatever tasks are currently scheduled. */
3eb25fda
MM
606 switch (scb->async_state)
607 {
608 case FD_SCHEDULED:
609 delete_file_handler (scb->fd);
610 break;
611 case NOTHING_SCHEDULED:
612 break;
613 default: /* TIMER SCHEDULED */
614 delete_timer (scb->async_state);
615 break;
616 }
a75868f5
PA
617
618 if (scb->error_fd != -1)
619 delete_file_handler (scb->error_fd);
3eb25fda
MM
620 }
621}