Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach045/attach045Agent02.cpp
40951 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22#include <stdio.h>23#include <stdlib.h>24#include <string.h>25#include <jni.h>26#include <jvmti.h>27#include <aod.h>28#include <jvmti_aod.h>2930extern "C" {3132/*33* Agent receives expected number of ThreadStart/ThreadEnd events and finishes work34* (events should be provoked by target application)35*/363738#define EXPECTED_EVENTS_NUMBER 2003940static Options* options = NULL;41static const char* agentName;4243static jvmtiEvent testEvents[] = { JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END };44static const int testEventsNumber = 2;4546static jrawMonitorID eventsCounterMonitor;4748static volatile int eventsCounter = 0;4950void eventHandler(jvmtiEnv *jvmti,51JNIEnv* jni,52jthread thread,53int threadStartEvent) {54char threadName[MAX_STRING_LENGTH];55int success = 1;56jint threadsCount = 0;57jthread * threads;5859if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {60nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);61return;62}6364if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threadsCount, &threads))) {65NSK_COMPLAIN1("%s: failed to get all threads\n", agentName);66nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);67return;68}6970nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);7172if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventsCounterMonitor))) {7374eventsCounter++;7576if (threadStartEvent)77NSK_DISPLAY3("%s: ThreadStart event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);78else79NSK_DISPLAY3("%s: ThreadEnd event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);8081if (eventsCounter == EXPECTED_EVENTS_NUMBER) {82NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);8384nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);85}8687if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventsCounterMonitor))) {88success = 0;89}90} else {91success = 0;92}9394if (!success) {95nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);96}97}9899void JNICALL threadStartHandler(100jvmtiEnv *jvmti,101JNIEnv* jni,102jthread thread) {103eventHandler(jvmti, jni, thread, 1);104}105106void JNICALL threadEndHandler(107jvmtiEnv *jvmti,108JNIEnv* jni,109jthread thread) {110eventHandler(jvmti, jni, thread, 0);111}112113#ifdef STATIC_BUILD114JNIEXPORT jint JNI_OnLoad_attach045Agent02(JavaVM *jvm, char *options, void *reserved) {115return JNI_VERSION_1_8;116}117#endif118119JNIEXPORT jint JNICALL120#ifdef STATIC_BUILD121Agent_OnAttach_attach045Agent02(JavaVM *vm, char *optionsString, void *reserved)122#else123Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)124#endif125{126jvmtiEventCallbacks eventCallbacks;127jvmtiEnv* jvmti;128JNIEnv* jni;129130options = (Options*) nsk_aod_createOptions(optionsString);131if (!NSK_VERIFY(options != NULL))132return JNI_ERR;133134agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);135136jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);137if (jni == NULL)138return JNI_ERR;139140jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);141if (!NSK_VERIFY(jvmti != NULL))142return JNI_ERR;143144if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent02-eventsCounterMonitor", &eventsCounterMonitor))) {145return JNI_ERR;146}147148memset(&eventCallbacks,0, sizeof(eventCallbacks));149eventCallbacks.ThreadStart = threadStartHandler;150eventCallbacks.ThreadEnd = threadEndHandler;151if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {152return JNI_ERR;153}154155if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {156return JNI_ERR;157}158159NSK_DISPLAY1("%s: initialization was done\n", agentName);160161if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))162return JNI_ERR;163164return JNI_OK;165}166167168}169170171