Merge pull request #2816 from xmcclure/profile-clean-0
[mono.git] / mono / utils / mono-context.h
index a2a0b7e4dc23a9d3a678555e8c4ee14745ed83bd..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
@@ -183,11 +180,11 @@ extern void mono_context_get_current (void *);
 #define MONO_CONTEXT_GET_CURRENT(ctx)  \
        __asm__ __volatile__(   \
                "movq $0x0,  %%nacl:0x00(%%r15, %0, 1)\n"       \
-               "movq %%rbx, %%nacl:0x08(%%r15, %0, 1)\n"       \
-               "movq %%rcx, %%nacl:0x10(%%r15, %0, 1)\n"       \
-               "movq %%rdx, %%nacl:0x18(%%r15, %0, 1)\n"       \
-               "movq %%rbp, %%nacl:0x20(%%r15, %0, 1)\n"       \
-               "movq %%rsp, %%nacl:0x28(%%r15, %0, 1)\n"       \
+               "movq %%rcx, %%nacl:0x08(%%r15, %0, 1)\n"       \
+               "movq %%rdx, %%nacl:0x10(%%r15, %0, 1)\n"       \
+               "movq %%rbx, %%nacl:0x18(%%r15, %0, 1)\n"       \
+               "movq %%rsp, %%nacl:0x20(%%r15, %0, 1)\n"       \
+               "movq %%rbp, %%nacl:0x28(%%r15, %0, 1)\n"       \
                "movq %%rsi, %%nacl:0x30(%%r15, %0, 1)\n"       \
                "movq %%rdi, %%nacl:0x38(%%r15, %0, 1)\n"       \
                "movq %%r8,  %%nacl:0x40(%%r15, %0, 1)\n"       \
@@ -204,14 +201,15 @@ extern void mono_context_get_current (void *);
                : "a" ((int64_t)&(ctx)) \
                : "rdx", "memory")
 #else
+
 #define MONO_CONTEXT_GET_CURRENT(ctx)  \
        __asm__ __volatile__(   \
                "movq $0x0,  0x00(%0)\n"        \
-               "movq %%rbx, 0x08(%0)\n"        \
-               "movq %%rcx, 0x10(%0)\n"        \
-               "movq %%rdx, 0x18(%0)\n"        \
-               "movq %%rbp, 0x20(%0)\n"        \
-               "movq %%rsp, 0x28(%0)\n"        \
+               "movq %%rcx, 0x08(%0)\n"        \
+               "movq %%rdx, 0x10(%0)\n"        \
+               "movq %%rbx, 0x18(%0)\n"        \
+               "movq %%rsp, 0x20(%0)\n"        \
+               "movq %%rbp, 0x28(%0)\n"        \
                "movq %%rsi, 0x30(%0)\n"        \
                "movq %%rdi, 0x38(%0)\n"        \
                "movq %%r8,  0x40(%0)\n"        \
@@ -255,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
@@ -327,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