1 /* Generic serial interface routines
3 Copyright (C) 1992-2023 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/>. */
24 #include "cli/cli-utils.h"
26 /* Is serial being debugged? */
28 static unsigned int global_serial_debug_p
;
30 /* Serial I/O handlers. */
32 static std::vector
<const struct serial_ops
*> serial_ops_list
;
34 /* Pointer to list of scb's. */
36 static struct serial
*scb_base
;
38 /* Non-NULL gives filename which contains a recording of the remote session,
39 suitable for playback by gdbserver. */
41 static std::string serial_logfile
;
42 static struct ui_file
*serial_logfp
= NULL
;
44 static const struct serial_ops
*serial_interface_lookup (const char *);
45 static void serial_logchar (struct ui_file
*stream
,
46 int ch_type
, int ch
, int timeout
);
47 static const char logbase_hex
[] = "hex";
48 static const char logbase_octal
[] = "octal";
49 static const char logbase_ascii
[] = "ascii";
50 static const char *const logbase_enums
[] =
51 {logbase_hex
, logbase_octal
, logbase_ascii
, NULL
};
52 static const char *serial_logbase
= logbase_ascii
;
55 static int serial_current_type
= 0;
57 /* Log char CH of type CHTYPE, with TIMEOUT. */
59 /* Define bogus char to represent a BREAK. Should be careful to choose a value
60 that can't be confused with a normal char, or an error code. */
61 #define SERIAL_BREAK 1235
64 serial_logchar (struct ui_file
*stream
, int ch_type
, int ch
, int timeout
)
66 if (ch_type
!= serial_current_type
)
68 gdb_printf (stream
, "\n%c ", ch_type
);
69 serial_current_type
= ch_type
;
72 if (serial_logbase
!= logbase_ascii
)
73 gdb_putc (' ', stream
);
78 gdb_printf (stream
, "<Timeout: %d seconds>", timeout
);
81 gdb_printf (stream
, "<Error: %s>", safe_strerror (errno
));
84 gdb_puts ("<Eof>", stream
);
87 gdb_puts ("<Break>", stream
);
90 if (serial_logbase
== logbase_hex
)
91 gdb_printf (stream
, "%02x", ch
& 0xff);
92 else if (serial_logbase
== logbase_octal
)
93 gdb_printf (stream
, "%03o", ch
& 0xff);
98 gdb_puts ("\\\\", stream
);
101 gdb_puts ("\\b", stream
);
104 gdb_puts ("\\f", stream
);
107 gdb_puts ("\\n", stream
);
110 gdb_puts ("\\r", stream
);
113 gdb_puts ("\\t", stream
);
116 gdb_puts ("\\v", stream
);
120 isprint (ch
) ? "%c" : "\\x%02x", ch
& 0xFF);
127 serial_log_command (struct target_ops
*self
, const char *cmd
)
132 serial_current_type
= 'c';
134 gdb_puts ("\nc ", serial_logfp
);
135 gdb_puts (cmd
, serial_logfp
);
137 /* Make sure that the log file is as up-to-date as possible,
138 in case we are getting ready to dump core or something. */
139 gdb_flush (serial_logfp
);
143 static const struct serial_ops
*
144 serial_interface_lookup (const char *name
)
146 for (const serial_ops
*ops
: serial_ops_list
)
147 if (strcmp (name
, ops
->name
) == 0)
154 serial_add_interface (const struct serial_ops
*optable
)
156 serial_ops_list
.push_back (optable
);
159 /* Return the open serial device for FD, if found, or NULL if FD is
160 not already opened. */
163 serial_for_fd (int fd
)
167 for (scb
= scb_base
; scb
; scb
= scb
->next
)
174 /* Create a new serial for OPS. */
176 static gdb::unique_xmalloc_ptr
<struct serial
>
177 new_serial (const struct serial_ops
*ops
)
179 gdb::unique_xmalloc_ptr
<struct serial
> scb (XCNEW (struct serial
));
183 scb
->bufp
= scb
->buf
;
190 static struct serial
*serial_open_ops_1 (const struct serial_ops
*ops
,
191 const char *open_name
);
193 /* Open up a device or a network socket, depending upon the syntax of NAME. */
196 serial_open (const char *name
)
198 const struct serial_ops
*ops
;
199 const char *open_name
= name
;
201 if (startswith (name
, "|"))
202 ops
= serial_interface_lookup ("pipe");
203 /* Check for a colon, suggesting an IP address/port pair.
204 Do this *after* checking for all the interesting prefixes. We
205 don't want to constrain the syntax of what can follow them. */
206 else if (strchr (name
, ':'))
207 ops
= serial_interface_lookup ("tcp");
211 /* Check to see if name is a socket. If it is, then treat it
212 as such. Otherwise assume that it's a character device. */
214 if (stat (name
, &sb
) == 0 && (sb
.st_mode
& S_IFMT
) == S_IFSOCK
)
215 ops
= serial_interface_lookup ("local");
218 ops
= serial_interface_lookup ("hardwire");
222 error (_("could not find serial handler for '%s'"), name
);
224 return serial_open_ops_1 (ops
, open_name
);
227 /* Open up a serial for OPS, passing OPEN_NAME to the open method. */
229 static struct serial
*
230 serial_open_ops_1 (const struct serial_ops
*ops
, const char *open_name
)
232 gdb::unique_xmalloc_ptr
<struct serial
> scb
= new_serial (ops
);
234 /* `...->open (...)' would get expanded by the open(2) syscall macro. */
235 (*scb
->ops
->open
) (scb
.get (), open_name
);
237 scb
->name
= open_name
!= NULL
? xstrdup (open_name
) : NULL
;
238 scb
->next
= scb_base
;
239 scb_base
= scb
.get ();
241 if (!serial_logfile
.empty ())
243 stdio_file_up
file (new stdio_file ());
245 if (!file
->open (serial_logfile
.c_str (), "w"))
246 perror_with_name (serial_logfile
.c_str ());
248 serial_logfp
= file
.release ();
251 return scb
.release ();
257 serial_open_ops (const struct serial_ops
*ops
)
259 return serial_open_ops_1 (ops
, NULL
);
262 /* Open a new serial stream using a file handle, using serial
263 interface ops OPS. */
265 static struct serial
*
266 serial_fdopen_ops (const int fd
, const struct serial_ops
*ops
)
270 ops
= serial_interface_lookup ("terminal");
272 ops
= serial_interface_lookup ("hardwire");
278 gdb::unique_xmalloc_ptr
<struct serial
> scb
= new_serial (ops
);
281 scb
->next
= scb_base
;
282 scb_base
= scb
.get ();
284 if ((ops
->fdopen
) != NULL
)
285 (*ops
->fdopen
) (scb
.get (), fd
);
289 return scb
.release ();
293 serial_fdopen (const int fd
)
295 return serial_fdopen_ops (fd
, NULL
);
299 do_serial_close (struct serial
*scb
, int really_close
)
301 struct serial
*tmp_scb
;
305 gdb_puts ("\nEnd of log\n", serial_logfp
);
306 serial_current_type
= 0;
308 /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr? */
313 /* ensure that the FD has been taken out of async mode. */
314 if (scb
->async_handler
!= NULL
)
315 serial_async (scb
, NULL
, NULL
);
318 scb
->ops
->close (scb
);
322 /* For serial_is_open. */
326 scb_base
= scb_base
->next
;
328 for (tmp_scb
= scb_base
; tmp_scb
; tmp_scb
= tmp_scb
->next
)
330 if (tmp_scb
->next
!= scb
)
333 tmp_scb
->next
= tmp_scb
->next
->next
;
341 serial_close (struct serial
*scb
)
343 do_serial_close (scb
, 1);
347 serial_un_fdopen (struct serial
*scb
)
349 do_serial_close (scb
, 0);
353 serial_is_open (struct serial
*scb
)
355 return scb
->bufp
!= NULL
;
359 serial_ref (struct serial
*scb
)
365 serial_unref (struct serial
*scb
)
368 if (scb
->refcnt
== 0)
373 serial_readchar (struct serial
*scb
, int timeout
)
377 /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC
379 if (0 && serial_is_async_p (scb
) && timeout
< 0)
380 internal_error (_("serial_readchar: blocking read in async mode"));
382 ch
= scb
->ops
->readchar (scb
, timeout
);
383 if (serial_logfp
!= NULL
)
385 serial_logchar (serial_logfp
, 'r', ch
, timeout
);
387 /* Make sure that the log file is as up-to-date as possible,
388 in case we are getting ready to dump core or something. */
389 gdb_flush (serial_logfp
);
391 if (serial_debug_p (scb
))
393 gdb_printf (gdb_stdlog
, "[");
394 serial_logchar (gdb_stdlog
, 'r', ch
, timeout
);
395 gdb_printf (gdb_stdlog
, "]");
396 gdb_flush (gdb_stdlog
);
403 serial_write (struct serial
*scb
, const void *buf
, size_t count
)
405 if (serial_logfp
!= NULL
)
407 const char *str
= (const char *) buf
;
410 for (c
= 0; c
< count
; c
++)
411 serial_logchar (serial_logfp
, 'w', str
[c
] & 0xff, 0);
413 /* Make sure that the log file is as up-to-date as possible,
414 in case we are getting ready to dump core or something. */
415 gdb_flush (serial_logfp
);
417 if (serial_debug_p (scb
))
419 const char *str
= (const char *) buf
;
422 for (c
= 0; c
< count
; c
++)
424 gdb_printf (gdb_stdlog
, "[");
425 serial_logchar (gdb_stdlog
, 'w', str
[c
] & 0xff, 0);
426 gdb_printf (gdb_stdlog
, "]");
428 gdb_flush (gdb_stdlog
);
431 scb
->ops
->write (scb
, buf
, count
);
435 serial_printf (struct serial
*desc
, const char *format
, ...)
438 va_start (args
, format
);
440 std::string buf
= string_vprintf (format
, args
);
441 serial_write (desc
, buf
.c_str (), buf
.length ());
447 serial_drain_output (struct serial
*scb
)
449 return scb
->ops
->drain_output (scb
);
453 serial_flush_output (struct serial
*scb
)
455 return scb
->ops
->flush_output (scb
);
459 serial_flush_input (struct serial
*scb
)
461 return scb
->ops
->flush_input (scb
);
465 serial_send_break (struct serial
*scb
)
467 if (serial_logfp
!= NULL
)
468 serial_logchar (serial_logfp
, 'w', SERIAL_BREAK
, 0);
470 scb
->ops
->send_break (scb
);
474 serial_raw (struct serial
*scb
)
476 scb
->ops
->go_raw (scb
);
480 serial_get_tty_state (struct serial
*scb
)
482 return scb
->ops
->get_tty_state (scb
);
486 serial_copy_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
488 return scb
->ops
->copy_tty_state (scb
, ttystate
);
492 serial_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
494 return scb
->ops
->set_tty_state (scb
, ttystate
);
498 serial_print_tty_state (struct serial
*scb
,
499 serial_ttystate ttystate
,
500 struct ui_file
*stream
)
502 scb
->ops
->print_tty_state (scb
, ttystate
, stream
);
506 serial_setbaudrate (struct serial
*scb
, int rate
)
508 scb
->ops
->setbaudrate (scb
, rate
);
512 serial_setstopbits (struct serial
*scb
, int num
)
514 return scb
->ops
->setstopbits (scb
, num
);
520 serial_setparity (struct serial
*scb
, int parity
)
522 return scb
->ops
->setparity (scb
, parity
);
526 serial_can_async_p (struct serial
*scb
)
528 return (scb
->ops
->async
!= NULL
);
532 serial_is_async_p (struct serial
*scb
)
534 return (scb
->ops
->async
!= NULL
) && (scb
->async_handler
!= NULL
);
538 serial_async (struct serial
*scb
,
539 serial_event_ftype
*handler
,
542 int changed
= ((scb
->async_handler
== NULL
) != (handler
== NULL
));
544 scb
->async_handler
= handler
;
545 scb
->async_context
= context
;
546 /* Only change mode if there is a need. */
548 scb
->ops
->async (scb
, handler
!= NULL
);
552 serial_debug (struct serial
*scb
, int debug_p
)
554 scb
->debug_p
= debug_p
;
558 serial_debug_p (struct serial
*scb
)
560 return scb
->debug_p
|| global_serial_debug_p
;
565 serial_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
567 if (scb
->ops
->wait_handle
)
568 scb
->ops
->wait_handle (scb
, read
, except
);
571 *read
= (HANDLE
) _get_osfhandle (scb
->fd
);
577 serial_done_wait_handle (struct serial
*scb
)
579 if (scb
->ops
->done_wait_handle
)
580 scb
->ops
->done_wait_handle (scb
);
585 serial_pipe (struct serial
*scbs
[2])
587 const struct serial_ops
*ops
;
590 ops
= serial_interface_lookup ("pipe");
597 if (gdb_pipe (fildes
) == -1)
600 scbs
[0] = serial_fdopen_ops (fildes
[0], ops
);
601 scbs
[1] = serial_fdopen_ops (fildes
[1], ops
);
605 /* Serial set/show framework. */
607 static struct cmd_list_element
*serial_set_cmdlist
;
608 static struct cmd_list_element
*serial_show_cmdlist
;
615 serial_baud_show_cmd (struct ui_file
*file
, int from_tty
,
616 struct cmd_list_element
*c
, const char *value
)
618 gdb_printf (file
, _("Baud rate for remote serial I/O is %s.\n"),
624 int serial_parity
= GDBPARITY_NONE
;
626 static const char parity_none
[] = "none";
627 static const char parity_odd
[] = "odd";
628 static const char parity_even
[] = "even";
629 static const char *const parity_enums
[] =
630 {parity_none
, parity_odd
, parity_even
, NULL
};
631 static const char *parity
= parity_none
;
633 /* Set serial_parity value. */
636 set_parity (const char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
638 if (parity
== parity_odd
)
639 serial_parity
= GDBPARITY_ODD
;
640 else if (parity
== parity_even
)
641 serial_parity
= GDBPARITY_EVEN
;
643 serial_parity
= GDBPARITY_NONE
;
646 void _initialize_serial ();
648 _initialize_serial ()
651 add_com ("connect", class_obscure
, connect_command
, _("\
652 Connect the terminal directly up to the command monitor.\n\
653 Use <CR>~. or <CR>~^D to break out."));
656 add_setshow_prefix_cmd ("serial", class_maintenance
,
657 _("Set default serial/parallel port configuration."),
658 _("Show default serial/parallel port configuration."),
659 &serial_set_cmdlist
, &serial_show_cmdlist
,
660 &setlist
, &showlist
);
662 /* If target is open when baud changes, it doesn't take effect until
663 the next open (I think, not sure). */
664 add_setshow_zinteger_cmd ("baud", no_class
, &baud_rate
, _("\
665 Set baud rate for remote serial I/O."), _("\
666 Show baud rate for remote serial I/O."), _("\
667 This value is used to set the speed of the serial port when debugging\n\
668 using remote targets."),
670 serial_baud_show_cmd
,
671 &serial_set_cmdlist
, &serial_show_cmdlist
);
673 add_setshow_enum_cmd ("parity", no_class
, parity_enums
,
675 Set parity for remote serial I/O."), _("\
676 Show parity for remote serial I/O."), NULL
,
678 NULL
, /* FIXME: i18n: */
679 &serial_set_cmdlist
, &serial_show_cmdlist
);
681 add_setshow_filename_cmd ("remotelogfile", no_class
, &serial_logfile
, _("\
682 Set filename for remote session recording."), _("\
683 Show filename for remote session recording."), _("\
684 This file is used to record the remote session for future playback\n\
687 NULL
, /* FIXME: i18n: */
688 &setlist
, &showlist
);
690 add_setshow_enum_cmd ("remotelogbase", no_class
, logbase_enums
,
691 &serial_logbase
, _("\
692 Set numerical base for remote session logging."), _("\
693 Show numerical base for remote session logging."), NULL
,
695 NULL
, /* FIXME: i18n: */
696 &setlist
, &showlist
);
698 add_setshow_zuinteger_cmd ("serial", class_maintenance
,
699 &global_serial_debug_p
, _("\
700 Set serial debugging."), _("\
701 Show serial debugging."), _("\
702 When non-zero, serial port debugging is enabled."),
704 NULL
, /* FIXME: i18n: */
705 &setdebuglist
, &showdebuglist
);