1 /* Generic serial interface routines
3 Copyright (C) 1992-2024 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/>. */
22 #include "cli/cli-cmds.h"
23 #include "cli/cli-utils.h"
25 /* Is serial being debugged? */
27 static unsigned int global_serial_debug_p
;
29 /* Serial I/O handlers. */
31 static std::vector
<const struct serial_ops
*> serial_ops_list
;
33 /* Pointer to list of scb's. */
35 static struct serial
*scb_base
;
37 /* Non-NULL gives filename which contains a recording of the remote session,
38 suitable for playback by gdbserver. */
40 static std::string serial_logfile
;
41 static struct ui_file
*serial_logfp
= NULL
;
43 static const struct serial_ops
*serial_interface_lookup (const char *);
44 static void serial_logchar (struct ui_file
*stream
,
45 int ch_type
, int ch
, int timeout
);
46 static const char logbase_hex
[] = "hex";
47 static const char logbase_octal
[] = "octal";
48 static const char logbase_ascii
[] = "ascii";
49 static const char *const logbase_enums
[] =
50 {logbase_hex
, logbase_octal
, logbase_ascii
, NULL
};
51 static const char *serial_logbase
= logbase_ascii
;
54 static int serial_current_type
= 0;
56 /* Log char CH of type CHTYPE, with TIMEOUT. */
58 /* Define bogus char to represent a BREAK. Should be careful to choose a value
59 that can't be confused with a normal char, or an error code. */
60 #define SERIAL_BREAK 1235
63 serial_logchar (struct ui_file
*stream
, int ch_type
, int ch
, int timeout
)
65 if (ch_type
!= serial_current_type
)
67 gdb_printf (stream
, "\n%c ", ch_type
);
68 serial_current_type
= ch_type
;
71 if (serial_logbase
!= logbase_ascii
)
72 gdb_putc (' ', stream
);
77 gdb_printf (stream
, "<Timeout: %d seconds>", timeout
);
80 gdb_printf (stream
, "<Error: %s>", safe_strerror (errno
));
83 gdb_puts ("<Eof>", stream
);
86 gdb_puts ("<Break>", stream
);
89 if (serial_logbase
== logbase_hex
)
90 gdb_printf (stream
, "%02x", ch
& 0xff);
91 else if (serial_logbase
== logbase_octal
)
92 gdb_printf (stream
, "%03o", ch
& 0xff);
97 gdb_puts ("\\\\", stream
);
100 gdb_puts ("\\b", stream
);
103 gdb_puts ("\\f", stream
);
106 gdb_puts ("\\n", stream
);
109 gdb_puts ("\\r", stream
);
112 gdb_puts ("\\t", stream
);
115 gdb_puts ("\\v", stream
);
119 isprint (ch
) ? "%c" : "\\x%02x", ch
& 0xFF);
126 serial_log_command (struct target_ops
*self
, const char *cmd
)
131 serial_current_type
= 'c';
133 gdb_puts ("\nc ", serial_logfp
);
134 gdb_puts (cmd
, serial_logfp
);
136 /* Make sure that the log file is as up-to-date as possible,
137 in case we are getting ready to dump core or something. */
138 gdb_flush (serial_logfp
);
142 static const struct serial_ops
*
143 serial_interface_lookup (const char *name
)
145 for (const serial_ops
*ops
: serial_ops_list
)
146 if (strcmp (name
, ops
->name
) == 0)
153 serial_add_interface (const struct serial_ops
*optable
)
155 serial_ops_list
.push_back (optable
);
158 /* Return the open serial device for FD, if found, or NULL if FD is
159 not already opened. */
162 serial_for_fd (int fd
)
166 for (scb
= scb_base
; scb
; scb
= scb
->next
)
173 /* Create a new serial for OPS. */
175 static gdb::unique_xmalloc_ptr
<struct serial
>
176 new_serial (const struct serial_ops
*ops
)
178 gdb::unique_xmalloc_ptr
<struct serial
> scb (XCNEW (struct serial
));
182 scb
->bufp
= scb
->buf
;
189 static struct serial
*serial_open_ops_1 (const struct serial_ops
*ops
,
190 const char *open_name
);
192 /* Open up a device or a network socket, depending upon the syntax of NAME. */
195 serial_open (const char *name
)
197 const struct serial_ops
*ops
;
198 const char *open_name
= name
;
200 if (startswith (name
, "|"))
201 ops
= serial_interface_lookup ("pipe");
202 /* Check for a colon, suggesting an IP address/port pair.
203 Do this *after* checking for all the interesting prefixes. We
204 don't want to constrain the syntax of what can follow them. */
205 else if (strchr (name
, ':'))
206 ops
= serial_interface_lookup ("tcp");
210 /* Check to see if name is a socket. If it is, then treat it
211 as such. Otherwise assume that it's a character device. */
213 if (stat (name
, &sb
) == 0 && (sb
.st_mode
& S_IFMT
) == S_IFSOCK
)
214 ops
= serial_interface_lookup ("local");
217 ops
= serial_interface_lookup ("hardwire");
221 error (_("could not find serial handler for '%s'"), name
);
223 return serial_open_ops_1 (ops
, open_name
);
226 /* Open up a serial for OPS, passing OPEN_NAME to the open method. */
228 static struct serial
*
229 serial_open_ops_1 (const struct serial_ops
*ops
, const char *open_name
)
231 gdb::unique_xmalloc_ptr
<struct serial
> scb
= new_serial (ops
);
233 /* `...->open (...)' would get expanded by the open(2) syscall macro. */
234 (*scb
->ops
->open
) (scb
.get (), open_name
);
236 scb
->name
= open_name
!= NULL
? xstrdup (open_name
) : NULL
;
237 scb
->next
= scb_base
;
238 scb_base
= scb
.get ();
240 if (!serial_logfile
.empty ())
242 stdio_file_up
file (new stdio_file ());
244 if (!file
->open (serial_logfile
.c_str (), "w"))
245 perror_with_name (serial_logfile
.c_str ());
247 serial_logfp
= file
.release ();
250 return scb
.release ();
256 serial_open_ops (const struct serial_ops
*ops
)
258 return serial_open_ops_1 (ops
, NULL
);
261 /* Open a new serial stream using a file handle, using serial
262 interface ops OPS. */
264 static struct serial
*
265 serial_fdopen_ops (const int fd
, const struct serial_ops
*ops
)
269 ops
= serial_interface_lookup ("terminal");
271 ops
= serial_interface_lookup ("hardwire");
277 gdb::unique_xmalloc_ptr
<struct serial
> scb
= new_serial (ops
);
280 scb
->next
= scb_base
;
281 scb_base
= scb
.get ();
283 if ((ops
->fdopen
) != NULL
)
284 (*ops
->fdopen
) (scb
.get (), fd
);
288 return scb
.release ();
292 serial_fdopen (const int fd
)
294 return serial_fdopen_ops (fd
, NULL
);
298 do_serial_close (struct serial
*scb
, int really_close
)
300 struct serial
*tmp_scb
;
304 gdb_puts ("\nEnd of log\n", serial_logfp
);
305 serial_current_type
= 0;
307 /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr? */
312 /* ensure that the FD has been taken out of async mode. */
313 if (scb
->async_handler
!= NULL
)
314 serial_async (scb
, NULL
, NULL
);
317 scb
->ops
->close (scb
);
321 /* For serial_is_open. */
325 scb_base
= scb_base
->next
;
327 for (tmp_scb
= scb_base
; tmp_scb
; tmp_scb
= tmp_scb
->next
)
329 if (tmp_scb
->next
!= scb
)
332 tmp_scb
->next
= tmp_scb
->next
->next
;
340 serial_close (struct serial
*scb
)
342 do_serial_close (scb
, 1);
346 serial_un_fdopen (struct serial
*scb
)
348 do_serial_close (scb
, 0);
352 serial_is_open (struct serial
*scb
)
354 return scb
->bufp
!= NULL
;
358 serial_ref (struct serial
*scb
)
364 serial_unref (struct serial
*scb
)
367 if (scb
->refcnt
== 0)
372 serial_readchar (struct serial
*scb
, int timeout
)
376 /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC
378 if (0 && serial_is_async_p (scb
) && timeout
< 0)
379 internal_error (_("serial_readchar: blocking read in async mode"));
381 ch
= scb
->ops
->readchar (scb
, timeout
);
382 if (serial_logfp
!= NULL
)
384 serial_logchar (serial_logfp
, 'r', ch
, timeout
);
386 /* Make sure that the log file is as up-to-date as possible,
387 in case we are getting ready to dump core or something. */
388 gdb_flush (serial_logfp
);
390 if (serial_debug_p (scb
))
392 gdb_printf (gdb_stdlog
, "[");
393 serial_logchar (gdb_stdlog
, 'r', ch
, timeout
);
394 gdb_printf (gdb_stdlog
, "]");
395 gdb_flush (gdb_stdlog
);
402 serial_write (struct serial
*scb
, const void *buf
, size_t count
)
404 if (serial_logfp
!= NULL
)
406 const char *str
= (const char *) buf
;
409 for (c
= 0; c
< count
; c
++)
410 serial_logchar (serial_logfp
, 'w', str
[c
] & 0xff, 0);
412 /* Make sure that the log file is as up-to-date as possible,
413 in case we are getting ready to dump core or something. */
414 gdb_flush (serial_logfp
);
416 if (serial_debug_p (scb
))
418 const char *str
= (const char *) buf
;
421 for (c
= 0; c
< count
; c
++)
423 gdb_printf (gdb_stdlog
, "[");
424 serial_logchar (gdb_stdlog
, 'w', str
[c
] & 0xff, 0);
425 gdb_printf (gdb_stdlog
, "]");
427 gdb_flush (gdb_stdlog
);
430 scb
->ops
->write (scb
, buf
, count
);
434 serial_printf (struct serial
*desc
, const char *format
, ...)
437 va_start (args
, format
);
439 std::string buf
= string_vprintf (format
, args
);
440 serial_write (desc
, buf
.c_str (), buf
.length ());
446 serial_drain_output (struct serial
*scb
)
448 return scb
->ops
->drain_output (scb
);
452 serial_flush_output (struct serial
*scb
)
454 return scb
->ops
->flush_output (scb
);
458 serial_flush_input (struct serial
*scb
)
460 return scb
->ops
->flush_input (scb
);
464 serial_send_break (struct serial
*scb
)
466 if (serial_logfp
!= NULL
)
467 serial_logchar (serial_logfp
, 'w', SERIAL_BREAK
, 0);
469 scb
->ops
->send_break (scb
);
473 serial_raw (struct serial
*scb
)
475 scb
->ops
->go_raw (scb
);
479 serial_get_tty_state (struct serial
*scb
)
481 return scb
->ops
->get_tty_state (scb
);
485 serial_copy_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
487 return scb
->ops
->copy_tty_state (scb
, ttystate
);
491 serial_set_tty_state (struct serial
*scb
, serial_ttystate ttystate
)
493 return scb
->ops
->set_tty_state (scb
, ttystate
);
497 serial_print_tty_state (struct serial
*scb
,
498 serial_ttystate ttystate
,
499 struct ui_file
*stream
)
501 scb
->ops
->print_tty_state (scb
, ttystate
, stream
);
505 serial_setbaudrate (struct serial
*scb
, int rate
)
507 scb
->ops
->setbaudrate (scb
, rate
);
511 serial_setstopbits (struct serial
*scb
, int num
)
513 return scb
->ops
->setstopbits (scb
, num
);
519 serial_setparity (struct serial
*scb
, int parity
)
521 return scb
->ops
->setparity (scb
, parity
);
525 serial_can_async_p (struct serial
*scb
)
527 return (scb
->ops
->async
!= NULL
);
531 serial_is_async_p (struct serial
*scb
)
533 return (scb
->ops
->async
!= NULL
) && (scb
->async_handler
!= NULL
);
537 serial_async (struct serial
*scb
,
538 serial_event_ftype
*handler
,
541 int changed
= ((scb
->async_handler
== NULL
) != (handler
== NULL
));
543 scb
->async_handler
= handler
;
544 scb
->async_context
= context
;
545 /* Only change mode if there is a need. */
547 scb
->ops
->async (scb
, handler
!= NULL
);
551 serial_debug (struct serial
*scb
, int debug_p
)
553 scb
->debug_p
= debug_p
;
557 serial_debug_p (struct serial
*scb
)
559 return scb
->debug_p
|| global_serial_debug_p
;
564 serial_wait_handle (struct serial
*scb
, HANDLE
*read
, HANDLE
*except
)
566 if (scb
->ops
->wait_handle
)
567 scb
->ops
->wait_handle (scb
, read
, except
);
570 *read
= (HANDLE
) _get_osfhandle (scb
->fd
);
576 serial_done_wait_handle (struct serial
*scb
)
578 if (scb
->ops
->done_wait_handle
)
579 scb
->ops
->done_wait_handle (scb
);
584 serial_pipe (struct serial
*scbs
[2])
586 const struct serial_ops
*ops
;
589 ops
= serial_interface_lookup ("pipe");
596 if (gdb_pipe (fildes
) == -1)
599 scbs
[0] = serial_fdopen_ops (fildes
[0], ops
);
600 scbs
[1] = serial_fdopen_ops (fildes
[1], ops
);
604 /* Serial set/show framework. */
606 static struct cmd_list_element
*serial_set_cmdlist
;
607 static struct cmd_list_element
*serial_show_cmdlist
;
614 serial_baud_show_cmd (struct ui_file
*file
, int from_tty
,
615 struct cmd_list_element
*c
, const char *value
)
617 gdb_printf (file
, _("Baud rate for remote serial I/O is %s.\n"),
623 int serial_parity
= GDBPARITY_NONE
;
625 static const char parity_none
[] = "none";
626 static const char parity_odd
[] = "odd";
627 static const char parity_even
[] = "even";
628 static const char *const parity_enums
[] =
629 {parity_none
, parity_odd
, parity_even
, NULL
};
630 static const char *parity
= parity_none
;
632 /* Set serial_parity value. */
635 set_parity (const char *ignore_args
, int from_tty
, struct cmd_list_element
*c
)
637 if (parity
== parity_odd
)
638 serial_parity
= GDBPARITY_ODD
;
639 else if (parity
== parity_even
)
640 serial_parity
= GDBPARITY_EVEN
;
642 serial_parity
= GDBPARITY_NONE
;
645 void _initialize_serial ();
647 _initialize_serial ()
650 add_com ("connect", class_obscure
, connect_command
, _("\
651 Connect the terminal directly up to the command monitor.\n\
652 Use <CR>~. or <CR>~^D to break out."));
655 add_setshow_prefix_cmd ("serial", class_maintenance
,
656 _("Set default serial/parallel port configuration."),
657 _("Show default serial/parallel port configuration."),
658 &serial_set_cmdlist
, &serial_show_cmdlist
,
659 &setlist
, &showlist
);
661 /* If target is open when baud changes, it doesn't take effect until
662 the next open (I think, not sure). */
663 add_setshow_zinteger_cmd ("baud", no_class
, &baud_rate
, _("\
664 Set baud rate for remote serial I/O."), _("\
665 Show baud rate for remote serial I/O."), _("\
666 This value is used to set the speed of the serial port when debugging\n\
667 using remote targets."),
669 serial_baud_show_cmd
,
670 &serial_set_cmdlist
, &serial_show_cmdlist
);
672 add_setshow_enum_cmd ("parity", no_class
, parity_enums
,
674 Set parity for remote serial I/O."), _("\
675 Show parity for remote serial I/O."), NULL
,
677 NULL
, /* FIXME: i18n: */
678 &serial_set_cmdlist
, &serial_show_cmdlist
);
680 add_setshow_filename_cmd ("remotelogfile", no_class
, &serial_logfile
, _("\
681 Set filename for remote session recording."), _("\
682 Show filename for remote session recording."), _("\
683 This file is used to record the remote session for future playback\n\
686 NULL
, /* FIXME: i18n: */
687 &setlist
, &showlist
);
689 add_setshow_enum_cmd ("remotelogbase", no_class
, logbase_enums
,
690 &serial_logbase
, _("\
691 Set numerical base for remote session logging."), _("\
692 Show numerical base for remote session logging."), NULL
,
694 NULL
, /* FIXME: i18n: */
695 &setlist
, &showlist
);
697 add_setshow_zuinteger_cmd ("serial", class_maintenance
,
698 &global_serial_debug_p
, _("\
699 Set serial debugging."), _("\
700 Show serial debugging."), _("\
701 When non-zero, serial port debugging is enabled."),
703 NULL
, /* FIXME: i18n: */
704 &setdebuglist
, &showdebuglist
);