1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/prims/jvmtiGetLoadedClasses.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,325 @@ 1.4 +/* 1.5 + * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 + 1.29 +# include "incls/_precompiled.incl" 1.30 +# include "incls/_jvmtiGetLoadedClasses.cpp.incl" 1.31 + 1.32 + 1.33 +// The closure for GetLoadedClasses and GetClassLoaderClasses 1.34 +class JvmtiGetLoadedClassesClosure : public StackObj { 1.35 + // Since the SystemDictionary::classes_do callback 1.36 + // doesn't pass a closureData pointer, 1.37 + // we use a thread-local slot to hold a pointer to 1.38 + // a stack allocated instance of this structure. 1.39 + private: 1.40 + jobject _initiatingLoader; 1.41 + int _count; 1.42 + Handle* _list; 1.43 + int _index; 1.44 + 1.45 + private: 1.46 + // Getting and setting the thread local pointer 1.47 + static JvmtiGetLoadedClassesClosure* get_this() { 1.48 + JvmtiGetLoadedClassesClosure* result = NULL; 1.49 + JavaThread* thread = JavaThread::current(); 1.50 + result = thread->get_jvmti_get_loaded_classes_closure(); 1.51 + return result; 1.52 + } 1.53 + static void set_this(JvmtiGetLoadedClassesClosure* that) { 1.54 + JavaThread* thread = JavaThread::current(); 1.55 + thread->set_jvmti_get_loaded_classes_closure(that); 1.56 + } 1.57 + 1.58 + public: 1.59 + // Constructor/Destructor 1.60 + JvmtiGetLoadedClassesClosure() { 1.61 + JvmtiGetLoadedClassesClosure* that = get_this(); 1.62 + assert(that == NULL, "JvmtiGetLoadedClassesClosure in use"); 1.63 + _initiatingLoader = NULL; 1.64 + _count = 0; 1.65 + _list = NULL; 1.66 + _index = 0; 1.67 + set_this(this); 1.68 + } 1.69 + 1.70 + JvmtiGetLoadedClassesClosure(jobject initiatingLoader) { 1.71 + JvmtiGetLoadedClassesClosure* that = get_this(); 1.72 + assert(that == NULL, "JvmtiGetLoadedClassesClosure in use"); 1.73 + _initiatingLoader = initiatingLoader; 1.74 + _count = 0; 1.75 + _list = NULL; 1.76 + _index = 0; 1.77 + set_this(this); 1.78 + } 1.79 + 1.80 + ~JvmtiGetLoadedClassesClosure() { 1.81 + JvmtiGetLoadedClassesClosure* that = get_this(); 1.82 + assert(that != NULL, "JvmtiGetLoadedClassesClosure not found"); 1.83 + set_this(NULL); 1.84 + _initiatingLoader = NULL; 1.85 + _count = 0; 1.86 + if (_list != NULL) { 1.87 + FreeHeap(_list); 1.88 + _list = NULL; 1.89 + } 1.90 + _index = 0; 1.91 + } 1.92 + 1.93 + // Accessors. 1.94 + jobject get_initiatingLoader() { 1.95 + return _initiatingLoader; 1.96 + } 1.97 + 1.98 + int get_count() { 1.99 + return _count; 1.100 + } 1.101 + 1.102 + void set_count(int value) { 1.103 + _count = value; 1.104 + } 1.105 + 1.106 + Handle* get_list() { 1.107 + return _list; 1.108 + } 1.109 + 1.110 + void set_list(Handle* value) { 1.111 + _list = value; 1.112 + } 1.113 + 1.114 + int get_index() { 1.115 + return _index; 1.116 + } 1.117 + 1.118 + void set_index(int value) { 1.119 + _index = value; 1.120 + } 1.121 + 1.122 + Handle get_element(int index) { 1.123 + if ((_list != NULL) && (index < _count)) { 1.124 + return _list[index]; 1.125 + } else { 1.126 + assert(false, "empty get_element"); 1.127 + return Handle(); 1.128 + } 1.129 + } 1.130 + 1.131 + void set_element(int index, Handle value) { 1.132 + if ((_list != NULL) && (index < _count)) { 1.133 + _list[index] = value; 1.134 + } else { 1.135 + assert(false, "bad set_element"); 1.136 + } 1.137 + } 1.138 + 1.139 + // Other predicates 1.140 + bool available() { 1.141 + return (_list != NULL); 1.142 + } 1.143 + 1.144 +#ifdef ASSERT 1.145 + // For debugging. 1.146 + void check(int limit) { 1.147 + for (int i = 0; i < limit; i += 1) { 1.148 + assert(Universe::heap()->is_in(get_element(i)()), "check fails"); 1.149 + } 1.150 + } 1.151 +#endif 1.152 + 1.153 + // Public methods that get called within the scope of the closure 1.154 + void allocate() { 1.155 + _list = NEW_C_HEAP_ARRAY(Handle, _count); 1.156 + assert(_list != NULL, "Out of memory"); 1.157 + if (_list == NULL) { 1.158 + _count = 0; 1.159 + } 1.160 + } 1.161 + 1.162 + void extract(JvmtiEnv *env, jclass* result) { 1.163 + for (int index = 0; index < _count; index += 1) { 1.164 + result[index] = (jclass) env->jni_reference(get_element(index)); 1.165 + } 1.166 + } 1.167 + 1.168 + // Finally, the static methods that are the callbacks 1.169 + static void increment(klassOop k) { 1.170 + JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this(); 1.171 + if (that->get_initiatingLoader() == NULL) { 1.172 + for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) { 1.173 + that->set_count(that->get_count() + 1); 1.174 + } 1.175 + } else if (k != NULL) { 1.176 + // if initiating loader not null, just include the instance with 1 dimension 1.177 + that->set_count(that->get_count() + 1); 1.178 + } 1.179 + } 1.180 + 1.181 + static void increment_with_loader(klassOop k, oop loader) { 1.182 + JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this(); 1.183 + if (loader == JNIHandles::resolve(that->get_initiatingLoader())) { 1.184 + for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) { 1.185 + that->set_count(that->get_count() + 1); 1.186 + } 1.187 + } 1.188 + } 1.189 + 1.190 + static void prim_array_increment_with_loader(klassOop array, oop loader) { 1.191 + JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this(); 1.192 + if (loader == JNIHandles::resolve(that->get_initiatingLoader())) { 1.193 + that->set_count(that->get_count() + 1); 1.194 + } 1.195 + } 1.196 + 1.197 + static void add(klassOop k) { 1.198 + JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this(); 1.199 + if (that->available()) { 1.200 + if (that->get_initiatingLoader() == NULL) { 1.201 + for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) { 1.202 + oop mirror = Klass::cast(l)->java_mirror(); 1.203 + that->set_element(that->get_index(), mirror); 1.204 + that->set_index(that->get_index() + 1); 1.205 + } 1.206 + } else if (k != NULL) { 1.207 + // if initiating loader not null, just include the instance with 1 dimension 1.208 + oop mirror = Klass::cast(k)->java_mirror(); 1.209 + that->set_element(that->get_index(), mirror); 1.210 + that->set_index(that->get_index() + 1); 1.211 + } 1.212 + } 1.213 + } 1.214 + 1.215 + static void add_with_loader(klassOop k, oop loader) { 1.216 + JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this(); 1.217 + if (that->available()) { 1.218 + if (loader == JNIHandles::resolve(that->get_initiatingLoader())) { 1.219 + for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) { 1.220 + oop mirror = Klass::cast(l)->java_mirror(); 1.221 + that->set_element(that->get_index(), mirror); 1.222 + that->set_index(that->get_index() + 1); 1.223 + } 1.224 + } 1.225 + } 1.226 + } 1.227 + 1.228 + // increment the count for the given basic type array class (and any 1.229 + // multi-dimensional arrays). For example, for [B we check for 1.230 + // [[B, [[[B, .. and the count is incremented for each one that exists. 1.231 + static void increment_for_basic_type_arrays(klassOop k) { 1.232 + JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this(); 1.233 + assert(that != NULL, "no JvmtiGetLoadedClassesClosure"); 1.234 + for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) { 1.235 + that->set_count(that->get_count() + 1); 1.236 + } 1.237 + } 1.238 + 1.239 + // add the basic type array class and its multi-dimensional array classes to the list 1.240 + static void add_for_basic_type_arrays(klassOop k) { 1.241 + JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this(); 1.242 + assert(that != NULL, "no JvmtiGetLoadedClassesClosure"); 1.243 + assert(that->available(), "no list"); 1.244 + for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) { 1.245 + oop mirror = Klass::cast(l)->java_mirror(); 1.246 + that->set_element(that->get_index(), mirror); 1.247 + that->set_index(that->get_index() + 1); 1.248 + } 1.249 + } 1.250 +}; 1.251 + 1.252 + 1.253 +jvmtiError 1.254 +JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) { 1.255 + // Since SystemDictionary::classes_do only takes a function pointer 1.256 + // and doesn't call back with a closure data pointer, 1.257 + // we can only pass static methods. 1.258 + 1.259 + JvmtiGetLoadedClassesClosure closure; 1.260 + { 1.261 + // To get a consistent list of classes we need MultiArray_lock to ensure 1.262 + // array classes aren't created, and SystemDictionary_lock to ensure that 1.263 + // classes aren't added to the system dictionary, 1.264 + MutexLocker ma(MultiArray_lock); 1.265 + MutexLocker sd(SystemDictionary_lock); 1.266 + 1.267 + // First, count the classes 1.268 + SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment); 1.269 + Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment); 1.270 + // Next, fill in the classes 1.271 + closure.allocate(); 1.272 + SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add); 1.273 + Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add); 1.274 + // Drop the SystemDictionary_lock, so the results could be wrong from here, 1.275 + // but we still have a snapshot. 1.276 + } 1.277 + // Post results 1.278 + jclass* result_list; 1.279 + jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass), 1.280 + (unsigned char**)&result_list); 1.281 + if (err != JVMTI_ERROR_NONE) { 1.282 + return err; 1.283 + } 1.284 + closure.extract(env, result_list); 1.285 + *classCountPtr = closure.get_count(); 1.286 + *classesPtr = result_list; 1.287 + return JVMTI_ERROR_NONE; 1.288 +} 1.289 + 1.290 +jvmtiError 1.291 +JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader, 1.292 + jint* classCountPtr, jclass** classesPtr) { 1.293 + // Since SystemDictionary::classes_do only takes a function pointer 1.294 + // and doesn't call back with a closure data pointer, 1.295 + // we can only pass static methods. 1.296 + JvmtiGetLoadedClassesClosure closure(initiatingLoader); 1.297 + { 1.298 + // To get a consistent list of classes we need MultiArray_lock to ensure 1.299 + // array classes aren't created, and SystemDictionary_lock to ensure that 1.300 + // classes aren't added to the system dictionary, 1.301 + MutexLocker ma(MultiArray_lock); 1.302 + MutexLocker sd(SystemDictionary_lock); 1.303 + // First, count the classes in the system dictionary which have this loader recorded 1.304 + // as an initiating loader. For basic type arrays this information is not recorded 1.305 + // so GetClassLoaderClasses will return all of the basic type arrays. This is okay 1.306 + // because the defining loader for basic type arrays is always the boot class loader 1.307 + // and these classes are "visible" to all loaders. 1.308 + SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment_with_loader); 1.309 + Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment_for_basic_type_arrays); 1.310 + // Next, fill in the classes 1.311 + closure.allocate(); 1.312 + SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add_with_loader); 1.313 + Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add_for_basic_type_arrays); 1.314 + // Drop the SystemDictionary_lock, so the results could be wrong from here, 1.315 + // but we still have a snapshot. 1.316 + } 1.317 + // Post results 1.318 + jclass* result_list; 1.319 + jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass), 1.320 + (unsigned char**)&result_list); 1.321 + if (err != JVMTI_ERROR_NONE) { 1.322 + return err; 1.323 + } 1.324 + closure.extract(env, result_list); 1.325 + *classCountPtr = closure.get_count(); 1.326 + *classesPtr = result_list; 1.327 + return JVMTI_ERROR_NONE; 1.328 +}