Automatic date update in version.in
[binutils-gdb.git] / gdb / ser-unix.c
blobbc8b087b6eb6ad3886de1bc1b037dc70d677c61d
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/>. */
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 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,
65 struct ui_file *);
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. */
74 static void
75 hardwire_open (struct serial *scb, const char *name)
77 scb->fd = gdb_open_cloexec (name, O_RDWR, 0).release ();
78 if (scb->fd < 0)
79 perror_with_name ("could not open device");
82 static int
83 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
85 if (tcgetattr (scb->fd, &state->termios) < 0)
86 return -1;
88 return 0;
91 static int
92 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
94 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
95 return -1;
97 return 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))
107 xfree (state);
108 return NULL;
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;
124 static int
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);
134 static void
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;
140 int i;
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);
148 #if 0
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);
152 #endif
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
160 (discarding) it. */
162 static int
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);
171 static int
172 hardwire_flush_output (struct serial *scb)
174 return tcflush (scb->fd, TCOFLUSH);
177 static int
178 hardwire_flush_input (struct serial *scb)
180 ser_base_flush_input (scb);
182 return tcflush (scb->fd, TCIFLUSH);
185 static void
186 hardwire_send_break (struct serial *scb)
188 if (tcsendbreak (scb->fd, 0) == -1)
189 perror_with_name ("sending break");
192 static void
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;
206 #ifdef CRTSCTS
207 /* h/w flow control. */
208 if (serial_hwflow)
209 state.termios.c_cflag |= CRTSCTS;
210 else
211 state.termios.c_cflag &= ~CRTSCTS;
212 #ifdef CRTS_IFLOW
213 if (serial_hwflow)
214 state.termios.c_cflag |= CRTS_IFLOW;
215 else
216 state.termios.c_cflag &= ~CRTS_IFLOW;
217 #endif
218 #endif
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));
227 #ifndef B19200
228 #define B19200 EXTA
229 #endif
231 #ifndef B38400
232 #define B38400 EXTB
233 #endif
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. */
238 static struct
240 int rate;
241 int code;
243 baudtab[] =
246 50, B50
250 75, B75
254 110, B110
258 134, B134
262 150, B150
266 200, B200
270 300, B300
274 600, B600
278 1200, B1200
282 1800, B1800
286 2400, B2400
290 4800, B4800
294 9600, B9600
298 19200, B19200
302 38400, B38400
305 #ifdef B57600
307 57600, B57600
310 #endif
311 #ifdef B115200
313 115200, B115200
316 #endif
317 #ifdef B230400
319 230400, B230400
322 #endif
323 #ifdef B460800
325 460800, B460800
328 #endif
329 #ifdef B500000
331 500000, B500000
334 #endif
335 #ifdef B576000
337 576000, B576000
340 #endif
341 #ifdef B921600
343 921600, B921600
346 #endif
347 #ifdef B1000000
349 1000000, B1000000
352 #endif
353 #ifdef B1152000
355 1152000, B1152000
358 #endif
359 #ifdef B1500000
361 1500000, B1500000
364 #endif
365 #ifdef B2000000
367 2000000, B2000000
370 #endif
371 #ifdef B2500000
373 2500000, B2500000
376 #endif
377 #ifdef B3000000
379 3000000, B3000000
382 #endif
383 #ifdef B3500000
385 3500000, B3500000
388 #endif
389 #ifdef B4000000
391 4000000, B4000000
394 #endif
396 -1, -1
401 static int
402 rate_to_code (int rate)
404 int i;
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;
411 else
413 /* check if it is in between valid values. */
414 if (rate < baudtab[i].rate)
416 if (i)
418 error (_("Invalid baud rate %d. "
419 "Closest values are %d and %d."),
420 rate, baudtab[i - 1].rate, baudtab[i].rate);
422 else
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);
436 static void
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");
452 static int
453 hardwire_setstopbits (struct serial *scb, int num)
455 struct hardwire_ttystate state;
456 int newbit;
458 if (get_tty_state (scb, &state))
459 return -1;
461 switch (num)
463 case SERIAL_1_STOPBITS:
464 newbit = 0;
465 break;
466 case SERIAL_1_AND_A_HALF_STOPBITS:
467 case SERIAL_2_STOPBITS:
468 newbit = 1;
469 break;
470 default:
471 return 1;
474 if (!newbit)
475 state.termios.c_cflag &= ~CSTOPB;
476 else
477 state.termios.c_cflag |= CSTOPB; /* two bits */
479 return set_tty_state (scb, &state);
482 /* Implement the "setparity" serial_ops callback. */
484 static int
485 hardwire_setparity (struct serial *scb, int parity)
487 struct hardwire_ttystate state;
488 int newparity = 0;
490 if (get_tty_state (scb, &state))
491 return -1;
493 switch (parity)
495 case GDBPARITY_NONE:
496 newparity = 0;
497 break;
498 case GDBPARITY_ODD:
499 newparity = PARENB | PARODD;
500 break;
501 case GDBPARITY_EVEN:
502 newparity = PARENB;
503 break;
504 default:
505 internal_warning ("Incorrect parity value: %d", parity);
506 return -1;
509 state.termios.c_cflag &= ~(PARENB | PARODD);
510 state.termios.c_cflag |= newparity;
512 return set_tty_state (scb, &state);
516 static void
517 hardwire_close (struct serial *scb)
519 if (scb->fd < 0)
520 return;
522 close (scb->fd);
523 scb->fd = -1;
528 /* The hardwire ops. */
530 static const struct serial_ops hardwire_ops =
532 "hardwire",
533 hardwire_open,
534 hardwire_close,
535 NULL,
536 ser_base_readchar,
537 ser_base_write,
538 hardwire_flush_output,
539 hardwire_flush_input,
540 hardwire_send_break,
541 hardwire_raw,
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,
548 hardwire_setparity,
549 hardwire_drain_output,
550 ser_base_async,
551 ser_unix_read_prim,
552 ser_unix_write_prim
555 void _initialize_ser_hardwire ();
556 void
557 _initialize_ser_hardwire ()
559 serial_add_interface (&hardwire_ops);
561 #ifdef CRTSCTS
562 add_setshow_boolean_cmd ("remoteflow", no_class,
563 &serial_hwflow, _("\
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."),
568 NULL,
569 show_serial_hwflow,
570 &setlist, &showlist);
571 #endif
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");
580 return result;
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");
589 return result;