1 /*
 2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include "jvmti.h"
27 #include "jvmti_common.h"
28 
29 extern "C" {
30 
31 static jvmtiEnv *jvmti;
32 
33 jint  Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
34 
35   jint res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1);
36   if (res != JNI_OK || jvmti == NULL) {
37     LOG("Wrong result of a valid call to GetEnv !\n");
38     return JNI_ERR;
39   }
40 
41   jvmtiCapabilities caps;
42   memset(&caps, 0, sizeof(jvmtiCapabilities));
43   caps.can_get_current_contended_monitor = 1;
44   jvmtiError err = jvmti->AddCapabilities(&caps);
45   if (err != JVMTI_ERROR_NONE) {
46     LOG("(AddCapabilities) unexpected error: %s (%d)\n", TranslateError(err), err);
47     return JNI_ERR;
48   }
49 
50   return JNI_OK;
51 }
52 
53 JNIEXPORT void JNICALL
54 Java_contmon02_checkMonitor(JNIEnv *jni, jclass cls, jint point, jthread thread) {
55   jobject monitor;
56   check_jvmti_status(jni, jvmti->GetCurrentContendedMonitor(thread, &monitor), "Error in GetCurrentContendedMonitor");
57   if (monitor != NULL) {
58     LOG("(#%d) unexpected monitor object: 0x%p\n", point, monitor);
59     fatal(jni, "GetCurrentContendedMonitor return unexpected monitor.");
60   }
61 }
62 
63 }