|
1 /* |
|
2 * Copyright (c) 2016, 2018, 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/jfr/Event"; |
|
137 unsigned int unused_hash = 0; |
|
138 Symbol* const event_klass_name = SymbolTable::lookup_only(jdk_jfr_event_name, sizeof jdk_jfr_event_name - 1, unused_hash); |
|
139 |
|
140 if (NULL == event_klass_name) { |
|
141 // not loaded yet |
|
142 return empty_java_util_arraylist; |
|
143 } |
|
144 |
|
145 const Klass* const klass = SystemDictionary::resolve_or_null(event_klass_name, THREAD); |
|
146 assert(klass != NULL, "invariant"); |
|
147 assert(JdkJfrEvent::is(klass), "invariant"); |
|
148 |
|
149 if (klass->subklass() == NULL) { |
|
150 return empty_java_util_arraylist; |
|
151 } |
|
152 |
|
153 ResourceMark rm(THREAD); |
|
154 GrowableArray<const void*> event_subklasses(THREAD, initial_size_growable_array); |
|
155 fill_klasses(event_subklasses, klass, THREAD); |
|
156 |
|
157 if (event_subklasses.is_empty()) { |
|
158 return empty_java_util_arraylist; |
|
159 } |
|
160 |
|
161 transform_klasses_to_local_jni_handles(event_subklasses, THREAD); |
|
162 |
|
163 Handle h_array_list(THREAD, new_java_util_arraylist(THREAD)); |
|
164 assert(h_array_list.not_null(), "invariant"); |
|
165 |
|
166 static const char add_method_name[] = "add"; |
|
167 static const char add_method_signature[] = "(Ljava/lang/Object;)Z"; |
|
168 const Klass* const array_list_klass = JfrJavaSupport::klass(empty_java_util_arraylist); |
|
169 assert(array_list_klass != NULL, "invariant"); |
|
170 |
|
171 const Symbol* const add_method_sym = SymbolTable::lookup(add_method_name, sizeof add_method_name - 1, THREAD); |
|
172 assert(add_method_sym != NULL, "invariant"); |
|
173 |
|
174 const Symbol* const add_method_sig_sym = SymbolTable::lookup(add_method_signature, sizeof add_method_signature - 1, THREAD); |
|
175 assert(add_method_signature != NULL, "invariant"); |
|
176 |
|
177 JavaValue result(T_BOOLEAN); |
|
178 for (int i = 0; i < event_subklasses.length(); ++i) { |
|
179 const jclass clazz = (const jclass)event_subklasses.at(i); |
|
180 assert(JdkJfrEvent::is_subklass(clazz), "invariant"); |
|
181 JfrJavaArguments args(&result, array_list_klass, add_method_sym, add_method_sig_sym); |
|
182 args.set_receiver(h_array_list()); |
|
183 args.push_jobject(clazz); |
|
184 JfrJavaSupport::call_virtual(&args, THREAD); |
|
185 if (HAS_PENDING_EXCEPTION || JNI_FALSE == result.get_jboolean()) { |
|
186 return empty_java_util_arraylist; |
|
187 } |
|
188 } |
|
189 return JfrJavaSupport::local_jni_handle(h_array_list(), THREAD); |
|
190 } |