[profiler] Add counters for numbers of signals sent/received/accepted.
[mono.git] / mono / mini / mini-posix.c
1 /*
2  * mini-posix.c: POSIX signal handling support for Mono.
3  *
4  * Authors:
5  *   Mono Team (mono-list@lists.ximian.com)
6  *
7  * Copyright 2001-2003 Ximian, Inc.
8  * Copyright 2003-2008 Ximian, Inc.
9  * Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
10  *
11  * See LICENSE for licensing information.
12  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
13  */
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>
31
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>
63
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"
70
71 #include "jit-icalls.h"
72
73 #ifdef PLATFORM_MACOSX
74 #include <mach/mach.h>
75 #include <mach/mach_time.h>
76 #include <mach/clock.h>
77 #endif
78
79 #if defined(__native_client__) || defined(HOST_WATCHOS)
80
81 void
82 mono_runtime_setup_stat_profiler (void)
83 {
84         printf("WARNING: mono_runtime_setup_stat_profiler() called!\n");
85 }
86
87
88 void
89 mono_runtime_shutdown_stat_profiler (void)
90 {
91 }
92
93
94 gboolean
95 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
96 {
97         return FALSE;
98 }
99
100 #ifndef PLATFORM_MACOSX
101 void
102 mono_runtime_install_handlers (void)
103 {
104 }
105 #endif
106
107 void
108 mono_runtime_posix_install_handlers(void)
109 {
110
111 }
112
113 void
114 mono_runtime_shutdown_handlers (void)
115 {
116 }
117
118 void
119 mono_runtime_cleanup_handlers (void)
120 {
121 }
122
123 #if !defined(PLATFORM_MACOSX)
124 pid_t
125 mono_runtime_syscall_fork (void)
126 {
127         g_assert_not_reached();
128         return 0;
129 }
130
131 void
132 mono_gdb_render_native_backtraces (pid_t crashed_pid)
133 {
134 }
135 #endif
136
137 #else
138
139 static GHashTable *mono_saved_signal_handlers = NULL;
140
141 static struct sigaction *
142 get_saved_signal_handler (int signo)
143 {
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;
148 }
149
150 static void
151 save_old_signal_handler (int signo, struct sigaction *old_action)
152 {
153         struct sigaction *handler_to_save = (struct sigaction *)g_malloc (sizeof (struct sigaction));
154
155         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_CONFIG,
156                                 "Saving old signal handler for signal %d.", signo);
157
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 */
164         }
165         handler_to_save->sa_mask = old_action->sa_mask;
166         handler_to_save->sa_flags = old_action->sa_flags;
167         
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);
171 }
172
173 static void
174 free_saved_sig_handler_func (gpointer key, gpointer value, gpointer user_data)
175 {
176         g_free (value);
177 }
178
179 static void
180 free_saved_signal_handlers (void)
181 {
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;
186         }
187 }
188
189 /*
190  * mono_chain_signal:
191  *
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.
195  */
196 gboolean
197 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
198 {
199         int signal = MONO_SIG_HANDLER_GET_SIGNO ();
200         struct sigaction *saved_handler = (struct sigaction *)get_saved_signal_handler (signal);
201
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 */
209                 }
210                 return TRUE;
211         }
212         return FALSE;
213 }
214
215 MONO_SIG_HANDLER_FUNC (static, sigabrt_signal_handler)
216 {
217         MonoJitInfo *ji = NULL;
218         MONO_SIG_HANDLER_INFO_TYPE *info = MONO_SIG_HANDLER_GET_INFO ();
219         MONO_SIG_HANDLER_GET_CONTEXT;
220
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);
227         }
228 }
229
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
244
245 #if (defined (USE_POSIX_BACKEND) && defined (SIGRTMIN)) || defined (SIGPROF)
246 #define HAVE_PROFILER_SIGNAL
247 #endif
248
249 #ifdef HAVE_PROFILER_SIGNAL
250
251 static void
252 per_thread_profiler_hit (void *ctx)
253 {
254         int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
255         MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
256
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];
264
265                 mono_sigctx_to_monoctx (ctx, &mono_context);
266                 ips [0] = (guchar *)MONO_CONTEXT_GET_IP (&mono_context);
267                 
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;
274                         
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);
278                         
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);
286                         }
287 #else
288                                 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_GLIBC;
289 #endif
290                         }
291                         
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
298                         }
299
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);
316                                         }
317                                 }
318                         }
319                 }
320                 
321                 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
322         }
323 }
324
325 static gint32 profiler_signals_sent;
326 static gint32 profiler_signals_received;
327 static gint32 profiler_signals_accepted;
328
329 MONO_SIG_HANDLER_FUNC (static, profiler_signal_handler)
330 {
331         int old_errno = errno;
332         int hp_save_index;
333         MONO_SIG_HANDLER_GET_CONTEXT;
334
335         InterlockedIncrement (&profiler_signals_received);
336
337         if (mono_thread_info_get_small_id () == -1)
338                 return; //an non-attached thread got the signal
339
340         if (!mono_domain_get () || !mono_native_tls_get_value (mono_jit_tls_id))
341                 return; //thread in the process of dettaching
342
343         InterlockedIncrement (&profiler_signals_accepted);
344
345         hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
346
347         mono_thread_info_set_is_async_context (TRUE);
348         per_thread_profiler_hit (ctx);
349         mono_thread_info_set_is_async_context (FALSE);
350
351         mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
352         errno = old_errno;
353
354         mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
355 }
356
357 #endif
358
359 MONO_SIG_HANDLER_FUNC (static, sigquit_signal_handler)
360 {
361         gboolean res;
362
363         /* We use this signal to start the attach agent too */
364         res = mono_attach_start ();
365         if (res)
366                 return;
367
368         mono_threads_request_thread_dump ();
369
370         mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
371 }
372
373 MONO_SIG_HANDLER_FUNC (static, sigusr2_signal_handler)
374 {
375         gboolean enabled = mono_trace_is_enabled ();
376
377         mono_trace_enable (!enabled);
378
379         mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
380 }
381
382 static void
383 add_signal_handler (int signo, gpointer handler, int flags)
384 {
385         struct sigaction sa;
386         struct sigaction previous_sa;
387
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
393
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;
401
402                 /* 
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.
406                  */
407                 if (mono_gc_get_suspend_signal () != -1)
408                         sigaddset (&sa.sa_mask, mono_gc_get_suspend_signal ());
409         }
410 #endif
411         if (signo == SIGSEGV) {
412                 /* 
413                  * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
414                  */
415                 sigset_t block_mask;
416      
417                 sigemptyset (&block_mask);
418         }
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);
425
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);
433         }
434 }
435
436 static void
437 remove_signal_handler (int signo)
438 {
439         struct sigaction sa;
440         struct sigaction *saved_action = get_saved_signal_handler (signo);
441
442         if (!saved_action) {
443                 sa.sa_handler = SIG_DFL;
444                 sigemptyset (&sa.sa_mask);
445                 sa.sa_flags = 0;
446
447                 sigaction (signo, &sa, NULL);
448         } else {
449                 g_assert (sigaction (signo, saved_action, NULL) != -1);
450         }
451 }
452
453 void
454 mono_runtime_posix_install_handlers (void)
455 {
456
457         sigset_t signal_set;
458
459         if (mini_get_debug_options ()->handle_sigint)
460                 add_signal_handler (SIGINT, mono_sigint_signal_handler, SA_RESTART);
461
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);
468
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.
472          */
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);
480
481         signal (SIGPIPE, SIG_IGN);
482
483         add_signal_handler (SIGABRT, sigabrt_signal_handler, 0);
484
485         /* catch SIGSEGV */
486         add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler, 0);
487 }
488
489 #ifndef PLATFORM_MACOSX
490 void
491 mono_runtime_install_handlers (void)
492 {
493         mono_runtime_posix_install_handlers ();
494 }
495 #endif
496
497 void
498 mono_runtime_cleanup_handlers (void)
499 {
500         if (mini_get_debug_options ()->handle_sigint)
501                 remove_signal_handler (SIGINT);
502
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);
509
510         remove_signal_handler (SIGABRT);
511
512         remove_signal_handler (SIGSEGV);
513
514         free_saved_signal_handlers ();
515 }
516
517 #ifdef HAVE_PROFILER_SIGNAL
518
519 #ifdef PLATFORM_MACOSX
520
521 static clock_serv_t sampling_clock_service;
522
523 static void
524 clock_init (void)
525 {
526         kern_return_t ret;
527
528         do {
529                 ret = host_get_clock_service (mach_host_self (), SYSTEM_CLOCK, &sampling_clock_service);
530         } while (ret == KERN_ABORTED);
531
532         if (ret != KERN_SUCCESS)
533                 g_error ("%s: host_get_clock_service () returned %d", __func__, ret);
534 }
535
536 static void
537 clock_cleanup (void)
538 {
539         kern_return_t ret;
540
541         do {
542                 ret = mach_port_deallocate (mach_task_self (), sampling_clock_service);
543         } while (ret == KERN_ABORTED);
544
545         if (ret != KERN_SUCCESS)
546                 g_error ("%s: mach_port_deallocate () returned %d", __func__, ret);
547 }
548
549 static guint64
550 clock_get_time_ns (void)
551 {
552         kern_return_t ret;
553         mach_timespec_t mach_ts;
554
555         do {
556                 ret = clock_get_time (sampling_clock_service, &mach_ts);
557         } while (ret == KERN_ABORTED);
558
559         if (ret != KERN_SUCCESS)
560                 g_error ("%s: clock_get_time () returned %d", __func__, ret);
561
562         return ((guint64) mach_ts.tv_sec * 1000000000) + (guint64) mach_ts.tv_nsec;
563 }
564
565 static void
566 clock_sleep_ns_abs (guint64 ns_abs)
567 {
568         kern_return_t ret;
569         mach_timespec_t then, remain_unused;
570
571         then.tv_sec = ns_abs / 1000000000;
572         then.tv_nsec = ns_abs % 1000000000;
573
574         do {
575                 ret = clock_sleep (sampling_clock_service, TIME_ABSOLUTE, then, &remain_unused);
576         } while (ret == KERN_ABORTED);
577
578         if (ret != KERN_SUCCESS)
579                 g_error ("%s: clock_sleep () returned %d", __func__, ret);
580
581 }
582
583 #else
584
585 clockid_t sampling_posix_clock;
586
587 static void
588 clock_init (void)
589 {
590         switch (mono_profiler_get_sampling_mode ()) {
591         case MONO_PROFILER_STAT_MODE_PROCESS:
592 #ifdef HAVE_CLOCK_NANOSLEEP
593                 /*
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.
597                  */
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;
603         }
604 }
605
606 static void
607 clock_cleanup (void)
608 {
609 }
610
611 static guint64
612 clock_get_time_ns (void)
613 {
614         struct timespec ts;
615
616         if (clock_gettime (sampling_posix_clock, &ts) == -1)
617                 g_error ("%s: clock_gettime () returned -1, errno = %d", __func__, errno);
618
619         return ((guint64) ts.tv_sec * 1000000000) + (guint64) ts.tv_nsec;
620 }
621
622 static void
623 clock_sleep_ns_abs (guint64 ns_abs)
624 {
625 #ifdef HAVE_CLOCK_NANOSLEEP
626         int ret;
627         struct timespec then;
628
629         then.tv_sec = ns_abs / 1000000000;
630         then.tv_nsec = ns_abs % 1000000000;
631
632         do {
633                 ret = clock_nanosleep (sampling_posix_clock, TIMER_ABSTIME, &then, NULL);
634
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;
642
643         /*
644          * What follows is a crude attempt at emulating clock_nanosleep () on OSs
645          * which don't provide it (e.g. FreeBSD).
646          *
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.
653          *
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.
657          *
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).
666          */
667         do {
668                 diff = (gint64) ns_abs - (gint64) clock_get_time_ns ();
669
670                 if (diff <= 0)
671                         break;
672
673                 req.tv_sec = diff / 1000000000;
674                 req.tv_nsec = diff % 1000000000;
675
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
680 }
681
682 #endif
683
684 static int profiler_signal;
685 static MonoNativeThreadId sampling_thread;
686 static volatile gint32 sampling_thread_running;
687
688 static mono_native_thread_return_t
689 sampling_thread_func (void *data)
690 {
691         mono_threads_attach_tools_thread ();
692         mono_thread_info_set_name (mono_native_thread_id_get (), "Profiler sampler");
693
694         gint64 rate = 1000000000 / mono_profiler_get_sampling_rate ();
695
696         int old_policy;
697         struct sched_param old_sched;
698         pthread_getschedparam (pthread_self (), &old_policy, &old_sched);
699
700         /*
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.
704          *
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.
713          */
714         struct sched_param sched = { .sched_priority = sched_get_priority_max (SCHED_FIFO) };
715         pthread_setschedparam (pthread_self (), SCHED_FIFO, &sched);
716
717         clock_init ();
718
719         guint64 sleep = clock_get_time_ns ();
720
721         while (InterlockedRead (&sampling_thread_running)) {
722                 sleep += rate;
723
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 ());
727
728                         mono_threads_pthread_kill (info, profiler_signal);
729                         InterlockedIncrement (&profiler_signals_sent);
730                 } FOREACH_THREAD_SAFE_END
731
732                 clock_sleep_ns_abs (sleep);
733         }
734
735         clock_cleanup ();
736
737         pthread_setschedparam (pthread_self (), old_policy, &old_sched);
738
739         mono_thread_info_detach ();
740
741         return NULL;
742 }
743
744 void
745 mono_runtime_shutdown_stat_profiler (void)
746 {
747         InterlockedWrite (&sampling_thread_running, 0);
748         pthread_join (sampling_thread, NULL);
749
750         /*
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.
754          */
755         //remove_signal_handler (profiler_signal);
756 }
757
758 void
759 mono_runtime_setup_stat_profiler (void)
760 {
761         /*
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.
766          *
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.
771          */
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
778
779         add_signal_handler (profiler_signal, profiler_signal_handler, SA_RESTART);
780
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);
784
785         InterlockedWrite (&sampling_thread_running, 1);
786         mono_native_thread_create (&sampling_thread, sampling_thread_func, NULL);
787 }
788
789 #else
790
791 void
792 mono_runtime_shutdown_stat_profiler (void)
793 {
794 }
795
796 void
797 mono_runtime_setup_stat_profiler (void)
798 {
799 }
800
801 #endif
802
803 #if !defined(PLATFORM_MACOSX)
804 pid_t
805 mono_runtime_syscall_fork ()
806 {
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
817 }
818
819 void
820 mono_gdb_render_native_backtraces (pid_t crashed_pid)
821 {
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;
827
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;
832         }
833
834         if (argv [0] == NULL)
835                 return;
836
837         if (using_lldb) {
838                 if (mkstemp (template_) == -1)
839                         return;
840
841                 commands = fopen (template_, "w");
842
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");
848
849                 fflush (commands);
850                 fclose (commands);
851
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;
865         }
866
867         execv (argv [0], (char**)argv);
868
869         if (using_lldb)
870                 unlink (template_);
871 }
872 #endif
873 #endif /* __native_client__ */
874
875 #if !defined (__MACH__)
876
877 gboolean
878 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
879 {
880         g_error ("Posix systems don't support mono_thread_state_init_from_handle");
881         return FALSE;
882 }
883
884 #endif