1 /* Exception (throw catch) mechanism, for GDB, the GNU debugger.
3 Copyright (C) 1986-2023 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"
22 #include <forward_list>
24 /* Possible catcher states. */
26 /* Initial state, a new catcher has just been created. */
28 /* The catch code is running. */
31 /* The catch code threw an exception. */
35 /* Possible catcher actions. */
44 enum catcher_state state
= CATCHER_CREATED
;
45 /* Jump buffer pointing back at the exception handler. */
47 /* Status buffer belonging to the exception handler. */
48 struct gdb_exception exception
;
51 /* Where to go for throw_exception(). */
52 static std::forward_list
<struct catcher
> catchers
;
55 exceptions_state_mc_init ()
57 catchers
.emplace_front ();
58 return &catchers
.front ().buf
;
61 /* Catcher state machine. Returns non-zero if the m/c should be run
62 again, zero if it should abort. */
65 exceptions_state_mc (enum catcher_action action
)
67 switch (catchers
.front ().state
)
73 /* Allow the code to run the catcher. */
74 catchers
.front ().state
= CATCHER_RUNNING
;
77 internal_error (_("bad state"));
83 /* No error/quit has occurred. */
86 catchers
.front ().state
= CATCHER_RUNNING_1
;
89 catchers
.front ().state
= CATCHER_ABORTING
;
90 /* See also throw_exception. */
93 internal_error (_("bad switch"));
95 case CATCHER_RUNNING_1
:
99 /* The did a "break" from the inner while loop. */
102 catchers
.front ().state
= CATCHER_RUNNING
;
105 catchers
.front ().state
= CATCHER_ABORTING
;
106 /* See also throw_exception. */
109 internal_error (_("bad switch"));
111 case CATCHER_ABORTING
:
116 /* Exit normally if this catcher can handle this
117 exception. The caller analyses the func return
122 internal_error (_("bad state"));
125 internal_error (_("bad switch"));
130 exceptions_state_mc_catch (struct gdb_exception
*exception
,
133 *exception
= std::move (catchers
.front ().exception
);
134 catchers
.pop_front ();
136 if (exception
->reason
< 0)
138 if (mask
& RETURN_MASK (exception
->reason
))
140 /* Exit normally and let the caller handle the
145 /* The caller didn't request that the event be caught, relay the
146 event to the next exception_catch/CATCH_SJLJ. */
147 throw_exception_sjlj (*exception
);
150 /* No exception was thrown. */
155 exceptions_state_mc_action_iter (void)
157 return exceptions_state_mc (CATCH_ITER
);
161 exceptions_state_mc_action_iter_1 (void)
163 return exceptions_state_mc (CATCH_ITER_1
);
166 /* Return EXCEPTION to the nearest containing CATCH_SJLJ block. */
169 throw_exception_sjlj (const struct gdb_exception
&exception
)
171 /* Jump to the nearest CATCH_SJLJ block, communicating REASON to
172 that call via setjmp's return value. Note that REASON can't be
173 zero, by definition in common-exceptions.h. */
174 exceptions_state_mc (CATCH_THROWING
);
175 enum return_reason reason
= exception
.reason
;
176 catchers
.front ().exception
= exception
;
177 longjmp (catchers
.front ().buf
, reason
);
180 /* Implementation of throw_exception that uses C++ try/catch. */
183 throw_exception (gdb_exception
&&exception
)
185 if (exception
.reason
== RETURN_QUIT
)
186 throw gdb_exception_quit (std::move (exception
));
187 else if (exception
.reason
== RETURN_FORCED_QUIT
)
188 throw gdb_exception_forced_quit (std::move (exception
));
189 else if (exception
.reason
== RETURN_ERROR
)
190 throw gdb_exception_error (std::move (exception
));
192 gdb_assert_not_reached ("invalid return reason");
195 static void ATTRIBUTE_NORETURN
ATTRIBUTE_PRINTF (3, 0)
196 throw_it (enum return_reason reason
, enum errors error
, const char *fmt
,
199 if (reason
== RETURN_QUIT
)
200 throw gdb_exception_quit (fmt
, ap
);
201 else if (reason
== RETURN_FORCED_QUIT
)
202 throw gdb_exception_forced_quit (fmt
, ap
);
203 else if (reason
== RETURN_ERROR
)
204 throw gdb_exception_error (error
, fmt
, ap
);
206 gdb_assert_not_reached ("invalid return reason");
210 throw_verror (enum errors error
, const char *fmt
, va_list ap
)
212 throw_it (RETURN_ERROR
, error
, fmt
, ap
);
216 throw_vquit (const char *fmt
, va_list ap
)
218 throw_it (RETURN_QUIT
, GDB_NO_ERROR
, fmt
, ap
);
222 throw_error (enum errors error
, const char *fmt
, ...)
226 va_start (args
, fmt
);
227 throw_verror (error
, fmt
, args
);
232 throw_quit (const char *fmt
, ...)
236 va_start (args
, fmt
);
237 throw_vquit (fmt
, args
);
242 throw_forced_quit (const char *fmt
, ...)
246 va_start (args
, fmt
);
247 throw_it (RETURN_FORCED_QUIT
, GDB_NO_ERROR
, fmt
, args
);