S390: Add target descriptions for vector register sets
[binutils-gdb.git] / gdb / common / common-exceptions.c
blobb65f259df0b213485ff6273485fb6e1efcca3edc
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. */
26 enum catcher_state {
27 /* Initial state, a new catcher has just been created. */
28 CATCHER_CREATED,
29 /* The catch code is running. */
30 CATCHER_RUNNING,
31 CATCHER_RUNNING_1,
32 /* The catch code threw an exception. */
33 CATCHER_ABORTING
36 /* Possible catcher actions. */
37 enum catcher_action {
38 CATCH_ITER,
39 CATCH_ITER_1,
40 CATCH_THROWING
43 struct catcher
45 enum catcher_state state;
46 /* Jump buffer pointing back at the exception handler. */
47 SIGJMP_BUF buf;
48 /* Status buffer belonging to the exception handler. */
49 volatile struct gdb_exception *exception;
50 /* Saved/current state. */
51 int mask;
52 struct cleanup *saved_cleanup_chain;
53 /* Back link. */
54 struct catcher *prev;
57 /* Where to go for throw_exception(). */
58 static struct catcher *current_catcher;
60 /* Return length of current_catcher list. */
62 static int
63 catcher_list_size (void)
65 int size;
66 struct catcher *catcher;
68 for (size = 0, catcher = current_catcher;
69 catcher != NULL;
70 catcher = catcher->prev)
71 ++size;
73 return size;
76 SIGJMP_BUF *
77 exceptions_state_mc_init (volatile struct gdb_exception *exception,
78 return_mask mask)
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
89 prior to here. */
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;
100 static void
101 catcher_pop (void)
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);
112 xfree (old_catcher);
115 /* Catcher state machine. Returns non-zero if the m/c should be run
116 again, zero if it should abort. */
118 static int
119 exceptions_state_mc (enum catcher_action action)
121 switch (current_catcher->state)
123 case CATCHER_CREATED:
124 switch (action)
126 case CATCH_ITER:
127 /* Allow the code to run the catcher. */
128 current_catcher->state = CATCHER_RUNNING;
129 return 1;
130 default:
131 internal_error (__FILE__, __LINE__, _("bad state"));
133 case CATCHER_RUNNING:
134 switch (action)
136 case CATCH_ITER:
137 /* No error/quit has occured. Just clean up. */
138 catcher_pop ();
139 return 0;
140 case CATCH_ITER_1:
141 current_catcher->state = CATCHER_RUNNING_1;
142 return 1;
143 case CATCH_THROWING:
144 current_catcher->state = CATCHER_ABORTING;
145 /* See also throw_exception. */
146 return 1;
147 default:
148 internal_error (__FILE__, __LINE__, _("bad switch"));
150 case CATCHER_RUNNING_1:
151 switch (action)
153 case CATCH_ITER:
154 /* The did a "break" from the inner while loop. */
155 catcher_pop ();
156 return 0;
157 case CATCH_ITER_1:
158 current_catcher->state = CATCHER_RUNNING;
159 return 0;
160 case CATCH_THROWING:
161 current_catcher->state = CATCHER_ABORTING;
162 /* See also throw_exception. */
163 return 1;
164 default:
165 internal_error (__FILE__, __LINE__, _("bad switch"));
167 case CATCHER_ABORTING:
168 switch (action)
170 case CATCH_ITER:
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
178 values. */
179 catcher_pop ();
180 return 0;
182 /* The caller didn't request that the event be caught,
183 relay the event to the next containing
184 catch_errors(). */
185 catcher_pop ();
186 throw_exception (exception);
188 default:
189 internal_error (__FILE__, __LINE__, _("bad state"));
191 default:
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(). */
210 void
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,
242 va_list ap)
244 struct gdb_exception e;
245 char *new_message;
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
260 * sizeof (char *));
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. */
269 e.reason = reason;
270 e.error = error;
271 e.message = new_message;
273 /* Throw the exception. */
274 throw_exception (e);
277 void
278 throw_verror (enum errors error, const char *fmt, va_list ap)
280 throw_it (RETURN_ERROR, error, fmt, ap);
283 void
284 throw_vquit (const char *fmt, va_list ap)
286 throw_it (RETURN_QUIT, GDB_NO_ERROR, fmt, ap);
289 void
290 throw_error (enum errors error, const char *fmt, ...)
292 va_list args;
294 va_start (args, fmt);
295 throw_verror (error, fmt, args);
296 va_end (args);
299 void
300 throw_quit (const char *fmt, ...)
302 va_list args;
304 va_start (args, fmt);
305 throw_vquit (fmt, args);
306 va_end (args);