| 1 | /* |
| 2 | * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "precompiled.hpp" |
| 26 | #include "classfile/javaClasses.inline.hpp" |
| 27 | #include "classfile/symbolTable.hpp" |
| 28 | #include "jfr/jni/jfrGetAllEventClasses.hpp" |
| 29 | #include "jfr/jni/jfrJavaSupport.hpp" |
| 30 | #include "jfr/support/jfrEventClass.hpp" |
| 31 | #include "oops/instanceKlass.hpp" |
| 32 | #include "memory/allocation.inline.hpp" |
| 33 | #include "memory/resourceArea.hpp" |
| 34 | #include "runtime/handles.inline.hpp" |
| 35 | #include "runtime/mutexLocker.hpp" |
| 36 | #include "runtime/safepoint.hpp" |
| 37 | #include "runtime/thread.inline.hpp" |
| 38 | #include "utilities/growableArray.hpp" |
| 39 | #include "utilities/stack.inline.hpp" |
| 40 | |
| 41 | // incremented during class unloading (safepoint) for each unloaded event class |
| 42 | static jlong unloaded_event_classes = 0; |
| 43 | |
| 44 | jlong JfrEventClasses::unloaded_event_classes_count() { |
| 45 | return unloaded_event_classes; |
| 46 | } |
| 47 | |
| 48 | void JfrEventClasses::increment_unloaded_event_class() { |
| 49 | // incremented during class unloading (safepoint) for each unloaded event class |
| 50 | assert(SafepointSynchronize::is_at_safepoint(), "invariant" ); |
| 51 | ++unloaded_event_classes; |
| 52 | } |
| 53 | |
| 54 | static jobject empty_java_util_arraylist = NULL; |
| 55 | |
| 56 | static oop new_java_util_arraylist(TRAPS) { |
| 57 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
| 58 | JavaValue result(T_OBJECT); |
| 59 | JfrJavaArguments args(&result, "java/util/ArrayList" , "<init>" , "()V" , CHECK_NULL); |
| 60 | JfrJavaSupport::new_object(&args, CHECK_NULL); |
| 61 | return (oop)result.get_jobject(); |
| 62 | } |
| 63 | |
| 64 | static bool initialize(TRAPS) { |
| 65 | static bool initialized = false; |
| 66 | if (!initialized) { |
| 67 | unloaded_event_classes = 0; |
| 68 | assert(NULL == empty_java_util_arraylist, "invariant" ); |
| 69 | const oop array_list = new_java_util_arraylist(CHECK_false); |
| 70 | empty_java_util_arraylist = JfrJavaSupport::global_jni_handle(array_list, THREAD); |
| 71 | initialized = empty_java_util_arraylist != NULL; |
| 72 | } |
| 73 | return initialized; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Abstract klasses are filtered out unconditionally. |
| 78 | * If a klass is not yet initialized, i.e yet to run its <clinit> |
| 79 | * it is also filtered out so we don't accidentally |
| 80 | * trigger initialization. |
| 81 | */ |
| 82 | static bool is_whitelisted(const Klass* k) { |
| 83 | assert(k != NULL, "invariant" ); |
| 84 | return !(k->is_abstract() || k->should_be_initialized()); |
| 85 | } |
| 86 | |
| 87 | static void fill_klasses(GrowableArray<const void*>& event_subklasses, const Klass* event_klass, Thread* thread) { |
| 88 | assert(event_subklasses.length() == 0, "invariant" ); |
| 89 | assert(event_klass != NULL, "invariant" ); |
| 90 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(thread)); |
| 91 | |
| 92 | Stack<const Klass*, mtTracing> mark_stack; |
| 93 | MutexLocker ml(Compile_lock, thread); |
| 94 | mark_stack.push(event_klass->subklass()); |
| 95 | |
| 96 | while (!mark_stack.is_empty()) { |
| 97 | const Klass* const current = mark_stack.pop(); |
| 98 | assert(current != NULL, "null element in stack!" ); |
| 99 | |
| 100 | if (is_whitelisted(current)) { |
| 101 | event_subklasses.append(current); |
| 102 | } |
| 103 | |
| 104 | // subclass (depth) |
| 105 | const Klass* next_klass = current->subklass(); |
| 106 | if (next_klass != NULL) { |
| 107 | mark_stack.push(next_klass); |
| 108 | } |
| 109 | |
| 110 | // siblings (breadth) |
| 111 | next_klass = current->next_sibling(); |
| 112 | if (next_klass != NULL) { |
| 113 | mark_stack.push(next_klass); |
| 114 | } |
| 115 | } |
| 116 | assert(mark_stack.is_empty(), "invariant" ); |
| 117 | } |
| 118 | |
| 119 | static void transform_klasses_to_local_jni_handles(GrowableArray<const void*>& event_subklasses, Thread* thread) { |
| 120 | assert(event_subklasses.is_nonempty(), "invariant" ); |
| 121 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(thread)); |
| 122 | |
| 123 | for (int i = 0; i < event_subklasses.length(); ++i) { |
| 124 | const InstanceKlass* k = static_cast<const InstanceKlass*>(event_subklasses.at(i)); |
| 125 | assert(is_whitelisted(k), "invariant" ); |
| 126 | event_subklasses.at_put(i, JfrJavaSupport::local_jni_handle(k->java_mirror(), thread)); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static const int initial_size_growable_array = 64; |
| 131 | |
| 132 | jobject JfrEventClasses::get_all_event_classes(TRAPS) { |
| 133 | DEBUG_ONLY(JfrJavaSupport::check_java_thread_in_vm(THREAD)); |
| 134 | initialize(THREAD); |
| 135 | assert(empty_java_util_arraylist != NULL, "should have been setup already!" ); |
| 136 | static const char jdk_jfr_event_name[] = "jdk/internal/event/Event" ; |
| 137 | Symbol* const event_klass_name = SymbolTable::probe(jdk_jfr_event_name, sizeof jdk_jfr_event_name - 1); |
| 138 | |
| 139 | if (NULL == event_klass_name) { |
| 140 | // not loaded yet |
| 141 | return empty_java_util_arraylist; |
| 142 | } |
| 143 | |
| 144 | const Klass* const klass = SystemDictionary::resolve_or_null(event_klass_name, THREAD); |
| 145 | assert(klass != NULL, "invariant" ); |
| 146 | assert(JdkJfrEvent::is(klass), "invariant" ); |
| 147 | |
| 148 | if (klass->subklass() == NULL) { |
| 149 | return empty_java_util_arraylist; |
| 150 | } |
| 151 | |
| 152 | ResourceMark rm(THREAD); |
| 153 | GrowableArray<const void*> event_subklasses(THREAD, initial_size_growable_array); |
| 154 | fill_klasses(event_subklasses, klass, THREAD); |
| 155 | |
| 156 | if (event_subklasses.is_empty()) { |
| 157 | return empty_java_util_arraylist; |
| 158 | } |
| 159 | |
| 160 | transform_klasses_to_local_jni_handles(event_subklasses, THREAD); |
| 161 | |
| 162 | Handle h_array_list(THREAD, new_java_util_arraylist(THREAD)); |
| 163 | assert(h_array_list.not_null(), "invariant" ); |
| 164 | |
| 165 | static const char add_method_name[] = "add" ; |
| 166 | static const char add_method_signature[] = "(Ljava/lang/Object;)Z" ; |
| 167 | const Klass* const array_list_klass = JfrJavaSupport::klass(empty_java_util_arraylist); |
| 168 | assert(array_list_klass != NULL, "invariant" ); |
| 169 | |
| 170 | const Symbol* const add_method_sym = SymbolTable::new_symbol(add_method_name); |
| 171 | assert(add_method_sym != NULL, "invariant" ); |
| 172 | |
| 173 | const Symbol* const add_method_sig_sym = SymbolTable::new_symbol(add_method_signature); |
| 174 | assert(add_method_signature != NULL, "invariant" ); |
| 175 | |
| 176 | JavaValue result(T_BOOLEAN); |
| 177 | for (int i = 0; i < event_subklasses.length(); ++i) { |
| 178 | const jclass clazz = (const jclass)event_subklasses.at(i); |
| 179 | assert(JdkJfrEvent::is_subklass(clazz), "invariant" ); |
| 180 | JfrJavaArguments args(&result, array_list_klass, add_method_sym, add_method_sig_sym); |
| 181 | args.set_receiver(h_array_list()); |
| 182 | args.push_jobject(clazz); |
| 183 | JfrJavaSupport::call_virtual(&args, THREAD); |
| 184 | if (HAS_PENDING_EXCEPTION || JNI_FALSE == result.get_jboolean()) { |
| 185 | return empty_java_util_arraylist; |
| 186 | } |
| 187 | } |
| 188 | return JfrJavaSupport::local_jni_handle(h_array_list(), THREAD); |
| 189 | } |
| 190 | |