1 /*
2  * Copyright (c) 2008, 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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <jni.h>
27 #include <jvmti.h>
28 #include <aod.h>
29 #include <jvmti_aod.h>
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 /*
36  * Agent receives expected number of ThreadStart/ThreadEnd events and finishes work
37  * (events should be provoked by target application)
38  */
39 
40 
41 #define EXPECTED_EVENTS_NUMBER 200
42 
43 static Options* options = NULL;
44 static const char* agentName;
45 
46 static jvmtiEvent testEvents[] = {JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END};
47 static const int testEventsNumber = 2;
48 
49 static jrawMonitorID eventsCounterMonitor;
50 
51 static volatile int eventsCounter = 0;
52 
eventHandler(jvmtiEnv * jvmti,JNIEnv * jni,jthread thread,int threadStartEvent)53 void eventHandler(jvmtiEnv *jvmti,
54         JNIEnv* jni,
55         jthread thread,
56         int threadStartEvent) {
57     char threadName[MAX_STRING_LENGTH];
58     int success = 1;
59     int threadsCount = 0;
60     jthread * threads;
61 
62     if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
63         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
64         return;
65     }
66 
67     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetAllThreads, jvmti, &threadsCount, &threads))) {
68         NSK_COMPLAIN1("%s: failed to get all threads\n", agentName);
69         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
70         return;
71     }
72 
73     nsk_jvmti_aod_deallocate(jvmti, (unsigned char*)threads);
74 
75     if (NSK_JVMTI_VERIFY(NSK_CPP_STUB2(
76             RawMonitorEnter, jvmti, eventsCounterMonitor))) {
77 
78         eventsCounter++;
79 
80         if (threadStartEvent)
81             NSK_DISPLAY3("%s: ThreadStart event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);
82         else
83             NSK_DISPLAY3("%s: ThreadEnd event received for thread '%s' (eventsCounter: %d)\n", agentName, threadName, eventsCounter);
84 
85         if (eventsCounter == EXPECTED_EVENTS_NUMBER) {
86             NSK_DISPLAY2("%s: all expected events were received (eventsCounter: %d)\n", agentName, eventsCounter);
87 
88             nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
89         }
90 
91         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, eventsCounterMonitor))) {
92             success = 0;
93         }
94     } else {
95         success = 0;
96     }
97 
98     if (!success) {
99         nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
100     }
101 }
102 
threadStartHandler(jvmtiEnv * jvmti,JNIEnv * jni,jthread thread)103 void JNICALL threadStartHandler(
104         jvmtiEnv *jvmti,
105         JNIEnv* jni,
106         jthread thread) {
107     eventHandler(jvmti, jni, thread, 1);
108 }
109 
threadEndHandler(jvmtiEnv * jvmti,JNIEnv * jni,jthread thread)110 void JNICALL threadEndHandler(
111         jvmtiEnv *jvmti,
112         JNIEnv* jni,
113         jthread thread) {
114     eventHandler(jvmti, jni, thread, 0);
115 }
116 
117 #ifdef STATIC_BUILD
JNI_OnLoad_attach045Agent02(JavaVM * jvm,char * options,void * reserved)118 JNIEXPORT jint JNI_OnLoad_attach045Agent02(JavaVM *jvm, char *options, void *reserved) {
119     return JNI_VERSION_1_8;
120 }
121 #endif
122 
123 JNIEXPORT jint JNICALL
124 #ifdef STATIC_BUILD
Agent_OnAttach_attach045Agent02(JavaVM * vm,char * optionsString,void * reserved)125 Agent_OnAttach_attach045Agent02(JavaVM *vm, char *optionsString, void *reserved)
126 #else
127 Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
128 #endif
129 {
130     jvmtiEventCallbacks eventCallbacks;
131     jvmtiEnv* jvmti;
132     JNIEnv* jni;
133 
134     if (!NSK_VERIFY((options = (Options*) nsk_aod_createOptions(optionsString)) != NULL))
135         return JNI_ERR;
136 
137     agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
138 
139     if ((jni = (JNIEnv*) nsk_aod_createJNIEnv(vm)) == NULL)
140         return JNI_ERR;
141 
142     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved)) != NULL))
143         return JNI_ERR;
144 
145     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(CreateRawMonitor, jvmti, "attach045-agent02-eventsCounterMonitor", &eventsCounterMonitor))) {
146         return JNI_ERR;
147     }
148 
149     memset(&eventCallbacks,0, sizeof(eventCallbacks));
150     eventCallbacks.ThreadStart = threadStartHandler;
151     eventCallbacks.ThreadEnd = threadEndHandler;
152     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, jvmti, &eventCallbacks, sizeof(eventCallbacks))) ) {
153         return JNI_ERR;
154     }
155 
156     if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
157         return JNI_ERR;
158     }
159 
160     NSK_DISPLAY1("%s: initialization was done\n", agentName);
161 
162     if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
163         return JNI_ERR;
164 
165     return JNI_OK;
166 }
167 
168 
169 #ifdef __cplusplus
170 }
171 #endif
172