Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/share/vm/prims/jvmtiGetLoadedClasses.cpp
48773 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/systemDictionary.hpp"26#include "memory/universe.inline.hpp"27#include "prims/jvmtiGetLoadedClasses.hpp"28#include "runtime/thread.hpp"29#if INCLUDE_ALL_GCS30#include "gc_implementation/g1/g1SATBCardTableModRefBS.hpp"31#endif323334// The closure for GetLoadedClasses35class LoadedClassesClosure : public KlassClosure {36private:37Stack<jclass, mtInternal> _classStack;38JvmtiEnv* _env;3940// Tell the GC to keep this klass alive41static void ensure_klass_alive(oop o) {42// A klass that was previously considered dead can be looked up in the43// CLD/SD, and its _java_mirror or _class_loader can be stored in a root44// or a reachable object making it alive again. The SATB part of G1 needs45// to get notified about this potential resurrection, otherwise the marking46// might not find the object.47#if INCLUDE_ALL_GCS48if (UseG1GC && o != NULL) {49G1SATBCardTableModRefBS::enqueue(o);50}51#endif52}5354public:55LoadedClassesClosure(JvmtiEnv* env) {56_env = env;57}5859void do_klass(Klass* k) {60// Collect all jclasses61_classStack.push((jclass) _env->jni_reference(k->java_mirror()));62ensure_klass_alive(k->java_mirror());63}6465int extract(jclass* result_list) {66// The size of the Stack will be 0 after extract, so get it here67int count = (int)_classStack.size();68int i = count;6970// Pop all jclasses, fill backwards71while (!_classStack.is_empty()) {72result_list[--i] = _classStack.pop();73}7475// Return the number of elements written76return count;77}7879// Return current size of the Stack80int get_count() {81return (int)_classStack.size();82}83};8485// The closure for GetClassLoaderClasses86class JvmtiGetLoadedClassesClosure : public StackObj {87// Since the SystemDictionary::classes_do callback88// doesn't pass a closureData pointer,89// we use a thread-local slot to hold a pointer to90// a stack allocated instance of this structure.91private:92jobject _initiatingLoader;93int _count;94Handle* _list;95int _index;9697private:98// Getting and setting the thread local pointer99static JvmtiGetLoadedClassesClosure* get_this() {100JvmtiGetLoadedClassesClosure* result = NULL;101JavaThread* thread = JavaThread::current();102result = thread->get_jvmti_get_loaded_classes_closure();103return result;104}105static void set_this(JvmtiGetLoadedClassesClosure* that) {106JavaThread* thread = JavaThread::current();107thread->set_jvmti_get_loaded_classes_closure(that);108}109110public:111// Constructor/Destructor112JvmtiGetLoadedClassesClosure() {113JvmtiGetLoadedClassesClosure* that = get_this();114assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");115_initiatingLoader = NULL;116_count = 0;117_list = NULL;118_index = 0;119set_this(this);120}121122JvmtiGetLoadedClassesClosure(jobject initiatingLoader) {123JvmtiGetLoadedClassesClosure* that = get_this();124assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");125_initiatingLoader = initiatingLoader;126_count = 0;127_list = NULL;128_index = 0;129set_this(this);130}131132~JvmtiGetLoadedClassesClosure() {133JvmtiGetLoadedClassesClosure* that = get_this();134assert(that != NULL, "JvmtiGetLoadedClassesClosure not found");135set_this(NULL);136_initiatingLoader = NULL;137_count = 0;138if (_list != NULL) {139FreeHeap(_list);140_list = NULL;141}142_index = 0;143}144145// Accessors.146jobject get_initiatingLoader() {147return _initiatingLoader;148}149150int get_count() {151return _count;152}153154void set_count(int value) {155_count = value;156}157158Handle* get_list() {159return _list;160}161162void set_list(Handle* value) {163_list = value;164}165166int get_index() {167return _index;168}169170void set_index(int value) {171_index = value;172}173174Handle get_element(int index) {175if ((_list != NULL) && (index < _count)) {176return _list[index];177} else {178assert(false, "empty get_element");179return Handle();180}181}182183void set_element(int index, Handle value) {184if ((_list != NULL) && (index < _count)) {185_list[index] = value;186} else {187assert(false, "bad set_element");188}189}190191// Other predicates192bool available() {193return (_list != NULL);194}195196#ifdef ASSERT197// For debugging.198void check(int limit) {199for (int i = 0; i < limit; i += 1) {200assert(Universe::heap()->is_in(get_element(i)()), "check fails");201}202}203#endif204205// Public methods that get called within the scope of the closure206void allocate() {207_list = NEW_C_HEAP_ARRAY(Handle, _count, mtInternal);208assert(_list != NULL, "Out of memory");209if (_list == NULL) {210_count = 0;211}212}213214void extract(JvmtiEnv *env, jclass* result) {215for (int index = 0; index < _count; index += 1) {216result[index] = (jclass) env->jni_reference(get_element(index));217}218}219220static void increment_with_loader(Klass* k, ClassLoaderData* loader_data) {221JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();222oop class_loader = loader_data->class_loader();223if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {224for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {225that->set_count(that->get_count() + 1);226}227}228}229230static void prim_array_increment_with_loader(Klass* array, ClassLoaderData* loader_data) {231JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();232oop class_loader = loader_data->class_loader();233if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {234that->set_count(that->get_count() + 1);235}236}237238static void add_with_loader(Klass* k, ClassLoaderData* loader_data) {239JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();240if (that->available()) {241oop class_loader = loader_data->class_loader();242if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {243for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {244oop mirror = l->java_mirror();245that->set_element(that->get_index(), mirror);246that->set_index(that->get_index() + 1);247}248}249}250}251252// increment the count for the given basic type array class (and any253// multi-dimensional arrays). For example, for [B we check for254// [[B, [[[B, .. and the count is incremented for each one that exists.255static void increment_for_basic_type_arrays(Klass* k) {256JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();257assert(that != NULL, "no JvmtiGetLoadedClassesClosure");258for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {259that->set_count(that->get_count() + 1);260}261}262263// add the basic type array class and its multi-dimensional array classes to the list264static void add_for_basic_type_arrays(Klass* k) {265JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();266assert(that != NULL, "no JvmtiGetLoadedClassesClosure");267assert(that->available(), "no list");268for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {269oop mirror = l->java_mirror();270that->set_element(that->get_index(), mirror);271that->set_index(that->get_index() + 1);272}273}274};275276277jvmtiError278JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {279280LoadedClassesClosure closure(env);281{282// To get a consistent list of classes we need MultiArray_lock to ensure283// array classes aren't created.284MutexLocker ma(MultiArray_lock);285286// Iterate through all classes in ClassLoaderDataGraph287// and collect them using the LoadedClassesClosure288ClassLoaderDataGraph::loaded_classes_do(&closure);289}290291// Return results by extracting the collected contents into a list292// allocated via JvmtiEnv293jclass* result_list;294jvmtiError error = env->Allocate(closure.get_count() * sizeof(jclass),295(unsigned char**)&result_list);296297if (error == JVMTI_ERROR_NONE) {298int count = closure.extract(result_list);299*classCountPtr = count;300*classesPtr = result_list;301}302return error;303}304305jvmtiError306JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,307jint* classCountPtr, jclass** classesPtr) {308// Since SystemDictionary::classes_do only takes a function pointer309// and doesn't call back with a closure data pointer,310// we can only pass static methods.311JvmtiGetLoadedClassesClosure closure(initiatingLoader);312{313// To get a consistent list of classes we need MultiArray_lock to ensure314// array classes aren't created, and SystemDictionary_lock to ensure that315// classes aren't added to the system dictionary,316MutexLocker ma(MultiArray_lock);317MutexLocker sd(SystemDictionary_lock);318// First, count the classes in the system dictionary which have this loader recorded319// as an initiating loader. For basic type arrays this information is not recorded320// so GetClassLoaderClasses will return all of the basic type arrays. This is okay321// because the defining loader for basic type arrays is always the boot class loader322// and these classes are "visible" to all loaders.323SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment_with_loader);324Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment_for_basic_type_arrays);325// Next, fill in the classes326closure.allocate();327SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add_with_loader);328Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add_for_basic_type_arrays);329// Drop the SystemDictionary_lock, so the results could be wrong from here,330// but we still have a snapshot.331}332// Post results333jclass* result_list;334jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),335(unsigned char**)&result_list);336if (err != JVMTI_ERROR_NONE) {337return err;338}339closure.extract(env, result_list);340*classCountPtr = closure.get_count();341*classesPtr = result_list;342return JVMTI_ERROR_NONE;343}344345346