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 /* Linked list of serial I/O handlers */
31 static struct serial_ops *serial_ops_list = NULL;
33 /* This is the last serial stream opened. Used by connect command. */
35 static serial_t last_serial_opened = NULL;
37 /* Pointer to list of scb's. */
39 static serial_t scb_base;
41 /* Non-NULL gives filename which contains a recording of the remote session,
42 suitable for playback by gdbserver. */
44 static char *serial_logfile = NULL;
45 static GDB_FILE *serial_logfp = NULL;
47 static struct serial_ops *serial_interface_lookup (char *);
48 static void serial_logchar (int, int, int);
49 static char logbase_hex[] = "hex";
50 static char logbase_octal[] = "octal";
51 static char logbase_ascii[] = "ascii";
52 static char *logbase_enums[] =
53 {logbase_hex, logbase_octal, logbase_ascii, NULL};
54 static char *serial_logbase = logbase_ascii;
58 static int serial_current_type = 0;
60 /* Log char CH of type CHTYPE, with TIMEOUT */
62 /* Define bogus char to represent a BREAK. Should be careful to choose a value
63 that can't be confused with a normal char, or an error code. */
64 #define SERIAL_BREAK 1235
67 serial_logchar (int ch_type, int ch, int timeout)
69 if (ch_type != serial_current_type)
71 fprintf_unfiltered (serial_logfp, "\n%c ", ch_type);
72 serial_current_type = ch_type;
75 if (serial_logbase != logbase_ascii)
76 fputc_unfiltered (' ', serial_logfp);
81 fprintf_unfiltered (serial_logfp, "<Timeout: %d seconds>", timeout);
84 fprintf_unfiltered (serial_logfp, "<Error: %s>", safe_strerror (errno));
87 fputs_unfiltered ("<Eof>", serial_logfp);
90 fputs_unfiltered ("<Break>", serial_logfp);
93 if (serial_logbase == logbase_hex)
94 fprintf_unfiltered (serial_logfp, "%02x", ch & 0xff);
95 else if (serial_logbase == logbase_octal)
96 fprintf_unfiltered (serial_logfp, "%03o", ch & 0xff);
101 fputs_unfiltered ("\\\\", serial_logfp);
104 fputs_unfiltered ("\\b", serial_logfp);
107 fputs_unfiltered ("\\f", serial_logfp);
110 fputs_unfiltered ("\\n", serial_logfp);
113 fputs_unfiltered ("\\r", serial_logfp);
116 fputs_unfiltered ("\\t", serial_logfp);
119 fputs_unfiltered ("\\v", serial_logfp);
122 fprintf_unfiltered (serial_logfp, isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
129 serial_log_command (const char *cmd)
134 serial_current_type = 'c';
136 fputs_unfiltered ("\nc ", serial_logfp);
137 fputs_unfiltered (cmd, serial_logfp);
139 /* Make sure that the log file is as up-to-date as possible,
140 in case we are getting ready to dump core or something. */
141 gdb_flush (serial_logfp);
145 static struct serial_ops *
146 serial_interface_lookup (char *name)
148 struct serial_ops *ops;
150 for (ops = serial_ops_list; ops; ops = ops->next)
151 if (strcmp (name, ops->name) == 0)
158 serial_add_interface (struct serial_ops *optable)
160 optable->next = serial_ops_list;
161 serial_ops_list = optable;
164 /* Open up a device or a network socket, depending upon the syntax of NAME. */
167 serial_open (const char *name)
170 struct serial_ops *ops;
171 const char *open_name = name;
173 for (scb = scb_base; scb; scb = scb->next)
174 if (scb->name && strcmp (scb->name, name) == 0)
180 if (strcmp (name, "ocd") == 0)
181 ops = serial_interface_lookup ("ocd");
182 else if (strcmp (name, "pc") == 0)
183 ops = serial_interface_lookup ("pc");
184 else if (strchr (name, ':'))
185 ops = serial_interface_lookup ("tcp");
186 else if (strncmp (name, "lpt", 3) == 0)
187 ops = serial_interface_lookup ("parallel");
188 else if (strncmp (name, "|", 1) == 0)
190 ops = serial_interface_lookup ("pipe");
191 open_name = name + 1; /* discard ``|'' */
194 ops = serial_interface_lookup ("hardwire");
199 scb = (serial_t) xmalloc (sizeof (struct _serial_t));
204 scb->bufp = scb->buf;
206 if (scb->ops->open (scb, open_name))
212 scb->name = strsave (name);
213 scb->next = scb_base;
215 scb->async_handler = NULL;
216 scb->async_context = NULL;
219 last_serial_opened = scb;
221 if (serial_logfile != NULL)
223 serial_logfp = gdb_fopen (serial_logfile, "w");
224 if (serial_logfp == NULL)
225 perror_with_name (serial_logfile);
232 serial_fdopen (const int fd)
235 struct serial_ops *ops;
237 for (scb = scb_base; scb; scb = scb->next)
244 ops = serial_interface_lookup ("hardwire");
249 scb = (serial_t) xmalloc (sizeof (struct _serial_t));
254 scb->bufp = scb->buf;
259 scb->next = scb_base;
261 scb->async_handler = NULL;
262 scb->async_context = NULL;
265 last_serial_opened = scb;
271 do_serial_close (serial_t scb, int really_close)
275 last_serial_opened = NULL;
279 fputs_unfiltered ("\nEnd of log\n", serial_logfp);
280 serial_current_type = 0;
282 /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr? */
283 gdb_fclose (&serial_logfp);
287 /* This is bogus. It's not our fault if you pass us a bad scb...! Rob, you
288 should fix your code instead. */
297 /* ensure that the FD has been taken out of async mode */
298 if (scb->async_handler != NULL)
299 serial_async (scb, NULL, NULL);
302 scb->ops->close (scb);
308 scb_base = scb_base->next;
310 for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
312 if (tmp_scb->next != scb)
315 tmp_scb->next = tmp_scb->next->next;
323 serial_close (serial_t scb)
325 do_serial_close (scb, 1);
329 serial_un_fdopen (serial_t scb)
331 do_serial_close (scb, 0);
335 serial_readchar (serial_t scb, int timeout)
339 ch = scb->ops->readchar (scb, timeout);
340 if (serial_logfp != NULL)
342 serial_logchar ('r', ch, timeout);
344 /* Make sure that the log file is as up-to-date as possible,
345 in case we are getting ready to dump core or something. */
346 gdb_flush (serial_logfp);
353 serial_write (serial_t scb, const char *str, int len)
355 if (serial_logfp != NULL)
359 for (count = 0; count < len; count++)
360 serial_logchar ('w', str[count] & 0xff, 0);
362 /* Make sure that the log file is as up-to-date as possible,
363 in case we are getting ready to dump core or something. */
364 gdb_flush (serial_logfp);
367 return (scb->ops->write (scb, str, len));
371 serial_printf (serial_t desc, const char *format,...)
375 va_start (args, format);
377 vasprintf (&buf, format, args);
378 SERIAL_WRITE (desc, buf, strlen (buf));
385 serial_drain_output (serial_t scb)
387 return scb->ops->drain_output (scb);
391 serial_flush_output (serial_t scb)
393 return scb->ops->flush_output (scb);
397 serial_flush_input (serial_t scb)
399 return scb->ops->flush_input (scb);
403 serial_send_break (serial_t scb)
405 if (serial_logfp != NULL)
406 serial_logchar ('w', SERIAL_BREAK, 0);
408 return (scb->ops->send_break (scb));
412 serial_raw (serial_t scb)
414 scb->ops->go_raw (scb);
418 serial_get_tty_state (serial_t scb)
420 return scb->ops->get_tty_state (scb);
424 serial_set_tty_state (serial_t scb, serial_ttystate ttystate)
426 return scb->ops->set_tty_state (scb, ttystate);
430 serial_print_tty_state (serial_t scb,
431 serial_ttystate ttystate,
432 struct gdb_file *stream)
434 scb->ops->print_tty_state (scb, ttystate, stream);
438 serial_noflush_set_tty_state (serial_t scb,
439 serial_ttystate new_ttystate,
440 serial_ttystate old_ttystate)
442 return scb->ops->noflush_set_tty_state (scb, new_ttystate, old_ttystate);
446 serial_setbaudrate (serial_t scb, int rate)
448 return scb->ops->setbaudrate (scb, rate);
452 serial_setstopbits (serial_t scb, int num)
454 return scb->ops->setstopbits (scb, num);
458 serial_can_async_p (serial_t scb)
460 return (scb->ops->async != NULL);
464 serial_is_async_p (serial_t scb)
466 return (scb->ops->async != NULL) && (scb->async_handler != NULL);
470 serial_async (serial_t scb,
471 serial_event_ftype *handler,
474 /* Only change mode if there is a need. */
475 if ((scb->async_handler == NULL)
476 != (handler == NULL))
477 scb->ops->async (scb, handler != NULL);
478 scb->async_handler = handler;
479 scb->async_context = context;
483 deprecated_serial_fd (serial_t scb)
485 /* FIXME: should this output a warning that deprecated code is being
489 internal_error ("serial: FD not valid");
491 return scb->fd; /* sigh */
496 The connect command is #if 0 because I hadn't thought of an elegant
497 way to wait for I/O on two serial_t's simultaneously. Two solutions
500 1) Fork, and have have one fork handle the to user direction,
501 and have the other hand the to target direction. This
502 obviously won't cut it for MSDOS.
504 2) Use something like select. This assumes that stdin and
505 the target side can both be waited on via the same
506 mechanism. This may not be true for DOS, if GDB is
507 talking to the target via a TCP socket.
511 /* Connect the user directly to the remote system. This command acts just like
512 the 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
514 static serial_t tty_desc; /* Controlling terminal */
517 cleanup_tty (serial_ttystate ttystate)
519 printf_unfiltered ("\r\n[Exiting connect mode]\r\n");
520 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
522 SERIAL_CLOSE (tty_desc);
526 connect_command (char *args, int fromtty)
530 serial_ttystate ttystate;
531 serial_t port_desc; /* TTY port */
536 fprintf_unfiltered (gdb_stderr, "This command takes no args. They have been ignored.\n");
538 printf_unfiltered ("[Entering connect mode. Use ~. or ~^D to escape]\n");
540 tty_desc = SERIAL_FDOPEN (0);
541 port_desc = last_serial_opened;
543 ttystate = SERIAL_GET_TTY_STATE (tty_desc);
545 SERIAL_RAW (tty_desc);
546 SERIAL_RAW (port_desc);
548 make_cleanup (cleanup_tty, ttystate);
554 mask = SERIAL_WAIT_2 (tty_desc, port_desc, -1);
562 c = SERIAL_READCHAR (tty_desc, 0);
564 if (c == SERIAL_TIMEOUT)
568 perror_with_name ("connect");
571 SERIAL_WRITE (port_desc, &cx, 1);
586 if (c == '.' || c == '\004')
600 c = SERIAL_READCHAR (port_desc, 0);
602 if (c == SERIAL_TIMEOUT)
606 perror_with_name ("connect");
610 SERIAL_WRITE (tty_desc, &cx, 1);
618 _initialize_serial (void)
621 add_com ("connect", class_obscure, connect_command,
622 "Connect the terminal directly up to the command monitor.\n\
623 Use <CR>~. or <CR>~^D to break out.");
627 (add_set_cmd ("remotelogfile", no_class,
628 var_filename, (char *) &serial_logfile,
629 "Set filename for remote session recording.\n\
630 This file is used to record the remote session for future playback\n\
636 (add_set_enum_cmd ("remotelogbase", no_class,
637 logbase_enums, (char *) &serial_logbase,
638 "Set numerical base for remote session logging",