Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorInfo/libGetOwnedMonitorInfoTest.c
41155 views
/*1* Copyright (c) 2017, 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#endif4344#define PASSED 045#define FAILED 24647#define TEST_CLASS "GetOwnedMonitorInfoTest"4849static volatile jboolean event_has_posted = JNI_FALSE;50static volatile jint status = PASSED;51static volatile jclass testClass = NULL;5253static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);5455static void ShowErrorMessage(jvmtiEnv *jvmti, jvmtiError errCode, const char *message) {56char *errMsg;57jvmtiError result;5859result = (*jvmti)->GetErrorName(jvmti, errCode, &errMsg);60if (result == JVMTI_ERROR_NONE) {61fprintf(stderr, "%s: %s (%d)\n", message, errMsg, errCode);62(*jvmti)->Deallocate(jvmti, (unsigned char *)errMsg);63} else {64fprintf(stderr, "%s (%d)\n", message, errCode);65}66}6768static jboolean CheckLockObject(JNIEnv *env, jobject monitor) {69if (testClass == NULL) {70// JNI_OnLoad has not been called yet, so can't possibly be an instance of TEST_CLASS.71return JNI_FALSE;72}73return (*env)->IsInstanceOf(env, monitor, testClass);74}7576JNIEXPORT void JNICALL77MonitorContendedEnter(jvmtiEnv *jvmti, JNIEnv *env, jthread thread, jobject monitor) {78jvmtiError err;79jvmtiThreadInfo threadInfo;80jint monitorCount;81jobject *ownedMonitors;8283if (CheckLockObject(env, monitor) == JNI_FALSE) {84return;85}8687err = (*jvmti)->GetThreadInfo(jvmti, thread, &threadInfo);88if (err != JVMTI_ERROR_NONE) {89ShowErrorMessage(jvmti, err,90"MonitorContendedEnter: error in JVMTI GetThreadInfo");91status = FAILED;92event_has_posted = JNI_TRUE;93return;94}95err = (*jvmti)->GetOwnedMonitorInfo(jvmti, thread, &monitorCount, &ownedMonitors);96if (err != JVMTI_ERROR_NONE) {97ShowErrorMessage(jvmti, err,98"MonitorContendedEnter: error in JVMTI GetOwnedMonitorInfo");99status = FAILED;100event_has_posted = JNI_TRUE;101return;102}103104printf("MonitorContendedEnter: %s owns %d monitor(s)\n",105threadInfo.name, monitorCount);106107(*jvmti)->Deallocate(jvmti, (unsigned char *)ownedMonitors);108(*jvmti)->Deallocate(jvmti, (unsigned char *)threadInfo.name);109110if (monitorCount != 0) {111fprintf(stderr, "MonitorContendedEnter: FAIL: monitorCount should be zero.\n");112status = FAILED;113}114115event_has_posted = JNI_TRUE;116}117118JNIEXPORT void JNICALL119MonitorContendedEntered(jvmtiEnv *jvmti, JNIEnv *env, jthread thread, jobject monitor) {120jvmtiError err;121jvmtiThreadInfo threadInfo;122jint monitorCount;123jobject *ownedMonitors;124125if (CheckLockObject(env, monitor) == JNI_FALSE) {126return;127}128129err = (*jvmti)->GetThreadInfo(jvmti, thread, &threadInfo);130if (err != JVMTI_ERROR_NONE) {131ShowErrorMessage(jvmti, err,132"MonitorContendedEntered: error in JVMTI GetThreadInfo");133status = FAILED;134return;135}136err = (*jvmti)->GetOwnedMonitorInfo(jvmti, thread, &monitorCount, &ownedMonitors);137if (err != JVMTI_ERROR_NONE) {138ShowErrorMessage(jvmti, err,139"MonitorContendedEntered: error in JVMTI GetOwnedMonitorInfo");140status = FAILED;141return;142}143144printf("MonitorContendedEntered: %s owns %d monitor(s)\n",145threadInfo.name, monitorCount);146147(*jvmti)->Deallocate(jvmti, (unsigned char *)ownedMonitors);148(*jvmti)->Deallocate(jvmti, (unsigned char *)threadInfo.name);149150if (monitorCount != 1) {151fprintf(stderr, "MonitorContendedEnter: FAIL: monitorCount should be one.\n");152status = FAILED;153}154}155156JNIEXPORT jint JNICALL157Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {158return Agent_Initialize(jvm, options, reserved);159}160161JNIEXPORT jint JNICALL162Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {163return Agent_Initialize(jvm, options, reserved);164}165166JNIEXPORT jint JNICALL167JNI_OnLoad(JavaVM *jvm, void *reserved) {168jint res;169JNIEnv *env;170171res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &env),172JNI_VERSION_9);173if (res != JNI_OK || env == NULL) {174fprintf(stderr, "Error: GetEnv call failed(%d)!\n", res);175return JNI_ERR;176}177178testClass = (*env)->FindClass(env, TEST_CLASS);179if (testClass != NULL) {180testClass = (*env)->NewGlobalRef(env, testClass);181}182if (testClass == NULL) {183fprintf(stderr, "Error: Could not load class %s!\n", TEST_CLASS);184return JNI_ERR;185}186187return JNI_VERSION_9;188}189190static191jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {192jint res;193jvmtiError err;194jvmtiEnv *jvmti;195jvmtiCapabilities caps;196jvmtiEventCallbacks callbacks;197198printf("Agent_OnLoad started\n");199200res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),201JVMTI_VERSION_9);202if (res != JNI_OK || jvmti == NULL) {203fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n");204return JNI_ERR;205}206207err = (*jvmti)->GetPotentialCapabilities(jvmti, &caps);208if (err != JVMTI_ERROR_NONE) {209ShowErrorMessage(jvmti, err,210"Agent_OnLoad: error in JVMTI GetPotentialCapabilities");211return JNI_ERR;212}213214err = (*jvmti)->AddCapabilities(jvmti, &caps);215if (err != JVMTI_ERROR_NONE) {216ShowErrorMessage(jvmti, err,217"Agent_OnLoad: error in JVMTI AddCapabilities");218return JNI_ERR;219}220221err = (*jvmti)->GetCapabilities(jvmti, &caps);222if (err != JVMTI_ERROR_NONE) {223ShowErrorMessage(jvmti, err,224"Agent_OnLoad: error in JVMTI GetCapabilities");225return JNI_ERR;226}227228if (!caps.can_generate_monitor_events) {229fprintf(stderr, "Warning: Monitor events are not implemented\n");230return JNI_ERR;231}232if (!caps.can_get_owned_monitor_info) {233fprintf(stderr, "Warning: GetOwnedMonitorInfo is not implemented\n");234return JNI_ERR;235}236237memset(&callbacks, 0, sizeof(callbacks));238callbacks.MonitorContendedEnter = &MonitorContendedEnter;239callbacks.MonitorContendedEntered = &MonitorContendedEntered;240241err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(jvmtiEventCallbacks));242if (err != JVMTI_ERROR_NONE) {243ShowErrorMessage(jvmti, err,244"Agent_OnLoad: error in JVMTI SetEventCallbacks");245return JNI_ERR;246}247248err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,249JVMTI_EVENT_MONITOR_CONTENDED_ENTER, NULL);250if (err != JVMTI_ERROR_NONE) {251ShowErrorMessage(jvmti, err,252"Agent_OnLoad: error in JVMTI SetEventNotificationMode #1");253return JNI_ERR;254}255err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,256JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, NULL);257if (err != JVMTI_ERROR_NONE) {258ShowErrorMessage(jvmti, err,259"Agent_OnLoad: error in JVMTI SetEventNotificationMode #2");260return JNI_ERR;261}262printf("Agent_OnLoad finished\n");263return JNI_OK;264}265266JNIEXPORT jint JNICALL267Java_GetOwnedMonitorInfoTest_check(JNIEnv *env, jclass cls) {268return status;269}270271JNIEXPORT jboolean JNICALL272Java_GetOwnedMonitorInfoTest_hasEventPosted(JNIEnv *env, jclass cls) {273return event_has_posted;274}275276#ifdef __cplusplus277}278#endif279280281