15796c8dcSSimon Schubert /* Handling of inferior events for the event loop for GDB, the GNU debugger. 2*cf7f2e2dSJohn Marino Copyright (C) 1999, 2007, 2008, 2009, 2010 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" 305796c8dcSSimon Schubert 315796c8dcSSimon Schubert static int fetch_inferior_event_wrapper (gdb_client_data client_data); 325796c8dcSSimon Schubert 335796c8dcSSimon Schubert void 345796c8dcSSimon Schubert inferior_event_handler_wrapper (gdb_client_data client_data) 355796c8dcSSimon Schubert { 365796c8dcSSimon Schubert inferior_event_handler (INF_QUIT_REQ, client_data); 375796c8dcSSimon Schubert } 385796c8dcSSimon Schubert 395796c8dcSSimon Schubert /* General function to handle events in the inferior. So far it just 405796c8dcSSimon Schubert takes care of detecting errors reported by select() or poll(), 415796c8dcSSimon Schubert otherwise it assumes that all is OK, and goes on reading data from 425796c8dcSSimon Schubert the fd. This however may not always be what we want to do. */ 435796c8dcSSimon Schubert void 445796c8dcSSimon Schubert inferior_event_handler (enum inferior_event_type event_type, 455796c8dcSSimon Schubert gdb_client_data client_data) 465796c8dcSSimon Schubert { 475796c8dcSSimon Schubert struct gdb_exception e; 485796c8dcSSimon Schubert int was_sync = 0; 49*cf7f2e2dSJohn Marino 505796c8dcSSimon Schubert switch (event_type) 515796c8dcSSimon Schubert { 525796c8dcSSimon Schubert case INF_ERROR: 535796c8dcSSimon Schubert printf_unfiltered (_("error detected from target.\n")); 545796c8dcSSimon Schubert pop_all_targets_above (file_stratum, 0); 555796c8dcSSimon Schubert discard_all_intermediate_continuations (); 565796c8dcSSimon Schubert discard_all_continuations (); 575796c8dcSSimon Schubert async_enable_stdin (); 585796c8dcSSimon Schubert break; 595796c8dcSSimon Schubert 605796c8dcSSimon Schubert case INF_REG_EVENT: 615796c8dcSSimon Schubert /* Use catch errors for now, until the inner layers of 625796c8dcSSimon Schubert fetch_inferior_event (i.e. readchar) can return meaningful 635796c8dcSSimon Schubert error status. If an error occurs while getting an event from 645796c8dcSSimon Schubert the target, just get rid of the target. */ 655796c8dcSSimon Schubert if (!catch_errors (fetch_inferior_event_wrapper, 665796c8dcSSimon Schubert client_data, "", RETURN_MASK_ALL)) 675796c8dcSSimon Schubert { 685796c8dcSSimon Schubert pop_all_targets_above (file_stratum, 0); 695796c8dcSSimon Schubert discard_all_intermediate_continuations (); 705796c8dcSSimon Schubert discard_all_continuations (); 715796c8dcSSimon Schubert async_enable_stdin (); 725796c8dcSSimon Schubert display_gdb_prompt (0); 735796c8dcSSimon Schubert } 745796c8dcSSimon Schubert break; 755796c8dcSSimon Schubert 765796c8dcSSimon Schubert case INF_EXEC_COMPLETE: 775796c8dcSSimon Schubert 785796c8dcSSimon Schubert if (!non_stop) 795796c8dcSSimon Schubert { 805796c8dcSSimon Schubert /* Unregister the inferior from the event loop. This is done 815796c8dcSSimon Schubert so that when the inferior is not running we don't get 825796c8dcSSimon Schubert distracted by spurious inferior output. */ 835796c8dcSSimon Schubert if (target_has_execution) 845796c8dcSSimon Schubert target_async (NULL, 0); 855796c8dcSSimon Schubert } 865796c8dcSSimon Schubert 875796c8dcSSimon Schubert /* The call to async_enable_stdin below resets 'sync_execution'. 885796c8dcSSimon Schubert However, if sync_execution is 1 now, we also need to show the 895796c8dcSSimon Schubert prompt below, so save the current value. */ 905796c8dcSSimon Schubert was_sync = sync_execution; 915796c8dcSSimon Schubert async_enable_stdin (); 925796c8dcSSimon Schubert 935796c8dcSSimon Schubert /* Do all continuations associated with the whole inferior (not 945796c8dcSSimon Schubert a particular thread). */ 955796c8dcSSimon Schubert if (!ptid_equal (inferior_ptid, null_ptid)) 965796c8dcSSimon Schubert do_all_inferior_continuations (); 975796c8dcSSimon Schubert 985796c8dcSSimon Schubert /* If we were doing a multi-step (eg: step n, next n), but it 995796c8dcSSimon Schubert got interrupted by a breakpoint, still do the pending 1005796c8dcSSimon Schubert continuations. The continuation itself is responsible for 1015796c8dcSSimon Schubert distinguishing the cases. The continuations are allowed to 1025796c8dcSSimon Schubert touch the inferior memory, e.g. to remove breakpoints, so run 1035796c8dcSSimon Schubert them before running breakpoint commands, which may resume the 1045796c8dcSSimon Schubert target. */ 1055796c8dcSSimon Schubert if (non_stop 1065796c8dcSSimon Schubert && target_has_execution 1075796c8dcSSimon Schubert && !ptid_equal (inferior_ptid, null_ptid)) 1085796c8dcSSimon Schubert do_all_intermediate_continuations_thread (inferior_thread ()); 1095796c8dcSSimon Schubert else 1105796c8dcSSimon Schubert do_all_intermediate_continuations (); 1115796c8dcSSimon Schubert 1125796c8dcSSimon Schubert /* Always finish the previous command before running any 1135796c8dcSSimon Schubert breakpoint commands. Any stop cancels the previous command. 1145796c8dcSSimon Schubert E.g. a "finish" or "step-n" command interrupted by an 1155796c8dcSSimon Schubert unrelated breakpoint is canceled. */ 1165796c8dcSSimon Schubert if (non_stop 1175796c8dcSSimon Schubert && target_has_execution 1185796c8dcSSimon Schubert && !ptid_equal (inferior_ptid, null_ptid)) 1195796c8dcSSimon Schubert do_all_continuations_thread (inferior_thread ()); 1205796c8dcSSimon Schubert else 1215796c8dcSSimon Schubert do_all_continuations (); 1225796c8dcSSimon Schubert 1235796c8dcSSimon Schubert if (current_language != expected_language 1245796c8dcSSimon Schubert && language_mode == language_mode_auto) 1255796c8dcSSimon Schubert language_info (1); /* Print what changed. */ 1265796c8dcSSimon Schubert 1275796c8dcSSimon Schubert /* Don't propagate breakpoint commands errors. Either we're 1285796c8dcSSimon Schubert stopping or some command resumes the inferior. The user will 1295796c8dcSSimon Schubert be informed. */ 1305796c8dcSSimon Schubert TRY_CATCH (e, RETURN_MASK_ALL) 1315796c8dcSSimon Schubert { 1325796c8dcSSimon Schubert bpstat_do_actions (); 1335796c8dcSSimon Schubert } 1345796c8dcSSimon Schubert 1355796c8dcSSimon Schubert if (!was_sync 1365796c8dcSSimon Schubert && exec_done_display_p 1375796c8dcSSimon Schubert && (ptid_equal (inferior_ptid, null_ptid) 1385796c8dcSSimon Schubert || !is_running (inferior_ptid))) 1395796c8dcSSimon Schubert printf_unfiltered (_("completed.\n")); 1405796c8dcSSimon Schubert break; 1415796c8dcSSimon Schubert 1425796c8dcSSimon Schubert case INF_EXEC_CONTINUE: 1435796c8dcSSimon Schubert /* Is there anything left to do for the command issued to 1445796c8dcSSimon Schubert complete? */ 1455796c8dcSSimon Schubert 1465796c8dcSSimon Schubert if (non_stop) 1475796c8dcSSimon Schubert do_all_intermediate_continuations_thread (inferior_thread ()); 1485796c8dcSSimon Schubert else 1495796c8dcSSimon Schubert do_all_intermediate_continuations (); 1505796c8dcSSimon Schubert break; 1515796c8dcSSimon Schubert 1525796c8dcSSimon Schubert case INF_QUIT_REQ: 1535796c8dcSSimon Schubert /* FIXME: ezannoni 1999-10-04. This call should really be a 1545796c8dcSSimon Schubert target vector entry, so that it can be used for any kind of 1555796c8dcSSimon Schubert targets. */ 1565796c8dcSSimon Schubert async_remote_interrupt_twice (NULL); 1575796c8dcSSimon Schubert break; 1585796c8dcSSimon Schubert 1595796c8dcSSimon Schubert case INF_TIMER: 1605796c8dcSSimon Schubert default: 1615796c8dcSSimon Schubert printf_unfiltered (_("Event type not recognized.\n")); 1625796c8dcSSimon Schubert break; 1635796c8dcSSimon Schubert } 1645796c8dcSSimon Schubert } 1655796c8dcSSimon Schubert 1665796c8dcSSimon Schubert static int 1675796c8dcSSimon Schubert fetch_inferior_event_wrapper (gdb_client_data client_data) 1685796c8dcSSimon Schubert { 1695796c8dcSSimon Schubert fetch_inferior_event (client_data); 1705796c8dcSSimon Schubert return 1; 1715796c8dcSSimon Schubert } 172