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>
74 * This code disables the CrashReporter of MacOS X by installing
75 * a dummy Mach exception handler.
79 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/exc_server.html
81 extern boolean_t
exc_server (mach_msg_header_t
*request_msg
, mach_msg_header_t
*reply_msg
);
84 * The exception message
87 mach_msg_base_t msg
; /* common mach message header */
88 char payload
[1024]; /* opaque */
89 } mach_exception_msg_t
;
91 /* The exception port */
92 static mach_port_t mach_exception_port
= VM_MAP_NULL
;
95 * Implicitly called by exc_server. Must be public.
97 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/catch_exception_raise.html
100 catch_exception_raise (
101 mach_port_t exception_port
,
104 exception_type_t exception
,
105 exception_data_t code
,
106 mach_msg_type_number_t code_count
)
108 /* consume the exception */
113 * Exception thread handler.
117 mach_exception_thread (void *arg
)
120 mach_exception_msg_t request
;
121 mach_exception_msg_t reply
;
122 mach_msg_return_t result
;
124 /* receive from "mach_exception_port" */
125 result
= mach_msg (&request
.msg
.header
,
126 MACH_RCV_MSG
| MACH_RCV_LARGE
,
130 MACH_MSG_TIMEOUT_NONE
,
133 g_assert (result
== MACH_MSG_SUCCESS
);
135 /* dispatch to catch_exception_raise () */
136 exc_server (&request
.msg
.header
, &reply
.msg
.header
);
138 /* send back to sender */
139 result
= mach_msg (&reply
.msg
.header
,
141 reply
.msg
.header
.msgh_size
,
144 MACH_MSG_TIMEOUT_NONE
,
147 g_assert (result
== MACH_MSG_SUCCESS
);
153 macosx_register_exception_handler ()
159 if (mach_exception_port
!= VM_MAP_NULL
)
162 task
= mach_task_self ();
164 /* create the "mach_exception_port" with send & receive rights */
165 g_assert (mach_port_allocate (task
, MACH_PORT_RIGHT_RECEIVE
,
166 &mach_exception_port
) == KERN_SUCCESS
);
167 g_assert (mach_port_insert_right (task
, mach_exception_port
, mach_exception_port
,
168 MACH_MSG_TYPE_MAKE_SEND
) == KERN_SUCCESS
);
170 /* create the exception handler thread */
171 g_assert (!pthread_attr_init (&attr
));
172 g_assert (!pthread_attr_setdetachstate (&attr
, PTHREAD_CREATE_DETACHED
));
173 g_assert (!pthread_create (&thread
, &attr
, mach_exception_thread
, NULL
));
174 pthread_attr_destroy (&attr
);
177 * register "mach_exception_port" as a receiver for the
178 * EXC_BAD_ACCESS exception
180 * http://darwinsource.opendarwin.org/10.4.3/xnu-792.6.22/osfmk/man/task_set_exception_ports.html
182 g_assert (task_set_exception_ports (task
, EXC_MASK_BAD_ACCESS
,
185 MACHINE_THREAD_STATE
) == KERN_SUCCESS
);
189 mono_runtime_install_handlers (void)
191 #if !defined(__arm__)
192 macosx_register_exception_handler ();
194 mono_runtime_posix_install_handlers ();