/* * Copyright (c) 2001, 2019, 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 hash table slot count */ #define CT_HASH_SLOT_COUNT 263 /* Prime which eauals 4k+3 for some k */ typedef struct KlassNode { jclass klass; /* weak global reference */ char *signature; /* class signature */ struct KlassNode *next; /* next node in this slot */ } KlassNode; /* * Hash table of prepared classes. Each entry is a pointer * to a linked list of KlassNode. */ static KlassNode **table; /* * The JVMTI env we use to keep track of klass tags which allows us to detect class-unloads. */ static jvmtiEnv *trackingEnv; /* * The current highest tag number in use by the trackingEnv. * * No need for synchronization since everything is done under the handlerLock. */ static jlong currentKlassTag; /* * A lock to protect access to 'deletedTagBag' */ static jrawMonitorID deletedTagLock; /* * A flag indicating whether classes have been unloaded. * * It is cleared each time classTrack_processUnloads is called. */ jboolean hasUnloadedClasses; /* * The callback for when classes are freed. Only classes are called because this is registered with * the trackingEnv which only tags classes. */ static void JNICALL cbTrackingObjectFree(jvmtiEnv* jvmti_env, jlong tag) { debugMonitorEnter(deletedTagLock); hasUnloadedClasses = JNI_TRUE; debugMonitorExit(deletedTagLock); } /* * Return slot in hash table to use for this class. */ static jint hashKlass(jclass klass) { jint hashCode = objectHashCode(klass); return abs(hashCode) % CT_HASH_SLOT_COUNT; } /* * Transfer a node (which represents klass) from the current * table to the new table. */ static void transferClass(JNIEnv *env, jclass klass, KlassNode **newTable) { jint slot = hashKlass(klass); KlassNode **head = &table[slot]; KlassNode **newHead = &newTable[slot]; KlassNode **nodePtr; KlassNode *node; /* Search the node list of the current table for klass */ for (nodePtr = head; node = *nodePtr, node != NULL; nodePtr = &(node->next)) { if (isSameObject(env, klass, node->klass)) { /* Match found transfer node */ /* unlink from old list */ *nodePtr = node->next; /* insert in new list */ node->next = *newHead; *newHead = node; return; } } /* we haven't found the class, only unloads should have happenned, * so the only reason a class should not have been found is * that it is not prepared yet, in which case we don't want it. * Asset that the above is true. */ /**** the HotSpot VM doesn't create prepare events for some internal classes *** JDI_ASSERT_MSG((classStatus(klass) & (JVMTI_CLASS_STATUS_PREPARED|JVMTI_CLASS_STATUS_ARRAY))==0, classSignature(klass)); ***/ } /* * Delete a hash table of classes. * The signatures of classes in the table are returned. */ static struct bag * deleteTable(JNIEnv *env, KlassNode *oldTable[]) { struct bag *signatures = bagCreateBag(sizeof(char*), 10); jint slot; if (signatures == NULL) { EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"signatures"); } for (slot = 0; slot < CT_HASH_SLOT_COUNT; slot++) { KlassNode *node = oldTable[slot]; while (node != NULL) { KlassNode *next; char **sigSpot; /* Add signature to the signature bag */ sigSpot = bagAdd(signatures); if (sigSpot == NULL) { EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY,"signature bag"); } *sigSpot = node->signature; /* Free weak ref and the node itself */ JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, node->klass); next = node->next; jvmtiDeallocate(node); node = next; } } jvmtiDeallocate(oldTable); return signatures; } /* * Called after class unloads have occurred. Creates a new hash table * of currently loaded prepared classes. * The signatures of classes which were unloaded (not present in the * new table) are returned. */ struct bag * classTrack_processUnloads(JNIEnv *env) { KlassNode **newTable; struct bag *unloadedSignatures; unloadedSignatures = NULL; newTable = jvmtiAllocate(CT_HASH_SLOT_COUNT * sizeof(KlassNode *)); if (newTable == NULL) { EXIT_ERROR(AGENT_ERROR_OUT_OF_MEMORY, "classTrack table"); } else { (void)memset(newTable, 0, CT_HASH_SLOT_COUNT * sizeof(KlassNode *)); WITH_LOCAL_REFS(env, 1) { jint classCount; jclass *classes; jvmtiError error; int i; jboolean hasUnloaded = JNI_FALSE; debugMonitorEnter(deletedTagLock); hasUnloaded = hasUnloadedClasses; hasUnloadedClasses = JNI_FALSE; debugMonitorExit(deletedTagLock); if (hasUnloaded) { error = allLoadedClasses(&classes, &classCount); if ( error != JVMTI_ERROR_NONE ) { jvmtiDeallocate(newTable); EXIT_ERROR(error,"loaded classes"); } else { /* Transfer each current class into the new table */ for (i=0; iassertOn) { /* Check this is not a duplicate */ for (node = *head; node != NULL; node = node->next) { if (isSameObject(env, klass, node->klass)) { JDI_ASSERT_FAILED("Attempting to insert duplicate class"); break; } } } 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"); } ++currentKlassTag; error = JVMTI_FUNC_PTR(trackingEnv,SetTag)(trackingEnv, klass, currentKlassTag); if (error != JVMTI_ERROR_NONE) { jvmtiDeallocate(node->signature); jvmtiDeallocate(node); EXIT_ERROR(error,"SetTag"); } if ((node->klass = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, klass)) == NULL) { jvmtiDeallocate(node->signature); jvmtiDeallocate(node); EXIT_ERROR(AGENT_ERROR_NULL_POINTER,"NewWeakGlobalRef"); } /* Insert the new node */ node->next = *head; *head = node; } 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 build the initial prepared class hash table. */ void classTrack_initialize(JNIEnv *env) { /* Setup the tracking env */ trackingEnv = getSpecialJvmti(); if ( trackingEnv == NULL ) { EXIT_ERROR(AGENT_ERROR_INTERNAL,"Failed to allocate tag-tracking jvmtiEnv"); } /* We want to create these before turning on the events or tagging anything. */ deletedTagLock = debugMonitorCreate("Deleted class tag lock"); hasUnloadedClasses = JNI_FALSE; /* Setup the trackingEnv's ObjectFree event */ if (!setupEvents()) { ERROR_MESSAGE(("Unable to setup class ObjectFree tracking! Class unloads will not " "be reported!")); } currentKlassTag = 0l; WITH_LOCAL_REFS(env, 1) { jint classCount; jclass *classes; jvmtiError error; jint i; error = allLoadedClasses(&classes, &classCount); if ( error == JVMTI_ERROR_NONE ) { table = jvmtiAllocate(CT_HASH_SLOT_COUNT * sizeof(KlassNode *)); if (table != NULL) { (void)memset(table, 0, CT_HASH_SLOT_COUNT * sizeof(KlassNode *)); for (i=0; i