1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992-2015 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
26 #include <sys/types.h>
28 #include <sys/socket.h>
31 #include "gdb_select.h"
33 #include "filestuff.h"
37 struct hardwire_ttystate
39 struct termios termios;
43 /* Boolean to explicitly enable or disable h/w flow control. */
44 static int serial_hwflow;
46 show_serial_hwflow (struct ui_file *file, int from_tty,
47 struct cmd_list_element *c, const char *value)
49 fprintf_filtered (file, _("Hardware flow control is %s.\n"), value);
57 /* It is believed that all systems which have added job control to SVR3
58 (e.g. sco) have also added termios. Even if not, trying to figure out
59 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
60 bewildering. So we don't attempt it. */
62 struct hardwire_ttystate
69 struct hardwire_ttystate
74 /* Line discipline flags. */
79 static int hardwire_open (struct serial *scb, const char *name);
80 static void hardwire_raw (struct serial *scb);
81 static int wait_for (struct serial *scb, int timeout);
82 static int hardwire_readchar (struct serial *scb, int timeout);
83 static int do_hardwire_readchar (struct serial *scb, int timeout);
84 static int rate_to_code (int rate);
85 static int hardwire_setbaudrate (struct serial *scb, int rate);
86 static void hardwire_close (struct serial *scb);
87 static int get_tty_state (struct serial *scb,
88 struct hardwire_ttystate * state);
89 static int set_tty_state (struct serial *scb,
90 struct hardwire_ttystate * state);
91 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
92 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
93 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
95 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
97 static int hardwire_drain_output (struct serial *);
98 static int hardwire_flush_output (struct serial *);
99 static int hardwire_flush_input (struct serial *);
100 static int hardwire_send_break (struct serial *);
101 static int hardwire_setstopbits (struct serial *, int);
103 void _initialize_ser_hardwire (void);
105 /* Open up a real live device for serial I/O. */
108 hardwire_open (struct serial *scb, const char *name)
110 scb->fd = gdb_open_cloexec (name, O_RDWR, 0);
118 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
121 if (tcgetattr (scb->fd, &state->termios) < 0)
128 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
134 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
136 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
138 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
140 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
148 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
151 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
158 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
164 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
166 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
168 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
170 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
177 static serial_ttystate
178 hardwire_get_tty_state (struct serial *scb)
180 struct hardwire_ttystate *state;
182 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
184 if (get_tty_state (scb, state))
190 return (serial_ttystate) state;
193 static serial_ttystate
194 hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
196 struct hardwire_ttystate *state;
198 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
199 *state = *(struct hardwire_ttystate *) ttystate;
201 return (serial_ttystate) state;
205 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
207 struct hardwire_ttystate *state;
209 state = (struct hardwire_ttystate *) ttystate;
211 return set_tty_state (scb, state);
215 hardwire_noflush_set_tty_state (struct serial *scb,
216 serial_ttystate new_ttystate,
217 serial_ttystate old_ttystate)
219 struct hardwire_ttystate new_state;
221 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
224 new_state = *(struct hardwire_ttystate *) new_ttystate;
226 /* Don't change in or out of raw mode; we don't want to flush input.
227 termio and termios have no such restriction; for them flushing input
228 is separate from setting the attributes. */
231 if (state->sgttyb.sg_flags & RAW)
232 new_state.sgttyb.sg_flags |= RAW;
234 new_state.sgttyb.sg_flags &= ~RAW;
236 /* I'm not sure whether this is necessary; the manpage just mentions
238 if (state->sgttyb.sg_flags & CBREAK)
239 new_state.sgttyb.sg_flags |= CBREAK;
241 new_state.sgttyb.sg_flags &= ~CBREAK;
244 return set_tty_state (scb, &new_state);
248 hardwire_print_tty_state (struct serial *scb,
249 serial_ttystate ttystate,
250 struct ui_file *stream)
252 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
256 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
257 (int) state->termios.c_iflag,
258 (int) state->termios.c_oflag);
259 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
260 (int) state->termios.c_cflag,
261 (int) state->termios.c_lflag);
263 /* This not in POSIX, and is not really documented by those systems
264 which have it (at least not Sun). */
265 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
267 fprintf_filtered (stream, "c_cc: ");
268 for (i = 0; i < NCCS; i += 1)
269 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
270 fprintf_filtered (stream, "\n");
274 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
275 state->termio.c_iflag, state->termio.c_oflag);
276 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
277 state->termio.c_cflag, state->termio.c_lflag,
278 state->termio.c_line);
279 fprintf_filtered (stream, "c_cc: ");
280 for (i = 0; i < NCC; i += 1)
281 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
282 fprintf_filtered (stream, "\n");
286 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
287 state->sgttyb.sg_flags);
289 fprintf_filtered (stream, "tchars: ");
290 for (i = 0; i < (int) sizeof (struct tchars); i++)
291 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
292 fprintf_filtered (stream, "\n");
294 fprintf_filtered (stream, "ltchars: ");
295 for (i = 0; i < (int) sizeof (struct ltchars); i++)
296 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
297 fprintf_filtered (stream, "\n");
299 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
303 /* Wait for the output to drain away, as opposed to flushing
307 hardwire_drain_output (struct serial *scb)
310 return tcdrain (scb->fd);
314 return ioctl (scb->fd, TCSBRK, 1);
318 /* Get the current state and then restore it using TIOCSETP,
319 which should cause the output to drain and pending input
322 struct hardwire_ttystate state;
324 if (get_tty_state (scb, &state))
330 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
337 hardwire_flush_output (struct serial *scb)
340 return tcflush (scb->fd, TCOFLUSH);
344 return ioctl (scb->fd, TCFLSH, 1);
348 /* This flushes both input and output, but we can't do better. */
349 return ioctl (scb->fd, TIOCFLUSH, 0);
354 hardwire_flush_input (struct serial *scb)
356 ser_base_flush_input (scb);
359 return tcflush (scb->fd, TCIFLUSH);
363 return ioctl (scb->fd, TCFLSH, 0);
367 /* This flushes both input and output, but we can't do better. */
368 return ioctl (scb->fd, TIOCFLUSH, 0);
373 hardwire_send_break (struct serial *scb)
376 return tcsendbreak (scb->fd, 0);
380 return ioctl (scb->fd, TCSBRK, 0);
387 status = ioctl (scb->fd, TIOCSBRK, 0);
389 /* Can't use usleep; it doesn't exist in BSD 4.2. */
390 /* Note that if this gdb_select() is interrupted by a signal it will not
391 wait the full length of time. I think that is OK. */
393 status = ioctl (scb->fd, TIOCCBRK, 0);
400 hardwire_raw (struct serial *scb)
402 struct hardwire_ttystate state;
404 if (get_tty_state (scb, &state))
405 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
406 safe_strerror (errno));
409 state.termios.c_iflag = 0;
410 state.termios.c_oflag = 0;
411 state.termios.c_lflag = 0;
412 state.termios.c_cflag &= ~(CSIZE | PARENB);
413 state.termios.c_cflag |= CLOCAL | CS8;
415 /* h/w flow control. */
417 state.termios.c_cflag |= CRTSCTS;
419 state.termios.c_cflag &= ~CRTSCTS;
422 state.termios.c_cflag |= CRTS_IFLOW;
424 state.termios.c_cflag &= ~CRTS_IFLOW;
427 state.termios.c_cc[VMIN] = 0;
428 state.termios.c_cc[VTIME] = 0;
432 state.termio.c_iflag = 0;
433 state.termio.c_oflag = 0;
434 state.termio.c_lflag = 0;
435 state.termio.c_cflag &= ~(CSIZE | PARENB);
436 state.termio.c_cflag |= CLOCAL | CS8;
437 state.termio.c_cc[VMIN] = 0;
438 state.termio.c_cc[VTIME] = 0;
442 state.sgttyb.sg_flags |= RAW | ANYP;
443 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
446 scb->current_timeout = 0;
448 if (set_tty_state (scb, &state))
449 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
450 safe_strerror (errno));
453 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
454 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
456 For termio{s}, we actually just setup VTIME if necessary, and let the
457 timeout occur in the read() in hardwire_read(). */
459 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
460 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
463 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
464 possible values of the TIMEOUT parameter are ONE and ZERO.
465 Consequently all the code that tries to handle the possability of
466 an overflowed timer is unnecessary. */
469 wait_for (struct serial *scb, int timeout)
478 /* NOTE: Some OS's can scramble the READFDS when the select()
479 call fails (ex the kernel with Red Hat 5.2). Initialize all
480 arguments before each call. */
486 FD_SET (scb->fd, &readfds);
489 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv);
491 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0);
495 return SERIAL_TIMEOUT;
496 else if (errno == EINTR)
499 return SERIAL_ERROR; /* Got an error from select or poll. */
503 #endif /* HAVE_SGTTY */
505 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
506 if (timeout == scb->current_timeout)
509 scb->current_timeout = timeout;
512 struct hardwire_ttystate state;
514 if (get_tty_state (scb, &state))
515 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
516 safe_strerror (errno));
522 state.termios.c_cc[VTIME] = 0;
523 state.termios.c_cc[VMIN] = 1;
527 state.termios.c_cc[VMIN] = 0;
528 state.termios.c_cc[VTIME] = timeout * 10;
529 if (state.termios.c_cc[VTIME] != timeout * 10)
532 /* If c_cc is an 8-bit signed character, we can't go
533 bigger than this. If it is always unsigned, we could use
536 scb->current_timeout = 12;
537 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
538 scb->timeout_remaining = timeout - scb->current_timeout;
547 state.termio.c_cc[VTIME] = 0;
548 state.termio.c_cc[VMIN] = 1;
552 state.termio.c_cc[VMIN] = 0;
553 state.termio.c_cc[VTIME] = timeout * 10;
554 if (state.termio.c_cc[VTIME] != timeout * 10)
556 /* If c_cc is an 8-bit signed character, we can't go
557 bigger than this. If it is always unsigned, we could use
560 scb->current_timeout = 12;
561 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
562 scb->timeout_remaining = timeout - scb->current_timeout;
567 if (set_tty_state (scb, &state))
568 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
569 safe_strerror (errno));
573 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
576 /* Read a character with user-specified timeout. TIMEOUT is number of
577 seconds to wait, or -1 to wait forever. Use timeout of 0 to effect
578 a poll. Returns char if successful. Returns SERIAL_TIMEOUT if
579 timeout expired, EOF if line dropped dead, or SERIAL_ERROR for any
580 other error (see errno in that case). */
582 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
583 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
586 /* NOTE: cagney/1999-09-16: This function is not identical to
587 ser_base_readchar() as part of replacing it with ser_base*()
588 merging will be required - this code handles the case where read()
589 times out due to no data while ser_base_readchar() doesn't expect
593 do_hardwire_readchar (struct serial *scb, int timeout)
601 /* We have to be able to keep the GUI alive here, so we break the
602 original timeout into steps of 1 second, running the "keep the
603 GUI alive" hook each time through the loop.
605 Also, timeout = 0 means to poll, so we just set the delta to 0,
606 so we will only go through the loop once. */
608 delta = (timeout == 0 ? 0 : 1);
612 /* N.B. The UI may destroy our world (for instance by calling
613 remote_stop,) in which case we want to get out of here as
614 quickly as possible. It is not safe to touch scb, since
615 someone else might have freed it. The
616 deprecated_ui_loop_hook signals that we should exit by
619 if (deprecated_ui_loop_hook)
620 detach = deprecated_ui_loop_hook (0);
623 return SERIAL_TIMEOUT;
625 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
626 status = wait_for (scb, delta);
631 status = read (scb->fd, scb->buf, BUFSIZ);
637 /* Zero characters means timeout (it could also be EOF, but
638 we don't (yet at least) distinguish). */
639 if (scb->timeout_remaining > 0)
641 timeout = scb->timeout_remaining;
644 else if (scb->timeout_remaining < 0)
647 return SERIAL_TIMEOUT;
649 else if (errno == EINTR)
652 return SERIAL_ERROR; /* Got an error from read. */
655 scb->bufcnt = status;
657 scb->bufp = scb->buf;
663 hardwire_readchar (struct serial *scb, int timeout)
665 return generic_readchar (scb, timeout, do_hardwire_readchar);
677 /* Translate baud rates from integers to damn B_codes. Unix should
678 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
778 rate_to_code (int rate)
782 for (i = 0; baudtab[i].rate != -1; i++)
784 /* test for perfect macth. */
785 if (rate == baudtab[i].rate)
786 return baudtab[i].code;
789 /* check if it is in between valid values. */
790 if (rate < baudtab[i].rate)
794 warning (_("Invalid baud rate %d. "
795 "Closest values are %d and %d."),
796 rate, baudtab[i - 1].rate, baudtab[i].rate);
800 warning (_("Invalid baud rate %d. Minimum value is %d."),
801 rate, baudtab[0].rate);
808 /* The requested speed was too large. */
809 warning (_("Invalid baud rate %d. Maximum value is %d."),
810 rate, baudtab[i - 1].rate);
815 hardwire_setbaudrate (struct serial *scb, int rate)
817 struct hardwire_ttystate state;
818 int baud_code = rate_to_code (rate);
822 /* The baud rate was not valid.
823 A warning has already been issued. */
828 if (get_tty_state (scb, &state))
832 cfsetospeed (&state.termios, baud_code);
833 cfsetispeed (&state.termios, baud_code);
841 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
842 state.termio.c_cflag |= baud_code;
846 state.sgttyb.sg_ispeed = baud_code;
847 state.sgttyb.sg_ospeed = baud_code;
850 return set_tty_state (scb, &state);
854 hardwire_setstopbits (struct serial *scb, int num)
856 struct hardwire_ttystate state;
859 if (get_tty_state (scb, &state))
864 case SERIAL_1_STOPBITS:
867 case SERIAL_1_AND_A_HALF_STOPBITS:
868 case SERIAL_2_STOPBITS:
877 state.termios.c_cflag &= ~CSTOPB;
879 state.termios.c_cflag |= CSTOPB; /* two bits */
884 state.termio.c_cflag &= ~CSTOPB;
886 state.termio.c_cflag |= CSTOPB; /* two bits */
890 return 0; /* sgtty doesn't support this */
893 return set_tty_state (scb, &state);
897 hardwire_close (struct serial *scb)
908 /* The hardwire ops. */
910 static const struct serial_ops hardwire_ops =
916 /* FIXME: Don't replace this with the equivalent ser_base*() until
917 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
921 hardwire_flush_output,
922 hardwire_flush_input,
925 hardwire_get_tty_state,
926 hardwire_copy_tty_state,
927 hardwire_set_tty_state,
928 hardwire_print_tty_state,
929 hardwire_noflush_set_tty_state,
930 hardwire_setbaudrate,
931 hardwire_setstopbits,
932 hardwire_drain_output,
939 _initialize_ser_hardwire (void)
941 serial_add_interface (&hardwire_ops);
945 add_setshow_boolean_cmd ("remoteflow", no_class,
947 Set use of hardware flow control for remote serial I/O."), _("\
948 Show use of hardware flow control for remote serial I/O."), _("\
949 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
950 when debugging using remote targets."),
953 &setlist, &showlist);
959 ser_unix_read_prim (struct serial *scb, size_t count)
965 status = read (scb->fd, scb->buf, count);
966 if (status != -1 || errno != EINTR)
973 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
975 /* ??? Historically, GDB has not retried calls to "write" that
977 return write (scb->fd, buf, len);