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