< prev index next >

src/hotspot/share/prims/jvmtiExtensions.cpp

Print this page

  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 "prims/jvmtiExport.hpp"
 27 #include "prims/jvmtiExtensions.hpp"




 28 
 29 // the list of extension functions
 30 GrowableArray<jvmtiExtensionFunctionInfo*>* JvmtiExtensions::_ext_functions;
 31 
 32 // the list of extension events
 33 GrowableArray<jvmtiExtensionEventInfo*>* JvmtiExtensions::_ext_events;
 34 
 35 
 36 // extension function


 37 static jvmtiError JNICALL IsClassUnloadingEnabled(const jvmtiEnv* env, ...) {
 38   jboolean* enabled = NULL;
 39   va_list ap;
 40 
 41   va_start(ap, env);
 42   enabled = va_arg(ap, jboolean *);
 43   va_end(ap);
 44 
 45   if (enabled == NULL) {
 46     return JVMTI_ERROR_NULL_POINTER;
 47   }
 48   *enabled = (jboolean)ClassUnloading;
 49   return JVMTI_ERROR_NONE;
 50 }
 51 





















































































 52 // register extension functions and events. In this implementation we
 53 // have a single extension function (to prove the API) that tests if class
 54 // unloading is enabled or disabled. We also have a single extension event
 55 // EXT_EVENT_CLASS_UNLOAD which is used to provide the JVMDI_EVENT_CLASS_UNLOAD
 56 // event. The function and the event are registered here.
 57 //
 58 void JvmtiExtensions::register_extensions() {
 59   _ext_functions = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiExtensionFunctionInfo*>(1, mtServiceability);
 60   _ext_events = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiExtensionEventInfo*>(1, mtServiceability);
 61 
 62   // register our extension function
 63   static jvmtiParamInfo func_params[] = {
 64     { (char*)"IsClassUnloadingEnabled", JVMTI_KIND_OUT,  JVMTI_TYPE_JBOOLEAN, JNI_FALSE }













 65   };
 66   static jvmtiExtensionFunctionInfo ext_func = {

 67     (jvmtiExtensionFunction)IsClassUnloadingEnabled,
 68     (char*)"com.sun.hotspot.functions.IsClassUnloadingEnabled",
 69     (char*)"Tell if class unloading is enabled (-noclassgc)",
 70     sizeof(func_params)/sizeof(func_params[0]),
 71     func_params,
 72     0,              // no non-universal errors
 73     NULL
 74   };
 75   _ext_functions->append(&ext_func);























 76 
 77   // register our extension event
 78 
 79   static jvmtiParamInfo event_params[] = {
 80     { (char*)"JNI Environment", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JNIENV, JNI_FALSE },
 81     { (char*)"Class", JVMTI_KIND_IN_PTR, JVMTI_TYPE_CCHAR, JNI_FALSE }
 82   };
 83   static jvmtiExtensionEventInfo ext_event = {





 84     EXT_EVENT_CLASS_UNLOAD,
 85     (char*)"com.sun.hotspot.events.ClassUnload",
 86     (char*)"CLASS_UNLOAD event",
 87     sizeof(event_params)/sizeof(event_params[0]),
 88     event_params
 89   };
 90   _ext_events->append(&ext_event);

















 91 }
 92 
 93 
 94 // return the list of extension functions
 95 
 96 jvmtiError JvmtiExtensions::get_functions(JvmtiEnv* env,
 97                                           jint* extension_count_ptr,
 98                                           jvmtiExtensionFunctionInfo** extensions)
 99 {
100   guarantee(_ext_functions != NULL, "registration not done");
101 
102   ResourceTracker rt(env);
103 
104   jvmtiExtensionFunctionInfo* ext_funcs;
105   jvmtiError err = rt.allocate(_ext_functions->length() *
106                                sizeof(jvmtiExtensionFunctionInfo),
107                                (unsigned char**)&ext_funcs);
108   if (err != JVMTI_ERROR_NONE) {
109     return err;
110   }

  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 "prims/jvmtiExport.hpp"
 28 #include "prims/jvmtiExtensions.hpp"
 29 #include "prims/jvmtiThreadState.inline.hpp"
 30 #include "runtime/interfaceSupport.inline.hpp"
 31 #include "runtime/handles.inline.hpp"
 32 #include "runtime/jniHandles.inline.hpp"
 33 
 34 // the list of extension functions
 35 GrowableArray<jvmtiExtensionFunctionInfo*>* JvmtiExtensions::_ext_functions;
 36 
 37 // the list of extension events
 38 GrowableArray<jvmtiExtensionEventInfo*>* JvmtiExtensions::_ext_events;
 39 
 40 
 41 /*
 42  * Extension Functions
 43  */
 44 static jvmtiError JNICALL IsClassUnloadingEnabled(const jvmtiEnv* env, ...) {
 45   jboolean* enabled = NULL;
 46   va_list ap;
 47 
 48   va_start(ap, env);
 49   enabled = va_arg(ap, jboolean *);
 50   va_end(ap);
 51 
 52   if (enabled == NULL) {
 53     return JVMTI_ERROR_NULL_POINTER;
 54   }
 55   *enabled = (jboolean)ClassUnloading;
 56   return JVMTI_ERROR_NONE;
 57 }
 58 
 59 // Parameters: (jthread thread, jthread* vthread_ptr)
 60 static jvmtiError JNICALL GetVirtualThread(const jvmtiEnv* env, ...) {
 61   JvmtiEnv* jvmti_env = JvmtiEnv::JvmtiEnv_from_jvmti_env((jvmtiEnv*)env);
 62   if (jvmti_env->get_capabilities()->can_support_virtual_threads == 0) {
 63     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
 64   }
 65 
 66   JavaThread* current_thread = JavaThread::current();
 67   ResourceMark rm(current_thread);
 68   jthread thread = NULL;
 69   jthread* vthread_ptr = NULL;
 70   JavaThread* java_thread = NULL;
 71   oop thread_oop = NULL;
 72   va_list ap;
 73 
 74   va_start(ap, env);
 75   thread = va_arg(ap, jthread);
 76   vthread_ptr = va_arg(ap, jthread*);
 77   va_end(ap);
 78 
 79   ThreadInVMfromNative tiv(current_thread);
 80   JvmtiVTMTDisabler vtmt_disabler;
 81   ThreadsListHandle tlh(current_thread);
 82 
 83   jvmtiError err;
 84 
 85   *vthread_ptr = NULL;
 86   if (thread == NULL) {
 87     java_thread = current_thread;
 88   } else {
 89     err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), thread, &java_thread, NULL);
 90     if (err != JVMTI_ERROR_NONE) {
 91       return err;
 92     }
 93   }
 94   if (vthread_ptr == NULL) {
 95     return JVMTI_ERROR_NULL_POINTER;
 96   }
 97 
 98   JvmtiThreadState *state = JvmtiThreadState::state_for(java_thread);
 99   if (state == NULL) {
100     return JVMTI_ERROR_THREAD_NOT_ALIVE;
101   }
102   oop vthread_oop = java_thread->vthread();
103   if (!java_lang_VirtualThread::is_instance(vthread_oop)) { // not a virtual thread
104     vthread_oop = NULL;
105   }
106   *vthread_ptr = (jthread)JNIHandles::make_local(current_thread, vthread_oop);
107   return JVMTI_ERROR_NONE;
108 }
109 
110 // Parameters: (jthread vthread, jthread* thread_ptr)
111 static jvmtiError JNICALL GetCarrierThread(const jvmtiEnv* env, ...) {
112   JvmtiEnv* jvmti_env = JvmtiEnv::JvmtiEnv_from_jvmti_env((jvmtiEnv*)env);
113   if (jvmti_env->get_capabilities()->can_support_virtual_threads == 0) {
114     return JVMTI_ERROR_MUST_POSSESS_CAPABILITY;
115   }
116 
117   JavaThread* current_thread = JavaThread::current();
118   HandleMark hm(current_thread);
119   jthread vthread = NULL;
120   jthread* thread_ptr = NULL;
121   va_list ap;
122 
123   va_start(ap, env);
124   vthread = va_arg(ap, jthread);
125   thread_ptr = va_arg(ap, jthread*);
126   va_end(ap);
127 
128   ThreadInVMfromNative tiv(current_thread);
129   JvmtiVTMTDisabler vtmt_disabler;
130 
131   oop vthread_obj = JNIHandles::resolve_external_guard(vthread);
132 
133   if (!java_lang_VirtualThread::is_instance(vthread_obj)) {
134     return JVMTI_ERROR_INVALID_THREAD;
135   }
136   if (thread_ptr == NULL) {
137       return JVMTI_ERROR_NULL_POINTER;
138   }
139   VThreadGetThreadClosure op(Handle(current_thread, vthread_obj), thread_ptr);
140   Handshake::execute(&op, current_thread);
141   return op.result();
142 }
143 
144 // register extension functions and events. In this implementation we
145 // have a single extension function (to prove the API) that tests if class
146 // unloading is enabled or disabled. We also have a single extension event
147 // EXT_EVENT_CLASS_UNLOAD which is used to provide the JVMDI_EVENT_CLASS_UNLOAD
148 // event. The function and the event are registered here.
149 //
150 void JvmtiExtensions::register_extensions() {
151   _ext_functions = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiExtensionFunctionInfo*>(1, mtServiceability);
152   _ext_events = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<jvmtiExtensionEventInfo*>(1, mtServiceability);
153 
154   // register our extension functions
155   static jvmtiParamInfo func_params0[] = {
156     { (char*)"IsClassUnloadingEnabled", JVMTI_KIND_OUT, JVMTI_TYPE_JBOOLEAN, JNI_FALSE }
157   };
158   static jvmtiParamInfo func_params1[] = {
159     { (char*)"GetVirtualThread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE },
160     { (char*)"GetVirtualThread", JVMTI_KIND_OUT, JVMTI_TYPE_JTHREAD, JNI_FALSE }
161   };
162   static jvmtiParamInfo func_params2[] = {
163     { (char*)"GetVirtualThread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE },
164     { (char*)"GetCarrierThread", JVMTI_KIND_OUT, JVMTI_TYPE_JTHREAD, JNI_FALSE }
165   };
166 
167   static jvmtiError errors[] = {
168     JVMTI_ERROR_MUST_POSSESS_CAPABILITY,
169     JVMTI_ERROR_INVALID_THREAD
170   };
171 
172   static jvmtiExtensionFunctionInfo ext_func0 = {
173     (jvmtiExtensionFunction)IsClassUnloadingEnabled,
174     (char*)"com.sun.hotspot.functions.IsClassUnloadingEnabled",
175     (char*)"Tell if class unloading is enabled (-noclassgc)",
176     sizeof(func_params0)/sizeof(func_params0[0]),
177     func_params0,
178     0,              // no non-universal errors
179     NULL
180   };
181 
182   static jvmtiExtensionFunctionInfo ext_func1 = {
183     (jvmtiExtensionFunction)GetVirtualThread,
184     (char*)"com.sun.hotspot.functions.GetVirtualThread",
185     (char*)"Get virtual thread executed on carrier thread",
186     sizeof(func_params1)/sizeof(func_params1[0]),
187     func_params1,
188     sizeof(errors)/sizeof(jvmtiError),   // non-universal errors
189     errors
190   };
191 
192   static jvmtiExtensionFunctionInfo ext_func2 = {
193     (jvmtiExtensionFunction)GetCarrierThread,
194     (char*)"com.sun.hotspot.functions.GetCarrierThread",
195     (char*)"Get carrier thread executing virtual thread",
196     sizeof(func_params2)/sizeof(func_params2[0]),
197     func_params2,
198     sizeof(errors)/sizeof(jvmtiError),   // non-universal errors
199     errors
200   };
201 
202   _ext_functions->append(&ext_func0);
203   _ext_functions->append(&ext_func1);
204   _ext_functions->append(&ext_func2);
205 
206   // register our extension event
207 
208   static jvmtiParamInfo class_unload_event_params[] = {
209     { (char*)"JNI Environment", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JNIENV, JNI_FALSE },
210     { (char*)"Class", JVMTI_KIND_IN_PTR, JVMTI_TYPE_CCHAR, JNI_FALSE }
211   };
212   static jvmtiParamInfo virtual_thread_event_params[] = {
213     { (char*)"JNI Environment", JVMTI_KIND_IN_PTR, JVMTI_TYPE_JNIENV, JNI_FALSE },
214     { (char*)"Virtual Thread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE }
215   };
216 
217   static jvmtiExtensionEventInfo class_unload_ext_event = {
218     EXT_EVENT_CLASS_UNLOAD,
219     (char*)"com.sun.hotspot.events.ClassUnload",
220     (char*)"CLASS_UNLOAD event",
221     sizeof(class_unload_event_params)/sizeof(class_unload_event_params[0]),
222     class_unload_event_params
223   };
224   static jvmtiExtensionEventInfo virtual_thread_mount_ext_event = {
225     EXT_EVENT_VIRTUAL_THREAD_MOUNT,
226     (char*)"com.sun.hotspot.events.VirtualThreadMount",
227     (char*)"VIRTUAL_THREAD_MOUNT event",
228     sizeof(virtual_thread_event_params)/sizeof(virtual_thread_event_params[0]),
229     virtual_thread_event_params
230   };
231   static jvmtiExtensionEventInfo virtual_thread_unmount_ext_event = {
232     EXT_EVENT_VIRTUAL_THREAD_UNMOUNT,
233     (char*)"com.sun.hotspot.events.VirtualThreadUnmount",
234     (char*)"VIRTUAL_THREAD_UNMOUNT event",
235     sizeof(virtual_thread_event_params)/sizeof(virtual_thread_event_params[0]),
236     virtual_thread_event_params
237   };
238 
239   _ext_events->append(&class_unload_ext_event);
240   _ext_events->append(&virtual_thread_mount_ext_event);
241   _ext_events->append(&virtual_thread_unmount_ext_event);
242 }
243 
244 
245 // return the list of extension functions
246 
247 jvmtiError JvmtiExtensions::get_functions(JvmtiEnv* env,
248                                           jint* extension_count_ptr,
249                                           jvmtiExtensionFunctionInfo** extensions)
250 {
251   guarantee(_ext_functions != NULL, "registration not done");
252 
253   ResourceTracker rt(env);
254 
255   jvmtiExtensionFunctionInfo* ext_funcs;
256   jvmtiError err = rt.allocate(_ext_functions->length() *
257                                sizeof(jvmtiExtensionFunctionInfo),
258                                (unsigned char**)&ext_funcs);
259   if (err != JVMTI_ERROR_NONE) {
260     return err;
261   }
< prev index next >