1/* -*- mode: C++; c-basic-offset: 4; -*- 2 * 3 * Copyright (c) 2008-2010 Apple Inc. All rights reserved. 4 * 5 * @APPLE_LICENSE_HEADER_START@ 6 * 7 * This file contains Original Code and/or Modifications of Original Code 8 * as defined in and that are subject to the Apple Public Source License 9 * Version 2.0 (the 'License'). You may not use this file except in 10 * compliance with the License. Please obtain a copy of the License at 11 * http://www.opensource.apple.com/apsl/ and read it before using this 12 * file. 13 * 14 * The Original Code and all software distributed under the License are 15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 19 * Please see the License for the specific language governing rights and 20 * limitations under the License. 21 * 22 * @APPLE_LICENSE_HEADER_END@ 23 * 24 * Implements gcc extensions to the C++ ABI Exception Handling Level 1 as documented at: 25 * <http://www.codesourcery.com/cxx-abi/abi-eh.html> 26 * using libunwind 27 * 28 */ 29 30#include <stdint.h> 31#include <stdbool.h> 32#include <stdlib.h> 33#include <stdio.h> 34 35#include "libunwind.h" 36#include "unwind.h" 37#include "libunwind_priv.h" 38#include "InternalMacros.h" 39 40 41#if __ppc__ || __i386__ || __x86_64__ 42 43// 44// Called by __cxa_rethrow() 45// 46EXPORT _Unwind_Reason_Code _Unwind_Resume_or_Rethrow(struct _Unwind_Exception* exception_object) 47{ 48 DEBUG_PRINT_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), private_1=%ld\n", exception_object, exception_object->private_1); 49 // if this is non-forced and a stopping place was found, then this is a re-throw 50 // call _Unwind_RaiseException() as if this was a new exception 51 if ( exception_object->private_1 == 0 ) { 52 return _Unwind_RaiseException(exception_object); 53 // should return if there is no catch clause, so that __cxa_rethrow can call std::terminate() 54 } 55 56 // call through to _Unwind_Resume() which distiguishes between forced and regular exceptions 57 _Unwind_Resume(exception_object); 58 ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException() which unexpectedly returned"); 59} 60 61 62// 63// Called by personality handler during phase 2 to get base address for data relative encodings 64// 65EXPORT uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context* context) 66{ 67 DEBUG_PRINT_API("_Unwind_GetDataRelBase(context=%p)\n", context); 68 ABORT("_Unwind_GetDataRelBase() not implemented"); 69} 70 71// 72// Called by personality handler during phase 2 to get base address for text relative encodings 73// 74EXPORT uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context* context) 75{ 76 DEBUG_PRINT_API("_Unwind_GetTextRelBase(context=%p)\n", context); 77 ABORT("_Unwind_GetTextRelBase() not implemented"); 78} 79 80 81 82// 83// Scans unwind information to find the function that contains the 84// specified code address "pc". 85// 86EXPORT void* _Unwind_FindEnclosingFunction(void* pc) 87{ 88 DEBUG_PRINT_API("_Unwind_FindEnclosingFunction(pc=%p)\n", pc); 89 // This is slow, but works. 90 // We create an unwind cursor then alter the IP to be pc 91 unw_cursor_t cursor; 92 unw_context_t uc; 93 unw_proc_info_t info; 94 unw_getcontext(&uc); 95 unw_init_local(&cursor, &uc); 96 unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(long)pc); 97 if ( unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS ) 98 return (void*)(long)info.start_ip; 99 else 100 return NULL; 101} 102 103 104// 105// Walk every frame and call trace function at each one. If trace function 106// returns anything other than _URC_NO_REASON, then walk is terminated. 107// 108EXPORT _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn callback, void* ref) 109{ 110 unw_cursor_t cursor; 111 unw_context_t uc; 112 unw_getcontext(&uc); 113 unw_init_local(&cursor, &uc); 114 115 DEBUG_PRINT_API("_Unwind_Backtrace(callback=%p)\n", callback); 116 117 // walk each frame 118 while ( true ) { 119 120 // ask libuwind to get next frame (skip over first frame which is _Unwind_Backtrace()) 121 if ( unw_step(&cursor) <= 0 ) { 122 DEBUG_PRINT_UNWINDING(" _backtrace: ended because cursor reached bottom of stack, returning %d\n", _URC_END_OF_STACK); 123 return _URC_END_OF_STACK; 124 } 125 126 // debugging 127 if ( DEBUG_PRINT_UNWINDING_TEST ) { 128 char functionName[512]; 129 unw_proc_info_t frameInfo; 130 unw_word_t offset; 131 unw_get_proc_name(&cursor, functionName, 512, &offset); 132 unw_get_proc_info(&cursor, &frameInfo); 133 DEBUG_PRINT_UNWINDING(" _backtrace: start_ip=0x%llX, func=%s, lsda=0x%llX, context=%p\n", 134 frameInfo.start_ip, functionName, frameInfo.lsda, &cursor); 135 } 136 137 // call trace function with this frame 138 _Unwind_Reason_Code result = (*callback)((struct _Unwind_Context*)(&cursor), ref); 139 if ( result != _URC_NO_REASON ) { 140 DEBUG_PRINT_UNWINDING(" _backtrace: ended because callback returned %d\n", result); 141 return result; 142 } 143 } 144} 145 146 147// 148// Find dwarf unwind info for an address 'pc' in some function. 149// 150EXPORT const void* _Unwind_Find_FDE(const void* pc, struct dwarf_eh_bases* bases) 151{ 152 // This is slow, but works. 153 // We create an unwind cursor then alter the IP to be pc 154 unw_cursor_t cursor; 155 unw_context_t uc; 156 unw_proc_info_t info; 157 unw_getcontext(&uc); 158 unw_init_local(&cursor, &uc); 159 unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(long)pc); 160 unw_get_proc_info(&cursor, &info); 161 bases->tbase = info.extra; 162 bases->dbase = 0; // dbase not used on Mac OS X 163 bases->func = info.start_ip; 164 DEBUG_PRINT_API("_Unwind_Find_FDE(pc=%p) => %p\n", pc, (void*)(long)info.unwind_info); 165 return (void*)(long)info.unwind_info; 166} 167 168 169 170EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context* context) 171{ 172 unw_cursor_t* cursor = (unw_cursor_t*)context; 173 unw_word_t result; 174 unw_get_reg(cursor, UNW_REG_SP, &result); 175 DEBUG_PRINT_API("_Unwind_GetCFA(context=%p) => 0x%llX\n", context, (uint64_t)result); 176 return result; 177} 178 179 180// 181// Called by personality handler during phase 2 to get instruction pointer. 182// ipBefore is a boolean that says if IP is already adjusted to be the call 183// site address. Normally IP is the return address. 184// 185EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context* context, int* ipBefore) 186{ 187 DEBUG_PRINT_API("_Unwind_GetIPInfo(context=%p)\n", context); 188 *ipBefore = 0; 189 return _Unwind_GetIP(context); 190} 191 192 193// 194// Called by programs with dynamic code generators that want 195// to register a dynamically generated FDE. 196// This function has existed on Mac OS X since 10.4, but 197// never worked before. 198// 199EXPORT void __register_frame(const void* fde) 200{ 201 DEBUG_PRINT_API("__register_frame(%p)\n", fde); 202 _unw_add_dynamic_fde((unw_word_t)(uintptr_t)fde); 203} 204 205 206// 207// Called by programs with dynamic code generators that want 208// to unregister a dynamically generated FDE. 209// This function has existed on Mac OS X since 10.4, but 210// never worked before. 211// 212EXPORT void __deregister_frame(const void* fde) 213{ 214 DEBUG_PRINT_API("__deregister_frame(%p)\n", fde); 215 _unw_remove_dynamic_fde((unw_word_t)(uintptr_t)fde); 216} 217 218 219 220// 221// The following register/deregister functions are gcc extensions. 222// They have existed on Mac OS X, but have never worked because Mac OS X 223// before 10.6 used keymgr to track known FDEs, but these functions 224// never got updated to use keymgr. 225// For now, we implement these as do-nothing functions to keep any existing 226// applications working. We also add the not in 10.6 symbol so that nwe 227// application won't be able to use them. 228// 229 230EXPORT void __register_frame_info_bases(const void* fde, void* ob, void* tb, void* db) 231{ 232 DEBUG_PRINT_API("__register_frame_info_bases(%p,%p, %p, %p)\n", fde, ob, tb, db); 233 // do nothing, this function never worked in Mac OS X 234} 235 236EXPORT void __register_frame_info(const void* fde, void* ob) 237{ 238 DEBUG_PRINT_API("__register_frame_info(%p, %p)\n", fde, ob); 239 // do nothing, this function never worked in Mac OS X 240} 241 242 243EXPORT void __register_frame_info_table_bases(const void* fde, void* ob, void* tb, void* db) 244{ 245 DEBUG_PRINT_API("__register_frame_info_table_bases(%p,%p, %p, %p)\n", fde, ob, tb, db); 246 // do nothing, this function never worked in Mac OS X 247} 248 249EXPORT void __register_frame_info_table(const void* fde, void* ob) 250{ 251 DEBUG_PRINT_API("__register_frame_info_table(%p, %p)\n", fde, ob); 252 // do nothing, this function never worked in Mac OS X 253} 254 255EXPORT void __register_frame_table(const void* fde) 256{ 257 DEBUG_PRINT_API("__register_frame_table(%p)\n", fde); 258 // do nothing, this function never worked in Mac OS X 259} 260 261EXPORT void* __deregister_frame_info(const void* fde) 262{ 263 DEBUG_PRINT_API("__deregister_frame_info(%p)\n", fde); 264 // do nothing, this function never worked in Mac OS X 265 return NULL; 266} 267 268EXPORT void* __deregister_frame_info_bases(const void* fde) 269{ 270 DEBUG_PRINT_API("__deregister_frame_info_bases(%p)\n", fde); 271 // do nothing, this function never worked in Mac OS X 272 return NULL; 273} 274 275 276 277 278// 279// symbols in libSystem.dylib in 10.6 and later, but are in libgcc_s.dylib in earlier versions 280// 281NOT_HERE_BEFORE_10_6(_Unwind_Backtrace) 282NOT_HERE_BEFORE_10_6(_Unwind_FindEnclosingFunction) 283NOT_HERE_BEFORE_10_6(_Unwind_GetCFA) 284NOT_HERE_BEFORE_10_6(_Unwind_GetDataRelBase) 285NOT_HERE_BEFORE_10_6(_Unwind_GetTextRelBase) 286NOT_HERE_BEFORE_10_6(_Unwind_Resume_or_Rethrow) 287NOT_HERE_BEFORE_10_6(_Unwind_GetIPInfo) 288 289NOT_HERE_BEFORE_10_6(__register_frame) 290NOT_HERE_BEFORE_10_6(__deregister_frame) 291 292 293// 294// symbols in libSystem.dylib for compatibility, but we don't want any new code using them 295// 296NEVER_HERE(__register_frame_info_bases) 297NEVER_HERE(__register_frame_info) 298NEVER_HERE(__register_frame_info_table_bases) 299NEVER_HERE(__register_frame_info_table) 300NEVER_HERE(__register_frame_table) 301NEVER_HERE(__deregister_frame_info) 302NEVER_HERE(__deregister_frame_info_bases) 303 304 305#endif // __ppc__ || __i386__ || __x86_64__ 306 307