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)
150 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
152 if (mono_chain_signal (SIG_HANDLER_PARAMS))
154 mono_handle_native_sigsegv (SIGABRT, ctx);
159 SIG_HANDLER_SIGNATURE (sigusr1_signal_handler)
161 gboolean running_managed;
163 MonoInternalThread *thread = mono_thread_internal_current ();
164 MonoDomain *domain = mono_domain_get ();
169 if (!thread || !domain)
170 /* The thread might not have started up yet */
171 /* FIXME: Specify the synchronization with start_wrapper () in threads.c */
174 if (thread->thread_dump_requested) {
175 thread->thread_dump_requested = FALSE;
177 mono_print_thread_dump (ctx);
182 * This is an async signal, so the code below must not call anything which
183 * is not async safe. That includes the pthread locking functions. If we
184 * know that we interrupted managed code, then locking is safe.
186 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
187 running_managed = ji != NULL;
189 if (mono_debugger_agent_thread_interrupt (ji))
192 exc = mono_thread_request_interruption (running_managed);
196 mono_arch_handle_exception (ctx, exc, FALSE);
199 #if defined(__i386__) || defined(__x86_64__)
200 #define FULL_STAT_PROFILER_BACKTRACE 1
201 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
202 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
203 #if MONO_ARCH_STACK_GROWS_UP
204 #define IS_BEFORE_ON_STACK <
205 #define IS_AFTER_ON_STACK >
207 #define IS_BEFORE_ON_STACK >
208 #define IS_AFTER_ON_STACK <
211 #define FULL_STAT_PROFILER_BACKTRACE 0
214 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
217 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
219 if (mono_chain_signal (SIG_HANDLER_PARAMS))
228 SIG_HANDLER_SIGNATURE (sigprof_signal_handler)
230 int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
231 MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
234 if (call_chain_depth == 0) {
235 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
237 MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
238 int current_frame_index = 1;
239 MonoContext mono_context;
240 guchar *ips [call_chain_depth + 1];
242 mono_arch_sigctx_to_monoctx (ctx, &mono_context);
243 ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
245 if (jit_tls != NULL) {
246 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_NATIVE) {
247 #if FULL_STAT_PROFILER_BACKTRACE
248 guchar *current_frame;
249 guchar *stack_bottom;
252 stack_bottom = jit_tls->end_of_stack;
253 stack_top = MONO_CONTEXT_GET_SP (&mono_context);
254 current_frame = MONO_CONTEXT_GET_BP (&mono_context);
256 while ((current_frame_index <= call_chain_depth) &&
257 (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
258 ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
259 ips [current_frame_index] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
260 current_frame_index ++;
261 stack_top = current_frame;
262 current_frame = CURRENT_FRAME_GET_BASE_POINTER (current_frame);
265 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_GLIBC;
269 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_GLIBC) {
270 #if GLIBC_PROFILER_BACKTRACE
271 current_frame_index = backtrace ((void**) & ips [1], call_chain_depth);
273 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_MANAGED;
277 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_MANAGED) {
278 MonoDomain *domain = mono_domain_get ();
279 if (domain != NULL) {
283 MonoContext new_mono_context;
285 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
286 &new_mono_context, NULL, &lmf, &native_offset, NULL);
287 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
288 ips [current_frame_index] = MONO_CONTEXT_GET_IP (&new_mono_context);
289 current_frame_index ++;
290 mono_context = new_mono_context;
291 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
292 &new_mono_context, NULL, &lmf, &native_offset, NULL);
298 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
301 mono_chain_signal (SIG_HANDLER_PARAMS);
307 SIG_HANDLER_SIGNATURE (sigquit_signal_handler)
313 /* We use this signal to start the attach agent too */
314 res = mono_attach_start ();
318 printf ("Full thread dump:\n");
320 mono_threads_request_thread_dump ();
323 * print_thread_dump () skips the current thread, since sending a signal
324 * to it would invoke the signal handler below the sigquit signal handler,
325 * and signal handlers don't create an lmf, so the stack walk could not
328 mono_print_thread_dump (ctx);
330 mono_chain_signal (SIG_HANDLER_PARAMS);
334 SIG_HANDLER_SIGNATURE (sigusr2_signal_handler)
336 gboolean enabled = mono_trace_is_enabled ();
338 mono_trace_enable (!enabled);
340 mono_chain_signal (SIG_HANDLER_PARAMS);
344 add_signal_handler (int signo, gpointer handler)
347 struct sigaction previous_sa;
349 #ifdef MONO_ARCH_USE_SIGACTION
350 sa.sa_sigaction = handler;
351 sigemptyset (&sa.sa_mask);
352 sa.sa_flags = SA_SIGINFO;
353 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
354 if (signo == SIGSEGV) {
355 sa.sa_flags |= SA_ONSTACK;
358 * libgc will crash when trying to do stack marking for threads which are on
359 * an altstack, so delay the suspend signal after the signal handler has
362 if (mono_gc_get_suspend_signal () != -1)
363 sigaddset (&sa.sa_mask, mono_gc_get_suspend_signal ());
366 if (signo == SIGSEGV) {
368 * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
372 sigemptyset (&block_mask);
373 sigaddset (&sa.sa_mask, mono_thread_get_abort_signal ());
376 sa.sa_handler = handler;
377 sigemptyset (&sa.sa_mask);
380 g_assert (sigaction (signo, &sa, &previous_sa) != -1);
382 /* if there was already a handler in place for this signal, store it */
383 if (! (previous_sa.sa_flags & SA_SIGINFO) &&
384 (SIG_DFL == previous_sa.sa_handler)) {
385 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
387 if (mono_do_signal_chaining)
388 save_old_signal_handler (signo, &previous_sa);
393 remove_signal_handler (int signo)
396 struct sigaction *saved_action = get_saved_signal_handler (signo);
399 sa.sa_handler = SIG_DFL;
400 sigemptyset (&sa.sa_mask);
403 sigaction (signo, &sa, NULL);
405 g_assert (sigaction (signo, saved_action, NULL) != -1);
410 mono_runtime_posix_install_handlers (void)
415 if (mini_get_debug_options ()->handle_sigint)
416 add_signal_handler (SIGINT, mono_sigint_signal_handler);
418 add_signal_handler (SIGFPE, mono_sigfpe_signal_handler);
419 add_signal_handler (SIGQUIT, sigquit_signal_handler);
420 add_signal_handler (SIGILL, mono_sigill_signal_handler);
421 add_signal_handler (SIGBUS, mono_sigsegv_signal_handler);
422 if (mono_jit_trace_calls != NULL)
423 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
425 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
426 /* it seems to have become a common bug for some programs that run as parents
427 * of many processes to block signal delivery for real time signals.
428 * We try to detect and work around their breakage here.
430 sigemptyset (&signal_set);
431 sigaddset (&signal_set, mono_thread_get_abort_signal ());
432 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
434 signal (SIGPIPE, SIG_IGN);
436 add_signal_handler (SIGABRT, sigabrt_signal_handler);
439 add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler);
442 #ifndef PLATFORM_MACOSX
444 mono_runtime_install_handlers (void)
446 mono_runtime_posix_install_handlers ();
451 mono_runtime_cleanup_handlers (void)
453 if (mini_get_debug_options ()->handle_sigint)
454 remove_signal_handler (SIGINT);
456 remove_signal_handler (SIGFPE);
457 remove_signal_handler (SIGQUIT);
458 remove_signal_handler (SIGILL);
459 remove_signal_handler (SIGBUS);
460 if (mono_jit_trace_calls != NULL)
461 remove_signal_handler (SIGUSR2);
463 remove_signal_handler (mono_thread_get_abort_signal ());
465 remove_signal_handler (SIGABRT);
467 remove_signal_handler (SIGSEGV);
469 free_saved_signal_handlers ();
472 #ifdef HAVE_LINUX_RTC_H
473 #include <linux/rtc.h>
474 #include <sys/ioctl.h>
476 static int rtc_fd = -1;
479 enable_rtc_timer (gboolean enable)
482 flags = fcntl (rtc_fd, F_GETFL);
491 if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
500 mono_runtime_shutdown_stat_profiler (void)
502 #ifdef HAVE_LINUX_RTC_H
504 enable_rtc_timer (FALSE);
509 mono_runtime_setup_stat_profiler (void)
512 struct itimerval itval;
513 static int inited = 0;
514 #ifdef HAVE_LINUX_RTC_H
515 const char *rtc_freq;
516 if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
520 freq = atoi (rtc_freq);
523 rtc_fd = open ("/dev/rtc", O_RDONLY);
525 perror ("open /dev/rtc");
528 add_signal_handler (SIGPROF, sigprof_signal_handler);
529 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
530 perror ("set rtc freq");
533 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
534 perror ("start rtc");
537 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
541 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
545 enable_rtc_timer (TRUE);
552 itval.it_interval.tv_usec = 999;
553 itval.it_interval.tv_sec = 0;
554 itval.it_value = itval.it_interval;
555 setitimer (ITIMER_PROF, &itval, NULL);
559 add_signal_handler (SIGPROF, sigprof_signal_handler);
563 #if !defined(__APPLE__)
565 mono_runtime_syscall_fork ()
567 #if defined(SYS_fork)
568 return (pid_t) syscall (SYS_fork);
570 g_assert_not_reached ();
575 mono_gdb_render_native_backtraces ()
577 const char *argv [9];
580 argv [0] = g_find_program_in_path ("gdb");
581 if (argv [0] == NULL) {
586 sprintf (buf1, "attach %ld", (long)getpid ());
589 argv [4] = "info threads";
591 argv [6] = "thread apply all bt";
592 argv [7] = "--batch";
595 execv (argv [0], (char**)argv);