1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2003,
4 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include <sys/types.h>
29 #include <sys/socket.h>
32 #include "gdb_select.h"
33 #include "gdb_string.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
= open (name
, O_RDWR
);
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
))
188 return (serial_ttystate
) state
;
192 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
194 struct hardwire_ttystate
*state
;
196 state
= (struct hardwire_ttystate
*) ttystate
;
198 return set_tty_state (scb
, state
);
202 hardwire_noflush_set_tty_state (struct serial
*scb
,
203 serial_ttystate new_ttystate
,
204 serial_ttystate old_ttystate
)
206 struct hardwire_ttystate new_state
;
208 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) old_ttystate
;
211 new_state
= *(struct hardwire_ttystate
*) new_ttystate
;
213 /* Don't change in or out of raw mode; we don't want to flush input.
214 termio and termios have no such restriction; for them flushing input
215 is separate from setting the attributes. */
218 if (state
->sgttyb
.sg_flags
& RAW
)
219 new_state
.sgttyb
.sg_flags
|= RAW
;
221 new_state
.sgttyb
.sg_flags
&= ~RAW
;
223 /* I'm not sure whether this is necessary; the manpage just mentions
225 if (state
->sgttyb
.sg_flags
& CBREAK
)
226 new_state
.sgttyb
.sg_flags
|= CBREAK
;
228 new_state
.sgttyb
.sg_flags
&= ~CBREAK
;
231 return set_tty_state (scb
, &new_state
);
235 hardwire_print_tty_state (struct serial
*scb
,
236 serial_ttystate ttystate
,
237 struct ui_file
*stream
)
239 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
243 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
244 (int) state
->termios
.c_iflag
,
245 (int) state
->termios
.c_oflag
);
246 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
247 (int) state
->termios
.c_cflag
,
248 (int) state
->termios
.c_lflag
);
250 /* This not in POSIX, and is not really documented by those systems
251 which have it (at least not Sun). */
252 fprintf_filtered (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
254 fprintf_filtered (stream
, "c_cc: ");
255 for (i
= 0; i
< NCCS
; i
+= 1)
256 fprintf_filtered (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
257 fprintf_filtered (stream
, "\n");
261 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
262 state
->termio
.c_iflag
, state
->termio
.c_oflag
);
263 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
264 state
->termio
.c_cflag
, state
->termio
.c_lflag
,
265 state
->termio
.c_line
);
266 fprintf_filtered (stream
, "c_cc: ");
267 for (i
= 0; i
< NCC
; i
+= 1)
268 fprintf_filtered (stream
, "0x%x ", state
->termio
.c_cc
[i
]);
269 fprintf_filtered (stream
, "\n");
273 fprintf_filtered (stream
, "sgttyb.sg_flags = 0x%x.\n",
274 state
->sgttyb
.sg_flags
);
276 fprintf_filtered (stream
, "tchars: ");
277 for (i
= 0; i
< (int) sizeof (struct tchars
); i
++)
278 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->tc
)[i
]);
279 fprintf_filtered (stream
, "\n");
281 fprintf_filtered (stream
, "ltchars: ");
282 for (i
= 0; i
< (int) sizeof (struct ltchars
); i
++)
283 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->ltc
)[i
]);
284 fprintf_filtered (stream
, "\n");
286 fprintf_filtered (stream
, "lmode: 0x%x\n", state
->lmode
);
290 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
293 hardwire_drain_output (struct serial
*scb
)
296 return tcdrain (scb
->fd
);
300 return ioctl (scb
->fd
, TCSBRK
, 1);
304 /* Get the current state and then restore it using TIOCSETP,
305 which should cause the output to drain and pending input
308 struct hardwire_ttystate state
;
309 if (get_tty_state (scb
, &state
))
315 return (ioctl (scb
->fd
, TIOCSETP
, &state
.sgttyb
));
322 hardwire_flush_output (struct serial
*scb
)
325 return tcflush (scb
->fd
, TCOFLUSH
);
329 return ioctl (scb
->fd
, TCFLSH
, 1);
333 /* This flushes both input and output, but we can't do better. */
334 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
339 hardwire_flush_input (struct serial
*scb
)
341 ser_base_flush_input (scb
);
344 return tcflush (scb
->fd
, TCIFLUSH
);
348 return ioctl (scb
->fd
, TCFLSH
, 0);
352 /* This flushes both input and output, but we can't do better. */
353 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
358 hardwire_send_break (struct serial
*scb
)
361 return tcsendbreak (scb
->fd
, 0);
365 return ioctl (scb
->fd
, TCSBRK
, 0);
372 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
374 /* Can't use usleep; it doesn't exist in BSD 4.2. */
375 /* Note that if this gdb_select() is interrupted by a signal it will not
376 wait the full length of time. I think that is OK. */
378 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
385 hardwire_raw (struct serial
*scb
)
387 struct hardwire_ttystate state
;
389 if (get_tty_state (scb
, &state
))
390 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
393 state
.termios
.c_iflag
= 0;
394 state
.termios
.c_oflag
= 0;
395 state
.termios
.c_lflag
= 0;
396 state
.termios
.c_cflag
&= ~(CSIZE
| PARENB
);
397 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
399 /* h/w flow control. */
401 state
.termios
.c_cflag
|= CRTSCTS
;
403 state
.termios
.c_cflag
&= ~CRTSCTS
;
406 state
.termios
.c_cflag
|= CRTS_IFLOW
;
408 state
.termios
.c_cflag
&= ~CRTS_IFLOW
;
411 state
.termios
.c_cc
[VMIN
] = 0;
412 state
.termios
.c_cc
[VTIME
] = 0;
416 state
.termio
.c_iflag
= 0;
417 state
.termio
.c_oflag
= 0;
418 state
.termio
.c_lflag
= 0;
419 state
.termio
.c_cflag
&= ~(CSIZE
| PARENB
);
420 state
.termio
.c_cflag
|= CLOCAL
| CS8
;
421 state
.termio
.c_cc
[VMIN
] = 0;
422 state
.termio
.c_cc
[VTIME
] = 0;
426 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
427 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
430 scb
->current_timeout
= 0;
432 if (set_tty_state (scb
, &state
))
433 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
436 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
437 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
439 For termio{s}, we actually just setup VTIME if necessary, and let the
440 timeout occur in the read() in hardwire_read().
443 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
444 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
447 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
448 possible values of the TIMEOUT parameter are ONE and ZERO.
449 Consequently all the code that tries to handle the possability of
450 an overflowed timer is unnecessary. */
453 wait_for (struct serial
*scb
, int timeout
)
462 /* NOTE: Some OS's can scramble the READFDS when the select()
463 call fails (ex the kernel with Red Hat 5.2). Initialize all
464 arguments before each call. */
470 FD_SET (scb
->fd
, &readfds
);
473 numfds
= gdb_select (scb
->fd
+ 1, &readfds
, 0, 0, &tv
);
475 numfds
= gdb_select (scb
->fd
+ 1, &readfds
, 0, 0, 0);
479 return SERIAL_TIMEOUT
;
480 else if (errno
== EINTR
)
483 return SERIAL_ERROR
; /* Got an error from select or poll */
487 #endif /* HAVE_SGTTY */
489 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
490 if (timeout
== scb
->current_timeout
)
493 scb
->current_timeout
= timeout
;
496 struct hardwire_ttystate state
;
498 if (get_tty_state (scb
, &state
))
499 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
505 state
.termios
.c_cc
[VTIME
] = 0;
506 state
.termios
.c_cc
[VMIN
] = 1;
510 state
.termios
.c_cc
[VMIN
] = 0;
511 state
.termios
.c_cc
[VTIME
] = timeout
* 10;
512 if (state
.termios
.c_cc
[VTIME
] != timeout
* 10)
515 /* If c_cc is an 8-bit signed character, we can't go
516 bigger than this. If it is always unsigned, we could use
519 scb
->current_timeout
= 12;
520 state
.termios
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
521 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
530 state
.termio
.c_cc
[VTIME
] = 0;
531 state
.termio
.c_cc
[VMIN
] = 1;
535 state
.termio
.c_cc
[VMIN
] = 0;
536 state
.termio
.c_cc
[VTIME
] = timeout
* 10;
537 if (state
.termio
.c_cc
[VTIME
] != timeout
* 10)
539 /* If c_cc is an 8-bit signed character, we can't go
540 bigger than this. If it is always unsigned, we could use
543 scb
->current_timeout
= 12;
544 state
.termio
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
545 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
550 if (set_tty_state (scb
, &state
))
551 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
555 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
558 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
559 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
560 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
561 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
563 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
564 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
567 /* NOTE: cagney/1999-09-16: This function is not identical to
568 ser_base_readchar() as part of replacing it with ser_base*()
569 merging will be required - this code handles the case where read()
570 times out due to no data while ser_base_readchar() doesn't expect
574 do_hardwire_readchar (struct serial
*scb
, int timeout
)
582 /* We have to be able to keep the GUI alive here, so we break the
583 original timeout into steps of 1 second, running the "keep the
584 GUI alive" hook each time through the loop.
586 Also, timeout = 0 means to poll, so we just set the delta to 0,
587 so we will only go through the loop once. */
589 delta
= (timeout
== 0 ? 0 : 1);
593 /* N.B. The UI may destroy our world (for instance by calling
594 remote_stop,) in which case we want to get out of here as
595 quickly as possible. It is not safe to touch scb, since
596 someone else might have freed it. The
597 deprecated_ui_loop_hook signals that we should exit by
600 if (deprecated_ui_loop_hook
)
601 detach
= deprecated_ui_loop_hook (0);
604 return SERIAL_TIMEOUT
;
606 scb
->timeout_remaining
= (timeout
< 0 ? timeout
: timeout
- delta
);
607 status
= wait_for (scb
, delta
);
612 status
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
618 /* Zero characters means timeout (it could also be EOF, but
619 we don't (yet at least) distinguish). */
620 if (scb
->timeout_remaining
> 0)
622 timeout
= scb
->timeout_remaining
;
625 else if (scb
->timeout_remaining
< 0)
628 return SERIAL_TIMEOUT
;
630 else if (errno
== EINTR
)
633 return SERIAL_ERROR
; /* Got an error from read */
636 scb
->bufcnt
= status
;
638 scb
->bufp
= scb
->buf
;
644 hardwire_readchar (struct serial
*scb
, int timeout
)
646 return generic_readchar (scb
, timeout
, do_hardwire_readchar
);
658 /* Translate baud rates from integers to damn B_codes. Unix should
659 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
759 rate_to_code (int rate
)
763 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
765 /* test for perfect macth. */
766 if (rate
== baudtab
[i
].rate
)
767 return baudtab
[i
].code
;
770 /* check if it is in between valid values. */
771 if (rate
< baudtab
[i
].rate
)
775 warning (_("Invalid baud rate %d. Closest values are %d and %d."),
776 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
780 warning (_("Invalid baud rate %d. Minimum value is %d."),
781 rate
, baudtab
[0].rate
);
788 /* The requested speed was too large. */
789 warning (_("Invalid baud rate %d. Maximum value is %d."),
790 rate
, baudtab
[i
- 1].rate
);
795 hardwire_setbaudrate (struct serial
*scb
, int rate
)
797 struct hardwire_ttystate state
;
798 int baud_code
= rate_to_code (rate
);
802 /* The baud rate was not valid.
803 A warning has already been issued. */
808 if (get_tty_state (scb
, &state
))
812 cfsetospeed (&state
.termios
, baud_code
);
813 cfsetispeed (&state
.termios
, baud_code
);
821 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
822 state
.termio
.c_cflag
|= baud_code
;
826 state
.sgttyb
.sg_ispeed
= baud_code
;
827 state
.sgttyb
.sg_ospeed
= baud_code
;
830 return set_tty_state (scb
, &state
);
834 hardwire_setstopbits (struct serial
*scb
, int num
)
836 struct hardwire_ttystate state
;
839 if (get_tty_state (scb
, &state
))
844 case SERIAL_1_STOPBITS
:
847 case SERIAL_1_AND_A_HALF_STOPBITS
:
848 case SERIAL_2_STOPBITS
:
857 state
.termios
.c_cflag
&= ~CSTOPB
;
859 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
864 state
.termio
.c_cflag
&= ~CSTOPB
;
866 state
.termio
.c_cflag
|= CSTOPB
; /* two bits */
870 return 0; /* sgtty doesn't support this */
873 return set_tty_state (scb
, &state
);
877 hardwire_close (struct serial
*scb
)
888 _initialize_ser_hardwire (void)
890 struct serial_ops
*ops
= XMALLOC (struct serial_ops
);
891 memset (ops
, 0, sizeof (struct serial_ops
));
892 ops
->name
= "hardwire";
894 ops
->open
= hardwire_open
;
895 ops
->close
= hardwire_close
;
896 /* FIXME: Don't replace this with the equivalent ser_base*() until
897 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
899 ops
->readchar
= hardwire_readchar
;
900 ops
->write
= ser_base_write
;
901 ops
->flush_output
= hardwire_flush_output
;
902 ops
->flush_input
= hardwire_flush_input
;
903 ops
->send_break
= hardwire_send_break
;
904 ops
->go_raw
= hardwire_raw
;
905 ops
->get_tty_state
= hardwire_get_tty_state
;
906 ops
->set_tty_state
= hardwire_set_tty_state
;
907 ops
->print_tty_state
= hardwire_print_tty_state
;
908 ops
->noflush_set_tty_state
= hardwire_noflush_set_tty_state
;
909 ops
->setbaudrate
= hardwire_setbaudrate
;
910 ops
->setstopbits
= hardwire_setstopbits
;
911 ops
->drain_output
= hardwire_drain_output
;
912 ops
->async
= ser_base_async
;
913 ops
->read_prim
= ser_unix_read_prim
;
914 ops
->write_prim
= ser_unix_write_prim
;
915 serial_add_interface (ops
);
919 add_setshow_boolean_cmd ("remoteflow", no_class
,
921 Set use of hardware flow control for remote serial I/O."), _("\
922 Show use of hardware flow control for remote serial I/O."), _("\
923 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
924 when debugging using remote targets."),
927 &setlist
, &showlist
);
933 ser_unix_read_prim (struct serial
*scb
, size_t count
)
939 status
= read (scb
->fd
, scb
->buf
, count
);
940 if (status
!= -1 || errno
!= EINTR
)
947 ser_unix_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
949 /* ??? Historically, GDB has not retried calls to "write" that
951 return write (scb
->fd
, buf
, len
);