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(jvmti_env->GetClassSignature(object_klass, &signature, NULL))) {
  56         nsk_jvmti_setFailStatus();
  57         return;
  58     }
  59     NSK_DISPLAY2("VMObjectAlloc: \"%s\", size=%d\n", signature, size);
  60     if (signature != NULL)
  61         jvmti_env->Deallocate((unsigned char*)signature);
  62 }
  63 
  64 /* ========================================================================== */
  65 
  66 /** Agent algorithm. */
  67 static void JNICALL
  68 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  69 
  70     if (!nsk_jvmti_waitForSync(timeout))
  71         return;
  72 
  73     NSK_DISPLAY1("VMObjectAlloc events received: %d\n",
  74         VMObjectAllocEventsCount);
  75     if (!NSK_VERIFY(VMObjectAllocEventsCount != 0))
  76         nsk_jvmti_setFailStatus();
  77 
  78     if (!nsk_jvmti_resumeSync())
  79         return;
  80 }
  81 
  82 /* ========================================================================== */
  83 
  84 /** Agent library initialization. */
  85 #ifdef STATIC_BUILD
  86 JNIEXPORT jint JNICALL Agent_OnLoad_ma10t005(JavaVM *jvm, char *options, void *reserved) {
  87     return Agent_Initialize(jvm, options, reserved);
  88 }
  89 JNIEXPORT jint JNICALL Agent_OnAttach_ma10t005(JavaVM *jvm, char *options, void *reserved) {
  90     return Agent_Initialize(jvm, options, reserved);
  91 }
  92 JNIEXPORT jint JNI_OnLoad_ma10t005(JavaVM *jvm, char *options, void *reserved) {
  93     return JNI_VERSION_1_8;
  94 }
  95 #endif
  96 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
  97     jvmtiEnv* jvmti = NULL;
  98     jvmtiCapabilities caps;
  99     jvmtiEventCallbacks callbacks;
 100 
 101     NSK_DISPLAY0("Agent_OnLoad\n");
 102 
 103     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 104         return JNI_ERR;
 105 
 106     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 107 
 108     if (!NSK_VERIFY((jvmti =
 109             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 110         return JNI_ERR;
 111 
 112     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 113         return JNI_ERR;
 114 
 115     memset(&caps, 0, sizeof(caps));
 116     caps.can_generate_vm_object_alloc_events = 1;
 117     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 118         return JNI_ERR;
 119     }
 120 
 121     memset(&callbacks, 0, sizeof(callbacks));
 122     callbacks.VMObjectAlloc = &VMObjectAlloc;
 123     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 124         return JNI_ERR;
 125 
 126     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, NULL)))
 127         return JNI_ERR;
 128 
 129     return JNI_OK;
 130 }
 131 
 132 /* ========================================================================== */
 133 
 134 }