1/* Handling of inferior events for the event loop for GDB, the GNU debugger.
2   Copyright (C) 1999, 2007 Free Software Foundation, Inc.
3   Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
4
5   This file is part of GDB.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "inferior.h"		/* For fetch_inferior_event. */
22#include "target.h"             /* For enum inferior_event_type. */
23#include "event-loop.h"
24#include "event-top.h"
25#include "inf-loop.h"
26#include "remote.h"
27#include "exceptions.h"
28
29static int fetch_inferior_event_wrapper (gdb_client_data client_data);
30static void complete_execution (void);
31
32void
33inferior_event_handler_wrapper (gdb_client_data client_data)
34{
35  inferior_event_handler (INF_QUIT_REQ, client_data);
36}
37
38/* General function to handle events in the inferior. So far it just
39   takes care of detecting errors reported by select() or poll(),
40   otherwise it assumes that all is OK, and goes on reading data from
41   the fd. This however may not always be what we want to do. */
42void
43inferior_event_handler (enum inferior_event_type event_type,
44			gdb_client_data client_data)
45{
46  switch (event_type)
47    {
48    case INF_ERROR:
49      printf_unfiltered (_("error detected from target.\n"));
50      target_async (NULL, 0);
51      pop_target ();
52      discard_all_continuations ();
53      do_exec_error_cleanups (ALL_CLEANUPS);
54      break;
55
56    case INF_REG_EVENT:
57      /* Use catch errors for now, until the inner layers of
58	 fetch_inferior_event (i.e. readchar) can return meaningful
59	 error status.  If an error occurs while getting an event from
60	 the target, just get rid of the target. */
61      if (!catch_errors (fetch_inferior_event_wrapper,
62			 client_data, "", RETURN_MASK_ALL))
63	{
64	  target_async (NULL, 0);
65	  pop_target ();
66	  discard_all_continuations ();
67	  do_exec_error_cleanups (ALL_CLEANUPS);
68	  display_gdb_prompt (0);
69	}
70      break;
71
72    case INF_EXEC_COMPLETE:
73      /* Is there anything left to do for the command issued to
74         complete? */
75      do_all_continuations ();
76      /* Reset things after target has stopped for the async commands. */
77      complete_execution ();
78      break;
79
80    case INF_EXEC_CONTINUE:
81      /* Is there anything left to do for the command issued to
82         complete? */
83      do_all_intermediate_continuations ();
84      break;
85
86    case INF_QUIT_REQ:
87      /* FIXME: ezannoni 1999-10-04. This call should really be a
88	 target vector entry, so that it can be used for any kind of
89	 targets. */
90      async_remote_interrupt_twice (NULL);
91      break;
92
93    case INF_TIMER:
94    default:
95      printf_unfiltered (_("Event type not recognized.\n"));
96      break;
97    }
98}
99
100static int
101fetch_inferior_event_wrapper (gdb_client_data client_data)
102{
103  fetch_inferior_event (client_data);
104  return 1;
105}
106
107/* Reset proper settings after an asynchronous command has finished.
108   If the execution command was in synchronous mode, register stdin
109   with the event loop, and reset the prompt. */
110
111static void
112complete_execution (void)
113{
114  target_executing = 0;
115
116  /* Unregister the inferior from the event loop. This is done so that
117     when the inferior is not running we don't get distracted by
118     spurious inferior output. */
119  target_async (NULL, 0);
120
121  if (sync_execution)
122    {
123      do_exec_error_cleanups (ALL_CLEANUPS);
124      display_gdb_prompt (0);
125    }
126  else
127    {
128      if (exec_done_display_p)
129	printf_unfiltered (_("completed.\n"));
130    }
131}
132