Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorStackDepthInfo/libGetOwnedMonitorStackDepthInfoWithEATest.c
41153 views
1
/*
2
* Copyright (c) 2019 SAP SE. 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 "jvmti.h"
27
#include "jni.h"
28
29
#ifdef __cplusplus
30
extern "C" {
31
#endif
32
33
#ifndef JNI_ENV_ARG
34
35
#ifdef __cplusplus
36
#define JNI_ENV_ARG(x, y) y
37
#define JNI_ENV_PTR(x) x
38
#else
39
#define JNI_ENV_ARG(x,y) x, y
40
#define JNI_ENV_PTR(x) (*x)
41
#endif
42
43
#endif
44
45
#define FAILED -1
46
47
static jvmtiEnv *jvmti;
48
49
static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
50
51
static void ShowErrorMessage(jvmtiEnv *jvmti, jvmtiError errCode, const char *message) {
52
char *errMsg;
53
jvmtiError result;
54
55
result = (*jvmti)->GetErrorName(jvmti, errCode, &errMsg);
56
if (result == JVMTI_ERROR_NONE) {
57
fprintf(stderr, "%s: %s (%d)\n", message, errMsg, errCode);
58
(*jvmti)->Deallocate(jvmti, (unsigned char *)errMsg);
59
} else {
60
fprintf(stderr, "%s (%d)\n", message, errCode);
61
}
62
}
63
64
JNIEXPORT jint JNICALL
65
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
66
return Agent_Initialize(jvm, options, reserved);
67
}
68
69
JNIEXPORT jint JNICALL
70
Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
71
return Agent_Initialize(jvm, options, reserved);
72
}
73
74
JNIEXPORT jint JNICALL
75
JNI_OnLoad(JavaVM *jvm, void *reserved) {
76
jint res;
77
JNIEnv *env;
78
79
res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &env),
80
JNI_VERSION_9);
81
if (res != JNI_OK || env == NULL) {
82
fprintf(stderr, "Error: GetEnv call failed(%d)!\n", res);
83
return JNI_ERR;
84
}
85
86
return JNI_VERSION_9;
87
}
88
89
static
90
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
91
jint res;
92
jvmtiError err;
93
jvmtiCapabilities caps;
94
95
printf("Agent_OnLoad started\n");
96
97
memset(&caps, 0, sizeof(caps));
98
99
res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
100
JVMTI_VERSION_9);
101
if (res != JNI_OK || jvmti == NULL) {
102
fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n");
103
return JNI_ERR;
104
}
105
106
caps.can_get_owned_monitor_stack_depth_info = 1;
107
108
err = (*jvmti)->AddCapabilities(jvmti, &caps);
109
if (err != JVMTI_ERROR_NONE) {
110
ShowErrorMessage(jvmti, err,
111
"Agent_OnLoad: error in JVMTI AddCapabilities");
112
return JNI_ERR;
113
}
114
115
err = (*jvmti)->GetCapabilities(jvmti, &caps);
116
if (err != JVMTI_ERROR_NONE) {
117
ShowErrorMessage(jvmti, err,
118
"Agent_OnLoad: error in JVMTI GetCapabilities");
119
return JNI_ERR;
120
}
121
122
if (!caps.can_get_owned_monitor_stack_depth_info) {
123
fprintf(stderr, "Warning: GetOwnedMonitorStackDepthInfo is not implemented\n");
124
return JNI_ERR;
125
}
126
127
printf("Agent_OnLoad finished\n");
128
return JNI_OK;
129
}
130
131
JNIEXPORT jint JNICALL
132
Java_GetOwnedMonitorStackDepthInfoWithEATest_getOwnedMonitorStackDepthInfo(JNIEnv *env, jclass cls, jobject targetThread, jobjectArray ownedMonitors, jintArray depths) {
133
jvmtiError err;
134
jvmtiThreadInfo threadInfo;
135
jint monitorCount;
136
jvmtiMonitorStackDepthInfo* stackDepthInfo;
137
jint* depthsPtr;
138
jint idx = 0;
139
140
err = (*jvmti)->GetThreadInfo(jvmti, targetThread, &threadInfo);
141
if (err != JVMTI_ERROR_NONE) {
142
ShowErrorMessage(jvmti, err,
143
"getOwnedMonitorsFor: error in JVMTI GetThreadInfo");
144
return FAILED;
145
}
146
147
err = (*jvmti)->GetOwnedMonitorStackDepthInfo(jvmti, targetThread, &monitorCount, &stackDepthInfo);
148
if (err != JVMTI_ERROR_NONE) {
149
ShowErrorMessage(jvmti, err,
150
"getOwnedMonitorsFor: error in JVMTI GetOwnedMonitorStackDepthInfo");
151
return FAILED;
152
}
153
154
printf("getOwnedMonitorsFor: %s owns %d monitor(s)\n", threadInfo.name, monitorCount);
155
156
depthsPtr = (*env)->GetIntArrayElements(env, depths, NULL);
157
for (idx = 0; idx < monitorCount; idx++) {
158
(*env)->SetObjectArrayElement(env, ownedMonitors, idx, stackDepthInfo[idx].monitor);
159
depthsPtr[idx] = stackDepthInfo[idx].stack_depth;
160
}
161
(*env)->ReleaseIntArrayElements(env, depths, depthsPtr, 0);
162
163
(*jvmti)->Deallocate(jvmti, (unsigned char *) stackDepthInfo);
164
(*jvmti)->Deallocate(jvmti, (unsigned char *) threadInfo.name);
165
return monitorCount;
166
}
167
168
#ifdef __cplusplus
169
}
170
#endif
171
172