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