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