487aa57a0e40198187d05cd895c01640b64e6a26
[external/binutils.git] / gdb / inf-loop.c
1 /* Handling of inferior events for the event loop for GDB, the GNU debugger.
2    Copyright 1999 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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "inferior.h"           /* For fetch_inferior_event. */
24 #include "target.h"             /* For enum inferior_event_type. */
25 #include "event-loop.h"
26 #include "event-top.h"
27 #include "inf-loop.h"
28 #include "remote.h"
29
30 static int fetch_inferior_event_wrapper (gdb_client_data client_data);
31 static void complete_execution (void);
32
33 void
34 inferior_event_handler_wrapper (gdb_client_data client_data)
35 {
36   inferior_event_handler (INF_QUIT_REQ, client_data);
37 }
38
39 /* General function to handle events in the inferior. So far it just
40    takes care of detecting errors reported by select() or poll(),
41    otherwise it assumes that all is OK, and goes on reading data from
42    the fd. This however may not always be what we want to do. */
43 void
44 inferior_event_handler (enum inferior_event_type event_type, 
45                         gdb_client_data client_data)
46 {
47   switch (event_type)
48     {
49     case INF_ERROR:
50       printf_unfiltered ("error detected from target.\n");
51       target_async (NULL, 0);
52       pop_target ();
53       discard_all_continuations ();
54       do_exec_error_cleanups (ALL_CLEANUPS);
55       break;
56
57     case INF_REG_EVENT:
58       /* Use catch errors for now, until the inner layers of
59          fetch_inferior_event (i.e. readchar) can return meaningful
60          error status.  If an error occurs while getting an event from
61          the target, just get rid of the target. */
62       if (!catch_errors (fetch_inferior_event_wrapper, 
63                          client_data, "", RETURN_MASK_ALL))
64         {
65           target_async (NULL, 0);
66           pop_target ();
67           discard_all_continuations ();
68           do_exec_error_cleanups (ALL_CLEANUPS);
69           display_gdb_prompt (0);
70         }
71       break;
72
73     case INF_EXEC_COMPLETE:
74       /* Is there anything left to do for the command issued to
75          complete? */
76       do_all_continuations ();
77       /* Reset things after target has stopped for the async commands. */
78       complete_execution ();
79       break;
80
81     case INF_QUIT_REQ: 
82       /* FIXME: ezannoni 1999-10-04. This call should really be a
83          target vector entry, so that it can be used for any kind of
84          targets. */
85       async_remote_interrupt_twice (NULL);
86       break;
87
88     case INF_TIMER:
89     default:
90       printf_unfiltered ("Event type not recognized.\n");
91       break;
92     }
93 }
94
95 static int 
96 fetch_inferior_event_wrapper (gdb_client_data client_data)
97 {
98   fetch_inferior_event (client_data);
99   return 1;
100 }
101
102 /* Reset proper settings after an asynchronous command has finished.
103    If the execution command was in synchronous mode, register stdin
104    with the event loop, and reset the prompt. */
105
106 static void
107 complete_execution (void)
108 {
109   target_executing = 0;
110   
111   /* Unregister the inferior from the event loop. This is done so that
112      when the inferior is not running we don't get distracted by
113      spurious inferior output. */
114   target_async (NULL, 0);
115
116   if (sync_execution)
117     {
118       do_exec_error_cleanups (ALL_CLEANUPS);
119       display_gdb_prompt (0);
120     }
121   else
122     {
123       if (exec_done_display_p)
124         printf_unfiltered ("completed.\n");
125     }
126 }