Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/VMEvent/libVMEventTest.c
41153 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2018, Google and/or its affiliates. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324#include <string.h>25#include "jvmti.h"2627#ifdef __cplusplus28extern "C" {29#endif3031#ifndef JNI_ENV_ARG3233#ifdef __cplusplus34#define JNI_ENV_ARG(x)35#define JNI_ENV_ARGS2(x, y) y36#define JNI_ENV_ARGS3(x, y, z) y, z37#define JNI_ENV_ARGS4(x, y, z, w) y, z, w38#define JNI_ENV_PTR(x) x39#else40#define JNI_ENV_ARG(x) x41#define JNI_ENV_ARGS2(x,y) x, y42#define JNI_ENV_ARGS3(x, y, z) x, y, z43#define JNI_ENV_ARGS4(x, y, z, w) x, y, z, w44#define JNI_ENV_PTR(x) (*x)45#endif4647#endif4849extern JNIEXPORT void JNICALL VMObjectAlloc(jvmtiEnv *jvmti,50JNIEnv* jni,51jthread thread,52jobject object,53jclass klass,54jlong size) {55char *signature = NULL;56jvmtiError error = (*jvmti)->GetClassSignature(jvmti, klass, &signature, NULL);5758if (error != JVMTI_ERROR_NONE || signature == NULL) {59JNI_ENV_PTR(jni)->FatalError(60JNI_ENV_ARGS2(jni, "Failed during the GetClassSignature call"));61}6263// If it is our test class, call clone now.64if (!strcmp(signature, "LMyPackage/VMEventRecursionTest;")) {65jmethodID clone_method =66JNI_ENV_PTR(jni)->GetMethodID(JNI_ENV_ARGS4(jni, klass, "clone", "()Ljava/lang/Object;"));6768if (JNI_ENV_PTR(jni)->ExceptionOccurred(JNI_ENV_ARG(jni))) {69JNI_ENV_PTR(jni)->FatalError(70JNI_ENV_ARGS2(jni, "Failed during the GetMethodID call"));71}7273JNI_ENV_PTR(jni)->CallObjectMethod(JNI_ENV_ARGS3(jni, object, clone_method));7475if (JNI_ENV_PTR(jni)->ExceptionOccurred(JNI_ENV_ARG(jni))) {76JNI_ENV_PTR(jni)->FatalError(77JNI_ENV_ARGS2(jni, "Failed during the CallObjectMethod call"));78}79}80}8182extern JNIEXPORT void JNICALL OnVMInit(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread) {83(*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, NULL);84}8586extern JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options,87void *reserved) {88jvmtiEnv *jvmti;89jvmtiEventCallbacks callbacks;90jvmtiCapabilities caps;9192if ((*jvm)->GetEnv(jvm, (void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {93return JNI_ERR;94}9596memset(&callbacks, 0, sizeof(callbacks));97callbacks.VMObjectAlloc = &VMObjectAlloc;98callbacks.VMInit = &OnVMInit;99100memset(&caps, 0, sizeof(caps));101caps.can_generate_vm_object_alloc_events = 1;102(*jvmti)->AddCapabilities(jvmti, &caps);103104(*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(jvmtiEventCallbacks));105(*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL);106return 0;107}108109#ifdef __cplusplus110}111#endif112113114