Automatic date update in version.in
[binutils-gdb.git] / gdb / ser-unix.c
blobede2a58308ab93b2c23d2a9cb4bb5c669c81b3ab
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992-2023 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/>. */
20 #include "defs.h"
21 #include "serial.h"
22 #include "ser-base.h"
23 #include "ser-unix.h"
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include "terminal.h"
28 #include <sys/socket.h>
29 #include "gdbsupport/gdb_sys_time.h"
31 #include "gdbsupport/gdb_select.h"
32 #include "gdbcmd.h"
33 #include "gdbsupport/filestuff.h"
34 #include <termios.h>
35 #include "gdbsupport/scoped_ignore_sigttou.h"
37 struct hardwire_ttystate
39 struct termios termios;
42 #ifdef CRTSCTS
43 /* Boolean to explicitly enable or disable h/w flow control. */
44 static bool serial_hwflow;
45 static void
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);
51 #endif
53 static int hardwire_open (struct serial *scb, const char *name);
54 static void hardwire_raw (struct serial *scb);
55 static int rate_to_code (int rate);
56 static int hardwire_setbaudrate (struct serial *scb, int rate);
57 static int hardwire_setparity (struct serial *scb, int parity);
58 static void hardwire_close (struct serial *scb);
59 static int get_tty_state (struct serial *scb,
60 struct hardwire_ttystate * state);
61 static int set_tty_state (struct serial *scb,
62 struct hardwire_ttystate * state);
63 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
64 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
65 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
66 struct ui_file *);
67 static int hardwire_drain_output (struct serial *);
68 static int hardwire_flush_output (struct serial *);
69 static int hardwire_flush_input (struct serial *);
70 static int hardwire_send_break (struct serial *);
71 static int hardwire_setstopbits (struct serial *, int);
73 /* Open up a real live device for serial I/O. */
75 static int
76 hardwire_open (struct serial *scb, const char *name)
78 scb->fd = gdb_open_cloexec (name, O_RDWR, 0).release ();
79 if (scb->fd < 0)
80 return -1;
82 return 0;
85 static int
86 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
88 if (tcgetattr (scb->fd, &state->termios) < 0)
89 return -1;
91 return 0;
94 static int
95 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
97 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
98 return -1;
100 return 0;
103 static serial_ttystate
104 hardwire_get_tty_state (struct serial *scb)
106 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
108 if (get_tty_state (scb, state))
110 xfree (state);
111 return NULL;
114 return (serial_ttystate) state;
117 static serial_ttystate
118 hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
120 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
122 *state = *(struct hardwire_ttystate *) ttystate;
124 return (serial_ttystate) state;
127 static int
128 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
130 struct hardwire_ttystate *state;
132 state = (struct hardwire_ttystate *) ttystate;
134 return set_tty_state (scb, state);
137 static void
138 hardwire_print_tty_state (struct serial *scb,
139 serial_ttystate ttystate,
140 struct ui_file *stream)
142 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
143 int i;
145 gdb_printf (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
146 (int) state->termios.c_iflag,
147 (int) state->termios.c_oflag);
148 gdb_printf (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
149 (int) state->termios.c_cflag,
150 (int) state->termios.c_lflag);
151 #if 0
152 /* This not in POSIX, and is not really documented by those systems
153 which have it (at least not Sun). */
154 gdb_printf (stream, "c_line = 0x%x.\n", state->termios.c_line);
155 #endif
156 gdb_printf (stream, "c_cc: ");
157 for (i = 0; i < NCCS; i += 1)
158 gdb_printf (stream, "0x%x ", state->termios.c_cc[i]);
159 gdb_printf (stream, "\n");
162 /* Wait for the output to drain away, as opposed to flushing
163 (discarding) it. */
165 static int
166 hardwire_drain_output (struct serial *scb)
168 /* Ignore SIGTTOU which may occur during the drain. */
169 scoped_ignore_sigttou ignore_sigttou;
171 return tcdrain (scb->fd);
174 static int
175 hardwire_flush_output (struct serial *scb)
177 return tcflush (scb->fd, TCOFLUSH);
180 static int
181 hardwire_flush_input (struct serial *scb)
183 ser_base_flush_input (scb);
185 return tcflush (scb->fd, TCIFLUSH);
188 static int
189 hardwire_send_break (struct serial *scb)
191 return tcsendbreak (scb->fd, 0);
194 static void
195 hardwire_raw (struct serial *scb)
197 struct hardwire_ttystate state;
199 if (get_tty_state (scb, &state))
200 gdb_printf (gdb_stderr, "get_tty_state failed: %s\n",
201 safe_strerror (errno));
203 state.termios.c_iflag = 0;
204 state.termios.c_oflag = 0;
205 state.termios.c_lflag = 0;
206 state.termios.c_cflag &= ~CSIZE;
207 state.termios.c_cflag |= CLOCAL | CS8;
208 #ifdef CRTSCTS
209 /* h/w flow control. */
210 if (serial_hwflow)
211 state.termios.c_cflag |= CRTSCTS;
212 else
213 state.termios.c_cflag &= ~CRTSCTS;
214 #ifdef CRTS_IFLOW
215 if (serial_hwflow)
216 state.termios.c_cflag |= CRTS_IFLOW;
217 else
218 state.termios.c_cflag &= ~CRTS_IFLOW;
219 #endif
220 #endif
221 state.termios.c_cc[VMIN] = 0;
222 state.termios.c_cc[VTIME] = 0;
224 if (set_tty_state (scb, &state))
225 gdb_printf (gdb_stderr, "set_tty_state failed: %s\n",
226 safe_strerror (errno));
229 #ifndef B19200
230 #define B19200 EXTA
231 #endif
233 #ifndef B38400
234 #define B38400 EXTB
235 #endif
237 /* Translate baud rates from integers to damn B_codes. Unix should
238 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
240 static struct
242 int rate;
243 int code;
245 baudtab[] =
248 50, B50
252 75, B75
256 110, B110
260 134, B134
264 150, B150
268 200, B200
272 300, B300
276 600, B600
280 1200, B1200
284 1800, B1800
288 2400, B2400
292 4800, B4800
296 9600, B9600
300 19200, B19200
304 38400, B38400
307 #ifdef B57600
309 57600, B57600
312 #endif
313 #ifdef B115200
315 115200, B115200
318 #endif
319 #ifdef B230400
321 230400, B230400
324 #endif
325 #ifdef B460800
327 460800, B460800
330 #endif
332 -1, -1
337 static int
338 rate_to_code (int rate)
340 int i;
342 for (i = 0; baudtab[i].rate != -1; i++)
344 /* test for perfect macth. */
345 if (rate == baudtab[i].rate)
346 return baudtab[i].code;
347 else
349 /* check if it is in between valid values. */
350 if (rate < baudtab[i].rate)
352 if (i)
354 warning (_("Invalid baud rate %d. "
355 "Closest values are %d and %d."),
356 rate, baudtab[i - 1].rate, baudtab[i].rate);
358 else
360 warning (_("Invalid baud rate %d. Minimum value is %d."),
361 rate, baudtab[0].rate);
363 return -1;
368 /* The requested speed was too large. */
369 warning (_("Invalid baud rate %d. Maximum value is %d."),
370 rate, baudtab[i - 1].rate);
371 return -1;
374 static int
375 hardwire_setbaudrate (struct serial *scb, int rate)
377 struct hardwire_ttystate state;
378 int baud_code = rate_to_code (rate);
380 if (baud_code < 0)
382 /* The baud rate was not valid.
383 A warning has already been issued. */
384 errno = EINVAL;
385 return -1;
388 if (get_tty_state (scb, &state))
389 return -1;
391 cfsetospeed (&state.termios, baud_code);
392 cfsetispeed (&state.termios, baud_code);
394 return set_tty_state (scb, &state);
397 static int
398 hardwire_setstopbits (struct serial *scb, int num)
400 struct hardwire_ttystate state;
401 int newbit;
403 if (get_tty_state (scb, &state))
404 return -1;
406 switch (num)
408 case SERIAL_1_STOPBITS:
409 newbit = 0;
410 break;
411 case SERIAL_1_AND_A_HALF_STOPBITS:
412 case SERIAL_2_STOPBITS:
413 newbit = 1;
414 break;
415 default:
416 return 1;
419 if (!newbit)
420 state.termios.c_cflag &= ~CSTOPB;
421 else
422 state.termios.c_cflag |= CSTOPB; /* two bits */
424 return set_tty_state (scb, &state);
427 /* Implement the "setparity" serial_ops callback. */
429 static int
430 hardwire_setparity (struct serial *scb, int parity)
432 struct hardwire_ttystate state;
433 int newparity = 0;
435 if (get_tty_state (scb, &state))
436 return -1;
438 switch (parity)
440 case GDBPARITY_NONE:
441 newparity = 0;
442 break;
443 case GDBPARITY_ODD:
444 newparity = PARENB | PARODD;
445 break;
446 case GDBPARITY_EVEN:
447 newparity = PARENB;
448 break;
449 default:
450 internal_warning ("Incorrect parity value: %d", parity);
451 return -1;
454 state.termios.c_cflag &= ~(PARENB | PARODD);
455 state.termios.c_cflag |= newparity;
457 return set_tty_state (scb, &state);
461 static void
462 hardwire_close (struct serial *scb)
464 if (scb->fd < 0)
465 return;
467 close (scb->fd);
468 scb->fd = -1;
473 /* The hardwire ops. */
475 static const struct serial_ops hardwire_ops =
477 "hardwire",
478 hardwire_open,
479 hardwire_close,
480 NULL,
481 ser_base_readchar,
482 ser_base_write,
483 hardwire_flush_output,
484 hardwire_flush_input,
485 hardwire_send_break,
486 hardwire_raw,
487 hardwire_get_tty_state,
488 hardwire_copy_tty_state,
489 hardwire_set_tty_state,
490 hardwire_print_tty_state,
491 hardwire_setbaudrate,
492 hardwire_setstopbits,
493 hardwire_setparity,
494 hardwire_drain_output,
495 ser_base_async,
496 ser_unix_read_prim,
497 ser_unix_write_prim
500 void _initialize_ser_hardwire ();
501 void
502 _initialize_ser_hardwire ()
504 serial_add_interface (&hardwire_ops);
506 #ifdef CRTSCTS
507 add_setshow_boolean_cmd ("remoteflow", no_class,
508 &serial_hwflow, _("\
509 Set use of hardware flow control for remote serial I/O."), _("\
510 Show use of hardware flow control for remote serial I/O."), _("\
511 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
512 when debugging using remote targets."),
513 NULL,
514 show_serial_hwflow,
515 &setlist, &showlist);
516 #endif
520 ser_unix_read_prim (struct serial *scb, size_t count)
522 return read (scb->fd, scb->buf, count);
526 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
528 return write (scb->fd, buf, len);