Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / serial.c
blob734a580ed02038321b3ff1f9280d2f7ef926e371
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/>. */
20 #include "defs.h"
21 #include <ctype.h>
22 #include "serial.h"
23 #include "gdbcmd.h"
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
63 static void
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);
75 switch (ch)
77 case SERIAL_TIMEOUT:
78 gdb_printf (stream, "<Timeout: %d seconds>", timeout);
79 return;
80 case SERIAL_ERROR:
81 gdb_printf (stream, "<Error: %s>", safe_strerror (errno));
82 return;
83 case SERIAL_EOF:
84 gdb_puts ("<Eof>", stream);
85 return;
86 case SERIAL_BREAK:
87 gdb_puts ("<Break>", stream);
88 return;
89 default:
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);
94 else
95 switch (ch)
97 case '\\':
98 gdb_puts ("\\\\", stream);
99 break;
100 case '\b':
101 gdb_puts ("\\b", stream);
102 break;
103 case '\f':
104 gdb_puts ("\\f", stream);
105 break;
106 case '\n':
107 gdb_puts ("\\n", stream);
108 break;
109 case '\r':
110 gdb_puts ("\\r", stream);
111 break;
112 case '\t':
113 gdb_puts ("\\t", stream);
114 break;
115 case '\v':
116 gdb_puts ("\\v", stream);
117 break;
118 default:
119 gdb_printf (stream,
120 isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
121 break;
126 void
127 serial_log_command (struct target_ops *self, const char *cmd)
129 if (!serial_logfp)
130 return;
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)
148 return ops;
150 return NULL;
153 void
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. */
162 struct serial *
163 serial_for_fd (int fd)
165 struct serial *scb;
167 for (scb = scb_base; scb; scb = scb->next)
168 if (scb->fd == fd)
169 return scb;
171 return NULL;
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));
181 scb->ops = ops;
183 scb->bufp = scb->buf;
184 scb->error_fd = -1;
185 scb->refcnt = 1;
187 return scb;
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. */
195 struct serial *
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");
208 else
210 #ifndef USE_WIN32API
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. */
213 struct stat sb;
214 if (stat (name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFSOCK)
215 ops = serial_interface_lookup ("local");
216 else
217 #endif
218 ops = serial_interface_lookup ("hardwire");
221 if (!ops)
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 ();
254 /* See serial.h. */
256 struct serial *
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)
268 if (!ops)
270 ops = serial_interface_lookup ("terminal");
271 if (!ops)
272 ops = serial_interface_lookup ("hardwire");
275 if (!ops)
276 return NULL;
278 gdb::unique_xmalloc_ptr<struct serial> scb = new_serial (ops);
280 scb->name = NULL;
281 scb->next = scb_base;
282 scb_base = scb.get ();
284 if ((ops->fdopen) != NULL)
285 (*ops->fdopen) (scb.get (), fd);
286 else
287 scb->fd = fd;
289 return scb.release ();
292 struct serial *
293 serial_fdopen (const int fd)
295 return serial_fdopen_ops (fd, NULL);
298 static void
299 do_serial_close (struct serial *scb, int really_close)
301 struct serial *tmp_scb;
303 if (serial_logfp)
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? */
309 delete serial_logfp;
310 serial_logfp = NULL;
313 /* ensure that the FD has been taken out of async mode. */
314 if (scb->async_handler != NULL)
315 serial_async (scb, NULL, NULL);
317 if (really_close)
318 scb->ops->close (scb);
320 xfree (scb->name);
322 /* For serial_is_open. */
323 scb->bufp = NULL;
325 if (scb_base == scb)
326 scb_base = scb_base->next;
327 else
328 for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
330 if (tmp_scb->next != scb)
331 continue;
333 tmp_scb->next = tmp_scb->next->next;
334 break;
337 serial_unref (scb);
340 void
341 serial_close (struct serial *scb)
343 do_serial_close (scb, 1);
346 void
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;
358 void
359 serial_ref (struct serial *scb)
361 scb->refcnt++;
364 void
365 serial_unref (struct serial *scb)
367 --scb->refcnt;
368 if (scb->refcnt == 0)
369 xfree (scb);
373 serial_readchar (struct serial *scb, int timeout)
375 int ch;
377 /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC
378 code is finished. */
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);
399 return (ch);
402 void
403 serial_write (struct serial *scb, const void *buf, size_t count)
405 if (serial_logfp != NULL)
407 const char *str = (const char *) buf;
408 size_t c;
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;
420 size_t c;
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);
434 void
435 serial_printf (struct serial *desc, const char *format, ...)
437 va_list args;
438 va_start (args, format);
440 std::string buf = string_vprintf (format, args);
441 serial_write (desc, buf.c_str (), buf.length ());
443 va_end (args);
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);
464 void
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);
473 void
474 serial_raw (struct serial *scb)
476 scb->ops->go_raw (scb);
479 serial_ttystate
480 serial_get_tty_state (struct serial *scb)
482 return scb->ops->get_tty_state (scb);
485 serial_ttystate
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);
497 void
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);
505 void
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);
517 /* See serial.h. */
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);
537 void
538 serial_async (struct serial *scb,
539 serial_event_ftype *handler,
540 void *context)
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. */
547 if (changed)
548 scb->ops->async (scb, handler != NULL);
551 void
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;
563 #ifdef USE_WIN32API
564 void
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);
569 else
571 *read = (HANDLE) _get_osfhandle (scb->fd);
572 *except = NULL;
576 void
577 serial_done_wait_handle (struct serial *scb)
579 if (scb->ops->done_wait_handle)
580 scb->ops->done_wait_handle (scb);
582 #endif
585 serial_pipe (struct serial *scbs[2])
587 const struct serial_ops *ops;
588 int fildes[2];
590 ops = serial_interface_lookup ("pipe");
591 if (!ops)
593 errno = ENOSYS;
594 return -1;
597 if (gdb_pipe (fildes) == -1)
598 return -1;
600 scbs[0] = serial_fdopen_ops (fildes[0], ops);
601 scbs[1] = serial_fdopen_ops (fildes[1], ops);
602 return 0;
605 /* Serial set/show framework. */
607 static struct cmd_list_element *serial_set_cmdlist;
608 static struct cmd_list_element *serial_show_cmdlist;
610 /* See serial.h. */
612 int baud_rate = -1;
614 static void
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"),
619 value);
622 /* See serial.h. */
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. */
635 static void
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;
642 else
643 serial_parity = GDBPARITY_NONE;
646 void _initialize_serial ();
647 void
648 _initialize_serial ()
650 #if 0
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."));
654 #endif /* 0 */
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."),
669 NULL,
670 serial_baud_show_cmd,
671 &serial_set_cmdlist, &serial_show_cmdlist);
673 add_setshow_enum_cmd ("parity", no_class, parity_enums,
674 &parity, _("\
675 Set parity for remote serial I/O."), _("\
676 Show parity for remote serial I/O."), NULL,
677 set_parity,
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\
685 by gdbserver."),
686 NULL,
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,
694 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."),
703 NULL,
704 NULL, /* FIXME: i18n: */
705 &setdebuglist, &showdebuglist);