Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/gc/shenandoah/jvmti/libTestHeapDump.c
41153 views
1
/*
2
* Copyright (c) 2017, 2018, Red Hat, Inc. 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
25
#include <stdio.h>
26
#include <string.h>
27
#include "jvmti.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 TranslateError(err) "JVMTI error"
46
47
#define PASSED 0
48
#define FAILED 2
49
50
static const char *EXC_CNAME = "java/lang/Exception";
51
52
static jvmtiEnv *jvmti = NULL;
53
static jint result = PASSED;
54
55
static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
56
57
JNIEXPORT
58
jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
59
return Agent_Initialize(jvm, options, reserved);
60
}
61
62
JNIEXPORT
63
jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
64
return Agent_Initialize(jvm, options, reserved);
65
}
66
67
JNIEXPORT
68
jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
69
return JNI_VERSION_1_8;
70
}
71
72
static
73
jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
74
jvmtiCapabilities capabilities;
75
jint res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
76
JVMTI_VERSION_9);
77
if (res != JNI_OK || jvmti == NULL) {
78
printf(" Error: wrong result of a valid call to GetEnv!\n");
79
return JNI_ERR;
80
}
81
82
(void)memset(&capabilities, 0, sizeof(capabilities));
83
capabilities.can_tag_objects = 1;
84
capabilities.can_generate_garbage_collection_events = 1;
85
(*jvmti)->AddCapabilities(jvmti, &capabilities);
86
87
return JNI_OK;
88
}
89
90
static
91
void throw_exc(JNIEnv *env, char *msg) {
92
jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));
93
jint rt = JNI_OK;
94
95
if (exc_class == NULL) {
96
printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);
97
return;
98
}
99
rt = JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);
100
if (rt == JNI_ERR) {
101
printf("throw_exc: Error in JNI ThrowNew(env, %s)\n", msg);
102
}
103
}
104
105
static jint JNICALL heap_iter_callback(jlong class_tag,
106
jlong size,
107
jlong* tag_ptr,
108
jint length,
109
void* user_data) {
110
(*((jint*)(user_data)))++;
111
return JVMTI_VISIT_OBJECTS;
112
}
113
114
JNIEXPORT jint JNICALL
115
Java_TestHeapDump_heapdump(JNIEnv *env, jclass cls, jclass filter_cls) {
116
jvmtiHeapCallbacks callbacks;
117
jint totalCount = 0;
118
if (jvmti == NULL) {
119
throw_exc(env, "JVMTI client was not properly loaded!\n");
120
return 0;
121
}
122
123
(void)memset(&callbacks, 0, sizeof(callbacks));
124
callbacks.heap_iteration_callback = &heap_iter_callback;
125
(*jvmti)->IterateThroughHeap(jvmti, 0, filter_cls, &callbacks, (const void *)&totalCount);
126
return totalCount;
127
}
128
129
#ifdef __cplusplus
130
}
131
#endif
132
133