Revert some changes which don't have proper dependencies.
[mono-project.git] / mono / mini / mini-posix.c
blobc0bf939d3070fdf2b592c1a4de2dce07b75953fa
1 /**
2 * \file
3 * POSIX signal handling support for Mono.
5 * Authors:
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.
15 #include <config.h>
16 #include <signal.h>
17 #ifdef HAVE_ALLOCA_H
18 #include <alloca.h>
19 #endif
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
23 #ifdef HAVE_EXECINFO_H
24 #include <execinfo.h>
25 #endif
26 #include <math.h>
27 #ifdef HAVE_SYS_TIME_H
28 #include <sys/time.h>
29 #endif
30 #ifdef HAVE_SYS_SYSCALL_H
31 #include <sys/syscall.h>
32 #endif
33 #ifdef HAVE_SYS_PRCTL_H
34 #include <sys/prctl.h>
35 #endif
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h>
38 #endif
39 #include <errno.h>
40 #include <sched.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>
75 #include "mini.h"
76 #include <string.h>
77 #include <ctype.h>
78 #include "trace.h"
79 #include "version.h"
80 #include "debugger-agent.h"
81 #include "mini-runtime.h"
82 #include "jit-icalls.h"
84 #ifdef HOST_DARWIN
85 #include <mach/mach.h>
86 #include <mach/mach_time.h>
87 #include <mach/clock.h>
88 #include <mono/utils/mono-merp.h>
89 #endif
91 #ifndef HOST_WIN32
92 #include <mono/utils/mono-threads-debug.h>
93 #endif
95 #include <fcntl.h>
96 #ifndef HOST_WIN32
97 #include <dlfcn.h>
98 #endif
99 #if HAVE_SYS_STAT_H
100 #include <sys/stat.h>
101 #endif
103 #if defined(HOST_WATCHOS)
105 void
106 mono_runtime_setup_stat_profiler (void)
108 printf("WARNING: mono_runtime_setup_stat_profiler() called!\n");
112 void
113 mono_runtime_shutdown_stat_profiler (void)
118 gboolean
119 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
121 return FALSE;
124 #ifndef HOST_DARWIN
125 void
126 mono_runtime_install_handlers (void)
129 #endif
131 void
132 mono_runtime_posix_install_handlers(void)
134 /* we still need to ignore SIGPIPE */
135 signal (SIGPIPE, SIG_IGN);
138 void
139 mono_runtime_shutdown_handlers (void)
143 void
144 mono_runtime_cleanup_handlers (void)
148 #else
150 static GHashTable *mono_saved_signal_handlers = NULL;
152 static struct sigaction *
153 get_saved_signal_handler (int signo, gboolean remove)
155 if (mono_saved_signal_handlers) {
156 /* The hash is only modified during startup, so no need for locking */
157 struct sigaction *handler = (struct sigaction*)g_hash_table_lookup (mono_saved_signal_handlers, GINT_TO_POINTER (signo));
158 if (remove && handler)
159 g_hash_table_remove (mono_saved_signal_handlers, GINT_TO_POINTER (signo));
160 return handler;
162 return NULL;
165 static void
166 save_old_signal_handler (int signo, struct sigaction *old_action)
168 struct sigaction *handler_to_save = (struct sigaction *)g_malloc (sizeof (struct sigaction));
170 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_CONFIG,
171 "Saving old signal handler for signal %d.", signo);
173 if (! (old_action->sa_flags & SA_SIGINFO)) {
174 handler_to_save->sa_handler = old_action->sa_handler;
175 } else {
176 #ifdef MONO_ARCH_USE_SIGACTION
177 handler_to_save->sa_sigaction = old_action->sa_sigaction;
178 #endif /* MONO_ARCH_USE_SIGACTION */
180 handler_to_save->sa_mask = old_action->sa_mask;
181 handler_to_save->sa_flags = old_action->sa_flags;
183 if (!mono_saved_signal_handlers)
184 mono_saved_signal_handlers = g_hash_table_new_full (NULL, NULL, NULL, g_free);
185 g_hash_table_insert (mono_saved_signal_handlers, GINT_TO_POINTER (signo), handler_to_save);
188 static void
189 free_saved_signal_handlers (void)
191 g_hash_table_destroy (mono_saved_signal_handlers);
192 mono_saved_signal_handlers = NULL;
196 * mono_chain_signal:
198 * Call the original signal handler for the signal given by the arguments, which
199 * should be the same as for a signal handler. Returns TRUE if the original handler
200 * was called, false otherwise.
202 gboolean
203 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
205 int signal = MONO_SIG_HANDLER_GET_SIGNO ();
206 struct sigaction *saved_handler = (struct sigaction *)get_saved_signal_handler (signal, FALSE);
208 if (saved_handler && saved_handler->sa_handler) {
209 if (!(saved_handler->sa_flags & SA_SIGINFO)) {
210 saved_handler->sa_handler (signal);
211 } else {
212 #ifdef MONO_ARCH_USE_SIGACTION
213 saved_handler->sa_sigaction (MONO_SIG_HANDLER_PARAMS);
214 #endif /* MONO_ARCH_USE_SIGACTION */
216 return TRUE;
218 return FALSE;
221 MONO_SIG_HANDLER_FUNC (static, sigabrt_signal_handler)
223 MonoJitInfo *ji = NULL;
224 MonoContext mctx;
225 MONO_SIG_HANDLER_INFO_TYPE *info = MONO_SIG_HANDLER_GET_INFO ();
226 MONO_SIG_HANDLER_GET_CONTEXT;
228 if (mono_thread_internal_current ())
229 ji = mono_jit_info_table_find_internal (mono_domain_get (), mono_arch_ip_from_context (ctx), TRUE, TRUE);
230 if (!ji) {
231 if (mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
232 return;
233 mono_sigctx_to_monoctx (ctx, &mctx);
234 mono_handle_native_crash ("SIGABRT", &mctx, info);
238 MONO_SIG_HANDLER_FUNC (static, sigterm_signal_handler)
240 #ifndef DISABLE_CRASH_REPORTING
241 MONO_SIG_HANDLER_GET_CONTEXT;
243 // Note: this is only run from the non-controlling thread
244 MonoContext mctx;
245 gchar *output = NULL;
246 MonoStackHash hashes;
247 mono_sigctx_to_monoctx (ctx, &mctx);
249 // Will return when the dumping is done, so this thread can continue
250 // running. Returns FALSE on unrecoverable error.
251 if (!mono_threads_summarize_execute (&mctx, &output, &hashes, FALSE, NULL, 0))
252 g_error ("Crash reporter dumper exited due to fatal error.");
253 #endif
255 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
258 #if (defined (USE_POSIX_BACKEND) && defined (SIGRTMIN)) || defined (SIGPROF)
259 #define HAVE_PROFILER_SIGNAL
260 #endif
262 #ifdef HAVE_PROFILER_SIGNAL
264 static MonoNativeThreadId sampling_thread;
266 static gint32 profiler_signals_sent;
267 static gint32 profiler_signals_received;
268 static gint32 profiler_signals_accepted;
269 static gint32 profiler_interrupt_signals_received;
271 MONO_SIG_HANDLER_FUNC (static, profiler_signal_handler)
273 int old_errno = errno;
275 MONO_SIG_HANDLER_GET_CONTEXT;
277 /* See the comment in mono_runtime_shutdown_stat_profiler (). */
278 if (mono_native_thread_id_get () == sampling_thread) {
279 mono_atomic_inc_i32 (&profiler_interrupt_signals_received);
280 return;
283 mono_atomic_inc_i32 (&profiler_signals_received);
285 // Did a non-attached or detaching thread get the signal?
286 if (mono_thread_info_get_small_id () == -1 ||
287 !mono_domain_get () ||
288 !mono_tls_get_jit_tls ()) {
289 mono_set_errno (old_errno);
290 return;
293 // See the comment in sampling_thread_func ().
294 mono_atomic_store_i32 (&mono_thread_info_current ()->profiler_signal_ack, 1);
296 mono_atomic_inc_i32 (&profiler_signals_accepted);
298 int hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
300 mono_thread_info_set_is_async_context (TRUE);
302 MONO_PROFILER_RAISE (sample_hit, ((const mono_byte*)mono_arch_ip_from_context (ctx), ctx));
304 mono_thread_info_set_is_async_context (FALSE);
306 mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
308 mono_set_errno (old_errno);
310 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
313 #endif
315 MONO_SIG_HANDLER_FUNC (static, sigquit_signal_handler)
317 gboolean res;
319 /* We use this signal to start the attach agent too */
320 res = mono_attach_start ();
321 if (res)
322 return;
324 mono_threads_request_thread_dump ();
326 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
329 MONO_SIG_HANDLER_FUNC (static, sigusr2_signal_handler)
331 gboolean enabled = mono_trace_is_enabled ();
333 mono_trace_enable (!enabled);
335 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
338 typedef void MONO_SIG_HANDLER_SIGNATURE ((*MonoSignalHandler));
340 static void
341 add_signal_handler (int signo, MonoSignalHandler handler, int flags)
343 struct sigaction sa;
344 struct sigaction previous_sa;
346 #ifdef MONO_ARCH_USE_SIGACTION
347 sa.sa_sigaction = handler;
348 sigemptyset (&sa.sa_mask);
349 sa.sa_flags = SA_SIGINFO | flags;
350 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
352 /*Apple likes to deliver SIGBUS for *0 */
353 #ifdef HOST_DARWIN
354 if (signo == SIGSEGV || signo == SIGBUS) {
355 #else
356 if (signo == SIGSEGV) {
357 #endif
358 sa.sa_flags |= SA_ONSTACK;
361 * libgc will crash when trying to do stack marking for threads which are on
362 * an altstack, so delay the suspend signal after the signal handler has
363 * executed.
365 if (mono_gc_get_suspend_signal () != -1)
366 sigaddset (&sa.sa_mask, mono_gc_get_suspend_signal ());
368 #endif
369 if (signo == SIGSEGV) {
371 * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
373 sigset_t block_mask;
375 sigemptyset (&block_mask);
377 #else
378 sa.sa_handler = (void (*)(int))handler;
379 sigemptyset (&sa.sa_mask);
380 sa.sa_flags = flags;
381 #endif
382 g_assert (sigaction (signo, &sa, &previous_sa) != -1);
384 /* if there was already a handler in place for this signal, store it */
385 if (! (previous_sa.sa_flags & SA_SIGINFO) &&
386 (SIG_DFL == previous_sa.sa_handler)) {
387 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
388 } else {
389 if (mono_do_signal_chaining)
390 save_old_signal_handler (signo, &previous_sa);
394 static void
395 remove_signal_handler (int signo)
397 struct sigaction sa;
398 struct sigaction *saved_action = get_saved_signal_handler (signo, TRUE);
400 if (!saved_action) {
401 sa.sa_handler = SIG_DFL;
402 sigemptyset (&sa.sa_mask);
403 sa.sa_flags = 0;
405 sigaction (signo, &sa, NULL);
406 } else {
407 g_assert (sigaction (signo, saved_action, NULL) != -1);
411 void
412 mini_register_sigterm_handler (void)
414 #ifndef DISABLE_CRASH_REPORTING
415 static gboolean enabled;
417 if (!enabled) {
418 enabled = TRUE;
420 /* always catch SIGTERM, conditionals inside of handler */
421 add_signal_handler (SIGTERM, sigterm_signal_handler, 0);
423 #endif
426 void
427 mono_runtime_posix_install_handlers (void)
430 sigset_t signal_set;
431 sigemptyset (&signal_set);
432 if (mini_get_debug_options ()->handle_sigint) {
433 add_signal_handler (SIGINT, mono_sigint_signal_handler, SA_RESTART);
434 sigaddset (&signal_set, SIGINT);
437 add_signal_handler (SIGFPE, mono_sigfpe_signal_handler, 0);
438 sigaddset (&signal_set, SIGFPE);
439 add_signal_handler (SIGQUIT, sigquit_signal_handler, SA_RESTART);
440 sigaddset (&signal_set, SIGQUIT);
441 add_signal_handler (SIGILL, mono_sigill_signal_handler, 0);
442 sigaddset (&signal_set, SIGILL);
443 add_signal_handler (SIGBUS, mono_sigsegv_signal_handler, 0);
444 sigaddset (&signal_set, SIGBUS);
445 if (mono_jit_trace_calls != NULL) {
446 add_signal_handler (SIGUSR2, sigusr2_signal_handler, SA_RESTART);
447 sigaddset (&signal_set, SIGUSR2);
450 /* it seems to have become a common bug for some programs that run as parents
451 * of many processes to block signal delivery for real time signals.
452 * We try to detect and work around their breakage here.
454 if (mono_gc_get_suspend_signal () != -1)
455 sigaddset (&signal_set, mono_gc_get_suspend_signal ());
456 if (mono_gc_get_restart_signal () != -1)
457 sigaddset (&signal_set, mono_gc_get_restart_signal ());
458 sigaddset (&signal_set, SIGCHLD);
460 signal (SIGPIPE, SIG_IGN);
461 sigaddset (&signal_set, SIGPIPE);
463 add_signal_handler (SIGABRT, sigabrt_signal_handler, 0);
464 sigaddset (&signal_set, SIGABRT);
466 /* catch SIGSEGV */
467 add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler, 0);
468 sigaddset (&signal_set, SIGSEGV);
470 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
473 #ifndef HOST_DARWIN
474 void
475 mono_runtime_install_handlers (void)
477 mono_runtime_posix_install_handlers ();
479 #endif
481 void
482 mono_runtime_cleanup_handlers (void)
484 if (mini_get_debug_options ()->handle_sigint)
485 remove_signal_handler (SIGINT);
487 remove_signal_handler (SIGFPE);
488 remove_signal_handler (SIGQUIT);
489 remove_signal_handler (SIGILL);
490 remove_signal_handler (SIGBUS);
491 if (mono_jit_trace_calls != NULL)
492 remove_signal_handler (SIGUSR2);
494 remove_signal_handler (SIGABRT);
496 remove_signal_handler (SIGSEGV);
498 free_saved_signal_handlers ();
501 #ifdef HAVE_PROFILER_SIGNAL
503 static volatile gint32 sampling_thread_running;
505 #ifdef HOST_DARWIN
507 static clock_serv_t sampling_clock_service;
509 static void
510 clock_init (MonoProfilerSampleMode mode)
512 kern_return_t ret;
514 do {
515 ret = host_get_clock_service (mach_host_self (), SYSTEM_CLOCK, &sampling_clock_service);
516 } while (ret == KERN_ABORTED);
518 if (ret != KERN_SUCCESS)
519 g_error ("%s: host_get_clock_service () returned %d", __func__, ret);
522 static void
523 clock_cleanup (void)
525 kern_return_t ret;
527 do {
528 ret = mach_port_deallocate (mach_task_self (), sampling_clock_service);
529 } while (ret == KERN_ABORTED);
531 if (ret != KERN_SUCCESS)
532 g_error ("%s: mach_port_deallocate () returned %d", __func__, ret);
535 static guint64
536 clock_get_time_ns (void)
538 kern_return_t ret;
539 mach_timespec_t mach_ts;
541 do {
542 ret = clock_get_time (sampling_clock_service, &mach_ts);
543 } while (ret == KERN_ABORTED);
545 if (ret != KERN_SUCCESS)
546 g_error ("%s: clock_get_time () returned %d", __func__, ret);
548 return ((guint64) mach_ts.tv_sec * 1000000000) + (guint64) mach_ts.tv_nsec;
551 static void
552 clock_sleep_ns_abs (guint64 ns_abs)
554 kern_return_t ret;
555 mach_timespec_t then, remain_unused;
557 then.tv_sec = ns_abs / 1000000000;
558 then.tv_nsec = ns_abs % 1000000000;
560 do {
561 ret = clock_sleep (sampling_clock_service, TIME_ABSOLUTE, then, &remain_unused);
563 if (ret != KERN_SUCCESS && ret != KERN_ABORTED)
564 g_error ("%s: clock_sleep () returned %d", __func__, ret);
565 } while (ret == KERN_ABORTED && mono_atomic_load_i32 (&sampling_thread_running));
568 #else
570 static clockid_t sampling_posix_clock;
572 static void
573 clock_init (MonoProfilerSampleMode mode)
575 switch (mode) {
576 case MONO_PROFILER_SAMPLE_MODE_PROCESS: {
578 * If we don't have clock_nanosleep (), measuring the process time
579 * makes very little sense as we can only use nanosleep () to sleep on
580 * real time.
582 #ifdef HAVE_CLOCK_NANOSLEEP
583 struct timespec ts = { 0 };
586 * Some systems (e.g. Windows Subsystem for Linux) declare the
587 * CLOCK_PROCESS_CPUTIME_ID clock but don't actually support it. For
588 * those systems, we fall back to CLOCK_MONOTONIC if we get EINVAL.
590 if (clock_nanosleep (CLOCK_PROCESS_CPUTIME_ID, TIMER_ABSTIME, &ts, NULL) != EINVAL) {
591 sampling_posix_clock = CLOCK_PROCESS_CPUTIME_ID;
592 break;
594 #endif
596 // fallthrough
598 case MONO_PROFILER_SAMPLE_MODE_REAL: sampling_posix_clock = CLOCK_MONOTONIC; break;
599 default: g_assert_not_reached (); break;
603 static void
604 clock_cleanup (void)
608 static guint64
609 clock_get_time_ns (void)
611 struct timespec ts;
613 if (clock_gettime (sampling_posix_clock, &ts) == -1)
614 g_error ("%s: clock_gettime () returned -1, errno = %d", __func__, errno);
616 return ((guint64) ts.tv_sec * 1000000000) + (guint64) ts.tv_nsec;
619 static void
620 clock_sleep_ns_abs (guint64 ns_abs)
622 #ifdef HAVE_CLOCK_NANOSLEEP
623 int ret;
624 struct timespec then;
626 then.tv_sec = ns_abs / 1000000000;
627 then.tv_nsec = ns_abs % 1000000000;
629 do {
630 ret = clock_nanosleep (sampling_posix_clock, TIMER_ABSTIME, &then, NULL);
632 if (ret != 0 && ret != EINTR)
633 g_error ("%s: clock_nanosleep () returned %d", __func__, ret);
634 } while (ret == EINTR && mono_atomic_load_i32 (&sampling_thread_running));
635 #else
636 int ret;
637 gint64 diff;
638 struct timespec req;
641 * What follows is a crude attempt at emulating clock_nanosleep () on OSs
642 * which don't provide it (e.g. FreeBSD).
644 * The problem with nanosleep () is that if it is interrupted by a signal,
645 * time will drift as a result of having to restart the call after the
646 * signal handler has finished. For this reason, we avoid using the rem
647 * argument of nanosleep (). Instead, before every nanosleep () call, we
648 * check if enough time has passed to satisfy the sleep request. If yes, we
649 * simply return. If not, we calculate the difference and do another sleep.
651 * This should reduce the amount of drift that happens because we account
652 * for the time spent executing the signal handler, which nanosleep () is
653 * not guaranteed to do for the rem argument.
655 * The downside to this approach is that it is slightly expensive: We have
656 * to make an extra system call to retrieve the current time whenever we're
657 * going to restart a nanosleep () call. This is unlikely to be a problem
658 * in practice since the sampling thread won't be receiving many signals in
659 * the first place (it's a tools thread, so no STW), and because typical
660 * sleep periods for the thread are many orders of magnitude bigger than
661 * the time it takes to actually perform that system call (just a few
662 * nanoseconds).
664 do {
665 diff = (gint64) ns_abs - (gint64) clock_get_time_ns ();
667 if (diff <= 0)
668 break;
670 req.tv_sec = diff / 1000000000;
671 req.tv_nsec = diff % 1000000000;
673 if ((ret = nanosleep (&req, NULL)) == -1 && errno != EINTR)
674 g_error ("%s: nanosleep () returned -1, errno = %d", __func__, errno);
675 } while (ret == -1 && mono_atomic_load_i32 (&sampling_thread_running));
676 #endif
679 #endif
681 static int profiler_signal;
682 static volatile gint32 sampling_thread_exiting;
683 static MonoOSEvent sampling_thread_exited;
685 static gsize
686 sampling_thread_func (gpointer unused)
688 MonoInternalThread *thread = mono_thread_internal_current ();
690 thread->flags |= MONO_THREAD_FLAG_DONT_MANAGE;
692 ERROR_DECL (error);
694 MonoString *name = mono_string_new_checked (mono_get_root_domain (), "Profiler Sampler", error);
695 mono_error_assert_ok (error);
696 mono_thread_set_name_internal (thread, name, FALSE, FALSE, error);
697 mono_error_assert_ok (error);
699 mono_thread_info_set_flags (MONO_THREAD_INFO_FLAGS_NO_GC | MONO_THREAD_INFO_FLAGS_NO_SAMPLE);
701 int old_policy;
702 struct sched_param old_sched;
703 pthread_getschedparam (pthread_self (), &old_policy, &old_sched);
706 * Attempt to switch the thread to real time scheduling. This will not
707 * necessarily work on all OSs; for example, most Linux systems will give
708 * us EPERM here unless configured to allow this.
710 * TODO: This does not work on Mac (and maybe some other OSs). On Mac, we
711 * have to use the Mach thread policy routines to switch to real-time
712 * scheduling. This is quite tricky as we need to specify how often we'll
713 * be doing work (easy), the normal processing time needed (also easy),
714 * and the maximum amount of processing time needed (hard). This is
715 * further complicated by the fact that if we misbehave and take too long
716 * to do our work, the kernel may knock us back down to the normal thread
717 * scheduling policy without telling us.
719 struct sched_param sched;
720 memset (&sched, 0, sizeof (sched));
721 sched.sched_priority = sched_get_priority_max (SCHED_FIFO);
722 pthread_setschedparam (pthread_self (), SCHED_FIFO, &sched);
724 MonoProfilerSampleMode mode;
726 init:
727 mono_profiler_get_sample_mode (NULL, &mode, NULL);
729 if (mode == MONO_PROFILER_SAMPLE_MODE_NONE) {
730 mono_profiler_sampling_thread_wait ();
732 if (!mono_atomic_load_i32 (&sampling_thread_running))
733 goto done;
735 goto init;
738 clock_init (mode);
740 for (guint64 sleep = clock_get_time_ns (); mono_atomic_load_i32 (&sampling_thread_running); clock_sleep_ns_abs (sleep)) {
741 uint32_t freq;
742 MonoProfilerSampleMode new_mode;
744 mono_profiler_get_sample_mode (NULL, &new_mode, &freq);
746 if (new_mode != mode) {
747 clock_cleanup ();
748 goto init;
751 sleep += 1000000000 / freq;
753 FOREACH_THREAD_SAFE_EXCLUDE (info, MONO_THREAD_INFO_FLAGS_NO_SAMPLE) {
754 g_assert (mono_thread_info_get_tid (info) != sampling_thread);
757 * Require an ack for the last sampling signal sent to the thread
758 * so that we don't overflow the signal queue, leading to all sorts
759 * of problems (e.g. GC STW failing).
761 if (profiler_signal != SIGPROF && !mono_atomic_cas_i32 (&info->profiler_signal_ack, 0, 1))
762 continue;
764 mono_threads_pthread_kill (info, profiler_signal);
765 mono_atomic_inc_i32 (&profiler_signals_sent);
766 } FOREACH_THREAD_SAFE_END
769 clock_cleanup ();
771 done:
772 mono_atomic_store_i32 (&sampling_thread_exiting, 1);
774 pthread_setschedparam (pthread_self (), old_policy, &old_sched);
776 mono_thread_info_set_flags (MONO_THREAD_INFO_FLAGS_NONE);
778 mono_os_event_set (&sampling_thread_exited);
780 return 0;
783 void
784 mono_runtime_shutdown_stat_profiler (void)
786 mono_atomic_store_i32 (&sampling_thread_running, 0);
788 mono_profiler_sampling_thread_post ();
790 #ifndef HOST_DARWIN
792 * There is a slight problem when we're using CLOCK_PROCESS_CPUTIME_ID: If
793 * we're shutting down and there's largely no activity in the process other
794 * than waiting for the sampler thread to shut down, it can take upwards of
795 * 20 seconds (depending on a lot of factors) for us to shut down because
796 * the sleep progresses very slowly as a result of the low CPU activity.
798 * We fix this by repeatedly sending the profiler signal to the sampler
799 * thread in order to interrupt the sleep. clock_sleep_ns_abs () will check
800 * sampling_thread_running upon an interrupt and return immediately if it's
801 * zero. profiler_signal_handler () has a special case to ignore the signal
802 * for the sampler thread.
804 MonoThreadInfo *info;
806 // Did it shut down already?
807 if ((info = mono_thread_info_lookup (sampling_thread))) {
808 while (!mono_atomic_load_i32 (&sampling_thread_exiting)) {
809 mono_threads_pthread_kill (info, profiler_signal);
810 mono_thread_info_usleep (10 * 1000 /* 10ms */);
813 // Make sure info can be freed.
814 mono_hazard_pointer_clear (mono_hazard_pointer_get (), 1);
816 #endif
818 mono_os_event_wait_one (&sampling_thread_exited, MONO_INFINITE_WAIT, FALSE);
819 mono_os_event_destroy (&sampling_thread_exited);
822 * We can't safely remove the signal handler because we have no guarantee
823 * that all pending signals have been delivered at this point. This should
824 * not really be a problem anyway.
826 //remove_signal_handler (profiler_signal);
829 void
830 mono_runtime_setup_stat_profiler (void)
833 * Use a real-time signal when possible. This gives us roughly a 99% signal
834 * delivery rate in all cases. On the other hand, using a regular signal
835 * tends to result in awful delivery rates when the application is heavily
836 * loaded.
838 * We avoid real-time signals on Android as they're super broken in certain
839 * API levels (too small sigset_t, nonsensical SIGRTMIN/SIGRTMAX values,
840 * etc).
842 * TODO: On Mac, we should explore using the Mach thread suspend/resume
843 * functions and doing the stack walk from the sampling thread. This would
844 * get us a 100% sampling rate. However, this may interfere with the GC's
845 * STW logic. Could perhaps be solved by taking the suspend lock.
847 #if defined (USE_POSIX_BACKEND) && defined (SIGRTMIN) && !defined (HOST_ANDROID)
848 /* Just take the first real-time signal we can get. */
849 profiler_signal = mono_threads_suspend_search_alternative_signal ();
850 #else
851 profiler_signal = SIGPROF;
852 #endif
854 add_signal_handler (profiler_signal, profiler_signal_handler, SA_RESTART);
856 mono_counters_register ("Sampling signals sent", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_signals_sent);
857 mono_counters_register ("Sampling signals received", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_signals_received);
858 mono_counters_register ("Sampling signals accepted", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_signals_accepted);
859 mono_counters_register ("Shutdown signals received", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_interrupt_signals_received);
861 mono_os_event_init (&sampling_thread_exited, FALSE);
863 mono_atomic_store_i32 (&sampling_thread_running, 1);
865 MonoError error;
866 MonoInternalThread *thread = mono_thread_create_internal (mono_get_root_domain (), (gpointer)sampling_thread_func, NULL, MONO_THREAD_CREATE_FLAGS_NONE, &error);
867 mono_error_assert_ok (&error);
869 sampling_thread = MONO_UINT_TO_NATIVE_THREAD_ID (thread->tid);
872 #else
874 void
875 mono_runtime_shutdown_stat_profiler (void)
879 void
880 mono_runtime_setup_stat_profiler (void)
884 #endif
886 #endif /* defined(HOST_WATCHOS) */
888 #ifndef MONO_CROSS_COMPILE
889 static void
890 dump_memory_around_ip (MonoContext *mctx)
892 if (!mctx)
893 return;
895 g_async_safe_printf ("\n=================================================================\n");
896 g_async_safe_printf ("\tBasic Fault Address Reporting\n");
897 g_async_safe_printf ("=================================================================\n");
899 gpointer native_ip = MONO_CONTEXT_GET_IP (mctx);
900 if (native_ip) {
901 g_async_safe_printf ("Memory around native instruction pointer (%p):", native_ip);
902 mono_dump_mem (((guint8 *) native_ip) - 0x10, 0x40);
903 } else {
904 g_async_safe_printf ("instruction pointer is NULL, skip dumping");
908 static void
909 assert_printer_callback (void)
911 mono_dump_native_crash_info ("SIGABRT", NULL, NULL);
914 static void
915 dump_native_stacktrace (const char *signal, MonoContext *mctx)
917 mono_memory_barrier ();
918 static gint32 middle_of_crash = 0x0;
919 gint32 double_faulted = mono_atomic_cas_i32 ((gint32 *)&middle_of_crash, 0x1, 0x0);
920 mono_memory_write_barrier ();
922 if (!double_faulted) {
923 g_assertion_disable_global (assert_printer_callback);
924 } else {
925 g_async_safe_printf ("\nAn error has occured in the native fault reporting. Some diagnostic information will be unavailable.\n");
927 #ifndef DISABLE_CRASH_REPORTING
928 // In case still enabled
929 mono_summarize_toggle_assertions (FALSE);
930 #endif
933 #ifdef HAVE_BACKTRACE_SYMBOLS
935 void *array [256];
936 int size = backtrace (array, 256);
938 g_async_safe_printf ("\n=================================================================\n");
939 g_async_safe_printf ("\tNative stacktrace:\n");
940 g_async_safe_printf ("=================================================================\n");
941 if (size == 0)
942 g_async_safe_printf ("\t (No frames) \n\n");
944 for (int i = 0; i < size; ++i) {
945 gpointer ip = array [i];
946 Dl_info info;
947 gboolean success = dladdr ((void*) ip, &info);
948 if (!success) {
949 g_async_safe_printf ("\t%p - Unknown\n", ip);
950 } else {
951 g_async_safe_printf ("\t%p - %s : %s\n", ip, info.dli_fname, info.dli_sname);
955 #if !defined(HOST_WIN32) && defined(HAVE_SYS_SYSCALL_H) && (defined(SYS_fork) || HAVE_FORK)
956 if (!mini_get_debug_options ()->no_gdb_backtrace) {
957 /* From g_spawn_command_line_sync () in eglib */
958 pid_t pid;
959 int status;
960 pid_t crashed_pid = getpid ();
961 gchar *output = NULL;
962 MonoStackHash hashes;
964 #ifndef DISABLE_CRASH_REPORTING
965 MonoStateMem merp_mem;
966 memset (&merp_mem, 0, sizeof (merp_mem));
968 if (!double_faulted) {
969 gboolean leave = FALSE;
970 gboolean dump_for_merp = FALSE;
971 #if defined(TARGET_OSX)
972 dump_for_merp = mono_merp_enabled ();
973 #endif
975 if (!dump_for_merp) {
976 #ifdef DISABLE_STRUCTURED_CRASH
977 leave = TRUE;
978 #else
979 mini_register_sigterm_handler ();
980 #endif
983 MonoContext *passed_ctx = NULL;
984 if (!leave && mctx) {
985 passed_ctx = mctx;
988 g_async_safe_printf ("\n=================================================================\n");
989 g_async_safe_printf ("\tTelemetry Dumper:\n");
990 g_async_safe_printf ("=================================================================\n");
992 if (!leave) {
993 mono_summarize_timeline_start ();
994 mono_summarize_toggle_assertions (TRUE);
996 int mono_max_summary_len = 500000;
997 int mono_state_tmp_file_tag = 1;
998 mono_state_alloc_mem (&merp_mem, mono_state_tmp_file_tag, mono_max_summary_len * sizeof (gchar));
1000 // Returns success, so leave if !success
1001 leave = !mono_threads_summarize (passed_ctx, &output, &hashes, FALSE, TRUE, (gchar *) merp_mem.mem, mono_max_summary_len);
1004 if (!leave) {
1005 // Wait for the other threads to clean up and exit their handlers
1006 // We can't lock / wait indefinitely, in case one of these threads got stuck somehow
1007 // while dumping.
1008 g_async_safe_printf ("\nWaiting for dumping threads to resume\n");
1009 sleep (1);
1012 // We want our crash, and don't have telemetry
1013 // So we dump to disk
1014 if (!leave && !dump_for_merp) {
1015 mono_summarize_timeline_phase_log (MonoSummaryCleanup);
1016 mono_crash_dump (output, &hashes);
1017 mono_summarize_timeline_phase_log (MonoSummaryDone);
1018 mono_summarize_toggle_assertions (FALSE);
1021 #endif // DISABLE_CRASH_REPORTING
1024 * glibc fork acquires some locks, so if the crash happened inside malloc/free,
1025 * it will deadlock. Call the syscall directly instead.
1027 #if defined(HOST_ANDROID)
1028 /* SYS_fork is defined to be __NR_fork which is not defined in some ndk versions */
1029 g_assert_not_reached ();
1030 #elif !defined(HOST_DARWIN) && defined(SYS_fork)
1031 pid = (pid_t) syscall (SYS_fork);
1032 #elif HAVE_FORK
1033 pid = (pid_t) fork ();
1034 #else
1035 g_assert_not_reached ();
1036 #endif
1038 #if defined (HAVE_PRCTL) && defined(PR_SET_PTRACER)
1039 if (pid > 0) {
1040 // Allow gdb to attach to the process even if ptrace_scope sysctl variable is set to
1041 // a value other than 0 (the most permissive ptrace scope). Most modern Linux
1042 // distributions set the scope to 1 which allows attaching only to direct children of
1043 // the current process
1044 prctl (PR_SET_PTRACER, pid, 0, 0, 0);
1046 #endif
1048 #if defined(TARGET_OSX) && !defined(DISABLE_CRASH_REPORTING)
1049 if (!double_faulted && mono_merp_enabled ()) {
1050 if (pid == 0) {
1051 if (output) {
1052 gboolean merp_upload_success = mono_merp_invoke (crashed_pid, signal, output, &hashes);
1054 if (!merp_upload_success) {
1055 g_async_safe_printf("\nThe MERP upload step has failed.\n");
1056 } else {
1057 // Remove
1058 g_async_safe_printf("\nThe MERP upload step has succeeded.\n");
1059 mono_summarize_timeline_phase_log (MonoSummaryDone);
1062 mono_summarize_toggle_assertions (FALSE);
1063 } else {
1064 g_async_safe_printf("\nMerp dump step not run, no dump created.\n");
1068 #endif
1070 if (pid == 0) {
1071 dup2 (STDERR_FILENO, STDOUT_FILENO);
1073 g_async_safe_printf ("\n=================================================================\n");
1074 g_async_safe_printf("\tExternal Debugger Dump:\n");
1075 g_async_safe_printf ("=================================================================\n");
1076 mono_gdb_render_native_backtraces (crashed_pid);
1077 _exit (1);
1078 } else if (pid > 0) {
1079 waitpid (pid, &status, 0);
1080 } else {
1081 // If we can't fork, do as little as possible before exiting
1082 #ifndef DISABLE_CRASH_REPORTING
1083 output = NULL;
1084 #endif
1087 if (double_faulted) {
1088 g_async_safe_printf("\nExiting early due to double fault.\n");
1089 #ifndef DISABLE_CRASH_REPORTING
1090 mono_state_free_mem (&merp_mem);
1091 #endif
1092 _exit (-1);
1095 #ifndef DISABLE_CRASH_REPORTING
1096 if (output) {
1097 // We've already done our gdb dump and our telemetry steps. Before exiting,
1098 // see if we can notify any attached debugger instances.
1100 // At this point we are accepting that the below step might end in a crash
1101 mini_get_dbg_callbacks ()->send_crash (output, &hashes, 0 /* wait # seconds */);
1103 output = NULL;
1104 mono_state_free_mem (&merp_mem);
1105 #endif
1108 #endif
1109 #else
1110 #ifdef HOST_ANDROID
1111 /* set DUMPABLE for this process so debuggerd can attach with ptrace(2), see:
1112 * https://android.googlesource.com/platform/bionic/+/151da681000c07da3c24cd30a3279b1ca017f452/linker/debugger.cpp#206
1113 * this has changed on later versions of Android. Also, we don't want to
1114 * set this on start-up as DUMPABLE has security implications. */
1115 prctl (PR_SET_DUMPABLE, 1);
1117 g_async_safe_printf("\nNo native Android stacktrace (see debuggerd output).\n");
1118 #endif
1119 #endif
1122 void
1123 mono_dump_native_crash_info (const char *signal, MonoContext *mctx, MONO_SIG_HANDLER_INFO_TYPE *info)
1125 dump_native_stacktrace (signal, mctx);
1127 dump_memory_around_ip (mctx);
1130 void
1131 mono_post_native_crash_handler (const char *signal, MonoContext *mctx, MONO_SIG_HANDLER_INFO_TYPE *info, gboolean crash_chaining)
1133 if (!crash_chaining) {
1134 /*Android abort is a fluke, it doesn't abort, it triggers another segv. */
1135 #if defined (HOST_ANDROID)
1136 exit (-1);
1137 #else
1138 abort ();
1139 #endif
1142 #endif /* !MONO_CROSS_COMPILE */
1144 static gchar *gdb_path;
1145 static gchar *lldb_path;
1147 void
1148 mono_init_native_crash_info (void)
1150 gdb_path = g_find_program_in_path ("gdb");
1151 lldb_path = g_find_program_in_path ("lldb");
1154 void
1155 mono_cleanup_native_crash_info (void)
1157 g_free (gdb_path);
1158 g_free (lldb_path);
1161 static gboolean
1162 native_stack_with_gdb (pid_t crashed_pid, const char **argv, int commands, char* commands_filename)
1164 if (!gdb_path)
1165 return FALSE;
1167 argv [0] = gdb_path;
1168 argv [1] = "-batch";
1169 argv [2] = "-x";
1170 argv [3] = commands_filename;
1171 argv [4] = "-nx";
1173 g_async_safe_fprintf (commands, "attach %ld\n", (long) crashed_pid);
1174 g_async_safe_fprintf (commands, "info threads\n");
1175 g_async_safe_fprintf (commands, "thread apply all bt\n");
1176 if (mini_get_debug_options ()->verbose_gdb) {
1177 for (int i = 0; i < 32; ++i) {
1178 g_async_safe_fprintf (commands, "info registers\n");
1179 g_async_safe_fprintf (commands, "info frame\n");
1180 g_async_safe_fprintf (commands, "info locals\n");
1181 g_async_safe_fprintf (commands, "up\n");
1185 return TRUE;
1189 static gboolean
1190 native_stack_with_lldb (pid_t crashed_pid, const char **argv, int commands, char* commands_filename)
1192 if (!lldb_path)
1193 return FALSE;
1195 argv [0] = lldb_path;
1196 argv [1] = "--batch";
1197 argv [2] = "--source";
1198 argv [3] = commands_filename;
1199 argv [4] = "--no-lldbinit";
1201 g_async_safe_fprintf (commands, "process attach --pid %ld\n", (long) crashed_pid);
1202 g_async_safe_fprintf (commands, "thread list\n");
1203 g_async_safe_fprintf (commands, "thread backtrace all\n");
1204 if (mini_get_debug_options ()->verbose_gdb) {
1205 for (int i = 0; i < 32; ++i) {
1206 g_async_safe_fprintf (commands, "reg read\n");
1207 g_async_safe_fprintf (commands, "frame info\n");
1208 g_async_safe_fprintf (commands, "frame variable\n");
1209 g_async_safe_fprintf (commands, "up\n");
1212 g_async_safe_fprintf (commands, "detach\n");
1213 g_async_safe_fprintf (commands, "quit\n");
1215 return TRUE;
1218 void
1219 mono_gdb_render_native_backtraces (pid_t crashed_pid)
1221 #ifdef HAVE_EXECV
1222 const char *argv [10];
1223 memset (argv, 0, sizeof (char*) * 10);
1225 char commands_filename [100];
1226 commands_filename [0] = '\0';
1227 g_snprintf (commands_filename, sizeof (commands_filename), "/tmp/mono-gdb-commands.%d", crashed_pid);
1229 // Create this file, overwriting if it already exists
1230 int commands_handle = g_open (commands_filename, O_TRUNC | O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
1231 if (commands_handle == -1) {
1232 g_async_safe_printf ("Could not make debugger temp file %s\n", commands_filename);
1233 return;
1236 #if defined(HOST_DARWIN)
1237 if (native_stack_with_lldb (crashed_pid, argv, commands_handle, commands_filename))
1238 goto exec;
1239 #endif
1241 if (native_stack_with_gdb (crashed_pid, argv, commands_handle, commands_filename))
1242 goto exec;
1244 #if !defined(HOST_DARWIN)
1245 if (native_stack_with_lldb (crashed_pid, argv, commands_handle, commands_filename))
1246 goto exec;
1247 #endif
1249 g_async_safe_printf ("mono_gdb_render_native_backtraces not supported on this platform, unable to find gdb or lldb\n");
1251 close (commands_handle);
1252 unlink (commands_filename);
1253 return;
1255 exec:
1256 close (commands_handle);
1257 execv (argv [0], (char**)argv);
1259 _exit (-1);
1260 #else
1261 g_async_safe_printf ("mono_gdb_render_native_backtraces not supported on this platform\n");
1262 #endif // HAVE_EXECV
1265 #if !defined (__MACH__)
1267 gboolean
1268 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info, void *sigctx)
1270 g_error ("Posix systems don't support mono_thread_state_init_from_handle");
1271 return FALSE;
1274 #endif