Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach038/attach038Agent00.cpp
40951 views
/*1* Copyright (c) 2007, 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* Expected agent work scenario:34* - receive ThreadStart event for thread ThreadGeneratingEvents35* - receive ThreadEnd event for thread ThreadGeneratingEvents and finish work36*/3738#define THREAD_GENERATING_EVENTS_NAME "ThreadGeneratingEvents"3940static Options* options = NULL;41static const char* agentName;4243static jvmtiEvent testEvents[] = { JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END };44static const int testEventsNumber = 2;4546static volatile int threadStartReceived = 0;4748void JNICALL threadStartHandler(jvmtiEnv *jvmti,49JNIEnv* jni,50jthread thread) {51char threadName[MAX_STRING_LENGTH];5253if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {54nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);55return;56}5758NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, threadName);5960if (!strcmp(THREAD_GENERATING_EVENTS_NAME, threadName)) {61threadStartReceived = 1;62}63}646566void JNICALL threadEndHandler(jvmtiEnv *jvmti,67JNIEnv* jni,68jthread thread) {69char threadName[MAX_STRING_LENGTH];7071if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {72nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);73return;74}7576NSK_DISPLAY2("%s: ThreadEnd event was received for thread '%s'\n", agentName, threadName);7778if (!strcmp(THREAD_GENERATING_EVENTS_NAME, threadName)) {79int success = 1;8081if (!threadStartReceived) {82NSK_COMPLAIN1("%s: ThreadStart event wasn't received\n", agentName);83success = 0;84}8586nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);87}88}8990#ifdef STATIC_BUILD91JNIEXPORT jint JNI_OnLoad_attach038Agent00(JavaVM *jvm, char *options, void *reserved) {92return JNI_VERSION_1_8;93}94#endif9596JNIEXPORT jint JNICALL97#ifdef STATIC_BUILD98Agent_OnAttach_attach038Agent00(JavaVM *vm, char *optionsString, void *reserved)99#else100Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)101#endif102{103jvmtiEventCallbacks eventCallbacks;104jvmtiEnv* jvmti;105JNIEnv* jni;106107options = (Options*) nsk_aod_createOptions(optionsString);108if (!NSK_VERIFY(options != NULL))109return JNI_ERR;110111agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);112113jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);114if (jni == NULL)115return JNI_ERR;116117jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);118if (!NSK_VERIFY(jvmti != NULL))119return JNI_ERR;120121memset(&eventCallbacks,0, sizeof(eventCallbacks));122eventCallbacks.ThreadEnd = threadEndHandler;123eventCallbacks.ThreadStart = threadStartHandler;124if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {125return JNI_ERR;126}127128if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {129return JNI_ERR;130}131132NSK_DISPLAY1("%s: initialization was done\n", agentName);133134if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))135return JNI_ERR;136137return JNI_OK;138}139140}141142143