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