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 jlong nanos = 0;
  37 static jvmtiTimerInfo timer_info1, timer_info2;
  38 static int *user_data = 0;
  39 
  40 /* ============================================================================= */
  41 
  42 jvmtiIterationControl JNICALL
  43 heapObjectCallback(jlong class_tag,
  44                    jlong size,
  45                    jlong* tag_ptr,
  46                    void* user_data) {
  47 
  48     if (!NSK_JVMTI_VERIFY(
  49             st_jvmti->GetCurrentThreadCpuTimerInfo(&timer_info1 ))) {
  50         nsk_jvmti_setFailStatus();
  51     }
  52     /* Check the returned jvmtiTimerInfo structure */
  53     if (timer_info1.max_value == 0) {
  54         NSK_COMPLAIN0("GetCurrentThreadCpuTimerInfo returned zero in jvmtiTimerInfo.max_value\n");
  55         nsk_jvmti_setFailStatus();
  56     }
  57     if (timer_info1.may_skip_forward != JNI_TRUE && timer_info1.may_skip_forward != JNI_FALSE) {
  58         NSK_COMPLAIN0("GetCurrentThreadCpuTimerInfo returned unknown type value in jvmtiTimerInfo.may_skip_forward\n");
  59         nsk_jvmti_setFailStatus();
  60     }
  61     if (timer_info1.may_skip_backward != JNI_TRUE && timer_info1.may_skip_backward != JNI_FALSE) {
  62         NSK_COMPLAIN0("GetCurrentThreadCpuTimerInfo returned unknown type value in jvmtiTimerInfo.may_skip_backward\n");
  63         nsk_jvmti_setFailStatus();
  64     }
  65     /* ---------------------------------------------------------------------- */
  66 
  67     if (!NSK_JVMTI_VERIFY(
  68             st_jvmti->GetCurrentThreadCpuTime(&nanos ))) {
  69         nsk_jvmti_setFailStatus();
  70     }
  71     /* ---------------------------------------------------------------------- */
  72 
  73     if (!NSK_JVMTI_VERIFY(
  74             st_jvmti->GetTimerInfo(&timer_info2 ))) {
  75         nsk_jvmti_setFailStatus();
  76     }
  77     /* Check the returned jvmtiTimerInfo structure */
  78     if (timer_info2.max_value == 0) {
  79         NSK_COMPLAIN0("GetTimerInfo returned zero in jvmtiTimerInfo.max_value\n");
  80         nsk_jvmti_setFailStatus();
  81     }
  82     if (timer_info2.may_skip_forward != JNI_TRUE && timer_info2.may_skip_forward != JNI_FALSE) {
  83         NSK_COMPLAIN0("GetTimerInfo returned unknown type value in jvmtiTimerInfo.may_skip_forward\n");
  84         nsk_jvmti_setFailStatus();
  85     }
  86     if (timer_info2.may_skip_backward != JNI_TRUE && timer_info2.may_skip_backward != JNI_FALSE) {
  87         NSK_COMPLAIN0("GetTimerInfo returned unknown type value in jvmtiTimerInfo.may_skip_backward\n");
  88         nsk_jvmti_setFailStatus();
  89     }
  90     /* ---------------------------------------------------------------------- */
  91 
  92     nanos = 0;
  93     if (!NSK_JVMTI_VERIFY(
  94             st_jvmti->GetTime(&nanos ))) {
  95         nsk_jvmti_setFailStatus();
  96     }
  97 
  98     /*  Iterate over only first object */
  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     {
 113         NSK_DISPLAY0("Calling IterateOverHeap with filter JVMTI_HEAP_OBJECT_EITHER\n");
 114         {
 115             if (!NSK_JVMTI_VERIFY(
 116                     jvmti->IterateOverHeap(JVMTI_HEAP_OBJECT_EITHER, heapObjectCallback, (void *)user_data))) {
 117                 nsk_jvmti_setFailStatus();
 118             }
 119         }
 120     }
 121 
 122     NSK_DISPLAY0("Let debugee to finish\n");
 123     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 124         return;
 125 }
 126 
 127 /* ============================================================================= */
 128 
 129 /** Agent library initialization. */
 130 #ifdef STATIC_BUILD
 131 JNIEXPORT jint JNICALL Agent_OnLoad_iterheap007(JavaVM *jvm, char *options, void *reserved) {
 132     return Agent_Initialize(jvm, options, reserved);
 133 }
 134 JNIEXPORT jint JNICALL Agent_OnAttach_iterheap007(JavaVM *jvm, char *options, void *reserved) {
 135     return Agent_Initialize(jvm, options, reserved);
 136 }
 137 JNIEXPORT jint JNI_OnLoad_iterheap007(JavaVM *jvm, char *options, void *reserved) {
 138     return JNI_VERSION_1_8;
 139 }
 140 #endif
 141 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 142     jvmtiEnv* jvmti = NULL;
 143 
 144     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 145         return JNI_ERR;
 146 
 147     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 148 
 149     if (!NSK_VERIFY((jvmti =
 150             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 151         return JNI_ERR;
 152 
 153     /* save pointer to environment to use it in callbacks */
 154     st_jvmti = jvmti;
 155 
 156     {
 157         jvmtiCapabilities caps;
 158         memset(&caps, 0, sizeof(caps));
 159         caps.can_tag_objects = 1;
 160         caps.can_get_current_thread_cpu_time = 1;
 161 
 162         if (!NSK_JVMTI_VERIFY(
 163                 jvmti->AddCapabilities(&caps))) {
 164             return JNI_ERR;
 165         }
 166     }
 167 
 168     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 169         return JNI_ERR;
 170 
 171     return JNI_OK;
 172 }
 173 
 174 /* ============================================================================= */
 175 
 176 }