]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ser-unix.c
Remove unnecessary function prototypes.
[thirdparty/binutils-gdb.git] / gdb / ser-unix.c
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
2
3 Copyright (C) 1992-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "serial.h"
22 #include "ser-base.h"
23 #include "ser-unix.h"
24
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include "terminal.h"
28 #include <sys/socket.h>
29 #include "gdb_sys_time.h"
30
31 #include "gdb_select.h"
32 #include "gdbcmd.h"
33 #include "filestuff.h"
34 #include "gdb_termios.h"
35
36 #ifdef HAVE_TERMIOS
37
38 struct hardwire_ttystate
39 {
40 struct termios termios;
41 };
42
43 #ifdef CRTSCTS
44 /* Boolean to explicitly enable or disable h/w flow control. */
45 static int serial_hwflow;
46 static void
47 show_serial_hwflow (struct ui_file *file, int from_tty,
48 struct cmd_list_element *c, const char *value)
49 {
50 fprintf_filtered (file, _("Hardware flow control is %s.\n"), value);
51 }
52 #endif
53
54 #endif /* termios */
55
56 #ifdef HAVE_TERMIO
57
58 /* It is believed that all systems which have added job control to SVR3
59 (e.g. sco) have also added termios. Even if not, trying to figure out
60 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
61 bewildering. So we don't attempt it. */
62
63 struct hardwire_ttystate
64 {
65 struct termio termio;
66 };
67 #endif /* termio */
68
69 #ifdef HAVE_SGTTY
70 struct hardwire_ttystate
71 {
72 struct sgttyb sgttyb;
73 struct tchars tc;
74 struct ltchars ltc;
75 /* Line discipline flags. */
76 int lmode;
77 };
78 #endif /* sgtty */
79
80 static int hardwire_open (struct serial *scb, const char *name);
81 static void hardwire_raw (struct serial *scb);
82 static int rate_to_code (int rate);
83 static int hardwire_setbaudrate (struct serial *scb, int rate);
84 static int hardwire_setparity (struct serial *scb, int parity);
85 static void hardwire_close (struct serial *scb);
86 static int get_tty_state (struct serial *scb,
87 struct hardwire_ttystate * state);
88 static int set_tty_state (struct serial *scb,
89 struct hardwire_ttystate * state);
90 static serial_ttystate hardwire_get_tty_state (struct serial *scb);
91 static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
92 static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
93 serial_ttystate);
94 static void hardwire_print_tty_state (struct serial *, serial_ttystate,
95 struct ui_file *);
96 static int hardwire_drain_output (struct serial *);
97 static int hardwire_flush_output (struct serial *);
98 static int hardwire_flush_input (struct serial *);
99 static int hardwire_send_break (struct serial *);
100 static int hardwire_setstopbits (struct serial *, int);
101
102 /* Open up a real live device for serial I/O. */
103
104 static int
105 hardwire_open (struct serial *scb, const char *name)
106 {
107 scb->fd = gdb_open_cloexec (name, O_RDWR, 0);
108 if (scb->fd < 0)
109 return -1;
110
111 return 0;
112 }
113
114 static int
115 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
116 {
117 #ifdef HAVE_TERMIOS
118 if (tcgetattr (scb->fd, &state->termios) < 0)
119 return -1;
120
121 return 0;
122 #endif
123
124 #ifdef HAVE_TERMIO
125 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
126 return -1;
127 return 0;
128 #endif
129
130 #ifdef HAVE_SGTTY
131 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
132 return -1;
133 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
134 return -1;
135 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
136 return -1;
137 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
138 return -1;
139
140 return 0;
141 #endif
142 }
143
144 static int
145 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
146 {
147 #ifdef HAVE_TERMIOS
148 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
149 return -1;
150
151 return 0;
152 #endif
153
154 #ifdef HAVE_TERMIO
155 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
156 return -1;
157 return 0;
158 #endif
159
160 #ifdef HAVE_SGTTY
161 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
162 return -1;
163 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
164 return -1;
165 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
166 return -1;
167 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
168 return -1;
169
170 return 0;
171 #endif
172 }
173
174 static serial_ttystate
175 hardwire_get_tty_state (struct serial *scb)
176 {
177 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
178
179 if (get_tty_state (scb, state))
180 {
181 xfree (state);
182 return NULL;
183 }
184
185 return (serial_ttystate) state;
186 }
187
188 static serial_ttystate
189 hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
190 {
191 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
192
193 *state = *(struct hardwire_ttystate *) ttystate;
194
195 return (serial_ttystate) state;
196 }
197
198 static int
199 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
200 {
201 struct hardwire_ttystate *state;
202
203 state = (struct hardwire_ttystate *) ttystate;
204
205 return set_tty_state (scb, state);
206 }
207
208 static int
209 hardwire_noflush_set_tty_state (struct serial *scb,
210 serial_ttystate new_ttystate,
211 serial_ttystate old_ttystate)
212 {
213 struct hardwire_ttystate new_state;
214 #ifdef HAVE_SGTTY
215 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
216 #endif
217
218 new_state = *(struct hardwire_ttystate *) new_ttystate;
219
220 /* Don't change in or out of raw mode; we don't want to flush input.
221 termio and termios have no such restriction; for them flushing input
222 is separate from setting the attributes. */
223
224 #ifdef HAVE_SGTTY
225 if (state->sgttyb.sg_flags & RAW)
226 new_state.sgttyb.sg_flags |= RAW;
227 else
228 new_state.sgttyb.sg_flags &= ~RAW;
229
230 /* I'm not sure whether this is necessary; the manpage just mentions
231 RAW not CBREAK. */
232 if (state->sgttyb.sg_flags & CBREAK)
233 new_state.sgttyb.sg_flags |= CBREAK;
234 else
235 new_state.sgttyb.sg_flags &= ~CBREAK;
236 #endif
237
238 return set_tty_state (scb, &new_state);
239 }
240
241 static void
242 hardwire_print_tty_state (struct serial *scb,
243 serial_ttystate ttystate,
244 struct ui_file *stream)
245 {
246 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
247 int i;
248
249 #ifdef HAVE_TERMIOS
250 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
251 (int) state->termios.c_iflag,
252 (int) state->termios.c_oflag);
253 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
254 (int) state->termios.c_cflag,
255 (int) state->termios.c_lflag);
256 #if 0
257 /* This not in POSIX, and is not really documented by those systems
258 which have it (at least not Sun). */
259 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
260 #endif
261 fprintf_filtered (stream, "c_cc: ");
262 for (i = 0; i < NCCS; i += 1)
263 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
264 fprintf_filtered (stream, "\n");
265 #endif
266
267 #ifdef HAVE_TERMIO
268 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
269 state->termio.c_iflag, state->termio.c_oflag);
270 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
271 state->termio.c_cflag, state->termio.c_lflag,
272 state->termio.c_line);
273 fprintf_filtered (stream, "c_cc: ");
274 for (i = 0; i < NCC; i += 1)
275 fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
276 fprintf_filtered (stream, "\n");
277 #endif
278
279 #ifdef HAVE_SGTTY
280 fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
281 state->sgttyb.sg_flags);
282
283 fprintf_filtered (stream, "tchars: ");
284 for (i = 0; i < (int) sizeof (struct tchars); i++)
285 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
286 fprintf_filtered (stream, "\n");
287
288 fprintf_filtered (stream, "ltchars: ");
289 for (i = 0; i < (int) sizeof (struct ltchars); i++)
290 fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
291 fprintf_filtered (stream, "\n");
292
293 fprintf_filtered (stream, "lmode: 0x%x\n", state->lmode);
294 #endif
295 }
296
297 /* Wait for the output to drain away, as opposed to flushing
298 (discarding) it. */
299
300 static int
301 hardwire_drain_output (struct serial *scb)
302 {
303 #ifdef HAVE_TERMIOS
304 return tcdrain (scb->fd);
305 #endif
306
307 #ifdef HAVE_TERMIO
308 return ioctl (scb->fd, TCSBRK, 1);
309 #endif
310
311 #ifdef HAVE_SGTTY
312 /* Get the current state and then restore it using TIOCSETP,
313 which should cause the output to drain and pending input
314 to be discarded. */
315 {
316 struct hardwire_ttystate state;
317
318 if (get_tty_state (scb, &state))
319 {
320 return (-1);
321 }
322 else
323 {
324 return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
325 }
326 }
327 #endif
328 }
329
330 static int
331 hardwire_flush_output (struct serial *scb)
332 {
333 #ifdef HAVE_TERMIOS
334 return tcflush (scb->fd, TCOFLUSH);
335 #endif
336
337 #ifdef HAVE_TERMIO
338 return ioctl (scb->fd, TCFLSH, 1);
339 #endif
340
341 #ifdef HAVE_SGTTY
342 /* This flushes both input and output, but we can't do better. */
343 return ioctl (scb->fd, TIOCFLUSH, 0);
344 #endif
345 }
346
347 static int
348 hardwire_flush_input (struct serial *scb)
349 {
350 ser_base_flush_input (scb);
351
352 #ifdef HAVE_TERMIOS
353 return tcflush (scb->fd, TCIFLUSH);
354 #endif
355
356 #ifdef HAVE_TERMIO
357 return ioctl (scb->fd, TCFLSH, 0);
358 #endif
359
360 #ifdef HAVE_SGTTY
361 /* This flushes both input and output, but we can't do better. */
362 return ioctl (scb->fd, TIOCFLUSH, 0);
363 #endif
364 }
365
366 static int
367 hardwire_send_break (struct serial *scb)
368 {
369 #ifdef HAVE_TERMIOS
370 return tcsendbreak (scb->fd, 0);
371 #endif
372
373 #ifdef HAVE_TERMIO
374 return ioctl (scb->fd, TCSBRK, 0);
375 #endif
376
377 #ifdef HAVE_SGTTY
378 {
379 int status;
380
381 status = ioctl (scb->fd, TIOCSBRK, 0);
382
383 /* Can't use usleep; it doesn't exist in BSD 4.2. */
384 /* Note that if this gdb_select() is interrupted by a signal it will not
385 wait the full length of time. I think that is OK. */
386 gdb_usleep (250000);
387 status = ioctl (scb->fd, TIOCCBRK, 0);
388 return status;
389 }
390 #endif
391 }
392
393 static void
394 hardwire_raw (struct serial *scb)
395 {
396 struct hardwire_ttystate state;
397
398 if (get_tty_state (scb, &state))
399 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
400 safe_strerror (errno));
401
402 #ifdef HAVE_TERMIOS
403 state.termios.c_iflag = 0;
404 state.termios.c_oflag = 0;
405 state.termios.c_lflag = 0;
406 state.termios.c_cflag &= ~CSIZE;
407 state.termios.c_cflag |= CLOCAL | CS8;
408 #ifdef CRTSCTS
409 /* h/w flow control. */
410 if (serial_hwflow)
411 state.termios.c_cflag |= CRTSCTS;
412 else
413 state.termios.c_cflag &= ~CRTSCTS;
414 #ifdef CRTS_IFLOW
415 if (serial_hwflow)
416 state.termios.c_cflag |= CRTS_IFLOW;
417 else
418 state.termios.c_cflag &= ~CRTS_IFLOW;
419 #endif
420 #endif
421 state.termios.c_cc[VMIN] = 0;
422 state.termios.c_cc[VTIME] = 0;
423 #endif
424
425 #ifdef HAVE_TERMIO
426 state.termio.c_iflag = 0;
427 state.termio.c_oflag = 0;
428 state.termio.c_lflag = 0;
429 state.termio.c_cflag &= ~CSIZE;
430 state.termio.c_cflag |= CLOCAL | CS8;
431 state.termio.c_cc[VMIN] = 0;
432 state.termio.c_cc[VTIME] = 0;
433 #endif
434
435 #ifdef HAVE_SGTTY
436 state.sgttyb.sg_flags |= RAW | ANYP;
437 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
438 #endif
439
440 if (set_tty_state (scb, &state))
441 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
442 safe_strerror (errno));
443 }
444
445 #ifndef B19200
446 #define B19200 EXTA
447 #endif
448
449 #ifndef B38400
450 #define B38400 EXTB
451 #endif
452
453 /* Translate baud rates from integers to damn B_codes. Unix should
454 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
455
456 static struct
457 {
458 int rate;
459 int code;
460 }
461 baudtab[] =
462 {
463 {
464 50, B50
465 }
466 ,
467 {
468 75, B75
469 }
470 ,
471 {
472 110, B110
473 }
474 ,
475 {
476 134, B134
477 }
478 ,
479 {
480 150, B150
481 }
482 ,
483 {
484 200, B200
485 }
486 ,
487 {
488 300, B300
489 }
490 ,
491 {
492 600, B600
493 }
494 ,
495 {
496 1200, B1200
497 }
498 ,
499 {
500 1800, B1800
501 }
502 ,
503 {
504 2400, B2400
505 }
506 ,
507 {
508 4800, B4800
509 }
510 ,
511 {
512 9600, B9600
513 }
514 ,
515 {
516 19200, B19200
517 }
518 ,
519 {
520 38400, B38400
521 }
522 ,
523 #ifdef B57600
524 {
525 57600, B57600
526 }
527 ,
528 #endif
529 #ifdef B115200
530 {
531 115200, B115200
532 }
533 ,
534 #endif
535 #ifdef B230400
536 {
537 230400, B230400
538 }
539 ,
540 #endif
541 #ifdef B460800
542 {
543 460800, B460800
544 }
545 ,
546 #endif
547 {
548 -1, -1
549 }
550 ,
551 };
552
553 static int
554 rate_to_code (int rate)
555 {
556 int i;
557
558 for (i = 0; baudtab[i].rate != -1; i++)
559 {
560 /* test for perfect macth. */
561 if (rate == baudtab[i].rate)
562 return baudtab[i].code;
563 else
564 {
565 /* check if it is in between valid values. */
566 if (rate < baudtab[i].rate)
567 {
568 if (i)
569 {
570 warning (_("Invalid baud rate %d. "
571 "Closest values are %d and %d."),
572 rate, baudtab[i - 1].rate, baudtab[i].rate);
573 }
574 else
575 {
576 warning (_("Invalid baud rate %d. Minimum value is %d."),
577 rate, baudtab[0].rate);
578 }
579 return -1;
580 }
581 }
582 }
583
584 /* The requested speed was too large. */
585 warning (_("Invalid baud rate %d. Maximum value is %d."),
586 rate, baudtab[i - 1].rate);
587 return -1;
588 }
589
590 static int
591 hardwire_setbaudrate (struct serial *scb, int rate)
592 {
593 struct hardwire_ttystate state;
594 int baud_code = rate_to_code (rate);
595
596 if (baud_code < 0)
597 {
598 /* The baud rate was not valid.
599 A warning has already been issued. */
600 errno = EINVAL;
601 return -1;
602 }
603
604 if (get_tty_state (scb, &state))
605 return -1;
606
607 #ifdef HAVE_TERMIOS
608 cfsetospeed (&state.termios, baud_code);
609 cfsetispeed (&state.termios, baud_code);
610 #endif
611
612 #ifdef HAVE_TERMIO
613 #ifndef CIBAUD
614 #define CIBAUD CBAUD
615 #endif
616
617 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
618 state.termio.c_cflag |= baud_code;
619 #endif
620
621 #ifdef HAVE_SGTTY
622 state.sgttyb.sg_ispeed = baud_code;
623 state.sgttyb.sg_ospeed = baud_code;
624 #endif
625
626 return set_tty_state (scb, &state);
627 }
628
629 static int
630 hardwire_setstopbits (struct serial *scb, int num)
631 {
632 struct hardwire_ttystate state;
633 int newbit;
634
635 if (get_tty_state (scb, &state))
636 return -1;
637
638 switch (num)
639 {
640 case SERIAL_1_STOPBITS:
641 newbit = 0;
642 break;
643 case SERIAL_1_AND_A_HALF_STOPBITS:
644 case SERIAL_2_STOPBITS:
645 newbit = 1;
646 break;
647 default:
648 return 1;
649 }
650
651 #ifdef HAVE_TERMIOS
652 if (!newbit)
653 state.termios.c_cflag &= ~CSTOPB;
654 else
655 state.termios.c_cflag |= CSTOPB; /* two bits */
656 #endif
657
658 #ifdef HAVE_TERMIO
659 if (!newbit)
660 state.termio.c_cflag &= ~CSTOPB;
661 else
662 state.termio.c_cflag |= CSTOPB; /* two bits */
663 #endif
664
665 #ifdef HAVE_SGTTY
666 return 0; /* sgtty doesn't support this */
667 #endif
668
669 return set_tty_state (scb, &state);
670 }
671
672 /* Implement the "setparity" serial_ops callback. */
673
674 static int
675 hardwire_setparity (struct serial *scb, int parity)
676 {
677 struct hardwire_ttystate state;
678 int newparity = 0;
679
680 if (get_tty_state (scb, &state))
681 return -1;
682
683 switch (parity)
684 {
685 case GDBPARITY_NONE:
686 newparity = 0;
687 break;
688 case GDBPARITY_ODD:
689 newparity = PARENB | PARODD;
690 break;
691 case GDBPARITY_EVEN:
692 newparity = PARENB;
693 break;
694 default:
695 internal_warning (__FILE__, __LINE__,
696 "Incorrect parity value: %d", parity);
697 return -1;
698 }
699
700 #ifdef HAVE_TERMIOS
701 state.termios.c_cflag &= ~(PARENB | PARODD);
702 state.termios.c_cflag |= newparity;
703 #endif
704
705 #ifdef HAVE_TERMIO
706 state.termio.c_cflag &= ~(PARENB | PARODD);
707 state.termio.c_cflag |= newparity;
708 #endif
709
710 #ifdef HAVE_SGTTY
711 return 0; /* sgtty doesn't support this */
712 #endif
713 return set_tty_state (scb, &state);
714 }
715
716
717 static void
718 hardwire_close (struct serial *scb)
719 {
720 if (scb->fd < 0)
721 return;
722
723 close (scb->fd);
724 scb->fd = -1;
725 }
726 \f
727 \f
728
729 /* The hardwire ops. */
730
731 static const struct serial_ops hardwire_ops =
732 {
733 "hardwire",
734 hardwire_open,
735 hardwire_close,
736 NULL,
737 ser_base_readchar,
738 ser_base_write,
739 hardwire_flush_output,
740 hardwire_flush_input,
741 hardwire_send_break,
742 hardwire_raw,
743 hardwire_get_tty_state,
744 hardwire_copy_tty_state,
745 hardwire_set_tty_state,
746 hardwire_print_tty_state,
747 hardwire_noflush_set_tty_state,
748 hardwire_setbaudrate,
749 hardwire_setstopbits,
750 hardwire_setparity,
751 hardwire_drain_output,
752 ser_base_async,
753 ser_unix_read_prim,
754 ser_unix_write_prim
755 };
756
757 void
758 _initialize_ser_hardwire (void)
759 {
760 serial_add_interface (&hardwire_ops);
761
762 #ifdef HAVE_TERMIOS
763 #ifdef CRTSCTS
764 add_setshow_boolean_cmd ("remoteflow", no_class,
765 &serial_hwflow, _("\
766 Set use of hardware flow control for remote serial I/O."), _("\
767 Show use of hardware flow control for remote serial I/O."), _("\
768 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
769 when debugging using remote targets."),
770 NULL,
771 show_serial_hwflow,
772 &setlist, &showlist);
773 #endif
774 #endif
775 }
776
777 int
778 ser_unix_read_prim (struct serial *scb, size_t count)
779 {
780 return read (scb->fd, scb->buf, count);
781 }
782
783 int
784 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
785 {
786 return write (scb->fd, buf, len);
787 }