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/utils/mono-math.h>
47 #include <mono/utils/mono-compiler.h>
48 #include <mono/utils/mono-counters.h>
49 #include <mono/utils/mono-logger-internal.h>
50 #include <mono/utils/mono-mmap.h>
51 #include <mono/utils/dtrace.h>
59 #include "jit-icalls.h"
62 #include <mach/mach.h>
63 #include <mach/mach_error.h>
64 #include <mach/exception.h>
65 #include <mach/task.h>
70 * This code disables the CrashReporter of MacOS X by installing
71 * a dummy Mach exception handler.
75 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
77 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
80 * The exception message
83 mach_msg_base_t msg; /* common mach message header */
84 char payload [1024]; /* opaque */
85 } mach_exception_msg_t;
87 /* The exception port */
88 static mach_port_t mach_exception_port = VM_MAP_NULL;
91 * Implicitly called by exc_server. Must be public.
93 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
96 catch_exception_raise (
97 mach_port_t exception_port,
100 exception_type_t exception,
101 exception_data_t code,
102 mach_msg_type_number_t code_count)
104 /* consume the exception */
109 * Exception thread handler.
113 mach_exception_thread (void *arg)
116 mach_exception_msg_t request;
117 mach_exception_msg_t reply;
118 mach_msg_return_t result;
120 /* receive from "mach_exception_port" */
121 result = mach_msg (&request.msg.header,
122 MACH_RCV_MSG | MACH_RCV_LARGE,
126 MACH_MSG_TIMEOUT_NONE,
129 g_assert (result == MACH_MSG_SUCCESS);
131 /* dispatch to catch_exception_raise () */
132 exc_server (&request.msg.header, &reply.msg.header);
134 /* send back to sender */
135 result = mach_msg (&reply.msg.header,
137 reply.msg.header.msgh_size,
140 MACH_MSG_TIMEOUT_NONE,
143 g_assert (result == MACH_MSG_SUCCESS);
149 macosx_register_exception_handler ()
155 if (mach_exception_port != VM_MAP_NULL)
158 task = mach_task_self ();
160 /* create the "mach_exception_port" with send & receive rights */
161 g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
162 &mach_exception_port) == KERN_SUCCESS);
163 g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
164 MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
166 /* create the exception handler thread */
167 g_assert (!pthread_attr_init (&attr));
168 g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
169 g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
170 pthread_attr_destroy (&attr);
173 * register "mach_exception_port" as a receiver for the
174 * EXC_BAD_ACCESS exception
176 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
178 g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
181 MACHINE_THREAD_STATE) == KERN_SUCCESS);
185 mono_runtime_install_handlers (void)
187 macosx_register_exception_handler ();
188 mono_runtime_posix_install_handlers ();
190 /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
191 * This causes obscure SIGTRAP's for any application that comes across this built on
192 * Snow Leopard. This is a horrible hack to ensure that the private __CFInitialize
193 * is run on the main thread, so that we don't get SIGTRAPs later
195 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
197 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
207 mono_runtime_syscall_fork ()
209 #if defined(__i386__)
210 /* Apple's fork syscall returns a regpair in EAX:EDX.
211 * EAX == pid of caller always
212 * EDX == 0 for parent, 1 for child
218 __asm__ __volatile__ (
223 : "=m" (eax), "=m" (edx));
227 } else if (edx == 1) {
230 g_assert_not_reached ();
235 g_assert_not_reached ();
240 mono_gdb_render_native_backtraces ()
242 const char *argv [5];
243 char gdb_template [] = "/tmp/mono-gdb-commands.XXXXXX";
245 argv [0] = g_find_program_in_path ("gdb");
246 if (argv [0] == NULL) {
250 if (mkstemp (gdb_template) != -1) {
251 FILE *gdb_commands = fopen (gdb_template, "w");
253 fprintf (gdb_commands, "attach %ld\n", (long) getpid ());
254 fprintf (gdb_commands, "info threads\n");
255 fprintf (gdb_commands, "thread apply all bt\n");
257 fflush (gdb_commands);
258 fclose (gdb_commands);
262 argv [3] = gdb_template;
265 execv (argv [0], (char**)argv);
267 unlink (gdb_template);