1 /*
   2  * Copyright (c) 2008, 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  * Agent receives expected number of ThreadStart/ThreadEnd events and finishes work
  35  * (events should be provoked by target application)
  36  */
  37 
  38 
  39 #define EXPECTED_EVENTS_NUMBER 200
  40 
  41 static Options* options = NULL;
  42 static const char* agentName;
  43 
  44 static jvmtiEvent testEvents[] = {JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END};
  45 static const int testEventsNumber = 2;
  46 
  47 static jrawMonitorID eventsCounterMonitor;
  48 
  49 static volatile int eventsCounter = 0;
  50 
  51 void eventHandler(jvmtiEnv *jvmti,
  52         JNIEnv* jni,
  53         jthread thread,
  54         int threadStartEvent) {
  55     char threadName[MAX_STRING_LENGTH];
  56     int success = 1;
  57     jint threadsCount = 0;
  58     jthread * threads;
  59 
  60     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
  61         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
  62         return;
  63     }
  64 
  65     if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threadsCount, &threads))) {
  66         NSK_COMPLAIN1("%s: failed to get all threads\n", agentName);
  67         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
  68         return;
  69     }
  70 
  71     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
  72 
  73     if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventsCounterMonitor))) {
  74 
  75         eventsCounter++;
  76 
  77         if (threadStartEvent)
  78             NSK_DISPLAY3("%s: ThreadStart event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);
  79         else
  80             NSK_DISPLAY3("%s: ThreadEnd event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);
  81 
  82         if (eventsCounter == EXPECTED_EVENTS_NUMBER) {
  83             NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);
  84 
  85             nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
  86         }
  87 
  88         if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventsCounterMonitor))) {
  89             success = 0;
  90         }
  91     } else {
  92         success = 0;
  93     }
  94 
  95     if (!success) {
  96         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
  97     }
  98 }
  99 
 100 void JNICALL threadStartHandler(
 101         jvmtiEnv *jvmti,
 102         JNIEnv* jni,
 103         jthread thread) {
 104     eventHandler(jvmti, jni, thread, 1);
 105 }
 106 
 107 void JNICALL threadEndHandler(
 108         jvmtiEnv *jvmti,
 109         JNIEnv* jni,
 110         jthread thread) {
 111     eventHandler(jvmti, jni, thread, 0);
 112 }
 113 
 114 #ifdef STATIC_BUILD
 115 JNIEXPORT jint JNI_OnLoad_attach045Agent02(JavaVM *jvm, char *options, void *reserved) {
 116     return JNI_VERSION_1_8;
 117 }
 118 #endif
 119 
 120 JNIEXPORT jint JNICALL
 121 #ifdef STATIC_BUILD
 122 Agent_OnAttach_attach045Agent02(JavaVM *vm, char *optionsString, void *reserved)
 123 #else
 124 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
 125 #endif
 126 {
 127     jvmtiEventCallbacks eventCallbacks;
 128     jvmtiEnv* jvmti;
 129     JNIEnv* jni;
 130 
 131     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
 132         return JNI_ERR;
 133 
 134     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
 135 
 136     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
 137         return JNI_ERR;
 138 
 139     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
 140         return JNI_ERR;
 141 
 142     if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent02-eventsCounterMonitor", &eventsCounterMonitor))) {
 143         return JNI_ERR;
 144     }
 145 
 146     memset(&eventCallbacks,0, sizeof(eventCallbacks));
 147     eventCallbacks.ThreadStart = threadStartHandler;
 148     eventCallbacks.ThreadEnd = threadEndHandler;
 149     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
 150         return JNI_ERR;
 151     }
 152 
 153     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
 154         return JNI_ERR;
 155     }
 156 
 157     NSK_DISPLAY1("%s: initialization was done\n", agentName);
 158 
 159     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
 160         return JNI_ERR;
 161 
 162     return JNI_OK;
 163 }
 164 
 165 
 166 }