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