1 /* src/vm/vm.c - VM startup and shutdown functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 $Id: vm.c 8397 2007-08-22 15:49:38Z twisti $
37 #if defined(WITH_JRE_LAYOUT)
47 #include "vm/jit/abi-asm.h"
49 #include "mm/gc-common.h"
50 #include "mm/memory.h"
52 #include "native/jni.h"
53 #include "native/llni.h"
54 #include "native/native.h"
56 #include "native/include/java_lang_Object.h" /* required by j.l.C */
57 #include "native/include/java_lang_String.h" /* required by j.l.C */
59 #if defined(WITH_CLASSPATH_SUN)
60 # include "native/include/java_nio_ByteBuffer.h" /* required by j.l.CL */
61 # include "native/include/java_lang_ClassLoader.h" /* required by j.l.C */
64 #include "native/include/java_lang_Class.h"
66 #include "native/vm/nativevm.h"
68 #include "threads/threads-common.h"
70 #include "toolbox/logging.h"
72 #include "vm/builtin.h"
73 #include "vm/exceptions.h"
74 #include "vm/finalizer.h"
75 #include "vm/global.h"
76 #include "vm/initialize.h"
77 #include "vm/primitive.h"
78 #include "vm/properties.h"
79 #include "vm/signallocal.h"
80 #include "vm/stringlocal.h"
83 #include "vm/jit/jit.h"
84 #include "vm/jit/md.h"
85 #include "vm/jit/asmpart.h"
87 #if defined(ENABLE_PROFILING)
88 # include "vm/jit/optimizing/profile.h"
91 #include "vm/jit/optimizing/recompile.h"
93 #include "vmcore/classcache.h"
94 #include "vmcore/options.h"
95 #include "vmcore/statistics.h"
96 #include "vmcore/suck.h"
98 #if defined(ENABLE_JVMTI)
99 # include "native/jvmti/cacaodbg.h"
102 #if defined(ENABLE_VMLOG)
103 #include <vmlog_cacao.h>
107 /* Invocation API variables ***************************************************/
109 _Jv_JavaVM *_Jv_jvm; /* denotes a Java VM */
110 _Jv_JNIEnv *_Jv_env; /* pointer to native method interface */
113 /* global variables ***********************************************************/
115 s4 vms = 0; /* number of VMs created */
117 bool vm_initializing = false;
118 bool vm_exiting = false;
120 char *cacao_prefix = NULL;
121 char *cacao_libjvm = NULL;
122 char *classpath_libdir = NULL;
124 char *_Jv_bootclasspath; /* contains the boot classpath */
125 char *_Jv_classpath; /* contains the classpath */
126 char *_Jv_java_library_path;
128 char *mainstring = NULL;
129 classinfo *mainclass = NULL;
131 char *specificmethodname = NULL;
132 char *specificsignature = NULL;
136 #if defined(ENABLE_INTRP)
137 u1 *intrp_main_stack = NULL;
141 /* define heap sizes **********************************************************/
143 #define HEAP_MAXSIZE 128 * 1024 * 1024 /* default 128MB */
144 #define HEAP_STARTSIZE 2 * 1024 * 1024 /* default 2MB */
145 #define STACK_SIZE 64 * 1024 /* default 64kB */
148 /* define command line options ************************************************/
179 /* Java non-standard options */
190 #if defined(ENABLE_PROFILING)
203 #if defined(ENABLE_STATISTICS)
220 #if defined(ENABLE_VERIFIER)
222 #if defined(TYPECHECK_VERBOSE)
225 #endif /* defined(ENABLE_VERIFIER) */
227 /* optimization options */
229 #if defined(ENABLE_LOOP)
233 #if defined(ENABLE_IFCONV)
237 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
241 #if defined(ENABLE_INLINING)
246 #if defined(ENABLE_INLINING_DEBUG)
247 OPT_INLINE_DEBUG_ALL,
248 OPT_INLINE_DEBUG_END,
249 OPT_INLINE_DEBUG_MIN,
250 OPT_INLINE_DEBUG_MAX,
251 #endif /* defined(ENABLE_INLINING_DEBUG) */
252 #endif /* defined(ENABLE_INLINING) */
254 #if defined(ENABLE_INTRP)
255 /* interpreter options */
274 #if defined(ENABLE_DEBUG_FILTER)
275 OPT_FILTER_VERBOSECALL_INCLUDE,
276 OPT_FILTER_VERBOSECALL_EXCLUDE,
277 OPT_FILTER_SHOW_METHOD,
284 opt_struct opts[] = {
285 { "foo", false, OPT_FOO },
289 { "jar", false, OPT_JAR },
291 { "d32", false, OPT_D32 },
292 { "d64", false, OPT_D64 },
293 { "client", false, OPT_IGNORE },
294 { "server", false, OPT_IGNORE },
295 { "jvm", false, OPT_IGNORE },
296 { "hotspot", false, OPT_IGNORE },
298 { "classpath", true, OPT_CLASSPATH },
299 { "cp", true, OPT_CLASSPATH },
300 { "D", true, OPT_D },
301 { "version", false, OPT_VERSION },
302 { "showversion", false, OPT_SHOWVERSION },
303 { "fullversion", false, OPT_FULLVERSION },
304 { "help", false, OPT_HELP },
305 { "?", false, OPT_HELP },
306 { "X", false, OPT_X },
307 { "XX:", true, OPT_XX },
308 { "XX", false, OPT_XX },
310 { "ea:", true, OPT_EA },
311 { "da:", true, OPT_DA },
312 { "ea", false, OPT_EA },
313 { "da", false, OPT_DA },
315 { "esa", false, OPT_ESA },
316 { "enablesystemassertions", false, OPT_ESA },
317 { "dsa", false, OPT_DSA },
318 { "disablesystemassertions", false, OPT_DSA },
320 { "noasyncgc", false, OPT_IGNORE },
321 #if defined(ENABLE_VERIFIER)
322 { "noverify", false, OPT_NOVERIFY },
323 { "Xverify:none", false, OPT_NOVERIFY },
325 { "v", false, OPT_VERBOSE1 },
326 { "verbose:", true, OPT_VERBOSE },
328 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
329 { "verbosetc", false, OPT_VERBOSETC },
331 #if defined(__ALPHA__)
332 { "noieee", false, OPT_NOIEEE },
334 #if defined(ENABLE_STATISTICS)
335 { "time", false, OPT_TIME },
336 { "stat", false, OPT_STAT },
338 { "log", true, OPT_LOG },
339 { "c", true, OPT_CHECK },
340 { "l", false, OPT_LOAD },
343 { "all", false, OPT_ALL },
344 { "sig", true, OPT_SIGNATURE },
347 #if defined(ENABLE_LOOP)
348 { "oloop", false, OPT_OLOOP },
350 #if defined(ENABLE_IFCONV)
351 { "ifconv", false, OPT_IFCONV },
353 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
354 { "lsra", false, OPT_LSRA },
357 #if defined(ENABLE_INTRP)
358 /* interpreter options */
360 { "trace", false, OPT_TRACE },
361 { "static-supers", true, OPT_STATIC_SUPERS },
362 { "no-dynamic", false, OPT_NO_DYNAMIC },
363 { "no-replication", false, OPT_NO_REPLICATION },
364 { "no-quicksuper", false, OPT_NO_QUICKSUPER },
367 /* JVMTI Agent Command Line Options */
369 { "agentlib:", true, OPT_AGENTLIB },
370 { "agentpath:", true, OPT_AGENTPATH },
373 /* Java non-standard options */
375 { "Xjit", false, OPT_JIT },
376 { "Xint", false, OPT_INTRP },
377 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
378 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
379 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
380 { "Xbootclasspath/c:", true, OPT_BOOTCLASSPATH_C },
383 { "Xdebug", false, OPT_DEBUG },
384 { "Xnoagent", false, OPT_NOAGENT },
385 { "Xrunjdwp", true, OPT_XRUNJDWP },
388 { "Xms", true, OPT_MS },
389 { "ms", true, OPT_MS },
390 { "Xmx", true, OPT_MX },
391 { "mx", true, OPT_MX },
392 { "Xss", true, OPT_SS },
393 { "ss", true, OPT_SS },
395 #if defined(ENABLE_PROFILING)
396 { "Xprof:", true, OPT_PROF_OPTION },
397 { "Xprof", false, OPT_PROF },
400 /* inlining options */
402 #if defined(ENABLE_INLINING)
403 #if defined(ENABLE_INLINING_DEBUG)
404 { "ia", false, OPT_INLINE_DEBUG_ALL },
405 { "ii", true, OPT_INLINE_DEBUG_MIN },
406 { "im", true, OPT_INLINE_DEBUG_MAX },
407 { "ie", true, OPT_INLINE_DEBUG_END },
408 #endif /* defined(ENABLE_INLINING_DEBUG) */
410 { "il", false, OPT_INLINE_LOG },
412 { "i", false, OPT_INLINING },
413 #endif /* defined(ENABLE_INLINING) */
415 /* keep these at the end of the list */
418 { "m", true, OPT_METHOD },
421 { "s", true, OPT_SHOW },
422 { "debug-color", false, OPT_DEBUGCOLOR },
424 #if defined(ENABLE_DEBUG_FILTER)
425 { "XXfi", true, OPT_FILTER_VERBOSECALL_INCLUDE },
426 { "XXfx", true, OPT_FILTER_VERBOSECALL_EXCLUDE },
427 { "XXfm", true, OPT_FILTER_SHOW_METHOD },
434 /* usage ***********************************************************************
436 Prints the correct usage syntax to stdout.
438 *******************************************************************************/
442 puts("Usage: cacao [-options] classname [arguments]");
443 puts(" (to run a class file)");
444 puts(" or cacao [-options] -jar jarfile [arguments]");
445 puts(" (to run a standalone jar file)\n");
447 puts("where options include:");
448 puts(" -d32 use 32-bit data model if available");
449 puts(" -d64 use 64-bit data model if available");
450 puts(" -client compatibility (currently ignored)");
451 puts(" -server compatibility (currently ignored)");
452 puts(" -jvm compatibility (currently ignored)");
453 puts(" -hotspot compatibility (currently ignored)\n");
455 puts(" -cp <path> specify a path to look for classes");
456 puts(" -classpath <path> specify a path to look for classes");
457 puts(" -D<name>=<value> add an entry to the property list");
458 puts(" -verbose[:class|gc|jni] enable specific verbose output");
459 puts(" -version print product version and exit");
460 puts(" -fullversion print jpackage-compatible product version and exit");
461 puts(" -showversion print product version and continue");
462 puts(" -help, -? print this help message");
463 puts(" -X print help on non-standard Java options");
464 puts(" -XX print help on debugging options");
465 puts(" -ea[:<packagename>...|:<classname>]");
466 puts(" -enableassertions[:<packagename>...|:<classname>]");
467 puts(" enable assertions with specified granularity");
468 puts(" -da[:<packagename>...|:<classname>]");
469 puts(" -disableassertions[:<packagename>...|:<classname>]");
470 puts(" disable assertions with specified granularity");
471 puts(" -esa | -enablesystemassertions");
472 puts(" enable system assertions");
473 puts(" -dsa | -disablesystemassertions");
474 puts(" disable system assertions");
477 puts(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent");
478 puts (" for jdwp help use: -agentlib:jdwp=help");
479 puts(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent");
482 /* exit with error code */
488 static void Xusage(void)
490 #if defined(ENABLE_JIT)
491 puts(" -Xjit JIT mode execution (default)");
493 #if defined(ENABLE_INTRP)
494 puts(" -Xint interpreter mode execution");
496 puts(" -Xbootclasspath:<zip/jar files and directories separated by :>");
497 puts(" value is set as bootstrap class path");
498 puts(" -Xbootclasspath/a:<zip/jar files and directories separated by :>");
499 puts(" value is appended to the bootstrap class path");
500 puts(" -Xbootclasspath/p:<zip/jar files and directories separated by :>");
501 puts(" value is prepended to the bootstrap class path");
502 puts(" -Xbootclasspath/c:<zip/jar files and directories separated by :>");
503 puts(" value is used as Java core library, but the");
504 puts(" hardcoded VM interface classes are prepended");
505 printf(" -Xms<size> set the initial size of the heap (default: %dMB)\n", HEAP_STARTSIZE / 1024 / 1024);
506 printf(" -Xmx<size> set the maximum size of the heap (default: %dMB)\n", HEAP_MAXSIZE / 1024 / 1024);
507 printf(" -Xss<size> set the thread stack size (default: %dkB)\n", STACK_SIZE / 1024);
509 #if defined(ENABLE_PROFILING)
510 puts(" -Xprof[:bb] collect and print profiling data");
513 #if defined(ENABLE_JVMTI)
514 /* -Xdebug option depend on gnu classpath JDWP options. options:
515 transport=dt_socket,address=<hostname:port>,server=(y|n),suspend(y|n) */
516 puts(" -Xdebug enable remote debugging\n");
517 puts(" -Xrunjdwp transport=[dt_socket|...],address=<hostname:port>,server=[y|n],suspend=[y|n]\n");
518 puts(" enable remote debugging\n");
521 /* exit with error code */
528 static void XXusage(void)
530 puts(" -v write state-information");
532 puts(" -verbose[:jit|threads]");
533 puts(" enable specific verbose output");
534 puts(" -debug-color colored output for ANSI terms");
536 #ifdef TYPECHECK_VERBOSE
537 puts(" -verbosetc write debug messages while typechecking");
539 #if defined(__ALPHA__)
540 puts(" -noieee don't use ieee compliant arithmetic");
542 #if defined(ENABLE_VERIFIER)
543 puts(" -noverify don't verify classfiles");
545 #if defined(ENABLE_STATISTICS)
546 puts(" -time measure the runtime");
547 puts(" -stat detailed compiler statistics");
549 puts(" -log logfile specify a name for the logfile");
550 puts(" -c(heck)b(ounds) don't check array bounds");
551 puts(" s(ync) don't check for synchronization");
552 #if defined(ENABLE_LOOP)
553 puts(" -oloop optimize array accesses in loops");
555 puts(" -l don't start the class after loading");
557 puts(" -all compile all methods, no execution");
558 puts(" -m compile only a specific method");
559 puts(" -sig specify signature for a specific method");
562 puts(" -s... show...");
563 puts(" (c)onstants the constant pool");
564 puts(" (m)ethods class fields and methods");
565 puts(" (u)tf the utf - hash");
566 puts(" (i)ntermediate intermediate representation");
567 #if defined(ENABLE_DISASSEMBLER)
568 puts(" (a)ssembler disassembled listing");
569 puts(" n(o)ps show NOPs in disassembler output");
570 puts(" (e)xceptionstubs disassembled exception stubs (only with -sa)");
571 puts(" (n)ative disassembled native stubs");
573 puts(" (d)atasegment data segment listing");
575 #if defined(ENABLE_INLINING)
576 puts(" -i activate inlining");
578 puts(" -il log inlining");
580 #if defined(ENABLE_INLINING_DEBUG)
581 puts(" -ia use inlining for all methods");
582 puts(" -ii <size> set minimum size for inlined result");
583 puts(" -im <size> set maximum size for inlined result");
584 puts(" -ie <number> stop inlining after the given number of roots");
585 #endif /* defined(ENABLE_INLINING_DEBUG) */
586 #endif /* defined(ENABLE_INLINING) */
588 #if defined(ENABLE_IFCONV)
589 puts(" -ifconv use if-conversion");
591 #if defined(ENABLE_LSRA)
592 puts(" -lsra use linear scan register allocation");
594 #if defined(ENABLE_SSA)
595 puts(" -lsra use linear scan register allocation (with SSA)");
597 #if defined(ENABLE_DEBUG_FILTER)
598 puts(" -XXfi <regex> begin of dynamic scope for verbosecall filter");
599 puts(" -XXfx <regex> end of dynamic scope for verbosecall filter");
600 puts(" -XXfm <regex> filter for show options");
602 /* exit with error code */
609 /* version *********************************************************************
611 Only prints cacao version information.
613 *******************************************************************************/
615 static void version(bool opt_exit)
617 puts("java version \""JAVA_VERSION"\"");
618 puts("CACAO version "VERSION"");
620 puts("Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,");
621 puts("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,");
622 puts("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,");
623 puts("J. Wenninger, Institut f. Computersprachen - TU Wien\n");
625 puts("This program is free software; you can redistribute it and/or");
626 puts("modify it under the terms of the GNU General Public License as");
627 puts("published by the Free Software Foundation; either version 2, or (at");
628 puts("your option) any later version.\n");
630 puts("This program is distributed in the hope that it will be useful, but");
631 puts("WITHOUT ANY WARRANTY; without even the implied warranty of");
632 puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU");
633 puts("General Public License for more details.");
635 /* exit normally, if requested */
642 /* fullversion *****************************************************************
644 Prints a Sun compatible version information (required e.g. by
645 jpackage, www.jpackage.org).
647 *******************************************************************************/
649 static void fullversion(void)
651 puts("java full version \"cacao-"JAVA_VERSION"\"");
659 void vm_printconfig(void)
661 puts("Configure/Build options:\n");
662 puts(" ./configure: "VERSION_CONFIGURE_ARGS"");
663 #if defined(__VERSION__)
664 puts(" CC : "VERSION_CC" ("__VERSION__")");
666 puts(" CC : "VERSION_CC"");
668 puts(" CFLAGS : "VERSION_CFLAGS"\n");
670 puts("Default variables:\n");
671 printf(" maximum heap size : %d\n", HEAP_MAXSIZE);
672 printf(" initial heap size : %d\n", HEAP_STARTSIZE);
673 printf(" stack size : %d\n", STACK_SIZE);
674 #if defined(WITH_CLASSPATH_GNU)
675 puts(" java.boot.class.path : "CACAO_VM_ZIP":"CLASSPATH_CLASSES"");
677 puts(" java.boot.class.path : "CLASSPATH_CLASSES"");
679 puts(" gnu.classpath.boot.library.path: "CLASSPATH_LIBDIR"/classpath\n");
681 puts("Runtime variables:\n");
682 printf(" maximum heap size : %d\n", opt_heapmaxsize);
683 printf(" initial heap size : %d\n", opt_heapstartsize);
684 printf(" stack size : %d\n", opt_stacksize);
685 printf(" libjvm.so : %s\n", cacao_libjvm);
686 printf(" java.boot.class.path : %s\n", _Jv_bootclasspath);
687 printf(" gnu.classpath.boot.library.path: %s\n", classpath_libdir);
688 printf(" java.class.path : %s\n", _Jv_classpath);
692 /* forward declarations *******************************************************/
694 static char *vm_get_mainclass_from_jar(char *mainstring);
696 static void vm_compile_all(void);
697 static void vm_compile_method(void);
701 /* vm_createjvm ****************************************************************
703 Implementation for JNI_CreateJavaVM.
705 *******************************************************************************/
707 bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
709 JavaVMInitArgs *_vm_args;
713 /* get the arguments for the new JVM */
715 _vm_args = (JavaVMInitArgs *) vm_args;
717 /* get the VM and Env tables (must be set before vm_create) */
719 env = NEW(_Jv_JNIEnv);
721 #if defined(ENABLE_JNI)
722 env->env = &_Jv_JNINativeInterface;
725 /* XXX Set the global variable. Maybe we should do that differently. */
729 /* create and fill a JavaVM structure */
731 vm = NEW(_Jv_JavaVM);
733 #if defined(ENABLE_JNI)
734 vm->functions = &_Jv_JNIInvokeInterface;
737 /* XXX Set the global variable. Maybe we should do that differently. */
738 /* XXX JVMTI Agents needs a JavaVM */
742 /* actually create the JVM */
744 if (!vm_create(_vm_args))
747 #if defined(ENABLE_JNI)
748 /* setup the local ref table (must be created after vm_create) */
750 if (!localref_table_init())
754 /* now return the values */
756 *p_vm = (JavaVM *) vm;
757 *p_env = (void *) env;
762 /* release allocated memory */
764 FREE(env, _Jv_JNIEnv);
765 FREE(vm, _Jv_JavaVM);
771 /* vm_create *******************************************************************
773 Creates a JVM. Called by vm_createjvm.
775 *******************************************************************************/
777 bool vm_create(JavaVMInitArgs *vm_args)
786 #if defined(ENABLE_JVMTI)
788 char *libname, *agentarg;
789 bool jdwp,agentbypath;
790 jdwp = agentbypath = false;
793 #if defined(ENABLE_VMLOG)
794 vmlog_cacao_init(vm_args);
797 /* check the JNI version requested */
799 switch (vm_args->version) {
800 case JNI_VERSION_1_1:
802 case JNI_VERSION_1_2:
803 case JNI_VERSION_1_4:
809 /* we only support 1 JVM instance */
814 if (atexit(vm_exit_handler))
815 vm_abort("atexit failed: %s\n", strerror(errno));
818 log_text("CACAO started -------------------------------------------------------");
820 /* We need to check if the actual size of a java.lang.Class object
821 is smaller or equal than the assumption made in
822 src/vmcore/class.h. */
824 if (sizeof(java_lang_Class) > sizeof(dummy_java_lang_Class))
825 vm_abort("vm_create: java_lang_Class structure is bigger than classinfo.object (%d > %d)", sizeof(java_lang_Class), sizeof(dummy_java_lang_Class));
827 /* set the VM starttime */
829 _Jv_jvm->starttime = builtin_currenttimemillis();
831 /* get stuff from the environment *****************************************/
833 #if defined(WITH_JRE_LAYOUT)
834 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
836 cacao_prefix = MNEW(char, 4096);
838 if (readlink("/proc/self/exe", cacao_prefix, 4095) == -1)
839 vm_abort("readlink failed: %s\n", strerror(errno));
841 /* Get the path of the current executable. */
843 cacao_prefix = dirname(cacao_prefix);
845 if ((strlen(cacao_prefix) + strlen("/..") + strlen("0")) > 4096)
846 vm_abort("libjvm name to long for buffer\n");
848 /* Walk up one directory. */
850 strcat(cacao_prefix, "/..");
852 # if defined(WITH_CLASSPATH_GNU)
854 /* Set path to libjvm.so. */
857 strlen(cacao_prefix) + strlen("/lib/libjvm") +
860 cacao_libjvm = MNEW(char, len);
862 strcpy(cacao_libjvm, cacao_prefix);
863 strcat(cacao_libjvm, "/lib/libjvm");
865 /* Set the path to Java core native libraries. */
868 strlen(cacao_prefix) + strlen("/lib/classpath") +
871 classpath_libdir = MNEW(char, len);
873 strcpy(classpath_libdir, cacao_prefix);
874 strcat(classpath_libdir, "/lib/classpath");
876 # elif defined(WITH_CLASSPATH_SUN)
878 /* Set path to libjvm.so. */
881 strlen(cacao_prefix) + strlen("/lib/"JAVA_ARCH"/server/libjvm") +
884 cacao_libjvm = MNEW(char, len);
886 strcpy(cacao_libjvm, cacao_prefix);
887 strcat(cacao_libjvm, "/lib/"JAVA_ARCH"/server/libjvm");
889 /* Set the path to Java core native libraries. */
892 strlen(cacao_prefix) + strlen("/lib/"JAVA_ARCH) +
895 classpath_libdir = MNEW(char, len);
897 strcpy(classpath_libdir, cacao_prefix);
898 strcat(classpath_libdir, "/lib/"JAVA_ARCH);
901 # error unknown classpath configuration
904 cacao_prefix = CACAO_PREFIX;
905 cacao_libjvm = CACAO_LIBDIR"/libjvm";
907 # if defined(WITH_CLASSPATH_GNU)
908 classpath_libdir = CLASSPATH_LIBDIR"/classpath";
910 classpath_libdir = CLASSPATH_LIBDIR;
914 /* Set the bootclasspath. */
916 cp = getenv("BOOTCLASSPATH");
919 _Jv_bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
920 strcpy(_Jv_bootclasspath, cp);
923 #if defined(WITH_JRE_LAYOUT)
924 # if defined(WITH_CLASSPATH_GNU)
927 strlen(cacao_prefix) + strlen("/share/cacao/vm.zip:") +
928 strlen(cacao_prefix) + strlen("/share/classpath/glibj.zip") +
931 _Jv_bootclasspath = MNEW(char, len);
933 strcpy(_Jv_bootclasspath, cacao_prefix);
934 strcat(_Jv_bootclasspath, "/share/cacao/vm.zip");
935 strcat(_Jv_bootclasspath, ":");
936 strcat(_Jv_bootclasspath, cacao_prefix);
937 strcat(_Jv_bootclasspath, "/share/classpath/glibj.zip");
939 # elif defined(WITH_CLASSPATH_SUN)
941 /* This is the bootclasspath taken from HotSpot (see
942 hotspot/src/share/vm/runtime/os.cpp
943 (os::set_boot_path)). */
946 strlen(cacao_prefix) + strlen("/lib/resources.jar:") +
947 strlen(cacao_prefix) + strlen("/lib/rt.jar:") +
948 strlen(cacao_prefix) + strlen("/lib/sunrsasign.jar:") +
949 strlen(cacao_prefix) + strlen("/lib/jsse.jar:") +
950 strlen(cacao_prefix) + strlen("/lib/jce.jar:") +
951 strlen(cacao_prefix) + strlen("/lib/charsets.jar:") +
952 strlen(cacao_prefix) + strlen("/classes") +
955 _Jv_bootclasspath = MNEW(char, len);
957 strcpy(_Jv_bootclasspath, cacao_prefix);
958 strcat(_Jv_bootclasspath, "/lib/resources.jar:");
959 strcat(_Jv_bootclasspath, cacao_prefix);
960 strcat(_Jv_bootclasspath, "/lib/rt.jar:");
961 strcat(_Jv_bootclasspath, cacao_prefix);
962 strcat(_Jv_bootclasspath, "/lib/sunrsasign.jar:");
963 strcat(_Jv_bootclasspath, cacao_prefix);
964 strcat(_Jv_bootclasspath, "/lib/jsse.jar:");
965 strcat(_Jv_bootclasspath, cacao_prefix);
966 strcat(_Jv_bootclasspath, "/lib/jce.jar:");
967 strcat(_Jv_bootclasspath, cacao_prefix);
968 strcat(_Jv_bootclasspath, "/lib/charsets.jar:");
969 strcat(_Jv_bootclasspath, cacao_prefix);
970 strcat(_Jv_bootclasspath, "/classes");
973 # error unknown classpath configuration
976 # if defined(WITH_CLASSPATH_GNU)
979 strlen(CACAO_VM_ZIP) +
981 strlen(CLASSPATH_CLASSES) +
984 _Jv_bootclasspath = MNEW(char, len);
986 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
987 strcat(_Jv_bootclasspath, ":");
988 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
990 # elif defined(WITH_CLASSPATH_SUN)
992 /* This is the bootclasspath taken from HotSpot (see
993 hotspot/src/share/vm/runtime/os.cpp
994 (os::set_boot_path)). */
997 strlen(CLASSPATH_PREFIX"/lib/resources.jar:") +
998 strlen(CLASSPATH_PREFIX"/lib/rt.jar:") +
999 strlen(CLASSPATH_PREFIX"/lib/sunrsasign.jar:") +
1000 strlen(CLASSPATH_PREFIX"/lib/jsse.jar:") +
1001 strlen(CLASSPATH_PREFIX"/lib/jce.jar:") +
1002 strlen(CLASSPATH_PREFIX"/lib/charsets.jar:") +
1003 strlen(CLASSPATH_PREFIX"/classes") +
1006 _Jv_bootclasspath = MNEW(char, len);
1008 strcpy(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/resources.jar:");
1009 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/rt.jar:");
1010 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/sunrsasign.jar:");
1011 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/jsse.jar:");
1012 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/jce.jar:");
1013 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/charsets.jar:");
1014 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/classes");
1016 # elif defined(WITH_CLASSPATH_CLDC1_1)
1019 strlen(CLASSPATH_CLASSES) +
1022 _Jv_bootclasspath = MNEW(char, len);
1024 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
1027 # error unknown classpath configuration
1032 /* Set the classpath. */
1034 cp = getenv("CLASSPATH");
1037 _Jv_classpath = MNEW(char, strlen(cp) + strlen("0"));
1038 strcpy(_Jv_classpath, cp);
1041 _Jv_classpath = MNEW(char, strlen(".") + strlen("0"));
1042 strcpy(_Jv_classpath, ".");
1045 /* Get and set java.library.path. */
1047 _Jv_java_library_path = getenv("LD_LIBRARY_PATH");
1049 if (_Jv_java_library_path == NULL)
1050 _Jv_java_library_path = "";
1052 /* interpret the options **************************************************/
1054 opt_version = false;
1059 opt_heapmaxsize = HEAP_MAXSIZE;
1060 opt_heapstartsize = HEAP_STARTSIZE;
1061 opt_stacksize = STACK_SIZE;
1064 #if defined(ENABLE_JVMTI)
1065 /* initialize JVMTI related **********************************************/
1069 /* Initialize and fill properties before command-line handling. */
1071 if (!properties_init())
1072 vm_abort("vm_create: properties_init failed");
1074 /* Set the classpath properties. */
1076 #if defined(ENABLE_JAVASE)
1077 properties_add("java.boot.class.path", _Jv_bootclasspath);
1078 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1079 properties_add("java.class.path", _Jv_classpath);
1082 /* iterate over all passed options */
1084 while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
1098 #if SIZEOF_VOID_P == 8
1099 puts("Running a 32-bit JVM is not supported on this platform.");
1105 #if SIZEOF_VOID_P == 4
1106 puts("Running a 64-bit JVM is not supported on this platform.");
1112 /* forget old classpath and set the argument as new classpath */
1113 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1115 _Jv_classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
1116 strcpy(_Jv_classpath, opt_arg);
1118 #if defined(ENABLE_JAVASE)
1119 properties_add("java.class.path", _Jv_classpath);
1124 for (i = 0; i < strlen(opt_arg); i++) {
1125 if (opt_arg[i] == '=') {
1127 properties_add(opt_arg, opt_arg + i + 1);
1132 /* if no '=' is given, just create an empty property */
1134 properties_add(opt_arg, "");
1139 case OPT_BOOTCLASSPATH:
1140 /* Forget default bootclasspath and set the argument as
1141 new boot classpath. */
1143 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1145 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
1146 strcpy(_Jv_bootclasspath, opt_arg);
1148 #if defined(ENABLE_JAVASE)
1149 properties_add("java.boot.class.path", _Jv_bootclasspath);
1150 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1154 case OPT_BOOTCLASSPATH_A:
1155 /* append to end of bootclasspath */
1157 len = strlen(_Jv_bootclasspath);
1159 _Jv_bootclasspath = MREALLOC(_Jv_bootclasspath,
1163 strlen(opt_arg) + strlen("0"));
1165 strcat(_Jv_bootclasspath, ":");
1166 strcat(_Jv_bootclasspath, opt_arg);
1168 #if defined(ENABLE_JAVASE)
1169 properties_add("java.boot.class.path", _Jv_bootclasspath);
1170 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1174 case OPT_BOOTCLASSPATH_P:
1175 /* prepend in front of bootclasspath */
1177 cp = _Jv_bootclasspath;
1180 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
1183 strcpy(_Jv_bootclasspath, opt_arg);
1184 strcat(_Jv_bootclasspath, ":");
1185 strcat(_Jv_bootclasspath, cp);
1187 MFREE(cp, char, len);
1189 #if defined(ENABLE_JAVASE)
1190 properties_add("java.boot.class.path", _Jv_bootclasspath);
1191 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1195 case OPT_BOOTCLASSPATH_C:
1196 /* use as Java core library, but prepend VM interface classes */
1198 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1200 len = strlen(CACAO_VM_ZIP) +
1205 _Jv_bootclasspath = MNEW(char, len);
1207 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
1208 strcat(_Jv_bootclasspath, ":");
1209 strcat(_Jv_bootclasspath, opt_arg);
1211 #if defined(ENABLE_JAVASE)
1212 properties_add("java.boot.class.path", _Jv_bootclasspath);
1213 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1217 #if defined(ENABLE_JVMTI)
1219 /* this option exists only for compatibility reasons */
1223 /* I don't know yet what Xnoagent should do. This is only for
1224 compatiblity with eclipse - motse */
1233 strlen(CACAO_LIBDIR) +
1234 strlen("/libjdwp.so=") +
1238 agentarg = MNEW(char, len);
1240 strcpy(agentarg, CACAO_LIBDIR);
1241 strcat(agentarg, "/libjdwp.so=");
1242 strcat(agentarg, &opt_arg[1]);
1259 c = opt_arg[strlen(opt_arg) - 1];
1261 if ((c == 'k') || (c == 'K')) {
1262 j = atoi(opt_arg) * 1024;
1264 } else if ((c == 'm') || (c == 'M')) {
1265 j = atoi(opt_arg) * 1024 * 1024;
1271 opt_heapmaxsize = j;
1272 else if (opt == OPT_MS)
1273 opt_heapstartsize = j;
1284 if (strcmp("class", opt_arg) == 0) {
1285 opt_verboseclass = true;
1287 else if (strcmp("gc", opt_arg) == 0) {
1288 opt_verbosegc = true;
1290 else if (strcmp("jni", opt_arg) == 0) {
1291 opt_verbosejni = true;
1293 #if !defined(NDEBUG)
1294 else if (strcmp("jit", opt_arg) == 0) {
1298 compileverbose = true;
1300 else if (strcmp("threads", opt_arg) == 0) {
1301 opt_verbosethreads = true;
1305 printf("Unknown -verbose option: %s\n", opt_arg);
1310 case OPT_DEBUGCOLOR:
1311 opt_debugcolor = true;
1314 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
1316 opt_typecheckverbose = true;
1325 case OPT_FULLVERSION:
1329 case OPT_SHOWVERSION:
1337 #if defined(ENABLE_VERIFIER)
1343 #if defined(ENABLE_STATISTICS)
1345 opt_getcompilingtime = true;
1346 opt_getloadingtime = true;
1359 for (i = 0; i < strlen(opt_arg); i++) {
1360 switch (opt_arg[i]) {
1362 checkbounds = false;
1375 makeinitializations = false;
1378 #if !defined(NDEBUG)
1382 makeinitializations = false;
1387 opt_method = opt_arg;
1388 makeinitializations = false;
1392 opt_signature = opt_arg;
1396 case OPT_SHOW: /* Display options */
1397 for (i = 0; i < strlen(opt_arg); i++) {
1398 switch (opt_arg[i]) {
1400 showconstantpool = true;
1412 opt_showintermediate = true;
1413 compileverbose = true;
1416 #if defined(ENABLE_DISASSEMBLER)
1418 opt_showdisassemble = true;
1419 compileverbose = true;
1423 opt_shownops = true;
1427 opt_showexceptionstubs = true;
1431 opt_shownativestub = true;
1436 opt_showddatasegment = true;
1445 #if defined(ENABLE_LOOP)
1451 #if defined(ENABLE_INLINING)
1452 #if defined(ENABLE_INLINING_DEBUG)
1453 case OPT_INLINE_DEBUG_ALL:
1454 opt_inline_debug_all = true;
1456 case OPT_INLINE_DEBUG_END:
1457 opt_inline_debug_end_counter = atoi(opt_arg);
1459 case OPT_INLINE_DEBUG_MIN:
1460 opt_inline_debug_min_size = atoi(opt_arg);
1462 case OPT_INLINE_DEBUG_MAX:
1463 opt_inline_debug_max_size = atoi(opt_arg);
1465 #endif /* defined(ENABLE_INLINING_DEBUG) */
1466 #if !defined(NDEBUG)
1467 case OPT_INLINE_LOG:
1468 opt_inline_debug_log = true;
1470 #endif /* !defined(NDEBUG) */
1473 opt_inlining = true;
1475 #endif /* defined(ENABLE_INLINING) */
1477 #if defined(ENABLE_IFCONV)
1483 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1498 options_xx(opt_arg);
1502 /* currently ignored */
1506 /* currently ignored */
1510 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = true;
1514 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = false;
1517 #if defined(ENABLE_PROFILING)
1518 case OPT_PROF_OPTION:
1519 /* use <= to get the last \0 too */
1521 for (i = 0, j = 0; i <= strlen(opt_arg); i++) {
1522 if (opt_arg[i] == ',')
1525 if (opt_arg[i] == '\0') {
1526 if (strcmp("bb", opt_arg + j) == 0)
1530 printf("Unknown option: -Xprof:%s\n", opt_arg + j);
1534 /* set k to next char */
1547 #if defined(ENABLE_JIT)
1550 printf("-Xjit option not enabled.\n");
1556 #if defined(ENABLE_INTRP)
1559 printf("-Xint option not enabled.\n");
1564 #if defined(ENABLE_INTRP)
1565 case OPT_STATIC_SUPERS:
1566 opt_static_supers = atoi(opt_arg);
1569 case OPT_NO_DYNAMIC:
1570 opt_no_dynamic = true;
1573 case OPT_NO_REPLICATION:
1574 opt_no_replication = true;
1577 case OPT_NO_QUICKSUPER:
1578 opt_no_quicksuper = true;
1586 #if defined(ENABLE_DEBUG_FILTER)
1587 case OPT_FILTER_VERBOSECALL_INCLUDE:
1588 opt_filter_verbosecall_include = opt_arg;
1591 case OPT_FILTER_VERBOSECALL_EXCLUDE:
1592 opt_filter_verbosecall_exclude = opt_arg;
1595 case OPT_FILTER_SHOW_METHOD:
1596 opt_filter_show_method = opt_arg;
1601 printf("Unknown option: %s\n",
1602 vm_args->options[opt_index].optionString);
1607 /* get the main class *****************************************************/
1609 if (opt_index < vm_args->nOptions) {
1610 mainstring = vm_args->options[opt_index++].optionString;
1612 /* Put the jar file into the classpath (if any). */
1614 if (opt_jar == true) {
1615 /* free old classpath */
1617 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1619 /* put jarfile into classpath */
1621 _Jv_classpath = MNEW(char, strlen(mainstring) + strlen("0"));
1623 strcpy(_Jv_classpath, mainstring);
1625 #if defined(ENABLE_JAVASE)
1626 properties_add("java.class.path", _Jv_classpath);
1630 /* replace .'s with /'s in classname */
1632 for (i = strlen(mainstring) - 1; i >= 0; i--)
1633 if (mainstring[i] == '.')
1634 mainstring[i] = '/';
1638 #if defined(ENABLE_JVMTI)
1640 jvmti_set_phase(JVMTI_PHASE_ONLOAD);
1641 jvmti_agentload(agentarg, agentbypath, &handle, &libname);
1644 MFREE(agentarg, char, strlen(agentarg));
1646 jvmti_set_phase(JVMTI_PHASE_PRIMORDIAL);
1650 /* initialize this JVM ****************************************************/
1652 vm_initializing = true;
1654 /* initialize the garbage collector */
1656 gc_init(opt_heapmaxsize, opt_heapstartsize);
1658 #if defined(ENABLE_THREADS)
1659 /* AFTER: gc_init (directly after, as this initializes the
1660 stopworldlock lock */
1665 /* install architecture dependent signal handlers */
1668 vm_abort("vm_create: signal_init failed");
1670 #if defined(ENABLE_INTRP)
1671 /* Allocate main thread stack on the Java heap. */
1674 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1675 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1679 /* AFTER: threads_preinit */
1682 vm_abort("vm_create: string_init failed");
1684 /* AFTER: threads_preinit */
1687 vm_abort("vm_create: utf8_init failed");
1689 /* AFTER: thread_preinit */
1692 vm_abort("vm_create: suck_init failed");
1694 suck_add_from_property("java.endorsed.dirs");
1696 /* Now we have all options handled and we can print the version
1699 AFTER: suck_add_from_property("java.endorsed.dirs"); */
1704 /* AFTER: utf8_init */
1706 suck_add(_Jv_bootclasspath);
1708 /* initialize the classcache hashtable stuff: lock, hashtable
1709 (must be done _after_ threads_preinit) */
1711 if (!classcache_init())
1712 vm_abort("vm_create: classcache_init failed");
1714 /* initialize the memory subsystem (must be done _after_
1718 vm_abort("vm_create: memory_init failed");
1720 /* initialize the finalizer stuff (must be done _after_
1723 if (!finalizer_init())
1724 vm_abort("vm_create: finalizer_init failed");
1726 /* initialize the codegen subsystems */
1730 /* initializes jit compiler */
1734 /* machine dependent initialization */
1736 #if defined(ENABLE_JIT)
1737 # if defined(ENABLE_INTRP)
1747 /* initialize the loader subsystems (must be done _after_
1751 vm_abort("vm_create: loader_init failed");
1753 /* Link some important VM classes. */
1754 /* AFTER: utf8_init */
1757 vm_abort("vm_create: linker_init failed");
1759 if (!primitive_init())
1760 vm_abort("vm_create: primitive_init failed");
1762 if (!exceptions_init())
1763 vm_abort("vm_create: exceptions_init failed");
1765 if (!builtin_init())
1766 vm_abort("vm_create: builtin_init failed");
1768 /* Initialize the native subsystem. */
1769 /* BEFORE: threads_init */
1772 vm_abort("vm_create: native_init failed");
1774 /* Register the native methods implemented in the VM. */
1775 /* BEFORE: threads_init */
1777 if (!nativevm_preinit())
1778 vm_abort("vm_create: nativevm_preinit failed");
1780 #if defined(ENABLE_JNI)
1781 /* Initialize the JNI subsystem (must be done _before_
1782 threads_init, as threads_init can call JNI methods
1783 (e.g. NewGlobalRef). */
1786 vm_abort("vm_create: jni_init failed");
1789 #if defined(ENABLE_THREADS)
1790 if (!threads_init())
1791 vm_abort("vm_create: threads_init failed");
1794 /* Initialize the native VM subsystem. */
1795 /* AFTER: threads_init (at least for SUN's classes) */
1797 if (!nativevm_init())
1798 vm_abort("vm_create: nativevm_init failed");
1800 #if defined(ENABLE_PROFILING)
1801 /* initialize profiling */
1803 if (!profile_init())
1804 vm_abort("vm_create: profile_init failed");
1807 #if defined(ENABLE_THREADS)
1808 /* initialize recompilation */
1810 if (!recompile_init())
1811 vm_abort("vm_create: recompile_init failed");
1813 /* start the signal handler thread */
1815 #if defined(__LINUX__)
1816 /* XXX Remove for exact-GC. */
1817 if (threads_pthreads_implementation_nptl)
1819 if (!signal_start_thread())
1820 vm_abort("vm_create: signal_start_thread failed");
1822 /* finally, start the finalizer thread */
1824 if (!finalizer_start_thread())
1825 vm_abort("vm_create: finalizer_start_thread failed");
1827 # if !defined(NDEBUG)
1828 /* start the memory profiling thread */
1830 if (opt_ProfileMemoryUsage || opt_ProfileGCMemoryUsage)
1831 if (!memory_start_thread())
1832 vm_abort("vm_create: memory_start_thread failed");
1835 /* start the recompilation thread (must be done before the
1836 profiling thread) */
1838 if (!recompile_start_thread())
1839 vm_abort("vm_create: recompile_start_thread failed");
1841 # if defined(ENABLE_PROFILING)
1842 /* start the profile sampling thread */
1845 /* if (!profile_start_thread()) */
1846 /* vm_abort("vm_create: profile_start_thread failed"); */
1850 #if defined(ENABLE_JVMTI)
1852 /* add agent library to native library hashtable */
1853 native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
1857 /* increment the number of VMs */
1861 /* initialization is done */
1863 vm_initializing = false;
1865 /* everything's ok */
1871 /* vm_run **********************************************************************
1873 Runs the main-method of the passed class.
1875 *******************************************************************************/
1877 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1880 classinfo *mainclass;
1883 java_handle_objectarray_t *oa;
1890 #if !defined(NDEBUG)
1896 if (opt_method != NULL) {
1897 vm_compile_method();
1900 #endif /* !defined(NDEBUG) */
1902 /* should we run the main-method? */
1904 if (mainstring == NULL)
1907 /* set return value to OK */
1911 if (opt_jar == true) {
1912 /* open jar file with java.util.jar.JarFile */
1914 mainstring = vm_get_mainclass_from_jar(mainstring);
1916 if (mainstring == NULL)
1920 /* load the main class */
1922 mainutf = utf_new_char(mainstring);
1924 #if defined(ENABLE_JAVAME_CLDC1_1)
1925 mainclass = load_class_bootstrap(mainutf);
1927 mainclass = load_class_from_sysloader(mainutf);
1930 /* error loading class */
1932 e = exceptions_get_and_clear_exception();
1934 if ((e != NULL) || (mainclass == NULL)) {
1935 exceptions_throw_noclassdeffounderror_cause(e);
1936 exceptions_print_stacktrace();
1940 if (!link_class(mainclass)) {
1941 exceptions_print_stacktrace();
1945 /* find the `main' method of the main class */
1947 m = class_resolveclassmethod(mainclass,
1948 utf_new_char("main"),
1949 utf_new_char("([Ljava/lang/String;)V"),
1950 class_java_lang_Object,
1953 if (exceptions_get_exception()) {
1954 exceptions_print_stacktrace();
1958 /* there is no main method or it isn't static */
1960 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
1961 exceptions_clear_exception();
1962 exceptions_throw_nosuchmethoderror(mainclass,
1963 utf_new_char("main"),
1964 utf_new_char("([Ljava/lang/String;)V"));
1966 exceptions_print_stacktrace();
1970 /* build argument array */
1972 oalength = vm_args->nOptions - opt_index;
1974 oa = builtin_anewarray(oalength, class_java_lang_String);
1976 for (i = 0; i < oalength; i++) {
1977 u = utf_new_char(vm_args->options[opt_index + i].optionString);
1978 s = javastring_new(u);
1980 LLNI_objectarray_element_set(oa, i, s);
1983 #ifdef TYPEINFO_DEBUG_TEST
1984 /* test the typeinfo system */
1987 /*class_showmethods(currentThread->group->header.vftbl->class); */
1989 #if defined(ENABLE_JVMTI)
1990 jvmti_set_phase(JVMTI_PHASE_LIVE);
1993 /* set ThreadMXBean variables */
1995 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
1996 _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
1998 if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
1999 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
2000 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
2001 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
2003 /* start the main thread */
2005 (void) vm_call_method(m, NULL, oa);
2007 /* exception occurred? */
2009 if (exceptions_get_exception()) {
2010 exceptions_print_stacktrace();
2014 /* unload the JavaVM */
2016 (void) vm_destroy(vm);
2024 /* vm_destroy ******************************************************************
2026 Unloads a Java VM and reclaims its resources.
2028 *******************************************************************************/
2030 s4 vm_destroy(JavaVM *vm)
2032 #if defined(ENABLE_THREADS)
2033 threads_join_all_threads();
2036 /* everything's ok */
2042 /* vm_exit *********************************************************************
2044 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
2046 *******************************************************************************/
2048 void vm_exit(s4 status)
2052 /* signal that we are exiting */
2056 assert(class_java_lang_System);
2057 assert(class_java_lang_System->state & CLASS_LOADED);
2059 #if defined(ENABLE_JVMTI)
2060 if (jvmti || (dbgcom!=NULL)) {
2061 jvmti_set_phase(JVMTI_PHASE_DEAD);
2062 if (jvmti) jvmti_agentunload();
2066 if (!link_class(class_java_lang_System)) {
2067 exceptions_print_stacktrace();
2071 /* call java.lang.System.exit(I)V */
2073 m = class_resolveclassmethod(class_java_lang_System,
2074 utf_new_char("exit"),
2076 class_java_lang_Object,
2080 exceptions_print_stacktrace();
2084 /* call the exit function with passed exit status */
2086 (void) vm_call_method(m, NULL, status);
2088 /* If we had an exception, just ignore the exception and exit with
2091 vm_shutdown(status);
2095 /* vm_shutdown *****************************************************************
2097 Terminates the system immediately without freeing memory explicitly
2098 (to be used only for abnormal termination).
2100 *******************************************************************************/
2102 void vm_shutdown(s4 status)
2105 #if defined(ENABLE_STATISTICS)
2106 || opt_getcompilingtime || opt_stat
2110 log_text("CACAO terminated by shutdown");
2111 dolog("Exit status: %d\n", (s4) status);
2115 #if defined(ENABLE_JVMTI)
2116 /* terminate cacaodbgserver */
2118 pthread_mutex_lock(&dbgcomlock);
2120 pthread_mutex_unlock(&dbgcomlock);
2121 jvmti_cacaodbgserver_quit();
2129 /* vm_exit_handler *************************************************************
2131 The exit_handler function is called upon program termination.
2133 ATTENTION: Don't free system resources here! Some threads may still
2134 be running as this is called from VMRuntime.exit(). The OS does the
2137 *******************************************************************************/
2139 void vm_exit_handler(void)
2141 #if !defined(NDEBUG)
2143 class_showmethods(mainclass);
2145 if (showconstantpool)
2146 class_showconstantpool(mainclass);
2151 # if defined(ENABLE_PROFILING)
2153 profile_printstats();
2155 #endif /* !defined(NDEBUG) */
2157 #if defined(ENABLE_RT_TIMING)
2158 rt_timing_print_time_stats(stderr);
2161 #if defined(ENABLE_CYCLES_STATS)
2162 builtin_print_cycles_stats(stderr);
2163 stacktrace_print_cycles_stats(stderr);
2167 #if defined(ENABLE_STATISTICS)
2168 || opt_getcompilingtime || opt_stat
2172 log_text("CACAO terminated");
2174 #if defined(ENABLE_STATISTICS)
2177 #ifdef TYPECHECK_STATISTICS
2178 typecheck_print_statistics(get_logfile());
2182 if (opt_getcompilingtime)
2184 #endif /* defined(ENABLE_STATISTICS) */
2186 /* vm_print_profile(stderr);*/
2190 /* vm_abort ********************************************************************
2192 Prints an error message and aborts the VM.
2194 *******************************************************************************/
2196 void vm_abort(const char *text, ...)
2200 /* print the log message */
2205 log_vprint(text, ap);
2210 /* now abort the VM */
2216 /* vm_get_mainclass_from_jar ***************************************************
2218 Gets the name of the main class from a JAR's manifest file.
2220 *******************************************************************************/
2222 static char *vm_get_mainclass_from_jar(char *mainstring)
2229 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
2232 exceptions_print_stacktrace();
2236 /* create JarFile object */
2241 exceptions_print_stacktrace();
2245 m = class_resolveclassmethod(c,
2247 utf_java_lang_String__void,
2248 class_java_lang_Object,
2252 exceptions_print_stacktrace();
2256 s = javastring_new_from_ascii(mainstring);
2258 (void) vm_call_method(m, o, s);
2260 if (exceptions_get_exception()) {
2261 exceptions_print_stacktrace();
2265 /* get manifest object */
2267 m = class_resolveclassmethod(c,
2268 utf_new_char("getManifest"),
2269 utf_new_char("()Ljava/util/jar/Manifest;"),
2270 class_java_lang_Object,
2274 exceptions_print_stacktrace();
2278 o = vm_call_method(m, o);
2281 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
2286 /* get Main Attributes */
2288 m = class_resolveclassmethod(o->vftbl->class,
2289 utf_new_char("getMainAttributes"),
2290 utf_new_char("()Ljava/util/jar/Attributes;"),
2291 class_java_lang_Object,
2295 exceptions_print_stacktrace();
2299 o = vm_call_method(m, o);
2302 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
2307 /* get property Main-Class */
2309 m = class_resolveclassmethod(o->vftbl->class,
2310 utf_new_char("getValue"),
2311 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
2312 class_java_lang_Object,
2316 exceptions_print_stacktrace();
2320 s = javastring_new_from_ascii("Main-Class");
2322 o = vm_call_method(m, o, s);
2325 exceptions_print_stacktrace();
2329 return javastring_tochar(o);
2333 /* vm_compile_all **************************************************************
2335 Compile all methods found in the bootclasspath.
2337 *******************************************************************************/
2339 #if !defined(NDEBUG)
2340 static void vm_compile_all(void)
2345 classcache_name_entry *nmen;
2346 classcache_class_entry *clsen;
2349 /* create all classes found in the bootclasspath */
2350 /* XXX currently only works with zip/jar's */
2352 loader_load_all_classes();
2354 /* link all classes */
2356 for (slot = 0; slot < hashtable_classcache.size; slot++) {
2357 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
2359 for (; nmen; nmen = nmen->hashlink) {
2360 /* iterate over all class entries */
2362 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
2363 c = clsen->classobj;
2368 if (!(c->state & CLASS_LINKED)) {
2369 if (!link_class(c)) {
2370 fprintf(stderr, "Error linking: ");
2371 utf_fprint_printable_ascii_classname(stderr, c->name);
2372 fprintf(stderr, "\n");
2374 /* print out exception and cause */
2376 exceptions_print_current_exception();
2378 /* goto next class */
2384 /* compile all class methods */
2386 for (i = 0; i < c->methodscount; i++) {
2387 m = &(c->methods[i]);
2389 if (m->jcode != NULL) {
2390 if (!jit_compile(m)) {
2391 fprintf(stderr, "Error compiling: ");
2392 utf_fprint_printable_ascii_classname(stderr, c->name);
2393 fprintf(stderr, ".");
2394 utf_fprint_printable_ascii(stderr, m->name);
2395 utf_fprint_printable_ascii(stderr, m->descriptor);
2396 fprintf(stderr, "\n");
2398 /* print out exception and cause */
2400 exceptions_print_current_exception();
2408 #endif /* !defined(NDEBUG) */
2411 /* vm_compile_method ***********************************************************
2413 Compile a specific method.
2415 *******************************************************************************/
2417 #if !defined(NDEBUG)
2418 static void vm_compile_method(void)
2422 /* create, load and link the main class */
2424 mainclass = load_class_bootstrap(utf_new_char(mainstring));
2426 if (mainclass == NULL)
2427 exceptions_print_stacktrace();
2429 if (!link_class(mainclass))
2430 exceptions_print_stacktrace();
2432 if (opt_signature != NULL) {
2433 m = class_resolveclassmethod(mainclass,
2434 utf_new_char(opt_method),
2435 utf_new_char(opt_signature),
2440 m = class_resolveclassmethod(mainclass,
2441 utf_new_char(opt_method),
2448 vm_abort("vm_compile_method: java.lang.NoSuchMethodException: %s.%s",
2449 opt_method, opt_signature ? opt_signature : "");
2453 #endif /* !defined(NDEBUG) */
2456 /* vm_array_store_int **********************************************************
2458 Helper function to store an integer into the argument array, taking
2459 care of architecture specific issues.
2461 *******************************************************************************/
2463 static void vm_array_store_int(uint64_t *array, paramdesc *pd, int32_t value)
2467 if (!pd->inmemory) {
2469 array[index] = (int64_t) value;
2472 index = ARG_CNT + pd->index;
2473 #if SIZEOF_VOID_P == 8
2474 array[index] = (int64_t) value;
2476 # if WORDS_BIGENDIAN == 1
2477 array[index] = ((int64_t) value) << 32;
2479 array[index] = (int64_t) value;
2486 /* vm_array_store_lng **********************************************************
2488 Helper function to store a long into the argument array, taking
2489 care of architecture specific issues.
2491 *******************************************************************************/
2493 static void vm_array_store_lng(uint64_t *array, paramdesc *pd, int64_t value)
2497 #if SIZEOF_VOID_P == 8
2501 index = ARG_CNT + pd->index;
2503 array[index] = value;
2505 if (!pd->inmemory) {
2506 /* move low and high 32-bits into it's own argument slot */
2508 index = GET_LOW_REG(pd->index);
2509 array[index] = value & 0x00000000ffffffff;
2511 index = GET_HIGH_REG(pd->index);
2512 array[index] = value >> 32;
2515 index = ARG_CNT + pd->index;
2516 array[index] = value;
2522 /* vm_array_store_flt **********************************************************
2524 Helper function to store a float into the argument array, taking
2525 care of architecture specific issues.
2527 *******************************************************************************/
2529 static void vm_array_store_flt(uint64_t *array, paramdesc *pd, uint64_t value)
2533 if (!pd->inmemory) {
2534 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2537 index = INT_ARG_CNT + pd->index;
2539 #if WORDS_BIGENDIAN == 1 && !defined(__POWERPC__) && !defined(__POWERPC64__) && !defined(__S390__)
2540 array[index] = value >> 32;
2542 array[index] = value;
2546 index = ARG_CNT + pd->index;
2547 #if defined(__SPARC_64__)
2548 array[index] = value >> 32;
2550 array[index] = value;
2556 /* vm_array_store_dbl **********************************************************
2558 Helper function to store a double into the argument array, taking
2559 care of architecture specific issues.
2561 *******************************************************************************/
2563 static void vm_array_store_dbl(uint64_t *array, paramdesc *pd, uint64_t value)
2567 if (!pd->inmemory) {
2568 #if SIZEOF_VOID_P != 8 && defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2569 index = GET_LOW_REG(pd->index);
2570 array[index] = value & 0x00000000ffffffff;
2572 index = GET_HIGH_REG(pd->index);
2573 array[index] = value >> 32;
2575 index = INT_ARG_CNT + pd->index;
2576 array[index] = value;
2580 index = ARG_CNT + pd->index;
2581 array[index] = value;
2586 /* vm_array_store_adr **********************************************************
2588 Helper function to store an address into the argument array, taking
2589 care of architecture specific issues.
2591 *******************************************************************************/
2593 static void vm_array_store_adr(uint64_t *array, paramdesc *pd, void *value)
2597 if (!pd->inmemory) {
2598 #if defined(HAS_ADDRESS_REGISTER_FILE)
2599 /* When the architecture has address registers, place them
2600 after integer and float registers. */
2602 index = INT_ARG_CNT + FLT_ARG_CNT + pd->index;
2606 array[index] = (uint64_t) (intptr_t) value;
2609 index = ARG_CNT + pd->index;
2610 #if SIZEOF_VOID_P == 8
2611 array[index] = (uint64_t) (intptr_t) value;
2613 # if WORDS_BIGENDIAN == 1
2614 array[index] = ((uint64_t) (intptr_t) value) << 32;
2616 array[index] = (uint64_t) (intptr_t) value;
2623 /* vm_array_from_valist ********************************************************
2627 *******************************************************************************/
2629 uint64_t *vm_array_from_valist(methodinfo *m, java_object_t *o, va_list ap)
2638 /* get the descriptors */
2642 td = md->paramtypes;
2644 /* allocate argument array */
2646 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2648 /* if method is non-static fill first block and skip `this' pointer */
2653 /* the `this' pointer */
2654 vm_array_store_adr(array, pd, o);
2661 for (; i < md->paramcount; i++, pd++, td++) {
2664 value.i = va_arg(ap, int32_t);
2665 vm_array_store_int(array, pd, value.i);
2669 value.l = va_arg(ap, int64_t);
2670 vm_array_store_lng(array, pd, value.l);
2674 #if defined(__ALPHA__) || defined(__POWERPC__) || defined(__POWERPC64__)
2675 /* This is required to load the correct float value in
2678 value.d = (double) va_arg(ap, double);
2680 value.f = (float) va_arg(ap, double);
2682 vm_array_store_flt(array, pd, value.l);
2686 value.d = va_arg(ap, double);
2687 vm_array_store_dbl(array, pd, value.l);
2691 value.a = va_arg(ap, void*);
2692 vm_array_store_adr(array, pd, value.a);
2701 /* vm_array_from_jvalue ********************************************************
2705 *******************************************************************************/
2707 static uint64_t *vm_array_from_jvalue(methodinfo *m, java_object_t *o,
2717 /* get the descriptors */
2721 td = md->paramtypes;
2723 /* allocate argument array */
2725 #if defined(HAS_ADDRESS_REGISTER_FILE)
2726 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + ADR_ARG_CNT + md->memuse);
2728 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2731 /* if method is non-static fill first block and skip `this' pointer */
2736 /* the `this' pointer */
2737 vm_array_store_adr(array, pd, o);
2744 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2745 switch (td->decltype) {
2747 vm_array_store_int(array, pd, args[j].i);
2751 vm_array_store_lng(array, pd, args[j].j);
2755 vm_array_store_flt(array, pd, args[j].j);
2759 vm_array_store_dbl(array, pd, args[j].j);
2763 vm_array_store_adr(array, pd, args[j].l);
2772 /* vm_array_from_objectarray ***************************************************
2776 *******************************************************************************/
2778 uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o,
2779 java_handle_objectarray_t *params)
2785 java_handle_t *param;
2792 /* get the descriptors */
2796 td = md->paramtypes;
2798 /* allocate argument array */
2800 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2802 /* if method is non-static fill first block and skip `this' pointer */
2808 vm_array_store_adr(array, pd, o);
2815 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2816 LLNI_objectarray_element_get(params, j, param);
2823 /* convert the value according to its declared type */
2825 c = param->vftbl->class;
2826 type = primitive_type_get_by_wrapperclass(c);
2828 switch (td->decltype) {
2829 case PRIMITIVETYPE_BOOLEAN:
2831 case PRIMITIVETYPE_BOOLEAN:
2832 /* This type is OK. */
2839 case PRIMITIVETYPE_BYTE:
2841 case PRIMITIVETYPE_BYTE:
2842 /* This type is OK. */
2849 case PRIMITIVETYPE_CHAR:
2851 case PRIMITIVETYPE_CHAR:
2852 /* This type is OK. */
2859 case PRIMITIVETYPE_SHORT:
2861 case PRIMITIVETYPE_BYTE:
2862 case PRIMITIVETYPE_SHORT:
2863 /* These types are OK. */
2870 case PRIMITIVETYPE_INT:
2872 case PRIMITIVETYPE_BYTE:
2873 case PRIMITIVETYPE_SHORT:
2874 case PRIMITIVETYPE_INT:
2875 /* These types are OK. */
2883 vm_abort("vm_array_from_objectarray: invalid type %d",
2887 value = primitive_unbox(param);
2888 vm_array_store_int(array, pd, value.i);
2895 c = param->vftbl->class;
2896 type = primitive_type_get_by_wrapperclass(c);
2898 assert(td->decltype == PRIMITIVETYPE_LONG);
2901 case PRIMITIVETYPE_BYTE:
2902 case PRIMITIVETYPE_SHORT:
2903 case PRIMITIVETYPE_INT:
2904 case PRIMITIVETYPE_LONG:
2905 /* These types are OK. */
2911 value = primitive_unbox(param);
2912 vm_array_store_lng(array, pd, value.l);
2919 c = param->vftbl->class;
2920 type = primitive_type_get_by_wrapperclass(c);
2922 assert(td->decltype == PRIMITIVETYPE_FLOAT);
2925 case PRIMITIVETYPE_FLOAT:
2926 /* This type is OK. */
2932 value = primitive_unbox(param);
2933 vm_array_store_flt(array, pd, value.l);
2940 c = param->vftbl->class;
2941 type = primitive_type_get_by_wrapperclass(c);
2943 assert(td->decltype == PRIMITIVETYPE_DOUBLE);
2946 case PRIMITIVETYPE_FLOAT:
2947 case PRIMITIVETYPE_DOUBLE:
2948 /* These types are OK. */
2954 value = primitive_unbox(param);
2955 vm_array_store_dbl(array, pd, value.l);
2959 if (!resolve_class_from_typedesc(td, true, true, &c))
2962 if (param != NULL) {
2963 if (td->arraydim > 0) {
2964 if (!builtin_arrayinstanceof(param, c))
2968 if (!builtin_instanceof(param, c))
2973 vm_array_store_adr(array, pd, param);
2977 vm_abort("vm_array_from_objectarray: invalid type %d", td->type);
2984 exceptions_throw_illegalargumentexception();
2989 /* vm_call_method **************************************************************
2991 Calls a Java method with a variable number of arguments.
2993 *******************************************************************************/
2995 #define VM_CALL_METHOD(name, type) \
2996 type vm_call_method##name(methodinfo *m, java_handle_t *o, ...) \
3002 value = vm_call_method##name##_valist(m, o, ap); \
3008 VM_CALL_METHOD(, java_handle_t *)
3009 VM_CALL_METHOD(_int, int32_t)
3010 VM_CALL_METHOD(_long, int64_t)
3011 VM_CALL_METHOD(_float, float)
3012 VM_CALL_METHOD(_double, double)
3015 /* vm_call_method_valist *******************************************************
3017 Calls a Java method with a variable number of arguments, passed via
3020 *******************************************************************************/
3022 #define VM_CALL_METHOD_VALIST(name, type) \
3023 type vm_call_method##name##_valist(methodinfo *m, java_handle_t *o, \
3030 dumpsize = dump_size(); \
3031 array = vm_array_from_valist(m, o, ap); \
3032 value = vm_call##name##_array(m, array); \
3033 dump_release(dumpsize); \
3038 VM_CALL_METHOD_VALIST(, java_handle_t *)
3039 VM_CALL_METHOD_VALIST(_int, int32_t)
3040 VM_CALL_METHOD_VALIST(_long, int64_t)
3041 VM_CALL_METHOD_VALIST(_float, float)
3042 VM_CALL_METHOD_VALIST(_double, double)
3045 /* vm_call_method_jvalue *******************************************************
3047 Calls a Java method with a variable number of arguments, passed via
3050 *******************************************************************************/
3052 #define VM_CALL_METHOD_JVALUE(name, type) \
3053 type vm_call_method##name##_jvalue(methodinfo *m, java_handle_t *o, \
3054 const jvalue *args) \
3060 dumpsize = dump_size(); \
3061 array = vm_array_from_jvalue(m, o, args); \
3062 value = vm_call##name##_array(m, array); \
3063 dump_release(dumpsize); \
3068 VM_CALL_METHOD_JVALUE(, java_handle_t *)
3069 VM_CALL_METHOD_JVALUE(_int, int32_t)
3070 VM_CALL_METHOD_JVALUE(_long, int64_t)
3071 VM_CALL_METHOD_JVALUE(_float, float)
3072 VM_CALL_METHOD_JVALUE(_double, double)
3075 /* vm_call_array ***************************************************************
3077 Calls a Java method with a variable number of arguments, passed via
3080 *******************************************************************************/
3082 #define VM_CALL_ARRAY(name, type) \
3083 type vm_call##name##_array(methodinfo *m, uint64_t *array) \
3089 md = m->parseddesc; \
3091 if (m->code == NULL) \
3092 if (!jit_compile(m)) \
3095 pv = m->code->entrypoint; \
3097 STATISTICS(count_calls_native_to_java++); \
3099 value = asm_vm_call_method##name(pv, array, md->memuse); \
3104 VM_CALL_ARRAY(, java_handle_t *)
3105 VM_CALL_ARRAY(_int, int32_t)
3106 VM_CALL_ARRAY(_long, int64_t)
3107 VM_CALL_ARRAY(_float, float)
3108 VM_CALL_ARRAY(_double, double)
3112 * These are local overrides for various environment variables in Emacs.
3113 * Please do not remove this and leave it at the end of the file, where
3114 * Emacs will automagically detect them.
3115 * ---------------------------------------------------------------------
3118 * indent-tabs-mode: t