Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach041/attach041Agent00.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 thread36* - agent receives ThreadStart event and tries to find thread provoked this event37* in all VM thread groups and finishes work38*/3940#define STARTED_TEST_THREAD_NAME "attach041-TestThread"4142static Options* options = NULL;43static const char* agentName;4445int tryFindThread(jvmtiEnv *jvmti, jthreadGroup group, const char* threadNameToFind) {46jint threadsCount = 0;47jthread *threads = NULL;48jint groupsCount = 0;49jthreadGroup* groups = NULL;50jvmtiThreadGroupInfo groupInfo;51int i;52char threadGroupName[MAX_STRING_LENGTH];5354if (!NSK_JVMTI_VERIFY(jvmti->GetThreadGroupInfo(group, &groupInfo))) {55return 0;56}5758strcpy(threadGroupName, groupInfo.name);59nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groupInfo.name);6061NSK_DISPLAY3("%s: trying to find thread '%s' in group '%s'\n", agentName, threadNameToFind, threadGroupName);6263if (!NSK_JVMTI_VERIFY(jvmti->GetThreadGroupChildren(group, &threadsCount, &threads, &groupsCount, &groups))) {64return 0;65}6667for (i = 0; i < threadsCount; i++) {68char threadName[MAX_STRING_LENGTH];69if (!nsk_jvmti_aod_getThreadName(jvmti, threads[i], threadName)) {70NSK_COMPLAIN1("%s: failed to get thread name\n", agentName);71nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);72return 0;73}74if (!strcmp(threadName, threadNameToFind)) {75nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);76nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups);77NSK_DISPLAY3("%s: thread '%s' was found in group '%s'\n", agentName, threadNameToFind, threadGroupName);78return 1;79}80}8182// threads array isn't needed more83nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);8485NSK_DISPLAY3("%s: thread '%s' wasn't found in group '%s'\n", agentName, threadNameToFind, threadGroupName);8687if (groupsCount != 0) {88for (i = 0; i < groupsCount; i++) {89if (tryFindThread(jvmti, groups[i], threadNameToFind)) {90nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups);91return 1;92}93}94}9596nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)groups);9798return 0;99}100101void JNICALL threadStartHandler(jvmtiEnv *jvmti,102JNIEnv* jni,103jthread thread) {104char startedThreadName[MAX_STRING_LENGTH];105106if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) {107nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);108return;109}110111NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName);112113if (!strcmp(startedThreadName, STARTED_TEST_THREAD_NAME)) {114int success = 1;115int threadWasFound = 0;116jint groupsCount = 0;117jthreadGroup *topGroups;118int i;119120if (!NSK_JVMTI_VERIFY(jvmti->GetTopThreadGroups(&groupsCount, &topGroups))) {121NSK_COMPLAIN1("%s: failed to get top thread groups\n", agentName);122nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);123return;124}125126for (i = 0; i < groupsCount; i++) {127if (tryFindThread(jvmti, topGroups[i], startedThreadName)) {128threadWasFound = 1;129break;130}131}132133nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)topGroups);134135if (!threadWasFound) {136success = 0;137NSK_COMPLAIN2("%s: failed to find thread '%s'\n", agentName, startedThreadName);138}139140nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);141}142}143144#ifdef STATIC_BUILD145JNIEXPORT jint JNI_OnLoad_attach041Agent00(JavaVM *jvm, char *options, void *reserved) {146return JNI_VERSION_1_8;147}148#endif149150JNIEXPORT jint JNICALL151#ifdef STATIC_BUILD152Agent_OnAttach_attach041Agent00(JavaVM *vm, char *optionsString, void *reserved)153#else154Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)155#endif156{157jvmtiEventCallbacks eventCallbacks;158jvmtiEnv* jvmti;159JNIEnv* jni;160161options = (Options*) nsk_aod_createOptions(optionsString);162if (!NSK_VERIFY(options != NULL))163return JNI_ERR;164165agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);166167jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);168if (jni == NULL)169return JNI_ERR;170171jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);172if (!NSK_VERIFY(jvmti != NULL))173return JNI_ERR;174175memset(&eventCallbacks,0, sizeof(eventCallbacks));176eventCallbacks.ThreadStart = threadStartHandler;177if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {178return JNI_ERR;179}180181if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) {182return JNI_ERR;183}184185NSK_DISPLAY1("%s: initialization was done\n", agentName);186187if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))188return JNI_ERR;189190return JNI_OK;191}192193}194195196