Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach009/attach009Agent00.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 ClassLoad event for class FirstLoadedClass, from handler for this event
36
* disable ClassLoad events for all threads except thread loading FirstLoadedClass
37
* (after these ClassLoad events should come only from this thread)
38
* - receive ClassLoad event for class LastLoadedClass and finish work
39
*/
40
41
static Options* options = NULL;
42
static const char* agentName;
43
44
#define FIRST_LOADED_CLASS "Lnsk/jvmti/AttachOnDemand/attach009/FirstLoadedClass;"
45
#define LAST_LOADED_CLASS "Lnsk/jvmti/AttachOnDemand/attach009/LastLoadedClass;"
46
47
static int disabledForOthers = 0;
48
49
static volatile int success = 1;
50
51
void JNICALL
52
classLoadHandler(jvmtiEnv *jvmti,
53
JNIEnv* jni,
54
jthread thread,
55
jclass klass) {
56
static char mainThreadName[MAX_STRING_LENGTH];
57
char loadedClassName[MAX_STRING_LENGTH];
58
char threadName[MAX_STRING_LENGTH];
59
60
if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
61
nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);
62
return;
63
}
64
65
if (!nsk_jvmti_aod_getClassName(jvmti, klass, loadedClassName)) {
66
nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);
67
return;
68
}
69
70
NSK_DISPLAY2("Class '%s' was loaded by thread '%s'\n", loadedClassName, threadName);
71
72
/*
73
* When class FIRST_LOADED_CLASS was loaded try to disable events for all threads
74
* except main target application thread
75
*/
76
if (strcmp(loadedClassName, FIRST_LOADED_CLASS) == 0) {
77
strcpy(mainThreadName, threadName);
78
79
if (!nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD)) {
80
NSK_COMPLAIN0("Failed to disable events\n");
81
nsk_aod_agentFinished(jni, agentName, 0);
82
return;
83
}
84
85
if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, thread))) {
86
NSK_COMPLAIN1("Failed to enable events for thread '%s'\n", mainThreadName);
87
nsk_aod_agentFinished(jni, agentName, 0);
88
return;
89
}
90
91
NSK_DISPLAY1("ClassLoad events are enabled only for thread '%s'", mainThreadName);
92
93
disabledForOthers = 1;
94
95
return;
96
}
97
98
if (disabledForOthers) {
99
if (strcmp(threadName, mainThreadName) != 0) {
100
success = 0;
101
NSK_COMPLAIN1("ClassLoad event was erroneously generated for thread '%s'\n", threadName);
102
}
103
}
104
105
/*
106
* Stop agent when LAST_LOADED_CLASS was loaded
107
*/
108
if (strcmp(loadedClassName, LAST_LOADED_CLASS) == 0) {
109
nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, success, jvmti, jni);
110
}
111
}
112
113
114
#ifdef STATIC_BUILD
115
JNIEXPORT jint JNI_OnLoad_attach009Agent00(JavaVM *jvm, char *options, void *reserved) {
116
return JNI_VERSION_1_8;
117
}
118
#endif
119
120
JNIEXPORT jint JNICALL
121
#ifdef STATIC_BUILD
122
Agent_OnAttach_attach009Agent00(JavaVM *vm, char *optionsString, void *reserved)
123
#else
124
Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
125
#endif
126
{
127
jvmtiEnv* jvmti;
128
jvmtiEventCallbacks eventCallbacks;
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 NSK_FALSE;
140
141
jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
142
if (!NSK_VERIFY(jvmti != NULL))
143
return JNI_ERR;
144
145
memset(&eventCallbacks,0, sizeof(eventCallbacks));
146
eventCallbacks.ClassLoad = classLoadHandler;
147
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
148
return JNI_ERR;
149
}
150
151
if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {
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