Don't use System.Environment.Exit to finish a sdb session on iOS.
[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-2011 Novell, Inc (http://www.novell.com)
9  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10  *
11  * See LICENSE for licensing information.
12  */
13 #include <config.h>
14 #include <signal.h>
15 #ifdef HAVE_ALLOCA_H
16 #include <alloca.h>
17 #endif
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #include <math.h>
22 #ifdef HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25
26 #include <mono/metadata/assembly.h>
27 #include <mono/metadata/loader.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/class.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/tokentype.h>
32 #include <mono/metadata/tabledefs.h>
33 #include <mono/metadata/threads.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/io-layer/io-layer.h>
37 #include "mono/metadata/profiler.h"
38 #include <mono/metadata/profiler-private.h>
39 #include <mono/metadata/mono-config.h>
40 #include <mono/metadata/environment.h>
41 #include <mono/metadata/mono-debug.h>
42 #include <mono/metadata/threads-types.h>
43 #include <mono/metadata/verify.h>
44 #include <mono/metadata/verify-internals.h>
45 #include <mono/metadata/mempool-internals.h>
46 #include <mono/metadata/attach.h>
47 #include <mono/metadata/gc-internal.h>
48 #include <mono/utils/mono-math.h>
49 #include <mono/utils/mono-compiler.h>
50 #include <mono/utils/mono-counters.h>
51 #include <mono/utils/mono-logger-internal.h>
52 #include <mono/utils/mono-mmap.h>
53 #include <mono/utils/dtrace.h>
54
55 #include "mini.h"
56 #include <string.h>
57 #include <ctype.h>
58 #include "trace.h"
59 #include "version.h"
60
61 #include "jit-icalls.h"
62
63 /* MacOS includes */
64 #include <mach/mach.h>
65 #include <mach/mach_error.h>
66 #include <mach/exception.h>
67 #include <mach/task.h>
68 #include <pthread.h>
69 #include <dlfcn.h>
70 #include <AvailabilityMacros.h>
71 #include <TargetConditionals.h>
72
73 #if (MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5) && TARGET_OS_IPHONE == 0 && TARGET_IPHONE_SIMULATOR == 0
74 #define NEEDS_EXCEPTION_THREAD
75 #endif
76
77 #ifdef NEEDS_EXCEPTION_THREAD
78
79 /*
80  * This code disables the CrashReporter of MacOS X by installing
81  * a dummy Mach exception handler.
82  */
83
84 /*
85  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
86  */
87 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
88
89 /*
90  * The exception message
91  */
92 typedef struct {
93         mach_msg_base_t msg;  /* common mach message header */
94         char payload [1024];  /* opaque */
95 } mach_exception_msg_t;
96
97 /* The exception port */
98 static mach_port_t mach_exception_port = VM_MAP_NULL;
99
100 kern_return_t
101 catch_exception_raise (
102         mach_port_t exception_port,
103         mach_port_t thread,
104         mach_port_t task,
105         exception_type_t exception,
106         exception_data_t code,
107         mach_msg_type_number_t code_count);
108
109 /*
110  * Implicitly called by exc_server. Must be public.
111  *
112  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
113  */
114 kern_return_t
115 catch_exception_raise (
116         mach_port_t exception_port,
117         mach_port_t thread,
118         mach_port_t task,
119         exception_type_t exception,
120         exception_data_t code,
121         mach_msg_type_number_t code_count)
122 {
123         /* consume the exception */
124         return KERN_FAILURE;
125 }
126
127 /*
128  * Exception thread handler.
129  */
130 static
131 void *
132 mach_exception_thread (void *arg)
133 {
134         for (;;) {
135                 mach_exception_msg_t request;
136                 mach_exception_msg_t reply;
137                 mach_msg_return_t result;
138
139                 /* receive from "mach_exception_port" */
140                 result = mach_msg (&request.msg.header,
141                                    MACH_RCV_MSG | MACH_RCV_LARGE,
142                                    0,
143                                    sizeof (request),
144                                    mach_exception_port,
145                                    MACH_MSG_TIMEOUT_NONE,
146                                    MACH_PORT_NULL);
147
148                 g_assert (result == MACH_MSG_SUCCESS);
149
150                 /* dispatch to catch_exception_raise () */
151                 exc_server (&request.msg.header, &reply.msg.header);
152
153                 /* send back to sender */
154                 result = mach_msg (&reply.msg.header,
155                                    MACH_SEND_MSG,
156                                    reply.msg.header.msgh_size,
157                                    0,
158                                    MACH_PORT_NULL,
159                                    MACH_MSG_TIMEOUT_NONE,
160                                    MACH_PORT_NULL);
161
162                 /*
163                 If we try to abort the thread while delivering an exception. The port will be gone since the kernel
164                 setup a send once port to deliver the resume message and thread_abort will consume it.
165                 */
166                 g_assert (result == MACH_MSG_SUCCESS || result == MACH_SEND_INVALID_DEST);
167         }
168         return NULL;
169 }
170
171 static void
172 macosx_register_exception_handler (void)
173 {
174         mach_port_t task;
175         pthread_attr_t attr;
176         pthread_t thread;
177
178         if (mach_exception_port != VM_MAP_NULL)
179                 return;
180
181         task = mach_task_self ();
182
183         /* create the "mach_exception_port" with send & receive rights */
184         g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
185                                       &mach_exception_port) == KERN_SUCCESS);
186         g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
187                                           MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
188
189         /* create the exception handler thread */
190         g_assert (!pthread_attr_init (&attr));
191         g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
192         g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
193         pthread_attr_destroy (&attr);
194
195         /*
196          * register "mach_exception_port" as a receiver for the
197          * EXC_BAD_ACCESS exception
198          *
199          * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
200          */
201         g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
202                                             mach_exception_port,
203                                             EXCEPTION_DEFAULT,
204                                             MACHINE_THREAD_STATE) == KERN_SUCCESS);
205
206         mono_gc_register_mach_exception_thread (thread);
207 }
208
209 #endif
210
211 /* This is #define'd by Boehm GC to _GC_dlopen. */
212 #undef dlopen
213
214 void* dlopen(const char* path, int mode);
215
216 void
217 mono_runtime_install_handlers (void)
218 {
219 #ifdef NEEDS_EXCEPTION_THREAD
220         macosx_register_exception_handler ();
221 #endif
222         mono_runtime_posix_install_handlers ();
223
224         /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
225          * This causes obscure SIGTRAP's for any application that comes across this built on
226          * Snow Leopard.  This is a horrible hack to ensure that the private __CFInitialize
227          * is run on the main thread, so that we don't get SIGTRAPs later
228          */
229 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
230         {
231                 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
232                 if (handle == NULL)
233                         return;
234
235                 dlclose (handle);
236         }
237 #endif
238 }
239
240 pid_t
241 mono_runtime_syscall_fork ()
242 {
243         return (pid_t) fork ();
244 }
245
246 void
247 mono_gdb_render_native_backtraces (pid_t crashed_pid)
248 {
249         const char *argv [5];
250         char gdb_template [] = "/tmp/mono-gdb-commands.XXXXXX";
251
252         argv [0] = g_find_program_in_path ("gdb");
253         if (argv [0] == NULL) {
254                 return;
255         }
256
257         if (mkstemp (gdb_template) != -1) {
258                 FILE *gdb_commands = fopen (gdb_template, "w");
259
260                 fprintf (gdb_commands, "attach %ld\n", (long) crashed_pid);
261                 fprintf (gdb_commands, "info threads\n");
262                 fprintf (gdb_commands, "thread apply all bt\n");
263
264                 fflush (gdb_commands);
265                 fclose (gdb_commands);
266
267                 argv [1] = "-batch";
268                 argv [2] = "-x";
269                 argv [3] = gdb_template;
270                 argv [4] = 0;
271
272                 execv (argv [0], (char**)argv);
273
274                 unlink (gdb_template);
275         }
276 }
277
278 gboolean
279 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoNativeThreadId thread_id, MonoNativeThreadHandle thread_handle)
280 {
281         kern_return_t ret;
282         mach_msg_type_number_t num_state;
283         thread_state_t state;
284         ucontext_t ctx;
285         mcontext_t mctx;
286         guint32 domain_key, jit_key;
287         MonoJitTlsData *jit_tls;
288         void *domain;
289 #if defined (MONO_ARCH_ENABLE_MONO_LMF_VAR)
290         guint32 lmf_key;
291 #endif
292
293         /*Zero enough state to make sure the caller doesn't confuse itself*/
294         tctx->valid = FALSE;
295         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = NULL;
296         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = NULL;
297         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = NULL;
298
299         state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
300         mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
301
302         ret = mono_mach_arch_get_thread_state (thread_handle, state, &num_state);
303         if (ret != KERN_SUCCESS)
304                 return FALSE;
305
306         mono_mach_arch_thread_state_to_mcontext (state, mctx);
307         ctx.uc_mcontext = mctx;
308
309         mono_sigctx_to_monoctx (&ctx, &tctx->ctx);
310
311         domain_key = mono_domain_get_tls_offset ();
312         jit_key = mono_get_jit_tls_key ();
313
314         jit_tls = mono_mach_arch_get_tls_value_from_thread (thread_id, jit_key);
315         domain = mono_mach_arch_get_tls_value_from_thread (thread_id, domain_key);
316
317         /*Thread already started to cleanup, can no longer capture unwind state*/
318         if (!jit_tls || !domain)
319                 return FALSE;
320
321 #if defined (MONO_ARCH_ENABLE_MONO_LMF_VAR)
322         lmf_key =  mono_get_lmf_tls_offset ();
323         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = mono_mach_arch_get_tls_value_from_thread (thread_id, lmf_key);;
324 #else
325         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = jit_tls ? jit_tls->lmf : NULL;
326 #endif
327
328         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = domain;
329         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = jit_tls;
330         tctx->valid = TRUE;
331
332         return TRUE;
333 }