Merge pull request #3025 from kumpera/mono_raise_exception_in_threads
[mono.git] / mono / mini / mini-windows.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  *
10  * See LICENSE for licensing information.
11  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
12  */
13 #include <config.h>
14 #include <signal.h>
15 #include <math.h>
16
17 #include <mono/metadata/assembly.h>
18 #include <mono/metadata/loader.h>
19 #include <mono/metadata/tabledefs.h>
20 #include <mono/metadata/class.h>
21 #include <mono/metadata/object.h>
22 #include <mono/metadata/tokentype.h>
23 #include <mono/metadata/tabledefs.h>
24 #include <mono/metadata/threads.h>
25 #include <mono/metadata/appdomain.h>
26 #include <mono/metadata/debug-helpers.h>
27 #include <mono/io-layer/io-layer.h>
28 #include "mono/metadata/profiler.h"
29 #include <mono/metadata/profiler-private.h>
30 #include <mono/metadata/mono-config.h>
31 #include <mono/metadata/environment.h>
32 #include <mono/metadata/mono-debug.h>
33 #include <mono/metadata/gc-internals.h>
34 #include <mono/metadata/threads-types.h>
35 #include <mono/metadata/verify.h>
36 #include <mono/metadata/verify-internals.h>
37 #include <mono/metadata/mempool-internals.h>
38 #include <mono/metadata/attach.h>
39 #include <mono/utils/mono-math.h>
40 #include <mono/utils/mono-compiler.h>
41 #include <mono/utils/mono-counters.h>
42 #include <mono/utils/mono-logger-internals.h>
43 #include <mono/utils/mono-mmap.h>
44 #include <mono/utils/dtrace.h>
45
46 #include "mini.h"
47 #include <string.h>
48 #include <ctype.h>
49 #include "trace.h"
50 #include "version.h"
51
52 #include "jit-icalls.h"
53
54 #ifdef _WIN32
55 #include <mmsystem.h>
56 #endif
57
58 void
59 mono_runtime_install_handlers (void)
60 {
61 #ifndef MONO_CROSS_COMPILE
62         win32_seh_init();
63         win32_seh_set_handler(SIGFPE, mono_sigfpe_signal_handler);
64         win32_seh_set_handler(SIGILL, mono_sigill_signal_handler);
65         win32_seh_set_handler(SIGSEGV, mono_sigsegv_signal_handler);
66         if (mini_get_debug_options ()->handle_sigint)
67                 win32_seh_set_handler(SIGINT, mono_sigint_signal_handler);
68 #endif
69 }
70
71 void
72 mono_runtime_cleanup_handlers (void)
73 {
74 #ifndef MONO_CROSS_COMPILE
75         win32_seh_cleanup();
76 #endif
77 }
78
79
80 /* mono_chain_signal:
81  *
82  *   Call the original signal handler for the signal given by the arguments, which
83  * should be the same as for a signal handler. Returns TRUE if the original handler
84  * was called, false otherwise.
85  */
86 gboolean
87 MONO_SIG_HANDLER_SIGNATURE (mono_chain_signal)
88 {
89         MonoJitTlsData *jit_tls = mono_native_tls_get_value (mono_jit_tls_id);
90         jit_tls->mono_win_chained_exception_needs_run = TRUE;
91         return TRUE;
92 }
93
94 static HANDLE win32_main_thread;
95 static MMRESULT win32_timer;
96
97 static void CALLBACK
98 win32_time_proc (UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
99 {
100         CONTEXT context;
101
102         context.ContextFlags = CONTEXT_CONTROL;
103         if (GetThreadContext (win32_main_thread, &context)) {
104 #ifdef _WIN64
105                 mono_profiler_stat_hit ((guchar *) context.Rip, &context);
106 #else
107                 mono_profiler_stat_hit ((guchar *) context.Eip, &context);
108 #endif
109         }
110 }
111
112 void
113 mono_runtime_setup_stat_profiler (void)
114 {
115         static int inited = 0;
116         TIMECAPS timecaps;
117
118         if (inited)
119                 return;
120
121         inited = 1;
122         if (timeGetDevCaps (&timecaps, sizeof (timecaps)) != TIMERR_NOERROR)
123                 return;
124
125         if ((win32_main_thread = OpenThread (READ_CONTROL | THREAD_GET_CONTEXT, FALSE, GetCurrentThreadId ())) == NULL)
126                 return;
127
128         if (timeBeginPeriod (1) != TIMERR_NOERROR)
129                 return;
130
131         if ((win32_timer = timeSetEvent (1, 0, (LPTIMECALLBACK)win32_time_proc, (DWORD_PTR)NULL, TIME_PERIODIC)) == 0) {
132                 timeEndPeriod (1);
133                 return;
134         }
135 }
136
137 void
138 mono_runtime_shutdown_stat_profiler (void)
139 {
140 }
141
142 gboolean
143 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoThreadInfo *info)
144 {
145         DWORD id = mono_thread_info_get_tid (info);
146         HANDLE handle;
147         CONTEXT context;
148         DWORD result;
149         MonoContext *ctx;
150         MonoJitTlsData *jit_tls;
151         void *domain;
152         MonoLMF *lmf = NULL;
153         gpointer *addr;
154
155         tctx->valid = FALSE;
156         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = NULL;
157         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = NULL;
158         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = NULL;
159
160         g_assert (id != GetCurrentThreadId ());
161
162         handle = OpenThread (THREAD_ALL_ACCESS, FALSE, id);
163         g_assert (handle);
164
165         context.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
166
167         if (!GetThreadContext (handle, &context)) {
168                 CloseHandle (handle);
169                 return FALSE;
170         }
171
172         g_assert (context.ContextFlags & CONTEXT_INTEGER);
173         g_assert (context.ContextFlags & CONTEXT_CONTROL);
174
175         ctx = &tctx->ctx;
176
177         memset (ctx, 0, sizeof (MonoContext));
178         mono_sigctx_to_monoctx (&context, ctx);
179
180         /* mono_set_jit_tls () sets this */
181         jit_tls = mono_thread_info_tls_get (info, TLS_KEY_JIT_TLS);
182         /* SET_APPDOMAIN () sets this */
183         domain = mono_thread_info_tls_get (info, TLS_KEY_DOMAIN);
184
185         /*Thread already started to cleanup, can no longer capture unwind state*/
186         if (!jit_tls || !domain)
187                 return FALSE;
188
189         /*
190          * The current LMF address is kept in a separate TLS variable, and its hard to read its value without
191          * arch-specific code. But the address of the TLS variable is stored in another TLS variable which
192          * can be accessed through MonoThreadInfo.
193          */
194         /* mono_set_lmf_addr () sets this */
195         addr = mono_thread_info_tls_get (info, TLS_KEY_LMF_ADDR);
196         if (addr)
197                 lmf = *addr;
198
199         tctx->unwind_data [MONO_UNWIND_DATA_DOMAIN] = domain;
200         tctx->unwind_data [MONO_UNWIND_DATA_JIT_TLS] = jit_tls;
201         tctx->unwind_data [MONO_UNWIND_DATA_LMF] = lmf;
202         tctx->valid = TRUE;
203
204         return TRUE;
205 }
206