Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorStackDepthInfo/libGetOwnedMonitorStackDepthInfoTest.c
41153 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#endif4344#define PASSED 045#define FAILED 24647#define TEST_CLASS "GetOwnedMonitorStackDepthInfoTest"48#define LOCK1_CLASS "GetOwnedMonitorStackDepthInfoTest$Lock1"49#define LOCK2_CLASS "GetOwnedMonitorStackDepthInfoTest$Lock2"5051#define TEST_OBJECT_LOCK_DEPTH 252#define LOCK1_DEPTH 353#define LOCK2_DEPTH 154#define EXP_MONITOR_COUNT 35556static jvmtiEnv *jvmti;5758static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);5960static void ShowErrorMessage(jvmtiEnv *jvmti, jvmtiError errCode, const char *message) {61char *errMsg;62jvmtiError result;6364result = (*jvmti)->GetErrorName(jvmti, errCode, &errMsg);65if (result == JVMTI_ERROR_NONE) {66fprintf(stderr, "%s: %s (%d)\n", message, errMsg, errCode);67(*jvmti)->Deallocate(jvmti, (unsigned char *)errMsg);68} else {69fprintf(stderr, "%s (%d)\n", message, errCode);70}71}7273static jboolean CheckLockObject(JNIEnv *env, jobject monitor, jclass testClass) {74return (*env)->IsInstanceOf(env, monitor, testClass);75}7677JNIEXPORT jint JNICALL78Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {79return Agent_Initialize(jvm, options, reserved);80}8182JNIEXPORT jint JNICALL83Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {84return Agent_Initialize(jvm, options, reserved);85}8687JNIEXPORT jint JNICALL88JNI_OnLoad(JavaVM *jvm, void *reserved) {89jint res;90JNIEnv *env;9192res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &env),93JNI_VERSION_9);94if (res != JNI_OK || env == NULL) {95fprintf(stderr, "Error: GetEnv call failed(%d)!\n", res);96return JNI_ERR;97}9899return JNI_VERSION_9;100}101102static103jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {104jint res;105jvmtiError err;106jvmtiCapabilities caps;107108printf("Agent_OnLoad started\n");109110res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),111JVMTI_VERSION_9);112if (res != JNI_OK || jvmti == NULL) {113fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n");114return JNI_ERR;115}116117err = (*jvmti)->GetPotentialCapabilities(jvmti, &caps);118if (err != JVMTI_ERROR_NONE) {119ShowErrorMessage(jvmti, err,120"Agent_OnLoad: error in JVMTI GetPotentialCapabilities");121return JNI_ERR;122}123124err = (*jvmti)->AddCapabilities(jvmti, &caps);125if (err != JVMTI_ERROR_NONE) {126ShowErrorMessage(jvmti, err,127"Agent_OnLoad: error in JVMTI AddCapabilities");128return JNI_ERR;129}130131err = (*jvmti)->GetCapabilities(jvmti, &caps);132if (err != JVMTI_ERROR_NONE) {133ShowErrorMessage(jvmti, err,134"Agent_OnLoad: error in JVMTI GetCapabilities");135return JNI_ERR;136}137138if (!caps.can_get_owned_monitor_stack_depth_info) {139fprintf(stderr, "Warning: GetOwnedMonitorStackDepthInfo is not implemented\n");140return JNI_ERR;141}142143printf("Agent_OnLoad finished\n");144return JNI_OK;145}146147JNIEXPORT jint JNICALL148Java_GetOwnedMonitorStackDepthInfoTest_verifyOwnedMonitors(JNIEnv *env, jclass cls) {149jthread thread;150jvmtiError err;151jvmtiThreadInfo threadInfo;152jint monitorCount;153jvmtiMonitorStackDepthInfo* stackDepthInfo;154jclass testClass;155jclass lock1Class;156jclass lock2Class;157158jint status = PASSED;159160jint idx = 0;161162testClass = (*env)->FindClass(env, TEST_CLASS);163if (testClass == NULL) {164fprintf(stderr, "Error: Could not load class %s!\n", TEST_CLASS);165return FAILED;166}167168lock1Class = (*env)->FindClass(env, LOCK1_CLASS);169if (lock1Class == NULL) {170fprintf(stderr, "Error: Could not load class %s!\n", LOCK1_CLASS);171return FAILED;172}173174lock2Class = (*env)->FindClass(env, LOCK2_CLASS);175if (lock2Class == NULL) {176fprintf(stderr, "Error: Could not load class %s!\n", LOCK2_CLASS);177return FAILED;178}179180err = (*jvmti) -> GetCurrentThread(jvmti, &thread);181if (err != JVMTI_ERROR_NONE) {182ShowErrorMessage(jvmti, err,183"VerifyOwnedMonitors: error in JVMTI GetCurrentThread");184return FAILED;185}186err = (*jvmti)->GetThreadInfo(jvmti, thread, &threadInfo);187if (err != JVMTI_ERROR_NONE) {188ShowErrorMessage(jvmti, err,189"VerifyOwnedMonitors: error in JVMTI GetThreadInfo");190return FAILED;191}192193err = (*jvmti)->GetOwnedMonitorStackDepthInfo(jvmti, thread, &monitorCount, &stackDepthInfo);194if (err != JVMTI_ERROR_NONE) {195ShowErrorMessage(jvmti, err,196"VerifyOwnedMonitors: error in JVMTI GetOwnedMonitorStackDepthInfo");197return FAILED;198}199200printf("VerifyOwnedMonitors: %s owns %d monitor(s)\n", threadInfo.name, monitorCount);201202if (monitorCount != EXP_MONITOR_COUNT) {203fprintf(stderr, "VerifyOwnedMonitors: FAIL: invalid monitorCount, expected: %d, found: %d.\n",204EXP_MONITOR_COUNT, monitorCount);205status = FAILED;206}207for (idx = 0; idx < monitorCount; idx++) {208if (CheckLockObject(env, stackDepthInfo[idx].monitor, testClass) == JNI_TRUE) {209if (stackDepthInfo[idx].stack_depth != TEST_OBJECT_LOCK_DEPTH) {210fprintf(stderr, "VerifyOwnedMonitors: FAIL: invalid stack_depth for %s monitor, expected: %d, found: %d.\n",211TEST_CLASS, TEST_OBJECT_LOCK_DEPTH, stackDepthInfo[idx].stack_depth);212status = FAILED;213}214} else if (CheckLockObject(env, stackDepthInfo[idx].monitor, lock1Class) == JNI_TRUE) {215if (stackDepthInfo[idx].stack_depth != LOCK1_DEPTH) {216fprintf(stderr, "VerifyOwnedMonitors: FAIL: invalid stack_depth for %s monitor, expected: %d, found: %d.\n",217LOCK1_CLASS, LOCK1_DEPTH, stackDepthInfo[idx].stack_depth);218status = FAILED;219}220} else if (CheckLockObject(env, stackDepthInfo[idx].monitor, lock2Class) == JNI_TRUE) {221if (stackDepthInfo[idx].stack_depth != LOCK2_DEPTH) {222fprintf(stderr, "VerifyOwnedMonitors: FAIL: invalid stack_depth for %s monitor, expected: %d, found: %d.\n",223LOCK2_CLASS, LOCK2_DEPTH, stackDepthInfo[idx].stack_depth);224status = FAILED;225}226} else {227fprintf(stderr, "VerifyOwnedMonitors: "228"FAIL: monitor should be instance of %s, %s, or %s\n", TEST_CLASS, LOCK1_CLASS, LOCK2_CLASS);229status = FAILED;230231}232}233234(*jvmti)->Deallocate(jvmti, (unsigned char *) stackDepthInfo);235(*jvmti)->Deallocate(jvmti, (unsigned char *) threadInfo.name);236return status;237}238239#ifdef __cplusplus240}241#endif242243244