< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach040/attach040Agent00.cpp

Print this page
rev 51942 : [mq]: refactor


  49 
  50 static Options* options = NULL;
  51 static const char* agentName;
  52 
  53 void JNICALL threadStartHandler(jvmtiEnv *jvmti,
  54         JNIEnv* jni,
  55         jthread thread) {
  56     int success = 1;
  57     jint threadsCount = 0;
  58     jthread * threads;
  59     jint i;
  60     char startedThreadName[MAX_STRING_LENGTH];
  61 
  62     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) {
  63         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);
  64         return;
  65     }
  66 
  67     NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName);
  68 
  69     if (NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetAllThreads, jvmti, &threadsCount, &threads))) {
  70         int startedThreadWasFound = 0;
  71 
  72         for (i = 0; i < threadsCount; i++) {
  73             char threadName[MAX_STRING_LENGTH];
  74 
  75             if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
  76                 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);
  77                 return;
  78             }
  79 
  80             if (!strcmp(threadName, startedThreadName)) {
  81                 startedThreadWasFound = 1;
  82                 break;
  83             }
  84         }
  85 
  86         if (!startedThreadWasFound) {
  87             NSK_COMPLAIN2("%s: GetAllThreads didn't return information about thread '%s'\n", agentName, startedThreadName);
  88             success = 0;
  89         }
  90 
  91         nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  92     } else {
  93         success = 0;
  94     }
  95 
  96     if (strstr(startedThreadName, TEST_THREAD_NAME_PREFIX)) {
  97         if (NSK_JVMTI_VERIFY(NSK_CPP_STUB2(
  98                 RawMonitorEnter, jvmti, threadsCounterMonitor))) {
  99 
 100             testThreadsCounter++;
 101 
 102             if (testThreadsCounter == TEST_THREADS_NUMBER) {
 103                 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);
 104             }
 105 
 106             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, threadsCounterMonitor))) {
 107                 success = 0;
 108             }
 109         } else {
 110             success = 0;
 111         }
 112     }
 113 
 114     if (!success) {
 115         NSK_COMPLAIN1("%s: unexpected error during agent work, stop agent\n", agentName);
 116         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);
 117     }
 118 }
 119 
 120 #ifdef STATIC_BUILD
 121 JNIEXPORT jint JNI_OnLoad_attach040Agent00(JavaVM *jvm, char *options, void *reserved) {
 122     return JNI_VERSION_1_8;
 123 }
 124 #endif
 125 
 126 JNIEXPORT jint JNICALL


 128 Agent_OnAttach_attach040Agent00(JavaVM *vm, char *optionsString, void *reserved)
 129 #else
 130 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 131 #endif
 132 {
 133     jvmtiEventCallbacks eventCallbacks;
 134     jvmtiEnv* jvmti;
 135     JNIEnv* jni;
 136 
 137     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 138         return JNI_ERR;
 139 
 140     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 141 
 142     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 143         return JNI_ERR;
 144 
 145     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 146         return JNI_ERR;
 147 
 148     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "threadsCounterMonitor", &threadsCounterMonitor))) {
 149         return JNI_ERR;
 150     }
 151 
 152     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 153     eventCallbacks.ThreadStart = threadStartHandler;
 154     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) {
 155         return JNI_ERR;
 156     }
 157 
 158     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) {
 159         return JNI_ERR;
 160     }
 161 
 162     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 163 
 164     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 165         return JNI_ERR;
 166 
 167     return JNI_OK;
 168 }
 169 
 170 }


  49 
  50 static Options* options = NULL;
  51 static const char* agentName;
  52 
  53 void JNICALL threadStartHandler(jvmtiEnv *jvmti,
  54         JNIEnv* jni,
  55         jthread thread) {
  56     int success = 1;
  57     jint threadsCount = 0;
  58     jthread * threads;
  59     jint i;
  60     char startedThreadName[MAX_STRING_LENGTH];
  61 
  62     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) {
  63         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);
  64         return;
  65     }
  66 
  67     NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName);
  68 
  69     if (NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threadsCount, &threads))) {
  70         int startedThreadWasFound = 0;
  71 
  72         for (i = 0; i < threadsCount; i++) {
  73             char threadName[MAX_STRING_LENGTH];
  74 
  75             if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
  76                 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);
  77                 return;
  78             }
  79 
  80             if (!strcmp(threadName, startedThreadName)) {
  81                 startedThreadWasFound = 1;
  82                 break;
  83             }
  84         }
  85 
  86         if (!startedThreadWasFound) {
  87             NSK_COMPLAIN2("%s: GetAllThreads didn't return information about thread '%s'\n", agentName, startedThreadName);
  88             success = 0;
  89         }
  90 
  91         nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  92     } else {
  93         success = 0;
  94     }
  95 
  96     if (strstr(startedThreadName, TEST_THREAD_NAME_PREFIX)) {
  97         if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(threadsCounterMonitor))) {

  98 
  99             testThreadsCounter++;
 100 
 101             if (testThreadsCounter == TEST_THREADS_NUMBER) {
 102                 nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);
 103             }
 104 
 105             if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(threadsCounterMonitor))) {
 106                 success = 0;
 107             }
 108         } else {
 109             success = 0;
 110         }
 111     }
 112 
 113     if (!success) {
 114         NSK_COMPLAIN1("%s: unexpected error during agent work, stop agent\n", agentName);
 115         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);
 116     }
 117 }
 118 
 119 #ifdef STATIC_BUILD
 120 JNIEXPORT jint JNI_OnLoad_attach040Agent00(JavaVM *jvm, char *options, void *reserved) {
 121     return JNI_VERSION_1_8;
 122 }
 123 #endif
 124 
 125 JNIEXPORT jint JNICALL


 127 Agent_OnAttach_attach040Agent00(JavaVM *vm, char *optionsString, void *reserved)
 128 #else
 129 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 130 #endif
 131 {
 132     jvmtiEventCallbacks eventCallbacks;
 133     jvmtiEnv* jvmti;
 134     JNIEnv* jni;
 135 
 136     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 137         return JNI_ERR;
 138 
 139     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 140 
 141     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 142         return JNI_ERR;
 143 
 144     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 145         return JNI_ERR;
 146 
 147     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("threadsCounterMonitor", &threadsCounterMonitor))) {
 148         return JNI_ERR;
 149     }
 150 
 151     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 152     eventCallbacks.ThreadStart = threadStartHandler;
 153     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) {
 154         return JNI_ERR;
 155     }
 156 
 157     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) {
 158         return JNI_ERR;
 159     }
 160 
 161     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 162 
 163     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 164         return JNI_ERR;
 165 
 166     return JNI_OK;
 167 }
 168 
 169 }
< prev index next >