* src/vm/hook.hpp: Added new file for hook points.
authorMichael Starzinger <michi@complang.tuwien.ac.at>
Thu, 15 Oct 2009 13:05:05 +0000 (15:05 +0200)
committerMichael Starzinger <michi@complang.tuwien.ac.at>
Thu, 15 Oct 2009 13:05:05 +0000 (15:05 +0200)
* src/vm/Makefile.am (libvm_la_SOURCES): Added above file.

* src/native/native.cpp (resolve_method): Added native_resolved hook.
* src/threads/posix/thread-posix.cpp (threads_startup_thread): Added hooks.
* src/vm/linker.cpp (link_class): Added class_linked hook.
* src/vm/loader.cpp (load_class_from_classbuffer): Added class_loaded hook.
* src/vm/vm.cpp (VM::VM): Added vm_init hook.
* src/vm/jit/patcher-common.cpp (patcher_breakpoint): Added breakpoint hook.
* src/vm/jit/trace.cpp: Added hooks.

* src/cacao/cacao.cpp: Launcher is not allowed to interact with JVMTI.
* src/native/jni.cpp (jni_RegisterNatives): Removed bogus JVMTI events.
* src/native/vm/cldc1.1/java_lang_Object.cpp: Likewise.
* src/native/vm/gnuclasspath/java_lang_VMObject.cpp: Likewise.

13 files changed:
src/cacao/cacao.cpp
src/native/jni.cpp
src/native/native.cpp
src/native/vm/cldc1.1/java_lang_Object.cpp
src/native/vm/gnuclasspath/java_lang_VMObject.cpp
src/threads/posix/thread-posix.cpp
src/vm/Makefile.am
src/vm/hook.hpp [new file with mode: 0644]
src/vm/jit/patcher-common.cpp
src/vm/jit/trace.cpp
src/vm/linker.cpp
src/vm/loader.cpp
src/vm/vm.cpp

index 9bf7c0588469488b08f0d89c3e38cb604f1b8582..0745caa7b7970e60a295b36a771891e32ec6d530 100644 (file)
 #include "native/jni.hpp"
 #include "native/native.hpp"
 
-#if defined(ENABLE_JVMTI)
-# include "native/jvmti/jvmti.h"
-# include "native/jvmti/cacaodbg.h"
-#endif
-
 #include "vm/os.hpp"
 #include "vm/vm.hpp"
 
@@ -160,12 +155,6 @@ int main(int argc, char **argv)
 
        (void) VM_create(&vm, &env, vm_args);
 
-#if defined(ENABLE_JVMTI)
-# error This should be a JVMTI function.
-       Mutex_init(&dbgcomlock);
-       if (jvmti) jvmti_set_phase(JVMTI_PHASE_START);
-#endif
-
 #if defined(ENABLE_LIBJVM)
        libjvm_vm_run = os::dlsym(libjvm_handle, "vm_run");
 
index 4c1585e6cf70a54e0ed6a036b2aab6f68011041c..431b2c9bcab03833889a0835e59aee91e7622751 100644 (file)
@@ -2965,14 +2965,10 @@ jint jni_RegisterNatives(JNIEnv* env, jclass clazz, const JNINativeMethod* metho
 
        classinfo* c = LLNI_classinfo_unwrap(clazz);
 
-       /* XXX: if implemented this needs a call to jvmti_NativeMethodBind
-       if (jvmti) jvmti_NativeMethodBind(method, address,  new_address_ptr);
-       */
-
        NativeMethods& nm = VM::get_current()->get_nativemethods();
        nm.register_methods(c->name, methods, nMethods);
 
-    return 0;
+       return 0;
 }
 
 
index bc47238e881de7587ce3fe5a815a7cfa93748e92..e5e2c7c6272ec3aae80fce5c56006960ea78f5c2 100644 (file)
@@ -47,6 +47,7 @@
 #include "vm/exceptions.hpp"
 #include "vm/global.h"
 #include "vm/globals.hpp"
+#include "vm/hook.hpp"
 #include "vm/loader.hpp"
 #include "vm/options.h"
 #include "vm/os.hpp"
