Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/jvmti/libTestGetLoadedClasses.c
32285 views
/*1* Copyright (c) 2019, 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#endif4243static const char *EXC_CNAME = "java/lang/Exception";4445static jvmtiEnv *jvmti = NULL;4647static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);4849JNIEXPORT50jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {51return Agent_Initialize(jvm, options, reserved);52}5354JNIEXPORT55jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {56return Agent_Initialize(jvm, options, reserved);57}5859JNIEXPORT60jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {61return JNI_VERSION_1_8;62}6364static65jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {66jvmtiCapabilities capabilities;67jint res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),68JVMTI_VERSION);69if (res != JNI_OK || jvmti == NULL) {70printf(" Error: wrong result of a valid call to GetEnv!\n");71return JNI_ERR;72}7374(void)memset(&capabilities, 0, sizeof(capabilities));75capabilities.can_tag_objects = 1;76capabilities.can_generate_garbage_collection_events = 1;77(*jvmti)->AddCapabilities(jvmti, &capabilities);7879return JNI_OK;80}8182static83void throw_exc(JNIEnv *env, char *msg) {84jclass exc_class = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env, EXC_CNAME));85jint rt = JNI_OK;8687if (exc_class == NULL) {88printf("throw_exc: Error in FindClass(env, %s)\n", EXC_CNAME);89return;90}91rt = JNI_ENV_PTR(env)->ThrowNew(JNI_ENV_ARG(env, exc_class), msg);92if (rt == JNI_ERR) {93printf("throw_exc: Error in JNI ThrowNew(env, %s)\n", msg);94}95}9697JNIEXPORT jint JNICALL98Java_TestGetLoadedClasses_getLoadedClasses(JNIEnv *env, jclass cls) {99jint totalCount = 0;100jclass* classes;101if (jvmti == NULL) {102throw_exc(env, "JVMTI client was not properly loaded!\n");103return 0;104}105106(*jvmti)->GetLoadedClasses(jvmti, &totalCount, &classes);107(*jvmti)->Deallocate(jvmti, (unsigned char*)classes);108return totalCount;109}110111#ifdef __cplusplus112}113#endif114115116