Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach039/attach039Agent00.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* - during initialization agent enables ThreadStart and ThreadEnd events and starts new35* thread using JVMTI RunAgentThread36* - agent receives ThreadStart event for started thread37- agent receives ThreadEnd event for started thread and finishes work38*/3940#define STARTED_THREAD_NAME "ThreadStartedByAgent"4142static Options* options = NULL;43static const char* agentName;4445static jvmtiEvent testEvents[] = { JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END };46static const int testEventsNumber = 2;4748volatile int threadStartReceived = 0;49volatile int threadWasExecuted = 0;5051void JNICALL startedThreadFunction(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {52char threadName[MAX_STRING_LENGTH];5354if (!nsk_jvmti_aod_getThreadName(jvmti, NULL, threadName)) {55nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);56return;57}5859NSK_DISPLAY2("%s: thread started from agent is running (thread name: '%s')\n", agentName, threadName);6061threadWasExecuted = 1;62}6364int startNewThread(jvmtiEnv* jvmti, JNIEnv* jni) {65jthread thread;6667thread = nsk_jvmti_aod_createThreadWithName(jni, STARTED_THREAD_NAME);68if (!NSK_VERIFY(thread != NULL))69return NSK_FALSE;7071if (!NSK_JVMTI_VERIFY(jvmti->RunAgentThread(thread, startedThreadFunction, NULL, JVMTI_THREAD_NORM_PRIORITY))) {72return NSK_FALSE;73}7475NSK_DISPLAY1("%s: new thread was started\n", agentName);7677return NSK_TRUE;78}7980void JNICALL threadEndHandler(jvmtiEnv *jvmti,81JNIEnv* jni,82jthread thread) {83char threadName[MAX_STRING_LENGTH];8485if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {86nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);87return;88}8990NSK_DISPLAY2("%s: ThreadEnd event was received for thread '%s'\n", agentName, threadName);9192if (!strcmp(STARTED_THREAD_NAME, threadName)) {93int success = 1;9495if (!threadStartReceived) {96NSK_COMPLAIN1("%s: ThreadStart event wasn't received\n", agentName);97success = 0;98}99100if (!threadWasExecuted) {101NSK_COMPLAIN1("%s: started thread haven't execute its code\n", agentName);102success = 0;103}104105nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);106}107}108109void JNICALL threadStartHandler(jvmtiEnv *jvmti,110JNIEnv* jni,111jthread thread) {112char threadName[MAX_STRING_LENGTH];113114if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {115nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);116return;117}118119NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, threadName);120121if (!strcmp(STARTED_THREAD_NAME, threadName)) {122threadStartReceived = 1;123}124}125126#ifdef STATIC_BUILD127JNIEXPORT jint JNI_OnLoad_attach039Agent00(JavaVM *jvm, char *options, void *reserved) {128return JNI_VERSION_1_8;129}130#endif131132JNIEXPORT jint JNICALL133#ifdef STATIC_BUILD134Agent_OnAttach_attach039Agent00(JavaVM *vm, char *optionsString, void *reserved)135#else136Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)137#endif138{139jvmtiEventCallbacks eventCallbacks;140jvmtiEnv* jvmti;141JNIEnv* jni;142143options = (Options*) nsk_aod_createOptions(optionsString);144if (!NSK_VERIFY(options != NULL))145return JNI_ERR;146147agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);148149jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);150if (jni == NULL)151return JNI_ERR;152153jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);154if (!NSK_VERIFY(jvmti != NULL))155return JNI_ERR;156157memset(&eventCallbacks,0, sizeof(eventCallbacks));158eventCallbacks.ThreadEnd = threadEndHandler;159eventCallbacks.ThreadStart = threadStartHandler;160if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {161return JNI_ERR;162}163164if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {165return JNI_ERR;166}167168NSK_DISPLAY1("%s: initialization was done\n", agentName);169170if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))171return JNI_ERR;172173NSK_DISPLAY1("%s: starting new thread\n", agentName);174175if (!NSK_VERIFY(startNewThread(jvmti, jni)))176return JNI_ERR;177178return JNI_OK;179}180181}182183184