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 static jlong timeout = 0; 35 static int fakeUserData = 0, objCounter = 0; 36 static jvmtiEnv* st_jvmti = NULL; 37 static const char* debugeeClassSignature = "Lnsk/jvmti/IterateOverObjectsReachableFromObject/iterobjreachobj003;"; 38 static const char* objectFieldName = "object"; 39 40 /* ============================================================================= */ 41 42 jvmtiIterationControl JNICALL 43 objectReferenceCallback( jvmtiObjectReferenceKind reference_kind, 44 jlong class_tag, 45 jlong size, 46 jlong* tag_ptr, 47 jlong referrer_tag, 48 jint referrer_index, 49 void* user_data) { 50 51 const char* name = "monitorName"; 52 jrawMonitorID monitor_ptr = NULL; 53 54 objCounter++; 55 56 if (!NSK_JVMTI_VERIFY(st_jvmti->CreateRawMonitor(name, &monitor_ptr))) { 57 nsk_jvmti_setFailStatus(); 58 return JVMTI_ITERATION_ABORT; 59 } 60 61 if (!NSK_JVMTI_VERIFY(st_jvmti->RawMonitorEnter(monitor_ptr))) { 62 nsk_jvmti_setFailStatus(); 63 return JVMTI_ITERATION_ABORT; 64 } 65 66 /* Enter second time */ 67 if (!NSK_JVMTI_VERIFY(st_jvmti->RawMonitorEnter(monitor_ptr))) { 68 nsk_jvmti_setFailStatus(); 69 return JVMTI_ITERATION_ABORT; 70 } 71 72 if (!NSK_JVMTI_VERIFY(st_jvmti->RawMonitorWait(monitor_ptr, (jlong)100))) { 73 nsk_jvmti_setFailStatus(); 74 } 75 76 if (!NSK_JVMTI_VERIFY(st_jvmti->RawMonitorNotify(monitor_ptr))) { 77 nsk_jvmti_setFailStatus(); 78 } 79 80 if (!NSK_JVMTI_VERIFY(st_jvmti->RawMonitorNotifyAll(monitor_ptr))) { 81 nsk_jvmti_setFailStatus(); 82 } 83 84 if (!NSK_JVMTI_VERIFY(st_jvmti->RawMonitorExit(monitor_ptr))) { 85 nsk_jvmti_setFailStatus(); 86 return JVMTI_ITERATION_ABORT; 87 } 88 89 /* Exit second time */ 90 if (!NSK_JVMTI_VERIFY(st_jvmti->RawMonitorExit(monitor_ptr))) { 91 nsk_jvmti_setFailStatus(); 92 return JVMTI_ITERATION_ABORT; 93 } 94 95 if (!NSK_JVMTI_VERIFY(st_jvmti->DestroyRawMonitor(monitor_ptr))) { 96 nsk_jvmti_setFailStatus(); 97 } 98 99 return JVMTI_ITERATION_ABORT; 100 } 101 102 /* ============================================================================= */ 103 104 /** Agent algorithm. */ 105 static void JNICALL 106 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) { 107 108 NSK_DISPLAY0("Wait for debugee start\n"); 109 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout))) 110 return; 111 112 do { 113 jclass debugeeClass = NULL; 114 jfieldID objectField = NULL; 115 jobject object = NULL; 116 117 NSK_DISPLAY1("Find debugee class: %s\n", debugeeClassSignature); 118 debugeeClass = nsk_jvmti_classBySignature(debugeeClassSignature); 119 if (debugeeClass == NULL) { 120 nsk_jvmti_setFailStatus(); 121 break; 122 } 123 124 NSK_DISPLAY1("Find static field in debugee class: %s\n", objectFieldName); 125 if (!NSK_JNI_VERIFY(jni, (objectField = jni->GetStaticFieldID( 126 debugeeClass, objectFieldName, debugeeClassSignature)) != NULL)) { 127 nsk_jvmti_setFailStatus(); 128 break; 129 } 130 131 NSK_DISPLAY1("Find value of static field in debugee class: %s\n", objectFieldName); 132 if (!NSK_JNI_VERIFY(jni, (object = 133 jni->GetStaticObjectField(debugeeClass, objectField)) != NULL)) { 134 nsk_jvmti_setFailStatus(); 135 break; 136 } 137 138 NSK_DISPLAY0("Calling IterateOverObjectsReachableFromObject with filter JVMTI_HEAP_OBJECT_EITHER\n"); 139 { 140 if (!NSK_JVMTI_VERIFY(jvmti->IterateOverObjectsReachableFromObject( 141 object, objectReferenceCallback, &fakeUserData))) { 142 nsk_jvmti_setFailStatus(); 143 } 144 } 145 146 if (objCounter == 0) { 147 NSK_COMPLAIN0("IterateOverObjectsReachableFromObject call had not visited any object\n"); 148 nsk_jvmti_setFailStatus(); 149 } 150 } while (0); 151 152 NSK_DISPLAY0("Let debugee to finish\n"); 153 if (!NSK_VERIFY(nsk_jvmti_resumeSync())) 154 return; 155 } 156 157 /* ============================================================================= */ 158 159 /** Agent library initialization. */ 160 #ifdef STATIC_BUILD 161 JNIEXPORT jint JNICALL Agent_OnLoad_iterobjreachobj003(JavaVM *jvm, char *options, void *reserved) { 162 return Agent_Initialize(jvm, options, reserved); 163 } 164 JNIEXPORT jint JNICALL Agent_OnAttach_iterobjreachobj003(JavaVM *jvm, char *options, void *reserved) { 165 return Agent_Initialize(jvm, options, reserved); 166 } 167 JNIEXPORT jint JNI_OnLoad_iterobjreachobj003(JavaVM *jvm, char *options, void *reserved) { 168 return JNI_VERSION_1_8; 169 } 170 #endif 171 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 172 jvmtiEnv* jvmti = NULL; 173 174 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 175 return JNI_ERR; 176 177 timeout = nsk_jvmti_getWaitTime() * 60 * 1000; 178 179 if (!NSK_VERIFY((jvmti = 180 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 181 return JNI_ERR; 182 183 /* save pointer to environment to use it in callbacks */ 184 st_jvmti = jvmti; 185 186 { 187 jvmtiCapabilities caps; 188 189 memset(&caps, 0, sizeof(caps)); 190 caps.can_tag_objects = 1; 191 if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) { 192 return JNI_ERR; 193 } 194 } 195 196 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) 197 return JNI_ERR; 198 199 return JNI_OK; 200 } 201 202 /* ============================================================================= */ 203 204 }