GDB (xrefs)
/tmp/gdb-7.10/gdb/ser-base.c
Go to the documentation of this file.
1 /* Generic serial interface functions.
2 
3  Copyright (C) 1992-2015 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 <sys/time.h>
27 #ifdef USE_WIN32API
28 #include <winsock2.h>
29 #endif
30 
31 
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
42  is told to go away. */
43 
44 /* Value of scb->async_state: */
45 enum {
46  /* >= 0 (TIMER_SCHEDULED) */
47  /* The ID of the currently scheduled timer event. This state is
48  rarely encountered. Timer events are one-off so as soon as the
49  event is delivered the state is shanged to NOTHING_SCHEDULED. */
51  /* The fd_event() handler is scheduled. It is called when ever the
52  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 };
58 
59 /* Identify and schedule the next ASYNC task based on scb->async_state
60  and scb->buf* (the input FIFO). A state machine is used to avoid
61  the need to make redundant calls into the event-loop - the next
62  scheduled task is only changed when needed. */
63 
64 static void
65 reschedule (struct serial *scb)
66 {
67  if (serial_is_async_p (scb))
68  {
69  int next_state;
70 
71  switch (scb->async_state)
72  {
73  case FD_SCHEDULED:
74  if (scb->bufcnt == 0)
75  next_state = FD_SCHEDULED;
76  else
77  {
78  delete_file_handler (scb->fd);
79  next_state = create_timer (0, push_event, scb);
80  }
81  break;
82  case NOTHING_SCHEDULED:
83  if (scb->bufcnt == 0)
84  {
85  add_file_handler (scb->fd, fd_event, scb);
86  next_state = FD_SCHEDULED;
87  }
88  else
89  {
90  next_state = create_timer (0, push_event, scb);
91  }
92  break;
93  default: /* TIMER SCHEDULED */
94  if (scb->bufcnt == 0)
95  {
97  add_file_handler (scb->fd, fd_event, scb);
98  next_state = FD_SCHEDULED;
99  }
100  else
101  next_state = scb->async_state;
102  break;
103  }
104  if (serial_debug_p (scb))
105  {
106  switch (next_state)
107  {
108  case FD_SCHEDULED:
109  if (scb->async_state != FD_SCHEDULED)
110  fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
111  scb->fd);
112  break;
113  default: /* TIMER SCHEDULED */
114  if (scb->async_state == FD_SCHEDULED)
115  fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
116  scb->fd);
117  break;
118  }
119  }
120  scb->async_state = next_state;
121  }
122 }
123 
124 /* Run the SCB's async handle, and reschedule, if the handler doesn't
125  close SCB. */
126 
127 static void
129 {
130  int is_open;
131 
132  /* Take a reference, so a serial_close call within the handler
133  doesn't make SCB a dangling pointer. */
134  serial_ref (scb);
135 
136  /* Run the handler. */
137  scb->async_handler (scb, scb->async_context);
138 
139  is_open = serial_is_open (scb);
140  serial_unref (scb);
141 
142  /* Get ready for more, if not already closed. */
143  if (is_open)
144  reschedule (scb);
145 }
146 
147 /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
148  is no pending error). As soon as data arrives, it is read into the
149  input FIFO and the client notified. The client should then drain
150  the FIFO using readchar(). If the FIFO isn't immediatly emptied,
151  push_event() is used to nag the client until it is. */
152 
153 static void
154 fd_event (int error, void *context)
155 {
156  struct serial *scb = context;
157  if (error != 0)
158  {
159  scb->bufcnt = SERIAL_ERROR;
160  }
161  else if (scb->bufcnt == 0)
162  {
163  /* Prime the input FIFO. The readchar() function is used to
164  pull characters out of the buffer. See also
165  generic_readchar(). */
166  int nr;
167  nr = scb->ops->read_prim (scb, BUFSIZ);
168  if (nr == 0)
169  {
170  scb->bufcnt = SERIAL_EOF;
171  }
172  else if (nr > 0)
173  {
174  scb->bufcnt = nr;
175  scb->bufp = scb->buf;
176  }
177  else
178  {
179  scb->bufcnt = SERIAL_ERROR;
180  }
181  }
183 }
184 
185 /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
186  error). Nag the client until all the data has been read. In the
187  case of errors, the client will need to close or de-async the
188  device before naging stops. */
189 
190 static void
191 push_event (void *context)
192 {
193  struct serial *scb = context;
194 
195  scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
197 }
198 
199 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
200  otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
201 
202 static int
203 ser_base_wait_for (struct serial *scb, int timeout)
204 {
205  while (1)
206  {
207  int numfds;
208  struct timeval tv;
209  fd_set readfds, exceptfds;
210 
211  /* NOTE: Some OS's can scramble the READFDS when the select()
212  call fails (ex the kernel with Red Hat 5.2). Initialize all
213  arguments before each call. */
214 
215  tv.tv_sec = timeout;
216  tv.tv_usec = 0;
217 
218  FD_ZERO (&readfds);
219  FD_ZERO (&exceptfds);
220  FD_SET (scb->fd, &readfds);
221  FD_SET (scb->fd, &exceptfds);
222 
223  if (timeout >= 0)
224  numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
225  else
226  numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
227 
228  if (numfds <= 0)
229  {
230  if (numfds == 0)
231  return SERIAL_TIMEOUT;
232  else if (errno == EINTR)
233  continue;
234  else
235  return SERIAL_ERROR; /* Got an error from select or
236  poll. */
237  }
238 
239  return 0;
240  }
241 }
242 
243 /* Read any error output we might have. */
244 
245 static void
246 ser_base_read_error_fd (struct serial *scb, int close_fd)
247 {
248  if (scb->error_fd != -1)
249  {
250  ssize_t s;
251  char buf[GDB_MI_MSG_WIDTH + 1];
252 
253  for (;;)
254  {
255  char *current;
256  char *newline;
257  int to_read = GDB_MI_MSG_WIDTH;
258  int num_bytes = -1;
259 
260  if (scb->ops->avail)
261  num_bytes = (scb->ops->avail)(scb, scb->error_fd);
262 
263  if (num_bytes != -1)
264  to_read = (num_bytes < to_read) ? num_bytes : to_read;
265 
266  if (to_read == 0)
267  break;
268 
269  s = read (scb->error_fd, &buf, to_read);
270  if ((s == -1) || (s == 0 && !close_fd))
271  break;
272 
273  if (s == 0 && close_fd)
274  {
275  /* End of file. */
276  close (scb->error_fd);
277  scb->error_fd = -1;
278  break;
279  }
280 
281  /* In theory, embedded newlines are not a problem.
282  But for MI, we want each output line to have just
283  one newline for legibility. So output things
284  in newline chunks. */
285  gdb_assert (s > 0 && s <= GDB_MI_MSG_WIDTH);
286  buf[s] = '\0';
287  current = buf;
288  while ((newline = strstr (current, "\n")) != NULL)
289  {
290  *newline = '\0';
291  fputs_unfiltered (current, gdb_stderr);
293  current = newline + 1;
294  }
295 
296  fputs_unfiltered (current, gdb_stderr);
297  }
298  }
299 }
300 
301 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
302  to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
303  char if successful. Returns -2 if timeout expired, EOF if line dropped
304  dead, or -3 for any other error (see errno in that case). */
305 
306 static int
307 do_ser_base_readchar (struct serial *scb, int timeout)
308 {
309  int status;
310  int delta;
311 
312  /* We have to be able to keep the GUI alive here, so we break the
313  original timeout into steps of 1 second, running the "keep the
314  GUI alive" hook each time through the loop.
315 
316  Also, timeout = 0 means to poll, so we just set the delta to 0,
317  so we will only go through the loop once. */
318 
319  delta = (timeout == 0 ? 0 : 1);
320  while (1)
321  {
322  /* N.B. The UI may destroy our world (for instance by calling
323  remote_stop,) in which case we want to get out of here as
324  quickly as possible. It is not safe to touch scb, since
325  someone else might have freed it. The
326  deprecated_ui_loop_hook signals that we should exit by
327  returning 1. */
328 
330  {
331  if (deprecated_ui_loop_hook (0))
332  return SERIAL_TIMEOUT;
333  }
334 
335  status = ser_base_wait_for (scb, delta);
336  if (timeout > 0)
337  timeout -= delta;
338 
339  /* If we got a character or an error back from wait_for, then we can
340  break from the loop before the timeout is completed. */
341  if (status != SERIAL_TIMEOUT)
342  break;
343 
344  /* If we have exhausted the original timeout, then generate
345  a SERIAL_TIMEOUT, and pass it out of the loop. */
346  else if (timeout == 0)
347  {
348  status = SERIAL_TIMEOUT;
349  break;
350  }
351 
352  /* We also need to check and consume the stderr because it could
353  come before the stdout for some stubs. If we just sit and wait
354  for stdout, we would hit a deadlock for that case. */
355  ser_base_read_error_fd (scb, 0);
356  }
357 
358  if (status < 0)
359  return status;
360 
361  status = scb->ops->read_prim (scb, BUFSIZ);
362 
363  if (status <= 0)
364  {
365  if (status == 0)
366  return SERIAL_EOF;
367  else
368  /* Got an error from read. */
369  return SERIAL_ERROR;
370  }
371 
372  scb->bufcnt = status;
373  scb->bufcnt--;
374  scb->bufp = scb->buf;
375  return *scb->bufp++;
376 }
377 
378 /* Perform operations common to both old and new readchar. */
379 
380 /* Return the next character from the input FIFO. If the FIFO is
381  empty, call the SERIAL specific routine to try and read in more
382  characters.
383 
384  Initially data from the input FIFO is returned (fd_event()
385  pre-reads the input into that FIFO. Once that has been emptied,
386  further data is obtained by polling the input FD using the device
387  specific readchar() function. Note: reschedule() is called after
388  every read. This is because there is no guarentee that the lower
389  level fd_event() poll_event() code (which also calls reschedule())
390  will be called. */
391 
392 int
393 generic_readchar (struct serial *scb, int timeout,
394  int (do_readchar) (struct serial *scb, int timeout))
395 {
396  int ch;
397  if (scb->bufcnt > 0)
398  {
399  ch = *scb->bufp;
400  scb->bufcnt--;
401  scb->bufp++;
402  }
403  else if (scb->bufcnt < 0)
404  {
405  /* Some errors/eof are are sticky. */
406  ch = scb->bufcnt;
407  }
408  else
409  {
410  ch = do_readchar (scb, timeout);
411  if (ch < 0)
412  {
413  switch ((enum serial_rc) ch)
414  {
415  case SERIAL_EOF:
416  case SERIAL_ERROR:
417  /* Make the error/eof stick. */
418  scb->bufcnt = ch;
419  break;
420  case SERIAL_TIMEOUT:
421  scb->bufcnt = 0;
422  break;
423  }
424  }
425  }
426 
427  /* Read any error output we might have. */
428  ser_base_read_error_fd (scb, 1);
429 
430  reschedule (scb);
431  return ch;
432 }
433 
434 int
435 ser_base_readchar (struct serial *scb, int timeout)
436 {
437  return generic_readchar (scb, timeout, do_ser_base_readchar);
438 }
439 
440 int
441 ser_base_write (struct serial *scb, const void *buf, size_t count)
442 {
443  const char *str = buf;
444  int cc;
445 
446  while (count > 0)
447  {
448  cc = scb->ops->write_prim (scb, str, count);
449 
450  if (cc < 0)
451  return 1;
452  count -= cc;
453  str += cc;
454  }
455  return 0;
456 }
457 
458 int
460 {
461  return 0;
462 }
463 
464 int
466 {
467  if (scb->bufcnt >= 0)
468  {
469  scb->bufcnt = 0;
470  scb->bufp = scb->buf;
471  return 0;
472  }
473  else
474  return SERIAL_ERROR;
475 }
476 
477 int
479 {
480  return 0;
481 }
482 
483 int
485 {
486  return 0;
487 }
488 
489 void
490 ser_base_raw (struct serial *scb)
491 {
492  return; /* Always in raw mode. */
493 }
494 
497 {
498  /* Allocate a dummy. */
499  return (serial_ttystate) XNEW (int);
500 }
501 
504 {
505  /* Allocate another dummy. */
506  return (serial_ttystate) XNEW (int);
507 }
508 
509 int
511 {
512  return 0;
513 }
514 
515 int
517  serial_ttystate new_ttystate,
518  serial_ttystate old_ttystate)
519 {
520  return 0;
521 }
522 
523 void
525  serial_ttystate ttystate,
526  struct ui_file *stream)
527 {
528  /* Nothing to print. */
529  return;
530 }
531 
532 int
533 ser_base_setbaudrate (struct serial *scb, int rate)
534 {
535  return 0; /* Never fails! */
536 }
537 
538 int
539 ser_base_setstopbits (struct serial *scb, int num)
540 {
541  return 0; /* Never fails! */
542 }
543 
544 /* Implement the "setparity" serial_ops callback. */
545 
546 int
547 ser_base_setparity (struct serial *scb, int parity)
548 {
549  return 0; /* Never fails! */
550 }
551 
552 /* Put the SERIAL device into/out-of ASYNC mode. */
553 
554 void
555 ser_base_async (struct serial *scb,
556  int async_p)
557 {
558  if (async_p)
559  {
560  /* Force a re-schedule. */
562  if (serial_debug_p (scb))
563  fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
564  scb->fd);
565  reschedule (scb);
566  }
567  else
568  {
569  if (serial_debug_p (scb))
570  fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
571  scb->fd);
572  /* De-schedule whatever tasks are currently scheduled. */
573  switch (scb->async_state)
574  {
575  case FD_SCHEDULED:
576  delete_file_handler (scb->fd);
577  break;
578  case NOTHING_SCHEDULED:
579  break;
580  default: /* TIMER SCHEDULED */
581  delete_timer (scb->async_state);
582  break;
583  }
584  }
585 }
ssize_t read(int fd, void *buf, size_t count)
Definition: expect-read1.c:26
int serial_is_async_p(struct serial *scb)
Definition: serial.c:542
void( timer_handler_func)(gdb_client_data)
Definition: event-loop.h:76
int generic_readchar(struct serial *scb, int timeout, int(do_readchar)(struct serial *scb, int timeout))
Definition: ser-base.c:393
serial_event_ftype * async_handler
Definition: serial.h:256
int ser_base_set_tty_state(struct serial *scb, serial_ttystate ttystate)
Definition: ser-base.c:510
void fputs_unfiltered(const char *buf, struct ui_file *file)
Definition: ui-file.c:252
int ser_base_flush_output(struct serial *scb)
Definition: ser-base.c:459
static timer_handler_func push_event
Definition: ser-base.c:32
int async_state
Definition: serial.h:254
int error_fd
Definition: serial.h:238
int ser_base_setbaudrate(struct serial *scb, int rate)
Definition: ser-base.c:533
int ser_base_setstopbits(struct serial *scb, int num)
Definition: ser-base.c:539
static void reschedule(struct serial *scb)
Definition: ser-base.c:65
int(* read_prim)(struct serial *scb, size_t count)
Definition: serial.h:293
void * async_context
Definition: serial.h:255
int serial_debug_p(struct serial *scb)
Definition: serial.c:568
void add_file_handler(int fd, handler_func *proc, gdb_client_data client_data)
Definition: event-loop.c:388
static void ser_base_read_error_fd(struct serial *scb, int close_fd)
Definition: ser-base.c:246
void ser_base_raw(struct serial *scb)
Definition: ser-base.c:490
void ser_base_async(struct serial *scb, int async_p)
Definition: ser-base.c:555
unsigned char * bufp
Definition: serial.h:244
static int timeout
Definition: monitor.c:75
void serial_unref(struct serial *scb)
Definition: serial.c:364
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2361
mach_port_t mach_port_t name mach_port_t mach_port_t name error_t int status
Definition: gnu-nat.c:1816
int ser_base_noflush_set_tty_state(struct serial *scb, serial_ttystate new_ttystate, serial_ttystate old_ttystate)
Definition: ser-base.c:516
int(* write_prim)(struct serial *scb, const void *buf, size_t count)
Definition: serial.h:296
static handler_func fd_event
Definition: ser-base.c:33
static const char * parity
Definition: serial.c:658
#define gdb_assert(expr)
Definition: gdb_assert.h:33
#define GDB_MI_MSG_WIDTH
Definition: defs.h:671
Definition: serial.h:227
int gdb_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
Definition: mingw-hdep.c:59
int ser_base_drain_output(struct serial *scb)
Definition: ser-base.c:484
unsigned char buf[BUFSIZ]
Definition: serial.h:245
struct ui_file * gdb_stdlog
Definition: main.c:73
serial_rc
Definition: serial.h:94
void( handler_func)(int, gdb_client_data)
Definition: event-loop.h:73
void ser_base_print_tty_state(struct serial *scb, serial_ttystate ttystate, struct ui_file *stream)
Definition: ser-base.c:524
int create_timer(int milliseconds, timer_handler_func *proc, gdb_client_data client_data)
Definition: event-loop.c:1080
int ser_base_setparity(struct serial *scb, int parity)
Definition: ser-base.c:547
int(* avail)(struct serial *scb, int fd)
Definition: serial.h:301
struct ui_file * gdb_stderr
Definition: main.c:72
int ser_base_flush_input(struct serial *scb)
Definition: ser-base.c:465
int ser_base_readchar(struct serial *scb, int timeout)
Definition: ser-base.c:435
int fd
Definition: serial.h:233
int ser_base_send_break(struct serial *scb)
Definition: ser-base.c:478
int ser_base_write(struct serial *scb, const void *buf, size_t count)
Definition: ser-base.c:441
int bufcnt
Definition: serial.h:242
void delete_timer(int id)
Definition: event-loop.c:1146
static void run_async_handler_and_reschedule(struct serial *scb)
Definition: ser-base.c:128
void serial_ref(struct serial *scb)
Definition: serial.c:358
serial_ttystate ser_base_copy_tty_state(struct serial *scb, serial_ttystate ttystate)
Definition: ser-base.c:503
void * serial_ttystate
Definition: serial.h:27
int serial_is_open(struct serial *scb)
Definition: serial.c:352
static int do_ser_base_readchar(struct serial *scb, int timeout)
Definition: ser-base.c:307
const struct serial_ops * ops
Definition: serial.h:239
void error(const char *fmt,...)
Definition: errors.c:38
void delete_file_handler(int fd)
Definition: event-loop.c:539
static int ser_base_wait_for(struct serial *scb, int timeout)
Definition: ser-base.c:203
int(* deprecated_ui_loop_hook)(int signo)
Definition: top.c:180
serial_ttystate ser_base_get_tty_state(struct serial *scb)
Definition: ser-base.c:496
int rate
Definition: ser-unix.c:683