jdk/src/share/demo/jvmti/versionCheck/versionCheck.c
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10292 ed7db6a12c2a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "jvmti.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#include "agent_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Create major.minor.micro version string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
version_check(jint cver, jint rver)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    jint cmajor, cminor, cmicro;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    jint rmajor, rminor, rmicro;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    cmajor = (cver & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    cminor = (cver & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    cmicro = (cver & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    rmajor = (rver & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    rminor = (rver & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    rmicro = (rver & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    stdout_message("Compile Time JVMTI Version: %d.%d.%d (0x%08x)\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                        cmajor, cminor, cmicro, cver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    stdout_message("Run Time JVMTI Version: %d.%d.%d (0x%08x)\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        rmajor, rminor, rmicro, rver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    if ( (cmajor > rmajor) || (cmajor == rmajor && cminor > rminor) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        fatal_error(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            "ERROR: Compile Time JVMTI and Run Time JVMTI are incompatible\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
/* Callback for JVMTI_EVENT_VM_INIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
vm_init(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    jint       runtime_version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /* The exact JVMTI version doesn't have to match, however this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *  code demonstrates how you can check that the JVMTI version seen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *  in the jvmti.h include file matches that being supplied at runtime
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *  by the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    err = (*jvmti)->GetVersionNumber(jvmti, &runtime_version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    check_jvmti_error(jvmti, err, "get version number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    version_check(JVMTI_VERSION, runtime_version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
/* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    jint                rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    jvmtiError          err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    jvmtiEventCallbacks callbacks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    jvmtiEnv           *jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* Get JVMTI environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    rc = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    if (rc != JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        fatal_error("ERROR: Unable to create jvmtiEnv, GetEnv failed, error=%d\n", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /* Set callbacks and enable event notifications */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    memset(&callbacks, 0, sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    callbacks.VMInit                  = &vm_init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    check_jvmti_error(jvmti, err, "set event callbacks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                        JVMTI_EVENT_VM_INIT, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
/* Agent_OnUnload() is called last */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
Agent_OnUnload(JavaVM *vm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}