From f0e803b6e10300e10cde31051ef77dec839d7162 Mon Sep 17 00:00:00 2001
From: kseitz <kseitz@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Fri, 1 Sep 2006 17:42:23 +0000
Subject: [PATCH]         * include/jvm.h (_Jv_JVMTI_Init): Declare.         *
 jvmti.cc (_Jv_JVMTI_Init): New function.         * prims.cc
 (_Jv_CreateJavaVM): Initialize JVMTI.

        * jvmti.cc (ILLEGAL_ARGUMENT): New macro.
        (_Jv_JVMTI_Allocate): Use ILLEGAL_ARUMENT.

        * jvmti.cc (_jvmtiEnvironments): New linked list of
        JVMTI environments.
        (FOREACH_ENVIRONMENT): New macro.
        (_envListLock): New object to act as synchronization lock
        for _jvmtiEnvironments.
        (_Jv_JVMTI_DisposeEnvironment): Check for NULL environment.
        Remove the environment from the list of known environments.
        (_Jv_GetJVMTIEnv): Add the new environment to the list
        of known environments.


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@116635 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libjava/ChangeLog     | 19 +++++++++++
 libjava/include/jvm.h |  5 ++-
 libjava/jvmti.cc      | 74 +++++++++++++++++++++++++++++++++++++++++--
 libjava/prims.cc      |  1 +
 4 files changed, 95 insertions(+), 4 deletions(-)

diff --git a/libjava/ChangeLog b/libjava/ChangeLog
index 594e2f2e22d6..b880c56ec07e 100644
--- a/libjava/ChangeLog
+++ b/libjava/ChangeLog
@@ -1,3 +1,22 @@
+2006-09-01  Keith Seitz  <keiths@redhat.com>
+
+	* include/jvm.h (_Jv_JVMTI_Init): Declare.
+	* jvmti.cc (_Jv_JVMTI_Init): New function.
+	* prims.cc (_Jv_CreateJavaVM): Initialize JVMTI.
+
+	* jvmti.cc (ILLEGAL_ARGUMENT): New macro.
+	(_Jv_JVMTI_Allocate): Use ILLEGAL_ARUMENT.
+
+	* jvmti.cc (_jvmtiEnvironments): New linked list of
+	JVMTI environments.
+	(FOREACH_ENVIRONMENT): New macro.
+	(_envListLock): New object to act as synchronization lock
+	for _jvmtiEnvironments.
+	(_Jv_JVMTI_DisposeEnvironment): Check for NULL environment.
+	Remove the environment from the list of known environments.
+	(_Jv_GetJVMTIEnv): Add the new environment to the list
+	of known environments.
+
 2006-09-01  Gary Benson  <gbenson@redhat.com>
 
 	* java/net/InetAddress.java (getLocalHost): Refactor to avoid
diff --git a/libjava/include/jvm.h b/libjava/include/jvm.h
index 8a970c0c8576..88b45be983db 100644
--- a/libjava/include/jvm.h
+++ b/libjava/include/jvm.h
@@ -573,10 +573,13 @@ void _Jv_FreeJNIEnv (_Jv_JNIEnv *);
 struct _Jv_JavaVM;
 _Jv_JavaVM *_Jv_GetJavaVM (); 
 
-/* Get the JVMTI environment */
+/* Get a JVMTI environment */
 struct _Jv_JVMTIEnv;
 _Jv_JVMTIEnv *_Jv_GetJVMTIEnv (void);
 
+/* Initialize JVMTI */
+extern void _Jv_JVMTI_Init (void);
+
 // Some verification functions from defineclass.cc.
 bool _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig);
 bool _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig);
diff --git a/libjava/jvmti.cc b/libjava/jvmti.cc
index 91d941a1a630..d2d8f52f5b5c 100644
--- a/libjava/jvmti.cc
+++ b/libjava/jvmti.cc
@@ -22,6 +22,7 @@ details.  */
 #include <gnu/gcj/runtime/BootClassLoader.h>
 #include <java/lang/Class.h>
 #include <java/lang/ClassLoader.h>
+#include <java/lang/Object.h>
 #include <java/lang/Thread.h>
 #include <java/lang/Throwable.h>
 #include <java/lang/VMClassLoader.h>
@@ -39,6 +40,20 @@ struct _Jv_rawMonitorID
   _Jv_ConditionVariable_t condition;
 };
 
