1 /*
  2  * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "classfile/classLoaderDataGraph.hpp"
 26 #include "classfile/dictionary.hpp"
 27 #include "classfile/javaClasses.hpp"
 28 #include "gc/shared/collectedHeap.hpp"
 29 #include "memory/universe.hpp"
 30 #include "oops/klass.inline.hpp"
 31 #include "prims/jvmtiGetLoadedClasses.hpp"
 32 #include "runtime/handles.inline.hpp"
 33 #include "runtime/javaThread.hpp"
 34 #include "runtime/jniHandles.inline.hpp"
 35 #include "utilities/stack.inline.hpp"
 36 
 37 // The closure for GetLoadedClasses
 38 class LoadedClassesClosure : public KlassClosure {
 39 private:
 40   Stack<jclass, mtInternal> _classStack;
 41   JvmtiEnv* _env;
 42   Thread*   _cur_thread;
 43   bool      _dictionary_walk;
 44 
 45   int extract(jclass* result_list) {
 46     // The size of the Stack will be 0 after extract, so get it here
 47     int count = (int)_classStack.size();
 48     int i = count;
 49 
 50     // Pop all jclasses, fill backwards
 51     while (!_classStack.is_empty()) {
 52       result_list[--i] = _classStack.pop();
 53     }
 54 
 55     // Return the number of elements written
 56     return count;
 57   }
 58 
 59   // Return current size of the Stack
 60   int get_count() {
 61     return (int)_classStack.size();
 62   }
 63 
 64 public:
 65   LoadedClassesClosure(JvmtiEnv* env, bool dictionary_walk) :
 66       _env(env),
 67       _cur_thread(Thread::current()),
 68       _dictionary_walk(dictionary_walk) {
 69   }
 70 
 71   void do_klass(Klass* k) {
 72     // Collect all jclasses
 73     _classStack.push((jclass) _env->jni_reference(Handle(_cur_thread, k->java_mirror())));
 74     if (_dictionary_walk) {
 75       // Collect array classes this way when walking the dictionary (because array classes are
 76       // not in the dictionary).
 77       for (Klass* l = k->array_klass_or_null(); l != nullptr; l = l->array_klass_or_null()) {
 78         _classStack.push((jclass) _env->jni_reference(Handle(_cur_thread, l->java_mirror())));
 79       }
 80     }
 81   }
 82 
 83   jvmtiError get_result(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
 84     // Return results by extracting the collected contents into a list
 85     // allocated via JvmtiEnv
 86     jclass* result_list;
 87     jvmtiError error = env->Allocate(get_count() * sizeof(jclass),
 88                                (unsigned char**)&result_list);
 89 
 90     if (error == JVMTI_ERROR_NONE) {
 91       int count = extract(result_list);
 92       *classCountPtr = count;
 93       *classesPtr = result_list;
 94     }
 95     return error;
 96   }
 97 };
 98 
 99 jvmtiError
100 JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
101 
102   LoadedClassesClosure closure(env, false);
103   {
104     // Iterate through all classes in ClassLoaderDataGraph
105     // and collect them using the LoadedClassesClosure
106     MutexLocker mcld(ClassLoaderDataGraph_lock);
107     ClassLoaderDataGraph::loaded_classes_do_keepalive(&closure);
108   }
109 
110   return closure.get_result(env, classCountPtr, classesPtr);
111 }
112 
113 jvmtiError
114 JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,
115                                              jint* classCountPtr, jclass** classesPtr) {
116 
117   LoadedClassesClosure closure(env, true);
118   {
119     // To get a consistent list of classes we need MultiArray_lock to ensure
120     // array classes aren't created by another thread during this walk. This walks through the
121     // InstanceKlass::_array_klasses links.
122     RecursiveLocker ma(MultiArray_lock, Thread::current());
123     MutexLocker sd(SystemDictionary_lock);
124     oop loader = JNIHandles::resolve(initiatingLoader);
125     // All classes loaded from this loader as initiating loader are
126     // requested, so only need to walk this loader's ClassLoaderData
127     // dictionary, or the null ClassLoaderData dictionary for bootstrap loader.
128     if (loader != nullptr) {
129       ClassLoaderData* data = java_lang_ClassLoader::loader_data_acquire(loader);
130       // ClassLoader may not be used yet for loading.
131       if (data != nullptr && data->dictionary() != nullptr) {
132         data->dictionary()->all_entries_do(&closure);
133       }
134     } else {
135       ClassLoaderData::the_null_class_loader_data()->dictionary()->all_entries_do(&closure);
136     }
137     // Get basic arrays for all loaders.
138     Universe::basic_type_classes_do(&closure);
139   }
140 
141   return closure.get_result(env, classCountPtr, classesPtr);
142 }