Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/GetOwnedMonitorInfo/libGetOwnedMonitorInfoWithEATest.c
41155 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_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_info) {122fprintf(stderr, "Warning: GetOwnedMonitorInfo is not implemented\n");123return JNI_ERR;124}125126printf("Agent_OnLoad finished\n");127return JNI_OK;128}129130JNIEXPORT jint JNICALL131Java_GetOwnedMonitorInfoWithEATest_getOwnedMonitorInfo(JNIEnv *env, jclass cls, jobject targetThread, jobjectArray resOwnedMonitors) {132jvmtiError err;133jvmtiThreadInfo threadInfo;134jint monitorCount;135jobject* monitors;136jint idx;137138err = (*jvmti)->GetThreadInfo(jvmti, targetThread, &threadInfo);139if (err != JVMTI_ERROR_NONE) {140ShowErrorMessage(jvmti, err,141"getOwnedMonitorsFor: error in JVMTI GetThreadInfo");142return FAILED;143}144145err = (*jvmti)->GetOwnedMonitorInfo(jvmti, targetThread, &monitorCount, &monitors);146if (err != JVMTI_ERROR_NONE) {147ShowErrorMessage(jvmti, err,148"getOwnedMonitorsFor: error in JVMTI GetOwnedMonitorInfo");149return FAILED;150}151152printf("getOwnedMonitorsFor: %s owns %d monitor(s)\n", threadInfo.name, monitorCount);153154for (idx = 0; idx < monitorCount; idx++) {155(*env)->SetObjectArrayElement(env, resOwnedMonitors, idx, monitors[idx]);156}157158(*jvmti)->Deallocate(jvmti, (unsigned char *) monitors);159(*jvmti)->Deallocate(jvmti, (unsigned char *) threadInfo.name);160return monitorCount;161}162163#ifdef __cplusplus164}165#endif166167168