1 /*
2  * Copyright (c) 2017, Red Hat, Inc. All rights reserved.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.
7  *
8  * This code is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * version 2 for more details (a copy is included in the LICENSE file that
12  * accompanied this code).
13  *
14  * You should have received a copy of the GNU General Public License version
15  * 2 along with this work; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
19  * or visit www.oracle.com if you need additional information or have any
20  * questions.
21  *
22  */
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include "jvmti.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #ifndef JNI_ENV_ARG
33 
34 #ifdef __cplusplus
35 #define JNI_ENV_ARG(x, y) y
36 #define JNI_ENV_PTR(x) x
37 #else
38 #define JNI_ENV_ARG(x,y) x, y
39 #define JNI_ENV_PTR(x) (*x)
40 #endif
41 
42 #endif
43 
44 #define TranslateError(err) "JVMTI error"
45 
46 #define PASSED 0
47 #define FAILED 2
48 
49 static const char *EXC_CNAME = "java/lang/Exception";
50 
51 static jvmtiEnv *jvmti = NULL;
52 static jint result = PASSED;
53 
54 static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);
55 
56 JNIEXPORT
Agent_OnLoad(JavaVM * jvm,char * options,void * reserved)57 jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
58     return Agent_Initialize(jvm, options, reserved);
59 }
60 
61 JNIEXPORT
Agent_OnAttach(JavaVM * jvm,char * options,void * reserved)62 jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
63     return Agent_Initialize(jvm, options, reserved);
64 }
65 
66 JNIEXPORT
JNI_OnLoad(JavaVM * jvm,void * reserved)67 jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {
68     return JNI_VERSION_1_8;
69 }
70 
71 static
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)72 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
73     jvmtiCapabilities capabilities;
74     jint res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
75                                         JVMTI_VERSION_9);
76     if (res != JNI_OK || jvmti == NULL) {
77         printf("    Error: wrong result of a valid call to GetEnv!\n");
78         return JNI_ERR;
79     }
80 
81     (void)memset(&capabilities, 0, sizeof(capabilities));
82     capabilities.can_tag_objects = 1;
83     capabilities.can_generate_garbage_collection_events = 1;
84     (*jvmti)->AddCapabilities(jvmti, &capabilities);
85 
86     return JNI_OK;
87 }
88 
89 static
throw_exc(JNIEnv * env,char * msg)90 void throw_exc(JNIEnv *env, char *msg) {
91     jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));
92     jint rt = JNI_OK;
93 
94     if (exc_class == NULL) {
95         printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);
96         return;
97     }
98     rt = JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);
99     if (rt == JNI_ERR) {
100         printf("throw_exc: Error in JNI ThrowNew(env, %s)\n", msg);
101     }
102 }
103 
heap_iter_callback(jlong class_tag,jlong size,jlong * tag_ptr,jint length,void * user_data)104 static jint JNICALL heap_iter_callback(jlong class_tag,
105                                jlong size,
106                                jlong* tag_ptr,
107                                jint length,
108                                void* user_data) {
109   (*((jint*)(user_data)))++;
110   return JVMTI_VISIT_OBJECTS;
111 }
112 
113 JNIEXPORT jint JNICALL
Java_TestHeapDump_heapdump(JNIEnv * env,jclass cls,jclass filter_cls)114 Java_TestHeapDump_heapdump(JNIEnv *env, jclass cls, jclass filter_cls) {
115     jvmtiHeapCallbacks callbacks;
116     jint totalCount = 0;
117     if (jvmti == NULL) {
118         throw_exc(env, "JVMTI client was not properly loaded!\n");
119         return 0;
120     }
121 
122     (void)memset(&callbacks, 0, sizeof(callbacks));
123     callbacks.heap_iteration_callback = &heap_iter_callback;
124     (*jvmti)->IterateThroughHeap(jvmti, 0, filter_cls, &callbacks, (const void *)&totalCount);
125     return totalCount;
126 }
127 
128 #ifdef __cplusplus
129 }
130 #endif
131