/* * 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" 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; /* * Linked-list of prepared classes */ static KlassNode *list; /* * 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 deletedTagBag */ static jrawMonitorID deletedTagLock; /* * A bag containing all the deleted klass_tags ids. Must be accessed under * deletedTagLock, */ struct bag* deletedTagBag; /* * Callback when classes are freed, */ static void JNICALL cbTrackingObjectFree(jvmtiEnv* jvmti_env, jlong tag) { debugMonitorEnter(deletedTagLock); *(jlong*)bagAdd(deletedTagBag) = tag; debugMonitorExit(deletedTagLock); } /* * Returns true if the item is not the searched-for tag. */ static jboolean isNotTag(void* item, void* needle) { return *(jlong*)item != *(jlong*)needle; } static jboolean isClassUnloaded(jlong tag) { return !bagEnumerateOver(deletedTagBag, isNotTag, &tag); } /* * 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) { debugMonitorEnter(deletedTagLock); // printf("num unloaded classes: %d", bagSize(deletedTagBag)); struct bag* deleted = bagCreateBag(sizeof(char*), bagSize(deletedTagBag)); if (bagSize(deletedTagBag) != 0) { KlassNode* node = list; KlassNode** previousNext = &list; while (node != NULL) { if (isClassUnloaded(node->klass_tag)) { *previousNext = node->next; *(char**)bagAdd(deleted) = node->signature; jvmtiDeallocate(node); } else { previousNext = &(node->next); } node = *previousNext; } bagDeleteAll(deletedTagBag); } debugMonitorExit(deletedTagLock); return deleted; } /* * Add a class to the prepared class list. * Assumes no duplicates. */ void classTrack_addPreparedClass(JNIEnv *env, jclass klass) { if (currentClassTag == -1) { // Class tracking not initialized yet, nobody's interested return; } KlassNode *node; jvmtiError error; if (gdata->assertOn) { /* 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) { JDI_ASSERT_FAILED("Attempting to insert duplicate class"); } } 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 */ node->next = list; list = 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 list. */ void classTrack_initialize(JNIEnv *env) { trackingEnv = getSpecialJvmti(); if (trackingEnv == NULL) { EXIT_ERROR(AGENT_ERROR_INTERNAL, "Failed to allocate tag-tracking jvmtiEnv"); } deletedTagLock = debugMonitorCreate("Deleted class tag lock"); deletedTagBag = bagCreateBag(sizeof(jlong), 10); currentClassTag = -1l; list = NULL; } void classTrack_activate(JNIEnv *env) { if (!setupEvents()) { EXIT_ERROR(AGENT_ERROR_INTERNAL, "Unable to setup ObjectFree tracking"); } currentClassTag = 0l; list = NULL; 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) } void classTrack_reset(void) { }