Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach014/attach014Agent00.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
#define LOADED_CLASS_NAME "Lnsk/jvmti/AttachOnDemand/attach014/ClassToLoad;"
34
35
/*
36
* Expected agent work scenario:
37
* - during initialization agent enables ClassLoad events
38
* - target application loads class 'ClassToLoad'
39
* - agent receives ClassLoad event for this class, calls DisposeEnvironment and finishes work
40
*/
41
42
static Options* options = NULL;
43
static const char* agentName;
44
45
void JNICALL
46
classLoadHandler(jvmtiEnv *jvmti,
47
JNIEnv* jni,
48
jthread thread,
49
jclass klass) {
50
char className[MAX_STRING_LENGTH];
51
52
if (!nsk_jvmti_aod_getClassName(jvmti, klass, className)) {
53
nsk_jvmti_aod_disableEventAndFinish(agentName, JVMTI_EVENT_CLASS_LOAD, 0, jvmti, jni);
54
return;
55
}
56
57
NSK_DISPLAY2("%s: ClassLoad event was received for class '%s'\n", agentName, className);
58
59
if (!strcmp(className, LOADED_CLASS_NAME)) {
60
int success = 1;
61
62
if (!nsk_jvmti_aod_disableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))
63
success = 0;
64
65
if (!NSK_JVMTI_VERIFY(jvmti->DisposeEnvironment())) {
66
success = 0;
67
NSK_COMPLAIN1("%s: failed to dispose environment\n", agentName);
68
} else {
69
NSK_DISPLAY1("%s: jvmti env was disposed\n", agentName);
70
}
71
72
nsk_aod_agentFinished(jni, agentName, success);
73
}
74
}
75
76
77
#ifdef STATIC_BUILD
78
JNIEXPORT jint JNI_OnLoad_attach014Agent00(JavaVM *jvm, char *options, void *reserved) {
79
return JNI_VERSION_1_8;
80
}
81
#endif
82
83
JNIEXPORT jint JNICALL
84
#ifdef STATIC_BUILD
85
Agent_OnAttach_attach014Agent00(JavaVM *vm, char *optionsString, void *reserved)
86
#else
87
Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
88
#endif
89
{
90
jvmtiEventCallbacks eventCallbacks;
91
jvmtiEnv* jvmti = NULL;
92
JNIEnv* jni = NULL;
93
94
options = (Options*) nsk_aod_createOptions(optionsString);
95
if (!NSK_VERIFY(options != NULL))
96
return JNI_ERR;
97
98
agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
99
100
jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
101
if (jni == NULL)
102
return NSK_FALSE;
103
104
jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
105
if (!NSK_VERIFY(jvmti != NULL))
106
return JNI_ERR;
107
108
memset(&eventCallbacks,0, sizeof(eventCallbacks));
109
eventCallbacks.ClassLoad = classLoadHandler;
110
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
111
return JNI_ERR;
112
}
113
114
if (!(nsk_jvmti_aod_enableEvent(jvmti, JVMTI_EVENT_CLASS_LOAD))) {
115
return JNI_ERR;
116
}
117
118
NSK_DISPLAY1("%s: initialization was done\n", agentName);
119
120
if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
121
return JNI_ERR;
122
123
return JNI_OK;
124
}
125
126
}
127
128