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.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)
162 gboolean running_managed;
164 MonoInternalThread *thread = mono_thread_internal_current ();
165 MonoDomain *domain = mono_domain_get ();
170 if (!thread || !domain)
171 /* The thread might not have started up yet */
172 /* FIXME: Specify the synchronization with start_wrapper () in threads.c */
175 if (thread->thread_dump_requested) {
176 thread->thread_dump_requested = FALSE;
178 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.
187 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
188 running_managed = ji != NULL;
190 if (mono_debugger_agent_thread_interrupt (ctx, ji))
193 exc = mono_thread_request_interruption (running_managed);
197 mono_arch_handle_exception (ctx, exc, FALSE);
200 #if defined(__i386__) || defined(__x86_64__)
201 #define FULL_STAT_PROFILER_BACKTRACE 1
202 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
203 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
204 #if MONO_ARCH_STACK_GROWS_UP
205 #define IS_BEFORE_ON_STACK <
206 #define IS_AFTER_ON_STACK >
208 #define IS_BEFORE_ON_STACK >
209 #define IS_AFTER_ON_STACK <
212 #define FULL_STAT_PROFILER_BACKTRACE 0
215 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
218 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
220 if (mono_chain_signal (SIG_HANDLER_PARAMS))
229 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
231 int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
232 MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
235 if (call_chain_depth == 0) {
236 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
238 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
239 int current_frame_index = 1;
240 MonoContext mono_context;
241 guchar *ips [call_chain_depth + 1];
243 mono_arch_sigctx_to_monoctx (ctx, &mono_context);
244 ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
246 if (jit_tls != NULL) {
247 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_NATIVE) {
248 #if FULL_STAT_PROFILER_BACKTRACE
249 guchar *current_frame;
250 guchar *stack_bottom;
253 stack_bottom = jit_tls->end_of_stack;
254 stack_top = MONO_CONTEXT_GET_SP (&mono_context);
255 current_frame = MONO_CONTEXT_GET_BP (&mono_context);
257 while ((current_frame_index <= call_chain_depth) &&
258 (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
259 ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
260 ips [current_frame_index] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
261 current_frame_index ++;
262 stack_top = current_frame;
263 current_frame = CURRENT_FRAME_GET_BASE_POINTER (current_frame);
266 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_GLIBC;
270 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_GLIBC) {
271 #if GLIBC_PROFILER_BACKTRACE
272 current_frame_index = backtrace ((void**) & ips [1], call_chain_depth);
274 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_MANAGED;
278 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_MANAGED) {
279 MonoDomain *domain = mono_domain_get ();
280 if (domain != NULL) {
284 MonoContext new_mono_context;
286 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
287 &new_mono_context, NULL, &lmf, &native_offset, NULL);
288 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
289 ips [current_frame_index] = MONO_CONTEXT_GET_IP (&new_mono_context);
290 current_frame_index ++;
291 mono_context = new_mono_context;
292 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
293 &new_mono_context, NULL, &lmf, &native_offset, NULL);
299 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
302 mono_chain_signal (SIG_HANDLER_PARAMS);
308 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
314 /* We use this signal to start the attach agent too */
315 res = mono_attach_start ();
319 printf ("Full thread dump:\n");
321 mono_threads_request_thread_dump ();
324 * print_thread_dump () skips the current thread, since sending a signal
325 * to it would invoke the signal handler below the sigquit signal handler,
326 * and signal handlers don't create an lmf, so the stack walk could not
329 mono_print_thread_dump (ctx);
331 mono_chain_signal (SIG_HANDLER_PARAMS);
335 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
337 gboolean enabled = mono_trace_is_enabled ();
339 mono_trace_enable (!enabled);
341 mono_chain_signal (SIG_HANDLER_PARAMS);
345 add_signal_handler (int signo, gpointer handler)
348 struct sigaction previous_sa;
350 #ifdef MONO_ARCH_USE_SIGACTION
351 sa.sa_sigaction = handler;
352 sigemptyset (&sa.sa_mask);
353 sa.sa_flags = SA_SIGINFO;
354 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
355 if (signo == SIGSEGV) {
356 sa.sa_flags |= SA_ONSTACK;
359 * libgc will crash when trying to do stack marking for threads which are on
360 * an altstack, so delay the suspend signal after the signal handler has
363 if (mono_gc_get_suspend_signal () != -1)
364 sigaddset (&sa.sa_mask, mono_gc_get_suspend_signal ());
367 if (signo == SIGSEGV) {
369 * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
373 sigemptyset (&block_mask);
374 sigaddset (&sa.sa_mask, mono_thread_get_abort_signal ());
377 sa.sa_handler = handler;
378 sigemptyset (&sa.sa_mask);
381 g_assert (sigaction (signo, &sa, &previous_sa) != -1);
383 /* if there was already a handler in place for this signal, store it */
384 if (! (previous_sa.sa_flags & SA_SIGINFO) &&
385 (SIG_DFL == previous_sa.sa_handler)) {
386 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
388 if (mono_do_signal_chaining)
389 save_old_signal_handler (signo, &previous_sa);
394 remove_signal_handler (int signo)
397 struct sigaction *saved_action = get_saved_signal_handler (signo);
400 sa.sa_handler = SIG_DFL;
401 sigemptyset (&sa.sa_mask);
404 sigaction (signo, &sa, NULL);
406 g_assert (sigaction (signo, saved_action, NULL) != -1);
411 mono_runtime_posix_install_handlers (void)
416 if (mini_get_debug_options ()->handle_sigint)
417 add_signal_handler (SIGINT, mono_sigint_signal_handler);
419 add_signal_handler (SIGFPE, mono_sigfpe_signal_handler);
420 add_signal_handler (SIGQUIT, sigquit_signal_handler);
421 add_signal_handler (SIGILL, mono_sigill_signal_handler);
422 add_signal_handler (SIGBUS, mono_sigsegv_signal_handler);
423 if (mono_jit_trace_calls != NULL)
424 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
426 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
427 /* it seems to have become a common bug for some programs that run as parents
428 * of many processes to block signal delivery for real time signals.
429 * We try to detect and work around their breakage here.
431 sigemptyset (&signal_set);
432 sigaddset (&signal_set, mono_thread_get_abort_signal ());
433 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
435 signal (SIGPIPE, SIG_IGN);
437 add_signal_handler (SIGABRT, sigabrt_signal_handler);
440 add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler);
443 #ifndef PLATFORM_MACOSX
445 mono_runtime_install_handlers (void)
447 mono_runtime_posix_install_handlers ();
452 mono_runtime_cleanup_handlers (void)
454 if (mini_get_debug_options ()->handle_sigint)
455 remove_signal_handler (SIGINT);
457 remove_signal_handler (SIGFPE);
458 remove_signal_handler (SIGQUIT);
459 remove_signal_handler (SIGILL);
460 remove_signal_handler (SIGBUS);
461 if (mono_jit_trace_calls != NULL)
462 remove_signal_handler (SIGUSR2);
464 remove_signal_handler (mono_thread_get_abort_signal ());
466 remove_signal_handler (SIGABRT);
468 remove_signal_handler (SIGSEGV);
470 free_saved_signal_handlers ();
473 #ifdef HAVE_LINUX_RTC_H
474 #include <linux/rtc.h>
475 #include <sys/ioctl.h>
477 static int rtc_fd = -1;
480 enable_rtc_timer (gboolean enable)
483 flags = fcntl (rtc_fd, F_GETFL);
492 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
501 mono_runtime_shutdown_stat_profiler (void)
503 #ifdef HAVE_LINUX_RTC_H
505 enable_rtc_timer (FALSE);
510 mono_runtime_setup_stat_profiler (void)
513 struct itimerval itval;
514 static int inited = 0;
515 #ifdef HAVE_LINUX_RTC_H
516 const char *rtc_freq;
517 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
521 freq = atoi (rtc_freq);
524 rtc_fd = open ("/dev/rtc", O_RDONLY);
526 perror ("open /dev/rtc");
529 add_signal_handler (SIGPROF, sigprof_signal_handler);
530 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
531 perror ("set rtc freq");
534 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
535 perror ("start rtc");
538 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
542 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
546 enable_rtc_timer (TRUE);
553 itval.it_interval.tv_usec = 999;
554 itval.it_interval.tv_sec = 0;
555 itval.it_value = itval.it_interval;
556 setitimer (ITIMER_PROF, &itval, NULL);
560 add_signal_handler (SIGPROF, sigprof_signal_handler);
564 #if !defined(__APPLE__)
566 mono_runtime_syscall_fork ()
568 #if defined(SYS_fork)
569 return (pid_t) syscall (SYS_fork);
571 g_assert_not_reached ();
577 mono_gdb_render_native_backtraces ()
579 const char *argv [9];
582 argv [0] = g_find_program_in_path ("gdb");
583 if (argv [0] == NULL) {
588 sprintf (buf1, "attach %ld", (long)getpid ());
591 argv [4] = "info threads";
593 argv [6] = "thread apply all bt";
594 argv [7] = "--batch";
597 execv (argv [0], (char**)argv);