jdk/test/com/sun/jdi/TestScaffold.java
author jjg
Thu, 22 May 2008 15:51:41 -0700
changeset 655 1ebc7ce89018
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6705945: com.sun.tools.javac.zip files do not have valid copyright Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import com.sun.jdi.request.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Framework used by all JDI regression tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
abstract public class TestScaffold extends TargetAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private boolean shouldTrace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private VMConnection connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private VirtualMachine vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private EventRequestManager requestManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private List listeners = Collections.synchronizedList(new LinkedList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private boolean redefineAtStart = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private boolean redefineAtEvents = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private boolean redefineAsynchronously = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private ReferenceType mainStartClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    ThreadReference mainThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * We create a VMDeathRequest, SUSPEND_ALL, to sync the BE and FE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private VMDeathRequest ourVMDeathRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * We create an ExceptionRequest, SUSPEND_NONE so that we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * catch it and output a msg if an exception occurs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * debuggee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private ExceptionRequest ourExceptionRequest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * If we do catch an uncaught exception, we set this true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * so the testcase can find out if it wants to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private boolean exceptionCaught = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    ThreadReference vmStartThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    boolean vmDied = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    boolean vmDisconnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    final String[] args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected boolean testFailed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static private class ArgInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        String targetVMArgs = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        String targetAppCommandLine = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        String connectorSpec = "com.sun.jdi.CommandLineLaunch:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        int traceFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * An easy way to sleep for awhile
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void mySleep(int millis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            Thread.sleep(millis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } catch (InterruptedException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    boolean getExceptionCaught() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return exceptionCaught;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    void setExceptionCaught(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        exceptionCaught = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Return true if eventSet contains the VMDeathEvent for the request in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * the ourVMDeathRequest ivar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private boolean containsOurVMDeathRequest(EventSet eventSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (ourVMDeathRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            Iterator myIter = eventSet.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            while (myIter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                Event myEvent = (Event)myIter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (!(myEvent instanceof VMDeathEvent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    // We assume that an EventSet contains only VMDeathEvents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    // or no VMDeathEvents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                if (ourVMDeathRequest.equals(myEvent.request())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * The following methods override those in our base class, TargetAdapter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Events handled directly by scaffold always resume (well, almost always)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void eventSetComplete(EventSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // The listener in connect(..) resumes after receiving our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // special VMDeathEvent.  We can't also do the resume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // here or we will probably get a VMDisconnectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (!containsOurVMDeathRequest(set)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            traceln("TS: set.resume() called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            set.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * This method sets up default requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Testcases can override this to change default behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected void createDefaultEventRequests() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        createDefaultVMDeathRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        createDefaultExceptionRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * We want the BE to stop when it issues a VMDeathEvent in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * give the FE time to complete handling events that occured before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * the VMDeath.  When we get the VMDeathEvent for this request in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the listener in connect(), we will do a resume.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * If a testcase wants to do something special with VMDeathEvent's,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * then it should override this method with an empty method or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * whatever in order to suppress the automatic resume.  The testcase
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * will then be responsible for the handling of VMDeathEvents.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * has to be sure that it does a resume if it gets a VMDeathEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * with SUSPEND_ALL, and it has to be sure that it doesn't do a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * resume after getting a VMDeath with SUSPEND_NONE (the automatically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * generated VMDeathEvent.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected void createDefaultVMDeathRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        ourVMDeathRequest = requestManager.createVMDeathRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        ourVMDeathRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        ourVMDeathRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * This will allow us to print a warning if a debuggee gets an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * unexpected exception.  The unexpected exception will be handled in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * the exceptionThrown method in the listener created in the connect()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * If a testcase does not want an uncaught exception to cause a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * msg, it must override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    protected void createDefaultExceptionRequest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        ourExceptionRequest = requestManager.createExceptionRequest(null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                                                false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // We can't afford to make this be other than SUSPEND_NONE.  Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        // it would have to be resumed.  If our connect() listener resumes it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // what about the case where the EventSet contains other events with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // SUSPEND_ALL and there are other listeners who expect the BE to still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // be suspended when their handlers get called?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ourExceptionRequest.setSuspendPolicy(EventRequest.SUSPEND_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        ourExceptionRequest.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private class EventHandler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        EventHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            Thread thread = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            thread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        private void notifyEvent(TargetListener listener, Event event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (event instanceof BreakpointEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                listener.breakpointReached((BreakpointEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } else if (event instanceof ExceptionEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                listener.exceptionThrown((ExceptionEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } else if (event instanceof StepEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                listener.stepCompleted((StepEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } else if (event instanceof ClassPrepareEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                listener.classPrepared((ClassPrepareEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            } else if (event instanceof ClassUnloadEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                listener.classUnloaded((ClassUnloadEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            } else if (event instanceof MethodEntryEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                listener.methodEntered((MethodEntryEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            } else if (event instanceof MethodExitEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                listener.methodExited((MethodExitEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } else if (event instanceof MonitorContendedEnterEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                listener.monitorContendedEnter((MonitorContendedEnterEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } else if (event instanceof MonitorContendedEnteredEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                listener.monitorContendedEntered((MonitorContendedEnteredEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            } else if (event instanceof MonitorWaitEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                listener.monitorWait((MonitorWaitEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            } else if (event instanceof MonitorWaitedEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                listener.monitorWaited((MonitorWaitedEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } else if (event instanceof AccessWatchpointEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                listener.fieldAccessed((AccessWatchpointEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } else if (event instanceof ModificationWatchpointEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                listener.fieldModified((ModificationWatchpointEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } else if (event instanceof ThreadStartEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                listener.threadStarted((ThreadStartEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            } else if (event instanceof ThreadDeathEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                listener.threadDied((ThreadDeathEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            } else if (event instanceof VMStartEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                listener.vmStarted((VMStartEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            } else if (event instanceof VMDeathEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                listener.vmDied((VMDeathEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            } else if (event instanceof VMDisconnectEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                listener.vmDisconnected((VMDisconnectEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                throw new InternalError("Unknown event type: " + event.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        private void traceSuspendPolicy(int policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            if (shouldTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                switch (policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                case EventRequest.SUSPEND_NONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                    traceln("TS: eventHandler: suspend = SUSPEND_NONE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                case EventRequest.SUSPEND_ALL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    traceln("TS: eventHandler: suspend = SUSPEND_ALL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                case EventRequest.SUSPEND_EVENT_THREAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    traceln("TS: eventHandler: suspend = SUSPEND_EVENT_THREAD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            boolean connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    EventSet set = vm.eventQueue().remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    traceSuspendPolicy(set.suspendPolicy());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    synchronized (listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        ListIterator iter = listeners.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            TargetListener listener = (TargetListener)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            traceln("TS: eventHandler: listener = " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            listener.eventSetReceived(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                            if (listener.shouldRemoveListener()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                Iterator jter = set.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                while (jter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                    Event event = (Event)jter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                    traceln("TS: eventHandler:    event = " + event.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                    if (event instanceof VMDisconnectEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                        connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                    listener.eventReceived(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                    if (listener.shouldRemoveListener()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                        iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                                    notifyEvent(listener, event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                    if (listener.shouldRemoveListener()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                        iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                traceln("TS: eventHandler:   end of events loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                                if (!listener.shouldRemoveListener()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                    traceln("TS: eventHandler:   calling ESC");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                    listener.eventSetComplete(set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                    if (listener.shouldRemoveListener()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                        iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                            traceln("TS: eventHandler: end of listeners loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    traceln("TS: eventHandler: InterruptedException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    failure("FAILED: Exception occured in eventHandler: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    synchronized(TestScaffold.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        // This will make the waiters such as waitForVMDisconnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        // exit their wait loops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                        vmDisconnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        TestScaffold.this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                traceln("TS: eventHandler: End of outerloop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            } while (connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            traceln("TS: eventHandler: finished");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public TestScaffold(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        this.args = args;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public void enableScaffoldTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        this.shouldTrace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public void disableScaffoldTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        this.shouldTrace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Helper for the redefine method.  Build the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * needed for a redefine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    protected Map makeRedefineMap(ReferenceType rt) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        String className = rt.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        File path = new File(System.getProperty("test.classes", "."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        className = className.replace('.', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        File phyl = new File(path, className + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        byte[] bytes = new byte[(int)phyl.length()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        InputStream in = new FileInputStream(phyl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        in.read(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        Map map = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        map.put(rt, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Redefine a class - HotSwap it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    protected void redefine(ReferenceType rt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            println("Redefining " + rt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            vm().redefineClasses(makeRedefineMap(rt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            failure("FAIL: redefine - unexpected exception: " + exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    protected void startUp(String targetName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        List argList = new ArrayList(Arrays.asList(args));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        argList.add(targetName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        println("run args: " + argList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        connect((String[]) argList.toArray(args));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        waitForVMStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    protected BreakpointEvent startToMain(String targetName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        return startTo(targetName, "main", "([Ljava/lang/String;)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    protected BreakpointEvent startTo(String targetName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                      String methodName, String signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        startUp(targetName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        traceln("TS: back from startUp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        BreakpointEvent bpr = resumeTo(targetName, methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                       signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        Location loc = bpr.location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        mainStartClass = loc.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (redefineAtStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            redefine(mainStartClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (redefineAsynchronously) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            Thread asyncDaemon = new Thread("Async Redefine") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        Map redefMap = makeRedefineMap(mainStartClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                            println("Redefining " + mainStartClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                            vm().redefineClasses(redefMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                            Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    } catch (VMDisconnectedException vmde) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                        println("async redefine - VM disconnected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                        failure("FAIL: async redefine - unexpected exception: " + exc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            asyncDaemon.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            asyncDaemon.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (System.getProperty("jpda.wait") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            waitForInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return bpr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    protected void waitForInput() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            System.err.println("Press <enter> to continue");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            System.in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            System.err.println("running...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Test cases should implement tests in runTests and should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * initiate testing by calling run().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    abstract protected void runTests() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    final public void startTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            runTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    protected void println(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        System.err.println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    protected void print(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        System.err.print(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    protected void traceln(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if (shouldTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    protected void failure(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private ArgInfo parseArgs(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        ArgInfo argInfo = new ArgInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        for (int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            if (args[i].equals("-connect")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                argInfo.connectorSpec = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            } else if (args[i].equals("-trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                argInfo.traceFlags = Integer.decode(args[i]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            } else if (args[i].equals("-redefstart")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                redefineAtStart = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            } else if (args[i].equals("-redefevent")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                redefineAtEvents = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            } else if (args[i].equals("-redefasync")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                redefineAsynchronously = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            } else if (args[i].startsWith("-J")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                argInfo.targetVMArgs += (args[i].substring(2) + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                 * classpath can span two arguments so we need to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                 * it specially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                if (args[i].equals("-J-classpath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    argInfo.targetVMArgs += (args[i] + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                argInfo.targetAppCommandLine += (args[i] + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return argInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * This is called to connect to a debuggee VM.  It starts the VM and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * installs a listener to catch VMStartEvent, our default events, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * VMDisconnectedEvent.  When these events appear, that is remembered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * and waiters are notified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * This is normally called in the main thread of the test case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * It starts up an EventHandler thread that gets events coming in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * from the debuggee and distributes them to listeners.  That thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * keeps running until a VMDisconnectedEvent occurs or some exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * occurs during its processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * The 'listenUntilVMDisconnect' method adds 'this' as a listener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * This means that 'this's vmDied method will get called.  This has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * default impl in TargetAdapter.java which can be overridden in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * testcase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * waitForRequestedEvent also adds an adaptor listener that listens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * for the particular event it is supposed to wait for (and it also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * catches VMDisconnectEvents.)  This listener is removed once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * its eventReceived method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * waitForRequestedEvent is called by most of the methods to do bkpts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public void connect(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        ArgInfo argInfo = parseArgs(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        argInfo.targetVMArgs += VMConnection.getDebuggeeVMOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        connection = new VMConnection(argInfo.connectorSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                      argInfo.traceFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        addListener(new TargetAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                public void eventSetComplete(EventSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    if (TestScaffold.this.containsOurVMDeathRequest(set)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                        traceln("TS: connect: set.resume() called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        set.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        // Note that we want to do the above resume before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        // waking up any sleepers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                        synchronized(TestScaffold.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                            TestScaffold.this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                public void eventReceived(Event event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    if (redefineAtEvents && event instanceof Locatable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                        Location loc = ((Locatable)event).location();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                        ReferenceType rt = loc.declaringType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        String name = rt.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        if (name.startsWith("java.") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                       !name.startsWith("sun.") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                                       !name.startsWith("com.")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                            if (mainStartClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                                redefine(mainStartClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                            redefine(rt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                public void vmStarted(VMStartEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    synchronized(TestScaffold.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                        vmStartThread = event.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        TestScaffold.this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                 * By default, we catch uncaught exceptions and print a msg.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                 * The testcase must override the createDefaultExceptionRequest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                 * method if it doesn't want this behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                public void exceptionThrown(ExceptionEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    if (TestScaffold.this.ourExceptionRequest != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                        TestScaffold.this.ourExceptionRequest.equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                                                        event.request())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                         * See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                         *    5038723: com/sun/jdi/sde/TemperatureTableTest.java:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                         *             intermittent ObjectCollectedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                         * Since this request was SUSPEND_NONE, the debuggee
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                         * could keep running and the calls below back into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                         * the debuggee might not work.  That is why we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                         * have this try/catch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                            println("Note: Unexpected Debuggee Exception: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                    event.exception().referenceType().name() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                    " at line " + event.location().lineNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                            TestScaffold.this.exceptionCaught = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                            ObjectReference obj = event.exception();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                            ReferenceType rtt = obj.referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                            Field detail = rtt.fieldByName("detailMessage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                            Value val = obj.getValue(detail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                            println("detailMessage = " + val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                             * This code is commented out because it needs a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                             * in which to do the invokeMethod and we don't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                             * one.  To enable this code change the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                             * to be SUSPEND_ALL in createDefaultExceptionRequest,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                             * and then put this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                             *    mainThread = bpe.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                             * in the testcase after the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                             *    BreakpointEvent bpe = startToMain("....");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                            if (false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                                List lll = rtt.methodsByName("printStackTrace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                                Method mm = (Method)lll.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                                obj.invokeMethod(mainThread, mm, new ArrayList(0), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                        } catch (Exception ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                            println("TestScaffold Exception while handling debuggee Exception: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                                    + ee);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                public void vmDied(VMDeathEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    vmDied = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    traceln("TS: vmDied called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                public void vmDisconnected(VMDisconnectEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                    synchronized(TestScaffold.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        vmDisconnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        TestScaffold.this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (connection.connector().name().equals("com.sun.jdi.CommandLineLaunch")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            if (argInfo.targetVMArgs.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                if (connection.connectorArg("options").length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    throw new IllegalArgumentException("VM options in two places");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                connection.setConnectorArg("options", argInfo.targetVMArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            if (argInfo.targetAppCommandLine.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                if (connection.connectorArg("main").length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    throw new IllegalArgumentException("Command line in two places");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                connection.setConnectorArg("main", argInfo.targetAppCommandLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        vm = connection.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        requestManager = vm.eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        createDefaultEventRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        new EventHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    public VirtualMachine vm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public EventRequestManager eventRequestManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return requestManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public void addListener(TargetListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        traceln("TS: Adding listener " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        listeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    public void removeListener(TargetListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        traceln("TS: Removing listener " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        listeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    protected void listenUntilVMDisconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            addListener (this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        } catch (Exception ex){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            // Allow application to complete and shut down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            resumeToVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public synchronized ThreadReference waitForVMStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        while ((vmStartThread == null) && !vmDisconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        if (vmStartThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            throw new VMDisconnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        return vmStartThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public synchronized void waitForVMDisconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        traceln("TS: waitForVMDisconnect");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        while (!vmDisconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        traceln("TS: waitForVMDisconnect: done");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public Event waitForRequestedEvent(final EventRequest request) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        class EventNotification {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            Event event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            boolean disconnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        final EventNotification en = new EventNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        TargetAdapter adapter = new TargetAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            public void eventReceived(Event event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                if (request.equals(event.request())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    traceln("TS:Listener2: got requested event");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    synchronized (en) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                        en.event = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                        en.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    removeThisListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                } else if (event instanceof VMDisconnectEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    traceln("TS:Listener2: got VMDisconnectEvent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    synchronized (en) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                        en.disconnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                        en.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    removeThisListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        addListener(adapter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            synchronized (en) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                traceln("TS: waitForRequestedEvent: vm.resume called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                while (!en.disconnected && (en.event == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    en.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (en.disconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            throw new RuntimeException("VM Disconnected before requested event occurred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        return en.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    private StepEvent doStep(ThreadReference thread, int gran, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        final StepRequest sr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                  requestManager.createStepRequest(thread, gran, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        sr.addClassExclusionFilter("java.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        sr.addClassExclusionFilter("sun.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        sr.addClassExclusionFilter("com.sun.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        sr.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        sr.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        StepEvent retEvent = (StepEvent)waitForRequestedEvent(sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        requestManager.deleteEventRequest(sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        return retEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    public StepEvent stepIntoInstruction(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        return doStep(thread, StepRequest.STEP_MIN, StepRequest.STEP_INTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    public StepEvent stepIntoLine(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        return doStep(thread, StepRequest.STEP_LINE, StepRequest.STEP_INTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    public StepEvent stepOverInstruction(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        return doStep(thread, StepRequest.STEP_MIN, StepRequest.STEP_OVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    public StepEvent stepOverLine(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        return doStep(thread, StepRequest.STEP_LINE, StepRequest.STEP_OVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    public StepEvent stepOut(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        return doStep(thread, StepRequest.STEP_LINE, StepRequest.STEP_OUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public BreakpointEvent resumeTo(Location loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        final BreakpointRequest request =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            requestManager.createBreakpointRequest(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        request.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        request.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        return (BreakpointEvent)waitForRequestedEvent(request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    public ReferenceType findReferenceType(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        List rts = vm.classesByName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        Iterator iter = rts.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            ReferenceType rt = (ReferenceType)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            if (rt.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                return rt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    public Method findMethod(ReferenceType rt, String name, String signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        List methods = rt.methods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        Iterator iter = methods.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            Method method = (Method)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (method.name().equals(name) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                method.signature().equals(signature)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    public Location findLocation(ReferenceType rt, int lineNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                         throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        List locs = rt.locationsOfLine(lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (locs.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            throw new IllegalArgumentException("Bad line number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        } else if (locs.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            throw new IllegalArgumentException("Line number has multiple locations");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        return (Location)locs.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    public BreakpointEvent resumeTo(String clsName, String methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                                         String methodSignature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        ReferenceType rt = findReferenceType(clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (rt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            rt = resumeToPrepareOf(clsName).referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        Method method = findMethod(rt, methodName, methodSignature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            throw new IllegalArgumentException("Bad method name/signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        return resumeTo(method.location());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    public BreakpointEvent resumeTo(String clsName, int lineNumber) throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        ReferenceType rt = findReferenceType(clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (rt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            rt = resumeToPrepareOf(clsName).referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        return resumeTo(findLocation(rt, lineNumber));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    public ClassPrepareEvent resumeToPrepareOf(String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        final ClassPrepareRequest request =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            requestManager.createClassPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        request.addClassFilter(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        request.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        request.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        return (ClassPrepareEvent)waitForRequestedEvent(request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    public void resumeForMsecs(long msecs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            addListener (this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        } catch (Exception ex){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            testFailed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            vm().resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        } catch (VMDisconnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if (!vmDisconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                System.out.println("Sleeping for " + msecs + " milleseconds");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                Thread.sleep(msecs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                vm().suspend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    public void resumeToVMDisconnect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            traceln("TS: resumeToVMDisconnect: vm.resume called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        } catch (VMDisconnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            // clean up below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        waitForVMDisconnect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    public void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        shutdown(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    public void shutdown(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        traceln("TS: shutdown: vmDied= " + vmDied +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                 ", vmDisconnected= " + vmDisconnected +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                 ", connection = " + connection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if ((connection != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                connection.disposeVM();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
             } catch (VMDisconnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                // Shutting down after the VM has gone away. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                // not an error, and we just ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            traceln("TS: shutdown: disposeVM not called");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (message != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        vmDied = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        vmDisconnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
}