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 #ifdef HAVE_VALGRIND_MEMCHECK_H
26 #include <valgrind/memcheck.h>
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>
64 #include "jit-icalls.h"
67 #include <mach/mach.h>
68 #include <mach/mach_error.h>
69 #include <mach/exception.h>
70 #include <mach/task.h>
80 * This code disables the CrashReporter of MacOS X by installing
81 * a dummy Mach exception handler.
85 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
87 extern boolean_t exc_server (mach_msg_header_t *request_msg, mach_msg_header_t *reply_msg);
90 * The exception message
93 mach_msg_base_t msg; /* common mach message header */
94 char payload [1024]; /* opaque */
95 } mach_exception_msg_t;
97 /* The exception port */
98 static mach_port_t mach_exception_port = VM_MAP_NULL;
101 * Implicitly called by exc_server. Must be public.
103 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
106 catch_exception_raise (
107 mach_port_t exception_port,
110 exception_type_t exception,
111 exception_data_t code,
112 mach_msg_type_number_t code_count)
114 /* consume the exception */
119 * Exception thread handler.
123 mach_exception_thread (void *arg)
126 mach_exception_msg_t request;
127 mach_exception_msg_t reply;
128 mach_msg_return_t result;
130 /* receive from "mach_exception_port" */
131 result = mach_msg (&request.msg.header,
132 MACH_RCV_MSG | MACH_RCV_LARGE,
136 MACH_MSG_TIMEOUT_NONE,
139 g_assert (result == MACH_MSG_SUCCESS);
141 /* dispatch to catch_exception_raise () */
142 exc_server (&request.msg.header, &reply.msg.header);
144 /* send back to sender */
145 result = mach_msg (&reply.msg.header,
147 reply.msg.header.msgh_size,
150 MACH_MSG_TIMEOUT_NONE,
153 g_assert (result == MACH_MSG_SUCCESS);
159 macosx_register_exception_handler ()
165 if (mach_exception_port != VM_MAP_NULL)
168 task = mach_task_self ();
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);
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);
183 * register "mach_exception_port" as a receiver for the
184 * EXC_BAD_ACCESS exception
186 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
188 g_assert (task_set_exception_ports (task, EXC_MASK_BAD_ACCESS,
191 MACHINE_THREAD_STATE) == KERN_SUCCESS);
195 mono_runtime_install_handlers (void)
197 macosx_register_exception_handler ();
198 mono_runtime_posix_install_handlers ();
202 mono_runtime_syscall_fork ()
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
213 __asm__ __volatile__ (
218 : "=m" (eax), "=m" (edx));
222 } else if (edx == 1) {
225 g_assert_not_reached ();
230 g_assert_not_reached ();
235 mono_gdb_render_native_backtraces ()
237 const char *argv [5];
238 char gdb_template [] = "/tmp/mono-gdb-commands.XXXXXX";
240 argv [0] = g_find_program_in_path ("gdb");
241 if (argv [0] == NULL) {
245 if (mkstemp (gdb_template) != -1) {
246 FILE *gdb_commands = fopen (gdb_template, "w");
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");
252 fflush (gdb_commands);
253 fclose (gdb_commands);
257 argv [3] = gdb_template;
260 execv (argv [0], (char**)argv);
262 unlink (gdb_template);