IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Signal.cs
Go to the documentation of this file.
1/*
2 Copyright (C) 2007-2015 Jeroen Frijters
3 Copyright (C) 2009 Volker Berlin (i-net software)
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20
21 Jeroen Frijters
22 jeroen@frijters.net
23
24*/
25using System;
26using System.Collections.Generic;
27using System.Reflection;
28using System.Runtime.ConstrainedExecution;
29using System.Runtime.InteropServices;
30using System.Security;
31
32using IKVM.Runtime;
33
35{
36
37 static class Signal
38 {
39
40 /* derived from version 6.0 VC98/include/signal.h */
41 private const int SIGINT = 2; /* interrupt */
42 private const int SIGILL = 4; /* illegal instruction - invalid function image */
43 private const int SIGFPE = 8; /* floating point exception */
44 private const int SIGSEGV = 11; /* segment violation */
45 private const int SIGTERM = 15; /* Software termination signal from kill */
46 private const int SIGBREAK = 21; /* Ctrl-Break sequence */
47 private const int SIGABRT = 22; /* abnormal termination triggered by abort call */
48
49 private static Dictionary<int, long> handler = new Dictionary<int, long>();
50
51 // Delegate type to be used as the Handler Routine for SetConsoleCtrlHandler
52 private delegate Boolean ConsoleCtrlDelegate(CtrlTypes CtrlType);
53
54 // Enumerated type for the control messages sent to the handler routine
55 private enum CtrlTypes : uint
56 {
57 CTRL_C_EVENT = 0,
58 CTRL_BREAK_EVENT,
59 CTRL_CLOSE_EVENT,
60 CTRL_LOGOFF_EVENT = 5,
61 CTRL_SHUTDOWN_EVENT
62 }
63
64 [SecurityCritical]
65 private sealed class CriticalCtrlHandler : CriticalFinalizerObject
66 {
67 private ConsoleCtrlDelegate consoleCtrlDelegate;
68 private bool ok;
69
70 [DllImport("kernel32.dll")]
71 private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate e, bool add);
72
73 internal CriticalCtrlHandler()
74 {
75 consoleCtrlDelegate = new ConsoleCtrlDelegate(ConsoleCtrlCheck);
76 ok = SetConsoleCtrlHandler(consoleCtrlDelegate, true);
77 }
78
79 [SecuritySafeCritical]
80 ~CriticalCtrlHandler()
81 {
82 if (ok)
83 {
84 SetConsoleCtrlHandler(consoleCtrlDelegate, false);
85 }
86 }
87 }
88
89 private static object defaultConsoleCtrlDelegate;
90
91 private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
92 {
93#if !FIRST_PASS
94 switch (ctrlType)
95 {
96 case CtrlTypes.CTRL_BREAK_EVENT:
97 DumpAllJavaThreads();
98 return true;
99
100 }
101#endif
102 return false;
103 }
104
105#if !FIRST_PASS
106
107 private static void DumpAllJavaThreads()
108 {
109 Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
110 global::java.util.Map traces = global::java.lang.Thread.getAllStackTraces();
111 Console.WriteLine("Full thread dump IKVM.NET {0} ({1} bit):", Assembly.GetExecutingAssembly().GetName().Version, IntPtr.Size * 8);
112 global::java.util.Iterator entries = traces.entrySet().iterator();
113 while (entries.hasNext())
114 {
115 global::java.util.Map.Entry entry = (global::java.util.Map.Entry)entries.next();
116 global::java.lang.Thread thread = (global::java.lang.Thread)entry.getKey();
117 Console.WriteLine("\n\"{0}\"{1} prio={2} tid=0x{3:X8}", thread.getName(), thread.isDaemon() ? " daemon" : "", thread.getPriority(), thread.getId());
118 Console.WriteLine(" java.lang.Thread.State: " + thread.getState());
119 global::java.lang.StackTraceElement[] trace = (global::java.lang.StackTraceElement[])entry.getValue();
120 for (int i = 0; i < trace.Length; i++)
121 {
122 Console.WriteLine("\tat {0}", trace[i]);
123 }
124 }
125 Console.WriteLine();
126 }
127
128#endif
129
130 public static int findSignal(string sigName)
131 {
132 if (Environment.OSVersion.Platform == PlatformID.Win32NT)
133 {
134 switch (sigName)
135 {
136 case "ABRT": /* abnormal termination triggered by abort cl */
137 return SIGABRT;
138 case "FPE": /* floating point exception */
139 return SIGFPE;
140 case "SEGV": /* segment violation */
141 return SIGSEGV;
142 case "INT": /* interrupt */
143 return SIGINT;
144 case "TERM": /* software term signal from kill */
145 return SIGTERM;
146 case "BREAK": /* Ctrl-Break sequence */
147 return SIGBREAK;
148 case "ILL": /* illegal instruction */
149 return SIGILL;
150 }
151 }
152 return -1;
153 }
154
155 // this is a separate method to be able to catch the SecurityException (for the LinkDemand)
156 [SecuritySafeCritical]
157 private static void RegisterCriticalCtrlHandler()
158 {
159 defaultConsoleCtrlDelegate = new CriticalCtrlHandler();
160 }
161
162 // Register a signal handler
163 public static long handle0(int sig, long nativeH)
164 {
165 long oldHandler;
166 handler.TryGetValue(sig, out oldHandler);
167 switch (nativeH)
168 {
169 case 0: // Default Signal Handler
170 if (defaultConsoleCtrlDelegate == null && Environment.OSVersion.Platform == PlatformID.Win32NT)
171 {
172 try
173 {
174 RegisterCriticalCtrlHandler();
175 }
176 catch (SecurityException)
177 {
178 }
179 }
180 break;
181 case 1: // Ignore Signal
182 break;
183 case 2: // Custom Signal Handler
184 switch (sig)
185 {
186 case SIGBREAK:
187 case SIGFPE:
188 return -1;
189 }
190 break;
191 }
192 handler[sig] = nativeH;
193 return oldHandler;
194 }
195
196 public static void raise0(int sig)
197 {
198#if !FIRST_PASS
199 global::java.security.AccessController.doPrivileged(global::ikvm.runtime.Delegates.toPrivilegedAction(delegate
200 {
201 global::java.lang.Class clazz = typeof(global::sun.misc.Signal);
202 global::java.lang.reflect.Method dispatch = clazz.getDeclaredMethod("dispatch", global::java.lang.Integer.TYPE);
203 dispatch.setAccessible(true);
204 dispatch.invoke(null, global::java.lang.Integer.valueOf(sig));
205 return null;
206 }));
207#endif
208 }
209
210 }
211
212}
IKVM.Reflection.Assembly Assembly
static void raise0(int sig)
Definition Signal.cs:196
static int findSignal(string sigName)
Definition Signal.cs:130
static long handle0(int sig, long nativeH)
Definition Signal.cs:163