2 * mini-darwin.c: Darwin/MacOS support for Mono.
5 * Mono Team (mono-list@lists.ximian.com)
7 * Copyright 2001-2003 Ximian, Inc.
8 * Copyright 2003-2008 Ximian, Inc.
10 * See LICENSE for licensing information.
21 #ifdef HAVE_SYS_TIME_H
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>
60 #include "jit-icalls.h"
63 #include <mach/mach.h>
64 #include <mach/mach_error.h>
65 #include <mach/exception.h>
66 #include <mach/task.h>
69 #include <AvailabilityMacros.h>
72 * This code disables the CrashReporter of MacOS X by installing
73 * a dummy Mach exception handler.
77 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
79 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
82 * The exception message
85 mach_msg_base_t msg; /* common mach message header */
86 char payload [1024]; /* opaque */
87 } mach_exception_msg_t;
89 /* The exception port */
90 static mach_port_t mach_exception_port = VM_MAP_NULL;
93 * Implicitly called by exc_server. Must be public.
95 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
98 catch_exception_raise (
99 mach_port_t exception_port,
102 exception_type_t exception,
103 exception_data_t code,
104 mach_msg_type_number_t code_count)
106 /* consume the exception */
111 * Exception thread handler.
115 mach_exception_thread (void *arg)
118 mach_exception_msg_t request;
119 mach_exception_msg_t reply;
120 mach_msg_return_t result;
122 /* receive from "mach_exception_port" */
123 result = mach_msg (&request.msg.header,
124 MACH_RCV_MSG | MACH_RCV_LARGE,
128 MACH_MSG_TIMEOUT_NONE,
131 g_assert (result == MACH_MSG_SUCCESS);
133 /* dispatch to catch_exception_raise () */
134 exc_server (&request.msg.header, &reply.msg.header);
136 /* send back to sender */
137 result = mach_msg (&reply.msg.header,
139 reply.msg.header.msgh_size,
142 MACH_MSG_TIMEOUT_NONE,
145 g_assert (result == MACH_MSG_SUCCESS);
151 macosx_register_exception_handler ()
157 if (mach_exception_port != VM_MAP_NULL)
160 task = mach_task_self ();
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);
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);
175 * register "mach_exception_port" as a receiver for the
176 * EXC_BAD_ACCESS exception
178 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
180 g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
183 MACHINE_THREAD_STATE) == KERN_SUCCESS);
185 mono_gc_register_mach_exception_thread (thread);
188 /* This is #define'd by Boehm GC to _GC_dlopen. */
192 mono_runtime_install_handlers (void)
194 #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_5
195 macosx_register_exception_handler ();
197 mono_runtime_posix_install_handlers ();
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
204 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
206 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
216 mono_runtime_syscall_fork ()
218 return (pid_t) fork ();
222 mono_gdb_render_native_backtraces ()
224 const char *argv [5];
225 char gdb_template [] = "/tmp/mono-gdb-commands.XXXXXX";
227 argv [0] = g_find_program_in_path ("gdb");
228 if (argv [0] == NULL) {
232 if (mkstemp (gdb_template) != -1) {
233 FILE *gdb_commands = fopen (gdb_template, "w");
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");
239 fflush (gdb_commands);
240 fclose (gdb_commands);
244 argv [3] = gdb_template;
247 execv (argv [0], (char**)argv);
249 unlink (gdb_template);
256 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoNativeThreadId thread_id, MonoNativeThreadHandle thread_handle)
259 mach_msg_type_number_t num_state;
260 thread_state_t state;
263 guint32 domain_key, jit_key;
264 MonoJitTlsData *jit_tls;
267 state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
268 mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
270 ret = mono_mach_arch_get_thread_state (thread_handle, state, &num_state);
271 if (ret != KERN_SUCCESS)
274 mono_mach_arch_thread_state_to_mcontext (state, mctx);
275 ctx.uc_mcontext = mctx;
277 mono_sigctx_to_monoctx (&ctx, &tctx->ctx);
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);
284 /*Thread already started to cleanup, can no longer capture unwind state*/
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;