Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetEnv/GetEnv001/GetEnv001.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*/2223#include <stdio.h>24#include <string.h>25#include <stdlib.h>26#include <jvmti.h>27#include "agent_common.h"28#include <jvmti_tools.h>29#include "JVMTITools.h"3031extern "C" {3233/* ============================================================================= */3435static int loaded_classes_num = 0;3637/* ============================================================================= */38static void JNICALL39ClassFileLoadHook (40jvmtiEnv *jvmti41, JNIEnv *jni42, jclass class_being_redefined43, jobject loader44, const char* name45, jobject protection_domain46, jint class_data_len47, const unsigned char* class_data48, jint *new_class_data_len49, unsigned char** new_class_data50)51{52loaded_classes_num++;5354NSK_DISPLAY2("ClassFileLoadHook: class \"%s\", loader %X\n"55, name56, loader57);58}5960/* ============================================================================= */61JNIEXPORT jint JNICALL62Java_nsk_jvmti_GetEnv_GetEnv001_GetEnv001_getLoadedClassesCount(JNIEnv *env, jobject owner) {63return loaded_classes_num;64}6566/* ============================================================================= */67/* Agent initialization procedure */68#ifdef STATIC_BUILD69JNIEXPORT jint JNICALL Agent_OnLoad_GetEnv001(JavaVM *jvm, char *options, void *reserved) {70return Agent_Initialize(jvm, options, reserved);71}72JNIEXPORT jint JNICALL Agent_OnAttach_GetEnv001(JavaVM *jvm, char *options, void *reserved) {73return Agent_Initialize(jvm, options, reserved);74}75JNIEXPORT jint JNI_OnLoad_GetEnv001(JavaVM *jvm, char *options, void *reserved) {76return JNI_VERSION_1_8;77}78#endif79jint Agent_Initialize(JavaVM *vm, char *options, void *reserved)80{81jvmtiEventCallbacks callbacks;82jvmtiCapabilities caps;83jvmtiEnv* jvmti;8485if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) {86return JNI_ERR;87}8889if (vm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1) != JNI_OK || jvmti == NULL) {90NSK_COMPLAIN0("JVMTI_VERSION_1_1 isn't supported.");91return JNI_OK;92}9394if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))95return JNI_ERR;9697caps.can_retransform_classes = 1;9899// Register all necessary JVM capabilities100if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))101return JNI_ERR;102103// Register all necessary event callbacks104memset(&callbacks, 0, sizeof(callbacks));105callbacks.ClassFileLoadHook = &ClassFileLoadHook;106107if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))108return JNI_ERR;109110// Enable class retransformation111if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))112return JNI_ERR;113114return JNI_OK;115}116117}118119120