@@ -452,6 +453,9 @@ void* NativeMethods::resolve_method(methodinfo* m)
                exceptions_throw_unsatisfiedlinkerror(m->name);
        }
 
+       // Hook point just after method resolving finished.
+       Hook::native_resolved(m, symbol, &symbol);
+
        return symbol;
 }
 
index ac38a3751ed2a2cf2f4a2b5c34f97c354fb8f352..8d4fa016b57107a5a144bc658b0f2d0a965d9dbf 100644 (file)
@@ -109,20 +109,9 @@ JNIEXPORT void JNICALL Java_java_lang_Object_notifyAll(JNIEnv *env, jobject _thi
  */
 JNIEXPORT void JNICALL Java_java_lang_Object_wait(JNIEnv *env, jobject _this, jlong timeout)
 {
-#if defined(ENABLE_JVMTI)
-       /* Monitor Wait */
-       if (jvmti) jvmti_MonitorWaiting(true, _this, timeout);
-#endif
-
 #if defined(ENABLE_THREADS)
        lock_wait_for_object((java_handle_t *) _this, timeout, 0);
 #endif
-
-#if defined(ENABLE_JVMTI)
-       /* Monitor Waited */
-       /* XXX: How do you know if wait timed out ?*/
-       if (jvmti) jvmti_MonitorWaiting(false, _this, 0);
-#endif
 }
 
 } // extern "C"
index 769d1a071f9390dd00bc4aec1c55734bc54db02d..95e7e8cbb350f93c36213e1fdb468499cf0e0d11 100644 (file)
@@ -108,20 +108,9 @@ JNIEXPORT void JNICALL Java_java_lang_VMObject_notifyAll(JNIEnv* env, jclass cla
  */
 JNIEXPORT void JNICALL Java_java_lang_VMObject_wait(JNIEnv* env, jclass clazz, jobject o, jlong ms, jint ns)
 {
-#if defined(ENABLE_JVMTI)
-       /* Monitor Wait */
-       if (jvmti) jvmti_MonitorWaiting(true, o, ms);
-#endif
-
 #if defined(ENABLE_THREADS)
        lock_wait_for_object(o, ms, ns);
 #endif
-
-#if defined(ENABLE_JVMTI)
-       /* Monitor Waited */
-       /* XXX: How do you know if wait timed out ?*/
-       if (jvmti) jvmti_MonitorWaiting(false, o, 0);
-#endif
 }
 
 } // extern "C"
index 0efabc005dc959f30e088507ebd4e1c0439d7beb..636638628eafc07bd51e8ff80ffe9aeb6604496e 100644 (file)
@@ -66,6 +66,7 @@
 #include "vm/exceptions.hpp"
 #include "vm/global.h"
 #include "vm/globals.hpp"
+#include "vm/hook.hpp"
 #include "vm/javaobjects.hpp"
 #include "vm/options.h"
 #include "vm/os.hpp"
 # include "mm/boehm-gc/include/gc.h"
 #endif
 
-#if defined(ENABLE_JVMTI)
-#include "native/jvmti/cacaodbg.h"
-#endif
-
 
 #if defined(__DARWIN__)
 /* Darwin has no working semaphore implementation.  This one is taken
@@ -795,12 +792,8 @@ static void *threads_startup_thread(void *arg)
                thread->_global_sp = (Cell *) (intrp_thread_stack + opt_stacksize);
 #endif
 
-#if defined(ENABLE_JVMTI)
-       /* fire thread start event */
-
-       if (jvmti) 
-               jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_START);
-#endif
+       // Hook point just before the threads initial method is executed.
+       Hook::thread_start(t);
 
        DEBUGTHREADS("starting", t);
 
@@ -850,12 +843,8 @@ static void *threads_startup_thread(void *arg)
 
        DEBUGTHREADS("stopping", t);
 
-#if defined(ENABLE_JVMTI)
-       /* fire thread end event */
-
-       if (jvmti)
-               jvmti_ThreadStartEnd(JVMTI_EVENT_THREAD_END);
-#endif
+       // Hook point just after the threads initial method returned.
+       Hook::thread_end(t);
 
        /* We ignore the return value. */
 
