Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/SuspendWithRawMonitorEnter/libSuspendWithRawMonitorEnter.cpp
41153 views
/*1* Copyright (c) 2001, 2021, 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 <string.h>24#include "jvmti.h"2526extern "C" {2728static jvmtiEnv* jvmti = NULL;29static jrawMonitorID threadLock = NULL;30static char threadLockName[] = "threadLock";3132#define LOG(...) \33do { \34printf(__VA_ARGS__); \35printf("\n"); \36fflush(stdout); \37} while (0)3839JNIEXPORT jint JNICALL40Java_SuspendWithRawMonitorEnter_createRawMonitor(JNIEnv *jni, jclass cls) {41return jvmti->CreateRawMonitor(threadLockName, &threadLock);42}4344JNIEXPORT jint JNICALL45Java_SuspendWithRawMonitorEnter_destroyRawMonitor(JNIEnv *jni, jclass cls) {46return jvmti->DestroyRawMonitor(threadLock);47}4849JNIEXPORT jint JNICALL50Java_SuspendWithRawMonitorEnter_suspendThread(JNIEnv *jni, jclass cls, jthread thr) {51return jvmti->SuspendThread(thr);52}5354JNIEXPORT jint JNICALL55Java_SuspendWithRawMonitorEnterWorker_rawMonitorEnter(JNIEnv *jni, jclass cls) {56return jvmti->RawMonitorEnter(threadLock);57}5859JNIEXPORT jint JNICALL60Java_SuspendWithRawMonitorEnterWorker_rawMonitorExit(JNIEnv *jni, jclass cls) {61return jvmti->RawMonitorExit(threadLock);62}6364JNIEXPORT jint JNICALL65Java_SuspendWithRawMonitorEnterWorker_resumeThread(JNIEnv *jni, jclass cls, jthread thr) {66return jvmti->ResumeThread(thr);67}686970/** Agent library initialization. */7172JNIEXPORT jint JNICALL73Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {74LOG("\nAgent_OnLoad started");7576// create JVMTI environment77if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {78return JNI_ERR;79}8081// add specific capabilities for suspending thread82jvmtiCapabilities suspendCaps;83memset(&suspendCaps, 0, sizeof(suspendCaps));84suspendCaps.can_suspend = 1;8586jvmtiError err = jvmti->AddCapabilities(&suspendCaps);87if (err != JVMTI_ERROR_NONE) {88return JNI_ERR;89}90LOG("Agent_OnLoad finished\n");91return JNI_OK;92}9394}959697