src/hotspot/share/prims/jvmtiGetLoadedClasses.cpp
author rkennke
Sun, 15 Oct 2017 17:37:29 -0400
changeset 47627 6218d903d2f2
parent 47216 71c04702a3d5
child 48882 6b510cb0f14f
permissions -rw-r--r--
8189333: Fix Zero build after Atomic::xchg changes Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
     2
 * Copyright (c) 2003, 2017, 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
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "classfile/systemDictionary.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 29081
diff changeset
    27
#include "gc/shared/collectedHeap.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/universe.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "prims/jvmtiGetLoadedClasses.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
#include "runtime/thread.hpp"
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 22234
diff changeset
    31
#include "utilities/stack.inline.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
21183
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    34
// The closure for GetLoadedClasses
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    35
class LoadedClassesClosure : public KlassClosure {
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    36
private:
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    37
  Stack<jclass, mtInternal> _classStack;
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    38
  JvmtiEnv* _env;
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
    39
  Thread*   _cur_thread;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
21183
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    41
public:
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
    42
  LoadedClassesClosure(Thread* thread, JvmtiEnv* env) : _cur_thread(thread), _env(env) {
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
    43
    assert(_cur_thread == Thread::current(), "must be current thread");
21183
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    44
  }
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    45
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    46
  void do_klass(Klass* k) {
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    47
    // Collect all jclasses
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
    48
    _classStack.push((jclass) _env->jni_reference(Handle(_cur_thread, k->java_mirror())));
21183
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    49
  }
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    50
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    51
  int extract(jclass* result_list) {
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    52
    // The size of the Stack will be 0 after extract, so get it here
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    53
    int count = (int)_classStack.size();
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    54
    int i = count;
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    55
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    56
    // Pop all jclasses, fill backwards
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    57
    while (!_classStack.is_empty()) {
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    58
      result_list[--i] = _classStack.pop();
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    59
    }
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    60
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    61
    // Return the number of elements written
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    62
    return count;
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    63
  }
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    64
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    65
  // Return current size of the Stack
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    66
  int get_count() {
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    67
    return (int)_classStack.size();
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    68
  }
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    69
};
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    70
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
    71
