Remove fake operand handling for extended mnemonics.
[binutils-gdb.git] / gdb / ser-unix.c
blob59a41be30a3080696fd8adb48d1f0d3c339bcfa9
1 /* Serial interface for local (hardwired) serial ports on Un*x like systems
3 Copyright (C) 1992-2018 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 "gdb_sys_time.h"
31 #include "gdb_select.h"
32 #include "gdbcmd.h"
33 #include "filestuff.h"
34 #include <termios.h>
36 struct hardwire_ttystate
38 struct termios termios;
41 #ifdef CRTSCTS
42 /* Boolean to explicitly enable or disable h/w flow control. */
43 static int serial_hwflow;
44 static void
45 show_serial_hwflow (struct ui_file *file, int from_tty,
46 struct cmd_list_element *c, const char *value)
48 fprintf_filtered (file, _("Hardware flow control is %s.\n"), value);
50 #endif
52 static int hardwire_open (struct serial *scb, const char *name);
53 static void hardwire_raw (struct serial *scb);
54 static int rate_to_code (int rate);
55 static int 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 int 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 int
75 hardwire_open (struct serial *scb, const char *name)
77 scb->fd = gdb_open_cloexec (name, O_RDWR, 0);
78 if (scb->fd < 0)
79 return -1;
81 return 0;
84 static int
85 get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
87 if (tcgetattr (scb->fd, &state->termios) < 0)
88 return -1;
90 return 0;
93 static int
94 set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
96 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
97 return -1;
99 return 0;
102 static serial_ttystate
103 hardwire_get_tty_state (struct serial *scb)
105 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
107 if (get_tty_state (scb, state))
109 xfree (state);
110 return NULL;
113 return (serial_ttystate) state;
116 static serial_ttystate
117 hardwire_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
119 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
121 *state = *(struct hardwire_ttystate *) ttystate;
123 return (serial_ttystate) state;
126 static int
127 hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
129 struct hardwire_ttystate *state;
131 state = (struct hardwire_ttystate *) ttystate;
133 return set_tty_state (scb, state);
136 static void
137 hardwire_print_tty_state (struct serial *scb,
138 serial_ttystate ttystate,
139 struct ui_file *stream)
141 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
142 int i;
144 fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
145 (int) state->termios.c_iflag,
146 (int) state->termios.c_oflag);
147 fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
148 (int) state->termios.c_cflag,
149 (int) state->termios.c_lflag);
150 #if 0
151 /* This not in POSIX, and is not really documented by those systems
152 which have it (at least not Sun). */
153 fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
154 #endif
155 fprintf_filtered (stream, "c_cc: ");
156 for (i = 0; i < NCCS; i += 1)
157 fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
158 fprintf_filtered (stream, "\n");
161 /* Wait for the output to drain away, as opposed to flushing
162 (discarding) it. */
164 static int
165 hardwire_drain_output (struct serial *scb)
167 return tcdrain (scb->fd);
170 static int
171 hardwire_flush_output (struct serial *scb)
173 return tcflush (scb->fd, TCOFLUSH);
176 static int
177 hardwire_flush_input (struct serial *scb)
179 ser_base_flush_input (scb);
181 return tcflush (scb->fd, TCIFLUSH);
184 static int
185 hardwire_send_break (struct serial *scb)
187 return tcsendbreak (scb->fd, 0);
190 static void
191 hardwire_raw (struct serial *scb)
193 struct hardwire_ttystate state;
195 if (get_tty_state (scb, &state))
196 fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n",
197 safe_strerror (errno));
199 state.termios.c_iflag = 0;
200 state.termios.c_oflag = 0;
201 state.termios.c_lflag = 0;
202 state.termios.c_cflag &= ~CSIZE;
203 state.termios.c_cflag |= CLOCAL | CS8;
204 #ifdef CRTSCTS
205 /* h/w flow control. */
206 if (serial_hwflow)
207 state.termios.c_cflag |= CRTSCTS;
208 else
209 state.termios.c_cflag &= ~CRTSCTS;
210 #ifdef CRTS_IFLOW
211 if (serial_hwflow)
212 state.termios.c_cflag |= CRTS_IFLOW;
213 else
214 state.termios.c_cflag &= ~CRTS_IFLOW;
215 #endif
216 #endif
217 state.termios.c_cc[VMIN] = 0;
218 state.termios.c_cc[VTIME] = 0;
220 if (set_tty_state (scb, &state))
221 fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n",
222 safe_strerror (errno));
225 #ifndef B19200
226 #define B19200 EXTA
227 #endif
229 #ifndef B38400
230 #define B38400 EXTB
231 #endif
233 /* Translate baud rates from integers to damn B_codes. Unix should
234 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
236 static struct
238 int rate;
239 int code;
241 baudtab[] =
244 50, B50
248 75, B75
252 110, B110
256 134, B134
260 150, B150
264 200, B200
268 300, B300
272 600, B600
276 1200, B1200
280 1800, B1800
284 2400, B2400
288 4800, B4800
292 9600, B9600
296 19200, B19200
300 38400, B38400
303 #ifdef B57600
305 57600, B57600
308 #endif
309 #ifdef B115200
311 115200, B115200
314 #endif
315 #ifdef B230400
317 230400, B230400
320 #endif
321 #ifdef B460800
323 460800, B460800
326 #endif
328 -1, -1
333 static int
334 rate_to_code (int rate)
336 int i;
338 for (i = 0; baudtab[i].rate != -1; i++)
340 /* test for perfect macth. */
341 if (rate == baudtab[i].rate)
342 return baudtab[i].code;
343 else
345 /* check if it is in between valid values. */
346 if (rate < baudtab[i].rate)
348 if (i)
350 warning (_("Invalid baud rate %d. "
351 "Closest values are %d and %d."),
352 rate, baudtab[i - 1].rate, baudtab[i].rate);
354 else
356 warning (_("Invalid baud rate %d. Minimum value is %d."),
357 rate, baudtab[0].rate);
359 return -1;
364 /* The requested speed was too large. */
365 warning (_("Invalid baud rate %d. Maximum value is %d."),
366 rate, baudtab[i - 1].rate);
367 return -1;
370 static int
371 hardwire_setbaudrate (struct serial *scb, int rate)
373 struct hardwire_ttystate state;
374 int baud_code = rate_to_code (rate);
376 if (baud_code < 0)
378 /* The baud rate was not valid.
379 A warning has already been issued. */
380 errno = EINVAL;
381 return -1;
384 if (get_tty_state (scb, &state))
385 return -1;
387 cfsetospeed (&state.termios, baud_code);
388 cfsetispeed (&state.termios, baud_code);
390 return set_tty_state (scb, &state);
393 static int
394 hardwire_setstopbits (struct serial *scb, int num)
396 struct hardwire_ttystate state;
397 int newbit;
399 if (get_tty_state (scb, &state))
400 return -1;
402 switch (num)
404 case SERIAL_1_STOPBITS:
405 newbit = 0;
406 break;
407 case SERIAL_1_AND_A_HALF_STOPBITS:
408 case SERIAL_2_STOPBITS:
409 newbit = 1;
410 break;
411 default:
412 return 1;
415 if (!newbit)
416 state.termios.c_cflag &= ~CSTOPB;
417 else
418 state.termios.c_cflag |= CSTOPB; /* two bits */
420 return set_tty_state (scb, &state);
423 /* Implement the "setparity" serial_ops callback. */
425 static int
426 hardwire_setparity (struct serial *scb, int parity)
428 struct hardwire_ttystate state;
429 int newparity = 0;
431 if (get_tty_state (scb, &state))
432 return -1;
434 switch (parity)
436 case GDBPARITY_NONE:
437 newparity = 0;
438 break;
439 case GDBPARITY_ODD:
440 newparity = PARENB | PARODD;
441 break;
442 case GDBPARITY_EVEN:
443 newparity = PARENB;
444 break;
445 default:
446 internal_warning (__FILE__, __LINE__,
447 "Incorrect parity value: %d", parity);
448 return -1;
451 state.termios.c_cflag &= ~(PARENB | PARODD);
452 state.termios.c_cflag |= newparity;
454 return set_tty_state (scb, &state);
458 static void
459 hardwire_close (struct serial *scb)
461 if (scb->fd < 0)
462 return;
464 close (scb->fd);
465 scb->fd = -1;
470 /* The hardwire ops. */
472 static const struct serial_ops hardwire_ops =
474 "hardwire",
475 hardwire_open,
476 hardwire_close,
477 NULL,
478 ser_base_readchar,
479 ser_base_write,
480 hardwire_flush_output,
481 hardwire_flush_input,
482 hardwire_send_break,
483 hardwire_raw,
484 hardwire_get_tty_state,
485 hardwire_copy_tty_state,
486 hardwire_set_tty_state,
487 hardwire_print_tty_state,
488 hardwire_setbaudrate,
489 hardwire_setstopbits,
490 hardwire_setparity,
491 hardwire_drain_output,
492 ser_base_async,
493 ser_unix_read_prim,
494 ser_unix_write_prim
497 void
498 _initialize_ser_hardwire (void)
500 serial_add_interface (&hardwire_ops);
502 #ifdef CRTSCTS
503 add_setshow_boolean_cmd ("remoteflow", no_class,
504 &serial_hwflow, _("\
505 Set use of hardware flow control for remote serial I/O."), _("\
506 Show use of hardware flow control for remote serial I/O."), _("\
507 Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
508 when debugging using remote targets."),
509 NULL,
510 show_serial_hwflow,
511 &setlist, &showlist);
512 #endif
516 ser_unix_read_prim (struct serial *scb, size_t count)
518 return read (scb->fd, scb->buf, count);
522 ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
524 return write (scb->fd, buf, len);