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