Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach042/attach042Agent00.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 for this thread and tries to call GetThreadState for37* all VM threads, then finishes work38*/3940#define STARTED_TEST_THREAD_NAME "attach042-TestThread"4142static Options* options = NULL;43static const char* agentName;4445void JNICALL threadStartHandler(jvmtiEnv *jvmti,46JNIEnv* jni,47jthread thread) {48char startedThreadName[MAX_STRING_LENGTH];4950if (!nsk_jvmti_aod_getThreadName(jvmti, thread, startedThreadName)) {51nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);52return;53}5455NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, startedThreadName);5657if (!strcmp(startedThreadName, STARTED_TEST_THREAD_NAME)) {58int success = 1;59jint threadsCount = 0;60jthread* threads = NULL;61int i;62int startedThreadWasFound = 0;6364if (!NSK_JVMTI_VERIFY(jvmti->GetAllThreads(&threadsCount, &threads))) {65NSK_COMPLAIN1("%s: failed to get all threads\n", agentName);66nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);67return;68}6970NSK_DISPLAY1("%s: displaying threads status:\n", agentName);7172for (i = 0; i < threadsCount; i++) {73jint threadState;74char threadName[MAX_STRING_LENGTH];7576if (!nsk_jvmti_aod_getThreadName(jvmti, threads[i], threadName)) {77NSK_COMPLAIN1("%s: failed to get thread name\n", agentName);78nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);79nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);80return;81}8283if (!strcmp(threadName, startedThreadName)) {84startedThreadWasFound = 1;85}8687if (!NSK_JVMTI_VERIFY(jvmti->GetThreadState(threads[i], &threadState))) {88NSK_COMPLAIN2("%s: failed to get status of thread '%s'\n", agentName, threadName);89nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);90nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, 0, jvmti, jni);91return;92}9394NSK_DISPLAY3("%s: status of '%s': %s\n", agentName, threadName, TranslateState(threadState));95}9697nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);9899if (!startedThreadWasFound) {100NSK_COMPLAIN2("%s: thread '%s' wasn't returned by GetAllThreads\n", agentName, startedThreadName);101success = 0;102}103104nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_THREAD_START, success, jvmti, jni);105}106}107108#ifdef STATIC_BUILD109JNIEXPORT jint JNI_OnLoad_attach042Agent00(JavaVM *jvm, char *options, void *reserved) {110return JNI_VERSION_1_8;111}112#endif113114JNIEXPORT jint JNICALL115#ifdef STATIC_BUILD116Agent_OnAttach_attach042Agent00(JavaVM *vm, char *optionsString, void *reserved)117#else118Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)119#endif120{121jvmtiEventCallbacks eventCallbacks;122jvmtiEnv* jvmti;123JNIEnv* jni;124125options = (Options*) nsk_aod_createOptions(optionsString);126if (!NSK_VERIFY(options != NULL))127return JNI_ERR;128129agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);130131jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);132if (jni == NULL)133return JNI_ERR;134135jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);136if (!NSK_VERIFY(jvmti != NULL))137return JNI_ERR;138139memset(&eventCallbacks,0, sizeof(eventCallbacks));140eventCallbacks.ThreadStart = threadStartHandler;141if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {142return JNI_ERR;143}144145if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_THREAD_START))) {146return JNI_ERR;147}148149NSK_DISPLAY1("%s: initialization was done\n", agentName);150151if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))152return JNI_ERR;153154return JNI_OK;155}156157}158159160