Don't call clear_quit_flag in prepare_to_throw_exception
[deliverable/binutils-gdb.git] / gdb / exceptions.c
CommitLineData
60250e8b
AC
1/* Exception (throw catch) mechanism, for GDB, the GNU debugger.
2
618f726f 3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
60250e8b
AC
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
60250e8b
AC
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
60250e8b
AC
19
20#include "defs.h"
21#include "exceptions.h"
60250e8b
AC
22#include "breakpoint.h"
23#include "target.h"
24#include "inferior.h"
25#include "annotate.h"
26#include "ui-out.h"
e06e2353 27#include "serial.h"
347bddb7 28#include "gdbthread.h"
60250e8b 29
c25c4a8b 30void
ff55e1b5 31prepare_to_throw_exception (void)
60250e8b 32{
60250e8b 33 immediate_quit = 0;
2a78bfb5
AC
34}
35
6b1b7650 36static void
c6da7a6d 37print_flush (void)
6b1b7650 38{
e06e2353
AC
39 struct serial *gdb_stdout_serial;
40
c6da7a6d
AC
41 if (deprecated_error_begin_hook)
42 deprecated_error_begin_hook ();
5df43998
GB
43
44 if (target_supports_terminal_ours ())
45 target_terminal_ours ();
e06e2353
AC
46
47 /* We want all output to appear now, before we print the error. We
48 have 3 levels of buffering we have to flush (it's possible that
49 some of these should be changed to flush the lower-level ones
50 too): */
51
52 /* 1. The _filtered buffer. */
5df43998
GB
53 if (filtered_printing_initialized ())
54 wrap_here ("");
e06e2353
AC
55
56 /* 2. The stdio buffer. */
c6da7a6d 57 gdb_flush (gdb_stdout);
e06e2353
AC
58 gdb_flush (gdb_stderr);
59
60 /* 3. The system-level buffer. */
61 gdb_stdout_serial = serial_fdopen (1);
cade9e54
PB
62 if (gdb_stdout_serial)
63 {
64 serial_drain_output (gdb_stdout_serial);
65 serial_un_fdopen (gdb_stdout_serial);
66 }
e06e2353 67
c6da7a6d 68 annotate_error_begin ();
6b1b7650
AC
69}
70
9cbc821d 71static void
71fff37b 72print_exception (struct ui_file *file, struct gdb_exception e)
9cbc821d
AC
73{
74 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
75 as that way the MI's behavior is preserved. */
76 const char *start;
77 const char *end;
d7f9d729 78
9cbc821d
AC
79 for (start = e.message; start != NULL; start = end)
80 {
81 end = strchr (start, '\n');
82 if (end == NULL)
83 fputs_filtered (start, file);
84 else
85 {
86 end++;
87 ui_file_write (file, start, end - start);
88 }
89 }
c6da7a6d 90 fprintf_filtered (file, "\n");
e48f5bee
AC
91
92 /* Now append the annotation. */
93 switch (e.reason)
94 {
95 case RETURN_QUIT:
96 annotate_quit ();
97 break;
98 case RETURN_ERROR:
99 /* Assume that these are all errors. */
100 annotate_error ();
101 break;
102 default:
103 internal_error (__FILE__, __LINE__, _("Bad switch."));
104 }
9cbc821d
AC
105}
106
8a076db9 107void
71fff37b 108exception_print (struct ui_file *file, struct gdb_exception e)
8a076db9
AC
109{
110 if (e.reason < 0 && e.message != NULL)
111 {
c6da7a6d 112 print_flush ();
9cbc821d 113 print_exception (file, e);
9cbc821d
AC
114 }
115}
8a076db9 116
9cbc821d 117void
71fff37b 118exception_fprintf (struct ui_file *file, struct gdb_exception e,
9cbc821d
AC
119 const char *prefix, ...)
120{
121 if (e.reason < 0 && e.message != NULL)
122 {
123 va_list args;
c6da7a6d
AC
124
125 print_flush ();
9cbc821d
AC
126
127 /* Print the prefix. */
128 va_start (args, prefix);
129 vfprintf_filtered (file, prefix, args);
130 va_end (args);
131
132 print_exception (file, e);
8a076db9
AC
133 }
134}
135
787274f0
DE
136/* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
137 handler. If an exception (enum return_reason) is thrown using
138 throw_exception() than all cleanups installed since
139 catch_exceptions() was entered are invoked, the (-ve) exception
140 value is then returned by catch_exceptions. If FUNC() returns
141 normally (with a positive or zero return value) then that value is
142 returned by catch_exceptions(). It is an internal_error() for
143 FUNC() to return a negative value.
144
145 See exceptions.h for further usage details.
60250e8b
AC
146
147 Must not be called with immediate_quit in effect (bad things might
148 happen, say we got a signal in the middle of a memcpy to quit_return).
149 This is an OK restriction; with very few exceptions immediate_quit can
787274f0 150 be replaced by judicious use of QUIT. */
60250e8b
AC
151
152/* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
7a9dd1b2 153 error() et al. could maintain a set of flags that indicate the
60250e8b
AC
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
0963b4bd 158 initialized the longjmp buffers. */
60250e8b 159
60250e8b
AC
160int
161catch_exceptions (struct ui_out *uiout,
162 catch_exceptions_ftype *func,
163 void *func_args,
60250e8b
AC
164 return_mask mask)
165{
1c3c7ee7 166 return catch_exceptions_with_msg (uiout, func, func_args, NULL, mask);
60250e8b
AC
167}
168
169int
f9679975 170catch_exceptions_with_msg (struct ui_out *func_uiout,
60250e8b
AC
171 catch_exceptions_ftype *func,
172 void *func_args,
60250e8b
AC
173 char **gdberrmsg,
174 return_mask mask)
175{
7556d4a4 176 struct gdb_exception exception = exception_none;
2a78bfb5 177 volatile int val = 0;
f9679975 178 struct ui_out *saved_uiout;
d7f9d729 179
f9679975 180 /* Save and override the global ``struct ui_out'' builder. */
79a45e25
PA
181 saved_uiout = current_uiout;
182 current_uiout = func_uiout;
f9679975 183
492d29ea 184 TRY
6941d02a 185 {
79a45e25 186 val = (*func) (current_uiout, func_args);
6941d02a 187 }
492d29ea 188 CATCH (ex, RETURN_MASK_ALL)
7556d4a4
PA
189 {
190 exception = ex;
191 }
492d29ea 192 END_CATCH
f9679975
PA
193
194 /* Restore the global builder. */
79a45e25 195 current_uiout = saved_uiout;
f9679975
PA
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
feefc97b 204 exception_print (gdb_stderr, exception);
60250e8b 205 gdb_assert (val >= 0);
2a78bfb5
AC
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)
6b1b7650
AC
213 {
214 if (exception.message != NULL)
215 *gdberrmsg = xstrdup (exception.message);
216 else
217 *gdberrmsg = NULL;
218 }
2a78bfb5
AC
219 return exception.reason;
220 }
60250e8b
AC
221 return val;
222}
223
787274f0
DE
224/* This function is superseded by catch_exceptions(). */
225
60250e8b
AC
226int
227catch_errors (catch_errors_ftype *func, void *func_args, char *errstring,
228 return_mask mask)
229{
492d29ea 230 struct gdb_exception exception = exception_none;
2a78bfb5 231 volatile int val = 0;
f9679975 232 struct ui_out *saved_uiout;
d7f9d729 233
f9679975 234 /* Save the global ``struct ui_out'' builder. */
79a45e25 235 saved_uiout = current_uiout;
f9679975 236
492d29ea 237 TRY
6941d02a
AC
238 {
239 val = func (func_args);
240 }
492d29ea
PA
241 CATCH (ex, RETURN_MASK_ALL)
242 {
243 exception = ex;
244 }
245 END_CATCH
f9679975
PA
246
247 /* Restore the global builder. */
79a45e25 248 current_uiout = saved_uiout;
f9679975
PA
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
feefc97b 257 exception_fprintf (gdb_stderr, exception, "%s", errstring);
2a78bfb5 258 if (exception.reason != 0)
60250e8b
AC
259 return 0;
260 return val;
261}
This page took 0.791557 seconds and 4 git commands to generate.