< prev index next >

src/hotspot/share/prims/jvmtiGetLoadedClasses.cpp

Print this page

 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 

 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/inlineKlass.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"
 37 
 38 // The closure for GetLoadedClasses
 39 class LoadedClassesClosure : public KlassClosure {
 40 private:
 41   Stack<jclass, mtInternal> _classStack;
 42   JvmtiEnv* _env;
 43   Thread*   _cur_thread;
 44   bool      _dictionary_walk;
 45 
 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();
 49     int i = count;
 50 
 51     // Pop all jclasses, fill backwards
 52     while (!_classStack.is_empty()) {
 53       result_list[--i] = _classStack.pop();
 54     }
 55 
 56     // Return the number of elements written
 57     return count;
 58   }
 59 
 60   // Return current size of the Stack
 61   int get_count() const {
 62     return (int)_classStack.size();
 63   }
 64 
 65   // Some klasses should not be reported by GetLoadedClasses/GetClassLoaderClasses
 66   bool exclude_klass(Klass* k) const {
 67     // Direct instances of ObjArrayKlass represent the Java types that Java code can see.
 68     // RefArrayKlass/FlatArrayKlass describe different implementations of the arrays, filter them out.
 69     if (k->is_objArray_klass() && k->kind() != Klass::KlassKind::ObjArrayKlassKind) {
 70       return true;
 71     }
 72     return false;
 73   }
 74 
 75 public:
 76   LoadedClassesClosure(JvmtiEnv* env, bool dictionary_walk) :
 77       _env(env),
 78       _cur_thread(Thread::current()),
 79       _dictionary_walk(dictionary_walk) {
 80   }
 81 
 82   void do_klass(Klass* k) {
 83     // Collect all jclasses
 84     if (!exclude_klass(k)) {
 85       _classStack.push((jclass)_env->jni_reference(Handle(_cur_thread, k->java_mirror())));
 86     }
 87     if (_dictionary_walk) {
 88       // Collect array classes this way when walking the dictionary (because array classes are
 89       // not in the dictionary).
 90       for (Klass* l = k->array_klass_or_null(); l != nullptr; l = l->array_klass_or_null()) {
 91         if (!exclude_klass(l)) {
 92           _classStack.push((jclass)_env->jni_reference(Handle(_cur_thread, l->java_mirror())));
 93         }
 94       }
 95     }
 96   }
 97 
 98   jvmtiError get_result(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
 99     // Return results by extracting the collected contents into a list
100     // allocated via JvmtiEnv
101     jclass* result_list;
102     jvmtiError error = env->Allocate(get_count() * sizeof(jclass),
103                                (unsigned char**)&result_list);
104 
105     if (error == JVMTI_ERROR_NONE) {
106       int count = extract(result_list);
107       *classCountPtr = count;
108       *classesPtr = result_list;
109     }
110     return error;
111   }
112 };
113 
< prev index next >