0N/A/*
2362N/A * Copyright (c) 2001, 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 *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport com.sun.jdi.*;
0N/Aimport com.sun.jdi.request.*;
0N/Aimport com.sun.jdi.event.*;
0N/Aimport java.util.*;
0N/Aimport java.io.*;
0N/A
0N/A/**
0N/A * Framework used by all JDI regression tests
0N/A */
0N/Aabstract public class TestScaffold extends TargetAdapter {
0N/A private boolean shouldTrace = false;
0N/A private VMConnection connection;
0N/A private VirtualMachine vm;
0N/A private EventRequestManager requestManager;
0N/A private List listeners = Collections.synchronizedList(new LinkedList());
0N/A private boolean redefineAtStart = false;
0N/A private boolean redefineAtEvents = false;
0N/A private boolean redefineAsynchronously = false;
0N/A private ReferenceType mainStartClass = null;
0N/A
0N/A ThreadReference mainThread;
0N/A /**
0N/A * We create a VMDeathRequest, SUSPEND_ALL, to sync the BE and FE.
0N/A */
0N/A private VMDeathRequest ourVMDeathRequest = null;
0N/A
0N/A /**
0N/A * We create an ExceptionRequest, SUSPEND_NONE so that we can
0N/A * catch it and output a msg if an exception occurs in the
0N/A * debuggee.
0N/A */
0N/A private ExceptionRequest ourExceptionRequest = null;
0N/A
0N/A /**
0N/A * If we do catch an uncaught exception, we set this true
0N/A * so the testcase can find out if it wants to.
0N/A */
0N/A private boolean exceptionCaught = false;
0N/A ThreadReference vmStartThread = null;
0N/A boolean vmDied = false;
0N/A boolean vmDisconnected = false;
0N/A final String[] args;
0N/A protected boolean testFailed = false;
0N/A
0N/A static private class ArgInfo {
0N/A String targetVMArgs = "";
0N/A String targetAppCommandLine = "";
0N/A String connectorSpec = "com.sun.jdi.CommandLineLaunch:";
0N/A int traceFlags = 0;
0N/A }
0N/A
0N/A /**
0N/A * An easy way to sleep for awhile
0N/A */
0N/A public void mySleep(int millis) {
0N/A try {
0N/A Thread.sleep(millis);
0N/A } catch (InterruptedException ee) {
0N/A }
0N/A }
0N/A
0N/A boolean getExceptionCaught() {
0N/A return exceptionCaught;
0N/A }
0N/A
0N/A void setExceptionCaught(boolean value) {
0N/A exceptionCaught = value;
0N/A }
0N/A
0N/A /**
0N/A * Return true if eventSet contains the VMDeathEvent for the request in
0N/A * the ourVMDeathRequest ivar.
0N/A */
0N/A private boolean containsOurVMDeathRequest(EventSet eventSet) {
0N/A if (ourVMDeathRequest != null) {
0N/A Iterator myIter = eventSet.iterator();
0N/A while (myIter.hasNext()) {
0N/A Event myEvent = (Event)myIter.next();
0N/A if (!(myEvent instanceof VMDeathEvent)) {
0N/A // We assume that an EventSet contains only VMDeathEvents
0N/A // or no VMDeathEvents.
0N/A break;
0N/A }
0N/A if (ourVMDeathRequest.equals(myEvent.request())) {
0N/A return true;
0N/A }
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /************************************************************************
0N/A * The following methods override those in our base class, TargetAdapter.
0N/A *************************************************************************/
0N/A
0N/A /**
0N/A * Events handled directly by scaffold always resume (well, almost always)
0N/A */
0N/A public void eventSetComplete(EventSet set) {
0N/A // The listener in connect(..) resumes after receiving our
0N/A // special VMDeathEvent. We can't also do the resume
0N/A // here or we will probably get a VMDisconnectedException
0N/A if (!containsOurVMDeathRequest(set)) {
0N/A traceln("TS: set.resume() called");
0N/A set.resume();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method sets up default requests.
0N/A * Testcases can override this to change default behavior.
0N/A */
0N/A protected void createDefaultEventRequests() {
0N/A createDefaultVMDeathRequest();
0N/A createDefaultExceptionRequest();
0N/A }
0N/A
0N/A /**
0N/A * We want the BE to stop when it issues a VMDeathEvent in order to
0N/A * give the FE time to complete handling events that occured before
0N/A * the VMDeath. When we get the VMDeathEvent for this request in
0N/A * the listener in connect(), we will do a resume.
0N/A * If a testcase wants to do something special with VMDeathEvent's,
0N/A * then it should override this method with an empty method or
0N/A * whatever in order to suppress the automatic resume. The testcase
0N/A * will then be responsible for the handling of VMDeathEvents. It
0N/A * has to be sure that it does a resume if it gets a VMDeathEvent
0N/A * with SUSPEND_ALL, and it has to be sure that it doesn't do a
0N/A * resume after getting a VMDeath with SUSPEND_NONE (the automatically
0N/A * generated VMDeathEvent.)
0N/A */
0N/A protected void createDefaultVMDeathRequest() {
0N/A ourVMDeathRequest = requestManager.createVMDeathRequest();
0N/A ourVMDeathRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
0N/A ourVMDeathRequest.enable();
0N/A }
0N/A
0N/A /**
0N/A * This will allow us to print a warning if a debuggee gets an
0N/A * unexpected exception. The unexpected exception will be handled in
0N/A * the exceptionThrown method in the listener created in the connect()
0N/A * method.
0N/A * If a testcase does not want an uncaught exception to cause a
0N/A * msg, it must override this method.
0N/A */
0N/A protected void createDefaultExceptionRequest() {
0N/A ourExceptionRequest = requestManager.createExceptionRequest(null,
0N/A false, true);
0N/A
0N/A // We can't afford to make this be other than SUSPEND_NONE. Otherwise,
0N/A // it would have to be resumed. If our connect() listener resumes it,
0N/A // what about the case where the EventSet contains other events with
0N/A // SUSPEND_ALL and there are other listeners who expect the BE to still
0N/A // be suspended when their handlers get called?
0N/A ourExceptionRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
0N/A ourExceptionRequest.enable();
0N/A }
0N/A
0N/A private class EventHandler implements Runnable {
0N/A EventHandler() {
0N/A Thread thread = new Thread(this);
0N/A thread.setDaemon(true);
0N/A thread.start();
0N/A }
0N/A
0N/A private void notifyEvent(TargetListener listener, Event event) {
0N/A if (event instanceof BreakpointEvent) {
0N/A listener.breakpointReached((BreakpointEvent)event);
0N/A } else if (event instanceof ExceptionEvent) {
0N/A listener.exceptionThrown((ExceptionEvent)event);
0N/A } else if (event instanceof StepEvent) {
0N/A listener.stepCompleted((StepEvent)event);
0N/A } else if (event instanceof ClassPrepareEvent) {
0N/A listener.classPrepared((ClassPrepareEvent)event);
0N/A } else if (event instanceof ClassUnloadEvent) {
0N/A listener.classUnloaded((ClassUnloadEvent)event);
0N/A } else if (event instanceof MethodEntryEvent) {
0N/A listener.methodEntered((MethodEntryEvent)event);
0N/A } else if (event instanceof MethodExitEvent) {
0N/A listener.methodExited((MethodExitEvent)event);
0N/A } else if (event instanceof MonitorContendedEnterEvent) {
0N/A listener.monitorContendedEnter((MonitorContendedEnterEvent)event);
0N/A } else if (event instanceof MonitorContendedEnteredEvent) {
0N/A listener.monitorContendedEntered((MonitorContendedEnteredEvent)event);
0N/A } else if (event instanceof MonitorWaitEvent) {
0N/A listener.monitorWait((MonitorWaitEvent)event);
0N/A } else if (event instanceof MonitorWaitedEvent) {
0N/A listener.monitorWaited((MonitorWaitedEvent)event);
0N/A } else if (event instanceof AccessWatchpointEvent) {
0N/A listener.fieldAccessed((AccessWatchpointEvent)event);
0N/A } else if (event instanceof ModificationWatchpointEvent) {
0N/A listener.fieldModified((ModificationWatchpointEvent)event);
0N/A } else if (event instanceof ThreadStartEvent) {
0N/A listener.threadStarted((ThreadStartEvent)event);
0N/A } else if (event instanceof ThreadDeathEvent) {
0N/A listener.threadDied((ThreadDeathEvent)event);
0N/A } else if (event instanceof VMStartEvent) {
0N/A listener.vmStarted((VMStartEvent)event);
0N/A } else if (event instanceof VMDeathEvent) {
0N/A listener.vmDied((VMDeathEvent)event);
0N/A } else if (event instanceof VMDisconnectEvent) {
0N/A listener.vmDisconnected((VMDisconnectEvent)event);
0N/A } else {
0N/A throw new InternalError("Unknown event type: " + event.getClass());
0N/A }
0N/A }
0N/A
0N/A private void traceSuspendPolicy(int policy) {
0N/A if (shouldTrace) {
0N/A switch (policy) {
0N/A case EventRequest.SUSPEND_NONE:
0N/A traceln("TS: eventHandler: suspend = SUSPEND_NONE");
0N/A break;
0N/A case EventRequest.SUSPEND_ALL:
0N/A traceln("TS: eventHandler: suspend = SUSPEND_ALL");
0N/A break;
0N/A case EventRequest.SUSPEND_EVENT_THREAD:
0N/A traceln("TS: eventHandler: suspend = SUSPEND_EVENT_THREAD");
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void run() {
0N/A boolean connected = true;
0N/A do {
0N/A try {
0N/A EventSet set = vm.eventQueue().remove();
0N/A traceSuspendPolicy(set.suspendPolicy());
0N/A synchronized (listeners) {
0N/A ListIterator iter = listeners.listIterator();
0N/A while (iter.hasNext()) {
0N/A TargetListener listener = (TargetListener)iter.next();
0N/A traceln("TS: eventHandler: listener = " + listener);
0N/A listener.eventSetReceived(set);
0N/A if (listener.shouldRemoveListener()) {
0N/A iter.remove();
0N/A } else {
0N/A Iterator jter = set.iterator();
0N/A while (jter.hasNext()) {
0N/A Event event = (Event)jter.next();
0N/A traceln("TS: eventHandler: event = " + event.getClass());
0N/A
0N/A if (event instanceof VMDisconnectEvent) {
0N/A connected = false;
0N/A }
0N/A listener.eventReceived(event);
0N/A if (listener.shouldRemoveListener()) {
0N/A iter.remove();
0N/A break;
0N/A }
0N/A notifyEvent(listener, event);
0N/A if (listener.shouldRemoveListener()) {
0N/A iter.remove();
0N/A break;
0N/A }
0N/A }
0N/A traceln("TS: eventHandler: end of events loop");
0N/A if (!listener.shouldRemoveListener()) {
0N/A traceln("TS: eventHandler: calling ESC");
0N/A listener.eventSetComplete(set);
0N/A if (listener.shouldRemoveListener()) {
0N/A iter.remove();
0N/A }
0N/A }
0N/A }
0N/A traceln("TS: eventHandler: end of listeners loop");
0N/A }
0N/A }
0N/A } catch (InterruptedException e) {
0N/A traceln("TS: eventHandler: InterruptedException");
0N/A } catch (Exception e) {
0N/A failure("FAILED: Exception occured in eventHandler: " + e);
0N/A e.printStackTrace();
0N/A connected = false;
0N/A synchronized(TestScaffold.this) {
0N/A // This will make the waiters such as waitForVMDisconnect
0N/A // exit their wait loops.
0N/A vmDisconnected = true;
0N/A TestScaffold.this.notifyAll();
0N/A }
0N/A }
0N/A traceln("TS: eventHandler: End of outerloop");
0N/A } while (connected);
0N/A traceln("TS: eventHandler: finished");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructor
0N/A */
0N/A public TestScaffold(String[] args) {
0N/A this.args = args;
0N/A }
0N/A
0N/A public void enableScaffoldTrace() {
0N/A this.shouldTrace = true;
0N/A }
0N/A
0N/A public void disableScaffoldTrace() {
0N/A this.shouldTrace = false;
0N/A }
0N/A
0N/A /**
0N/A * Helper for the redefine method. Build the map
0N/A * needed for a redefine.
0N/A */
0N/A protected Map makeRedefineMap(ReferenceType rt) throws Exception {
0N/A String className = rt.name();
0N/A File path = new File(System.getProperty("test.classes", "."));
0N/A className = className.replace('.', File.separatorChar);
0N/A File phyl = new File(path, className + ".class");
0N/A byte[] bytes = new byte[(int)phyl.length()];
0N/A InputStream in = new FileInputStream(phyl);
0N/A in.read(bytes);
0N/A in.close();
0N/A
0N/A Map map = new HashMap();
0N/A map.put(rt, bytes);
0N/A
0N/A return map;
0N/A }
0N/A
0N/A /**
0N/A * Redefine a class - HotSwap it
0N/A */
0N/A protected void redefine(ReferenceType rt) {
0N/A try {
0N/A println("Redefining " + rt);
0N/A vm().redefineClasses(makeRedefineMap(rt));
0N/A } catch (Exception exc) {
0N/A failure("FAIL: redefine - unexpected exception: " + exc);
0N/A }
0N/A }
0N/A
0N/A protected void startUp(String targetName) {
0N/A List argList = new ArrayList(Arrays.asList(args));
0N/A argList.add(targetName);
0N/A println("run args: " + argList);
0N/A connect((String[]) argList.toArray(args));
0N/A waitForVMStart();
0N/A }
0N/A
0N/A protected BreakpointEvent startToMain(String targetName) {
0N/A return startTo(targetName, "main", "([Ljava/lang/String;)V");
0N/A }
0N/A
0N/A protected BreakpointEvent startTo(String targetName,
0N/A String methodName, String signature) {
0N/A startUp(targetName);
0N/A traceln("TS: back from startUp");
0N/A
0N/A BreakpointEvent bpr = resumeTo(targetName, methodName,
0N/A signature);
0N/A Location loc = bpr.location();
0N/A mainStartClass = loc.declaringType();
0N/A if (redefineAtStart) {
0N/A redefine(mainStartClass);
0N/A }
0N/A if (redefineAsynchronously) {
0N/A Thread asyncDaemon = new Thread("Async Redefine") {
0N/A public void run() {
0N/A try {
0N/A Map redefMap = makeRedefineMap(mainStartClass);
0N/A
0N/A while (true) {
0N/A println("Redefining " + mainStartClass);
0N/A vm().redefineClasses(redefMap);
0N/A Thread.sleep(100);
0N/A }
0N/A } catch (VMDisconnectedException vmde) {
0N/A println("async redefine - VM disconnected");
0N/A } catch (Exception exc) {
0N/A failure("FAIL: async redefine - unexpected exception: " + exc);
0N/A }
0N/A }
0N/A };
0N/A asyncDaemon.setDaemon(true);
0N/A asyncDaemon.start();
0N/A }
0N/A
0N/A if (System.getProperty("jpda.wait") != null) {
0N/A waitForInput();
0N/A }
0N/A return bpr;
0N/A }
0N/A
0N/A protected void waitForInput() {
0N/A try {
0N/A System.err.println("Press <enter> to continue");
0N/A System.in.read();
0N/A System.err.println("running...");
0N/A
0N/A } catch(Exception e) {
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Test cases should implement tests in runTests and should
0N/A * initiate testing by calling run().
0N/A */
0N/A abstract protected void runTests() throws Exception;
0N/A
0N/A final public void startTests() throws Exception {
0N/A try {
0N/A runTests();
0N/A } finally {
0N/A shutdown();
0N/A }
0N/A }
0N/A
0N/A protected void println(String str) {
0N/A System.err.println(str);
0N/A }
0N/A
0N/A protected void print(String str) {
0N/A System.err.print(str);
0N/A }
0N/A
0N/A protected void traceln(String str) {
0N/A if (shouldTrace) {
0N/A println(str);
0N/A }
0N/A }
0N/A
0N/A protected void failure(String str) {
0N/A println(str);
0N/A testFailed = true;
0N/A }
0N/A
0N/A private ArgInfo parseArgs(String args[]) {
0N/A ArgInfo argInfo = new ArgInfo();
0N/A for (int i = 0; i < args.length; i++) {
0N/A if (args[i].equals("-connect")) {
0N/A i++;
0N/A argInfo.connectorSpec = args[i];
0N/A } else if (args[i].equals("-trace")) {
0N/A i++;
0N/A argInfo.traceFlags = Integer.decode(args[i]).intValue();
0N/A } else if (args[i].equals("-redefstart")) {
0N/A redefineAtStart = true;
0N/A } else if (args[i].equals("-redefevent")) {
0N/A redefineAtEvents = true;
0N/A } else if (args[i].equals("-redefasync")) {
0N/A redefineAsynchronously = true;
0N/A } else if (args[i].startsWith("-J")) {
0N/A argInfo.targetVMArgs += (args[i].substring(2) + ' ');
0N/A
0N/A /*
0N/A * classpath can span two arguments so we need to handle
0N/A * it specially.
0N/A */
0N/A if (args[i].equals("-J-classpath")) {
0N/A i++;
0N/A argInfo.targetVMArgs += (args[i] + ' ');
0N/A }
0N/A } else {
0N/A argInfo.targetAppCommandLine += (args[i] + ' ');
0N/A }
0N/A }
0N/A return argInfo;
0N/A }
0N/A
0N/A /**
0N/A * This is called to connect to a debuggee VM. It starts the VM and
0N/A * installs a listener to catch VMStartEvent, our default events, and
0N/A * VMDisconnectedEvent. When these events appear, that is remembered
0N/A * and waiters are notified.
0N/A * This is normally called in the main thread of the test case.
0N/A * It starts up an EventHandler thread that gets events coming in
0N/A * from the debuggee and distributes them to listeners. That thread
0N/A * keeps running until a VMDisconnectedEvent occurs or some exception
0N/A * occurs during its processing.
0N/A *
0N/A * The 'listenUntilVMDisconnect' method adds 'this' as a listener.
0N/A * This means that 'this's vmDied method will get called. This has a
0N/A * default impl in TargetAdapter.java which can be overridden in the
0N/A * testcase.
0N/A *
0N/A * waitForRequestedEvent also adds an adaptor listener that listens
0N/A * for the particular event it is supposed to wait for (and it also
0N/A * catches VMDisconnectEvents.) This listener is removed once
0N/A * its eventReceived method is called.
0N/A * waitForRequestedEvent is called by most of the methods to do bkpts,
0N/A * etc.
0N/A */
0N/A public void connect(String args[]) {
0N/A ArgInfo argInfo = parseArgs(args);
0N/A
0N/A argInfo.targetVMArgs += VMConnection.getDebuggeeVMOptions();
0N/A connection = new VMConnection(argInfo.connectorSpec,
0N/A argInfo.traceFlags);
0N/A
0N/A addListener(new TargetAdapter() {
0N/A public void eventSetComplete(EventSet set) {
0N/A if (TestScaffold.this.containsOurVMDeathRequest(set)) {
0N/A traceln("TS: connect: set.resume() called");
0N/A set.resume();
0N/A
0N/A // Note that we want to do the above resume before
0N/A // waking up any sleepers.
0N/A synchronized(TestScaffold.this) {
0N/A TestScaffold.this.notifyAll();
0N/A }
0N/A }
0N/A }
0N/A public void eventReceived(Event event) {
0N/A if (redefineAtEvents && event instanceof Locatable) {
0N/A Location loc = ((Locatable)event).location();
0N/A ReferenceType rt = loc.declaringType();
0N/A String name = rt.name();
0N/A if (name.startsWith("java.") &&
0N/A !name.startsWith("sun.") &&
0N/A !name.startsWith("com.")) {
0N/A if (mainStartClass != null) {
0N/A redefine(mainStartClass);
0N/A }
0N/A } else {
0N/A redefine(rt);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void vmStarted(VMStartEvent event) {
0N/A synchronized(TestScaffold.this) {
0N/A vmStartThread = event.thread();
0N/A TestScaffold.this.notifyAll();
0N/A }
0N/A }
0N/A /**
0N/A * By default, we catch uncaught exceptions and print a msg.
0N/A * The testcase must override the createDefaultExceptionRequest
0N/A * method if it doesn't want this behavior.
0N/A */
0N/A public void exceptionThrown(ExceptionEvent event) {
0N/A if (TestScaffold.this.ourExceptionRequest != null &&
0N/A TestScaffold.this.ourExceptionRequest.equals(
0N/A event.request())) {
0N/A /*
0N/A * See
0N/A * 5038723: com/sun/jdi/sde/TemperatureTableTest.java:
0N/A * intermittent ObjectCollectedException
0N/A * Since this request was SUSPEND_NONE, the debuggee
0N/A * could keep running and the calls below back into
0N/A * the debuggee might not work. That is why we
0N/A * have this try/catch.
0N/A */
0N/A try {
0N/A println("Note: Unexpected Debuggee Exception: " +
0N/A event.exception().referenceType().name() +
0N/A " at line " + event.location().lineNumber());
0N/A TestScaffold.this.exceptionCaught = true;
0N/A
0N/A ObjectReference obj = event.exception();
0N/A ReferenceType rtt = obj.referenceType();
0N/A Field detail = rtt.fieldByName("detailMessage");
0N/A Value val = obj.getValue(detail);
0N/A println("detailMessage = " + val);
0N/A
0N/A /*
0N/A * This code is commented out because it needs a thread
0N/A * in which to do the invokeMethod and we don't have
0N/A * one. To enable this code change the request
0N/A * to be SUSPEND_ALL in createDefaultExceptionRequest,
0N/A * and then put this line
0N/A * mainThread = bpe.thread();
0N/A * in the testcase after the line
0N/A * BreakpointEvent bpe = startToMain("....");
0N/A */
0N/A if (false) {
0N/A List lll = rtt.methodsByName("printStackTrace");
0N/A Method mm = (Method)lll.get(0);
0N/A obj.invokeMethod(mainThread, mm, new ArrayList(0), 0);
0N/A }
0N/A } catch (Exception ee) {
0N/A println("TestScaffold Exception while handling debuggee Exception: "
0N/A + ee);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void vmDied(VMDeathEvent event) {
0N/A vmDied = true;
0N/A traceln("TS: vmDied called");
0N/A }
0N/A
0N/A public void vmDisconnected(VMDisconnectEvent event) {
0N/A synchronized(TestScaffold.this) {
0N/A vmDisconnected = true;
0N/A TestScaffold.this.notifyAll();
0N/A }
0N/A }
0N/A });
0N/A if (connection.connector().name().equals("com.sun.jdi.CommandLineLaunch")) {
0N/A if (argInfo.targetVMArgs.length() > 0) {
0N/A if (connection.connectorArg("options").length() > 0) {
0N/A throw new IllegalArgumentException("VM options in two places");
0N/A }
0N/A connection.setConnectorArg("options", argInfo.targetVMArgs);
0N/A }
0N/A if (argInfo.targetAppCommandLine.length() > 0) {
0N/A if (connection.connectorArg("main").length() > 0) {
0N/A throw new IllegalArgumentException("Command line in two places");
0N/A }
0N/A connection.setConnectorArg("main", argInfo.targetAppCommandLine);
0N/A }
0N/A }
0N/A
0N/A vm = connection.open();
0N/A requestManager = vm.eventRequestManager();
0N/A createDefaultEventRequests();
0N/A new EventHandler();
0N/A }
0N/A
0N/A
0N/A public VirtualMachine vm() {
0N/A return vm;
0N/A }
0N/A
0N/A public EventRequestManager eventRequestManager() {
0N/A return requestManager;
0N/A }
0N/A
0N/A public void addListener(TargetListener listener) {
0N/A traceln("TS: Adding listener " + listener);
0N/A listeners.add(listener);
0N/A }
0N/A
0N/A public void removeListener(TargetListener listener) {
0N/A traceln("TS: Removing listener " + listener);
0N/A listeners.remove(listener);
0N/A }
0N/A
0N/A
0N/A protected void listenUntilVMDisconnect() {
0N/A try {
0N/A addListener (this);
0N/A } catch (Exception ex){
0N/A ex.printStackTrace();
0N/A testFailed = true;
0N/A } finally {
0N/A // Allow application to complete and shut down
0N/A resumeToVMDisconnect();
0N/A }
0N/A }
0N/A
0N/A public synchronized ThreadReference waitForVMStart() {
0N/A while ((vmStartThread == null) && !vmDisconnected) {
0N/A try {
0N/A wait();
0N/A } catch (InterruptedException e) {
0N/A }
0N/A }
0N/A
0N/A if (vmStartThread == null) {
0N/A throw new VMDisconnectedException();
0N/A }
0N/A
0N/A return vmStartThread;
0N/A }
0N/A
0N/A public synchronized void waitForVMDisconnect() {
0N/A traceln("TS: waitForVMDisconnect");
0N/A while (!vmDisconnected) {
0N/A try {
0N/A wait();
0N/A } catch (InterruptedException e) {
0N/A }
0N/A }
0N/A traceln("TS: waitForVMDisconnect: done");
0N/A }
0N/A
0N/A public Event waitForRequestedEvent(final EventRequest request) {
0N/A class EventNotification {
0N/A Event event;
0N/A boolean disconnected = false;
0N/A }
0N/A final EventNotification en = new EventNotification();
0N/A
0N/A TargetAdapter adapter = new TargetAdapter() {
0N/A public void eventReceived(Event event) {
0N/A if (request.equals(event.request())) {
0N/A traceln("TS:Listener2: got requested event");
0N/A synchronized (en) {
0N/A en.event = event;
0N/A en.notifyAll();
0N/A }
0N/A removeThisListener();
0N/A } else if (event instanceof VMDisconnectEvent) {
0N/A traceln("TS:Listener2: got VMDisconnectEvent");
0N/A synchronized (en) {
0N/A en.disconnected = true;
0N/A en.notifyAll();
0N/A }
0N/A removeThisListener();
0N/A }
0N/A }
0N/A };
0N/A
0N/A addListener(adapter);
0N/A
0N/A try {
0N/A synchronized (en) {
0N/A traceln("TS: waitForRequestedEvent: vm.resume called");
0N/A vm.resume();
0N/A
0N/A while (!en.disconnected && (en.event == null)) {
0N/A en.wait();
0N/A }
0N/A }
0N/A } catch (InterruptedException e) {
0N/A return null;
0N/A }
0N/A
0N/A if (en.disconnected) {
0N/A throw new RuntimeException("VM Disconnected before requested event occurred");
0N/A }
0N/A return en.event;
0N/A }
0N/A
0N/A private StepEvent doStep(ThreadReference thread, int gran, int depth) {
0N/A final StepRequest sr =
0N/A requestManager.createStepRequest(thread, gran, depth);
0N/A
0N/A sr.addClassExclusionFilter("java.*");
0N/A sr.addClassExclusionFilter("sun.*");
0N/A sr.addClassExclusionFilter("com.sun.*");
0N/A sr.addCountFilter(1);
0N/A sr.enable();
0N/A StepEvent retEvent = (StepEvent)waitForRequestedEvent(sr);
0N/A requestManager.deleteEventRequest(sr);
0N/A return retEvent;
0N/A }
0N/A
0N/A public StepEvent stepIntoInstruction(ThreadReference thread) {
0N/A return doStep(thread, StepRequest.STEP_MIN, StepRequest.STEP_INTO);
0N/A }
0N/A
0N/A public StepEvent stepIntoLine(ThreadReference thread) {
0N/A return doStep(thread, StepRequest.STEP_LINE, StepRequest.STEP_INTO);
0N/A }
0N/A
0N/A public StepEvent stepOverInstruction(ThreadReference thread) {
0N/A return doStep(thread, StepRequest.STEP_MIN, StepRequest.STEP_OVER);
0N/A }
0N/A
0N/A public StepEvent stepOverLine(ThreadReference thread) {
0N/A return doStep(thread, StepRequest.STEP_LINE, StepRequest.STEP_OVER);
0N/A }
0N/A
0N/A public StepEvent stepOut(ThreadReference thread) {
0N/A return doStep(thread, StepRequest.STEP_LINE, StepRequest.STEP_OUT);
0N/A }
0N/A
0N/A public BreakpointEvent resumeTo(Location loc) {
0N/A final BreakpointRequest request =
0N/A requestManager.createBreakpointRequest(loc);
0N/A request.addCountFilter(1);
0N/A request.enable();
0N/A return (BreakpointEvent)waitForRequestedEvent(request);
0N/A }
0N/A
0N/A public ReferenceType findReferenceType(String name) {
0N/A List rts = vm.classesByName(name);
0N/A Iterator iter = rts.iterator();
0N/A while (iter.hasNext()) {
0N/A ReferenceType rt = (ReferenceType)iter.next();
0N/A if (rt.name().equals(name)) {
0N/A return rt;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A public Method findMethod(ReferenceType rt, String name, String signature) {
0N/A List methods = rt.methods();
0N/A Iterator iter = methods.iterator();
0N/A while (iter.hasNext()) {
0N/A Method method = (Method)iter.next();
0N/A if (method.name().equals(name) &&
0N/A method.signature().equals(signature)) {
0N/A return method;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A public Location findLocation(ReferenceType rt, int lineNumber)
0N/A throws AbsentInformationException {
0N/A List locs = rt.locationsOfLine(lineNumber);
0N/A if (locs.size() == 0) {
0N/A throw new IllegalArgumentException("Bad line number");
0N/A } else if (locs.size() > 1) {
0N/A throw new IllegalArgumentException("Line number has multiple locations");
0N/A }
0N/A
0N/A return (Location)locs.get(0);
0N/A }
0N/A
0N/A public BreakpointEvent resumeTo(String clsName, String methodName,
0N/A String methodSignature) {
0N/A ReferenceType rt = findReferenceType(clsName);
0N/A if (rt == null) {
0N/A rt = resumeToPrepareOf(clsName).referenceType();
0N/A }
0N/A
0N/A Method method = findMethod(rt, methodName, methodSignature);
0N/A if (method == null) {
0N/A throw new IllegalArgumentException("Bad method name/signature");
0N/A }
0N/A
0N/A return resumeTo(method.location());
0N/A }
0N/A
0N/A public BreakpointEvent resumeTo(String clsName, int lineNumber) throws AbsentInformationException {
0N/A ReferenceType rt = findReferenceType(clsName);
0N/A if (rt == null) {
0N/A rt = resumeToPrepareOf(clsName).referenceType();
0N/A }
0N/A
0N/A return resumeTo(findLocation(rt, lineNumber));
0N/A }
0N/A
0N/A public ClassPrepareEvent resumeToPrepareOf(String className) {
0N/A final ClassPrepareRequest request =
0N/A requestManager.createClassPrepareRequest();
0N/A request.addClassFilter(className);
0N/A request.addCountFilter(1);
0N/A request.enable();
0N/A return (ClassPrepareEvent)waitForRequestedEvent(request);
0N/A }
0N/A
0N/A public void resumeForMsecs(long msecs) {
0N/A try {
0N/A addListener (this);
0N/A } catch (Exception ex){
0N/A ex.printStackTrace();
0N/A testFailed = true;
0N/A return;
0N/A }
0N/A
0N/A try {
0N/A vm().resume();
0N/A } catch (VMDisconnectedException e) {
0N/A }
0N/A
0N/A if (!vmDisconnected) {
0N/A try {
0N/A System.out.println("Sleeping for " + msecs + " milleseconds");
0N/A Thread.sleep(msecs);
0N/A vm().suspend();
0N/A } catch (InterruptedException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void resumeToVMDisconnect() {
0N/A try {
0N/A traceln("TS: resumeToVMDisconnect: vm.resume called");
0N/A vm.resume();
0N/A } catch (VMDisconnectedException e) {
0N/A // clean up below
0N/A }
0N/A waitForVMDisconnect();
0N/A }
0N/A
0N/A public void shutdown() {
0N/A shutdown(null);
0N/A }
0N/A
0N/A public void shutdown(String message) {
0N/A traceln("TS: shutdown: vmDied= " + vmDied +
0N/A ", vmDisconnected= " + vmDisconnected +
0N/A ", connection = " + connection);
0N/A
0N/A if ((connection != null)) {
0N/A try {
0N/A connection.disposeVM();
0N/A } catch (VMDisconnectedException e) {
0N/A // Shutting down after the VM has gone away. This is
0N/A // not an error, and we just ignore it.
0N/A }
0N/A } else {
0N/A traceln("TS: shutdown: disposeVM not called");
0N/A }
0N/A if (message != null) {
0N/A println(message);
0N/A }
0N/A
0N/A vmDied = true;
0N/A vmDisconnected = true;
0N/A }
0N/A}