Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach002a/attach002aAgent00.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 events VMObjectAlloc
36
* - agent receives VMObjectAlloc event for instance of InterruptedException,
37
* redefines class InterruptedException and finishes work
38
*/
39
40
#define REDEFINED_CLASS_NAME "Ljava/lang/InterruptedException;"
41
#define REDEFINED_CLASS_FILE_NAME "java/lang/InterruptedException"
42
43
static Options* options = NULL;
44
static const char* agentName;
45
46
static jvmtiEvent testEvents[] = { JVMTI_EVENT_VM_OBJECT_ALLOC };
47
48
static const int testEventsNumber = 1;
49
50
void JNICALL vmObjectAllocHandler(jvmtiEnv * jvmti,
51
JNIEnv * jni,
52
jthread thread,
53
jobject object,
54
jclass object_class,
55
jlong size) {
56
char className[MAX_STRING_LENGTH];
57
58
if (!nsk_jvmti_aod_getClassName(jvmti, object_class, className)) {
59
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, 0, jvmti, jni);
60
return;
61
}
62
63
NSK_DISPLAY2("%s: ObjectAlloc event received (object class: %s)\n", agentName, className);
64
65
if (!strcmp(className, REDEFINED_CLASS_NAME)) {
66
int success = 1;
67
68
if (!NSK_VERIFY(nsk_jvmti_aod_redefineClass(options, jvmti, object_class, REDEFINED_CLASS_FILE_NAME))) {
69
NSK_COMPLAIN1("%s: failed to redefine class\n", agentName);
70
success = 0;
71
}
72
73
nsk_jvmti_aod_disableEventsAndFinish(agentName, testEvents, testEventsNumber, success, jvmti, jni);
74
}
75
}
76
77
#ifdef STATIC_BUILD
78
JNIEXPORT jint JNI_OnLoad_attach002aAgent00(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_attach002aAgent00(JavaVM *vm, char *optionsString, void *reserved)
86
#else
87
Agent_OnAttach(JavaVM *vm, char *optionsString, void *reserved)
88
#endif
89
{
90
jvmtiEventCallbacks eventCallbacks;
91
jvmtiCapabilities caps;
92
jvmtiEnv* jvmti = NULL;
93
JNIEnv* jni = NULL;
94
95
options = (Options*) nsk_aod_createOptions(optionsString);
96
if (!NSK_VERIFY(options != NULL))
97
return JNI_ERR;
98
99
agentName = nsk_aod_getOptionValue(options, NSK_AOD_AGENT_NAME_OPTION);
100
101
jni = (JNIEnv*) nsk_aod_createJNIEnv(vm);
102
if (jni == NULL)
103
return NSK_FALSE;
104
105
jvmti = nsk_jvmti_createJVMTIEnv(vm, reserved);
106
if (!NSK_VERIFY(jvmti != NULL))
107
return JNI_ERR;
108
109
memset(&caps, 0, sizeof(caps));
110
caps.can_redefine_classes = 1;
111
caps.can_generate_vm_object_alloc_events = 1;
112
if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
113
return JNI_ERR;
114
}
115
116
memset(&eventCallbacks,0, sizeof(eventCallbacks));
117
eventCallbacks.VMObjectAlloc = vmObjectAllocHandler;
118
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&eventCallbacks, sizeof(eventCallbacks)))) {
119
return JNI_ERR;
120
}
121
122
if (!(nsk_jvmti_aod_enableEvents(jvmti, testEvents, testEventsNumber))) {
123
return JNI_ERR;
124
}
125
126
NSK_DISPLAY1("%s: initialization was done\n", agentName);
127
128
if (!NSK_VERIFY(nsk_aod_agentLoaded(jni, agentName)))
129
return JNI_ERR;
130
131
return JNI_OK;
132
}
133
134
}
135
136