< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/unit/agentthr/agentthr.cpp

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


  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 JavaVM *jvm_ins;
  50 static jvmtiEnv *jvmti = NULL;
  51 static jvmtiEventCallbacks callbacks;
  52 static jint result = PASSED;
  53 static jboolean printdump = JNI_FALSE;
  54 static jboolean agent_was_started = JNI_FALSE;
  55 static jboolean done = JNI_FALSE;
  56 
  57 
  58 jthread jthr(JNIEnv *env) {
  59     jclass thrClass;
  60     jmethodID cid;
  61     jthread res;
  62 
  63     thrClass = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, "java/lang/Thread"));
  64     cid = JNI_ENV_PTR(env)->GetMethodID(JNI_ENV_ARG(env, thrClass), "<init>", "()V");
  65     res = JNI_ENV_PTR(env)->NewObject(JNI_ENV_ARG(env, thrClass), cid);
  66     return res;
  67 }
  68 
  69 void JNICALL agent_start(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void *p) {
  70     JNIEnv *env;
  71 
  72     if (jvmti_env != jvmti) {
  73         printf("(agent_start) JVMTI envs don't match\n");
  74         result = STATUS_FAILED;
  75     }
  76 
  77     JNI_ENV_PTR(jvm_ins)->GetEnv(JNI_ENV_ARG(jvm_ins, (void **) &env),
  78         JNI_VERSION_1_2);
  79     if (jni_env != env) {
  80         printf("(agent_start) JNI envs don't match\n");
  81         result = STATUS_FAILED;
  82     }
  83 
  84     if ((size_t)p != 12345) {
  85         printf("(agent_start) args don't match\n");
  86         result = STATUS_FAILED;
  87     }
  88     done = JNI_TRUE;
  89 }
  90 
  91 
  92 
  93 void JNICALL vm_init(jvmtiEnv* jvmti_env, JNIEnv *env, jthread thread) {
  94     jvmtiError err;
  95 
  96     if (!agent_was_started) {
  97         agent_was_started = JNI_TRUE;
  98 


 123 #ifdef STATIC_BUILD
 124 JNIEXPORT jint JNICALL Agent_OnLoad_agentthr(JavaVM *jvm, char *options, void *reserved) {
 125     return Agent_Initialize(jvm, options, reserved);
 126 }
 127 JNIEXPORT jint JNICALL Agent_OnAttach_agentthr(JavaVM *jvm, char *options, void *reserved) {
 128     return Agent_Initialize(jvm, options, reserved);
 129 }
 130 JNIEXPORT jint JNI_OnLoad_agentthr(JavaVM *jvm, char *options, void *reserved) {
 131     return JNI_VERSION_1_8;
 132 }
 133 #endif
 134 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 135     jvmtiError err;
 136     jint res;
 137 
 138     if (options != NULL && strcmp(options, "printdump") == 0) {
 139         printdump = JNI_TRUE;
 140     }
 141 
 142     jvm_ins = jvm;
 143     res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
 144         JVMTI_VERSION_1_1);
 145     if (res != JNI_OK || jvmti == NULL) {
 146         printf("Wrong result of a valid call to GetEnv!\n");
 147         return JNI_ERR;
 148     }
 149 
 150     callbacks.VMInit = &vm_init;
 151     err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
 152     if (err != JVMTI_ERROR_NONE) {
 153         printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
 154                TranslateError(err), err);
 155         return JNI_ERR;
 156     }
 157 
 158     err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
 159     if (err != JVMTI_ERROR_NONE) {
 160         printf("Failed to enable JVMTI_EVENT_THREAD_START: %s (%d)\n",
 161                TranslateError(err), err);
 162         return JNI_ERR;
 163     }
 164 
 165     return JNI_OK;
 166 }
 167 
 168 JNIEXPORT jboolean JNICALL
 169 Java_nsk_jvmti_unit_agentthr_isOver(JNIEnv *env, jclass cls) {
 170     return done;
 171 }
 172 
 173 JNIEXPORT jint JNICALL
 174 Java_nsk_jvmti_unit_agentthr_getRes(JNIEnv *env, jclass cls) {
 175     if (!done) {
 176         printf("agent thread has not completed\n");
 177         result = STATUS_FAILED;
 178     }
 179     return result;
 180 }
 181 
 182 #ifdef __cplusplus
 183 }
 184 #endif


  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 extern "C" {











  31 

  32 
  33 #define PASSED 0
  34 #define STATUS_FAILED 2
  35 
  36 static JavaVM *jvm_ins;
  37 static jvmtiEnv *jvmti = NULL;
  38 static jvmtiEventCallbacks callbacks;
  39 static jint result = PASSED;
  40 static jboolean printdump = JNI_FALSE;
  41 static jboolean agent_was_started = JNI_FALSE;
  42 static jboolean done = JNI_FALSE;
  43 
  44 
  45 jthread jthr(JNIEnv *env) {
  46     jclass thrClass;
  47     jmethodID cid;
  48     jthread res;
  49 
  50     thrClass = env->FindClass("java/lang/Thread");
  51     cid = env->GetMethodID(thrClass, "<init>", "()V");
  52     res = env->NewObject(thrClass, cid);
  53     return res;
  54 }
  55 
  56 void JNICALL agent_start(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void *p) {
  57     JNIEnv *env;
  58 
  59     if (jvmti_env != jvmti) {
  60         printf("(agent_start) JVMTI envs don't match\n");
  61         result = STATUS_FAILED;
  62     }
  63 
  64     jvm_ins->GetEnv((void **) &env, JNI_VERSION_1_2);

  65     if (jni_env != env) {
  66         printf("(agent_start) JNI envs don't match\n");
  67         result = STATUS_FAILED;
  68     }
  69 
  70     if ((size_t)p != 12345) {
  71         printf("(agent_start) args don't match\n");
  72         result = STATUS_FAILED;
  73     }
  74     done = JNI_TRUE;
  75 }
  76 
  77 
  78 
  79 void JNICALL vm_init(jvmtiEnv* jvmti_env, JNIEnv *env, jthread thread) {
  80     jvmtiError err;
  81 
  82     if (!agent_was_started) {
  83         agent_was_started = JNI_TRUE;
  84 


 109 #ifdef STATIC_BUILD
 110 JNIEXPORT jint JNICALL Agent_OnLoad_agentthr(JavaVM *jvm, char *options, void *reserved) {
 111     return Agent_Initialize(jvm, options, reserved);
 112 }
 113 JNIEXPORT jint JNICALL Agent_OnAttach_agentthr(JavaVM *jvm, char *options, void *reserved) {
 114     return Agent_Initialize(jvm, options, reserved);
 115 }
 116 JNIEXPORT jint JNI_OnLoad_agentthr(JavaVM *jvm, char *options, void *reserved) {
 117     return JNI_VERSION_1_8;
 118 }
 119 #endif
 120 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 121     jvmtiError err;
 122     jint res;
 123 
 124     if (options != NULL && strcmp(options, "printdump") == 0) {
 125         printdump = JNI_TRUE;
 126     }
 127 
 128     jvm_ins = jvm;
 129     res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);

 130     if (res != JNI_OK || jvmti == NULL) {
 131         printf("Wrong result of a valid call to GetEnv!\n");
 132         return JNI_ERR;
 133     }
 134 
 135     callbacks.VMInit = &vm_init;
 136     err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
 137     if (err != JVMTI_ERROR_NONE) {
 138         printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
 139                TranslateError(err), err);
 140         return JNI_ERR;
 141     }
 142 
 143     err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);
 144     if (err != JVMTI_ERROR_NONE) {
 145         printf("Failed to enable JVMTI_EVENT_THREAD_START: %s (%d)\n",
 146                TranslateError(err), err);
 147         return JNI_ERR;
 148     }
 149 
 150     return JNI_OK;
 151 }
 152 
 153 JNIEXPORT jboolean JNICALL
 154 Java_nsk_jvmti_unit_agentthr_isOver(JNIEnv *env, jclass cls) {
 155     return done;
 156 }
 157 
 158 JNIEXPORT jint JNICALL
 159 Java_nsk_jvmti_unit_agentthr_getRes(JNIEnv *env, jclass cls) {
 160     if (!done) {
 161         printf("agent thread has not completed\n");
 162         result = STATUS_FAILED;
 163     }
 164     return result;
 165 }
 166 

 167 }

< prev index next >