1 /* src/cacao/cacao.c - contains main() of cacao
3 Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4 R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5 C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6 Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Reinhard Grafl
34 This module does the following tasks:
35 - Command line option handling
36 - Calling initialization routines
37 - Calling the class loader
38 - Running the main method
40 $Id: cacao.c 3570 2005-11-04 16:58:36Z motse $
50 #include "cacao/cacao.h"
52 #include "mm/memory.h"
53 #include "native/jni.h"
54 #include "native/native.h"
56 #if defined(ENABLE_JVMTI)
57 #include "native/jvmti/jvmti.h"
58 #include "native/jvmti/dbg.h"
59 #include <sys/types.h>
64 #include "toolbox/logging.h"
65 #include "vm/exceptions.h"
66 #include "vm/finalizer.h"
67 #include "vm/global.h"
68 #include "vm/initialize.h"
69 #include "vm/loader.h"
70 #include "vm/options.h"
71 #include "vm/signallocal.h"
72 #include "vm/statistics.h"
73 #include "vm/stringlocal.h"
74 #include "vm/tables.h"
75 #include "vm/classcache.h"
76 #include "vm/jit/asmpart.h"
77 #include "vm/jit/jit.h"
79 #ifdef TYPEINFO_DEBUG_TEST
80 #include "vm/jit/verify/typeinfo.h"
84 /* define heap sizes **********************************************************/
86 #define HEAP_MAXSIZE 64 * 1024 * 1024; /* default 64MB */
87 #define HEAP_STARTSIZE 2 * 1024 * 1024; /* default 2MB */
88 #define STACK_SIZE 128 * 1024; /* default 128kB */
90 #if defined(ENABLE_INTRP)
95 /* CACAO related stuff ********************************************************/
97 bool cacao_initializing;
101 /* Invocation API variables ***************************************************/
103 JavaVM *jvm; /* denotes a Java VM */
104 JNIEnv *env; /* pointer to native method interface */
106 JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization arguments */
109 char *bootclasspath; /* contains the boot classpath */
110 char *classpath; /* contains the classpath */
113 static classinfo *mainclass;
115 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
116 void **stackbottom = 0;
120 /* define command line options ************************************************/
122 #define OPT_CLASSPATH 2
126 #define OPT_VERBOSE1 6
127 #define OPT_VERBOSE 7
128 #define OPT_VERBOSESPECIFIC 8
129 #define OPT_VERBOSECALL 9
130 #define OPT_NOIEEE 10
131 #define OPT_SOFTNULL 11
134 #if defined(STATISTICS)
136 #endif /* defined(STATISTICS) */
141 #define OPT_METHOD 17
142 #define OPT_SIGNATURE 18
146 #define OPT_INLINING 25
148 #define STATIC_ANALYSIS
149 #if defined(STATIC_ANALYSIS)
153 #endif /* defined(STATIC_ANALYSIS) */
155 #define OPT_VERBOSETC 29
156 #define OPT_NOVERIFY 30
157 #define OPT_LIBERALUTF 31
158 #define OPT_VERBOSEEXCEPTION 32
163 #endif /* defined(LSRA) */
166 #define OPT_BOOTCLASSPATH 36
167 #define OPT_BOOTCLASSPATH_A 37
168 #define OPT_BOOTCLASSPATH_P 38
169 #define OPT_VERSION 39
170 #define OPT_SHOWVERSION 40
171 #define OPT_FULLVERSION 41
177 #define OPT_INTRP 103
179 #define OPT_STATIC_SUPERS 104
180 #define OPT_TRACE 105
185 #define OPT_DEBUG 107
186 #define OPT_AGENTLIB 108
187 #define OPT_AGENTPATH 109
190 opt_struct opts[] = {
191 { "classpath", true, OPT_CLASSPATH },
192 { "cp", true, OPT_CLASSPATH },
193 { "D", true, OPT_D },
194 { "noasyncgc", false, OPT_IGNORE },
195 { "noverify", false, OPT_NOVERIFY },
196 { "liberalutf", false, OPT_LIBERALUTF },
197 { "v", false, OPT_VERBOSE1 },
198 { "verbose", false, OPT_VERBOSE },
199 { "verbose:", true, OPT_VERBOSESPECIFIC },
200 { "verbosecall", false, OPT_VERBOSECALL },
201 { "verboseexception", false, OPT_VERBOSEEXCEPTION },
202 #ifdef TYPECHECK_VERBOSE
203 { "verbosetc", false, OPT_VERBOSETC },
205 #if defined(__ALPHA__)
206 { "noieee", false, OPT_NOIEEE },
208 { "softnull", false, OPT_SOFTNULL },
209 { "time", false, OPT_TIME },
210 #if defined(STATISTICS)
211 { "stat", false, OPT_STAT },
213 { "log", true, OPT_LOG },
214 { "c", true, OPT_CHECK },
215 { "l", false, OPT_LOAD },
216 { "eager", false, OPT_EAGER },
217 { "sig", true, OPT_SIGNATURE },
218 { "all", false, OPT_ALL },
219 { "oloop", false, OPT_OLOOP },
220 #ifdef STATIC_ANALYSIS
221 { "rt", false, OPT_RT },
222 { "xta", false, OPT_XTA },
223 { "vta", false, OPT_VTA },
226 { "lsra", false, OPT_LSRA },
228 { "jar", false, OPT_JAR },
229 { "version", false, OPT_VERSION },
230 { "showversion", false, OPT_SHOWVERSION },
231 { "fullversion", false, OPT_FULLVERSION },
232 { "help", false, OPT_HELP },
233 { "?", false, OPT_HELP },
235 /* interpreter options */
237 { "trace", false, OPT_TRACE },
238 { "static-supers", true, OPT_STATIC_SUPERS },
240 /* JVMTI Agent Command Line Options */
242 { "agentlib:", true, OPT_AGENTLIB },
243 { "agentpath:", true, OPT_AGENTPATH },
248 { "X", false, OPT_X },
249 { "Xjit", false, OPT_JIT },
250 { "Xint", false, OPT_INTRP },
251 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
252 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
253 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
255 { "Xdebug", false, OPT_DEBUG },
257 { "Xms", true, OPT_MS },
258 { "Xmx", true, OPT_MX },
259 { "Xss", true, OPT_SS },
260 { "ms", true, OPT_MS },
261 { "mx", true, OPT_MX },
262 { "ss", true, OPT_SS },
264 /* keep these at the end of the list */
266 { "i", true, OPT_INLINING },
267 { "m", true, OPT_METHOD },
268 { "s", true, OPT_SHOW },
274 /* usage ***********************************************************************
276 Prints the correct usage syntax to stdout.
278 *******************************************************************************/
280 static void usage(void)
282 printf("Usage: cacao [-options] classname [arguments]\n");
283 printf(" (to run a class file)\n");
284 printf(" cacao [-options] -jar jarfile [arguments]\n");
285 printf(" (to run a standalone jar file)\n\n");
287 printf("Java options:\n");
288 printf(" -cp <path> specify a path to look for classes\n");
289 printf(" -classpath <path> specify a path to look for classes\n");
290 printf(" -D<name>=<value> add an entry to the property list\n");
291 printf(" -verbose[:class|gc|jni] enable specific verbose output\n");
292 printf(" -version print product version and exit\n");
293 printf(" -fullversion print jpackage-compatible product version and exit\n");
294 printf(" -showversion print product version and continue\n");
295 printf(" -help, -? print this help message\n");
296 printf(" -X print help on non-standard Java options\n\n");
299 printf(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent\n");
300 printf(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent\n");
303 printf("CACAO options:\n");
304 printf(" -v write state-information\n");
305 printf(" -verbose write more information\n");
306 printf(" -verbosegc write message for each GC\n");
307 printf(" -verbosecall write message for each call\n");
308 printf(" -verboseexception write message for each step of stack unwinding\n");
309 #ifdef TYPECHECK_VERBOSE
310 printf(" -verbosetc write debug messages while typechecking\n");
312 #if defined(__ALPHA__)
313 printf(" -noieee don't use ieee compliant arithmetic\n");
315 printf(" -noverify don't verify classfiles\n");
316 printf(" -liberalutf don't warn about overlong UTF-8 sequences\n");
317 printf(" -softnull use software nullpointer check\n");
318 printf(" -time measure the runtime\n");
319 #if defined(STATISTICS)
320 printf(" -stat detailed compiler statistics\n");
322 printf(" -log logfile specify a name for the logfile\n");
323 printf(" -c(heck)b(ounds) don't check array bounds\n");
324 printf(" s(ync) don't check for synchronization\n");
325 printf(" -oloop optimize array accesses in loops\n");
326 printf(" -l don't start the class after loading\n");
327 printf(" -eager perform eager class loading and linking\n");
328 printf(" -all compile all methods, no execution\n");
329 printf(" -m compile only a specific method\n");
330 printf(" -sig specify signature for a specific method\n");
331 printf(" -s(how)a(ssembler) show disassembled listing\n");
332 printf(" c(onstants) show the constant pool\n");
333 printf(" d(atasegment) show data segment listing\n");
334 printf(" e(xceptionstubs) show disassembled exception stubs (only with -sa)\n");
335 printf(" i(ntermediate) show intermediate representation\n");
336 printf(" m(ethods) show class fields and methods\n");
337 printf(" n(ative) show disassembled native stubs\n");
338 printf(" u(tf) show the utf - hash\n");
339 printf(" -i n(line) activate inlining\n");
340 printf(" v(irtual) inline virtual methods (uses/turns rt option on)\n");
341 printf(" e(exception) inline methods with exceptions\n");
342 printf(" p(aramopt) optimize argument renaming\n");
343 printf(" o(utsiders) inline methods of foreign classes\n");
344 #ifdef STATIC_ANALYSIS
345 printf(" -rt use rapid type analysis\n");
346 printf(" -xta use x type analysis\n");
347 printf(" -vta use variable type analysis\n");
350 printf(" -lsra use linear scan register allocation\n");
353 /* exit with error code */
359 static void Xusage(void)
361 #if defined(ENABLE_JIT)
362 printf(" -Xjit JIT mode execution (default)\n");
364 #if defined(ENABLE_INTRP)
365 printf(" -Xint interpreter mode execution\n");
367 printf(" -Xbootclasspath:<zip/jar files and directories separated by :>\n");
368 printf(" value is set as bootstrap class path\n");
369 printf(" -Xbootclasspath/a:<zip/jar files and directories separated by :>\n");
370 printf(" value is appended to the bootstrap class path\n");
371 printf(" -Xbootclasspath/p:<zip/jar files and directories separated by :>\n");
372 printf(" value is prepended to the bootstrap class path\n");
373 printf(" -Xms<size> set the initial size of the heap (default: 2MB)\n");
374 printf(" -Xmx<size> set the maximum size of the heap (default: 64MB)\n");
375 printf(" -Xss<size> set the thread stack size (default: 128kB)\n");
376 #if defined(ENABLE_JVMTI)
377 printf(" -Xdebug<transport> enable remote debugging\n");
380 /* exit with error code */
386 /* version *********************************************************************
388 Only prints cacao version information.
390 *******************************************************************************/
392 static void version(void)
394 printf("java version \""JAVA_VERSION"\"\n");
395 printf("CACAO version "VERSION"\n");
397 printf("Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,\n");
398 printf("R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,\n");
399 printf("C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,\n");
400 printf("Institut f. Computersprachen - TU Wien\n\n");
402 printf("This program is free software; you can redistribute it and/or\n");
403 printf("modify it under the terms of the GNU General Public License as\n");
404 printf("published by the Free Software Foundation; either version 2, or (at\n");
405 printf("your option) any later version.\n\n");
407 printf("This program is distributed in the hope that it will be useful, but\n");
408 printf("WITHOUT ANY WARRANTY; without even the implied warranty of\n");
409 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n");
410 printf("General Public License for more details.\n");
414 /* fullversion *****************************************************************
416 Prints a Sun compatible version information (required e.g. by
417 jpackage, www.jpackage.org).
419 *******************************************************************************/
421 static void fullversion(void)
423 printf("java full version \"cacao-"JAVA_VERSION"\"\n");
431 #ifdef TYPECHECK_STATISTICS
432 void typecheck_print_statistics(FILE *file);
435 /* setup_debugger_process *****************************************************
437 Helper function to start JDWP threads
439 *******************************************************************************/
440 #if defined(ENABLE_JVMTI)
442 static void setup_debugger_process(char* transport) {
443 java_objectheader *o;
447 /* new gnu.classpath.jdwp.Jdwp() */
449 load_class_from_sysloader(utf_new_char("gnu.classpath.jdwp.Jdwp"));
451 throw_main_exception_exit();
453 o = builtin_new(mainclass);
456 throw_main_exception_exit();
458 m = class_resolveclassmethod(mainclass,
460 utf_java_lang_String__void,
461 class_java_lang_Object,
464 throw_main_exception_exit();
466 asm_calljavafunction(m, o, NULL, NULL, NULL);
468 /* configure(transport,NULL) */
469 m = class_resolveclassmethod(
470 mainclass, utf_new_char("configure"),
471 utf_new_char("(Ljava/lang/String;Ljava/lang/Thread;)V"),
472 class_java_lang_Object,
476 s = javastring_new_char(transport);
477 asm_calljavafunction(m, o, s, NULL, NULL);
479 throw_main_exception_exit();
481 /* _doInitialization */
482 m = class_resolveclassmethod(mainclass,
483 utf_new_char("_doInitialization"),
489 throw_main_exception_exit();
491 asm_calljavafunction(m, o, NULL, NULL, NULL);
496 /* getmainclassfromjar *********************************************************
498 Gets the name of the main class form a JAR's manifest file.
500 *******************************************************************************/
502 static char *getmainclassnamefromjar(char *mainstring)
505 java_objectheader *o;
509 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
512 throw_main_exception_exit();
514 /* create JarFile object */
519 throw_main_exception_exit();
522 m = class_resolveclassmethod(c,
524 utf_java_lang_String__void,
525 class_java_lang_Object,
529 throw_main_exception_exit();
531 s = javastring_new_char(mainstring);
533 asm_calljavafunction(m, o, s, NULL, NULL);
536 throw_main_exception_exit();
538 /* get manifest object */
540 m = class_resolveclassmethod(c,
541 utf_new_char("getManifest"),
542 utf_new_char("()Ljava/util/jar/Manifest;"),
543 class_java_lang_Object,
547 throw_main_exception_exit();
549 o = asm_calljavafunction(m, o, NULL, NULL, NULL);
552 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
557 /* get Main Attributes */
559 m = class_resolveclassmethod(o->vftbl->class,
560 utf_new_char("getMainAttributes"),
561 utf_new_char("()Ljava/util/jar/Attributes;"),
562 class_java_lang_Object,
566 throw_main_exception_exit();
568 o = asm_calljavafunction(m, o, NULL, NULL, NULL);
571 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
576 /* get property Main-Class */
578 m = class_resolveclassmethod(o->vftbl->class,
579 utf_new_char("getValue"),
580 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
581 class_java_lang_Object,
585 throw_main_exception_exit();
587 s = javastring_new_char("Main-Class");
589 o = asm_calljavafunction(m, o, s, NULL, NULL);
592 throw_main_exception_exit();
594 return javastring_tochar(o);
598 void exit_handler(void);
601 /* main ************************************************************************
605 *******************************************************************************/
607 int main(int argc, char **argv)
612 /* local variables ********************************************************/
614 char logfilename[200] = "";
620 char *specificmethodname = NULL;
621 char *specificsignature = NULL;
623 #if defined(ENABLE_JVMTI)
630 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
631 stackbottom = &dummy;
634 if (atexit(exit_handler))
635 throw_cacao_exception_exit(string_java_lang_InternalError,
636 "Unable to register exit_handler");
638 /* initialize global variables */
640 cacao_exiting = false;
643 /************ Collect info from the environment ************************/
645 /* set the bootclasspath */
647 cp = getenv("BOOTCLASSPATH");
649 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
650 strcpy(bootclasspath, cp);
653 cplen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_VM_ZIP_PATH) +
655 strlen(CLASSPATH_INSTALL_DIR) +
656 strlen(CLASSPATH_GLIBJ_ZIP_PATH) +
659 bootclasspath = MNEW(char, cplen);
660 strcpy(bootclasspath, CACAO_INSTALL_PREFIX);
661 strcat(bootclasspath, CACAO_VM_ZIP_PATH);
662 strcat(bootclasspath, ":");
663 strcat(bootclasspath, CLASSPATH_INSTALL_DIR);
664 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP_PATH);
668 /* set the classpath */
670 cp = getenv("CLASSPATH");
672 classpath = MNEW(char, strlen(cp) + strlen("0"));
673 strcat(classpath, cp);
676 classpath = MNEW(char, strlen(".") + strlen("0"));
677 strcpy(classpath, ".");
681 /***************** Interpret the command line *****************/
686 heapmaxsize = HEAP_MAXSIZE;
687 heapstartsize = HEAP_STARTSIZE;
688 opt_stacksize = STACK_SIZE;
690 while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
695 case OPT_BOOTCLASSPATH:
696 /* Forget default bootclasspath and set the argument as new boot */
698 MFREE(bootclasspath, char, strlen(bootclasspath));
700 bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
701 strcpy(bootclasspath, opt_arg);
704 case OPT_BOOTCLASSPATH_A:
705 /* append to end of bootclasspath */
706 cplen = strlen(bootclasspath);
708 bootclasspath = MREALLOC(bootclasspath,
711 cplen + strlen(":") +
712 strlen(opt_arg) + strlen("0"));
714 strcat(bootclasspath, ":");
715 strcat(bootclasspath, opt_arg);
718 case OPT_BOOTCLASSPATH_P:
719 /* prepend in front of bootclasspath */
723 bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
724 cplen + strlen("0"));
726 strcpy(bootclasspath, opt_arg);
727 strcat(bootclasspath, ":");
728 strcat(bootclasspath, cp);
730 MFREE(cp, char, cplen);
734 /* forget old classpath and set the argument as new classpath */
735 MFREE(classpath, char, strlen(classpath));
737 classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
738 strcpy(classpath, opt_arg);
745 #if defined(ENABLE_JVMTI)
753 set_jvmti_phase(JVMTI_PHASE_ONLOAD);
755 set_jvmti_phase(JVMTI_PHASE_PRIMORDIAL);
761 for (j = 0; j < strlen(opt_arg); j++) {
762 if (opt_arg[j] == '=') {
764 create_property(opt_arg, opt_arg + j + 1);
769 /* if no '=' is given, just create an empty property */
770 create_property(opt_arg, "");
781 c = opt_arg[strlen(opt_arg) - 1];
783 if (c == 'k' || c == 'K') {
784 j = 1024 * atoi(opt_arg);
786 } else if (c == 'm' || c == 'M') {
787 j = 1024 * 1024 * atoi(opt_arg);
794 else if (i == OPT_MS)
810 compileverbose = true;
813 case OPT_VERBOSESPECIFIC:
814 if (strcmp("class", opt_arg) == 0)
815 opt_verboseclass = true;
817 else if (strcmp("gc", opt_arg) == 0)
818 opt_verbosegc = true;
820 else if (strcmp("jni", opt_arg) == 0)
821 opt_verbosejni = true;
824 case OPT_VERBOSEEXCEPTION:
825 opt_verboseexception = true;
828 #ifdef TYPECHECK_VERBOSE
830 typecheckverbose = true;
834 case OPT_VERBOSECALL:
843 case OPT_FULLVERSION:
847 case OPT_SHOWVERSION:
860 opt_liberalutf = true;
868 getcompilingtime = true;
869 getloadingtime = true;
872 #if defined(STATISTICS)
879 strcpy(logfilename, opt_arg);
883 for (j = 0; j < strlen(opt_arg); j++) {
884 switch (opt_arg[j]) {
899 makeinitializations = false;
908 specificmethodname = opt_arg;
909 makeinitializations = false;
913 specificsignature = opt_arg;
919 makeinitializations = false;
922 case OPT_SHOW: /* Display options */
923 for (j = 0; j < strlen(opt_arg); j++) {
924 switch (opt_arg[j]) {
926 opt_showdisassemble = true;
927 compileverbose = true;
930 showconstantpool = true;
933 opt_showddatasegment = true;
936 opt_showexceptionstubs = true;
939 opt_showintermediate = true;
940 compileverbose = true;
946 opt_shownativestub = true;
962 for (j = 0; j < strlen(opt_arg); j++) {
963 switch (opt_arg[j]) {
965 /* define in options.h; Used in main.c, jit.c & inline.c */
974 inlinevirtuals = true;
978 inlineexceptions = true;
981 inlineparamopt = true;
984 inlineoutsiders = true;
992 #ifdef STATIC_ANALYSIS
994 opt_rt = true; /* default for inlining */
998 opt_xta = true; /* in test currently */
1002 printf("\nVTA is not yet available\n");
1004 /***opt_vta = true; not yet **/
1023 #if defined(ENABLE_JIT)
1026 printf("-Xjit option not enabled.\n");
1032 #if defined(ENABLE_INTRP)
1035 printf("-Xint option not enabled.\n");
1040 case OPT_STATIC_SUPERS:
1041 opt_static_supers = atoi(opt_arg);
1049 printf("Unknown option: %s\n", argv[opt_ind]);
1054 if (opt_ind >= argc)
1058 /* transform dots into slashes in the class name */
1060 mainstring = argv[opt_ind++];
1063 /* do not mangle jar filename */
1065 for (i = strlen(mainstring) - 1; i >= 0; i--) {
1066 if (mainstring[i] == '.') mainstring[i] = '/';
1070 /* put jarfile in classpath */
1074 classpath = MNEW(char, strlen(mainstring) + strlen(":") +
1075 strlen(classpath) + strlen("0"));
1077 strcpy(classpath, mainstring);
1078 strcat(classpath, ":");
1079 strcat(classpath, cp);
1081 MFREE(cp, char, strlen(cp));
1084 /**************************** Program start *****************************/
1086 log_init(logfilename);
1089 log_text("CACAO started -------------------------------------------------------");
1091 /* initialize JavaVM */
1093 vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
1095 /* Get the default initialization arguments and set the class path */
1097 JNI_GetDefaultJavaVMInitArgs(&vm_args);
1099 vm_args.minHeapSize = heapstartsize;
1100 vm_args.maxHeapSize = heapmaxsize;
1102 vm_args.classpath = classpath;
1104 /* load and initialize a Java VM, return a JNI interface pointer in env */
1106 JNI_CreateJavaVM(&jvm, &env, &vm_args);
1108 #if defined(ENABLE_JVMTI)
1109 set_jvmti_phase(JVMTI_PHASE_START);
1112 /* initialize the garbage collector */
1114 gc_init(heapmaxsize, heapstartsize);
1116 #if defined(ENABLE_INTRP)
1117 /* allocate main thread stack */
1120 intrp_main_stack = (u1 *) alloca(opt_stacksize);
1121 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1127 /* initialize the loader with bootclasspath */
1129 suck_init(bootclasspath);
1131 cacao_initializing = true;
1133 #if defined(USE_THREADS)
1134 #if defined(NATIVE_THREADS)
1140 /* initialize the memory subsystem (must be done _after_
1144 throw_main_exception_exit();
1146 /* initialize the finalizer stuff: lock, linked list (must be done
1147 _after_ threads_preinit) */
1149 if (!finalizer_init())
1150 throw_main_exception_exit();
1152 /* install architecture dependent signal handler used for exceptions */
1156 /* initialize the codegen subsystems */
1160 /* initializes jit compiler */
1164 /* machine dependent initialization */
1168 /* initialize some cacao subsystems */
1172 if (!loader_init((u1 *) &dummy))
1173 throw_main_exception_exit();
1176 throw_main_exception_exit();
1179 throw_main_exception_exit();
1181 if (!exceptions_init())
1182 throw_main_exception_exit();
1184 if (!builtin_init())
1185 throw_main_exception_exit();
1187 #if defined(USE_THREADS)
1188 if (!threads_init((u1 *) &dummy))
1189 throw_main_exception_exit();
1192 /* That's important, otherwise we get into trouble, if the Runtime
1193 static initializer is called before (circular dependency. This
1194 is with classpath 0.09. Another important thing is, that this
1195 has to happen after initThreads!!! */
1197 if (!initialize_class(class_java_lang_System))
1198 throw_main_exception_exit();
1200 /* JNI init creates a Java object (this means running Java code) */
1203 throw_main_exception_exit();
1205 #if defined(USE_THREADS)
1206 /* finally, start the finalizer thread */
1208 if (!finalizer_start_thread())
1209 throw_main_exception_exit();
1212 cacao_initializing = false;
1215 /* start worker routines **************************************************/
1218 classinfo *mainclass; /* java/lang/Class */
1220 java_objectarray *a;
1223 /* set return value to OK */
1228 /* open jar file with java.util.jar.JarFile */
1229 mainstring = getmainclassnamefromjar(mainstring);
1232 /* load the main class */
1234 if (!(mainclass = load_class_from_sysloader(utf_new_char(mainstring))))
1235 throw_main_exception_exit();
1237 /* error loading class, clear exceptionptr for new exception */
1239 if (*exceptionptr || !mainclass) {
1240 /* *exceptionptr = NULL; */
1242 /* *exceptionptr = */
1243 /* new_exception_message(string_java_lang_NoClassDefFoundError, */
1245 throw_main_exception_exit();
1248 /* find the `main' method of the main class */
1250 m = class_resolveclassmethod(mainclass,
1251 utf_new_char("main"),
1252 utf_new_char("([Ljava/lang/String;)V"),
1253 class_java_lang_Object,
1256 if (*exceptionptr) {
1257 throw_main_exception_exit();
1260 /* there is no main method or it isn't static */
1262 if (!m || !(m->flags & ACC_STATIC)) {
1263 *exceptionptr = NULL;
1266 new_exception_message(string_java_lang_NoSuchMethodError,
1268 throw_main_exception_exit();
1271 /* build argument array */
1273 a = builtin_anewarray(argc - opt_ind, class_java_lang_String);
1274 for (i = opt_ind; i < argc; i++) {
1275 a->data[i - opt_ind] =
1276 (java_objectheader *) javastring_new(utf_new_char(argv[i]));
1279 #ifdef TYPEINFO_DEBUG_TEST
1280 /* test the typeinfo system */
1283 /*class_showmethods(currentThread->group->header.vftbl->class); */
1285 #if defined(ENABLE_JVMTI) && defined(NATIVE_THREADS)
1288 if (debuggee == (-1)) {
1289 log_text("fork error");
1292 if (debuggee == 0) {
1293 /* child: allow helper process to trace us */
1294 if (TRACEME != 0) exit(0);
1296 /* give parent/helper debugger process control */
1297 kill(0, SIGTRAP); /* do we need this at this stage ? */
1299 /* continue with normal startup */
1303 /* parent/helper debugger process */
1306 remotedbgjvmtienv = new_jvmtienv();
1307 /* set eventcallbacks */
1308 if (JVMTI_ERROR_NONE ==
1310 SetEventCallbacks(remotedbgjvmtienv,
1311 &jvmti_jdwp_EventCallbacks,
1312 sizeof(jvmti_jdwp_EventCallbacks))){
1313 log_text("unable to setup event callbacks");
1317 /* setup listening process (JDWP) */
1318 setup_debugger_process(transport);
1320 /* start to be debugged program */
1323 /* exit debugger process - todo: cleanup */
1335 asm_calljavafunction(m, a, NULL, NULL, NULL);
1337 /* exception occurred? */
1338 if (*exceptionptr) {
1339 throw_main_exception();
1343 #if defined(USE_THREADS)
1344 #if defined(NATIVE_THREADS)
1347 killThread(currentThread);
1351 /* now exit the JavaVM */
1353 /* (*jvm)->DestroyJavaVM(jvm); */
1358 /************* If requested, compile all methods ********************/
1365 classcache_name_entry *nmen;
1366 classcache_class_entry *clsen;
1368 /* create all classes found in the classpath */
1369 /* XXX currently only works with zip/jar's */
1371 loader_load_all_classes();
1373 /* link all classes */
1375 for (slot = 0; slot < classcache_hash.size; slot++) {
1376 nmen = (classcache_name_entry *) classcache_hash.ptr[slot];
1378 for (; nmen; nmen = nmen->hashlink) {
1379 /* iterate over all class entries */
1381 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
1382 c = clsen->classobj;
1389 /*utf_fprint_classname(stderr,c->name);fprintf(stderr,"\n");*/
1393 throw_main_exception_exit();
1395 /* compile all class methods */
1396 for (i = 0; i < c->methodscount; i++) {
1397 m = &(c->methods[i]);
1399 /*fprintf(stderr," compiling:");utf_fprint(stderr,m->name);fprintf(stderr,"\n");*/
1400 (void) jit_compile(m);
1409 /******** If requested, compile a specific method ***************/
1411 if (specificmethodname) {
1414 /* create, load and link the main class */
1416 if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
1417 throw_main_exception_exit();
1419 if (!link_class(mainclass))
1420 throw_main_exception_exit();
1422 if (specificsignature) {
1423 m = class_resolveclassmethod(mainclass,
1424 utf_new_char(specificmethodname),
1425 utf_new_char(specificsignature),
1429 m = class_resolveclassmethod(mainclass,
1430 utf_new_char(specificmethodname),
1437 char message[MAXLOGTEXT];
1438 sprintf(message, "%s%s", specificmethodname,
1439 specificsignature ? specificsignature : "");
1442 new_exception_message(string_java_lang_NoSuchMethodException,
1445 throw_main_exception_exit();
1453 /* keep compiler happy */
1459 /* cacao_exit ******************************************************************
1461 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1463 *******************************************************************************/
1465 void cacao_exit(s4 status)
1469 assert(class_java_lang_System);
1470 assert(class_java_lang_System->loaded);
1472 #if defined(ENABLE_JVMTI)
1473 set_jvmti_phase(JVMTI_PHASE_DEAD);
1477 if (!link_class(class_java_lang_System))
1478 throw_main_exception_exit();
1480 /* signal that we are exiting */
1482 cacao_exiting = true;
1484 /* call java.lang.System.exit(I)V */
1486 m = class_resolveclassmethod(class_java_lang_System,
1487 utf_new_char("exit"),
1489 class_java_lang_Object,
1493 throw_main_exception_exit();
1495 /* call the exit function with passed exit status */
1497 /* both inlinevirtual and outsiders not allowed on exit */
1498 /* not sure if permanant or temp restriction */
1499 if (inlinevirtuals) inlineoutsiders = false;
1501 asm_calljavafunction(m, (void *) (ptrint) status, NULL, NULL, NULL);
1503 /* this should never happen */
1506 throw_exception_exit();
1508 throw_cacao_exception_exit(string_java_lang_InternalError,
1509 "System.exit(I)V returned without exception");
1513 /*************************** Shutdown function *********************************
1515 Terminates the system immediately without freeing memory explicitly (to be
1516 used only for abnormal termination)
1518 *******************************************************************************/
1520 void cacao_shutdown(s4 status)
1523 #if defined(ENABLE_JVMTI)
1527 if (opt_verbose || getcompilingtime || opt_stat) {
1528 log_text("CACAO terminated by shutdown");
1529 dolog("Exit status: %d\n", (s4) status);
1536 /* exit_handler ****************************************************************
1538 The exit_handler function is called upon program termination.
1540 ATTENTION: Don't free system resources here! Some threads may still
1541 be running as this is called from VMRuntime.exit(). The OS does the
1544 *******************************************************************************/
1546 void exit_handler(void)
1548 /********************* Print debug tables ************************/
1550 if (showmethods) class_showmethods(mainclass);
1551 if (showconstantpool) class_showconstantpool(mainclass);
1552 if (showutf) utf_show();
1554 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
1555 clear_thread_flags(); /* restores standard file descriptor
1559 if (opt_verbose || getcompilingtime || opt_stat) {
1560 log_text("CACAO terminated");
1562 #if defined(STATISTICS)
1565 #ifdef TYPECHECK_STATISTICS
1566 typecheck_print_statistics(get_logfile());
1572 if (getcompilingtime)
1576 /* vm_print_profile(stderr);*/
1581 * These are local overrides for various environment variables in Emacs.
1582 * Please do not remove this and leave it at the end of the file, where
1583 * Emacs will automagically detect them.
1584 * ---------------------------------------------------------------------
1587 * indent-tabs-mode: t