index f443c78cd29ae33b26822dbd801abeb94ad08b04..93778b11e762e2cc439599978f2f8b38141f66cb 100644 (file)
@@ -99,6 +99,7 @@ libvm_la_SOURCES = \
        finalizer.hpp \
        globals.cpp \
        globals.hpp \
+       hook.hpp \
        initialize.cpp \
        initialize.hpp \
        javaobjects.cpp \
diff --git a/src/vm/hook.hpp b/src/vm/hook.hpp
new file mode 100644 (file)
index 0000000..02964e7
--- /dev/null
@@ -0,0 +1,125 @@
+/* src/vm/hook.hpp - hook points inside the VM
+
+   Copyright (C) 2009
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
+
+   This file is part of CACAO.
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2, or (at
+   your option) any later version.
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
+
+*/
+
+
+#ifndef _HOOK_HPP
+#define _HOOK_HPP
+
+#include "config.h"
+
+
+/**
+ * Hook points are inline functions acting as probes scattered throughout
+ * several VM subsystems. They can be used to implement event generation
+ * or statistics gathering without polluting the source code. Hence all
+ * compiler macro and runtime checks should be done in this file. One
+ * example of where hooks are useful is JVMTI event firing.
+ */
+namespace Hook {
+       void breakpoint     (Breakpoint *bp);
+       void class_linked   (classinfo *c);
+       void class_loaded   (classinfo *c);
+       void method_enter   (methodinfo *m);
+       void method_exit    (methodinfo *m);
+       void method_unwind  (methodinfo *m);
+       void native_resolved(methodinfo *m, void *symbol, void **symbolptr);
+       void thread_start   (threadobject *t);
+       void thread_end     (threadobject *t);
+       void vm_init        ();
+}
+
+
+inline void Hook::breakpoint(Breakpoint *bp)
+{
+#if defined(ENABLE_JVMTI)
+       methodinfo* m = bp->method;
+       int32_t     l = bp->location;
+
+       log_message_method("JVMTI: Reached breakpoint in method ", m);
+       log_println("JVMTI: Reached breakpoint at location %d", l);
+#endif
+}
+
+inline void Hook::class_linked(classinfo *c)
+{
+       /* nop */
+}
+
+inline void Hook::class_loaded(classinfo *c)
+{
+       /* nop */
+}
+
+inline void Hook::method_enter(methodinfo *m)
+{
+       /* nop */
+}
+
+inline void Hook::method_exit(methodinfo *m)
+{
+       /* nop */
+}
+
+inline void Hook::method_unwind(methodinfo *m)
+{
+       /* nop */
+}
+
+inline void Hook::native_resolved(methodinfo *m, void *symbol, void **symbolptr)
+{
+       /* nop */
+}
+
+inline void Hook::thread_start(threadobject *t)
+{
+       /* nop */
+}
+
+inline void Hook::thread_end(threadobject *t)
+{
+       /* nop */
+}
+
+inline void Hook::vm_init()
+{
+       /* nop */
+}
+
+
+#endif /* _HOOK_HPP */
+
+
+/*
+ * These are local overrides for various environment variables in Emacs.
+ * Please do not remove this and leave it at the end of the file, where
+ * Emacs will automagically detect them.
+ * ---------------------------------------------------------------------
+ * Local variables:
+ * mode: c++
+ * indent-tabs-mode: t
+ * c-basic-offset: 4
+ * tab-width: 4
+ * End:
+ * vim:noexpandtab:sw=4:ts=4:
+ */
index 216b4fc856f6cb3e7acef6d458c5703146ca262f..e7ec2086ef2566752fa5a093511e39d5b8e8a2b8 100644 (file)
@@ -45,6 +45,7 @@
 
 #include "vm/breakpoint.hpp"
 #include "vm/exceptions.hpp"
+#include "vm/hook.hpp"
 #include "vm/initialize.hpp"
 #include "vm/options.h"
 #include "vm/os.hpp"
@@ -576,13 +577,8 @@ bool patcher_breakpoint(patchref_t *pr)
        // Get stuff from the patcher reference.
        Breakpoint* breakp = (Breakpoint*) pr->ref;
 
