You are viewing a plain text version of this content. The canonical link for it is here.
Posted to kato-commits@incubator.apache.org by mo...@apache.org on 2009/11/27 17:09:03 UTC

svn commit: r884910 - in /incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti: linux-x86/ src/main/native/ src/main/native/include/

Author: monteith
Date: Fri Nov 27 17:09:03 2009
New Revision: 884910

URL: http://svn.apache.org/viewvc?rev=884910&view=rev
Log:
Add support for references from classloaders to classes.

Modified:
    incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/linux-x86/pom.xml
    incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addClass.c
    incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addObject.c
    incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/cjvmti.c
    incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/addClass.h
    incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/cjvmti.h

Modified: incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/linux-x86/pom.xml
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/linux-x86/pom.xml?rev=884910&r1=884909&r2=884910&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/linux-x86/pom.xml (original)
+++ incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/linux-x86/pom.xml Fri Nov 27 17:09:03 2009
@@ -43,7 +43,7 @@
         <extensions>true</extensions>   
         <configuration>
           <compilerStartOptions>
-            <compilerStartOption>-fPIC -O</compilerStartOption>
+            <compilerStartOption>-fPIC -O0 -ggdb3</compilerStartOption>
           </compilerStartOptions>
           
           <javahOS>linux</javahOS>

Modified: incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addClass.c
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addClass.c?rev=884910&r1=884909&r2=884910&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addClass.c (original)
+++ incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addClass.c Fri Nov 27 17:09:03 2009
@@ -85,9 +85,9 @@
 	classObject = 0;
 	if (tag!=0){
 		oldObj = (void *)tag;
-		if (!oldObj->isClass){
+		if (oldObj->type == OBJECT){
 			classObject = 1;
-			oldObj->isClass = 1;
+			oldObj->type = CLASS;
 		}
 	}
 
@@ -155,7 +155,7 @@
 			free(oldObj);
 		}
 		jcls->positionInFile = FTELL(variableFile);
-		jcls->obj.isClass = 1; // Indicates its a class object
+		jcls->obj.type = CLASS; // Indicates its a class object
 		staticsCount++;
 		
 		writeIDNoSize(CJVMTI_CLASS, variableFile);
@@ -428,11 +428,12 @@
 	// write class loader details to file
 	
 	FSEEK(variableFile, 0, SEEK_END);
-	if (classloader != 0) {
-		jcls->classLoader = addObject(classloader, 0)->positionInFile;
-	} else {
-		jcls->classLoader = CJVMTI_NULL_OBJECT;
-	}
+
+    {
+        struct JClassLoader *jcl = getClassLoaderDetails(classloader);
+        jcls->classLoader = (jcl == NULL) ? NULL : jcl->positionInFile;
+    }
+        
 	FSETPOS(variableFile, &fieldPos);
 	writeIDNoSize(CJVMTI_CLASSLOADERS, variableFile);
 	writeReference(jcls->classLoader, variableFile);
@@ -549,3 +550,143 @@
 	return 1;
 }
 
