unwind.inc revision 117395
1229784Suqs/* Exception handling and frame unwind runtime interface routines. -*- C -*- 2229784Suqs Copyright (C) 2001 Free Software Foundation, Inc. 3178828Sdfr 4178828Sdfr This file is part of GCC. 5178828Sdfr 6178828Sdfr GCC is free software; you can redistribute it and/or modify it 7178828Sdfr under the terms of the GNU General Public License as published by 8178828Sdfr the Free Software Foundation; either version 2, or (at your option) 9178828Sdfr any later version. 10178828Sdfr 11178828Sdfr GCC is distributed in the hope that it will be useful, but WITHOUT 12178828Sdfr ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 13178828Sdfr or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public 14178828Sdfr License for more details. 15178828Sdfr 16178828Sdfr You should have received a copy of the GNU General Public License 17178828Sdfr along with GCC; see the file COPYING. If not, write to the Free 18178828Sdfr Software Foundation, 59 Temple Place - Suite 330, Boston, MA 19178828Sdfr 02111-1307, USA. */ 20178828Sdfr 21178828Sdfr/* This is derived from the C++ ABI for IA-64. Where we diverge 22178828Sdfr for cross-architecture compatibility are noted with "@@@". 23178828Sdfr This file is included from unwind-dw2.c or unwind-ia64.c. */ 24178828Sdfr 25178828Sdfr/* Subroutine of _Unwind_RaiseException also invoked from _Unwind_Resume. 26178828Sdfr 27178828Sdfr Unwind the stack calling the personality routine to find both the 28178828Sdfr exception handler and intermediary cleanup code. We'll only locate 29178828Sdfr the first such frame here. Cleanup code will call back into 30178828Sdfr _Unwind_Resume and we'll continue Phase 2 there. */ 31178828Sdfr 32178828Sdfrstatic _Unwind_Reason_Code 33178828Sdfr_Unwind_RaiseException_Phase2(struct _Unwind_Exception *exc, 34178828Sdfr struct _Unwind_Context *context) 35178828Sdfr{ 36178828Sdfr _Unwind_Reason_Code code; 37178828Sdfr 38178828Sdfr while (1) 39178828Sdfr { 40178828Sdfr _Unwind_FrameState fs; 41178828Sdfr int match_handler; 42178828Sdfr 43178828Sdfr code = uw_frame_state_for (context, &fs); 44178828Sdfr 45178828Sdfr /* Identify when we've reached the designated handler context. */ 46178828Sdfr match_handler = (uw_identify_context (context) == exc->private_2 47178828Sdfr ? _UA_HANDLER_FRAME : 0); 48178828Sdfr 49178828Sdfr if (code != _URC_NO_REASON) 50178828Sdfr /* Some error encountered. Usually the unwinder doesn't 51178828Sdfr diagnose these and merely crashes. */ 52178828Sdfr return _URC_FATAL_PHASE2_ERROR; 53178828Sdfr 54178828Sdfr /* Unwind successful. Run the personality routine, if any. */ 55178828Sdfr if (fs.personality) 56178828Sdfr { 57178828Sdfr code = (*fs.personality) (1, _UA_CLEANUP_PHASE | match_handler, 58178828Sdfr exc->exception_class, exc, context); 59178828Sdfr if (code == _URC_INSTALL_CONTEXT) 60178828Sdfr break; 61178828Sdfr if (code != _URC_CONTINUE_UNWIND) 62178828Sdfr return _URC_FATAL_PHASE2_ERROR; 63178828Sdfr } 64178828Sdfr 65178828Sdfr /* Don't let us unwind past the handler context. */ 66178828Sdfr if (match_handler) 67178828Sdfr abort (); 68178828Sdfr 69178828Sdfr uw_update_context (context, &fs); 70178828Sdfr } 71178828Sdfr 72178828Sdfr return code; 73} 74 75/* Raise an exception, passing along the given exception object. */ 76 77_Unwind_Reason_Code 78_Unwind_RaiseException(struct _Unwind_Exception *exc) 79{ 80 struct _Unwind_Context this_context, cur_context; 81 _Unwind_Reason_Code code; 82 83 /* Set up this_context to describe the current stack frame. */ 84 uw_init_context (&this_context); 85 cur_context = this_context; 86 87 /* Phase 1: Search. Unwind the stack, calling the personality routine 88 with the _UA_SEARCH_PHASE flag set. Do not modify the stack yet. */ 89 while (1) 90 { 91 _Unwind_FrameState fs; 92 93 /* Set up fs to describe the FDE for the caller of cur_context. The 94 first time through the loop, that means __cxa_throw. */ 95 code = uw_frame_state_for (&cur_context, &fs); 96 97 if (code == _URC_END_OF_STACK) 98 /* Hit end of stack with no handler found. */ 99 return _URC_END_OF_STACK; 100 101 if (code != _URC_NO_REASON) 102 /* Some error encountered. Ususally the unwinder doesn't 103 diagnose these and merely crashes. */ 104 return _URC_FATAL_PHASE1_ERROR; 105 106 /* Unwind successful. Run the personality routine, if any. */ 107 if (fs.personality) 108 { 109 code = (*fs.personality) (1, _UA_SEARCH_PHASE, exc->exception_class, 110 exc, &cur_context); 111 if (code == _URC_HANDLER_FOUND) 112 break; 113 else if (code != _URC_CONTINUE_UNWIND) 114 return _URC_FATAL_PHASE1_ERROR; 115 } 116 117 /* Update cur_context to describe the same frame as fs. */ 118 uw_update_context (&cur_context, &fs); 119 } 120 121 /* Indicate to _Unwind_Resume and associated subroutines that this 122 is not a forced unwind. Further, note where we found a handler. */ 123 exc->private_1 = 0; 124 exc->private_2 = uw_identify_context (&cur_context); 125 126 cur_context = this_context; 127 code = _Unwind_RaiseException_Phase2 (exc, &cur_context); 128 if (code != _URC_INSTALL_CONTEXT) 129 return code; 130 131 uw_install_context (&this_context, &cur_context); 132} 133 134 135/* Subroutine of _Unwind_ForcedUnwind also invoked from _Unwind_Resume. */ 136 137static _Unwind_Reason_Code 138_Unwind_ForcedUnwind_Phase2(struct _Unwind_Exception *exc, 139 struct _Unwind_Context *context) 140{ 141 _Unwind_Stop_Fn stop = (_Unwind_Stop_Fn) (_Unwind_Ptr) exc->private_1; 142 void *stop_argument = (void *) (_Unwind_Ptr) exc->private_2; 143 _Unwind_Reason_Code code, stop_code; 144 145 while (1) 146 { 147 _Unwind_FrameState fs; 148 int action; 149 150 /* Set up fs to describe the FDE for the caller of cur_context. */ 151 code = uw_frame_state_for (context, &fs); 152 if (code != _URC_NO_REASON && code != _URC_END_OF_STACK) 153 return _URC_FATAL_PHASE2_ERROR; 154 155 /* Unwind successful. */ 156 action = _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE; 157 if (code == _URC_END_OF_STACK) 158 action |= _UA_END_OF_STACK; 159 stop_code = (*stop) (1, action, exc->exception_class, exc, 160 context, stop_argument); 161 if (stop_code != _URC_NO_REASON) 162 return _URC_FATAL_PHASE2_ERROR; 163 164 /* Stop didn't want to do anything. Invoke the personality 165 handler, if applicable, to run cleanups. */ 166 if (code == _URC_END_OF_STACK) 167 break; 168 169 if (fs.personality) 170 { 171 code = (*fs.personality) (1, _UA_FORCE_UNWIND | _UA_CLEANUP_PHASE, 172 exc->exception_class, exc, context); 173 if (code == _URC_INSTALL_CONTEXT) 174 break; 175 if (code != _URC_CONTINUE_UNWIND) 176 return _URC_FATAL_PHASE2_ERROR; 177 } 178 179 /* Update cur_context to describe the same frame as fs. */ 180 uw_update_context (context, &fs); 181 } 182 183 return code; 184} 185 186 187/* Raise an exception for forced unwinding. */ 188 189_Unwind_Reason_Code 190_Unwind_ForcedUnwind (struct _Unwind_Exception *exc, 191 _Unwind_Stop_Fn stop, void * stop_argument) 192{ 193 struct _Unwind_Context this_context, cur_context; 194 _Unwind_Reason_Code code; 195 196 uw_init_context (&this_context); 197 cur_context = this_context; 198 199 exc->private_1 = (_Unwind_Ptr) stop; 200 exc->private_2 = (_Unwind_Ptr) stop_argument; 201 202 code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context); 203 if (code != _URC_INSTALL_CONTEXT) 204 return code; 205 206 uw_install_context (&this_context, &cur_context); 207} 208 209 210/* Resume propagation of an existing exception. This is used after 211 e.g. executing cleanup code, and not to implement rethrowing. */ 212 213void 214_Unwind_Resume (struct _Unwind_Exception *exc) 215{ 216 struct _Unwind_Context this_context, cur_context; 217 _Unwind_Reason_Code code; 218 219 uw_init_context (&this_context); 220 cur_context = this_context; 221 222 /* Choose between continuing to process _Unwind_RaiseException 223 or _Unwind_ForcedUnwind. */ 224 if (exc->private_1 == 0) 225 code = _Unwind_RaiseException_Phase2 (exc, &cur_context); 226 else 227 code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context); 228 229 if (code != _URC_INSTALL_CONTEXT) 230 abort (); 231 232 uw_install_context (&this_context, &cur_context); 233} 234 235 236/* Resume propagation of an FORCE_UNWIND exception, or to rethrow 237 a normal exception that was handled. */ 238 239_Unwind_Reason_Code 240_Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc) 241{ 242 struct _Unwind_Context this_context, cur_context; 243 _Unwind_Reason_Code code; 244 245 /* Choose between continuing to process _Unwind_RaiseException 246 or _Unwind_ForcedUnwind. */ 247 if (exc->private_1 == 0) 248 return _Unwind_RaiseException (exc); 249 250 uw_init_context (&this_context); 251 cur_context = this_context; 252 253 code = _Unwind_ForcedUnwind_Phase2 (exc, &cur_context); 254 255 if (code != _URC_INSTALL_CONTEXT) 256 abort (); 257 258 uw_install_context (&this_context, &cur_context); 259} 260 261 262/* A convenience function that calls the exception_cleanup field. */ 263 264void 265_Unwind_DeleteException (struct _Unwind_Exception *exc) 266{ 267 if (exc->exception_cleanup) 268 (*exc->exception_cleanup) (_URC_FOREIGN_EXCEPTION_CAUGHT, exc); 269} 270 271 272/* Perform stack backtrace through unwind data. */ 273 274_Unwind_Reason_Code 275_Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument) 276{ 277 struct _Unwind_Context context; 278 _Unwind_Reason_Code code; 279 280 uw_init_context (&context); 281 282 while (1) 283 { 284 _Unwind_FrameState fs; 285 286 /* Set up fs to describe the FDE for the caller of context. */ 287 code = uw_frame_state_for (&context, &fs); 288 if (code != _URC_NO_REASON && code != _URC_END_OF_STACK) 289 return _URC_FATAL_PHASE1_ERROR; 290 291 /* Call trace function. */ 292 if ((*trace) (&context, trace_argument) != _URC_NO_REASON) 293 return _URC_FATAL_PHASE1_ERROR; 294 295 /* We're done at end of stack. */ 296 if (code == _URC_END_OF_STACK) 297 break; 298 299 /* Update context to describe the same frame as fs. */ 300 uw_update_context (&context, &fs); 301 } 302 303 return code; 304} 305