1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "exceptions.h"
22 #include "breakpoint.h"
28 #include "gdbthread.h"
34 struct ui
*ui
= current_ui
;
35 struct serial
*gdb_stdout_serial
;
36 struct cleanup
*old_chain
= make_cleanup (null_cleanup
, NULL
);
38 if (deprecated_error_begin_hook
)
39 deprecated_error_begin_hook ();
41 if (target_supports_terminal_ours ())
43 make_cleanup_restore_target_terminal ();
44 target_terminal_ours_for_output ();
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
52 /* 1. The _filtered buffer. */
53 if (filtered_printing_initialized ())
56 /* 2. The stdio buffer. */
57 gdb_flush (gdb_stdout
);
58 gdb_flush (gdb_stderr
);
60 /* 3. The system-level buffer. */
61 gdb_stdout_serial
= serial_fdopen (fileno (ui
->outstream
));
62 if (gdb_stdout_serial
)
64 serial_drain_output (gdb_stdout_serial
);
65 serial_un_fdopen (gdb_stdout_serial
);
68 annotate_error_begin ();
70 do_cleanups (old_chain
);
74 print_exception (struct ui_file
*file
, struct gdb_exception e
)
76 /* KLUGE: cagney/2005-01-13: Write the string out one line at a time
77 as that way the MI's behavior is preserved. */
81 for (start
= e
.message
; start
!= NULL
; start
= end
)
83 end
= strchr (start
, '\n');
85 fputs_filtered (start
, file
);
89 ui_file_write (file
, start
, end
- start
);
92 fprintf_filtered (file
, "\n");
94 /* Now append the annotation. */
101 /* Assume that these are all errors. */
105 internal_error (__FILE__
, __LINE__
, _("Bad switch."));
110 exception_print (struct ui_file
*file
, struct gdb_exception e
)
112 if (e
.reason
< 0 && e
.message
!= NULL
)
115 print_exception (file
, e
);
120 exception_fprintf (struct ui_file
*file
, struct gdb_exception e
,
121 const char *prefix
, ...)
123 if (e
.reason
< 0 && e
.message
!= NULL
)
129 /* Print the prefix. */
130 va_start (args
, prefix
);
131 vfprintf_filtered (file
, prefix
, args
);
134 print_exception (file
, e
);
138 /* Call FUNC(UIOUT, FUNC_ARGS) but wrapped within an exception
139 handler. If an exception (enum return_reason) is thrown using
140 throw_exception() than all cleanups installed since
141 catch_exceptions() was entered are invoked, the (-ve) exception
142 value is then returned by catch_exceptions. If FUNC() returns
143 normally (with a positive or zero return value) then that value is
144 returned by catch_exceptions(). It is an internal_error() for
145 FUNC() to return a negative value.
147 See exceptions.h for further usage details. */
149 /* MAYBE: cagney/1999-11-05: catch_errors() in conjunction with
150 error() et al. could maintain a set of flags that indicate the
151 current state of each of the longjmp buffers. This would give the
152 longjmp code the chance to detect a longjmp botch (before it gets
153 to longjmperror()). Prior to 1999-11-05 this wasn't possible as
154 code also randomly used a SET_TOP_LEVEL macro that directly
155 initialized the longjmp buffers. */
158 catch_exceptions (struct ui_out
*uiout
,
159 catch_exceptions_ftype
*func
,
163 return catch_exceptions_with_msg (uiout
, func
, func_args
, NULL
, mask
);
167 catch_exceptions_with_msg (struct ui_out
*func_uiout
,
168 catch_exceptions_ftype
*func
,
173 struct gdb_exception exception
= exception_none
;
174 volatile int val
= 0;
175 struct ui_out
*saved_uiout
;
177 /* Save and override the global ``struct ui_out'' builder. */
178 saved_uiout
= current_uiout
;
179 current_uiout
= func_uiout
;
183 val
= (*func
) (current_uiout
, func_args
);
185 CATCH (ex
, RETURN_MASK_ALL
)
191 /* Restore the global builder. */
192 current_uiout
= saved_uiout
;
194 if (exception
.reason
< 0 && (mask
& RETURN_MASK (exception
.reason
)) == 0)
196 /* The caller didn't request that the event be caught.
198 throw_exception (exception
);
201 exception_print (gdb_stderr
, exception
);
202 gdb_assert (val
>= 0);
203 gdb_assert (exception
.reason
<= 0);
204 if (exception
.reason
< 0)
206 /* If caller wants a copy of the low-level error message, make
207 one. This is used in the case of a silent error whereby the
208 caller may optionally want to issue the message. */
209 if (gdberrmsg
!= NULL
)
211 if (exception
.message
!= NULL
)
212 *gdberrmsg
= xstrdup (exception
.message
);
216 return exception
.reason
;
221 /* This function is superseded by catch_exceptions(). */
224 catch_errors (catch_errors_ftype
*func
, void *func_args
, char *errstring
,
227 struct gdb_exception exception
= exception_none
;
228 volatile int val
= 0;
229 struct ui_out
*saved_uiout
;
231 /* Save the global ``struct ui_out'' builder. */
232 saved_uiout
= current_uiout
;
236 val
= func (func_args
);
238 CATCH (ex
, RETURN_MASK_ALL
)
244 /* Restore the global builder. */
245 current_uiout
= saved_uiout
;
247 if (exception
.reason
< 0 && (mask
& RETURN_MASK (exception
.reason
)) == 0)
249 /* The caller didn't request that the event be caught.
251 throw_exception (exception
);
254 exception_fprintf (gdb_stderr
, exception
, "%s", errstring
);
255 if (exception
.reason
!= 0)
260 /* See exceptions.h. */
263 exception_print_same (struct gdb_exception e1
, struct gdb_exception e2
)
265 const char *msg1
= e1
.message
;
266 const char *msg2
= e2
.message
;
273 return (e1
.reason
== e2
.reason
274 && e1
.error
== e2
.error
275 && strcmp (msg1
, msg2
) == 0);