1 /* Exception handling and frame unwind runtime interface routines. -*- C -*-
2 Copyright (C) 2001, 2003, 2008, 2009 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* This is derived from the C++ ABI for IA-64. Where we diverge
26 for cross-architecture compatibility are noted with "@@@".
27 This file is included from unwind-dw2.c, unwind-sjlj.c or
30 /* Subroutine of _Unwind_RaiseException also invoked from _Unwind_Resume.
32 Unwind the stack calling the personality routine to find both the
33 exception handler and intermediary cleanup code. We'll only locate
34 the first such frame here. Cleanup code will call back into
35 _Unwind_Resume and we'll continue Phase 2 there. */
37 static _Unwind_Reason_Code
38 _Unwind_RaiseException_Phase2(struct _Unwind_Exception *exc,
39 struct _Unwind_Context *context)
41 _Unwind_Reason_Code code;
45 _Unwind_FrameState fs;
48 code = uw_frame_state_for (context, &fs);
50 /* Identify when we've reached the designated handler context. */
51 match_handler = (uw_identify_context (context) == exc->private_2
52 ? _UA_HANDLER_FRAME : 0);
54 if (code != _URC_NO_REASON)
55 /* Some error encountered. Usually the unwinder doesn't
56 diagnose these and merely crashes. */
57 return _URC_FATAL_PHASE2_ERROR;
59 /* Unwind successful. Run the personality routine, if any. */
62 code = (*fs.personality) (1, _UA_CLEANUP_PHASE | match_handler,
63 exc->exception_class, exc, context);
64 if (code == _URC_INSTALL_CONTEXT)
66 if (code != _URC_CONTINUE_UNWIND)
67 return _URC_FATAL_PHASE2_ERROR;
70 /* Don't let us unwind past the handler context. */
71 gcc_assert (!match_handler);
73 uw_update_context (context, &fs);
79 /* Raise an exception, passing along the given exception object. */
81 _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
82 _Unwind_RaiseException(struct _Unwind_Exception *exc)
84 struct _Unwind_Context this_context, cur_context;
85 _Unwind_Reason_Code code;
87 /* Set up this_context to describe the current stack frame. */
88 uw_init_context (&this_context);
89 cur_context = this_context;
91 /* Phase 1: Search. Unwind the stack, calling the personality routine
92 with the _UA_SEARCH_PHASE flag set. Do not modify the stack yet. */
95 _Unwind_FrameState fs;
97 /* Set up fs to describe the FDE for the caller of cur_context. The
98 first time through the loop, that means __cxa_throw. */
99 code = uw_frame_state_for (&cur_context, &fs);
101 if (code == _URC_END_OF_STACK)
102 /* Hit end of stack with no handler found. */
103 return _URC_END_OF_STACK;
105 if (code != _URC_NO_REASON)
106 /* Some error encountered. Usually the unwinder doesn't
107 diagnose these and merely crashes. */
108 return _URC_FATAL_PHASE1_ERROR;
110 /* Unwind successful. Run the personality routine, if any. */
113 code = (*fs.personality) (1, _UA_SEARCH_PHASE, exc->exception_class,
115 if (code == _URC_HANDLER_FOUND)
117 else if (code != _URC_CONTINUE_UNWIND)
118 return _URC_FATAL_PHASE1_ERROR;
121 /* Update cur_context to describe the same frame as fs. */
122 uw_update_context (&cur_context, &fs);
125 /* Indicate to _Unwind_Resume and associated subroutines that this
126 is not a forced unwind. Further, note where we found a handler. */
128 exc->private_2 = uw_identify_context (&cur_context);
130 cur_context = this_context;
131 code = _Unwind_RaiseException_Phase2 (exc, &cur_context);
132 if (code != _URC_INSTALL_CONTEXT)
135 uw_install_context (&this_context, &cur_context);
139 /* Subroutine of _Unwind_ForcedUnwind also invoked from _Unwind_Resume. */
141 static _Unwind_Reason_Code
142 _Unwind_ForcedUnwind_Phase2 (struct _Unwind_Exception *exc,
143 struct _Unwind_Context *context)
145 _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) (_Unwind_Ptr) exc->private_1;
146 void *stop_argument = (void *) (_Unwind_Ptr) exc->private_2;
147 _Unwind_Reason_Code code, stop_code;
151 _Unwind_FrameState fs;
154 /* Set up fs to describe the FDE for the caller of cur_context. */
155 code = uw_frame_state_for (context, &fs);
156 if (code != _URC_NO_REASON && code != _URC_END_OF_STACK)
157 return _URC_FATAL_PHASE2_ERROR;
159 /* Unwind successful. */
160 action = _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE;
161 if (code == _URC_END_OF_STACK)
162 action |= _UA_END_OF_STACK;
163 stop_code = (*stop) (1, action, exc->exception_class, exc,
164 context, stop_argument);
165 if (stop_code != _URC_NO_REASON)
166 return _URC_FATAL_PHASE2_ERROR;
168 /* Stop didn't want to do anything. Invoke the personality
169 handler, if applicable, to run cleanups. */
170 if (code == _URC_END_OF_STACK)
175 code = (*fs.personality) (1, _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE,
176 exc->exception_class, exc, context);
177 if (code == _URC_INSTALL_CONTEXT)
179 if (code != _URC_CONTINUE_UNWIND)
180 return _URC_FATAL_PHASE2_ERROR;
183 /* Update cur_context to describe the same frame as fs, and discard
184 the previous context if necessary. */
185 uw_advance_context (context, &fs);
192 /* Raise an exception for forced unwinding. */
194 _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
195 _Unwind_ForcedUnwind (struct _Unwind_Exception *exc,
196 _Unwind_Stop_Fn stop, void * stop_argument)
198 struct _Unwind_Context this_context, cur_context;
199 _Unwind_Reason_Code code;
201 uw_init_context (&this_context);
202 cur_context = this_context;
204 exc->private_1 = (_Unwind_Ptr) stop;
205 exc->private_2 = (_Unwind_Ptr) stop_argument;
207 code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
208 if (code != _URC_INSTALL_CONTEXT)
211 uw_install_context (&this_context, &cur_context);
215 /* Resume propagation of an existing exception. This is used after
216 e.g. executing cleanup code, and not to implement rethrowing. */
218 void LIBGCC2_UNWIND_ATTRIBUTE
219 _Unwind_Resume (struct _Unwind_Exception *exc)
221 struct _Unwind_Context this_context, cur_context;
222 _Unwind_Reason_Code code;
224 uw_init_context (&this_context);
225 cur_context = this_context;
227 /* Choose between continuing to process _Unwind_RaiseException
228 or _Unwind_ForcedUnwind. */
229 if (exc->private_1 == 0)
230 code = _Unwind_RaiseException_Phase2 (exc, &cur_context);
232 code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
234 gcc_assert (code == _URC_INSTALL_CONTEXT);
236 uw_install_context (&this_context, &cur_context);
240 /* Resume propagation of an FORCE_UNWIND exception, or to rethrow
241 a normal exception that was handled. */
243 _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
244 _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc)
246 struct _Unwind_Context this_context, cur_context;
247 _Unwind_Reason_Code code;
249 /* Choose between continuing to process _Unwind_RaiseException
250 or _Unwind_ForcedUnwind. */
251 if (exc->private_1 == 0)
252 return _Unwind_RaiseException (exc);
254 uw_init_context (&this_context);
255 cur_context = this_context;
257 code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context);
259 gcc_assert (code == _URC_INSTALL_CONTEXT);
261 uw_install_context (&this_context, &cur_context);
265 /* A convenience function that calls the exception_cleanup field. */
268 _Unwind_DeleteException (struct _Unwind_Exception *exc)
270 if (exc->exception_cleanup)
271 (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc);
275 /* Perform stack backtrace through unwind data. */
277 _Unwind_Reason_Code LIBGCC2_UNWIND_ATTRIBUTE
278 _Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument)
280 struct _Unwind_Context context;
281 _Unwind_Reason_Code code;
283 uw_init_context (&context);
287 _Unwind_FrameState fs;
289 /* Set up fs to describe the FDE for the caller of context. */
290 code = uw_frame_state_for (&context, &fs);
291 if (code != _URC_NO_REASON && code != _URC_END_OF_STACK)
292 return _URC_FATAL_PHASE1_ERROR;
294 /* Call trace function. */
295 if ((*trace) (&context, trace_argument) != _URC_NO_REASON)
296 return _URC_FATAL_PHASE1_ERROR;
298 /* We're done at end of stack. */
299 if (code == _URC_END_OF_STACK)
302 /* Update context to describe the same frame as fs. */
303 uw_update_context (&context, &fs);