Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/jvmti/libTestHeapDump.c
32285 views
/*1* Copyright (c) 2017, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223#include <stdio.h>24#include <string.h>25#include "jvmti.h"2627#ifdef __cplusplus28extern "C" {29#endif3031#ifndef JNI_ENV_ARG3233#ifdef __cplusplus34#define JNI_ENV_ARG(x, y) y35#define JNI_ENV_PTR(x) x36#else37#define JNI_ENV_ARG(x,y) x, y38#define JNI_ENV_PTR(x) (*x)39#endif4041#endif4243#define TranslateError(err) "JVMTI error"4445#define PASSED 046#define FAILED 24748static const char *EXC_CNAME = "java/lang/Exception";4950static jvmtiEnv *jvmti = NULL;51static jint result = PASSED;5253static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);5455JNIEXPORT56jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {57return Agent_Initialize(jvm, options, reserved);58}5960JNIEXPORT61jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {62return Agent_Initialize(jvm, options, reserved);63}6465JNIEXPORT66jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {67return JNI_VERSION_1_8;68}6970static71jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {72jvmtiCapabilities capabilities;73jint res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),74JVMTI_VERSION);75if (res != JNI_OK || jvmti == NULL) {76printf(" Error: wrong result of a valid call to GetEnv!\n");77return JNI_ERR;78}7980(void)memset(&capabilities, 0, sizeof(capabilities));81capabilities.can_tag_objects = 1;82capabilities.can_generate_garbage_collection_events = 1;83(*jvmti)->AddCapabilities(jvmti, &capabilities);8485return JNI_OK;86}8788static89void throw_exc(JNIEnv *env, char *msg) {90jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));91jint rt = JNI_OK;9293if (exc_class == NULL) {94printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);95return;96}97rt = JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);98if (rt == JNI_ERR) {99printf("throw_exc: Error in JNI ThrowNew(env, %s)\n", msg);100}101}102103static jint JNICALL heap_iter_callback(jlong class_tag,104jlong size,105jlong* tag_ptr,106jint length,107void* user_data) {108(*((jint*)(user_data)))++;109return JVMTI_VISIT_OBJECTS;110}111112JNIEXPORT jint JNICALL113Java_TestHeapDump_heapdump(JNIEnv *env, jclass cls, jclass filter_cls) {114jvmtiHeapCallbacks callbacks;115jint totalCount = 0;116if (jvmti == NULL) {117throw_exc(env, "JVMTI client was not properly loaded!\n");118return 0;119}120121(void)memset(&callbacks, 0, sizeof(callbacks));122callbacks.heap_iteration_callback = &heap_iter_callback;123(*jvmti)->IterateThroughHeap(jvmti, 0, filter_cls, &callbacks, (const void *)&totalCount);124return totalCount;125}126127#ifdef __cplusplus128}129#endif130131132