15796c8dcSSimon Schubert /* Handling of inferior events for the event loop for GDB, the GNU debugger. 2*a45ae5f8SJohn Marino Copyright (C) 1999, 2007-2012 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 #include "defs.h" 215796c8dcSSimon Schubert #include "inferior.h" /* For fetch_inferior_event. */ 225796c8dcSSimon Schubert #include "target.h" /* For enum inferior_event_type. */ 235796c8dcSSimon Schubert #include "event-loop.h" 245796c8dcSSimon Schubert #include "event-top.h" 255796c8dcSSimon Schubert #include "inf-loop.h" 265796c8dcSSimon Schubert #include "remote.h" 275796c8dcSSimon Schubert #include "exceptions.h" 285796c8dcSSimon Schubert #include "language.h" 295796c8dcSSimon Schubert #include "gdbthread.h" 30*a45ae5f8SJohn Marino #include "continuations.h" 31*a45ae5f8SJohn Marino #include "interps.h" 325796c8dcSSimon Schubert 335796c8dcSSimon Schubert static int fetch_inferior_event_wrapper (gdb_client_data client_data); 345796c8dcSSimon Schubert 355796c8dcSSimon Schubert /* General function to handle events in the inferior. So far it just 365796c8dcSSimon Schubert takes care of detecting errors reported by select() or poll(), 375796c8dcSSimon Schubert otherwise it assumes that all is OK, and goes on reading data from 385796c8dcSSimon Schubert the fd. This however may not always be what we want to do. */ 395796c8dcSSimon Schubert void 405796c8dcSSimon Schubert inferior_event_handler (enum inferior_event_type event_type, 415796c8dcSSimon Schubert gdb_client_data client_data) 425796c8dcSSimon Schubert { 43*a45ae5f8SJohn Marino struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup (); 44cf7f2e2dSJohn Marino 455796c8dcSSimon Schubert switch (event_type) 465796c8dcSSimon Schubert { 475796c8dcSSimon Schubert case INF_REG_EVENT: 485796c8dcSSimon Schubert /* Use catch errors for now, until the inner layers of 495796c8dcSSimon Schubert fetch_inferior_event (i.e. readchar) can return meaningful 505796c8dcSSimon Schubert error status. If an error occurs while getting an event from 51*a45ae5f8SJohn Marino the target, just cancel the current command. */ 525796c8dcSSimon Schubert if (!catch_errors (fetch_inferior_event_wrapper, 535796c8dcSSimon Schubert client_data, "", RETURN_MASK_ALL)) 545796c8dcSSimon Schubert { 55*a45ae5f8SJohn Marino bpstat_clear_actions (); 56*a45ae5f8SJohn Marino do_all_intermediate_continuations (1); 57*a45ae5f8SJohn Marino do_all_continuations (1); 585796c8dcSSimon Schubert async_enable_stdin (); 595796c8dcSSimon Schubert display_gdb_prompt (0); 605796c8dcSSimon Schubert } 615796c8dcSSimon Schubert break; 625796c8dcSSimon Schubert 635796c8dcSSimon Schubert case INF_EXEC_COMPLETE: 645796c8dcSSimon Schubert if (!non_stop) 655796c8dcSSimon Schubert { 665796c8dcSSimon Schubert /* Unregister the inferior from the event loop. This is done 675796c8dcSSimon Schubert so that when the inferior is not running we don't get 685796c8dcSSimon Schubert distracted by spurious inferior output. */ 695796c8dcSSimon Schubert if (target_has_execution) 705796c8dcSSimon Schubert target_async (NULL, 0); 715796c8dcSSimon Schubert } 725796c8dcSSimon Schubert 735796c8dcSSimon Schubert /* Do all continuations associated with the whole inferior (not 745796c8dcSSimon Schubert a particular thread). */ 755796c8dcSSimon Schubert if (!ptid_equal (inferior_ptid, null_ptid)) 76*a45ae5f8SJohn Marino do_all_inferior_continuations (0); 775796c8dcSSimon Schubert 785796c8dcSSimon Schubert /* If we were doing a multi-step (eg: step n, next n), but it 795796c8dcSSimon Schubert got interrupted by a breakpoint, still do the pending 805796c8dcSSimon Schubert continuations. The continuation itself is responsible for 815796c8dcSSimon Schubert distinguishing the cases. The continuations are allowed to 825796c8dcSSimon Schubert touch the inferior memory, e.g. to remove breakpoints, so run 835796c8dcSSimon Schubert them before running breakpoint commands, which may resume the 845796c8dcSSimon Schubert target. */ 855796c8dcSSimon Schubert if (non_stop 865796c8dcSSimon Schubert && target_has_execution 875796c8dcSSimon Schubert && !ptid_equal (inferior_ptid, null_ptid)) 88*a45ae5f8SJohn Marino do_all_intermediate_continuations_thread (inferior_thread (), 0); 895796c8dcSSimon Schubert else 90*a45ae5f8SJohn Marino do_all_intermediate_continuations (0); 915796c8dcSSimon Schubert 925796c8dcSSimon Schubert /* Always finish the previous command before running any 935796c8dcSSimon Schubert breakpoint commands. Any stop cancels the previous command. 945796c8dcSSimon Schubert E.g. a "finish" or "step-n" command interrupted by an 955796c8dcSSimon Schubert unrelated breakpoint is canceled. */ 965796c8dcSSimon Schubert if (non_stop 975796c8dcSSimon Schubert && target_has_execution 985796c8dcSSimon Schubert && !ptid_equal (inferior_ptid, null_ptid)) 99*a45ae5f8SJohn Marino do_all_continuations_thread (inferior_thread (), 0); 1005796c8dcSSimon Schubert else 101*a45ae5f8SJohn Marino do_all_continuations (0); 1025796c8dcSSimon Schubert 103*a45ae5f8SJohn Marino /* When running a command list (from a user command, say), these 104*a45ae5f8SJohn Marino are only run when the command list is all done. */ 105*a45ae5f8SJohn Marino if (interpreter_async) 106*a45ae5f8SJohn Marino { 107*a45ae5f8SJohn Marino volatile struct gdb_exception e; 108*a45ae5f8SJohn Marino 109*a45ae5f8SJohn Marino if (info_verbose 110*a45ae5f8SJohn Marino && current_language != expected_language 1115796c8dcSSimon Schubert && language_mode == language_mode_auto) 1125796c8dcSSimon Schubert language_info (1); /* Print what changed. */ 1135796c8dcSSimon Schubert 1145796c8dcSSimon Schubert /* Don't propagate breakpoint commands errors. Either we're 1155796c8dcSSimon Schubert stopping or some command resumes the inferior. The user will 1165796c8dcSSimon Schubert be informed. */ 1175796c8dcSSimon Schubert TRY_CATCH (e, RETURN_MASK_ALL) 1185796c8dcSSimon Schubert { 1195796c8dcSSimon Schubert bpstat_do_actions (); 1205796c8dcSSimon Schubert } 121*a45ae5f8SJohn Marino exception_print (gdb_stderr, e); 122*a45ae5f8SJohn Marino } 1235796c8dcSSimon Schubert break; 1245796c8dcSSimon Schubert 1255796c8dcSSimon Schubert case INF_EXEC_CONTINUE: 1265796c8dcSSimon Schubert /* Is there anything left to do for the command issued to 1275796c8dcSSimon Schubert complete? */ 1285796c8dcSSimon Schubert 1295796c8dcSSimon Schubert if (non_stop) 130*a45ae5f8SJohn Marino do_all_intermediate_continuations_thread (inferior_thread (), 0); 1315796c8dcSSimon Schubert else 132*a45ae5f8SJohn Marino do_all_intermediate_continuations (0); 1335796c8dcSSimon Schubert break; 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert case INF_TIMER: 1365796c8dcSSimon Schubert default: 1375796c8dcSSimon Schubert printf_unfiltered (_("Event type not recognized.\n")); 1385796c8dcSSimon Schubert break; 1395796c8dcSSimon Schubert } 140*a45ae5f8SJohn Marino 141*a45ae5f8SJohn Marino discard_cleanups (cleanup_if_error); 1425796c8dcSSimon Schubert } 1435796c8dcSSimon Schubert 1445796c8dcSSimon Schubert static int 1455796c8dcSSimon Schubert fetch_inferior_event_wrapper (gdb_client_data client_data) 1465796c8dcSSimon Schubert { 1475796c8dcSSimon Schubert fetch_inferior_event (client_data); 1485796c8dcSSimon Schubert return 1; 1495796c8dcSSimon Schubert } 150