2009-11-23 Zoltan Varga <vargaz@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 #ifdef HAVE_VALGRIND_MEMCHECK_H
26 #include <valgrind/memcheck.h>
27 #endif
28
29 #include <mono/metadata/assembly.h>
30 #include <mono/metadata/loader.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/class.h>
33 #include <mono/metadata/object.h>
34 #include <mono/metadata/tokentype.h>
35 #include <mono/metadata/tabledefs.h>
36 #include <mono/metadata/threads.h>
37 #include <mono/metadata/appdomain.h>
38 #include <mono/metadata/debug-helpers.h>
39 #include <mono/io-layer/io-layer.h>
40 #include "mono/metadata/profiler.h"
41 #include <mono/metadata/profiler-private.h>
42 #include <mono/metadata/mono-config.h>
43 #include <mono/metadata/environment.h>
44 #include <mono/metadata/mono-debug.h>
45 #include <mono/metadata/gc-internal.h>
46 #include <mono/metadata/threads-types.h>
47 #include <mono/metadata/verify.h>
48 #include <mono/metadata/verify-internals.h>
49 #include <mono/metadata/mempool-internals.h>
50 #include <mono/metadata/attach.h>
51 #include <mono/utils/mono-math.h>
52 #include <mono/utils/mono-compiler.h>
53 #include <mono/utils/mono-counters.h>
54 #include <mono/utils/mono-logger.h>
55 #include <mono/utils/mono-mmap.h>
56 #include <mono/utils/dtrace.h>
57
58 #include "mini.h"
59 #include <string.h>
60 #include <ctype.h>
61 #include "trace.h"
62 #include "version.h"
63
64 #include "jit-icalls.h"
65
66 /* MacOS includes */
67 #include <mach/mach.h>
68 #include <mach/mach_error.h>
69 #include <mach/exception.h>
70 #include <mach/task.h>
71 #include <pthread.h>
72
73 #ifdef HAVE_SGEN_GC
74 #undef pthread_create
75 #undef pthread_join
76 #undef pthread_detach
77 #endif
78
79 /*
80  * This code disables the CrashReporter of MacOS X by installing
81  * a dummy Mach exception handler.
82  */
83
84 /*
85  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
86  */
87 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
88
89 /*
90  * The exception message
91  */
92 typedef struct {
93         mach_msg_base_t msg;  /* common mach message header */
94         char payload [1024];  /* opaque */
95 } mach_exception_msg_t;
96
97 /* The exception port */
98 static mach_port_t mach_exception_port = VM_MAP_NULL;
99
100 /*
101  * Implicitly called by exc_server. Must be public.
102  *
103  * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
104  */
105 kern_return_t
106 catch_exception_raise (
107         mach_port_t exception_port,
108         mach_port_t thread,
109         mach_port_t task,
110         exception_type_t exception,
111         exception_data_t code,
112         mach_msg_type_number_t code_count)
113 {
114         /* consume the exception */
115         return KERN_FAILURE;
116 }
117
118 /*
119  * Exception thread handler.
120  */
121 static
122 void *
123 mach_exception_thread (void *arg)
124 {
125         for (;;) {
126                 mach_exception_msg_t request;
127                 mach_exception_msg_t reply;
128                 mach_msg_return_t result;
129
130                 /* receive from "mach_exception_port" */
131                 result = mach_msg (&request.msg.header,
132                                    MACH_RCV_MSG | MACH_RCV_LARGE,
133                                    0,
134                                    sizeof (request),
135                                    mach_exception_port,
136                                    MACH_MSG_TIMEOUT_NONE,
137                                    MACH_PORT_NULL);
138
139                 g_assert (result == MACH_MSG_SUCCESS);
140
141                 /* dispatch to catch_exception_raise () */
142                 exc_server (&request.msg.header, &reply.msg.header);
143
144                 /* send back to sender */
145                 result = mach_msg (&reply.msg.header,
146                                    MACH_SEND_MSG,
147                                    reply.msg.header.msgh_size,
148                                    0,
149                                    MACH_PORT_NULL,
150                                    MACH_MSG_TIMEOUT_NONE,
151                                    MACH_PORT_NULL);
152
153                 g_assert (result == MACH_MSG_SUCCESS);
154         }
155         return NULL;
156 }
157
158 static void
159 macosx_register_exception_handler ()
160 {
161         mach_port_t task;
162         pthread_attr_t attr;
163         pthread_t thread;
164
165         if (mach_exception_port != VM_MAP_NULL)
166                 return;
167
168         task = mach_task_self ();
169
170         /* create the "mach_exception_port" with send & receive rights */
171         g_assert (mach_port_allocate (task, MACH_PORT_RIGHT_RECEIVE,
172                                       &mach_exception_port) == KERN_SUCCESS);
173         g_assert (mach_port_insert_right (task, mach_exception_port, mach_exception_port,
174                                           MACH_MSG_TYPE_MAKE_SEND) == KERN_SUCCESS);
175
176         /* create the exception handler thread */
177         g_assert (!pthread_attr_init (&attr));
178         g_assert (!pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED));
179         g_assert (!pthread_create (&thread, &attr, mach_exception_thread, NULL));
180         pthread_attr_destroy (&attr);
181
182         /*
183          * register "mach_exception_port" as a receiver for the
184          * EXC_BAD_ACCESS exception
185          *
186          * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
187          */
188         g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
189                                             mach_exception_port,
190                                             EXCEPTION_DEFAULT,
191                                             MACHINE_THREAD_STATE) == KERN_SUCCESS);
192 }
193
194 void
195 mono_runtime_install_handlers (void)
196 {
197         macosx_register_exception_handler ();
198         mono_runtime_posix_install_handlers ();
199 }
200
201 pid_t
202 mono_runtime_syscall_fork ()
203 {
204 #if defined(__i386__)
205         /* Apple's fork syscall returns a regpair in EAX:EDX.
206          *  EAX == pid of caller always
207          *  EDX == 0 for parent, 1 for child
208          */             
209         register_t eax;
210         register_t edx;
211         pid_t pid;
212
213         __asm__  __volatile__ (
214                 "mov $0x2, %%eax;"
215                 "int $0x80;"
216                 "mov %%eax, %0;"
217                 "mov %%edx, %1;"
218                 : "=m" (eax), "=m" (edx));
219
220         if (edx == 0) {
221                 pid = eax;
222         } else if (edx == 1) {
223                 pid = 0;
224         } else {
225                 g_assert_not_reached ();
226         }
227
228         return pid;
229 #else
230         g_assert_not_reached ();
231 #endif
232 }
233
234 gboolean
235 mono_gdb_render_native_backtraces ()
236 {
237         const char *argv [5];
238         char gdb_template [] = "/tmp/mono-gdb-commands.XXXXXX";
239
240         argv [0] = g_find_program_in_path ("gdb");
241         if (argv [0] == NULL) {
242                 return FALSE;
243         }
244
245         if (mkstemp (gdb_template) != -1) {
246                 FILE *gdb_commands = fopen (gdb_template, "w");
247
248                 fprintf (gdb_commands, "attach %ld\n", (long) getpid ());
249                 fprintf (gdb_commands, "info threads\n");
250                 fprintf (gdb_commands, "thread apply all bt\n");
251
252                 fflush (gdb_commands);
253                 fclose (gdb_commands);
254
255                 argv [1] = "-batch";
256                 argv [2] = "-x";
257                 argv [3] = gdb_template;
258                 argv [4] = 0;
259
260                 execv (argv [0], (char**)argv);
261
262                 unlink (gdb_template);
263         }
264
265         return TRUE;
266 }