1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992-2013 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"
32 #include "gdb_string.h"
34 #include "filestuff.h"
38 struct hardwire_ttystate
40 struct termios termios;
44 /* Boolean to explicitly enable or disable h/w flow control. */
45 static int serial_hwflow;
47 show_serial_hwflow (struct ui_file *file, int from_tty,
48 struct cmd_list_element *c, const char *value)
50 fprintf_filtered (file, _("Hardware flow control is %s.\n"), value);
58 /* It is believed that all systems which have added job control to SVR3
59 (e.g. sco) have also added termios. Even if not, trying to figure out
60 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
61 bewildering. So we don't attempt it. */
63 struct hardwire_ttystate
70 struct hardwire_ttystate
75 /* Line discipline flags. */
80 static int hardwire_open (struct serial *scb, const char *name);
81 static void hardwire_raw (struct serial *scb);
82 static int wait_for (struct serial *scb, int timeout);
83 static int hardwire_readchar (struct serial *scb, int timeout);
84 static int do_hardwire_readchar (struct serial *scb, int timeout);
85 static int rate_to_code (int rate);
86 static int hardwire_setbaudrate (struct serial *scb, int rate);
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;
183 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
185 if (get_tty_state (scb, state))
191 return (serial_ttystate) state;
194 static serial_ttystate
195 hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
197 struct hardwire_ttystate *state;
199 state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
200 *state = *(struct hardwire_ttystate *) ttystate;
202 return (serial_ttystate) state;
206 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
208 struct hardwire_ttystate *state;
210 state = (struct hardwire_ttystate *) ttystate;
212 return set_tty_state (scb, state);
216 hardwire_noflush_set_tty_state (struct serial *scb,
217 serial_ttystate new_ttystate,
218 serial_ttystate old_ttystate)
220 struct hardwire_ttystate new_state;
222 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
225 new_state = *(struct hardwire_ttystate *) new_ttystate;
227 /* Don't change in or out of raw mode; we don't want to flush input.
228 termio and termios have no such restriction; for them flushing input
229 is separate from setting the attributes. */
232 if (state->sgttyb.sg_flags & RAW)
233 new_state.sgttyb.sg_flags |= RAW;
235 new_state.sgttyb.sg_flags &= ~RAW;
237 /* I'm not sure whether this is necessary; the manpage just mentions
239 if (state->sgttyb.sg_flags & CBREAK)
240 new_state.sgttyb.sg_flags |= CBREAK;
242 new_state.sgttyb.sg_flags &= ~CBREAK;
245 return set_tty_state (scb, &new_state);
249 hardwire_print_tty_state (struct serial *scb,
250 serial_ttystate ttystate,
251 struct ui_file *stream)
253 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
257 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
258 (int) state->termios.c_iflag,
259 (int) state->termios.c_oflag);
260 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
261 (int) state->termios.c_cflag,
262 (int) state->termios.c_lflag);
264 /* This not in POSIX, and is not really documented by those systems
265 which have it (at least not Sun). */
266 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
268 fprintf_filtered (stream, "c_cc: ");
269 for (i = 0; i < NCCS; i += 1)
270 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
271 fprintf_filtered (stream, "\n");
275 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
276 state->termio.c_iflag, state->termio.c_oflag);
277 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
278 state->termio.c_cflag, state->termio.c_lflag,
279 state->termio.c_line);
280 fprintf_filtered (stream, "c_cc: ");
281 for (i = 0; i < NCC; i += 1)
282 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
283 fprintf_filtered (stream, "\n");
287 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
288 state->sgttyb.sg_flags);
290 fprintf_filtered (stream, "tchars: ");
291 for (i = 0; i < (int) sizeof (struct tchars); i++)
292 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
293 fprintf_filtered (stream, "\n");
295 fprintf_filtered (stream, "ltchars: ");
296 for (i = 0; i < (int) sizeof (struct ltchars); i++)
297 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
298 fprintf_filtered (stream, "\n");
300 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
304 /* Wait for the output to drain away, as opposed to flushing
308 hardwire_drain_output (struct serial *scb)
311 return tcdrain (scb->fd);
315 return ioctl (scb->fd, TCSBRK, 1);
319 /* Get the current state and then restore it using TIOCSETP,
320 which should cause the output to drain and pending input
323 struct hardwire_ttystate state;
325 if (get_tty_state (scb, &state))
331 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
338 hardwire_flush_output (struct serial *scb)
341 return tcflush (scb->fd, TCOFLUSH);
345 return ioctl (scb->fd, TCFLSH, 1);
349 /* This flushes both input and output, but we can't do better. */
350 return ioctl (scb->fd, TIOCFLUSH, 0);
355 hardwire_flush_input (struct serial *scb)
357 ser_base_flush_input (scb);
360 return tcflush (scb->fd, TCIFLUSH);
364 return ioctl (scb->fd, TCFLSH, 0);
368 /* This flushes both input and output, but we can't do better. */
369 return ioctl (scb->fd, TIOCFLUSH, 0);
374 hardwire_send_break (struct serial *scb)
377 return tcsendbreak (scb->fd, 0);
381 return ioctl (scb->fd, TCSBRK, 0);
388 status = ioctl (scb->fd, TIOCSBRK, 0);
390 /* Can't use usleep; it doesn't exist in BSD 4.2. */
391 /* Note that if this gdb_select() is interrupted by a signal it will not
392 wait the full length of time. I think that is OK. */
394 status = ioctl (scb->fd, TIOCCBRK, 0);
401 hardwire_raw (struct serial *scb)
403 struct hardwire_ttystate state;
405 if (get_tty_state (scb, &state))
406 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
407 safe_strerror (errno));
410 state.termios.c_iflag = 0;
411 state.termios.c_oflag = 0;
412 state.termios.c_lflag = 0;
413 state.termios.c_cflag &= ~(CSIZE | PARENB);
414 state.termios.c_cflag |= CLOCAL | CS8;
416 /* h/w flow control. */
418 state.termios.c_cflag |= CRTSCTS;
420 state.termios.c_cflag &= ~CRTSCTS;
423 state.termios.c_cflag |= CRTS_IFLOW;
425 state.termios.c_cflag &= ~CRTS_IFLOW;
428 state.termios.c_cc[VMIN] = 0;
429 state.termios.c_cc[VTIME] = 0;
433 state.termio.c_iflag = 0;
434 state.termio.c_oflag = 0;
435 state.termio.c_lflag = 0;
436 state.termio.c_cflag &= ~(CSIZE | PARENB);
437 state.termio.c_cflag |= CLOCAL | CS8;
438 state.termio.c_cc[VMIN] = 0;
439 state.termio.c_cc[VTIME] = 0;
443 state.sgttyb.sg_flags |= RAW | ANYP;
444 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
447 scb->current_timeout = 0;
449 if (set_tty_state (scb, &state))
450 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
451 safe_strerror (errno));
454 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
455 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
457 For termio{s}, we actually just setup VTIME if necessary, and let the
458 timeout occur in the read() in hardwire_read(). */
460 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
461 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
464 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
465 possible values of the TIMEOUT parameter are ONE and ZERO.
466 Consequently all the code that tries to handle the possability of
467 an overflowed timer is unnecessary. */
470 wait_for (struct serial *scb, int timeout)
479 /* NOTE: Some OS's can scramble the READFDS when the select()
480 call fails (ex the kernel with Red Hat 5.2). Initialize all
481 arguments before each call. */
487 FD_SET (scb->fd, &readfds);
490 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, &tv);
492 numfds = gdb_select (scb->fd + 1, &readfds, 0, 0, 0);
496 return SERIAL_TIMEOUT;
497 else if (errno == EINTR)
500 return SERIAL_ERROR; /* Got an error from select or poll. */
504 #endif /* HAVE_SGTTY */
506 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
507 if (timeout == scb->current_timeout)
510 scb->current_timeout = timeout;
513 struct hardwire_ttystate state;
515 if (get_tty_state (scb, &state))
516 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
517 safe_strerror (errno));
523 state.termios.c_cc[VTIME] = 0;
524 state.termios.c_cc[VMIN] = 1;
528 state.termios.c_cc[VMIN] = 0;
529 state.termios.c_cc[VTIME] = timeout * 10;
530 if (state.termios.c_cc[VTIME] != timeout * 10)
533 /* If c_cc is an 8-bit signed character, we can't go
534 bigger than this. If it is always unsigned, we could use
537 scb->current_timeout = 12;
538 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
539 scb->timeout_remaining = timeout - scb->current_timeout;
548 state.termio.c_cc[VTIME] = 0;
549 state.termio.c_cc[VMIN] = 1;
553 state.termio.c_cc[VMIN] = 0;
554 state.termio.c_cc[VTIME] = timeout * 10;
555 if (state.termio.c_cc[VTIME] != timeout * 10)
557 /* If c_cc is an 8-bit signed character, we can't go
558 bigger than this. If it is always unsigned, we could use
561 scb->current_timeout = 12;
562 state.termio.c_cc[VTIME] = scb->current_timeout * 10;
563 scb->timeout_remaining = timeout - scb->current_timeout;
568 if (set_tty_state (scb, &state))
569 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
570 safe_strerror (errno));
574 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
577 /* Read a character with user-specified timeout. TIMEOUT is number of
578 seconds to wait, or -1 to wait forever. Use timeout of 0 to effect
579 a poll. Returns char if successful. Returns SERIAL_TIMEOUT if
580 timeout expired, EOF if line dropped dead, or SERIAL_ERROR for any
581 other error (see errno in that case). */
583 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
584 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
587 /* NOTE: cagney/1999-09-16: This function is not identical to
588 ser_base_readchar() as part of replacing it with ser_base*()
589 merging will be required - this code handles the case where read()
590 times out due to no data while ser_base_readchar() doesn't expect
594 do_hardwire_readchar (struct serial *scb, int timeout)
602 /* We have to be able to keep the GUI alive here, so we break the
603 original timeout into steps of 1 second, running the "keep the
604 GUI alive" hook each time through the loop.
606 Also, timeout = 0 means to poll, so we just set the delta to 0,
607 so we will only go through the loop once. */
609 delta = (timeout == 0 ? 0 : 1);
613 /* N.B. The UI may destroy our world (for instance by calling
614 remote_stop,) in which case we want to get out of here as
615 quickly as possible. It is not safe to touch scb, since
616 someone else might have freed it. The
617 deprecated_ui_loop_hook signals that we should exit by
620 if (deprecated_ui_loop_hook)
621 detach = deprecated_ui_loop_hook (0);
624 return SERIAL_TIMEOUT;
626 scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
627 status = wait_for (scb, delta);
632 status = read (scb->fd, scb->buf, BUFSIZ);
638 /* Zero characters means timeout (it could also be EOF, but
639 we don't (yet at least) distinguish). */
640 if (scb->timeout_remaining > 0)
642 timeout = scb->timeout_remaining;
645 else if (scb->timeout_remaining < 0)
648 return SERIAL_TIMEOUT;
650 else if (errno == EINTR)
653 return SERIAL_ERROR; /* Got an error from read. */
656 scb->bufcnt = status;
658 scb->bufp = scb->buf;
664 hardwire_readchar (struct serial *scb, int timeout)
666 return generic_readchar (scb, timeout, do_hardwire_readchar);
678 /* Translate baud rates from integers to damn B_codes. Unix should
679 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
779 rate_to_code (int rate)
783 for (i = 0; baudtab[i].rate != -1; i++)
785 /* test for perfect macth. */
786 if (rate == baudtab[i].rate)
787 return baudtab[i].code;
790 /* check if it is in between valid values. */
791 if (rate < baudtab[i].rate)
795 warning (_("Invalid baud rate %d. "
796 "Closest values are %d and %d."),
797 rate, baudtab[i - 1].rate, baudtab[i].rate);
801 warning (_("Invalid baud rate %d. Minimum value is %d."),
802 rate, baudtab[0].rate);
809 /* The requested speed was too large. */
810 warning (_("Invalid baud rate %d. Maximum value is %d."),
811 rate, baudtab[i - 1].rate);
816 hardwire_setbaudrate (struct serial *scb, int rate)
818 struct hardwire_ttystate state;
819 int baud_code = rate_to_code (rate);
823 /* The baud rate was not valid.
824 A warning has already been issued. */
829 if (get_tty_state (scb, &state))
833 cfsetospeed (&state.termios, baud_code);
834 cfsetispeed (&state.termios, baud_code);
842 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
843 state.termio.c_cflag |= baud_code;
847 state.sgttyb.sg_ispeed = baud_code;
848 state.sgttyb.sg_ospeed = baud_code;
851 return set_tty_state (scb, &state);
855 hardwire_setstopbits (struct serial *scb, int num)
857 struct hardwire_ttystate state;
860 if (get_tty_state (scb, &state))
865 case SERIAL_1_STOPBITS:
868 case SERIAL_1_AND_A_HALF_STOPBITS:
869 case SERIAL_2_STOPBITS:
878 state.termios.c_cflag &= ~CSTOPB;
880 state.termios.c_cflag |= CSTOPB; /* two bits */
885 state.termio.c_cflag &= ~CSTOPB;
887 state.termio.c_cflag |= CSTOPB; /* two bits */
891 return 0; /* sgtty doesn't support this */
894 return set_tty_state (scb, &state);
898 hardwire_close (struct serial *scb)
909 _initialize_ser_hardwire (void)
911 struct serial_ops *ops = XMALLOC (struct serial_ops);
913 memset (ops, 0, sizeof (struct serial_ops));
914 ops->name = "hardwire";
916 ops->open = hardwire_open;
917 ops->close = hardwire_close;
918 /* FIXME: Don't replace this with the equivalent ser_base*() until
919 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
921 ops->readchar = hardwire_readchar;
922 ops->write = ser_base_write;
923 ops->flush_output = hardwire_flush_output;
924 ops->flush_input = hardwire_flush_input;
925 ops->send_break = hardwire_send_break;
926 ops->go_raw = hardwire_raw;
927 ops->get_tty_state = hardwire_get_tty_state;
928 ops->copy_tty_state = hardwire_copy_tty_state;
929 ops->set_tty_state = hardwire_set_tty_state;
930 ops->print_tty_state = hardwire_print_tty_state;
931 ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
932 ops->setbaudrate = hardwire_setbaudrate;
933 ops->setstopbits = hardwire_setstopbits;
934 ops->drain_output = hardwire_drain_output;
935 ops->async = ser_base_async;
936 ops->read_prim = ser_unix_read_prim;
937 ops->write_prim = ser_unix_write_prim;
938 serial_add_interface (ops);
942 add_setshow_boolean_cmd ("remoteflow", no_class,
944 Set use of hardware flow control for remote serial I/O."), _("\
945 Show use of hardware flow control for remote serial I/O."), _("\
946 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
947 when debugging using remote targets."),
950 &setlist, &showlist);
956 ser_unix_read_prim (struct serial *scb, size_t count)
962 status = read (scb->fd, scb->buf, count);
963 if (status != -1 || errno != EINTR)
970 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
972 /* ??? Historically, GDB has not retried calls to "write" that
974 return write (scb->fd, buf, len);