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 <stdlib.h>
26 #include <string.h>
27 #include <jvmti.h>
28 #include "agent_common.h"
29 
30 #include "nsk_tools.h"
31 #include "JVMTITools.h"
32 #include "jvmti_tools.h"
33 
34 extern "C" {
35 
36 #define STATUS_FAILED 2
37 #define PASSED 0
38 
39 static volatile jint result = PASSED;
40 static volatile int gcstart = 0;
41 static volatile int gcfinish = 0;
42 static jvmtiEnv *jvmti = NULL;
43 static jvmtiEventCallbacks callbacks;
44 static jvmtiCapabilities caps;
45 
46 /** callback functions **/
47 void JNICALL
GarbageCollectionStart(jvmtiEnv * jvmti_env)48 GarbageCollectionStart(jvmtiEnv *jvmti_env) {
49     gcstart++;
50     NSK_DISPLAY1("GarbageCollectionStart event #%d received\n",
51         gcstart);
52 
53     if (gcstart != (gcfinish+1)) {
54         result = STATUS_FAILED;
55         NSK_COMPLAIN2(
56             "TEST FAILED: GarbageCollectionStart event has no a matched pair GarbageCollectionFinish:\n"
57             "\t%d GarbageCollectionStart events\t%d GarbageCollectionFinish events\n\n",
58             gcstart, gcfinish);
59     }
60     else
61         NSK_DISPLAY0("CHECK PASSED: GarbageCollectionStart event has a matched pair GarbageCollectionFinish as expected\n\n");
62 }
63 
64 void JNICALL
GarbageCollectionFinish(jvmtiEnv * jvmti_env)65 GarbageCollectionFinish(jvmtiEnv *jvmti_env) {
66     gcfinish++;
67     NSK_DISPLAY1("GarbageCollectionFinish event #%d received\n",
68         gcfinish);
69 
70     if (gcstart != gcfinish) {
71         result = STATUS_FAILED;
72         NSK_COMPLAIN2(
73             "TEST FAILED: GarbageCollectionFinish event has no a matched pair GarbageCollectionStart:\n"
74             "\t%d GarbageCollectionStart events\t%d GarbageCollectionFinish events\n\n",
75             gcstart, gcfinish);
76     }
77     else
78         NSK_DISPLAY0("CHECK PASSED: GarbageCollectionFinish event has a matched pair GarbageCollectionStart as expected\n\n");
79 }
80 
81 void JNICALL
VMDeath(jvmtiEnv * jvmti_env,JNIEnv * env)82 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) {
83     NSK_DISPLAY0("VMDeath event received\n");
84 
85     if (gcstart != gcfinish || result == STATUS_FAILED) {
86         NSK_COMPLAIN2(
87             "TEST FAILED: some GarbageCollectionFinish events have no a matched pair GarbageCollectionStart:\n"
88             "\t%d GarbageCollectionStart events\t%d GarbageCollectionFinish events\n\n",
89             gcstart, gcfinish);
90 
91         exit(95 + STATUS_FAILED);
92     }
93     else
94         NSK_DISPLAY0("CHECK PASSED: all GarbageCollectionStart/GarbageCollectionFinish events have a matched pair as expected\n\n");
95 }
96 
97 /************************/
98 
99 #ifdef STATIC_BUILD
Agent_OnLoad_gcstart001(JavaVM * jvm,char * options,void * reserved)100 JNIEXPORT jint JNICALL Agent_OnLoad_gcstart001(JavaVM *jvm, char *options, void *reserved) {
101     return Agent_Initialize(jvm, options, reserved);
102 }
Agent_OnAttach_gcstart001(JavaVM * jvm,char * options,void * reserved)103 JNIEXPORT jint JNICALL Agent_OnAttach_gcstart001(JavaVM *jvm, char *options, void *reserved) {
104     return Agent_Initialize(jvm, options, reserved);
105 }
JNI_OnLoad_gcstart001(JavaVM * jvm,char * options,void * reserved)106 JNIEXPORT jint JNI_OnLoad_gcstart001(JavaVM *jvm, char *options, void *reserved) {
107     return JNI_VERSION_1_8;
108 }
109 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)110 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
111     /* init framework and parse options */
112     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
113         return JNI_ERR;
114 
115     /* create JVMTI environment */
116     if (!NSK_VERIFY((jvmti =
117             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
118         return JNI_ERR;
119 
120     /* add capability to generate compiled method events */
121     memset(&caps, 0, sizeof(jvmtiCapabilities));
122     caps.can_generate_garbage_collection_events = 1;
123     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
124         return JNI_ERR;
125 
126     if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
127         return JNI_ERR;
128 
129     if (!caps.can_generate_garbage_collection_events)
130         NSK_DISPLAY0("Warning: generation of garbage collection events is not implemented\n");
131 
132     /* set event callback */
133     NSK_DISPLAY0("setting event callbacks ...\n");
134     (void) memset(&callbacks, 0, sizeof(callbacks));
135     callbacks.VMDeath = &VMDeath;
136     callbacks.GarbageCollectionStart = &GarbageCollectionStart;
137     callbacks.GarbageCollectionFinish = &GarbageCollectionFinish;
138     if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
139         return JNI_ERR;
140 
141     NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");
142     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL)))
143         return JNI_ERR;
144     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL)))
145         return JNI_ERR;
146     if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL)))
147         return JNI_ERR;
148     NSK_DISPLAY0("enabling the events done\n\n");
149 
150     return JNI_OK;
151 }
152 
153 }
154