src/share/vm/prims/jvmtiGetLoadedClasses.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/prims/jvmtiGetLoadedClasses.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,326 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/systemDictionary.hpp"
    1.30 +#include "memory/universe.inline.hpp"
    1.31 +#include "prims/jvmtiGetLoadedClasses.hpp"
    1.32 +#include "runtime/thread.hpp"
    1.33 +
    1.34 +
    1.35 +// The closure for GetLoadedClasses
    1.36 +class LoadedClassesClosure : public KlassClosure {
    1.37 +private:
    1.38 +  Stack<jclass, mtInternal> _classStack;
    1.39 +  JvmtiEnv* _env;
    1.40 +
    1.41 +public:
    1.42 +  LoadedClassesClosure(JvmtiEnv* env) {
    1.43 +    _env = env;
    1.44 +  }
    1.45 +
    1.46 +  void do_klass(Klass* k) {
    1.47 +    // Collect all jclasses
    1.48 +    _classStack.push((jclass) _env->jni_reference(k->java_mirror()));
    1.49 +  }
    1.50 +
    1.51 +  int extract(jclass* result_list) {
    1.52 +    // The size of the Stack will be 0 after extract, so get it here
    1.53 +    int count = (int)_classStack.size();
    1.54 +    int i = count;
    1.55 +
    1.56 +    // Pop all jclasses, fill backwards
    1.57 +    while (!_classStack.is_empty()) {
    1.58 +      result_list[--i] = _classStack.pop();
    1.59 +    }
    1.60 +
    1.61 +    // Return the number of elements written
    1.62 +    return count;
    1.63 +  }
    1.64 +
    1.65 +  // Return current size of the Stack
    1.66 +  int get_count() {
    1.67 +    return (int)_classStack.size();
    1.68 +  }
    1.69 +};
    1.70 +
    1.71 +// The closure for GetClassLoaderClasses
    1.72 +class JvmtiGetLoadedClassesClosure : public StackObj {
    1.73 +  // Since the SystemDictionary::classes_do callback
    1.74 +  // doesn't pass a closureData pointer,
    1.75 +  // we use a thread-local slot to hold a pointer to
    1.76 +  // a stack allocated instance of this structure.
    1.77 + private:
    1.78 +  jobject _initiatingLoader;
    1.79 +  int     _count;
    1.80 +  Handle* _list;
    1.81 +  int     _index;
    1.82 +
    1.83 + private:
    1.84 +  // Getting and setting the thread local pointer
    1.85 +  static JvmtiGetLoadedClassesClosure* get_this() {
    1.86 +    JvmtiGetLoadedClassesClosure* result = NULL;
    1.87 +    JavaThread* thread = JavaThread::current();
    1.88 +    result = thread->get_jvmti_get_loaded_classes_closure();
    1.89 +    return result;
    1.90 +  }
    1.91 +  static void set_this(JvmtiGetLoadedClassesClosure* that) {
    1.92 +    JavaThread* thread = JavaThread::current();
    1.93 +    thread->set_jvmti_get_loaded_classes_closure(that);
    1.94 +  }
    1.95 +
    1.96 + public:
    1.97 +  // Constructor/Destructor
    1.98 +  JvmtiGetLoadedClassesClosure() {
    1.99 +    JvmtiGetLoadedClassesClosure* that = get_this();
   1.100 +    assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
   1.101 +    _initiatingLoader = NULL;
   1.102 +    _count = 0;
   1.103 +    _list = NULL;
   1.104 +    _index = 0;
   1.105 +    set_this(this);
   1.106 +  }
   1.107 +
   1.108 +  JvmtiGetLoadedClassesClosure(jobject initiatingLoader) {
   1.109 +    JvmtiGetLoadedClassesClosure* that = get_this();
   1.110 +    assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
   1.111 +    _initiatingLoader = initiatingLoader;
   1.112 +    _count = 0;
   1.113 +    _list = NULL;
   1.114 +    _index = 0;
   1.115 +    set_this(this);
   1.116 +  }
   1.117 +
   1.118 +  ~JvmtiGetLoadedClassesClosure() {
   1.119 +    JvmtiGetLoadedClassesClosure* that = get_this();
   1.120 +    assert(that != NULL, "JvmtiGetLoadedClassesClosure not found");
   1.121 +    set_this(NULL);
   1.122 +    _initiatingLoader = NULL;
   1.123 +    _count = 0;
   1.124 +    if (_list != NULL) {
   1.125 +      FreeHeap(_list);
   1.126 +      _list = NULL;
   1.127 +    }
   1.128 +    _index = 0;
   1.129 +  }
   1.130 +
   1.131 +  // Accessors.
   1.132 +  jobject get_initiatingLoader() {
   1.133 +    return _initiatingLoader;
   1.134 +  }
   1.135 +
   1.136 +  int get_count() {
   1.137 +    return _count;
   1.138 +  }
   1.139 +
   1.140 +  void set_count(int value) {
   1.141 +    _count = value;
   1.142 +  }
   1.143 +
   1.144 +  Handle* get_list() {
   1.145 +    return _list;
   1.146 +  }
   1.147 +
   1.148 +  void set_list(Handle* value) {
   1.149 +    _list = value;
   1.150 +  }
   1.151 +
   1.152 +  int get_index() {
   1.153 +    return _index;
   1.154 +  }
   1.155 +
   1.156 +  void set_index(int value) {
   1.157 +    _index = value;
   1.158 +  }
   1.159 +
   1.160 +  Handle get_element(int index) {
   1.161 +    if ((_list != NULL) && (index < _count)) {
   1.162 +      return _list[index];
   1.163 +    } else {
   1.164 +      assert(false, "empty get_element");
   1.165 +      return Handle();
   1.166 +    }
   1.167 +  }
   1.168 +
   1.169 +  void set_element(int index, Handle value) {
   1.170 +    if ((_list != NULL) && (index < _count)) {
   1.171 +      _list[index] = value;
   1.172 +    } else {
   1.173 +      assert(false, "bad set_element");
   1.174 +    }
   1.175 +  }
   1.176 +
   1.177 +  // Other predicates
   1.178 +  bool available() {
   1.179 +    return (_list != NULL);
   1.180 +  }
   1.181 +
   1.182 +#ifdef ASSERT
   1.183 +  // For debugging.
   1.184 +  void check(int limit) {
   1.185 +    for (int i = 0; i < limit; i += 1) {
   1.186 +      assert(Universe::heap()->is_in(get_element(i)()), "check fails");
   1.187 +    }
   1.188 +  }
   1.189 +#endif
   1.190 +
   1.191 +  // Public methods that get called within the scope of the closure
   1.192 +  void allocate() {
   1.193 +    _list = NEW_C_HEAP_ARRAY(Handle, _count, mtInternal);
   1.194 +    assert(_list != NULL, "Out of memory");
   1.195 +    if (_list == NULL) {
   1.196 +      _count = 0;
   1.197 +    }
   1.198 +  }
   1.199 +
   1.200 +  void extract(JvmtiEnv *env, jclass* result) {
   1.201 +    for (int index = 0; index < _count; index += 1) {
   1.202 +      result[index] = (jclass) env->jni_reference(get_element(index));
   1.203 +    }
   1.204 +  }
   1.205 +
   1.206 +  static void increment_with_loader(Klass* k, ClassLoaderData* loader_data) {
   1.207 +    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
   1.208 +    oop class_loader = loader_data->class_loader();
   1.209 +    if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
   1.210 +      for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
   1.211 +        that->set_count(that->get_count() + 1);
   1.212 +      }
   1.213 +    }
   1.214 +  }
   1.215 +
   1.216 +  static void prim_array_increment_with_loader(Klass* array, ClassLoaderData* loader_data) {
   1.217 +    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
   1.218 +    oop class_loader = loader_data->class_loader();
   1.219 +    if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
   1.220 +      that->set_count(that->get_count() + 1);
   1.221 +    }
   1.222 +  }
   1.223 +
   1.224 +  static void add_with_loader(Klass* k, ClassLoaderData* loader_data) {
   1.225 +    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
   1.226 +    if (that->available()) {
   1.227 +      oop class_loader = loader_data->class_loader();
   1.228 +      if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
   1.229 +        for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
   1.230 +          oop mirror = l->java_mirror();
   1.231 +          that->set_element(that->get_index(), mirror);
   1.232 +          that->set_index(that->get_index() + 1);
   1.233 +        }
   1.234 +      }
   1.235 +    }
   1.236 +  }
   1.237 +
   1.238 +  // increment the count for the given basic type array class (and any
   1.239 +  // multi-dimensional arrays). For example, for [B we check for
   1.240 +  // [[B, [[[B, .. and the count is incremented for each one that exists.
   1.241 +  static void increment_for_basic_type_arrays(Klass* k) {
   1.242 +    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
   1.243 +    assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
   1.244 +    for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
   1.245 +      that->set_count(that->get_count() + 1);
   1.246 +    }
   1.247 +  }
   1.248 +
   1.249 +  // add the basic type array class and its multi-dimensional array classes to the list
   1.250 +  static void add_for_basic_type_arrays(Klass* k) {
   1.251 +    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
   1.252 +    assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
   1.253 +    assert(that->available(), "no list");
   1.254 +    for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
   1.255 +      oop mirror = l->java_mirror();
   1.256 +      that->set_element(that->get_index(), mirror);
   1.257 +      that->set_index(that->get_index() + 1);
   1.258 +    }
   1.259 +  }
   1.260 +};
   1.261 +
   1.262 +
   1.263 +jvmtiError
   1.264 +JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
   1.265 +
   1.266 +  LoadedClassesClosure closure(env);
   1.267 +  {
   1.268 +    // To get a consistent list of classes we need MultiArray_lock to ensure
   1.269 +    // array classes aren't created.
   1.270 +    MutexLocker ma(MultiArray_lock);
   1.271 +
   1.272 +    // Iterate through all classes in ClassLoaderDataGraph
   1.273 +    // and collect them using the LoadedClassesClosure
   1.274 +    ClassLoaderDataGraph::loaded_classes_do(&closure);
   1.275 +  }
   1.276 +
   1.277 +  // Return results by extracting the collected contents into a list
   1.278 +  // allocated via JvmtiEnv
   1.279 +  jclass* result_list;
   1.280 +  jvmtiError error = env->Allocate(closure.get_count() * sizeof(jclass),
   1.281 +                               (unsigned char**)&result_list);
   1.282 +
   1.283 +  if (error == JVMTI_ERROR_NONE) {
   1.284 +    int count = closure.extract(result_list);
   1.285 +    *classCountPtr = count;
   1.286 +    *classesPtr = result_list;
   1.287 +  }
   1.288 +  return error;
   1.289 +}
   1.290 +
   1.291 +jvmtiError
   1.292 +JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,
   1.293 +                                             jint* classCountPtr, jclass** classesPtr) {
   1.294 +  // Since SystemDictionary::classes_do only takes a function pointer
   1.295 +  // and doesn't call back with a closure data pointer,
   1.296 +  // we can only pass static methods.
   1.297 +  JvmtiGetLoadedClassesClosure closure(initiatingLoader);
   1.298 +  {
   1.299 +    // To get a consistent list of classes we need MultiArray_lock to ensure
   1.300 +    // array classes aren't created, and SystemDictionary_lock to ensure that
   1.301 +    // classes aren't added to the system dictionary,
   1.302 +    MutexLocker ma(MultiArray_lock);
   1.303 +    MutexLocker sd(SystemDictionary_lock);
   1.304 +    // First, count the classes in the system dictionary which have this loader recorded
   1.305 +    // as an initiating loader. For basic type arrays this information is not recorded
   1.306 +    // so GetClassLoaderClasses will return all of the basic type arrays. This is okay
   1.307 +    // because the defining loader for basic type arrays is always the boot class loader
   1.308 +    // and these classes are "visible" to all loaders.
   1.309 +    SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment_with_loader);
   1.310 +    Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment_for_basic_type_arrays);
   1.311 +    // Next, fill in the classes
   1.312 +    closure.allocate();
   1.313 +    SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add_with_loader);
   1.314 +    Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add_for_basic_type_arrays);
   1.315 +    // Drop the SystemDictionary_lock, so the results could be wrong from here,
   1.316 +    // but we still have a snapshot.
   1.317 +  }
   1.318 +  // Post results
   1.319 +  jclass* result_list;
   1.320 +  jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),
   1.321 +                                 (unsigned char**)&result_list);
   1.322 +  if (err != JVMTI_ERROR_NONE) {
   1.323 +    return err;
   1.324 +  }
   1.325 +  closure.extract(env, result_list);
   1.326 +  *classCountPtr = closure.get_count();
   1.327 +  *classesPtr = result_list;
   1.328 +  return JVMTI_ERROR_NONE;
   1.329 +}

mercurial