Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetEnv/GetEnv001/GetEnv001.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
24
#include <stdio.h>
25
#include <string.h>
26
#include <stdlib.h>
27
#include <jvmti.h>
28
#include "agent_common.h"
29
#include <jvmti_tools.h>
30
#include "JVMTITools.h"
31
32
extern "C" {
33
34
/* ============================================================================= */
35
36
static int loaded_classes_num = 0;
37
38
/* ============================================================================= */
39
static void JNICALL
40
ClassFileLoadHook (
41
jvmtiEnv *jvmti
42
, JNIEnv *jni
43
, jclass class_being_redefined
44
, jobject loader
45
, const char* name
46
, jobject protection_domain
47
, jint class_data_len
48
, const unsigned char* class_data
49
, jint *new_class_data_len
50
, unsigned char** new_class_data
51
)
52
{
53
loaded_classes_num++;
54
55
NSK_DISPLAY2("ClassFileLoadHook: class \"%s\", loader %X\n"
56
, name
57
, loader
58
);
59
}
60
61
/* ============================================================================= */
62
JNIEXPORT jint JNICALL
63
Java_nsk_jvmti_GetEnv_GetEnv001_GetEnv001_getLoadedClassesCount(JNIEnv *env, jobject owner) {
64
return loaded_classes_num;
65
}
66
67
/* ============================================================================= */
68
/* Agent initialization procedure */
69
#ifdef STATIC_BUILD
70
JNIEXPORT jint JNICALL Agent_OnLoad_GetEnv001(JavaVM *jvm, char *options, void *reserved) {
71
return Agent_Initialize(jvm, options, reserved);
72
}
73
JNIEXPORT jint JNICALL Agent_OnAttach_GetEnv001(JavaVM *jvm, char *options, void *reserved) {
74
return Agent_Initialize(jvm, options, reserved);
75
}
76
JNIEXPORT jint JNI_OnLoad_GetEnv001(JavaVM *jvm, char *options, void *reserved) {
77
return JNI_VERSION_1_8;
78
}
79
#endif
80
jint Agent_Initialize(JavaVM *vm, char *options, void *reserved)
81
{
82
jvmtiEventCallbacks callbacks;
83
jvmtiCapabilities caps;
84
jvmtiEnv* jvmti;
85
86
if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) {
87
return JNI_ERR;
88
}
89
90
if (vm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1) != JNI_OK || jvmti == NULL) {
91
NSK_COMPLAIN0("JVMTI_VERSION_1_1 isn't supported.");
92
return JNI_OK;
93
}
94
95
if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&caps)))
96
return JNI_ERR;
97
98
caps.can_retransform_classes = 1;
99
100
// Register all necessary JVM capabilities
101
if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
102
return JNI_ERR;
103
104
// Register all necessary event callbacks
105
memset(&callbacks, 0, sizeof(callbacks));
106
callbacks.ClassFileLoadHook = &ClassFileLoadHook;
107
108
if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
109
return JNI_ERR;
110
111
// Enable class retransformation
112
if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
113
return JNI_ERR;
114
115
return JNI_OK;
116
}
117
118
}
119
120