Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/CanGenerateAllClassHook/libCanGenerateAllClassHook.c
41155 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. 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*/2223#include <stdio.h>24#include <string.h>25#include "jvmti.h"26#include "jni.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#endif434445static jvmtiEnv *jvmti = NULL;4647typedef enum {48Yes = 1,49No = 0,50Error = -151} IsAvail;5253static IsAvail onLoadIsAvail = Error;5455void reportError(const char *msg, int err) {56printf("%s, error: %d\n", msg, err);57}5859static IsAvail isClassHookAvail() {60IsAvail result = Error;61do {62jvmtiCapabilities caps;63jvmtiPhase phase;64jvmtiError err;6566if (jvmti == NULL) {67reportError("jvmti is NULL", -1);68break;69}7071err = (*jvmti)->GetPhase(jvmti, &phase);72if (err != JVMTI_ERROR_NONE) {73reportError("GetPhase failed", err);74break;75}7677err = (*jvmti)->GetPotentialCapabilities(jvmti, &caps);78if (err != JVMTI_ERROR_NONE) {79reportError("GetPotentialCapabilities failed", err);80break;81}8283result = caps.can_generate_all_class_hook_events ? Yes : No;8485printf("isClassHookAvail: phase=%d, value=%d\n", phase, result);86} while (0);87return result;88}899091JNIEXPORT jint JNICALL92Agent_OnLoad(JavaVM *jvm, char *options, void *reserved)93{94jint res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), JVMTI_VERSION_9);95if (res != JNI_OK || jvmti == NULL) {96reportError("GetEnv failed", res);97return JNI_ERR;98}99100// check and save can_generate_all_class_hook_events for ONLOAD phase101onLoadIsAvail = isClassHookAvail();102103return JNI_OK;104}105106JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {107return JNI_VERSION_9;108}109110111JNIEXPORT jint JNICALL112Java_CanGenerateAllClassHook_getClassHookAvail(JNIEnv *env, jclass cls) {113return isClassHookAvail();114}115116JNIEXPORT jint JNICALL117Java_CanGenerateAllClassHook_getOnLoadClassHookAvail(JNIEnv *env, jclass cls) {118return onLoadIsAvail;119}120121122#ifdef __cplusplus123}124#endif125126127128