jdk/test/com/sun/jdi/JDIScaffold.java
author mikejwre
Thu, 25 Mar 2010 15:05:15 -0700
changeset 5074 9c9bfe8f3a47
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Added tag jdk7-b87 for changeset 8367da959bab
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 1999-2001 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Framework used by all JDI regression tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
abstract public class JDIScaffold {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private boolean shouldTrace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private VMConnection connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private VirtualMachine vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private EventRequestManager requestManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private List listeners = Collections.synchronizedList(new LinkedList());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    ThreadReference vmStartThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    boolean vmDied = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    boolean vmDisconnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static private class ArgInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        String targetVMArgs = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        String targetAppCommandLine = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        String connectorSpec = "com.sun.jdi.CommandLineLaunch:";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        int traceFlags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static public interface TargetListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        boolean eventSetReceived(EventSet set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        boolean eventSetComplete(EventSet set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        boolean eventReceived(Event event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        boolean breakpointReached(BreakpointEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        boolean exceptionThrown(ExceptionEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        boolean stepCompleted(StepEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        boolean classPrepared(ClassPrepareEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        boolean classUnloaded(ClassUnloadEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        boolean methodEntered(MethodEntryEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        boolean methodExited(MethodExitEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        boolean fieldAccessed(AccessWatchpointEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        boolean fieldModified(ModificationWatchpointEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        boolean threadStarted(ThreadStartEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        boolean threadDied(ThreadDeathEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        boolean vmStarted(VMStartEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        boolean vmDied(VMDeathEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        boolean vmDisconnected(VMDisconnectEvent event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static public class TargetAdapter implements TargetListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        public boolean eventSetReceived(EventSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        public boolean eventSetComplete(EventSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public boolean eventReceived(Event event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public boolean breakpointReached(BreakpointEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public boolean exceptionThrown(ExceptionEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public boolean stepCompleted(StepEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public boolean classPrepared(ClassPrepareEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        public boolean classUnloaded(ClassUnloadEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        public boolean methodEntered(MethodEntryEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public boolean methodExited(MethodExitEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public boolean fieldAccessed(AccessWatchpointEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        public boolean fieldModified(ModificationWatchpointEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        public boolean threadStarted(ThreadStartEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        public boolean threadDied(ThreadDeathEvent event) {
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
        public boolean vmStarted(VMStartEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public boolean vmDied(VMDeathEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public boolean vmDisconnected(VMDisconnectEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private class EventHandler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        EventHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            Thread thread = new Thread(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            thread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        private boolean notifyEvent(TargetListener listener, Event event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (listener.eventReceived(event) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else if (event instanceof BreakpointEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return listener.breakpointReached((BreakpointEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else if (event instanceof ExceptionEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                return listener.exceptionThrown((ExceptionEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } else if (event instanceof StepEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                return listener.stepCompleted((StepEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } else if (event instanceof ClassPrepareEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                return listener.classPrepared((ClassPrepareEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } else if (event instanceof ClassUnloadEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                return listener.classUnloaded((ClassUnloadEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            } else if (event instanceof MethodEntryEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                return listener.methodEntered((MethodEntryEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            } else if (event instanceof MethodExitEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return listener.methodExited((MethodExitEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } else if (event instanceof AccessWatchpointEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return listener.fieldAccessed((AccessWatchpointEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } else if (event instanceof ModificationWatchpointEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                return listener.fieldModified((ModificationWatchpointEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } else if (event instanceof ThreadStartEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                return listener.threadStarted((ThreadStartEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } else if (event instanceof ThreadDeathEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                return listener.threadDied((ThreadDeathEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } else if (event instanceof VMStartEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                return listener.vmStarted((VMStartEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } else if (event instanceof VMDeathEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                return listener.vmDied((VMDeathEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            } else if (event instanceof VMDisconnectEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return listener.vmDisconnected((VMDisconnectEvent)event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                throw new InternalError("Unknown event type: " + event.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        private void traceSuspendPolicy(int policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (shouldTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                switch (policy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                case EventRequest.SUSPEND_NONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    traceln("JDI: runloop: suspend = SUSPEND_NONE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                case EventRequest.SUSPEND_ALL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    traceln("JDI: runloop: suspend = SUSPEND_ALL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                case EventRequest.SUSPEND_EVENT_THREAD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    traceln("JDI: runloop: suspend = SUSPEND_EVENT_THREAD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            boolean connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    EventSet set = vm.eventQueue().remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    traceSuspendPolicy(set.suspendPolicy());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    synchronized (listeners) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        ListIterator iter = listeners.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                            TargetListener listener = (TargetListener)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                            traceln("JDI: runloop: listener = " + listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                            if (listener.eventSetReceived(set) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                Iterator jter = set.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                while (jter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                    Event event = (Event)jter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                    traceln("JDI: runloop:    event = " + event.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                    if (event instanceof VMDisconnectEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                        connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                    if (notifyEvent(listener, event) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                        iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                traceln("JDI: runloop:   end of events loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                if (listener.eventSetComplete(set) == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                    iter.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        traceln("JDI: runloop: end of listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                traceln("JDI: runloop: end of outer loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } while (connected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public JDIScaffold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public void enableScaffoldTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        this.shouldTrace = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public void disableScaffoldTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        this.shouldTrace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Test cases should implement tests in runTests and should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * initiate testing by calling run().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    abstract protected void runTests() throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    final public void startTests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            runTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    protected void println(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        System.err.println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    protected void traceln(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if (shouldTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private ArgInfo parseArgs(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        ArgInfo argInfo = new ArgInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        for (int i = 0; i < args.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (args[i].equals("-connect")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                argInfo.connectorSpec = args[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            } else if (args[i].equals("-trace")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                argInfo.traceFlags = Integer.decode(args[i]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            } else if (args[i].startsWith("-J")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                argInfo.targetVMArgs += (args[i].substring(2) + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                 * classpath can span two arguments so we need to handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                 * it specially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                if (args[i].equals("-J-classpath")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    argInfo.targetVMArgs += (args[i] + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                argInfo.targetAppCommandLine += (args[i] + ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return argInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public void connect(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        ArgInfo argInfo = parseArgs(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        argInfo.targetVMArgs += VMConnection.getDebuggeeVMOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        connection = new VMConnection(argInfo.connectorSpec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                      argInfo.traceFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (!connection.isLaunch()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                 "Listening and Attaching not yet supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         * Add a listener to track VM start/death/disconnection and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         * to update status fields accordingly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        addListener(new TargetAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                        public boolean vmStarted(VMStartEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                            traceln("JDI: listener1:  got VMStart");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                            synchronized(JDIScaffold.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                vmStartThread = event.thread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                JDIScaffold.this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        public boolean vmDied(VMDeathEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                            traceln("JDI: listener1:  got VMDeath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                            synchronized(JDIScaffold.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                vmDied = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                JDIScaffold.this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        public boolean vmDisconnected(VMDisconnectEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                            traceln("JDI: listener1:  got VMDisconnectedEvent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                            synchronized(JDIScaffold.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                vmDisconnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                JDIScaffold.this.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (connection.connector().name().equals("com.sun.jdi.CommandLineLaunch")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (argInfo.targetVMArgs.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (connection.connectorArg("options").length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    throw new IllegalArgumentException("VM options in two places");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                connection.setConnectorArg("options", argInfo.targetVMArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (argInfo.targetAppCommandLine.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                if (connection.connectorArg("main").length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    throw new IllegalArgumentException("Command line in two places");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                connection.setConnectorArg("main", argInfo.targetAppCommandLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        vm = connection.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        requestManager = vm.eventRequestManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        new EventHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public VirtualMachine vm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        return vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public EventRequestManager eventRequestManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return requestManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public void addListener(TargetListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        listeners.add(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public void removeListener(TargetListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        listeners.remove(listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public synchronized ThreadReference waitForVMStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        while ((vmStartThread == null) && !vmDisconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (vmStartThread == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throw new VMDisconnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return vmStartThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public synchronized void waitForVMDeath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        while (!vmDied && !vmDisconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                traceln("JDI: waitForVMDeath:  waiting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        traceln("JDI: waitForVMDeath:  done waiting");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (!vmDied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            throw new VMDisconnectedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public Event waitForRequestedEvent(final EventRequest request) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        class EventNotification {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            Event event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            boolean disconnected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        final EventNotification en = new EventNotification();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        TargetAdapter adapter = new TargetAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            public boolean eventReceived(Event event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                if (request.equals(event.request())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    synchronized (en) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        en.event = event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                        en.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                } else if (event instanceof VMDisconnectEvent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    synchronized (en) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        en.disconnected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                        en.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        addListener(adapter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            synchronized (en) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                while (!en.disconnected && (en.event == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    en.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        } catch (InterruptedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (en.disconnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            throw new RuntimeException("VM Disconnected before requested event occurred");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return en.event;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    private StepEvent doStep(ThreadReference thread, int gran, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        final StepRequest sr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                  requestManager.createStepRequest(thread, gran, depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        sr.addClassExclusionFilter("java.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        sr.addClassExclusionFilter("sun.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        sr.addClassExclusionFilter("com.sun.*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        sr.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        sr.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        StepEvent retEvent = (StepEvent)waitForRequestedEvent(sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        requestManager.deleteEventRequest(sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return retEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public StepEvent stepIntoInstruction(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return doStep(thread, StepRequest.STEP_MIN, StepRequest.STEP_INTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public StepEvent stepIntoLine(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        return doStep(thread, StepRequest.STEP_LINE, StepRequest.STEP_INTO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public StepEvent stepOverInstruction(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return doStep(thread, StepRequest.STEP_MIN, StepRequest.STEP_OVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public StepEvent stepOverLine(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        return doStep(thread, StepRequest.STEP_LINE, StepRequest.STEP_OVER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    public StepEvent stepOut(ThreadReference thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return doStep(thread, StepRequest.STEP_LINE, StepRequest.STEP_OUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public BreakpointEvent resumeTo(Location loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        final BreakpointRequest request =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            requestManager.createBreakpointRequest(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        request.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        request.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        return (BreakpointEvent)waitForRequestedEvent(request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public ReferenceType findReferenceType(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        List rts = vm.classesByName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        Iterator iter = rts.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            ReferenceType rt = (ReferenceType)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            if (rt.name().equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                return rt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    public Method findMethod(ReferenceType rt, String name, String signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        List methods = rt.methods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        Iterator iter = methods.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        while (iter.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            Method method = (Method)iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            if (method.name().equals(name) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                method.signature().equals(signature)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public Location findLocation(ReferenceType rt, int lineNumber)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                         throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        List locs = rt.locationsOfLine(lineNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (locs.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            throw new IllegalArgumentException("Bad line number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        } else if (locs.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            throw new IllegalArgumentException("Line number has multiple locations");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return (Location)locs.get(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public BreakpointEvent resumeTo(String clsName, String methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                         String methodSignature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        ReferenceType rt = findReferenceType(clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        if (rt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            rt = resumeToPrepareOf(clsName).referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        Method method = findMethod(rt, methodName, methodSignature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            throw new IllegalArgumentException("Bad method name/signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return resumeTo(method.location());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public BreakpointEvent resumeTo(String clsName, int lineNumber) throws AbsentInformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        ReferenceType rt = findReferenceType(clsName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (rt == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            rt = resumeToPrepareOf(clsName).referenceType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return resumeTo(findLocation(rt, lineNumber));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public ClassPrepareEvent resumeToPrepareOf(String className) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        final ClassPrepareRequest request =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            requestManager.createClassPrepareRequest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        request.addClassFilter(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        request.addCountFilter(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        request.enable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return (ClassPrepareEvent)waitForRequestedEvent(request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public void resumeToVMDeath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        // If we are very close to VM death, we might get a VM disconnect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        // before resume is complete. In that case ignore any VMDisconnectException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        // and let the waitForVMDeath to clean up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            traceln("JDI: resumeToVMDeath:  resuming");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            traceln("JDI: resumeToVMDeath:  resumed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        } catch (VMDisconnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            // clean up below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        waitForVMDeath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        shutdown(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    public void shutdown(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if ((connection != null) && !vmDied) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                connection.disposeVM();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            } catch (VMDisconnectedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                // Shutting down after the VM has gone away. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                // not an error, and we just ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (message != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
}