< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach041/attach041Agent00.cpp

Print this page
rev 51942 : [mq]: refactor


  35  *  - during initialization agent enables ThreadStart events
  36  *  - target application starts thread
  37  *  - agent receives ThreadStart event and tries to find thread provoked this event
  38  *    in all VM thread groups and finishes work
  39  */
  40 
  41 #define STARTED_TEST_THREAD_NAME "attach041-TestThread"
  42 
  43 static Options* options = NULL;
  44 static const char* agentName;
  45 
  46 int tryFindThread(jvmtiEnv *jvmti, jthreadGroup group, const char* threadNameToFind) {
  47     jint threadsCount = 0;
  48     jthread *threads = NULL;
  49     jint groupsCount = 0;
  50     jthreadGroup* groups = NULL;
  51     jvmtiThreadGroupInfo groupInfo;
  52     int i;
  53     char threadGroupName[MAX_STRING_LENGTH];
  54 
  55     if(!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetThreadGroupInfo, jvmti, group, &groupInfo))) {
  56         return 0;
  57     }
  58 
  59     strcpy(threadGroupName, groupInfo.name);
  60     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groupInfo.name);
  61 
  62     NSK_DISPLAY3("%s: trying to find thread '%s' in group '%s'\n", agentName, threadNameToFind, threadGroupName);
  63 
  64     if(!NSK_JVMTI_VERIFY(NSK_CPP_STUB6(GetThreadGroupChildren, jvmti, group, &threadsCount, &threads, &groupsCount, &groups))) {
  65         return 0;
  66     }
  67 
  68     for (i = 0; i < threadsCount; i++) {
  69         char threadName[MAX_STRING_LENGTH];
  70         if (!nsk_jvmti_aod_getThreadName(jvmti, threads[i], threadName)) {
  71             NSK_COMPLAIN1("%s: failed to get thread name\n", agentName);
  72             nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  73             return 0;
  74         }
  75         if (!strcmp(threadName, threadNameToFind)) {
  76             nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  77             nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups);
  78             NSK_DISPLAY3("%s: thread '%s' was found in group '%s'\n", agentName, threadNameToFind, threadGroupName);
  79             return 1;
  80         }
  81     }
  82 
  83     // threads array isn't needed more
  84     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);


 101 
 102 void JNICALL threadStartHandler(jvmtiEnv *jvmti,
 103         JNIEnv* jni,
 104         jthread thread) {
 105     char startedThreadName[MAX_STRING_LENGTH];
 106 
 107     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) {
 108         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);
 109         return;
 110     }
 111 
 112     NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName);
 113 
 114     if (!strcmp(startedThreadName, STARTED_TEST_THREAD_NAME)) {
 115         int success = 1;
 116         int threadWasFound = 0;
 117         jint groupsCount = 0;
 118         jthreadGroup *topGroups;
 119         int i;
 120 
 121         if(!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTopThreadGroups, jvmti, &groupsCount, &topGroups))) {
 122             NSK_COMPLAIN1("%s: failed to get top thread groups\n", agentName);
 123             nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);
 124             return;
 125         }
 126 
 127         for (i = 0; i < groupsCount; i++) {
 128             if (tryFindThread(jvmti, topGroups[i], startedThreadName)) {
 129                 threadWasFound = 1;
 130                 break;
 131             }
 132         }
 133 
 134         nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)topGroups);
 135 
 136         if (!threadWasFound) {
 137             success = 0;
 138             NSK_COMPLAIN2("%s: failed to find thread '%s'\n", agentName, startedThreadName);
 139         }
 140 
 141         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);


 155 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 156 #endif
 157 {
 158     jvmtiEventCallbacks eventCallbacks;
 159     jvmtiEnv* jvmti;
 160     JNIEnv* jni;
 161 
 162     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 163         return JNI_ERR;
 164 
 165     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 166 
 167     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 168         return JNI_ERR;
 169 
 170     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 171         return JNI_ERR;
 172 
 173     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 174     eventCallbacks.ThreadStart = threadStartHandler;
 175     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) {
 176         return JNI_ERR;
 177     }
 178 
 179     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) {
 180         return JNI_ERR;
 181     }
 182 
 183     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 184 
 185     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 186         return JNI_ERR;
 187 
 188     return JNI_OK;
 189 }
 190 
 191 }


  35  *  - during initialization agent enables ThreadStart events
  36  *  - target application starts thread
  37  *  - agent receives ThreadStart event and tries to find thread provoked this event
  38  *    in all VM thread groups and finishes work
  39  */
  40 
  41 #define STARTED_TEST_THREAD_NAME "attach041-TestThread"
  42 
  43 static Options* options = NULL;
  44 static const char* agentName;
  45 
  46 int tryFindThread(jvmtiEnv *jvmti, jthreadGroup group, const char* threadNameToFind) {
  47     jint threadsCount = 0;
  48     jthread *threads = NULL;
  49     jint groupsCount = 0;
  50     jthreadGroup* groups = NULL;
  51     jvmtiThreadGroupInfo groupInfo;
  52     int i;
  53     char threadGroupName[MAX_STRING_LENGTH];
  54 
  55     if(!NSK_JVMTI_VERIFY(jvmti->GetThreadGroupInfo(group, &groupInfo))) {
  56         return 0;
  57     }
  58 
  59     strcpy(threadGroupName, groupInfo.name);
  60     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groupInfo.name);
  61 
  62     NSK_DISPLAY3("%s: trying to find thread '%s' in group '%s'\n", agentName, threadNameToFind, threadGroupName);
  63 
  64     if(!NSK_JVMTI_VERIFY(jvmti->GetThreadGroupChildren(group, &threadsCount, &threads, &groupsCount, &groups))) {
  65         return 0;
  66     }
  67 
  68     for (i = 0; i < threadsCount; i++) {
  69         char threadName[MAX_STRING_LENGTH];
  70         if (!nsk_jvmti_aod_getThreadName(jvmti, threads[i], threadName)) {
  71             NSK_COMPLAIN1("%s: failed to get thread name\n", agentName);
  72             nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  73             return 0;
  74         }
  75         if (!strcmp(threadName, threadNameToFind)) {
  76             nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  77             nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups);
  78             NSK_DISPLAY3("%s: thread '%s' was found in group '%s'\n", agentName, threadNameToFind, threadGroupName);
  79             return 1;
  80         }
  81     }
  82 
  83     // threads array isn't needed more
  84     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);


 101 
 102 void JNICALL threadStartHandler(jvmtiEnv *jvmti,
 103         JNIEnv* jni,
 104         jthread thread) {
 105     char startedThreadName[MAX_STRING_LENGTH];
 106 
 107     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) {
 108         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);
 109         return;
 110     }
 111 
 112     NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName);
 113 
 114     if (!strcmp(startedThreadName, STARTED_TEST_THREAD_NAME)) {
 115         int success = 1;
 116         int threadWasFound = 0;
 117         jint groupsCount = 0;
 118         jthreadGroup *topGroups;
 119         int i;
 120 
 121         if(!NSK_JVMTI_VERIFY(jvmti->GetTopThreadGroups(&groupsCount, &topGroups))) {
 122             NSK_COMPLAIN1("%s: failed to get top thread groups\n", agentName);
 123             nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);
 124             return;
 125         }
 126 
 127         for (i = 0; i < groupsCount; i++) {
 128             if (tryFindThread(jvmti, topGroups[i], startedThreadName)) {
 129                 threadWasFound = 1;
 130                 break;
 131             }
 132         }
 133 
 134         nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)topGroups);
 135 
 136         if (!threadWasFound) {
 137             success = 0;
 138             NSK_COMPLAIN2("%s: failed to find thread '%s'\n", agentName, startedThreadName);
 139         }
 140 
 141         nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);


 155 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 156 #endif
 157 {
 158     jvmtiEventCallbacks eventCallbacks;
 159     jvmtiEnv* jvmti;
 160     JNIEnv* jni;
 161 
 162     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 163         return JNI_ERR;
 164 
 165     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 166 
 167     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 168         return JNI_ERR;
 169 
 170     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 171         return JNI_ERR;
 172 
 173     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 174     eventCallbacks.ThreadStart = threadStartHandler;
 175     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks))) ) {
 176         return JNI_ERR;
 177     }
 178 
 179     if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) {
 180         return JNI_ERR;
 181     }
 182 
 183     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 184 
 185     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 186         return JNI_ERR;
 187 
 188     return JNI_OK;
 189 }
 190 
 191 }
< prev index next >