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 static GHashTable *mono_saved_signal_handlers = NULL;
69 get_saved_signal_handler (int signo)
71 if (mono_saved_signal_handlers)
72 /* The hash is only modified during startup, so no need for locking */
73 return g_hash_table_lookup (mono_saved_signal_handlers, GINT_TO_POINTER (signo));
78 save_old_signal_handler (int signo, struct sigaction *old_action)
80 struct sigaction *handler_to_save = g_malloc (sizeof (struct sigaction));
82 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_CONFIG,
83 "Saving old signal handler for signal %d.", signo);
85 if (! (old_action->sa_flags & SA_SIGINFO)) {
86 handler_to_save->sa_handler = old_action->sa_handler;
88 #ifdef MONO_ARCH_USE_SIGACTION
89 handler_to_save->sa_sigaction = old_action->sa_sigaction;
90 #endif /* MONO_ARCH_USE_SIGACTION */
92 handler_to_save->sa_mask = old_action->sa_mask;
93 handler_to_save->sa_flags = old_action->sa_flags;
95 if (!mono_saved_signal_handlers)
96 mono_saved_signal_handlers = g_hash_table_new (NULL, NULL);
97 g_hash_table_insert (mono_saved_signal_handlers, GINT_TO_POINTER (signo), handler_to_save);
101 free_saved_sig_handler_func (gpointer key, gpointer value, gpointer user_data)
107 free_saved_signal_handlers (void)
109 if (mono_saved_signal_handlers) {
110 g_hash_table_foreach (mono_saved_signal_handlers, free_saved_sig_handler_func, NULL);
111 g_hash_table_destroy (mono_saved_signal_handlers);
112 mono_saved_signal_handlers = NULL;
119 * Call the original signal handler for the signal given by the arguments, which
120 * should be the same as for a signal handler. Returns TRUE if the original handler
121 * was called, false otherwise.
124 SIG_HANDLER_SIGNATURE (mono_chain_signal)
127 struct sigaction *saved_handler = get_saved_signal_handler (signal);
132 if (!(saved_handler->sa_flags & SA_SIGINFO)) {
133 saved_handler->sa_handler (signal);
135 #ifdef MONO_ARCH_USE_SIGACTION
136 saved_handler->sa_sigaction (signal, info, ctx);
137 #endif /* MONO_ARCH_USE_SIGACTION */
145 SIG_HANDLER_SIGNATURE (sigabrt_signal_handler)
147 MonoJitInfo *ji = NULL;
150 if (mono_thread_internal_current ())
151 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
153 if (mono_chain_signal (SIG_HANDLER_PARAMS))
155 mono_handle_native_sigsegv (SIGABRT, ctx);
160 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
163 gboolean running_managed;
165 MonoInternalThread *thread = mono_thread_internal_current ();
166 MonoDomain *domain = mono_domain_get ();
171 if (!thread || !domain)
172 /* The thread might not have started up yet */
173 /* FIXME: Specify the synchronization with start_wrapper () in threads.c */
176 if (thread->thread_dump_requested) {
177 thread->thread_dump_requested = FALSE;
179 mono_print_thread_dump (ctx);
183 * This is an async signal, so the code below must not call anything which
184 * is not async safe. That includes the pthread locking functions. If we
185 * know that we interrupted managed code, then locking is safe.
188 * On OpenBSD, ctx can be NULL if we are interrupting poll ().
191 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
192 running_managed = ji != NULL;
194 if (mono_debugger_agent_thread_interrupt (ctx, ji))
197 running_managed = FALSE;
200 /* We can't do handler block checking from metadata since it requires doing
201 * a stack walk with context.
203 * FIXME add full-aot support.
205 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
206 if (!mono_aot_only && ctx) {
207 mono_arch_sigctx_to_monoctx (ctx, &mctx);
208 if (mono_install_handler_block_guard (thread, &mctx)) {
214 exc = mono_thread_request_interruption (running_managed);
218 mono_arch_handle_exception (ctx, exc, FALSE);
221 #if defined(__i386__) || defined(__x86_64__)
222 #define FULL_STAT_PROFILER_BACKTRACE 1
223 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
224 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
225 #if MONO_ARCH_STACK_GROWS_UP
226 #define IS_BEFORE_ON_STACK <
227 #define IS_AFTER_ON_STACK >
229 #define IS_BEFORE_ON_STACK >
230 #define IS_AFTER_ON_STACK <
233 #define FULL_STAT_PROFILER_BACKTRACE 0
236 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
239 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
241 if (mono_chain_signal (SIG_HANDLER_PARAMS))
250 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
252 int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
253 MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
256 if (call_chain_depth == 0) {
257 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
259 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
260 int current_frame_index = 1;
261 MonoContext mono_context;
262 guchar *ips [call_chain_depth + 1];
264 mono_arch_sigctx_to_monoctx (ctx, &mono_context);
265 ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
267 if (jit_tls != NULL) {
268 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_NATIVE) {
269 #if FULL_STAT_PROFILER_BACKTRACE
270 guchar *current_frame;
271 guchar *stack_bottom;
274 stack_bottom = jit_tls->end_of_stack;
275 stack_top = MONO_CONTEXT_GET_SP (&mono_context);
276 current_frame = MONO_CONTEXT_GET_BP (&mono_context);
278 while ((current_frame_index <= call_chain_depth) &&
279 (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
280 ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
281 ips [current_frame_index] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
282 current_frame_index ++;
283 stack_top = current_frame;
284 current_frame = CURRENT_FRAME_GET_BASE_POINTER (current_frame);
287 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_GLIBC;
291 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_GLIBC) {
292 #if GLIBC_PROFILER_BACKTRACE
293 current_frame_index = backtrace ((void**) & ips [1], call_chain_depth);
295 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_MANAGED;
299 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_MANAGED) {
300 MonoDomain *domain = mono_domain_get ();
301 if (domain != NULL) {
305 MonoContext new_mono_context;
307 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
308 &new_mono_context, NULL, &lmf, &native_offset, NULL);
309 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
310 ips [current_frame_index] = MONO_CONTEXT_GET_IP (&new_mono_context);
311 current_frame_index ++;
312 mono_context = new_mono_context;
313 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
314 &new_mono_context, NULL, &lmf, &native_offset, NULL);
320 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
323 mono_chain_signal (SIG_HANDLER_PARAMS);
329 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
335 /* We use this signal to start the attach agent too */
336 res = mono_attach_start ();
340 printf ("Full thread dump:\n");
342 mono_threads_request_thread_dump ();
345 * print_thread_dump () skips the current thread, since sending a signal
346 * to it would invoke the signal handler below the sigquit signal handler,
347 * and signal handlers don't create an lmf, so the stack walk could not
350 mono_print_thread_dump (ctx);
352 mono_chain_signal (SIG_HANDLER_PARAMS);
356 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
358 gboolean enabled = mono_trace_is_enabled ();
360 mono_trace_enable (!enabled);
362 mono_chain_signal (SIG_HANDLER_PARAMS);
366 add_signal_handler (int signo, gpointer handler)
369 struct sigaction previous_sa;
371 #ifdef MONO_ARCH_USE_SIGACTION
372 sa.sa_sigaction = handler;
373 sigemptyset (&sa.sa_mask);
374 sa.sa_flags = SA_SIGINFO;
375 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
376 if (signo == SIGSEGV) {
377 sa.sa_flags |= SA_ONSTACK;
380 * libgc will crash when trying to do stack marking for threads which are on
381 * an altstack, so delay the suspend signal after the signal handler has
384 if (mono_gc_get_suspend_signal () != -1)
385 sigaddset (&sa.sa_mask, mono_gc_get_suspend_signal ());
388 if (signo == SIGSEGV) {
390 * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
394 sigemptyset (&block_mask);
395 sigaddset (&sa.sa_mask, mono_thread_get_abort_signal ());
398 sa.sa_handler = handler;
399 sigemptyset (&sa.sa_mask);
402 g_assert (sigaction (signo, &sa, &previous_sa) != -1);
404 /* if there was already a handler in place for this signal, store it */
405 if (! (previous_sa.sa_flags & SA_SIGINFO) &&
406 (SIG_DFL == previous_sa.sa_handler)) {
407 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
409 if (mono_do_signal_chaining)
410 save_old_signal_handler (signo, &previous_sa);
415 remove_signal_handler (int signo)
418 struct sigaction *saved_action = get_saved_signal_handler (signo);
421 sa.sa_handler = SIG_DFL;
422 sigemptyset (&sa.sa_mask);
425 sigaction (signo, &sa, NULL);
427 g_assert (sigaction (signo, saved_action, NULL) != -1);
432 mono_runtime_posix_install_handlers (void)
437 if (mini_get_debug_options ()->handle_sigint)
438 add_signal_handler (SIGINT, mono_sigint_signal_handler);
440 add_signal_handler (SIGFPE, mono_sigfpe_signal_handler);
441 add_signal_handler (SIGQUIT, sigquit_signal_handler);
442 add_signal_handler (SIGILL, mono_sigill_signal_handler);
443 add_signal_handler (SIGBUS, mono_sigsegv_signal_handler);
444 if (mono_jit_trace_calls != NULL)
445 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
447 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
448 /* it seems to have become a common bug for some programs that run as parents
449 * of many processes to block signal delivery for real time signals.
450 * We try to detect and work around their breakage here.
452 sigemptyset (&signal_set);
453 sigaddset (&signal_set, mono_thread_get_abort_signal ());
454 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
456 signal (SIGPIPE, SIG_IGN);
458 add_signal_handler (SIGABRT, sigabrt_signal_handler);
461 add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler);
464 #ifndef PLATFORM_MACOSX
466 mono_runtime_install_handlers (void)
468 mono_runtime_posix_install_handlers ();
473 mono_runtime_cleanup_handlers (void)
475 if (mini_get_debug_options ()->handle_sigint)
476 remove_signal_handler (SIGINT);
478 remove_signal_handler (SIGFPE);
479 remove_signal_handler (SIGQUIT);
480 remove_signal_handler (SIGILL);
481 remove_signal_handler (SIGBUS);
482 if (mono_jit_trace_calls != NULL)
483 remove_signal_handler (SIGUSR2);
485 remove_signal_handler (mono_thread_get_abort_signal ());
487 remove_signal_handler (SIGABRT);
489 remove_signal_handler (SIGSEGV);
491 free_saved_signal_handlers ();
494 #ifdef HAVE_LINUX_RTC_H
495 #include <linux/rtc.h>
496 #include <sys/ioctl.h>
498 static int rtc_fd = -1;
501 enable_rtc_timer (gboolean enable)
504 flags = fcntl (rtc_fd, F_GETFL);
513 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
522 mono_runtime_shutdown_stat_profiler (void)
524 #ifdef HAVE_LINUX_RTC_H
526 enable_rtc_timer (FALSE);
531 mono_runtime_setup_stat_profiler (void)
534 struct itimerval itval;
535 static int inited = 0;
536 #ifdef HAVE_LINUX_RTC_H
537 const char *rtc_freq;
538 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
542 freq = atoi (rtc_freq);
545 rtc_fd = open ("/dev/rtc", O_RDONLY);
547 perror ("open /dev/rtc");
550 add_signal_handler (SIGPROF, sigprof_signal_handler);
551 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
552 perror ("set rtc freq");
555 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
556 perror ("start rtc");
559 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
563 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
567 enable_rtc_timer (TRUE);
574 itval.it_interval.tv_usec = 999;
575 itval.it_interval.tv_sec = 0;
576 itval.it_value = itval.it_interval;
577 setitimer (ITIMER_PROF, &itval, NULL);
581 add_signal_handler (SIGPROF, sigprof_signal_handler);
585 #if !defined(__APPLE__)
587 mono_runtime_syscall_fork ()
589 #if defined(SYS_fork)
590 return (pid_t) syscall (SYS_fork);
592 g_assert_not_reached ();
598 mono_gdb_render_native_backtraces ()
600 const char *argv [9];
603 argv [0] = g_find_program_in_path ("gdb");
604 if (argv [0] == NULL) {
609 sprintf (buf1, "attach %ld", (long)getpid ());
612 argv [4] = "info threads";
614 argv [6] = "thread apply all bt";
615 argv [7] = "--batch";
618 execv (argv [0], (char**)argv);