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/>. */
26 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include "gdbsupport/gdb_sys_time.h"
31 #include "gdbsupport/gdb_select.h"
33 #include "gdbsupport/filestuff.h"
35 #include "gdbsupport/scoped_ignore_sigttou.h"
37 struct hardwire_ttystate
39 struct termios termios
;
43 /* Boolean to explicitly enable or disable h/w flow control. */
44 static bool serial_hwflow
;
46 show_serial_hwflow (struct ui_file
*file
, int from_tty
,
47 struct cmd_list_element
*c
, const char *value
)
49 gdb_printf (file
, _("Hardware flow control is %s.\n"), value
);
53 static void hardwire_raw (struct serial
*scb
);
54 static int rate_to_code (int rate
);
55 static void hardwire_setbaudrate (struct serial
*scb
, int rate
);
56 static int hardwire_setparity (struct serial
*scb
, int parity
);
57 static void hardwire_close (struct serial
*scb
);
58 static int get_tty_state (struct serial
*scb
,
59 struct hardwire_ttystate
* state
);
60 static int set_tty_state (struct serial
*scb
,
61 struct hardwire_ttystate
* state
);
62 static serial_ttystate
hardwire_get_tty_state (struct serial
*scb
);
63 static int hardwire_set_tty_state (struct serial
*scb
, serial_ttystate state
);
64 static void hardwire_print_tty_state (struct serial
*, serial_ttystate
,
66 static int hardwire_drain_output (struct serial
*);
67 static int hardwire_flush_output (struct serial
*);
68 static int hardwire_flush_input (struct serial
*);
69 static void hardwire_send_break (struct serial
*);
70 static int hardwire_setstopbits (struct serial
*, int);
72 /* Open up a real live device for serial I/O. */
75 hardwire_open (struct serial
*scb
, const char *name
)
77 scb
->fd
= gdb_open_cloexec (name
, O_RDWR
, 0).release ();
79 perror_with_name ("could not open device");
83 get_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
85 if (tcgetattr (scb
->fd
, &state
->termios
) < 0)
92 set_tty_state (struct serial
*scb
, struct hardwire_ttystate
*state
)
94 if (tcsetattr (scb
->fd
, TCSANOW
, &state
->termios
) < 0)
100 static serial_ttystate
101 hardwire_get_tty_state (struct serial
*scb
)
103 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
105 if (get_tty_state (scb
, state
))
111 return (serial_ttystate
) state
;
114 static serial_ttystate
115 hardwire_copy_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
117 struct hardwire_ttystate
*state
= XNEW (struct hardwire_ttystate
);
119 *state
= *(struct hardwire_ttystate
*) ttystate
;
121 return (serial_ttystate
) state
;
125 hardwire_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
127 struct hardwire_ttystate
*state
;
129 state
= (struct hardwire_ttystate
*) ttystate
;
131 return set_tty_state (scb
, state
);
135 hardwire_print_tty_state (struct serial
*scb
,
136 serial_ttystate ttystate
,
137 struct ui_file
*stream
)
139 struct hardwire_ttystate
*state
= (struct hardwire_ttystate
*) ttystate
;
142 gdb_printf (stream
, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
143 (int) state
->termios
.c_iflag
,
144 (int) state
->termios
.c_oflag
);
145 gdb_printf (stream
, "c_cflag = 0x%x, c_lflag = 0x%x\n",
146 (int) state
->termios
.c_cflag
,
147 (int) state
->termios
.c_lflag
);
149 /* This not in POSIX, and is not really documented by those systems
150 which have it (at least not Sun). */
151 gdb_printf (stream
, "c_line = 0x%x.\n", state
->termios
.c_line
);
153 gdb_printf (stream
, "c_cc: ");
154 for (i
= 0; i
< NCCS
; i
+= 1)
155 gdb_printf (stream
, "0x%x ", state
->termios
.c_cc
[i
]);
156 gdb_printf (stream
, "\n");
159 /* Wait for the output to drain away, as opposed to flushing
163 hardwire_drain_output (struct serial
*scb
)
165 /* Ignore SIGTTOU which may occur during the drain. */
166 scoped_ignore_sigttou ignore_sigttou
;
168 return tcdrain (scb
->fd
);
172 hardwire_flush_output (struct serial
*scb
)
174 return tcflush (scb
->fd
, TCOFLUSH
);
178 hardwire_flush_input (struct serial
*scb
)
180 ser_base_flush_input (scb
);
182 return tcflush (scb
->fd
, TCIFLUSH
);
186 hardwire_send_break (struct serial
*scb
)
188 if (tcsendbreak (scb
->fd
, 0) == -1)
189 perror_with_name ("sending break");
193 hardwire_raw (struct serial
*scb
)
195 struct hardwire_ttystate state
;
197 if (get_tty_state (scb
, &state
))
198 gdb_printf (gdb_stderr
, "get_tty_state failed: %s\n",
199 safe_strerror (errno
));
201 state
.termios
.c_iflag
= 0;
202 state
.termios
.c_oflag
= 0;
203 state
.termios
.c_lflag
= 0;
204 state
.termios
.c_cflag
&= ~CSIZE
;
205 state
.termios
.c_cflag
|= CLOCAL
| CS8
;
207 /* h/w flow control. */
209 state
.termios
.c_cflag
|= CRTSCTS
;
211 state
.termios
.c_cflag
&= ~CRTSCTS
;
214 state
.termios
.c_cflag
|= CRTS_IFLOW
;
216 state
.termios
.c_cflag
&= ~CRTS_IFLOW
;
219 state
.termios
.c_cc
[VMIN
] = 0;
220 state
.termios
.c_cc
[VTIME
] = 0;
222 if (set_tty_state (scb
, &state
))
223 gdb_printf (gdb_stderr
, "set_tty_state failed: %s\n",
224 safe_strerror (errno
));
235 /* Translate baud rates from integers to damn B_codes. Unix should
236 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
402 rate_to_code (int rate
)
406 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
408 /* test for perfect macth. */
409 if (rate
== baudtab
[i
].rate
)
410 return baudtab
[i
].code
;
413 /* check if it is in between valid values. */
414 if (rate
< baudtab
[i
].rate
)
418 error (_("Invalid baud rate %d. "
419 "Closest values are %d and %d."),
420 rate
, baudtab
[i
- 1].rate
, baudtab
[i
].rate
);
424 error (_("Invalid baud rate %d. Minimum value is %d."),
425 rate
, baudtab
[0].rate
);
431 /* The requested speed was too large. */
432 error (_("Invalid baud rate %d. Maximum value is %d."),
433 rate
, baudtab
[i
- 1].rate
);
437 hardwire_setbaudrate (struct serial
*scb
, int rate
)
439 struct hardwire_ttystate state
;
440 int baud_code
= rate_to_code (rate
);
442 if (get_tty_state (scb
, &state
))
443 perror_with_name ("could not get tty state");
445 cfsetospeed (&state
.termios
, baud_code
);
446 cfsetispeed (&state
.termios
, baud_code
);
448 if (set_tty_state (scb
, &state
))
449 perror_with_name ("could not set tty state");
453 hardwire_setstopbits (struct serial
*scb
, int num
)
455 struct hardwire_ttystate state
;
458 if (get_tty_state (scb
, &state
))
463 case SERIAL_1_STOPBITS
:
466 case SERIAL_1_AND_A_HALF_STOPBITS
:
467 case SERIAL_2_STOPBITS
:
475 state
.termios
.c_cflag
&= ~CSTOPB
;
477 state
.termios
.c_cflag
|= CSTOPB
; /* two bits */
479 return set_tty_state (scb
, &state
);
482 /* Implement the "setparity" serial_ops callback. */
485 hardwire_setparity (struct serial
*scb
, int parity
)
487 struct hardwire_ttystate state
;
490 if (get_tty_state (scb
, &state
))
499 newparity
= PARENB
| PARODD
;
505 internal_warning ("Incorrect parity value: %d", parity
);
509 state
.termios
.c_cflag
&= ~(PARENB
| PARODD
);
510 state
.termios
.c_cflag
|= newparity
;
512 return set_tty_state (scb
, &state
);
517 hardwire_close (struct serial
*scb
)
528 /* The hardwire ops. */
530 static const struct serial_ops hardwire_ops
=
538 hardwire_flush_output
,
539 hardwire_flush_input
,
542 hardwire_get_tty_state
,
543 hardwire_copy_tty_state
,
544 hardwire_set_tty_state
,
545 hardwire_print_tty_state
,
546 hardwire_setbaudrate
,
547 hardwire_setstopbits
,
549 hardwire_drain_output
,
555 void _initialize_ser_hardwire ();
557 _initialize_ser_hardwire ()
559 serial_add_interface (&hardwire_ops
);
562 add_setshow_boolean_cmd ("remoteflow", no_class
,
564 Set use of hardware flow control for remote serial I/O."), _("\
565 Show use of hardware flow control for remote serial I/O."), _("\
566 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
567 when debugging using remote targets."),
570 &setlist
, &showlist
);
575 ser_unix_read_prim (struct serial
*scb
, size_t count
)
577 int result
= read (scb
->fd
, scb
->buf
, count
);
578 if (result
== -1 && errno
!= EINTR
)
579 perror_with_name ("error while reading");
584 ser_unix_write_prim (struct serial
*scb
, const void *buf
, size_t len
)
586 int result
= write (scb
->fd
, buf
, len
);
587 if (result
== -1 && errno
!= EINTR
)
588 perror_with_name ("error while writing");