2009-01-29 Miguel de Icaza <miguel@novell.com>
[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  */
12 #include <config.h>
13 #include <signal.h>
14 #include <math.h>
15
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.h>
42 #include <mono/utils/mono-mmap.h>
43 #include <mono/utils/dtrace.h>
44
45 #include "mini.h"
46 #include <string.h>
47 #include <ctype.h>
48 #include "trace.h"
49 #include "version.h"
50
51 #include "jit-icalls.h"
52
53 void
54 mono_runtime_install_handlers (void)
55 {
56         win32_seh_init();
57         win32_seh_set_handler(SIGFPE, mono_sigfpe_signal_handler);
58         win32_seh_set_handler(SIGILL, mono_sigill_signal_handler);
59         win32_seh_set_handler(SIGSEGV, mono_sigsegv_signal_handler);
60         if (mini_get_debug_options ()->handle_sigint)
61                 win32_seh_set_handler(SIGINT, mono_sigint_signal_handler);
62 }
63
64 void
65 mono_runtime_cleanup_handlers (void)
66 {
67         win32_seh_cleanup();
68 }
69
70 static HANDLE win32_main_thread;
71 static MMRESULT win32_timer;
72
73 static void CALLBACK
74 win32_time_proc (UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
75 {
76         CONTEXT context;
77
78         context.ContextFlags = CONTEXT_CONTROL;
79         if (GetThreadContext (win32_main_thread, &context)) {
80 #ifdef _WIN64
81                 mono_profiler_stat_hit ((guchar *) context.Rip, &context);
82 #else
83                 mono_profiler_stat_hit ((guchar *) context.Eip, &context);
84 #endif
85         }
86 }
87
88 void
89 mono_runtime_setup_stat_profiler (void)
90 {
91         static int inited = 0;
92         TIMECAPS timecaps;
93
94         if (inited)
95                 return;
96
97         inited = 1;
98         if (timeGetDevCaps (&timecaps, sizeof (timecaps)) != TIMERR_NOERROR)
99                 return;
100
101         if ((win32_main_thread = OpenThread (READ_CONTROL | THREAD_GET_CONTEXT, FALSE, GetCurrentThreadId ())) == NULL)
102                 return;
103
104         if (timeBeginPeriod (1) != TIMERR_NOERROR)
105                 return;
106
107         if ((win32_timer = timeSetEvent (1, 0, win32_time_proc, 0, TIME_PERIODIC)) == 0) {
108                 timeEndPeriod (1);
109                 return;
110         }
111 }
112
113 void
114 mono_runtime_shutdown_stat_profiler (void)
115 {
116 }