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 3586 2005-11-05 22:32:38Z twisti $
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 #if defined(DISABLE_GC)
646 nogc_init(HEAP_MAXSIZE, HEAP_STARTSIZE);
649 /* set the bootclasspath */
651 cp = getenv("BOOTCLASSPATH");
653 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
654 strcpy(bootclasspath, cp);
657 cplen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_VM_ZIP_PATH) +
659 strlen(CLASSPATH_INSTALL_DIR) +
660 strlen(CLASSPATH_GLIBJ_ZIP_PATH) +
663 bootclasspath = MNEW(char, cplen);
664 strcpy(bootclasspath, CACAO_INSTALL_PREFIX);
665 strcat(bootclasspath, CACAO_VM_ZIP_PATH);
666 strcat(bootclasspath, ":");
667 strcat(bootclasspath, CLASSPATH_INSTALL_DIR);
668 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP_PATH);
672 /* set the classpath */
674 cp = getenv("CLASSPATH");
676 classpath = MNEW(char, strlen(cp) + strlen("0"));
677 strcat(classpath, cp);
680 classpath = MNEW(char, strlen(".") + strlen("0"));
681 strcpy(classpath, ".");
685 /***************** Interpret the command line *****************/
690 heapmaxsize = HEAP_MAXSIZE;
691 heapstartsize = HEAP_STARTSIZE;
692 opt_stacksize = STACK_SIZE;
694 while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
699 case OPT_BOOTCLASSPATH:
700 /* Forget default bootclasspath and set the argument as new boot */
702 MFREE(bootclasspath, char, strlen(bootclasspath));
704 bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
705 strcpy(bootclasspath, opt_arg);
708 case OPT_BOOTCLASSPATH_A:
709 /* append to end of bootclasspath */
710 cplen = strlen(bootclasspath);
712 bootclasspath = MREALLOC(bootclasspath,
715 cplen + strlen(":") +
716 strlen(opt_arg) + strlen("0"));
718 strcat(bootclasspath, ":");
719 strcat(bootclasspath, opt_arg);
722 case OPT_BOOTCLASSPATH_P:
723 /* prepend in front of bootclasspath */
727 bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
728 cplen + strlen("0"));
730 strcpy(bootclasspath, opt_arg);
731 strcat(bootclasspath, ":");
732 strcat(bootclasspath, cp);
734 MFREE(cp, char, cplen);
738 /* forget old classpath and set the argument as new classpath */
739 MFREE(classpath, char, strlen(classpath));
741 classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
742 strcpy(classpath, opt_arg);
749 #if defined(ENABLE_JVMTI)
757 set_jvmti_phase(JVMTI_PHASE_ONLOAD);
759 set_jvmti_phase(JVMTI_PHASE_PRIMORDIAL);
765 for (j = 0; j < strlen(opt_arg); j++) {
766 if (opt_arg[j] == '=') {
768 create_property(opt_arg, opt_arg + j + 1);
773 /* if no '=' is given, just create an empty property */
774 create_property(opt_arg, "");
785 c = opt_arg[strlen(opt_arg) - 1];
787 if (c == 'k' || c == 'K') {
788 j = 1024 * atoi(opt_arg);
790 } else if (c == 'm' || c == 'M') {
791 j = 1024 * 1024 * atoi(opt_arg);
798 else if (i == OPT_MS)
814 compileverbose = true;
817 case OPT_VERBOSESPECIFIC:
818 if (strcmp("class", opt_arg) == 0)
819 opt_verboseclass = true;
821 else if (strcmp("gc", opt_arg) == 0)
822 opt_verbosegc = true;
824 else if (strcmp("jni", opt_arg) == 0)
825 opt_verbosejni = true;
828 case OPT_VERBOSEEXCEPTION:
829 opt_verboseexception = true;
832 #ifdef TYPECHECK_VERBOSE
834 typecheckverbose = true;
838 case OPT_VERBOSECALL:
847 case OPT_FULLVERSION:
851 case OPT_SHOWVERSION:
864 opt_liberalutf = true;
872 getcompilingtime = true;
873 getloadingtime = true;
876 #if defined(STATISTICS)
883 strcpy(logfilename, opt_arg);
887 for (j = 0; j < strlen(opt_arg); j++) {
888 switch (opt_arg[j]) {
903 makeinitializations = false;
912 specificmethodname = opt_arg;
913 makeinitializations = false;
917 specificsignature = opt_arg;
923 makeinitializations = false;
926 case OPT_SHOW: /* Display options */
927 for (j = 0; j < strlen(opt_arg); j++) {
928 switch (opt_arg[j]) {
930 opt_showdisassemble = true;
931 compileverbose = true;
934 showconstantpool = true;
937 opt_showddatasegment = true;
940 opt_showexceptionstubs = true;
943 opt_showintermediate = true;
944 compileverbose = true;
950 opt_shownativestub = true;
966 for (j = 0; j < strlen(opt_arg); j++) {
967 switch (opt_arg[j]) {
969 /* define in options.h; Used in main.c, jit.c & inline.c */
978 inlinevirtuals = true;
982 inlineexceptions = true;
985 inlineparamopt = true;
988 inlineoutsiders = true;
996 #ifdef STATIC_ANALYSIS
998 opt_rt = true; /* default for inlining */
1002 opt_xta = true; /* in test currently */
1006 printf("\nVTA is not yet available\n");
1008 /***opt_vta = true; not yet **/
1027 #if defined(ENABLE_JIT)
1030 printf("-Xjit option not enabled.\n");
1036 #if defined(ENABLE_INTRP)
1039 printf("-Xint option not enabled.\n");
1044 case OPT_STATIC_SUPERS:
1045 opt_static_supers = atoi(opt_arg);
1053 printf("Unknown option: %s\n", argv[opt_ind]);
1058 if (opt_ind >= argc)
1062 /* transform dots into slashes in the class name */
1064 mainstring = argv[opt_ind++];
1067 /* do not mangle jar filename */
1069 for (i = strlen(mainstring) - 1; i >= 0; i--) {
1070 if (mainstring[i] == '.') mainstring[i] = '/';
1074 /* put jarfile in classpath */
1078 classpath = MNEW(char, strlen(mainstring) + strlen(":") +
1079 strlen(classpath) + strlen("0"));
1081 strcpy(classpath, mainstring);
1082 strcat(classpath, ":");
1083 strcat(classpath, cp);
1085 MFREE(cp, char, strlen(cp));
1088 /**************************** Program start *****************************/
1090 log_init(logfilename);
1093 log_text("CACAO started -------------------------------------------------------");
1095 /* initialize JavaVM */
1097 vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
1099 /* Get the default initialization arguments and set the class path */
1101 JNI_GetDefaultJavaVMInitArgs(&vm_args);
1103 vm_args.minHeapSize = heapstartsize;
1104 vm_args.maxHeapSize = heapmaxsize;
1106 vm_args.classpath = classpath;
1108 /* load and initialize a Java VM, return a JNI interface pointer in env */
1110 JNI_CreateJavaVM(&jvm, &env, &vm_args);
1112 #if defined(ENABLE_JVMTI)
1113 set_jvmti_phase(JVMTI_PHASE_START);
1116 /* initialize the garbage collector */
1118 gc_init(heapmaxsize, heapstartsize);
1120 #if defined(ENABLE_INTRP)
1121 /* allocate main thread stack */
1124 intrp_main_stack = (u1 *) alloca(opt_stacksize);
1125 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1131 /* initialize the loader with bootclasspath */
1133 suck_init(bootclasspath);
1135 cacao_initializing = true;
1137 #if defined(USE_THREADS)
1138 #if defined(NATIVE_THREADS)
1144 /* initialize the memory subsystem (must be done _after_
1148 throw_main_exception_exit();
1150 /* initialize the finalizer stuff: lock, linked list (must be done
1151 _after_ threads_preinit) */
1153 if (!finalizer_init())
1154 throw_main_exception_exit();
1156 /* install architecture dependent signal handler used for exceptions */
1160 /* initialize the codegen subsystems */
1164 /* initializes jit compiler */
1168 /* machine dependent initialization */
1172 /* initialize some cacao subsystems */
1176 if (!loader_init((u1 *) &dummy))
1177 throw_main_exception_exit();
1180 throw_main_exception_exit();
1183 throw_main_exception_exit();
1185 if (!exceptions_init())
1186 throw_main_exception_exit();
1188 if (!builtin_init())
1189 throw_main_exception_exit();
1191 #if defined(USE_THREADS)
1192 if (!threads_init((u1 *) &dummy))
1193 throw_main_exception_exit();
1196 /* That's important, otherwise we get into trouble, if the Runtime
1197 static initializer is called before (circular dependency. This
1198 is with classpath 0.09. Another important thing is, that this
1199 has to happen after initThreads!!! */
1201 if (!initialize_class(class_java_lang_System))
1202 throw_main_exception_exit();
1204 /* JNI init creates a Java object (this means running Java code) */
1207 throw_main_exception_exit();
1209 #if defined(USE_THREADS)
1210 /* finally, start the finalizer thread */
1212 if (!finalizer_start_thread())
1213 throw_main_exception_exit();
1216 cacao_initializing = false;
1219 /* start worker routines **************************************************/
1222 classinfo *mainclass; /* java/lang/Class */
1224 java_objectarray *a;
1227 /* set return value to OK */
1232 /* open jar file with java.util.jar.JarFile */
1233 mainstring = getmainclassnamefromjar(mainstring);
1236 /* load the main class */
1238 if (!(mainclass = load_class_from_sysloader(utf_new_char(mainstring))))
1239 throw_main_exception_exit();
1241 /* error loading class, clear exceptionptr for new exception */
1243 if (*exceptionptr || !mainclass) {
1244 /* *exceptionptr = NULL; */
1246 /* *exceptionptr = */
1247 /* new_exception_message(string_java_lang_NoClassDefFoundError, */
1249 throw_main_exception_exit();
1252 /* find the `main' method of the main class */
1254 m = class_resolveclassmethod(mainclass,
1255 utf_new_char("main"),
1256 utf_new_char("([Ljava/lang/String;)V"),
1257 class_java_lang_Object,
1260 if (*exceptionptr) {
1261 throw_main_exception_exit();
1264 /* there is no main method or it isn't static */
1266 if (!m || !(m->flags & ACC_STATIC)) {
1267 *exceptionptr = NULL;
1270 new_exception_message(string_java_lang_NoSuchMethodError,
1272 throw_main_exception_exit();
1275 /* build argument array */
1277 a = builtin_anewarray(argc - opt_ind, class_java_lang_String);
1278 for (i = opt_ind; i < argc; i++) {
1279 a->data[i - opt_ind] =
1280 (java_objectheader *) javastring_new(utf_new_char(argv[i]));
1283 #ifdef TYPEINFO_DEBUG_TEST
1284 /* test the typeinfo system */
1287 /*class_showmethods(currentThread->group->header.vftbl->class); */
1289 #if defined(ENABLE_JVMTI) && defined(NATIVE_THREADS)
1292 if (debuggee == (-1)) {
1293 log_text("fork error");
1296 if (debuggee == 0) {
1297 /* child: allow helper process to trace us */
1298 if (TRACEME != 0) exit(0);
1300 /* give parent/helper debugger process control */
1301 kill(0, SIGTRAP); /* do we need this at this stage ? */
1303 /* continue with normal startup */
1307 /* parent/helper debugger process */
1310 remotedbgjvmtienv = new_jvmtienv();
1311 /* set eventcallbacks */
1312 if (JVMTI_ERROR_NONE ==
1314 SetEventCallbacks(remotedbgjvmtienv,
1315 &jvmti_jdwp_EventCallbacks,
1316 sizeof(jvmti_jdwp_EventCallbacks))){
1317 log_text("unable to setup event callbacks");
1321 /* setup listening process (JDWP) */
1322 setup_debugger_process(transport);
1324 /* start to be debugged program */
1327 /* exit debugger process - todo: cleanup */
1339 asm_calljavafunction(m, a, NULL, NULL, NULL);
1341 /* exception occurred? */
1342 if (*exceptionptr) {
1343 throw_main_exception();
1347 #if defined(USE_THREADS)
1348 #if defined(NATIVE_THREADS)
1351 killThread(currentThread);
1355 /* now exit the JavaVM */
1357 /* (*jvm)->DestroyJavaVM(jvm); */
1362 /************* If requested, compile all methods ********************/
1369 classcache_name_entry *nmen;
1370 classcache_class_entry *clsen;
1372 /* create all classes found in the classpath */
1373 /* XXX currently only works with zip/jar's */
1375 loader_load_all_classes();
1377 /* link all classes */
1379 for (slot = 0; slot < classcache_hash.size; slot++) {
1380 nmen = (classcache_name_entry *) classcache_hash.ptr[slot];
1382 for (; nmen; nmen = nmen->hashlink) {
1383 /* iterate over all class entries */
1385 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
1386 c = clsen->classobj;
1393 /*utf_fprint_classname(stderr,c->name);fprintf(stderr,"\n");*/
1397 throw_main_exception_exit();
1399 /* compile all class methods */
1400 for (i = 0; i < c->methodscount; i++) {
1401 m = &(c->methods[i]);
1403 /*fprintf(stderr," compiling:");utf_fprint(stderr,m->name);fprintf(stderr,"\n");*/
1404 (void) jit_compile(m);
1413 /******** If requested, compile a specific method ***************/
1415 if (specificmethodname) {
1418 /* create, load and link the main class */
1420 if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
1421 throw_main_exception_exit();
1423 if (!link_class(mainclass))
1424 throw_main_exception_exit();
1426 if (specificsignature) {
1427 m = class_resolveclassmethod(mainclass,
1428 utf_new_char(specificmethodname),
1429 utf_new_char(specificsignature),
1433 m = class_resolveclassmethod(mainclass,
1434 utf_new_char(specificmethodname),
1441 char message[MAXLOGTEXT];
1442 sprintf(message, "%s%s", specificmethodname,
1443 specificsignature ? specificsignature : "");
1446 new_exception_message(string_java_lang_NoSuchMethodException,
1449 throw_main_exception_exit();
1457 /* keep compiler happy */
1463 /* cacao_exit ******************************************************************
1465 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1467 *******************************************************************************/
1469 void cacao_exit(s4 status)
1473 assert(class_java_lang_System);
1474 assert(class_java_lang_System->loaded);
1476 #if defined(ENABLE_JVMTI)
1477 set_jvmti_phase(JVMTI_PHASE_DEAD);
1481 if (!link_class(class_java_lang_System))
1482 throw_main_exception_exit();
1484 /* signal that we are exiting */
1486 cacao_exiting = true;
1488 /* call java.lang.System.exit(I)V */
1490 m = class_resolveclassmethod(class_java_lang_System,
1491 utf_new_char("exit"),
1493 class_java_lang_Object,
1497 throw_main_exception_exit();
1499 /* call the exit function with passed exit status */
1501 /* both inlinevirtual and outsiders not allowed on exit */
1502 /* not sure if permanant or temp restriction */
1503 if (inlinevirtuals) inlineoutsiders = false;
1505 asm_calljavafunction(m, (void *) (ptrint) status, NULL, NULL, NULL);
1507 /* this should never happen */
1510 throw_exception_exit();
1512 throw_cacao_exception_exit(string_java_lang_InternalError,
1513 "System.exit(I)V returned without exception");
1517 /*************************** Shutdown function *********************************
1519 Terminates the system immediately without freeing memory explicitly (to be
1520 used only for abnormal termination)
1522 *******************************************************************************/
1524 void cacao_shutdown(s4 status)
1527 #if defined(ENABLE_JVMTI)
1531 if (opt_verbose || getcompilingtime || opt_stat) {
1532 log_text("CACAO terminated by shutdown");
1533 dolog("Exit status: %d\n", (s4) status);
1540 /* exit_handler ****************************************************************
1542 The exit_handler function is called upon program termination.
1544 ATTENTION: Don't free system resources here! Some threads may still
1545 be running as this is called from VMRuntime.exit(). The OS does the
1548 *******************************************************************************/
1550 void exit_handler(void)
1552 /********************* Print debug tables ************************/
1554 if (showmethods) class_showmethods(mainclass);
1555 if (showconstantpool) class_showconstantpool(mainclass);
1556 if (showutf) utf_show();
1558 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
1559 clear_thread_flags(); /* restores standard file descriptor
1563 if (opt_verbose || getcompilingtime || opt_stat) {
1564 log_text("CACAO terminated");
1566 #if defined(STATISTICS)
1569 #ifdef TYPECHECK_STATISTICS
1570 typecheck_print_statistics(get_logfile());
1576 if (getcompilingtime)
1580 /* vm_print_profile(stderr);*/
1585 * These are local overrides for various environment variables in Emacs.
1586 * Please do not remove this and leave it at the end of the file, where
1587 * Emacs will automagically detect them.
1588 * ---------------------------------------------------------------------
1591 * indent-tabs-mode: t