1*5796c8dcSSimon Schubert /* Handling of inferior events for the event loop for GDB, the GNU debugger. 2*5796c8dcSSimon Schubert Copyright (C) 1999, 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 #include "defs.h" 21*5796c8dcSSimon Schubert #include "inferior.h" /* For fetch_inferior_event. */ 22*5796c8dcSSimon Schubert #include "target.h" /* For enum inferior_event_type. */ 23*5796c8dcSSimon Schubert #include "event-loop.h" 24*5796c8dcSSimon Schubert #include "event-top.h" 25*5796c8dcSSimon Schubert #include "inf-loop.h" 26*5796c8dcSSimon Schubert #include "remote.h" 27*5796c8dcSSimon Schubert #include "exceptions.h" 28*5796c8dcSSimon Schubert #include "language.h" 29*5796c8dcSSimon Schubert #include "gdbthread.h" 30*5796c8dcSSimon Schubert 31*5796c8dcSSimon Schubert static int fetch_inferior_event_wrapper (gdb_client_data client_data); 32*5796c8dcSSimon Schubert 33*5796c8dcSSimon Schubert void 34*5796c8dcSSimon Schubert inferior_event_handler_wrapper (gdb_client_data client_data) 35*5796c8dcSSimon Schubert { 36*5796c8dcSSimon Schubert inferior_event_handler (INF_QUIT_REQ, client_data); 37*5796c8dcSSimon Schubert } 38*5796c8dcSSimon Schubert 39*5796c8dcSSimon Schubert /* General function to handle events in the inferior. So far it just 40*5796c8dcSSimon Schubert takes care of detecting errors reported by select() or poll(), 41*5796c8dcSSimon Schubert otherwise it assumes that all is OK, and goes on reading data from 42*5796c8dcSSimon Schubert the fd. This however may not always be what we want to do. */ 43*5796c8dcSSimon Schubert void 44*5796c8dcSSimon Schubert inferior_event_handler (enum inferior_event_type event_type, 45*5796c8dcSSimon Schubert gdb_client_data client_data) 46*5796c8dcSSimon Schubert { 47*5796c8dcSSimon Schubert struct gdb_exception e; 48*5796c8dcSSimon Schubert int was_sync = 0; 49*5796c8dcSSimon Schubert switch (event_type) 50*5796c8dcSSimon Schubert { 51*5796c8dcSSimon Schubert case INF_ERROR: 52*5796c8dcSSimon Schubert printf_unfiltered (_("error detected from target.\n")); 53*5796c8dcSSimon Schubert pop_all_targets_above (file_stratum, 0); 54*5796c8dcSSimon Schubert discard_all_intermediate_continuations (); 55*5796c8dcSSimon Schubert discard_all_continuations (); 56*5796c8dcSSimon Schubert async_enable_stdin (); 57*5796c8dcSSimon Schubert break; 58*5796c8dcSSimon Schubert 59*5796c8dcSSimon Schubert case INF_REG_EVENT: 60*5796c8dcSSimon Schubert /* Use catch errors for now, until the inner layers of 61*5796c8dcSSimon Schubert fetch_inferior_event (i.e. readchar) can return meaningful 62*5796c8dcSSimon Schubert error status. If an error occurs while getting an event from 63*5796c8dcSSimon Schubert the target, just get rid of the target. */ 64*5796c8dcSSimon Schubert if (!catch_errors (fetch_inferior_event_wrapper, 65*5796c8dcSSimon Schubert client_data, "", RETURN_MASK_ALL)) 66*5796c8dcSSimon Schubert { 67*5796c8dcSSimon Schubert pop_all_targets_above (file_stratum, 0); 68*5796c8dcSSimon Schubert discard_all_intermediate_continuations (); 69*5796c8dcSSimon Schubert discard_all_continuations (); 70*5796c8dcSSimon Schubert async_enable_stdin (); 71*5796c8dcSSimon Schubert display_gdb_prompt (0); 72*5796c8dcSSimon Schubert } 73*5796c8dcSSimon Schubert break; 74*5796c8dcSSimon Schubert 75*5796c8dcSSimon Schubert case INF_EXEC_COMPLETE: 76*5796c8dcSSimon Schubert 77*5796c8dcSSimon Schubert if (!non_stop) 78*5796c8dcSSimon Schubert { 79*5796c8dcSSimon Schubert /* Unregister the inferior from the event loop. This is done 80*5796c8dcSSimon Schubert so that when the inferior is not running we don't get 81*5796c8dcSSimon Schubert distracted by spurious inferior output. */ 82*5796c8dcSSimon Schubert if (target_has_execution) 83*5796c8dcSSimon Schubert target_async (NULL, 0); 84*5796c8dcSSimon Schubert } 85*5796c8dcSSimon Schubert 86*5796c8dcSSimon Schubert /* The call to async_enable_stdin below resets 'sync_execution'. 87*5796c8dcSSimon Schubert However, if sync_execution is 1 now, we also need to show the 88*5796c8dcSSimon Schubert prompt below, so save the current value. */ 89*5796c8dcSSimon Schubert was_sync = sync_execution; 90*5796c8dcSSimon Schubert async_enable_stdin (); 91*5796c8dcSSimon Schubert 92*5796c8dcSSimon Schubert /* Do all continuations associated with the whole inferior (not 93*5796c8dcSSimon Schubert a particular thread). */ 94*5796c8dcSSimon Schubert if (!ptid_equal (inferior_ptid, null_ptid)) 95*5796c8dcSSimon Schubert do_all_inferior_continuations (); 96*5796c8dcSSimon Schubert 97*5796c8dcSSimon Schubert /* If we were doing a multi-step (eg: step n, next n), but it 98*5796c8dcSSimon Schubert got interrupted by a breakpoint, still do the pending 99*5796c8dcSSimon Schubert continuations. The continuation itself is responsible for 100*5796c8dcSSimon Schubert distinguishing the cases. The continuations are allowed to 101*5796c8dcSSimon Schubert touch the inferior memory, e.g. to remove breakpoints, so run 102*5796c8dcSSimon Schubert them before running breakpoint commands, which may resume the 103*5796c8dcSSimon Schubert target. */ 104*5796c8dcSSimon Schubert if (non_stop 105*5796c8dcSSimon Schubert && target_has_execution 106*5796c8dcSSimon Schubert && !ptid_equal (inferior_ptid, null_ptid)) 107*5796c8dcSSimon Schubert do_all_intermediate_continuations_thread (inferior_thread ()); 108*5796c8dcSSimon Schubert else 109*5796c8dcSSimon Schubert do_all_intermediate_continuations (); 110*5796c8dcSSimon Schubert 111*5796c8dcSSimon Schubert /* Always finish the previous command before running any 112*5796c8dcSSimon Schubert breakpoint commands. Any stop cancels the previous command. 113*5796c8dcSSimon Schubert E.g. a "finish" or "step-n" command interrupted by an 114*5796c8dcSSimon Schubert unrelated breakpoint is canceled. */ 115*5796c8dcSSimon Schubert if (non_stop 116*5796c8dcSSimon Schubert && target_has_execution 117*5796c8dcSSimon Schubert && !ptid_equal (inferior_ptid, null_ptid)) 118*5796c8dcSSimon Schubert do_all_continuations_thread (inferior_thread ()); 119*5796c8dcSSimon Schubert else 120*5796c8dcSSimon Schubert do_all_continuations (); 121*5796c8dcSSimon Schubert 122*5796c8dcSSimon Schubert if (current_language != expected_language 123*5796c8dcSSimon Schubert && language_mode == language_mode_auto) 124*5796c8dcSSimon Schubert language_info (1); /* Print what changed. */ 125*5796c8dcSSimon Schubert 126*5796c8dcSSimon Schubert /* Don't propagate breakpoint commands errors. Either we're 127*5796c8dcSSimon Schubert stopping or some command resumes the inferior. The user will 128*5796c8dcSSimon Schubert be informed. */ 129*5796c8dcSSimon Schubert TRY_CATCH (e, RETURN_MASK_ALL) 130*5796c8dcSSimon Schubert { 131*5796c8dcSSimon Schubert bpstat_do_actions (); 132*5796c8dcSSimon Schubert } 133*5796c8dcSSimon Schubert 134*5796c8dcSSimon Schubert if (!was_sync 135*5796c8dcSSimon Schubert && exec_done_display_p 136*5796c8dcSSimon Schubert && (ptid_equal (inferior_ptid, null_ptid) 137*5796c8dcSSimon Schubert || !is_running (inferior_ptid))) 138*5796c8dcSSimon Schubert printf_unfiltered (_("completed.\n")); 139*5796c8dcSSimon Schubert break; 140*5796c8dcSSimon Schubert 141*5796c8dcSSimon Schubert case INF_EXEC_CONTINUE: 142*5796c8dcSSimon Schubert /* Is there anything left to do for the command issued to 143*5796c8dcSSimon Schubert complete? */ 144*5796c8dcSSimon Schubert 145*5796c8dcSSimon Schubert if (non_stop) 146*5796c8dcSSimon Schubert do_all_intermediate_continuations_thread (inferior_thread ()); 147*5796c8dcSSimon Schubert else 148*5796c8dcSSimon Schubert do_all_intermediate_continuations (); 149*5796c8dcSSimon Schubert break; 150*5796c8dcSSimon Schubert 151*5796c8dcSSimon Schubert case INF_QUIT_REQ: 152*5796c8dcSSimon Schubert /* FIXME: ezannoni 1999-10-04. This call should really be a 153*5796c8dcSSimon Schubert target vector entry, so that it can be used for any kind of 154*5796c8dcSSimon Schubert targets. */ 155*5796c8dcSSimon Schubert async_remote_interrupt_twice (NULL); 156*5796c8dcSSimon Schubert break; 157*5796c8dcSSimon Schubert 158*5796c8dcSSimon Schubert case INF_TIMER: 159*5796c8dcSSimon Schubert default: 160*5796c8dcSSimon Schubert printf_unfiltered (_("Event type not recognized.\n")); 161*5796c8dcSSimon Schubert break; 162*5796c8dcSSimon Schubert } 163*5796c8dcSSimon Schubert } 164*5796c8dcSSimon Schubert 165*5796c8dcSSimon Schubert static int 166*5796c8dcSSimon Schubert fetch_inferior_event_wrapper (gdb_client_data client_data) 167*5796c8dcSSimon Schubert { 168*5796c8dcSSimon Schubert fetch_inferior_event (client_data); 169*5796c8dcSSimon Schubert return 1; 170*5796c8dcSSimon Schubert } 171