Merge pull request #2720 from mono/fix-39325
[mono.git] / mono / utils / mono-context.h
index dc53e857aae60158bae7474d76c8a662dcdd8465..c1159ca723a684d75ed193a6be933b2d9b193fca 100644 (file)
@@ -3,6 +3,7 @@
  *
  *
  * Copyright (c) 2011 Novell, Inc (http://www.novell.com)
+ * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  */
 
 
 #include <signal.h>
 #endif
 
-#if defined (HOST_WATCHOS) || defined (HOST_APPLETVOS)
-#include <libunwind.h>
-#endif
-
 /*
  * General notes about mono-context.
  * Each arch defines a MonoContext struct with all GPR regs + IP/PC.
 #if defined( __linux__) || defined(__sun) || defined(__APPLE__) || defined(__NetBSD__) || \
        defined(__FreeBSD__) || defined(__OpenBSD__)
 #ifdef HAVE_SIGACTION
-#define MONO_SIGNAL_USE_SIGACTION 1
+#define MONO_SIGNAL_USE_UCONTEXT_T 1
 #endif
 #endif
 
 #if defined(__native_client__)
-#undef MONO_SIGNAL_USE_SIGACTION
+#undef MONO_SIGNAL_USE_UCONTEXT_T
 #endif
 
 #ifdef HOST_WIN32
@@ -156,7 +153,7 @@ typedef struct {
 #if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
 
 #ifdef HAVE_SIGACTION
-#define MONO_SIGNAL_USE_SIGACTION 1
+#define MONO_SIGNAL_USE_UCONTEXT_T 1
 #endif
 
 #endif
@@ -256,26 +253,6 @@ typedef struct {
 #if defined(HOST_WATCHOS)
 
 #define MONO_CONTEXT_GET_CURRENT(ctx) do { \
-       unw_context_t uctx; \
-       unw_cursor_t c; \
-       unw_word_t data; \
-       g_assert (unw_getcontext (&uctx) == 0); \
-       g_assert (unw_init_local (&c, &uctx) == 0); \
-       for (int reg = 0; reg < 13; ++reg) { \
-               unw_get_reg (&c, (unw_regnum_t) UNW_ARM_R0 + reg, &data); \
-               ctx.regs[reg] = data; \
-       } \
-       unw_get_reg (&c, UNW_ARM_SP, &data); \
-       ctx.regs[ARMREG_SP] = data; \
-       unw_get_reg (&c, UNW_ARM_LR, &data); \
-       ctx.regs[ARMREG_LR] = data; \
-       unw_get_reg (&c, UNW_ARM_IP, &data); \
-       ctx.regs[ARMREG_PC] = data; \
-       ctx.pc = ctx.regs[ARMREG_PC]; \
-       for (int reg = 0; reg < 16; ++reg) { \
-               unw_get_reg (&c, (unw_regnum_t) UNW_ARM_D0 + reg, &data); \
-               ctx.fregs[reg] = data; \
-       } \
 } while (0);
 
 #else
@@ -328,8 +305,14 @@ typedef struct {
 #if defined (HOST_APPLETVOS)
 
 #define MONO_CONTEXT_GET_CURRENT(ctx) do { \
-       fprintf (stderr, "MONO_CONTEXT_GET_CURRENT: Not implemented"); \
-       g_error ("MONO_CONTEXT_GET_CURRENT: Not implemented"); \
+       arm_unified_thread_state_t thread_state;        \
+       int state_flavor = ARM_UNIFIED_THREAD_STATE;    \
+       unsigned state_count = ARM_UNIFIED_THREAD_STATE_COUNT;  \
+       thread_port_t self = mach_thread_self ();       \
+       kern_return_t ret = thread_get_state (self, state_flavor, (thread_state_t) &thread_state, &state_count);        \
+       g_assert (ret == 0);    \
+       mono_mach_arch_thread_state_to_mono_context ((thread_state_t)&thread_state, &ctx); \
+       mach_port_deallocate (current_task (), self);   \
 } while (0);
 
 #else