Path: blob/master/test/hotspot/jtreg/serviceability/monitoring/ThreadInfo/GetLockOwnerName/libGetLockOwnerName.cpp
41161 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 jint JNICALL38Java_GetLockOwnerName_wait4ContendedEnter(JNIEnv *jni, jclass cls, jthread thr) {39jvmtiError err;40jint thread_state;41do {42err = jvmti->GetThreadState(thr, &thread_state);43if (err != JVMTI_ERROR_NONE) {44return err;45}46if ((thread_state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) != 0) {47// For this test, we need the contended monitor reference and48// that is saved right after the thread state is updated so we49// only return from this function when we have both.50jobject monitor_ptr;51err = jvmti->GetCurrentContendedMonitor(thr, &monitor_ptr);52if (err != JVMTI_ERROR_NONE) {53return err;54}55if (monitor_ptr != NULL) {56break;57}58}59} while (true);60return err;61}6263/** Agent library initialization. */6465JNIEXPORT jint JNICALL66Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {67LOG("\nAgent_OnLoad started");6869// create JVMTI environment70if (jvm->GetEnv((void **) (&jvmti), JVMTI_VERSION) != JNI_OK) {71return JNI_ERR;72}7374// add capabilities needed for this test75jvmtiCapabilities needCaps;76memset(&needCaps, 0, sizeof(needCaps));77needCaps.can_get_current_contended_monitor = 1;7879jvmtiError err = jvmti->AddCapabilities(&needCaps);80if (err != JVMTI_ERROR_NONE) {81return JNI_ERR;82}83LOG("Agent_OnLoad finished\n");84return JNI_OK;85}8687}888990