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