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.
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
23 #ifdef HAVE_SYS_TIME_H
26 #ifdef HAVE_SYS_SYSCALL_H
27 #include <sys/syscall.h>
32 #include <mono/metadata/assembly.h>
33 #include <mono/metadata/loader.h>
34 #include <mono/metadata/tabledefs.h>
35 #include <mono/metadata/class.h>
36 #include <mono/metadata/object.h>
37 #include <mono/metadata/tokentype.h>
38 #include <mono/metadata/tabledefs.h>
39 #include <mono/metadata/threads.h>
40 #include <mono/metadata/appdomain.h>
41 #include <mono/metadata/debug-helpers.h>
42 #include <mono/io-layer/io-layer.h>
43 #include "mono/metadata/profiler.h"
44 #include <mono/metadata/profiler-private.h>
45 #include <mono/metadata/mono-config.h>
46 #include <mono/metadata/environment.h>
47 #include <mono/metadata/mono-debug.h>
48 #include <mono/metadata/gc-internals.h>
49 #include <mono/metadata/threads-types.h>
50 #include <mono/metadata/verify.h>
51 #include <mono/metadata/verify-internals.h>
52 #include <mono/metadata/mempool-internals.h>
53 #include <mono/metadata/attach.h>
54 #include <mono/utils/mono-math.h>
55 #include <mono/utils/mono-compiler.h>
56 #include <mono/utils/mono-counters.h>
57 #include <mono/utils/mono-logger-internals.h>
58 #include <mono/utils/mono-mmap.h>
59 #include <mono/utils/dtrace.h>
60 #include <mono/utils/mono-signal-handler.h>
61 #include <mono/utils/mono-threads.h>
62 #include <mono/utils/mono-threads-posix-signals.h>
69 #include "debugger-agent.h"
71 #include "jit-icalls.h"
73 #ifdef PLATFORM_MACOSX
74 #include <mach/mach.h>
75 #include <mach/mach_time.h>
76 #include <mach/clock.h>
79 #if defined(__native_client__) || defined(HOST_WATCHOS)
82 mono_runtime_setup_stat_profiler (void)
84 printf("WARNING: mono_runtime_setup_stat_profiler() called!\n");
89 mono_runtime_shutdown_stat_profiler (void)
95 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
100 #ifndef PLATFORM_MACOSX
102 mono_runtime_install_handlers (void)
108 mono_runtime_posix_install_handlers(void)
110 /* we still need to ignore SIGPIPE */
111 signal (SIGPIPE, SIG_IGN);
115 mono_runtime_shutdown_handlers (void)
120 mono_runtime_cleanup_handlers (void)
124 #if !defined(PLATFORM_MACOSX)
126 mono_runtime_syscall_fork (void)
128 g_assert_not_reached();
133 mono_gdb_render_native_backtraces (pid_t crashed_pid)
140 static GHashTable *mono_saved_signal_handlers = NULL;
142 static struct sigaction *
143 get_saved_signal_handler (int signo, gboolean remove)
145 if (mono_saved_signal_handlers) {
146 /* The hash is only modified during startup, so no need for locking */
147 struct sigaction *handler = g_hash_table_lookup (mono_saved_signal_handlers, GINT_TO_POINTER (signo));
148 if (remove && handler)
149 g_hash_table_remove (mono_saved_signal_handlers, GINT_TO_POINTER (signo));
156 save_old_signal_handler (int signo, struct sigaction *old_action)
158 struct sigaction *handler_to_save = (struct sigaction *)g_malloc (sizeof (struct sigaction));
160 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_CONFIG,
161 "Saving old signal handler for signal %d.", signo);
163 if (! (old_action->sa_flags & SA_SIGINFO)) {
164 handler_to_save->sa_handler = old_action->sa_handler;
166 #ifdef MONO_ARCH_USE_SIGACTION
167 handler_to_save->sa_sigaction = old_action->sa_sigaction;
168 #endif /* MONO_ARCH_USE_SIGACTION */
170 handler_to_save->sa_mask = old_action->sa_mask;
171 handler_to_save->sa_flags = old_action->sa_flags;
173 if (!mono_saved_signal_handlers)
174 mono_saved_signal_handlers = g_hash_table_new_full (NULL, NULL, NULL, g_free);
175 g_hash_table_insert (mono_saved_signal_handlers, GINT_TO_POINTER (signo), handler_to_save);
179 free_saved_signal_handlers (void)
181 if (mono_saved_signal_handlers) {
182 g_hash_table_destroy (mono_saved_signal_handlers);
183 mono_saved_signal_handlers = NULL;
190 * Call the original signal handler for the signal given by the arguments, which
191 * should be the same as for a signal handler. Returns TRUE if the original handler
192 * was called, false otherwise.
195 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
197 int signal = MONO_SIG_HANDLER_GET_SIGNO ();
198 struct sigaction *saved_handler = (struct sigaction *)get_saved_signal_handler (signal, FALSE);
200 if (saved_handler && saved_handler->sa_handler) {
201 if (!(saved_handler->sa_flags & SA_SIGINFO)) {
202 saved_handler->sa_handler (signal);
204 #ifdef MONO_ARCH_USE_SIGACTION
205 saved_handler->sa_sigaction (MONO_SIG_HANDLER_PARAMS);
206 #endif /* MONO_ARCH_USE_SIGACTION */
213 MONO_SIG_HANDLER_FUNC (static, sigabrt_signal_handler)
215 MonoJitInfo *ji = NULL;
216 MONO_SIG_HANDLER_INFO_TYPE *info = MONO_SIG_HANDLER_GET_INFO ();
217 MONO_SIG_HANDLER_GET_CONTEXT;
219 if (mono_thread_internal_current ())
220 ji = mono_jit_info_table_find_internal (mono_domain_get (), (char *)mono_arch_ip_from_context (ctx), TRUE, TRUE);
222 if (mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
224 mono_handle_native_sigsegv (SIGABRT, ctx, info);
228 #if defined(__i386__) || defined(__x86_64__)
229 #define FULL_STAT_PROFILER_BACKTRACE 1
230 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
231 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
232 #if MONO_ARCH_STACK_GROWS_UP
233 #define IS_BEFORE_ON_STACK <
234 #define IS_AFTER_ON_STACK >
236 #define IS_BEFORE_ON_STACK >
237 #define IS_AFTER_ON_STACK <
240 #define FULL_STAT_PROFILER_BACKTRACE 0
243 #if (defined (USE_POSIX_BACKEND) && defined (SIGRTMIN)) || defined (SIGPROF)
244 #define HAVE_PROFILER_SIGNAL
247 #ifdef HAVE_PROFILER_SIGNAL
250 per_thread_profiler_hit (void *ctx)
252 int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
253 MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
255 if (call_chain_depth == 0) {
256 mono_profiler_stat_hit ((guchar *)mono_arch_ip_from_context (ctx), ctx);
258 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
259 int current_frame_index = 1;
260 MonoContext mono_context;
261 guchar *ips [call_chain_depth + 1];
263 mono_sigctx_to_monoctx (ctx, &mono_context);
264 ips [0] = (guchar *)MONO_CONTEXT_GET_IP (&mono_context);
266 if (jit_tls != NULL) {
267 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_NATIVE) {
268 #if FULL_STAT_PROFILER_BACKTRACE
269 guchar *current_frame;
270 guchar *stack_bottom;
273 stack_bottom = (guchar *)jit_tls->end_of_stack;
274 stack_top = (guchar *)MONO_CONTEXT_GET_SP (&mono_context);
275 current_frame = (guchar *)MONO_CONTEXT_GET_BP (&mono_context);
277 while ((current_frame_index <= call_chain_depth) &&
278 (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
279 ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
280 ips [current_frame_index] = (guchar *)CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
281 current_frame_index ++;
282 stack_top = current_frame;
283 current_frame = (guchar *)CURRENT_FRAME_GET_BASE_POINTER (current_frame);
286 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_GLIBC;
290 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_GLIBC) {
291 #if GLIBC_PROFILER_BACKTRACE
292 current_frame_index = backtrace ((void**) & ips [1], call_chain_depth);
294 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_MANAGED;
298 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_MANAGED) {
299 MonoDomain *domain = mono_domain_get ();
300 if (domain != NULL) {
304 MonoContext new_mono_context;
306 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
307 &new_mono_context, NULL, &lmf, &native_offset, NULL);
308 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
309 ips [current_frame_index] = (guchar *)MONO_CONTEXT_GET_IP (&new_mono_context);
310 current_frame_index ++;
311 mono_context = new_mono_context;
312 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
313 &new_mono_context, NULL, &lmf, &native_offset, NULL);
319 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
323 static MonoNativeThreadId sampling_thread;
325 static gint32 profiler_signals_sent;
326 static gint32 profiler_signals_received;
327 static gint32 profiler_signals_accepted;
328 static gint32 profiler_interrupt_signals_received;
330 MONO_SIG_HANDLER_FUNC (static, profiler_signal_handler)
332 int old_errno = errno;
334 MONO_SIG_HANDLER_GET_CONTEXT;
336 /* See the comment in mono_runtime_shutdown_stat_profiler (). */
337 if (mono_native_thread_id_get () == sampling_thread) {
338 InterlockedIncrement (&profiler_interrupt_signals_received);
342 InterlockedIncrement (&profiler_signals_received);
344 if (mono_thread_info_get_small_id () == -1)
345 return; //an non-attached thread got the signal
347 if (!mono_domain_get () || !mono_native_tls_get_value (mono_jit_tls_id))
348 return; //thread in the process of dettaching
350 InterlockedIncrement (&profiler_signals_accepted);
352 hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
354 mono_thread_info_set_is_async_context (TRUE);
355 per_thread_profiler_hit (ctx);
356 mono_thread_info_set_is_async_context (FALSE);
358 mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
361 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
366 MONO_SIG_HANDLER_FUNC (static, sigquit_signal_handler)
370 /* We use this signal to start the attach agent too */
371 res = mono_attach_start ();
375 mono_threads_request_thread_dump ();
377 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
380 MONO_SIG_HANDLER_FUNC (static, sigusr2_signal_handler)
382 gboolean enabled = mono_trace_is_enabled ();
384 mono_trace_enable (!enabled);
386 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
390 add_signal_handler (int signo, gpointer handler, int flags)
393 struct sigaction previous_sa;
395 #ifdef MONO_ARCH_USE_SIGACTION
396 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
397 sigemptyset (&sa.sa_mask);
398 sa.sa_flags = SA_SIGINFO | flags;
399 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
401 /*Apple likes to deliver SIGBUS for *0 */
402 #ifdef PLATFORM_MACOSX
403 if (signo == SIGSEGV || signo == SIGBUS) {
405 if (signo == SIGSEGV) {
407 sa.sa_flags |= SA_ONSTACK;
410 * libgc will crash when trying to do stack marking for threads which are on
411 * an altstack, so delay the suspend signal after the signal handler has
414 if (mono_gc_get_suspend_signal () != -1)
415 sigaddset (&sa.sa_mask, mono_gc_get_suspend_signal ());
418 if (signo == SIGSEGV) {
420 * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
424 sigemptyset (&block_mask);
427 sa.sa_handler = handler;
428 sigemptyset (&sa.sa_mask);
431 g_assert (sigaction (signo, &sa, &previous_sa) != -1);
433 /* if there was already a handler in place for this signal, store it */
434 if (! (previous_sa.sa_flags & SA_SIGINFO) &&
435 (SIG_DFL == previous_sa.sa_handler)) {
436 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
438 if (mono_do_signal_chaining)
439 save_old_signal_handler (signo, &previous_sa);
444 remove_signal_handler (int signo)
447 struct sigaction *saved_action = get_saved_signal_handler (signo, TRUE);
450 sa.sa_handler = SIG_DFL;
451 sigemptyset (&sa.sa_mask);
454 sigaction (signo, &sa, NULL);
456 g_assert (sigaction (signo, saved_action, NULL) != -1);
461 mono_runtime_posix_install_handlers (void)
466 if (mini_get_debug_options ()->handle_sigint)
467 add_signal_handler (SIGINT, mono_sigint_signal_handler, SA_RESTART);
469 add_signal_handler (SIGFPE, mono_sigfpe_signal_handler, 0);
470 add_signal_handler (SIGQUIT, sigquit_signal_handler, SA_RESTART);
471 add_signal_handler (SIGILL, mono_sigill_signal_handler, 0);
472 add_signal_handler (SIGBUS, mono_sigsegv_signal_handler, 0);
473 if (mono_jit_trace_calls != NULL)
474 add_signal_handler (SIGUSR2, sigusr2_signal_handler, SA_RESTART);
476 /* it seems to have become a common bug for some programs that run as parents
477 * of many processes to block signal delivery for real time signals.
478 * We try to detect and work around their breakage here.
480 sigemptyset (&signal_set);
481 if (mono_gc_get_suspend_signal () != -1)
482 sigaddset (&signal_set, mono_gc_get_suspend_signal ());
483 if (mono_gc_get_restart_signal () != -1)
484 sigaddset (&signal_set, mono_gc_get_restart_signal ());
485 sigaddset (&signal_set, SIGCHLD);
486 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
488 signal (SIGPIPE, SIG_IGN);
490 add_signal_handler (SIGABRT, sigabrt_signal_handler, 0);
493 add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler, 0);
496 #ifndef PLATFORM_MACOSX
498 mono_runtime_install_handlers (void)
500 mono_runtime_posix_install_handlers ();
505 mono_runtime_cleanup_handlers (void)
507 if (mini_get_debug_options ()->handle_sigint)
508 remove_signal_handler (SIGINT);
510 remove_signal_handler (SIGFPE);
511 remove_signal_handler (SIGQUIT);
512 remove_signal_handler (SIGILL);
513 remove_signal_handler (SIGBUS);
514 if (mono_jit_trace_calls != NULL)
515 remove_signal_handler (SIGUSR2);
517 remove_signal_handler (SIGABRT);
519 remove_signal_handler (SIGSEGV);
521 free_saved_signal_handlers ();
524 #ifdef HAVE_PROFILER_SIGNAL
526 static volatile gint32 sampling_thread_running;
528 #ifdef PLATFORM_MACOSX
530 static clock_serv_t sampling_clock_service;
538 ret = host_get_clock_service (mach_host_self (), SYSTEM_CLOCK, &sampling_clock_service);
539 } while (ret == KERN_ABORTED);
541 if (ret != KERN_SUCCESS)
542 g_error ("%s: host_get_clock_service () returned %d", __func__, ret);
551 ret = mach_port_deallocate (mach_task_self (), sampling_clock_service);
552 } while (ret == KERN_ABORTED);
554 if (ret != KERN_SUCCESS)
555 g_error ("%s: mach_port_deallocate () returned %d", __func__, ret);
559 clock_get_time_ns (void)
562 mach_timespec_t mach_ts;
565 ret = clock_get_time (sampling_clock_service, &mach_ts);
566 } while (ret == KERN_ABORTED);
568 if (ret != KERN_SUCCESS)
569 g_error ("%s: clock_get_time () returned %d", __func__, ret);
571 return ((guint64) mach_ts.tv_sec * 1000000000) + (guint64) mach_ts.tv_nsec;
575 clock_sleep_ns_abs (guint64 ns_abs)
578 mach_timespec_t then, remain_unused;
580 then.tv_sec = ns_abs / 1000000000;
581 then.tv_nsec = ns_abs % 1000000000;
584 ret = clock_sleep (sampling_clock_service, TIME_ABSOLUTE, then, &remain_unused);
586 if (ret != KERN_SUCCESS && ret != KERN_ABORTED)
587 g_error ("%s: clock_sleep () returned %d", __func__, ret);
588 } while (ret == KERN_ABORTED && InterlockedRead (&sampling_thread_running));
593 clockid_t sampling_posix_clock;
598 switch (mono_profiler_get_sampling_mode ()) {
599 case MONO_PROFILER_STAT_MODE_PROCESS:
600 #ifdef HAVE_CLOCK_NANOSLEEP
602 * If we don't have clock_nanosleep (), measuring the process time
603 * makes very little sense as we can only use nanosleep () to sleep on
606 sampling_posix_clock = CLOCK_PROCESS_CPUTIME_ID;
609 case MONO_PROFILER_STAT_MODE_REAL: sampling_posix_clock = CLOCK_MONOTONIC; break;
610 default: g_assert_not_reached (); break;
620 clock_get_time_ns (void)
624 if (clock_gettime (sampling_posix_clock, &ts) == -1)
625 g_error ("%s: clock_gettime () returned -1, errno = %d", __func__, errno);
627 return ((guint64) ts.tv_sec * 1000000000) + (guint64) ts.tv_nsec;
631 clock_sleep_ns_abs (guint64 ns_abs)
633 #ifdef HAVE_CLOCK_NANOSLEEP
635 struct timespec then;
637 then.tv_sec = ns_abs / 1000000000;
638 then.tv_nsec = ns_abs % 1000000000;
641 ret = clock_nanosleep (sampling_posix_clock, TIMER_ABSTIME, &then, NULL);
643 if (ret != 0 && ret != EINTR)
644 g_error ("%s: clock_nanosleep () returned %d", __func__, ret);
645 } while (ret == EINTR && InterlockedRead (&sampling_thread_running));
652 * What follows is a crude attempt at emulating clock_nanosleep () on OSs
653 * which don't provide it (e.g. FreeBSD).
655 * The problem with nanosleep () is that if it is interrupted by a signal,
656 * time will drift as a result of having to restart the call after the
657 * signal handler has finished. For this reason, we avoid using the rem
658 * argument of nanosleep (). Instead, before every nanosleep () call, we
659 * check if enough time has passed to satisfy the sleep request. If yes, we
660 * simply return. If not, we calculate the difference and do another sleep.
662 * This should reduce the amount of drift that happens because we account
663 * for the time spent executing the signal handler, which nanosleep () is
664 * not guaranteed to do for the rem argument.
666 * The downside to this approach is that it is slightly expensive: We have
667 * to make an extra system call to retrieve the current time whenever we're
668 * going to restart a nanosleep () call. This is unlikely to be a problem
669 * in practice since the sampling thread won't be receiving many signals in
670 * the first place (it's a tools thread, so no STW), and because typical
671 * sleep periods for the thread are many orders of magnitude bigger than
672 * the time it takes to actually perform that system call (just a few
676 diff = (gint64) ns_abs - (gint64) clock_get_time_ns ();
681 req.tv_sec = diff / 1000000000;
682 req.tv_nsec = diff % 1000000000;
684 if ((ret = nanosleep (&req, NULL)) == -1 && errno != EINTR)
685 g_error ("%s: nanosleep () returned -1, errno = %d", __func__, errno);
686 } while (ret == -1 && InterlockedRead (&sampling_thread_running));
692 static int profiler_signal;
693 static volatile gint32 sampling_thread_exiting;
695 static mono_native_thread_return_t
696 sampling_thread_func (void *data)
698 mono_threads_attach_tools_thread ();
699 mono_native_thread_set_name (mono_native_thread_id_get (), "Profiler sampler");
701 gint64 rate = 1000000000 / mono_profiler_get_sampling_rate ();
704 struct sched_param old_sched;
705 pthread_getschedparam (pthread_self (), &old_policy, &old_sched);
708 * Attempt to switch the thread to real time scheduling. This will not
709 * necessarily work on all OSs; for example, most Linux systems will give
710 * us EPERM here unless configured to allow this.
712 * TODO: This does not work on Mac (and maybe some other OSs). On Mac, we
713 * have to use the Mach thread policy routines to switch to real-time
714 * scheduling. This is quite tricky as we need to specify how often we'll
715 * be doing work (easy), the normal processing time needed (also easy),
716 * and the maximum amount of processing time needed (hard). This is
717 * further complicated by the fact that if we misbehave and take too long
718 * to do our work, the kernel may knock us back down to the normal thread
719 * scheduling policy without telling us.
721 struct sched_param sched = { .sched_priority = sched_get_priority_max (SCHED_FIFO) };
722 pthread_setschedparam (pthread_self (), SCHED_FIFO, &sched);
726 guint64 sleep = clock_get_time_ns ();
728 while (InterlockedRead (&sampling_thread_running)) {
731 FOREACH_THREAD_SAFE (info) {
732 /* info should never be this thread as we're a tools thread. */
733 g_assert (mono_thread_info_get_tid (info) != mono_native_thread_id_get ());
735 mono_threads_pthread_kill (info, profiler_signal);
736 InterlockedIncrement (&profiler_signals_sent);
737 } FOREACH_THREAD_SAFE_END
739 clock_sleep_ns_abs (sleep);
742 InterlockedWrite (&sampling_thread_exiting, 1);
746 pthread_setschedparam (pthread_self (), old_policy, &old_sched);
748 mono_thread_info_detach ();
754 mono_runtime_shutdown_stat_profiler (void)
756 InterlockedWrite (&sampling_thread_running, 0);
758 #ifndef PLATFORM_MACOSX
760 * There is a slight problem when we're using CLOCK_PROCESS_CPUTIME_ID: If
761 * we're shutting down and there's largely no activity in the process other
762 * than waiting for the sampler thread to shut down, it can take upwards of
763 * 20 seconds (depending on a lot of factors) for us to shut down because
764 * the sleep progresses very slowly as a result of the low CPU activity.
766 * We fix this by repeatedly sending the profiler signal to the sampler
767 * thread in order to interrupt the sleep. clock_sleep_ns_abs () will check
768 * sampling_thread_running upon an interrupt and return immediately if it's
769 * zero. profiler_signal_handler () has a special case to ignore the signal
770 * for the sampler thread.
772 MonoThreadInfo *info;
774 // Did it shut down already?
775 if ((info = mono_thread_info_lookup (sampling_thread))) {
776 while (!InterlockedRead (&sampling_thread_exiting)) {
777 mono_threads_pthread_kill (info, profiler_signal);
778 mono_thread_info_usleep (10 * 1000 /* 10ms */);
781 // Make sure info can be freed.
782 mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
786 mono_native_thread_join (sampling_thread);
789 * We can't safely remove the signal handler because we have no guarantee
790 * that all pending signals have been delivered at this point. This should
791 * not really be a problem anyway.
793 //remove_signal_handler (profiler_signal);
797 mono_runtime_setup_stat_profiler (void)
800 * Use a real-time signal when possible. This gives us roughly a 99% signal
801 * delivery rate in all cases. On the other hand, using a regular signal
802 * tends to result in awful delivery rates when the application is heavily
805 * We avoid real-time signals on Android as they're super broken in certain
806 * API levels (too small sigset_t, nonsensical SIGRTMIN/SIGRTMAX values,
809 * TODO: On Mac, we should explore using the Mach thread suspend/resume
810 * functions and doing the stack walk from the sampling thread. This would
811 * get us a 100% sampling rate. However, this may interfere with the GC's
812 * STW logic. Could perhaps be solved by taking the suspend lock.
814 #if defined (USE_POSIX_BACKEND) && defined (SIGRTMIN) && !defined (PLATFORM_ANDROID)
815 /* Just take the first real-time signal we can get. */
816 profiler_signal = mono_threads_posix_signal_search_alternative (-1);
818 profiler_signal = SIGPROF;
821 add_signal_handler (profiler_signal, profiler_signal_handler, SA_RESTART);
823 mono_counters_register ("Sampling signals sent", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_signals_sent);
824 mono_counters_register ("Sampling signals received", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_signals_received);
825 mono_counters_register ("Sampling signals accepted", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_signals_accepted);
826 mono_counters_register ("Shutdown signals received", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_interrupt_signals_received);
828 InterlockedWrite (&sampling_thread_running, 1);
829 mono_native_thread_create (&sampling_thread, sampling_thread_func, NULL);
835 mono_runtime_shutdown_stat_profiler (void)
840 mono_runtime_setup_stat_profiler (void)
846 #if !defined(PLATFORM_MACOSX)
848 mono_runtime_syscall_fork ()
850 #if defined(PLATFORM_ANDROID)
851 /* SYS_fork is defined to be __NR_fork which is not defined in some ndk versions */
852 g_assert_not_reached ();
854 #elif defined(SYS_fork)
855 return (pid_t) syscall (SYS_fork);
857 g_assert_not_reached ();
863 mono_gdb_render_native_backtraces (pid_t crashed_pid)
865 const char *argv [9];
866 char template_ [] = "/tmp/mono-lldb-commands.XXXXXX";
869 gboolean using_lldb = FALSE;
871 argv [0] = g_find_program_in_path ("gdb");
872 if (argv [0] == NULL) {
873 argv [0] = g_find_program_in_path ("lldb");
877 if (argv [0] == NULL)
881 if (mkstemp (template_) == -1)
884 commands = fopen (template_, "w");
886 fprintf (commands, "process attach --pid %ld\n", (long) crashed_pid);
887 fprintf (commands, "thread list\n");
888 fprintf (commands, "thread backtrace all\n");
889 fprintf (commands, "detach\n");
890 fprintf (commands, "quit\n");
895 argv [1] = "--source";
896 argv [2] = template_;
900 sprintf (buf1, "attach %ld", (long) crashed_pid);
903 argv [4] = "info threads";
905 argv [6] = "thread apply all bt";
906 argv [7] = "--batch";
910 execv (argv [0], (char**)argv);
916 #endif /* __native_client__ */
918 #if !defined (__MACH__)
921 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
923 g_error ("Posix systems don't support mono_thread_state_init_from_handle");