ser-base.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /* Generic serial interface functions.
  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 "serial.h"
  16. #include "ser-base.h"
  17. #include "gdbsupport/event-loop.h"
  18. #include "gdbsupport/gdb_select.h"
  19. #include "gdbsupport/gdb_sys_time.h"
  20. #ifdef USE_WIN32API
  21. #include <winsock2.h>
  22. #endif
  23. static timer_handler_func push_event;
  24. static handler_func fd_event;
  25. /* Event handling for ASYNC serial code.
  26. At any time the SERIAL device either: has an empty FIFO and is
  27. waiting on a FD event; or has a non-empty FIFO/error condition and
  28. is constantly scheduling timer events.
  29. ASYNC only stops pestering its client when it is de-async'ed or it
  30. is told to go away. */
  31. /* Value of scb->async_state: */
  32. enum {
  33. /* When >= 0, this contains the ID of the currently scheduled timer event.
  34. This state is rarely encountered. Timer events are one-off so as soon as
  35. the event is delivered the state is changed to NOTHING_SCHEDULED. */
  36. /* The fd_event() handler is scheduled. It is called when ever the
  37. file descriptor becomes ready. */
  38. FD_SCHEDULED = -1,
  39. /* Either no task is scheduled (just going into ASYNC mode) or a
  40. timer event has just gone off and the current state has been
  41. forced into nothing scheduled. */
  42. NOTHING_SCHEDULED = -2
  43. };
  44. /* Identify and schedule the next ASYNC task based on scb->async_state
  45. and scb->buf* (the input FIFO). A state machine is used to avoid
  46. the need to make redundant calls into the event-loop - the next
  47. scheduled task is only changed when needed. */
  48. static void
  49. reschedule (struct serial *scb)
  50. {
  51. if (serial_is_async_p (scb))
  52. {
  53. int next_state;
  54. switch (scb->async_state)
  55. {
  56. case FD_SCHEDULED:
  57. if (scb->bufcnt == 0)
  58. next_state = FD_SCHEDULED;
  59. else
  60. {
  61. delete_file_handler (scb->fd);
  62. next_state = create_timer (0, push_event, scb);
  63. }
  64. break;
  65. case NOTHING_SCHEDULED:
  66. if (scb->bufcnt == 0)
  67. {
  68. add_file_handler (scb->fd, fd_event, scb, "serial");
  69. next_state = FD_SCHEDULED;
  70. }
  71. else
  72. {
  73. next_state = create_timer (0, push_event, scb);
  74. }
  75. break;
  76. default: /* TIMER SCHEDULED */
  77. if (scb->bufcnt == 0)
  78. {
  79. delete_timer (scb->async_state);
  80. add_file_handler (scb->fd, fd_event, scb, "serial");
  81. next_state = FD_SCHEDULED;
  82. }
  83. else
  84. next_state = scb->async_state;
  85. break;
  86. }
  87. if (serial_debug_p (scb))
  88. {
  89. switch (next_state)
  90. {
  91. case FD_SCHEDULED:
  92. if (scb->async_state != FD_SCHEDULED)
  93. gdb_printf (gdb_stdlog, "[fd%d->fd-scheduled]\n",
  94. scb->fd);
  95. break;
  96. default: /* TIMER SCHEDULED */
  97. if (scb->async_state == FD_SCHEDULED)
  98. gdb_printf (gdb_stdlog, "[fd%d->timer-scheduled]\n",
  99. scb->fd);
  100. break;
  101. }
  102. }
  103. scb->async_state = next_state;
  104. }
  105. }
  106. /* Run the SCB's async handle, and reschedule, if the handler doesn't
  107. close SCB. */
  108. static void
  109. run_async_handler_and_reschedule (struct serial *scb)
  110. {
  111. int is_open;
  112. /* Take a reference, so a serial_close call within the handler
  113. doesn't make SCB a dangling pointer. */
  114. serial_ref (scb);
  115. /* Run the handler. */
  116. scb->async_handler (scb, scb->async_context);
  117. is_open = serial_is_open (scb);
  118. serial_unref (scb);
  119. /* Get ready for more, if not already closed. */
  120. if (is_open)
  121. reschedule (scb);
  122. }
  123. /* FD_EVENT: This is scheduled when the input FIFO is empty (and there
  124. is no pending error). As soon as data arrives, it is read into the
  125. input FIFO and the client notified. The client should then drain
  126. the FIFO using readchar(). If the FIFO isn't immediatly emptied,
  127. push_event() is used to nag the client until it is. */
  128. static void
  129. fd_event (int error, void *context)
  130. {
  131. struct serial *scb = (struct serial *) context;
  132. if (error != 0)
  133. {
  134. scb->bufcnt = SERIAL_ERROR;
  135. }
  136. else if (scb->bufcnt == 0)
  137. {
  138. /* Prime the input FIFO. The readchar() function is used to
  139. pull characters out of the buffer. See also
  140. generic_readchar(). */
  141. int nr;
  142. do
  143. {
  144. nr = scb->ops->read_prim (scb, BUFSIZ);
  145. }
  146. while (nr < 0 && errno == EINTR);
  147. if (nr == 0)
  148. {
  149. scb->bufcnt = SERIAL_EOF;
  150. }
  151. else if (nr > 0)
  152. {
  153. scb->bufcnt = nr;
  154. scb->bufp = scb->buf;
  155. }
  156. else
  157. {
  158. scb->bufcnt = SERIAL_ERROR;
  159. }
  160. }
  161. run_async_handler_and_reschedule (scb);
  162. }
  163. /* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
  164. error). Nag the client until all the data has been read. In the
  165. case of errors, the client will need to close or de-async the
  166. device before nagging stops. */
  167. static void
  168. push_event (void *context)
  169. {
  170. struct serial *scb = (struct serial *) context;
  171. scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
  172. run_async_handler_and_reschedule (scb);
  173. }
  174. /* Wait for input on scb, with timeout seconds. Returns 0 on success,
  175. otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
  176. /* NOTE: Some of the code below is dead. The only possible values of
  177. the TIMEOUT parameter are ONE and ZERO. OTOH, we should probably
  178. get rid of the deprecated_ui_loop_hook call in do_ser_base_readchar
  179. instead and support infinite time outs here. */
  180. static int
  181. ser_base_wait_for (struct serial *scb, int timeout)
  182. {
  183. while (1)
  184. {
  185. int numfds;
  186. struct timeval tv;
  187. fd_set readfds, exceptfds;
  188. int nfds;
  189. /* NOTE: Some OS's can scramble the READFDS when the select()
  190. call fails (ex the kernel with Red Hat 5.2). Initialize all
  191. arguments before each call. */
  192. tv.tv_sec = timeout;
  193. tv.tv_usec = 0;
  194. FD_ZERO (&readfds);
  195. FD_ZERO (&exceptfds);
  196. FD_SET (scb->fd, &readfds);
  197. FD_SET (scb->fd, &exceptfds);
  198. QUIT;
  199. nfds = scb->fd + 1;
  200. if (timeout >= 0)
  201. numfds = interruptible_select (nfds, &readfds, 0, &exceptfds, &tv);
  202. else
  203. numfds = interruptible_select (nfds, &readfds, 0, &exceptfds, 0);
  204. if (numfds <= 0)
  205. {
  206. if (numfds == 0)
  207. return SERIAL_TIMEOUT;
  208. else if (errno == EINTR)
  209. continue;
  210. else
  211. return SERIAL_ERROR; /* Got an error from select or
  212. poll. */
  213. }
  214. return 0;
  215. }
  216. }
  217. /* Read any error output we might have. */
  218. static void
  219. ser_base_read_error_fd (struct serial *scb, int close_fd)
  220. {
  221. if (scb->error_fd != -1)
  222. {
  223. ssize_t s;
  224. char buf[GDB_MI_MSG_WIDTH + 1];
  225. for (;;)
  226. {
  227. char *current;
  228. char *newline;
  229. int to_read = GDB_MI_MSG_WIDTH;
  230. int num_bytes = -1;
  231. if (scb->ops->avail)
  232. num_bytes = (scb->ops->avail)(scb, scb->error_fd);
  233. if (num_bytes != -1)
  234. to_read = (num_bytes < to_read) ? num_bytes : to_read;
  235. if (to_read == 0)
  236. break;
  237. s = read (scb->error_fd, &buf, to_read);
  238. if ((s == -1) || (s == 0 && !close_fd))
  239. break;
  240. if (s == 0 && close_fd)
  241. {
  242. /* End of file. */
  243. if (serial_is_async_p (scb))
  244. delete_file_handler (scb->error_fd);
  245. close (scb->error_fd);
  246. scb->error_fd = -1;
  247. break;
  248. }
  249. /* In theory, embedded newlines are not a problem.
  250. But for MI, we want each output line to have just
  251. one newline for legibility. So output things
  252. in newline chunks. */
  253. gdb_assert (s > 0 && s <= GDB_MI_MSG_WIDTH);
  254. buf[s] = '\0';
  255. current = buf;
  256. while ((newline = strstr (current, "\n")) != NULL)
  257. {
  258. *newline = '\0';
  259. gdb_puts (current, gdb_stderr);
  260. gdb_puts ("\n", gdb_stderr);
  261. current = newline + 1;
  262. }
  263. gdb_puts (current, gdb_stderr);
  264. }
  265. }
  266. }
  267. /* Event-loop callback for a serial's error_fd. Flushes any error
  268. output we might have. */
  269. static void
  270. handle_error_fd (int error, gdb_client_data client_data)
  271. {
  272. serial *scb = (serial *) client_data;
  273. ser_base_read_error_fd (scb, 0);
  274. }
  275. /* Read a character with user-specified timeout. TIMEOUT is number of
  276. seconds to wait, or -1 to wait forever. Use timeout of 0 to effect
  277. a poll. Returns char if successful. Returns SERIAL_TIMEOUT if
  278. timeout expired, SERIAL_EOF if line dropped dead, or SERIAL_ERROR
  279. for any other error (see errno in that case). */
  280. static int
  281. do_ser_base_readchar (struct serial *scb, int timeout)
  282. {
  283. int status;
  284. int delta;
  285. /* We have to be able to keep the GUI alive here, so we break the
  286. original timeout into steps of 1 second, running the "keep the
  287. GUI alive" hook each time through the loop.
  288. Also, timeout = 0 means to poll, so we just set the delta to 0,
  289. so we will only go through the loop once. */
  290. delta = (timeout == 0 ? 0 : 1);
  291. while (1)
  292. {
  293. /* N.B. The UI may destroy our world (for instance by calling
  294. remote_stop,) in which case we want to get out of here as
  295. quickly as possible. It is not safe to touch scb, since
  296. someone else might have freed it. The
  297. deprecated_ui_loop_hook signals that we should exit by
  298. returning 1. */
  299. if (deprecated_ui_loop_hook)
  300. {
  301. if (deprecated_ui_loop_hook (0))
  302. return SERIAL_TIMEOUT;
  303. }
  304. status = ser_base_wait_for (scb, delta);
  305. if (timeout > 0)
  306. timeout -= delta;
  307. /* If we got a character or an error back from wait_for, then we can
  308. break from the loop before the timeout is completed. */
  309. if (status != SERIAL_TIMEOUT)
  310. break;
  311. /* If we have exhausted the original timeout, then generate
  312. a SERIAL_TIMEOUT, and pass it out of the loop. */
  313. else if (timeout == 0)
  314. {
  315. status = SERIAL_TIMEOUT;
  316. break;
  317. }
  318. /* We also need to check and consume the stderr because it could
  319. come before the stdout for some stubs. If we just sit and wait
  320. for stdout, we would hit a deadlock for that case. */
  321. ser_base_read_error_fd (scb, 0);
  322. }
  323. if (status < 0)
  324. return status;
  325. do
  326. {
  327. status = scb->ops->read_prim (scb, BUFSIZ);
  328. }
  329. while (status < 0 && errno == EINTR);
  330. if (status <= 0)
  331. {
  332. if (status == 0)
  333. return SERIAL_EOF;
  334. else
  335. /* Got an error from read. */
  336. return SERIAL_ERROR;
  337. }
  338. scb->bufcnt = status;
  339. scb->bufcnt--;
  340. scb->bufp = scb->buf;
  341. return *scb->bufp++;
  342. }
  343. /* Perform operations common to both old and new readchar. */
  344. /* Return the next character from the input FIFO. If the FIFO is
  345. empty, call the SERIAL specific routine to try and read in more
  346. characters.
  347. Initially data from the input FIFO is returned (fd_event()
  348. pre-reads the input into that FIFO. Once that has been emptied,
  349. further data is obtained by polling the input FD using the device
  350. specific readchar() function. Note: reschedule() is called after
  351. every read. This is because there is no guarentee that the lower
  352. level fd_event() poll_event() code (which also calls reschedule())
  353. will be called. */
  354. int
  355. generic_readchar (struct serial *scb, int timeout,
  356. int (do_readchar) (struct serial *scb, int timeout))
  357. {
  358. int ch;
  359. if (scb->bufcnt > 0)
  360. {
  361. ch = *scb->bufp;
  362. scb->bufcnt--;
  363. scb->bufp++;
  364. }
  365. else if (scb->bufcnt < 0)
  366. {
  367. /* Some errors/eof are are sticky. */
  368. ch = scb->bufcnt;
  369. }
  370. else
  371. {
  372. ch = do_readchar (scb, timeout);
  373. if (ch < 0)
  374. {
  375. switch ((enum serial_rc) ch)
  376. {
  377. case SERIAL_EOF:
  378. case SERIAL_ERROR:
  379. /* Make the error/eof stick. */
  380. scb->bufcnt = ch;
  381. break;
  382. case SERIAL_TIMEOUT:
  383. scb->bufcnt = 0;
  384. break;
  385. }
  386. }
  387. }
  388. /* Read any error output we might have. */
  389. ser_base_read_error_fd (scb, 1);
  390. reschedule (scb);
  391. return ch;
  392. }
  393. int
  394. ser_base_readchar (struct serial *scb, int timeout)
  395. {
  396. return generic_readchar (scb, timeout, do_ser_base_readchar);
  397. }
  398. int
  399. ser_base_write (struct serial *scb, const void *buf, size_t count)
  400. {
  401. const char *str = (const char *) buf;
  402. int cc;
  403. while (count > 0)
  404. {
  405. QUIT;
  406. cc = scb->ops->write_prim (scb, str, count);
  407. if (cc < 0)
  408. {
  409. if (errno == EINTR)
  410. continue;
  411. return 1;
  412. }
  413. count -= cc;
  414. str += cc;
  415. }
  416. return 0;
  417. }
  418. int
  419. ser_base_flush_output (struct serial *scb)
  420. {
  421. return 0;
  422. }
  423. int
  424. ser_base_flush_input (struct serial *scb)
  425. {
  426. if (scb->bufcnt >= 0)
  427. {
  428. scb->bufcnt = 0;
  429. scb->bufp = scb->buf;
  430. return 0;
  431. }
  432. else
  433. return SERIAL_ERROR;
  434. }
  435. int
  436. ser_base_send_break (struct serial *scb)
  437. {
  438. return 0;
  439. }
  440. int
  441. ser_base_drain_output (struct serial *scb)
  442. {
  443. return 0;
  444. }
  445. void
  446. ser_base_raw (struct serial *scb)
  447. {
  448. return; /* Always in raw mode. */
  449. }
  450. serial_ttystate
  451. ser_base_get_tty_state (struct serial *scb)
  452. {
  453. /* Allocate a dummy. */
  454. return (serial_ttystate) XNEW (int);
  455. }
  456. serial_ttystate
  457. ser_base_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
  458. {
  459. /* Allocate another dummy. */
  460. return (serial_ttystate) XNEW (int);
  461. }
  462. int
  463. ser_base_set_tty_state (struct serial *scb, serial_ttystate ttystate)
  464. {
  465. return 0;
  466. }
  467. void
  468. ser_base_print_tty_state (struct serial *scb,
  469. serial_ttystate ttystate,
  470. struct ui_file *stream)
  471. {
  472. /* Nothing to print. */
  473. return;
  474. }
  475. int
  476. ser_base_setbaudrate (struct serial *scb, int rate)
  477. {
  478. return 0; /* Never fails! */
  479. }
  480. int
  481. ser_base_setstopbits (struct serial *scb, int num)
  482. {
  483. return 0; /* Never fails! */
  484. }
  485. /* Implement the "setparity" serial_ops callback. */
  486. int
  487. ser_base_setparity (struct serial *scb, int parity)
  488. {
  489. return 0; /* Never fails! */
  490. }
  491. /* Put the SERIAL device into/out-of ASYNC mode. */
  492. void
  493. ser_base_async (struct serial *scb,
  494. int async_p)
  495. {
  496. if (async_p)
  497. {
  498. /* Force a re-schedule. */
  499. scb->async_state = NOTHING_SCHEDULED;
  500. if (serial_debug_p (scb))
  501. gdb_printf (gdb_stdlog, "[fd%d->asynchronous]\n",
  502. scb->fd);
  503. reschedule (scb);
  504. if (scb->error_fd != -1)
  505. add_file_handler (scb->error_fd, handle_error_fd, scb, "serial-error");
  506. }
  507. else
  508. {
  509. if (serial_debug_p (scb))
  510. gdb_printf (gdb_stdlog, "[fd%d->synchronous]\n",
  511. scb->fd);
  512. /* De-schedule whatever tasks are currently scheduled. */
  513. switch (scb->async_state)
  514. {
  515. case FD_SCHEDULED:
  516. delete_file_handler (scb->fd);
  517. break;
  518. case NOTHING_SCHEDULED:
  519. break;
  520. default: /* TIMER SCHEDULED */
  521. delete_timer (scb->async_state);
  522. break;
  523. }
  524. if (scb->error_fd != -1)
  525. delete_file_handler (scb->error_fd);
  526. }
  527. }