fprintf/vfprintf dependency fix
[cacao.git] / src / vm / vm.cpp
index 7aff240f09e7f86f220e1acd67f94741a119a4f5..ef1e93dbcdba2d659f596b7ee0872a423f06820c 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/vm.cpp - VM startup and shutdown functions
 
-   Copyright (C) 1996-2005, 2006, 2007, 2008
+   Copyright (C) 1996-2005, 2006, 2007, 2008, 2009, 2010
    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
@@ -69,6 +69,7 @@
 #include "vm/finalizer.hpp"
 #include "vm/global.h"
 #include "vm/globals.hpp"
+#include "vm/hook.hpp"
 #include "vm/initialize.hpp"
 #include "vm/options.h"
 #include "vm/os.hpp"
@@ -389,7 +390,7 @@ opt_struct opts[] = {
 
 *******************************************************************************/
 
-void usage(void)
+static void usage(void)
 {
        puts("Usage: cacao [-options] classname [arguments]");
        puts("               (to run a class file)");
@@ -539,9 +540,9 @@ static void XXusage(void)
 static void version(bool opt_exit)
 {
        puts("java version \""JAVA_VERSION"\"");
-       puts("CACAO version "VERSION"\n");
+       puts("CACAO version "VERSION_FULL"\n");
 
-       puts("Copyright (C) 1996-2005, 2006, 2007, 2008");
+       puts("Copyright (C) 1996-2005, 2006, 2007, 2008, 2009, 2010, 2011");
        puts("CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO");
        puts("This is free software; see the source for copying conditions.  There is NO");
        puts("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
@@ -717,7 +718,7 @@ VM::VM(JavaVMInitArgs* vm_args)
           is smaller or equal than the assumption made in
           src/vm/class.hpp. */
 
-#warning FIXME We need to check the size of java.lang.Class!!!
+// FIXME We need to check the size of java.lang.Class!!!
 //     if (sizeof(java_lang_Class) > sizeof(dummy_java_lang_Class))
 //             vm_abort("vm_create: java_lang_Class structure is bigger than classinfo.object (%d > %d)", sizeof(java_lang_Class), sizeof(dummy_java_lang_Class));
 
@@ -1283,6 +1284,9 @@ VM::VM(JavaVMInitArgs* vm_args)
 
        utf8_init();
 
+       // Hook point before the VM is initialized.
+       Hook::vm_preinit();
+
 #if defined(ENABLE_JVMTI)
        // AFTER: utf8_init
        if (!_nativeagents.load_agents())
@@ -1444,11 +1448,21 @@ VM::VM(JavaVMInitArgs* vm_args)
        // Initialization is done, VM is created.
        _created      = true;
        _initializing = false;
-       
+
+       // Set the VM inittime.
+       _inittime = builtin_currenttimemillis();
+
+       // Hook point after the VM is initialized.
+       Hook::vm_init();
+
        // Print the run-time VM configuration after all stuff is set and
        // the VM is initialized.
        if (opt_PrintConfig)
                print_run_time_config();
+
+       // Start runtime agents after the VM is created.
+       if (!start_runtime_agents())
+               os::abort("vm_create: start_runtime_agents failed");
 }
 
 
@@ -1457,7 +1471,7 @@ VM::VM(JavaVMInitArgs* vm_args)
  */
 void VM::print_build_time_config(void)
 {
-       puts("CACAO "VERSION" configure/build options:");
+       puts("CACAO "VERSION_FULL" configure/build options:");
        puts("");
        puts("  ./configure: "VERSION_CONFIGURE_ARGS"");
 #if defined(__VERSION__)
@@ -1517,6 +1531,60 @@ void VM::print_run_time_config()
 }
 
 
+/**
+ * Start runtime agents which are provided by the JRE but need to be
+ * started explicitly by the VM.
+ */
+bool VM::start_runtime_agents()
+{
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
+
+       // Nothing to do.
+
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
+
+       // Check whether the management agent should be loaded.
+       if ((_properties.get("com.sun.management.jmxremote") != NULL) ||
+               (_properties.get("com.sun.management.snmp") != NULL))
+       {
+
+               // Load the management agent class.
+               classinfo* class_sun_management_Agent;
+               if (!(class_sun_management_Agent = load_class_from_sysloader(utf_new_char("sun/management/Agent"))))
+                       return false;
+
+               // Link the management agent class.
+               if (!link_class(class_sun_management_Agent))
+                       return false;
+
+               // Actually start the management agent.
+               methodinfo* m = class_resolveclassmethod(class_sun_management_Agent,
+                                                                                                utf_new_char("startAgent"),
+                                                                                                utf_void__void,
+                                                                                                class_java_lang_Object,
+                                                                                                false);
+
+               if (m == NULL)
+                       return false;
+
+               (void) vm_call_method(m, NULL);
+
+               if (exceptions_get_exception() != NULL)
+                       return false;
+       }
+
+#elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
+
+       // Nothing to do.
+
+#else
+# error unknown classpath configuration
+#endif
+
+       return true;
+}
+
+
 /* vm_run **********************************************************************
 
    Runs the main-method of the passed class.
@@ -1667,17 +1735,6 @@ void vm_run(JavaVM *vm, JavaVMInitArgs *vm_args)
        typeinfo_test();
 #endif
 
-       /* set ThreadMXBean variables */
-
-//     _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount++;
-//     _Jv_jvm->java_lang_management_ThreadMXBean_TotalStartedThreadCount++;
-
-//     if (_Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount >
-//             _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount)
-//             _Jv_jvm->java_lang_management_ThreadMXBean_PeakThreadCount =
-//                     _Jv_jvm->java_lang_management_ThreadMXBean_ThreadCount;
-#warning Move to C++
-
        /* start the main thread */
 
        (void) vm_call_method(m, NULL, oa.get_handle());
@@ -1732,10 +1789,12 @@ int vm_destroy(JavaVM *vm)
        threads_join_all_threads();
 #endif
 
+       // Hook point before the VM is actually destroyed.
+       Hook::vm_shutdown();
+
        /* VM is gone. */
 
 //     _created = false;
-#warning Move to C++
 
        /* Everything is ok. */
 
@@ -1756,7 +1815,6 @@ void vm_exit(s4 status)
        /* signal that we are exiting */
 
 //     _exiting = true;
-#warning Move to C++
 
        assert(class_java_lang_System);
        assert(class_java_lang_System->state & CLASS_LOADED);
@@ -2454,11 +2512,7 @@ java_handle_t *vm_call_method_objectarray(methodinfo *m, java_handle_t *o,
 
 extern "C" {
 
-JavaVM* VM_get_javavm()      { return VM::get_current()->get_javavm(); }
 JNIEnv* VM_get_jnienv()      { return VM::get_current()->get_jnienv(); }
-bool    VM_is_initializing() { return VM::get_current()->is_initializing(); }
-bool    VM_is_created()      { return VM::get_current()->is_created(); }
-int64_t VM_get_starttime()   { return VM::get_current()->get_starttime(); }
 
 void vm_abort(const char* text, ...)
 {