]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ser-base.c
Include gdb_assert.h in common-defs.h
[thirdparty/binutils-gdb.git] / gdb / ser-base.c
1 /* Generic serial interface functions.
2
3 Copyright (C) 1992-2014 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include "ser-base.h"
23 #include "event-loop.h"
24
25 #include "gdb_select.h"
26 #include <string.h>
27 #include <sys/time.h>
28 #ifdef USE_WIN32API
29 #include <winsock2.h>
30 #endif
31
32
33 static timer_handler_func push_event;
34 static handler_func fd_event;
35
36 /* Event handling for ASYNC serial code.
37
38 At any time the SERIAL device either: has an empty FIFO and is
39 waiting on a FD event; or has a non-empty FIFO/error condition and
40 is constantly scheduling timer events.
41
42 ASYNC only stops pestering its client when it is de-async'ed or it
43 is told to go away. */
44
45 /* Value of scb->async_state: */
46 enum {
47 /* >= 0 (TIMER_SCHEDULED) */
48 /* The ID of the currently scheduled timer event. This state is
49 rarely encountered. Timer events are one-off so as soon as the
50 event is delivered the state is shanged to NOTHING_SCHEDULED. */
51 FD_SCHEDULED = -1,
52 /* The fd_event() handler is scheduled. It is called when ever the
53 file descriptor becomes ready. */
54 NOTHING_SCHEDULED = -2
55 /* Either no task is scheduled (just going into ASYNC mode) or a
56 timer event has just gone off and the current state has been
57 forced into nothing scheduled. */
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
63 scheduled task is only changed when needed. */
64
65 static void
66 reschedule (struct serial *scb)
67 {
68 if (serial_is_async_p (scb))
69 {
70 int next_state;
71
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 {
86 add_file_handler (scb->fd, fd_event, scb);
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);
98 add_file_handler (scb->fd, fd_event, scb);
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)
111 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
112 scb->fd);
113 break;
114 default: /* TIMER SCHEDULED */
115 if (scb->async_state == FD_SCHEDULED)
116 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
117 scb->fd);
118 break;
119 }
120 }
121 scb->async_state = next_state;
122 }
123 }
124
125 /* Run the SCB's async handle, and reschedule, if the handler doesn't
126 close SCB. */
127
128 static void
129 run_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
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. */
153
154 static void
155 fd_event (int error, void *context)
156 {
157 struct serial *scb = context;
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
165 pull characters out of the buffer. See also
166 generic_readchar(). */
167 int nr;
168 nr = scb->ops->read_prim (scb, BUFSIZ);
169 if (nr == 0)
170 {
171 scb->bufcnt = SERIAL_EOF;
172 }
173 else if (nr > 0)
174 {
175 scb->bufcnt = nr;
176 scb->bufp = scb->buf;
177 }
178 else
179 {
180 scb->bufcnt = SERIAL_ERROR;
181 }
182 }
183 run_async_handler_and_reschedule (scb);
184 }
185
186 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
187 error). Nag the client until all the data has been read. In the
188 case of errors, the client will need to close or de-async the
189 device before naging stops. */
190
191 static void
192 push_event (void *context)
193 {
194 struct serial *scb = context;
195
196 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
197 run_async_handler_and_reschedule (scb);
198 }
199
200 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
201 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
202
203 static int
204 ser_base_wait_for (struct serial *scb, int timeout)
205 {
206 while (1)
207 {
208 int numfds;
209 struct timeval tv;
210 fd_set readfds, exceptfds;
211
212 /* NOTE: Some OS's can scramble the READFDS when the select()
213 call fails (ex the kernel with Red Hat 5.2). Initialize all
214 arguments before each call. */
215
216 tv.tv_sec = timeout;
217 tv.tv_usec = 0;
218
219 FD_ZERO (&readfds);
220 FD_ZERO (&exceptfds);
221 FD_SET (scb->fd, &readfds);
222 FD_SET (scb->fd, &exceptfds);
223
224 if (timeout >= 0)
225 numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
226 else
227 numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
228
229 if (numfds <= 0)
230 {
231 if (numfds == 0)
232 return SERIAL_TIMEOUT;
233 else if (errno == EINTR)
234 continue;
235 else
236 return SERIAL_ERROR; /* Got an error from select or
237 poll. */
238 }
239
240 return 0;
241 }
242 }
243
244 /* Read any error output we might have. */
245
246 static void
247 ser_base_read_error_fd (struct serial *scb, int close_fd)
248 {
249 if (scb->error_fd != -1)
250 {
251 ssize_t s;
252 char buf[GDB_MI_MSG_WIDTH + 1];
253
254 for (;;)
255 {
256 char *current;
257 char *newline;
258 int to_read = GDB_MI_MSG_WIDTH;
259 int num_bytes = -1;
260
261 if (scb->ops->avail)
262 num_bytes = (scb->ops->avail)(scb, scb->error_fd);
263
264 if (num_bytes != -1)
265 to_read = (num_bytes < to_read) ? num_bytes : to_read;
266
267 if (to_read == 0)
268 break;
269
270 s = read (scb->error_fd, &buf, to_read);
271 if ((s == -1) || (s == 0 && !close_fd))
272 break;
273
274 if (s == 0 && close_fd)
275 {
276 /* End of file. */
277 close (scb->error_fd);
278 scb->error_fd = -1;
279 break;
280 }
281
282 /* In theory, embedded newlines are not a problem.
283 But for MI, we want each output line to have just
284 one newline for legibility. So output things
285 in newline chunks. */
286 gdb_assert (s > 0 && s <= GDB_MI_MSG_WIDTH);
287 buf[s] = '\0';
288 current = buf;
289 while ((newline = strstr (current, "\n")) != NULL)
290 {
291 *newline = '\0';
292 fputs_unfiltered (current, gdb_stderr);
293 fputs_unfiltered ("\n", gdb_stderr);
294 current = newline + 1;
295 }
296
297 fputs_unfiltered (current, gdb_stderr);
298 }
299 }
300 }
301
302 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
303 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
304 char if successful. Returns -2 if timeout expired, EOF if line dropped
305 dead, or -3 for any other error (see errno in that case). */
306
307 static int
308 do_ser_base_readchar (struct serial *scb, int timeout)
309 {
310 int status;
311 int delta;
312
313 /* We have to be able to keep the GUI alive here, so we break the
314 original timeout into steps of 1 second, running the "keep the
315 GUI alive" hook each time through the loop.
316
317 Also, timeout = 0 means to poll, so we just set the delta to 0,
318 so we will only go through the loop once. */
319
320 delta = (timeout == 0 ? 0 : 1);
321 while (1)
322 {
323 /* N.B. The UI may destroy our world (for instance by calling
324 remote_stop,) in which case we want to get out of here as
325 quickly as possible. It is not safe to touch scb, since
326 someone else might have freed it. The
327 deprecated_ui_loop_hook signals that we should exit by
328 returning 1. */
329
330 if (deprecated_ui_loop_hook)
331 {
332 if (deprecated_ui_loop_hook (0))
333 return SERIAL_TIMEOUT;
334 }
335
336 status = ser_base_wait_for (scb, delta);
337 if (timeout > 0)
338 timeout -= delta;
339
340 /* If we got a character or an error back from wait_for, then we can
341 break from the loop before the timeout is completed. */
342 if (status != SERIAL_TIMEOUT)
343 break;
344
345 /* If we have exhausted the original timeout, then generate
346 a SERIAL_TIMEOUT, and pass it out of the loop. */
347 else if (timeout == 0)
348 {
349 status = SERIAL_TIMEOUT;
350 break;
351 }
352
353 /* We also need to check and consume the stderr because it could
354 come before the stdout for some stubs. If we just sit and wait
355 for stdout, we would hit a deadlock for that case. */
356 ser_base_read_error_fd (scb, 0);
357 }
358
359 if (status < 0)
360 return status;
361
362 status = scb->ops->read_prim (scb, BUFSIZ);
363
364 if (status <= 0)
365 {
366 if (status == 0)
367 return SERIAL_EOF;
368 else
369 /* Got an error from read. */
370 return SERIAL_ERROR;
371 }
372
373 scb->bufcnt = status;
374 scb->bufcnt--;
375 scb->bufp = scb->buf;
376 return *scb->bufp++;
377 }
378
379 /* Perform operations common to both old and new readchar. */
380
381 /* Return the next character from the input FIFO. If the FIFO is
382 empty, call the SERIAL specific routine to try and read in more
383 characters.
384
385 Initially data from the input FIFO is returned (fd_event()
386 pre-reads the input into that FIFO. Once that has been emptied,
387 further data is obtained by polling the input FD using the device
388 specific readchar() function. Note: reschedule() is called after
389 every read. This is because there is no guarentee that the lower
390 level fd_event() poll_event() code (which also calls reschedule())
391 will be called. */
392
393 int
394 generic_readchar (struct serial *scb, int timeout,
395 int (do_readchar) (struct serial *scb, int timeout))
396 {
397 int ch;
398 if (scb->bufcnt > 0)
399 {
400 ch = *scb->bufp;
401 scb->bufcnt--;
402 scb->bufp++;
403 }
404 else if (scb->bufcnt < 0)
405 {
406 /* Some errors/eof are are sticky. */
407 ch = scb->bufcnt;
408 }
409 else
410 {
411 ch = do_readchar (scb, timeout);
412 if (ch < 0)
413 {
414 switch ((enum serial_rc) ch)
415 {
416 case SERIAL_EOF:
417 case SERIAL_ERROR:
418 /* Make the error/eof stick. */
419 scb->bufcnt = ch;
420 break;
421 case SERIAL_TIMEOUT:
422 scb->bufcnt = 0;
423 break;
424 }
425 }
426 }
427
428 /* Read any error output we might have. */
429 ser_base_read_error_fd (scb, 1);
430
431 reschedule (scb);
432 return ch;
433 }
434
435 int
436 ser_base_readchar (struct serial *scb, int timeout)
437 {
438 return generic_readchar (scb, timeout, do_ser_base_readchar);
439 }
440
441 int
442 ser_base_write (struct serial *scb, const void *buf, size_t count)
443 {
444 const char *str = buf;
445 int cc;
446
447 while (count > 0)
448 {
449 cc = scb->ops->write_prim (scb, str, count);
450
451 if (cc < 0)
452 return 1;
453 count -= cc;
454 str += cc;
455 }
456 return 0;
457 }
458
459 int
460 ser_base_flush_output (struct serial *scb)
461 {
462 return 0;
463 }
464
465 int
466 ser_base_flush_input (struct serial *scb)
467 {
468 if (scb->bufcnt >= 0)
469 {
470 scb->bufcnt = 0;
471 scb->bufp = scb->buf;
472 return 0;
473 }
474 else
475 return SERIAL_ERROR;
476 }
477
478 int
479 ser_base_send_break (struct serial *scb)
480 {
481 return 0;
482 }
483
484 int
485 ser_base_drain_output (struct serial *scb)
486 {
487 return 0;
488 }
489
490 void
491 ser_base_raw (struct serial *scb)
492 {
493 return; /* Always in raw mode. */
494 }
495
496 serial_ttystate
497 ser_base_get_tty_state (struct serial *scb)
498 {
499 /* Allocate a dummy. */
500 return (serial_ttystate) XNEW (int);
501 }
502
503 serial_ttystate
504 ser_base_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
505 {
506 /* Allocate another dummy. */
507 return (serial_ttystate) XNEW (int);
508 }
509
510 int
511 ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
512 {
513 return 0;
514 }
515
516 int
517 ser_base_noflush_set_tty_state (struct serial *scb,
518 serial_ttystate new_ttystate,
519 serial_ttystate old_ttystate)
520 {
521 return 0;
522 }
523
524 void
525 ser_base_print_tty_state (struct serial *scb,
526 serial_ttystate ttystate,
527 struct ui_file *stream)
528 {
529 /* Nothing to print. */
530 return;
531 }
532
533 int
534 ser_base_setbaudrate (struct serial *scb, int rate)
535 {
536 return 0; /* Never fails! */
537 }
538
539 int
540 ser_base_setstopbits (struct serial *scb, int num)
541 {
542 return 0; /* Never fails! */
543 }
544
545 /* Put the SERIAL device into/out-of ASYNC mode. */
546
547 void
548 ser_base_async (struct serial *scb,
549 int async_p)
550 {
551 if (async_p)
552 {
553 /* Force a re-schedule. */
554 scb->async_state = NOTHING_SCHEDULED;
555 if (serial_debug_p (scb))
556 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
557 scb->fd);
558 reschedule (scb);
559 }
560 else
561 {
562 if (serial_debug_p (scb))
563 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
564 scb->fd);
565 /* De-schedule whatever tasks are currently scheduled. */
566 switch (scb->async_state)
567 {
568 case FD_SCHEDULED:
569 delete_file_handler (scb->fd);
570 break;
571 case NOTHING_SCHEDULED:
572 break;
573 default: /* TIMER SCHEDULED */
574 delete_timer (scb->async_state);
575 break;
576 }
577 }
578 }