1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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/>. */
20 #include "common-defs.h"
21 #include "common-exceptions.h"
23 const struct gdb_exception exception_none
= { (enum return_reason
) 0, GDB_NO_ERROR
, NULL
};
25 /* Possible catcher states. */
27 /* Initial state, a new catcher has just been created. */
29 /* The catch code is running. */
32 /* The catch code threw an exception. */
36 /* Possible catcher actions. */
45 enum catcher_state state
;
46 /* Jump buffer pointing back at the exception handler. */
48 /* Status buffer belonging to the exception handler. */
49 struct gdb_exception exception
;
54 /* Where to go for throw_exception(). */
55 static struct catcher
*current_catcher
;
58 exceptions_state_mc_init (void)
60 struct catcher
*new_catcher
= XCNEW (struct catcher
);
62 /* Start with no exception. */
63 new_catcher
->exception
= exception_none
;
65 /* Push this new catcher on the top. */
66 new_catcher
->prev
= current_catcher
;
67 current_catcher
= new_catcher
;
68 new_catcher
->state
= CATCHER_CREATED
;
70 return &new_catcher
->buf
;
76 struct catcher
*old_catcher
= current_catcher
;
78 current_catcher
= old_catcher
->prev
;
83 /* Catcher state machine. Returns non-zero if the m/c should be run
84 again, zero if it should abort. */
87 exceptions_state_mc (enum catcher_action action
)
89 switch (current_catcher
->state
)
95 /* Allow the code to run the catcher. */
96 current_catcher
->state
= CATCHER_RUNNING
;
99 internal_error (__FILE__
, __LINE__
, _("bad state"));
101 case CATCHER_RUNNING
:
105 /* No error/quit has occured. */
108 current_catcher
->state
= CATCHER_RUNNING_1
;
111 current_catcher
->state
= CATCHER_ABORTING
;
112 /* See also throw_exception. */
115 internal_error (__FILE__
, __LINE__
, _("bad switch"));
117 case CATCHER_RUNNING_1
:
121 /* The did a "break" from the inner while loop. */
124 current_catcher
->state
= CATCHER_RUNNING
;
127 current_catcher
->state
= CATCHER_ABORTING
;
128 /* See also throw_exception. */
131 internal_error (__FILE__
, __LINE__
, _("bad switch"));
133 case CATCHER_ABORTING
:
138 /* Exit normally if this catcher can handle this
139 exception. The caller analyses the func return
144 internal_error (__FILE__
, __LINE__
, _("bad state"));
147 internal_error (__FILE__
, __LINE__
, _("bad switch"));
152 exceptions_state_mc_catch (struct gdb_exception
*exception
,
155 *exception
= current_catcher
->exception
;
158 if (exception
->reason
< 0)
160 if (mask
& RETURN_MASK (exception
->reason
))
162 /* Exit normally and let the caller handle the
167 /* The caller didn't request that the event be caught, relay the
168 event to the next exception_catch/CATCH_SJLJ. */
169 throw_exception_sjlj (*exception
);
172 /* No exception was thrown. */
177 exceptions_state_mc_action_iter (void)
179 return exceptions_state_mc (CATCH_ITER
);
183 exceptions_state_mc_action_iter_1 (void)
185 return exceptions_state_mc (CATCH_ITER_1
);
188 /* How many nested TRY blocks we have. See exception_messages and
191 static int try_scope_depth
;
193 /* Called on entry to a TRY scope. */
196 exception_try_scope_entry (void)
202 /* Called on exit of a TRY scope, either normal exit or exception
206 exception_try_scope_exit (void *saved_state
)
211 /* Called by the default catch block. IOW, we'll get here before
212 jumping out to the next outermost scope an exception if a GDB
213 exception is not caught. */
216 exception_rethrow (void)
221 /* Copy the 'gdb_exception' portion of FROM to TO. */
224 gdb_exception_sliced_copy (struct gdb_exception
*to
, const struct gdb_exception
*from
)
229 /* Return EXCEPTION to the nearest containing CATCH_SJLJ block. */
232 throw_exception_sjlj (struct gdb_exception exception
)
234 /* Jump to the nearest CATCH_SJLJ block, communicating REASON to
235 that call via setjmp's return value. Note that REASON can't be
236 zero, by definition in common-exceptions.h. */
237 exceptions_state_mc (CATCH_THROWING
);
238 current_catcher
->exception
= exception
;
239 longjmp (current_catcher
->buf
, exception
.reason
);
242 /* Implementation of throw_exception that uses C++ try/catch. */
244 static ATTRIBUTE_NORETURN
void
245 throw_exception_cxx (struct gdb_exception exception
)
247 if (exception
.reason
== RETURN_QUIT
)
249 gdb_exception_RETURN_MASK_QUIT ex
;
251 gdb_exception_sliced_copy (&ex
, &exception
);
254 else if (exception
.reason
== RETURN_ERROR
)
256 gdb_exception_RETURN_MASK_ERROR ex
;
258 gdb_exception_sliced_copy (&ex
, &exception
);
262 gdb_assert_not_reached ("invalid return reason");
266 throw_exception (struct gdb_exception exception
)
268 throw_exception_cxx (exception
);
271 /* A stack of exception messages.
272 This is needed to handle nested calls to throw_it: we don't want to
273 xfree space for a message before it's used.
274 This can happen if we throw an exception during a cleanup:
275 An outer TRY_CATCH may have an exception message it wants to print,
276 but while doing cleanups further calls to throw_it are made.
278 This is indexed by the size of the current_catcher list.
279 It is a dynamically allocated array so that we don't care how deeply
280 GDB nests its TRY_CATCHs. */
281 static char **exception_messages
;
283 /* The number of currently allocated entries in exception_messages. */
284 static int exception_messages_size
;
286 static void ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (3, 0)
287 throw_it (enum return_reason reason
, enum errors error
, const char *fmt
,
290 struct gdb_exception e
;
292 int depth
= try_scope_depth
;
294 gdb_assert (depth
> 0);
296 /* Note: The new message may use an old message's text. */
297 new_message
= xstrvprintf (fmt
, ap
);
299 if (depth
> exception_messages_size
)
301 int old_size
= exception_messages_size
;
303 exception_messages_size
= depth
+ 10;
304 exception_messages
= XRESIZEVEC (char *, exception_messages
,
305 exception_messages_size
);
306 memset (exception_messages
+ old_size
, 0,
307 (exception_messages_size
- old_size
) * sizeof (char *));
310 xfree (exception_messages
[depth
- 1]);
311 exception_messages
[depth
- 1] = new_message
;
313 /* Create the exception. */
316 e
.message
= new_message
;
318 /* Throw the exception. */
323 throw_verror (enum errors error
, const char *fmt
, va_list ap
)
325 throw_it (RETURN_ERROR
, error
, fmt
, ap
);
329 throw_vquit (const char *fmt
, va_list ap
)
331 throw_it (RETURN_QUIT
, GDB_NO_ERROR
, fmt
, ap
);
335 throw_error (enum errors error
, const char *fmt
, ...)
339 va_start (args
, fmt
);
340 throw_verror (error
, fmt
, args
);
345 throw_quit (const char *fmt
, ...)
349 va_start (args
, fmt
);
350 throw_vquit (fmt
, args
);