0N/A/*
1879N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "memory/universe.inline.hpp"
1879N/A#include "prims/jvmtiGetLoadedClasses.hpp"
1879N/A#include "runtime/thread.hpp"
0N/A
0N/A
0N/A
0N/A// The closure for GetLoadedClasses and GetClassLoaderClasses
0N/Aclass JvmtiGetLoadedClassesClosure : public StackObj {
0N/A // Since the SystemDictionary::classes_do callback
0N/A // doesn't pass a closureData pointer,
0N/A // we use a thread-local slot to hold a pointer to
0N/A // a stack allocated instance of this structure.
0N/A private:
0N/A jobject _initiatingLoader;
0N/A int _count;
0N/A Handle* _list;
0N/A int _index;
0N/A
0N/A private:
0N/A // Getting and setting the thread local pointer
0N/A static JvmtiGetLoadedClassesClosure* get_this() {
0N/A JvmtiGetLoadedClassesClosure* result = NULL;
0N/A JavaThread* thread = JavaThread::current();
0N/A result = thread->get_jvmti_get_loaded_classes_closure();
0N/A return result;
0N/A }
0N/A static void set_this(JvmtiGetLoadedClassesClosure* that) {
0N/A JavaThread* thread = JavaThread::current();
0N/A thread->set_jvmti_get_loaded_classes_closure(that);
0N/A }
0N/A
0N/A public:
0N/A // Constructor/Destructor
0N/A JvmtiGetLoadedClassesClosure() {
0N/A JvmtiGetLoadedClassesClosure* that = get_this();
0N/A assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
0N/A _initiatingLoader = NULL;
0N/A _count = 0;
0N/A _list = NULL;
0N/A _index = 0;
0N/A set_this(this);
0N/A }
0N/A
0N/A JvmtiGetLoadedClassesClosure(jobject initiatingLoader) {
0N/A JvmtiGetLoadedClassesClosure* that = get_this();
0N/A assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
0N/A _initiatingLoader = initiatingLoader;
0N/A _count = 0;
0N/A _list = NULL;
0N/A _index = 0;
0N/A set_this(this);
0N/A }
0N/A
0N/A ~JvmtiGetLoadedClassesClosure() {
0N/A JvmtiGetLoadedClassesClosure* that = get_this();
0N/A assert(that != NULL, "JvmtiGetLoadedClassesClosure not found");
0N/A set_this(NULL);
0N/A _initiatingLoader = NULL;
0N/A _count = 0;
0N/A if (_list != NULL) {
0N/A FreeHeap(_list);
0N/A _list = NULL;
0N/A }
0N/A _index = 0;
0N/A }
0N/A
0N/A // Accessors.
0N/A jobject get_initiatingLoader() {
0N/A return _initiatingLoader;
0N/A }
0N/A
0N/A int get_count() {
0N/A return _count;
0N/A }
0N/A
0N/A void set_count(int value) {
0N/A _count = value;
0N/A }
0N/A
0N/A Handle* get_list() {
0N/A return _list;
0N/A }
0N/A
0N/A void set_list(Handle* value) {
0N/A _list = value;
0N/A }
0N/A
0N/A int get_index() {
0N/A return _index;
0N/A }
0N/A
0N/A void set_index(int value) {
0N/A _index = value;
0N/A }
0N/A
0N/A Handle get_element(int index) {
0N/A if ((_list != NULL) && (index < _count)) {
0N/A return _list[index];
0N/A } else {
0N/A assert(false, "empty get_element");
0N/A return Handle();
0N/A }
0N/A }
0N/A
0N/A void set_element(int index, Handle value) {
0N/A if ((_list != NULL) && (index < _count)) {
0N/A _list[index] = value;
0N/A } else {
0N/A assert(false, "bad set_element");
0N/A }
0N/A }
0N/A
0N/A // Other predicates
0N/A bool available() {
0N/A return (_list != NULL);
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A // For debugging.
0N/A void check(int limit) {
0N/A for (int i = 0; i < limit; i += 1) {
0N/A assert(Universe::heap()->is_in(get_element(i)()), "check fails");
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A // Public methods that get called within the scope of the closure
0N/A void allocate() {
3863N/A _list = NEW_C_HEAP_ARRAY(Handle, _count, mtInternal);
0N/A assert(_list != NULL, "Out of memory");
0N/A if (_list == NULL) {
0N/A _count = 0;
0N/A }
0N/A }
0N/A
0N/A void extract(JvmtiEnv *env, jclass* result) {
0N/A for (int index = 0; index < _count; index += 1) {
0N/A result[index] = (jclass) env->jni_reference(get_element(index));
0N/A }
0N/A }
0N/A
0N/A // Finally, the static methods that are the callbacks
0N/A static void increment(klassOop k) {
0N/A JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
0N/A if (that->get_initiatingLoader() == NULL) {
0N/A for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
0N/A that->set_count(that->get_count() + 1);
0N/A }
0N/A } else if (k != NULL) {
0N/A // if initiating loader not null, just include the instance with 1 dimension
0N/A that->set_count(that->get_count() + 1);
0N/A }
0N/A }
0N/A
0N/A static void increment_with_loader(klassOop k, oop loader) {
0N/A JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
0N/A if (loader == JNIHandles::resolve(that->get_initiatingLoader())) {
0N/A for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
0N/A that->set_count(that->get_count() + 1);
0N/A }
0N/A }
0N/A }
0N/A
0N/A static void prim_array_increment_with_loader(klassOop array, oop loader) {
0N/A JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
0N/A if (loader == JNIHandles::resolve(that->get_initiatingLoader())) {
0N/A that->set_count(that->get_count() + 1);
0N/A }
0N/A }
0N/A
0N/A static void add(klassOop k) {
0N/A JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
0N/A if (that->available()) {
0N/A if (that->get_initiatingLoader() == NULL) {
0N/A for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
0N/A oop mirror = Klass::cast(l)->java_mirror();
0N/A that->set_element(that->get_index(), mirror);
0N/A that->set_index(that->get_index() + 1);
0N/A }
0N/A } else if (k != NULL) {
0N/A // if initiating loader not null, just include the instance with 1 dimension
0N/A oop mirror = Klass::cast(k)->java_mirror();
0N/A that->set_element(that->get_index(), mirror);
0N/A that->set_index(that->get_index() + 1);
0N/A }
0N/A }
0N/A }
0N/A
0N/A static void add_with_loader(klassOop k, oop loader) {
0N/A JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
0N/A if (that->available()) {
0N/A if (loader == JNIHandles::resolve(that->get_initiatingLoader())) {
0N/A for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
0N/A oop mirror = Klass::cast(l)->java_mirror();
0N/A that->set_element(that->get_index(), mirror);
0N/A that->set_index(that->get_index() + 1);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A // increment the count for the given basic type array class (and any
0N/A // multi-dimensional arrays). For example, for [B we check for
0N/A // [[B, [[[B, .. and the count is incremented for each one that exists.
0N/A static void increment_for_basic_type_arrays(klassOop k) {
0N/A JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
0N/A assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
0N/A for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
0N/A that->set_count(that->get_count() + 1);
0N/A }
0N/A }
0N/A
0N/A // add the basic type array class and its multi-dimensional array classes to the list
0N/A static void add_for_basic_type_arrays(klassOop k) {
0N/A JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
0N/A assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
0N/A assert(that->available(), "no list");
0N/A for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
0N/A oop mirror = Klass::cast(l)->java_mirror();
0N/A that->set_element(that->get_index(), mirror);
0N/A that->set_index(that->get_index() + 1);
0N/A }
0N/A }
0N/A};
0N/A
0N/A
0N/AjvmtiError
0N/AJvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
0N/A // Since SystemDictionary::classes_do only takes a function pointer
0N/A // and doesn't call back with a closure data pointer,
0N/A // we can only pass static methods.
0N/A
0N/A JvmtiGetLoadedClassesClosure closure;
0N/A {
0N/A // To get a consistent list of classes we need MultiArray_lock to ensure
0N/A // array classes aren't created, and SystemDictionary_lock to ensure that
0N/A // classes aren't added to the system dictionary,
0N/A MutexLocker ma(MultiArray_lock);
0N/A MutexLocker sd(SystemDictionary_lock);
0N/A
0N/A // First, count the classes
0N/A SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment);
0N/A Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment);
0N/A // Next, fill in the classes
0N/A closure.allocate();
0N/A SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add);
0N/A Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add);
0N/A // Drop the SystemDictionary_lock, so the results could be wrong from here,
0N/A // but we still have a snapshot.
0N/A }
0N/A // Post results
0N/A jclass* result_list;
0N/A jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),
0N/A (unsigned char**)&result_list);
0N/A if (err != JVMTI_ERROR_NONE) {
0N/A return err;
0N/A }
0N/A closure.extract(env, result_list);
0N/A *classCountPtr = closure.get_count();
0N/A *classesPtr = result_list;
0N/A return JVMTI_ERROR_NONE;
0N/A}
0N/A
0N/AjvmtiError
0N/AJvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,
0N/A jint* classCountPtr, jclass** classesPtr) {
0N/A // Since SystemDictionary::classes_do only takes a function pointer
0N/A // and doesn't call back with a closure data pointer,
0N/A // we can only pass static methods.
0N/A JvmtiGetLoadedClassesClosure closure(initiatingLoader);
0N/A {
0N/A // To get a consistent list of classes we need MultiArray_lock to ensure
0N/A // array classes aren't created, and SystemDictionary_lock to ensure that
0N/A // classes aren't added to the system dictionary,
0N/A MutexLocker ma(MultiArray_lock);
0N/A MutexLocker sd(SystemDictionary_lock);
0N/A // First, count the classes in the system dictionary which have this loader recorded
0N/A // as an initiating loader. For basic type arrays this information is not recorded
0N/A // so GetClassLoaderClasses will return all of the basic type arrays. This is okay
0N/A // because the defining loader for basic type arrays is always the boot class loader
0N/A // and these classes are "visible" to all loaders.
0N/A SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment_with_loader);
0N/A Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment_for_basic_type_arrays);
0N/A // Next, fill in the classes
0N/A closure.allocate();
0N/A SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add_with_loader);
0N/A Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add_for_basic_type_arrays);
0N/A // Drop the SystemDictionary_lock, so the results could be wrong from here,
0N/A // but we still have a snapshot.
0N/A }
0N/A // Post results
0N/A jclass* result_list;
0N/A jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),
0N/A (unsigned char**)&result_list);
0N/A if (err != JVMTI_ERROR_NONE) {
0N/A return err;
0N/A }
0N/A closure.extract(env, result_list);
0N/A *classCountPtr = closure.get_count();
0N/A *classesPtr = result_list;
0N/A return JVMTI_ERROR_NONE;
0N/A}