Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach040/attach040Agent00.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 events35* - target application starts several threads36* - agent receives ThreadStart events and tries to find thread provoked this event in the array returned37* by JVMTI function GetAllThreads38* - when expected number of ThreadStart events is received agent finishes work39*/4041#define TEST_THREADS_NUMBER 54243#define TEST_THREAD_NAME_PREFIX "attach040-TestThread-"4445static jrawMonitorID threadsCounterMonitor;4647static volatile int testThreadsCounter = 0;4849static Options* options = NULL;50static const char* agentName;5152void JNICALL threadStartHandler(jvmtiEnv *jvmti,53JNIEnv* jni,54jthread thread) {55int success = 1;56jint threadsCount = 0;57jthread * threads;58jint i;59char startedThreadName[MAX_STRING_LENGTH];6061if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) {62nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);63return;64}6566NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName);6768if (NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threadsCount, &threads))) {69int startedThreadWasFound = 0;7071for (i = 0; i < threadsCount; i++) {72char threadName[MAX_STRING_LENGTH];7374if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {75nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);76return;77}7879if (!strcmp(threadName, startedThreadName)) {80startedThreadWasFound = 1;81break;82}83}8485if (!startedThreadWasFound) {86NSK_COMPLAIN2("%s: GetAllThreads didn't return information about thread '%s'\n", agentName, startedThreadName);87success = 0;88}8990nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);91} else {92success = 0;93}9495if (strstr(startedThreadName, TEST_THREAD_NAME_PREFIX)) {96if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(threadsCounterMonitor))) {9798testThreadsCounter++;99100if (testThreadsCounter == TEST_THREADS_NUMBER) {101nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);102}103104if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(threadsCounterMonitor))) {105success = 0;106}107} else {108success = 0;109}110}111112if (!success) {113NSK_COMPLAIN1("%s: unexpected error during agent work, stop agent\n", agentName);114nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);115}116}117118#ifdef STATIC_BUILD119JNIEXPORT jint JNI_OnLoad_attach040Agent00(JavaVM *jvm, char *options, void *reserved) {120return JNI_VERSION_1_8;121}122#endif123124JNIEXPORT jint JNICALL125#ifdef STATIC_BUILD126Agent_OnAttach_attach040Agent00(JavaVM *vm, char *optionsString, void *reserved)127#else128Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)129#endif130{131jvmtiEventCallbacks eventCallbacks;132jvmtiEnv* jvmti;133JNIEnv* jni;134135options = (Options*) nsk_aod_createOptions(optionsString);136if (!NSK_VERIFY(options != NULL))137return JNI_ERR;138139agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);140141jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);142if (jni == NULL)143return JNI_ERR;144145jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);146if (!NSK_VERIFY(jvmti != NULL))147return JNI_ERR;148149if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("threadsCounterMonitor", &threadsCounterMonitor))) {150return JNI_ERR;151}152153memset(&eventCallbacks,0, sizeof(eventCallbacks));154eventCallbacks.ThreadStart = threadStartHandler;155if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {156return JNI_ERR;157}158159if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) {160return JNI_ERR;161}162163NSK_DISPLAY1("%s: initialization was done\n", agentName);164165if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))166return JNI_ERR;167168return JNI_OK;169}170171}172173174