event-loop.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. /* Event loop machinery for GDB, the GNU debugger.
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "gdbsupport/common-defs.h"
  16. #include "gdbsupport/event-loop.h"
  17. #include <chrono>
  18. #ifdef HAVE_POLL
  19. #if defined (HAVE_POLL_H)
  20. #include <poll.h>
  21. #elif defined (HAVE_SYS_POLL_H)
  22. #include <sys/poll.h>
  23. #endif
  24. #endif
  25. #include <sys/types.h>
  26. #include "gdbsupport/gdb_sys_time.h"
  27. #include "gdbsupport/gdb_select.h"
  28. /* See event-loop.h. */
  29. debug_event_loop_kind debug_event_loop;
  30. /* Tell create_file_handler what events we are interested in.
  31. This is used by the select version of the event loop. */
  32. #define GDB_READABLE (1<<1)
  33. #define GDB_WRITABLE (1<<2)
  34. #define GDB_EXCEPTION (1<<3)
  35. /* Information about each file descriptor we register with the event
  36. loop. */
  37. struct file_handler
  38. {
  39. /* File descriptor. */
  40. int fd;
  41. /* Events we want to monitor: POLLIN, etc. */
  42. int mask;
  43. /* Events that have been seen since the last time. */
  44. int ready_mask;
  45. /* Procedure to call when fd is ready. */
  46. handler_func *proc;
  47. /* Argument to pass to proc. */
  48. gdb_client_data client_data;
  49. /* User-friendly name of this handler. */
  50. std::string name;
  51. /* If set, this file descriptor is used for a user interface. */
  52. bool is_ui;
  53. /* Was an error detected on this fd? */
  54. int error;
  55. /* Next registered file descriptor. */
  56. struct file_handler *next_file;
  57. };
  58. /* Do we use poll or select ? */
  59. #ifdef HAVE_POLL
  60. #define USE_POLL 1
  61. #else
  62. #define USE_POLL 0
  63. #endif /* HAVE_POLL */
  64. static unsigned char use_poll = USE_POLL;
  65. #ifdef USE_WIN32API
  66. #include <windows.h>
  67. #include <io.h>
  68. #endif
  69. /* Gdb_notifier is just a list of file descriptors gdb is interested in.
  70. These are the input file descriptor, and the target file
  71. descriptor. We have two flavors of the notifier, one for platforms
  72. that have the POLL function, the other for those that don't, and
  73. only support SELECT. Each of the elements in the gdb_notifier list is
  74. basically a description of what kind of events gdb is interested
  75. in, for each fd. */
  76. static struct
  77. {
  78. /* Ptr to head of file handler list. */
  79. file_handler *first_file_handler;
  80. /* Next file handler to handle, for the select variant. To level
  81. the fairness across event sources, we serve file handlers in a
  82. round-robin-like fashion. The number and order of the polled
  83. file handlers may change between invocations, but this is good
  84. enough. */
  85. file_handler *next_file_handler;
  86. #ifdef HAVE_POLL
  87. /* Ptr to array of pollfd structures. */
  88. struct pollfd *poll_fds;
  89. /* Next file descriptor to handle, for the poll variant. To level
  90. the fairness across event sources, we poll the file descriptors
  91. in a round-robin-like fashion. The number and order of the
  92. polled file descriptors may change between invocations, but
  93. this is good enough. */
  94. int next_poll_fds_index;
  95. /* Timeout in milliseconds for calls to poll(). */
  96. int poll_timeout;
  97. #endif
  98. /* Masks to be used in the next call to select.
  99. Bits are set in response to calls to create_file_handler. */
  100. fd_set check_masks[3];
  101. /* What file descriptors were found ready by select. */
  102. fd_set ready_masks[3];
  103. /* Number of file descriptors to monitor (for poll). */
  104. /* Number of valid bits (highest fd value + 1) (for select). */
  105. int num_fds;
  106. /* Time structure for calls to select(). */
  107. struct timeval select_timeout;
  108. /* Flag to tell whether the timeout should be used. */
  109. int timeout_valid;
  110. }
  111. gdb_notifier;
  112. /* Structure associated with a timer. PROC will be executed at the
  113. first occasion after WHEN. */
  114. struct gdb_timer
  115. {
  116. std::chrono::steady_clock::time_point when;
  117. int timer_id;
  118. struct gdb_timer *next;
  119. timer_handler_func *proc; /* Function to call to do the work. */
  120. gdb_client_data client_data; /* Argument to async_handler_func. */
  121. };
  122. /* List of currently active timers. It is sorted in order of
  123. increasing timers. */
  124. static struct
  125. {
  126. /* Pointer to first in timer list. */
  127. struct gdb_timer *first_timer;
  128. /* Id of the last timer created. */
  129. int num_timers;
  130. }
  131. timer_list;
  132. static void create_file_handler (int fd, int mask, handler_func *proc,
  133. gdb_client_data client_data,
  134. std::string &&name, bool is_ui);
  135. static int gdb_wait_for_event (int);
  136. static int update_wait_timeout (void);
  137. static int poll_timers (void);
  138. /* Process one high level event. If nothing is ready at this time,
  139. wait for something to happen (via gdb_wait_for_event), then process
  140. it. Returns >0 if something was done otherwise returns <0 (this
  141. can happen if there are no event sources to wait for). */
  142. int
  143. gdb_do_one_event (void)
  144. {
  145. static int event_source_head = 0;
  146. const int number_of_sources = 3;
  147. int current = 0;
  148. /* First let's see if there are any asynchronous signal handlers
  149. that are ready. These would be the result of invoking any of the
  150. signal handlers. */
  151. if (invoke_async_signal_handlers ())
  152. return 1;
  153. /* To level the fairness across event sources, we poll them in a
  154. round-robin fashion. */
  155. for (current = 0; current < number_of_sources; current++)
  156. {
  157. int res;
  158. switch (event_source_head)
  159. {
  160. case 0:
  161. /* Are any timers that are ready? */
  162. res = poll_timers ();
  163. break;
  164. case 1:
  165. /* Are there events already waiting to be collected on the
  166. monitored file descriptors? */
  167. res = gdb_wait_for_event (0);
  168. break;
  169. case 2:
  170. /* Are there any asynchronous event handlers ready? */
  171. res = check_async_event_handlers ();
  172. break;
  173. default:
  174. internal_error (__FILE__, __LINE__,
  175. "unexpected event_source_head %d",
  176. event_source_head);
  177. }
  178. event_source_head++;
  179. if (event_source_head == number_of_sources)
  180. event_source_head = 0;
  181. if (res > 0)
  182. return 1;
  183. }
  184. /* Block waiting for a new event. If gdb_wait_for_event returns -1,
  185. we should get out because this means that there are no event
  186. sources left. This will make the event loop stop, and the
  187. application exit. */
  188. if (gdb_wait_for_event (1) < 0)
  189. return -1;
  190. /* If gdb_wait_for_event has returned 1, it means that one event has
  191. been handled. We break out of the loop. */
  192. return 1;
  193. }
  194. /* See event-loop.h */
  195. void
  196. add_file_handler (int fd, handler_func *proc, gdb_client_data client_data,
  197. std::string &&name, bool is_ui)
  198. {
  199. #ifdef HAVE_POLL
  200. struct pollfd fds;
  201. #endif
  202. if (use_poll)
  203. {
  204. #ifdef HAVE_POLL
  205. /* Check to see if poll () is usable. If not, we'll switch to
  206. use select. This can happen on systems like
  207. m68k-motorola-sys, `poll' cannot be used to wait for `stdin'.
  208. On m68k-motorola-sysv, tty's are not stream-based and not
  209. `poll'able. */
  210. fds.fd = fd;
  211. fds.events = POLLIN;
  212. if (poll (&fds, 1, 0) == 1 && (fds.revents & POLLNVAL))
  213. use_poll = 0;
  214. #else
  215. internal_error (__FILE__, __LINE__,
  216. _("use_poll without HAVE_POLL"));
  217. #endif /* HAVE_POLL */
  218. }
  219. if (use_poll)
  220. {
  221. #ifdef HAVE_POLL
  222. create_file_handler (fd, POLLIN, proc, client_data, std::move (name),
  223. is_ui);
  224. #else
  225. internal_error (__FILE__, __LINE__,
  226. _("use_poll without HAVE_POLL"));
  227. #endif
  228. }
  229. else
  230. create_file_handler (fd, GDB_READABLE | GDB_EXCEPTION,
  231. proc, client_data, std::move (name), is_ui);
  232. }
  233. /* Helper for add_file_handler.
  234. For the poll case, MASK is a combination (OR) of POLLIN,
  235. POLLRDNORM, POLLRDBAND, POLLPRI, POLLOUT, POLLWRNORM, POLLWRBAND:
  236. these are the events we are interested in. If any of them occurs,
  237. proc should be called.
  238. For the select case, MASK is a combination of READABLE, WRITABLE,
  239. EXCEPTION. PROC is the procedure that will be called when an event
  240. occurs for FD. CLIENT_DATA is the argument to pass to PROC. */
  241. static void
  242. create_file_handler (int fd, int mask, handler_func * proc,
  243. gdb_client_data client_data, std::string &&name,
  244. bool is_ui)
  245. {
  246. file_handler *file_ptr;
  247. /* Do we already have a file handler for this file? (We may be
  248. changing its associated procedure). */
  249. for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
  250. file_ptr = file_ptr->next_file)
  251. {
  252. if (file_ptr->fd == fd)
  253. break;
  254. }
  255. /* It is a new file descriptor. Add it to the list. Otherwise, just
  256. change the data associated with it. */
  257. if (file_ptr == NULL)
  258. {
  259. file_ptr = new file_handler;
  260. file_ptr->fd = fd;
  261. file_ptr->ready_mask = 0;
  262. file_ptr->next_file = gdb_notifier.first_file_handler;
  263. gdb_notifier.first_file_handler = file_ptr;
  264. if (use_poll)
  265. {
  266. #ifdef HAVE_POLL
  267. gdb_notifier.num_fds++;
  268. if (gdb_notifier.poll_fds)
  269. gdb_notifier.poll_fds =
  270. (struct pollfd *) xrealloc (gdb_notifier.poll_fds,
  271. (gdb_notifier.num_fds
  272. * sizeof (struct pollfd)));
  273. else
  274. gdb_notifier.poll_fds =
  275. XNEW (struct pollfd);
  276. (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->fd = fd;
  277. (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->events = mask;
  278. (gdb_notifier.poll_fds + gdb_notifier.num_fds - 1)->revents = 0;
  279. #else
  280. internal_error (__FILE__, __LINE__,
  281. _("use_poll without HAVE_POLL"));
  282. #endif /* HAVE_POLL */
  283. }
  284. else
  285. {
  286. if (mask & GDB_READABLE)
  287. FD_SET (fd, &gdb_notifier.check_masks[0]);
  288. else
  289. FD_CLR (fd, &gdb_notifier.check_masks[0]);
  290. if (mask & GDB_WRITABLE)
  291. FD_SET (fd, &gdb_notifier.check_masks[1]);
  292. else
  293. FD_CLR (fd, &gdb_notifier.check_masks[1]);
  294. if (mask & GDB_EXCEPTION)
  295. FD_SET (fd, &gdb_notifier.check_masks[2]);
  296. else
  297. FD_CLR (fd, &gdb_notifier.check_masks[2]);
  298. if (gdb_notifier.num_fds <= fd)
  299. gdb_notifier.num_fds = fd + 1;
  300. }
  301. }
  302. file_ptr->proc = proc;
  303. file_ptr->client_data = client_data;
  304. file_ptr->mask = mask;
  305. file_ptr->name = std::move (name);
  306. file_ptr->is_ui = is_ui;
  307. }
  308. /* Return the next file handler to handle, and advance to the next
  309. file handler, wrapping around if the end of the list is
  310. reached. */
  311. static file_handler *
  312. get_next_file_handler_to_handle_and_advance (void)
  313. {
  314. file_handler *curr_next;
  315. /* The first time around, this is still NULL. */
  316. if (gdb_notifier.next_file_handler == NULL)
  317. gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
  318. curr_next = gdb_notifier.next_file_handler;
  319. gdb_assert (curr_next != NULL);
  320. /* Advance. */
  321. gdb_notifier.next_file_handler = curr_next->next_file;
  322. /* Wrap around, if necessary. */
  323. if (gdb_notifier.next_file_handler == NULL)
  324. gdb_notifier.next_file_handler = gdb_notifier.first_file_handler;
  325. return curr_next;
  326. }
  327. /* Remove the file descriptor FD from the list of monitored fd's:
  328. i.e. we don't care anymore about events on the FD. */
  329. void
  330. delete_file_handler (int fd)
  331. {
  332. file_handler *file_ptr, *prev_ptr = NULL;
  333. int i;
  334. #ifdef HAVE_POLL
  335. int j;
  336. struct pollfd *new_poll_fds;
  337. #endif
  338. /* Find the entry for the given file. */
  339. for (file_ptr = gdb_notifier.first_file_handler; file_ptr != NULL;
  340. file_ptr = file_ptr->next_file)
  341. {
  342. if (file_ptr->fd == fd)
  343. break;
  344. }
  345. if (file_ptr == NULL)
  346. return;
  347. if (use_poll)
  348. {
  349. #ifdef HAVE_POLL
  350. /* Create a new poll_fds array by copying every fd's information
  351. but the one we want to get rid of. */
  352. new_poll_fds = (struct pollfd *)
  353. xmalloc ((gdb_notifier.num_fds - 1) * sizeof (struct pollfd));
  354. for (i = 0, j = 0; i < gdb_notifier.num_fds; i++)
  355. {
  356. if ((gdb_notifier.poll_fds + i)->fd != fd)
  357. {
  358. (new_poll_fds + j)->fd = (gdb_notifier.poll_fds + i)->fd;
  359. (new_poll_fds + j)->events = (gdb_notifier.poll_fds + i)->events;
  360. (new_poll_fds + j)->revents
  361. = (gdb_notifier.poll_fds + i)->revents;
  362. j++;
  363. }
  364. }
  365. xfree (gdb_notifier.poll_fds);
  366. gdb_notifier.poll_fds = new_poll_fds;
  367. gdb_notifier.num_fds--;
  368. #else
  369. internal_error (__FILE__, __LINE__,
  370. _("use_poll without HAVE_POLL"));
  371. #endif /* HAVE_POLL */
  372. }
  373. else
  374. {
  375. if (file_ptr->mask & GDB_READABLE)
  376. FD_CLR (fd, &gdb_notifier.check_masks[0]);
  377. if (file_ptr->mask & GDB_WRITABLE)
  378. FD_CLR (fd, &gdb_notifier.check_masks[1]);
  379. if (file_ptr->mask & GDB_EXCEPTION)
  380. FD_CLR (fd, &gdb_notifier.check_masks[2]);
  381. /* Find current max fd. */
  382. if ((fd + 1) == gdb_notifier.num_fds)
  383. {
  384. gdb_notifier.num_fds--;
  385. for (i = gdb_notifier.num_fds; i; i--)
  386. {
  387. if (FD_ISSET (i - 1, &gdb_notifier.check_masks[0])
  388. || FD_ISSET (i - 1, &gdb_notifier.check_masks[1])
  389. || FD_ISSET (i - 1, &gdb_notifier.check_masks[2]))
  390. break;
  391. }
  392. gdb_notifier.num_fds = i;
  393. }
  394. }
  395. /* Deactivate the file descriptor, by clearing its mask,
  396. so that it will not fire again. */
  397. file_ptr->mask = 0;
  398. /* If this file handler was going to be the next one to be handled,
  399. advance to the next's next, if any. */
  400. if (gdb_notifier.next_file_handler == file_ptr)
  401. {
  402. if (file_ptr->next_file == NULL
  403. && file_ptr == gdb_notifier.first_file_handler)
  404. gdb_notifier.next_file_handler = NULL;
  405. else
  406. get_next_file_handler_to_handle_and_advance ();
  407. }
  408. /* Get rid of the file handler in the file handler list. */
  409. if (file_ptr == gdb_notifier.first_file_handler)
  410. gdb_notifier.first_file_handler = file_ptr->next_file;
  411. else
  412. {
  413. for (prev_ptr = gdb_notifier.first_file_handler;
  414. prev_ptr->next_file != file_ptr;
  415. prev_ptr = prev_ptr->next_file)
  416. ;
  417. prev_ptr->next_file = file_ptr->next_file;
  418. }
  419. delete file_ptr;
  420. }
  421. /* Handle the given event by calling the procedure associated to the
  422. corresponding file handler. */
  423. static void
  424. handle_file_event (file_handler *file_ptr, int ready_mask)
  425. {
  426. int mask;
  427. #ifdef HAVE_POLL
  428. int error_mask;
  429. #endif
  430. {
  431. {
  432. /* With poll, the ready_mask could have any of three events
  433. set to 1: POLLHUP, POLLERR, POLLNVAL. These events
  434. cannot be used in the requested event mask (events), but
  435. they can be returned in the return mask (revents). We
  436. need to check for those event too, and add them to the
  437. mask which will be passed to the handler. */
  438. /* See if the desired events (mask) match the received
  439. events (ready_mask). */
  440. if (use_poll)
  441. {
  442. #ifdef HAVE_POLL
  443. /* POLLHUP means EOF, but can be combined with POLLIN to
  444. signal more data to read. */
  445. error_mask = POLLHUP | POLLERR | POLLNVAL;
  446. mask = ready_mask & (file_ptr->mask | error_mask);
  447. if ((mask & (POLLERR | POLLNVAL)) != 0)
  448. {
  449. /* Work in progress. We may need to tell somebody
  450. what kind of error we had. */
  451. if (mask & POLLERR)
  452. warning (_("Error detected on fd %d"), file_ptr->fd);
  453. if (mask & POLLNVAL)
  454. warning (_("Invalid or non-`poll'able fd %d"),
  455. file_ptr->fd);
  456. file_ptr->error = 1;
  457. }
  458. else
  459. file_ptr->error = 0;
  460. #else
  461. internal_error (__FILE__, __LINE__,
  462. _("use_poll without HAVE_POLL"));
  463. #endif /* HAVE_POLL */
  464. }
  465. else
  466. {
  467. if (ready_mask & GDB_EXCEPTION)
  468. {
  469. warning (_("Exception condition detected on fd %d"),
  470. file_ptr->fd);
  471. file_ptr->error = 1;
  472. }
  473. else
  474. file_ptr->error = 0;
  475. mask = ready_mask & file_ptr->mask;
  476. }
  477. /* If there was a match, then call the handler. */
  478. if (mask != 0)
  479. {
  480. event_loop_ui_debug_printf (file_ptr->is_ui,
  481. "invoking fd file handler `%s`",
  482. file_ptr->name.c_str ());
  483. file_ptr->proc (file_ptr->error, file_ptr->client_data);
  484. }
  485. }
  486. }
  487. }
  488. /* Wait for new events on the monitored file descriptors. Run the
  489. event handler if the first descriptor that is detected by the poll.
  490. If BLOCK and if there are no events, this function will block in
  491. the call to poll. Return 1 if an event was handled. Return -1 if
  492. there are no file descriptors to monitor. Return 1 if an event was
  493. handled, otherwise returns 0. */
  494. static int
  495. gdb_wait_for_event (int block)
  496. {
  497. file_handler *file_ptr;
  498. int num_found = 0;
  499. /* Make sure all output is done before getting another event. */
  500. flush_streams ();
  501. if (gdb_notifier.num_fds == 0)
  502. return -1;
  503. if (block)
  504. update_wait_timeout ();
  505. if (use_poll)
  506. {
  507. #ifdef HAVE_POLL
  508. int timeout;
  509. if (block)
  510. timeout = gdb_notifier.timeout_valid ? gdb_notifier.poll_timeout : -1;
  511. else
  512. timeout = 0;
  513. num_found = poll (gdb_notifier.poll_fds,
  514. (unsigned long) gdb_notifier.num_fds, timeout);
  515. /* Don't print anything if we get out of poll because of a
  516. signal. */
  517. if (num_found == -1 && errno != EINTR)
  518. perror_with_name (("poll"));
  519. #else
  520. internal_error (__FILE__, __LINE__,
  521. _("use_poll without HAVE_POLL"));
  522. #endif /* HAVE_POLL */
  523. }
  524. else
  525. {
  526. struct timeval select_timeout;
  527. struct timeval *timeout_p;
  528. if (block)
  529. timeout_p = gdb_notifier.timeout_valid
  530. ? &gdb_notifier.select_timeout : NULL;
  531. else
  532. {
  533. memset (&select_timeout, 0, sizeof (select_timeout));
  534. timeout_p = &select_timeout;
  535. }
  536. gdb_notifier.ready_masks[0] = gdb_notifier.check_masks[0];
  537. gdb_notifier.ready_masks[1] = gdb_notifier.check_masks[1];
  538. gdb_notifier.ready_masks[2] = gdb_notifier.check_masks[2];
  539. num_found = gdb_select (gdb_notifier.num_fds,
  540. &gdb_notifier.ready_masks[0],
  541. &gdb_notifier.ready_masks[1],
  542. &gdb_notifier.ready_masks[2],
  543. timeout_p);
  544. /* Clear the masks after an error from select. */
  545. if (num_found == -1)
  546. {
  547. FD_ZERO (&gdb_notifier.ready_masks[0]);
  548. FD_ZERO (&gdb_notifier.ready_masks[1]);
  549. FD_ZERO (&gdb_notifier.ready_masks[2]);
  550. /* Dont print anything if we got a signal, let gdb handle
  551. it. */
  552. if (errno != EINTR)
  553. perror_with_name (("select"));
  554. }
  555. }
  556. /* Avoid looking at poll_fds[i]->revents if no event fired. */
  557. if (num_found <= 0)
  558. return 0;
  559. /* Run event handlers. We always run just one handler and go back
  560. to polling, in case a handler changes the notifier list. Since
  561. events for sources we haven't consumed yet wake poll/select
  562. immediately, no event is lost. */
  563. /* To level the fairness across event descriptors, we handle them in
  564. a round-robin-like fashion. The number and order of descriptors
  565. may change between invocations, but this is good enough. */
  566. if (use_poll)
  567. {
  568. #ifdef HAVE_POLL
  569. int i;
  570. int mask;
  571. while (1)
  572. {
  573. if (gdb_notifier.next_poll_fds_index >= gdb_notifier.num_fds)
  574. gdb_notifier.next_poll_fds_index = 0;
  575. i = gdb_notifier.next_poll_fds_index++;
  576. gdb_assert (i < gdb_notifier.num_fds);
  577. if ((gdb_notifier.poll_fds + i)->revents)
  578. break;
  579. }
  580. for (file_ptr = gdb_notifier.first_file_handler;
  581. file_ptr != NULL;
  582. file_ptr = file_ptr->next_file)
  583. {
  584. if (file_ptr->fd == (gdb_notifier.poll_fds + i)->fd)
  585. break;
  586. }
  587. gdb_assert (file_ptr != NULL);
  588. mask = (gdb_notifier.poll_fds + i)->revents;
  589. handle_file_event (file_ptr, mask);
  590. return 1;
  591. #else
  592. internal_error (__FILE__, __LINE__,
  593. _("use_poll without HAVE_POLL"));
  594. #endif /* HAVE_POLL */
  595. }
  596. else
  597. {
  598. /* See comment about even source fairness above. */
  599. int mask = 0;
  600. do
  601. {
  602. file_ptr = get_next_file_handler_to_handle_and_advance ();
  603. if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[0]))
  604. mask |= GDB_READABLE;
  605. if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[1]))
  606. mask |= GDB_WRITABLE;
  607. if (FD_ISSET (file_ptr->fd, &gdb_notifier.ready_masks[2]))
  608. mask |= GDB_EXCEPTION;
  609. }
  610. while (mask == 0);
  611. handle_file_event (file_ptr, mask);
  612. return 1;
  613. }
  614. return 0;
  615. }
  616. /* Create a timer that will expire in MS milliseconds from now. When
  617. the timer is ready, PROC will be executed. At creation, the timer
  618. is added to the timers queue. This queue is kept sorted in order
  619. of increasing timers. Return a handle to the timer struct. */
  620. int
  621. create_timer (int ms, timer_handler_func *proc,
  622. gdb_client_data client_data)
  623. {
  624. using namespace std::chrono;
  625. struct gdb_timer *timer_ptr, *timer_index, *prev_timer;
  626. steady_clock::time_point time_now = steady_clock::now ();
  627. timer_ptr = new gdb_timer ();
  628. timer_ptr->when = time_now + milliseconds (ms);
  629. timer_ptr->proc = proc;
  630. timer_ptr->client_data = client_data;
  631. timer_list.num_timers++;
  632. timer_ptr->timer_id = timer_list.num_timers;
  633. /* Now add the timer to the timer queue, making sure it is sorted in
  634. increasing order of expiration. */
  635. for (timer_index = timer_list.first_timer;
  636. timer_index != NULL;
  637. timer_index = timer_index->next)
  638. {
  639. if (timer_index->when > timer_ptr->when)
  640. break;
  641. }
  642. if (timer_index == timer_list.first_timer)
  643. {
  644. timer_ptr->next = timer_list.first_timer;
  645. timer_list.first_timer = timer_ptr;
  646. }
  647. else
  648. {
  649. for (prev_timer = timer_list.first_timer;
  650. prev_timer->next != timer_index;
  651. prev_timer = prev_timer->next)
  652. ;
  653. prev_timer->next = timer_ptr;
  654. timer_ptr->next = timer_index;
  655. }
  656. gdb_notifier.timeout_valid = 0;
  657. return timer_ptr->timer_id;
  658. }
  659. /* There is a chance that the creator of the timer wants to get rid of
  660. it before it expires. */
  661. void
  662. delete_timer (int id)
  663. {
  664. struct gdb_timer *timer_ptr, *prev_timer = NULL;
  665. /* Find the entry for the given timer. */
  666. for (timer_ptr = timer_list.first_timer; timer_ptr != NULL;
  667. timer_ptr = timer_ptr->next)
  668. {
  669. if (timer_ptr->timer_id == id)
  670. break;
  671. }
  672. if (timer_ptr == NULL)
  673. return;
  674. /* Get rid of the timer in the timer list. */
  675. if (timer_ptr == timer_list.first_timer)
  676. timer_list.first_timer = timer_ptr->next;
  677. else
  678. {
  679. for (prev_timer = timer_list.first_timer;
  680. prev_timer->next != timer_ptr;
  681. prev_timer = prev_timer->next)
  682. ;
  683. prev_timer->next = timer_ptr->next;
  684. }
  685. delete timer_ptr;
  686. gdb_notifier.timeout_valid = 0;
  687. }
  688. /* Convert a std::chrono duration to a struct timeval. */
  689. template<typename Duration>
  690. static struct timeval
  691. duration_cast_timeval (const Duration &d)
  692. {
  693. using namespace std::chrono;
  694. seconds sec = duration_cast<seconds> (d);
  695. microseconds msec = duration_cast<microseconds> (d - sec);
  696. struct timeval tv;
  697. tv.tv_sec = sec.count ();
  698. tv.tv_usec = msec.count ();
  699. return tv;
  700. }
  701. /* Update the timeout for the select() or poll(). Returns true if the
  702. timer has already expired, false otherwise. */
  703. static int
  704. update_wait_timeout (void)
  705. {
  706. if (timer_list.first_timer != NULL)
  707. {
  708. using namespace std::chrono;
  709. steady_clock::time_point time_now = steady_clock::now ();
  710. struct timeval timeout;
  711. if (timer_list.first_timer->when < time_now)
  712. {
  713. /* It expired already. */
  714. timeout.tv_sec = 0;
  715. timeout.tv_usec = 0;
  716. }
  717. else
  718. {
  719. steady_clock::duration d = timer_list.first_timer->when - time_now;
  720. timeout = duration_cast_timeval (d);
  721. }
  722. /* Update the timeout for select/ poll. */
  723. if (use_poll)
  724. {
  725. #ifdef HAVE_POLL
  726. gdb_notifier.poll_timeout = timeout.tv_sec * 1000;
  727. #else
  728. internal_error (__FILE__, __LINE__,
  729. _("use_poll without HAVE_POLL"));
  730. #endif /* HAVE_POLL */
  731. }
  732. else
  733. {
  734. gdb_notifier.select_timeout.tv_sec = timeout.tv_sec;
  735. gdb_notifier.select_timeout.tv_usec = timeout.tv_usec;
  736. }
  737. gdb_notifier.timeout_valid = 1;
  738. if (timer_list.first_timer->when < time_now)
  739. return 1;
  740. }
  741. else
  742. gdb_notifier.timeout_valid = 0;
  743. return 0;
  744. }
  745. /* Check whether a timer in the timers queue is ready. If a timer is
  746. ready, call its handler and return. Update the timeout for the
  747. select() or poll() as well. Return 1 if an event was handled,
  748. otherwise returns 0.*/
  749. static int
  750. poll_timers (void)
  751. {
  752. if (update_wait_timeout ())
  753. {
  754. struct gdb_timer *timer_ptr = timer_list.first_timer;
  755. timer_handler_func *proc = timer_ptr->proc;
  756. gdb_client_data client_data = timer_ptr->client_data;
  757. /* Get rid of the timer from the beginning of the list. */
  758. timer_list.first_timer = timer_ptr->next;
  759. /* Delete the timer before calling the callback, not after, in
  760. case the callback itself decides to try deleting the timer
  761. too. */
  762. delete timer_ptr;
  763. /* Call the procedure associated with that timer. */
  764. (proc) (client_data);
  765. return 1;
  766. }
  767. return 0;
  768. }