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 8351 2007-08-19 17:56:23Z 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/include/java_lang_Byte.h"
67 #include "native/include/java_lang_Character.h"
68 #include "native/include/java_lang_Short.h"
69 #include "native/include/java_lang_Integer.h"
70 #include "native/include/java_lang_Boolean.h"
71 #include "native/include/java_lang_Long.h"
72 #include "native/include/java_lang_Float.h"
73 #include "native/include/java_lang_Double.h"
75 #include "native/vm/nativevm.h"
77 #include "threads/threads-common.h"
79 #include "toolbox/logging.h"
81 #include "vm/builtin.h"
82 #include "vm/exceptions.h"
83 #include "vm/finalizer.h"
84 #include "vm/global.h"
85 #include "vm/initialize.h"
86 #include "vm/primitive.h"
87 #include "vm/properties.h"
88 #include "vm/signallocal.h"
89 #include "vm/stringlocal.h"
92 #include "vm/jit/jit.h"
93 #include "vm/jit/md.h"
94 #include "vm/jit/asmpart.h"
96 #if defined(ENABLE_PROFILING)
97 # include "vm/jit/optimizing/profile.h"
100 #include "vm/jit/optimizing/recompile.h"
102 #include "vmcore/classcache.h"
103 #include "vmcore/options.h"
104 #include "vmcore/statistics.h"
105 #include "vmcore/suck.h"
107 #if defined(ENABLE_JVMTI)
108 # include "native/jvmti/cacaodbg.h"
111 #if defined(ENABLE_VMLOG)
112 #include <vmlog_cacao.h>
116 /* Invocation API variables ***************************************************/
118 _Jv_JavaVM *_Jv_jvm; /* denotes a Java VM */
119 _Jv_JNIEnv *_Jv_env; /* pointer to native method interface */
122 /* global variables ***********************************************************/
124 s4 vms = 0; /* number of VMs created */
126 bool vm_initializing = false;
127 bool vm_exiting = false;
129 char *cacao_prefix = NULL;
130 char *cacao_libjvm = NULL;
131 char *classpath_libdir = NULL;
133 char *_Jv_bootclasspath; /* contains the boot classpath */
134 char *_Jv_classpath; /* contains the classpath */
135 char *_Jv_java_library_path;
137 char *mainstring = NULL;
138 classinfo *mainclass = NULL;
140 char *specificmethodname = NULL;
141 char *specificsignature = NULL;
145 #if defined(ENABLE_INTRP)
146 u1 *intrp_main_stack = NULL;
150 /* define heap sizes **********************************************************/
152 #define HEAP_MAXSIZE 128 * 1024 * 1024 /* default 128MB */
153 #define HEAP_STARTSIZE 2 * 1024 * 1024 /* default 2MB */
154 #define STACK_SIZE 64 * 1024 /* default 64kB */
157 /* define command line options ************************************************/
188 /* Java non-standard options */
199 #if defined(ENABLE_PROFILING)
212 #if defined(ENABLE_STATISTICS)
229 #if defined(ENABLE_VERIFIER)
231 #if defined(TYPECHECK_VERBOSE)
234 #endif /* defined(ENABLE_VERIFIER) */
236 /* optimization options */
238 #if defined(ENABLE_LOOP)
242 #if defined(ENABLE_IFCONV)
246 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
250 #if defined(ENABLE_INLINING)
255 #if defined(ENABLE_INLINING_DEBUG)
256 OPT_INLINE_DEBUG_ALL,
257 OPT_INLINE_DEBUG_END,
258 OPT_INLINE_DEBUG_MIN,
259 OPT_INLINE_DEBUG_MAX,
260 #endif /* defined(ENABLE_INLINING_DEBUG) */
261 #endif /* defined(ENABLE_INLINING) */
263 #if defined(ENABLE_INTRP)
264 /* interpreter options */
283 #if defined(ENABLE_DEBUG_FILTER)
284 OPT_FILTER_VERBOSECALL_INCLUDE,
285 OPT_FILTER_VERBOSECALL_EXCLUDE,
286 OPT_FILTER_SHOW_METHOD,
293 opt_struct opts[] = {
294 { "foo", false, OPT_FOO },
298 { "jar", false, OPT_JAR },
300 { "d32", false, OPT_D32 },
301 { "d64", false, OPT_D64 },
302 { "client", false, OPT_IGNORE },
303 { "server", false, OPT_IGNORE },
304 { "jvm", false, OPT_IGNORE },
305 { "hotspot", false, OPT_IGNORE },
307 { "classpath", true, OPT_CLASSPATH },
308 { "cp", true, OPT_CLASSPATH },
309 { "D", true, OPT_D },
310 { "version", false, OPT_VERSION },
311 { "showversion", false, OPT_SHOWVERSION },
312 { "fullversion", false, OPT_FULLVERSION },
313 { "help", false, OPT_HELP },
314 { "?", false, OPT_HELP },
315 { "X", false, OPT_X },
316 { "XX:", true, OPT_XX },
317 { "XX", false, OPT_XX },
319 { "ea:", true, OPT_EA },
320 { "da:", true, OPT_DA },
321 { "ea", false, OPT_EA },
322 { "da", false, OPT_DA },
324 { "esa", false, OPT_ESA },
325 { "enablesystemassertions", false, OPT_ESA },
326 { "dsa", false, OPT_DSA },
327 { "disablesystemassertions", false, OPT_DSA },
329 { "noasyncgc", false, OPT_IGNORE },
330 #if defined(ENABLE_VERIFIER)
331 { "noverify", false, OPT_NOVERIFY },
332 { "Xverify:none", false, OPT_NOVERIFY },
334 { "v", false, OPT_VERBOSE1 },
335 { "verbose:", true, OPT_VERBOSE },
337 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
338 { "verbosetc", false, OPT_VERBOSETC },
340 #if defined(__ALPHA__)
341 { "noieee", false, OPT_NOIEEE },
343 #if defined(ENABLE_STATISTICS)
344 { "time", false, OPT_TIME },
345 { "stat", false, OPT_STAT },
347 { "log", true, OPT_LOG },
348 { "c", true, OPT_CHECK },
349 { "l", false, OPT_LOAD },
352 { "all", false, OPT_ALL },
353 { "sig", true, OPT_SIGNATURE },
356 #if defined(ENABLE_LOOP)
357 { "oloop", false, OPT_OLOOP },
359 #if defined(ENABLE_IFCONV)
360 { "ifconv", false, OPT_IFCONV },
362 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
363 { "lsra", false, OPT_LSRA },
366 #if defined(ENABLE_INTRP)
367 /* interpreter options */
369 { "trace", false, OPT_TRACE },
370 { "static-supers", true, OPT_STATIC_SUPERS },
371 { "no-dynamic", false, OPT_NO_DYNAMIC },
372 { "no-replication", false, OPT_NO_REPLICATION },
373 { "no-quicksuper", false, OPT_NO_QUICKSUPER },
376 /* JVMTI Agent Command Line Options */
378 { "agentlib:", true, OPT_AGENTLIB },
379 { "agentpath:", true, OPT_AGENTPATH },
382 /* Java non-standard options */
384 { "Xjit", false, OPT_JIT },
385 { "Xint", false, OPT_INTRP },
386 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
387 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
388 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
389 { "Xbootclasspath/c:", true, OPT_BOOTCLASSPATH_C },
392 { "Xdebug", false, OPT_DEBUG },
393 { "Xnoagent", false, OPT_NOAGENT },
394 { "Xrunjdwp", true, OPT_XRUNJDWP },
397 { "Xms", true, OPT_MS },
398 { "ms", true, OPT_MS },
399 { "Xmx", true, OPT_MX },
400 { "mx", true, OPT_MX },
401 { "Xss", true, OPT_SS },
402 { "ss", true, OPT_SS },
404 #if defined(ENABLE_PROFILING)
405 { "Xprof:", true, OPT_PROF_OPTION },
406 { "Xprof", false, OPT_PROF },
409 /* inlining options */
411 #if defined(ENABLE_INLINING)
412 #if defined(ENABLE_INLINING_DEBUG)
413 { "ia", false, OPT_INLINE_DEBUG_ALL },
414 { "ii", true, OPT_INLINE_DEBUG_MIN },
415 { "im", true, OPT_INLINE_DEBUG_MAX },
416 { "ie", true, OPT_INLINE_DEBUG_END },
417 #endif /* defined(ENABLE_INLINING_DEBUG) */
419 { "il", false, OPT_INLINE_LOG },
421 { "i", false, OPT_INLINING },
422 #endif /* defined(ENABLE_INLINING) */
424 /* keep these at the end of the list */
427 { "m", true, OPT_METHOD },
430 { "s", true, OPT_SHOW },
431 { "debug-color", false, OPT_DEBUGCOLOR },
433 #if defined(ENABLE_DEBUG_FILTER)
434 { "XXfi", true, OPT_FILTER_VERBOSECALL_INCLUDE },
435 { "XXfx", true, OPT_FILTER_VERBOSECALL_EXCLUDE },
436 { "XXfm", true, OPT_FILTER_SHOW_METHOD },
443 /* usage ***********************************************************************
445 Prints the correct usage syntax to stdout.
447 *******************************************************************************/
451 puts("Usage: cacao [-options] classname [arguments]");
452 puts(" (to run a class file)");
453 puts(" or cacao [-options] -jar jarfile [arguments]");
454 puts(" (to run a standalone jar file)\n");
456 puts("where options include:");
457 puts(" -d32 use 32-bit data model if available");
458 puts(" -d64 use 64-bit data model if available");
459 puts(" -client compatibility (currently ignored)");
460 puts(" -server compatibility (currently ignored)");
461 puts(" -jvm compatibility (currently ignored)");
462 puts(" -hotspot compatibility (currently ignored)\n");
464 puts(" -cp <path> specify a path to look for classes");
465 puts(" -classpath <path> specify a path to look for classes");
466 puts(" -D<name>=<value> add an entry to the property list");
467 puts(" -verbose[:class|gc|jni] enable specific verbose output");
468 puts(" -version print product version and exit");
469 puts(" -fullversion print jpackage-compatible product version and exit");
470 puts(" -showversion print product version and continue");
471 puts(" -help, -? print this help message");
472 puts(" -X print help on non-standard Java options");
473 puts(" -XX print help on debugging options");
474 puts(" -ea[:<packagename>...|:<classname>]");
475 puts(" -enableassertions[:<packagename>...|:<classname>]");
476 puts(" enable assertions with specified granularity");
477 puts(" -da[:<packagename>...|:<classname>]");
478 puts(" -disableassertions[:<packagename>...|:<classname>]");
479 puts(" disable assertions with specified granularity");
480 puts(" -esa | -enablesystemassertions");
481 puts(" enable system assertions");
482 puts(" -dsa | -disablesystemassertions");
483 puts(" disable system assertions");
486 puts(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent");
487 puts (" for jdwp help use: -agentlib:jdwp=help");
488 puts(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent");
491 /* exit with error code */
497 static void Xusage(void)
499 #if defined(ENABLE_JIT)
500 puts(" -Xjit JIT mode execution (default)");
502 #if defined(ENABLE_INTRP)
503 puts(" -Xint interpreter mode execution");
505 puts(" -Xbootclasspath:<zip/jar files and directories separated by :>");
506 puts(" value is set as bootstrap class path");
507 puts(" -Xbootclasspath/a:<zip/jar files and directories separated by :>");
508 puts(" value is appended to the bootstrap class path");
509 puts(" -Xbootclasspath/p:<zip/jar files and directories separated by :>");
510 puts(" value is prepended to the bootstrap class path");
511 puts(" -Xbootclasspath/c:<zip/jar files and directories separated by :>");
512 puts(" value is used as Java core library, but the");
513 puts(" hardcoded VM interface classes are prepended");
514 printf(" -Xms<size> set the initial size of the heap (default: %dMB)\n", HEAP_STARTSIZE / 1024 / 1024);
515 printf(" -Xmx<size> set the maximum size of the heap (default: %dMB)\n", HEAP_MAXSIZE / 1024 / 1024);
516 printf(" -Xss<size> set the thread stack size (default: %dkB)\n", STACK_SIZE / 1024);
518 #if defined(ENABLE_PROFILING)
519 puts(" -Xprof[:bb] collect and print profiling data");
522 #if defined(ENABLE_JVMTI)
523 /* -Xdebug option depend on gnu classpath JDWP options. options:
524 transport=dt_socket,address=<hostname:port>,server=(y|n),suspend(y|n) */
525 puts(" -Xdebug enable remote debugging\n");
526 puts(" -Xrunjdwp transport=[dt_socket|...],address=<hostname:port>,server=[y|n],suspend=[y|n]\n");
527 puts(" enable remote debugging\n");
530 /* exit with error code */
537 static void XXusage(void)
539 puts(" -v write state-information");
541 puts(" -verbose[:jit|threads]");
542 puts(" enable specific verbose output");
543 puts(" -debug-color colored output for ANSI terms");
545 #ifdef TYPECHECK_VERBOSE
546 puts(" -verbosetc write debug messages while typechecking");
548 #if defined(__ALPHA__)
549 puts(" -noieee don't use ieee compliant arithmetic");
551 #if defined(ENABLE_VERIFIER)
552 puts(" -noverify don't verify classfiles");
554 #if defined(ENABLE_STATISTICS)
555 puts(" -time measure the runtime");
556 puts(" -stat detailed compiler statistics");
558 puts(" -log logfile specify a name for the logfile");
559 puts(" -c(heck)b(ounds) don't check array bounds");
560 puts(" s(ync) don't check for synchronization");
561 #if defined(ENABLE_LOOP)
562 puts(" -oloop optimize array accesses in loops");
564 puts(" -l don't start the class after loading");
566 puts(" -all compile all methods, no execution");
567 puts(" -m compile only a specific method");
568 puts(" -sig specify signature for a specific method");
571 puts(" -s... show...");
572 puts(" (c)onstants the constant pool");
573 puts(" (m)ethods class fields and methods");
574 puts(" (u)tf the utf - hash");
575 puts(" (i)ntermediate intermediate representation");
576 #if defined(ENABLE_DISASSEMBLER)
577 puts(" (a)ssembler disassembled listing");
578 puts(" n(o)ps show NOPs in disassembler output");
579 puts(" (e)xceptionstubs disassembled exception stubs (only with -sa)");
580 puts(" (n)ative disassembled native stubs");
582 puts(" (d)atasegment data segment listing");
584 #if defined(ENABLE_INLINING)
585 puts(" -i activate inlining");
587 puts(" -il log inlining");
589 #if defined(ENABLE_INLINING_DEBUG)
590 puts(" -ia use inlining for all methods");
591 puts(" -ii <size> set minimum size for inlined result");
592 puts(" -im <size> set maximum size for inlined result");
593 puts(" -ie <number> stop inlining after the given number of roots");
594 #endif /* defined(ENABLE_INLINING_DEBUG) */
595 #endif /* defined(ENABLE_INLINING) */
597 #if defined(ENABLE_IFCONV)
598 puts(" -ifconv use if-conversion");
600 #if defined(ENABLE_LSRA)
601 puts(" -lsra use linear scan register allocation");
603 #if defined(ENABLE_SSA)
604 puts(" -lsra use linear scan register allocation (with SSA)");
606 #if defined(ENABLE_DEBUG_FILTER)
607 puts(" -XXfi <regex> begin of dynamic scope for verbosecall filter");
608 puts(" -XXfx <regex> end of dynamic scope for verbosecall filter");
609 puts(" -XXfm <regex> filter for show options");
611 /* exit with error code */
618 /* version *********************************************************************
620 Only prints cacao version information.
622 *******************************************************************************/
624 static void version(bool opt_exit)
626 puts("java version \""JAVA_VERSION"\"");
627 puts("CACAO version "VERSION"");
629 puts("Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,");
630 puts("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,");
631 puts("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,");
632 puts("J. Wenninger, Institut f. Computersprachen - TU Wien\n");
634 puts("This program is free software; you can redistribute it and/or");
635 puts("modify it under the terms of the GNU General Public License as");
636 puts("published by the Free Software Foundation; either version 2, or (at");
637 puts("your option) any later version.\n");
639 puts("This program is distributed in the hope that it will be useful, but");
640 puts("WITHOUT ANY WARRANTY; without even the implied warranty of");
641 puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU");
642 puts("General Public License for more details.");
644 /* exit normally, if requested */
651 /* fullversion *****************************************************************
653 Prints a Sun compatible version information (required e.g. by
654 jpackage, www.jpackage.org).
656 *******************************************************************************/
658 static void fullversion(void)
660 puts("java full version \"cacao-"JAVA_VERSION"\"");
668 void vm_printconfig(void)
670 puts("Configure/Build options:\n");
671 puts(" ./configure: "VERSION_CONFIGURE_ARGS"");
672 #if defined(__VERSION__)
673 puts(" CC : "VERSION_CC" ("__VERSION__")");
675 puts(" CC : "VERSION_CC"");
677 puts(" CFLAGS : "VERSION_CFLAGS"\n");
679 puts("Default variables:\n");
680 printf(" maximum heap size : %d\n", HEAP_MAXSIZE);
681 printf(" initial heap size : %d\n", HEAP_STARTSIZE);
682 printf(" stack size : %d\n", STACK_SIZE);
683 #if defined(WITH_CLASSPATH_GNU)
684 puts(" java.boot.class.path : "CACAO_VM_ZIP":"CLASSPATH_CLASSES"");
686 puts(" java.boot.class.path : "CLASSPATH_CLASSES"");
688 puts(" gnu.classpath.boot.library.path: "CLASSPATH_LIBDIR"/classpath\n");
690 puts("Runtime variables:\n");
691 printf(" maximum heap size : %d\n", opt_heapmaxsize);
692 printf(" initial heap size : %d\n", opt_heapstartsize);
693 printf(" stack size : %d\n", opt_stacksize);
694 printf(" libjvm.so : %s\n", cacao_libjvm);
695 printf(" java.boot.class.path : %s\n", _Jv_bootclasspath);
696 printf(" gnu.classpath.boot.library.path: %s\n", classpath_libdir);
697 printf(" java.class.path : %s\n", _Jv_classpath);
701 /* forward declarations *******************************************************/
703 static char *vm_get_mainclass_from_jar(char *mainstring);
705 static void vm_compile_all(void);
706 static void vm_compile_method(void);
710 /* vm_createjvm ****************************************************************
712 Implementation for JNI_CreateJavaVM.
714 *******************************************************************************/
716 bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
718 JavaVMInitArgs *_vm_args;
722 /* get the arguments for the new JVM */
724 _vm_args = (JavaVMInitArgs *) vm_args;
726 /* get the VM and Env tables (must be set before vm_create) */
728 env = NEW(_Jv_JNIEnv);
730 #if defined(ENABLE_JNI)
731 env->env = &_Jv_JNINativeInterface;
734 /* XXX Set the global variable. Maybe we should do that differently. */
738 /* create and fill a JavaVM structure */
740 vm = NEW(_Jv_JavaVM);
742 #if defined(ENABLE_JNI)
743 vm->functions = &_Jv_JNIInvokeInterface;
746 /* XXX Set the global variable. Maybe we should do that differently. */
747 /* XXX JVMTI Agents needs a JavaVM */
751 /* actually create the JVM */
753 if (!vm_create(_vm_args))
756 #if defined(ENABLE_JNI)
757 /* setup the local ref table (must be created after vm_create) */
759 if (!localref_table_init())
763 /* now return the values */
765 *p_vm = (JavaVM *) vm;
766 *p_env = (void *) env;
771 /* release allocated memory */
773 FREE(env, _Jv_JNIEnv);
774 FREE(vm, _Jv_JavaVM);
780 /* vm_create *******************************************************************
782 Creates a JVM. Called by vm_createjvm.
784 *******************************************************************************/
786 bool vm_create(JavaVMInitArgs *vm_args)
795 #if defined(ENABLE_JVMTI)
797 char *libname, *agentarg;
798 bool jdwp,agentbypath;
799 jdwp = agentbypath = false;
802 #if defined(ENABLE_VMLOG)
803 vmlog_cacao_init(vm_args);
806 /* check the JNI version requested */
808 switch (vm_args->version) {
809 case JNI_VERSION_1_1:
811 case JNI_VERSION_1_2:
812 case JNI_VERSION_1_4:
818 /* we only support 1 JVM instance */
823 if (atexit(vm_exit_handler))
824 vm_abort("atexit failed: %s\n", strerror(errno));
827 log_text("CACAO started -------------------------------------------------------");
829 /* We need to check if the actual size of a java.lang.Class object
830 is smaller or equal than the assumption made in
831 src/vmcore/class.h. */
833 if (sizeof(java_lang_Class) > sizeof(dummy_java_lang_Class))
834 vm_abort("vm_create: java_lang_Class structure is bigger than classinfo.object (%d > %d)", sizeof(java_lang_Class), sizeof(dummy_java_lang_Class));
836 /* set the VM starttime */
838 _Jv_jvm->starttime = builtin_currenttimemillis();
840 /* get stuff from the environment *****************************************/
842 #if defined(WITH_JRE_LAYOUT)
843 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
845 cacao_prefix = MNEW(char, 4096);
847 if (readlink("/proc/self/exe", cacao_prefix, 4095) == -1)
848 vm_abort("readlink failed: %s\n", strerror(errno));
850 /* Get the path of the current executable. */
852 cacao_prefix = dirname(cacao_prefix);
854 if ((strlen(cacao_prefix) + strlen("/..") + strlen("0")) > 4096)
855 vm_abort("libjvm name to long for buffer\n");
857 /* Walk up one directory. */
859 strcat(cacao_prefix, "/..");
861 # if defined(WITH_CLASSPATH_GNU)
863 /* Set path to libjvm.so. */
866 strlen(cacao_prefix) + strlen("/lib/libjvm") +
869 cacao_libjvm = MNEW(char, len);
871 strcpy(cacao_libjvm, cacao_prefix);
872 strcat(cacao_libjvm, "/lib/libjvm");
874 /* Set the path to Java core native libraries. */
877 strlen(cacao_prefix) + strlen("/lib/classpath") +
880 classpath_libdir = MNEW(char, len);
882 strcpy(classpath_libdir, cacao_prefix);
883 strcat(classpath_libdir, "/lib/classpath");
885 # elif defined(WITH_CLASSPATH_SUN)
887 /* Set path to libjvm.so. */
890 strlen(cacao_prefix) + strlen("/lib/"JAVA_ARCH"/server/libjvm") +
893 cacao_libjvm = MNEW(char, len);
895 strcpy(cacao_libjvm, cacao_prefix);
896 strcat(cacao_libjvm, "/lib/"JAVA_ARCH"/server/libjvm");
898 /* Set the path to Java core native libraries. */
901 strlen(cacao_prefix) + strlen("/lib/"JAVA_ARCH) +
904 classpath_libdir = MNEW(char, len);
906 strcpy(classpath_libdir, cacao_prefix);
907 strcat(classpath_libdir, "/lib/"JAVA_ARCH);
910 # error unknown classpath configuration
913 cacao_prefix = CACAO_PREFIX;
914 cacao_libjvm = CACAO_LIBDIR"/libjvm";
916 # if defined(WITH_CLASSPATH_GNU)
917 classpath_libdir = CLASSPATH_LIBDIR"/classpath";
919 classpath_libdir = CLASSPATH_LIBDIR;
923 /* Set the bootclasspath. */
925 cp = getenv("BOOTCLASSPATH");
928 _Jv_bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
929 strcpy(_Jv_bootclasspath, cp);
932 #if defined(WITH_JRE_LAYOUT)
933 # if defined(WITH_CLASSPATH_GNU)
936 strlen(cacao_prefix) + strlen("/share/cacao/vm.zip:") +
937 strlen(cacao_prefix) + strlen("/share/classpath/glibj.zip") +
940 _Jv_bootclasspath = MNEW(char, len);
942 strcpy(_Jv_bootclasspath, cacao_prefix);
943 strcat(_Jv_bootclasspath, "/share/cacao/vm.zip");
944 strcat(_Jv_bootclasspath, ":");
945 strcat(_Jv_bootclasspath, cacao_prefix);
946 strcat(_Jv_bootclasspath, "/share/classpath/glibj.zip");
948 # elif defined(WITH_CLASSPATH_SUN)
950 /* This is the bootclasspath taken from HotSpot (see
951 hotspot/src/share/vm/runtime/os.cpp
952 (os::set_boot_path)). */
955 strlen(cacao_prefix) + strlen("/lib/resources.jar:") +
956 strlen(cacao_prefix) + strlen("/lib/rt.jar:") +
957 strlen(cacao_prefix) + strlen("/lib/sunrsasign.jar:") +
958 strlen(cacao_prefix) + strlen("/lib/jsse.jar:") +
959 strlen(cacao_prefix) + strlen("/lib/jce.jar:") +
960 strlen(cacao_prefix) + strlen("/lib/charsets.jar:") +
961 strlen(cacao_prefix) + strlen("/classes") +
964 _Jv_bootclasspath = MNEW(char, len);
966 strcpy(_Jv_bootclasspath, cacao_prefix);
967 strcat(_Jv_bootclasspath, "/lib/resources.jar:");
968 strcat(_Jv_bootclasspath, cacao_prefix);
969 strcat(_Jv_bootclasspath, "/lib/rt.jar:");
970 strcat(_Jv_bootclasspath, cacao_prefix);
971 strcat(_Jv_bootclasspath, "/lib/sunrsasign.jar:");
972 strcat(_Jv_bootclasspath, cacao_prefix);
973 strcat(_Jv_bootclasspath, "/lib/jsse.jar:");
974 strcat(_Jv_bootclasspath, cacao_prefix);
975 strcat(_Jv_bootclasspath, "/lib/jce.jar:");
976 strcat(_Jv_bootclasspath, cacao_prefix);
977 strcat(_Jv_bootclasspath, "/lib/charsets.jar:");
978 strcat(_Jv_bootclasspath, cacao_prefix);
979 strcat(_Jv_bootclasspath, "/classes");
982 # error unknown classpath configuration
985 # if defined(WITH_CLASSPATH_GNU)
988 strlen(CACAO_VM_ZIP) +
990 strlen(CLASSPATH_CLASSES) +
993 _Jv_bootclasspath = MNEW(char, len);
995 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
996 strcat(_Jv_bootclasspath, ":");
997 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
999 # elif defined(WITH_CLASSPATH_SUN)
1001 /* This is the bootclasspath taken from HotSpot (see
1002 hotspot/src/share/vm/runtime/os.cpp
1003 (os::set_boot_path)). */
1006 strlen(CLASSPATH_PREFIX"/lib/resources.jar:") +
1007 strlen(CLASSPATH_PREFIX"/lib/rt.jar:") +
1008 strlen(CLASSPATH_PREFIX"/lib/sunrsasign.jar:") +
1009 strlen(CLASSPATH_PREFIX"/lib/jsse.jar:") +
1010 strlen(CLASSPATH_PREFIX"/lib/jce.jar:") +
1011 strlen(CLASSPATH_PREFIX"/lib/charsets.jar:") +
1012 strlen(CLASSPATH_PREFIX"/classes") +
1015 _Jv_bootclasspath = MNEW(char, len);
1017 strcpy(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/resources.jar:");
1018 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/rt.jar:");
1019 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/sunrsasign.jar:");
1020 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/jsse.jar:");
1021 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/jce.jar:");
1022 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/lib/charsets.jar:");
1023 strcat(_Jv_bootclasspath, CLASSPATH_PREFIX"/classes");
1025 # elif defined(WITH_CLASSPATH_CLDC1_1)
1028 strlen(CLASSPATH_CLASSES) +
1031 _Jv_bootclasspath = MNEW(char, len);
1033 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
1036 # error unknown classpath configuration
1041 /* Set the classpath. */
1043 cp = getenv("CLASSPATH");
1046 _Jv_classpath = MNEW(char, strlen(cp) + strlen("0"));
1047 strcpy(_Jv_classpath, cp);
1050 _Jv_classpath = MNEW(char, strlen(".") + strlen("0"));
1051 strcpy(_Jv_classpath, ".");
1054 /* Get and set java.library.path. */
1056 _Jv_java_library_path = getenv("LD_LIBRARY_PATH");
1058 if (_Jv_java_library_path == NULL)
1059 _Jv_java_library_path = "";
1061 /* interpret the options **************************************************/
1063 opt_version = false;
1068 opt_heapmaxsize = HEAP_MAXSIZE;
1069 opt_heapstartsize = HEAP_STARTSIZE;
1070 opt_stacksize = STACK_SIZE;
1073 #if defined(ENABLE_JVMTI)
1074 /* initialize JVMTI related **********************************************/
1078 /* Initialize and fill properties before command-line handling. */
1080 if (!properties_init())
1081 vm_abort("vm_create: properties_init failed");
1083 /* Set the classpath properties. */
1085 #if defined(ENABLE_JAVASE)
1086 properties_add("java.boot.class.path", _Jv_bootclasspath);
1087 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1088 properties_add("java.class.path", _Jv_classpath);
1091 /* iterate over all passed options */
1093 while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
1107 #if SIZEOF_VOID_P == 8
1108 puts("Running a 32-bit JVM is not supported on this platform.");
1114 #if SIZEOF_VOID_P == 4
1115 puts("Running a 64-bit JVM is not supported on this platform.");
1121 /* forget old classpath and set the argument as new classpath */
1122 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1124 _Jv_classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
1125 strcpy(_Jv_classpath, opt_arg);
1127 #if defined(ENABLE_JAVASE)
1128 properties_add("java.class.path", _Jv_classpath);
1133 for (i = 0; i < strlen(opt_arg); i++) {
1134 if (opt_arg[i] == '=') {
1136 properties_add(opt_arg, opt_arg + i + 1);
1141 /* if no '=' is given, just create an empty property */
1143 properties_add(opt_arg, "");
1148 case OPT_BOOTCLASSPATH:
1149 /* Forget default bootclasspath and set the argument as
1150 new boot classpath. */
1152 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1154 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
1155 strcpy(_Jv_bootclasspath, opt_arg);
1157 #if defined(ENABLE_JAVASE)
1158 properties_add("java.boot.class.path", _Jv_bootclasspath);
1159 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1163 case OPT_BOOTCLASSPATH_A:
1164 /* append to end of bootclasspath */
1166 len = strlen(_Jv_bootclasspath);
1168 _Jv_bootclasspath = MREALLOC(_Jv_bootclasspath,
1172 strlen(opt_arg) + strlen("0"));
1174 strcat(_Jv_bootclasspath, ":");
1175 strcat(_Jv_bootclasspath, opt_arg);
1177 #if defined(ENABLE_JAVASE)
1178 properties_add("java.boot.class.path", _Jv_bootclasspath);
1179 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1183 case OPT_BOOTCLASSPATH_P:
1184 /* prepend in front of bootclasspath */
1186 cp = _Jv_bootclasspath;
1189 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
1192 strcpy(_Jv_bootclasspath, opt_arg);
1193 strcat(_Jv_bootclasspath, ":");
1194 strcat(_Jv_bootclasspath, cp);
1196 MFREE(cp, char, len);
1198 #if defined(ENABLE_JAVASE)
1199 properties_add("java.boot.class.path", _Jv_bootclasspath);
1200 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1204 case OPT_BOOTCLASSPATH_C:
1205 /* use as Java core library, but prepend VM interface classes */
1207 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1209 len = strlen(CACAO_VM_ZIP) +
1214 _Jv_bootclasspath = MNEW(char, len);
1216 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
1217 strcat(_Jv_bootclasspath, ":");
1218 strcat(_Jv_bootclasspath, opt_arg);
1220 #if defined(ENABLE_JAVASE)
1221 properties_add("java.boot.class.path", _Jv_bootclasspath);
1222 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1226 #if defined(ENABLE_JVMTI)
1228 /* this option exists only for compatibility reasons */
1232 /* I don't know yet what Xnoagent should do. This is only for
1233 compatiblity with eclipse - motse */
1242 strlen(CACAO_LIBDIR) +
1243 strlen("/libjdwp.so=") +
1247 agentarg = MNEW(char, len);
1249 strcpy(agentarg, CACAO_LIBDIR);
1250 strcat(agentarg, "/libjdwp.so=");
1251 strcat(agentarg, &opt_arg[1]);
1268 c = opt_arg[strlen(opt_arg) - 1];
1270 if ((c == 'k') || (c == 'K')) {
1271 j = atoi(opt_arg) * 1024;
1273 } else if ((c == 'm') || (c == 'M')) {
1274 j = atoi(opt_arg) * 1024 * 1024;
1280 opt_heapmaxsize = j;
1281 else if (opt == OPT_MS)
1282 opt_heapstartsize = j;
1293 if (strcmp("class", opt_arg) == 0) {
1294 opt_verboseclass = true;
1296 else if (strcmp("gc", opt_arg) == 0) {
1297 opt_verbosegc = true;
1299 else if (strcmp("jni", opt_arg) == 0) {
1300 opt_verbosejni = true;
1302 #if !defined(NDEBUG)
1303 else if (strcmp("jit", opt_arg) == 0) {
1307 compileverbose = true;
1309 else if (strcmp("threads", opt_arg) == 0) {
1310 opt_verbosethreads = true;
1314 printf("Unknown -verbose option: %s\n", opt_arg);
1319 case OPT_DEBUGCOLOR:
1320 opt_debugcolor = true;
1323 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
1325 opt_typecheckverbose = true;
1334 case OPT_FULLVERSION:
1338 case OPT_SHOWVERSION:
1346 #if defined(ENABLE_VERIFIER)
1352 #if defined(ENABLE_STATISTICS)
1354 opt_getcompilingtime = true;
1355 opt_getloadingtime = true;
1368 for (i = 0; i < strlen(opt_arg); i++) {
1369 switch (opt_arg[i]) {
1371 checkbounds = false;
1384 makeinitializations = false;
1387 #if !defined(NDEBUG)
1391 makeinitializations = false;
1396 opt_method = opt_arg;
1397 makeinitializations = false;
1401 opt_signature = opt_arg;
1405 case OPT_SHOW: /* Display options */
1406 for (i = 0; i < strlen(opt_arg); i++) {
1407 switch (opt_arg[i]) {
1409 showconstantpool = true;
1421 opt_showintermediate = true;
1422 compileverbose = true;
1425 #if defined(ENABLE_DISASSEMBLER)
1427 opt_showdisassemble = true;
1428 compileverbose = true;
1432 opt_shownops = true;
1436 opt_showexceptionstubs = true;
1440 opt_shownativestub = true;
1445 opt_showddatasegment = true;
1454 #if defined(ENABLE_LOOP)
1460 #if defined(ENABLE_INLINING)
1461 #if defined(ENABLE_INLINING_DEBUG)
1462 case OPT_INLINE_DEBUG_ALL:
1463 opt_inline_debug_all = true;
1465 case OPT_INLINE_DEBUG_END:
1466 opt_inline_debug_end_counter = atoi(opt_arg);
1468 case OPT_INLINE_DEBUG_MIN:
1469 opt_inline_debug_min_size = atoi(opt_arg);
1471 case OPT_INLINE_DEBUG_MAX:
1472 opt_inline_debug_max_size = atoi(opt_arg);
1474 #endif /* defined(ENABLE_INLINING_DEBUG) */
1475 #if !defined(NDEBUG)
1476 case OPT_INLINE_LOG:
1477 opt_inline_debug_log = true;
1479 #endif /* !defined(NDEBUG) */
1482 opt_inlining = true;
1484 #endif /* defined(ENABLE_INLINING) */
1486 #if defined(ENABLE_IFCONV)
1492 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1507 options_xx(opt_arg);
1511 /* currently ignored */
1515 /* currently ignored */
1519 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = true;
1523 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = false;
1526 #if defined(ENABLE_PROFILING)
1527 case OPT_PROF_OPTION:
1528 /* use <= to get the last \0 too */
1530 for (i = 0, j = 0; i <= strlen(opt_arg); i++) {
1531 if (opt_arg[i] == ',')
1534 if (opt_arg[i] == '\0') {
1535 if (strcmp("bb", opt_arg + j) == 0)
1539 printf("Unknown option: -Xprof:%s\n", opt_arg + j);
1543 /* set k to next char */
1556 #if defined(ENABLE_JIT)
1559 printf("-Xjit option not enabled.\n");
1565 #if defined(ENABLE_INTRP)
1568 printf("-Xint option not enabled.\n");
1573 #if defined(ENABLE_INTRP)
1574 case OPT_STATIC_SUPERS:
1575 opt_static_supers = atoi(opt_arg);
1578 case OPT_NO_DYNAMIC:
1579 opt_no_dynamic = true;
1582 case OPT_NO_REPLICATION:
1583 opt_no_replication = true;
1586 case OPT_NO_QUICKSUPER:
1587 opt_no_quicksuper = true;
1595 #if defined(ENABLE_DEBUG_FILTER)
1596 case OPT_FILTER_VERBOSECALL_INCLUDE:
1597 opt_filter_verbosecall_include = opt_arg;
1600 case OPT_FILTER_VERBOSECALL_EXCLUDE:
1601 opt_filter_verbosecall_exclude = opt_arg;
1604 case OPT_FILTER_SHOW_METHOD:
1605 opt_filter_show_method = opt_arg;
1610 printf("Unknown option: %s\n",
1611 vm_args->options[opt_index].optionString);
1616 /* get the main class *****************************************************/
1618 if (opt_index < vm_args->nOptions) {
1619 mainstring = vm_args->options[opt_index++].optionString;
1621 /* Put the jar file into the classpath (if any). */
1623 if (opt_jar == true) {
1624 /* free old classpath */
1626 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1628 /* put jarfile into classpath */
1630 _Jv_classpath = MNEW(char, strlen(mainstring) + strlen("0"));
1632 strcpy(_Jv_classpath, mainstring);
1634 #if defined(ENABLE_JAVASE)
1635 properties_add("java.class.path", _Jv_classpath);
1639 /* replace .'s with /'s in classname */
1641 for (i = strlen(mainstring) - 1; i >= 0; i--)
1642 if (mainstring[i] == '.')
1643 mainstring[i] = '/';
1647 #if defined(ENABLE_JVMTI)
1649 jvmti_set_phase(JVMTI_PHASE_ONLOAD);
1650 jvmti_agentload(agentarg, agentbypath, &handle, &libname);
1653 MFREE(agentarg, char, strlen(agentarg));
1655 jvmti_set_phase(JVMTI_PHASE_PRIMORDIAL);
1659 /* initialize this JVM ****************************************************/
1661 vm_initializing = true;
1663 /* initialize the garbage collector */
1665 gc_init(opt_heapmaxsize, opt_heapstartsize);
1667 #if defined(ENABLE_THREADS)
1668 /* AFTER: gc_init (directly after, as this initializes the
1669 stopworldlock lock */
1674 /* install architecture dependent signal handlers */
1677 vm_abort("vm_create: signal_init failed");
1679 #if defined(ENABLE_INTRP)
1680 /* Allocate main thread stack on the Java heap. */
1683 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1684 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1688 /* AFTER: threads_preinit */
1691 vm_abort("vm_create: string_init failed");
1693 /* AFTER: threads_preinit */
1696 vm_abort("vm_create: utf8_init failed");
1698 /* AFTER: thread_preinit */
1701 vm_abort("vm_create: suck_init failed");
1703 suck_add_from_property("java.endorsed.dirs");
1705 /* Now we have all options handled and we can print the version
1708 AFTER: suck_add_from_property("java.endorsed.dirs"); */
1713 /* AFTER: utf8_init */
1715 suck_add(_Jv_bootclasspath);
1717 /* initialize the classcache hashtable stuff: lock, hashtable
1718 (must be done _after_ threads_preinit) */
1720 if (!classcache_init())
1721 vm_abort("vm_create: classcache_init failed");
1723 /* initialize the memory subsystem (must be done _after_
1727 vm_abort("vm_create: memory_init failed");
1729 /* initialize the finalizer stuff (must be done _after_
1732 if (!finalizer_init())
1733 vm_abort("vm_create: finalizer_init failed");
1735 /* initialize the codegen subsystems */
1739 /* initializes jit compiler */
1743 /* machine dependent initialization */
1745 #if defined(ENABLE_JIT)
1746 # if defined(ENABLE_INTRP)
1756 /* initialize the loader subsystems (must be done _after_
1760 vm_abort("vm_create: loader_init failed");
1762 /* Link some important VM classes. */
1763 /* AFTER: utf8_init */
1766 vm_abort("vm_create: linker_init failed");
1768 if (!primitive_init())
1769 vm_abort("vm_create: primitive_init failed");
1771 if (!exceptions_init())
1772 vm_abort("vm_create: exceptions_init failed");
1774 if (!builtin_init())
1775 vm_abort("vm_create: builtin_init failed");
1777 /* Initialize the native subsystem. */
1778 /* BEFORE: threads_init */
1781 vm_abort("vm_create: native_init failed");
1783 /* Register the native methods implemented in the VM. */
1784 /* BEFORE: threads_init */
1786 if (!nativevm_preinit())
1787 vm_abort("vm_create: nativevm_preinit failed");
1789 #if defined(ENABLE_JNI)
1790 /* Initialize the JNI subsystem (must be done _before_
1791 threads_init, as threads_init can call JNI methods
1792 (e.g. NewGlobalRef). */
1795 vm_abort("vm_create: jni_init failed");
1798 #if defined(ENABLE_THREADS)
1799 if (!threads_init())
1800 vm_abort("vm_create: threads_init failed");
1803 /* Initialize the native VM subsystem. */
1804 /* AFTER: threads_init (at least for SUN's classes) */
1806 if (!nativevm_init())
1807 vm_abort("vm_create: nativevm_init failed");
1809 #if defined(ENABLE_PROFILING)
1810 /* initialize profiling */
1812 if (!profile_init())
1813 vm_abort("vm_create: profile_init failed");
1816 #if defined(ENABLE_THREADS)
1817 /* initialize recompilation */
1819 if (!recompile_init())
1820 vm_abort("vm_create: recompile_init failed");
1822 /* start the signal handler thread */
1824 #if defined(__LINUX__)
1825 /* XXX Remove for exact-GC. */
1826 if (threads_pthreads_implementation_nptl)
1828 if (!signal_start_thread())
1829 vm_abort("vm_create: signal_start_thread failed");
1831 /* finally, start the finalizer thread */
1833 if (!finalizer_start_thread())
1834 vm_abort("vm_create: finalizer_start_thread failed");
1836 # if !defined(NDEBUG)
1837 /* start the memory profiling thread */
1839 if (opt_ProfileMemoryUsage || opt_ProfileGCMemoryUsage)
1840 if (!memory_start_thread())
1841 vm_abort("vm_create: memory_start_thread failed");
1844 /* start the recompilation thread (must be done before the
1845 profiling thread) */
1847 if (!recompile_start_thread())
1848 vm_abort("vm_create: recompile_start_thread failed");
1850 # if defined(ENABLE_PROFILING)
1851 /* start the profile sampling thread */
1854 /* if (!profile_start_thread()) */
1855 /* vm_abort("vm_create: profile_start_thread failed"); */
1859 #if defined(ENABLE_JVMTI)
1861 /* add agent library to native library hashtable */
1862 native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
1866 /* increment the number of VMs */
1870 /* initialization is done */
1872 vm_initializing = false;
1874 /* everything's ok */
1880 /* vm_run **********************************************************************
1882 Runs the main-method of the passed class.
1884 *******************************************************************************/
1886 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1889 classinfo *mainclass;
1892 java_handle_objectarray_t *oa;
1899 #if !defined(NDEBUG)
1905 if (opt_method != NULL) {
1906 vm_compile_method();
1909 #endif /* !defined(NDEBUG) */
1911 /* should we run the main-method? */
1913 if (mainstring == NULL)
1916 /* set return value to OK */
1920 if (opt_jar == true) {
1921 /* open jar file with java.util.jar.JarFile */
1923 mainstring = vm_get_mainclass_from_jar(mainstring);
1925 if (mainstring == NULL)
1929 /* load the main class */
1931 mainutf = utf_new_char(mainstring);
1933 #if defined(ENABLE_JAVAME_CLDC1_1)
1934 mainclass = load_class_bootstrap(mainutf);
1936 mainclass = load_class_from_sysloader(mainutf);
1939 /* error loading class */
1941 e = exceptions_get_and_clear_exception();
1943 if ((e != NULL) || (mainclass == NULL)) {
1944 exceptions_throw_noclassdeffounderror_cause(e);
1945 exceptions_print_stacktrace();
1949 if (!link_class(mainclass)) {
1950 exceptions_print_stacktrace();
1954 /* find the `main' method of the main class */
1956 m = class_resolveclassmethod(mainclass,
1957 utf_new_char("main"),
1958 utf_new_char("([Ljava/lang/String;)V"),
1959 class_java_lang_Object,
1962 if (exceptions_get_exception()) {
1963 exceptions_print_stacktrace();
1967 /* there is no main method or it isn't static */
1969 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
1970 exceptions_clear_exception();
1971 exceptions_throw_nosuchmethoderror(mainclass,
1972 utf_new_char("main"),
1973 utf_new_char("([Ljava/lang/String;)V"));
1975 exceptions_print_stacktrace();
1979 /* build argument array */
1981 oalength = vm_args->nOptions - opt_index;
1983 oa = builtin_anewarray(oalength, class_java_lang_String);
1985 for (i = 0; i < oalength; i++) {
1986 u = utf_new_char(vm_args->options[opt_index + i].optionString);
1987 s = javastring_new(u);
1989 LLNI_objectarray_element_set(oa, i, s);
1992 #ifdef TYPEINFO_DEBUG_TEST
1993 /* test the typeinfo system */
1996 /*class_showmethods(currentThread->group->header.vftbl->class); */
1998 #if defined(ENABLE_JVMTI)
1999 jvmti_set_phase(JVMTI_PHASE_LIVE);
2002 /* set ThreadMXBean variables */
2004 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
2005 _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
2007 if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
2008 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
2009 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
2010 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
2012 /* start the main thread */
2014 (void) vm_call_method(m, NULL, oa);
2016 /* exception occurred? */
2018 if (exceptions_get_exception()) {
2019 exceptions_print_stacktrace();
2023 /* unload the JavaVM */
2025 (void) vm_destroy(vm);
2033 /* vm_destroy ******************************************************************
2035 Unloads a Java VM and reclaims its resources.
2037 *******************************************************************************/
2039 s4 vm_destroy(JavaVM *vm)
2041 #if defined(ENABLE_THREADS)
2042 threads_join_all_threads();
2045 /* everything's ok */
2051 /* vm_exit *********************************************************************
2053 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
2055 *******************************************************************************/
2057 void vm_exit(s4 status)
2061 /* signal that we are exiting */
2065 assert(class_java_lang_System);
2066 assert(class_java_lang_System->state & CLASS_LOADED);
2068 #if defined(ENABLE_JVMTI)
2069 if (jvmti || (dbgcom!=NULL)) {
2070 jvmti_set_phase(JVMTI_PHASE_DEAD);
2071 if (jvmti) jvmti_agentunload();
2075 if (!link_class(class_java_lang_System)) {
2076 exceptions_print_stacktrace();
2080 /* call java.lang.System.exit(I)V */
2082 m = class_resolveclassmethod(class_java_lang_System,
2083 utf_new_char("exit"),
2085 class_java_lang_Object,
2089 exceptions_print_stacktrace();
2093 /* call the exit function with passed exit status */
2095 (void) vm_call_method(m, NULL, status);
2097 /* If we had an exception, just ignore the exception and exit with
2100 vm_shutdown(status);
2104 /* vm_shutdown *****************************************************************
2106 Terminates the system immediately without freeing memory explicitly
2107 (to be used only for abnormal termination).
2109 *******************************************************************************/
2111 void vm_shutdown(s4 status)
2114 #if defined(ENABLE_STATISTICS)
2115 || opt_getcompilingtime || opt_stat
2119 log_text("CACAO terminated by shutdown");
2120 dolog("Exit status: %d\n", (s4) status);
2124 #if defined(ENABLE_JVMTI)
2125 /* terminate cacaodbgserver */
2127 pthread_mutex_lock(&dbgcomlock);
2129 pthread_mutex_unlock(&dbgcomlock);
2130 jvmti_cacaodbgserver_quit();
2138 /* vm_exit_handler *************************************************************
2140 The exit_handler function is called upon program termination.
2142 ATTENTION: Don't free system resources here! Some threads may still
2143 be running as this is called from VMRuntime.exit(). The OS does the
2146 *******************************************************************************/
2148 void vm_exit_handler(void)
2150 #if !defined(NDEBUG)
2152 class_showmethods(mainclass);
2154 if (showconstantpool)
2155 class_showconstantpool(mainclass);
2160 # if defined(ENABLE_PROFILING)
2162 profile_printstats();
2164 #endif /* !defined(NDEBUG) */
2166 #if defined(ENABLE_RT_TIMING)
2167 rt_timing_print_time_stats(stderr);
2170 #if defined(ENABLE_CYCLES_STATS)
2171 builtin_print_cycles_stats(stderr);
2172 stacktrace_print_cycles_stats(stderr);
2176 #if defined(ENABLE_STATISTICS)
2177 || opt_getcompilingtime || opt_stat
2181 log_text("CACAO terminated");
2183 #if defined(ENABLE_STATISTICS)
2186 #ifdef TYPECHECK_STATISTICS
2187 typecheck_print_statistics(get_logfile());
2191 if (opt_getcompilingtime)
2193 #endif /* defined(ENABLE_STATISTICS) */
2195 /* vm_print_profile(stderr);*/
2199 /* vm_abort ********************************************************************
2201 Prints an error message and aborts the VM.
2203 *******************************************************************************/
2205 void vm_abort(const char *text, ...)
2209 /* print the log message */
2214 log_vprint(text, ap);
2219 /* now abort the VM */
2225 /* vm_get_mainclass_from_jar ***************************************************
2227 Gets the name of the main class from a JAR's manifest file.
2229 *******************************************************************************/
2231 static char *vm_get_mainclass_from_jar(char *mainstring)
2238 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
2241 exceptions_print_stacktrace();
2245 /* create JarFile object */
2250 exceptions_print_stacktrace();
2254 m = class_resolveclassmethod(c,
2256 utf_java_lang_String__void,
2257 class_java_lang_Object,
2261 exceptions_print_stacktrace();
2265 s = javastring_new_from_ascii(mainstring);
2267 (void) vm_call_method(m, o, s);
2269 if (exceptions_get_exception()) {
2270 exceptions_print_stacktrace();
2274 /* get manifest object */
2276 m = class_resolveclassmethod(c,
2277 utf_new_char("getManifest"),
2278 utf_new_char("()Ljava/util/jar/Manifest;"),
2279 class_java_lang_Object,
2283 exceptions_print_stacktrace();
2287 o = vm_call_method(m, o);
2290 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
2295 /* get Main Attributes */
2297 m = class_resolveclassmethod(o->vftbl->class,
2298 utf_new_char("getMainAttributes"),
2299 utf_new_char("()Ljava/util/jar/Attributes;"),
2300 class_java_lang_Object,
2304 exceptions_print_stacktrace();
2308 o = vm_call_method(m, o);
2311 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
2316 /* get property Main-Class */
2318 m = class_resolveclassmethod(o->vftbl->class,
2319 utf_new_char("getValue"),
2320 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
2321 class_java_lang_Object,
2325 exceptions_print_stacktrace();
2329 s = javastring_new_from_ascii("Main-Class");
2331 o = vm_call_method(m, o, s);
2334 exceptions_print_stacktrace();
2338 return javastring_tochar(o);
2342 /* vm_compile_all **************************************************************
2344 Compile all methods found in the bootclasspath.
2346 *******************************************************************************/
2348 #if !defined(NDEBUG)
2349 static void vm_compile_all(void)
2354 classcache_name_entry *nmen;
2355 classcache_class_entry *clsen;
2358 /* create all classes found in the bootclasspath */
2359 /* XXX currently only works with zip/jar's */
2361 loader_load_all_classes();
2363 /* link all classes */
2365 for (slot = 0; slot < hashtable_classcache.size; slot++) {
2366 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
2368 for (; nmen; nmen = nmen->hashlink) {
2369 /* iterate over all class entries */
2371 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
2372 c = clsen->classobj;
2377 if (!(c->state & CLASS_LINKED)) {
2378 if (!link_class(c)) {
2379 fprintf(stderr, "Error linking: ");
2380 utf_fprint_printable_ascii_classname(stderr, c->name);
2381 fprintf(stderr, "\n");
2383 /* print out exception and cause */
2385 exceptions_print_current_exception();
2387 /* goto next class */
2393 /* compile all class methods */
2395 for (i = 0; i < c->methodscount; i++) {
2396 m = &(c->methods[i]);
2398 if (m->jcode != NULL) {
2399 if (!jit_compile(m)) {
2400 fprintf(stderr, "Error compiling: ");
2401 utf_fprint_printable_ascii_classname(stderr, c->name);
2402 fprintf(stderr, ".");
2403 utf_fprint_printable_ascii(stderr, m->name);
2404 utf_fprint_printable_ascii(stderr, m->descriptor);
2405 fprintf(stderr, "\n");
2407 /* print out exception and cause */
2409 exceptions_print_current_exception();
2417 #endif /* !defined(NDEBUG) */
2420 /* vm_compile_method ***********************************************************
2422 Compile a specific method.
2424 *******************************************************************************/
2426 #if !defined(NDEBUG)
2427 static void vm_compile_method(void)
2431 /* create, load and link the main class */
2433 mainclass = load_class_bootstrap(utf_new_char(mainstring));
2435 if (mainclass == NULL)
2436 exceptions_print_stacktrace();
2438 if (!link_class(mainclass))
2439 exceptions_print_stacktrace();
2441 if (opt_signature != NULL) {
2442 m = class_resolveclassmethod(mainclass,
2443 utf_new_char(opt_method),
2444 utf_new_char(opt_signature),
2449 m = class_resolveclassmethod(mainclass,
2450 utf_new_char(opt_method),
2457 vm_abort("vm_compile_method: java.lang.NoSuchMethodException: %s.%s",
2458 opt_method, opt_signature ? opt_signature : "");
2462 #endif /* !defined(NDEBUG) */
2465 /* vm_array_store_int **********************************************************
2467 Helper function to store an integer into the argument array, taking
2468 care of architecture specific issues.
2470 *******************************************************************************/
2472 static void vm_array_store_int(uint64_t *array, paramdesc *pd, int32_t value)
2476 if (!pd->inmemory) {
2478 array[index] = (int64_t) value;
2481 index = ARG_CNT + pd->index;
2482 #if SIZEOF_VOID_P == 8
2483 array[index] = (int64_t) value;
2485 # if WORDS_BIGENDIAN == 1
2486 array[index] = ((int64_t) value) << 32;
2488 array[index] = (int64_t) value;
2495 /* vm_array_store_lng **********************************************************
2497 Helper function to store a long into the argument array, taking
2498 care of architecture specific issues.
2500 *******************************************************************************/
2502 static void vm_array_store_lng(uint64_t *array, paramdesc *pd, int64_t value)
2506 #if SIZEOF_VOID_P == 8
2510 index = ARG_CNT + pd->index;
2512 array[index] = value;
2514 if (!pd->inmemory) {
2515 /* move low and high 32-bits into it's own argument slot */
2517 index = GET_LOW_REG(pd->index);
2518 array[index] = value & 0x00000000ffffffff;
2520 index = GET_HIGH_REG(pd->index);
2521 array[index] = value >> 32;
2524 index = ARG_CNT + pd->index;
2525 array[index] = value;
2531 /* vm_array_store_flt **********************************************************
2533 Helper function to store a float into the argument array, taking
2534 care of architecture specific issues.
2536 *******************************************************************************/
2538 static void vm_array_store_flt(uint64_t *array, paramdesc *pd, uint64_t value)
2542 if (!pd->inmemory) {
2543 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2546 index = INT_ARG_CNT + pd->index;
2548 #if WORDS_BIGENDIAN == 1 && !defined(__POWERPC__) && !defined(__POWERPC64__) && !defined(__S390__)
2549 array[index] = value >> 32;
2551 array[index] = value;
2555 index = ARG_CNT + pd->index;
2556 #if defined(__SPARC_64__)
2557 array[index] = value >> 32;
2559 array[index] = value;
2565 /* vm_array_store_dbl **********************************************************
2567 Helper function to store a double into the argument array, taking
2568 care of architecture specific issues.
2570 *******************************************************************************/
2572 static void vm_array_store_dbl(uint64_t *array, paramdesc *pd, uint64_t value)
2576 if (!pd->inmemory) {
2577 #if SIZEOF_VOID_P != 8 && defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2578 index = GET_LOW_REG(pd->index);
2579 array[index] = value & 0x00000000ffffffff;
2581 index = GET_HIGH_REG(pd->index);
2582 array[index] = value >> 32;
2584 index = INT_ARG_CNT + pd->index;
2585 array[index] = value;
2589 index = ARG_CNT + pd->index;
2590 array[index] = value;
2595 /* vm_array_store_adr **********************************************************
2597 Helper function to store an address into the argument array, taking
2598 care of architecture specific issues.
2600 *******************************************************************************/
2602 static void vm_array_store_adr(uint64_t *array, paramdesc *pd, void *value)
2606 if (!pd->inmemory) {
2607 #if defined(HAS_ADDRESS_REGISTER_FILE)
2608 /* When the architecture has address registers, place them
2609 after integer and float registers. */
2611 index = INT_ARG_CNT + FLT_ARG_CNT + pd->index;
2615 array[index] = (uint64_t) (intptr_t) value;
2618 index = ARG_CNT + pd->index;
2619 #if SIZEOF_VOID_P == 8
2620 array[index] = (uint64_t) (intptr_t) value;
2622 # if WORDS_BIGENDIAN == 1
2623 array[index] = ((uint64_t) (intptr_t) value) << 32;
2625 array[index] = (uint64_t) (intptr_t) value;
2632 /* vm_array_from_valist ********************************************************
2636 *******************************************************************************/
2638 uint64_t *vm_array_from_valist(methodinfo *m, java_object_t *o, va_list ap)
2647 /* get the descriptors */
2651 td = md->paramtypes;
2653 /* allocate argument array */
2655 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2657 /* if method is non-static fill first block and skip `this' pointer */
2662 /* the `this' pointer */
2663 vm_array_store_adr(array, pd, o);
2670 for (; i < md->paramcount; i++, pd++, td++) {
2673 value.i = va_arg(ap, int32_t);
2674 vm_array_store_int(array, pd, value.i);
2678 value.l = va_arg(ap, int64_t);
2679 vm_array_store_lng(array, pd, value.l);
2683 #if defined(__ALPHA__) || defined(__POWERPC__) || defined(__POWERPC64__)
2684 /* This is required to load the correct float value in
2687 value.d = (double) va_arg(ap, double);
2689 value.f = (float) va_arg(ap, double);
2691 vm_array_store_flt(array, pd, value.l);
2695 value.d = va_arg(ap, double);
2696 vm_array_store_dbl(array, pd, value.l);
2700 value.a = va_arg(ap, void*);
2701 vm_array_store_adr(array, pd, value.a);
2710 /* vm_array_from_jvalue ********************************************************
2714 *******************************************************************************/
2716 static uint64_t *vm_array_from_jvalue(methodinfo *m, java_object_t *o,
2726 /* get the descriptors */
2730 td = md->paramtypes;
2732 /* allocate argument array */
2734 #if defined(HAS_ADDRESS_REGISTER_FILE)
2735 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + ADR_ARG_CNT + md->memuse);
2737 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2740 /* if method is non-static fill first block and skip `this' pointer */
2745 /* the `this' pointer */
2746 vm_array_store_adr(array, pd, o);
2753 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2754 switch (td->decltype) {
2756 vm_array_store_int(array, pd, args[j].i);
2760 vm_array_store_lng(array, pd, args[j].j);
2764 vm_array_store_flt(array, pd, args[j].j);
2768 vm_array_store_dbl(array, pd, args[j].j);
2772 vm_array_store_adr(array, pd, args[j].l);
2781 /* vm_array_from_objectarray ***************************************************
2785 *******************************************************************************/
2787 uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o,
2788 java_handle_objectarray_t *params)
2794 java_handle_t *param;
2801 /* get the descriptors */
2805 td = md->paramtypes;
2807 /* allocate argument array */
2809 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2811 /* if method is non-static fill first block and skip `this' pointer */
2817 vm_array_store_adr(array, pd, o);
2824 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2825 LLNI_objectarray_element_get(params, j, param);
2832 /* convert the value according to its declared type */
2834 c = param->vftbl->class;
2835 type = primitive_type_get_by_wrapperclass(c);
2837 switch (td->decltype) {
2838 case PRIMITIVETYPE_BOOLEAN:
2840 case PRIMITIVETYPE_BOOLEAN:
2841 /* This type is OK. */
2848 case PRIMITIVETYPE_BYTE:
2850 case PRIMITIVETYPE_BYTE:
2851 /* This type is OK. */
2858 case PRIMITIVETYPE_CHAR:
2860 case PRIMITIVETYPE_CHAR:
2861 /* This type is OK. */
2868 case PRIMITIVETYPE_SHORT:
2870 case PRIMITIVETYPE_BYTE:
2871 case PRIMITIVETYPE_SHORT:
2872 /* These types are OK. */
2879 case PRIMITIVETYPE_INT:
2881 case PRIMITIVETYPE_BYTE:
2882 case PRIMITIVETYPE_SHORT:
2883 case PRIMITIVETYPE_INT:
2884 /* These types are OK. */
2892 vm_abort("vm_array_from_objectarray: invalid type %d",
2896 value = primitive_unbox(param);
2897 vm_array_store_int(array, pd, value.i);
2904 c = param->vftbl->class;
2905 type = primitive_type_get_by_wrapperclass(c);
2907 assert(td->decltype == PRIMITIVETYPE_LONG);
2910 case PRIMITIVETYPE_BYTE:
2911 case PRIMITIVETYPE_SHORT:
2912 case PRIMITIVETYPE_INT:
2913 case PRIMITIVETYPE_LONG:
2914 /* These types are OK. */
2920 value = primitive_unbox(param);
2921 vm_array_store_lng(array, pd, value.l);
2928 c = param->vftbl->class;
2929 type = primitive_type_get_by_wrapperclass(c);
2931 assert(td->decltype == PRIMITIVETYPE_FLOAT);
2934 case PRIMITIVETYPE_FLOAT:
2935 /* This type is OK. */
2941 value = primitive_unbox(param);
2942 vm_array_store_flt(array, pd, value.l);
2949 c = param->vftbl->class;
2950 type = primitive_type_get_by_wrapperclass(c);
2952 assert(td->decltype == PRIMITIVETYPE_DOUBLE);
2955 case PRIMITIVETYPE_FLOAT:
2956 case PRIMITIVETYPE_DOUBLE:
2957 /* These types are OK. */
2963 value = primitive_unbox(param);
2964 vm_array_store_dbl(array, pd, value.l);
2968 if (!resolve_class_from_typedesc(td, true, true, &c))
2971 if (param != NULL) {
2972 if (td->arraydim > 0) {
2973 if (!builtin_arrayinstanceof(param, c))
2977 if (!builtin_instanceof(param, c))
2982 vm_array_store_adr(array, pd, param);
2986 vm_abort("vm_array_from_objectarray: invalid type %d", td->type);
2993 exceptions_throw_illegalargumentexception();
2998 /* vm_call_method **************************************************************
3000 Calls a Java method with a variable number of arguments.
3002 *******************************************************************************/
3004 #define VM_CALL_METHOD(name, type) \
3005 type vm_call_method##name(methodinfo *m, java_handle_t *o, ...) \
3011 value = vm_call_method##name##_valist(m, o, ap); \
3017 VM_CALL_METHOD(, java_handle_t *)
3018 VM_CALL_METHOD(_int, int32_t)
3019 VM_CALL_METHOD(_long, int64_t)
3020 VM_CALL_METHOD(_float, float)
3021 VM_CALL_METHOD(_double, double)
3024 /* vm_call_method_valist *******************************************************
3026 Calls a Java method with a variable number of arguments, passed via
3029 *******************************************************************************/
3031 #define VM_CALL_METHOD_VALIST(name, type) \
3032 type vm_call_method##name##_valist(methodinfo *m, java_handle_t *o, \
3039 dumpsize = dump_size(); \
3040 array = vm_array_from_valist(m, o, ap); \
3041 value = vm_call##name##_array(m, array); \
3042 dump_release(dumpsize); \
3047 VM_CALL_METHOD_VALIST(, java_handle_t *)
3048 VM_CALL_METHOD_VALIST(_int, int32_t)
3049 VM_CALL_METHOD_VALIST(_long, int64_t)
3050 VM_CALL_METHOD_VALIST(_float, float)
3051 VM_CALL_METHOD_VALIST(_double, double)
3054 /* vm_call_method_jvalue *******************************************************
3056 Calls a Java method with a variable number of arguments, passed via
3059 *******************************************************************************/
3061 #define VM_CALL_METHOD_JVALUE(name, type) \
3062 type vm_call_method##name##_jvalue(methodinfo *m, java_handle_t *o, \
3063 const jvalue *args) \
3069 dumpsize = dump_size(); \
3070 array = vm_array_from_jvalue(m, o, args); \
3071 value = vm_call##name##_array(m, array); \
3072 dump_release(dumpsize); \
3077 VM_CALL_METHOD_JVALUE(, java_handle_t *)
3078 VM_CALL_METHOD_JVALUE(_int, int32_t)
3079 VM_CALL_METHOD_JVALUE(_long, int64_t)
3080 VM_CALL_METHOD_JVALUE(_float, float)
3081 VM_CALL_METHOD_JVALUE(_double, double)
3084 /* vm_call_array ***************************************************************
3086 Calls a Java method with a variable number of arguments, passed via
3089 *******************************************************************************/
3091 #define VM_CALL_ARRAY(name, type) \
3092 type vm_call##name##_array(methodinfo *m, uint64_t *array) \
3098 md = m->parseddesc; \
3100 if (m->code == NULL) \
3101 if (!jit_compile(m)) \
3104 pv = m->code->entrypoint; \
3106 STATISTICS(count_calls_native_to_java++); \
3108 value = asm_vm_call_method##name(pv, array, md->memuse); \
3113 VM_CALL_ARRAY(, java_handle_t *)
3114 VM_CALL_ARRAY(_int, int32_t)
3115 VM_CALL_ARRAY(_long, int64_t)
3116 VM_CALL_ARRAY(_float, float)
3117 VM_CALL_ARRAY(_double, double)
3121 * These are local overrides for various environment variables in Emacs.
3122 * Please do not remove this and leave it at the end of the file, where
3123 * Emacs will automagically detect them.
3124 * ---------------------------------------------------------------------
3127 * indent-tabs-mode: t