2010-05-11 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / mini / mini-darwin.c
blobaaa74271e79ea9d0625d83f9cbef40f00a6ecb11
1 /*
2 * mini-darwin.c: Darwin/MacOS support for Mono.
4 * Authors:
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.
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
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>
53 #include "mini.h"
54 #include <string.h>
55 #include <ctype.h>
56 #include "trace.h"
57 #include "version.h"
59 #include "jit-icalls.h"
61 /* MacOS includes */
62 #include <mach/mach.h>
63 #include <mach/mach_error.h>
64 #include <mach/exception.h>
65 #include <mach/task.h>
66 #include <pthread.h>
67 #include <dlfcn.h>
69 #ifdef HAVE_SGEN_GC
70 #undef pthread_create
71 #undef pthread_join
72 #undef pthread_detach
73 #endif
76 * This code disables the CrashReporter of MacOS X by installing
77 * a dummy Mach exception handler.
81 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
83 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
86 * The exception message
88 typedef struct {
89 mach_msg_base_t msg; /* common mach message header */
90 char payload [1024]; /* opaque */
91 } mach_exception_msg_t;
93 /* The exception port */
94 static mach_port_t mach_exception_port = VM_MAP_NULL;
97 * Implicitly called by exc_server. Must be public.
99 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
101 kern_return_t
102 catch_exception_raise (
103 mach_port_t exception_port,
104 mach_port_t thread,
105 mach_port_t task,
106 exception_type_t exception,
107 exception_data_t code,
108 mach_msg_type_number_t code_count)
110 /* consume the exception */
111 return KERN_FAILURE;
115 * Exception thread handler.
117 static
118 void *
119 mach_exception_thread (void *arg)
121 for (;;) {
122 mach_exception_msg_t request;
123 mach_exception_msg_t reply;
124 mach_msg_return_t result;
126 /* receive from "mach_exception_port" */
127 result = mach_msg (&request.msg.header,
128 MACH_RCV_MSG | MACH_RCV_LARGE,
130 sizeof (request),
131 mach_exception_port,
132 MACH_MSG_TIMEOUT_NONE,
133 MACH_PORT_NULL);
135 g_assert (result == MACH_MSG_SUCCESS);
137 /* dispatch to catch_exception_raise () */
138 exc_server (&request.msg.header, &reply.msg.header);
140 /* send back to sender */
141 result = mach_msg (&reply.msg.header,
142 MACH_SEND_MSG,
143 reply.msg.header.msgh_size,
145 MACH_PORT_NULL,
146 MACH_MSG_TIMEOUT_NONE,
147 MACH_PORT_NULL);
149 g_assert (result == MACH_MSG_SUCCESS);
151 return NULL;
154 static void
155 macosx_register_exception_handler ()
157 mach_port_t task;
158 pthread_attr_t attr;
159 pthread_t thread;
161 if (mach_exception_port != VM_MAP_NULL)
162 return;
164 task = mach_task_self ();
166 /* create the "mach_exception_port" with send & receive rights */
167 g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
168 &mach_exception_port) == KERN_SUCCESS);
169 g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
170 MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
172 /* create the exception handler thread */
173 g_assert (!pthread_attr_init (&attr));
174 g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
175 g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
176 pthread_attr_destroy (&attr);
179 * register "mach_exception_port" as a receiver for the
180 * EXC_BAD_ACCESS exception
182 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
184 g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
185 mach_exception_port,
186 EXCEPTION_DEFAULT,
187 MACHINE_THREAD_STATE) == KERN_SUCCESS);
190 void
191 mono_runtime_install_handlers (void)
193 macosx_register_exception_handler ();
194 mono_runtime_posix_install_handlers ();
196 /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
197 * This causes obscure SIGTRAP's for any application that comes across this built on
198 * Snow Leopard. This is a horrible hack to ensure that the private __CFInitialize
199 * is run on the main thread, so that we don't get SIGTRAPs later
201 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
203 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
204 if (handle == NULL)
205 return;
207 dlclose (handle);
209 #endif
212 pid_t
213 mono_runtime_syscall_fork ()
215 #if defined(__i386__)
216 /* Apple's fork syscall returns a regpair in EAX:EDX.
217 * EAX == pid of caller always
218 * EDX == 0 for parent, 1 for child
220 register_t eax;
221 register_t edx;
222 pid_t pid;
224 __asm__ __volatile__ (
225 "mov $0x2, %%eax;"
226 "int $0x80;"
227 "mov %%eax, %0;"
228 "mov %%edx, %1;"
229 : "=m" (eax), "=m" (edx));
231 if (edx == 0) {
232 pid = eax;
233 } else if (edx == 1) {
234 pid = 0;
235 } else {
236 g_assert_not_reached ();
239 return pid;
240 #else
241 g_assert_not_reached ();
242 #endif
245 gboolean
246 mono_gdb_render_native_backtraces ()
248 const char *argv [5];
249 char gdb_template [] = "/tmp/mono-gdb-commands.XXXXXX";
251 argv [0] = g_find_program_in_path ("gdb");
252 if (argv [0] == NULL) {
253 return FALSE;
256 if (mkstemp (gdb_template) != -1) {
257 FILE *gdb_commands = fopen (gdb_template, "w");
259 fprintf (gdb_commands, "attach %ld\n", (long) getpid ());
260 fprintf (gdb_commands, "info threads\n");
261 fprintf (gdb_commands, "thread apply all bt\n");
263 fflush (gdb_commands);
264 fclose (gdb_commands);
266 argv [1] = "-batch";
267 argv [2] = "-x";
268 argv [3] = gdb_template;
269 argv [4] = 0;
271 execv (argv [0], (char**)argv);
273 unlink (gdb_template);
276 return TRUE;