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