Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach039/attach039Agent00.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 ThreadStart and ThreadEnd events and starts new
36
* thread using JVMTI RunAgentThread
37
* - agent receives ThreadStart event for started thread
38
- agent receives ThreadEnd event for started thread and finishes work
39
*/
40
41
#define STARTED_THREAD_NAME "ThreadStartedByAgent"
42
43
static Options* options = NULL;
44
static const char* agentName;
45
46
static jvmtiEvent testEvents[] = { JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END };
47
static const int testEventsNumber = 2;
48
49
volatile int threadStartReceived = 0;
50
volatile int threadWasExecuted = 0;
51
52
void JNICALL startedThreadFunction(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
53
char threadName[MAX_STRING_LENGTH];
54
55
if (!nsk_jvmti_aod_getThreadName(jvmti, NULL, threadName)) {
56
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
57
return;
58
}
59
60
NSK_DISPLAY2("%s: thread started from agent is running (thread name: '%s')\n", agentName, threadName);
61
62
threadWasExecuted = 1;
63
}
64
65
int startNewThread(jvmtiEnv* jvmti, JNIEnv* jni) {
66
jthread thread;
67
68
thread = nsk_jvmti_aod_createThreadWithName(jni, STARTED_THREAD_NAME);
69
if (!NSK_VERIFY(thread != NULL))
70
return NSK_FALSE;
71
72
if (!NSK_JVMTI_VERIFY(jvmti->RunAgentThread(thread, startedThreadFunction, NULL, JVMTI_THREAD_NORM_PRIORITY))) {
73
return NSK_FALSE;
74
}
75
76
NSK_DISPLAY1("%s: new thread was started\n", agentName);
77
78
return NSK_TRUE;
79
}
80
81
void JNICALL threadEndHandler(jvmtiEnv *jvmti,
82
JNIEnv* jni,
83
jthread thread) {
84
char threadName[MAX_STRING_LENGTH];
85
86
if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
87
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
88
return;
89
}
90
91
NSK_DISPLAY2("%s: ThreadEnd event was received for thread '%s'\n", agentName, threadName);
92
93
if (!strcmp(STARTED_THREAD_NAME, threadName)) {
94
int success = 1;
95
96
if (!threadStartReceived) {
97
NSK_COMPLAIN1("%s: ThreadStart event wasn't received\n", agentName);
98
success = 0;
99
}
100
101
if (!threadWasExecuted) {
102
NSK_COMPLAIN1("%s: started thread haven't execute its code\n", agentName);
103
success = 0;
104
}
105
106
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
107
}
108
}
109
110
void JNICALL threadStartHandler(jvmtiEnv *jvmti,
111
JNIEnv* jni,
112
jthread thread) {
113
char threadName[MAX_STRING_LENGTH];
114
115
if (!nsk_jvmti_aod_getThreadName(jvmti, thread, threadName)) {
116
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
117
return;
118
}
119
120
NSK_DISPLAY2("%s: ThreadStart event was received for thread '%s'\n", agentName, threadName);
121
122
if (!strcmp(STARTED_THREAD_NAME, threadName)) {
123
threadStartReceived = 1;
124
}
125
}
126
127
#ifdef STATIC_BUILD
128
JNIEXPORT jint JNI_OnLoad_attach039Agent00(JavaVM *jvm, char *options, void *reserved) {
129
return JNI_VERSION_1_8;
130
}
131
#endif
132
133
JNIEXPORT jint JNICALL
134
#ifdef STATIC_BUILD
135
Agent_OnAttach_attach039Agent00(JavaVM *vm, char *optionsString, void *reserved)
136
#else
137
Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
138
#endif
139
{
140
jvmtiEventCallbacks eventCallbacks;
141
jvmtiEnv* jvmti;
142
JNIEnv* jni;
143
144
options = (Options*) nsk_aod_createOptions(optionsString);
145
if (!NSK_VERIFY(options != NULL))
146
return JNI_ERR;
147
148
agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
149
150
jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
151
if (jni == NULL)
152
return JNI_ERR;
153
154
jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
155
if (!NSK_VERIFY(jvmti != NULL))
156
return JNI_ERR;
157
158
memset(&eventCallbacks,0, sizeof(eventCallbacks));
159
eventCallbacks.ThreadEnd = threadEndHandler;
160
eventCallbacks.ThreadStart = threadStartHandler;
161
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
162
return JNI_ERR;
163
}
164
165
if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
166
return JNI_ERR;
167
}
168
169
NSK_DISPLAY1("%s: initialization was done\n", agentName);
170
171
if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
172
return JNI_ERR;
173
174
NSK_DISPLAY1("%s: starting new thread\n", agentName);
175
176
if (!NSK_VERIFY(startNewThread(jvmti, jni)))
177
return JNI_ERR;
178
179
return JNI_OK;
180
}
181
182
}
183
184