Make sure x86 ATOMIC_CAS doesn't overwrite its own operands.
[mono-debugger.git] / mono / mini / mini-posix.c
blobe6068657758a031c7388d0bdeb0ae065b9dafed9
1 /*
2 * mini-posix.c: POSIX signal handling support for Mono.
4 * Authors:
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.
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
25 #include <mono/metadata/assembly.h>
26 #include <mono/metadata/loader.h>
27 #include <mono/metadata/tabledefs.h>
28 #include <mono/metadata/class.h>
29 #include <mono/metadata/object.h>
30 #include <mono/metadata/tokentype.h>
31 #include <mono/metadata/tabledefs.h>
32 #include <mono/metadata/threads.h>
33 #include <mono/metadata/appdomain.h>
34 #include <mono/metadata/debug-helpers.h>
35 #include <mono/io-layer/io-layer.h>
36 #include "mono/metadata/profiler.h"
37 #include <mono/metadata/profiler-private.h>
38 #include <mono/metadata/mono-config.h>
39 #include <mono/metadata/environment.h>
40 #include <mono/metadata/mono-debug.h>
41 #include <mono/metadata/gc-internal.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/utils/mono-math.h>
48 #include <mono/utils/mono-compiler.h>
49 #include <mono/utils/mono-counters.h>
50 #include <mono/utils/mono-logger.h>
51 #include <mono/utils/mono-mmap.h>
52 #include <mono/utils/dtrace.h>
54 #include "mini.h"
55 #include <string.h>
56 #include <ctype.h>
57 #include "trace.h"
58 #include "version.h"
60 #include "jit-icalls.h"
62 static GHashTable *mono_saved_signal_handlers = NULL;
64 static gpointer
65 get_saved_signal_handler (int signo)
67 if (mono_saved_signal_handlers)
68 /* The hash is only modified during startup, so no need for locking */
69 return g_hash_table_lookup (mono_saved_signal_handlers, GINT_TO_POINTER (signo));
70 return NULL;
73 static void
74 save_old_signal_handler (int signo, struct sigaction *old_action)
76 struct sigaction *handler_to_save = g_malloc (sizeof (struct sigaction));
78 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_CONFIG,
79 "Saving old signal handler for signal %d.", signo);
81 if (! (old_action->sa_flags & SA_SIGINFO)) {
82 handler_to_save->sa_handler = old_action->sa_handler;
83 } else {
84 #ifdef MONO_ARCH_USE_SIGACTION
85 handler_to_save->sa_sigaction = old_action->sa_sigaction;
86 #endif /* MONO_ARCH_USE_SIGACTION */
88 handler_to_save->sa_mask = old_action->sa_mask;
89 handler_to_save->sa_flags = old_action->sa_flags;
91 if (!mono_saved_signal_handlers)
92 mono_saved_signal_handlers = g_hash_table_new (NULL, NULL);
93 g_hash_table_insert (mono_saved_signal_handlers, GINT_TO_POINTER (signo), handler_to_save);
96 static void
97 free_saved_sig_handler_func (gpointer key, gpointer value, gpointer user_data)
99 g_free (value);
102 static void
103 free_saved_signal_handlers (void)
105 if (mono_saved_signal_handlers) {
106 g_hash_table_foreach (mono_saved_signal_handlers, free_saved_sig_handler_func, NULL);
107 g_hash_table_destroy (mono_saved_signal_handlers);
108 mono_saved_signal_handlers = NULL;
113 * mono_chain_signal:
115 * Call the original signal handler for the signal given by the arguments, which
116 * should be the same as for a signal handler. Returns TRUE if the original handler
117 * was called, false otherwise.
119 gboolean
120 SIG_HANDLER_SIGNATURE (mono_chain_signal)
122 int signal = _dummy;
123 struct sigaction *saved_handler = get_saved_signal_handler (signal);
125 GET_CONTEXT;
127 if (saved_handler) {
128 if (!(saved_handler->sa_flags & SA_SIGINFO)) {
129 saved_handler->sa_handler (signal);
130 } else {
131 #ifdef MONO_ARCH_USE_SIGACTION
132 saved_handler->sa_sigaction (signal, info, ctx);
133 #endif /* MONO_ARCH_USE_SIGACTION */
135 return TRUE;
137 return FALSE;
140 static void
141 SIG_HANDLER_SIGNATURE (sigabrt_signal_handler)
143 MonoJitInfo *ji;
144 GET_CONTEXT;
146 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
147 if (!ji) {
148 if (mono_chain_signal (SIG_HANDLER_PARAMS))
149 return;
150 mono_handle_native_sigsegv (SIGABRT, ctx);
154 static void
155 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
157 gboolean running_managed;
158 MonoException *exc;
159 MonoThread *thread = mono_thread_current ();
160 MonoDomain *domain = mono_domain_get ();
161 void *ji;
163 GET_CONTEXT;
165 if (!thread || !domain)
166 /* The thread might not have started up yet */
167 /* FIXME: Specify the synchronization with start_wrapper () in threads.c */
168 return;
170 if (thread->thread_dump_requested) {
171 thread->thread_dump_requested = FALSE;
173 mono_print_thread_dump (ctx);
177 * FIXME:
178 * This is an async signal, so the code below must not call anything which
179 * is not async safe. That includes the pthread locking functions. If we
180 * know that we interrupted managed code, then locking is safe.
182 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
183 running_managed = ji != NULL;
185 exc = mono_thread_request_interruption (running_managed);
186 if (!exc)
187 return;
189 mono_arch_handle_exception (ctx, exc, FALSE);
192 #if defined(__i386__) || defined(__x86_64__)
193 #define FULL_STAT_PROFILER_BACKTRACE 1
194 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
195 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
196 #if MONO_ARCH_STACK_GROWS_UP
197 #define IS_BEFORE_ON_STACK <
198 #define IS_AFTER_ON_STACK >
199 #else
200 #define IS_BEFORE_ON_STACK >
201 #define IS_AFTER_ON_STACK <
202 #endif
203 #else
204 #define FULL_STAT_PROFILER_BACKTRACE 0
205 #endif
207 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
209 static void
210 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
212 if (mono_chain_signal (SIG_HANDLER_PARAMS))
213 return;
215 NOT_IMPLEMENTED;
218 #else
220 static void
221 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
223 int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
224 GET_CONTEXT;
226 if (call_chain_depth == 0) {
227 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
228 } else {
229 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
230 int current_frame_index = 1;
231 MonoContext mono_context;
232 #if FULL_STAT_PROFILER_BACKTRACE
233 guchar *current_frame;
234 guchar *stack_bottom;
235 guchar *stack_top;
236 #else
237 MonoDomain *domain;
238 #endif
239 guchar *ips [call_chain_depth + 1];
241 mono_arch_sigctx_to_monoctx (ctx, &mono_context);
242 ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
244 if (jit_tls != NULL) {
245 #if FULL_STAT_PROFILER_BACKTRACE
246 stack_bottom = jit_tls->end_of_stack;
247 stack_top = MONO_CONTEXT_GET_SP (&mono_context);
248 current_frame = MONO_CONTEXT_GET_BP (&mono_context);
250 while ((current_frame_index <= call_chain_depth) &&
251 (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
252 ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
253 ips [current_frame_index] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
254 current_frame_index ++;
255 stack_top = current_frame;
256 current_frame = CURRENT_FRAME_GET_BASE_POINTER (current_frame);
258 #else
259 domain = mono_domain_get ();
260 if (domain != NULL) {
261 MonoLMF *lmf = NULL;
262 MonoJitInfo *ji;
263 MonoJitInfo res;
264 MonoContext new_mono_context;
265 int native_offset;
266 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
267 &new_mono_context, NULL, &lmf, &native_offset, NULL);
268 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
269 ips [current_frame_index] = MONO_CONTEXT_GET_IP (&new_mono_context);
270 current_frame_index ++;
271 mono_context = new_mono_context;
272 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
273 &new_mono_context, NULL, &lmf, &native_offset, NULL);
276 #endif
279 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
282 mono_chain_signal (SIG_HANDLER_PARAMS);
285 #endif
287 static void
288 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
290 gboolean res;
292 GET_CONTEXT;
294 /* We use this signal to start the attach agent too */
295 res = mono_attach_start ();
296 if (res)
297 return;
299 printf ("Full thread dump:\n");
301 mono_threads_request_thread_dump ();
304 * print_thread_dump () skips the current thread, since sending a signal
305 * to it would invoke the signal handler below the sigquit signal handler,
306 * and signal handlers don't create an lmf, so the stack walk could not
307 * be performed.
309 mono_print_thread_dump (ctx);
311 mono_chain_signal (SIG_HANDLER_PARAMS);
314 static void
315 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
317 gboolean enabled = mono_trace_is_enabled ();
319 mono_trace_enable (!enabled);
321 mono_chain_signal (SIG_HANDLER_PARAMS);
324 static void
325 add_signal_handler (int signo, gpointer handler)
327 struct sigaction sa;
328 struct sigaction previous_sa;
330 #ifdef MONO_ARCH_USE_SIGACTION
331 sa.sa_sigaction = handler;
332 sigemptyset (&sa.sa_mask);
333 sa.sa_flags = SA_SIGINFO;
334 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
335 if (signo == SIGSEGV)
336 sa.sa_flags |= SA_ONSTACK;
337 #endif
338 #else
339 sa.sa_handler = handler;
340 sigemptyset (&sa.sa_mask);
341 sa.sa_flags = 0;
342 #endif
343 g_assert (sigaction (signo, &sa, &previous_sa) != -1);
345 /* if there was already a handler in place for this signal, store it */
346 if (! (previous_sa.sa_flags & SA_SIGINFO) &&
347 (SIG_DFL == previous_sa.sa_handler)) {
348 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
349 } else {
350 if (mono_do_signal_chaining)
351 save_old_signal_handler (signo, &previous_sa);
355 static void
356 remove_signal_handler (int signo)
358 struct sigaction sa;
359 struct sigaction *saved_action = get_saved_signal_handler (signo);
361 if (!saved_action) {
362 sa.sa_handler = SIG_DFL;
363 sigemptyset (&sa.sa_mask);
364 sa.sa_flags = 0;
366 sigaction (signo, &sa, NULL);
367 } else {
368 g_assert (sigaction (signo, saved_action, NULL) != -1);
372 void
373 mono_runtime_posix_install_handlers (void)
376 sigset_t signal_set;
378 if (mini_get_debug_options ()->handle_sigint)
379 add_signal_handler (SIGINT, mono_sigint_signal_handler);
381 add_signal_handler (SIGFPE, mono_sigfpe_signal_handler);
382 add_signal_handler (SIGQUIT, sigquit_signal_handler);
383 add_signal_handler (SIGILL, mono_sigill_signal_handler);
384 add_signal_handler (SIGBUS, mono_sigsegv_signal_handler);
385 if (mono_jit_trace_calls != NULL)
386 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
388 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
389 /* it seems to have become a common bug for some programs that run as parents
390 * of many processes to block signal delivery for real time signals.
391 * We try to detect and work around their breakage here.
393 sigemptyset (&signal_set);
394 sigaddset (&signal_set, mono_thread_get_abort_signal ());
395 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
397 signal (SIGPIPE, SIG_IGN);
399 add_signal_handler (SIGABRT, sigabrt_signal_handler);
401 /* catch SIGSEGV */
402 add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler);
405 #ifndef PLATFORM_MACOSX
406 void
407 mono_runtime_install_handlers (void)
409 mono_runtime_posix_install_handlers ();
411 #endif
413 void
414 mono_runtime_cleanup_handlers (void)
416 if (mini_get_debug_options ()->handle_sigint)
417 remove_signal_handler (SIGINT);
419 remove_signal_handler (SIGFPE);
420 remove_signal_handler (SIGQUIT);
421 remove_signal_handler (SIGILL);
422 remove_signal_handler (SIGBUS);
423 if (mono_jit_trace_calls != NULL)
424 remove_signal_handler (SIGUSR2);
426 remove_signal_handler (mono_thread_get_abort_signal ());
428 remove_signal_handler (SIGABRT);
430 remove_signal_handler (SIGSEGV);
432 free_saved_signal_handlers ();
435 #ifdef HAVE_LINUX_RTC_H
436 #include <linux/rtc.h>
437 #include <sys/ioctl.h>
438 #include <fcntl.h>
439 static int rtc_fd = -1;
441 static int
442 enable_rtc_timer (gboolean enable)
444 int flags;
445 flags = fcntl (rtc_fd, F_GETFL);
446 if (flags < 0) {
447 perror ("getflags");
448 return 0;
450 if (enable)
451 flags |= FASYNC;
452 else
453 flags &= ~FASYNC;
454 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
455 perror ("setflags");
456 return 0;
458 return 1;
460 #endif
462 void
463 mono_runtime_shutdown_stat_profiler (void)
465 #ifdef HAVE_LINUX_RTC_H
466 if (rtc_fd >= 0)
467 enable_rtc_timer (FALSE);
468 #endif
471 void
472 mono_runtime_setup_stat_profiler (void)
474 #ifdef ITIMER_PROF
475 struct itimerval itval;
476 static int inited = 0;
477 #ifdef HAVE_LINUX_RTC_H
478 const char *rtc_freq;
479 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
480 int freq = 0;
481 inited = 1;
482 if (*rtc_freq)
483 freq = atoi (rtc_freq);
484 if (!freq)
485 freq = 1024;
486 rtc_fd = open ("/dev/rtc", O_RDONLY);
487 if (rtc_fd == -1) {
488 perror ("open /dev/rtc");
489 return;
491 add_signal_handler (SIGPROF, sigprof_signal_handler);
492 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
493 perror ("set rtc freq");
494 return;
496 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
497 perror ("start rtc");
498 return;
500 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
501 perror ("setsig");
502 return;
504 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
505 perror ("setown");
506 return;
508 enable_rtc_timer (TRUE);
509 return;
511 if (rtc_fd >= 0)
512 return;
513 #endif
515 itval.it_interval.tv_usec = 999;
516 itval.it_interval.tv_sec = 0;
517 itval.it_value = itval.it_interval;
518 setitimer (ITIMER_PROF, &itval, NULL);
519 if (inited)
520 return;
521 inited = 1;
522 add_signal_handler (SIGPROF, sigprof_signal_handler);
523 #endif