src/share/vm/prims/jvmtiExtensions.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 4153
b9a9ed0f8eeb
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "prims/jvmtiExport.hpp"
aoqi@0 27 #include "prims/jvmtiExtensions.hpp"
aoqi@0 28
aoqi@0 29 // the list of extension functions
aoqi@0 30 GrowableArray<jvmtiExtensionFunctionInfo*>* JvmtiExtensions::_ext_functions;
aoqi@0 31
aoqi@0 32 // the list of extension events
aoqi@0 33 GrowableArray<jvmtiExtensionEventInfo*>* JvmtiExtensions::_ext_events;
aoqi@0 34
aoqi@0 35
aoqi@0 36 // extension function
aoqi@0 37 static jvmtiError JNICALL IsClassUnloadingEnabled(const jvmtiEnv* env, jboolean* enabled, ...) {
aoqi@0 38 if (enabled == NULL) {
aoqi@0 39 return JVMTI_ERROR_NULL_POINTER;
aoqi@0 40 }
aoqi@0 41 *enabled = (jboolean)ClassUnloading;
aoqi@0 42 return JVMTI_ERROR_NONE;
aoqi@0 43 }
aoqi@0 44
aoqi@0 45 // register extension functions and events. In this implementation we
aoqi@0 46 // have a single extension function (to prove the API) that tests if class
aoqi@0 47 // unloading is enabled or disabled. We also have a single extension event
aoqi@0 48 // EXT_EVENT_CLASS_UNLOAD which is used to provide the JVMDI_EVENT_CLASS_UNLOAD
aoqi@0 49 // event. The function and the event are registered here.
aoqi@0 50 //
aoqi@0 51 void JvmtiExtensions::register_extensions() {
aoqi@0 52 _ext_functions = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiExtensionFunctionInfo*>(1,true);
aoqi@0 53 _ext_events = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<jvmtiExtensionEventInfo*>(1,true);
aoqi@0 54
aoqi@0 55 // register our extension function
aoqi@0 56 static jvmtiParamInfo func_params[] = {
aoqi@0 57 { (char*)"IsClassUnloadingEnabled", JVMTI_KIND_OUT, JVMTI_TYPE_JBOOLEAN, JNI_FALSE }
aoqi@0 58 };
aoqi@0 59 static jvmtiExtensionFunctionInfo ext_func = {
aoqi@0 60 (jvmtiExtensionFunction)IsClassUnloadingEnabled,
aoqi@0 61 (char*)"com.sun.hotspot.functions.IsClassUnloadingEnabled",
aoqi@0 62 (char*)"Tell if class unloading is enabled (-noclassgc)",
aoqi@0 63 sizeof(func_params)/sizeof(func_params[0]),
aoqi@0 64 func_params,
aoqi@0 65 0, // no non-universal errors
aoqi@0 66 NULL
aoqi@0 67 };
aoqi@0 68 _ext_functions->append(&ext_func);
aoqi@0 69
aoqi@0 70 // register our extension event
aoqi@0 71
aoqi@0 72 static jvmtiParamInfo event_params[] = {
aoqi@0 73 { (char*)"JNI Environment", JVMTI_KIND_IN, JVMTI_TYPE_JNIENV, JNI_FALSE },
aoqi@0 74 { (char*)"Thread", JVMTI_KIND_IN, JVMTI_TYPE_JTHREAD, JNI_FALSE },
aoqi@0 75 { (char*)"Class", JVMTI_KIND_IN, JVMTI_TYPE_JCLASS, JNI_FALSE }
aoqi@0 76 };
aoqi@0 77 static jvmtiExtensionEventInfo ext_event = {
aoqi@0 78 EXT_EVENT_CLASS_UNLOAD,
aoqi@0 79 (char*)"com.sun.hotspot.events.ClassUnload",
aoqi@0 80 (char*)"CLASS_UNLOAD event",
aoqi@0 81 sizeof(event_params)/sizeof(event_params[0]),
aoqi@0 82 event_params
aoqi@0 83 };
aoqi@0 84 _ext_events->append(&ext_event);
aoqi@0 85 }
aoqi@0 86
aoqi@0 87
aoqi@0 88 // return the list of extension functions
aoqi@0 89
aoqi@0 90 jvmtiError JvmtiExtensions::get_functions(JvmtiEnv* env,
aoqi@0 91 jint* extension_count_ptr,
aoqi@0 92 jvmtiExtensionFunctionInfo** extensions)
aoqi@0 93 {
aoqi@0 94 guarantee(_ext_functions != NULL, "registration not done");
aoqi@0 95
aoqi@0 96 ResourceTracker rt(env);
aoqi@0 97
aoqi@0 98 jvmtiExtensionFunctionInfo* ext_funcs;
aoqi@0 99 jvmtiError err = rt.allocate(_ext_functions->length() *
aoqi@0 100 sizeof(jvmtiExtensionFunctionInfo),
aoqi@0 101 (unsigned char**)&ext_funcs);
aoqi@0 102 if (err != JVMTI_ERROR_NONE) {
aoqi@0 103 return err;
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 for (int i=0; i<_ext_functions->length(); i++ ) {
aoqi@0 107 ext_funcs[i].func = _ext_functions->at(i)->func;
aoqi@0 108
aoqi@0 109 char *id = _ext_functions->at(i)->id;
aoqi@0 110 err = rt.allocate(strlen(id)+1, (unsigned char**)&(ext_funcs[i].id));
aoqi@0 111 if (err != JVMTI_ERROR_NONE) {
aoqi@0 112 return err;
aoqi@0 113 }
aoqi@0 114 strcpy(ext_funcs[i].id, id);
aoqi@0 115
aoqi@0 116 char *desc = _ext_functions->at(i)->short_description;
aoqi@0 117 err = rt.allocate(strlen(desc)+1,
aoqi@0 118 (unsigned char**)&(ext_funcs[i].short_description));
aoqi@0 119 if (err != JVMTI_ERROR_NONE) {
aoqi@0 120 return err;
aoqi@0 121 }
aoqi@0 122 strcpy(ext_funcs[i].short_description, desc);
aoqi@0 123
aoqi@0 124 // params
aoqi@0 125
aoqi@0 126 jint param_count = _ext_functions->at(i)->param_count;
aoqi@0 127
aoqi@0 128 ext_funcs[i].param_count = param_count;
aoqi@0 129 if (param_count == 0) {
aoqi@0 130 ext_funcs[i].params = NULL;
aoqi@0 131 } else {
aoqi@0 132 err = rt.allocate(param_count*sizeof(jvmtiParamInfo),
aoqi@0 133 (unsigned char**)&(ext_funcs[i].params));
aoqi@0 134 if (err != JVMTI_ERROR_NONE) {
aoqi@0 135 return err;
aoqi@0 136 }
aoqi@0 137 jvmtiParamInfo* src_params = _ext_functions->at(i)->params;
aoqi@0 138 jvmtiParamInfo* dst_params = ext_funcs[i].params;
aoqi@0 139
aoqi@0 140 for (int j=0; j<param_count; j++) {
aoqi@0 141 err = rt.allocate(strlen(src_params[j].name)+1,
aoqi@0 142 (unsigned char**)&(dst_params[j].name));
aoqi@0 143 if (err != JVMTI_ERROR_NONE) {
aoqi@0 144 return err;
aoqi@0 145 }
aoqi@0 146 strcpy(dst_params[j].name, src_params[j].name);
aoqi@0 147
aoqi@0 148 dst_params[j].kind = src_params[j].kind;
aoqi@0 149 dst_params[j].base_type = src_params[j].base_type;
aoqi@0 150 dst_params[j].null_ok = src_params[j].null_ok;
aoqi@0 151 }
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 // errors
aoqi@0 155
aoqi@0 156 jint error_count = _ext_functions->at(i)->error_count;
aoqi@0 157 ext_funcs[i].error_count = error_count;
aoqi@0 158 if (error_count == 0) {
aoqi@0 159 ext_funcs[i].errors = NULL;
aoqi@0 160 } else {
aoqi@0 161 err = rt.allocate(error_count*sizeof(jvmtiError),
aoqi@0 162 (unsigned char**)&(ext_funcs[i].errors));
aoqi@0 163 if (err != JVMTI_ERROR_NONE) {
aoqi@0 164 return err;
aoqi@0 165 }
aoqi@0 166 memcpy(ext_funcs[i].errors, _ext_functions->at(i)->errors,
aoqi@0 167 error_count*sizeof(jvmtiError));
aoqi@0 168 }
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 *extension_count_ptr = _ext_functions->length();
aoqi@0 172 *extensions = ext_funcs;
aoqi@0 173 return JVMTI_ERROR_NONE;
aoqi@0 174 }
aoqi@0 175
aoqi@0 176
aoqi@0 177 // return the list of extension events
aoqi@0 178
aoqi@0 179 jvmtiError JvmtiExtensions::get_events(JvmtiEnv* env,
aoqi@0 180 jint* extension_count_ptr,
aoqi@0 181 jvmtiExtensionEventInfo** extensions)
aoqi@0 182 {
aoqi@0 183 guarantee(_ext_events != NULL, "registration not done");
aoqi@0 184
aoqi@0 185 ResourceTracker rt(env);
aoqi@0 186
aoqi@0 187 jvmtiExtensionEventInfo* ext_events;
aoqi@0 188 jvmtiError err = rt.allocate(_ext_events->length() * sizeof(jvmtiExtensionEventInfo),
aoqi@0 189 (unsigned char**)&ext_events);
aoqi@0 190 if (err != JVMTI_ERROR_NONE) {
aoqi@0 191 return err;
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 for (int i=0; i<_ext_events->length(); i++ ) {
aoqi@0 195 ext_events[i].extension_event_index = _ext_events->at(i)->extension_event_index;
aoqi@0 196
aoqi@0 197 char *id = _ext_events->at(i)->id;
aoqi@0 198 err = rt.allocate(strlen(id)+1, (unsigned char**)&(ext_events[i].id));
aoqi@0 199 if (err != JVMTI_ERROR_NONE) {
aoqi@0 200 return err;
aoqi@0 201 }
aoqi@0 202 strcpy(ext_events[i].id, id);
aoqi@0 203
aoqi@0 204 char *desc = _ext_events->at(i)->short_description;
aoqi@0 205 err = rt.allocate(strlen(desc)+1,
aoqi@0 206 (unsigned char**)&(ext_events[i].short_description));
aoqi@0 207 if (err != JVMTI_ERROR_NONE) {
aoqi@0 208 return err;
aoqi@0 209 }
aoqi@0 210 strcpy(ext_events[i].short_description, desc);
aoqi@0 211
aoqi@0 212 // params
aoqi@0 213
aoqi@0 214 jint param_count = _ext_events->at(i)->param_count;
aoqi@0 215
aoqi@0 216 ext_events[i].param_count = param_count;
aoqi@0 217 if (param_count == 0) {
aoqi@0 218 ext_events[i].params = NULL;
aoqi@0 219 } else {
aoqi@0 220 err = rt.allocate(param_count*sizeof(jvmtiParamInfo),
aoqi@0 221 (unsigned char**)&(ext_events[i].params));
aoqi@0 222 if (err != JVMTI_ERROR_NONE) {
aoqi@0 223 return err;
aoqi@0 224 }
aoqi@0 225 jvmtiParamInfo* src_params = _ext_events->at(i)->params;
aoqi@0 226 jvmtiParamInfo* dst_params = ext_events[i].params;
aoqi@0 227
aoqi@0 228 for (int j=0; j<param_count; j++) {
aoqi@0 229 err = rt.allocate(strlen(src_params[j].name)+1,
aoqi@0 230 (unsigned char**)&(dst_params[j].name));
aoqi@0 231 if (err != JVMTI_ERROR_NONE) {
aoqi@0 232 return err;
aoqi@0 233 }
aoqi@0 234 strcpy(dst_params[j].name, src_params[j].name);
aoqi@0 235
aoqi@0 236 dst_params[j].kind = src_params[j].kind;
aoqi@0 237 dst_params[j].base_type = src_params[j].base_type;
aoqi@0 238 dst_params[j].null_ok = src_params[j].null_ok;
aoqi@0 239 }
aoqi@0 240 }
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 *extension_count_ptr = _ext_events->length();
aoqi@0 244 *extensions = ext_events;
aoqi@0 245 return JVMTI_ERROR_NONE;
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 // set callback for an extension event and enable/disable it.
aoqi@0 249
aoqi@0 250 jvmtiError JvmtiExtensions::set_event_callback(JvmtiEnv* env,
aoqi@0 251 jint extension_event_index,
aoqi@0 252 jvmtiExtensionEvent callback)
aoqi@0 253 {
aoqi@0 254 guarantee(_ext_events != NULL, "registration not done");
aoqi@0 255
aoqi@0 256 jvmtiExtensionEventInfo* event = NULL;
aoqi@0 257
aoqi@0 258 // if there are extension events registered then validate that the
aoqi@0 259 // extension_event_index matches one of the registered events.
aoqi@0 260 if (_ext_events != NULL) {
aoqi@0 261 for (int i=0; i<_ext_events->length(); i++ ) {
aoqi@0 262 if (_ext_events->at(i)->extension_event_index == extension_event_index) {
aoqi@0 263 event = _ext_events->at(i);
aoqi@0 264 break;
aoqi@0 265 }
aoqi@0 266 }
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 // invalid event index
aoqi@0 270 if (event == NULL) {
aoqi@0 271 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 JvmtiEventController::set_extension_event_callback(env, extension_event_index,
aoqi@0 275 callback);
aoqi@0 276
aoqi@0 277 return JVMTI_ERROR_NONE;
aoqi@0 278 }

mercurial