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 8329 2007-08-16 17:57:27Z 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 },
333 { "v", false, OPT_VERBOSE1 },
334 { "verbose:", true, OPT_VERBOSE },
336 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
337 { "verbosetc", false, OPT_VERBOSETC },
339 #if defined(__ALPHA__)
340 { "noieee", false, OPT_NOIEEE },
342 #if defined(ENABLE_STATISTICS)
343 { "time", false, OPT_TIME },
344 { "stat", false, OPT_STAT },
346 { "log", true, OPT_LOG },
347 { "c", true, OPT_CHECK },
348 { "l", false, OPT_LOAD },
351 { "all", false, OPT_ALL },
352 { "sig", true, OPT_SIGNATURE },
355 #if defined(ENABLE_LOOP)
356 { "oloop", false, OPT_OLOOP },
358 #if defined(ENABLE_IFCONV)
359 { "ifconv", false, OPT_IFCONV },
361 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
362 { "lsra", false, OPT_LSRA },
365 #if defined(ENABLE_INTRP)
366 /* interpreter options */
368 { "trace", false, OPT_TRACE },
369 { "static-supers", true, OPT_STATIC_SUPERS },
370 { "no-dynamic", false, OPT_NO_DYNAMIC },
371 { "no-replication", false, OPT_NO_REPLICATION },
372 { "no-quicksuper", false, OPT_NO_QUICKSUPER },
375 /* JVMTI Agent Command Line Options */
377 { "agentlib:", true, OPT_AGENTLIB },
378 { "agentpath:", true, OPT_AGENTPATH },
381 /* Java non-standard options */
383 { "Xjit", false, OPT_JIT },
384 { "Xint", false, OPT_INTRP },
385 { "Xbootclasspath:", true, OPT_BOOTCLASSPATH },
386 { "Xbootclasspath/a:", true, OPT_BOOTCLASSPATH_A },
387 { "Xbootclasspath/p:", true, OPT_BOOTCLASSPATH_P },
388 { "Xbootclasspath/c:", true, OPT_BOOTCLASSPATH_C },
391 { "Xdebug", false, OPT_DEBUG },
392 { "Xnoagent", false, OPT_NOAGENT },
393 { "Xrunjdwp", true, OPT_XRUNJDWP },
396 { "Xms", true, OPT_MS },
397 { "ms", true, OPT_MS },
398 { "Xmx", true, OPT_MX },
399 { "mx", true, OPT_MX },
400 { "Xss", true, OPT_SS },
401 { "ss", true, OPT_SS },
403 #if defined(ENABLE_PROFILING)
404 { "Xprof:", true, OPT_PROF_OPTION },
405 { "Xprof", false, OPT_PROF },
408 /* inlining options */
410 #if defined(ENABLE_INLINING)
411 #if defined(ENABLE_INLINING_DEBUG)
412 { "ia", false, OPT_INLINE_DEBUG_ALL },
413 { "ii", true, OPT_INLINE_DEBUG_MIN },
414 { "im", true, OPT_INLINE_DEBUG_MAX },
415 { "ie", true, OPT_INLINE_DEBUG_END },
416 #endif /* defined(ENABLE_INLINING_DEBUG) */
418 { "il", false, OPT_INLINE_LOG },
420 { "i", false, OPT_INLINING },
421 #endif /* defined(ENABLE_INLINING) */
423 /* keep these at the end of the list */
426 { "m", true, OPT_METHOD },
429 { "s", true, OPT_SHOW },
430 { "debug-color", false, OPT_DEBUGCOLOR },
432 #if defined(ENABLE_DEBUG_FILTER)
433 { "XXfi", true, OPT_FILTER_VERBOSECALL_INCLUDE },
434 { "XXfx", true, OPT_FILTER_VERBOSECALL_EXCLUDE },
435 { "XXfm", true, OPT_FILTER_SHOW_METHOD },
442 /* usage ***********************************************************************
444 Prints the correct usage syntax to stdout.
446 *******************************************************************************/
450 puts("Usage: cacao [-options] classname [arguments]");
451 puts(" (to run a class file)");
452 puts(" or cacao [-options] -jar jarfile [arguments]");
453 puts(" (to run a standalone jar file)\n");
455 puts("where options include:");
456 puts(" -d32 use 32-bit data model if available");
457 puts(" -d64 use 64-bit data model if available");
458 puts(" -client compatibility (currently ignored)");
459 puts(" -server compatibility (currently ignored)");
460 puts(" -jvm compatibility (currently ignored)");
461 puts(" -hotspot compatibility (currently ignored)\n");
463 puts(" -cp <path> specify a path to look for classes");
464 puts(" -classpath <path> specify a path to look for classes");
465 puts(" -D<name>=<value> add an entry to the property list");
466 puts(" -verbose[:class|gc|jni] enable specific verbose output");
467 puts(" -version print product version and exit");
468 puts(" -fullversion print jpackage-compatible product version and exit");
469 puts(" -showversion print product version and continue");
470 puts(" -help, -? print this help message");
471 puts(" -X print help on non-standard Java options");
472 puts(" -XX print help on debugging options");
473 puts(" -ea[:<packagename>...|:<classname>]");
474 puts(" -enableassertions[:<packagename>...|:<classname>]");
475 puts(" enable assertions with specified granularity");
476 puts(" -da[:<packagename>...|:<classname>]");
477 puts(" -disableassertions[:<packagename>...|:<classname>]");
478 puts(" disable assertions with specified granularity");
479 puts(" -esa | -enablesystemassertions");
480 puts(" enable system assertions");
481 puts(" -dsa | -disablesystemassertions");
482 puts(" disable system assertions");
485 puts(" -agentlib:<agent-lib-name>=<options> library to load containg JVMTI agent");
486 puts (" for jdwp help use: -agentlib:jdwp=help");
487 puts(" -agentpath:<path-to-agent>=<options> path to library containg JVMTI agent");
490 /* exit with error code */
496 static void Xusage(void)
498 #if defined(ENABLE_JIT)
499 puts(" -Xjit JIT mode execution (default)");
501 #if defined(ENABLE_INTRP)
502 puts(" -Xint interpreter mode execution");
504 puts(" -Xbootclasspath:<zip/jar files and directories separated by :>");
505 puts(" value is set as bootstrap class path");
506 puts(" -Xbootclasspath/a:<zip/jar files and directories separated by :>");
507 puts(" value is appended to the bootstrap class path");
508 puts(" -Xbootclasspath/p:<zip/jar files and directories separated by :>");
509 puts(" value is prepended to the bootstrap class path");
510 puts(" -Xbootclasspath/c:<zip/jar files and directories separated by :>");
511 puts(" value is used as Java core library, but the");
512 puts(" hardcoded VM interface classes are prepended");
513 printf(" -Xms<size> set the initial size of the heap (default: %dMB)\n", HEAP_STARTSIZE / 1024 / 1024);
514 printf(" -Xmx<size> set the maximum size of the heap (default: %dMB)\n", HEAP_MAXSIZE / 1024 / 1024);
515 printf(" -Xss<size> set the thread stack size (default: %dkB)\n", STACK_SIZE / 1024);
517 #if defined(ENABLE_PROFILING)
518 puts(" -Xprof[:bb] collect and print profiling data");
521 #if defined(ENABLE_JVMTI)
522 /* -Xdebug option depend on gnu classpath JDWP options. options:
523 transport=dt_socket,address=<hostname:port>,server=(y|n),suspend(y|n) */
524 puts(" -Xdebug enable remote debugging\n");
525 puts(" -Xrunjdwp transport=[dt_socket|...],address=<hostname:port>,server=[y|n],suspend=[y|n]\n");
526 puts(" enable remote debugging\n");
529 /* exit with error code */
536 static void XXusage(void)
538 puts(" -v write state-information");
540 puts(" -verbose[:jit|threads]");
541 puts(" enable specific verbose output");
542 puts(" -debug-color colored output for ANSI terms");
544 #ifdef TYPECHECK_VERBOSE
545 puts(" -verbosetc write debug messages while typechecking");
547 #if defined(__ALPHA__)
548 puts(" -noieee don't use ieee compliant arithmetic");
550 #if defined(ENABLE_VERIFIER)
551 puts(" -noverify don't verify classfiles");
553 #if defined(ENABLE_STATISTICS)
554 puts(" -time measure the runtime");
555 puts(" -stat detailed compiler statistics");
557 puts(" -log logfile specify a name for the logfile");
558 puts(" -c(heck)b(ounds) don't check array bounds");
559 puts(" s(ync) don't check for synchronization");
560 #if defined(ENABLE_LOOP)
561 puts(" -oloop optimize array accesses in loops");
563 puts(" -l don't start the class after loading");
565 puts(" -all compile all methods, no execution");
566 puts(" -m compile only a specific method");
567 puts(" -sig specify signature for a specific method");
570 puts(" -s... show...");
571 puts(" (c)onstants the constant pool");
572 puts(" (m)ethods class fields and methods");
573 puts(" (u)tf the utf - hash");
574 puts(" (i)ntermediate intermediate representation");
575 #if defined(ENABLE_DISASSEMBLER)
576 puts(" (a)ssembler disassembled listing");
577 puts(" n(o)ps show NOPs in disassembler output");
578 puts(" (e)xceptionstubs disassembled exception stubs (only with -sa)");
579 puts(" (n)ative disassembled native stubs");
581 puts(" (d)atasegment data segment listing");
583 #if defined(ENABLE_INLINING)
584 puts(" -i activate inlining");
586 puts(" -il log inlining");
588 #if defined(ENABLE_INLINING_DEBUG)
589 puts(" -ia use inlining for all methods");
590 puts(" -ii <size> set minimum size for inlined result");
591 puts(" -im <size> set maximum size for inlined result");
592 puts(" -ie <number> stop inlining after the given number of roots");
593 #endif /* defined(ENABLE_INLINING_DEBUG) */
594 #endif /* defined(ENABLE_INLINING) */
596 #if defined(ENABLE_IFCONV)
597 puts(" -ifconv use if-conversion");
599 #if defined(ENABLE_LSRA)
600 puts(" -lsra use linear scan register allocation");
602 #if defined(ENABLE_SSA)
603 puts(" -lsra use linear scan register allocation (with SSA)");
605 #if defined(ENABLE_DEBUG_FILTER)
606 puts(" -XXfi <regex> begin of dynamic scope for verbosecall filter");
607 puts(" -XXfx <regex> end of dynamic scope for verbosecall filter");
608 puts(" -XXfm <regex> filter for show options");
610 /* exit with error code */
617 /* version *********************************************************************
619 Only prints cacao version information.
621 *******************************************************************************/
623 static void version(bool opt_exit)
625 puts("java version \""JAVA_VERSION"\"");
626 puts("CACAO version "VERSION"");
628 puts("Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,");
629 puts("C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,");
630 puts("E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,");
631 puts("J. Wenninger, Institut f. Computersprachen - TU Wien\n");
633 puts("This program is free software; you can redistribute it and/or");
634 puts("modify it under the terms of the GNU General Public License as");
635 puts("published by the Free Software Foundation; either version 2, or (at");
636 puts("your option) any later version.\n");
638 puts("This program is distributed in the hope that it will be useful, but");
639 puts("WITHOUT ANY WARRANTY; without even the implied warranty of");
640 puts("MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU");
641 puts("General Public License for more details.");
643 /* exit normally, if requested */
650 /* fullversion *****************************************************************
652 Prints a Sun compatible version information (required e.g. by
653 jpackage, www.jpackage.org).
655 *******************************************************************************/
657 static void fullversion(void)
659 puts("java full version \"cacao-"JAVA_VERSION"\"");
667 void vm_printconfig(void)
669 puts("Configure/Build options:\n");
670 puts(" ./configure: "VERSION_CONFIGURE_ARGS"");
671 #if defined(__VERSION__)
672 puts(" CC : "VERSION_CC" ("__VERSION__")");
674 puts(" CC : "VERSION_CC"");
676 puts(" CFLAGS : "VERSION_CFLAGS"\n");
678 puts("Default variables:\n");
679 printf(" maximum heap size : %d\n", HEAP_MAXSIZE);
680 printf(" initial heap size : %d\n", HEAP_STARTSIZE);
681 printf(" stack size : %d\n", STACK_SIZE);
682 #if defined(WITH_CLASSPATH_GNU)
683 puts(" java.boot.class.path : "CACAO_VM_ZIP":"CLASSPATH_CLASSES"");
685 puts(" java.boot.class.path : "CLASSPATH_CLASSES"");
687 puts(" gnu.classpath.boot.library.path: "CLASSPATH_LIBDIR"/classpath\n");
689 puts("Runtime variables:\n");
690 printf(" maximum heap size : %d\n", opt_heapmaxsize);
691 printf(" initial heap size : %d\n", opt_heapstartsize);
692 printf(" stack size : %d\n", opt_stacksize);
693 printf(" libjvm.so : %s\n", cacao_libjvm);
694 printf(" java.boot.class.path : %s\n", _Jv_bootclasspath);
695 printf(" gnu.classpath.boot.library.path: %s\n", classpath_libdir);
696 printf(" java.class.path : %s\n", _Jv_classpath);
700 /* forward declarations *******************************************************/
702 static char *vm_get_mainclass_from_jar(char *mainstring);
704 static void vm_compile_all(void);
705 static void vm_compile_method(void);
709 /* vm_createjvm ****************************************************************
711 Implementation for JNI_CreateJavaVM.
713 *******************************************************************************/
715 bool vm_createjvm(JavaVM **p_vm, void **p_env, void *vm_args)
717 JavaVMInitArgs *_vm_args;
721 /* get the arguments for the new JVM */
723 _vm_args = (JavaVMInitArgs *) vm_args;
725 /* get the VM and Env tables (must be set before vm_create) */
727 env = NEW(_Jv_JNIEnv);
729 #if defined(ENABLE_JNI)
730 env->env = &_Jv_JNINativeInterface;
733 /* XXX Set the global variable. Maybe we should do that differently. */
737 /* create and fill a JavaVM structure */
739 vm = NEW(_Jv_JavaVM);
741 #if defined(ENABLE_JNI)
742 vm->functions = &_Jv_JNIInvokeInterface;
745 /* XXX Set the global variable. Maybe we should do that differently. */
746 /* XXX JVMTI Agents needs a JavaVM */
750 /* actually create the JVM */
752 if (!vm_create(_vm_args))
755 #if defined(ENABLE_JNI)
756 /* setup the local ref table (must be created after vm_create) */
758 if (!localref_table_init())
762 /* now return the values */
764 *p_vm = (JavaVM *) vm;
765 *p_env = (void *) env;
770 /* release allocated memory */
772 FREE(env, _Jv_JNIEnv);
773 FREE(vm, _Jv_JavaVM);
779 /* vm_create *******************************************************************
781 Creates a JVM. Called by vm_createjvm.
783 *******************************************************************************/
785 bool vm_create(JavaVMInitArgs *vm_args)
794 #if defined(ENABLE_JVMTI)
796 char *libname, *agentarg;
797 bool jdwp,agentbypath;
798 jdwp = agentbypath = false;
801 #if defined(ENABLE_VMLOG)
802 vmlog_cacao_init(vm_args);
805 /* check the JNI version requested */
807 switch (vm_args->version) {
808 case JNI_VERSION_1_1:
810 case JNI_VERSION_1_2:
811 case JNI_VERSION_1_4:
817 /* we only support 1 JVM instance */
822 if (atexit(vm_exit_handler))
823 vm_abort("atexit failed: %s\n", strerror(errno));
826 log_text("CACAO started -------------------------------------------------------");
828 /* We need to check if the actual size of a java.lang.Class object
829 is smaller or equal than the assumption made in
830 src/vmcore/class.h. */
832 if (sizeof(java_lang_Class) > sizeof(dummy_java_lang_Class))
833 vm_abort("vm_create: java_lang_Class structure is bigger than classinfo.object (%d > %d)", sizeof(java_lang_Class), sizeof(dummy_java_lang_Class));
835 /* set the VM starttime */
837 _Jv_jvm->starttime = builtin_currenttimemillis();
839 /* get stuff from the environment *****************************************/
841 #if defined(WITH_JRE_LAYOUT)
842 /* SUN also uses a buffer of 4096-bytes (strace is your friend). */
844 cacao_prefix = MNEW(char, 4096);
846 if (readlink("/proc/self/exe", cacao_prefix, 4095) == -1)
847 vm_abort("readlink failed: %s\n", strerror(errno));
849 /* get the path of the current executable */
851 cacao_prefix = dirname(cacao_prefix);
853 if ((strlen(cacao_prefix) + strlen("/..") + strlen("0")) > 4096)
854 vm_abort("libjvm name to long for buffer\n");
856 /* concatenate the library name */
858 strcat(cacao_prefix, "/..");
860 /* now set path to libjvm.so */
862 len = strlen(cacao_prefix) + strlen("/lib/libjvm") + strlen("0");
864 cacao_libjvm = MNEW(char, len);
865 strcpy(cacao_libjvm, cacao_prefix);
866 strcat(cacao_libjvm, "/lib/libjvm");
868 /* and finally set the path to GNU Classpath libraries */
870 len = strlen(cacao_prefix) + strlen("/lib/classpath") + strlen("0");
872 classpath_libdir = MNEW(char, len);
873 strcpy(classpath_libdir, cacao_prefix);
874 strcat(classpath_libdir, "/lib/classpath");
876 cacao_prefix = CACAO_PREFIX;
877 cacao_libjvm = CACAO_LIBDIR"/libjvm";
879 # if defined(WITH_CLASSPATH_GNU)
880 classpath_libdir = CLASSPATH_LIBDIR"/classpath";
882 classpath_libdir = CLASSPATH_LIBDIR;
886 /* set the bootclasspath */
888 cp = getenv("BOOTCLASSPATH");
891 _Jv_bootclasspath = MNEW(char, strlen(cp) + strlen("0"));
892 strcpy(_Jv_bootclasspath, cp);
895 #if defined(WITH_JRE_LAYOUT)
897 # if defined(WITH_CLASSPATH_GNU)
898 strlen(cacao_prefix) +
899 strlen("/share/cacao/vm.zip") +
902 strlen(cacao_prefix) +
903 strlen("/share/classpath/glibj.zip") +
906 _Jv_bootclasspath = MNEW(char, len);
907 # if defined(WITH_CLASSPATH_GNU)
908 strcat(_Jv_bootclasspath, cacao_prefix);
909 strcat(_Jv_bootclasspath, "/share/cacao/vm.zip");
910 strcat(_Jv_bootclasspath, ":");
912 strcat(_Jv_bootclasspath, cacao_prefix);
913 strcat(_Jv_bootclasspath, "/share/classpath/glibj.zip");
915 # if defined(WITH_CLASSPATH_GNU)
917 strlen(CACAO_VM_ZIP) +
919 strlen(CLASSPATH_CLASSES) +
921 # elif defined(WITH_CLASSPATH_SUN)
922 /* This is the bootclasspath taken from HotSpot (see
923 hotspot/src/share/vm/runtime/os.cpp
924 (os::set_boot_path)). */
927 strlen(CLASSPATH_PREFIX"/lib/resources.jar:"
928 CLASSPATH_PREFIX"/lib/rt.jar:"
929 CLASSPATH_PREFIX"/lib/sunrsasign.jar:"
930 CLASSPATH_PREFIX"/lib/jsse.jar:"
931 CLASSPATH_PREFIX"/lib/jce.jar:"
932 CLASSPATH_PREFIX"/lib/charsets.jar:"
933 CLASSPATH_PREFIX"/classes") +
935 # elif defined(WITH_CLASSPATH_CLDC1_1)
937 strlen(CLASSPATH_CLASSES) +
940 # error unknown classpath configuration
943 _Jv_bootclasspath = MNEW(char, len);
945 # if defined(WITH_CLASSPATH_GNU)
946 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
947 strcat(_Jv_bootclasspath, ":");
948 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
949 # elif defined(WITH_CLASSPATH_SUN)
950 strcpy(_Jv_bootclasspath,
951 CLASSPATH_PREFIX"/lib/resources.jar:"
952 CLASSPATH_PREFIX"/lib/rt.jar:"
953 CLASSPATH_PREFIX"/lib/sunrsasign.jar:"
954 CLASSPATH_PREFIX"/lib/jsse.jar:"
955 CLASSPATH_PREFIX"/lib/jce.jar:"
956 CLASSPATH_PREFIX"/lib/charsets.jar:"
957 CLASSPATH_PREFIX"/classes");
958 # elif defined(WITH_CLASSPATH_CLDC1_1)
959 strcat(_Jv_bootclasspath, CLASSPATH_CLASSES);
961 # error unknown classpath configuration
966 /* set the classpath */
968 cp = getenv("CLASSPATH");
971 _Jv_classpath = MNEW(char, strlen(cp) + strlen("0"));
972 strcat(_Jv_classpath, cp);
975 _Jv_classpath = MNEW(char, strlen(".") + strlen("0"));
976 strcpy(_Jv_classpath, ".");
979 /* Get and set java.library.path. */
981 _Jv_java_library_path = getenv("LD_LIBRARY_PATH");
983 if (_Jv_java_library_path == NULL)
984 _Jv_java_library_path = "";
986 /* interpret the options **************************************************/
993 opt_heapmaxsize = HEAP_MAXSIZE;
994 opt_heapstartsize = HEAP_STARTSIZE;
995 opt_stacksize = STACK_SIZE;
998 #if defined(ENABLE_JVMTI)
999 /* initialize JVMTI related **********************************************/
1003 /* Initialize and fill properties before command-line handling. */
1005 if (!properties_init())
1006 vm_abort("vm_create: properties_init failed");
1008 /* Set the classpath properties. */
1010 #if defined(ENABLE_JAVASE)
1011 properties_add("java.boot.class.path", _Jv_bootclasspath);
1012 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1013 properties_add("java.class.path", _Jv_classpath);
1016 /* iterate over all passed options */
1018 while ((opt = options_get(opts, vm_args)) != OPT_DONE) {
1032 #if SIZEOF_VOID_P == 8
1033 puts("Running a 32-bit JVM is not supported on this platform.");
1039 #if SIZEOF_VOID_P == 4
1040 puts("Running a 64-bit JVM is not supported on this platform.");
1046 /* forget old classpath and set the argument as new classpath */
1047 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1049 _Jv_classpath = MNEW(char, strlen(opt_arg) + strlen("0"));
1050 strcpy(_Jv_classpath, opt_arg);
1052 #if defined(ENABLE_JAVASE)
1053 properties_add("java.class.path", _Jv_classpath);
1058 for (i = 0; i < strlen(opt_arg); i++) {
1059 if (opt_arg[i] == '=') {
1061 properties_add(opt_arg, opt_arg + i + 1);
1066 /* if no '=' is given, just create an empty property */
1068 properties_add(opt_arg, "");
1073 case OPT_BOOTCLASSPATH:
1074 /* Forget default bootclasspath and set the argument as
1075 new boot classpath. */
1077 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1079 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen("0"));
1080 strcpy(_Jv_bootclasspath, opt_arg);
1082 #if defined(ENABLE_JAVASE)
1083 properties_add("java.boot.class.path", _Jv_bootclasspath);
1084 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1088 case OPT_BOOTCLASSPATH_A:
1089 /* append to end of bootclasspath */
1091 len = strlen(_Jv_bootclasspath);
1093 _Jv_bootclasspath = MREALLOC(_Jv_bootclasspath,
1097 strlen(opt_arg) + strlen("0"));
1099 strcat(_Jv_bootclasspath, ":");
1100 strcat(_Jv_bootclasspath, opt_arg);
1102 #if defined(ENABLE_JAVASE)
1103 properties_add("java.boot.class.path", _Jv_bootclasspath);
1104 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1108 case OPT_BOOTCLASSPATH_P:
1109 /* prepend in front of bootclasspath */
1111 cp = _Jv_bootclasspath;
1114 _Jv_bootclasspath = MNEW(char, strlen(opt_arg) + strlen(":") +
1117 strcpy(_Jv_bootclasspath, opt_arg);
1118 strcat(_Jv_bootclasspath, ":");
1119 strcat(_Jv_bootclasspath, cp);
1121 MFREE(cp, char, len);
1123 #if defined(ENABLE_JAVASE)
1124 properties_add("java.boot.class.path", _Jv_bootclasspath);
1125 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1129 case OPT_BOOTCLASSPATH_C:
1130 /* use as Java core library, but prepend VM interface classes */
1132 MFREE(_Jv_bootclasspath, char, strlen(_Jv_bootclasspath));
1134 len = strlen(CACAO_VM_ZIP) +
1139 _Jv_bootclasspath = MNEW(char, len);
1141 strcpy(_Jv_bootclasspath, CACAO_VM_ZIP);
1142 strcat(_Jv_bootclasspath, ":");
1143 strcat(_Jv_bootclasspath, opt_arg);
1145 #if defined(ENABLE_JAVASE)
1146 properties_add("java.boot.class.path", _Jv_bootclasspath);
1147 properties_add("sun.boot.class.path", _Jv_bootclasspath);
1151 #if defined(ENABLE_JVMTI)
1153 /* this option exists only for compatibility reasons */
1157 /* I don't know yet what Xnoagent should do. This is only for
1158 compatiblity with eclipse - motse */
1167 strlen(CACAO_LIBDIR) +
1168 strlen("/libjdwp.so=") +
1172 agentarg = MNEW(char, len);
1174 strcpy(agentarg, CACAO_LIBDIR);
1175 strcat(agentarg, "/libjdwp.so=");
1176 strcat(agentarg, &opt_arg[1]);
1193 c = opt_arg[strlen(opt_arg) - 1];
1195 if ((c == 'k') || (c == 'K')) {
1196 j = atoi(opt_arg) * 1024;
1198 } else if ((c == 'm') || (c == 'M')) {
1199 j = atoi(opt_arg) * 1024 * 1024;
1205 opt_heapmaxsize = j;
1206 else if (opt == OPT_MS)
1207 opt_heapstartsize = j;
1218 if (strcmp("class", opt_arg) == 0) {
1219 opt_verboseclass = true;
1221 else if (strcmp("gc", opt_arg) == 0) {
1222 opt_verbosegc = true;
1224 else if (strcmp("jni", opt_arg) == 0) {
1225 opt_verbosejni = true;
1227 #if !defined(NDEBUG)
1228 else if (strcmp("jit", opt_arg) == 0) {
1232 compileverbose = true;
1234 else if (strcmp("threads", opt_arg) == 0) {
1235 opt_verbosethreads = true;
1239 printf("Unknown -verbose option: %s\n", opt_arg);
1244 case OPT_DEBUGCOLOR:
1245 opt_debugcolor = true;
1248 #if defined(ENABLE_VERIFIER) && defined(TYPECHECK_VERBOSE)
1250 opt_typecheckverbose = true;
1259 case OPT_FULLVERSION:
1263 case OPT_SHOWVERSION:
1271 #if defined(ENABLE_VERIFIER)
1277 #if defined(ENABLE_STATISTICS)
1279 opt_getcompilingtime = true;
1280 opt_getloadingtime = true;
1293 for (i = 0; i < strlen(opt_arg); i++) {
1294 switch (opt_arg[i]) {
1296 checkbounds = false;
1309 makeinitializations = false;
1312 #if !defined(NDEBUG)
1316 makeinitializations = false;
1321 opt_method = opt_arg;
1322 makeinitializations = false;
1326 opt_signature = opt_arg;
1330 case OPT_SHOW: /* Display options */
1331 for (i = 0; i < strlen(opt_arg); i++) {
1332 switch (opt_arg[i]) {
1334 showconstantpool = true;
1346 opt_showintermediate = true;
1347 compileverbose = true;
1350 #if defined(ENABLE_DISASSEMBLER)
1352 opt_showdisassemble = true;
1353 compileverbose = true;
1357 opt_shownops = true;
1361 opt_showexceptionstubs = true;
1365 opt_shownativestub = true;
1370 opt_showddatasegment = true;
1379 #if defined(ENABLE_LOOP)
1385 #if defined(ENABLE_INLINING)
1386 #if defined(ENABLE_INLINING_DEBUG)
1387 case OPT_INLINE_DEBUG_ALL:
1388 opt_inline_debug_all = true;
1390 case OPT_INLINE_DEBUG_END:
1391 opt_inline_debug_end_counter = atoi(opt_arg);
1393 case OPT_INLINE_DEBUG_MIN:
1394 opt_inline_debug_min_size = atoi(opt_arg);
1396 case OPT_INLINE_DEBUG_MAX:
1397 opt_inline_debug_max_size = atoi(opt_arg);
1399 #endif /* defined(ENABLE_INLINING_DEBUG) */
1400 #if !defined(NDEBUG)
1401 case OPT_INLINE_LOG:
1402 opt_inline_debug_log = true;
1404 #endif /* !defined(NDEBUG) */
1407 opt_inlining = true;
1409 #endif /* defined(ENABLE_INLINING) */
1411 #if defined(ENABLE_IFCONV)
1417 #if defined(ENABLE_LSRA) || defined(ENABLE_SSA)
1432 options_xx(opt_arg);
1436 /* currently ignored */
1440 /* currently ignored */
1444 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = true;
1448 _Jv_jvm->Java_java_lang_VMClassLoader_defaultAssertionStatus = false;
1451 #if defined(ENABLE_PROFILING)
1452 case OPT_PROF_OPTION:
1453 /* use <= to get the last \0 too */
1455 for (i = 0, j = 0; i <= strlen(opt_arg); i++) {
1456 if (opt_arg[i] == ',')
1459 if (opt_arg[i] == '\0') {
1460 if (strcmp("bb", opt_arg + j) == 0)
1464 printf("Unknown option: -Xprof:%s\n", opt_arg + j);
1468 /* set k to next char */
1481 #if defined(ENABLE_JIT)
1484 printf("-Xjit option not enabled.\n");
1490 #if defined(ENABLE_INTRP)
1493 printf("-Xint option not enabled.\n");
1498 #if defined(ENABLE_INTRP)
1499 case OPT_STATIC_SUPERS:
1500 opt_static_supers = atoi(opt_arg);
1503 case OPT_NO_DYNAMIC:
1504 opt_no_dynamic = true;
1507 case OPT_NO_REPLICATION:
1508 opt_no_replication = true;
1511 case OPT_NO_QUICKSUPER:
1512 opt_no_quicksuper = true;
1520 #if defined(ENABLE_DEBUG_FILTER)
1521 case OPT_FILTER_VERBOSECALL_INCLUDE:
1522 opt_filter_verbosecall_include = opt_arg;
1525 case OPT_FILTER_VERBOSECALL_EXCLUDE:
1526 opt_filter_verbosecall_exclude = opt_arg;
1529 case OPT_FILTER_SHOW_METHOD:
1530 opt_filter_show_method = opt_arg;
1535 printf("Unknown option: %s\n",
1536 vm_args->options[opt_index].optionString);
1541 /* get the main class *****************************************************/
1543 if (opt_index < vm_args->nOptions) {
1544 mainstring = vm_args->options[opt_index++].optionString;
1546 /* Put the jar file into the classpath (if any). */
1548 if (opt_jar == true) {
1549 /* free old classpath */
1551 MFREE(_Jv_classpath, char, strlen(_Jv_classpath));
1553 /* put jarfile into classpath */
1555 _Jv_classpath = MNEW(char, strlen(mainstring) + strlen("0"));
1557 strcpy(_Jv_classpath, mainstring);
1559 #if defined(ENABLE_JAVASE)
1560 properties_add("java.class.path", _Jv_classpath);
1564 /* replace .'s with /'s in classname */
1566 for (i = strlen(mainstring) - 1; i >= 0; i--)
1567 if (mainstring[i] == '.')
1568 mainstring[i] = '/';
1572 #if defined(ENABLE_JVMTI)
1574 jvmti_set_phase(JVMTI_PHASE_ONLOAD);
1575 jvmti_agentload(agentarg, agentbypath, &handle, &libname);
1578 MFREE(agentarg, char, strlen(agentarg));
1580 jvmti_set_phase(JVMTI_PHASE_PRIMORDIAL);
1584 /* initialize this JVM ****************************************************/
1586 vm_initializing = true;
1588 /* initialize the garbage collector */
1590 gc_init(opt_heapmaxsize, opt_heapstartsize);
1592 #if defined(ENABLE_THREADS)
1593 /* AFTER: gc_init (directly after, as this initializes the
1594 stopworldlock lock */
1599 /* install architecture dependent signal handlers */
1602 vm_abort("vm_create: signal_init failed");
1604 #if defined(ENABLE_INTRP)
1605 /* Allocate main thread stack on the Java heap. */
1608 intrp_main_stack = GCMNEW(u1, opt_stacksize);
1609 MSET(intrp_main_stack, 0, u1, opt_stacksize);
1613 /* AFTER: threads_preinit */
1616 vm_abort("vm_create: string_init failed");
1618 /* AFTER: threads_preinit */
1621 vm_abort("vm_create: utf8_init failed");
1623 /* AFTER: thread_preinit */
1626 vm_abort("vm_create: suck_init failed");
1628 suck_add_from_property("java.endorsed.dirs");
1630 /* Now we have all options handled and we can print the version
1633 AFTER: suck_add_from_property("java.endorsed.dirs"); */
1638 /* AFTER: utf8_init */
1640 suck_add(_Jv_bootclasspath);
1642 /* initialize the classcache hashtable stuff: lock, hashtable
1643 (must be done _after_ threads_preinit) */
1645 if (!classcache_init())
1646 vm_abort("vm_create: classcache_init failed");
1648 /* initialize the memory subsystem (must be done _after_
1652 vm_abort("vm_create: memory_init failed");
1654 /* initialize the finalizer stuff (must be done _after_
1657 if (!finalizer_init())
1658 vm_abort("vm_create: finalizer_init failed");
1660 /* initialize the codegen subsystems */
1664 /* initializes jit compiler */
1668 /* machine dependent initialization */
1670 #if defined(ENABLE_JIT)
1671 # if defined(ENABLE_INTRP)
1681 /* initialize the loader subsystems (must be done _after_
1685 vm_abort("vm_create: loader_init failed");
1687 /* Link some important VM classes. */
1688 /* AFTER: utf8_init */
1691 vm_abort("vm_create: linker_init failed");
1693 if (!primitive_init())
1694 vm_abort("vm_create: primitive_init failed");
1696 if (!exceptions_init())
1697 vm_abort("vm_create: exceptions_init failed");
1699 if (!builtin_init())
1700 vm_abort("vm_create: builtin_init failed");
1702 /* Initialize the native subsystem. */
1703 /* BEFORE: threads_init */
1706 vm_abort("vm_create: native_init failed");
1708 /* Register the native methods implemented in the VM. */
1709 /* BEFORE: threads_init */
1711 if (!nativevm_preinit())
1712 vm_abort("vm_create: nativevm_preinit failed");
1714 #if defined(ENABLE_JNI)
1715 /* Initialize the JNI subsystem (must be done _before_
1716 threads_init, as threads_init can call JNI methods
1717 (e.g. NewGlobalRef). */
1720 vm_abort("vm_create: jni_init failed");
1723 #if defined(ENABLE_THREADS)
1724 if (!threads_init())
1725 vm_abort("vm_create: threads_init failed");
1728 /* Initialize the native VM subsystem. */
1729 /* AFTER: threads_init (at least for SUN's classes) */
1731 if (!nativevm_init())
1732 vm_abort("vm_create: nativevm_init failed");
1734 #if defined(ENABLE_PROFILING)
1735 /* initialize profiling */
1737 if (!profile_init())
1738 vm_abort("vm_create: profile_init failed");
1741 #if defined(ENABLE_THREADS)
1742 /* initialize recompilation */
1744 if (!recompile_init())
1745 vm_abort("vm_create: recompile_init failed");
1747 /* start the signal handler thread */
1749 #if defined(__LINUX__)
1750 /* XXX Remove for exact-GC. */
1751 if (threads_pthreads_implementation_nptl)
1753 if (!signal_start_thread())
1754 vm_abort("vm_create: signal_start_thread failed");
1756 /* finally, start the finalizer thread */
1758 if (!finalizer_start_thread())
1759 vm_abort("vm_create: finalizer_start_thread failed");
1761 # if !defined(NDEBUG)
1762 /* start the memory profiling thread */
1764 if (opt_ProfileMemoryUsage || opt_ProfileGCMemoryUsage)
1765 if (!memory_start_thread())
1766 vm_abort("vm_create: memory_start_thread failed");
1769 /* start the recompilation thread (must be done before the
1770 profiling thread) */
1772 if (!recompile_start_thread())
1773 vm_abort("vm_create: recompile_start_thread failed");
1775 # if defined(ENABLE_PROFILING)
1776 /* start the profile sampling thread */
1779 /* if (!profile_start_thread()) */
1780 /* vm_abort("vm_create: profile_start_thread failed"); */
1784 #if defined(ENABLE_JVMTI)
1786 /* add agent library to native library hashtable */
1787 native_hashtable_library_add(utf_new_char(libname), class_java_lang_Object->classloader, handle);
1791 /* increment the number of VMs */
1795 /* initialization is done */
1797 vm_initializing = false;
1799 /* everything's ok */
1805 /* vm_run **********************************************************************
1807 Runs the main-method of the passed class.
1809 *******************************************************************************/
1811 void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
1814 classinfo *mainclass;
1817 java_handle_objectarray_t *oa;
1824 #if !defined(NDEBUG)
1830 if (opt_method != NULL) {
1831 vm_compile_method();
1834 #endif /* !defined(NDEBUG) */
1836 /* should we run the main-method? */
1838 if (mainstring == NULL)
1841 /* set return value to OK */
1845 if (opt_jar == true) {
1846 /* open jar file with java.util.jar.JarFile */
1848 mainstring = vm_get_mainclass_from_jar(mainstring);
1850 if (mainstring == NULL)
1854 /* load the main class */
1856 mainutf = utf_new_char(mainstring);
1858 #if defined(ENABLE_JAVAME_CLDC1_1)
1859 mainclass = load_class_bootstrap(mainutf);
1861 mainclass = load_class_from_sysloader(mainutf);
1864 /* error loading class */
1866 e = exceptions_get_and_clear_exception();
1868 if ((e != NULL) || (mainclass == NULL)) {
1869 exceptions_throw_noclassdeffounderror_cause(e);
1870 exceptions_print_stacktrace();
1874 if (!link_class(mainclass)) {
1875 exceptions_print_stacktrace();
1879 /* find the `main' method of the main class */
1881 m = class_resolveclassmethod(mainclass,
1882 utf_new_char("main"),
1883 utf_new_char("([Ljava/lang/String;)V"),
1884 class_java_lang_Object,
1887 if (exceptions_get_exception()) {
1888 exceptions_print_stacktrace();
1892 /* there is no main method or it isn't static */
1894 if ((m == NULL) || !(m->flags & ACC_STATIC)) {
1895 exceptions_clear_exception();
1896 exceptions_throw_nosuchmethoderror(mainclass,
1897 utf_new_char("main"),
1898 utf_new_char("([Ljava/lang/String;)V"));
1900 exceptions_print_stacktrace();
1904 /* build argument array */
1906 oalength = vm_args->nOptions - opt_index;
1908 oa = builtin_anewarray(oalength, class_java_lang_String);
1910 for (i = 0; i < oalength; i++) {
1911 u = utf_new_char(vm_args->options[opt_index + i].optionString);
1912 s = javastring_new(u);
1914 LLNI_objectarray_element_set(oa, i, s);
1917 #ifdef TYPEINFO_DEBUG_TEST
1918 /* test the typeinfo system */
1921 /*class_showmethods(currentThread->group->header.vftbl->class); */
1923 #if defined(ENABLE_JVMTI)
1924 jvmti_set_phase(JVMTI_PHASE_LIVE);
1927 /* set ThreadMXBean variables */
1929 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
1930 _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
1932 if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
1933 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
1934 _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
1935 _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
1937 /* start the main thread */
1939 (void) vm_call_method(m, NULL, oa);
1941 /* exception occurred? */
1943 if (exceptions_get_exception()) {
1944 exceptions_print_stacktrace();
1948 /* unload the JavaVM */
1950 (void) vm_destroy(vm);
1958 /* vm_destroy ******************************************************************
1960 Unloads a Java VM and reclaims its resources.
1962 *******************************************************************************/
1964 s4 vm_destroy(JavaVM *vm)
1966 #if defined(ENABLE_THREADS)
1967 threads_join_all_threads();
1970 /* everything's ok */
1976 /* vm_exit *********************************************************************
1978 Calls java.lang.System.exit(I)V to exit the JavaVM correctly.
1980 *******************************************************************************/
1982 void vm_exit(s4 status)
1986 /* signal that we are exiting */
1990 assert(class_java_lang_System);
1991 assert(class_java_lang_System->state & CLASS_LOADED);
1993 #if defined(ENABLE_JVMTI)
1994 if (jvmti || (dbgcom!=NULL)) {
1995 jvmti_set_phase(JVMTI_PHASE_DEAD);
1996 if (jvmti) jvmti_agentunload();
2000 if (!link_class(class_java_lang_System)) {
2001 exceptions_print_stacktrace();
2005 /* call java.lang.System.exit(I)V */
2007 m = class_resolveclassmethod(class_java_lang_System,
2008 utf_new_char("exit"),
2010 class_java_lang_Object,
2014 exceptions_print_stacktrace();
2018 /* call the exit function with passed exit status */
2020 (void) vm_call_method(m, NULL, status);
2022 /* If we had an exception, just ignore the exception and exit with
2025 vm_shutdown(status);
2029 /* vm_shutdown *****************************************************************
2031 Terminates the system immediately without freeing memory explicitly
2032 (to be used only for abnormal termination).
2034 *******************************************************************************/
2036 void vm_shutdown(s4 status)
2039 #if defined(ENABLE_STATISTICS)
2040 || opt_getcompilingtime || opt_stat
2044 log_text("CACAO terminated by shutdown");
2045 dolog("Exit status: %d\n", (s4) status);
2049 #if defined(ENABLE_JVMTI)
2050 /* terminate cacaodbgserver */
2052 pthread_mutex_lock(&dbgcomlock);
2054 pthread_mutex_unlock(&dbgcomlock);
2055 jvmti_cacaodbgserver_quit();
2063 /* vm_exit_handler *************************************************************
2065 The exit_handler function is called upon program termination.
2067 ATTENTION: Don't free system resources here! Some threads may still
2068 be running as this is called from VMRuntime.exit(). The OS does the
2071 *******************************************************************************/
2073 void vm_exit_handler(void)
2075 #if !defined(NDEBUG)
2077 class_showmethods(mainclass);
2079 if (showconstantpool)
2080 class_showconstantpool(mainclass);
2085 # if defined(ENABLE_PROFILING)
2087 profile_printstats();
2089 #endif /* !defined(NDEBUG) */
2091 #if defined(ENABLE_RT_TIMING)
2092 rt_timing_print_time_stats(stderr);
2095 #if defined(ENABLE_CYCLES_STATS)
2096 builtin_print_cycles_stats(stderr);
2097 stacktrace_print_cycles_stats(stderr);
2101 #if defined(ENABLE_STATISTICS)
2102 || opt_getcompilingtime || opt_stat
2106 log_text("CACAO terminated");
2108 #if defined(ENABLE_STATISTICS)
2111 #ifdef TYPECHECK_STATISTICS
2112 typecheck_print_statistics(get_logfile());
2116 if (opt_getcompilingtime)
2118 #endif /* defined(ENABLE_STATISTICS) */
2120 /* vm_print_profile(stderr);*/
2124 /* vm_abort ********************************************************************
2126 Prints an error message and aborts the VM.
2128 *******************************************************************************/
2130 void vm_abort(const char *text, ...)
2134 /* print the log message */
2139 log_vprint(text, ap);
2144 /* now abort the VM */
2150 /* vm_get_mainclass_from_jar ***************************************************
2152 Gets the name of the main class from a JAR's manifest file.
2154 *******************************************************************************/
2156 static char *vm_get_mainclass_from_jar(char *mainstring)
2163 c = load_class_from_sysloader(utf_new_char("java/util/jar/JarFile"));
2166 exceptions_print_stacktrace();
2170 /* create JarFile object */
2175 exceptions_print_stacktrace();
2179 m = class_resolveclassmethod(c,
2181 utf_java_lang_String__void,
2182 class_java_lang_Object,
2186 exceptions_print_stacktrace();
2190 s = javastring_new_from_ascii(mainstring);
2192 (void) vm_call_method(m, o, s);
2194 if (exceptions_get_exception()) {
2195 exceptions_print_stacktrace();
2199 /* get manifest object */
2201 m = class_resolveclassmethod(c,
2202 utf_new_char("getManifest"),
2203 utf_new_char("()Ljava/util/jar/Manifest;"),
2204 class_java_lang_Object,
2208 exceptions_print_stacktrace();
2212 o = vm_call_method(m, o);
2215 fprintf(stderr, "Could not get manifest from %s (invalid or corrupt jarfile?)\n", mainstring);
2220 /* get Main Attributes */
2222 m = class_resolveclassmethod(o->vftbl->class,
2223 utf_new_char("getMainAttributes"),
2224 utf_new_char("()Ljava/util/jar/Attributes;"),
2225 class_java_lang_Object,
2229 exceptions_print_stacktrace();
2233 o = vm_call_method(m, o);
2236 fprintf(stderr, "Could not get main attributes from %s (invalid or corrupt jarfile?)\n", mainstring);
2241 /* get property Main-Class */
2243 m = class_resolveclassmethod(o->vftbl->class,
2244 utf_new_char("getValue"),
2245 utf_new_char("(Ljava/lang/String;)Ljava/lang/String;"),
2246 class_java_lang_Object,
2250 exceptions_print_stacktrace();
2254 s = javastring_new_from_ascii("Main-Class");
2256 o = vm_call_method(m, o, s);
2259 exceptions_print_stacktrace();
2263 return javastring_tochar(o);
2267 /* vm_compile_all **************************************************************
2269 Compile all methods found in the bootclasspath.
2271 *******************************************************************************/
2273 #if !defined(NDEBUG)
2274 static void vm_compile_all(void)
2279 classcache_name_entry *nmen;
2280 classcache_class_entry *clsen;
2283 /* create all classes found in the bootclasspath */
2284 /* XXX currently only works with zip/jar's */
2286 loader_load_all_classes();
2288 /* link all classes */
2290 for (slot = 0; slot < hashtable_classcache.size; slot++) {
2291 nmen = (classcache_name_entry *) hashtable_classcache.ptr[slot];
2293 for (; nmen; nmen = nmen->hashlink) {
2294 /* iterate over all class entries */
2296 for (clsen = nmen->classes; clsen; clsen = clsen->next) {
2297 c = clsen->classobj;
2302 if (!(c->state & CLASS_LINKED)) {
2303 if (!link_class(c)) {
2304 fprintf(stderr, "Error linking: ");
2305 utf_fprint_printable_ascii_classname(stderr, c->name);
2306 fprintf(stderr, "\n");
2308 /* print out exception and cause */
2310 exceptions_print_current_exception();
2312 /* goto next class */
2318 /* compile all class methods */
2320 for (i = 0; i < c->methodscount; i++) {
2321 m = &(c->methods[i]);
2323 if (m->jcode != NULL) {
2324 if (!jit_compile(m)) {
2325 fprintf(stderr, "Error compiling: ");
2326 utf_fprint_printable_ascii_classname(stderr, c->name);
2327 fprintf(stderr, ".");
2328 utf_fprint_printable_ascii(stderr, m->name);
2329 utf_fprint_printable_ascii(stderr, m->descriptor);
2330 fprintf(stderr, "\n");
2332 /* print out exception and cause */
2334 exceptions_print_current_exception();
2342 #endif /* !defined(NDEBUG) */
2345 /* vm_compile_method ***********************************************************
2347 Compile a specific method.
2349 *******************************************************************************/
2351 #if !defined(NDEBUG)
2352 static void vm_compile_method(void)
2356 /* create, load and link the main class */
2358 mainclass = load_class_bootstrap(utf_new_char(mainstring));
2360 if (mainclass == NULL)
2361 exceptions_print_stacktrace();
2363 if (!link_class(mainclass))
2364 exceptions_print_stacktrace();
2366 if (opt_signature != NULL) {
2367 m = class_resolveclassmethod(mainclass,
2368 utf_new_char(opt_method),
2369 utf_new_char(opt_signature),
2374 m = class_resolveclassmethod(mainclass,
2375 utf_new_char(opt_method),
2382 vm_abort("vm_compile_method: java.lang.NoSuchMethodException: %s.%s",
2383 opt_method, opt_signature ? opt_signature : "");
2387 #endif /* !defined(NDEBUG) */
2390 /* vm_array_store_int **********************************************************
2392 Helper function to store an integer into the argument array, taking
2393 care of architecture specific issues.
2395 *******************************************************************************/
2397 static void vm_array_store_int(uint64_t *array, paramdesc *pd, int32_t value)
2401 if (!pd->inmemory) {
2403 array[index] = (int64_t) value;
2406 index = ARG_CNT + pd->index;
2407 #if SIZEOF_VOID_P == 8
2408 array[index] = (int64_t) value;
2410 # if WORDS_BIGENDIAN == 1
2411 array[index] = ((int64_t) value) << 32;
2413 array[index] = (int64_t) value;
2420 /* vm_array_store_lng **********************************************************
2422 Helper function to store a long into the argument array, taking
2423 care of architecture specific issues.
2425 *******************************************************************************/
2427 static void vm_array_store_lng(uint64_t *array, paramdesc *pd, int64_t value)
2431 #if SIZEOF_VOID_P == 8
2435 index = ARG_CNT + pd->index;
2437 array[index] = value;
2439 if (!pd->inmemory) {
2440 /* move low and high 32-bits into it's own argument slot */
2442 index = GET_LOW_REG(pd->index);
2443 array[index] = value & 0x00000000ffffffff;
2445 index = GET_HIGH_REG(pd->index);
2446 array[index] = value >> 32;
2449 index = ARG_CNT + pd->index;
2450 array[index] = value;
2456 /* vm_array_store_flt **********************************************************
2458 Helper function to store a float into the argument array, taking
2459 care of architecture specific issues.
2461 *******************************************************************************/
2463 static void vm_array_store_flt(uint64_t *array, paramdesc *pd, uint64_t value)
2467 if (!pd->inmemory) {
2468 #if defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2471 index = INT_ARG_CNT + pd->index;
2473 #if WORDS_BIGENDIAN == 1 && !defined(__POWERPC__) && !defined(__POWERPC64__) && !defined(__S390__)
2474 array[index] = value >> 32;
2476 array[index] = value;
2480 index = ARG_CNT + pd->index;
2481 #if defined(__SPARC_64__)
2482 array[index] = value >> 32;
2484 array[index] = value;
2490 /* vm_array_store_dbl **********************************************************
2492 Helper function to store a double into the argument array, taking
2493 care of architecture specific issues.
2495 *******************************************************************************/
2497 static void vm_array_store_dbl(uint64_t *array, paramdesc *pd, uint64_t value)
2501 if (!pd->inmemory) {
2502 #if SIZEOF_VOID_P != 8 && defined(SUPPORT_PASS_FLOATARGS_IN_INTREGS)
2503 index = GET_LOW_REG(pd->index);
2504 array[index] = value & 0x00000000ffffffff;
2506 index = GET_HIGH_REG(pd->index);
2507 array[index] = value >> 32;
2509 index = INT_ARG_CNT + pd->index;
2510 array[index] = value;
2514 index = ARG_CNT + pd->index;
2515 array[index] = value;
2520 /* vm_array_store_adr **********************************************************
2522 Helper function to store an address into the argument array, taking
2523 care of architecture specific issues.
2525 *******************************************************************************/
2527 static void vm_array_store_adr(uint64_t *array, paramdesc *pd, void *value)
2531 if (!pd->inmemory) {
2532 #if defined(HAS_ADDRESS_REGISTER_FILE)
2533 /* When the architecture has address registers, place them
2534 after integer and float registers. */
2536 index = INT_ARG_CNT + FLT_ARG_CNT + pd->index;
2540 array[index] = (uint64_t) (intptr_t) value;
2543 index = ARG_CNT + pd->index;
2544 #if SIZEOF_VOID_P == 8
2545 array[index] = (uint64_t) (intptr_t) value;
2547 # if WORDS_BIGENDIAN == 1
2548 array[index] = ((uint64_t) (intptr_t) value) << 32;
2550 array[index] = (uint64_t) (intptr_t) value;
2557 /* vm_array_from_valist ********************************************************
2561 *******************************************************************************/
2563 uint64_t *vm_array_from_valist(methodinfo *m, java_object_t *o, va_list ap)
2572 /* get the descriptors */
2576 td = md->paramtypes;
2578 /* allocate argument array */
2580 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2582 /* if method is non-static fill first block and skip `this' pointer */
2587 /* the `this' pointer */
2588 vm_array_store_adr(array, pd, o);
2595 for (; i < md->paramcount; i++, pd++, td++) {
2598 value.i = va_arg(ap, int32_t);
2599 vm_array_store_int(array, pd, value.i);
2603 value.l = va_arg(ap, int64_t);
2604 vm_array_store_lng(array, pd, value.l);
2608 #if defined(__ALPHA__) || defined(__POWERPC__) || defined(__POWERPC64__)
2609 /* This is required to load the correct float value in
2612 value.d = (double) va_arg(ap, double);
2614 value.f = (float) va_arg(ap, double);
2616 vm_array_store_flt(array, pd, value.l);
2620 value.d = va_arg(ap, double);
2621 vm_array_store_dbl(array, pd, value.l);
2625 value.a = va_arg(ap, void*);
2626 vm_array_store_adr(array, pd, value.a);
2635 /* vm_array_from_jvalue ********************************************************
2639 *******************************************************************************/
2641 static uint64_t *vm_array_from_jvalue(methodinfo *m, java_object_t *o,
2651 /* get the descriptors */
2655 td = md->paramtypes;
2657 /* allocate argument array */
2659 #if defined(HAS_ADDRESS_REGISTER_FILE)
2660 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + ADR_ARG_CNT + md->memuse);
2662 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2665 /* if method is non-static fill first block and skip `this' pointer */
2670 /* the `this' pointer */
2671 vm_array_store_adr(array, pd, o);
2678 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2679 switch (td->decltype) {
2681 vm_array_store_int(array, pd, args[j].i);
2685 vm_array_store_lng(array, pd, args[j].j);
2689 vm_array_store_flt(array, pd, args[j].j);
2693 vm_array_store_dbl(array, pd, args[j].j);
2697 vm_array_store_adr(array, pd, args[j].l);
2706 /* vm_array_from_objectarray ***************************************************
2710 *******************************************************************************/
2712 uint64_t *vm_array_from_objectarray(methodinfo *m, java_object_t *o,
2713 java_handle_objectarray_t *params)
2719 java_handle_t *param;
2726 /* get the descriptors */
2730 td = md->paramtypes;
2732 /* allocate argument array */
2734 array = DMNEW(uint64_t, INT_ARG_CNT + FLT_ARG_CNT + md->memuse);
2736 /* if method is non-static fill first block and skip `this' pointer */
2742 vm_array_store_adr(array, pd, o);
2749 for (j = 0; i < md->paramcount; i++, j++, pd++, td++) {
2750 LLNI_objectarray_element_get(params, j, param);
2757 /* convert the value according to its declared type */
2759 c = param->vftbl->class;
2760 type = primitive_type_get_by_wrapperclass(c);
2762 switch (td->decltype) {
2763 case PRIMITIVETYPE_BOOLEAN:
2765 case PRIMITIVETYPE_BOOLEAN:
2766 /* This type is OK. */
2773 case PRIMITIVETYPE_BYTE:
2775 case PRIMITIVETYPE_BYTE:
2776 /* This type is OK. */
2783 case PRIMITIVETYPE_CHAR:
2785 case PRIMITIVETYPE_CHAR:
2786 /* This type is OK. */
2793 case PRIMITIVETYPE_SHORT:
2795 case PRIMITIVETYPE_BYTE:
2796 case PRIMITIVETYPE_SHORT:
2797 /* These types are OK. */
2804 case PRIMITIVETYPE_INT:
2806 case PRIMITIVETYPE_BYTE:
2807 case PRIMITIVETYPE_SHORT:
2808 case PRIMITIVETYPE_INT:
2809 /* These types are OK. */
2817 vm_abort("vm_array_from_objectarray: invalid type %d",
2821 value = primitive_unbox(param);
2822 vm_array_store_int(array, pd, value.i);
2829 c = param->vftbl->class;
2830 type = primitive_type_get_by_wrapperclass(c);
2832 assert(td->decltype == PRIMITIVETYPE_LONG);
2835 case PRIMITIVETYPE_BYTE:
2836 case PRIMITIVETYPE_SHORT:
2837 case PRIMITIVETYPE_INT:
2838 case PRIMITIVETYPE_LONG:
2839 /* These types are OK. */
2845 value = primitive_unbox(param);
2846 vm_array_store_lng(array, pd, value.l);
2853 c = param->vftbl->class;
2854 type = primitive_type_get_by_wrapperclass(c);
2856 assert(td->decltype == PRIMITIVETYPE_FLOAT);
2859 case PRIMITIVETYPE_FLOAT:
2860 /* This type is OK. */
2866 value = primitive_unbox(param);
2867 vm_array_store_flt(array, pd, value.l);
2874 c = param->vftbl->class;
2875 type = primitive_type_get_by_wrapperclass(c);
2877 assert(td->decltype == PRIMITIVETYPE_DOUBLE);
2880 case PRIMITIVETYPE_FLOAT:
2881 case PRIMITIVETYPE_DOUBLE:
2882 /* These types are OK. */
2888 value = primitive_unbox(param);
2889 vm_array_store_dbl(array, pd, value.l);
2893 if (!resolve_class_from_typedesc(td, true, true, &c))
2896 if (param != NULL) {
2897 if (td->arraydim > 0) {
2898 if (!builtin_arrayinstanceof(param, c))
2902 if (!builtin_instanceof(param, c))
2907 vm_array_store_adr(array, pd, param);
2911 vm_abort("vm_array_from_objectarray: invalid type %d", td->type);
2918 exceptions_throw_illegalargumentexception();
2923 /* vm_call_method **************************************************************
2925 Calls a Java method with a variable number of arguments.
2927 *******************************************************************************/
2929 #define VM_CALL_METHOD(name, type) \
2930 type vm_call_method##name(methodinfo *m, java_handle_t *o, ...) \
2936 value = vm_call_method##name##_valist(m, o, ap); \
2942 VM_CALL_METHOD(, java_handle_t *)
2943 VM_CALL_METHOD(_int, int32_t)
2944 VM_CALL_METHOD(_long, int64_t)
2945 VM_CALL_METHOD(_float, float)
2946 VM_CALL_METHOD(_double, double)
2949 /* vm_call_method_valist *******************************************************
2951 Calls a Java method with a variable number of arguments, passed via
2954 *******************************************************************************/
2956 #define VM_CALL_METHOD_VALIST(name, type) \
2957 type vm_call_method##name##_valist(methodinfo *m, java_handle_t *o, \
2964 dumpsize = dump_size(); \
2965 array = vm_array_from_valist(m, o, ap); \
2966 value = vm_call##name##_array(m, array); \
2967 dump_release(dumpsize); \
2972 VM_CALL_METHOD_VALIST(, java_handle_t *)
2973 VM_CALL_METHOD_VALIST(_int, int32_t)
2974 VM_CALL_METHOD_VALIST(_long, int64_t)
2975 VM_CALL_METHOD_VALIST(_float, float)
2976 VM_CALL_METHOD_VALIST(_double, double)
2979 /* vm_call_method_jvalue *******************************************************
2981 Calls a Java method with a variable number of arguments, passed via
2984 *******************************************************************************/
2986 #define VM_CALL_METHOD_JVALUE(name, type) \
2987 type vm_call_method##name##_jvalue(methodinfo *m, java_handle_t *o, \
2988 const jvalue *args) \
2994 dumpsize = dump_size(); \
2995 array = vm_array_from_jvalue(m, o, args); \
2996 value = vm_call##name##_array(m, array); \
2997 dump_release(dumpsize); \
3002 VM_CALL_METHOD_JVALUE(, java_handle_t *)
3003 VM_CALL_METHOD_JVALUE(_int, int32_t)
3004 VM_CALL_METHOD_JVALUE(_long, int64_t)
3005 VM_CALL_METHOD_JVALUE(_float, float)
3006 VM_CALL_METHOD_JVALUE(_double, double)
3009 /* vm_call_array ***************************************************************
3011 Calls a Java method with a variable number of arguments, passed via
3014 *******************************************************************************/
3016 #define VM_CALL_ARRAY(name, type) \
3017 type vm_call##name##_array(methodinfo *m, uint64_t *array) \
3023 md = m->parseddesc; \
3025 if (m->code == NULL) \
3026 if (!jit_compile(m)) \
3029 pv = m->code->entrypoint; \
3031 STATISTICS(count_calls_native_to_java++); \
3033 value = asm_vm_call_method##name(pv, array, md->memuse); \
3038 VM_CALL_ARRAY(, java_handle_t *)
3039 VM_CALL_ARRAY(_int, int32_t)
3040 VM_CALL_ARRAY(_long, int64_t)
3041 VM_CALL_ARRAY(_float, float)
3042 VM_CALL_ARRAY(_double, double)
3046 * These are local overrides for various environment variables in Emacs.
3047 * Please do not remove this and leave it at the end of the file, where
3048 * Emacs will automagically detect them.
3049 * ---------------------------------------------------------------------
3052 * indent-tabs-mode: t