2010-07-10 Mark Probst <mark.probst@gmail.com>
[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
70 /*
71  * This code disables the CrashReporter of MacOS X by installing
72  * a dummy Mach exception handler.
73  */
74
75 /*
76  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
77  */
78 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
79
80 /*
81  * The exception message
82  */
83 typedef struct {
84         mach_msg_base_t msg;  /* common mach message header */
85         char payload [1024];  /* opaque */
86 } mach_exception_msg_t;
87
88 /* The exception port */
89 static mach_port_t mach_exception_port = VM_MAP_NULL;
90
91 /*
92  * Implicitly called by exc_server. Must be public.
93  *
94  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
95  */
96 kern_return_t
97 catch_exception_raise (
98         mach_port_t exception_port,
99         mach_port_t thread,
100         mach_port_t task,
101         exception_type_t exception,
102         exception_data_t code,
103         mach_msg_type_number_t code_count)
104 {
105         /* consume the exception */
106         return KERN_FAILURE;
107 }
108
109 /*
110  * Exception thread handler.
111  */
112 static
113 void *
114 mach_exception_thread (void *arg)
115 {
116         for (;;) {
117                 mach_exception_msg_t request;
118                 mach_exception_msg_t reply;
119                 mach_msg_return_t result;
120
121                 /* receive from "mach_exception_port" */
122                 result = mach_msg (&request.msg.header,
123                                    MACH_RCV_MSG | MACH_RCV_LARGE,
124                                    0,
125                                    sizeof (request),
126                                    mach_exception_port,
127                                    MACH_MSG_TIMEOUT_NONE,
128                                    MACH_PORT_NULL);
129
130                 g_assert (result == MACH_MSG_SUCCESS);
131
132                 /* dispatch to catch_exception_raise () */
133                 exc_server (&request.msg.header, &reply.msg.header);
134
135                 /* send back to sender */
136                 result = mach_msg (&reply.msg.header,
137                                    MACH_SEND_MSG,
138                                    reply.msg.header.msgh_size,
139                                    0,
140                                    MACH_PORT_NULL,
141                                    MACH_MSG_TIMEOUT_NONE,
142                                    MACH_PORT_NULL);
143
144                 g_assert (result == MACH_MSG_SUCCESS);
145         }
146         return NULL;
147 }
148
149 static void
150 macosx_register_exception_handler ()
151 {
152         mach_port_t task;
153         pthread_attr_t attr;
154         pthread_t thread;
155
156         if (mach_exception_port != VM_MAP_NULL)
157                 return;
158
159         task = mach_task_self ();
160
161         /* create the "mach_exception_port" with send & receive rights */
162         g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
163                                       &mach_exception_port) == KERN_SUCCESS);
164         g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
165                                           MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
166
167         /* create the exception handler thread */
168         g_assert (!pthread_attr_init (&attr));
169         g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
170         g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
171         pthread_attr_destroy (&attr);
172
173         /*
174          * register "mach_exception_port" as a receiver for the
175          * EXC_BAD_ACCESS exception
176          *
177          * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
178          */
179         g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
180                                             mach_exception_port,
181                                             EXCEPTION_DEFAULT,
182                                             MACHINE_THREAD_STATE) == KERN_SUCCESS);
183
184         mono_gc_register_mach_exception_thread (thread);
185 }
186
187 /* This is #define'd by Boehm GC to _GC_dlopen. */
188 #undef dlopen
189
190 void
191 mono_runtime_install_handlers (void)
192 {
193         macosx_register_exception_handler ();
194         mono_runtime_posix_install_handlers ();
195
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
200          */
201 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
202         {
203                 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
204                 if (handle == NULL)
205                         return;
206
207                 dlclose (handle);
208         }
209 #endif
210 }
211
212 pid_t
213 mono_runtime_syscall_fork ()
214 {
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
219          */             
220         register_t eax;
221         register_t edx;
222         pid_t pid;
223
224         __asm__  __volatile__ (
225                 "mov $0x2, %%eax;"
226                 "int $0x80;"
227                 "mov %%eax, %0;"
228                 "mov %%edx, %1;"
229                 : "=m" (eax), "=m" (edx));
230
231         if (edx == 0) {
232                 pid = eax;
233         } else if (edx == 1) {
234                 pid = 0;
235         } else {
236                 g_assert_not_reached ();
237         }
238
239         return pid;
240 #else
241         g_assert_not_reached ();
242 #endif
243 }
244
245 gboolean
246 mono_gdb_render_native_backtraces ()
247 {
248         const char *argv [5];
249         char gdb_template [] = "/tmp/mono-gdb-commands.XXXXXX";
250
251         argv [0] = g_find_program_in_path ("gdb");
252         if (argv [0] == NULL) {
253                 return FALSE;
254         }
255
256         if (mkstemp (gdb_template) != -1) {
257                 FILE *gdb_commands = fopen (gdb_template, "w");
258
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");
262
263                 fflush (gdb_commands);
264                 fclose (gdb_commands);
265
266                 argv [1] = "-batch";
267                 argv [2] = "-x";
268                 argv [3] = gdb_template;
269                 argv [4] = 0;
270
271                 execv (argv [0], (char**)argv);
272
273                 unlink (gdb_template);
274         }
275
276         return TRUE;
277 }