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