Path: blob/master/test/hotspot/jtreg/gc/shenandoah/jvmti/libTestHeapDump.c
41153 views
/*1* Copyright (c) 2017, 2018, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include <stdio.h>25#include <string.h>26#include "jvmti.h"2728#ifdef __cplusplus29extern "C" {30#endif3132#ifndef JNI_ENV_ARG3334#ifdef __cplusplus35#define JNI_ENV_ARG(x, y) y36#define JNI_ENV_PTR(x) x37#else38#define JNI_ENV_ARG(x,y) x, y39#define JNI_ENV_PTR(x) (*x)40#endif4142#endif4344#define TranslateError(err) "JVMTI error"4546#define PASSED 047#define FAILED 24849static const char *EXC_CNAME = "java/lang/Exception";5051static jvmtiEnv *jvmti = NULL;52static jint result = PASSED;5354static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);5556JNIEXPORT57jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {58return Agent_Initialize(jvm, options, reserved);59}6061JNIEXPORT62jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {63return Agent_Initialize(jvm, options, reserved);64}6566JNIEXPORT67jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {68return JNI_VERSION_1_8;69}7071static72jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {73jvmtiCapabilities capabilities;74jint res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),75JVMTI_VERSION_9);76if (res != JNI_OK || jvmti == NULL) {77printf(" Error: wrong result of a valid call to GetEnv!\n");78return JNI_ERR;79}8081(void)memset(&capabilities, 0, sizeof(capabilities));82capabilities.can_tag_objects = 1;83capabilities.can_generate_garbage_collection_events = 1;84(*jvmti)->AddCapabilities(jvmti, &capabilities);8586return JNI_OK;87}8889static90void throw_exc(JNIEnv *env, char *msg) {91jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));92jint rt = JNI_OK;9394if (exc_class == NULL) {95printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);96return;97}98rt = JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);99if (rt == JNI_ERR) {100printf("throw_exc: Error in JNI ThrowNew(env, %s)\n", msg);101}102}103104static jint JNICALL heap_iter_callback(jlong class_tag,105jlong size,106jlong* tag_ptr,107jint length,108void* user_data) {109(*((jint*)(user_data)))++;110return JVMTI_VISIT_OBJECTS;111}112113JNIEXPORT jint JNICALL114Java_TestHeapDump_heapdump(JNIEnv *env, jclass cls, jclass filter_cls) {115jvmtiHeapCallbacks callbacks;116jint totalCount = 0;117if (jvmti == NULL) {118throw_exc(env, "JVMTI client was not properly loaded!\n");119return 0;120}121122(void)memset(&callbacks, 0, sizeof(callbacks));123callbacks.heap_iteration_callback = &heap_iter_callback;124(*jvmti)->IterateThroughHeap(jvmti, 0, filter_cls, &callbacks, (const void *)&totalCount);125return totalCount;126}127128#ifdef __cplusplus129}130#endif131132133