1*5796c8dcSSimon Schubert /* Definitions used by the GDB event loop. 2*5796c8dcSSimon Schubert Copyright (C) 1999, 2000, 2007, 2008, 2009 Free Software Foundation, Inc. 3*5796c8dcSSimon Schubert Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions. 4*5796c8dcSSimon Schubert 5*5796c8dcSSimon Schubert This file is part of GDB. 6*5796c8dcSSimon Schubert 7*5796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 8*5796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 9*5796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 10*5796c8dcSSimon Schubert (at your option) any later version. 11*5796c8dcSSimon Schubert 12*5796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 13*5796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 14*5796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*5796c8dcSSimon Schubert GNU General Public License for more details. 16*5796c8dcSSimon Schubert 17*5796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 18*5796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19*5796c8dcSSimon Schubert 20*5796c8dcSSimon Schubert /* An event loop listens for events from multiple event sources. When 21*5796c8dcSSimon Schubert an event arrives, it is queued and processed by calling the 22*5796c8dcSSimon Schubert appropriate event handler. The event loop then continues to listen 23*5796c8dcSSimon Schubert for more events. An event loop completes when there are no event 24*5796c8dcSSimon Schubert sources to listen on. External event sources can be plugged into 25*5796c8dcSSimon Schubert the loop. 26*5796c8dcSSimon Schubert 27*5796c8dcSSimon Schubert There are 4 main components: 28*5796c8dcSSimon Schubert - a list of file descriptors to be monitored, GDB_NOTIFIER. 29*5796c8dcSSimon Schubert - a list of asynchronous event sources to be monitored, 30*5796c8dcSSimon Schubert ASYNC_EVENT_HANDLER_LIST. 31*5796c8dcSSimon Schubert - a list of events that have occurred, EVENT_QUEUE. 32*5796c8dcSSimon Schubert - a list of signal handling functions, SIGHANDLER_LIST. 33*5796c8dcSSimon Schubert 34*5796c8dcSSimon Schubert GDB_NOTIFIER keeps track of the file descriptor based event 35*5796c8dcSSimon Schubert sources. ASYNC_EVENT_HANDLER_LIST keeps track of asynchronous 36*5796c8dcSSimon Schubert event sources that are signalled by some component of gdb, usually 37*5796c8dcSSimon Schubert a target_ops instance. Event sources for gdb are currently the UI 38*5796c8dcSSimon Schubert and the target. Gdb communicates with the command line user 39*5796c8dcSSimon Schubert interface via the readline library and usually communicates with 40*5796c8dcSSimon Schubert remote targets via a serial port. Serial ports are represented in 41*5796c8dcSSimon Schubert GDB as file descriptors and select/poll calls. For native targets 42*5796c8dcSSimon Schubert instead, the communication varies across operating system debug 43*5796c8dcSSimon Schubert APIs, but usually consists of calls to ptrace and waits (via 44*5796c8dcSSimon Schubert signals) or calls to poll/select (via file descriptors). In the 45*5796c8dcSSimon Schubert current gdb, the code handling events related to the target resides 46*5796c8dcSSimon Schubert in wait_for_inferior for synchronous targets; or, for asynchronous 47*5796c8dcSSimon Schubert capable targets, by having the target register either a target 48*5796c8dcSSimon Schubert controlled file descriptor and/or an asynchronous event source in 49*5796c8dcSSimon Schubert the event loop, with the fetch_inferior_event function as the event 50*5796c8dcSSimon Schubert callback. In both the synchronous and asynchronous cases, usually 51*5796c8dcSSimon Schubert the target event is collected through the target_wait interface. 52*5796c8dcSSimon Schubert The target is free to install other event sources in the event loop 53*5796c8dcSSimon Schubert if it so requires. 54*5796c8dcSSimon Schubert 55*5796c8dcSSimon Schubert EVENT_QUEUE keeps track of the events that have happened during the 56*5796c8dcSSimon Schubert last iteration of the event loop, and need to be processed. An 57*5796c8dcSSimon Schubert event is represented by a procedure to be invoked in order to 58*5796c8dcSSimon Schubert process the event. The queue is scanned head to tail. If the 59*5796c8dcSSimon Schubert event of interest is a change of state in a file descriptor, then a 60*5796c8dcSSimon Schubert call to poll or select will be made to detect it. 61*5796c8dcSSimon Schubert 62*5796c8dcSSimon Schubert If the events generate signals, they are also queued by special 63*5796c8dcSSimon Schubert functions that are invoked through traditional signal handlers. 64*5796c8dcSSimon Schubert The actions to be taken is response to such events will be executed 65*5796c8dcSSimon Schubert when the SIGHANDLER_LIST is scanned, the next time through the 66*5796c8dcSSimon Schubert infinite loop. 67*5796c8dcSSimon Schubert 68*5796c8dcSSimon Schubert Corollary tasks are the creation and deletion of event sources. */ 69*5796c8dcSSimon Schubert 70*5796c8dcSSimon Schubert typedef void *gdb_client_data; 71*5796c8dcSSimon Schubert struct async_signal_handler; 72*5796c8dcSSimon Schubert struct async_event_handler; 73*5796c8dcSSimon Schubert typedef void (handler_func) (int, gdb_client_data); 74*5796c8dcSSimon Schubert typedef void (sig_handler_func) (gdb_client_data); 75*5796c8dcSSimon Schubert typedef void (async_event_handler_func) (gdb_client_data); 76*5796c8dcSSimon Schubert typedef void (timer_handler_func) (gdb_client_data); 77*5796c8dcSSimon Schubert 78*5796c8dcSSimon Schubert /* Where to add an event onto the event queue, by queue_event. */ 79*5796c8dcSSimon Schubert typedef enum 80*5796c8dcSSimon Schubert { 81*5796c8dcSSimon Schubert /* Add at tail of queue. It will be processed in first in first 82*5796c8dcSSimon Schubert out order. */ 83*5796c8dcSSimon Schubert TAIL, 84*5796c8dcSSimon Schubert /* Add at head of queue. It will be processed in last in first out 85*5796c8dcSSimon Schubert order. */ 86*5796c8dcSSimon Schubert HEAD 87*5796c8dcSSimon Schubert } 88*5796c8dcSSimon Schubert queue_position; 89*5796c8dcSSimon Schubert 90*5796c8dcSSimon Schubert /* Tell create_file_handler what events we are interested in. 91*5796c8dcSSimon Schubert This is used by the select version of the event loop. */ 92*5796c8dcSSimon Schubert 93*5796c8dcSSimon Schubert #define GDB_READABLE (1<<1) 94*5796c8dcSSimon Schubert #define GDB_WRITABLE (1<<2) 95*5796c8dcSSimon Schubert #define GDB_EXCEPTION (1<<3) 96*5796c8dcSSimon Schubert 97*5796c8dcSSimon Schubert /* Exported functions from event-loop.c */ 98*5796c8dcSSimon Schubert 99*5796c8dcSSimon Schubert extern void start_event_loop (void); 100*5796c8dcSSimon Schubert extern int gdb_do_one_event (void *data); 101*5796c8dcSSimon Schubert extern void delete_file_handler (int fd); 102*5796c8dcSSimon Schubert extern void add_file_handler (int fd, handler_func * proc, gdb_client_data client_data); 103*5796c8dcSSimon Schubert extern struct async_signal_handler * 104*5796c8dcSSimon Schubert create_async_signal_handler (sig_handler_func * proc, gdb_client_data client_data); 105*5796c8dcSSimon Schubert extern void delete_async_signal_handler (struct async_signal_handler **async_handler_ptr); 106*5796c8dcSSimon Schubert extern int create_timer (int milliseconds, timer_handler_func * proc, gdb_client_data client_data); 107*5796c8dcSSimon Schubert extern void delete_timer (int id); 108*5796c8dcSSimon Schubert 109*5796c8dcSSimon Schubert /* Call the handler from HANDLER immediately. This function 110*5796c8dcSSimon Schubert runs signal handlers when returning to the event loop would be too 111*5796c8dcSSimon Schubert slow. Do not call this directly; use gdb_call_async_signal_handler, 112*5796c8dcSSimon Schubert below, with IMMEDIATE_P == 1. */ 113*5796c8dcSSimon Schubert void call_async_signal_handler (struct async_signal_handler *handler); 114*5796c8dcSSimon Schubert 115*5796c8dcSSimon Schubert /* Call the handler from HANDLER the next time through the event loop. 116*5796c8dcSSimon Schubert Do not call this directly; use gdb_call_async_signal_handler, 117*5796c8dcSSimon Schubert below, with IMMEDIATE_P == 0. */ 118*5796c8dcSSimon Schubert void mark_async_signal_handler (struct async_signal_handler *handler); 119*5796c8dcSSimon Schubert 120*5796c8dcSSimon Schubert /* Wrapper for the body of signal handlers. Call this function from 121*5796c8dcSSimon Schubert any SIGINT handler which needs to access GDB data structures or 122*5796c8dcSSimon Schubert escape via longjmp. If IMMEDIATE_P is set, this triggers either 123*5796c8dcSSimon Schubert immediately (for POSIX platforms), or from gdb_select (for 124*5796c8dcSSimon Schubert MinGW). If IMMEDIATE_P is clear, the handler will run the next 125*5796c8dcSSimon Schubert time we return to the event loop and any current select calls 126*5796c8dcSSimon Schubert will be interrupted. */ 127*5796c8dcSSimon Schubert 128*5796c8dcSSimon Schubert void gdb_call_async_signal_handler (struct async_signal_handler *handler, 129*5796c8dcSSimon Schubert int immediate_p); 130*5796c8dcSSimon Schubert 131*5796c8dcSSimon Schubert /* Create and register an asynchronous event source in the event loop, 132*5796c8dcSSimon Schubert and set PROC as its callback. CLIENT_DATA is passed as argument to 133*5796c8dcSSimon Schubert PROC upon its invocation. Returns a pointer to an opaque structure 134*5796c8dcSSimon Schubert used to mark as ready and to later delete this event source from 135*5796c8dcSSimon Schubert the event loop. */ 136*5796c8dcSSimon Schubert extern struct async_event_handler * 137*5796c8dcSSimon Schubert create_async_event_handler (async_event_handler_func *proc, 138*5796c8dcSSimon Schubert gdb_client_data client_data); 139*5796c8dcSSimon Schubert 140*5796c8dcSSimon Schubert /* Remove the event source pointed by HANDLER_PTR created by 141*5796c8dcSSimon Schubert CREATE_ASYNC_EVENT_HANDLER from the event loop, and release it. */ 142*5796c8dcSSimon Schubert extern void 143*5796c8dcSSimon Schubert delete_async_event_handler (struct async_event_handler **handler_ptr); 144*5796c8dcSSimon Schubert 145*5796c8dcSSimon Schubert /* Call the handler from HANDLER the next time through the event 146*5796c8dcSSimon Schubert loop. */ 147*5796c8dcSSimon Schubert extern void mark_async_event_handler (struct async_event_handler *handler); 148