1 /* src/cacao/cacao.c - contains main() of cacao
3 Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, J. Wenninger, 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., 51 Franklin Street, Fifth Floor, Boston, MA
25 Contact: cacao@cacaojvm.org
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 4474 2006-02-06 21:01:54Z twisti $
53 #include "cacao/cacao.h"
55 #include "mm/memory.h"
56 #include "native/jni.h"
57 #include "native/native.h"
59 #if defined(ENABLE_JVMTI)
60 #include "native/jvmti/jvmti.h"
61 #include "native/jvmti/dbg.h"
62 #include <sys/types.h>
67 #include "toolbox/logging.h"
68 #include "vm/classcache.h"
69 #include "vm/exceptions.h"
70 #include "vm/finalizer.h"
71 #include "vm/global.h"
72 #include "vm/initialize.h"
73 #include "vm/loader.h"
74 #include "vm/options.h"
75 #include "vm/properties.h"
76 #include "vm/signallocal.h"
77 #include "vm/statistics.h"
78 #include "vm/stringlocal.h"
80 #include "vm/jit/asmpart.h"
81 #include "vm/jit/jit.h"
82 #include "vm/jit/profile/profile.h"
84 #ifdef TYPEINFO_DEBUG_TEST
85 #include "vm/jit/verify/typeinfo.h"
89 /* define heap sizes **********************************************************/
91 #define HEAP_MAXSIZE 64 * 1024 * 1024 /* default 64MB */
92 #define HEAP_STARTSIZE 2 * 1024 * 1024 /* default 2MB */
93 #define STACK_SIZE 128 * 1024 /* default 128kB */
95 #if defined(ENABLE_INTRP)
100 /* CACAO related stuff ********************************************************/
102 bool cacao_initializing;
106 /* Invocation API variables ***************************************************/
108 JavaVM *jvm; /* denotes a Java VM */
109 JNIEnv *env; /* pointer to native method interface */
111 JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization arguments */
115 static classinfo *mainclass;
117 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
118 void **stackbottom = 0;
122 /* define command line options ************************************************/
137 #if defined(ENABLE_STATISTICS)
154 OPT_VERBOSEEXCEPTION,
157 #if defined(ENABLE_LSRA)
178 #if defined(ENABLE_INTRP)
179 /* interpreter options */
200 opt_struct opts[] = {
201 { "classpath", true, OPT_CLASSPATH },
202 { "cp", true, OPT_CLASSPATH },
203 { "D", true, OPT_D },
204 { "noasyncgc", false, OPT_IGNORE },
205 { "noverify", false, OPT_NOVERIFY },
206 { "liberalutf", false, OPT_LIBERALUTF },
207 { "v", false, OPT_VERBOSE1 },
208 { "verbose", false, OPT_VERBOSE },
209 { "verbose:", true, OPT_VERBOSESPECIFIC },
210 { "verbosecall", false, OPT_VERBOSECALL },
211 { "verboseexception", false, OPT_VERBOSEEXCEPTION },
212 #ifdef TYPECHECK_VERBOSE
213 { "verbosetc", false, OPT_VERBOSETC },
215 #if defined(__ALPHA__)
216 { "noieee", false, OPT_NOIEEE },
218 { "softnull", false, OPT_SOFTNULL },
219 { "time", false, OPT_TIME },
220 #if defined(ENABLE_STATISTICS)
221 { "stat", false, OPT_STAT },
223 { "log", true, OPT_LOG },
224 { "c", true, OPT_CHECK },
225 { "l", false, OPT_LOAD },
226 { "eager", false, OPT_EAGER },
227 { "sig", true, OPT_SIGNATURE },
228 { "all", false, OPT_ALL },
229 { "oloop", false, OPT_OLOOP },
230 #if defined(ENABLE_LSRA)
231 { "lsra", false, OPT_LSRA },
233 { "jar", false, OPT_JAR },
234 { "version", false, OPT_VERSION },
235 { "showversion", false, OPT_SHOWVERSION },
236 { "fullversion", false, OPT_FULLVERSION },
237 { "help", false, OPT_HELP },
238 { "?", false, OPT_HELP },
240 #if defined(ENABLE_INTRP)
241 /* interpreter options */
243 { "trace", false, OPT_TRACE },
244 { "static-supers", true, OPT_STATIC_SUPERS },
245 { "no-dynamic", false, OPT_NO_DYNAMIC },
246 { "no-replication", false, OPT_NO_REPLICATION },
247 { "no-quicksuper", false, OPT_NO_QUICKSUPER },
250 /* JVMTI Agent Command Line Options */
252 { "agentlib:", true, OPT_AGENTLIB },
253 { "agentpath:", true, OPT_AGENTPATH },
258 { "X", false, OPT_X },
259 { "Xjit", false, OPT_JIT },
260 { "Xint", false, OPT_INTRP },
261 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
262 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
263 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
265 { "Xdebug", false, OPT_DEBUG },
267 { "Xms", true, OPT_MS },
268 { "Xmx", true, OPT_MX },
269 { "Xprof:", true, OPT_PROF_OPTION },
270 { "Xprof", false, OPT_PROF },
271 { "Xss", true, OPT_SS },
272 { "ms", true, OPT_MS },
273 { "mx", true, OPT_MX },
274 { "ss", true, OPT_SS },
276 /* keep these at the end of the list */
278 { "i", true, OPT_INLINING },
279 { "m", true, OPT_METHOD },
280 { "s", true, OPT_SHOW },
286 /* usage ***********************************************************************
288 Prints the correct usage syntax to stdout.
290 *******************************************************************************/
292 static void usage(void)
294 printf("Usage: cacao [-options] classname [arguments]\n");
295 printf(" (to run a class file)\n");
296 printf(" cacao [-options] -jar jarfile [arguments]\n");
297 printf(" (to run a standalone jar file)\n\n");
299 printf("Java options:\n");
300 printf(" -cp <path> specify a path to look for classes\n");
301 printf(" -classpath <path> specify a path to look for classes\n");
302 printf(" -D<name>=<value> add an entry to the property list\n");
303 printf(" -verbose[:class|gc|jni] enable specific verbose output\n");
304 printf(" -version print product version and exit\n");
305 printf(" -fullversion print jpackage-compatible product version and exit\n");
306 printf(" -showversion print product version and continue\n");
307 printf(" -help, -? print this help message\n");
308 printf(" -X print help on non-standard Java options\n\n");
311 printf(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent\n");
312 printf(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent\n");
315 printf("CACAO options:\n");
316 printf(" -v write state-information\n");
317 printf(" -verbose write more information\n");
318 printf(" -verbosegc write message for each GC\n");
319 printf(" -verbosecall write message for each call\n");
320 printf(" -verboseexception write message for each step of stack unwinding\n");
321 #ifdef TYPECHECK_VERBOSE
322 printf(" -verbosetc write debug messages while typechecking\n");
324 #if defined(__ALPHA__)
325 printf(" -noieee don't use ieee compliant arithmetic\n");
327 printf(" -noverify don't verify classfiles\n");
328 printf(" -liberalutf don't warn about overlong UTF-8 sequences\n");
329 printf(" -softnull use software nullpointer check\n");
330 printf(" -time measure the runtime\n");
331 #if defined(ENABLE_STATISTICS)
332 printf(" -stat detailed compiler statistics\n");
334 printf(" -log logfile specify a name for the logfile\n");
335 printf(" -c(heck)b(ounds) don't check array bounds\n");
336 printf(" s(ync) don't check for synchronization\n");
337 printf(" -oloop optimize array accesses in loops\n");
338 printf(" -l don't start the class after loading\n");
339 printf(" -eager perform eager class loading and linking\n");
340 printf(" -all compile all methods, no execution\n");
341 printf(" -m compile only a specific method\n");
342 printf(" -sig specify signature for a specific method\n");
343 printf(" -s(how)a(ssembler) show disassembled listing\n");
344 printf(" c(onstants) show the constant pool\n");
345 printf(" d(atasegment) show data segment listing\n");
346 printf(" e(xceptionstubs) show disassembled exception stubs (only with -sa)\n");
347 printf(" i(ntermediate) show intermediate representation\n");
348 printf(" m(ethods) show class fields and methods\n");
349 printf(" n(ative) show disassembled native stubs\n");
350 printf(" u(tf) show the utf - hash\n");
351 printf(" -i n(line) activate inlining\n");
352 printf(" v(irtual) inline virtual methods (uses/turns rt option on)\n");
353 printf(" e(exception) inline methods with exceptions\n");
354 printf(" p(aramopt) optimize argument renaming\n");
355 printf(" o(utsiders) inline methods of foreign classes\n");
356 #if defined(ENABLE_LSRA)
357 printf(" -lsra use linear scan register allocation\n");
360 /* exit with error code */
366 static void Xusage(void)
368 #if defined(ENABLE_JIT)
369 printf(" -Xjit JIT mode execution (default)\n");
371 #if defined(ENABLE_INTRP)
372 printf(" -Xint interpreter mode execution\n");
374 printf(" -Xbootclasspath:<zip/jar files and directories separated by :>\n");
375 printf(" value is set as bootstrap class path\n");
376 printf(" -Xbootclasspath/a:<zip/jar files and directories separated by :>\n");
377 printf(" value is appended to the bootstrap class path\n");
378 printf(" -Xbootclasspath/p:<zip/jar files and directories separated by :>\n");
379 printf(" value is prepended to the bootstrap class path\n");
380 printf(" -Xms<size> set the initial size of the heap (default: 2MB)\n");
381 printf(" -Xmx<size> set the maximum size of the heap (default: 64MB)\n");
382 printf(" -Xss<size> set the thread stack size (default: 128kB)\n");
383 printf(" -Xprof[:bb] collect and print profiling data\n");
384 #if defined(ENABLE_JVMTI)
385 printf(" -Xdebug<transport> enable remote debugging\n");
388 /* exit with error code */
394 /* version *********************************************************************
396 Only prints cacao version information.
398 *******************************************************************************/
400 static void version(void)
402 printf("java version \""JAVA_VERSION"\"\n");
403 printf("CACAO version "VERSION"\n");
405 printf("Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,\n");
406 printf("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,\n");
407 printf("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,\n");
408 printf("J. Wenninger, Institut f. Computersprachen - TU Wien\n\n");
410 printf("This program is free software; you can redistribute it and/or\n");
411 printf("modify it under the terms of the GNU General Public License as\n");
412 printf("published by the Free Software Foundation; either version 2, or (at\n");
413 printf("your option) any later version.\n\n");
415 printf("This program is distributed in the hope that it will be useful, but\n");
416 printf("WITHOUT ANY WARRANTY; without even the implied warranty of\n");
417 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n");
418 printf("General Public License for more details.\n");
422 /* fullversion *****************************************************************
424 Prints a Sun compatible version information (required e.g. by
425 jpackage, www.jpackage.org).
427 *******************************************************************************/
429 static void fullversion(void)
431 printf("java full version \"cacao-"JAVA_VERSION"\"\n");
439 #ifdef TYPECHECK_STATISTICS
440 void typecheck_print_statistics(FILE *file);
443 /* setup_debugger_process *****************************************************
445 Helper function to start JDWP threads
447 *******************************************************************************/
448 #if defined(ENABLE_JVMTI)
450 static void setup_debugger_process(char* transport) {
451 java_objectheader *o;
455 /* new gnu.classpath.jdwp.Jdwp() */
457 load_class_from_sysloader(utf_new_char("gnu.classpath.jdwp.Jdwp"));
459 throw_main_exception_exit();
461 o = builtin_new(mainclass);
464 throw_main_exception_exit();
466 m = class_resolveclassmethod(mainclass,
468 utf_java_lang_String__void,
469 class_java_lang_Object,
472 throw_main_exception_exit();
474 ASM_CALLJAVAFUNCTION(m, o, NULL, NULL, NULL);
476 /* configure(transport,NULL) */
477 m = class_resolveclassmethod(
478 mainclass, utf_new_char("configure"),
479 utf_new_char("(Ljava/lang/String;Ljava/lang/Thread;)V"),
480 class_java_lang_Object,
484 s = javastring_new_char(transport);
486 ASM_CALLJAVAFUNCTION(m, o, s, NULL, NULL);
489 throw_main_exception_exit();
491 /* _doInitialization */
492 m = class_resolveclassmethod(mainclass,
493 utf_new_char("_doInitialization"),
499 throw_main_exception_exit();
501 ASM_CALLJAVAFUNCTION(m, o, NULL, NULL, NULL);
506 /* getmainclassfromjar *********************************************************
508 Gets the name of the main class form a JAR's manifest file.
510 *******************************************************************************/
512 static char *getmainclassnamefromjar(char *mainstring)
515 java_objectheader *o;
519 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
522 throw_main_exception_exit();
524 /* create JarFile object */
529 throw_main_exception_exit();
532 m = class_resolveclassmethod(c,
534 utf_java_lang_String__void,
535 class_java_lang_Object,
539 throw_main_exception_exit();
541 s = javastring_new_char(mainstring);
543 ASM_CALLJAVAFUNCTION(m, o, s, NULL, NULL);
546 throw_main_exception_exit();
548 /* get manifest object */
550 m = class_resolveclassmethod(c,
551 utf_new_char("getManifest"),
552 utf_new_char("()Ljava/util/jar/Manifest;"),
553 class_java_lang_Object,
557 throw_main_exception_exit();
559 ASM_CALLJAVAFUNCTION_ADR(o, m, o, NULL, NULL, NULL);
562 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
567 /* get Main Attributes */
569 m = class_resolveclassmethod(o->vftbl->class,
570 utf_new_char("getMainAttributes"),
571 utf_new_char("()Ljava/util/jar/Attributes;"),
572 class_java_lang_Object,
576 throw_main_exception_exit();
578 ASM_CALLJAVAFUNCTION_ADR(o, m, o, NULL, NULL, NULL);
581 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
586 /* get property Main-Class */
588 m = class_resolveclassmethod(o->vftbl->class,
589 utf_new_char("getValue"),
590 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
591 class_java_lang_Object,
595 throw_main_exception_exit();
597 s = javastring_new_char("Main-Class");
599 ASM_CALLJAVAFUNCTION_ADR(o, m, o, s, NULL, NULL);
602 throw_main_exception_exit();
604 return javastring_tochar(o);
608 void exit_handler(void);
611 /* main ************************************************************************
615 *******************************************************************************/
617 int main(int argc, char **argv)
622 /* local variables ********************************************************/
624 char logfilename[200] = "";
630 char *specificmethodname = NULL;
631 char *specificsignature = NULL;
633 #if defined(ENABLE_JVMTI)
640 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
641 stackbottom = &dummy;
644 if (atexit(exit_handler))
645 throw_cacao_exception_exit(string_java_lang_InternalError,
646 "Unable to register exit_handler");
648 /* initialize global variables */
650 cacao_exiting = false;
653 /************ Collect info from the environment ************************/
655 #if defined(DISABLE_GC)
656 nogc_init(HEAP_MAXSIZE, HEAP_STARTSIZE);
659 /* set the bootclasspath */
661 cp = getenv("BOOTCLASSPATH");
664 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
665 strcpy(bootclasspath, cp);
668 cplen = strlen(CACAO_VM_ZIP_PATH) +
670 strlen(CLASSPATH_GLIBJ_ZIP_PATH) +
673 bootclasspath = MNEW(char, cplen);
674 strcat(bootclasspath, CACAO_VM_ZIP_PATH);
675 strcat(bootclasspath, ":");
676 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP_PATH);
680 /* set the classpath */
682 cp = getenv("CLASSPATH");
685 classpath = MNEW(char, strlen(cp) + strlen("0"));
686 strcat(classpath, cp);
689 classpath = MNEW(char, strlen(".") + strlen("0"));
690 strcpy(classpath, ".");
694 /***************** Interpret the command line *****************/
699 heapmaxsize = HEAP_MAXSIZE;
700 heapstartsize = HEAP_STARTSIZE;
701 opt_stacksize = STACK_SIZE;
703 /* initialize properties before commandline handling */
705 if (!properties_init())
706 throw_cacao_exception_exit(string_java_lang_InternalError,
707 "Unable to init properties");
709 while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
714 case OPT_BOOTCLASSPATH:
715 /* Forget default bootclasspath and set the argument as new boot */
717 MFREE(bootclasspath, char, strlen(bootclasspath));
719 bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
720 strcpy(bootclasspath, opt_arg);
723 case OPT_BOOTCLASSPATH_A:
724 /* append to end of bootclasspath */
725 cplen = strlen(bootclasspath);
727 bootclasspath = MREALLOC(bootclasspath,
730 cplen + strlen(":") +
731 strlen(opt_arg) + strlen("0"));
733 strcat(bootclasspath, ":");
734 strcat(bootclasspath, opt_arg);
737 case OPT_BOOTCLASSPATH_P:
738 /* prepend in front of bootclasspath */
742 bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
743 cplen + strlen("0"));
745 strcpy(bootclasspath, opt_arg);
746 strcat(bootclasspath, ":");
747 strcat(bootclasspath, cp);
749 MFREE(cp, char, cplen);
753 /* forget old classpath and set the argument as new classpath */
754 MFREE(classpath, char, strlen(classpath));
756 classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
757 strcpy(classpath, opt_arg);
764 #if defined(ENABLE_JVMTI)
772 set_jvmti_phase(JVMTI_PHASE_ONLOAD);
774 set_jvmti_phase(JVMTI_PHASE_PRIMORDIAL);
780 for (j = 0; j < strlen(opt_arg); j++) {
781 if (opt_arg[j] == '=') {
783 properties_add(opt_arg, opt_arg + j + 1);
788 /* if no '=' is given, just create an empty property */
790 properties_add(opt_arg, "");
802 c = opt_arg[strlen(opt_arg) - 1];
804 if (c == 'k' || c == 'K') {
805 j = 1024 * atoi(opt_arg);
807 } else if (c == 'm' || c == 'M') {
808 j = 1024 * 1024 * atoi(opt_arg);
815 else if (i == OPT_MS)
831 compileverbose = true;
834 case OPT_VERBOSESPECIFIC:
835 if (strcmp("class", opt_arg) == 0)
836 opt_verboseclass = true;
838 else if (strcmp("gc", opt_arg) == 0)
839 opt_verbosegc = true;
841 else if (strcmp("jni", opt_arg) == 0)
842 opt_verbosejni = true;
845 case OPT_VERBOSEEXCEPTION:
846 opt_verboseexception = true;
849 #ifdef TYPECHECK_VERBOSE
851 typecheckverbose = true;
855 case OPT_VERBOSECALL:
864 case OPT_FULLVERSION:
868 case OPT_SHOWVERSION:
881 opt_liberalutf = true;
889 getcompilingtime = true;
890 getloadingtime = true;
893 #if defined(ENABLE_STATISTICS)
900 strcpy(logfilename, opt_arg);
904 for (j = 0; j < strlen(opt_arg); j++) {
905 switch (opt_arg[j]) {
920 makeinitializations = false;
929 specificmethodname = opt_arg;
930 makeinitializations = false;
934 specificsignature = opt_arg;
940 makeinitializations = false;
943 case OPT_SHOW: /* Display options */
944 for (j = 0; j < strlen(opt_arg); j++) {
945 switch (opt_arg[j]) {
947 opt_showdisassemble = true;
948 compileverbose = true;
951 showconstantpool = true;
954 opt_showddatasegment = true;
957 opt_showexceptionstubs = true;
960 opt_showintermediate = true;
961 compileverbose = true;
967 opt_shownativestub = true;
983 for (j = 0; j < strlen(opt_arg); j++) {
984 switch (opt_arg[j]) {
986 /* define in options.h; Used in main.c, jit.c & inline.c */
987 /* inlining is currently deactivated */
990 inlinevirtuals = true;
993 inlineexceptions = true;
996 inlineparamopt = true;
999 inlineoutsiders = true;
1007 #if defined(ENABLE_LSRA)
1021 case OPT_PROF_OPTION:
1022 /* use <= to get the last \0 too */
1024 for (j = 0, k = 0; j <= strlen(opt_arg); j++) {
1025 if (opt_arg[j] == ',')
1028 if (opt_arg[j] == '\0') {
1029 if (strcmp("bb", opt_arg + k) == 0)
1033 printf("Unknown option: -Xprof:%s\n", opt_arg + k);
1037 /* set k to next char */
1049 #if defined(ENABLE_JIT)
1052 printf("-Xjit option not enabled.\n");
1058 #if defined(ENABLE_INTRP)
1061 printf("-Xint option not enabled.\n");
1066 #if defined(ENABLE_INTRP)
1067 case OPT_STATIC_SUPERS:
1068 opt_static_supers = atoi(opt_arg);
1071 case OPT_NO_DYNAMIC:
1072 opt_no_dynamic = true;
1075 case OPT_NO_REPLICATION:
1076 opt_no_replication = true;
1079 case OPT_NO_QUICKSUPER:
1080 opt_no_quicksuper = true;
1089 printf("Unknown option: %s\n", argv[opt_ind]);
1094 if (opt_ind >= argc)
1098 /* transform dots into slashes in the class name */
1100 mainstring = argv[opt_ind++];
1103 /* do not mangle jar filename */
1105 for (i = strlen(mainstring) - 1; i >= 0; i--) {
1106 if (mainstring[i] == '.') mainstring[i] = '/';
1110 /* put jarfile in classpath */
1114 classpath = MNEW(char, strlen(mainstring) + strlen(":") +
1115 strlen(classpath) + strlen("0"));
1117 strcpy(classpath, mainstring);
1118 strcat(classpath, ":");
1119 strcat(classpath, cp);
1121 MFREE(cp, char, strlen(cp));
1124 /**************************** Program start *****************************/
1126 log_init(logfilename);
1129 log_text("CACAO started -------------------------------------------------------");
1131 /* initialize JavaVM */
1133 vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
1135 /* Get the default initialization arguments and set the class path */
1137 JNI_GetDefaultJavaVMInitArgs(&vm_args);
1139 vm_args.minHeapSize = heapstartsize;
1140 vm_args.maxHeapSize = heapmaxsize;
1142 vm_args.classpath = classpath;
1144 /* load and initialize a Java VM, return a JNI interface pointer in env */
1146 JNI_CreateJavaVM(&jvm, &env, &vm_args);
1148 #if defined(ENABLE_JVMTI)
1149 set_jvmti_phase(JVMTI_PHASE_START);
1152 /* initialize the garbage collector */
1154 gc_init(heapmaxsize, heapstartsize);
1156 #if defined(ENABLE_INTRP)
1157 /* allocate main thread stack */
1160 intrp_main_stack = (u1 *) alloca(opt_stacksize);
1161 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1165 cacao_initializing = true;
1167 #if defined(USE_THREADS)
1168 #if defined(NATIVE_THREADS)
1174 /* initialize the string hashtable stuff: lock (must be done
1175 _after_ threads_preinit) */
1178 throw_main_exception_exit();
1180 /* initialize the utf8 hashtable stuff: lock, often used utf8
1181 strings (must be done _after_ threads_preinit) */
1184 throw_main_exception_exit();
1186 /* initialize the classcache hashtable stuff: lock, hashtable
1187 (must be done _after_ threads_preinit) */
1189 if (!classcache_init())
1190 throw_main_exception_exit();
1192 /* initialize the loader with bootclasspath (must be done _after_
1196 throw_main_exception_exit();
1198 suck_add_from_property("java.endorsed.dirs");
1199 suck_add(bootclasspath);
1201 /* initialize the memory subsystem (must be done _after_
1205 throw_main_exception_exit();
1207 /* initialize the finalizer stuff: lock, linked list (must be done
1208 _after_ threads_preinit) */
1210 if (!finalizer_init())
1211 throw_main_exception_exit();
1213 /* install architecture dependent signal handler used for exceptions */
1217 /* initialize the codegen subsystems */
1221 /* initializes jit compiler */
1225 /* machine dependent initialization */
1227 #if defined(ENABLE_JIT)
1228 # if defined(ENABLE_INTRP)
1238 /* initialize the loader subsystems (must be done _after_
1241 if (!loader_init((u1 *) &dummy))
1242 throw_main_exception_exit();
1245 throw_main_exception_exit();
1248 throw_main_exception_exit();
1250 if (!exceptions_init())
1251 throw_main_exception_exit();
1253 if (!builtin_init())
1254 throw_main_exception_exit();
1256 #if defined(USE_THREADS)
1257 if (!threads_init((u1 *) &dummy))
1258 throw_main_exception_exit();
1261 /* That's important, otherwise we get into trouble, if the Runtime
1262 static initializer is called before (circular dependency. This
1263 is with classpath 0.09. Another important thing is, that this
1264 has to happen after initThreads!!! */
1266 if (!initialize_class(class_java_lang_System))
1267 throw_main_exception_exit();
1269 /* JNI init creates a Java object (this means running Java code) */
1272 throw_main_exception_exit();
1274 /* initialize profiling */
1276 if (!profile_init())
1277 throw_main_exception_exit();
1279 #if defined(USE_THREADS)
1280 /* finally, start the finalizer thread */
1282 if (!finalizer_start_thread())
1283 throw_main_exception_exit();
1285 /* start the profile sampling thread */
1287 /* if (!profile_start_thread()) */
1288 /* throw_main_exception_exit(); */
1291 cacao_initializing = false;
1294 /* start worker routines **************************************************/
1297 classinfo *mainclass; /* java/lang/Class */
1299 java_objectarray *a;
1302 /* set return value to OK */
1307 /* open jar file with java.util.jar.JarFile */
1308 mainstring = getmainclassnamefromjar(mainstring);
1311 /* load the main class */
1313 if (!(mainclass = load_class_from_sysloader(utf_new_char(mainstring))))
1314 throw_main_exception_exit();
1316 /* error loading class, clear exceptionptr for new exception */
1318 if (*exceptionptr || !mainclass) {
1319 /* *exceptionptr = NULL; */
1321 /* *exceptionptr = */
1322 /* new_exception_message(string_java_lang_NoClassDefFoundError, */
1324 throw_main_exception_exit();
1327 if (!link_class(mainclass))
1328 throw_main_exception_exit();
1330 /* find the `main' method of the main class */
1332 m = class_resolveclassmethod(mainclass,
1333 utf_new_char("main"),
1334 utf_new_char("([Ljava/lang/String;)V"),
1335 class_java_lang_Object,
1338 if (*exceptionptr) {
1339 throw_main_exception_exit();
1342 /* there is no main method or it isn't static */
1344 if (!m || !(m->flags & ACC_STATIC)) {
1345 *exceptionptr = NULL;
1348 new_exception_message(string_java_lang_NoSuchMethodError,
1350 throw_main_exception_exit();
1353 /* build argument array */
1355 a = builtin_anewarray(argc - opt_ind, class_java_lang_String);
1356 for (i = opt_ind; i < argc; i++) {
1357 a->data[i - opt_ind] =
1358 (java_objectheader *) javastring_new(utf_new_char(argv[i]));
1361 #ifdef TYPEINFO_DEBUG_TEST
1362 /* test the typeinfo system */
1365 /*class_showmethods(currentThread->group->header.vftbl->class); */
1367 #if defined(ENABLE_JVMTI) && defined(NATIVE_THREADS)
1370 if (debuggee == (-1)) {
1371 log_text("fork error");
1374 if (debuggee == 0) {
1375 /* child: allow helper process to trace us */
1376 if (TRACEME != 0) exit(0);
1378 /* give parent/helper debugger process control */
1379 kill(0, SIGTRAP); /* do we need this at this stage ? */
1381 /* continue with normal startup */
1385 /* parent/helper debugger process */
1388 remotedbgjvmtienv = new_jvmtienv();
1389 /* set eventcallbacks */
1390 if (JVMTI_ERROR_NONE ==
1392 SetEventCallbacks(remotedbgjvmtienv,
1393 &jvmti_jdwp_EventCallbacks,
1394 sizeof(jvmti_jdwp_EventCallbacks))){
1395 log_text("unable to setup event callbacks");
1399 /* setup listening process (JDWP) */
1400 setup_debugger_process(transport);
1402 /* start to be debugged program */
1405 /* exit debugger process - todo: cleanup */
1417 ASM_CALLJAVAFUNCTION(m, a, NULL, NULL, NULL);
1419 /* exception occurred? */
1421 if (*exceptionptr) {
1422 throw_main_exception();
1426 #if defined(USE_THREADS)
1427 #if defined(NATIVE_THREADS)
1430 killThread(currentThread);
1434 /* now exit the JavaVM */
1436 /* (*jvm)->DestroyJavaVM(jvm); */
1441 /************* If requested, compile all methods ********************/
1448 classcache_name_entry *nmen;
1449 classcache_class_entry *clsen;
1451 /* create all classes found in the bootclasspath */
1452 /* XXX currently only works with zip/jar's */
1454 loader_load_all_classes();
1456 /* link all classes */
1458 for (slot = 0; slot < hashtable_classcache.size; slot++) {
1459 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
1461 for (; nmen; nmen = nmen->hashlink) {
1462 /* iterate over all class entries */
1464 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
1465 c = clsen->classobj;
1470 if (!(c->state & CLASS_LINKED)) {
1471 if (!link_class(c)) {
1472 fprintf(stderr, "Error linking: ");
1473 utf_fprint_classname(stderr, c->name);
1474 fprintf(stderr, "\n");
1476 /* print out exception and cause */
1478 exceptions_print_exception(*exceptionptr);
1480 /* goto next class */
1486 /* compile all class methods */
1488 for (i = 0; i < c->methodscount; i++) {
1489 m = &(c->methods[i]);
1492 if (!jit_compile(m)) {
1493 fprintf(stderr, "Error compiling: ");
1494 utf_fprint_classname(stderr, c->name);
1495 fprintf(stderr, ".");
1496 utf_fprint(stderr, m->name);
1497 utf_fprint(stderr, m->descriptor);
1498 fprintf(stderr, "\n");
1500 /* print out exception and cause */
1502 exceptions_print_exception(*exceptionptr);
1512 /******** If requested, compile a specific method ***************/
1514 if (specificmethodname) {
1517 /* create, load and link the main class */
1519 if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
1520 throw_main_exception_exit();
1522 if (!link_class(mainclass))
1523 throw_main_exception_exit();
1525 if (specificsignature) {
1526 m = class_resolveclassmethod(mainclass,
1527 utf_new_char(specificmethodname),
1528 utf_new_char(specificsignature),
1532 m = class_resolveclassmethod(mainclass,
1533 utf_new_char(specificmethodname),
1540 char message[MAXLOGTEXT];
1541 sprintf(message, "%s%s", specificmethodname,
1542 specificsignature ? specificsignature : "");
1545 new_exception_message(string_java_lang_NoSuchMethodException,
1548 throw_main_exception_exit();
1556 /* keep compiler happy */
1562 /* cacao_exit ******************************************************************
1564 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1566 *******************************************************************************/
1568 void cacao_exit(s4 status)
1572 assert(class_java_lang_System);
1573 assert(class_java_lang_System->state & CLASS_LOADED);
1575 #if defined(ENABLE_JVMTI)
1576 set_jvmti_phase(JVMTI_PHASE_DEAD);
1580 if (!link_class(class_java_lang_System))
1581 throw_main_exception_exit();
1583 /* signal that we are exiting */
1585 cacao_exiting = true;
1587 /* call java.lang.System.exit(I)V */
1589 m = class_resolveclassmethod(class_java_lang_System,
1590 utf_new_char("exit"),
1592 class_java_lang_Object,
1596 throw_main_exception_exit();
1598 /* call the exit function with passed exit status */
1600 /* both inlinevirtual and outsiders not allowed on exit */
1601 /* not sure if permanant or temp restriction */
1602 if (inlinevirtuals) inlineoutsiders = false;
1604 ASM_CALLJAVAFUNCTION(m, (void *) (ptrint) status, NULL, NULL, NULL);
1606 /* this should never happen */
1609 throw_exception_exit();
1611 throw_cacao_exception_exit(string_java_lang_InternalError,
1612 "System.exit(I)V returned without exception");
1616 /*************************** Shutdown function *********************************
1618 Terminates the system immediately without freeing memory explicitly (to be
1619 used only for abnormal termination)
1621 *******************************************************************************/
1623 void cacao_shutdown(s4 status)
1626 #if defined(ENABLE_JVMTI)
1630 if (opt_verbose || getcompilingtime || opt_stat) {
1631 log_text("CACAO terminated by shutdown");
1632 dolog("Exit status: %d\n", (s4) status);
1639 /* exit_handler ****************************************************************
1641 The exit_handler function is called upon program termination.
1643 ATTENTION: Don't free system resources here! Some threads may still
1644 be running as this is called from VMRuntime.exit(). The OS does the
1647 *******************************************************************************/
1649 void exit_handler(void)
1651 /********************* Print debug tables ************************/
1653 #if !defined(NDEBUG)
1655 class_showmethods(mainclass);
1657 if (showconstantpool)
1658 class_showconstantpool(mainclass);
1664 profile_printstats();
1667 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
1668 clear_thread_flags(); /* restores standard file descriptor
1672 if (opt_verbose || getcompilingtime || opt_stat) {
1673 log_text("CACAO terminated");
1675 #if defined(ENABLE_STATISTICS)
1678 #ifdef TYPECHECK_STATISTICS
1679 typecheck_print_statistics(get_logfile());
1685 if (getcompilingtime)
1689 /* vm_print_profile(stderr);*/
1694 * These are local overrides for various environment variables in Emacs.
1695 * Please do not remove this and leave it at the end of the file, where
1696 * Emacs will automagically detect them.
1697 * ---------------------------------------------------------------------
1700 * indent-tabs-mode: t