OLD | NEW |
(Empty) | |
| 1 //===--------------------- UnwindLevel1-gcc-ext.c -------------------------===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 // Source Licenses. See LICENSE.TXT for details. |
| 7 // |
| 8 // |
| 9 // Implements gcc extensions to the C++ ABI Exception Handling Level 1. |
| 10 // |
| 11 //===----------------------------------------------------------------------===// |
| 12 |
| 13 #include <stdint.h> |
| 14 #include <stdbool.h> |
| 15 #include <stdlib.h> |
| 16 #include <stdio.h> |
| 17 |
| 18 #include "libunwind.h" |
| 19 #include "unwind.h" |
| 20 #include "libunwind_ext.h" |
| 21 #include "config.h" |
| 22 |
| 23 #if _LIBUNWIND_BUILD_ZERO_COST_APIS |
| 24 |
| 25 /// Called by __cxa_rethrow(). |
| 26 _LIBUNWIND_EXPORT _Unwind_Reason_Code |
| 27 _Unwind_Resume_or_Rethrow(struct _Unwind_Exception *exception_object) { |
| 28 _LIBUNWIND_TRACE_API("_Unwind_Resume_or_Rethrow(ex_obj=%p), " |
| 29 "private_1=%ld\n", |
| 30 exception_object, exception_object->private_1); |
| 31 // If this is non-forced and a stopping place was found, then this is a |
| 32 // re-throw. |
| 33 // Call _Unwind_RaiseException() as if this was a new exception |
| 34 if (exception_object->private_1 == 0) { |
| 35 return _Unwind_RaiseException(exception_object); |
| 36 // Will return if there is no catch clause, so that __cxa_rethrow can call |
| 37 // std::terminate(). |
| 38 } |
| 39 |
| 40 // Call through to _Unwind_Resume() which distiguishes between forced and |
| 41 // regular exceptions. |
| 42 _Unwind_Resume(exception_object); |
| 43 _LIBUNWIND_ABORT("_Unwind_Resume_or_Rethrow() called _Unwind_RaiseException()" |
| 44 " which unexpectedly returned"); |
| 45 } |
| 46 |
| 47 |
| 48 /// Called by personality handler during phase 2 to get base address for data |
| 49 /// relative encodings. |
| 50 _LIBUNWIND_EXPORT uintptr_t |
| 51 _Unwind_GetDataRelBase(struct _Unwind_Context *context) { |
| 52 (void)context; |
| 53 _LIBUNWIND_TRACE_API("_Unwind_GetDataRelBase(context=%p)\n", context); |
| 54 _LIBUNWIND_ABORT("_Unwind_GetDataRelBase() not implemented"); |
| 55 } |
| 56 |
| 57 |
| 58 /// Called by personality handler during phase 2 to get base address for text |
| 59 /// relative encodings. |
| 60 _LIBUNWIND_EXPORT uintptr_t |
| 61 _Unwind_GetTextRelBase(struct _Unwind_Context *context) { |
| 62 (void)context; |
| 63 _LIBUNWIND_TRACE_API("_Unwind_GetTextRelBase(context=%p)\n", context); |
| 64 _LIBUNWIND_ABORT("_Unwind_GetTextRelBase() not implemented"); |
| 65 } |
| 66 |
| 67 |
| 68 /// Scans unwind information to find the function that contains the |
| 69 /// specified code address "pc". |
| 70 _LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) { |
| 71 _LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)\n", pc); |
| 72 // This is slow, but works. |
| 73 // We create an unwind cursor then alter the IP to be pc |
| 74 unw_cursor_t cursor; |
| 75 unw_context_t uc; |
| 76 unw_proc_info_t info; |
| 77 unw_getcontext(&uc); |
| 78 unw_init_local(&cursor, &uc); |
| 79 unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(long) pc); |
| 80 if (unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS) |
| 81 return (void *)(long) info.start_ip; |
| 82 else |
| 83 return NULL; |
| 84 } |
| 85 |
| 86 |
| 87 /// Walk every frame and call trace function at each one. If trace function |
| 88 /// returns anything other than _URC_NO_REASON, then walk is terminated. |
| 89 _LIBUNWIND_EXPORT _Unwind_Reason_Code |
| 90 _Unwind_Backtrace(_Unwind_Trace_Fn callback, void *ref) { |
| 91 unw_cursor_t cursor; |
| 92 unw_context_t uc; |
| 93 unw_getcontext(&uc); |
| 94 unw_init_local(&cursor, &uc); |
| 95 |
| 96 _LIBUNWIND_TRACE_API("_Unwind_Backtrace(callback=%p)\n", callback); |
| 97 |
| 98 // walk each frame |
| 99 while (true) { |
| 100 |
| 101 // ask libuwind to get next frame (skip over first frame which is |
| 102 // _Unwind_Backtrace()) |
| 103 if (unw_step(&cursor) <= 0) { |
| 104 _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because cursor reached " |
| 105 "bottom of stack, returning %d\n", |
| 106 _URC_END_OF_STACK); |
| 107 return _URC_END_OF_STACK; |
| 108 } |
| 109 |
| 110 // debugging |
| 111 if (_LIBUNWIND_TRACING_UNWINDING) { |
| 112 char functionName[512]; |
| 113 unw_proc_info_t frameInfo; |
| 114 unw_word_t offset; |
| 115 unw_get_proc_name(&cursor, functionName, 512, &offset); |
| 116 unw_get_proc_info(&cursor, &frameInfo); |
| 117 _LIBUNWIND_TRACE_UNWINDING( |
| 118 " _backtrace: start_ip=0x%llX, func=%s, lsda=0x%llX, context=%p\n", |
| 119 frameInfo.start_ip, functionName, frameInfo.lsda, &cursor); |
| 120 } |
| 121 |
| 122 // call trace function with this frame |
| 123 _Unwind_Reason_Code result = |
| 124 (*callback)((struct _Unwind_Context *)(&cursor), ref); |
| 125 if (result != _URC_NO_REASON) { |
| 126 _LIBUNWIND_TRACE_UNWINDING(" _backtrace: ended because callback " |
| 127 "returned %d\n", |
| 128 result); |
| 129 return result; |
| 130 } |
| 131 } |
| 132 } |
| 133 |
| 134 |
| 135 /// Find dwarf unwind info for an address 'pc' in some function. |
| 136 _LIBUNWIND_EXPORT const void *_Unwind_Find_FDE(const void *pc, |
| 137 struct dwarf_eh_bases *bases) { |
| 138 // This is slow, but works. |
| 139 // We create an unwind cursor then alter the IP to be pc |
| 140 unw_cursor_t cursor; |
| 141 unw_context_t uc; |
| 142 unw_proc_info_t info; |
| 143 unw_getcontext(&uc); |
| 144 unw_init_local(&cursor, &uc); |
| 145 unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(long) pc); |
| 146 unw_get_proc_info(&cursor, &info); |
| 147 bases->tbase = (uintptr_t)info.extra; |
| 148 bases->dbase = 0; // dbase not used on Mac OS X |
| 149 bases->func = (uintptr_t)info.start_ip; |
| 150 _LIBUNWIND_TRACE_API("_Unwind_Find_FDE(pc=%p) => %p\n", pc, |
| 151 (void *)(long) info.unwind_info); |
| 152 return (void *)(long) info.unwind_info; |
| 153 } |
| 154 |
| 155 /// Returns the CFA (call frame area, or stack pointer at start of function) |
| 156 /// for the current context. |
| 157 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetCFA(struct _Unwind_Context *context) { |
| 158 unw_cursor_t *cursor = (unw_cursor_t *)context; |
| 159 unw_word_t result; |
| 160 unw_get_reg(cursor, UNW_REG_SP, &result); |
| 161 _LIBUNWIND_TRACE_API("_Unwind_GetCFA(context=%p) => 0x%llX\n", context, |
| 162 (uint64_t) result); |
| 163 return (uintptr_t)result; |
| 164 } |
| 165 |
| 166 |
| 167 /// Called by personality handler during phase 2 to get instruction pointer. |
| 168 /// ipBefore is a boolean that says if IP is already adjusted to be the call |
| 169 /// site address. Normally IP is the return address. |
| 170 _LIBUNWIND_EXPORT uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context, |
| 171 int *ipBefore) { |
| 172 _LIBUNWIND_TRACE_API("_Unwind_GetIPInfo(context=%p)\n", context); |
| 173 *ipBefore = 0; |
| 174 return _Unwind_GetIP(context); |
| 175 } |
| 176 |
| 177 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND |
| 178 |
| 179 /// Called by programs with dynamic code generators that want |
| 180 /// to register a dynamically generated FDE. |
| 181 /// This function has existed on Mac OS X since 10.4, but |
| 182 /// was broken until 10.6. |
| 183 _LIBUNWIND_EXPORT void __register_frame(const void *fde) { |
| 184 _LIBUNWIND_TRACE_API("__register_frame(%p)\n", fde); |
| 185 _unw_add_dynamic_fde((unw_word_t)(uintptr_t) fde); |
| 186 } |
| 187 |
| 188 |
| 189 /// Called by programs with dynamic code generators that want |
| 190 /// to unregister a dynamically generated FDE. |
| 191 /// This function has existed on Mac OS X since 10.4, but |
| 192 /// was broken until 10.6. |
| 193 _LIBUNWIND_EXPORT void __deregister_frame(const void *fde) { |
| 194 _LIBUNWIND_TRACE_API("__deregister_frame(%p)\n", fde); |
| 195 _unw_remove_dynamic_fde((unw_word_t)(uintptr_t) fde); |
| 196 } |
| 197 |
| 198 |
| 199 // The following register/deregister functions are gcc extensions. |
| 200 // They have existed on Mac OS X, but have never worked because Mac OS X |
| 201 // before 10.6 used keymgr to track known FDEs, but these functions |
| 202 // never got updated to use keymgr. |
| 203 // For now, we implement these as do-nothing functions to keep any existing |
| 204 // applications working. We also add the not in 10.6 symbol so that nwe |
| 205 // application won't be able to use them. |
| 206 |
| 207 #if _LIBUNWIND_SUPPORT_FRAME_APIS |
| 208 _LIBUNWIND_EXPORT void __register_frame_info_bases(const void *fde, void *ob, |
| 209 void *tb, void *db) { |
| 210 (void)fde; |
| 211 (void)ob; |
| 212 (void)tb; |
| 213 (void)db; |
| 214 _LIBUNWIND_TRACE_API("__register_frame_info_bases(%p,%p, %p, %p)\n", |
| 215 fde, ob, tb, db); |
| 216 // do nothing, this function never worked in Mac OS X |
| 217 } |
| 218 |
| 219 _LIBUNWIND_EXPORT void __register_frame_info(const void *fde, void *ob) { |
| 220 (void)fde; |
| 221 (void)ob; |
| 222 _LIBUNWIND_TRACE_API("__register_frame_info(%p, %p)\n", fde, ob); |
| 223 // do nothing, this function never worked in Mac OS X |
| 224 } |
| 225 |
| 226 _LIBUNWIND_EXPORT void __register_frame_info_table_bases(const void *fde, |
| 227 void *ob, void *tb, |
| 228 void *db) { |
| 229 (void)fde; |
| 230 (void)ob; |
| 231 (void)tb; |
| 232 (void)db; |
| 233 _LIBUNWIND_TRACE_API("__register_frame_info_table_bases" |
| 234 "(%p,%p, %p, %p)\n", fde, ob, tb, db); |
| 235 // do nothing, this function never worked in Mac OS X |
| 236 } |
| 237 |
| 238 _LIBUNWIND_EXPORT void __register_frame_info_table(const void *fde, void *ob) { |
| 239 (void)fde; |
| 240 (void)ob; |
| 241 _LIBUNWIND_TRACE_API("__register_frame_info_table(%p, %p)\n", fde, ob); |
| 242 // do nothing, this function never worked in Mac OS X |
| 243 } |
| 244 |
| 245 _LIBUNWIND_EXPORT void __register_frame_table(const void *fde) { |
| 246 (void)fde; |
| 247 _LIBUNWIND_TRACE_API("__register_frame_table(%p)\n", fde); |
| 248 // do nothing, this function never worked in Mac OS X |
| 249 } |
| 250 |
| 251 _LIBUNWIND_EXPORT void *__deregister_frame_info(const void *fde) { |
| 252 (void)fde; |
| 253 _LIBUNWIND_TRACE_API("__deregister_frame_info(%p)\n", fde); |
| 254 // do nothing, this function never worked in Mac OS X |
| 255 return NULL; |
| 256 } |
| 257 |
| 258 _LIBUNWIND_EXPORT void *__deregister_frame_info_bases(const void *fde) { |
| 259 (void)fde; |
| 260 _LIBUNWIND_TRACE_API("__deregister_frame_info_bases(%p)\n", fde); |
| 261 // do nothing, this function never worked in Mac OS X |
| 262 return NULL; |
| 263 } |
| 264 #endif // _LIBUNWIND_SUPPORT_FRAME_APIS |
| 265 |
| 266 #endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND |
| 267 |
| 268 #endif // _LIBUNWIND_BUILD_ZERO_COST_APIS |
OLD | NEW |