Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/prims/jvmtiGetLoadedClasses.cpp
32285 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 ((o != NULL) && (UseG1GC || (UseShenandoahGC && ShenandoahSATBBarrier))) {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()));62}6364int extract(jclass* result_list) {65// The size of the Stack will be 0 after extract, so get it here66int count = (int)_classStack.size();67int i = count;6869// Pop all jclasses, fill backwards70while (!_classStack.is_empty()) {71jclass klass_handle = _classStack.pop();72oop klass_mirror = JNIHandles::resolve(klass_handle);73ensure_klass_alive(klass_mirror);74result_list[--i] = klass_handle;75}7677// Return the number of elements written78return count;79}8081// Return current size of the Stack82int get_count() {83return (int)_classStack.size();84}85};8687// The closure for GetClassLoaderClasses88class JvmtiGetLoadedClassesClosure : public StackObj {89// Since the SystemDictionary::classes_do callback90// doesn't pass a closureData pointer,91// we use a thread-local slot to hold a pointer to92// a stack allocated instance of this structure.93private:94jobject _initiatingLoader;95int _count;96Handle* _list;97int _index;9899private:100// Getting and setting the thread local pointer101static JvmtiGetLoadedClassesClosure* get_this() {102JvmtiGetLoadedClassesClosure* result = NULL;103JavaThread* thread = JavaThread::current();104result = thread->get_jvmti_get_loaded_classes_closure();105return result;106}107static void set_this(JvmtiGetLoadedClassesClosure* that) {108JavaThread* thread = JavaThread::current();109thread->set_jvmti_get_loaded_classes_closure(that);110}111112public:113// Constructor/Destructor114JvmtiGetLoadedClassesClosure() {115JvmtiGetLoadedClassesClosure* that = get_this();116assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");117_initiatingLoader = NULL;118_count = 0;119_list = NULL;120_index = 0;121set_this(this);122}123124JvmtiGetLoadedClassesClosure(jobject initiatingLoader) {125JvmtiGetLoadedClassesClosure* that = get_this();126assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");127_initiatingLoader = initiatingLoader;128_count = 0;129_list = NULL;130_index = 0;131set_this(this);132}133134~JvmtiGetLoadedClassesClosure() {135JvmtiGetLoadedClassesClosure* that = get_this();136assert(that != NULL, "JvmtiGetLoadedClassesClosure not found");137set_this(NULL);138_initiatingLoader = NULL;139_count = 0;140if (_list != NULL) {141FreeHeap(_list);142_list = NULL;143}144_index = 0;145}146147// Accessors.148jobject get_initiatingLoader() {149return _initiatingLoader;150}151152int get_count() {153return _count;154}155156void set_count(int value) {157_count = value;158}159160Handle* get_list() {161return _list;162}163164void set_list(Handle* value) {165_list = value;166}167168int get_index() {169return _index;170}171172void set_index(int value) {173_index = value;174}175176Handle get_element(int index) {177if ((_list != NULL) && (index < _count)) {178return _list[index];179} else {180assert(false, "empty get_element");181return Handle();182}183}184185void set_element(int index, Handle value) {186if ((_list != NULL) && (index < _count)) {187_list[index] = value;188} else {189assert(false, "bad set_element");190}191}192193// Other predicates194bool available() {195return (_list != NULL);196}197198#ifdef ASSERT199// For debugging.200void check(int limit) {201for (int i = 0; i < limit; i += 1) {202assert(Universe::heap()->is_in(get_element(i)()), "check fails");203}204}205#endif206207// Public methods that get called within the scope of the closure208void allocate() {209_list = NEW_C_HEAP_ARRAY(Handle, _count, mtInternal);210assert(_list != NULL, "Out of memory");211if (_list == NULL) {212_count = 0;213}214}215216void extract(JvmtiEnv *env, jclass* result) {217for (int index = 0; index < _count; index += 1) {218result[index] = (jclass) env->jni_reference(get_element(index));219}220}221222static void increment_with_loader(Klass* k, ClassLoaderData* loader_data) {223JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();224oop class_loader = loader_data->class_loader();225if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {226for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {227that->set_count(that->get_count() + 1);228}229}230}231232static void prim_array_increment_with_loader(Klass* array, ClassLoaderData* loader_data) {233JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();234oop class_loader = loader_data->class_loader();235if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {236that->set_count(that->get_count() + 1);237}238}239240static void add_with_loader(Klass* k, ClassLoaderData* loader_data) {241JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();242if (that->available()) {243oop class_loader = loader_data->class_loader();244if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {245for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {246oop mirror = l->java_mirror();247that->set_element(that->get_index(), mirror);248that->set_index(that->get_index() + 1);249}250}251}252}253254// increment the count for the given basic type array class (and any255// multi-dimensional arrays). For example, for [B we check for256// [[B, [[[B, .. and the count is incremented for each one that exists.257static void increment_for_basic_type_arrays(Klass* k) {258JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();259assert(that != NULL, "no JvmtiGetLoadedClassesClosure");260for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {261that->set_count(that->get_count() + 1);262}263}264265// add the basic type array class and its multi-dimensional array classes to the list266static void add_for_basic_type_arrays(Klass* k) {267JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();268assert(that != NULL, "no JvmtiGetLoadedClassesClosure");269assert(that->available(), "no list");270for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {271oop mirror = l->java_mirror();272that->set_element(that->get_index(), mirror);273that->set_index(that->get_index() + 1);274}275}276};277278279jvmtiError280JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {281282LoadedClassesClosure closure(env);283{284// To get a consistent list of classes we need MultiArray_lock to ensure285// array classes aren't created.286MutexLocker ma(MultiArray_lock);287288// Iterate through all classes in ClassLoaderDataGraph289// and collect them using the LoadedClassesClosure290ClassLoaderDataGraph::loaded_classes_do(&closure);291}292293// Return results by extracting the collected contents into a list294// allocated via JvmtiEnv295jclass* result_list;296jvmtiError error = env->Allocate(closure.get_count() * sizeof(jclass),297(unsigned char**)&result_list);298299if (error == JVMTI_ERROR_NONE) {300int count = closure.extract(result_list);301*classCountPtr = count;302*classesPtr = result_list;303}304return error;305}306307jvmtiError308JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,309jint* classCountPtr, jclass** classesPtr) {310// Since SystemDictionary::classes_do only takes a function pointer311// and doesn't call back with a closure data pointer,312// we can only pass static methods.313JvmtiGetLoadedClassesClosure closure(initiatingLoader);314{315// To get a consistent list of classes we need MultiArray_lock to ensure316// array classes aren't created, and SystemDictionary_lock to ensure that317// classes aren't added to the system dictionary,318MutexLocker ma(MultiArray_lock);319MutexLocker sd(SystemDictionary_lock);320// First, count the classes in the system dictionary which have this loader recorded321// as an initiating loader. For basic type arrays this information is not recorded322// so GetClassLoaderClasses will return all of the basic type arrays. This is okay323// because the defining loader for basic type arrays is always the boot class loader324// and these classes are "visible" to all loaders.325SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment_with_loader);326Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment_for_basic_type_arrays);327// Next, fill in the classes328closure.allocate();329SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add_with_loader);330Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add_for_basic_type_arrays);331// Drop the SystemDictionary_lock, so the results could be wrong from here,332// but we still have a snapshot.333}334// Post results335jclass* result_list;336jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),337(unsigned char**)&result_list);338if (err != JVMTI_ERROR_NONE) {339return err;340}341closure.extract(env, result_list);342*classCountPtr = closure.get_count();343*classesPtr = result_list;344return JVMTI_ERROR_NONE;345}346347348