[profiler] Add counters for numbers of signals sent/received/accepted.
[mono-project.git] / mono / mini / mini-posix.c
blobdab2ac288f2cdf6a270d475e23c476c9c7a8fd68
1 /*
2 * mini-posix.c: POSIX signal handling support for Mono.
4 * Authors:
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.
14 #include <config.h>
15 #include <signal.h>
16 #ifdef HAVE_ALLOCA_H
17 #include <alloca.h>
18 #endif
19 #ifdef HAVE_UNISTD_H
20 #include <unistd.h>
21 #endif
22 #include <math.h>
23 #ifdef HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #endif
26 #ifdef HAVE_SYS_SYSCALL_H
27 #include <sys/syscall.h>
28 #endif
29 #include <errno.h>
30 #include <sched.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>
64 #include "mini.h"
65 #include <string.h>
66 #include <ctype.h>
67 #include "trace.h"
68 #include "version.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>
77 #endif
79 #if defined(__native_client__) || defined(HOST_WATCHOS)
81 void
82 mono_runtime_setup_stat_profiler (void)
84 printf("WARNING: mono_runtime_setup_stat_profiler() called!\n");
88 void
89 mono_runtime_shutdown_stat_profiler (void)
94 gboolean
95 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
97 return FALSE;
100 #ifndef PLATFORM_MACOSX
101 void
102 mono_runtime_install_handlers (void)
105 #endif
107 void
108 mono_runtime_posix_install_handlers(void)
113 void
114 mono_runtime_shutdown_handlers (void)
118 void
119 mono_runtime_cleanup_handlers (void)
123 #if !defined(PLATFORM_MACOSX)
124 pid_t
125 mono_runtime_syscall_fork (void)
127 g_assert_not_reached();
128 return 0;
131 void
132 mono_gdb_render_native_backtraces (pid_t crashed_pid)
135 #endif
137 #else
139 static GHashTable *mono_saved_signal_handlers = NULL;
141 static struct sigaction *
142 get_saved_signal_handler (int signo)
144 if (mono_saved_signal_handlers)
145 /* The hash is only modified during startup, so no need for locking */
146 return (struct sigaction *)g_hash_table_lookup (mono_saved_signal_handlers, GINT_TO_POINTER (signo));
147 return NULL;
150 static void
151 save_old_signal_handler (int signo, struct sigaction *old_action)
153 struct sigaction *handler_to_save = (struct sigaction *)g_malloc (sizeof (struct sigaction));
155 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_CONFIG,
156 "Saving old signal handler for signal %d.", signo);
158 if (! (old_action->sa_flags & SA_SIGINFO)) {
159 handler_to_save->sa_handler = old_action->sa_handler;
160 } else {
161 #ifdef MONO_ARCH_USE_SIGACTION
162 handler_to_save->sa_sigaction = old_action->sa_sigaction;
163 #endif /* MONO_ARCH_USE_SIGACTION */
165 handler_to_save->sa_mask = old_action->sa_mask;
166 handler_to_save->sa_flags = old_action->sa_flags;
168 if (!mono_saved_signal_handlers)
169 mono_saved_signal_handlers = g_hash_table_new (NULL, NULL);
170 g_hash_table_insert (mono_saved_signal_handlers, GINT_TO_POINTER (signo), handler_to_save);
173 static void
174 free_saved_sig_handler_func (gpointer key, gpointer value, gpointer user_data)
176 g_free (value);
179 static void
180 free_saved_signal_handlers (void)
182 if (mono_saved_signal_handlers) {
183 g_hash_table_foreach (mono_saved_signal_handlers, free_saved_sig_handler_func, NULL);
184 g_hash_table_destroy (mono_saved_signal_handlers);
185 mono_saved_signal_handlers = NULL;
190 * mono_chain_signal:
192 * Call the original signal handler for the signal given by the arguments, which
193 * should be the same as for a signal handler. Returns TRUE if the original handler
194 * was called, false otherwise.
196 gboolean
197 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
199 int signal = MONO_SIG_HANDLER_GET_SIGNO ();
200 struct sigaction *saved_handler = (struct sigaction *)get_saved_signal_handler (signal);
202 if (saved_handler && saved_handler->sa_handler) {
203 if (!(saved_handler->sa_flags & SA_SIGINFO)) {
204 saved_handler->sa_handler (signal);
205 } else {
206 #ifdef MONO_ARCH_USE_SIGACTION
207 saved_handler->sa_sigaction (MONO_SIG_HANDLER_PARAMS);
208 #endif /* MONO_ARCH_USE_SIGACTION */
210 return TRUE;
212 return FALSE;
215 MONO_SIG_HANDLER_FUNC (static, sigabrt_signal_handler)
217 MonoJitInfo *ji = NULL;
218 MONO_SIG_HANDLER_INFO_TYPE *info = MONO_SIG_HANDLER_GET_INFO ();
219 MONO_SIG_HANDLER_GET_CONTEXT;
221 if (mono_thread_internal_current ())
222 ji = mono_jit_info_table_find_internal (mono_domain_get (), (char *)mono_arch_ip_from_context (ctx), TRUE, TRUE);
223 if (!ji) {
224 if (mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
225 return;
226 mono_handle_native_sigsegv (SIGABRT, ctx, info);
230 #if defined(__i386__) || defined(__x86_64__)
231 #define FULL_STAT_PROFILER_BACKTRACE 1
232 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
233 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
234 #if MONO_ARCH_STACK_GROWS_UP
235 #define IS_BEFORE_ON_STACK <
236 #define IS_AFTER_ON_STACK >
237 #else
238 #define IS_BEFORE_ON_STACK >
239 #define IS_AFTER_ON_STACK <
240 #endif
241 #else
242 #define FULL_STAT_PROFILER_BACKTRACE 0
243 #endif
245 #if (defined (USE_POSIX_BACKEND) && defined (SIGRTMIN)) || defined (SIGPROF)
246 #define HAVE_PROFILER_SIGNAL
247 #endif
249 #ifdef HAVE_PROFILER_SIGNAL
251 static void
252 per_thread_profiler_hit (void *ctx)
254 int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
255 MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
257 if (call_chain_depth == 0) {
258 mono_profiler_stat_hit ((guchar *)mono_arch_ip_from_context (ctx), ctx);
259 } else {
260 MonoJitTlsData *jit_tls = (MonoJitTlsData *)mono_native_tls_get_value (mono_jit_tls_id);
261 int current_frame_index = 1;
262 MonoContext mono_context;
263 guchar *ips [call_chain_depth + 1];
265 mono_sigctx_to_monoctx (ctx, &mono_context);
266 ips [0] = (guchar *)MONO_CONTEXT_GET_IP (&mono_context);
268 if (jit_tls != NULL) {
269 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_NATIVE) {
270 #if FULL_STAT_PROFILER_BACKTRACE
271 guchar *current_frame;
272 guchar *stack_bottom;
273 guchar *stack_top;
275 stack_bottom = (guchar *)jit_tls->end_of_stack;
276 stack_top = (guchar *)MONO_CONTEXT_GET_SP (&mono_context);
277 current_frame = (guchar *)MONO_CONTEXT_GET_BP (&mono_context);
279 while ((current_frame_index <= call_chain_depth) &&
280 (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
281 ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
282 ips [current_frame_index] = (guchar *)CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
283 current_frame_index ++;
284 stack_top = current_frame;
285 current_frame = (guchar *)CURRENT_FRAME_GET_BASE_POINTER (current_frame);
287 #else
288 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_GLIBC;
289 #endif
292 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_GLIBC) {
293 #if GLIBC_PROFILER_BACKTRACE
294 current_frame_index = backtrace ((void**) & ips [1], call_chain_depth);
295 #else
296 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_MANAGED;
297 #endif
300 if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_MANAGED) {
301 MonoDomain *domain = mono_domain_get ();
302 if (domain != NULL) {
303 MonoLMF *lmf = NULL;
304 MonoJitInfo *ji;
305 MonoJitInfo res;
306 MonoContext new_mono_context;
307 int native_offset;
308 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
309 &new_mono_context, NULL, &lmf, &native_offset, NULL);
310 while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
311 ips [current_frame_index] = (guchar *)MONO_CONTEXT_GET_IP (&new_mono_context);
312 current_frame_index ++;
313 mono_context = new_mono_context;
314 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
315 &new_mono_context, NULL, &lmf, &native_offset, NULL);
321 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
325 static gint32 profiler_signals_sent;
326 static gint32 profiler_signals_received;
327 static gint32 profiler_signals_accepted;
329 MONO_SIG_HANDLER_FUNC (static, profiler_signal_handler)
331 int old_errno = errno;
332 int hp_save_index;
333 MONO_SIG_HANDLER_GET_CONTEXT;
335 InterlockedIncrement (&profiler_signals_received);
337 if (mono_thread_info_get_small_id () == -1)
338 return; //an non-attached thread got the signal
340 if (!mono_domain_get () || !mono_native_tls_get_value (mono_jit_tls_id))
341 return; //thread in the process of dettaching
343 InterlockedIncrement (&profiler_signals_accepted);
345 hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
347 mono_thread_info_set_is_async_context (TRUE);
348 per_thread_profiler_hit (ctx);
349 mono_thread_info_set_is_async_context (FALSE);
351 mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
352 errno = old_errno;
354 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
357 #endif
359 MONO_SIG_HANDLER_FUNC (static, sigquit_signal_handler)
361 gboolean res;
363 /* We use this signal to start the attach agent too */
364 res = mono_attach_start ();
365 if (res)
366 return;
368 mono_threads_request_thread_dump ();
370 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
373 MONO_SIG_HANDLER_FUNC (static, sigusr2_signal_handler)
375 gboolean enabled = mono_trace_is_enabled ();
377 mono_trace_enable (!enabled);
379 mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
382 static void
383 add_signal_handler (int signo, gpointer handler, int flags)
385 struct sigaction sa;
386 struct sigaction previous_sa;
388 #ifdef MONO_ARCH_USE_SIGACTION
389 sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))handler;
390 sigemptyset (&sa.sa_mask);
391 sa.sa_flags = SA_SIGINFO | flags;
392 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
394 /*Apple likes to deliver SIGBUS for *0 */
395 #ifdef PLATFORM_MACOSX
396 if (signo == SIGSEGV || signo == SIGBUS) {
397 #else
398 if (signo == SIGSEGV) {
399 #endif
400 sa.sa_flags |= SA_ONSTACK;
403 * libgc will crash when trying to do stack marking for threads which are on
404 * an altstack, so delay the suspend signal after the signal handler has
405 * executed.
407 if (mono_gc_get_suspend_signal () != -1)
408 sigaddset (&sa.sa_mask, mono_gc_get_suspend_signal ());
410 #endif
411 if (signo == SIGSEGV) {
413 * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
415 sigset_t block_mask;
417 sigemptyset (&block_mask);
419 #else
420 sa.sa_handler = handler;
421 sigemptyset (&sa.sa_mask);
422 sa.sa_flags = flags;
423 #endif
424 g_assert (sigaction (signo, &sa, &previous_sa) != -1);
426 /* if there was already a handler in place for this signal, store it */
427 if (! (previous_sa.sa_flags & SA_SIGINFO) &&
428 (SIG_DFL == previous_sa.sa_handler)) {
429 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
430 } else {
431 if (mono_do_signal_chaining)
432 save_old_signal_handler (signo, &previous_sa);
436 static void
437 remove_signal_handler (int signo)
439 struct sigaction sa;
440 struct sigaction *saved_action = get_saved_signal_handler (signo);
442 if (!saved_action) {
443 sa.sa_handler = SIG_DFL;
444 sigemptyset (&sa.sa_mask);
445 sa.sa_flags = 0;
447 sigaction (signo, &sa, NULL);
448 } else {
449 g_assert (sigaction (signo, saved_action, NULL) != -1);
453 void
454 mono_runtime_posix_install_handlers (void)
457 sigset_t signal_set;
459 if (mini_get_debug_options ()->handle_sigint)
460 add_signal_handler (SIGINT, mono_sigint_signal_handler, SA_RESTART);
462 add_signal_handler (SIGFPE, mono_sigfpe_signal_handler, 0);
463 add_signal_handler (SIGQUIT, sigquit_signal_handler, SA_RESTART);
464 add_signal_handler (SIGILL, mono_sigill_signal_handler, 0);
465 add_signal_handler (SIGBUS, mono_sigsegv_signal_handler, 0);
466 if (mono_jit_trace_calls != NULL)
467 add_signal_handler (SIGUSR2, sigusr2_signal_handler, SA_RESTART);
469 /* it seems to have become a common bug for some programs that run as parents
470 * of many processes to block signal delivery for real time signals.
471 * We try to detect and work around their breakage here.
473 sigemptyset (&signal_set);
474 if (mono_gc_get_suspend_signal () != -1)
475 sigaddset (&signal_set, mono_gc_get_suspend_signal ());
476 if (mono_gc_get_restart_signal () != -1)
477 sigaddset (&signal_set, mono_gc_get_restart_signal ());
478 sigaddset (&signal_set, SIGCHLD);
479 sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
481 signal (SIGPIPE, SIG_IGN);
483 add_signal_handler (SIGABRT, sigabrt_signal_handler, 0);
485 /* catch SIGSEGV */
486 add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler, 0);
489 #ifndef PLATFORM_MACOSX
490 void
491 mono_runtime_install_handlers (void)
493 mono_runtime_posix_install_handlers ();
495 #endif
497 void
498 mono_runtime_cleanup_handlers (void)
500 if (mini_get_debug_options ()->handle_sigint)
501 remove_signal_handler (SIGINT);
503 remove_signal_handler (SIGFPE);
504 remove_signal_handler (SIGQUIT);
505 remove_signal_handler (SIGILL);
506 remove_signal_handler (SIGBUS);
507 if (mono_jit_trace_calls != NULL)
508 remove_signal_handler (SIGUSR2);
510 remove_signal_handler (SIGABRT);
512 remove_signal_handler (SIGSEGV);
514 free_saved_signal_handlers ();
517 #ifdef HAVE_PROFILER_SIGNAL
519 #ifdef PLATFORM_MACOSX
521 static clock_serv_t sampling_clock_service;
523 static void
524 clock_init (void)
526 kern_return_t ret;
528 do {
529 ret = host_get_clock_service (mach_host_self (), SYSTEM_CLOCK, &sampling_clock_service);
530 } while (ret == KERN_ABORTED);
532 if (ret != KERN_SUCCESS)
533 g_error ("%s: host_get_clock_service () returned %d", __func__, ret);
536 static void
537 clock_cleanup (void)
539 kern_return_t ret;
541 do {
542 ret = mach_port_deallocate (mach_task_self (), sampling_clock_service);
543 } while (ret == KERN_ABORTED);
545 if (ret != KERN_SUCCESS)
546 g_error ("%s: mach_port_deallocate () returned %d", __func__, ret);
549 static guint64
550 clock_get_time_ns (void)
552 kern_return_t ret;
553 mach_timespec_t mach_ts;
555 do {
556 ret = clock_get_time (sampling_clock_service, &mach_ts);
557 } while (ret == KERN_ABORTED);
559 if (ret != KERN_SUCCESS)
560 g_error ("%s: clock_get_time () returned %d", __func__, ret);
562 return ((guint64) mach_ts.tv_sec * 1000000000) + (guint64) mach_ts.tv_nsec;
565 static void
566 clock_sleep_ns_abs (guint64 ns_abs)
568 kern_return_t ret;
569 mach_timespec_t then, remain_unused;
571 then.tv_sec = ns_abs / 1000000000;
572 then.tv_nsec = ns_abs % 1000000000;
574 do {
575 ret = clock_sleep (sampling_clock_service, TIME_ABSOLUTE, then, &remain_unused);
576 } while (ret == KERN_ABORTED);
578 if (ret != KERN_SUCCESS)
579 g_error ("%s: clock_sleep () returned %d", __func__, ret);
583 #else
585 clockid_t sampling_posix_clock;
587 static void
588 clock_init (void)
590 switch (mono_profiler_get_sampling_mode ()) {
591 case MONO_PROFILER_STAT_MODE_PROCESS:
592 #ifdef HAVE_CLOCK_NANOSLEEP
594 * If we don't have clock_nanosleep (), measuring the process time
595 * makes very little sense as we can only use nanosleep () to sleep on
596 * real time.
598 sampling_posix_clock = CLOCK_PROCESS_CPUTIME_ID;
599 break;
600 #endif
601 case MONO_PROFILER_STAT_MODE_REAL: sampling_posix_clock = CLOCK_MONOTONIC; break;
602 default: g_assert_not_reached (); break;
606 static void
607 clock_cleanup (void)
611 static guint64
612 clock_get_time_ns (void)
614 struct timespec ts;
616 if (clock_gettime (sampling_posix_clock, &ts) == -1)
617 g_error ("%s: clock_gettime () returned -1, errno = %d", __func__, errno);
619 return ((guint64) ts.tv_sec * 1000000000) + (guint64) ts.tv_nsec;
622 static void
623 clock_sleep_ns_abs (guint64 ns_abs)
625 #ifdef HAVE_CLOCK_NANOSLEEP
626 int ret;
627 struct timespec then;
629 then.tv_sec = ns_abs / 1000000000;
630 then.tv_nsec = ns_abs % 1000000000;
632 do {
633 ret = clock_nanosleep (sampling_posix_clock, TIMER_ABSTIME, &then, NULL);
635 if (ret != 0 && ret != EINTR)
636 g_error ("%s: clock_nanosleep () returned %d", __func__, ret);
637 } while (ret == EINTR);
638 #else
639 int ret;
640 gint64 diff;
641 struct timespec req;
644 * What follows is a crude attempt at emulating clock_nanosleep () on OSs
645 * which don't provide it (e.g. FreeBSD).
647 * The problem with nanosleep () is that if it is interrupted by a signal,
648 * time will drift as a result of having to restart the call after the
649 * signal handler has finished. For this reason, we avoid using the rem
650 * argument of nanosleep (). Instead, before every nanosleep () call, we
651 * check if enough time has passed to satisfy the sleep request. If yes, we
652 * simply return. If not, we calculate the difference and do another sleep.
654 * This should reduce the amount of drift that happens because we account
655 * for the time spent executing the signal handler, which nanosleep () is
656 * not guaranteed to do for the rem argument.
658 * The downside to this approach is that it is slightly expensive: We have
659 * to make an extra system call to retrieve the current time whenever we're
660 * going to restart a nanosleep () call. This is unlikely to be a problem
661 * in practice since the sampling thread won't be receiving many signals in
662 * the first place (it's a tools thread, so no STW), and because typical
663 * sleep periods for the thread are many orders of magnitude bigger than
664 * the time it takes to actually perform that system call (just a few
665 * nanoseconds).
667 do {
668 diff = (gint64) ns_abs - (gint64) clock_get_time_ns ();
670 if (diff <= 0)
671 break;
673 req.tv_sec = diff / 1000000000;
674 req.tv_nsec = diff % 1000000000;
676 if ((ret = nanosleep (&req, NULL)) == -1 && errno != EINTR)
677 g_error ("%s: nanosleep () returned -1, errno = %d", __func__, errno);
678 } while (ret == -1);
679 #endif
682 #endif
684 static int profiler_signal;
685 static MonoNativeThreadId sampling_thread;
686 static volatile gint32 sampling_thread_running;
688 static mono_native_thread_return_t
689 sampling_thread_func (void *data)
691 mono_threads_attach_tools_thread ();
692 mono_thread_info_set_name (mono_native_thread_id_get (), "Profiler sampler");
694 gint64 rate = 1000000000 / mono_profiler_get_sampling_rate ();
696 int old_policy;
697 struct sched_param old_sched;
698 pthread_getschedparam (pthread_self (), &old_policy, &old_sched);
701 * Attempt to switch the thread to real time scheduling. This will not
702 * necessarily work on all OSs; for example, most Linux systems will give
703 * us EPERM here unless configured to allow this.
705 * TODO: This does not work on Mac (and maybe some other OSs). On Mac, we
706 * have to use the Mach thread policy routines to switch to real-time
707 * scheduling. This is quite tricky as we need to specify how often we'll
708 * be doing work (easy), the normal processing time needed (also easy),
709 * and the maximum amount of processing time needed (hard). This is
710 * further complicated by the fact that if we misbehave and take too long
711 * to do our work, the kernel may knock us back down to the normal thread
712 * scheduling policy without telling us.
714 struct sched_param sched = { .sched_priority = sched_get_priority_max (SCHED_FIFO) };
715 pthread_setschedparam (pthread_self (), SCHED_FIFO, &sched);
717 clock_init ();
719 guint64 sleep = clock_get_time_ns ();
721 while (InterlockedRead (&sampling_thread_running)) {
722 sleep += rate;
724 FOREACH_THREAD_SAFE (info) {
725 /* info should never be this thread as we're a tools thread. */
726 g_assert (mono_thread_info_get_tid (info) != mono_native_thread_id_get ());
728 mono_threads_pthread_kill (info, profiler_signal);
729 InterlockedIncrement (&profiler_signals_sent);
730 } FOREACH_THREAD_SAFE_END
732 clock_sleep_ns_abs (sleep);
735 clock_cleanup ();
737 pthread_setschedparam (pthread_self (), old_policy, &old_sched);
739 mono_thread_info_detach ();
741 return NULL;
744 void
745 mono_runtime_shutdown_stat_profiler (void)
747 InterlockedWrite (&sampling_thread_running, 0);
748 pthread_join (sampling_thread, NULL);
751 * We can't safely remove the signal handler because we have no guarantee
752 * that all pending signals have been delivered at this point. This should
753 * not really be a problem anyway.
755 //remove_signal_handler (profiler_signal);
758 void
759 mono_runtime_setup_stat_profiler (void)
762 * Use a real-time signal when possible. This gives us roughly a 99% signal
763 * delivery rate in all cases. On the other hand, using a regular signal
764 * tends to result in awful delivery rates when the application is heavily
765 * loaded.
767 * TODO: On Mac, we should explore using the Mach thread suspend/resume
768 * functions and doing the stack walk from the sampling thread. This would
769 * get us a 100% sampling rate. However, this may interfere with the GC's
770 * STW logic. Could perhaps be solved by taking the suspend lock.
772 #if defined (USE_POSIX_BACKEND) && defined (SIGRTMIN)
773 /* Just take the first real-time signal we can get. */
774 profiler_signal = mono_threads_posix_signal_search_alternative (-1);
775 #else
776 profiler_signal = SIGPROF;
777 #endif
779 add_signal_handler (profiler_signal, profiler_signal_handler, SA_RESTART);
781 mono_counters_register ("Sampling signals sent", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_signals_sent);
782 mono_counters_register ("Sampling signals received", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_signals_received);
783 mono_counters_register ("Sampling signals accepted", MONO_COUNTER_UINT | MONO_COUNTER_PROFILER | MONO_COUNTER_MONOTONIC, &profiler_signals_accepted);
785 InterlockedWrite (&sampling_thread_running, 1);
786 mono_native_thread_create (&sampling_thread, sampling_thread_func, NULL);
789 #else
791 void
792 mono_runtime_shutdown_stat_profiler (void)
796 void
797 mono_runtime_setup_stat_profiler (void)
801 #endif
803 #if !defined(PLATFORM_MACOSX)
804 pid_t
805 mono_runtime_syscall_fork ()
807 #if defined(PLATFORM_ANDROID)
808 /* SYS_fork is defined to be __NR_fork which is not defined in some ndk versions */
809 g_assert_not_reached ();
810 return 0;
811 #elif defined(SYS_fork)
812 return (pid_t) syscall (SYS_fork);
813 #else
814 g_assert_not_reached ();
815 return 0;
816 #endif
819 void
820 mono_gdb_render_native_backtraces (pid_t crashed_pid)
822 const char *argv [9];
823 char template_ [] = "/tmp/mono-lldb-commands.XXXXXX";
824 char buf1 [128];
825 FILE *commands;
826 gboolean using_lldb = FALSE;
828 argv [0] = g_find_program_in_path ("gdb");
829 if (argv [0] == NULL) {
830 argv [0] = g_find_program_in_path ("lldb");
831 using_lldb = TRUE;
834 if (argv [0] == NULL)
835 return;
837 if (using_lldb) {
838 if (mkstemp (template_) == -1)
839 return;
841 commands = fopen (template_, "w");
843 fprintf (commands, "process attach --pid %ld\n", (long) crashed_pid);
844 fprintf (commands, "thread list\n");
845 fprintf (commands, "thread backtrace all\n");
846 fprintf (commands, "detach\n");
847 fprintf (commands, "quit\n");
849 fflush (commands);
850 fclose (commands);
852 argv [1] = "--source";
853 argv [2] = template_;
854 argv [3] = 0;
855 } else {
856 argv [1] = "-ex";
857 sprintf (buf1, "attach %ld", (long) crashed_pid);
858 argv [2] = buf1;
859 argv [3] = "--ex";
860 argv [4] = "info threads";
861 argv [5] = "--ex";
862 argv [6] = "thread apply all bt";
863 argv [7] = "--batch";
864 argv [8] = 0;
867 execv (argv [0], (char**)argv);
869 if (using_lldb)
870 unlink (template_);
872 #endif
873 #endif /* __native_client__ */
875 #if !defined (__MACH__)
877 gboolean
878 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
880 g_error ("Posix systems don't support mono_thread_state_init_from_handle");
881 return FALSE;
884 #endif