1 /*
2  * Copyright (c) 2003, 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 "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
IsClassUnloadingEnabled(const jvmtiEnv * env,...)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 //
register_extensions()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 
get_functions(JvmtiEnv * env,jint * extension_count_ptr,jvmtiExtensionFunctionInfo ** extensions)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   }
111 
112   for (int i=0; i<_ext_functions->length(); i++ ) {
113     ext_funcs[i].func = _ext_functions->at(i)->func;
114 
115     char *id = _ext_functions->at(i)->id;
116     err = rt.allocate(strlen(id)+1, (unsigned char**)&(ext_funcs[i].id));
117     if (err != JVMTI_ERROR_NONE) {
118       return err;
119     }
120     strcpy(ext_funcs[i].id, id);
121 
122     char *desc = _ext_functions->at(i)->short_description;
123     err = rt.allocate(strlen(desc)+1,
124                       (unsigned char**)&(ext_funcs[i].short_description));
125     if (err != JVMTI_ERROR_NONE) {
126       return err;
127     }
128     strcpy(ext_funcs[i].short_description, desc);
129 
130     // params
131 
132     jint param_count = _ext_functions->at(i)->param_count;
133 
134     ext_funcs[i].param_count = param_count;
135     if (param_count == 0) {
136       ext_funcs[i].params = NULL;
137     } else {
138       err = rt.allocate(param_count*sizeof(jvmtiParamInfo),
139                         (unsigned char**)&(ext_funcs[i].params));
140       if (err != JVMTI_ERROR_NONE) {
141         return err;
142       }
143       jvmtiParamInfo* src_params = _ext_functions->at(i)->params;
144       jvmtiParamInfo* dst_params = ext_funcs[i].params;
145 
146       for (int j=0; j<param_count; j++) {
147         err = rt.allocate(strlen(src_params[j].name)+1,
148                           (unsigned char**)&(dst_params[j].name));
149         if (err != JVMTI_ERROR_NONE) {
150           return err;
151         }
152         strcpy(dst_params[j].name, src_params[j].name);
153 
154         dst_params[j].kind = src_params[j].kind;
155         dst_params[j].base_type = src_params[j].base_type;
156         dst_params[j].null_ok = src_params[j].null_ok;
157       }
158     }
159 
160     // errors
161 
162     jint error_count = _ext_functions->at(i)->error_count;
163     ext_funcs[i].error_count = error_count;
164     if (error_count == 0) {
165       ext_funcs[i].errors = NULL;
166     } else {
167       err = rt.allocate(error_count*sizeof(jvmtiError),
168                         (unsigned char**)&(ext_funcs[i].errors));
169       if (err != JVMTI_ERROR_NONE) {
170         return err;
171       }
172       memcpy(ext_funcs[i].errors, _ext_functions->at(i)->errors,
173              error_count*sizeof(jvmtiError));
174     }
175   }
176 
177   *extension_count_ptr = _ext_functions->length();
178   *extensions = ext_funcs;
179   return JVMTI_ERROR_NONE;
180 }
181 
182 
183 // return the list of extension events
184 
get_events(JvmtiEnv * env,jint * extension_count_ptr,jvmtiExtensionEventInfo ** extensions)185 jvmtiError JvmtiExtensions::get_events(JvmtiEnv* env,
186                                        jint* extension_count_ptr,
187                                        jvmtiExtensionEventInfo** extensions)
188 {
189   guarantee(_ext_events != NULL, "registration not done");
190 
191   ResourceTracker rt(env);
192 
193   jvmtiExtensionEventInfo* ext_events;
194   jvmtiError err = rt.allocate(_ext_events->length() * sizeof(jvmtiExtensionEventInfo),
195                                (unsigned char**)&ext_events);
196   if (err != JVMTI_ERROR_NONE) {
197     return err;
198   }
199 
200   for (int i=0; i<_ext_events->length(); i++ ) {
201     ext_events[i].extension_event_index = _ext_events->at(i)->extension_event_index;
202 
203     char *id = _ext_events->at(i)->id;
204     err = rt.allocate(strlen(id)+1, (unsigned char**)&(ext_events[i].id));
205     if (err != JVMTI_ERROR_NONE) {
206       return err;
207     }
208     strcpy(ext_events[i].id, id);
209 
210     char *desc = _ext_events->at(i)->short_description;
211     err = rt.allocate(strlen(desc)+1,
212                       (unsigned char**)&(ext_events[i].short_description));
213     if (err != JVMTI_ERROR_NONE) {
214       return err;
215     }
216     strcpy(ext_events[i].short_description, desc);
217 
218     // params
219 
220     jint param_count = _ext_events->at(i)->param_count;
221 
222     ext_events[i].param_count = param_count;
223     if (param_count == 0) {
224       ext_events[i].params = NULL;
225     } else {
226       err = rt.allocate(param_count*sizeof(jvmtiParamInfo),
227                         (unsigned char**)&(ext_events[i].params));
228       if (err != JVMTI_ERROR_NONE) {
229         return err;
230       }
231       jvmtiParamInfo* src_params = _ext_events->at(i)->params;
232       jvmtiParamInfo* dst_params = ext_events[i].params;
233 
234       for (int j=0; j<param_count; j++) {
235         err = rt.allocate(strlen(src_params[j].name)+1,
236                           (unsigned char**)&(dst_params[j].name));
237         if (err != JVMTI_ERROR_NONE) {
238           return err;
239         }
240         strcpy(dst_params[j].name, src_params[j].name);
241 
242         dst_params[j].kind = src_params[j].kind;
243         dst_params[j].base_type = src_params[j].base_type;
244         dst_params[j].null_ok = src_params[j].null_ok;
245       }
246     }
247   }
248 
249   *extension_count_ptr = _ext_events->length();
250   *extensions = ext_events;
251   return JVMTI_ERROR_NONE;
252 }
253 
254 // set callback for an extension event and enable/disable it.
255 
set_event_callback(JvmtiEnv * env,jint extension_event_index,jvmtiExtensionEvent callback)256 jvmtiError JvmtiExtensions::set_event_callback(JvmtiEnv* env,
257                                                jint extension_event_index,
258                                                jvmtiExtensionEvent callback)
259 {
260   guarantee(_ext_events != NULL, "registration not done");
261 
262   jvmtiExtensionEventInfo* event = NULL;
263 
264   // if there are extension events registered then validate that the
265   // extension_event_index matches one of the registered events.
266   if (_ext_events != NULL) {
267     for (int i=0; i<_ext_events->length(); i++ ) {
268       if (_ext_events->at(i)->extension_event_index == extension_event_index) {
269          event = _ext_events->at(i);
270          break;
271       }
272     }
273   }
274 
275   // invalid event index
276   if (event == NULL) {
277     return JVMTI_ERROR_ILLEGAL_ARGUMENT;
278   }
279 
280   JvmtiEventController::set_extension_event_callback(env, extension_event_index,
281                                                      callback);
282 
283   return JVMTI_ERROR_NONE;
284 }
285