1 /* 2 * Copyright (c) 2005, 2015, 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 /** 25 * @test 26 * @bug 4401399 27 * @summary Simple basic test of jdi Monitor request and event. 28 * 29 * @author Swamy Venkataramanappa 30 * 31 * @modules jdk.jdi 32 * @run build TestScaffold VMConnection TargetListener TargetAdapter 33 * @run compile -g MonitorEventTest.java 34 * @run driver MonitorEventTest 35 */ 36 import com.sun.jdi.*; 37 import com.sun.jdi.event.*; 38 import com.sun.jdi.request.*; 39 40 import java.util.*; 41 42 /********** target program **********/ 43 44 class MonitorTestTarg { 45 public static Object endingMonitor; 46 public static Object startingMonitor; 47 public static final long timeout = 30 * 6000; // milliseconds 48 49 public static volatile boolean aboutEnterLock; 50 51 static void foo() { 52 System.out.println("Howdy!"); 53 } 54 55 public static void main(String[] args){ 56 endingMonitor = new Object(); 57 startingMonitor = new Object(); 58 59 myThread t1 = new myThread(); 60 foo(); 61 aboutEnterLock = false; 62 63 synchronized(endingMonitor) { 64 65 // run thread 66 try { 67 // start thread 68 synchronized (startingMonitor) { 69 t1.start(); 70 startingMonitor.wait(timeout); 71 } 72 } catch (InterruptedException e) { 73 System.out.println("Interrupted exception " + e); 74 } 75 76 Thread.yield(); 77 while(!(aboutEnterLock && t1.getState() == Thread.State.BLOCKED)) { 78 try { 79 Thread.sleep(1000); 80 }catch(Exception x){ 81 System.out.println(x); 82 } 83 } 84 } 85 try { 86 t1.join(timeout); 87 } catch (Exception x){ 88 System.out.println("Exception while thread.join :" + x); 89 } 90 System.out.println("Test exiting"); 91 } 92 } 93 94 class myThread extends Thread { 95 public void run() { 96 synchronized(MonitorTestTarg.startingMonitor) { 97 MonitorTestTarg.startingMonitor.notify(); 98 } 99 100 // contended enter wait until main thread release monitor 101 MonitorTestTarg.aboutEnterLock = true; 102 synchronized (MonitorTestTarg.endingMonitor) { 103 } 104 } 105 } 106 107 108 /********** test program **********/ 109 110 public class MonitorEventTest extends TestScaffold { 111 ReferenceType targetClass; 112 ThreadReference mainThread; 113 List monitors; 114 MonitorContendedEnterRequest contendedEnterRequest; 115 MonitorWaitedRequest monitorWaitedRequest; 116 MonitorContendedEnteredRequest contendedEnteredRequest; 117 MonitorWaitRequest monitorWaitRequest; 118 119 static int actualWaitCount = 0; 120 static int actualWaitedCount = 0; 121 static int actualContendedEnterCount = 0; 122 static int actualContendedEnteredCount= 0; 123 124 MonitorEventTest (String args[]) { 125 super(args); 126 } 127 128 public static void main(String[] args) throws Exception { 129 new MonitorEventTest(args).startTests(); 130 } 131 132 /********** event handlers **********/ 133 134 public void monitorContendedEnter(MonitorContendedEnterEvent event) { 135 136 actualContendedEnterCount++; 137 } 138 139 public void monitorContendedEntered(MonitorContendedEnteredEvent event) { 140 141 actualContendedEnteredCount++; 142 143 } 144 145 public void monitorWait(MonitorWaitEvent event) { 146 147 actualWaitCount++; 148 149 } 150 public void monitorWaited(MonitorWaitedEvent event) { 151 152 actualWaitedCount++; 153 154 } 155 156 157 158 /********** test core **********/ 159 160 protected void runTests() throws Exception { 161 /* 162 * Get to the top of main() 163 * to determine targetClass and mainThread 164 */ 165 BreakpointEvent bpe = startToMain("MonitorTestTarg"); 166 targetClass = bpe.location().declaringType(); 167 mainThread = bpe.thread(); 168 169 int initialSize = mainThread.frames().size(); 170 if (vm().canRequestMonitorEvents()) { 171 contendedEnterRequest = eventRequestManager().createMonitorContendedEnterRequest(); 172 contendedEnterRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE); 173 contendedEnterRequest.enable(); 174 contendedEnteredRequest = eventRequestManager().createMonitorContendedEnteredRequest(); 175 contendedEnteredRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE); 176 contendedEnteredRequest.enable(); 177 monitorWaitRequest = eventRequestManager().createMonitorWaitRequest(); 178 monitorWaitRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE); 179 monitorWaitRequest.enable(); 180 monitorWaitedRequest = eventRequestManager().createMonitorWaitedRequest(); 181 monitorWaitedRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE); 182 monitorWaitedRequest.enable(); 183 } else { 184 System.out.println("request monitor events not supported " ); 185 } 186 187 188 resumeTo("MonitorTestTarg", "foo", "()V"); 189 190 /* 191 * resume until end 192 */ 193 listenUntilVMDisconnect(); 194 195 /* 196 * At least one of each type event should have recevied by this test. 197 */ 198 if (vm().canRequestMonitorEvents()) { 199 if (actualContendedEnterCount == 0) { 200 failure("Did not receive any contended enter event."); 201 } 202 if (actualContendedEnteredCount == 0) { 203 failure("Did not receive any contended entered event. "); 204 } 205 if (actualWaitCount == 0) { 206 failure("Did not receive any contended monitor wait event"); 207 } 208 if (actualWaitedCount == 0) { 209 failure("Did not receive any contended monitor waited event"); 210 } 211 } 212 213 214 /* 215 * deal with results of test 216 * if anything has called failure("foo") testFailed will be true 217 */ 218 if (!testFailed) { 219 println("MonitorEventTest: passed"); 220 } else { 221 throw new Exception("MonitorEventTest: failed"); 222 } 223 } 224 }