* serial.h (SERIAL_ASYNC): Delete.
[platform/upstream/binutils.git] / gdb / serial.c
1 /* Generic serial interface routines
2
3    Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
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.
12
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.
17
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.  */
22
23 #include "defs.h"
24 #include <ctype.h>
25 #include "serial.h"
26 #include "gdb_string.h"
27 #include "gdbcmd.h"
28
29 extern void _initialize_serial (void);
30
31 /* Is serial being debugged? */
32
33 static int global_serial_debug_p;
34
35 /* Linked list of serial I/O handlers */
36
37 static struct serial_ops *serial_ops_list = NULL;
38
39 /* This is the last serial stream opened.  Used by connect command. */
40
41 static struct serial *last_serial_opened = NULL;
42
43 /* Pointer to list of scb's. */
44
45 static struct serial *scb_base;
46
47 /* Non-NULL gives filename which contains a recording of the remote session,
48    suitable for playback by gdbserver. */
49
50 static char *serial_logfile = NULL;
51 static struct ui_file *serial_logfp = NULL;
52
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;
61
62 #undef XMALLOC
63 #define XMALLOC(TYPE) ((TYPE*) xmalloc (sizeof (TYPE)))
64 \f
65
66 static 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
74 static void
75 serial_logchar (struct ui_file *stream, int ch_type, int ch, int timeout)
76 {
77   if (ch_type != serial_current_type)
78     {
79       fprintf_unfiltered (stream, "\n%c ", ch_type);
80       serial_current_type = ch_type;
81     }
82
83   if (serial_logbase != logbase_ascii)
84     fputc_unfiltered (' ', stream);
85
86   switch (ch)
87     {
88     case SERIAL_TIMEOUT:
89       fprintf_unfiltered (stream, "<Timeout: %d seconds>", timeout);
90       return;
91     case SERIAL_ERROR:
92       fprintf_unfiltered (stream, "<Error: %s>", safe_strerror (errno));
93       return;
94     case SERIAL_EOF:
95       fputs_unfiltered ("<Eof>", stream);
96       return;
97     case SERIAL_BREAK:
98       fputs_unfiltered ("<Break>", stream);
99       return;
100     default:
101       if (serial_logbase == logbase_hex)
102         fprintf_unfiltered (stream, "%02x", ch & 0xff);
103       else if (serial_logbase == logbase_octal)
104         fprintf_unfiltered (stream, "%03o", ch & 0xff);
105       else
106         switch (ch)
107           {
108           case '\\':
109             fputs_unfiltered ("\\\\", stream);
110             break;
111           case '\b':
112             fputs_unfiltered ("\\b", stream);
113             break;
114           case '\f':
115             fputs_unfiltered ("\\f", stream);
116             break;
117           case '\n':
118             fputs_unfiltered ("\\n", stream);
119             break;
120           case '\r':
121             fputs_unfiltered ("\\r", stream);
122             break;
123           case '\t':
124             fputs_unfiltered ("\\t", stream);
125             break;
126           case '\v':
127             fputs_unfiltered ("\\v", stream);
128             break;
129           default:
130             fprintf_unfiltered (stream, isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
131             break;
132           }
133     }
134 }
135
136 void
137 serial_log_command (const char *cmd)
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
152 \f
153 static struct serial_ops *
154 serial_interface_lookup (char *name)
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
165 void
166 serial_add_interface (struct serial_ops *optable)
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
174 struct serial *
175 serial_open (const char *name)
176 {
177   struct serial *scb;
178   struct serial_ops *ops;
179   const char *open_name = name;
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");
196   else if (strncmp (name, "|", 1) == 0)
197     {
198       ops = serial_interface_lookup ("pipe");
199       open_name = name + 1; /* discard ``|'' */
200     }
201   else
202     ops = serial_interface_lookup ("hardwire");
203
204   if (!ops)
205     return NULL;
206
207   scb = XMALLOC (struct serial);
208
209   scb->ops = ops;
210
211   scb->bufcnt = 0;
212   scb->bufp = scb->buf;
213
214   if (scb->ops->open (scb, open_name))
215     {
216       xfree (scb);
217       return NULL;
218     }
219
220   scb->name = xstrdup (name);
221   scb->next = scb_base;
222   scb->refcnt = 1;
223   scb->debug_p = 0;
224   scb->async_state = 0;
225   scb->async_handler = NULL;
226   scb->async_context = NULL;
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
241 struct serial *
242 serial_fdopen (const int fd)
243 {
244   struct serial *scb;
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
259   scb = XMALLOC (struct serial);
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;
271   scb->debug_p = 0;
272   scb->async_state = 0;
273   scb->async_handler = NULL;
274   scb->async_context = NULL;
275   scb_base = scb;
276
277   last_serial_opened = scb;
278
279   return scb;
280 }
281
282 static void
283 do_serial_close (struct serial *scb, int really_close)
284 {
285   struct serial *tmp_scb;
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? */
295       ui_file_delete (serial_logfp);
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
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
313   if (really_close)
314     scb->ops->close (scb);
315
316   if (scb->name)
317     xfree (scb->name);
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
331   xfree (scb);
332 }
333
334 void
335 serial_close (struct serial *scb)
336 {
337   do_serial_close (scb, 1);
338 }
339
340 void
341 serial_un_fdopen (struct serial *scb)
342 {
343   do_serial_close (scb, 0);
344 }
345
346 int
347 serial_readchar (struct serial *scb, int timeout)
348 {
349   int ch;
350
351   /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC
352      code is finished. */
353   if (0 && serial_is_async_p (scb) && timeout < 0)
354     internal_error (__FILE__, __LINE__,
355                     "serial_readchar: blocking read in async mode");
356
357   ch = scb->ops->readchar (scb, timeout);
358   if (serial_logfp != NULL)
359     {
360       serial_logchar (serial_logfp, 'r', ch, timeout);
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     }
366   if (serial_debug_p (scb))
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     }
373
374   return (ch);
375 }
376
377 int
378 serial_write (struct serial *scb, const char *str, int len)
379 {
380   if (serial_logfp != NULL)
381     {
382       int count;
383
384       for (count = 0; count < len; count++)
385         serial_logchar (serial_logfp, 'w', str[count] & 0xff, 0);
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
395 void
396 serial_printf (struct serial *desc, const char *format,...)
397 {
398   va_list args;
399   char *buf;
400   va_start (args, format);
401
402   xvasprintf (&buf, format, args);
403   serial_write (desc, buf, strlen (buf));
404
405   xfree (buf);
406   va_end (args);
407 }
408
409 int
410 serial_drain_output (struct serial *scb)
411 {
412   return scb->ops->drain_output (scb);
413 }
414
415 int
416 serial_flush_output (struct serial *scb)
417 {
418   return scb->ops->flush_output (scb);
419 }
420
421 int
422 serial_flush_input (struct serial *scb)
423 {
424   return scb->ops->flush_input (scb);
425 }
426
427 int
428 serial_send_break (struct serial *scb)
429 {
430   if (serial_logfp != NULL)
431     serial_logchar (serial_logfp, 'w', SERIAL_BREAK, 0);
432
433   return (scb->ops->send_break (scb));
434 }
435
436 void
437 serial_raw (struct serial *scb)
438 {
439   scb->ops->go_raw (scb);
440 }
441
442 serial_ttystate
443 serial_get_tty_state (struct serial *scb)
444 {
445   return scb->ops->get_tty_state (scb);
446 }
447
448 int
449 serial_set_tty_state (struct serial *scb, serial_ttystate ttystate)
450 {
451   return scb->ops->set_tty_state (scb, ttystate);
452 }
453
454 void
455 serial_print_tty_state (struct serial *scb,
456                         serial_ttystate ttystate,
457                         struct ui_file *stream)
458 {
459   scb->ops->print_tty_state (scb, ttystate, stream);
460 }
461
462 int
463 serial_noflush_set_tty_state (struct serial *scb,
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
470 int
471 serial_setbaudrate (struct serial *scb, int rate)
472 {
473   return scb->ops->setbaudrate (scb, rate);
474 }
475
476 int
477 serial_setstopbits (struct serial *scb, int num)
478 {
479   return scb->ops->setstopbits (scb, num);
480 }
481
482 int
483 serial_can_async_p (struct serial *scb)
484 {
485   return (scb->ops->async != NULL);
486 }
487
488 int
489 serial_is_async_p (struct serial *scb)
490 {
491   return (scb->ops->async != NULL) && (scb->async_handler != NULL);
492 }
493
494 void
495 serial_async (struct serial *scb,
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
507 int
508 deprecated_serial_fd (struct serial *scb)
509 {
510   /* FIXME: should this output a warning that deprecated code is being
511      called? */
512   if (scb->fd < 0)
513     {
514       internal_error (__FILE__, __LINE__,
515                       "serial: FD not valid");
516     }
517   return scb->fd; /* sigh */
518 }
519
520 void
521 serial_debug (struct serial *scb, int debug_p)
522 {
523   scb->debug_p = debug_p;
524 }
525
526 int
527 serial_debug_p (struct serial *scb)
528 {
529   return scb->debug_p || global_serial_debug_p;
530 }
531
532
533 #if 0
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:
537
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.
541
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.
546    -grossman, 8 Jun 93 */
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
551 static struct serial *tty_desc; /* Controlling terminal */
552
553 static void
554 cleanup_tty (serial_ttystate ttystate)
555 {
556   printf_unfiltered ("\r\n[Exiting connect mode]\r\n");
557   serial_set_tty_state (tty_desc, ttystate);
558   xfree (ttystate);
559   serial_close (tty_desc);
560 }
561
562 static void
563 connect_command (char *args, int fromtty)
564 {
565   int c;
566   char cur_esc = 0;
567   serial_ttystate ttystate;
568   struct serial *port_desc;             /* TTY port */
569
570   dont_repeat ();
571
572   if (args)
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");
576
577   tty_desc = serial_fdopen (0);
578   port_desc = last_serial_opened;
579
580   ttystate = serial_get_tty_state (tty_desc);
581
582   serial_raw (tty_desc);
583   serial_raw (port_desc);
584
585   make_cleanup (cleanup_tty, ttystate);
586
587   while (1)
588     {
589       int mask;
590
591       mask = serial_wait_2 (tty_desc, port_desc, -1);
592
593       if (mask & 2)
594         {                       /* tty input */
595           char cx;
596
597           while (1)
598             {
599               c = serial_readchar (tty_desc, 0);
600
601               if (c == SERIAL_TIMEOUT)
602                 break;
603
604               if (c < 0)
605                 perror_with_name ("connect");
606
607               cx = c;
608               serial_write (port_desc, &cx, 1);
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             {
637               c = serial_readchar (port_desc, 0);
638
639               if (c == SERIAL_TIMEOUT)
640                 break;
641
642               if (c < 0)
643                 perror_with_name ("connect");
644
645               cx = c;
646
647               serial_write (tty_desc, &cx, 1);
648             }
649         }
650     }
651 }
652 #endif /* 0 */
653
654 void
655 _initialize_serial (void)
656 {
657 #if 0
658   add_com ("connect", class_obscure, connect_command,
659            "Connect the terminal directly up to the command monitor.\n\
660 Use <CR>~. or <CR>~^D to break out.");
661 #endif /* 0 */
662
663   add_show_from_set
664     (add_set_cmd ("remotelogfile", no_class,
665                   var_filename, (char *) &serial_logfile,
666                   "Set filename for remote session recording.\n\
667 This file is used to record the remote session for future playback\n\
668 by gdbserver.",
669                   &setlist),
670      &showlist);
671
672   add_show_from_set
673     (add_set_enum_cmd ("remotelogbase", no_class,
674                        logbase_enums, &serial_logbase,
675                        "Set numerical base for remote session logging",
676                        &setlist),
677      &showlist);
678
679   add_show_from_set (add_set_cmd ("serial",
680                                   class_maintenance,
681                                   var_zinteger,
682                                   (char *)&global_serial_debug_p,
683                                   "Set serial debugging.\n\
684 When non-zero, serial port debugging is enabled.", &setdebuglist),
685                      &showdebuglist);
686 }