1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2015 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
= { 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 volatile struct gdb_exception
*exception
;
50 /* Saved/current state. */
52 struct cleanup
*saved_cleanup_chain
;
57 /* Where to go for throw_exception(). */
58 static struct catcher
*current_catcher
;
60 /* Return length of current_catcher list. */
63 catcher_list_size (void)
66 struct catcher
*catcher
;
68 for (size
= 0, catcher
= current_catcher
;
70 catcher
= catcher
->prev
)
77 exceptions_state_mc_init (volatile struct gdb_exception
*exception
,
80 struct catcher
*new_catcher
= XCNEW (struct catcher
);
82 /* Start with no exception, save it's address. */
83 *exception
= exception_none
;
84 new_catcher
->exception
= exception
;
86 new_catcher
->mask
= mask
;
88 /* Prevent error/quit during FUNC from calling cleanups established
90 new_catcher
->saved_cleanup_chain
= save_cleanups ();
92 /* Push this new catcher on the top. */
93 new_catcher
->prev
= current_catcher
;
94 current_catcher
= new_catcher
;
95 new_catcher
->state
= CATCHER_CREATED
;
97 return &new_catcher
->buf
;
103 struct catcher
*old_catcher
= current_catcher
;
105 current_catcher
= old_catcher
->prev
;
107 /* Restore the cleanup chain, the error/quit messages, and the uiout
108 builder, to their original states. */
110 restore_cleanups (old_catcher
->saved_cleanup_chain
);
115 /* Catcher state machine. Returns non-zero if the m/c should be run
116 again, zero if it should abort. */
119 exceptions_state_mc (enum catcher_action action
)
121 switch (current_catcher
->state
)
123 case CATCHER_CREATED
:
127 /* Allow the code to run the catcher. */
128 current_catcher
->state
= CATCHER_RUNNING
;
131 internal_error (__FILE__
, __LINE__
, _("bad state"));
133 case CATCHER_RUNNING
:
137 /* No error/quit has occured. Just clean up. */
141 current_catcher
->state
= CATCHER_RUNNING_1
;
144 current_catcher
->state
= CATCHER_ABORTING
;
145 /* See also throw_exception. */
148 internal_error (__FILE__
, __LINE__
, _("bad switch"));
150 case CATCHER_RUNNING_1
:
154 /* The did a "break" from the inner while loop. */
158 current_catcher
->state
= CATCHER_RUNNING
;
161 current_catcher
->state
= CATCHER_ABORTING
;
162 /* See also throw_exception. */
165 internal_error (__FILE__
, __LINE__
, _("bad switch"));
167 case CATCHER_ABORTING
:
172 struct gdb_exception exception
= *current_catcher
->exception
;
174 if (current_catcher
->mask
& RETURN_MASK (exception
.reason
))
176 /* Exit normally if this catcher can handle this
177 exception. The caller analyses the func return
182 /* The caller didn't request that the event be caught,
183 relay the event to the next containing
186 throw_exception (exception
);
189 internal_error (__FILE__
, __LINE__
, _("bad state"));
192 internal_error (__FILE__
, __LINE__
, _("bad switch"));
197 exceptions_state_mc_action_iter (void)
199 return exceptions_state_mc (CATCH_ITER
);
203 exceptions_state_mc_action_iter_1 (void)
205 return exceptions_state_mc (CATCH_ITER_1
);
208 /* Return EXCEPTION to the nearest containing catch_errors(). */
211 throw_exception (struct gdb_exception exception
)
213 prepare_to_throw_exception ();
215 do_cleanups (all_cleanups ());
217 /* Jump to the containing catch_errors() call, communicating REASON
218 to that call via setjmp's return value. Note that REASON can't
219 be zero, by definition in defs.h. */
220 exceptions_state_mc (CATCH_THROWING
);
221 *current_catcher
->exception
= exception
;
222 SIGLONGJMP (current_catcher
->buf
, exception
.reason
);
225 /* A stack of exception messages.
226 This is needed to handle nested calls to throw_it: we don't want to
227 xfree space for a message before it's used.
228 This can happen if we throw an exception during a cleanup:
229 An outer TRY_CATCH may have an exception message it wants to print,
230 but while doing cleanups further calls to throw_it are made.
232 This is indexed by the size of the current_catcher list.
233 It is a dynamically allocated array so that we don't care how deeply
234 GDB nests its TRY_CATCHs. */
235 static char **exception_messages
;
237 /* The number of currently allocated entries in exception_messages. */
238 static int exception_messages_size
;
240 static void ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (3, 0)
241 throw_it (enum return_reason reason
, enum errors error
, const char *fmt
,
244 struct gdb_exception e
;
246 int depth
= catcher_list_size ();
248 gdb_assert (depth
> 0);
250 /* Note: The new message may use an old message's text. */
251 new_message
= xstrvprintf (fmt
, ap
);
253 if (depth
> exception_messages_size
)
255 int old_size
= exception_messages_size
;
257 exception_messages_size
= depth
+ 10;
258 exception_messages
= (char **) xrealloc (exception_messages
,
259 exception_messages_size
261 memset (exception_messages
+ old_size
, 0,
262 (exception_messages_size
- old_size
) * sizeof (char *));
265 xfree (exception_messages
[depth
- 1]);
266 exception_messages
[depth
- 1] = new_message
;
268 /* Create the exception. */
271 e
.message
= new_message
;
273 /* Throw the exception. */
278 throw_verror (enum errors error
, const char *fmt
, va_list ap
)
280 throw_it (RETURN_ERROR
, error
, fmt
, ap
);
284 throw_vquit (const char *fmt
, va_list ap
)
286 throw_it (RETURN_QUIT
, GDB_NO_ERROR
, fmt
, ap
);
290 throw_error (enum errors error
, const char *fmt
, ...)
294 va_start (args
, fmt
);
295 throw_verror (error
, fmt
, args
);
300 throw_quit (const char *fmt
, ...)
304 va_start (args
, fmt
);
305 throw_vquit (fmt
, args
);