1 /* Generic serial interface routines
3 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
4 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
26 #include "gdb_string.h"
29 extern void _initialize_serial (void);
31 /* Is serial being debugged? */
33 static int global_serial_debug_p
;
35 /* Linked list of serial I/O handlers */
37 static struct serial_ops
*serial_ops_list
= NULL
;
39 /* This is the last serial stream opened. Used by connect command. */
41 static serial_t last_serial_opened
= NULL
;
43 /* Pointer to list of scb's. */
45 static serial_t scb_base
;
47 /* Non-NULL gives filename which contains a recording of the remote session,
48 suitable for playback by gdbserver. */
50 static char *serial_logfile
= NULL
;
51 static struct ui_file
*serial_logfp
= NULL
;
53 static struct serial_ops
*serial_interface_lookup (char *);
54 static void serial_logchar (struct ui_file
*stream
, int ch_type
, int ch
, int timeout
);
55 static const char logbase_hex
[] = "hex";
56 static const char logbase_octal
[] = "octal";
57 static const char logbase_ascii
[] = "ascii";
58 static const char *logbase_enums
[] =
59 {logbase_hex
, logbase_octal
, logbase_ascii
, NULL
};
60 static const char *serial_logbase
= logbase_ascii
;
63 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
67 static int serial_current_type
= 0;
69 /* Log char CH of type CHTYPE, with TIMEOUT */
71 /* Define bogus char to represent a BREAK. Should be careful to choose a value
72 that can't be confused with a normal char, or an error code. */
73 #define SERIAL_BREAK 1235
76 serial_logchar (struct ui_file
*stream
, int ch_type
, int ch
, int timeout
)
78 if (ch_type
!= serial_current_type
)
80 fprintf_unfiltered (stream
, "\n%c ", ch_type
);
81 serial_current_type
= ch_type
;
84 if (serial_logbase
!= logbase_ascii
)
85 fputc_unfiltered (' ', stream
);
90 fprintf_unfiltered (stream
, "<Timeout: %d seconds>", timeout
);
93 fprintf_unfiltered (stream
, "<Error: %s>", safe_strerror (errno
));
96 fputs_unfiltered ("<Eof>", stream
);
99 fputs_unfiltered ("<Break>", stream
);
102 if (serial_logbase
== logbase_hex
)
103 fprintf_unfiltered (stream
, "%02x", ch
& 0xff);
104 else if (serial_logbase
== logbase_octal
)
105 fprintf_unfiltered (stream
, "%03o", ch
& 0xff);
110 fputs_unfiltered ("\\\\", stream
);
113 fputs_unfiltered ("\\b", stream
);
116 fputs_unfiltered ("\\f", stream
);
119 fputs_unfiltered ("\\n", stream
);
122 fputs_unfiltered ("\\r", stream
);
125 fputs_unfiltered ("\\t", stream
);
128 fputs_unfiltered ("\\v", stream
);
131 fprintf_unfiltered (stream
, isprint (ch
) ? "%c" : "\\x%02x", ch
& 0xFF);
138 serial_log_command (const char *cmd
)
143 serial_current_type
= 'c';
145 fputs_unfiltered ("\nc ", serial_logfp
);
146 fputs_unfiltered (cmd
, serial_logfp
);
148 /* Make sure that the log file is as up-to-date as possible,
149 in case we are getting ready to dump core or something. */
150 gdb_flush (serial_logfp
);
154 static struct serial_ops
*
155 serial_interface_lookup (char *name
)
157 struct serial_ops
*ops
;
159 for (ops
= serial_ops_list
; ops
; ops
= ops
->next
)
160 if (strcmp (name
, ops
->name
) == 0)
167 serial_add_interface (struct serial_ops
*optable
)
169 optable
->next
= serial_ops_list
;
170 serial_ops_list
= optable
;
173 /* Open up a device or a network socket, depending upon the syntax of NAME. */
176 serial_open (const char *name
)
179 struct serial_ops
*ops
;
180 const char *open_name
= name
;
182 for (scb
= scb_base
; scb
; scb
= scb
->next
)
183 if (scb
->name
&& strcmp (scb
->name
, name
) == 0)
189 if (strcmp (name
, "ocd") == 0)
190 ops
= serial_interface_lookup ("ocd");
191 else if (strcmp (name
, "pc") == 0)
192 ops
= serial_interface_lookup ("pc");
193 else if (strchr (name
, ':'))
194 ops
= serial_interface_lookup ("tcp");
195 else if (strncmp (name
, "lpt", 3) == 0)
196 ops
= serial_interface_lookup ("parallel");
197 else if (strncmp (name
, "|", 1) == 0)
199 ops
= serial_interface_lookup ("pipe");
200 open_name
= name
+ 1; /* discard ``|'' */
203 ops
= serial_interface_lookup ("hardwire");
208 scb
= XMALLOC (struct serial
);
213 scb
->bufp
= scb
->buf
;
215 if (scb
->ops
->open (scb
, open_name
))
221 scb
->name
= xstrdup (name
);
222 scb
->next
= scb_base
;
225 scb
->async_state
= 0;
226 scb
->async_handler
= NULL
;
227 scb
->async_context
= NULL
;
230 last_serial_opened
= scb
;
232 if (serial_logfile
!= NULL
)
234 serial_logfp
= gdb_fopen (serial_logfile
, "w");
235 if (serial_logfp
== NULL
)
236 perror_with_name (serial_logfile
);
243 serial_fdopen (const int fd
)
246 struct serial_ops
*ops
;
248 for (scb
= scb_base
; scb
; scb
= scb
->next
)
255 ops
= serial_interface_lookup ("hardwire");
260 scb
= XMALLOC (struct serial
);
265 scb
->bufp
= scb
->buf
;
270 scb
->next
= scb_base
;
273 scb
->async_state
= 0;
274 scb
->async_handler
= NULL
;
275 scb
->async_context
= NULL
;
278 last_serial_opened
= scb
;
284 do_serial_close (serial_t scb
, int really_close
)
288 last_serial_opened
= NULL
;
292 fputs_unfiltered ("\nEnd of log\n", serial_logfp
);
293 serial_current_type
= 0;
295 /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr? */
296 ui_file_delete (serial_logfp
);
300 /* This is bogus. It's not our fault if you pass us a bad scb...! Rob, you
301 should fix your code instead. */
310 /* ensure that the FD has been taken out of async mode */
311 if (scb
->async_handler
!= NULL
)
312 serial_async (scb
, NULL
, NULL
);
315 scb
->ops
->close (scb
);
321 scb_base
= scb_base
->next
;
323 for (tmp_scb
= scb_base
; tmp_scb
; tmp_scb
= tmp_scb
->next
)
325 if (tmp_scb
->next
!= scb
)
328 tmp_scb
->next
= tmp_scb
->next
->next
;
336 serial_close (serial_t scb
)
338 do_serial_close (scb
, 1);
342 serial_un_fdopen (serial_t scb
)
344 do_serial_close (scb
, 0);
348 serial_readchar (serial_t scb
, int timeout
)
352 /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC
354 if (0 && SERIAL_IS_ASYNC_P (scb
) && timeout
< 0)
355 internal_error (__FILE__
, __LINE__
,
356 "serial_readchar: blocking read in async mode");
358 ch
= scb
->ops
->readchar (scb
, timeout
);
359 if (serial_logfp
!= NULL
)
361 serial_logchar (serial_logfp
, 'r', ch
, timeout
);
363 /* Make sure that the log file is as up-to-date as possible,
364 in case we are getting ready to dump core or something. */
365 gdb_flush (serial_logfp
);
367 if (SERIAL_DEBUG_P (scb
))
369 fprintf_unfiltered (gdb_stdlog
, "[");
370 serial_logchar (gdb_stdlog
, 'r', ch
, timeout
);
371 fprintf_unfiltered (gdb_stdlog
, "]");
372 gdb_flush (gdb_stdlog
);
379 serial_write (serial_t scb
, const char *str
, int len
)
381 if (serial_logfp
!= NULL
)
385 for (count
= 0; count
< len
; count
++)
386 serial_logchar (serial_logfp
, 'w', str
[count
] & 0xff, 0);
388 /* Make sure that the log file is as up-to-date as possible,
389 in case we are getting ready to dump core or something. */
390 gdb_flush (serial_logfp
);
393 return (scb
->ops
->write (scb
, str
, len
));
397 serial_printf (serial_t desc
, const char *format
,...)
401 va_start (args
, format
);
403 xvasprintf (&buf
, format
, args
);
404 SERIAL_WRITE (desc
, buf
, strlen (buf
));
411 serial_drain_output (serial_t scb
)
413 return scb
->ops
->drain_output (scb
);
417 serial_flush_output (serial_t scb
)
419 return scb
->ops
->flush_output (scb
);
423 serial_flush_input (serial_t scb
)
425 return scb
->ops
->flush_input (scb
);
429 serial_send_break (serial_t scb
)
431 if (serial_logfp
!= NULL
)
432 serial_logchar (serial_logfp
, 'w', SERIAL_BREAK
, 0);
434 return (scb
->ops
->send_break (scb
));
438 serial_raw (serial_t scb
)
440 scb
->ops
->go_raw (scb
);
444 serial_get_tty_state (serial_t scb
)
446 return scb
->ops
->get_tty_state (scb
);
450 serial_set_tty_state (serial_t scb
, serial_ttystate ttystate
)
452 return scb
->ops
->set_tty_state (scb
, ttystate
);
456 serial_print_tty_state (serial_t scb
,
457 serial_ttystate ttystate
,
458 struct ui_file
*stream
)
460 scb
->ops
->print_tty_state (scb
, ttystate
, stream
);
464 serial_noflush_set_tty_state (serial_t scb
,
465 serial_ttystate new_ttystate
,
466 serial_ttystate old_ttystate
)
468 return scb
->ops
->noflush_set_tty_state (scb
, new_ttystate
, old_ttystate
);
472 serial_setbaudrate (serial_t scb
, int rate
)
474 return scb
->ops
->setbaudrate (scb
, rate
);
478 serial_setstopbits (serial_t scb
, int num
)
480 return scb
->ops
->setstopbits (scb
, num
);
484 serial_can_async_p (serial_t scb
)
486 return (scb
->ops
->async
!= NULL
);
490 serial_is_async_p (serial_t scb
)
492 return (scb
->ops
->async
!= NULL
) && (scb
->async_handler
!= NULL
);
496 serial_async (serial_t scb
,
497 serial_event_ftype
*handler
,
500 /* Only change mode if there is a need. */
501 if ((scb
->async_handler
== NULL
)
502 != (handler
== NULL
))
503 scb
->ops
->async (scb
, handler
!= NULL
);
504 scb
->async_handler
= handler
;
505 scb
->async_context
= context
;
509 deprecated_serial_fd (serial_t scb
)
511 /* FIXME: should this output a warning that deprecated code is being
515 internal_error (__FILE__
, __LINE__
,
516 "serial: FD not valid");
518 return scb
->fd
; /* sigh */
522 serial_debug (serial_t scb
, int debug_p
)
524 scb
->debug_p
= debug_p
;
528 serial_debug_p (serial_t scb
)
530 return scb
->debug_p
|| global_serial_debug_p
;
536 The connect command is #if 0 because I hadn't thought of an elegant
537 way to wait for I/O on two serial_t's simultaneously. Two solutions
540 1) Fork, and have have one fork handle the to user direction,
541 and have the other hand the to target direction. This
542 obviously won't cut it for MSDOS.
544 2) Use something like select. This assumes that stdin and
545 the target side can both be waited on via the same
546 mechanism. This may not be true for DOS, if GDB is
547 talking to the target via a TCP socket.
551 /* Connect the user directly to the remote system. This command acts just like
552 the 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
554 static serial_t tty_desc
; /* Controlling terminal */
557 cleanup_tty (serial_ttystate ttystate
)
559 printf_unfiltered ("\r\n[Exiting connect mode]\r\n");
560 SERIAL_SET_TTY_STATE (tty_desc
, ttystate
);
562 SERIAL_CLOSE (tty_desc
);
566 connect_command (char *args
, int fromtty
)
570 serial_ttystate ttystate
;
571 serial_t port_desc
; /* TTY port */
576 fprintf_unfiltered (gdb_stderr
, "This command takes no args. They have been ignored.\n");
578 printf_unfiltered ("[Entering connect mode. Use ~. or ~^D to escape]\n");
580 tty_desc
= SERIAL_FDOPEN (0);
581 port_desc
= last_serial_opened
;
583 ttystate
= SERIAL_GET_TTY_STATE (tty_desc
);
585 SERIAL_RAW (tty_desc
);
586 SERIAL_RAW (port_desc
);
588 make_cleanup (cleanup_tty
, ttystate
);
594 mask
= SERIAL_WAIT_2 (tty_desc
, port_desc
, -1);
602 c
= SERIAL_READCHAR (tty_desc
, 0);
604 if (c
== SERIAL_TIMEOUT
)
608 perror_with_name ("connect");
611 SERIAL_WRITE (port_desc
, &cx
, 1);
626 if (c
== '.' || c
== '\004')
640 c
= SERIAL_READCHAR (port_desc
, 0);
642 if (c
== SERIAL_TIMEOUT
)
646 perror_with_name ("connect");
650 SERIAL_WRITE (tty_desc
, &cx
, 1);
658 _initialize_serial (void)
661 add_com ("connect", class_obscure
, connect_command
,
662 "Connect the terminal directly up to the command monitor.\n\
663 Use <CR>~. or <CR>~^D to break out.");
667 (add_set_cmd ("remotelogfile", no_class
,
668 var_filename
, (char *) &serial_logfile
,
669 "Set filename for remote session recording.\n\
670 This file is used to record the remote session for future playback\n\
676 (add_set_enum_cmd ("remotelogbase", no_class
,
677 logbase_enums
, &serial_logbase
,
678 "Set numerical base for remote session logging",
682 add_show_from_set (add_set_cmd ("serial",
685 (char *)&global_serial_debug_p
,
686 "Set serial debugging.\n\
687 When non-zero, serial port debugging is enabled.", &setdebuglist
),