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