1 /*
   2  * Copyright (c) 2003, 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 <stdio.h>
  25 #include <string.h>
  26 #include "jvmti.h"
  27 #include "agent_common.h"
  28 
  29 #ifdef __cplusplus
  30 extern "C" {
  31 #endif
  32 
  33 #ifndef JNI_ENV_ARG
  34 
  35 #ifdef __cplusplus
  36 #define JNI_ENV_ARG(x, y) y
  37 #define JNI_ENV_ARG1(x)
  38 #define JNI_ENV_PTR(x) x
  39 #else
  40 #define JNI_ENV_ARG(x,y) x, y
  41 #define JNI_ENV_ARG1(x) x
  42 #define JNI_ENV_PTR(x) (*x)
  43 #endif
  44 
  45 #endif
  46 
  47 #define JVMTI_ENV_ARG JNI_ENV_ARG
  48 #define JVMTI_ENV_ARG1 JNI_ENV_ARG1
  49 #define JVMTI_ENV_PTR JNI_ENV_PTR
  50 
  51 #define JVMTI_ERROR_CHECK(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); return res;}
  52 #define JVMTI_ERROR_CHECK_VOID(str,res) if ( res != JVMTI_ERROR_NONE) { printf(str); printf("%d\n",res); iGlobalStatus = 2; }
  53 
  54 #define THREADS_LIMIT 8
  55 
  56 jrawMonitorID access_lock;
  57 jvmtiEnv *jvmti;
  58 jint iGlobalStatus = 0;
  59 jthread susp_thrd[THREADS_LIMIT];
  60 static jvmtiEventCallbacks callbacks;
  61 static jvmtiCapabilities jvmti_caps;
  62 
  63 
  64 int printdump = 0;
  65 int gc_start_count =0;
  66 int gc_finish_count =0;
  67 
  68 
  69 void debug_printf(const char *fmt, ...) {
  70     va_list args;
  71 
  72     va_start(args, fmt);
  73     if (printdump) {
  74         vprintf(fmt, args);
  75     }
  76     va_end(args);
  77 }
  78 
  79 
  80 void JNICALL vmInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thread) {
  81 
  82     debug_printf("VMInit event  done\n");
  83 }
  84 
  85 void JNICALL vmExit(jvmtiEnv *jvmti_env, JNIEnv *env) {
  86     debug_printf("------------ JVMTI_EVENT_VM_DEATH ------------\n");
  87     debug_printf("JVMTI_EVENT_GARBAGE_COLLECTION_START count: %d\n",gc_start_count);
  88     debug_printf("JVMTI_EVENT_GARBAGE_COLLECTION_FINISH count: %d\n",gc_finish_count);
  89 }
  90 
  91 void JNICALL gc_start(jvmtiEnv *jvmti_env) {
  92 
  93     gc_start_count++;
  94     debug_printf("Event: JVMTI_EVENT_GC_START\n");
  95 }
  96 
  97 void JNICALL gc_finish(jvmtiEnv *jvmti_env) {
  98 
  99     gc_finish_count++;
 100     debug_printf("Event: JVMTI_EVENT_GC_FINISH\n");
 101 }
 102 
 103 
 104 void init_callbacks() {
 105     memset((void *)&callbacks, 0, sizeof(jvmtiEventCallbacks));
 106     callbacks.VMInit = vmInit;
 107     callbacks.VMDeath = vmExit;
 108     callbacks.GarbageCollectionStart = gc_start;
 109     callbacks.GarbageCollectionFinish = gc_finish;
 110 }
 111 
 112 
 113 #ifdef STATIC_BUILD
 114 JNIEXPORT jint JNICALL Agent_OnLoad_gc(JavaVM *jvm, char *options, void *reserved) {
 115     return Agent_Initialize(jvm, options, reserved);
 116 }
 117 JNIEXPORT jint JNICALL Agent_OnAttach_gc(JavaVM *jvm, char *options, void *reserved) {
 118     return Agent_Initialize(jvm, options, reserved);
 119 }
 120 JNIEXPORT jint JNI_OnLoad_gc(JavaVM *jvm, char *options, void *reserved) {
 121     return JNI_VERSION_1_8;
 122 }
 123 #endif
 124 jint Agent_Initialize(JavaVM * jvm, char *options, void *reserved) {
 125     jint res;
 126 
 127     if (options && strlen(options) > 0) {
 128         if (strstr(options, "printdump")) {
 129             printdump = 1;
 130         }
 131     }
 132 
 133     res = JNI_ENV_PTR(jvm)->
 134         GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_1_1);
 135     if (res < 0) {
 136         debug_printf("Wrong result of a valid call to GetEnv!\n");
 137         return JNI_ERR;
 138     }
 139 
 140     /* Create data access lock */
 141     res = JVMTI_ENV_PTR(jvmti)->CreateRawMonitor(JVMTI_ENV_ARG(jvmti,"_access_lock"),&access_lock);
 142     JVMTI_ERROR_CHECK("RawMonitorEnter in monitor_contended_entered failed with error code ", res);
 143 
 144 
 145     /* Add capabilities */
 146     res = JVMTI_ENV_PTR(jvmti)->GetPotentialCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
 147     JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
 148 
 149     res = JVMTI_ENV_PTR(jvmti)->AddCapabilities(JVMTI_ENV_ARG(jvmti, &jvmti_caps));
 150     JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
 151 
 152     /* Enable events */
 153     init_callbacks();
 154     res = JVMTI_ENV_PTR(jvmti)->SetEventCallbacks(JVMTI_ENV_ARG(jvmti, &callbacks), sizeof(callbacks));
 155     JVMTI_ERROR_CHECK("SetEventCallbacks returned error", res);
 156 
 157     res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti,JVMTI_ENABLE),JVMTI_EVENT_VM_INIT,NULL);
 158     JVMTI_ERROR_CHECK("SetEventNotificationMode for VM_INIT returned error", res);
 159 
 160     res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti,JVMTI_ENABLE),JVMTI_EVENT_VM_DEATH,NULL);
 161     JVMTI_ERROR_CHECK("SetEventNotificationMode for vm death event returned error", res);
 162 
 163     res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti,JVMTI_ENABLE),JVMTI_EVENT_GARBAGE_COLLECTION_START,NULL);
 164     JVMTI_ERROR_CHECK("SetEventNotificationMode for gc start returned error", res);
 165 
 166     res = JVMTI_ENV_PTR(jvmti)->SetEventNotificationMode(JVMTI_ENV_ARG(jvmti,JVMTI_ENABLE),JVMTI_EVENT_GARBAGE_COLLECTION_FINISH,NULL);
 167     JVMTI_ERROR_CHECK("SetEventNotificationMode for gc finish returned error", res);
 168 
 169     return JNI_OK;
 170 }
 171 
 172 
 173 
 174 
 175 JNIEXPORT void JNICALL
 176 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_checkGCStart(JNIEnv * env, jclass cls) {
 177     if (gc_start_count == 0) {
 178         printf("Error: JVMTI_EVENT_GARBAGE_COLLECTION_START count = 0\n");
 179         iGlobalStatus = 2;
 180     }
 181 }
 182 
 183 JNIEXPORT void JNICALL
 184 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_checkGCFinish(JNIEnv * env, jclass cls) {
 185     if (gc_finish_count == 0) {
 186         printf("Error: JVMTI_EVENT_GARBAGE_COLLECTION_FINISH count = 0\n");
 187         iGlobalStatus = 2;
 188     }
 189 }
 190 
 191 JNIEXPORT jint JNICALL
 192 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_GetResult(JNIEnv * env, jclass cls) {
 193     return iGlobalStatus;
 194 }
 195 
 196 
 197 JNIEXPORT void JNICALL
 198 Java_nsk_jvmti_unit_functions_ForceGarbageCollection_gc_jvmtiForceGC (JNIEnv * env, jclass cls) {
 199     jvmtiError ret;
 200 
 201     debug_printf("jvmti Force gc requested \n");
 202     ret = JVMTI_ENV_PTR(jvmti)->ForceGarbageCollection(JVMTI_ENV_ARG1(jvmti));
 203 
 204     if (ret != JVMTI_ERROR_NONE) {
 205         printf("Error: ForceGarbageCollection %d \n", ret);
 206         iGlobalStatus = 2;
 207     }
 208 }
 209 
 210 #ifdef __cplusplus
 211 }
 212 #endif