Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach045/attach045Agent03.cpp
40955 views
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 VMObjectAlloc events and finishes work
35
* (events should be provoked by target application)
36
*/
37
38
#define EXPECTED_EVENTS_NUMBER 500
39
40
static Options* options = NULL;
41
static const char* agentName;
42
43
static jvmtiEvent testEvents[] = { JVMTI_EVENT_VM_OBJECT_ALLOC };
44
static const int testEventsNumber = 1;
45
46
static jrawMonitorID eventsCounterMonitor;
47
48
static volatile int eventsCounter = 0;
49
50
void JNICALL
51
VMObjectAllocHandler(jvmtiEnv *jvmti,
52
JNIEnv* jni,
53
jthread thread,
54
jobject object,
55
jclass object_klass,
56
jlong size) {
57
char threadName[MAX_STRING_LENGTH];
58
char className[MAX_STRING_LENGTH];
59
int success = 1;
60
61
if (!nsk_jvmti_aod_getClassName(jvmti, object_klass, className)) {
62
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
63
return;
64
}
65
66
if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
67
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
68
return;
69
}
70
71
if (NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventsCounterMonitor))) {
72
73
eventsCounter++;
74
75
NSK_DISPLAY4("%s: VMObjectAlloc received in thread '%s' for instance of '%s' (eventsCounter: %d)\n",
76
agentName, threadName, className, eventsCounter);
77
78
if ((eventsCounter % 10) == 0) {
79
NSK_DISPLAY1("%s: force garbage collection\n", agentName);
80
81
if (!NSK_JVMTI_VERIFY(jvmti->ForceGarbageCollection()))
82
success = 0;
83
}
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(jvmti->RawMonitorExit(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
103
#ifdef STATIC_BUILD
104
JNIEXPORT jint JNI_OnLoad_attach045Agent03(JavaVM *jvm, char *options, void *reserved) {
105
return JNI_VERSION_1_8;
106
}
107
#endif
108
109
JNIEXPORT jint JNICALL
110
#ifdef STATIC_BUILD
111
Agent_OnAttach_attach045Agent03(JavaVM *vm, char *optionsString, void *reserved)
112
#else
113
Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
114
#endif
115
{
116
jvmtiEventCallbacks eventCallbacks;
117
jvmtiEnv* jvmti;
118
jvmtiCapabilities caps;
119
JNIEnv* jni;
120
121
options = (Options*) nsk_aod_createOptions(optionsString);
122
if (!NSK_VERIFY(options != NULL))
123
return JNI_ERR;
124
125
agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
126
127
jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
128
if (jni == NULL)
129
return JNI_ERR;
130
131
jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
132
if (!NSK_VERIFY(jvmti != NULL))
133
return JNI_ERR;
134
135
if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("attach045-agent03-eventsCounterMonitor", &eventsCounterMonitor))) {
136
return JNI_ERR;
137
}
138
139
memset(&caps, 0, sizeof(caps));
140
caps.can_generate_vm_object_alloc_events = 1;
141
if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
142
return JNI_ERR;
143
}
144
145
memset(&eventCallbacks,0, sizeof(eventCallbacks));
146
eventCallbacks.VMObjectAlloc = VMObjectAllocHandler;
147
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
148
return JNI_ERR;
149
}
150
151
if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
152
return JNI_ERR;
153
}
154
155
NSK_DISPLAY1("%s: initialization was done\n", agentName);
156
157
if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
158
return JNI_ERR;
159
160
return JNI_OK;
161
}
162
163
164
}
165
166