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