Eliminate immediate_quit
[external/binutils.git] / gdb / exceptions.c
1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2016 Free Software Foundation, Inc.
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 "exceptions.h"
22 #include "breakpoint.h"
23 #include "target.h"
24 #include "inferior.h"
25 #include "annotate.h"
26 #include "ui-out.h"
27 #include "serial.h"
28 #include "gdbthread.h"
29
30 void
31 prepare_to_throw_exception (void)
32 {
33 }
34
35 static void
36 print_flush (void)
37 {
38   struct serial *gdb_stdout_serial;
39   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
40
41   if (deprecated_error_begin_hook)
42     deprecated_error_begin_hook ();
43
44   if (target_supports_terminal_ours ())
45     {
46       make_cleanup_restore_target_terminal ();
47       target_terminal_ours_for_output ();
48     }
49
50   /* We want all output to appear now, before we print the error.  We
51      have 3 levels of buffering we have to flush (it's possible that
52      some of these should be changed to flush the lower-level ones
53      too):  */
54
55   /* 1.  The _filtered buffer.  */
56   if (filtered_printing_initialized ())
57     wrap_here ("");
58
59   /* 2.  The stdio buffer.  */
60   gdb_flush (gdb_stdout);
61   gdb_flush (gdb_stderr);
62
63   /* 3.  The system-level buffer.  */
64   gdb_stdout_serial = serial_fdopen (1);
65   if (gdb_stdout_serial)
66     {
67       serial_drain_output (gdb_stdout_serial);
68       serial_un_fdopen (gdb_stdout_serial);
69     }
70
71   annotate_error_begin ();
72
73   do_cleanups (old_chain);
74 }
75
76 static void
77 print_exception (struct ui_file *file, struct gdb_exception e)
78 {
79   /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
80      as that way the MI's behavior is preserved.  */
81   const char *start;
82   const char *end;
83
84   for (start = e.message; start != NULL; start = end)
85     {
86       end = strchr (start, '\n');
87       if (end == NULL)
88         fputs_filtered (start, file);
89       else
90         {
91           end++;
92           ui_file_write (file, start, end - start);
93         }
94     }                                       
95   fprintf_filtered (file, "\n");
96
97   /* Now append the annotation.  */
98   switch (e.reason)
99     {
100     case RETURN_QUIT:
101       annotate_quit ();
102       break;
103     case RETURN_ERROR:
104       /* Assume that these are all errors.  */
105       annotate_error ();
106       break;
107     default:
108       internal_error (__FILE__, __LINE__, _("Bad switch."));
109     }
110 }
111
112 void
113 exception_print (struct ui_file *file, struct gdb_exception e)
114 {
115   if (e.reason < 0 && e.message != NULL)
116     {
117       print_flush ();
118       print_exception (file, e);
119     }
120 }
121
122 void
123 exception_fprintf (struct ui_file *file, struct gdb_exception e,
124                    const char *prefix, ...)
125 {
126   if (e.reason < 0 && e.message != NULL)
127     {
128       va_list args;
129
130       print_flush ();
131
132       /* Print the prefix.  */
133       va_start (args, prefix);
134       vfprintf_filtered (file, prefix, args);
135       va_end (args);
136
137       print_exception (file, e);
138     }
139 }
140
141 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
142    handler.  If an exception (enum return_reason) is thrown using
143    throw_exception() than all cleanups installed since
144    catch_exceptions() was entered are invoked, the (-ve) exception
145    value is then returned by catch_exceptions.  If FUNC() returns
146    normally (with a positive or zero return value) then that value is
147    returned by catch_exceptions().  It is an internal_error() for
148    FUNC() to return a negative value.
149
150    See exceptions.h for further usage details.  */
151
152 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
153    error() et al. could maintain a set of flags that indicate the
154    current state of each of the longjmp buffers.  This would give the
155    longjmp code the chance to detect a longjmp botch (before it gets
156    to longjmperror()).  Prior to 1999-11-05 this wasn't possible as
157    code also randomly used a SET_TOP_LEVEL macro that directly
158    initialized the longjmp buffers.  */
159
160 int
161 catch_exceptions (struct ui_out *uiout,
162                   catch_exceptions_ftype *func,
163                   void *func_args,
164                   return_mask mask)
165 {
166   return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
167 }
168
169 int
170 catch_exceptions_with_msg (struct ui_out *func_uiout,
171                            catch_exceptions_ftype *func,
172                            void *func_args,
173                            char **gdberrmsg,
174                            return_mask mask)
175 {
176   struct gdb_exception exception = exception_none;
177   volatile int val = 0;
178   struct ui_out *saved_uiout;
179
180   /* Save and override the global ``struct ui_out'' builder.  */
181   saved_uiout = current_uiout;
182   current_uiout = func_uiout;
183
184   TRY
185     {
186       val = (*func) (current_uiout, func_args);
187     }
188   CATCH (ex, RETURN_MASK_ALL)
189     {
190       exception = ex;
191     }
192   END_CATCH
193
194   /* Restore the global builder.  */
195   current_uiout = saved_uiout;
196
197   if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
198     {
199       /* The caller didn't request that the event be caught.
200          Rethrow.  */
201       throw_exception (exception);
202     }
203
204   exception_print (gdb_stderr, exception);
205   gdb_assert (val >= 0);
206   gdb_assert (exception.reason <= 0);
207   if (exception.reason < 0)
208     {
209       /* If caller wants a copy of the low-level error message, make
210          one.  This is used in the case of a silent error whereby the
211          caller may optionally want to issue the message.  */
212       if (gdberrmsg != NULL)
213         {
214           if (exception.message != NULL)
215             *gdberrmsg = xstrdup (exception.message);
216           else
217             *gdberrmsg = NULL;
218         }
219       return exception.reason;
220     }
221   return val;
222 }
223
224 /* This function is superseded by catch_exceptions().  */
225
226 int
227 catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
228               return_mask mask)
229 {
230   struct gdb_exception exception = exception_none;
231   volatile int val = 0;
232   struct ui_out *saved_uiout;
233
234   /* Save the global ``struct ui_out'' builder.  */
235   saved_uiout = current_uiout;
236
237   TRY
238     {
239       val = func (func_args);
240     }
241   CATCH (ex, RETURN_MASK_ALL)
242     {
243       exception = ex;
244     }
245   END_CATCH
246
247   /* Restore the global builder.  */
248   current_uiout = saved_uiout;
249
250   if (exception.reason < 0 && (mask & RETURN_MASK (exception.reason)) == 0)
251     {
252       /* The caller didn't request that the event be caught.
253          Rethrow.  */
254       throw_exception (exception);
255     }
256
257   exception_fprintf (gdb_stderr, exception, "%s", errstring);
258   if (exception.reason != 0)
259     return 0;
260   return val;
261 }