Merge pull request #444 from knocte/xbuild_improvements
[mono-project.git] / mono / mini / mini-windows.c
blob80a9c9bf148f26143bf069470aae984825a3fb5c
1 /*
2 * mini-posix.c: POSIX signal handling support for Mono.
4 * Authors:
5 * Mono Team (mono-list@lists.ximian.com)
7 * Copyright 2001-2003 Ximian, Inc.
8 * Copyright 2003-2008 Ximian, Inc.
10 * See LICENSE for licensing information.
12 #include <config.h>
13 #include <signal.h>
14 #include <math.h>
16 #include <mono/metadata/assembly.h>
17 #include <mono/metadata/loader.h>
18 #include <mono/metadata/tabledefs.h>
19 #include <mono/metadata/class.h>
20 #include <mono/metadata/object.h>
21 #include <mono/metadata/tokentype.h>
22 #include <mono/metadata/tabledefs.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/debug-helpers.h>
26 #include <mono/io-layer/io-layer.h>
27 #include "mono/metadata/profiler.h"
28 #include <mono/metadata/profiler-private.h>
29 #include <mono/metadata/mono-config.h>
30 #include <mono/metadata/environment.h>
31 #include <mono/metadata/mono-debug.h>
32 #include <mono/metadata/gc-internal.h>
33 #include <mono/metadata/threads-types.h>
34 #include <mono/metadata/verify.h>
35 #include <mono/metadata/verify-internals.h>
36 #include <mono/metadata/mempool-internals.h>
37 #include <mono/metadata/attach.h>
38 #include <mono/utils/mono-math.h>
39 #include <mono/utils/mono-compiler.h>
40 #include <mono/utils/mono-counters.h>
41 #include <mono/utils/mono-logger-internal.h>
42 #include <mono/utils/mono-mmap.h>
43 #include <mono/utils/dtrace.h>
45 #include "mini.h"
46 #include <string.h>
47 #include <ctype.h>
48 #include "trace.h"
49 #include "version.h"
51 #include "jit-icalls.h"
53 extern LPTOP_LEVEL_EXCEPTION_FILTER mono_old_win_toplevel_exception_filter;
54 extern guint64 mono_win_chained_exception_filter_result;
55 extern gboolean mono_win_chained_exception_filter_didrun;
57 void
58 mono_runtime_install_handlers (void)
60 #ifndef MONO_CROSS_COMPILE
61 win32_seh_init();
62 win32_seh_set_handler(SIGFPE, mono_sigfpe_signal_handler);
63 win32_seh_set_handler(SIGILL, mono_sigill_signal_handler);
64 win32_seh_set_handler(SIGSEGV, mono_sigsegv_signal_handler);
65 if (mini_get_debug_options ()->handle_sigint)
66 win32_seh_set_handler(SIGINT, mono_sigint_signal_handler);
67 #endif
70 void
71 mono_runtime_cleanup_handlers (void)
73 #ifndef MONO_CROSS_COMPILE
74 win32_seh_cleanup();
75 #endif
79 /* mono_chain_signal:
81 * Call the original signal handler for the signal given by the arguments, which
82 * should be the same as for a signal handler. Returns TRUE if the original handler
83 * was called, false otherwise.
85 gboolean
86 SIG_HANDLER_SIGNATURE (mono_chain_signal)
88 int signal = _dummy;
89 GET_CONTEXT;
91 if (mono_old_win_toplevel_exception_filter) {
92 mono_win_chained_exception_filter_didrun = TRUE;
93 mono_win_chained_exception_filter_result = (*mono_old_win_toplevel_exception_filter)(info);
94 return TRUE;
96 return FALSE;
99 static HANDLE win32_main_thread;
100 static MMRESULT win32_timer;
102 static void CALLBACK
103 win32_time_proc (UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
105 CONTEXT context;
107 context.ContextFlags = CONTEXT_CONTROL;
108 if (GetThreadContext (win32_main_thread, &context)) {
109 #ifdef _WIN64
110 mono_profiler_stat_hit ((guchar *) context.Rip, &context);
111 #else
112 mono_profiler_stat_hit ((guchar *) context.Eip, &context);
113 #endif
117 void
118 mono_runtime_setup_stat_profiler (void)
120 static int inited = 0;
121 TIMECAPS timecaps;
123 if (inited)
124 return;
126 inited = 1;
127 if (timeGetDevCaps (&timecaps, sizeof (timecaps)) != TIMERR_NOERROR)
128 return;
130 if ((win32_main_thread = OpenThread (READ_CONTROL | THREAD_GET_CONTEXT, FALSE, GetCurrentThreadId ())) == NULL)
131 return;
133 if (timeBeginPeriod (1) != TIMERR_NOERROR)
134 return;
136 if ((win32_timer = timeSetEvent (1, 0, win32_time_proc, 0, TIME_PERIODIC)) == 0) {
137 timeEndPeriod (1);
138 return;
142 void
143 mono_runtime_shutdown_stat_profiler (void)
147 gboolean
148 mono_thread_state_init_from_handle (MonoThreadUnwindState *tctx, MonoNativeThreadId thread_id, MonoNativeThreadHandle thread_handle)
150 g_error ("Windows systems haven't been ported to support mono_thread_state_init_from_handle");
151 return FALSE;