Path: blob/master/test/hotspot/jtreg/serviceability/jvmti/SuspendWithObjectMonitorWait/libSuspendWithObjectMonitorWait.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;2930#define LOG(...) \31do { \32printf(__VA_ARGS__); \33printf("\n"); \34fflush(stdout); \35} while (0)3637JNIEXPORT int JNICALL38Java_SuspendWithObjectMonitorWait_suspendThread(JNIEnv *jni, jclass cls, jthread thr) {39return jvmti->SuspendThread(thr);40}4142JNIEXPORT jint JNICALL43Java_SuspendWithObjectMonitorWaitWorker_resumeThread(JNIEnv *jni, jclass cls, jthread thr) {44return jvmti->ResumeThread(thr);45}4647JNIEXPORT jint JNICALL48Java_SuspendWithObjectMonitorWait_wait4ContendedEnter(JNIEnv *jni, jclass cls, jthread thr) {49jvmtiError err;50jint thread_state;51do {52err = jvmti->GetThreadState(thr, &thread_state);53if (err != JVMTI_ERROR_NONE) {54return err;55}56} while ((thread_state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) == 0);57return err;58}596061/** Agent library initialization. */6263JNIEXPORT jint JNICALL64Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {65LOG("\nAgent_OnLoad started");6667// create JVMTI environment68if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {69return JNI_ERR;70}7172// add specific capabilities for suspending thread73jvmtiCapabilities suspendCaps;74memset(&suspendCaps, 0, sizeof(suspendCaps));75suspendCaps.can_suspend = 1;7677jvmtiError err = jvmti->AddCapabilities(&suspendCaps);78if (err != JVMTI_ERROR_NONE) {79return JNI_ERR;80}81LOG("Agent_OnLoad finished\n");82return JNI_OK;83}8485}868788