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