Merge pull request #136 from spencerhakim/master
[mono.git] / mono / mini / mini-darwin.c
1 /*
2  * mini-darwin.c: Darwin/MacOS support for Mono.
3  *
4  * Authors:
5  *   Mono Team (mono-list@lists.ximian.com)
6  *
7  * Copyright 2001-2003 Ximian, Inc.
8  * Copyright 2003-2008 Ximian, Inc.
9  *
10  * See LICENSE for licensing information.
11  */
12 #include <config.h>
13 #include <signal.h>
14 #ifdef HAVE_ALLOCA_H
15 #include <alloca.h>
16 #endif
17 #ifdef HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif
20 #include <math.h>
21 #ifdef HAVE_SYS_TIME_H
22 #include <sys/time.h>
23 #endif
24
25 #include <mono/metadata/assembly.h>
26 #include <mono/metadata/loader.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/class.h>
29 #include <mono/metadata/object.h>
30 #include <mono/metadata/tokentype.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/threads.h>
33 #include <mono/metadata/appdomain.h>
34 #include <mono/metadata/debug-helpers.h>
35 #include <mono/io-layer/io-layer.h>
36 #include "mono/metadata/profiler.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-internal.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-internal.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 #if (MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5) && !defined (TARGET_ARM)
72 #define NEEDS_EXCEPTION_THREAD
73 #endif
74
75 #ifdef NEEDS_EXCEPTION_THREAD
76
77 /*
78  * This code disables the CrashReporter of MacOS X by installing
79  * a dummy Mach exception handler.
80  */
81
82 /*
83  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
84  */
85 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
86
87 /*
88  * The exception message
89  */
90 typedef struct {
91         mach_msg_base_t msg;  /* common mach message header */
92         char payload [1024];  /* opaque */
93 } mach_exception_msg_t;
94
95 /* The exception port */
96 static mach_port_t mach_exception_port = VM_MAP_NULL;
97
98 /*
99  * Implicitly called by exc_server. Must be public.
100  *
101  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
102  */
103 kern_return_t
104 catch_exception_raise (
105         mach_port_t exception_port,
106         mach_port_t thread,
107         mach_port_t task,
108         exception_type_t exception,
109         exception_data_t code,
110         mach_msg_type_number_t code_count)
111 {
112         /* consume the exception */
113         return KERN_FAILURE;
114 }
115
116 /*
117  * Exception thread handler.
118  */
119 static
120 void *
121 mach_exception_thread (void *arg)
122 {
123         for (;;) {
124                 mach_exception_msg_t request;
125                 mach_exception_msg_t reply;
126                 mach_msg_return_t result;
127
128                 /* receive from "mach_exception_port" */
129                 result = mach_msg (&request.msg.header,
130                                    MACH_RCV_MSG | MACH_RCV_LARGE,
131                                    0,
132                                    sizeof (request),
133                                    mach_exception_port,
134                                    MACH_MSG_TIMEOUT_NONE,
135                                    MACH_PORT_NULL);
136
137                 g_assert (result == MACH_MSG_SUCCESS);
138
139                 /* dispatch to catch_exception_raise () */
140                 exc_server (&request.msg.header, &reply.msg.header);
141
142                 /* send back to sender */
143                 result = mach_msg (&reply.msg.header,
144                                    MACH_SEND_MSG,
145                                    reply.msg.header.msgh_size,
146                                    0,
147                                    MACH_PORT_NULL,
148                                    MACH_MSG_TIMEOUT_NONE,
149                                    MACH_PORT_NULL);
150
151                 g_assert (result == MACH_MSG_SUCCESS);
152         }
153         return NULL;
154 }
155
156 static void
157 macosx_register_exception_handler ()
158 {
159         mach_port_t task;
160         pthread_attr_t attr;
161         pthread_t thread;
162
163         if (mach_exception_port != VM_MAP_NULL)
164                 return;
165
166         task = mach_task_self ();
167
168         /* create the "mach_exception_port" with send & receive rights */
169         g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
170                                       &mach_exception_port) == KERN_SUCCESS);
171         g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
172                                           MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
173
174         /* create the exception handler thread */
175         g_assert (!pthread_attr_init (&attr));
176         g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
177         g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
178         pthread_attr_destroy (&attr);
179
180         /*
181          * register "mach_exception_port" as a receiver for the
182          * EXC_BAD_ACCESS exception
183          *
184          * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
185          */
186         g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
187                                             mach_exception_port,
188                                             EXCEPTION_DEFAULT,
189                                             MACHINE_THREAD_STATE) == KERN_SUCCESS);
190
191         mono_gc_register_mach_exception_thread (thread);
192 }
193
194 #endif
195
196 /* This is #define'd by Boehm GC to _GC_dlopen. */
197 #undef dlopen
198
199 void
200 mono_runtime_install_handlers (void)
201 {
202 #ifdef NEEDS_EXCEPTION_THREAD
203         macosx_register_exception_handler ();
204 #endif
205         mono_runtime_posix_install_handlers ();
206
207         /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
208          * This causes obscure SIGTRAP's for any application that comes across this built on
209          * Snow Leopard.  This is a horrible hack to ensure that the private __CFInitialize
210          * is run on the main thread, so that we don't get SIGTRAPs later
211          */
212 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
213         {
214                 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
215                 if (handle == NULL)
216                         return;
217
218                 dlclose (handle);
219         }
220 #endif
221 }
222
223 pid_t
224 mono_runtime_syscall_fork ()
225 {
226         return (pid_t) fork ();
227 }
228
229 gboolean
230 mono_gdb_render_native_backtraces ()
231 {
232         const char *argv [5];
233         char gdb_template [] = "/tmp/mono-gdb-commands.XXXXXX";
234
235         argv [0] = g_find_program_in_path ("gdb");
236         if (argv [0] == NULL) {
237                 return FALSE;
238         }
239
240         if (mkstemp (gdb_template) != -1) {
241                 FILE *gdb_commands = fopen (gdb_template, "w");
242
243                 fprintf (gdb_commands, "attach %ld\n", (long) getpid ());
244                 fprintf (gdb_commands, "info threads\n");
245                 fprintf (gdb_commands, "thread apply all bt\n");
246
247                 fflush (gdb_commands);
248                 fclose (gdb_commands);
249
250                 argv [1] = "-batch";
251                 argv [2] = "-x";
252                 argv [3] = gdb_template;
253                 argv [4] = 0;
254
255                 execv (argv [0], (char**)argv);
256
257                 unlink (gdb_template);
258         }
259
260         return TRUE;
261 }
262
263 gboolean
264 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoNativeThreadId thread_id, MonoNativeThreadHandle thread_handle)
265 {
266         kern_return_t ret;
267         mach_msg_type_number_t num_state;
268         thread_state_t state;
269         ucontext_t ctx;
270         mcontext_t mctx;
271         guint32 domain_key, jit_key;
272         MonoJitTlsData *jit_tls;
273         void *domain;
274
275         state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
276         mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
277
278         ret = mono_mach_arch_get_thread_state (thread_handle, state, &num_state);
279         if (ret != KERN_SUCCESS)
280                 return FALSE;
281
282         mono_mach_arch_thread_state_to_mcontext (state, mctx);
283         ctx.uc_mcontext = mctx;
284
285         mono_sigctx_to_monoctx (&ctx, &tctx->ctx);
286
287         domain_key = mono_domain_get_tls_offset ();
288         jit_key = mono_get_jit_tls_key ();
289         jit_tls = mono_mach_arch_get_tls_value_from_thread (thread_id, jit_key);
290         domain = mono_mach_arch_get_tls_value_from_thread (thread_id, domain_key);
291
292         /*Thread already started to cleanup, can no longer capture unwind state*/
293         if (!jit_tls)
294                 return FALSE;
295         g_assert (domain);
296
297         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = domain;
298         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = jit_tls ? jit_tls->lmf : NULL;
299         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = jit_tls;
300         tctx->valid = TRUE;
301
302         return TRUE;
303 }