test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClearFieldModificationWatch/clrfmodw002/clrfmodw002.cpp
changeset 51551 e409244ce72e
parent 50260 46c67f5e27c2
child 51699 543a3fb81c4c
equal deleted inserted replaced
51550:a2f1923b3e16 51551:e409244ce72e
       
     1 /*
       
     2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 #include <stdio.h>
       
    25 #include <string.h>
       
    26 #include "jvmti.h"
       
    27 #include "agent_common.h"
       
    28 #include "JVMTITools.h"
       
    29 
       
    30 #ifdef __cplusplus
       
    31 extern "C" {
       
    32 #endif
       
    33 
       
    34 #ifndef JNI_ENV_ARG
       
    35 
       
    36 #ifdef __cplusplus
       
    37 #define JNI_ENV_ARG(x, y) y
       
    38 #define JNI_ENV_PTR(x) x
       
    39 #else
       
    40 #define JNI_ENV_ARG(x,y) x, y
       
    41 #define JNI_ENV_PTR(x) (*x)
       
    42 #endif
       
    43 
       
    44 #endif
       
    45 
       
    46 #define PASSED  0
       
    47 #define STATUS_FAILED  2
       
    48 
       
    49 static jvmtiEnv *jvmti;
       
    50 static jvmtiEventCallbacks callbacks;
       
    51 static jvmtiCapabilities caps;
       
    52 static jint result = PASSED;
       
    53 
       
    54 void JNICALL FieldModification(jvmtiEnv *jvmti_env, JNIEnv *env,
       
    55         jthread thd, jmethodID mid, jlocation loc,
       
    56         jclass field_klass, jobject obj, jfieldID field,
       
    57         char sig, jvalue new_value) {
       
    58 }
       
    59 
       
    60 #ifdef STATIC_BUILD
       
    61 JNIEXPORT jint JNICALL Agent_OnLoad_clrfmodw002(JavaVM *jvm, char *options, void *reserved) {
       
    62     return Agent_Initialize(jvm, options, reserved);
       
    63 }
       
    64 JNIEXPORT jint JNICALL Agent_OnAttach_clrfmodw002(JavaVM *jvm, char *options, void *reserved) {
       
    65     return Agent_Initialize(jvm, options, reserved);
       
    66 }
       
    67 JNIEXPORT jint JNI_OnLoad_clrfmodw002(JavaVM *jvm, char *options, void *reserved) {
       
    68     return JNI_VERSION_1_8;
       
    69 }
       
    70 #endif
       
    71 jint  Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
       
    72     jint res;
       
    73     jvmtiError err;
       
    74 
       
    75     res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
       
    76         JVMTI_VERSION_1_1);
       
    77     if (res != JNI_OK || jvmti == NULL) {
       
    78         printf("Wrong result of a valid call to GetEnv !\n");
       
    79         return JNI_ERR;
       
    80     }
       
    81 
       
    82     err = jvmti->GetPotentialCapabilities(&caps);
       
    83     if (err != JVMTI_ERROR_NONE) {
       
    84         printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
       
    85                TranslateError(err), err);
       
    86         return JNI_ERR;
       
    87     }
       
    88 
       
    89     err = jvmti->AddCapabilities(&caps);
       
    90     if (err != JVMTI_ERROR_NONE) {
       
    91         printf("(AddCapabilities) unexpected error: %s (%d)\n",
       
    92                TranslateError(err), err);
       
    93         return JNI_ERR;
       
    94     }
       
    95 
       
    96     err = jvmti->GetCapabilities(&caps);
       
    97     if (err != JVMTI_ERROR_NONE) {
       
    98         printf("(GetCapabilities) unexpected error: %s (%d)\n",
       
    99                TranslateError(err), err);
       
   100         return JNI_ERR;
       
   101     }
       
   102 
       
   103     if (caps.can_generate_field_modification_events) {
       
   104         callbacks.FieldModification = &FieldModification;
       
   105         err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
       
   106         if (err != JVMTI_ERROR_NONE) {
       
   107             printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
       
   108                    TranslateError(err), err);
       
   109             return JNI_ERR;
       
   110         }
       
   111 
       
   112         err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
       
   113                 JVMTI_EVENT_FIELD_MODIFICATION, NULL);
       
   114         if (err != JVMTI_ERROR_NONE) {
       
   115             printf("Failed to enable JVMTI_EVENT_FIELD_MODIFICATION: %s (%d)\n",
       
   116                    TranslateError(err), err);
       
   117             return JNI_ERR;
       
   118         }
       
   119     } else {
       
   120         printf("Warning: FieldModification watch is not implemented\n");
       
   121     }
       
   122 
       
   123     return JNI_OK;
       
   124 }
       
   125 
       
   126 JNIEXPORT void JNICALL
       
   127 Java_nsk_jvmti_ClearFieldModificationWatch_clrfmodw002_check(JNIEnv *env,
       
   128         jclass cls) {
       
   129     jvmtiError err;
       
   130     jfieldID fid1, fid2;
       
   131 
       
   132     fid1 = JNI_ENV_PTR(env)->GetStaticFieldID(JNI_ENV_ARG(env, cls),
       
   133         "fld1", "I");
       
   134     fid2 = JNI_ENV_PTR(env)->GetStaticFieldID(JNI_ENV_ARG(env, cls),
       
   135         "fld2", "I");
       
   136 
       
   137     if (!caps.can_generate_field_modification_events) {
       
   138         printf("Warning: ClearFieldModificationWatch is not implemented\n");
       
   139         err = jvmti->ClearFieldModificationWatch(cls, fid1);
       
   140         if (err != JVMTI_ERROR_MUST_POSSESS_CAPABILITY) {
       
   141             result = STATUS_FAILED;
       
   142             printf("Failed to return MUST_POSSESS_CAPABILITY: %s (%d)\n",
       
   143                    TranslateError(err), err);
       
   144         }
       
   145     } else {
       
   146         err = jvmti->ClearFieldModificationWatch(NULL, fid2);
       
   147         if (err != JVMTI_ERROR_INVALID_CLASS) {
       
   148             result = STATUS_FAILED;
       
   149             printf("Failed to return JVMTI_ERROR_INVALID_CLASS: %s (%d)\n",
       
   150                    TranslateError(err), err);
       
   151         }
       
   152 
       
   153         err = jvmti->ClearFieldModificationWatch(cls, NULL);
       
   154         if (err != JVMTI_ERROR_INVALID_FIELDID) {
       
   155             result = STATUS_FAILED;
       
   156             printf("Failed to return INVALID_FIELDID: %s (%d)\n",
       
   157                    TranslateError(err), err);
       
   158         }
       
   159 
       
   160         err = jvmti->ClearFieldModificationWatch(cls, fid2);
       
   161         if (err != JVMTI_ERROR_NOT_FOUND) {
       
   162             result = STATUS_FAILED;
       
   163             printf("Failed to return NOT_FOUND: %s (%d)\n",
       
   164                    TranslateError(err), err);
       
   165         }
       
   166     }
       
   167 }
       
   168 
       
   169 JNIEXPORT jint JNICALL
       
   170 Java_nsk_jvmti_ClearFieldModificationWatch_clrfmodw002_getRes(JNIEnv *env,
       
   171         jclass cls) {
       
   172     return result;
       
   173 }
       
   174 
       
   175 #ifdef __cplusplus
       
   176 }
       
   177 #endif