1 /* Generic serial interface routines
2 Copyright 1992, 1993, 1996, 1997, 1999 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
24 #include "gdb_string.h"
27 extern void _initialize_serial (void);
29 /* Is serial being debugged? */
31 static int global_serial_debug_p;
33 /* Linked list of serial I/O handlers */
35 static struct serial_ops *serial_ops_list = NULL;
37 /* This is the last serial stream opened. Used by connect command. */
39 static serial_t last_serial_opened = NULL;
41 /* Pointer to list of scb's. */
43 static serial_t scb_base;
45 /* Non-NULL gives filename which contains a recording of the remote session,
46 suitable for playback by gdbserver. */
48 static char *serial_logfile = NULL;
49 static GDB_FILE *serial_logfp = NULL;
51 static struct serial_ops *serial_interface_lookup (char *);
52 static void serial_logchar (struct gdb_file *stream, int ch_type, int ch, int timeout);
53 static char logbase_hex[] = "hex";
54 static char logbase_octal[] = "octal";
55 static char logbase_ascii[] = "ascii";
56 static char *logbase_enums[] =
57 {logbase_hex, logbase_octal, logbase_ascii, NULL};
58 static char *serial_logbase = logbase_ascii;
62 static int serial_current_type = 0;
64 /* Log char CH of type CHTYPE, with TIMEOUT */
66 /* Define bogus char to represent a BREAK. Should be careful to choose a value
67 that can't be confused with a normal char, or an error code. */
68 #define SERIAL_BREAK 1235
71 serial_logchar (struct gdb_file *stream, int ch_type, int ch, int timeout)
73 if (ch_type != serial_current_type)
75 fprintf_unfiltered (stream, "\n%c ", ch_type);
76 serial_current_type = ch_type;
79 if (serial_logbase != logbase_ascii)
80 fputc_unfiltered (' ', stream);
85 fprintf_unfiltered (stream, "<Timeout: %d seconds>", timeout);
88 fprintf_unfiltered (stream, "<Error: %s>", safe_strerror (errno));
91 fputs_unfiltered ("<Eof>", stream);
94 fputs_unfiltered ("<Break>", stream);
97 if (serial_logbase == logbase_hex)
98 fprintf_unfiltered (stream, "%02x", ch & 0xff);
99 else if (serial_logbase == logbase_octal)
100 fprintf_unfiltered (stream, "%03o", ch & 0xff);
105 fputs_unfiltered ("\\\\", stream);
108 fputs_unfiltered ("\\b", stream);
111 fputs_unfiltered ("\\f", stream);
114 fputs_unfiltered ("\\n", stream);
117 fputs_unfiltered ("\\r", stream);
120 fputs_unfiltered ("\\t", stream);
123 fputs_unfiltered ("\\v", stream);
126 fprintf_unfiltered (stream, isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
133 serial_log_command (const char *cmd)
138 serial_current_type = 'c';
140 fputs_unfiltered ("\nc ", serial_logfp);
141 fputs_unfiltered (cmd, serial_logfp);
143 /* Make sure that the log file is as up-to-date as possible,
144 in case we are getting ready to dump core or something. */
145 gdb_flush (serial_logfp);
149 static struct serial_ops *
150 serial_interface_lookup (char *name)
152 struct serial_ops *ops;
154 for (ops = serial_ops_list; ops; ops = ops->next)
155 if (strcmp (name, ops->name) == 0)
162 serial_add_interface (struct serial_ops *optable)
164 optable->next = serial_ops_list;
165 serial_ops_list = optable;
168 /* Open up a device or a network socket, depending upon the syntax of NAME. */
171 serial_open (const char *name)
174 struct serial_ops *ops;
175 const char *open_name = name;
177 for (scb = scb_base; scb; scb = scb->next)
178 if (scb->name && strcmp (scb->name, name) == 0)
184 if (strcmp (name, "ocd") == 0)
185 ops = serial_interface_lookup ("ocd");
186 else if (strcmp (name, "pc") == 0)
187 ops = serial_interface_lookup ("pc");
188 else if (strchr (name, ':'))
189 ops = serial_interface_lookup ("tcp");
190 else if (strncmp (name, "lpt", 3) == 0)
191 ops = serial_interface_lookup ("parallel");
192 else if (strncmp (name, "|", 1) == 0)
194 ops = serial_interface_lookup ("pipe");
195 open_name = name + 1; /* discard ``|'' */
198 ops = serial_interface_lookup ("hardwire");
203 scb = (serial_t) xmalloc (sizeof (struct _serial_t));
208 scb->bufp = scb->buf;
210 if (scb->ops->open (scb, open_name))
216 scb->name = strsave (name);
217 scb->next = scb_base;
220 scb->async_state = 0;
221 scb->async_handler = NULL;
222 scb->async_context = NULL;
225 last_serial_opened = scb;
227 if (serial_logfile != NULL)
229 serial_logfp = gdb_fopen (serial_logfile, "w");
230 if (serial_logfp == NULL)
231 perror_with_name (serial_logfile);
238 serial_fdopen (const int fd)
241 struct serial_ops *ops;
243 for (scb = scb_base; scb; scb = scb->next)
250 ops = serial_interface_lookup ("hardwire");
255 scb = (serial_t) xmalloc (sizeof (struct _serial_t));
260 scb->bufp = scb->buf;
265 scb->next = scb_base;
268 scb->async_state = 0;
269 scb->async_handler = NULL;
270 scb->async_context = NULL;
273 last_serial_opened = scb;
279 do_serial_close (serial_t scb, int really_close)
283 last_serial_opened = NULL;
287 fputs_unfiltered ("\nEnd of log\n", serial_logfp);
288 serial_current_type = 0;
290 /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr? */
291 gdb_fclose (&serial_logfp);
295 /* This is bogus. It's not our fault if you pass us a bad scb...! Rob, you
296 should fix your code instead. */
305 /* ensure that the FD has been taken out of async mode */
306 if (scb->async_handler != NULL)
307 serial_async (scb, NULL, NULL);
310 scb->ops->close (scb);
316 scb_base = scb_base->next;
318 for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
320 if (tmp_scb->next != scb)
323 tmp_scb->next = tmp_scb->next->next;
331 serial_close (serial_t scb)
333 do_serial_close (scb, 1);
337 serial_un_fdopen (serial_t scb)
339 do_serial_close (scb, 0);
343 serial_readchar (serial_t scb, int timeout)
347 ch = scb->ops->readchar (scb, timeout);
348 if (serial_logfp != NULL)
350 serial_logchar (serial_logfp, 'r', ch, timeout);
352 /* Make sure that the log file is as up-to-date as possible,
353 in case we are getting ready to dump core or something. */
354 gdb_flush (serial_logfp);
356 if (SERIAL_DEBUG_P (scb))
358 fprintf_unfiltered (gdb_stdlog, "[");
359 serial_logchar (gdb_stdlog, 'r', ch, timeout);
360 fprintf_unfiltered (gdb_stdlog, "]");
361 gdb_flush (gdb_stdlog);
368 serial_write (serial_t scb, const char *str, int len)
370 if (serial_logfp != NULL)
374 for (count = 0; count < len; count++)
375 serial_logchar (serial_logfp, 'w', str[count] & 0xff, 0);
377 /* Make sure that the log file is as up-to-date as possible,
378 in case we are getting ready to dump core or something. */
379 gdb_flush (serial_logfp);
382 return (scb->ops->write (scb, str, len));
386 serial_printf (serial_t desc, const char *format,...)
390 va_start (args, format);
392 vasprintf (&buf, format, args);
393 SERIAL_WRITE (desc, buf, strlen (buf));
400 serial_drain_output (serial_t scb)
402 return scb->ops->drain_output (scb);
406 serial_flush_output (serial_t scb)
408 return scb->ops->flush_output (scb);
412 serial_flush_input (serial_t scb)
414 return scb->ops->flush_input (scb);
418 serial_send_break (serial_t scb)
420 if (serial_logfp != NULL)
421 serial_logchar (serial_logfp, 'w', SERIAL_BREAK, 0);
423 return (scb->ops->send_break (scb));
427 serial_raw (serial_t scb)
429 scb->ops->go_raw (scb);
433 serial_get_tty_state (serial_t scb)
435 return scb->ops->get_tty_state (scb);
439 serial_set_tty_state (serial_t scb, serial_ttystate ttystate)
441 return scb->ops->set_tty_state (scb, ttystate);
445 serial_print_tty_state (serial_t scb,
446 serial_ttystate ttystate,
447 struct gdb_file *stream)
449 scb->ops->print_tty_state (scb, ttystate, stream);
453 serial_noflush_set_tty_state (serial_t scb,
454 serial_ttystate new_ttystate,
455 serial_ttystate old_ttystate)
457 return scb->ops->noflush_set_tty_state (scb, new_ttystate, old_ttystate);
461 serial_setbaudrate (serial_t scb, int rate)
463 return scb->ops->setbaudrate (scb, rate);
467 serial_setstopbits (serial_t scb, int num)
469 return scb->ops->setstopbits (scb, num);
473 serial_can_async_p (serial_t scb)
475 return (scb->ops->async != NULL);
479 serial_is_async_p (serial_t scb)
481 return (scb->ops->async != NULL) && (scb->async_handler != NULL);
485 serial_async (serial_t scb,
486 serial_event_ftype *handler,
489 /* Only change mode if there is a need. */
490 if ((scb->async_handler == NULL)
491 != (handler == NULL))
492 scb->ops->async (scb, handler != NULL);
493 scb->async_handler = handler;
494 scb->async_context = context;
498 deprecated_serial_fd (serial_t scb)
500 /* FIXME: should this output a warning that deprecated code is being
504 internal_error ("serial: FD not valid");
506 return scb->fd; /* sigh */
510 serial_debug (serial_t scb, int debug_p)
512 scb->debug_p = debug_p;
516 serial_debug_p (serial_t scb)
518 return scb->debug_p || global_serial_debug_p;
524 The connect command is #if 0 because I hadn't thought of an elegant
525 way to wait for I/O on two serial_t's simultaneously. Two solutions
528 1) Fork, and have have one fork handle the to user direction,
529 and have the other hand the to target direction. This
530 obviously won't cut it for MSDOS.
532 2) Use something like select. This assumes that stdin and
533 the target side can both be waited on via the same
534 mechanism. This may not be true for DOS, if GDB is
535 talking to the target via a TCP socket.
539 /* Connect the user directly to the remote system. This command acts just like
540 the 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
542 static serial_t tty_desc; /* Controlling terminal */
545 cleanup_tty (serial_ttystate ttystate)
547 printf_unfiltered ("\r\n[Exiting connect mode]\r\n");
548 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
550 SERIAL_CLOSE (tty_desc);
554 connect_command (char *args, int fromtty)
558 serial_ttystate ttystate;
559 serial_t port_desc; /* TTY port */
564 fprintf_unfiltered (gdb_stderr, "This command takes no args. They have been ignored.\n");
566 printf_unfiltered ("[Entering connect mode. Use ~. or ~^D to escape]\n");
568 tty_desc = SERIAL_FDOPEN (0);
569 port_desc = last_serial_opened;
571 ttystate = SERIAL_GET_TTY_STATE (tty_desc);
573 SERIAL_RAW (tty_desc);
574 SERIAL_RAW (port_desc);
576 make_cleanup (cleanup_tty, ttystate);
582 mask = SERIAL_WAIT_2 (tty_desc, port_desc, -1);
590 c = SERIAL_READCHAR (tty_desc, 0);
592 if (c == SERIAL_TIMEOUT)
596 perror_with_name ("connect");
599 SERIAL_WRITE (port_desc, &cx, 1);
614 if (c == '.' || c == '\004')
628 c = SERIAL_READCHAR (port_desc, 0);
630 if (c == SERIAL_TIMEOUT)
634 perror_with_name ("connect");
638 SERIAL_WRITE (tty_desc, &cx, 1);
646 _initialize_serial (void)
649 add_com ("connect", class_obscure, connect_command,
650 "Connect the terminal directly up to the command monitor.\n\
651 Use <CR>~. or <CR>~^D to break out.");
655 (add_set_cmd ("remotelogfile", no_class,
656 var_filename, (char *) &serial_logfile,
657 "Set filename for remote session recording.\n\
658 This file is used to record the remote session for future playback\n\
664 (add_set_enum_cmd ("remotelogbase", no_class,
665 logbase_enums, (char *) &serial_logbase,
666 "Set numerical base for remote session logging",
670 add_show_from_set (add_set_cmd ("serialdebug",
673 (char *)&global_serial_debug_p,
674 "Set serial debugging.\n\
675 When non-zero, serial port debugging is enabled.", &setlist),