1 /* Generic serial interface routines
2 Copyright 1992, 1993, 1996, 1997 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 PARAMS ((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 PARAMS ((char *));
48 static void serial_logchar PARAMS ((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;
57 static int serial_current_type = 0;
59 /* Log char CH of type CHTYPE, with TIMEOUT */
61 /* Define bogus char to represent a BREAK. Should be careful to choose a value
62 that can't be confused with a normal char, or an error code. */
63 #define SERIAL_BREAK 1235
66 serial_logchar (ch_type, ch, timeout)
71 if (ch_type != serial_current_type)
73 fprintf_unfiltered (serial_logfp, "\n%c ", ch_type);
74 serial_current_type = ch_type;
77 if (serial_logbase != logbase_ascii)
78 fputc_unfiltered (' ', serial_logfp);
83 fprintf_unfiltered (serial_logfp, "<Timeout: %d seconds>", timeout);
86 fprintf_unfiltered (serial_logfp, "<Error: %s>", safe_strerror (errno));
89 fputs_unfiltered ("<Eof>", serial_logfp);
92 fputs_unfiltered ("<Break>", serial_logfp);
95 if (serial_logbase == logbase_hex)
96 fprintf_unfiltered (serial_logfp, "%02x", ch & 0xff);
97 else if (serial_logbase == logbase_octal)
98 fprintf_unfiltered (serial_logfp, "%03o", ch & 0xff);
103 fputs_unfiltered ("\\\\", serial_logfp);
106 fputs_unfiltered ("\\b", serial_logfp);
109 fputs_unfiltered ("\\f", serial_logfp);
112 fputs_unfiltered ("\\n", serial_logfp);
115 fputs_unfiltered ("\\r", serial_logfp);
118 fputs_unfiltered ("\\t", serial_logfp);
121 fputs_unfiltered ("\\v", serial_logfp);
124 fprintf_unfiltered (serial_logfp, isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
131 serial_log_command (cmd)
137 serial_current_type = 'c';
139 fputs_unfiltered ("\nc ", serial_logfp);
140 fputs_unfiltered (cmd, serial_logfp);
142 /* Make sure that the log file is as up-to-date as possible,
143 in case we are getting ready to dump core or something. */
144 gdb_flush (serial_logfp);
148 serial_write (scb, str, len)
153 if (serial_logfp != NULL)
157 for (count = 0; count < len; count++)
158 serial_logchar ('w', str[count] & 0xff, 0);
160 /* Make sure that the log file is as up-to-date as possible,
161 in case we are getting ready to dump core or something. */
162 gdb_flush (serial_logfp);
165 return (scb->ops->write (scb, str, len));
169 serial_readchar (scb, timeout)
175 ch = scb->ops->readchar (scb, timeout);
176 if (serial_logfp != NULL)
178 serial_logchar ('r', ch, timeout);
180 /* Make sure that the log file is as up-to-date as possible,
181 in case we are getting ready to dump core or something. */
182 gdb_flush (serial_logfp);
189 serial_send_break (scb)
192 if (serial_logfp != NULL)
193 serial_logchar ('w', SERIAL_BREAK, 0);
195 return (scb->ops->send_break (scb));
198 static struct serial_ops *
199 serial_interface_lookup (name)
202 struct serial_ops *ops;
204 for (ops = serial_ops_list; ops; ops = ops->next)
205 if (strcmp (name, ops->name) == 0)
212 serial_add_interface (optable)
213 struct serial_ops *optable;
215 optable->next = serial_ops_list;
216 serial_ops_list = optable;
219 /* Open up a device or a network socket, depending upon the syntax of NAME. */
226 struct serial_ops *ops;
228 for (scb = scb_base; scb; scb = scb->next)
229 if (scb->name && strcmp (scb->name, name) == 0)
235 if (strcmp (name, "ocd") == 0)
236 ops = serial_interface_lookup ("ocd");
237 else if (strcmp (name, "pc") == 0)
238 ops = serial_interface_lookup ("pc");
239 else if (strchr (name, ':'))
240 ops = serial_interface_lookup ("tcp");
241 else if (strncmp (name, "lpt", 3) == 0)
242 ops = serial_interface_lookup ("parallel");
243 else if (strncmp (name, "|", 1) == 0)
244 ops = serial_interface_lookup ("pipe");
246 ops = serial_interface_lookup ("hardwire");
251 scb = (serial_t) xmalloc (sizeof (struct _serial_t));
256 scb->bufp = scb->buf;
258 if (scb->ops->open (scb, name))
264 scb->name = strsave (name);
265 scb->next = scb_base;
269 last_serial_opened = scb;
271 if (serial_logfile != NULL)
273 serial_logfp = gdb_fopen (serial_logfile, "w");
274 if (serial_logfp == NULL)
275 perror_with_name (serial_logfile);
286 struct serial_ops *ops;
288 for (scb = scb_base; scb; scb = scb->next)
295 ops = serial_interface_lookup ("hardwire");
300 scb = (serial_t) xmalloc (sizeof (struct _serial_t));
305 scb->bufp = scb->buf;
310 scb->next = scb_base;
314 last_serial_opened = scb;
320 serial_close (scb, really_close)
326 last_serial_opened = NULL;
330 fputs_unfiltered ("\nEnd of log\n", serial_logfp);
331 serial_current_type = 0;
333 /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr? */
334 gdb_fclose (&serial_logfp);
338 /* This is bogus. It's not our fault if you pass us a bad scb...! Rob, you
339 should fix your code instead. */
349 scb->ops->close (scb);
355 scb_base = scb_base->next;
357 for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
359 if (tmp_scb->next != scb)
362 tmp_scb->next = tmp_scb->next->next;
371 The connect command is #if 0 because I hadn't thought of an elegant
372 way to wait for I/O on two serial_t's simultaneously. Two solutions
375 1) Fork, and have have one fork handle the to user direction,
376 and have the other hand the to target direction. This
377 obviously won't cut it for MSDOS.
379 2) Use something like select. This assumes that stdin and
380 the target side can both be waited on via the same
381 mechanism. This may not be true for DOS, if GDB is
382 talking to the target via a TCP socket.
386 /* Connect the user directly to the remote system. This command acts just like
387 the 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
389 static serial_t tty_desc; /* Controlling terminal */
392 cleanup_tty (ttystate)
393 serial_ttystate ttystate;
395 printf_unfiltered ("\r\n[Exiting connect mode]\r\n");
396 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
398 SERIAL_CLOSE (tty_desc);
402 connect_command (args, fromtty)
408 serial_ttystate ttystate;
409 serial_t port_desc; /* TTY port */
414 fprintf_unfiltered (gdb_stderr, "This command takes no args. They have been ignored.\n");
416 printf_unfiltered ("[Entering connect mode. Use ~. or ~^D to escape]\n");
418 tty_desc = SERIAL_FDOPEN (0);
419 port_desc = last_serial_opened;
421 ttystate = SERIAL_GET_TTY_STATE (tty_desc);
423 SERIAL_RAW (tty_desc);
424 SERIAL_RAW (port_desc);
426 make_cleanup (cleanup_tty, ttystate);
432 mask = SERIAL_WAIT_2 (tty_desc, port_desc, -1);
440 c = SERIAL_READCHAR (tty_desc, 0);
442 if (c == SERIAL_TIMEOUT)
446 perror_with_name ("connect");
449 SERIAL_WRITE (port_desc, &cx, 1);
464 if (c == '.' || c == '\004')
478 c = SERIAL_READCHAR (port_desc, 0);
480 if (c == SERIAL_TIMEOUT)
484 perror_with_name ("connect");
488 SERIAL_WRITE (tty_desc, &cx, 1);
496 serial_printf (serial_t desc, const char *format,...)
500 va_start (args, format);
502 vasprintf (&buf, format, args);
503 SERIAL_WRITE (desc, buf, strlen (buf));
510 _initialize_serial ()
513 add_com ("connect", class_obscure, connect_command,
514 "Connect the terminal directly up to the command monitor.\n\
515 Use <CR>~. or <CR>~^D to break out.");
519 (add_set_cmd ("remotelogfile", no_class,
520 var_filename, (char *) &serial_logfile,
521 "Set filename for remote session recording.\n\
522 This file is used to record the remote session for future playback\n\
528 (add_set_enum_cmd ("remotelogbase", no_class,
529 logbase_enums, (char *) &serial_logbase,
530 "Set numerical base for remote session logging",