+
+/*
+ * Adds classloader details to the file.
+ * Each class calls this function with it's classloader object.
+ * The classloader stores the object reference, the number of classes
+ * and a reference to each of the classes.
+ * This might have to transform the java object tag into a classloader.
+ *
+ * Format:
+ * CJVMTI_CLASSLOADER | OBJECT REFERENCE | jint numberClasses | jlong classloader | ...
+ */
+struct JClassLoader * getClassLoaderDetails(jobject object) {
+    jvmtiError err;
+    struct JObject *oldObj;
+    jlong tag;
+    struct JClassLoader *jcl;
+    char isObject = 0;
+
+    if (object != NULL) {
+    	err = (*env)->GetTag(env, object,(jlong *) &tag);
+
+        if (((struct JObject*) tag)->type == OBJECT) {
+            isObject = 1; 
+            oldObj = (struct JObject *) tag;
+        }
+	    if (err != JVMTI_ERROR_NONE) {
+		    return NULL;
+    	}
+    } else {
+        tag = (jlong) systemLoader;
+    }
+
+    if ((tag == NULL) || (isObject == 1)) {
+        jint class_count; /* The number of classes in this classloader.*/
+        jclass *classes; /* The array of classes in this classloader. */
+        jint i;
+        FPOS_T objPos, classesPos;
+
+        jcl = (struct JClassLoader *) calloc(1, sizeof(struct JClassLoader));
+        tag = (jlong) jcl;
+
+        if (tag == NULL) {
+            fprintf(stderr, "Failed to malloc JClassLoader structure.");
+            abort();
+        }
+
+        if (isObject) {
+            memcpy(jcl, oldObj, sizeof(struct JObject));
+            jcl->obj.type = CLASSLOADER;
+            free(oldObj);
+        }
+
+        err = (*env)->GetClassLoaderClasses(env, object, &class_count, &classes);
+
+        if ((classes == NULL) && (class_count != 0)) {
+            fprintf(stderr, "GetClassloaderClasses returns a NULL class array.\n", err);
+            abort();
+        }
+
+        if (err != JVMTI_ERROR_NONE) {
+            fprintf(stderr, "Unexpected JVMTI error %d in GetClassloaderClasses\n", err);
+            abort();
+        }
+
+
+        /* If not the bootstrap classloader, get the Java object
+         * (ClassLoader instance) and store it's position,
+         * setup the tag for deallocation and setup tag for
+         * deallocation.
+         */
+        if (object != NULL) {
+            err = (*env)->SetTag(env, object, (jlong) tag);
+            Q_enqueue(&object, toDetag);
+            objPos = addObject(object, 0)->positionInFile;
+        } else {
+            systemLoader = jcl;
+            objPos = CJVMTI_NULL_OBJECT;
+        }
+
+
+        /* Resolve all classes references in the array
+         * This allows a second walk of the array to write out
+         * the file positions stored in the JClass structures.
+          */
+
+    
+        /* Set the tag:
+         * CJVMTI_CLASSLOADER | OBJECT REFERENCE | jint numberClasses
+         * Remember position in tag.
+         */
+        FSEEK(variableFile, 0, SEEK_END);
+        jcl->positionInFile = FTELL(variableFile);
+        writeIDNoSize(CJVMTI_CLASSLOADER, variableFile);
+        writeReference((FPOS_T) objPos, variableFile);
+        writeJint(class_count, variableFile);
+        
+        // Reserve space for classes
+        classesPos = FTELL(variableFile);
+        for(i=0; i < class_count; i++) {
+            writeReference(CJVMTI_NULL_OBJECT, variableFile);
+        }        
+
+        for(i=0; i < class_count; i++) {
+            getClassReferences(classes[i]);
+        }        
+        
+        FSEEK(variableFile, classesPos, SEEK_SET); 
+
+        /* Write out all references to the classes */
+        for(i=0; i < class_count; i++) {
+            jlong clTag;
+
+	        err = (*env)->GetTag(env, classes[i], (jlong *)&clTag);
+            
+            if (err != JVMTI_ERROR_NONE) {
+                fprintf(stderr, "Error getting tag from class %p, got error %d\n", classes[i], err); 
+                abort();
+            } else if (clTag == NULL) {
+                /* The class might not be in a fit state to be stored. */
+                writeReference( CJVMTI_NULL_OBJECT, variableFile);
+            } else {
+                writeReference((FPOS_T) ((struct JClass *)clTag)->positionInFile, variableFile);
+
+                /* Ensure we don't accumulate the class references */
+                (*jniptr)->DeleteLocalRef(jniptr, classes[i]);
+            }
+
+        } 
+
+        if (classes != NULL) { 
+            (*env)->Deallocate(env, (unsigned char*) classes);
+        }
+
+        FSEEK(variableFile, 0, SEEK_END);
+        return jcl;
+    }
+
+    return (struct JClassLoader *) tag;
+}
+

