JVM.c 10.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
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
 */

#include <stdlib.h>

#include "jvm.h"
#include "jni.h"
#include "jni_util.h"

#include "jvm_symbols.h"
#include "sun_tracing_dtrace_JVM.h"

#ifdef __cplusplus
extern "C" {
#endif

static JvmSymbols* jvm_symbols = NULL;

static void initialize() {
    static int initialized = 0;
    if (initialized == 0) {
        jvm_symbols = lookupJvmSymbols();
        initialized = 1;
    }
}

/*
 * Class:     sun_tracing_dtrace_JVM
 * Method:    isSupported0
 * Signature: ()I
 */
JNIEXPORT jboolean JNICALL Java_sun_tracing_dtrace_JVM_isSupported0(
        JNIEnv* env, jclass cls) {
    initialize();
    if (jvm_symbols != NULL) {
        return jvm_symbols->IsSupported(env) ? JNI_TRUE : JNI_FALSE;
    } else {
        return JNI_FALSE;
    }
}

// Macros that cause an immediate return if we detect an exception
#define CHECK if ((*env)->ExceptionOccurred(env)) { return; }
#define CHECK_(x) if ((*env)->ExceptionOccurred(env)) { return x; }

static void readProbeData (
        JNIEnv* env, jobject probe, JVM_DTraceProbe* jvm_probe) {
    jclass clazz;
    jmethodID mid;
    jobject method;

    if (jvm_probe == NULL) {
        return; // just in case
    }

    clazz = (*env)->GetObjectClass(env, probe); CHECK

    mid = (*env)->GetMethodID(
        env, clazz, "getFunctionName", "()Ljava/lang/String;"); CHECK
    jvm_probe->function = (jstring)(*env)->CallObjectMethod(
        env, probe, mid); CHECK

    mid = (*env)->GetMethodID(
        env, clazz, "getProbeName", "()Ljava/lang/String;"); CHECK
    jvm_probe->name = (jstring)(*env)->CallObjectMethod(env, probe, mid); CHECK

    mid = (*env)->GetMethodID(
        env, clazz, "getMethod", "()Ljava/lang/reflect/Method;"); CHECK
    method = (*env)->CallObjectMethod(env, probe, mid); CHECK
    jvm_probe->method = (*env)->FromReflectedMethod(env, method); CHECK
}

static void readFieldInterfaceAttributes(
        char* annotationName, JNIEnv* env, jobject provider,
        JVM_DTraceInterfaceAttributes* attrs) {
    jobject result;
    jobject result_clazz;
    jclass provider_clazz;
    jclass annotation_clazz;
    jmethodID get;
    jmethodID enc;

    provider_clazz = (*env)->GetObjectClass(env, provider); CHECK
    annotation_clazz = (*env)->FindClass(env, annotationName); CHECK

    get = (*env)->GetMethodID(env, provider_clazz, "getNameStabilityFor",
        "(Ljava/lang/Class;)Lcom/sun/tracing/dtrace/StabilityLevel;"); CHECK
    result = (*env)->CallObjectMethod(
        env, provider, get, annotation_clazz); CHECK
    result_clazz = (*env)->GetObjectClass(env, result); CHECK
    enc = (*env)->GetMethodID(env, result_clazz, "getEncoding", "()I"); CHECK
    attrs->nameStability = (*env)->CallIntMethod(env, result, enc); CHECK

    get = (*env)->GetMethodID(env, provider_clazz, "getDataStabilityFor",
        "(Ljava/lang/Class;)Lcom/sun/tracing/dtrace/StabilityLevel;"); CHECK
    result = (*env)->CallObjectMethod(
        env, provider, get, annotation_clazz); CHECK
    result_clazz = (*env)->GetObjectClass(env, result); CHECK
    enc = (*env)->GetMethodID(env, result_clazz, "getEncoding", "()I"); CHECK
    attrs->dataStability = (*env)->CallIntMethod(env, result, enc); CHECK

    get = (*env)->GetMethodID(env, provider_clazz, "getDependencyClassFor",
        "(Ljava/lang/Class;)Lcom/sun/tracing/dtrace/DependencyClass;"); CHECK
    result = (*env)->CallObjectMethod(
        env, provider, get, annotation_clazz); CHECK
    result_clazz = (*env)->GetObjectClass(env, result); CHECK
    enc = (*env)->GetMethodID(env, result_clazz, "getEncoding", "()I"); CHECK
    attrs->dependencyClass = (*env)->CallIntMethod(env, result, enc); CHECK
}

static void readInterfaceAttributes(
        JNIEnv* env, jobject provider, JVM_DTraceProvider* jvm_provider) {
    readFieldInterfaceAttributes("com/sun/tracing/dtrace/ProviderAttributes",
        env, provider, &(jvm_provider->providerAttributes));
    readFieldInterfaceAttributes("com/sun/tracing/dtrace/ModuleAttributes",
        env, provider, &(jvm_provider->moduleAttributes));
    readFieldInterfaceAttributes("com/sun/tracing/dtrace/FunctionAttributes",
        env, provider, &(jvm_provider->functionAttributes));
    readFieldInterfaceAttributes("com/sun/tracing/dtrace/NameAttributes",
        env, provider, &(jvm_provider->nameAttributes));
    readFieldInterfaceAttributes("com/sun/tracing/dtrace/ArgsAttributes",
        env, provider, &(jvm_provider->argsAttributes));
}

147
static int readProviderData(
148 149 150 151
        JNIEnv* env, jobject provider, JVM_DTraceProvider* jvm_provider) {
    jmethodID mid;
    jobjectArray probes;
    jsize i;
152
    jclass clazz = (*env)->GetObjectClass(env, provider); CHECK_(0)
153
    mid = (*env)->GetMethodID(
154
        env, clazz, "getProbes", "()[Lsun/tracing/dtrace/DTraceProbe;"); CHECK_(0)
155
    probes = (jobjectArray)(*env)->CallObjectMethod(
156
        env, provider, mid); CHECK_(0)
157 158

    // Fill JVM structure, describing provider
159
    jvm_provider->probe_count = (*env)->GetArrayLength(env, probes); CHECK_(0)
160 161 162
    jvm_provider->probes = (JVM_DTraceProbe*)calloc(
        jvm_provider->probe_count, sizeof(*jvm_provider->probes));
    mid = (*env)->GetMethodID(
163
        env, clazz, "getProviderName", "()Ljava/lang/String;"); CHECK_(0)
164
    jvm_provider->name = (jstring)(*env)->CallObjectMethod(
165
        env, provider, mid); CHECK_(0)
166

167
    readInterfaceAttributes(env, provider, jvm_provider); CHECK_(0)
168 169

    for (i = 0; i < jvm_provider->probe_count; ++i) {
170 171
        jobject probe = (*env)->GetObjectArrayElement(env, probes, i); CHECK_(0)
        readProbeData(env, probe, &jvm_provider->probes[i]); CHECK_(0)
172
    }
173 174

    return 1;
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
}

/*
 * Class:     sun_tracing_dtrace_JVM
 * Method:    activate0
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_sun_tracing_dtrace_JVM_activate0(
        JNIEnv* env, jclass cls, jstring moduleName, jobjectArray providers) {
    jlong handle = 0;
    jsize num_providers;
    jsize i;
    JVM_DTraceProvider* jvm_providers;

    initialize();

    if (jvm_symbols == NULL) {
      return 0;
    }

    num_providers = (*env)->GetArrayLength(env, providers); CHECK_(0L)

    jvm_providers = (JVM_DTraceProvider*)calloc(
        num_providers, sizeof(*jvm_providers));

200 201 202
    int count = 0;
    for (; count < num_providers; ++count) {
        JVM_DTraceProvider* p = &(jvm_providers[count]);
203
        jobject provider = (*env)->GetObjectArrayElement(
204 205 206 207 208 209
            env, providers, count);
        if ((*env)->ExceptionOccurred(env) ||
            ! readProviderData(env, provider, p)) {
            // got an error, bail out!
            break;
        }
210 211
    }

212 213 214 215 216 217
    if (count == num_providers) {
        // all providers successfully loaded - get the handle
        handle = jvm_symbols->Activate(
            env, JVM_TRACING_DTRACE_VERSION, moduleName,
            num_providers, jvm_providers);
    }
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

    for (i = 0; i < num_providers; ++i) {
        JVM_DTraceProvider* p = &(jvm_providers[i]);
        free(p->probes);
    }
    free(jvm_providers);

    return handle;
}

/*
 * Class:     sun_tracing_dtrace_JVM
 * Method:    dispose0
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_sun_tracing_dtrace_JVM_dispose0(
        JNIEnv* env, jclass cls, jlong handle) {
    if (jvm_symbols != NULL && handle != 0) {
        jvm_symbols->Dispose(env, handle);
    }
}

/*
 * Class:     sun_tracing_dtrace_JVM
 * Method:    isEnabled0
 * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
 */
JNIEXPORT jboolean JNICALL Java_sun_tracing_dtrace_JVM_isEnabled0(
        JNIEnv* env, jclass cls, jobject method) {
    jmethodID mid;
    if (jvm_symbols != NULL && method != NULL) {
        mid = (*env)->FromReflectedMethod(env, method);
        return jvm_symbols->IsProbeEnabled(env, mid);
    }
    return JNI_FALSE;
}

/*
 * Class:     sun_tracing_dtrace_JVM
 * Method:    defineClass0
 * Signature: (Ljava/lang/ClassLoader;Ljava/lang/String;[BII)Ljava/lang/Class;
 *
 * The implementation of this native static method is a copy of that of
 * the native instance method Java_java_lang_ClassLoader_defineClass0()
 * with the implicit "this" parameter becoming the "loader" parameter.
 *
 * This code was cloned and modified from java_lang_reflect_Proxy
 */
JNIEXPORT jclass JNICALL
Java_sun_tracing_dtrace_JVM_defineClass0(
        JNIEnv *env, jclass ignore, jobject loader, jstring name, jbyteArray data,
        jint offset, jint length)
{
    jbyte *body;
    char *utfName;
    jclass result = 0;
    char buf[128];

    if (data == NULL) {
        return 0;
    }

    /* Work around 4153825. malloc crashes on Solaris when passed a
     * negative size.
     */
    if (length < 0) {
        return 0;
    }

    body = (jbyte *)malloc(length);

    if (body == 0) {
        return 0;
    }

    (*env)->GetByteArrayRegion(env, data, offset, length, body);

    if ((*env)->ExceptionOccurred(env))
        goto free_body;

    if (name != NULL) {
        int i;
        int len = (*env)->GetStringUTFLength(env, name);
        int unicode_len = (*env)->GetStringLength(env, name);
        if (len >= sizeof(buf)) {
            utfName = malloc(len + 1);
            if (utfName == NULL) {
                goto free_body;
            }
        } else {
            utfName = buf;
        }
        (*env)->GetStringUTFRegion(env, name, 0, unicode_len, utfName);

        // Convert '.' to '/' in the package name
        for (i = 0; i < unicode_len; ++i) {
            if (utfName[i] == '.') {
                utfName[i] = '/';
            }
        }
    } else {
        utfName = NULL;
    }

    result = (*env)->DefineClass(env, utfName, loader, body, length);

    if (utfName && utfName != buf)
        free(utfName);

 free_body:
    free(body);
    return result;
}

#ifdef __cplusplus
}
#endif