1 /* 2 * Copyright (c) 2004, 2018, 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 #include "agent_common.h" 27 #include "jni_tools.h" 28 #include "jvmti_tools.h" 29 30 extern "C" { 31 32 /* ========================================================================== */ 33 34 /* scaffold objects */ 35 static jlong timeout = 0; 36 37 /* test objects */ 38 static int eventsCount = 0; 39 40 /* ========================================================================== */ 41 42 /* check if any VMObjectAlloc events received */ 43 static int checkVMObjectAllocEvents() { 44 45 NSK_DISPLAY1("VMObjectAlloc events received: %d\n", eventsCount); 46 47 if (eventsCount == 0) { 48 NSK_DISPLAY0("# WARNING: no VMObjectAlloc events\n"); 49 NSK_DISPLAY0("# (VM might not allocate such objects at all)\n"); 50 } 51 52 return NSK_TRUE; 53 } 54 55 /* ========================================================================== */ 56 57 JNIEXPORT void JNICALL 58 VMObjectAlloc(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread, jobject object, 59 jclass object_klass, jlong size) { 60 char *signature, *generic; 61 62 eventsCount++; 63 64 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, jvmti, 65 object_klass, &signature, &generic))) { 66 nsk_jvmti_setFailStatus(); 67 return; 68 } 69 70 NSK_DISPLAY2("VMObjectAlloc: \"%s\", size=%d\n", signature, size); 71 72 if (signature != NULL) 73 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)signature); 74 75 if (generic != NULL) 76 NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)generic); 77 78 } 79 80 /* ========================================================================== */ 81 82 /* agent algorithm */ 83 static void JNICALL 84 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 85 86 /* wait for debuggee start */ 87 if (!nsk_jvmti_waitForSync(timeout)) 88 return; 89 90 /* testcase #1: check if any VMObjectAlloc events received*/ 91 NSK_DISPLAY0("Testcase #1: check if any VMObjectAlloc events received\n"); 92 if (!checkVMObjectAllocEvents()) 93 nsk_jvmti_setFailStatus(); 94 95 /* resume debugee after last sync */ 96 if (!nsk_jvmti_resumeSync()) 97 return; 98 } 99 100 /* ========================================================================== */ 101 102 /* agent library initialization */ 103 #ifdef STATIC_BUILD 104 JNIEXPORT jint JNICALL Agent_OnLoad_vmobjalloc001(JavaVM *jvm, char *options, void *reserved) { 105 return Agent_Initialize(jvm, options, reserved); 106 } 107 JNIEXPORT jint JNICALL Agent_OnAttach_vmobjalloc001(JavaVM *jvm, char *options, void *reserved) { 108 return Agent_Initialize(jvm, options, reserved); 109 } 110 JNIEXPORT jint JNI_OnLoad_vmobjalloc001(JavaVM *jvm, char *options, void *reserved) { 111 return JNI_VERSION_1_8; 112 } 113 #endif 114 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 115 jvmtiEnv* jvmti = NULL; 116 jvmtiCapabilities caps; 117 jvmtiEventCallbacks callbacks; 118 119 /* init framework and parse options */ 120 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 121 return JNI_ERR; 122 123 timeout = nsk_jvmti_getWaitTime() * 60000; 124 NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout); 125 126 /* create JVMTI environment */ 127 if (!NSK_VERIFY((jvmti = 128 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 129 return JNI_ERR; 130 131 memset(&caps, 0, sizeof(caps)); 132 caps.can_generate_vm_object_alloc_events = 1; 133 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) { 134 return JNI_ERR; 135 } 136 137 memset(&callbacks, 0, sizeof(callbacks)); 138 callbacks.VMObjectAlloc= &VMObjectAlloc; 139 if (!NSK_JVMTI_VERIFY( 140 NSK_CPP_STUB3(SetEventCallbacks, jvmti, 141 &callbacks, sizeof(callbacks)))) 142 return JNI_ERR; 143 144 /* enable VMObjectAlloc event */ 145 if (!NSK_JVMTI_VERIFY( 146 NSK_CPP_STUB4(SetEventNotificationMode, jvmti, JVMTI_ENABLE, 147 JVMTI_EVENT_VM_OBJECT_ALLOC, NULL))) 148 return JNI_ERR; 149 150 /* register agent proc and arg */ 151 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 152 return JNI_ERR; 153 154 return JNI_OK; 155 } 156 157 /* ========================================================================== */ 158 159 }