2 * This is the Darwin incarnation of OS-dependent routines. See also
7 * This software is part of the SBCL system. See the README file for
10 * This software is derived from the CMU CL system, which was
11 * written at Carnegie Mellon University and released into the
12 * public domain. The software is in the public domain and is
13 * provided with absolutely no warranty. See the COPYING and CREDITS
14 * files for more information.
23 #include <mach-o/dyld.h>
27 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
28 #include <mach/mach.h>
32 os_get_runtime_executable_path(int external)
34 char path[PATH_MAX + 1];
35 uint32_t size = sizeof(path);
37 if (_NSGetExecutablePath(path, &size) == -1)
42 return copied_string(path);
45 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
47 /* exc_server handles mach exception messages from the kernel and
48 * calls catch exception raise. We use the system-provided
49 * mach_msg_server, which, I assume, calls exc_server in a loop.
52 extern boolean_t exc_server();
55 mach_exception_handler(void *port)
57 mach_msg_server(exc_server, 2048, (mach_port_t) port, 0);
58 /* mach_msg_server should never return, but it should dispatch mach
59 * exceptions to our catch_exception_raise function
61 lose("mach_msg_server returned");
64 /* Sets up the thread that will listen for mach exceptions. note that
65 the exception handlers will be run on this thread. This is
66 different from the BSD-style signal handling situation in which the
67 signal handlers run in the relevant thread directly. */
69 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
70 mach_port_t current_mach_task = MACH_PORT_NULL;
73 setup_mach_exception_handling_thread()
76 pthread_t mach_exception_handling_thread = NULL;
79 current_mach_task = mach_task_self();
81 /* allocate a mach_port for this process */
82 ret = mach_port_allocate(current_mach_task,
83 MACH_PORT_RIGHT_PORT_SET,
84 &mach_exception_handler_port_set);
86 /* create the thread that will receive the mach exceptions */
88 FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
90 pthread_attr_init(&attr);
91 pthread_create(&mach_exception_handling_thread,
93 mach_exception_handler,
94 (void*) mach_exception_handler_port_set);
95 pthread_attr_destroy(&attr);
97 return mach_exception_handling_thread;
100 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
101 exception port (which is being listened to do by the mach
102 exception handling thread). */
104 mach_thread_init(mach_port_t thread_exception_port)
107 mach_port_t current_mach_thread;
109 /* allocate a named port for the thread */
110 FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
111 ret = mach_port_allocate_name(current_mach_task,
112 MACH_PORT_RIGHT_RECEIVE,
113 thread_exception_port);
115 lose("mach_port_allocate_name failed with return_code %d\n", ret);
118 /* establish the right for the thread_exception_port to send messages */
119 ret = mach_port_insert_right(current_mach_task,
120 thread_exception_port,
121 thread_exception_port,
122 MACH_MSG_TYPE_MAKE_SEND);
124 lose("mach_port_insert_right failed with return_code %d\n", ret);
127 current_mach_thread = mach_thread_self();
128 ret = thread_set_exception_ports(current_mach_thread,
129 EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
130 thread_exception_port,
134 lose("thread_set_exception_ports failed with return_code %d\n", ret);
137 ret = mach_port_deallocate (current_mach_task, current_mach_thread);
139 lose("mach_port_deallocate failed with return_code %d\n", ret);
142 ret = mach_port_move_member(current_mach_task,
143 thread_exception_port,
144 mach_exception_handler_port_set);
146 lose("mach_port_move_member failed with return_code %d\n", ret);
153 setup_mach_exceptions() {
154 setup_mach_exception_handling_thread();
155 mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
162 setup_mach_exceptions();