// The closure for GetClassLoaderClasses
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
class JvmtiGetLoadedClassesClosure : public StackObj {
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 46380
diff changeset
    73
  // Since the ClassLoaderDataGraph::dictionary_all_entries_do callback
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  // doesn't pass a closureData pointer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  // we use a thread-local slot to hold a pointer to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  // a stack allocated instance of this structure.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  jobject _initiatingLoader;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  int     _count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  Handle* _list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  int     _index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  // Getting and setting the thread local pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  static JvmtiGetLoadedClassesClosure* get_this() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    JvmtiGetLoadedClassesClosure* result = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    JavaThread* thread = JavaThread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    result = thread->get_jvmti_get_loaded_classes_closure();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  static void set_this(JvmtiGetLoadedClassesClosure* that) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    JavaThread* thread = JavaThread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    thread->set_jvmti_get_loaded_classes_closure(that);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  // Constructor/Destructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  JvmtiGetLoadedClassesClosure() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    JvmtiGetLoadedClassesClosure* that = get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    _initiatingLoader = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    _list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    _index = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    set_this(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  JvmtiGetLoadedClassesClosure(jobject initiatingLoader) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    JvmtiGetLoadedClassesClosure* that = get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    assert(that == NULL, "JvmtiGetLoadedClassesClosure in use");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    _initiatingLoader = initiatingLoader;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    _list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    _index = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    set_this(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  ~JvmtiGetLoadedClassesClosure() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    JvmtiGetLoadedClassesClosure* that = get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    assert(that != NULL, "JvmtiGetLoadedClassesClosure not found");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    set_this(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    _initiatingLoader = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    if (_list != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
      FreeHeap(_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
      _list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    _index = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
  // Accessors.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  jobject get_initiatingLoader() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    return _initiatingLoader;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  int get_count() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    return _count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  void set_count(int value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    _count = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  Handle* get_list() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    return _list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  void set_list(Handle* value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    _list = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  int get_index() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    return _index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  void set_index(int value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    _index = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  Handle get_element(int index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    if ((_list != NULL) && (index < _count)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      return _list[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
      assert(false, "empty get_element");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
      return Handle();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  void set_element(int index, Handle value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
    if ((_list != NULL) && (index < _count)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
      _list[index] = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
      assert(false, "bad set_element");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  // Other predicates
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  bool available() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    return (_list != NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  // For debugging.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  void check(int limit) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
    for (int i = 0; i < limit; i += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
      assert(Universe::heap()->is_in(get_element(i)()), "check fails");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  // Public methods that get called within the scope of the closure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
  void allocate() {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   193
    _list = NEW_C_HEAP_ARRAY(Handle, _count, mtInternal);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    assert(_list != NULL, "Out of memory");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
    if (_list == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
      _count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  void extract(JvmtiEnv *env, jclass* result) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    for (int index = 0; index < _count; index += 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
      result[index] = (jclass) env->jni_reference(get_element(index));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 46380
diff changeset
   206
  static void increment_with_loader(InstanceKlass* k, ClassLoaderData* loader_data) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   208
    oop class_loader = loader_data->class_loader();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   209
    if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
14488
ab48109f7d1b 8001471: Klass::cast() does nothing
hseigel
parents: 13728
diff changeset
   210
      for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
        that->set_count(that->get_count() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 46380
diff changeset
   216
  static void add_with_loader(InstanceKlass* k, ClassLoaderData* loader_data) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    if (that->available()) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   219
      oop class_loader = loader_data->class_loader();
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   220
      if (class_loader == JNIHandles::resolve(that->get_initiatingLoader())) {
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
   221
        Thread *thread = Thread::current();
14488
ab48109f7d1b 8001471: Klass::cast() does nothing
hseigel
parents: 13728
diff changeset
   222
        for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
   223
          Handle mirror(thread, l->java_mirror());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
          that->set_element(that->get_index(), mirror);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
          that->set_index(that->get_index() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  // increment the count for the given basic type array class (and any
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  // multi-dimensional arrays). For example, for [B we check for
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  // [[B, [[[B, .. and the count is incremented for each one that exists.
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   234
  static void increment_for_basic_type_arrays(Klass* k) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
14488
ab48109f7d1b 8001471: Klass::cast() does nothing
hseigel
parents: 13728
diff changeset
   237
    for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      that->set_count(that->get_count() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  // add the basic type array class and its multi-dimensional array classes to the list
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   243
  static void add_for_basic_type_arrays(Klass* k) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    JvmtiGetLoadedClassesClosure* that = JvmtiGetLoadedClassesClosure::get_this();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    assert(that != NULL, "no JvmtiGetLoadedClassesClosure");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    assert(that->available(), "no list");
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
   247
    Thread *thread = Thread::current();
14488
ab48109f7d1b 8001471: Klass::cast() does nothing
hseigel
parents: 13728
diff changeset
   248
    for (Klass* l = k; l != NULL; l = l->array_klass_or_null()) {
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
   249
      Handle mirror(thread, l->java_mirror());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      that->set_element(that->get_index(), mirror);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      that->set_index(that->get_index() + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
JvmtiGetLoadedClasses::getLoadedClasses(JvmtiEnv *env, jint* classCountPtr, jclass** classesPtr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 30764
diff changeset
   260
  LoadedClassesClosure closure(Thread::current(), env);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    // To get a consistent list of classes we need MultiArray_lock to ensure
21183
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   263
    // array classes aren't created.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    MutexLocker ma(MultiArray_lock);
21183
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   265
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   266
    // Iterate through all classes in ClassLoaderDataGraph
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   267
    // and collect them using the LoadedClassesClosure
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   268
    ClassLoaderDataGraph::loaded_classes_do(&closure);
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   269
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
21183
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   271
  // Return results by extracting the collected contents into a list
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   272
  // allocated via JvmtiEnv
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   273
  jclass* result_list;
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   274
  jvmtiError error = env->Allocate(closure.get_count() * sizeof(jclass),
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   275
                               (unsigned char**)&result_list);
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   276
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   277
  if (error == JVMTI_ERROR_NONE) {
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   278
    int count = closure.extract(result_list);
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   279
    *classCountPtr = count;
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   280
    *classesPtr = result_list;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  }
21183
e148e499e5cd 8024423: JVMTI: GetLoadedClasses doesn't enumerate anonymous classes
farvidsson
parents: 14488
diff changeset
   282
  return error;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
JvmtiGetLoadedClasses::getClassLoaderClasses(JvmtiEnv *env, jobject initiatingLoader,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
                                             jint* classCountPtr, jclass** classesPtr) {
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 46380
diff changeset
   288
  // Since ClassLoaderDataGraph::dictionary_all_entries_do only takes a function pointer
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  // and doesn't call back with a closure data pointer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  // we can only pass static methods.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  JvmtiGetLoadedClassesClosure closure(initiatingLoader);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
    // To get a consistent list of classes we need MultiArray_lock to ensure
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    // array classes aren't created, and SystemDictionary_lock to ensure that
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 46380
diff changeset
   295
    // classes aren't added to the class loader data dictionaries.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    MutexLocker ma(MultiArray_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
    MutexLocker sd(SystemDictionary_lock);
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 46380
diff changeset
   298
    // First, count the classes in the class loader data dictionaries which have this loader recorded
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    // as an initiating loader. For basic type arrays this information is not recorded
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    // so GetClassLoaderClasses will return all of the basic type arrays. This is okay
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    // because the defining loader for basic type arrays is always the boot class loader
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    // and these classes are "visible" to all loaders.
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 46380
diff changeset
   303
    ClassLoaderDataGraph::dictionary_all_entries_do(&JvmtiGetLoadedClassesClosure::increment_with_loader);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::increment_for_basic_type_arrays);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    // Next, fill in the classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    closure.allocate();
46729
c62d2e8b2728 7133093: Improve system dictionary performance
coleenp
parents: 46380
diff changeset
   307
    ClassLoaderDataGraph::dictionary_all_entries_do(&JvmtiGetLoadedClassesClosure::add_with_loader);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    Universe::basic_type_classes_do(&JvmtiGetLoadedClassesClosure::add_for_basic_type_arrays);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    // Drop the SystemDictionary_lock, so the results could be wrong from here,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
    // but we still have a snapshot.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
  // Post results
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  jclass* result_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  jvmtiError err = env->Allocate(closure.get_count() * sizeof(jclass),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
                                 (unsigned char**)&result_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  if (err != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
    return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  closure.extract(env, result_list);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  *classCountPtr = closure.get_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  *classesPtr = result_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
}