2 * mini-posix.c: POSIX signal handling support for Mono.
5 * Mono Team (mono-list@lists.ximian.com)
7 * Copyright 2001-2003 Ximian, Inc.
8 * Copyright 2003-2008 Ximian, Inc.
9 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
11 * See LICENSE for licensing information.
22 #ifdef HAVE_SYS_TIME_H
25 #ifdef HAVE_SYS_SYSCALL_H
26 #include <sys/syscall.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-internal.h>
55 #include <mono/utils/mono-mmap.h>
56 #include <mono/utils/dtrace.h>
63 #include "debugger-agent.h"
65 #include "jit-icalls.h"
67 #if defined(__native_client__)
70 mono_runtime_setup_stat_profiler (void)
72 printf("WARNING: mono_runtime_setup_stat_profiler() called!\n");
77 mono_runtime_shutdown_stat_profiler (void)
83 SIG_HANDLER_SIGNATURE (mono_chain_signal)
89 mono_runtime_install_handlers (void)
94 mono_runtime_shutdown_handlers (void)
99 mono_runtime_cleanup_handlers (void)
104 mono_runtime_syscall_fork (void)
106 g_assert_not_reached();
111 mono_gdb_render_native_backtraces (pid_t crashed_pid)
117 static GHashTable *mono_saved_signal_handlers = NULL;
120 get_saved_signal_handler (int signo)
122 if (mono_saved_signal_handlers)
123 /* The hash is only modified during startup, so no need for locking */
124 return g_hash_table_lookup (mono_saved_signal_handlers, GINT_TO_POINTER (signo));
129 save_old_signal_handler (int signo, struct sigaction *old_action)
131 struct sigaction *handler_to_save = g_malloc (sizeof (struct sigaction));
133 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_CONFIG,
134 "Saving old signal handler for signal %d.", signo);
136 if (! (old_action->sa_flags & SA_SIGINFO)) {
137 handler_to_save->sa_handler = old_action->sa_handler;
139 #ifdef MONO_ARCH_USE_SIGACTION
140 handler_to_save->sa_sigaction = old_action->sa_sigaction;
141 #endif /* MONO_ARCH_USE_SIGACTION */
143 handler_to_save->sa_mask = old_action->sa_mask;
144 handler_to_save->sa_flags = old_action->sa_flags;
146 if (!mono_saved_signal_handlers)
147 mono_saved_signal_handlers = g_hash_table_new (NULL, NULL);
148 g_hash_table_insert (mono_saved_signal_handlers, GINT_TO_POINTER (signo), handler_to_save);
152 free_saved_sig_handler_func (gpointer key, gpointer value, gpointer user_data)
158 free_saved_signal_handlers (void)
160 if (mono_saved_signal_handlers) {
161 g_hash_table_foreach (mono_saved_signal_handlers, free_saved_sig_handler_func, NULL);
162 g_hash_table_destroy (mono_saved_signal_handlers);
163 mono_saved_signal_handlers = NULL;
170 * Call the original signal handler for the signal given by the arguments, which
171 * should be the same as for a signal handler. Returns TRUE if the original handler
172 * was called, false otherwise.
175 SIG_HANDLER_SIGNATURE (mono_chain_signal)
178 struct sigaction *saved_handler = get_saved_signal_handler (signal);
182 if (saved_handler && saved_handler->sa_handler) {
183 if (!(saved_handler->sa_flags & SA_SIGINFO)) {
184 saved_handler->sa_handler (signal);
186 #ifdef MONO_ARCH_USE_SIGACTION
187 saved_handler->sa_sigaction (signal, info, ctx);
188 #endif /* MONO_ARCH_USE_SIGACTION */
196 SIG_HANDLER_SIGNATURE (sigabrt_signal_handler)
198 MonoJitInfo *ji = NULL;
201 if (mono_thread_internal_current ())
202 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
204 if (mono_chain_signal (SIG_HANDLER_PARAMS))
206 mono_handle_native_sigsegv (SIGABRT, ctx);
211 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
213 gboolean running_managed;
215 MonoInternalThread *thread = mono_thread_internal_current ();
216 MonoDomain *domain = mono_domain_get ();
221 if (!thread || !domain) {
222 /* The thread might not have started up yet */
223 /* FIXME: Specify the synchronization with start_wrapper () in threads.c */
224 mono_debugger_agent_thread_interrupt (ctx, NULL);
228 if (thread->ignore_next_signal) {
229 thread->ignore_next_signal = FALSE;
233 if (thread->thread_dump_requested) {
234 thread->thread_dump_requested = FALSE;
236 mono_print_thread_dump (ctx);
240 * This is an async signal, so the code below must not call anything which
241 * is not async safe. That includes the pthread locking functions. If we
242 * know that we interrupted managed code, then locking is safe.
245 * On OpenBSD, ctx can be NULL if we are interrupting poll ().
248 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
249 running_managed = ji != NULL;
251 if (mono_debugger_agent_thread_interrupt (ctx, ji))
254 running_managed = FALSE;
257 /* We can't do handler block checking from metadata since it requires doing
258 * a stack walk with context.
260 * FIXME add full-aot support.
262 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
263 if (!mono_aot_only && ctx) {
264 MonoThreadUnwindState unwind_state;
265 if (mono_thread_state_init_from_sigctx (&unwind_state, ctx)) {
266 if (mono_install_handler_block_guard (&unwind_state)) {
268 /*Clear current thread from been wapi interrupted otherwise things can go south*/
269 wapi_clear_interruption ();
277 exc = mono_thread_request_interruption (running_managed);
281 mono_arch_handle_exception (ctx, exc);
284 #if defined(__i386__) || defined(__x86_64__)
285 #define FULL_STAT_PROFILER_BACKTRACE 1
286 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
287 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
288 #if MONO_ARCH_STACK_GROWS_UP
289 #define IS_BEFORE_ON_STACK <
290 #define IS_AFTER_ON_STACK >
292 #define IS_BEFORE_ON_STACK >
293 #define IS_AFTER_ON_STACK <
296 #define FULL_STAT_PROFILER_BACKTRACE 0
299 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
302 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
304 if (mono_chain_signal (SIG_HANDLER_PARAMS))
313 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
315 int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
316 MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
319 if (call_chain_depth == 0) {
320 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
322 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
323 int current_frame_index = 1;
324 MonoContext mono_context;
325 guchar *ips [call_chain_depth + 1];
327 mono_arch_sigctx_to_monoctx (ctx, &mono_context);
328 ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
330 if (jit_tls != NULL) {
331 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_NATIVE) {
332 #if FULL_STAT_PROFILER_BACKTRACE
333 guchar *current_frame;
334 guchar *stack_bottom;
337 stack_bottom = jit_tls->end_of_stack;
338 stack_top = MONO_CONTEXT_GET_SP (&mono_context);
339 current_frame = MONO_CONTEXT_GET_BP (&mono_context);
341 while ((current_frame_index <= call_chain_depth) &&
342 (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
343 ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
344 ips [current_frame_index] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
345 current_frame_index ++;
346 stack_top = current_frame;
347 current_frame = CURRENT_FRAME_GET_BASE_POINTER (current_frame);
350 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_GLIBC;
354 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_GLIBC) {
355 #if GLIBC_PROFILER_BACKTRACE
356 current_frame_index = backtrace ((void**) & ips [1], call_chain_depth);
358 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_MANAGED;
362 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_MANAGED) {
363 MonoDomain *domain = mono_domain_get ();
364 if (domain != NULL) {
368 MonoContext new_mono_context;
370 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
371 &new_mono_context, NULL, &lmf, &native_offset, NULL);
372 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
373 ips [current_frame_index] = MONO_CONTEXT_GET_IP (&new_mono_context);
374 current_frame_index ++;
375 mono_context = new_mono_context;
376 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
377 &new_mono_context, NULL, &lmf, &native_offset, NULL);
383 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
386 mono_chain_signal (SIG_HANDLER_PARAMS);
392 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
398 /* We use this signal to start the attach agent too */
399 res = mono_attach_start ();
403 if (mono_thread_info_new_interrupt_enabled ()) {
404 mono_threads_request_thread_dump ();
406 printf ("Full thread dump:\n");
408 mono_threads_request_thread_dump ();
411 * print_thread_dump () skips the current thread, since sending a signal
412 * to it would invoke the signal handler below the sigquit signal handler,
413 * and signal handlers don't create an lmf, so the stack walk could not
416 mono_print_thread_dump (ctx);
419 mono_chain_signal (SIG_HANDLER_PARAMS);
423 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
425 gboolean enabled = mono_trace_is_enabled ();
427 mono_trace_enable (!enabled);
429 mono_chain_signal (SIG_HANDLER_PARAMS);
433 add_signal_handler (int signo, gpointer handler)
436 struct sigaction previous_sa;
438 #ifdef MONO_ARCH_USE_SIGACTION
439 sa.sa_sigaction = handler;
440 sigemptyset (&sa.sa_mask);
441 sa.sa_flags = SA_SIGINFO;
442 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
444 /*Apple likes to deliver SIGBUS for *0 */
446 if (signo == SIGSEGV || signo == SIGBUS) {
448 if (signo == SIGSEGV) {
450 sa.sa_flags |= SA_ONSTACK;
453 * libgc will crash when trying to do stack marking for threads which are on
454 * an altstack, so delay the suspend signal after the signal handler has
457 if (mono_gc_get_suspend_signal () != -1)
458 sigaddset (&sa.sa_mask, mono_gc_get_suspend_signal ());
461 if (signo == SIGSEGV) {
463 * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
467 sigemptyset (&block_mask);
468 sigaddset (&sa.sa_mask, mono_thread_get_abort_signal ());
471 sa.sa_handler = handler;
472 sigemptyset (&sa.sa_mask);
475 g_assert (sigaction (signo, &sa, &previous_sa) != -1);
477 /* if there was already a handler in place for this signal, store it */
478 if (! (previous_sa.sa_flags & SA_SIGINFO) &&
479 (SIG_DFL == previous_sa.sa_handler)) {
480 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
482 if (mono_do_signal_chaining)
483 save_old_signal_handler (signo, &previous_sa);
488 remove_signal_handler (int signo)
491 struct sigaction *saved_action = get_saved_signal_handler (signo);
494 sa.sa_handler = SIG_DFL;
495 sigemptyset (&sa.sa_mask);
498 sigaction (signo, &sa, NULL);
500 g_assert (sigaction (signo, saved_action, NULL) != -1);
505 mono_runtime_posix_install_handlers (void)
510 if (mini_get_debug_options ()->handle_sigint)
511 add_signal_handler (SIGINT, mono_sigint_signal_handler);
513 add_signal_handler (SIGFPE, mono_sigfpe_signal_handler);
514 add_signal_handler (SIGQUIT, sigquit_signal_handler);
515 add_signal_handler (SIGILL, mono_sigill_signal_handler);
516 add_signal_handler (SIGBUS, mono_sigsegv_signal_handler);
517 if (mono_jit_trace_calls != NULL)
518 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
520 if (!mono_thread_info_new_interrupt_enabled ())
521 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
522 /* it seems to have become a common bug for some programs that run as parents
523 * of many processes to block signal delivery for real time signals.
524 * We try to detect and work around their breakage here.
526 sigemptyset (&signal_set);
527 sigaddset (&signal_set, mono_thread_get_abort_signal ());
528 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
530 signal (SIGPIPE, SIG_IGN);
532 add_signal_handler (SIGABRT, sigabrt_signal_handler);
535 add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler);
538 #ifndef PLATFORM_MACOSX
540 mono_runtime_install_handlers (void)
542 mono_runtime_posix_install_handlers ();
547 mono_runtime_cleanup_handlers (void)
549 if (mini_get_debug_options ()->handle_sigint)
550 remove_signal_handler (SIGINT);
552 remove_signal_handler (SIGFPE);
553 remove_signal_handler (SIGQUIT);
554 remove_signal_handler (SIGILL);
555 remove_signal_handler (SIGBUS);
556 if (mono_jit_trace_calls != NULL)
557 remove_signal_handler (SIGUSR2);
559 remove_signal_handler (mono_thread_get_abort_signal ());
561 remove_signal_handler (SIGABRT);
563 remove_signal_handler (SIGSEGV);
565 free_saved_signal_handlers ();
568 #ifdef HAVE_LINUX_RTC_H
569 #include <linux/rtc.h>
570 #include <sys/ioctl.h>
572 static int rtc_fd = -1;
575 enable_rtc_timer (gboolean enable)
578 flags = fcntl (rtc_fd, F_GETFL);
587 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
596 mono_runtime_shutdown_stat_profiler (void)
598 #ifdef HAVE_LINUX_RTC_H
600 enable_rtc_timer (FALSE);
605 mono_runtime_setup_stat_profiler (void)
608 struct itimerval itval;
609 static int inited = 0;
610 #ifdef HAVE_LINUX_RTC_H
611 const char *rtc_freq;
612 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
616 freq = atoi (rtc_freq);
619 rtc_fd = open ("/dev/rtc", O_RDONLY);
621 perror ("open /dev/rtc");
624 add_signal_handler (SIGPROF, sigprof_signal_handler);
625 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
626 perror ("set rtc freq");
629 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
630 perror ("start rtc");
633 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
637 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
641 enable_rtc_timer (TRUE);
648 itval.it_interval.tv_usec = 999;
649 itval.it_interval.tv_sec = 0;
650 itval.it_value = itval.it_interval;
651 setitimer (ITIMER_PROF, &itval, NULL);
655 add_signal_handler (SIGPROF, sigprof_signal_handler);
659 #if !defined(__APPLE__)
661 mono_runtime_syscall_fork ()
663 #if defined(SYS_fork)
664 return (pid_t) syscall (SYS_fork);
666 g_assert_not_reached ();
672 mono_gdb_render_native_backtraces (pid_t crashed_pid)
674 const char *argv [9];
677 argv [0] = g_find_program_in_path ("gdb");
678 if (argv [0] == NULL) {
683 sprintf (buf1, "attach %ld", (long) crashed_pid);
686 argv [4] = "info threads";
688 argv [6] = "thread apply all bt";
689 argv [7] = "--batch";
692 execv (argv [0], (char**)argv);
695 #endif /* __native_client__ */
697 #if !defined (__MACH__)
700 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoNativeThreadId thread_id, MonoNativeThreadHandle thread_handle)
702 g_error ("Posix systems don't support mono_thread_state_init_from_handle");