3 * POSIX signal handling support for Mono.
6 * Mono Team (mono-list@lists.ximian.com)
8 * Copyright 2001-2003 Ximian, Inc.
9 * Copyright 2003-2008 Ximian, Inc.
10 * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
12 * See LICENSE for licensing information.
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
23 #ifdef HAVE_EXECINFO_H
27 #ifdef HAVE_SYS_TIME_H
30 #ifdef HAVE_SYS_SYSCALL_H
31 #include <sys/syscall.h>
33 #ifdef HAVE_SYS_PRCTL_H
34 #include <sys/prctl.h>
36 #ifdef HAVE_SYS_WAIT_H
42 #include <mono/metadata/assembly.h>
43 #include <mono/metadata/loader.h>
44 #include <mono/metadata/tabledefs.h>
45 #include <mono/metadata/class.h>
46 #include <mono/metadata/object.h>
47 #include <mono/metadata/tokentype.h>
48 #include <mono/metadata/tabledefs.h>
49 #include <mono/metadata/threads.h>
50 #include <mono/metadata/appdomain.h>
51 #include <mono/metadata/debug-helpers.h>
52 #include <mono/metadata/profiler-private.h>
53 #include <mono/metadata/mono-config.h>
54 #include <mono/metadata/environment.h>
55 #include <mono/metadata/mono-debug.h>
56 #include <mono/metadata/gc-internals.h>
57 #include <mono/metadata/threads-types.h>
58 #include <mono/metadata/verify.h>
59 #include <mono/metadata/verify-internals.h>
60 #include <mono/metadata/mempool-internals.h>
61 #include <mono/metadata/attach.h>
62 #include <mono/utils/mono-math.h>
63 #include <mono/utils/mono-errno.h>
64 #include <mono/utils/mono-compiler.h>
65 #include <mono/utils/mono-counters.h>
66 #include <mono/utils/mono-logger-internals.h>
67 #include <mono/utils/mono-mmap.h>
68 #include <mono/utils/dtrace.h>
69 #include <mono/utils/mono-signal-handler.h>
70 #include <mono/utils/mono-threads.h>
71 #include <mono/utils/os-event.h>
72 #include <mono/utils/mono-state.h>
73 #include <mono/mini/debugger-state-machine.h>
80 #include "debugger-agent.h"
81 #include "mini-runtime.h"
82 #include "jit-icalls.h"
85 #include <mach/mach.h>
86 #include <mach/mach_time.h>
87 #include <mach/clock.h>
88 #include <mono/utils/mono-merp.h>
92 #include <mono/utils/mono-threads-debug.h>
100 #include "mono/utils/mono-tls-inline.h"
102 #if defined(HOST_WATCHOS)
105 mono_runtime_setup_stat_profiler (void)
107 printf("WARNING: mono_runtime_setup_stat_profiler() called!\n");
112 mono_runtime_shutdown_stat_profiler (void)
118 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal
)
125 mono_runtime_install_handlers (void)
131 mono_runtime_posix_install_handlers(void)
133 /* we still need to ignore SIGPIPE */
134 signal (SIGPIPE
, SIG_IGN
);
138 mono_runtime_shutdown_handlers (void)
143 mono_runtime_cleanup_handlers (void)
149 static GHashTable
*mono_saved_signal_handlers
= NULL
;
151 static struct sigaction
*
152 get_saved_signal_handler (int signo
, gboolean remove
)
154 if (mono_saved_signal_handlers
) {
155 /* The hash is only modified during startup, so no need for locking */
156 struct sigaction
*handler
= (struct sigaction
*)g_hash_table_lookup (mono_saved_signal_handlers
, GINT_TO_POINTER (signo
));
157 if (remove
&& handler
)
158 g_hash_table_remove (mono_saved_signal_handlers
, GINT_TO_POINTER (signo
));
165 save_old_signal_handler (int signo
, struct sigaction
*old_action
)
167 struct sigaction
*handler_to_save
= (struct sigaction
*)g_malloc (sizeof (struct sigaction
));
169 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_CONFIG
,
170 "Saving old signal handler for signal %d.", signo
);
172 if (! (old_action
->sa_flags
& SA_SIGINFO
)) {
173 handler_to_save
->sa_handler
= old_action
->sa_handler
;
175 #ifdef MONO_ARCH_USE_SIGACTION
176 handler_to_save
->sa_sigaction
= old_action
->sa_sigaction
;
177 #endif /* MONO_ARCH_USE_SIGACTION */
179 handler_to_save
->sa_mask
= old_action
->sa_mask
;
180 handler_to_save
->sa_flags
= old_action
->sa_flags
;
182 if (!mono_saved_signal_handlers
)
183 mono_saved_signal_handlers
= g_hash_table_new_full (NULL
, NULL
, NULL
, g_free
);
184 g_hash_table_insert (mono_saved_signal_handlers
, GINT_TO_POINTER (signo
), handler_to_save
);
188 free_saved_signal_handlers (void)
190 g_hash_table_destroy (mono_saved_signal_handlers
);
191 mono_saved_signal_handlers
= NULL
;
197 * Call the original signal handler for the signal given by the arguments, which
198 * should be the same as for a signal handler. Returns TRUE if the original handler
199 * was called, false otherwise.
202 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal
)
204 int signal
= MONO_SIG_HANDLER_GET_SIGNO ();
205 struct sigaction
*saved_handler
= (struct sigaction
*)get_saved_signal_handler (signal
, FALSE
);
207 if (saved_handler
&& saved_handler
->sa_handler
) {
208 if (!(saved_handler
->sa_flags
& SA_SIGINFO
)) {
209 saved_handler
->sa_handler (signal
);
211 #ifdef MONO_ARCH_USE_SIGACTION
212 saved_handler
->sa_sigaction (MONO_SIG_HANDLER_PARAMS
);
213 #endif /* MONO_ARCH_USE_SIGACTION */
220 MONO_SIG_HANDLER_FUNC (static, sigabrt_signal_handler
)
222 MonoJitInfo
*ji
= NULL
;
224 MONO_SIG_HANDLER_INFO_TYPE
*info
= MONO_SIG_HANDLER_GET_INFO ();
225 MONO_SIG_HANDLER_GET_CONTEXT
;
227 if (mono_thread_internal_current ())
228 ji
= mono_jit_info_table_find_internal (mono_domain_get (), mono_arch_ip_from_context (ctx
), TRUE
, TRUE
);
230 if (mono_chain_signal (MONO_SIG_HANDLER_PARAMS
))
232 mono_sigctx_to_monoctx (ctx
, &mctx
);
233 if (mono_dump_start ())
234 mono_handle_native_crash ("SIGABRT", &mctx
, info
);
240 MONO_SIG_HANDLER_FUNC (static, sigterm_signal_handler
)
242 #ifndef DISABLE_CRASH_REPORTING
243 MONO_SIG_HANDLER_GET_CONTEXT
;
245 // Note: this is only run from the non-controlling thread
247 gchar
*output
= NULL
;
248 MonoStackHash hashes
;
249 mono_sigctx_to_monoctx (ctx
, &mctx
);
251 // Will return when the dumping is done, so this thread can continue
252 // running. Returns FALSE on unrecoverable error.
253 if (mono_dump_start ()) {
254 // Process was killed from outside since crash reporting wasn't running yet.
255 mono_handle_native_crash ("SIGTERM", &mctx
, NULL
);
257 // Crash reporting already running and we got a second SIGTERM from as part of thread-summarizing
258 if (!mono_threads_summarize_execute (&mctx
, &output
, &hashes
, FALSE
, NULL
, 0))
259 g_error ("Crash reporter dumper exited due to fatal error.");
263 mono_chain_signal (MONO_SIG_HANDLER_PARAMS
);
266 #if (defined (USE_POSIX_BACKEND) && defined (SIGRTMIN)) || defined (SIGPROF)
267 #define HAVE_PROFILER_SIGNAL
270 #ifdef HAVE_PROFILER_SIGNAL
272 static MonoNativeThreadId sampling_thread
;
274 static gint32 profiler_signals_sent
;
275 static gint32 profiler_signals_received
;
276 static gint32 profiler_signals_accepted
;
277 static gint32 profiler_interrupt_signals_received
;
279 MONO_SIG_HANDLER_FUNC (static, profiler_signal_handler
)
281 int old_errno
= errno
;
283 MONO_SIG_HANDLER_GET_CONTEXT
;
285 /* See the comment in mono_runtime_shutdown_stat_profiler (). */
286 if (mono_native_thread_id_get () == sampling_thread
) {
287 mono_atomic_inc_i32 (&profiler_interrupt_signals_received
);
291 mono_atomic_inc_i32 (&profiler_signals_received
);
293 // Did a non-attached or detaching thread get the signal?
294 if (mono_thread_info_get_small_id () == -1 ||
295 !mono_domain_get () ||
296 !mono_tls_get_jit_tls ()) {
297 mono_set_errno (old_errno
);
301 // See the comment in sampling_thread_func ().
302 mono_atomic_store_i32 (&mono_thread_info_current ()->profiler_signal_ack
, 1);
304 mono_atomic_inc_i32 (&profiler_signals_accepted
);
306 int hp_save_index
= mono_hazard_pointer_save_for_signal_handler ();
308 mono_thread_info_set_is_async_context (TRUE
);
310 MONO_PROFILER_RAISE (sample_hit
, ((const mono_byte
*)mono_arch_ip_from_context (ctx
), ctx
));
312 mono_thread_info_set_is_async_context (FALSE
);
314 mono_hazard_pointer_restore_for_signal_handler (hp_save_index
);
316 mono_set_errno (old_errno
);
318 mono_chain_signal (MONO_SIG_HANDLER_PARAMS
);
323 MONO_SIG_HANDLER_FUNC (static, sigquit_signal_handler
)
327 /* We use this signal to start the attach agent too */
328 res
= mono_attach_start ();
332 mono_threads_request_thread_dump ();
334 mono_chain_signal (MONO_SIG_HANDLER_PARAMS
);
337 MONO_SIG_HANDLER_FUNC (static, sigusr2_signal_handler
)
339 gboolean enabled
= mono_trace_is_enabled ();
341 mono_trace_enable (!enabled
);
343 mono_chain_signal (MONO_SIG_HANDLER_PARAMS
);
346 typedef void MONO_SIG_HANDLER_SIGNATURE ((*MonoSignalHandler
));
349 add_signal_handler (int signo
, MonoSignalHandler handler
, int flags
)
352 struct sigaction previous_sa
;
354 #ifdef MONO_ARCH_USE_SIGACTION
355 sa
.sa_sigaction
= handler
;
356 sigemptyset (&sa
.sa_mask
);
357 sa
.sa_flags
= SA_SIGINFO
| flags
;
358 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
360 /*Apple likes to deliver SIGBUS for *0 */
362 if (signo
== SIGSEGV
|| signo
== SIGBUS
) {
364 if (signo
== SIGSEGV
) {
366 sa
.sa_flags
|= SA_ONSTACK
;
369 * libgc will crash when trying to do stack marking for threads which are on
370 * an altstack, so delay the suspend signal after the signal handler has
373 if (mono_gc_get_suspend_signal () != -1)
374 sigaddset (&sa
.sa_mask
, mono_gc_get_suspend_signal ());
377 if (signo
== SIGSEGV
) {
379 * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
383 sigemptyset (&block_mask
);
386 sa
.sa_handler
= (void (*)(int))handler
;
387 sigemptyset (&sa
.sa_mask
);
390 g_assert (sigaction (signo
, &sa
, &previous_sa
) != -1);
392 /* if there was already a handler in place for this signal, store it */
393 if (! (previous_sa
.sa_flags
& SA_SIGINFO
) &&
394 (SIG_DFL
== previous_sa
.sa_handler
)) {
395 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
397 if (mono_do_signal_chaining
)
398 save_old_signal_handler (signo
, &previous_sa
);
403 remove_signal_handler (int signo
)
406 struct sigaction
*saved_action
= get_saved_signal_handler (signo
, TRUE
);
409 sa
.sa_handler
= SIG_DFL
;
410 sigemptyset (&sa
.sa_mask
);
413 sigaction (signo
, &sa
, NULL
);
415 g_assert (sigaction (signo
, saved_action
, NULL
) != -1);
420 mini_register_sigterm_handler (void)
422 #ifndef DISABLE_CRASH_REPORTING
423 static gboolean enabled
;
428 /* always catch SIGTERM, conditionals inside of handler */
429 add_signal_handler (SIGTERM
, sigterm_signal_handler
, 0);
435 mono_runtime_posix_install_handlers (void)
439 sigemptyset (&signal_set
);
440 if (mini_debug_options
.handle_sigint
) {
441 add_signal_handler (SIGINT
, mono_sigint_signal_handler
, SA_RESTART
);
442 sigaddset (&signal_set
, SIGINT
);
445 add_signal_handler (SIGFPE
, mono_sigfpe_signal_handler
, 0);
446 sigaddset (&signal_set
, SIGFPE
);
447 add_signal_handler (SIGQUIT
, sigquit_signal_handler
, SA_RESTART
);
448 sigaddset (&signal_set
, SIGQUIT
);
449 add_signal_handler (SIGILL
, mono_sigill_signal_handler
, 0);
450 sigaddset (&signal_set
, SIGILL
);
451 add_signal_handler (SIGBUS
, mono_sigsegv_signal_handler
, 0);
452 sigaddset (&signal_set
, SIGBUS
);
453 if (mono_jit_trace_calls
!= NULL
) {
454 add_signal_handler (SIGUSR2
, sigusr2_signal_handler
, SA_RESTART
);
455 sigaddset (&signal_set
, SIGUSR2
);
458 /* it seems to have become a common bug for some programs that run as parents
459 * of many processes to block signal delivery for real time signals.
460 * We try to detect and work around their breakage here.
462 if (mono_gc_get_suspend_signal () != -1)
463 sigaddset (&signal_set
, mono_gc_get_suspend_signal ());
464 if (mono_gc_get_restart_signal () != -1)
465 sigaddset (&signal_set
, mono_gc_get_restart_signal ());
466 sigaddset (&signal_set
, SIGCHLD
);
468 signal (SIGPIPE
, SIG_IGN
);
469 sigaddset (&signal_set
, SIGPIPE
);
471 add_signal_handler (SIGABRT
, sigabrt_signal_handler
, 0);
472 sigaddset (&signal_set
, SIGABRT
);
475 add_signal_handler (SIGSEGV
, mono_sigsegv_signal_handler
, 0);
476 sigaddset (&signal_set
, SIGSEGV
);
478 sigprocmask (SIG_UNBLOCK
, &signal_set
, NULL
);
483 mono_runtime_install_handlers (void)
485 mono_runtime_posix_install_handlers ();
490 mono_runtime_cleanup_handlers (void)
492 if (mini_debug_options
.handle_sigint
)
493 remove_signal_handler (SIGINT
);
495 remove_signal_handler (SIGFPE
);
496 remove_signal_handler (SIGQUIT
);
497 remove_signal_handler (SIGILL
);
498 remove_signal_handler (SIGBUS
);
499 if (mono_jit_trace_calls
!= NULL
)
500 remove_signal_handler (SIGUSR2
);
502 remove_signal_handler (SIGABRT
);
504 remove_signal_handler (SIGSEGV
);
506 free_saved_signal_handlers ();
509 #ifdef HAVE_PROFILER_SIGNAL
511 static volatile gint32 sampling_thread_running
;
515 static clock_serv_t sampling_clock_service
;
518 clock_init (MonoProfilerSampleMode mode
)
523 ret
= host_get_clock_service (mach_host_self (), SYSTEM_CLOCK
, &sampling_clock_service
);
524 } while (ret
== KERN_ABORTED
);
526 if (ret
!= KERN_SUCCESS
)
527 g_error ("%s: host_get_clock_service () returned %d", __func__
, ret
);
536 ret
= mach_port_deallocate (mach_task_self (), sampling_clock_service
);
537 } while (ret
== KERN_ABORTED
);
539 if (ret
!= KERN_SUCCESS
)
540 g_error ("%s: mach_port_deallocate () returned %d", __func__
, ret
);
544 clock_get_time_ns (void)
547 mach_timespec_t mach_ts
;
550 ret
= clock_get_time (sampling_clock_service
, &mach_ts
);
551 } while (ret
== KERN_ABORTED
);
553 if (ret
!= KERN_SUCCESS
)
554 g_error ("%s: clock_get_time () returned %d", __func__
, ret
);
556 return ((guint64
) mach_ts
.tv_sec
* 1000000000) + (guint64
) mach_ts
.tv_nsec
;
560 clock_sleep_ns_abs (guint64 ns_abs
)
563 mach_timespec_t then
, remain_unused
;
565 then
.tv_sec
= ns_abs
/ 1000000000;
566 then
.tv_nsec
= ns_abs
% 1000000000;
569 ret
= clock_sleep (sampling_clock_service
, TIME_ABSOLUTE
, then
, &remain_unused
);
571 if (ret
!= KERN_SUCCESS
&& ret
!= KERN_ABORTED
)
572 g_error ("%s: clock_sleep () returned %d", __func__
, ret
);
573 } while (ret
== KERN_ABORTED
&& mono_atomic_load_i32 (&sampling_thread_running
));
578 static clockid_t sampling_posix_clock
;
581 clock_init (MonoProfilerSampleMode mode
)
584 case MONO_PROFILER_SAMPLE_MODE_PROCESS
: {
586 * If we don't have clock_nanosleep (), measuring the process time
587 * makes very little sense as we can only use nanosleep () to sleep on
590 #if defined(HAVE_CLOCK_NANOSLEEP) && !defined(__PASE__)
591 struct timespec ts
= { 0 };
594 * Some systems (e.g. Windows Subsystem for Linux) declare the
595 * CLOCK_PROCESS_CPUTIME_ID clock but don't actually support it. For
596 * those systems, we fall back to CLOCK_MONOTONIC if we get EINVAL.
598 if (clock_nanosleep (CLOCK_PROCESS_CPUTIME_ID
, TIMER_ABSTIME
, &ts
, NULL
) != EINVAL
) {
599 sampling_posix_clock
= CLOCK_PROCESS_CPUTIME_ID
;
606 case MONO_PROFILER_SAMPLE_MODE_REAL
: sampling_posix_clock
= CLOCK_MONOTONIC
; break;
607 default: g_assert_not_reached (); break;
617 clock_get_time_ns (void)
621 if (clock_gettime (sampling_posix_clock
, &ts
) == -1)
622 g_error ("%s: clock_gettime () returned -1, errno = %d", __func__
, errno
);
624 return ((guint64
) ts
.tv_sec
* 1000000000) + (guint64
) ts
.tv_nsec
;
628 clock_sleep_ns_abs (guint64 ns_abs
)
630 #if defined(HAVE_CLOCK_NANOSLEEP) && !defined(__PASE__)
632 struct timespec then
;
634 then
.tv_sec
= ns_abs
/ 1000000000;
635 then
.tv_nsec
= ns_abs
% 1000000000;
638 ret
= clock_nanosleep (sampling_posix_clock
, TIMER_ABSTIME
, &then
, NULL
);
640 if (ret
!= 0 && ret
!= EINTR
)
641 g_error ("%s: clock_nanosleep () returned %d", __func__
, ret
);
642 } while (ret
== EINTR
&& mono_atomic_load_i32 (&sampling_thread_running
));
649 * What follows is a crude attempt at emulating clock_nanosleep () on OSs
650 * which don't provide it (e.g. FreeBSD).
652 * The problem with nanosleep () is that if it is interrupted by a signal,
653 * time will drift as a result of having to restart the call after the
654 * signal handler has finished. For this reason, we avoid using the rem
655 * argument of nanosleep (). Instead, before every nanosleep () call, we
656 * check if enough time has passed to satisfy the sleep request. If yes, we
657 * simply return. If not, we calculate the difference and do another sleep.
659 * This should reduce the amount of drift that happens because we account
660 * for the time spent executing the signal handler, which nanosleep () is
661 * not guaranteed to do for the rem argument.
663 * The downside to this approach is that it is slightly expensive: We have
664 * to make an extra system call to retrieve the current time whenever we're
665 * going to restart a nanosleep () call. This is unlikely to be a problem
666 * in practice since the sampling thread won't be receiving many signals in
667 * the first place (it's a tools thread, so no STW), and because typical
668 * sleep periods for the thread are many orders of magnitude bigger than
669 * the time it takes to actually perform that system call (just a few
673 diff
= (gint64
) ns_abs
- (gint64
) clock_get_time_ns ();
678 req
.tv_sec
= diff
/ 1000000000;
679 req
.tv_nsec
= diff
% 1000000000;
681 if ((ret
= nanosleep (&req
, NULL
)) == -1 && errno
!= EINTR
)
682 g_error ("%s: nanosleep () returned -1, errno = %d", __func__
, errno
);
683 } while (ret
== -1 && mono_atomic_load_i32 (&sampling_thread_running
));
689 static int profiler_signal
;
690 static volatile gint32 sampling_thread_exiting
;
691 static MonoOSEvent sampling_thread_exited
;
694 sampling_thread_func (gpointer unused
)
696 MonoInternalThread
*thread
= mono_thread_internal_current ();
698 thread
->flags
|= MONO_THREAD_FLAG_DONT_MANAGE
;
700 mono_thread_set_name_constant_ignore_error (thread
, "Profiler Sampler", MonoSetThreadNameFlag_None
);
702 mono_thread_info_set_flags (MONO_THREAD_INFO_FLAGS_NO_GC
| MONO_THREAD_INFO_FLAGS_NO_SAMPLE
);
705 struct sched_param old_sched
;
706 pthread_getschedparam (pthread_self (), &old_policy
, &old_sched
);
709 * Attempt to switch the thread to real time scheduling. This will not
710 * necessarily work on all OSs; for example, most Linux systems will give
711 * us EPERM here unless configured to allow this.
713 * TODO: This does not work on Mac (and maybe some other OSs). On Mac, we
714 * have to use the Mach thread policy routines to switch to real-time
715 * scheduling. This is quite tricky as we need to specify how often we'll
716 * be doing work (easy), the normal processing time needed (also easy),
717 * and the maximum amount of processing time needed (hard). This is
718 * further complicated by the fact that if we misbehave and take too long
719 * to do our work, the kernel may knock us back down to the normal thread
720 * scheduling policy without telling us.
722 struct sched_param sched
;
723 memset (&sched
, 0, sizeof (sched
));
724 sched
.sched_priority
= sched_get_priority_max (SCHED_FIFO
);
725 pthread_setschedparam (pthread_self (), SCHED_FIFO
, &sched
);
727 MonoProfilerSampleMode mode
;
730 mono_profiler_get_sample_mode (NULL
, &mode
, NULL
);
732 if (mode
== MONO_PROFILER_SAMPLE_MODE_NONE
) {
733 mono_profiler_sampling_thread_wait ();
735 if (!mono_atomic_load_i32 (&sampling_thread_running
))
743 for (guint64 sleep
= clock_get_time_ns (); mono_atomic_load_i32 (&sampling_thread_running
); clock_sleep_ns_abs (sleep
)) {
745 MonoProfilerSampleMode new_mode
;
747 mono_profiler_get_sample_mode (NULL
, &new_mode
, &freq
);
749 if (new_mode
!= mode
) {
754 sleep
+= 1000000000 / freq
;
756 FOREACH_THREAD_SAFE_EXCLUDE (info
, MONO_THREAD_INFO_FLAGS_NO_SAMPLE
) {
757 g_assert (mono_thread_info_get_tid (info
) != sampling_thread
);
760 * Require an ack for the last sampling signal sent to the thread
761 * so that we don't overflow the signal queue, leading to all sorts
762 * of problems (e.g. GC STW failing).
764 if (profiler_signal
!= SIGPROF
&& !mono_atomic_cas_i32 (&info
->profiler_signal_ack
, 0, 1))
767 mono_threads_pthread_kill (info
, profiler_signal
);
768 mono_atomic_inc_i32 (&profiler_signals_sent
);
769 } FOREACH_THREAD_SAFE_END
775 mono_atomic_store_i32 (&sampling_thread_exiting
, 1);
777 pthread_setschedparam (pthread_self (), old_policy
, &old_sched
);
779 mono_thread_info_set_flags (MONO_THREAD_INFO_FLAGS_NONE
);
781 mono_os_event_set (&sampling_thread_exited
);
787 mono_runtime_shutdown_stat_profiler (void)
789 mono_atomic_store_i32 (&sampling_thread_running
, 0);
791 mono_profiler_sampling_thread_post ();
795 * There is a slight problem when we're using CLOCK_PROCESS_CPUTIME_ID: If
796 * we're shutting down and there's largely no activity in the process other
797 * than waiting for the sampler thread to shut down, it can take upwards of
798 * 20 seconds (depending on a lot of factors) for us to shut down because
799 * the sleep progresses very slowly as a result of the low CPU activity.
801 * We fix this by repeatedly sending the profiler signal to the sampler
802 * thread in order to interrupt the sleep. clock_sleep_ns_abs () will check
803 * sampling_thread_running upon an interrupt and return immediately if it's
804 * zero. profiler_signal_handler () has a special case to ignore the signal
805 * for the sampler thread.
807 MonoThreadInfo
*info
;
809 // Did it shut down already?
810 if ((info
= mono_thread_info_lookup (sampling_thread
))) {
811 while (!mono_atomic_load_i32 (&sampling_thread_exiting
)) {
812 mono_threads_pthread_kill (info
, profiler_signal
);
813 mono_thread_info_usleep (10 * 1000 /* 10ms */);
816 // Make sure info can be freed.
817 mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
821 mono_os_event_wait_one (&sampling_thread_exited
, MONO_INFINITE_WAIT
, FALSE
);
822 mono_os_event_destroy (&sampling_thread_exited
);
825 * We can't safely remove the signal handler because we have no guarantee
826 * that all pending signals have been delivered at this point. This should
827 * not really be a problem anyway.
829 //remove_signal_handler (profiler_signal);
833 mono_runtime_setup_stat_profiler (void)
836 * Use a real-time signal when possible. This gives us roughly a 99% signal
837 * delivery rate in all cases. On the other hand, using a regular signal
838 * tends to result in awful delivery rates when the application is heavily
841 * We avoid real-time signals on Android as they're super broken in certain
842 * API levels (too small sigset_t, nonsensical SIGRTMIN/SIGRTMAX values,
845 * TODO: On Mac, we should explore using the Mach thread suspend/resume
846 * functions and doing the stack walk from the sampling thread. This would
847 * get us a 100% sampling rate. However, this may interfere with the GC's
848 * STW logic. Could perhaps be solved by taking the suspend lock.
850 #if defined (USE_POSIX_BACKEND) && defined (SIGRTMIN) && !defined (HOST_ANDROID)
851 /* Just take the first real-time signal we can get. */
852 profiler_signal
= mono_threads_suspend_search_alternative_signal ();
854 profiler_signal
= SIGPROF
;
857 add_signal_handler (profiler_signal
, profiler_signal_handler
, SA_RESTART
);
859 mono_counters_register ("Sampling signals sent", MONO_COUNTER_UINT
| MONO_COUNTER_PROFILER
| MONO_COUNTER_MONOTONIC
, &profiler_signals_sent
);
860 mono_counters_register ("Sampling signals received", MONO_COUNTER_UINT
| MONO_COUNTER_PROFILER
| MONO_COUNTER_MONOTONIC
, &profiler_signals_received
);
861 mono_counters_register ("Sampling signals accepted", MONO_COUNTER_UINT
| MONO_COUNTER_PROFILER
| MONO_COUNTER_MONOTONIC
, &profiler_signals_accepted
);
862 mono_counters_register ("Shutdown signals received", MONO_COUNTER_UINT
| MONO_COUNTER_PROFILER
| MONO_COUNTER_MONOTONIC
, &profiler_interrupt_signals_received
);
864 mono_os_event_init (&sampling_thread_exited
, FALSE
);
866 mono_atomic_store_i32 (&sampling_thread_running
, 1);
869 MonoInternalThread
*thread
= mono_thread_create_internal (mono_get_root_domain (), (gpointer
)sampling_thread_func
, NULL
, MONO_THREAD_CREATE_FLAGS_NONE
, error
);
870 mono_error_assert_ok (error
);
872 sampling_thread
= MONO_UINT_TO_NATIVE_THREAD_ID (thread
->tid
);
878 mono_runtime_shutdown_stat_profiler (void)
883 mono_runtime_setup_stat_profiler (void)
889 #endif /* defined(HOST_WATCHOS) */
891 #ifndef MONO_CROSS_COMPILE
893 dump_memory_around_ip (MonoContext
*mctx
)
898 g_async_safe_printf ("\n=================================================================\n");
899 g_async_safe_printf ("\tBasic Fault Address Reporting\n");
900 g_async_safe_printf ("=================================================================\n");
902 gpointer native_ip
= MONO_CONTEXT_GET_IP (mctx
);
904 g_async_safe_printf ("Memory around native instruction pointer (%p):", native_ip
);
905 mono_dump_mem (((guint8
*) native_ip
) - 0x10, 0x40);
907 g_async_safe_printf ("instruction pointer is NULL, skip dumping");
912 assert_printer_callback (void)
914 mono_dump_native_crash_info ("SIGABRT", NULL
, NULL
);
918 dump_native_stacktrace (const char *signal
, MonoContext
*mctx
)
920 mono_memory_barrier ();
921 static gint32 middle_of_crash
= 0x0;
922 gint32 double_faulted
= mono_atomic_cas_i32 ((gint32
*)&middle_of_crash
, 0x1, 0x0);
923 mono_memory_write_barrier ();
925 if (!double_faulted
) {
926 g_assertion_disable_global (assert_printer_callback
);
928 g_async_safe_printf ("\nAn error has occured in the native fault reporting. Some diagnostic information will be unavailable.\n");
930 #ifndef DISABLE_CRASH_REPORTING
931 // In case still enabled
932 mono_summarize_toggle_assertions (FALSE
);
936 #ifdef HAVE_BACKTRACE_SYMBOLS
939 int size
= backtrace (array
, 256);
941 g_async_safe_printf ("\n=================================================================\n");
942 g_async_safe_printf ("\tNative stacktrace:\n");
943 g_async_safe_printf ("=================================================================\n");
945 g_async_safe_printf ("\t (No frames) \n\n");
947 for (int i
= 0; i
< size
; ++i
) {
948 gpointer ip
= array
[i
];
949 char sname
[256], fname
[256];
950 gboolean success
= g_module_address ((void*)ip
, fname
, 256, NULL
, sname
, 256, NULL
);
952 g_async_safe_printf ("\t%p - Unknown\n", ip
);
954 g_async_safe_printf ("\t%p - %s : %s\n", ip
, fname
, sname
);
958 #if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && (defined(SYS_fork) || HAVE_FORK)
959 if (!mini_debug_options
.no_gdb_backtrace
) {
960 /* From g_spawn_command_line_sync () in eglib */
963 pid_t crashed_pid
= getpid ();
965 #ifndef DISABLE_CRASH_REPORTING
966 gchar
*output
= NULL
;
967 MonoStackHash hashes
;
968 MonoStateMem merp_mem
;
969 memset (&merp_mem
, 0, sizeof (merp_mem
));
971 if (!double_faulted
) {
972 gboolean leave
= FALSE
;
973 gboolean dump_for_merp
= FALSE
;
974 #if defined(TARGET_OSX)
975 dump_for_merp
= mono_merp_enabled ();
978 #ifndef DISABLE_STRUCTURED_CRASH
979 mini_register_sigterm_handler ();
982 if (!dump_for_merp
) {
983 #ifdef DISABLE_STRUCTURED_CRASH
988 MonoContext
*passed_ctx
= NULL
;
989 if (!leave
&& mctx
) {
993 g_async_safe_printf ("\n=================================================================\n");
994 g_async_safe_printf ("\tTelemetry Dumper:\n");
995 g_async_safe_printf ("=================================================================\n");
998 mono_summarize_timeline_start ();
999 mono_summarize_toggle_assertions (TRUE
);
1001 int mono_max_summary_len
= 500000;
1002 int mono_state_tmp_file_tag
= 1;
1003 mono_state_alloc_mem (&merp_mem
, mono_state_tmp_file_tag
, mono_max_summary_len
* sizeof (gchar
));
1005 // Returns success, so leave if !success
1006 leave
= !mono_threads_summarize (passed_ctx
, &output
, &hashes
, FALSE
, TRUE
, (gchar
*) merp_mem
.mem
, mono_max_summary_len
);
1010 // Wait for the other threads to clean up and exit their handlers
1011 // We can't lock / wait indefinitely, in case one of these threads got stuck somehow
1013 g_async_safe_printf ("\nWaiting for dumping threads to resume\n");
1017 // We want our crash, and don't have telemetry
1018 // So we dump to disk
1019 if (!leave
&& !dump_for_merp
) {
1020 mono_summarize_timeline_phase_log (MonoSummaryCleanup
);
1021 mono_crash_dump (output
, &hashes
);
1022 mono_summarize_timeline_phase_log (MonoSummaryDone
);
1023 mono_summarize_toggle_assertions (FALSE
);
1026 #endif // DISABLE_CRASH_REPORTING
1029 * glibc fork acquires some locks, so if the crash happened inside malloc/free,
1030 * it will deadlock. Call the syscall directly instead.
1032 #if defined(HOST_ANDROID)
1033 /* SYS_fork is defined to be __NR_fork which is not defined in some ndk versions */
1034 g_assert_not_reached ();
1035 #elif !defined(HOST_DARWIN) && defined(SYS_fork)
1036 pid
= (pid_t
) syscall (SYS_fork
);
1038 pid
= (pid_t
) fork ();
1040 g_assert_not_reached ();
1043 #if defined (HAVE_PRCTL) && defined(PR_SET_PTRACER)
1045 // Allow gdb to attach to the process even if ptrace_scope sysctl variable is set to
1046 // a value other than 0 (the most permissive ptrace scope). Most modern Linux
1047 // distributions set the scope to 1 which allows attaching only to direct children of
1048 // the current process
1049 prctl (PR_SET_PTRACER
, pid
, 0, 0, 0);
1053 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
1054 if (!double_faulted
&& mono_merp_enabled ()) {
1057 gboolean merp_upload_success
= mono_merp_invoke (crashed_pid
, signal
, output
, &hashes
);
1059 if (!merp_upload_success
) {
1060 g_async_safe_printf("\nThe MERP upload step has failed.\n");
1063 g_async_safe_printf("\nThe MERP upload step has succeeded.\n");
1064 mono_summarize_timeline_phase_log (MonoSummaryDone
);
1066 mono_summarize_toggle_assertions (FALSE
);
1068 g_async_safe_printf("\nMerp dump step not run, no dump created.\n");
1075 dup2 (STDERR_FILENO
, STDOUT_FILENO
);
1077 g_async_safe_printf ("\n=================================================================\n");
1078 g_async_safe_printf("\tExternal Debugger Dump:\n");
1079 g_async_safe_printf ("=================================================================\n");
1080 mono_gdb_render_native_backtraces (crashed_pid
);
1082 } else if (pid
> 0) {
1083 waitpid (pid
, &status
, 0);
1085 // If we can't fork, do as little as possible before exiting
1086 #ifndef DISABLE_CRASH_REPORTING
1091 if (double_faulted
) {
1092 g_async_safe_printf("\nExiting early due to double fault.\n");
1093 #ifndef DISABLE_CRASH_REPORTING
1094 mono_state_free_mem (&merp_mem
);
1099 #ifndef DISABLE_CRASH_REPORTING
1101 // We've already done our gdb dump and our telemetry steps. Before exiting,
1102 // see if we can notify any attached debugger instances.
1104 // At this point we are accepting that the below step might end in a crash
1105 mini_get_dbg_callbacks ()->send_crash (output
, &hashes
, 0 /* wait # seconds */);
1108 mono_state_free_mem (&merp_mem
);
1115 /* set DUMPABLE for this process so debuggerd can attach with ptrace(2), see:
1116 * https://android.googlesource.com/platform/bionic/+/151da681000c07da3c24cd30a3279b1ca017f452/linker/debugger.cpp#206
1117 * this has changed on later versions of Android. Also, we don't want to
1118 * set this on start-up as DUMPABLE has security implications. */
1119 prctl (PR_SET_DUMPABLE
, 1);
1121 g_async_safe_printf("\nNo native Android stacktrace (see debuggerd output).\n");
1127 mono_dump_native_crash_info (const char *signal
, MonoContext
*mctx
, MONO_SIG_HANDLER_INFO_TYPE
*info
)
1129 dump_native_stacktrace (signal
, mctx
);
1130 dump_memory_around_ip (mctx
);
1134 mono_post_native_crash_handler (const char *signal
, MonoContext
*mctx
, MONO_SIG_HANDLER_INFO_TYPE
*info
, gboolean crash_chaining
)
1136 if (!crash_chaining
) {
1137 /*Android abort is a fluke, it doesn't abort, it triggers another segv. */
1138 #if defined (HOST_ANDROID)
1145 #endif /* !MONO_CROSS_COMPILE */
1147 static gchar
*gdb_path
;
1148 static gchar
*lldb_path
;
1151 mono_init_native_crash_info (void)
1153 gdb_path
= g_find_program_in_path ("gdb");
1154 lldb_path
= g_find_program_in_path ("lldb");
1155 mono_threads_summarize_init ();
1159 mono_cleanup_native_crash_info (void)
1166 native_stack_with_gdb (pid_t crashed_pid
, const char **argv
, int commands
, char* commands_filename
)
1171 argv
[0] = gdb_path
;
1172 argv
[1] = "-batch";
1174 argv
[3] = commands_filename
;
1177 g_async_safe_fprintf (commands
, "attach %ld\n", (long) crashed_pid
);
1178 g_async_safe_fprintf (commands
, "info threads\n");
1179 g_async_safe_fprintf (commands
, "thread apply all bt\n");
1180 if (mini_debug_options
.verbose_gdb
) {
1181 for (int i
= 0; i
< 32; ++i
) {
1182 g_async_safe_fprintf (commands
, "info registers\n");
1183 g_async_safe_fprintf (commands
, "info frame\n");
1184 g_async_safe_fprintf (commands
, "info locals\n");
1185 g_async_safe_fprintf (commands
, "up\n");
1194 native_stack_with_lldb (pid_t crashed_pid
, const char **argv
, int commands
, char* commands_filename
)
1199 argv
[0] = lldb_path
;
1200 argv
[1] = "--batch";
1201 argv
[2] = "--source";
1202 argv
[3] = commands_filename
;
1203 argv
[4] = "--no-lldbinit";
1205 g_async_safe_fprintf (commands
, "process attach --pid %ld\n", (long) crashed_pid
);
1206 g_async_safe_fprintf (commands
, "thread list\n");
1207 g_async_safe_fprintf (commands
, "thread backtrace all\n");
1208 if (mini_debug_options
.verbose_gdb
) {
1209 for (int i
= 0; i
< 32; ++i
) {
1210 g_async_safe_fprintf (commands
, "reg read\n");
1211 g_async_safe_fprintf (commands
, "frame info\n");
1212 g_async_safe_fprintf (commands
, "frame variable\n");
1213 g_async_safe_fprintf (commands
, "up\n");
1216 g_async_safe_fprintf (commands
, "detach\n");
1217 g_async_safe_fprintf (commands
, "quit\n");
1223 mono_gdb_render_native_backtraces (pid_t crashed_pid
)
1226 const char *argv
[10];
1227 memset (argv
, 0, sizeof (char*) * 10);
1229 char commands_filename
[100];
1230 commands_filename
[0] = '\0';
1231 g_snprintf (commands_filename
, sizeof (commands_filename
), "/tmp/mono-gdb-commands.%d", crashed_pid
);
1233 // Create this file, overwriting if it already exists
1234 int commands_handle
= g_open (commands_filename
, O_TRUNC
| O_WRONLY
| O_CREAT
, S_IWUSR
| S_IRUSR
| S_IRGRP
| S_IROTH
);
1235 if (commands_handle
== -1) {
1236 g_async_safe_printf ("Could not make debugger temp file %s\n", commands_filename
);
1240 #if defined(HOST_DARWIN)
1241 // lldb hangs on attaching on Catalina
1243 //if (native_stack_with_lldb (crashed_pid, argv, commands_handle, commands_filename))
1247 if (native_stack_with_gdb (crashed_pid
, argv
, commands_handle
, commands_filename
))
1250 #if !defined(HOST_DARWIN)
1251 if (native_stack_with_lldb (crashed_pid
, argv
, commands_handle
, commands_filename
))
1255 g_async_safe_printf ("mono_gdb_render_native_backtraces not supported on this platform, unable to find gdb or lldb\n");
1257 close (commands_handle
);
1258 unlink (commands_filename
);
1262 close (commands_handle
);
1263 execv (argv
[0], (char**)argv
);
1267 g_async_safe_printf ("mono_gdb_render_native_backtraces not supported on this platform\n");
1268 #endif // HAVE_EXECV
1271 #if !defined (__MACH__)
1274 mono_thread_state_init_from_handle (MonoThreadUnwindState
*tctx
, MonoThreadInfo
*info
, void *sigctx
)
1276 g_error ("Posix systems don't support mono_thread_state_init_from_handle");