1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992-2024 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/>. */
25 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include "gdbsupport/gdb_sys_time.h"
30 #include "gdbsupport/gdb_select.h"
31 #include "cli/cli-cmds.h"
32 #include "gdbsupport/filestuff.h"
34 #include "gdbsupport/scoped_ignore_sigttou.h"
36 struct hardwire_ttystate
38 struct termios termios
;
42 /* Boolean to explicitly enable or disable h/w flow control. */
43 static bool serial_hwflow
;
45 show_serial_hwflow (struct ui_file
*file
, int from_tty
,
46 struct cmd_list_element
*c
, const char *value
)
48 gdb_printf (file
, _("Hardware flow control is %s.\n"), value
);
52 static void hardwire_raw (struct serial
*scb
);
53 static int rate_to_code (int rate
);
54 static void hardwire_setbaudrate (struct serial
*scb
, int rate
);
55 static int hardwire_setparity (struct serial
*scb
, int parity
);
56 static void hardwire_close (struct serial
*scb
);
57 static int get_tty_state (struct serial
*scb
,
58 struct hardwire_ttystate
* state
);
59 static int set_tty_state (struct serial
*scb
,
60 struct hardwire_ttystate
* state
);
61 static serial_ttystate
hardwire_get_tty_state (struct serial
*scb
);
62 static int hardwire_set_tty_state (struct serial
*scb
, serial_ttystate state
);
63 static void hardwire_print_tty_state (struct serial
*, serial_ttystate
,
65 static int hardwire_drain_output (struct serial
*);
66 static int hardwire_flush_output (struct serial
*);
67 static int hardwire_flush_input (struct serial
*);
68 static void hardwire_send_break (struct serial
*);
69 static int hardwire_setstopbits (struct serial
*, int);
71 /* Open up a real live device for serial I/O. */
74 hardwire_open (struct serial
*scb
, const char *name
)
76 scb
->fd
= gdb_open_cloexec (name
, O_RDWR
, 0).release ();
78 perror_with_name ("could not open device");
82 get_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
84 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
91 set_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
93 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
99 static serial_ttystate
100 hardwire_get_tty_state (struct serial
*scb
)
102 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
104 if (get_tty_state (scb
, state
))
110 return (serial_ttystate
) state
;
113 static serial_ttystate
114 hardwire_copy_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
116 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
118 *state
= *(struct hardwire_ttystate
*) ttystate
;
120 return (serial_ttystate
) state
;
124 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
126 struct hardwire_ttystate
*state
;
128 state
= (struct hardwire_ttystate
*) ttystate
;
130 return set_tty_state (scb
, state
);
134 hardwire_print_tty_state (struct serial
*scb
,
135 serial_ttystate ttystate
,
136 struct ui_file
*stream
)
138 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
141 gdb_printf (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
142 (int) state
->termios
.c_iflag
,
143 (int) state
->termios
.c_oflag
);
144 gdb_printf (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
145 (int) state
->termios
.c_cflag
,
146 (int) state
->termios
.c_lflag
);
148 /* This not in POSIX, and is not really documented by those systems
149 which have it (at least not Sun). */
150 gdb_printf (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
152 gdb_printf (stream
, "c_cc: ");
153 for (i
= 0; i
< NCCS
; i
+= 1)
154 gdb_printf (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
155 gdb_printf (stream
, "\n");
158 /* Wait for the output to drain away, as opposed to flushing
162 hardwire_drain_output (struct serial
*scb
)
164 /* Ignore SIGTTOU which may occur during the drain. */
165 scoped_ignore_sigttou ignore_sigttou
;
167 return tcdrain (scb
->fd
);
171 hardwire_flush_output (struct serial
*scb
)
173 return tcflush (scb
->fd
, TCOFLUSH
);
177 hardwire_flush_input (struct serial
*scb
)
179 ser_base_flush_input (scb
);
181 return tcflush (scb
->fd
, TCIFLUSH
);
185 hardwire_send_break (struct serial
*scb
)
187 if (tcsendbreak (scb
->fd
, 0) == -1)
188 perror_with_name ("sending break");
192 hardwire_raw (struct serial
*scb
)
194 struct hardwire_ttystate state
;
196 if (get_tty_state (scb
, &state
))
197 gdb_printf (gdb_stderr
, "get_tty_state failed: %s\n",
198 safe_strerror (errno
));
200 state
.termios
.c_iflag
= 0;
201 state
.termios
.c_oflag
= 0;
202 state
.termios
.c_lflag
= 0;
203 state
.termios
.c_cflag
&= ~CSIZE
;
204 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
206 /* h/w flow control. */
208 state
.termios
.c_cflag
|= CRTSCTS
;
210 state
.termios
.c_cflag
&= ~CRTSCTS
;
213 state
.termios
.c_cflag
|= CRTS_IFLOW
;
215 state
.termios
.c_cflag
&= ~CRTS_IFLOW
;
218 state
.termios
.c_cc
[VMIN
] = 0;
219 state
.termios
.c_cc
[VTIME
] = 0;
221 if (set_tty_state (scb
, &state
))
222 gdb_printf (gdb_stderr
, "set_tty_state failed: %s\n",
223 safe_strerror (errno
));
234 /* Translate baud rates from integers to damn B_codes. Unix should
235 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
401 rate_to_code (int rate
)
405 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
407 /* test for perfect macth. */
408 if (rate
== baudtab
[i
].rate
)
409 return baudtab
[i
].code
;
412 /* check if it is in between valid values. */
413 if (rate
< baudtab
[i
].rate
)
417 error (_("Invalid baud rate %d. "
418 "Closest values are %d and %d."),
419 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
423 error (_("Invalid baud rate %d. Minimum value is %d."),
424 rate
, baudtab
[0].rate
);
430 /* The requested speed was too large. */
431 error (_("Invalid baud rate %d. Maximum value is %d."),
432 rate
, baudtab
[i
- 1].rate
);
436 hardwire_setbaudrate (struct serial
*scb
, int rate
)
438 struct hardwire_ttystate state
;
439 int baud_code
= rate_to_code (rate
);
441 if (get_tty_state (scb
, &state
))
442 perror_with_name ("could not get tty state");
444 cfsetospeed (&state
.termios
, baud_code
);
445 cfsetispeed (&state
.termios
, baud_code
);
447 if (set_tty_state (scb
, &state
))
448 perror_with_name ("could not set tty state");
452 hardwire_setstopbits (struct serial
*scb
, int num
)
454 struct hardwire_ttystate state
;
457 if (get_tty_state (scb
, &state
))
462 case SERIAL_1_STOPBITS
:
465 case SERIAL_1_AND_A_HALF_STOPBITS
:
466 case SERIAL_2_STOPBITS
:
474 state
.termios
.c_cflag
&= ~CSTOPB
;
476 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
478 return set_tty_state (scb
, &state
);
481 /* Implement the "setparity" serial_ops callback. */
484 hardwire_setparity (struct serial
*scb
, int parity
)
486 struct hardwire_ttystate state
;
489 if (get_tty_state (scb
, &state
))
498 newparity
= PARENB
| PARODD
;
504 internal_warning ("Incorrect parity value: %d", parity
);
508 state
.termios
.c_cflag
&= ~(PARENB
| PARODD
);
509 state
.termios
.c_cflag
|= newparity
;
511 return set_tty_state (scb
, &state
);
516 hardwire_close (struct serial
*scb
)
527 /* The hardwire ops. */
529 static const struct serial_ops hardwire_ops
=
537 hardwire_flush_output
,
538 hardwire_flush_input
,
541 hardwire_get_tty_state
,
542 hardwire_copy_tty_state
,
543 hardwire_set_tty_state
,
544 hardwire_print_tty_state
,
545 hardwire_setbaudrate
,
546 hardwire_setstopbits
,
548 hardwire_drain_output
,
554 void _initialize_ser_hardwire ();
556 _initialize_ser_hardwire ()
558 serial_add_interface (&hardwire_ops
);
561 add_setshow_boolean_cmd ("remoteflow", no_class
,
563 Set use of hardware flow control for remote serial I/O."), _("\
564 Show use of hardware flow control for remote serial I/O."), _("\
565 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
566 when debugging using remote targets."),
569 &setlist
, &showlist
);
574 ser_unix_read_prim (struct serial
*scb
, size_t count
)
576 int result
= read (scb
->fd
, scb
->buf
, count
);
577 if (result
== -1 && errno
!= EINTR
)
578 perror_with_name ("error while reading");
583 ser_unix_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
585 int result
= write (scb
->fd
, buf
, len
);
586 if (result
== -1 && errno
!= EINTR
)
587 perror_with_name ("error while writing");