0N/A/*
1472N/A * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6293795
0N/A * @summary Backend hangs when invokeMethod is called from a JDI eventHandler
0N/A *
0N/A * @author jjh
0N/A *
0N/A * @run build TestScaffold VMConnection TargetListener TargetAdapter
0N/A * @run compile -g InvokeHangTest.java
0N/A * @run main InvokeHangTest
0N/A */
0N/Aimport com.sun.jdi.*;
0N/Aimport com.sun.jdi.event.*;
0N/Aimport com.sun.jdi.request.*;
0N/A
0N/Aimport java.util.*;
0N/A
0N/A/*
0N/A * This debuggee basically runs two threads each of
0N/A * which loop, hitting a bkpt in each iteration.
0N/A *
0N/A */
0N/Aclass InvokeHangTarg extends Thread {
0N/A static boolean one = false;
0N/A static String name1 = "Thread 1";
0N/A static String name2 = "Thread 2";
0N/A static int count = 100;
0N/A
0N/A public static void main(String[] args) {
0N/A System.out.println("Howdy!");
0N/A InvokeHangTarg t1 = new InvokeHangTarg(name1);
0N/A InvokeHangTarg t2 = new InvokeHangTarg(name2);
0N/A
0N/A t1.start();
0N/A t2.start();
0N/A }
1080N/A
1080N/A // This is called from the debugger via invokeMethod
1080N/A public double invokeee() {
1080N/A System.out.println("Debuggee: invokeee in thread "+Thread.currentThread().toString());
1080N/A yield();
1080N/A return longMethod(2);
1080N/A }
0N/A public double longMethod(int n) {
0N/A double a = 0;
0N/A double s = 0;
0N/A for (int i = 0; i < n; i++) {
0N/A a += i;
0N/A for (int j = -1000*i; j < 1000*i; j++) {
0N/A a = a*(1 + i/(j + 0.5));
0N/A s += Math.sin(a);
0N/A }
0N/A }
0N/A System.out.println("Debuggee: invokeee finished");
0N/A return s;
0N/A }
0N/A
0N/A public InvokeHangTarg(String name) {
0N/A super(name);
0N/A }
0N/A
0N/A public void run() {
0N/A if (getName().equals(name1)) {
0N/A run1();
0N/A } else {
0N/A run2();
0N/A }
0N/A }
0N/A
0N/A public void bkpt1(int i) {
0N/A System.out.println("Debuggee: " + Thread.currentThread() +" is running:" + i);
0N/A try {
0N/A Thread.currentThread().sleep(2);
0N/A } catch (InterruptedException iex) {}
0N/A //yield();
0N/A }
0N/A
0N/A public void run1() {
1080N/A int i = 0;
1137N/A while (i < count) {
0N/A i++;
0N/A bkpt1(i);
1080N/A }
1080N/A }
1080N/A
0N/A public void bkpt2(int i) {
0N/A System.out.println("Debuggee: " + Thread.currentThread() +" is running:" + i);
0N/A try {
0N/A Thread.currentThread().sleep(2);
0N/A } catch (InterruptedException iex) {}
0N/A //yield();
0N/A }
0N/A
0N/A public void run2() {
0N/A int i = 0;
0N/A while (i < count) {
1138N/A i++;
1138N/A bkpt2(i);
1138N/A }
1138N/A }
1138N/A}
1138N/A
0N/A/********** test program **********/
0N/A
0N/Apublic class InvokeHangTest extends TestScaffold {
0N/A ReferenceType targetClass;
0N/A ThreadReference mainThread;
0N/A BreakpointRequest request1;
0N/A BreakpointRequest request2;
0N/A static volatile int bkpts = 0;
0N/A Thread timerThread;
0N/A static int waitTime = 20000;
0N/A
0N/A InvokeHangTest (String args[]) {
0N/A super(args);
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A new InvokeHangTest(args).startTests();
0N/A }
0N/A
0N/A void doInvoke(ThreadReference thread, ObjectReference ref, String methodName) {
0N/A List methods = ref.referenceType().methodsByName(methodName);
0N/A Method method = (Method) methods.get(0);
0N/A try {
0N/A System.err.println(" Debugger: Invoking in thread" + thread);
0N/A ref.invokeMethod(thread, method, new ArrayList(), ref.INVOKE_NONVIRTUAL);
0N/A System.err.println(" Debugger: Invoke done");
0N/A } catch (Exception ex) {
0N/A ex.printStackTrace();
0N/A failure("failure: Exception");
0N/A }
0N/A }
0N/A
0N/A // BreakpointEvent handler
0N/A public void breakpointReached(BreakpointEvent event) {
0N/A if (bkpts == 0) {
0N/A /*
0N/A * This thread will watch for n secs to go by with no
0N/A * calls to this method.
0N/A */
0N/A timerThread.start();
0N/A }
0N/A
0N/A synchronized("abc") {
0N/A /*
0N/A * Note that this will most likely never get to
0N/A * the number of times the two bkpt lines in the debuggee
0N/A * are hit because bkpts are lost while they are disabled.
0N/A */
0N/A bkpts++;
0N/A }
0N/A
0N/A /*
0N/A * The bug occurs when the requests are disabled
0N/A * and then an invoke is done in the event handler. In some cases
0N/A * the other thread has hit a bkpt and the back-end is waiting
0N/A * to send it. When the back-end resumes the debuggee to do the
0N/A * invokeMethod, this 2nd bkpt is released, the debuggee is suspended, including
0N/A * the thread on which the invoke was done (because it is a SUSPEND_ALL bkpt),
0N/A * the bkpt is sent to the front-end, but the client event handler is sitting
0N/A * here waiting for the invoke to finish, so it doesn't get the 2nd bkpt and
0N/A * do the resume for it. Thus, the debuggee is suspended waiting for a resume
0N/A * that never comes.
0N/A */
0N/A request1.disable();
0N/A request2.disable();
0N/A
0N/A ThreadReference thread = event.thread();
0N/A try {
0N/A StackFrame sf = thread.frame(0);
0N/A System.err.println(" Debugger: Breakpoint hit at "+sf.location());
0N/A doInvoke(thread, sf.thisObject(), "invokeee");
0N/A } catch (IncompatibleThreadStateException itsex) {
0N/A itsex.printStackTrace();
0N/A failure("failure: Exception");
0N/A }
0N/A request1.enable();
0N/A request2.enable();
0N/A
0N/A }
0N/A
0N/A /********** test core **********/
0N/A
0N/A protected void runTests() throws Exception {
0N/A
0N/A /*
0N/A * Get to the top of main()
0N/A * to determine targetClass and mainThread
0N/A */
0N/A BreakpointEvent bpe = startToMain("InvokeHangTarg");
0N/A targetClass = bpe.location().declaringType();
0N/A mainThread = bpe.thread();
0N/A EventRequestManager erm = vm().eventRequestManager();
0N/A final Thread mainThread = Thread.currentThread();
0N/A
0N/A /*
0N/A * Set event requests
0N/A */
0N/A Location loc1 = findMethod(targetClass, "bkpt1", "(I)V").location();
0N/A Location loc2 = findMethod(targetClass, "bkpt2", "(I)V").location();
0N/A request1 = erm.createBreakpointRequest(loc1);
0N/A request2 = erm.createBreakpointRequest(loc2);
0N/A request1.enable();
0N/A request2.enable();
0N/A
0N/A /*
0N/A * This thread will be started when we get the first bkpt.
0N/A * (Which we always expect to get).
0N/A * It awakens every n seconds and checks to see if we
0N/A * got any breakpoint events while it was asleep. If not, then
0N/A * we assume the debuggee is hung and fail the test.
0N/A */
0N/A timerThread = new Thread("test timer") {
0N/A public void run() {
0N/A int myBkpts = bkpts;
0N/A while (true) {
0N/A try {
0N/A Thread.sleep(waitTime);
0N/A System.out.println("bkpts = " + bkpts);
0N/A if (myBkpts == bkpts) {
0N/A // no bkpt for 'waitTime' msecs
0N/A failure("failure: Debuggee appears to be hung");
0N/A vmDisconnected = true;
0N/A // This awakens the main thread which is
0N/A // waiting for a VMDisconnect.
0N/A mainThread.interrupt();
0N/A break;
0N/A }
0N/A myBkpts = bkpts;
0N/A } catch (InterruptedException ee) {
0N/A // If the test completes, this occurs.
0N/A println("timer Interrupted");
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A };
0N/A
0N/A /*
0N/A * resume the target, listening for events
0N/A */
0N/A listenUntilVMDisconnect();
0N/A timerThread.interrupt();
0N/A /*
0N/A * deal with results of test
0N/A * if anything has called failure("foo") testFailed will be true
0N/A */
0N/A if (!testFailed) {
0N/A println("InvokeHangTest: passed; bkpts = " + bkpts);
0N/A } else {
0N/A throw new Exception("InvokeHangTest: failed; bkpts = " + bkpts);
0N/A }
0N/A }
0N/A}
0N/A