1 /*
   2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  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 #include <stdio.h>
  24 #include <stdlib.h>
  25 #include <string.h>
  26 #include <jni.h>
  27 #include <jvmti.h>
  28 #include <aod.h>
  29 #include <jvmti_aod.h>
  30 
  31 extern "C" {
  32 
  33 /*
  34  * Expected agent work scenario:
  35  *  - during initialization agent enables ThreadStart events
  36  *  - target application starts several threads
  37  *  - agent receives ThreadStart events and tries to find thread provoked this event in the array returned
  38  *    by JVMTI function GetAllThreads
  39  *  - when expected number of ThreadStart events is received agent finishes work
  40  */
  41 
  42 #define TEST_THREADS_NUMBER 5
  43 
  44 #define TEST_THREAD_NAME_PREFIX "attach040-TestThread-"
  45 
  46 static jrawMonitorID threadsCounterMonitor;
  47 
  48 static volatile int testThreadsCounter = 0;
  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
 126 #ifdef STATIC_BUILD
 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 }