-#if defined(ENABLE_JVMTI)
-       methodinfo* m = breakp->method;
-       int32_t     l = breakp->location;
-
-       log_message_method("JVMTI: Reached breakpoint in method ", m);
-       log_println("JVMTI: Reached breakpoint at location %d", l);
-#endif
+       // Hook point when a breakpoint was triggered.
+       Hook::breakpoint(breakp);
 
        // In case the breakpoint wants to be kept active, we simply
        // fail to "patch" at this point.
index d515db09c5750a683cc6d7fd7867e3f18308c1a5..c57c702c59bcae603a5fdc47ba5527433779d300 100644 (file)
@@ -41,6 +41,7 @@
 #include "vm/array.hpp"
 #include "vm/global.h"
 #include "vm/globals.hpp"
+#include "vm/hook.hpp"
 #include "vm/javaobjects.hpp"
 #include "vm/options.h"
 #include "vm/string.hpp"
@@ -225,6 +226,9 @@ void trace_java_call_enter(methodinfo *m, uint64_t *arg_regs, uint64_t *stack)
        return;
 #endif
 
+       // Hook point on entry into Java method.
+       Hook::method_enter(m);
+
        md = m->parseddesc;
 
        /* calculate message length */
@@ -361,6 +365,9 @@ void trace_java_call_exit(methodinfo *m, uint64_t *return_regs)
        return;
 #endif
 
+       // Hook point upon exit from Java method.
+       Hook::method_exit(m);
+
        md = m->parseddesc;
 
        /* outdent the log message */
index d956341e88bd873662de15b804a2ce9e6ba9e1cf..42e82954adc96c6be5c9b4aa7192adeb4a25f1bd 100644 (file)
@@ -45,6 +45,7 @@
 #include "vm/classcache.hpp"
 #include "vm/exceptions.hpp"
 #include "vm/globals.hpp"
+#include "vm/hook.hpp"
 #include "vm/loader.hpp"
 #include "vm/options.h"
 #include "vm/primitive.hpp"
@@ -425,6 +426,9 @@ classinfo *link_class(classinfo *c)
 
        RT_TIMING_TIME_DIFF(time_start,time_end,RT_TIMING_LINK_TOTAL);
 
+       // Hook point just after a class was linked.
+       Hook::class_linked(r);
+
        return r;
 }
 
index c509c094e08c13888c4b0e9937f8945bc714adf1..9ff93a4aa527601af3b90c97b4180b271a1585ba 100644 (file)
@@ -49,6 +49,7 @@
 #include "vm/field.hpp"
 #include "vm/global.h"
 #include "vm/globals.hpp"
+#include "vm/hook.hpp"
 #include "vm/javaobjects.hpp"
 #include "vm/linker.hpp"
 #include "vm/loader.hpp"
 
 #include "vm/jit/stubs.hpp"
 
-#if defined(ENABLE_JVMTI)
-# include "native/jvmti/cacaodbg.h"
-#endif
-
 
 /* global variables ***********************************************************/
 
@@ -1177,12 +1174,6 @@ classinfo *load_class_from_classloader(utf *name, classloader_t *cl)
                        printf("]\n");
                }
 
-#if defined(ENABLE_JVMTI)
-               /* fire Class Load JVMTI event */
-               if (jvmti) jvmti_ClassLoadPrepare(false, c);
-#endif
-
-
                return c;
        } 
 
@@ -1972,18 +1963,14 @@ classinfo *load_class_from_classbuffer(classbuffer *cb)
 
        c->state = (c->state & ~CLASS_LOADING) | CLASS_LOADED;
 
-#if defined(ENABLE_JVMTI)
-       /* fire Class Prepare JVMTI event */
-
-       if (jvmti)
-               jvmti_ClassLoadPrepare(true, c);
-#endif
-
 #if !defined(NDEBUG)
        if (loadverbose)
                log_message_class("Loading done class: ", c);
 #endif
 
+       // Hook point just after a class was loaded.
+       Hook::class_loaded(c);
+
        return c;
 }
 
index afc836865dabce9d35d1d39d4c504aeb880920de..bfff3db1eac8d31050172e627a4fc411d4805567 100644 (file)
@@ -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"
@@ -1448,6 +1449,9 @@ VM::VM(JavaVMInitArgs* vm_args)
        // 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)