1 /* 2 * Copyright (c) 2008, 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 6751643 27 * @summary ThreadReference.ownedMonitors() can return null 28 * 29 * @author jjh 30 * 31 * @key intermittent 32 * @modules jdk.jdi 33 * @run build TestScaffold VMConnection TargetListener TargetAdapter 34 * @run compile -g SimulResumerTest.java 35 * @run driver SimulResumerTest 36 */ 37 import com.sun.jdi.*; 38 import com.sun.jdi.event.*; 39 import com.sun.jdi.request.*; 40 41 import java.util.*; 42 43 /* 44 * This debuggee basically runs two threads each of 45 * which loop, hitting a bkpt in each iteration. 46 * 47 */ 48 class SimulResumerTarg extends Thread { 49 static boolean one = false; 50 static String name1 = "Thread 1"; 51 static String name2 = "Thread 2"; 52 static int count = 10000; 53 public static void main(String[] args) { 54 System.out.println("Howdy!"); 55 SimulResumerTarg t1 = new SimulResumerTarg(name1); 56 SimulResumerTarg t2 = new SimulResumerTarg(name2); 57 58 t1.start(); 59 t2.start(); 60 } 61 62 public SimulResumerTarg(String name) { 63 super(name); 64 } 65 66 public void run() { 67 if (getName().equals(name1)) { 68 run1(); 69 } else { 70 run2(); 71 } 72 } 73 74 public void bkpt1(int i) { 75 synchronized(name1) { 76 yield(); 77 } 78 } 79 80 public void run1() { 81 int i = 0; 82 while (i < count) { 83 i++; 84 bkpt1(i); 85 } 86 } 87 88 public void bkpt2(int i) { 89 synchronized(name2) { 90 yield(); 91 } 92 } 93 94 public void run2() { 95 int i = 0; 96 while (i < count) { 97 i++; 98 bkpt2(i); 99 } 100 } 101 } 102 103 /********** test program **********/ 104 105 public class SimulResumerTest extends TestScaffold { 106 ReferenceType targetClass; 107 ThreadReference mainThread; 108 BreakpointRequest request1; 109 BreakpointRequest request2; 110 static volatile int bkpts = 0; 111 static int iters = 0; 112 Thread resumerThread; 113 static int waitTime = 100; 114 ThreadReference debuggeeThread1 = null; 115 ThreadReference debuggeeThread2 = null; 116 117 SimulResumerTest (String args[]) { 118 super(args); 119 } 120 121 public static void main(String[] args) throws Exception { 122 new SimulResumerTest(args).startTests(); 123 } 124 125 /* BreakpointEvent handler */ 126 127 public void breakpointReached(BreakpointEvent event) { 128 // save ThreadRefs for the two debuggee threads 129 ThreadReference thr = event.thread(); 130 if (bkpts == 0) { 131 resumerThread.start(); 132 debuggeeThread1 = thr; 133 System.out.println("thr1 = " + debuggeeThread1); 134 } 135 136 if (debuggeeThread2 == null && thr != debuggeeThread1) { 137 debuggeeThread2 = thr; 138 System.out.println("thr2 = " + debuggeeThread2); 139 } 140 141 synchronized("abc") { 142 bkpts++; 143 } 144 /** 145 if (bkpts >= SimulResumerTarg.count * 2) { 146 resumerThread.interrupt(); 147 } 148 *****/ 149 150 } 151 152 /********** test core **********/ 153 154 void check(ThreadReference thr) { 155 // This calls each ThreadReference method that could fail due to the bug 156 // that occurs if a resume is done while a call to the method is in process. 157 String kind = ""; 158 if (thr != null) { 159 try { 160 kind = "ownedMonitors()"; 161 System.out.println("kind = " + kind); 162 if (thr.ownedMonitors() == null) { 163 failure("failure: ownedMonitors = null"); 164 } 165 166 kind = "ownedMonitorsAndFrames()"; 167 System.out.println("kind = " + kind); 168 if (thr.ownedMonitorsAndFrames() == null) { 169 failure("failure: ownedMonitorsAndFrames = null"); 170 } 171 172 kind = "currentContendedMonitor()"; 173 System.out.println("kind = " + kind); 174 thr.currentContendedMonitor(); 175 // no failure return value here; could cause an NPE 176 177 kind = "frames()"; 178 System.out.println("kind = " + kind); 179 List<StackFrame> frames = thr.frames(); 180 // no failure return value here; could cause an NPE 181 182 kind = "frames(0, size - 1)"; 183 System.out.println("kind = " + kind); 184 int nframes = frames.size(); 185 while (nframes > 0) { 186 try { 187 thr.frames(0, nframes - 1); 188 break; 189 } catch (IndexOutOfBoundsException iobe) { 190 // 6815126. let's try to get less frames 191 iobe.printStackTrace(); 192 nframes--; 193 } 194 } 195 196 kind = "frameCount()"; 197 System.out.println("kind = " + kind); 198 if (thr.frameCount() == -1) { 199 failure("failure: frameCount = -1"); 200 } 201 202 kind = "name()"; 203 System.out.println("kind = " + kind); 204 if (thr.name() == null) { 205 failure("failure: name = null"); 206 } 207 208 kind = "status()"; 209 System.out.println("kind = " + kind); 210 if (thr.status() < 0) { 211 failure("failure: status < 0"); 212 } 213 214 } catch (IncompatibleThreadStateException ee) { 215 // ignore 216 } catch (VMDisconnectedException ee) { 217 // This is how we stop. The debuggee runs to completion 218 // and we get this exception. 219 throw ee; 220 } catch (Exception ee) { 221 failure("failure: Got exception from " + kind + ": " + ee ); 222 } 223 } 224 } 225 226 protected void runTests() throws Exception { 227 /* 228 * Get to the top of main() 229 * to determine targetClass and mainThread 230 */ 231 BreakpointEvent bpe = startToMain("SimulResumerTarg"); 232 targetClass = bpe.location().declaringType(); 233 mainThread = bpe.thread(); 234 EventRequestManager erm = vm().eventRequestManager(); 235 final Thread mainThread = Thread.currentThread(); 236 237 /* 238 * Set event requests 239 */ 240 Location loc1 = findMethod(targetClass, "bkpt1", "(I)V").location(); 241 Location loc2 = findMethod(targetClass, "bkpt2", "(I)V").location(); 242 request1 = erm.createBreakpointRequest(loc1); 243 request2 = erm.createBreakpointRequest(loc2); 244 request1.enable(); 245 request2.enable(); 246 247 /* 248 * This thread will be started when we get the first bkpt. 249 */ 250 resumerThread = new Thread("test resumer") { 251 public void run() { 252 while (true) { 253 iters++; 254 System.out.println("bkpts = " + bkpts + ", iters = " + iters); 255 try { 256 Thread.sleep(waitTime); 257 check(debuggeeThread1); 258 check(debuggeeThread2); 259 } catch (InterruptedException ee) { 260 // If the test completes, this occurs. 261 println("resumer Interrupted"); 262 break; 263 } catch (VMDisconnectedException ee) { 264 println("VMDisconnectedException"); 265 break; 266 } 267 } 268 } 269 }; 270 271 /* 272 * resume the target, listening for events 273 */ 274 listenUntilVMDisconnect(); 275 resumerThread.interrupt(); 276 /* 277 * deal with results of test 278 * if anything has called failure("foo") testFailed will be true 279 */ 280 if (!testFailed) { 281 println("SimulResumerTest: passed; bkpts = " + bkpts + ", iters = " + iters); 282 } else { 283 throw new Exception("SimulResumerTest: failed; bkpts = " + bkpts + ", iters = " + iters); 284 } 285 } 286 }