GDB (xrefs)
Loading...
Searching...
No Matches
ser-unix.c
Go to the documentation of this file.
1/* Serial interface for local (hardwired) serial ports on Un*x like systems
2
3 Copyright (C) 1992-2023 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 "gdbsupport/gdb_sys_time.h"
30
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"
36
38 {
40 };
41
42#ifdef CRTSCTS
43/* Boolean to explicitly enable or disable h/w flow control. */
44static bool serial_hwflow;
45static void
46show_serial_hwflow (struct ui_file *file, int from_tty,
47 struct cmd_list_element *c, const char *value)
48{
49 gdb_printf (file, _("Hardware flow control is %s.\n"), value);
50}
51#endif
52
53static int hardwire_open (struct serial *scb, const char *name);
54static void hardwire_raw (struct serial *scb);
55static int rate_to_code (int rate);
56static int hardwire_setbaudrate (struct serial *scb, int rate);
57static int hardwire_setparity (struct serial *scb, int parity);
58static void hardwire_close (struct serial *scb);
59static int get_tty_state (struct serial *scb,
60 struct hardwire_ttystate * state);
61static int set_tty_state (struct serial *scb,
62 struct hardwire_ttystate * state);
64static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
66 struct ui_file *);
67static int hardwire_drain_output (struct serial *);
68static int hardwire_flush_output (struct serial *);
69static int hardwire_flush_input (struct serial *);
70static int hardwire_send_break (struct serial *);
71static int hardwire_setstopbits (struct serial *, int);
72
73/* Open up a real live device for serial I/O. */
74
75static int
76hardwire_open (struct serial *scb, const char *name)
77{
78 scb->fd = gdb_open_cloexec (name, O_RDWR, 0).release ();
79 if (scb->fd < 0)
80 return -1;
81
82 return 0;
83}
84
85static int
86get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
87{
88 if (tcgetattr (scb->fd, &state->termios) < 0)
89 return -1;
90
91 return 0;
92}
93
94static int
95set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
96{
97 if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
98 return -1;
99
100 return 0;
101}
102
103static serial_ttystate
105{
106 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
107
108 if (get_tty_state (scb, state))
109 {
110 xfree (state);
111 return NULL;
112 }
113
114 return (serial_ttystate) state;
115}
116
117static serial_ttystate
119{
120 struct hardwire_ttystate *state = XNEW (struct hardwire_ttystate);
121
122 *state = *(struct hardwire_ttystate *) ttystate;
123
124 return (serial_ttystate) state;
125}
126
127static int
129{
130 struct hardwire_ttystate *state;
131
132 state = (struct hardwire_ttystate *) ttystate;
133
134 return set_tty_state (scb, state);
135}
136
137static void
139 serial_ttystate ttystate,
140 struct ui_file *stream)
141{
142 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
143 int i;
144
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");
160}
161
162/* Wait for the output to drain away, as opposed to flushing
163 (discarding) it. */
164
165static int
167{
168 /* Ignore SIGTTOU which may occur during the drain. */
169 scoped_ignore_sigttou ignore_sigttou;
170
171 return tcdrain (scb->fd);
172}
173
174static int
176{
177 return tcflush (scb->fd, TCOFLUSH);
178}
179
180static int
182{
184
185 return tcflush (scb->fd, TCIFLUSH);
186}
187
188static int
190{
191 return tcsendbreak (scb->fd, 0);
192}
193
194static void
195hardwire_raw (struct serial *scb)
196{
197 struct hardwire_ttystate state;
198
199 if (get_tty_state (scb, &state))
200 gdb_printf (gdb_stderr, "get_tty_state failed: %s\n",
201 safe_strerror (errno));
202
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;
223
224 if (set_tty_state (scb, &state))
225 gdb_printf (gdb_stderr, "set_tty_state failed: %s\n",
226 safe_strerror (errno));
227}
228
229#ifndef B19200
230#define B19200 EXTA
231#endif
232
233#ifndef B38400
234#define B38400 EXTB
235#endif
236
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. */
239
240static struct
241{
242 int rate;
243 int code;
244}
245baudtab[] =
246{
247 {
248 50, B50
249 }
250 ,
251 {
252 75, B75
253 }
254 ,
255 {
256 110, B110
257 }
258 ,
259 {
260 134, B134
261 }
262 ,
263 {
264 150, B150
265 }
266 ,
267 {
268 200, B200
269 }
270 ,
271 {
272 300, B300
273 }
274 ,
275 {
276 600, B600
277 }
278 ,
279 {
280 1200, B1200
281 }
282 ,
283 {
284 1800, B1800
285 }
286 ,
287 {
288 2400, B2400
289 }
290 ,
291 {
292 4800, B4800
293 }
294 ,
295 {
296 9600, B9600
297 }
298 ,
299 {
300 19200, B19200
301 }
302 ,
303 {
304 38400, B38400
305 }
306 ,
307#ifdef B57600
308 {
309 57600, B57600
310 }
311 ,
312#endif
313#ifdef B115200
314 {
315 115200, B115200
316 }
317 ,
318#endif
319#ifdef B230400
320 {
321 230400, B230400
322 }
323 ,
324#endif
325#ifdef B460800
326 {
327 460800, B460800
328 }
329 ,
330#endif
331#ifdef B500000
332 {
333 500000, B500000
334 }
335 ,
336#endif
337#ifdef B576000
338 {
339 576000, B576000
340 }
341 ,
342#endif
343#ifdef B921600
344 {
345 921600, B921600
346 }
347 ,
348#endif
349#ifdef B1000000
350 {
351 1000000, B1000000
352 }
353 ,
354#endif
355#ifdef B1152000
356 {
357 1152000, B1152000
358 }
359 ,
360#endif
361#ifdef B1500000
362 {
363 1500000, B1500000
364 }
365 ,
366#endif
367#ifdef B2000000
368 {
369 2000000, B2000000
370 }
371 ,
372#endif
373#ifdef B2500000
374 {
375 2500000, B2500000
376 }
377 ,
378#endif
379#ifdef B3000000
380 {
381 3000000, B3000000
382 }
383 ,
384#endif
385#ifdef B3500000
386 {
387 3500000, B3500000
388 }
389 ,
390#endif
391#ifdef B4000000
392 {
393 4000000, B4000000
394 }
395 ,
396#endif
397 {
398 -1, -1
399 }
400 ,
402
403static int
405{
406 int i;
407
408 for (i = 0; baudtab[i].rate != -1; i++)
409 {
410 /* test for perfect macth. */
411 if (rate == baudtab[i].rate)
412 return baudtab[i].code;
413 else
414 {
415 /* check if it is in between valid values. */
416 if (rate < baudtab[i].rate)
417 {
418 if (i)
419 {
420 warning (_("Invalid baud rate %d. "
421 "Closest values are %d and %d."),
422 rate, baudtab[i - 1].rate, baudtab[i].rate);
423 }
424 else
425 {
426 warning (_("Invalid baud rate %d. Minimum value is %d."),
427 rate, baudtab[0].rate);
428 }
429 return -1;
430 }
431 }
432 }
433
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;
438}
439
440static int
442{
443 struct hardwire_ttystate state;
444 int baud_code = rate_to_code (rate);
445
446 if (baud_code < 0)
447 {
448 /* The baud rate was not valid.
449 A warning has already been issued. */
450 errno = EINVAL;
451 return -1;
452 }
453
454 if (get_tty_state (scb, &state))
455 return -1;
456
457 cfsetospeed (&state.termios, baud_code);
458 cfsetispeed (&state.termios, baud_code);
459
460 return set_tty_state (scb, &state);
461}
462
463static int
464hardwire_setstopbits (struct serial *scb, int num)
465{
466 struct hardwire_ttystate state;
467 int newbit;
468
469 if (get_tty_state (scb, &state))
470 return -1;
471
472 switch (num)
473 {
475 newbit = 0;
476 break;
479 newbit = 1;
480 break;
481 default:
482 return 1;
483 }
484
485 if (!newbit)
486 state.termios.c_cflag &= ~CSTOPB;
487 else
488 state.termios.c_cflag |= CSTOPB; /* two bits */
489
490 return set_tty_state (scb, &state);
491}
492
493/* Implement the "setparity" serial_ops callback. */
494
495static int
497{
498 struct hardwire_ttystate state;
499 int newparity = 0;
500
501 if (get_tty_state (scb, &state))
502 return -1;
503
504 switch (parity)
505 {
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;
518 }
519
520 state.termios.c_cflag &= ~(PARENB | PARODD);
521 state.termios.c_cflag |= newparity;
522
523 return set_tty_state (scb, &state);
524}
525
526
527static void
529{
530 if (scb->fd < 0)
531 return;
532
533 close (scb->fd);
534 scb->fd = -1;
535}
536
537
538
539/* The hardwire ops. */
540
565
567void
569{
571
572#ifdef CRTSCTS
573 add_setshow_boolean_cmd ("remoteflow", no_class,
574 &serial_hwflow, _("\
575Set use of hardware flow control for remote serial I/O."), _("\
576Show use of hardware flow control for remote serial I/O."), _("\
577Enable or disable hardware flow control (RTS/CTS) on the serial port\n\
578when debugging using remote targets."),
579 NULL,
580 show_serial_hwflow,
581 &setlist, &showlist);
582#endif
583}
584
585int
586ser_unix_read_prim (struct serial *scb, size_t count)
587{
588 return read (scb->fd, scb->buf, count);
589}
590
591int
592ser_unix_write_prim (struct serial *scb, const void *buf, size_t len)
593{
594 return write (scb->fd, buf, len);
595}
const char *const name
void xfree(void *)
struct cmd_list_element * showlist
Definition cli-cmds.c:127
struct cmd_list_element * setlist
Definition cli-cmds.c:119
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition cli-decode.c:809
@ no_class
Definition command.h:53
ssize_t read(int fd, void *buf, size_t count)
void ser_base_async(struct serial *scb, int async_p)
Definition ser-base.c:587
int ser_base_write(struct serial *scb, const void *buf, size_t count)
Definition ser-base.c:475
int ser_base_flush_input(struct serial *scb)
Definition ser-base.c:505
int ser_base_readchar(struct serial *scb, int timeout)
Definition ser-base.c:469
static int hardwire_flush_output(struct serial *)
Definition ser-unix.c:175
static void hardwire_print_tty_state(struct serial *, serial_ttystate, struct ui_file *)
Definition ser-unix.c:138
static int rate_to_code(int rate)
Definition ser-unix.c:404
static struct @175 baudtab[]
static int hardwire_setstopbits(struct serial *, int)
Definition ser-unix.c:464
static const struct serial_ops hardwire_ops
Definition ser-unix.c:541
int code
Definition ser-unix.c:243
static int hardwire_open(struct serial *scb, const char *name)
Definition ser-unix.c:76
static int get_tty_state(struct serial *scb, struct hardwire_ttystate *state)
Definition ser-unix.c:86
void _initialize_ser_hardwire()
Definition ser-unix.c:568
static int hardwire_drain_output(struct serial *)
Definition ser-unix.c:166
int ser_unix_write_prim(struct serial *scb, const void *buf, size_t len)
Definition ser-unix.c:592
static serial_ttystate hardwire_copy_tty_state(struct serial *scb, serial_ttystate ttystate)
Definition ser-unix.c:118
int rate
Definition ser-unix.c:242
static int hardwire_flush_input(struct serial *)
Definition ser-unix.c:181
static void hardwire_close(struct serial *scb)
Definition ser-unix.c:528
static serial_ttystate hardwire_get_tty_state(struct serial *scb)
Definition ser-unix.c:104
static int hardwire_setparity(struct serial *scb, int parity)
Definition ser-unix.c:496
int ser_unix_read_prim(struct serial *scb, size_t count)
Definition ser-unix.c:586
#define B38400
Definition ser-unix.c:234
static int hardwire_send_break(struct serial *)
Definition ser-unix.c:189
static int hardwire_set_tty_state(struct serial *scb, serial_ttystate state)
Definition ser-unix.c:128
static int set_tty_state(struct serial *scb, struct hardwire_ttystate *state)
Definition ser-unix.c:95
#define B19200
Definition ser-unix.c:230
static int hardwire_setbaudrate(struct serial *scb, int rate)
Definition ser-unix.c:441
static void hardwire_raw(struct serial *scb)
Definition ser-unix.c:195
static const char * parity
Definition serial.c:641
void serial_add_interface(const struct serial_ops *optable)
Definition serial.c:154
#define GDBPARITY_NONE
Definition serial.h:194
#define GDBPARITY_EVEN
Definition serial.h:196
#define SERIAL_2_STOPBITS
Definition serial.h:190
#define SERIAL_1_AND_A_HALF_STOPBITS
Definition serial.h:189
void * serial_ttystate
Definition serial.h:35
#define SERIAL_1_STOPBITS
Definition serial.h:188
#define GDBPARITY_ODD
Definition serial.h:195
struct termios termios
Definition ser-unix.c:39
int(* write)(struct serial *, const void *buf, size_t count)
Definition serial.h:266
int fd
Definition serial.h:238
unsigned char buf[BUFSIZ]
Definition serial.h:250
Definition value.h:130
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
#define gdb_stderr
Definition utils.h:187