New Georgian translation for the ld sub-directory
[binutils-gdb.git] / gdb / ser-unix.c
blobcdc0cf98b7b12b4d13f72e9cd0c79a0c1e61a047
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
331 #ifdef B500000
333 500000, B500000
336 #endif
337 #ifdef B576000
339 576000, B576000
342 #endif
343 #ifdef B921600
345 921600, B921600
348 #endif
349 #ifdef B1000000
351 1000000, B1000000
354 #endif
355 #ifdef B1152000
357 1152000, B1152000
360 #endif
361 #ifdef B1500000
363 1500000, B1500000
366 #endif
367 #ifdef B2000000
369 2000000, B2000000
372 #endif
373 #ifdef B2500000
375 2500000, B2500000
378 #endif
379 #ifdef B3000000
381 3000000, B3000000
384 #endif
385 #ifdef B3500000
387 3500000, B3500000
390 #endif
391 #ifdef B4000000
393 4000000, B4000000
396 #endif
398 -1, -1
403 static int
404 rate_to_code (int rate)
406 int i;
408 for (i = 0; baudtab[i].rate != -1; i++)
410 /* test for perfect macth. */
411 if (rate == baudtab[i].rate)
412 return baudtab[i].code;
413 else
415 /* check if it is in between valid values. */
416 if (rate < baudtab[i].rate)
418 if (i)
420 warning (_("Invalid baud rate %d. "
421 "Closest values are %d and %d."),
422 rate, baudtab[i - 1].rate, baudtab[i].rate);
424 else
426 warning (_("Invalid baud rate %d. Minimum value is %d."),
427 rate, baudtab[0].rate);
429 return -1;
434 /* The requested speed was too large. */
435 warning (_("Invalid baud rate %d. Maximum value is %d."),
436 rate, baudtab[i - 1].rate);
437 return -1;
440 static int
441 hardwire_setbaudrate (struct serial *scb, int rate)
443 struct hardwire_ttystate state;
444 int baud_code = rate_to_code (rate);
446 if (baud_code < 0)
448 /* The baud rate was not valid.
449 A warning has already been issued. */
450 errno = EINVAL;
451 return -1;
454 if (get_tty_state (scb, &state))
455 return -1;
457 cfsetospeed (&state.termios, baud_code);
458 cfsetispeed (&state.termios, baud_code);
460 return set_tty_state (scb, &state);
463 static int
464 hardwire_setstopbits (struct serial *scb, int num)
466 struct hardwire_ttystate state;
467 int newbit;
469 if (get_tty_state (scb, &state))
470 return -1;
472 switch (num)
474 case SERIAL_1_STOPBITS:
475 newbit = 0;
476 break;
477 case SERIAL_1_AND_A_HALF_STOPBITS:
478 case SERIAL_2_STOPBITS:
479 newbit = 1;
480 break;
481 default:
482 return 1;
485 if (!newbit)
486 state.termios.c_cflag &= ~CSTOPB;
487 else
488 state.termios.c_cflag |= CSTOPB; /* two bits */
490 return set_tty_state (scb, &state);
493 /* Implement the "setparity" serial_ops callback. */
495 static int
496 hardwire_setparity (struct serial *scb, int parity)
498 struct hardwire_ttystate state;
499 int newparity = 0;
501 if (get_tty_state (scb, &state))
502 return -1;
504 switch (parity)
506 case GDBPARITY_NONE:
507 newparity = 0;
508 break;
509 case GDBPARITY_ODD:
510 newparity = PARENB | PARODD;
511 break;
512 case GDBPARITY_EVEN:
513 newparity = PARENB;
514 break;
515 default:
516 internal_warning ("Incorrect parity value: %d", parity);
517 return -1;
520 state.termios.c_cflag &= ~(PARENB | PARODD);
521 state.termios.c_cflag |= newparity;
523 return set_tty_state (scb, &state);
527 static void
528 hardwire_close (struct serial *scb)
530 if (scb->fd < 0)
531 return;
533 close (scb->fd);
534 scb->fd = -1;
539 /* The hardwire ops. */
541 static const struct serial_ops hardwire_ops =
543 "hardwire",
544 hardwire_open,
545 hardwire_close,
546 NULL,
547 ser_base_readchar,
548 ser_base_write,
549 hardwire_flush_output,
550 hardwire_flush_input,
551 hardwire_send_break,
552 hardwire_raw,
553 hardwire_get_tty_state,
554 hardwire_copy_tty_state,
555 hardwire_set_tty_state,
556 hardwire_print_tty_state,
557 hardwire_setbaudrate,
558 hardwire_setstopbits,
559 hardwire_setparity,
560 hardwire_drain_output,
561 ser_base_async,
562 ser_unix_read_prim,
563 ser_unix_write_prim
566 void _initialize_ser_hardwire ();
567 void
568 _initialize_ser_hardwire ()
570 serial_add_interface (&hardwire_ops);
572 #ifdef CRTSCTS
573 add_setshow_boolean_cmd ("remoteflow", no_class,
574 &serial_hwflow, _("\
575 Set use of hardware flow control for remote serial I/O."), _("\
576 Show use of hardware flow control for remote serial I/O."), _("\
577 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
578 when debugging using remote targets."),
579 NULL,
580 show_serial_hwflow,
581 &setlist, &showlist);
582 #endif
586 ser_unix_read_prim (struct serial *scb, size_t count)
588 return read (scb->fd, scb->buf, count);
592 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
594 return write (scb->fd, buf, len);