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 "jvmti_common.h"
 27 #include "jvmti_thread.h"
 28 
 29 
 30 extern "C" {
 31 
 32 /* ========================================================================== */
 33 
 34 /* scaffold objects */
 35 static jlong timeout = 0;
 36 
 37 /* test objects */
 38 static int eventsCount = 0;
 39 
 40 /* ========================================================================== */
 41 
 42 /* check if any VMObjectAlloc events received */
 43 static int checkVMObjectAllocEvents() {
 44 
 45   NSK_DISPLAY1("VMObjectAlloc events received: %d\n", eventsCount);
 46 
 47   if (eventsCount == 0) {
 48     NSK_DISPLAY0("# WARNING: no VMObjectAlloc events\n");
 49     NSK_DISPLAY0("#    (VM might not allocate such objects at all)\n");
 50   }
 51 
 52   return NSK_TRUE;
 53 }
 54 
 55 /* ========================================================================== */
 56 
 57 JNIEXPORT void JNICALL
 58 VMObjectAlloc(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread, jobject object,
 59               jclass object_klass, jlong size) {
 60   char *signature, *generic;
 61   jvmtiError err;
 62 
 63   eventsCount++;
 64 
 65   err = jvmti->GetClassSignature(object_klass, &signature, &generic);
 66   if (err != JVMTI_ERROR_NONE) {
 67     nsk_jvmti_setFailStatus();
 68     return;
 69   }
 70 
 71   NSK_DISPLAY2("VMObjectAlloc: \"%s\", size=%ld\n", signature, (long)size);
 72 
 73   if (signature != NULL)
 74     jvmti->Deallocate((unsigned char*)signature);
 75 
 76   if (generic != NULL)
 77     jvmti->Deallocate((unsigned char*)generic);
 78 
 79 }
 80 
 81 /* ========================================================================== */
 82 
 83 /* agent algorithm */
 84 static void JNICALL
 85 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 86 
 87   /* wait for debuggee start */
 88   if (!nsk_jvmti_waitForSync(timeout))
 89     return;
 90 
 91   /* testcase #1: check if any VMObjectAlloc events received*/
 92   NSK_DISPLAY0("Testcase #1: check if any VMObjectAlloc events received\n");
 93   if (!checkVMObjectAllocEvents())
 94     nsk_jvmti_setFailStatus();
 95 
 96   /* resume debugee after last sync */
 97   if (!nsk_jvmti_resumeSync())
 98     return;
 99 }
100 
101 /* ========================================================================== */
102 
103 /* agent library initialization */
104 #ifdef STATIC_BUILD
105 JNIEXPORT jint JNICALL Agent_OnLoad_vmobjalloc01(JavaVM *jvm, char *options, void *reserved) {
106     return Agent_Initialize(jvm, options, reserved);
107 }
108 JNIEXPORT jint JNICALL Agent_OnAttach_vmobjalloc01(JavaVM *jvm, char *options, void *reserved) {
109     return Agent_Initialize(jvm, options, reserved);
110 }
111 JNIEXPORT jint JNI_OnLoad_vmobjalloc01(JavaVM *jvm, char *options, void *reserved) {
112     return JNI_VERSION_1_8;
113 }
114 #endif
115 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
116   jvmtiEnv* jvmti = NULL;
117   jvmtiCapabilities caps;
118   jvmtiEventCallbacks callbacks;
119   jvmtiError err;
120   jint res;
121 
122   timeout = 60000; // TODO Fix timeout
123   NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
124 
125   /* create JVMTI environment */
126   res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_9);
127   if (res != JNI_OK || jvmti == NULL) {
128     LOG("Wrong result of a valid call to GetEnv!\n");
129     return JNI_ERR;
130   }
131 
132   err = init_agent_data(jvmti, &agent_data);
133   if (err != JVMTI_ERROR_NONE) {
134     return JNI_ERR;
135   }
136 
137 
138   memset(&caps, 0, sizeof(caps));
139   caps.can_generate_vm_object_alloc_events = 1;
140   if (jvmti->AddCapabilities(&caps) != JVMTI_ERROR_NONE) {
141     return JNI_ERR;
142   }
143 
144   memset(&callbacks, 0, sizeof(callbacks));
145   callbacks.VMObjectAlloc= &VMObjectAlloc;
146   if (jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks) != JVMTI_ERROR_NONE)) {
147     return JNI_ERR;
148   }
149 
150   /* enable VMObjectAlloc event */
151   if (jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, NULL) != JVMTI_ERROR_NONE) {
152     return JNI_ERR;
153   }
154 
155   /* register agent proc and arg */
156   nsk_jvmti_setAgentProc(agentProc, NULL);
157 
158   return JNI_OK;
159 }
160 
161 JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
162   return Agent_Initialize(jvm, options, reserved);
163 }
164 
165 JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
166   return Agent_Initialize(jvm, options, reserved);
167 }
168 }