/* * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * This module tracks classes that have been prepared, so as to * be able to compute which have been unloaded. On VM start-up * all prepared classes are put in a table. As class prepare * events come in they are added to the table. After an unload * event or series of them, the VM can be asked for the list * of classes; this list is compared against the table keep by * this module, any classes no longer present are known to * have been unloaded. * * For efficient access, classes are keep in a hash table. * Each slot in the hash table has a linked list of KlassNode. * * Comparing current set of classes is compared with previous * set by transferring all classes in the current set into * a new table, any that remain in the old table have been * unloaded. */ #include "util.h" #include "bag.h" #include "classTrack.h" /* ClassTrack table slot count */ #define CT_SLOT_COUNT 263 /* Prime which eauals 4k+3 for some k */ typedef struct KlassNode { jlong klass_tag; /* Klass's tag in tracking env */ char *signature; /* class signature */ struct KlassNode *next; /* next node in this slot */ } KlassNode; /* * Table mapping tag % CT_SLOT_COUNT to linked-list of KlassNode*. */ static KlassNode** table; /* * The JVMTI tracking env to keep track of klass tags, for class-unloads */ static jvmtiEnv* trackingEnv; /* * The current highest tag number */ static jlong currentClassTag; /* * Lock to protect deletedSignatureBag */ static jrawMonitorID deletedSignatureLock; /* * A bag containing all the deleted classes' signatures. Must be accessed under * deletedTagLock, */ struct bag* deletedSignatureBag; /* * Callback when classes are freed, Finds the signature and remembers it in deletedSignatureBag. */ static void JNICALL cbTrackingObjectFree(jvmtiEnv* jvmti_env, jlong tag) { debugMonitorEnter(deletedSignatureLock); if (currentClassTag == -1) { // Class tracking not initialized, nobody's interested debugMonitorExit(deletedSignatureLock); return; } // Find deleted KlassNode size_t idx = tag % CT_SLOT_COUNT; KlassNode** klass_ptr = &table[idx]; KlassNode* klass = *klass_ptr; // Tag not found? Ignore. if (klass == NULL) { debugMonitorExit(deletedSignatureLock); return; } // Scan linked-list. jlong found_tag = klass->klass_tag; while (klass != NULL && found_tag != tag) { klass_ptr = &klass->next; klass = *klass_ptr; found_tag = klass->klass_tag; } // Tag not found? Ignore. if (found_tag != tag) { debugMonitorExit(deletedSignatureLock); return; } // At this point we have the KlassNode corresponding to the tag // in klass, and the pointer to it in klass_node. // Remember the unloaded signature. *(char**)bagAdd(deletedSignatureBag) = klass->signature; // Unlink the KlassNode. *klass_ptr = klass->next; jvmtiDeallocate(klass); // Done. debugMonitorExit(deletedSignatureLock); } /* * Called after class unloads have occurred. * The signatures of classes which were unloaded are returned. */ struct bag * classTrack_processUnloads(JNIEnv *env) { debugMonitorEnter(deletedSignatureLock); if (currentClassTag == -1) { // Class tracking not initialized, nobody's interested debugMonitorExit(deletedSignatureLock); return bagCreateBag(sizeof(char*), 0); } struct bag* deleted = deletedSignatureBag; deletedSignatureBag = bagCreateBag(sizeof(char*), 10); debugMonitorExit(deletedSignatureLock); return deleted; } /* * Add a class to the prepared class table. */ void classTrack_addPreparedClass(JNIEnv *env, jclass klass) { jvmtiError error; debugMonitorEnter(deletedSignatureLock); if (currentClassTag == -1) { // Class tracking not initialized yet, nobody's interested debugMonitorExit(deletedSignatureLock); return; } /* Check this is not a duplicate */ jlong tag; error = JVMTI_FUNC_PTR(trackingEnv, GetTag)(trackingEnv, klass, &tag); if (error != JVMTI_ERROR_NONE) { EXIT_ERROR(error, "Unable to GetTag with class trackingEnv"); } if (tag != 0l) { debugMonitorExit(deletedSignatureLock); return; // Already added } KlassNode* node = jvmtiAllocate(sizeof(KlassNode)); if (node == NULL) { EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"KlassNode"); } error = classSignature(klass, &(node->signature), NULL); if (error != JVMTI_ERROR_NONE) { jvmtiDeallocate(node); EXIT_ERROR(error,"signature"); } node->klass_tag = ++currentClassTag; error = JVMTI_FUNC_PTR(trackingEnv, SetTag)(trackingEnv, klass, node->klass_tag); if (error != JVMTI_ERROR_NONE) { jvmtiDeallocate(node->signature); jvmtiDeallocate(node); EXIT_ERROR(error,"SetTag"); } /* Insert the new node */ size_t idx = node->klass_tag % CT_SLOT_COUNT; node->next = table[idx]; table[idx] = node; debugMonitorExit(deletedSignatureLock); } static jboolean setupEvents() { jvmtiCapabilities caps; memset(&caps, 0, sizeof(caps)); caps.can_generate_object_free_events = 1; jvmtiError error = JVMTI_FUNC_PTR(trackingEnv, AddCapabilities)(trackingEnv, &caps); if (error != JVMTI_ERROR_NONE) { return JNI_FALSE; } jvmtiEventCallbacks cb; memset(&cb, 0, sizeof(cb)); cb.ObjectFree = cbTrackingObjectFree; error = JVMTI_FUNC_PTR(trackingEnv, SetEventCallbacks)(trackingEnv, &cb, sizeof(cb)); if (error != JVMTI_ERROR_NONE) { return JNI_FALSE; } error = JVMTI_FUNC_PTR(trackingEnv, SetEventNotificationMode)(trackingEnv, JVMTI_ENABLE, JVMTI_EVENT_OBJECT_FREE, NULL); if (error != JVMTI_ERROR_NONE) { return JNI_FALSE; } return JNI_TRUE; } /* * Called once to initialize class-tracking. */ void classTrack_initialize(JNIEnv *env) { trackingEnv = getSpecialJvmti(); if (trackingEnv == NULL) { EXIT_ERROR(AGENT_ERROR_INTERNAL, "Failed to allocate tag-tracking jvmtiEnv"); } deletedSignatureLock = debugMonitorCreate("Deleted class tag lock"); deletedSignatureBag = bagCreateBag(sizeof(char*), 10); currentClassTag = -1l; table = NULL; } /* * Called to activate class-tracking when a listener registers for EI_GC_FINISH. */ void classTrack_activate(JNIEnv *env) { if (!setupEvents()) { EXIT_ERROR(AGENT_ERROR_INTERNAL, "Unable to setup ObjectFree tracking"); } currentClassTag = 0l; table = jvmtiAllocate(CT_SLOT_COUNT * sizeof(KlassNode*)); if (table != NULL) { (void)memset(table, 0, CT_SLOT_COUNT * sizeof(KlassNode*)); } else { EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY, "failed allocating class-track table"); } WITH_LOCAL_REFS(env, 1) { jint classCount; jclass *classes; jvmtiError error; jint i; error = allLoadedClasses(&classes, &classCount); if ( error == JVMTI_ERROR_NONE ) { for (i = 0; i < classCount; i++) { jclass klass = classes[i]; jint status; jint wanted = JVMTI_CLASS_STATUS_PREPARED | JVMTI_CLASS_STATUS_ARRAY; status = classStatus(klass); if ((status & wanted) != 0) { classTrack_addPreparedClass(env, klass); } } jvmtiDeallocate(classes); } else { EXIT_ERROR(error,"loaded classes array"); } } END_WITH_LOCAL_REFS(env) } static jboolean cleanDeleted(void *signatureVoid, void *arg) { char* sig = (char*)signatureVoid; jvmtiDeallocate(sig); return JNI_TRUE; } /* * Called when agent detaches. */ void classTrack_reset(void) { int idx; debugMonitorEnter(deletedSignatureLock); for (idx = 0; idx < CT_SLOT_COUNT; ++idx) { KlassNode* node = table[idx]; while (node != NULL) { KlassNode* next = node->next; jvmtiDeallocate(node->signature); jvmtiDeallocate(node); node = next; } } jvmtiDeallocate(table); bagEnumerateOver(deletedSignatureBag, cleanDeleted, NULL); bagDestroyBag(deletedSignatureBag); currentClassTag = -1; (void)JVMTI_FUNC_PTR(trackingEnv,DisposeEnvironment)(trackingEnv); trackingEnv = NULL; debugMonitorExit(deletedSignatureLock); }