Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk001/classfloadhk001.cpp
40955 views
/*1* Copyright (c) 2003, 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*/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 23940const char* CLASS_NAME = "nsk/jvmti/ClassFileLoadHook/classfloadhk001";4142static jint result = STATUS_FAILED;43static jvmtiEnv *jvmti = NULL;44static jvmtiEventCallbacks callbacks;4546/** callback functions **/47void JNICALL48ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *env, jclass class_beeing_redefined,49jobject loader, const char* name, jobject protection_domain,50jint class_data_len, const unsigned char* class_data,51jint *new_class_data_len, unsigned char** new_class_data) {5253if (name != NULL && (strcmp(name, CLASS_NAME) == 0)) {54NSK_DISPLAY1("CHECK PASSED: ClassFileLoadHook event received for the class \"%s\" as expected\n",55name);56result = PASSED;57}58}59/************************/6061JNIEXPORT jint JNICALL62Java_nsk_jvmti_ClassFileLoadHook_classfloadhk001_check(63JNIEnv *env, jobject obj) {64if (result == STATUS_FAILED)65NSK_COMPLAIN1("TEST FAILED: no JVMTI_EVENT_CLASS_FILE_LOAD_HOOK event for the class \"%s\"\n",66CLASS_NAME);6768return result;69}7071#ifdef STATIC_BUILD72JNIEXPORT jint JNICALL Agent_OnLoad_classfloadhk001(JavaVM *jvm, char *options, void *reserved) {73return Agent_Initialize(jvm, options, reserved);74}75JNIEXPORT jint JNICALL Agent_OnAttach_classfloadhk001(JavaVM *jvm, char *options, void *reserved) {76return Agent_Initialize(jvm, options, reserved);77}78JNIEXPORT jint JNI_OnLoad_classfloadhk001(JavaVM *jvm, char *options, void *reserved) {79return JNI_VERSION_1_8;80}81#endif82jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {83/* init framework and parse options */84if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))85return JNI_ERR;8687/* create JVMTI environment */88if (!NSK_VERIFY((jvmti =89nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))90return JNI_ERR;9192NSK_DISPLAY0("setting event callbacks ...\n");93(void) memset(&callbacks, 0, sizeof(callbacks));94callbacks.ClassFileLoadHook = &ClassFileLoadHook;95if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))96return JNI_ERR;9798NSK_DISPLAY0("setting event callbacks done\nenabling ClassFileLoadHook event ...\n");99if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))100return JNI_ERR;101NSK_DISPLAY0("enabling ClassFileLoadHook event done\n");102103return JNI_OK;104}105106}107108109