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