Update readelf's display of RELR sections to include the number of locations relocated
[binutils-gdb.git] / gdb / serial.c
blobaeab7ebcfb08d94ed7412296cb7216c6cd8b3b34
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 <ctype.h>
21 #include "serial.h"
22 #include "gdbcmd.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
62 static void
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);
74 switch (ch)
76 case SERIAL_TIMEOUT:
77 gdb_printf (stream, "<Timeout: %d seconds>", timeout);
78 return;
79 case SERIAL_ERROR:
80 gdb_printf (stream, "<Error: %s>", safe_strerror (errno));
81 return;
82 case SERIAL_EOF:
83 gdb_puts ("<Eof>", stream);
84 return;
85 case SERIAL_BREAK:
86 gdb_puts ("<Break>", stream);
87 return;
88 default:
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);
93 else
94 switch (ch)
96 case '\\':
97 gdb_puts ("\\\\", stream);
98 break;
99 case '\b':
100 gdb_puts ("\\b", stream);
101 break;
102 case '\f':
103 gdb_puts ("\\f", stream);
104 break;
105 case '\n':
106 gdb_puts ("\\n", stream);
107 break;
108 case '\r':
109 gdb_puts ("\\r", stream);
110 break;
111 case '\t':
112 gdb_puts ("\\t", stream);
113 break;
114 case '\v':
115 gdb_puts ("\\v", stream);
116 break;
117 default:
118 gdb_printf (stream,
119 isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
120 break;
125 void
126 serial_log_command (struct target_ops *self, const char *cmd)
128 if (!serial_logfp)
129 return;
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)
147 return ops;
149 return NULL;
152 void
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. */
161 struct serial *
162 serial_for_fd (int fd)
164 struct serial *scb;
166 for (scb = scb_base; scb; scb = scb->next)
167 if (scb->fd == fd)
168 return scb;
170 return NULL;
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));
180 scb->ops = ops;
182 scb->bufp = scb->buf;
183 scb->error_fd = -1;
184 scb->refcnt = 1;
186 return scb;
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. */
194 struct serial *
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");
207 else
209 #ifndef USE_WIN32API
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. */
212 struct stat sb;
213 if (stat (name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFSOCK)
214 ops = serial_interface_lookup ("local");
215 else
216 #endif
217 ops = serial_interface_lookup ("hardwire");
220 if (!ops)
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 ();
253 /* See serial.h. */
255 struct serial *
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)
267 if (!ops)
269 ops = serial_interface_lookup ("terminal");
270 if (!ops)
271 ops = serial_interface_lookup ("hardwire");
274 if (!ops)
275 return NULL;
277 gdb::unique_xmalloc_ptr<struct serial> scb = new_serial (ops);
279 scb->name = NULL;
280 scb->next = scb_base;
281 scb_base = scb.get ();
283 if ((ops->fdopen) != NULL)
284 (*ops->fdopen) (scb.get (), fd);
285 else
286 scb->fd = fd;
288 return scb.release ();
291 struct serial *
292 serial_fdopen (const int fd)
294 return serial_fdopen_ops (fd, NULL);
297 static void
298 do_serial_close (struct serial *scb, int really_close)
300 struct serial *tmp_scb;
302 if (serial_logfp)
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? */
308 delete serial_logfp;
309 serial_logfp = NULL;
312 /* ensure that the FD has been taken out of async mode. */
313 if (scb->async_handler != NULL)
314 serial_async (scb, NULL, NULL);
316 if (really_close)
317 scb->ops->close (scb);
319 xfree (scb->name);
321 /* For serial_is_open. */
322 scb->bufp = NULL;
324 if (scb_base == scb)
325 scb_base = scb_base->next;
326 else
327 for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
329 if (tmp_scb->next != scb)
330 continue;
332 tmp_scb->next = tmp_scb->next->next;
333 break;
336 serial_unref (scb);
339 void
340 serial_close (struct serial *scb)
342 do_serial_close (scb, 1);
345 void
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;
357 void
358 serial_ref (struct serial *scb)
360 scb->refcnt++;
363 void
364 serial_unref (struct serial *scb)
366 --scb->refcnt;
367 if (scb->refcnt == 0)
368 xfree (scb);
372 serial_readchar (struct serial *scb, int timeout)
374 int ch;
376 /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC
377 code is finished. */
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);
398 return (ch);
401 void
402 serial_write (struct serial *scb, const void *buf, size_t count)
404 if (serial_logfp != NULL)
406 const char *str = (const char *) buf;
407 size_t c;
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;
419 size_t c;
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);
433 void
434 serial_printf (struct serial *desc, const char *format, ...)
436 va_list args;
437 va_start (args, format);
439 std::string buf = string_vprintf (format, args);
440 serial_write (desc, buf.c_str (), buf.length ());
442 va_end (args);
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);
463 void
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);
472 void
473 serial_raw (struct serial *scb)
475 scb->ops->go_raw (scb);
478 serial_ttystate
479 serial_get_tty_state (struct serial *scb)
481 return scb->ops->get_tty_state (scb);
484 serial_ttystate
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);
496 void
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);
504 void
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);
516 /* See serial.h. */
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);
536 void
537 serial_async (struct serial *scb,
538 serial_event_ftype *handler,
539 void *context)
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. */
546 if (changed)
547 scb->ops->async (scb, handler != NULL);
550 void
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;
562 #ifdef USE_WIN32API
563 void
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);
568 else
570 *read = (HANDLE) _get_osfhandle (scb->fd);
571 *except = NULL;
575 void
576 serial_done_wait_handle (struct serial *scb)
578 if (scb->ops->done_wait_handle)
579 scb->ops->done_wait_handle (scb);
581 #endif
584 serial_pipe (struct serial *scbs[2])
586 const struct serial_ops *ops;
587 int fildes[2];
589 ops = serial_interface_lookup ("pipe");
590 if (!ops)
592 errno = ENOSYS;
593 return -1;
596 if (gdb_pipe (fildes) == -1)
597 return -1;
599 scbs[0] = serial_fdopen_ops (fildes[0], ops);
600 scbs[1] = serial_fdopen_ops (fildes[1], ops);
601 return 0;
604 /* Serial set/show framework. */
606 static struct cmd_list_element *serial_set_cmdlist;
607 static struct cmd_list_element *serial_show_cmdlist;
609 /* See serial.h. */
611 int baud_rate = -1;
613 static void
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"),
618 value);
621 /* See serial.h. */
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. */
634 static void
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;
641 else
642 serial_parity = GDBPARITY_NONE;
645 void _initialize_serial ();
646 void
647 _initialize_serial ()
649 #if 0
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."));
653 #endif /* 0 */
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."),
668 NULL,
669 serial_baud_show_cmd,
670 &serial_set_cmdlist, &serial_show_cmdlist);
672 add_setshow_enum_cmd ("parity", no_class, parity_enums,
673 &parity, _("\
674 Set parity for remote serial I/O."), _("\
675 Show parity for remote serial I/O."), NULL,
676 set_parity,
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\
684 by gdbserver."),
685 NULL,
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,
693 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."),
702 NULL,
703 NULL, /* FIXME: i18n: */
704 &setdebuglist, &showdebuglist);