[sdb] Add command CMD_STACK_FRAME_GET_DOMAIN
[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  */
13 #include <config.h>
14 #include <signal.h>
15 #ifdef HAVE_ALLOCA_H
16 #include <alloca.h>
17 #endif
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #include <math.h>
22 #ifdef HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25 #ifdef HAVE_SYS_SYSCALL_H
26 #include <sys/syscall.h>
27 #endif
28 #include <errno.h>
29
30
31 #include <mono/metadata/assembly.h>
32 #include <mono/metadata/loader.h>
33 #include <mono/metadata/tabledefs.h>
34 #include <mono/metadata/class.h>
35 #include <mono/metadata/object.h>
36 #include <mono/metadata/tokentype.h>
37 #include <mono/metadata/tabledefs.h>
38 #include <mono/metadata/threads.h>
39 #include <mono/metadata/appdomain.h>
40 #include <mono/metadata/debug-helpers.h>
41 #include <mono/io-layer/io-layer.h>
42 #include "mono/metadata/profiler.h"
43 #include <mono/metadata/profiler-private.h>
44 #include <mono/metadata/mono-config.h>
45 #include <mono/metadata/environment.h>
46 #include <mono/metadata/mono-debug.h>
47 #include <mono/metadata/gc-internal.h>
48 #include <mono/metadata/threads-types.h>
49 #include <mono/metadata/verify.h>
50 #include <mono/metadata/verify-internals.h>
51 #include <mono/metadata/mempool-internals.h>
52 #include <mono/metadata/attach.h>
53 #include <mono/utils/mono-math.h>
54 #include <mono/utils/mono-compiler.h>
55 #include <mono/utils/mono-counters.h>
56 #include <mono/utils/mono-logger-internal.h>
57 #include <mono/utils/mono-mmap.h>
58 #include <mono/utils/dtrace.h>
59 #include <mono/utils/mono-signal-handler.h>
60 #include <mono/utils/mono-threads.h>
61
62 #include "mini.h"
63 #include <string.h>
64 #include <ctype.h>
65 #include "trace.h"
66 #include "version.h"
67 #include "debugger-agent.h"
68
69 #include "jit-icalls.h"
70
71 #if defined(__native_client__)
72
73 void
74 mono_runtime_setup_stat_profiler (void)
75 {
76         printf("WARNING: mono_runtime_setup_stat_profiler() called!\n");
77 }
78
79
80 void
81 mono_runtime_shutdown_stat_profiler (void)
82 {
83 }
84
85
86 gboolean
87 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
88 {
89         return FALSE;
90 }
91
92 void
93 mono_runtime_install_handlers (void)
94 {
95 }
96
97 void
98 mono_runtime_shutdown_handlers (void)
99 {
100 }
101
102 void
103 mono_runtime_cleanup_handlers (void)
104 {
105 }
106
107 pid_t
108 mono_runtime_syscall_fork (void)
109 {
110         g_assert_not_reached();
111         return 0;
112 }
113
114 void
115 mono_gdb_render_native_backtraces (pid_t crashed_pid)
116 {
117 }
118
119 #else
120
121 static GHashTable *mono_saved_signal_handlers = NULL;
122
123 static gpointer
124 get_saved_signal_handler (int signo)
125 {
126         if (mono_saved_signal_handlers)
127                 /* The hash is only modified during startup, so no need for locking */
128                 return g_hash_table_lookup (mono_saved_signal_handlers, GINT_TO_POINTER (signo));
129         return NULL;
130 }
131
132 static void
133 save_old_signal_handler (int signo, struct sigaction *old_action)
134 {
135         struct sigaction *handler_to_save = g_malloc (sizeof (struct sigaction));
136
137         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_CONFIG,
138                                 "Saving old signal handler for signal %d.", signo);
139
140         if (! (old_action->sa_flags & SA_SIGINFO)) {
141                 handler_to_save->sa_handler = old_action->sa_handler;
142         } else {
143 #ifdef MONO_ARCH_USE_SIGACTION
144                 handler_to_save->sa_sigaction = old_action->sa_sigaction;
145 #endif /* MONO_ARCH_USE_SIGACTION */
146         }
147         handler_to_save->sa_mask = old_action->sa_mask;
148         handler_to_save->sa_flags = old_action->sa_flags;
149         
150         if (!mono_saved_signal_handlers)
151                 mono_saved_signal_handlers = g_hash_table_new (NULL, NULL);
152         g_hash_table_insert (mono_saved_signal_handlers, GINT_TO_POINTER (signo), handler_to_save);
153 }
154
155 static void
156 free_saved_sig_handler_func (gpointer key, gpointer value, gpointer user_data)
157 {
158         g_free (value);
159 }
160
161 static void
162 free_saved_signal_handlers (void)
163 {
164         if (mono_saved_signal_handlers) {
165                 g_hash_table_foreach (mono_saved_signal_handlers, free_saved_sig_handler_func, NULL);
166                 g_hash_table_destroy (mono_saved_signal_handlers);
167                 mono_saved_signal_handlers = NULL;
168         }
169 }
170
171 /*
172  * mono_chain_signal:
173  *
174  *   Call the original signal handler for the signal given by the arguments, which
175  * should be the same as for a signal handler. Returns TRUE if the original handler
176  * was called, false otherwise.
177  */
178 gboolean
179 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
180 {
181         int signal = MONO_SIG_HANDLER_GET_SIGNO ();
182         struct sigaction *saved_handler = get_saved_signal_handler (signal);
183
184         if (saved_handler && saved_handler->sa_handler) {
185                 if (!(saved_handler->sa_flags & SA_SIGINFO)) {
186                         saved_handler->sa_handler (signal);
187                 } else {
188 #ifdef MONO_ARCH_USE_SIGACTION
189                         saved_handler->sa_sigaction (MONO_SIG_HANDLER_PARAMS);
190 #endif /* MONO_ARCH_USE_SIGACTION */
191                 }
192                 return TRUE;
193         }
194         return FALSE;
195 }
196
197 MONO_SIG_HANDLER_FUNC (static, sigabrt_signal_handler)
198 {
199         MonoJitInfo *ji = NULL;
200         MONO_SIG_HANDLER_GET_CONTEXT;
201
202         if (mono_thread_internal_current ())
203                 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context (ctx));
204         if (!ji) {
205         if (mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
206                         return;
207                 mono_handle_native_sigsegv (SIGABRT, ctx);
208         }
209 }
210
211 MONO_SIG_HANDLER_FUNC (static, sigusr1_signal_handler)
212 {
213         gboolean running_managed;
214         MonoException *exc;
215         MonoInternalThread *thread = mono_thread_internal_current ();
216         MonoDomain *domain = mono_domain_get ();
217         void *ji;
218         MONO_SIG_HANDLER_GET_CONTEXT;
219
220         if (!thread || !domain) {
221                 /* The thread might not have started up yet */
222                 /* FIXME: Specify the synchronization with start_wrapper () in threads.c */
223                 mono_debugger_agent_thread_interrupt (ctx, NULL);
224                 return;
225         }
226
227         if (thread->ignore_next_signal) {
228                 thread->ignore_next_signal = FALSE;
229                 return;
230         }
231
232         if (thread->thread_dump_requested) {
233                 thread->thread_dump_requested = FALSE;
234
235                 mono_print_thread_dump (ctx);
236         }
237
238         /*
239          * This is an async signal, so the code below must not call anything which
240          * is not async safe. That includes the pthread locking functions. If we
241          * know that we interrupted managed code, then locking is safe.
242          */
243         /*
244          * On OpenBSD, ctx can be NULL if we are interrupting poll ().
245          */
246         if (ctx) {
247                 ji = mono_jit_info_table_find (mono_domain_get (), mono_arch_ip_from_context(ctx));
248                 running_managed = ji != NULL;
249
250                 if (mono_debugger_agent_thread_interrupt (ctx, ji))
251                         return;
252         } else {
253                 running_managed = FALSE;
254         }
255
256         /* We can't do handler block checking from metadata since it requires doing
257          * a stack walk with context.
258          *
259          * FIXME add full-aot support.
260          */
261 #ifdef MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX
262         if (!mono_aot_only && ctx) {
263                 MonoThreadUnwindState unwind_state;
264                 if (mono_thread_state_init_from_sigctx (&unwind_state, ctx)) {
265                         if (mono_install_handler_block_guard (&unwind_state)) {
266 #ifndef HOST_WIN32
267                                 /*Clear current thread from been wapi interrupted otherwise things can go south*/
268                                 wapi_clear_interruption ();
269 #endif
270                                 return;
271                         }
272                 }
273         }
274 #endif
275
276         exc = mono_thread_request_interruption (running_managed); 
277         if (!exc)
278                 return;
279
280         mono_arch_handle_exception (ctx, exc);
281 }
282
283
284 #if defined(__i386__) || defined(__x86_64__)
285 #define FULL_STAT_PROFILER_BACKTRACE 1
286 #define CURRENT_FRAME_GET_BASE_POINTER(f) (* (gpointer*)(f))
287 #define CURRENT_FRAME_GET_RETURN_ADDRESS(f) (* (((gpointer*)(f)) + 1))
288 #if MONO_ARCH_STACK_GROWS_UP
289 #define IS_BEFORE_ON_STACK <
290 #define IS_AFTER_ON_STACK >
291 #else
292 #define IS_BEFORE_ON_STACK >
293 #define IS_AFTER_ON_STACK <
294 #endif
295 #else
296 #define FULL_STAT_PROFILER_BACKTRACE 0
297 #endif
298
299 #ifdef SIGPROF
300 #if defined(__ia64__) || defined(__sparc__) || defined(sparc) || defined(__s390__) || defined(s390)
301
302 MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
303 {
304         if (mono_chain_signal (MONO_SIG_HANDLER_PARAMS))
305                 return;
306
307         NOT_IMPLEMENTED;
308 }
309
310 #else
311
312 static int profiling_signal_in_use;
313
314 static void
315 per_thread_profiler_hit (void *ctx)
316 {
317         int call_chain_depth = mono_profiler_stat_get_call_chain_depth ();
318         MonoProfilerCallChainStrategy call_chain_strategy = mono_profiler_stat_get_call_chain_strategy ();
319
320         if (call_chain_depth == 0) {
321                 mono_profiler_stat_hit (mono_arch_ip_from_context (ctx), ctx);
322         } else {
323                 MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
324                 int current_frame_index = 1;
325                 MonoContext mono_context;
326                 guchar *ips [call_chain_depth + 1];
327
328                 mono_sigctx_to_monoctx (ctx, &mono_context);
329                 ips [0] = MONO_CONTEXT_GET_IP (&mono_context);
330                 
331                 if (jit_tls != NULL) {
332                         if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_NATIVE) {
333 #if FULL_STAT_PROFILER_BACKTRACE
334                         guchar *current_frame;
335                         guchar *stack_bottom;
336                         guchar *stack_top;
337                         
338                         stack_bottom = jit_tls->end_of_stack;
339                         stack_top = MONO_CONTEXT_GET_SP (&mono_context);
340                         current_frame = MONO_CONTEXT_GET_BP (&mono_context);
341                         
342                         while ((current_frame_index <= call_chain_depth) &&
343                                         (stack_bottom IS_BEFORE_ON_STACK (guchar*) current_frame) &&
344                                         ((guchar*) current_frame IS_BEFORE_ON_STACK stack_top)) {
345                                 ips [current_frame_index] = CURRENT_FRAME_GET_RETURN_ADDRESS (current_frame);
346                                 current_frame_index ++;
347                                 stack_top = current_frame;
348                                 current_frame = CURRENT_FRAME_GET_BASE_POINTER (current_frame);
349                         }
350 #else
351                                 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_GLIBC;
352 #endif
353                         }
354                         
355                         if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_GLIBC) {
356 #if GLIBC_PROFILER_BACKTRACE
357                                 current_frame_index = backtrace ((void**) & ips [1], call_chain_depth);
358 #else
359                                 call_chain_strategy = MONO_PROFILER_CALL_CHAIN_MANAGED;
360 #endif
361                         }
362
363                         if (call_chain_strategy == MONO_PROFILER_CALL_CHAIN_MANAGED) {
364                                 MonoDomain *domain = mono_domain_get ();
365                                 if (domain != NULL) {
366                                         MonoLMF *lmf = NULL;
367                                         MonoJitInfo *ji;
368                                         MonoJitInfo res;
369                                         MonoContext new_mono_context;
370                                         int native_offset;
371                                         ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
372                                                         &new_mono_context, NULL, &lmf, &native_offset, NULL);
373                                         while ((ji != NULL) && (current_frame_index <= call_chain_depth)) {
374                                                 ips [current_frame_index] = MONO_CONTEXT_GET_IP (&new_mono_context);
375                                                 current_frame_index ++;
376                                                 mono_context = new_mono_context;
377                                                 ji = mono_find_jit_info (domain, jit_tls, &res, NULL, &mono_context,
378                                                                 &new_mono_context, NULL, &lmf, &native_offset, NULL);
379                                         }
380                                 }
381                         }
382                 }
383                 
384                 mono_profiler_stat_call_chain (current_frame_index, & ips [0], ctx);
385         }
386 }
387
388 MONO_SIG_HANDLER_FUNC (static, sigprof_signal_handler)
389 {
390         MonoThreadInfo *info;
391         int old_errno = errno;
392         int hp_save_index;
393         MONO_SIG_HANDLER_GET_CONTEXT;
394
395         if (mono_thread_info_get_small_id () == -1)
396                 return; //an non-attached thread got the signal
397
398         hp_save_index = mono_hazard_pointer_save_for_signal_handler ();
399
400         /* If we can't consume a profiling request it means we're the initiator. */
401         if (!(mono_threads_consume_async_jobs () & MONO_SERVICE_REQUEST_SAMPLE)) {
402                 FOREACH_THREAD_SAFE (info) {
403                         if (mono_thread_info_get_tid (info) == mono_native_thread_id_get ())
404                                 continue;
405
406                         mono_threads_add_async_job (info, MONO_SERVICE_REQUEST_SAMPLE);
407                         mono_threads_pthread_kill (info, profiling_signal_in_use);
408                 } END_FOREACH_THREAD_SAFE;
409         }
410
411         mono_thread_info_set_is_async_context (TRUE);
412         per_thread_profiler_hit (ctx);
413         mono_thread_info_set_is_async_context (FALSE);
414
415         mono_hazard_pointer_restore_for_signal_handler (hp_save_index);
416         errno = old_errno;
417
418         mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
419 }
420
421 #endif
422 #endif
423
424 MONO_SIG_HANDLER_FUNC (static, sigquit_signal_handler)
425 {
426         gboolean res;
427         MONO_SIG_HANDLER_GET_CONTEXT;
428
429         /* We use this signal to start the attach agent too */
430         res = mono_attach_start ();
431         if (res)
432                 return;
433
434         if (mono_thread_info_new_interrupt_enabled ()) {
435                 mono_threads_request_thread_dump ();
436         } else {
437                 printf ("Full thread dump:\n");
438
439                 mono_threads_request_thread_dump ();
440
441                 /*
442                  * print_thread_dump () skips the current thread, since sending a signal
443                  * to it would invoke the signal handler below the sigquit signal handler,
444                  * and signal handlers don't create an lmf, so the stack walk could not
445                  * be performed.
446                  */
447                 mono_print_thread_dump (ctx);
448         }
449
450         mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
451 }
452
453 MONO_SIG_HANDLER_FUNC (static, sigusr2_signal_handler)
454 {
455         gboolean enabled = mono_trace_is_enabled ();
456
457         mono_trace_enable (!enabled);
458
459         mono_chain_signal (MONO_SIG_HANDLER_PARAMS);
460 }
461
462 static void
463 add_signal_handler (int signo, gpointer handler)
464 {
465         struct sigaction sa;
466         struct sigaction previous_sa;
467
468 #ifdef MONO_ARCH_USE_SIGACTION
469         sa.sa_sigaction = handler;
470         sigemptyset (&sa.sa_mask);
471         sa.sa_flags = SA_SIGINFO;
472 #ifdef MONO_ARCH_SIGSEGV_ON_ALTSTACK
473
474 /*Apple likes to deliver SIGBUS for *0 */
475 #ifdef __APPLE__
476         if (signo == SIGSEGV || signo == SIGBUS) {
477 #else
478         if (signo == SIGSEGV) {
479 #endif
480                 sa.sa_flags |= SA_ONSTACK;
481
482                 /* 
483                  * libgc will crash when trying to do stack marking for threads which are on
484                  * an altstack, so delay the suspend signal after the signal handler has
485                  * executed.
486                  */
487                 if (mono_gc_get_suspend_signal () != -1)
488                         sigaddset (&sa.sa_mask, mono_gc_get_suspend_signal ());
489         }
490 #endif
491         if (signo == SIGSEGV) {
492                 /* 
493                  * Delay abort signals while handling SIGSEGVs since they could go unnoticed.
494                  */
495                 sigset_t block_mask;
496      
497                 sigemptyset (&block_mask);
498                 sigaddset (&sa.sa_mask, mono_thread_get_abort_signal ());
499         }
500 #else
501         sa.sa_handler = handler;
502         sigemptyset (&sa.sa_mask);
503         sa.sa_flags = 0;
504 #endif
505         g_assert (sigaction (signo, &sa, &previous_sa) != -1);
506
507         /* if there was already a handler in place for this signal, store it */
508         if (! (previous_sa.sa_flags & SA_SIGINFO) &&
509                         (SIG_DFL == previous_sa.sa_handler)) { 
510                 /* it there is no sa_sigaction function and the sa_handler is default, we can safely ignore this */
511         } else {
512                 if (mono_do_signal_chaining)
513                         save_old_signal_handler (signo, &previous_sa);
514         }
515 }
516
517 static void
518 remove_signal_handler (int signo)
519 {
520         struct sigaction sa;
521         struct sigaction *saved_action = get_saved_signal_handler (signo);
522
523         if (!saved_action) {
524                 sa.sa_handler = SIG_DFL;
525                 sigemptyset (&sa.sa_mask);
526                 sa.sa_flags = 0;
527
528                 sigaction (signo, &sa, NULL);
529         } else {
530                 g_assert (sigaction (signo, saved_action, NULL) != -1);
531         }
532 }
533
534 void
535 mono_runtime_posix_install_handlers (void)
536 {
537
538         sigset_t signal_set;
539
540         if (mini_get_debug_options ()->handle_sigint)
541                 add_signal_handler (SIGINT, mono_sigint_signal_handler);
542
543         add_signal_handler (SIGFPE, mono_sigfpe_signal_handler);
544         add_signal_handler (SIGQUIT, sigquit_signal_handler);
545         add_signal_handler (SIGILL, mono_sigill_signal_handler);
546         add_signal_handler (SIGBUS, mono_sigsegv_signal_handler);
547         if (mono_jit_trace_calls != NULL)
548                 add_signal_handler (SIGUSR2, sigusr2_signal_handler);
549
550         if (!mono_thread_info_new_interrupt_enabled ())
551                 add_signal_handler (mono_thread_get_abort_signal (), sigusr1_signal_handler);
552         /* it seems to have become a common bug for some programs that run as parents
553          * of many processes to block signal delivery for real time signals.
554          * We try to detect and work around their breakage here.
555          */
556         sigemptyset (&signal_set);
557         sigaddset (&signal_set, mono_thread_get_abort_signal ());
558         if (mono_gc_get_suspend_signal () != -1)
559                 sigaddset (&signal_set, mono_gc_get_suspend_signal ());
560         if (mono_gc_get_restart_signal () != -1)
561                 sigaddset (&signal_set, mono_gc_get_restart_signal ());
562         sigaddset (&signal_set, SIGCHLD);
563         sigprocmask (SIG_UNBLOCK, &signal_set, NULL);
564
565         signal (SIGPIPE, SIG_IGN);
566
567         add_signal_handler (SIGABRT, sigabrt_signal_handler);
568
569         /* catch SIGSEGV */
570         add_signal_handler (SIGSEGV, mono_sigsegv_signal_handler);
571 }
572
573 #ifndef PLATFORM_MACOSX
574 void
575 mono_runtime_install_handlers (void)
576 {
577         mono_runtime_posix_install_handlers ();
578 }
579 #endif
580
581 void
582 mono_runtime_cleanup_handlers (void)
583 {
584         if (mini_get_debug_options ()->handle_sigint)
585                 remove_signal_handler (SIGINT);
586
587         remove_signal_handler (SIGFPE);
588         remove_signal_handler (SIGQUIT);
589         remove_signal_handler (SIGILL);
590         remove_signal_handler (SIGBUS);
591         if (mono_jit_trace_calls != NULL)
592                 remove_signal_handler (SIGUSR2);
593
594         remove_signal_handler (mono_thread_get_abort_signal ());
595
596         remove_signal_handler (SIGABRT);
597
598         remove_signal_handler (SIGSEGV);
599
600         free_saved_signal_handlers ();
601 }
602
603 #ifdef HAVE_LINUX_RTC_H
604 #include <linux/rtc.h>
605 #include <sys/ioctl.h>
606 #include <fcntl.h>
607 static int rtc_fd = -1;
608
609 static int
610 enable_rtc_timer (gboolean enable)
611 {
612         int flags;
613         flags = fcntl (rtc_fd, F_GETFL);
614         if (flags < 0) {
615                 perror ("getflags");
616                 return 0;
617         }
618         if (enable)
619                 flags |= FASYNC;
620         else
621                 flags &= ~FASYNC;
622         if (fcntl (rtc_fd, F_SETFL, flags) == -1) {
623                 perror ("setflags");
624                 return 0;
625         }
626         return 1;
627 }
628 #endif
629
630 void
631 mono_runtime_shutdown_stat_profiler (void)
632 {
633 #ifdef HAVE_LINUX_RTC_H
634         if (rtc_fd >= 0)
635                 enable_rtc_timer (FALSE);
636 #endif
637 }
638
639 #ifdef ITIMER_PROF
640 static int
641 get_itimer_mode (void)
642 {
643         switch (mono_profiler_get_sampling_mode ()) {
644         case MONO_PROFILER_STAT_MODE_PROCESS: return ITIMER_PROF;
645         case MONO_PROFILER_STAT_MODE_REAL: return ITIMER_REAL;
646         }
647         g_assert_not_reached ();
648         return 0;
649 }
650
651 static int
652 get_itimer_signal (void)
653 {
654         switch (mono_profiler_get_sampling_mode ()) {
655         case MONO_PROFILER_STAT_MODE_PROCESS: return SIGPROF;
656         case MONO_PROFILER_STAT_MODE_REAL: return SIGALRM;
657         }
658         g_assert_not_reached ();
659         return 0;
660 }
661 #endif
662
663 void
664 mono_runtime_setup_stat_profiler (void)
665 {
666 #ifdef ITIMER_PROF
667         struct itimerval itval;
668         static int inited = 0;
669 #ifdef HAVE_LINUX_RTC_H
670         const char *rtc_freq;
671         if (!inited && (rtc_freq = g_getenv ("MONO_RTC"))) {
672                 int freq = 0;
673                 inited = 1;
674                 if (*rtc_freq)
675                         freq = atoi (rtc_freq);
676                 if (!freq)
677                         freq = 1024;
678                 rtc_fd = open ("/dev/rtc", O_RDONLY);
679                 if (rtc_fd == -1) {
680                         perror ("open /dev/rtc");
681                         return;
682                 }
683                 profiling_signal_in_use = SIGPROF;
684                 add_signal_handler (profiling_signal_in_use, sigprof_signal_handler);
685                 if (ioctl (rtc_fd, RTC_IRQP_SET, freq) == -1) {
686                         perror ("set rtc freq");
687                         return;
688                 }
689                 if (ioctl (rtc_fd, RTC_PIE_ON, 0) == -1) {
690                         perror ("start rtc");
691                         return;
692                 }
693                 if (fcntl (rtc_fd, F_SETSIG, SIGPROF) == -1) {
694                         perror ("setsig");
695                         return;
696                 }
697                 if (fcntl (rtc_fd, F_SETOWN, getpid ()) == -1) {
698                         perror ("setown");
699                         return;
700                 }
701                 enable_rtc_timer (TRUE);
702                 return;
703         }
704         if (rtc_fd >= 0)
705                 return;
706 #endif
707
708         itval.it_interval.tv_usec = (1000000 / mono_profiler_get_sampling_rate ()) - 1;
709         itval.it_interval.tv_sec = 0;
710         itval.it_value = itval.it_interval;
711         if (inited)
712                 return;
713         inited = 1;
714         profiling_signal_in_use = get_itimer_signal ();
715         add_signal_handler (profiling_signal_in_use, sigprof_signal_handler);
716         setitimer (get_itimer_mode (), &itval, NULL);
717 #endif
718 }
719
720 #if !defined(__APPLE__)
721 pid_t
722 mono_runtime_syscall_fork ()
723 {
724 #if defined(SYS_fork)
725         return (pid_t) syscall (SYS_fork);
726 #else
727         g_assert_not_reached ();
728         return 0;
729 #endif
730 }
731
732 void
733 mono_gdb_render_native_backtraces (pid_t crashed_pid)
734 {
735         const char *argv [9];
736         char template [] = "/tmp/mono-lldb-commands.XXXXXX";
737         char buf1 [128];
738         FILE *commands;
739         gboolean using_lldb = FALSE;
740
741         argv [0] = g_find_program_in_path ("gdb");
742         if (argv [0] == NULL) {
743                 argv [0] = g_find_program_in_path ("lldb");
744                 using_lldb = TRUE;
745         }
746
747         if (argv [0] == NULL)
748                 return;
749
750         if (using_lldb) {
751                 if (mkstemp (template) == -1)
752                         return;
753
754                 commands = fopen (template, "w");
755
756                 fprintf (commands, "process attach --pid %ld\n", (long) crashed_pid);
757                 fprintf (commands, "thread list\n");
758                 fprintf (commands, "thread backtrace all\n");
759                 fprintf (commands, "detach\n");
760                 fprintf (commands, "quit\n");
761
762                 fflush (commands);
763                 fclose (commands);
764
765                 argv [1] = "--source";
766                 argv [2] = template;
767                 argv [3] = 0;
768         } else {
769                 argv [1] = "-ex";
770                 sprintf (buf1, "attach %ld", (long) crashed_pid);
771                 argv [2] = buf1;
772                 argv [3] = "--ex";
773                 argv [4] = "info threads";
774                 argv [5] = "--ex";
775                 argv [6] = "thread apply all bt";
776                 argv [7] = "--batch";
777                 argv [8] = 0;
778         }
779
780         execv (argv [0], (char**)argv);
781
782         if (using_lldb)
783                 unlink (template);
784 }
785 #endif
786 #endif /* __native_client__ */
787
788 #if !defined (__MACH__)
789
790 gboolean
791 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
792 {
793         g_error ("Posix systems don't support mono_thread_state_init_from_handle");
794         return FALSE;
795 }
796
797 #endif