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