05adf306e535eeed61ec45e33cc408150b0aea42
[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-2011 Novell, Inc (http://www.novell.com)
9  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10  *
11  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12  */
13 #include <config.h>
14 #include <signal.h>
15 #ifdef HAVE_ALLOCA_H
16 #include <alloca.h>
17 #endif
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #include <math.h>
22 #ifdef HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25
26 #include <mono/metadata/assembly.h>
27 #include <mono/metadata/loader.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/class.h>
30 #include <mono/metadata/object.h>
31 #include <mono/metadata/tokentype.h>
32 #include <mono/metadata/tabledefs.h>
33 #include <mono/metadata/threads.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/io-layer/io-layer.h>
37 #include "mono/metadata/profiler.h"
38 #include <mono/metadata/profiler-private.h>
39 #include <mono/metadata/mono-config.h>
40 #include <mono/metadata/environment.h>
41 #include <mono/metadata/mono-debug.h>
42 #include <mono/metadata/threads-types.h>
43 #include <mono/metadata/verify.h>
44 #include <mono/metadata/verify-internals.h>
45 #include <mono/metadata/mempool-internals.h>
46 #include <mono/metadata/attach.h>
47 #include <mono/metadata/gc-internals.h>
48 #include <mono/utils/mono-math.h>
49 #include <mono/utils/mono-compiler.h>
50 #include <mono/utils/mono-counters.h>
51 #include <mono/utils/mono-logger-internals.h>
52 #include <mono/utils/mono-mmap.h>
53 #include <mono/utils/dtrace.h>
54
55 #include "mini.h"
56 #include <string.h>
57 #include <ctype.h>
58 #include "trace.h"
59 #include "version.h"
60
61 #include "jit-icalls.h"
62
63 /* MacOS includes */
64 #include <mach/mach.h>
65 #include <mach/mach_error.h>
66 #include <mach/exception.h>
67 #include <mach/task.h>
68 #include <pthread.h>
69 #include <dlfcn.h>
70 #include <AvailabilityMacros.h>
71
72 /* This is #define'd by Boehm GC to _GC_dlopen. */
73 #undef dlopen
74
75 void* dlopen(const char* path, int mode);
76
77 void
78 mono_runtime_install_handlers (void)
79 {
80         mono_runtime_posix_install_handlers ();
81
82         /* Snow Leopard has a horrible bug: http://openradar.appspot.com/7209349
83          * This causes obscure SIGTRAP's for any application that comes across this built on
84          * Snow Leopard.  This is a horrible hack to ensure that the private __CFInitialize
85          * is run on the main thread, so that we don't get SIGTRAPs later
86          */
87 #if defined (__APPLE__) && (defined (__i386__) || defined (__x86_64__))
88         {
89                 void *handle = dlopen ("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
90                 if (handle == NULL)
91                         return;
92
93                 dlclose (handle);
94         }
95 #endif
96 }
97
98 pid_t
99 mono_runtime_syscall_fork ()
100 {
101 #ifdef HAVE_FORK
102         return (pid_t) fork ();
103 #else
104         g_assert_not_reached ();
105 #endif
106 }
107
108 void
109 mono_gdb_render_native_backtraces (pid_t crashed_pid)
110 {
111 #ifdef HAVE_EXECV
112         const char *argv [6];
113         char template [] = "/tmp/mono-gdb-commands.XXXXXX";
114         FILE *commands;
115         gboolean using_lldb = FALSE;
116
117         using_lldb = TRUE;
118
119         argv [0] = g_find_program_in_path ("gdb");
120         if (argv [0])
121                 using_lldb = FALSE;
122
123         if (using_lldb)
124                 argv [0] = g_find_program_in_path ("lldb");
125
126         if (argv [0] == NULL)
127                 return;
128
129         if (mkstemp (template) == -1)
130                 return;
131
132         commands = fopen (template, "w");
133         if (using_lldb) {
134                 fprintf (commands, "process attach --pid %ld\n", (long) crashed_pid);
135                 fprintf (commands, "thread list\n");
136                 fprintf (commands, "thread backtrace all\n");
137                 fprintf (commands, "detach\n");
138                 fprintf (commands, "quit\n");
139                 argv [1] = "--source";
140                 argv [2] = template;
141                 argv [3] = 0;
142                 
143         } else {
144                 fprintf (commands, "attach %ld\n", (long) crashed_pid);
145                 fprintf (commands, "info threads\n");
146                 fprintf (commands, " t a a info thread\n");
147                 fprintf (commands, "thread apply all bt\n");
148                 argv [1] = "-batch";
149                 argv [2] = "-x";
150                 argv [3] = template;
151                 argv [4] = "-nx";
152                 argv [5] = 0;
153         }
154         fflush (commands);
155         fclose (commands);
156
157         fclose (stdin);
158
159         execv (argv [0], (char**)argv);
160         unlink (template);
161 #else
162         fprintf (stderr, "mono_gdb_render_native_backtraces not supported on this platform\n");
163 #endif // HAVE_EXECV
164 }
165
166 gboolean
167 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
168 {
169         kern_return_t ret;
170         mach_msg_type_number_t num_state, num_fpstate;
171         thread_state_t state, fpstate;
172         ucontext_t ctx;
173         mcontext_t mctx;
174         MonoJitTlsData *jit_tls;
175         void *domain;
176         MonoLMF *lmf = NULL;
177         gpointer *addr;
178
179         g_assert (info);
180         /*Zero enough state to make sure the caller doesn't confuse itself*/
181         tctx->valid = FALSE;
182         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = NULL;
183         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = NULL;
184         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = NULL;
185
186         state = (thread_state_t) alloca (mono_mach_arch_get_thread_state_size ());
187         fpstate = (thread_state_t) alloca (mono_mach_arch_get_thread_fpstate_size ());
188         mctx = (mcontext_t) alloca (mono_mach_arch_get_mcontext_size ());
189
190         do {
191                 ret = mono_mach_arch_get_thread_states (info->native_handle, state, &num_state, fpstate, &num_fpstate);
192         } while (ret == KERN_ABORTED);
193         if (ret != KERN_SUCCESS)
194                 return FALSE;
195
196         mono_mach_arch_thread_states_to_mcontext (state, fpstate, mctx);
197         ctx.uc_mcontext = mctx;
198
199         mono_sigctx_to_monoctx (&ctx, &tctx->ctx);
200
201         /* mono_set_jit_tls () sets this */
202         jit_tls = mono_thread_info_tls_get (info, TLS_KEY_JIT_TLS);
203         /* SET_APPDOMAIN () sets this */
204         domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
205
206         /*Thread already started to cleanup, can no longer capture unwind state*/
207         if (!jit_tls || !domain)
208                 return FALSE;
209
210         /*
211          * The current LMF address is kept in a separate TLS variable, and its hard to read its value without
212          * arch-specific code. But the address of the TLS variable is stored in another TLS variable which
213          * can be accessed through MonoThreadInfo.
214          */
215         /* mono_set_lmf_addr () sets this */
216         addr = mono_thread_info_tls_get (info, TLS_KEY_LMF_ADDR);
217         if (addr)
218                 lmf = *addr;
219
220
221         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = domain;
222         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = jit_tls;
223         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = lmf;
224         tctx->valid = TRUE;
225
226         return TRUE;
227 }