Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach008/attach008Agent00.cpp
40951 views
1
/*
2
* Copyright (c) 2007, 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
* Expected agent work scenario:
35
* - receive MonitorContentedEnter event for thread ThreadGeneratingEvents
36
* - receive MonitorContentedEntered event for thread ThreadGeneratingEvents and finish work
37
*/
38
39
#define THREAD_GENERATING_EVENTS_NAME "ThreadGeneratingEvents"
40
41
static Options* options = NULL;
42
static const char* agentName;
43
44
static jvmtiEvent testEvents[] = { JVMTI_EVENT_MONITOR_CONTENDED_ENTER, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED };
45
static const int testEventsNumber = 2;
46
47
static volatile int monitorEnter = 0;
48
49
static int success = 1;
50
51
void JNICALL monitorContentedEnterHandler(jvmtiEnv * jvmti,
52
JNIEnv * jni,
53
jthread thread,
54
jobject object) {
55
char threadName[MAX_STRING_LENGTH];
56
57
if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
58
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
59
return;
60
}
61
62
NSK_DISPLAY2("%s: MonitorContentedEnter event received for thread '%s'\n", agentName, threadName);
63
64
if (!strcmp(threadName, THREAD_GENERATING_EVENTS_NAME)) {
65
monitorEnter = 1;
66
}
67
}
68
69
void JNICALL monitorContentedEnteredHandler(
70
jvmtiEnv * jvmti,
71
JNIEnv * jni,
72
jthread thread,
73
jobject object) {
74
char threadName[MAX_STRING_LENGTH];
75
76
if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
77
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
78
return;
79
}
80
81
NSK_DISPLAY2("%s: MonitorContentedEntered event received for thread '%s'\n", agentName, threadName);
82
83
if (!strcmp(threadName, THREAD_GENERATING_EVENTS_NAME)) {
84
int success = 1;
85
86
if (!monitorEnter) {
87
success = 0;
88
NSK_COMPLAIN2("%s: MonitorContentedEnter event wasn't received for thread %s\n", agentName, threadName);
89
}
90
91
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
92
}
93
}
94
95
#ifdef STATIC_BUILD
96
JNIEXPORT jint JNI_OnLoad_attach008Agent00(JavaVM *jvm, char *options, void *reserved) {
97
return JNI_VERSION_1_8;
98
}
99
#endif
100
101
JNIEXPORT jint JNICALL
102
#ifdef STATIC_BUILD
103
Agent_OnAttach_attach008Agent00(JavaVM *vm, char *optionsString, void *reserved)
104
#else
105
Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
106
#endif
107
{
108
jvmtiEventCallbacks eventCallbacks;
109
jvmtiCapabilities caps;
110
jvmtiEnv* jvmti = NULL;
111
JNIEnv* jni = NULL;
112
113
options = (Options*) nsk_aod_createOptions(optionsString);
114
if (!NSK_VERIFY(options != NULL))
115
return JNI_ERR;
116
117
agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
118
119
jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
120
if (jni == NULL)
121
return NSK_FALSE;
122
123
jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
124
if (!NSK_VERIFY(jvmti != NULL))
125
return JNI_ERR;
126
127
memset(&caps, 0, sizeof(caps));
128
caps.can_generate_monitor_events = 1;
129
if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
130
return JNI_ERR;
131
}
132
133
memset(&eventCallbacks,0, sizeof(eventCallbacks));
134
eventCallbacks.MonitorContendedEntered = monitorContentedEnteredHandler;
135
eventCallbacks.MonitorContendedEnter = monitorContentedEnterHandler;
136
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
137
return JNI_ERR;
138
}
139
140
if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
141
return JNI_ERR;
142
}
143
144
NSK_DISPLAY1("%s: initialization was done\n", agentName);
145
146
if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
147
return JNI_ERR;
148
149
return JNI_OK;
150
}
151
152
}
153
154