1 /* Generic serial interface routines
2 Copyright 1992, 1993, 1996 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, Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
26 /* Linked list of serial I/O handlers */
28 static struct serial_ops *serial_ops_list = NULL;
30 /* This is the last serial stream opened. Used by connect command. */
32 static serial_t last_serial_opened = NULL;
34 /* Pointer to list of scb's. */
36 static serial_t scb_base;
38 /* Non-NULL gives filename which contains a recording of the remote session,
39 suitable for playback by gdbserver. */
41 static char *serial_logfile = NULL;
42 static FILE *serial_logfp = NULL;
44 static struct serial_ops *serial_interface_lookup PARAMS ((char *));
45 static void serial_logchar PARAMS ((int ch, int timeout));
46 static char logbase_hex[] = "hex";
47 static char logbase_octal[] = "octal";
48 static char logbase_ascii[] = "ascii";
49 static char *logbase_enums[] = {logbase_hex, logbase_octal, logbase_ascii, NULL};
50 static char *serial_logbase = logbase_ascii;
53 static int serial_reading = 0;
54 static int serial_writing = 0;
57 serial_log_command (cmd)
63 if (serial_reading || serial_writing)
65 fputc_unfiltered ('\n', serial_logfp);
69 fprintf_unfiltered (serial_logfp, "c %s\n", cmd);
70 /* Make sure that the log file is as up-to-date as possible,
71 in case we are getting ready to dump core or something. */
72 fflush (serial_logfp);
75 /* Define bogus char to represent a BREAK. Should be careful to choose a value
76 that can't be confused with a normal char, or an error code. */
77 #define SERIAL_BREAK 1235
80 serial_logchar (ch, timeout)
84 if (serial_logbase != logbase_ascii)
85 fputc_unfiltered (' ', serial_logfp);
90 fprintf_unfiltered (serial_logfp, "<Timeout: %d seconds>", timeout);
93 fprintf_unfiltered (serial_logfp, "<Error: %s>", safe_strerror (errno));
96 fputs_unfiltered ("<Eof>", serial_logfp);
99 fputs_unfiltered ("<Break>", serial_logfp);
102 if (serial_logbase == logbase_hex)
103 fprintf_unfiltered (serial_logfp, "%02x", ch & 0xff);
104 else if (serial_logbase == logbase_octal)
105 fprintf_unfiltered (serial_logfp, "%03o", ch & 0xff);
109 case '\\': fputs_unfiltered ("\\\\", serial_logfp); break;
110 case '\b': fputs_unfiltered ("\\b", serial_logfp); break;
111 case '\f': fputs_unfiltered ("\\f", serial_logfp); break;
112 case '\n': fputs_unfiltered ("\\n", serial_logfp); break;
113 case '\r': fputs_unfiltered ("\\r", serial_logfp); break;
114 case '\t': fputs_unfiltered ("\\t", serial_logfp); break;
115 case '\v': fputs_unfiltered ("\\v", serial_logfp); break;
116 default: fprintf_unfiltered (serial_logfp, isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF); break;
122 serial_write (scb, str, len)
129 if (serial_logfp != NULL)
133 fputc_unfiltered ('\n', serial_logfp);
138 fputs_unfiltered ("w ", serial_logfp);
141 for (count = 0; count < len; count++)
143 serial_logchar (str[count] & 0xff, 0);
145 /* Make sure that the log file is as up-to-date as possible,
146 in case we are getting ready to dump core or something. */
147 fflush (serial_logfp);
149 return (scb -> ops -> write (scb, str, len));
153 serial_readchar (scb, timeout)
159 ch = scb -> ops -> readchar (scb, timeout);
160 if (serial_logfp != NULL)
164 fputc_unfiltered ('\n', serial_logfp);
169 fputs_unfiltered ("r ", serial_logfp);
172 serial_logchar (ch, timeout);
173 /* Make sure that the log file is as up-to-date as possible,
174 in case we are getting ready to dump core or something. */
175 fflush (serial_logfp);
181 serial_send_break (scb)
184 if (serial_logfp != NULL)
188 fputc_unfiltered ('\n', serial_logfp);
193 fputs_unfiltered ("w ", serial_logfp);
196 serial_logchar (SERIAL_BREAK, 0);
197 /* Make sure that the log file is as up-to-date as possible,
198 in case we are getting ready to dump core or something. */
199 fflush (serial_logfp);
201 return (scb -> ops -> send_break (scb));
204 static struct serial_ops *
205 serial_interface_lookup (name)
208 struct serial_ops *ops;
210 for (ops = serial_ops_list; ops; ops = ops->next)
211 if (strcmp (name, ops->name) == 0)
218 serial_add_interface(optable)
219 struct serial_ops *optable;
221 optable->next = serial_ops_list;
222 serial_ops_list = optable;
225 /* Open up a device or a network socket, depending upon the syntax of NAME. */
232 struct serial_ops *ops;
234 for (scb = scb_base; scb; scb = scb->next)
235 if (scb->name && strcmp (scb->name, name) == 0)
241 if (strcmp (name, "pc") == 0)
242 ops = serial_interface_lookup ("pc");
243 else if (strchr (name, ':'))
244 ops = serial_interface_lookup ("tcp");
245 else if (strncmp (name, "lpt", 3) == 0)
246 ops = serial_interface_lookup ("parallel");
248 ops = serial_interface_lookup ("hardwire");
253 scb = (serial_t)xmalloc (sizeof (struct _serial_t));
258 scb->bufp = scb->buf;
260 if (scb->ops->open(scb, name))
266 scb->name = strsave (name);
267 scb->next = scb_base;
271 last_serial_opened = scb;
273 if (serial_logfile != NULL)
275 serial_logfp = fopen (serial_logfile, "w");
276 if (serial_logfp == NULL)
278 perror_with_name (serial_logfile);
290 struct serial_ops *ops;
292 for (scb = scb_base; scb; scb = scb->next)
299 ops = serial_interface_lookup ("hardwire");
304 scb = (serial_t)xmalloc (sizeof (struct _serial_t));
309 scb->bufp = scb->buf;
314 scb->next = scb_base;
318 last_serial_opened = scb;
324 serial_close(scb, really_close)
330 last_serial_opened = NULL;
334 if (serial_reading || serial_writing)
336 fputc_unfiltered ('\n', serial_logfp);
340 fclose (serial_logfp);
344 /* This is bogus. It's not our fault if you pass us a bad scb...! Rob, you
345 should fix your code instead. */
355 scb->ops->close (scb);
361 scb_base = scb_base->next;
363 for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
365 if (tmp_scb->next != scb)
368 tmp_scb->next = tmp_scb->next->next;
377 The connect command is #if 0 because I hadn't thought of an elegant
378 way to wait for I/O on two serial_t's simultaneously. Two solutions
381 1) Fork, and have have one fork handle the to user direction,
382 and have the other hand the to target direction. This
383 obviously won't cut it for MSDOS.
385 2) Use something like select. This assumes that stdin and
386 the target side can both be waited on via the same
387 mechanism. This may not be true for DOS, if GDB is
388 talking to the target via a TCP socket.
392 /* Connect the user directly to the remote system. This command acts just like
393 the 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
395 static serial_t tty_desc; /* Controlling terminal */
398 cleanup_tty(ttystate)
399 serial_ttystate ttystate;
401 printf_unfiltered ("\r\n[Exiting connect mode]\r\n");
402 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
404 SERIAL_CLOSE (tty_desc);
408 connect_command (args, fromtty)
414 serial_ttystate ttystate;
415 serial_t port_desc; /* TTY port */
420 fprintf_unfiltered(gdb_stderr, "This command takes no args. They have been ignored.\n");
422 printf_unfiltered("[Entering connect mode. Use ~. or ~^D to escape]\n");
424 tty_desc = SERIAL_FDOPEN (0);
425 port_desc = last_serial_opened;
427 ttystate = SERIAL_GET_TTY_STATE (tty_desc);
429 SERIAL_RAW (tty_desc);
430 SERIAL_RAW (port_desc);
432 make_cleanup (cleanup_tty, ttystate);
438 mask = SERIAL_WAIT_2 (tty_desc, port_desc, -1);
446 c = SERIAL_READCHAR(tty_desc, 0);
448 if (c == SERIAL_TIMEOUT)
452 perror_with_name("connect");
455 SERIAL_WRITE(port_desc, &cx, 1);
470 if (c == '.' || c == '\004')
484 c = SERIAL_READCHAR(port_desc, 0);
486 if (c == SERIAL_TIMEOUT)
490 perror_with_name("connect");
494 SERIAL_WRITE(tty_desc, &cx, 1);
503 #ifdef ANSI_PROTOTYPES
504 serial_printf (serial_t desc, const char *format, ...)
506 serial_printf (va_alist)
512 #ifdef ANSI_PROTOTYPES
513 va_start (args, format);
519 desc = va_arg (args, serial_t);
520 format = va_arg (args, char *);
523 vasprintf (&buf, format, args);
524 SERIAL_WRITE (desc, buf, strlen (buf));
531 _initialize_serial ()
533 struct cmd_list_element *cmd;
536 add_com ("connect", class_obscure, connect_command,
537 "Connect the terminal directly up to the command monitor.\n\
538 Use <CR>~. or <CR>~^D to break out.");
541 add_show_from_set (add_set_cmd ("remotelogfile", no_class,
542 var_filename, (char *)&serial_logfile,
543 "Set filename for remote session recording.\n\
544 This file is used to record the remote session for future playback\n\
545 by gdbserver.", &setlist),
548 add_show_from_set (add_set_enum_cmd ("remotelogbase", no_class,
550 (char *)&serial_logbase,