73be22c7c6e91d531b43f47c54efdeb9be0424ec
[mono.git] / mono / mini / mini-darwin.c
1 /**
2  * \file
3  * Darwin/MacOS support for Mono.
4  *
5  * Authors:
6  *   Mono Team (mono-list@lists.ximian.com)
7  *
8  * Copyright 2001-2003 Ximian, Inc.
9  * Copyright 2003-2011 Novell, Inc (http://www.novell.com)
10  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11  *
12  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13  */
14 #include <config.h>
15 #include <signal.h>
16 #ifdef HAVE_ALLOCA_H
17 #include <alloca.h>
18 #endif
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #include <math.h>
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
26
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/loader.h>
29 #include <mono/metadata/tabledefs.h>
30 #include <mono/metadata/class.h>
31 #include <mono/metadata/object.h>
32 #include <mono/metadata/tokentype.h>
33 #include <mono/metadata/tabledefs.h>
34 #include <mono/metadata/threads.h>
35 #include <mono/metadata/appdomain.h>
36 #include <mono/metadata/debug-helpers.h>
37 #include <mono/metadata/profiler-private.h>
38 #include <mono/metadata/mono-config.h>
39 #include <mono/metadata/environment.h>
40 #include <mono/metadata/mono-debug.h>
41 #include <mono/metadata/threads-types.h>
42 #include <mono/metadata/verify.h>
43 #include <mono/metadata/verify-internals.h>
44 #include <mono/metadata/mempool-internals.h>
45 #include <mono/metadata/attach.h>
46 #include <mono/metadata/gc-internals.h>
47 #include <mono/utils/mono-math.h>
48 #include <mono/utils/mono-compiler.h>
49 #include <mono/utils/mono-counters.h>
50 #include <mono/utils/mono-logger-internals.h>
51 #include <mono/utils/mono-mmap.h>
52 #include <mono/utils/dtrace.h>
53
54 #include "mini.h"
55 #include <string.h>
56 #include <ctype.h>
57 #include "trace.h"
58 #include "version.h"
59
60 #include "jit-icalls.h"
61
62 /* MacOS includes */
63 #include <mach/mach.h>
64 #include <mach/mach_error.h>
65 #include <mach/exception.h>
66 #include <mach/task.h>
67 #include <pthread.h>
68 #include <dlfcn.h>
69 #include <AvailabilityMacros.h>
70
71 /* This is #define'd by Boehm GC to _GC_dlopen. */
72 #undef dlopen
73
74 void* dlopen(const char* path, int mode);
75
76 void
77 mono_runtime_install_handlers (void)
78 {
79         mono_runtime_posix_install_handlers ();
80
81         /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
82          * This causes obscure SIGTRAP's for any application that comes across this built on
83          * Snow Leopard.  This is a horrible hack to ensure that the private __CFInitialize
84          * is run on the main thread, so that we don't get SIGTRAPs later
85          */
86 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
87         {
88                 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
89                 if (handle == NULL)
90                         return;
91
92                 dlclose (handle);
93         }
94 #endif
95 }
96
97 gboolean
98 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
99 {
100         kern_return_t ret;
101         mach_msg_type_number_t num_state, num_fpstate;
102         thread_state_t state, fpstate;
103         ucontext_t ctx;
104         mcontext_t mctx;
105         MonoJitTlsData *jit_tls;
106         void *domain;
107         MonoLMF *lmf = NULL;
108         gpointer *addr;
109
110         g_assert (info);
111         /*Zero enough state to make sure the caller doesn't confuse itself*/
112         tctx->valid = FALSE;
113         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = NULL;
114         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = NULL;
115         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = NULL;
116
117         state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
118         fpstate = (thread_state_t) alloca (mono_mach_arch_get_thread_fpstate_size ());
119         mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
120
121         do {
122                 ret = mono_mach_arch_get_thread_states (info->native_handle, state, &num_state, fpstate, &num_fpstate);
123         } while (ret == KERN_ABORTED);
124         if (ret != KERN_SUCCESS)
125                 return FALSE;
126
127         mono_mach_arch_thread_states_to_mcontext (state, fpstate, mctx);
128         ctx.uc_mcontext = mctx;
129
130         mono_sigctx_to_monoctx (&ctx, &tctx->ctx);
131
132         /* mono_set_jit_tls () sets this */
133         jit_tls = mono_thread_info_tls_get (info, TLS_KEY_JIT_TLS);
134         /* SET_APPDOMAIN () sets this */
135         domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
136
137         /*Thread already started to cleanup, can no longer capture unwind state*/
138         if (!jit_tls || !domain)
139                 return FALSE;
140
141         /*
142          * The current LMF address is kept in a separate TLS variable, and its hard to read its value without
143          * arch-specific code. But the address of the TLS variable is stored in another TLS variable which
144          * can be accessed through MonoThreadInfo.
145          */
146         /* mono_set_lmf_addr () sets this */
147         addr = mono_thread_info_tls_get (info, TLS_KEY_LMF_ADDR);
148         if (addr)
149                 lmf = *addr;
150
151
152         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = domain;
153         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = jit_tls;
154         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = lmf;
155         tctx->valid = TRUE;
156
157         return TRUE;
158 }