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(jvmti->GetClassSignature(object_klass, &signature, &generic))) { 65 nsk_jvmti_setFailStatus(); 66 return; 67 } 68 69 NSK_DISPLAY2("VMObjectAlloc: \"%s\", size=%d\n", signature, size); 70 71 if (signature != NULL) 72 jvmti->Deallocate((unsigned char*)signature); 73 74 if (generic != NULL) 75 jvmti->Deallocate((unsigned char*)generic); 76 77 } 78 79 /* ========================================================================== */ 80 81 /* agent algorithm */ 82 static void JNICALL 83 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 84 85 /* wait for debuggee start */ 86 if (!nsk_jvmti_waitForSync(timeout)) 87 return; 88 89 /* testcase #1: check if any VMObjectAlloc events received*/ 90 NSK_DISPLAY0("Testcase #1: check if any VMObjectAlloc events received\n"); 91 if (!checkVMObjectAllocEvents()) 92 nsk_jvmti_setFailStatus(); 93 94 /* resume debugee after last sync */ 95 if (!nsk_jvmti_resumeSync()) 96 return; 97 } 98 99 /* ========================================================================== */ 100 101 /* agent library initialization */ 102 #ifdef STATIC_BUILD 103 JNIEXPORT jint JNICALL Agent_OnLoad_vmobjalloc001(JavaVM *jvm, char *options, void *reserved) { 104 return Agent_Initialize(jvm, options, reserved); 105 } 106 JNIEXPORT jint JNICALL Agent_OnAttach_vmobjalloc001(JavaVM *jvm, char *options, void *reserved) { 107 return Agent_Initialize(jvm, options, reserved); 108 } 109 JNIEXPORT jint JNI_OnLoad_vmobjalloc001(JavaVM *jvm, char *options, void *reserved) { 110 return JNI_VERSION_1_8; 111 } 112 #endif 113 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 114 jvmtiEnv* jvmti = NULL; 115 jvmtiCapabilities caps; 116 jvmtiEventCallbacks callbacks; 117 118 /* init framework and parse options */ 119 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 120 return JNI_ERR; 121 122 timeout = nsk_jvmti_getWaitTime() * 60000; 123 NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout); 124 125 /* create JVMTI environment */ 126 if (!NSK_VERIFY((jvmti = 127 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 128 return JNI_ERR; 129 130 memset(&caps, 0, sizeof(caps)); 131 caps.can_generate_vm_object_alloc_events = 1; 132 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) { 133 return JNI_ERR; 134 } 135 136 memset(&callbacks, 0, sizeof(callbacks)); 137 callbacks.VMObjectAlloc= &VMObjectAlloc; 138 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)))) 139 return JNI_ERR; 140 141 /* enable VMObjectAlloc event */ 142 if (!NSK_JVMTI_VERIFY( 143 jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, NULL))) 144 return JNI_ERR; 145 146 /* register agent proc and arg */ 147 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 148 return JNI_ERR; 149 150 return JNI_OK; 151 } 152 153 /* ========================================================================== */ 154 155 }