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 3937 2005-12-10 23:59:04Z 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/classcache.h"
66 #include "vm/exceptions.h"
67 #include "vm/finalizer.h"
68 #include "vm/global.h"
69 #include "vm/initialize.h"
70 #include "vm/loader.h"
71 #include "vm/options.h"
72 #include "vm/signallocal.h"
73 #include "vm/statistics.h"
74 #include "vm/stringlocal.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 #define OPT_NO_DYNAMIC 110
192 opt_struct opts[] = {
193 { "classpath", true, OPT_CLASSPATH },
194 { "cp", true, OPT_CLASSPATH },
195 { "D", true, OPT_D },
196 { "noasyncgc", false, OPT_IGNORE },
197 { "noverify", false, OPT_NOVERIFY },
198 { "liberalutf", false, OPT_LIBERALUTF },
199 { "v", false, OPT_VERBOSE1 },
200 { "verbose", false, OPT_VERBOSE },
201 { "verbose:", true, OPT_VERBOSESPECIFIC },
202 { "verbosecall", false, OPT_VERBOSECALL },
203 { "verboseexception", false, OPT_VERBOSEEXCEPTION },
204 #ifdef TYPECHECK_VERBOSE
205 { "verbosetc", false, OPT_VERBOSETC },
207 #if defined(__ALPHA__)
208 { "noieee", false, OPT_NOIEEE },
210 { "softnull", false, OPT_SOFTNULL },
211 { "time", false, OPT_TIME },
212 #if defined(STATISTICS)
213 { "stat", false, OPT_STAT },
215 { "log", true, OPT_LOG },
216 { "c", true, OPT_CHECK },
217 { "l", false, OPT_LOAD },
218 { "eager", false, OPT_EAGER },
219 { "sig", true, OPT_SIGNATURE },
220 { "all", false, OPT_ALL },
221 { "oloop", false, OPT_OLOOP },
222 #ifdef STATIC_ANALYSIS
223 { "rt", false, OPT_RT },
224 { "xta", false, OPT_XTA },
225 { "vta", false, OPT_VTA },
228 { "lsra", false, OPT_LSRA },
230 { "jar", false, OPT_JAR },
231 { "version", false, OPT_VERSION },
232 { "showversion", false, OPT_SHOWVERSION },
233 { "fullversion", false, OPT_FULLVERSION },
234 { "help", false, OPT_HELP },
235 { "?", false, OPT_HELP },
237 /* interpreter options */
239 { "trace", false, OPT_TRACE },
240 { "static-supers", true, OPT_STATIC_SUPERS },
241 { "no-dynamic", false, OPT_NO_DYNAMIC },
243 /* JVMTI Agent Command Line Options */
245 { "agentlib:", true, OPT_AGENTLIB },
246 { "agentpath:", true, OPT_AGENTPATH },
251 { "X", false, OPT_X },
252 { "Xjit", false, OPT_JIT },
253 { "Xint", false, OPT_INTRP },
254 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
255 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
256 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
258 { "Xdebug", false, OPT_DEBUG },
260 { "Xms", true, OPT_MS },
261 { "Xmx", true, OPT_MX },
262 { "Xss", true, OPT_SS },
263 { "ms", true, OPT_MS },
264 { "mx", true, OPT_MX },
265 { "ss", true, OPT_SS },
267 /* keep these at the end of the list */
269 { "i", true, OPT_INLINING },
270 { "m", true, OPT_METHOD },
271 { "s", true, OPT_SHOW },
277 /* usage ***********************************************************************
279 Prints the correct usage syntax to stdout.
281 *******************************************************************************/
283 static void usage(void)
285 printf("Usage: cacao [-options] classname [arguments]\n");
286 printf(" (to run a class file)\n");
287 printf(" cacao [-options] -jar jarfile [arguments]\n");
288 printf(" (to run a standalone jar file)\n\n");
290 printf("Java options:\n");
291 printf(" -cp <path> specify a path to look for classes\n");
292 printf(" -classpath <path> specify a path to look for classes\n");
293 printf(" -D<name>=<value> add an entry to the property list\n");
294 printf(" -verbose[:class|gc|jni] enable specific verbose output\n");
295 printf(" -version print product version and exit\n");
296 printf(" -fullversion print jpackage-compatible product version and exit\n");
297 printf(" -showversion print product version and continue\n");
298 printf(" -help, -? print this help message\n");
299 printf(" -X print help on non-standard Java options\n\n");
302 printf(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent\n");
303 printf(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent\n");
306 printf("CACAO options:\n");
307 printf(" -v write state-information\n");
308 printf(" -verbose write more information\n");
309 printf(" -verbosegc write message for each GC\n");
310 printf(" -verbosecall write message for each call\n");
311 printf(" -verboseexception write message for each step of stack unwinding\n");
312 #ifdef TYPECHECK_VERBOSE
313 printf(" -verbosetc write debug messages while typechecking\n");
315 #if defined(__ALPHA__)
316 printf(" -noieee don't use ieee compliant arithmetic\n");
318 printf(" -noverify don't verify classfiles\n");
319 printf(" -liberalutf don't warn about overlong UTF-8 sequences\n");
320 printf(" -softnull use software nullpointer check\n");
321 printf(" -time measure the runtime\n");
322 #if defined(STATISTICS)
323 printf(" -stat detailed compiler statistics\n");
325 printf(" -log logfile specify a name for the logfile\n");
326 printf(" -c(heck)b(ounds) don't check array bounds\n");
327 printf(" s(ync) don't check for synchronization\n");
328 printf(" -oloop optimize array accesses in loops\n");
329 printf(" -l don't start the class after loading\n");
330 printf(" -eager perform eager class loading and linking\n");
331 printf(" -all compile all methods, no execution\n");
332 printf(" -m compile only a specific method\n");
333 printf(" -sig specify signature for a specific method\n");
334 printf(" -s(how)a(ssembler) show disassembled listing\n");
335 printf(" c(onstants) show the constant pool\n");
336 printf(" d(atasegment) show data segment listing\n");
337 printf(" e(xceptionstubs) show disassembled exception stubs (only with -sa)\n");
338 printf(" i(ntermediate) show intermediate representation\n");
339 printf(" m(ethods) show class fields and methods\n");
340 printf(" n(ative) show disassembled native stubs\n");
341 printf(" u(tf) show the utf - hash\n");
342 printf(" -i n(line) activate inlining\n");
343 printf(" v(irtual) inline virtual methods (uses/turns rt option on)\n");
344 printf(" e(exception) inline methods with exceptions\n");
345 printf(" p(aramopt) optimize argument renaming\n");
346 printf(" o(utsiders) inline methods of foreign classes\n");
347 #ifdef STATIC_ANALYSIS
348 printf(" -rt use rapid type analysis\n");
349 printf(" -xta use x type analysis\n");
350 printf(" -vta use variable type analysis\n");
353 printf(" -lsra use linear scan register allocation\n");
356 /* exit with error code */
362 static void Xusage(void)
364 #if defined(ENABLE_JIT)
365 printf(" -Xjit JIT mode execution (default)\n");
367 #if defined(ENABLE_INTRP)
368 printf(" -Xint interpreter mode execution\n");
370 printf(" -Xbootclasspath:<zip/jar files and directories separated by :>\n");
371 printf(" value is set as bootstrap class path\n");
372 printf(" -Xbootclasspath/a:<zip/jar files and directories separated by :>\n");
373 printf(" value is appended to the bootstrap class path\n");
374 printf(" -Xbootclasspath/p:<zip/jar files and directories separated by :>\n");
375 printf(" value is prepended to the bootstrap class path\n");
376 printf(" -Xms<size> set the initial size of the heap (default: 2MB)\n");
377 printf(" -Xmx<size> set the maximum size of the heap (default: 64MB)\n");
378 printf(" -Xss<size> set the thread stack size (default: 128kB)\n");
379 #if defined(ENABLE_JVMTI)
380 printf(" -Xdebug<transport> enable remote debugging\n");
383 /* exit with error code */
389 /* version *********************************************************************
391 Only prints cacao version information.
393 *******************************************************************************/
395 static void version(void)
397 printf("java version \""JAVA_VERSION"\"\n");
398 printf("CACAO version "VERSION"\n");
400 printf("Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,\n");
401 printf("R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,\n");
402 printf("C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,\n");
403 printf("Institut f. Computersprachen - TU Wien\n\n");
405 printf("This program is free software; you can redistribute it and/or\n");
406 printf("modify it under the terms of the GNU General Public License as\n");
407 printf("published by the Free Software Foundation; either version 2, or (at\n");
408 printf("your option) any later version.\n\n");
410 printf("This program is distributed in the hope that it will be useful, but\n");
411 printf("WITHOUT ANY WARRANTY; without even the implied warranty of\n");
412 printf("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n");
413 printf("General Public License for more details.\n");
417 /* fullversion *****************************************************************
419 Prints a Sun compatible version information (required e.g. by
420 jpackage, www.jpackage.org).
422 *******************************************************************************/
424 static void fullversion(void)
426 printf("java full version \"cacao-"JAVA_VERSION"\"\n");
434 #ifdef TYPECHECK_STATISTICS
435 void typecheck_print_statistics(FILE *file);
438 /* setup_debugger_process *****************************************************
440 Helper function to start JDWP threads
442 *******************************************************************************/
443 #if defined(ENABLE_JVMTI)
445 static void setup_debugger_process(char* transport) {
446 java_objectheader *o;
450 /* new gnu.classpath.jdwp.Jdwp() */
452 load_class_from_sysloader(utf_new_char("gnu.classpath.jdwp.Jdwp"));
454 throw_main_exception_exit();
456 o = builtin_new(mainclass);
459 throw_main_exception_exit();
461 m = class_resolveclassmethod(mainclass,
463 utf_java_lang_String__void,
464 class_java_lang_Object,
467 throw_main_exception_exit();
469 asm_calljavafunction(m, o, NULL, NULL, NULL);
471 /* configure(transport,NULL) */
472 m = class_resolveclassmethod(
473 mainclass, utf_new_char("configure"),
474 utf_new_char("(Ljava/lang/String;Ljava/lang/Thread;)V"),
475 class_java_lang_Object,
479 s = javastring_new_char(transport);
480 asm_calljavafunction(m, o, s, NULL, NULL);
482 throw_main_exception_exit();
484 /* _doInitialization */
485 m = class_resolveclassmethod(mainclass,
486 utf_new_char("_doInitialization"),
492 throw_main_exception_exit();
494 asm_calljavafunction(m, o, NULL, NULL, NULL);
499 /* getmainclassfromjar *********************************************************
501 Gets the name of the main class form a JAR's manifest file.
503 *******************************************************************************/
505 static char *getmainclassnamefromjar(char *mainstring)
508 java_objectheader *o;
512 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
515 throw_main_exception_exit();
517 /* create JarFile object */
522 throw_main_exception_exit();
525 m = class_resolveclassmethod(c,
527 utf_java_lang_String__void,
528 class_java_lang_Object,
532 throw_main_exception_exit();
534 s = javastring_new_char(mainstring);
536 asm_calljavafunction(m, o, s, NULL, NULL);
539 throw_main_exception_exit();
541 /* get manifest object */
543 m = class_resolveclassmethod(c,
544 utf_new_char("getManifest"),
545 utf_new_char("()Ljava/util/jar/Manifest;"),
546 class_java_lang_Object,
550 throw_main_exception_exit();
552 o = asm_calljavafunction(m, o, NULL, NULL, NULL);
555 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
560 /* get Main Attributes */
562 m = class_resolveclassmethod(o->vftbl->class,
563 utf_new_char("getMainAttributes"),
564 utf_new_char("()Ljava/util/jar/Attributes;"),
565 class_java_lang_Object,
569 throw_main_exception_exit();
571 o = asm_calljavafunction(m, o, NULL, NULL, NULL);
574 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
579 /* get property Main-Class */
581 m = class_resolveclassmethod(o->vftbl->class,
582 utf_new_char("getValue"),
583 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
584 class_java_lang_Object,
588 throw_main_exception_exit();
590 s = javastring_new_char("Main-Class");
592 o = asm_calljavafunction(m, o, s, NULL, NULL);
595 throw_main_exception_exit();
597 return javastring_tochar(o);
601 void exit_handler(void);
604 /* main ************************************************************************
608 *******************************************************************************/
610 int main(int argc, char **argv)
615 /* local variables ********************************************************/
617 char logfilename[200] = "";
623 char *specificmethodname = NULL;
624 char *specificsignature = NULL;
626 #if defined(ENABLE_JVMTI)
633 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
634 stackbottom = &dummy;
637 if (atexit(exit_handler))
638 throw_cacao_exception_exit(string_java_lang_InternalError,
639 "Unable to register exit_handler");
641 /* initialize global variables */
643 cacao_exiting = false;
646 /************ Collect info from the environment ************************/
648 #if defined(DISABLE_GC)
649 nogc_init(HEAP_MAXSIZE, HEAP_STARTSIZE);
652 /* set the bootclasspath */
654 cp = getenv("BOOTCLASSPATH");
656 bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
657 strcpy(bootclasspath, cp);
660 cplen = strlen(CACAO_INSTALL_PREFIX) + strlen(CACAO_VM_ZIP_PATH) +
662 strlen(CLASSPATH_INSTALL_DIR) +
663 strlen(CLASSPATH_GLIBJ_ZIP_PATH) +
666 bootclasspath = MNEW(char, cplen);
667 strcpy(bootclasspath, CACAO_INSTALL_PREFIX);
668 strcat(bootclasspath, CACAO_VM_ZIP_PATH);
669 strcat(bootclasspath, ":");
670 strcat(bootclasspath, CLASSPATH_INSTALL_DIR);
671 strcat(bootclasspath, CLASSPATH_GLIBJ_ZIP_PATH);
675 /* set the classpath */
677 cp = getenv("CLASSPATH");
679 classpath = MNEW(char, strlen(cp) + strlen("0"));
680 strcat(classpath, cp);
683 classpath = MNEW(char, strlen(".") + strlen("0"));
684 strcpy(classpath, ".");
688 /***************** Interpret the command line *****************/
693 heapmaxsize = HEAP_MAXSIZE;
694 heapstartsize = HEAP_STARTSIZE;
695 opt_stacksize = STACK_SIZE;
697 while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
702 case OPT_BOOTCLASSPATH:
703 /* Forget default bootclasspath and set the argument as new boot */
705 MFREE(bootclasspath, char, strlen(bootclasspath));
707 bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
708 strcpy(bootclasspath, opt_arg);
711 case OPT_BOOTCLASSPATH_A:
712 /* append to end of bootclasspath */
713 cplen = strlen(bootclasspath);
715 bootclasspath = MREALLOC(bootclasspath,
718 cplen + strlen(":") +
719 strlen(opt_arg) + strlen("0"));
721 strcat(bootclasspath, ":");
722 strcat(bootclasspath, opt_arg);
725 case OPT_BOOTCLASSPATH_P:
726 /* prepend in front of bootclasspath */
730 bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
731 cplen + strlen("0"));
733 strcpy(bootclasspath, opt_arg);
734 strcat(bootclasspath, ":");
735 strcat(bootclasspath, cp);
737 MFREE(cp, char, cplen);
741 /* forget old classpath and set the argument as new classpath */
742 MFREE(classpath, char, strlen(classpath));
744 classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
745 strcpy(classpath, opt_arg);
752 #if defined(ENABLE_JVMTI)
760 set_jvmti_phase(JVMTI_PHASE_ONLOAD);
762 set_jvmti_phase(JVMTI_PHASE_PRIMORDIAL);
768 for (j = 0; j < strlen(opt_arg); j++) {
769 if (opt_arg[j] == '=') {
771 create_property(opt_arg, opt_arg + j + 1);
776 /* if no '=' is given, just create an empty property */
777 create_property(opt_arg, "");
788 c = opt_arg[strlen(opt_arg) - 1];
790 if (c == 'k' || c == 'K') {
791 j = 1024 * atoi(opt_arg);
793 } else if (c == 'm' || c == 'M') {
794 j = 1024 * 1024 * atoi(opt_arg);
801 else if (i == OPT_MS)
817 compileverbose = true;
820 case OPT_VERBOSESPECIFIC:
821 if (strcmp("class", opt_arg) == 0)
822 opt_verboseclass = true;
824 else if (strcmp("gc", opt_arg) == 0)
825 opt_verbosegc = true;
827 else if (strcmp("jni", opt_arg) == 0)
828 opt_verbosejni = true;
831 case OPT_VERBOSEEXCEPTION:
832 opt_verboseexception = true;
835 #ifdef TYPECHECK_VERBOSE
837 typecheckverbose = true;
841 case OPT_VERBOSECALL:
850 case OPT_FULLVERSION:
854 case OPT_SHOWVERSION:
867 opt_liberalutf = true;
875 getcompilingtime = true;
876 getloadingtime = true;
879 #if defined(STATISTICS)
886 strcpy(logfilename, opt_arg);
890 for (j = 0; j < strlen(opt_arg); j++) {
891 switch (opt_arg[j]) {
906 makeinitializations = false;
915 specificmethodname = opt_arg;
916 makeinitializations = false;
920 specificsignature = opt_arg;
926 makeinitializations = false;
929 case OPT_SHOW: /* Display options */
930 for (j = 0; j < strlen(opt_arg); j++) {
931 switch (opt_arg[j]) {
933 opt_showdisassemble = true;
934 compileverbose = true;
937 showconstantpool = true;
940 opt_showddatasegment = true;
943 opt_showexceptionstubs = true;
946 opt_showintermediate = true;
947 compileverbose = true;
953 opt_shownativestub = true;
969 for (j = 0; j < strlen(opt_arg); j++) {
970 switch (opt_arg[j]) {
972 /* define in options.h; Used in main.c, jit.c & inline.c */
981 inlinevirtuals = true;
985 inlineexceptions = true;
988 inlineparamopt = true;
991 inlineoutsiders = true;
999 #ifdef STATIC_ANALYSIS
1001 opt_rt = true; /* default for inlining */
1005 opt_xta = true; /* in test currently */
1009 printf("\nVTA is not yet available\n");
1011 /***opt_vta = true; not yet **/
1030 #if defined(ENABLE_JIT)
1033 printf("-Xjit option not enabled.\n");
1039 #if defined(ENABLE_INTRP)
1042 printf("-Xint option not enabled.\n");
1047 case OPT_STATIC_SUPERS:
1048 opt_static_supers = atoi(opt_arg);
1051 case OPT_NO_DYNAMIC:
1052 opt_no_dynamic = true;
1060 printf("Unknown option: %s\n", argv[opt_ind]);
1065 if (opt_ind >= argc)
1069 /* transform dots into slashes in the class name */
1071 mainstring = argv[opt_ind++];
1074 /* do not mangle jar filename */
1076 for (i = strlen(mainstring) - 1; i >= 0; i--) {
1077 if (mainstring[i] == '.') mainstring[i] = '/';
1081 /* put jarfile in classpath */
1085 classpath = MNEW(char, strlen(mainstring) + strlen(":") +
1086 strlen(classpath) + strlen("0"));
1088 strcpy(classpath, mainstring);
1089 strcat(classpath, ":");
1090 strcat(classpath, cp);
1092 MFREE(cp, char, strlen(cp));
1095 /**************************** Program start *****************************/
1097 log_init(logfilename);
1100 log_text("CACAO started -------------------------------------------------------");
1102 /* initialize JavaVM */
1104 vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
1106 /* Get the default initialization arguments and set the class path */
1108 JNI_GetDefaultJavaVMInitArgs(&vm_args);
1110 vm_args.minHeapSize = heapstartsize;
1111 vm_args.maxHeapSize = heapmaxsize;
1113 vm_args.classpath = classpath;
1115 /* load and initialize a Java VM, return a JNI interface pointer in env */
1117 JNI_CreateJavaVM(&jvm, &env, &vm_args);
1119 #if defined(ENABLE_JVMTI)
1120 set_jvmti_phase(JVMTI_PHASE_START);
1123 /* initialize the garbage collector */
1125 gc_init(heapmaxsize, heapstartsize);
1127 #if defined(ENABLE_INTRP)
1128 /* allocate main thread stack */
1131 intrp_main_stack = (u1 *) alloca(opt_stacksize);
1132 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1136 cacao_initializing = true;
1138 #if defined(USE_THREADS)
1139 #if defined(NATIVE_THREADS)
1145 /* initialize the string hashtable stuff: lock (must be done
1146 _after_ threads_preinit) */
1149 throw_main_exception_exit();
1151 /* initialize the utf8 hashtable stuff: lock, often used utf8
1152 strings (must be done _after_ threads_preinit) */
1155 throw_main_exception_exit();
1157 /* initialize the classcache hashtable stuff: lock, hashtable
1158 (must be done _after_ threads_preinit) */
1160 if (!classcache_init())
1161 throw_main_exception_exit();
1163 /* initialize the loader with bootclasspath (must be done _after_
1167 throw_main_exception_exit();
1169 suck_add(bootclasspath);
1171 /* initialize the memory subsystem (must be done _after_
1175 throw_main_exception_exit();
1177 /* initialize the finalizer stuff: lock, linked list (must be done
1178 _after_ threads_preinit) */
1180 if (!finalizer_init())
1181 throw_main_exception_exit();
1183 /* install architecture dependent signal handler used for exceptions */
1187 /* initialize the codegen subsystems */
1191 /* initializes jit compiler */
1195 /* machine dependent initialization */
1199 /* initialize the loader subsystems (must be done _after_
1202 if (!loader_init((u1 *) &dummy))
1203 throw_main_exception_exit();
1206 throw_main_exception_exit();
1209 throw_main_exception_exit();
1211 if (!exceptions_init())
1212 throw_main_exception_exit();
1214 if (!builtin_init())
1215 throw_main_exception_exit();
1217 #if defined(USE_THREADS)
1218 if (!threads_init((u1 *) &dummy))
1219 throw_main_exception_exit();
1222 /* That's important, otherwise we get into trouble, if the Runtime
1223 static initializer is called before (circular dependency. This
1224 is with classpath 0.09. Another important thing is, that this
1225 has to happen after initThreads!!! */
1227 if (!initialize_class(class_java_lang_System))
1228 throw_main_exception_exit();
1230 /* JNI init creates a Java object (this means running Java code) */
1233 throw_main_exception_exit();
1235 #if defined(USE_THREADS)
1236 /* finally, start the finalizer thread */
1238 if (!finalizer_start_thread())
1239 throw_main_exception_exit();
1242 cacao_initializing = false;
1245 /* start worker routines **************************************************/
1248 classinfo *mainclass; /* java/lang/Class */
1250 java_objectarray *a;
1253 /* set return value to OK */
1258 /* open jar file with java.util.jar.JarFile */
1259 mainstring = getmainclassnamefromjar(mainstring);
1262 /* load the main class */
1264 if (!(mainclass = load_class_from_sysloader(utf_new_char(mainstring))))
1265 throw_main_exception_exit();
1267 /* error loading class, clear exceptionptr for new exception */
1269 if (*exceptionptr || !mainclass) {
1270 /* *exceptionptr = NULL; */
1272 /* *exceptionptr = */
1273 /* new_exception_message(string_java_lang_NoClassDefFoundError, */
1275 throw_main_exception_exit();
1278 if (!link_class(mainclass))
1279 throw_main_exception_exit();
1281 /* find the `main' method of the main class */
1283 m = class_resolveclassmethod(mainclass,
1284 utf_new_char("main"),
1285 utf_new_char("([Ljava/lang/String;)V"),
1286 class_java_lang_Object,
1289 if (*exceptionptr) {
1290 throw_main_exception_exit();
1293 /* there is no main method or it isn't static */
1295 if (!m || !(m->flags & ACC_STATIC)) {
1296 *exceptionptr = NULL;
1299 new_exception_message(string_java_lang_NoSuchMethodError,
1301 throw_main_exception_exit();
1304 /* build argument array */
1306 a = builtin_anewarray(argc - opt_ind, class_java_lang_String);
1307 for (i = opt_ind; i < argc; i++) {
1308 a->data[i - opt_ind] =
1309 (java_objectheader *) javastring_new(utf_new_char(argv[i]));
1312 #ifdef TYPEINFO_DEBUG_TEST
1313 /* test the typeinfo system */
1316 /*class_showmethods(currentThread->group->header.vftbl->class); */
1318 #if defined(ENABLE_JVMTI) && defined(NATIVE_THREADS)
1321 if (debuggee == (-1)) {
1322 log_text("fork error");
1325 if (debuggee == 0) {
1326 /* child: allow helper process to trace us */
1327 if (TRACEME != 0) exit(0);
1329 /* give parent/helper debugger process control */
1330 kill(0, SIGTRAP); /* do we need this at this stage ? */
1332 /* continue with normal startup */
1336 /* parent/helper debugger process */
1339 remotedbgjvmtienv = new_jvmtienv();
1340 /* set eventcallbacks */
1341 if (JVMTI_ERROR_NONE ==
1343 SetEventCallbacks(remotedbgjvmtienv,
1344 &jvmti_jdwp_EventCallbacks,
1345 sizeof(jvmti_jdwp_EventCallbacks))){
1346 log_text("unable to setup event callbacks");
1350 /* setup listening process (JDWP) */
1351 setup_debugger_process(transport);
1353 /* start to be debugged program */
1356 /* exit debugger process - todo: cleanup */
1368 asm_calljavafunction(m, a, NULL, NULL, NULL);
1370 /* exception occurred? */
1371 if (*exceptionptr) {
1372 throw_main_exception();
1376 #if defined(USE_THREADS)
1377 #if defined(NATIVE_THREADS)
1380 killThread(currentThread);
1384 /* now exit the JavaVM */
1386 /* (*jvm)->DestroyJavaVM(jvm); */
1391 /************* If requested, compile all methods ********************/
1398 classcache_name_entry *nmen;
1399 classcache_class_entry *clsen;
1401 /* create all classes found in the bootclasspath */
1402 /* XXX currently only works with zip/jar's */
1404 loader_load_all_classes();
1406 /* link all classes */
1408 for (slot = 0; slot < hashtable_classcache.size; slot++) {
1409 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
1411 for (; nmen; nmen = nmen->hashlink) {
1412 /* iterate over all class entries */
1414 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
1415 c = clsen->classobj;
1420 if (!(c->state & CLASS_LINKED)) {
1421 if (!link_class(c)) {
1422 fprintf(stderr, "Error linking: ");
1423 utf_fprint_classname(stderr, c->name);
1424 fprintf(stderr, "\n");
1426 /* print out exception and cause */
1428 exceptions_print_exception(*exceptionptr);
1430 /* goto next class */
1436 /* compile all class methods */
1438 for (i = 0; i < c->methodscount; i++) {
1439 m = &(c->methods[i]);
1442 if (!jit_compile(m)) {
1443 fprintf(stderr, "Error compiling: ");
1444 utf_fprint_classname(stderr, c->name);
1445 fprintf(stderr, ".");
1446 utf_fprint(stderr, m->name);
1447 utf_fprint(stderr, m->descriptor);
1448 fprintf(stderr, "\n");
1450 /* print out exception and cause */
1452 exceptions_print_exception(*exceptionptr);
1462 /******** If requested, compile a specific method ***************/
1464 if (specificmethodname) {
1467 /* create, load and link the main class */
1469 if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
1470 throw_main_exception_exit();
1472 if (!link_class(mainclass))
1473 throw_main_exception_exit();
1475 if (specificsignature) {
1476 m = class_resolveclassmethod(mainclass,
1477 utf_new_char(specificmethodname),
1478 utf_new_char(specificsignature),
1482 m = class_resolveclassmethod(mainclass,
1483 utf_new_char(specificmethodname),
1490 char message[MAXLOGTEXT];
1491 sprintf(message, "%s%s", specificmethodname,
1492 specificsignature ? specificsignature : "");
1495 new_exception_message(string_java_lang_NoSuchMethodException,
1498 throw_main_exception_exit();
1506 /* keep compiler happy */
1512 /* cacao_exit ******************************************************************
1514 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1516 *******************************************************************************/
1518 void cacao_exit(s4 status)
1522 assert(class_java_lang_System);
1523 assert(class_java_lang_System->state & CLASS_LOADED);
1525 #if defined(ENABLE_JVMTI)
1526 set_jvmti_phase(JVMTI_PHASE_DEAD);
1530 if (!link_class(class_java_lang_System))
1531 throw_main_exception_exit();
1533 /* signal that we are exiting */
1535 cacao_exiting = true;
1537 /* call java.lang.System.exit(I)V */
1539 m = class_resolveclassmethod(class_java_lang_System,
1540 utf_new_char("exit"),
1542 class_java_lang_Object,
1546 throw_main_exception_exit();
1548 /* call the exit function with passed exit status */
1550 /* both inlinevirtual and outsiders not allowed on exit */
1551 /* not sure if permanant or temp restriction */
1552 if (inlinevirtuals) inlineoutsiders = false;
1554 asm_calljavafunction(m, (void *) (ptrint) status, NULL, NULL, NULL);
1556 /* this should never happen */
1559 throw_exception_exit();
1561 throw_cacao_exception_exit(string_java_lang_InternalError,
1562 "System.exit(I)V returned without exception");
1566 /*************************** Shutdown function *********************************
1568 Terminates the system immediately without freeing memory explicitly (to be
1569 used only for abnormal termination)
1571 *******************************************************************************/
1573 void cacao_shutdown(s4 status)
1576 #if defined(ENABLE_JVMTI)
1580 if (opt_verbose || getcompilingtime || opt_stat) {
1581 log_text("CACAO terminated by shutdown");
1582 dolog("Exit status: %d\n", (s4) status);
1589 /* exit_handler ****************************************************************
1591 The exit_handler function is called upon program termination.
1593 ATTENTION: Don't free system resources here! Some threads may still
1594 be running as this is called from VMRuntime.exit(). The OS does the
1597 *******************************************************************************/
1599 void exit_handler(void)
1601 /********************* Print debug tables ************************/
1603 #if defined(ENABLE_DEBUG)
1605 class_showmethods(mainclass);
1607 if (showconstantpool)
1608 class_showconstantpool(mainclass);
1614 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
1615 clear_thread_flags(); /* restores standard file descriptor
1619 if (opt_verbose || getcompilingtime || opt_stat) {
1620 log_text("CACAO terminated");
1622 #if defined(STATISTICS)
1625 #ifdef TYPECHECK_STATISTICS
1626 typecheck_print_statistics(get_logfile());
1632 if (getcompilingtime)
1636 /* vm_print_profile(stderr);*/
1641 * These are local overrides for various environment variables in Emacs.
1642 * Please do not remove this and leave it at the end of the file, where
1643 * Emacs will automagically detect them.
1644 * ---------------------------------------------------------------------
1647 * indent-tabs-mode: t