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.
10 * See LICENSE for licensing information.
21 #ifdef HAVE_SYS_TIME_H
24 #ifdef HAVE_SYS_SYSCALL_H
25 #include <sys/syscall.h>
28 #include <mono/metadata/assembly.h>
29 #include <mono/metadata/loader.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/class.h>
32 #include <mono/metadata/object.h>
33 #include <mono/metadata/tokentype.h>
34 #include <mono/metadata/tabledefs.h>
35 #include <mono/metadata/threads.h>
36 #include <mono/metadata/appdomain.h>
37 #include <mono/metadata/debug-helpers.h>
38 #include <mono/io-layer/io-layer.h>
39 #include "mono/metadata/profiler.h"
40 #include <mono/metadata/profiler-private.h>
41 #include <mono/metadata/mono-config.h>
42 #include <mono/metadata/environment.h>
43 #include <mono/metadata/mono-debug.h>
44 #include <mono/metadata/gc-internal.h>
45 #include <mono/metadata/threads-types.h>
46 #include <mono/metadata/verify.h>
47 #include <mono/metadata/verify-internals.h>
48 #include <mono/metadata/mempool-internals.h>
49 #include <mono/metadata/attach.h>
50 #include <mono/utils/mono-math.h>
51 #include <mono/utils/mono-compiler.h>
52 #include <mono/utils/mono-counters.h>
53 #include <mono/utils/mono-logger-internal.h>
54 #include <mono/utils/mono-mmap.h>
55 #include <mono/utils/dtrace.h>
62 #include "debugger-agent.h"
64 #include "jit-icalls.h"
66 #if defined(__native_client__)
69 mono_runtime_setup_stat_profiler (void)
71 printf("WARNING: mono_runtime_setup_stat_profiler() called!\n");
76 mono_runtime_shutdown_stat_profiler (void)
82 SIG_HANDLER_SIGNATURE (mono_chain_signal
)
88 mono_runtime_install_handlers (void)
93 mono_runtime_shutdown_handlers (void)
98 mono_runtime_cleanup_handlers (void)
106 static GHashTable
*mono_saved_signal_handlers
= NULL
;
109 get_saved_signal_handler (int signo
)
111 if (mono_saved_signal_handlers
)
112 /* The hash is only modified during startup, so no need for locking */
113 return g_hash_table_lookup (mono_saved_signal_handlers
, GINT_TO_POINTER (signo
));
118 save_old_signal_handler (int signo
, struct sigaction
*old_action
)
120 struct sigaction
*handler_to_save
= g_malloc (sizeof (struct sigaction
));
122 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_CONFIG
,
123 "Saving old signal handler for signal %d.", signo
);
125 if (! (old_action
->sa_flags
& SA_SIGINFO
)) {
126 handler_to_save
->sa_handler
= old_action
->sa_handler
;
128 #ifdef MONO_ARCH_USE_SIGACTION
129 handler_to_save
->sa_sigaction
= old_action
->sa_sigaction
;
130 #endif /* MONO_ARCH_USE_SIGACTION */
132 handler_to_save
->sa_mask
= old_action
->sa_mask
;
133 handler_to_save
->sa_flags
= old_action
->sa_flags
;
135 if (!mono_saved_signal_handlers
)
136 mono_saved_signal_handlers
= g_hash_table_new (NULL
, NULL
);
137 g_hash_table_insert (mono_saved_signal_handlers
, GINT_TO_POINTER (signo
), handler_to_save
);
141 free_saved_sig_handler_func (gpointer key
, gpointer value
, gpointer user_data
)
147 free_saved_signal_handlers (void)
149 if (mono_saved_signal_handlers
) {
150 g_hash_table_foreach (mono_saved_signal_handlers
, free_saved_sig_handler_func
, NULL
);
151 g_hash_table_destroy (mono_saved_signal_handlers
);
152 mono_saved_signal_handlers
= NULL
;
159 * Call the original signal handler for the signal given by the arguments, which
160 * should be the same as for a signal handler. Returns TRUE if the original handler
161 * was called, false otherwise.
164 SIG_HANDLER_SIGNATURE (mono_chain_signal
)
167 struct sigaction
*saved_handler
= get_saved_signal_handler (signal
);
171 if (saved_handler
&& saved_handler
->sa_handler
) {
172 if (!(saved_handler
->sa_flags
& SA_SIGINFO
)) {
173 saved_handler
->sa_handler (signal
);
175 #ifdef MONO_ARCH_USE_SIGACTION
176 saved_handler
->sa_sigaction (signal
, info
, ctx
);
177 #endif /* MONO_ARCH_USE_SIGACTION */
185 SIG_HANDLER_SIGNATURE (sigabrt_signal_handler
)
187 MonoJitInfo
*ji
= NULL
;
190 if (mono_thread_internal_current ())
191 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx
));
193 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
195 mono_handle_native_sigsegv (SIGABRT
, ctx
);
200 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler
)
202 gboolean running_managed
;
204 MonoInternalThread
*thread
= mono_thread_internal_current ();
205 MonoDomain
*domain
= mono_domain_get ();
210 if (!thread
|| !domain
)
211 /* The thread might not have started up yet */
212 /* FIXME: Specify the synchronization with start_wrapper () in threads.c */
215 if (thread
->thread_dump_requested
) {
216 thread
->thread_dump_requested
= FALSE
;
218 mono_print_thread_dump (ctx
);
222 * This is an async signal, so the code below must not call anything which
223 * is not async safe. That includes the pthread locking functions. If we
224 * know that we interrupted managed code, then locking is safe.
227 * On OpenBSD, ctx can be NULL if we are interrupting poll ().
230 ji
= mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx
));
231 running_managed
= ji
!= NULL
;
233 if (mono_debugger_agent_thread_interrupt (ctx
, ji
))
236 running_managed
= FALSE
;
239 /* We can't do handler block checking from metadata since it requires doing
240 * a stack walk with context.
242 * FIXME add full-aot support.
244 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
245 if (!mono_aot_only
&& ctx
) {
246 MonoThreadUnwindState unwind_state
;
247 if (mono_thread_state_init_from_sigctx (&unwind_state
, ctx
)) {
248 if (mono_install_handler_block_guard (&unwind_state
)) {
250 /*Clear current thread from been wapi interrupted otherwise things can go south*/
251 wapi_clear_interruption ();
259 exc
= mono_thread_request_interruption (running_managed
);
263 mono_arch_handle_exception (ctx
, exc
, FALSE
);
266 #if defined(__i386__) || defined(__x86_64__)
267 #define FULL_STAT_PROFILER_BACKTRACE 1
268 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
269 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
270 #if MONO_ARCH_STACK_GROWS_UP
271 #define IS_BEFORE_ON_STACK <
272 #define IS_AFTER_ON_STACK >
274 #define IS_BEFORE_ON_STACK >
275 #define IS_AFTER_ON_STACK <
278 #define FULL_STAT_PROFILER_BACKTRACE 0
281 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
284 SIG_HANDLER_SIGNATURE (sigprof_signal_handler
)
286 if (mono_chain_signal (SIG_HANDLER_PARAMS
))
295 SIG_HANDLER_SIGNATURE (sigprof_signal_handler
)
297 int call_chain_depth
= mono_profiler_stat_get_call_chain_depth ();
298 MonoProfilerCallChainStrategy call_chain_strategy
= mono_profiler_stat_get_call_chain_strategy ();
301 if (call_chain_depth
== 0) {
302 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx
), ctx
);
304 MonoJitTlsData
*jit_tls
= TlsGetValue (mono_jit_tls_id
);
305 int current_frame_index
= 1;
306 MonoContext mono_context
;
307 guchar
*ips
[call_chain_depth
+ 1];
309 mono_arch_sigctx_to_monoctx (ctx
, &mono_context
);
310 ips
[0] = MONO_CONTEXT_GET_IP (&mono_context
);
312 if (jit_tls
!= NULL
) {
313 if (call_chain_strategy
== MONO_PROFILER_CALL_CHAIN_NATIVE
) {
314 #if FULL_STAT_PROFILER_BACKTRACE
315 guchar
*current_frame
;
316 guchar
*stack_bottom
;
319 stack_bottom
= jit_tls
->end_of_stack
;
320 stack_top
= MONO_CONTEXT_GET_SP (&mono_context
);
321 current_frame
= MONO_CONTEXT_GET_BP (&mono_context
);
323 while ((current_frame_index
<= call_chain_depth
) &&
324 (stack_bottom
IS_BEFORE_ON_STACK (guchar
*) current_frame
) &&
325 ((guchar
*) current_frame IS_BEFORE_ON_STACK stack_top
)) {
326 ips
[current_frame_index
] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame
);
327 current_frame_index
++;
328 stack_top
= current_frame
;
329 current_frame
= CURRENT_FRAME_GET_BASE_POINTER (current_frame
);
332 call_chain_strategy
= MONO_PROFILER_CALL_CHAIN_GLIBC
;
336 if (call_chain_strategy
== MONO_PROFILER_CALL_CHAIN_GLIBC
) {
337 #if GLIBC_PROFILER_BACKTRACE
338 current_frame_index
= backtrace ((void**) & ips
[1], call_chain_depth
);
340 call_chain_strategy
= MONO_PROFILER_CALL_CHAIN_MANAGED
;
344 if (call_chain_strategy
== MONO_PROFILER_CALL_CHAIN_MANAGED
) {
345 MonoDomain
*domain
= mono_domain_get ();
346 if (domain
!= NULL
) {
350 MonoContext new_mono_context
;
352 ji
= mono_find_jit_info (domain
, jit_tls
, &res
, NULL
, &mono_context
,
353 &new_mono_context
, NULL
, &lmf
, &native_offset
, NULL
);
354 while ((ji
!= NULL
) && (current_frame_index
<= call_chain_depth
)) {
355 ips
[current_frame_index
] = MONO_CONTEXT_GET_IP (&new_mono_context
);
356 current_frame_index
++;
357 mono_context
= new_mono_context
;
358 ji
= mono_find_jit_info (domain
, jit_tls
, &res
, NULL
, &mono_context
,
359 &new_mono_context
, NULL
, &lmf
, &native_offset
, NULL
);
365 mono_profiler_stat_call_chain (current_frame_index
, & ips
[0], ctx
);
368 mono_chain_signal (SIG_HANDLER_PARAMS
);
374 SIG_HANDLER_SIGNATURE (sigquit_signal_handler
)
380 /* We use this signal to start the attach agent too */
381 res
= mono_attach_start ();
385 if (mono_thread_info_new_interrupt_enabled ()) {
386 mono_threads_request_thread_dump ();
388 printf ("Full thread dump:\n");
390 mono_threads_request_thread_dump ();
393 * print_thread_dump () skips the current thread, since sending a signal
394 * to it would invoke the signal handler below the sigquit signal handler,
395 * and signal handlers don't create an lmf, so the stack walk could not
398 mono_print_thread_dump (ctx
);
401 mono_chain_signal (SIG_HANDLER_PARAMS
);
405 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler
)
407 gboolean enabled
= mono_trace_is_enabled ();
409 mono_trace_enable (!enabled
);
411 mono_chain_signal (SIG_HANDLER_PARAMS
);
415 add_signal_handler (int signo
, gpointer handler
)
418 struct sigaction previous_sa
;
420 #ifdef MONO_ARCH_USE_SIGACTION
421 sa
.sa_sigaction
= handler
;
422 sigemptyset (&sa
.sa_mask
);
423 sa
.sa_flags
= SA_SIGINFO
;
424 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
426 /*Apple likes to deliver SIGBUS for *0 */
428 if (signo
== SIGSEGV
|| signo
== SIGBUS
) {
430 if (signo
== SIGSEGV
) {
432 sa
.sa_flags
|= SA_ONSTACK
;
435 * libgc will crash when trying to do stack marking for threads which are on
436 * an altstack, so delay the suspend signal after the signal handler has
439 if (mono_gc_get_suspend_signal () != -1)
440 sigaddset (&sa
.sa_mask
, mono_gc_get_suspend_signal ());
443 if (signo
== SIGSEGV
) {
445 * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
449 sigemptyset (&block_mask
);
450 sigaddset (&sa
.sa_mask
, mono_thread_get_abort_signal ());
453 sa
.sa_handler
= handler
;
454 sigemptyset (&sa
.sa_mask
);
457 g_assert (sigaction (signo
, &sa
, &previous_sa
) != -1);
459 /* if there was already a handler in place for this signal, store it */
460 if (! (previous_sa
.sa_flags
& SA_SIGINFO
) &&
461 (SIG_DFL
== previous_sa
.sa_handler
)) {
462 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
464 if (mono_do_signal_chaining
)
465 save_old_signal_handler (signo
, &previous_sa
);
470 remove_signal_handler (int signo
)
473 struct sigaction
*saved_action
= get_saved_signal_handler (signo
);
476 sa
.sa_handler
= SIG_DFL
;
477 sigemptyset (&sa
.sa_mask
);
480 sigaction (signo
, &sa
, NULL
);
482 g_assert (sigaction (signo
, saved_action
, NULL
) != -1);
487 mono_runtime_posix_install_handlers (void)
492 if (mini_get_debug_options ()->handle_sigint
)
493 add_signal_handler (SIGINT
, mono_sigint_signal_handler
);
495 add_signal_handler (SIGFPE
, mono_sigfpe_signal_handler
);
496 add_signal_handler (SIGQUIT
, sigquit_signal_handler
);
497 add_signal_handler (SIGILL
, mono_sigill_signal_handler
);
498 add_signal_handler (SIGBUS
, mono_sigsegv_signal_handler
);
499 if (mono_jit_trace_calls
!= NULL
)
500 add_signal_handler (SIGUSR2
, sigusr2_signal_handler
);
502 if (!mono_thread_info_new_interrupt_enabled ())
503 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler
);
504 /* it seems to have become a common bug for some programs that run as parents
505 * of many processes to block signal delivery for real time signals.
506 * We try to detect and work around their breakage here.
508 sigemptyset (&signal_set
);
509 sigaddset (&signal_set
, mono_thread_get_abort_signal ());
510 sigprocmask (SIG_UNBLOCK
, &signal_set
, NULL
);
512 signal (SIGPIPE
, SIG_IGN
);
514 add_signal_handler (SIGABRT
, sigabrt_signal_handler
);
517 add_signal_handler (SIGSEGV
, mono_sigsegv_signal_handler
);
520 #ifndef PLATFORM_MACOSX
522 mono_runtime_install_handlers (void)
524 mono_runtime_posix_install_handlers ();
529 mono_runtime_cleanup_handlers (void)
531 if (mini_get_debug_options ()->handle_sigint
)
532 remove_signal_handler (SIGINT
);
534 remove_signal_handler (SIGFPE
);
535 remove_signal_handler (SIGQUIT
);
536 remove_signal_handler (SIGILL
);
537 remove_signal_handler (SIGBUS
);
538 if (mono_jit_trace_calls
!= NULL
)
539 remove_signal_handler (SIGUSR2
);
541 remove_signal_handler (mono_thread_get_abort_signal ());
543 remove_signal_handler (SIGABRT
);
545 remove_signal_handler (SIGSEGV
);
547 free_saved_signal_handlers ();
550 #ifdef HAVE_LINUX_RTC_H
551 #include <linux/rtc.h>
552 #include <sys/ioctl.h>
554 static int rtc_fd
= -1;
557 enable_rtc_timer (gboolean enable
)
560 flags
= fcntl (rtc_fd
, F_GETFL
);
569 if (fcntl (rtc_fd
, F_SETFL
, flags
) == -1) {
578 mono_runtime_shutdown_stat_profiler (void)
580 #ifdef HAVE_LINUX_RTC_H
582 enable_rtc_timer (FALSE
);
587 mono_runtime_setup_stat_profiler (void)
590 struct itimerval itval
;
591 static int inited
= 0;
592 #ifdef HAVE_LINUX_RTC_H
593 const char *rtc_freq
;
594 if (!inited
&& (rtc_freq
= g_getenv ("MONO_RTC"))) {
598 freq
= atoi (rtc_freq
);
601 rtc_fd
= open ("/dev/rtc", O_RDONLY
);
603 perror ("open /dev/rtc");
606 add_signal_handler (SIGPROF
, sigprof_signal_handler
);
607 if (ioctl (rtc_fd
, RTC_IRQP_SET
, freq
) == -1) {
608 perror ("set rtc freq");
611 if (ioctl (rtc_fd
, RTC_PIE_ON
, 0) == -1) {
612 perror ("start rtc");
615 if (fcntl (rtc_fd
, F_SETSIG
, SIGPROF
) == -1) {
619 if (fcntl (rtc_fd
, F_SETOWN
, getpid ()) == -1) {
623 enable_rtc_timer (TRUE
);
630 itval
.it_interval
.tv_usec
= 999;
631 itval
.it_interval
.tv_sec
= 0;
632 itval
.it_value
= itval
.it_interval
;
633 setitimer (ITIMER_PROF
, &itval
, NULL
);
637 add_signal_handler (SIGPROF
, sigprof_signal_handler
);
641 #if !defined(__APPLE__)
643 mono_runtime_syscall_fork ()
645 #if defined(SYS_fork)
646 return (pid_t
) syscall (SYS_fork
);
648 g_assert_not_reached ();
654 mono_gdb_render_native_backtraces ()
656 const char *argv
[9];
659 argv
[0] = g_find_program_in_path ("gdb");
660 if (argv
[0] == NULL
) {
665 sprintf (buf1
, "attach %ld", (long)getpid ());
668 argv
[4] = "info threads";
670 argv
[6] = "thread apply all bt";
671 argv
[7] = "--batch";
674 execv (argv
[0], (char**)argv
);
679 #endif /* __native_client__ */
681 #if !defined (__MACH__)
684 mono_thread_state_init_from_handle (MonoThreadUnwindState
*tctx
, MonoNativeThreadId thread_id
, MonoNativeThreadHandle thread_handle
)
686 g_error ("Posix systems don't support mono_thread_state_init_from_handle");