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 3904 2005-12-07 17:44:37Z 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_
1166 if (!suck_init(bootclasspath))
1167 throw_main_exception_exit();
1169 /* initialize the memory subsystem (must be done _after_
1173 throw_main_exception_exit();
1175 /* initialize the finalizer stuff: lock, linked list (must be done
1176 _after_ threads_preinit) */
1178 if (!finalizer_init())
1179 throw_main_exception_exit();
1181 /* install architecture dependent signal handler used for exceptions */
1185 /* initialize the codegen subsystems */
1189 /* initializes jit compiler */
1193 /* machine dependent initialization */
1197 /* initialize the loader subsystems (must be done _after_
1200 if (!loader_init((u1 *) &dummy))
1201 throw_main_exception_exit();
1204 throw_main_exception_exit();
1207 throw_main_exception_exit();
1209 if (!exceptions_init())
1210 throw_main_exception_exit();
1212 if (!builtin_init())
1213 throw_main_exception_exit();
1215 #if defined(USE_THREADS)
1216 if (!threads_init((u1 *) &dummy))
1217 throw_main_exception_exit();
1220 /* That's important, otherwise we get into trouble, if the Runtime
1221 static initializer is called before (circular dependency. This
1222 is with classpath 0.09. Another important thing is, that this
1223 has to happen after initThreads!!! */
1225 if (!initialize_class(class_java_lang_System))
1226 throw_main_exception_exit();
1228 /* JNI init creates a Java object (this means running Java code) */
1231 throw_main_exception_exit();
1233 #if defined(USE_THREADS)
1234 /* finally, start the finalizer thread */
1236 if (!finalizer_start_thread())
1237 throw_main_exception_exit();
1240 cacao_initializing = false;
1243 /* start worker routines **************************************************/
1246 classinfo *mainclass; /* java/lang/Class */
1248 java_objectarray *a;
1251 /* set return value to OK */
1256 /* open jar file with java.util.jar.JarFile */
1257 mainstring = getmainclassnamefromjar(mainstring);
1260 /* load the main class */
1262 if (!(mainclass = load_class_from_sysloader(utf_new_char(mainstring))))
1263 throw_main_exception_exit();
1265 /* error loading class, clear exceptionptr for new exception */
1267 if (*exceptionptr || !mainclass) {
1268 /* *exceptionptr = NULL; */
1270 /* *exceptionptr = */
1271 /* new_exception_message(string_java_lang_NoClassDefFoundError, */
1273 throw_main_exception_exit();
1276 if (!link_class(mainclass))
1277 throw_main_exception_exit();
1279 /* find the `main' method of the main class */
1281 m = class_resolveclassmethod(mainclass,
1282 utf_new_char("main"),
1283 utf_new_char("([Ljava/lang/String;)V"),
1284 class_java_lang_Object,
1287 if (*exceptionptr) {
1288 throw_main_exception_exit();
1291 /* there is no main method or it isn't static */
1293 if (!m || !(m->flags & ACC_STATIC)) {
1294 *exceptionptr = NULL;
1297 new_exception_message(string_java_lang_NoSuchMethodError,
1299 throw_main_exception_exit();
1302 /* build argument array */
1304 a = builtin_anewarray(argc - opt_ind, class_java_lang_String);
1305 for (i = opt_ind; i < argc; i++) {
1306 a->data[i - opt_ind] =
1307 (java_objectheader *) javastring_new(utf_new_char(argv[i]));
1310 #ifdef TYPEINFO_DEBUG_TEST
1311 /* test the typeinfo system */
1314 /*class_showmethods(currentThread->group->header.vftbl->class); */
1316 #if defined(ENABLE_JVMTI) && defined(NATIVE_THREADS)
1319 if (debuggee == (-1)) {
1320 log_text("fork error");
1323 if (debuggee == 0) {
1324 /* child: allow helper process to trace us */
1325 if (TRACEME != 0) exit(0);
1327 /* give parent/helper debugger process control */
1328 kill(0, SIGTRAP); /* do we need this at this stage ? */
1330 /* continue with normal startup */
1334 /* parent/helper debugger process */
1337 remotedbgjvmtienv = new_jvmtienv();
1338 /* set eventcallbacks */
1339 if (JVMTI_ERROR_NONE ==
1341 SetEventCallbacks(remotedbgjvmtienv,
1342 &jvmti_jdwp_EventCallbacks,
1343 sizeof(jvmti_jdwp_EventCallbacks))){
1344 log_text("unable to setup event callbacks");
1348 /* setup listening process (JDWP) */
1349 setup_debugger_process(transport);
1351 /* start to be debugged program */
1354 /* exit debugger process - todo: cleanup */
1366 asm_calljavafunction(m, a, NULL, NULL, NULL);
1368 /* exception occurred? */
1369 if (*exceptionptr) {
1370 throw_main_exception();
1374 #if defined(USE_THREADS)
1375 #if defined(NATIVE_THREADS)
1378 killThread(currentThread);
1382 /* now exit the JavaVM */
1384 /* (*jvm)->DestroyJavaVM(jvm); */
1389 /************* If requested, compile all methods ********************/
1396 classcache_name_entry *nmen;
1397 classcache_class_entry *clsen;
1399 /* create all classes found in the bootclasspath */
1400 /* XXX currently only works with zip/jar's */
1402 loader_load_all_classes();
1404 /* link all classes */
1406 for (slot = 0; slot < hashtable_classcache.size; slot++) {
1407 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
1409 for (; nmen; nmen = nmen->hashlink) {
1410 /* iterate over all class entries */
1412 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
1413 c = clsen->classobj;
1418 if (!(c->state & CLASS_LINKED)) {
1419 if (!link_class(c)) {
1420 fprintf(stderr, "Error linking: ");
1421 utf_fprint_classname(stderr, c->name);
1422 fprintf(stderr, ".");
1423 utf_fprint(stderr, m->name);
1424 utf_fprint(stderr, m->descriptor);
1425 fprintf(stderr, "\n");
1427 /* print out exception and cause */
1429 exceptions_print_exception(*exceptionptr);
1431 /* goto next class */
1437 /* compile all class methods */
1439 for (i = 0; i < c->methodscount; i++) {
1440 m = &(c->methods[i]);
1443 if (!jit_compile(m)) {
1444 fprintf(stderr, "Error compiling: ");
1445 utf_fprint_classname(stderr, c->name);
1446 fprintf(stderr, ".");
1447 utf_fprint(stderr, m->name);
1448 utf_fprint(stderr, m->descriptor);
1449 fprintf(stderr, "\n");
1451 /* print out exception and cause */
1453 exceptions_print_exception(*exceptionptr);
1463 /******** If requested, compile a specific method ***************/
1465 if (specificmethodname) {
1468 /* create, load and link the main class */
1470 if (!(mainclass = load_class_bootstrap(utf_new_char(mainstring))))
1471 throw_main_exception_exit();
1473 if (!link_class(mainclass))
1474 throw_main_exception_exit();
1476 if (specificsignature) {
1477 m = class_resolveclassmethod(mainclass,
1478 utf_new_char(specificmethodname),
1479 utf_new_char(specificsignature),
1483 m = class_resolveclassmethod(mainclass,
1484 utf_new_char(specificmethodname),
1491 char message[MAXLOGTEXT];
1492 sprintf(message, "%s%s", specificmethodname,
1493 specificsignature ? specificsignature : "");
1496 new_exception_message(string_java_lang_NoSuchMethodException,
1499 throw_main_exception_exit();
1507 /* keep compiler happy */
1513 /* cacao_exit ******************************************************************
1515 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1517 *******************************************************************************/
1519 void cacao_exit(s4 status)
1523 assert(class_java_lang_System);
1524 assert(class_java_lang_System->state & CLASS_LOADED);
1526 #if defined(ENABLE_JVMTI)
1527 set_jvmti_phase(JVMTI_PHASE_DEAD);
1531 if (!link_class(class_java_lang_System))
1532 throw_main_exception_exit();
1534 /* signal that we are exiting */
1536 cacao_exiting = true;
1538 /* call java.lang.System.exit(I)V */
1540 m = class_resolveclassmethod(class_java_lang_System,
1541 utf_new_char("exit"),
1543 class_java_lang_Object,
1547 throw_main_exception_exit();
1549 /* call the exit function with passed exit status */
1551 /* both inlinevirtual and outsiders not allowed on exit */
1552 /* not sure if permanant or temp restriction */
1553 if (inlinevirtuals) inlineoutsiders = false;
1555 asm_calljavafunction(m, (void *) (ptrint) status, NULL, NULL, NULL);
1557 /* this should never happen */
1560 throw_exception_exit();
1562 throw_cacao_exception_exit(string_java_lang_InternalError,
1563 "System.exit(I)V returned without exception");
1567 /*************************** Shutdown function *********************************
1569 Terminates the system immediately without freeing memory explicitly (to be
1570 used only for abnormal termination)
1572 *******************************************************************************/
1574 void cacao_shutdown(s4 status)
1577 #if defined(ENABLE_JVMTI)
1581 if (opt_verbose || getcompilingtime || opt_stat) {
1582 log_text("CACAO terminated by shutdown");
1583 dolog("Exit status: %d\n", (s4) status);
1590 /* exit_handler ****************************************************************
1592 The exit_handler function is called upon program termination.
1594 ATTENTION: Don't free system resources here! Some threads may still
1595 be running as this is called from VMRuntime.exit(). The OS does the
1598 *******************************************************************************/
1600 void exit_handler(void)
1602 /********************* Print debug tables ************************/
1604 #if defined(ENABLE_DEBUG)
1606 class_showmethods(mainclass);
1608 if (showconstantpool)
1609 class_showconstantpool(mainclass);
1615 #if defined(USE_THREADS) && !defined(NATIVE_THREADS)
1616 clear_thread_flags(); /* restores standard file descriptor
1620 if (opt_verbose || getcompilingtime || opt_stat) {
1621 log_text("CACAO terminated");
1623 #if defined(STATISTICS)
1626 #ifdef TYPECHECK_STATISTICS
1627 typecheck_print_statistics(get_logfile());
1633 if (getcompilingtime)
1637 /* vm_print_profile(stderr);*/
1642 * These are local overrides for various environment variables in Emacs.
1643 * Please do not remove this and leave it at the end of the file, where
1644 * Emacs will automagically detect them.
1645 * ---------------------------------------------------------------------
1648 * indent-tabs-mode: t