~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/ikvm/starter.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2002-2011 Jeroen Frijters
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
using System;
 
25
using System.IO;
 
26
using System.Reflection;
 
27
using IKVM.Internal;
 
28
 
 
29
using ikvm.runtime;
 
30
using java.lang.reflect;
 
31
using java.net;
 
32
using java.util.jar;
 
33
using java.io;
 
34
 
 
35
using Console = System.Console;
 
36
using System.Diagnostics;
 
37
 
 
38
public class Starter
 
39
{
 
40
        private class Timer
 
41
        {
 
42
                private static Timer t;
 
43
                private DateTime now = DateTime.Now;
 
44
 
 
45
                internal Timer()
 
46
                {
 
47
                        t = this;
 
48
                }
 
49
 
 
50
                ~Timer()
 
51
                {
 
52
                        Console.WriteLine(DateTime.Now - now);
 
53
                }
 
54
        }
 
55
 
 
56
        private class SaveAssemblyShutdownHook : java.lang.Thread
 
57
        {
 
58
                private java.lang.Class clazz;
 
59
 
 
60
                internal SaveAssemblyShutdownHook(java.lang.Class clazz)
 
61
                        : base("SaveAssemblyShutdownHook")
 
62
                {
 
63
                        this.clazz = clazz;
 
64
                }
 
65
 
 
66
                public override void run()
 
67
                {
 
68
                        System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.AboveNormal;
 
69
                        Console.Error.WriteLine("Saving dynamic assembly...");
 
70
                        try
 
71
                        {
 
72
                                IKVM.Internal.Starter.SaveDebugImage();
 
73
                                Console.Error.WriteLine("Saving done.");
 
74
                        }
 
75
                        catch(Exception x)
 
76
                        {
 
77
                                Console.Error.WriteLine(x);
 
78
                                Console.Error.WriteLine(new System.Diagnostics.StackTrace(x, true));
 
79
                                System.Diagnostics.Debug.Assert(false, x.ToString());
 
80
                        }
 
81
                }
 
82
        }
 
83
 
 
84
        private class WaitShutdownHook : java.lang.Thread
 
85
        {
 
86
                internal WaitShutdownHook()
 
87
                        : base("WaitShutdownHook")
 
88
                {
 
89
                }
 
90
 
 
91
                public override void run()
 
92
                {
 
93
                        Console.Error.WriteLine("IKVM runtime terminated. Waiting for Ctrl+C...");
 
94
                        for(;;)
 
95
                        {
 
96
                                System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
 
97
                        }
 
98
                }
 
99
        }
 
100
 
 
101
        [STAThread]     // NOTE this is here because otherwise SWT's RegisterDragDrop (a COM thing) doesn't work
 
102
        [IKVM.Attributes.HideFromJava]
 
103
        static int Main(string[] args)
 
104
        {
 
105
                Tracer.EnableTraceConsoleListener();
 
106
                Tracer.EnableTraceForDebug();
 
107
                System.Collections.Hashtable props = new System.Collections.Hashtable();
 
108
                string classpath = Environment.GetEnvironmentVariable("CLASSPATH");
 
109
                if(classpath == null || classpath == "")
 
110
                {
 
111
                        classpath = ".";
 
112
                }
 
113
                props["java.class.path"] = classpath;
 
114
                bool jar = false;
 
115
                bool saveAssembly = false;
 
116
                bool saveAssemblyX = false;
 
117
                bool waitOnExit = false;
 
118
                bool showVersion = false;
 
119
                string mainClass = null;
 
120
                int vmargsIndex = -1;
 
121
        bool debug = false;
 
122
        String debugArg = null;
 
123
                bool noglobbing = false;
 
124
                for(int i = 0; i < args.Length; i++)
 
125
                {
 
126
            String arg = args[i];
 
127
                        if(arg[0] == '-')
 
128
                        {
 
129
                                if(arg == "-help" || arg == "-?")
 
130
                                {
 
131
                                        break;
 
132
                                }
 
133
                                else if(arg == "-Xsave")
 
134
                                {
 
135
                                        saveAssembly = true;
 
136
                                        IKVM.Internal.Starter.PrepareForSaveDebugImage();
 
137
                                }
 
138
                                else if(arg == "-XXsave")
 
139
                                {
 
140
                                        saveAssemblyX = true;
 
141
                                        IKVM.Internal.Starter.PrepareForSaveDebugImage();
 
142
                                }
 
143
                                else if(arg == "-Xtime")
 
144
                                {
 
145
                                        new Timer();
 
146
                                }
 
147
                                else if(arg == "-Xwait")
 
148
                                {
 
149
                                        waitOnExit = true;
 
150
                                }
 
151
                                else if(arg == "-Xbreak")
 
152
                                {
 
153
                                        System.Diagnostics.Debugger.Break();
 
154
                                }
 
155
                                else if(arg == "-Xnoclassgc")
 
156
                                {
 
157
                                        IKVM.Internal.Starter.ClassUnloading = false;
 
158
                                }
 
159
                                else if(arg == "-Xverify")
 
160
                                {
 
161
                                        IKVM.Internal.Starter.RelaxedVerification = false;
 
162
                                }
 
163
                                else if(arg == "-jar")
 
164
                                {
 
165
                                        jar = true;
 
166
                                }
 
167
                                else if(arg == "-version")
 
168
                                {
 
169
                                        Console.WriteLine(Startup.getVersionAndCopyrightInfo());
 
170
                                        Console.WriteLine();
 
171
                                        Console.WriteLine("CLR version: {0} ({1} bit)", Environment.Version, IntPtr.Size * 8);
 
172
                                        System.Type type = System.Type.GetType("Mono.Runtime");
 
173
                                        if(type != null)
 
174
                                        {
 
175
                                                Console.WriteLine("Mono version: {0}", type.InvokeMember("GetDisplayName", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, null, new object[0]));
 
176
                                        }
 
177
                                        foreach(Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
 
178
                                        {
 
179
                                                Console.WriteLine("{0}: {1}", asm.GetName().Name, asm.GetName().Version);
 
180
                                        }
 
181
                                        string ver = java.lang.System.getProperty("openjdk.version");
 
182
                                        if(ver != null)
 
183
                                        {
 
184
                                                Console.WriteLine("OpenJDK version: {0}", ver);
 
185
                                        }
 
186
                                        return 0;
 
187
                                }
 
188
                                else if(arg == "-showversion")
 
189
                                {
 
190
                                        showVersion = true;
 
191
                                }
 
192
                                else if(arg.StartsWith("-D"))
 
193
                                {
 
194
                                    arg = arg.Substring(2);
 
195
                    string[] keyvalue = arg.Split('=');
 
196
                                    string value;
 
197
                                        if(keyvalue.Length == 2) // -Dabc=x
 
198
                                        {
 
199
                        value = keyvalue[1];
 
200
                                        } 
 
201
                    else if (keyvalue.Length == 1) // -Dabc
 
202
                    {
 
203
                        value = "";
 
204
                    } 
 
205
                    else // -Dabc=x=y
 
206
                                        {
 
207
                        value = arg.Substring(keyvalue[0].Length + 1);
 
208
                                        }
 
209
                    props[keyvalue[0]] = value;
 
210
                                }
 
211
                                else if(arg == "-ea" || arg == "-enableassertions")
 
212
                                {
 
213
                                        IKVM.Runtime.Assertions.EnableAssertions();
 
214
                                }
 
215
                                else if(arg == "-da" || arg == "-disableassertions")
 
216
                                {
 
217
                                        IKVM.Runtime.Assertions.DisableAssertions();
 
218
                                }
 
219
                                else if(arg == "-esa" || arg == "-enablesystemassertions")
 
220
                                {
 
221
                                        IKVM.Runtime.Assertions.EnableSystemAssertions();
 
222
                                }
 
223
                                else if(arg == "-dsa" || arg == "-disablesystemassertions")
 
224
                                {
 
225
                                        IKVM.Runtime.Assertions.DisableSystemAssertions();
 
226
                                }
 
227
                                else if(arg.StartsWith("-ea:") || arg.StartsWith("-enableassertions:"))
 
228
                                {
 
229
                                        IKVM.Runtime.Assertions.EnableAssertions(arg.Substring(arg.IndexOf(':') + 1));
 
230
                                }
 
231
                                else if(arg.StartsWith("-da:") || arg.StartsWith("-disableassertions:"))
 
232
                                {
 
233
                                        IKVM.Runtime.Assertions.DisableAssertions(arg.Substring(arg.IndexOf(':') + 1));
 
234
                                }
 
235
                                else if(arg == "-cp" || arg == "-classpath")
 
236
                                {
 
237
                                        props["java.class.path"] = args[++i];
 
238
                                }
 
239
                                else if(arg.StartsWith("-Xtrace:"))
 
240
                                {
 
241
                                        Tracer.SetTraceLevel(arg.Substring(8));
 
242
                                }
 
243
                                else if(arg.StartsWith("-Xmethodtrace:"))
 
244
                                {
 
245
                                        Tracer.HandleMethodTrace(arg.Substring(14));
 
246
                                }
 
247
                else if(arg == "-Xdebug")
 
248
                {
 
249
                    debug = true;
 
250
                }
 
251
                else if (arg == "-Xnoagent")
 
252
                {
 
253
                    //ignore it, disable support for oldjdb
 
254
                }
 
255
                else if (arg.StartsWith("-Xrunjdwp") || arg.StartsWith("-agentlib:jdwp"))
 
256
                {
 
257
                    debugArg = arg;
 
258
                    debug = true;
 
259
                }
 
260
                else if (arg.StartsWith("-Xreference:"))
 
261
                {
 
262
                    Startup.addBootClassPathAssemby(Assembly.LoadFrom(arg.Substring(12)));
 
263
                }
 
264
                else if (arg == "-Xnoglobbing")
 
265
                {
 
266
                    noglobbing = true;
 
267
                }
 
268
                else if (arg.StartsWith("-Xms")
 
269
                    || arg.StartsWith("-Xmx")
 
270
                    || arg.StartsWith("-Xss")
 
271
                    || arg == "-Xmixed"
 
272
                    || arg == "-Xint"
 
273
                    || arg == "-Xincgc"
 
274
                    || arg == "-Xbatch"
 
275
                    || arg == "-Xfuture"
 
276
                    || arg == "-Xrs"
 
277
                    || arg == "-Xcheck:jni"
 
278
                    || arg == "-Xshare:off"
 
279
                    || arg == "-Xshare:auto"
 
280
                    || arg == "-Xshare:on"
 
281
                    )
 
282
                {
 
283
                    Console.Error.WriteLine("Unsupported option ignored: {0}", arg);
 
284
                }
 
285
                else
 
286
                {
 
287
                    Console.Error.WriteLine("{0}: illegal argument", arg);
 
288
                    break;
 
289
                }
 
290
                        }
 
291
                        else
 
292
                        {
 
293
                                mainClass = arg;
 
294
                                vmargsIndex = i + 1;
 
295
                                break;
 
296
                        }
 
297
                }
 
298
                if(mainClass == null || showVersion)
 
299
                {
 
300
                        Console.Error.WriteLine(Startup.getVersionAndCopyrightInfo());
 
301
                        Console.Error.WriteLine();
 
302
                }
 
303
                if(mainClass == null)
 
304
                {
 
305
                        Console.Error.WriteLine("usage: ikvm [-options] <class> [args...]");
 
306
                        Console.Error.WriteLine("          (to execute a class)");
 
307
                        Console.Error.WriteLine("    or ikvm -jar [-options] <jarfile> [args...]");
 
308
                        Console.Error.WriteLine("          (to execute a jar file)");
 
309
                        Console.Error.WriteLine();
 
310
                        Console.Error.WriteLine("where options include:");
 
311
                        Console.Error.WriteLine("    -? -help          Display this message");
 
312
                        Console.Error.WriteLine("    -version          Display IKVM and runtime version");
 
313
                        Console.Error.WriteLine("    -showversion      Display version and continue running");
 
314
                        Console.Error.WriteLine("    -cp -classpath <directories and zip/jar files separated by {0}>", Path.PathSeparator);
 
315
                        Console.Error.WriteLine("                      Set search path for application classes and resources");
 
316
                        Console.Error.WriteLine("    -D<name>=<value>  Set a system property");
 
317
                        Console.Error.WriteLine("    -ea[:<packagename>...|:<classname>]");
 
318
                        Console.Error.WriteLine("    -enableassertions[:<packagename>...|:<classname>]");
 
319
                        Console.Error.WriteLine("                      Enable assertions.");
 
320
                        Console.Error.WriteLine("    -da[:<packagename>...|:<classname>]");
 
321
                        Console.Error.WriteLine("    -disableassertions[:<packagename>...|:<classname>]");
 
322
                        Console.Error.WriteLine("                      Disable assertions");
 
323
                        Console.Error.WriteLine("    -Xsave            Save the generated assembly (for debugging)");
 
324
                        Console.Error.WriteLine("    -Xtime            Time the execution");
 
325
                        Console.Error.WriteLine("    -Xtrace:<string>  Displays all tracepoints with the given name");
 
326
                        Console.Error.WriteLine("    -Xmethodtrace:<string>");
 
327
                        Console.Error.WriteLine("                      Builds method trace into the specified output methods");
 
328
                        Console.Error.WriteLine("    -Xwait            Keep process hanging around after exit");
 
329
                        Console.Error.WriteLine("    -Xbreak           Trigger a user defined breakpoint at startup");
 
330
                        Console.Error.WriteLine("    -Xnoclassgc       Disable class garbage collection");
 
331
                        Console.Error.WriteLine("    -Xnoglobbing      Disable argument globbing");
 
332
                        Console.Error.WriteLine("    -Xverify          Enable strict class file verification");
 
333
                        return 1;
 
334
                }
 
335
                try
 
336
                {
 
337
            if (debug)
 
338
            {
 
339
                // Starting the debugger
 
340
                Assembly asm = Assembly.GetExecutingAssembly();
 
341
                String arguments = debugArg + " -pid:" + System.Diagnostics.Process.GetCurrentProcess().Id;
 
342
                String program = new FileInfo(asm.Location).DirectoryName + "\\debugger.exe";
 
343
                try
 
344
                {
 
345
                    ProcessStartInfo info = new ProcessStartInfo(program, arguments);
 
346
                    info.UseShellExecute = false;
 
347
                    Process debugger = new Process();
 
348
                    debugger.StartInfo = info;
 
349
                    debugger.Start();
 
350
                }
 
351
                catch (Exception ex)
 
352
                {
 
353
                    Console.Error.WriteLine(program + " " + arguments);
 
354
                    throw ex;
 
355
                }
 
356
            }
 
357
                        if(jar)
 
358
                        {
 
359
                                props["java.class.path"] = mainClass;
 
360
                        }
 
361
                        // like the JDK we don't quote the args (even if they contain spaces)
 
362
                        props["sun.java.command"] = String.Join(" ", args, vmargsIndex - 1, args.Length - (vmargsIndex - 1));
 
363
                        props["sun.java.launcher"] = "SUN_STANDARD";
 
364
                        Startup.setProperties(props);
 
365
                        Startup.enterMainThread();
 
366
                        string[] vmargs;
 
367
                        if (noglobbing)
 
368
                        {
 
369
                                vmargs = new string[args.Length - vmargsIndex];
 
370
                                System.Array.Copy(args, vmargsIndex, vmargs, 0, vmargs.Length);
 
371
                        }
 
372
                        else
 
373
                        {
 
374
                                // Startup.glob() uses Java code, so we need to do this after we've initialized
 
375
                                vmargs = Startup.glob(args, vmargsIndex);
 
376
                        }
 
377
                        if (jar)
 
378
                        {
 
379
                                mainClass = GetMainClassFromJarManifest(mainClass);
 
380
                                if (mainClass == null)
 
381
                                {
 
382
                                        return 1;
 
383
                                }
 
384
                        }
 
385
                        java.lang.Class clazz = java.lang.Class.forName(mainClass, true, java.lang.ClassLoader.getSystemClassLoader());
 
386
                        try
 
387
                        {
 
388
                                Method method = IKVM.Internal.Starter.FindMainMethod(clazz);
 
389
                                if(method == null)
 
390
                                {
 
391
                                        throw new java.lang.NoSuchMethodError("main");
 
392
                                }
 
393
                                else if(!Modifier.isPublic(method.getModifiers()))
 
394
                                {
 
395
                                        Console.Error.WriteLine("Main method not public.");
 
396
                                }
 
397
                                else
 
398
                                {
 
399
                                        // if clazz isn't public, we can still call main
 
400
                                        method.setAccessible(true);
 
401
                                        if(saveAssembly)
 
402
                                        {
 
403
                                                java.lang.Runtime.getRuntime().addShutdownHook(new SaveAssemblyShutdownHook(clazz));
 
404
                                        }
 
405
                                        if(waitOnExit)
 
406
                                        {
 
407
                                                java.lang.Runtime.getRuntime().addShutdownHook(new WaitShutdownHook());
 
408
                                        }
 
409
                                        try
 
410
                                        {
 
411
                                                method.invoke(null, new object[] { vmargs });
 
412
                                                return 0;
 
413
                                        }
 
414
                                        catch(InvocationTargetException x)
 
415
                                        {
 
416
                                                throw x.getCause();
 
417
                                        }
 
418
                                }
 
419
                        }
 
420
                        finally
 
421
                        {
 
422
                                if(saveAssemblyX)
 
423
                                {
 
424
                                        IKVM.Internal.Starter.SaveDebugImage();
 
425
                                }
 
426
                        }
 
427
                }
 
428
                catch(System.Exception x)
 
429
                {
 
430
                        java.lang.Thread thread = java.lang.Thread.currentThread();
 
431
                        thread.getThreadGroup().uncaughtException(thread, ikvm.runtime.Util.mapException(x));
 
432
                }
 
433
                finally
 
434
                {
 
435
                        Startup.exitMainThread();
 
436
                }
 
437
                return 1;
 
438
        }
 
439
 
 
440
        private static string GetMainClassFromJarManifest(string mainClass)
 
441
        {
 
442
                JarFile jf = new JarFile(mainClass);
 
443
                try
 
444
                {
 
445
                        Manifest manifest = jf.getManifest();
 
446
                        if (manifest == null)
 
447
                        {
 
448
                                Console.Error.WriteLine("Jar file doesn't contain manifest");
 
449
                                return null;
 
450
                        }
 
451
                        mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS);
 
452
                }
 
453
                finally
 
454
                {
 
455
                        jf.close();
 
456
                }
 
457
                if (mainClass == null)
 
458
                {
 
459
                        Console.Error.WriteLine("Manifest doesn't contain a Main-Class.");
 
460
                        return null;
 
461
                }
 
462
                return mainClass.Replace('/', '.');
 
463
        }
 
464
}