15796c8dcSSimon Schubert /* Handling of inferior events for the event loop for GDB, the GNU debugger.
2*ef5ccd6cSJohn Marino Copyright (C) 1999-2013 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"
30a45ae5f8SJohn Marino #include "continuations.h"
31a45ae5f8SJohn Marino #include "interps.h"
32*ef5ccd6cSJohn Marino #include "top.h"
335796c8dcSSimon Schubert
345796c8dcSSimon Schubert static int fetch_inferior_event_wrapper (gdb_client_data client_data);
355796c8dcSSimon Schubert
365796c8dcSSimon Schubert /* General function to handle events in the inferior. So far it just
375796c8dcSSimon Schubert takes care of detecting errors reported by select() or poll(),
385796c8dcSSimon Schubert otherwise it assumes that all is OK, and goes on reading data from
395796c8dcSSimon Schubert the fd. This however may not always be what we want to do. */
405796c8dcSSimon Schubert void
inferior_event_handler(enum inferior_event_type event_type,gdb_client_data client_data)415796c8dcSSimon Schubert inferior_event_handler (enum inferior_event_type event_type,
425796c8dcSSimon Schubert gdb_client_data client_data)
435796c8dcSSimon Schubert {
44a45ae5f8SJohn Marino struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
45cf7f2e2dSJohn Marino
465796c8dcSSimon Schubert switch (event_type)
475796c8dcSSimon Schubert {
485796c8dcSSimon Schubert case INF_REG_EVENT:
495796c8dcSSimon Schubert /* Use catch errors for now, until the inner layers of
505796c8dcSSimon Schubert fetch_inferior_event (i.e. readchar) can return meaningful
515796c8dcSSimon Schubert error status. If an error occurs while getting an event from
52a45ae5f8SJohn Marino the target, just cancel the current command. */
535796c8dcSSimon Schubert if (!catch_errors (fetch_inferior_event_wrapper,
545796c8dcSSimon Schubert client_data, "", RETURN_MASK_ALL))
555796c8dcSSimon Schubert {
56a45ae5f8SJohn Marino bpstat_clear_actions ();
57a45ae5f8SJohn Marino do_all_intermediate_continuations (1);
58a45ae5f8SJohn Marino do_all_continuations (1);
595796c8dcSSimon Schubert async_enable_stdin ();
605796c8dcSSimon Schubert display_gdb_prompt (0);
615796c8dcSSimon Schubert }
625796c8dcSSimon Schubert break;
635796c8dcSSimon Schubert
645796c8dcSSimon Schubert case INF_EXEC_COMPLETE:
655796c8dcSSimon Schubert if (!non_stop)
665796c8dcSSimon Schubert {
675796c8dcSSimon Schubert /* Unregister the inferior from the event loop. This is done
685796c8dcSSimon Schubert so that when the inferior is not running we don't get
695796c8dcSSimon Schubert distracted by spurious inferior output. */
705796c8dcSSimon Schubert if (target_has_execution)
715796c8dcSSimon Schubert target_async (NULL, 0);
725796c8dcSSimon Schubert }
735796c8dcSSimon Schubert
745796c8dcSSimon Schubert /* Do all continuations associated with the whole inferior (not
755796c8dcSSimon Schubert a particular thread). */
765796c8dcSSimon Schubert if (!ptid_equal (inferior_ptid, null_ptid))
77a45ae5f8SJohn Marino do_all_inferior_continuations (0);
785796c8dcSSimon Schubert
795796c8dcSSimon Schubert /* If we were doing a multi-step (eg: step n, next n), but it
805796c8dcSSimon Schubert got interrupted by a breakpoint, still do the pending
815796c8dcSSimon Schubert continuations. The continuation itself is responsible for
825796c8dcSSimon Schubert distinguishing the cases. The continuations are allowed to
835796c8dcSSimon Schubert touch the inferior memory, e.g. to remove breakpoints, so run
845796c8dcSSimon Schubert them before running breakpoint commands, which may resume the
855796c8dcSSimon Schubert target. */
865796c8dcSSimon Schubert if (non_stop
875796c8dcSSimon Schubert && target_has_execution
885796c8dcSSimon Schubert && !ptid_equal (inferior_ptid, null_ptid))
89a45ae5f8SJohn Marino do_all_intermediate_continuations_thread (inferior_thread (), 0);
905796c8dcSSimon Schubert else
91a45ae5f8SJohn Marino do_all_intermediate_continuations (0);
925796c8dcSSimon Schubert
935796c8dcSSimon Schubert /* Always finish the previous command before running any
945796c8dcSSimon Schubert breakpoint commands. Any stop cancels the previous command.
955796c8dcSSimon Schubert E.g. a "finish" or "step-n" command interrupted by an
965796c8dcSSimon Schubert unrelated breakpoint is canceled. */
975796c8dcSSimon Schubert if (non_stop
985796c8dcSSimon Schubert && target_has_execution
995796c8dcSSimon Schubert && !ptid_equal (inferior_ptid, null_ptid))
100a45ae5f8SJohn Marino do_all_continuations_thread (inferior_thread (), 0);
1015796c8dcSSimon Schubert else
102a45ae5f8SJohn Marino do_all_continuations (0);
1035796c8dcSSimon Schubert
104a45ae5f8SJohn Marino /* When running a command list (from a user command, say), these
105a45ae5f8SJohn Marino are only run when the command list is all done. */
106a45ae5f8SJohn Marino if (interpreter_async)
107a45ae5f8SJohn Marino {
108a45ae5f8SJohn Marino volatile struct gdb_exception e;
109a45ae5f8SJohn Marino
110*ef5ccd6cSJohn Marino check_frame_language_change ();
1115796c8dcSSimon Schubert
1125796c8dcSSimon Schubert /* Don't propagate breakpoint commands errors. Either we're
1135796c8dcSSimon Schubert stopping or some command resumes the inferior. The user will
1145796c8dcSSimon Schubert be informed. */
1155796c8dcSSimon Schubert TRY_CATCH (e, RETURN_MASK_ALL)
1165796c8dcSSimon Schubert {
1175796c8dcSSimon Schubert bpstat_do_actions ();
1185796c8dcSSimon Schubert }
119a45ae5f8SJohn Marino exception_print (gdb_stderr, e);
120a45ae5f8SJohn Marino }
1215796c8dcSSimon Schubert break;
1225796c8dcSSimon Schubert
1235796c8dcSSimon Schubert case INF_EXEC_CONTINUE:
1245796c8dcSSimon Schubert /* Is there anything left to do for the command issued to
1255796c8dcSSimon Schubert complete? */
1265796c8dcSSimon Schubert
1275796c8dcSSimon Schubert if (non_stop)
128a45ae5f8SJohn Marino do_all_intermediate_continuations_thread (inferior_thread (), 0);
1295796c8dcSSimon Schubert else
130a45ae5f8SJohn Marino do_all_intermediate_continuations (0);
1315796c8dcSSimon Schubert break;
1325796c8dcSSimon Schubert
1335796c8dcSSimon Schubert case INF_TIMER:
1345796c8dcSSimon Schubert default:
1355796c8dcSSimon Schubert printf_unfiltered (_("Event type not recognized.\n"));
1365796c8dcSSimon Schubert break;
1375796c8dcSSimon Schubert }
138a45ae5f8SJohn Marino
139a45ae5f8SJohn Marino discard_cleanups (cleanup_if_error);
1405796c8dcSSimon Schubert }
1415796c8dcSSimon Schubert
1425796c8dcSSimon Schubert static int
fetch_inferior_event_wrapper(gdb_client_data client_data)1435796c8dcSSimon Schubert fetch_inferior_event_wrapper (gdb_client_data client_data)
1445796c8dcSSimon Schubert {
1455796c8dcSSimon Schubert fetch_inferior_event (client_data);
1465796c8dcSSimon Schubert return 1;
1475796c8dcSSimon Schubert }
148