]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/serial.c
s/struct _serial_t/struct serial/
[thirdparty/binutils-gdb.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 serial_t last_serial_opened = NULL;
42
43 /* Pointer to list of scb's. */
44
45 static 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
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
67 static 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
75 static void
76 serial_logchar (struct ui_file *stream, int ch_type, int ch, int timeout)
77 {
78 if (ch_type != serial_current_type)
79 {
80 fprintf_unfiltered (stream, "\n%c ", ch_type);
81 serial_current_type = ch_type;
82 }
83
84 if (serial_logbase != logbase_ascii)
85 fputc_unfiltered (' ', stream);
86
87 switch (ch)
88 {
89 case SERIAL_TIMEOUT:
90 fprintf_unfiltered (stream, "<Timeout: %d seconds>", timeout);
91 return;
92 case SERIAL_ERROR:
93 fprintf_unfiltered (stream, "<Error: %s>", safe_strerror (errno));
94 return;
95 case SERIAL_EOF:
96 fputs_unfiltered ("<Eof>", stream);
97 return;
98 case SERIAL_BREAK:
99 fputs_unfiltered ("<Break>", stream);
100 return;
101 default:
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);
106 else
107 switch (ch)
108 {
109 case '\\':
110 fputs_unfiltered ("\\\\", stream);
111 break;
112 case '\b':
113 fputs_unfiltered ("\\b", stream);
114 break;
115 case '\f':
116 fputs_unfiltered ("\\f", stream);
117 break;
118 case '\n':
119 fputs_unfiltered ("\\n", stream);
120 break;
121 case '\r':
122 fputs_unfiltered ("\\r", stream);
123 break;
124 case '\t':
125 fputs_unfiltered ("\\t", stream);
126 break;
127 case '\v':
128 fputs_unfiltered ("\\v", stream);
129 break;
130 default:
131 fprintf_unfiltered (stream, isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
132 break;
133 }
134 }
135 }
136
137 void
138 serial_log_command (const char *cmd)
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
153 \f
154 static struct serial_ops *
155 serial_interface_lookup (char *name)
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
166 void
167 serial_add_interface (struct serial_ops *optable)
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
175 serial_t
176 serial_open (const char *name)
177 {
178 serial_t scb;
179 struct serial_ops *ops;
180 const char *open_name = name;
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");
197 else if (strncmp (name, "|", 1) == 0)
198 {
199 ops = serial_interface_lookup ("pipe");
200 open_name = name + 1; /* discard ``|'' */
201 }
202 else
203 ops = serial_interface_lookup ("hardwire");
204
205 if (!ops)
206 return NULL;
207
208 scb = XMALLOC (struct serial);
209
210 scb->ops = ops;
211
212 scb->bufcnt = 0;
213 scb->bufp = scb->buf;
214
215 if (scb->ops->open (scb, open_name))
216 {
217 xfree (scb);
218 return NULL;
219 }
220
221 scb->name = xstrdup (name);
222 scb->next = scb_base;
223 scb->refcnt = 1;
224 scb->debug_p = 0;
225 scb->async_state = 0;
226 scb->async_handler = NULL;
227 scb->async_context = NULL;
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
242 serial_t
243 serial_fdopen (const int fd)
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
260 scb = XMALLOC (struct serial);
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;
272 scb->debug_p = 0;
273 scb->async_state = 0;
274 scb->async_handler = NULL;
275 scb->async_context = NULL;
276 scb_base = scb;
277
278 last_serial_opened = scb;
279
280 return scb;
281 }
282
283 static void
284 do_serial_close (serial_t scb, int really_close)
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? */
296 ui_file_delete (serial_logfp);
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
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
314 if (really_close)
315 scb->ops->close (scb);
316
317 if (scb->name)
318 xfree (scb->name);
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
332 xfree (scb);
333 }
334
335 void
336 serial_close (serial_t scb)
337 {
338 do_serial_close (scb, 1);
339 }
340
341 void
342 serial_un_fdopen (serial_t scb)
343 {
344 do_serial_close (scb, 0);
345 }
346
347 int
348 serial_readchar (serial_t scb, int timeout)
349 {
350 int ch;
351
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)
355 internal_error (__FILE__, __LINE__,
356 "serial_readchar: blocking read in async mode");
357
358 ch = scb->ops->readchar (scb, timeout);
359 if (serial_logfp != NULL)
360 {
361 serial_logchar (serial_logfp, 'r', ch, timeout);
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 }
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 }
374
375 return (ch);
376 }
377
378 int
379 serial_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++)
386 serial_logchar (serial_logfp, 'w', str[count] & 0xff, 0);
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
396 void
397 serial_printf (serial_t desc, const char *format,...)
398 {
399 va_list args;
400 char *buf;
401 va_start (args, format);
402
403 xvasprintf (&buf, format, args);
404 SERIAL_WRITE (desc, buf, strlen (buf));
405
406 xfree (buf);
407 va_end (args);
408 }
409
410 int
411 serial_drain_output (serial_t scb)
412 {
413 return scb->ops->drain_output (scb);
414 }
415
416 int
417 serial_flush_output (serial_t scb)
418 {
419 return scb->ops->flush_output (scb);
420 }
421
422 int
423 serial_flush_input (serial_t scb)
424 {
425 return scb->ops->flush_input (scb);
426 }
427
428 int
429 serial_send_break (serial_t scb)
430 {
431 if (serial_logfp != NULL)
432 serial_logchar (serial_logfp, 'w', SERIAL_BREAK, 0);
433
434 return (scb->ops->send_break (scb));
435 }
436
437 void
438 serial_raw (serial_t scb)
439 {
440 scb->ops->go_raw (scb);
441 }
442
443 serial_ttystate
444 serial_get_tty_state (serial_t scb)
445 {
446 return scb->ops->get_tty_state (scb);
447 }
448
449 int
450 serial_set_tty_state (serial_t scb, serial_ttystate ttystate)
451 {
452 return scb->ops->set_tty_state (scb, ttystate);
453 }
454
455 void
456 serial_print_tty_state (serial_t scb,
457 serial_ttystate ttystate,
458 struct ui_file *stream)
459 {
460 scb->ops->print_tty_state (scb, ttystate, stream);
461 }
462
463 int
464 serial_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
471 int
472 serial_setbaudrate (serial_t scb, int rate)
473 {
474 return scb->ops->setbaudrate (scb, rate);
475 }
476
477 int
478 serial_setstopbits (serial_t scb, int num)
479 {
480 return scb->ops->setstopbits (scb, num);
481 }
482
483 int
484 serial_can_async_p (serial_t scb)
485 {
486 return (scb->ops->async != NULL);
487 }
488
489 int
490 serial_is_async_p (serial_t scb)
491 {
492 return (scb->ops->async != NULL) && (scb->async_handler != NULL);
493 }
494
495 void
496 serial_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
508 int
509 deprecated_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 {
515 internal_error (__FILE__, __LINE__,
516 "serial: FD not valid");
517 }
518 return scb->fd; /* sigh */
519 }
520
521 void
522 serial_debug (serial_t scb, int debug_p)
523 {
524 scb->debug_p = debug_p;
525 }
526
527 int
528 serial_debug_p (serial_t scb)
529 {
530 return scb->debug_p || global_serial_debug_p;
531 }
532
533
534 #if 0
535 /*
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:
539
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.
543
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 */
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
554 static serial_t tty_desc; /* Controlling terminal */
555
556 static void
557 cleanup_tty (serial_ttystate ttystate)
558 {
559 printf_unfiltered ("\r\n[Exiting connect mode]\r\n");
560 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
561 xfree (ttystate);
562 SERIAL_CLOSE (tty_desc);
563 }
564
565 static void
566 connect_command (char *args, int fromtty)
567 {
568 int c;
569 char cur_esc = 0;
570 serial_ttystate ttystate;
571 serial_t port_desc; /* TTY port */
572
573 dont_repeat ();
574
575 if (args)
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");
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 {
602 c = SERIAL_READCHAR (tty_desc, 0);
603
604 if (c == SERIAL_TIMEOUT)
605 break;
606
607 if (c < 0)
608 perror_with_name ("connect");
609
610 cx = c;
611 SERIAL_WRITE (port_desc, &cx, 1);
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 {
640 c = SERIAL_READCHAR (port_desc, 0);
641
642 if (c == SERIAL_TIMEOUT)
643 break;
644
645 if (c < 0)
646 perror_with_name ("connect");
647
648 cx = c;
649
650 SERIAL_WRITE (tty_desc, &cx, 1);
651 }
652 }
653 }
654 }
655 #endif /* 0 */
656
657 void
658 _initialize_serial (void)
659 {
660 #if 0
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.");
664 #endif /* 0 */
665
666 add_show_from_set
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\
671 by gdbserver.",
672 &setlist),
673 &showlist);
674
675 add_show_from_set
676 (add_set_enum_cmd ("remotelogbase", no_class,
677 logbase_enums, &serial_logbase,
678 "Set numerical base for remote session logging",
679 &setlist),
680 &showlist);
681
682 add_show_from_set (add_set_cmd ("serial",
683 class_maintenance,
684 var_zinteger,
685 (char *)&global_serial_debug_p,
686 "Set serial debugging.\n\
687 When non-zero, serial port debugging is enabled.", &setdebuglist),
688 &showdebuglist);
689 }