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 <stdlib.h>
  25 #include <string.h>
  26 #include "jni_tools.h"
  27 #include "agent_common.h"
  28 #include "jvmti_tools.h"
  29 
  30 #define PASSED 0
  31 #define STATUS_FAILED 2
  32 
  33 extern "C" {
  34 
  35 /* ========================================================================== */
  36 
  37 /* scaffold objects */
  38 static jlong timeout = 0;
  39 
  40 /* event counts */
  41 static int VMObjectAllocEventsCount = 0;
  42 
  43 /* ========================================================================== */
  44 
  45 /** callback functions **/
  46 
  47 static void JNICALL
  48 VMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
  49               jthread thread, jobject object,
  50               jclass object_klass, jlong size) {
  51     char *signature;
  52 
  53     VMObjectAllocEventsCount++;
  54 
  55     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(GetClassSignature, jvmti_env,
  56             object_klass, &signature, NULL))) {
  57         nsk_jvmti_setFailStatus();
  58         return;
  59     }
  60     NSK_DISPLAY2("VMObjectAlloc: \"%s\", size=%d\n", signature, size);
  61     if (signature != NULL)
  62         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)signature);
  63 }
  64 
  65 /* ========================================================================== */
  66 
  67 /** Agent algorithm. */
  68 static void JNICALL
  69 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  70 
  71     if (!nsk_jvmti_waitForSync(timeout))
  72         return;
  73 
  74     NSK_DISPLAY1("VMObjectAlloc events received: %d\n",
  75         VMObjectAllocEventsCount);
  76     if (!NSK_VERIFY(VMObjectAllocEventsCount == 0))
  77         nsk_jvmti_setFailStatus();
  78 
  79     if (!nsk_jvmti_resumeSync())
  80         return;
  81 }
  82 
  83 /* ========================================================================== */
  84 
  85 /** Agent library initialization. */
  86 #ifdef STATIC_BUILD
  87 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t005a(JavaVM *jvm, char *options, void *reserved) {
  88     return Agent_Initialize(jvm, options, reserved);
  89 }
  90 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t005a(JavaVM *jvm, char *options, void *reserved) {
  91     return Agent_Initialize(jvm, options, reserved);
  92 }
  93 JNIEXPORT jint JNI_OnLoad_ma10t005a(JavaVM *jvm, char *options, void *reserved) {
  94     return JNI_VERSION_1_8;
  95 }
  96 #endif
  97 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
  98     jvmtiEnv* jvmti = NULL;
  99     jvmtiCapabilities caps;
 100     jvmtiEventCallbacks callbacks;
 101 
 102     NSK_DISPLAY0("Agent_OnLoad\n");
 103 
 104     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 105         return JNI_ERR;
 106 
 107     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 108 
 109     if (!NSK_VERIFY((jvmti =
 110             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 111         return JNI_ERR;
 112 
 113     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 114         return JNI_ERR;
 115 
 116     memset(&caps, 0, sizeof(caps));
 117     caps.can_generate_vm_object_alloc_events = 1;
 118     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 119         return JNI_ERR;
 120     }
 121 
 122     memset(&callbacks, 0, sizeof(callbacks));
 123     callbacks.VMObjectAlloc = &VMObjectAlloc;
 124     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 125         return JNI_ERR;
 126 
 127     return JNI_OK;
 128 }
 129 
 130 /* ========================================================================== */
 131 
 132 }