gdb/serial.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Generic serial interface routines

  2.    Copyright (C) 1992-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include <ctype.h>
  16. #include "serial.h"
  17. #include "gdbcmd.h"
  18. #include "cli/cli-utils.h"

  19. extern void _initialize_serial (void);

  20. /* Is serial being debugged?  */

  21. static unsigned int global_serial_debug_p;

  22. typedef const struct serial_ops *serial_ops_p;
  23. DEF_VEC_P (serial_ops_p);

  24. /* Serial I/O handlers.  */

  25. VEC (serial_ops_p) *serial_ops_list = NULL;

  26. /* Pointer to list of scb's.  */

  27. static struct serial *scb_base;

  28. /* Non-NULL gives filename which contains a recording of the remote session,
  29.    suitable for playback by gdbserver.  */

  30. static char *serial_logfile = NULL;
  31. static struct ui_file *serial_logfp = NULL;

  32. static const struct serial_ops *serial_interface_lookup (const char *);
  33. static void serial_logchar (struct ui_file *stream,
  34.                             int ch_type, int ch, int timeout);
  35. static const char logbase_hex[] = "hex";
  36. static const char logbase_octal[] = "octal";
  37. static const char logbase_ascii[] = "ascii";
  38. static const char *const logbase_enums[] =
  39. {logbase_hex, logbase_octal, logbase_ascii, NULL};
  40. static const char *serial_logbase = logbase_ascii;


  41. static int serial_current_type = 0;

  42. /* Log char CH of type CHTYPE, with TIMEOUT.  */

  43. /* Define bogus char to represent a BREAK.  Should be careful to choose a value
  44.    that can't be confused with a normal char, or an error code.  */
  45. #define SERIAL_BREAK 1235

  46. static void
  47. serial_logchar (struct ui_file *stream, int ch_type, int ch, int timeout)
  48. {
  49.   if (ch_type != serial_current_type)
  50.     {
  51.       fprintf_unfiltered (stream, "\n%c ", ch_type);
  52.       serial_current_type = ch_type;
  53.     }

  54.   if (serial_logbase != logbase_ascii)
  55.     fputc_unfiltered (' ', stream);

  56.   switch (ch)
  57.     {
  58.     case SERIAL_TIMEOUT:
  59.       fprintf_unfiltered (stream, "<Timeout: %d seconds>", timeout);
  60.       return;
  61.     case SERIAL_ERROR:
  62.       fprintf_unfiltered (stream, "<Error: %s>", safe_strerror (errno));
  63.       return;
  64.     case SERIAL_EOF:
  65.       fputs_unfiltered ("<Eof>", stream);
  66.       return;
  67.     case SERIAL_BREAK:
  68.       fputs_unfiltered ("<Break>", stream);
  69.       return;
  70.     default:
  71.       if (serial_logbase == logbase_hex)
  72.         fprintf_unfiltered (stream, "%02x", ch & 0xff);
  73.       else if (serial_logbase == logbase_octal)
  74.         fprintf_unfiltered (stream, "%03o", ch & 0xff);
  75.       else
  76.         switch (ch)
  77.           {
  78.           case '\\':
  79.             fputs_unfiltered ("\\\\", stream);
  80.             break;
  81.           case '\b':
  82.             fputs_unfiltered ("\\b", stream);
  83.             break;
  84.           case '\f':
  85.             fputs_unfiltered ("\\f", stream);
  86.             break;
  87.           case '\n':
  88.             fputs_unfiltered ("\\n", stream);
  89.             break;
  90.           case '\r':
  91.             fputs_unfiltered ("\\r", stream);
  92.             break;
  93.           case '\t':
  94.             fputs_unfiltered ("\\t", stream);
  95.             break;
  96.           case '\v':
  97.             fputs_unfiltered ("\\v", stream);
  98.             break;
  99.           default:
  100.             fprintf_unfiltered (stream,
  101.                                 isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
  102.             break;
  103.           }
  104.     }
  105. }

  106. void
  107. serial_log_command (struct target_ops *self, const char *cmd)
  108. {
  109.   if (!serial_logfp)
  110.     return;

  111.   serial_current_type = 'c';

  112.   fputs_unfiltered ("\nc ", serial_logfp);
  113.   fputs_unfiltered (cmd, serial_logfp);

  114.   /* Make sure that the log file is as up-to-date as possible,
  115.      in case we are getting ready to dump core or something.  */
  116.   gdb_flush (serial_logfp);
  117. }


  118. static const struct serial_ops *
  119. serial_interface_lookup (const char *name)
  120. {
  121.   const struct serial_ops *ops;
  122.   int i;

  123.   for (i = 0; VEC_iterate (serial_ops_p, serial_ops_list, i, ops); ++i)
  124.     if (strcmp (name, ops->name) == 0)
  125.       return ops;

  126.   return NULL;
  127. }

  128. void
  129. serial_add_interface (const struct serial_ops *optable)
  130. {
  131.   VEC_safe_push (serial_ops_p, serial_ops_list, optable);
  132. }

  133. /* Return the open serial device for FD, if found, or NULL if FD is
  134.    not already opened.  */

  135. struct serial *
  136. serial_for_fd (int fd)
  137. {
  138.   struct serial *scb;

  139.   for (scb = scb_base; scb; scb = scb->next)
  140.     if (scb->fd == fd)
  141.       return scb;

  142.   return NULL;
  143. }

  144. /* Open up a device or a network socket, depending upon the syntax of NAME.  */

  145. struct serial *
  146. serial_open (const char *name)
  147. {
  148.   struct serial *scb;
  149.   const struct serial_ops *ops;
  150.   const char *open_name = name;

  151.   if (strcmp (name, "pc") == 0)
  152.     ops = serial_interface_lookup ("pc");
  153.   else if (strncmp (name, "lpt", 3) == 0)
  154.     ops = serial_interface_lookup ("parallel");
  155.   else if (strncmp (name, "|", 1) == 0)
  156.     {
  157.       ops = serial_interface_lookup ("pipe");
  158.       /* Discard ``|'' and any space before the command itself.  */
  159.       ++open_name;
  160.       open_name = skip_spaces_const (open_name);
  161.     }
  162.   /* Check for a colon, suggesting an IP address/port pair.
  163.      Do this *after* checking for all the interesting prefixes.  We
  164.      don't want to constrain the syntax of what can follow them.  */
  165.   else if (strchr (name, ':'))
  166.     ops = serial_interface_lookup ("tcp");
  167.   else
  168.     ops = serial_interface_lookup ("hardwire");

  169.   if (!ops)
  170.     return NULL;

  171.   scb = XNEW (struct serial);

  172.   scb->ops = ops;

  173.   scb->bufcnt = 0;
  174.   scb->bufp = scb->buf;
  175.   scb->error_fd = -1;
  176.   scb->refcnt = 1;

  177.   /* `...->open (...)' would get expanded by the open(2) syscall macro.  */
  178.   if ((*scb->ops->open) (scb, open_name))
  179.     {
  180.       xfree (scb);
  181.       return NULL;
  182.     }

  183.   scb->name = xstrdup (name);
  184.   scb->next = scb_base;
  185.   scb->debug_p = 0;
  186.   scb->async_state = 0;
  187.   scb->async_handler = NULL;
  188.   scb->async_context = NULL;
  189.   scb_base = scb;

  190.   if (serial_logfile != NULL)
  191.     {
  192.       serial_logfp = gdb_fopen (serial_logfile, "w");
  193.       if (serial_logfp == NULL)
  194.         perror_with_name (serial_logfile);
  195.     }

  196.   return scb;
  197. }

  198. /* Open a new serial stream using a file handle, using serial
  199.    interface ops OPS.  */

  200. static struct serial *
  201. serial_fdopen_ops (const int fd, const struct serial_ops *ops)
  202. {
  203.   struct serial *scb;

  204.   if (!ops)
  205.     {
  206.       ops = serial_interface_lookup ("terminal");
  207.       if (!ops)
  208.          ops = serial_interface_lookup ("hardwire");
  209.     }

  210.   if (!ops)
  211.     return NULL;

  212.   scb = XCNEW (struct serial);

  213.   scb->ops = ops;

  214.   scb->bufcnt = 0;
  215.   scb->bufp = scb->buf;
  216.   scb->error_fd = -1;
  217.   scb->refcnt = 1;

  218.   scb->name = NULL;
  219.   scb->next = scb_base;
  220.   scb->debug_p = 0;
  221.   scb->async_state = 0;
  222.   scb->async_handler = NULL;
  223.   scb->async_context = NULL;
  224.   scb_base = scb;

  225.   if ((ops->fdopen) != NULL)
  226.     (*ops->fdopen) (scb, fd);
  227.   else
  228.     scb->fd = fd;

  229.   return scb;
  230. }

  231. struct serial *
  232. serial_fdopen (const int fd)
  233. {
  234.   return serial_fdopen_ops (fd, NULL);
  235. }

  236. static void
  237. do_serial_close (struct serial *scb, int really_close)
  238. {
  239.   struct serial *tmp_scb;

  240.   if (serial_logfp)
  241.     {
  242.       fputs_unfiltered ("\nEnd of log\n", serial_logfp);
  243.       serial_current_type = 0;

  244.       /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr?  */
  245.       ui_file_delete (serial_logfp);
  246.       serial_logfp = NULL;
  247.     }

  248.   /* ensure that the FD has been taken out of async mode.  */
  249.   if (scb->async_handler != NULL)
  250.     serial_async (scb, NULL, NULL);

  251.   if (really_close)
  252.     scb->ops->close (scb);

  253.   if (scb->name)
  254.     xfree (scb->name);

  255.   /* For serial_is_open.  */
  256.   scb->bufp = NULL;

  257.   if (scb_base == scb)
  258.     scb_base = scb_base->next;
  259.   else
  260.     for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
  261.       {
  262.         if (tmp_scb->next != scb)
  263.           continue;

  264.         tmp_scb->next = tmp_scb->next->next;
  265.         break;
  266.       }

  267.   serial_unref (scb);
  268. }

  269. void
  270. serial_close (struct serial *scb)
  271. {
  272.   do_serial_close (scb, 1);
  273. }

  274. void
  275. serial_un_fdopen (struct serial *scb)
  276. {
  277.   do_serial_close (scb, 0);
  278. }

  279. int
  280. serial_is_open (struct serial *scb)
  281. {
  282.   return scb->bufp != NULL;
  283. }

  284. void
  285. serial_ref (struct serial *scb)
  286. {
  287.   scb->refcnt++;
  288. }

  289. void
  290. serial_unref (struct serial *scb)
  291. {
  292.   --scb->refcnt;
  293.   if (scb->refcnt == 0)
  294.     xfree (scb);
  295. }

  296. int
  297. serial_readchar (struct serial *scb, int timeout)
  298. {
  299.   int ch;

  300.   /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC
  301.      code is finished.  */
  302.   if (0 && serial_is_async_p (scb) && timeout < 0)
  303.     internal_error (__FILE__, __LINE__,
  304.                     _("serial_readchar: blocking read in async mode"));

  305.   ch = scb->ops->readchar (scb, timeout);
  306.   if (serial_logfp != NULL)
  307.     {
  308.       serial_logchar (serial_logfp, 'r', ch, timeout);

  309.       /* Make sure that the log file is as up-to-date as possible,
  310.          in case we are getting ready to dump core or something.  */
  311.       gdb_flush (serial_logfp);
  312.     }
  313.   if (serial_debug_p (scb))
  314.     {
  315.       fprintf_unfiltered (gdb_stdlog, "[");
  316.       serial_logchar (gdb_stdlog, 'r', ch, timeout);
  317.       fprintf_unfiltered (gdb_stdlog, "]");
  318.       gdb_flush (gdb_stdlog);
  319.     }

  320.   return (ch);
  321. }

  322. int
  323. serial_write (struct serial *scb, const void *buf, size_t count)
  324. {
  325.   if (serial_logfp != NULL)
  326.     {
  327.       const char *str = buf;
  328.       size_t c;

  329.       for (c = 0; c < count; c++)
  330.         serial_logchar (serial_logfp, 'w', str[c] & 0xff, 0);

  331.       /* Make sure that the log file is as up-to-date as possible,
  332.          in case we are getting ready to dump core or something.  */
  333.       gdb_flush (serial_logfp);
  334.     }
  335.   if (serial_debug_p (scb))
  336.     {
  337.       const char *str = buf;
  338.       size_t c;

  339.       for (c = 0; c < count; c++)
  340.         {
  341.           fprintf_unfiltered (gdb_stdlog, "[");
  342.           serial_logchar (gdb_stdlog, 'w', str[c] & 0xff, 0);
  343.           fprintf_unfiltered (gdb_stdlog, "]");
  344.         }
  345.       gdb_flush (gdb_stdlog);
  346.     }

  347.   return (scb->ops->write (scb, buf, count));
  348. }

  349. void
  350. serial_printf (struct serial *desc, const char *format,...)
  351. {
  352.   va_list args;
  353.   char *buf;
  354.   va_start (args, format);

  355.   buf = xstrvprintf (format, args);
  356.   serial_write (desc, buf, strlen (buf));

  357.   xfree (buf);
  358.   va_end (args);
  359. }

  360. int
  361. serial_drain_output (struct serial *scb)
  362. {
  363.   return scb->ops->drain_output (scb);
  364. }

  365. int
  366. serial_flush_output (struct serial *scb)
  367. {
  368.   return scb->ops->flush_output (scb);
  369. }

  370. int
  371. serial_flush_input (struct serial *scb)
  372. {
  373.   return scb->ops->flush_input (scb);
  374. }

  375. int
  376. serial_send_break (struct serial *scb)
  377. {
  378.   if (serial_logfp != NULL)
  379.     serial_logchar (serial_logfp, 'w', SERIAL_BREAK, 0);

  380.   return (scb->ops->send_break (scb));
  381. }

  382. void
  383. serial_raw (struct serial *scb)
  384. {
  385.   scb->ops->go_raw (scb);
  386. }

  387. serial_ttystate
  388. serial_get_tty_state (struct serial *scb)
  389. {
  390.   return scb->ops->get_tty_state (scb);
  391. }

  392. serial_ttystate
  393. serial_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
  394. {
  395.   return scb->ops->copy_tty_state (scb, ttystate);
  396. }

  397. int
  398. serial_set_tty_state (struct serial *scb, serial_ttystate ttystate)
  399. {
  400.   return scb->ops->set_tty_state (scb, ttystate);
  401. }

  402. void
  403. serial_print_tty_state (struct serial *scb,
  404.                         serial_ttystate ttystate,
  405.                         struct ui_file *stream)
  406. {
  407.   scb->ops->print_tty_state (scb, ttystate, stream);
  408. }

  409. int
  410. serial_noflush_set_tty_state (struct serial *scb,
  411.                               serial_ttystate new_ttystate,
  412.                               serial_ttystate old_ttystate)
  413. {
  414.   return scb->ops->noflush_set_tty_state (scb, new_ttystate, old_ttystate);
  415. }

  416. int
  417. serial_setbaudrate (struct serial *scb, int rate)
  418. {
  419.   return scb->ops->setbaudrate (scb, rate);
  420. }

  421. int
  422. serial_setstopbits (struct serial *scb, int num)
  423. {
  424.   return scb->ops->setstopbits (scb, num);
  425. }

  426. int
  427. serial_can_async_p (struct serial *scb)
  428. {
  429.   return (scb->ops->async != NULL);
  430. }

  431. int
  432. serial_is_async_p (struct serial *scb)
  433. {
  434.   return (scb->ops->async != NULL) && (scb->async_handler != NULL);
  435. }

  436. void
  437. serial_async (struct serial *scb,
  438.               serial_event_ftype *handler,
  439.               void *context)
  440. {
  441.   int changed = ((scb->async_handler == NULL) != (handler == NULL));

  442.   scb->async_handler = handler;
  443.   scb->async_context = context;
  444.   /* Only change mode if there is a need.  */
  445.   if (changed)
  446.     scb->ops->async (scb, handler != NULL);
  447. }

  448. void
  449. serial_debug (struct serial *scb, int debug_p)
  450. {
  451.   scb->debug_p = debug_p;
  452. }

  453. int
  454. serial_debug_p (struct serial *scb)
  455. {
  456.   return scb->debug_p || global_serial_debug_p;
  457. }

  458. #ifdef USE_WIN32API
  459. void
  460. serial_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
  461. {
  462.   if (scb->ops->wait_handle)
  463.     scb->ops->wait_handle (scb, read, except);
  464.   else
  465.     {
  466.       *read = (HANDLE) _get_osfhandle (scb->fd);
  467.       *except = NULL;
  468.     }
  469. }

  470. void
  471. serial_done_wait_handle (struct serial *scb)
  472. {
  473.   if (scb->ops->done_wait_handle)
  474.     scb->ops->done_wait_handle (scb);
  475. }
  476. #endif

  477. int
  478. serial_pipe (struct serial *scbs[2])
  479. {
  480.   const struct serial_ops *ops;
  481.   int fildes[2];

  482.   ops = serial_interface_lookup ("pipe");
  483.   if (!ops)
  484.     {
  485.       errno = ENOSYS;
  486.       return -1;
  487.     }

  488.   if (gdb_pipe (fildes) == -1)
  489.     return -1;

  490.   scbs[0] = serial_fdopen_ops (fildes[0], ops);
  491.   scbs[1] = serial_fdopen_ops (fildes[1], ops);
  492.   return 0;
  493. }

  494. /* Serial set/show framework.  */

  495. static struct cmd_list_element *serial_set_cmdlist;
  496. static struct cmd_list_element *serial_show_cmdlist;

  497. static void
  498. serial_set_cmd (char *args, int from_tty)
  499. {
  500.   printf_unfiltered ("\"set serial\" must be followed "
  501.                      "by the name of a command.\n");
  502.   help_list (serial_set_cmdlist, "set serial ", all_commands, gdb_stdout);
  503. }

  504. static void
  505. serial_show_cmd (char *args, int from_tty)
  506. {
  507.   cmd_show_list (serial_show_cmdlist, from_tty, "");
  508. }

  509. /* Baud rate specified for talking to serial target systems.  Default
  510.    is left as -1, so targets can choose their own defaults.  */
  511. /* FIXME: This means that "show serial baud" and gr_files_info can
  512.    print -1 or (unsigned int)-1.  This is a Bad User Interface.  */

  513. int baud_rate = -1;

  514. static void
  515. serial_baud_show_cmd (struct ui_file *file, int from_tty,
  516.                       struct cmd_list_element *c, const char *value)
  517. {
  518.   fprintf_filtered (file, _("Baud rate for remote serial I/O is %s.\n"),
  519.                     value);
  520. }

  521. void
  522. _initialize_serial (void)
  523. {
  524. #if 0
  525.   add_com ("connect", class_obscure, connect_command, _("\
  526. Connect the terminal directly up to the command monitor.\n\
  527. Use <CR>~. or <CR>~^D to break out."));
  528. #endif /* 0 */

  529.   add_prefix_cmd ("serial", class_maintenance, serial_set_cmd, _("\
  530. Set default serial/parallel port configuration."),
  531.                   &serial_set_cmdlist, "set serial ",
  532.                   0/*allow-unknown*/,
  533.                   &setlist);

  534.   add_prefix_cmd ("serial", class_maintenance, serial_show_cmd, _("\
  535. Show default serial/parallel port configuration."),
  536.                   &serial_show_cmdlist, "show serial ",
  537.                   0/*allow-unknown*/,
  538.                   &showlist);

  539.   /* If target is open when baud changes, it doesn't take effect until
  540.      the next open (I think, not sure).  */
  541.   add_setshow_zinteger_cmd ("baud", no_class, &baud_rate, _("\
  542. Set baud rate for remote serial I/O."), _("\
  543. Show baud rate for remote serial I/O."), _("\
  544. This value is used to set the speed of the serial port when debugging\n\
  545. using remote targets."),
  546.                             NULL,
  547.                             serial_baud_show_cmd,
  548.                             &serial_set_cmdlist, &serial_show_cmdlist);

  549.   add_setshow_filename_cmd ("remotelogfile", no_class, &serial_logfile, _("\
  550. Set filename for remote session recording."), _("\
  551. Show filename for remote session recording."), _("\
  552. This file is used to record the remote session for future playback\n\
  553. by gdbserver."),
  554.                             NULL,
  555.                             NULL, /* FIXME: i18n: */
  556.                             &setlist, &showlist);

  557.   add_setshow_enum_cmd ("remotelogbase", no_class, logbase_enums,
  558.                         &serial_logbase, _("\
  559. Set numerical base for remote session logging"), _("\
  560. Show numerical base for remote session logging"), NULL,
  561.                         NULL,
  562.                         NULL, /* FIXME: i18n: */
  563.                         &setlist, &showlist);

  564.   add_setshow_zuinteger_cmd ("serial", class_maintenance,
  565.                              &global_serial_debug_p, _("\
  566. Set serial debugging."), _("\
  567. Show serial debugging."), _("\
  568. When non-zero, serial port debugging is enabled."),
  569.                              NULL,
  570.                              NULL, /* FIXME: i18n: */
  571.                              &setdebuglist, &showdebuglist);
  572. }