< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/unit/setNullVMInit/JvmtiTest/JvmtiTest.cpp

Print this page
rev 51722 : 8210700: Clean up JNI_ENV_ARG and factorize the macros for vmTestbase/jvmti/unit tests
Summary:
Reviewed-by:


  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 /*
  25  This test case to test the following:
  26 
  27            VMInit initial thread arg.
  28            SetThreadLocalStorage and SetEnvironmentLocalStorage should allow
  29            value to be set to NULL.
  30  */
  31 
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <inttypes.h>
  35 #include "jvmti.h"
  36 #include "jni_tools.h"
  37 #include "agent_common.h"
  38 
  39 #ifdef __cplusplus
  40 extern "C" {
  41 #endif
  42 
  43 #ifndef JNI_ENV_ARG
  44 
  45 #ifdef __cplusplus
  46 #define JNI_ENV_ARG(x, y) y
  47 #define JNI_ENV_ARG1(x)
  48 #define JNI_ENV_PTR(x) x
  49 #else
  50 #define JNI_ENV_ARG(x,y) x, y
  51 #define JNI_ENV_ARG1(x) x
  52 #define JNI_ENV_PTR(x) (*x)
  53 #endif
  54 
  55 #endif
  56 
  57 #define JVMTI_ENV_ARG JNI_ENV_ARG
  58 #define JVMTI_ENV_ARG1 JNI_ENV_ARG1
  59 #define JVMTI_ENV_PTR JNI_ENV_PTR
  60 
  61 #define JVMTI_ERROR_CHECK(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); return res;}
  62 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR(str,res,err) if ( res != err) { printf(str); printf("unexpected error %d\n",res); return res;}
  63 
  64 #define JVMTI_ERROR_CHECK_VOID(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); iGlobalStatus = 2; }
  65 
  66 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR_VOID(str,res,err) if ( res != err) { printf(str); printf("unexpected error %d\n",res); iGlobalStatus = 2; }
  67 
  68 jvmtiEnv *jvmti;
  69 jint iGlobalStatus = 0;
  70 static jvmtiEventCallbacks callbacks;
  71 
  72 int printdump = 0;
  73 
  74 
  75 void debug_printf(char *fmt, ...) {
  76     va_list args;
  77 
  78     va_start(args, fmt);
  79     if (printdump) {
  80         vprintf(fmt, args);
  81     }
  82     va_end(args);
  83 }
  84 
  85 
  86 intptr_t get_env_local() {
  87   jvmtiError res;
  88   void *val;
  89   res = JVMTI_ENV_PTR(jvmti)->GetEnvironmentLocalStorage(JVMTI_ENV_ARG(jvmti, &val));
  90   JVMTI_ERROR_CHECK("GetEnvironmentLocalStorage returned error", res);
  91   return (intptr_t)val;
  92 }
  93 
  94 void set_env_local(intptr_t x) {
  95   jvmtiError res;
  96   void *val = (void*)x;
  97   res = JVMTI_ENV_PTR(jvmti)->SetEnvironmentLocalStorage(JVMTI_ENV_ARG(jvmti, val));
  98   JVMTI_ERROR_CHECK_VOID("SetEnvironmentLocalStorage returned error", res);
  99 }
 100 
 101 intptr_t get_thread_local(jthread thread) {
 102   jvmtiError res;
 103   void *val;
 104   res = JVMTI_ENV_PTR(jvmti)->GetThreadLocalStorage(JVMTI_ENV_ARG(jvmti, thread), &val);
 105   JVMTI_ERROR_CHECK("GetThreadLocalStorage returned error", res);
 106   return (intptr_t)val;
 107 }
 108 
 109 void set_thread_local(jthread thread, intptr_t x) {
 110   jvmtiError res;
 111   void *val = (void*)x;
 112   res = JVMTI_ENV_PTR(jvmti)->SetThreadLocalStorage(JVMTI_ENV_ARG(jvmti, thread), val);
 113   JVMTI_ERROR_CHECK_VOID("SetThreadLocalStorage returned error", res);
 114 }
 115 
 116 void check_val(intptr_t x, intptr_t y, const char* msg) {
 117   if (x != y) {
 118     printf("Error in %s: expected %" PRIdPTR " to be %" PRIdPTR "\n", msg, x, y);
 119     iGlobalStatus = 2;
 120   } else if (printdump) {
 121     printf("Correct in %s: expected %" PRIdPTR " to be %" PRIdPTR "\n", msg, x, y);
 122   }
 123 }
 124 
 125 
 126 void JNICALL vmInit(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread) {
 127   check_val(get_thread_local(thread), 0, "thread initial");
 128   check_val(get_thread_local(NULL), 0, "thread initial");
 129   set_thread_local(thread, 35);
 130   check_val(get_thread_local(thread), 35, "thread set non-zero");
 131   check_val(get_thread_local(NULL), 35, "thread set non-zero");
 132   set_thread_local(NULL, 0);


 147 #ifdef STATIC_BUILD
 148 JNIEXPORT jint JNICALL Agent_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
 149     return Agent_Initialize(jvm, options, reserved);
 150 }
 151 JNIEXPORT jint JNICALL Agent_OnAttach_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
 152     return Agent_Initialize(jvm, options, reserved);
 153 }
 154 JNIEXPORT jint JNI_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
 155     return JNI_VERSION_1_8;
 156 }
 157 #endif
 158 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
 159     jint res;
 160 
 161     if (options && strlen(options) > 0) {
 162         if (strstr(options, "printdump")) {
 163             printdump = 1;
 164         }
 165     }
 166 
 167     res = JNI_ENV_PTR(jvm)->
 168         GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_1_1);
 169     if (res < 0) {
 170         printf("Wrong result of a valid call to GetEnv!\n");
 171         return JNI_ERR;
 172     }
 173 
 174     check_val(get_env_local(), 0, "env initial");
 175     set_env_local(0);
 176     check_val(get_env_local(), 0, "env set zero");
 177     set_env_local(14);
 178     check_val(get_env_local(), 14, "env set non-zero");
 179 
 180     /* Enable events */
 181     init_callbacks();
 182     res = JVMTI_ENV_PTR(jvmti)->SetEventCallbacks(JVMTI_ENV_ARG(jvmti, &callbacks), sizeof(callbacks));
 183     JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
 184 
 185     res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti, JVMTI_ENABLE), JVMTI_EVENT_VM_INIT,NULL);
 186     JVMTI_ERROR_CHECK("SetEventNotificationMode for VM_INIT returned error", res);
 187 
 188     return JNI_OK;
 189 }
 190 
 191 
 192 
 193 JNIEXPORT jint JNICALL
 194 Java_nsk_jvmti_unit_setNullVMInit_JvmtiTest_check(JNIEnv *env, jclass cls) {
 195   check_val(get_env_local(), 77, "env lasts");
 196   set_env_local(0);
 197   check_val(get_env_local(), 0, "env reset to zero");
 198 
 199   check_val(get_thread_local(NULL), 0, "thread check");
 200 
 201   return iGlobalStatus;
 202 }
 203 
 204 #ifdef __cplusplus
 205 }
 206 #endif


  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 /*
  25  This test case to test the following:
  26 
  27            VMInit initial thread arg.
  28            SetThreadLocalStorage and SetEnvironmentLocalStorage should allow
  29            value to be set to NULL.
  30  */
  31 
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <inttypes.h>
  35 #include "jvmti.h"
  36 #include "jni_tools.h"
  37 #include "agent_common.h"
  38 

  39 extern "C" {



















  40 
  41 #define JVMTI_ERROR_CHECK(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); return res;}
  42 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR(str,res,err) if ( res != err) { printf(str); printf("unexpected error %d\n",res); return res;}
  43 
  44 #define JVMTI_ERROR_CHECK_VOID(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); iGlobalStatus = 2; }
  45 
  46 #define JVMTI_ERROR_CHECK_EXPECTED_ERROR_VOID(str,res,err) if ( res != err) { printf(str); printf("unexpected error %d\n",res); iGlobalStatus = 2; }
  47 
  48 jvmtiEnv *jvmti;
  49 jint iGlobalStatus = 0;
  50 static jvmtiEventCallbacks callbacks;
  51 
  52 int printdump = 0;
  53 
  54 
  55 void debug_printf(char *fmt, ...) {
  56     va_list args;
  57 
  58     va_start(args, fmt);
  59     if (printdump) {
  60         vprintf(fmt, args);
  61     }
  62     va_end(args);
  63 }
  64 
  65 
  66 intptr_t get_env_local() {
  67   jvmtiError res;
  68   void *val;
  69   res = jvmti->GetEnvironmentLocalStorage(&val);
  70   JVMTI_ERROR_CHECK("GetEnvironmentLocalStorage returned error", res);
  71   return (intptr_t)val;
  72 }
  73 
  74 void set_env_local(intptr_t x) {
  75   jvmtiError res;
  76   void *val = (void*)x;
  77   res = jvmti->SetEnvironmentLocalStorage(val);
  78   JVMTI_ERROR_CHECK_VOID("SetEnvironmentLocalStorage returned error", res);
  79 }
  80 
  81 intptr_t get_thread_local(jthread thread) {
  82   jvmtiError res;
  83   void *val;
  84   res = jvmti->GetThreadLocalStorage(thread, &val);
  85   JVMTI_ERROR_CHECK("GetThreadLocalStorage returned error", res);
  86   return (intptr_t)val;
  87 }
  88 
  89 void set_thread_local(jthread thread, intptr_t x) {
  90   jvmtiError res;
  91   void *val = (void*)x;
  92   res = jvmti->SetThreadLocalStorage(thread, val);
  93   JVMTI_ERROR_CHECK_VOID("SetThreadLocalStorage returned error", res);
  94 }
  95 
  96 void check_val(intptr_t x, intptr_t y, const char* msg) {
  97   if (x != y) {
  98     printf("Error in %s: expected %" PRIdPTR " to be %" PRIdPTR "\n", msg, x, y);
  99     iGlobalStatus = 2;
 100   } else if (printdump) {
 101     printf("Correct in %s: expected %" PRIdPTR " to be %" PRIdPTR "\n", msg, x, y);
 102   }
 103 }
 104 
 105 
 106 void JNICALL vmInit(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread) {
 107   check_val(get_thread_local(thread), 0, "thread initial");
 108   check_val(get_thread_local(NULL), 0, "thread initial");
 109   set_thread_local(thread, 35);
 110   check_val(get_thread_local(thread), 35, "thread set non-zero");
 111   check_val(get_thread_local(NULL), 35, "thread set non-zero");
 112   set_thread_local(NULL, 0);


 127 #ifdef STATIC_BUILD
 128 JNIEXPORT jint JNICALL Agent_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
 129     return Agent_Initialize(jvm, options, reserved);
 130 }
 131 JNIEXPORT jint JNICALL Agent_OnAttach_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
 132     return Agent_Initialize(jvm, options, reserved);
 133 }
 134 JNIEXPORT jint JNI_OnLoad_JvmtiTest(JavaVM *jvm, char *options, void *reserved) {
 135     return JNI_VERSION_1_8;
 136 }
 137 #endif
 138 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
 139     jint res;
 140 
 141     if (options && strlen(options) > 0) {
 142         if (strstr(options, "printdump")) {
 143             printdump = 1;
 144         }
 145     }
 146 
 147     res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);

 148     if (res < 0) {
 149         printf("Wrong result of a valid call to GetEnv!\n");
 150         return JNI_ERR;
 151     }
 152 
 153     check_val(get_env_local(), 0, "env initial");
 154     set_env_local(0);
 155     check_val(get_env_local(), 0, "env set zero");
 156     set_env_local(14);
 157     check_val(get_env_local(), 14, "env set non-zero");
 158 
 159     /* Enable events */
 160     init_callbacks();
 161     res = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
 162     JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
 163 
 164     res = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT,NULL);
 165     JVMTI_ERROR_CHECK("SetEventNotificationMode for VM_INIT returned error", res);
 166 
 167     return JNI_OK;
 168 }
 169 
 170 
 171 
 172 JNIEXPORT jint JNICALL
 173 Java_nsk_jvmti_unit_setNullVMInit_JvmtiTest_check(JNIEnv *env, jclass cls) {
 174   check_val(get_env_local(), 77, "env lasts");
 175   set_env_local(0);
 176   check_val(get_env_local(), 0, "env reset to zero");
 177 
 178   check_val(get_thread_local(NULL), 0, "thread check");
 179 
 180   return iGlobalStatus;
 181 }
 182 

 183 }

< prev index next >