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 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
29 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include "gdb_select.h"
35 #include "gdb_string.h"
40 struct hardwire_ttystate
42 struct termios termios
;
46 /* Boolean to explicitly enable or disable h/w flow control. */
47 static int serial_hwflow
;
49 show_serial_hwflow (struct ui_file
*file
, int from_tty
,
50 struct cmd_list_element
*c
, const char *value
)
52 fprintf_filtered (file
, _("Hardware flow control is %s.\n"), value
);
60 /* It is believed that all systems which have added job control to SVR3
61 (e.g. sco) have also added termios. Even if not, trying to figure out
62 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
63 bewildering. So we don't attempt it. */
65 struct hardwire_ttystate
72 struct hardwire_ttystate
77 /* Line discipline flags. */
82 static int hardwire_open (struct serial
*scb
, const char *name
);
83 static void hardwire_raw (struct serial
*scb
);
84 static int wait_for (struct serial
*scb
, int timeout
);
85 static int hardwire_readchar (struct serial
*scb
, int timeout
);
86 static int do_hardwire_readchar (struct serial
*scb
, int timeout
);
87 static int rate_to_code (int rate
);
88 static int hardwire_setbaudrate (struct serial
*scb
, int rate
);
89 static void hardwire_close (struct serial
*scb
);
90 static int get_tty_state (struct serial
*scb
,
91 struct hardwire_ttystate
* state
);
92 static int set_tty_state (struct serial
*scb
,
93 struct hardwire_ttystate
* state
);
94 static serial_ttystate
hardwire_get_tty_state (struct serial
*scb
);
95 static int hardwire_set_tty_state (struct serial
*scb
, serial_ttystate state
);
96 static int hardwire_noflush_set_tty_state (struct serial
*, serial_ttystate
,
98 static void hardwire_print_tty_state (struct serial
*, serial_ttystate
,
100 static int hardwire_drain_output (struct serial
*);
101 static int hardwire_flush_output (struct serial
*);
102 static int hardwire_flush_input (struct serial
*);
103 static int hardwire_send_break (struct serial
*);
104 static int hardwire_setstopbits (struct serial
*, int);
106 void _initialize_ser_hardwire (void);
108 /* Open up a real live device for serial I/O */
111 hardwire_open (struct serial
*scb
, const char *name
)
113 scb
->fd
= open (name
, O_RDWR
);
121 get_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
124 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
131 if (ioctl (scb
->fd
, TCGETA
, &state
->termio
) < 0)
137 if (ioctl (scb
->fd
, TIOCGETP
, &state
->sgttyb
) < 0)
139 if (ioctl (scb
->fd
, TIOCGETC
, &state
->tc
) < 0)
141 if (ioctl (scb
->fd
, TIOCGLTC
, &state
->ltc
) < 0)
143 if (ioctl (scb
->fd
, TIOCLGET
, &state
->lmode
) < 0)
151 set_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
154 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
161 if (ioctl (scb
->fd
, TCSETA
, &state
->termio
) < 0)
167 if (ioctl (scb
->fd
, TIOCSETN
, &state
->sgttyb
) < 0)
169 if (ioctl (scb
->fd
, TIOCSETC
, &state
->tc
) < 0)
171 if (ioctl (scb
->fd
, TIOCSLTC
, &state
->ltc
) < 0)
173 if (ioctl (scb
->fd
, TIOCLSET
, &state
->lmode
) < 0)
180 static serial_ttystate
181 hardwire_get_tty_state (struct serial
*scb
)
183 struct hardwire_ttystate
*state
;
185 state
= (struct hardwire_ttystate
*) xmalloc (sizeof *state
);
187 if (get_tty_state (scb
, state
))
190 return (serial_ttystate
) state
;
194 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
196 struct hardwire_ttystate
*state
;
198 state
= (struct hardwire_ttystate
*) ttystate
;
200 return set_tty_state (scb
, state
);
204 hardwire_noflush_set_tty_state (struct serial
*scb
,
205 serial_ttystate new_ttystate
,
206 serial_ttystate old_ttystate
)
208 struct hardwire_ttystate new_state
;
210 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) old_ttystate
;
213 new_state
= *(struct hardwire_ttystate
*) new_ttystate
;
215 /* Don't change in or out of raw mode; we don't want to flush input.
216 termio and termios have no such restriction; for them flushing input
217 is separate from setting the attributes. */
220 if (state
->sgttyb
.sg_flags
& RAW
)
221 new_state
.sgttyb
.sg_flags
|= RAW
;
223 new_state
.sgttyb
.sg_flags
&= ~RAW
;
225 /* I'm not sure whether this is necessary; the manpage just mentions
227 if (state
->sgttyb
.sg_flags
& CBREAK
)
228 new_state
.sgttyb
.sg_flags
|= CBREAK
;
230 new_state
.sgttyb
.sg_flags
&= ~CBREAK
;
233 return set_tty_state (scb
, &new_state
);
237 hardwire_print_tty_state (struct serial
*scb
,
238 serial_ttystate ttystate
,
239 struct ui_file
*stream
)
241 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
245 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
246 (int) state
->termios
.c_iflag
,
247 (int) state
->termios
.c_oflag
);
248 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
249 (int) state
->termios
.c_cflag
,
250 (int) state
->termios
.c_lflag
);
252 /* This not in POSIX, and is not really documented by those systems
253 which have it (at least not Sun). */
254 fprintf_filtered (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
256 fprintf_filtered (stream
, "c_cc: ");
257 for (i
= 0; i
< NCCS
; i
+= 1)
258 fprintf_filtered (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
259 fprintf_filtered (stream
, "\n");
263 fprintf_filtered (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
264 state
->termio
.c_iflag
, state
->termio
.c_oflag
);
265 fprintf_filtered (stream
, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
266 state
->termio
.c_cflag
, state
->termio
.c_lflag
,
267 state
->termio
.c_line
);
268 fprintf_filtered (stream
, "c_cc: ");
269 for (i
= 0; i
< NCC
; i
+= 1)
270 fprintf_filtered (stream
, "0x%x ", state
->termio
.c_cc
[i
]);
271 fprintf_filtered (stream
, "\n");
275 fprintf_filtered (stream
, "sgttyb.sg_flags = 0x%x.\n",
276 state
->sgttyb
.sg_flags
);
278 fprintf_filtered (stream
, "tchars: ");
279 for (i
= 0; i
< (int) sizeof (struct tchars
); i
++)
280 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->tc
)[i
]);
281 fprintf_filtered (stream
, "\n");
283 fprintf_filtered (stream
, "ltchars: ");
284 for (i
= 0; i
< (int) sizeof (struct ltchars
); i
++)
285 fprintf_filtered (stream
, "0x%x ", ((unsigned char *) &state
->ltc
)[i
]);
286 fprintf_filtered (stream
, "\n");
288 fprintf_filtered (stream
, "lmode: 0x%x\n", state
->lmode
);
292 /* Wait for the output to drain away, as opposed to flushing (discarding) it */
295 hardwire_drain_output (struct serial
*scb
)
298 return tcdrain (scb
->fd
);
302 return ioctl (scb
->fd
, TCSBRK
, 1);
306 /* Get the current state and then restore it using TIOCSETP,
307 which should cause the output to drain and pending input
310 struct hardwire_ttystate state
;
311 if (get_tty_state (scb
, &state
))
317 return (ioctl (scb
->fd
, TIOCSETP
, &state
.sgttyb
));
324 hardwire_flush_output (struct serial
*scb
)
327 return tcflush (scb
->fd
, TCOFLUSH
);
331 return ioctl (scb
->fd
, TCFLSH
, 1);
335 /* This flushes both input and output, but we can't do better. */
336 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
341 hardwire_flush_input (struct serial
*scb
)
343 ser_base_flush_input (scb
);
346 return tcflush (scb
->fd
, TCIFLUSH
);
350 return ioctl (scb
->fd
, TCFLSH
, 0);
354 /* This flushes both input and output, but we can't do better. */
355 return ioctl (scb
->fd
, TIOCFLUSH
, 0);
360 hardwire_send_break (struct serial
*scb
)
363 return tcsendbreak (scb
->fd
, 0);
367 return ioctl (scb
->fd
, TCSBRK
, 0);
373 struct timeval timeout
;
375 status
= ioctl (scb
->fd
, TIOCSBRK
, 0);
377 /* Can't use usleep; it doesn't exist in BSD 4.2. */
378 /* Note that if this select() is interrupted by a signal it will not wait
379 the full length of time. I think that is OK. */
381 timeout
.tv_usec
= 250000;
382 gdb_select (0, 0, 0, 0, &timeout
);
383 status
= ioctl (scb
->fd
, TIOCCBRK
, 0);
390 hardwire_raw (struct serial
*scb
)
392 struct hardwire_ttystate state
;
394 if (get_tty_state (scb
, &state
))
395 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
398 state
.termios
.c_iflag
= 0;
399 state
.termios
.c_oflag
= 0;
400 state
.termios
.c_lflag
= 0;
401 state
.termios
.c_cflag
&= ~(CSIZE
| PARENB
);
402 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
404 /* h/w flow control. */
406 state
.termios
.c_cflag
|= CRTSCTS
;
408 state
.termios
.c_cflag
&= ~CRTSCTS
;
411 state
.termios
.c_cflag
|= CRTS_IFLOW
;
413 state
.termios
.c_cflag
&= ~CRTS_IFLOW
;
416 state
.termios
.c_cc
[VMIN
] = 0;
417 state
.termios
.c_cc
[VTIME
] = 0;
421 state
.termio
.c_iflag
= 0;
422 state
.termio
.c_oflag
= 0;
423 state
.termio
.c_lflag
= 0;
424 state
.termio
.c_cflag
&= ~(CSIZE
| PARENB
);
425 state
.termio
.c_cflag
|= CLOCAL
| CS8
;
426 state
.termio
.c_cc
[VMIN
] = 0;
427 state
.termio
.c_cc
[VTIME
] = 0;
431 state
.sgttyb
.sg_flags
|= RAW
| ANYP
;
432 state
.sgttyb
.sg_flags
&= ~(CBREAK
| ECHO
);
435 scb
->current_timeout
= 0;
437 if (set_tty_state (scb
, &state
))
438 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
441 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
442 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
444 For termio{s}, we actually just setup VTIME if necessary, and let the
445 timeout occur in the read() in hardwire_read().
448 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
449 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
452 /* NOTE: cagney/1999-09-30: Much of the code below is dead. The only
453 possible values of the TIMEOUT parameter are ONE and ZERO.
454 Consequently all the code that tries to handle the possability of
455 an overflowed timer is unnecessary. */
458 wait_for (struct serial
*scb
, int timeout
)
467 /* NOTE: Some OS's can scramble the READFDS when the select()
468 call fails (ex the kernel with Red Hat 5.2). Initialize all
469 arguments before each call. */
475 FD_SET (scb
->fd
, &readfds
);
478 numfds
= gdb_select (scb
->fd
+ 1, &readfds
, 0, 0, &tv
);
480 numfds
= gdb_select (scb
->fd
+ 1, &readfds
, 0, 0, 0);
484 return SERIAL_TIMEOUT
;
485 else if (errno
== EINTR
)
488 return SERIAL_ERROR
; /* Got an error from select or poll */
492 #endif /* HAVE_SGTTY */
494 #if defined HAVE_TERMIO || defined HAVE_TERMIOS
495 if (timeout
== scb
->current_timeout
)
498 scb
->current_timeout
= timeout
;
501 struct hardwire_ttystate state
;
503 if (get_tty_state (scb
, &state
))
504 fprintf_unfiltered (gdb_stderr
, "get_tty_state failed: %s\n", safe_strerror (errno
));
510 state
.termios
.c_cc
[VTIME
] = 0;
511 state
.termios
.c_cc
[VMIN
] = 1;
515 state
.termios
.c_cc
[VMIN
] = 0;
516 state
.termios
.c_cc
[VTIME
] = timeout
* 10;
517 if (state
.termios
.c_cc
[VTIME
] != timeout
* 10)
520 /* If c_cc is an 8-bit signed character, we can't go
521 bigger than this. If it is always unsigned, we could use
524 scb
->current_timeout
= 12;
525 state
.termios
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
526 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
535 state
.termio
.c_cc
[VTIME
] = 0;
536 state
.termio
.c_cc
[VMIN
] = 1;
540 state
.termio
.c_cc
[VMIN
] = 0;
541 state
.termio
.c_cc
[VTIME
] = timeout
* 10;
542 if (state
.termio
.c_cc
[VTIME
] != timeout
* 10)
544 /* If c_cc is an 8-bit signed character, we can't go
545 bigger than this. If it is always unsigned, we could use
548 scb
->current_timeout
= 12;
549 state
.termio
.c_cc
[VTIME
] = scb
->current_timeout
* 10;
550 scb
->timeout_remaining
= timeout
- scb
->current_timeout
;
555 if (set_tty_state (scb
, &state
))
556 fprintf_unfiltered (gdb_stderr
, "set_tty_state failed: %s\n", safe_strerror (errno
));
560 #endif /* HAVE_TERMIO || HAVE_TERMIOS */
563 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
564 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
565 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
566 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
568 /* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
569 ser_base*() until the old TERMIOS/SGTTY/... timer code has been
572 /* NOTE: cagney/1999-09-16: This function is not identical to
573 ser_base_readchar() as part of replacing it with ser_base*()
574 merging will be required - this code handles the case where read()
575 times out due to no data while ser_base_readchar() doesn't expect
579 do_hardwire_readchar (struct serial
*scb
, int timeout
)
587 /* We have to be able to keep the GUI alive here, so we break the
588 original timeout into steps of 1 second, running the "keep the
589 GUI alive" hook each time through the loop.
591 Also, timeout = 0 means to poll, so we just set the delta to 0,
592 so we will only go through the loop once. */
594 delta
= (timeout
== 0 ? 0 : 1);
598 /* N.B. The UI may destroy our world (for instance by calling
599 remote_stop,) in which case we want to get out of here as
600 quickly as possible. It is not safe to touch scb, since
601 someone else might have freed it. The
602 deprecated_ui_loop_hook signals that we should exit by
605 if (deprecated_ui_loop_hook
)
606 detach
= deprecated_ui_loop_hook (0);
609 return SERIAL_TIMEOUT
;
611 scb
->timeout_remaining
= (timeout
< 0 ? timeout
: timeout
- delta
);
612 status
= wait_for (scb
, delta
);
617 status
= read (scb
->fd
, scb
->buf
, BUFSIZ
);
623 /* Zero characters means timeout (it could also be EOF, but
624 we don't (yet at least) distinguish). */
625 if (scb
->timeout_remaining
> 0)
627 timeout
= scb
->timeout_remaining
;
630 else if (scb
->timeout_remaining
< 0)
633 return SERIAL_TIMEOUT
;
635 else if (errno
== EINTR
)
638 return SERIAL_ERROR
; /* Got an error from read */
641 scb
->bufcnt
= status
;
643 scb
->bufp
= scb
->buf
;
649 hardwire_readchar (struct serial
*scb
, int timeout
)
651 return generic_readchar (scb
, timeout
, do_hardwire_readchar
);
663 /* Translate baud rates from integers to damn B_codes. Unix should
664 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
764 rate_to_code (int rate
)
768 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
770 /* test for perfect macth. */
771 if (rate
== baudtab
[i
].rate
)
772 return baudtab
[i
].code
;
775 /* check if it is in between valid values. */
776 if (rate
< baudtab
[i
].rate
)
780 warning (_("Invalid baud rate %d. Closest values are %d and %d."),
781 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
785 warning (_("Invalid baud rate %d. Minimum value is %d."),
786 rate
, baudtab
[0].rate
);
793 /* The requested speed was too large. */
794 warning (_("Invalid baud rate %d. Maximum value is %d."),
795 rate
, baudtab
[i
- 1].rate
);
800 hardwire_setbaudrate (struct serial
*scb
, int rate
)
802 struct hardwire_ttystate state
;
803 int baud_code
= rate_to_code (rate
);
807 /* The baud rate was not valid.
808 A warning has already been issued. */
813 if (get_tty_state (scb
, &state
))
817 cfsetospeed (&state
.termios
, baud_code
);
818 cfsetispeed (&state
.termios
, baud_code
);
826 state
.termio
.c_cflag
&= ~(CBAUD
| CIBAUD
);
827 state
.termio
.c_cflag
|= baud_code
;
831 state
.sgttyb
.sg_ispeed
= baud_code
;
832 state
.sgttyb
.sg_ospeed
= baud_code
;
835 return set_tty_state (scb
, &state
);
839 hardwire_setstopbits (struct serial
*scb
, int num
)
841 struct hardwire_ttystate state
;
844 if (get_tty_state (scb
, &state
))
849 case SERIAL_1_STOPBITS
:
852 case SERIAL_1_AND_A_HALF_STOPBITS
:
853 case SERIAL_2_STOPBITS
:
862 state
.termios
.c_cflag
&= ~CSTOPB
;
864 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
869 state
.termio
.c_cflag
&= ~CSTOPB
;
871 state
.termio
.c_cflag
|= CSTOPB
; /* two bits */
875 return 0; /* sgtty doesn't support this */
878 return set_tty_state (scb
, &state
);
882 hardwire_close (struct serial
*scb
)
893 _initialize_ser_hardwire (void)
895 struct serial_ops
*ops
= XMALLOC (struct serial_ops
);
896 memset (ops
, 0, sizeof (struct serial_ops
));
897 ops
->name
= "hardwire";
899 ops
->open
= hardwire_open
;
900 ops
->close
= hardwire_close
;
901 /* FIXME: Don't replace this with the equivalent ser_base*() until
902 the old TERMIOS/SGTTY/... timer code has been flushed. cagney
904 ops
->readchar
= hardwire_readchar
;
905 ops
->write
= ser_base_write
;
906 ops
->flush_output
= hardwire_flush_output
;
907 ops
->flush_input
= hardwire_flush_input
;
908 ops
->send_break
= hardwire_send_break
;
909 ops
->go_raw
= hardwire_raw
;
910 ops
->get_tty_state
= hardwire_get_tty_state
;
911 ops
->set_tty_state
= hardwire_set_tty_state
;
912 ops
->print_tty_state
= hardwire_print_tty_state
;
913 ops
->noflush_set_tty_state
= hardwire_noflush_set_tty_state
;
914 ops
->setbaudrate
= hardwire_setbaudrate
;
915 ops
->setstopbits
= hardwire_setstopbits
;
916 ops
->drain_output
= hardwire_drain_output
;
917 ops
->async
= ser_base_async
;
918 ops
->read_prim
= ser_unix_read_prim
;
919 ops
->write_prim
= ser_unix_write_prim
;
920 serial_add_interface (ops
);
924 add_setshow_boolean_cmd ("remoteflow", no_class
,
926 Set use of hardware flow control for remote serial I/O."), _("\
927 Show use of hardware flow control for remote serial I/O."), _("\
928 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
929 when debugging using remote targets."),
932 &setlist
, &showlist
);
938 ser_unix_read_prim (struct serial
*scb
, size_t count
)
944 status
= read (scb
->fd
, scb
->buf
, count
);
945 if (status
!= -1 || errno
!= EINTR
)
952 ser_unix_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
954 /* ??? Historically, GDB has not retried calls to "write" that
956 return write (scb
->fd
, buf
, len
);