Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorStackDepthInfo/libGetOwnedMonitorStackDepthInfoWithEATest.c
41153 views
/*1* Copyright (c) 2019 SAP SE. 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 FAILED -14546static jvmtiEnv *jvmti;4748static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);4950static void ShowErrorMessage(jvmtiEnv *jvmti, jvmtiError errCode, const char *message) {51char *errMsg;52jvmtiError result;5354result = (*jvmti)->GetErrorName(jvmti, errCode, &errMsg);55if (result == JVMTI_ERROR_NONE) {56fprintf(stderr, "%s: %s (%d)\n", message, errMsg, errCode);57(*jvmti)->Deallocate(jvmti, (unsigned char *)errMsg);58} else {59fprintf(stderr, "%s (%d)\n", message, errCode);60}61}6263JNIEXPORT jint JNICALL64Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {65return Agent_Initialize(jvm, options, reserved);66}6768JNIEXPORT jint JNICALL69Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {70return Agent_Initialize(jvm, options, reserved);71}7273JNIEXPORT jint JNICALL74JNI_OnLoad(JavaVM *jvm, void *reserved) {75jint res;76JNIEnv *env;7778res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &env),79JNI_VERSION_9);80if (res != JNI_OK || env == NULL) {81fprintf(stderr, "Error: GetEnv call failed(%d)!\n", res);82return JNI_ERR;83}8485return JNI_VERSION_9;86}8788static89jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {90jint res;91jvmtiError err;92jvmtiCapabilities caps;9394printf("Agent_OnLoad started\n");9596memset(&caps, 0, sizeof(caps));9798res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),99JVMTI_VERSION_9);100if (res != JNI_OK || jvmti == NULL) {101fprintf(stderr, "Error: wrong result of a valid call to GetEnv!\n");102return JNI_ERR;103}104105caps.can_get_owned_monitor_stack_depth_info = 1;106107err = (*jvmti)->AddCapabilities(jvmti, &caps);108if (err != JVMTI_ERROR_NONE) {109ShowErrorMessage(jvmti, err,110"Agent_OnLoad: error in JVMTI AddCapabilities");111return JNI_ERR;112}113114err = (*jvmti)->GetCapabilities(jvmti, &caps);115if (err != JVMTI_ERROR_NONE) {116ShowErrorMessage(jvmti, err,117"Agent_OnLoad: error in JVMTI GetCapabilities");118return JNI_ERR;119}120121if (!caps.can_get_owned_monitor_stack_depth_info) {122fprintf(stderr, "Warning: GetOwnedMonitorStackDepthInfo is not implemented\n");123return JNI_ERR;124}125126printf("Agent_OnLoad finished\n");127return JNI_OK;128}129130JNIEXPORT jint JNICALL131Java_GetOwnedMonitorStackDepthInfoWithEATest_getOwnedMonitorStackDepthInfo(JNIEnv *env, jclass cls, jobject targetThread, jobjectArray ownedMonitors, jintArray depths) {132jvmtiError err;133jvmtiThreadInfo threadInfo;134jint monitorCount;135jvmtiMonitorStackDepthInfo* stackDepthInfo;136jint* depthsPtr;137jint idx = 0;138139err = (*jvmti)->GetThreadInfo(jvmti, targetThread, &threadInfo);140if (err != JVMTI_ERROR_NONE) {141ShowErrorMessage(jvmti, err,142"getOwnedMonitorsFor: error in JVMTI GetThreadInfo");143return FAILED;144}145146err = (*jvmti)->GetOwnedMonitorStackDepthInfo(jvmti, targetThread, &monitorCount, &stackDepthInfo);147if (err != JVMTI_ERROR_NONE) {148ShowErrorMessage(jvmti, err,149"getOwnedMonitorsFor: error in JVMTI GetOwnedMonitorStackDepthInfo");150return FAILED;151}152153printf("getOwnedMonitorsFor: %s owns %d monitor(s)\n", threadInfo.name, monitorCount);154155depthsPtr = (*env)->GetIntArrayElements(env, depths, NULL);156for (idx = 0; idx < monitorCount; idx++) {157(*env)->SetObjectArrayElement(env, ownedMonitors, idx, stackDepthInfo[idx].monitor);158depthsPtr[idx] = stackDepthInfo[idx].stack_depth;159}160(*env)->ReleaseIntArrayElements(env, depths, depthsPtr, 0);161162(*jvmti)->Deallocate(jvmti, (unsigned char *) stackDepthInfo);163(*jvmti)->Deallocate(jvmti, (unsigned char *) threadInfo.name);164return monitorCount;165}166167#ifdef __cplusplus168}169#endif170171172