event-loop.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Definitions used by the GDB event loop.
  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. #ifndef EVENT_LOOP_H
  16. #define EVENT_LOOP_H
  17. /* An event loop listens for events from multiple event sources. When
  18. an event arrives, it is queued and processed by calling the
  19. appropriate event handler. The event loop then continues to listen
  20. for more events. An event loop completes when there are no event
  21. sources to listen on. External event sources can be plugged into
  22. the loop.
  23. There are 4 main components:
  24. - a list of file descriptors to be monitored, GDB_NOTIFIER.
  25. - a list of asynchronous event sources to be monitored,
  26. ASYNC_EVENT_HANDLER_LIST.
  27. - a list of events that have occurred, EVENT_QUEUE.
  28. - a list of signal handling functions, SIGHANDLER_LIST.
  29. GDB_NOTIFIER keeps track of the file descriptor based event
  30. sources. ASYNC_EVENT_HANDLER_LIST keeps track of asynchronous
  31. event sources that are signalled by some component of gdb, usually
  32. a target_ops instance. Event sources for gdb are currently the UI
  33. and the target. Gdb communicates with the command line user
  34. interface via the readline library and usually communicates with
  35. remote targets via a serial port. Serial ports are represented in
  36. GDB as file descriptors and select/poll calls. For native targets
  37. instead, the communication varies across operating system debug
  38. APIs, but usually consists of calls to ptrace and waits (via
  39. signals) or calls to poll/select (via file descriptors). In the
  40. current gdb, the code handling events related to the target resides
  41. in wait_for_inferior for synchronous targets; or, for asynchronous
  42. capable targets, by having the target register either a target
  43. controlled file descriptor and/or an asynchronous event source in
  44. the event loop, with the fetch_inferior_event function as the event
  45. callback. In both the synchronous and asynchronous cases, usually
  46. the target event is collected through the target_wait interface.
  47. The target is free to install other event sources in the event loop
  48. if it so requires.
  49. EVENT_QUEUE keeps track of the events that have happened during the
  50. last iteration of the event loop, and need to be processed. An
  51. event is represented by a procedure to be invoked in order to
  52. process the event. The queue is scanned head to tail. If the
  53. event of interest is a change of state in a file descriptor, then a
  54. call to poll or select will be made to detect it.
  55. If the events generate signals, they are also queued by special
  56. functions that are invoked through traditional signal handlers.
  57. The actions to be taken is response to such events will be executed
  58. when the SIGHANDLER_LIST is scanned, the next time through the
  59. infinite loop.
  60. Corollary tasks are the creation and deletion of event sources. */
  61. typedef void *gdb_client_data;
  62. typedef void (handler_func) (int, gdb_client_data);
  63. typedef void (timer_handler_func) (gdb_client_data);
  64. /* Exported functions from event-loop.c */
  65. extern int gdb_do_one_event (void);
  66. extern void delete_file_handler (int fd);
  67. /* Add a file handler/descriptor to the list of descriptors we are
  68. interested in.
  69. FD is the file descriptor for the file/stream to be listened to.
  70. NAME is a user-friendly name for the handler.
  71. If IS_UI is set, this file descriptor is used for a user interface. */
  72. extern void add_file_handler (int fd, handler_func *proc,
  73. gdb_client_data client_data,
  74. std::string &&name, bool is_ui = false);
  75. extern int create_timer (int milliseconds,
  76. timer_handler_func *proc,
  77. gdb_client_data client_data);
  78. extern void delete_timer (int id);
  79. /* Must be defined by client. */
  80. extern void handle_event_loop_exception (const gdb_exception &);
  81. /* Must be defined by client. Returns true if any signal handler was
  82. ready. */
  83. extern int invoke_async_signal_handlers ();
  84. /* Must be defined by client. Returns true if any event handler was
  85. ready. */
  86. extern int check_async_event_handlers ();
  87. enum class debug_event_loop_kind
  88. {
  89. OFF,
  90. /* Print all event-loop related messages, except events from user-interface
  91. event sources. */
  92. ALL_EXCEPT_UI,
  93. /* Print all event-loop related messages. */
  94. ALL,
  95. };
  96. /* True if we are printing event loop debug statements. */
  97. extern debug_event_loop_kind debug_event_loop;
  98. /* Print an "event loop" debug statement. */
  99. #define event_loop_debug_printf(fmt, ...) \
  100. debug_prefixed_printf_cond (debug_event_loop != debug_event_loop_kind::OFF, \
  101. "event-loop", fmt, ##__VA_ARGS__)
  102. /* Print an "event loop" debug statement that is know to come from a UI-related
  103. event (e.g. calling the event handler for the fd of the CLI). */
  104. #define event_loop_ui_debug_printf(is_ui, fmt, ...) \
  105. do \
  106. { \
  107. if (debug_event_loop == debug_event_loop_kind::ALL \
  108. || (debug_event_loop == debug_event_loop_kind::ALL_EXCEPT_UI \
  109. && !is_ui)) \
  110. debug_prefixed_printf ("event-loop", __func__, fmt, ##__VA_ARGS__); \
  111. } \
  112. while (0)
  113. #endif /* EVENT_LOOP_H */