1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992-2016 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>
29 #include "gdb_sys_time.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 int hardwire_setparity (struct serial *scb, int parity);
87 static void hardwire_close (struct serial *scb);
88 static int get_tty_state (struct serial *scb,
89 struct hardwire_ttystate * state);
90 static int set_tty_state (struct serial *scb,
91 struct hardwire_ttystate * state);
92 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
93 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
94 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
96 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
98 static int hardwire_drain_output (struct serial *);
99 static int hardwire_flush_output (struct serial *);
100 static int hardwire_flush_input (struct serial *);
101 static int hardwire_send_break (struct serial *);
102 static int hardwire_setstopbits (struct serial *, int);
104 void _initialize_ser_hardwire (void);
106 /* Open up a real live device for serial I/O. */
109 hardwire_open (struct serial *scb, const char *name)
111 scb->fd = gdb_open_cloexec (name, O_RDWR, 0);
119 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
122 if (tcgetattr (scb->fd, &state->termios) < 0)
129 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
135 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
137 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
139 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
141 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
149 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
152 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
159 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
165 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
167 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
169 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
171 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
178 static serial_ttystate
179 hardwire_get_tty_state (struct serial *scb)
181 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
183 if (get_tty_state (scb, state))
189 return (serial_ttystate) state;
192 static serial_ttystate
193 hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
195 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
197 *state = *(struct hardwire_ttystate *) ttystate;
199 return (serial_ttystate) state;
203 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
205 struct hardwire_ttystate *state;
207 state = (struct hardwire_ttystate *) ttystate;
209 return set_tty_state (scb, state);
213 hardwire_noflush_set_tty_state (struct serial *scb,
214 serial_ttystate new_ttystate,
215 serial_ttystate old_ttystate)
217 struct hardwire_ttystate new_state;
219 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
222 new_state = *(struct hardwire_ttystate *) new_ttystate;
224 /* Don't change in or out of raw mode; we don't want to flush input.
225 termio and termios have no such restriction; for them flushing input
226 is separate from setting the attributes. */
229 if (state->sgttyb.sg_flags & RAW)
230 new_state.sgttyb.sg_flags |= RAW;
232 new_state.sgttyb.sg_flags &= ~RAW;
234 /* I'm not sure whether this is necessary; the manpage just mentions
236 if (state->sgttyb.sg_flags & CBREAK)
237 new_state.sgttyb.sg_flags |= CBREAK;
239 new_state.sgttyb.sg_flags &= ~CBREAK;
242 return set_tty_state (scb, &new_state);
246 hardwire_print_tty_state (struct serial *scb,
247 serial_ttystate ttystate,
248 struct ui_file *stream)
250 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
254 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
255 (int) state->termios.c_iflag,
256 (int) state->termios.c_oflag);
257 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
258 (int) state->termios.c_cflag,
259 (int) state->termios.c_lflag);
261 /* This not in POSIX, and is not really documented by those systems
262 which have it (at least not Sun). */
263 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
265 fprintf_filtered (stream, "c_cc: ");
266 for (i = 0; i < NCCS; i += 1)
267 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
268 fprintf_filtered (stream, "\n");
272 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
273 state->termio.c_iflag, state->termio.c_oflag);
274 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
275 state->termio.c_cflag, state->termio.c_lflag,
276 state->termio.c_line);
277 fprintf_filtered (stream, "c_cc: ");
278 for (i = 0; i < NCC; i += 1)
279 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
280 fprintf_filtered (stream, "\n");
284 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
285 state->sgttyb.sg_flags);
287 fprintf_filtered (stream, "tchars: ");
288 for (i = 0; i < (int) sizeof (struct tchars); i++)
289 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
290 fprintf_filtered (stream, "\n");
292 fprintf_filtered (stream, "ltchars: ");
293 for (i = 0; i < (int) sizeof (struct ltchars); i++)
294 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
295 fprintf_filtered (stream, "\n");
297 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
301 /* Wait for the output to drain away, as opposed to flushing
305 hardwire_drain_output (struct serial *scb)
308 return tcdrain (scb->fd);
312 return ioctl (scb->fd, TCSBRK, 1);
316 /* Get the current state and then restore it using TIOCSETP,
317 which should cause the output to drain and pending input
320 struct hardwire_ttystate state;
322 if (get_tty_state (scb, &state))
328 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
335 hardwire_flush_output (struct serial *scb)
338 return tcflush (scb->fd, TCOFLUSH);
342 return ioctl (scb->fd, TCFLSH, 1);
346 /* This flushes both input and output, but we can't do better. */
347 return ioctl (scb->fd, TIOCFLUSH, 0);
352 hardwire_flush_input (struct serial *scb)
354 ser_base_flush_input (scb);
357 return tcflush (scb->fd, TCIFLUSH);
361 return ioctl (scb->fd, TCFLSH, 0);
365 /* This flushes both input and output, but we can't do better. */
366 return ioctl (scb->fd, TIOCFLUSH, 0);
371 hardwire_send_break (struct serial *scb)
374 return tcsendbreak (scb->fd, 0);
378 return ioctl (scb->fd, TCSBRK, 0);
385 status = ioctl (scb->fd, TIOCSBRK, 0);
387 /* Can't use usleep; it doesn't exist in BSD 4.2. */
388 /* Note that if this gdb_select() is interrupted by a signal it will not
389 wait the full length of time. I think that is OK. */
391 status = ioctl (scb->fd, TIOCCBRK, 0);
398 hardwire_raw (struct serial *scb)
400 struct hardwire_ttystate state;
402 if (get_tty_state (scb, &state))
403 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
404 safe_strerror (errno));
407 state.termios.c_iflag = 0;
408 state.termios.c_oflag = 0;
409 state.termios.c_lflag = 0;
410 state.termios.c_cflag &= ~CSIZE;
411 state.termios.c_cflag |= CLOCAL | CS8;
413 /* h/w flow control. */
415 state.termios.c_cflag |= CRTSCTS;
417 state.termios.c_cflag &= ~CRTSCTS;
420 state.termios.c_cflag |= CRTS_IFLOW;
422 state.termios.c_cflag &= ~CRTS_IFLOW;
425 state.termios.c_cc[VMIN] = 0;
426 state.termios.c_cc[VTIME] = 0;
430 state.termio.c_iflag = 0;
431 state.termio.c_oflag = 0;
432 state.termio.c_lflag = 0;
433 state.termio.c_cflag &= ~CSIZE;
434 state.termio.c_cflag |= CLOCAL | CS8;
435 state.termio.c_cc[VMIN] = 0;
436 state.termio.c_cc[VTIME] = 0;
440 state.sgttyb.sg_flags |= RAW | ANYP;
441 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
444 scb->current_timeout = 0;
446 if (set_tty_state (scb, &state))
447 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
448 safe_strerror (errno));
451 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
452 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
454 For termio{s}, we actually just setup VTIME if necessary, and let the
455 timeout occur in the read() in hardwire_read(). */
457 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
458 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
461 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
462 possible values of the TIMEOUT parameter are ONE and ZERO.
463 Consequently all the code that tries to handle the possability of
464 an overflowed timer is unnecessary. */
467 wait_for (struct serial *scb, int timeout)
476 /* NOTE: Some OS's can scramble the READFDS when the select()
477 call fails (ex the kernel with Red Hat 5.2). Initialize all
478 arguments before each call. */
484 FD_SET (scb->fd, &readfds);
487 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv);
489 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0);
493 return SERIAL_TIMEOUT;
494 else if (errno == EINTR)
497 return SERIAL_ERROR; /* Got an error from select or poll. */
501 #endif /* HAVE_SGTTY */
503 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
504 if (timeout == scb->current_timeout)
507 scb->current_timeout = timeout;
510 struct hardwire_ttystate state;
512 if (get_tty_state (scb, &state))
513 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
514 safe_strerror (errno));
520 state.termios.c_cc[VTIME] = 0;
521 state.termios.c_cc[VMIN] = 1;
525 state.termios.c_cc[VMIN] = 0;
526 state.termios.c_cc[VTIME] = timeout * 10;
527 if (state.termios.c_cc[VTIME] != timeout * 10)
530 /* If c_cc is an 8-bit signed character, we can't go
531 bigger than this. If it is always unsigned, we could use
534 scb->current_timeout = 12;
535 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
536 scb->timeout_remaining = timeout - scb->current_timeout;
545 state.termio.c_cc[VTIME] = 0;
546 state.termio.c_cc[VMIN] = 1;
550 state.termio.c_cc[VMIN] = 0;
551 state.termio.c_cc[VTIME] = timeout * 10;
552 if (state.termio.c_cc[VTIME] != timeout * 10)
554 /* If c_cc is an 8-bit signed character, we can't go
555 bigger than this. If it is always unsigned, we could use
558 scb->current_timeout = 12;
559 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
560 scb->timeout_remaining = timeout - scb->current_timeout;
565 if (set_tty_state (scb, &state))
566 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
567 safe_strerror (errno));
571 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
574 /* Read a character with user-specified timeout. TIMEOUT is number of
575 seconds to wait, or -1 to wait forever. Use timeout of 0 to effect
576 a poll. Returns char if successful. Returns SERIAL_TIMEOUT if
577 timeout expired, EOF if line dropped dead, or SERIAL_ERROR for any
578 other error (see errno in that case). */
580 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
581 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
584 /* NOTE: cagney/1999-09-16: This function is not identical to
585 ser_base_readchar() as part of replacing it with ser_base*()
586 merging will be required - this code handles the case where read()
587 times out due to no data while ser_base_readchar() doesn't expect
591 do_hardwire_readchar (struct serial *scb, int timeout)
599 /* We have to be able to keep the GUI alive here, so we break the
600 original timeout into steps of 1 second, running the "keep the
601 GUI alive" hook each time through the loop.
603 Also, timeout = 0 means to poll, so we just set the delta to 0,
604 so we will only go through the loop once. */
606 delta = (timeout == 0 ? 0 : 1);
610 /* N.B. The UI may destroy our world (for instance by calling
611 remote_stop,) in which case we want to get out of here as
612 quickly as possible. It is not safe to touch scb, since
613 someone else might have freed it. The
614 deprecated_ui_loop_hook signals that we should exit by
617 if (deprecated_ui_loop_hook)
618 detach = deprecated_ui_loop_hook (0);
621 return SERIAL_TIMEOUT;
623 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
624 status = wait_for (scb, delta);
629 status = read (scb->fd, scb->buf, BUFSIZ);
635 /* Zero characters means timeout (it could also be EOF, but
636 we don't (yet at least) distinguish). */
637 if (scb->timeout_remaining > 0)
639 timeout = scb->timeout_remaining;
642 else if (scb->timeout_remaining < 0)
645 return SERIAL_TIMEOUT;
647 else if (errno == EINTR)
650 return SERIAL_ERROR; /* Got an error from read. */
653 scb->bufcnt = status;
655 scb->bufp = scb->buf;
661 hardwire_readchar (struct serial *scb, int timeout)
663 return generic_readchar (scb, timeout, do_hardwire_readchar);
675 /* Translate baud rates from integers to damn B_codes. Unix should
676 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
776 rate_to_code (int rate)
780 for (i = 0; baudtab[i].rate != -1; i++)
782 /* test for perfect macth. */
783 if (rate == baudtab[i].rate)
784 return baudtab[i].code;
787 /* check if it is in between valid values. */
788 if (rate < baudtab[i].rate)
792 warning (_("Invalid baud rate %d. "
793 "Closest values are %d and %d."),
794 rate, baudtab[i - 1].rate, baudtab[i].rate);
798 warning (_("Invalid baud rate %d. Minimum value is %d."),
799 rate, baudtab[0].rate);
806 /* The requested speed was too large. */
807 warning (_("Invalid baud rate %d. Maximum value is %d."),
808 rate, baudtab[i - 1].rate);
813 hardwire_setbaudrate (struct serial *scb, int rate)
815 struct hardwire_ttystate state;
816 int baud_code = rate_to_code (rate);
820 /* The baud rate was not valid.
821 A warning has already been issued. */
826 if (get_tty_state (scb, &state))
830 cfsetospeed (&state.termios, baud_code);
831 cfsetispeed (&state.termios, baud_code);
839 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
840 state.termio.c_cflag |= baud_code;
844 state.sgttyb.sg_ispeed = baud_code;
845 state.sgttyb.sg_ospeed = baud_code;
848 return set_tty_state (scb, &state);
852 hardwire_setstopbits (struct serial *scb, int num)
854 struct hardwire_ttystate state;
857 if (get_tty_state (scb, &state))
862 case SERIAL_1_STOPBITS:
865 case SERIAL_1_AND_A_HALF_STOPBITS:
866 case SERIAL_2_STOPBITS:
875 state.termios.c_cflag &= ~CSTOPB;
877 state.termios.c_cflag |= CSTOPB; /* two bits */
882 state.termio.c_cflag &= ~CSTOPB;
884 state.termio.c_cflag |= CSTOPB; /* two bits */
888 return 0; /* sgtty doesn't support this */
891 return set_tty_state (scb, &state);
894 /* Implement the "setparity" serial_ops callback. */
897 hardwire_setparity (struct serial *scb, int parity)
899 struct hardwire_ttystate state;
902 if (get_tty_state (scb, &state))
911 newparity = PARENB | PARODD;
917 internal_warning (__FILE__, __LINE__,
918 "Incorrect parity value: %d", parity);
923 state.termios.c_cflag &= ~(PARENB | PARODD);
924 state.termios.c_cflag |= newparity;
928 state.termio.c_cflag &= ~(PARENB | PARODD);
929 state.termio.c_cflag |= newparity;
933 return 0; /* sgtty doesn't support this */
935 return set_tty_state (scb, &state);
940 hardwire_close (struct serial *scb)
951 /* The hardwire ops. */
953 static const struct serial_ops hardwire_ops =
959 /* FIXME: Don't replace this with the equivalent ser_base*() until
960 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
964 hardwire_flush_output,
965 hardwire_flush_input,
968 hardwire_get_tty_state,
969 hardwire_copy_tty_state,
970 hardwire_set_tty_state,
971 hardwire_print_tty_state,
972 hardwire_noflush_set_tty_state,
973 hardwire_setbaudrate,
974 hardwire_setstopbits,
976 hardwire_drain_output,
983 _initialize_ser_hardwire (void)
985 serial_add_interface (&hardwire_ops);
989 add_setshow_boolean_cmd ("remoteflow", no_class,
991 Set use of hardware flow control for remote serial I/O."), _("\
992 Show use of hardware flow control for remote serial I/O."), _("\
993 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
994 when debugging using remote targets."),
997 &setlist, &showlist);
1003 ser_unix_read_prim (struct serial *scb, size_t count)
1009 status = read (scb->fd, scb->buf, count);
1010 if (status != -1 || errno != EINTR)
1017 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
1019 /* ??? Historically, GDB has not retried calls to "write" that
1021 return write (scb->fd, buf, len);