< prev index next >

test/hotspot/jtreg/vmTestbase/nsk/jvmti/SuspendThreadList/suspendthrdlst001/suspendthrdlst001.cpp

Print this page
rev 52185 : [mq]: refactor


  45 /* ============================================================================= */
  46 
  47 static int fillThreadsByName(jvmtiEnv* jvmti, JNIEnv* jni,
  48                                 const char name[], int foundCount, jthread foundThreads[]);
  49 
  50 /** Agent algorithm. */
  51 static void JNICALL
  52 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  53 
  54     NSK_DISPLAY0("Wait for threads to start\n");
  55     if (!nsk_jvmti_waitForSync(timeout))
  56         return;
  57 
  58     /* perform testing */
  59     {
  60         jthread* threads = NULL;
  61         jvmtiError* results = NULL;
  62         int i;
  63 
  64         NSK_DISPLAY1("Allocate threads array: %d threads\n", threadsCount);
  65         if (!NSK_JVMTI_VERIFY(
  66                 NSK_CPP_STUB3(Allocate, jvmti, (threadsCount * sizeof(jthread)),
  67                                                 (unsigned char**)&threads))) {
  68             nsk_jvmti_setFailStatus();
  69             return;
  70         }
  71         NSK_DISPLAY1("  ... allocated array: %p\n", (void*)threads);
  72 
  73         NSK_DISPLAY1("Allocate results array: %d threads\n", threadsCount);
  74         if (!NSK_JVMTI_VERIFY(
  75                 NSK_CPP_STUB3(Allocate, jvmti, (threadsCount * sizeof(jvmtiError)),
  76                                                 (unsigned char**)&results))) {
  77             nsk_jvmti_setFailStatus();
  78             return;
  79         }
  80         NSK_DISPLAY1("  ... allocated array: %p\n", (void*)threads);
  81 
  82         NSK_DISPLAY1("Find threads: %d threads\n", threadsCount);
  83         if (!NSK_VERIFY(fillThreadsByName(jvmti, jni, THREAD_NAME, threadsCount, threads)))
  84             return;
  85 
  86         NSK_DISPLAY0("Suspend threads list\n");
  87         if (!NSK_JVMTI_VERIFY(
  88                 NSK_CPP_STUB4(SuspendThreadList, jvmti, threadsCount, threads, results))) {
  89             nsk_jvmti_setFailStatus();
  90             return;
  91         }
  92 
  93         NSK_DISPLAY0("Check threads results:\n");
  94         for (i = 0; i < threadsCount; i++) {
  95             NSK_DISPLAY3("  ... thread #%d: %s (%d)\n",
  96                                 i, TranslateError(results[i]), (int)results[i]);
  97             if (!NSK_JVMTI_VERIFY(results[i]))
  98                 nsk_jvmti_setFailStatus();
  99         }
 100 
 101         NSK_DISPLAY0("Let threads to run and finish\n");
 102         if (!nsk_jvmti_resumeSync())
 103             return;
 104 
 105         NSK_DISPLAY0("Get state vector for each thread\n");
 106         for (i = 0; i < threadsCount; i++) {
 107             jint state = 0;
 108 
 109             NSK_DISPLAY2("  thread #%d (%p):\n", i, (void*)threads[i]);
 110             if (!NSK_JVMTI_VERIFY(
 111                     NSK_CPP_STUB3(GetThreadState, jvmti, threads[i], &state))) {
 112                 nsk_jvmti_setFailStatus();
 113             }
 114             NSK_DISPLAY2("  ... got state vector: %s (%d)\n",
 115                             TranslateState(state), (int)state);
 116 
 117             if ((state & JVMTI_THREAD_STATE_SUSPENDED) == 0) {
 118                 NSK_COMPLAIN3("SuspendThreadList() does not turn on flag SUSPENDED for thread #%i:\n"
 119                               "#   state: %s (%d)\n",
 120                                 i, TranslateState(state), (int)state);
 121                 nsk_jvmti_setFailStatus();
 122             }
 123         }
 124 
 125         NSK_DISPLAY0("Resume threads list\n");
 126         if (!NSK_JVMTI_VERIFY(
 127                 NSK_CPP_STUB4(ResumeThreadList, jvmti, threadsCount, threads, results))) {
 128             nsk_jvmti_setFailStatus();
 129             return;
 130         }
 131 
 132         NSK_DISPLAY0("Wait for thread to finish\n");
 133         if (!nsk_jvmti_waitForSync(timeout))
 134             return;
 135 
 136         NSK_DISPLAY0("Delete threads references\n");
 137         for (i = 0; i < threadsCount; i++) {
 138             if (threads[i] != NULL)
 139                 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, threads[i]));
 140         }
 141 
 142         NSK_DISPLAY1("Deallocate threads array: %p\n", (void*)threads);
 143         if (!NSK_JVMTI_VERIFY(
 144                 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads))) {
 145             nsk_jvmti_setFailStatus();
 146         }
 147 
 148         NSK_DISPLAY1("Deallocate results array: %p\n", (void*)results);
 149         if (!NSK_JVMTI_VERIFY(
 150                 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)results))) {
 151             nsk_jvmti_setFailStatus();
 152         }
 153     }
 154 
 155     NSK_DISPLAY0("Let debugee to finish\n");
 156     if (!nsk_jvmti_resumeSync())
 157         return;
 158 }
 159 
 160 /* ============================================================================= */
 161 
 162 /** Find threads whose name starts with specified name prefix. */
 163 static int fillThreadsByName(jvmtiEnv* jvmti, JNIEnv* jni,
 164                         const char name[], int foundCount, jthread foundThreads[]) {
 165     jint count = 0;
 166     jthread* threads = NULL;
 167 
 168     size_t len = strlen(name);
 169     int found = 0;
 170     int i;
 171 
 172     for (i = 0; i < foundCount; i++) {
 173         foundThreads[i] = NULL;
 174     }
 175 
 176     if (!NSK_JVMTI_VERIFY(
 177             NSK_CPP_STUB3(GetAllThreads, jvmti, &count, &threads))) {
 178         nsk_jvmti_setFailStatus();
 179         return NSK_FALSE;
 180     }
 181 
 182     found = 0;
 183     for (i = 0; i < count; i++) {
 184         jvmtiThreadInfo info;
 185 
 186         if (!NSK_JVMTI_VERIFY(
 187                 NSK_CPP_STUB3(GetThreadInfo, jvmti, threads[i], &info))) {
 188             nsk_jvmti_setFailStatus();
 189             break;
 190         }
 191 
 192         if (info.name != NULL && strncmp(name, info.name, len) == 0) {
 193             NSK_DISPLAY3("  ... found thread #%d: %p (%s)\n",
 194                                     found, threads[i], info.name);
 195             if (found < foundCount)
 196                 foundThreads[found] = threads[i];
 197             found++;
 198         }
 199 
 200     }
 201 
 202     if (!NSK_JVMTI_VERIFY(
 203                 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)threads))) {
 204         nsk_jvmti_setFailStatus();
 205         return NSK_FALSE;
 206     }
 207 
 208     if (found != foundCount) {
 209         NSK_COMPLAIN3("Unexpected number of tested threads found:\n"
 210                       "#   name:     %s\n"
 211                       "#   found:    %d\n"
 212                       "#   expected: %d\n",
 213                       name, found, foundCount);
 214         nsk_jvmti_setFailStatus();
 215         return NSK_FALSE;
 216     }
 217 
 218     NSK_DISPLAY1("Make global references for threads: %d threads\n", foundCount);
 219     for (i = 0; i < foundCount; i++) {
 220         if (!NSK_JNI_VERIFY(jni, (foundThreads[i] = (jthread)
 221                     NSK_CPP_STUB2(NewGlobalRef, jni, foundThreads[i])) != NULL)) {
 222             nsk_jvmti_setFailStatus();
 223             return NSK_FALSE;
 224         }
 225         NSK_DISPLAY2("  ... thread #%d: %p\n", i, foundThreads[i]);
 226     }
 227 
 228     return NSK_TRUE;
 229 }
 230 
 231 /* ============================================================================= */
 232 
 233 /** Agent library initialization. */
 234 #ifdef STATIC_BUILD
 235 JNIEXPORT jint JNICALL Agent_OnLoad_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) {
 236     return Agent_Initialize(jvm, options, reserved);
 237 }
 238 JNIEXPORT jint JNICALL Agent_OnAttach_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) {
 239     return Agent_Initialize(jvm, options, reserved);
 240 }
 241 JNIEXPORT jint JNI_OnLoad_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) {


 249     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 250         return JNI_ERR;
 251 
 252     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 253 
 254     /* get options */
 255     threadsCount = nsk_jvmti_findOptionIntValue("threads", DEFAULT_THREADS_COUNT);
 256     if (!NSK_VERIFY(threadsCount > 0))
 257         return JNI_ERR;
 258 
 259     /* create JVMTI environment */
 260     if (!NSK_VERIFY((jvmti =
 261             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 262         return JNI_ERR;
 263 
 264     /* add specific capabilities for suspending thread */
 265     {
 266         jvmtiCapabilities suspendCaps;
 267         memset(&suspendCaps, 0, sizeof(suspendCaps));
 268         suspendCaps.can_suspend = 1;
 269         if (!NSK_JVMTI_VERIFY(
 270                 NSK_CPP_STUB2(AddCapabilities, jvmti, &suspendCaps)))
 271             return JNI_ERR;
 272     }
 273 
 274     /* register agent proc and arg */
 275     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 276         return JNI_ERR;
 277 
 278     return JNI_OK;
 279 }
 280 
 281 /* ============================================================================= */
 282 
 283 }


  45 /* ============================================================================= */
  46 
  47 static int fillThreadsByName(jvmtiEnv* jvmti, JNIEnv* jni,
  48                                 const char name[], int foundCount, jthread foundThreads[]);
  49 
  50 /** Agent algorithm. */
  51 static void JNICALL
  52 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  53 
  54     NSK_DISPLAY0("Wait for threads to start\n");
  55     if (!nsk_jvmti_waitForSync(timeout))
  56         return;
  57 
  58     /* perform testing */
  59     {
  60         jthread* threads = NULL;
  61         jvmtiError* results = NULL;
  62         int i;
  63 
  64         NSK_DISPLAY1("Allocate threads array: %d threads\n", threadsCount);
  65         if (!NSK_JVMTI_VERIFY(jvmti->Allocate((threadsCount * sizeof(jthread)),

  66                                               (unsigned char**)&threads))) {
  67             nsk_jvmti_setFailStatus();
  68             return;
  69         }
  70         NSK_DISPLAY1("  ... allocated array: %p\n", (void*)threads);
  71 
  72         NSK_DISPLAY1("Allocate results array: %d threads\n", threadsCount);
  73         if (!NSK_JVMTI_VERIFY(jvmti->Allocate((threadsCount * sizeof(jvmtiError)),

  74                                               (unsigned char**)&results))) {
  75             nsk_jvmti_setFailStatus();
  76             return;
  77         }
  78         NSK_DISPLAY1("  ... allocated array: %p\n", (void*)threads);
  79 
  80         NSK_DISPLAY1("Find threads: %d threads\n", threadsCount);
  81         if (!NSK_VERIFY(fillThreadsByName(jvmti, jni, THREAD_NAME, threadsCount, threads)))
  82             return;
  83 
  84         NSK_DISPLAY0("Suspend threads list\n");
  85         if (!NSK_JVMTI_VERIFY(jvmti->SuspendThreadList(threadsCount, threads, results))) {

  86             nsk_jvmti_setFailStatus();
  87             return;
  88         }
  89 
  90         NSK_DISPLAY0("Check threads results:\n");
  91         for (i = 0; i < threadsCount; i++) {
  92             NSK_DISPLAY3("  ... thread #%d: %s (%d)\n",
  93                                 i, TranslateError(results[i]), (int)results[i]);
  94             if (!NSK_JVMTI_VERIFY(results[i]))
  95                 nsk_jvmti_setFailStatus();
  96         }
  97 
  98         NSK_DISPLAY0("Let threads to run and finish\n");
  99         if (!nsk_jvmti_resumeSync())
 100             return;
 101 
 102         NSK_DISPLAY0("Get state vector for each thread\n");
 103         for (i = 0; i < threadsCount; i++) {
 104             jint state = 0;
 105 
 106             NSK_DISPLAY2("  thread #%d (%p):\n", i, (void*)threads[i]);
 107             if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(threads[i], &state))) {

 108                 nsk_jvmti_setFailStatus();
 109             }
 110             NSK_DISPLAY2("  ... got state vector: %s (%d)\n",
 111                             TranslateState(state), (int)state);
 112 
 113             if ((state & JVMTI_THREAD_STATE_SUSPENDED) == 0) {
 114                 NSK_COMPLAIN3("SuspendThreadList() does not turn on flag SUSPENDED for thread #%i:\n"
 115                               "#   state: %s (%d)\n",
 116                                 i, TranslateState(state), (int)state);
 117                 nsk_jvmti_setFailStatus();
 118             }
 119         }
 120 
 121         NSK_DISPLAY0("Resume threads list\n");
 122         if (!NSK_JVMTI_VERIFY(jvmti->ResumeThreadList(threadsCount, threads, results))) {

 123             nsk_jvmti_setFailStatus();
 124             return;
 125         }
 126 
 127         NSK_DISPLAY0("Wait for thread to finish\n");
 128         if (!nsk_jvmti_waitForSync(timeout))
 129             return;
 130 
 131         NSK_DISPLAY0("Delete threads references\n");
 132         for (i = 0; i < threadsCount; i++) {
 133             if (threads[i] != NULL)
 134                 NSK_TRACE(jni->DeleteGlobalRef(threads[i]));
 135         }
 136 
 137         NSK_DISPLAY1("Deallocate threads array: %p\n", (void*)threads);
 138         if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads))) {

 139             nsk_jvmti_setFailStatus();
 140         }
 141 
 142         NSK_DISPLAY1("Deallocate results array: %p\n", (void*)results);
 143         if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)results))) {

 144             nsk_jvmti_setFailStatus();
 145         }
 146     }
 147 
 148     NSK_DISPLAY0("Let debugee to finish\n");
 149     if (!nsk_jvmti_resumeSync())
 150         return;
 151 }
 152 
 153 /* ============================================================================= */
 154 
 155 /** Find threads whose name starts with specified name prefix. */
 156 static int fillThreadsByName(jvmtiEnv* jvmti, JNIEnv* jni,
 157                         const char name[], int foundCount, jthread foundThreads[]) {
 158     jint count = 0;
 159     jthread* threads = NULL;
 160 
 161     size_t len = strlen(name);
 162     int found = 0;
 163     int i;
 164 
 165     for (i = 0; i < foundCount; i++) {
 166         foundThreads[i] = NULL;
 167     }
 168 
 169     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&count, &threads))) {

 170         nsk_jvmti_setFailStatus();
 171         return NSK_FALSE;
 172     }
 173 
 174     found = 0;
 175     for (i = 0; i < count; i++) {
 176         jvmtiThreadInfo info;
 177 
 178         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadInfo(threads[i], &info))) {

 179             nsk_jvmti_setFailStatus();
 180             break;
 181         }
 182 
 183         if (info.name != NULL && strncmp(name, info.name, len) == 0) {
 184             NSK_DISPLAY3("  ... found thread #%d: %p (%s)\n",
 185                                     found, threads[i], info.name);
 186             if (found < foundCount)
 187                 foundThreads[found] = threads[i];
 188             found++;
 189         }
 190 
 191     }
 192 
 193     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)threads))) {

 194         nsk_jvmti_setFailStatus();
 195         return NSK_FALSE;
 196     }
 197 
 198     if (found != foundCount) {
 199         NSK_COMPLAIN3("Unexpected number of tested threads found:\n"
 200                       "#   name:     %s\n"
 201                       "#   found:    %d\n"
 202                       "#   expected: %d\n",
 203                       name, found, foundCount);
 204         nsk_jvmti_setFailStatus();
 205         return NSK_FALSE;
 206     }
 207 
 208     NSK_DISPLAY1("Make global references for threads: %d threads\n", foundCount);
 209     for (i = 0; i < foundCount; i++) {
 210         if (!NSK_JNI_VERIFY(jni, (foundThreads[i] = (jthread)
 211                     jni->NewGlobalRef(foundThreads[i])) != NULL)) {
 212             nsk_jvmti_setFailStatus();
 213             return NSK_FALSE;
 214         }
 215         NSK_DISPLAY2("  ... thread #%d: %p\n", i, foundThreads[i]);
 216     }
 217 
 218     return NSK_TRUE;
 219 }
 220 
 221 /* ============================================================================= */
 222 
 223 /** Agent library initialization. */
 224 #ifdef STATIC_BUILD
 225 JNIEXPORT jint JNICALL Agent_OnLoad_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) {
 226     return Agent_Initialize(jvm, options, reserved);
 227 }
 228 JNIEXPORT jint JNICALL Agent_OnAttach_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) {
 229     return Agent_Initialize(jvm, options, reserved);
 230 }
 231 JNIEXPORT jint JNI_OnLoad_suspendthrdlst001(JavaVM *jvm, char *options, void *reserved) {


 239     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 240         return JNI_ERR;
 241 
 242     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 243 
 244     /* get options */
 245     threadsCount = nsk_jvmti_findOptionIntValue("threads", DEFAULT_THREADS_COUNT);
 246     if (!NSK_VERIFY(threadsCount > 0))
 247         return JNI_ERR;
 248 
 249     /* create JVMTI environment */
 250     if (!NSK_VERIFY((jvmti =
 251             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 252         return JNI_ERR;
 253 
 254     /* add specific capabilities for suspending thread */
 255     {
 256         jvmtiCapabilities suspendCaps;
 257         memset(&suspendCaps, 0, sizeof(suspendCaps));
 258         suspendCaps.can_suspend = 1;
 259         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&suspendCaps)))

 260             return JNI_ERR;
 261     }
 262 
 263     /* register agent proc and arg */
 264     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 265         return JNI_ERR;
 266 
 267     return JNI_OK;
 268 }
 269 
 270 /* ============================================================================= */
 271 
 272 }
< prev index next >