daily update
[deliverable/binutils-gdb.git] / gdb / ser-base.c
CommitLineData
3eb25fda
MM
1/* Generic serial interface functions.
2
6aba47ca 3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2003,
7b6bb8da
JB
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
3eb25fda
MM
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
3eb25fda
MM
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
3eb25fda
MM
21
22#include "defs.h"
23#include "serial.h"
a07ff70c 24#include "ser-base.h"
3eb25fda 25#include "event-loop.h"
a07ff70c 26
0ea3f30e 27#include "gdb_select.h"
a07ff70c 28#include "gdb_string.h"
248697b2 29#include <sys/time.h>
b4505029
MM
30#ifdef USE_WIN32API
31#include <winsock2.h>
32#endif
3eb25fda 33
401e7faf 34
3eb25fda
MM
35static timer_handler_func push_event;
36static handler_func fd_event;
37
38/* Event handling for ASYNC serial code.
39
40 At any time the SERIAL device either: has an empty FIFO and is
41 waiting on a FD event; or has a non-empty FIFO/error condition and
42 is constantly scheduling timer events.
43
44 ASYNC only stops pestering its client when it is de-async'ed or it
c378eb4e 45 is told to go away. */
3eb25fda
MM
46
47/* Value of scb->async_state: */
48enum {
49 /* >= 0 (TIMER_SCHEDULED) */
c378eb4e 50 /* The ID of the currently scheduled timer event. This state is
3eb25fda 51 rarely encountered. Timer events are one-off so as soon as the
c378eb4e 52 event is delivered the state is shanged to NOTHING_SCHEDULED. */
3eb25fda
MM
53 FD_SCHEDULED = -1,
54 /* The fd_event() handler is scheduled. It is called when ever the
c378eb4e 55 file descriptor becomes ready. */
3eb25fda
MM
56 NOTHING_SCHEDULED = -2
57 /* Either no task is scheduled (just going into ASYNC mode) or a
58 timer event has just gone off and the current state has been
c378eb4e 59 forced into nothing scheduled. */
3eb25fda
MM
60};
61
62/* Identify and schedule the next ASYNC task based on scb->async_state
63 and scb->buf* (the input FIFO). A state machine is used to avoid
64 the need to make redundant calls into the event-loop - the next
c378eb4e 65 scheduled task is only changed when needed. */
3eb25fda 66
a26d8d11 67static void
3eb25fda
MM
68reschedule (struct serial *scb)
69{
70 if (serial_is_async_p (scb))
71 {
72 int next_state;
433759f7 73
3eb25fda
MM
74 switch (scb->async_state)
75 {
76 case FD_SCHEDULED:
77 if (scb->bufcnt == 0)
78 next_state = FD_SCHEDULED;
79 else
80 {
81 delete_file_handler (scb->fd);
82 next_state = create_timer (0, push_event, scb);
83 }
84 break;
85 case NOTHING_SCHEDULED:
86 if (scb->bufcnt == 0)
87 {
88 add_file_handler (scb->fd, fd_event, scb);
89 next_state = FD_SCHEDULED;
90 }
91 else
92 {
93 next_state = create_timer (0, push_event, scb);
94 }
95 break;
96 default: /* TIMER SCHEDULED */
97 if (scb->bufcnt == 0)
98 {
99 delete_timer (scb->async_state);
100 add_file_handler (scb->fd, fd_event, scb);
101 next_state = FD_SCHEDULED;
102 }
103 else
104 next_state = scb->async_state;
105 break;
106 }
107 if (serial_debug_p (scb))
108 {
109 switch (next_state)
110 {
111 case FD_SCHEDULED:
112 if (scb->async_state != FD_SCHEDULED)
113 fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
114 scb->fd);
115 break;
116 default: /* TIMER SCHEDULED */
117 if (scb->async_state == FD_SCHEDULED)
118 fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
119 scb->fd);
120 break;
121 }
122 }
123 scb->async_state = next_state;
124 }
125}
126
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,
c378eb4e 131 push_event() is used to nag the client until it is. */
3eb25fda
MM
132
133static void
134fd_event (int error, void *context)
135{
136 struct serial *scb = context;
137 if (error != 0)
138 {
139 scb->bufcnt = SERIAL_ERROR;
140 }
141 else if (scb->bufcnt == 0)
142 {
143 /* Prime the input FIFO. The readchar() function is used to
144 pull characters out of the buffer. See also
c378eb4e 145 generic_readchar(). */
3eb25fda 146 int nr;
b4505029 147 nr = scb->ops->read_prim (scb, BUFSIZ);
3eb25fda
MM
148 if (nr == 0)
149 {
150 scb->bufcnt = SERIAL_EOF;
151 }
152 else if (nr > 0)
153 {
154 scb->bufcnt = nr;
155 scb->bufp = scb->buf;
156 }
157 else
158 {
159 scb->bufcnt = SERIAL_ERROR;
160 }
161 }
162 scb->async_handler (scb, scb->async_context);
163 reschedule (scb);
164}
165
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
c378eb4e 169 device before naging stops. */
3eb25fda
MM
170
171static void
172push_event (void *context)
173{
174 struct serial *scb = context;
433759f7 175
3eb25fda
MM
176 scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
177 scb->async_handler (scb, scb->async_context);
178 /* re-schedule */
179 reschedule (scb);
180}
181
b4505029 182/* Wait for input on scb, with timeout seconds. Returns 0 on success,
c378eb4e 183 otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
b4505029
MM
184
185static int
186ser_base_wait_for (struct serial *scb, int timeout)
187{
188 while (1)
189 {
190 int numfds;
191 struct timeval tv;
192 fd_set readfds, exceptfds;
193
194 /* NOTE: Some OS's can scramble the READFDS when the select()
195 call fails (ex the kernel with Red Hat 5.2). Initialize all
c378eb4e 196 arguments before each call. */
b4505029
MM
197
198 tv.tv_sec = timeout;
199 tv.tv_usec = 0;
200
201 FD_ZERO (&readfds);
202 FD_ZERO (&exceptfds);
203 FD_SET (scb->fd, &readfds);
204 FD_SET (scb->fd, &exceptfds);
205
206 if (timeout >= 0)
0ea3f30e 207 numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
b4505029 208 else
0ea3f30e 209 numfds = gdb_select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
b4505029
MM
210
211 if (numfds <= 0)
212 {
213 if (numfds == 0)
214 return SERIAL_TIMEOUT;
215 else if (errno == EINTR)
216 continue;
217 else
c378eb4e
MS
218 return SERIAL_ERROR; /* Got an error from select or
219 poll. */
b4505029
MM
220 }
221
222 return 0;
223 }
224}
225
226/* Read a character with user-specified timeout. TIMEOUT is number of seconds
227 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
228 char if successful. Returns -2 if timeout expired, EOF if line dropped
c378eb4e 229 dead, or -3 for any other error (see errno in that case). */
b4505029
MM
230
231static int
232do_ser_base_readchar (struct serial *scb, int timeout)
233{
234 int status;
235 int delta;
236
237 /* We have to be able to keep the GUI alive here, so we break the
238 original timeout into steps of 1 second, running the "keep the
239 GUI alive" hook each time through the loop.
240
241 Also, timeout = 0 means to poll, so we just set the delta to 0,
242 so we will only go through the loop once. */
243
244 delta = (timeout == 0 ? 0 : 1);
245 while (1)
246 {
247 /* N.B. The UI may destroy our world (for instance by calling
248 remote_stop,) in which case we want to get out of here as
249 quickly as possible. It is not safe to touch scb, since
250 someone else might have freed it. The
251 deprecated_ui_loop_hook signals that we should exit by
252 returning 1. */
253
254 if (deprecated_ui_loop_hook)
255 {
256 if (deprecated_ui_loop_hook (0))
257 return SERIAL_TIMEOUT;
258 }
259
260 status = ser_base_wait_for (scb, delta);
261 if (timeout > 0)
262 timeout -= delta;
263
264 /* If we got a character or an error back from wait_for, then we can
c378eb4e 265 break from the loop before the timeout is completed. */
b4505029
MM
266 if (status != SERIAL_TIMEOUT)
267 break;
268
269 /* If we have exhausted the original timeout, then generate
c378eb4e 270 a SERIAL_TIMEOUT, and pass it out of the loop. */
b4505029
MM
271 else if (timeout == 0)
272 {
273 status = SERIAL_TIMEOUT;
274 break;
275 }
276 }
277
278 if (status < 0)
279 return status;
280
281 status = scb->ops->read_prim (scb, BUFSIZ);
282
283 if (status <= 0)
284 {
285 if (status == 0)
d41ebd5d 286 return SERIAL_EOF;
b4505029
MM
287 else
288 /* Got an error from read. */
289 return SERIAL_ERROR;
290 }
291
292 scb->bufcnt = status;
293 scb->bufcnt--;
294 scb->bufp = scb->buf;
295 return *scb->bufp++;
296}
297
c378eb4e 298/* Perform operations common to both old and new readchar. */
b4505029
MM
299
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
302 characters.
303
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())
c378eb4e 310 will be called. */
b4505029
MM
311
312int
313generic_readchar (struct serial *scb, int timeout,
314 int (do_readchar) (struct serial *scb, int timeout))
315{
316 int ch;
317 if (scb->bufcnt > 0)
318 {
319 ch = *scb->bufp;
320 scb->bufcnt--;
321 scb->bufp++;
322 }
323 else if (scb->bufcnt < 0)
324 {
c378eb4e 325 /* Some errors/eof are are sticky. */
b4505029
MM
326 ch = scb->bufcnt;
327 }
328 else
329 {
330 ch = do_readchar (scb, timeout);
331 if (ch < 0)
332 {
333 switch ((enum serial_rc) ch)
334 {
335 case SERIAL_EOF:
336 case SERIAL_ERROR:
c378eb4e 337 /* Make the error/eof stick. */
b4505029
MM
338 scb->bufcnt = ch;
339 break;
340 case SERIAL_TIMEOUT:
341 scb->bufcnt = 0;
342 break;
343 }
344 }
345 }
65cc4390
VP
346 /* Read any error output we might have. */
347 if (scb->error_fd != -1)
348 {
349 ssize_t s;
350 char buf[81];
351
352 for (;;)
353 {
354 char *current;
355 char *newline;
356 int to_read = 80;
357
358 int num_bytes = -1;
359 if (scb->ops->avail)
360 num_bytes = (scb->ops->avail)(scb, scb->error_fd);
361 if (num_bytes != -1)
362 to_read = (num_bytes < to_read) ? num_bytes : to_read;
363
364 if (to_read == 0)
365 break;
366
367 s = read (scb->error_fd, &buf, to_read);
9f117f47 368 if (s == -1)
65cc4390 369 break;
9f117f47
DE
370 if (s == 0)
371 {
372 /* EOF */
373 close (scb->error_fd);
374 scb->error_fd = -1;
375 break;
376 }
65cc4390
VP
377
378 /* In theory, embedded newlines are not a problem.
379 But for MI, we want each output line to have just
380 one newline for legibility. So output things
381 in newline chunks. */
382 buf[s] = '\0';
383 current = buf;
384 while ((newline = strstr (current, "\n")) != NULL)
385 {
386 *newline = '\0';
387 fputs_unfiltered (current, gdb_stderr);
388 fputs_unfiltered ("\n", gdb_stderr);
389 current = newline + 1;
390 }
391 fputs_unfiltered (current, gdb_stderr);
392 }
393 }
394
b4505029
MM
395 reschedule (scb);
396 return ch;
397}
398
399int
400ser_base_readchar (struct serial *scb, int timeout)
401{
402 return generic_readchar (scb, timeout, do_ser_base_readchar);
403}
404
3eb25fda 405int
dd5da072 406ser_base_write (struct serial *scb, const char *str, int len)
3eb25fda
MM
407{
408 int cc;
409
410 while (len > 0)
411 {
b4505029 412 cc = scb->ops->write_prim (scb, str, len);
3eb25fda
MM
413
414 if (cc < 0)
415 return 1;
416 len -= cc;
417 str += cc;
418 }
419 return 0;
420}
421
422int
dd5da072 423ser_base_flush_output (struct serial *scb)
3eb25fda
MM
424{
425 return 0;
426}
427
428int
dd5da072 429ser_base_flush_input (struct serial *scb)
3eb25fda
MM
430{
431 if (scb->bufcnt >= 0)
432 {
433 scb->bufcnt = 0;
434 scb->bufp = scb->buf;
435 return 0;
436 }
437 else
438 return SERIAL_ERROR;
439}
440
441int
dd5da072 442ser_base_send_break (struct serial *scb)
3eb25fda
MM
443{
444 return 0;
445}
446
447int
dd5da072 448ser_base_drain_output (struct serial *scb)
3eb25fda
MM
449{
450 return 0;
451}
452
453void
dd5da072 454ser_base_raw (struct serial *scb)
3eb25fda 455{
c378eb4e 456 return; /* Always in raw mode. */
3eb25fda
MM
457}
458
459serial_ttystate
dd5da072 460ser_base_get_tty_state (struct serial *scb)
3eb25fda 461{
c378eb4e 462 /* Allocate a dummy. */
3eb25fda
MM
463 return (serial_ttystate) XMALLOC (int);
464}
465
466int
dd5da072 467ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
3eb25fda
MM
468{
469 return 0;
470}
471
472int
dd5da072
MM
473ser_base_noflush_set_tty_state (struct serial *scb,
474 serial_ttystate new_ttystate,
475 serial_ttystate old_ttystate)
3eb25fda
MM
476{
477 return 0;
478}
479
480void
dd5da072
MM
481ser_base_print_tty_state (struct serial *scb,
482 serial_ttystate ttystate,
483 struct ui_file *stream)
3eb25fda
MM
484{
485 /* Nothing to print. */
486 return;
487}
488
489int
dd5da072 490ser_base_setbaudrate (struct serial *scb, int rate)
3eb25fda 491{
c378eb4e 492 return 0; /* Never fails! */
3eb25fda
MM
493}
494
495int
dd5da072 496ser_base_setstopbits (struct serial *scb, int num)
3eb25fda 497{
c378eb4e 498 return 0; /* Never fails! */
3eb25fda
MM
499}
500
501/* Put the SERIAL device into/out-of ASYNC mode. */
502
503void
dd5da072 504ser_base_async (struct serial *scb,
3eb25fda
MM
505 int async_p)
506{
507 if (async_p)
508 {
c378eb4e 509 /* Force a re-schedule. */
3eb25fda
MM
510 scb->async_state = NOTHING_SCHEDULED;
511 if (serial_debug_p (scb))
512 fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
513 scb->fd);
514 reschedule (scb);
515 }
516 else
517 {
518 if (serial_debug_p (scb))
519 fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
520 scb->fd);
c378eb4e 521 /* De-schedule whatever tasks are currently scheduled. */
3eb25fda
MM
522 switch (scb->async_state)
523 {
524 case FD_SCHEDULED:
525 delete_file_handler (scb->fd);
526 break;
527 case NOTHING_SCHEDULED:
528 break;
529 default: /* TIMER SCHEDULED */
530 delete_timer (scb->async_state);
531 break;
532 }
533 }
534}
This page took 0.527924 seconds and 4 git commands to generate.