1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
* Other names may be trademarks of their respective owners.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
/*
* author Tomas Hurka
* Ian Formanek
* Misha Dmitriev
*/
#ifdef WIN32
#include <Windows.h>
#else
#include <sys/time.h>
#include <fcntl.h>
#include <time.h>
#endif
#ifdef SOLARIS
#define _STRUCTURED_PROC 1
#include <sys/procfs.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "jni.h"
#include "jvmti.h"
#include "org_netbeans_lib_profiler_server_system_Classes.h"
#include "common_functions.h"
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/*
* Class: org_netbeans_lib_profiler_server_system_Classes
* Method: getAllLoadedClasses
* Signature: ()[Ljava/lang/Class;
*/
JNIEXPORT jobjectArray JNICALL Java_org_netbeans_lib_profiler_server_system_Classes_getAllLoadedClasses
(JNIEnv *env, jclass clz)
{
jvmtiError res;
jint classCount, classStatus;
jclass *classes;
jobjectArray ret;
jclass type;
int i, j, n_linked_classes;
char *class_status;
res = (*_jvmti)->GetLoadedClasses(_jvmti, &classCount, &classes);
assert(res == JVMTI_ERROR_NONE);
n_linked_classes = 0;
class_status = malloc(classCount);
for (i = 0; i < classCount; i++) {
(*_jvmti)->GetClassStatus(_jvmti, classes[i], &classStatus);
if ((classStatus & JVMTI_CLASS_STATUS_PREPARED) != 0 && (classStatus & JVMTI_CLASS_STATUS_ERROR) == 0) {
class_status[i] = 1;
n_linked_classes++;
} else {
class_status[i] = 0;
}
}
type = (*env)->FindClass(env, "java/lang/Class");
assert(type != NULL);
ret = (*env)->NewObjectArray(env, n_linked_classes, type, NULL);
if (ret != NULL) {
j = 0;
for (i = 0; i < classCount; i++) {
if (class_status[i]) {
(*env)->SetObjectArrayElement(env, ret, j++, classes[i]);
}
}
}
free(class_status);
res = (*_jvmti)->Deallocate(_jvmti, (unsigned char*) classes);
assert(res == JVMTI_ERROR_NONE);
return ret;
}
/*
* Class: org_netbeans_lib_profiler_server_system_Classes
* Method: cacheLoadedClasses
* Signature: ([Ljava/lang/Class;I)V
*/
JNIEXPORT void JNICALL Java_org_netbeans_lib_profiler_server_system_Classes_cacheLoadedClasses
(JNIEnv *env, jclass clz, jobjectArray non_system_classes, jint class_count)
{
jclass *classDefs = calloc(class_count,sizeof(jclass));
int i;
for (i = 0; i < class_count; i++) {
classDefs[i] = (*env)->GetObjectArrayElement(env, non_system_classes, i);
}
cache_loaded_classes(_jvmti,classDefs,class_count);
free(classDefs);
}
/*
* Class: org_netbeans_lib_profiler_server_system_Classes
* Method: getCachedClassFileBytes
* Signature: (Ljava/lang/Class;)[B
*/
JNIEXPORT jbyteArray JNICALL Java_org_netbeans_lib_profiler_server_system_Classes_getCachedClassFileBytes
(JNIEnv *env, jclass clz, jclass clazz)
{
char *class_sig, *class_gen_sig;
jobject loader;
unsigned char *class_data;
int class_data_len;
jbyteArray ret;
jvmtiError res;
res = (*_jvmti)->GetClassSignature(_jvmti, clazz, &class_sig, &class_gen_sig);
assert(res == JVMTI_ERROR_NONE);
res = (*_jvmti)->GetClassLoader(_jvmti, clazz, &loader);
assert(res == JVMTI_ERROR_NONE);
/* class_sig is gonna look something like Lfoo/Bar; Convert it back into normal */
class_sig[strlen(class_sig) - 1] = 0;
get_saved_class_file_bytes(env, class_sig+1, loader, (jint*)&class_data_len, &class_data);
(*_jvmti)->Deallocate(_jvmti, (void*) class_sig);
(*_jvmti)->Deallocate(_jvmti, (void*) class_gen_sig);
if (class_data == NULL) {
return NULL;
}
ret = (*env)->NewByteArray(env, class_data_len);
(*env)->SetByteArrayRegion(env, ret, 0, class_data_len, (jbyte*) class_data);
free(class_data);
return ret;
}
static jclass profilerInterfaceClazz;
static jmethodID classLoadHookMethod = NULL;
void JNICALL register_class_prepare(jvmtiEnv *jvmti_env, JNIEnv* env, jthread thread, jclass clazz) {
(*env)->CallStaticVoidMethod(env, profilerInterfaceClazz, classLoadHookMethod, clazz);
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionDescribe(env);
}
}
/*
* Class: org_netbeans_lib_profiler_server_system_Classes
* Method: enableClassLoadHook
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_netbeans_lib_profiler_server_system_Classes_enableClassLoadHook
(JNIEnv *env, jclass clz)
{
jvmtiError res;
if (classLoadHookMethod == NULL) {
profilerInterfaceClazz = (*env)->FindClass(env, "org/netbeans/lib/profiler/server/ProfilerInterface");
profilerInterfaceClazz = (*env)->NewGlobalRef(env, profilerInterfaceClazz);
classLoadHookMethod = (*env)->GetStaticMethodID(env, profilerInterfaceClazz, "classLoadHook", "(Ljava/lang/Class;)V");
_jvmti_callbacks->ClassPrepare = register_class_prepare;
res = (*_jvmti)->SetEventCallbacks(_jvmti, _jvmti_callbacks, sizeof(*_jvmti_callbacks));
assert (res == JVMTI_ERROR_NONE);
}
res = (*_jvmti)->SetEventNotificationMode(_jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL);
assert(res == JVMTI_ERROR_NONE);
}
/*
* Class: org_netbeans_lib_profiler_server_system_Classes
* Method: disableClassLoadHook
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_netbeans_lib_profiler_server_system_Classes_disableClassLoadHook
(JNIEnv *env, jclass clz)
{
jvmtiError res;
res = (*_jvmti)->SetEventNotificationMode(_jvmti, JVMTI_DISABLE, JVMTI_EVENT_CLASS_PREPARE, NULL);
assert(res == JVMTI_ERROR_NONE);
}
/*
* Class: org_netbeans_lib_profiler_server_system_Classes
* Method: getObjectSize
* Signature: (Ljava/lang/Object;)J
*/
JNIEXPORT jlong JNICALL Java_org_netbeans_lib_profiler_server_system_Classes_getObjectSize
(JNIEnv *env, jclass clz, jobject jobject)
{
jlong res;
(*_jvmti)->GetObjectSize(_jvmti, jobject, &res);
return res;
}
/*
* Class: org_netbeans_lib_profiler_server_system_Classes
* Method: doRedefineClasses
* Signature: ([Ljava/lang/Class;[[B)I
*/
JNIEXPORT jint JNICALL Java_org_netbeans_lib_profiler_server_system_Classes_doRedefineClasses
(JNIEnv *env, jclass clz, jobjectArray jclasses, jobjectArray jnewClassFileBytes)
{
static jboolean nativeMethodBindDisabled = FALSE;
jvmtiError res = JVMTI_ERROR_NONE;
jint nClasses, i;
jvmtiClassDefinition* classDefs;
if (!nativeMethodBindDisabled) {
// First, disable the NativeMethodBind event, assume that Thread.sleep and Object.wait have already been intercepted
res = (*_jvmti)->SetEventNotificationMode(_jvmti, JVMTI_DISABLE, JVMTI_EVENT_NATIVE_METHOD_BIND, NULL);
if (res != JVMTI_ERROR_NONE) {
fprintf (stderr, "Profiler Agent Error: Error while turning NativeMethodBind off: %d\n",res);
assert(res == JVMTI_ERROR_NONE);
}
nativeMethodBindDisabled = TRUE;
}
nClasses = (*env)->GetArrayLength(env, jclasses);
classDefs = malloc(sizeof(jvmtiClassDefinition) * nClasses);
for (i = 0; i < nClasses; i++) {
jbyteArray jnewClassBytes;
jbyte *tmpClassBytes;
jint classBytesLen;
jvmtiClassDefinition *classDef = classDefs + i;
classDef->klass = (*env)->GetObjectArrayElement(env, jclasses, i);
jnewClassBytes = (*env)->GetObjectArrayElement(env, jnewClassFileBytes, i);
classBytesLen = classDef->class_byte_count = (*env)->GetArrayLength(env, jnewClassBytes);
assert(classBytesLen > 0);
tmpClassBytes = (*env)->GetByteArrayElements(env, jnewClassBytes, NULL);
classDef->class_bytes = malloc(classBytesLen);
memcpy((jbyte*) classDef->class_bytes, tmpClassBytes, classBytesLen);
(*env)->ReleaseByteArrayElements(env, jnewClassBytes, tmpClassBytes, JNI_ABORT);
(*env)->DeleteLocalRef(env, jnewClassBytes);
}
if (nClasses <= 100) {
res = (*_jvmti)->RedefineClasses(_jvmti, nClasses, classDefs);
} else {
// perform batch redefine in units of 100 classes
int idx = 0;
while (idx < nClasses) {
int redefineCount = nClasses - idx;
if (redefineCount > 100) {
redefineCount = 100;
}
fprintf (stdout, "Profiler Agent: Redefining %d classes at idx %d, out of total %d \n",redefineCount, idx, (int)nClasses);
res = (*_jvmti)->RedefineClasses(_jvmti, redefineCount, classDefs + idx);
idx += 100;
}
}
for (i = 0; i < nClasses; i++) {
(*env)->DeleteLocalRef(env, classDefs[i].klass);
free((jbyte*) classDefs[i].class_bytes);
}
free(classDefs);
return res;
}
/*
* Class: org_netbeans_lib_profiler_server_system_Classes
* Method: notifyAboutClassLoaderUnloading
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_org_netbeans_lib_profiler_server_system_Classes_notifyAboutClassLoaderUnloading
(JNIEnv *env, jclass clz)
{
try_removing_bytes_for_unloaded_classes(env);
}
|