* CONTRIBUTE: Update.
[deliverable/binutils-gdb.git] / gdb / serial.c
CommitLineData
c906108c 1/* Generic serial interface routines
4fcf66da 2
b6ba6518
KB
3 Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
4 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
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. */
c906108c
SS
22
23#include "defs.h"
24#include <ctype.h>
25#include "serial.h"
26#include "gdb_string.h"
27#include "gdbcmd.h"
28
c2c6d25f 29extern void _initialize_serial (void);
392a587b 30
2acceee2
JM
31/* Is serial being debugged? */
32
33static int global_serial_debug_p;
34
c906108c
SS
35/* Linked list of serial I/O handlers */
36
37static struct serial_ops *serial_ops_list = NULL;
38
39/* This is the last serial stream opened. Used by connect command. */
40
819cc324 41static struct serial *last_serial_opened = NULL;
c906108c
SS
42
43/* Pointer to list of scb's. */
44
819cc324 45static struct serial *scb_base;
c906108c
SS
46
47/* Non-NULL gives filename which contains a recording of the remote session,
48 suitable for playback by gdbserver. */
49
50static char *serial_logfile = NULL;
d9fcf2fb 51static struct ui_file *serial_logfp = NULL;
c906108c 52
c2c6d25f 53static struct serial_ops *serial_interface_lookup (char *);
d9fcf2fb 54static void serial_logchar (struct ui_file *stream, int ch_type, int ch, int timeout);
53904c9e
AC
55static const char logbase_hex[] = "hex";
56static const char logbase_octal[] = "octal";
57static const char logbase_ascii[] = "ascii";
58static const char *logbase_enums[] =
c5aa993b 59{logbase_hex, logbase_octal, logbase_ascii, NULL};
53904c9e 60static const char *serial_logbase = logbase_ascii;
65e2f740
AC
61
62#undef XMALLOC
63#define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
c906108c 64\f
c5aa993b 65
c906108c
SS
66static int serial_current_type = 0;
67
68/* Log char CH of type CHTYPE, with TIMEOUT */
69
70/* Define bogus char to represent a BREAK. Should be careful to choose a value
71 that can't be confused with a normal char, or an error code. */
72#define SERIAL_BREAK 1235
73
74static void
d9fcf2fb 75serial_logchar (struct ui_file *stream, int ch_type, int ch, int timeout)
c906108c
SS
76{
77 if (ch_type != serial_current_type)
78 {
2acceee2 79 fprintf_unfiltered (stream, "\n%c ", ch_type);
c906108c
SS
80 serial_current_type = ch_type;
81 }
82
83 if (serial_logbase != logbase_ascii)
2acceee2 84 fputc_unfiltered (' ', stream);
c906108c
SS
85
86 switch (ch)
87 {
88 case SERIAL_TIMEOUT:
2acceee2 89 fprintf_unfiltered (stream, "<Timeout: %d seconds>", timeout);
c906108c
SS
90 return;
91 case SERIAL_ERROR:
2acceee2 92 fprintf_unfiltered (stream, "<Error: %s>", safe_strerror (errno));
c906108c
SS
93 return;
94 case SERIAL_EOF:
2acceee2 95 fputs_unfiltered ("<Eof>", stream);
c906108c
SS
96 return;
97 case SERIAL_BREAK:
2acceee2 98 fputs_unfiltered ("<Break>", stream);
c906108c
SS
99 return;
100 default:
101 if (serial_logbase == logbase_hex)
2acceee2 102 fprintf_unfiltered (stream, "%02x", ch & 0xff);
c906108c 103 else if (serial_logbase == logbase_octal)
2acceee2 104 fprintf_unfiltered (stream, "%03o", ch & 0xff);
c906108c
SS
105 else
106 switch (ch)
107 {
c5aa993b 108 case '\\':
2acceee2 109 fputs_unfiltered ("\\\\", stream);
c5aa993b
JM
110 break;
111 case '\b':
2acceee2 112 fputs_unfiltered ("\\b", stream);
c5aa993b
JM
113 break;
114 case '\f':
2acceee2 115 fputs_unfiltered ("\\f", stream);
c5aa993b
JM
116 break;
117 case '\n':
2acceee2 118 fputs_unfiltered ("\\n", stream);
c5aa993b
JM
119 break;
120 case '\r':
2acceee2 121 fputs_unfiltered ("\\r", stream);
c5aa993b
JM
122 break;
123 case '\t':
2acceee2 124 fputs_unfiltered ("\\t", stream);
c5aa993b
JM
125 break;
126 case '\v':
2acceee2 127 fputs_unfiltered ("\\v", stream);
c5aa993b
JM
128 break;
129 default:
2acceee2 130 fprintf_unfiltered (stream, isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
c5aa993b 131 break;
c906108c
SS
132 }
133 }
134}
135
136void
c2c6d25f 137serial_log_command (const char *cmd)
c906108c
SS
138{
139 if (!serial_logfp)
140 return;
141
142 serial_current_type = 'c';
143
144 fputs_unfiltered ("\nc ", serial_logfp);
145 fputs_unfiltered (cmd, serial_logfp);
146
147 /* Make sure that the log file is as up-to-date as possible,
148 in case we are getting ready to dump core or something. */
149 gdb_flush (serial_logfp);
150}
151
c2c6d25f 152\f
c906108c 153static struct serial_ops *
c2c6d25f 154serial_interface_lookup (char *name)
c906108c
SS
155{
156 struct serial_ops *ops;
157
158 for (ops = serial_ops_list; ops; ops = ops->next)
159 if (strcmp (name, ops->name) == 0)
160 return ops;
161
162 return NULL;
163}
164
165void
c2c6d25f 166serial_add_interface (struct serial_ops *optable)
c906108c
SS
167{
168 optable->next = serial_ops_list;
169 serial_ops_list = optable;
170}
171
172/* Open up a device or a network socket, depending upon the syntax of NAME. */
173
819cc324 174struct serial *
c2c6d25f 175serial_open (const char *name)
c906108c 176{
819cc324 177 struct serial *scb;
c906108c 178 struct serial_ops *ops;
c2c6d25f 179 const char *open_name = name;
c906108c
SS
180
181 for (scb = scb_base; scb; scb = scb->next)
182 if (scb->name && strcmp (scb->name, name) == 0)
183 {
184 scb->refcnt++;
185 return scb;
186 }
187
188 if (strcmp (name, "ocd") == 0)
189 ops = serial_interface_lookup ("ocd");
190 else if (strcmp (name, "pc") == 0)
191 ops = serial_interface_lookup ("pc");
192 else if (strchr (name, ':'))
193 ops = serial_interface_lookup ("tcp");
194 else if (strncmp (name, "lpt", 3) == 0)
195 ops = serial_interface_lookup ("parallel");
43e526b9 196 else if (strncmp (name, "|", 1) == 0)
c2c6d25f
JM
197 {
198 ops = serial_interface_lookup ("pipe");
199 open_name = name + 1; /* discard ``|'' */
200 }
c906108c
SS
201 else
202 ops = serial_interface_lookup ("hardwire");
203
204 if (!ops)
205 return NULL;
206
65e2f740 207 scb = XMALLOC (struct serial);
c906108c
SS
208
209 scb->ops = ops;
210
211 scb->bufcnt = 0;
212 scb->bufp = scb->buf;
213
c2c6d25f 214 if (scb->ops->open (scb, open_name))
c906108c 215 {
b8c9b27d 216 xfree (scb);
c906108c
SS
217 return NULL;
218 }
219
4fcf66da 220 scb->name = xstrdup (name);
c906108c
SS
221 scb->next = scb_base;
222 scb->refcnt = 1;
2acceee2
JM
223 scb->debug_p = 0;
224 scb->async_state = 0;
c2c6d25f
JM
225 scb->async_handler = NULL;
226 scb->async_context = NULL;
c906108c
SS
227 scb_base = scb;
228
229 last_serial_opened = scb;
230
231 if (serial_logfile != NULL)
232 {
233 serial_logfp = gdb_fopen (serial_logfile, "w");
234 if (serial_logfp == NULL)
235 perror_with_name (serial_logfile);
236 }
237
238 return scb;
239}
240
819cc324 241struct serial *
c2c6d25f 242serial_fdopen (const int fd)
c906108c 243{
819cc324 244 struct serial *scb;
c906108c
SS
245 struct serial_ops *ops;
246
247 for (scb = scb_base; scb; scb = scb->next)
248 if (scb->fd == fd)
249 {
250 scb->refcnt++;
251 return scb;
252 }
253
254 ops = serial_interface_lookup ("hardwire");
255
256 if (!ops)
257 return NULL;
258
65e2f740 259 scb = XMALLOC (struct serial);
c906108c
SS
260
261 scb->ops = ops;
262
263 scb->bufcnt = 0;
264 scb->bufp = scb->buf;
265
266 scb->fd = fd;
267
268 scb->name = NULL;
269 scb->next = scb_base;
270 scb->refcnt = 1;
2acceee2
JM
271 scb->debug_p = 0;
272 scb->async_state = 0;
c2c6d25f
JM
273 scb->async_handler = NULL;
274 scb->async_context = NULL;
c906108c
SS
275 scb_base = scb;
276
277 last_serial_opened = scb;
278
279 return scb;
280}
281
c2c6d25f 282static void
819cc324 283do_serial_close (struct serial *scb, int really_close)
c906108c 284{
819cc324 285 struct serial *tmp_scb;
c906108c
SS
286
287 last_serial_opened = NULL;
288
289 if (serial_logfp)
290 {
291 fputs_unfiltered ("\nEnd of log\n", serial_logfp);
292 serial_current_type = 0;
293
294 /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr? */
d9fcf2fb 295 ui_file_delete (serial_logfp);
c906108c
SS
296 serial_logfp = NULL;
297 }
298
299/* This is bogus. It's not our fault if you pass us a bad scb...! Rob, you
300 should fix your code instead. */
301
302 if (!scb)
303 return;
304
305 scb->refcnt--;
306 if (scb->refcnt > 0)
307 return;
308
c2c6d25f
JM
309 /* ensure that the FD has been taken out of async mode */
310 if (scb->async_handler != NULL)
311 serial_async (scb, NULL, NULL);
312
c906108c
SS
313 if (really_close)
314 scb->ops->close (scb);
315
316 if (scb->name)
b8c9b27d 317 xfree (scb->name);
c906108c
SS
318
319 if (scb_base == scb)
320 scb_base = scb_base->next;
321 else
322 for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
323 {
324 if (tmp_scb->next != scb)
325 continue;
326
327 tmp_scb->next = tmp_scb->next->next;
328 break;
329 }
330
b8c9b27d 331 xfree (scb);
c906108c
SS
332}
333
c2c6d25f 334void
819cc324 335serial_close (struct serial *scb)
c2c6d25f
JM
336{
337 do_serial_close (scb, 1);
338}
339
340void
819cc324 341serial_un_fdopen (struct serial *scb)
c2c6d25f
JM
342{
343 do_serial_close (scb, 0);
344}
345
346int
819cc324 347serial_readchar (struct serial *scb, int timeout)
c2c6d25f
JM
348{
349 int ch;
350
2df3850c
JM
351 /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC
352 code is finished. */
2cd58942 353 if (0 && serial_is_async_p (scb) && timeout < 0)
8e65ff28
AC
354 internal_error (__FILE__, __LINE__,
355 "serial_readchar: blocking read in async mode");
2df3850c 356
c2c6d25f
JM
357 ch = scb->ops->readchar (scb, timeout);
358 if (serial_logfp != NULL)
359 {
2acceee2 360 serial_logchar (serial_logfp, 'r', ch, timeout);
c2c6d25f
JM
361
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);
365 }
2cd58942 366 if (serial_debug_p (scb))
2acceee2
JM
367 {
368 fprintf_unfiltered (gdb_stdlog, "[");
369 serial_logchar (gdb_stdlog, 'r', ch, timeout);
370 fprintf_unfiltered (gdb_stdlog, "]");
371 gdb_flush (gdb_stdlog);
372 }
c2c6d25f
JM
373
374 return (ch);
375}
376
377int
819cc324 378serial_write (struct serial *scb, const char *str, int len)
c2c6d25f
JM
379{
380 if (serial_logfp != NULL)
381 {
382 int count;
383
384 for (count = 0; count < len; count++)
2acceee2 385 serial_logchar (serial_logfp, 'w', str[count] & 0xff, 0);
c2c6d25f
JM
386
387 /* Make sure that the log file is as up-to-date as possible,
388 in case we are getting ready to dump core or something. */
389 gdb_flush (serial_logfp);
390 }
391
392 return (scb->ops->write (scb, str, len));
393}
394
395void
819cc324 396serial_printf (struct serial *desc, const char *format,...)
c2c6d25f
JM
397{
398 va_list args;
399 char *buf;
400 va_start (args, format);
401
12859c09 402 xvasprintf (&buf, format, args);
2cd58942 403 serial_write (desc, buf, strlen (buf));
c2c6d25f 404
b8c9b27d 405 xfree (buf);
c2c6d25f
JM
406 va_end (args);
407}
408
409int
819cc324 410serial_drain_output (struct serial *scb)
c2c6d25f
JM
411{
412 return scb->ops->drain_output (scb);
413}
414
415int
819cc324 416serial_flush_output (struct serial *scb)
c2c6d25f
JM
417{
418 return scb->ops->flush_output (scb);
419}
420
421int
819cc324 422serial_flush_input (struct serial *scb)
c2c6d25f
JM
423{
424 return scb->ops->flush_input (scb);
425}
426
427int
819cc324 428serial_send_break (struct serial *scb)
c2c6d25f
JM
429{
430 if (serial_logfp != NULL)
2acceee2 431 serial_logchar (serial_logfp, 'w', SERIAL_BREAK, 0);
c2c6d25f
JM
432
433 return (scb->ops->send_break (scb));
434}
435
436void
819cc324 437serial_raw (struct serial *scb)
c2c6d25f
JM
438{
439 scb->ops->go_raw (scb);
440}
441
442serial_ttystate
819cc324 443serial_get_tty_state (struct serial *scb)
c2c6d25f
JM
444{
445 return scb->ops->get_tty_state (scb);
446}
447
448int
819cc324 449serial_set_tty_state (struct serial *scb, serial_ttystate ttystate)
c2c6d25f
JM
450{
451 return scb->ops->set_tty_state (scb, ttystate);
452}
453
454void
819cc324 455serial_print_tty_state (struct serial *scb,
c2c6d25f 456 serial_ttystate ttystate,
d9fcf2fb 457 struct ui_file *stream)
c2c6d25f
JM
458{
459 scb->ops->print_tty_state (scb, ttystate, stream);
460}
461
462int
819cc324 463serial_noflush_set_tty_state (struct serial *scb,
c2c6d25f
JM
464 serial_ttystate new_ttystate,
465 serial_ttystate old_ttystate)
466{
467 return scb->ops->noflush_set_tty_state (scb, new_ttystate, old_ttystate);
468}
469
470int
819cc324 471serial_setbaudrate (struct serial *scb, int rate)
c2c6d25f
JM
472{
473 return scb->ops->setbaudrate (scb, rate);
474}
475
476int
819cc324 477serial_setstopbits (struct serial *scb, int num)
c2c6d25f
JM
478{
479 return scb->ops->setstopbits (scb, num);
480}
481
482int
819cc324 483serial_can_async_p (struct serial *scb)
c2c6d25f
JM
484{
485 return (scb->ops->async != NULL);
486}
487
488int
819cc324 489serial_is_async_p (struct serial *scb)
c2c6d25f
JM
490{
491 return (scb->ops->async != NULL) && (scb->async_handler != NULL);
492}
493
494void
819cc324 495serial_async (struct serial *scb,
c2c6d25f
JM
496 serial_event_ftype *handler,
497 void *context)
498{
499 /* Only change mode if there is a need. */
500 if ((scb->async_handler == NULL)
501 != (handler == NULL))
502 scb->ops->async (scb, handler != NULL);
503 scb->async_handler = handler;
504 scb->async_context = context;
505}
506
507int
819cc324 508deprecated_serial_fd (struct serial *scb)
c2c6d25f
JM
509{
510 /* FIXME: should this output a warning that deprecated code is being
511 called? */
512 if (scb->fd < 0)
513 {
8e65ff28
AC
514 internal_error (__FILE__, __LINE__,
515 "serial: FD not valid");
c2c6d25f
JM
516 }
517 return scb->fd; /* sigh */
518}
519
2acceee2 520void
819cc324 521serial_debug (struct serial *scb, int debug_p)
2acceee2
JM
522{
523 scb->debug_p = debug_p;
524}
525
526int
819cc324 527serial_debug_p (struct serial *scb)
2acceee2
JM
528{
529 return scb->debug_p || global_serial_debug_p;
530}
531
532
c906108c 533#if 0
819cc324
AC
534/* The connect command is #if 0 because I hadn't thought of an elegant
535 way to wait for I/O on two `struct serial *'s simultaneously. Two
536 solutions came to mind:
c906108c 537
c5aa993b
JM
538 1) Fork, and have have one fork handle the to user direction,
539 and have the other hand the to target direction. This
540 obviously won't cut it for MSDOS.
c906108c 541
c5aa993b
JM
542 2) Use something like select. This assumes that stdin and
543 the target side can both be waited on via the same
544 mechanism. This may not be true for DOS, if GDB is
545 talking to the target via a TCP socket.
819cc324 546 -grossman, 8 Jun 93 */
c906108c
SS
547
548/* Connect the user directly to the remote system. This command acts just like
549 the 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
550
819cc324 551static struct serial *tty_desc; /* Controlling terminal */
c906108c
SS
552
553static void
c2c6d25f 554cleanup_tty (serial_ttystate ttystate)
c906108c
SS
555{
556 printf_unfiltered ("\r\n[Exiting connect mode]\r\n");
2cd58942 557 serial_set_tty_state (tty_desc, ttystate);
b8c9b27d 558 xfree (ttystate);
2cd58942 559 serial_close (tty_desc);
c906108c
SS
560}
561
562static void
c2c6d25f 563connect_command (char *args, int fromtty)
c906108c
SS
564{
565 int c;
566 char cur_esc = 0;
567 serial_ttystate ttystate;
819cc324 568 struct serial *port_desc; /* TTY port */
c906108c 569
c5aa993b 570 dont_repeat ();
c906108c
SS
571
572 if (args)
c5aa993b
JM
573 fprintf_unfiltered (gdb_stderr, "This command takes no args. They have been ignored.\n");
574
575 printf_unfiltered ("[Entering connect mode. Use ~. or ~^D to escape]\n");
c906108c 576
2cd58942 577 tty_desc = serial_fdopen (0);
c906108c
SS
578 port_desc = last_serial_opened;
579
2cd58942 580 ttystate = serial_get_tty_state (tty_desc);
c906108c 581
2cd58942
AC
582 serial_raw (tty_desc);
583 serial_raw (port_desc);
c906108c
SS
584
585 make_cleanup (cleanup_tty, ttystate);
586
587 while (1)
588 {
589 int mask;
590
2cd58942 591 mask = serial_wait_2 (tty_desc, port_desc, -1);
c906108c
SS
592
593 if (mask & 2)
594 { /* tty input */
595 char cx;
596
597 while (1)
598 {
2cd58942 599 c = serial_readchar (tty_desc, 0);
c906108c
SS
600
601 if (c == SERIAL_TIMEOUT)
c5aa993b 602 break;
c906108c
SS
603
604 if (c < 0)
c5aa993b 605 perror_with_name ("connect");
c906108c
SS
606
607 cx = c;
2cd58942 608 serial_write (port_desc, &cx, 1);
c906108c
SS
609
610 switch (cur_esc)
611 {
612 case 0:
613 if (c == '\r')
614 cur_esc = c;
615 break;
616 case '\r':
617 if (c == '~')
618 cur_esc = c;
619 else
620 cur_esc = 0;
621 break;
622 case '~':
623 if (c == '.' || c == '\004')
624 return;
625 else
626 cur_esc = 0;
627 }
628 }
629 }
630
631 if (mask & 1)
632 { /* Port input */
633 char cx;
634
635 while (1)
636 {
2cd58942 637 c = serial_readchar (port_desc, 0);
c906108c
SS
638
639 if (c == SERIAL_TIMEOUT)
c5aa993b 640 break;
c906108c
SS
641
642 if (c < 0)
c5aa993b 643 perror_with_name ("connect");
c906108c
SS
644
645 cx = c;
646
2cd58942 647 serial_write (tty_desc, &cx, 1);
c906108c
SS
648 }
649 }
650 }
651}
652#endif /* 0 */
653
c906108c 654void
c2c6d25f 655_initialize_serial (void)
c906108c
SS
656{
657#if 0
658 add_com ("connect", class_obscure, connect_command,
659 "Connect the terminal directly up to the command monitor.\n\
660Use <CR>~. or <CR>~^D to break out.");
661#endif /* 0 */
662
c5aa993b 663 add_show_from_set
c906108c
SS
664 (add_set_cmd ("remotelogfile", no_class,
665 var_filename, (char *) &serial_logfile,
666 "Set filename for remote session recording.\n\
667This file is used to record the remote session for future playback\n\
c5aa993b 668by gdbserver.",
c906108c
SS
669 &setlist),
670 &showlist);
671
c5aa993b 672 add_show_from_set
c906108c 673 (add_set_enum_cmd ("remotelogbase", no_class,
1ed2a135 674 logbase_enums, &serial_logbase,
c906108c
SS
675 "Set numerical base for remote session logging",
676 &setlist),
677 &showlist);
2acceee2 678
5d161b24 679 add_show_from_set (add_set_cmd ("serial",
2acceee2
JM
680 class_maintenance,
681 var_zinteger,
682 (char *)&global_serial_debug_p,
683 "Set serial debugging.\n\
5d161b24
DB
684When non-zero, serial port debugging is enabled.", &setdebuglist),
685 &showdebuglist);
c906108c 686}
This page took 0.168232 seconds and 4 git commands to generate.