Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach022/attach022Agent00.cpp
40951 views
1
/*
2
* Copyright (c) 2007, 2020, 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
#include "ExceptionCheckingJniEnv.hpp"
31
32
extern "C" {
33
34
#define OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach022/ClassForAllocationEventsTest;"
35
36
static jvmtiEnv* jvmti;
37
static Options* options = NULL;
38
static const char* agentName;
39
40
static jvmtiEvent testEvents[] = { JVMTI_EVENT_OBJECT_FREE, JVMTI_EVENT_VM_OBJECT_ALLOC };
41
static const int testEventsNumber = 2;
42
43
static volatile int taggedObjectsCounter = 0;
44
static volatile int freedObjectsCounter = 0;
45
46
static jrawMonitorID objectTagMonitor;
47
static jrawMonitorID objectFreeMonitor;
48
49
volatile int success = 1;
50
51
volatile int agentFinished;
52
53
void shutdownAgent(JNIEnv* jni) {
54
if (agentFinished)
55
return;
56
57
if (!nsk_jvmti_aod_disableEvents(jvmti, testEvents, testEventsNumber))
58
success = 0;
59
60
nsk_aod_agentFinished(jni, agentName, success);
61
62
agentFinished = 1;
63
}
64
65
JNIEXPORT jboolean JNICALL
66
Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent(JNIEnv * jni,
67
jclass klass, jint expectedTaggedObjectsCounter) {
68
69
// Flush any pending ObjectFree events.
70
if (!nsk_jvmti_aod_disableEvents(jvmti, testEvents, testEventsNumber))
71
success = 0;
72
73
if (taggedObjectsCounter != expectedTaggedObjectsCounter) {
74
success = 0;
75
NSK_COMPLAIN2("ERROR: unexpected taggedObjectsCounter: %d (expected value is %d)\n", taggedObjectsCounter, expectedTaggedObjectsCounter);
76
}
77
78
if (taggedObjectsCounter != freedObjectsCounter) {
79
success = 0;
80
NSK_COMPLAIN2("ERROR: taggedObjectsCounter != freedObjectsCounter (taggedObjectsCounter: %d, freedObjectsCounter: %d)\n",
81
taggedObjectsCounter, freedObjectsCounter);
82
}
83
84
shutdownAgent(jni);
85
86
return success ? JNI_TRUE : JNI_FALSE;
87
}
88
89
void JNICALL objectFreeHandler(jvmtiEnv *jvmti, jlong tag) {
90
NSK_DISPLAY2("%s: ObjectFree event received (object tag: %ld)\n", agentName, tag);
91
92
if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectFreeMonitor))) {
93
freedObjectsCounter++;
94
95
if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectFreeMonitor))) {
96
success = 0;
97
}
98
} else {
99
success = 0;
100
}
101
}
102
103
#define ATTACH022_TARGET_APP_CLASS_NAME "nsk/jvmti/AttachOnDemand/attach022/attach022Target"
104
105
void registerNativeMethods(JNIEnv* jni_env) {
106
ExceptionCheckingJniEnvPtr ec_jni(jni_env);
107
jclass appClass;
108
JNINativeMethod nativeMethods[] = {
109
{ (char*)"shutdownAgent", (char*)"(I)Z",
110
(void*) Java_nsk_jvmti_AttachOnDemand_attach022_attach022Target_shutdownAgent } };
111
jint nativeMethodsNumber = 1;
112
113
appClass = ec_jni->FindClass(ATTACH022_TARGET_APP_CLASS_NAME, TRACE_JNI_CALL);
114
ec_jni->RegisterNatives(appClass, nativeMethods, nativeMethodsNumber, TRACE_JNI_CALL);
115
}
116
117
void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,
118
JNIEnv * jni,
119
jthread thread,
120
jobject object,
121
jclass object_class,
122
jlong size) {
123
char className[MAX_STRING_LENGTH];
124
125
if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {
126
success = 0;
127
shutdownAgent(jni);
128
return;
129
}
130
131
NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);
132
133
if (!strcmp(className, OBJECTS_FOR_ALLOCATION_TEST_CLASS_NAME)) {
134
if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(objectTagMonitor))) {
135
jlong tagValue = taggedObjectsCounter + 1;
136
137
if (!NSK_JVMTI_VERIFY(jvmti->SetTag(object, tagValue))) {
138
NSK_COMPLAIN1("%s: failed to set tag\n", agentName);
139
success = 0;
140
} else {
141
NSK_DISPLAY2("%s: object was tagged (tag value: %ld)\n", agentName, tagValue);
142
taggedObjectsCounter++;
143
}
144
145
if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(objectTagMonitor))) {
146
success = 0;
147
}
148
} else {
149
success = 0;
150
}
151
}
152
153
if (!success) {
154
NSK_COMPLAIN1("%s: error happened during agent work, stop agent\n", agentName);
155
shutdownAgent(jni);
156
}
157
}
158
159
#ifdef STATIC_BUILD
160
JNIEXPORT jint JNI_OnLoad_attach022Agent00(JavaVM *jvm, char *options, void *reserved) {
161
return JNI_VERSION_1_8;
162
}
163
#endif
164
165
JNIEXPORT jint JNICALL
166
#ifdef STATIC_BUILD
167
Agent_OnAttach_attach022Agent00(JavaVM *vm, char *optionsString, void *reserved)
168
#else
169
Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
170
#endif
171
{
172
jvmtiEventCallbacks eventCallbacks;
173
jvmtiCapabilities caps;
174
JNIEnv* jni;
175
176
options = (Options*) nsk_aod_createOptions(optionsString);
177
if (!NSK_VERIFY(options != NULL))
178
return JNI_ERR;
179
180
agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
181
182
jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
183
if (jni == NULL)
184
return JNI_ERR;
185
186
jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
187
if (!NSK_VERIFY(jvmti != NULL))
188
return JNI_ERR;
189
190
registerNativeMethods(jni);
191
192
if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectTagMonitor", &objectTagMonitor))) {
193
return JNI_ERR;
194
}
195
196
if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("ObjectFreeMonitor", &objectFreeMonitor))) {
197
return JNI_ERR;
198
}
199
200
memset(&caps, 0, sizeof(caps));
201
caps.can_tag_objects = 1;
202
caps.can_generate_object_free_events = 1;
203
caps.can_generate_vm_object_alloc_events = 1;
204
if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
205
return JNI_ERR;
206
}
207
208
memset(&eventCallbacks,0, sizeof(eventCallbacks));
209
eventCallbacks.ObjectFree = objectFreeHandler;
210
eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;
211
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
212
return JNI_ERR;
213
}
214
215
if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
216
return JNI_ERR;
217
}
218
219
NSK_DISPLAY1("%s: initialization was done\n", agentName);
220
221
if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
222
return JNI_ERR;
223
224
return JNI_OK;
225
}
226
227
}
228
229