hotspot/src/share/vm/prims/jvmtiGetLoadedClasses.cpp
author trims
Thu, 27 May 2010 19:08:38 -0700
changeset 5547 f4b087cbb361
parent 1 489c9b5090e2
child 7397 5b173b4ca846
permissions -rw-r--r--
6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
     2
 * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
# include "incls/_jvmtiGetLoadedClasses.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// The closure for GetLoadedClasses and GetClassLoaderClasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
class JvmtiGetLoadedClassesClosure : public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  // Since the SystemDictionary::classes_do callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  // doesn't pass a closureData pointer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  // we use a thread-local slot to hold a pointer to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  // a stack allocated instance of this structure.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
  jobject _initiatingLoader;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  int     _count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  Handle* _list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  int     _index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  // Getting and setting the thread local pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  static JvmtiGetLoadedClassesClosure* get_this() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    JvmtiGetLoadedClassesClosure* result = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    JavaThread* thread = JavaThread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    result = thread->get_jvmti_get_loaded_classes_closure();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  static void set_this(JvmtiGetLoadedClassesClosure* that) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    JavaThread* thread = JavaThread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    thread->set_jvmti_get_loaded_classes_closure(that);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  // Constructor/Destructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  JvmtiGetLoadedClassesClosure() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    JvmtiGetLoadedClassesClosure* that = get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
    _initiatingLoader = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    _list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    _index = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    set_this(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  JvmtiGetLoadedClassesClosure(jobject initiatingLoader) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    JvmtiGetLoadedClassesClosure* that = get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
    assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
    _initiatingLoader = initiatingLoader;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    _list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    _index = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    set_this(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  ~JvmtiGetLoadedClassesClosure() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    JvmtiGetLoadedClassesClosure* that = get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    assert(that != NULL, "JvmtiGetLoadedClassesClosure not found");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
    set_this(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
    _initiatingLoader = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
    _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
    if (_list != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
      FreeHeap(_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
      _list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    _index = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  // Accessors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  jobject get_initiatingLoader() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    return _initiatingLoader;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  int get_count() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    return _count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  void set_count(int value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    _count = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  Handle* get_list() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    return _list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  void set_list(Handle* value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    _list = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  int get_index() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    return _index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  void set_index(int value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    _index = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  Handle get_element(int index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    if ((_list != NULL) && (index < _count)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
      return _list[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
      assert(false, "empty get_element");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      return Handle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  void set_element(int index, Handle value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    if ((_list != NULL) && (index < _count)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
      _list[index] = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      assert(false, "bad set_element");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  // Other predicates
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  bool available() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    return (_list != NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  // For debugging.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  void check(int limit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    for (int i = 0; i < limit; i += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      assert(Universe::heap()->is_in(get_element(i)()), "check fails");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  // Public methods that get called within the scope of the closure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  void allocate() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    _list = NEW_C_HEAP_ARRAY(Handle, _count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    assert(_list != NULL, "Out of memory");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    if (_list == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
      _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  void extract(JvmtiEnv *env, jclass* result) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    for (int index = 0; index < _count; index += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
      result[index] = (jclass) env->jni_reference(get_element(index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  // Finally, the static methods that are the callbacks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  static void increment(klassOop k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    if (that->get_initiatingLoader() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
      for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
        that->set_count(that->get_count() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    } else if (k != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      // if initiating loader not null, just include the instance with 1 dimension
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
      that->set_count(that->get_count() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  static void increment_with_loader(klassOop k, oop loader) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    if (loader == JNIHandles::resolve(that->get_initiatingLoader())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
      for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
        that->set_count(that->get_count() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  static void prim_array_increment_with_loader(klassOop array, oop loader) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    if (loader == JNIHandles::resolve(that->get_initiatingLoader())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
      that->set_count(that->get_count() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  static void add(klassOop k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    if (that->available()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
      if (that->get_initiatingLoader() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
        for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
          oop mirror = Klass::cast(l)->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
          that->set_element(that->get_index(), mirror);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
          that->set_index(that->get_index() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
      } else if (k != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
        // if initiating loader not null, just include the instance with 1 dimension
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
        oop mirror = Klass::cast(k)->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
        that->set_element(that->get_index(), mirror);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
        that->set_index(that->get_index() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  static void add_with_loader(klassOop k, oop loader) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
    if (that->available()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
      if (loader == JNIHandles::resolve(that->get_initiatingLoader())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
        for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
          oop mirror = Klass::cast(l)->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
          that->set_element(that->get_index(), mirror);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
          that->set_index(that->get_index() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  // increment the count for the given basic type array class (and any
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  // multi-dimensional arrays). For example, for [B we check for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  // [[B, [[[B, .. and the count is incremented for each one that exists.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  static void increment_for_basic_type_arrays(klassOop k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      that->set_count(that->get_count() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  // add the basic type array class and its multi-dimensional array classes to the list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  static void add_for_basic_type_arrays(klassOop k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    assert(that->available(), "no list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    for (klassOop l = k; l != NULL; l = Klass::cast(l)->array_klass_or_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
      oop mirror = Klass::cast(l)->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
      that->set_element(that->get_index(), mirror);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
      that->set_index(that->get_index() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  // Since SystemDictionary::classes_do only takes a function pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  // and doesn't call back with a closure data pointer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  // we can only pass static methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  JvmtiGetLoadedClassesClosure closure;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    // To get a consistent list of classes we need MultiArray_lock to ensure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    // array classes aren't created, and SystemDictionary_lock to ensure that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    // classes aren't added to the system dictionary,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
    MutexLocker ma(MultiArray_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    MutexLocker sd(SystemDictionary_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    // First, count the classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
    // Next, fill in the classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    closure.allocate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
    // Drop the SystemDictionary_lock, so the results could be wrong from here,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
    // but we still have a snapshot.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  // Post results
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  jclass* result_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
                                 (unsigned char**)&result_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  if (err != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  closure.extract(env, result_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  *classCountPtr = closure.get_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  *classesPtr = result_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
                                             jint* classCountPtr, jclass** classesPtr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  // Since SystemDictionary::classes_do only takes a function pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  // and doesn't call back with a closure data pointer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  // we can only pass static methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  JvmtiGetLoadedClassesClosure closure(initiatingLoader);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    // To get a consistent list of classes we need MultiArray_lock to ensure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    // array classes aren't created, and SystemDictionary_lock to ensure that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
    // classes aren't added to the system dictionary,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    MutexLocker ma(MultiArray_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    MutexLocker sd(SystemDictionary_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    // First, count the classes in the system dictionary which have this loader recorded
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    // as an initiating loader. For basic type arrays this information is not recorded
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    // so GetClassLoaderClasses will return all of the basic type arrays. This is okay
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    // because the defining loader for basic type arrays is always the boot class loader
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    // and these classes are "visible" to all loaders.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::increment_with_loader);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment_for_basic_type_arrays);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    // Next, fill in the classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    closure.allocate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    SystemDictionary::classes_do(&JvmtiGetLoadedClassesClosure::add_with_loader);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
    Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add_for_basic_type_arrays);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
    // Drop the SystemDictionary_lock, so the results could be wrong from here,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    // but we still have a snapshot.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  // Post results
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
  jclass* result_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
                                 (unsigned char**)&result_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  if (err != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  closure.extract(env, result_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  *classCountPtr = closure.get_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  *classesPtr = result_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
}