Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassLoad/classload001/classload001.cpp
40951 views
/*1* Copyright (c) 2003, 2020, 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*/2223#include <stdio.h>24#include <stdarg.h>25#include <stdlib.h>26#include <string.h>2728#include <jvmti.h>29#include "agent_common.h"3031#include "nsk_tools.h"32#include "JVMTITools.h"33#include "jvmti_tools.h"3435extern "C" {3637#define PASSED 038#define STATUS_FAILED 23940/* classes which must have the class load event */41static const char *expSigs[] = {42"Lnsk/jvmti/ClassLoad/classload001;",43"Lnsk/jvmti/ClassLoad/classload001$TestedClass;"44};45#define EXP_SIG_NUM (sizeof(expSigs)/sizeof(char*))4647/* classes which must not have the class load event */48static const char *unexpSigs[] = {49"Z", /* boolean */50"B", /* byte */51"C", /* char */52"D", /* double */53"F", /* float */54"I", /* integer */55"J", /* long */56"S", /* short */5758"[Z", /* boolean array */59"[B", /* byte array */60"[C", /* char array */61"[D", /* double array */62"[F", /* float array */63"[I", /* integer array */64"[J", /* long array */65"[S", /* short array */66"[Lnsk/jvmti/ClassLoad/classload001$TestedClass;"67};68#define UNEXP_SIG_NUM (sizeof(unexpSigs)/sizeof(char*))6970static volatile int clsEvents[EXP_SIG_NUM];71static volatile int primClsEvents[UNEXP_SIG_NUM];7273static jint result = PASSED;74static jvmtiEnv *jvmti = NULL;75static jvmtiEventCallbacks callbacks;76static jrawMonitorID countLock;7778static void initCounters() {79size_t i;8081for (i=0; i<EXP_SIG_NUM; i++)82clsEvents[i] = 0;8384for (i=0; i<UNEXP_SIG_NUM; i++)85primClsEvents[i] = 0;86}8788static int findSig(char *sig, int expected) {89unsigned int i;9091for (i=0; i<((expected == 1) ? EXP_SIG_NUM : UNEXP_SIG_NUM); i++)92if (sig != NULL &&93strcmp(((expected == 1) ? expSigs[i] : unexpSigs[i]), sig) == 0)94return i; /* the signature found, return index */9596return -1; /* the signature not found */97}9899static void lock(jvmtiEnv *jvmti_env, JNIEnv *jni_env) {100if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorEnter(countLock)))101jni_env->FatalError("failed to enter a raw monitor\n");102}103104static void unlock(jvmtiEnv *jvmti_env, JNIEnv *jni_env) {105if (!NSK_JVMTI_VERIFY(jvmti_env->RawMonitorExit(countLock)))106jni_env->FatalError("failed to exit a raw monitor\n");107}108109/** callback functions **/110void JNICALL111ClassLoad(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread, jclass klass) {112int i = 0;113char *sig, *generic;114115lock(jvmti_env, env);116117if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &sig, &generic))) {118result = STATUS_FAILED;119NSK_COMPLAIN0("TEST FAILURE: unable to obtain a class signature\n");120unlock(jvmti_env, env);121return;122}123124i = findSig(sig, 1);125if (i != -1) {126clsEvents[i]++;127NSK_DISPLAY1("CHECK PASSED: ClassLoad event received for the class \"%s\" as expected\n",128sig);129}130else {131i = findSig(sig, 0);132if (i != -1) {133result = STATUS_FAILED;134primClsEvents[i]++;135NSK_COMPLAIN1(136"TEST FAILED: JVMTI_EVENT_CLASS_LOAD event received for\n"137"\t a primitive class/array of primitive types with the signature \"%s\"\n",138sig);139}140}141142unlock(jvmti_env, env);143}144/************************/145146JNIEXPORT jint JNICALL147Java_nsk_jvmti_ClassLoad_classload001_check(148JNIEnv *env, jobject obj) {149size_t i;150151for (i=0; i<EXP_SIG_NUM; i++)152if (clsEvents[i] != 1) {153result = STATUS_FAILED;154NSK_COMPLAIN2("TEST FAILED: wrong number of JVMTI_EVENT_CLASS_LOAD events for \"%s\":\n\tgot: %d\texpected: 1\n",155expSigs[i], clsEvents[i]);156}157158for (i=0; i<UNEXP_SIG_NUM; i++)159if (primClsEvents[i] != 0)160NSK_COMPLAIN0("TEST FAILED: there are JVMTI_EVENT_CLASS_LOAD events for the primitive classes\n");161162return result;163}164165#ifdef STATIC_BUILD166JNIEXPORT jint JNICALL Agent_OnLoad_classload001(JavaVM *jvm, char *options, void *reserved) {167return Agent_Initialize(jvm, options, reserved);168}169JNIEXPORT jint JNICALL Agent_OnAttach_classload001(JavaVM *jvm, char *options, void *reserved) {170return Agent_Initialize(jvm, options, reserved);171}172JNIEXPORT jint JNI_OnLoad_classload001(JavaVM *jvm, char *options, void *reserved) {173return JNI_VERSION_1_8;174}175#endif176jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {177/* init framework and parse options */178if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))179return JNI_ERR;180181/* create JVMTI environment */182if (!NSK_VERIFY((jvmti =183nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))184return JNI_ERR;185186initCounters();187188if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_counter_lock", &countLock)))189return JNI_ERR;190191NSK_DISPLAY0("setting event callbacks ...\n");192(void) memset(&callbacks, 0, sizeof(callbacks));193callbacks.ClassLoad = &ClassLoad;194if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))195return JNI_ERR;196197NSK_DISPLAY0("setting event callbacks done\nenabling ClassLoad event ...\n");198if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL)))199return JNI_ERR;200NSK_DISPLAY0("the event enabled\n");201202return JNI_OK;203}204205}206207208