async-event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Async events for the GDB event loop.
  2. Copyright (C) 1999-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 "async-event.h"
  16. #include "ser-event.h"
  17. #include "top.h"
  18. /* PROC is a function to be invoked when the READY flag is set. This
  19. happens when there has been a signal and the corresponding signal
  20. handler has 'triggered' this async_signal_handler for execution.
  21. The actual work to be done in response to a signal will be carried
  22. out by PROC at a later time, within process_event. This provides a
  23. deferred execution of signal handlers.
  24. Async_init_signals takes care of setting up such an
  25. async_signal_handler for each interesting signal. */
  26. struct async_signal_handler
  27. {
  28. /* If ready, call this handler from the main event loop, using
  29. invoke_async_handler. */
  30. int ready;
  31. /* Pointer to next handler. */
  32. struct async_signal_handler *next_handler;
  33. /* Function to call to do the work. */
  34. sig_handler_func *proc;
  35. /* Argument to PROC. */
  36. gdb_client_data client_data;
  37. /* User-friendly name of this handler. */
  38. const char *name;
  39. };
  40. /* PROC is a function to be invoked when the READY flag is set. This
  41. happens when the event has been marked with
  42. MARK_ASYNC_EVENT_HANDLER. The actual work to be done in response
  43. to an event will be carried out by PROC at a later time, within
  44. process_event. This provides a deferred execution of event
  45. handlers. */
  46. struct async_event_handler
  47. {
  48. /* If ready, call this handler from the main event loop, using
  49. invoke_event_handler. */
  50. int ready;
  51. /* Pointer to next handler. */
  52. struct async_event_handler *next_handler;
  53. /* Function to call to do the work. */
  54. async_event_handler_func *proc;
  55. /* Argument to PROC. */
  56. gdb_client_data client_data;
  57. /* User-friendly name of this handler. */
  58. const char *name;
  59. };
  60. /* All the async_signal_handlers gdb is interested in are kept onto
  61. this list. */
  62. static struct
  63. {
  64. /* Pointer to first in handler list. */
  65. async_signal_handler *first_handler;
  66. /* Pointer to last in handler list. */
  67. async_signal_handler *last_handler;
  68. }
  69. sighandler_list;
  70. /* All the async_event_handlers gdb is interested in are kept onto
  71. this list. */
  72. static struct
  73. {
  74. /* Pointer to first in handler list. */
  75. async_event_handler *first_handler;
  76. /* Pointer to last in handler list. */
  77. async_event_handler *last_handler;
  78. }
  79. async_event_handler_list;
  80. /* This event is signalled whenever an asynchronous handler needs to
  81. defer an action to the event loop. */
  82. static struct serial_event *async_signal_handlers_serial_event;
  83. /* Callback registered with ASYNC_SIGNAL_HANDLERS_SERIAL_EVENT. */
  84. static void
  85. async_signals_handler (int error, gdb_client_data client_data)
  86. {
  87. /* Do nothing. Handlers are run by invoke_async_signal_handlers
  88. from instead. */
  89. }
  90. void
  91. initialize_async_signal_handlers (void)
  92. {
  93. async_signal_handlers_serial_event = make_serial_event ();
  94. add_file_handler (serial_event_fd (async_signal_handlers_serial_event),
  95. async_signals_handler, NULL, "async-signals");
  96. }
  97. /* Create an asynchronous handler, allocating memory for it.
  98. Return a pointer to the newly created handler.
  99. This pointer will be used to invoke the handler by
  100. invoke_async_signal_handler.
  101. PROC is the function to call with CLIENT_DATA argument
  102. whenever the handler is invoked. */
  103. async_signal_handler *
  104. create_async_signal_handler (sig_handler_func * proc,
  105. gdb_client_data client_data,
  106. const char *name)
  107. {
  108. async_signal_handler *async_handler_ptr;
  109. async_handler_ptr = XNEW (async_signal_handler);
  110. async_handler_ptr->ready = 0;
  111. async_handler_ptr->next_handler = NULL;
  112. async_handler_ptr->proc = proc;
  113. async_handler_ptr->client_data = client_data;
  114. async_handler_ptr->name = name;
  115. if (sighandler_list.first_handler == NULL)
  116. sighandler_list.first_handler = async_handler_ptr;
  117. else
  118. sighandler_list.last_handler->next_handler = async_handler_ptr;
  119. sighandler_list.last_handler = async_handler_ptr;
  120. return async_handler_ptr;
  121. }
  122. /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
  123. will be used when the handlers are invoked, after we have waited
  124. for some event. The caller of this function is the interrupt
  125. handler associated with a signal. */
  126. void
  127. mark_async_signal_handler (async_signal_handler *async_handler_ptr)
  128. {
  129. if (debug_event_loop != debug_event_loop_kind::OFF)
  130. {
  131. /* This is called by signal handlers, so we print it "by hand" using
  132. the async-signal-safe methods. */
  133. const char head[] = ("[event-loop] mark_async_signal_handler: marking"
  134. "async signal handler `");
  135. gdb_stdlog->write_async_safe (head, strlen (head));
  136. gdb_stdlog->write_async_safe (async_handler_ptr->name,
  137. strlen (async_handler_ptr->name));
  138. const char tail[] = "`\n";
  139. gdb_stdlog->write_async_safe (tail, strlen (tail));
  140. }
  141. async_handler_ptr->ready = 1;
  142. serial_event_set (async_signal_handlers_serial_event);
  143. }
  144. /* See event-loop.h. */
  145. void
  146. clear_async_signal_handler (async_signal_handler *async_handler_ptr)
  147. {
  148. event_loop_debug_printf ("clearing async signal handler `%s`",
  149. async_handler_ptr->name);
  150. async_handler_ptr->ready = 0;
  151. }
  152. /* See event-loop.h. */
  153. int
  154. async_signal_handler_is_marked (async_signal_handler *async_handler_ptr)
  155. {
  156. return async_handler_ptr->ready;
  157. }
  158. /* Call all the handlers that are ready. Returns true if any was
  159. indeed ready. */
  160. int
  161. invoke_async_signal_handlers (void)
  162. {
  163. async_signal_handler *async_handler_ptr;
  164. int any_ready = 0;
  165. /* We're going to handle all pending signals, so no need to wake up
  166. the event loop again the next time around. Note this must be
  167. cleared _before_ calling the callbacks, to avoid races. */
  168. serial_event_clear (async_signal_handlers_serial_event);
  169. /* Invoke all ready handlers. */
  170. while (1)
  171. {
  172. for (async_handler_ptr = sighandler_list.first_handler;
  173. async_handler_ptr != NULL;
  174. async_handler_ptr = async_handler_ptr->next_handler)
  175. {
  176. if (async_handler_ptr->ready)
  177. break;
  178. }
  179. if (async_handler_ptr == NULL)
  180. break;
  181. any_ready = 1;
  182. async_handler_ptr->ready = 0;
  183. /* Async signal handlers have no connection to whichever was the
  184. current UI, and thus always run on the main one. */
  185. current_ui = main_ui;
  186. event_loop_debug_printf ("invoking async signal handler `%s`",
  187. async_handler_ptr->name);
  188. (*async_handler_ptr->proc) (async_handler_ptr->client_data);
  189. }
  190. return any_ready;
  191. }
  192. /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
  193. Free the space allocated for it. */
  194. void
  195. delete_async_signal_handler (async_signal_handler ** async_handler_ptr)
  196. {
  197. async_signal_handler *prev_ptr;
  198. if (sighandler_list.first_handler == (*async_handler_ptr))
  199. {
  200. sighandler_list.first_handler = (*async_handler_ptr)->next_handler;
  201. if (sighandler_list.first_handler == NULL)
  202. sighandler_list.last_handler = NULL;
  203. }
  204. else
  205. {
  206. prev_ptr = sighandler_list.first_handler;
  207. while (prev_ptr && prev_ptr->next_handler != (*async_handler_ptr))
  208. prev_ptr = prev_ptr->next_handler;
  209. gdb_assert (prev_ptr);
  210. prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
  211. if (sighandler_list.last_handler == (*async_handler_ptr))
  212. sighandler_list.last_handler = prev_ptr;
  213. }
  214. xfree ((*async_handler_ptr));
  215. (*async_handler_ptr) = NULL;
  216. }
  217. /* See async-event.h. */
  218. async_event_handler *
  219. create_async_event_handler (async_event_handler_func *proc,
  220. gdb_client_data client_data,
  221. const char *name)
  222. {
  223. async_event_handler *h;
  224. h = XNEW (struct async_event_handler);
  225. h->ready = 0;
  226. h->next_handler = NULL;
  227. h->proc = proc;
  228. h->client_data = client_data;
  229. h->name = name;
  230. if (async_event_handler_list.first_handler == NULL)
  231. async_event_handler_list.first_handler = h;
  232. else
  233. async_event_handler_list.last_handler->next_handler = h;
  234. async_event_handler_list.last_handler = h;
  235. return h;
  236. }
  237. /* Mark the handler (ASYNC_HANDLER_PTR) as ready. This information
  238. will be used by gdb_do_one_event. The caller will be whoever
  239. created the event source, and wants to signal that the event is
  240. ready to be handled. */
  241. void
  242. mark_async_event_handler (async_event_handler *async_handler_ptr)
  243. {
  244. event_loop_debug_printf ("marking async event handler `%s`",
  245. async_handler_ptr->name);
  246. async_handler_ptr->ready = 1;
  247. }
  248. /* See event-loop.h. */
  249. void
  250. clear_async_event_handler (async_event_handler *async_handler_ptr)
  251. {
  252. event_loop_debug_printf ("clearing async event handler `%s`",
  253. async_handler_ptr->name);
  254. async_handler_ptr->ready = 0;
  255. }
  256. /* See event-loop.h. */
  257. bool
  258. async_event_handler_marked (async_event_handler *handler)
  259. {
  260. return handler->ready;
  261. }
  262. /* Check if asynchronous event handlers are ready, and call the
  263. handler function for one that is. */
  264. int
  265. check_async_event_handlers ()
  266. {
  267. async_event_handler *async_handler_ptr;
  268. for (async_handler_ptr = async_event_handler_list.first_handler;
  269. async_handler_ptr != NULL;
  270. async_handler_ptr = async_handler_ptr->next_handler)
  271. {
  272. if (async_handler_ptr->ready)
  273. {
  274. event_loop_debug_printf ("invoking async event handler `%s`",
  275. async_handler_ptr->name);
  276. (*async_handler_ptr->proc) (async_handler_ptr->client_data);
  277. return 1;
  278. }
  279. }
  280. return 0;
  281. }
  282. /* Delete an asynchronous handler (ASYNC_HANDLER_PTR).
  283. Free the space allocated for it. */
  284. void
  285. delete_async_event_handler (async_event_handler **async_handler_ptr)
  286. {
  287. async_event_handler *prev_ptr;
  288. if (async_event_handler_list.first_handler == *async_handler_ptr)
  289. {
  290. async_event_handler_list.first_handler
  291. = (*async_handler_ptr)->next_handler;
  292. if (async_event_handler_list.first_handler == NULL)
  293. async_event_handler_list.last_handler = NULL;
  294. }
  295. else
  296. {
  297. prev_ptr = async_event_handler_list.first_handler;
  298. while (prev_ptr && prev_ptr->next_handler != *async_handler_ptr)
  299. prev_ptr = prev_ptr->next_handler;
  300. gdb_assert (prev_ptr);
  301. prev_ptr->next_handler = (*async_handler_ptr)->next_handler;
  302. if (async_event_handler_list.last_handler == (*async_handler_ptr))
  303. async_event_handler_list.last_handler = prev_ptr;
  304. }
  305. xfree (*async_handler_ptr);
  306. *async_handler_ptr = NULL;
  307. }