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