Modified: incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addObject.c
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addObject.c?rev=884910&r1=884909&r2=884910&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addObject.c (original)
+++ incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/addObject.c Fri Nov 27 17:09:03 2009
@@ -74,7 +74,7 @@
 	if (tag != 0) {
 		objp = (void *) tag;
 		if (!objp->state) { // Check it has not previously been filled in 
-			objp->isClass = 1;
+			objp->type = CLASS;
 			classObject = 1;
 		}else{
 			if (objp->depth > depth && maxReferenceDepth != 0){ // Check depth is not lower from this reference
@@ -115,7 +115,7 @@
 		}
 
 		objp = (void *) tag; // Set pointer
-		if (!classObject) objp->isClass = 0;
+		if (!classObject) objp->type = OBJECT;
 
 		objp->state = 1; // Indicates object has been written (but references not followed)
 

Modified: incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/cjvmti.c
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/cjvmti.c?rev=884910&r1=884909&r2=884910&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/cjvmti.c (original)
+++ incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/cjvmti.c Fri Nov 27 17:09:03 2009
@@ -211,6 +211,7 @@
 	(*jniptr)->PushLocalFrame(jniptr, 5000); // Automatically deallocates local references when popped
 
 	nullObject.positionInFile = CJVMTI_NULL_OBJECT;
+    systemLoader = NULL; /* null system class loader */
 
 	initAgent(); // Prepare dump file
 
@@ -257,7 +258,7 @@
 		(*env)->GetTag(env, deTagObj, &tag);
 		if (tag == 0)continue; // We sometimes free an object from earlier in the queue if it went from an object to a class object
 		deAlObj = (struct JObject *)tag;
-		if (deAlObj->isClass){
+		if (deAlObj->type == CLASS){
 			deAlClass = (struct JClass *) tag;
 
 			for (i = 0; i < deAlClass->numInstanceFields; i++){
@@ -285,6 +286,11 @@
 	Q_free(qObjects);
 	Q_free(qStaticFields);
 
+    /* Free allocated JClassLoader representing system class loader */
+    if (systemLoader != NULL) {
+        free(systemLoader);
+        systemLoader = NULL;
+    }
 	(*env)->Deallocate(env, (unsigned char *)suspendedThreads);
 	printf("Finished creating dump file \n");
 	(*jniptr)->PopLocalFrame(jniptr, NULL); // Clears local references

Modified: incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/addClass.h
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/addClass.h?rev=884910&r1=884909&r2=884910&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/addClass.h (original)
+++ incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/addClass.h Fri Nov 27 17:09:03 2009
@@ -18,4 +18,5 @@
 #include "cjvmti.h"
 struct JClass * addClassDetails(jclass class);
 struct JClass * getClassReferences(jclass class);
+struct JClassLoader * getClassLoaderDetails(jobject object);
 #endif

Modified: incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/cjvmti.h
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/cjvmti.h?rev=884910&r1=884909&r2=884910&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/cjvmti.h (original)
+++ incubator/kato/trunk/org.apache.kato/kato.native/kato.native.cjvmti/src/main/native/include/cjvmti.h Fri Nov 27 17:09:03 2009
@@ -75,7 +75,7 @@
 	FPOS_T positionInFile; // Actual object position in dump file
 	FPOS_T referenceLocation; // Used to store where references should be saved
 	int totalFieldCount; // Number of fields after following the superclass hierarchy
-	char isClass; // Identifies if this is a class object
+    enum { OBJECT, CLASS, CLASSLOADER} type; // Identifies what type of object this is
 	char depth; // Used to limit depth references are followed to
 };
 
@@ -95,70 +95,77 @@
 	jint * instanceModifiers; // malloced normally
 };
 
+struct JClassLoader {
+	struct JObject obj; // Classloader object
+    FPOS_T positionInFile; // Position of classloader in dump file
+};
+
 #define CJVMTI_LOCAL_VAR  0
 #define CJVMTI_STATIC_VAR 1
 #define CJVMTI_INSTANCE_VAR 2
 #define CJVMTI_ARRAY_VAR 3
 // Field values
-#define CJVMTI_BYTE 0x1c
-#define CJVMTI_CHAR 0x01
-#define CJVMTI_DOUBLE 0x02
-#define CJVMTI_FLOAT 0x03
-#define CJVMTI_INT 0x04
-#define CJVMTI_LONG 0x05
-#define CJVMTI_OBJECT 0x06
-#define CJVMTI_SHORT 0x07
-#define CJVMTI_BOOLEAN 0x08
+#define CJVMTI_BYTE 0x01
+#define CJVMTI_CHAR 0x02
+#define CJVMTI_DOUBLE 0x03
+#define CJVMTI_FLOAT 0x04
+#define CJVMTI_INT 0x05
+#define CJVMTI_LONG 0x06
+#define CJVMTI_OBJECT 0x07
+#define CJVMTI_SHORT 0x08
+#define CJVMTI_BOOLEAN 0x09
 // Field arrays
-#define CJVMTI_OBJECTARRAY 0x09
-#define CJVMTI_BYTE_ARRAY 0x0a
-#define CJVMTI_CHAR_ARRAY 0x0b
-#define CJVMTI_DOUBLE_ARRAY 0x0c
-#define CJVMTI_FLOAT_ARRAY 0x0d
-#define CJVMTI_INT_ARRAY 0x0e
-#define CJVMTI_LONG_ARRAY 0x0f
-#define CJVMTI_OBJECT_ARRAY 0x10
-#define CJVMTI_SHORT_ARRAY 0x11
-#define CJVMTI_BOOLEAN_ARRAY 0x12
+#define CJVMTI_OBJECTARRAY 0x0a
+#define CJVMTI_BYTE_ARRAY 0x0b
+#define CJVMTI_CHAR_ARRAY 0x0c
+#define CJVMTI_DOUBLE_ARRAY 0x0d
+#define CJVMTI_FLOAT_ARRAY 0x0e
+#define CJVMTI_INT_ARRAY 0x0f
+#define CJVMTI_LONG_ARRAY 0x10
+#define CJVMTI_OBJECT_ARRAY 0x11
+#define CJVMTI_SHORT_ARRAY 0x12
+#define CJVMTI_BOOLEAN_ARRAY 0x13
 // Structure ID's
-#define CJVMTI_METHOD 0x13
-#define CJVMTI_THREAD_GROUP 0x14
-#define CJVMTI_THREAD 0x15
-#define CJVMTI_FRAME 0x16
-#define CJVMTI_FIELD_MODIFIERS 0x17
-#define CJVMTI_MONITORS 0x18
-#define CJVMTI_CLASSLOADERS 0x19
+#define CJVMTI_METHOD 0x14
+#define CJVMTI_THREAD_GROUP 0x15
+#define CJVMTI_THREAD 0x16
+#define CJVMTI_FRAME 0x17
+#define CJVMTI_FIELD_MODIFIERS 0x18
+#define CJVMTI_MONITORS 0x19
+#define CJVMTI_CLASSLOADER 0x1a
+#define CJVMTI_CLASSLOADERS 0x1b
 // General error's and extra additions
-#define CJVMTI_LOCAL_NATIVECALL 0x1a
-#define CJVMTI_CLASS 0x1b
-#define CJVMTI_NULL_OBJECT 0x00
-#define CJVMTI_CLASS_INSTANCE_FIELDS 0x1d
-#define CJVMTI_CLASS_STATIC_FIELDS 0x1e
-#define CJVMTI_CLASS_FIELDS 0x1f
-#define CJVMTI_NULL_OBJECT_ARRAY 0x00
-#define CJVMTI_LOCAL_VARIABLE 0x20
-#define CJVMTI_JVMTI_ERROR 0x21
-#define CJVMTI_STRING 0x22;
+#define CJVMTI_LOCAL_NATIVECALL 0x1c
+#define CJVMTI_CLASS 0x1d
+#define CJVMTI_CLASS_INSTANCE_FIELDS 0x1e
+#define CJVMTI_CLASS_STATIC_FIELDS 0x1f
+#define CJVMTI_CLASS_FIELDS 0x20
+#define CJVMTI_LOCAL_VARIABLE 0x21
+#define CJVMTI_JVMTI_ERROR 0x22
 #define CJVMTI_SUPERCLASS_FIELDS 0x23
 #define CJVMTI_CLASS_INTERFACES 0x24
 
+#define CJVMTI_STRING 0x25; /* Not Actually used*/
+#define CJVMTI_NULL_OBJECT 0x00
+#define CJVMTI_NULL_OBJECT_ARRAY 0x00
 
-// Global variables
+/* Global variables */
 jvmtiEnv *env;
 JavaVM *vmptr;
 JNIEnv *jniptr;
 jrawMonitorID lock;
-FILE *fp; // log file
-FILE *variableFile; // dump file
+FILE *fp; /* log file */
+FILE *variableFile; /* dump file */
 int maxReferenceDepth;
 int maxFrameDepth;
 int printDepth;
-struct queueADT * qObjects; // References waiting to be followed in objects
-struct queueADT * qStaticFields; // References waiting to be followed in classes
-struct queueADT * toDetag; // Memory that needs to be freed/objects that need to be detagged
+struct queueADT * qObjects; /* References waiting to be followed in objects */
+struct queueADT * qStaticFields; /* References waiting to be followed in classes */
+struct queueADT * toDetag; /* Memory that needs to be freed/objects that need to be detagged */
 long objectCount;
 long staticsCount;
 struct JObject nullObject;
+struct JClassLoader *systemLoader; /* System classloader has no object on heap to tag. Use this */
 #ifdef __DEBUG_CJVMTI__
 HANDLE hLogFile;
 #endif