unwind.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //
  8. // C++ ABI Level 1 ABI documented at:
  9. // https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef __UNWIND_H__
  13. #define __UNWIND_H__
  14. #include "__libunwind_config.h"
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__) && defined(_WIN32)
  18. #include <windows.h>
  19. #include <ntverp.h>
  20. #endif
  21. #if defined(__APPLE__)
  22. #define LIBUNWIND_UNAVAIL __attribute__ (( deprecated ))
  23. #else
  24. #define LIBUNWIND_UNAVAIL
  25. #endif
  26. typedef enum {
  27. _URC_NO_REASON = 0,
  28. _URC_OK = 0,
  29. _URC_FOREIGN_EXCEPTION_CAUGHT = 1,
  30. _URC_FATAL_PHASE2_ERROR = 2,
  31. _URC_FATAL_PHASE1_ERROR = 3,
  32. _URC_NORMAL_STOP = 4,
  33. _URC_END_OF_STACK = 5,
  34. _URC_HANDLER_FOUND = 6,
  35. _URC_INSTALL_CONTEXT = 7,
  36. _URC_CONTINUE_UNWIND = 8,
  37. #if defined(_LIBUNWIND_ARM_EHABI)
  38. _URC_FAILURE = 9
  39. #endif
  40. } _Unwind_Reason_Code;
  41. typedef enum {
  42. _UA_SEARCH_PHASE = 1,
  43. _UA_CLEANUP_PHASE = 2,
  44. _UA_HANDLER_FRAME = 4,
  45. _UA_FORCE_UNWIND = 8,
  46. _UA_END_OF_STACK = 16 // gcc extension to C++ ABI
  47. } _Unwind_Action;
  48. typedef struct _Unwind_Context _Unwind_Context; // opaque
  49. #if defined(_LIBUNWIND_ARM_EHABI)
  50. #include "unwind_arm_ehabi.h"
  51. #else
  52. #include "unwind_itanium.h"
  53. #endif
  54. typedef _Unwind_Reason_Code (*_Unwind_Stop_Fn)
  55. (int version,
  56. _Unwind_Action actions,
  57. _Unwind_Exception_Class exceptionClass,
  58. _Unwind_Exception* exceptionObject,
  59. struct _Unwind_Context* context,
  60. void* stop_parameter);
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. extern uintptr_t _Unwind_GetRegionStart(struct _Unwind_Context *context);
  65. extern uintptr_t
  66. _Unwind_GetLanguageSpecificData(struct _Unwind_Context *context);
  67. #ifdef __USING_SJLJ_EXCEPTIONS__
  68. extern _Unwind_Reason_Code
  69. _Unwind_SjLj_ForcedUnwind(_Unwind_Exception *exception_object,
  70. _Unwind_Stop_Fn stop, void *stop_parameter);
  71. #else
  72. extern _Unwind_Reason_Code
  73. _Unwind_ForcedUnwind(_Unwind_Exception *exception_object,
  74. _Unwind_Stop_Fn stop, void *stop_parameter);
  75. #endif
  76. #ifdef __USING_SJLJ_EXCEPTIONS__
  77. typedef struct _Unwind_FunctionContext *_Unwind_FunctionContext_t;
  78. extern void _Unwind_SjLj_Register(_Unwind_FunctionContext_t fc);
  79. extern void _Unwind_SjLj_Unregister(_Unwind_FunctionContext_t fc);
  80. #endif
  81. //
  82. // The following are semi-supported extensions to the C++ ABI
  83. //
  84. //
  85. // called by __cxa_rethrow().
  86. //
  87. #ifdef __USING_SJLJ_EXCEPTIONS__
  88. extern _Unwind_Reason_Code
  89. _Unwind_SjLj_Resume_or_Rethrow(_Unwind_Exception *exception_object);
  90. #else
  91. extern _Unwind_Reason_Code
  92. _Unwind_Resume_or_Rethrow(_Unwind_Exception *exception_object);
  93. #endif
  94. // _Unwind_Backtrace() is a gcc extension that walks the stack and calls the
  95. // _Unwind_Trace_Fn once per frame until it reaches the bottom of the stack
  96. // or the _Unwind_Trace_Fn function returns something other than _URC_NO_REASON.
  97. typedef _Unwind_Reason_Code (*_Unwind_Trace_Fn)(struct _Unwind_Context *,
  98. void *);
  99. extern _Unwind_Reason_Code _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
  100. // _Unwind_GetCFA is a gcc extension that can be called from within a
  101. // personality handler to get the CFA (stack pointer before call) of
  102. // current frame.
  103. extern uintptr_t _Unwind_GetCFA(struct _Unwind_Context *);
  104. // _Unwind_GetIPInfo is a gcc extension that can be called from within a
  105. // personality handler. Similar to _Unwind_GetIP() but also returns in
  106. // *ipBefore a non-zero value if the instruction pointer is at or before the
  107. // instruction causing the unwind. Normally, in a function call, the IP returned
  108. // is the return address which is after the call instruction and may be past the
  109. // end of the function containing the call instruction.
  110. extern uintptr_t _Unwind_GetIPInfo(struct _Unwind_Context *context,
  111. int *ipBefore);
  112. // __register_frame() is used with dynamically generated code to register the
  113. // FDE for a generated (JIT) code. The FDE must use pc-rel addressing to point
  114. // to its function and optional LSDA.
  115. // __register_frame() has existed in all versions of Mac OS X, but in 10.4 and
  116. // 10.5 it was buggy and did not actually register the FDE with the unwinder.
  117. // In 10.6 and later it does register properly.
  118. extern void __register_frame(const void *fde);
  119. extern void __deregister_frame(const void *fde);
  120. // _Unwind_Find_FDE() will locate the FDE if the pc is in some function that has
  121. // an associated FDE. Note, Mac OS X 10.6 and later, introduces "compact unwind
  122. // info" which the runtime uses in preference to DWARF unwind info. This
  123. // function will only work if the target function has an FDE but no compact
  124. // unwind info.
  125. struct dwarf_eh_bases {
  126. uintptr_t tbase;
  127. uintptr_t dbase;
  128. uintptr_t func;
  129. };
  130. extern const void *_Unwind_Find_FDE(const void *pc, struct dwarf_eh_bases *);
  131. // This function attempts to find the start (address of first instruction) of
  132. // a function given an address inside the function. It only works if the
  133. // function has an FDE (DWARF unwind info).
  134. // This function is unimplemented on Mac OS X 10.6 and later. Instead, use
  135. // _Unwind_Find_FDE() and look at the dwarf_eh_bases.func result.
  136. extern void *_Unwind_FindEnclosingFunction(void *pc);
  137. // Mac OS X does not support text-rel and data-rel addressing so these functions
  138. // are unimplemented.
  139. extern uintptr_t _Unwind_GetDataRelBase(struct _Unwind_Context *context)
  140. LIBUNWIND_UNAVAIL;
  141. extern uintptr_t _Unwind_GetTextRelBase(struct _Unwind_Context *context)
  142. LIBUNWIND_UNAVAIL;
  143. // Mac OS X 10.4 and 10.5 had implementations of these functions in
  144. // libgcc_s.dylib, but they never worked.
  145. /// These functions are no longer available on Mac OS X.
  146. extern void __register_frame_info_bases(const void *fde, void *ob, void *tb,
  147. void *db) LIBUNWIND_UNAVAIL;
  148. extern void __register_frame_info(const void *fde, void *ob)
  149. LIBUNWIND_UNAVAIL;
  150. extern void __register_frame_info_table_bases(const void *fde, void *ob,
  151. void *tb, void *db)
  152. LIBUNWIND_UNAVAIL;
  153. extern void __register_frame_info_table(const void *fde, void *ob)
  154. LIBUNWIND_UNAVAIL;
  155. extern void __register_frame_table(const void *fde)
  156. LIBUNWIND_UNAVAIL;
  157. extern void *__deregister_frame_info(const void *fde)
  158. LIBUNWIND_UNAVAIL;
  159. extern void *__deregister_frame_info_bases(const void *fde)
  160. LIBUNWIND_UNAVAIL;
  161. #if defined(__SEH__) && !defined(__USING_SJLJ_EXCEPTIONS__)
  162. #ifndef _WIN32
  163. typedef struct _EXCEPTION_RECORD EXCEPTION_RECORD;
  164. typedef struct _CONTEXT CONTEXT;
  165. typedef struct _DISPATCHER_CONTEXT DISPATCHER_CONTEXT;
  166. #elif !defined(__MINGW32__) && VER_PRODUCTBUILD < 8000
  167. typedef struct _DISPATCHER_CONTEXT DISPATCHER_CONTEXT;
  168. #endif
  169. // This is the common wrapper for GCC-style personality functions with SEH.
  170. extern EXCEPTION_DISPOSITION _GCC_specific_handler(EXCEPTION_RECORD *exc,
  171. void *frame, CONTEXT *ctx,
  172. DISPATCHER_CONTEXT *disp,
  173. _Unwind_Personality_Fn pers);
  174. #endif
  175. #ifdef _YNDX_LIBUNWIND_ENABLE_EXCEPTION_BACKTRACE
  176. #define _YNDX_LIBUNWIND_EXCEPTION_BACKTRACE_SIZE 128
  177. // NB. How to compute:
  178. // offsetof(__cxa_exception, unwindHeader) + (sizeof(_Unwind_Backtrace_Buffer) + 15) / 16 * 16 - sizeof(_Unwind_Backtrace_Buffer)
  179. // Correctness of this value is static_assert'd in contrib/libs/cxxsupp/libcxxrta/exception.cc
  180. #define _YNDX_LIBUNWIND_EXCEPTION_BACKTRACE_MAGIC_OFFSET 104
  181. #define _YNDX_LIBUNWIND_EXCEPTION_BACKTRACE_PRIMARY_CLASS 0xacadacadull
  182. #define _YNDX_LIBUNWIND_EXCEPTION_BACKTRACE_DEPENDENT_CLASS 0xddddacadull
  183. typedef struct _Unwind_Backtrace_Buffer {
  184. size_t size;
  185. void* backtrace[_YNDX_LIBUNWIND_EXCEPTION_BACKTRACE_SIZE];
  186. } _Unwind_Backtrace_Buffer;
  187. #endif
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. #endif // __UNWIND_H__