2
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "classfile/classLoaderDataGraph.hpp"
27
#include "classfile/dictionary.hpp"
28
#include "classfile/javaClasses.hpp"
29
#include "gc/shared/collectedHeap.hpp"
30
#include "memory/universe.hpp"
31
#include "oops/klass.inline.hpp"
32
#include "prims/jvmtiGetLoadedClasses.hpp"
33
#include "runtime/handles.inline.hpp"
34
#include "runtime/javaThread.hpp"
35
#include "runtime/jniHandles.inline.hpp"
36
#include "utilities/stack.inline.hpp"
38
// The closure for GetLoadedClasses
39
class LoadedClassesClosure : public KlassClosure {
41
Stack<jclass, mtInternal> _classStack;
44
bool _dictionary_walk;
46
int extract(jclass* result_list) {
47
// The size of the Stack will be 0 after extract, so get it here
48
int count = (int)_classStack.size();
51
// Pop all jclasses, fill backwards
52
while (!_classStack.is_empty()) {
53
result_list[--i] = _classStack.pop();
56
// Return the number of elements written
60
// Return current size of the Stack
62
return (int)_classStack.size();
66
LoadedClassesClosure(JvmtiEnv* env, bool dictionary_walk) :
68
_cur_thread(Thread::current()),
69
_dictionary_walk(dictionary_walk) {
72
void do_klass(Klass* k) {
73
// Collect all jclasses
74
_classStack.push((jclass) _env->jni_reference(Handle(_cur_thread, k->java_mirror())));
75
if (_dictionary_walk) {
76
// Collect array classes this way when walking the dictionary (because array classes are
77
// not in the dictionary).
78
for (Klass* l = k->array_klass_or_null(); l != nullptr; l = l->array_klass_or_null()) {
79
_classStack.push((jclass) _env->jni_reference(Handle(_cur_thread, l->java_mirror())));
84
jvmtiError get_result(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
85
// Return results by extracting the collected contents into a list
86
// allocated via JvmtiEnv
88
jvmtiError error = env->Allocate(get_count() * sizeof(jclass),
89
(unsigned char**)&result_list);
91
if (error == JVMTI_ERROR_NONE) {
92
int count = extract(result_list);
93
*classCountPtr = count;
94
*classesPtr = result_list;
101
JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
103
LoadedClassesClosure closure(env, false);
105
// Iterate through all classes in ClassLoaderDataGraph
106
// and collect them using the LoadedClassesClosure
107
MutexLocker mcld(ClassLoaderDataGraph_lock);
108
ClassLoaderDataGraph::loaded_classes_do_keepalive(&closure);
111
return closure.get_result(env, classCountPtr, classesPtr);
115
JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,
116
jint* classCountPtr, jclass** classesPtr) {
118
LoadedClassesClosure closure(env, true);
120
// To get a consistent list of classes we need MultiArray_lock to ensure
121
// array classes aren't created by another thread during this walk. This walks through the
122
// InstanceKlass::_array_klasses links.
123
RecursiveLocker ma(MultiArray_lock, Thread::current());
124
MutexLocker sd(SystemDictionary_lock);
125
oop loader = JNIHandles::resolve(initiatingLoader);
126
// All classes loaded from this loader as initiating loader are
127
// requested, so only need to walk this loader's ClassLoaderData
128
// dictionary, or the null ClassLoaderData dictionary for bootstrap loader.
129
if (loader != nullptr) {
130
ClassLoaderData* data = java_lang_ClassLoader::loader_data_acquire(loader);
131
// ClassLoader may not be used yet for loading.
132
if (data != nullptr && data->dictionary() != nullptr) {
133
data->dictionary()->all_entries_do(&closure);
136
ClassLoaderData::the_null_class_loader_data()->dictionary()->all_entries_do(&closure);
138
// Get basic arrays for all loaders.
139
Universe::basic_type_classes_do(&closure);
142
return closure.get_result(env, classCountPtr, classesPtr);