15796c8dcSSimon Schubert /* Definitions used by the GDB event loop. 2*ef5ccd6cSJohn Marino Copyright (C) 1999-2013 Free Software Foundation, Inc. 35796c8dcSSimon Schubert Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions. 45796c8dcSSimon Schubert 55796c8dcSSimon Schubert This file is part of GDB. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 85796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 95796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 105796c8dcSSimon Schubert (at your option) any later version. 115796c8dcSSimon Schubert 125796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 135796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 145796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 155796c8dcSSimon Schubert GNU General Public License for more details. 165796c8dcSSimon Schubert 175796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 185796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 195796c8dcSSimon Schubert 205796c8dcSSimon Schubert /* An event loop listens for events from multiple event sources. When 215796c8dcSSimon Schubert an event arrives, it is queued and processed by calling the 225796c8dcSSimon Schubert appropriate event handler. The event loop then continues to listen 235796c8dcSSimon Schubert for more events. An event loop completes when there are no event 245796c8dcSSimon Schubert sources to listen on. External event sources can be plugged into 255796c8dcSSimon Schubert the loop. 265796c8dcSSimon Schubert 275796c8dcSSimon Schubert There are 4 main components: 285796c8dcSSimon Schubert - a list of file descriptors to be monitored, GDB_NOTIFIER. 295796c8dcSSimon Schubert - a list of asynchronous event sources to be monitored, 305796c8dcSSimon Schubert ASYNC_EVENT_HANDLER_LIST. 315796c8dcSSimon Schubert - a list of events that have occurred, EVENT_QUEUE. 325796c8dcSSimon Schubert - a list of signal handling functions, SIGHANDLER_LIST. 335796c8dcSSimon Schubert 345796c8dcSSimon Schubert GDB_NOTIFIER keeps track of the file descriptor based event 355796c8dcSSimon Schubert sources. ASYNC_EVENT_HANDLER_LIST keeps track of asynchronous 365796c8dcSSimon Schubert event sources that are signalled by some component of gdb, usually 375796c8dcSSimon Schubert a target_ops instance. Event sources for gdb are currently the UI 385796c8dcSSimon Schubert and the target. Gdb communicates with the command line user 395796c8dcSSimon Schubert interface via the readline library and usually communicates with 405796c8dcSSimon Schubert remote targets via a serial port. Serial ports are represented in 415796c8dcSSimon Schubert GDB as file descriptors and select/poll calls. For native targets 425796c8dcSSimon Schubert instead, the communication varies across operating system debug 435796c8dcSSimon Schubert APIs, but usually consists of calls to ptrace and waits (via 445796c8dcSSimon Schubert signals) or calls to poll/select (via file descriptors). In the 455796c8dcSSimon Schubert current gdb, the code handling events related to the target resides 465796c8dcSSimon Schubert in wait_for_inferior for synchronous targets; or, for asynchronous 475796c8dcSSimon Schubert capable targets, by having the target register either a target 485796c8dcSSimon Schubert controlled file descriptor and/or an asynchronous event source in 495796c8dcSSimon Schubert the event loop, with the fetch_inferior_event function as the event 505796c8dcSSimon Schubert callback. In both the synchronous and asynchronous cases, usually 515796c8dcSSimon Schubert the target event is collected through the target_wait interface. 525796c8dcSSimon Schubert The target is free to install other event sources in the event loop 535796c8dcSSimon Schubert if it so requires. 545796c8dcSSimon Schubert 555796c8dcSSimon Schubert EVENT_QUEUE keeps track of the events that have happened during the 565796c8dcSSimon Schubert last iteration of the event loop, and need to be processed. An 575796c8dcSSimon Schubert event is represented by a procedure to be invoked in order to 585796c8dcSSimon Schubert process the event. The queue is scanned head to tail. If the 595796c8dcSSimon Schubert event of interest is a change of state in a file descriptor, then a 605796c8dcSSimon Schubert call to poll or select will be made to detect it. 615796c8dcSSimon Schubert 625796c8dcSSimon Schubert If the events generate signals, they are also queued by special 635796c8dcSSimon Schubert functions that are invoked through traditional signal handlers. 645796c8dcSSimon Schubert The actions to be taken is response to such events will be executed 655796c8dcSSimon Schubert when the SIGHANDLER_LIST is scanned, the next time through the 665796c8dcSSimon Schubert infinite loop. 675796c8dcSSimon Schubert 685796c8dcSSimon Schubert Corollary tasks are the creation and deletion of event sources. */ 695796c8dcSSimon Schubert 705796c8dcSSimon Schubert typedef void *gdb_client_data; 715796c8dcSSimon Schubert struct async_signal_handler; 725796c8dcSSimon Schubert struct async_event_handler; 735796c8dcSSimon Schubert typedef void (handler_func) (int, gdb_client_data); 745796c8dcSSimon Schubert typedef void (sig_handler_func) (gdb_client_data); 755796c8dcSSimon Schubert typedef void (async_event_handler_func) (gdb_client_data); 765796c8dcSSimon Schubert typedef void (timer_handler_func) (gdb_client_data); 775796c8dcSSimon Schubert 785796c8dcSSimon Schubert /* Exported functions from event-loop.c */ 795796c8dcSSimon Schubert 80*ef5ccd6cSJohn Marino extern void initialize_event_loop (void); 815796c8dcSSimon Schubert extern void start_event_loop (void); 82a45ae5f8SJohn Marino extern int gdb_do_one_event (void); 835796c8dcSSimon Schubert extern void delete_file_handler (int fd); 84c50c785cSJohn Marino extern void add_file_handler (int fd, handler_func *proc, 85c50c785cSJohn Marino gdb_client_data client_data); 865796c8dcSSimon Schubert extern struct async_signal_handler * 87c50c785cSJohn Marino create_async_signal_handler (sig_handler_func *proc, 88c50c785cSJohn Marino gdb_client_data client_data); 89c50c785cSJohn Marino extern void delete_async_signal_handler (struct async_signal_handler **); 90c50c785cSJohn Marino extern int create_timer (int milliseconds, 91c50c785cSJohn Marino timer_handler_func *proc, 92c50c785cSJohn Marino gdb_client_data client_data); 935796c8dcSSimon Schubert extern void delete_timer (int id); 945796c8dcSSimon Schubert 955796c8dcSSimon Schubert /* Call the handler from HANDLER immediately. This function 965796c8dcSSimon Schubert runs signal handlers when returning to the event loop would be too 975796c8dcSSimon Schubert slow. Do not call this directly; use gdb_call_async_signal_handler, 985796c8dcSSimon Schubert below, with IMMEDIATE_P == 1. */ 995796c8dcSSimon Schubert void call_async_signal_handler (struct async_signal_handler *handler); 1005796c8dcSSimon Schubert 1015796c8dcSSimon Schubert /* Call the handler from HANDLER the next time through the event loop. 1025796c8dcSSimon Schubert Do not call this directly; use gdb_call_async_signal_handler, 1035796c8dcSSimon Schubert below, with IMMEDIATE_P == 0. */ 1045796c8dcSSimon Schubert void mark_async_signal_handler (struct async_signal_handler *handler); 1055796c8dcSSimon Schubert 1065796c8dcSSimon Schubert /* Wrapper for the body of signal handlers. Call this function from 1075796c8dcSSimon Schubert any SIGINT handler which needs to access GDB data structures or 1085796c8dcSSimon Schubert escape via longjmp. If IMMEDIATE_P is set, this triggers either 1095796c8dcSSimon Schubert immediately (for POSIX platforms), or from gdb_select (for 1105796c8dcSSimon Schubert MinGW). If IMMEDIATE_P is clear, the handler will run the next 1115796c8dcSSimon Schubert time we return to the event loop and any current select calls 1125796c8dcSSimon Schubert will be interrupted. */ 1135796c8dcSSimon Schubert 1145796c8dcSSimon Schubert void gdb_call_async_signal_handler (struct async_signal_handler *handler, 1155796c8dcSSimon Schubert int immediate_p); 1165796c8dcSSimon Schubert 1175796c8dcSSimon Schubert /* Create and register an asynchronous event source in the event loop, 1185796c8dcSSimon Schubert and set PROC as its callback. CLIENT_DATA is passed as argument to 1195796c8dcSSimon Schubert PROC upon its invocation. Returns a pointer to an opaque structure 1205796c8dcSSimon Schubert used to mark as ready and to later delete this event source from 1215796c8dcSSimon Schubert the event loop. */ 1225796c8dcSSimon Schubert extern struct async_event_handler * 1235796c8dcSSimon Schubert create_async_event_handler (async_event_handler_func *proc, 1245796c8dcSSimon Schubert gdb_client_data client_data); 1255796c8dcSSimon Schubert 1265796c8dcSSimon Schubert /* Remove the event source pointed by HANDLER_PTR created by 1275796c8dcSSimon Schubert CREATE_ASYNC_EVENT_HANDLER from the event loop, and release it. */ 1285796c8dcSSimon Schubert extern void 1295796c8dcSSimon Schubert delete_async_event_handler (struct async_event_handler **handler_ptr); 1305796c8dcSSimon Schubert 1315796c8dcSSimon Schubert /* Call the handler from HANDLER the next time through the event 1325796c8dcSSimon Schubert loop. */ 1335796c8dcSSimon Schubert extern void mark_async_event_handler (struct async_event_handler *handler); 134