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