+/* A simple linked list of all JVMTI environments. Since
+   events must be delivered to environments in the order
+   in which the environments were created, new environments
+   are added to the end of the list. */
+struct jvmti_env_list
+{
+  jvmtiEnv *env;
+  struct jvmti_env_list *next;
+};
+static struct jvmti_env_list *_jvmtiEnvironments = NULL;
+static java::lang::Object *_envListLock = NULL;
+#define FOREACH_ENVIRONMENT(Ele) \
+  for (Ele = _jvmtiEnvironments; Ele != NULL; Ele = Ele->next)
+
 // Some commonly-used checks
 
 #define THREAD_DEFAULT_TO_CURRENT(jthread)				\
@@ -58,6 +73,9 @@ struct _Jv_rawMonitorID
 #define NULL_CHECK(Ptr)					\
   if (Ptr == NULL) return JVMTI_ERROR_NULL_POINTER;
 
+#define ILLEGAL_ARGUMENT(Cond)				\
+  if ((Cond)) return JVMTI_ERROR_ILLEGAL_ARGUMENT
+
 static jvmtiError JNICALL
 _Jv_JVMTI_SuspendThread (MAYBE_UNUSED jvmtiEnv *env, jthread thread)
 {
@@ -195,8 +213,7 @@ static jvmtiError JNICALL
 _Jv_JVMTI_Allocate (MAYBE_UNUSED jvmtiEnv *env, jlong size,
 		    unsigned char **result)
 {
-  if (size < 0)
-    return JVMTI_ERROR_ILLEGAL_ARGUMENT;
+  ILLEGAL_ARGUMENT (size < 0);
   NULL_CHECK (result);
   if (size == 0)
     *result = NULL;
@@ -437,7 +454,32 @@ _Jv_JVMTI_GetJNIFunctionTable (MAYBE_UNUSED jvmtiEnv *env,
 static jvmtiError JNICALL
 _Jv_JVMTI_DisposeEnvironment (jvmtiEnv *env)
 {
-  // All we need to do is free memory allocated by _Jv_GetJVMTIEnv
+  NULL_CHECK (env);
+
+  if (_jvmtiEnvironments == NULL)
+    return JVMTI_ERROR_INVALID_ENVIRONMENT;
+  else
+    {
+      JvSynchronize dummy (_envListLock);
+      if (_jvmtiEnvironments->env == env)
+	{
+	  _Jv_Free (_jvmtiEnvironments);
+	  _jvmtiEnvironments = _jvmtiEnvironments->next;
+	}
+      else
+	{
+	  struct jvmti_env_list *e = _jvmtiEnvironments; 
+	  while (e->next != NULL && e->next->env != env)
+	    e = e->next;
+	  if (e->next == NULL)
+	    return JVMTI_ERROR_INVALID_ENVIRONMENT;
+
+	  struct jvmti_env_list *next = e->next->next;
+	  _Jv_Free (e->next);
+	  e->next = next;
+	}
+    }
+
   _Jv_Free (env);
   return JVMTI_ERROR_NONE;
 }
@@ -750,5 +792,31 @@ _Jv_GetJVMTIEnv (void)
   _Jv_JVMTIEnv *env
     = (_Jv_JVMTIEnv *) _Jv_MallocUnchecked (sizeof (_Jv_JVMTIEnv));
   env->p = &_Jv_JVMTI_Interface;
+
+  {
+    JvSynchronize dummy (_envListLock);
+    struct jvmti_env_list *element
+      = (struct jvmti_env_list *) _Jv_MallocUnchecked (sizeof (struct jvmti_env_list));
+    element->env = env;
+    element->next = NULL;
+
+    if (_jvmtiEnvironments == NULL)
+      _jvmtiEnvironments = element;
+    else
+      {
+	struct jvmti_env_list *e;
+	for (e = _jvmtiEnvironments; e->next != NULL; e = e->next)
+	  ;
+	e->next = element;
+      }
+  }
+
   return env;
 }
+
+void
+_Jv_JVMTI_Init ()
+{
+  _jvmtiEnvironments = NULL;
+  _envListLock = new java::lang::Object ();
+}
diff --git a/libjava/prims.cc b/libjava/prims.cc
index 33972f3bc7b8..3db82c19fa7f 100644
--- a/libjava/prims.cc
+++ b/libjava/prims.cc
@@ -1484,6 +1484,7 @@ _Jv_CreateJavaVM (JvVMInitArgs* vm_args)
   _Jv_platform_initialize ();
 
   _Jv_JNI_Init ();
+  _Jv_JVMTI_Init ();
 
   _Jv_GCInitializeFinalizers (&::gnu::gcj::runtime::FinalizerThread::finalizerReady);
 
-- 
GitLab