|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle 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 it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 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 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +#include <string.h> |
| 25 | +#include "jvmti.h" |
| 26 | + |
| 27 | +extern "C" { |
| 28 | + |
| 29 | +static int number_of_allocation = 0; |
| 30 | + |
| 31 | +extern JNIEXPORT void JNICALL |
| 32 | +VMObjectAlloc(jvmtiEnv *jvmti, |
| 33 | + JNIEnv* jni, |
| 34 | + jthread thread, |
| 35 | + jobject object, |
| 36 | + jclass cls, |
| 37 | + jlong size) { |
| 38 | + char *signature = NULL; |
| 39 | + jvmtiError err = jvmti->GetClassSignature(cls, &signature, NULL); |
| 40 | + if (err != JVMTI_ERROR_NONE) { |
| 41 | + jni->FatalError("Failed during the GetClassSignature call"); |
| 42 | + } |
| 43 | + |
| 44 | + printf("VMObjectAlloc called for %s\n", signature); |
| 45 | + |
| 46 | + if (!strcmp(signature, "LVMObjectAllocTest;")) { |
| 47 | + number_of_allocation++; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | + |
| 52 | +JNIEXPORT jint JNICALL |
| 53 | +Java_VMObjectAllocTest_getNumberOfAllocation(JNIEnv *env, jclass cls) { |
| 54 | + return number_of_allocation; |
| 55 | +} |
| 56 | + |
| 57 | +extern JNIEXPORT jint JNICALL |
| 58 | +Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { |
| 59 | + jvmtiEnv *jvmti; |
| 60 | + jvmtiEventCallbacks callbacks; |
| 61 | + jvmtiError err; |
| 62 | + jvmtiCapabilities caps; |
| 63 | + |
| 64 | + if (jvm->GetEnv((void **) &jvmti, JVMTI_VERSION) != JNI_OK) { |
| 65 | + return JNI_ERR; |
| 66 | + } |
| 67 | + |
| 68 | + memset(&callbacks, 0, sizeof(callbacks)); |
| 69 | + callbacks.VMObjectAlloc = &VMObjectAlloc; |
| 70 | + memset(&caps, 0, sizeof(caps)); |
| 71 | + caps.can_generate_vm_object_alloc_events = 1; |
| 72 | + |
| 73 | + err = jvmti->AddCapabilities( &caps); |
| 74 | + if (err != JVMTI_ERROR_NONE) { |
| 75 | + return JNI_ERR; |
| 76 | + } |
| 77 | + |
| 78 | + err = jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks)); |
| 79 | + if (err != JVMTI_ERROR_NONE) { |
| 80 | + return JNI_ERR; |
| 81 | + } |
| 82 | + |
| 83 | + err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC , NULL); |
| 84 | + if (err != JVMTI_ERROR_NONE) { |
| 85 | + return JNI_ERR; |
| 86 | + } |
| 87 | + |
| 88 | + return JNI_OK; |
| 89 | +} |
| 90 | + |
| 91 | +} //extern "C" |
0 commit comments