serial.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /* Generic serial interface routines
  2. Copyright (C) 1992-2022 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. /* Is serial being debugged? */
  20. static unsigned int global_serial_debug_p;
  21. /* Serial I/O handlers. */
  22. static std::vector<const struct serial_ops *> serial_ops_list;
  23. /* Pointer to list of scb's. */
  24. static struct serial *scb_base;
  25. /* Non-NULL gives filename which contains a recording of the remote session,
  26. suitable for playback by gdbserver. */
  27. static std::string serial_logfile;
  28. static struct ui_file *serial_logfp = NULL;
  29. static const struct serial_ops *serial_interface_lookup (const char *);
  30. static void serial_logchar (struct ui_file *stream,
  31. int ch_type, int ch, int timeout);
  32. static const char logbase_hex[] = "hex";
  33. static const char logbase_octal[] = "octal";
  34. static const char logbase_ascii[] = "ascii";
  35. static const char *const logbase_enums[] =
  36. {logbase_hex, logbase_octal, logbase_ascii, NULL};
  37. static const char *serial_logbase = logbase_ascii;
  38. static int serial_current_type = 0;
  39. /* Log char CH of type CHTYPE, with TIMEOUT. */
  40. /* Define bogus char to represent a BREAK. Should be careful to choose a value
  41. that can't be confused with a normal char, or an error code. */
  42. #define SERIAL_BREAK 1235
  43. static void
  44. serial_logchar (struct ui_file *stream, int ch_type, int ch, int timeout)
  45. {
  46. if (ch_type != serial_current_type)
  47. {
  48. gdb_printf (stream, "\n%c ", ch_type);
  49. serial_current_type = ch_type;
  50. }
  51. if (serial_logbase != logbase_ascii)
  52. gdb_putc (' ', stream);
  53. switch (ch)
  54. {
  55. case SERIAL_TIMEOUT:
  56. gdb_printf (stream, "<Timeout: %d seconds>", timeout);
  57. return;
  58. case SERIAL_ERROR:
  59. gdb_printf (stream, "<Error: %s>", safe_strerror (errno));
  60. return;
  61. case SERIAL_EOF:
  62. gdb_puts ("<Eof>", stream);
  63. return;
  64. case SERIAL_BREAK:
  65. gdb_puts ("<Break>", stream);
  66. return;
  67. default:
  68. if (serial_logbase == logbase_hex)
  69. gdb_printf (stream, "%02x", ch & 0xff);
  70. else if (serial_logbase == logbase_octal)
  71. gdb_printf (stream, "%03o", ch & 0xff);
  72. else
  73. switch (ch)
  74. {
  75. case '\\':
  76. gdb_puts ("\\\\", stream);
  77. break;
  78. case '\b':
  79. gdb_puts ("\\b", stream);
  80. break;
  81. case '\f':
  82. gdb_puts ("\\f", stream);
  83. break;
  84. case '\n':
  85. gdb_puts ("\\n", stream);
  86. break;
  87. case '\r':
  88. gdb_puts ("\\r", stream);
  89. break;
  90. case '\t':
  91. gdb_puts ("\\t", stream);
  92. break;
  93. case '\v':
  94. gdb_puts ("\\v", stream);
  95. break;
  96. default:
  97. gdb_printf (stream,
  98. isprint (ch) ? "%c" : "\\x%02x", ch & 0xFF);
  99. break;
  100. }
  101. }
  102. }
  103. void
  104. serial_log_command (struct target_ops *self, const char *cmd)
  105. {
  106. if (!serial_logfp)
  107. return;
  108. serial_current_type = 'c';
  109. gdb_puts ("\nc ", serial_logfp);
  110. gdb_puts (cmd, serial_logfp);
  111. /* Make sure that the log file is as up-to-date as possible,
  112. in case we are getting ready to dump core or something. */
  113. gdb_flush (serial_logfp);
  114. }
  115. static const struct serial_ops *
  116. serial_interface_lookup (const char *name)
  117. {
  118. for (const serial_ops *ops : serial_ops_list)
  119. if (strcmp (name, ops->name) == 0)
  120. return ops;
  121. return NULL;
  122. }
  123. void
  124. serial_add_interface (const struct serial_ops *optable)
  125. {
  126. serial_ops_list.push_back (optable);
  127. }
  128. /* Return the open serial device for FD, if found, or NULL if FD is
  129. not already opened. */
  130. struct serial *
  131. serial_for_fd (int fd)
  132. {
  133. struct serial *scb;
  134. for (scb = scb_base; scb; scb = scb->next)
  135. if (scb->fd == fd)
  136. return scb;
  137. return NULL;
  138. }
  139. /* Create a new serial for OPS. */
  140. static struct serial *
  141. new_serial (const struct serial_ops *ops)
  142. {
  143. struct serial *scb;
  144. scb = XCNEW (struct serial);
  145. scb->ops = ops;
  146. scb->bufp = scb->buf;
  147. scb->error_fd = -1;
  148. scb->refcnt = 1;
  149. return scb;
  150. }
  151. static struct serial *serial_open_ops_1 (const struct serial_ops *ops,
  152. const char *open_name);
  153. /* Open up a device or a network socket, depending upon the syntax of NAME. */
  154. struct serial *
  155. serial_open (const char *name)
  156. {
  157. const struct serial_ops *ops;
  158. const char *open_name = name;
  159. if (startswith (name, "|"))
  160. ops = serial_interface_lookup ("pipe");
  161. /* Check for a colon, suggesting an IP address/port pair.
  162. Do this *after* checking for all the interesting prefixes. We
  163. don't want to constrain the syntax of what can follow them. */
  164. else if (strchr (name, ':'))
  165. ops = serial_interface_lookup ("tcp");
  166. else
  167. {
  168. #ifndef USE_WIN32API
  169. /* Check to see if name is a socket. If it is, then treat it
  170. as such. Otherwise assume that it's a character device. */
  171. struct stat sb;
  172. if (stat (name, &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFSOCK)
  173. ops = serial_interface_lookup ("local");
  174. else
  175. #endif
  176. ops = serial_interface_lookup ("hardwire");
  177. }
  178. if (!ops)
  179. return NULL;
  180. return serial_open_ops_1 (ops, open_name);
  181. }
  182. /* Open up a serial for OPS, passing OPEN_NAME to the open method. */
  183. static struct serial *
  184. serial_open_ops_1 (const struct serial_ops *ops, const char *open_name)
  185. {
  186. struct serial *scb;
  187. scb = new_serial (ops);
  188. /* `...->open (...)' would get expanded by the open(2) syscall macro. */
  189. if ((*scb->ops->open) (scb, open_name))
  190. {
  191. xfree (scb);
  192. return NULL;
  193. }
  194. scb->name = open_name != NULL ? xstrdup (open_name) : NULL;
  195. scb->next = scb_base;
  196. scb_base = scb;
  197. if (!serial_logfile.empty ())
  198. {
  199. stdio_file_up file (new stdio_file ());
  200. if (!file->open (serial_logfile.c_str (), "w"))
  201. perror_with_name (serial_logfile.c_str ());
  202. serial_logfp = file.release ();
  203. }
  204. return scb;
  205. }
  206. /* See serial.h. */
  207. struct serial *
  208. serial_open_ops (const struct serial_ops *ops)
  209. {
  210. return serial_open_ops_1 (ops, NULL);
  211. }
  212. /* Open a new serial stream using a file handle, using serial
  213. interface ops OPS. */
  214. static struct serial *
  215. serial_fdopen_ops (const int fd, const struct serial_ops *ops)
  216. {
  217. struct serial *scb;
  218. if (!ops)
  219. {
  220. ops = serial_interface_lookup ("terminal");
  221. if (!ops)
  222. ops = serial_interface_lookup ("hardwire");
  223. }
  224. if (!ops)
  225. return NULL;
  226. scb = new_serial (ops);
  227. scb->name = NULL;
  228. scb->next = scb_base;
  229. scb_base = scb;
  230. if ((ops->fdopen) != NULL)
  231. (*ops->fdopen) (scb, fd);
  232. else
  233. scb->fd = fd;
  234. return scb;
  235. }
  236. struct serial *
  237. serial_fdopen (const int fd)
  238. {
  239. return serial_fdopen_ops (fd, NULL);
  240. }
  241. static void
  242. do_serial_close (struct serial *scb, int really_close)
  243. {
  244. struct serial *tmp_scb;
  245. if (serial_logfp)
  246. {
  247. gdb_puts ("\nEnd of log\n", serial_logfp);
  248. serial_current_type = 0;
  249. /* XXX - What if serial_logfp == gdb_stdout or gdb_stderr? */
  250. delete serial_logfp;
  251. serial_logfp = NULL;
  252. }
  253. /* ensure that the FD has been taken out of async mode. */
  254. if (scb->async_handler != NULL)
  255. serial_async (scb, NULL, NULL);
  256. if (really_close)
  257. scb->ops->close (scb);
  258. xfree (scb->name);
  259. /* For serial_is_open. */
  260. scb->bufp = NULL;
  261. if (scb_base == scb)
  262. scb_base = scb_base->next;
  263. else
  264. for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
  265. {
  266. if (tmp_scb->next != scb)
  267. continue;
  268. tmp_scb->next = tmp_scb->next->next;
  269. break;
  270. }
  271. serial_unref (scb);
  272. }
  273. void
  274. serial_close (struct serial *scb)
  275. {
  276. do_serial_close (scb, 1);
  277. }
  278. void
  279. serial_un_fdopen (struct serial *scb)
  280. {
  281. do_serial_close (scb, 0);
  282. }
  283. int
  284. serial_is_open (struct serial *scb)
  285. {
  286. return scb->bufp != NULL;
  287. }
  288. void
  289. serial_ref (struct serial *scb)
  290. {
  291. scb->refcnt++;
  292. }
  293. void
  294. serial_unref (struct serial *scb)
  295. {
  296. --scb->refcnt;
  297. if (scb->refcnt == 0)
  298. xfree (scb);
  299. }
  300. int
  301. serial_readchar (struct serial *scb, int timeout)
  302. {
  303. int ch;
  304. /* FIXME: cagney/1999-10-11: Don't enable this check until the ASYNC
  305. code is finished. */
  306. if (0 && serial_is_async_p (scb) && timeout < 0)
  307. internal_error (__FILE__, __LINE__,
  308. _("serial_readchar: blocking read in async mode"));
  309. ch = scb->ops->readchar (scb, timeout);
  310. if (serial_logfp != NULL)
  311. {
  312. serial_logchar (serial_logfp, 'r', ch, timeout);
  313. /* Make sure that the log file is as up-to-date as possible,
  314. in case we are getting ready to dump core or something. */
  315. gdb_flush (serial_logfp);
  316. }
  317. if (serial_debug_p (scb))
  318. {
  319. gdb_printf (gdb_stdlog, "[");
  320. serial_logchar (gdb_stdlog, 'r', ch, timeout);
  321. gdb_printf (gdb_stdlog, "]");
  322. gdb_flush (gdb_stdlog);
  323. }
  324. return (ch);
  325. }
  326. int
  327. serial_write (struct serial *scb, const void *buf, size_t count)
  328. {
  329. if (serial_logfp != NULL)
  330. {
  331. const char *str = (const char *) buf;
  332. size_t c;
  333. for (c = 0; c < count; c++)
  334. serial_logchar (serial_logfp, 'w', str[c] & 0xff, 0);
  335. /* Make sure that the log file is as up-to-date as possible,
  336. in case we are getting ready to dump core or something. */
  337. gdb_flush (serial_logfp);
  338. }
  339. if (serial_debug_p (scb))
  340. {
  341. const char *str = (const char *) buf;
  342. size_t c;
  343. for (c = 0; c < count; c++)
  344. {
  345. gdb_printf (gdb_stdlog, "[");
  346. serial_logchar (gdb_stdlog, 'w', str[c] & 0xff, 0);
  347. gdb_printf (gdb_stdlog, "]");
  348. }
  349. gdb_flush (gdb_stdlog);
  350. }
  351. return (scb->ops->write (scb, buf, count));
  352. }
  353. void
  354. serial_printf (struct serial *desc, const char *format, ...)
  355. {
  356. va_list args;
  357. va_start (args, format);
  358. std::string buf = string_vprintf (format, args);
  359. serial_write (desc, buf.c_str (), buf.length ());
  360. va_end (args);
  361. }
  362. int
  363. serial_drain_output (struct serial *scb)
  364. {
  365. return scb->ops->drain_output (scb);
  366. }
  367. int
  368. serial_flush_output (struct serial *scb)
  369. {
  370. return scb->ops->flush_output (scb);
  371. }
  372. int
  373. serial_flush_input (struct serial *scb)
  374. {
  375. return scb->ops->flush_input (scb);
  376. }
  377. int
  378. serial_send_break (struct serial *scb)
  379. {
  380. if (serial_logfp != NULL)
  381. serial_logchar (serial_logfp, 'w', SERIAL_BREAK, 0);
  382. return (scb->ops->send_break (scb));
  383. }
  384. void
  385. serial_raw (struct serial *scb)
  386. {
  387. scb->ops->go_raw (scb);
  388. }
  389. serial_ttystate
  390. serial_get_tty_state (struct serial *scb)
  391. {
  392. return scb->ops->get_tty_state (scb);
  393. }
  394. serial_ttystate
  395. serial_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
  396. {
  397. return scb->ops->copy_tty_state (scb, ttystate);
  398. }
  399. int
  400. serial_set_tty_state (struct serial *scb, serial_ttystate ttystate)
  401. {
  402. return scb->ops->set_tty_state (scb, ttystate);
  403. }
  404. void
  405. serial_print_tty_state (struct serial *scb,
  406. serial_ttystate ttystate,
  407. struct ui_file *stream)
  408. {
  409. scb->ops->print_tty_state (scb, ttystate, stream);
  410. }
  411. int
  412. serial_setbaudrate (struct serial *scb, int rate)
  413. {
  414. return scb->ops->setbaudrate (scb, rate);
  415. }
  416. int
  417. serial_setstopbits (struct serial *scb, int num)
  418. {
  419. return scb->ops->setstopbits (scb, num);
  420. }
  421. /* See serial.h. */
  422. int
  423. serial_setparity (struct serial *scb, int parity)
  424. {
  425. return scb->ops->setparity (scb, parity);
  426. }
  427. int
  428. serial_can_async_p (struct serial *scb)
  429. {
  430. return (scb->ops->async != NULL);
  431. }
  432. int
  433. serial_is_async_p (struct serial *scb)
  434. {
  435. return (scb->ops->async != NULL) && (scb->async_handler != NULL);
  436. }
  437. void
  438. serial_async (struct serial *scb,
  439. serial_event_ftype *handler,
  440. void *context)
  441. {
  442. int changed = ((scb->async_handler == NULL) != (handler == NULL));
  443. scb->async_handler = handler;
  444. scb->async_context = context;
  445. /* Only change mode if there is a need. */
  446. if (changed)
  447. scb->ops->async (scb, handler != NULL);
  448. }
  449. void
  450. serial_debug (struct serial *scb, int debug_p)
  451. {
  452. scb->debug_p = debug_p;
  453. }
  454. int
  455. serial_debug_p (struct serial *scb)
  456. {
  457. return scb->debug_p || global_serial_debug_p;
  458. }
  459. #ifdef USE_WIN32API
  460. void
  461. serial_wait_handle (struct serial *scb, HANDLE *read, HANDLE *except)
  462. {
  463. if (scb->ops->wait_handle)
  464. scb->ops->wait_handle (scb, read, except);
  465. else
  466. {
  467. *read = (HANDLE) _get_osfhandle (scb->fd);
  468. *except = NULL;
  469. }
  470. }
  471. void
  472. serial_done_wait_handle (struct serial *scb)
  473. {
  474. if (scb->ops->done_wait_handle)
  475. scb->ops->done_wait_handle (scb);
  476. }
  477. #endif
  478. int
  479. serial_pipe (struct serial *scbs[2])
  480. {
  481. const struct serial_ops *ops;
  482. int fildes[2];
  483. ops = serial_interface_lookup ("pipe");
  484. if (!ops)
  485. {
  486. errno = ENOSYS;
  487. return -1;
  488. }
  489. if (gdb_pipe (fildes) == -1)
  490. return -1;
  491. scbs[0] = serial_fdopen_ops (fildes[0], ops);
  492. scbs[1] = serial_fdopen_ops (fildes[1], ops);
  493. return 0;
  494. }
  495. /* Serial set/show framework. */
  496. static struct cmd_list_element *serial_set_cmdlist;
  497. static struct cmd_list_element *serial_show_cmdlist;
  498. /* See serial.h. */
  499. int baud_rate = -1;
  500. static void
  501. serial_baud_show_cmd (struct ui_file *file, int from_tty,
  502. struct cmd_list_element *c, const char *value)
  503. {
  504. gdb_printf (file, _("Baud rate for remote serial I/O is %s.\n"),
  505. value);
  506. }
  507. /* See serial.h. */
  508. int serial_parity = GDBPARITY_NONE;
  509. static const char parity_none[] = "none";
  510. static const char parity_odd[] = "odd";
  511. static const char parity_even[] = "even";
  512. static const char *const parity_enums[] =
  513. {parity_none, parity_odd, parity_even, NULL};
  514. static const char *parity = parity_none;
  515. /* Set serial_parity value. */
  516. static void
  517. set_parity (const char *ignore_args, int from_tty, struct cmd_list_element *c)
  518. {
  519. if (parity == parity_odd)
  520. serial_parity = GDBPARITY_ODD;
  521. else if (parity == parity_even)
  522. serial_parity = GDBPARITY_EVEN;
  523. else
  524. serial_parity = GDBPARITY_NONE;
  525. }
  526. void _initialize_serial ();
  527. void
  528. _initialize_serial ()
  529. {
  530. #if 0
  531. add_com ("connect", class_obscure, connect_command, _("\
  532. Connect the terminal directly up to the command monitor.\n\
  533. Use <CR>~. or <CR>~^D to break out."));
  534. #endif /* 0 */
  535. add_setshow_prefix_cmd ("serial", class_maintenance,
  536. _("Set default serial/parallel port configuration."),
  537. _("Show default serial/parallel port configuration."),
  538. &serial_set_cmdlist, &serial_show_cmdlist,
  539. &setlist, &showlist);
  540. /* If target is open when baud changes, it doesn't take effect until
  541. the next open (I think, not sure). */
  542. add_setshow_zinteger_cmd ("baud", no_class, &baud_rate, _("\
  543. Set baud rate for remote serial I/O."), _("\
  544. Show baud rate for remote serial I/O."), _("\
  545. This value is used to set the speed of the serial port when debugging\n\
  546. using remote targets."),
  547. NULL,
  548. serial_baud_show_cmd,
  549. &serial_set_cmdlist, &serial_show_cmdlist);
  550. add_setshow_enum_cmd ("parity", no_class, parity_enums,
  551. &parity, _("\
  552. Set parity for remote serial I/O."), _("\
  553. Show parity for remote serial I/O."), NULL,
  554. set_parity,
  555. NULL, /* FIXME: i18n: */
  556. &serial_set_cmdlist, &serial_show_cmdlist);
  557. add_setshow_filename_cmd ("remotelogfile", no_class, &serial_logfile, _("\
  558. Set filename for remote session recording."), _("\
  559. Show filename for remote session recording."), _("\
  560. This file is used to record the remote session for future playback\n\
  561. by gdbserver."),
  562. NULL,
  563. NULL, /* FIXME: i18n: */
  564. &setlist, &showlist);
  565. add_setshow_enum_cmd ("remotelogbase", no_class, logbase_enums,
  566. &serial_logbase, _("\
  567. Set numerical base for remote session logging."), _("\
  568. Show numerical base for remote session logging."), NULL,
  569. NULL,
  570. NULL, /* FIXME: i18n: */
  571. &setlist, &showlist);
  572. add_setshow_zuinteger_cmd ("serial", class_maintenance,
  573. &global_serial_debug_p, _("\
  574. Set serial debugging."), _("\
  575. Show serial debugging."), _("\
  576. When non-zero, serial port debugging is enabled."),
  577. NULL,
  578. NULL, /* FIXME: i18n: */
  579. &setdebuglist, &showdebuglist);
  580. }